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