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