2 * "$Id: adminutil.c 6378 2007-03-21 07:18:18Z mike $"
4 * Administration utility API definitions for the Common UNIX Printing
7 * Copyright 2001-2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Easy Software Products and are protected by Federal
11 * copyright law. Distribution and use rights are outlined in the file
12 * "LICENSE.txt" which should have been included with this file. If this
13 * file is missing or damaged please contact Easy Software Products
16 * Attn: CUPS Licensing Information
17 * Easy Software Products
18 * 44141 Airport View Drive, Suite 204
19 * Hollywood, Maryland 20636 USA
21 * Voice: (301) 373-9600
22 * EMail: cups-info@cups.org
23 * WWW: http://www.cups.org
25 * This file is subject to the Apple OS-Developed Software exception.
29 * cupsAdminCreateWindowsPPD() - Create the Windows PPD file for a printer.
30 * cupsAdminExportSamba() - Export a printer to Samba.
31 * cupsAdminGetServerSettings() - Get settings from the server.
32 * _cupsAdminGetServerSettings() - Get settings from the server (private).
33 * cupsAdminSetServerSettings() - Set settings on the server.
34 * _cupsAdminSetServerSettings() - Set settings on the server (private).
35 * do_samba_command() - Do a SAMBA command.
36 * get_cupsd_conf() - Get the current cupsd.conf file.
37 * invalidate_cupsd_cache() - Invalidate the cached cupsd.conf settings.
38 * write_option() - Write a CUPS option to a PPD file.
42 * Include necessary headers...
45 #include "adminutil.h"
54 # include <sys/wait.h>
62 extern int _cupsAdminGetServerSettings(http_t
*http
,
64 cups_option_t
**settings
);
65 extern int _cupsAdminSetServerSettings(http_t
*http
,
67 cups_option_t
*settings
);
68 static int do_samba_command(const char *command
,
70 const char *subcommand
,
73 static http_status_t
get_cupsd_conf(http_t
*http
, _cups_globals_t
*cg
,
74 time_t last_update
, char *name
,
75 int namelen
, int *remote
);
76 static void invalidate_cupsd_cache(_cups_globals_t
*cg
);
77 static void write_option(cups_file_t
*dstfp
, int order
,
78 const char *name
, const char *text
,
80 ipp_attribute_t
*suppattr
,
81 ipp_attribute_t
*defattr
, int defval
,
86 * 'cupsAdminCreateWindowsPPD()' - Create the Windows PPD file for a printer.
89 char * /* O - PPD file or NULL */
90 cupsAdminCreateWindowsPPD(
91 http_t
*http
, /* I - Connection to server */
92 const char *dest
, /* I - Printer or class */
93 char *buffer
, /* I - Filename buffer */
94 int bufsize
) /* I - Size of filename buffer */
96 const char *src
; /* Source PPD filename */
97 cups_file_t
*srcfp
, /* Source PPD file */
98 *dstfp
; /* Destination PPD file */
99 ipp_t
*request
, /* IPP request */
100 *response
; /* IPP response */
101 ipp_attribute_t
*suppattr
, /* IPP -supported attribute */
102 *defattr
; /* IPP -default attribute */
103 cups_lang_t
*language
; /* Current language */
104 char line
[256], /* Line from PPD file */
105 junk
[256], /* Extra junk to throw away */
106 *ptr
, /* Pointer into line */
107 uri
[1024], /* Printer URI */
108 option
[41], /* Option */
109 choice
[41]; /* Choice */
110 int jcloption
, /* In a JCL option? */
111 linenum
; /* Current line number */
112 time_t curtime
; /* Current time */
113 struct tm
*curdate
; /* Current date */
114 static const char * const pattrs
[] = /* Printer attributes we want */
116 "job-hold-until-supported",
117 "job-hold-until-default",
118 "job-sheets-supported",
119 "job-sheets-default",
120 "job-priority-supported",
121 "job-priority-default"
126 * Range check the input...
132 if (!http
|| !dest
|| !buffer
|| bufsize
< 2)
136 * Get the PPD file...
139 if ((src
= cupsGetPPD2(http
, dest
)) == NULL
)
143 * Get the supported banner pages, etc. for the printer...
146 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
148 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
149 "localhost", 0, "/printers/%s", dest
);
150 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
151 "printer-uri", NULL
, uri
);
153 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
154 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
158 * Do the request and get back a response...
161 response
= cupsDoRequest(http
, request
, "/");
162 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
169 * Open the original PPD file...
172 if ((srcfp
= cupsFileOpen(src
, "rb")) == NULL
)
176 * Create a temporary output file using the destination buffer...
179 if ((dstfp
= cupsTempFile2(buffer
, bufsize
)) == NULL
)
181 cupsFileClose(srcfp
);
189 * Write a new header explaining that this isn't the original PPD...
192 cupsFilePuts(dstfp
, "*PPD-Adobe: \"4.3\"\n");
194 curtime
= time(NULL
);
195 curdate
= gmtime(&curtime
);
197 cupsFilePrintf(dstfp
, "*%% Modified on %04d%02d%02d%02d%02d%02d+0000 "
198 "for CUPS Windows Driver\n",
199 curdate
->tm_year
+ 1900, curdate
->tm_mon
+ 1, curdate
->tm_mday
,
200 curdate
->tm_hour
, curdate
->tm_min
, curdate
->tm_sec
);
203 * Read the existing PPD file, converting all PJL commands to CUPS
204 * job ticket comments...
209 language
= cupsLangDefault();
211 while (cupsFileGets(srcfp
, line
, sizeof(line
)))
215 if (!strncmp(line
, "*PPD-Adobe:", 11))
218 * Already wrote the PPD header...
223 else if (!strncmp(line
, "*JCLBegin:", 10) ||
224 !strncmp(line
, "*JCLToPSInterpreter:", 20) ||
225 !strncmp(line
, "*JCLEnd:", 8) ||
226 !strncmp(line
, "*Protocols:", 11))
229 * Don't use existing JCL keywords; we'll create our own, below...
232 cupsFilePrintf(dstfp
, "*%% Commented out for CUPS Windows Driver...\n"
233 "*%%%s\n", line
+ 1);
236 else if (!strncmp(line
, "*JCLOpenUI", 10))
239 cupsFilePrintf(dstfp
, "%s\n", line
);
241 else if (!strncmp(line
, "*JCLCloseUI", 11))
244 cupsFilePrintf(dstfp
, "%s\n", line
);
246 else if (jcloption
&&
247 strncmp(line
, "*End", 4) &&
248 strncmp(line
, "*Default", 8) &&
249 strncmp(line
, "*OrderDependency", 16))
251 if ((ptr
= strchr(line
, ':')) == NULL
)
253 snprintf(line
, sizeof(line
),
254 _cupsLangString(language
, _("Missing value on line %d!")),
256 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
258 cupsFileClose(srcfp
);
259 cupsFileClose(dstfp
);
269 if ((ptr
= strchr(ptr
, '\"')) == NULL
)
271 snprintf(line
, sizeof(line
),
272 _cupsLangString(language
,
273 _("Missing double quote on line %d!")),
275 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
277 cupsFileClose(srcfp
);
278 cupsFileClose(dstfp
);
288 if (sscanf(line
, "*%40s%*[ \t]%40[^/]", option
, choice
) != 2)
290 snprintf(line
, sizeof(line
),
291 _cupsLangString(language
,
292 _("Bad option + choice on line %d!")),
294 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
296 cupsFileClose(srcfp
);
297 cupsFileClose(dstfp
);
307 if (strchr(ptr
+ 1, '\"') == NULL
)
313 while (cupsFileGets(srcfp
, junk
, sizeof(junk
)) != NULL
)
317 if (!strncmp(junk
, "*End", 4))
322 snprintf(ptr
+ 1, sizeof(line
) - (ptr
- line
+ 1),
323 "%%cupsJobTicket: %s=%s\n\"\n*End", option
, choice
);
325 cupsFilePrintf(dstfp
, "*%% Changed for CUPS Windows Driver...\n%s\n",
329 cupsFilePrintf(dstfp
, "%s\n", line
);
332 cupsFileClose(srcfp
);
337 snprintf(line
, sizeof(line
),
338 _cupsLangString(language
, _("Empty PPD file!")),
340 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
342 cupsFileClose(dstfp
);
351 * Now add the CUPS-specific attributes and options...
354 cupsFilePuts(dstfp
, "\n*% CUPS Job Ticket support and options...\n");
355 cupsFilePuts(dstfp
, "*Protocols: PJL\n");
356 cupsFilePuts(dstfp
, "*JCLBegin: \"%!PS-Adobe-3.0<0A>\"\n");
357 cupsFilePuts(dstfp
, "*JCLToPSInterpreter: \"\"\n");
358 cupsFilePuts(dstfp
, "*JCLEnd: \"\"\n");
360 cupsFilePuts(dstfp
, "\n*OpenGroup: CUPS/CUPS Options\n\n");
362 if ((defattr
= ippFindAttribute(response
, "job-hold-until-default",
363 IPP_TAG_ZERO
)) != NULL
&&
364 (suppattr
= ippFindAttribute(response
, "job-hold-until-supported",
365 IPP_TAG_ZERO
)) != NULL
)
366 write_option(dstfp
, 10, "cupsJobHoldUntil", "Hold Until", "job-hold-until",
367 suppattr
, defattr
, 0, 1);
369 if ((defattr
= ippFindAttribute(response
, "job-priority-default",
370 IPP_TAG_INTEGER
)) != NULL
&&
371 (suppattr
= ippFindAttribute(response
, "job-priority-supported",
372 IPP_TAG_RANGE
)) != NULL
)
373 write_option(dstfp
, 11, "cupsJobPriority", "Priority", "job-priority",
374 suppattr
, defattr
, 0, 1);
376 if ((defattr
= ippFindAttribute(response
, "job-sheets-default",
377 IPP_TAG_ZERO
)) != NULL
&&
378 (suppattr
= ippFindAttribute(response
, "job-sheets-supported",
379 IPP_TAG_ZERO
)) != NULL
)
381 write_option(dstfp
, 20, "cupsJobSheetsStart", "Start Banner",
382 "job-sheets", suppattr
, defattr
, 0, 2);
383 write_option(dstfp
, 21, "cupsJobSheetsEnd", "End Banner",
384 "job-sheets", suppattr
, defattr
, 1, 2);
387 cupsFilePuts(dstfp
, "*CloseGroup: CUPS\n");
388 cupsFileClose(dstfp
);
397 * 'cupsAdminExportSamba()' - Export a printer to Samba.
400 int /* O - 1 on success, 0 on failure */
401 cupsAdminExportSamba(
402 const char *dest
, /* I - Destination to export */
403 const char *ppd
, /* I - PPD file */
404 const char *samba_server
, /* I - Samba server */
405 const char *samba_user
, /* I - Samba username */
406 const char *samba_password
, /* I - Samba password */
407 FILE *logfile
) /* I - Log file, if any */
409 int status
; /* Status of Samba commands */
410 int have_drivers
; /* Have drivers? */
411 char file
[1024], /* File to test for */
412 authfile
[1024], /* Temporary authentication file */
413 address
[1024], /* Address for command */
414 subcmd
[1024], /* Sub-command */
415 message
[1024]; /* Error message */
416 cups_file_t
*fp
; /* Authentication file */
417 cups_lang_t
*language
; /* Current language */
418 _cups_globals_t
*cg
= _cupsGlobals();
423 * Range check input...
426 if (!dest
|| !ppd
|| !samba_server
|| !samba_user
|| !samba_password
)
428 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
433 * Create a temporary authentication file for Samba...
436 if ((fp
= cupsTempFile2(authfile
, sizeof(authfile
))) == NULL
)
438 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
442 cupsFilePrintf(fp
, "username = %s\n", samba_user
);
443 cupsFilePrintf(fp
, "password = %s\n", samba_password
);
447 * See which drivers are available; the new CUPS v6 and Adobe drivers
448 * depend on the Windows 2k PS driver, so copy that driver first:
459 language
= cupsLangDefault();
461 snprintf(file
, sizeof(file
), "%s/drivers/pscript5.dll", cg
->cups_datadir
);
462 if (!access(file
, 0))
467 * Windows 2k driver is installed; do the smbclient commands needed
468 * to copy the Win2k drivers over...
471 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
473 snprintf(subcmd
, sizeof(subcmd
),
475 "put %s W32X86/%s.ppd;"
476 "put %s/drivers/ps5ui.dll W32X86/ps5ui.dll;"
477 "put %s/drivers/pscript.hlp W32X86/pscript.hlp;"
478 "put %s/drivers/pscript.ntf W32X86/pscript.ntf;"
479 "put %s/drivers/pscript5.dll W32X86/pscript5.dll",
480 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
481 cg
->cups_datadir
, cg
->cups_datadir
);
483 if ((status
= do_samba_command("smbclient", address
, subcmd
,
484 authfile
, logfile
)) != 0)
486 snprintf(message
, sizeof(message
),
487 _cupsLangString(language
,
488 _("Unable to copy Windows 2000 printer "
489 "driver files (%d)!")), status
);
491 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
494 _cupsLangPrintf(logfile
, "%s\n", message
);
502 * See if we also have the CUPS driver files; if so, use them!
505 snprintf(file
, sizeof(file
), "%s/drivers/cupsps6.dll", cg
->cups_datadir
);
506 if (!access(file
, 0))
509 * Copy the CUPS driver files over...
512 snprintf(subcmd
, sizeof(subcmd
),
513 "put %s/drivers/cups6.ini W32X86/cups6.ini;"
514 "put %s/drivers/cupsps6.dll W32X86/cupsps6.dll;"
515 "put %s/drivers/cupsui6.dll W32X86/cupsui6.dll",
516 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
518 if ((status
= do_samba_command("smbclient", address
, subcmd
,
519 authfile
, logfile
)) != 0)
521 snprintf(message
, sizeof(message
),
522 _cupsLangString(language
,
523 _("Unable to copy CUPS printer driver "
524 "files (%d)!")), status
);
526 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
529 _cupsLangPrintf(logfile
, "%s\n", message
);
537 * Do the rpcclient command needed for the CUPS drivers...
540 snprintf(subcmd
, sizeof(subcmd
),
541 "adddriver \"Windows NT x86\" \"%s:"
542 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
543 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
544 "cups6.ini,cupsps6.dll,cupsui6.dll\"",
550 * Don't have the CUPS drivers, so just use the standard Windows
554 snprintf(subcmd
, sizeof(subcmd
),
555 "adddriver \"Windows NT x86\" \"%s:"
556 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
557 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
561 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
562 authfile
, logfile
)) != 0)
564 snprintf(message
, sizeof(message
),
565 _cupsLangString(language
,
566 _("Unable to install Windows 2000 printer "
567 "driver files (%d)!")), status
);
569 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
572 _cupsLangPrintf(logfile
, "%s\n", message
);
580 snprintf(file
, sizeof(file
), "%s/drivers/ADOBEPS4.DRV", cg
->cups_datadir
);
581 if (!access(file
, 0))
586 * Do the smbclient commands needed for the Adobe Win9x drivers...
589 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
591 snprintf(subcmd
, sizeof(subcmd
),
593 "put %s WIN40/%s.PPD;"
594 "put %s/drivers/ADFONTS.MFM WIN40/ADFONTS.MFM;"
595 "put %s/drivers/ADOBEPS4.DRV WIN40/ADOBEPS4.DRV;"
596 "put %s/drivers/ADOBEPS4.HLP WIN40/ADOBEPS4.HLP;"
597 "put %s/drivers/ICONLIB.DLL WIN40/ICONLIB.DLL;"
598 "put %s/drivers/PSMON.DLL WIN40/PSMON.DLL;",
599 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
600 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
602 if ((status
= do_samba_command("smbclient", address
, subcmd
,
603 authfile
, logfile
)) != 0)
605 snprintf(message
, sizeof(message
),
606 _cupsLangString(language
,
607 _("Unable to copy Windows 9x printer "
608 "driver files (%d)!")), status
);
610 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
613 _cupsLangPrintf(logfile
, "%s\n", message
);
621 * Do the rpcclient commands needed for the Adobe Win9x drivers...
624 snprintf(subcmd
, sizeof(subcmd
),
625 "adddriver \"Windows 4.0\" \"%s:ADOBEPS4.DRV:%s.PPD:NULL:"
626 "ADOBEPS4.HLP:PSMON.DLL:RAW:"
627 "ADOBEPS4.DRV,%s.PPD,ADOBEPS4.HLP,PSMON.DLL,ADFONTS.MFM,"
631 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
632 authfile
, logfile
)) != 0)
634 snprintf(message
, sizeof(message
),
635 _cupsLangString(language
,
636 _("Unable to install Windows 9x printer "
637 "driver files (%d)!")), status
);
639 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
642 _cupsLangPrintf(logfile
, "%s\n", message
);
650 if (logfile
&& !(have_drivers
& 1))
654 _cupsLangString(language
,
655 _("No Windows printer drivers are installed!")),
659 _cupsLangString(language
,
660 _("Warning, no Windows 2000 printer drivers "
664 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
665 _cupsLangPrintf(logfile
, "%s\n", message
);
668 if (have_drivers
== 0)
672 * Finally, associate the drivers we just added with the queue...
675 snprintf(subcmd
, sizeof(subcmd
), "setdriver %s %s", dest
, dest
);
677 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
678 authfile
, logfile
)) != 0)
680 snprintf(message
, sizeof(message
),
681 _cupsLangString(language
,
682 _("Unable to set Windows printer driver (%d)!")),
685 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
688 _cupsLangPrintf(logfile
, "%s\n", message
);
702 * 'cupsAdminGetServerSettings()' - Get settings from the server.
704 * The returned settings should be freed with cupsFreeOptions() when
705 * you are done with them.
710 int /* O - 1 on success, 0 on failure */
711 cupsAdminGetServerSettings(
712 http_t
*http
, /* I - Connection to server */
713 int *num_settings
, /* O - Number of settings */
714 cups_option_t
**settings
) /* O - Settings */
716 return (_cupsAdminGetServerSettings(http
, num_settings
, settings
));
721 * '_cupsAdminGetServerSettings()' - Get settings from the server.
723 * The returned settings should be freed with cupsFreeOptions() when
724 * you are done with them.
729 int /* O - 1 on success, 0 on failure */
730 _cupsAdminGetServerSettings(
731 http_t
*http
, /* I - Connection to server */
732 int *num_settings
, /* O - Number of settings */
733 cups_option_t
**settings
) /* O - Settings */
735 int i
; /* Looping var */
736 cups_file_t
*cupsd
; /* cupsd.conf file */
737 char cupsdconf
[1024]; /* cupsd.conf filename */
738 int remote
; /* Remote cupsd.conf file? */
739 http_status_t status
; /* Status of getting cupsd.conf */
740 char line
[1024], /* Line from cupsd.conf file */
741 *value
; /* Value on line */
742 cups_option_t
*setting
; /* Current setting */
743 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
747 * Range check input...
750 if (!http
|| !num_settings
|| !settings
)
752 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
767 * Get the cupsd.conf file...
770 if ((status
= get_cupsd_conf(http
, cg
, cg
->cupsd_update
, cupsdconf
,
771 sizeof(cupsdconf
), &remote
)) == HTTP_OK
)
773 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
775 char message
[1024]; /* Message string */
778 snprintf(message
, sizeof(message
),
779 _cupsLangString(cupsLangDefault(), _("open of %s failed: %s")),
780 cupsdconf
, strerror(errno
));
781 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
790 * Read the file, keeping track of what settings are enabled...
793 int remote_access
= 0, /* Remote access allowed? */
794 remote_admin
= 0, /* Remote administration allowed? */
795 remote_any
= 0, /* Remote access from anywhere allowed? */
796 browsing
= 1, /* Browsing enabled? */
797 browse_allow
= 1, /* Browse address set? */
798 browse_address
= 0, /* Browse address set? */
799 cancel_policy
= 1, /* Cancel-job policy set? */
800 debug_logging
= 0; /* LogLevel debug set? */
801 int linenum
= 0, /* Line number in file */
802 in_location
= 0, /* In a location section? */
803 in_policy
= 0, /* In a policy section? */
804 in_cancel_job
= 0, /* In a cancel-job section? */
805 in_admin_location
= 0; /* In the /admin location? */
808 invalidate_cupsd_cache(cg
);
810 cg
->cupsd_update
= time(NULL
);
811 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
813 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
818 if (!strcasecmp(line
, "Port") || !strcasecmp(line
, "Listen"))
820 char *port
; /* Pointer to port number, if any */
823 if ((port
= strrchr(value
, ':')) != NULL
)
825 else if (isdigit(*value
& 255))
828 * Listen on a port number implies remote access...
835 if (strcasecmp(value
, "localhost") && strcmp(value
, "127.0.0.1")
838 #endif /* AF_LOCAL */
840 && strcmp(value
, "::1")
841 #endif /* AF_INET6 */
845 else if (!strcasecmp(line
, "Browsing"))
847 browsing
= !strcasecmp(value
, "yes") || !strcasecmp(value
, "on") ||
848 !strcasecmp(value
, "true");
850 else if (!strcasecmp(line
, "BrowseAddress"))
854 else if (!strcasecmp(line
, "BrowseAllow"))
858 else if (!strcasecmp(line
, "BrowseOrder"))
860 browse_allow
= !strncasecmp(value
, "deny,", 5);
862 else if (!strcasecmp(line
, "LogLevel"))
864 debug_logging
= !strncasecmp(value
, "debug", 5);
866 else if (!strcasecmp(line
, "<Policy") && !strcasecmp(value
, "default"))
870 else if (!strcasecmp(line
, "</Policy>"))
874 else if (!strcasecmp(line
, "<Limit") && in_policy
)
877 * See if the policy limit is for the Cancel-Job operation...
880 char *valptr
; /* Pointer into value */
885 for (valptr
= value
; !isspace(*valptr
& 255) && *valptr
; valptr
++);
890 if (!strcasecmp(value
, "cancel-job") || !strcasecmp(value
, "all"))
896 for (value
= valptr
; isspace(*value
& 255); value
++);
899 else if (!strcasecmp(line
, "</Limit>"))
903 else if (!strcasecmp(line
, "Require") && in_cancel_job
)
907 else if (!strcasecmp(line
, "<Location"))
909 in_admin_location
= !strcasecmp(value
, "/admin");
912 else if (!strcasecmp(line
, "</Location>"))
914 in_admin_location
= 0;
917 else if (!strcasecmp(line
, "Allow") && in_admin_location
&&
918 strcasecmp(value
, "localhost") && strcasecmp(value
, "127.0.0.1")
921 #endif /* AF_LOCAL */
923 && strcmp(value
, "::1")
924 #endif /* AF_INET6 */
929 if (!strcasecmp(value
, "all"))
932 else if (line
[0] != '<' && !in_location
&& !in_policy
)
933 cg
->cupsd_num_settings
= cupsAddOption(line
, value
,
934 cg
->cupsd_num_settings
,
935 &(cg
->cupsd_settings
));
938 cupsFileClose(cupsd
);
940 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
941 debug_logging
? "1" : "0",
942 cg
->cupsd_num_settings
,
943 &(cg
->cupsd_settings
));
945 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
946 (remote_access
&& remote_admin
) ?
948 cg
->cupsd_num_settings
,
949 &(cg
->cupsd_settings
));
951 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
952 remote_any
? "1" : "0",
953 cg
->cupsd_num_settings
,
954 &(cg
->cupsd_settings
));
956 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
957 (browsing
&& browse_allow
) ?
959 cg
->cupsd_num_settings
,
960 &(cg
->cupsd_settings
));
962 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
963 (remote_access
&& browsing
&&
964 browse_address
) ? "1" : "0",
965 cg
->cupsd_num_settings
,
966 &(cg
->cupsd_settings
));
968 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
969 cancel_policy
? "1" : "0",
970 cg
->cupsd_num_settings
,
971 &(cg
->cupsd_settings
));
973 else if (status
!= HTTP_NOT_MODIFIED
)
974 invalidate_cupsd_cache(cg
);
977 * Remove any temporary files and copy the settings array...
983 for (i
= cg
->cupsd_num_settings
, setting
= cg
->cupsd_settings
;
986 *num_settings
= cupsAddOption(setting
->name
, setting
->value
,
987 *num_settings
, settings
);
989 return (cg
->cupsd_num_settings
> 0);
994 * 'cupsAdminSetServerSettings()' - Set settings on the server.
999 int /* O - 1 on success, 0 on failure */
1000 cupsAdminSetServerSettings(
1001 http_t
*http
, /* I - Connection to server */
1002 int num_settings
, /* I - Number of settings */
1003 cups_option_t
*settings
) /* I - Settings */
1005 return (_cupsAdminSetServerSettings(http
, num_settings
, settings
));
1010 * '_cupsAdminSetServerSettings()' - Set settings on the server.
1015 int /* O - 1 on success, 0 on failure */
1016 _cupsAdminSetServerSettings(
1017 http_t
*http
, /* I - Connection to server */
1018 int num_settings
, /* I - Number of settings */
1019 cups_option_t
*settings
) /* I - Settings */
1021 int i
; /* Looping var */
1022 http_status_t status
; /* GET/PUT status */
1023 const char *server_port_env
; /* SERVER_PORT env var */
1024 int server_port
; /* IPP port for server */
1025 cups_file_t
*cupsd
; /* cupsd.conf file */
1026 char cupsdconf
[1024]; /* cupsd.conf filename */
1027 int remote
; /* Remote cupsd.conf file? */
1028 char tempfile
[1024]; /* Temporary new cupsd.conf */
1029 cups_file_t
*temp
; /* Temporary file */
1030 char line
[1024], /* Line from cupsd.conf file */
1031 *value
; /* Value on line */
1032 int linenum
, /* Line number in file */
1033 in_location
, /* In a location section? */
1034 in_policy
, /* In a policy section? */
1035 in_default_policy
, /* In the default policy section? */
1036 in_cancel_job
, /* In a cancel-job section? */
1037 in_admin_location
, /* In the /admin location? */
1038 in_conf_location
, /* In the /admin/conf location? */
1039 in_root_location
; /* In the / location? */
1040 const char *val
; /* Setting value */
1041 int remote_printers
, /* Show remote printers */
1042 share_printers
, /* Share local printers */
1043 remote_admin
, /* Remote administration allowed? */
1044 remote_any
, /* Remote access from anywhere? */
1045 user_cancel_any
, /* Cancel-job policy set? */
1046 debug_logging
; /* LogLevel debug set? */
1047 int wrote_port_listen
, /* Wrote the port/listen lines? */
1048 wrote_browsing
, /* Wrote the browsing lines? */
1049 wrote_policy
, /* Wrote the policy? */
1050 wrote_loglevel
, /* Wrote the LogLevel line? */
1051 wrote_admin_location
, /* Wrote the /admin location? */
1052 wrote_conf_location
, /* Wrote the /admin/conf location? */
1053 wrote_root_location
; /* Wrote the / location? */
1054 int indent
; /* Indentation */
1055 int cupsd_num_settings
; /* New number of settings */
1056 int old_remote_printers
, /* Show remote printers */
1057 old_share_printers
, /* Share local printers */
1058 old_remote_admin
, /* Remote administration allowed? */
1059 old_user_cancel_any
, /* Cancel-job policy set? */
1060 old_debug_logging
; /* LogLevel debug set? */
1061 cups_option_t
*cupsd_settings
, /* New settings */
1062 *setting
; /* Current setting */
1063 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1067 * Range check input...
1070 if (!http
|| !num_settings
|| !settings
)
1072 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
1078 * Get the cupsd.conf file...
1081 if ((status
= get_cupsd_conf(http
, cg
, 0, cupsdconf
, sizeof(cupsdconf
),
1082 &remote
)) == HTTP_OK
)
1084 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
1086 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
1094 * Get current settings...
1097 if (!_cupsAdminGetServerSettings(http
, &cupsd_num_settings
,
1101 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, cupsd_num_settings
,
1102 cupsd_settings
)) != NULL
)
1103 old_debug_logging
= atoi(val
);
1105 old_debug_logging
= 0;
1107 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, cupsd_num_settings
,
1108 cupsd_settings
)) != NULL
)
1109 old_remote_admin
= atoi(val
);
1111 old_remote_admin
= 0;
1113 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, cupsd_num_settings
,
1114 cupsd_settings
)) != NULL
)
1115 remote_any
= atoi(val
);
1119 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
, cupsd_num_settings
,
1120 cupsd_settings
)) != NULL
)
1121 old_remote_printers
= atoi(val
);
1123 old_remote_printers
= 1;
1125 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, cupsd_num_settings
,
1126 cupsd_settings
)) != NULL
)
1127 old_share_printers
= atoi(val
);
1129 old_share_printers
= 0;
1131 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, cupsd_num_settings
,
1132 cupsd_settings
)) != NULL
)
1133 old_user_cancel_any
= atoi(val
);
1135 old_user_cancel_any
= 0;
1137 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
1140 * Get basic settings...
1143 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, num_settings
,
1146 debug_logging
= atoi(val
);
1148 if (debug_logging
== old_debug_logging
)
1151 * No change to this setting...
1160 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, num_settings
,
1162 remote_any
= atoi(val
);
1164 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, num_settings
,
1167 remote_admin
= atoi(val
);
1169 if (remote_admin
== old_remote_admin
&& remote_any
< 0)
1172 * No change to this setting...
1181 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
, num_settings
,
1184 remote_printers
= atoi(val
);
1186 if (remote_printers
== old_remote_printers
)
1189 * No change to this setting...
1192 remote_printers
= -1;
1196 remote_printers
= -1;
1198 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, num_settings
,
1201 share_printers
= atoi(val
);
1203 if (share_printers
== old_share_printers
&& remote_any
< 0)
1206 * No change to this setting...
1209 share_printers
= -1;
1213 share_printers
= -1;
1215 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, num_settings
,
1218 user_cancel_any
= atoi(val
);
1220 if (user_cancel_any
== old_user_cancel_any
)
1223 * No change to this setting...
1226 user_cancel_any
= -1;
1230 user_cancel_any
= -1;
1233 * Create a temporary file for the new cupsd.conf file...
1236 if ((temp
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1238 cupsFileClose(cupsd
);
1243 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
1248 * Copy the old file to the new, making changes along the way...
1251 cupsd_num_settings
= 0;
1252 in_admin_location
= 0;
1254 in_conf_location
= 0;
1255 in_default_policy
= 0;
1258 in_root_location
= 0;
1260 wrote_admin_location
= 0;
1262 wrote_conf_location
= 0;
1265 wrote_port_listen
= 0;
1266 wrote_root_location
= 0;
1269 if ((server_port_env
= getenv("SERVER_PORT")) != NULL
)
1271 if ((server_port
= atoi(server_port_env
)) <= 0)
1272 server_port
= ippPort();
1275 server_port
= ippPort();
1277 if (server_port
<= 0)
1278 server_port
= IPP_PORT
;
1280 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
1282 if ((!strcasecmp(line
, "Port") || !strcasecmp(line
, "Listen")) &&
1283 (share_printers
>= 0 || remote_admin
>= 0))
1285 if (!wrote_port_listen
)
1287 wrote_port_listen
= 1;
1289 if (share_printers
> 0 || remote_admin
> 0)
1291 cupsFilePuts(temp
, "# Allow remote access\n");
1292 cupsFilePrintf(temp
, "Port %d\n", server_port
);
1296 cupsFilePuts(temp
, "# Only listen for connections from the local "
1298 cupsFilePrintf(temp
, "Listen localhost:%d\n", server_port
);
1301 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1302 if ((!value
|| strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)) &&
1303 !access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1304 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1305 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1307 else if (value
&& value
[0] == '/'
1308 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1309 && strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)
1310 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1312 cupsFilePrintf(temp
, "Listen %s\n", value
);
1314 else if ((!strcasecmp(line
, "Browsing") ||
1315 !strcasecmp(line
, "BrowseAddress") ||
1316 !strcasecmp(line
, "BrowseAllow") ||
1317 !strcasecmp(line
, "BrowseDeny") ||
1318 !strcasecmp(line
, "BrowseOrder")) &&
1319 (remote_printers
>= 0 || share_printers
>= 0))
1321 if (!wrote_browsing
)
1325 if (remote_printers
> 0 || share_printers
> 0)
1327 if (remote_printers
> 0 && share_printers
> 0)
1329 "# Enable printer sharing and shared printers.\n");
1330 else if (remote_printers
> 0)
1332 "# Show shared printers on the local network.\n");
1335 "# Share local printers on the local network.\n");
1337 cupsFilePuts(temp
, "Browsing On\n");
1338 cupsFilePuts(temp
, "BrowseOrder allow,deny\n");
1340 if (remote_printers
> 0)
1341 cupsFilePuts(temp
, "BrowseAllow all\n");
1343 if (share_printers
> 0)
1344 cupsFilePuts(temp
, "BrowseAddress @LOCAL\n");
1349 "# Disable printer sharing and shared printers.\n");
1350 cupsFilePuts(temp
, "Browsing Off\n");
1354 else if (!strcasecmp(line
, "LogLevel") && debug_logging
>= 0)
1361 "# Show troubleshooting information in error_log.\n");
1362 cupsFilePuts(temp
, "LogLevel debug\n");
1366 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1367 cupsFilePuts(temp
, "LogLevel info\n");
1370 else if (!strcasecmp(line
, "<Policy"))
1372 in_default_policy
= !strcasecmp(value
, "default");
1375 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1378 else if (!strcasecmp(line
, "</Policy>"))
1381 if (!wrote_policy
&& in_default_policy
)
1385 if (!user_cancel_any
)
1386 cupsFilePuts(temp
, " # Only the owner or an administrator can "
1388 " <Limit Cancel-Job>\n"
1389 " Order deny,allow\n"
1396 in_default_policy
= 0;
1398 cupsFilePuts(temp
, "</Policy>\n");
1400 else if (!strcasecmp(line
, "<Location"))
1404 if (!strcmp(value
, "/admin"))
1405 in_admin_location
= 1;
1406 if (!strcmp(value
, "/admin/conf"))
1407 in_conf_location
= 1;
1408 else if (!strcmp(value
, "/"))
1409 in_root_location
= 1;
1411 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1413 else if (!strcasecmp(line
, "</Location>"))
1417 if (in_admin_location
&& remote_admin
>= 0)
1419 wrote_admin_location
= 1;
1422 cupsFilePuts(temp
, " # Allow remote administration...\n");
1423 else if (remote_admin
== 0)
1424 cupsFilePuts(temp
, " # Restrict access to the admin pages...\n");
1426 cupsFilePuts(temp
, " Order allow,deny\n");
1429 cupsFilePrintf(temp
, " Allow %s\n",
1430 remote_any
> 0 ? "all" : "@LOCAL");
1432 cupsFilePuts(temp
, " Allow localhost\n");
1434 else if (in_conf_location
&& remote_admin
>= 0)
1436 wrote_conf_location
= 1;
1439 cupsFilePuts(temp
, " # Allow remote access to the configuration "
1442 cupsFilePuts(temp
, " # Restrict access to the configuration "
1445 cupsFilePuts(temp
, " Order allow,deny\n");
1448 cupsFilePrintf(temp
, " Allow %s\n",
1449 remote_any
> 0 ? "all" : "@LOCAL");
1451 cupsFilePuts(temp
, " Allow localhost\n");
1453 else if (in_root_location
&& (remote_admin
>= 0 || share_printers
>= 0))
1455 wrote_root_location
= 1;
1457 if (remote_admin
> 0 && share_printers
> 0)
1458 cupsFilePuts(temp
, " # Allow shared printing and remote "
1459 "administration...\n");
1460 else if (remote_admin
> 0)
1461 cupsFilePuts(temp
, " # Allow remote administration...\n");
1462 else if (share_printers
> 0)
1463 cupsFilePuts(temp
, " # Allow shared printing...\n");
1465 cupsFilePuts(temp
, " # Restrict access to the server...\n");
1467 cupsFilePuts(temp
, " Order allow,deny\n");
1469 if (remote_admin
> 0 || share_printers
> 0)
1470 cupsFilePrintf(temp
, " Allow %s\n",
1471 remote_any
> 0 ? "all" : "@LOCAL");
1473 cupsFilePuts(temp
, " Allow localhost\n");
1476 in_admin_location
= 0;
1477 in_conf_location
= 0;
1478 in_root_location
= 0;
1480 cupsFilePuts(temp
, "</Location>\n");
1482 else if (!strcasecmp(line
, "<Limit") && in_default_policy
)
1485 * See if the policy limit is for the Cancel-Job operation...
1488 char *valptr
; /* Pointer into value */
1493 if (!strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1496 * Don't write anything for this limit section...
1503 cupsFilePrintf(temp
, " %s", line
);
1507 for (valptr
= value
; !isspace(*valptr
& 255) && *valptr
; valptr
++);
1512 if (!strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1515 * Write everything except for this definition...
1521 cupsFilePrintf(temp
, " %s", value
);
1523 for (value
= valptr
; isspace(*value
& 255); value
++);
1526 cupsFilePuts(temp
, ">\n");
1529 else if (!strcasecmp(line
, "</Limit>") && in_cancel_job
)
1533 if (in_cancel_job
== 1)
1534 cupsFilePuts(temp
, " </Limit>\n");
1538 if (!user_cancel_any
)
1539 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1541 " <Limit Cancel-Job>\n"
1542 " Order deny,allow\n"
1543 " Require user @OWNER @SYSTEM\n"
1548 else if ((((in_admin_location
|| in_conf_location
|| in_root_location
) &&
1549 remote_admin
>= 0) ||
1550 (in_root_location
&& share_printers
>= 0)) &&
1551 (!strcasecmp(line
, "Allow") || !strcasecmp(line
, "Deny") ||
1552 !strcasecmp(line
, "Order")))
1554 else if (in_cancel_job
== 2)
1556 else if (!strcasecmp(line
, "<Limit") && value
)
1557 cupsFilePrintf(temp
, " %s %s>\n", line
, value
);
1558 else if (line
[0] == '<')
1562 cupsFilePrintf(temp
, "%*s%s %s>\n", indent
, "", line
, value
);
1570 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1573 else if (!in_policy
&& !in_location
&&
1574 (val
= cupsGetOption(line
, num_settings
, settings
)) != NULL
)
1577 * Replace this directive's value with the new one...
1580 cupsd_num_settings
= cupsAddOption(line
, val
, cupsd_num_settings
,
1584 * Write the new value in its place, without indentation since we
1585 * only support setting root directives, not in sections...
1588 cupsFilePrintf(temp
, "%s %s\n", line
, val
);
1592 if (!in_policy
&& !in_location
)
1595 * Record the non-policy, non-location directives that we find
1596 * in the server settings, since we cache this info and record it
1597 * in _cupsAdminGetServerSettings()...
1600 cupsd_num_settings
= cupsAddOption(line
, value
, cupsd_num_settings
,
1604 cupsFilePrintf(temp
, "%*s%s %s\n", indent
, "", line
, value
);
1607 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1611 * Write any missing info...
1614 if (!wrote_browsing
&& (remote_printers
>= 0 || share_printers
>= 0))
1616 if (remote_printers
> 0 || share_printers
> 0)
1618 if (remote_printers
> 0 && share_printers
> 0)
1619 cupsFilePuts(temp
, "# Enable printer sharing and shared printers.\n");
1620 else if (remote_printers
> 0)
1621 cupsFilePuts(temp
, "# Show shared printers on the local network.\n");
1623 cupsFilePuts(temp
, "# Share local printers on the local network.\n");
1625 cupsFilePuts(temp
, "Browsing On\n");
1626 cupsFilePuts(temp
, "BrowseOrder allow,deny\n");
1628 if (remote_printers
> 0)
1629 cupsFilePuts(temp
, "BrowseAllow all\n");
1631 if (share_printers
> 0)
1632 cupsFilePuts(temp
, "BrowseAddress @LOCAL\n");
1636 cupsFilePuts(temp
, "# Disable printer sharing and shared printers.\n");
1637 cupsFilePuts(temp
, "Browsing Off\n");
1641 if (!wrote_loglevel
&& debug_logging
>= 0)
1645 cupsFilePuts(temp
, "# Show troubleshooting information in error_log.\n");
1646 cupsFilePuts(temp
, "LogLevel debug\n");
1650 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1651 cupsFilePuts(temp
, "LogLevel info\n");
1655 if (!wrote_port_listen
&& (share_printers
>= 0 || remote_admin
>= 0))
1657 if (share_printers
> 0 || remote_admin
> 0)
1659 cupsFilePuts(temp
, "# Allow remote access\n");
1660 cupsFilePrintf(temp
, "Port %d\n", ippPort());
1665 "# Only listen for connections from the local machine.\n");
1666 cupsFilePrintf(temp
, "Listen localhost:%d\n", ippPort());
1669 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1670 if (!access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1671 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1672 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1675 if (!wrote_root_location
&& (remote_admin
>= 0 || share_printers
>= 0))
1677 if (remote_admin
> 0 && share_printers
> 0)
1679 "# Allow shared printing and remote administration...\n");
1680 else if (remote_admin
> 0)
1681 cupsFilePuts(temp
, "# Allow remote administration...\n");
1682 else if (share_printers
> 0)
1683 cupsFilePuts(temp
, "# Allow shared printing...\n");
1685 cupsFilePuts(temp
, "# Restrict access to the server...\n");
1687 cupsFilePuts(temp
, "<Location />\n"
1688 " Order allow,deny\n");
1690 if (remote_admin
> 0 || share_printers
> 0)
1691 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1693 cupsFilePuts(temp
, " Allow localhost\n");
1695 cupsFilePuts(temp
, "</Location>\n");
1698 if (!wrote_admin_location
&& remote_admin
>= 0)
1701 cupsFilePuts(temp
, "# Allow remote administration...\n");
1703 cupsFilePuts(temp
, "# Restrict access to the admin pages...\n");
1705 cupsFilePuts(temp
, "<Location /admin>\n"
1706 " Order allow,deny\n");
1709 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1711 cupsFilePuts(temp
, " Allow localhost\n");
1713 cupsFilePuts(temp
, "</Location>\n");
1716 if (!wrote_conf_location
&& remote_admin
>= 0)
1720 "# Allow remote access to the configuration files...\n");
1722 cupsFilePuts(temp
, "# Restrict access to the configuration files...\n");
1724 cupsFilePuts(temp
, "<Location /admin/conf>\n"
1726 " Require user @SYSTEM\n"
1727 " Order allow,deny\n");
1730 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1732 cupsFilePuts(temp
, " Allow localhost\n");
1734 cupsFilePuts(temp
, "</Location>\n");
1737 if (!wrote_policy
&& user_cancel_any
>= 0)
1739 cupsFilePuts(temp
, "<Policy default>\n"
1740 " # Job-related operations must be done by the owner "
1741 "or an adminstrator...\n"
1742 " <Limit Send-Document Send-URI Hold-Job Release-Job "
1743 "Restart-Job Purge-Jobs Set-Job-Attributes "
1744 "Create-Job-Subscription Renew-Subscription "
1745 "Cancel-Subscription Get-Notifications Reprocess-Job "
1746 "Cancel-Current-Job Suspend-Current-Job Resume-Job "
1748 " Require user @OWNER @SYSTEM\n"
1749 " Order deny,allow\n"
1751 " # All administration operations require an "
1752 "adminstrator to authenticate...\n"
1753 " <Limit Pause-Printer Resume-Printer "
1754 "Set-Printer-Attributes Enable-Printer "
1755 "Disable-Printer Pause-Printer-After-Current-Job "
1756 "Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer "
1757 "Activate-Printer Restart-Printer Shutdown-Printer "
1758 "Startup-Printer Promote-Job Schedule-Job-After "
1759 "CUPS-Add-Printer CUPS-Delete-Printer "
1760 "CUPS-Add-Class CUPS-Delete-Class "
1761 "CUPS-Accept-Jobs CUPS-Reject-Jobs "
1762 "CUPS-Set-Default CUPS-Add-Device CUPS-Delete-Device>\n"
1764 " Require user @SYSTEM\n"
1765 " Order deny,allow\n"
1768 if (!user_cancel_any
)
1769 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1771 " <Limit Cancel-Job>\n"
1772 " Require user @OWNER @SYSTEM\n"
1773 " Order deny,allow\n"
1776 cupsFilePuts(temp
, " <Limit All>\n"
1777 " Order deny,allow\n"
1782 for (i
= num_settings
, setting
= settings
; i
> 0; i
--, setting
++)
1783 if (setting
->name
[0] != '_' &&
1784 !cupsGetOption(setting
->name
, cupsd_num_settings
, cupsd_settings
))
1787 * Add this directive to the list of directives we have written...
1790 cupsd_num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1791 cupsd_num_settings
, &cupsd_settings
);
1794 * Write the new value, without indentation since we only support
1795 * setting root directives, not in sections...
1798 cupsFilePrintf(temp
, "%s %s\n", setting
->name
, setting
->value
);
1801 cupsFileClose(cupsd
);
1802 cupsFileClose(temp
);
1805 * Upload the configuration file to the server...
1808 status
= cupsPutFile(http
, "/admin/conf/cupsd.conf", tempfile
);
1810 if (status
== HTTP_CREATED
)
1813 * Updated OK, add the basic settings...
1816 if (debug_logging
>= 0)
1817 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1818 debug_logging
? "1" : "0",
1819 cupsd_num_settings
, &cupsd_settings
);
1821 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1822 old_debug_logging
? "1" : "0",
1823 cupsd_num_settings
, &cupsd_settings
);
1825 if (remote_admin
>= 0)
1826 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1827 remote_admin
? "1" : "0",
1828 cupsd_num_settings
, &cupsd_settings
);
1830 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1831 old_remote_admin
? "1" : "0",
1832 cupsd_num_settings
, &cupsd_settings
);
1834 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
1835 remote_any
? "1" : "0",
1836 cupsd_num_settings
, &cupsd_settings
);
1838 if (remote_printers
>= 0)
1839 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
1840 remote_printers
? "1" : "0",
1841 cupsd_num_settings
, &cupsd_settings
);
1843 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
1844 old_remote_printers
? "1" : "0",
1845 cupsd_num_settings
, &cupsd_settings
);
1847 if (share_printers
>= 0)
1848 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1849 share_printers
? "1" : "0",
1850 cupsd_num_settings
, &cupsd_settings
);
1852 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1853 old_share_printers
? "1" : "0",
1854 cupsd_num_settings
, &cupsd_settings
);
1856 if (user_cancel_any
>= 0)
1857 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1858 user_cancel_any
? "1" : "0",
1859 cupsd_num_settings
, &cupsd_settings
);
1861 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1862 old_user_cancel_any
? "1" : "0",
1863 cupsd_num_settings
, &cupsd_settings
);
1866 * Save the new values...
1869 invalidate_cupsd_cache(cg
);
1871 cg
->cupsd_num_settings
= cupsd_num_settings
;
1872 cg
->cupsd_settings
= cupsd_settings
;
1873 cg
->cupsd_update
= time(NULL
);
1875 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
1878 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
1881 * Remote our temp files and return...
1889 return (status
== HTTP_CREATED
);
1894 * 'do_samba_command()' - Do a SAMBA command.
1897 static int /* O - Status of command */
1898 do_samba_command(const char *command
, /* I - Command to run */
1899 const char *address
, /* I - Address for command */
1900 const char *subcmd
, /* I - Sub-command */
1901 const char *authfile
, /* I - Samba authentication file */
1902 FILE *logfile
) /* I - Optional log file */
1905 return (1); /* Always fail on Windows... */
1908 int status
; /* Status of command */
1909 int pid
; /* Process ID of child */
1913 _cupsLangPrintf(logfile
,
1914 _("Running command: %s %s -N -A %s -c \'%s\'\n"),
1915 command
, address
, authfile
, subcmd
);
1917 if ((pid
= fork()) == 0)
1920 * Child goes here, redirect stdin/out/err and execute the command...
1924 open("/dev/null", O_RDONLY
);
1929 dup(fileno(logfile
));
1931 open("/dev/null", O_WRONLY
);
1936 execlp(command
, command
, address
, "-N", "-A", authfile
, "-c", subcmd
,
1945 _cupsLangPrintf(logfile
, _("Unable to run \"%s\": %s\n"),
1946 command
, strerror(errno
));
1951 * Wait for the process to complete...
1954 while (wait(&status
) != pid
);
1958 _cupsLangPuts(logfile
, "\n");
1960 DEBUG_printf(("status=%d\n", status
));
1962 if (WIFEXITED(status
))
1963 return (WEXITSTATUS(status
));
1965 return (-WTERMSIG(status
));
1971 * 'get_cupsd_conf()' - Get the current cupsd.conf file.
1974 static http_status_t
/* O - Status of request */
1976 http_t
*http
, /* I - Connection to server */
1977 _cups_globals_t
*cg
, /* I - Global data */
1978 time_t last_update
, /* I - Last update time for file */
1979 char *name
, /* I - Filename buffer */
1980 int namesize
, /* I - Size of filename buffer */
1981 int *remote
) /* O - Remote file? */
1983 int fd
; /* Temporary file descriptor */
1985 struct stat info
; /* cupsd.conf file information */
1987 http_status_t status
; /* Status of getting cupsd.conf */
1988 char host
[HTTP_MAX_HOST
]; /* Hostname for connection */
1992 * See if we already have the data we need...
1995 httpGetHostname(http
, host
, sizeof(host
));
1997 if (strcasecmp(cg
->cupsd_hostname
, host
))
1998 invalidate_cupsd_cache(cg
);
2000 snprintf(name
, namesize
, "%s/cupsd.conf", cg
->cups_serverroot
);
2004 if (!strcasecmp(host
, "localhost") && !access(name
, R_OK
))
2007 * Read the local file rather than using HTTP...
2010 if (stat(name
, &info
))
2012 char message
[1024]; /* Message string */
2015 snprintf(message
, sizeof(message
),
2016 _cupsLangString(cupsLangDefault(), _("stat of %s failed: %s")),
2017 name
, strerror(errno
));
2018 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
2022 return (HTTP_SERVER_ERROR
);
2024 else if (last_update
&& info
.st_mtime
<= last_update
)
2025 status
= HTTP_NOT_MODIFIED
;
2033 * Read cupsd.conf via a HTTP GET request...
2036 if ((fd
= cupsTempFd(name
, namesize
)) < 0)
2040 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
2042 invalidate_cupsd_cache(cg
);
2044 return (HTTP_SERVER_ERROR
);
2049 httpClearFields(http
);
2052 httpSetField(http
, HTTP_FIELD_IF_MODIFIED_SINCE
,
2053 httpGetDateString(last_update
));
2055 status
= cupsGetFd(http
, "/admin/conf/cupsd.conf", fd
);
2059 if (status
!= HTTP_OK
)
2071 * 'invalidate_cupsd_cache()' - Invalidate the cached cupsd.conf settings.
2075 invalidate_cupsd_cache(
2076 _cups_globals_t
*cg
) /* I - Global data */
2078 cupsFreeOptions(cg
->cupsd_num_settings
, cg
->cupsd_settings
);
2080 cg
->cupsd_hostname
[0] = '\0';
2081 cg
->cupsd_update
= 0;
2082 cg
->cupsd_num_settings
= 0;
2083 cg
->cupsd_settings
= NULL
;
2088 * 'write_option()' - Write a CUPS option to a PPD file.
2092 write_option(cups_file_t
*dstfp
, /* I - PPD file */
2093 int order
, /* I - Order dependency */
2094 const char *name
, /* I - Option name */
2095 const char *text
, /* I - Option text */
2096 const char *attrname
, /* I - Attribute name */
2097 ipp_attribute_t
*suppattr
, /* I - IPP -supported attribute */
2098 ipp_attribute_t
*defattr
, /* I - IPP -default attribute */
2099 int defval
, /* I - Default value number */
2100 int valcount
) /* I - Number of values */
2102 int i
; /* Looping var */
2105 cupsFilePrintf(dstfp
, "*JCLOpenUI *%s/%s: PickOne\n"
2106 "*OrderDependency: %d JCLSetup *%s\n",
2107 name
, text
, order
, name
);
2109 if (defattr
->value_tag
== IPP_TAG_INTEGER
)
2112 * Do numeric options with a range or list...
2115 cupsFilePrintf(dstfp
, "*Default%s: %d\n", name
,
2116 defattr
->values
[defval
].integer
);
2118 if (suppattr
->value_tag
== IPP_TAG_RANGE
)
2121 * List each number in the range...
2124 for (i
= suppattr
->values
[0].range
.lower
;
2125 i
<= suppattr
->values
[0].range
.upper
;
2128 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, i
);
2131 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n",
2133 else if (defval
== 0)
2134 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
, i
);
2135 else if (defval
< (valcount
- 1))
2136 cupsFilePrintf(dstfp
, ",%d\"\n", i
);
2138 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", i
);
2144 * List explicit numbers...
2147 for (i
= 0; i
< suppattr
->num_values
; i
++)
2149 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, suppattr
->values
[i
].integer
);
2152 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n", attrname
,
2153 suppattr
->values
[i
].integer
);
2154 else if (defval
== 0)
2155 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
,
2156 suppattr
->values
[i
].integer
);
2157 else if (defval
< (valcount
- 1))
2158 cupsFilePrintf(dstfp
, ",%d\"\n", suppattr
->values
[i
].integer
);
2160 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", suppattr
->values
[i
].integer
);
2167 * Do text options with a list...
2170 cupsFilePrintf(dstfp
, "*Default%s: %s\n", name
,
2171 defattr
->values
[defval
].string
.text
);
2173 for (i
= 0; i
< suppattr
->num_values
; i
++)
2175 cupsFilePrintf(dstfp
, "*%s %s: \"", name
,
2176 suppattr
->values
[i
].string
.text
);
2179 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\n\"\n*End\n", attrname
,
2180 suppattr
->values
[i
].string
.text
);
2181 else if (defval
== 0)
2182 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\"\n", attrname
,
2183 suppattr
->values
[i
].string
.text
);
2184 else if (defval
< (valcount
- 1))
2185 cupsFilePrintf(dstfp
, ",%s\"\n", suppattr
->values
[i
].string
.text
);
2187 cupsFilePrintf(dstfp
, ",%s\n\"\n*End\n",
2188 suppattr
->values
[i
].string
.text
);
2192 cupsFilePrintf(dstfp
, "*JCLCloseUI: *%s\n\n", name
);
2197 * End of "$Id: adminutil.c 6378 2007-03-21 07:18:18Z mike $".