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