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