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