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