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