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