2 * "$Id: adminutil.c 6435 2007-04-02 22:43:39Z mike $"
4 * Administration utility API definitions for the Common UNIX Printing
7 * Copyright 2001-2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Easy Software Products and are protected by Federal
11 * copyright law. Distribution and use rights are outlined in the file
12 * "LICENSE.txt" which should have been included with this file. If this
13 * file is missing or damaged please contact Easy Software Products
16 * Attn: CUPS Licensing Information
17 * Easy Software Products
18 * 44141 Airport View Drive, Suite 204
19 * Hollywood, Maryland 20636 USA
21 * Voice: (301) 373-9600
22 * EMail: cups-info@cups.org
23 * WWW: http://www.cups.org
25 * This file is subject to the Apple OS-Developed Software exception.
29 * cupsAdminCreateWindowsPPD() - Create the Windows PPD file for a printer.
30 * cupsAdminExportSamba() - Export a printer to Samba.
31 * cupsAdminGetServerSettings() - Get settings from the server.
32 * _cupsAdminGetServerSettings() - Get settings from the server (private).
33 * cupsAdminSetServerSettings() - Set settings on the server.
34 * _cupsAdminSetServerSettings() - Set settings on the server (private).
35 * do_samba_command() - Do a SAMBA command.
36 * get_cupsd_conf() - Get the current cupsd.conf file.
37 * invalidate_cupsd_cache() - Invalidate the cached cupsd.conf settings.
38 * write_option() - Write a CUPS option to a PPD file.
42 * Include necessary headers...
45 #include "adminutil.h"
54 # include <sys/wait.h>
62 extern int _cupsAdminGetServerSettings(http_t
*http
,
64 cups_option_t
**settings
);
65 extern int _cupsAdminSetServerSettings(http_t
*http
,
67 cups_option_t
*settings
);
68 static int do_samba_command(const char *command
,
70 const char *subcommand
,
73 static http_status_t
get_cupsd_conf(http_t
*http
, _cups_globals_t
*cg
,
74 time_t last_update
, char *name
,
75 int namelen
, int *remote
);
76 static void invalidate_cupsd_cache(_cups_globals_t
*cg
);
77 static void write_option(cups_file_t
*dstfp
, int order
,
78 const char *name
, const char *text
,
80 ipp_attribute_t
*suppattr
,
81 ipp_attribute_t
*defattr
, int defval
,
86 * 'cupsAdminCreateWindowsPPD()' - Create the Windows PPD file for a printer.
89 char * /* O - PPD file or NULL */
90 cupsAdminCreateWindowsPPD(
91 http_t
*http
, /* I - Connection to server */
92 const char *dest
, /* I - Printer or class */
93 char *buffer
, /* I - Filename buffer */
94 int bufsize
) /* I - Size of filename buffer */
96 const char *src
; /* Source PPD filename */
97 cups_file_t
*srcfp
, /* Source PPD file */
98 *dstfp
; /* Destination PPD file */
99 ipp_t
*request
, /* IPP request */
100 *response
; /* IPP response */
101 ipp_attribute_t
*suppattr
, /* IPP -supported attribute */
102 *defattr
; /* IPP -default attribute */
103 cups_lang_t
*language
; /* Current language */
104 char line
[256], /* Line from PPD file */
105 junk
[256], /* Extra junk to throw away */
106 *ptr
, /* Pointer into line */
107 uri
[1024], /* Printer URI */
108 option
[41], /* Option */
109 choice
[41]; /* Choice */
110 int jcloption
, /* In a JCL option? */
111 jclorder
, /* Next JCL order dependency */
112 linenum
; /* Current line number */
113 time_t curtime
; /* Current time */
114 struct tm
*curdate
; /* Current date */
115 static const char * const pattrs
[] = /* Printer attributes we want */
117 "job-hold-until-supported",
118 "job-hold-until-default",
119 "job-sheets-supported",
120 "job-sheets-default",
121 "job-priority-supported",
122 "job-priority-default"
127 * Range check the input...
133 if (!http
|| !dest
|| !buffer
|| bufsize
< 2)
137 * Get the PPD file...
140 if ((src
= cupsGetPPD2(http
, dest
)) == NULL
)
144 * Get the supported banner pages, etc. for the printer...
147 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
149 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
150 "localhost", 0, "/printers/%s", dest
);
151 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
152 "printer-uri", NULL
, uri
);
154 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
155 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
159 * Do the request and get back a response...
162 response
= cupsDoRequest(http
, request
, "/");
163 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
170 * Open the original PPD file...
173 if ((srcfp
= cupsFileOpen(src
, "rb")) == NULL
)
177 * Create a temporary output file using the destination buffer...
180 if ((dstfp
= cupsTempFile2(buffer
, bufsize
)) == NULL
)
182 cupsFileClose(srcfp
);
190 * Write a new header explaining that this isn't the original PPD...
193 cupsFilePuts(dstfp
, "*PPD-Adobe: \"4.3\"\n");
195 curtime
= time(NULL
);
196 curdate
= gmtime(&curtime
);
198 cupsFilePrintf(dstfp
, "*%% Modified on %04d%02d%02d%02d%02d%02d+0000 "
199 "for CUPS Windows Driver\n",
200 curdate
->tm_year
+ 1900, curdate
->tm_mon
+ 1, curdate
->tm_mday
,
201 curdate
->tm_hour
, curdate
->tm_min
, curdate
->tm_sec
);
204 * Read the existing PPD file, converting all PJL commands to CUPS
205 * job ticket comments...
211 language
= cupsLangDefault();
213 while (cupsFileGets(srcfp
, line
, sizeof(line
)))
217 if (!strncmp(line
, "*PPD-Adobe:", 11))
220 * Already wrote the PPD header...
225 else if (!strncmp(line
, "*JCLBegin:", 10) ||
226 !strncmp(line
, "*JCLToPSInterpreter:", 20) ||
227 !strncmp(line
, "*JCLEnd:", 8) ||
228 !strncmp(line
, "*Protocols:", 11))
231 * Don't use existing JCL keywords; we'll create our own, below...
234 cupsFilePrintf(dstfp
, "*%% Commented out for CUPS Windows Driver...\n"
235 "*%%%s\n", line
+ 1);
238 else if (!strncmp(line
, "*JCLOpenUI", 10))
241 cupsFilePrintf(dstfp
, "%s\n", line
);
243 else if (!strncmp(line
, "*JCLCloseUI", 11))
246 cupsFilePrintf(dstfp
, "%s\n", line
);
248 else if (jcloption
&& !strncmp(line
, "*OrderDependency:", 17))
250 for (ptr
= line
+ 17; *ptr
&& isspace(*ptr
& 255); ptr
++);
252 ptr
= strchr(ptr
, ' ');
256 cupsFilePrintf(dstfp
, "*OrderDependency: %d%s\n", jclorder
, ptr
);
260 cupsFilePrintf(dstfp
, "%s\n", line
);
262 else if (jcloption
&&
263 strncmp(line
, "*End", 4) &&
264 strncmp(line
, "*Default", 8))
266 if ((ptr
= strchr(line
, ':')) == NULL
)
268 snprintf(line
, sizeof(line
),
269 _cupsLangString(language
, _("Missing value on line %d!")),
271 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
273 cupsFileClose(srcfp
);
274 cupsFileClose(dstfp
);
284 if ((ptr
= strchr(ptr
, '\"')) == NULL
)
286 snprintf(line
, sizeof(line
),
287 _cupsLangString(language
,
288 _("Missing double quote on line %d!")),
290 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
292 cupsFileClose(srcfp
);
293 cupsFileClose(dstfp
);
303 if (sscanf(line
, "*%40s%*[ \t]%40[^/]", option
, choice
) != 2)
305 snprintf(line
, sizeof(line
),
306 _cupsLangString(language
,
307 _("Bad option + choice on line %d!")),
309 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
311 cupsFileClose(srcfp
);
312 cupsFileClose(dstfp
);
322 if (strchr(ptr
+ 1, '\"') == NULL
)
328 while (cupsFileGets(srcfp
, junk
, sizeof(junk
)) != NULL
)
332 if (!strncmp(junk
, "*End", 4))
337 snprintf(ptr
+ 1, sizeof(line
) - (ptr
- line
+ 1),
338 "%%cupsJobTicket: %s=%s\n\"\n*End", option
, choice
);
340 cupsFilePrintf(dstfp
, "*%% Changed for CUPS Windows Driver...\n%s\n",
344 cupsFilePrintf(dstfp
, "%s\n", line
);
347 cupsFileClose(srcfp
);
352 snprintf(line
, sizeof(line
),
353 _cupsLangString(language
, _("Empty PPD file!")),
355 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
357 cupsFileClose(dstfp
);
366 * Now add the CUPS-specific attributes and options...
369 cupsFilePuts(dstfp
, "\n*% CUPS Job Ticket support and options...\n");
370 cupsFilePuts(dstfp
, "*Protocols: PJL\n");
371 cupsFilePuts(dstfp
, "*JCLBegin: \"%!PS-Adobe-3.0<0A>\"\n");
372 cupsFilePuts(dstfp
, "*JCLToPSInterpreter: \"\"\n");
373 cupsFilePuts(dstfp
, "*JCLEnd: \"\"\n");
375 cupsFilePuts(dstfp
, "\n*OpenGroup: CUPS/CUPS Options\n\n");
377 if ((defattr
= ippFindAttribute(response
, "job-hold-until-default",
378 IPP_TAG_ZERO
)) != NULL
&&
379 (suppattr
= ippFindAttribute(response
, "job-hold-until-supported",
380 IPP_TAG_ZERO
)) != NULL
)
381 write_option(dstfp
, jclorder
++, "cupsJobHoldUntil", "Hold Until",
382 "job-hold-until", suppattr
, defattr
, 0, 1);
384 if ((defattr
= ippFindAttribute(response
, "job-priority-default",
385 IPP_TAG_INTEGER
)) != NULL
&&
386 (suppattr
= ippFindAttribute(response
, "job-priority-supported",
387 IPP_TAG_RANGE
)) != NULL
)
388 write_option(dstfp
, jclorder
++, "cupsJobPriority", "Priority",
389 "job-priority", suppattr
, defattr
, 0, 1);
391 if ((defattr
= ippFindAttribute(response
, "job-sheets-default",
392 IPP_TAG_ZERO
)) != NULL
&&
393 (suppattr
= ippFindAttribute(response
, "job-sheets-supported",
394 IPP_TAG_ZERO
)) != NULL
)
396 write_option(dstfp
, jclorder
++, "cupsJobSheetsStart", "Start Banner",
397 "job-sheets", suppattr
, defattr
, 0, 2);
398 write_option(dstfp
, jclorder
++, "cupsJobSheetsEnd", "End Banner",
399 "job-sheets", suppattr
, defattr
, 1, 2);
402 cupsFilePuts(dstfp
, "*CloseGroup: CUPS\n");
403 cupsFileClose(dstfp
);
412 * 'cupsAdminExportSamba()' - Export a printer to Samba.
415 int /* O - 1 on success, 0 on failure */
416 cupsAdminExportSamba(
417 const char *dest
, /* I - Destination to export */
418 const char *ppd
, /* I - PPD file */
419 const char *samba_server
, /* I - Samba server */
420 const char *samba_user
, /* I - Samba username */
421 const char *samba_password
, /* I - Samba password */
422 FILE *logfile
) /* I - Log file, if any */
424 int status
; /* Status of Samba commands */
425 int have_drivers
; /* Have drivers? */
426 char file
[1024], /* File to test for */
427 authfile
[1024], /* Temporary authentication file */
428 address
[1024], /* Address for command */
429 subcmd
[1024], /* Sub-command */
430 message
[1024]; /* Error message */
431 cups_file_t
*fp
; /* Authentication file */
432 cups_lang_t
*language
; /* Current language */
433 _cups_globals_t
*cg
= _cupsGlobals();
438 * Range check input...
441 if (!dest
|| !ppd
|| !samba_server
|| !samba_user
|| !samba_password
)
443 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
448 * Create a temporary authentication file for Samba...
451 if ((fp
= cupsTempFile2(authfile
, sizeof(authfile
))) == NULL
)
453 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
457 cupsFilePrintf(fp
, "username = %s\n", samba_user
);
458 cupsFilePrintf(fp
, "password = %s\n", samba_password
);
462 * See which drivers are available; the new CUPS v6 and Adobe drivers
463 * depend on the Windows 2k PS driver, so copy that driver first:
474 language
= cupsLangDefault();
476 snprintf(file
, sizeof(file
), "%s/drivers/pscript5.dll", cg
->cups_datadir
);
477 if (!access(file
, 0))
482 * Windows 2k driver is installed; do the smbclient commands needed
483 * to copy the Win2k drivers over...
486 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
488 snprintf(subcmd
, sizeof(subcmd
),
490 "put %s W32X86/%s.ppd;"
491 "put %s/drivers/ps5ui.dll W32X86/ps5ui.dll;"
492 "put %s/drivers/pscript.hlp W32X86/pscript.hlp;"
493 "put %s/drivers/pscript.ntf W32X86/pscript.ntf;"
494 "put %s/drivers/pscript5.dll W32X86/pscript5.dll",
495 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
496 cg
->cups_datadir
, cg
->cups_datadir
);
498 if ((status
= do_samba_command("smbclient", address
, subcmd
,
499 authfile
, logfile
)) != 0)
501 snprintf(message
, sizeof(message
),
502 _cupsLangString(language
,
503 _("Unable to copy Windows 2000 printer "
504 "driver files (%d)!")), status
);
506 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
509 _cupsLangPrintf(logfile
, "%s\n", message
);
517 * See if we also have the CUPS driver files; if so, use them!
520 snprintf(file
, sizeof(file
), "%s/drivers/cupsps6.dll", cg
->cups_datadir
);
521 if (!access(file
, 0))
524 * Copy the CUPS driver files over...
527 snprintf(subcmd
, sizeof(subcmd
),
528 "put %s/drivers/cups6.ini W32X86/cups6.ini;"
529 "put %s/drivers/cupsps6.dll W32X86/cupsps6.dll;"
530 "put %s/drivers/cupsui6.dll W32X86/cupsui6.dll",
531 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
533 if ((status
= do_samba_command("smbclient", address
, subcmd
,
534 authfile
, logfile
)) != 0)
536 snprintf(message
, sizeof(message
),
537 _cupsLangString(language
,
538 _("Unable to copy CUPS printer driver "
539 "files (%d)!")), status
);
541 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
544 _cupsLangPrintf(logfile
, "%s\n", message
);
552 * Do the rpcclient command needed for the CUPS drivers...
555 snprintf(subcmd
, sizeof(subcmd
),
556 "adddriver \"Windows NT x86\" \"%s:"
557 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
558 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
559 "cups6.ini,cupsps6.dll,cupsui6.dll\"",
565 * Don't have the CUPS drivers, so just use the standard Windows
569 snprintf(subcmd
, sizeof(subcmd
),
570 "adddriver \"Windows NT x86\" \"%s:"
571 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
572 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
576 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
577 authfile
, logfile
)) != 0)
579 snprintf(message
, sizeof(message
),
580 _cupsLangString(language
,
581 _("Unable to install Windows 2000 printer "
582 "driver files (%d)!")), status
);
584 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
587 _cupsLangPrintf(logfile
, "%s\n", message
);
595 snprintf(file
, sizeof(file
), "%s/drivers/ADOBEPS4.DRV", cg
->cups_datadir
);
596 if (!access(file
, 0))
601 * Do the smbclient commands needed for the Adobe Win9x drivers...
604 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
606 snprintf(subcmd
, sizeof(subcmd
),
608 "put %s WIN40/%s.PPD;"
609 "put %s/drivers/ADFONTS.MFM WIN40/ADFONTS.MFM;"
610 "put %s/drivers/ADOBEPS4.DRV WIN40/ADOBEPS4.DRV;"
611 "put %s/drivers/ADOBEPS4.HLP WIN40/ADOBEPS4.HLP;"
612 "put %s/drivers/ICONLIB.DLL WIN40/ICONLIB.DLL;"
613 "put %s/drivers/PSMON.DLL WIN40/PSMON.DLL;",
614 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
615 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
617 if ((status
= do_samba_command("smbclient", address
, subcmd
,
618 authfile
, logfile
)) != 0)
620 snprintf(message
, sizeof(message
),
621 _cupsLangString(language
,
622 _("Unable to copy Windows 9x printer "
623 "driver files (%d)!")), status
);
625 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
628 _cupsLangPrintf(logfile
, "%s\n", message
);
636 * Do the rpcclient commands needed for the Adobe Win9x drivers...
639 snprintf(subcmd
, sizeof(subcmd
),
640 "adddriver \"Windows 4.0\" \"%s:ADOBEPS4.DRV:%s.PPD:NULL:"
641 "ADOBEPS4.HLP:PSMON.DLL:RAW:"
642 "ADOBEPS4.DRV,%s.PPD,ADOBEPS4.HLP,PSMON.DLL,ADFONTS.MFM,"
646 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
647 authfile
, logfile
)) != 0)
649 snprintf(message
, sizeof(message
),
650 _cupsLangString(language
,
651 _("Unable to install Windows 9x printer "
652 "driver files (%d)!")), status
);
654 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
657 _cupsLangPrintf(logfile
, "%s\n", message
);
665 if (logfile
&& !(have_drivers
& 1))
669 _cupsLangString(language
,
670 _("No Windows printer drivers are installed!")),
674 _cupsLangString(language
,
675 _("Warning, no Windows 2000 printer drivers "
679 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
680 _cupsLangPrintf(logfile
, "%s\n", message
);
683 if (have_drivers
== 0)
687 * Finally, associate the drivers we just added with the queue...
690 snprintf(subcmd
, sizeof(subcmd
), "setdriver %s %s", dest
, dest
);
692 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
693 authfile
, logfile
)) != 0)
695 snprintf(message
, sizeof(message
),
696 _cupsLangString(language
,
697 _("Unable to set Windows printer driver (%d)!")),
700 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
703 _cupsLangPrintf(logfile
, "%s\n", message
);
717 * 'cupsAdminGetServerSettings()' - Get settings from the server.
719 * The returned settings should be freed with cupsFreeOptions() when
720 * you are done with them.
725 int /* O - 1 on success, 0 on failure */
726 cupsAdminGetServerSettings(
727 http_t
*http
, /* I - Connection to server */
728 int *num_settings
, /* O - Number of settings */
729 cups_option_t
**settings
) /* O - Settings */
731 return (_cupsAdminGetServerSettings(http
, num_settings
, settings
));
736 * '_cupsAdminGetServerSettings()' - Get settings from the server.
738 * The returned settings should be freed with cupsFreeOptions() when
739 * you are done with them.
744 int /* O - 1 on success, 0 on failure */
745 _cupsAdminGetServerSettings(
746 http_t
*http
, /* I - Connection to server */
747 int *num_settings
, /* O - Number of settings */
748 cups_option_t
**settings
) /* O - Settings */
750 int i
; /* Looping var */
751 cups_file_t
*cupsd
; /* cupsd.conf file */
752 char cupsdconf
[1024]; /* cupsd.conf filename */
753 int remote
; /* Remote cupsd.conf file? */
754 http_status_t status
; /* Status of getting cupsd.conf */
755 char line
[1024], /* Line from cupsd.conf file */
756 *value
; /* Value on line */
757 cups_option_t
*setting
; /* Current setting */
758 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
762 * Range check input...
765 if (!http
|| !num_settings
|| !settings
)
767 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
782 * Get the cupsd.conf file...
785 if ((status
= get_cupsd_conf(http
, cg
, cg
->cupsd_update
, cupsdconf
,
786 sizeof(cupsdconf
), &remote
)) == HTTP_OK
)
788 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
790 char message
[1024]; /* Message string */
793 snprintf(message
, sizeof(message
),
794 _cupsLangString(cupsLangDefault(), _("open of %s failed: %s")),
795 cupsdconf
, strerror(errno
));
796 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
805 * Read the file, keeping track of what settings are enabled...
808 int remote_access
= 0, /* Remote access allowed? */
809 remote_admin
= 0, /* Remote administration allowed? */
810 remote_any
= 0, /* Remote access from anywhere allowed? */
811 browsing
= 1, /* Browsing enabled? */
812 browse_allow
= 1, /* Browse address set? */
813 browse_address
= 0, /* Browse address set? */
814 cancel_policy
= 1, /* Cancel-job policy set? */
815 debug_logging
= 0; /* LogLevel debug set? */
816 int linenum
= 0, /* Line number in file */
817 in_location
= 0, /* In a location section? */
818 in_policy
= 0, /* In a policy section? */
819 in_cancel_job
= 0, /* In a cancel-job section? */
820 in_admin_location
= 0; /* In the /admin location? */
823 invalidate_cupsd_cache(cg
);
825 cg
->cupsd_update
= time(NULL
);
826 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
828 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
833 if (!strcasecmp(line
, "Port") || !strcasecmp(line
, "Listen"))
835 char *port
; /* Pointer to port number, if any */
838 if ((port
= strrchr(value
, ':')) != NULL
)
840 else if (isdigit(*value
& 255))
843 * Listen on a port number implies remote access...
850 if (strcasecmp(value
, "localhost") && strcmp(value
, "127.0.0.1")
853 #endif /* AF_LOCAL */
855 && strcmp(value
, "::1")
856 #endif /* AF_INET6 */
860 else if (!strcasecmp(line
, "Browsing"))
862 browsing
= !strcasecmp(value
, "yes") || !strcasecmp(value
, "on") ||
863 !strcasecmp(value
, "true");
865 else if (!strcasecmp(line
, "BrowseAddress"))
869 else if (!strcasecmp(line
, "BrowseAllow"))
873 else if (!strcasecmp(line
, "BrowseOrder"))
875 browse_allow
= !strncasecmp(value
, "deny,", 5);
877 else if (!strcasecmp(line
, "LogLevel"))
879 debug_logging
= !strncasecmp(value
, "debug", 5);
881 else if (!strcasecmp(line
, "<Policy") && !strcasecmp(value
, "default"))
885 else if (!strcasecmp(line
, "</Policy>"))
889 else if (!strcasecmp(line
, "<Limit") && in_policy
)
892 * See if the policy limit is for the Cancel-Job operation...
895 char *valptr
; /* Pointer into value */
900 for (valptr
= value
; !isspace(*valptr
& 255) && *valptr
; valptr
++);
905 if (!strcasecmp(value
, "cancel-job") || !strcasecmp(value
, "all"))
911 for (value
= valptr
; isspace(*value
& 255); value
++);
914 else if (!strcasecmp(line
, "</Limit>"))
918 else if (!strcasecmp(line
, "Require") && in_cancel_job
)
922 else if (!strcasecmp(line
, "<Location"))
924 in_admin_location
= !strcasecmp(value
, "/admin");
927 else if (!strcasecmp(line
, "</Location>"))
929 in_admin_location
= 0;
932 else if (!strcasecmp(line
, "Allow") && in_admin_location
&&
933 strcasecmp(value
, "localhost") && strcasecmp(value
, "127.0.0.1")
936 #endif /* AF_LOCAL */
938 && strcmp(value
, "::1")
939 #endif /* AF_INET6 */
944 if (!strcasecmp(value
, "all"))
947 else if (line
[0] != '<' && !in_location
&& !in_policy
)
948 cg
->cupsd_num_settings
= cupsAddOption(line
, value
,
949 cg
->cupsd_num_settings
,
950 &(cg
->cupsd_settings
));
953 cupsFileClose(cupsd
);
955 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
956 debug_logging
? "1" : "0",
957 cg
->cupsd_num_settings
,
958 &(cg
->cupsd_settings
));
960 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
961 (remote_access
&& remote_admin
) ?
963 cg
->cupsd_num_settings
,
964 &(cg
->cupsd_settings
));
966 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
967 remote_any
? "1" : "0",
968 cg
->cupsd_num_settings
,
969 &(cg
->cupsd_settings
));
971 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
972 (browsing
&& browse_allow
) ?
974 cg
->cupsd_num_settings
,
975 &(cg
->cupsd_settings
));
977 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
978 (remote_access
&& browsing
&&
979 browse_address
) ? "1" : "0",
980 cg
->cupsd_num_settings
,
981 &(cg
->cupsd_settings
));
983 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
984 cancel_policy
? "1" : "0",
985 cg
->cupsd_num_settings
,
986 &(cg
->cupsd_settings
));
988 else if (status
!= HTTP_NOT_MODIFIED
)
989 invalidate_cupsd_cache(cg
);
992 * Remove any temporary files and copy the settings array...
998 for (i
= cg
->cupsd_num_settings
, setting
= cg
->cupsd_settings
;
1001 *num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1002 *num_settings
, settings
);
1004 return (cg
->cupsd_num_settings
> 0);
1009 * 'cupsAdminSetServerSettings()' - Set settings on the server.
1014 int /* O - 1 on success, 0 on failure */
1015 cupsAdminSetServerSettings(
1016 http_t
*http
, /* I - Connection to server */
1017 int num_settings
, /* I - Number of settings */
1018 cups_option_t
*settings
) /* I - Settings */
1020 return (_cupsAdminSetServerSettings(http
, num_settings
, settings
));
1025 * '_cupsAdminSetServerSettings()' - Set settings on the server.
1030 int /* O - 1 on success, 0 on failure */
1031 _cupsAdminSetServerSettings(
1032 http_t
*http
, /* I - Connection to server */
1033 int num_settings
, /* I - Number of settings */
1034 cups_option_t
*settings
) /* I - Settings */
1036 int i
; /* Looping var */
1037 http_status_t status
; /* GET/PUT status */
1038 const char *server_port_env
; /* SERVER_PORT env var */
1039 int server_port
; /* IPP port for server */
1040 cups_file_t
*cupsd
; /* cupsd.conf file */
1041 char cupsdconf
[1024]; /* cupsd.conf filename */
1042 int remote
; /* Remote cupsd.conf file? */
1043 char tempfile
[1024]; /* Temporary new cupsd.conf */
1044 cups_file_t
*temp
; /* Temporary file */
1045 char line
[1024], /* Line from cupsd.conf file */
1046 *value
; /* Value on line */
1047 int linenum
, /* Line number in file */
1048 in_location
, /* In a location section? */
1049 in_policy
, /* In a policy section? */
1050 in_default_policy
, /* In the default policy section? */
1051 in_cancel_job
, /* In a cancel-job section? */
1052 in_admin_location
, /* In the /admin location? */
1053 in_conf_location
, /* In the /admin/conf location? */
1054 in_root_location
; /* In the / location? */
1055 const char *val
; /* Setting value */
1056 int remote_printers
, /* Show remote printers */
1057 share_printers
, /* Share local printers */
1058 remote_admin
, /* Remote administration allowed? */
1059 remote_any
, /* Remote access from anywhere? */
1060 user_cancel_any
, /* Cancel-job policy set? */
1061 debug_logging
; /* LogLevel debug set? */
1062 int wrote_port_listen
, /* Wrote the port/listen lines? */
1063 wrote_browsing
, /* Wrote the browsing lines? */
1064 wrote_policy
, /* Wrote the policy? */
1065 wrote_loglevel
, /* Wrote the LogLevel line? */
1066 wrote_admin_location
, /* Wrote the /admin location? */
1067 wrote_conf_location
, /* Wrote the /admin/conf location? */
1068 wrote_root_location
; /* Wrote the / location? */
1069 int indent
; /* Indentation */
1070 int cupsd_num_settings
; /* New number of settings */
1071 int old_remote_printers
, /* Show remote printers */
1072 old_share_printers
, /* Share local printers */
1073 old_remote_admin
, /* Remote administration allowed? */
1074 old_user_cancel_any
, /* Cancel-job policy set? */
1075 old_debug_logging
; /* LogLevel debug set? */
1076 cups_option_t
*cupsd_settings
, /* New settings */
1077 *setting
; /* Current setting */
1078 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1082 * Range check input...
1085 if (!http
|| !num_settings
|| !settings
)
1087 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
1093 * Get the cupsd.conf file...
1096 if ((status
= get_cupsd_conf(http
, cg
, 0, cupsdconf
, sizeof(cupsdconf
),
1097 &remote
)) == HTTP_OK
)
1099 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
1101 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
1109 * Get current settings...
1112 if (!_cupsAdminGetServerSettings(http
, &cupsd_num_settings
,
1116 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, cupsd_num_settings
,
1117 cupsd_settings
)) != NULL
)
1118 old_debug_logging
= atoi(val
);
1120 old_debug_logging
= 0;
1122 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, cupsd_num_settings
,
1123 cupsd_settings
)) != NULL
)
1124 old_remote_admin
= atoi(val
);
1126 old_remote_admin
= 0;
1128 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, cupsd_num_settings
,
1129 cupsd_settings
)) != NULL
)
1130 remote_any
= atoi(val
);
1134 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
, cupsd_num_settings
,
1135 cupsd_settings
)) != NULL
)
1136 old_remote_printers
= atoi(val
);
1138 old_remote_printers
= 1;
1140 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, cupsd_num_settings
,
1141 cupsd_settings
)) != NULL
)
1142 old_share_printers
= atoi(val
);
1144 old_share_printers
= 0;
1146 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, cupsd_num_settings
,
1147 cupsd_settings
)) != NULL
)
1148 old_user_cancel_any
= atoi(val
);
1150 old_user_cancel_any
= 0;
1152 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
1155 * Get basic settings...
1158 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, num_settings
,
1161 debug_logging
= atoi(val
);
1163 if (debug_logging
== old_debug_logging
)
1166 * No change to this setting...
1175 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, num_settings
,
1177 remote_any
= atoi(val
);
1179 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, num_settings
,
1182 remote_admin
= atoi(val
);
1184 if (remote_admin
== old_remote_admin
&& remote_any
< 0)
1187 * No change to this setting...
1196 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
, num_settings
,
1199 remote_printers
= atoi(val
);
1201 if (remote_printers
== old_remote_printers
)
1204 * No change to this setting...
1207 remote_printers
= -1;
1211 remote_printers
= -1;
1213 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, num_settings
,
1216 share_printers
= atoi(val
);
1218 if (share_printers
== old_share_printers
&& remote_any
< 0)
1221 * No change to this setting...
1224 share_printers
= -1;
1228 share_printers
= -1;
1230 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, num_settings
,
1233 user_cancel_any
= atoi(val
);
1235 if (user_cancel_any
== old_user_cancel_any
)
1238 * No change to this setting...
1241 user_cancel_any
= -1;
1245 user_cancel_any
= -1;
1248 * Create a temporary file for the new cupsd.conf file...
1251 if ((temp
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1253 cupsFileClose(cupsd
);
1258 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
1263 * Copy the old file to the new, making changes along the way...
1266 cupsd_num_settings
= 0;
1267 in_admin_location
= 0;
1269 in_conf_location
= 0;
1270 in_default_policy
= 0;
1273 in_root_location
= 0;
1275 wrote_admin_location
= 0;
1277 wrote_conf_location
= 0;
1280 wrote_port_listen
= 0;
1281 wrote_root_location
= 0;
1284 if ((server_port_env
= getenv("SERVER_PORT")) != NULL
)
1286 if ((server_port
= atoi(server_port_env
)) <= 0)
1287 server_port
= ippPort();
1290 server_port
= ippPort();
1292 if (server_port
<= 0)
1293 server_port
= IPP_PORT
;
1295 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
1297 if ((!strcasecmp(line
, "Port") || !strcasecmp(line
, "Listen")) &&
1298 (share_printers
>= 0 || remote_admin
>= 0))
1300 if (!wrote_port_listen
)
1302 wrote_port_listen
= 1;
1304 if (share_printers
> 0 || remote_admin
> 0)
1306 cupsFilePuts(temp
, "# Allow remote access\n");
1307 cupsFilePrintf(temp
, "Port %d\n", server_port
);
1311 cupsFilePuts(temp
, "# Only listen for connections from the local "
1313 cupsFilePrintf(temp
, "Listen localhost:%d\n", server_port
);
1316 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1317 if ((!value
|| strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)) &&
1318 !access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1319 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1320 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1322 else if (value
&& value
[0] == '/'
1323 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1324 && strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)
1325 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1327 cupsFilePrintf(temp
, "Listen %s\n", value
);
1329 else if ((!strcasecmp(line
, "Browsing") ||
1330 !strcasecmp(line
, "BrowseAddress") ||
1331 !strcasecmp(line
, "BrowseAllow") ||
1332 !strcasecmp(line
, "BrowseDeny") ||
1333 !strcasecmp(line
, "BrowseOrder")) &&
1334 (remote_printers
>= 0 || share_printers
>= 0))
1336 if (!wrote_browsing
)
1340 if (remote_printers
> 0 || share_printers
> 0)
1342 if (remote_printers
> 0 && share_printers
> 0)
1344 "# Enable printer sharing and shared printers.\n");
1345 else if (remote_printers
> 0)
1347 "# Show shared printers on the local network.\n");
1350 "# Share local printers on the local network.\n");
1352 cupsFilePuts(temp
, "Browsing On\n");
1353 cupsFilePuts(temp
, "BrowseOrder allow,deny\n");
1355 if (remote_printers
> 0)
1356 cupsFilePuts(temp
, "BrowseAllow all\n");
1358 if (share_printers
> 0)
1359 cupsFilePuts(temp
, "BrowseAddress @LOCAL\n");
1364 "# Disable printer sharing and shared printers.\n");
1365 cupsFilePuts(temp
, "Browsing Off\n");
1369 else if (!strcasecmp(line
, "LogLevel") && debug_logging
>= 0)
1376 "# Show troubleshooting information in error_log.\n");
1377 cupsFilePuts(temp
, "LogLevel debug\n");
1381 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1382 cupsFilePuts(temp
, "LogLevel info\n");
1385 else if (!strcasecmp(line
, "<Policy"))
1387 in_default_policy
= !strcasecmp(value
, "default");
1390 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1393 else if (!strcasecmp(line
, "</Policy>"))
1396 if (!wrote_policy
&& in_default_policy
)
1400 if (!user_cancel_any
)
1401 cupsFilePuts(temp
, " # Only the owner or an administrator can "
1403 " <Limit Cancel-Job>\n"
1404 " Order deny,allow\n"
1411 in_default_policy
= 0;
1413 cupsFilePuts(temp
, "</Policy>\n");
1415 else if (!strcasecmp(line
, "<Location"))
1419 if (!strcmp(value
, "/admin"))
1420 in_admin_location
= 1;
1421 if (!strcmp(value
, "/admin/conf"))
1422 in_conf_location
= 1;
1423 else if (!strcmp(value
, "/"))
1424 in_root_location
= 1;
1426 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1428 else if (!strcasecmp(line
, "</Location>"))
1432 if (in_admin_location
&& remote_admin
>= 0)
1434 wrote_admin_location
= 1;
1437 cupsFilePuts(temp
, " # Allow remote administration...\n");
1438 else if (remote_admin
== 0)
1439 cupsFilePuts(temp
, " # Restrict access to the admin pages...\n");
1441 cupsFilePuts(temp
, " Order allow,deny\n");
1444 cupsFilePrintf(temp
, " Allow %s\n",
1445 remote_any
> 0 ? "all" : "@LOCAL");
1447 cupsFilePuts(temp
, " Allow localhost\n");
1449 else if (in_conf_location
&& remote_admin
>= 0)
1451 wrote_conf_location
= 1;
1454 cupsFilePuts(temp
, " # Allow remote access to the configuration "
1457 cupsFilePuts(temp
, " # Restrict access to the configuration "
1460 cupsFilePuts(temp
, " Order allow,deny\n");
1463 cupsFilePrintf(temp
, " Allow %s\n",
1464 remote_any
> 0 ? "all" : "@LOCAL");
1466 cupsFilePuts(temp
, " Allow localhost\n");
1468 else if (in_root_location
&& (remote_admin
>= 0 || share_printers
>= 0))
1470 wrote_root_location
= 1;
1472 if (remote_admin
> 0 && share_printers
> 0)
1473 cupsFilePuts(temp
, " # Allow shared printing and remote "
1474 "administration...\n");
1475 else if (remote_admin
> 0)
1476 cupsFilePuts(temp
, " # Allow remote administration...\n");
1477 else if (share_printers
> 0)
1478 cupsFilePuts(temp
, " # Allow shared printing...\n");
1480 cupsFilePuts(temp
, " # Restrict access to the server...\n");
1482 cupsFilePuts(temp
, " Order allow,deny\n");
1484 if (remote_admin
> 0 || share_printers
> 0)
1485 cupsFilePrintf(temp
, " Allow %s\n",
1486 remote_any
> 0 ? "all" : "@LOCAL");
1488 cupsFilePuts(temp
, " Allow localhost\n");
1491 in_admin_location
= 0;
1492 in_conf_location
= 0;
1493 in_root_location
= 0;
1495 cupsFilePuts(temp
, "</Location>\n");
1497 else if (!strcasecmp(line
, "<Limit") && in_default_policy
)
1500 * See if the policy limit is for the Cancel-Job operation...
1503 char *valptr
; /* Pointer into value */
1508 if (!strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1511 * Don't write anything for this limit section...
1518 cupsFilePrintf(temp
, " %s", line
);
1522 for (valptr
= value
; !isspace(*valptr
& 255) && *valptr
; valptr
++);
1527 if (!strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1530 * Write everything except for this definition...
1536 cupsFilePrintf(temp
, " %s", value
);
1538 for (value
= valptr
; isspace(*value
& 255); value
++);
1541 cupsFilePuts(temp
, ">\n");
1544 else if (!strcasecmp(line
, "</Limit>") && in_cancel_job
)
1548 if (in_cancel_job
== 1)
1549 cupsFilePuts(temp
, " </Limit>\n");
1553 if (!user_cancel_any
)
1554 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1556 " <Limit Cancel-Job>\n"
1557 " Order deny,allow\n"
1558 " Require user @OWNER @SYSTEM\n"
1563 else if ((((in_admin_location
|| in_conf_location
|| in_root_location
) &&
1564 remote_admin
>= 0) ||
1565 (in_root_location
&& share_printers
>= 0)) &&
1566 (!strcasecmp(line
, "Allow") || !strcasecmp(line
, "Deny") ||
1567 !strcasecmp(line
, "Order")))
1569 else if (in_cancel_job
== 2)
1571 else if (!strcasecmp(line
, "<Limit") && value
)
1572 cupsFilePrintf(temp
, " %s %s>\n", line
, value
);
1573 else if (line
[0] == '<')
1577 cupsFilePrintf(temp
, "%*s%s %s>\n", indent
, "", line
, value
);
1585 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1588 else if (!in_policy
&& !in_location
&&
1589 (val
= cupsGetOption(line
, num_settings
, settings
)) != NULL
)
1592 * Replace this directive's value with the new one...
1595 cupsd_num_settings
= cupsAddOption(line
, val
, cupsd_num_settings
,
1599 * Write the new value in its place, without indentation since we
1600 * only support setting root directives, not in sections...
1603 cupsFilePrintf(temp
, "%s %s\n", line
, val
);
1607 if (!in_policy
&& !in_location
)
1610 * Record the non-policy, non-location directives that we find
1611 * in the server settings, since we cache this info and record it
1612 * in _cupsAdminGetServerSettings()...
1615 cupsd_num_settings
= cupsAddOption(line
, value
, cupsd_num_settings
,
1619 cupsFilePrintf(temp
, "%*s%s %s\n", indent
, "", line
, value
);
1622 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1626 * Write any missing info...
1629 if (!wrote_browsing
&& (remote_printers
>= 0 || share_printers
>= 0))
1631 if (remote_printers
> 0 || share_printers
> 0)
1633 if (remote_printers
> 0 && share_printers
> 0)
1634 cupsFilePuts(temp
, "# Enable printer sharing and shared printers.\n");
1635 else if (remote_printers
> 0)
1636 cupsFilePuts(temp
, "# Show shared printers on the local network.\n");
1638 cupsFilePuts(temp
, "# Share local printers on the local network.\n");
1640 cupsFilePuts(temp
, "Browsing On\n");
1641 cupsFilePuts(temp
, "BrowseOrder allow,deny\n");
1643 if (remote_printers
> 0)
1644 cupsFilePuts(temp
, "BrowseAllow all\n");
1646 if (share_printers
> 0)
1647 cupsFilePuts(temp
, "BrowseAddress @LOCAL\n");
1651 cupsFilePuts(temp
, "# Disable printer sharing and shared printers.\n");
1652 cupsFilePuts(temp
, "Browsing Off\n");
1656 if (!wrote_loglevel
&& debug_logging
>= 0)
1660 cupsFilePuts(temp
, "# Show troubleshooting information in error_log.\n");
1661 cupsFilePuts(temp
, "LogLevel debug\n");
1665 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1666 cupsFilePuts(temp
, "LogLevel info\n");
1670 if (!wrote_port_listen
&& (share_printers
>= 0 || remote_admin
>= 0))
1672 if (share_printers
> 0 || remote_admin
> 0)
1674 cupsFilePuts(temp
, "# Allow remote access\n");
1675 cupsFilePrintf(temp
, "Port %d\n", ippPort());
1680 "# Only listen for connections from the local machine.\n");
1681 cupsFilePrintf(temp
, "Listen localhost:%d\n", ippPort());
1684 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1685 if (!access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1686 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1687 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1690 if (!wrote_root_location
&& (remote_admin
>= 0 || share_printers
>= 0))
1692 if (remote_admin
> 0 && share_printers
> 0)
1694 "# Allow shared printing and remote administration...\n");
1695 else if (remote_admin
> 0)
1696 cupsFilePuts(temp
, "# Allow remote administration...\n");
1697 else if (share_printers
> 0)
1698 cupsFilePuts(temp
, "# Allow shared printing...\n");
1700 cupsFilePuts(temp
, "# Restrict access to the server...\n");
1702 cupsFilePuts(temp
, "<Location />\n"
1703 " Order allow,deny\n");
1705 if (remote_admin
> 0 || share_printers
> 0)
1706 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1708 cupsFilePuts(temp
, " Allow localhost\n");
1710 cupsFilePuts(temp
, "</Location>\n");
1713 if (!wrote_admin_location
&& remote_admin
>= 0)
1716 cupsFilePuts(temp
, "# Allow remote administration...\n");
1718 cupsFilePuts(temp
, "# Restrict access to the admin pages...\n");
1720 cupsFilePuts(temp
, "<Location /admin>\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_conf_location
&& remote_admin
>= 0)
1735 "# Allow remote access to the configuration files...\n");
1737 cupsFilePuts(temp
, "# Restrict access to the configuration files...\n");
1739 cupsFilePuts(temp
, "<Location /admin/conf>\n"
1741 " Require user @SYSTEM\n"
1742 " Order allow,deny\n");
1745 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1747 cupsFilePuts(temp
, " Allow localhost\n");
1749 cupsFilePuts(temp
, "</Location>\n");
1752 if (!wrote_policy
&& user_cancel_any
>= 0)
1754 cupsFilePuts(temp
, "<Policy default>\n"
1755 " # Job-related operations must be done by the owner "
1756 "or an adminstrator...\n"
1757 " <Limit Send-Document Send-URI Hold-Job Release-Job "
1758 "Restart-Job Purge-Jobs Set-Job-Attributes "
1759 "Create-Job-Subscription Renew-Subscription "
1760 "Cancel-Subscription Get-Notifications Reprocess-Job "
1761 "Cancel-Current-Job Suspend-Current-Job Resume-Job "
1763 " Require user @OWNER @SYSTEM\n"
1764 " Order deny,allow\n"
1766 " # All administration operations require an "
1767 "adminstrator to authenticate...\n"
1768 " <Limit Pause-Printer Resume-Printer "
1769 "Set-Printer-Attributes Enable-Printer "
1770 "Disable-Printer Pause-Printer-After-Current-Job "
1771 "Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer "
1772 "Activate-Printer Restart-Printer Shutdown-Printer "
1773 "Startup-Printer Promote-Job Schedule-Job-After "
1774 "CUPS-Add-Printer CUPS-Delete-Printer "
1775 "CUPS-Add-Class CUPS-Delete-Class "
1776 "CUPS-Accept-Jobs CUPS-Reject-Jobs "
1777 "CUPS-Set-Default CUPS-Add-Device CUPS-Delete-Device>\n"
1779 " Require user @SYSTEM\n"
1780 " Order deny,allow\n"
1783 if (!user_cancel_any
)
1784 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1786 " <Limit Cancel-Job>\n"
1787 " Require user @OWNER @SYSTEM\n"
1788 " Order deny,allow\n"
1791 cupsFilePuts(temp
, " <Limit All>\n"
1792 " Order deny,allow\n"
1797 for (i
= num_settings
, setting
= settings
; i
> 0; i
--, setting
++)
1798 if (setting
->name
[0] != '_' &&
1799 !cupsGetOption(setting
->name
, cupsd_num_settings
, cupsd_settings
))
1802 * Add this directive to the list of directives we have written...
1805 cupsd_num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1806 cupsd_num_settings
, &cupsd_settings
);
1809 * Write the new value, without indentation since we only support
1810 * setting root directives, not in sections...
1813 cupsFilePrintf(temp
, "%s %s\n", setting
->name
, setting
->value
);
1816 cupsFileClose(cupsd
);
1817 cupsFileClose(temp
);
1820 * Upload the configuration file to the server...
1823 status
= cupsPutFile(http
, "/admin/conf/cupsd.conf", tempfile
);
1825 if (status
== HTTP_CREATED
)
1828 * Updated OK, add the basic settings...
1831 if (debug_logging
>= 0)
1832 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1833 debug_logging
? "1" : "0",
1834 cupsd_num_settings
, &cupsd_settings
);
1836 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1837 old_debug_logging
? "1" : "0",
1838 cupsd_num_settings
, &cupsd_settings
);
1840 if (remote_admin
>= 0)
1841 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1842 remote_admin
? "1" : "0",
1843 cupsd_num_settings
, &cupsd_settings
);
1845 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1846 old_remote_admin
? "1" : "0",
1847 cupsd_num_settings
, &cupsd_settings
);
1849 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
1850 remote_any
? "1" : "0",
1851 cupsd_num_settings
, &cupsd_settings
);
1853 if (remote_printers
>= 0)
1854 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
1855 remote_printers
? "1" : "0",
1856 cupsd_num_settings
, &cupsd_settings
);
1858 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
1859 old_remote_printers
? "1" : "0",
1860 cupsd_num_settings
, &cupsd_settings
);
1862 if (share_printers
>= 0)
1863 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1864 share_printers
? "1" : "0",
1865 cupsd_num_settings
, &cupsd_settings
);
1867 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1868 old_share_printers
? "1" : "0",
1869 cupsd_num_settings
, &cupsd_settings
);
1871 if (user_cancel_any
>= 0)
1872 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1873 user_cancel_any
? "1" : "0",
1874 cupsd_num_settings
, &cupsd_settings
);
1876 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1877 old_user_cancel_any
? "1" : "0",
1878 cupsd_num_settings
, &cupsd_settings
);
1881 * Save the new values...
1884 invalidate_cupsd_cache(cg
);
1886 cg
->cupsd_num_settings
= cupsd_num_settings
;
1887 cg
->cupsd_settings
= cupsd_settings
;
1888 cg
->cupsd_update
= time(NULL
);
1890 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
1893 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
1896 * Remote our temp files and return...
1904 return (status
== HTTP_CREATED
);
1909 * 'do_samba_command()' - Do a SAMBA command.
1912 static int /* O - Status of command */
1913 do_samba_command(const char *command
, /* I - Command to run */
1914 const char *address
, /* I - Address for command */
1915 const char *subcmd
, /* I - Sub-command */
1916 const char *authfile
, /* I - Samba authentication file */
1917 FILE *logfile
) /* I - Optional log file */
1920 return (1); /* Always fail on Windows... */
1923 int status
; /* Status of command */
1924 int pid
; /* Process ID of child */
1928 _cupsLangPrintf(logfile
,
1929 _("Running command: %s %s -N -A %s -c \'%s\'\n"),
1930 command
, address
, authfile
, subcmd
);
1932 if ((pid
= fork()) == 0)
1935 * Child goes here, redirect stdin/out/err and execute the command...
1939 open("/dev/null", O_RDONLY
);
1944 dup(fileno(logfile
));
1946 open("/dev/null", O_WRONLY
);
1951 execlp(command
, command
, address
, "-N", "-A", authfile
, "-c", subcmd
,
1960 _cupsLangPrintf(logfile
, _("Unable to run \"%s\": %s\n"),
1961 command
, strerror(errno
));
1966 * Wait for the process to complete...
1969 while (wait(&status
) != pid
);
1973 _cupsLangPuts(logfile
, "\n");
1975 DEBUG_printf(("status=%d\n", status
));
1977 if (WIFEXITED(status
))
1978 return (WEXITSTATUS(status
));
1980 return (-WTERMSIG(status
));
1986 * 'get_cupsd_conf()' - Get the current cupsd.conf file.
1989 static http_status_t
/* O - Status of request */
1991 http_t
*http
, /* I - Connection to server */
1992 _cups_globals_t
*cg
, /* I - Global data */
1993 time_t last_update
, /* I - Last update time for file */
1994 char *name
, /* I - Filename buffer */
1995 int namesize
, /* I - Size of filename buffer */
1996 int *remote
) /* O - Remote file? */
1998 int fd
; /* Temporary file descriptor */
2000 struct stat info
; /* cupsd.conf file information */
2002 http_status_t status
; /* Status of getting cupsd.conf */
2003 char host
[HTTP_MAX_HOST
]; /* Hostname for connection */
2007 * See if we already have the data we need...
2010 httpGetHostname(http
, host
, sizeof(host
));
2012 if (strcasecmp(cg
->cupsd_hostname
, host
))
2013 invalidate_cupsd_cache(cg
);
2015 snprintf(name
, namesize
, "%s/cupsd.conf", cg
->cups_serverroot
);
2019 if (!strcasecmp(host
, "localhost") && !access(name
, R_OK
))
2022 * Read the local file rather than using HTTP...
2025 if (stat(name
, &info
))
2027 char message
[1024]; /* Message string */
2030 snprintf(message
, sizeof(message
),
2031 _cupsLangString(cupsLangDefault(), _("stat of %s failed: %s")),
2032 name
, strerror(errno
));
2033 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
2037 return (HTTP_SERVER_ERROR
);
2039 else if (last_update
&& info
.st_mtime
<= last_update
)
2040 status
= HTTP_NOT_MODIFIED
;
2048 * Read cupsd.conf via a HTTP GET request...
2051 if ((fd
= cupsTempFd(name
, namesize
)) < 0)
2055 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
2057 invalidate_cupsd_cache(cg
);
2059 return (HTTP_SERVER_ERROR
);
2064 httpClearFields(http
);
2067 httpSetField(http
, HTTP_FIELD_IF_MODIFIED_SINCE
,
2068 httpGetDateString(last_update
));
2070 status
= cupsGetFd(http
, "/admin/conf/cupsd.conf", fd
);
2074 if (status
!= HTTP_OK
)
2086 * 'invalidate_cupsd_cache()' - Invalidate the cached cupsd.conf settings.
2090 invalidate_cupsd_cache(
2091 _cups_globals_t
*cg
) /* I - Global data */
2093 cupsFreeOptions(cg
->cupsd_num_settings
, cg
->cupsd_settings
);
2095 cg
->cupsd_hostname
[0] = '\0';
2096 cg
->cupsd_update
= 0;
2097 cg
->cupsd_num_settings
= 0;
2098 cg
->cupsd_settings
= NULL
;
2103 * 'write_option()' - Write a CUPS option to a PPD file.
2107 write_option(cups_file_t
*dstfp
, /* I - PPD file */
2108 int order
, /* I - Order dependency */
2109 const char *name
, /* I - Option name */
2110 const char *text
, /* I - Option text */
2111 const char *attrname
, /* I - Attribute name */
2112 ipp_attribute_t
*suppattr
, /* I - IPP -supported attribute */
2113 ipp_attribute_t
*defattr
, /* I - IPP -default attribute */
2114 int defval
, /* I - Default value number */
2115 int valcount
) /* I - Number of values */
2117 int i
; /* Looping var */
2120 cupsFilePrintf(dstfp
, "*JCLOpenUI *%s/%s: PickOne\n"
2121 "*OrderDependency: %d JCLSetup *%s\n",
2122 name
, text
, order
, name
);
2124 if (defattr
->value_tag
== IPP_TAG_INTEGER
)
2127 * Do numeric options with a range or list...
2130 cupsFilePrintf(dstfp
, "*Default%s: %d\n", name
,
2131 defattr
->values
[defval
].integer
);
2133 if (suppattr
->value_tag
== IPP_TAG_RANGE
)
2136 * List each number in the range...
2139 for (i
= suppattr
->values
[0].range
.lower
;
2140 i
<= suppattr
->values
[0].range
.upper
;
2143 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, i
);
2146 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n",
2148 else if (defval
== 0)
2149 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
, i
);
2150 else if (defval
< (valcount
- 1))
2151 cupsFilePrintf(dstfp
, ",%d\"\n", i
);
2153 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", i
);
2159 * List explicit numbers...
2162 for (i
= 0; i
< suppattr
->num_values
; i
++)
2164 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, suppattr
->values
[i
].integer
);
2167 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n", attrname
,
2168 suppattr
->values
[i
].integer
);
2169 else if (defval
== 0)
2170 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
,
2171 suppattr
->values
[i
].integer
);
2172 else if (defval
< (valcount
- 1))
2173 cupsFilePrintf(dstfp
, ",%d\"\n", suppattr
->values
[i
].integer
);
2175 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", suppattr
->values
[i
].integer
);
2182 * Do text options with a list...
2185 cupsFilePrintf(dstfp
, "*Default%s: %s\n", name
,
2186 defattr
->values
[defval
].string
.text
);
2188 for (i
= 0; i
< suppattr
->num_values
; i
++)
2190 cupsFilePrintf(dstfp
, "*%s %s: \"", name
,
2191 suppattr
->values
[i
].string
.text
);
2194 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\n\"\n*End\n", attrname
,
2195 suppattr
->values
[i
].string
.text
);
2196 else if (defval
== 0)
2197 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\"\n", attrname
,
2198 suppattr
->values
[i
].string
.text
);
2199 else if (defval
< (valcount
- 1))
2200 cupsFilePrintf(dstfp
, ",%s\"\n", suppattr
->values
[i
].string
.text
);
2202 cupsFilePrintf(dstfp
, ",%s\n\"\n*End\n",
2203 suppattr
->values
[i
].string
.text
);
2207 cupsFilePrintf(dstfp
, "*JCLCloseUI: *%s\n\n", name
);
2212 * End of "$Id: adminutil.c 6435 2007-04-02 22:43:39Z mike $".