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