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