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