]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/adminutil.c
More svn:properties changes.
[thirdparty/cups.git] / cups / adminutil.c
1 /*
2 * "$Id$"
3 *
4 * Administration utility API definitions for the Common UNIX Printing
5 * System (CUPS).
6 *
7 * MANY OF THE FUNCTIONS IN THIS HEADER ARE PRIVATE AND SUBJECT TO
8 * CHANGE AT ANY TIME. USE AT YOUR OWN RISK.
9 *
10 * Copyright 2001-2006 by Easy Software Products.
11 *
12 * These coded instructions, statements, and computer programs are the
13 * property of Easy Software Products and are protected by Federal
14 * copyright law. Distribution and use rights are outlined in the file
15 * "LICENSE.txt" which should have been included with this file. If this
16 * file is missing or damaged please contact Easy Software Products
17 * at:
18 *
19 * Attn: CUPS Licensing Information
20 * Easy Software Products
21 * 44141 Airport View Drive, Suite 204
22 * Hollywood, Maryland 20636 USA
23 *
24 * Voice: (301) 373-9600
25 * EMail: cups-info@cups.org
26 * WWW: http://www.cups.org
27 *
28 * This file is subject to the Apple OS-Developed Software exception.
29 *
30 * Contents:
31 *
32 * cupsAdminCreateWindowsPPD() - Create the Windows PPD file for a printer.
33 * cupsAdminExportSamba() - Export a printer to Samba.
34 * _cupsAdminGetServerSettings() - Get settings from the server.
35 * _cupsAdminSetServerSettings() - Set settings on the server.
36 * do_samba_command() - Do a SAMBA command.
37 * get_cupsd_conf() - Get the current cupsd.conf file.
38 * invalidate_cupsd_cache() - Invalidate the cached cupsd.conf settings.
39 * write_option() - Write a CUPS option to a PPD file.
40 */
41
42 /*
43 * Include necessary headers...
44 */
45
46 #include "adminutil.h"
47 #include "globals.h"
48 #include "debug.h"
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <unistd.h>
52 #include <sys/stat.h>
53 #include <sys/wait.h>
54
55
56 /*
57 * Local functions...
58 */
59
60 static int do_samba_command(const char *command,
61 const char *address,
62 const char *subcommand,
63 const char *authfile,
64 FILE *logfile);
65 static http_status_t get_cupsd_conf(http_t *http, _cups_globals_t *cg,
66 time_t last_update, char *name,
67 int namelen, int *remote);
68 static void invalidate_cupsd_cache(_cups_globals_t *cg);
69 static void write_option(cups_file_t *dstfp, int order,
70 const char *name, const char *text,
71 const char *attrname,
72 ipp_attribute_t *suppattr,
73 ipp_attribute_t *defattr, int defval,
74 int valcount);
75
76
77 /*
78 * 'cupsAdminCreateWindowsPPD()' - Create the Windows PPD file for a printer.
79 */
80
81 char * /* O - PPD file or NULL */
82 cupsAdminCreateWindowsPPD(
83 http_t *http, /* I - Connection to server */
84 const char *dest, /* I - Printer or class */
85 char *buffer, /* I - Filename buffer */
86 int bufsize) /* I - Size of filename buffer */
87 {
88 const char *src; /* Source PPD filename */
89 cups_file_t *srcfp, /* Source PPD file */
90 *dstfp; /* Destination PPD file */
91 ipp_t *request, /* IPP request */
92 *response; /* IPP response */
93 ipp_attribute_t *suppattr, /* IPP -supported attribute */
94 *defattr; /* IPP -default attribute */
95 cups_lang_t *language; /* Current language */
96 char line[256], /* Line from PPD file */
97 junk[256], /* Extra junk to throw away */
98 *ptr, /* Pointer into line */
99 uri[1024], /* Printer URI */
100 option[41], /* Option */
101 choice[41]; /* Choice */
102 int jcloption, /* In a JCL option? */
103 linenum; /* Current line number */
104 time_t curtime; /* Current time */
105 struct tm *curdate; /* Current date */
106 static const char *pattrs[] = /* Printer attributes we want */
107 {
108 "job-hold-until-supported",
109 "job-hold-until-default",
110 "job-sheets-supported",
111 "job-sheets-default",
112 "job-priority-supported",
113 "job-priority-default"
114 };
115
116
117 /*
118 * Range check the input...
119 */
120
121 if (buffer)
122 *buffer = '\0';
123
124 if (!http || !dest || !buffer || bufsize < 2)
125 return (NULL);
126
127 /*
128 * Get the PPD file...
129 */
130
131 if ((src = cupsGetPPD2(http, dest)) == NULL)
132 return (NULL);
133
134 /*
135 * Get the supported banner pages, etc. for the printer...
136 */
137
138 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
139
140 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
141 "localhost", 0, "/printers/%s", dest);
142 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
143 "printer-uri", NULL, uri);
144
145 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
146 "requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]),
147 NULL, pattrs);
148
149 /*
150 * Do the request and get back a response...
151 */
152
153 response = cupsDoRequest(http, request, "/");
154 if (!response || cupsLastError() > IPP_OK_CONFLICT)
155 {
156 unlink(src);
157 return (NULL);
158 }
159
160 /*
161 * Open the original PPD file...
162 */
163
164 if ((srcfp = cupsFileOpen(src, "rb")) == NULL)
165 return (NULL);
166
167 /*
168 * Create a temporary output file using the destination buffer...
169 */
170
171 if ((dstfp = cupsTempFile2(buffer, bufsize)) == NULL)
172 {
173 cupsFileClose(srcfp);
174
175 unlink(src);
176
177 return (NULL);
178 }
179
180 /*
181 * Write a new header explaining that this isn't the original PPD...
182 */
183
184 cupsFilePuts(dstfp, "*PPD-Adobe: \"4.3\"\n");
185
186 curtime = time(NULL);
187 curdate = gmtime(&curtime);
188
189 cupsFilePrintf(dstfp, "*%% Modified on %04d%02d%02d%02d%02d%02d+0000 "
190 "for CUPS Windows Driver\n",
191 curdate->tm_year + 1900, curdate->tm_mon + 1, curdate->tm_mday,
192 curdate->tm_hour, curdate->tm_min, curdate->tm_sec);
193
194 /*
195 * Read the existing PPD file, converting all PJL commands to CUPS
196 * job ticket comments...
197 */
198
199 jcloption = 0;
200 linenum = 0;
201 language = cupsLangDefault();
202
203 while (cupsFileGets(srcfp, line, sizeof(line)))
204 {
205 linenum ++;
206
207 if (!strncmp(line, "*PPD-Adobe:", 11))
208 {
209 /*
210 * Already wrote the PPD header...
211 */
212
213 continue;
214 }
215 else if (!strncmp(line, "*JCLBegin:", 10) ||
216 !strncmp(line, "*JCLToPSInterpreter:", 20) ||
217 !strncmp(line, "*JCLEnd:", 8) ||
218 !strncmp(line, "*Protocols:", 11))
219 {
220 /*
221 * Don't use existing JCL keywords; we'll create our own, below...
222 */
223
224 cupsFilePrintf(dstfp, "*%% Commented out for CUPS Windows Driver...\n"
225 "*%%%s", line + 1);
226 continue;
227 }
228 else if (!strncmp(line, "*JCLOpenUI", 10))
229 {
230 jcloption = 1;
231 cupsFilePuts(dstfp, line);
232 }
233 else if (!strncmp(line, "*JCLCloseUI", 11))
234 {
235 jcloption = 0;
236 cupsFilePuts(dstfp, line);
237 }
238 else if (jcloption &&
239 strncmp(line, "*End", 4) &&
240 strncmp(line, "*Default", 8) &&
241 strncmp(line, "*OrderDependency", 16))
242 {
243 if ((ptr = strchr(line, ':')) == NULL)
244 {
245 snprintf(line, sizeof(line),
246 _cupsLangString(language, _("Missing value on line %d!\n")),
247 linenum);
248 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, line);
249
250 cupsFileClose(srcfp);
251 cupsFileClose(dstfp);
252
253 unlink(src);
254 unlink(buffer);
255
256 *buffer = '\0';
257
258 return (NULL);
259 }
260
261 if ((ptr = strchr(ptr, '\"')) == NULL)
262 {
263 snprintf(line, sizeof(line),
264 _cupsLangString(language,
265 _("Missing double quote on line %d!\n")),
266 linenum);
267 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, line);
268
269 cupsFileClose(srcfp);
270 cupsFileClose(dstfp);
271
272 unlink(src);
273 unlink(buffer);
274
275 *buffer = '\0';
276
277 return (NULL);
278 }
279
280 if (sscanf(line, "*%40s%*[ \t]%40[^/]", option, choice) != 2)
281 {
282 snprintf(line, sizeof(line),
283 _cupsLangString(language,
284 _("Bad option + choice on line %d!\n")),
285 linenum);
286 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, line);
287
288 cupsFileClose(srcfp);
289 cupsFileClose(dstfp);
290
291 unlink(src);
292 unlink(buffer);
293
294 *buffer = '\0';
295
296 return (NULL);
297 }
298
299 if (strchr(ptr + 1, '\"') == NULL)
300 {
301 /*
302 * Skip remaining...
303 */
304
305 while (cupsFileGets(srcfp, junk, sizeof(junk)) != NULL)
306 {
307 linenum ++;
308
309 if (!strncmp(junk, "*End", 4))
310 break;
311 }
312 }
313
314 snprintf(ptr + 1, sizeof(line) - (ptr - line + 1),
315 "%%cupsJobTicket: %s=%s\n\"\n*End\n", option, choice);
316
317 cupsFilePrintf(dstfp, "*%% Changed for CUPS Windows Driver...\n%s", line);
318 }
319 else
320 cupsFilePuts(dstfp, line);
321 }
322
323 cupsFileClose(srcfp);
324 unlink(src);
325
326 /*
327 * Now add the CUPS-specific attributes and options...
328 */
329
330 cupsFilePuts(dstfp, "\n*% CUPS Job Ticket support and options...\n");
331 cupsFilePuts(dstfp, "*Protocols: PJL\n");
332 cupsFilePuts(dstfp, "*JCLBegin: \"%!PS-Adobe-3.0<0A>\"\n");
333 cupsFilePuts(dstfp, "*JCLToPSInterpreter: \"\"\n");
334 cupsFilePuts(dstfp, "*JCLEnd: \"\"\n");
335
336 cupsFilePuts(dstfp, "\n*OpenGroup: CUPS/CUPS Options\n\n");
337
338 if ((defattr = ippFindAttribute(response, "job-hold-until-default",
339 IPP_TAG_ZERO)) != NULL &&
340 (suppattr = ippFindAttribute(response, "job-hold-until-supported",
341 IPP_TAG_ZERO)) != NULL)
342 write_option(dstfp, 10, "cupsJobHoldUntil", "Hold Until", "job-hold-until",
343 suppattr, defattr, 0, 1);
344
345 if ((defattr = ippFindAttribute(response, "job-priority-default",
346 IPP_TAG_INTEGER)) != NULL &&
347 (suppattr = ippFindAttribute(response, "job-priority-supported",
348 IPP_TAG_RANGE)) != NULL)
349 write_option(dstfp, 11, "cupsJobPriority", "Priority", "job-priority",
350 suppattr, defattr, 0, 1);
351
352 if ((defattr = ippFindAttribute(response, "job-sheets-default",
353 IPP_TAG_ZERO)) != NULL &&
354 (suppattr = ippFindAttribute(response, "job-sheets-supported",
355 IPP_TAG_ZERO)) != NULL)
356 {
357 write_option(dstfp, 20, "cupsJobSheetsStart", "Start Banner",
358 "job-sheets", suppattr, defattr, 0, 2);
359 write_option(dstfp, 21, "cupsJobSheetsEnd", "End Banner",
360 "job-sheets", suppattr, defattr, 1, 2);
361 }
362
363 cupsFilePuts(dstfp, "*CloseGroup: CUPS\n");
364 cupsFileClose(dstfp);
365
366 ippDelete(response);
367
368 return (buffer);
369 }
370
371
372 /*
373 * 'cupsAdminExportSamba()' - Export a printer to Samba.
374 */
375
376 int /* O - 1 on success, 0 on failure */
377 cupsAdminExportSamba(
378 const char *dest, /* I - Destination to export */
379 const char *ppd, /* I - PPD file */
380 const char *samba_server, /* I - Samba server */
381 const char *samba_user, /* I - Samba username */
382 const char *samba_password, /* I - Samba password */
383 FILE *logfile) /* I - Log file, if any */
384 {
385 int status; /* Status of Samba commands */
386 int have_drivers; /* Have drivers? */
387 char file[1024], /* File to test for */
388 authfile[1024], /* Temporary authentication file */
389 address[1024], /* Address for command */
390 subcmd[1024], /* Sub-command */
391 message[1024]; /* Error message */
392 cups_file_t *fp; /* Authentication file */
393 cups_lang_t *language; /* Current language */
394 _cups_globals_t *cg = _cupsGlobals();
395 /* Global data */
396
397
398 /*
399 * Range check input...
400 */
401
402 if (!dest || !ppd || !samba_server || !samba_user || !samba_password)
403 {
404 _cupsSetError(IPP_INTERNAL_ERROR, NULL);
405 return (0);
406 }
407
408 /*
409 * Create a temporary authentication file for Samba...
410 */
411
412 if ((fp = cupsTempFile2(authfile, sizeof(authfile))) == NULL)
413 {
414 _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno));
415 return (0);
416 }
417
418 cupsFilePrintf(fp, "username = %s\n", samba_user);
419 cupsFilePrintf(fp, "password = %s\n", samba_password);
420 cupsFileClose(fp);
421
422 /*
423 * See which drivers are available; the new CUPS v6 and Adobe drivers
424 * depend on the Windows 2k PS driver, so copy that driver first:
425 *
426 * Files:
427 *
428 * ps5ui.dll
429 * pscript.hlp
430 * pscript.ntf
431 * pscript5.dll
432 */
433
434 have_drivers = 0;
435 language = cupsLangDefault();
436
437 snprintf(file, sizeof(file), "%s/drivers/pscript5.dll", cg->cups_datadir);
438 if (!access(file, 0))
439 {
440 have_drivers |= 1;
441
442 /*
443 * Windows 2k driver is installed; do the smbclient commands needed
444 * to copy the Win2k drivers over...
445 */
446
447 snprintf(address, sizeof(address), "//%s/print$", samba_server);
448
449 snprintf(subcmd, sizeof(subcmd),
450 "mkdir W32X86;"
451 "put %s W32X86/%s.ppd;"
452 "put %s/drivers/ps5ui.dll W32X86/ps5ui.dll;"
453 "put %s/drivers/pscript.hlp W32X86/pscript.hlp;"
454 "put %s/drivers/pscript.ntf W32X86/pscript.ntf;"
455 "put %s/drivers/pscript5.dll W32X86/pscript5.dll",
456 ppd, dest, cg->cups_datadir, cg->cups_datadir,
457 cg->cups_datadir, cg->cups_datadir);
458
459 if ((status = do_samba_command("smbclient", address, subcmd,
460 authfile, logfile)) != 0)
461 {
462 snprintf(message, sizeof(message),
463 _cupsLangString(language,
464 _("Unable to copy Windows 2000 printer "
465 "driver files (%d)!")), status);
466
467 _cupsSetError(IPP_INTERNAL_ERROR, message);
468
469 if (logfile)
470 _cupsLangPrintf(logfile, "%s\n", message);
471
472 unlink(authfile);
473
474 return (0);
475 }
476
477 /*
478 * See if we also have the CUPS driver files; if so, use them!
479 */
480
481 snprintf(file, sizeof(file), "%s/drivers/cupsps6.dll", cg->cups_datadir);
482 if (!access(file, 0))
483 {
484 /*
485 * Copy the CUPS driver files over...
486 */
487
488 snprintf(subcmd, sizeof(subcmd),
489 "put %s/drivers/cups6.ini W32X86/cups6.ini;"
490 "put %s/drivers/cupsps6.dll W32X86/cupsps6.dll;"
491 "put %s/drivers/cupsui6.dll W32X86/cupsui6.dll",
492 cg->cups_datadir, cg->cups_datadir, cg->cups_datadir);
493
494 if ((status = do_samba_command("smbclient", address, subcmd,
495 authfile, logfile)) != 0)
496 {
497 snprintf(message, sizeof(message),
498 _cupsLangString(language,
499 _("Unable to copy CUPS printer driver "
500 "files (%d)!")), status);
501
502 _cupsSetError(IPP_INTERNAL_ERROR, message);
503
504 if (logfile)
505 _cupsLangPrintf(logfile, "%s\n", message);
506
507 unlink(authfile);
508
509 return (0);
510 }
511
512 /*
513 * Do the rpcclient command needed for the CUPS drivers...
514 */
515
516 snprintf(subcmd, sizeof(subcmd),
517 "adddriver \"Windows NT x86\" \"%s:"
518 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
519 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
520 "cups6.ini,cupsps6.dll,cupsui6.dll\"",
521 dest, dest, dest);
522 }
523 else
524 {
525 /*
526 * Don't have the CUPS drivers, so just use the standard Windows
527 * drivers...
528 */
529
530 snprintf(subcmd, sizeof(subcmd),
531 "adddriver \"Windows NT x86\" \"%s:"
532 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
533 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
534 dest, dest, dest);
535 }
536
537 if ((status = do_samba_command("rpcclient", samba_server, subcmd,
538 authfile, logfile)) != 0)
539 {
540 snprintf(message, sizeof(message),
541 _cupsLangString(language,
542 _("Unable to install Windows 2000 printer "
543 "driver files (%d)!")), status);
544
545 _cupsSetError(IPP_INTERNAL_ERROR, message);
546
547 if (logfile)
548 _cupsLangPrintf(logfile, "%s\n", message);
549
550 unlink(authfile);
551
552 return (0);
553 }
554 }
555
556 snprintf(file, sizeof(file), "%s/drivers/ADOBEPS4.DRV", cg->cups_datadir);
557 if (!access(file, 0))
558 {
559 have_drivers |= 2;
560
561 /*
562 * Do the smbclient commands needed for the Adobe Win9x drivers...
563 */
564
565 snprintf(address, sizeof(address), "//%s/print$", samba_server);
566
567 snprintf(subcmd, sizeof(subcmd),
568 "mkdir WIN40;"
569 "put %s WIN40/%s.PPD;"
570 "put %s/drivers/ADFONTS.MFM WIN40/ADFONTS.MFM;"
571 "put %s/drivers/ADOBEPS4.DRV WIN40/ADOBEPS4.DRV;"
572 "put %s/drivers/ADOBEPS4.HLP WIN40/ADOBEPS4.HLP;"
573 "put %s/drivers/ICONLIB.DLL WIN40/ICONLIB.DLL;"
574 "put %s/drivers/PSMON.DLL WIN40/PSMON.DLL;",
575 ppd, dest, cg->cups_datadir, cg->cups_datadir,
576 cg->cups_datadir, cg->cups_datadir, cg->cups_datadir);
577
578 if ((status = do_samba_command("smbclient", address, subcmd,
579 authfile, logfile)) != 0)
580 {
581 snprintf(message, sizeof(message),
582 _cupsLangString(language,
583 _("Unable to copy Windows 9x printer "
584 "driver files (%d)!")), status);
585
586 _cupsSetError(IPP_INTERNAL_ERROR, message);
587
588 if (logfile)
589 _cupsLangPrintf(logfile, "%s\n", message);
590
591 unlink(authfile);
592
593 return (0);
594 }
595
596 /*
597 * Do the rpcclient commands needed for the Adobe Win9x drivers...
598 */
599
600 snprintf(subcmd, sizeof(subcmd),
601 "adddriver \"Windows 4.0\" \"%s:ADOBEPS4.DRV:%s.PPD:NULL:"
602 "ADOBEPS4.HLP:PSMON.DLL:RAW:"
603 "ADOBEPS4.DRV,%s.PPD,ADOBEPS4.HLP,PSMON.DLL,ADFONTS.MFM,"
604 "ICONLIB.DLL\"",
605 dest, dest, dest);
606
607 if ((status = do_samba_command("rpcclient", samba_server, subcmd,
608 authfile, logfile)) != 0)
609 {
610 snprintf(message, sizeof(message),
611 _cupsLangString(language,
612 _("Unable to install Windows 9x printer "
613 "driver files (%d)!")), status);
614
615 _cupsSetError(IPP_INTERNAL_ERROR, message);
616
617 if (logfile)
618 _cupsLangPrintf(logfile, "%s\n", message);
619
620 unlink(authfile);
621
622 return (0);
623 }
624 }
625
626 if (logfile && !(have_drivers & 1))
627 {
628 if (!have_drivers)
629 strlcpy(message,
630 _cupsLangString(language,
631 _("No Windows printer drivers are installed!")),
632 sizeof(message));
633 else
634 strlcpy(message,
635 _cupsLangString(language,
636 _("Warning, no Windows 2000 printer drivers "
637 "are installed!")),
638 sizeof(message));
639
640 _cupsSetError(IPP_INTERNAL_ERROR, message);
641 _cupsLangPrintf(logfile, "%s\n", message);
642 }
643
644 if (have_drivers == 0)
645 return (0);
646
647 /*
648 * Finally, associate the drivers we just added with the queue...
649 */
650
651 snprintf(subcmd, sizeof(subcmd), "setdriver %s %s", dest, dest);
652
653 if ((status = do_samba_command("rpcclient", samba_server, subcmd,
654 authfile, logfile)) != 0)
655 {
656 snprintf(message, sizeof(message),
657 _cupsLangString(language,
658 _("Unable to set Windows printer driver (%d)!\n")),
659 status);
660
661 _cupsSetError(IPP_INTERNAL_ERROR, message);
662
663 if (logfile)
664 _cupsLangPrintf(logfile, "%s\n", message);
665
666 unlink(authfile);
667
668 return (0);
669 }
670
671 unlink(authfile);
672
673 return (1);
674 }
675
676
677 /*
678 * '_cupsAdminGetServerSettings()' - Get settings from the server.
679 *
680 * The returned settings should be freed with cupsFreeOptions() when
681 * you are done with them.
682 *
683 * @since CUPS 1.2@
684 */
685
686 int /* O - 1 on success, 0 on failure */
687 _cupsAdminGetServerSettings(
688 http_t *http, /* I - Connection to server */
689 int *num_settings, /* O - Number of settings */
690 cups_option_t **settings) /* O - Settings */
691 {
692 int i; /* Looping var */
693 cups_file_t *cupsd; /* cupsd.conf file */
694 char cupsdconf[1024]; /* cupsd.conf filename */
695 int remote; /* Remote cupsd.conf file? */
696 http_status_t status; /* Status of getting cupsd.conf */
697 char line[1024], /* Line from cupsd.conf file */
698 *value; /* Value on line */
699 cups_option_t *setting; /* Current setting */
700 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
701
702
703 /*
704 * Range check input...
705 */
706
707 if (!http || !num_settings || !settings)
708 {
709 _cupsSetError(IPP_INTERNAL_ERROR, NULL);
710
711 if (num_settings)
712 *num_settings = 0;
713
714 if (settings)
715 *settings = NULL;
716
717 return (0);
718 }
719
720 *num_settings = 0;
721 *settings = NULL;
722
723 /*
724 * Get the cupsd.conf file...
725 */
726
727 if ((status = get_cupsd_conf(http, cg, cg->cupsd_update, cupsdconf,
728 sizeof(cupsdconf), &remote)) == HTTP_OK)
729 {
730 if ((cupsd = cupsFileOpen(cupsdconf, "r")) == NULL)
731 {
732 char message[1024]; /* Message string */
733
734
735 snprintf(message, sizeof(message),
736 _cupsLangString(cupsLangDefault(), _("open of %s failed: %s")),
737 cupsdconf, strerror(errno));
738 _cupsSetError(IPP_INTERNAL_ERROR, message);
739 }
740 }
741 else
742 cupsd = NULL;
743
744 if (cupsd)
745 {
746 /*
747 * Read the file, keeping track of what settings are enabled...
748 */
749
750 int remote_access = 0, /* Remote access allowed? */
751 remote_admin = 0, /* Remote administration allowed? */
752 browsing = 1, /* Browsing enabled? */
753 browse_allow = 1, /* Browse address set? */
754 browse_address = 0, /* Browse address set? */
755 cancel_policy = 1, /* Cancel-job policy set? */
756 debug_logging = 0; /* LogLevel debug set? */
757 int linenum = 0, /* Line number in file */
758 in_location = 0, /* In a location section? */
759 in_policy = 0, /* In a policy section? */
760 in_cancel_job = 0, /* In a cancel-job section? */
761 in_admin_location = 0; /* In the /admin location? */
762
763
764 invalidate_cupsd_cache(cg);
765
766 cg->cupsd_update = time(NULL);
767 httpGetHostname(http, cg->cupsd_hostname, sizeof(cg->cupsd_hostname));
768
769 while (cupsFileGetConf(cupsd, line, sizeof(line), &value, &linenum))
770 {
771 if (!value)
772 continue;
773
774 if (!strcasecmp(line, "Port"))
775 {
776 remote_access = 1;
777 }
778 else if (!strcasecmp(line, "Listen"))
779 {
780 char *port; /* Pointer to port number, if any */
781
782
783 if ((port = strrchr(value, ':')) != NULL)
784 *port = '\0';
785
786 if (strcasecmp(value, "localhost") && strcmp(value, "127.0.0.1"))
787 remote_access = 1;
788 }
789 else if (!strcasecmp(line, "Browsing"))
790 {
791 browsing = !strcasecmp(value, "yes") || !strcasecmp(value, "on") ||
792 !strcasecmp(value, "true");
793 }
794 else if (!strcasecmp(line, "BrowseAddress"))
795 {
796 browse_address = 1;
797 }
798 else if (!strcasecmp(line, "BrowseAllow"))
799 {
800 browse_allow = 1;
801 }
802 else if (!strcasecmp(line, "BrowseOrder"))
803 {
804 browse_allow = !strncasecmp(value, "deny,", 5);
805 }
806 else if (!strcasecmp(line, "LogLevel"))
807 {
808 debug_logging = !strncasecmp(value, "debug", 5);
809 }
810 else if (!strcasecmp(line, "<Policy") && !strcasecmp(value, "default"))
811 {
812 in_policy = 1;
813 }
814 else if (!strcasecmp(line, "</Policy>"))
815 {
816 in_policy = 0;
817 }
818 else if (!strcasecmp(line, "<Limit") && in_policy)
819 {
820 /*
821 * See if the policy limit is for the Cancel-Job operation...
822 */
823
824 char *valptr; /* Pointer into value */
825
826
827 while (*value)
828 {
829 for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
830
831 if (*valptr)
832 *valptr++ = '\0';
833
834 if (!strcasecmp(value, "cancel-job") || !strcasecmp(value, "all"))
835 {
836 in_cancel_job = 1;
837 break;
838 }
839
840 for (value = valptr; isspace(*value & 255); value ++);
841 }
842 }
843 else if (!strcasecmp(line, "</Limit>"))
844 {
845 in_cancel_job = 0;
846 }
847 else if (!strcasecmp(line, "Require") && in_cancel_job)
848 {
849 cancel_policy = 0;
850 }
851 else if (!strcasecmp(line, "<Location"))
852 {
853 in_admin_location = !strcasecmp(value, "/admin");
854 in_location = 1;
855 }
856 else if (!strcasecmp(line, "</Location>"))
857 {
858 in_admin_location = 0;
859 in_location = 0;
860 }
861 else if (!strcasecmp(line, "Allow") && in_admin_location &&
862 strcasecmp(value, "localhost") && strcasecmp(value, "127.0.0.1"))
863 {
864 remote_admin = 1;
865 }
866 else if (line[0] != '<' && !in_location && !in_policy)
867 cg->cupsd_num_settings = cupsAddOption(line, value,
868 cg->cupsd_num_settings,
869 &(cg->cupsd_settings));
870 }
871
872 cupsFileClose(cupsd);
873
874 cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_DEBUG_LOGGING,
875 debug_logging ? "1" : "0",
876 cg->cupsd_num_settings,
877 &(cg->cupsd_settings));
878
879 cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ADMIN,
880 (remote_access && remote_admin) ?
881 "1" : "0",
882 cg->cupsd_num_settings,
883 &(cg->cupsd_settings));
884
885 cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS,
886 (browsing && browse_allow) ?
887 "1" : "0",
888 cg->cupsd_num_settings,
889 &(cg->cupsd_settings));
890
891 cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_SHARE_PRINTERS,
892 (remote_access && browsing &&
893 browse_address) ? "1" : "0",
894 cg->cupsd_num_settings,
895 &(cg->cupsd_settings));
896
897 cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY,
898 cancel_policy ? "1" : "0",
899 cg->cupsd_num_settings,
900 &(cg->cupsd_settings));
901 }
902 else if (status != HTTP_NOT_MODIFIED)
903 invalidate_cupsd_cache(cg);
904
905 /*
906 * Remove any temporary files and copy the settings array...
907 */
908
909 if (remote)
910 unlink(cupsdconf);
911
912 for (i = cg->cupsd_num_settings, setting = cg->cupsd_settings;
913 i > 0;
914 i --, setting ++)
915 *num_settings = cupsAddOption(setting->name, setting->value,
916 *num_settings, settings);
917
918 return (cg->cupsd_num_settings > 0);
919 }
920
921
922 /*
923 * '_cupsAdminSetServerSettings()' - Set settings on the server.
924 *
925 * @since CUPS 1.2@
926 */
927
928 int /* O - 1 on success, 0 on failure */
929 _cupsAdminSetServerSettings(
930 http_t *http, /* I - Connection to server */
931 int num_settings, /* I - Number of settings */
932 cups_option_t *settings) /* I - Settings */
933 {
934 int i; /* Looping var */
935 http_status_t status; /* GET/PUT status */
936 cups_file_t *cupsd; /* cupsd.conf file */
937 char cupsdconf[1024]; /* cupsd.conf filename */
938 int remote; /* Remote cupsd.conf file? */
939 char tempfile[1024]; /* Temporary new cupsd.conf */
940 cups_file_t *temp; /* Temporary file */
941 char line[1024], /* Line from cupsd.conf file */
942 *value; /* Value on line */
943 int linenum, /* Line number in file */
944 in_location, /* In a location section? */
945 in_policy, /* In a policy section? */
946 in_default_policy, /* In the default policy section? */
947 in_cancel_job, /* In a cancel-job section? */
948 in_admin_location, /* In the /admin location? */
949 in_conf_location, /* In the /admin/conf location? */
950 in_root_location; /* In the / location? */
951 const char *val; /* Setting value */
952 int remote_printers, /* Show remote printers */
953 share_printers, /* Share local printers */
954 remote_admin, /* Remote administration allowed? */
955 user_cancel_any, /* Cancel-job policy set? */
956 debug_logging; /* LogLevel debug set? */
957 int wrote_port_listen, /* Wrote the port/listen lines? */
958 wrote_browsing, /* Wrote the browsing lines? */
959 wrote_policy, /* Wrote the policy? */
960 wrote_loglevel, /* Wrote the LogLevel line? */
961 wrote_admin_location, /* Wrote the /admin location? */
962 wrote_conf_location, /* Wrote the /admin/conf location? */
963 wrote_root_location; /* Wrote the / location? */
964 int indent; /* Indentation */
965 int cupsd_num_settings; /* New number of settings */
966 cups_option_t *cupsd_settings, /* New settings */
967 *setting; /* Current setting */
968 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
969
970
971 /*
972 * Range check input...
973 */
974
975 if (!http || !num_settings || !settings)
976 {
977 _cupsSetError(IPP_INTERNAL_ERROR, NULL);
978
979 return (0);
980 }
981
982 /*
983 * Get the cupsd.conf file...
984 */
985
986 if ((status = get_cupsd_conf(http, cg, 0, cupsdconf, sizeof(cupsdconf),
987 &remote)) == HTTP_OK)
988 {
989 if ((cupsd = cupsFileOpen(cupsdconf, "r")) == NULL)
990 {
991 _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno));
992 return (0);
993 }
994 }
995 else
996 return (0);
997
998 /*
999 * Get basic settings...
1000 */
1001
1002 if ((val = cupsGetOption(CUPS_SERVER_DEBUG_LOGGING, num_settings,
1003 settings)) != NULL)
1004 debug_logging = atoi(val);
1005 else
1006 debug_logging = 0;
1007
1008 if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ADMIN, num_settings,
1009 settings)) != NULL)
1010 remote_admin = atoi(val);
1011 else
1012 remote_admin = 0;
1013
1014 if ((val = cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS, num_settings,
1015 settings)) != NULL)
1016 remote_printers = atoi(val);
1017 else
1018 remote_printers = 1;
1019
1020 if ((val = cupsGetOption(CUPS_SERVER_SHARE_PRINTERS, num_settings,
1021 settings)) != NULL)
1022 share_printers = atoi(val);
1023 else
1024 share_printers = 0;
1025
1026 if ((val = cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY, num_settings,
1027 settings)) != NULL)
1028 user_cancel_any = atoi(val);
1029 else
1030 user_cancel_any = 0;
1031
1032 /*
1033 * Create a temporary file for the new cupsd.conf file...
1034 */
1035
1036 if ((temp = cupsTempFile2(tempfile, sizeof(tempfile))) == NULL)
1037 {
1038 cupsFileClose(cupsd);
1039
1040 if (remote)
1041 unlink(cupsdconf);
1042
1043 _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno));
1044 return (0);
1045 }
1046
1047 /*
1048 * Copy the old file to the new, making changes along the way...
1049 */
1050
1051 cupsd_num_settings = 0;
1052 in_admin_location = 0;
1053 in_cancel_job = 0;
1054 in_conf_location = 0;
1055 in_default_policy = 0;
1056 in_location = 0;
1057 in_policy = 0;
1058 in_root_location = 0;
1059 linenum = 0;
1060 wrote_admin_location = 0;
1061 wrote_browsing = 0;
1062 wrote_conf_location = 0;
1063 wrote_loglevel = 0;
1064 wrote_policy = 0;
1065 wrote_port_listen = 0;
1066 wrote_root_location = 0;
1067 indent = 0;
1068
1069 while (cupsFileGetConf(cupsd, line, sizeof(line), &value, &linenum))
1070 {
1071 if (!strcasecmp(line, "Port") || !strcasecmp(line, "Listen"))
1072 {
1073 if (!wrote_port_listen)
1074 {
1075 wrote_port_listen = 1;
1076
1077 if (share_printers || remote_admin)
1078 {
1079 cupsFilePuts(temp, "# Allow remote access\n");
1080 cupsFilePrintf(temp, "Port %d\n", ippPort());
1081 }
1082 else
1083 {
1084 cupsFilePuts(temp, "# Only listen for connections from the local "
1085 "machine.\n");
1086 cupsFilePrintf(temp, "Listen 127.0.0.1:%d\n", ippPort());
1087 }
1088
1089 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1090 if (!access(CUPS_DEFAULT_DOMAINSOCKET, 0))
1091 cupsFilePuts(temp, "Listen " CUPS_DEFAULT_DOMAINSOCKET "\n");
1092 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1093 }
1094 }
1095 else if (!strcasecmp(line, "Browsing") ||
1096 !strcasecmp(line, "BrowseAddress") ||
1097 !strcasecmp(line, "BrowseAllow") ||
1098 !strcasecmp(line, "BrowseDeny") ||
1099 !strcasecmp(line, "BrowseOrder"))
1100 {
1101 if (!wrote_browsing)
1102 {
1103 wrote_browsing = 1;
1104
1105 if (remote_printers || share_printers)
1106 {
1107 if (remote_printers && share_printers)
1108 cupsFilePuts(temp,
1109 "# Enable printer sharing and shared printers.\n");
1110 else if (remote_printers)
1111 cupsFilePuts(temp,
1112 "# Show shared printers on the local network.\n");
1113 else
1114 cupsFilePuts(temp,
1115 "# Share local printers on the local network.\n");
1116
1117 cupsFilePuts(temp, "Browsing On\n");
1118 cupsFilePuts(temp, "BrowseOrder allow,deny\n");
1119
1120 if (remote_printers)
1121 cupsFilePuts(temp, "BrowseAllow @LOCAL\n");
1122
1123 if (share_printers)
1124 cupsFilePuts(temp, "BrowseAddress @LOCAL\n");
1125 }
1126 else
1127 {
1128 cupsFilePuts(temp,
1129 "# Disable printer sharing and shared printers.\n");
1130 cupsFilePuts(temp, "Browsing Off\n");
1131 }
1132 }
1133 }
1134 else if (!strcasecmp(line, "LogLevel"))
1135 {
1136 wrote_loglevel = 1;
1137
1138 if (debug_logging)
1139 {
1140 cupsFilePuts(temp,
1141 "# Show troubleshooting information in error_log.\n");
1142 cupsFilePuts(temp, "LogLevel debug\n");
1143 }
1144 else
1145 {
1146 cupsFilePuts(temp, "# Show general information in error_log.\n");
1147 cupsFilePuts(temp, "LogLevel info\n");
1148 }
1149 }
1150 else if (!strcasecmp(line, "<Policy"))
1151 {
1152 in_default_policy = !strcasecmp(value, "default");
1153 in_policy = 1;
1154
1155 cupsFilePrintf(temp, "%s %s>\n", line, value);
1156 indent += 2;
1157 }
1158 else if (!strcasecmp(line, "</Policy>"))
1159 {
1160 indent -= 2;
1161 if (!wrote_policy && in_default_policy)
1162 {
1163 wrote_policy = 1;
1164
1165 if (!user_cancel_any)
1166 cupsFilePuts(temp, " # Only the owner or an administrator can "
1167 "cancel a job...\n"
1168 " <Limit Cancel-Job>\n"
1169 " Order deny,allow\n"
1170 " Allow @SYSTEM\n"
1171 " Allow @OWNER\n"
1172 " </Limit>\n");
1173 }
1174
1175 in_policy = 0;
1176 in_default_policy = 0;
1177
1178 cupsFilePuts(temp, "</Policy>\n");
1179 }
1180 else if (!strcasecmp(line, "<Location"))
1181 {
1182 in_location = 1;
1183 indent += 2;
1184 if (!strcmp(value, "/admin"))
1185 in_admin_location = 1;
1186 if (!strcmp(value, "/admin/conf"))
1187 in_conf_location = 1;
1188 else if (!strcmp(value, "/"))
1189 in_root_location = 1;
1190
1191 cupsFilePrintf(temp, "%s %s>\n", line, value);
1192 }
1193 else if (!strcasecmp(line, "</Location>"))
1194 {
1195 in_location = 0;
1196 indent -= 2;
1197 if (in_admin_location)
1198 {
1199 wrote_admin_location = 1;
1200
1201 if (remote_admin)
1202 cupsFilePuts(temp, " # Allow remote administration...\n");
1203 else
1204 cupsFilePuts(temp, " # Restrict access to the admin pages...\n");
1205
1206 cupsFilePuts(temp, " Order allow,deny\n");
1207
1208 if (remote_admin)
1209 cupsFilePuts(temp, " Allow @LOCAL\n");
1210 else
1211 cupsFilePuts(temp, " Allow localhost\n");
1212 }
1213 else if (in_conf_location)
1214 {
1215 wrote_conf_location = 1;
1216
1217 if (remote_admin)
1218 cupsFilePuts(temp, " # Allow remote access to the configuration "
1219 "files...\n");
1220 else
1221 cupsFilePuts(temp, " # Restrict access to the configuration "
1222 "files...\n");
1223
1224 cupsFilePuts(temp, " Order allow,deny\n");
1225
1226 if (remote_admin)
1227 cupsFilePuts(temp, " Allow @LOCAL\n");
1228 else
1229 cupsFilePuts(temp, " Allow localhost\n");
1230 }
1231 else if (in_root_location)
1232 {
1233 wrote_root_location = 1;
1234
1235 if (remote_admin && share_printers)
1236 cupsFilePuts(temp, " # Allow shared printing and remote "
1237 "administration...\n");
1238 else if (remote_admin)
1239 cupsFilePuts(temp, " # Allow remote administration...\n");
1240 else if (share_printers)
1241 cupsFilePuts(temp, " # Allow shared printing...\n");
1242 else
1243 cupsFilePuts(temp, " # Restrict access to the server...\n");
1244
1245 cupsFilePuts(temp, " Order allow,deny\n");
1246
1247 if (remote_admin || share_printers)
1248 cupsFilePuts(temp, " Allow @LOCAL\n");
1249 else
1250 cupsFilePuts(temp, " Allow localhost\n");
1251 }
1252
1253 in_admin_location = 0;
1254 in_conf_location = 0;
1255 in_root_location = 0;
1256
1257 cupsFilePuts(temp, "</Location>\n");
1258 }
1259 else if (!strcasecmp(line, "<Limit") && in_default_policy)
1260 {
1261 /*
1262 * See if the policy limit is for the Cancel-Job operation...
1263 */
1264
1265 char *valptr; /* Pointer into value */
1266
1267
1268 indent += 2;
1269
1270 if (!strcasecmp(value, "cancel-job"))
1271 {
1272 /*
1273 * Don't write anything for this limit section...
1274 */
1275
1276 in_cancel_job = 2;
1277 }
1278 else
1279 {
1280 cupsFilePrintf(temp, " %s", line);
1281
1282 while (*value)
1283 {
1284 for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
1285
1286 if (*valptr)
1287 *valptr++ = '\0';
1288
1289 if (!strcasecmp(value, "cancel-job"))
1290 {
1291 /*
1292 * Write everything except for this definition...
1293 */
1294
1295 in_cancel_job = 1;
1296 }
1297 else
1298 cupsFilePrintf(temp, " %s", value);
1299
1300 for (value = valptr; isspace(*value & 255); value ++);
1301 }
1302
1303 cupsFilePuts(temp, ">\n");
1304 }
1305 }
1306 else if (!strcasecmp(line, "</Limit>") && in_cancel_job)
1307 {
1308 indent -= 2;
1309
1310 if (in_cancel_job == 1)
1311 cupsFilePuts(temp, " </Limit>\n");
1312
1313 wrote_policy = 1;
1314
1315 if (!user_cancel_any)
1316 cupsFilePuts(temp, " # Only the owner or an administrator can cancel "
1317 "a job...\n"
1318 " <Limit Cancel-Job>\n"
1319 " Order deny,allow\n"
1320 " Require user @OWNER @SYSTEM\n"
1321 " </Limit>\n");
1322
1323 in_cancel_job = 0;
1324 }
1325 else if ((in_admin_location || in_conf_location || in_root_location) &&
1326 (!strcasecmp(line, "Allow") || !strcasecmp(line, "Deny") ||
1327 !strcasecmp(line, "Order")))
1328 continue;
1329 else if (in_cancel_job == 2)
1330 continue;
1331 else if (!strcasecmp(line, "<Limit") && value)
1332 cupsFilePrintf(temp, " %s %s>\n", line, value);
1333 else if (line[0] == '<')
1334 {
1335 if (value)
1336 {
1337 cupsFilePrintf(temp, "%*s%s %s>\n", indent, "", line, value);
1338 indent += 2;
1339 }
1340 else
1341 {
1342 if (line[1] == '/')
1343 indent -= 2;
1344
1345 cupsFilePrintf(temp, "%*s%s\n", indent, "", line);
1346 }
1347 }
1348 else if (!in_policy && !in_location &&
1349 (val = cupsGetOption(line, num_settings, settings)) != NULL &&
1350 !cupsGetOption(line, cupsd_num_settings, cupsd_settings))
1351 {
1352 /*
1353 * Add this directive to the list of directives we have written...
1354 */
1355
1356 cupsd_num_settings = cupsAddOption(line, value, cupsd_num_settings,
1357 &cupsd_settings);
1358
1359 /*
1360 * Write the new value in its place, without indentation since we
1361 * only support setting root directives, not in sections...
1362 */
1363
1364 cupsFilePrintf(temp, "%s %s\n", line, value);
1365 }
1366 else if (value)
1367 {
1368 if (!in_policy && !in_location)
1369 {
1370 /*
1371 * Record the non-policy, non-location directives that we find
1372 * in the server settings, since we cache this info and record it
1373 * in _cupsAdminGetServerSettings()...
1374 */
1375
1376 cupsd_num_settings = cupsAddOption(line, value, cupsd_num_settings,
1377 &cupsd_settings);
1378 }
1379
1380 cupsFilePrintf(temp, "%*s%s %s\n", indent, "", line, value);
1381 }
1382 else
1383 cupsFilePrintf(temp, "%*s%s\n", indent, "", line);
1384 }
1385
1386 /*
1387 * Write any missing info...
1388 */
1389
1390 if (!wrote_browsing)
1391 {
1392 if (remote_printers || share_printers)
1393 {
1394 if (remote_printers && share_printers)
1395 cupsFilePuts(temp, "# Enable printer sharing and shared printers.\n");
1396 else if (remote_printers)
1397 cupsFilePuts(temp, "# Show shared printers on the local network.\n");
1398 else
1399 cupsFilePuts(temp, "# Share local printers on the local network.\n");
1400
1401 cupsFilePuts(temp, "Browsing On\n");
1402 cupsFilePuts(temp, "BrowseOrder allow,deny\n");
1403
1404 if (remote_printers)
1405 cupsFilePuts(temp, "BrowseAllow @LOCAL\n");
1406
1407 if (share_printers)
1408 cupsFilePuts(temp, "BrowseAddress @LOCAL\n");
1409 }
1410 else
1411 {
1412 cupsFilePuts(temp, "# Disable printer sharing and shared printers.\n");
1413 cupsFilePuts(temp, "Browsing Off\n");
1414 }
1415 }
1416
1417 if (!wrote_loglevel)
1418 {
1419 if (debug_logging)
1420 {
1421 cupsFilePuts(temp, "# Show troubleshooting information in error_log.\n");
1422 cupsFilePuts(temp, "LogLevel debug\n");
1423 }
1424 else
1425 {
1426 cupsFilePuts(temp, "# Show general information in error_log.\n");
1427 cupsFilePuts(temp, "LogLevel info\n");
1428 }
1429 }
1430
1431 if (!wrote_port_listen)
1432 {
1433 if (share_printers || remote_admin)
1434 {
1435 cupsFilePuts(temp, "# Allow remote access\n");
1436 cupsFilePrintf(temp, "Port %d\n", ippPort());
1437 }
1438 else
1439 {
1440 cupsFilePuts(temp,
1441 "# Only listen for connections from the local machine.\n");
1442 cupsFilePrintf(temp, "Listen 127.0.0.1:%d\n", ippPort());
1443 }
1444
1445 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1446 if (!access(CUPS_DEFAULT_DOMAINSOCKET, 0))
1447 cupsFilePuts(temp, "Listen " CUPS_DEFAULT_DOMAINSOCKET "\n");
1448 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1449 }
1450
1451 if (!wrote_root_location)
1452 {
1453 if (remote_admin && share_printers)
1454 cupsFilePuts(temp,
1455 "# Allow shared printing and remote administration...\n");
1456 else if (remote_admin)
1457 cupsFilePuts(temp, "# Allow remote administration...\n");
1458 else if (share_printers)
1459 cupsFilePuts(temp, "# Allow shared printing...\n");
1460 else
1461 cupsFilePuts(temp, "# Restrict access to the server...\n");
1462
1463 cupsFilePuts(temp, "<Location />\n"
1464 " Order allow,deny\n");
1465
1466 if (remote_admin || share_printers)
1467 cupsFilePuts(temp, " Allow @LOCAL\n");
1468 else
1469 cupsFilePuts(temp, " Allow localhost\n");
1470
1471 cupsFilePuts(temp, "</Location>\n");
1472 }
1473
1474 if (!wrote_admin_location)
1475 {
1476 if (remote_admin)
1477 cupsFilePuts(temp, "# Allow remote administration...\n");
1478 else
1479 cupsFilePuts(temp, "# Restrict access to the admin pages...\n");
1480
1481 cupsFilePuts(temp, "<Location /admin>\n"
1482 " Order allow,deny\n");
1483
1484 if (remote_admin)
1485 cupsFilePuts(temp, " Allow @LOCAL\n");
1486 else
1487 cupsFilePuts(temp, " Allow localhost\n");
1488
1489 cupsFilePuts(temp, "</Location>\n");
1490 }
1491
1492 if (!wrote_conf_location)
1493 {
1494 if (remote_admin)
1495 cupsFilePuts(temp,
1496 "# Allow remote access to the configuration files...\n");
1497 else
1498 cupsFilePuts(temp, "# Restrict access to the configuration files...\n");
1499
1500 cupsFilePuts(temp, "<Location /admin/conf>\n"
1501 " AuthType Basic\n"
1502 " Require user @SYSTEM\n"
1503 " Order allow,deny\n");
1504
1505 if (remote_admin)
1506 cupsFilePuts(temp, " Allow @LOCAL\n");
1507 else
1508 cupsFilePuts(temp, " Allow localhost\n");
1509
1510 cupsFilePuts(temp, "</Location>\n");
1511 }
1512
1513 if (!wrote_policy)
1514 {
1515 cupsFilePuts(temp, "<Policy default>\n"
1516 " # Job-related operations must be done by the owner "
1517 "or an adminstrator...\n"
1518 " <Limit Send-Document Send-URI Hold-Job Release-Job "
1519 "Restart-Job Purge-Jobs Set-Job-Attributes "
1520 "Create-Job-Subscription Renew-Subscription "
1521 "Cancel-Subscription Get-Notifications Reprocess-Job "
1522 "Cancel-Current-Job Suspend-Current-Job Resume-Job "
1523 "CUPS-Move-Job>\n"
1524 " Require user @OWNER @SYSTEM\n"
1525 " Order deny,allow\n"
1526 " </Limit>\n"
1527 " # All administration operations require an "
1528 "adminstrator to authenticate...\n"
1529 " <Limit Pause-Printer Resume-Printer "
1530 "Set-Printer-Attributes Enable-Printer "
1531 "Disable-Printer Pause-Printer-After-Current-Job "
1532 "Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer "
1533 "Activate-Printer Restart-Printer Shutdown-Printer "
1534 "Startup-Printer Promote-Job Schedule-Job-After "
1535 "CUPS-Add-Printer CUPS-Delete-Printer "
1536 "CUPS-Add-Class CUPS-Delete-Class "
1537 "CUPS-Accept-Jobs CUPS-Reject-Jobs "
1538 "CUPS-Set-Default CUPS-Add-Device CUPS-Delete-Device>\n"
1539 " AuthType Basic\n"
1540 " Require user @SYSTEM\n"
1541 " Order deny,allow\n"
1542 "</Limit>\n");
1543
1544 if (!user_cancel_any)
1545 cupsFilePuts(temp, " # Only the owner or an administrator can cancel "
1546 "a job...\n"
1547 " <Limit Cancel-Job>\n"
1548 " Require user @OWNER @SYSTEM\n"
1549 " Order deny,allow\n"
1550 " </Limit>\n");
1551
1552 cupsFilePuts(temp, " <Limit All>\n"
1553 " Order deny,allow\n"
1554 " </Limit>\n"
1555 "</Policy>\n");
1556 }
1557
1558 for (i = num_settings, setting = settings; i > 0; i --, setting ++)
1559 if (setting->name[0] != '_' &&
1560 !cupsGetOption(setting->name, cupsd_num_settings, cupsd_settings))
1561 {
1562 /*
1563 * Add this directive to the list of directives we have written...
1564 */
1565
1566 cupsd_num_settings = cupsAddOption(setting->name, setting->value,
1567 cupsd_num_settings, &cupsd_settings);
1568
1569 /*
1570 * Write the new value in its place, without indentation since we
1571 * only support setting root directives, not in sections...
1572 */
1573
1574 cupsFilePrintf(temp, "%s %s\n", setting->name, setting->value);
1575 }
1576
1577 cupsFileClose(cupsd);
1578 cupsFileClose(temp);
1579
1580 /*
1581 * Upload the configuration file to the server...
1582 */
1583
1584 status = cupsPutFile(http, "/admin/conf/cupsd.conf", tempfile);
1585
1586 if (status == HTTP_CREATED)
1587 {
1588 /*
1589 * Updated OK, add the basic settings...
1590 */
1591
1592 cupsd_num_settings = cupsAddOption(CUPS_SERVER_DEBUG_LOGGING,
1593 debug_logging ? "1" : "0",
1594 cupsd_num_settings, &cupsd_settings);
1595 cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ADMIN,
1596 remote_admin ? "1" : "0",
1597 cupsd_num_settings, &cupsd_settings);
1598 cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS,
1599 remote_printers ? "1" : "0",
1600 cupsd_num_settings, &cupsd_settings);
1601 cupsd_num_settings = cupsAddOption(CUPS_SERVER_SHARE_PRINTERS,
1602 share_printers ? "1" : "0",
1603 cupsd_num_settings, &cupsd_settings);
1604 cupsd_num_settings = cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY,
1605 user_cancel_any ? "1" : "0",
1606 cupsd_num_settings, &cupsd_settings);
1607
1608 /*
1609 * Save the new values...
1610 */
1611
1612 invalidate_cupsd_cache(cg);
1613
1614 cg->cupsd_num_settings = cupsd_num_settings;
1615 cg->cupsd_settings = cupsd_settings;
1616 cg->cupsd_update = time(NULL);
1617
1618 httpGetHostname(http, cg->cupsd_hostname, sizeof(cg->cupsd_hostname));
1619 }
1620 else
1621 cupsFreeOptions(cupsd_num_settings, cupsd_settings);
1622
1623 /*
1624 * Remote our temp files and return...
1625 */
1626
1627 if (remote)
1628 unlink(cupsdconf);
1629
1630 unlink(tempfile);
1631
1632 return (status == HTTP_CREATED);
1633 }
1634
1635
1636 /*
1637 * 'do_samba_command()' - Do a SAMBA command.
1638 */
1639
1640 static int /* O - Status of command */
1641 do_samba_command(const char *command, /* I - Command to run */
1642 const char *address, /* I - Address for command */
1643 const char *subcmd, /* I - Sub-command */
1644 const char *authfile, /* I - Samba authentication file */
1645 FILE *logfile) /* I - Optional log file */
1646 {
1647 int status; /* Status of command */
1648 int pid; /* Process ID of child */
1649
1650
1651 if (logfile)
1652 _cupsLangPrintf(logfile,
1653 _("Running command: %s %s -N -A %s -c \'%s\'\n"),
1654 command, address, authfile, subcmd);
1655
1656 if ((pid = fork()) == 0)
1657 {
1658 /*
1659 * Child goes here, redirect stdin/out/err and execute the command...
1660 */
1661
1662 close(0);
1663 open("/dev/null", O_RDONLY);
1664
1665 close(1);
1666
1667 if (logfile)
1668 dup(fileno(logfile));
1669 else
1670 open("/dev/null", O_WRONLY);
1671
1672 close(2);
1673 dup(1);
1674
1675 execlp(command, command, address, "-N", "-A", authfile, "-c", subcmd,
1676 (char *)0);
1677 exit(errno);
1678 }
1679 else if (pid < 0)
1680 {
1681 status = -1;
1682
1683 _cupsLangPrintf(stderr, _("cupsaddsmb: Unable to run \"%s\": %s\n"),
1684 command, strerror(errno));
1685 }
1686 else
1687 {
1688 /*
1689 * Wait for the process to complete...
1690 */
1691
1692 while (wait(&status) != pid);
1693 }
1694
1695 if (logfile)
1696 _cupsLangPuts(logfile, "\n");
1697
1698 DEBUG_printf(("status=%d\n", status));
1699
1700 if (WIFEXITED(status))
1701 return (WEXITSTATUS(status));
1702 else
1703 return (-WTERMSIG(status));
1704 }
1705
1706
1707 /*
1708 * 'get_cupsd_conf()' - Get the current cupsd.conf file.
1709 */
1710
1711 static http_status_t /* O - Status of request */
1712 get_cupsd_conf(
1713 http_t *http, /* I - Connection to server */
1714 _cups_globals_t *cg, /* I - Global data */
1715 time_t last_update, /* I - Last update time for file */
1716 char *name, /* I - Filename buffer */
1717 int namesize, /* I - Size of filename buffer */
1718 int *remote) /* O - Remote file? */
1719 {
1720 int fd; /* Temporary file descriptor */
1721 struct stat info; /* cupsd.conf file information */
1722 http_status_t status; /* Status of getting cupsd.conf */
1723 char host[HTTP_MAX_HOST]; /* Hostname for connection */
1724
1725
1726 /*
1727 * See if we already have the data we need...
1728 */
1729
1730 httpGetHostname(http, host, sizeof(host));
1731
1732 if (strcasecmp(cg->cupsd_hostname, host))
1733 invalidate_cupsd_cache(cg);
1734
1735 snprintf(name, namesize, "%s/cupsd.conf", cg->cups_serverroot);
1736 *remote = 0;
1737
1738 if (!strcasecmp(host, "localhost") && !access(name, R_OK))
1739 {
1740 /*
1741 * Read the local file rather than using HTTP...
1742 */
1743
1744 if (stat(name, &info))
1745 {
1746 char message[1024]; /* Message string */
1747
1748
1749 snprintf(message, sizeof(message),
1750 _cupsLangString(cupsLangDefault(), _("stat of %s failed: %s")),
1751 name, strerror(errno));
1752 _cupsSetError(IPP_INTERNAL_ERROR, message);
1753
1754 *name = '\0';
1755
1756 return (HTTP_SERVER_ERROR);
1757 }
1758 else if (last_update && info.st_mtime <= last_update)
1759 status = HTTP_NOT_MODIFIED;
1760 else
1761 status = HTTP_OK;
1762 }
1763 else
1764 {
1765 /*
1766 * Read cupsd.conf via a HTTP GET request...
1767 */
1768
1769 if ((fd = cupsTempFd(name, sizeof(name))) < 0)
1770 {
1771 *name = '\0';
1772
1773 _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno));
1774
1775 invalidate_cupsd_cache(cg);
1776
1777 return (HTTP_SERVER_ERROR);
1778 }
1779
1780 *remote = 1;
1781
1782 httpClearFields(http);
1783
1784 if (last_update)
1785 httpSetField(http, HTTP_FIELD_IF_MODIFIED_SINCE,
1786 httpGetDateString(last_update));
1787
1788 status = cupsGetFd(http, "/admin/conf/cupsd.conf", fd);
1789
1790 close(fd);
1791
1792 if (status != HTTP_OK)
1793 {
1794 unlink(name);
1795 *name = '\0';
1796 }
1797 }
1798
1799 return (status);
1800 }
1801
1802
1803 /*
1804 * 'invalidate_cupsd_cache()' - Invalidate the cached cupsd.conf settings.
1805 */
1806
1807 static void
1808 invalidate_cupsd_cache(
1809 _cups_globals_t *cg) /* I - Global data */
1810 {
1811 cupsFreeOptions(cg->cupsd_num_settings, cg->cupsd_settings);
1812
1813 cg->cupsd_hostname[0] = '\0';
1814 cg->cupsd_update = 0;
1815 cg->cupsd_num_settings = 0;
1816 cg->cupsd_settings = NULL;
1817 }
1818
1819
1820 /*
1821 * 'write_option()' - Write a CUPS option to a PPD file.
1822 */
1823
1824 static void
1825 write_option(cups_file_t *dstfp, /* I - PPD file */
1826 int order, /* I - Order dependency */
1827 const char *name, /* I - Option name */
1828 const char *text, /* I - Option text */
1829 const char *attrname, /* I - Attribute name */
1830 ipp_attribute_t *suppattr, /* I - IPP -supported attribute */
1831 ipp_attribute_t *defattr, /* I - IPP -default attribute */
1832 int defval, /* I - Default value number */
1833 int valcount) /* I - Number of values */
1834 {
1835 int i; /* Looping var */
1836
1837
1838 cupsFilePrintf(dstfp, "*JCLOpenUI *%s/%s: PickOne\n"
1839 "*OrderDependency: %d JCLSetup *%s\n",
1840 name, text, order, name);
1841
1842 if (defattr->value_tag == IPP_TAG_INTEGER)
1843 {
1844 /*
1845 * Do numeric options with a range or list...
1846 */
1847
1848 cupsFilePrintf(dstfp, "*Default%s: %d\n", name,
1849 defattr->values[defval].integer);
1850
1851 if (suppattr->value_tag == IPP_TAG_RANGE)
1852 {
1853 /*
1854 * List each number in the range...
1855 */
1856
1857 for (i = suppattr->values[0].range.lower;
1858 i <= suppattr->values[0].range.upper;
1859 i ++)
1860 {
1861 cupsFilePrintf(dstfp, "*%s %d: \"", name, i);
1862
1863 if (valcount == 1)
1864 cupsFilePrintf(dstfp, "%%cupsJobTicket: %s=%d\n\"\n*End\n",
1865 attrname, i);
1866 else if (defval == 0)
1867 cupsFilePrintf(dstfp, "%%cupsJobTicket: %s=%d\"\n", attrname, i);
1868 else if (defval < (valcount - 1))
1869 cupsFilePrintf(dstfp, ",%d\"\n", i);
1870 else
1871 cupsFilePrintf(dstfp, ",%d\n\"\n*End\n", i);
1872 }
1873 }
1874 else
1875 {
1876 /*
1877 * List explicit numbers...
1878 */
1879
1880 for (i = 0; i < suppattr->num_values; i ++)
1881 {
1882 cupsFilePrintf(dstfp, "*%s %d: \"", name, suppattr->values[i].integer);
1883
1884 if (valcount == 1)
1885 cupsFilePrintf(dstfp, "%%cupsJobTicket: %s=%d\n\"\n*End\n", attrname,
1886 suppattr->values[i].integer);
1887 else if (defval == 0)
1888 cupsFilePrintf(dstfp, "%%cupsJobTicket: %s=%d\"\n", attrname,
1889 suppattr->values[i].integer);
1890 else if (defval < (valcount - 1))
1891 cupsFilePrintf(dstfp, ",%d\"\n", suppattr->values[i].integer);
1892 else
1893 cupsFilePrintf(dstfp, ",%d\n\"\n*End\n", suppattr->values[i].integer);
1894 }
1895 }
1896 }
1897 else
1898 {
1899 /*
1900 * Do text options with a list...
1901 */
1902
1903 cupsFilePrintf(dstfp, "*Default%s: %s\n", name,
1904 defattr->values[defval].string.text);
1905
1906 for (i = 0; i < suppattr->num_values; i ++)
1907 {
1908 cupsFilePrintf(dstfp, "*%s %s: \"", name,
1909 suppattr->values[i].string.text);
1910
1911 if (valcount == 1)
1912 cupsFilePrintf(dstfp, "%%cupsJobTicket: %s=%s\n\"\n*End\n", attrname,
1913 suppattr->values[i].string.text);
1914 else if (defval == 0)
1915 cupsFilePrintf(dstfp, "%%cupsJobTicket: %s=%s\"\n", attrname,
1916 suppattr->values[i].string.text);
1917 else if (defval < (valcount - 1))
1918 cupsFilePrintf(dstfp, ",%s\"\n", suppattr->values[i].string.text);
1919 else
1920 cupsFilePrintf(dstfp, ",%s\n\"\n*End\n",
1921 suppattr->values[i].string.text);
1922 }
1923 }
1924
1925 cupsFilePrintf(dstfp, "*JCLCloseUI: *%s\n\n", name);
1926 }
1927
1928
1929 /*
1930 * End of "$Id$".
1931 */