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