2 * "$Id: adminutil.c 6361 2007-03-19 16:01:28Z 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 static int do_samba_command(const char *command
,
64 const char *subcommand
,
67 static http_status_t
get_cupsd_conf(http_t
*http
, _cups_globals_t
*cg
,
68 time_t last_update
, char *name
,
69 int namelen
, int *remote
);
70 static void invalidate_cupsd_cache(_cups_globals_t
*cg
);
71 static void write_option(cups_file_t
*dstfp
, int order
,
72 const char *name
, const char *text
,
74 ipp_attribute_t
*suppattr
,
75 ipp_attribute_t
*defattr
, int defval
,
80 * 'cupsAdminCreateWindowsPPD()' - Create the Windows PPD file for a printer.
83 char * /* O - PPD file or NULL */
84 cupsAdminCreateWindowsPPD(
85 http_t
*http
, /* I - Connection to server */
86 const char *dest
, /* I - Printer or class */
87 char *buffer
, /* I - Filename buffer */
88 int bufsize
) /* I - Size of filename buffer */
90 const char *src
; /* Source PPD filename */
91 cups_file_t
*srcfp
, /* Source PPD file */
92 *dstfp
; /* Destination PPD file */
93 ipp_t
*request
, /* IPP request */
94 *response
; /* IPP response */
95 ipp_attribute_t
*suppattr
, /* IPP -supported attribute */
96 *defattr
; /* IPP -default attribute */
97 cups_lang_t
*language
; /* Current language */
98 char line
[256], /* Line from PPD file */
99 junk
[256], /* Extra junk to throw away */
100 *ptr
, /* Pointer into line */
101 uri
[1024], /* Printer URI */
102 option
[41], /* Option */
103 choice
[41]; /* Choice */
104 int jcloption
, /* In a JCL option? */
105 linenum
; /* Current line number */
106 time_t curtime
; /* Current time */
107 struct tm
*curdate
; /* Current date */
108 static const char * const pattrs
[] = /* Printer attributes we want */
110 "job-hold-until-supported",
111 "job-hold-until-default",
112 "job-sheets-supported",
113 "job-sheets-default",
114 "job-priority-supported",
115 "job-priority-default"
120 * Range check the input...
126 if (!http
|| !dest
|| !buffer
|| bufsize
< 2)
130 * Get the PPD file...
133 if ((src
= cupsGetPPD2(http
, dest
)) == NULL
)
137 * Get the supported banner pages, etc. for the printer...
140 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
142 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
143 "localhost", 0, "/printers/%s", dest
);
144 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
145 "printer-uri", NULL
, uri
);
147 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
148 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
152 * Do the request and get back a response...
155 response
= cupsDoRequest(http
, request
, "/");
156 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
163 * Open the original PPD file...
166 if ((srcfp
= cupsFileOpen(src
, "rb")) == NULL
)
170 * Create a temporary output file using the destination buffer...
173 if ((dstfp
= cupsTempFile2(buffer
, bufsize
)) == NULL
)
175 cupsFileClose(srcfp
);
183 * Write a new header explaining that this isn't the original PPD...
186 cupsFilePuts(dstfp
, "*PPD-Adobe: \"4.3\"\n");
188 curtime
= time(NULL
);
189 curdate
= gmtime(&curtime
);
191 cupsFilePrintf(dstfp
, "*%% Modified on %04d%02d%02d%02d%02d%02d+0000 "
192 "for CUPS Windows Driver\n",
193 curdate
->tm_year
+ 1900, curdate
->tm_mon
+ 1, curdate
->tm_mday
,
194 curdate
->tm_hour
, curdate
->tm_min
, curdate
->tm_sec
);
197 * Read the existing PPD file, converting all PJL commands to CUPS
198 * job ticket comments...
203 language
= cupsLangDefault();
205 while (cupsFileGets(srcfp
, line
, sizeof(line
)))
209 if (!strncmp(line
, "*PPD-Adobe:", 11))
212 * Already wrote the PPD header...
217 else if (!strncmp(line
, "*JCLBegin:", 10) ||
218 !strncmp(line
, "*JCLToPSInterpreter:", 20) ||
219 !strncmp(line
, "*JCLEnd:", 8) ||
220 !strncmp(line
, "*Protocols:", 11))
223 * Don't use existing JCL keywords; we'll create our own, below...
226 cupsFilePrintf(dstfp
, "*%% Commented out for CUPS Windows Driver...\n"
227 "*%%%s\n", line
+ 1);
230 else if (!strncmp(line
, "*JCLOpenUI", 10))
233 cupsFilePrintf(dstfp
, "%s\n", line
);
235 else if (!strncmp(line
, "*JCLCloseUI", 11))
238 cupsFilePrintf(dstfp
, "%s\n", line
);
240 else if (jcloption
&&
241 strncmp(line
, "*End", 4) &&
242 strncmp(line
, "*Default", 8) &&
243 strncmp(line
, "*OrderDependency", 16))
245 if ((ptr
= strchr(line
, ':')) == NULL
)
247 snprintf(line
, sizeof(line
),
248 _cupsLangString(language
, _("Missing value on line %d!")),
250 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
252 cupsFileClose(srcfp
);
253 cupsFileClose(dstfp
);
263 if ((ptr
= strchr(ptr
, '\"')) == NULL
)
265 snprintf(line
, sizeof(line
),
266 _cupsLangString(language
,
267 _("Missing double quote on line %d!")),
269 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
271 cupsFileClose(srcfp
);
272 cupsFileClose(dstfp
);
282 if (sscanf(line
, "*%40s%*[ \t]%40[^/]", option
, choice
) != 2)
284 snprintf(line
, sizeof(line
),
285 _cupsLangString(language
,
286 _("Bad option + choice on line %d!")),
288 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
290 cupsFileClose(srcfp
);
291 cupsFileClose(dstfp
);
301 if (strchr(ptr
+ 1, '\"') == NULL
)
307 while (cupsFileGets(srcfp
, junk
, sizeof(junk
)) != NULL
)
311 if (!strncmp(junk
, "*End", 4))
316 snprintf(ptr
+ 1, sizeof(line
) - (ptr
- line
+ 1),
317 "%%cupsJobTicket: %s=%s\n\"\n*End", option
, choice
);
319 cupsFilePrintf(dstfp
, "*%% Changed for CUPS Windows Driver...\n%s\n",
323 cupsFilePrintf(dstfp
, "%s\n", line
);
326 cupsFileClose(srcfp
);
331 snprintf(line
, sizeof(line
),
332 _cupsLangString(language
, _("Empty PPD file!")),
334 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
336 cupsFileClose(dstfp
);
345 * Now add the CUPS-specific attributes and options...
348 cupsFilePuts(dstfp
, "\n*% CUPS Job Ticket support and options...\n");
349 cupsFilePuts(dstfp
, "*Protocols: PJL\n");
350 cupsFilePuts(dstfp
, "*JCLBegin: \"%!PS-Adobe-3.0<0A>\"\n");
351 cupsFilePuts(dstfp
, "*JCLToPSInterpreter: \"\"\n");
352 cupsFilePuts(dstfp
, "*JCLEnd: \"\"\n");
354 cupsFilePuts(dstfp
, "\n*OpenGroup: CUPS/CUPS Options\n\n");
356 if ((defattr
= ippFindAttribute(response
, "job-hold-until-default",
357 IPP_TAG_ZERO
)) != NULL
&&
358 (suppattr
= ippFindAttribute(response
, "job-hold-until-supported",
359 IPP_TAG_ZERO
)) != NULL
)
360 write_option(dstfp
, 10, "cupsJobHoldUntil", "Hold Until", "job-hold-until",
361 suppattr
, defattr
, 0, 1);
363 if ((defattr
= ippFindAttribute(response
, "job-priority-default",
364 IPP_TAG_INTEGER
)) != NULL
&&
365 (suppattr
= ippFindAttribute(response
, "job-priority-supported",
366 IPP_TAG_RANGE
)) != NULL
)
367 write_option(dstfp
, 11, "cupsJobPriority", "Priority", "job-priority",
368 suppattr
, defattr
, 0, 1);
370 if ((defattr
= ippFindAttribute(response
, "job-sheets-default",
371 IPP_TAG_ZERO
)) != NULL
&&
372 (suppattr
= ippFindAttribute(response
, "job-sheets-supported",
373 IPP_TAG_ZERO
)) != NULL
)
375 write_option(dstfp
, 20, "cupsJobSheetsStart", "Start Banner",
376 "job-sheets", suppattr
, defattr
, 0, 2);
377 write_option(dstfp
, 21, "cupsJobSheetsEnd", "End Banner",
378 "job-sheets", suppattr
, defattr
, 1, 2);
381 cupsFilePuts(dstfp
, "*CloseGroup: CUPS\n");
382 cupsFileClose(dstfp
);
391 * 'cupsAdminExportSamba()' - Export a printer to Samba.
394 int /* O - 1 on success, 0 on failure */
395 cupsAdminExportSamba(
396 const char *dest
, /* I - Destination to export */
397 const char *ppd
, /* I - PPD file */
398 const char *samba_server
, /* I - Samba server */
399 const char *samba_user
, /* I - Samba username */
400 const char *samba_password
, /* I - Samba password */
401 FILE *logfile
) /* I - Log file, if any */
403 int status
; /* Status of Samba commands */
404 int have_drivers
; /* Have drivers? */
405 char file
[1024], /* File to test for */
406 authfile
[1024], /* Temporary authentication file */
407 address
[1024], /* Address for command */
408 subcmd
[1024], /* Sub-command */
409 message
[1024]; /* Error message */
410 cups_file_t
*fp
; /* Authentication file */
411 cups_lang_t
*language
; /* Current language */
412 _cups_globals_t
*cg
= _cupsGlobals();
417 * Range check input...
420 if (!dest
|| !ppd
|| !samba_server
|| !samba_user
|| !samba_password
)
422 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
427 * Create a temporary authentication file for Samba...
430 if ((fp
= cupsTempFile2(authfile
, sizeof(authfile
))) == NULL
)
432 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
436 cupsFilePrintf(fp
, "username = %s\n", samba_user
);
437 cupsFilePrintf(fp
, "password = %s\n", samba_password
);
441 * See which drivers are available; the new CUPS v6 and Adobe drivers
442 * depend on the Windows 2k PS driver, so copy that driver first:
453 language
= cupsLangDefault();
455 snprintf(file
, sizeof(file
), "%s/drivers/pscript5.dll", cg
->cups_datadir
);
456 if (!access(file
, 0))
461 * Windows 2k driver is installed; do the smbclient commands needed
462 * to copy the Win2k drivers over...
465 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
467 snprintf(subcmd
, sizeof(subcmd
),
469 "put %s W32X86/%s.ppd;"
470 "put %s/drivers/ps5ui.dll W32X86/ps5ui.dll;"
471 "put %s/drivers/pscript.hlp W32X86/pscript.hlp;"
472 "put %s/drivers/pscript.ntf W32X86/pscript.ntf;"
473 "put %s/drivers/pscript5.dll W32X86/pscript5.dll",
474 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
475 cg
->cups_datadir
, cg
->cups_datadir
);
477 if ((status
= do_samba_command("smbclient", address
, subcmd
,
478 authfile
, logfile
)) != 0)
480 snprintf(message
, sizeof(message
),
481 _cupsLangString(language
,
482 _("Unable to copy Windows 2000 printer "
483 "driver files (%d)!")), status
);
485 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
488 _cupsLangPrintf(logfile
, "%s\n", message
);
496 * See if we also have the CUPS driver files; if so, use them!
499 snprintf(file
, sizeof(file
), "%s/drivers/cupsps6.dll", cg
->cups_datadir
);
500 if (!access(file
, 0))
503 * Copy the CUPS driver files over...
506 snprintf(subcmd
, sizeof(subcmd
),
507 "put %s/drivers/cups6.ini W32X86/cups6.ini;"
508 "put %s/drivers/cupsps6.dll W32X86/cupsps6.dll;"
509 "put %s/drivers/cupsui6.dll W32X86/cupsui6.dll",
510 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
512 if ((status
= do_samba_command("smbclient", address
, subcmd
,
513 authfile
, logfile
)) != 0)
515 snprintf(message
, sizeof(message
),
516 _cupsLangString(language
,
517 _("Unable to copy CUPS printer driver "
518 "files (%d)!")), status
);
520 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
523 _cupsLangPrintf(logfile
, "%s\n", message
);
531 * Do the rpcclient command needed for the CUPS drivers...
534 snprintf(subcmd
, sizeof(subcmd
),
535 "adddriver \"Windows NT x86\" \"%s:"
536 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
537 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
538 "cups6.ini,cupsps6.dll,cupsui6.dll\"",
544 * Don't have the CUPS drivers, so just use the standard Windows
548 snprintf(subcmd
, sizeof(subcmd
),
549 "adddriver \"Windows NT x86\" \"%s:"
550 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
551 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
555 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
556 authfile
, logfile
)) != 0)
558 snprintf(message
, sizeof(message
),
559 _cupsLangString(language
,
560 _("Unable to install Windows 2000 printer "
561 "driver files (%d)!")), status
);
563 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
566 _cupsLangPrintf(logfile
, "%s\n", message
);
574 snprintf(file
, sizeof(file
), "%s/drivers/ADOBEPS4.DRV", cg
->cups_datadir
);
575 if (!access(file
, 0))
580 * Do the smbclient commands needed for the Adobe Win9x drivers...
583 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
585 snprintf(subcmd
, sizeof(subcmd
),
587 "put %s WIN40/%s.PPD;"
588 "put %s/drivers/ADFONTS.MFM WIN40/ADFONTS.MFM;"
589 "put %s/drivers/ADOBEPS4.DRV WIN40/ADOBEPS4.DRV;"
590 "put %s/drivers/ADOBEPS4.HLP WIN40/ADOBEPS4.HLP;"
591 "put %s/drivers/ICONLIB.DLL WIN40/ICONLIB.DLL;"
592 "put %s/drivers/PSMON.DLL WIN40/PSMON.DLL;",
593 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
594 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
596 if ((status
= do_samba_command("smbclient", address
, subcmd
,
597 authfile
, logfile
)) != 0)
599 snprintf(message
, sizeof(message
),
600 _cupsLangString(language
,
601 _("Unable to copy Windows 9x printer "
602 "driver files (%d)!")), status
);
604 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
607 _cupsLangPrintf(logfile
, "%s\n", message
);
615 * Do the rpcclient commands needed for the Adobe Win9x drivers...
618 snprintf(subcmd
, sizeof(subcmd
),
619 "adddriver \"Windows 4.0\" \"%s:ADOBEPS4.DRV:%s.PPD:NULL:"
620 "ADOBEPS4.HLP:PSMON.DLL:RAW:"
621 "ADOBEPS4.DRV,%s.PPD,ADOBEPS4.HLP,PSMON.DLL,ADFONTS.MFM,"
625 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
626 authfile
, logfile
)) != 0)
628 snprintf(message
, sizeof(message
),
629 _cupsLangString(language
,
630 _("Unable to install Windows 9x printer "
631 "driver files (%d)!")), status
);
633 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
636 _cupsLangPrintf(logfile
, "%s\n", message
);
644 if (logfile
&& !(have_drivers
& 1))
648 _cupsLangString(language
,
649 _("No Windows printer drivers are installed!")),
653 _cupsLangString(language
,
654 _("Warning, no Windows 2000 printer drivers "
658 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
659 _cupsLangPrintf(logfile
, "%s\n", message
);
662 if (have_drivers
== 0)
666 * Finally, associate the drivers we just added with the queue...
669 snprintf(subcmd
, sizeof(subcmd
), "setdriver %s %s", dest
, dest
);
671 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
672 authfile
, logfile
)) != 0)
674 snprintf(message
, sizeof(message
),
675 _cupsLangString(language
,
676 _("Unable to set Windows printer driver (%d)!")),
679 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
682 _cupsLangPrintf(logfile
, "%s\n", message
);
696 * 'cupsAdminGetServerSettings()' - Get settings from the server.
698 * The returned settings should be freed with cupsFreeOptions() when
699 * you are done with them.
704 int /* O - 1 on success, 0 on failure */
705 cupsAdminGetServerSettings(
706 http_t
*http
, /* I - Connection to server */
707 int *num_settings
, /* O - Number of settings */
708 cups_option_t
**settings
) /* O - Settings */
710 return (_cupsAdminGetServerSettings(http
, num_settings
, settings
));
715 * '_cupsAdminGetServerSettings()' - Get settings from the server.
717 * The returned settings should be freed with cupsFreeOptions() when
718 * you are done with them.
723 int /* O - 1 on success, 0 on failure */
724 _cupsAdminGetServerSettings(
725 http_t
*http
, /* I - Connection to server */
726 int *num_settings
, /* O - Number of settings */
727 cups_option_t
**settings
) /* O - Settings */
729 int i
; /* Looping var */
730 cups_file_t
*cupsd
; /* cupsd.conf file */
731 char cupsdconf
[1024]; /* cupsd.conf filename */
732 int remote
; /* Remote cupsd.conf file? */
733 http_status_t status
; /* Status of getting cupsd.conf */
734 char line
[1024], /* Line from cupsd.conf file */
735 *value
; /* Value on line */
736 cups_option_t
*setting
; /* Current setting */
737 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
741 * Range check input...
744 if (!http
|| !num_settings
|| !settings
)
746 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
761 * Get the cupsd.conf file...
764 if ((status
= get_cupsd_conf(http
, cg
, cg
->cupsd_update
, cupsdconf
,
765 sizeof(cupsdconf
), &remote
)) == HTTP_OK
)
767 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
769 char message
[1024]; /* Message string */
772 snprintf(message
, sizeof(message
),
773 _cupsLangString(cupsLangDefault(), _("open of %s failed: %s")),
774 cupsdconf
, strerror(errno
));
775 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
784 * Read the file, keeping track of what settings are enabled...
787 int remote_access
= 0, /* Remote access allowed? */
788 remote_admin
= 0, /* Remote administration allowed? */
789 remote_any
= 0, /* Remote access from anywhere allowed? */
790 browsing
= 1, /* Browsing enabled? */
791 browse_allow
= 1, /* Browse address set? */
792 browse_address
= 0, /* Browse address set? */
793 cancel_policy
= 1, /* Cancel-job policy set? */
794 debug_logging
= 0; /* LogLevel debug set? */
795 int linenum
= 0, /* Line number in file */
796 in_location
= 0, /* In a location section? */
797 in_policy
= 0, /* In a policy section? */
798 in_cancel_job
= 0, /* In a cancel-job section? */
799 in_admin_location
= 0; /* In the /admin location? */
802 invalidate_cupsd_cache(cg
);
804 cg
->cupsd_update
= time(NULL
);
805 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
807 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
812 if (!strcasecmp(line
, "Port") || !strcasecmp(line
, "Listen"))
814 char *port
; /* Pointer to port number, if any */
817 if ((port
= strrchr(value
, ':')) != NULL
)
819 else if (isdigit(*value
& 255))
822 * Listen on a port number implies remote access...
829 if (strcasecmp(value
, "localhost") && strcmp(value
, "127.0.0.1")
832 #endif /* AF_LOCAL */
834 && strcmp(value
, "::1")
835 #endif /* AF_INET6 */
839 else if (!strcasecmp(line
, "Browsing"))
841 browsing
= !strcasecmp(value
, "yes") || !strcasecmp(value
, "on") ||
842 !strcasecmp(value
, "true");
844 else if (!strcasecmp(line
, "BrowseAddress"))
848 else if (!strcasecmp(line
, "BrowseAllow"))
852 else if (!strcasecmp(line
, "BrowseOrder"))
854 browse_allow
= !strncasecmp(value
, "deny,", 5);
856 else if (!strcasecmp(line
, "LogLevel"))
858 debug_logging
= !strncasecmp(value
, "debug", 5);
860 else if (!strcasecmp(line
, "<Policy") && !strcasecmp(value
, "default"))
864 else if (!strcasecmp(line
, "</Policy>"))
868 else if (!strcasecmp(line
, "<Limit") && in_policy
)
871 * See if the policy limit is for the Cancel-Job operation...
874 char *valptr
; /* Pointer into value */
879 for (valptr
= value
; !isspace(*valptr
& 255) && *valptr
; valptr
++);
884 if (!strcasecmp(value
, "cancel-job") || !strcasecmp(value
, "all"))
890 for (value
= valptr
; isspace(*value
& 255); value
++);
893 else if (!strcasecmp(line
, "</Limit>"))
897 else if (!strcasecmp(line
, "Require") && in_cancel_job
)
901 else if (!strcasecmp(line
, "<Location"))
903 in_admin_location
= !strcasecmp(value
, "/admin");
906 else if (!strcasecmp(line
, "</Location>"))
908 in_admin_location
= 0;
911 else if (!strcasecmp(line
, "Allow") && in_admin_location
&&
912 strcasecmp(value
, "localhost") && strcasecmp(value
, "127.0.0.1")
915 #endif /* AF_LOCAL */
917 && strcmp(value
, "::1")
918 #endif /* AF_INET6 */
923 if (!strcasecmp(value
, "all"))
926 else if (line
[0] != '<' && !in_location
&& !in_policy
)
927 cg
->cupsd_num_settings
= cupsAddOption(line
, value
,
928 cg
->cupsd_num_settings
,
929 &(cg
->cupsd_settings
));
932 cupsFileClose(cupsd
);
934 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
935 debug_logging
? "1" : "0",
936 cg
->cupsd_num_settings
,
937 &(cg
->cupsd_settings
));
939 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
940 (remote_access
&& remote_admin
) ?
942 cg
->cupsd_num_settings
,
943 &(cg
->cupsd_settings
));
945 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
946 remote_any
? "1" : "0",
947 cg
->cupsd_num_settings
,
948 &(cg
->cupsd_settings
));
950 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
951 (browsing
&& browse_allow
) ?
953 cg
->cupsd_num_settings
,
954 &(cg
->cupsd_settings
));
956 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
957 (remote_access
&& browsing
&&
958 browse_address
) ? "1" : "0",
959 cg
->cupsd_num_settings
,
960 &(cg
->cupsd_settings
));
962 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
963 cancel_policy
? "1" : "0",
964 cg
->cupsd_num_settings
,
965 &(cg
->cupsd_settings
));
967 else if (status
!= HTTP_NOT_MODIFIED
)
968 invalidate_cupsd_cache(cg
);
971 * Remove any temporary files and copy the settings array...
977 for (i
= cg
->cupsd_num_settings
, setting
= cg
->cupsd_settings
;
980 *num_settings
= cupsAddOption(setting
->name
, setting
->value
,
981 *num_settings
, settings
);
983 return (cg
->cupsd_num_settings
> 0);
988 * 'cupsAdminSetServerSettings()' - Set settings on the server.
993 int /* O - 1 on success, 0 on failure */
994 cupsAdminSetServerSettings(
995 http_t
*http
, /* I - Connection to server */
996 int num_settings
, /* I - Number of settings */
997 cups_option_t
*settings
) /* I - Settings */
999 return (_cupsAdminSetServerSettings(http
, num_settings
, settings
));
1004 * '_cupsAdminSetServerSettings()' - Set settings on the server.
1009 int /* O - 1 on success, 0 on failure */
1010 _cupsAdminSetServerSettings(
1011 http_t
*http
, /* I - Connection to server */
1012 int num_settings
, /* I - Number of settings */
1013 cups_option_t
*settings
) /* I - Settings */
1015 int i
; /* Looping var */
1016 http_status_t status
; /* GET/PUT status */
1017 const char *server_port_env
; /* SERVER_PORT env var */
1018 int server_port
; /* IPP port for server */
1019 cups_file_t
*cupsd
; /* cupsd.conf file */
1020 char cupsdconf
[1024]; /* cupsd.conf filename */
1021 int remote
; /* Remote cupsd.conf file? */
1022 char tempfile
[1024]; /* Temporary new cupsd.conf */
1023 cups_file_t
*temp
; /* Temporary file */
1024 char line
[1024], /* Line from cupsd.conf file */
1025 *value
; /* Value on line */
1026 int linenum
, /* Line number in file */
1027 in_location
, /* In a location section? */
1028 in_policy
, /* In a policy section? */
1029 in_default_policy
, /* In the default policy section? */
1030 in_cancel_job
, /* In a cancel-job section? */
1031 in_admin_location
, /* In the /admin location? */
1032 in_conf_location
, /* In the /admin/conf location? */
1033 in_root_location
; /* In the / location? */
1034 const char *val
; /* Setting value */
1035 int remote_printers
, /* Show remote printers */
1036 share_printers
, /* Share local printers */
1037 remote_admin
, /* Remote administration allowed? */
1038 remote_any
, /* Remote access from anywhere? */
1039 user_cancel_any
, /* Cancel-job policy set? */
1040 debug_logging
; /* LogLevel debug set? */
1041 int wrote_port_listen
, /* Wrote the port/listen lines? */
1042 wrote_browsing
, /* Wrote the browsing lines? */
1043 wrote_policy
, /* Wrote the policy? */
1044 wrote_loglevel
, /* Wrote the LogLevel line? */
1045 wrote_admin_location
, /* Wrote the /admin location? */
1046 wrote_conf_location
, /* Wrote the /admin/conf location? */
1047 wrote_root_location
; /* Wrote the / location? */
1048 int indent
; /* Indentation */
1049 int cupsd_num_settings
; /* New number of settings */
1050 int old_remote_printers
, /* Show remote printers */
1051 old_share_printers
, /* Share local printers */
1052 old_remote_admin
, /* Remote administration allowed? */
1053 old_user_cancel_any
, /* Cancel-job policy set? */
1054 old_debug_logging
; /* LogLevel debug set? */
1055 cups_option_t
*cupsd_settings
, /* New settings */
1056 *setting
; /* Current setting */
1057 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1061 * Range check input...
1064 if (!http
|| !num_settings
|| !settings
)
1066 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
1072 * Get the cupsd.conf file...
1075 if ((status
= get_cupsd_conf(http
, cg
, 0, cupsdconf
, sizeof(cupsdconf
),
1076 &remote
)) == HTTP_OK
)
1078 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
1080 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
1088 * Get current settings...
1091 if (!_cupsAdminGetServerSettings(http
, &cupsd_num_settings
,
1095 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, cupsd_num_settings
,
1096 cupsd_settings
)) != NULL
)
1097 old_debug_logging
= atoi(val
);
1099 old_debug_logging
= 0;
1101 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, cupsd_num_settings
,
1102 cupsd_settings
)) != NULL
)
1103 old_remote_admin
= atoi(val
);
1105 old_remote_admin
= 0;
1107 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, cupsd_num_settings
,
1108 cupsd_settings
)) != NULL
)
1109 remote_any
= atoi(val
);
1113 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
, cupsd_num_settings
,
1114 cupsd_settings
)) != NULL
)
1115 old_remote_printers
= atoi(val
);
1117 old_remote_printers
= 1;
1119 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, cupsd_num_settings
,
1120 cupsd_settings
)) != NULL
)
1121 old_share_printers
= atoi(val
);
1123 old_share_printers
= 0;
1125 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, cupsd_num_settings
,
1126 cupsd_settings
)) != NULL
)
1127 old_user_cancel_any
= atoi(val
);
1129 old_user_cancel_any
= 0;
1131 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
1134 * Get basic settings...
1137 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, num_settings
,
1140 debug_logging
= atoi(val
);
1142 if (debug_logging
== old_debug_logging
)
1145 * No change to this setting...
1154 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, num_settings
,
1156 remote_any
= atoi(val
);
1158 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, num_settings
,
1161 remote_admin
= atoi(val
);
1163 if (remote_admin
== old_remote_admin
&& remote_any
< 0)
1166 * No change to this setting...
1175 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
, num_settings
,
1178 remote_printers
= atoi(val
);
1180 if (remote_printers
== old_remote_printers
)
1183 * No change to this setting...
1186 remote_printers
= -1;
1190 remote_printers
= -1;
1192 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, num_settings
,
1195 share_printers
= atoi(val
);
1197 if (share_printers
== old_share_printers
&& remote_any
< 0)
1200 * No change to this setting...
1203 share_printers
= -1;
1207 share_printers
= -1;
1209 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, num_settings
,
1212 user_cancel_any
= atoi(val
);
1214 if (user_cancel_any
== old_user_cancel_any
)
1217 * No change to this setting...
1220 user_cancel_any
= -1;
1224 user_cancel_any
= -1;
1227 * Create a temporary file for the new cupsd.conf file...
1230 if ((temp
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1232 cupsFileClose(cupsd
);
1237 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
1242 * Copy the old file to the new, making changes along the way...
1245 cupsd_num_settings
= 0;
1246 in_admin_location
= 0;
1248 in_conf_location
= 0;
1249 in_default_policy
= 0;
1252 in_root_location
= 0;
1254 wrote_admin_location
= 0;
1256 wrote_conf_location
= 0;
1259 wrote_port_listen
= 0;
1260 wrote_root_location
= 0;
1263 if ((server_port_env
= getenv("SERVER_PORT")) != NULL
)
1265 if ((server_port
= atoi(server_port_env
)) <= 0)
1266 server_port
= ippPort();
1269 server_port
= ippPort();
1271 if (server_port
<= 0)
1272 server_port
= IPP_PORT
;
1274 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
1276 if ((!strcasecmp(line
, "Port") || !strcasecmp(line
, "Listen")) &&
1277 (share_printers
>= 0 || remote_admin
>= 0))
1279 if (!wrote_port_listen
)
1281 wrote_port_listen
= 1;
1283 if (share_printers
> 0 || remote_admin
> 0)
1285 cupsFilePuts(temp
, "# Allow remote access\n");
1286 cupsFilePrintf(temp
, "Port %d\n", server_port
);
1290 cupsFilePuts(temp
, "# Only listen for connections from the local "
1292 cupsFilePrintf(temp
, "Listen localhost:%d\n", server_port
);
1295 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1296 if ((!value
|| strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)) &&
1297 !access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1298 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1299 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1301 else if (value
&& value
[0] == '/'
1302 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1303 && strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)
1304 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1306 cupsFilePrintf(temp
, "Listen %s\n", value
);
1308 else if ((!strcasecmp(line
, "Browsing") ||
1309 !strcasecmp(line
, "BrowseAddress") ||
1310 !strcasecmp(line
, "BrowseAllow") ||
1311 !strcasecmp(line
, "BrowseDeny") ||
1312 !strcasecmp(line
, "BrowseOrder")) &&
1313 (remote_printers
>= 0 || share_printers
>= 0))
1315 if (!wrote_browsing
)
1319 if (remote_printers
> 0 || share_printers
> 0)
1321 if (remote_printers
> 0 && share_printers
> 0)
1323 "# Enable printer sharing and shared printers.\n");
1324 else if (remote_printers
> 0)
1326 "# Show shared printers on the local network.\n");
1329 "# Share local printers on the local network.\n");
1331 cupsFilePuts(temp
, "Browsing On\n");
1332 cupsFilePuts(temp
, "BrowseOrder allow,deny\n");
1334 if (remote_printers
> 0)
1335 cupsFilePuts(temp
, "BrowseAllow all\n");
1337 if (share_printers
> 0)
1338 cupsFilePuts(temp
, "BrowseAddress @LOCAL\n");
1343 "# Disable printer sharing and shared printers.\n");
1344 cupsFilePuts(temp
, "Browsing Off\n");
1348 else if (!strcasecmp(line
, "LogLevel") && debug_logging
>= 0)
1355 "# Show troubleshooting information in error_log.\n");
1356 cupsFilePuts(temp
, "LogLevel debug\n");
1360 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1361 cupsFilePuts(temp
, "LogLevel info\n");
1364 else if (!strcasecmp(line
, "<Policy"))
1366 in_default_policy
= !strcasecmp(value
, "default");
1369 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1372 else if (!strcasecmp(line
, "</Policy>"))
1375 if (!wrote_policy
&& in_default_policy
)
1379 if (!user_cancel_any
)
1380 cupsFilePuts(temp
, " # Only the owner or an administrator can "
1382 " <Limit Cancel-Job>\n"
1383 " Order deny,allow\n"
1390 in_default_policy
= 0;
1392 cupsFilePuts(temp
, "</Policy>\n");
1394 else if (!strcasecmp(line
, "<Location"))
1398 if (!strcmp(value
, "/admin"))
1399 in_admin_location
= 1;
1400 if (!strcmp(value
, "/admin/conf"))
1401 in_conf_location
= 1;
1402 else if (!strcmp(value
, "/"))
1403 in_root_location
= 1;
1405 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1407 else if (!strcasecmp(line
, "</Location>"))
1411 if (in_admin_location
&& remote_admin
>= 0)
1413 wrote_admin_location
= 1;
1416 cupsFilePuts(temp
, " # Allow remote administration...\n");
1417 else if (remote_admin
== 0)
1418 cupsFilePuts(temp
, " # Restrict access to the admin pages...\n");
1420 cupsFilePuts(temp
, " Order allow,deny\n");
1423 cupsFilePrintf(temp
, " Allow %s\n",
1424 remote_any
> 0 ? "all" : "@LOCAL");
1426 cupsFilePuts(temp
, " Allow localhost\n");
1428 else if (in_conf_location
&& remote_admin
>= 0)
1430 wrote_conf_location
= 1;
1433 cupsFilePuts(temp
, " # Allow remote access to the configuration "
1436 cupsFilePuts(temp
, " # Restrict access to the configuration "
1439 cupsFilePuts(temp
, " Order allow,deny\n");
1442 cupsFilePrintf(temp
, " Allow %s\n",
1443 remote_any
> 0 ? "all" : "@LOCAL");
1445 cupsFilePuts(temp
, " Allow localhost\n");
1447 else if (in_root_location
&& (remote_admin
>= 0 || share_printers
>= 0))
1449 wrote_root_location
= 1;
1451 if (remote_admin
> 0 && share_printers
> 0)
1452 cupsFilePuts(temp
, " # Allow shared printing and remote "
1453 "administration...\n");
1454 else if (remote_admin
> 0)
1455 cupsFilePuts(temp
, " # Allow remote administration...\n");
1456 else if (share_printers
> 0)
1457 cupsFilePuts(temp
, " # Allow shared printing...\n");
1459 cupsFilePuts(temp
, " # Restrict access to the server...\n");
1461 cupsFilePuts(temp
, " Order allow,deny\n");
1463 if (remote_admin
> 0 || share_printers
> 0)
1464 cupsFilePrintf(temp
, " Allow %s\n",
1465 remote_any
> 0 ? "all" : "@LOCAL");
1467 cupsFilePuts(temp
, " Allow localhost\n");
1470 in_admin_location
= 0;
1471 in_conf_location
= 0;
1472 in_root_location
= 0;
1474 cupsFilePuts(temp
, "</Location>\n");
1476 else if (!strcasecmp(line
, "<Limit") && in_default_policy
)
1479 * See if the policy limit is for the Cancel-Job operation...
1482 char *valptr
; /* Pointer into value */
1487 if (!strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1490 * Don't write anything for this limit section...
1497 cupsFilePrintf(temp
, " %s", line
);
1501 for (valptr
= value
; !isspace(*valptr
& 255) && *valptr
; valptr
++);
1506 if (!strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1509 * Write everything except for this definition...
1515 cupsFilePrintf(temp
, " %s", value
);
1517 for (value
= valptr
; isspace(*value
& 255); value
++);
1520 cupsFilePuts(temp
, ">\n");
1523 else if (!strcasecmp(line
, "</Limit>") && in_cancel_job
)
1527 if (in_cancel_job
== 1)
1528 cupsFilePuts(temp
, " </Limit>\n");
1532 if (!user_cancel_any
)
1533 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1535 " <Limit Cancel-Job>\n"
1536 " Order deny,allow\n"
1537 " Require user @OWNER @SYSTEM\n"
1542 else if ((((in_admin_location
|| in_conf_location
|| in_root_location
) &&
1543 remote_admin
>= 0) ||
1544 (in_root_location
&& share_printers
>= 0)) &&
1545 (!strcasecmp(line
, "Allow") || !strcasecmp(line
, "Deny") ||
1546 !strcasecmp(line
, "Order")))
1548 else if (in_cancel_job
== 2)
1550 else if (!strcasecmp(line
, "<Limit") && value
)
1551 cupsFilePrintf(temp
, " %s %s>\n", line
, value
);
1552 else if (line
[0] == '<')
1556 cupsFilePrintf(temp
, "%*s%s %s>\n", indent
, "", line
, value
);
1564 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1567 else if (!in_policy
&& !in_location
&&
1568 (val
= cupsGetOption(line
, num_settings
, settings
)) != NULL
)
1571 * Replace this directive's value with the new one...
1574 cupsd_num_settings
= cupsAddOption(line
, val
, cupsd_num_settings
,
1578 * Write the new value in its place, without indentation since we
1579 * only support setting root directives, not in sections...
1582 cupsFilePrintf(temp
, "%s %s\n", line
, val
);
1586 if (!in_policy
&& !in_location
)
1589 * Record the non-policy, non-location directives that we find
1590 * in the server settings, since we cache this info and record it
1591 * in _cupsAdminGetServerSettings()...
1594 cupsd_num_settings
= cupsAddOption(line
, value
, cupsd_num_settings
,
1598 cupsFilePrintf(temp
, "%*s%s %s\n", indent
, "", line
, value
);
1601 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1605 * Write any missing info...
1608 if (!wrote_browsing
&& (remote_printers
>= 0 || share_printers
>= 0))
1610 if (remote_printers
> 0 || share_printers
> 0)
1612 if (remote_printers
> 0 && share_printers
> 0)
1613 cupsFilePuts(temp
, "# Enable printer sharing and shared printers.\n");
1614 else if (remote_printers
> 0)
1615 cupsFilePuts(temp
, "# Show shared printers on the local network.\n");
1617 cupsFilePuts(temp
, "# Share local printers on the local network.\n");
1619 cupsFilePuts(temp
, "Browsing On\n");
1620 cupsFilePuts(temp
, "BrowseOrder allow,deny\n");
1622 if (remote_printers
> 0)
1623 cupsFilePuts(temp
, "BrowseAllow all\n");
1625 if (share_printers
> 0)
1626 cupsFilePuts(temp
, "BrowseAddress @LOCAL\n");
1630 cupsFilePuts(temp
, "# Disable printer sharing and shared printers.\n");
1631 cupsFilePuts(temp
, "Browsing Off\n");
1635 if (!wrote_loglevel
&& debug_logging
>= 0)
1639 cupsFilePuts(temp
, "# Show troubleshooting information in error_log.\n");
1640 cupsFilePuts(temp
, "LogLevel debug\n");
1644 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1645 cupsFilePuts(temp
, "LogLevel info\n");
1649 if (!wrote_port_listen
&& (share_printers
>= 0 || remote_admin
>= 0))
1651 if (share_printers
> 0 || remote_admin
> 0)
1653 cupsFilePuts(temp
, "# Allow remote access\n");
1654 cupsFilePrintf(temp
, "Port %d\n", ippPort());
1659 "# Only listen for connections from the local machine.\n");
1660 cupsFilePrintf(temp
, "Listen localhost:%d\n", ippPort());
1663 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1664 if (!access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1665 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1666 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1669 if (!wrote_root_location
&& (remote_admin
>= 0 || share_printers
>= 0))
1671 if (remote_admin
> 0 && share_printers
> 0)
1673 "# Allow shared printing and remote administration...\n");
1674 else if (remote_admin
> 0)
1675 cupsFilePuts(temp
, "# Allow remote administration...\n");
1676 else if (share_printers
> 0)
1677 cupsFilePuts(temp
, "# Allow shared printing...\n");
1679 cupsFilePuts(temp
, "# Restrict access to the server...\n");
1681 cupsFilePuts(temp
, "<Location />\n"
1682 " Order allow,deny\n");
1684 if (remote_admin
> 0 || share_printers
> 0)
1685 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1687 cupsFilePuts(temp
, " Allow localhost\n");
1689 cupsFilePuts(temp
, "</Location>\n");
1692 if (!wrote_admin_location
&& remote_admin
>= 0)
1695 cupsFilePuts(temp
, "# Allow remote administration...\n");
1697 cupsFilePuts(temp
, "# Restrict access to the admin pages...\n");
1699 cupsFilePuts(temp
, "<Location /admin>\n"
1700 " Order allow,deny\n");
1703 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1705 cupsFilePuts(temp
, " Allow localhost\n");
1707 cupsFilePuts(temp
, "</Location>\n");
1710 if (!wrote_conf_location
&& remote_admin
>= 0)
1714 "# Allow remote access to the configuration files...\n");
1716 cupsFilePuts(temp
, "# Restrict access to the configuration files...\n");
1718 cupsFilePuts(temp
, "<Location /admin/conf>\n"
1720 " Require user @SYSTEM\n"
1721 " Order allow,deny\n");
1724 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1726 cupsFilePuts(temp
, " Allow localhost\n");
1728 cupsFilePuts(temp
, "</Location>\n");
1731 if (!wrote_policy
&& user_cancel_any
>= 0)
1733 cupsFilePuts(temp
, "<Policy default>\n"
1734 " # Job-related operations must be done by the owner "
1735 "or an adminstrator...\n"
1736 " <Limit Send-Document Send-URI Hold-Job Release-Job "
1737 "Restart-Job Purge-Jobs Set-Job-Attributes "
1738 "Create-Job-Subscription Renew-Subscription "
1739 "Cancel-Subscription Get-Notifications Reprocess-Job "
1740 "Cancel-Current-Job Suspend-Current-Job Resume-Job "
1742 " Require user @OWNER @SYSTEM\n"
1743 " Order deny,allow\n"
1745 " # All administration operations require an "
1746 "adminstrator to authenticate...\n"
1747 " <Limit Pause-Printer Resume-Printer "
1748 "Set-Printer-Attributes Enable-Printer "
1749 "Disable-Printer Pause-Printer-After-Current-Job "
1750 "Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer "
1751 "Activate-Printer Restart-Printer Shutdown-Printer "
1752 "Startup-Printer Promote-Job Schedule-Job-After "
1753 "CUPS-Add-Printer CUPS-Delete-Printer "
1754 "CUPS-Add-Class CUPS-Delete-Class "
1755 "CUPS-Accept-Jobs CUPS-Reject-Jobs "
1756 "CUPS-Set-Default CUPS-Add-Device CUPS-Delete-Device>\n"
1758 " Require user @SYSTEM\n"
1759 " Order deny,allow\n"
1762 if (!user_cancel_any
)
1763 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1765 " <Limit Cancel-Job>\n"
1766 " Require user @OWNER @SYSTEM\n"
1767 " Order deny,allow\n"
1770 cupsFilePuts(temp
, " <Limit All>\n"
1771 " Order deny,allow\n"
1776 for (i
= num_settings
, setting
= settings
; i
> 0; i
--, setting
++)
1777 if (setting
->name
[0] != '_' &&
1778 !cupsGetOption(setting
->name
, cupsd_num_settings
, cupsd_settings
))
1781 * Add this directive to the list of directives we have written...
1784 cupsd_num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1785 cupsd_num_settings
, &cupsd_settings
);
1788 * Write the new value, without indentation since we only support
1789 * setting root directives, not in sections...
1792 cupsFilePrintf(temp
, "%s %s\n", setting
->name
, setting
->value
);
1795 cupsFileClose(cupsd
);
1796 cupsFileClose(temp
);
1799 * Upload the configuration file to the server...
1802 status
= cupsPutFile(http
, "/admin/conf/cupsd.conf", tempfile
);
1804 if (status
== HTTP_CREATED
)
1807 * Updated OK, add the basic settings...
1810 if (debug_logging
>= 0)
1811 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1812 debug_logging
? "1" : "0",
1813 cupsd_num_settings
, &cupsd_settings
);
1815 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1816 old_debug_logging
? "1" : "0",
1817 cupsd_num_settings
, &cupsd_settings
);
1819 if (remote_admin
>= 0)
1820 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1821 remote_admin
? "1" : "0",
1822 cupsd_num_settings
, &cupsd_settings
);
1824 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1825 old_remote_admin
? "1" : "0",
1826 cupsd_num_settings
, &cupsd_settings
);
1828 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
1829 remote_any
? "1" : "0",
1830 cupsd_num_settings
, &cupsd_settings
);
1832 if (remote_printers
>= 0)
1833 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
1834 remote_printers
? "1" : "0",
1835 cupsd_num_settings
, &cupsd_settings
);
1837 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
1838 old_remote_printers
? "1" : "0",
1839 cupsd_num_settings
, &cupsd_settings
);
1841 if (share_printers
>= 0)
1842 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1843 share_printers
? "1" : "0",
1844 cupsd_num_settings
, &cupsd_settings
);
1846 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1847 old_share_printers
? "1" : "0",
1848 cupsd_num_settings
, &cupsd_settings
);
1850 if (user_cancel_any
>= 0)
1851 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1852 user_cancel_any
? "1" : "0",
1853 cupsd_num_settings
, &cupsd_settings
);
1855 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1856 old_user_cancel_any
? "1" : "0",
1857 cupsd_num_settings
, &cupsd_settings
);
1860 * Save the new values...
1863 invalidate_cupsd_cache(cg
);
1865 cg
->cupsd_num_settings
= cupsd_num_settings
;
1866 cg
->cupsd_settings
= cupsd_settings
;
1867 cg
->cupsd_update
= time(NULL
);
1869 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
1872 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
1875 * Remote our temp files and return...
1883 return (status
== HTTP_CREATED
);
1888 * 'do_samba_command()' - Do a SAMBA command.
1891 static int /* O - Status of command */
1892 do_samba_command(const char *command
, /* I - Command to run */
1893 const char *address
, /* I - Address for command */
1894 const char *subcmd
, /* I - Sub-command */
1895 const char *authfile
, /* I - Samba authentication file */
1896 FILE *logfile
) /* I - Optional log file */
1899 return (1); /* Always fail on Windows... */
1902 int status
; /* Status of command */
1903 int pid
; /* Process ID of child */
1907 _cupsLangPrintf(logfile
,
1908 _("Running command: %s %s -N -A %s -c \'%s\'\n"),
1909 command
, address
, authfile
, subcmd
);
1911 if ((pid
= fork()) == 0)
1914 * Child goes here, redirect stdin/out/err and execute the command...
1918 open("/dev/null", O_RDONLY
);
1923 dup(fileno(logfile
));
1925 open("/dev/null", O_WRONLY
);
1930 execlp(command
, command
, address
, "-N", "-A", authfile
, "-c", subcmd
,
1939 _cupsLangPrintf(logfile
, _("Unable to run \"%s\": %s\n"),
1940 command
, strerror(errno
));
1945 * Wait for the process to complete...
1948 while (wait(&status
) != pid
);
1952 _cupsLangPuts(logfile
, "\n");
1954 DEBUG_printf(("status=%d\n", status
));
1956 if (WIFEXITED(status
))
1957 return (WEXITSTATUS(status
));
1959 return (-WTERMSIG(status
));
1965 * 'get_cupsd_conf()' - Get the current cupsd.conf file.
1968 static http_status_t
/* O - Status of request */
1970 http_t
*http
, /* I - Connection to server */
1971 _cups_globals_t
*cg
, /* I - Global data */
1972 time_t last_update
, /* I - Last update time for file */
1973 char *name
, /* I - Filename buffer */
1974 int namesize
, /* I - Size of filename buffer */
1975 int *remote
) /* O - Remote file? */
1977 int fd
; /* Temporary file descriptor */
1979 struct stat info
; /* cupsd.conf file information */
1981 http_status_t status
; /* Status of getting cupsd.conf */
1982 char host
[HTTP_MAX_HOST
]; /* Hostname for connection */
1986 * See if we already have the data we need...
1989 httpGetHostname(http
, host
, sizeof(host
));
1991 if (strcasecmp(cg
->cupsd_hostname
, host
))
1992 invalidate_cupsd_cache(cg
);
1994 snprintf(name
, namesize
, "%s/cupsd.conf", cg
->cups_serverroot
);
1998 if (!strcasecmp(host
, "localhost") && !access(name
, R_OK
))
2001 * Read the local file rather than using HTTP...
2004 if (stat(name
, &info
))
2006 char message
[1024]; /* Message string */
2009 snprintf(message
, sizeof(message
),
2010 _cupsLangString(cupsLangDefault(), _("stat of %s failed: %s")),
2011 name
, strerror(errno
));
2012 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
2016 return (HTTP_SERVER_ERROR
);
2018 else if (last_update
&& info
.st_mtime
<= last_update
)
2019 status
= HTTP_NOT_MODIFIED
;
2027 * Read cupsd.conf via a HTTP GET request...
2030 if ((fd
= cupsTempFd(name
, namesize
)) < 0)
2034 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
2036 invalidate_cupsd_cache(cg
);
2038 return (HTTP_SERVER_ERROR
);
2043 httpClearFields(http
);
2046 httpSetField(http
, HTTP_FIELD_IF_MODIFIED_SINCE
,
2047 httpGetDateString(last_update
));
2049 status
= cupsGetFd(http
, "/admin/conf/cupsd.conf", fd
);
2053 if (status
!= HTTP_OK
)
2065 * 'invalidate_cupsd_cache()' - Invalidate the cached cupsd.conf settings.
2069 invalidate_cupsd_cache(
2070 _cups_globals_t
*cg
) /* I - Global data */
2072 cupsFreeOptions(cg
->cupsd_num_settings
, cg
->cupsd_settings
);
2074 cg
->cupsd_hostname
[0] = '\0';
2075 cg
->cupsd_update
= 0;
2076 cg
->cupsd_num_settings
= 0;
2077 cg
->cupsd_settings
= NULL
;
2082 * 'write_option()' - Write a CUPS option to a PPD file.
2086 write_option(cups_file_t
*dstfp
, /* I - PPD file */
2087 int order
, /* I - Order dependency */
2088 const char *name
, /* I - Option name */
2089 const char *text
, /* I - Option text */
2090 const char *attrname
, /* I - Attribute name */
2091 ipp_attribute_t
*suppattr
, /* I - IPP -supported attribute */
2092 ipp_attribute_t
*defattr
, /* I - IPP -default attribute */
2093 int defval
, /* I - Default value number */
2094 int valcount
) /* I - Number of values */
2096 int i
; /* Looping var */
2099 cupsFilePrintf(dstfp
, "*JCLOpenUI *%s/%s: PickOne\n"
2100 "*OrderDependency: %d JCLSetup *%s\n",
2101 name
, text
, order
, name
);
2103 if (defattr
->value_tag
== IPP_TAG_INTEGER
)
2106 * Do numeric options with a range or list...
2109 cupsFilePrintf(dstfp
, "*Default%s: %d\n", name
,
2110 defattr
->values
[defval
].integer
);
2112 if (suppattr
->value_tag
== IPP_TAG_RANGE
)
2115 * List each number in the range...
2118 for (i
= suppattr
->values
[0].range
.lower
;
2119 i
<= suppattr
->values
[0].range
.upper
;
2122 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, i
);
2125 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n",
2127 else if (defval
== 0)
2128 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
, i
);
2129 else if (defval
< (valcount
- 1))
2130 cupsFilePrintf(dstfp
, ",%d\"\n", i
);
2132 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", i
);
2138 * List explicit numbers...
2141 for (i
= 0; i
< suppattr
->num_values
; i
++)
2143 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, suppattr
->values
[i
].integer
);
2146 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n", attrname
,
2147 suppattr
->values
[i
].integer
);
2148 else if (defval
== 0)
2149 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
,
2150 suppattr
->values
[i
].integer
);
2151 else if (defval
< (valcount
- 1))
2152 cupsFilePrintf(dstfp
, ",%d\"\n", suppattr
->values
[i
].integer
);
2154 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", suppattr
->values
[i
].integer
);
2161 * Do text options with a list...
2164 cupsFilePrintf(dstfp
, "*Default%s: %s\n", name
,
2165 defattr
->values
[defval
].string
.text
);
2167 for (i
= 0; i
< suppattr
->num_values
; i
++)
2169 cupsFilePrintf(dstfp
, "*%s %s: \"", name
,
2170 suppattr
->values
[i
].string
.text
);
2173 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\n\"\n*End\n", attrname
,
2174 suppattr
->values
[i
].string
.text
);
2175 else if (defval
== 0)
2176 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\"\n", attrname
,
2177 suppattr
->values
[i
].string
.text
);
2178 else if (defval
< (valcount
- 1))
2179 cupsFilePrintf(dstfp
, ",%s\"\n", suppattr
->values
[i
].string
.text
);
2181 cupsFilePrintf(dstfp
, ",%s\n\"\n*End\n",
2182 suppattr
->values
[i
].string
.text
);
2186 cupsFilePrintf(dstfp
, "*JCLCloseUI: *%s\n\n", name
);
2191 * End of "$Id: adminutil.c 6361 2007-03-19 16:01:28Z mike $".