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