2 * "$Id: adminutil.c 6930 2007-09-08 00:28:06Z mike $"
4 * Administration utility API definitions for the Common UNIX Printing
7 * Copyright 2007-2008 by Apple Inc.
8 * Copyright 2001-2007 by Easy Software Products.
10 * These coded instructions, statements, and computer programs are the
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/".
16 * This file is subject to the Apple OS-Developed Software exception.
20 * cupsAdminCreateWindowsPPD() - Create the Windows PPD file for a printer.
21 * cupsAdminExportSamba() - Export a printer to Samba.
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).
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.
33 * Include necessary headers...
36 #include "adminutil.h"
45 # include <sys/wait.h>
53 extern int _cupsAdminGetServerSettings(http_t
*http
,
55 cups_option_t
**settings
);
56 extern int _cupsAdminSetServerSettings(http_t
*http
,
58 cups_option_t
*settings
);
59 static int do_samba_command(const char *command
,
61 const char *subcommand
,
64 static 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
);
67 static void invalidate_cupsd_cache(_cups_globals_t
*cg
);
68 static void write_option(cups_file_t
*dstfp
, int order
,
69 const char *name
, const char *text
,
71 ipp_attribute_t
*suppattr
,
72 ipp_attribute_t
*defattr
, int defval
,
77 * 'cupsAdminCreateWindowsPPD()' - Create the Windows PPD file for a printer.
80 char * /* O - PPD file or NULL */
81 cupsAdminCreateWindowsPPD(
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 */
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? */
102 jclorder
, /* Next JCL order dependency */
103 linenum
; /* Current line number */
104 time_t curtime
; /* Current time */
105 struct tm
*curdate
; /* Current date */
106 static const char * const pattrs
[] = /* Printer attributes we want */
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"
118 * Range check the input...
124 if (!http
|| !dest
|| !buffer
|| bufsize
< 2)
128 * Get the PPD file...
131 if ((src
= cupsGetPPD2(http
, dest
)) == NULL
)
135 * Get the supported banner pages, etc. for the printer...
138 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
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
);
145 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
146 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
150 * Do the request and get back a response...
153 response
= cupsDoRequest(http
, request
, "/");
154 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
161 * Open the original PPD file...
164 if ((srcfp
= cupsFileOpen(src
, "rb")) == NULL
)
168 * Create a temporary output file using the destination buffer...
171 if ((dstfp
= cupsTempFile2(buffer
, bufsize
)) == NULL
)
173 cupsFileClose(srcfp
);
181 * Write a new header explaining that this isn't the original PPD...
184 cupsFilePuts(dstfp
, "*PPD-Adobe: \"4.3\"\n");
186 curtime
= time(NULL
);
187 curdate
= gmtime(&curtime
);
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
);
195 * Read the existing PPD file, converting all PJL commands to CUPS
196 * job ticket comments...
202 language
= cupsLangDefault();
204 while (cupsFileGets(srcfp
, line
, sizeof(line
)))
208 if (!strncmp(line
, "*PPD-Adobe:", 11))
211 * Already wrote the PPD header...
216 else if (!strncmp(line
, "*JCLBegin:", 10) ||
217 !strncmp(line
, "*JCLToPSInterpreter:", 20) ||
218 !strncmp(line
, "*JCLEnd:", 8) ||
219 !strncmp(line
, "*Protocols:", 11))
222 * Don't use existing JCL keywords; we'll create our own, below...
225 cupsFilePrintf(dstfp
, "*%% Commented out for CUPS Windows Driver...\n"
226 "*%%%s\n", line
+ 1);
229 else if (!strncmp(line
, "*JCLOpenUI", 10))
232 cupsFilePrintf(dstfp
, "%s\n", line
);
234 else if (!strncmp(line
, "*JCLCloseUI", 11))
237 cupsFilePrintf(dstfp
, "%s\n", line
);
239 else if (jcloption
&& !strncmp(line
, "*OrderDependency:", 17))
241 for (ptr
= line
+ 17; *ptr
&& isspace(*ptr
& 255); ptr
++);
243 ptr
= strchr(ptr
, ' ');
247 cupsFilePrintf(dstfp
, "*OrderDependency: %d%s\n", jclorder
, ptr
);
251 cupsFilePrintf(dstfp
, "%s\n", line
);
253 else if (jcloption
&&
254 strncmp(line
, "*End", 4) &&
255 strncmp(line
, "*Default", 8))
257 if ((ptr
= strchr(line
, ':')) == NULL
)
259 snprintf(line
, sizeof(line
),
260 _cupsLangString(language
, _("Missing value on line %d!")),
262 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
264 cupsFileClose(srcfp
);
265 cupsFileClose(dstfp
);
275 if ((ptr
= strchr(ptr
, '\"')) == NULL
)
277 snprintf(line
, sizeof(line
),
278 _cupsLangString(language
,
279 _("Missing double quote on line %d!")),
281 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
283 cupsFileClose(srcfp
);
284 cupsFileClose(dstfp
);
294 if (sscanf(line
, "*%40s%*[ \t]%40[^:/]", option
, choice
) != 2)
296 snprintf(line
, sizeof(line
),
297 _cupsLangString(language
,
298 _("Bad option + choice on line %d!")),
300 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
302 cupsFileClose(srcfp
);
303 cupsFileClose(dstfp
);
313 if (strchr(ptr
+ 1, '\"') == NULL
)
319 while (cupsFileGets(srcfp
, junk
, sizeof(junk
)) != NULL
)
323 if (!strncmp(junk
, "*End", 4))
328 snprintf(ptr
+ 1, sizeof(line
) - (ptr
- line
+ 1),
329 "%%cupsJobTicket: %s=%s\n\"\n*End", option
, choice
);
331 cupsFilePrintf(dstfp
, "*%% Changed for CUPS Windows Driver...\n%s\n",
335 cupsFilePrintf(dstfp
, "%s\n", line
);
338 cupsFileClose(srcfp
);
343 snprintf(line
, sizeof(line
),
344 _cupsLangString(language
, _("Empty PPD file!")),
346 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
348 cupsFileClose(dstfp
);
357 * Now add the CUPS-specific attributes and options...
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");
366 cupsFilePuts(dstfp
, "\n*OpenGroup: CUPS/CUPS Options\n\n");
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
)
372 write_option(dstfp
, jclorder
++, "cupsJobHoldUntil", "Hold Until",
373 "job-hold-until", suppattr
, defattr
, 0, 1);
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
)
379 write_option(dstfp
, jclorder
++, "cupsJobPriority", "Priority",
380 "job-priority", suppattr
, defattr
, 0, 1);
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
)
387 write_option(dstfp
, jclorder
++, "cupsJobSheetsStart", "Start Banner",
388 "job-sheets", suppattr
, defattr
, 0, 2);
389 write_option(dstfp
, jclorder
++, "cupsJobSheetsEnd", "End Banner",
390 "job-sheets", suppattr
, defattr
, 1, 2);
393 cupsFilePuts(dstfp
, "*CloseGroup: CUPS\n");
394 cupsFileClose(dstfp
);
403 * 'cupsAdminExportSamba()' - Export a printer to Samba.
406 int /* O - 1 on success, 0 on failure */
407 cupsAdminExportSamba(
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 */
415 int status
; /* Status of Samba commands */
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();
429 * Range check input...
432 if (!dest
|| !ppd
|| !samba_server
|| !samba_user
|| !samba_password
)
434 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
439 * Create a temporary authentication file for Samba...
442 if ((fp
= cupsTempFile2(authfile
, sizeof(authfile
))) == NULL
)
444 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
448 cupsFilePrintf(fp
, "username = %s\n", samba_user
);
449 cupsFilePrintf(fp
, "password = %s\n", samba_password
);
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:
465 language
= cupsLangDefault();
467 snprintf(file
, sizeof(file
), "%s/drivers/pscript5.dll", cg
->cups_datadir
);
468 if (!access(file
, 0))
473 * Windows 2k driver is installed; do the smbclient commands needed
474 * to copy the Win2k drivers over...
477 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
479 snprintf(subcmd
, sizeof(subcmd
),
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
);
489 if ((status
= do_samba_command("smbclient", address
, subcmd
,
490 authfile
, logfile
)) != 0)
492 snprintf(message
, sizeof(message
),
493 _cupsLangString(language
,
494 _("Unable to copy Windows 2000 printer "
495 "driver files (%d)!")), status
);
497 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
500 _cupsLangPrintf(logfile
, "%s\n", message
);
508 * See if we also have the CUPS driver files; if so, use them!
511 snprintf(file
, sizeof(file
), "%s/drivers/cupsps6.dll", cg
->cups_datadir
);
512 if (!access(file
, 0))
515 * Copy the CUPS driver files over...
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
);
524 if ((status
= do_samba_command("smbclient", address
, subcmd
,
525 authfile
, logfile
)) != 0)
527 snprintf(message
, sizeof(message
),
528 _cupsLangString(language
,
529 _("Unable to copy CUPS printer driver "
530 "files (%d)!")), status
);
532 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
535 _cupsLangPrintf(logfile
, "%s\n", message
);
543 * Do the rpcclient command needed for the CUPS drivers...
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\"",
556 * Don't have the CUPS drivers, so just use the standard Windows
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\"",
567 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
568 authfile
, logfile
)) != 0)
570 snprintf(message
, sizeof(message
),
571 _cupsLangString(language
,
572 _("Unable to install Windows 2000 printer "
573 "driver files (%d)!")), status
);
575 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
578 _cupsLangPrintf(logfile
, "%s\n", message
);
587 * See if we have the Win9x PS driver...
590 snprintf(file
, sizeof(file
), "%s/drivers/ADOBEPS4.DRV", cg
->cups_datadir
);
591 if (!access(file
, 0))
596 * Do the smbclient commands needed for the Adobe Win9x drivers...
599 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
601 snprintf(subcmd
, sizeof(subcmd
),
603 "put %s WIN40/%s.PPD;"
604 "put %s/drivers/ADFONTS.MFM WIN40/ADFONTS.MFM;"
605 "put %s/drivers/ADOBEPS4.DRV WIN40/ADOBEPS4.DRV;"
606 "put %s/drivers/ADOBEPS4.HLP WIN40/ADOBEPS4.HLP;"
607 "put %s/drivers/ICONLIB.DLL WIN40/ICONLIB.DLL;"
608 "put %s/drivers/PSMON.DLL WIN40/PSMON.DLL;",
609 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
610 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
612 if ((status
= do_samba_command("smbclient", address
, subcmd
,
613 authfile
, logfile
)) != 0)
615 snprintf(message
, sizeof(message
),
616 _cupsLangString(language
,
617 _("Unable to copy Windows 9x printer "
618 "driver files (%d)!")), status
);
620 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
623 _cupsLangPrintf(logfile
, "%s\n", message
);
631 * Do the rpcclient commands needed for the Adobe Win9x drivers...
634 snprintf(subcmd
, sizeof(subcmd
),
635 "adddriver \"Windows 4.0\" \"%s:ADOBEPS4.DRV:%s.PPD:NULL:"
636 "ADOBEPS4.HLP:PSMON.DLL:RAW:"
637 "ADOBEPS4.DRV,%s.PPD,ADOBEPS4.HLP,PSMON.DLL,ADFONTS.MFM,"
641 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
642 authfile
, logfile
)) != 0)
644 snprintf(message
, sizeof(message
),
645 _cupsLangString(language
,
646 _("Unable to install Windows 9x printer "
647 "driver files (%d)!")), status
);
649 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
652 _cupsLangPrintf(logfile
, "%s\n", message
);
661 * See if we have the 64-bit Windows PS driver...
671 snprintf(file
, sizeof(file
), "%s/drivers/x64/pscript5.dll", cg
->cups_datadir
);
672 if (!access(file
, 0))
677 * 64-bit Windows driver is installed; do the smbclient commands needed
678 * to copy the Win64 drivers over...
681 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
683 snprintf(subcmd
, sizeof(subcmd
),
686 "put %s/drivers/x64/ps5ui.dll x64/ps5ui.dll;"
687 "put %s/drivers/x64/pscript.hlp x64/pscript.hlp;"
688 "put %s/drivers/x64/pscript.ntf x64/pscript.ntf;"
689 "put %s/drivers/x64/pscript5.dll x64/pscript5.dll",
690 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
691 cg
->cups_datadir
, cg
->cups_datadir
);
693 if ((status
= do_samba_command("smbclient", address
, subcmd
,
694 authfile
, logfile
)) != 0)
696 snprintf(message
, sizeof(message
),
697 _cupsLangString(language
,
698 _("Unable to copy 64-bit Windows printer "
699 "driver files (%d)!")), status
);
701 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
704 _cupsLangPrintf(logfile
, "%s\n", message
);
712 * See if we also have the CUPS driver files; if so, use them!
715 snprintf(file
, sizeof(file
), "%s/drivers/x64/cupsps6.dll", cg
->cups_datadir
);
716 if (!access(file
, 0))
719 * Copy the CUPS driver files over...
722 snprintf(subcmd
, sizeof(subcmd
),
723 "put %s/drivers/x64/cups6.ini x64/cups6.ini;"
724 "put %s/drivers/x64/cupsps6.dll x64/cupsps6.dll;"
725 "put %s/drivers/x64/cupsui6.dll x64/cupsui6.dll",
726 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
728 if ((status
= do_samba_command("smbclient", address
, subcmd
,
729 authfile
, logfile
)) != 0)
731 snprintf(message
, sizeof(message
),
732 _cupsLangString(language
,
733 _("Unable to copy 64-bit CUPS printer driver "
734 "files (%d)!")), status
);
736 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
739 _cupsLangPrintf(logfile
, "%s\n", message
);
747 * Do the rpcclient command needed for the CUPS drivers...
750 snprintf(subcmd
, sizeof(subcmd
),
751 "adddriver \"Windows x64\" \"%s:"
752 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
753 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
754 "cups6.ini,cupsps6.dll,cupsui6.dll\"",
760 * Don't have the CUPS drivers, so just use the standard Windows
764 snprintf(subcmd
, sizeof(subcmd
),
765 "adddriver \"Windows x64\" \"%s:"
766 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
767 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
771 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
772 authfile
, logfile
)) != 0)
774 snprintf(message
, sizeof(message
),
775 _cupsLangString(language
,
776 _("Unable to install Windows 2000 printer "
777 "driver files (%d)!")), status
);
779 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
782 _cupsLangPrintf(logfile
, "%s\n", message
);
790 if (logfile
&& !(have_drivers
& 1))
794 _cupsLangString(language
,
795 _("No Windows printer drivers are installed!")),
799 _cupsLangString(language
,
800 _("Warning, no Windows 2000 printer drivers "
804 _cupsSetError(IPP_NOT_FOUND
, message
);
805 _cupsLangPrintf(logfile
, "%s\n", message
);
808 if (have_drivers
== 0)
810 _cupsSetError(IPP_NOT_FOUND
, message
);
818 * Finally, associate the drivers we just added with the queue...
821 snprintf(subcmd
, sizeof(subcmd
), "setdriver %s %s", dest
, dest
);
823 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
824 authfile
, logfile
)) != 0)
826 snprintf(message
, sizeof(message
),
827 _cupsLangString(language
,
828 _("Unable to set Windows printer driver (%d)!")),
831 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
834 _cupsLangPrintf(logfile
, "%s\n", message
);
848 * 'cupsAdminGetServerSettings()' - Get settings from the server.
850 * The returned settings should be freed with cupsFreeOptions() when
851 * you are done with them.
856 int /* O - 1 on success, 0 on failure */
857 cupsAdminGetServerSettings(
858 http_t
*http
, /* I - Connection to server */
859 int *num_settings
, /* O - Number of settings */
860 cups_option_t
**settings
) /* O - Settings */
862 return (_cupsAdminGetServerSettings(http
, num_settings
, settings
));
867 * '_cupsAdminGetServerSettings()' - Get settings from the server.
869 * The returned settings should be freed with cupsFreeOptions() when
870 * you are done with them.
875 int /* O - 1 on success, 0 on failure */
876 _cupsAdminGetServerSettings(
877 http_t
*http
, /* I - Connection to server */
878 int *num_settings
, /* O - Number of settings */
879 cups_option_t
**settings
) /* O - Settings */
881 int i
; /* Looping var */
882 cups_file_t
*cupsd
; /* cupsd.conf file */
883 char cupsdconf
[1024]; /* cupsd.conf filename */
884 int remote
; /* Remote cupsd.conf file? */
885 http_status_t status
; /* Status of getting cupsd.conf */
886 char line
[1024], /* Line from cupsd.conf file */
887 *value
; /* Value on line */
888 cups_option_t
*setting
; /* Current setting */
889 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
893 * Range check input...
896 if (!http
|| !num_settings
|| !settings
)
898 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
913 * Get the cupsd.conf file...
916 if ((status
= get_cupsd_conf(http
, cg
, cg
->cupsd_update
, cupsdconf
,
917 sizeof(cupsdconf
), &remote
)) == HTTP_OK
)
919 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
921 char message
[1024]; /* Message string */
924 snprintf(message
, sizeof(message
),
925 _cupsLangString(cupsLangDefault(), _("open of %s failed: %s")),
926 cupsdconf
, strerror(errno
));
927 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
936 * Read the file, keeping track of what settings are enabled...
939 int remote_access
= 0, /* Remote access allowed? */
940 remote_admin
= 0, /* Remote administration allowed? */
941 remote_any
= 0, /* Remote access from anywhere allowed? */
942 browsing
= 1, /* Browsing enabled? */
943 browse_allow
= 1, /* Browse address set? */
944 browse_address
= 0, /* Browse address set? */
945 cancel_policy
= 1, /* Cancel-job policy set? */
946 debug_logging
= 0; /* LogLevel debug set? */
947 int linenum
= 0, /* Line number in file */
948 in_location
= 0, /* In a location section? */
949 in_policy
= 0, /* In a policy section? */
950 in_cancel_job
= 0, /* In a cancel-job section? */
951 in_admin_location
= 0; /* In the /admin location? */
954 invalidate_cupsd_cache(cg
);
956 cg
->cupsd_update
= time(NULL
);
957 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
959 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
961 if (!value
&& strncmp(line
, "</", 2))
962 value
= line
+ strlen(line
);
964 if (!strcasecmp(line
, "Port") || !strcasecmp(line
, "Listen"))
966 char *port
; /* Pointer to port number, if any */
969 if ((port
= strrchr(value
, ':')) != NULL
)
971 else if (isdigit(*value
& 255))
974 * Listen on a port number implies remote access...
981 if (strcasecmp(value
, "localhost") && strcmp(value
, "127.0.0.1")
984 #endif /* AF_LOCAL */
986 && strcmp(value
, "::1")
987 #endif /* AF_INET6 */
991 else if (!strcasecmp(line
, "Browsing"))
993 browsing
= !strcasecmp(value
, "yes") || !strcasecmp(value
, "on") ||
994 !strcasecmp(value
, "true");
996 else if (!strcasecmp(line
, "BrowseAddress"))
1000 else if (!strcasecmp(line
, "BrowseAllow"))
1004 else if (!strcasecmp(line
, "BrowseOrder"))
1006 browse_allow
= !strncasecmp(value
, "deny,", 5);
1008 else if (!strcasecmp(line
, "LogLevel"))
1010 debug_logging
= !strncasecmp(value
, "debug", 5);
1012 else if (!strcasecmp(line
, "<Policy") && !strcasecmp(value
, "default"))
1016 else if (!strcasecmp(line
, "</Policy>"))
1020 else if (!strcasecmp(line
, "<Limit") && in_policy
)
1023 * See if the policy limit is for the Cancel-Job operation...
1026 char *valptr
; /* Pointer into value */
1031 for (valptr
= value
; !isspace(*valptr
& 255) && *valptr
; valptr
++);
1036 if (!strcasecmp(value
, "cancel-job") || !strcasecmp(value
, "all"))
1042 for (value
= valptr
; isspace(*value
& 255); value
++);
1045 else if (!strcasecmp(line
, "</Limit>"))
1049 else if (!strcasecmp(line
, "Require") && in_cancel_job
)
1053 else if (!strcasecmp(line
, "<Location"))
1055 in_admin_location
= !strcasecmp(value
, "/admin");
1058 else if (!strcasecmp(line
, "</Location>"))
1060 in_admin_location
= 0;
1063 else if (!strcasecmp(line
, "Allow") &&
1064 strcasecmp(value
, "localhost") && strcasecmp(value
, "127.0.0.1")
1067 #endif /* AF_LOCAL */
1069 && strcmp(value
, "::1")
1070 #endif /* AF_INET6 */
1073 if (in_admin_location
)
1075 else if (!strcasecmp(value
, "all"))
1078 else if (line
[0] != '<' && !in_location
&& !in_policy
)
1079 cg
->cupsd_num_settings
= cupsAddOption(line
, value
,
1080 cg
->cupsd_num_settings
,
1081 &(cg
->cupsd_settings
));
1084 cupsFileClose(cupsd
);
1086 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1087 debug_logging
? "1" : "0",
1088 cg
->cupsd_num_settings
,
1089 &(cg
->cupsd_settings
));
1091 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1092 (remote_access
&& remote_admin
) ?
1094 cg
->cupsd_num_settings
,
1095 &(cg
->cupsd_settings
));
1097 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
1098 remote_any
? "1" : "0",
1099 cg
->cupsd_num_settings
,
1100 &(cg
->cupsd_settings
));
1102 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
1103 (browsing
&& browse_allow
) ?
1105 cg
->cupsd_num_settings
,
1106 &(cg
->cupsd_settings
));
1108 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1109 (remote_access
&& browsing
&&
1110 browse_address
) ? "1" : "0",
1111 cg
->cupsd_num_settings
,
1112 &(cg
->cupsd_settings
));
1114 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1115 cancel_policy
? "1" : "0",
1116 cg
->cupsd_num_settings
,
1117 &(cg
->cupsd_settings
));
1119 else if (status
!= HTTP_NOT_MODIFIED
)
1120 invalidate_cupsd_cache(cg
);
1123 * Remove any temporary files and copy the settings array...
1129 for (i
= cg
->cupsd_num_settings
, setting
= cg
->cupsd_settings
;
1132 *num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1133 *num_settings
, settings
);
1135 return (cg
->cupsd_num_settings
> 0);
1140 * 'cupsAdminSetServerSettings()' - Set settings on the server.
1145 int /* O - 1 on success, 0 on failure */
1146 cupsAdminSetServerSettings(
1147 http_t
*http
, /* I - Connection to server */
1148 int num_settings
, /* I - Number of settings */
1149 cups_option_t
*settings
) /* I - Settings */
1151 return (_cupsAdminSetServerSettings(http
, num_settings
, settings
));
1156 * '_cupsAdminSetServerSettings()' - Set settings on the server.
1161 int /* O - 1 on success, 0 on failure */
1162 _cupsAdminSetServerSettings(
1163 http_t
*http
, /* I - Connection to server */
1164 int num_settings
, /* I - Number of settings */
1165 cups_option_t
*settings
) /* I - Settings */
1167 int i
; /* Looping var */
1168 http_status_t status
; /* GET/PUT status */
1169 const char *server_port_env
; /* SERVER_PORT env var */
1170 int server_port
; /* IPP port for server */
1171 cups_file_t
*cupsd
; /* cupsd.conf file */
1172 char cupsdconf
[1024]; /* cupsd.conf filename */
1173 int remote
; /* Remote cupsd.conf file? */
1174 char tempfile
[1024]; /* Temporary new cupsd.conf */
1175 cups_file_t
*temp
; /* Temporary file */
1176 char line
[1024], /* Line from cupsd.conf file */
1177 *value
; /* Value on line */
1178 int linenum
, /* Line number in file */
1179 in_location
, /* In a location section? */
1180 in_policy
, /* In a policy section? */
1181 in_default_policy
, /* In the default policy section? */
1182 in_cancel_job
, /* In a cancel-job section? */
1183 in_admin_location
, /* In the /admin location? */
1184 in_conf_location
, /* In the /admin/conf location? */
1185 in_root_location
; /* In the / location? */
1186 const char *val
; /* Setting value */
1187 int remote_printers
, /* Show remote printers */
1188 share_printers
, /* Share local printers */
1189 remote_admin
, /* Remote administration allowed? */
1190 remote_any
, /* Remote access from anywhere? */
1191 user_cancel_any
, /* Cancel-job policy set? */
1192 debug_logging
; /* LogLevel debug set? */
1193 int wrote_port_listen
, /* Wrote the port/listen lines? */
1194 wrote_browsing
, /* Wrote the browsing lines? */
1195 wrote_policy
, /* Wrote the policy? */
1196 wrote_loglevel
, /* Wrote the LogLevel line? */
1197 wrote_admin_location
, /* Wrote the /admin location? */
1198 wrote_conf_location
, /* Wrote the /admin/conf location? */
1199 wrote_root_location
; /* Wrote the / location? */
1200 int indent
; /* Indentation */
1201 int cupsd_num_settings
; /* New number of settings */
1202 int old_remote_printers
, /* Show remote printers */
1203 old_share_printers
, /* Share local printers */
1204 old_remote_admin
, /* Remote administration allowed? */
1205 old_user_cancel_any
, /* Cancel-job policy set? */
1206 old_debug_logging
; /* LogLevel debug set? */
1207 cups_option_t
*cupsd_settings
, /* New settings */
1208 *setting
; /* Current setting */
1209 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1213 * Range check input...
1216 if (!http
|| !num_settings
|| !settings
)
1218 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
1224 * Get the cupsd.conf file...
1227 if ((status
= get_cupsd_conf(http
, cg
, 0, cupsdconf
, sizeof(cupsdconf
),
1228 &remote
)) == HTTP_OK
)
1230 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
1232 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
1240 * Get current settings...
1243 if (!_cupsAdminGetServerSettings(http
, &cupsd_num_settings
,
1247 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, cupsd_num_settings
,
1248 cupsd_settings
)) != NULL
)
1249 old_debug_logging
= atoi(val
);
1251 old_debug_logging
= 0;
1253 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, cupsd_num_settings
,
1254 cupsd_settings
)) != NULL
)
1255 old_remote_admin
= atoi(val
);
1257 old_remote_admin
= 0;
1259 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, cupsd_num_settings
,
1260 cupsd_settings
)) != NULL
)
1261 remote_any
= atoi(val
);
1265 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
, cupsd_num_settings
,
1266 cupsd_settings
)) != NULL
)
1267 old_remote_printers
= atoi(val
);
1269 old_remote_printers
= 1;
1271 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, cupsd_num_settings
,
1272 cupsd_settings
)) != NULL
)
1273 old_share_printers
= atoi(val
);
1275 old_share_printers
= 0;
1277 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, cupsd_num_settings
,
1278 cupsd_settings
)) != NULL
)
1279 old_user_cancel_any
= atoi(val
);
1281 old_user_cancel_any
= 0;
1283 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
1286 * Get basic settings...
1289 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, num_settings
,
1292 debug_logging
= atoi(val
);
1294 if (debug_logging
== old_debug_logging
)
1297 * No change to this setting...
1306 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, num_settings
,
1308 remote_any
= atoi(val
);
1310 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, num_settings
,
1313 remote_admin
= atoi(val
);
1315 if (remote_admin
== old_remote_admin
&& remote_any
< 0)
1318 * No change to this setting...
1327 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
, num_settings
,
1330 remote_printers
= atoi(val
);
1332 if (remote_printers
== old_remote_printers
)
1335 * No change to this setting...
1338 remote_printers
= -1;
1342 remote_printers
= -1;
1344 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, num_settings
,
1347 share_printers
= atoi(val
);
1349 if (share_printers
== old_share_printers
&& remote_any
< 0)
1352 * No change to this setting...
1355 share_printers
= -1;
1359 share_printers
= -1;
1361 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, num_settings
,
1364 user_cancel_any
= atoi(val
);
1366 if (user_cancel_any
== old_user_cancel_any
)
1369 * No change to this setting...
1372 user_cancel_any
= -1;
1376 user_cancel_any
= -1;
1379 * Create a temporary file for the new cupsd.conf file...
1382 if ((temp
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1384 cupsFileClose(cupsd
);
1389 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
1394 * Copy the old file to the new, making changes along the way...
1397 cupsd_num_settings
= 0;
1398 in_admin_location
= 0;
1400 in_conf_location
= 0;
1401 in_default_policy
= 0;
1404 in_root_location
= 0;
1406 wrote_admin_location
= 0;
1408 wrote_conf_location
= 0;
1411 wrote_port_listen
= 0;
1412 wrote_root_location
= 0;
1415 if ((server_port_env
= getenv("SERVER_PORT")) != NULL
)
1417 if ((server_port
= atoi(server_port_env
)) <= 0)
1418 server_port
= ippPort();
1421 server_port
= ippPort();
1423 if (server_port
<= 0)
1424 server_port
= IPP_PORT
;
1426 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
1428 if ((!strcasecmp(line
, "Port") || !strcasecmp(line
, "Listen")) &&
1429 (share_printers
>= 0 || remote_admin
>= 0))
1431 if (!wrote_port_listen
)
1433 wrote_port_listen
= 1;
1435 if (share_printers
> 0 || remote_admin
> 0)
1437 cupsFilePuts(temp
, "# Allow remote access\n");
1438 cupsFilePrintf(temp
, "Port %d\n", server_port
);
1442 cupsFilePuts(temp
, "# Only listen for connections from the local "
1444 cupsFilePrintf(temp
, "Listen localhost:%d\n", server_port
);
1447 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1448 if ((!value
|| strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)) &&
1449 !access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1450 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1451 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1453 else if (value
&& value
[0] == '/'
1454 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1455 && strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)
1456 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1458 cupsFilePrintf(temp
, "Listen %s\n", value
);
1460 else if ((!strcasecmp(line
, "Browsing") ||
1461 !strcasecmp(line
, "BrowseAddress") ||
1462 !strcasecmp(line
, "BrowseAllow") ||
1463 !strcasecmp(line
, "BrowseDeny") ||
1464 !strcasecmp(line
, "BrowseLocalProtocols") ||
1465 !strcasecmp(line
, "BrowseRemoteProtocols") ||
1466 !strcasecmp(line
, "BrowseOrder")) &&
1467 (remote_printers
>= 0 || share_printers
>= 0))
1469 if (!wrote_browsing
)
1471 int new_remote_printers
= (remote_printers
> 0 ||
1472 (remote_printers
== -1 &&
1473 old_remote_printers
> 0));
1474 int new_share_printers
= (share_printers
> 0 ||
1475 (share_printers
== -1 &&
1476 old_share_printers
> 0));
1480 if (new_remote_printers
|| new_share_printers
)
1482 const char *localp
= cupsGetOption("BrowseLocalProtocols",
1483 num_settings
, settings
);
1484 const char *remotep
= cupsGetOption("BrowseRemoteProtocols",
1485 num_settings
, settings
);
1488 localp
= cupsGetOption("BrowseLocalProtocols", cupsd_num_settings
,
1492 remotep
= cupsGetOption("BrowseRemoteProtocols", cupsd_num_settings
,
1495 if (new_remote_printers
&& new_share_printers
)
1497 "# Enable printer sharing and shared printers.\n");
1498 else if (new_remote_printers
)
1500 "# Show shared printers on the local network.\n");
1503 "# Share local printers on the local network.\n");
1505 cupsFilePuts(temp
, "Browsing On\n");
1506 cupsFilePuts(temp
, "BrowseOrder allow,deny\n");
1508 if (new_remote_printers
)
1510 cupsFilePuts(temp
, "BrowseAllow all\n");
1512 if (!remotep
|| !*remotep
)
1513 cupsFilePuts(temp
, "BrowseRemoteProtocols "
1514 CUPS_DEFAULT_BROWSE_REMOTE_PROTOCOLS
"\n");
1516 cupsFilePrintf(temp
, "BrowseRemoteProtocols %s\n", remotep
);
1519 cupsFilePuts(temp
, "BrowseRemoteProtocols\n");
1521 if (new_share_printers
)
1523 cupsFilePuts(temp
, "BrowseAddress @LOCAL\n");
1525 if (!localp
|| !*localp
)
1526 cupsFilePuts(temp
, "BrowseLocalProtocols "
1527 CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS
"\n");
1529 cupsFilePrintf(temp
, "BrowseLocalProtocols %s\n", localp
);
1532 cupsFilePuts(temp
, "BrowseLocalProtocols\n");
1537 "# Disable printer sharing and shared printers.\n");
1538 cupsFilePuts(temp
, "Browsing Off\n");
1542 else if (!strcasecmp(line
, "LogLevel") && debug_logging
>= 0)
1549 "# Show troubleshooting information in error_log.\n");
1550 cupsFilePuts(temp
, "LogLevel debug\n");
1554 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1555 cupsFilePuts(temp
, "LogLevel info\n");
1558 else if (!strcasecmp(line
, "<Policy"))
1560 in_default_policy
= !strcasecmp(value
, "default");
1563 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1566 else if (!strcasecmp(line
, "</Policy>"))
1569 if (!wrote_policy
&& in_default_policy
)
1573 if (!user_cancel_any
)
1574 cupsFilePuts(temp
, " # Only the owner or an administrator can "
1576 " <Limit Cancel-Job>\n"
1577 " Order deny,allow\n"
1578 " Require user @OWNER "
1579 CUPS_DEFAULT_PRINTADMIN_AUTH
"\n"
1584 in_default_policy
= 0;
1586 cupsFilePuts(temp
, "</Policy>\n");
1588 else if (!strcasecmp(line
, "<Location"))
1592 if (!strcmp(value
, "/admin"))
1593 in_admin_location
= 1;
1594 if (!strcmp(value
, "/admin/conf"))
1595 in_conf_location
= 1;
1596 else if (!strcmp(value
, "/"))
1597 in_root_location
= 1;
1599 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1601 else if (!strcasecmp(line
, "</Location>"))
1605 if (in_admin_location
&& remote_admin
>= 0)
1607 wrote_admin_location
= 1;
1610 cupsFilePuts(temp
, " # Allow remote administration...\n");
1611 else if (remote_admin
== 0)
1612 cupsFilePuts(temp
, " # Restrict access to the admin pages...\n");
1614 cupsFilePuts(temp
, " Order allow,deny\n");
1617 cupsFilePrintf(temp
, " Allow %s\n",
1618 remote_any
> 0 ? "all" : "@LOCAL");
1620 else if (in_conf_location
&& remote_admin
>= 0)
1622 wrote_conf_location
= 1;
1625 cupsFilePuts(temp
, " # Allow remote access to the configuration "
1628 cupsFilePuts(temp
, " # Restrict access to the configuration "
1631 cupsFilePuts(temp
, " Order allow,deny\n");
1634 cupsFilePrintf(temp
, " Allow %s\n",
1635 remote_any
> 0 ? "all" : "@LOCAL");
1637 else if (in_root_location
&& (remote_admin
>= 0 || share_printers
>= 0))
1639 wrote_root_location
= 1;
1641 if (remote_admin
> 0 && share_printers
> 0)
1642 cupsFilePuts(temp
, " # Allow shared printing and remote "
1643 "administration...\n");
1644 else if (remote_admin
> 0)
1645 cupsFilePuts(temp
, " # Allow remote administration...\n");
1646 else if (share_printers
> 0)
1647 cupsFilePuts(temp
, " # Allow shared printing...\n");
1649 cupsFilePuts(temp
, " # Restrict access to the server...\n");
1651 cupsFilePuts(temp
, " Order allow,deny\n");
1653 if (remote_admin
> 0 || share_printers
> 0)
1654 cupsFilePrintf(temp
, " Allow %s\n",
1655 remote_any
> 0 ? "all" : "@LOCAL");
1658 in_admin_location
= 0;
1659 in_conf_location
= 0;
1660 in_root_location
= 0;
1662 cupsFilePuts(temp
, "</Location>\n");
1664 else if (!strcasecmp(line
, "<Limit") && in_default_policy
)
1667 * See if the policy limit is for the Cancel-Job operation...
1670 char *valptr
; /* Pointer into value */
1675 if (!strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1678 * Don't write anything for this limit section...
1685 cupsFilePrintf(temp
, " %s", line
);
1689 for (valptr
= value
; !isspace(*valptr
& 255) && *valptr
; valptr
++);
1694 if (!strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1697 * Write everything except for this definition...
1703 cupsFilePrintf(temp
, " %s", value
);
1705 for (value
= valptr
; isspace(*value
& 255); value
++);
1708 cupsFilePuts(temp
, ">\n");
1711 else if (!strcasecmp(line
, "</Limit>") && in_cancel_job
)
1715 if (in_cancel_job
== 1)
1716 cupsFilePuts(temp
, " </Limit>\n");
1720 if (!user_cancel_any
)
1721 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1723 " <Limit Cancel-Job>\n"
1724 " Order deny,allow\n"
1725 " Require user @OWNER "
1726 CUPS_DEFAULT_PRINTADMIN_AUTH
"\n"
1731 else if ((((in_admin_location
|| in_conf_location
|| in_root_location
) &&
1732 remote_admin
>= 0) ||
1733 (in_root_location
&& share_printers
>= 0)) &&
1734 (((!strcasecmp(line
, "Allow") || !strcasecmp(line
, "Deny")) &&
1735 !strcasecmp(value
, "@LOCAL")) ||
1736 !strcasecmp(line
, "Order")))
1738 else if (in_cancel_job
== 2)
1740 else if (!strcasecmp(line
, "<Limit") && value
)
1741 cupsFilePrintf(temp
, " %s %s>\n", line
, value
);
1742 else if (line
[0] == '<')
1746 cupsFilePrintf(temp
, "%*s%s %s>\n", indent
, "", line
, value
);
1754 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1757 else if (!in_policy
&& !in_location
&&
1758 (val
= cupsGetOption(line
, num_settings
, settings
)) != NULL
)
1761 * Replace this directive's value with the new one...
1764 cupsd_num_settings
= cupsAddOption(line
, val
, cupsd_num_settings
,
1768 * Write the new value in its place, without indentation since we
1769 * only support setting root directives, not in sections...
1772 cupsFilePrintf(temp
, "%s %s\n", line
, val
);
1776 if (!in_policy
&& !in_location
)
1779 * Record the non-policy, non-location directives that we find
1780 * in the server settings, since we cache this info and record it
1781 * in _cupsAdminGetServerSettings()...
1784 cupsd_num_settings
= cupsAddOption(line
, value
, cupsd_num_settings
,
1788 cupsFilePrintf(temp
, "%*s%s %s\n", indent
, "", line
, value
);
1791 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1795 * Write any missing info...
1798 if (!wrote_browsing
&& (remote_printers
>= 0 || share_printers
>= 0))
1800 if (remote_printers
> 0 || share_printers
> 0)
1802 if (remote_printers
> 0 && share_printers
> 0)
1803 cupsFilePuts(temp
, "# Enable printer sharing and shared printers.\n");
1804 else if (remote_printers
> 0)
1805 cupsFilePuts(temp
, "# Show shared printers on the local network.\n");
1807 cupsFilePuts(temp
, "# Share local printers on the local network.\n");
1809 cupsFilePuts(temp
, "Browsing On\n");
1810 cupsFilePuts(temp
, "BrowseOrder allow,deny\n");
1812 if (remote_printers
> 0)
1813 cupsFilePuts(temp
, "BrowseAllow all\n");
1815 if (share_printers
> 0)
1816 cupsFilePuts(temp
, "BrowseAddress @LOCAL\n");
1820 cupsFilePuts(temp
, "# Disable printer sharing and shared printers.\n");
1821 cupsFilePuts(temp
, "Browsing Off\n");
1825 if (!wrote_loglevel
&& debug_logging
>= 0)
1829 cupsFilePuts(temp
, "# Show troubleshooting information in error_log.\n");
1830 cupsFilePuts(temp
, "LogLevel debug\n");
1834 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1835 cupsFilePuts(temp
, "LogLevel info\n");
1839 if (!wrote_port_listen
&& (share_printers
>= 0 || remote_admin
>= 0))
1841 if (share_printers
> 0 || remote_admin
> 0)
1843 cupsFilePuts(temp
, "# Allow remote access\n");
1844 cupsFilePrintf(temp
, "Port %d\n", ippPort());
1849 "# Only listen for connections from the local machine.\n");
1850 cupsFilePrintf(temp
, "Listen localhost:%d\n", ippPort());
1853 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1854 if (!access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1855 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1856 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1859 if (!wrote_root_location
&& (remote_admin
>= 0 || share_printers
>= 0))
1861 if (remote_admin
> 0 && share_printers
> 0)
1863 "# Allow shared printing and remote administration...\n");
1864 else if (remote_admin
> 0)
1865 cupsFilePuts(temp
, "# Allow remote administration...\n");
1866 else if (share_printers
> 0)
1867 cupsFilePuts(temp
, "# Allow shared printing...\n");
1869 cupsFilePuts(temp
, "# Restrict access to the server...\n");
1871 cupsFilePuts(temp
, "<Location />\n"
1872 " Order allow,deny\n");
1874 if (remote_admin
> 0 || share_printers
> 0)
1875 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1877 cupsFilePuts(temp
, "</Location>\n");
1880 if (!wrote_admin_location
&& remote_admin
>= 0)
1883 cupsFilePuts(temp
, "# Allow remote administration...\n");
1885 cupsFilePuts(temp
, "# Restrict access to the admin pages...\n");
1887 cupsFilePuts(temp
, "<Location /admin>\n"
1888 " Order allow,deny\n");
1891 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1893 cupsFilePuts(temp
, "</Location>\n");
1896 if (!wrote_conf_location
&& remote_admin
>= 0)
1900 "# Allow remote access to the configuration files...\n");
1902 cupsFilePuts(temp
, "# Restrict access to the configuration files...\n");
1904 cupsFilePuts(temp
, "<Location /admin/conf>\n"
1905 " AuthType Default\n"
1906 " Require user @SYSTEM\n"
1907 " Order allow,deny\n");
1910 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1912 cupsFilePuts(temp
, "</Location>\n");
1915 if (!wrote_policy
&& user_cancel_any
>= 0)
1917 cupsFilePuts(temp
, "<Policy default>\n"
1918 " # Job-related operations must be done by the owner "
1919 "or an administrator...\n"
1920 " <Limit Send-Document Send-URI Hold-Job Release-Job "
1921 "Restart-Job Purge-Jobs Set-Job-Attributes "
1922 "Create-Job-Subscription Renew-Subscription "
1923 "Cancel-Subscription Get-Notifications Reprocess-Job "
1924 "Cancel-Current-Job Suspend-Current-Job Resume-Job "
1926 " Require user @OWNER @SYSTEM\n"
1927 " Order deny,allow\n"
1929 " # All administration operations require an "
1930 "administrator to authenticate...\n"
1931 " <Limit Pause-Printer Resume-Printer "
1932 "Set-Printer-Attributes Enable-Printer "
1933 "Disable-Printer Pause-Printer-After-Current-Job "
1934 "Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer "
1935 "Activate-Printer Restart-Printer Shutdown-Printer "
1936 "Startup-Printer Promote-Job Schedule-Job-After "
1937 "CUPS-Add-Printer CUPS-Delete-Printer "
1938 "CUPS-Add-Class CUPS-Delete-Class "
1939 "CUPS-Accept-Jobs CUPS-Reject-Jobs "
1940 "CUPS-Set-Default CUPS-Add-Device CUPS-Delete-Device>\n"
1941 " AuthType Default\n"
1942 " Require user @SYSTEM\n"
1943 " Order deny,allow\n"
1946 if (!user_cancel_any
)
1947 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1949 " <Limit Cancel-Job>\n"
1950 " Order deny,allow\n"
1951 " Require user @OWNER "
1952 CUPS_DEFAULT_PRINTADMIN_AUTH
"\n"
1955 cupsFilePuts(temp
, " <Limit All>\n"
1956 " Order deny,allow\n"
1961 for (i
= num_settings
, setting
= settings
; i
> 0; i
--, setting
++)
1962 if (setting
->name
[0] != '_' &&
1963 !cupsGetOption(setting
->name
, cupsd_num_settings
, cupsd_settings
))
1966 * Add this directive to the list of directives we have written...
1969 cupsd_num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1970 cupsd_num_settings
, &cupsd_settings
);
1973 * Write the new value, without indentation since we only support
1974 * setting root directives, not in sections...
1977 cupsFilePrintf(temp
, "%s %s\n", setting
->name
, setting
->value
);
1980 cupsFileClose(cupsd
);
1981 cupsFileClose(temp
);
1984 * Upload the configuration file to the server...
1987 status
= cupsPutFile(http
, "/admin/conf/cupsd.conf", tempfile
);
1989 if (status
== HTTP_CREATED
)
1992 * Updated OK, add the basic settings...
1995 if (debug_logging
>= 0)
1996 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1997 debug_logging
? "1" : "0",
1998 cupsd_num_settings
, &cupsd_settings
);
2000 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
2001 old_debug_logging
? "1" : "0",
2002 cupsd_num_settings
, &cupsd_settings
);
2004 if (remote_admin
>= 0)
2005 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
2006 remote_admin
? "1" : "0",
2007 cupsd_num_settings
, &cupsd_settings
);
2009 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
2010 old_remote_admin
? "1" : "0",
2011 cupsd_num_settings
, &cupsd_settings
);
2013 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
2014 remote_any
? "1" : "0",
2015 cupsd_num_settings
, &cupsd_settings
);
2017 if (remote_printers
>= 0)
2018 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
2019 remote_printers
? "1" : "0",
2020 cupsd_num_settings
, &cupsd_settings
);
2022 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
2023 old_remote_printers
? "1" : "0",
2024 cupsd_num_settings
, &cupsd_settings
);
2026 if (share_printers
>= 0)
2027 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
2028 share_printers
? "1" : "0",
2029 cupsd_num_settings
, &cupsd_settings
);
2031 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
2032 old_share_printers
? "1" : "0",
2033 cupsd_num_settings
, &cupsd_settings
);
2035 if (user_cancel_any
>= 0)
2036 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
2037 user_cancel_any
? "1" : "0",
2038 cupsd_num_settings
, &cupsd_settings
);
2040 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
2041 old_user_cancel_any
? "1" : "0",
2042 cupsd_num_settings
, &cupsd_settings
);
2045 * Save the new values...
2048 invalidate_cupsd_cache(cg
);
2050 cg
->cupsd_num_settings
= cupsd_num_settings
;
2051 cg
->cupsd_settings
= cupsd_settings
;
2052 cg
->cupsd_update
= time(NULL
);
2054 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
2057 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
2060 * Remote our temp files and return...
2068 return (status
== HTTP_CREATED
);
2073 * 'do_samba_command()' - Do a SAMBA command.
2076 static int /* O - Status of command */
2077 do_samba_command(const char *command
, /* I - Command to run */
2078 const char *address
, /* I - Address for command */
2079 const char *subcmd
, /* I - Sub-command */
2080 const char *authfile
, /* I - Samba authentication file */
2081 FILE *logfile
) /* I - Optional log file */
2084 return (1); /* Always fail on Windows... */
2087 int status
; /* Status of command */
2088 int pid
; /* Process ID of child */
2092 _cupsLangPrintf(logfile
,
2093 _("Running command: %s %s -N -A %s -c \'%s\'\n"),
2094 command
, address
, authfile
, subcmd
);
2096 if ((pid
= fork()) == 0)
2099 * Child goes here, redirect stdin/out/err and execute the command...
2103 open("/dev/null", O_RDONLY
);
2108 dup(fileno(logfile
));
2110 open("/dev/null", O_WRONLY
);
2115 execlp(command
, command
, address
, "-N", "-A", authfile
, "-c", subcmd
,
2124 _cupsLangPrintf(logfile
, _("Unable to run \"%s\": %s\n"),
2125 command
, strerror(errno
));
2130 * Wait for the process to complete...
2133 while (wait(&status
) != pid
);
2137 _cupsLangPuts(logfile
, "\n");
2139 DEBUG_printf(("status=%d\n", status
));
2141 if (WIFEXITED(status
))
2142 return (WEXITSTATUS(status
));
2144 return (-WTERMSIG(status
));
2150 * 'get_cupsd_conf()' - Get the current cupsd.conf file.
2153 static http_status_t
/* O - Status of request */
2155 http_t
*http
, /* I - Connection to server */
2156 _cups_globals_t
*cg
, /* I - Global data */
2157 time_t last_update
, /* I - Last update time for file */
2158 char *name
, /* I - Filename buffer */
2159 int namesize
, /* I - Size of filename buffer */
2160 int *remote
) /* O - Remote file? */
2162 int fd
; /* Temporary file descriptor */
2164 struct stat info
; /* cupsd.conf file information */
2166 http_status_t status
; /* Status of getting cupsd.conf */
2167 char host
[HTTP_MAX_HOST
]; /* Hostname for connection */
2171 * See if we already have the data we need...
2174 httpGetHostname(http
, host
, sizeof(host
));
2176 if (strcasecmp(cg
->cupsd_hostname
, host
))
2177 invalidate_cupsd_cache(cg
);
2179 snprintf(name
, namesize
, "%s/cupsd.conf", cg
->cups_serverroot
);
2183 if (!strcasecmp(host
, "localhost") && !access(name
, R_OK
))
2186 * Read the local file rather than using HTTP...
2189 if (stat(name
, &info
))
2191 char message
[1024]; /* Message string */
2194 snprintf(message
, sizeof(message
),
2195 _cupsLangString(cupsLangDefault(), _("stat of %s failed: %s")),
2196 name
, strerror(errno
));
2197 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
2201 return (HTTP_SERVER_ERROR
);
2203 else if (last_update
&& info
.st_mtime
<= last_update
)
2204 status
= HTTP_NOT_MODIFIED
;
2212 * Read cupsd.conf via a HTTP GET request...
2215 if ((fd
= cupsTempFd(name
, namesize
)) < 0)
2219 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
2221 invalidate_cupsd_cache(cg
);
2223 return (HTTP_SERVER_ERROR
);
2228 httpClearFields(http
);
2231 httpSetField(http
, HTTP_FIELD_IF_MODIFIED_SINCE
,
2232 httpGetDateString(last_update
));
2234 status
= cupsGetFd(http
, "/admin/conf/cupsd.conf", fd
);
2238 if (status
!= HTTP_OK
)
2250 * 'invalidate_cupsd_cache()' - Invalidate the cached cupsd.conf settings.
2254 invalidate_cupsd_cache(
2255 _cups_globals_t
*cg
) /* I - Global data */
2257 cupsFreeOptions(cg
->cupsd_num_settings
, cg
->cupsd_settings
);
2259 cg
->cupsd_hostname
[0] = '\0';
2260 cg
->cupsd_update
= 0;
2261 cg
->cupsd_num_settings
= 0;
2262 cg
->cupsd_settings
= NULL
;
2267 * 'write_option()' - Write a CUPS option to a PPD file.
2271 write_option(cups_file_t
*dstfp
, /* I - PPD file */
2272 int order
, /* I - Order dependency */
2273 const char *name
, /* I - Option name */
2274 const char *text
, /* I - Option text */
2275 const char *attrname
, /* I - Attribute name */
2276 ipp_attribute_t
*suppattr
, /* I - IPP -supported attribute */
2277 ipp_attribute_t
*defattr
, /* I - IPP -default attribute */
2278 int defval
, /* I - Default value number */
2279 int valcount
) /* I - Number of values */
2281 int i
; /* Looping var */
2284 cupsFilePrintf(dstfp
, "*JCLOpenUI *%s/%s: PickOne\n"
2285 "*OrderDependency: %d JCLSetup *%s\n",
2286 name
, text
, order
, name
);
2288 if (defattr
->value_tag
== IPP_TAG_INTEGER
)
2291 * Do numeric options with a range or list...
2294 cupsFilePrintf(dstfp
, "*Default%s: %d\n", name
,
2295 defattr
->values
[defval
].integer
);
2297 if (suppattr
->value_tag
== IPP_TAG_RANGE
)
2300 * List each number in the range...
2303 for (i
= suppattr
->values
[0].range
.lower
;
2304 i
<= suppattr
->values
[0].range
.upper
;
2307 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, i
);
2310 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n",
2312 else if (defval
== 0)
2313 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
, i
);
2314 else if (defval
< (valcount
- 1))
2315 cupsFilePrintf(dstfp
, ",%d\"\n", i
);
2317 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", i
);
2323 * List explicit numbers...
2326 for (i
= 0; i
< suppattr
->num_values
; i
++)
2328 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, suppattr
->values
[i
].integer
);
2331 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n", attrname
,
2332 suppattr
->values
[i
].integer
);
2333 else if (defval
== 0)
2334 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
,
2335 suppattr
->values
[i
].integer
);
2336 else if (defval
< (valcount
- 1))
2337 cupsFilePrintf(dstfp
, ",%d\"\n", suppattr
->values
[i
].integer
);
2339 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", suppattr
->values
[i
].integer
);
2346 * Do text options with a list...
2349 cupsFilePrintf(dstfp
, "*Default%s: %s\n", name
,
2350 defattr
->values
[defval
].string
.text
);
2352 for (i
= 0; i
< suppattr
->num_values
; i
++)
2354 cupsFilePrintf(dstfp
, "*%s %s: \"", name
,
2355 suppattr
->values
[i
].string
.text
);
2358 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\n\"\n*End\n", attrname
,
2359 suppattr
->values
[i
].string
.text
);
2360 else if (defval
== 0)
2361 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\"\n", attrname
,
2362 suppattr
->values
[i
].string
.text
);
2363 else if (defval
< (valcount
- 1))
2364 cupsFilePrintf(dstfp
, ",%s\"\n", suppattr
->values
[i
].string
.text
);
2366 cupsFilePrintf(dstfp
, ",%s\n\"\n*End\n",
2367 suppattr
->values
[i
].string
.text
);
2371 cupsFilePrintf(dstfp
, "*JCLCloseUI: *%s\n\n", name
);
2376 * End of "$Id: adminutil.c 6930 2007-09-08 00:28:06Z mike $".