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