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