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