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