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