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