]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/conf.c
Load cups into easysw/current.
[thirdparty/cups.git] / scheduler / conf.c
CommitLineData
ef416fc2 1/*
e1d6a774 2 * "$Id: conf.c 5289 2006-03-14 11:54:45Z mike $"
ef416fc2 3 *
4 * Configuration routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
26 * cupsdReadConfiguration() - Read the cupsd.conf file.
fa73b229 27 * check_permissions() - Fix the mode and ownership of a file or
28 * directory.
ef416fc2 29 * get_address() - Get an address + port number from a line.
30 * get_addr_and_mask() - Get an IP address and netmask.
31 * parse_aaa() - Parse authentication, authorization, and
32 * access control lines.
bd7854cb 33 * parse_groups() - Parse system group names in a string.
34 * parse_protocols() - Parse browse protocols in a string.
ef416fc2 35 * read_configuration() - Read a configuration file.
36 * read_location() - Read a <Location path> definition.
37 * read_policy() - Read a <Policy name> definition.
38 */
39
40/*
41 * Include necessary headers...
42 */
43
44#include "cupsd.h"
45#include <stdarg.h>
46#include <grp.h>
47#include <sys/utsname.h>
48#include <cups/dir.h>
49
50#ifdef HAVE_VSYSLOG
51# include <syslog.h>
52#endif /* HAVE_VSYSLOG */
53
54
55/*
56 * Possibly missing network definitions...
57 */
58
59#ifndef INADDR_NONE
60# define INADDR_NONE 0xffffffff
61#endif /* !INADDR_NONE */
62
63
64/*
65 * Configuration variable structure...
66 */
67
68typedef enum
69{
70 CUPSD_VARTYPE_INTEGER, /* Integer option */
71 CUPSD_VARTYPE_STRING, /* String option */
72 CUPSD_VARTYPE_BOOLEAN /* Boolean option */
73} cupsd_vartype_t;
74
75typedef struct
76{
77 char *name; /* Name of variable */
78 void *ptr; /* Pointer to variable */
79 cupsd_vartype_t type; /* Type (int, string, address) */
80} cupsd_var_t;
81
82
83/*
84 * Local globals...
85 */
86
87static cupsd_var_t variables[] =
88{
89 { "AccessLog", &AccessLog, CUPSD_VARTYPE_STRING },
90 { "AutoPurgeJobs", &JobAutoPurge, CUPSD_VARTYPE_BOOLEAN },
91 { "BrowseInterval", &BrowseInterval, CUPSD_VARTYPE_INTEGER },
b423cd4c 92#ifdef HAVE_LDAP
93 { "BrowseLDAPBindDN", &BrowseLDAPBindDN, CUPSD_VARTYPE_STRING },
94 { "BrowseLDAPDN", &BrowseLDAPDN, CUPSD_VARTYPE_STRING },
95 { "BrowseLDAPPassword", &BrowseLDAPPassword, CUPSD_VARTYPE_STRING },
96 { "BrowseLDAPServer", &BrowseLDAPServer, CUPSD_VARTYPE_STRING },
97#endif /* HAVE_LDAP */
ef416fc2 98 { "BrowseLocalOptions", &BrowseLocalOptions, CUPSD_VARTYPE_STRING },
99 { "BrowsePort", &BrowsePort, CUPSD_VARTYPE_INTEGER },
100 { "BrowseRemoteOptions", &BrowseRemoteOptions, CUPSD_VARTYPE_STRING },
101 { "BrowseShortNames", &BrowseShortNames, CUPSD_VARTYPE_BOOLEAN },
102 { "BrowseTimeout", &BrowseTimeout, CUPSD_VARTYPE_INTEGER },
103 { "Browsing", &Browsing, CUPSD_VARTYPE_BOOLEAN },
104 { "CacheDir", &CacheDir, CUPSD_VARTYPE_STRING },
105 { "Classification", &Classification, CUPSD_VARTYPE_STRING },
106 { "ClassifyOverride", &ClassifyOverride, CUPSD_VARTYPE_BOOLEAN },
107 { "ConfigFilePerm", &ConfigFilePerm, CUPSD_VARTYPE_INTEGER },
108 { "DataDir", &DataDir, CUPSD_VARTYPE_STRING },
109 { "DefaultCharset", &DefaultCharset, CUPSD_VARTYPE_STRING },
110 { "DefaultLanguage", &DefaultLanguage, CUPSD_VARTYPE_STRING },
111 { "DefaultLeaseDuration", &DefaultLeaseDuration, CUPSD_VARTYPE_INTEGER },
112 { "DefaultPolicy", &DefaultPolicy, CUPSD_VARTYPE_STRING },
fa73b229 113 { "DefaultShared", &DefaultShared, CUPSD_VARTYPE_BOOLEAN },
ef416fc2 114 { "DocumentRoot", &DocumentRoot, CUPSD_VARTYPE_STRING },
115 { "ErrorLog", &ErrorLog, CUPSD_VARTYPE_STRING },
116 { "FileDevice", &FileDevice, CUPSD_VARTYPE_BOOLEAN },
117 { "FilterLimit", &FilterLimit, CUPSD_VARTYPE_INTEGER },
118 { "FilterNice", &FilterNice, CUPSD_VARTYPE_INTEGER },
119 { "FontPath", &FontPath, CUPSD_VARTYPE_STRING },
120 { "HideImplicitMembers", &HideImplicitMembers, CUPSD_VARTYPE_BOOLEAN },
121 { "ImplicitClasses", &ImplicitClasses, CUPSD_VARTYPE_BOOLEAN },
122 { "ImplicitAnyClasses", &ImplicitAnyClasses, CUPSD_VARTYPE_BOOLEAN },
123 { "JobRetryLimit", &JobRetryLimit, CUPSD_VARTYPE_INTEGER },
124 { "JobRetryInterval", &JobRetryInterval, CUPSD_VARTYPE_INTEGER },
125 { "KeepAliveTimeout", &KeepAliveTimeout, CUPSD_VARTYPE_INTEGER },
126 { "KeepAlive", &KeepAlive, CUPSD_VARTYPE_BOOLEAN },
127 { "LimitRequestBody", &MaxRequestSize, CUPSD_VARTYPE_INTEGER },
128 { "ListenBackLog", &ListenBackLog, CUPSD_VARTYPE_INTEGER },
129 { "LogFilePerm", &LogFilePerm, CUPSD_VARTYPE_INTEGER },
130 { "MaxActiveJobs", &MaxActiveJobs, CUPSD_VARTYPE_INTEGER },
131 { "MaxClients", &MaxClients, CUPSD_VARTYPE_INTEGER },
132 { "MaxClientsPerHost", &MaxClientsPerHost, CUPSD_VARTYPE_INTEGER },
133 { "MaxCopies", &MaxCopies, CUPSD_VARTYPE_INTEGER },
134 { "MaxEvents", &MaxEvents, CUPSD_VARTYPE_INTEGER },
135 { "MaxJobs", &MaxJobs, CUPSD_VARTYPE_INTEGER },
136 { "MaxJobsPerPrinter", &MaxJobsPerPrinter, CUPSD_VARTYPE_INTEGER },
137 { "MaxJobsPerUser", &MaxJobsPerUser, CUPSD_VARTYPE_INTEGER },
138 { "MaxLeaseDuration", &MaxLeaseDuration, CUPSD_VARTYPE_INTEGER },
139 { "MaxLogSize", &MaxLogSize, CUPSD_VARTYPE_INTEGER },
140 { "MaxPrinterHistory", &MaxPrinterHistory, CUPSD_VARTYPE_INTEGER },
141 { "MaxRequestSize", &MaxRequestSize, CUPSD_VARTYPE_INTEGER },
142 { "MaxSubscriptions", &MaxSubscriptions, CUPSD_VARTYPE_INTEGER },
143 { "MaxSubscriptionsPerJob", &MaxSubscriptionsPerJob, CUPSD_VARTYPE_INTEGER },
144 { "MaxSubscriptionsPerPrinter",&MaxSubscriptionsPerPrinter, CUPSD_VARTYPE_INTEGER },
145 { "MaxSubscriptionsPerUser", &MaxSubscriptionsPerUser, CUPSD_VARTYPE_INTEGER },
146 { "PageLog", &PageLog, CUPSD_VARTYPE_STRING },
147 { "PreserveJobFiles", &JobFiles, CUPSD_VARTYPE_BOOLEAN },
148 { "PreserveJobHistory", &JobHistory, CUPSD_VARTYPE_BOOLEAN },
149 { "Printcap", &Printcap, CUPSD_VARTYPE_STRING },
150 { "PrintcapGUI", &PrintcapGUI, CUPSD_VARTYPE_STRING },
151 { "ReloadTimeout", &ReloadTimeout, CUPSD_VARTYPE_INTEGER },
152 { "RemoteRoot", &RemoteRoot, CUPSD_VARTYPE_STRING },
153 { "RequestRoot", &RequestRoot, CUPSD_VARTYPE_STRING },
154 { "RIPCache", &RIPCache, CUPSD_VARTYPE_STRING },
ef416fc2 155 { "RootCertDuration", &RootCertDuration, CUPSD_VARTYPE_INTEGER },
156 { "ServerAdmin", &ServerAdmin, CUPSD_VARTYPE_STRING },
157 { "ServerBin", &ServerBin, CUPSD_VARTYPE_STRING },
158#ifdef HAVE_SSL
159 { "ServerCertificate", &ServerCertificate, CUPSD_VARTYPE_STRING },
160# if defined(HAVE_LIBSSL) || defined(HAVE_GNUTLS)
161 { "ServerKey", &ServerKey, CUPSD_VARTYPE_STRING },
162# endif /* HAVE_LIBSSL || HAVE_GNUTLS */
163#endif /* HAVE_SSL */
a4d04587 164#ifdef HAVE_LAUNCHD
165 { "LaunchdTimeout", &LaunchdTimeout, CUPSD_VARTYPE_INTEGER },
166 { "LaunchdConf", &LaunchdConf, CUPSD_VARTYPE_STRING },
167#endif /* HAVE_LAUNCHD */
ef416fc2 168 { "ServerName", &ServerName, CUPSD_VARTYPE_STRING },
169 { "ServerRoot", &ServerRoot, CUPSD_VARTYPE_STRING },
170 { "StateDir", &StateDir, CUPSD_VARTYPE_STRING },
171 { "TempDir", &TempDir, CUPSD_VARTYPE_STRING },
e00b005a 172 { "Timeout", &Timeout, CUPSD_VARTYPE_INTEGER },
173 { "UseNetworkDefault", &UseNetworkDefault, CUPSD_VARTYPE_BOOLEAN }
ef416fc2 174};
175#define NUM_VARS (sizeof(variables) / sizeof(variables[0]))
176
177
178static unsigned ones[4] =
179 {
180 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff
181 };
182static unsigned zeros[4] =
183 {
184 0x00000000, 0x00000000, 0x00000000, 0x00000000
185 };
186
187
188/*
189 * Local functions...
190 */
fa73b229 191static int check_permissions(const char *filename,
192 const char *suffix, int mode,
193 int user, int group, int is_dir,
194 int create_dir);
ef416fc2 195static http_addrlist_t *get_address(const char *value, int defport);
196static int get_addr_and_mask(const char *value, unsigned *ip,
197 unsigned *mask);
198static int parse_aaa(cupsd_location_t *loc, char *line,
199 char *value, int linenum);
bd7854cb 200static int parse_groups(const char *s);
201static int parse_protocols(const char *s);
ef416fc2 202static int read_configuration(cups_file_t *fp);
203static int read_location(cups_file_t *fp, char *name, int linenum);
204static int read_policy(cups_file_t *fp, char *name, int linenum);
205
206
207/*
208 * 'cupsdReadConfiguration()' - Read the cupsd.conf file.
209 */
210
211int /* O - 1 on success, 0 otherwise */
212cupsdReadConfiguration(void)
213{
214 int i; /* Looping var */
215 cups_file_t *fp; /* Configuration file */
216 int status; /* Return status */
217 char temp[1024], /* Temporary buffer */
218 *slash; /* Directory separator */
ef416fc2 219 cups_lang_t *language; /* Language */
220 struct passwd *user; /* Default user */
221 struct group *group; /* Default group */
222 char *old_serverroot, /* Old ServerRoot */
223 *old_requestroot; /* Old RequestRoot */
b423cd4c 224 const char *tmpdir; /* TMPDIR environment variable */
225 struct stat tmpinfo; /* Temporary directory info */
226
ef416fc2 227
ef416fc2 228 /*
229 * Save the old root paths...
230 */
231
232 old_serverroot = NULL;
233 cupsdSetString(&old_serverroot, ServerRoot);
234 old_requestroot = NULL;
235 cupsdSetString(&old_requestroot, RequestRoot);
236
237 /*
238 * Reset the server configuration data...
239 */
240
241 cupsdDeleteAllLocations();
242
243 if (NumBrowsers > 0)
244 {
245 free(Browsers);
246
247 NumBrowsers = 0;
248 }
249
250 if (NumPolled > 0)
251 {
252 free(Polled);
253
254 NumPolled = 0;
255 }
256
257 if (NumRelays > 0)
258 {
259 for (i = 0; i < NumRelays; i ++)
260 if (Relays[i].from.type == AUTH_NAME)
261 free(Relays[i].from.mask.name.name);
262
263 free(Relays);
264
265 NumRelays = 0;
266 }
267
bd7854cb 268 cupsdDeleteAllListeners();
ef416fc2 269
270 /*
271 * String options...
272 */
273
757d2cad 274 cupsdSetString(&ServerName, httpGetHostname(NULL, temp, sizeof(temp)));
ef416fc2 275 cupsdSetStringf(&ServerAdmin, "root@%s", temp);
276 cupsdSetString(&ServerBin, CUPS_SERVERBIN);
277 cupsdSetString(&RequestRoot, CUPS_REQUESTS);
278 cupsdSetString(&CacheDir, CUPS_CACHEDIR);
279 cupsdSetString(&DataDir, CUPS_DATADIR);
280 cupsdSetString(&DocumentRoot, CUPS_DOCROOT);
281 cupsdSetString(&AccessLog, CUPS_LOGDIR "/access_log");
282 cupsdSetString(&ErrorLog, CUPS_LOGDIR "/error_log");
283 cupsdSetString(&PageLog, CUPS_LOGDIR "/page_log");
284 cupsdSetString(&Printcap, "/etc/printcap");
285 cupsdSetString(&PrintcapGUI, "/usr/bin/glpoptions");
286 cupsdSetString(&FontPath, CUPS_FONTPATH);
287 cupsdSetString(&RemoteRoot, "remroot");
bd7854cb 288 cupsdSetString(&ServerHeader, "CUPS/1.2");
ef416fc2 289 cupsdSetString(&StateDir, CUPS_STATEDIR);
290
291 strlcpy(temp, ConfigurationFile, sizeof(temp));
292 if ((slash = strrchr(temp, '/')) != NULL)
293 *slash = '\0';
294
295 cupsdSetString(&ServerRoot, temp);
296
297 cupsdClearString(&Classification);
298 ClassifyOverride = 0;
299
300#ifdef HAVE_SSL
301# ifdef HAVE_CDSASSL
fa73b229 302 cupsdSetString(&ServerCertificate, "/Library/Keychains/System.keychain");
ef416fc2 303# else
304 cupsdSetString(&ServerCertificate, "ssl/server.crt");
305 cupsdSetString(&ServerKey, "ssl/server.key");
306# endif /* HAVE_CDSASSL */
307#endif /* HAVE_SSL */
308
309 language = cupsLangDefault();
310
311 if (!strcmp(language->language, "C") || !strcmp(language->language, "POSIX"))
312 cupsdSetString(&DefaultLanguage, "en");
313 else
314 cupsdSetString(&DefaultLanguage, language->language);
315
316 cupsdSetString(&DefaultCharset, _cupsEncodingName(language->encoding));
317
318 cupsdSetString(&RIPCache, "8m");
319
b423cd4c 320 cupsdSetString(&TempDir, NULL);
ef416fc2 321
ef416fc2 322 /*
323 * Find the default user...
324 */
325
326 if ((user = getpwnam(CUPS_DEFAULT_USER)) != NULL)
327 User = user->pw_uid;
328 else
329 {
330 /*
331 * Use the (historical) NFS nobody user ID (-2 as a 16-bit twos-
332 * complement number...)
333 */
334
335 User = 65534;
336 }
337
338 endpwent();
339
340 /*
341 * Find the default group (nobody)...
342 */
343
344 group = getgrnam("nobody");
345 endgrent();
346
347 if (group != NULL)
348 Group = group->gr_gid;
349 else
350 {
351 /*
352 * Use the (historical) NFS nobody group ID (-2 as a 16-bit twos-
353 * complement number...)
354 */
355
356 Group = 65534;
357 }
358
359 /*
360 * Numeric options...
361 */
362
bd7854cb 363 ConfigFilePerm = CUPS_DEFAULT_CONFIG_FILE_PERM;
364 DefaultAuthType = AUTH_BASIC;
4744bd90 365#ifdef HAVE_SSL
366 DefaultEncryption = HTTP_ENCRYPT_REQUIRED;
367#endif /* HAVE_SSL */
bd7854cb 368 JobRetryLimit = 5;
369 JobRetryInterval = 300;
370 FileDevice = FALSE;
371 FilterLevel = 0;
372 FilterLimit = 0;
373 FilterNice = 0;
374 HostNameLookups = FALSE;
375 ImplicitClasses = CUPS_DEFAULT_IMPLICIT_CLASSES;
376 ImplicitAnyClasses = FALSE;
377 HideImplicitMembers = TRUE;
378 KeepAlive = TRUE;
379 KeepAliveTimeout = DEFAULT_KEEPALIVE;
380 ListenBackLog = SOMAXCONN;
381 LogFilePerm = CUPS_DEFAULT_LOG_FILE_PERM;
382 LogLevel = CUPSD_LOG_ERROR;
383 MaxClients = 100;
384 MaxClientsPerHost = 0;
385 MaxLogSize = 1024 * 1024;
386 MaxPrinterHistory = 10;
387 MaxRequestSize = 0;
388 ReloadTimeout = 60;
389 RootCertDuration = 300;
bd7854cb 390 Timeout = DEFAULT_TIMEOUT;
391 NumSystemGroups = 0;
ef416fc2 392
393 BrowseInterval = DEFAULT_INTERVAL;
394 BrowsePort = ippPort();
bd7854cb 395 BrowseLocalProtocols = parse_protocols(CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS);
396 BrowseRemoteProtocols = parse_protocols(CUPS_DEFAULT_BROWSE_REMOTE_PROTOCOLS);
09ec0018 397 BrowseShortNames = CUPS_DEFAULT_BROWSE_SHORT_NAMES;
ef416fc2 398 BrowseTimeout = DEFAULT_TIMEOUT;
09ec0018 399 Browsing = CUPS_DEFAULT_BROWSING;
400 DefaultShared = CUPS_DEFAULT_DEFAULT_SHARED;
ef416fc2 401
402 cupsdClearString(&BrowseLocalOptions);
403 cupsdClearString(&BrowseRemoteOptions);
404
b423cd4c 405#ifdef HAVE_LDAP
406 cupsdClearString(&BrowseLDAPBindDN);
407 cupsdClearString(&BrowseLDAPDN);
408 cupsdClearString(&BrowseLDAPPassword);
409 cupsdClearString(&BrowseLDAPServer);
410#endif /* HAVE_LDAP */
411
ef416fc2 412 JobHistory = DEFAULT_HISTORY;
413 JobFiles = DEFAULT_FILES;
414 JobAutoPurge = 0;
415 MaxJobs = 500;
416 MaxActiveJobs = 0;
417 MaxJobsPerUser = 0;
418 MaxJobsPerPrinter = 0;
419 MaxCopies = 100;
420
421 cupsdDeleteAllPolicies();
422 cupsdClearString(&DefaultPolicy);
423
424 MaxSubscriptions = 100;
425 MaxSubscriptionsPerJob = 0;
426 MaxSubscriptionsPerPrinter = 0;
427 MaxSubscriptionsPerUser = 0;
428 DefaultLeaseDuration = 86400;
429 MaxLeaseDuration = 0;
430
a4d04587 431#ifdef HAVE_LAUNCHD
432 LaunchdTimeout = DEFAULT_TIMEOUT + 10;
433 cupsdSetString(&LaunchdConf, CUPS_DEFAULT_LAUNCHD_CONF);
434#endif /* HAVE_LAUNCHD */
435
ef416fc2 436 /*
437 * Read the configuration file...
438 */
439
440 if ((fp = cupsFileOpen(ConfigurationFile, "r")) == NULL)
441 return (0);
442
443 status = read_configuration(fp);
444
445 cupsFileClose(fp);
446
447 if (!status)
448 return (0);
449
4744bd90 450 RunUser = getuid();
ef416fc2 451
452 /*
453 * Use the default system group if none was supplied in cupsd.conf...
454 */
455
456 if (NumSystemGroups == 0)
bd7854cb 457 {
458 if (!parse_groups(CUPS_DEFAULT_SYSTEM_GROUPS))
459 {
460 /*
461 * Find the group associated with GID 0...
462 */
463
464 group = getgrgid(0);
465 endgrent();
466
467 if (group != NULL)
468 cupsdSetString(&SystemGroups[0], group->gr_name);
469 else
470 cupsdSetString(&SystemGroups[0], "unknown");
471
472 SystemGroupIDs[0] = 0;
473 NumSystemGroups = 1;
474 }
475 }
ef416fc2 476
477 /*
478 * Get the access control list for browsing...
479 */
480
481 BrowseACL = cupsdFindLocation("CUPS_INTERNAL_BROWSE_ACL");
482
483 /*
484 * Open the system log for cupsd if necessary...
485 */
486
487#ifdef HAVE_VSYSLOG
488 if (!strcmp(AccessLog, "syslog") ||
489 !strcmp(ErrorLog, "syslog") ||
490 !strcmp(PageLog, "syslog"))
491 openlog("cupsd", LOG_PID | LOG_NOWAIT | LOG_NDELAY, LOG_LPR);
492#endif /* HAVE_VSYSLOG */
493
494 /*
495 * Log the configuration file that was used...
496 */
497
498 cupsdLogMessage(CUPSD_LOG_INFO, "Loaded configuration file \"%s\"",
499 ConfigurationFile);
500
501 /*
502 * Validate the Group and SystemGroup settings - they cannot be the same,
503 * otherwise the CGI programs will be able to authenticate as root without
504 * a password!
505 */
506
507 if (!RunUser)
508 {
509 for (i = 0; i < NumSystemGroups; i ++)
510 if (Group == SystemGroupIDs[i])
511 break;
512
513 if (i < NumSystemGroups)
514 {
515 /*
516 * Log the error and reset the group to a safe value...
517 */
518
519 cupsdLogMessage(CUPSD_LOG_NOTICE,
520 "Group and SystemGroup cannot use the same groups!");
521 cupsdLogMessage(CUPSD_LOG_INFO, "Resetting Group to \"nobody\"...");
522
523 group = getgrnam("nobody");
524 endgrent();
525
526 if (group != NULL)
527 Group = group->gr_gid;
528 else
529 {
530 /*
531 * Use the (historical) NFS nobody group ID (-2 as a 16-bit twos-
532 * complement number...)
533 */
534
535 Group = 65534;
536 }
537 }
538 }
539
540 /*
541 * Check that we have at least one listen/port line; if not, report this
542 * as an error and exit!
543 */
544
bd7854cb 545 if (cupsArrayCount(Listeners) == 0)
ef416fc2 546 {
547 /*
548 * No listeners!
549 */
550
551 cupsdLogMessage(CUPSD_LOG_EMERG,
552 "No valid Listen or Port lines were found in the configuration file!");
553
554 /*
555 * Commit suicide...
556 */
557
558 cupsdEndProcess(getpid(), 0);
559 }
560
561 /*
562 * Set the default locale using the language and charset...
563 */
564
565 cupsdSetStringf(&DefaultLocale, "%s.%s", DefaultLanguage, DefaultCharset);
566
567 /*
568 * Update all relative filenames to include the full path from ServerRoot...
569 */
570
571 if (DocumentRoot[0] != '/')
572 cupsdSetStringf(&DocumentRoot, "%s/%s", ServerRoot, DocumentRoot);
573
574 if (RequestRoot[0] != '/')
575 cupsdSetStringf(&RequestRoot, "%s/%s", ServerRoot, RequestRoot);
576
577 if (ServerBin[0] != '/')
578 cupsdSetStringf(&ServerBin, "%s/%s", ServerRoot, ServerBin);
579
580 if (StateDir[0] != '/')
581 cupsdSetStringf(&StateDir, "%s/%s", ServerRoot, StateDir);
582
583 if (CacheDir[0] != '/')
584 cupsdSetStringf(&CacheDir, "%s/%s", ServerRoot, CacheDir);
585
586#ifdef HAVE_SSL
587 if (ServerCertificate[0] != '/')
588 cupsdSetStringf(&ServerCertificate, "%s/%s", ServerRoot, ServerCertificate);
589
590 if (!strncmp(ServerRoot, ServerCertificate, strlen(ServerRoot)))
591 {
592 chown(ServerCertificate, RunUser, Group);
fa73b229 593 chmod(ServerCertificate, 0600);
ef416fc2 594 }
595
596# if defined(HAVE_LIBSSL) || defined(HAVE_GNUTLS)
597 if (ServerKey[0] != '/')
598 cupsdSetStringf(&ServerKey, "%s/%s", ServerRoot, ServerKey);
599
600 if (!strncmp(ServerRoot, ServerKey, strlen(ServerRoot)))
601 {
602 chown(ServerKey, RunUser, Group);
fa73b229 603 chmod(ServerKey, 0600);
ef416fc2 604 }
605# endif /* HAVE_LIBSSL || HAVE_GNUTLS */
606#endif /* HAVE_SSL */
607
608 /*
609 * Make sure that directories and config files are owned and
610 * writable by the user and group in the cupsd.conf file...
611 */
612
fa73b229 613 check_permissions(CacheDir, NULL, 0775, RunUser, Group, 1, 1);
bd7854cb 614/* check_permissions(CacheDir, "ppd", 0755, RunUser, Group, 1, 1);*/
ef416fc2 615
923edb68 616 check_permissions(StateDir, NULL, 0755, RunUser, Group, 1, 1);
fa73b229 617 check_permissions(StateDir, "certs", RunUser ? 0711 : 0511, User,
618 SystemGroupIDs[0], 1, 1);
ef416fc2 619
fa73b229 620 check_permissions(ServerRoot, NULL, 0755, RunUser, Group, 1, 0);
621 check_permissions(ServerRoot, "ppd", 0755, RunUser, Group, 1, 1);
622 check_permissions(ServerRoot, "ssl", 0700, RunUser, Group, 1, 0);
623 check_permissions(ServerRoot, "cupsd.conf", ConfigFilePerm, RunUser, Group,
624 0, 0);
625 check_permissions(ServerRoot, "classes.conf", 0600, RunUser, Group, 0, 0);
626 check_permissions(ServerRoot, "printers.conf", 0600, RunUser, Group, 0, 0);
627 check_permissions(ServerRoot, "passwd.md5", 0600, User, Group, 0, 0);
ef416fc2 628
b423cd4c 629 /*
630 * Update TempDir to the default if it hasn't been set already...
631 */
632
633 if (!TempDir)
634 {
635 if ((tmpdir = getenv("TMPDIR")) != NULL)
636 {
637 /*
638 * TMPDIR is defined, see if it is OK for us to use...
639 */
640
641 if (stat(tmpdir, &tmpinfo))
642 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to access TMPDIR (%s): %s",
643 tmpdir, strerror(errno));
644 else if (!S_ISDIR(tmpinfo.st_mode))
645 cupsdLogMessage(CUPSD_LOG_ERROR, "TMPDIR (%s) is not a directory!",
646 tmpdir);
647 else if ((tmpinfo.st_uid != User || !(tmpinfo.st_mode & S_IWUSR)) &&
648 (tmpinfo.st_gid != Group || !(tmpinfo.st_mode & S_IWGRP)) &&
649 !(tmpinfo.st_mode & S_IWOTH))
650 cupsdLogMessage(CUPSD_LOG_ERROR,
651 "TMPDIR (%s) has the wrong permissions!", tmpdir);
652 else
653 cupsdSetString(&TempDir, tmpdir);
654
655 if (!TempDir)
656 cupsdLogMessage(CUPSD_LOG_INFO, "Using default TempDir of %s/tmp...",
657 RequestRoot);
658 }
659
660 if (!TempDir)
661 cupsdSetStringf(&TempDir, "%s/tmp", RequestRoot);
662 }
663
ef416fc2 664 /*
665 * Make sure the request and temporary directories have the right
666 * permissions...
667 */
668
fa73b229 669 check_permissions(RequestRoot, NULL, 0710, RunUser, Group, 1, 1);
ef416fc2 670
671 if (!strncmp(TempDir, RequestRoot, strlen(RequestRoot)) ||
672 access(TempDir, 0))
673 {
674 /*
675 * Update ownership and permissions if the CUPS temp directory
676 * is under the spool directory or does not exist...
677 */
678
fa73b229 679 check_permissions(TempDir, NULL, 01770, RunUser, Group, 1, 1);
ef416fc2 680 }
681
682 if (!strncmp(TempDir, RequestRoot, strlen(RequestRoot)))
683 {
684 /*
685 * Clean out the temporary directory...
686 */
687
688 cups_dir_t *dir; /* Temporary directory */
689 cups_dentry_t *dent; /* Directory entry */
690 char tempfile[1024]; /* Temporary filename */
691
692
693 if ((dir = cupsDirOpen(TempDir)) != NULL)
694 {
695 cupsdLogMessage(CUPSD_LOG_INFO,
696 "Cleaning out old temporary files in \"%s\"...", TempDir);
697
698 while ((dent = cupsDirRead(dir)) != NULL)
699 {
700 snprintf(tempfile, sizeof(tempfile), "%s/%s", TempDir, dent->filename);
701
702 if (unlink(tempfile))
703 cupsdLogMessage(CUPSD_LOG_ERROR,
704 "Unable to remove temporary file \"%s\" - %s",
705 tempfile, strerror(errno));
706 else
707 cupsdLogMessage(CUPSD_LOG_DEBUG, "Removed temporary file \"%s\"...",
708 tempfile);
709 }
710
711 cupsDirClose(dir);
712 }
713 else
714 cupsdLogMessage(CUPSD_LOG_ERROR,
715 "Unable to open temporary directory \"%s\" - %s",
716 TempDir, strerror(errno));
717 }
718
719 /*
720 * Setup environment variables...
721 */
722
723 cupsdInitEnv();
724
725 /*
726 * Check the MaxClients setting, and then allocate memory for it...
727 */
728
729 if (MaxClients > (MaxFDs / 3) || MaxClients <= 0)
730 {
731 if (MaxClients > 0)
732 cupsdLogMessage(CUPSD_LOG_INFO, "MaxClients limited to 1/3 (%d) of the file descriptor limit (%d)...",
733 MaxFDs / 3, MaxFDs);
734
735 MaxClients = MaxFDs / 3;
736 }
737
bd7854cb 738 cupsdLogMessage(CUPSD_LOG_INFO, "Configured for up to %d clients.",
739 MaxClients);
ef416fc2 740
741 /*
742 * Check the MaxActiveJobs setting; limit to 1/3 the available
743 * file descriptors, since we need a pipe for each job...
744 */
745
746 if (MaxActiveJobs > (MaxFDs / 3))
747 MaxActiveJobs = MaxFDs / 3;
748
749 if (Classification && strcasecmp(Classification, "none") == 0)
750 cupsdClearString(&Classification);
751
752 if (Classification)
753 cupsdLogMessage(CUPSD_LOG_INFO, "Security set to \"%s\"", Classification);
754
755 /*
756 * Update the MaxClientsPerHost value, as needed...
757 */
758
759 if (MaxClientsPerHost <= 0)
760 MaxClientsPerHost = MaxClients;
761
762 if (MaxClientsPerHost > MaxClients)
763 MaxClientsPerHost = MaxClients;
764
765 cupsdLogMessage(CUPSD_LOG_INFO,
766 "Allowing up to %d client connections per host.",
767 MaxClientsPerHost);
768
769 /*
770 * Update the default policy, as needed...
771 */
772
773 if (DefaultPolicy)
774 DefaultPolicyPtr = cupsdFindPolicy(DefaultPolicy);
775 else
776 DefaultPolicyPtr = NULL;
777
778 if (!DefaultPolicyPtr)
779 {
780 cupsd_policy_t *p; /* New policy */
781 cupsd_location_t *po; /* New policy operation */
782
783
784 if (DefaultPolicy)
785 cupsdLogMessage(CUPSD_LOG_ERROR, "Default policy \"%s\" not found!",
786 DefaultPolicy);
787
e1d6a774 788 cupsdSetString(&DefaultPolicy, "default");
789
ef416fc2 790 if ((DefaultPolicyPtr = cupsdFindPolicy("default")) != NULL)
791 cupsdLogMessage(CUPSD_LOG_INFO,
792 "Using policy \"default\" as the default!");
793 else
794 {
795 cupsdLogMessage(CUPSD_LOG_INFO,
796 "Creating CUPS default administrative policy:");
797
798 DefaultPolicyPtr = p = cupsdAddPolicy("default");
799
800 cupsdLogMessage(CUPSD_LOG_INFO, "<Policy default>");
801 cupsdLogMessage(CUPSD_LOG_INFO,
802 "<Limit Send-Document Send-URI Cancel-Job Hold-Job "
803 "Release-Job Restart-Job Purge-Jobs "
804 "Set-Job-Attributes Create-Job-Subscription "
805 "Renew-Subscription Cancel-Subscription "
806 "Get-Notifications Reprocess-Job Cancel-Current-Job "
807 "Suspend-Current-Job Resume-Job CUPS-Move-Job "
808 "CUPS-Authenticate-Job>");
809 cupsdLogMessage(CUPSD_LOG_INFO, "Order Deny,Allow");
810
811 po = cupsdAddPolicyOp(p, NULL, IPP_SEND_DOCUMENT);
812 po->order_type = AUTH_ALLOW;
813 po->level = AUTH_USER;
814
815 cupsdAddName(po, "@OWNER");
816 cupsdAddName(po, "@SYSTEM");
817 cupsdLogMessage(CUPSD_LOG_INFO, "Require user @OWNER @SYSTEM");
818
819 cupsdAddPolicyOp(p, po, IPP_SEND_URI);
820 cupsdAddPolicyOp(p, po, IPP_CANCEL_JOB);
821 cupsdAddPolicyOp(p, po, IPP_HOLD_JOB);
822 cupsdAddPolicyOp(p, po, IPP_RELEASE_JOB);
823 cupsdAddPolicyOp(p, po, IPP_RESTART_JOB);
824 cupsdAddPolicyOp(p, po, IPP_PURGE_JOBS);
825 cupsdAddPolicyOp(p, po, IPP_SET_JOB_ATTRIBUTES);
826 cupsdAddPolicyOp(p, po, IPP_CREATE_JOB_SUBSCRIPTION);
827 cupsdAddPolicyOp(p, po, IPP_RENEW_SUBSCRIPTION);
828 cupsdAddPolicyOp(p, po, IPP_CANCEL_SUBSCRIPTION);
829 cupsdAddPolicyOp(p, po, IPP_GET_NOTIFICATIONS);
830 cupsdAddPolicyOp(p, po, IPP_REPROCESS_JOB);
831 cupsdAddPolicyOp(p, po, IPP_CANCEL_CURRENT_JOB);
832 cupsdAddPolicyOp(p, po, IPP_SUSPEND_CURRENT_JOB);
833 cupsdAddPolicyOp(p, po, IPP_RESUME_JOB);
834 cupsdAddPolicyOp(p, po, CUPS_MOVE_JOB);
835 cupsdAddPolicyOp(p, po, CUPS_AUTHENTICATE_JOB);
836
837 cupsdLogMessage(CUPSD_LOG_INFO, "</Limit>");
838
839 cupsdLogMessage(CUPSD_LOG_INFO,
840 "<Limit Pause-Printer Resume-Printer "
841 "Set-Printer-Attributes Enable-Printer "
842 "Disable-Printer Pause-Printer-After-Current-Job "
843 "Hold-New-Jobs Release-Held-New-Jobs "
844 "Deactivate-Printer Activate-Printer Restart-Printer "
845 "Shutdown-Printer Startup-Printer Promote-Job "
846 "Schedule-Job-After CUPS-Add-Printer "
847 "CUPS-Delete-Printer CUPS-Add-Class CUPS-Delete-Class "
848 "CUPS-Accept-Jobs CUPS-Reject-Jobs CUPS-Set-Default>");
849 cupsdLogMessage(CUPSD_LOG_INFO, "Order Deny,Allow");
850 cupsdLogMessage(CUPSD_LOG_INFO, "AuthType Basic");
851
852 po = cupsdAddPolicyOp(p, NULL, IPP_PAUSE_PRINTER);
853 po->order_type = AUTH_ALLOW;
854 po->type = AUTH_BASIC;
855 po->level = AUTH_USER;
856
857 cupsdAddName(po, "@SYSTEM");
858 cupsdLogMessage(CUPSD_LOG_INFO, "Require user @SYSTEM");
859
860 cupsdAddPolicyOp(p, po, IPP_RESUME_PRINTER);
861 cupsdAddPolicyOp(p, po, IPP_SET_PRINTER_ATTRIBUTES);
862 cupsdAddPolicyOp(p, po, IPP_ENABLE_PRINTER);
863 cupsdAddPolicyOp(p, po, IPP_DISABLE_PRINTER);
864 cupsdAddPolicyOp(p, po, IPP_PAUSE_PRINTER_AFTER_CURRENT_JOB);
865 cupsdAddPolicyOp(p, po, IPP_HOLD_NEW_JOBS);
866 cupsdAddPolicyOp(p, po, IPP_RELEASE_HELD_NEW_JOBS);
867 cupsdAddPolicyOp(p, po, IPP_DEACTIVATE_PRINTER);
868 cupsdAddPolicyOp(p, po, IPP_ACTIVATE_PRINTER);
869 cupsdAddPolicyOp(p, po, IPP_RESTART_PRINTER);
870 cupsdAddPolicyOp(p, po, IPP_SHUTDOWN_PRINTER);
871 cupsdAddPolicyOp(p, po, IPP_STARTUP_PRINTER);
872 cupsdAddPolicyOp(p, po, IPP_PROMOTE_JOB);
873 cupsdAddPolicyOp(p, po, IPP_SCHEDULE_JOB_AFTER);
874 cupsdAddPolicyOp(p, po, CUPS_ADD_PRINTER);
875 cupsdAddPolicyOp(p, po, CUPS_DELETE_PRINTER);
876 cupsdAddPolicyOp(p, po, CUPS_ADD_CLASS);
877 cupsdAddPolicyOp(p, po, CUPS_DELETE_CLASS);
878 cupsdAddPolicyOp(p, po, CUPS_ACCEPT_JOBS);
879 cupsdAddPolicyOp(p, po, CUPS_REJECT_JOBS);
880 cupsdAddPolicyOp(p, po, CUPS_SET_DEFAULT);
881
882 cupsdLogMessage(CUPSD_LOG_INFO, "</Limit>");
883
884 cupsdLogMessage(CUPSD_LOG_INFO, "<Limit All>");
885 cupsdLogMessage(CUPSD_LOG_INFO, "Order Deny,Allow");
886
887 po = cupsdAddPolicyOp(p, NULL, IPP_ANY_OPERATION);
888 po->order_type = AUTH_ALLOW;
889
890 cupsdLogMessage(CUPSD_LOG_INFO, "</Limit>");
891 cupsdLogMessage(CUPSD_LOG_INFO, "</Policy>");
892 }
893 }
894
bd7854cb 895 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdReadConfiguration: NumPolicies=%d",
896 NumPolicies);
ef416fc2 897 for (i = 0; i < NumPolicies; i ++)
bd7854cb 898 cupsdLogMessage(CUPSD_LOG_DEBUG2,
899 "cupsdReadConfiguration: Policies[%d]=\"%s\"", i,
ef416fc2 900 Policies[i]->name);
901
902 /*
903 * If we are doing a full reload or the server root has changed, flush
904 * the jobs, printers, etc. and start from scratch...
905 */
906
907 if (NeedReload == RELOAD_ALL ||
908 !old_serverroot || !ServerRoot || strcmp(old_serverroot, ServerRoot) ||
909 !old_requestroot || !RequestRoot || strcmp(old_requestroot, RequestRoot))
910 {
fa73b229 911 mime_type_t *type; /* Current type */
912 char mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE];
913 /* MIME type name */
914
915
ef416fc2 916 cupsdLogMessage(CUPSD_LOG_INFO, "Full reload is required.");
917
918 /*
919 * Free all memory...
920 */
921
922 cupsdDeleteAllSubscriptions();
923 cupsdFreeAllJobs();
924 cupsdDeleteAllClasses();
925 cupsdDeleteAllPrinters();
926
927 DefaultPrinter = NULL;
928
929 if (MimeDatabase != NULL)
930 mimeDelete(MimeDatabase);
931
932 if (NumMimeTypes)
933 {
934 for (i = 0; i < NumMimeTypes; i ++)
757d2cad 935 _cupsStrFree(MimeTypes[i]);
ef416fc2 936
937 free(MimeTypes);
938 }
939
940 /*
941 * Read the MIME type and conversion database...
942 */
943
944 snprintf(temp, sizeof(temp), "%s/filter", ServerBin);
945
946 MimeDatabase = mimeLoad(ServerRoot, temp);
947
948 if (!MimeDatabase)
949 {
950 cupsdLogMessage(CUPSD_LOG_EMERG,
951 "Unable to load MIME database from \'%s\'!", ServerRoot);
952 exit(errno);
953 }
954
955 cupsdLogMessage(CUPSD_LOG_INFO,
956 "Loaded MIME database from \'%s\': %d types, %d filters...",
fa73b229 957 ServerRoot, mimeNumTypes(MimeDatabase),
958 mimeNumFilters(MimeDatabase));
ef416fc2 959
960 /*
961 * Create a list of MIME types for the document-format-supported
962 * attribute...
963 */
964
fa73b229 965 NumMimeTypes = mimeNumTypes(MimeDatabase);
ef416fc2 966 if (!mimeType(MimeDatabase, "application", "octet-stream"))
967 NumMimeTypes ++;
968
969 MimeTypes = calloc(NumMimeTypes, sizeof(const char *));
970
fa73b229 971 for (i = 0, type = mimeFirstType(MimeDatabase);
972 type;
973 i ++, type = mimeNextType(MimeDatabase))
ef416fc2 974 {
fa73b229 975 snprintf(mimetype, sizeof(mimetype), "%s/%s", type->super, type->type);
ef416fc2 976
757d2cad 977 MimeTypes[i] = _cupsStrAlloc(mimetype);
ef416fc2 978 }
979
980 if (i < NumMimeTypes)
757d2cad 981 MimeTypes[i] = _cupsStrAlloc("application/octet-stream");
bd7854cb 982
983 if (LogLevel == CUPSD_LOG_DEBUG2)
984 {
985 mime_filter_t *filter; /* Current filter */
986
987
988 for (type = mimeFirstType(MimeDatabase);
989 type;
990 type = mimeNextType(MimeDatabase))
991 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdReadConfiguration: type %s/%s",
992 type->super, type->type);
993
994 for (filter = mimeFirstFilter(MimeDatabase);
995 filter;
996 filter = mimeNextFilter(MimeDatabase))
997 cupsdLogMessage(CUPSD_LOG_DEBUG2,
998 "cupsdReadConfiguration: filter %s/%s to %s/%s %d %s",
999 filter->src->super, filter->src->type,
1000 filter->dst->super, filter->dst->type,
1001 filter->cost, filter->filter);
1002 }
ef416fc2 1003
1004 /*
1005 * Load banners...
1006 */
1007
1008 snprintf(temp, sizeof(temp), "%s/banners", DataDir);
1009 cupsdLoadBanners(temp);
1010
1011 /*
1012 * Load printers and classes...
1013 */
1014
1015 cupsdLoadAllPrinters();
1016 cupsdLoadAllClasses();
1017 cupsdLoadRemoteCache();
1018 cupsdWritePrintcap();
1019
1020 cupsdCreateCommonData();
1021
1022 /*
1023 * Load queued jobs...
1024 */
1025
1026 cupsdLoadAllJobs();
1027
1028 /*
1029 * Load subscriptions...
1030 */
1031
1032 cupsdLoadAllSubscriptions();
1033
1034 cupsdLogMessage(CUPSD_LOG_INFO, "Full reload complete.");
1035 }
1036 else
1037 {
1038 /*
1039 * Not a full reload, so recreate the common printer attributes...
1040 */
1041
1042 cupsdCreateCommonData();
1043
1044 /*
1045 * Update all printers as needed...
1046 */
1047
1048 cupsdUpdatePrinters();
1049 cupsdWritePrintcap();
1050
1051 cupsdLogMessage(CUPSD_LOG_INFO, "Partial reload complete.");
1052 }
1053
1054 /*
1055 * Reset the reload state...
1056 */
1057
1058 NeedReload = RELOAD_NONE;
1059
1060 cupsdClearString(&old_serverroot);
1061 cupsdClearString(&old_requestroot);
1062
ef416fc2 1063 return (1);
1064}
1065
1066
fa73b229 1067/*
1068 * 'check_permissions()' - Fix the mode and ownership of a file or directory.
1069 */
1070
1071static int /* O - 0 on success, -1 on error */
1072check_permissions(const char *filename, /* I - File/directory name */
1073 const char *suffix, /* I - Additional file/directory name */
1074 int mode, /* I - Permissions */
1075 int user, /* I - Owner */
1076 int group, /* I - Group */
1077 int is_dir, /* I - 1 = directory, 0 = file */
1078 int create_dir)/* I - 1 = create directory, 0 = not */
1079{
1080 int dir_created = 0; /* Did we create a directory? */
1081 char pathname[1024]; /* File name with prefix */
1082 struct stat fileinfo; /* Stat buffer */
1083
1084
1085 /*
1086 * Prepend the given root to the filename before testing it...
1087 */
1088
1089 if (suffix)
1090 {
1091 snprintf(pathname, sizeof(pathname), "%s/%s", filename, suffix);
1092 filename = pathname;
1093 }
1094
1095 /*
1096 * See if we can stat the file/directory...
1097 */
1098
1099 if (stat(filename, &fileinfo))
1100 {
1101 if (errno == ENOENT && create_dir)
1102 {
1103 cupsdLogMessage(CUPSD_LOG_ERROR, "Creating missing directory \"%s\"",
1104 filename);
1105
1106 if (mkdir(filename, mode))
1107 {
1108 cupsdLogMessage(CUPSD_LOG_ERROR,
1109 "Unable to create directory \"%s\" - %s", filename,
1110 strerror(errno));
1111 return (-1);
1112 }
1113
1114 dir_created = 1;
1115 }
1116 else
1117 return (-1);
1118 }
1119
1120 /*
1121 * Make sure it's a regular file...
1122 */
1123
1124 if (!dir_created && !is_dir && !S_ISREG(fileinfo.st_mode))
1125 {
1126 cupsdLogMessage(CUPSD_LOG_ERROR, "\"%s\" is not a regular file!", filename);
1127 return (-1);
1128 }
1129
1130 if (!dir_created && is_dir && !S_ISDIR(fileinfo.st_mode))
1131 {
1132 cupsdLogMessage(CUPSD_LOG_ERROR, "\"%s\" is not a directory!", filename);
1133 return (-1);
1134 }
1135
1136 /*
1137 * Fix owner, group, and mode as needed...
1138 */
1139
1140 if (dir_created || fileinfo.st_uid != user || fileinfo.st_gid != group)
1141 {
1142 cupsdLogMessage(CUPSD_LOG_WARN, "Repairing ownership of \"%s\"", filename);
1143
1144 if (chown(filename, user, group))
1145 {
1146 cupsdLogMessage(CUPSD_LOG_ERROR,
1147 "Unable to change ownership of \"%s\" - %s", filename,
1148 strerror(errno));
1149 return (-1);
1150 }
1151 }
1152
bd7854cb 1153 if (dir_created || (fileinfo.st_mode & 07777) != mode)
fa73b229 1154 {
1155 cupsdLogMessage(CUPSD_LOG_WARN, "Repairing access permissions of \"%s\"", filename);
1156
1157 if (chmod(filename, mode))
1158 {
1159 cupsdLogMessage(CUPSD_LOG_ERROR,
1160 "Unable to change permissions of \"%s\" - %s", filename,
1161 strerror(errno));
1162 return (-1);
1163 }
1164 }
1165
1166 /*
1167 * Everything is OK...
1168 */
1169
1170 return (0);
1171}
1172
1173
ef416fc2 1174/*
1175 * 'get_address()' - Get an address + port number from a line.
1176 */
1177
1178static http_addrlist_t * /* O - Pointer to list if address good, NULL if bad */
1179get_address(const char *value, /* I - Value string */
1180 int defport) /* I - Default port */
1181{
1182 char buffer[1024], /* Hostname + port number buffer */
1183 defpname[255], /* Default port name */
1184 *hostname, /* Hostname or IP */
1185 *portname; /* Port number or name */
1186 http_addrlist_t *addrlist; /* Address list */
1187
1188
1189 /*
1190 * Check for an empty value...
1191 */
1192
1193 if (!*value)
1194 {
1195 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad (empty) address!");
1196 return (NULL);
1197 }
1198
1199 /*
1200 * Grab a hostname and port number; if there is no colon and the port name
1201 * is only digits, then we have a port number by itself...
1202 */
1203
1204 strlcpy(buffer, value, sizeof(buffer));
1205
1206 if ((portname = strrchr(buffer, ':')) != NULL && !strchr(portname, ']'))
1207 {
1208 *portname++ = '\0';
1209 hostname = buffer;
1210 }
1211 else
1212 {
1213 for (portname = buffer; isdigit(*portname & 255); portname ++);
1214
1215 if (*portname)
1216 {
1217 /*
1218 * Use the default port...
1219 */
1220
1221 sprintf(defpname, "%d", defport);
1222 portname = defpname;
1223 hostname = buffer;
1224 }
1225 else
1226 {
1227 /*
1228 * The buffer contains just a port number...
1229 */
1230
1231 portname = buffer;
1232 hostname = NULL;
1233 }
1234 }
1235
1236 if (hostname && !strcmp(hostname, "*"))
1237 hostname = NULL;
1238
1239 /*
1240 * Now lookup the address using httpAddrGetList()...
1241 */
1242
1243 if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL)
1244 cupsdLogMessage(CUPSD_LOG_ERROR, "Hostname lookup for \"%s\" failed!",
1245 hostname ? hostname : "(nil)");
1246
1247 return (addrlist);
1248}
1249
1250
1251/*
1252 * 'get_addr_and_mask()' - Get an IP address and netmask.
1253 */
1254
1255static int /* O - 1 on success, 0 on failure */
1256get_addr_and_mask(const char *value, /* I - String from config file */
1257 unsigned *ip, /* O - Address value */
1258 unsigned *mask) /* O - Mask value */
1259{
1260 int i, j, /* Looping vars */
1261 family, /* Address family */
1262 ipcount; /* Count of fields in address */
1263 unsigned ipval; /* Value */
1264 const char *maskval, /* Pointer to start of mask value */
1265 *ptr, /* Pointer into value */
1266 *ptr2; /* ... */
1267 static unsigned netmasks[4][4] = /* Standard IPv4 netmasks... */
1268 {
1269 { 0xffffffff, 0xffffffff, 0xffffffff, 0xff000000 },
1270 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffff0000 },
1271 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffff00 },
1272 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
1273 };
1274#ifdef AF_INET6
1275 static unsigned netmasks6[8][4] = /* Standard IPv6 netmasks... */
1276 {
1277 { 0xffff0000, 0x00000000, 0x00000000, 0x00000000 },
1278 { 0xffffffff, 0x00000000, 0x00000000, 0x00000000 },
1279 { 0xffffffff, 0xffff0000, 0x00000000, 0x00000000 },
1280 { 0xffffffff, 0xffffffff, 0x00000000, 0x00000000 },
1281 { 0xffffffff, 0xffffffff, 0xffff0000, 0x00000000 },
1282 { 0xffffffff, 0xffffffff, 0xffffffff, 0x00000000 },
1283 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffff0000 },
1284 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
1285 };
1286#endif /* AF_INET6 */
1287
1288
1289 /*
1290 * Get the address...
1291 */
1292
1293 memset(ip, 0, sizeof(unsigned) * 4);
1294
1295 if ((maskval = strchr(value, '/')) != NULL)
1296 maskval ++;
1297 else
1298 maskval = value + strlen(value);
1299
1300#ifdef AF_INET6
1301 /*
1302 * Check for an IPv6 address...
1303 */
1304
1305 if (*value == '[')
1306 {
1307 /*
1308 * Parse hexadecimal IPv6 address...
1309 */
1310
1311 family = AF_INET6;
1312
1313 for (i = 0, ptr = value + 1; *ptr && i < 8; i ++)
1314 {
1315 if (*ptr == ']')
1316 break;
1317 else if (!strncmp(ptr, "::", 2))
1318 {
1319 for (ptr2 = strchr(ptr + 2, ':'), j = 0;
1320 ptr2;
1321 ptr2 = strchr(ptr2 + 1, ':'), j ++);
1322
1323 i = 7 - j;
1324 }
1325 else if (isxdigit(*ptr & 255))
1326 {
1327 ipval = strtoul(ptr, (char **)&ptr, 16);
1328
1329 if (ipval > 0xffff)
1330 return (0);
1331
1332 if (i & 1)
1333 ip[i] |= ipval;
1334 else
1335 ip[i] |= ipval << 16;
1336 }
1337 else
1338 return (0);
1339
1340 while (*ptr == ':')
1341 ptr ++;
1342 }
1343
1344 ipcount = i;
1345
1346 if (*ptr && *ptr != '/')
1347 return (0);
1348 }
1349 else
1350#endif /* AF_INET6 */
1351 {
1352 /*
1353 * Parse dotted-decimal IPv4 address...
1354 */
1355
1356 family = AF_INET;
1357 ipcount = sscanf(value, "%u.%u.%u.%u", ip + 0, ip + 1, ip + 2, ip + 3);
1358
1359 ip[3] |= ((((ip[0] << 8) | ip[1]) << 8) | ip[2]) << 8;
1360 ip[0] = ip[1] = ip[2] = 0;
1361 }
1362
1363 if (*maskval)
1364 {
1365 /*
1366 * Get the netmask value(s)...
1367 */
1368
1369 memset(mask, 0, sizeof(unsigned) * 4);
1370
1371#ifdef AF_INET6
1372 if (*maskval == '[')
1373 {
1374 /*
1375 * Get hexadecimal mask value...
1376 */
1377
1378 for (i = 0, ptr = maskval + 1; *ptr && i < 8; i ++)
1379 {
1380 if (*ptr == ']')
1381 break;
1382 else if (!strncmp(ptr, "::", 2))
1383 {
1384 for (ptr2 = strchr(ptr + 2, ':'), j = 0;
1385 ptr2;
1386 ptr2 = strchr(ptr2 + 1, ':'), j ++);
1387
1388 i = 7 - j;
1389 }
1390 else if (isxdigit(*ptr & 255))
1391 {
1392 ipval = strtoul(ptr, (char **)&ptr, 16);
1393
1394 if (ipval > 0xffff)
1395 return (0);
1396
1397 if (i & 1)
1398 mask[i] |= ipval;
1399 else
1400 mask[i] |= ipval << 16;
1401 }
1402 else
1403 return (0);
1404
1405 while (*ptr == ':')
1406 ptr ++;
1407 }
1408
1409 if (*ptr)
1410 return (0);
1411 }
1412 else
1413#endif /* AF_INET6 */
1414 if (strchr(maskval, '.'))
1415 {
1416 /*
1417 * Get dotted-decimal mask...
1418 */
1419
1420 if (sscanf(maskval, "%u.%u.%u.%u", mask + 0, mask + 1, mask + 2, mask + 3) != 4)
1421 return (0);
1422
1423 mask[3] |= ((((mask[0] << 8) | mask[1]) << 8) | mask[2]) << 8;
1424 mask[0] = mask[1] = mask[2] = 0;
1425 }
1426 else
1427 {
1428 /*
1429 * Get address/bits format...
1430 */
1431
1432 i = atoi(maskval);
1433
1434#ifdef AF_INET6
1435 if (family == AF_INET6)
1436 {
1437 i = 128 - i;
1438
1439 if (i <= 96)
1440 mask[0] = 0xffffffff;
1441 else
1442 mask[0] = (0xffffffff << (i - 96)) & 0xffffffff;
1443
1444 if (i <= 64)
1445 mask[1] = 0xffffffff;
1446 else if (i >= 96)
1447 mask[1] = 0;
1448 else
1449 mask[1] = (0xffffffff << (i - 64)) & 0xffffffff;
1450
1451 if (i <= 32)
1452 mask[2] = 0xffffffff;
1453 else if (i >= 64)
1454 mask[2] = 0;
1455 else
1456 mask[2] = (0xffffffff << (i - 32)) & 0xffffffff;
1457
1458 if (i == 0)
1459 mask[3] = 0xffffffff;
1460 else if (i >= 32)
1461 mask[3] = 0;
1462 else
1463 mask[3] = (0xffffffff << i) & 0xffffffff;
1464 }
1465 else
1466#endif /* AF_INET6 */
1467 {
1468 i = 32 - i;
1469
1470 mask[0] = 0xffffffff;
1471 mask[1] = 0xffffffff;
1472 mask[2] = 0xffffffff;
1473
1474 if (i > 0)
1475 mask[3] = (0xffffffff << i) & 0xffffffff;
1476 else
1477 mask[3] = 0xffffffff;
1478 }
1479 }
1480 }
1481#ifdef AF_INET6
1482 else if (family == AF_INET6)
1483 memcpy(mask, netmasks6[ipcount - 1], sizeof(unsigned) * 4);
1484#endif /* AF_INET6 */
1485 else
1486 memcpy(mask, netmasks[ipcount - 1], sizeof(unsigned) * 4);
1487
1488 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1489 "get_addr_and_mask(value=\"%s\", "
1490 "ip=[%08x:%08x:%08x:%08x], mask=[%08x:%08x:%08x:%08x]",
1491 value, ip[0], ip[1], ip[2], ip[3], mask[0], mask[1], mask[2],
1492 mask[3]);
1493
1494 /*
1495 * Check for a valid netmask; no fallback like in CUPS 1.1.x!
1496 */
1497
1498 if ((ip[0] & ~mask[0]) != 0 ||
1499 (ip[1] & ~mask[1]) != 0 ||
1500 (ip[2] & ~mask[2]) != 0 ||
1501 (ip[3] & ~mask[3]) != 0)
1502 return (0);
1503
1504 return (1);
1505}
1506
1507
1508/*
1509 * 'parse_aaa()' - Parse authentication, authorization, and access control lines.
1510 */
1511
1512static int /* O - 1 on success, 0 on failure */
1513parse_aaa(cupsd_location_t *loc, /* I - Location */
1514 char *line, /* I - Line from file */
1515 char *value, /* I - Start of value data */
1516 int linenum) /* I - Current line number */
1517{
1518 char *valptr; /* Pointer into value */
1519 unsigned ip[4], /* IP address components */
1520 mask[4]; /* IP netmask components */
1521
1522
1523 if (!strcasecmp(line, "Encryption"))
1524 {
1525 /*
1526 * "Encryption xxx" - set required encryption level...
1527 */
1528
1529 if (!strcasecmp(value, "never"))
1530 loc->encryption = HTTP_ENCRYPT_NEVER;
1531 else if (!strcasecmp(value, "always"))
1532 {
1533 cupsdLogMessage(CUPSD_LOG_ERROR,
1534 "Encryption value \"%s\" on line %d is invalid in this "
1535 "context. Using \"required\" instead.", value, linenum);
1536
1537 loc->encryption = HTTP_ENCRYPT_REQUIRED;
1538 }
1539 else if (!strcasecmp(value, "required"))
1540 loc->encryption = HTTP_ENCRYPT_REQUIRED;
1541 else if (!strcasecmp(value, "ifrequested"))
1542 loc->encryption = HTTP_ENCRYPT_IF_REQUESTED;
1543 else
1544 {
1545 cupsdLogMessage(CUPSD_LOG_ERROR,
1546 "Unknown Encryption value %s on line %d.", value, linenum);
1547 return (0);
1548 }
1549 }
1550 else if (!strcasecmp(line, "Order"))
1551 {
1552 /*
1553 * "Order Deny,Allow" or "Order Allow,Deny"...
1554 */
1555
1556 if (!strncasecmp(value, "deny", 4))
1557 loc->order_type = AUTH_ALLOW;
1558 else if (!strncasecmp(value, "allow", 5))
1559 loc->order_type = AUTH_DENY;
1560 else
1561 {
1562 cupsdLogMessage(CUPSD_LOG_ERROR, "Unknown Order value %s on line %d.",
1563 value, linenum);
1564 return (0);
1565 }
1566 }
1567 else if (!strcasecmp(line, "Allow") || !strcasecmp(line, "Deny"))
1568 {
1569 /*
1570 * Allow [From] host/ip...
1571 * Deny [From] host/ip...
1572 */
1573
1574 if (!strncasecmp(value, "from", 4))
1575 {
1576 /*
1577 * Strip leading "from"...
1578 */
1579
1580 value += 4;
1581
1582 while (isspace(*value & 255))
1583 value ++;
1584 }
1585
1586 /*
1587 * Figure out what form the allow/deny address takes:
1588 *
1589 * All
1590 * None
1591 * *.domain.com
1592 * .domain.com
1593 * host.domain.com
1594 * nnn.*
1595 * nnn.nnn.*
1596 * nnn.nnn.nnn.*
1597 * nnn.nnn.nnn.nnn
1598 * nnn.nnn.nnn.nnn/mm
1599 * nnn.nnn.nnn.nnn/mmm.mmm.mmm.mmm
1600 */
1601
1602 if (!strcasecmp(value, "all"))
1603 {
1604 /*
1605 * All hosts...
1606 */
1607
1608 if (!strcasecmp(line, "Allow"))
1609 cupsdAllowIP(loc, zeros, zeros);
1610 else
1611 cupsdDenyIP(loc, zeros, zeros);
1612 }
1613 else if (!strcasecmp(value, "none"))
1614 {
1615 /*
1616 * No hosts...
1617 */
1618
1619 if (!strcasecmp(line, "Allow"))
1620 cupsdAllowIP(loc, ones, zeros);
1621 else
1622 cupsdDenyIP(loc, ones, zeros);
1623 }
1624 else if (value[0] == '*' || value[0] == '.' || !isdigit(value[0] & 255))
1625 {
1626 /*
1627 * Host or domain name...
1628 */
1629
1630 if (value[0] == '*')
1631 value ++;
1632
1633 if (!strcasecmp(line, "Allow"))
1634 cupsdAllowHost(loc, value);
1635 else
1636 cupsdDenyHost(loc, value);
1637 }
1638 else
1639 {
1640 /*
1641 * One of many IP address forms...
1642 */
1643
1644 if (!get_addr_and_mask(value, ip, mask))
1645 {
1646 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad netmask value %s on line %d.",
1647 value, linenum);
1648 return (0);
1649 }
1650
1651 if (!strcasecmp(line, "Allow"))
1652 cupsdAllowIP(loc, ip, mask);
1653 else
1654 cupsdDenyIP(loc, ip, mask);
1655 }
1656 }
1657 else if (!strcasecmp(line, "AuthType"))
1658 {
1659 /*
1660 * AuthType {none,basic,digest,basicdigest}
1661 */
1662
1663 if (!strcasecmp(value, "none"))
1664 {
1665 loc->type = AUTH_NONE;
1666 loc->level = AUTH_ANON;
1667 }
1668 else if (!strcasecmp(value, "basic"))
1669 {
1670 loc->type = AUTH_BASIC;
1671
1672 if (loc->level == AUTH_ANON)
1673 loc->level = AUTH_USER;
1674 }
1675 else if (!strcasecmp(value, "digest"))
1676 {
1677 loc->type = AUTH_DIGEST;
1678
1679 if (loc->level == AUTH_ANON)
1680 loc->level = AUTH_USER;
1681 }
1682 else if (!strcasecmp(value, "basicdigest"))
1683 {
1684 loc->type = AUTH_BASICDIGEST;
1685
1686 if (loc->level == AUTH_ANON)
1687 loc->level = AUTH_USER;
1688 }
1689 else
1690 {
1691 cupsdLogMessage(CUPSD_LOG_WARN,
1692 "Unknown authorization type %s on line %d.",
1693 value, linenum);
1694 return (0);
1695 }
1696 }
1697 else if (!strcasecmp(line, "AuthClass"))
1698 {
1699 /*
1700 * AuthClass anonymous, user, system, group
1701 */
1702
1703 if (!strcasecmp(value, "anonymous"))
1704 {
1705 loc->type = AUTH_NONE;
1706 loc->level = AUTH_ANON;
1707
1708 cupsdLogMessage(CUPSD_LOG_WARN,
1709 "\"AuthClass %s\" is deprecated; consider removing "
1710 "it from line %d.",
1711 value, linenum);
1712 }
1713 else if (!strcasecmp(value, "user"))
1714 {
1715 loc->level = AUTH_USER;
1716
1717 cupsdLogMessage(CUPSD_LOG_WARN,
1718 "\"AuthClass %s\" is deprecated; consider using "
1719 "\"Require valid-user\" on line %d.",
1720 value, linenum);
1721 }
1722 else if (!strcasecmp(value, "group"))
1723 {
1724 loc->level = AUTH_GROUP;
1725
1726 cupsdLogMessage(CUPSD_LOG_WARN,
1727 "\"AuthClass %s\" is deprecated; consider using "
1728 "\"Require @groupname\" on line %d.",
1729 value, linenum);
1730 }
1731 else if (!strcasecmp(value, "system"))
1732 {
1733 loc->level = AUTH_GROUP;
1734
1735 cupsdAddName(loc, "@SYSTEM");
1736
1737 cupsdLogMessage(CUPSD_LOG_WARN,
1738 "\"AuthClass %s\" is deprecated; consider using "
1739 "\"Require @SYSTEM\" on line %d.",
1740 value, linenum);
1741 }
1742 else
1743 {
1744 cupsdLogMessage(CUPSD_LOG_WARN,
1745 "Unknown authorization class %s on line %d.",
1746 value, linenum);
1747 return (0);
1748 }
1749 }
1750 else if (!strcasecmp(line, "AuthGroupName"))
1751 {
1752 cupsdAddName(loc, value);
1753
1754 cupsdLogMessage(CUPSD_LOG_WARN,
1755 "\"AuthGroupName %s\" directive is deprecated; consider "
1756 "using \"Require @%s\" on line %d.",
1757 value, value, linenum);
1758 }
1759 else if (!strcasecmp(line, "Require"))
1760 {
1761 /*
1762 * Apache synonym for AuthClass and AuthGroupName...
1763 *
1764 * Get initial word:
1765 *
1766 * Require valid-user
1767 * Require group names
1768 * Require user names
1769 */
1770
1771 for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
1772
1773 if (*valptr)
1774 *valptr++ = '\0';
1775
1776 if (!strcasecmp(value, "valid-user") ||
1777 !strcasecmp(value, "user"))
1778 loc->level = AUTH_USER;
1779 else if (!strcasecmp(value, "group"))
1780 loc->level = AUTH_GROUP;
1781 else
1782 {
1783 cupsdLogMessage(CUPSD_LOG_WARN, "Unknown Require type %s on line %d.",
1784 value, linenum);
1785 return (0);
1786 }
1787
1788 /*
1789 * Get the list of names from the line...
1790 */
1791
1792 for (value = valptr; *value;)
1793 {
1794 while (isspace(*value & 255))
1795 value ++;
1796
1797 if (*value == '\"' || *value == '\'')
1798 {
1799 /*
1800 * Grab quoted name...
1801 */
1802
1803 for (valptr = value + 1; *valptr != *value && *valptr; valptr ++);
1804
1805 value ++;
1806 }
1807 else
1808 {
1809 /*
1810 * Grab literal name.
1811 */
1812
1813 for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
1814 }
1815
1816 if (*valptr)
1817 *valptr++ = '\0';
1818
1819 cupsdAddName(loc, value);
1820
1821 for (value = valptr; isspace(*value & 255); value ++);
1822 }
1823 }
1824 else if (!strcasecmp(line, "Satisfy"))
1825 {
1826 if (!strcasecmp(value, "all"))
1827 loc->satisfy = AUTH_SATISFY_ALL;
1828 else if (!strcasecmp(value, "any"))
1829 loc->satisfy = AUTH_SATISFY_ANY;
1830 else
1831 {
1832 cupsdLogMessage(CUPSD_LOG_WARN, "Unknown Satisfy value %s on line %d.",
1833 value, linenum);
1834 return (0);
1835 }
1836 }
1837 else
1838 return (0);
1839
1840 return (1);
1841}
1842
1843
bd7854cb 1844/*
1845 * 'parse_groups()' - Parse system group names in a string.
1846 */
1847
1848static int /* O - 1 on success, 0 on failure */
1849parse_groups(const char *s) /* I - Space-delimited groups */
1850{
1851 int status; /* Return status */
1852 char value[1024], /* Value string */
1853 *valstart, /* Pointer into value */
1854 *valend, /* End of value */
1855 quote; /* Quote character */
1856 struct group *group; /* Group */
1857
1858
1859 /*
1860 * Make a copy of the string and parse out the groups...
1861 */
1862
1863 strlcpy(value, s, sizeof(value));
1864
1865 status = 1;
1866 valstart = value;
1867
1868 while (*valstart && NumSystemGroups < MAX_SYSTEM_GROUPS)
1869 {
1870 if (*valstart == '\'' || *valstart == '\"')
1871 {
1872 /*
1873 * Scan quoted name...
1874 */
1875
1876 quote = *valstart++;
1877
1878 for (valend = valstart; *valend; valend ++)
1879 if (*valend == quote)
1880 break;
1881 }
1882 else
1883 {
1884 /*
1885 * Scan space or comma-delimited name...
1886 */
1887
1888 for (valend = valstart; *valend; valend ++)
1889 if (isspace(*valend) || *valend == ',')
1890 break;
1891 }
1892
1893 if (*valend)
1894 *valend++ = '\0';
1895
1896 group = getgrnam(valstart);
1897 if (group)
1898 {
1899 cupsdSetString(SystemGroups + NumSystemGroups, valstart);
1900 SystemGroupIDs[NumSystemGroups] = group->gr_gid;
1901
1902 NumSystemGroups ++;
1903 }
1904 else
1905 status = 0;
1906
1907 endgrent();
1908
1909 valstart = valend;
1910
1911 while (*valstart == ',' || isspace(*valstart))
1912 valstart ++;
1913 }
1914
1915 return (status);
1916}
1917
1918
1919/*
1920 * 'parse_protocols()' - Parse browse protocols in a string.
1921 */
1922
1923static int /* O - Browse protocol bits */
1924parse_protocols(const char *s) /* I - Space-delimited protocols */
1925{
1926 int protocols; /* Browse protocol bits */
1927 char value[1024], /* Value string */
1928 *valstart, /* Pointer into value */
1929 *valend; /* End of value */
1930
1931
1932 /*
1933 * Loop through the value string,...
1934 */
1935
1936 strlcpy(value, s, sizeof(value));
1937
1938 protocols = 0;
1939
1940 for (valstart = value; *valstart;)
1941 {
1942 /*
1943 * Get the current space/comma-delimited protocol name...
1944 */
1945
1946 for (valend = valstart; *valend; valend ++)
1947 if (isspace(*valend & 255) || *valend == ',')
1948 break;
1949
1950 if (*valend)
1951 *valend++ = '\0';
1952
1953 /*
1954 * Add the protocol to the bitmask...
1955 */
1956
1957 if (!strcasecmp(valstart, "cups"))
1958 protocols |= BROWSE_CUPS;
1959 else if (!strcasecmp(valstart, "slp"))
1960 protocols |= BROWSE_SLP;
1961 else if (!strcasecmp(valstart, "ldap"))
1962 protocols |= BROWSE_LDAP;
1963 else if (!strcasecmp(valstart, "dnssd") || !strcasecmp(valstart, "bonjour"))
1964 protocols |= BROWSE_DNSSD;
1965 else if (!strcasecmp(valstart, "all"))
1966 protocols |= BROWSE_ALL;
1967 else
1968 return (-1);
1969
1970 for (valstart = valend; *valstart; valstart ++)
1971 if (!isspace(*valstart & 255) || *valstart != ',')
1972 break;
1973 }
1974
1975 return (protocols);
1976}
1977
1978
ef416fc2 1979/*
1980 * 'read_configuration()' - Read a configuration file.
1981 */
1982
1983static int /* O - 1 on success, 0 on failure */
1984read_configuration(cups_file_t *fp) /* I - File to read from */
1985{
1986 int i; /* Looping var */
1987 int linenum; /* Current line number */
1988 char line[HTTP_MAX_BUFFER],
1989 /* Line from file */
1990 temp[HTTP_MAX_BUFFER],
1991 /* Temporary buffer for value */
1992 temp2[HTTP_MAX_BUFFER],
1993 /* Temporary buffer 2 for value */
1994 *ptr, /* Pointer into line/temp */
1995 *value, /* Pointer to value */
bd7854cb 1996 *valueptr; /* Pointer into value */
ef416fc2 1997 int valuelen; /* Length of value */
1998 cupsd_var_t *var; /* Current variable */
1999 http_addrlist_t *addrlist, /* Address list */
2000 *addr; /* Current address */
2001 unsigned ip[4], /* Address value */
2002 mask[4]; /* Netmask value */
2003 cupsd_dirsvc_relay_t *relay; /* Relay data */
2004 cupsd_dirsvc_poll_t *pollp; /* Polling data */
2005 cupsd_location_t *location; /* Browse location */
2006 cups_file_t *incfile; /* Include file */
2007 char incname[1024]; /* Include filename */
2008 struct group *group; /* Group */
2009
2010
2011 /*
2012 * Loop through each line in the file...
2013 */
2014
2015 linenum = 0;
2016
2017 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
2018 {
2019 /*
2020 * Decode the directive...
2021 */
2022
2023 if (!strcasecmp(line, "Include"))
2024 {
2025 /*
2026 * Include filename
2027 */
2028
2029 if (value[0] == '/')
2030 strlcpy(incname, value, sizeof(incname));
2031 else
2032 snprintf(incname, sizeof(incname), "%s/%s", ServerRoot, value);
2033
2034 if ((incfile = cupsFileOpen(incname, "rb")) == NULL)
2035 cupsdLogMessage(CUPSD_LOG_ERROR,
2036 "Unable to include config file \"%s\" - %s",
2037 incname, strerror(errno));
2038 else
2039 {
2040 read_configuration(incfile);
2041 cupsFileClose(incfile);
2042 }
2043 }
2044 else if (!strcasecmp(line, "<Location"))
2045 {
2046 /*
2047 * <Location path>
2048 */
2049
2050 if (value)
2051 {
2052 linenum = read_location(fp, value, linenum);
2053 if (linenum == 0)
2054 return (0);
2055 }
2056 else
2057 {
2058 cupsdLogMessage(CUPSD_LOG_ERROR, "Syntax error on line %d.",
2059 linenum);
2060 return (0);
2061 }
2062 }
2063 else if (!strcasecmp(line, "<Policy"))
2064 {
2065 /*
2066 * <Policy name>
2067 */
2068
2069 if (value)
2070 {
2071 linenum = read_policy(fp, value, linenum);
2072 if (linenum == 0)
2073 return (0);
2074 }
2075 else
2076 {
2077 cupsdLogMessage(CUPSD_LOG_ERROR, "Syntax error on line %d.", linenum);
2078 return (0);
2079 }
2080 }
2081 else if (!strcasecmp(line, "FaxRetryInterval"))
2082 {
2083 if (value)
2084 {
2085 JobRetryInterval = atoi(value);
2086 cupsdLogMessage(CUPSD_LOG_WARN,
2087 "FaxRetryInterval is deprecated; use "
2088 "JobRetryInterval on line %d.", linenum);
2089 }
2090 else
2091 {
2092 cupsdLogMessage(CUPSD_LOG_ERROR, "Syntax error on line %d.", linenum);
2093 return (0);
2094 }
2095 }
2096 else if (!strcasecmp(line, "FaxRetryLimit"))
2097 {
2098 if (value)
2099 {
2100 JobRetryLimit = atoi(value);
2101 cupsdLogMessage(CUPSD_LOG_WARN,
2102 "FaxRetryLimit is deprecated; use "
2103 "JobRetryLimit on line %d.", linenum);
2104 }
2105 else
2106 {
2107 cupsdLogMessage(CUPSD_LOG_ERROR, "Syntax error on line %d.", linenum);
2108 return (0);
2109 }
2110 }
2111 else if (!strcasecmp(line, "Port") || !strcasecmp(line, "Listen")
2112#ifdef HAVE_SSL
2113 || !strcasecmp(line, "SSLPort") || !strcasecmp(line, "SSLListen")
2114#endif /* HAVE_SSL */
2115 )
2116 {
2117 /*
2118 * Add listening address(es) to the list...
2119 */
2120
2121 cupsd_listener_t *lis; /* New listeners array */
2122
2123
2124 /*
2125 * Get the address list...
2126 */
2127
2128 addrlist = get_address(value, IPP_PORT);
2129
2130 if (!addrlist)
2131 {
2132 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad %s address %s at line %d.", line,
2133 value, linenum);
2134 continue;
2135 }
2136
2137 /*
2138 * Add each address...
2139 */
2140
2141 for (addr = addrlist; addr; addr = addr->next)
2142 {
2143 /*
2144 * Allocate another listener...
2145 */
2146
bd7854cb 2147 if (!Listeners)
2148 Listeners = cupsArrayNew(NULL, NULL);
ef416fc2 2149
bd7854cb 2150 if (!Listeners)
ef416fc2 2151 {
2152 cupsdLogMessage(CUPSD_LOG_ERROR,
2153 "Unable to allocate %s at line %d - %s.",
2154 line, linenum, strerror(errno));
2155 break;
2156 }
2157
bd7854cb 2158 if ((lis = calloc(1, sizeof(cupsd_listener_t))) == NULL)
2159 {
2160 cupsdLogMessage(CUPSD_LOG_ERROR,
2161 "Unable to allocate %s at line %d - %s.",
2162 line, linenum, strerror(errno));
2163 break;
2164 }
2165
2166 cupsArrayAdd(Listeners, lis);
ef416fc2 2167
2168 /*
2169 * Copy the current address and log it...
2170 */
2171
ef416fc2 2172 memcpy(&(lis->address), &(addr->addr), sizeof(lis->address));
a4d04587 2173 lis->fd = -1;
ef416fc2 2174
2175#ifdef HAVE_SSL
2176 if (!strcasecmp(line, "SSLPort") || !strcasecmp(line, "SSLListen"))
2177 lis->encryption = HTTP_ENCRYPT_ALWAYS;
2178#endif /* HAVE_SSL */
2179
a4d04587 2180 httpAddrString(&lis->address, temp, sizeof(temp));
2181
ef416fc2 2182#ifdef AF_INET6
2183 if (lis->address.addr.sa_family == AF_INET6)
2184 cupsdLogMessage(CUPSD_LOG_INFO, "Listening to %s:%d (IPv6)", temp,
2185 ntohs(lis->address.ipv6.sin6_port));
2186 else
2187#endif /* AF_INET6 */
2188#ifdef AF_LOCAL
2189 if (lis->address.addr.sa_family == AF_LOCAL)
2190 cupsdLogMessage(CUPSD_LOG_INFO, "Listening to %s (Domain)", temp);
2191 else
2192#endif /* AF_LOCAL */
2193 cupsdLogMessage(CUPSD_LOG_INFO, "Listening to %s:%d (IPv4)", temp,
2194 ntohs(lis->address.ipv4.sin_port));
ef416fc2 2195 }
2196
2197 /*
2198 * Free the list...
2199 */
2200
2201 httpAddrFreeList(addrlist);
2202 }
2203 else if (!strcasecmp(line, "BrowseAddress"))
2204 {
2205 /*
2206 * Add a browse address to the list...
2207 */
2208
2209 cupsd_dirsvc_addr_t *dira; /* New browse address array */
2210
2211
2212 if (NumBrowsers == 0)
2213 dira = malloc(sizeof(cupsd_dirsvc_addr_t));
2214 else
2215 dira = realloc(Browsers, (NumBrowsers + 1) * sizeof(cupsd_dirsvc_addr_t));
2216
2217 if (!dira)
2218 {
2219 cupsdLogMessage(CUPSD_LOG_ERROR,
2220 "Unable to allocate BrowseAddress at line %d - %s.",
2221 linenum, strerror(errno));
2222 continue;
2223 }
2224
2225 Browsers = dira;
2226 dira += NumBrowsers;
2227
2228 memset(dira, 0, sizeof(cupsd_dirsvc_addr_t));
2229
2230 if (!strcasecmp(value, "@LOCAL"))
2231 {
2232 /*
2233 * Send browse data to all local interfaces...
2234 */
2235
2236 strcpy(dira->iface, "*");
2237 NumBrowsers ++;
2238 }
2239 else if (!strncasecmp(value, "@IF(", 4))
2240 {
2241 /*
2242 * Send browse data to the named interface...
2243 */
2244
2245 strlcpy(dira->iface, value + 4, sizeof(Browsers[0].iface));
2246
2247 ptr = dira->iface + strlen(dira->iface) - 1;
2248 if (*ptr == ')')
2249 *ptr = '\0';
2250
2251 NumBrowsers ++;
2252 }
2253 else if ((addrlist = get_address(value, BrowsePort)) != NULL)
2254 {
2255 /*
2256 * Only IPv4 addresses are supported...
2257 */
2258
2259 for (addr = addrlist; addr; addr = addr->next)
2260 if (addr->addr.addr.sa_family == AF_INET)
2261 break;
2262
2263 if (addr)
2264 {
2265 memcpy(&(dira->to), &(addrlist->addr), sizeof(dira->to));
2266 httpAddrString(&(dira->to), temp, sizeof(temp));
2267
2268 cupsdLogMessage(CUPSD_LOG_INFO,
2269 "Sending browsing info to %s:%d (IPv4)",
2270 temp, ntohs(dira->to.ipv4.sin_port));
2271
2272 NumBrowsers ++;
2273 }
2274 else
2275 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad BrowseAddress %s at line %d.",
2276 value, linenum);
2277
2278 httpAddrFreeList(addrlist);
2279 }
2280 else
2281 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad BrowseAddress %s at line %d.",
2282 value, linenum);
2283 }
2284 else if (!strcasecmp(line, "BrowseOrder"))
2285 {
2286 /*
2287 * "BrowseOrder Deny,Allow" or "BrowseOrder Allow,Deny"...
2288 */
2289
2290 if ((location = cupsdFindLocation("CUPS_INTERNAL_BROWSE_ACL")) == NULL)
2291 location = cupsdAddLocation("CUPS_INTERNAL_BROWSE_ACL");
2292
2293 if (location == NULL)
2294 cupsdLogMessage(CUPSD_LOG_ERROR,
2295 "Unable to initialize browse access control list!");
2296 else if (!strncasecmp(value, "deny", 4))
2297 location->order_type = AUTH_ALLOW;
2298 else if (!strncasecmp(value, "allow", 5))
2299 location->order_type = AUTH_DENY;
2300 else
2301 cupsdLogMessage(CUPSD_LOG_ERROR,
2302 "Unknown BrowseOrder value %s on line %d.",
2303 value, linenum);
2304 }
2305 else if (!strcasecmp(line, "BrowseProtocols") ||
2306 !strcasecmp(line, "BrowseLocalProtocols") ||
2307 !strcasecmp(line, "BrowseRemoteProtocols"))
2308 {
2309 /*
bd7854cb 2310 * "BrowseProtocols name [... name]"
2311 * "BrowseLocalProtocols name [... name]"
2312 * "BrowseRemoteProtocols name [... name]"
ef416fc2 2313 */
2314
bd7854cb 2315 int protocols = parse_protocols(value);
ef416fc2 2316
bd7854cb 2317 if (protocols < 0)
ef416fc2 2318 {
bd7854cb 2319 cupsdLogMessage(CUPSD_LOG_ERROR,
2320 "Unknown browse protocol \"%s\" on line %d.",
2321 value, linenum);
2322 break;
ef416fc2 2323 }
bd7854cb 2324
2325 if (strcasecmp(line, "BrowseLocalProtocols"))
2326 BrowseRemoteProtocols = protocols;
2327 if (strcasecmp(line, "BrowseRemoteProtocols"))
2328 BrowseLocalProtocols = protocols;
ef416fc2 2329 }
2330 else if (!strcasecmp(line, "BrowseAllow") ||
2331 !strcasecmp(line, "BrowseDeny"))
2332 {
2333 /*
2334 * BrowseAllow [From] host/ip...
2335 * BrowseDeny [From] host/ip...
2336 */
2337
2338 if ((location = cupsdFindLocation("CUPS_INTERNAL_BROWSE_ACL")) == NULL)
2339 location = cupsdAddLocation("CUPS_INTERNAL_BROWSE_ACL");
2340
2341 if (location == NULL)
2342 cupsdLogMessage(CUPSD_LOG_ERROR,
2343 "Unable to initialize browse access control list!");
2344 else
2345 {
2346 if (!strncasecmp(value, "from ", 5))
2347 {
2348 /*
2349 * Strip leading "from"...
2350 */
2351
2352 value += 5;
2353
2354 while (isspace(*value))
2355 value ++;
2356 }
2357
2358 /*
2359 * Figure out what form the allow/deny address takes:
2360 *
2361 * All
2362 * None
2363 * *.domain.com
2364 * .domain.com
2365 * host.domain.com
2366 * nnn.*
2367 * nnn.nnn.*
2368 * nnn.nnn.nnn.*
2369 * nnn.nnn.nnn.nnn
2370 * nnn.nnn.nnn.nnn/mm
2371 * nnn.nnn.nnn.nnn/mmm.mmm.mmm.mmm
2372 */
2373
2374 if (!strcasecmp(value, "all"))
2375 {
2376 /*
2377 * All hosts...
2378 */
2379
2380 if (!strcasecmp(line, "BrowseAllow"))
2381 cupsdAllowIP(location, zeros, zeros);
2382 else
2383 cupsdDenyIP(location, zeros, zeros);
2384 }
2385 else if (!strcasecmp(value, "none"))
2386 {
2387 /*
2388 * No hosts...
2389 */
2390
2391 if (!strcasecmp(line, "BrowseAllow"))
2392 cupsdAllowIP(location, ones, zeros);
2393 else
2394 cupsdDenyIP(location, ones, zeros);
2395 }
2396 else if (value[0] == '*' || value[0] == '.' || !isdigit(value[0]))
2397 {
2398 /*
2399 * Host or domain name...
2400 */
2401
2402 if (value[0] == '*')
2403 value ++;
2404
2405 if (!strcasecmp(line, "BrowseAllow"))
2406 cupsdAllowHost(location, value);
2407 else
2408 cupsdDenyHost(location, value);
2409 }
2410 else
2411 {
2412 /*
2413 * One of many IP address forms...
2414 */
2415
2416 if (!get_addr_and_mask(value, ip, mask))
2417 {
2418 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad netmask value %s on line %d.",
2419 value, linenum);
2420 break;
2421 }
2422
2423 if (!strcasecmp(line, "BrowseAllow"))
2424 cupsdAllowIP(location, ip, mask);
2425 else
2426 cupsdDenyIP(location, ip, mask);
2427 }
2428 }
2429 }
2430 else if (!strcasecmp(line, "BrowseRelay"))
2431 {
2432 /*
2433 * BrowseRelay [from] source [to] destination
2434 */
2435
2436 if (NumRelays == 0)
2437 relay = malloc(sizeof(cupsd_dirsvc_relay_t));
2438 else
2439 relay = realloc(Relays, (NumRelays + 1) * sizeof(cupsd_dirsvc_relay_t));
2440
2441 if (!relay)
2442 {
2443 cupsdLogMessage(CUPSD_LOG_ERROR,
2444 "Unable to allocate BrowseRelay at line %d - %s.",
2445 linenum, strerror(errno));
2446 continue;
2447 }
2448
2449 Relays = relay;
2450 relay += NumRelays;
2451
2452 memset(relay, 0, sizeof(cupsd_dirsvc_relay_t));
2453
2454 if (!strncasecmp(value, "from ", 5))
2455 {
2456 /*
2457 * Strip leading "from"...
2458 */
2459
2460 value += 5;
2461
2462 while (isspace(*value))
2463 value ++;
2464 }
2465
2466 /*
2467 * Figure out what form the from address takes:
2468 *
2469 * *.domain.com
2470 * .domain.com
2471 * host.domain.com
2472 * nnn.*
2473 * nnn.nnn.*
2474 * nnn.nnn.nnn.*
2475 * nnn.nnn.nnn.nnn
2476 * nnn.nnn.nnn.nnn/mm
2477 * nnn.nnn.nnn.nnn/mmm.mmm.mmm.mmm
2478 */
2479
2480 if (value[0] == '*' || value[0] == '.' || !isdigit(value[0]))
2481 {
2482 /*
2483 * Host or domain name...
2484 */
2485
2486 if (value[0] == '*')
2487 value ++;
2488
2489 strlcpy(temp, value, sizeof(temp));
2490 if ((ptr = strchr(temp, ' ')) != NULL)
2491 *ptr = '\0';
2492
2493 relay->from.type = AUTH_NAME;
2494 relay->from.mask.name.name = strdup(temp);
2495 relay->from.mask.name.length = strlen(temp);
2496 }
2497 else
2498 {
2499 /*
2500 * One of many IP address forms...
2501 */
2502
2503 if (!get_addr_and_mask(value, ip, mask))
2504 {
2505 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad netmask value %s on line %d.",
2506 value, linenum);
2507 break;
2508 }
2509
2510 relay->from.type = AUTH_IP;
2511 memcpy(relay->from.mask.ip.address, ip,
2512 sizeof(relay->from.mask.ip.address));
2513 memcpy(relay->from.mask.ip.netmask, mask,
2514 sizeof(relay->from.mask.ip.netmask));
2515 }
2516
2517 /*
2518 * Skip value and trailing whitespace...
2519 */
2520
2521 for (; *value; value ++)
2522 if (isspace(*value))
2523 break;
2524
2525 while (isspace(*value))
2526 value ++;
2527
2528 if (!strncasecmp(value, "to ", 3))
2529 {
2530 /*
2531 * Strip leading "to"...
2532 */
2533
2534 value += 3;
2535
2536 while (isspace(*value))
2537 value ++;
2538 }
2539
2540 /*
2541 * Get "to" address and port...
2542 */
2543
2544 if ((addrlist = get_address(value, BrowsePort)) != NULL)
2545 {
2546 /*
2547 * Only IPv4 addresses are supported...
2548 */
2549
2550 for (addr = addrlist; addr; addr = addr->next)
2551 if (addr->addr.addr.sa_family == AF_INET)
2552 break;
2553
2554 if (addr)
2555 {
2556 memcpy(&(relay->to), &(addrlist->addr), sizeof(relay->to));
2557
2558 httpAddrString(&(relay->to), temp, sizeof(temp));
2559
2560 if (relay->from.type == AUTH_IP)
2561 snprintf(temp2, sizeof(temp2), "%u.%u.%u.%u/%u.%u.%u.%u",
2562 relay->from.mask.ip.address[0],
2563 relay->from.mask.ip.address[1],
2564 relay->from.mask.ip.address[2],
2565 relay->from.mask.ip.address[3],
2566 relay->from.mask.ip.netmask[0],
2567 relay->from.mask.ip.netmask[1],
2568 relay->from.mask.ip.netmask[2],
2569 relay->from.mask.ip.netmask[3]);
2570 else
2571 strlcpy(temp2, relay->from.mask.name.name, sizeof(temp2));
2572
2573 cupsdLogMessage(CUPSD_LOG_INFO, "Relaying from %s to %s:%d (IPv4)",
2574 temp, temp2, ntohs(relay->to.ipv4.sin_port));
2575
2576 NumRelays ++;
2577 }
2578 else
2579 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad relay address %s at line %d.",
2580 value, linenum);
2581
2582 httpAddrFreeList(addrlist);
2583 }
2584 else
2585 {
2586 if (relay->from.type == AUTH_NAME)
2587 free(relay->from.mask.name.name);
2588
2589 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad relay address %s at line %d.",
2590 value, linenum);
2591 }
2592 }
2593 else if (!strcasecmp(line, "BrowsePoll"))
2594 {
2595 /*
2596 * BrowsePoll address[:port]
2597 */
2598
2599 char *portname; /* Port name */
2600 int portnum; /* Port number */
2601 struct servent *service; /* Service */
2602
2603
2604 /*
2605 * Extract the port name from the address...
2606 */
2607
2608 if ((portname = strrchr(value, ':')) != NULL && !strchr(portname, ']'))
2609 {
2610 *portname++ = '\0';
2611
2612 if (isdigit(*portname & 255))
2613 portnum = atoi(portname);
2614 else if ((service = getservbyname(portname, NULL)) != NULL)
2615 portnum = ntohs(service->s_port);
2616 else
2617 {
2618 cupsdLogMessage(CUPSD_LOG_ERROR, "Lookup of service \"%s\" failed!",
2619 portname);
2620 continue;
2621 }
2622 }
2623 else
2624 portnum = ippPort();
2625
2626 /*
2627 * Add the poll entry...
2628 */
2629
2630 if (NumPolled == 0)
2631 pollp = malloc(sizeof(cupsd_dirsvc_poll_t));
2632 else
2633 pollp = realloc(Polled, (NumPolled + 1) * sizeof(cupsd_dirsvc_poll_t));
2634
2635 if (!pollp)
2636 {
2637 cupsdLogMessage(CUPSD_LOG_ERROR,
2638 "Unable to allocate BrowsePoll at line %d - %s.",
2639 linenum, strerror(errno));
2640 continue;
2641 }
2642
2643 Polled = pollp;
2644 pollp += NumPolled;
2645
2646 NumPolled ++;
2647 memset(pollp, 0, sizeof(cupsd_dirsvc_poll_t));
2648
2649 strlcpy(pollp->hostname, value, sizeof(pollp->hostname));
2650 pollp->port = portnum;
2651
2652 cupsdLogMessage(CUPSD_LOG_INFO, "Polling %s:%d", pollp->hostname,
2653 pollp->port);
2654 }
2655 else if (!strcasecmp(line, "DefaultAuthType"))
2656 {
2657 /*
2658 * DefaultAuthType {basic,digest,basicdigest}
2659 */
2660
2661 if (!strcasecmp(value, "basic"))
2662 DefaultAuthType = AUTH_BASIC;
2663 else if (!strcasecmp(value, "digest"))
2664 DefaultAuthType = AUTH_DIGEST;
2665 else if (!strcasecmp(value, "basicdigest"))
2666 DefaultAuthType = AUTH_BASICDIGEST;
2667 else
2668 {
2669 cupsdLogMessage(CUPSD_LOG_WARN,
2670 "Unknown default authorization type %s on line %d.",
2671 value, linenum);
2672 return (0);
2673 }
2674 }
4744bd90 2675#ifdef HAVE_SSL
2676 else if (!strcasecmp(line, "DefaultEncryption"))
2677 {
2678 /*
2679 * DefaultEncryption {Never,IfRequested,Required}
2680 */
2681
2682 if (!value || !strcasecmp(value, "never"))
2683 DefaultEncryption = HTTP_ENCRYPT_NEVER;
2684 else if (!strcasecmp(value, "required"))
2685 DefaultEncryption = HTTP_ENCRYPT_REQUIRED;
2686 else if (!strcasecmp(value, "ifrequested"))
2687 DefaultEncryption = HTTP_ENCRYPT_IF_REQUESTED;
2688 else
2689 {
2690 cupsdLogMessage(CUPSD_LOG_WARN,
2691 "Unknown default encryption %s on line %d.",
2692 value, linenum);
2693 return (0);
2694 }
2695 }
2696#endif /* HAVE_SSL */
ef416fc2 2697 else if (!strcasecmp(line, "User"))
2698 {
2699 /*
2700 * User ID to run as...
2701 */
2702
2703 if (value && isdigit(value[0] & 255))
2704 {
2705 int uid = atoi(value);
2706
2707 if (!uid)
2708 cupsdLogMessage(CUPSD_LOG_ERROR,
2709 "Will not use User 0 as specified on line %d "
2710 "for security reasons. You must use a non-"
2711 "privileged account instead.",
2712 linenum);
2713 else
2714 User = atoi(value);
2715 }
2716 else if (value)
2717 {
2718 struct passwd *p; /* Password information */
2719
2720 endpwent();
2721 p = getpwnam(value);
2722
2723 if (p)
2724 {
2725 if (!p->pw_uid)
2726 cupsdLogMessage(CUPSD_LOG_ERROR,
2727 "Will not use User %s (UID=0) as specified on line "
2728 "%d for security reasons. You must use a non-"
2729 "privileged account instead.",
2730 value, linenum);
2731 else
2732 User = p->pw_uid;
2733 }
2734 else
2735 cupsdLogMessage(CUPSD_LOG_ERROR,
2736 "Unknown User \"%s\" on line %d, ignoring!",
2737 value, linenum);
2738 }
2739 else
2740 cupsdLogMessage(CUPSD_LOG_ERROR,
2741 "User directive on line %d missing the username!",
2742 linenum);
2743 }
2744 else if (!strcasecmp(line, "Group"))
2745 {
2746 /*
2747 * Group ID to run as...
2748 */
2749
2750 if (isdigit(value[0]))
2751 Group = atoi(value);
2752 else
2753 {
2754 endgrent();
2755 group = getgrnam(value);
2756
2757 if (group != NULL)
2758 Group = group->gr_gid;
2759 else
2760 cupsdLogMessage(CUPSD_LOG_ERROR,
2761 "Unknown Group \"%s\" on line %d, ignoring!",
2762 value, linenum);
2763 }
2764 }
2765 else if (!strcasecmp(line, "SystemGroup"))
2766 {
2767 /*
bd7854cb 2768 * SystemGroup (admin) group(s)...
ef416fc2 2769 */
2770
bd7854cb 2771 if (!parse_groups(value))
2772 cupsdLogMessage(CUPSD_LOG_ERROR,
2773 "Unknown SystemGroup \"%s\" on line %d, ignoring!",
2774 value, linenum);
ef416fc2 2775 }
2776 else if (!strcasecmp(line, "HostNameLookups"))
2777 {
2778 /*
2779 * Do hostname lookups?
2780 */
2781
2782 if (!strcasecmp(value, "off"))
2783 HostNameLookups = 0;
2784 else if (!strcasecmp(value, "on"))
2785 HostNameLookups = 1;
2786 else if (!strcasecmp(value, "double"))
2787 HostNameLookups = 2;
2788 else
2789 cupsdLogMessage(CUPSD_LOG_WARN, "Unknown HostNameLookups %s on line %d.",
2790 value, linenum);
2791 }
2792 else if (!strcasecmp(line, "LogLevel"))
2793 {
2794 /*
2795 * Amount of logging to do...
2796 */
2797
2798 if (!strcasecmp(value, "debug2"))
2799 LogLevel = CUPSD_LOG_DEBUG2;
2800 else if (!strcasecmp(value, "debug"))
2801 LogLevel = CUPSD_LOG_DEBUG;
2802 else if (!strcasecmp(value, "info"))
2803 LogLevel = CUPSD_LOG_INFO;
2804 else if (!strcasecmp(value, "notice"))
2805 LogLevel = CUPSD_LOG_NOTICE;
2806 else if (!strcasecmp(value, "warn"))
2807 LogLevel = CUPSD_LOG_WARN;
2808 else if (!strcasecmp(value, "error"))
2809 LogLevel = CUPSD_LOG_ERROR;
2810 else if (!strcasecmp(value, "crit"))
2811 LogLevel = CUPSD_LOG_CRIT;
2812 else if (!strcasecmp(value, "alert"))
2813 LogLevel = CUPSD_LOG_ALERT;
2814 else if (!strcasecmp(value, "emerg"))
2815 LogLevel = CUPSD_LOG_EMERG;
2816 else if (!strcasecmp(value, "none"))
2817 LogLevel = CUPSD_LOG_NONE;
2818 else
2819 cupsdLogMessage(CUPSD_LOG_WARN, "Unknown LogLevel %s on line %d.",
2820 value, linenum);
2821 }
2822 else if (!strcasecmp(line, "PrintcapFormat"))
2823 {
2824 /*
2825 * Format of printcap file?
2826 */
2827
2828 if (!strcasecmp(value, "bsd"))
2829 PrintcapFormat = PRINTCAP_BSD;
2830 else if (!strcasecmp(value, "solaris"))
2831 PrintcapFormat = PRINTCAP_SOLARIS;
2832 else
2833 cupsdLogMessage(CUPSD_LOG_WARN, "Unknown PrintcapFormat %s on line %d.",
2834 value, linenum);
2835 }
2836 else if (!strcasecmp(line, "ServerTokens"))
2837 {
2838 /*
2839 * Set the string used for the Server header...
2840 */
2841
2842 struct utsname plat; /* Platform info */
2843
2844
2845 uname(&plat);
2846
2847 if (!strcasecmp(value, "ProductOnly"))
2848 cupsdSetString(&ServerHeader, "CUPS");
2849 else if (!strcasecmp(value, "Major"))
2850 cupsdSetString(&ServerHeader, "CUPS/1");
2851 else if (!strcasecmp(value, "Minor"))
bd7854cb 2852 cupsdSetString(&ServerHeader, "CUPS/1.2");
ef416fc2 2853 else if (!strcasecmp(value, "Minimal"))
2854 cupsdSetString(&ServerHeader, CUPS_MINIMAL);
2855 else if (!strcasecmp(value, "OS"))
2856 cupsdSetStringf(&ServerHeader, CUPS_MINIMAL " (%s)", plat.sysname);
2857 else if (!strcasecmp(value, "Full"))
2858 cupsdSetStringf(&ServerHeader, CUPS_MINIMAL " (%s) IPP/1.1",
2859 plat.sysname);
2860 else if (!strcasecmp(value, "None"))
2861 cupsdClearString(&ServerHeader);
2862 else
2863 cupsdLogMessage(CUPSD_LOG_WARN, "Unknown ServerTokens %s on line %d.",
2864 value, linenum);
2865 }
2866 else if (!strcasecmp(line, "PassEnv"))
2867 {
2868 /*
2869 * PassEnv variable [... variable]
2870 */
2871
2872 for (; *value;)
2873 {
2874 for (valuelen = 0; value[valuelen]; valuelen ++)
2875 if (isspace(value[valuelen]) || value[valuelen] == ',')
2876 break;
2877
2878 if (value[valuelen])
2879 {
2880 value[valuelen] = '\0';
2881 valuelen ++;
2882 }
2883
2884 cupsdSetEnv(value, NULL);
2885
2886 for (value += valuelen; *value; value ++)
2887 if (!isspace(*value) || *value != ',')
2888 break;
2889 }
2890 }
2891 else if (!strcasecmp(line, "SetEnv"))
2892 {
2893 /*
2894 * SetEnv variable value
2895 */
2896
2897 for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++);
2898
2899 if (*valueptr)
2900 {
2901 /*
2902 * Found a value...
2903 */
2904
2905 while (isspace(*valueptr & 255))
2906 *valueptr++ = '\0';
2907
2908 cupsdSetEnv(value, valueptr);
2909 }
2910 else
2911 cupsdLogMessage(CUPSD_LOG_ERROR,
2912 "Missing value for SetEnv directive on line %d.",
2913 linenum);
2914 }
2915 else
2916 {
2917 /*
2918 * Find a simple variable in the list...
2919 */
2920
2921 for (i = NUM_VARS, var = variables; i > 0; i --, var ++)
2922 if (!strcasecmp(line, var->name))
2923 break;
2924
2925 if (i == 0)
2926 {
2927 /*
2928 * Unknown directive! Output an error message and continue...
2929 */
2930
2931 cupsdLogMessage(CUPSD_LOG_ERROR, "Unknown directive %s on line %d.",
2932 line, linenum);
2933 continue;
2934 }
2935
2936 switch (var->type)
2937 {
2938 case CUPSD_VARTYPE_INTEGER :
2939 {
2940 int n; /* Number */
2941 char *units; /* Units */
2942
2943
2944 n = strtol(value, &units, 0);
2945
2946 if (units && *units)
2947 {
2948 if (tolower(units[0] & 255) == 'g')
2949 n *= 1024 * 1024 * 1024;
2950 else if (tolower(units[0] & 255) == 'm')
2951 n *= 1024 * 1024;
2952 else if (tolower(units[0] & 255) == 'k')
2953 n *= 1024;
2954 else if (tolower(units[0] & 255) == 't')
2955 n *= 262144;
2956 }
2957
2958 *((int *)var->ptr) = n;
2959 }
2960 break;
2961
2962 case CUPSD_VARTYPE_BOOLEAN :
2963 if (!strcasecmp(value, "true") ||
2964 !strcasecmp(value, "on") ||
2965 !strcasecmp(value, "enabled") ||
2966 !strcasecmp(value, "yes") ||
2967 atoi(value) != 0)
2968 *((int *)var->ptr) = TRUE;
2969 else if (!strcasecmp(value, "false") ||
2970 !strcasecmp(value, "off") ||
2971 !strcasecmp(value, "disabled") ||
2972 !strcasecmp(value, "no") ||
2973 !strcasecmp(value, "0"))
2974 *((int *)var->ptr) = FALSE;
2975 else
2976 cupsdLogMessage(CUPSD_LOG_ERROR,
2977 "Unknown boolean value %s on line %d.",
2978 value, linenum);
2979 break;
2980
2981 case CUPSD_VARTYPE_STRING :
2982 cupsdSetString((char **)var->ptr, value);
2983 break;
2984 }
2985 }
2986 }
2987
2988 return (1);
2989}
2990
2991
2992/*
2993 * 'read_location()' - Read a <Location path> definition.
2994 */
2995
2996static int /* O - New line number or 0 on error */
2997read_location(cups_file_t *fp, /* I - Configuration file */
2998 char *location, /* I - Location name/path */
2999 int linenum) /* I - Current line number */
3000{
3001 cupsd_location_t *loc, /* New location */
3002 *parent; /* Parent location */
3003 char line[HTTP_MAX_BUFFER],
3004 /* Line buffer */
3005 *value, /* Value for directive */
3006 *valptr; /* Pointer into value */
3007
3008
3009 if ((parent = cupsdAddLocation(location)) == NULL)
3010 return (0);
3011
3012 parent->limit = AUTH_LIMIT_ALL;
3013 loc = parent;
3014
3015 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
3016 {
3017 /*
3018 * Decode the directive...
3019 */
3020
3021 if (!strcasecmp(line, "</Location>"))
3022 return (linenum);
3023 else if (!strcasecmp(line, "<Limit") ||
3024 !strcasecmp(line, "<LimitExcept"))
3025 {
3026 if (!value)
3027 {
3028 cupsdLogMessage(CUPSD_LOG_ERROR, "Syntax error on line %d.", linenum);
3029 return (0);
3030 }
3031
3032 if ((loc = cupsdCopyLocation(&parent)) == NULL)
3033 return (0);
3034
3035 loc->limit = 0;
3036 while (*value)
3037 {
3038 for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
3039
3040 if (*valptr)
3041 *valptr++ = '\0';
3042
3043 if (!strcmp(value, "ALL"))
3044 loc->limit = AUTH_LIMIT_ALL;
3045 else if (!strcmp(value, "GET"))
3046 loc->limit |= AUTH_LIMIT_GET;
3047 else if (!strcmp(value, "HEAD"))
3048 loc->limit |= AUTH_LIMIT_HEAD;
3049 else if (!strcmp(value, "OPTIONS"))
3050 loc->limit |= AUTH_LIMIT_OPTIONS;
3051 else if (!strcmp(value, "POST"))
3052 loc->limit |= AUTH_LIMIT_POST;
3053 else if (!strcmp(value, "PUT"))
3054 loc->limit |= AUTH_LIMIT_PUT;
3055 else if (!strcmp(value, "TRACE"))
3056 loc->limit |= AUTH_LIMIT_TRACE;
3057 else
3058 cupsdLogMessage(CUPSD_LOG_WARN, "Unknown request type %s on line %d!",
3059 value, linenum);
3060
3061 for (value = valptr; isspace(*value & 255); value ++);
3062 }
3063
3064 if (!strcasecmp(line, "<LimitExcept"))
3065 loc->limit = AUTH_LIMIT_ALL ^ loc->limit;
3066
3067 parent->limit &= ~loc->limit;
3068 }
3069 else if (!strcasecmp(line, "</Limit>"))
3070 loc = parent;
3071 else if (!parse_aaa(loc, line, value, linenum))
3072 {
3073 cupsdLogMessage(CUPSD_LOG_ERROR,
3074 "Unknown Location directive %s on line %d.",
3075 line, linenum);
3076 return (0);
3077 }
3078 }
3079
3080 cupsdLogMessage(CUPSD_LOG_ERROR,
3081 "Unexpected end-of-file at line %d while reading location!",
3082 linenum);
3083
3084 return (0);
3085}
3086
3087
3088/*
3089 * 'read_policy()' - Read a <Policy name> definition.
3090 */
3091
3092static int /* O - New line number or 0 on error */
3093read_policy(cups_file_t *fp, /* I - Configuration file */
3094 char *policy, /* I - Location name/path */
3095 int linenum) /* I - Current line number */
3096{
3097 int i; /* Looping var */
3098 cupsd_policy_t *pol; /* Policy */
3099 cupsd_location_t *op; /* Policy operation */
3100 int num_ops; /* Number of IPP operations */
3101 ipp_op_t ops[100]; /* Operations */
3102 char line[HTTP_MAX_BUFFER],
3103 /* Line buffer */
3104 *value, /* Value for directive */
3105 *valptr; /* Pointer into value */
3106
3107
3108 /*
3109 * Create the policy...
3110 */
3111
3112 if ((pol = cupsdAddPolicy(policy)) == NULL)
3113 return (0);
3114
3115 /*
3116 * Read from the file...
3117 */
3118
3119 op = NULL;
3120 num_ops = 0;
3121
3122 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
3123 {
3124 /*
3125 * Decode the directive...
3126 */
3127
3128 if (!strcasecmp(line, "</Policy>"))
3129 {
3130 if (op)
3131 cupsdLogMessage(CUPSD_LOG_WARN,
3132 "Missing </Limit> before </Policy> on line %d!",
3133 linenum);
3134
3135 return (linenum);
3136 }
3137 else if (!strcasecmp(line, "<Limit") && !op)
3138 {
3139 if (!value)
3140 {
3141 cupsdLogMessage(CUPSD_LOG_ERROR, "Syntax error on line %d.", linenum);
3142 return (0);
3143 }
3144
3145 /*
3146 * Scan for IPP operation names...
3147 */
3148
3149 num_ops = 0;
3150
3151 while (*value)
3152 {
3153 for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
3154
3155 if (*valptr)
3156 *valptr++ = '\0';
3157
3158 if (num_ops < (int)(sizeof(ops) / sizeof(ops[0])))
3159 {
3160 if (!strcasecmp(value, "All"))
3161 ops[num_ops] = IPP_ANY_OPERATION;
3162 else if ((ops[num_ops] = ippOpValue(value)) == IPP_BAD_OPERATION)
3163 cupsdLogMessage(CUPSD_LOG_ERROR,
3164 "Bad IPP operation name \"%s\" on line %d!",
3165 value, linenum);
3166 else
3167 num_ops ++;
3168 }
3169 else
3170 cupsdLogMessage(CUPSD_LOG_ERROR,
3171 "Too many operations listed on line %d!",
3172 linenum);
3173
3174 for (value = valptr; isspace(*value & 255); value ++);
3175 }
3176
3177 /*
3178 * If none are specified, apply the policy to all operations...
3179 */
3180
3181 if (num_ops == 0)
3182 {
3183 ops[0] = IPP_ANY_OPERATION;
3184 num_ops = 1;
3185 }
3186
3187 /*
3188 * Add a new policy for the first operation...
3189 */
3190
3191 op = cupsdAddPolicyOp(pol, NULL, ops[0]);
3192 }
3193 else if (!strcasecmp(line, "</Limit>") && op)
3194 {
3195 /*
3196 * Finish the current operation limit...
3197 */
3198
3199 if (num_ops > 1)
3200 {
3201 /*
3202 * Copy the policy to the other operations...
3203 */
3204
3205 for (i = 1; i < num_ops; i ++)
3206 cupsdAddPolicyOp(pol, op, ops[i]);
3207 }
3208
3209 op = NULL;
3210 }
3211 else if (!op)
3212 {
3213 cupsdLogMessage(CUPSD_LOG_ERROR,
3214 "Missing <Limit ops> directive before %s on line %d.",
3215 line, linenum);
3216 return (0);
3217 }
3218 else if (!parse_aaa(op, line, value, linenum))
3219 {
3220 if (op)
3221 cupsdLogMessage(CUPSD_LOG_ERROR,
3222 "Unknown Policy Limit directive %s on line %d.",
3223 line, linenum);
3224 else
3225 cupsdLogMessage(CUPSD_LOG_ERROR,
3226 "Unknown Policy directive %s on line %d.",
3227 line, linenum);
3228
3229 return (0);
3230 }
3231 }
3232
3233 cupsdLogMessage(CUPSD_LOG_ERROR,
3234 "Unexpected end-of-file at line %d while reading policy \"%s\"!",
3235 linenum, policy);
3236
3237 return (0);
3238}
3239
3240
3241/*
e1d6a774 3242 * End of "$Id: conf.c 5289 2006-03-14 11:54:45Z mike $".
ef416fc2 3243 */