2 * "$Id: adminutil.c 6549 2007-06-04 15:32:58Z 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
)
1338 int new_remote_printers
= (remote_printers
> 0 ||
1339 (remote_printers
== -1 &&
1340 old_remote_printers
> 0));
1341 int new_share_printers
= (share_printers
> 0 ||
1342 (share_printers
== -1 &&
1343 old_share_printers
> 0));
1347 if (new_remote_printers
|| new_share_printers
)
1349 if (new_remote_printers
&& new_share_printers
)
1351 "# Enable printer sharing and shared printers.\n");
1352 else if (new_remote_printers
)
1354 "# Show shared printers on the local network.\n");
1357 "# Share local printers on the local network.\n");
1359 cupsFilePuts(temp
, "Browsing On\n");
1360 cupsFilePuts(temp
, "BrowseOrder allow,deny\n");
1362 if (new_remote_printers
)
1363 cupsFilePuts(temp
, "BrowseAllow all\n");
1365 if (new_share_printers
)
1366 cupsFilePuts(temp
, "BrowseAddress @LOCAL\n");
1371 "# Disable printer sharing and shared printers.\n");
1372 cupsFilePuts(temp
, "Browsing Off\n");
1376 else if (!strcasecmp(line
, "LogLevel") && debug_logging
>= 0)
1383 "# Show troubleshooting information in error_log.\n");
1384 cupsFilePuts(temp
, "LogLevel debug\n");
1388 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1389 cupsFilePuts(temp
, "LogLevel info\n");
1392 else if (!strcasecmp(line
, "<Policy"))
1394 in_default_policy
= !strcasecmp(value
, "default");
1397 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1400 else if (!strcasecmp(line
, "</Policy>"))
1403 if (!wrote_policy
&& in_default_policy
)
1407 if (!user_cancel_any
)
1408 cupsFilePuts(temp
, " # Only the owner or an administrator can "
1410 " <Limit Cancel-Job>\n"
1411 " Order deny,allow\n"
1418 in_default_policy
= 0;
1420 cupsFilePuts(temp
, "</Policy>\n");
1422 else if (!strcasecmp(line
, "<Location"))
1426 if (!strcmp(value
, "/admin"))
1427 in_admin_location
= 1;
1428 if (!strcmp(value
, "/admin/conf"))
1429 in_conf_location
= 1;
1430 else if (!strcmp(value
, "/"))
1431 in_root_location
= 1;
1433 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1435 else if (!strcasecmp(line
, "</Location>"))
1439 if (in_admin_location
&& remote_admin
>= 0)
1441 wrote_admin_location
= 1;
1444 cupsFilePuts(temp
, " # Allow remote administration...\n");
1445 else if (remote_admin
== 0)
1446 cupsFilePuts(temp
, " # Restrict access to the admin pages...\n");
1448 cupsFilePuts(temp
, " Order allow,deny\n");
1451 cupsFilePrintf(temp
, " Allow %s\n",
1452 remote_any
> 0 ? "all" : "@LOCAL");
1454 cupsFilePuts(temp
, " Allow localhost\n");
1456 else if (in_conf_location
&& remote_admin
>= 0)
1458 wrote_conf_location
= 1;
1461 cupsFilePuts(temp
, " # Allow remote access to the configuration "
1464 cupsFilePuts(temp
, " # Restrict access to the configuration "
1467 cupsFilePuts(temp
, " Order allow,deny\n");
1470 cupsFilePrintf(temp
, " Allow %s\n",
1471 remote_any
> 0 ? "all" : "@LOCAL");
1473 cupsFilePuts(temp
, " Allow localhost\n");
1475 else if (in_root_location
&& (remote_admin
>= 0 || share_printers
>= 0))
1477 wrote_root_location
= 1;
1479 if (remote_admin
> 0 && share_printers
> 0)
1480 cupsFilePuts(temp
, " # Allow shared printing and remote "
1481 "administration...\n");
1482 else if (remote_admin
> 0)
1483 cupsFilePuts(temp
, " # Allow remote administration...\n");
1484 else if (share_printers
> 0)
1485 cupsFilePuts(temp
, " # Allow shared printing...\n");
1487 cupsFilePuts(temp
, " # Restrict access to the server...\n");
1489 cupsFilePuts(temp
, " Order allow,deny\n");
1491 if (remote_admin
> 0 || share_printers
> 0)
1492 cupsFilePrintf(temp
, " Allow %s\n",
1493 remote_any
> 0 ? "all" : "@LOCAL");
1495 cupsFilePuts(temp
, " Allow localhost\n");
1498 in_admin_location
= 0;
1499 in_conf_location
= 0;
1500 in_root_location
= 0;
1502 cupsFilePuts(temp
, "</Location>\n");
1504 else if (!strcasecmp(line
, "<Limit") && in_default_policy
)
1507 * See if the policy limit is for the Cancel-Job operation...
1510 char *valptr
; /* Pointer into value */
1515 if (!strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1518 * Don't write anything for this limit section...
1525 cupsFilePrintf(temp
, " %s", line
);
1529 for (valptr
= value
; !isspace(*valptr
& 255) && *valptr
; valptr
++);
1534 if (!strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1537 * Write everything except for this definition...
1543 cupsFilePrintf(temp
, " %s", value
);
1545 for (value
= valptr
; isspace(*value
& 255); value
++);
1548 cupsFilePuts(temp
, ">\n");
1551 else if (!strcasecmp(line
, "</Limit>") && in_cancel_job
)
1555 if (in_cancel_job
== 1)
1556 cupsFilePuts(temp
, " </Limit>\n");
1560 if (!user_cancel_any
)
1561 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1563 " <Limit Cancel-Job>\n"
1564 " Order deny,allow\n"
1565 " Require user @OWNER @SYSTEM\n"
1570 else if ((((in_admin_location
|| in_conf_location
|| in_root_location
) &&
1571 remote_admin
>= 0) ||
1572 (in_root_location
&& share_printers
>= 0)) &&
1573 (!strcasecmp(line
, "Allow") || !strcasecmp(line
, "Deny") ||
1574 !strcasecmp(line
, "Order")))
1576 else if (in_cancel_job
== 2)
1578 else if (!strcasecmp(line
, "<Limit") && value
)
1579 cupsFilePrintf(temp
, " %s %s>\n", line
, value
);
1580 else if (line
[0] == '<')
1584 cupsFilePrintf(temp
, "%*s%s %s>\n", indent
, "", line
, value
);
1592 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1595 else if (!in_policy
&& !in_location
&&
1596 (val
= cupsGetOption(line
, num_settings
, settings
)) != NULL
)
1599 * Replace this directive's value with the new one...
1602 cupsd_num_settings
= cupsAddOption(line
, val
, cupsd_num_settings
,
1606 * Write the new value in its place, without indentation since we
1607 * only support setting root directives, not in sections...
1610 cupsFilePrintf(temp
, "%s %s\n", line
, val
);
1614 if (!in_policy
&& !in_location
)
1617 * Record the non-policy, non-location directives that we find
1618 * in the server settings, since we cache this info and record it
1619 * in _cupsAdminGetServerSettings()...
1622 cupsd_num_settings
= cupsAddOption(line
, value
, cupsd_num_settings
,
1626 cupsFilePrintf(temp
, "%*s%s %s\n", indent
, "", line
, value
);
1629 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1633 * Write any missing info...
1636 if (!wrote_browsing
&& (remote_printers
>= 0 || share_printers
>= 0))
1638 if (remote_printers
> 0 || share_printers
> 0)
1640 if (remote_printers
> 0 && share_printers
> 0)
1641 cupsFilePuts(temp
, "# Enable printer sharing and shared printers.\n");
1642 else if (remote_printers
> 0)
1643 cupsFilePuts(temp
, "# Show shared printers on the local network.\n");
1645 cupsFilePuts(temp
, "# Share local printers on the local network.\n");
1647 cupsFilePuts(temp
, "Browsing On\n");
1648 cupsFilePuts(temp
, "BrowseOrder allow,deny\n");
1650 if (remote_printers
> 0)
1651 cupsFilePuts(temp
, "BrowseAllow all\n");
1653 if (share_printers
> 0)
1654 cupsFilePuts(temp
, "BrowseAddress @LOCAL\n");
1658 cupsFilePuts(temp
, "# Disable printer sharing and shared printers.\n");
1659 cupsFilePuts(temp
, "Browsing Off\n");
1663 if (!wrote_loglevel
&& debug_logging
>= 0)
1667 cupsFilePuts(temp
, "# Show troubleshooting information in error_log.\n");
1668 cupsFilePuts(temp
, "LogLevel debug\n");
1672 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1673 cupsFilePuts(temp
, "LogLevel info\n");
1677 if (!wrote_port_listen
&& (share_printers
>= 0 || remote_admin
>= 0))
1679 if (share_printers
> 0 || remote_admin
> 0)
1681 cupsFilePuts(temp
, "# Allow remote access\n");
1682 cupsFilePrintf(temp
, "Port %d\n", ippPort());
1687 "# Only listen for connections from the local machine.\n");
1688 cupsFilePrintf(temp
, "Listen localhost:%d\n", ippPort());
1691 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1692 if (!access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1693 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1694 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1697 if (!wrote_root_location
&& (remote_admin
>= 0 || share_printers
>= 0))
1699 if (remote_admin
> 0 && share_printers
> 0)
1701 "# Allow shared printing and remote administration...\n");
1702 else if (remote_admin
> 0)
1703 cupsFilePuts(temp
, "# Allow remote administration...\n");
1704 else if (share_printers
> 0)
1705 cupsFilePuts(temp
, "# Allow shared printing...\n");
1707 cupsFilePuts(temp
, "# Restrict access to the server...\n");
1709 cupsFilePuts(temp
, "<Location />\n"
1710 " Order allow,deny\n");
1712 if (remote_admin
> 0 || share_printers
> 0)
1713 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1715 cupsFilePuts(temp
, " Allow localhost\n");
1717 cupsFilePuts(temp
, "</Location>\n");
1720 if (!wrote_admin_location
&& remote_admin
>= 0)
1723 cupsFilePuts(temp
, "# Allow remote administration...\n");
1725 cupsFilePuts(temp
, "# Restrict access to the admin pages...\n");
1727 cupsFilePuts(temp
, "<Location /admin>\n"
1728 " Order allow,deny\n");
1731 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1733 cupsFilePuts(temp
, " Allow localhost\n");
1735 cupsFilePuts(temp
, "</Location>\n");
1738 if (!wrote_conf_location
&& remote_admin
>= 0)
1742 "# Allow remote access to the configuration files...\n");
1744 cupsFilePuts(temp
, "# Restrict access to the configuration files...\n");
1746 cupsFilePuts(temp
, "<Location /admin/conf>\n"
1748 " Require user @SYSTEM\n"
1749 " Order allow,deny\n");
1752 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1754 cupsFilePuts(temp
, " Allow localhost\n");
1756 cupsFilePuts(temp
, "</Location>\n");
1759 if (!wrote_policy
&& user_cancel_any
>= 0)
1761 cupsFilePuts(temp
, "<Policy default>\n"
1762 " # Job-related operations must be done by the owner "
1763 "or an adminstrator...\n"
1764 " <Limit Send-Document Send-URI Hold-Job Release-Job "
1765 "Restart-Job Purge-Jobs Set-Job-Attributes "
1766 "Create-Job-Subscription Renew-Subscription "
1767 "Cancel-Subscription Get-Notifications Reprocess-Job "
1768 "Cancel-Current-Job Suspend-Current-Job Resume-Job "
1770 " Require user @OWNER @SYSTEM\n"
1771 " Order deny,allow\n"
1773 " # All administration operations require an "
1774 "adminstrator to authenticate...\n"
1775 " <Limit Pause-Printer Resume-Printer "
1776 "Set-Printer-Attributes Enable-Printer "
1777 "Disable-Printer Pause-Printer-After-Current-Job "
1778 "Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer "
1779 "Activate-Printer Restart-Printer Shutdown-Printer "
1780 "Startup-Printer Promote-Job Schedule-Job-After "
1781 "CUPS-Add-Printer CUPS-Delete-Printer "
1782 "CUPS-Add-Class CUPS-Delete-Class "
1783 "CUPS-Accept-Jobs CUPS-Reject-Jobs "
1784 "CUPS-Set-Default CUPS-Add-Device CUPS-Delete-Device>\n"
1786 " Require user @SYSTEM\n"
1787 " Order deny,allow\n"
1790 if (!user_cancel_any
)
1791 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1793 " <Limit Cancel-Job>\n"
1794 " Require user @OWNER @SYSTEM\n"
1795 " Order deny,allow\n"
1798 cupsFilePuts(temp
, " <Limit All>\n"
1799 " Order deny,allow\n"
1804 for (i
= num_settings
, setting
= settings
; i
> 0; i
--, setting
++)
1805 if (setting
->name
[0] != '_' &&
1806 !cupsGetOption(setting
->name
, cupsd_num_settings
, cupsd_settings
))
1809 * Add this directive to the list of directives we have written...
1812 cupsd_num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1813 cupsd_num_settings
, &cupsd_settings
);
1816 * Write the new value, without indentation since we only support
1817 * setting root directives, not in sections...
1820 cupsFilePrintf(temp
, "%s %s\n", setting
->name
, setting
->value
);
1823 cupsFileClose(cupsd
);
1824 cupsFileClose(temp
);
1827 * Upload the configuration file to the server...
1830 status
= cupsPutFile(http
, "/admin/conf/cupsd.conf", tempfile
);
1832 if (status
== HTTP_CREATED
)
1835 * Updated OK, add the basic settings...
1838 if (debug_logging
>= 0)
1839 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1840 debug_logging
? "1" : "0",
1841 cupsd_num_settings
, &cupsd_settings
);
1843 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1844 old_debug_logging
? "1" : "0",
1845 cupsd_num_settings
, &cupsd_settings
);
1847 if (remote_admin
>= 0)
1848 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1849 remote_admin
? "1" : "0",
1850 cupsd_num_settings
, &cupsd_settings
);
1852 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1853 old_remote_admin
? "1" : "0",
1854 cupsd_num_settings
, &cupsd_settings
);
1856 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
1857 remote_any
? "1" : "0",
1858 cupsd_num_settings
, &cupsd_settings
);
1860 if (remote_printers
>= 0)
1861 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
1862 remote_printers
? "1" : "0",
1863 cupsd_num_settings
, &cupsd_settings
);
1865 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
1866 old_remote_printers
? "1" : "0",
1867 cupsd_num_settings
, &cupsd_settings
);
1869 if (share_printers
>= 0)
1870 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1871 share_printers
? "1" : "0",
1872 cupsd_num_settings
, &cupsd_settings
);
1874 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1875 old_share_printers
? "1" : "0",
1876 cupsd_num_settings
, &cupsd_settings
);
1878 if (user_cancel_any
>= 0)
1879 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1880 user_cancel_any
? "1" : "0",
1881 cupsd_num_settings
, &cupsd_settings
);
1883 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1884 old_user_cancel_any
? "1" : "0",
1885 cupsd_num_settings
, &cupsd_settings
);
1888 * Save the new values...
1891 invalidate_cupsd_cache(cg
);
1893 cg
->cupsd_num_settings
= cupsd_num_settings
;
1894 cg
->cupsd_settings
= cupsd_settings
;
1895 cg
->cupsd_update
= time(NULL
);
1897 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
1900 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
1903 * Remote our temp files and return...
1911 return (status
== HTTP_CREATED
);
1916 * 'do_samba_command()' - Do a SAMBA command.
1919 static int /* O - Status of command */
1920 do_samba_command(const char *command
, /* I - Command to run */
1921 const char *address
, /* I - Address for command */
1922 const char *subcmd
, /* I - Sub-command */
1923 const char *authfile
, /* I - Samba authentication file */
1924 FILE *logfile
) /* I - Optional log file */
1927 return (1); /* Always fail on Windows... */
1930 int status
; /* Status of command */
1931 int pid
; /* Process ID of child */
1935 _cupsLangPrintf(logfile
,
1936 _("Running command: %s %s -N -A %s -c \'%s\'\n"),
1937 command
, address
, authfile
, subcmd
);
1939 if ((pid
= fork()) == 0)
1942 * Child goes here, redirect stdin/out/err and execute the command...
1946 open("/dev/null", O_RDONLY
);
1951 dup(fileno(logfile
));
1953 open("/dev/null", O_WRONLY
);
1958 execlp(command
, command
, address
, "-N", "-A", authfile
, "-c", subcmd
,
1967 _cupsLangPrintf(logfile
, _("Unable to run \"%s\": %s\n"),
1968 command
, strerror(errno
));
1973 * Wait for the process to complete...
1976 while (wait(&status
) != pid
);
1980 _cupsLangPuts(logfile
, "\n");
1982 DEBUG_printf(("status=%d\n", status
));
1984 if (WIFEXITED(status
))
1985 return (WEXITSTATUS(status
));
1987 return (-WTERMSIG(status
));
1993 * 'get_cupsd_conf()' - Get the current cupsd.conf file.
1996 static http_status_t
/* O - Status of request */
1998 http_t
*http
, /* I - Connection to server */
1999 _cups_globals_t
*cg
, /* I - Global data */
2000 time_t last_update
, /* I - Last update time for file */
2001 char *name
, /* I - Filename buffer */
2002 int namesize
, /* I - Size of filename buffer */
2003 int *remote
) /* O - Remote file? */
2005 int fd
; /* Temporary file descriptor */
2007 struct stat info
; /* cupsd.conf file information */
2009 http_status_t status
; /* Status of getting cupsd.conf */
2010 char host
[HTTP_MAX_HOST
]; /* Hostname for connection */
2014 * See if we already have the data we need...
2017 httpGetHostname(http
, host
, sizeof(host
));
2019 if (strcasecmp(cg
->cupsd_hostname
, host
))
2020 invalidate_cupsd_cache(cg
);
2022 snprintf(name
, namesize
, "%s/cupsd.conf", cg
->cups_serverroot
);
2026 if (!strcasecmp(host
, "localhost") && !access(name
, R_OK
))
2029 * Read the local file rather than using HTTP...
2032 if (stat(name
, &info
))
2034 char message
[1024]; /* Message string */
2037 snprintf(message
, sizeof(message
),
2038 _cupsLangString(cupsLangDefault(), _("stat of %s failed: %s")),
2039 name
, strerror(errno
));
2040 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
2044 return (HTTP_SERVER_ERROR
);
2046 else if (last_update
&& info
.st_mtime
<= last_update
)
2047 status
= HTTP_NOT_MODIFIED
;
2055 * Read cupsd.conf via a HTTP GET request...
2058 if ((fd
= cupsTempFd(name
, namesize
)) < 0)
2062 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
2064 invalidate_cupsd_cache(cg
);
2066 return (HTTP_SERVER_ERROR
);
2071 httpClearFields(http
);
2074 httpSetField(http
, HTTP_FIELD_IF_MODIFIED_SINCE
,
2075 httpGetDateString(last_update
));
2077 status
= cupsGetFd(http
, "/admin/conf/cupsd.conf", fd
);
2081 if (status
!= HTTP_OK
)
2093 * 'invalidate_cupsd_cache()' - Invalidate the cached cupsd.conf settings.
2097 invalidate_cupsd_cache(
2098 _cups_globals_t
*cg
) /* I - Global data */
2100 cupsFreeOptions(cg
->cupsd_num_settings
, cg
->cupsd_settings
);
2102 cg
->cupsd_hostname
[0] = '\0';
2103 cg
->cupsd_update
= 0;
2104 cg
->cupsd_num_settings
= 0;
2105 cg
->cupsd_settings
= NULL
;
2110 * 'write_option()' - Write a CUPS option to a PPD file.
2114 write_option(cups_file_t
*dstfp
, /* I - PPD file */
2115 int order
, /* I - Order dependency */
2116 const char *name
, /* I - Option name */
2117 const char *text
, /* I - Option text */
2118 const char *attrname
, /* I - Attribute name */
2119 ipp_attribute_t
*suppattr
, /* I - IPP -supported attribute */
2120 ipp_attribute_t
*defattr
, /* I - IPP -default attribute */
2121 int defval
, /* I - Default value number */
2122 int valcount
) /* I - Number of values */
2124 int i
; /* Looping var */
2127 cupsFilePrintf(dstfp
, "*JCLOpenUI *%s/%s: PickOne\n"
2128 "*OrderDependency: %d JCLSetup *%s\n",
2129 name
, text
, order
, name
);
2131 if (defattr
->value_tag
== IPP_TAG_INTEGER
)
2134 * Do numeric options with a range or list...
2137 cupsFilePrintf(dstfp
, "*Default%s: %d\n", name
,
2138 defattr
->values
[defval
].integer
);
2140 if (suppattr
->value_tag
== IPP_TAG_RANGE
)
2143 * List each number in the range...
2146 for (i
= suppattr
->values
[0].range
.lower
;
2147 i
<= suppattr
->values
[0].range
.upper
;
2150 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, i
);
2153 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n",
2155 else if (defval
== 0)
2156 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
, i
);
2157 else if (defval
< (valcount
- 1))
2158 cupsFilePrintf(dstfp
, ",%d\"\n", i
);
2160 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", i
);
2166 * List explicit numbers...
2169 for (i
= 0; i
< suppattr
->num_values
; i
++)
2171 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, suppattr
->values
[i
].integer
);
2174 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n", attrname
,
2175 suppattr
->values
[i
].integer
);
2176 else if (defval
== 0)
2177 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
,
2178 suppattr
->values
[i
].integer
);
2179 else if (defval
< (valcount
- 1))
2180 cupsFilePrintf(dstfp
, ",%d\"\n", suppattr
->values
[i
].integer
);
2182 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", suppattr
->values
[i
].integer
);
2189 * Do text options with a list...
2192 cupsFilePrintf(dstfp
, "*Default%s: %s\n", name
,
2193 defattr
->values
[defval
].string
.text
);
2195 for (i
= 0; i
< suppattr
->num_values
; i
++)
2197 cupsFilePrintf(dstfp
, "*%s %s: \"", name
,
2198 suppattr
->values
[i
].string
.text
);
2201 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\n\"\n*End\n", attrname
,
2202 suppattr
->values
[i
].string
.text
);
2203 else if (defval
== 0)
2204 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\"\n", attrname
,
2205 suppattr
->values
[i
].string
.text
);
2206 else if (defval
< (valcount
- 1))
2207 cupsFilePrintf(dstfp
, ",%s\"\n", suppattr
->values
[i
].string
.text
);
2209 cupsFilePrintf(dstfp
, ",%s\n\"\n*End\n",
2210 suppattr
->values
[i
].string
.text
);
2214 cupsFilePrintf(dstfp
, "*JCLCloseUI: *%s\n\n", name
);
2219 * End of "$Id: adminutil.c 6549 2007-06-04 15:32:58Z mike $".