Skip to content

Commit 0532190

Browse files
Merge pull request #45 from OpenPrinting/xdg-changes
Modernize user and system configuration directory handling
2 parents 17316a3 + f6fb1ec commit 0532190

10 files changed

Lines changed: 166 additions & 245 deletions

File tree

cups/auth.c

Lines changed: 5 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -540,16 +540,14 @@ cups_local_auth(http_t *http) /* I - HTTP connection to server */
540540

541541
return (1);
542542
#else
543-
int pid; /* Current process ID */
544-
FILE *fp; /* Certificate file */
545-
char trc[16], /* Try Root Certificate parameter */
546-
filename[1024]; /* Certificate filename */
547-
const char *www_auth, /* WWW-Authenticate header */
548-
*schemedata; /* Data for the named auth scheme */
543+
# ifdef DEBUG
544+
char hostaddr[256]; // Host address string
545+
# endif // DEBUG
546+
const char *www_auth; /* WWW-Authenticate header */
549547
_cups_globals_t *cg = _cupsGlobals(); /* Global data */
550548

551549

552-
DEBUG_printf(("7cups_local_auth(http=%p) hostaddr=%s, hostname=\"%s\"", (void *)http, httpAddrGetString(http->hostaddr, filename, sizeof(filename)), http->hostname));
550+
DEBUG_printf(("7cups_local_auth(http=%p) hostaddr=%s, hostname=\"%s\"", (void *)http, httpAddrGetString(http->hostaddr, hostaddr, sizeof(hostaddr)), http->hostname));
553551

554552
/*
555553
* See if we are accessing localhost...
@@ -597,65 +595,6 @@ cups_local_auth(http_t *http) /* I - HTTP connection to server */
597595
}
598596
# endif /* SO_PEERCRED && AF_LOCAL */
599597

600-
if ((schemedata = cups_auth_find(www_auth, "Local")) == NULL)
601-
return (1);
602-
603-
/*
604-
* Try opening a certificate file for this PID. If that fails,
605-
* try the root certificate...
606-
*/
607-
608-
pid = getpid();
609-
snprintf(filename, sizeof(filename), "%s/certs/%d", cg->cups_statedir, pid);
610-
if ((fp = fopen(filename, "r")) == NULL && pid > 0)
611-
{
612-
/*
613-
* No certificate for this PID; see if we can get the root certificate...
614-
*/
615-
616-
DEBUG_printf(("9cups_local_auth: Unable to open file \"%s\": %s", filename, strerror(errno)));
617-
618-
if (!cups_auth_param(schemedata, "trc", trc, sizeof(trc)))
619-
{
620-
/*
621-
* Scheduler doesn't want us to use the root certificate...
622-
*/
623-
624-
return (1);
625-
}
626-
627-
snprintf(filename, sizeof(filename), "%s/certs/0", cg->cups_statedir);
628-
if ((fp = fopen(filename, "r")) == NULL)
629-
DEBUG_printf(("9cups_local_auth: Unable to open file \"%s\": %s", filename, strerror(errno)));
630-
}
631-
632-
if (fp)
633-
{
634-
/*
635-
* Read the certificate from the file...
636-
*/
637-
638-
char certificate[33], /* Certificate string */
639-
*certptr; /* Pointer to certificate string */
640-
641-
certptr = fgets(certificate, sizeof(certificate), fp);
642-
fclose(fp);
643-
644-
if (certptr)
645-
{
646-
/*
647-
* Set the authorization string and return...
648-
*/
649-
650-
httpSetAuthString(http, "Local", certificate);
651-
652-
DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"",
653-
http->authstring));
654-
655-
return (0);
656-
}
657-
}
658-
659598
return (1);
660599
#endif /* _WIN32 || __EMX__ */
661600
}

cups/cups-private.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,9 @@ typedef enum _cups_uatokens_e /**** UserAgentTokens values */
7575
typedef struct _cups_globals_s /**** CUPS global state data ****/
7676
{
7777
/* Multiple places... */
78-
const char *cups_datadir, /* CUPS_DATADIR environment var */
79-
*cups_serverbin,/* CUPS_SERVERBIN environment var */
80-
*cups_serverroot,
81-
/* CUPS_SERVERROOT environment var */
82-
*cups_statedir, /* CUPS_STATEDIR environment var */
83-
*home, /* HOME environment var */
84-
*localedir; /* LOCALDIR environment var */
78+
const char *datadir, // Data directory (CUPS_DATADIR environment var)
79+
*sysconfig, // System config files (CUPS_SERVERROOT environment var)
80+
*userconfig; // User-specific config files (various environment vars)
8581
#ifndef _WIN32
8682
#define PW_BUF_SIZE 16384 /* As per glibc manual page */
8783
char pw_buf[PW_BUF_SIZE];

cups/dest.c

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,19 +1578,13 @@ cupsGetNamedDest(http_t *http, /* I - Connection to server or @code CUPS_HTT
15781578
instance = NULL;
15791579
}
15801580
}
1581-
else if (cg->home)
1581+
else if (cg->userconfig)
15821582
{
15831583
/*
15841584
* No default in the environment, try the user's lpoptions files...
15851585
*/
15861586

1587-
#if _WIN32
1588-
snprintf(filename, sizeof(filename), "%s/AppData/Local/cups/lpoptions", cg->home);
1589-
#elif defined(__APPLE__)
1590-
snprintf(filename, sizeof(filename), "%s/Library/Application Support/cups/lpoptions", cg->home);
1591-
#else
1592-
snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", cg->home);
1593-
#endif // _WIN32
1587+
snprintf(filename, sizeof(filename), "%s/lpoptions", cg->userconfig);
15941588

15951589
dest_name = cups_get_default(filename, defname, sizeof(defname), &instance);
15961590

@@ -1604,7 +1598,7 @@ cupsGetNamedDest(http_t *http, /* I - Connection to server or @code CUPS_HTT
16041598
* Still not there? Try the system lpoptions file...
16051599
*/
16061600

1607-
snprintf(filename, sizeof(filename), "%s/lpoptions", cg->cups_serverroot);
1601+
snprintf(filename, sizeof(filename), "%s/lpoptions", cg->sysconfig);
16081602
dest_name = cups_get_default(filename, defname, sizeof(defname), &instance);
16091603

16101604
if (dest_name)
@@ -1696,18 +1690,12 @@ cupsGetNamedDest(http_t *http, /* I - Connection to server or @code CUPS_HTT
16961690
* Then add local options...
16971691
*/
16981692

1699-
snprintf(filename, sizeof(filename), "%s/lpoptions", cg->cups_serverroot);
1693+
snprintf(filename, sizeof(filename), "%s/lpoptions", cg->sysconfig);
17001694
cups_get_dests(filename, dest_name, instance, 0, 1, 1, &dest);
17011695

1702-
if (cg->home)
1696+
if (cg->userconfig)
17031697
{
1704-
#if _WIN32
1705-
snprintf(filename, sizeof(filename), "%s/AppData/Local/cups/lpoptions", cg->home);
1706-
#elif defined(__APPLE__)
1707-
snprintf(filename, sizeof(filename), "%s/Library/Application Support/cups/lpoptions", cg->home);
1708-
#else
1709-
snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", cg->home);
1710-
#endif // _WIN32
1698+
snprintf(filename, sizeof(filename), "%s/lpoptions", cg->userconfig);
17111699

17121700
cups_get_dests(filename, dest_name, instance, 0, 1, 1, &dest);
17131701
}
@@ -1853,34 +1841,21 @@ cupsSetDests(http_t *http, /* I - Connection to server or @code CUPS_HTTP_
18531841
* Figure out which file to write to...
18541842
*/
18551843

1856-
snprintf(filename, sizeof(filename), "%s/lpoptions", cg->cups_serverroot);
1844+
snprintf(filename, sizeof(filename), "%s/lpoptions", cg->sysconfig);
18571845

1858-
if (cg->home)
1846+
if (cg->userconfig)
18591847
{
18601848
/*
1861-
* Create ~/.cups subdirectory...
1849+
* Create user subdirectory...
18621850
*/
18631851

1864-
#if _WIN32
1865-
snprintf(filename, sizeof(filename), "%s/AppData/Local/cups", cg->home);
1866-
#elif defined(__APPLE__)
1867-
snprintf(filename, sizeof(filename), "%s/Library/Application Support/cups", cg->home);
1868-
#else
1869-
snprintf(filename, sizeof(filename), "%s/.cups", cg->home);
1870-
#endif // _WIN32
1871-
if (mkdir(filename, 0700) && errno != EEXIST)
1852+
if (mkdir(cg->userconfig, 0700) && errno != EEXIST)
18721853
{
18731854
cupsFreeDests(num_temps, temps);
18741855
return (false);
18751856
}
18761857

1877-
#if _WIN32
1878-
snprintf(filename, sizeof(filename), "%s/AppData/Local/cups/lpoptions", cg->home);
1879-
#elif defined(__APPLE__)
1880-
snprintf(filename, sizeof(filename), "%s/Library/Application Support/cups/lpoptions", cg->home);
1881-
#else
1882-
snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", cg->home);
1883-
#endif // _WIN32
1858+
snprintf(filename, sizeof(filename), "%s/lpoptions", cg->userconfig);
18841859
}
18851860

18861861
/*
@@ -3016,18 +2991,12 @@ cups_enum_dests(
30162991

30172992
user_default = _cupsGetUserDefault(data.def_name, sizeof(data.def_name));
30182993

3019-
snprintf(filename, sizeof(filename), "%s/lpoptions", cg->cups_serverroot);
2994+
snprintf(filename, sizeof(filename), "%s/lpoptions", cg->sysconfig);
30202995
data.num_dests = cups_get_dests(filename, NULL, NULL, 1, user_default != NULL, data.num_dests, &data.dests);
30212996

3022-
if (cg->home)
2997+
if (cg->userconfig)
30232998
{
3024-
#if _WIN32
3025-
snprintf(filename, sizeof(filename), "%s/AppData/Local/cups/lpoptions", cg->home);
3026-
#elif defined(__APPLE__)
3027-
snprintf(filename, sizeof(filename), "%s/Library/Application Support/cups/lpoptions", cg->home);
3028-
#else
3029-
snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", cg->home);
3030-
#endif // _WIN32
2999+
snprintf(filename, sizeof(filename), "%s/lpoptions", cg->userconfig);
30313000

30323001
data.num_dests = cups_get_dests(filename, NULL, NULL, 1, user_default != NULL, data.num_dests, &data.dests);
30333002
}

0 commit comments

Comments
 (0)