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