]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/conf.c
Fix parsing of address masks, more of STR #1313.
[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 * cupsdReadConfiguration() - 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_CDSASSL
49 # include <Security/SecureTransport.h>
50 # include <Security/SecIdentitySearch.h>
51 #endif /* HAVE_CDSASSL */
52
53 #ifdef HAVE_VSYSLOG
54 # include <syslog.h>
55 #endif /* HAVE_VSYSLOG */
56
57
58 /*
59 * Possibly missing network definitions...
60 */
61
62 #ifndef INADDR_NONE
63 # define INADDR_NONE 0xffffffff
64 #endif /* !INADDR_NONE */
65
66
67 /*
68 * Configuration variable structure...
69 */
70
71 typedef enum
72 {
73 CUPSD_VARTYPE_INTEGER, /* Integer option */
74 CUPSD_VARTYPE_STRING, /* String option */
75 CUPSD_VARTYPE_BOOLEAN /* Boolean option */
76 } cupsd_vartype_t;
77
78 typedef struct
79 {
80 char *name; /* Name of variable */
81 void *ptr; /* Pointer to variable */
82 cupsd_vartype_t type; /* Type (int, string, address) */
83 } cupsd_var_t;
84
85
86 /*
87 * Local globals...
88 */
89
90 static cupsd_var_t variables[] =
91 {
92 { "AccessLog", &AccessLog, CUPSD_VARTYPE_STRING },
93 { "AutoPurgeJobs", &JobAutoPurge, CUPSD_VARTYPE_BOOLEAN },
94 { "BrowseInterval", &BrowseInterval, CUPSD_VARTYPE_INTEGER },
95 { "BrowseLocalOptions", &BrowseLocalOptions, CUPSD_VARTYPE_STRING },
96 { "BrowsePort", &BrowsePort, CUPSD_VARTYPE_INTEGER },
97 { "BrowseRemoteOptions", &BrowseRemoteOptions, CUPSD_VARTYPE_STRING },
98 { "BrowseShortNames", &BrowseShortNames, CUPSD_VARTYPE_BOOLEAN },
99 { "BrowseTimeout", &BrowseTimeout, CUPSD_VARTYPE_INTEGER },
100 { "Browsing", &Browsing, CUPSD_VARTYPE_BOOLEAN },
101 { "CacheDir", &CacheDir, CUPSD_VARTYPE_STRING },
102 { "Classification", &Classification, CUPSD_VARTYPE_STRING },
103 { "ClassifyOverride", &ClassifyOverride, CUPSD_VARTYPE_BOOLEAN },
104 { "ConfigFilePerm", &ConfigFilePerm, CUPSD_VARTYPE_INTEGER },
105 { "DataDir", &DataDir, CUPSD_VARTYPE_STRING },
106 { "DefaultCharset", &DefaultCharset, CUPSD_VARTYPE_STRING },
107 { "DefaultLanguage", &DefaultLanguage, CUPSD_VARTYPE_STRING },
108 { "DefaultPolicy", &DefaultPolicy, CUPSD_VARTYPE_STRING },
109 { "DocumentRoot", &DocumentRoot, CUPSD_VARTYPE_STRING },
110 { "ErrorLog", &ErrorLog, CUPSD_VARTYPE_STRING },
111 { "FaxRetryLimit", &FaxRetryLimit, CUPSD_VARTYPE_INTEGER },
112 { "FaxRetryInterval", &FaxRetryInterval, CUPSD_VARTYPE_INTEGER },
113 { "FileDevice", &FileDevice, CUPSD_VARTYPE_BOOLEAN },
114 { "FilterLimit", &FilterLimit, CUPSD_VARTYPE_INTEGER },
115 { "FilterNice", &FilterNice, CUPSD_VARTYPE_INTEGER },
116 { "FontPath", &FontPath, CUPSD_VARTYPE_STRING },
117 { "HideImplicitMembers", &HideImplicitMembers, CUPSD_VARTYPE_BOOLEAN },
118 { "ImplicitClasses", &ImplicitClasses, CUPSD_VARTYPE_BOOLEAN },
119 { "ImplicitAnyClasses", &ImplicitAnyClasses, CUPSD_VARTYPE_BOOLEAN },
120 { "KeepAliveTimeout", &KeepAliveTimeout, CUPSD_VARTYPE_INTEGER },
121 { "KeepAlive", &KeepAlive, CUPSD_VARTYPE_BOOLEAN },
122 { "LimitRequestBody", &MaxRequestSize, CUPSD_VARTYPE_INTEGER },
123 { "ListenBackLog", &ListenBackLog, CUPSD_VARTYPE_INTEGER },
124 { "LogFilePerm", &LogFilePerm, CUPSD_VARTYPE_INTEGER },
125 { "MaxActiveJobs", &MaxActiveJobs, CUPSD_VARTYPE_INTEGER },
126 { "MaxClients", &MaxClients, CUPSD_VARTYPE_INTEGER },
127 { "MaxClientsPerHost", &MaxClientsPerHost, CUPSD_VARTYPE_INTEGER },
128 { "MaxCopies", &MaxCopies, CUPSD_VARTYPE_INTEGER },
129 { "MaxJobs", &MaxJobs, CUPSD_VARTYPE_INTEGER },
130 { "MaxJobsPerPrinter", &MaxJobsPerPrinter, CUPSD_VARTYPE_INTEGER },
131 { "MaxJobsPerUser", &MaxJobsPerUser, CUPSD_VARTYPE_INTEGER },
132 { "MaxLogSize", &MaxLogSize, CUPSD_VARTYPE_INTEGER },
133 { "MaxPrinterHistory", &MaxPrinterHistory, CUPSD_VARTYPE_INTEGER },
134 { "MaxRequestSize", &MaxRequestSize, CUPSD_VARTYPE_INTEGER },
135 { "PageLog", &PageLog, CUPSD_VARTYPE_STRING },
136 { "PreserveJobFiles", &JobFiles, CUPSD_VARTYPE_BOOLEAN },
137 { "PreserveJobHistory", &JobHistory, CUPSD_VARTYPE_BOOLEAN },
138 { "Printcap", &Printcap, CUPSD_VARTYPE_STRING },
139 { "PrintcapGUI", &PrintcapGUI, CUPSD_VARTYPE_STRING },
140 { "ReloadTimeout", &ReloadTimeout, CUPSD_VARTYPE_INTEGER },
141 { "RemoteRoot", &RemoteRoot, CUPSD_VARTYPE_STRING },
142 { "RequestRoot", &RequestRoot, CUPSD_VARTYPE_STRING },
143 { "RIPCache", &RIPCache, CUPSD_VARTYPE_STRING },
144 { "RunAsUser", &RunAsUser, CUPSD_VARTYPE_BOOLEAN },
145 { "RootCertDuration", &RootCertDuration, CUPSD_VARTYPE_INTEGER },
146 { "ServerAdmin", &ServerAdmin, CUPSD_VARTYPE_STRING },
147 { "ServerBin", &ServerBin, CUPSD_VARTYPE_STRING },
148 #ifdef HAVE_SSL
149 { "ServerCertificate", &ServerCertificate, CUPSD_VARTYPE_STRING },
150 # if defined(HAVE_LIBSSL) || defined(HAVE_GNUTLS)
151 { "ServerKey", &ServerKey, CUPSD_VARTYPE_STRING },
152 # endif /* HAVE_LIBSSL || HAVE_GNUTLS */
153 #endif /* HAVE_SSL */
154 { "ServerName", &ServerName, CUPSD_VARTYPE_STRING },
155 { "ServerRoot", &ServerRoot, CUPSD_VARTYPE_STRING },
156 { "StateDir", &StateDir, CUPSD_VARTYPE_STRING },
157 { "TempDir", &TempDir, CUPSD_VARTYPE_STRING },
158 { "Timeout", &Timeout, CUPSD_VARTYPE_INTEGER }
159 };
160 #define NUM_VARS (sizeof(variables) / sizeof(variables[0]))
161
162
163 static unsigned ones[4] =
164 {
165 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff
166 };
167 static unsigned zeros[4] =
168 {
169 0x00000000, 0x00000000, 0x00000000, 0x00000000
170 };
171
172 #ifdef HAVE_CDSASSL
173 CFArrayRef CDSAGetServerCerts();
174 #endif /* HAVE_CDSASSL */
175
176
177 /*
178 * Local functions...
179 */
180
181 static http_addrlist_t *get_address(const char *value, unsigned defaddress,
182 int defport, int deffamily);
183 static int get_addr_and_mask(const char *value, unsigned *ip,
184 unsigned *mask);
185 static int parse_aaa(cupsd_location_t *loc, char *line,
186 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 * 'cupsdReadConfiguration()' - Read the cupsd.conf file.
194 */
195
196 int /* O - 1 on success, 0 otherwise */
197 cupsdReadConfiguration(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 cups_lang_t *language; /* Language */
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 * Shutdown the server...
214 */
215
216 cupsdStopServer();
217
218 /*
219 * Save the old root paths...
220 */
221
222 old_serverroot = NULL;
223 cupsdSetString(&old_serverroot, ServerRoot);
224 old_requestroot = NULL;
225 cupsdSetString(&old_requestroot, RequestRoot);
226
227 /*
228 * Reset the server configuration data...
229 */
230
231 cupsdDeleteAllLocations();
232
233 if (NumBrowsers > 0)
234 {
235 free(Browsers);
236
237 NumBrowsers = 0;
238 }
239
240 if (NumPolled > 0)
241 {
242 free(Polled);
243
244 NumPolled = 0;
245 }
246
247 if (NumRelays > 0)
248 {
249 for (i = 0; i < NumRelays; i ++)
250 if (Relays[i].from.type == AUTH_NAME)
251 free(Relays[i].from.mask.name.name);
252
253 free(Relays);
254
255 NumRelays = 0;
256 }
257
258 if (NumListeners > 0)
259 {
260 #ifdef HAVE_DOMAINSOCKETS
261 int i; /* Looping var */
262 cupsd_listener_t *lis; /* Current listening socket */
263
264 for (i = NumListeners, lis = Listeners; i > 0; i --, lis ++)
265 if (lis->address.sin_family == AF_LOCAL)
266 cupsdClearString((char **)&lis->address.sin_addr);
267 #endif /* HAVE_DOMAINSOCKETS */
268
269 free(Listeners);
270
271 NumListeners = 0;
272 }
273
274 /*
275 * String options...
276 */
277
278 cupsdSetString(&ServerName, httpGetHostname(temp, sizeof(temp)));
279 cupsdSetStringf(&ServerAdmin, "root@%s", temp);
280 cupsdSetString(&ServerBin, CUPS_SERVERBIN);
281 cupsdSetString(&RequestRoot, CUPS_REQUESTS);
282 cupsdSetString(&CacheDir, CUPS_CACHEDIR);
283 cupsdSetString(&DataDir, CUPS_DATADIR);
284 cupsdSetString(&DocumentRoot, CUPS_DOCROOT);
285 cupsdSetString(&AccessLog, CUPS_LOGDIR "/access_log");
286 cupsdSetString(&ErrorLog, CUPS_LOGDIR "/error_log");
287 cupsdSetString(&PageLog, CUPS_LOGDIR "/page_log");
288 cupsdSetString(&Printcap, "/etc/printcap");
289 cupsdSetString(&PrintcapGUI, "/usr/bin/glpoptions");
290 cupsdSetString(&FontPath, CUPS_FONTPATH);
291 cupsdSetString(&RemoteRoot, "remroot");
292 cupsdSetString(&ServerHeader, "CUPS/1.1");
293 cupsdSetString(&StateDir, CUPS_STATEDIR);
294
295 strlcpy(temp, ConfigurationFile, sizeof(temp));
296 if ((slash = strrchr(temp, '/')) != NULL)
297 *slash = '\0';
298
299 cupsdSetString(&ServerRoot, temp);
300
301 cupsdClearString(&Classification);
302 ClassifyOverride = 0;
303
304 #ifdef HAVE_SSL
305 # ifdef HAVE_CDSASSL
306 cupsdSetString(&ServerCertificate, "/var/root/Library/Keychains/CUPS");
307 # else
308 cupsdSetString(&ServerCertificate, "ssl/server.crt");
309 cupsdSetString(&ServerKey, "ssl/server.key");
310 # endif /* HAVE_CDSASSL */
311 #endif /* HAVE_SSL */
312
313 language = cupsLangDefault();
314
315 if (!strcmp(language->language, "C") || !strcmp(language->language, "POSIX"))
316 cupsdSetString(&DefaultLanguage, "en");
317 else
318 cupsdSetString(&DefaultLanguage, language->language);
319
320 cupsdSetString(&DefaultCharset, cupsEncodingName(language->encoding));
321
322 cupsdSetString(&RIPCache, "8m");
323
324 if (getenv("TMPDIR") == NULL)
325 cupsdSetString(&TempDir, CUPS_REQUESTS "/tmp");
326 else
327 cupsdSetString(&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 cupsdSetString(&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 cupsdSetString(&SystemGroups[0], group->gr_name);
359 else
360 cupsdSetString(&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 = CUPSD_LOG_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 cupsdClearString(&BrowseLocalOptions);
442 cupsdClearString(&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 cupsdClearString(&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 = cupsdFindLocation("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 cupsdLogMessage(CUPSD_LOG_INFO, "Loaded configuration file \"%s\"",
503 ConfigurationFile);
504
505 /*
506 * Validate the Group and SystemGroup settings - they cannot be the same,
507 * otherwise the CGI programs will be able to authenticate as root without
508 * a password!
509 */
510
511 if (!RunUser)
512 {
513 for (i = 0; i < NumSystemGroups; i ++)
514 if (Group == SystemGroupIDs[i])
515 break;
516
517 if (i < NumSystemGroups)
518 {
519 /*
520 * Log the error and reset the group to a safe value...
521 */
522
523 cupsdLogMessage(CUPSD_LOG_NOTICE,
524 "Group and SystemGroup cannot use the same groups!");
525 cupsdLogMessage(CUPSD_LOG_INFO, "Resetting Group to \"nobody\"...");
526
527 group = getgrnam("nobody");
528 endgrent();
529
530 if (group != NULL)
531 Group = group->gr_gid;
532 else
533 {
534 /*
535 * Use the (historical) NFS nobody group ID (-2 as a 16-bit twos-
536 * complement number...)
537 */
538
539 Group = 65534;
540 }
541 }
542 }
543
544 /*
545 * Check that we have at least one listen/port line; if not, report this
546 * as an error and exit!
547 */
548
549 if (NumListeners == 0)
550 {
551 /*
552 * No listeners!
553 */
554
555 cupsdLogMessage(CUPSD_LOG_EMERG,
556 "No valid Listen or Port lines were found in the configuration file!");
557
558 /*
559 * Commit suicide...
560 */
561
562 cupsdEndProcess(getpid(), 0);
563 }
564
565 /*
566 * Set the default locale using the language and charset...
567 */
568
569 cupsdSetStringf(&DefaultLocale, "%s.%s", DefaultLanguage, DefaultCharset);
570
571 /*
572 * Update all relative filenames to include the full path from ServerRoot...
573 */
574
575 if (DocumentRoot[0] != '/')
576 cupsdSetStringf(&DocumentRoot, "%s/%s", ServerRoot, DocumentRoot);
577
578 if (RequestRoot[0] != '/')
579 cupsdSetStringf(&RequestRoot, "%s/%s", ServerRoot, RequestRoot);
580
581 if (ServerBin[0] != '/')
582 cupsdSetStringf(&ServerBin, "%s/%s", ServerRoot, ServerBin);
583
584 if (StateDir[0] != '/')
585 cupsdSetStringf(&StateDir, "%s/%s", ServerRoot, StateDir);
586
587 if (CacheDir[0] != '/')
588 cupsdSetStringf(&CacheDir, "%s/%s", ServerRoot, CacheDir);
589
590 #ifdef HAVE_SSL
591 if (ServerCertificate[0] != '/')
592 cupsdSetStringf(&ServerCertificate, "%s/%s", ServerRoot, ServerCertificate);
593
594 # if defined(HAVE_LIBSSL) || defined(HAVE_GNUTLS)
595 chown(ServerCertificate, RunUser, Group);
596 chmod(ServerCertificate, ConfigFilePerm);
597
598 if (ServerKey[0] != '/')
599 cupsdSetStringf(&ServerKey, "%s/%s", ServerRoot, ServerKey);
600
601 chown(ServerKey, RunUser, Group);
602 chmod(ServerKey, ConfigFilePerm);
603 # endif /* HAVE_LIBSSL || HAVE_GNUTLS */
604 #endif /* HAVE_SSL */
605
606 /*
607 * Make sure that directories and config files are owned and
608 * writable by the user and group in the cupsd.conf file...
609 */
610
611 chown(CacheDir, RunUser, Group);
612 chmod(CacheDir, 0775);
613
614 snprintf(temp, sizeof(temp), "%s/ppd", CacheDir);
615 if (access(temp, 0))
616 mkdir(temp, 0755);
617 chown(temp, RunUser, Group);
618 chmod(temp, 0755);
619
620 chown(StateDir, RunUser, Group);
621 chmod(StateDir, 0775);
622
623 snprintf(temp, sizeof(temp), "%s/certs", StateDir);
624 if (access(temp, 0))
625 mkdir(temp, 0510);
626 chown(temp, User, SystemGroupIDs[0]);
627 if (RunUser)
628 chmod(temp, 0710);
629 else
630 chmod(temp, 0510);
631
632 chown(ServerRoot, RunUser, Group);
633 chmod(ServerRoot, 0755);
634
635 snprintf(temp, sizeof(temp), "%s/ppd", ServerRoot);
636 if (access(temp, 0))
637 mkdir(temp, 0755);
638 chown(temp, RunUser, Group);
639 chmod(temp, 0755);
640
641 snprintf(temp, sizeof(temp), "%s/ssl", ServerRoot);
642 if (access(temp, 0))
643 mkdir(temp, 0700);
644 chown(temp, RunUser, Group);
645 chmod(temp, 0700);
646
647 snprintf(temp, sizeof(temp), "%s/cupsd.conf", ServerRoot);
648 chown(temp, RunUser, Group);
649 chmod(temp, ConfigFilePerm);
650
651 snprintf(temp, sizeof(temp), "%s/classes.conf", ServerRoot);
652 chown(temp, RunUser, Group);
653 chmod(temp, 0600);
654
655 snprintf(temp, sizeof(temp), "%s/printers.conf", ServerRoot);
656 chown(temp, RunUser, Group);
657 chmod(temp, 0600);
658
659 snprintf(temp, sizeof(temp), "%s/passwd.md5", ServerRoot);
660 chown(temp, User, Group);
661 chmod(temp, 0600);
662
663 /*
664 * Make sure the request and temporary directories have the right
665 * permissions...
666 */
667
668 chown(RequestRoot, RunUser, Group);
669 chmod(RequestRoot, 0710);
670
671 if (!strncmp(TempDir, RequestRoot, strlen(RequestRoot)) ||
672 access(TempDir, 0))
673 {
674 /*
675 * Update ownership and permissions if the CUPS temp directory
676 * is under the spool directory or does not exist...
677 */
678
679 if (access(TempDir, 0))
680 mkdir(TempDir, 01770);
681
682 chown(TempDir, RunUser, Group);
683 chmod(TempDir, 01770);
684 }
685
686 if (!strncmp(TempDir, RequestRoot, strlen(RequestRoot)))
687 {
688 /*
689 * Clean out the temporary directory...
690 */
691
692 cups_dir_t *dir; /* Temporary directory */
693 cups_dentry_t *dent; /* Directory entry */
694 char tempfile[1024]; /* Temporary filename */
695
696
697 if ((dir = cupsDirOpen(TempDir)) != NULL)
698 {
699 cupsdLogMessage(CUPSD_LOG_INFO,
700 "Cleaning out old temporary files in \"%s\"...", TempDir);
701
702 while ((dent = cupsDirRead(dir)) != NULL)
703 {
704 snprintf(tempfile, sizeof(tempfile), "%s/%s", TempDir, dent->filename);
705
706 if (unlink(tempfile))
707 cupsdLogMessage(CUPSD_LOG_ERROR,
708 "Unable to remove temporary file \"%s\" - %s",
709 tempfile, strerror(errno));
710 else
711 cupsdLogMessage(CUPSD_LOG_DEBUG, "Removed temporary file \"%s\"...",
712 tempfile);
713 }
714
715 cupsDirClose(dir);
716 }
717 else
718 cupsdLogMessage(CUPSD_LOG_ERROR,
719 "Unable to open temporary directory \"%s\" - %s",
720 TempDir, strerror(errno));
721 }
722
723 /*
724 * Setup environment variables...
725 */
726
727 cupsdInitEnv();
728
729 /*
730 * Check the MaxClients setting, and then allocate memory for it...
731 */
732
733 if (MaxClients > (MaxFDs / 3) || MaxClients <= 0)
734 {
735 if (MaxClients > 0)
736 cupsdLogMessage(CUPSD_LOG_INFO, "MaxClients limited to 1/3 (%d) of the file descriptor limit (%d)...",
737 MaxFDs / 3, MaxFDs);
738
739 MaxClients = MaxFDs / 3;
740 }
741
742 if ((Clients = calloc(sizeof(cupsd_client_t), MaxClients)) == NULL)
743 {
744 cupsdLogMessage(CUPSD_LOG_ERROR,
745 "cupsdReadConfiguration: Unable to allocate memory for %d clients: %s",
746 MaxClients, strerror(errno));
747 exit(1);
748 }
749 else
750 cupsdLogMessage(CUPSD_LOG_INFO, "Configured for up to %d clients.",
751 MaxClients);
752
753 /*
754 * Check the MaxActiveJobs setting; limit to 1/3 the available
755 * file descriptors, since we need a pipe for each job...
756 */
757
758 if (MaxActiveJobs > (MaxFDs / 3))
759 MaxActiveJobs = MaxFDs / 3;
760
761 if (Classification && strcasecmp(Classification, "none") == 0)
762 cupsdClearString(&Classification);
763
764 if (Classification)
765 cupsdLogMessage(CUPSD_LOG_INFO, "Security set to \"%s\"", Classification);
766
767 /*
768 * Update the MaxClientsPerHost value, as needed...
769 */
770
771 if (MaxClientsPerHost <= 0)
772 MaxClientsPerHost = MaxClients;
773
774 if (MaxClientsPerHost > MaxClients)
775 MaxClientsPerHost = MaxClients;
776
777 cupsdLogMessage(CUPSD_LOG_INFO,
778 "Allowing up to %d client connections per host.",
779 MaxClientsPerHost);
780
781 /*
782 * Update the default policy, as needed...
783 */
784
785 if (DefaultPolicy)
786 DefaultPolicyPtr = cupsdFindPolicy(DefaultPolicy);
787 else
788 DefaultPolicyPtr = NULL;
789
790 if (!DefaultPolicyPtr)
791 {
792 cupsd_policy_t *p; /* New policy */
793 cupsd_location_t *po; /* New policy operation */
794
795
796 if (DefaultPolicy)
797 cupsdLogMessage(CUPSD_LOG_ERROR, "Default policy \"%s\" not found!",
798 DefaultPolicy);
799
800 if ((DefaultPolicyPtr = cupsdFindPolicy("default")) != NULL)
801 cupsdLogMessage(CUPSD_LOG_INFO,
802 "Using policy \"default\" as the default!");
803 else
804 {
805 cupsdLogMessage(CUPSD_LOG_INFO,
806 "Creating CUPS default administrative policy:");
807
808 DefaultPolicyPtr = p = cupsdAddPolicy("default");
809
810 cupsdLogMessage(CUPSD_LOG_INFO, "<Policy default>");
811 cupsdLogMessage(CUPSD_LOG_INFO,
812 "<Limit Send-Document Send-URI Cancel-Job Hold-Job "
813 "Release-Job Restart-Job Purge-Jobs "
814 "Set-Job-Attributes Create-Job-Subscription "
815 "Renew-Subscription Cancel-Subscription "
816 "Get-Notifications Reprocess-Job Cancel-Current-Job "
817 "Suspend-Current-Job Resume-Job CUPS-Move-Job "
818 "CUPS-Authenticate-Job>");
819 cupsdLogMessage(CUPSD_LOG_INFO, "Order Deny,Allow");
820
821 po = cupsdAddPolicyOp(p, NULL, IPP_SEND_DOCUMENT);
822 po->order_type = AUTH_ALLOW;
823 po->level = AUTH_USER;
824
825 cupsdAddName(po, "@OWNER");
826 cupsdAddName(po, "@SYSTEM");
827 cupsdLogMessage(CUPSD_LOG_INFO, "Require user @OWNER @SYSTEM");
828
829 cupsdAddPolicyOp(p, po, IPP_SEND_URI);
830 cupsdAddPolicyOp(p, po, IPP_CANCEL_JOB);
831 cupsdAddPolicyOp(p, po, IPP_HOLD_JOB);
832 cupsdAddPolicyOp(p, po, IPP_RELEASE_JOB);
833 cupsdAddPolicyOp(p, po, IPP_RESTART_JOB);
834 cupsdAddPolicyOp(p, po, IPP_PURGE_JOBS);
835 cupsdAddPolicyOp(p, po, IPP_SET_JOB_ATTRIBUTES);
836 cupsdAddPolicyOp(p, po, IPP_CREATE_JOB_SUBSCRIPTION);
837 cupsdAddPolicyOp(p, po, IPP_RENEW_SUBSCRIPTION);
838 cupsdAddPolicyOp(p, po, IPP_CANCEL_SUBSCRIPTION);
839 cupsdAddPolicyOp(p, po, IPP_GET_NOTIFICATIONS);
840 cupsdAddPolicyOp(p, po, IPP_REPROCESS_JOB);
841 cupsdAddPolicyOp(p, po, IPP_CANCEL_CURRENT_JOB);
842 cupsdAddPolicyOp(p, po, IPP_SUSPEND_CURRENT_JOB);
843 cupsdAddPolicyOp(p, po, IPP_RESUME_JOB);
844 cupsdAddPolicyOp(p, po, CUPS_MOVE_JOB);
845 cupsdAddPolicyOp(p, po, CUPS_AUTHENTICATE_JOB);
846
847 cupsdLogMessage(CUPSD_LOG_INFO, "</Limit>");
848
849 cupsdLogMessage(CUPSD_LOG_INFO,
850 "<Limit Pause-Printer Resume-Printer "
851 "Set-Printer-Attributes Enable-Printer "
852 "Disable-Printer Pause-Printer-After-Current-Job "
853 "Hold-New-Jobs Release-Held-New-Jobs "
854 "Deactivate-Printer Activate-Printer Restart-Printer "
855 "Shutdown-Printer Startup-Printer Promote-Job "
856 "Schedule-Job-After CUPS-Add-Printer "
857 "CUPS-Delete-Printer CUPS-Add-Class CUPS-Delete-Class "
858 "CUPS-Accept-Jobs CUPS-Reject-Jobs CUPS-Set-Default>");
859 cupsdLogMessage(CUPSD_LOG_INFO, "Order Deny,Allow");
860 cupsdLogMessage(CUPSD_LOG_INFO, "AuthType Basic");
861
862 po = cupsdAddPolicyOp(p, NULL, IPP_PAUSE_PRINTER);
863 po->order_type = AUTH_ALLOW;
864 po->type = AUTH_BASIC;
865 po->level = AUTH_USER;
866
867 cupsdAddName(po, "@SYSTEM");
868 cupsdLogMessage(CUPSD_LOG_INFO, "Require user @SYSTEM");
869
870 cupsdAddPolicyOp(p, po, IPP_RESUME_PRINTER);
871 cupsdAddPolicyOp(p, po, IPP_SET_PRINTER_ATTRIBUTES);
872 cupsdAddPolicyOp(p, po, IPP_ENABLE_PRINTER);
873 cupsdAddPolicyOp(p, po, IPP_DISABLE_PRINTER);
874 cupsdAddPolicyOp(p, po, IPP_PAUSE_PRINTER_AFTER_CURRENT_JOB);
875 cupsdAddPolicyOp(p, po, IPP_HOLD_NEW_JOBS);
876 cupsdAddPolicyOp(p, po, IPP_RELEASE_HELD_NEW_JOBS);
877 cupsdAddPolicyOp(p, po, IPP_DEACTIVATE_PRINTER);
878 cupsdAddPolicyOp(p, po, IPP_ACTIVATE_PRINTER);
879 cupsdAddPolicyOp(p, po, IPP_RESTART_PRINTER);
880 cupsdAddPolicyOp(p, po, IPP_SHUTDOWN_PRINTER);
881 cupsdAddPolicyOp(p, po, IPP_STARTUP_PRINTER);
882 cupsdAddPolicyOp(p, po, IPP_PROMOTE_JOB);
883 cupsdAddPolicyOp(p, po, IPP_SCHEDULE_JOB_AFTER);
884 cupsdAddPolicyOp(p, po, CUPS_ADD_PRINTER);
885 cupsdAddPolicyOp(p, po, CUPS_DELETE_PRINTER);
886 cupsdAddPolicyOp(p, po, CUPS_ADD_CLASS);
887 cupsdAddPolicyOp(p, po, CUPS_DELETE_CLASS);
888 cupsdAddPolicyOp(p, po, CUPS_ACCEPT_JOBS);
889 cupsdAddPolicyOp(p, po, CUPS_REJECT_JOBS);
890 cupsdAddPolicyOp(p, po, CUPS_SET_DEFAULT);
891
892 cupsdLogMessage(CUPSD_LOG_INFO, "</Limit>");
893
894 cupsdLogMessage(CUPSD_LOG_INFO, "<Limit All>");
895 cupsdLogMessage(CUPSD_LOG_INFO, "Order Deny,Allow");
896
897 po = cupsdAddPolicyOp(p, NULL, IPP_ANY_OPERATION);
898 po->order_type = AUTH_ALLOW;
899
900 cupsdLogMessage(CUPSD_LOG_INFO, "</Limit>");
901 cupsdLogMessage(CUPSD_LOG_INFO, "</Policy>");
902 }
903 }
904
905 /*
906 * If we are doing a full reload or the server root has changed, flush
907 * the jobs, printers, etc. and start from scratch...
908 */
909
910 if (NeedReload == RELOAD_ALL ||
911 !old_serverroot || !ServerRoot || strcmp(old_serverroot, ServerRoot) ||
912 !old_requestroot || !RequestRoot || strcmp(old_requestroot, RequestRoot))
913 {
914 cupsdLogMessage(CUPSD_LOG_INFO, "Full reload is required.");
915
916 /*
917 * Free all memory...
918 */
919
920 cupsdFreeAllJobs();
921 cupsdDeleteAllClasses();
922 cupsdDeleteAllPrinters();
923
924 DefaultPrinter = NULL;
925
926 if (MimeDatabase != NULL)
927 mimeDelete(MimeDatabase);
928
929 if (NumMimeTypes)
930 {
931 for (i = 0; i < NumMimeTypes; i ++)
932 free((void *)MimeTypes[i]);
933
934 free(MimeTypes);
935 }
936
937 /*
938 * Read the MIME type and conversion database...
939 */
940
941 snprintf(temp, sizeof(temp), "%s/filter", ServerBin);
942
943 MimeDatabase = mimeLoad(ServerRoot, temp);
944
945 if (!MimeDatabase)
946 {
947 cupsdLogMessage(CUPSD_LOG_EMERG,
948 "Unable to load MIME database from \'%s\'!", ServerRoot);
949 exit(errno);
950 }
951
952 cupsdLogMessage(CUPSD_LOG_INFO,
953 "Loaded MIME database from \'%s\': %d types, %d filters...",
954 ServerRoot, MimeDatabase->num_types, MimeDatabase->num_filters);
955
956 /*
957 * Create a list of MIME types for the document-format-supported
958 * attribute...
959 */
960
961 NumMimeTypes = MimeDatabase->num_types;
962 if (!mimeType(MimeDatabase, "application", "octet-stream"))
963 NumMimeTypes ++;
964
965 MimeTypes = calloc(NumMimeTypes, sizeof(const char *));
966
967 for (i = 0; i < MimeDatabase->num_types; i ++)
968 {
969 snprintf(type, sizeof(type), "%s/%s", MimeDatabase->types[i]->super,
970 MimeDatabase->types[i]->type);
971
972 MimeTypes[i] = strdup(type);
973 }
974
975 if (i < NumMimeTypes)
976 MimeTypes[i] = strdup("application/octet-stream");
977
978 /*
979 * Load banners...
980 */
981
982 snprintf(temp, sizeof(temp), "%s/banners", DataDir);
983 cupsdLoadBanners(temp);
984
985 /*
986 * Load printers and classes...
987 */
988
989 cupsdLoadAllPrinters();
990 cupsdLoadAllClasses();
991
992 cupsdCreateCommonData();
993
994 /*
995 * Load queued jobs...
996 */
997
998 cupsdLoadAllJobs();
999
1000 cupsdLogMessage(CUPSD_LOG_INFO, "Full reload complete.");
1001 }
1002 else
1003 {
1004 cupsdCreateCommonData();
1005
1006 cupsdLogMessage(CUPSD_LOG_INFO, "Partial reload complete.");
1007 }
1008
1009 /*
1010 * Reset the reload state...
1011 */
1012
1013 NeedReload = RELOAD_NONE;
1014
1015 cupsdClearString(&old_serverroot);
1016 cupsdClearString(&old_requestroot);
1017
1018 /*
1019 * Startup the server and return...
1020 */
1021
1022 cupsdStartServer();
1023
1024 return (1);
1025 }
1026
1027
1028 /*
1029 * 'get_address()' - Get an address + port number from a line.
1030 */
1031
1032 static http_addrlist_t * /* O - Pointer to list if address good, NULL if bad */
1033 get_address(const char *value, /* I - Value string */
1034 unsigned defaddress, /* I - Default address */
1035 int defport, /* I - Default port */
1036 int deffamily) /* I - Default family */
1037 {
1038 char buffer[1024], /* Hostname + port number buffer */
1039 defpname[255], /* Default port name */
1040 *hostname, /* Hostname or IP */
1041 *portname; /* Port number or name */
1042 int portnum; /* Port number */
1043 http_addrlist_t *addrlist; /* Address list */
1044 struct servent *service; /* Service entry */
1045
1046
1047 /*
1048 * Check for an empty value...
1049 */
1050
1051 if (!*value)
1052 {
1053 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad (empty) address!");
1054 return (NULL);
1055 }
1056
1057 /*
1058 * Grab a hostname and port number; if there is no colon and the port name
1059 * is only digits, then we have a port number by itself...
1060 */
1061
1062 strlcpy(buffer, value, sizeof(buffer));
1063
1064 if ((portname = strrchr(buffer, ':')) != NULL && !strchr(portname, ']'))
1065 {
1066 *portname++ = '\0';
1067 hostname = buffer;
1068 }
1069 else
1070 {
1071 for (portname = buffer; isdigit(*portname & 255); portname ++);
1072
1073 if (*portname)
1074 {
1075 /*
1076 * Use the default port...
1077 */
1078
1079 sprintf(defpname, "%d", defport);
1080 portname = defpname;
1081 hostname = buffer;
1082 }
1083 else
1084 {
1085 /*
1086 * The buffer contains just a port number...
1087 */
1088
1089 portname = buffer;
1090 hostname = NULL;
1091 }
1092 }
1093
1094 if (!strcmp(hostname, "*"))
1095 hostname = NULL;
1096
1097 /*
1098 * Now lookup the address or use the default address...
1099 */
1100
1101 if (hostname)
1102 {
1103 /*
1104 * Use httpAddrGetList() to get 1 or more addresses for the
1105 * hostname and port name...
1106 */
1107
1108 if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL)
1109 cupsdLogMessage(CUPSD_LOG_ERROR, "Hostname lookup for \"%s\" failed!",
1110 hostname);
1111 }
1112 else
1113 {
1114 /*
1115 * Create up to 2 addresses entry using the default address and
1116 * family, and set the port...
1117 */
1118
1119 if (isdigit(*portname & 255))
1120 portnum = atoi(portname);
1121 else if ((service = getservbyname(portname, NULL)) != NULL)
1122 portnum = ntohs(service->s_port);
1123 else
1124 {
1125 cupsdLogMessage(CUPSD_LOG_ERROR, "Service lookup for \"%s\" failed!",
1126 portname);
1127 free(addrlist);
1128 return (NULL);
1129 }
1130
1131 addrlist = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t));
1132
1133 if (!addrlist)
1134 {
1135 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to allocate address for \"%s\"!",
1136 value);
1137 return (NULL);
1138 }
1139
1140 #ifdef AF_INET6
1141 if (deffamily == AF_INET6)
1142 {
1143 addrlist->addr.ipv6.sin6_family = AF_INET6;
1144 addrlist->addr.ipv6.sin6_addr.s6_addr32[0] = htonl(defaddress);
1145 addrlist->addr.ipv6.sin6_addr.s6_addr32[1] = htonl(defaddress);
1146 addrlist->addr.ipv6.sin6_addr.s6_addr32[2] = htonl(defaddress);
1147 addrlist->addr.ipv6.sin6_addr.s6_addr32[3] = htonl(defaddress);
1148 addrlist->addr.ipv6.sin6_port = htons(portnum);
1149 }
1150 else
1151 #endif /* AF_INET6 */
1152 {
1153 addrlist->addr.ipv4.sin_family = AF_INET;
1154 addrlist->addr.ipv4.sin_addr.s_addr = htonl(defaddress);
1155 addrlist->addr.ipv4.sin_port = htons(portnum);
1156 }
1157
1158 #if defined(AF_INET6) && defined(__OpenBSD__)
1159 /*
1160 * Since OpenBSD does no allow an IPv6 socket to accept IPv4 connections,
1161 * add an IPv4 address...
1162 */
1163
1164 if (deffamily == AF_INET6 && !defaddress)
1165 {
1166 /*
1167 * Add an additional IPv4 address...
1168 */
1169
1170 http_addrlist_t *ipv4; /* IPv4 address */
1171
1172
1173 ipv4 = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t));
1174 if (!ipv4)
1175 cupsdLogMessage(CUPSD_LOG_ERROR,
1176 "Unable to allocate IPv4 address for \"%s\"!", value);
1177 {
1178 addrlist->next = ipv4;
1179
1180 ipv4->addr.ipv4.sin_family = AF_INET;
1181 ipv4->addr.ipv4.sin_addr.s_addr = htonl(defaddress);
1182 ipv4->addr.ipv4.sin_port = htons(defport);
1183 ipv4->addr.ipv4.sin_port = htons(portnum);
1184 }
1185 }
1186 #endif /* AF_INET6 && __OpenBSD__ */
1187 }
1188
1189 return (addrlist);
1190 }
1191
1192
1193 /*
1194 * 'get_addr_and_mask()' - Get an IP address and netmask.
1195 */
1196
1197 static int /* O - 1 on success, 0 on failure */
1198 get_addr_and_mask(const char *value, /* I - String from config file */
1199 unsigned *ip, /* O - Address value */
1200 unsigned *mask) /* O - Mask value */
1201 {
1202 int i, j, /* Looping vars */
1203 family, /* Address family */
1204 ipcount; /* Count of fields in address */
1205 unsigned ipval; /* Value */
1206 const char *maskval, /* Pointer to start of mask value */
1207 *ptr, /* Pointer into value */
1208 *ptr2; /* ... */
1209 static unsigned netmasks[4][4] = /* Standard IPv4 netmasks... */
1210 {
1211 { 0xffffffff, 0xffffffff, 0xffffffff, 0xff000000 },
1212 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffff0000 },
1213 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffff00 },
1214 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
1215 };
1216 #ifdef AF_INET6
1217 static unsigned netmasks6[8][4] = /* Standard IPv6 netmasks... */
1218 {
1219 { 0xffff0000, 0x00000000, 0x00000000, 0x00000000 },
1220 { 0xffffffff, 0x00000000, 0x00000000, 0x00000000 },
1221 { 0xffffffff, 0xffff0000, 0x00000000, 0x00000000 },
1222 { 0xffffffff, 0xffffffff, 0x00000000, 0x00000000 },
1223 { 0xffffffff, 0xffffffff, 0xffff0000, 0x00000000 },
1224 { 0xffffffff, 0xffffffff, 0xffffffff, 0x00000000 },
1225 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffff0000 },
1226 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
1227 };
1228 #endif /* AF_INET6 */
1229
1230
1231 /*
1232 * Get the address...
1233 */
1234
1235 memset(ip, 0, sizeof(unsigned) * 4);
1236
1237 if ((maskval = strchr(value, '/')) != NULL)
1238 maskval ++;
1239 else
1240 maskval = value + strlen(value);
1241
1242 #ifdef AF_INET6
1243 /*
1244 * Check for an IPv6 address...
1245 */
1246
1247 if (*value == '[')
1248 {
1249 /*
1250 * Parse hexadecimal IPv6 address...
1251 */
1252
1253 family = AF_INET6;
1254
1255 for (i = 0, ptr = value + 1; *ptr && i < 8; i ++)
1256 {
1257 if (*ptr == ']')
1258 break;
1259 else if (!strncmp(ptr, "::", 2))
1260 {
1261 for (ptr2 = strchr(ptr + 2, ':'), j = 0;
1262 ptr2;
1263 ptr2 = strchr(ptr2 + 1, ':'), j ++);
1264
1265 i = 7 - j;
1266 }
1267 else if (isxdigit(*ptr & 255))
1268 {
1269 ipval = strtoul(ptr, (char **)&ptr, 16);
1270
1271 if (ipval > 0xffff)
1272 return (0);
1273
1274 if (i & 1)
1275 ip[i] |= ipval;
1276 else
1277 ip[i] |= ipval << 16;
1278 }
1279 else
1280 return (0);
1281
1282 while (*ptr == ':')
1283 ptr ++;
1284 }
1285
1286 ipcount = i;
1287
1288 if (*ptr && *ptr != '/')
1289 return (0);
1290 }
1291 else
1292 #endif /* AF_INET6 */
1293 {
1294 /*
1295 * Parse dotted-decimal IPv4 address...
1296 */
1297
1298 family = AF_INET;
1299 ipcount = sscanf(value, "%u.%u.%u.%u", ip + 0, ip + 1, ip + 2, ip + 3);
1300
1301 ip[3] |= ((((ip[0] << 8) | ip[1]) << 8) | ip[2]) << 8;
1302 ip[0] = ip[1] = ip[2] = 0;
1303 }
1304
1305 if (*maskval)
1306 {
1307 /*
1308 * Get the netmask value(s)...
1309 */
1310
1311 memset(mask, 0, sizeof(unsigned) * 4);
1312
1313 #ifdef AF_INET6
1314 if (*maskval == '[')
1315 {
1316 /*
1317 * Get hexadecimal mask value...
1318 */
1319
1320 for (i = 0, ptr = maskval + 1; *ptr && i < 8; i ++)
1321 {
1322 if (*ptr == ']')
1323 break;
1324 else if (!strncmp(ptr, "::", 2))
1325 {
1326 for (ptr2 = strchr(ptr + 2, ':'), j = 0;
1327 ptr2;
1328 ptr2 = strchr(ptr2 + 1, ':'), j ++);
1329
1330 i = 7 - j;
1331 }
1332 else if (isxdigit(*ptr & 255))
1333 {
1334 ipval = strtoul(ptr, (char **)&ptr, 16);
1335
1336 if (ipval > 0xffff)
1337 return (0);
1338
1339 if (i & 1)
1340 mask[i] |= ipval;
1341 else
1342 mask[i] |= ipval << 16;
1343 }
1344 else
1345 return (0);
1346
1347 while (*ptr == ':')
1348 ptr ++;
1349 }
1350
1351 if (*ptr)
1352 return (0);
1353 }
1354 else
1355 #endif /* AF_INET6 */
1356 if (strchr(maskval, '.'))
1357 {
1358 /*
1359 * Get dotted-decimal mask...
1360 */
1361
1362 if (sscanf(maskval, "%u.%u.%u.%u", mask + 0, mask + 1, mask + 2, mask + 3) != 4)
1363 return (0);
1364
1365 mask[3] |= ((((mask[0] << 8) | mask[1]) << 8) | mask[2]) << 8;
1366 mask[0] = mask[1] = mask[2] = 0;
1367 }
1368 else
1369 {
1370 /*
1371 * Get address/bits format...
1372 */
1373
1374 i = atoi(maskval);
1375
1376 #ifdef AF_INET6
1377 if (family == AF_INET6)
1378 {
1379 i = 128 - i;
1380
1381 if (i <= 96)
1382 mask[0] = 0xffffffff;
1383 else
1384 mask[0] = (0xffffffff << (i - 96)) & 0xffffffff;
1385
1386 if (i <= 64)
1387 mask[1] = 0xffffffff;
1388 else if (i >= 96)
1389 mask[1] = 0;
1390 else
1391 mask[1] = (0xffffffff << (i - 64)) & 0xffffffff;
1392
1393 if (i <= 32)
1394 mask[2] = 0xffffffff;
1395 else if (i >= 64)
1396 mask[2] = 0;
1397 else
1398 mask[2] = (0xffffffff << (i - 32)) & 0xffffffff;
1399
1400 if (i == 0)
1401 mask[3] = 0xffffffff;
1402 else if (i >= 32)
1403 mask[3] = 0;
1404 else
1405 mask[3] = (0xffffffff << i) & 0xffffffff;
1406 }
1407 else
1408 #endif /* AF_INET6 */
1409 {
1410 i = 32 - i;
1411
1412 mask[0] = 0xffffffff;
1413 mask[1] = 0xffffffff;
1414 mask[2] = 0xffffffff;
1415
1416 if (i > 0)
1417 mask[3] = (0xffffffff << i) & 0xffffffff;
1418 else
1419 mask[3] = 0xffffffff;
1420 }
1421 }
1422 }
1423 #ifdef AF_INET6
1424 else if (family == AF_INET6)
1425 memcpy(mask, netmasks6[ipcount - 1], sizeof(unsigned) * 4);
1426 #endif /* AF_INET6 */
1427 else
1428 memcpy(mask, netmasks[ipcount - 1], sizeof(unsigned) * 4);
1429
1430 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1431 "get_addr_and_mask(value=\"%s\", "
1432 "ip=[%08x:%08x:%08x:%08x], mask=[%08x:%08x:%08x:%08x]",
1433 value, ip[0], ip[1], ip[2], ip[3], mask[0], mask[1], mask[2],
1434 mask[3]);
1435
1436 /*
1437 * Check for a valid netmask; no fallback like in CUPS 1.1.x!
1438 */
1439
1440 if ((ip[0] & ~mask[0]) != 0 ||
1441 (ip[1] & ~mask[1]) != 0 ||
1442 (ip[2] & ~mask[2]) != 0 ||
1443 (ip[3] & ~mask[3]) != 0)
1444 return (0);
1445
1446 return (1);
1447 }
1448
1449
1450 /*
1451 * 'parse_aaa()' - Parse authentication, authorization, and access control lines.
1452 */
1453
1454 static int /* O - 1 on success, 0 on failure */
1455 parse_aaa(cupsd_location_t *loc, /* I - Location */
1456 char *line, /* I - Line from file */
1457 char *value, /* I - Start of value data */
1458 int linenum) /* I - Current line number */
1459 {
1460 char *valptr; /* Pointer into value */
1461 unsigned ip[4], /* IP address components */
1462 mask[4]; /* IP netmask components */
1463
1464
1465 if (!strcasecmp(line, "Encryption"))
1466 {
1467 /*
1468 * "Encryption xxx" - set required encryption level...
1469 */
1470
1471 if (!strcasecmp(value, "never"))
1472 loc->encryption = HTTP_ENCRYPT_NEVER;
1473 else if (!strcasecmp(value, "always"))
1474 {
1475 cupsdLogMessage(CUPSD_LOG_ERROR,
1476 "Encryption value \"%s\" on line %d is invalid in this "
1477 "context. Using \"required\" instead.", value, linenum);
1478
1479 loc->encryption = HTTP_ENCRYPT_REQUIRED;
1480 }
1481 else if (!strcasecmp(value, "required"))
1482 loc->encryption = HTTP_ENCRYPT_REQUIRED;
1483 else if (!strcasecmp(value, "ifrequested"))
1484 loc->encryption = HTTP_ENCRYPT_IF_REQUESTED;
1485 else
1486 {
1487 cupsdLogMessage(CUPSD_LOG_ERROR,
1488 "Unknown Encryption value %s on line %d.", value, linenum);
1489 return (0);
1490 }
1491 }
1492 else if (!strcasecmp(line, "Order"))
1493 {
1494 /*
1495 * "Order Deny,Allow" or "Order Allow,Deny"...
1496 */
1497
1498 if (!strncasecmp(value, "deny", 4))
1499 loc->order_type = AUTH_ALLOW;
1500 else if (!strncasecmp(value, "allow", 5))
1501 loc->order_type = AUTH_DENY;
1502 else
1503 {
1504 cupsdLogMessage(CUPSD_LOG_ERROR, "Unknown Order value %s on line %d.",
1505 value, linenum);
1506 return (0);
1507 }
1508 }
1509 else if (!strcasecmp(line, "Allow") || !strcasecmp(line, "Deny"))
1510 {
1511 /*
1512 * Allow [From] host/ip...
1513 * Deny [From] host/ip...
1514 */
1515
1516 if (!strncasecmp(value, "from", 4))
1517 {
1518 /*
1519 * Strip leading "from"...
1520 */
1521
1522 value += 4;
1523
1524 while (isspace(*value & 255))
1525 value ++;
1526 }
1527
1528 /*
1529 * Figure out what form the allow/deny address takes:
1530 *
1531 * All
1532 * None
1533 * *.domain.com
1534 * .domain.com
1535 * host.domain.com
1536 * nnn.*
1537 * nnn.nnn.*
1538 * nnn.nnn.nnn.*
1539 * nnn.nnn.nnn.nnn
1540 * nnn.nnn.nnn.nnn/mm
1541 * nnn.nnn.nnn.nnn/mmm.mmm.mmm.mmm
1542 */
1543
1544 if (!strcasecmp(value, "all"))
1545 {
1546 /*
1547 * All hosts...
1548 */
1549
1550 if (!strcasecmp(line, "Allow"))
1551 cupsdAllowIP(loc, zeros, zeros);
1552 else
1553 cupsdDenyIP(loc, zeros, zeros);
1554 }
1555 else if (!strcasecmp(value, "none"))
1556 {
1557 /*
1558 * No hosts...
1559 */
1560
1561 if (!strcasecmp(line, "Allow"))
1562 cupsdAllowIP(loc, ones, zeros);
1563 else
1564 cupsdDenyIP(loc, ones, zeros);
1565 }
1566 else if (value[0] == '*' || value[0] == '.' || !isdigit(value[0] & 255))
1567 {
1568 /*
1569 * Host or domain name...
1570 */
1571
1572 if (value[0] == '*')
1573 value ++;
1574
1575 if (!strcasecmp(line, "Allow"))
1576 cupsdAllowHost(loc, value);
1577 else
1578 cupsdDenyHost(loc, value);
1579 }
1580 else
1581 {
1582 /*
1583 * One of many IP address forms...
1584 */
1585
1586 if (!get_addr_and_mask(value, ip, mask))
1587 {
1588 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad netmask value %s on line %d.",
1589 value, linenum);
1590 return (0);
1591 }
1592
1593 if (!strcasecmp(line, "Allow"))
1594 cupsdAllowIP(loc, ip, mask);
1595 else
1596 cupsdDenyIP(loc, ip, mask);
1597 }
1598 }
1599 else if (!strcasecmp(line, "AuthType"))
1600 {
1601 /*
1602 * AuthType {none,basic,digest,basicdigest}
1603 */
1604
1605 if (!strcasecmp(value, "none"))
1606 {
1607 loc->type = AUTH_NONE;
1608 loc->level = AUTH_ANON;
1609 }
1610 else if (!strcasecmp(value, "basic"))
1611 {
1612 loc->type = AUTH_BASIC;
1613
1614 if (loc->level == AUTH_ANON)
1615 loc->level = AUTH_USER;
1616 }
1617 else if (!strcasecmp(value, "digest"))
1618 {
1619 loc->type = AUTH_DIGEST;
1620
1621 if (loc->level == AUTH_ANON)
1622 loc->level = AUTH_USER;
1623 }
1624 else if (!strcasecmp(value, "basicdigest"))
1625 {
1626 loc->type = AUTH_BASICDIGEST;
1627
1628 if (loc->level == AUTH_ANON)
1629 loc->level = AUTH_USER;
1630 }
1631 else
1632 {
1633 cupsdLogMessage(CUPSD_LOG_WARN,
1634 "Unknown authorization type %s on line %d.",
1635 value, linenum);
1636 return (0);
1637 }
1638 }
1639 else if (!strcasecmp(line, "AuthClass"))
1640 {
1641 /*
1642 * AuthClass anonymous, user, system, group
1643 */
1644
1645 if (!strcasecmp(value, "anonymous"))
1646 {
1647 loc->type = AUTH_NONE;
1648 loc->level = AUTH_ANON;
1649 }
1650 else if (!strcasecmp(value, "user"))
1651 loc->level = AUTH_USER;
1652 else if (!strcasecmp(value, "group"))
1653 loc->level = AUTH_GROUP;
1654 else if (!strcasecmp(value, "system"))
1655 {
1656 loc->level = AUTH_GROUP;
1657
1658 cupsdAddName(loc, "@SYSTEM");
1659 }
1660 else
1661 {
1662 cupsdLogMessage(CUPSD_LOG_WARN,
1663 "Unknown authorization class %s on line %d.",
1664 value, linenum);
1665 return (0);
1666 }
1667 }
1668 else if (!strcasecmp(line, "AuthGroupName"))
1669 cupsdAddName(loc, value);
1670 else if (!strcasecmp(line, "Require"))
1671 {
1672 /*
1673 * Apache synonym for AuthClass and AuthGroupName...
1674 *
1675 * Get initial word:
1676 *
1677 * Require valid-user
1678 * Require group names
1679 * Require user names
1680 */
1681
1682 for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
1683
1684 if (*valptr)
1685 *valptr++ = '\0';
1686
1687 if (!strcasecmp(value, "valid-user") ||
1688 !strcasecmp(value, "user"))
1689 loc->level = AUTH_USER;
1690 else if (!strcasecmp(value, "group"))
1691 loc->level = AUTH_GROUP;
1692 else
1693 {
1694 cupsdLogMessage(CUPSD_LOG_WARN, "Unknown Require type %s on line %d.",
1695 value, linenum);
1696 return (0);
1697 }
1698
1699 /*
1700 * Get the list of names from the line...
1701 */
1702
1703 for (value = valptr; *value;)
1704 {
1705 while (isspace(*value & 255))
1706 value ++;
1707
1708 if (*value == '\"' || *value == '\'')
1709 {
1710 /*
1711 * Grab quoted name...
1712 */
1713
1714 for (valptr = value + 1; *valptr != *value && *valptr; valptr ++);
1715
1716 value ++;
1717 }
1718 else
1719 {
1720 /*
1721 * Grab literal name.
1722 */
1723
1724 for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
1725 }
1726
1727 if (*valptr)
1728 *valptr++ = '\0';
1729
1730 cupsdAddName(loc, value);
1731
1732 for (value = valptr; isspace(*value & 255); value ++);
1733 }
1734 }
1735 else if (!strcasecmp(line, "Satisfy"))
1736 {
1737 if (!strcasecmp(value, "all"))
1738 loc->satisfy = AUTH_SATISFY_ALL;
1739 else if (!strcasecmp(value, "any"))
1740 loc->satisfy = AUTH_SATISFY_ANY;
1741 else
1742 {
1743 cupsdLogMessage(CUPSD_LOG_WARN, "Unknown Satisfy value %s on line %d.",
1744 value, linenum);
1745 return (0);
1746 }
1747 }
1748 else
1749 return (0);
1750
1751 return (1);
1752 }
1753
1754
1755 /*
1756 * 'read_configuration()' - Read a configuration file.
1757 */
1758
1759 static int /* O - 1 on success, 0 on failure */
1760 read_configuration(cups_file_t *fp) /* I - File to read from */
1761 {
1762 int i; /* Looping var */
1763 int linenum; /* Current line number */
1764 char line[HTTP_MAX_BUFFER],
1765 /* Line from file */
1766 temp[HTTP_MAX_BUFFER],
1767 /* Temporary buffer for value */
1768 temp2[HTTP_MAX_BUFFER],
1769 /* Temporary buffer 2 for value */
1770 *ptr, /* Pointer into line/temp */
1771 *value, /* Pointer to value */
1772 *valueptr, /* Pointer into value */
1773 quote; /* Quote character */
1774 int valuelen; /* Length of value */
1775 cupsd_var_t *var; /* Current variable */
1776 http_addrlist_t *addrlist, /* Address list */
1777 *addr; /* Current address */
1778 unsigned ip[4], /* Address value */
1779 mask[4]; /* Netmask value */
1780 cupsd_dirsvc_relay_t *relay; /* Relay data */
1781 cupsd_dirsvc_poll_t *poll; /* Polling data */
1782 cupsd_location_t *location; /* Browse location */
1783 cups_file_t *incfile; /* Include file */
1784 char incname[1024]; /* Include filename */
1785 struct group *group; /* Group */
1786
1787
1788 /*
1789 * Loop through each line in the file...
1790 */
1791
1792 linenum = 0;
1793
1794 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
1795 {
1796 /*
1797 * Decode the directive...
1798 */
1799
1800 if (!strcasecmp(line, "Include"))
1801 {
1802 /*
1803 * Include filename
1804 */
1805
1806 if (value[0] == '/')
1807 strlcpy(incname, value, sizeof(incname));
1808 else
1809 snprintf(incname, sizeof(incname), "%s/%s", ServerRoot, value);
1810
1811 if ((incfile = cupsFileOpen(incname, "rb")) == NULL)
1812 cupsdLogMessage(CUPSD_LOG_ERROR,
1813 "Unable to include config file \"%s\" - %s",
1814 incname, strerror(errno));
1815 else
1816 {
1817 read_configuration(incfile);
1818 cupsFileClose(incfile);
1819 }
1820 }
1821 else if (!strcasecmp(line, "<Location"))
1822 {
1823 /*
1824 * <Location path>
1825 */
1826
1827 if (value)
1828 {
1829 linenum = read_location(fp, value, linenum);
1830 if (linenum == 0)
1831 return (0);
1832 }
1833 else
1834 {
1835 cupsdLogMessage(CUPSD_LOG_ERROR, "Syntax error on line %d.",
1836 linenum);
1837 return (0);
1838 }
1839 }
1840 else if (!strcasecmp(line, "<Policy"))
1841 {
1842 /*
1843 * <Policy name>
1844 */
1845
1846 if (value)
1847 {
1848 linenum = read_policy(fp, value, linenum);
1849 if (linenum == 0)
1850 return (0);
1851 }
1852 else
1853 {
1854 cupsdLogMessage(CUPSD_LOG_ERROR, "Syntax error on line %d.", linenum);
1855 return (0);
1856 }
1857 }
1858 else if (!strcasecmp(line, "Port") || !strcasecmp(line, "Listen")
1859 #ifdef HAVE_SSL
1860 || !strcasecmp(line, "SSLPort") || !strcasecmp(line, "SSLListen")
1861 #endif /* HAVE_SSL */
1862 )
1863 {
1864 /*
1865 * Add listening address(es) to the list...
1866 */
1867
1868 cupsd_listener_t *lis; /* New listeners array */
1869
1870
1871 /*
1872 * Get the address list...
1873 */
1874
1875 #if defined(AF_INET6)
1876 addrlist = get_address(value, INADDR_ANY, IPP_PORT, AF_INET6);
1877 #else
1878 addrlist = get_address(value, INADDR_ANY, IPP_PORT, AF_INET);
1879 #endif /* AF_INET6 */
1880
1881 if (!addrlist)
1882 {
1883 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad %s address %s at line %d.", line,
1884 value, linenum);
1885 continue;
1886 }
1887
1888 /*
1889 * Add each address...
1890 */
1891
1892 for (addr = addrlist; addr; addr = addr->next)
1893 {
1894 /*
1895 * Allocate another listener...
1896 */
1897
1898 if (NumListeners == 0)
1899 lis = malloc(sizeof(cupsd_listener_t));
1900 else
1901 lis = realloc(Listeners, (NumListeners + 1) * sizeof(cupsd_listener_t));
1902
1903 if (!lis)
1904 {
1905 cupsdLogMessage(CUPSD_LOG_ERROR,
1906 "Unable to allocate %s at line %d - %s.",
1907 line, linenum, strerror(errno));
1908 break;
1909 }
1910
1911 Listeners = lis;
1912 lis += NumListeners;
1913
1914 /*
1915 * Copy the current address and log it...
1916 */
1917
1918 memset(lis, 0, sizeof(cupsd_listener_t));
1919 memcpy(&(lis->address), &(addr->addr), sizeof(lis->address));
1920
1921 #ifdef HAVE_SSL
1922 if (!strcasecmp(line, "SSLPort") || !strcasecmp(line, "SSLListen"))
1923 lis->encryption = HTTP_ENCRYPT_ALWAYS;
1924 #endif /* HAVE_SSL */
1925
1926 #ifdef AF_INET6
1927 if (lis->address.addr.sa_family == AF_INET6)
1928 cupsdLogMessage(CUPSD_LOG_INFO, "Listening to %s:%d (IPv6)", temp,
1929 ntohs(lis->address.ipv6.sin6_port));
1930 else
1931 #endif /* AF_INET6 */
1932 #ifdef AF_LOCAL
1933 if (lis->address.addr.sa_family == AF_LOCAL)
1934 cupsdLogMessage(CUPSD_LOG_INFO, "Listening to %s (Domain)", temp);
1935 else
1936 #endif /* AF_LOCAL */
1937 cupsdLogMessage(CUPSD_LOG_INFO, "Listening to %s:%d (IPv4)", temp,
1938 ntohs(lis->address.ipv4.sin_port));
1939
1940 NumListeners ++;
1941 }
1942
1943 /*
1944 * Free the list...
1945 */
1946
1947 httpAddrFreeList(addrlist);
1948 }
1949 else if (!strcasecmp(line, "BrowseAddress"))
1950 {
1951 /*
1952 * Add a browse address to the list...
1953 */
1954
1955 cupsd_dirsvc_addr_t *dira; /* New browse address array */
1956
1957
1958 if (NumBrowsers == 0)
1959 dira = malloc(sizeof(cupsd_dirsvc_addr_t));
1960 else
1961 dira = realloc(Browsers, (NumBrowsers + 1) * sizeof(cupsd_dirsvc_addr_t));
1962
1963 if (!dira)
1964 {
1965 cupsdLogMessage(CUPSD_LOG_ERROR,
1966 "Unable to allocate BrowseAddress at line %d - %s.",
1967 linenum, strerror(errno));
1968 continue;
1969 }
1970
1971 Browsers = dira;
1972 dira += NumBrowsers;
1973
1974 memset(dira, 0, sizeof(cupsd_dirsvc_addr_t));
1975
1976 if (!strcasecmp(value, "@LOCAL"))
1977 {
1978 /*
1979 * Send browse data to all local interfaces...
1980 */
1981
1982 strcpy(dira->iface, "*");
1983 NumBrowsers ++;
1984 }
1985 else if (!strncasecmp(value, "@IF(", 4))
1986 {
1987 /*
1988 * Send browse data to the named interface...
1989 */
1990
1991 strlcpy(dira->iface, value + 4, sizeof(Browsers[0].iface));
1992
1993 ptr = dira->iface + strlen(dira->iface) - 1;
1994 if (*ptr == ')')
1995 *ptr = '\0';
1996
1997 NumBrowsers ++;
1998 }
1999 else if ((addrlist = get_address(value, INADDR_NONE, BrowsePort,
2000 AF_INET)) != NULL)
2001 {
2002 memcpy(&(dira->to), &(addrlist->addr), sizeof(dira->to));
2003 httpAddrFreeList(addrlist);
2004
2005 httpAddrString(&(dira->to), temp, sizeof(temp));
2006
2007 #ifdef AF_INET6
2008 if (dira->to.addr.sa_family == AF_INET6)
2009 cupsdLogMessage(CUPSD_LOG_INFO,
2010 "Sending browsing info to %s:%d (IPv6)", temp,
2011 ntohs(dira->to.ipv6.sin6_port));
2012 else
2013 #endif /* AF_INET6 */
2014 cupsdLogMessage(CUPSD_LOG_INFO, "Sending browsing info to %s:%d (IPv4)",
2015 temp, ntohs(dira->to.ipv4.sin_port));
2016
2017 NumBrowsers ++;
2018 }
2019 else
2020 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad BrowseAddress %s at line %d.",
2021 value, linenum);
2022 }
2023 else if (!strcasecmp(line, "BrowseOrder"))
2024 {
2025 /*
2026 * "BrowseOrder Deny,Allow" or "BrowseOrder Allow,Deny"...
2027 */
2028
2029 if ((location = cupsdFindLocation("CUPS_INTERNAL_BROWSE_ACL")) == NULL)
2030 location = cupsdAddLocation("CUPS_INTERNAL_BROWSE_ACL");
2031
2032 if (location == NULL)
2033 cupsdLogMessage(CUPSD_LOG_ERROR,
2034 "Unable to initialize browse access control list!");
2035 else if (!strncasecmp(value, "deny", 4))
2036 location->order_type = AUTH_ALLOW;
2037 else if (!strncasecmp(value, "allow", 5))
2038 location->order_type = AUTH_DENY;
2039 else
2040 cupsdLogMessage(CUPSD_LOG_ERROR,
2041 "Unknown BrowseOrder value %s on line %d.",
2042 value, linenum);
2043 }
2044 else if (!strcasecmp(line, "BrowseProtocols") ||
2045 !strcasecmp(line, "BrowseLocalProtocols") ||
2046 !strcasecmp(line, "BrowseRemoteProtocols"))
2047 {
2048 /*
2049 * "BrowseProtocol name [... name]"
2050 */
2051
2052 if (strcasecmp(line, "BrowseLocalProtocols"))
2053 BrowseRemoteProtocols = 0;
2054 if (strcasecmp(line, "BrowseRemoteProtocols"))
2055 BrowseLocalProtocols = 0;
2056
2057 for (; *value;)
2058 {
2059 for (valuelen = 0; value[valuelen]; valuelen ++)
2060 if (isspace(value[valuelen]) || value[valuelen] == ',')
2061 break;
2062
2063 if (value[valuelen])
2064 {
2065 value[valuelen] = '\0';
2066 valuelen ++;
2067 }
2068
2069 if (!strcasecmp(value, "cups"))
2070 {
2071 if (strcasecmp(line, "BrowseLocalProtocols"))
2072 BrowseRemoteProtocols |= BROWSE_CUPS;
2073 if (strcasecmp(line, "BrowseRemoteProtocols"))
2074 BrowseLocalProtocols |= BROWSE_CUPS;
2075 }
2076 else if (!strcasecmp(value, "slp"))
2077 {
2078 if (strcasecmp(line, "BrowseLocalProtocols"))
2079 BrowseRemoteProtocols |= BROWSE_SLP;
2080 if (strcasecmp(line, "BrowseRemoteProtocols"))
2081 BrowseLocalProtocols |= BROWSE_SLP;
2082 }
2083 else if (!strcasecmp(value, "ldap"))
2084 {
2085 if (strcasecmp(line, "BrowseLocalProtocols"))
2086 BrowseRemoteProtocols |= BROWSE_LDAP;
2087 if (strcasecmp(line, "BrowseRemoteProtocols"))
2088 BrowseLocalProtocols |= BROWSE_LDAP;
2089 }
2090 else if (!strcasecmp(value, "all"))
2091 {
2092 if (strcasecmp(line, "BrowseLocalProtocols"))
2093 BrowseRemoteProtocols |= BROWSE_ALL;
2094 if (strcasecmp(line, "BrowseRemoteProtocols"))
2095 BrowseLocalProtocols |= BROWSE_ALL;
2096 }
2097 else
2098 {
2099 cupsdLogMessage(CUPSD_LOG_ERROR,
2100 "Unknown browse protocol \"%s\" on line %d.",
2101 value, linenum);
2102 break;
2103 }
2104
2105 for (value += valuelen; *value; value ++)
2106 if (!isspace(*value) || *value != ',')
2107 break;
2108 }
2109 }
2110 else if (!strcasecmp(line, "BrowseAllow") ||
2111 !strcasecmp(line, "BrowseDeny"))
2112 {
2113 /*
2114 * BrowseAllow [From] host/ip...
2115 * BrowseDeny [From] host/ip...
2116 */
2117
2118 if ((location = cupsdFindLocation("CUPS_INTERNAL_BROWSE_ACL")) == NULL)
2119 location = cupsdAddLocation("CUPS_INTERNAL_BROWSE_ACL");
2120
2121 if (location == NULL)
2122 cupsdLogMessage(CUPSD_LOG_ERROR,
2123 "Unable to initialize browse access control list!");
2124 else
2125 {
2126 if (!strncasecmp(value, "from ", 5))
2127 {
2128 /*
2129 * Strip leading "from"...
2130 */
2131
2132 value += 5;
2133
2134 while (isspace(*value))
2135 value ++;
2136 }
2137
2138 /*
2139 * Figure out what form the allow/deny address takes:
2140 *
2141 * All
2142 * None
2143 * *.domain.com
2144 * .domain.com
2145 * host.domain.com
2146 * nnn.*
2147 * nnn.nnn.*
2148 * nnn.nnn.nnn.*
2149 * nnn.nnn.nnn.nnn
2150 * nnn.nnn.nnn.nnn/mm
2151 * nnn.nnn.nnn.nnn/mmm.mmm.mmm.mmm
2152 */
2153
2154 if (!strcasecmp(value, "all"))
2155 {
2156 /*
2157 * All hosts...
2158 */
2159
2160 if (!strcasecmp(line, "BrowseAllow"))
2161 cupsdAllowIP(location, zeros, zeros);
2162 else
2163 cupsdDenyIP(location, zeros, zeros);
2164 }
2165 else if (!strcasecmp(value, "none"))
2166 {
2167 /*
2168 * No hosts...
2169 */
2170
2171 if (!strcasecmp(line, "BrowseAllow"))
2172 cupsdAllowIP(location, ones, zeros);
2173 else
2174 cupsdDenyIP(location, ones, zeros);
2175 }
2176 else if (value[0] == '*' || value[0] == '.' || !isdigit(value[0]))
2177 {
2178 /*
2179 * Host or domain name...
2180 */
2181
2182 if (value[0] == '*')
2183 value ++;
2184
2185 if (!strcasecmp(line, "BrowseAllow"))
2186 cupsdAllowHost(location, value);
2187 else
2188 cupsdDenyHost(location, value);
2189 }
2190 else
2191 {
2192 /*
2193 * One of many IP address forms...
2194 */
2195
2196 if (!get_addr_and_mask(value, ip, mask))
2197 {
2198 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad netmask value %s on line %d.",
2199 value, linenum);
2200 break;
2201 }
2202
2203 if (!strcasecmp(line, "BrowseAllow"))
2204 cupsdAllowIP(location, ip, mask);
2205 else
2206 cupsdDenyIP(location, ip, mask);
2207 }
2208 }
2209 }
2210 else if (!strcasecmp(line, "BrowseRelay"))
2211 {
2212 /*
2213 * BrowseRelay [from] source [to] destination
2214 */
2215
2216 if (NumRelays == 0)
2217 relay = malloc(sizeof(cupsd_dirsvc_relay_t));
2218 else
2219 relay = realloc(Relays, (NumRelays + 1) * sizeof(cupsd_dirsvc_relay_t));
2220
2221 if (!relay)
2222 {
2223 cupsdLogMessage(CUPSD_LOG_ERROR,
2224 "Unable to allocate BrowseRelay at line %d - %s.",
2225 linenum, strerror(errno));
2226 continue;
2227 }
2228
2229 Relays = relay;
2230 relay += NumRelays;
2231
2232 memset(relay, 0, sizeof(cupsd_dirsvc_relay_t));
2233
2234 if (!strncasecmp(value, "from ", 5))
2235 {
2236 /*
2237 * Strip leading "from"...
2238 */
2239
2240 value += 5;
2241
2242 while (isspace(*value))
2243 value ++;
2244 }
2245
2246 /*
2247 * Figure out what form the from address takes:
2248 *
2249 * *.domain.com
2250 * .domain.com
2251 * host.domain.com
2252 * nnn.*
2253 * nnn.nnn.*
2254 * nnn.nnn.nnn.*
2255 * nnn.nnn.nnn.nnn
2256 * nnn.nnn.nnn.nnn/mm
2257 * nnn.nnn.nnn.nnn/mmm.mmm.mmm.mmm
2258 */
2259
2260 if (value[0] == '*' || value[0] == '.' || !isdigit(value[0]))
2261 {
2262 /*
2263 * Host or domain name...
2264 */
2265
2266 if (value[0] == '*')
2267 value ++;
2268
2269 strlcpy(temp, value, sizeof(temp));
2270 if ((ptr = strchr(temp, ' ')) != NULL)
2271 *ptr = '\0';
2272
2273 relay->from.type = AUTH_NAME;
2274 relay->from.mask.name.name = strdup(temp);
2275 relay->from.mask.name.length = strlen(temp);
2276 }
2277 else
2278 {
2279 /*
2280 * One of many IP address forms...
2281 */
2282
2283 if (!get_addr_and_mask(value, ip, mask))
2284 {
2285 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad netmask value %s on line %d.",
2286 value, linenum);
2287 break;
2288 }
2289
2290 relay->from.type = AUTH_IP;
2291 memcpy(relay->from.mask.ip.address, ip,
2292 sizeof(relay->from.mask.ip.address));
2293 memcpy(relay->from.mask.ip.netmask, mask,
2294 sizeof(relay->from.mask.ip.netmask));
2295 }
2296
2297 /*
2298 * Skip value and trailing whitespace...
2299 */
2300
2301 for (; *value; value ++)
2302 if (isspace(*value))
2303 break;
2304
2305 while (isspace(*value))
2306 value ++;
2307
2308 if (!strncasecmp(value, "to ", 3))
2309 {
2310 /*
2311 * Strip leading "to"...
2312 */
2313
2314 value += 3;
2315
2316 while (isspace(*value))
2317 value ++;
2318 }
2319
2320 /*
2321 * Get "to" address and port...
2322 */
2323
2324 if ((addrlist = get_address(value, INADDR_BROADCAST, BrowsePort,
2325 AF_INET)) != NULL)
2326 {
2327 memcpy(&(relay->to), &(addrlist->addr), sizeof(relay->to));
2328 httpAddrFreeList(addrlist);
2329
2330 httpAddrString(&(relay->to), temp, sizeof(temp));
2331
2332 if (relay->from.type == AUTH_IP)
2333 snprintf(temp2, sizeof(temp2), "%u.%u.%u.%u/%u.%u.%u.%u",
2334 relay->from.mask.ip.address[0],
2335 relay->from.mask.ip.address[1],
2336 relay->from.mask.ip.address[2],
2337 relay->from.mask.ip.address[3],
2338 relay->from.mask.ip.netmask[0],
2339 relay->from.mask.ip.netmask[1],
2340 relay->from.mask.ip.netmask[2],
2341 relay->from.mask.ip.netmask[3]);
2342 else
2343 strlcpy(temp2, relay->from.mask.name.name, sizeof(temp2));
2344
2345 #ifdef AF_INET6
2346 if (relay->to.addr.sa_family == AF_INET6)
2347 cupsdLogMessage(CUPSD_LOG_INFO, "Relaying from %s to %s:%d (IPv6)",
2348 temp, temp2, ntohs(relay->to.ipv6.sin6_port));
2349 else
2350 #endif /* AF_INET6 */
2351 cupsdLogMessage(CUPSD_LOG_INFO, "Relaying from %s to %s:%d (IPv4)",
2352 temp, temp2, ntohs(relay->to.ipv4.sin_port));
2353
2354 NumRelays ++;
2355 }
2356 else
2357 {
2358 if (relay->from.type == AUTH_NAME)
2359 free(relay->from.mask.name.name);
2360
2361 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad relay address %s at line %d.",
2362 value, linenum);
2363 }
2364 }
2365 else if (!strcasecmp(line, "BrowsePoll"))
2366 {
2367 /*
2368 * BrowsePoll address[:port]
2369 */
2370
2371 char *portname; /* Port name */
2372 int portnum; /* Port number */
2373 struct servent *service; /* Service */
2374
2375
2376 /*
2377 * Extract the port name from the address...
2378 */
2379
2380 if ((portname = strrchr(value, ':')) != NULL && !strchr(portname, ']'))
2381 {
2382 *portname++ = '\0';
2383
2384 if (isdigit(*portname & 255))
2385 portnum = atoi(portname);
2386 else if ((service = getservbyname(portname, NULL)) != NULL)
2387 portnum = ntohs(service->s_port);
2388 else
2389 {
2390 cupsdLogMessage(CUPSD_LOG_ERROR, "Lookup of service \"%s\" failed!",
2391 portname);
2392 continue;
2393 }
2394 }
2395 else
2396 portnum = ippPort();
2397
2398 /*
2399 * Add the poll entry...
2400 */
2401
2402 if (NumPolled == 0)
2403 poll = malloc(sizeof(cupsd_dirsvc_poll_t));
2404 else
2405 poll = realloc(Polled, (NumPolled + 1) * sizeof(cupsd_dirsvc_poll_t));
2406
2407 if (!poll)
2408 {
2409 cupsdLogMessage(CUPSD_LOG_ERROR,
2410 "Unable to allocate BrowsePoll at line %d - %s.",
2411 linenum, strerror(errno));
2412 continue;
2413 }
2414
2415 Polled = poll;
2416 poll += NumPolled;
2417
2418 NumPolled ++;
2419 memset(poll, 0, sizeof(cupsd_dirsvc_poll_t));
2420
2421 strlcpy(poll->hostname, value, sizeof(poll->hostname));
2422 poll->port = portnum;
2423
2424 cupsdLogMessage(CUPSD_LOG_INFO, "Polling %s:%d", poll->hostname,
2425 poll->port);
2426 }
2427 else if (!strcasecmp(line, "DefaultAuthType"))
2428 {
2429 /*
2430 * DefaultAuthType {basic,digest,basicdigest}
2431 */
2432
2433 if (!strcasecmp(value, "basic"))
2434 DefaultAuthType = AUTH_BASIC;
2435 else if (!strcasecmp(value, "digest"))
2436 DefaultAuthType = AUTH_DIGEST;
2437 else if (!strcasecmp(value, "basicdigest"))
2438 DefaultAuthType = AUTH_BASICDIGEST;
2439 else
2440 {
2441 cupsdLogMessage(CUPSD_LOG_WARN,
2442 "Unknown default authorization type %s on line %d.",
2443 value, linenum);
2444 return (0);
2445 }
2446 }
2447 else if (!strcasecmp(line, "User"))
2448 {
2449 /*
2450 * User ID to run as...
2451 */
2452
2453 if (isdigit(value[0]))
2454 User = atoi(value);
2455 else
2456 {
2457 struct passwd *p; /* Password information */
2458
2459 endpwent();
2460 p = getpwnam(value);
2461
2462 if (p != NULL)
2463 User = p->pw_uid;
2464 else
2465 cupsdLogMessage(CUPSD_LOG_ERROR,
2466 "Unknown User \"%s\" on line %d, ignoring!",
2467 value, linenum);
2468 }
2469 }
2470 else if (!strcasecmp(line, "Group"))
2471 {
2472 /*
2473 * Group ID to run as...
2474 */
2475
2476 if (isdigit(value[0]))
2477 Group = atoi(value);
2478 else
2479 {
2480 endgrent();
2481 group = getgrnam(value);
2482
2483 if (group != NULL)
2484 Group = group->gr_gid;
2485 else
2486 cupsdLogMessage(CUPSD_LOG_ERROR,
2487 "Unknown Group \"%s\" on line %d, ignoring!",
2488 value, linenum);
2489 }
2490 }
2491 else if (!strcasecmp(line, "SystemGroup"))
2492 {
2493 /*
2494 * System (admin) group(s)...
2495 */
2496
2497 for (i = NumSystemGroups; *value && i < MAX_SYSTEM_GROUPS;)
2498 {
2499 if (*value == '\'' || *value == '\"')
2500 {
2501 /*
2502 * Scan quoted name...
2503 */
2504
2505 quote = *value++;
2506
2507 for (valueptr = value; *valueptr; valueptr ++)
2508 if (*valueptr == quote)
2509 break;
2510 }
2511 else
2512 {
2513 /*
2514 * Scan space or comma-delimited name...
2515 */
2516
2517 for (valueptr = value; *valueptr; valueptr ++)
2518 if (isspace(*valueptr) || *valueptr == ',')
2519 break;
2520 }
2521
2522 if (*valueptr)
2523 *valueptr++ = '\0';
2524
2525 group = getgrnam(value);
2526 if (group)
2527 {
2528 cupsdSetString(SystemGroups + i, value);
2529 SystemGroupIDs[i] = group->gr_gid;
2530
2531 i ++;
2532 }
2533 else
2534 cupsdLogMessage(CUPSD_LOG_ERROR,
2535 "Unknown SystemGroup \"%s\" on line %d, ignoring!",
2536 value, linenum);
2537
2538 endgrent();
2539
2540 value = valueptr;
2541
2542 while (*value == ',' || isspace(*value))
2543 value ++;
2544 }
2545
2546 if (i)
2547 NumSystemGroups = i;
2548 }
2549 else if (!strcasecmp(line, "HostNameLookups"))
2550 {
2551 /*
2552 * Do hostname lookups?
2553 */
2554
2555 if (!strcasecmp(value, "off"))
2556 HostNameLookups = 0;
2557 else if (!strcasecmp(value, "on"))
2558 HostNameLookups = 1;
2559 else if (!strcasecmp(value, "double"))
2560 HostNameLookups = 2;
2561 else
2562 cupsdLogMessage(CUPSD_LOG_WARN, "Unknown HostNameLookups %s on line %d.",
2563 value, linenum);
2564 }
2565 else if (!strcasecmp(line, "LogLevel"))
2566 {
2567 /*
2568 * Amount of logging to do...
2569 */
2570
2571 if (!strcasecmp(value, "debug2"))
2572 LogLevel = CUPSD_LOG_DEBUG2;
2573 else if (!strcasecmp(value, "debug"))
2574 LogLevel = CUPSD_LOG_DEBUG;
2575 else if (!strcasecmp(value, "info"))
2576 LogLevel = CUPSD_LOG_INFO;
2577 else if (!strcasecmp(value, "notice"))
2578 LogLevel = CUPSD_LOG_NOTICE;
2579 else if (!strcasecmp(value, "warn"))
2580 LogLevel = CUPSD_LOG_WARN;
2581 else if (!strcasecmp(value, "error"))
2582 LogLevel = CUPSD_LOG_ERROR;
2583 else if (!strcasecmp(value, "crit"))
2584 LogLevel = CUPSD_LOG_CRIT;
2585 else if (!strcasecmp(value, "alert"))
2586 LogLevel = CUPSD_LOG_ALERT;
2587 else if (!strcasecmp(value, "emerg"))
2588 LogLevel = CUPSD_LOG_EMERG;
2589 else if (!strcasecmp(value, "none"))
2590 LogLevel = CUPSD_LOG_NONE;
2591 else
2592 cupsdLogMessage(CUPSD_LOG_WARN, "Unknown LogLevel %s on line %d.",
2593 value, linenum);
2594 }
2595 else if (!strcasecmp(line, "PrintcapFormat"))
2596 {
2597 /*
2598 * Format of printcap file?
2599 */
2600
2601 if (!strcasecmp(value, "bsd"))
2602 PrintcapFormat = PRINTCAP_BSD;
2603 else if (!strcasecmp(value, "solaris"))
2604 PrintcapFormat = PRINTCAP_SOLARIS;
2605 else
2606 cupsdLogMessage(CUPSD_LOG_WARN, "Unknown PrintcapFormat %s on line %d.",
2607 value, linenum);
2608 }
2609 else if (!strcasecmp(line, "ServerTokens"))
2610 {
2611 /*
2612 * Set the string used for the Server header...
2613 */
2614
2615 struct utsname plat; /* Platform info */
2616
2617
2618 uname(&plat);
2619
2620 if (!strcasecmp(value, "ProductOnly"))
2621 cupsdSetString(&ServerHeader, "CUPS");
2622 else if (!strcasecmp(value, "Major"))
2623 cupsdSetString(&ServerHeader, "CUPS/1");
2624 else if (!strcasecmp(value, "Minor"))
2625 cupsdSetString(&ServerHeader, "CUPS/1.1");
2626 else if (!strcasecmp(value, "Minimal"))
2627 cupsdSetString(&ServerHeader, CUPS_MINIMAL);
2628 else if (!strcasecmp(value, "OS"))
2629 cupsdSetStringf(&ServerHeader, CUPS_MINIMAL " (%s)", plat.sysname);
2630 else if (!strcasecmp(value, "Full"))
2631 cupsdSetStringf(&ServerHeader, CUPS_MINIMAL " (%s) IPP/1.1",
2632 plat.sysname);
2633 else if (!strcasecmp(value, "None"))
2634 cupsdClearString(&ServerHeader);
2635 else
2636 cupsdLogMessage(CUPSD_LOG_WARN, "Unknown ServerTokens %s on line %d.",
2637 value, linenum);
2638 }
2639 else if (!strcasecmp(line, "PassEnv"))
2640 {
2641 /*
2642 * PassEnv variable [... variable]
2643 */
2644
2645 for (; *value;)
2646 {
2647 for (valuelen = 0; value[valuelen]; valuelen ++)
2648 if (isspace(value[valuelen]) || value[valuelen] == ',')
2649 break;
2650
2651 if (value[valuelen])
2652 {
2653 value[valuelen] = '\0';
2654 valuelen ++;
2655 }
2656
2657 cupsdSetEnv(value, NULL);
2658
2659 for (value += valuelen; *value; value ++)
2660 if (!isspace(*value) || *value != ',')
2661 break;
2662 }
2663 }
2664 else if (!strcasecmp(line, "SetEnv"))
2665 {
2666 /*
2667 * SetEnv variable value
2668 */
2669
2670 for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++);
2671
2672 if (*valueptr)
2673 {
2674 /*
2675 * Found a value...
2676 */
2677
2678 while (isspace(*valueptr & 255))
2679 *valueptr++ = '\0';
2680
2681 cupsdSetEnv(value, valueptr);
2682 }
2683 else
2684 cupsdLogMessage(CUPSD_LOG_ERROR,
2685 "Missing value for SetEnv directive on line %d.",
2686 linenum);
2687 }
2688 else
2689 {
2690 /*
2691 * Find a simple variable in the list...
2692 */
2693
2694 for (i = NUM_VARS, var = variables; i > 0; i --, var ++)
2695 if (!strcasecmp(line, var->name))
2696 break;
2697
2698 if (i == 0)
2699 {
2700 /*
2701 * Unknown directive! Output an error message and continue...
2702 */
2703
2704 cupsdLogMessage(CUPSD_LOG_ERROR, "Unknown directive %s on line %d.",
2705 line, linenum);
2706 continue;
2707 }
2708
2709 switch (var->type)
2710 {
2711 case CUPSD_VARTYPE_INTEGER :
2712 {
2713 int n; /* Number */
2714 char *units; /* Units */
2715
2716
2717 n = strtol(value, &units, 0);
2718
2719 if (units && *units)
2720 {
2721 if (tolower(units[0] & 255) == 'g')
2722 n *= 1024 * 1024 * 1024;
2723 else if (tolower(units[0] & 255) == 'm')
2724 n *= 1024 * 1024;
2725 else if (tolower(units[0] & 255) == 'k')
2726 n *= 1024;
2727 else if (tolower(units[0] & 255) == 't')
2728 n *= 262144;
2729 }
2730
2731 *((int *)var->ptr) = n;
2732 }
2733 break;
2734
2735 case CUPSD_VARTYPE_BOOLEAN :
2736 if (!strcasecmp(value, "true") ||
2737 !strcasecmp(value, "on") ||
2738 !strcasecmp(value, "enabled") ||
2739 !strcasecmp(value, "yes") ||
2740 atoi(value) != 0)
2741 *((int *)var->ptr) = TRUE;
2742 else if (!strcasecmp(value, "false") ||
2743 !strcasecmp(value, "off") ||
2744 !strcasecmp(value, "disabled") ||
2745 !strcasecmp(value, "no") ||
2746 !strcasecmp(value, "0"))
2747 *((int *)var->ptr) = FALSE;
2748 else
2749 cupsdLogMessage(CUPSD_LOG_ERROR,
2750 "Unknown boolean value %s on line %d.",
2751 value, linenum);
2752 break;
2753
2754 case CUPSD_VARTYPE_STRING :
2755 cupsdSetString((char **)var->ptr, value);
2756 break;
2757 }
2758 }
2759 }
2760
2761 return (1);
2762 }
2763
2764
2765 /*
2766 * 'read_location()' - Read a <Location path> definition.
2767 */
2768
2769 static int /* O - New line number or 0 on error */
2770 read_location(cups_file_t *fp, /* I - Configuration file */
2771 char *location, /* I - Location name/path */
2772 int linenum) /* I - Current line number */
2773 {
2774 cupsd_location_t *loc, /* New location */
2775 *parent; /* Parent location */
2776 char line[HTTP_MAX_BUFFER],
2777 /* Line buffer */
2778 *value, /* Value for directive */
2779 *valptr; /* Pointer into value */
2780
2781
2782 if ((parent = cupsdAddLocation(location)) == NULL)
2783 return (0);
2784
2785 parent->limit = AUTH_LIMIT_ALL;
2786 loc = parent;
2787
2788 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
2789 {
2790 /*
2791 * Decode the directive...
2792 */
2793
2794 if (!strcasecmp(line, "</Location>"))
2795 return (linenum);
2796 else if (!strcasecmp(line, "<Limit") ||
2797 !strcasecmp(line, "<LimitExcept"))
2798 {
2799 if (!value)
2800 {
2801 cupsdLogMessage(CUPSD_LOG_ERROR, "Syntax error on line %d.", linenum);
2802 return (0);
2803 }
2804
2805 if ((loc = cupsdCopyLocation(&parent)) == NULL)
2806 return (0);
2807
2808 loc->limit = 0;
2809 while (*value)
2810 {
2811 for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
2812
2813 if (*valptr)
2814 *valptr++ = '\0';
2815
2816 if (!strcmp(value, "ALL"))
2817 loc->limit = AUTH_LIMIT_ALL;
2818 else if (!strcmp(value, "GET"))
2819 loc->limit |= AUTH_LIMIT_GET;
2820 else if (!strcmp(value, "HEAD"))
2821 loc->limit |= AUTH_LIMIT_HEAD;
2822 else if (!strcmp(value, "OPTIONS"))
2823 loc->limit |= AUTH_LIMIT_OPTIONS;
2824 else if (!strcmp(value, "POST"))
2825 loc->limit |= AUTH_LIMIT_POST;
2826 else if (!strcmp(value, "PUT"))
2827 loc->limit |= AUTH_LIMIT_PUT;
2828 else if (!strcmp(value, "TRACE"))
2829 loc->limit |= AUTH_LIMIT_TRACE;
2830 else
2831 cupsdLogMessage(CUPSD_LOG_WARN, "Unknown request type %s on line %d!",
2832 value, linenum);
2833
2834 for (value = valptr; isspace(*value & 255); value ++);
2835 }
2836
2837 if (!strcasecmp(line, "<LimitExcept"))
2838 loc->limit = AUTH_LIMIT_ALL ^ loc->limit;
2839
2840 parent->limit &= ~loc->limit;
2841 }
2842 else if (!strcasecmp(line, "</Limit>"))
2843 loc = parent;
2844 else if (!parse_aaa(loc, line, value, linenum))
2845 {
2846 cupsdLogMessage(CUPSD_LOG_ERROR,
2847 "Unknown Location directive %s on line %d.",
2848 line, linenum);
2849 return (0);
2850 }
2851 }
2852
2853 cupsdLogMessage(CUPSD_LOG_ERROR,
2854 "Unexpected end-of-file at line %d while reading location!",
2855 linenum);
2856
2857 return (0);
2858 }
2859
2860
2861 /*
2862 * 'read_policy()' - Read a <Policy name> definition.
2863 */
2864
2865 static int /* O - New line number or 0 on error */
2866 read_policy(cups_file_t *fp, /* I - Configuration file */
2867 char *policy, /* I - Location name/path */
2868 int linenum) /* I - Current line number */
2869 {
2870 int i; /* Looping var */
2871 cupsd_policy_t *pol; /* Policy */
2872 cupsd_location_t *op; /* Policy operation */
2873 int num_ops; /* Number of IPP operations */
2874 ipp_op_t ops[100]; /* Operations */
2875 char line[HTTP_MAX_BUFFER],
2876 /* Line buffer */
2877 *value, /* Value for directive */
2878 *valptr; /* Pointer into value */
2879
2880
2881 /*
2882 * Create the policy...
2883 */
2884
2885 if ((pol = cupsdAddPolicy(policy)) == NULL)
2886 return (0);
2887
2888 /*
2889 * Read from the file...
2890 */
2891
2892 op = NULL;
2893 num_ops = 0;
2894
2895 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
2896 {
2897 /*
2898 * Decode the directive...
2899 */
2900
2901 if (!strcasecmp(line, "</Policy>"))
2902 {
2903 if (op)
2904 cupsdLogMessage(CUPSD_LOG_WARN,
2905 "Missing </Limit> before </Policy> on line %d!",
2906 linenum);
2907
2908 return (linenum);
2909 }
2910 else if (!strcasecmp(line, "<Limit") && !op)
2911 {
2912 if (!value)
2913 {
2914 cupsdLogMessage(CUPSD_LOG_ERROR, "Syntax error on line %d.", linenum);
2915 return (0);
2916 }
2917
2918 /*
2919 * Scan for IPP operation names...
2920 */
2921
2922 num_ops = 0;
2923
2924 while (*value)
2925 {
2926 for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
2927
2928 if (*valptr)
2929 *valptr++ = '\0';
2930
2931 if (num_ops < (int)(sizeof(ops) / sizeof(ops[0])))
2932 {
2933 if (!strcasecmp(value, "All"))
2934 ops[num_ops] = IPP_ANY_OPERATION;
2935 else if ((ops[num_ops] = ippOpValue(value)) == IPP_BAD_OPERATION)
2936 cupsdLogMessage(CUPSD_LOG_ERROR,
2937 "Bad IPP operation name \"%s\" on line %d!",
2938 value, linenum);
2939 else
2940 num_ops ++;
2941 }
2942 else
2943 cupsdLogMessage(CUPSD_LOG_ERROR,
2944 "Too many operations listed on line %d!",
2945 linenum);
2946
2947 for (value = valptr; isspace(*value & 255); value ++);
2948 }
2949
2950 /*
2951 * If none are specified, apply the policy to all operations...
2952 */
2953
2954 if (num_ops == 0)
2955 {
2956 ops[0] = IPP_ANY_OPERATION;
2957 num_ops = 1;
2958 }
2959
2960 /*
2961 * Add a new policy for the first operation...
2962 */
2963
2964 op = cupsdAddPolicyOp(pol, NULL, ops[0]);
2965 }
2966 else if (!strcasecmp(line, "</Limit>") && op)
2967 {
2968 /*
2969 * Finish the current operation limit...
2970 */
2971
2972 if (num_ops > 1)
2973 {
2974 /*
2975 * Copy the policy to the other operations...
2976 */
2977
2978 for (i = 1; i < num_ops; i ++)
2979 cupsdAddPolicyOp(pol, op, ops[i]);
2980 }
2981
2982 op = NULL;
2983 }
2984 else if (!op)
2985 {
2986 cupsdLogMessage(CUPSD_LOG_ERROR,
2987 "Missing <Limit ops> directive before %s on line %d.",
2988 line, linenum);
2989 return (0);
2990 }
2991 else if (!parse_aaa(op, line, value, linenum))
2992 {
2993 if (op)
2994 cupsdLogMessage(CUPSD_LOG_ERROR,
2995 "Unknown Policy Limit directive %s on line %d.",
2996 line, linenum);
2997 else
2998 cupsdLogMessage(CUPSD_LOG_ERROR,
2999 "Unknown Policy directive %s on line %d.",
3000 line, linenum);
3001
3002 return (0);
3003 }
3004 }
3005
3006 cupsdLogMessage(CUPSD_LOG_ERROR,
3007 "Unexpected end-of-file at line %d while reading policy \"%s\"!",
3008 linenum, policy);
3009
3010 return (0);
3011 }
3012
3013
3014 #ifdef HAVE_CDSASSL
3015 /*
3016 * 'CDSAGetServerCerts()' - Convert a keychain name into the CFArrayRef
3017 * required by SSLSetCertificate.
3018 *
3019 * For now we assumes that there is exactly one SecIdentity in the
3020 * keychain - i.e. there is exactly one matching cert/private key pair.
3021 * In the future we will search a keychain for a SecIdentity matching a
3022 * specific criteria. We also skip the operation of adding additional
3023 * non-signing certs from the keychain to the CFArrayRef.
3024 *
3025 * To create a self-signed certificate for testing use the certtool.
3026 * Executing the following as root will do it:
3027 *
3028 * certtool c c v k=CUPS
3029 */
3030
3031 CFArrayRef
3032 CDSAGetServerCerts(void)
3033 {
3034 OSStatus err; /* Error info */
3035 SecKeychainRef kcRef; /* Keychain reference */
3036 SecIdentitySearchRef srchRef; /* Search reference */
3037 SecIdentityRef identity; /* Identity */
3038 CFArrayRef ca; /* Certificate array */
3039
3040
3041 kcRef = NULL;
3042 srchRef = NULL;
3043 identity = NULL;
3044 ca = NULL;
3045 err = SecKeychainOpen(ServerCertificate, &kcRef);
3046
3047 if (err)
3048 cupsdLogMessage(CUPSD_LOG_ERROR, "Cannot open keychain \"%s\", error %d.",
3049 ServerCertificate, err);
3050 else
3051 {
3052 /*
3053 * Search for "any" identity matching specified key use;
3054 * in this app, we expect there to be exactly one.
3055 */
3056
3057 err = SecIdentitySearchCreate(kcRef, CSSM_KEYUSE_SIGN, &srchRef);
3058
3059 if (err)
3060 cupsdLogMessage(CUPSD_LOG_ERROR,
3061 "Cannot find signing key in keychain \"%s\", error %d",
3062 ServerCertificate, err);
3063 else
3064 {
3065 err = SecIdentitySearchCopyNext(srchRef, &identity);
3066
3067 if (err)
3068 cupsdLogMessage(CUPSD_LOG_ERROR,
3069 "Cannot find signing key in keychain \"%s\", error %d",
3070 ServerCertificate, err);
3071 else
3072 {
3073 if (CFGetTypeID(identity) != SecIdentityGetTypeID())
3074 cupsdLogMessage(CUPSD_LOG_ERROR,
3075 "SecIdentitySearchCopyNext CFTypeID failure!");
3076 else
3077 {
3078 /*
3079 * Found one. Place it in a CFArray.
3080 * TBD: snag other (non-identity) certs from keychain and add them
3081 * to array as well.
3082 */
3083
3084 ca = CFArrayCreate(NULL, (const void **)&identity, 1, NULL);
3085
3086 if (ca == nil)
3087 cupsdLogMessage(CUPSD_LOG_ERROR, "CFArrayCreate error");
3088 }
3089
3090 /*CFRelease(identity);*/
3091 }
3092
3093 /*CFRelease(srchRef);*/
3094 }
3095
3096 /*CFRelease(kcRef);*/
3097 }
3098
3099 return ca;
3100 }
3101 #endif /* HAVE_CDSASSL */
3102
3103
3104 /*
3105 * End of "$Id$".
3106 */