]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/conf.c
Check for "All" operation in policy Limit separately.
[thirdparty/cups.git] / scheduler / conf.c
1 /*
2 * "$Id$"
3 *
4 * Configuration routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2005 by Easy Software Products, all rights reserved.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
26 * ReadConfiguration() - Read the cupsd.conf file.
27 * get_address() - Get an address + port number from a line.
28 * get_addr_and_mask() - Get an IP address and netmask.
29 * parse_aaa() - Parse authentication, authorization, and
30 * access control lines.
31 * read_configuration() - Read a configuration file.
32 * read_location() - Read a <Location path> definition.
33 * read_policy() - Read a <Policy name> definition.
34 * CDSAGetServerCerts() - Convert a keychain name into the CFArrayRef
35 * required by SSLSetCertificate.
36 */
37
38 /*
39 * Include necessary headers...
40 */
41
42 #include "cupsd.h"
43 #include <stdarg.h>
44 #include <grp.h>
45 #include <sys/utsname.h>
46 #include <cups/dir.h>
47
48 #ifdef HAVE_DOMAINSOCKETS
49 # include <sys/un.h>
50 #endif /* HAVE_DOMAINSOCKETS */
51
52 #ifdef HAVE_CDSASSL
53 # include <Security/SecureTransport.h>
54 # include <Security/SecIdentitySearch.h>
55 #endif /* HAVE_CDSASSL */
56
57 #ifdef HAVE_VSYSLOG
58 # include <syslog.h>
59 #endif /* HAVE_VSYSLOG */
60
61
62 /*
63 * Possibly missing network definitions...
64 */
65
66 #ifndef INADDR_NONE
67 # define INADDR_NONE 0xffffffff
68 #endif /* !INADDR_NONE */
69
70
71 /*
72 * Configuration variable structure...
73 */
74
75 typedef struct
76 {
77 char *name; /* Name of variable */
78 void *ptr; /* Pointer to variable */
79 int type; /* Type (int, string, address) */
80 } var_t;
81
82 #define VAR_INTEGER 0
83 #define VAR_STRING 1
84 #define VAR_BOOLEAN 2
85
86
87 /*
88 * Local globals...
89 */
90
91 static var_t variables[] =
92 {
93 { "AccessLog", &AccessLog, VAR_STRING },
94 { "AutoPurgeJobs", &JobAutoPurge, VAR_BOOLEAN },
95 { "BrowseInterval", &BrowseInterval, VAR_INTEGER },
96 { "BrowseLocalOptions", &BrowseLocalOptions, VAR_STRING },
97 { "BrowsePort", &BrowsePort, VAR_INTEGER },
98 { "BrowseRemoteOptions", &BrowseRemoteOptions, VAR_STRING },
99 { "BrowseShortNames", &BrowseShortNames, VAR_BOOLEAN },
100 { "BrowseTimeout", &BrowseTimeout, VAR_INTEGER },
101 { "Browsing", &Browsing, VAR_BOOLEAN },
102 { "CacheDir", &CacheDir, VAR_STRING },
103 { "Classification", &Classification, VAR_STRING },
104 { "ClassifyOverride", &ClassifyOverride, VAR_BOOLEAN },
105 { "ConfigFilePerm", &ConfigFilePerm, VAR_INTEGER },
106 { "DataDir", &DataDir, VAR_STRING },
107 { "DefaultCharset", &DefaultCharset, VAR_STRING },
108 { "DefaultLanguage", &DefaultLanguage, VAR_STRING },
109 { "DefaultPolicy", &DefaultPolicy, VAR_STRING },
110 { "DocumentRoot", &DocumentRoot, VAR_STRING },
111 { "ErrorLog", &ErrorLog, VAR_STRING },
112 { "FaxRetryLimit", &FaxRetryLimit, VAR_INTEGER },
113 { "FaxRetryInterval", &FaxRetryInterval, VAR_INTEGER },
114 { "FileDevice", &FileDevice, VAR_BOOLEAN },
115 { "FilterLimit", &FilterLimit, VAR_INTEGER },
116 { "FilterNice", &FilterNice, VAR_INTEGER },
117 { "FontPath", &FontPath, VAR_STRING },
118 { "HideImplicitMembers", &HideImplicitMembers, VAR_BOOLEAN },
119 { "ImplicitClasses", &ImplicitClasses, VAR_BOOLEAN },
120 { "ImplicitAnyClasses", &ImplicitAnyClasses, VAR_BOOLEAN },
121 { "KeepAliveTimeout", &KeepAliveTimeout, VAR_INTEGER },
122 { "KeepAlive", &KeepAlive, VAR_BOOLEAN },
123 { "LimitRequestBody", &MaxRequestSize, VAR_INTEGER },
124 { "ListenBackLog", &ListenBackLog, VAR_INTEGER },
125 { "LogFilePerm", &LogFilePerm, VAR_INTEGER },
126 { "MaxActiveJobs", &MaxActiveJobs, VAR_INTEGER },
127 { "MaxClients", &MaxClients, VAR_INTEGER },
128 { "MaxClientsPerHost", &MaxClientsPerHost, VAR_INTEGER },
129 { "MaxCopies", &MaxCopies, VAR_INTEGER },
130 { "MaxJobs", &MaxJobs, VAR_INTEGER },
131 { "MaxJobsPerPrinter", &MaxJobsPerPrinter, VAR_INTEGER },
132 { "MaxJobsPerUser", &MaxJobsPerUser, VAR_INTEGER },
133 { "MaxLogSize", &MaxLogSize, VAR_INTEGER },
134 { "MaxPrinterHistory", &MaxPrinterHistory, VAR_INTEGER },
135 { "MaxRequestSize", &MaxRequestSize, VAR_INTEGER },
136 { "PageLog", &PageLog, VAR_STRING },
137 { "PreserveJobFiles", &JobFiles, VAR_BOOLEAN },
138 { "PreserveJobHistory", &JobHistory, VAR_BOOLEAN },
139 { "Printcap", &Printcap, VAR_STRING },
140 { "PrintcapGUI", &PrintcapGUI, VAR_STRING },
141 { "ReloadTimeout", &ReloadTimeout, VAR_INTEGER },
142 { "RemoteRoot", &RemoteRoot, VAR_STRING },
143 { "RequestRoot", &RequestRoot, VAR_STRING },
144 { "RIPCache", &RIPCache, VAR_STRING },
145 { "RunAsUser", &RunAsUser, VAR_BOOLEAN },
146 { "RootCertDuration", &RootCertDuration, VAR_INTEGER },
147 { "ServerAdmin", &ServerAdmin, VAR_STRING },
148 { "ServerBin", &ServerBin, VAR_STRING },
149 #ifdef HAVE_SSL
150 { "ServerCertificate", &ServerCertificate, VAR_STRING },
151 # if defined(HAVE_LIBSSL) || defined(HAVE_GNUTLS)
152 { "ServerKey", &ServerKey, VAR_STRING },
153 # endif /* HAVE_LIBSSL || HAVE_GNUTLS */
154 #endif /* HAVE_SSL */
155 { "ServerName", &ServerName, VAR_STRING },
156 { "ServerRoot", &ServerRoot, VAR_STRING },
157 { "StateDir", &StateDir, VAR_STRING },
158 { "TempDir", &TempDir, VAR_STRING },
159 { "Timeout", &Timeout, VAR_INTEGER }
160 };
161 #define NUM_VARS (sizeof(variables) / sizeof(variables[0]))
162
163
164 static unsigned ones[4] =
165 {
166 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff
167 };
168 static unsigned zeros[4] =
169 {
170 0x00000000, 0x00000000, 0x00000000, 0x00000000
171 };
172
173 #ifdef HAVE_CDSASSL
174 CFArrayRef CDSAGetServerCerts();
175 #endif /* HAVE_CDSASSL */
176
177
178 /*
179 * Local functions...
180 */
181
182 static int get_address(const char *value, unsigned defaddress, int defport,
183 int deffamily, http_addr_t *address);
184 static int get_addr_and_mask(const char *value, unsigned *ip,
185 unsigned *mask);
186 static int parse_aaa(location_t *loc, char *line, char *value, int linenum);
187 static int read_configuration(cups_file_t *fp);
188 static int read_location(cups_file_t *fp, char *name, int linenum);
189 static int read_policy(cups_file_t *fp, char *name, int linenum);
190
191
192 /*
193 * 'ReadConfiguration()' - Read the cupsd.conf file.
194 */
195
196 int /* O - 1 on success, 0 otherwise */
197 ReadConfiguration(void)
198 {
199 int i; /* Looping var */
200 cups_file_t *fp; /* Configuration file */
201 int status; /* Return status */
202 char temp[1024], /* Temporary buffer */
203 *slash; /* Directory separator */
204 char type[MIME_MAX_SUPER + MIME_MAX_TYPE];
205 /* MIME type name */
206 char *language; /* Language string */
207 struct passwd *user; /* Default user */
208 struct group *group; /* Default group */
209 char *old_serverroot, /* Old ServerRoot */
210 *old_requestroot; /* Old RequestRoot */
211
212
213 /*
214 * Shutdown the server...
215 */
216
217 StopServer();
218
219 /*
220 * Save the old root paths...
221 */
222
223 old_serverroot = NULL;
224 SetString(&old_serverroot, ServerRoot);
225 old_requestroot = NULL;
226 SetString(&old_requestroot, RequestRoot);
227
228 /*
229 * Reset the server configuration data...
230 */
231
232 DeleteAllLocations();
233
234 if (NumBrowsers > 0)
235 {
236 free(Browsers);
237
238 NumBrowsers = 0;
239 }
240
241 if (NumPolled > 0)
242 {
243 free(Polled);
244
245 NumPolled = 0;
246 }
247
248 if (NumRelays > 0)
249 {
250 for (i = 0; i < NumRelays; i ++)
251 if (Relays[i].from.type == AUTH_NAME)
252 free(Relays[i].from.mask.name.name);
253
254 free(Relays);
255
256 NumRelays = 0;
257 }
258
259 if (NumListeners > 0)
260 {
261 #ifdef HAVE_DOMAINSOCKETS
262 int i; /* Looping var */
263 listener_t *lis; /* Current listening socket */
264
265 for (i = NumListeners, lis = Listeners; i > 0; i --, lis ++)
266 if (lis->address.sin_family == AF_LOCAL)
267 ClearString((char **)&lis->address.sin_addr);
268 #endif /* HAVE_DOMAINSOCKETS */
269
270 free(Listeners);
271
272 NumListeners = 0;
273 }
274
275 /*
276 * String options...
277 */
278
279 gethostname(temp, sizeof(temp));
280 SetString(&ServerName, temp);
281 SetStringf(&ServerAdmin, "root@%s", temp);
282 SetString(&ServerBin, CUPS_SERVERBIN);
283 SetString(&RequestRoot, CUPS_REQUESTS);
284 SetString(&CacheDir, CUPS_CACHEDIR);
285 SetString(&DataDir, CUPS_DATADIR);
286 SetString(&DocumentRoot, CUPS_DOCROOT);
287 SetString(&AccessLog, CUPS_LOGDIR "/access_log");
288 SetString(&ErrorLog, CUPS_LOGDIR "/error_log");
289 SetString(&PageLog, CUPS_LOGDIR "/page_log");
290 SetString(&Printcap, "/etc/printcap");
291 SetString(&PrintcapGUI, "/usr/bin/glpoptions");
292 SetString(&FontPath, CUPS_FONTPATH);
293 SetString(&RemoteRoot, "remroot");
294 SetString(&ServerHeader, "CUPS/1.1");
295 SetString(&StateDir, CUPS_STATEDIR);
296
297 strlcpy(temp, ConfigurationFile, sizeof(temp));
298 if ((slash = strrchr(temp, '/')) != NULL)
299 *slash = '\0';
300
301 SetString(&ServerRoot, temp);
302
303 ClearString(&Classification);
304 ClassifyOverride = 0;
305
306 #ifdef HAVE_SSL
307 # ifdef HAVE_CDSASSL
308 SetString(&ServerCertificate, "/var/root/Library/Keychains/CUPS");
309 # else
310 SetString(&ServerCertificate, "ssl/server.crt");
311 SetString(&ServerKey, "ssl/server.key");
312 # endif /* HAVE_CDSASSL */
313 #endif /* HAVE_SSL */
314
315 if ((language = DEFAULT_LANGUAGE) == NULL)
316 language = "en";
317 else if (strcmp(language, "C") == 0 || strcmp(language, "POSIX") == 0)
318 language = "en";
319
320 SetString(&DefaultLanguage, language);
321 SetString(&DefaultCharset, DEFAULT_CHARSET);
322
323 SetString(&RIPCache, "8m");
324
325 if (getenv("TMPDIR") == NULL)
326 SetString(&TempDir, CUPS_REQUESTS "/tmp");
327 else
328 SetString(&TempDir, getenv("TMPDIR"));
329
330 /*
331 * Find the default system group: "sys", "system", or "root"...
332 */
333
334 group = getgrnam(CUPS_DEFAULT_GROUP);
335 endgrent();
336
337 NumSystemGroups = 0;
338
339 if (group != NULL)
340 {
341 SetString(&SystemGroups[0], CUPS_DEFAULT_GROUP);
342 Group = group->gr_gid;
343 }
344 else
345 {
346 group = getgrgid(0);
347 endgrent();
348
349 if (group != NULL)
350 {
351 SetString(&SystemGroups[0], group->gr_name);
352 Group = 0;
353 }
354 else
355 {
356 SetString(&SystemGroups[0], "unknown");
357 Group = 0;
358 }
359 }
360
361 /*
362 * Find the default user...
363 */
364
365 if ((user = getpwnam(CUPS_DEFAULT_USER)) == NULL)
366 User = 1; /* Force to a non-priviledged account */
367 else
368 User = user->pw_uid;
369
370 endpwent();
371
372 /*
373 * Numeric options...
374 */
375
376 ConfigFilePerm = 0640;
377 LogFilePerm = 0644;
378
379 FaxRetryLimit = 5;
380 FaxRetryInterval = 300;
381 FileDevice = FALSE;
382 FilterLevel = 0;
383 FilterLimit = 0;
384 FilterNice = 0;
385 HostNameLookups = FALSE;
386 ImplicitClasses = TRUE;
387 ImplicitAnyClasses = FALSE;
388 HideImplicitMembers = TRUE;
389 KeepAlive = TRUE;
390 KeepAliveTimeout = DEFAULT_KEEPALIVE;
391 ListenBackLog = SOMAXCONN;
392 LogLevel = L_ERROR;
393 MaxClients = 100;
394 MaxClientsPerHost = 0;
395 MaxLogSize = 1024 * 1024;
396 MaxPrinterHistory = 10;
397 MaxRequestSize = 0;
398 ReloadTimeout = 60;
399 RootCertDuration = 300;
400 RunAsUser = FALSE;
401 Timeout = DEFAULT_TIMEOUT;
402
403 BrowseInterval = DEFAULT_INTERVAL;
404 BrowsePort = ippPort();
405 BrowseProtocols = BROWSE_CUPS;
406 BrowseShortNames = TRUE;
407 BrowseTimeout = DEFAULT_TIMEOUT;
408 Browsing = TRUE;
409
410 ClearString(&BrowseLocalOptions);
411 ClearString(&BrowseRemoteOptions);
412
413 JobHistory = DEFAULT_HISTORY;
414 JobFiles = DEFAULT_FILES;
415 JobAutoPurge = 0;
416 MaxJobs = 500;
417 MaxActiveJobs = 0;
418 MaxJobsPerUser = 0;
419 MaxJobsPerPrinter = 0;
420 MaxCopies = 100;
421
422 ClearString(&DefaultPolicy);
423
424 /*
425 * Read the configuration file...
426 */
427
428 if ((fp = cupsFileOpen(ConfigurationFile, "r")) == NULL)
429 return (0);
430
431 status = read_configuration(fp);
432
433 cupsFileClose(fp);
434
435 if (!status)
436 return (0);
437
438 if (RunAsUser)
439 RunUser = User;
440 else
441 RunUser = getuid();
442
443 /*
444 * Use the default system group if none was supplied in cupsd.conf...
445 */
446
447 if (NumSystemGroups == 0)
448 NumSystemGroups ++;
449
450 /*
451 * Get the access control list for browsing...
452 */
453
454 BrowseACL = FindLocation("CUPS_INTERNAL_BROWSE_ACL");
455
456 /*
457 * Open the system log for cupsd if necessary...
458 */
459
460 #ifdef HAVE_VSYSLOG
461 if (strcmp(AccessLog, "syslog") == 0 ||
462 strcmp(ErrorLog, "syslog") == 0 ||
463 strcmp(PageLog, "syslog") == 0)
464 openlog("cupsd", LOG_PID | LOG_NOWAIT | LOG_NDELAY, LOG_LPR);
465 #endif /* HAVE_VSYSLOG */
466
467 /*
468 * Log the configuration file that was used...
469 */
470
471 LogMessage(L_INFO, "Loaded configuration file \"%s\"", ConfigurationFile);
472
473 /*
474 * Check that we have at least one listen/port line; if not, report this
475 * as an error and exit!
476 */
477
478 if (NumListeners == 0)
479 {
480 /*
481 * No listeners!
482 */
483
484 LogMessage(L_EMERG, "No valid Listen or Port lines were found in the configuration file!");
485
486 /*
487 * Commit suicide...
488 */
489
490 cupsdEndProcess(getpid(), 0);
491 }
492
493 /*
494 * Set the default locale using the language and charset...
495 */
496
497 SetStringf(&DefaultLocale, "%s.%s", DefaultLanguage, DefaultCharset);
498
499 /*
500 * Update all relative filenames to include the full path from ServerRoot...
501 */
502
503 if (DocumentRoot[0] != '/')
504 SetStringf(&DocumentRoot, "%s/%s", ServerRoot, DocumentRoot);
505
506 if (RequestRoot[0] != '/')
507 SetStringf(&RequestRoot, "%s/%s", ServerRoot, RequestRoot);
508
509 if (ServerBin[0] != '/')
510 SetStringf(&ServerBin, "%s/%s", ServerRoot, ServerBin);
511
512 if (StateDir[0] != '/')
513 SetStringf(&StateDir, "%s/%s", ServerRoot, StateDir);
514
515 if (CacheDir[0] != '/')
516 SetStringf(&CacheDir, "%s/%s", ServerRoot, CacheDir);
517
518 #ifdef HAVE_SSL
519 if (ServerCertificate[0] != '/')
520 SetStringf(&ServerCertificate, "%s/%s", ServerRoot, ServerCertificate);
521
522 # if defined(HAVE_LIBSSL) || defined(HAVE_GNUTLS)
523 chown(ServerCertificate, RunUser, Group);
524 chmod(ServerCertificate, ConfigFilePerm);
525
526 if (ServerKey[0] != '/')
527 SetStringf(&ServerKey, "%s/%s", ServerRoot, ServerKey);
528
529 chown(ServerKey, RunUser, Group);
530 chmod(ServerKey, ConfigFilePerm);
531 # endif /* HAVE_LIBSSL || HAVE_GNUTLS */
532 #endif /* HAVE_SSL */
533
534 /*
535 * Make sure that directories and config files are owned and
536 * writable by the user and group in the cupsd.conf file...
537 */
538
539 chown(CacheDir, RunUser, Group);
540 chmod(CacheDir, 0775);
541
542 snprintf(temp, sizeof(temp), "%s/ppd", CacheDir);
543 if (access(temp, 0))
544 mkdir(temp, 0755);
545 chown(temp, RunUser, Group);
546 chmod(temp, 0755);
547
548 chown(StateDir, RunUser, Group);
549 chmod(StateDir, 0775);
550
551 snprintf(temp, sizeof(temp), "%s/certs", StateDir);
552 if (access(temp, 0))
553 mkdir(temp, 0711);
554 chown(temp, RunUser, Group);
555 chmod(temp, 0711);
556
557 chown(ServerRoot, RunUser, Group);
558 chmod(ServerRoot, 0775);
559
560 snprintf(temp, sizeof(temp), "%s/ppd", ServerRoot);
561 if (access(temp, 0))
562 mkdir(temp, 0755);
563 chown(temp, RunUser, Group);
564 chmod(temp, 0755);
565
566 snprintf(temp, sizeof(temp), "%s/ssl", ServerRoot);
567 if (access(temp, 0))
568 mkdir(temp, 0700);
569 chown(temp, RunUser, Group);
570 chmod(temp, 0700);
571
572 snprintf(temp, sizeof(temp), "%s/cupsd.conf", ServerRoot);
573 chown(temp, RunUser, Group);
574 chmod(temp, ConfigFilePerm);
575
576 snprintf(temp, sizeof(temp), "%s/classes.conf", ServerRoot);
577 chown(temp, RunUser, Group);
578 #ifdef __APPLE__
579 chmod(temp, 0600);
580 #else
581 chmod(temp, ConfigFilePerm);
582 #endif /* __APPLE__ */
583
584 snprintf(temp, sizeof(temp), "%s/printers.conf", ServerRoot);
585 chown(temp, RunUser, Group);
586 #ifdef __APPLE__
587 chmod(temp, 0600);
588 #else
589 chmod(temp, ConfigFilePerm);
590 #endif /* __APPLE__ */
591
592 snprintf(temp, sizeof(temp), "%s/passwd.md5", ServerRoot);
593 chown(temp, User, Group);
594 chmod(temp, 0600);
595
596 /*
597 * Make sure the request and temporary directories have the right
598 * permissions...
599 */
600
601 chown(RequestRoot, RunUser, Group);
602 chmod(RequestRoot, 0710);
603
604 if (!strncmp(TempDir, RequestRoot, strlen(RequestRoot)) ||
605 access(TempDir, 0))
606 {
607 /*
608 * Update ownership and permissions if the CUPS temp directory
609 * is under the spool directory or does not exist...
610 */
611
612 if (access(TempDir, 0))
613 mkdir(TempDir, 01770);
614
615 chown(TempDir, RunUser, Group);
616 chmod(TempDir, 01770);
617 }
618
619 if (!strncmp(TempDir, RequestRoot, strlen(RequestRoot)))
620 {
621 /*
622 * Clean out the temporary directory...
623 */
624
625 cups_dir_t *dir; /* Temporary directory */
626 cups_dentry_t *dent; /* Directory entry */
627 char tempfile[1024]; /* Temporary filename */
628
629
630 if ((dir = cupsDirOpen(TempDir)) != NULL)
631 {
632 LogMessage(L_INFO, "Cleaning out old temporary files in \"%s\"...",
633 TempDir);
634
635 while ((dent = cupsDirRead(dir)) != NULL)
636 {
637 snprintf(tempfile, sizeof(tempfile), "%s/%s", TempDir, dent->filename);
638
639 if (unlink(tempfile))
640 LogMessage(L_ERROR, "Unable to remove temporary file \"%s\" - %s",
641 tempfile, strerror(errno));
642 else
643 LogMessage(L_DEBUG, "Removed temporary file \"%s\"...", tempfile);
644 }
645
646 cupsDirClose(dir);
647 }
648 else
649 LogMessage(L_ERROR, "Unable to open temporary directory \"%s\" - %s",
650 TempDir, strerror(errno));
651 }
652
653 /*
654 * Check the MaxClients setting, and then allocate memory for it...
655 */
656
657 if (MaxClients > (MaxFDs / 3) || MaxClients <= 0)
658 {
659 if (MaxClients > 0)
660 LogMessage(L_INFO, "MaxClients limited to 1/3 (%d) of the file descriptor limit (%d)...",
661 MaxFDs / 3, MaxFDs);
662
663 MaxClients = MaxFDs / 3;
664 }
665
666 if ((Clients = calloc(sizeof(client_t), MaxClients)) == NULL)
667 {
668 LogMessage(L_ERROR, "ReadConfiguration: Unable to allocate memory for %d clients: %s",
669 MaxClients, strerror(errno));
670 exit(1);
671 }
672 else
673 LogMessage(L_INFO, "Configured for up to %d clients.", MaxClients);
674
675 /*
676 * Check the MaxActiveJobs setting; limit to 1/3 the available
677 * file descriptors, since we need a pipe for each job...
678 */
679
680 if (MaxActiveJobs > (MaxFDs / 3))
681 MaxActiveJobs = MaxFDs / 3;
682
683 if (Classification && strcasecmp(Classification, "none") == 0)
684 ClearString(&Classification);
685
686 if (Classification)
687 LogMessage(L_INFO, "Security set to \"%s\"", Classification);
688
689 /*
690 * Update the MaxClientsPerHost value, as needed...
691 */
692
693 if (MaxClientsPerHost <= 0)
694 MaxClientsPerHost = MaxClients;
695
696 if (MaxClientsPerHost > MaxClients)
697 MaxClientsPerHost = MaxClients;
698
699 LogMessage(L_INFO, "Allowing up to %d client connections per host.",
700 MaxClientsPerHost);
701
702 /*
703 * Update the default policy, as needed...
704 */
705
706 if (DefaultPolicy)
707 DefaultPolicyPtr = cupsdFindPolicy(DefaultPolicy);
708 else
709 DefaultPolicyPtr = NULL;
710
711 if (!DefaultPolicyPtr)
712 {
713 cupsd_policy_t *p; /* New policy */
714 location_t *po; /* New policy operation */
715
716
717 if (DefaultPolicy)
718 LogMessage(L_ERROR, "Default policy \"%s\" not found!", DefaultPolicy);
719
720 if ((DefaultPolicyPtr = cupsdFindPolicy("default")) != NULL)
721 LogMessage(L_INFO, "Using policy \"default\" as the default!");
722 else
723 {
724 LogMessage(L_INFO, "Creating CUPS default administrative policy:");
725
726 DefaultPolicyPtr = p = cupsdAddPolicy("default");
727
728 LogMessage(L_INFO, "<Policy default>");
729 LogMessage(L_INFO, "<Limit Send-Document Send-URI Cancel-Job Hold-Job "
730 "Release-Job Restart-Job Purge-Jobs "
731 "Set-Job-Attributes Create-Job-Subscription "
732 "Renew-Subscription Cancel-Subscription "
733 "Get-Notifications Reprocess-Job Cancel-Current-Job "
734 "Suspend-Current-Job Resume-Job CUPS-Move-Job>");
735 LogMessage(L_INFO, "Order Allow,Deny");
736
737 po = cupsdAddPolicyOp(p, NULL, IPP_SEND_DOCUMENT);
738 po->order_type = AUTH_DENY;
739 po->level = AUTH_USER;
740
741 AddName(po, "@OWNER");
742 AddName(po, "@SYSTEM");
743 LogMessage(L_INFO, "Require user @OWNER @SYSTEM");
744
745 cupsdAddPolicyOp(p, po, IPP_SEND_URI);
746 cupsdAddPolicyOp(p, po, IPP_CANCEL_JOB);
747 cupsdAddPolicyOp(p, po, IPP_HOLD_JOB);
748 cupsdAddPolicyOp(p, po, IPP_RELEASE_JOB);
749 cupsdAddPolicyOp(p, po, IPP_RESTART_JOB);
750 cupsdAddPolicyOp(p, po, IPP_PURGE_JOBS);
751 cupsdAddPolicyOp(p, po, IPP_SET_JOB_ATTRIBUTES);
752 cupsdAddPolicyOp(p, po, IPP_CREATE_JOB_SUBSCRIPTION);
753 cupsdAddPolicyOp(p, po, IPP_RENEW_SUBSCRIPTION);
754 cupsdAddPolicyOp(p, po, IPP_CANCEL_SUBSCRIPTION);
755 cupsdAddPolicyOp(p, po, IPP_GET_NOTIFICATIONS);
756 cupsdAddPolicyOp(p, po, IPP_REPROCESS_JOB);
757 cupsdAddPolicyOp(p, po, IPP_CANCEL_CURRENT_JOB);
758 cupsdAddPolicyOp(p, po, IPP_SUSPEND_CURRENT_JOB);
759 cupsdAddPolicyOp(p, po, IPP_RESUME_JOB);
760 cupsdAddPolicyOp(p, po, CUPS_MOVE_JOB);
761
762 LogMessage(L_INFO, "</Limit>");
763
764 LogMessage(L_INFO, "<Limit Pause-Printer Resume-Printer "
765 "Set-Printer-Attributes Enable-Printer "
766 "Disable-Printer Pause-Printer-After-Current-Job "
767 "Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer "
768 "Activate-Printer Restart-Printer Shutdown-Printer "
769 "Startup-Printer Promote-Job Schedule-Job-After "
770 "CUPS-Add-Printer CUPS-Delete-Printer "
771 "CUPS-Add-Class CUPS-Delete-Class "
772 "CUPS-Accept-Jobs CUPS-Reject-Jobs "
773 "CUPS-Set-Default CUPS-Add-Device CUPS-Delete-Device>");
774 LogMessage(L_INFO, "Order Allow,Deny");
775 LogMessage(L_INFO, "AuthType Basic");
776
777 po = cupsdAddPolicyOp(p, NULL, IPP_PAUSE_PRINTER);
778 po->order_type = AUTH_DENY;
779 po->type = AUTH_BASIC;
780 po->level = AUTH_USER;
781
782 AddName(po, "@SYSTEM");
783 LogMessage(L_INFO, "Require user @SYSTEM");
784
785 cupsdAddPolicyOp(p, po, IPP_RESUME_PRINTER);
786 cupsdAddPolicyOp(p, po, IPP_SET_PRINTER_ATTRIBUTES);
787 cupsdAddPolicyOp(p, po, IPP_ENABLE_PRINTER);
788 cupsdAddPolicyOp(p, po, IPP_DISABLE_PRINTER);
789 cupsdAddPolicyOp(p, po, IPP_PAUSE_PRINTER_AFTER_CURRENT_JOB);
790 cupsdAddPolicyOp(p, po, IPP_HOLD_NEW_JOBS);
791 cupsdAddPolicyOp(p, po, IPP_RELEASE_HELD_NEW_JOBS);
792 cupsdAddPolicyOp(p, po, IPP_DEACTIVATE_PRINTER);
793 cupsdAddPolicyOp(p, po, IPP_ACTIVATE_PRINTER);
794 cupsdAddPolicyOp(p, po, IPP_RESTART_PRINTER);
795 cupsdAddPolicyOp(p, po, IPP_SHUTDOWN_PRINTER);
796 cupsdAddPolicyOp(p, po, IPP_STARTUP_PRINTER);
797 cupsdAddPolicyOp(p, po, IPP_PROMOTE_JOB);
798 cupsdAddPolicyOp(p, po, IPP_SCHEDULE_JOB_AFTER);
799 cupsdAddPolicyOp(p, po, CUPS_ADD_PRINTER);
800 cupsdAddPolicyOp(p, po, CUPS_DELETE_PRINTER);
801 cupsdAddPolicyOp(p, po, CUPS_ADD_CLASS);
802 cupsdAddPolicyOp(p, po, CUPS_DELETE_CLASS);
803 cupsdAddPolicyOp(p, po, CUPS_ACCEPT_JOBS);
804 cupsdAddPolicyOp(p, po, CUPS_REJECT_JOBS);
805 cupsdAddPolicyOp(p, po, CUPS_SET_DEFAULT);
806 cupsdAddPolicyOp(p, po, CUPS_ADD_DEVICE);
807 cupsdAddPolicyOp(p, po, CUPS_DELETE_DEVICE);
808
809 LogMessage(L_INFO, "</Limit>");
810
811 LogMessage(L_INFO, "<Limit All>");
812 LogMessage(L_INFO, "Order Allow,Deny");
813
814 po = cupsdAddPolicyOp(p, NULL, IPP_ANY_OPERATION);
815 po->order_type = AUTH_DENY;
816
817 LogMessage(L_INFO, "</Limit>");
818 LogMessage(L_INFO, "</Policy>");
819 }
820 }
821
822 /*
823 * If we are doing a full reload or the server root has changed, flush
824 * the jobs, printers, etc. and start from scratch...
825 */
826
827 if (NeedReload == RELOAD_ALL ||
828 !old_serverroot || !ServerRoot || strcmp(old_serverroot, ServerRoot) ||
829 !old_requestroot || !RequestRoot || strcmp(old_requestroot, RequestRoot))
830 {
831 LogMessage(L_INFO, "Full reload is required.");
832
833 /*
834 * Free all memory...
835 */
836
837 FreeAllJobs();
838 DeleteAllClasses();
839 DeleteAllPrinters();
840
841 DefaultPrinter = NULL;
842
843 if (MimeDatabase != NULL)
844 mimeDelete(MimeDatabase);
845
846 if (NumMimeTypes)
847 {
848 for (i = 0; i < NumMimeTypes; i ++)
849 free((void *)MimeTypes[i]);
850
851 free(MimeTypes);
852 }
853
854 /*
855 * Read the MIME type and conversion database...
856 */
857
858 snprintf(temp, sizeof(temp), "%s/filter", ServerBin);
859
860 MimeDatabase = mimeLoad(ServerRoot, temp);
861
862 LogMessage(L_INFO, "Loaded MIME database from \'%s\': %d types, %d filters...",
863 ServerRoot, MimeDatabase->num_types, MimeDatabase->num_filters);
864
865 /*
866 * Create a list of MIME types for the document-format-supported
867 * attribute...
868 */
869
870 NumMimeTypes = MimeDatabase->num_types;
871 if (!mimeType(MimeDatabase, "application", "octet-stream"))
872 NumMimeTypes ++;
873
874 MimeTypes = calloc(NumMimeTypes, sizeof(const char *));
875
876 for (i = 0; i < MimeDatabase->num_types; i ++)
877 {
878 snprintf(type, sizeof(type), "%s/%s", MimeDatabase->types[i]->super,
879 MimeDatabase->types[i]->type);
880
881 MimeTypes[i] = strdup(type);
882 }
883
884 if (i < NumMimeTypes)
885 MimeTypes[i] = strdup("application/octet-stream");
886
887 /*
888 * Load banners...
889 */
890
891 snprintf(temp, sizeof(temp), "%s/banners", DataDir);
892 LoadBanners(temp);
893
894 /*
895 * Load printers and classes...
896 */
897
898 LoadAllPrinters();
899 LoadAllClasses();
900
901 CreateCommonData();
902
903 /*
904 * Load queued jobs...
905 */
906
907 LoadAllJobs();
908
909 LogMessage(L_INFO, "Full reload complete.");
910 }
911 else
912 {
913 CreateCommonData();
914
915 LogMessage(L_INFO, "Partial reload complete.");
916 }
917
918 /*
919 * Reset the reload state...
920 */
921
922 NeedReload = RELOAD_NONE;
923
924 ClearString(&old_serverroot);
925 ClearString(&old_requestroot);
926
927 /*
928 * Startup the server and return...
929 */
930
931 StartServer();
932
933 return (1);
934 }
935
936
937 /*
938 * 'get_address()' - Get an address + port number from a line.
939 */
940
941 static int /* O - 1 if address good, 0 if bad */
942 get_address(const char *value, /* I - Value string */
943 unsigned defaddress, /* I - Default address */
944 int defport, /* I - Default port */
945 int deffamily, /* I - Default family */
946 http_addr_t *address) /* O - Socket address */
947 {
948 char hostname[256], /* Hostname or IP */
949 portname[256]; /* Port number or name */
950 struct hostent *host; /* Host address */
951 struct servent *port; /* Port number */
952
953
954 /*
955 * Initialize the socket address to the defaults...
956 */
957
958 memset(address, 0, sizeof(http_addr_t));
959
960 #ifdef AF_INET6
961 if (deffamily == AF_INET6)
962 {
963 address->ipv6.sin6_family = AF_INET6;
964 address->ipv6.sin6_addr.s6_addr32[0] = htonl(defaddress);
965 address->ipv6.sin6_addr.s6_addr32[1] = htonl(defaddress);
966 address->ipv6.sin6_addr.s6_addr32[2] = htonl(defaddress);
967 address->ipv6.sin6_addr.s6_addr32[3] = htonl(defaddress);
968 address->ipv6.sin6_port = htons(defport);
969 }
970 else
971 #endif /* AF_INET6 */
972 {
973 address->ipv4.sin_family = AF_INET;
974 address->ipv4.sin_addr.s_addr = htonl(defaddress);
975 address->ipv4.sin_port = htons(defport);
976 }
977
978 #ifdef AF_LOCAL
979 /*
980 * If the address starts with a "/", it is a domain socket...
981 */
982
983 if (*value == '/')
984 {
985 if (strlen(value) >= sizeof(address->un.sun_path))
986 {
987 LogMessage(L_ERROR, "Domain socket name \"%s\" too long!", value);
988 return (0);
989 }
990
991 address->un.sun_family = AF_LOCAL;
992 strcpy(address->un.sun_path, value);
993
994 return (1);
995 }
996 #endif /* AF_LOCAL */
997
998 /*
999 * Try to grab a hostname and port number...
1000 */
1001
1002 switch (sscanf(value, "%255[^:]:%255s", hostname, portname))
1003 {
1004 case 1 :
1005 if (strchr(hostname, '.') == NULL && defaddress == INADDR_ANY)
1006 {
1007 /*
1008 * Hostname is a port number...
1009 */
1010
1011 strlcpy(portname, hostname, sizeof(portname));
1012 hostname[0] = '\0';
1013 }
1014 else
1015 portname[0] = '\0';
1016 break;
1017
1018 case 2 :
1019 break;
1020
1021 default :
1022 LogMessage(L_ERROR, "Unable to decode address \"%s\"!", value);
1023 return (0);
1024 }
1025
1026 /*
1027 * Decode the hostname and port number as needed...
1028 */
1029
1030 if (hostname[0] && strcmp(hostname, "*"))
1031 {
1032 if ((host = httpGetHostByName(hostname)) == NULL)
1033 {
1034 LogMessage(L_ERROR, "httpGetHostByName(\"%s\") failed - %s!", hostname,
1035 hstrerror(h_errno));
1036 return (0);
1037 }
1038
1039 httpAddrLoad(host, defport, 0, address);
1040 }
1041
1042 if (portname[0] != '\0')
1043 {
1044 if (isdigit(portname[0] & 255))
1045 {
1046 #ifdef AF_INET6
1047 if (address->addr.sa_family == AF_INET6)
1048 address->ipv6.sin6_port = htons(atoi(portname));
1049 else
1050 #endif /* AF_INET6 */
1051 address->ipv4.sin_port = htons(atoi(portname));
1052 }
1053 else
1054 {
1055 if ((port = getservbyname(portname, NULL)) == NULL)
1056 {
1057 LogMessage(L_ERROR, "getservbyname(\"%s\") failed - %s!", portname,
1058 strerror(errno));
1059 return (0);
1060 }
1061 else
1062 {
1063 #ifdef AF_INET6
1064 if (address->addr.sa_family == AF_INET6)
1065 address->ipv6.sin6_port = htons(port->s_port);
1066 else
1067 #endif /* AF_INET6 */
1068 address->ipv4.sin_port = htons(port->s_port);
1069 }
1070 }
1071 }
1072
1073 return (1);
1074 }
1075
1076
1077 /*
1078 * 'get_addr_and_mask()' - Get an IP address and netmask.
1079 */
1080
1081 static int /* O - 1 on success, 0 on failure */
1082 get_addr_and_mask(const char *value, /* I - String from config file */
1083 unsigned *ip, /* O - Address value */
1084 unsigned *mask) /* O - Mask value */
1085 {
1086 int i, /* Looping var */
1087 family, /* Address family */
1088 ipcount; /* Count of fields in address */
1089 static unsigned netmasks[4][4] = /* Standard netmasks... */
1090 {
1091 { 0xffffffff, 0x00000000, 0x00000000, 0x00000000 },
1092 { 0xffffffff, 0xffffffff, 0x00000000, 0x00000000 },
1093 { 0xffffffff, 0xffffffff, 0xffffffff, 0x00000000 },
1094 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
1095 };
1096
1097
1098 /*
1099 * Get the address...
1100 */
1101
1102 memset(ip, 0, sizeof(unsigned) * 4);
1103 family = AF_INET;
1104 ipcount = sscanf(value, "%u.%u.%u.%u", ip + 0, ip + 1, ip + 2, ip + 3);
1105
1106 #ifdef AF_INET6
1107 /*
1108 * See if we have any values > 255; if so, this is an IPv6 address only.
1109 */
1110
1111 for (i = 0; i < ipcount; i ++)
1112 if (ip[0] > 255)
1113 {
1114 family = AF_INET6;
1115 break;
1116 }
1117 #endif /* AF_INET6 */
1118
1119 if ((value = strchr(value, '/')) != NULL)
1120 {
1121 /*
1122 * Get the netmask value(s)...
1123 */
1124
1125 value ++;
1126 memset(mask, 0, sizeof(unsigned) * 4);
1127 switch (sscanf(value, "%u.%u.%u.%u", mask + 0, mask + 1,
1128 mask + 2, mask + 3))
1129 {
1130 case 1 :
1131 #ifdef AF_INET6
1132 if (mask[0] >= 32)
1133 family = AF_INET6;
1134
1135 if (family == AF_INET6)
1136 {
1137 i = 128 - mask[0];
1138
1139 if (i <= 96)
1140 mask[0] = 0xffffffff;
1141 else
1142 mask[0] = (0xffffffff << (128 - mask[0])) & 0xffffffff;
1143
1144 if (i <= 64)
1145 mask[1] = 0xffffffff;
1146 else if (i >= 96)
1147 mask[1] = 0;
1148 else
1149 mask[1] = (0xffffffff << (96 - mask[0])) & 0xffffffff;
1150
1151 if (i <= 32)
1152 mask[1] = 0xffffffff;
1153 else if (i >= 64)
1154 mask[1] = 0;
1155 else
1156 mask[1] = (0xffffffff << (64 - mask[0])) & 0xffffffff;
1157
1158 if (i >= 32)
1159 mask[1] = 0;
1160 else
1161 mask[1] = (0xffffffff << (32 - mask[0])) & 0xffffffff;
1162 }
1163 else
1164 #endif /* AF_INET6 */
1165 {
1166 i = 32 - mask[0];
1167
1168 if (i <= 24)
1169 mask[0] = 0xffffffff;
1170 else
1171 mask[0] = (0xffffffff << (32 - mask[0])) & 0xffffffff;
1172
1173 if (i <= 16)
1174 mask[1] = 0xffffffff;
1175 else if (i >= 24)
1176 mask[1] = 0;
1177 else
1178 mask[1] = (0xffffffff << (24 - mask[0])) & 0xffffffff;
1179
1180 if (i <= 8)
1181 mask[1] = 0xffffffff;
1182 else if (i >= 16)
1183 mask[1] = 0;
1184 else
1185 mask[1] = (0xffffffff << (16 - mask[0])) & 0xffffffff;
1186
1187 if (i >= 8)
1188 mask[1] = 0;
1189 else
1190 mask[1] = (0xffffffff << (8 - mask[0])) & 0xffffffff;
1191 }
1192
1193 case 4 :
1194 break;
1195
1196 default :
1197 return (0);
1198 }
1199 }
1200 else
1201 memcpy(mask, netmasks[ipcount - 1], sizeof(unsigned) * 4);
1202
1203 /*
1204 * Check for a valid netmask; no fallback like in CUPS 1.1.x!
1205 */
1206
1207 if ((ip[0] & ~mask[0]) != 0 ||
1208 (ip[1] & ~mask[1]) != 0 ||
1209 (ip[2] & ~mask[2]) != 0 ||
1210 (ip[3] & ~mask[3]) != 0)
1211 return (0);
1212
1213 return (1);
1214 }
1215
1216
1217 /*
1218 * 'parse_aaa()' - Parse authentication, authorization, and access control lines.
1219 */
1220
1221 static int /* O - 1 on success, 0 on failure */
1222 parse_aaa(location_t *loc, /* I - Location */
1223 char *line, /* I - Line from file */
1224 char *value, /* I - Start of value data */
1225 int linenum) /* I - Current line number */
1226 {
1227 char *valptr; /* Pointer into value */
1228 unsigned ip[4], /* IP address components */
1229 mask[4]; /* IP netmask components */
1230
1231
1232 if (!strcasecmp(line, "Encryption"))
1233 {
1234 /*
1235 * "Encryption xxx" - set required encryption level...
1236 */
1237
1238 if (!strcasecmp(value, "never"))
1239 loc->encryption = HTTP_ENCRYPT_NEVER;
1240 else if (!strcasecmp(value, "always"))
1241 {
1242 LogMessage(L_ERROR, "Encryption value \"%s\" on line %d is invalid in this context. "
1243 "Using \"required\" instead.", value, linenum);
1244
1245 loc->encryption = HTTP_ENCRYPT_REQUIRED;
1246 }
1247 else if (!strcasecmp(value, "required"))
1248 loc->encryption = HTTP_ENCRYPT_REQUIRED;
1249 else if (!strcasecmp(value, "ifrequested"))
1250 loc->encryption = HTTP_ENCRYPT_IF_REQUESTED;
1251 else
1252 {
1253 LogMessage(L_ERROR, "Unknown Encryption value %s on line %d.",
1254 value, linenum);
1255 return (0);
1256 }
1257 }
1258 else if (!strcasecmp(line, "Order"))
1259 {
1260 /*
1261 * "Order Deny,Allow" or "Order Allow,Deny"...
1262 */
1263
1264 if (!strncasecmp(value, "deny", 4))
1265 loc->order_type = AUTH_ALLOW;
1266 else if (!strncasecmp(value, "allow", 5))
1267 loc->order_type = AUTH_DENY;
1268 else
1269 {
1270 LogMessage(L_ERROR, "Unknown Order value %s on line %d.",
1271 value, linenum);
1272 return (0);
1273 }
1274 }
1275 else if (!strcasecmp(line, "Allow") || !strcasecmp(line, "Deny"))
1276 {
1277 /*
1278 * Allow [From] host/ip...
1279 * Deny [From] host/ip...
1280 */
1281
1282 if (!strncasecmp(value, "from", 4))
1283 {
1284 /*
1285 * Strip leading "from"...
1286 */
1287
1288 value += 4;
1289
1290 while (isspace(*value & 255))
1291 value ++;
1292 }
1293
1294 /*
1295 * Figure out what form the allow/deny address takes:
1296 *
1297 * All
1298 * None
1299 * *.domain.com
1300 * .domain.com
1301 * host.domain.com
1302 * nnn.*
1303 * nnn.nnn.*
1304 * nnn.nnn.nnn.*
1305 * nnn.nnn.nnn.nnn
1306 * nnn.nnn.nnn.nnn/mm
1307 * nnn.nnn.nnn.nnn/mmm.mmm.mmm.mmm
1308 */
1309
1310 if (!strcasecmp(value, "all"))
1311 {
1312 /*
1313 * All hosts...
1314 */
1315
1316 if (!strcasecmp(line, "Allow"))
1317 AllowIP(loc, zeros, zeros);
1318 else
1319 DenyIP(loc, zeros, zeros);
1320 }
1321 else if (!strcasecmp(value, "none"))
1322 {
1323 /*
1324 * No hosts...
1325 */
1326
1327 if (!strcasecmp(line, "Allow"))
1328 AllowIP(loc, ones, zeros);
1329 else
1330 DenyIP(loc, ones, zeros);
1331 }
1332 else if (value[0] == '*' || value[0] == '.' || !isdigit(value[0] & 255))
1333 {
1334 /*
1335 * Host or domain name...
1336 */
1337
1338 if (value[0] == '*')
1339 value ++;
1340
1341 if (!strcasecmp(line, "Allow"))
1342 AllowHost(loc, value);
1343 else
1344 DenyHost(loc, value);
1345 }
1346 else
1347 {
1348 /*
1349 * One of many IP address forms...
1350 */
1351
1352 if (!get_addr_and_mask(value, ip, mask))
1353 {
1354 LogMessage(L_ERROR, "Bad netmask value %s on line %d.",
1355 value, linenum);
1356 return (0);
1357 }
1358
1359 if (!strcasecmp(line, "Allow"))
1360 AllowIP(loc, ip, mask);
1361 else
1362 DenyIP(loc, ip, mask);
1363 }
1364 }
1365 else if (!strcasecmp(line, "AuthType"))
1366 {
1367 /*
1368 * AuthType {none,basic,digest,basicdigest}
1369 */
1370
1371 if (!strcasecmp(value, "none"))
1372 {
1373 loc->type = AUTH_NONE;
1374 loc->level = AUTH_ANON;
1375 }
1376 else if (!strcasecmp(value, "basic"))
1377 {
1378 loc->type = AUTH_BASIC;
1379
1380 if (loc->level == AUTH_ANON)
1381 loc->level = AUTH_USER;
1382 }
1383 else if (!strcasecmp(value, "digest"))
1384 {
1385 loc->type = AUTH_DIGEST;
1386
1387 if (loc->level == AUTH_ANON)
1388 loc->level = AUTH_USER;
1389 }
1390 else if (!strcasecmp(value, "basicdigest"))
1391 {
1392 loc->type = AUTH_BASICDIGEST;
1393
1394 if (loc->level == AUTH_ANON)
1395 loc->level = AUTH_USER;
1396 }
1397 else
1398 {
1399 LogMessage(L_WARN, "Unknown authorization type %s on line %d.",
1400 value, linenum);
1401 return (0);
1402 }
1403 }
1404 else if (!strcasecmp(line, "AuthClass"))
1405 {
1406 /*
1407 * AuthClass anonymous, user, system, group
1408 */
1409
1410 if (!strcasecmp(value, "anonymous"))
1411 {
1412 loc->type = AUTH_NONE;
1413 loc->level = AUTH_ANON;
1414 }
1415 else if (!strcasecmp(value, "user"))
1416 loc->level = AUTH_USER;
1417 else if (!strcasecmp(value, "group"))
1418 loc->level = AUTH_GROUP;
1419 else if (!strcasecmp(value, "system"))
1420 {
1421 loc->level = AUTH_GROUP;
1422
1423 AddName(loc, "@SYSTEM");
1424 }
1425 else
1426 {
1427 LogMessage(L_WARN, "Unknown authorization class %s on line %d.",
1428 value, linenum);
1429 return (0);
1430 }
1431 }
1432 else if (!strcasecmp(line, "AuthGroupName"))
1433 AddName(loc, value);
1434 else if (!strcasecmp(line, "Require"))
1435 {
1436 /*
1437 * Apache synonym for AuthClass and AuthGroupName...
1438 *
1439 * Get initial word:
1440 *
1441 * Require valid-user
1442 * Require group names
1443 * Require user names
1444 */
1445
1446 for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
1447
1448 if (*valptr)
1449 *valptr++ = '\0';
1450
1451 if (!strcasecmp(value, "valid-user") ||
1452 !strcasecmp(value, "user"))
1453 loc->level = AUTH_USER;
1454 else if (!strcasecmp(value, "group"))
1455 loc->level = AUTH_GROUP;
1456 else
1457 {
1458 LogMessage(L_WARN, "Unknown Require type %s on line %d.",
1459 value, linenum);
1460 return (0);
1461 }
1462
1463 /*
1464 * Get the list of names from the line...
1465 */
1466
1467 for (value = valptr; *value;)
1468 {
1469 while (isspace(*value & 255))
1470 value ++;
1471
1472 if (*value == '\"' || *value == '\'')
1473 {
1474 /*
1475 * Grab quoted name...
1476 */
1477
1478 for (valptr = value + 1; *valptr != *value && *valptr; valptr ++);
1479
1480 value ++;
1481 }
1482 else
1483 {
1484 /*
1485 * Grab literal name.
1486 */
1487
1488 for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
1489 }
1490
1491 if (*valptr)
1492 *valptr++ = '\0';
1493
1494 AddName(loc, value);
1495
1496 for (value = valptr; isspace(*value & 255); value ++);
1497 }
1498 }
1499 else if (!strcasecmp(line, "Satisfy"))
1500 {
1501 if (!strcasecmp(value, "all"))
1502 loc->satisfy = AUTH_SATISFY_ALL;
1503 else if (!strcasecmp(value, "any"))
1504 loc->satisfy = AUTH_SATISFY_ANY;
1505 else
1506 {
1507 LogMessage(L_WARN, "Unknown Satisfy value %s on line %d.", value,
1508 linenum);
1509 return (0);
1510 }
1511 }
1512 else
1513 return (0);
1514
1515 return (1);
1516 }
1517
1518
1519 /*
1520 * 'read_configuration()' - Read a configuration file.
1521 */
1522
1523 static int /* O - 1 on success, 0 on failure */
1524 read_configuration(cups_file_t *fp) /* I - File to read from */
1525 {
1526 int i; /* Looping var */
1527 int linenum; /* Current line number */
1528 char line[HTTP_MAX_BUFFER], /* Line from file */
1529 temp[HTTP_MAX_BUFFER], /* Temporary buffer for value */
1530 temp2[HTTP_MAX_BUFFER], /* Temporary buffer 2 for value */
1531 *ptr, /* Pointer into line/temp */
1532 *value; /* Pointer to value */
1533 int valuelen; /* Length of value */
1534 var_t *var; /* Current variable */
1535 unsigned ip[4], /* Address value */
1536 mask[4]; /* Netmask value */
1537 dirsvc_relay_t *relay; /* Relay data */
1538 dirsvc_poll_t *poll; /* Polling data */
1539 http_addr_t polladdr; /* Polling address */
1540 location_t *location; /* Browse location */
1541 cups_file_t *incfile; /* Include file */
1542 char incname[1024]; /* Include filename */
1543
1544
1545 /*
1546 * Loop through each line in the file...
1547 */
1548
1549 linenum = 0;
1550
1551 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
1552 {
1553 /*
1554 * Decode the directive...
1555 */
1556
1557 if (!strcasecmp(line, "Include"))
1558 {
1559 /*
1560 * Include filename
1561 */
1562
1563 if (value[0] == '/')
1564 strlcpy(incname, value, sizeof(incname));
1565 else
1566 snprintf(incname, sizeof(incname), "%s/%s", ServerRoot, value);
1567
1568 if ((incfile = cupsFileOpen(incname, "rb")) == NULL)
1569 LogMessage(L_ERROR, "Unable to include config file \"%s\" - %s",
1570 incname, strerror(errno));
1571 else
1572 {
1573 read_configuration(incfile);
1574 cupsFileClose(incfile);
1575 }
1576 }
1577 else if (!strcasecmp(line, "<Location"))
1578 {
1579 /*
1580 * <Location path>
1581 */
1582
1583 if (value)
1584 {
1585 linenum = read_location(fp, value, linenum);
1586 if (linenum == 0)
1587 return (0);
1588 }
1589 else
1590 {
1591 LogMessage(L_ERROR, "Syntax error on line %d.",
1592 linenum);
1593 return (0);
1594 }
1595 }
1596 else if (!strcasecmp(line, "<Policy"))
1597 {
1598 /*
1599 * <Policy name>
1600 */
1601
1602 if (value)
1603 {
1604 linenum = read_policy(fp, value, linenum);
1605 if (linenum == 0)
1606 return (0);
1607 }
1608 else
1609 {
1610 LogMessage(L_ERROR, "Syntax error on line %d.",
1611 linenum);
1612 return (0);
1613 }
1614 }
1615 else if (!strcasecmp(line, "Port") || !strcasecmp(line, "Listen"))
1616 {
1617 /*
1618 * Add a listening address to the list...
1619 */
1620
1621 listener_t *lis; /* New listeners array */
1622
1623
1624 if (NumListeners == 0)
1625 lis = malloc(sizeof(listener_t));
1626 else
1627 lis = realloc(Listeners, (NumListeners + 1) * sizeof(listener_t));
1628
1629 if (!lis)
1630 {
1631 LogMessage(L_ERROR, "Unable to allocate %s at line %d - %s.",
1632 line, linenum, strerror(errno));
1633 continue;
1634 }
1635
1636 Listeners = lis;
1637 lis += NumListeners;
1638
1639 memset(lis, 0, sizeof(listener_t));
1640
1641 if (get_address(value, INADDR_ANY, IPP_PORT, AF_INET, &(lis->address)))
1642 {
1643 httpAddrString(&(lis->address), temp, sizeof(temp));
1644
1645 #ifdef AF_INET6
1646 if (lis->address.addr.sa_family == AF_INET6)
1647 LogMessage(L_INFO, "Listening to %s:%d (IPv6)", temp,
1648 ntohs(lis->address.ipv6.sin6_port));
1649 else
1650 #endif /* AF_INET6 */
1651 LogMessage(L_INFO, "Listening to %s:%d", temp,
1652 ntohs(lis->address.ipv4.sin_port));
1653 NumListeners ++;
1654 }
1655 else
1656 LogMessage(L_ERROR, "Bad %s address %s at line %d.", line,
1657 value, linenum);
1658 }
1659 #ifdef HAVE_SSL
1660 else if (!strcasecmp(line, "SSLPort") || !strcasecmp(line, "SSLListen"))
1661 {
1662 /*
1663 * Add a listening address to the list...
1664 */
1665
1666 listener_t *lis; /* New listeners array */
1667
1668
1669 if (NumListeners == 0)
1670 lis = malloc(sizeof(listener_t));
1671 else
1672 lis = realloc(Listeners, (NumListeners + 1) * sizeof(listener_t));
1673
1674 if (!lis)
1675 {
1676 LogMessage(L_ERROR, "Unable to allocate %s at line %d - %s.",
1677 line, linenum, strerror(errno));
1678 continue;
1679 }
1680
1681 Listeners = lis;
1682 lis += NumListeners;
1683
1684 if (get_address(value, INADDR_ANY, IPP_PORT, AF_INET, &(lis->address)))
1685 {
1686 httpAddrString(&(lis->address), temp, sizeof(temp));
1687
1688 #ifdef AF_INET6
1689 if (lis->address.addr.sa_family == AF_INET6)
1690 LogMessage(L_INFO, "Listening to %s:%d (IPv6)", temp,
1691 ntohs(lis->address.ipv6.sin6_port));
1692 else
1693 #endif /* AF_INET6 */
1694 LogMessage(L_INFO, "Listening to %s:%d", temp,
1695 ntohs(lis->address.ipv4.sin_port));
1696 lis->encryption = HTTP_ENCRYPT_ALWAYS;
1697 NumListeners ++;
1698 }
1699 else
1700 LogMessage(L_ERROR, "Bad %s address %s at line %d.", line,
1701 value, linenum);
1702 }
1703 #endif /* HAVE_SSL */
1704 else if (!strcasecmp(line, "BrowseAddress"))
1705 {
1706 /*
1707 * Add a browse address to the list...
1708 */
1709
1710 dirsvc_addr_t *dira; /* New browse address array */
1711
1712
1713 if (NumBrowsers == 0)
1714 dira = malloc(sizeof(dirsvc_addr_t));
1715 else
1716 dira = realloc(Browsers, (NumBrowsers + 1) * sizeof(dirsvc_addr_t));
1717
1718 if (!dira)
1719 {
1720 LogMessage(L_ERROR, "Unable to allocate BrowseAddress at line %d - %s.",
1721 linenum, strerror(errno));
1722 continue;
1723 }
1724
1725 Browsers = dira;
1726 dira += NumBrowsers;
1727
1728 memset(dira, 0, sizeof(dirsvc_addr_t));
1729
1730 if (!strcasecmp(value, "@LOCAL"))
1731 {
1732 /*
1733 * Send browse data to all local interfaces...
1734 */
1735
1736 strcpy(dira->iface, "*");
1737 NumBrowsers ++;
1738 }
1739 else if (!strncasecmp(value, "@IF(", 4))
1740 {
1741 /*
1742 * Send browse data to the named interface...
1743 */
1744
1745 strlcpy(dira->iface, value + 4, sizeof(Browsers[0].iface));
1746
1747 ptr = dira->iface + strlen(dira->iface) - 1;
1748 if (*ptr == ')')
1749 *ptr = '\0';
1750
1751 NumBrowsers ++;
1752 }
1753 else if (get_address(value, INADDR_NONE, BrowsePort, AF_INET, &(dira->to)))
1754 {
1755 httpAddrString(&(dira->to), temp, sizeof(temp));
1756
1757 #ifdef AF_INET6
1758 if (dira->to.addr.sa_family == AF_INET6)
1759 LogMessage(L_INFO, "Sending browsing info to %s:%d (IPv6)", temp,
1760 ntohs(dira->to.ipv6.sin6_port));
1761 else
1762 #endif /* AF_INET6 */
1763 LogMessage(L_INFO, "Sending browsing info to %s:%d", temp,
1764 ntohs(dira->to.ipv4.sin_port));
1765
1766 NumBrowsers ++;
1767 }
1768 else
1769 LogMessage(L_ERROR, "Bad BrowseAddress %s at line %d.", value,
1770 linenum);
1771 }
1772 else if (!strcasecmp(line, "BrowseOrder"))
1773 {
1774 /*
1775 * "BrowseOrder Deny,Allow" or "BrowseOrder Allow,Deny"...
1776 */
1777
1778 if ((location = FindLocation("CUPS_INTERNAL_BROWSE_ACL")) == NULL)
1779 location = AddLocation("CUPS_INTERNAL_BROWSE_ACL");
1780
1781 if (location == NULL)
1782 LogMessage(L_ERROR, "Unable to initialize browse access control list!");
1783 else if (!strncasecmp(value, "deny", 4))
1784 location->order_type = AUTH_ALLOW;
1785 else if (!strncasecmp(value, "allow", 5))
1786 location->order_type = AUTH_DENY;
1787 else
1788 LogMessage(L_ERROR, "Unknown BrowseOrder value %s on line %d.",
1789 value, linenum);
1790 }
1791 else if (!strcasecmp(line, "BrowseProtocols"))
1792 {
1793 /*
1794 * "BrowseProtocol name [... name]"
1795 */
1796
1797 BrowseProtocols = 0;
1798
1799 for (; *value;)
1800 {
1801 for (valuelen = 0; value[valuelen]; valuelen ++)
1802 if (isspace(value[valuelen]) || value[valuelen] == ',')
1803 break;
1804
1805 if (value[valuelen])
1806 {
1807 value[valuelen] = '\0';
1808 valuelen ++;
1809 }
1810
1811 if (!strcasecmp(value, "cups"))
1812 BrowseProtocols |= BROWSE_CUPS;
1813 else if (!strcasecmp(value, "slp"))
1814 BrowseProtocols |= BROWSE_SLP;
1815 else if (!strcasecmp(value, "ldap"))
1816 BrowseProtocols |= BROWSE_LDAP;
1817 else if (!strcasecmp(value, "all"))
1818 BrowseProtocols |= BROWSE_ALL;
1819 else
1820 {
1821 LogMessage(L_ERROR, "Unknown browse protocol \"%s\" on line %d.",
1822 value, linenum);
1823 break;
1824 }
1825
1826 for (value += valuelen; *value; value ++)
1827 if (!isspace(*value) || *value != ',')
1828 break;
1829 }
1830 }
1831 else if (!strcasecmp(line, "BrowseAllow") ||
1832 !strcasecmp(line, "BrowseDeny"))
1833 {
1834 /*
1835 * BrowseAllow [From] host/ip...
1836 * BrowseDeny [From] host/ip...
1837 */
1838
1839 if ((location = FindLocation("CUPS_INTERNAL_BROWSE_ACL")) == NULL)
1840 location = AddLocation("CUPS_INTERNAL_BROWSE_ACL");
1841
1842 if (location == NULL)
1843 LogMessage(L_ERROR, "Unable to initialize browse access control list!");
1844 else
1845 {
1846 if (!strncasecmp(value, "from ", 5))
1847 {
1848 /*
1849 * Strip leading "from"...
1850 */
1851
1852 value += 5;
1853
1854 while (isspace(*value))
1855 value ++;
1856 }
1857
1858 /*
1859 * Figure out what form the allow/deny address takes:
1860 *
1861 * All
1862 * None
1863 * *.domain.com
1864 * .domain.com
1865 * host.domain.com
1866 * nnn.*
1867 * nnn.nnn.*
1868 * nnn.nnn.nnn.*
1869 * nnn.nnn.nnn.nnn
1870 * nnn.nnn.nnn.nnn/mm
1871 * nnn.nnn.nnn.nnn/mmm.mmm.mmm.mmm
1872 */
1873
1874 if (!strcasecmp(value, "all"))
1875 {
1876 /*
1877 * All hosts...
1878 */
1879
1880 if (!strcasecmp(line, "BrowseAllow"))
1881 AllowIP(location, zeros, zeros);
1882 else
1883 DenyIP(location, zeros, zeros);
1884 }
1885 else if (!strcasecmp(value, "none"))
1886 {
1887 /*
1888 * No hosts...
1889 */
1890
1891 if (!strcasecmp(line, "BrowseAllow"))
1892 AllowIP(location, ones, zeros);
1893 else
1894 DenyIP(location, ones, zeros);
1895 }
1896 else if (value[0] == '*' || value[0] == '.' || !isdigit(value[0]))
1897 {
1898 /*
1899 * Host or domain name...
1900 */
1901
1902 if (value[0] == '*')
1903 value ++;
1904
1905 if (!strcasecmp(line, "BrowseAllow"))
1906 AllowHost(location, value);
1907 else
1908 DenyHost(location, value);
1909 }
1910 else
1911 {
1912 /*
1913 * One of many IP address forms...
1914 */
1915
1916 if (!get_addr_and_mask(value, ip, mask))
1917 {
1918 LogMessage(L_ERROR, "Bad netmask value %s on line %d.",
1919 value, linenum);
1920 break;
1921 }
1922
1923 if (!strcasecmp(line, "BrowseAllow"))
1924 AllowIP(location, ip, mask);
1925 else
1926 DenyIP(location, ip, mask);
1927 }
1928 }
1929 }
1930 else if (!strcasecmp(line, "BrowseRelay"))
1931 {
1932 /*
1933 * BrowseRelay [from] source [to] destination
1934 */
1935
1936 if (NumRelays == 0)
1937 relay = malloc(sizeof(dirsvc_relay_t));
1938 else
1939 relay = realloc(Relays, (NumRelays + 1) * sizeof(dirsvc_relay_t));
1940
1941 if (!relay)
1942 {
1943 LogMessage(L_ERROR, "Unable to allocate BrowseRelay at line %d - %s.",
1944 linenum, strerror(errno));
1945 continue;
1946 }
1947
1948 Relays = relay;
1949 relay += NumRelays;
1950
1951 memset(relay, 0, sizeof(dirsvc_relay_t));
1952
1953 if (!strncasecmp(value, "from ", 5))
1954 {
1955 /*
1956 * Strip leading "from"...
1957 */
1958
1959 value += 5;
1960
1961 while (isspace(*value))
1962 value ++;
1963 }
1964
1965 /*
1966 * Figure out what form the from address takes:
1967 *
1968 * *.domain.com
1969 * .domain.com
1970 * host.domain.com
1971 * nnn.*
1972 * nnn.nnn.*
1973 * nnn.nnn.nnn.*
1974 * nnn.nnn.nnn.nnn
1975 * nnn.nnn.nnn.nnn/mm
1976 * nnn.nnn.nnn.nnn/mmm.mmm.mmm.mmm
1977 */
1978
1979 if (value[0] == '*' || value[0] == '.' || !isdigit(value[0]))
1980 {
1981 /*
1982 * Host or domain name...
1983 */
1984
1985 if (value[0] == '*')
1986 value ++;
1987
1988 strlcpy(temp, value, sizeof(temp));
1989 if ((ptr = strchr(temp, ' ')) != NULL)
1990 *ptr = '\0';
1991
1992 relay->from.type = AUTH_NAME;
1993 relay->from.mask.name.name = strdup(temp);
1994 relay->from.mask.name.length = strlen(temp);
1995 }
1996 else
1997 {
1998 /*
1999 * One of many IP address forms...
2000 */
2001
2002 if (!get_addr_and_mask(value, ip, mask))
2003 {
2004 LogMessage(L_ERROR, "Bad netmask value %s on line %d.",
2005 value, linenum);
2006 break;
2007 }
2008
2009 relay->from.type = AUTH_IP;
2010 memcpy(relay->from.mask.ip.address, ip,
2011 sizeof(relay->from.mask.ip.address));
2012 memcpy(relay->from.mask.ip.netmask, mask,
2013 sizeof(relay->from.mask.ip.netmask));
2014 }
2015
2016 /*
2017 * Skip value and trailing whitespace...
2018 */
2019
2020 for (; *value; value ++)
2021 if (isspace(*value))
2022 break;
2023
2024 while (isspace(*value))
2025 value ++;
2026
2027 if (!strncasecmp(value, "to ", 3))
2028 {
2029 /*
2030 * Strip leading "to"...
2031 */
2032
2033 value += 3;
2034
2035 while (isspace(*value))
2036 value ++;
2037 }
2038
2039 /*
2040 * Get "to" address and port...
2041 */
2042
2043 if (get_address(value, INADDR_BROADCAST, BrowsePort, AF_INET, &(relay->to)))
2044 {
2045 httpAddrString(&(relay->to), temp, sizeof(temp));
2046
2047 if (relay->from.type == AUTH_IP)
2048 snprintf(temp2, sizeof(temp2), "%u.%u.%u.%u/%u.%u.%u.%u",
2049 relay->from.mask.ip.address[0],
2050 relay->from.mask.ip.address[1],
2051 relay->from.mask.ip.address[2],
2052 relay->from.mask.ip.address[3],
2053 relay->from.mask.ip.netmask[0],
2054 relay->from.mask.ip.netmask[1],
2055 relay->from.mask.ip.netmask[2],
2056 relay->from.mask.ip.netmask[3]);
2057 else
2058 strlcpy(temp2, relay->from.mask.name.name, sizeof(temp2));
2059
2060 #ifdef AF_INET6
2061 if (relay->to.addr.sa_family == AF_INET6)
2062 LogMessage(L_INFO, "Relaying from %s to %s:%d", temp, temp2,
2063 ntohs(relay->to.ipv6.sin6_port));
2064 else
2065 #endif /* AF_INET6 */
2066 LogMessage(L_INFO, "Relaying from %s to %s:%d", temp, temp2,
2067 ntohs(relay->to.ipv4.sin_port));
2068
2069 NumRelays ++;
2070 }
2071 else
2072 {
2073 if (relay->from.type == AUTH_NAME)
2074 free(relay->from.mask.name.name);
2075
2076 LogMessage(L_ERROR, "Bad relay address %s at line %d.", value, linenum);
2077 }
2078 }
2079 else if (!strcasecmp(line, "BrowsePoll"))
2080 {
2081 /*
2082 * BrowsePoll address[:port]
2083 */
2084
2085 if (NumPolled == 0)
2086 poll = malloc(sizeof(dirsvc_poll_t));
2087 else
2088 poll = realloc(Polled, (NumPolled + 1) * sizeof(dirsvc_poll_t));
2089
2090 if (!poll)
2091 {
2092 LogMessage(L_ERROR, "Unable to allocate BrowsePoll at line %d - %s.",
2093 linenum, strerror(errno));
2094 continue;
2095 }
2096
2097 Polled = poll;
2098 poll += NumPolled;
2099
2100 /*
2101 * Get poll address and port...
2102 */
2103
2104 if (get_address(value, INADDR_NONE, ippPort(), AF_INET, &polladdr))
2105 {
2106 NumPolled ++;
2107 memset(poll, 0, sizeof(dirsvc_poll_t));
2108
2109 httpAddrString(&polladdr, poll->hostname, sizeof(poll->hostname));
2110
2111 #ifdef AF_INET6
2112 if (polladdr.addr.sa_family == AF_INET6)
2113 poll->port = ntohs(polladdr.ipv6.sin6_port);
2114 else
2115 #endif /* AF_INET6 */
2116 poll->port = ntohs(polladdr.ipv4.sin_port);
2117
2118 LogMessage(L_INFO, "Polling %s:%d", poll->hostname, poll->port);
2119 }
2120 else
2121 LogMessage(L_ERROR, "Bad poll address %s at line %d.", value, linenum);
2122 }
2123 else if (!strcasecmp(line, "User"))
2124 {
2125 /*
2126 * User ID to run as...
2127 */
2128
2129 if (isdigit(value[0]))
2130 User = atoi(value);
2131 else
2132 {
2133 struct passwd *p; /* Password information */
2134
2135 endpwent();
2136 p = getpwnam(value);
2137
2138 if (p != NULL)
2139 User = p->pw_uid;
2140 else
2141 LogMessage(L_WARN, "Unknown username \"%s\"", value);
2142 }
2143 }
2144 else if (!strcasecmp(line, "Group"))
2145 {
2146 /*
2147 * Group ID to run as...
2148 */
2149
2150 if (isdigit(value[0]))
2151 Group = atoi(value);
2152 else
2153 {
2154 struct group *g; /* Group information */
2155
2156 endgrent();
2157 g = getgrnam(value);
2158
2159 if (g != NULL)
2160 Group = g->gr_gid;
2161 else
2162 LogMessage(L_WARN, "Unknown groupname \"%s\"", value);
2163 }
2164 }
2165 else if (!strcasecmp(line, "SystemGroup"))
2166 {
2167 /*
2168 * System (admin) group(s)...
2169 */
2170
2171 char *valueptr, /* Pointer into value */
2172 quote; /* Quote character */
2173
2174
2175 for (i = NumSystemGroups; *value && i < MAX_SYSTEM_GROUPS; i ++)
2176 {
2177 if (*value == '\'' || *value == '\"')
2178 {
2179 /*
2180 * Scan quoted name...
2181 */
2182
2183 quote = *value++;
2184
2185 for (valueptr = value; *valueptr; valueptr ++)
2186 if (*valueptr == quote)
2187 break;
2188 }
2189 else
2190 {
2191 /*
2192 * Scan space or comma-delimited name...
2193 */
2194
2195 for (valueptr = value; *valueptr; valueptr ++)
2196 if (isspace(*valueptr) || *valueptr == ',')
2197 break;
2198 }
2199
2200 if (*valueptr)
2201 *valueptr++ = '\0';
2202
2203 SetString(SystemGroups + i, value);
2204
2205 value = valueptr;
2206
2207 while (*value == ',' || isspace(*value))
2208 value ++;
2209 }
2210
2211 if (i)
2212 NumSystemGroups = i;
2213 }
2214 else if (!strcasecmp(line, "HostNameLookups"))
2215 {
2216 /*
2217 * Do hostname lookups?
2218 */
2219
2220 if (!strcasecmp(value, "off"))
2221 HostNameLookups = 0;
2222 else if (!strcasecmp(value, "on"))
2223 HostNameLookups = 1;
2224 else if (!strcasecmp(value, "double"))
2225 HostNameLookups = 2;
2226 else
2227 LogMessage(L_WARN, "Unknown HostNameLookups %s on line %d.",
2228 value, linenum);
2229 }
2230 else if (!strcasecmp(line, "LogLevel"))
2231 {
2232 /*
2233 * Amount of logging to do...
2234 */
2235
2236 if (!strcasecmp(value, "debug2"))
2237 LogLevel = L_DEBUG2;
2238 else if (!strcasecmp(value, "debug"))
2239 LogLevel = L_DEBUG;
2240 else if (!strcasecmp(value, "info"))
2241 LogLevel = L_INFO;
2242 else if (!strcasecmp(value, "notice"))
2243 LogLevel = L_NOTICE;
2244 else if (!strcasecmp(value, "warn"))
2245 LogLevel = L_WARN;
2246 else if (!strcasecmp(value, "error"))
2247 LogLevel = L_ERROR;
2248 else if (!strcasecmp(value, "crit"))
2249 LogLevel = L_CRIT;
2250 else if (!strcasecmp(value, "alert"))
2251 LogLevel = L_ALERT;
2252 else if (!strcasecmp(value, "emerg"))
2253 LogLevel = L_EMERG;
2254 else if (!strcasecmp(value, "none"))
2255 LogLevel = L_NONE;
2256 else
2257 LogMessage(L_WARN, "Unknown LogLevel %s on line %d.", value, linenum);
2258 }
2259 else if (!strcasecmp(line, "PrintcapFormat"))
2260 {
2261 /*
2262 * Format of printcap file?
2263 */
2264
2265 if (!strcasecmp(value, "bsd"))
2266 PrintcapFormat = PRINTCAP_BSD;
2267 else if (!strcasecmp(value, "solaris"))
2268 PrintcapFormat = PRINTCAP_SOLARIS;
2269 else
2270 LogMessage(L_WARN, "Unknown PrintcapFormat %s on line %d.",
2271 value, linenum);
2272 }
2273 else if (!strcasecmp(line, "ServerTokens"))
2274 {
2275 /*
2276 * Set the string used for the Server header...
2277 */
2278
2279 struct utsname plat; /* Platform info */
2280
2281
2282 uname(&plat);
2283
2284 if (!strcasecmp(value, "ProductOnly"))
2285 SetString(&ServerHeader, "CUPS");
2286 else if (!strcasecmp(value, "Major"))
2287 SetString(&ServerHeader, "CUPS/1");
2288 else if (!strcasecmp(value, "Minor"))
2289 SetString(&ServerHeader, "CUPS/1.1");
2290 else if (!strcasecmp(value, "Minimal"))
2291 SetString(&ServerHeader, CUPS_MINIMAL);
2292 else if (!strcasecmp(value, "OS"))
2293 SetStringf(&ServerHeader, CUPS_MINIMAL " (%s)", plat.sysname);
2294 else if (!strcasecmp(value, "Full"))
2295 SetStringf(&ServerHeader, CUPS_MINIMAL " (%s) IPP/1.1", plat.sysname);
2296 else if (!strcasecmp(value, "None"))
2297 ClearString(&ServerHeader);
2298 else
2299 LogMessage(L_WARN, "Unknown ServerTokens %s on line %d.", value, linenum);
2300 }
2301 else
2302 {
2303 /*
2304 * Find a simple variable in the list...
2305 */
2306
2307 for (i = NUM_VARS, var = variables; i > 0; i --, var ++)
2308 if (!strcasecmp(line, var->name))
2309 break;
2310
2311 if (i == 0)
2312 {
2313 /*
2314 * Unknown directive! Output an error message and continue...
2315 */
2316
2317 LogMessage(L_ERROR, "Unknown directive %s on line %d.", line,
2318 linenum);
2319 continue;
2320 }
2321
2322 switch (var->type)
2323 {
2324 case VAR_INTEGER :
2325 {
2326 int n; /* Number */
2327 char *units; /* Units */
2328
2329
2330 n = strtol(value, &units, 0);
2331
2332 if (units && *units)
2333 {
2334 if (tolower(units[0] & 255) == 'g')
2335 n *= 1024 * 1024 * 1024;
2336 else if (tolower(units[0] & 255) == 'm')
2337 n *= 1024 * 1024;
2338 else if (tolower(units[0] & 255) == 'k')
2339 n *= 1024;
2340 else if (tolower(units[0] & 255) == 't')
2341 n *= 262144;
2342 }
2343
2344 *((int *)var->ptr) = n;
2345 }
2346 break;
2347
2348 case VAR_BOOLEAN :
2349 if (!strcasecmp(value, "true") ||
2350 !strcasecmp(value, "on") ||
2351 !strcasecmp(value, "enabled") ||
2352 !strcasecmp(value, "yes") ||
2353 atoi(value) != 0)
2354 *((int *)var->ptr) = TRUE;
2355 else if (!strcasecmp(value, "false") ||
2356 !strcasecmp(value, "off") ||
2357 !strcasecmp(value, "disabled") ||
2358 !strcasecmp(value, "no") ||
2359 !strcasecmp(value, "0"))
2360 *((int *)var->ptr) = FALSE;
2361 else
2362 LogMessage(L_ERROR, "Unknown boolean value %s on line %d.",
2363 value, linenum);
2364 break;
2365
2366 case VAR_STRING :
2367 SetString((char **)var->ptr, value);
2368 break;
2369 }
2370 }
2371 }
2372
2373 return (1);
2374 }
2375
2376
2377 /*
2378 * 'read_location()' - Read a <Location path> definition.
2379 */
2380
2381 static int /* O - New line number or 0 on error */
2382 read_location(cups_file_t *fp, /* I - Configuration file */
2383 char *location, /* I - Location name/path */
2384 int linenum) /* I - Current line number */
2385 {
2386 location_t *loc, /* New location */
2387 *parent; /* Parent location */
2388 char line[HTTP_MAX_BUFFER], /* Line buffer */
2389 *value, /* Value for directive */
2390 *valptr; /* Pointer into value */
2391
2392
2393 if ((parent = AddLocation(location)) == NULL)
2394 return (0);
2395
2396 parent->limit = AUTH_LIMIT_ALL;
2397 loc = parent;
2398
2399 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
2400 {
2401 /*
2402 * Decode the directive...
2403 */
2404
2405 if (!strcasecmp(line, "</Location>"))
2406 return (linenum);
2407 else if (!strcasecmp(line, "<Limit") ||
2408 !strcasecmp(line, "<LimitExcept"))
2409 {
2410 if (!value)
2411 {
2412 LogMessage(L_ERROR, "Syntax error on line %d.", linenum);
2413 return (0);
2414 }
2415
2416 if ((loc = CopyLocation(&parent)) == NULL)
2417 return (0);
2418
2419 loc->limit = 0;
2420 while (*value)
2421 {
2422 for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
2423
2424 if (*valptr)
2425 *valptr++ = '\0';
2426
2427 if (!strcmp(value, "ALL"))
2428 loc->limit = AUTH_LIMIT_ALL;
2429 else if (!strcmp(value, "GET"))
2430 loc->limit |= AUTH_LIMIT_GET;
2431 else if (!strcmp(value, "HEAD"))
2432 loc->limit |= AUTH_LIMIT_HEAD;
2433 else if (!strcmp(value, "OPTIONS"))
2434 loc->limit |= AUTH_LIMIT_OPTIONS;
2435 else if (!strcmp(value, "POST"))
2436 loc->limit |= AUTH_LIMIT_POST;
2437 else if (!strcmp(value, "PUT"))
2438 loc->limit |= AUTH_LIMIT_PUT;
2439 else if (!strcmp(value, "TRACE"))
2440 loc->limit |= AUTH_LIMIT_TRACE;
2441 else
2442 LogMessage(L_WARN, "Unknown request type %s on line %d!", value,
2443 linenum);
2444
2445 for (value = valptr; isspace(*value & 255); value ++);
2446 }
2447
2448 if (!strcasecmp(line, "<LimitExcept"))
2449 loc->limit = AUTH_LIMIT_ALL ^ loc->limit;
2450
2451 parent->limit &= ~loc->limit;
2452 }
2453 else if (!strcasecmp(line, "</Limit>"))
2454 loc = parent;
2455 else if (!parse_aaa(loc, line, value, linenum))
2456 {
2457 LogMessage(L_ERROR, "Unknown Location directive %s on line %d.",
2458 line, linenum);
2459 return (0);
2460 }
2461 }
2462
2463 LogMessage(L_ERROR, "Unexpected end-of-file at line %d while reading location!",
2464 linenum);
2465
2466 return (0);
2467 }
2468
2469
2470 /*
2471 * 'read_policy()' - Read a <Policy name> definition.
2472 */
2473
2474 static int /* O - New line number or 0 on error */
2475 read_policy(cups_file_t *fp, /* I - Configuration file */
2476 char *policy, /* I - Location name/path */
2477 int linenum) /* I - Current line number */
2478 {
2479 int i; /* Looping var */
2480 cupsd_policy_t *pol; /* Policy */
2481 location_t *op; /* Policy operation */
2482 int num_ops; /* Number of IPP operations */
2483 ipp_op_t ops[100]; /* Operations */
2484 char line[HTTP_MAX_BUFFER],
2485 /* Line buffer */
2486 *value, /* Value for directive */
2487 *valptr; /* Pointer into value */
2488
2489
2490 /*
2491 * Create the policy...
2492 */
2493
2494 if ((pol = cupsdAddPolicy(policy)) == NULL)
2495 return (0);
2496
2497 /*
2498 * Read from the file...
2499 */
2500
2501 op = NULL;
2502 num_ops = 0;
2503
2504 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
2505 {
2506 /*
2507 * Decode the directive...
2508 */
2509
2510 if (!strcasecmp(line, "</Policy>"))
2511 {
2512 if (op)
2513 LogMessage(L_WARN, "Missing </Limit> before </Policy> on line %d!",
2514 linenum);
2515
2516 return (linenum);
2517 }
2518 else if (!strcasecmp(line, "<Limit") && !op)
2519 {
2520 if (!value)
2521 {
2522 LogMessage(L_ERROR, "Syntax error on line %d.", linenum);
2523 return (0);
2524 }
2525
2526 /*
2527 * Scan for IPP operation names...
2528 */
2529
2530 num_ops = 0;
2531
2532 while (*value)
2533 {
2534 for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
2535
2536 if (*valptr)
2537 *valptr++ = '\0';
2538
2539 if (num_ops < (int)(sizeof(ops) / sizeof(ops[0])))
2540 {
2541 if (!strcasecmp(value, "All"))
2542 ops[num_ops] = IPP_ANY_OPERATION;
2543 else if ((ops[num_ops] = ippOpValue(value)) == IPP_BAD_OPERATION)
2544 LogMessage(L_ERROR, "Bad IPP operation name \"%s\" on line %d!",
2545 value, linenum);
2546 else
2547 num_ops ++;
2548 }
2549 else
2550 LogMessage(L_ERROR, "Too many operations listed on line %d!",
2551 linenum);
2552
2553 for (value = valptr; isspace(*value & 255); value ++);
2554 }
2555
2556 /*
2557 * If none are specified, apply the policy to all operations...
2558 */
2559
2560 if (num_ops == 0)
2561 {
2562 ops[0] = IPP_ANY_OPERATION;
2563 num_ops = 1;
2564 }
2565
2566 /*
2567 * Add a new policy for the first operation...
2568 */
2569
2570 op = cupsdAddPolicyOp(pol, NULL, ops[0]);
2571 }
2572 else if (!strcasecmp(line, "</Limit>") && op)
2573 {
2574 /*
2575 * Finish the current operation limit...
2576 */
2577
2578 if (num_ops > 1)
2579 {
2580 /*
2581 * Copy the policy to the other operations...
2582 */
2583
2584 for (i = 1; i < num_ops; i ++)
2585 cupsdAddPolicyOp(pol, op, ops[i]);
2586 }
2587
2588 op = NULL;
2589 }
2590 else if (!op)
2591 {
2592 LogMessage(L_ERROR, "Missing <Limit ops> directive before %s on line %d.",
2593 line, linenum);
2594 return (0);
2595 }
2596 else if (!parse_aaa(op, line, value, linenum))
2597 {
2598 if (op)
2599 LogMessage(L_ERROR, "Unknown Policy Limit directive %s on line %d.",
2600 line, linenum);
2601 else
2602 LogMessage(L_ERROR, "Unknown Policy directive %s on line %d.",
2603 line, linenum);
2604
2605 return (0);
2606 }
2607 }
2608
2609 LogMessage(L_ERROR, "Unexpected end-of-file at line %d while reading policy \"%s\"!",
2610 linenum, policy);
2611
2612 return (0);
2613 }
2614
2615
2616 #ifdef HAVE_CDSASSL
2617 /*
2618 * 'CDSAGetServerCerts()' - Convert a keychain name into the CFArrayRef
2619 * required by SSLSetCertificate.
2620 *
2621 * For now we assumes that there is exactly one SecIdentity in the
2622 * keychain - i.e. there is exactly one matching cert/private key pair.
2623 * In the future we will search a keychain for a SecIdentity matching a
2624 * specific criteria. We also skip the operation of adding additional
2625 * non-signing certs from the keychain to the CFArrayRef.
2626 *
2627 * To create a self-signed certificate for testing use the certtool.
2628 * Executing the following as root will do it:
2629 *
2630 * certtool c c v k=CUPS
2631 */
2632
2633 CFArrayRef
2634 CDSAGetServerCerts(void)
2635 {
2636 OSStatus err; /* Error info */
2637 SecKeychainRef kcRef; /* Keychain reference */
2638 SecIdentitySearchRef srchRef; /* Search reference */
2639 SecIdentityRef identity; /* Identity */
2640 CFArrayRef ca; /* Certificate array */
2641
2642
2643 kcRef = NULL;
2644 srchRef = NULL;
2645 identity = NULL;
2646 ca = NULL;
2647 err = SecKeychainOpen(ServerCertificate, &kcRef);
2648
2649 if (err)
2650 LogMessage(L_ERROR, "Cannot open keychain \"%s\", error %d.",
2651 ServerCertificate, err);
2652 else
2653 {
2654 /*
2655 * Search for "any" identity matching specified key use;
2656 * in this app, we expect there to be exactly one.
2657 */
2658
2659 err = SecIdentitySearchCreate(kcRef, CSSM_KEYUSE_SIGN, &srchRef);
2660
2661 if (err)
2662 LogMessage(L_ERROR,
2663 "Cannot find signing key in keychain \"%s\", error %d",
2664 ServerCertificate, err);
2665 else
2666 {
2667 err = SecIdentitySearchCopyNext(srchRef, &identity);
2668
2669 if (err)
2670 LogMessage(L_ERROR,
2671 "Cannot find signing key in keychain \"%s\", error %d",
2672 ServerCertificate, err);
2673 else
2674 {
2675 if (CFGetTypeID(identity) != SecIdentityGetTypeID())
2676 LogMessage(L_ERROR, "SecIdentitySearchCopyNext CFTypeID failure!");
2677 else
2678 {
2679 /*
2680 * Found one. Place it in a CFArray.
2681 * TBD: snag other (non-identity) certs from keychain and add them
2682 * to array as well.
2683 */
2684
2685 ca = CFArrayCreate(NULL, (const void **)&identity, 1, NULL);
2686
2687 if (ca == nil)
2688 LogMessage(L_ERROR, "CFArrayCreate error");
2689 }
2690
2691 /*CFRelease(identity);*/
2692 }
2693
2694 /*CFRelease(srchRef);*/
2695 }
2696
2697 /*CFRelease(kcRef);*/
2698 }
2699
2700 return ca;
2701 }
2702 #endif /* HAVE_CDSASSL */
2703
2704
2705 /*
2706 * End of "$Id$".
2707 */