]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/conf.c
Fix log file permissions and cleanup checks for run user (STR #686)
[thirdparty/cups.git] / scheduler / conf.c
1 /*
2 * "$Id: conf.c,v 1.145 2004/04/20 13:34:49 mike Exp $"
3 *
4 * Configuration routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2004 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-3111 USA
19 *
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
26 * ReadConfiguration() - Read the cupsd.conf file.
27 * read_configuration() - Read a configuration file.
28 * read_location() - Read a <Location path> definition.
29 * get_address() - Get an address + port number from a line.
30 * CDSAGetServerCerts() - Convert a keychain name into the CFArrayRef
31 * required by SSLSetCertificate.
32 */
33
34 /*
35 * Include necessary headers...
36 */
37
38 #include "cupsd.h"
39 #include <stdarg.h>
40 #include <pwd.h>
41 #include <grp.h>
42
43 #ifdef HAVE_CDSASSL
44 # include <Security/SecureTransport.h>
45 # include <Security/SecIdentitySearch.h>
46 #endif /* HAVE_CDSASSL */
47
48 #ifdef HAVE_VSYSLOG
49 # include <syslog.h>
50 #endif /* HAVE_VSYSLOG */
51
52
53 /*
54 * Possibly missing network definitions...
55 */
56
57 #ifndef INADDR_NONE
58 # define INADDR_NONE 0xffffffff
59 #endif /* !INADDR_NONE */
60
61
62 /*
63 * Configuration variable structure...
64 */
65
66 typedef struct
67 {
68 char *name; /* Name of variable */
69 void *ptr; /* Pointer to variable */
70 int type; /* Type (int, string, address) */
71 } var_t;
72
73 #define VAR_INTEGER 0
74 #define VAR_STRING 1
75 #define VAR_BOOLEAN 2
76
77
78 /*
79 * Local globals...
80 */
81
82 static var_t variables[] =
83 {
84 { "AccessLog", &AccessLog, VAR_STRING },
85 { "AutoPurgeJobs", &JobAutoPurge, VAR_BOOLEAN },
86 { "BrowseInterval", &BrowseInterval, VAR_INTEGER },
87 { "BrowsePort", &BrowsePort, VAR_INTEGER },
88 { "BrowseShortNames", &BrowseShortNames, VAR_BOOLEAN },
89 { "BrowseTimeout", &BrowseTimeout, VAR_INTEGER },
90 { "Browsing", &Browsing, VAR_BOOLEAN },
91 { "Classification", &Classification, VAR_STRING },
92 { "ClassifyOverride", &ClassifyOverride, VAR_BOOLEAN },
93 { "ConfigFilePerm", &ConfigFilePerm, VAR_INTEGER },
94 { "DataDir", &DataDir, VAR_STRING },
95 { "DefaultCharset", &DefaultCharset, VAR_STRING },
96 { "DefaultLanguage", &DefaultLanguage, VAR_STRING },
97 { "DocumentRoot", &DocumentRoot, VAR_STRING },
98 { "ErrorLog", &ErrorLog, VAR_STRING },
99 { "FaxRetryLimit", &FaxRetryLimit, VAR_INTEGER },
100 { "FaxRetryInterval", &FaxRetryInterval, VAR_INTEGER },
101 { "FileDevice", &FileDevice, VAR_BOOLEAN },
102 { "FilterLimit", &FilterLimit, VAR_INTEGER },
103 { "FilterNice", &FilterNice, VAR_INTEGER },
104 { "FontPath", &FontPath, VAR_STRING },
105 { "HideImplicitMembers", &HideImplicitMembers, VAR_BOOLEAN },
106 { "ImplicitClasses", &ImplicitClasses, VAR_BOOLEAN },
107 { "ImplicitAnyClasses", &ImplicitAnyClasses, VAR_BOOLEAN },
108 { "KeepAliveTimeout", &KeepAliveTimeout, VAR_INTEGER },
109 { "KeepAlive", &KeepAlive, VAR_BOOLEAN },
110 { "LimitRequestBody", &MaxRequestSize, VAR_INTEGER },
111 { "ListenBackLog", &ListenBackLog, VAR_INTEGER },
112 { "LogFilePerm", &LogFilePerm, VAR_INTEGER },
113 { "MaxClients", &MaxClients, VAR_INTEGER },
114 { "MaxClientsPerHost", &MaxClientsPerHost, VAR_INTEGER },
115 { "MaxCopies", &MaxCopies, VAR_INTEGER },
116 { "MaxJobs", &MaxJobs, VAR_INTEGER },
117 { "MaxJobsPerPrinter", &MaxJobsPerPrinter, VAR_INTEGER },
118 { "MaxJobsPerUser", &MaxJobsPerUser, VAR_INTEGER },
119 { "MaxLogSize", &MaxLogSize, VAR_INTEGER },
120 { "MaxPrinterHistory", &MaxPrinterHistory, VAR_INTEGER },
121 { "MaxRequestSize", &MaxRequestSize, VAR_INTEGER },
122 { "PageLog", &PageLog, VAR_STRING },
123 { "PreserveJobFiles", &JobFiles, VAR_BOOLEAN },
124 { "PreserveJobHistory", &JobHistory, VAR_BOOLEAN },
125 { "Printcap", &Printcap, VAR_STRING },
126 { "PrintcapGUI", &PrintcapGUI, VAR_STRING },
127 { "RemoteRoot", &RemoteRoot, VAR_STRING },
128 { "RequestRoot", &RequestRoot, VAR_STRING },
129 { "RIPCache", &RIPCache, VAR_STRING },
130 { "RunAsUser", &RunAsUser, VAR_BOOLEAN },
131 { "RootCertDuration", &RootCertDuration, VAR_INTEGER },
132 { "ServerAdmin", &ServerAdmin, VAR_STRING },
133 { "ServerBin", &ServerBin, VAR_STRING },
134 #ifdef HAVE_SSL
135 { "ServerCertificate", &ServerCertificate, VAR_STRING },
136 # if defined(HAVE_LIBSSL) || defined(HAVE_GNUTLS)
137 { "ServerKey", &ServerKey, VAR_STRING },
138 # endif /* HAVE_LIBSSL || HAVE_GNUTLS */
139 #endif /* HAVE_SSL */
140 { "ServerName", &ServerName, VAR_STRING },
141 { "ServerRoot", &ServerRoot, VAR_STRING },
142 { "TempDir", &TempDir, VAR_STRING },
143 { "Timeout", &Timeout, VAR_INTEGER }
144 };
145 #define NUM_VARS (sizeof(variables) / sizeof(variables[0]))
146
147
148 /*
149 * Local functions...
150 */
151
152 static int read_configuration(cups_file_t *fp);
153 static int read_location(cups_file_t *fp, char *name, int linenum);
154 static int get_address(char *value, unsigned defaddress, int defport,
155 struct sockaddr_in *address);
156
157 #ifdef HAVE_CDSASSL
158 static CFArrayRef CDSAGetServerCerts();
159 #endif /* HAVE_CDSASSL */
160
161
162 /*
163 * 'ReadConfiguration()' - Read the cupsd.conf file.
164 */
165
166 int /* O - 1 on success, 0 otherwise */
167 ReadConfiguration(void)
168 {
169 int i; /* Looping var */
170 cups_file_t *fp; /* Configuration file */
171 int status; /* Return status */
172 char temp[1024], /* Temporary buffer */
173 *slash; /* Directory separator */
174 char type[MIME_MAX_SUPER + MIME_MAX_TYPE];
175 /* MIME type name */
176 char *language; /* Language string */
177 struct passwd *user; /* Default user */
178 struct group *group; /* Default group */
179 char *old_serverroot, /* Old ServerRoot */
180 *old_requestroot; /* Old RequestRoot */
181
182
183 /*
184 * Shutdown the server...
185 */
186
187 StopServer();
188
189 /*
190 * Save the old root paths...
191 */
192
193 old_serverroot = NULL;
194 SetString(&old_serverroot, ServerRoot);
195 old_requestroot = NULL;
196 SetString(&old_requestroot, RequestRoot);
197
198 /*
199 * Reset the server configuration data...
200 */
201
202 DeleteAllLocations();
203
204 if (NumBrowsers > 0)
205 {
206 free(Browsers);
207
208 NumBrowsers = 0;
209 }
210
211 if (NumPolled > 0)
212 {
213 free(Polled);
214
215 NumPolled = 0;
216 }
217
218 if (NumRelays > 0)
219 {
220 for (i = 0; i < NumRelays; i ++)
221 if (Relays[i].from.type == AUTH_NAME)
222 free(Relays[i].from.mask.name.name);
223
224 free(Relays);
225
226 NumRelays = 0;
227 }
228
229 if (NumListeners > 0)
230 {
231 free(Listeners);
232
233 NumListeners = 0;
234 }
235
236 /*
237 * String options...
238 */
239
240 gethostname(temp, sizeof(temp));
241 SetString(&ServerName, temp);
242 SetStringf(&ServerAdmin, "root@%s", temp);
243 SetString(&ServerBin, CUPS_SERVERBIN);
244 SetString(&RequestRoot, CUPS_REQUESTS);
245 SetString(&DocumentRoot, CUPS_DOCROOT);
246 SetString(&DataDir, CUPS_DATADIR);
247 SetString(&AccessLog, CUPS_LOGDIR "/access_log");
248 SetString(&ErrorLog, CUPS_LOGDIR "/error_log");
249 SetString(&PageLog, CUPS_LOGDIR "/page_log");
250 SetString(&Printcap, "/etc/printcap");
251 SetString(&PrintcapGUI, "/usr/bin/glpoptions");
252 SetString(&FontPath, CUPS_FONTPATH);
253 SetString(&RemoteRoot, "remroot");
254
255 strlcpy(temp, ConfigurationFile, sizeof(temp));
256 if ((slash = strrchr(temp, '/')) != NULL)
257 *slash = '\0';
258
259 SetString(&ServerRoot, temp);
260
261 ClearString(&Classification);
262 ClassifyOverride = 0;
263
264 #ifdef HAVE_SSL
265 # ifdef HAVE_CDSASSL
266 SetString(&ServerCertificate, "/var/root/Library/Keychains/CUPS");
267 # else
268 SetString(&ServerCertificate, "ssl/server.crt");
269 SetString(&ServerKey, "ssl/server.key");
270 # endif /* HAVE_CDSASSL */
271 #endif /* HAVE_SSL */
272
273 if ((language = DEFAULT_LANGUAGE) == NULL)
274 language = "en";
275 else if (strcmp(language, "C") == 0 || strcmp(language, "POSIX") == 0)
276 language = "en";
277
278 SetString(&DefaultLanguage, language);
279 SetString(&DefaultCharset, DEFAULT_CHARSET);
280
281 SetString(&RIPCache, "8m");
282
283 if (getenv("TMPDIR") == NULL)
284 SetString(&TempDir, CUPS_REQUESTS "/tmp");
285 else
286 SetString(&TempDir, getenv("TMPDIR"));
287
288 /*
289 * Find the default system group: "sys", "system", or "root"...
290 */
291
292 group = getgrnam(CUPS_DEFAULT_GROUP);
293 endgrent();
294
295 NumSystemGroups = 0;
296
297 if (group != NULL)
298 {
299 SetString(&SystemGroups[0], CUPS_DEFAULT_GROUP);
300 Group = group->gr_gid;
301 }
302 else
303 {
304 group = getgrgid(0);
305 endgrent();
306
307 if (group != NULL)
308 {
309 SetString(&SystemGroups[0], group->gr_name);
310 Group = 0;
311 }
312 else
313 {
314 SetString(&SystemGroups[0], "unknown");
315 Group = 0;
316 }
317 }
318
319 /*
320 * Find the default user...
321 */
322
323 if ((user = getpwnam(CUPS_DEFAULT_USER)) == NULL)
324 User = 1; /* Force to a non-priviledged account */
325 else
326 User = user->pw_uid;
327
328 endpwent();
329
330 /*
331 * Numeric options...
332 */
333
334 ConfigFilePerm = 0640;
335 LogFilePerm = 0644;
336
337 FaxRetryLimit = 5;
338 FaxRetryInterval = 300;
339 FileDevice = FALSE;
340 FilterLevel = 0;
341 FilterLimit = 0;
342 FilterNice = 0;
343 HostNameLookups = FALSE;
344 ImplicitClasses = TRUE;
345 ImplicitAnyClasses = FALSE;
346 HideImplicitMembers = TRUE;
347 KeepAlive = TRUE;
348 KeepAliveTimeout = DEFAULT_KEEPALIVE;
349 ListenBackLog = SOMAXCONN;
350 LogLevel = L_ERROR;
351 MaxClients = 100;
352 MaxClientsPerHost = 0;
353 MaxLogSize = 1024 * 1024;
354 MaxPrinterHistory = 10;
355 MaxRequestSize = 0;
356 RootCertDuration = 300;
357 RunAsUser = FALSE;
358 Timeout = DEFAULT_TIMEOUT;
359
360 BrowseInterval = DEFAULT_INTERVAL;
361 BrowsePort = ippPort();
362 BrowseProtocols = BROWSE_CUPS;
363 BrowseShortNames = TRUE;
364 BrowseTimeout = DEFAULT_TIMEOUT;
365 Browsing = TRUE;
366
367 JobHistory = DEFAULT_HISTORY;
368 JobFiles = DEFAULT_FILES;
369 JobAutoPurge = 0;
370 MaxJobs = 500;
371 MaxJobsPerUser = 0;
372 MaxJobsPerPrinter = 0;
373 MaxCopies = 100;
374
375 /*
376 * Read the configuration file...
377 */
378
379 if ((fp = cupsFileOpen(ConfigurationFile, "r")) == NULL)
380 return (0);
381
382 status = read_configuration(fp);
383
384 cupsFileClose(fp);
385
386 if (!status)
387 return (0);
388
389 if (RunAsUser)
390 RunUser = User;
391 else
392 RunUser = getuid();
393
394 /*
395 * Use the default system group if none was supplied in cupsd.conf...
396 */
397
398 if (NumSystemGroups == 0)
399 NumSystemGroups ++;
400
401 /*
402 * Get the access control list for browsing...
403 */
404
405 BrowseACL = FindLocation("CUPS_INTERNAL_BROWSE_ACL");
406
407 /*
408 * Open the system log for cupsd if necessary...
409 */
410
411 #ifdef HAVE_VSYSLOG
412 if (strcmp(AccessLog, "syslog") == 0 ||
413 strcmp(ErrorLog, "syslog") == 0 ||
414 strcmp(PageLog, "syslog") == 0)
415 openlog("cupsd", LOG_PID | LOG_NOWAIT | LOG_NDELAY, LOG_LPR);
416 #endif /* HAVE_VSYSLOG */
417
418 /*
419 * Log the configuration file that was used...
420 */
421
422 LogMessage(L_INFO, "Loaded configuration file \"%s\"", ConfigurationFile);
423
424 /*
425 * Check that we have at least one listen/port line; if not, report this
426 * as an error and exit!
427 */
428
429 if (NumListeners == 0)
430 {
431 /*
432 * No listeners!
433 */
434
435 LogMessage(L_EMERG, "No valid Listen or Port lines were found in the configuration file!");
436
437 /*
438 * Commit suicide...
439 */
440
441 kill(getpid(), SIGTERM);
442 }
443
444 /*
445 * Set the default locale using the language and charset...
446 */
447
448 SetStringf(&DefaultLocale, "%s.%s", DefaultLanguage, DefaultCharset);
449
450 /*
451 * Update all relative filenames to include the full path from ServerRoot...
452 */
453
454 if (DocumentRoot[0] != '/')
455 SetStringf(&DocumentRoot, "%s/%s", ServerRoot, DocumentRoot);
456
457 if (RequestRoot[0] != '/')
458 SetStringf(&RequestRoot, "%s/%s", ServerRoot, RequestRoot);
459
460 if (ServerBin[0] != '/')
461 SetStringf(&ServerBin, "%s/%s", ServerRoot, ServerBin);
462
463 #ifdef HAVE_SSL
464 if (ServerCertificate[0] != '/')
465 SetStringf(&ServerCertificate, "%s/%s", ServerRoot, ServerCertificate);
466
467 # if defined(HAVE_LIBSSL) || defined(HAVE_GNUTLS)
468 chown(ServerCertificate, RunUser, Group);
469 chmod(ServerCertificate, ConfigFilePerm);
470
471 if (ServerKey[0] != '/')
472 SetStringf(&ServerKey, "%s/%s", ServerRoot, ServerKey);
473
474 chown(ServerKey, RunUser, Group);
475 chmod(ServerKey, ConfigFilePerm);
476 # endif /* HAVE_LIBSSL || HAVE_GNUTLS */
477 #endif /* HAVE_SSL */
478
479 /*
480 * Make sure that ServerRoot and the config files are owned and
481 * writable by the user and group in the cupsd.conf file...
482 */
483
484 chown(ServerRoot, RunUser, Group);
485 chmod(ServerRoot, 0775);
486
487 snprintf(temp, sizeof(temp), "%s/certs", ServerRoot);
488 chown(temp, RunUser, Group);
489 chmod(temp, 0711);
490
491 snprintf(temp, sizeof(temp), "%s/ppd", ServerRoot);
492 chown(temp, RunUser, Group);
493 chmod(temp, 0755);
494
495 snprintf(temp, sizeof(temp), "%s/ssl", ServerRoot);
496 chown(temp, RunUser, Group);
497 chmod(temp, 0700);
498
499 snprintf(temp, sizeof(temp), "%s/cupsd.conf", ServerRoot);
500 chown(temp, RunUser, Group);
501 chmod(temp, ConfigFilePerm);
502
503 snprintf(temp, sizeof(temp), "%s/classes.conf", ServerRoot);
504 chown(temp, RunUser, Group);
505 #ifdef __APPLE__
506 chmod(temp, 0600);
507 #else
508 chmod(temp, ConfigFilePerm);
509 #endif /* __APPLE__ */
510
511 snprintf(temp, sizeof(temp), "%s/printers.conf", ServerRoot);
512 chown(temp, RunUser, Group);
513 #ifdef __APPLE__
514 chmod(temp, 0600);
515 #else
516 chmod(temp, ConfigFilePerm);
517 #endif /* __APPLE__ */
518
519 snprintf(temp, sizeof(temp), "%s/passwd.md5", ServerRoot);
520 chown(temp, User, Group);
521 chmod(temp, 0600);
522
523 /*
524 * Make sure the request and temporary directories have the right
525 * permissions...
526 */
527
528 chown(RequestRoot, RunUser, Group);
529 chmod(RequestRoot, 0710);
530
531 if (strncmp(TempDir, RequestRoot, strlen(RequestRoot)) == 0)
532 {
533 /*
534 * Only update ownership and permissions if the CUPS temp directory
535 * is under the spool directory...
536 */
537
538 chown(TempDir, RunUser, Group);
539 chmod(TempDir, 01770);
540 }
541
542 /*
543 * Check the MaxClients setting, and then allocate memory for it...
544 */
545
546 if (MaxClients > (MaxFDs / 3) || MaxClients <= 0)
547 {
548 if (MaxClients > 0)
549 LogMessage(L_INFO, "MaxClients limited to 1/3 of the file descriptor limit (%d)...",
550 MaxFDs);
551
552 MaxClients = MaxFDs / 3;
553 }
554
555 if ((Clients = calloc(sizeof(client_t), MaxClients)) == NULL)
556 {
557 LogMessage(L_ERROR, "ReadConfiguration: Unable to allocate memory for %d clients: %s",
558 MaxClients, strerror(errno));
559 exit(1);
560 }
561 else
562 LogMessage(L_INFO, "Configured for up to %d clients.", MaxClients);
563
564 if (Classification && strcasecmp(Classification, "none") == 0)
565 ClearString(&Classification);
566
567 if (Classification)
568 LogMessage(L_INFO, "Security set to \"%s\"", Classification);
569
570 /*
571 * Update the MaxClientsPerHost value, as needed...
572 */
573
574 if (MaxClientsPerHost <= 0)
575 MaxClientsPerHost = MaxClients;
576
577 if (MaxClientsPerHost > MaxClients)
578 MaxClientsPerHost = MaxClients;
579
580 LogMessage(L_INFO, "Allowing up to %d client connections per host.",
581 MaxClientsPerHost);
582
583 /*
584 * If we are doing a full reload or the server root has changed, flush
585 * the jobs, printers, etc. and start from scratch...
586 */
587
588 if (NeedReload == RELOAD_ALL ||
589 !old_serverroot || !ServerRoot || strcmp(old_serverroot, ServerRoot) ||
590 !old_requestroot || !RequestRoot || strcmp(old_requestroot, RequestRoot))
591 {
592 LogMessage(L_INFO, "Full reload is required.");
593
594 /*
595 * Free all memory...
596 */
597
598 FreeAllJobs();
599 DeleteAllClasses();
600 DeleteAllPrinters();
601
602 DefaultPrinter = NULL;
603
604 if (Devices)
605 {
606 ippDelete(Devices);
607 Devices = NULL;
608 }
609
610 if (PPDs)
611 {
612 ippDelete(PPDs);
613 PPDs = NULL;
614 }
615
616 if (MimeDatabase != NULL)
617 mimeDelete(MimeDatabase);
618
619 if (NumMimeTypes)
620 {
621 for (i = 0; i < NumMimeTypes; i ++)
622 free((void *)MimeTypes[i]);
623
624 free(MimeTypes);
625 }
626
627 /*
628 * Read the MIME type and conversion database...
629 */
630
631 snprintf(temp, sizeof(temp), "%s/filter", ServerBin);
632
633 MimeDatabase = mimeNew();
634 mimeMerge(MimeDatabase, ServerRoot, temp);
635
636 /*
637 * Create a list of MIME types for the document-format-supported
638 * attribute...
639 */
640
641 NumMimeTypes = MimeDatabase->num_types;
642 if (!mimeType(MimeDatabase, "application", "octet-stream"))
643 NumMimeTypes ++;
644
645 MimeTypes = calloc(NumMimeTypes, sizeof(const char *));
646
647 for (i = 0; i < MimeDatabase->num_types; i ++)
648 {
649 snprintf(type, sizeof(type), "%s/%s", MimeDatabase->types[i]->super,
650 MimeDatabase->types[i]->type);
651
652 MimeTypes[i] = strdup(type);
653 }
654
655 if (i < NumMimeTypes)
656 MimeTypes[i] = strdup("application/octet-stream");
657
658 /*
659 * Load banners...
660 */
661
662 snprintf(temp, sizeof(temp), "%s/banners", DataDir);
663 LoadBanners(temp);
664
665 /*
666 * Load printers and classes...
667 */
668
669 LoadAllPrinters();
670 LoadAllClasses();
671
672 CreateCommonData();
673
674 /*
675 * Load devices and PPDs...
676 */
677
678 snprintf(temp, sizeof(temp), "%s/backend", ServerBin);
679 LoadDevices(temp);
680
681 snprintf(temp, sizeof(temp), "%s/model", DataDir);
682 LoadPPDs(temp);
683
684 /*
685 * Load queued jobs...
686 */
687
688 LoadAllJobs();
689
690 LogMessage(L_INFO, "Full reload complete.");
691 }
692 else
693 {
694 CreateCommonData();
695
696 LogMessage(L_INFO, "Partial reload complete.");
697 }
698
699 /*
700 * Reset the reload state...
701 */
702
703 NeedReload = RELOAD_NONE;
704
705 ClearString(&old_serverroot);
706 ClearString(&old_requestroot);
707
708 /*
709 * Startup the server and return...
710 */
711
712 StartServer();
713
714 return (1);
715 }
716
717
718 /*
719 * 'read_configuration()' - Read a configuration file.
720 */
721
722 static int /* O - 1 on success, 0 on failure */
723 read_configuration(cups_file_t *fp) /* I - File to read from */
724 {
725 int i; /* Looping var */
726 int linenum; /* Current line number */
727 int len; /* Length of line */
728 char line[HTTP_MAX_BUFFER], /* Line from file */
729 name[256], /* Parameter name */
730 *nameptr, /* Pointer into name */
731 *value; /* Pointer to value */
732 int valuelen; /* Length of value */
733 var_t *var; /* Current variable */
734 unsigned address, /* Address value */
735 netmask; /* Netmask value */
736 int ip[4], /* IP address components */
737 ipcount, /* Number of components provided */
738 mask[4]; /* IP netmask components */
739 dirsvc_relay_t *relay; /* Relay data */
740 dirsvc_poll_t *poll; /* Polling data */
741 struct sockaddr_in polladdr; /* Polling address */
742 location_t *location; /* Browse location */
743 cups_file_t *incfile; /* Include file */
744 char incname[1024]; /* Include filename */
745 static unsigned netmasks[4] = /* Standard netmasks... */
746 {
747 0xff000000,
748 0xffff0000,
749 0xffffff00,
750 0xffffffff
751 };
752
753
754 /*
755 * Loop through each line in the file...
756 */
757
758 linenum = 0;
759
760 while (cupsFileGets(fp, line, sizeof(line)) != NULL)
761 {
762 linenum ++;
763
764 /*
765 * Skip comment lines...
766 */
767
768 if (line[0] == '#')
769 continue;
770
771 /*
772 * Strip trailing whitespace, if any...
773 */
774
775 len = strlen(line);
776
777 while (len > 0 && isspace(line[len - 1]))
778 {
779 len --;
780 line[len] = '\0';
781 }
782
783 /*
784 * Extract the name from the beginning of the line...
785 */
786
787 for (value = line; isspace(*value); value ++);
788
789 for (nameptr = name; *value != '\0' && !isspace(*value) &&
790 nameptr < (name + sizeof(name) - 1);)
791 *nameptr++ = *value++;
792 *nameptr = '\0';
793
794 while (isspace(*value))
795 value ++;
796
797 if (name[0] == '\0')
798 continue;
799
800 /*
801 * Decode the directive...
802 */
803
804 if (strcasecmp(name, "Include") == 0)
805 {
806 /*
807 * Include filename
808 */
809
810 if (value[0] == '/')
811 strlcpy(incname, value, sizeof(incname));
812 else
813 snprintf(incname, sizeof(incname), "%s/%s", ServerRoot, value);
814
815 if ((incfile = cupsFileOpen(incname, "rb")) == NULL)
816 LogMessage(L_ERROR, "Unable to include config file \"%s\" - %s",
817 incname, strerror(errno));
818 else
819 {
820 read_configuration(incfile);
821 cupsFileClose(incfile);
822 }
823 }
824 else if (strcasecmp(name, "<Location") == 0)
825 {
826 /*
827 * <Location path>
828 */
829
830 if (line[len - 1] == '>')
831 {
832 line[len - 1] = '\0';
833
834 linenum = read_location(fp, value, linenum);
835 if (linenum == 0)
836 return (0);
837 }
838 else
839 {
840 LogMessage(L_ERROR, "ReadConfiguration() Syntax error on line %d.",
841 linenum);
842 return (0);
843 }
844 }
845 else if (strcasecmp(name, "Port") == 0 ||
846 strcasecmp(name, "Listen") == 0)
847 {
848 /*
849 * Add a listening address to the list...
850 */
851
852 listener_t *temp; /* New listeners array */
853
854
855 if (NumListeners == 0)
856 temp = malloc(sizeof(listener_t));
857 else
858 temp = realloc(Listeners, (NumListeners + 1) * sizeof(listener_t));
859
860 if (!temp)
861 {
862 LogMessage(L_ERROR, "Unable to allocate %s at line %d - %s.",
863 name, linenum, strerror(errno));
864 continue;
865 }
866
867 Listeners = temp;
868 temp += NumListeners;
869
870 memset(temp, 0, sizeof(listener_t));
871
872 if (get_address(value, INADDR_ANY, IPP_PORT, &(temp->address)))
873 {
874 LogMessage(L_INFO, "Listening to %x:%d",
875 (unsigned)ntohl(temp->address.sin_addr.s_addr),
876 ntohs(temp->address.sin_port));
877 NumListeners ++;
878 }
879 else
880 LogMessage(L_ERROR, "Bad %s address %s at line %d.", name,
881 value, linenum);
882 }
883 #ifdef HAVE_SSL
884 else if (strcasecmp(name, "SSLPort") == 0 ||
885 strcasecmp(name, "SSLListen") == 0)
886 {
887 /*
888 * Add a listening address to the list...
889 */
890
891 listener_t *temp; /* New listeners array */
892
893
894 if (NumListeners == 0)
895 temp = malloc(sizeof(listener_t));
896 else
897 temp = realloc(Listeners, (NumListeners + 1) * sizeof(listener_t));
898
899 if (!temp)
900 {
901 LogMessage(L_ERROR, "Unable to allocate %s at line %d - %s.",
902 name, linenum, strerror(errno));
903 continue;
904 }
905
906 Listeners = temp;
907 temp += NumListeners;
908
909 if (get_address(value, INADDR_ANY, IPP_PORT, &(temp->address)))
910 {
911 LogMessage(L_INFO, "Listening to %x:%d (SSL)",
912 (unsigned)ntohl(temp->address.sin_addr.s_addr),
913 ntohs(temp->address.sin_port));
914 temp->encryption = HTTP_ENCRYPT_ALWAYS;
915 NumListeners ++;
916 }
917 else
918 LogMessage(L_ERROR, "Bad %s address %s at line %d.", name,
919 value, linenum);
920 }
921 #endif /* HAVE_SSL */
922 else if (strcasecmp(name, "BrowseAddress") == 0)
923 {
924 /*
925 * Add a browse address to the list...
926 */
927
928 dirsvc_addr_t *temp; /* New browse address array */
929
930
931 if (NumBrowsers == 0)
932 temp = malloc(sizeof(dirsvc_addr_t));
933 else
934 temp = realloc(Browsers, (NumBrowsers + 1) * sizeof(dirsvc_addr_t));
935
936 if (!temp)
937 {
938 LogMessage(L_ERROR, "Unable to allocate BrowseAddress at line %d - %s.",
939 linenum, strerror(errno));
940 continue;
941 }
942
943 Browsers = temp;
944 temp += NumBrowsers;
945
946 memset(temp, 0, sizeof(dirsvc_addr_t));
947
948 if (strcasecmp(value, "@LOCAL") == 0)
949 {
950 /*
951 * Send browse data to all local interfaces...
952 */
953
954 strcpy(temp->iface, "*");
955 NumBrowsers ++;
956 }
957 else if (strncasecmp(value, "@IF(", 4) == 0)
958 {
959 /*
960 * Send browse data to the named interface...
961 */
962
963 strlcpy(temp->iface, value + 4, sizeof(Browsers[0].iface));
964
965 nameptr = temp->iface + strlen(temp->iface) - 1;
966 if (*nameptr == ')')
967 *nameptr = '\0';
968
969 NumBrowsers ++;
970 }
971 else if (get_address(value, INADDR_NONE, BrowsePort, &(temp->to)))
972 {
973 LogMessage(L_INFO, "Sending browsing info to %x:%d",
974 (unsigned)ntohl(temp->to.sin_addr.s_addr),
975 ntohs(temp->to.sin_port));
976
977 NumBrowsers ++;
978 }
979 else
980 LogMessage(L_ERROR, "Bad BrowseAddress %s at line %d.", value,
981 linenum);
982 }
983 else if (strcasecmp(name, "BrowseOrder") == 0)
984 {
985 /*
986 * "BrowseOrder Deny,Allow" or "BrowseOrder Allow,Deny"...
987 */
988
989 if ((location = FindLocation("CUPS_INTERNAL_BROWSE_ACL")) == NULL)
990 location = AddLocation("CUPS_INTERNAL_BROWSE_ACL");
991
992 if (location == NULL)
993 LogMessage(L_ERROR, "Unable to initialize browse access control list!");
994 else if (strncasecmp(value, "deny", 4) == 0)
995 location->order_type = AUTH_ALLOW;
996 else if (strncasecmp(value, "allow", 5) == 0)
997 location->order_type = AUTH_DENY;
998 else
999 LogMessage(L_ERROR, "Unknown BrowseOrder value %s on line %d.",
1000 value, linenum);
1001 }
1002 else if (strcasecmp(name, "BrowseProtocols") == 0)
1003 {
1004 /*
1005 * "BrowseProtocol name [... name]"
1006 */
1007
1008 BrowseProtocols = 0;
1009
1010 for (; *value;)
1011 {
1012 for (valuelen = 0; value[valuelen]; valuelen ++)
1013 if (isspace(value[valuelen]) || value[valuelen] == ',')
1014 break;
1015
1016 if (value[valuelen])
1017 {
1018 value[valuelen] = '\0';
1019 valuelen ++;
1020 }
1021
1022 if (strcasecmp(value, "cups") == 0)
1023 BrowseProtocols |= BROWSE_CUPS;
1024 else if (strcasecmp(value, "slp") == 0)
1025 BrowseProtocols |= BROWSE_SLP;
1026 else if (strcasecmp(value, "ldap") == 0)
1027 BrowseProtocols |= BROWSE_LDAP;
1028 else if (strcasecmp(value, "all") == 0)
1029 BrowseProtocols |= BROWSE_ALL;
1030 else
1031 {
1032 LogMessage(L_ERROR, "Unknown browse protocol \"%s\" on line %d.",
1033 value, linenum);
1034 break;
1035 }
1036
1037 for (value += valuelen; *value; value ++)
1038 if (!isspace(*value) || *value != ',')
1039 break;
1040 }
1041 }
1042 else if (strcasecmp(name, "BrowseAllow") == 0 ||
1043 strcasecmp(name, "BrowseDeny") == 0)
1044 {
1045 /*
1046 * BrowseAllow [From] host/ip...
1047 * BrowseDeny [From] host/ip...
1048 */
1049
1050 if ((location = FindLocation("CUPS_INTERNAL_BROWSE_ACL")) == NULL)
1051 location = AddLocation("CUPS_INTERNAL_BROWSE_ACL");
1052
1053 if (location == NULL)
1054 LogMessage(L_ERROR, "Unable to initialize browse access control list!");
1055 else
1056 {
1057 if (strncasecmp(value, "from ", 5) == 0)
1058 {
1059 /*
1060 * Strip leading "from"...
1061 */
1062
1063 value += 5;
1064
1065 while (isspace(*value))
1066 value ++;
1067 }
1068
1069 /*
1070 * Figure out what form the allow/deny address takes:
1071 *
1072 * All
1073 * None
1074 * *.domain.com
1075 * .domain.com
1076 * host.domain.com
1077 * nnn.*
1078 * nnn.nnn.*
1079 * nnn.nnn.nnn.*
1080 * nnn.nnn.nnn.nnn
1081 * nnn.nnn.nnn.nnn/mm
1082 * nnn.nnn.nnn.nnn/mmm.mmm.mmm.mmm
1083 */
1084
1085 if (strcasecmp(value, "all") == 0)
1086 {
1087 /*
1088 * All hosts...
1089 */
1090
1091 if (strcasecmp(name, "BrowseAllow") == 0)
1092 AllowIP(location, 0, 0);
1093 else
1094 DenyIP(location, 0, 0);
1095 }
1096 else if (strcasecmp(value, "none") == 0)
1097 {
1098 /*
1099 * No hosts...
1100 */
1101
1102 if (strcasecmp(name, "BrowseAllow") == 0)
1103 AllowIP(location, ~0, 0);
1104 else
1105 DenyIP(location, ~0, 0);
1106 }
1107 else if (value[0] == '*' || value[0] == '.' || !isdigit(value[0]))
1108 {
1109 /*
1110 * Host or domain name...
1111 */
1112
1113 if (value[0] == '*')
1114 value ++;
1115
1116 if (strcasecmp(name, "BrowseAllow") == 0)
1117 AllowHost(location, value);
1118 else
1119 DenyHost(location, value);
1120 }
1121 else
1122 {
1123 /*
1124 * One of many IP address forms...
1125 */
1126
1127 memset(ip, 0, sizeof(ip));
1128 ipcount = sscanf(value, "%d.%d.%d.%d", ip + 0, ip + 1, ip + 2, ip + 3);
1129 address = (((((ip[0] << 8) | ip[1]) << 8) | ip[2]) << 8) | ip[3];
1130
1131 if ((value = strchr(value, '/')) != NULL)
1132 {
1133 value ++;
1134 memset(mask, 0, sizeof(mask));
1135 switch (sscanf(value, "%d.%d.%d.%d", mask + 0, mask + 1,
1136 mask + 2, mask + 3))
1137 {
1138 case 1 :
1139 netmask = (0xffffffff << (32 - mask[0])) & 0xffffffff;
1140 break;
1141 case 4 :
1142 netmask = (((((mask[0] << 8) | mask[1]) << 8) |
1143 mask[2]) << 8) | mask[3];
1144 break;
1145 default :
1146 LogMessage(L_ERROR, "Bad netmask value %s on line %d.",
1147 value, linenum);
1148 netmask = 0xffffffff;
1149 break;
1150 }
1151 }
1152 else
1153 netmask = netmasks[ipcount - 1];
1154
1155 if ((address & ~netmask) != 0)
1156 {
1157 LogMessage(L_WARN, "Discarding extra bits in %s address %08x for netmask %08x...",
1158 name, address, netmask);
1159 address &= netmask;
1160 }
1161
1162 if (strcasecmp(name, "BrowseAllow") == 0)
1163 AllowIP(location, address, netmask);
1164 else
1165 DenyIP(location, address, netmask);
1166 }
1167 }
1168 }
1169 else if (strcasecmp(name, "BrowseRelay") == 0)
1170 {
1171 /*
1172 * BrowseRelay [from] source [to] destination
1173 */
1174
1175 if (NumRelays == 0)
1176 relay = malloc(sizeof(dirsvc_relay_t));
1177 else
1178 relay = realloc(Relays, (NumRelays + 1) * sizeof(dirsvc_relay_t));
1179
1180 if (!relay)
1181 {
1182 LogMessage(L_ERROR, "Unable to allocate BrowseRelay at line %d - %s.",
1183 linenum, strerror(errno));
1184 continue;
1185 }
1186
1187 Relays = relay;
1188 relay += NumRelays;
1189
1190 memset(relay, 0, sizeof(dirsvc_relay_t));
1191
1192 if (strncasecmp(value, "from ", 5) == 0)
1193 {
1194 /*
1195 * Strip leading "from"...
1196 */
1197
1198 value += 5;
1199
1200 while (isspace(*value))
1201 value ++;
1202 }
1203
1204 /*
1205 * Figure out what form the from address takes:
1206 *
1207 * *.domain.com
1208 * .domain.com
1209 * host.domain.com
1210 * nnn.*
1211 * nnn.nnn.*
1212 * nnn.nnn.nnn.*
1213 * nnn.nnn.nnn.nnn
1214 * nnn.nnn.nnn.nnn/mm
1215 * nnn.nnn.nnn.nnn/mmm.mmm.mmm.mmm
1216 */
1217
1218 if (value[0] == '*' || value[0] == '.' || !isdigit(value[0]))
1219 {
1220 /*
1221 * Host or domain name...
1222 */
1223
1224 if (value[0] == '*')
1225 value ++;
1226
1227 relay->from.type = AUTH_NAME;
1228 relay->from.mask.name.name = strdup(value);
1229 relay->from.mask.name.length = strlen(value);
1230 }
1231 else
1232 {
1233 /*
1234 * One of many IP address forms...
1235 */
1236
1237 memset(ip, 0, sizeof(ip));
1238 ipcount = sscanf(value, "%d.%d.%d.%d", ip + 0, ip + 1, ip + 2, ip + 3);
1239 address = (((((ip[0] << 8) | ip[1]) << 8) | ip[2]) << 8) | ip[3];
1240
1241 for (; *value; value ++)
1242 if (*value == '/' || isspace(*value))
1243 break;
1244
1245 if (*value == '/')
1246 {
1247 value ++;
1248 memset(mask, 0, sizeof(mask));
1249 switch (sscanf(value, "%d.%d.%d.%d", mask + 0, mask + 1,
1250 mask + 2, mask + 3))
1251 {
1252 case 1 :
1253 netmask = (0xffffffff << (32 - mask[0])) & 0xffffffff;
1254 break;
1255 case 4 :
1256 netmask = (((((mask[0] << 8) | mask[1]) << 8) |
1257 mask[2]) << 8) | mask[3];
1258 break;
1259 default :
1260 LogMessage(L_ERROR, "Bad netmask value %s on line %d.",
1261 value, linenum);
1262 netmask = 0xffffffff;
1263 break;
1264 }
1265 }
1266 else
1267 netmask = netmasks[ipcount - 1];
1268
1269 relay->from.type = AUTH_IP;
1270 relay->from.mask.ip.address = address;
1271 relay->from.mask.ip.netmask = netmask;
1272 }
1273
1274 /*
1275 * Skip value and trailing whitespace...
1276 */
1277
1278 for (; *value; value ++)
1279 if (isspace(*value))
1280 break;
1281
1282 while (isspace(*value))
1283 value ++;
1284
1285 if (strncasecmp(value, "to ", 3) == 0)
1286 {
1287 /*
1288 * Strip leading "to"...
1289 */
1290
1291 value += 3;
1292
1293 while (isspace(*value))
1294 value ++;
1295 }
1296
1297 /*
1298 * Get "to" address and port...
1299 */
1300
1301 if (get_address(value, INADDR_BROADCAST, BrowsePort, &(relay->to)))
1302 {
1303 if (relay->from.type == AUTH_NAME)
1304 LogMessage(L_INFO, "Relaying from %s to %x:%d",
1305 relay->from.mask.name.name,
1306 (unsigned)ntohl(relay->to.sin_addr.s_addr),
1307 ntohs(relay->to.sin_port));
1308 else
1309 LogMessage(L_INFO, "Relaying from %x/%x to %x:%d",
1310 relay->from.mask.ip.address, relay->from.mask.ip.netmask,
1311 (unsigned)ntohl(relay->to.sin_addr.s_addr),
1312 ntohs(relay->to.sin_port));
1313
1314 NumRelays ++;
1315 }
1316 else
1317 {
1318 if (relay->from.type == AUTH_NAME)
1319 free(relay->from.mask.name.name);
1320
1321 LogMessage(L_ERROR, "Bad relay address %s at line %d.", value, linenum);
1322 }
1323 }
1324 else if (strcasecmp(name, "BrowsePoll") == 0)
1325 {
1326 /*
1327 * BrowsePoll address[:port]
1328 */
1329
1330 if (NumPolled == 0)
1331 poll = malloc(sizeof(dirsvc_poll_t));
1332 else
1333 poll = realloc(Polled, (NumPolled + 1) * sizeof(dirsvc_poll_t));
1334
1335 if (!poll)
1336 {
1337 LogMessage(L_ERROR, "Unable to allocate BrowsePoll at line %d - %s.",
1338 linenum, strerror(errno));
1339 continue;
1340 }
1341
1342 Polled = poll;
1343 poll += NumPolled;
1344
1345 /*
1346 * Get poll address and port...
1347 */
1348
1349 if (get_address(value, INADDR_NONE, ippPort(), &polladdr))
1350 {
1351 LogMessage(L_INFO, "Polling %x:%d",
1352 (unsigned)ntohl(polladdr.sin_addr.s_addr),
1353 ntohs(polladdr.sin_port));
1354
1355 NumPolled ++;
1356 memset(poll, 0, sizeof(dirsvc_poll_t));
1357
1358 address = ntohl(polladdr.sin_addr.s_addr);
1359
1360 sprintf(poll->hostname, "%d.%d.%d.%d", address >> 24,
1361 (address >> 16) & 255, (address >> 8) & 255, address & 255);
1362 poll->port = ntohs(polladdr.sin_port);
1363 }
1364 else
1365 LogMessage(L_ERROR, "Bad poll address %s at line %d.", value, linenum);
1366 }
1367 else if (strcasecmp(name, "User") == 0)
1368 {
1369 /*
1370 * User ID to run as...
1371 */
1372
1373 if (isdigit(value[0]))
1374 User = atoi(value);
1375 else
1376 {
1377 struct passwd *p; /* Password information */
1378
1379 endpwent();
1380 p = getpwnam(value);
1381
1382 if (p != NULL)
1383 User = p->pw_uid;
1384 else
1385 LogMessage(L_WARN, "ReadConfiguration() Unknown username \"%s\"",
1386 value);
1387 }
1388 }
1389 else if (strcasecmp(name, "Group") == 0)
1390 {
1391 /*
1392 * Group ID to run as...
1393 */
1394
1395 if (isdigit(value[0]))
1396 Group = atoi(value);
1397 else
1398 {
1399 struct group *g; /* Group information */
1400
1401 endgrent();
1402 g = getgrnam(value);
1403
1404 if (g != NULL)
1405 Group = g->gr_gid;
1406 else
1407 LogMessage(L_WARN, "ReadConfiguration() Unknown groupname \"%s\"",
1408 value);
1409 }
1410 }
1411 else if (strcasecmp(name, "SystemGroup") == 0)
1412 {
1413 /*
1414 * System (admin) group(s)...
1415 */
1416
1417 char *valueptr; /* Pointer into value */
1418
1419
1420 for (i = NumSystemGroups; *value && i < MAX_SYSTEM_GROUPS; i ++)
1421 {
1422 for (valueptr = value; *valueptr; valueptr ++)
1423 if (isspace(*valueptr) || *valueptr == ',')
1424 break;
1425
1426 if (*valueptr)
1427 *valueptr++ = '\0';
1428
1429 SetString(SystemGroups + i, value);
1430
1431 value = valueptr;
1432
1433 while (*value == ',' || isspace(*value))
1434 value ++;
1435 }
1436
1437 if (i)
1438 NumSystemGroups = i;
1439 }
1440 else if (strcasecmp(name, "HostNameLookups") == 0)
1441 {
1442 /*
1443 * Do hostname lookups?
1444 */
1445
1446 if (strcasecmp(value, "off") == 0)
1447 HostNameLookups = 0;
1448 else if (strcasecmp(value, "on") == 0)
1449 HostNameLookups = 1;
1450 else if (strcasecmp(value, "double") == 0)
1451 HostNameLookups = 2;
1452 else
1453 LogMessage(L_WARN, "ReadConfiguration() Unknown HostNameLookups %s on line %d.",
1454 value, linenum);
1455 }
1456 else if (strcasecmp(name, "LogLevel") == 0)
1457 {
1458 /*
1459 * Amount of logging to do...
1460 */
1461
1462 if (strcasecmp(value, "debug2") == 0)
1463 LogLevel = L_DEBUG2;
1464 else if (strcasecmp(value, "debug") == 0)
1465 LogLevel = L_DEBUG;
1466 else if (strcasecmp(value, "info") == 0)
1467 LogLevel = L_INFO;
1468 else if (strcasecmp(value, "notice") == 0)
1469 LogLevel = L_NOTICE;
1470 else if (strcasecmp(value, "warn") == 0)
1471 LogLevel = L_WARN;
1472 else if (strcasecmp(value, "error") == 0)
1473 LogLevel = L_ERROR;
1474 else if (strcasecmp(value, "crit") == 0)
1475 LogLevel = L_CRIT;
1476 else if (strcasecmp(value, "alert") == 0)
1477 LogLevel = L_ALERT;
1478 else if (strcasecmp(value, "emerg") == 0)
1479 LogLevel = L_EMERG;
1480 else if (strcasecmp(value, "none") == 0)
1481 LogLevel = L_NONE;
1482 else
1483 LogMessage(L_WARN, "Unknown LogLevel %s on line %d.", value, linenum);
1484 }
1485 else if (strcasecmp(name, "PrintcapFormat") == 0)
1486 {
1487 /*
1488 * Format of printcap file?
1489 */
1490
1491 if (strcasecmp(value, "bsd") == 0)
1492 PrintcapFormat = PRINTCAP_BSD;
1493 else if (strcasecmp(value, "solaris") == 0)
1494 PrintcapFormat = PRINTCAP_SOLARIS;
1495 else
1496 LogMessage(L_WARN, "ReadConfiguration() Unknown PrintcapFormat %s on line %d.",
1497 value, linenum);
1498 }
1499 else
1500 {
1501 /*
1502 * Find a simple variable in the list...
1503 */
1504
1505 for (i = NUM_VARS, var = variables; i > 0; i --, var ++)
1506 if (strcasecmp(name, var->name) == 0)
1507 break;
1508
1509 if (i == 0)
1510 {
1511 /*
1512 * Unknown directive! Output an error message and continue...
1513 */
1514
1515 LogMessage(L_ERROR, "Unknown directive %s on line %d.", name,
1516 linenum);
1517 continue;
1518 }
1519
1520 switch (var->type)
1521 {
1522 case VAR_INTEGER :
1523 {
1524 int n; /* Number */
1525 char *units; /* Units */
1526
1527
1528 n = strtol(value, &units, 0);
1529
1530 if (units && *units)
1531 {
1532 if (tolower(units[0] & 255) == 'g')
1533 n *= 1024 * 1024 * 1024;
1534 else if (tolower(units[0] & 255) == 'm')
1535 n *= 1024 * 1024;
1536 else if (tolower(units[0] & 255) == 'k')
1537 n *= 1024;
1538 else if (tolower(units[0] & 255) == 't')
1539 n *= 262144;
1540 }
1541
1542 *((int *)var->ptr) = n;
1543 }
1544 break;
1545
1546 case VAR_BOOLEAN :
1547 if (strcasecmp(value, "true") == 0 ||
1548 strcasecmp(value, "on") == 0 ||
1549 strcasecmp(value, "enabled") == 0 ||
1550 strcasecmp(value, "yes") == 0 ||
1551 atoi(value) != 0)
1552 *((int *)var->ptr) = TRUE;
1553 else if (strcasecmp(value, "false") == 0 ||
1554 strcasecmp(value, "off") == 0 ||
1555 strcasecmp(value, "disabled") == 0 ||
1556 strcasecmp(value, "no") == 0 ||
1557 strcasecmp(value, "0") == 0)
1558 *((int *)var->ptr) = FALSE;
1559 else
1560 LogMessage(L_ERROR, "Unknown boolean value %s on line %d.",
1561 value, linenum);
1562 break;
1563
1564 case VAR_STRING :
1565 SetString((char **)var->ptr, value);
1566 break;
1567 }
1568 }
1569 }
1570
1571 return (1);
1572 }
1573
1574
1575 /*
1576 * 'read_location()' - Read a <Location path> definition.
1577 */
1578
1579 static int /* O - New line number or 0 on error */
1580 read_location(cups_file_t *fp, /* I - Configuration file */
1581 char *location, /* I - Location name/path */
1582 int linenum) /* I - Current line number */
1583 {
1584 int i; /* Looping var */
1585 location_t *loc, /* New location */
1586 *parent; /* Parent location */
1587 int len; /* Length of line */
1588 char line[HTTP_MAX_BUFFER], /* Line buffer */
1589 name[256], /* Configuration directive */
1590 *nameptr, /* Pointer into name */
1591 *value, /* Value for directive */
1592 *valptr; /* Pointer into value */
1593 unsigned address, /* Address value */
1594 netmask; /* Netmask value */
1595 int ip[4], /* IP address components */
1596 ipcount, /* Number of components provided */
1597 mask[4]; /* IP netmask components */
1598 static unsigned netmasks[4] = /* Standard netmasks... */
1599 {
1600 0xff000000,
1601 0xffff0000,
1602 0xffffff00,
1603 0xffffffff
1604 };
1605
1606
1607 if ((parent = AddLocation(location)) == NULL)
1608 return (0);
1609
1610 parent->limit = AUTH_LIMIT_ALL;
1611 loc = parent;
1612
1613 while (cupsFileGets(fp, line, sizeof(line)) != NULL)
1614 {
1615 linenum ++;
1616
1617 /*
1618 * Skip comment lines...
1619 */
1620
1621 if (line[0] == '#')
1622 continue;
1623
1624 /*
1625 * Strip trailing whitespace, if any...
1626 */
1627
1628 len = strlen(line);
1629
1630 while (len > 0 && isspace(line[len - 1] & 255))
1631 {
1632 len --;
1633 line[len] = '\0';
1634 }
1635
1636 /*
1637 * Extract the name from the beginning of the line...
1638 */
1639
1640 for (value = line; isspace(*value & 255); value ++);
1641
1642 for (nameptr = name; *value != '\0' && !isspace(*value & 255) &&
1643 nameptr < (name + sizeof(name) - 1);)
1644 *nameptr++ = *value++;
1645 *nameptr = '\0';
1646
1647 while (isspace(*value & 255))
1648 value ++;
1649
1650 if (name[0] == '\0')
1651 continue;
1652
1653 /*
1654 * Decode the directive...
1655 */
1656
1657 if (strcasecmp(name, "</Location>") == 0)
1658 return (linenum);
1659 else if (strcasecmp(name, "<Limit") == 0 ||
1660 strcasecmp(name, "<LimitExcept") == 0)
1661 {
1662 if ((loc = CopyLocation(&parent)) == NULL)
1663 return (0);
1664
1665 loc->limit = 0;
1666 while (*value)
1667 {
1668 for (valptr = value;
1669 !isspace(*valptr & 255) && *valptr != '>' && *valptr;
1670 valptr ++);
1671
1672 if (*valptr)
1673 *valptr++ = '\0';
1674
1675 if (strcmp(value, "ALL") == 0)
1676 loc->limit = AUTH_LIMIT_ALL;
1677 else if (strcmp(value, "GET") == 0)
1678 loc->limit |= AUTH_LIMIT_GET;
1679 else if (strcmp(value, "HEAD") == 0)
1680 loc->limit |= AUTH_LIMIT_HEAD;
1681 else if (strcmp(value, "OPTIONS") == 0)
1682 loc->limit |= AUTH_LIMIT_OPTIONS;
1683 else if (strcmp(value, "POST") == 0)
1684 loc->limit |= AUTH_LIMIT_POST;
1685 else if (strcmp(value, "PUT") == 0)
1686 loc->limit |= AUTH_LIMIT_PUT;
1687 else if (strcmp(value, "TRACE") == 0)
1688 loc->limit |= AUTH_LIMIT_TRACE;
1689 else
1690 LogMessage(L_WARN, "Unknown request type %s on line %d!", value,
1691 linenum);
1692
1693 for (value = valptr; isspace(*value & 255) || *value == '>'; value ++);
1694 }
1695
1696 if (strcasecmp(name, "<LimitExcept") == 0)
1697 loc->limit = AUTH_LIMIT_ALL ^ loc->limit;
1698
1699 parent->limit &= ~loc->limit;
1700 }
1701 else if (strcasecmp(name, "</Limit>") == 0)
1702 loc = parent;
1703 else if (strcasecmp(name, "Encryption") == 0)
1704 {
1705 /*
1706 * "Encryption xxx" - set required encryption level...
1707 */
1708
1709 if (strcasecmp(value, "never") == 0)
1710 loc->encryption = HTTP_ENCRYPT_NEVER;
1711 else if (strcasecmp(value, "always") == 0)
1712 {
1713 LogMessage(L_ERROR, "Encryption value \"%s\" on line %d is invalid in this context. "
1714 "Using \"required\" instead.", value, linenum);
1715
1716 loc->encryption = HTTP_ENCRYPT_REQUIRED;
1717 }
1718 else if (strcasecmp(value, "required") == 0)
1719 loc->encryption = HTTP_ENCRYPT_REQUIRED;
1720 else if (strcasecmp(value, "ifrequested") == 0)
1721 loc->encryption = HTTP_ENCRYPT_IF_REQUESTED;
1722 else
1723 LogMessage(L_ERROR, "Unknown Encryption value %s on line %d.",
1724 value, linenum);
1725 }
1726 else if (strcasecmp(name, "Order") == 0)
1727 {
1728 /*
1729 * "Order Deny,Allow" or "Order Allow,Deny"...
1730 */
1731
1732 if (strncasecmp(value, "deny", 4) == 0)
1733 loc->order_type = AUTH_ALLOW;
1734 else if (strncasecmp(value, "allow", 5) == 0)
1735 loc->order_type = AUTH_DENY;
1736 else
1737 LogMessage(L_ERROR, "Unknown Order value %s on line %d.",
1738 value, linenum);
1739 }
1740 else if (strcasecmp(name, "Allow") == 0 ||
1741 strcasecmp(name, "Deny") == 0)
1742 {
1743 /*
1744 * Allow [From] host/ip...
1745 * Deny [From] host/ip...
1746 */
1747
1748 if (strncasecmp(value, "from", 4) == 0)
1749 {
1750 /*
1751 * Strip leading "from"...
1752 */
1753
1754 value += 4;
1755
1756 while (isspace(*value & 255))
1757 value ++;
1758 }
1759
1760 /*
1761 * Figure out what form the allow/deny address takes:
1762 *
1763 * All
1764 * None
1765 * *.domain.com
1766 * .domain.com
1767 * host.domain.com
1768 * nnn.*
1769 * nnn.nnn.*
1770 * nnn.nnn.nnn.*
1771 * nnn.nnn.nnn.nnn
1772 * nnn.nnn.nnn.nnn/mm
1773 * nnn.nnn.nnn.nnn/mmm.mmm.mmm.mmm
1774 */
1775
1776 if (strcasecmp(value, "all") == 0)
1777 {
1778 /*
1779 * All hosts...
1780 */
1781
1782 if (strcasecmp(name, "Allow") == 0)
1783 AllowIP(loc, 0, 0);
1784 else
1785 DenyIP(loc, 0, 0);
1786 }
1787 else if (strcasecmp(value, "none") == 0)
1788 {
1789 /*
1790 * No hosts...
1791 */
1792
1793 if (strcasecmp(name, "Allow") == 0)
1794 AllowIP(loc, ~0, 0);
1795 else
1796 DenyIP(loc, ~0, 0);
1797 }
1798 else if (value[0] == '*' || value[0] == '.' || !isdigit(value[0] & 255))
1799 {
1800 /*
1801 * Host or domain name...
1802 */
1803
1804 if (value[0] == '*')
1805 value ++;
1806
1807 if (strcasecmp(name, "Allow") == 0)
1808 AllowHost(loc, value);
1809 else
1810 DenyHost(loc, value);
1811 }
1812 else
1813 {
1814 /*
1815 * One of many IP address forms...
1816 */
1817
1818 memset(ip, 0, sizeof(ip));
1819 ipcount = sscanf(value, "%d.%d.%d.%d", ip + 0, ip + 1, ip + 2, ip + 3);
1820 address = (((((ip[0] << 8) | ip[1]) << 8) | ip[2]) << 8) | ip[3];
1821
1822 if ((value = strchr(value, '/')) != NULL)
1823 {
1824 value ++;
1825 memset(mask, 0, sizeof(mask));
1826 switch (sscanf(value, "%d.%d.%d.%d", mask + 0, mask + 1,
1827 mask + 2, mask + 3))
1828 {
1829 case 1 :
1830 netmask = (0xffffffff << (32 - mask[0])) & 0xffffffff;
1831 break;
1832 case 4 :
1833 netmask = (((((mask[0] << 8) | mask[1]) << 8) |
1834 mask[2]) << 8) | mask[3];
1835 break;
1836 default :
1837 LogMessage(L_ERROR, "Bad netmask value %s on line %d.",
1838 value, linenum);
1839 netmask = 0xffffffff;
1840 break;
1841 }
1842 }
1843 else
1844 netmask = netmasks[ipcount - 1];
1845
1846 if ((address & ~netmask) != 0)
1847 {
1848 LogMessage(L_WARN, "Discarding extra bits in %s address %08x for netmask %08x...",
1849 name, address, netmask);
1850 address &= netmask;
1851 }
1852
1853 if (strcasecmp(name, "Allow") == 0)
1854 AllowIP(loc, address, netmask);
1855 else
1856 DenyIP(loc, address, netmask);
1857 }
1858 }
1859 else if (strcasecmp(name, "AuthType") == 0)
1860 {
1861 /*
1862 * AuthType {none,basic,digest,basicdigest}
1863 */
1864
1865 if (strcasecmp(value, "none") == 0)
1866 {
1867 loc->type = AUTH_NONE;
1868 loc->level = AUTH_ANON;
1869 }
1870 else if (strcasecmp(value, "basic") == 0)
1871 {
1872 loc->type = AUTH_BASIC;
1873
1874 if (loc->level == AUTH_ANON)
1875 loc->level = AUTH_USER;
1876 }
1877 else if (strcasecmp(value, "digest") == 0)
1878 {
1879 loc->type = AUTH_DIGEST;
1880
1881 if (loc->level == AUTH_ANON)
1882 loc->level = AUTH_USER;
1883 }
1884 else if (strcasecmp(value, "basicdigest") == 0)
1885 {
1886 loc->type = AUTH_BASICDIGEST;
1887
1888 if (loc->level == AUTH_ANON)
1889 loc->level = AUTH_USER;
1890 }
1891 else
1892 LogMessage(L_WARN, "Unknown authorization type %s on line %d.",
1893 value, linenum);
1894 }
1895 else if (strcasecmp(name, "AuthClass") == 0)
1896 {
1897 /*
1898 * AuthClass anonymous, user, system, group
1899 */
1900
1901 if (strcasecmp(value, "anonymous") == 0)
1902 {
1903 loc->type = AUTH_NONE;
1904 loc->level = AUTH_ANON;
1905 }
1906 else if (strcasecmp(value, "user") == 0)
1907 loc->level = AUTH_USER;
1908 else if (strcasecmp(value, "group") == 0)
1909 loc->level = AUTH_GROUP;
1910 else if (strcasecmp(value, "system") == 0)
1911 {
1912 loc->level = AUTH_GROUP;
1913
1914 /*
1915 * Use the default system group if none is defined so far...
1916 */
1917
1918 if (NumSystemGroups == 0)
1919 NumSystemGroups = 1;
1920
1921 for (i = 0; i < NumSystemGroups; i ++)
1922 AddName(loc, SystemGroups[i]);
1923 }
1924 else
1925 LogMessage(L_WARN, "Unknown authorization class %s on line %d.",
1926 value, linenum);
1927 }
1928 else if (strcasecmp(name, "AuthGroupName") == 0)
1929 AddName(loc, value);
1930 else if (strcasecmp(name, "Require") == 0)
1931 {
1932 /*
1933 * Apache synonym for AuthClass and AuthGroupName...
1934 *
1935 * Get initial word:
1936 *
1937 * Require valid-user
1938 * Require group names
1939 * Require user names
1940 */
1941
1942 for (valptr = value;
1943 !isspace(*valptr & 255) && *valptr != '>' && *valptr;
1944 valptr ++);
1945
1946 if (*valptr)
1947 *valptr++ = '\0';
1948
1949 if (strcasecmp(value, "valid-user") == 0 ||
1950 strcasecmp(value, "user") == 0)
1951 loc->level = AUTH_USER;
1952 else if (strcasecmp(value, "group") == 0)
1953 loc->level = AUTH_GROUP;
1954 else
1955 {
1956 LogMessage(L_WARN, "Unknown Require type %s on line %d.",
1957 value, linenum);
1958 continue;
1959 }
1960
1961 /*
1962 * Get the list of names from the line...
1963 */
1964
1965 for (value = valptr; *value;)
1966 {
1967 for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
1968
1969 if (*valptr)
1970 *valptr++ = '\0';
1971
1972 AddName(loc, value);
1973
1974 for (value = valptr; isspace(*value & 255); value ++);
1975 }
1976 }
1977 else if (strcasecmp(name, "Satisfy") == 0)
1978 {
1979 if (strcasecmp(value, "all") == 0)
1980 loc->satisfy = AUTH_SATISFY_ALL;
1981 else if (strcasecmp(value, "any") == 0)
1982 loc->satisfy = AUTH_SATISFY_ANY;
1983 else
1984 LogMessage(L_WARN, "Unknown Satisfy value %s on line %d.", value,
1985 linenum);
1986 }
1987 else
1988 LogMessage(L_ERROR, "Unknown Location directive %s on line %d.",
1989 name, linenum);
1990 }
1991
1992 return (0);
1993 }
1994
1995
1996 /*
1997 * 'get_address()' - Get an address + port number from a line.
1998 */
1999
2000 static int /* O - 1 if address good, 0 if bad */
2001 get_address(char *value, /* I - Value string */
2002 unsigned defaddress, /* I - Default address */
2003 int defport, /* I - Default port */
2004 struct sockaddr_in *address) /* O - Socket address */
2005 {
2006 char hostname[256], /* Hostname or IP */
2007 portname[256]; /* Port number or name */
2008 struct hostent *host; /* Host address */
2009 struct servent *port; /* Port number */
2010
2011
2012 /*
2013 * Initialize the socket address to the defaults...
2014 */
2015
2016 memset(address, 0, sizeof(struct sockaddr_in));
2017 address->sin_family = AF_INET;
2018 address->sin_addr.s_addr = htonl(defaddress);
2019 address->sin_port = htons(defport);
2020
2021 /*
2022 * Try to grab a hostname and port number...
2023 */
2024
2025 switch (sscanf(value, "%255[^:]:%255s", hostname, portname))
2026 {
2027 case 1 :
2028 if (strchr(hostname, '.') == NULL && defaddress == INADDR_ANY)
2029 {
2030 /*
2031 * Hostname is a port number...
2032 */
2033
2034 strlcpy(portname, hostname, sizeof(portname));
2035 hostname[0] = '\0';
2036 }
2037 else
2038 portname[0] = '\0';
2039 break;
2040 case 2 :
2041 break;
2042 default :
2043 LogMessage(L_ERROR, "Unable to decode address \"%s\"!", value);
2044 return (0);
2045 }
2046
2047 /*
2048 * Decode the hostname and port number as needed...
2049 */
2050
2051 if (hostname[0] && strcmp(hostname, "*"))
2052 {
2053 if ((host = httpGetHostByName(hostname)) == NULL)
2054 {
2055 LogMessage(L_ERROR, "httpGetHostByName(\"%s\") failed - %s!", hostname,
2056 hstrerror(h_errno));
2057 return (0);
2058 }
2059
2060 memcpy(&(address->sin_addr), host->h_addr, host->h_length);
2061 address->sin_port = htons(defport);
2062 }
2063
2064 if (portname[0] != '\0')
2065 {
2066 if (isdigit(portname[0] & 255))
2067 address->sin_port = htons(atoi(portname));
2068 else
2069 {
2070 if ((port = getservbyname(portname, NULL)) == NULL)
2071 {
2072 LogMessage(L_ERROR, "getservbyname(\"%s\") failed - %s!", portname,
2073 strerror(errno));
2074 return (0);
2075 }
2076 else
2077 address->sin_port = htons(port->s_port);
2078 }
2079 }
2080
2081 return (1);
2082 }
2083
2084
2085 #ifdef HAVE_CDSASSL
2086 /*
2087 * 'CDSAGetServerCerts()' - Convert a keychain name into the CFArrayRef
2088 * required by SSLSetCertificate.
2089 *
2090 * For now we assumes that there is exactly one SecIdentity in the
2091 * keychain - i.e. there is exactly one matching cert/private key pair.
2092 * In the future we will search a keychain for a SecIdentity matching a
2093 * specific criteria. We also skip the operation of adding additional
2094 * non-signing certs from the keychain to the CFArrayRef.
2095 *
2096 * To create a self-signed certificate for testing use the certtool.
2097 * Executing the following as root will do it:
2098 *
2099 * certtool c c v k=CUPS
2100 */
2101
2102 static CFArrayRef
2103 CDSAGetServerCerts(void)
2104 {
2105 OSStatus err; /* Error info */
2106 SecKeychainRef kcRef; /* Keychain reference */
2107 SecIdentitySearchRef srchRef; /* Search reference */
2108 SecIdentityRef identity; /* Identity */
2109 CFArrayRef ca; /* Certificate array */
2110
2111
2112 kcRef = NULL;
2113 srchRef = NULL;
2114 identity = NULL;
2115 ca = NULL;
2116 err = SecKeychainOpen(ServerCertificate, &kcRef);
2117
2118 if (err)
2119 LogMessage(L_ERROR, "Cannot open keychain \"%s\", error %d.",
2120 ServerCertificate, err);
2121 else
2122 {
2123 /*
2124 * Search for "any" identity matching specified key use;
2125 * in this app, we expect there to be exactly one.
2126 */
2127
2128 err = SecIdentitySearchCreate(kcRef, CSSM_KEYUSE_SIGN, &srchRef);
2129
2130 if (err)
2131 LogMessage(L_ERROR,
2132 "Cannot find signing key in keychain \"%s\", error %d",
2133 ServerCertificate, err);
2134 else
2135 {
2136 err = SecIdentitySearchCopyNext(srchRef, &identity);
2137
2138 if (err)
2139 LogMessage(L_ERROR,
2140 "Cannot find signing key in keychain \"%s\", error %d",
2141 ServerCertificate, err);
2142 else
2143 {
2144 if (CFGetTypeID(identity) != SecIdentityGetTypeID())
2145 LogMessage(L_ERROR, "SecIdentitySearchCopyNext CFTypeID failure!");
2146 else
2147 {
2148 /*
2149 * Found one. Place it in a CFArray.
2150 * TBD: snag other (non-identity) certs from keychain and add them
2151 * to array as well.
2152 */
2153
2154 ca = CFArrayCreate(NULL, (const void **)&identity, 1, NULL);
2155
2156 if (ca == nil)
2157 LogMessage(L_ERROR, "CFArrayCreate error");
2158 }
2159
2160 /*CFRelease(identity);*/
2161 }
2162
2163 /*CFRelease(srchRef);*/
2164 }
2165
2166 /*CFRelease(kcRef);*/
2167 }
2168
2169 return ca;
2170 }
2171 #endif /* HAVE_CDSASSL */
2172
2173
2174 /*
2175 * End of "$Id: conf.c,v 1.145 2004/04/20 13:34:49 mike Exp $".
2176 */