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