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