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