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