2 * "$Id: adminutil.c 6649 2007-07-11 21:46:42Z mike $"
4 * Administration utility API definitions for the Common UNIX Printing
7 * Copyright 2007 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
);
586 snprintf(file
, sizeof(file
), "%s/drivers/ADOBEPS4.DRV", cg
->cups_datadir
);
587 if (!access(file
, 0))
592 * Do the smbclient commands needed for the Adobe Win9x drivers...
595 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
597 snprintf(subcmd
, sizeof(subcmd
),
599 "put %s WIN40/%s.PPD;"
600 "put %s/drivers/ADFONTS.MFM WIN40/ADFONTS.MFM;"
601 "put %s/drivers/ADOBEPS4.DRV WIN40/ADOBEPS4.DRV;"
602 "put %s/drivers/ADOBEPS4.HLP WIN40/ADOBEPS4.HLP;"
603 "put %s/drivers/ICONLIB.DLL WIN40/ICONLIB.DLL;"
604 "put %s/drivers/PSMON.DLL WIN40/PSMON.DLL;",
605 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
606 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
608 if ((status
= do_samba_command("smbclient", address
, subcmd
,
609 authfile
, logfile
)) != 0)
611 snprintf(message
, sizeof(message
),
612 _cupsLangString(language
,
613 _("Unable to copy Windows 9x printer "
614 "driver files (%d)!")), status
);
616 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
619 _cupsLangPrintf(logfile
, "%s\n", message
);
627 * Do the rpcclient commands needed for the Adobe Win9x drivers...
630 snprintf(subcmd
, sizeof(subcmd
),
631 "adddriver \"Windows 4.0\" \"%s:ADOBEPS4.DRV:%s.PPD:NULL:"
632 "ADOBEPS4.HLP:PSMON.DLL:RAW:"
633 "ADOBEPS4.DRV,%s.PPD,ADOBEPS4.HLP,PSMON.DLL,ADFONTS.MFM,"
637 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
638 authfile
, logfile
)) != 0)
640 snprintf(message
, sizeof(message
),
641 _cupsLangString(language
,
642 _("Unable to install Windows 9x printer "
643 "driver files (%d)!")), status
);
645 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
648 _cupsLangPrintf(logfile
, "%s\n", message
);
656 if (logfile
&& !(have_drivers
& 1))
660 _cupsLangString(language
,
661 _("No Windows printer drivers are installed!")),
665 _cupsLangString(language
,
666 _("Warning, no Windows 2000 printer drivers "
670 _cupsSetError(IPP_NOT_FOUND
, message
);
671 _cupsLangPrintf(logfile
, "%s\n", message
);
674 if (have_drivers
== 0)
676 _cupsSetError(IPP_NOT_FOUND
, message
);
681 * Finally, associate the drivers we just added with the queue...
684 snprintf(subcmd
, sizeof(subcmd
), "setdriver %s %s", dest
, dest
);
686 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
687 authfile
, logfile
)) != 0)
689 snprintf(message
, sizeof(message
),
690 _cupsLangString(language
,
691 _("Unable to set Windows printer driver (%d)!")),
694 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
697 _cupsLangPrintf(logfile
, "%s\n", message
);
711 * 'cupsAdminGetServerSettings()' - Get settings from the server.
713 * The returned settings should be freed with cupsFreeOptions() when
714 * you are done with them.
719 int /* O - 1 on success, 0 on failure */
720 cupsAdminGetServerSettings(
721 http_t
*http
, /* I - Connection to server */
722 int *num_settings
, /* O - Number of settings */
723 cups_option_t
**settings
) /* O - Settings */
725 return (_cupsAdminGetServerSettings(http
, num_settings
, settings
));
730 * '_cupsAdminGetServerSettings()' - Get settings from the server.
732 * The returned settings should be freed with cupsFreeOptions() when
733 * you are done with them.
738 int /* O - 1 on success, 0 on failure */
739 _cupsAdminGetServerSettings(
740 http_t
*http
, /* I - Connection to server */
741 int *num_settings
, /* O - Number of settings */
742 cups_option_t
**settings
) /* O - Settings */
744 int i
; /* Looping var */
745 cups_file_t
*cupsd
; /* cupsd.conf file */
746 char cupsdconf
[1024]; /* cupsd.conf filename */
747 int remote
; /* Remote cupsd.conf file? */
748 http_status_t status
; /* Status of getting cupsd.conf */
749 char line
[1024], /* Line from cupsd.conf file */
750 *value
; /* Value on line */
751 cups_option_t
*setting
; /* Current setting */
752 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
756 * Range check input...
759 if (!http
|| !num_settings
|| !settings
)
761 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
776 * Get the cupsd.conf file...
779 if ((status
= get_cupsd_conf(http
, cg
, cg
->cupsd_update
, cupsdconf
,
780 sizeof(cupsdconf
), &remote
)) == HTTP_OK
)
782 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
784 char message
[1024]; /* Message string */
787 snprintf(message
, sizeof(message
),
788 _cupsLangString(cupsLangDefault(), _("open of %s failed: %s")),
789 cupsdconf
, strerror(errno
));
790 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
799 * Read the file, keeping track of what settings are enabled...
802 int remote_access
= 0, /* Remote access allowed? */
803 remote_admin
= 0, /* Remote administration allowed? */
804 remote_any
= 0, /* Remote access from anywhere allowed? */
805 browsing
= 1, /* Browsing enabled? */
806 browse_allow
= 1, /* Browse address set? */
807 browse_address
= 0, /* Browse address set? */
808 cancel_policy
= 1, /* Cancel-job policy set? */
809 debug_logging
= 0; /* LogLevel debug set? */
810 int linenum
= 0, /* Line number in file */
811 in_location
= 0, /* In a location section? */
812 in_policy
= 0, /* In a policy section? */
813 in_cancel_job
= 0, /* In a cancel-job section? */
814 in_admin_location
= 0; /* In the /admin location? */
817 invalidate_cupsd_cache(cg
);
819 cg
->cupsd_update
= time(NULL
);
820 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
822 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
827 if (!strcasecmp(line
, "Port") || !strcasecmp(line
, "Listen"))
829 char *port
; /* Pointer to port number, if any */
832 if ((port
= strrchr(value
, ':')) != NULL
)
834 else if (isdigit(*value
& 255))
837 * Listen on a port number implies remote access...
844 if (strcasecmp(value
, "localhost") && strcmp(value
, "127.0.0.1")
847 #endif /* AF_LOCAL */
849 && strcmp(value
, "::1")
850 #endif /* AF_INET6 */
854 else if (!strcasecmp(line
, "Browsing"))
856 browsing
= !strcasecmp(value
, "yes") || !strcasecmp(value
, "on") ||
857 !strcasecmp(value
, "true");
859 else if (!strcasecmp(line
, "BrowseAddress"))
863 else if (!strcasecmp(line
, "BrowseAllow"))
867 else if (!strcasecmp(line
, "BrowseOrder"))
869 browse_allow
= !strncasecmp(value
, "deny,", 5);
871 else if (!strcasecmp(line
, "LogLevel"))
873 debug_logging
= !strncasecmp(value
, "debug", 5);
875 else if (!strcasecmp(line
, "<Policy") && !strcasecmp(value
, "default"))
879 else if (!strcasecmp(line
, "</Policy>"))
883 else if (!strcasecmp(line
, "<Limit") && in_policy
)
886 * See if the policy limit is for the Cancel-Job operation...
889 char *valptr
; /* Pointer into value */
894 for (valptr
= value
; !isspace(*valptr
& 255) && *valptr
; valptr
++);
899 if (!strcasecmp(value
, "cancel-job") || !strcasecmp(value
, "all"))
905 for (value
= valptr
; isspace(*value
& 255); value
++);
908 else if (!strcasecmp(line
, "</Limit>"))
912 else if (!strcasecmp(line
, "Require") && in_cancel_job
)
916 else if (!strcasecmp(line
, "<Location"))
918 in_admin_location
= !strcasecmp(value
, "/admin");
921 else if (!strcasecmp(line
, "</Location>"))
923 in_admin_location
= 0;
926 else if (!strcasecmp(line
, "Allow") && in_admin_location
&&
927 strcasecmp(value
, "localhost") && strcasecmp(value
, "127.0.0.1")
930 #endif /* AF_LOCAL */
932 && strcmp(value
, "::1")
933 #endif /* AF_INET6 */
938 if (!strcasecmp(value
, "all"))
941 else if (line
[0] != '<' && !in_location
&& !in_policy
)
942 cg
->cupsd_num_settings
= cupsAddOption(line
, value
,
943 cg
->cupsd_num_settings
,
944 &(cg
->cupsd_settings
));
947 cupsFileClose(cupsd
);
949 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
950 debug_logging
? "1" : "0",
951 cg
->cupsd_num_settings
,
952 &(cg
->cupsd_settings
));
954 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
955 (remote_access
&& remote_admin
) ?
957 cg
->cupsd_num_settings
,
958 &(cg
->cupsd_settings
));
960 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
961 remote_any
? "1" : "0",
962 cg
->cupsd_num_settings
,
963 &(cg
->cupsd_settings
));
965 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
966 (browsing
&& browse_allow
) ?
968 cg
->cupsd_num_settings
,
969 &(cg
->cupsd_settings
));
971 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
972 (remote_access
&& browsing
&&
973 browse_address
) ? "1" : "0",
974 cg
->cupsd_num_settings
,
975 &(cg
->cupsd_settings
));
977 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
978 cancel_policy
? "1" : "0",
979 cg
->cupsd_num_settings
,
980 &(cg
->cupsd_settings
));
982 else if (status
!= HTTP_NOT_MODIFIED
)
983 invalidate_cupsd_cache(cg
);
986 * Remove any temporary files and copy the settings array...
992 for (i
= cg
->cupsd_num_settings
, setting
= cg
->cupsd_settings
;
995 *num_settings
= cupsAddOption(setting
->name
, setting
->value
,
996 *num_settings
, settings
);
998 return (cg
->cupsd_num_settings
> 0);
1003 * 'cupsAdminSetServerSettings()' - Set settings on the server.
1008 int /* O - 1 on success, 0 on failure */
1009 cupsAdminSetServerSettings(
1010 http_t
*http
, /* I - Connection to server */
1011 int num_settings
, /* I - Number of settings */
1012 cups_option_t
*settings
) /* I - Settings */
1014 return (_cupsAdminSetServerSettings(http
, num_settings
, settings
));
1019 * '_cupsAdminSetServerSettings()' - Set settings on the server.
1024 int /* O - 1 on success, 0 on failure */
1025 _cupsAdminSetServerSettings(
1026 http_t
*http
, /* I - Connection to server */
1027 int num_settings
, /* I - Number of settings */
1028 cups_option_t
*settings
) /* I - Settings */
1030 int i
; /* Looping var */
1031 http_status_t status
; /* GET/PUT status */
1032 const char *server_port_env
; /* SERVER_PORT env var */
1033 int server_port
; /* IPP port for server */
1034 cups_file_t
*cupsd
; /* cupsd.conf file */
1035 char cupsdconf
[1024]; /* cupsd.conf filename */
1036 int remote
; /* Remote cupsd.conf file? */
1037 char tempfile
[1024]; /* Temporary new cupsd.conf */
1038 cups_file_t
*temp
; /* Temporary file */
1039 char line
[1024], /* Line from cupsd.conf file */
1040 *value
; /* Value on line */
1041 int linenum
, /* Line number in file */
1042 in_location
, /* In a location section? */
1043 in_policy
, /* In a policy section? */
1044 in_default_policy
, /* In the default policy section? */
1045 in_cancel_job
, /* In a cancel-job section? */
1046 in_admin_location
, /* In the /admin location? */
1047 in_conf_location
, /* In the /admin/conf location? */
1048 in_root_location
; /* In the / location? */
1049 const char *val
; /* Setting value */
1050 int remote_printers
, /* Show remote printers */
1051 share_printers
, /* Share local printers */
1052 remote_admin
, /* Remote administration allowed? */
1053 remote_any
, /* Remote access from anywhere? */
1054 user_cancel_any
, /* Cancel-job policy set? */
1055 debug_logging
; /* LogLevel debug set? */
1056 int wrote_port_listen
, /* Wrote the port/listen lines? */
1057 wrote_browsing
, /* Wrote the browsing lines? */
1058 wrote_policy
, /* Wrote the policy? */
1059 wrote_loglevel
, /* Wrote the LogLevel line? */
1060 wrote_admin_location
, /* Wrote the /admin location? */
1061 wrote_conf_location
, /* Wrote the /admin/conf location? */
1062 wrote_root_location
; /* Wrote the / location? */
1063 int indent
; /* Indentation */
1064 int cupsd_num_settings
; /* New number of settings */
1065 int old_remote_printers
, /* Show remote printers */
1066 old_share_printers
, /* Share local printers */
1067 old_remote_admin
, /* Remote administration allowed? */
1068 old_user_cancel_any
, /* Cancel-job policy set? */
1069 old_debug_logging
; /* LogLevel debug set? */
1070 cups_option_t
*cupsd_settings
, /* New settings */
1071 *setting
; /* Current setting */
1072 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1076 * Range check input...
1079 if (!http
|| !num_settings
|| !settings
)
1081 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
1087 * Get the cupsd.conf file...
1090 if ((status
= get_cupsd_conf(http
, cg
, 0, cupsdconf
, sizeof(cupsdconf
),
1091 &remote
)) == HTTP_OK
)
1093 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
1095 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
1103 * Get current settings...
1106 if (!_cupsAdminGetServerSettings(http
, &cupsd_num_settings
,
1110 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, cupsd_num_settings
,
1111 cupsd_settings
)) != NULL
)
1112 old_debug_logging
= atoi(val
);
1114 old_debug_logging
= 0;
1116 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, cupsd_num_settings
,
1117 cupsd_settings
)) != NULL
)
1118 old_remote_admin
= atoi(val
);
1120 old_remote_admin
= 0;
1122 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, cupsd_num_settings
,
1123 cupsd_settings
)) != NULL
)
1124 remote_any
= atoi(val
);
1128 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
, cupsd_num_settings
,
1129 cupsd_settings
)) != NULL
)
1130 old_remote_printers
= atoi(val
);
1132 old_remote_printers
= 1;
1134 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, cupsd_num_settings
,
1135 cupsd_settings
)) != NULL
)
1136 old_share_printers
= atoi(val
);
1138 old_share_printers
= 0;
1140 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, cupsd_num_settings
,
1141 cupsd_settings
)) != NULL
)
1142 old_user_cancel_any
= atoi(val
);
1144 old_user_cancel_any
= 0;
1146 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
1149 * Get basic settings...
1152 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, num_settings
,
1155 debug_logging
= atoi(val
);
1157 if (debug_logging
== old_debug_logging
)
1160 * No change to this setting...
1169 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, num_settings
,
1171 remote_any
= atoi(val
);
1173 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, num_settings
,
1176 remote_admin
= atoi(val
);
1178 if (remote_admin
== old_remote_admin
&& remote_any
< 0)
1181 * No change to this setting...
1190 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
, num_settings
,
1193 remote_printers
= atoi(val
);
1195 if (remote_printers
== old_remote_printers
)
1198 * No change to this setting...
1201 remote_printers
= -1;
1205 remote_printers
= -1;
1207 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, num_settings
,
1210 share_printers
= atoi(val
);
1212 if (share_printers
== old_share_printers
&& remote_any
< 0)
1215 * No change to this setting...
1218 share_printers
= -1;
1222 share_printers
= -1;
1224 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, num_settings
,
1227 user_cancel_any
= atoi(val
);
1229 if (user_cancel_any
== old_user_cancel_any
)
1232 * No change to this setting...
1235 user_cancel_any
= -1;
1239 user_cancel_any
= -1;
1242 * Create a temporary file for the new cupsd.conf file...
1245 if ((temp
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1247 cupsFileClose(cupsd
);
1252 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
1257 * Copy the old file to the new, making changes along the way...
1260 cupsd_num_settings
= 0;
1261 in_admin_location
= 0;
1263 in_conf_location
= 0;
1264 in_default_policy
= 0;
1267 in_root_location
= 0;
1269 wrote_admin_location
= 0;
1271 wrote_conf_location
= 0;
1274 wrote_port_listen
= 0;
1275 wrote_root_location
= 0;
1278 if ((server_port_env
= getenv("SERVER_PORT")) != NULL
)
1280 if ((server_port
= atoi(server_port_env
)) <= 0)
1281 server_port
= ippPort();
1284 server_port
= ippPort();
1286 if (server_port
<= 0)
1287 server_port
= IPP_PORT
;
1289 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
1291 if ((!strcasecmp(line
, "Port") || !strcasecmp(line
, "Listen")) &&
1292 (share_printers
>= 0 || remote_admin
>= 0))
1294 if (!wrote_port_listen
)
1296 wrote_port_listen
= 1;
1298 if (share_printers
> 0 || remote_admin
> 0)
1300 cupsFilePuts(temp
, "# Allow remote access\n");
1301 cupsFilePrintf(temp
, "Port %d\n", server_port
);
1305 cupsFilePuts(temp
, "# Only listen for connections from the local "
1307 cupsFilePrintf(temp
, "Listen localhost:%d\n", server_port
);
1310 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1311 if ((!value
|| strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)) &&
1312 !access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1313 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1314 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1316 else if (value
&& value
[0] == '/'
1317 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1318 && strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)
1319 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1321 cupsFilePrintf(temp
, "Listen %s\n", value
);
1323 else if ((!strcasecmp(line
, "Browsing") ||
1324 !strcasecmp(line
, "BrowseAddress") ||
1325 !strcasecmp(line
, "BrowseAllow") ||
1326 !strcasecmp(line
, "BrowseDeny") ||
1327 !strcasecmp(line
, "BrowseOrder")) &&
1328 (remote_printers
>= 0 || share_printers
>= 0))
1330 if (!wrote_browsing
)
1332 int new_remote_printers
= (remote_printers
> 0 ||
1333 (remote_printers
== -1 &&
1334 old_remote_printers
> 0));
1335 int new_share_printers
= (share_printers
> 0 ||
1336 (share_printers
== -1 &&
1337 old_share_printers
> 0));
1341 if (new_remote_printers
|| new_share_printers
)
1343 if (new_remote_printers
&& new_share_printers
)
1345 "# Enable printer sharing and shared printers.\n");
1346 else if (new_remote_printers
)
1348 "# Show shared printers on the local network.\n");
1351 "# Share local printers on the local network.\n");
1353 cupsFilePuts(temp
, "Browsing On\n");
1354 cupsFilePuts(temp
, "BrowseOrder allow,deny\n");
1356 if (new_remote_printers
)
1357 cupsFilePuts(temp
, "BrowseAllow all\n");
1359 if (new_share_printers
)
1360 cupsFilePuts(temp
, "BrowseAddress @LOCAL\n");
1365 "# Disable printer sharing and shared printers.\n");
1366 cupsFilePuts(temp
, "Browsing Off\n");
1370 else if (!strcasecmp(line
, "LogLevel") && debug_logging
>= 0)
1377 "# Show troubleshooting information in error_log.\n");
1378 cupsFilePuts(temp
, "LogLevel debug\n");
1382 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1383 cupsFilePuts(temp
, "LogLevel info\n");
1386 else if (!strcasecmp(line
, "<Policy"))
1388 in_default_policy
= !strcasecmp(value
, "default");
1391 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1394 else if (!strcasecmp(line
, "</Policy>"))
1397 if (!wrote_policy
&& in_default_policy
)
1401 if (!user_cancel_any
)
1402 cupsFilePuts(temp
, " # Only the owner or an administrator can "
1404 " <Limit Cancel-Job>\n"
1405 " Order deny,allow\n"
1412 in_default_policy
= 0;
1414 cupsFilePuts(temp
, "</Policy>\n");
1416 else if (!strcasecmp(line
, "<Location"))
1420 if (!strcmp(value
, "/admin"))
1421 in_admin_location
= 1;
1422 if (!strcmp(value
, "/admin/conf"))
1423 in_conf_location
= 1;
1424 else if (!strcmp(value
, "/"))
1425 in_root_location
= 1;
1427 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1429 else if (!strcasecmp(line
, "</Location>"))
1433 if (in_admin_location
&& remote_admin
>= 0)
1435 wrote_admin_location
= 1;
1438 cupsFilePuts(temp
, " # Allow remote administration...\n");
1439 else if (remote_admin
== 0)
1440 cupsFilePuts(temp
, " # Restrict access to the admin pages...\n");
1442 cupsFilePuts(temp
, " Order allow,deny\n");
1445 cupsFilePrintf(temp
, " Allow %s\n",
1446 remote_any
> 0 ? "all" : "@LOCAL");
1448 cupsFilePuts(temp
, " Allow localhost\n");
1450 else if (in_conf_location
&& remote_admin
>= 0)
1452 wrote_conf_location
= 1;
1455 cupsFilePuts(temp
, " # Allow remote access to the configuration "
1458 cupsFilePuts(temp
, " # Restrict access to the configuration "
1461 cupsFilePuts(temp
, " Order allow,deny\n");
1464 cupsFilePrintf(temp
, " Allow %s\n",
1465 remote_any
> 0 ? "all" : "@LOCAL");
1467 cupsFilePuts(temp
, " Allow localhost\n");
1469 else if (in_root_location
&& (remote_admin
>= 0 || share_printers
>= 0))
1471 wrote_root_location
= 1;
1473 if (remote_admin
> 0 && share_printers
> 0)
1474 cupsFilePuts(temp
, " # Allow shared printing and remote "
1475 "administration...\n");
1476 else if (remote_admin
> 0)
1477 cupsFilePuts(temp
, " # Allow remote administration...\n");
1478 else if (share_printers
> 0)
1479 cupsFilePuts(temp
, " # Allow shared printing...\n");
1481 cupsFilePuts(temp
, " # Restrict access to the server...\n");
1483 cupsFilePuts(temp
, " Order allow,deny\n");
1485 if (remote_admin
> 0 || share_printers
> 0)
1486 cupsFilePrintf(temp
, " Allow %s\n",
1487 remote_any
> 0 ? "all" : "@LOCAL");
1489 cupsFilePuts(temp
, " Allow localhost\n");
1492 in_admin_location
= 0;
1493 in_conf_location
= 0;
1494 in_root_location
= 0;
1496 cupsFilePuts(temp
, "</Location>\n");
1498 else if (!strcasecmp(line
, "<Limit") && in_default_policy
)
1501 * See if the policy limit is for the Cancel-Job operation...
1504 char *valptr
; /* Pointer into value */
1509 if (!strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1512 * Don't write anything for this limit section...
1519 cupsFilePrintf(temp
, " %s", line
);
1523 for (valptr
= value
; !isspace(*valptr
& 255) && *valptr
; valptr
++);
1528 if (!strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1531 * Write everything except for this definition...
1537 cupsFilePrintf(temp
, " %s", value
);
1539 for (value
= valptr
; isspace(*value
& 255); value
++);
1542 cupsFilePuts(temp
, ">\n");
1545 else if (!strcasecmp(line
, "</Limit>") && in_cancel_job
)
1549 if (in_cancel_job
== 1)
1550 cupsFilePuts(temp
, " </Limit>\n");
1554 if (!user_cancel_any
)
1555 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1557 " <Limit Cancel-Job>\n"
1558 " Order deny,allow\n"
1559 " Require user @OWNER @SYSTEM\n"
1564 else if ((((in_admin_location
|| in_conf_location
|| in_root_location
) &&
1565 remote_admin
>= 0) ||
1566 (in_root_location
&& share_printers
>= 0)) &&
1567 (!strcasecmp(line
, "Allow") || !strcasecmp(line
, "Deny") ||
1568 !strcasecmp(line
, "Order")))
1570 else if (in_cancel_job
== 2)
1572 else if (!strcasecmp(line
, "<Limit") && value
)
1573 cupsFilePrintf(temp
, " %s %s>\n", line
, value
);
1574 else if (line
[0] == '<')
1578 cupsFilePrintf(temp
, "%*s%s %s>\n", indent
, "", line
, value
);
1586 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1589 else if (!in_policy
&& !in_location
&&
1590 (val
= cupsGetOption(line
, num_settings
, settings
)) != NULL
)
1593 * Replace this directive's value with the new one...
1596 cupsd_num_settings
= cupsAddOption(line
, val
, cupsd_num_settings
,
1600 * Write the new value in its place, without indentation since we
1601 * only support setting root directives, not in sections...
1604 cupsFilePrintf(temp
, "%s %s\n", line
, val
);
1608 if (!in_policy
&& !in_location
)
1611 * Record the non-policy, non-location directives that we find
1612 * in the server settings, since we cache this info and record it
1613 * in _cupsAdminGetServerSettings()...
1616 cupsd_num_settings
= cupsAddOption(line
, value
, cupsd_num_settings
,
1620 cupsFilePrintf(temp
, "%*s%s %s\n", indent
, "", line
, value
);
1623 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1627 * Write any missing info...
1630 if (!wrote_browsing
&& (remote_printers
>= 0 || share_printers
>= 0))
1632 if (remote_printers
> 0 || share_printers
> 0)
1634 if (remote_printers
> 0 && share_printers
> 0)
1635 cupsFilePuts(temp
, "# Enable printer sharing and shared printers.\n");
1636 else if (remote_printers
> 0)
1637 cupsFilePuts(temp
, "# Show shared printers on the local network.\n");
1639 cupsFilePuts(temp
, "# Share local printers on the local network.\n");
1641 cupsFilePuts(temp
, "Browsing On\n");
1642 cupsFilePuts(temp
, "BrowseOrder allow,deny\n");
1644 if (remote_printers
> 0)
1645 cupsFilePuts(temp
, "BrowseAllow all\n");
1647 if (share_printers
> 0)
1648 cupsFilePuts(temp
, "BrowseAddress @LOCAL\n");
1652 cupsFilePuts(temp
, "# Disable printer sharing and shared printers.\n");
1653 cupsFilePuts(temp
, "Browsing Off\n");
1657 if (!wrote_loglevel
&& debug_logging
>= 0)
1661 cupsFilePuts(temp
, "# Show troubleshooting information in error_log.\n");
1662 cupsFilePuts(temp
, "LogLevel debug\n");
1666 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1667 cupsFilePuts(temp
, "LogLevel info\n");
1671 if (!wrote_port_listen
&& (share_printers
>= 0 || remote_admin
>= 0))
1673 if (share_printers
> 0 || remote_admin
> 0)
1675 cupsFilePuts(temp
, "# Allow remote access\n");
1676 cupsFilePrintf(temp
, "Port %d\n", ippPort());
1681 "# Only listen for connections from the local machine.\n");
1682 cupsFilePrintf(temp
, "Listen localhost:%d\n", ippPort());
1685 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1686 if (!access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1687 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1688 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1691 if (!wrote_root_location
&& (remote_admin
>= 0 || share_printers
>= 0))
1693 if (remote_admin
> 0 && share_printers
> 0)
1695 "# Allow shared printing and remote administration...\n");
1696 else if (remote_admin
> 0)
1697 cupsFilePuts(temp
, "# Allow remote administration...\n");
1698 else if (share_printers
> 0)
1699 cupsFilePuts(temp
, "# Allow shared printing...\n");
1701 cupsFilePuts(temp
, "# Restrict access to the server...\n");
1703 cupsFilePuts(temp
, "<Location />\n"
1704 " Order allow,deny\n");
1706 if (remote_admin
> 0 || share_printers
> 0)
1707 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1709 cupsFilePuts(temp
, " Allow localhost\n");
1711 cupsFilePuts(temp
, "</Location>\n");
1714 if (!wrote_admin_location
&& remote_admin
>= 0)
1717 cupsFilePuts(temp
, "# Allow remote administration...\n");
1719 cupsFilePuts(temp
, "# Restrict access to the admin pages...\n");
1721 cupsFilePuts(temp
, "<Location /admin>\n"
1722 " Order allow,deny\n");
1725 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1727 cupsFilePuts(temp
, " Allow localhost\n");
1729 cupsFilePuts(temp
, "</Location>\n");
1732 if (!wrote_conf_location
&& remote_admin
>= 0)
1736 "# Allow remote access to the configuration files...\n");
1738 cupsFilePuts(temp
, "# Restrict access to the configuration files...\n");
1740 cupsFilePuts(temp
, "<Location /admin/conf>\n"
1742 " Require user @SYSTEM\n"
1743 " Order allow,deny\n");
1746 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1748 cupsFilePuts(temp
, " Allow localhost\n");
1750 cupsFilePuts(temp
, "</Location>\n");
1753 if (!wrote_policy
&& user_cancel_any
>= 0)
1755 cupsFilePuts(temp
, "<Policy default>\n"
1756 " # Job-related operations must be done by the owner "
1757 "or an adminstrator...\n"
1758 " <Limit Send-Document Send-URI Hold-Job Release-Job "
1759 "Restart-Job Purge-Jobs Set-Job-Attributes "
1760 "Create-Job-Subscription Renew-Subscription "
1761 "Cancel-Subscription Get-Notifications Reprocess-Job "
1762 "Cancel-Current-Job Suspend-Current-Job Resume-Job "
1764 " Require user @OWNER @SYSTEM\n"
1765 " Order deny,allow\n"
1767 " # All administration operations require an "
1768 "adminstrator to authenticate...\n"
1769 " <Limit Pause-Printer Resume-Printer "
1770 "Set-Printer-Attributes Enable-Printer "
1771 "Disable-Printer Pause-Printer-After-Current-Job "
1772 "Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer "
1773 "Activate-Printer Restart-Printer Shutdown-Printer "
1774 "Startup-Printer Promote-Job Schedule-Job-After "
1775 "CUPS-Add-Printer CUPS-Delete-Printer "
1776 "CUPS-Add-Class CUPS-Delete-Class "
1777 "CUPS-Accept-Jobs CUPS-Reject-Jobs "
1778 "CUPS-Set-Default CUPS-Add-Device CUPS-Delete-Device>\n"
1780 " Require user @SYSTEM\n"
1781 " Order deny,allow\n"
1784 if (!user_cancel_any
)
1785 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1787 " <Limit Cancel-Job>\n"
1788 " Require user @OWNER @SYSTEM\n"
1789 " Order deny,allow\n"
1792 cupsFilePuts(temp
, " <Limit All>\n"
1793 " Order deny,allow\n"
1798 for (i
= num_settings
, setting
= settings
; i
> 0; i
--, setting
++)
1799 if (setting
->name
[0] != '_' &&
1800 !cupsGetOption(setting
->name
, cupsd_num_settings
, cupsd_settings
))
1803 * Add this directive to the list of directives we have written...
1806 cupsd_num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1807 cupsd_num_settings
, &cupsd_settings
);
1810 * Write the new value, without indentation since we only support
1811 * setting root directives, not in sections...
1814 cupsFilePrintf(temp
, "%s %s\n", setting
->name
, setting
->value
);
1817 cupsFileClose(cupsd
);
1818 cupsFileClose(temp
);
1821 * Upload the configuration file to the server...
1824 status
= cupsPutFile(http
, "/admin/conf/cupsd.conf", tempfile
);
1826 if (status
== HTTP_CREATED
)
1829 * Updated OK, add the basic settings...
1832 if (debug_logging
>= 0)
1833 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1834 debug_logging
? "1" : "0",
1835 cupsd_num_settings
, &cupsd_settings
);
1837 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1838 old_debug_logging
? "1" : "0",
1839 cupsd_num_settings
, &cupsd_settings
);
1841 if (remote_admin
>= 0)
1842 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1843 remote_admin
? "1" : "0",
1844 cupsd_num_settings
, &cupsd_settings
);
1846 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1847 old_remote_admin
? "1" : "0",
1848 cupsd_num_settings
, &cupsd_settings
);
1850 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
1851 remote_any
? "1" : "0",
1852 cupsd_num_settings
, &cupsd_settings
);
1854 if (remote_printers
>= 0)
1855 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
1856 remote_printers
? "1" : "0",
1857 cupsd_num_settings
, &cupsd_settings
);
1859 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
1860 old_remote_printers
? "1" : "0",
1861 cupsd_num_settings
, &cupsd_settings
);
1863 if (share_printers
>= 0)
1864 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1865 share_printers
? "1" : "0",
1866 cupsd_num_settings
, &cupsd_settings
);
1868 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1869 old_share_printers
? "1" : "0",
1870 cupsd_num_settings
, &cupsd_settings
);
1872 if (user_cancel_any
>= 0)
1873 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1874 user_cancel_any
? "1" : "0",
1875 cupsd_num_settings
, &cupsd_settings
);
1877 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1878 old_user_cancel_any
? "1" : "0",
1879 cupsd_num_settings
, &cupsd_settings
);
1882 * Save the new values...
1885 invalidate_cupsd_cache(cg
);
1887 cg
->cupsd_num_settings
= cupsd_num_settings
;
1888 cg
->cupsd_settings
= cupsd_settings
;
1889 cg
->cupsd_update
= time(NULL
);
1891 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
1894 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
1897 * Remote our temp files and return...
1905 return (status
== HTTP_CREATED
);
1910 * 'do_samba_command()' - Do a SAMBA command.
1913 static int /* O - Status of command */
1914 do_samba_command(const char *command
, /* I - Command to run */
1915 const char *address
, /* I - Address for command */
1916 const char *subcmd
, /* I - Sub-command */
1917 const char *authfile
, /* I - Samba authentication file */
1918 FILE *logfile
) /* I - Optional log file */
1921 return (1); /* Always fail on Windows... */
1924 int status
; /* Status of command */
1925 int pid
; /* Process ID of child */
1929 _cupsLangPrintf(logfile
,
1930 _("Running command: %s %s -N -A %s -c \'%s\'\n"),
1931 command
, address
, authfile
, subcmd
);
1933 if ((pid
= fork()) == 0)
1936 * Child goes here, redirect stdin/out/err and execute the command...
1940 open("/dev/null", O_RDONLY
);
1945 dup(fileno(logfile
));
1947 open("/dev/null", O_WRONLY
);
1952 execlp(command
, command
, address
, "-N", "-A", authfile
, "-c", subcmd
,
1961 _cupsLangPrintf(logfile
, _("Unable to run \"%s\": %s\n"),
1962 command
, strerror(errno
));
1967 * Wait for the process to complete...
1970 while (wait(&status
) != pid
);
1974 _cupsLangPuts(logfile
, "\n");
1976 DEBUG_printf(("status=%d\n", status
));
1978 if (WIFEXITED(status
))
1979 return (WEXITSTATUS(status
));
1981 return (-WTERMSIG(status
));
1987 * 'get_cupsd_conf()' - Get the current cupsd.conf file.
1990 static http_status_t
/* O - Status of request */
1992 http_t
*http
, /* I - Connection to server */
1993 _cups_globals_t
*cg
, /* I - Global data */
1994 time_t last_update
, /* I - Last update time for file */
1995 char *name
, /* I - Filename buffer */
1996 int namesize
, /* I - Size of filename buffer */
1997 int *remote
) /* O - Remote file? */
1999 int fd
; /* Temporary file descriptor */
2001 struct stat info
; /* cupsd.conf file information */
2003 http_status_t status
; /* Status of getting cupsd.conf */
2004 char host
[HTTP_MAX_HOST
]; /* Hostname for connection */
2008 * See if we already have the data we need...
2011 httpGetHostname(http
, host
, sizeof(host
));
2013 if (strcasecmp(cg
->cupsd_hostname
, host
))
2014 invalidate_cupsd_cache(cg
);
2016 snprintf(name
, namesize
, "%s/cupsd.conf", cg
->cups_serverroot
);
2020 if (!strcasecmp(host
, "localhost") && !access(name
, R_OK
))
2023 * Read the local file rather than using HTTP...
2026 if (stat(name
, &info
))
2028 char message
[1024]; /* Message string */
2031 snprintf(message
, sizeof(message
),
2032 _cupsLangString(cupsLangDefault(), _("stat of %s failed: %s")),
2033 name
, strerror(errno
));
2034 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
2038 return (HTTP_SERVER_ERROR
);
2040 else if (last_update
&& info
.st_mtime
<= last_update
)
2041 status
= HTTP_NOT_MODIFIED
;
2049 * Read cupsd.conf via a HTTP GET request...
2052 if ((fd
= cupsTempFd(name
, namesize
)) < 0)
2056 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
2058 invalidate_cupsd_cache(cg
);
2060 return (HTTP_SERVER_ERROR
);
2065 httpClearFields(http
);
2068 httpSetField(http
, HTTP_FIELD_IF_MODIFIED_SINCE
,
2069 httpGetDateString(last_update
));
2071 status
= cupsGetFd(http
, "/admin/conf/cupsd.conf", fd
);
2075 if (status
!= HTTP_OK
)
2087 * 'invalidate_cupsd_cache()' - Invalidate the cached cupsd.conf settings.
2091 invalidate_cupsd_cache(
2092 _cups_globals_t
*cg
) /* I - Global data */
2094 cupsFreeOptions(cg
->cupsd_num_settings
, cg
->cupsd_settings
);
2096 cg
->cupsd_hostname
[0] = '\0';
2097 cg
->cupsd_update
= 0;
2098 cg
->cupsd_num_settings
= 0;
2099 cg
->cupsd_settings
= NULL
;
2104 * 'write_option()' - Write a CUPS option to a PPD file.
2108 write_option(cups_file_t
*dstfp
, /* I - PPD file */
2109 int order
, /* I - Order dependency */
2110 const char *name
, /* I - Option name */
2111 const char *text
, /* I - Option text */
2112 const char *attrname
, /* I - Attribute name */
2113 ipp_attribute_t
*suppattr
, /* I - IPP -supported attribute */
2114 ipp_attribute_t
*defattr
, /* I - IPP -default attribute */
2115 int defval
, /* I - Default value number */
2116 int valcount
) /* I - Number of values */
2118 int i
; /* Looping var */
2121 cupsFilePrintf(dstfp
, "*JCLOpenUI *%s/%s: PickOne\n"
2122 "*OrderDependency: %d JCLSetup *%s\n",
2123 name
, text
, order
, name
);
2125 if (defattr
->value_tag
== IPP_TAG_INTEGER
)
2128 * Do numeric options with a range or list...
2131 cupsFilePrintf(dstfp
, "*Default%s: %d\n", name
,
2132 defattr
->values
[defval
].integer
);
2134 if (suppattr
->value_tag
== IPP_TAG_RANGE
)
2137 * List each number in the range...
2140 for (i
= suppattr
->values
[0].range
.lower
;
2141 i
<= suppattr
->values
[0].range
.upper
;
2144 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, i
);
2147 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n",
2149 else if (defval
== 0)
2150 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
, i
);
2151 else if (defval
< (valcount
- 1))
2152 cupsFilePrintf(dstfp
, ",%d\"\n", i
);
2154 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", i
);
2160 * List explicit numbers...
2163 for (i
= 0; i
< suppattr
->num_values
; i
++)
2165 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, suppattr
->values
[i
].integer
);
2168 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n", attrname
,
2169 suppattr
->values
[i
].integer
);
2170 else if (defval
== 0)
2171 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
,
2172 suppattr
->values
[i
].integer
);
2173 else if (defval
< (valcount
- 1))
2174 cupsFilePrintf(dstfp
, ",%d\"\n", suppattr
->values
[i
].integer
);
2176 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", suppattr
->values
[i
].integer
);
2183 * Do text options with a list...
2186 cupsFilePrintf(dstfp
, "*Default%s: %s\n", name
,
2187 defattr
->values
[defval
].string
.text
);
2189 for (i
= 0; i
< suppattr
->num_values
; i
++)
2191 cupsFilePrintf(dstfp
, "*%s %s: \"", name
,
2192 suppattr
->values
[i
].string
.text
);
2195 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\n\"\n*End\n", attrname
,
2196 suppattr
->values
[i
].string
.text
);
2197 else if (defval
== 0)
2198 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\"\n", attrname
,
2199 suppattr
->values
[i
].string
.text
);
2200 else if (defval
< (valcount
- 1))
2201 cupsFilePrintf(dstfp
, ",%s\"\n", suppattr
->values
[i
].string
.text
);
2203 cupsFilePrintf(dstfp
, ",%s\n\"\n*End\n",
2204 suppattr
->values
[i
].string
.text
);
2208 cupsFilePrintf(dstfp
, "*JCLCloseUI: *%s\n\n", name
);
2213 * End of "$Id: adminutil.c 6649 2007-07-11 21:46:42Z mike $".