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