]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/conf.c
23358a0d7b4a23cdf302a28987b979852b306c70
[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 SetString(&ServerName, httpGetHostname(temp, sizeof(temp)));
280 SetStringf(&ServerAdmin, "root@%s", temp);
281 SetString(&ServerBin, CUPS_SERVERBIN);
282 SetString(&RequestRoot, CUPS_REQUESTS);
283 SetString(&CacheDir, CUPS_CACHEDIR);
284 SetString(&DataDir, CUPS_DATADIR);
285 SetString(&DocumentRoot, CUPS_DOCROOT);
286 SetString(&AccessLog, CUPS_LOGDIR "/access_log");
287 SetString(&ErrorLog, CUPS_LOGDIR "/error_log");
288 SetString(&PageLog, CUPS_LOGDIR "/page_log");
289 SetString(&Printcap, "/etc/printcap");
290 SetString(&PrintcapGUI, "/usr/bin/glpoptions");
291 SetString(&FontPath, CUPS_FONTPATH);
292 SetString(&RemoteRoot, "remroot");
293 SetString(&ServerHeader, "CUPS/1.1");
294 SetString(&StateDir, CUPS_STATEDIR);
295
296 strlcpy(temp, ConfigurationFile, sizeof(temp));
297 if ((slash = strrchr(temp, '/')) != NULL)
298 *slash = '\0';
299
300 SetString(&ServerRoot, temp);
301
302 ClearString(&Classification);
303 ClassifyOverride = 0;
304
305 #ifdef HAVE_SSL
306 # ifdef HAVE_CDSASSL
307 SetString(&ServerCertificate, "/var/root/Library/Keychains/CUPS");
308 # else
309 SetString(&ServerCertificate, "ssl/server.crt");
310 SetString(&ServerKey, "ssl/server.key");
311 # endif /* HAVE_CDSASSL */
312 #endif /* HAVE_SSL */
313
314 if ((language = DEFAULT_LANGUAGE) == NULL)
315 language = "en";
316 else if (strcmp(language, "C") == 0 || strcmp(language, "POSIX") == 0)
317 language = "en";
318
319 SetString(&DefaultLanguage, language);
320 SetString(&DefaultCharset, DEFAULT_CHARSET);
321
322 SetString(&RIPCache, "8m");
323
324 if (getenv("TMPDIR") == NULL)
325 SetString(&TempDir, CUPS_REQUESTS "/tmp");
326 else
327 SetString(&TempDir, getenv("TMPDIR"));
328
329 /*
330 * Find the default system group: "sys", "system", or "root"...
331 */
332
333 group = getgrnam(CUPS_DEFAULT_GROUP);
334 endgrent();
335
336 NumSystemGroups = 0;
337
338 if (group != NULL)
339 {
340 SetString(&SystemGroups[0], CUPS_DEFAULT_GROUP);
341 Group = group->gr_gid;
342 }
343 else
344 {
345 group = getgrgid(0);
346 endgrent();
347
348 if (group != NULL)
349 {
350 SetString(&SystemGroups[0], group->gr_name);
351 Group = 0;
352 }
353 else
354 {
355 SetString(&SystemGroups[0], "unknown");
356 Group = 0;
357 }
358 }
359
360 /*
361 * Find the default user...
362 */
363
364 if ((user = getpwnam(CUPS_DEFAULT_USER)) == NULL)
365 User = 1; /* Force to a non-priviledged account */
366 else
367 User = user->pw_uid;
368
369 endpwent();
370
371 /*
372 * Numeric options...
373 */
374
375 ConfigFilePerm = 0640;
376 DefaultAuthType = AUTH_BASIC;
377 FaxRetryLimit = 5;
378 FaxRetryInterval = 300;
379 FileDevice = FALSE;
380 FilterLevel = 0;
381 FilterLimit = 0;
382 FilterNice = 0;
383 HostNameLookups = FALSE;
384 ImplicitClasses = TRUE;
385 ImplicitAnyClasses = FALSE;
386 HideImplicitMembers = TRUE;
387 KeepAlive = TRUE;
388 KeepAliveTimeout = DEFAULT_KEEPALIVE;
389 ListenBackLog = SOMAXCONN;
390 LogFilePerm = 0644;
391 LogLevel = L_ERROR;
392 MaxClients = 100;
393 MaxClientsPerHost = 0;
394 MaxLogSize = 1024 * 1024;
395 MaxPrinterHistory = 10;
396 MaxRequestSize = 0;
397 ReloadTimeout = 60;
398 RootCertDuration = 300;
399 RunAsUser = FALSE;
400 Timeout = DEFAULT_TIMEOUT;
401
402 BrowseInterval = DEFAULT_INTERVAL;
403 BrowsePort = ippPort();
404 BrowseLocalProtocols = BROWSE_CUPS;
405 BrowseRemoteProtocols = 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") ||
462 !strcmp(ErrorLog, "syslog") ||
463 !strcmp(PageLog, "syslog"))
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 Deny,Allow");
736
737 po = cupsdAddPolicyOp(p, NULL, IPP_SEND_DOCUMENT);
738 po->order_type = AUTH_ALLOW;
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 "
768 "Deactivate-Printer Activate-Printer Restart-Printer "
769 "Shutdown-Printer Startup-Printer Promote-Job "
770 "Schedule-Job-After CUPS-Add-Printer "
771 "CUPS-Delete-Printer CUPS-Add-Class CUPS-Delete-Class "
772 "CUPS-Accept-Jobs CUPS-Reject-Jobs CUPS-Set-Default>");
773 LogMessage(L_INFO, "Order Deny,Allow");
774 LogMessage(L_INFO, "AuthType Basic");
775
776 po = cupsdAddPolicyOp(p, NULL, IPP_PAUSE_PRINTER);
777 po->order_type = AUTH_ALLOW;
778 po->type = AUTH_BASIC;
779 po->level = AUTH_USER;
780
781 AddName(po, "@SYSTEM");
782 LogMessage(L_INFO, "Require user @SYSTEM");
783
784 cupsdAddPolicyOp(p, po, IPP_RESUME_PRINTER);
785 cupsdAddPolicyOp(p, po, IPP_SET_PRINTER_ATTRIBUTES);
786 cupsdAddPolicyOp(p, po, IPP_ENABLE_PRINTER);
787 cupsdAddPolicyOp(p, po, IPP_DISABLE_PRINTER);
788 cupsdAddPolicyOp(p, po, IPP_PAUSE_PRINTER_AFTER_CURRENT_JOB);
789 cupsdAddPolicyOp(p, po, IPP_HOLD_NEW_JOBS);
790 cupsdAddPolicyOp(p, po, IPP_RELEASE_HELD_NEW_JOBS);
791 cupsdAddPolicyOp(p, po, IPP_DEACTIVATE_PRINTER);
792 cupsdAddPolicyOp(p, po, IPP_ACTIVATE_PRINTER);
793 cupsdAddPolicyOp(p, po, IPP_RESTART_PRINTER);
794 cupsdAddPolicyOp(p, po, IPP_SHUTDOWN_PRINTER);
795 cupsdAddPolicyOp(p, po, IPP_STARTUP_PRINTER);
796 cupsdAddPolicyOp(p, po, IPP_PROMOTE_JOB);
797 cupsdAddPolicyOp(p, po, IPP_SCHEDULE_JOB_AFTER);
798 cupsdAddPolicyOp(p, po, CUPS_ADD_PRINTER);
799 cupsdAddPolicyOp(p, po, CUPS_DELETE_PRINTER);
800 cupsdAddPolicyOp(p, po, CUPS_ADD_CLASS);
801 cupsdAddPolicyOp(p, po, CUPS_DELETE_CLASS);
802 cupsdAddPolicyOp(p, po, CUPS_ACCEPT_JOBS);
803 cupsdAddPolicyOp(p, po, CUPS_REJECT_JOBS);
804 cupsdAddPolicyOp(p, po, CUPS_SET_DEFAULT);
805
806 LogMessage(L_INFO, "</Limit>");
807
808 LogMessage(L_INFO, "<Limit All>");
809 LogMessage(L_INFO, "Order Deny,Allow");
810
811 po = cupsdAddPolicyOp(p, NULL, IPP_ANY_OPERATION);
812 po->order_type = AUTH_ALLOW;
813
814 LogMessage(L_INFO, "</Limit>");
815 LogMessage(L_INFO, "</Policy>");
816 }
817 }
818
819 /*
820 * If we are doing a full reload or the server root has changed, flush
821 * the jobs, printers, etc. and start from scratch...
822 */
823
824 if (NeedReload == RELOAD_ALL ||
825 !old_serverroot || !ServerRoot || strcmp(old_serverroot, ServerRoot) ||
826 !old_requestroot || !RequestRoot || strcmp(old_requestroot, RequestRoot))
827 {
828 LogMessage(L_INFO, "Full reload is required.");
829
830 /*
831 * Free all memory...
832 */
833
834 FreeAllJobs();
835 DeleteAllClasses();
836 DeleteAllPrinters();
837
838 DefaultPrinter = NULL;
839
840 if (MimeDatabase != NULL)
841 mimeDelete(MimeDatabase);
842
843 if (NumMimeTypes)
844 {
845 for (i = 0; i < NumMimeTypes; i ++)
846 free((void *)MimeTypes[i]);
847
848 free(MimeTypes);
849 }
850
851 /*
852 * Read the MIME type and conversion database...
853 */
854
855 snprintf(temp, sizeof(temp), "%s/filter", ServerBin);
856
857 MimeDatabase = mimeLoad(ServerRoot, temp);
858
859 LogMessage(L_INFO, "Loaded MIME database from \'%s\': %d types, %d filters...",
860 ServerRoot, MimeDatabase->num_types, MimeDatabase->num_filters);
861
862 /*
863 * Create a list of MIME types for the document-format-supported
864 * attribute...
865 */
866
867 NumMimeTypes = MimeDatabase->num_types;
868 if (!mimeType(MimeDatabase, "application", "octet-stream"))
869 NumMimeTypes ++;
870
871 MimeTypes = calloc(NumMimeTypes, sizeof(const char *));
872
873 for (i = 0; i < MimeDatabase->num_types; i ++)
874 {
875 snprintf(type, sizeof(type), "%s/%s", MimeDatabase->types[i]->super,
876 MimeDatabase->types[i]->type);
877
878 MimeTypes[i] = strdup(type);
879 }
880
881 if (i < NumMimeTypes)
882 MimeTypes[i] = strdup("application/octet-stream");
883
884 /*
885 * Load banners...
886 */
887
888 snprintf(temp, sizeof(temp), "%s/banners", DataDir);
889 LoadBanners(temp);
890
891 /*
892 * Load printers and classes...
893 */
894
895 LoadAllPrinters();
896 LoadAllClasses();
897
898 CreateCommonData();
899
900 /*
901 * Load queued jobs...
902 */
903
904 LoadAllJobs();
905
906 LogMessage(L_INFO, "Full reload complete.");
907 }
908 else
909 {
910 CreateCommonData();
911
912 LogMessage(L_INFO, "Partial reload complete.");
913 }
914
915 /*
916 * Reset the reload state...
917 */
918
919 NeedReload = RELOAD_NONE;
920
921 ClearString(&old_serverroot);
922 ClearString(&old_requestroot);
923
924 /*
925 * Startup the server and return...
926 */
927
928 StartServer();
929
930 return (1);
931 }
932
933
934 /*
935 * 'get_address()' - Get an address + port number from a line.
936 */
937
938 static int /* O - 1 if address good, 0 if bad */
939 get_address(const char *value, /* I - Value string */
940 unsigned defaddress, /* I - Default address */
941 int defport, /* I - Default port */
942 int deffamily, /* I - Default family */
943 http_addr_t *address) /* O - Socket address */
944 {
945 char hostname[256], /* Hostname or IP */
946 portname[256], /* Port number or name */
947 *ptr; /* Pointer into hostname string */
948 struct hostent *host; /* Host address */
949 struct servent *port; /* Port number */
950
951
952 /*
953 * Check for an empty value...
954 */
955
956 if (!*value)
957 {
958 LogMessage(L_ERROR, "Bad (empty) address!");
959 return (0);
960 }
961
962 /*
963 * Initialize the socket address to the defaults...
964 */
965
966 memset(address, 0, sizeof(http_addr_t));
967
968 #ifdef AF_INET6
969 if (deffamily == AF_INET6)
970 {
971 address->ipv6.sin6_family = AF_INET6;
972 address->ipv6.sin6_addr.s6_addr32[0] = htonl(defaddress);
973 address->ipv6.sin6_addr.s6_addr32[1] = htonl(defaddress);
974 address->ipv6.sin6_addr.s6_addr32[2] = htonl(defaddress);
975 address->ipv6.sin6_addr.s6_addr32[3] = htonl(defaddress);
976 address->ipv6.sin6_port = htons(defport);
977 }
978 else
979 #endif /* AF_INET6 */
980 {
981 address->ipv4.sin_family = AF_INET;
982 address->ipv4.sin_addr.s_addr = htonl(defaddress);
983 address->ipv4.sin_port = htons(defport);
984 }
985
986 #ifdef AF_LOCAL
987 /*
988 * If the address starts with a "/", it is a domain socket...
989 */
990
991 if (*value == '/')
992 {
993 if (strlen(value) >= sizeof(address->un.sun_path))
994 {
995 LogMessage(L_ERROR, "Domain socket name \"%s\" too long!", value);
996 return (0);
997 }
998
999 address->un.sun_family = AF_LOCAL;
1000 strcpy(address->un.sun_path, value);
1001 }
1002 else
1003 #endif /* AF_LOCAL */
1004
1005 /*
1006 * Try to grab a hostname and port number...
1007 */
1008
1009 strlcpy(hostname, value, sizeof(hostname));
1010
1011 if ((ptr = strrchr(hostname, ':')) != NULL)
1012 {
1013 /*
1014 * Copy hostname and port separately...
1015 */
1016
1017 *ptr++ = '\0';
1018
1019 strlcpy(portname, ptr, sizeof(portname));
1020 }
1021 else if (isdigit(value[0] & 255))
1022 {
1023 /*
1024 * Port number...
1025 */
1026
1027 hostname[0] = '\0';
1028 strlcpy(portname, value, sizeof(portname));
1029 }
1030 else
1031 {
1032 /*
1033 * Hostname by itself...
1034 */
1035
1036 portname[0] = '\0';
1037 }
1038
1039 /*
1040 * Decode the hostname and port number as needed...
1041 */
1042
1043 if (hostname[0] && strcmp(hostname, "*"))
1044 {
1045 if ((host = httpGetHostByName(hostname)) == NULL)
1046 {
1047 LogMessage(L_ERROR, "httpGetHostByName(\"%s\") failed - %s!", hostname,
1048 hstrerror(h_errno));
1049 return (0);
1050 }
1051
1052 httpAddrLoad(host, defport, 0, address);
1053 }
1054
1055 if (portname[0] != '\0')
1056 {
1057 if (isdigit(portname[0] & 255))
1058 {
1059 #ifdef AF_INET6
1060 if (address->addr.sa_family == AF_INET6)
1061 address->ipv6.sin6_port = htons(atoi(portname));
1062 else
1063 #endif /* AF_INET6 */
1064 address->ipv4.sin_port = htons(atoi(portname));
1065 }
1066 else
1067 {
1068 if ((port = getservbyname(portname, NULL)) == NULL)
1069 {
1070 LogMessage(L_ERROR, "getservbyname(\"%s\") failed - %s!", portname,
1071 strerror(errno));
1072 return (0);
1073 }
1074 else
1075 {
1076 #ifdef AF_INET6
1077 if (address->addr.sa_family == AF_INET6)
1078 address->ipv6.sin6_port = htons(port->s_port);
1079 else
1080 #endif /* AF_INET6 */
1081 address->ipv4.sin_port = htons(port->s_port);
1082 }
1083 }
1084 }
1085
1086 return (1);
1087 }
1088
1089
1090 /*
1091 * 'get_addr_and_mask()' - Get an IP address and netmask.
1092 */
1093
1094 static int /* O - 1 on success, 0 on failure */
1095 get_addr_and_mask(const char *value, /* I - String from config file */
1096 unsigned *ip, /* O - Address value */
1097 unsigned *mask) /* O - Mask value */
1098 {
1099 int i, /* Looping var */
1100 family, /* Address family */
1101 ipcount; /* Count of fields in address */
1102 const char *maskval, /* Pointer to start of mask value */
1103 *ptr; /* Pointer into value */
1104 static unsigned netmasks[4][4] = /* Standard netmasks... */
1105 {
1106 { 0xffffffff, 0x00000000, 0x00000000, 0x00000000 },
1107 { 0xffffffff, 0xffffffff, 0x00000000, 0x00000000 },
1108 { 0xffffffff, 0xffffffff, 0xffffffff, 0x00000000 },
1109 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
1110 };
1111
1112
1113 /*
1114 * Get the address...
1115 */
1116
1117 memset(ip, 0, sizeof(unsigned) * 4);
1118
1119 if ((maskval = strchr(value, '/')) != NULL)
1120 maskval ++;
1121 else
1122 maskval = value + strlen(value);
1123
1124 #ifdef AF_INET6
1125 /*
1126 * Check for an IPv6 address...
1127 */
1128
1129 if (*value == '[')
1130 {
1131 /*
1132 * Parse hexadecimal IPv6 address...
1133 */
1134
1135 family = AF_INET6;
1136
1137 for (i = 0, ptr = value + 1; *ptr && i < 4; i ++)
1138 {
1139 if (*ptr == ']')
1140 break;
1141 else if (*ptr == ':')
1142 ip[i] = 0;
1143 else
1144 ip[i] = strtoul(ptr, (char **)&ptr, 16);
1145
1146 if (*ptr == ':' || *ptr == ']')
1147 ptr ++;
1148 }
1149
1150 ipcount = i;
1151
1152 if (*ptr && *ptr != '/')
1153 return (0);
1154 }
1155 else
1156 #endif /* AF_INET6 */
1157 {
1158 /*
1159 * Parse dotted-decimal IPv4 address...
1160 */
1161
1162 family = AF_INET;
1163 ipcount = sscanf(value, "%u.%u.%u.%u", ip + 0, ip + 1, ip + 2, ip + 3);
1164
1165 ip[3] |= ((((ip[0] << 8) | ip[1]) << 8) | ip[2]) << 8;
1166 ip[0] = ip[1] = ip[2] = 0;
1167 }
1168
1169 if (*maskval)
1170 {
1171 /*
1172 * Get the netmask value(s)...
1173 */
1174
1175 memset(mask, 0, sizeof(unsigned) * 4);
1176
1177 #ifdef AF_INET6
1178 if (maskval[1] == '[')
1179 {
1180 /*
1181 * Get hexadecimal mask value...
1182 */
1183
1184 for (i = 0, ptr = maskval + 1; *ptr && i < 4; i ++)
1185 {
1186 if (*ptr == ']')
1187 break;
1188 else if (*ptr == ':')
1189 mask[i] = 0;
1190 else
1191 mask[i] = strtoul(ptr, (char **)&ptr, 16);
1192
1193 if (*ptr == ':' || *ptr == ']')
1194 ptr ++;
1195 }
1196
1197 if (*ptr)
1198 return (0);
1199 }
1200 else
1201 #endif /* AF_INET6 */
1202 if (strchr(maskval, '.'))
1203 {
1204 /*
1205 * Get dotted-decimal mask...
1206 */
1207
1208 if (sscanf(maskval, "%u.%u.%u.%u", mask + 0, mask + 1, mask + 2, mask + 3) != 4)
1209 return (0);
1210
1211 mask[3] |= ((((mask[0] << 8) | mask[1]) << 8) | mask[2]) << 8;
1212 mask[0] = mask[1] = mask[2] = 0;
1213 }
1214 else
1215 {
1216 /*
1217 * Get address/bits format...
1218 */
1219
1220 i = atoi(maskval);
1221
1222 #ifdef AF_INET6
1223 if (family == AF_INET6)
1224 {
1225 i = 128 - i;
1226
1227 if (i <= 96)
1228 mask[0] = 0xffffffff;
1229 else
1230 mask[0] = (0xffffffff << (i - 96)) & 0xffffffff;
1231
1232 if (i <= 64)
1233 mask[1] = 0xffffffff;
1234 else if (i >= 96)
1235 mask[1] = 0;
1236 else
1237 mask[1] = (0xffffffff << (i - 64)) & 0xffffffff;
1238
1239 if (i <= 32)
1240 mask[2] = 0xffffffff;
1241 else if (i >= 64)
1242 mask[2] = 0;
1243 else
1244 mask[2] = (0xffffffff << (i - 32)) & 0xffffffff;
1245
1246 if (i == 0)
1247 mask[3] = 0xffffffff;
1248 else if (i >= 32)
1249 mask[3] = 0;
1250 else
1251 mask[3] = (0xffffffff << i) & 0xffffffff;
1252 }
1253 else
1254 #endif /* AF_INET6 */
1255 {
1256 i = 32 - i;
1257
1258 mask[0] = 0xffffffff;
1259 mask[1] = 0xffffffff;
1260 mask[2] = 0xffffffff;
1261
1262 if (i > 0)
1263 mask[3] = (0xffffffff << i) & 0xffffffff;
1264 else
1265 mask[3] = 0xffffffff;
1266 }
1267 }
1268 }
1269 else
1270 memcpy(mask, netmasks[ipcount - 1], sizeof(unsigned) * 4);
1271
1272 LogMessage(L_DEBUG2, "get_addr_and_mask(value=\"%s\", "
1273 "ip=[%08x:%08x:%08x:%08x], mask=[%08x:%08x:%08x:%08x]",
1274 value, ip[0], ip[1], ip[2], ip[3], mask[0], mask[1], mask[2],
1275 mask[3]);
1276
1277 /*
1278 * Check for a valid netmask; no fallback like in CUPS 1.1.x!
1279 */
1280
1281 if ((ip[0] & ~mask[0]) != 0 ||
1282 (ip[1] & ~mask[1]) != 0 ||
1283 (ip[2] & ~mask[2]) != 0 ||
1284 (ip[3] & ~mask[3]) != 0)
1285 return (0);
1286
1287 return (1);
1288 }
1289
1290
1291 /*
1292 * 'parse_aaa()' - Parse authentication, authorization, and access control lines.
1293 */
1294
1295 static int /* O - 1 on success, 0 on failure */
1296 parse_aaa(location_t *loc, /* I - Location */
1297 char *line, /* I - Line from file */
1298 char *value, /* I - Start of value data */
1299 int linenum) /* I - Current line number */
1300 {
1301 char *valptr; /* Pointer into value */
1302 unsigned ip[4], /* IP address components */
1303 mask[4]; /* IP netmask components */
1304
1305
1306 if (!strcasecmp(line, "Encryption"))
1307 {
1308 /*
1309 * "Encryption xxx" - set required encryption level...
1310 */
1311
1312 if (!strcasecmp(value, "never"))
1313 loc->encryption = HTTP_ENCRYPT_NEVER;
1314 else if (!strcasecmp(value, "always"))
1315 {
1316 LogMessage(L_ERROR, "Encryption value \"%s\" on line %d is invalid in this context. "
1317 "Using \"required\" instead.", value, linenum);
1318
1319 loc->encryption = HTTP_ENCRYPT_REQUIRED;
1320 }
1321 else if (!strcasecmp(value, "required"))
1322 loc->encryption = HTTP_ENCRYPT_REQUIRED;
1323 else if (!strcasecmp(value, "ifrequested"))
1324 loc->encryption = HTTP_ENCRYPT_IF_REQUESTED;
1325 else
1326 {
1327 LogMessage(L_ERROR, "Unknown Encryption value %s on line %d.",
1328 value, linenum);
1329 return (0);
1330 }
1331 }
1332 else if (!strcasecmp(line, "Order"))
1333 {
1334 /*
1335 * "Order Deny,Allow" or "Order Allow,Deny"...
1336 */
1337
1338 if (!strncasecmp(value, "deny", 4))
1339 loc->order_type = AUTH_ALLOW;
1340 else if (!strncasecmp(value, "allow", 5))
1341 loc->order_type = AUTH_DENY;
1342 else
1343 {
1344 LogMessage(L_ERROR, "Unknown Order value %s on line %d.",
1345 value, linenum);
1346 return (0);
1347 }
1348 }
1349 else if (!strcasecmp(line, "Allow") || !strcasecmp(line, "Deny"))
1350 {
1351 /*
1352 * Allow [From] host/ip...
1353 * Deny [From] host/ip...
1354 */
1355
1356 if (!strncasecmp(value, "from", 4))
1357 {
1358 /*
1359 * Strip leading "from"...
1360 */
1361
1362 value += 4;
1363
1364 while (isspace(*value & 255))
1365 value ++;
1366 }
1367
1368 /*
1369 * Figure out what form the allow/deny address takes:
1370 *
1371 * All
1372 * None
1373 * *.domain.com
1374 * .domain.com
1375 * host.domain.com
1376 * nnn.*
1377 * nnn.nnn.*
1378 * nnn.nnn.nnn.*
1379 * nnn.nnn.nnn.nnn
1380 * nnn.nnn.nnn.nnn/mm
1381 * nnn.nnn.nnn.nnn/mmm.mmm.mmm.mmm
1382 */
1383
1384 if (!strcasecmp(value, "all"))
1385 {
1386 /*
1387 * All hosts...
1388 */
1389
1390 if (!strcasecmp(line, "Allow"))
1391 AllowIP(loc, zeros, zeros);
1392 else
1393 DenyIP(loc, zeros, zeros);
1394 }
1395 else if (!strcasecmp(value, "none"))
1396 {
1397 /*
1398 * No hosts...
1399 */
1400
1401 if (!strcasecmp(line, "Allow"))
1402 AllowIP(loc, ones, zeros);
1403 else
1404 DenyIP(loc, ones, zeros);
1405 }
1406 else if (value[0] == '*' || value[0] == '.' || !isdigit(value[0] & 255))
1407 {
1408 /*
1409 * Host or domain name...
1410 */
1411
1412 if (value[0] == '*')
1413 value ++;
1414
1415 if (!strcasecmp(line, "Allow"))
1416 AllowHost(loc, value);
1417 else
1418 DenyHost(loc, value);
1419 }
1420 else
1421 {
1422 /*
1423 * One of many IP address forms...
1424 */
1425
1426 if (!get_addr_and_mask(value, ip, mask))
1427 {
1428 LogMessage(L_ERROR, "Bad netmask value %s on line %d.",
1429 value, linenum);
1430 return (0);
1431 }
1432
1433 if (!strcasecmp(line, "Allow"))
1434 AllowIP(loc, ip, mask);
1435 else
1436 DenyIP(loc, ip, mask);
1437 }
1438 }
1439 else if (!strcasecmp(line, "AuthType"))
1440 {
1441 /*
1442 * AuthType {none,basic,digest,basicdigest}
1443 */
1444
1445 if (!strcasecmp(value, "none"))
1446 {
1447 loc->type = AUTH_NONE;
1448 loc->level = AUTH_ANON;
1449 }
1450 else if (!strcasecmp(value, "basic"))
1451 {
1452 loc->type = AUTH_BASIC;
1453
1454 if (loc->level == AUTH_ANON)
1455 loc->level = AUTH_USER;
1456 }
1457 else if (!strcasecmp(value, "digest"))
1458 {
1459 loc->type = AUTH_DIGEST;
1460
1461 if (loc->level == AUTH_ANON)
1462 loc->level = AUTH_USER;
1463 }
1464 else if (!strcasecmp(value, "basicdigest"))
1465 {
1466 loc->type = AUTH_BASICDIGEST;
1467
1468 if (loc->level == AUTH_ANON)
1469 loc->level = AUTH_USER;
1470 }
1471 else
1472 {
1473 LogMessage(L_WARN, "Unknown authorization type %s on line %d.",
1474 value, linenum);
1475 return (0);
1476 }
1477 }
1478 else if (!strcasecmp(line, "AuthClass"))
1479 {
1480 /*
1481 * AuthClass anonymous, user, system, group
1482 */
1483
1484 if (!strcasecmp(value, "anonymous"))
1485 {
1486 loc->type = AUTH_NONE;
1487 loc->level = AUTH_ANON;
1488 }
1489 else if (!strcasecmp(value, "user"))
1490 loc->level = AUTH_USER;
1491 else if (!strcasecmp(value, "group"))
1492 loc->level = AUTH_GROUP;
1493 else if (!strcasecmp(value, "system"))
1494 {
1495 loc->level = AUTH_GROUP;
1496
1497 AddName(loc, "@SYSTEM");
1498 }
1499 else
1500 {
1501 LogMessage(L_WARN, "Unknown authorization class %s on line %d.",
1502 value, linenum);
1503 return (0);
1504 }
1505 }
1506 else if (!strcasecmp(line, "AuthGroupName"))
1507 AddName(loc, value);
1508 else if (!strcasecmp(line, "Require"))
1509 {
1510 /*
1511 * Apache synonym for AuthClass and AuthGroupName...
1512 *
1513 * Get initial word:
1514 *
1515 * Require valid-user
1516 * Require group names
1517 * Require user names
1518 */
1519
1520 for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
1521
1522 if (*valptr)
1523 *valptr++ = '\0';
1524
1525 if (!strcasecmp(value, "valid-user") ||
1526 !strcasecmp(value, "user"))
1527 loc->level = AUTH_USER;
1528 else if (!strcasecmp(value, "group"))
1529 loc->level = AUTH_GROUP;
1530 else
1531 {
1532 LogMessage(L_WARN, "Unknown Require type %s on line %d.",
1533 value, linenum);
1534 return (0);
1535 }
1536
1537 /*
1538 * Get the list of names from the line...
1539 */
1540
1541 for (value = valptr; *value;)
1542 {
1543 while (isspace(*value & 255))
1544 value ++;
1545
1546 if (*value == '\"' || *value == '\'')
1547 {
1548 /*
1549 * Grab quoted name...
1550 */
1551
1552 for (valptr = value + 1; *valptr != *value && *valptr; valptr ++);
1553
1554 value ++;
1555 }
1556 else
1557 {
1558 /*
1559 * Grab literal name.
1560 */
1561
1562 for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
1563 }
1564
1565 if (*valptr)
1566 *valptr++ = '\0';
1567
1568 AddName(loc, value);
1569
1570 for (value = valptr; isspace(*value & 255); value ++);
1571 }
1572 }
1573 else if (!strcasecmp(line, "Satisfy"))
1574 {
1575 if (!strcasecmp(value, "all"))
1576 loc->satisfy = AUTH_SATISFY_ALL;
1577 else if (!strcasecmp(value, "any"))
1578 loc->satisfy = AUTH_SATISFY_ANY;
1579 else
1580 {
1581 LogMessage(L_WARN, "Unknown Satisfy value %s on line %d.", value,
1582 linenum);
1583 return (0);
1584 }
1585 }
1586 else
1587 return (0);
1588
1589 return (1);
1590 }
1591
1592
1593 /*
1594 * 'read_configuration()' - Read a configuration file.
1595 */
1596
1597 static int /* O - 1 on success, 0 on failure */
1598 read_configuration(cups_file_t *fp) /* I - File to read from */
1599 {
1600 int i; /* Looping var */
1601 int linenum; /* Current line number */
1602 char line[HTTP_MAX_BUFFER], /* Line from file */
1603 temp[HTTP_MAX_BUFFER], /* Temporary buffer for value */
1604 temp2[HTTP_MAX_BUFFER], /* Temporary buffer 2 for value */
1605 *ptr, /* Pointer into line/temp */
1606 *value; /* Pointer to value */
1607 int valuelen; /* Length of value */
1608 var_t *var; /* Current variable */
1609 unsigned ip[4], /* Address value */
1610 mask[4]; /* Netmask value */
1611 dirsvc_relay_t *relay; /* Relay data */
1612 dirsvc_poll_t *poll; /* Polling data */
1613 http_addr_t polladdr; /* Polling address */
1614 location_t *location; /* Browse location */
1615 cups_file_t *incfile; /* Include file */
1616 char incname[1024]; /* Include filename */
1617
1618
1619 /*
1620 * Loop through each line in the file...
1621 */
1622
1623 linenum = 0;
1624
1625 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
1626 {
1627 /*
1628 * Decode the directive...
1629 */
1630
1631 if (!strcasecmp(line, "Include"))
1632 {
1633 /*
1634 * Include filename
1635 */
1636
1637 if (value[0] == '/')
1638 strlcpy(incname, value, sizeof(incname));
1639 else
1640 snprintf(incname, sizeof(incname), "%s/%s", ServerRoot, value);
1641
1642 if ((incfile = cupsFileOpen(incname, "rb")) == NULL)
1643 LogMessage(L_ERROR, "Unable to include config file \"%s\" - %s",
1644 incname, strerror(errno));
1645 else
1646 {
1647 read_configuration(incfile);
1648 cupsFileClose(incfile);
1649 }
1650 }
1651 else if (!strcasecmp(line, "<Location"))
1652 {
1653 /*
1654 * <Location path>
1655 */
1656
1657 if (value)
1658 {
1659 linenum = read_location(fp, value, linenum);
1660 if (linenum == 0)
1661 return (0);
1662 }
1663 else
1664 {
1665 LogMessage(L_ERROR, "Syntax error on line %d.",
1666 linenum);
1667 return (0);
1668 }
1669 }
1670 else if (!strcasecmp(line, "<Policy"))
1671 {
1672 /*
1673 * <Policy name>
1674 */
1675
1676 if (value)
1677 {
1678 linenum = read_policy(fp, value, linenum);
1679 if (linenum == 0)
1680 return (0);
1681 }
1682 else
1683 {
1684 LogMessage(L_ERROR, "Syntax error on line %d.",
1685 linenum);
1686 return (0);
1687 }
1688 }
1689 else if (!strcasecmp(line, "Port") || !strcasecmp(line, "Listen"))
1690 {
1691 /*
1692 * Add a listening address to the list...
1693 */
1694
1695 listener_t *lis; /* New listeners array */
1696
1697
1698 if (NumListeners == 0)
1699 lis = malloc(sizeof(listener_t));
1700 else
1701 lis = realloc(Listeners, (NumListeners + 1) * sizeof(listener_t));
1702
1703 if (!lis)
1704 {
1705 LogMessage(L_ERROR, "Unable to allocate %s at line %d - %s.",
1706 line, linenum, strerror(errno));
1707 continue;
1708 }
1709
1710 Listeners = lis;
1711 lis += NumListeners;
1712
1713 memset(lis, 0, sizeof(listener_t));
1714
1715 #ifdef AF_INET6
1716 if (get_address(value, INADDR_ANY, IPP_PORT, AF_INET6, &(lis->address)))
1717 #else
1718 if (get_address(value, INADDR_ANY, IPP_PORT, AF_INET, &(lis->address)))
1719 #endif /* AF_INET6 */
1720 {
1721 httpAddrString(&(lis->address), temp, sizeof(temp));
1722
1723 #ifdef AF_INET6
1724 if (lis->address.addr.sa_family == AF_INET6)
1725 LogMessage(L_INFO, "Listening to %s:%d (IPv6)", temp,
1726 ntohs(lis->address.ipv6.sin6_port));
1727 else
1728 #endif /* AF_INET6 */
1729 #ifdef AF_LOCAL
1730 if (lis->address.addr.sa_family == AF_LOCAL)
1731 LogMessage(L_INFO, "Listening to %s (Domain)", temp);
1732 else
1733 #endif /* AF_LOCAL */
1734 LogMessage(L_INFO, "Listening to %s:%d", temp,
1735 ntohs(lis->address.ipv4.sin_port));
1736
1737 NumListeners ++;
1738 }
1739 else
1740 LogMessage(L_ERROR, "Bad %s address %s at line %d.", line,
1741 value, linenum);
1742 }
1743 #ifdef HAVE_SSL
1744 else if (!strcasecmp(line, "SSLPort") || !strcasecmp(line, "SSLListen"))
1745 {
1746 /*
1747 * Add a listening address to the list...
1748 */
1749
1750 listener_t *lis; /* New listeners array */
1751
1752
1753 if (NumListeners == 0)
1754 lis = malloc(sizeof(listener_t));
1755 else
1756 lis = realloc(Listeners, (NumListeners + 1) * sizeof(listener_t));
1757
1758 if (!lis)
1759 {
1760 LogMessage(L_ERROR, "Unable to allocate %s at line %d - %s.",
1761 line, linenum, strerror(errno));
1762 continue;
1763 }
1764
1765 Listeners = lis;
1766 lis += NumListeners;
1767
1768 if (get_address(value, INADDR_ANY, IPP_PORT, AF_INET, &(lis->address)))
1769 {
1770 httpAddrString(&(lis->address), temp, sizeof(temp));
1771
1772 #ifdef AF_INET6
1773 if (lis->address.addr.sa_family == AF_INET6)
1774 LogMessage(L_INFO, "Listening to %s:%d (IPv6)", temp,
1775 ntohs(lis->address.ipv6.sin6_port));
1776 else
1777 #endif /* AF_INET6 */
1778 LogMessage(L_INFO, "Listening to %s:%d", temp,
1779 ntohs(lis->address.ipv4.sin_port));
1780 lis->encryption = HTTP_ENCRYPT_ALWAYS;
1781 NumListeners ++;
1782 }
1783 else
1784 LogMessage(L_ERROR, "Bad %s address %s at line %d.", line,
1785 value, linenum);
1786 }
1787 #endif /* HAVE_SSL */
1788 else if (!strcasecmp(line, "BrowseAddress"))
1789 {
1790 /*
1791 * Add a browse address to the list...
1792 */
1793
1794 dirsvc_addr_t *dira; /* New browse address array */
1795
1796
1797 if (NumBrowsers == 0)
1798 dira = malloc(sizeof(dirsvc_addr_t));
1799 else
1800 dira = realloc(Browsers, (NumBrowsers + 1) * sizeof(dirsvc_addr_t));
1801
1802 if (!dira)
1803 {
1804 LogMessage(L_ERROR, "Unable to allocate BrowseAddress at line %d - %s.",
1805 linenum, strerror(errno));
1806 continue;
1807 }
1808
1809 Browsers = dira;
1810 dira += NumBrowsers;
1811
1812 memset(dira, 0, sizeof(dirsvc_addr_t));
1813
1814 if (!strcasecmp(value, "@LOCAL"))
1815 {
1816 /*
1817 * Send browse data to all local interfaces...
1818 */
1819
1820 strcpy(dira->iface, "*");
1821 NumBrowsers ++;
1822 }
1823 else if (!strncasecmp(value, "@IF(", 4))
1824 {
1825 /*
1826 * Send browse data to the named interface...
1827 */
1828
1829 strlcpy(dira->iface, value + 4, sizeof(Browsers[0].iface));
1830
1831 ptr = dira->iface + strlen(dira->iface) - 1;
1832 if (*ptr == ')')
1833 *ptr = '\0';
1834
1835 NumBrowsers ++;
1836 }
1837 else if (get_address(value, INADDR_NONE, BrowsePort, AF_INET, &(dira->to)))
1838 {
1839 httpAddrString(&(dira->to), temp, sizeof(temp));
1840
1841 #ifdef AF_INET6
1842 if (dira->to.addr.sa_family == AF_INET6)
1843 LogMessage(L_INFO, "Sending browsing info to %s:%d (IPv6)", temp,
1844 ntohs(dira->to.ipv6.sin6_port));
1845 else
1846 #endif /* AF_INET6 */
1847 LogMessage(L_INFO, "Sending browsing info to %s:%d", temp,
1848 ntohs(dira->to.ipv4.sin_port));
1849
1850 NumBrowsers ++;
1851 }
1852 else
1853 LogMessage(L_ERROR, "Bad BrowseAddress %s at line %d.", value,
1854 linenum);
1855 }
1856 else if (!strcasecmp(line, "BrowseOrder"))
1857 {
1858 /*
1859 * "BrowseOrder Deny,Allow" or "BrowseOrder Allow,Deny"...
1860 */
1861
1862 if ((location = FindLocation("CUPS_INTERNAL_BROWSE_ACL")) == NULL)
1863 location = AddLocation("CUPS_INTERNAL_BROWSE_ACL");
1864
1865 if (location == NULL)
1866 LogMessage(L_ERROR, "Unable to initialize browse access control list!");
1867 else if (!strncasecmp(value, "deny", 4))
1868 location->order_type = AUTH_ALLOW;
1869 else if (!strncasecmp(value, "allow", 5))
1870 location->order_type = AUTH_DENY;
1871 else
1872 LogMessage(L_ERROR, "Unknown BrowseOrder value %s on line %d.",
1873 value, linenum);
1874 }
1875 else if (!strcasecmp(line, "BrowseProtocols") ||
1876 !strcasecmp(line, "BrowseLocalProtocols") ||
1877 !strcasecmp(line, "BrowseRemoteProtocols"))
1878 {
1879 /*
1880 * "BrowseProtocol name [... name]"
1881 */
1882
1883 if (strcasecmp(line, "BrowseLocalProtocols"))
1884 BrowseRemoteProtocols = 0;
1885 if (strcasecmp(line, "BrowseRemoteProtocols"))
1886 BrowseLocalProtocols = 0;
1887
1888 for (; *value;)
1889 {
1890 for (valuelen = 0; value[valuelen]; valuelen ++)
1891 if (isspace(value[valuelen]) || value[valuelen] == ',')
1892 break;
1893
1894 if (value[valuelen])
1895 {
1896 value[valuelen] = '\0';
1897 valuelen ++;
1898 }
1899
1900 if (!strcasecmp(value, "cups"))
1901 {
1902 if (strcasecmp(line, "BrowseLocalProtocols"))
1903 BrowseRemoteProtocols |= BROWSE_CUPS;
1904 if (strcasecmp(line, "BrowseRemoteProtocols"))
1905 BrowseLocalProtocols |= BROWSE_CUPS;
1906 }
1907 else if (!strcasecmp(value, "slp"))
1908 {
1909 if (strcasecmp(line, "BrowseLocalProtocols"))
1910 BrowseRemoteProtocols |= BROWSE_SLP;
1911 if (strcasecmp(line, "BrowseRemoteProtocols"))
1912 BrowseLocalProtocols |= BROWSE_SLP;
1913 }
1914 else if (!strcasecmp(value, "ldap"))
1915 {
1916 if (strcasecmp(line, "BrowseLocalProtocols"))
1917 BrowseRemoteProtocols |= BROWSE_LDAP;
1918 if (strcasecmp(line, "BrowseRemoteProtocols"))
1919 BrowseLocalProtocols |= BROWSE_LDAP;
1920 }
1921 else if (!strcasecmp(value, "all"))
1922 {
1923 if (strcasecmp(line, "BrowseLocalProtocols"))
1924 BrowseRemoteProtocols |= BROWSE_ALL;
1925 if (strcasecmp(line, "BrowseRemoteProtocols"))
1926 BrowseLocalProtocols |= BROWSE_ALL;
1927 }
1928 else
1929 {
1930 LogMessage(L_ERROR, "Unknown browse protocol \"%s\" on line %d.",
1931 value, linenum);
1932 break;
1933 }
1934
1935 for (value += valuelen; *value; value ++)
1936 if (!isspace(*value) || *value != ',')
1937 break;
1938 }
1939 }
1940 else if (!strcasecmp(line, "BrowseAllow") ||
1941 !strcasecmp(line, "BrowseDeny"))
1942 {
1943 /*
1944 * BrowseAllow [From] host/ip...
1945 * BrowseDeny [From] host/ip...
1946 */
1947
1948 if ((location = FindLocation("CUPS_INTERNAL_BROWSE_ACL")) == NULL)
1949 location = AddLocation("CUPS_INTERNAL_BROWSE_ACL");
1950
1951 if (location == NULL)
1952 LogMessage(L_ERROR, "Unable to initialize browse access control list!");
1953 else
1954 {
1955 if (!strncasecmp(value, "from ", 5))
1956 {
1957 /*
1958 * Strip leading "from"...
1959 */
1960
1961 value += 5;
1962
1963 while (isspace(*value))
1964 value ++;
1965 }
1966
1967 /*
1968 * Figure out what form the allow/deny address takes:
1969 *
1970 * All
1971 * None
1972 * *.domain.com
1973 * .domain.com
1974 * host.domain.com
1975 * nnn.*
1976 * nnn.nnn.*
1977 * nnn.nnn.nnn.*
1978 * nnn.nnn.nnn.nnn
1979 * nnn.nnn.nnn.nnn/mm
1980 * nnn.nnn.nnn.nnn/mmm.mmm.mmm.mmm
1981 */
1982
1983 if (!strcasecmp(value, "all"))
1984 {
1985 /*
1986 * All hosts...
1987 */
1988
1989 if (!strcasecmp(line, "BrowseAllow"))
1990 AllowIP(location, zeros, zeros);
1991 else
1992 DenyIP(location, zeros, zeros);
1993 }
1994 else if (!strcasecmp(value, "none"))
1995 {
1996 /*
1997 * No hosts...
1998 */
1999
2000 if (!strcasecmp(line, "BrowseAllow"))
2001 AllowIP(location, ones, zeros);
2002 else
2003 DenyIP(location, ones, zeros);
2004 }
2005 else if (value[0] == '*' || value[0] == '.' || !isdigit(value[0]))
2006 {
2007 /*
2008 * Host or domain name...
2009 */
2010
2011 if (value[0] == '*')
2012 value ++;
2013
2014 if (!strcasecmp(line, "BrowseAllow"))
2015 AllowHost(location, value);
2016 else
2017 DenyHost(location, value);
2018 }
2019 else
2020 {
2021 /*
2022 * One of many IP address forms...
2023 */
2024
2025 if (!get_addr_and_mask(value, ip, mask))
2026 {
2027 LogMessage(L_ERROR, "Bad netmask value %s on line %d.",
2028 value, linenum);
2029 break;
2030 }
2031
2032 if (!strcasecmp(line, "BrowseAllow"))
2033 AllowIP(location, ip, mask);
2034 else
2035 DenyIP(location, ip, mask);
2036 }
2037 }
2038 }
2039 else if (!strcasecmp(line, "BrowseRelay"))
2040 {
2041 /*
2042 * BrowseRelay [from] source [to] destination
2043 */
2044
2045 if (NumRelays == 0)
2046 relay = malloc(sizeof(dirsvc_relay_t));
2047 else
2048 relay = realloc(Relays, (NumRelays + 1) * sizeof(dirsvc_relay_t));
2049
2050 if (!relay)
2051 {
2052 LogMessage(L_ERROR, "Unable to allocate BrowseRelay at line %d - %s.",
2053 linenum, strerror(errno));
2054 continue;
2055 }
2056
2057 Relays = relay;
2058 relay += NumRelays;
2059
2060 memset(relay, 0, sizeof(dirsvc_relay_t));
2061
2062 if (!strncasecmp(value, "from ", 5))
2063 {
2064 /*
2065 * Strip leading "from"...
2066 */
2067
2068 value += 5;
2069
2070 while (isspace(*value))
2071 value ++;
2072 }
2073
2074 /*
2075 * Figure out what form the from address takes:
2076 *
2077 * *.domain.com
2078 * .domain.com
2079 * host.domain.com
2080 * nnn.*
2081 * nnn.nnn.*
2082 * nnn.nnn.nnn.*
2083 * nnn.nnn.nnn.nnn
2084 * nnn.nnn.nnn.nnn/mm
2085 * nnn.nnn.nnn.nnn/mmm.mmm.mmm.mmm
2086 */
2087
2088 if (value[0] == '*' || value[0] == '.' || !isdigit(value[0]))
2089 {
2090 /*
2091 * Host or domain name...
2092 */
2093
2094 if (value[0] == '*')
2095 value ++;
2096
2097 strlcpy(temp, value, sizeof(temp));
2098 if ((ptr = strchr(temp, ' ')) != NULL)
2099 *ptr = '\0';
2100
2101 relay->from.type = AUTH_NAME;
2102 relay->from.mask.name.name = strdup(temp);
2103 relay->from.mask.name.length = strlen(temp);
2104 }
2105 else
2106 {
2107 /*
2108 * One of many IP address forms...
2109 */
2110
2111 if (!get_addr_and_mask(value, ip, mask))
2112 {
2113 LogMessage(L_ERROR, "Bad netmask value %s on line %d.",
2114 value, linenum);
2115 break;
2116 }
2117
2118 relay->from.type = AUTH_IP;
2119 memcpy(relay->from.mask.ip.address, ip,
2120 sizeof(relay->from.mask.ip.address));
2121 memcpy(relay->from.mask.ip.netmask, mask,
2122 sizeof(relay->from.mask.ip.netmask));
2123 }
2124
2125 /*
2126 * Skip value and trailing whitespace...
2127 */
2128
2129 for (; *value; value ++)
2130 if (isspace(*value))
2131 break;
2132
2133 while (isspace(*value))
2134 value ++;
2135
2136 if (!strncasecmp(value, "to ", 3))
2137 {
2138 /*
2139 * Strip leading "to"...
2140 */
2141
2142 value += 3;
2143
2144 while (isspace(*value))
2145 value ++;
2146 }
2147
2148 /*
2149 * Get "to" address and port...
2150 */
2151
2152 if (get_address(value, INADDR_BROADCAST, BrowsePort, AF_INET, &(relay->to)))
2153 {
2154 httpAddrString(&(relay->to), temp, sizeof(temp));
2155
2156 if (relay->from.type == AUTH_IP)
2157 snprintf(temp2, sizeof(temp2), "%u.%u.%u.%u/%u.%u.%u.%u",
2158 relay->from.mask.ip.address[0],
2159 relay->from.mask.ip.address[1],
2160 relay->from.mask.ip.address[2],
2161 relay->from.mask.ip.address[3],
2162 relay->from.mask.ip.netmask[0],
2163 relay->from.mask.ip.netmask[1],
2164 relay->from.mask.ip.netmask[2],
2165 relay->from.mask.ip.netmask[3]);
2166 else
2167 strlcpy(temp2, relay->from.mask.name.name, sizeof(temp2));
2168
2169 #ifdef AF_INET6
2170 if (relay->to.addr.sa_family == AF_INET6)
2171 LogMessage(L_INFO, "Relaying from %s to %s:%d", temp, temp2,
2172 ntohs(relay->to.ipv6.sin6_port));
2173 else
2174 #endif /* AF_INET6 */
2175 LogMessage(L_INFO, "Relaying from %s to %s:%d", temp, temp2,
2176 ntohs(relay->to.ipv4.sin_port));
2177
2178 NumRelays ++;
2179 }
2180 else
2181 {
2182 if (relay->from.type == AUTH_NAME)
2183 free(relay->from.mask.name.name);
2184
2185 LogMessage(L_ERROR, "Bad relay address %s at line %d.", value, linenum);
2186 }
2187 }
2188 else if (!strcasecmp(line, "BrowsePoll"))
2189 {
2190 /*
2191 * BrowsePoll address[:port]
2192 */
2193
2194 if (NumPolled == 0)
2195 poll = malloc(sizeof(dirsvc_poll_t));
2196 else
2197 poll = realloc(Polled, (NumPolled + 1) * sizeof(dirsvc_poll_t));
2198
2199 if (!poll)
2200 {
2201 LogMessage(L_ERROR, "Unable to allocate BrowsePoll at line %d - %s.",
2202 linenum, strerror(errno));
2203 continue;
2204 }
2205
2206 Polled = poll;
2207 poll += NumPolled;
2208
2209 /*
2210 * Get poll address and port...
2211 */
2212
2213 if (get_address(value, INADDR_NONE, ippPort(), AF_INET, &polladdr))
2214 {
2215 NumPolled ++;
2216 memset(poll, 0, sizeof(dirsvc_poll_t));
2217
2218 httpAddrString(&polladdr, poll->hostname, sizeof(poll->hostname));
2219
2220 #ifdef AF_INET6
2221 if (polladdr.addr.sa_family == AF_INET6)
2222 poll->port = ntohs(polladdr.ipv6.sin6_port);
2223 else
2224 #endif /* AF_INET6 */
2225 poll->port = ntohs(polladdr.ipv4.sin_port);
2226
2227 LogMessage(L_INFO, "Polling %s:%d", poll->hostname, poll->port);
2228 }
2229 else
2230 LogMessage(L_ERROR, "Bad poll address %s at line %d.", value, linenum);
2231 }
2232 else if (!strcasecmp(line, "DefaultAuthType"))
2233 {
2234 /*
2235 * DefaultAuthType {basic,digest,basicdigest}
2236 */
2237
2238 if (!strcasecmp(value, "basic"))
2239 DefaultAuthType = AUTH_BASIC;
2240 else if (!strcasecmp(value, "digest"))
2241 DefaultAuthType = AUTH_DIGEST;
2242 else if (!strcasecmp(value, "basicdigest"))
2243 DefaultAuthType = AUTH_BASICDIGEST;
2244 else
2245 {
2246 LogMessage(L_WARN, "Unknown default authorization type %s on line %d.",
2247 value, linenum);
2248 return (0);
2249 }
2250 }
2251 else if (!strcasecmp(line, "User"))
2252 {
2253 /*
2254 * User ID to run as...
2255 */
2256
2257 if (isdigit(value[0]))
2258 User = atoi(value);
2259 else
2260 {
2261 struct passwd *p; /* Password information */
2262
2263 endpwent();
2264 p = getpwnam(value);
2265
2266 if (p != NULL)
2267 User = p->pw_uid;
2268 else
2269 LogMessage(L_WARN, "Unknown username \"%s\"", value);
2270 }
2271 }
2272 else if (!strcasecmp(line, "Group"))
2273 {
2274 /*
2275 * Group ID to run as...
2276 */
2277
2278 if (isdigit(value[0]))
2279 Group = atoi(value);
2280 else
2281 {
2282 struct group *g; /* Group information */
2283
2284 endgrent();
2285 g = getgrnam(value);
2286
2287 if (g != NULL)
2288 Group = g->gr_gid;
2289 else
2290 LogMessage(L_WARN, "Unknown groupname \"%s\"", value);
2291 }
2292 }
2293 else if (!strcasecmp(line, "SystemGroup"))
2294 {
2295 /*
2296 * System (admin) group(s)...
2297 */
2298
2299 char *valueptr, /* Pointer into value */
2300 quote; /* Quote character */
2301
2302
2303 for (i = NumSystemGroups; *value && i < MAX_SYSTEM_GROUPS; i ++)
2304 {
2305 if (*value == '\'' || *value == '\"')
2306 {
2307 /*
2308 * Scan quoted name...
2309 */
2310
2311 quote = *value++;
2312
2313 for (valueptr = value; *valueptr; valueptr ++)
2314 if (*valueptr == quote)
2315 break;
2316 }
2317 else
2318 {
2319 /*
2320 * Scan space or comma-delimited name...
2321 */
2322
2323 for (valueptr = value; *valueptr; valueptr ++)
2324 if (isspace(*valueptr) || *valueptr == ',')
2325 break;
2326 }
2327
2328 if (*valueptr)
2329 *valueptr++ = '\0';
2330
2331 SetString(SystemGroups + i, value);
2332
2333 value = valueptr;
2334
2335 while (*value == ',' || isspace(*value))
2336 value ++;
2337 }
2338
2339 if (i)
2340 NumSystemGroups = i;
2341 }
2342 else if (!strcasecmp(line, "HostNameLookups"))
2343 {
2344 /*
2345 * Do hostname lookups?
2346 */
2347
2348 if (!strcasecmp(value, "off"))
2349 HostNameLookups = 0;
2350 else if (!strcasecmp(value, "on"))
2351 HostNameLookups = 1;
2352 else if (!strcasecmp(value, "double"))
2353 HostNameLookups = 2;
2354 else
2355 LogMessage(L_WARN, "Unknown HostNameLookups %s on line %d.",
2356 value, linenum);
2357 }
2358 else if (!strcasecmp(line, "LogLevel"))
2359 {
2360 /*
2361 * Amount of logging to do...
2362 */
2363
2364 if (!strcasecmp(value, "debug2"))
2365 LogLevel = L_DEBUG2;
2366 else if (!strcasecmp(value, "debug"))
2367 LogLevel = L_DEBUG;
2368 else if (!strcasecmp(value, "info"))
2369 LogLevel = L_INFO;
2370 else if (!strcasecmp(value, "notice"))
2371 LogLevel = L_NOTICE;
2372 else if (!strcasecmp(value, "warn"))
2373 LogLevel = L_WARN;
2374 else if (!strcasecmp(value, "error"))
2375 LogLevel = L_ERROR;
2376 else if (!strcasecmp(value, "crit"))
2377 LogLevel = L_CRIT;
2378 else if (!strcasecmp(value, "alert"))
2379 LogLevel = L_ALERT;
2380 else if (!strcasecmp(value, "emerg"))
2381 LogLevel = L_EMERG;
2382 else if (!strcasecmp(value, "none"))
2383 LogLevel = L_NONE;
2384 else
2385 LogMessage(L_WARN, "Unknown LogLevel %s on line %d.", value, linenum);
2386 }
2387 else if (!strcasecmp(line, "PrintcapFormat"))
2388 {
2389 /*
2390 * Format of printcap file?
2391 */
2392
2393 if (!strcasecmp(value, "bsd"))
2394 PrintcapFormat = PRINTCAP_BSD;
2395 else if (!strcasecmp(value, "solaris"))
2396 PrintcapFormat = PRINTCAP_SOLARIS;
2397 else
2398 LogMessage(L_WARN, "Unknown PrintcapFormat %s on line %d.",
2399 value, linenum);
2400 }
2401 else if (!strcasecmp(line, "ServerTokens"))
2402 {
2403 /*
2404 * Set the string used for the Server header...
2405 */
2406
2407 struct utsname plat; /* Platform info */
2408
2409
2410 uname(&plat);
2411
2412 if (!strcasecmp(value, "ProductOnly"))
2413 SetString(&ServerHeader, "CUPS");
2414 else if (!strcasecmp(value, "Major"))
2415 SetString(&ServerHeader, "CUPS/1");
2416 else if (!strcasecmp(value, "Minor"))
2417 SetString(&ServerHeader, "CUPS/1.1");
2418 else if (!strcasecmp(value, "Minimal"))
2419 SetString(&ServerHeader, CUPS_MINIMAL);
2420 else if (!strcasecmp(value, "OS"))
2421 SetStringf(&ServerHeader, CUPS_MINIMAL " (%s)", plat.sysname);
2422 else if (!strcasecmp(value, "Full"))
2423 SetStringf(&ServerHeader, CUPS_MINIMAL " (%s) IPP/1.1", plat.sysname);
2424 else if (!strcasecmp(value, "None"))
2425 ClearString(&ServerHeader);
2426 else
2427 LogMessage(L_WARN, "Unknown ServerTokens %s on line %d.", value, linenum);
2428 }
2429 else
2430 {
2431 /*
2432 * Find a simple variable in the list...
2433 */
2434
2435 for (i = NUM_VARS, var = variables; i > 0; i --, var ++)
2436 if (!strcasecmp(line, var->name))
2437 break;
2438
2439 if (i == 0)
2440 {
2441 /*
2442 * Unknown directive! Output an error message and continue...
2443 */
2444
2445 LogMessage(L_ERROR, "Unknown directive %s on line %d.", line,
2446 linenum);
2447 continue;
2448 }
2449
2450 switch (var->type)
2451 {
2452 case VAR_INTEGER :
2453 {
2454 int n; /* Number */
2455 char *units; /* Units */
2456
2457
2458 n = strtol(value, &units, 0);
2459
2460 if (units && *units)
2461 {
2462 if (tolower(units[0] & 255) == 'g')
2463 n *= 1024 * 1024 * 1024;
2464 else if (tolower(units[0] & 255) == 'm')
2465 n *= 1024 * 1024;
2466 else if (tolower(units[0] & 255) == 'k')
2467 n *= 1024;
2468 else if (tolower(units[0] & 255) == 't')
2469 n *= 262144;
2470 }
2471
2472 *((int *)var->ptr) = n;
2473 }
2474 break;
2475
2476 case VAR_BOOLEAN :
2477 if (!strcasecmp(value, "true") ||
2478 !strcasecmp(value, "on") ||
2479 !strcasecmp(value, "enabled") ||
2480 !strcasecmp(value, "yes") ||
2481 atoi(value) != 0)
2482 *((int *)var->ptr) = TRUE;
2483 else if (!strcasecmp(value, "false") ||
2484 !strcasecmp(value, "off") ||
2485 !strcasecmp(value, "disabled") ||
2486 !strcasecmp(value, "no") ||
2487 !strcasecmp(value, "0"))
2488 *((int *)var->ptr) = FALSE;
2489 else
2490 LogMessage(L_ERROR, "Unknown boolean value %s on line %d.",
2491 value, linenum);
2492 break;
2493
2494 case VAR_STRING :
2495 SetString((char **)var->ptr, value);
2496 break;
2497 }
2498 }
2499 }
2500
2501 return (1);
2502 }
2503
2504
2505 /*
2506 * 'read_location()' - Read a <Location path> definition.
2507 */
2508
2509 static int /* O - New line number or 0 on error */
2510 read_location(cups_file_t *fp, /* I - Configuration file */
2511 char *location, /* I - Location name/path */
2512 int linenum) /* I - Current line number */
2513 {
2514 location_t *loc, /* New location */
2515 *parent; /* Parent location */
2516 char line[HTTP_MAX_BUFFER], /* Line buffer */
2517 *value, /* Value for directive */
2518 *valptr; /* Pointer into value */
2519
2520
2521 if ((parent = AddLocation(location)) == NULL)
2522 return (0);
2523
2524 parent->limit = AUTH_LIMIT_ALL;
2525 loc = parent;
2526
2527 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
2528 {
2529 /*
2530 * Decode the directive...
2531 */
2532
2533 if (!strcasecmp(line, "</Location>"))
2534 return (linenum);
2535 else if (!strcasecmp(line, "<Limit") ||
2536 !strcasecmp(line, "<LimitExcept"))
2537 {
2538 if (!value)
2539 {
2540 LogMessage(L_ERROR, "Syntax error on line %d.", linenum);
2541 return (0);
2542 }
2543
2544 if ((loc = CopyLocation(&parent)) == NULL)
2545 return (0);
2546
2547 loc->limit = 0;
2548 while (*value)
2549 {
2550 for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
2551
2552 if (*valptr)
2553 *valptr++ = '\0';
2554
2555 if (!strcmp(value, "ALL"))
2556 loc->limit = AUTH_LIMIT_ALL;
2557 else if (!strcmp(value, "GET"))
2558 loc->limit |= AUTH_LIMIT_GET;
2559 else if (!strcmp(value, "HEAD"))
2560 loc->limit |= AUTH_LIMIT_HEAD;
2561 else if (!strcmp(value, "OPTIONS"))
2562 loc->limit |= AUTH_LIMIT_OPTIONS;
2563 else if (!strcmp(value, "POST"))
2564 loc->limit |= AUTH_LIMIT_POST;
2565 else if (!strcmp(value, "PUT"))
2566 loc->limit |= AUTH_LIMIT_PUT;
2567 else if (!strcmp(value, "TRACE"))
2568 loc->limit |= AUTH_LIMIT_TRACE;
2569 else
2570 LogMessage(L_WARN, "Unknown request type %s on line %d!", value,
2571 linenum);
2572
2573 for (value = valptr; isspace(*value & 255); value ++);
2574 }
2575
2576 if (!strcasecmp(line, "<LimitExcept"))
2577 loc->limit = AUTH_LIMIT_ALL ^ loc->limit;
2578
2579 parent->limit &= ~loc->limit;
2580 }
2581 else if (!strcasecmp(line, "</Limit>"))
2582 loc = parent;
2583 else if (!parse_aaa(loc, line, value, linenum))
2584 {
2585 LogMessage(L_ERROR, "Unknown Location directive %s on line %d.",
2586 line, linenum);
2587 return (0);
2588 }
2589 }
2590
2591 LogMessage(L_ERROR, "Unexpected end-of-file at line %d while reading location!",
2592 linenum);
2593
2594 return (0);
2595 }
2596
2597
2598 /*
2599 * 'read_policy()' - Read a <Policy name> definition.
2600 */
2601
2602 static int /* O - New line number or 0 on error */
2603 read_policy(cups_file_t *fp, /* I - Configuration file */
2604 char *policy, /* I - Location name/path */
2605 int linenum) /* I - Current line number */
2606 {
2607 int i; /* Looping var */
2608 cupsd_policy_t *pol; /* Policy */
2609 location_t *op; /* Policy operation */
2610 int num_ops; /* Number of IPP operations */
2611 ipp_op_t ops[100]; /* Operations */
2612 char line[HTTP_MAX_BUFFER],
2613 /* Line buffer */
2614 *value, /* Value for directive */
2615 *valptr; /* Pointer into value */
2616
2617
2618 /*
2619 * Create the policy...
2620 */
2621
2622 if ((pol = cupsdAddPolicy(policy)) == NULL)
2623 return (0);
2624
2625 /*
2626 * Read from the file...
2627 */
2628
2629 op = NULL;
2630 num_ops = 0;
2631
2632 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
2633 {
2634 /*
2635 * Decode the directive...
2636 */
2637
2638 if (!strcasecmp(line, "</Policy>"))
2639 {
2640 if (op)
2641 LogMessage(L_WARN, "Missing </Limit> before </Policy> on line %d!",
2642 linenum);
2643
2644 return (linenum);
2645 }
2646 else if (!strcasecmp(line, "<Limit") && !op)
2647 {
2648 if (!value)
2649 {
2650 LogMessage(L_ERROR, "Syntax error on line %d.", linenum);
2651 return (0);
2652 }
2653
2654 /*
2655 * Scan for IPP operation names...
2656 */
2657
2658 num_ops = 0;
2659
2660 while (*value)
2661 {
2662 for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
2663
2664 if (*valptr)
2665 *valptr++ = '\0';
2666
2667 if (num_ops < (int)(sizeof(ops) / sizeof(ops[0])))
2668 {
2669 if (!strcasecmp(value, "All"))
2670 ops[num_ops] = IPP_ANY_OPERATION;
2671 else if ((ops[num_ops] = ippOpValue(value)) == IPP_BAD_OPERATION)
2672 LogMessage(L_ERROR, "Bad IPP operation name \"%s\" on line %d!",
2673 value, linenum);
2674 else
2675 num_ops ++;
2676 }
2677 else
2678 LogMessage(L_ERROR, "Too many operations listed on line %d!",
2679 linenum);
2680
2681 for (value = valptr; isspace(*value & 255); value ++);
2682 }
2683
2684 /*
2685 * If none are specified, apply the policy to all operations...
2686 */
2687
2688 if (num_ops == 0)
2689 {
2690 ops[0] = IPP_ANY_OPERATION;
2691 num_ops = 1;
2692 }
2693
2694 /*
2695 * Add a new policy for the first operation...
2696 */
2697
2698 op = cupsdAddPolicyOp(pol, NULL, ops[0]);
2699 }
2700 else if (!strcasecmp(line, "</Limit>") && op)
2701 {
2702 /*
2703 * Finish the current operation limit...
2704 */
2705
2706 if (num_ops > 1)
2707 {
2708 /*
2709 * Copy the policy to the other operations...
2710 */
2711
2712 for (i = 1; i < num_ops; i ++)
2713 cupsdAddPolicyOp(pol, op, ops[i]);
2714 }
2715
2716 op = NULL;
2717 }
2718 else if (!op)
2719 {
2720 LogMessage(L_ERROR, "Missing <Limit ops> directive before %s on line %d.",
2721 line, linenum);
2722 return (0);
2723 }
2724 else if (!parse_aaa(op, line, value, linenum))
2725 {
2726 if (op)
2727 LogMessage(L_ERROR, "Unknown Policy Limit directive %s on line %d.",
2728 line, linenum);
2729 else
2730 LogMessage(L_ERROR, "Unknown Policy directive %s on line %d.",
2731 line, linenum);
2732
2733 return (0);
2734 }
2735 }
2736
2737 LogMessage(L_ERROR, "Unexpected end-of-file at line %d while reading policy \"%s\"!",
2738 linenum, policy);
2739
2740 return (0);
2741 }
2742
2743
2744 #ifdef HAVE_CDSASSL
2745 /*
2746 * 'CDSAGetServerCerts()' - Convert a keychain name into the CFArrayRef
2747 * required by SSLSetCertificate.
2748 *
2749 * For now we assumes that there is exactly one SecIdentity in the
2750 * keychain - i.e. there is exactly one matching cert/private key pair.
2751 * In the future we will search a keychain for a SecIdentity matching a
2752 * specific criteria. We also skip the operation of adding additional
2753 * non-signing certs from the keychain to the CFArrayRef.
2754 *
2755 * To create a self-signed certificate for testing use the certtool.
2756 * Executing the following as root will do it:
2757 *
2758 * certtool c c v k=CUPS
2759 */
2760
2761 CFArrayRef
2762 CDSAGetServerCerts(void)
2763 {
2764 OSStatus err; /* Error info */
2765 SecKeychainRef kcRef; /* Keychain reference */
2766 SecIdentitySearchRef srchRef; /* Search reference */
2767 SecIdentityRef identity; /* Identity */
2768 CFArrayRef ca; /* Certificate array */
2769
2770
2771 kcRef = NULL;
2772 srchRef = NULL;
2773 identity = NULL;
2774 ca = NULL;
2775 err = SecKeychainOpen(ServerCertificate, &kcRef);
2776
2777 if (err)
2778 LogMessage(L_ERROR, "Cannot open keychain \"%s\", error %d.",
2779 ServerCertificate, err);
2780 else
2781 {
2782 /*
2783 * Search for "any" identity matching specified key use;
2784 * in this app, we expect there to be exactly one.
2785 */
2786
2787 err = SecIdentitySearchCreate(kcRef, CSSM_KEYUSE_SIGN, &srchRef);
2788
2789 if (err)
2790 LogMessage(L_ERROR,
2791 "Cannot find signing key in keychain \"%s\", error %d",
2792 ServerCertificate, err);
2793 else
2794 {
2795 err = SecIdentitySearchCopyNext(srchRef, &identity);
2796
2797 if (err)
2798 LogMessage(L_ERROR,
2799 "Cannot find signing key in keychain \"%s\", error %d",
2800 ServerCertificate, err);
2801 else
2802 {
2803 if (CFGetTypeID(identity) != SecIdentityGetTypeID())
2804 LogMessage(L_ERROR, "SecIdentitySearchCopyNext CFTypeID failure!");
2805 else
2806 {
2807 /*
2808 * Found one. Place it in a CFArray.
2809 * TBD: snag other (non-identity) certs from keychain and add them
2810 * to array as well.
2811 */
2812
2813 ca = CFArrayCreate(NULL, (const void **)&identity, 1, NULL);
2814
2815 if (ca == nil)
2816 LogMessage(L_ERROR, "CFArrayCreate error");
2817 }
2818
2819 /*CFRelease(identity);*/
2820 }
2821
2822 /*CFRelease(srchRef);*/
2823 }
2824
2825 /*CFRelease(kcRef);*/
2826 }
2827
2828 return ca;
2829 }
2830 #endif /* HAVE_CDSASSL */
2831
2832
2833 /*
2834 * End of "$Id$".
2835 */