]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/conf.c
Load cups into easysw/current.
[thirdparty/cups.git] / scheduler / conf.c
1 /*
2 * "$Id: conf.c 6649 2007-07-11 21:46:42Z 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 #include <cups/dir.h>
41
42
43 /*
44 * Possibly missing network definitions...
45 */
46
47 #ifndef INADDR_NONE
48 # define INADDR_NONE 0xffffffff
49 #endif /* !INADDR_NONE */
50
51
52 /*
53 * Configuration variable structure...
54 */
55
56 typedef enum
57 {
58 CUPSD_VARTYPE_INTEGER, /* Integer option */
59 CUPSD_VARTYPE_STRING, /* String option */
60 CUPSD_VARTYPE_BOOLEAN /* Boolean 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_STRING },
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_STRING },
157 #ifdef HAVE_SSL
158 { "ServerCertificate", &ServerCertificate, CUPSD_VARTYPE_STRING },
159 # if defined(HAVE_LIBSSL) || defined(HAVE_GNUTLS)
160 { "ServerKey", &ServerKey, CUPSD_VARTYPE_STRING },
161 # endif /* HAVE_LIBSSL || HAVE_GNUTLS */
162 #endif /* HAVE_SSL */
163 { "ServerName", &ServerName, CUPSD_VARTYPE_STRING },
164 { "ServerRoot", &ServerRoot, CUPSD_VARTYPE_STRING },
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_STRING },
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 if (!strncmp(TempDir, RequestRoot, strlen(RequestRoot)))
855 {
856 /*
857 * Clean out the temporary directory...
858 */
859
860 cups_dir_t *dir; /* Temporary directory */
861 cups_dentry_t *dent; /* Directory entry */
862 char tempfile[1024]; /* Temporary filename */
863
864
865 if ((dir = cupsDirOpen(TempDir)) != NULL)
866 {
867 cupsdLogMessage(CUPSD_LOG_INFO,
868 "Cleaning out old temporary files in \"%s\"...", TempDir);
869
870 while ((dent = cupsDirRead(dir)) != NULL)
871 {
872 snprintf(tempfile, sizeof(tempfile), "%s/%s", TempDir, dent->filename);
873
874 if (unlink(tempfile))
875 cupsdLogMessage(CUPSD_LOG_ERROR,
876 "Unable to remove temporary file \"%s\" - %s",
877 tempfile, strerror(errno));
878 else
879 cupsdLogMessage(CUPSD_LOG_DEBUG, "Removed temporary file \"%s\"...",
880 tempfile);
881 }
882
883 cupsDirClose(dir);
884 }
885 else
886 cupsdLogMessage(CUPSD_LOG_ERROR,
887 "Unable to open temporary directory \"%s\" - %s",
888 TempDir, strerror(errno));
889 }
890
891 /*
892 * Setup environment variables...
893 */
894
895 cupsdInitEnv();
896
897 /*
898 * Check the MaxClients setting, and then allocate memory for it...
899 */
900
901 if (MaxClients > (MaxFDs / 3) || MaxClients <= 0)
902 {
903 if (MaxClients > 0)
904 cupsdLogMessage(CUPSD_LOG_INFO, "MaxClients limited to 1/3 (%d) of the file descriptor limit (%d)...",
905 MaxFDs / 3, MaxFDs);
906
907 MaxClients = MaxFDs / 3;
908 }
909
910 cupsdLogMessage(CUPSD_LOG_INFO, "Configured for up to %d clients.",
911 MaxClients);
912
913 /*
914 * Check the MaxActiveJobs setting; limit to 1/3 the available
915 * file descriptors, since we need a pipe for each job...
916 */
917
918 if (MaxActiveJobs > (MaxFDs / 3))
919 MaxActiveJobs = MaxFDs / 3;
920
921 if (Classification && !strcasecmp(Classification, "none"))
922 cupsdClearString(&Classification);
923
924 if (Classification)
925 cupsdLogMessage(CUPSD_LOG_INFO, "Security set to \"%s\"", Classification);
926
927 /*
928 * Update the MaxClientsPerHost value, as needed...
929 */
930
931 if (MaxClientsPerHost <= 0)
932 MaxClientsPerHost = MaxClients;
933
934 if (MaxClientsPerHost > MaxClients)
935 MaxClientsPerHost = MaxClients;
936
937 cupsdLogMessage(CUPSD_LOG_INFO,
938 "Allowing up to %d client connections per host.",
939 MaxClientsPerHost);
940
941 /*
942 * Make sure that BrowseTimeout is at least twice the interval...
943 */
944
945 if (BrowseTimeout < (2 * BrowseInterval) || BrowseTimeout <= 0)
946 {
947 cupsdLogMessage(CUPSD_LOG_ALERT, "Invalid BrowseTimeout value %d!",
948 BrowseTimeout);
949
950 if (BrowseInterval)
951 BrowseTimeout = BrowseInterval * 2;
952 else
953 BrowseTimeout = DEFAULT_TIMEOUT;
954
955 cupsdLogMessage(CUPSD_LOG_ALERT, "Reset BrowseTimeout to %d!",
956 BrowseTimeout);
957 }
958
959 /*
960 * Update the default policy, as needed...
961 */
962
963 if (DefaultPolicy)
964 DefaultPolicyPtr = cupsdFindPolicy(DefaultPolicy);
965 else
966 DefaultPolicyPtr = NULL;
967
968 if (!DefaultPolicyPtr)
969 {
970 cupsd_policy_t *p; /* New policy */
971 cupsd_location_t *po; /* New policy operation */
972
973
974 if (DefaultPolicy)
975 cupsdLogMessage(CUPSD_LOG_ERROR, "Default policy \"%s\" not found!",
976 DefaultPolicy);
977
978 cupsdSetString(&DefaultPolicy, "default");
979
980 if ((DefaultPolicyPtr = cupsdFindPolicy("default")) != NULL)
981 cupsdLogMessage(CUPSD_LOG_INFO,
982 "Using policy \"default\" as the default!");
983 else
984 {
985 cupsdLogMessage(CUPSD_LOG_INFO,
986 "Creating CUPS default administrative policy:");
987
988 DefaultPolicyPtr = p = cupsdAddPolicy("default");
989
990 cupsdLogMessage(CUPSD_LOG_INFO, "<Policy default>");
991 cupsdLogMessage(CUPSD_LOG_INFO,
992 "<Limit Send-Document Send-URI Cancel-Job Hold-Job "
993 "Release-Job Restart-Job Purge-Jobs "
994 "Set-Job-Attributes Create-Job-Subscription "
995 "Renew-Subscription Cancel-Subscription "
996 "Get-Notifications Reprocess-Job Cancel-Current-Job "
997 "Suspend-Current-Job Resume-Job CUPS-Move-Job "
998 "CUPS-Authenticate-Job>");
999 cupsdLogMessage(CUPSD_LOG_INFO, "Order Deny,Allow");
1000
1001 po = cupsdAddPolicyOp(p, NULL, IPP_SEND_DOCUMENT);
1002 po->order_type = AUTH_ALLOW;
1003 po->level = AUTH_USER;
1004
1005 cupsdAddName(po, "@OWNER");
1006 cupsdAddName(po, "@SYSTEM");
1007 cupsdLogMessage(CUPSD_LOG_INFO, "Require user @OWNER @SYSTEM");
1008
1009 cupsdAddPolicyOp(p, po, IPP_SEND_URI);
1010 cupsdAddPolicyOp(p, po, IPP_CANCEL_JOB);
1011 cupsdAddPolicyOp(p, po, IPP_HOLD_JOB);
1012 cupsdAddPolicyOp(p, po, IPP_RELEASE_JOB);
1013 cupsdAddPolicyOp(p, po, IPP_RESTART_JOB);
1014 cupsdAddPolicyOp(p, po, IPP_PURGE_JOBS);
1015 cupsdAddPolicyOp(p, po, IPP_SET_JOB_ATTRIBUTES);
1016 cupsdAddPolicyOp(p, po, IPP_CREATE_JOB_SUBSCRIPTION);
1017 cupsdAddPolicyOp(p, po, IPP_RENEW_SUBSCRIPTION);
1018 cupsdAddPolicyOp(p, po, IPP_CANCEL_SUBSCRIPTION);
1019 cupsdAddPolicyOp(p, po, IPP_GET_NOTIFICATIONS);
1020 cupsdAddPolicyOp(p, po, IPP_REPROCESS_JOB);
1021 cupsdAddPolicyOp(p, po, IPP_CANCEL_CURRENT_JOB);
1022 cupsdAddPolicyOp(p, po, IPP_SUSPEND_CURRENT_JOB);
1023 cupsdAddPolicyOp(p, po, IPP_RESUME_JOB);
1024 cupsdAddPolicyOp(p, po, CUPS_MOVE_JOB);
1025 cupsdAddPolicyOp(p, po, CUPS_AUTHENTICATE_JOB);
1026
1027 cupsdLogMessage(CUPSD_LOG_INFO, "</Limit>");
1028
1029 cupsdLogMessage(CUPSD_LOG_INFO,
1030 "<Limit Pause-Printer Resume-Printer "
1031 "Set-Printer-Attributes Enable-Printer "
1032 "Disable-Printer Pause-Printer-After-Current-Job "
1033 "Hold-New-Jobs Release-Held-New-Jobs "
1034 "Deactivate-Printer Activate-Printer Restart-Printer "
1035 "Shutdown-Printer Startup-Printer Promote-Job "
1036 "Schedule-Job-After CUPS-Add-Printer "
1037 "CUPS-Delete-Printer CUPS-Add-Class CUPS-Delete-Class "
1038 "CUPS-Accept-Jobs CUPS-Reject-Jobs CUPS-Set-Default>");
1039 cupsdLogMessage(CUPSD_LOG_INFO, "Order Deny,Allow");
1040 cupsdLogMessage(CUPSD_LOG_INFO, "AuthType Default");
1041
1042 po = cupsdAddPolicyOp(p, NULL, IPP_PAUSE_PRINTER);
1043 po->order_type = AUTH_ALLOW;
1044 po->type = DefaultAuthType;
1045 po->level = AUTH_USER;
1046
1047 cupsdAddName(po, "@SYSTEM");
1048 cupsdLogMessage(CUPSD_LOG_INFO, "Require user @SYSTEM");
1049
1050 cupsdAddPolicyOp(p, po, IPP_RESUME_PRINTER);
1051 cupsdAddPolicyOp(p, po, IPP_SET_PRINTER_ATTRIBUTES);
1052 cupsdAddPolicyOp(p, po, IPP_ENABLE_PRINTER);
1053 cupsdAddPolicyOp(p, po, IPP_DISABLE_PRINTER);
1054 cupsdAddPolicyOp(p, po, IPP_PAUSE_PRINTER_AFTER_CURRENT_JOB);
1055 cupsdAddPolicyOp(p, po, IPP_HOLD_NEW_JOBS);
1056 cupsdAddPolicyOp(p, po, IPP_RELEASE_HELD_NEW_JOBS);
1057 cupsdAddPolicyOp(p, po, IPP_DEACTIVATE_PRINTER);
1058 cupsdAddPolicyOp(p, po, IPP_ACTIVATE_PRINTER);
1059 cupsdAddPolicyOp(p, po, IPP_RESTART_PRINTER);
1060 cupsdAddPolicyOp(p, po, IPP_SHUTDOWN_PRINTER);
1061 cupsdAddPolicyOp(p, po, IPP_STARTUP_PRINTER);
1062 cupsdAddPolicyOp(p, po, IPP_PROMOTE_JOB);
1063 cupsdAddPolicyOp(p, po, IPP_SCHEDULE_JOB_AFTER);
1064 cupsdAddPolicyOp(p, po, CUPS_ADD_PRINTER);
1065 cupsdAddPolicyOp(p, po, CUPS_DELETE_PRINTER);
1066 cupsdAddPolicyOp(p, po, CUPS_ADD_CLASS);
1067 cupsdAddPolicyOp(p, po, CUPS_DELETE_CLASS);
1068 cupsdAddPolicyOp(p, po, CUPS_ACCEPT_JOBS);
1069 cupsdAddPolicyOp(p, po, CUPS_REJECT_JOBS);
1070 cupsdAddPolicyOp(p, po, CUPS_SET_DEFAULT);
1071
1072 cupsdLogMessage(CUPSD_LOG_INFO, "</Limit>");
1073
1074 cupsdLogMessage(CUPSD_LOG_INFO, "<Limit All>");
1075 cupsdLogMessage(CUPSD_LOG_INFO, "Order Deny,Allow");
1076
1077 po = cupsdAddPolicyOp(p, NULL, IPP_ANY_OPERATION);
1078 po->order_type = AUTH_ALLOW;
1079
1080 cupsdLogMessage(CUPSD_LOG_INFO, "</Limit>");
1081 cupsdLogMessage(CUPSD_LOG_INFO, "</Policy>");
1082 }
1083 }
1084
1085 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdReadConfiguration: NumPolicies=%d",
1086 NumPolicies);
1087 for (i = 0; i < NumPolicies; i ++)
1088 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1089 "cupsdReadConfiguration: Policies[%d]=\"%s\"", i,
1090 Policies[i]->name);
1091
1092 /*
1093 * If we are doing a full reload or the server root has changed, flush
1094 * the jobs, printers, etc. and start from scratch...
1095 */
1096
1097 if (NeedReload == RELOAD_ALL ||
1098 !old_serverroot || !ServerRoot || strcmp(old_serverroot, ServerRoot) ||
1099 !old_requestroot || !RequestRoot || strcmp(old_requestroot, RequestRoot))
1100 {
1101 mime_type_t *type; /* Current type */
1102 char mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE];
1103 /* MIME type name */
1104
1105
1106 cupsdLogMessage(CUPSD_LOG_INFO, "Full reload is required.");
1107
1108 /*
1109 * Free all memory...
1110 */
1111
1112 cupsdDeleteAllSubscriptions();
1113 cupsdFreeAllJobs();
1114 cupsdDeleteAllClasses();
1115 cupsdDeleteAllPrinters();
1116
1117 DefaultPrinter = NULL;
1118
1119 if (MimeDatabase != NULL)
1120 mimeDelete(MimeDatabase);
1121
1122 if (NumMimeTypes)
1123 {
1124 for (i = 0; i < NumMimeTypes; i ++)
1125 _cupsStrFree(MimeTypes[i]);
1126
1127 free(MimeTypes);
1128 }
1129
1130 /*
1131 * Read the MIME type and conversion database...
1132 */
1133
1134 snprintf(temp, sizeof(temp), "%s/filter", ServerBin);
1135
1136 MimeDatabase = mimeLoad(ServerRoot, temp);
1137
1138 if (!MimeDatabase)
1139 {
1140 cupsdLogMessage(CUPSD_LOG_EMERG,
1141 "Unable to load MIME database from \'%s\'!", ServerRoot);
1142 exit(errno);
1143 }
1144
1145 cupsdLogMessage(CUPSD_LOG_INFO,
1146 "Loaded MIME database from \'%s\': %d types, %d filters...",
1147 ServerRoot, mimeNumTypes(MimeDatabase),
1148 mimeNumFilters(MimeDatabase));
1149
1150 /*
1151 * Create a list of MIME types for the document-format-supported
1152 * attribute...
1153 */
1154
1155 NumMimeTypes = mimeNumTypes(MimeDatabase);
1156 if (!mimeType(MimeDatabase, "application", "octet-stream"))
1157 NumMimeTypes ++;
1158
1159 MimeTypes = calloc(NumMimeTypes, sizeof(const char *));
1160
1161 for (i = 0, type = mimeFirstType(MimeDatabase);
1162 type;
1163 i ++, type = mimeNextType(MimeDatabase))
1164 {
1165 snprintf(mimetype, sizeof(mimetype), "%s/%s", type->super, type->type);
1166
1167 MimeTypes[i] = _cupsStrAlloc(mimetype);
1168 }
1169
1170 if (i < NumMimeTypes)
1171 MimeTypes[i] = _cupsStrAlloc("application/octet-stream");
1172
1173 if (LogLevel == CUPSD_LOG_DEBUG2)
1174 {
1175 mime_filter_t *filter; /* Current filter */
1176
1177
1178 for (type = mimeFirstType(MimeDatabase);
1179 type;
1180 type = mimeNextType(MimeDatabase))
1181 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdReadConfiguration: type %s/%s",
1182 type->super, type->type);
1183
1184 for (filter = mimeFirstFilter(MimeDatabase);
1185 filter;
1186 filter = mimeNextFilter(MimeDatabase))
1187 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1188 "cupsdReadConfiguration: filter %s/%s to %s/%s %d %s",
1189 filter->src->super, filter->src->type,
1190 filter->dst->super, filter->dst->type,
1191 filter->cost, filter->filter);
1192 }
1193
1194 /*
1195 * Load banners...
1196 */
1197
1198 snprintf(temp, sizeof(temp), "%s/banners", DataDir);
1199 cupsdLoadBanners(temp);
1200
1201 /*
1202 * Load printers and classes...
1203 */
1204
1205 cupsdLoadAllPrinters();
1206 cupsdLoadAllClasses();
1207 cupsdLoadRemoteCache();
1208 cupsdWritePrintcap();
1209
1210 cupsdCreateCommonData();
1211
1212 /*
1213 * Load queued jobs...
1214 */
1215
1216 cupsdLoadAllJobs();
1217
1218 /*
1219 * Load subscriptions...
1220 */
1221
1222 cupsdLoadAllSubscriptions();
1223
1224 cupsdLogMessage(CUPSD_LOG_INFO, "Full reload complete.");
1225 }
1226 else
1227 {
1228 /*
1229 * Not a full reload, so recreate the common printer attributes...
1230 */
1231
1232 cupsdCreateCommonData();
1233
1234 /*
1235 * Update all printers as needed...
1236 */
1237
1238 cupsdUpdatePrinters();
1239 cupsdWritePrintcap();
1240
1241 cupsdLogMessage(CUPSD_LOG_INFO, "Partial reload complete.");
1242 }
1243
1244 /*
1245 * Reset the reload state...
1246 */
1247
1248 NeedReload = RELOAD_NONE;
1249
1250 cupsdClearString(&old_serverroot);
1251 cupsdClearString(&old_requestroot);
1252
1253 return (1);
1254 }
1255
1256
1257 /*
1258 * 'get_address()' - Get an address + port number from a line.
1259 */
1260
1261 static http_addrlist_t * /* O - Pointer to list if address good, NULL if bad */
1262 get_address(const char *value, /* I - Value string */
1263 int defport) /* I - Default port */
1264 {
1265 char buffer[1024], /* Hostname + port number buffer */
1266 defpname[255], /* Default port name */
1267 *hostname, /* Hostname or IP */
1268 *portname; /* Port number or name */
1269 http_addrlist_t *addrlist; /* Address list */
1270
1271
1272 /*
1273 * Check for an empty value...
1274 */
1275
1276 if (!*value)
1277 {
1278 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad (empty) address!");
1279 return (NULL);
1280 }
1281
1282 /*
1283 * Grab a hostname and port number; if there is no colon and the port name
1284 * is only digits, then we have a port number by itself...
1285 */
1286
1287 strlcpy(buffer, value, sizeof(buffer));
1288
1289 if ((portname = strrchr(buffer, ':')) != NULL && !strchr(portname, ']'))
1290 {
1291 *portname++ = '\0';
1292 hostname = buffer;
1293 }
1294 else
1295 {
1296 for (portname = buffer; isdigit(*portname & 255); portname ++);
1297
1298 if (*portname)
1299 {
1300 /*
1301 * Use the default port...
1302 */
1303
1304 sprintf(defpname, "%d", defport);
1305 portname = defpname;
1306 hostname = buffer;
1307 }
1308 else
1309 {
1310 /*
1311 * The buffer contains just a port number...
1312 */
1313
1314 portname = buffer;
1315 hostname = NULL;
1316 }
1317 }
1318
1319 if (hostname && !strcmp(hostname, "*"))
1320 hostname = NULL;
1321
1322 /*
1323 * Now lookup the address using httpAddrGetList()...
1324 */
1325
1326 if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL)
1327 cupsdLogMessage(CUPSD_LOG_ERROR, "Hostname lookup for \"%s\" failed!",
1328 hostname ? hostname : "(nil)");
1329
1330 return (addrlist);
1331 }
1332
1333
1334 /*
1335 * 'get_addr_and_mask()' - Get an IP address and netmask.
1336 */
1337
1338 static int /* O - 1 on success, 0 on failure */
1339 get_addr_and_mask(const char *value, /* I - String from config file */
1340 unsigned *ip, /* O - Address value */
1341 unsigned *mask) /* O - Mask value */
1342 {
1343 int i, j, /* Looping vars */
1344 family, /* Address family */
1345 ipcount; /* Count of fields in address */
1346 unsigned ipval; /* Value */
1347 const char *maskval, /* Pointer to start of mask value */
1348 *ptr, /* Pointer into value */
1349 *ptr2; /* ... */
1350
1351
1352 /*
1353 * Get the address...
1354 */
1355
1356 ip[0] = ip[1] = ip[2] = ip[3] = 0x00000000;
1357 mask[0] = mask[1] = mask[2] = mask[3] = 0xffffffff;
1358
1359 if ((maskval = strchr(value, '/')) != NULL)
1360 maskval ++;
1361 else
1362 maskval = value + strlen(value);
1363
1364 #ifdef AF_INET6
1365 /*
1366 * Check for an IPv6 address...
1367 */
1368
1369 if (*value == '[')
1370 {
1371 /*
1372 * Parse hexadecimal IPv6 address...
1373 */
1374
1375 family = AF_INET6;
1376
1377 for (i = 0, ptr = value + 1; *ptr && i < 8; i ++)
1378 {
1379 if (*ptr == ']')
1380 break;
1381 else if (!strncmp(ptr, "::", 2))
1382 {
1383 for (ptr2 = strchr(ptr + 2, ':'), j = 0;
1384 ptr2;
1385 ptr2 = strchr(ptr2 + 1, ':'), j ++);
1386
1387 i = 7 - j;
1388 ptr ++;
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 ip[i / 2] |= ipval;
1399 else
1400 ip[i / 2] |= ipval << 16;
1401 }
1402 else
1403 return (0);
1404
1405 while (*ptr == ':')
1406 ptr ++;
1407 }
1408
1409 if (*ptr != ']')
1410 return (0);
1411
1412 ptr ++;
1413
1414 if (*ptr && *ptr != '/')
1415 return (0);
1416 }
1417 else
1418 #endif /* AF_INET6 */
1419 {
1420 /*
1421 * Parse dotted-decimal IPv4 address...
1422 */
1423
1424 unsigned val[4]; /* IPv4 address values */
1425
1426
1427 family = AF_INET;
1428 ipcount = sscanf(value, "%u.%u.%u.%u", val + 0, val + 1, val + 2, val + 3);
1429
1430 /*
1431 * Range check the IP numbers...
1432 */
1433
1434 for (i = 0; i < ipcount; i ++)
1435 if (val[i] > 255)
1436 return (0);
1437
1438 /*
1439 * Make sure the trailing values are zeroed, as some C libraries like
1440 * glibc apparently like to fill the unused arguments with garbage...
1441 */
1442
1443 for (i = ipcount; i < 4; i ++)
1444 val[i] = 0;
1445
1446 /*
1447 * Merge everything into a 32-bit IPv4 address in ip[3]...
1448 */
1449
1450 ip[3] = (((((val[0] << 8) | val[1]) << 8) | val[2]) << 8) | val[3];
1451
1452 if (ipcount < 4)
1453 mask[3] = (0xffffffff << (32 - 8 * ipcount)) & 0xffffffff;
1454 }
1455
1456 if (*maskval)
1457 {
1458 /*
1459 * Get the netmask value(s)...
1460 */
1461
1462 memset(mask, 0, sizeof(unsigned) * 4);
1463
1464 if (strchr(maskval, '.'))
1465 {
1466 /*
1467 * Get dotted-decimal mask...
1468 */
1469
1470 if (family != AF_INET)
1471 return (0);
1472
1473 if (sscanf(maskval, "%u.%u.%u.%u", mask + 0, mask + 1, mask + 2, mask + 3) != 4)
1474 return (0);
1475
1476 mask[3] |= ((((mask[0] << 8) | mask[1]) << 8) | mask[2]) << 8;
1477 mask[0] = mask[1] = mask[2] = 0;
1478 }
1479 else
1480 {
1481 /*
1482 * Get address/bits format...
1483 */
1484
1485 i = atoi(maskval);
1486
1487 #ifdef AF_INET6
1488 if (family == AF_INET6)
1489 {
1490 if (i > 128)
1491 return (0);
1492
1493 i = 128 - i;
1494
1495 if (i <= 96)
1496 mask[0] = 0xffffffff;
1497 else
1498 mask[0] = (0xffffffff << (i - 96)) & 0xffffffff;
1499
1500 if (i <= 64)
1501 mask[1] = 0xffffffff;
1502 else if (i >= 96)
1503 mask[1] = 0;
1504 else
1505 mask[1] = (0xffffffff << (i - 64)) & 0xffffffff;
1506
1507 if (i <= 32)
1508 mask[2] = 0xffffffff;
1509 else if (i >= 64)
1510 mask[2] = 0;
1511 else
1512 mask[2] = (0xffffffff << (i - 32)) & 0xffffffff;
1513
1514 if (i == 0)
1515 mask[3] = 0xffffffff;
1516 else if (i >= 32)
1517 mask[3] = 0;
1518 else
1519 mask[3] = (0xffffffff << i) & 0xffffffff;
1520 }
1521 else
1522 #endif /* AF_INET6 */
1523 {
1524 if (i > 32)
1525 return (0);
1526
1527 mask[0] = 0xffffffff;
1528 mask[1] = 0xffffffff;
1529 mask[2] = 0xffffffff;
1530
1531 if (i < 32)
1532 mask[3] = (0xffffffff << (32 - i)) & 0xffffffff;
1533 else
1534 mask[3] = 0xffffffff;
1535 }
1536 }
1537 }
1538
1539 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1540 "get_addr_and_mask(value=\"%s\", "
1541 "ip=[%08x:%08x:%08x:%08x], mask=[%08x:%08x:%08x:%08x])",
1542 value, ip[0], ip[1], ip[2], ip[3], mask[0], mask[1], mask[2],
1543 mask[3]);
1544
1545 /*
1546 * Check for a valid netmask; no fallback like in CUPS 1.1.x!
1547 */
1548
1549 if ((ip[0] & ~mask[0]) != 0 ||
1550 (ip[1] & ~mask[1]) != 0 ||
1551 (ip[2] & ~mask[2]) != 0 ||
1552 (ip[3] & ~mask[3]) != 0)
1553 return (0);
1554
1555 return (1);
1556 }
1557
1558
1559 /*
1560 * 'parse_aaa()' - Parse authentication, authorization, and access control lines.
1561 */
1562
1563 static int /* O - 1 on success, 0 on failure */
1564 parse_aaa(cupsd_location_t *loc, /* I - Location */
1565 char *line, /* I - Line from file */
1566 char *value, /* I - Start of value data */
1567 int linenum) /* I - Current line number */
1568 {
1569 char *valptr; /* Pointer into value */
1570 unsigned ip[4], /* IP address components */
1571 mask[4]; /* IP netmask components */
1572
1573
1574 if (!strcasecmp(line, "Encryption"))
1575 {
1576 /*
1577 * "Encryption xxx" - set required encryption level...
1578 */
1579
1580 if (!strcasecmp(value, "never"))
1581 loc->encryption = HTTP_ENCRYPT_NEVER;
1582 else if (!strcasecmp(value, "always"))
1583 {
1584 cupsdLogMessage(CUPSD_LOG_ERROR,
1585 "Encryption value \"%s\" on line %d is invalid in this "
1586 "context. Using \"required\" instead.", value, linenum);
1587
1588 loc->encryption = HTTP_ENCRYPT_REQUIRED;
1589 }
1590 else if (!strcasecmp(value, "required"))
1591 loc->encryption = HTTP_ENCRYPT_REQUIRED;
1592 else if (!strcasecmp(value, "ifrequested"))
1593 loc->encryption = HTTP_ENCRYPT_IF_REQUESTED;
1594 else
1595 {
1596 cupsdLogMessage(CUPSD_LOG_ERROR,
1597 "Unknown Encryption value %s on line %d.", value, linenum);
1598 return (0);
1599 }
1600 }
1601 else if (!strcasecmp(line, "Order"))
1602 {
1603 /*
1604 * "Order Deny,Allow" or "Order Allow,Deny"...
1605 */
1606
1607 if (!strncasecmp(value, "deny", 4))
1608 loc->order_type = AUTH_ALLOW;
1609 else if (!strncasecmp(value, "allow", 5))
1610 loc->order_type = AUTH_DENY;
1611 else
1612 {
1613 cupsdLogMessage(CUPSD_LOG_ERROR, "Unknown Order value %s on line %d.",
1614 value, linenum);
1615 return (0);
1616 }
1617 }
1618 else if (!strcasecmp(line, "Allow") || !strcasecmp(line, "Deny"))
1619 {
1620 /*
1621 * Allow [From] host/ip...
1622 * Deny [From] host/ip...
1623 */
1624
1625 if (!strncasecmp(value, "from", 4))
1626 {
1627 /*
1628 * Strip leading "from"...
1629 */
1630
1631 value += 4;
1632
1633 while (isspace(*value & 255))
1634 value ++;
1635 }
1636
1637 /*
1638 * Figure out what form the allow/deny address takes:
1639 *
1640 * All
1641 * None
1642 * *.domain.com
1643 * .domain.com
1644 * host.domain.com
1645 * nnn.*
1646 * nnn.nnn.*
1647 * nnn.nnn.nnn.*
1648 * nnn.nnn.nnn.nnn
1649 * nnn.nnn.nnn.nnn/mm
1650 * nnn.nnn.nnn.nnn/mmm.mmm.mmm.mmm
1651 */
1652
1653 if (!strcasecmp(value, "all"))
1654 {
1655 /*
1656 * All hosts...
1657 */
1658
1659 if (!strcasecmp(line, "Allow"))
1660 cupsdAllowIP(loc, zeros, zeros);
1661 else
1662 cupsdDenyIP(loc, zeros, zeros);
1663 }
1664 else if (!strcasecmp(value, "none"))
1665 {
1666 /*
1667 * No hosts...
1668 */
1669
1670 if (!strcasecmp(line, "Allow"))
1671 cupsdAllowIP(loc, ones, zeros);
1672 else
1673 cupsdDenyIP(loc, ones, zeros);
1674 }
1675 #ifdef AF_INET6
1676 else if (value[0] == '*' || value[0] == '.' ||
1677 (!isdigit(value[0] & 255) && value[0] != '['))
1678 #else
1679 else if (value[0] == '*' || value[0] == '.' || !isdigit(value[0] & 255))
1680 #endif /* AF_INET6 */
1681 {
1682 /*
1683 * Host or domain name...
1684 */
1685
1686 if (value[0] == '*')
1687 value ++;
1688
1689 if (!strcasecmp(line, "Allow"))
1690 cupsdAllowHost(loc, value);
1691 else
1692 cupsdDenyHost(loc, value);
1693 }
1694 else
1695 {
1696 /*
1697 * One of many IP address forms...
1698 */
1699
1700 if (!get_addr_and_mask(value, ip, mask))
1701 {
1702 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad netmask value %s on line %d.",
1703 value, linenum);
1704 return (0);
1705 }
1706
1707 if (!strcasecmp(line, "Allow"))
1708 cupsdAllowIP(loc, ip, mask);
1709 else
1710 cupsdDenyIP(loc, ip, mask);
1711 }
1712 }
1713 else if (!strcasecmp(line, "AuthType"))
1714 {
1715 /*
1716 * AuthType {none,basic,digest,basicdigest,negotiate,default}
1717 */
1718
1719 if (!strcasecmp(value, "none"))
1720 {
1721 loc->type = AUTH_NONE;
1722 loc->level = AUTH_ANON;
1723 }
1724 else if (!strcasecmp(value, "basic"))
1725 {
1726 loc->type = AUTH_BASIC;
1727
1728 if (loc->level == AUTH_ANON)
1729 loc->level = AUTH_USER;
1730 }
1731 else if (!strcasecmp(value, "digest"))
1732 {
1733 loc->type = AUTH_DIGEST;
1734
1735 if (loc->level == AUTH_ANON)
1736 loc->level = AUTH_USER;
1737 }
1738 else if (!strcasecmp(value, "basicdigest"))
1739 {
1740 loc->type = AUTH_BASICDIGEST;
1741
1742 if (loc->level == AUTH_ANON)
1743 loc->level = AUTH_USER;
1744 }
1745 else if (!strcasecmp(value, "default"))
1746 {
1747 loc->type = DefaultAuthType;
1748
1749 if (loc->level == AUTH_ANON)
1750 loc->level = AUTH_USER;
1751 }
1752 #ifdef HAVE_GSSAPI
1753 else if (!strcasecmp(value, "negotiate"))
1754 {
1755 loc->type = AUTH_NEGOTIATE;
1756
1757 if (loc->level == AUTH_ANON)
1758 loc->level = AUTH_USER;
1759 }
1760 #endif /* HAVE_GSSAPI */
1761 else
1762 {
1763 cupsdLogMessage(CUPSD_LOG_WARN,
1764 "Unknown authorization type %s on line %d.",
1765 value, linenum);
1766 return (0);
1767 }
1768 }
1769 else if (!strcasecmp(line, "AuthClass"))
1770 {
1771 /*
1772 * AuthClass anonymous, user, system, group
1773 */
1774
1775 if (!strcasecmp(value, "anonymous"))
1776 {
1777 loc->type = AUTH_NONE;
1778 loc->level = AUTH_ANON;
1779
1780 cupsdLogMessage(CUPSD_LOG_WARN,
1781 "\"AuthClass %s\" is deprecated; consider removing "
1782 "it from line %d.",
1783 value, linenum);
1784 }
1785 else if (!strcasecmp(value, "user"))
1786 {
1787 loc->level = AUTH_USER;
1788
1789 cupsdLogMessage(CUPSD_LOG_WARN,
1790 "\"AuthClass %s\" is deprecated; consider using "
1791 "\"Require valid-user\" on line %d.",
1792 value, linenum);
1793 }
1794 else if (!strcasecmp(value, "group"))
1795 {
1796 loc->level = AUTH_GROUP;
1797
1798 cupsdLogMessage(CUPSD_LOG_WARN,
1799 "\"AuthClass %s\" is deprecated; consider using "
1800 "\"Require user @groupname\" on line %d.",
1801 value, linenum);
1802 }
1803 else if (!strcasecmp(value, "system"))
1804 {
1805 loc->level = AUTH_GROUP;
1806
1807 cupsdAddName(loc, "@SYSTEM");
1808
1809 cupsdLogMessage(CUPSD_LOG_WARN,
1810 "\"AuthClass %s\" is deprecated; consider using "
1811 "\"Require user @SYSTEM\" on line %d.",
1812 value, linenum);
1813 }
1814 else
1815 {
1816 cupsdLogMessage(CUPSD_LOG_WARN,
1817 "Unknown authorization class %s on line %d.",
1818 value, linenum);
1819 return (0);
1820 }
1821 }
1822 else if (!strcasecmp(line, "AuthGroupName"))
1823 {
1824 cupsdAddName(loc, value);
1825
1826 cupsdLogMessage(CUPSD_LOG_WARN,
1827 "\"AuthGroupName %s\" directive is deprecated; consider "
1828 "using \"Require user @%s\" on line %d.",
1829 value, value, linenum);
1830 }
1831 else if (!strcasecmp(line, "Require"))
1832 {
1833 /*
1834 * Apache synonym for AuthClass and AuthGroupName...
1835 *
1836 * Get initial word:
1837 *
1838 * Require valid-user
1839 * Require group names
1840 * Require user names
1841 */
1842
1843 for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
1844
1845 if (*valptr)
1846 *valptr++ = '\0';
1847
1848 if (!strcasecmp(value, "valid-user") ||
1849 !strcasecmp(value, "user"))
1850 loc->level = AUTH_USER;
1851 else if (!strcasecmp(value, "group"))
1852 loc->level = AUTH_GROUP;
1853 else
1854 {
1855 cupsdLogMessage(CUPSD_LOG_WARN, "Unknown Require type %s on line %d.",
1856 value, linenum);
1857 return (0);
1858 }
1859
1860 /*
1861 * Get the list of names from the line...
1862 */
1863
1864 for (value = valptr; *value;)
1865 {
1866 while (isspace(*value & 255))
1867 value ++;
1868
1869 #ifdef HAVE_AUTHORIZATION_H
1870 if (!strncmp(value, "@AUTHKEY(", 9))
1871 {
1872 /*
1873 * Grab "@AUTHKEY(name)" value...
1874 */
1875
1876 for (valptr = value + 9; *valptr != ')' && *valptr; valptr ++);
1877
1878 if (*valptr)
1879 *valptr++ = '\0';
1880 }
1881 else
1882 #endif /* HAVE_AUTHORIZATION_H */
1883 if (*value == '\"' || *value == '\'')
1884 {
1885 /*
1886 * Grab quoted name...
1887 */
1888
1889 for (valptr = value + 1; *valptr != *value && *valptr; valptr ++);
1890
1891 value ++;
1892 }
1893 else
1894 {
1895 /*
1896 * Grab literal name.
1897 */
1898
1899 for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
1900 }
1901
1902 if (*valptr)
1903 *valptr++ = '\0';
1904
1905 cupsdAddName(loc, value);
1906
1907 for (value = valptr; isspace(*value & 255); value ++);
1908 }
1909 }
1910 else if (!strcasecmp(line, "Satisfy"))
1911 {
1912 if (!strcasecmp(value, "all"))
1913 loc->satisfy = AUTH_SATISFY_ALL;
1914 else if (!strcasecmp(value, "any"))
1915 loc->satisfy = AUTH_SATISFY_ANY;
1916 else
1917 {
1918 cupsdLogMessage(CUPSD_LOG_WARN, "Unknown Satisfy value %s on line %d.",
1919 value, linenum);
1920 return (0);
1921 }
1922 }
1923 else
1924 return (0);
1925
1926 return (1);
1927 }
1928
1929
1930 /*
1931 * 'parse_groups()' - Parse system group names in a string.
1932 */
1933
1934 static int /* O - 1 on success, 0 on failure */
1935 parse_groups(const char *s) /* I - Space-delimited groups */
1936 {
1937 int status; /* Return status */
1938 char value[1024], /* Value string */
1939 *valstart, /* Pointer into value */
1940 *valend, /* End of value */
1941 quote; /* Quote character */
1942 struct group *group; /* Group */
1943
1944
1945 /*
1946 * Make a copy of the string and parse out the groups...
1947 */
1948
1949 strlcpy(value, s, sizeof(value));
1950
1951 status = 1;
1952 valstart = value;
1953
1954 while (*valstart && NumSystemGroups < MAX_SYSTEM_GROUPS)
1955 {
1956 if (*valstart == '\'' || *valstart == '\"')
1957 {
1958 /*
1959 * Scan quoted name...
1960 */
1961
1962 quote = *valstart++;
1963
1964 for (valend = valstart; *valend; valend ++)
1965 if (*valend == quote)
1966 break;
1967 }
1968 else
1969 {
1970 /*
1971 * Scan space or comma-delimited name...
1972 */
1973
1974 for (valend = valstart; *valend; valend ++)
1975 if (isspace(*valend) || *valend == ',')
1976 break;
1977 }
1978
1979 if (*valend)
1980 *valend++ = '\0';
1981
1982 group = getgrnam(valstart);
1983 if (group)
1984 {
1985 cupsdSetString(SystemGroups + NumSystemGroups, valstart);
1986 SystemGroupIDs[NumSystemGroups] = group->gr_gid;
1987
1988 NumSystemGroups ++;
1989 }
1990 else
1991 status = 0;
1992
1993 endgrent();
1994
1995 valstart = valend;
1996
1997 while (*valstart == ',' || isspace(*valstart))
1998 valstart ++;
1999 }
2000
2001 return (status);
2002 }
2003
2004
2005 /*
2006 * 'parse_protocols()' - Parse browse protocols in a string.
2007 */
2008
2009 static int /* O - Browse protocol bits */
2010 parse_protocols(const char *s) /* I - Space-delimited protocols */
2011 {
2012 int protocols; /* Browse protocol bits */
2013 char value[1024], /* Value string */
2014 *valstart, /* Pointer into value */
2015 *valend; /* End of value */
2016
2017
2018 /*
2019 * Empty protocol line yields NULL pointer...
2020 */
2021
2022 if (!s)
2023 return (0);
2024
2025 /*
2026 * Loop through the value string,...
2027 */
2028
2029 strlcpy(value, s, sizeof(value));
2030
2031 protocols = 0;
2032
2033 for (valstart = value; *valstart;)
2034 {
2035 /*
2036 * Get the current space/comma-delimited protocol name...
2037 */
2038
2039 for (valend = valstart; *valend; valend ++)
2040 if (isspace(*valend & 255) || *valend == ',')
2041 break;
2042
2043 if (*valend)
2044 *valend++ = '\0';
2045
2046 /*
2047 * Add the protocol to the bitmask...
2048 */
2049
2050 if (!strcasecmp(valstart, "cups"))
2051 protocols |= BROWSE_CUPS;
2052 else if (!strcasecmp(valstart, "slp"))
2053 protocols |= BROWSE_SLP;
2054 else if (!strcasecmp(valstart, "ldap"))
2055 protocols |= BROWSE_LDAP;
2056 else if (!strcasecmp(valstart, "dnssd") || !strcasecmp(valstart, "bonjour"))
2057 protocols |= BROWSE_DNSSD;
2058 else if (!strcasecmp(valstart, "all"))
2059 protocols |= BROWSE_ALL;
2060 else if (strcasecmp(valstart, "none"))
2061 return (-1);
2062
2063 for (valstart = valend; *valstart; valstart ++)
2064 if (!isspace(*valstart & 255) || *valstart != ',')
2065 break;
2066 }
2067
2068 return (protocols);
2069 }
2070
2071
2072 /*
2073 * 'read_configuration()' - Read a configuration file.
2074 */
2075
2076 static int /* O - 1 on success, 0 on failure */
2077 read_configuration(cups_file_t *fp) /* I - File to read from */
2078 {
2079 int i; /* Looping var */
2080 int linenum; /* Current line number */
2081 char line[HTTP_MAX_BUFFER],
2082 /* Line from file */
2083 temp[HTTP_MAX_BUFFER],
2084 /* Temporary buffer for value */
2085 temp2[HTTP_MAX_BUFFER],
2086 /* Temporary buffer 2 for value */
2087 *ptr, /* Pointer into line/temp */
2088 *value, /* Pointer to value */
2089 *valueptr; /* Pointer into value */
2090 int valuelen; /* Length of value */
2091 cupsd_var_t *var; /* Current variable */
2092 http_addrlist_t *addrlist, /* Address list */
2093 *addr; /* Current address */
2094 unsigned ip[4], /* Address value */
2095 mask[4]; /* Netmask value */
2096 cupsd_dirsvc_relay_t *relay; /* Relay data */
2097 cupsd_dirsvc_poll_t *pollp; /* Polling data */
2098 cupsd_location_t *location; /* Browse location */
2099 cups_file_t *incfile; /* Include file */
2100 char incname[1024]; /* Include filename */
2101 struct group *group; /* Group */
2102
2103
2104 /*
2105 * Loop through each line in the file...
2106 */
2107
2108 linenum = 0;
2109
2110 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
2111 {
2112 /*
2113 * Decode the directive...
2114 */
2115
2116 if (!strcasecmp(line, "Include"))
2117 {
2118 /*
2119 * Include filename
2120 */
2121
2122 if (value[0] == '/')
2123 strlcpy(incname, value, sizeof(incname));
2124 else
2125 snprintf(incname, sizeof(incname), "%s/%s", ServerRoot, value);
2126
2127 if ((incfile = cupsFileOpen(incname, "rb")) == NULL)
2128 cupsdLogMessage(CUPSD_LOG_ERROR,
2129 "Unable to include config file \"%s\" - %s",
2130 incname, strerror(errno));
2131 else
2132 {
2133 read_configuration(incfile);
2134 cupsFileClose(incfile);
2135 }
2136 }
2137 else if (!strcasecmp(line, "<Location"))
2138 {
2139 /*
2140 * <Location path>
2141 */
2142
2143 if (value)
2144 {
2145 linenum = read_location(fp, value, linenum);
2146 if (linenum == 0)
2147 return (0);
2148 }
2149 else
2150 {
2151 cupsdLogMessage(CUPSD_LOG_ERROR, "Syntax error on line %d.",
2152 linenum);
2153 return (0);
2154 }
2155 }
2156 else if (!strcasecmp(line, "<Policy"))
2157 {
2158 /*
2159 * <Policy name>
2160 */
2161
2162 if (value)
2163 {
2164 linenum = read_policy(fp, value, linenum);
2165 if (linenum == 0)
2166 return (0);
2167 }
2168 else
2169 {
2170 cupsdLogMessage(CUPSD_LOG_ERROR, "Syntax error on line %d.", linenum);
2171 return (0);
2172 }
2173 }
2174 else if (!strcasecmp(line, "FaxRetryInterval"))
2175 {
2176 if (value)
2177 {
2178 JobRetryInterval = atoi(value);
2179 cupsdLogMessage(CUPSD_LOG_WARN,
2180 "FaxRetryInterval is deprecated; use "
2181 "JobRetryInterval on line %d.", linenum);
2182 }
2183 else
2184 {
2185 cupsdLogMessage(CUPSD_LOG_ERROR, "Syntax error on line %d.", linenum);
2186 return (0);
2187 }
2188 }
2189 else if (!strcasecmp(line, "FaxRetryLimit"))
2190 {
2191 if (value)
2192 {
2193 JobRetryLimit = atoi(value);
2194 cupsdLogMessage(CUPSD_LOG_WARN,
2195 "FaxRetryLimit is deprecated; use "
2196 "JobRetryLimit on line %d.", linenum);
2197 }
2198 else
2199 {
2200 cupsdLogMessage(CUPSD_LOG_ERROR, "Syntax error on line %d.", linenum);
2201 return (0);
2202 }
2203 }
2204 else if (!strcasecmp(line, "Port") || !strcasecmp(line, "Listen")
2205 #ifdef HAVE_SSL
2206 || !strcasecmp(line, "SSLPort") || !strcasecmp(line, "SSLListen")
2207 #endif /* HAVE_SSL */
2208 )
2209 {
2210 /*
2211 * Add listening address(es) to the list...
2212 */
2213
2214 cupsd_listener_t *lis; /* New listeners array */
2215
2216
2217 /*
2218 * Get the address list...
2219 */
2220
2221 addrlist = get_address(value, IPP_PORT);
2222
2223 if (!addrlist)
2224 {
2225 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad %s address %s at line %d.", line,
2226 value, linenum);
2227 continue;
2228 }
2229
2230 /*
2231 * Add each address...
2232 */
2233
2234 for (addr = addrlist; addr; addr = addr->next)
2235 {
2236 /*
2237 * Allocate another listener...
2238 */
2239
2240 if (!Listeners)
2241 Listeners = cupsArrayNew(NULL, NULL);
2242
2243 if (!Listeners)
2244 {
2245 cupsdLogMessage(CUPSD_LOG_ERROR,
2246 "Unable to allocate %s at line %d - %s.",
2247 line, linenum, strerror(errno));
2248 break;
2249 }
2250
2251 if ((lis = calloc(1, sizeof(cupsd_listener_t))) == NULL)
2252 {
2253 cupsdLogMessage(CUPSD_LOG_ERROR,
2254 "Unable to allocate %s at line %d - %s.",
2255 line, linenum, strerror(errno));
2256 break;
2257 }
2258
2259 cupsArrayAdd(Listeners, lis);
2260
2261 /*
2262 * Copy the current address and log it...
2263 */
2264
2265 memcpy(&(lis->address), &(addr->addr), sizeof(lis->address));
2266 lis->fd = -1;
2267
2268 #ifdef HAVE_SSL
2269 if (!strcasecmp(line, "SSLPort") || !strcasecmp(line, "SSLListen"))
2270 lis->encryption = HTTP_ENCRYPT_ALWAYS;
2271 #endif /* HAVE_SSL */
2272
2273 httpAddrString(&lis->address, temp, sizeof(temp));
2274
2275 #ifdef AF_INET6
2276 if (lis->address.addr.sa_family == AF_INET6)
2277 cupsdLogMessage(CUPSD_LOG_INFO, "Listening to %s:%d (IPv6)", temp,
2278 ntohs(lis->address.ipv6.sin6_port));
2279 else
2280 #endif /* AF_INET6 */
2281 #ifdef AF_LOCAL
2282 if (lis->address.addr.sa_family == AF_LOCAL)
2283 cupsdLogMessage(CUPSD_LOG_INFO, "Listening to %s (Domain)", temp);
2284 else
2285 #endif /* AF_LOCAL */
2286 cupsdLogMessage(CUPSD_LOG_INFO, "Listening to %s:%d (IPv4)", temp,
2287 ntohs(lis->address.ipv4.sin_port));
2288 }
2289
2290 /*
2291 * Free the list...
2292 */
2293
2294 httpAddrFreeList(addrlist);
2295 }
2296 else if (!strcasecmp(line, "BrowseAddress"))
2297 {
2298 /*
2299 * Add a browse address to the list...
2300 */
2301
2302 cupsd_dirsvc_addr_t *dira; /* New browse address array */
2303
2304
2305 if (NumBrowsers == 0)
2306 dira = malloc(sizeof(cupsd_dirsvc_addr_t));
2307 else
2308 dira = realloc(Browsers, (NumBrowsers + 1) * sizeof(cupsd_dirsvc_addr_t));
2309
2310 if (!dira)
2311 {
2312 cupsdLogMessage(CUPSD_LOG_ERROR,
2313 "Unable to allocate BrowseAddress at line %d - %s.",
2314 linenum, strerror(errno));
2315 continue;
2316 }
2317
2318 Browsers = dira;
2319 dira += NumBrowsers;
2320
2321 memset(dira, 0, sizeof(cupsd_dirsvc_addr_t));
2322
2323 if (!strcasecmp(value, "@LOCAL"))
2324 {
2325 /*
2326 * Send browse data to all local interfaces...
2327 */
2328
2329 strcpy(dira->iface, "*");
2330 NumBrowsers ++;
2331 }
2332 else if (!strncasecmp(value, "@IF(", 4))
2333 {
2334 /*
2335 * Send browse data to the named interface...
2336 */
2337
2338 strlcpy(dira->iface, value + 4, sizeof(Browsers[0].iface));
2339
2340 ptr = dira->iface + strlen(dira->iface) - 1;
2341 if (*ptr == ')')
2342 *ptr = '\0';
2343
2344 NumBrowsers ++;
2345 }
2346 else if ((addrlist = get_address(value, BrowsePort)) != NULL)
2347 {
2348 /*
2349 * Only IPv4 addresses are supported...
2350 */
2351
2352 for (addr = addrlist; addr; addr = addr->next)
2353 if (addr->addr.addr.sa_family == AF_INET)
2354 break;
2355
2356 if (addr)
2357 {
2358 memcpy(&(dira->to), &(addrlist->addr), sizeof(dira->to));
2359 httpAddrString(&(dira->to), temp, sizeof(temp));
2360
2361 cupsdLogMessage(CUPSD_LOG_INFO,
2362 "Sending browsing info to %s:%d (IPv4)",
2363 temp, ntohs(dira->to.ipv4.sin_port));
2364
2365 NumBrowsers ++;
2366 }
2367 else
2368 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad BrowseAddress %s at line %d.",
2369 value, linenum);
2370
2371 httpAddrFreeList(addrlist);
2372 }
2373 else
2374 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad BrowseAddress %s at line %d.",
2375 value, linenum);
2376 }
2377 else if (!strcasecmp(line, "BrowseOrder"))
2378 {
2379 /*
2380 * "BrowseOrder Deny,Allow" or "BrowseOrder Allow,Deny"...
2381 */
2382
2383 if ((location = cupsdFindLocation("CUPS_INTERNAL_BROWSE_ACL")) == NULL)
2384 location = cupsdAddLocation("CUPS_INTERNAL_BROWSE_ACL");
2385
2386 if (location == NULL)
2387 cupsdLogMessage(CUPSD_LOG_ERROR,
2388 "Unable to initialize browse access control list!");
2389 else if (!strncasecmp(value, "deny", 4))
2390 location->order_type = AUTH_ALLOW;
2391 else if (!strncasecmp(value, "allow", 5))
2392 location->order_type = AUTH_DENY;
2393 else
2394 cupsdLogMessage(CUPSD_LOG_ERROR,
2395 "Unknown BrowseOrder value %s on line %d.",
2396 value, linenum);
2397 }
2398 else if (!strcasecmp(line, "BrowseProtocols") ||
2399 !strcasecmp(line, "BrowseLocalProtocols") ||
2400 !strcasecmp(line, "BrowseRemoteProtocols"))
2401 {
2402 /*
2403 * "BrowseProtocols name [... name]"
2404 * "BrowseLocalProtocols name [... name]"
2405 * "BrowseRemoteProtocols name [... name]"
2406 */
2407
2408 int protocols = parse_protocols(value);
2409
2410 if (protocols < 0)
2411 {
2412 cupsdLogMessage(CUPSD_LOG_ERROR,
2413 "Unknown browse protocol \"%s\" on line %d.",
2414 value, linenum);
2415 break;
2416 }
2417
2418 if (strcasecmp(line, "BrowseLocalProtocols"))
2419 BrowseRemoteProtocols = protocols;
2420 if (strcasecmp(line, "BrowseRemoteProtocols"))
2421 BrowseLocalProtocols = protocols;
2422 }
2423 else if (!strcasecmp(line, "BrowseAllow") ||
2424 !strcasecmp(line, "BrowseDeny"))
2425 {
2426 /*
2427 * BrowseAllow [From] host/ip...
2428 * BrowseDeny [From] host/ip...
2429 */
2430
2431 if ((location = cupsdFindLocation("CUPS_INTERNAL_BROWSE_ACL")) == NULL)
2432 location = cupsdAddLocation("CUPS_INTERNAL_BROWSE_ACL");
2433
2434 if (location == NULL)
2435 cupsdLogMessage(CUPSD_LOG_ERROR,
2436 "Unable to initialize browse access control list!");
2437 else
2438 {
2439 if (!strncasecmp(value, "from ", 5))
2440 {
2441 /*
2442 * Strip leading "from"...
2443 */
2444
2445 value += 5;
2446
2447 while (isspace(*value))
2448 value ++;
2449 }
2450
2451 /*
2452 * Figure out what form the allow/deny address takes:
2453 *
2454 * All
2455 * None
2456 * *.domain.com
2457 * .domain.com
2458 * host.domain.com
2459 * nnn.*
2460 * nnn.nnn.*
2461 * nnn.nnn.nnn.*
2462 * nnn.nnn.nnn.nnn
2463 * nnn.nnn.nnn.nnn/mm
2464 * nnn.nnn.nnn.nnn/mmm.mmm.mmm.mmm
2465 */
2466
2467 if (!strcasecmp(value, "all"))
2468 {
2469 /*
2470 * All hosts...
2471 */
2472
2473 if (!strcasecmp(line, "BrowseAllow"))
2474 cupsdAllowIP(location, zeros, zeros);
2475 else
2476 cupsdDenyIP(location, zeros, zeros);
2477 }
2478 else if (!strcasecmp(value, "none"))
2479 {
2480 /*
2481 * No hosts...
2482 */
2483
2484 if (!strcasecmp(line, "BrowseAllow"))
2485 cupsdAllowIP(location, ones, zeros);
2486 else
2487 cupsdDenyIP(location, ones, zeros);
2488 }
2489 #ifdef AF_INET6
2490 else if (value[0] == '*' || value[0] == '.' ||
2491 (!isdigit(value[0] & 255) && value[0] != '['))
2492 #else
2493 else if (value[0] == '*' || value[0] == '.' || !isdigit(value[0] & 255))
2494 #endif /* AF_INET6 */
2495 {
2496 /*
2497 * Host or domain name...
2498 */
2499
2500 if (value[0] == '*')
2501 value ++;
2502
2503 if (!strcasecmp(line, "BrowseAllow"))
2504 cupsdAllowHost(location, value);
2505 else
2506 cupsdDenyHost(location, value);
2507 }
2508 else
2509 {
2510 /*
2511 * One of many IP address forms...
2512 */
2513
2514 if (!get_addr_and_mask(value, ip, mask))
2515 {
2516 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad netmask value %s on line %d.",
2517 value, linenum);
2518 break;
2519 }
2520
2521 if (!strcasecmp(line, "BrowseAllow"))
2522 cupsdAllowIP(location, ip, mask);
2523 else
2524 cupsdDenyIP(location, ip, mask);
2525 }
2526 }
2527 }
2528 else if (!strcasecmp(line, "BrowseRelay"))
2529 {
2530 /*
2531 * BrowseRelay [from] source [to] destination
2532 */
2533
2534 if (NumRelays == 0)
2535 relay = malloc(sizeof(cupsd_dirsvc_relay_t));
2536 else
2537 relay = realloc(Relays, (NumRelays + 1) * sizeof(cupsd_dirsvc_relay_t));
2538
2539 if (!relay)
2540 {
2541 cupsdLogMessage(CUPSD_LOG_ERROR,
2542 "Unable to allocate BrowseRelay at line %d - %s.",
2543 linenum, strerror(errno));
2544 continue;
2545 }
2546
2547 Relays = relay;
2548 relay += NumRelays;
2549
2550 memset(relay, 0, sizeof(cupsd_dirsvc_relay_t));
2551
2552 if (!strncasecmp(value, "from ", 5))
2553 {
2554 /*
2555 * Strip leading "from"...
2556 */
2557
2558 value += 5;
2559
2560 while (isspace(*value))
2561 value ++;
2562 }
2563
2564 /*
2565 * Figure out what form the from address takes:
2566 *
2567 * *.domain.com
2568 * .domain.com
2569 * host.domain.com
2570 * nnn.*
2571 * nnn.nnn.*
2572 * nnn.nnn.nnn.*
2573 * nnn.nnn.nnn.nnn
2574 * nnn.nnn.nnn.nnn/mm
2575 * nnn.nnn.nnn.nnn/mmm.mmm.mmm.mmm
2576 */
2577
2578 #ifdef AF_INET6
2579 if (value[0] == '*' || value[0] == '.' ||
2580 (!isdigit(value[0] & 255) && value[0] != '['))
2581 #else
2582 if (value[0] == '*' || value[0] == '.' || !isdigit(value[0] & 255))
2583 #endif /* AF_INET6 */
2584 {
2585 /*
2586 * Host or domain name...
2587 */
2588
2589 if (value[0] == '*')
2590 value ++;
2591
2592 strlcpy(temp, value, sizeof(temp));
2593 if ((ptr = strchr(temp, ' ')) != NULL)
2594 *ptr = '\0';
2595
2596 relay->from.type = AUTH_NAME;
2597 relay->from.mask.name.name = strdup(temp);
2598 relay->from.mask.name.length = strlen(temp);
2599 }
2600 else
2601 {
2602 /*
2603 * One of many IP address forms...
2604 */
2605
2606 if (!get_addr_and_mask(value, ip, mask))
2607 {
2608 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad netmask value %s on line %d.",
2609 value, linenum);
2610 break;
2611 }
2612
2613 relay->from.type = AUTH_IP;
2614 memcpy(relay->from.mask.ip.address, ip,
2615 sizeof(relay->from.mask.ip.address));
2616 memcpy(relay->from.mask.ip.netmask, mask,
2617 sizeof(relay->from.mask.ip.netmask));
2618 }
2619
2620 /*
2621 * Skip value and trailing whitespace...
2622 */
2623
2624 for (; *value; value ++)
2625 if (isspace(*value))
2626 break;
2627
2628 while (isspace(*value))
2629 value ++;
2630
2631 if (!strncasecmp(value, "to ", 3))
2632 {
2633 /*
2634 * Strip leading "to"...
2635 */
2636
2637 value += 3;
2638
2639 while (isspace(*value))
2640 value ++;
2641 }
2642
2643 /*
2644 * Get "to" address and port...
2645 */
2646
2647 if ((addrlist = get_address(value, BrowsePort)) != NULL)
2648 {
2649 /*
2650 * Only IPv4 addresses are supported...
2651 */
2652
2653 for (addr = addrlist; addr; addr = addr->next)
2654 if (addr->addr.addr.sa_family == AF_INET)
2655 break;
2656
2657 if (addr)
2658 {
2659 memcpy(&(relay->to), &(addrlist->addr), sizeof(relay->to));
2660
2661 httpAddrString(&(relay->to), temp, sizeof(temp));
2662
2663 if (relay->from.type == AUTH_IP)
2664 snprintf(temp2, sizeof(temp2), "%u.%u.%u.%u/%u.%u.%u.%u",
2665 relay->from.mask.ip.address[0] >> 24,
2666 (relay->from.mask.ip.address[0] >> 16) & 255,
2667 (relay->from.mask.ip.address[0] >> 8) & 255,
2668 relay->from.mask.ip.address[0] & 255,
2669 relay->from.mask.ip.netmask[0] >> 24,
2670 (relay->from.mask.ip.netmask[0] >> 16) & 255,
2671 (relay->from.mask.ip.netmask[0] >> 8) & 255,
2672 relay->from.mask.ip.netmask[0] & 255);
2673 else
2674 strlcpy(temp2, relay->from.mask.name.name, sizeof(temp2));
2675
2676 cupsdLogMessage(CUPSD_LOG_INFO, "Relaying from %s to %s:%d (IPv4)",
2677 temp2, temp, ntohs(relay->to.ipv4.sin_port));
2678
2679 NumRelays ++;
2680 }
2681 else
2682 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad relay address %s at line %d.",
2683 value, linenum);
2684
2685 httpAddrFreeList(addrlist);
2686 }
2687 else
2688 {
2689 if (relay->from.type == AUTH_NAME)
2690 free(relay->from.mask.name.name);
2691
2692 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad relay address %s at line %d.",
2693 value, linenum);
2694 }
2695 }
2696 else if (!strcasecmp(line, "BrowsePoll"))
2697 {
2698 /*
2699 * BrowsePoll address[:port]
2700 */
2701
2702 char *portname; /* Port name */
2703 int portnum; /* Port number */
2704 struct servent *service; /* Service */
2705
2706
2707 /*
2708 * Extract the port name from the address...
2709 */
2710
2711 if ((portname = strrchr(value, ':')) != NULL && !strchr(portname, ']'))
2712 {
2713 *portname++ = '\0';
2714
2715 if (isdigit(*portname & 255))
2716 portnum = atoi(portname);
2717 else if ((service = getservbyname(portname, NULL)) != NULL)
2718 portnum = ntohs(service->s_port);
2719 else
2720 {
2721 cupsdLogMessage(CUPSD_LOG_ERROR, "Lookup of service \"%s\" failed!",
2722 portname);
2723 continue;
2724 }
2725 }
2726 else
2727 portnum = ippPort();
2728
2729 /*
2730 * Add the poll entry...
2731 */
2732
2733 if (NumPolled == 0)
2734 pollp = malloc(sizeof(cupsd_dirsvc_poll_t));
2735 else
2736 pollp = realloc(Polled, (NumPolled + 1) * sizeof(cupsd_dirsvc_poll_t));
2737
2738 if (!pollp)
2739 {
2740 cupsdLogMessage(CUPSD_LOG_ERROR,
2741 "Unable to allocate BrowsePoll at line %d - %s.",
2742 linenum, strerror(errno));
2743 continue;
2744 }
2745
2746 Polled = pollp;
2747 pollp += NumPolled;
2748
2749 NumPolled ++;
2750 memset(pollp, 0, sizeof(cupsd_dirsvc_poll_t));
2751
2752 strlcpy(pollp->hostname, value, sizeof(pollp->hostname));
2753 pollp->port = portnum;
2754
2755 cupsdLogMessage(CUPSD_LOG_INFO, "Polling %s:%d", pollp->hostname,
2756 pollp->port);
2757 }
2758 else if (!strcasecmp(line, "DefaultAuthType"))
2759 {
2760 /*
2761 * DefaultAuthType {basic,digest,basicdigest,negotiate}
2762 */
2763
2764 if (!strcasecmp(value, "basic"))
2765 DefaultAuthType = AUTH_BASIC;
2766 else if (!strcasecmp(value, "digest"))
2767 DefaultAuthType = AUTH_DIGEST;
2768 else if (!strcasecmp(value, "basicdigest"))
2769 DefaultAuthType = AUTH_BASICDIGEST;
2770 #ifdef HAVE_GSSAPI
2771 else if (!strcasecmp(value, "negotiate"))
2772 DefaultAuthType = AUTH_NEGOTIATE;
2773 #endif /* HAVE_GSSAPI */
2774 else
2775 {
2776 cupsdLogMessage(CUPSD_LOG_WARN,
2777 "Unknown default authorization type %s on line %d.",
2778 value, linenum);
2779 return (0);
2780 }
2781 }
2782 #ifdef HAVE_SSL
2783 else if (!strcasecmp(line, "DefaultEncryption"))
2784 {
2785 /*
2786 * DefaultEncryption {Never,IfRequested,Required}
2787 */
2788
2789 if (!value || !strcasecmp(value, "never"))
2790 DefaultEncryption = HTTP_ENCRYPT_NEVER;
2791 else if (!strcasecmp(value, "required"))
2792 DefaultEncryption = HTTP_ENCRYPT_REQUIRED;
2793 else if (!strcasecmp(value, "ifrequested"))
2794 DefaultEncryption = HTTP_ENCRYPT_IF_REQUESTED;
2795 else
2796 {
2797 cupsdLogMessage(CUPSD_LOG_WARN,
2798 "Unknown default encryption %s on line %d.",
2799 value, linenum);
2800 return (0);
2801 }
2802 }
2803 #endif /* HAVE_SSL */
2804 #ifdef HAVE_GSSAPI
2805 else if (!strcasecmp(line, "Krb5Keytab"))
2806 {
2807 cupsdSetStringf(&Krb5Keytab, "KRB5_KTNAME=%s", value);
2808 putenv(Krb5Keytab);
2809
2810 # ifdef HAVE_GSSKRB5_REGISTER_ACCEPTOR_IDENTITY
2811 gsskrb5_register_acceptor_identity(value);
2812 # else
2813 cupsdSetEnv("KRB5_KTNAME", value);
2814 # endif /* HAVE_GSSKRB5_REGISTER_ACCEPTOR_IDENTITY */
2815 }
2816 #endif /* HAVE_GSSAPI */
2817 else if (!strcasecmp(line, "User"))
2818 {
2819 /*
2820 * User ID to run as...
2821 */
2822
2823 if (value && isdigit(value[0] & 255))
2824 {
2825 int uid = atoi(value);
2826
2827 if (!uid)
2828 cupsdLogMessage(CUPSD_LOG_ERROR,
2829 "Will not use User 0 as specified on line %d "
2830 "for security reasons. You must use a non-"
2831 "privileged account instead.",
2832 linenum);
2833 else
2834 User = atoi(value);
2835 }
2836 else if (value)
2837 {
2838 struct passwd *p; /* Password information */
2839
2840 endpwent();
2841 p = getpwnam(value);
2842
2843 if (p)
2844 {
2845 if (!p->pw_uid)
2846 cupsdLogMessage(CUPSD_LOG_ERROR,
2847 "Will not use User %s (UID=0) as specified on line "
2848 "%d for security reasons. You must use a non-"
2849 "privileged account instead.",
2850 value, linenum);
2851 else
2852 User = p->pw_uid;
2853 }
2854 else
2855 cupsdLogMessage(CUPSD_LOG_ERROR,
2856 "Unknown User \"%s\" on line %d, ignoring!",
2857 value, linenum);
2858 }
2859 else
2860 cupsdLogMessage(CUPSD_LOG_ERROR,
2861 "User directive on line %d missing the username!",
2862 linenum);
2863 }
2864 else if (!strcasecmp(line, "Group"))
2865 {
2866 /*
2867 * Group ID to run as...
2868 */
2869
2870 if (isdigit(value[0]))
2871 Group = atoi(value);
2872 else
2873 {
2874 endgrent();
2875 group = getgrnam(value);
2876
2877 if (group != NULL)
2878 Group = group->gr_gid;
2879 else
2880 cupsdLogMessage(CUPSD_LOG_ERROR,
2881 "Unknown Group \"%s\" on line %d, ignoring!",
2882 value, linenum);
2883 }
2884 }
2885 else if (!strcasecmp(line, "SystemGroup"))
2886 {
2887 /*
2888 * SystemGroup (admin) group(s)...
2889 */
2890
2891 if (!parse_groups(value))
2892 cupsdLogMessage(CUPSD_LOG_ERROR,
2893 "Unknown SystemGroup \"%s\" on line %d, ignoring!",
2894 value, linenum);
2895 }
2896 else if (!strcasecmp(line, "HostNameLookups"))
2897 {
2898 /*
2899 * Do hostname lookups?
2900 */
2901
2902 if (!strcasecmp(value, "off"))
2903 HostNameLookups = 0;
2904 else if (!strcasecmp(value, "on"))
2905 HostNameLookups = 1;
2906 else if (!strcasecmp(value, "double"))
2907 HostNameLookups = 2;
2908 else
2909 cupsdLogMessage(CUPSD_LOG_WARN, "Unknown HostNameLookups %s on line %d.",
2910 value, linenum);
2911 }
2912 else if (!strcasecmp(line, "LogLevel"))
2913 {
2914 /*
2915 * Amount of logging to do...
2916 */
2917
2918 if (!strcasecmp(value, "debug2"))
2919 LogLevel = CUPSD_LOG_DEBUG2;
2920 else if (!strcasecmp(value, "debug"))
2921 LogLevel = CUPSD_LOG_DEBUG;
2922 else if (!strcasecmp(value, "info"))
2923 LogLevel = CUPSD_LOG_INFO;
2924 else if (!strcasecmp(value, "notice"))
2925 LogLevel = CUPSD_LOG_NOTICE;
2926 else if (!strcasecmp(value, "warn"))
2927 LogLevel = CUPSD_LOG_WARN;
2928 else if (!strcasecmp(value, "error"))
2929 LogLevel = CUPSD_LOG_ERROR;
2930 else if (!strcasecmp(value, "crit"))
2931 LogLevel = CUPSD_LOG_CRIT;
2932 else if (!strcasecmp(value, "alert"))
2933 LogLevel = CUPSD_LOG_ALERT;
2934 else if (!strcasecmp(value, "emerg"))
2935 LogLevel = CUPSD_LOG_EMERG;
2936 else if (!strcasecmp(value, "none"))
2937 LogLevel = CUPSD_LOG_NONE;
2938 else
2939 cupsdLogMessage(CUPSD_LOG_WARN, "Unknown LogLevel %s on line %d.",
2940 value, linenum);
2941 }
2942 else if (!strcasecmp(line, "PrintcapFormat"))
2943 {
2944 /*
2945 * Format of printcap file?
2946 */
2947
2948 if (!strcasecmp(value, "bsd"))
2949 PrintcapFormat = PRINTCAP_BSD;
2950 else if (!strcasecmp(value, "solaris"))
2951 PrintcapFormat = PRINTCAP_SOLARIS;
2952 else
2953 cupsdLogMessage(CUPSD_LOG_WARN, "Unknown PrintcapFormat %s on line %d.",
2954 value, linenum);
2955 }
2956 else if (!strcasecmp(line, "ServerTokens"))
2957 {
2958 /*
2959 * Set the string used for the Server header...
2960 */
2961
2962 struct utsname plat; /* Platform info */
2963
2964
2965 uname(&plat);
2966
2967 if (!strcasecmp(value, "ProductOnly"))
2968 cupsdSetString(&ServerHeader, "CUPS");
2969 else if (!strcasecmp(value, "Major"))
2970 cupsdSetString(&ServerHeader, "CUPS/1");
2971 else if (!strcasecmp(value, "Minor"))
2972 cupsdSetString(&ServerHeader, "CUPS/1.2");
2973 else if (!strcasecmp(value, "Minimal"))
2974 cupsdSetString(&ServerHeader, CUPS_MINIMAL);
2975 else if (!strcasecmp(value, "OS"))
2976 cupsdSetStringf(&ServerHeader, CUPS_MINIMAL " (%s)", plat.sysname);
2977 else if (!strcasecmp(value, "Full"))
2978 cupsdSetStringf(&ServerHeader, CUPS_MINIMAL " (%s) IPP/1.1",
2979 plat.sysname);
2980 else if (!strcasecmp(value, "None"))
2981 cupsdClearString(&ServerHeader);
2982 else
2983 cupsdLogMessage(CUPSD_LOG_WARN, "Unknown ServerTokens %s on line %d.",
2984 value, linenum);
2985 }
2986 else if (!strcasecmp(line, "PassEnv"))
2987 {
2988 /*
2989 * PassEnv variable [... variable]
2990 */
2991
2992 for (; *value;)
2993 {
2994 for (valuelen = 0; value[valuelen]; valuelen ++)
2995 if (isspace(value[valuelen]) || value[valuelen] == ',')
2996 break;
2997
2998 if (value[valuelen])
2999 {
3000 value[valuelen] = '\0';
3001 valuelen ++;
3002 }
3003
3004 cupsdSetEnv(value, NULL);
3005
3006 for (value += valuelen; *value; value ++)
3007 if (!isspace(*value) || *value != ',')
3008 break;
3009 }
3010 }
3011 else if (!strcasecmp(line, "SetEnv"))
3012 {
3013 /*
3014 * SetEnv variable value
3015 */
3016
3017 for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++);
3018
3019 if (*valueptr)
3020 {
3021 /*
3022 * Found a value...
3023 */
3024
3025 while (isspace(*valueptr & 255))
3026 *valueptr++ = '\0';
3027
3028 cupsdSetEnv(value, valueptr);
3029 }
3030 else
3031 cupsdLogMessage(CUPSD_LOG_ERROR,
3032 "Missing value for SetEnv directive on line %d.",
3033 linenum);
3034 }
3035 else
3036 {
3037 /*
3038 * Find a simple variable in the list...
3039 */
3040
3041 for (i = NUM_VARS, var = variables; i > 0; i --, var ++)
3042 if (!strcasecmp(line, var->name))
3043 break;
3044
3045 if (i == 0)
3046 {
3047 /*
3048 * Unknown directive! Output an error message and continue...
3049 */
3050
3051 cupsdLogMessage(CUPSD_LOG_ERROR, "Unknown directive %s on line %d.",
3052 line, linenum);
3053 continue;
3054 }
3055
3056 switch (var->type)
3057 {
3058 case CUPSD_VARTYPE_INTEGER :
3059 if (!value)
3060 cupsdLogMessage(CUPSD_LOG_ERROR,
3061 "Missing integer value for %s on line %d!",
3062 line, linenum);
3063 else
3064 {
3065 int n; /* Number */
3066 char *units; /* Units */
3067
3068
3069 n = strtol(value, &units, 0);
3070
3071 if (units && *units)
3072 {
3073 if (tolower(units[0] & 255) == 'g')
3074 n *= 1024 * 1024 * 1024;
3075 else if (tolower(units[0] & 255) == 'm')
3076 n *= 1024 * 1024;
3077 else if (tolower(units[0] & 255) == 'k')
3078 n *= 1024;
3079 else if (tolower(units[0] & 255) == 't')
3080 n *= 262144;
3081 }
3082
3083 if (n < 0)
3084 cupsdLogMessage(CUPSD_LOG_ERROR,
3085 "Bad negative integer value for %s on line %d!",
3086 line, linenum);
3087 else
3088 *((int *)var->ptr) = n;
3089 }
3090 break;
3091
3092 case CUPSD_VARTYPE_BOOLEAN :
3093 if (!value)
3094 cupsdLogMessage(CUPSD_LOG_ERROR,
3095 "Missing boolean value for %s on line %d!",
3096 line, linenum);
3097 else if (!strcasecmp(value, "true") ||
3098 !strcasecmp(value, "on") ||
3099 !strcasecmp(value, "enabled") ||
3100 !strcasecmp(value, "yes") ||
3101 atoi(value) != 0)
3102 *((int *)var->ptr) = TRUE;
3103 else if (!strcasecmp(value, "false") ||
3104 !strcasecmp(value, "off") ||
3105 !strcasecmp(value, "disabled") ||
3106 !strcasecmp(value, "no") ||
3107 !strcasecmp(value, "0"))
3108 *((int *)var->ptr) = FALSE;
3109 else
3110 cupsdLogMessage(CUPSD_LOG_ERROR,
3111 "Unknown boolean value %s on line %d.",
3112 value, linenum);
3113 break;
3114
3115 case CUPSD_VARTYPE_STRING :
3116 cupsdSetString((char **)var->ptr, value);
3117 break;
3118 }
3119 }
3120 }
3121
3122 return (1);
3123 }
3124
3125
3126 /*
3127 * 'read_location()' - Read a <Location path> definition.
3128 */
3129
3130 static int /* O - New line number or 0 on error */
3131 read_location(cups_file_t *fp, /* I - Configuration file */
3132 char *location, /* I - Location name/path */
3133 int linenum) /* I - Current line number */
3134 {
3135 cupsd_location_t *loc, /* New location */
3136 *parent; /* Parent location */
3137 char line[HTTP_MAX_BUFFER],
3138 /* Line buffer */
3139 *value, /* Value for directive */
3140 *valptr; /* Pointer into value */
3141
3142
3143 if ((parent = cupsdAddLocation(location)) == NULL)
3144 return (0);
3145
3146 parent->limit = AUTH_LIMIT_ALL;
3147 loc = parent;
3148
3149 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
3150 {
3151 /*
3152 * Decode the directive...
3153 */
3154
3155 if (!strcasecmp(line, "</Location>"))
3156 return (linenum);
3157 else if (!strcasecmp(line, "<Limit") ||
3158 !strcasecmp(line, "<LimitExcept"))
3159 {
3160 if (!value)
3161 {
3162 cupsdLogMessage(CUPSD_LOG_ERROR, "Syntax error on line %d.", linenum);
3163 return (0);
3164 }
3165
3166 if ((loc = cupsdCopyLocation(&parent)) == NULL)
3167 return (0);
3168
3169 loc->limit = 0;
3170 while (*value)
3171 {
3172 for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
3173
3174 if (*valptr)
3175 *valptr++ = '\0';
3176
3177 if (!strcmp(value, "ALL"))
3178 loc->limit = AUTH_LIMIT_ALL;
3179 else if (!strcmp(value, "GET"))
3180 loc->limit |= AUTH_LIMIT_GET;
3181 else if (!strcmp(value, "HEAD"))
3182 loc->limit |= AUTH_LIMIT_HEAD;
3183 else if (!strcmp(value, "OPTIONS"))
3184 loc->limit |= AUTH_LIMIT_OPTIONS;
3185 else if (!strcmp(value, "POST"))
3186 loc->limit |= AUTH_LIMIT_POST;
3187 else if (!strcmp(value, "PUT"))
3188 loc->limit |= AUTH_LIMIT_PUT;
3189 else if (!strcmp(value, "TRACE"))
3190 loc->limit |= AUTH_LIMIT_TRACE;
3191 else
3192 cupsdLogMessage(CUPSD_LOG_WARN, "Unknown request type %s on line %d!",
3193 value, linenum);
3194
3195 for (value = valptr; isspace(*value & 255); value ++);
3196 }
3197
3198 if (!strcasecmp(line, "<LimitExcept"))
3199 loc->limit = AUTH_LIMIT_ALL ^ loc->limit;
3200
3201 parent->limit &= ~loc->limit;
3202 }
3203 else if (!strcasecmp(line, "</Limit>"))
3204 loc = parent;
3205 else if (!parse_aaa(loc, line, value, linenum))
3206 {
3207 cupsdLogMessage(CUPSD_LOG_ERROR,
3208 "Unknown Location directive %s on line %d.",
3209 line, linenum);
3210 return (0);
3211 }
3212 }
3213
3214 cupsdLogMessage(CUPSD_LOG_ERROR,
3215 "Unexpected end-of-file at line %d while reading location!",
3216 linenum);
3217
3218 return (0);
3219 }
3220
3221
3222 /*
3223 * 'read_policy()' - Read a <Policy name> definition.
3224 */
3225
3226 static int /* O - New line number or 0 on error */
3227 read_policy(cups_file_t *fp, /* I - Configuration file */
3228 char *policy, /* I - Location name/path */
3229 int linenum) /* I - Current line number */
3230 {
3231 int i; /* Looping var */
3232 cupsd_policy_t *pol; /* Policy */
3233 cupsd_location_t *op; /* Policy operation */
3234 int num_ops; /* Number of IPP operations */
3235 ipp_op_t ops[100]; /* Operations */
3236 char line[HTTP_MAX_BUFFER],
3237 /* Line buffer */
3238 *value, /* Value for directive */
3239 *valptr; /* Pointer into value */
3240
3241
3242 /*
3243 * Create the policy...
3244 */
3245
3246 if ((pol = cupsdAddPolicy(policy)) == NULL)
3247 return (0);
3248
3249 /*
3250 * Read from the file...
3251 */
3252
3253 op = NULL;
3254 num_ops = 0;
3255
3256 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
3257 {
3258 /*
3259 * Decode the directive...
3260 */
3261
3262 if (!strcasecmp(line, "</Policy>"))
3263 {
3264 if (op)
3265 cupsdLogMessage(CUPSD_LOG_WARN,
3266 "Missing </Limit> before </Policy> on line %d!",
3267 linenum);
3268
3269 return (linenum);
3270 }
3271 else if (!strcasecmp(line, "<Limit") && !op)
3272 {
3273 if (!value)
3274 {
3275 cupsdLogMessage(CUPSD_LOG_ERROR, "Syntax error on line %d.", linenum);
3276 return (0);
3277 }
3278
3279 /*
3280 * Scan for IPP operation names...
3281 */
3282
3283 num_ops = 0;
3284
3285 while (*value)
3286 {
3287 for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
3288
3289 if (*valptr)
3290 *valptr++ = '\0';
3291
3292 if (num_ops < (int)(sizeof(ops) / sizeof(ops[0])))
3293 {
3294 if (!strcasecmp(value, "All"))
3295 ops[num_ops] = IPP_ANY_OPERATION;
3296 else if ((ops[num_ops] = ippOpValue(value)) == IPP_BAD_OPERATION)
3297 cupsdLogMessage(CUPSD_LOG_ERROR,
3298 "Bad IPP operation name \"%s\" on line %d!",
3299 value, linenum);
3300 else
3301 num_ops ++;
3302 }
3303 else
3304 cupsdLogMessage(CUPSD_LOG_ERROR,
3305 "Too many operations listed on line %d!",
3306 linenum);
3307
3308 for (value = valptr; isspace(*value & 255); value ++);
3309 }
3310
3311 /*
3312 * If none are specified, apply the policy to all operations...
3313 */
3314
3315 if (num_ops == 0)
3316 {
3317 ops[0] = IPP_ANY_OPERATION;
3318 num_ops = 1;
3319 }
3320
3321 /*
3322 * Add a new policy for the first operation...
3323 */
3324
3325 op = cupsdAddPolicyOp(pol, NULL, ops[0]);
3326 }
3327 else if (!strcasecmp(line, "</Limit>") && op)
3328 {
3329 /*
3330 * Finish the current operation limit...
3331 */
3332
3333 if (num_ops > 1)
3334 {
3335 /*
3336 * Copy the policy to the other operations...
3337 */
3338
3339 for (i = 1; i < num_ops; i ++)
3340 cupsdAddPolicyOp(pol, op, ops[i]);
3341 }
3342
3343 op = NULL;
3344 }
3345 else if (!op)
3346 {
3347 cupsdLogMessage(CUPSD_LOG_ERROR,
3348 "Missing <Limit ops> directive before %s on line %d.",
3349 line, linenum);
3350 return (0);
3351 }
3352 else if (!parse_aaa(op, line, value, linenum))
3353 {
3354 if (op)
3355 cupsdLogMessage(CUPSD_LOG_ERROR,
3356 "Unknown Policy Limit directive %s on line %d.",
3357 line, linenum);
3358 else
3359 cupsdLogMessage(CUPSD_LOG_ERROR,
3360 "Unknown Policy directive %s on line %d.",
3361 line, linenum);
3362
3363 return (0);
3364 }
3365 }
3366
3367 cupsdLogMessage(CUPSD_LOG_ERROR,
3368 "Unexpected end-of-file at line %d while reading policy \"%s\"!",
3369 linenum, policy);
3370
3371 return (0);
3372 }
3373
3374
3375 /*
3376 * End of "$Id: conf.c 6649 2007-07-11 21:46:42Z mike $".
3377 */