2 * "$Id: adminutil.c 7850 2008-08-20 00:07:25Z mike $"
4 * Administration utility API definitions for CUPS.
6 * Copyright 2007-2010 by Apple Inc.
7 * Copyright 2001-2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * cupsAdminCreateWindowsPPD() - Create the Windows PPD file for a printer.
20 * cupsAdminExportSamba() - Export a printer to Samba.
21 * cupsAdminGetServerSettings() - Get settings from the server.
22 * _cupsAdminGetServerSettings() - Get settings from the server (private).
23 * cupsAdminSetServerSettings() - Set settings on the server.
24 * _cupsAdminSetServerSettings() - Set settings on the server (private).
25 * do_samba_command() - Do a SAMBA command.
26 * get_cupsd_conf() - Get the current cupsd.conf file.
27 * invalidate_cupsd_cache() - Invalidate the cached cupsd.conf settings.
28 * write_option() - Write a CUPS option to a PPD file.
32 * Include necessary headers...
35 #include "cups-private.h"
36 #include "adminutil.h"
42 # include <sys/wait.h>
50 extern int _cupsAdminGetServerSettings(http_t
*http
,
52 cups_option_t
**settings
);
53 extern int _cupsAdminSetServerSettings(http_t
*http
,
55 cups_option_t
*settings
);
56 static int do_samba_command(const char *command
,
58 const char *subcommand
,
61 static http_status_t
get_cupsd_conf(http_t
*http
, _cups_globals_t
*cg
,
62 time_t last_update
, char *name
,
63 int namelen
, int *remote
);
64 static void invalidate_cupsd_cache(_cups_globals_t
*cg
);
65 static void write_option(cups_file_t
*dstfp
, int order
,
66 const char *name
, const char *text
,
68 ipp_attribute_t
*suppattr
,
69 ipp_attribute_t
*defattr
, int defval
,
74 * 'cupsAdminCreateWindowsPPD()' - Create the Windows PPD file for a printer.
76 * @since CUPS 1.2/Mac OS X 10.5@
79 char * /* O - PPD file or NULL */
80 cupsAdminCreateWindowsPPD(
81 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
82 const char *dest
, /* I - Printer or class */
83 char *buffer
, /* I - Filename buffer */
84 int bufsize
) /* I - Size of filename buffer */
86 const char *src
; /* Source PPD filename */
87 cups_file_t
*srcfp
, /* Source PPD file */
88 *dstfp
; /* Destination PPD file */
89 ipp_t
*request
, /* IPP request */
90 *response
; /* IPP response */
91 ipp_attribute_t
*suppattr
, /* IPP -supported attribute */
92 *defattr
; /* IPP -default attribute */
93 cups_lang_t
*language
; /* Current language */
94 char line
[256], /* Line from PPD file */
95 junk
[256], /* Extra junk to throw away */
96 *ptr
, /* Pointer into line */
97 uri
[1024], /* Printer URI */
98 option
[41], /* Option */
99 choice
[41]; /* Choice */
100 int jcloption
, /* In a JCL option? */
101 jclorder
, /* Next JCL order dependency */
102 linenum
; /* Current line number */
103 time_t curtime
; /* Current time */
104 struct tm
*curdate
; /* Current date */
105 static const char * const pattrs
[] = /* Printer attributes we want */
107 "job-hold-until-supported",
108 "job-hold-until-default",
109 "job-sheets-supported",
110 "job-sheets-default",
111 "job-priority-supported",
112 "job-priority-default"
117 * Range check the input...
124 http
= _cupsConnect();
126 if (!http
|| !dest
|| !buffer
|| bufsize
< 2)
130 * Get the PPD file...
133 if ((src
= cupsGetPPD2(http
, dest
)) == NULL
)
137 * Get the supported banner pages, etc. for the printer...
140 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
142 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
143 "localhost", 0, "/printers/%s", dest
);
144 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
145 "printer-uri", NULL
, uri
);
147 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
148 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
152 * Do the request and get back a response...
155 response
= cupsDoRequest(http
, request
, "/");
156 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
163 * Open the original PPD file...
166 if ((srcfp
= cupsFileOpen(src
, "rb")) == NULL
)
170 * Create a temporary output file using the destination buffer...
173 if ((dstfp
= cupsTempFile2(buffer
, bufsize
)) == NULL
)
175 cupsFileClose(srcfp
);
183 * Write a new header explaining that this isn't the original PPD...
186 cupsFilePuts(dstfp
, "*PPD-Adobe: \"4.3\"\n");
188 curtime
= time(NULL
);
189 curdate
= gmtime(&curtime
);
191 cupsFilePrintf(dstfp
, "*%% Modified on %04d%02d%02d%02d%02d%02d+0000 "
192 "for CUPS Windows Driver\n",
193 curdate
->tm_year
+ 1900, curdate
->tm_mon
+ 1, curdate
->tm_mday
,
194 curdate
->tm_hour
, curdate
->tm_min
, curdate
->tm_sec
);
197 * Read the existing PPD file, converting all PJL commands to CUPS
198 * job ticket comments...
204 language
= cupsLangDefault();
206 while (cupsFileGets(srcfp
, line
, sizeof(line
)))
210 if (!strncmp(line
, "*PPD-Adobe:", 11))
213 * Already wrote the PPD header...
218 else if (!strncmp(line
, "*JCLBegin:", 10) ||
219 !strncmp(line
, "*JCLToPSInterpreter:", 20) ||
220 !strncmp(line
, "*JCLEnd:", 8) ||
221 !strncmp(line
, "*Protocols:", 11))
224 * Don't use existing JCL keywords; we'll create our own, below...
227 cupsFilePrintf(dstfp
, "*%% Commented out for CUPS Windows Driver...\n"
228 "*%%%s\n", line
+ 1);
231 else if (!strncmp(line
, "*JCLOpenUI", 10))
234 cupsFilePrintf(dstfp
, "%s\n", line
);
236 else if (!strncmp(line
, "*JCLCloseUI", 11))
239 cupsFilePrintf(dstfp
, "%s\n", line
);
241 else if (jcloption
&& !strncmp(line
, "*OrderDependency:", 17))
243 for (ptr
= line
+ 17; *ptr
&& isspace(*ptr
& 255); ptr
++);
245 ptr
= strchr(ptr
, ' ');
249 cupsFilePrintf(dstfp
, "*OrderDependency: %d%s\n", jclorder
, ptr
);
253 cupsFilePrintf(dstfp
, "%s\n", line
);
255 else if (jcloption
&&
256 strncmp(line
, "*End", 4) &&
257 strncmp(line
, "*Default", 8))
259 if ((ptr
= strchr(line
, ':')) == NULL
)
261 snprintf(line
, sizeof(line
),
262 _cupsLangString(language
, _("Missing value on line %d")),
264 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
, 0);
266 cupsFileClose(srcfp
);
267 cupsFileClose(dstfp
);
277 if ((ptr
= strchr(ptr
, '\"')) == NULL
)
279 snprintf(line
, sizeof(line
),
280 _cupsLangString(language
,
281 _("Missing double quote on line %d")),
283 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
, 0);
285 cupsFileClose(srcfp
);
286 cupsFileClose(dstfp
);
296 if (sscanf(line
, "*%40s%*[ \t]%40[^:/]", option
, choice
) != 2)
298 snprintf(line
, sizeof(line
),
299 _cupsLangString(language
,
300 _("Bad option + choice on line %d")),
302 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
, 0);
304 cupsFileClose(srcfp
);
305 cupsFileClose(dstfp
);
315 if (strchr(ptr
+ 1, '\"') == NULL
)
321 while (cupsFileGets(srcfp
, junk
, sizeof(junk
)) != NULL
)
325 if (!strncmp(junk
, "*End", 4))
330 snprintf(ptr
+ 1, sizeof(line
) - (ptr
- line
+ 1),
331 "%%cupsJobTicket: %s=%s\n\"\n*End", option
, choice
);
333 cupsFilePrintf(dstfp
, "*%% Changed for CUPS Windows Driver...\n%s\n",
337 cupsFilePrintf(dstfp
, "%s\n", line
);
340 cupsFileClose(srcfp
);
345 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, _("Empty PPD file"), 1);
347 cupsFileClose(dstfp
);
356 * Now add the CUPS-specific attributes and options...
359 cupsFilePuts(dstfp
, "\n*% CUPS Job Ticket support and options...\n");
360 cupsFilePuts(dstfp
, "*Protocols: PJL\n");
361 cupsFilePuts(dstfp
, "*JCLBegin: \"%!PS-Adobe-3.0<0A>\"\n");
362 cupsFilePuts(dstfp
, "*JCLToPSInterpreter: \"\"\n");
363 cupsFilePuts(dstfp
, "*JCLEnd: \"\"\n");
365 cupsFilePuts(dstfp
, "\n*OpenGroup: CUPS/CUPS Options\n\n");
367 if ((defattr
= ippFindAttribute(response
, "job-hold-until-default",
368 IPP_TAG_ZERO
)) != NULL
&&
369 (suppattr
= ippFindAttribute(response
, "job-hold-until-supported",
370 IPP_TAG_ZERO
)) != NULL
)
371 write_option(dstfp
, jclorder
++, "cupsJobHoldUntil", "Hold Until",
372 "job-hold-until", suppattr
, defattr
, 0, 1);
374 if ((defattr
= ippFindAttribute(response
, "job-priority-default",
375 IPP_TAG_INTEGER
)) != NULL
&&
376 (suppattr
= ippFindAttribute(response
, "job-priority-supported",
377 IPP_TAG_RANGE
)) != NULL
)
378 write_option(dstfp
, jclorder
++, "cupsJobPriority", "Priority",
379 "job-priority", suppattr
, defattr
, 0, 1);
381 if ((defattr
= ippFindAttribute(response
, "job-sheets-default",
382 IPP_TAG_ZERO
)) != NULL
&&
383 (suppattr
= ippFindAttribute(response
, "job-sheets-supported",
384 IPP_TAG_ZERO
)) != NULL
)
386 write_option(dstfp
, jclorder
++, "cupsJobSheetsStart", "Start Banner",
387 "job-sheets", suppattr
, defattr
, 0, 2);
388 write_option(dstfp
, jclorder
, "cupsJobSheetsEnd", "End Banner",
389 "job-sheets", suppattr
, defattr
, 1, 2);
392 cupsFilePuts(dstfp
, "*CloseGroup: CUPS\n");
393 cupsFileClose(dstfp
);
402 * 'cupsAdminExportSamba()' - Export a printer to Samba.
404 * @since CUPS 1.2/Mac OS X 10.5@
407 int /* O - 1 on success, 0 on failure */
408 cupsAdminExportSamba(
409 const char *dest
, /* I - Destination to export */
410 const char *ppd
, /* I - PPD file */
411 const char *samba_server
, /* I - Samba server */
412 const char *samba_user
, /* I - Samba username */
413 const char *samba_password
, /* I - Samba password */
414 FILE *logfile
) /* I - Log file, if any */
416 int status
; /* Status of Samba commands */
417 int have_drivers
; /* Have drivers? */
418 char file
[1024], /* File to test for */
419 authfile
[1024], /* Temporary authentication file */
420 address
[1024], /* Address for command */
421 subcmd
[1024], /* Sub-command */
422 message
[1024]; /* Error message */
423 cups_file_t
*fp
; /* Authentication file */
424 cups_lang_t
*language
; /* Current language */
425 _cups_globals_t
*cg
= _cupsGlobals();
430 * Range check input...
433 if (!dest
|| !ppd
|| !samba_server
|| !samba_user
|| !samba_password
)
435 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
440 * Create a temporary authentication file for Samba...
443 if ((fp
= cupsTempFile2(authfile
, sizeof(authfile
))) == NULL
)
445 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
, 0);
449 cupsFilePrintf(fp
, "username = %s\n", samba_user
);
450 cupsFilePrintf(fp
, "password = %s\n", samba_password
);
454 * See which drivers are available; the new CUPS v6 and Adobe drivers
455 * depend on the Windows 2k PS driver, so copy that driver first:
466 language
= cupsLangDefault();
468 snprintf(file
, sizeof(file
), "%s/drivers/pscript5.dll", cg
->cups_datadir
);
469 if (!access(file
, 0))
474 * Windows 2k driver is installed; do the smbclient commands needed
475 * to copy the Win2k drivers over...
478 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
480 snprintf(subcmd
, sizeof(subcmd
),
482 "put %s W32X86/%s.ppd;"
483 "put %s/drivers/ps5ui.dll W32X86/ps5ui.dll;"
484 "put %s/drivers/pscript.hlp W32X86/pscript.hlp;"
485 "put %s/drivers/pscript.ntf W32X86/pscript.ntf;"
486 "put %s/drivers/pscript5.dll W32X86/pscript5.dll",
487 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
488 cg
->cups_datadir
, cg
->cups_datadir
);
490 if ((status
= do_samba_command("smbclient", address
, subcmd
,
491 authfile
, logfile
)) != 0)
493 snprintf(message
, sizeof(message
),
494 _cupsLangString(language
,
495 _("Unable to copy Windows 2000 printer "
496 "driver files (%d)")), status
);
498 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
501 _cupsLangPrintf(logfile
, "%s\n", message
);
509 * See if we also have the CUPS driver files; if so, use them!
512 snprintf(file
, sizeof(file
), "%s/drivers/cupsps6.dll", cg
->cups_datadir
);
513 if (!access(file
, 0))
516 * Copy the CUPS driver files over...
519 snprintf(subcmd
, sizeof(subcmd
),
520 "put %s/drivers/cups6.ini W32X86/cups6.ini;"
521 "put %s/drivers/cupsps6.dll W32X86/cupsps6.dll;"
522 "put %s/drivers/cupsui6.dll W32X86/cupsui6.dll",
523 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
525 if ((status
= do_samba_command("smbclient", address
, subcmd
,
526 authfile
, logfile
)) != 0)
528 snprintf(message
, sizeof(message
),
529 _cupsLangString(language
,
530 _("Unable to copy CUPS printer driver "
531 "files (%d)")), status
);
533 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
536 _cupsLangPrintf(logfile
, "%s\n", message
);
544 * Do the rpcclient command needed for the CUPS drivers...
547 snprintf(subcmd
, sizeof(subcmd
),
548 "adddriver \"Windows NT x86\" \"%s:"
549 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
550 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
551 "cups6.ini,cupsps6.dll,cupsui6.dll\"",
557 * Don't have the CUPS drivers, so just use the standard Windows
561 snprintf(subcmd
, sizeof(subcmd
),
562 "adddriver \"Windows NT x86\" \"%s:"
563 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
564 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
568 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
569 authfile
, logfile
)) != 0)
571 snprintf(message
, sizeof(message
),
572 _cupsLangString(language
,
573 _("Unable to install Windows 2000 printer "
574 "driver files (%d)")), status
);
576 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
579 _cupsLangPrintf(logfile
, "%s\n", message
);
588 * See if we have the Win9x PS driver...
591 snprintf(file
, sizeof(file
), "%s/drivers/ADOBEPS4.DRV", cg
->cups_datadir
);
592 if (!access(file
, 0))
597 * Do the smbclient commands needed for the Adobe Win9x drivers...
600 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
602 snprintf(subcmd
, sizeof(subcmd
),
604 "put %s WIN40/%s.PPD;"
605 "put %s/drivers/ADFONTS.MFM WIN40/ADFONTS.MFM;"
606 "put %s/drivers/ADOBEPS4.DRV WIN40/ADOBEPS4.DRV;"
607 "put %s/drivers/ADOBEPS4.HLP WIN40/ADOBEPS4.HLP;"
608 "put %s/drivers/ICONLIB.DLL WIN40/ICONLIB.DLL;"
609 "put %s/drivers/PSMON.DLL WIN40/PSMON.DLL;",
610 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
611 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
613 if ((status
= do_samba_command("smbclient", address
, subcmd
,
614 authfile
, logfile
)) != 0)
616 snprintf(message
, sizeof(message
),
617 _cupsLangString(language
,
618 _("Unable to copy Windows 9x printer "
619 "driver files (%d)")), status
);
621 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
624 _cupsLangPrintf(logfile
, "%s\n", message
);
632 * Do the rpcclient commands needed for the Adobe Win9x drivers...
635 snprintf(subcmd
, sizeof(subcmd
),
636 "adddriver \"Windows 4.0\" \"%s:ADOBEPS4.DRV:%s.PPD:NULL:"
637 "ADOBEPS4.HLP:PSMON.DLL:RAW:"
638 "ADOBEPS4.DRV,%s.PPD,ADOBEPS4.HLP,PSMON.DLL,ADFONTS.MFM,"
642 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
643 authfile
, logfile
)) != 0)
645 snprintf(message
, sizeof(message
),
646 _cupsLangString(language
,
647 _("Unable to install Windows 9x printer "
648 "driver files (%d)")), status
);
650 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
653 _cupsLangPrintf(logfile
, "%s\n", message
);
662 * See if we have the 64-bit Windows PS driver...
672 snprintf(file
, sizeof(file
), "%s/drivers/x64/pscript5.dll", cg
->cups_datadir
);
673 if (!access(file
, 0))
678 * 64-bit Windows driver is installed; do the smbclient commands needed
679 * to copy the Win64 drivers over...
682 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
684 snprintf(subcmd
, sizeof(subcmd
),
687 "put %s/drivers/x64/ps5ui.dll x64/ps5ui.dll;"
688 "put %s/drivers/x64/pscript.hlp x64/pscript.hlp;"
689 "put %s/drivers/x64/pscript.ntf x64/pscript.ntf;"
690 "put %s/drivers/x64/pscript5.dll x64/pscript5.dll",
691 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
692 cg
->cups_datadir
, cg
->cups_datadir
);
694 if ((status
= do_samba_command("smbclient", address
, subcmd
,
695 authfile
, logfile
)) != 0)
697 snprintf(message
, sizeof(message
),
698 _cupsLangString(language
,
699 _("Unable to copy 64-bit Windows printer "
700 "driver files (%d)")), status
);
702 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
705 _cupsLangPrintf(logfile
, "%s\n", message
);
713 * See if we also have the CUPS driver files; if so, use them!
716 snprintf(file
, sizeof(file
), "%s/drivers/x64/cupsps6.dll", cg
->cups_datadir
);
717 if (!access(file
, 0))
720 * Copy the CUPS driver files over...
723 snprintf(subcmd
, sizeof(subcmd
),
724 "put %s/drivers/x64/cups6.ini x64/cups6.ini;"
725 "put %s/drivers/x64/cupsps6.dll x64/cupsps6.dll;"
726 "put %s/drivers/x64/cupsui6.dll x64/cupsui6.dll",
727 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
729 if ((status
= do_samba_command("smbclient", address
, subcmd
,
730 authfile
, logfile
)) != 0)
732 snprintf(message
, sizeof(message
),
733 _cupsLangString(language
,
734 _("Unable to copy 64-bit CUPS printer driver "
735 "files (%d)")), status
);
737 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
740 _cupsLangPrintf(logfile
, "%s\n", message
);
748 * Do the rpcclient command needed for the CUPS drivers...
751 snprintf(subcmd
, sizeof(subcmd
),
752 "adddriver \"Windows x64\" \"%s:"
753 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
754 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
755 "cups6.ini,cupsps6.dll,cupsui6.dll\"",
761 * Don't have the CUPS drivers, so just use the standard Windows
765 snprintf(subcmd
, sizeof(subcmd
),
766 "adddriver \"Windows x64\" \"%s:"
767 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
768 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
772 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
773 authfile
, logfile
)) != 0)
775 snprintf(message
, sizeof(message
),
776 _cupsLangString(language
,
777 _("Unable to install Windows 2000 printer "
778 "driver files (%d)")), status
);
780 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
783 _cupsLangPrintf(logfile
, "%s\n", message
);
791 if (logfile
&& !(have_drivers
& 1))
795 _cupsLangString(language
,
796 _("No Windows printer drivers are installed")),
800 _cupsLangString(language
,
801 _("Warning, no Windows 2000 printer drivers "
805 _cupsSetError(IPP_NOT_FOUND
, message
, 0);
806 _cupsLangPrintf(logfile
, "%s\n", message
);
809 if (have_drivers
== 0)
811 _cupsSetError(IPP_NOT_FOUND
, message
, 0);
819 * Finally, associate the drivers we just added with the queue...
822 snprintf(subcmd
, sizeof(subcmd
), "setdriver %s %s", dest
, dest
);
824 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
825 authfile
, logfile
)) != 0)
827 snprintf(message
, sizeof(message
),
828 _cupsLangString(language
,
829 _("Unable to set Windows printer driver (%d)")),
832 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
835 _cupsLangPrintf(logfile
, "%s\n", message
);
849 * 'cupsAdminGetServerSettings()' - Get settings from the server.
851 * The returned settings should be freed with cupsFreeOptions() when
852 * you are done with them.
854 * @since CUPS 1.3/Mac OS X 10.5@
857 int /* O - 1 on success, 0 on failure */
858 cupsAdminGetServerSettings(
859 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
860 int *num_settings
, /* O - Number of settings */
861 cups_option_t
**settings
) /* O - Settings */
863 int i
; /* Looping var */
864 cups_file_t
*cupsd
; /* cupsd.conf file */
865 char cupsdconf
[1024]; /* cupsd.conf filename */
866 int remote
; /* Remote cupsd.conf file? */
867 http_status_t status
; /* Status of getting cupsd.conf */
868 char line
[1024], /* Line from cupsd.conf file */
869 *value
; /* Value on line */
870 cups_option_t
*setting
; /* Current setting */
871 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
875 * Range check input...
881 * See if we are connected to the same server...
887 * Compare the connection hostname, port, and encryption settings to
888 * the cached defaults; these were initialized the first time we
892 if (strcmp(cg
->http
->hostname
, cg
->server
) ||
893 cg
->ipp_port
!= _httpAddrPort(cg
->http
->hostaddr
) ||
894 (cg
->http
->encryption
!= cg
->encryption
&&
895 cg
->http
->encryption
== HTTP_ENCRYPT_NEVER
))
898 * Need to close the current connection because something has changed...
907 * (Re)connect as needed...
912 if ((cg
->http
= _httpCreate(cupsServer(), ippPort(),
913 cupsEncryption())) == NULL
)
916 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, NULL
, 0);
918 _cupsSetError(IPP_SERVICE_UNAVAILABLE
,
919 _("Unable to connect to host."), 1);
934 if (!http
|| !num_settings
|| !settings
)
936 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
951 * Get the cupsd.conf file...
954 if ((status
= get_cupsd_conf(http
, cg
, cg
->cupsd_update
, cupsdconf
,
955 sizeof(cupsdconf
), &remote
)) == HTTP_OK
)
957 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
959 char message
[1024]; /* Message string */
962 snprintf(message
, sizeof(message
),
963 _cupsLangString(cupsLangDefault(), _("Open of %s failed: %s")),
964 cupsdconf
, strerror(errno
));
965 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
974 * Read the file, keeping track of what settings are enabled...
977 int remote_access
= 0, /* Remote access allowed? */
978 remote_admin
= 0, /* Remote administration allowed? */
979 remote_any
= 0, /* Remote access from anywhere allowed? */
980 browsing
= 1, /* Browsing enabled? */
981 browse_allow
= 1, /* Browse address set? */
982 browse_address
= 0, /* Browse address set? */
983 cancel_policy
= 1, /* Cancel-job policy set? */
984 debug_logging
= 0; /* LogLevel debug set? */
985 int linenum
= 0, /* Line number in file */
986 in_location
= 0, /* In a location section? */
987 in_policy
= 0, /* In a policy section? */
988 in_cancel_job
= 0, /* In a cancel-job section? */
989 in_admin_location
= 0; /* In the /admin location? */
992 invalidate_cupsd_cache(cg
);
994 cg
->cupsd_update
= time(NULL
);
995 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
997 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
999 if (!value
&& strncmp(line
, "</", 2))
1000 value
= line
+ strlen(line
);
1002 if ((!strcasecmp(line
, "Port") || !strcasecmp(line
, "Listen")) && value
)
1004 char *port
; /* Pointer to port number, if any */
1007 if ((port
= strrchr(value
, ':')) != NULL
)
1009 else if (isdigit(*value
& 255))
1012 * Listen on a port number implies remote access...
1019 if (strcasecmp(value
, "localhost") && strcmp(value
, "127.0.0.1")
1022 #endif /* AF_LOCAL */
1024 && strcmp(value
, "::1")
1025 #endif /* AF_INET6 */
1029 else if (!strcasecmp(line
, "Browsing"))
1031 browsing
= !strcasecmp(value
, "yes") || !strcasecmp(value
, "on") ||
1032 !strcasecmp(value
, "true");
1034 else if (!strcasecmp(line
, "BrowseAddress"))
1038 else if (!strcasecmp(line
, "BrowseAllow"))
1042 else if (!strcasecmp(line
, "BrowseOrder"))
1044 browse_allow
= !strncasecmp(value
, "deny,", 5);
1046 else if (!strcasecmp(line
, "LogLevel"))
1048 debug_logging
= !strncasecmp(value
, "debug", 5);
1050 else if (!strcasecmp(line
, "<Policy") && !strcasecmp(value
, "default"))
1054 else if (!strcasecmp(line
, "</Policy>"))
1058 else if (!strcasecmp(line
, "<Limit") && in_policy
&& value
)
1061 * See if the policy limit is for the Cancel-Job operation...
1064 char *valptr
; /* Pointer into value */
1069 for (valptr
= value
; !isspace(*valptr
& 255) && *valptr
; valptr
++);
1074 if (!strcasecmp(value
, "cancel-job") || !strcasecmp(value
, "all"))
1080 for (value
= valptr
; isspace(*value
& 255); value
++);
1083 else if (!strcasecmp(line
, "</Limit>"))
1087 else if (!strcasecmp(line
, "Require") && in_cancel_job
)
1091 else if (!strcasecmp(line
, "<Location") && value
)
1093 in_admin_location
= !strcasecmp(value
, "/admin");
1096 else if (!strcasecmp(line
, "</Location>"))
1098 in_admin_location
= 0;
1101 else if (!strcasecmp(line
, "Allow") && value
&&
1102 strcasecmp(value
, "localhost") && strcasecmp(value
, "127.0.0.1")
1105 #endif /* AF_LOCAL */
1107 && strcmp(value
, "::1")
1108 #endif /* AF_INET6 */
1111 if (in_admin_location
)
1113 else if (!strcasecmp(value
, "all"))
1116 else if (line
[0] != '<' && !in_location
&& !in_policy
&&
1117 strcasecmp(line
, "Allow") &&
1118 strcasecmp(line
, "AuthType") &&
1119 strcasecmp(line
, "Deny") &&
1120 strcasecmp(line
, "Order") &&
1121 strcasecmp(line
, "Require") &&
1122 strcasecmp(line
, "Satisfy"))
1123 cg
->cupsd_num_settings
= cupsAddOption(line
, value
,
1124 cg
->cupsd_num_settings
,
1125 &(cg
->cupsd_settings
));
1128 cupsFileClose(cupsd
);
1130 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1131 debug_logging
? "1" : "0",
1132 cg
->cupsd_num_settings
,
1133 &(cg
->cupsd_settings
));
1135 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1136 (remote_access
&& remote_admin
) ?
1138 cg
->cupsd_num_settings
,
1139 &(cg
->cupsd_settings
));
1141 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
1142 remote_any
? "1" : "0",
1143 cg
->cupsd_num_settings
,
1144 &(cg
->cupsd_settings
));
1146 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
1147 (browsing
&& browse_allow
) ?
1149 cg
->cupsd_num_settings
,
1150 &(cg
->cupsd_settings
));
1152 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1153 (remote_access
&& browsing
&&
1154 browse_address
) ? "1" : "0",
1155 cg
->cupsd_num_settings
,
1156 &(cg
->cupsd_settings
));
1158 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1159 cancel_policy
? "1" : "0",
1160 cg
->cupsd_num_settings
,
1161 &(cg
->cupsd_settings
));
1163 else if (status
!= HTTP_NOT_MODIFIED
)
1164 invalidate_cupsd_cache(cg
);
1167 * Remove any temporary files and copy the settings array...
1173 for (i
= cg
->cupsd_num_settings
, setting
= cg
->cupsd_settings
;
1176 *num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1177 *num_settings
, settings
);
1179 return (cg
->cupsd_num_settings
> 0);
1184 * 'cupsAdminSetServerSettings()' - Set settings on the server.
1186 * @since CUPS 1.3/Mac OS X 10.5@
1189 int /* O - 1 on success, 0 on failure */
1190 cupsAdminSetServerSettings(
1191 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1192 int num_settings
, /* I - Number of settings */
1193 cups_option_t
*settings
) /* I - Settings */
1195 int i
; /* Looping var */
1196 http_status_t status
; /* GET/PUT status */
1197 const char *server_port_env
; /* SERVER_PORT env var */
1198 int server_port
; /* IPP port for server */
1199 cups_file_t
*cupsd
; /* cupsd.conf file */
1200 char cupsdconf
[1024]; /* cupsd.conf filename */
1201 int remote
; /* Remote cupsd.conf file? */
1202 char tempfile
[1024]; /* Temporary new cupsd.conf */
1203 cups_file_t
*temp
; /* Temporary file */
1204 char line
[1024], /* Line from cupsd.conf file */
1205 *value
; /* Value on line */
1206 int linenum
, /* Line number in file */
1207 in_location
, /* In a location section? */
1208 in_policy
, /* In a policy section? */
1209 in_default_policy
, /* In the default policy section? */
1210 in_cancel_job
, /* In a cancel-job section? */
1211 in_admin_location
, /* In the /admin location? */
1212 in_conf_location
, /* In the /admin/conf location? */
1213 in_root_location
; /* In the / location? */
1214 const char *val
; /* Setting value */
1215 int remote_printers
, /* Show remote printers */
1216 share_printers
, /* Share local printers */
1217 remote_admin
, /* Remote administration allowed? */
1218 remote_any
, /* Remote access from anywhere? */
1219 user_cancel_any
, /* Cancel-job policy set? */
1220 debug_logging
; /* LogLevel debug set? */
1221 int wrote_port_listen
, /* Wrote the port/listen lines? */
1222 wrote_browsing
, /* Wrote the browsing lines? */
1223 wrote_policy
, /* Wrote the policy? */
1224 wrote_loglevel
, /* Wrote the LogLevel line? */
1225 wrote_admin_location
, /* Wrote the /admin location? */
1226 wrote_conf_location
, /* Wrote the /admin/conf location? */
1227 wrote_root_location
; /* Wrote the / location? */
1228 int indent
; /* Indentation */
1229 int cupsd_num_settings
; /* New number of settings */
1230 int old_remote_printers
, /* Show remote printers */
1231 old_share_printers
, /* Share local printers */
1232 old_remote_admin
, /* Remote administration allowed? */
1233 old_user_cancel_any
, /* Cancel-job policy set? */
1234 old_debug_logging
; /* LogLevel debug set? */
1235 cups_option_t
*cupsd_settings
, /* New settings */
1236 *setting
; /* Current setting */
1237 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1241 * Range check input...
1245 http
= _cupsConnect();
1247 if (!http
|| !num_settings
|| !settings
)
1249 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
1255 * Get the cupsd.conf file...
1258 if (get_cupsd_conf(http
, cg
, 0, cupsdconf
, sizeof(cupsdconf
),
1259 &remote
) == HTTP_OK
)
1261 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
1263 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
, 0);
1271 * Get current settings...
1274 if (!cupsAdminGetServerSettings(http
, &cupsd_num_settings
,
1278 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, cupsd_num_settings
,
1279 cupsd_settings
)) != NULL
)
1280 old_debug_logging
= atoi(val
);
1282 old_debug_logging
= 0;
1284 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, cupsd_num_settings
,
1285 cupsd_settings
)) != NULL
)
1286 old_remote_admin
= atoi(val
);
1288 old_remote_admin
= 0;
1290 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, cupsd_num_settings
,
1291 cupsd_settings
)) != NULL
)
1292 remote_any
= atoi(val
);
1296 DEBUG_printf(("1cupsAdminSetServerSettings: old remote_any=%d",
1299 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
, cupsd_num_settings
,
1300 cupsd_settings
)) != NULL
)
1301 old_remote_printers
= atoi(val
);
1303 old_remote_printers
= 1;
1305 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, cupsd_num_settings
,
1306 cupsd_settings
)) != NULL
)
1307 old_share_printers
= atoi(val
);
1309 old_share_printers
= 0;
1311 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, cupsd_num_settings
,
1312 cupsd_settings
)) != NULL
)
1313 old_user_cancel_any
= atoi(val
);
1315 old_user_cancel_any
= 0;
1317 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
1320 * Get basic settings...
1323 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, num_settings
,
1326 debug_logging
= atoi(val
);
1328 if (debug_logging
== old_debug_logging
)
1331 * No change to this setting...
1340 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, num_settings
,
1342 remote_any
= atoi(val
);
1344 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, num_settings
,
1347 remote_admin
= atoi(val
);
1349 if (remote_admin
== old_remote_admin
&& remote_any
< 0)
1352 * No change to this setting...
1361 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
, num_settings
,
1364 remote_printers
= atoi(val
);
1366 if (remote_printers
== old_remote_printers
)
1369 * No change to this setting...
1372 remote_printers
= -1;
1376 remote_printers
= -1;
1378 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, num_settings
,
1381 share_printers
= atoi(val
);
1383 if (share_printers
== old_share_printers
&& remote_any
< 0)
1386 * No change to this setting...
1389 share_printers
= -1;
1393 share_printers
= -1;
1395 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, num_settings
,
1398 user_cancel_any
= atoi(val
);
1400 if (user_cancel_any
== old_user_cancel_any
)
1403 * No change to this setting...
1406 user_cancel_any
= -1;
1410 user_cancel_any
= -1;
1413 * Create a temporary file for the new cupsd.conf file...
1416 if ((temp
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1418 cupsFileClose(cupsd
);
1423 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
, 0);
1428 * Copy the old file to the new, making changes along the way...
1431 cupsd_num_settings
= 0;
1432 in_admin_location
= 0;
1434 in_conf_location
= 0;
1435 in_default_policy
= 0;
1438 in_root_location
= 0;
1440 wrote_admin_location
= 0;
1442 wrote_conf_location
= 0;
1445 wrote_port_listen
= 0;
1446 wrote_root_location
= 0;
1449 if ((server_port_env
= getenv("SERVER_PORT")) != NULL
)
1451 if ((server_port
= atoi(server_port_env
)) <= 0)
1452 server_port
= ippPort();
1455 server_port
= ippPort();
1457 if (server_port
<= 0)
1458 server_port
= IPP_PORT
;
1460 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
1462 if ((!strcasecmp(line
, "Port") || !strcasecmp(line
, "Listen")) &&
1463 (remote_admin
>= 0 || remote_any
> 0 || share_printers
>= 0))
1465 if (!wrote_port_listen
)
1467 wrote_port_listen
= 1;
1469 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1471 cupsFilePuts(temp
, "# Allow remote access\n");
1472 cupsFilePrintf(temp
, "Port %d\n", server_port
);
1476 cupsFilePuts(temp
, "# Only listen for connections from the local "
1478 cupsFilePrintf(temp
, "Listen localhost:%d\n", server_port
);
1481 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1482 if ((!value
|| strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)) &&
1483 !access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1484 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1485 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1487 else if (value
&& value
[0] == '/'
1488 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1489 && strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)
1490 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1492 cupsFilePrintf(temp
, "Listen %s\n", value
);
1494 else if ((!strcasecmp(line
, "Browsing") ||
1495 !strcasecmp(line
, "BrowseAddress") ||
1496 !strcasecmp(line
, "BrowseAllow") ||
1497 !strcasecmp(line
, "BrowseDeny") ||
1498 !strcasecmp(line
, "BrowseLocalProtocols") ||
1499 !strcasecmp(line
, "BrowseRemoteProtocols") ||
1500 !strcasecmp(line
, "BrowseOrder")) &&
1501 (remote_printers
>= 0 || share_printers
>= 0))
1503 if (!wrote_browsing
)
1505 int new_remote_printers
= (remote_printers
> 0 ||
1506 (remote_printers
== -1 &&
1507 old_remote_printers
> 0));
1508 int new_share_printers
= (share_printers
> 0 ||
1509 (share_printers
== -1 &&
1510 old_share_printers
> 0));
1514 if (new_remote_printers
|| new_share_printers
)
1516 const char *localp
= cupsGetOption("BrowseLocalProtocols",
1517 num_settings
, settings
);
1518 const char *remotep
= cupsGetOption("BrowseRemoteProtocols",
1519 num_settings
, settings
);
1521 if (!localp
|| !localp
[0])
1522 localp
= cupsGetOption("BrowseLocalProtocols", cupsd_num_settings
,
1526 remotep
= cupsGetOption("BrowseRemoteProtocols", cupsd_num_settings
,
1529 if (new_remote_printers
&& new_share_printers
)
1531 "# Enable printer sharing and shared printers.\n");
1532 else if (new_remote_printers
)
1534 "# Show shared printers on the local network.\n");
1537 "# Share local printers on the local network.\n");
1539 cupsFilePuts(temp
, "Browsing On\n");
1540 cupsFilePuts(temp
, "BrowseOrder allow,deny\n");
1542 if (new_remote_printers
)
1544 cupsFilePuts(temp
, "BrowseAllow all\n");
1547 remotep
= CUPS_DEFAULT_BROWSE_REMOTE_PROTOCOLS
;
1549 cupsFilePrintf(temp
, "BrowseRemoteProtocols %s\n", remotep
);
1552 cupsFilePuts(temp
, "BrowseRemoteProtocols\n");
1554 cupsd_num_settings
= cupsAddOption("BrowseRemoteProtocols", remotep
,
1558 if (new_share_printers
)
1560 cupsFilePuts(temp
, "BrowseAddress @LOCAL\n");
1563 localp
= CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS
;
1565 cupsFilePrintf(temp
, "BrowseLocalProtocols %s\n", localp
);
1568 cupsFilePuts(temp
, "BrowseLocalProtocols\n");
1570 cupsd_num_settings
= cupsAddOption("BrowseLocalProtocols", localp
,
1577 "# Disable printer sharing and shared printers.\n");
1578 cupsFilePuts(temp
, "Browsing Off\n");
1582 else if (!strcasecmp(line
, "LogLevel") && debug_logging
>= 0)
1589 "# Show troubleshooting information in error_log.\n");
1590 cupsFilePuts(temp
, "LogLevel debug\n");
1594 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1595 cupsFilePuts(temp
, "LogLevel " CUPS_DEFAULT_LOG_LEVEL
"\n");
1598 else if (!strcasecmp(line
, "<Policy"))
1600 in_default_policy
= !strcasecmp(value
, "default");
1603 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1606 else if (!strcasecmp(line
, "</Policy>"))
1609 if (!wrote_policy
&& in_default_policy
)
1613 if (!user_cancel_any
)
1614 cupsFilePuts(temp
, " # Only the owner or an administrator can "
1616 " <Limit Cancel-Job>\n"
1617 " Order deny,allow\n"
1618 " Require user @OWNER "
1619 CUPS_DEFAULT_PRINTOPERATOR_AUTH
"\n"
1624 in_default_policy
= 0;
1626 cupsFilePuts(temp
, "</Policy>\n");
1628 else if (!strcasecmp(line
, "<Location"))
1632 if (!strcmp(value
, "/admin"))
1633 in_admin_location
= 1;
1634 if (!strcmp(value
, "/admin/conf"))
1635 in_conf_location
= 1;
1636 else if (!strcmp(value
, "/"))
1637 in_root_location
= 1;
1639 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1641 else if (!strcasecmp(line
, "</Location>"))
1645 if (in_admin_location
&& remote_admin
>= 0)
1647 wrote_admin_location
= 1;
1650 cupsFilePuts(temp
, " # Allow remote administration...\n");
1651 else if (remote_admin
== 0)
1652 cupsFilePuts(temp
, " # Restrict access to the admin pages...\n");
1654 cupsFilePuts(temp
, " Order allow,deny\n");
1657 cupsFilePrintf(temp
, " Allow %s\n",
1658 remote_any
> 0 ? "all" : "@LOCAL");
1660 else if (in_conf_location
&& remote_admin
>= 0)
1662 wrote_conf_location
= 1;
1665 cupsFilePuts(temp
, " # Allow remote access to the configuration "
1668 cupsFilePuts(temp
, " # Restrict access to the configuration "
1671 cupsFilePuts(temp
, " Order allow,deny\n");
1674 cupsFilePrintf(temp
, " Allow %s\n",
1675 remote_any
> 0 ? "all" : "@LOCAL");
1677 else if (in_root_location
&&
1678 (remote_admin
>= 0 || remote_any
>= 0 || share_printers
>= 0))
1680 wrote_root_location
= 1;
1682 if (remote_admin
> 0 && share_printers
> 0)
1683 cupsFilePuts(temp
, " # Allow shared printing and remote "
1684 "administration...\n");
1685 else if (remote_admin
> 0)
1686 cupsFilePuts(temp
, " # Allow remote administration...\n");
1687 else if (share_printers
> 0)
1688 cupsFilePuts(temp
, " # Allow shared printing...\n");
1689 else if (remote_any
> 0)
1690 cupsFilePuts(temp
, " # Allow remote access...\n");
1692 cupsFilePuts(temp
, " # Restrict access to the server...\n");
1694 cupsFilePuts(temp
, " Order allow,deny\n");
1696 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1697 cupsFilePrintf(temp
, " Allow %s\n",
1698 remote_any
> 0 ? "all" : "@LOCAL");
1701 in_admin_location
= 0;
1702 in_conf_location
= 0;
1703 in_root_location
= 0;
1705 cupsFilePuts(temp
, "</Location>\n");
1707 else if (!strcasecmp(line
, "<Limit") && in_default_policy
)
1710 * See if the policy limit is for the Cancel-Job operation...
1713 char *valptr
; /* Pointer into value */
1718 if (!strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1721 * Don't write anything for this limit section...
1728 cupsFilePrintf(temp
, " %s", line
);
1732 for (valptr
= value
; !isspace(*valptr
& 255) && *valptr
; valptr
++);
1737 if (!strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1740 * Write everything except for this definition...
1746 cupsFilePrintf(temp
, " %s", value
);
1748 for (value
= valptr
; isspace(*value
& 255); value
++);
1751 cupsFilePuts(temp
, ">\n");
1754 else if (!strcasecmp(line
, "</Limit>") && in_cancel_job
)
1758 if (in_cancel_job
== 1)
1759 cupsFilePuts(temp
, " </Limit>\n");
1763 if (!user_cancel_any
)
1764 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1766 " <Limit Cancel-Job>\n"
1767 " Order deny,allow\n"
1768 " Require user @OWNER "
1769 CUPS_DEFAULT_PRINTOPERATOR_AUTH
"\n"
1774 else if ((((in_admin_location
|| in_conf_location
|| in_root_location
) &&
1775 remote_admin
>= 0) ||
1776 (in_root_location
&& share_printers
>= 0)) &&
1777 (!strcasecmp(line
, "Allow") || !strcasecmp(line
, "Deny") ||
1778 !strcasecmp(line
, "Order")))
1780 else if (in_cancel_job
== 2)
1782 else if (!strcasecmp(line
, "<Limit") && value
)
1783 cupsFilePrintf(temp
, " %s %s>\n", line
, value
);
1784 else if (line
[0] == '<')
1788 cupsFilePrintf(temp
, "%*s%s %s>\n", indent
, "", line
, value
);
1796 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1799 else if (!in_policy
&& !in_location
&&
1800 (val
= cupsGetOption(line
, num_settings
, settings
)) != NULL
)
1803 * Replace this directive's value with the new one...
1806 cupsd_num_settings
= cupsAddOption(line
, val
, cupsd_num_settings
,
1810 * Write the new value in its place, without indentation since we
1811 * only support setting root directives, not in sections...
1814 cupsFilePrintf(temp
, "%s %s\n", line
, val
);
1818 if (!in_policy
&& !in_location
)
1821 * Record the non-policy, non-location directives that we find
1822 * in the server settings, since we cache this info and record it
1823 * in _cupsAdminGetServerSettings()...
1826 cupsd_num_settings
= cupsAddOption(line
, value
, cupsd_num_settings
,
1830 cupsFilePrintf(temp
, "%*s%s %s\n", indent
, "", line
, value
);
1833 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1837 * Write any missing info...
1840 if (!wrote_browsing
&& (remote_printers
>= 0 || share_printers
>= 0))
1842 if (remote_printers
> 0 || share_printers
> 0)
1844 if (remote_printers
> 0 && share_printers
> 0)
1845 cupsFilePuts(temp
, "# Enable printer sharing and shared printers.\n");
1846 else if (remote_printers
> 0)
1847 cupsFilePuts(temp
, "# Show shared printers on the local network.\n");
1849 cupsFilePuts(temp
, "# Share local printers on the local network.\n");
1851 cupsFilePuts(temp
, "Browsing On\n");
1852 cupsFilePuts(temp
, "BrowseOrder allow,deny\n");
1854 if (remote_printers
> 0)
1855 cupsFilePuts(temp
, "BrowseAllow all\n");
1857 if (share_printers
> 0)
1858 cupsFilePuts(temp
, "BrowseAddress @LOCAL\n");
1862 cupsFilePuts(temp
, "# Disable printer sharing and shared printers.\n");
1863 cupsFilePuts(temp
, "Browsing Off\n");
1867 if (!wrote_loglevel
&& debug_logging
>= 0)
1871 cupsFilePuts(temp
, "# Show troubleshooting information in error_log.\n");
1872 cupsFilePuts(temp
, "LogLevel debug\n");
1876 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1877 cupsFilePuts(temp
, "LogLevel " CUPS_DEFAULT_LOG_LEVEL
"\n");
1881 if (!wrote_port_listen
&&
1882 (remote_admin
>= 0 || remote_any
>= 0 || share_printers
>= 0))
1884 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1886 cupsFilePuts(temp
, "# Allow remote access\n");
1887 cupsFilePrintf(temp
, "Port %d\n", ippPort());
1892 "# Only listen for connections from the local machine.\n");
1893 cupsFilePrintf(temp
, "Listen localhost:%d\n", ippPort());
1896 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1897 if (!access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1898 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1899 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1902 if (!wrote_root_location
&&
1903 (remote_admin
>= 0 || remote_any
>= 0 || share_printers
>= 0))
1905 if (remote_admin
> 0 && share_printers
> 0)
1907 "# Allow shared printing and remote administration...\n");
1908 else if (remote_admin
> 0)
1909 cupsFilePuts(temp
, "# Allow remote administration...\n");
1910 else if (share_printers
> 0)
1911 cupsFilePuts(temp
, "# Allow shared printing...\n");
1912 else if (remote_any
> 0)
1913 cupsFilePuts(temp
, "# Allow remote access...\n");
1915 cupsFilePuts(temp
, "# Restrict access to the server...\n");
1917 cupsFilePuts(temp
, "<Location />\n"
1918 " Order allow,deny\n");
1920 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1921 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1923 cupsFilePuts(temp
, "</Location>\n");
1926 if (!wrote_admin_location
&& remote_admin
>= 0)
1929 cupsFilePuts(temp
, "# Allow remote administration...\n");
1931 cupsFilePuts(temp
, "# Restrict access to the admin pages...\n");
1933 cupsFilePuts(temp
, "<Location /admin>\n"
1934 " Order allow,deny\n");
1937 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1939 cupsFilePuts(temp
, "</Location>\n");
1942 if (!wrote_conf_location
&& remote_admin
>= 0)
1946 "# Allow remote access to the configuration files...\n");
1948 cupsFilePuts(temp
, "# Restrict access to the configuration files...\n");
1950 cupsFilePuts(temp
, "<Location /admin/conf>\n"
1951 " AuthType Default\n"
1952 " Require user @SYSTEM\n"
1953 " Order allow,deny\n");
1956 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1958 cupsFilePuts(temp
, "</Location>\n");
1961 if (!wrote_policy
&& user_cancel_any
>= 0)
1963 cupsFilePuts(temp
, "<Policy default>\n"
1964 " # Job-related operations must be done by the owner "
1965 "or an administrator...\n"
1966 " <Limit Send-Document Send-URI Hold-Job Release-Job "
1967 "Restart-Job Purge-Jobs Set-Job-Attributes "
1968 "Create-Job-Subscription Renew-Subscription "
1969 "Cancel-Subscription Get-Notifications Reprocess-Job "
1970 "Cancel-Current-Job Suspend-Current-Job Resume-Job "
1972 " Require user @OWNER @SYSTEM\n"
1973 " Order deny,allow\n"
1975 " # All administration operations require an "
1976 "administrator to authenticate...\n"
1977 " <Limit Pause-Printer Resume-Printer "
1978 "Set-Printer-Attributes Enable-Printer "
1979 "Disable-Printer Pause-Printer-After-Current-Job "
1980 "Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer "
1981 "Activate-Printer Restart-Printer Shutdown-Printer "
1982 "Startup-Printer Promote-Job Schedule-Job-After "
1983 "CUPS-Add-Printer CUPS-Delete-Printer "
1984 "CUPS-Add-Class CUPS-Delete-Class "
1985 "CUPS-Accept-Jobs CUPS-Reject-Jobs "
1986 "CUPS-Set-Default CUPS-Add-Device CUPS-Delete-Device>\n"
1987 " AuthType Default\n"
1988 " Require user @SYSTEM\n"
1989 " Order deny,allow\n"
1992 if (!user_cancel_any
)
1993 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1995 " <Limit Cancel-Job>\n"
1996 " Order deny,allow\n"
1997 " Require user @OWNER "
1998 CUPS_DEFAULT_PRINTOPERATOR_AUTH
"\n"
2001 cupsFilePuts(temp
, " <Limit All>\n"
2002 " Order deny,allow\n"
2007 for (i
= num_settings
, setting
= settings
; i
> 0; i
--, setting
++)
2008 if (setting
->name
[0] != '_' &&
2009 !cupsGetOption(setting
->name
, cupsd_num_settings
, cupsd_settings
))
2012 * Add this directive to the list of directives we have written...
2015 cupsd_num_settings
= cupsAddOption(setting
->name
, setting
->value
,
2016 cupsd_num_settings
, &cupsd_settings
);
2019 * Write the new value, without indentation since we only support
2020 * setting root directives, not in sections...
2023 cupsFilePrintf(temp
, "%s %s\n", setting
->name
, setting
->value
);
2026 cupsFileClose(cupsd
);
2027 cupsFileClose(temp
);
2030 * Upload the configuration file to the server...
2033 status
= cupsPutFile(http
, "/admin/conf/cupsd.conf", tempfile
);
2035 if (status
== HTTP_CREATED
)
2038 * Updated OK, add the basic settings...
2041 if (debug_logging
>= 0)
2042 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
2043 debug_logging
? "1" : "0",
2044 cupsd_num_settings
, &cupsd_settings
);
2046 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
2047 old_debug_logging
? "1" : "0",
2048 cupsd_num_settings
, &cupsd_settings
);
2050 if (remote_admin
>= 0)
2051 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
2052 remote_admin
? "1" : "0",
2053 cupsd_num_settings
, &cupsd_settings
);
2055 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
2056 old_remote_admin
? "1" : "0",
2057 cupsd_num_settings
, &cupsd_settings
);
2059 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
2060 remote_any
? "1" : "0",
2061 cupsd_num_settings
, &cupsd_settings
);
2063 if (remote_printers
>= 0)
2064 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
2065 remote_printers
? "1" : "0",
2066 cupsd_num_settings
, &cupsd_settings
);
2068 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
2069 old_remote_printers
? "1" : "0",
2070 cupsd_num_settings
, &cupsd_settings
);
2072 if (share_printers
>= 0)
2073 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
2074 share_printers
? "1" : "0",
2075 cupsd_num_settings
, &cupsd_settings
);
2077 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
2078 old_share_printers
? "1" : "0",
2079 cupsd_num_settings
, &cupsd_settings
);
2081 if (user_cancel_any
>= 0)
2082 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
2083 user_cancel_any
? "1" : "0",
2084 cupsd_num_settings
, &cupsd_settings
);
2086 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
2087 old_user_cancel_any
? "1" : "0",
2088 cupsd_num_settings
, &cupsd_settings
);
2091 * Save the new values...
2094 invalidate_cupsd_cache(cg
);
2096 cg
->cupsd_num_settings
= cupsd_num_settings
;
2097 cg
->cupsd_settings
= cupsd_settings
;
2098 cg
->cupsd_update
= time(NULL
);
2100 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
2103 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
2106 * Remote our temp files and return...
2114 return (status
== HTTP_CREATED
);
2119 * 'do_samba_command()' - Do a SAMBA command.
2122 static int /* O - Status of command */
2123 do_samba_command(const char *command
, /* I - Command to run */
2124 const char *address
, /* I - Address for command */
2125 const char *subcmd
, /* I - Sub-command */
2126 const char *authfile
, /* I - Samba authentication file */
2127 FILE *logfile
) /* I - Optional log file */
2130 return (1); /* Always fail on Windows... */
2133 int status
; /* Status of command */
2134 int pid
; /* Process ID of child */
2138 _cupsLangPrintf(logfile
,
2139 _("Running command: %s %s -N -A %s -c \'%s\'\n"),
2140 command
, address
, authfile
, subcmd
);
2142 if ((pid
= fork()) == 0)
2145 * Child goes here, redirect stdin/out/err and execute the command...
2148 int fd
= open("/dev/null", O_RDONLY
);
2157 dup2(fileno(logfile
), 1);
2158 else if ((fd
= open("/dev/null", O_WRONLY
)) > 1)
2166 execlp(command
, command
, address
, "-N", "-A", authfile
, "-c", subcmd
,
2175 _cupsLangPrintf(logfile
, _("Unable to run \"%s\": %s\n"),
2176 command
, strerror(errno
));
2181 * Wait for the process to complete...
2184 while (wait(&status
) != pid
);
2188 _cupsLangPuts(logfile
, "\n");
2190 DEBUG_printf(("9do_samba_command: status=%d", status
));
2192 if (WIFEXITED(status
))
2193 return (WEXITSTATUS(status
));
2195 return (-WTERMSIG(status
));
2201 * 'get_cupsd_conf()' - Get the current cupsd.conf file.
2204 static http_status_t
/* O - Status of request */
2206 http_t
*http
, /* I - Connection to server */
2207 _cups_globals_t
*cg
, /* I - Global data */
2208 time_t last_update
, /* I - Last update time for file */
2209 char *name
, /* I - Filename buffer */
2210 int namesize
, /* I - Size of filename buffer */
2211 int *remote
) /* O - Remote file? */
2213 int fd
; /* Temporary file descriptor */
2215 struct stat info
; /* cupsd.conf file information */
2217 http_status_t status
; /* Status of getting cupsd.conf */
2218 char host
[HTTP_MAX_HOST
]; /* Hostname for connection */
2222 * See if we already have the data we need...
2225 httpGetHostname(http
, host
, sizeof(host
));
2227 if (strcasecmp(cg
->cupsd_hostname
, host
))
2228 invalidate_cupsd_cache(cg
);
2230 snprintf(name
, namesize
, "%s/cupsd.conf", cg
->cups_serverroot
);
2234 if (!strcasecmp(host
, "localhost") && !access(name
, R_OK
))
2237 * Read the local file rather than using HTTP...
2240 if (stat(name
, &info
))
2242 char message
[1024]; /* Message string */
2245 snprintf(message
, sizeof(message
),
2246 _cupsLangString(cupsLangDefault(), _("stat of %s failed: %s")),
2247 name
, strerror(errno
));
2248 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
2252 return (HTTP_SERVER_ERROR
);
2254 else if (last_update
&& info
.st_mtime
<= last_update
)
2255 status
= HTTP_NOT_MODIFIED
;
2263 * Read cupsd.conf via a HTTP GET request...
2266 if ((fd
= cupsTempFd(name
, namesize
)) < 0)
2270 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
, 0);
2272 invalidate_cupsd_cache(cg
);
2274 return (HTTP_SERVER_ERROR
);
2279 httpClearFields(http
);
2282 httpSetField(http
, HTTP_FIELD_IF_MODIFIED_SINCE
,
2283 httpGetDateString(last_update
));
2285 status
= cupsGetFd(http
, "/admin/conf/cupsd.conf", fd
);
2289 if (status
!= HTTP_OK
)
2301 * 'invalidate_cupsd_cache()' - Invalidate the cached cupsd.conf settings.
2305 invalidate_cupsd_cache(
2306 _cups_globals_t
*cg
) /* I - Global data */
2308 cupsFreeOptions(cg
->cupsd_num_settings
, cg
->cupsd_settings
);
2310 cg
->cupsd_hostname
[0] = '\0';
2311 cg
->cupsd_update
= 0;
2312 cg
->cupsd_num_settings
= 0;
2313 cg
->cupsd_settings
= NULL
;
2318 * 'write_option()' - Write a CUPS option to a PPD file.
2322 write_option(cups_file_t
*dstfp
, /* I - PPD file */
2323 int order
, /* I - Order dependency */
2324 const char *name
, /* I - Option name */
2325 const char *text
, /* I - Option text */
2326 const char *attrname
, /* I - Attribute name */
2327 ipp_attribute_t
*suppattr
, /* I - IPP -supported attribute */
2328 ipp_attribute_t
*defattr
, /* I - IPP -default attribute */
2329 int defval
, /* I - Default value number */
2330 int valcount
) /* I - Number of values */
2332 int i
; /* Looping var */
2335 cupsFilePrintf(dstfp
, "*JCLOpenUI *%s/%s: PickOne\n"
2336 "*OrderDependency: %d JCLSetup *%s\n",
2337 name
, text
, order
, name
);
2339 if (defattr
->value_tag
== IPP_TAG_INTEGER
)
2342 * Do numeric options with a range or list...
2345 cupsFilePrintf(dstfp
, "*Default%s: %d\n", name
,
2346 defattr
->values
[defval
].integer
);
2348 if (suppattr
->value_tag
== IPP_TAG_RANGE
)
2351 * List each number in the range...
2354 for (i
= suppattr
->values
[0].range
.lower
;
2355 i
<= suppattr
->values
[0].range
.upper
;
2358 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, i
);
2361 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n",
2363 else if (defval
== 0)
2364 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
, i
);
2365 else if (defval
< (valcount
- 1))
2366 cupsFilePrintf(dstfp
, ",%d\"\n", i
);
2368 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", i
);
2374 * List explicit numbers...
2377 for (i
= 0; i
< suppattr
->num_values
; i
++)
2379 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, suppattr
->values
[i
].integer
);
2382 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n", attrname
,
2383 suppattr
->values
[i
].integer
);
2384 else if (defval
== 0)
2385 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
,
2386 suppattr
->values
[i
].integer
);
2387 else if (defval
< (valcount
- 1))
2388 cupsFilePrintf(dstfp
, ",%d\"\n", suppattr
->values
[i
].integer
);
2390 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", suppattr
->values
[i
].integer
);
2397 * Do text options with a list...
2400 cupsFilePrintf(dstfp
, "*Default%s: %s\n", name
,
2401 defattr
->values
[defval
].string
.text
);
2403 for (i
= 0; i
< suppattr
->num_values
; i
++)
2405 cupsFilePrintf(dstfp
, "*%s %s: \"", name
,
2406 suppattr
->values
[i
].string
.text
);
2409 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\n\"\n*End\n", attrname
,
2410 suppattr
->values
[i
].string
.text
);
2411 else if (defval
== 0)
2412 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\"\n", attrname
,
2413 suppattr
->values
[i
].string
.text
);
2414 else if (defval
< (valcount
- 1))
2415 cupsFilePrintf(dstfp
, ",%s\"\n", suppattr
->values
[i
].string
.text
);
2417 cupsFilePrintf(dstfp
, ",%s\n\"\n*End\n",
2418 suppattr
->values
[i
].string
.text
);
2422 cupsFilePrintf(dstfp
, "*JCLCloseUI: *%s\n\n", name
);
2427 * End of "$Id: adminutil.c 7850 2008-08-20 00:07:25Z mike $".