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