2 * "$Id: adminutil.c 6270 2007-02-12 14:27:47Z mike $"
4 * Administration utility API definitions for the Common UNIX Printing
7 * MANY OF THE FUNCTIONS IN THIS HEADER ARE PRIVATE AND SUBJECT TO
8 * CHANGE AT ANY TIME. USE AT YOUR OWN RISK.
10 * Copyright 2001-2007 by Easy Software Products.
12 * These coded instructions, statements, and computer programs are the
13 * property of Easy Software Products and are protected by Federal
14 * copyright law. Distribution and use rights are outlined in the file
15 * "LICENSE.txt" which should have been included with this file. If this
16 * file is missing or damaged please contact Easy Software Products
19 * Attn: CUPS Licensing Information
20 * Easy Software Products
21 * 44141 Airport View Drive, Suite 204
22 * Hollywood, Maryland 20636 USA
24 * Voice: (301) 373-9600
25 * EMail: cups-info@cups.org
26 * WWW: http://www.cups.org
28 * This file is subject to the Apple OS-Developed Software exception.
32 * cupsAdminCreateWindowsPPD() - Create the Windows PPD file for a printer.
33 * cupsAdminExportSamba() - Export a printer to Samba.
34 * _cupsAdminGetServerSettings() - Get settings from the server.
35 * _cupsAdminSetServerSettings() - Set settings on the server.
36 * do_samba_command() - Do a SAMBA command.
37 * get_cupsd_conf() - Get the current cupsd.conf file.
38 * invalidate_cupsd_cache() - Invalidate the cached cupsd.conf settings.
39 * write_option() - Write a CUPS option to a PPD file.
43 * Include necessary headers...
46 #include "adminutil.h"
55 # include <sys/wait.h>
63 static int do_samba_command(const char *command
,
65 const char *subcommand
,
68 static http_status_t
get_cupsd_conf(http_t
*http
, _cups_globals_t
*cg
,
69 time_t last_update
, char *name
,
70 int namelen
, int *remote
);
71 static void invalidate_cupsd_cache(_cups_globals_t
*cg
);
72 static void write_option(cups_file_t
*dstfp
, int order
,
73 const char *name
, const char *text
,
75 ipp_attribute_t
*suppattr
,
76 ipp_attribute_t
*defattr
, int defval
,
81 * 'cupsAdminCreateWindowsPPD()' - Create the Windows PPD file for a printer.
84 char * /* O - PPD file or NULL */
85 cupsAdminCreateWindowsPPD(
86 http_t
*http
, /* I - Connection to server */
87 const char *dest
, /* I - Printer or class */
88 char *buffer
, /* I - Filename buffer */
89 int bufsize
) /* I - Size of filename buffer */
91 const char *src
; /* Source PPD filename */
92 cups_file_t
*srcfp
, /* Source PPD file */
93 *dstfp
; /* Destination PPD file */
94 ipp_t
*request
, /* IPP request */
95 *response
; /* IPP response */
96 ipp_attribute_t
*suppattr
, /* IPP -supported attribute */
97 *defattr
; /* IPP -default attribute */
98 cups_lang_t
*language
; /* Current language */
99 char line
[256], /* Line from PPD file */
100 junk
[256], /* Extra junk to throw away */
101 *ptr
, /* Pointer into line */
102 uri
[1024], /* Printer URI */
103 option
[41], /* Option */
104 choice
[41]; /* Choice */
105 int jcloption
, /* In a JCL option? */
106 linenum
; /* Current line number */
107 time_t curtime
; /* Current time */
108 struct tm
*curdate
; /* Current date */
109 static const char * const pattrs
[] = /* Printer attributes we want */
111 "job-hold-until-supported",
112 "job-hold-until-default",
113 "job-sheets-supported",
114 "job-sheets-default",
115 "job-priority-supported",
116 "job-priority-default"
121 * Range check the input...
127 if (!http
|| !dest
|| !buffer
|| bufsize
< 2)
131 * Get the PPD file...
134 if ((src
= cupsGetPPD2(http
, dest
)) == NULL
)
138 * Get the supported banner pages, etc. for the printer...
141 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
143 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
144 "localhost", 0, "/printers/%s", dest
);
145 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
146 "printer-uri", NULL
, uri
);
148 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
149 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
153 * Do the request and get back a response...
156 response
= cupsDoRequest(http
, request
, "/");
157 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
164 * Open the original PPD file...
167 if ((srcfp
= cupsFileOpen(src
, "rb")) == NULL
)
171 * Create a temporary output file using the destination buffer...
174 if ((dstfp
= cupsTempFile2(buffer
, bufsize
)) == NULL
)
176 cupsFileClose(srcfp
);
184 * Write a new header explaining that this isn't the original PPD...
187 cupsFilePuts(dstfp
, "*PPD-Adobe: \"4.3\"\n");
189 curtime
= time(NULL
);
190 curdate
= gmtime(&curtime
);
192 cupsFilePrintf(dstfp
, "*%% Modified on %04d%02d%02d%02d%02d%02d+0000 "
193 "for CUPS Windows Driver\n",
194 curdate
->tm_year
+ 1900, curdate
->tm_mon
+ 1, curdate
->tm_mday
,
195 curdate
->tm_hour
, curdate
->tm_min
, curdate
->tm_sec
);
198 * Read the existing PPD file, converting all PJL commands to CUPS
199 * job ticket comments...
204 language
= cupsLangDefault();
206 while (cupsFileGets(srcfp
, line
, sizeof(line
)))
210 if (!strncmp(line
, "*PPD-Adobe:", 11))
213 * Already wrote the PPD header...
218 else if (!strncmp(line
, "*JCLBegin:", 10) ||
219 !strncmp(line
, "*JCLToPSInterpreter:", 20) ||
220 !strncmp(line
, "*JCLEnd:", 8) ||
221 !strncmp(line
, "*Protocols:", 11))
224 * Don't use existing JCL keywords; we'll create our own, below...
227 cupsFilePrintf(dstfp
, "*%% Commented out for CUPS Windows Driver...\n"
228 "*%%%s\n", line
+ 1);
231 else if (!strncmp(line
, "*JCLOpenUI", 10))
234 cupsFilePrintf(dstfp
, "%s\n", line
);
236 else if (!strncmp(line
, "*JCLCloseUI", 11))
239 cupsFilePrintf(dstfp
, "%s\n", line
);
241 else if (jcloption
&&
242 strncmp(line
, "*End", 4) &&
243 strncmp(line
, "*Default", 8) &&
244 strncmp(line
, "*OrderDependency", 16))
246 if ((ptr
= strchr(line
, ':')) == NULL
)
248 snprintf(line
, sizeof(line
),
249 _cupsLangString(language
, _("Missing value on line %d!")),
251 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
253 cupsFileClose(srcfp
);
254 cupsFileClose(dstfp
);
264 if ((ptr
= strchr(ptr
, '\"')) == NULL
)
266 snprintf(line
, sizeof(line
),
267 _cupsLangString(language
,
268 _("Missing double quote on line %d!")),
270 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
272 cupsFileClose(srcfp
);
273 cupsFileClose(dstfp
);
283 if (sscanf(line
, "*%40s%*[ \t]%40[^/]", option
, choice
) != 2)
285 snprintf(line
, sizeof(line
),
286 _cupsLangString(language
,
287 _("Bad option + choice on line %d!")),
289 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
291 cupsFileClose(srcfp
);
292 cupsFileClose(dstfp
);
302 if (strchr(ptr
+ 1, '\"') == NULL
)
308 while (cupsFileGets(srcfp
, junk
, sizeof(junk
)) != NULL
)
312 if (!strncmp(junk
, "*End", 4))
317 snprintf(ptr
+ 1, sizeof(line
) - (ptr
- line
+ 1),
318 "%%cupsJobTicket: %s=%s\n\"\n*End", option
, choice
);
320 cupsFilePrintf(dstfp
, "*%% Changed for CUPS Windows Driver...\n%s\n",
324 cupsFilePrintf(dstfp
, "%s\n", line
);
327 cupsFileClose(srcfp
);
332 snprintf(line
, sizeof(line
),
333 _cupsLangString(language
, _("Empty PPD file!")),
335 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
337 cupsFileClose(dstfp
);
346 * Now add the CUPS-specific attributes and options...
349 cupsFilePuts(dstfp
, "\n*% CUPS Job Ticket support and options...\n");
350 cupsFilePuts(dstfp
, "*Protocols: PJL\n");
351 cupsFilePuts(dstfp
, "*JCLBegin: \"%!PS-Adobe-3.0<0A>\"\n");
352 cupsFilePuts(dstfp
, "*JCLToPSInterpreter: \"\"\n");
353 cupsFilePuts(dstfp
, "*JCLEnd: \"\"\n");
355 cupsFilePuts(dstfp
, "\n*OpenGroup: CUPS/CUPS Options\n\n");
357 if ((defattr
= ippFindAttribute(response
, "job-hold-until-default",
358 IPP_TAG_ZERO
)) != NULL
&&
359 (suppattr
= ippFindAttribute(response
, "job-hold-until-supported",
360 IPP_TAG_ZERO
)) != NULL
)
361 write_option(dstfp
, 10, "cupsJobHoldUntil", "Hold Until", "job-hold-until",
362 suppattr
, defattr
, 0, 1);
364 if ((defattr
= ippFindAttribute(response
, "job-priority-default",
365 IPP_TAG_INTEGER
)) != NULL
&&
366 (suppattr
= ippFindAttribute(response
, "job-priority-supported",
367 IPP_TAG_RANGE
)) != NULL
)
368 write_option(dstfp
, 11, "cupsJobPriority", "Priority", "job-priority",
369 suppattr
, defattr
, 0, 1);
371 if ((defattr
= ippFindAttribute(response
, "job-sheets-default",
372 IPP_TAG_ZERO
)) != NULL
&&
373 (suppattr
= ippFindAttribute(response
, "job-sheets-supported",
374 IPP_TAG_ZERO
)) != NULL
)
376 write_option(dstfp
, 20, "cupsJobSheetsStart", "Start Banner",
377 "job-sheets", suppattr
, defattr
, 0, 2);
378 write_option(dstfp
, 21, "cupsJobSheetsEnd", "End Banner",
379 "job-sheets", suppattr
, defattr
, 1, 2);
382 cupsFilePuts(dstfp
, "*CloseGroup: CUPS\n");
383 cupsFileClose(dstfp
);
392 * 'cupsAdminExportSamba()' - Export a printer to Samba.
395 int /* O - 1 on success, 0 on failure */
396 cupsAdminExportSamba(
397 const char *dest
, /* I - Destination to export */
398 const char *ppd
, /* I - PPD file */
399 const char *samba_server
, /* I - Samba server */
400 const char *samba_user
, /* I - Samba username */
401 const char *samba_password
, /* I - Samba password */
402 FILE *logfile
) /* I - Log file, if any */
404 int status
; /* Status of Samba commands */
405 int have_drivers
; /* Have drivers? */
406 char file
[1024], /* File to test for */
407 authfile
[1024], /* Temporary authentication file */
408 address
[1024], /* Address for command */
409 subcmd
[1024], /* Sub-command */
410 message
[1024]; /* Error message */
411 cups_file_t
*fp
; /* Authentication file */
412 cups_lang_t
*language
; /* Current language */
413 _cups_globals_t
*cg
= _cupsGlobals();
418 * Range check input...
421 if (!dest
|| !ppd
|| !samba_server
|| !samba_user
|| !samba_password
)
423 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
428 * Create a temporary authentication file for Samba...
431 if ((fp
= cupsTempFile2(authfile
, sizeof(authfile
))) == NULL
)
433 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
437 cupsFilePrintf(fp
, "username = %s\n", samba_user
);
438 cupsFilePrintf(fp
, "password = %s\n", samba_password
);
442 * See which drivers are available; the new CUPS v6 and Adobe drivers
443 * depend on the Windows 2k PS driver, so copy that driver first:
454 language
= cupsLangDefault();
456 snprintf(file
, sizeof(file
), "%s/drivers/pscript5.dll", cg
->cups_datadir
);
457 if (!access(file
, 0))
462 * Windows 2k driver is installed; do the smbclient commands needed
463 * to copy the Win2k drivers over...
466 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
468 snprintf(subcmd
, sizeof(subcmd
),
470 "put %s W32X86/%s.ppd;"
471 "put %s/drivers/ps5ui.dll W32X86/ps5ui.dll;"
472 "put %s/drivers/pscript.hlp W32X86/pscript.hlp;"
473 "put %s/drivers/pscript.ntf W32X86/pscript.ntf;"
474 "put %s/drivers/pscript5.dll W32X86/pscript5.dll",
475 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
476 cg
->cups_datadir
, cg
->cups_datadir
);
478 if ((status
= do_samba_command("smbclient", address
, subcmd
,
479 authfile
, logfile
)) != 0)
481 snprintf(message
, sizeof(message
),
482 _cupsLangString(language
,
483 _("Unable to copy Windows 2000 printer "
484 "driver files (%d)!")), status
);
486 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
489 _cupsLangPrintf(logfile
, "%s\n", message
);
497 * See if we also have the CUPS driver files; if so, use them!
500 snprintf(file
, sizeof(file
), "%s/drivers/cupsps6.dll", cg
->cups_datadir
);
501 if (!access(file
, 0))
504 * Copy the CUPS driver files over...
507 snprintf(subcmd
, sizeof(subcmd
),
508 "put %s/drivers/cups6.ini W32X86/cups6.ini;"
509 "put %s/drivers/cupsps6.dll W32X86/cupsps6.dll;"
510 "put %s/drivers/cupsui6.dll W32X86/cupsui6.dll",
511 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
513 if ((status
= do_samba_command("smbclient", address
, subcmd
,
514 authfile
, logfile
)) != 0)
516 snprintf(message
, sizeof(message
),
517 _cupsLangString(language
,
518 _("Unable to copy CUPS printer driver "
519 "files (%d)!")), status
);
521 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
524 _cupsLangPrintf(logfile
, "%s\n", message
);
532 * Do the rpcclient command needed for the CUPS drivers...
535 snprintf(subcmd
, sizeof(subcmd
),
536 "adddriver \"Windows NT x86\" \"%s:"
537 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
538 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
539 "cups6.ini,cupsps6.dll,cupsui6.dll\"",
545 * Don't have the CUPS drivers, so just use the standard Windows
549 snprintf(subcmd
, sizeof(subcmd
),
550 "adddriver \"Windows NT x86\" \"%s:"
551 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
552 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
556 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
557 authfile
, logfile
)) != 0)
559 snprintf(message
, sizeof(message
),
560 _cupsLangString(language
,
561 _("Unable to install Windows 2000 printer "
562 "driver files (%d)!")), status
);
564 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
567 _cupsLangPrintf(logfile
, "%s\n", message
);
575 snprintf(file
, sizeof(file
), "%s/drivers/ADOBEPS4.DRV", cg
->cups_datadir
);
576 if (!access(file
, 0))
581 * Do the smbclient commands needed for the Adobe Win9x drivers...
584 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
586 snprintf(subcmd
, sizeof(subcmd
),
588 "put %s WIN40/%s.PPD;"
589 "put %s/drivers/ADFONTS.MFM WIN40/ADFONTS.MFM;"
590 "put %s/drivers/ADOBEPS4.DRV WIN40/ADOBEPS4.DRV;"
591 "put %s/drivers/ADOBEPS4.HLP WIN40/ADOBEPS4.HLP;"
592 "put %s/drivers/ICONLIB.DLL WIN40/ICONLIB.DLL;"
593 "put %s/drivers/PSMON.DLL WIN40/PSMON.DLL;",
594 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
595 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
597 if ((status
= do_samba_command("smbclient", address
, subcmd
,
598 authfile
, logfile
)) != 0)
600 snprintf(message
, sizeof(message
),
601 _cupsLangString(language
,
602 _("Unable to copy Windows 9x printer "
603 "driver files (%d)!")), status
);
605 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
608 _cupsLangPrintf(logfile
, "%s\n", message
);
616 * Do the rpcclient commands needed for the Adobe Win9x drivers...
619 snprintf(subcmd
, sizeof(subcmd
),
620 "adddriver \"Windows 4.0\" \"%s:ADOBEPS4.DRV:%s.PPD:NULL:"
621 "ADOBEPS4.HLP:PSMON.DLL:RAW:"
622 "ADOBEPS4.DRV,%s.PPD,ADOBEPS4.HLP,PSMON.DLL,ADFONTS.MFM,"
626 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
627 authfile
, logfile
)) != 0)
629 snprintf(message
, sizeof(message
),
630 _cupsLangString(language
,
631 _("Unable to install Windows 9x printer "
632 "driver files (%d)!")), status
);
634 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
637 _cupsLangPrintf(logfile
, "%s\n", message
);
645 if (logfile
&& !(have_drivers
& 1))
649 _cupsLangString(language
,
650 _("No Windows printer drivers are installed!")),
654 _cupsLangString(language
,
655 _("Warning, no Windows 2000 printer drivers "
659 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
660 _cupsLangPrintf(logfile
, "%s\n", message
);
663 if (have_drivers
== 0)
667 * Finally, associate the drivers we just added with the queue...
670 snprintf(subcmd
, sizeof(subcmd
), "setdriver %s %s", dest
, dest
);
672 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
673 authfile
, logfile
)) != 0)
675 snprintf(message
, sizeof(message
),
676 _cupsLangString(language
,
677 _("Unable to set Windows printer driver (%d)!")),
680 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
683 _cupsLangPrintf(logfile
, "%s\n", message
);
697 * '_cupsAdminGetServerSettings()' - Get settings from the server.
699 * The returned settings should be freed with cupsFreeOptions() when
700 * you are done with them.
705 int /* O - 1 on success, 0 on failure */
706 _cupsAdminGetServerSettings(
707 http_t
*http
, /* I - Connection to server */
708 int *num_settings
, /* O - Number of settings */
709 cups_option_t
**settings
) /* O - Settings */
711 int i
; /* Looping var */
712 cups_file_t
*cupsd
; /* cupsd.conf file */
713 char cupsdconf
[1024]; /* cupsd.conf filename */
714 int remote
; /* Remote cupsd.conf file? */
715 http_status_t status
; /* Status of getting cupsd.conf */
716 char line
[1024], /* Line from cupsd.conf file */
717 *value
; /* Value on line */
718 cups_option_t
*setting
; /* Current setting */
719 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
723 * Range check input...
726 if (!http
|| !num_settings
|| !settings
)
728 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
743 * Get the cupsd.conf file...
746 if ((status
= get_cupsd_conf(http
, cg
, cg
->cupsd_update
, cupsdconf
,
747 sizeof(cupsdconf
), &remote
)) == HTTP_OK
)
749 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
751 char message
[1024]; /* Message string */
754 snprintf(message
, sizeof(message
),
755 _cupsLangString(cupsLangDefault(), _("open of %s failed: %s")),
756 cupsdconf
, strerror(errno
));
757 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
766 * Read the file, keeping track of what settings are enabled...
769 int remote_access
= 0, /* Remote access allowed? */
770 remote_admin
= 0, /* Remote administration allowed? */
771 remote_any
= 0, /* Remote access from anywhere allowed? */
772 browsing
= 1, /* Browsing enabled? */
773 browse_allow
= 1, /* Browse address set? */
774 browse_address
= 0, /* Browse address set? */
775 cancel_policy
= 1, /* Cancel-job policy set? */
776 debug_logging
= 0; /* LogLevel debug set? */
777 int linenum
= 0, /* Line number in file */
778 in_location
= 0, /* In a location section? */
779 in_policy
= 0, /* In a policy section? */
780 in_cancel_job
= 0, /* In a cancel-job section? */
781 in_admin_location
= 0; /* In the /admin location? */
784 invalidate_cupsd_cache(cg
);
786 cg
->cupsd_update
= time(NULL
);
787 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
789 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
794 if (!strcasecmp(line
, "Port") || !strcasecmp(line
, "Listen"))
796 char *port
; /* Pointer to port number, if any */
799 if ((port
= strrchr(value
, ':')) != NULL
)
801 else if (isdigit(*value
& 255))
804 * Listen on a port number implies remote access...
811 if (strcasecmp(value
, "localhost") && strcmp(value
, "127.0.0.1")
814 #endif /* AF_LOCAL */
816 && strcmp(value
, "::1")
817 #endif /* AF_INET6 */
821 else if (!strcasecmp(line
, "Browsing"))
823 browsing
= !strcasecmp(value
, "yes") || !strcasecmp(value
, "on") ||
824 !strcasecmp(value
, "true");
826 else if (!strcasecmp(line
, "BrowseAddress"))
830 else if (!strcasecmp(line
, "BrowseAllow"))
834 else if (!strcasecmp(line
, "BrowseOrder"))
836 browse_allow
= !strncasecmp(value
, "deny,", 5);
838 else if (!strcasecmp(line
, "LogLevel"))
840 debug_logging
= !strncasecmp(value
, "debug", 5);
842 else if (!strcasecmp(line
, "<Policy") && !strcasecmp(value
, "default"))
846 else if (!strcasecmp(line
, "</Policy>"))
850 else if (!strcasecmp(line
, "<Limit") && in_policy
)
853 * See if the policy limit is for the Cancel-Job operation...
856 char *valptr
; /* Pointer into value */
861 for (valptr
= value
; !isspace(*valptr
& 255) && *valptr
; valptr
++);
866 if (!strcasecmp(value
, "cancel-job") || !strcasecmp(value
, "all"))
872 for (value
= valptr
; isspace(*value
& 255); value
++);
875 else if (!strcasecmp(line
, "</Limit>"))
879 else if (!strcasecmp(line
, "Require") && in_cancel_job
)
883 else if (!strcasecmp(line
, "<Location"))
885 in_admin_location
= !strcasecmp(value
, "/admin");
888 else if (!strcasecmp(line
, "</Location>"))
890 in_admin_location
= 0;
893 else if (!strcasecmp(line
, "Allow") && in_admin_location
&&
894 strcasecmp(value
, "localhost") && strcasecmp(value
, "127.0.0.1")
897 #endif /* AF_LOCAL */
899 && strcmp(value
, "::1")
900 #endif /* AF_INET6 */
905 if (!strcasecmp(value
, "all"))
908 else if (line
[0] != '<' && !in_location
&& !in_policy
)
909 cg
->cupsd_num_settings
= cupsAddOption(line
, value
,
910 cg
->cupsd_num_settings
,
911 &(cg
->cupsd_settings
));
914 cupsFileClose(cupsd
);
916 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
917 debug_logging
? "1" : "0",
918 cg
->cupsd_num_settings
,
919 &(cg
->cupsd_settings
));
921 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
922 (remote_access
&& remote_admin
) ?
924 cg
->cupsd_num_settings
,
925 &(cg
->cupsd_settings
));
927 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
928 remote_any
? "1" : "0",
929 cg
->cupsd_num_settings
,
930 &(cg
->cupsd_settings
));
932 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
933 (browsing
&& browse_allow
) ?
935 cg
->cupsd_num_settings
,
936 &(cg
->cupsd_settings
));
938 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
939 (remote_access
&& browsing
&&
940 browse_address
) ? "1" : "0",
941 cg
->cupsd_num_settings
,
942 &(cg
->cupsd_settings
));
944 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
945 cancel_policy
? "1" : "0",
946 cg
->cupsd_num_settings
,
947 &(cg
->cupsd_settings
));
949 else if (status
!= HTTP_NOT_MODIFIED
)
950 invalidate_cupsd_cache(cg
);
953 * Remove any temporary files and copy the settings array...
959 for (i
= cg
->cupsd_num_settings
, setting
= cg
->cupsd_settings
;
962 *num_settings
= cupsAddOption(setting
->name
, setting
->value
,
963 *num_settings
, settings
);
965 return (cg
->cupsd_num_settings
> 0);
970 * '_cupsAdminSetServerSettings()' - Set settings on the server.
975 int /* O - 1 on success, 0 on failure */
976 _cupsAdminSetServerSettings(
977 http_t
*http
, /* I - Connection to server */
978 int num_settings
, /* I - Number of settings */
979 cups_option_t
*settings
) /* I - Settings */
981 int i
; /* Looping var */
982 http_status_t status
; /* GET/PUT status */
983 const char *server_port_env
; /* SERVER_PORT env var */
984 int server_port
; /* IPP port for server */
985 cups_file_t
*cupsd
; /* cupsd.conf file */
986 char cupsdconf
[1024]; /* cupsd.conf filename */
987 int remote
; /* Remote cupsd.conf file? */
988 char tempfile
[1024]; /* Temporary new cupsd.conf */
989 cups_file_t
*temp
; /* Temporary file */
990 char line
[1024], /* Line from cupsd.conf file */
991 *value
; /* Value on line */
992 int linenum
, /* Line number in file */
993 in_location
, /* In a location section? */
994 in_policy
, /* In a policy section? */
995 in_default_policy
, /* In the default policy section? */
996 in_cancel_job
, /* In a cancel-job section? */
997 in_admin_location
, /* In the /admin location? */
998 in_conf_location
, /* In the /admin/conf location? */
999 in_root_location
; /* In the / location? */
1000 const char *val
; /* Setting value */
1001 int remote_printers
, /* Show remote printers */
1002 share_printers
, /* Share local printers */
1003 remote_admin
, /* Remote administration allowed? */
1004 remote_any
, /* Remote access from anywhere? */
1005 user_cancel_any
, /* Cancel-job policy set? */
1006 debug_logging
; /* LogLevel debug set? */
1007 int wrote_port_listen
, /* Wrote the port/listen lines? */
1008 wrote_browsing
, /* Wrote the browsing lines? */
1009 wrote_policy
, /* Wrote the policy? */
1010 wrote_loglevel
, /* Wrote the LogLevel line? */
1011 wrote_admin_location
, /* Wrote the /admin location? */
1012 wrote_conf_location
, /* Wrote the /admin/conf location? */
1013 wrote_root_location
; /* Wrote the / location? */
1014 int indent
; /* Indentation */
1015 int cupsd_num_settings
; /* New number of settings */
1016 int old_remote_printers
, /* Show remote printers */
1017 old_share_printers
, /* Share local printers */
1018 old_remote_admin
, /* Remote administration allowed? */
1019 old_user_cancel_any
, /* Cancel-job policy set? */
1020 old_debug_logging
; /* LogLevel debug set? */
1021 cups_option_t
*cupsd_settings
, /* New settings */
1022 *setting
; /* Current setting */
1023 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1027 * Range check input...
1030 if (!http
|| !num_settings
|| !settings
)
1032 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
1038 * Get the cupsd.conf file...
1041 if ((status
= get_cupsd_conf(http
, cg
, 0, cupsdconf
, sizeof(cupsdconf
),
1042 &remote
)) == HTTP_OK
)
1044 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
1046 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
1054 * Get current settings...
1057 if (!_cupsAdminGetServerSettings(http
, &cupsd_num_settings
,
1061 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, cupsd_num_settings
,
1062 cupsd_settings
)) != NULL
)
1063 old_debug_logging
= atoi(val
);
1065 old_debug_logging
= 0;
1067 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, cupsd_num_settings
,
1068 cupsd_settings
)) != NULL
)
1069 old_remote_admin
= atoi(val
);
1071 old_remote_admin
= 0;
1073 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, cupsd_num_settings
,
1074 cupsd_settings
)) != NULL
)
1075 remote_any
= atoi(val
);
1079 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
, cupsd_num_settings
,
1080 cupsd_settings
)) != NULL
)
1081 old_remote_printers
= atoi(val
);
1083 old_remote_printers
= 1;
1085 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, cupsd_num_settings
,
1086 cupsd_settings
)) != NULL
)
1087 old_share_printers
= atoi(val
);
1089 old_share_printers
= 0;
1091 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, cupsd_num_settings
,
1092 cupsd_settings
)) != NULL
)
1093 old_user_cancel_any
= atoi(val
);
1095 old_user_cancel_any
= 0;
1097 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
1100 * Get basic settings...
1103 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, num_settings
,
1106 debug_logging
= atoi(val
);
1108 if (debug_logging
== old_debug_logging
)
1111 * No change to this setting...
1120 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, num_settings
,
1122 remote_any
= atoi(val
);
1124 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, num_settings
,
1127 remote_admin
= atoi(val
);
1129 if (remote_admin
== old_remote_admin
&& remote_any
< 0)
1132 * No change to this setting...
1141 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
, num_settings
,
1144 remote_printers
= atoi(val
);
1146 if (remote_printers
== old_remote_printers
)
1149 * No change to this setting...
1152 remote_printers
= -1;
1156 remote_printers
= -1;
1158 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, num_settings
,
1161 share_printers
= atoi(val
);
1163 if (share_printers
== old_share_printers
&& remote_any
< 0)
1166 * No change to this setting...
1169 share_printers
= -1;
1173 share_printers
= -1;
1175 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, num_settings
,
1178 user_cancel_any
= atoi(val
);
1180 if (user_cancel_any
== old_user_cancel_any
)
1183 * No change to this setting...
1186 user_cancel_any
= -1;
1190 user_cancel_any
= -1;
1193 * Create a temporary file for the new cupsd.conf file...
1196 if ((temp
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1198 cupsFileClose(cupsd
);
1203 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
1208 * Copy the old file to the new, making changes along the way...
1211 cupsd_num_settings
= 0;
1212 in_admin_location
= 0;
1214 in_conf_location
= 0;
1215 in_default_policy
= 0;
1218 in_root_location
= 0;
1220 wrote_admin_location
= 0;
1222 wrote_conf_location
= 0;
1225 wrote_port_listen
= 0;
1226 wrote_root_location
= 0;
1229 if ((server_port_env
= getenv("SERVER_PORT")) != NULL
)
1231 if ((server_port
= atoi(server_port_env
)) <= 0)
1232 server_port
= ippPort();
1235 server_port
= ippPort();
1237 if (server_port
<= 0)
1238 server_port
= IPP_PORT
;
1240 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
1242 if ((!strcasecmp(line
, "Port") || !strcasecmp(line
, "Listen")) &&
1243 (share_printers
>= 0 || remote_admin
>= 0))
1245 if (!wrote_port_listen
)
1247 wrote_port_listen
= 1;
1249 if (share_printers
> 0 || remote_admin
> 0)
1251 cupsFilePuts(temp
, "# Allow remote access\n");
1252 cupsFilePrintf(temp
, "Port %d\n", server_port
);
1256 cupsFilePuts(temp
, "# Only listen for connections from the local "
1258 cupsFilePrintf(temp
, "Listen localhost:%d\n", server_port
);
1261 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1262 if ((!value
|| strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)) &&
1263 !access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1264 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1265 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1267 else if (value
&& value
[0] == '/'
1268 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1269 && strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)
1270 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1272 cupsFilePrintf(temp
, "Listen %s\n", value
);
1274 else if ((!strcasecmp(line
, "Browsing") ||
1275 !strcasecmp(line
, "BrowseAddress") ||
1276 !strcasecmp(line
, "BrowseAllow") ||
1277 !strcasecmp(line
, "BrowseDeny") ||
1278 !strcasecmp(line
, "BrowseOrder")) &&
1279 (remote_printers
>= 0 || share_printers
>= 0))
1281 if (!wrote_browsing
)
1285 if (remote_printers
> 0 || share_printers
> 0)
1287 if (remote_printers
> 0 && share_printers
> 0)
1289 "# Enable printer sharing and shared printers.\n");
1290 else if (remote_printers
> 0)
1292 "# Show shared printers on the local network.\n");
1295 "# Share local printers on the local network.\n");
1297 cupsFilePuts(temp
, "Browsing On\n");
1298 cupsFilePuts(temp
, "BrowseOrder allow,deny\n");
1300 if (remote_printers
> 0)
1301 cupsFilePuts(temp
, "BrowseAllow all\n");
1303 if (share_printers
> 0)
1304 cupsFilePuts(temp
, "BrowseAddress @LOCAL\n");
1309 "# Disable printer sharing and shared printers.\n");
1310 cupsFilePuts(temp
, "Browsing Off\n");
1314 else if (!strcasecmp(line
, "LogLevel") && debug_logging
>= 0)
1321 "# Show troubleshooting information in error_log.\n");
1322 cupsFilePuts(temp
, "LogLevel debug\n");
1326 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1327 cupsFilePuts(temp
, "LogLevel info\n");
1330 else if (!strcasecmp(line
, "<Policy"))
1332 in_default_policy
= !strcasecmp(value
, "default");
1335 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1338 else if (!strcasecmp(line
, "</Policy>"))
1341 if (!wrote_policy
&& in_default_policy
)
1345 if (!user_cancel_any
)
1346 cupsFilePuts(temp
, " # Only the owner or an administrator can "
1348 " <Limit Cancel-Job>\n"
1349 " Order deny,allow\n"
1356 in_default_policy
= 0;
1358 cupsFilePuts(temp
, "</Policy>\n");
1360 else if (!strcasecmp(line
, "<Location"))
1364 if (!strcmp(value
, "/admin"))
1365 in_admin_location
= 1;
1366 if (!strcmp(value
, "/admin/conf"))
1367 in_conf_location
= 1;
1368 else if (!strcmp(value
, "/"))
1369 in_root_location
= 1;
1371 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1373 else if (!strcasecmp(line
, "</Location>"))
1377 if (in_admin_location
&& remote_admin
>= 0)
1379 wrote_admin_location
= 1;
1382 cupsFilePuts(temp
, " # Allow remote administration...\n");
1383 else if (remote_admin
== 0)
1384 cupsFilePuts(temp
, " # Restrict access to the admin pages...\n");
1386 cupsFilePuts(temp
, " Order allow,deny\n");
1389 cupsFilePrintf(temp
, " Allow %s\n",
1390 remote_any
> 0 ? "all" : "@LOCAL");
1392 cupsFilePuts(temp
, " Allow localhost\n");
1394 else if (in_conf_location
&& remote_admin
>= 0)
1396 wrote_conf_location
= 1;
1399 cupsFilePuts(temp
, " # Allow remote access to the configuration "
1402 cupsFilePuts(temp
, " # Restrict access to the configuration "
1405 cupsFilePuts(temp
, " Order allow,deny\n");
1408 cupsFilePrintf(temp
, " Allow %s\n",
1409 remote_any
> 0 ? "all" : "@LOCAL");
1411 cupsFilePuts(temp
, " Allow localhost\n");
1413 else if (in_root_location
&& (remote_admin
>= 0 || share_printers
>= 0))
1415 wrote_root_location
= 1;
1417 if (remote_admin
> 0 && share_printers
> 0)
1418 cupsFilePuts(temp
, " # Allow shared printing and remote "
1419 "administration...\n");
1420 else if (remote_admin
> 0)
1421 cupsFilePuts(temp
, " # Allow remote administration...\n");
1422 else if (share_printers
> 0)
1423 cupsFilePuts(temp
, " # Allow shared printing...\n");
1425 cupsFilePuts(temp
, " # Restrict access to the server...\n");
1427 cupsFilePuts(temp
, " Order allow,deny\n");
1429 if (remote_admin
> 0 || share_printers
> 0)
1430 cupsFilePrintf(temp
, " Allow %s\n",
1431 remote_any
> 0 ? "all" : "@LOCAL");
1433 cupsFilePuts(temp
, " Allow localhost\n");
1436 in_admin_location
= 0;
1437 in_conf_location
= 0;
1438 in_root_location
= 0;
1440 cupsFilePuts(temp
, "</Location>\n");
1442 else if (!strcasecmp(line
, "<Limit") && in_default_policy
)
1445 * See if the policy limit is for the Cancel-Job operation...
1448 char *valptr
; /* Pointer into value */
1453 if (!strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1456 * Don't write anything for this limit section...
1463 cupsFilePrintf(temp
, " %s", line
);
1467 for (valptr
= value
; !isspace(*valptr
& 255) && *valptr
; valptr
++);
1472 if (!strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1475 * Write everything except for this definition...
1481 cupsFilePrintf(temp
, " %s", value
);
1483 for (value
= valptr
; isspace(*value
& 255); value
++);
1486 cupsFilePuts(temp
, ">\n");
1489 else if (!strcasecmp(line
, "</Limit>") && in_cancel_job
)
1493 if (in_cancel_job
== 1)
1494 cupsFilePuts(temp
, " </Limit>\n");
1498 if (!user_cancel_any
)
1499 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1501 " <Limit Cancel-Job>\n"
1502 " Order deny,allow\n"
1503 " Require user @OWNER @SYSTEM\n"
1508 else if ((((in_admin_location
|| in_conf_location
|| in_root_location
) &&
1509 remote_admin
>= 0) ||
1510 (in_root_location
&& share_printers
>= 0)) &&
1511 (!strcasecmp(line
, "Allow") || !strcasecmp(line
, "Deny") ||
1512 !strcasecmp(line
, "Order")))
1514 else if (in_cancel_job
== 2)
1516 else if (!strcasecmp(line
, "<Limit") && value
)
1517 cupsFilePrintf(temp
, " %s %s>\n", line
, value
);
1518 else if (line
[0] == '<')
1522 cupsFilePrintf(temp
, "%*s%s %s>\n", indent
, "", line
, value
);
1530 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1533 else if (!in_policy
&& !in_location
&&
1534 (val
= cupsGetOption(line
, num_settings
, settings
)) != NULL
&&
1535 !cupsGetOption(line
, cupsd_num_settings
, cupsd_settings
))
1538 * Add this directive to the list of directives we have written...
1541 cupsd_num_settings
= cupsAddOption(line
, value
, cupsd_num_settings
,
1545 * Write the new value in its place, without indentation since we
1546 * only support setting root directives, not in sections...
1549 cupsFilePrintf(temp
, "%s %s\n", line
, value
);
1553 if (!in_policy
&& !in_location
)
1556 * Record the non-policy, non-location directives that we find
1557 * in the server settings, since we cache this info and record it
1558 * in _cupsAdminGetServerSettings()...
1561 cupsd_num_settings
= cupsAddOption(line
, value
, cupsd_num_settings
,
1565 cupsFilePrintf(temp
, "%*s%s %s\n", indent
, "", line
, value
);
1568 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1572 * Write any missing info...
1575 if (!wrote_browsing
&& (remote_printers
>= 0 || share_printers
>= 0))
1577 if (remote_printers
> 0 || share_printers
> 0)
1579 if (remote_printers
> 0 && share_printers
> 0)
1580 cupsFilePuts(temp
, "# Enable printer sharing and shared printers.\n");
1581 else if (remote_printers
> 0)
1582 cupsFilePuts(temp
, "# Show shared printers on the local network.\n");
1584 cupsFilePuts(temp
, "# Share local printers on the local network.\n");
1586 cupsFilePuts(temp
, "Browsing On\n");
1587 cupsFilePuts(temp
, "BrowseOrder allow,deny\n");
1589 if (remote_printers
> 0)
1590 cupsFilePuts(temp
, "BrowseAllow all\n");
1592 if (share_printers
> 0)
1593 cupsFilePuts(temp
, "BrowseAddress @LOCAL\n");
1597 cupsFilePuts(temp
, "# Disable printer sharing and shared printers.\n");
1598 cupsFilePuts(temp
, "Browsing Off\n");
1602 if (!wrote_loglevel
&& debug_logging
>= 0)
1606 cupsFilePuts(temp
, "# Show troubleshooting information in error_log.\n");
1607 cupsFilePuts(temp
, "LogLevel debug\n");
1611 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1612 cupsFilePuts(temp
, "LogLevel info\n");
1616 if (!wrote_port_listen
&& (share_printers
>= 0 || remote_admin
>= 0))
1618 if (share_printers
> 0 || remote_admin
> 0)
1620 cupsFilePuts(temp
, "# Allow remote access\n");
1621 cupsFilePrintf(temp
, "Port %d\n", ippPort());
1626 "# Only listen for connections from the local machine.\n");
1627 cupsFilePrintf(temp
, "Listen localhost:%d\n", ippPort());
1630 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1631 if (!access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1632 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1633 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1636 if (!wrote_root_location
&& (remote_admin
>= 0 || share_printers
>= 0))
1638 if (remote_admin
> 0 && share_printers
> 0)
1640 "# Allow shared printing and remote administration...\n");
1641 else if (remote_admin
> 0)
1642 cupsFilePuts(temp
, "# Allow remote administration...\n");
1643 else if (share_printers
> 0)
1644 cupsFilePuts(temp
, "# Allow shared printing...\n");
1646 cupsFilePuts(temp
, "# Restrict access to the server...\n");
1648 cupsFilePuts(temp
, "<Location />\n"
1649 " Order allow,deny\n");
1651 if (remote_admin
> 0 || share_printers
> 0)
1652 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1654 cupsFilePuts(temp
, " Allow localhost\n");
1656 cupsFilePuts(temp
, "</Location>\n");
1659 if (!wrote_admin_location
&& remote_admin
>= 0)
1662 cupsFilePuts(temp
, "# Allow remote administration...\n");
1664 cupsFilePuts(temp
, "# Restrict access to the admin pages...\n");
1666 cupsFilePuts(temp
, "<Location /admin>\n"
1667 " Order allow,deny\n");
1670 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1672 cupsFilePuts(temp
, " Allow localhost\n");
1674 cupsFilePuts(temp
, "</Location>\n");
1677 if (!wrote_conf_location
&& remote_admin
>= 0)
1681 "# Allow remote access to the configuration files...\n");
1683 cupsFilePuts(temp
, "# Restrict access to the configuration files...\n");
1685 cupsFilePuts(temp
, "<Location /admin/conf>\n"
1687 " Require user @SYSTEM\n"
1688 " Order allow,deny\n");
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_policy
&& user_cancel_any
>= 0)
1700 cupsFilePuts(temp
, "<Policy default>\n"
1701 " # Job-related operations must be done by the owner "
1702 "or an adminstrator...\n"
1703 " <Limit Send-Document Send-URI Hold-Job Release-Job "
1704 "Restart-Job Purge-Jobs Set-Job-Attributes "
1705 "Create-Job-Subscription Renew-Subscription "
1706 "Cancel-Subscription Get-Notifications Reprocess-Job "
1707 "Cancel-Current-Job Suspend-Current-Job Resume-Job "
1709 " Require user @OWNER @SYSTEM\n"
1710 " Order deny,allow\n"
1712 " # All administration operations require an "
1713 "adminstrator to authenticate...\n"
1714 " <Limit Pause-Printer Resume-Printer "
1715 "Set-Printer-Attributes Enable-Printer "
1716 "Disable-Printer Pause-Printer-After-Current-Job "
1717 "Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer "
1718 "Activate-Printer Restart-Printer Shutdown-Printer "
1719 "Startup-Printer Promote-Job Schedule-Job-After "
1720 "CUPS-Add-Printer CUPS-Delete-Printer "
1721 "CUPS-Add-Class CUPS-Delete-Class "
1722 "CUPS-Accept-Jobs CUPS-Reject-Jobs "
1723 "CUPS-Set-Default CUPS-Add-Device CUPS-Delete-Device>\n"
1725 " Require user @SYSTEM\n"
1726 " Order deny,allow\n"
1729 if (!user_cancel_any
)
1730 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1732 " <Limit Cancel-Job>\n"
1733 " Require user @OWNER @SYSTEM\n"
1734 " Order deny,allow\n"
1737 cupsFilePuts(temp
, " <Limit All>\n"
1738 " Order deny,allow\n"
1743 for (i
= num_settings
, setting
= settings
; i
> 0; i
--, setting
++)
1744 if (setting
->name
[0] != '_' &&
1745 !cupsGetOption(setting
->name
, cupsd_num_settings
, cupsd_settings
))
1748 * Add this directive to the list of directives we have written...
1751 cupsd_num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1752 cupsd_num_settings
, &cupsd_settings
);
1755 * Write the new value in its place, without indentation since we
1756 * only support setting root directives, not in sections...
1759 cupsFilePrintf(temp
, "%s %s\n", setting
->name
, setting
->value
);
1762 cupsFileClose(cupsd
);
1763 cupsFileClose(temp
);
1766 * Upload the configuration file to the server...
1769 status
= cupsPutFile(http
, "/admin/conf/cupsd.conf", tempfile
);
1771 if (status
== HTTP_CREATED
)
1774 * Updated OK, add the basic settings...
1777 if (debug_logging
>= 0)
1778 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1779 debug_logging
? "1" : "0",
1780 cupsd_num_settings
, &cupsd_settings
);
1782 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1783 old_debug_logging
? "1" : "0",
1784 cupsd_num_settings
, &cupsd_settings
);
1786 if (remote_admin
>= 0)
1787 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1788 remote_admin
? "1" : "0",
1789 cupsd_num_settings
, &cupsd_settings
);
1791 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1792 old_remote_admin
? "1" : "0",
1793 cupsd_num_settings
, &cupsd_settings
);
1795 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
1796 remote_any
? "1" : "0",
1797 cupsd_num_settings
, &cupsd_settings
);
1799 if (remote_printers
>= 0)
1800 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
1801 remote_printers
? "1" : "0",
1802 cupsd_num_settings
, &cupsd_settings
);
1804 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
1805 old_remote_printers
? "1" : "0",
1806 cupsd_num_settings
, &cupsd_settings
);
1808 if (share_printers
>= 0)
1809 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1810 share_printers
? "1" : "0",
1811 cupsd_num_settings
, &cupsd_settings
);
1813 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1814 old_share_printers
? "1" : "0",
1815 cupsd_num_settings
, &cupsd_settings
);
1817 if (user_cancel_any
>= 0)
1818 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1819 user_cancel_any
? "1" : "0",
1820 cupsd_num_settings
, &cupsd_settings
);
1822 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1823 old_user_cancel_any
? "1" : "0",
1824 cupsd_num_settings
, &cupsd_settings
);
1827 * Save the new values...
1830 invalidate_cupsd_cache(cg
);
1832 cg
->cupsd_num_settings
= cupsd_num_settings
;
1833 cg
->cupsd_settings
= cupsd_settings
;
1834 cg
->cupsd_update
= time(NULL
);
1836 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
1839 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
1842 * Remote our temp files and return...
1850 return (status
== HTTP_CREATED
);
1855 * 'do_samba_command()' - Do a SAMBA command.
1858 static int /* O - Status of command */
1859 do_samba_command(const char *command
, /* I - Command to run */
1860 const char *address
, /* I - Address for command */
1861 const char *subcmd
, /* I - Sub-command */
1862 const char *authfile
, /* I - Samba authentication file */
1863 FILE *logfile
) /* I - Optional log file */
1866 return (1); /* Always fail on Windows... */
1869 int status
; /* Status of command */
1870 int pid
; /* Process ID of child */
1874 _cupsLangPrintf(logfile
,
1875 _("Running command: %s %s -N -A %s -c \'%s\'\n"),
1876 command
, address
, authfile
, subcmd
);
1878 if ((pid
= fork()) == 0)
1881 * Child goes here, redirect stdin/out/err and execute the command...
1885 open("/dev/null", O_RDONLY
);
1890 dup(fileno(logfile
));
1892 open("/dev/null", O_WRONLY
);
1897 execlp(command
, command
, address
, "-N", "-A", authfile
, "-c", subcmd
,
1906 _cupsLangPrintf(logfile
, _("Unable to run \"%s\": %s\n"),
1907 command
, strerror(errno
));
1912 * Wait for the process to complete...
1915 while (wait(&status
) != pid
);
1919 _cupsLangPuts(logfile
, "\n");
1921 DEBUG_printf(("status=%d\n", status
));
1923 if (WIFEXITED(status
))
1924 return (WEXITSTATUS(status
));
1926 return (-WTERMSIG(status
));
1932 * 'get_cupsd_conf()' - Get the current cupsd.conf file.
1935 static http_status_t
/* O - Status of request */
1937 http_t
*http
, /* I - Connection to server */
1938 _cups_globals_t
*cg
, /* I - Global data */
1939 time_t last_update
, /* I - Last update time for file */
1940 char *name
, /* I - Filename buffer */
1941 int namesize
, /* I - Size of filename buffer */
1942 int *remote
) /* O - Remote file? */
1944 int fd
; /* Temporary file descriptor */
1946 struct stat info
; /* cupsd.conf file information */
1948 http_status_t status
; /* Status of getting cupsd.conf */
1949 char host
[HTTP_MAX_HOST
]; /* Hostname for connection */
1953 * See if we already have the data we need...
1956 httpGetHostname(http
, host
, sizeof(host
));
1958 if (strcasecmp(cg
->cupsd_hostname
, host
))
1959 invalidate_cupsd_cache(cg
);
1961 snprintf(name
, namesize
, "%s/cupsd.conf", cg
->cups_serverroot
);
1965 if (!strcasecmp(host
, "localhost") && !access(name
, R_OK
))
1968 * Read the local file rather than using HTTP...
1971 if (stat(name
, &info
))
1973 char message
[1024]; /* Message string */
1976 snprintf(message
, sizeof(message
),
1977 _cupsLangString(cupsLangDefault(), _("stat of %s failed: %s")),
1978 name
, strerror(errno
));
1979 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
1983 return (HTTP_SERVER_ERROR
);
1985 else if (last_update
&& info
.st_mtime
<= last_update
)
1986 status
= HTTP_NOT_MODIFIED
;
1994 * Read cupsd.conf via a HTTP GET request...
1997 if ((fd
= cupsTempFd(name
, namesize
)) < 0)
2001 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
2003 invalidate_cupsd_cache(cg
);
2005 return (HTTP_SERVER_ERROR
);
2010 httpClearFields(http
);
2013 httpSetField(http
, HTTP_FIELD_IF_MODIFIED_SINCE
,
2014 httpGetDateString(last_update
));
2016 status
= cupsGetFd(http
, "/admin/conf/cupsd.conf", fd
);
2020 if (status
!= HTTP_OK
)
2032 * 'invalidate_cupsd_cache()' - Invalidate the cached cupsd.conf settings.
2036 invalidate_cupsd_cache(
2037 _cups_globals_t
*cg
) /* I - Global data */
2039 cupsFreeOptions(cg
->cupsd_num_settings
, cg
->cupsd_settings
);
2041 cg
->cupsd_hostname
[0] = '\0';
2042 cg
->cupsd_update
= 0;
2043 cg
->cupsd_num_settings
= 0;
2044 cg
->cupsd_settings
= NULL
;
2049 * 'write_option()' - Write a CUPS option to a PPD file.
2053 write_option(cups_file_t
*dstfp
, /* I - PPD file */
2054 int order
, /* I - Order dependency */
2055 const char *name
, /* I - Option name */
2056 const char *text
, /* I - Option text */
2057 const char *attrname
, /* I - Attribute name */
2058 ipp_attribute_t
*suppattr
, /* I - IPP -supported attribute */
2059 ipp_attribute_t
*defattr
, /* I - IPP -default attribute */
2060 int defval
, /* I - Default value number */
2061 int valcount
) /* I - Number of values */
2063 int i
; /* Looping var */
2066 cupsFilePrintf(dstfp
, "*JCLOpenUI *%s/%s: PickOne\n"
2067 "*OrderDependency: %d JCLSetup *%s\n",
2068 name
, text
, order
, name
);
2070 if (defattr
->value_tag
== IPP_TAG_INTEGER
)
2073 * Do numeric options with a range or list...
2076 cupsFilePrintf(dstfp
, "*Default%s: %d\n", name
,
2077 defattr
->values
[defval
].integer
);
2079 if (suppattr
->value_tag
== IPP_TAG_RANGE
)
2082 * List each number in the range...
2085 for (i
= suppattr
->values
[0].range
.lower
;
2086 i
<= suppattr
->values
[0].range
.upper
;
2089 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, i
);
2092 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n",
2094 else if (defval
== 0)
2095 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
, i
);
2096 else if (defval
< (valcount
- 1))
2097 cupsFilePrintf(dstfp
, ",%d\"\n", i
);
2099 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", i
);
2105 * List explicit numbers...
2108 for (i
= 0; i
< suppattr
->num_values
; i
++)
2110 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, suppattr
->values
[i
].integer
);
2113 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n", attrname
,
2114 suppattr
->values
[i
].integer
);
2115 else if (defval
== 0)
2116 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
,
2117 suppattr
->values
[i
].integer
);
2118 else if (defval
< (valcount
- 1))
2119 cupsFilePrintf(dstfp
, ",%d\"\n", suppattr
->values
[i
].integer
);
2121 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", suppattr
->values
[i
].integer
);
2128 * Do text options with a list...
2131 cupsFilePrintf(dstfp
, "*Default%s: %s\n", name
,
2132 defattr
->values
[defval
].string
.text
);
2134 for (i
= 0; i
< suppattr
->num_values
; i
++)
2136 cupsFilePrintf(dstfp
, "*%s %s: \"", name
,
2137 suppattr
->values
[i
].string
.text
);
2140 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\n\"\n*End\n", attrname
,
2141 suppattr
->values
[i
].string
.text
);
2142 else if (defval
== 0)
2143 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\"\n", attrname
,
2144 suppattr
->values
[i
].string
.text
);
2145 else if (defval
< (valcount
- 1))
2146 cupsFilePrintf(dstfp
, ",%s\"\n", suppattr
->values
[i
].string
.text
);
2148 cupsFilePrintf(dstfp
, ",%s\n\"\n*End\n",
2149 suppattr
->values
[i
].string
.text
);
2153 cupsFilePrintf(dstfp
, "*JCLCloseUI: *%s\n\n", name
);
2158 * End of "$Id: adminutil.c 6270 2007-02-12 14:27:47Z mike $".