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