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