2 * "$Id: adminutil.c 7850 2008-08-20 00:07:25Z mike $"
4 * Administration utility API definitions for the Common UNIX Printing
7 * Copyright 2007-2009 by Apple Inc.
8 * Copyright 2001-2007 by Easy Software Products.
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * file is missing or damaged, see the license at "http://www.cups.org/".
16 * This file is subject to the Apple OS-Developed Software exception.
20 * cupsAdminCreateWindowsPPD() - Create the Windows PPD file for a printer.
21 * cupsAdminExportSamba() - Export a printer to Samba.
22 * cupsAdminGetServerSettings() - Get settings from the server.
23 * _cupsAdminGetServerSettings() - Get settings from the server (private).
24 * cupsAdminSetServerSettings() - Set settings on the server.
25 * _cupsAdminSetServerSettings() - Set settings on the server (private).
26 * do_samba_command() - Do a SAMBA command.
27 * get_cupsd_conf() - Get the current cupsd.conf file.
28 * invalidate_cupsd_cache() - Invalidate the cached cupsd.conf settings.
29 * write_option() - Write a CUPS option to a PPD file.
33 * Include necessary headers...
36 #include "adminutil.h"
45 # include <sys/wait.h>
53 extern int _cupsAdminGetServerSettings(http_t
*http
,
55 cups_option_t
**settings
);
56 extern int _cupsAdminSetServerSettings(http_t
*http
,
58 cups_option_t
*settings
);
59 static int do_samba_command(const char *command
,
61 const char *subcommand
,
64 static http_status_t
get_cupsd_conf(http_t
*http
, _cups_globals_t
*cg
,
65 time_t last_update
, char *name
,
66 int namelen
, int *remote
);
67 static void invalidate_cupsd_cache(_cups_globals_t
*cg
);
68 static void write_option(cups_file_t
*dstfp
, int order
,
69 const char *name
, const char *text
,
71 ipp_attribute_t
*suppattr
,
72 ipp_attribute_t
*defattr
, int defval
,
77 * 'cupsAdminCreateWindowsPPD()' - Create the Windows PPD file for a printer.
79 * @since CUPS 1.2/Mac OS X 10.5@
82 char * /* O - PPD file or NULL */
83 cupsAdminCreateWindowsPPD(
84 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
85 const char *dest
, /* I - Printer or class */
86 char *buffer
, /* I - Filename buffer */
87 int bufsize
) /* I - Size of filename buffer */
89 const char *src
; /* Source PPD filename */
90 cups_file_t
*srcfp
, /* Source PPD file */
91 *dstfp
; /* Destination PPD file */
92 ipp_t
*request
, /* IPP request */
93 *response
; /* IPP response */
94 ipp_attribute_t
*suppattr
, /* IPP -supported attribute */
95 *defattr
; /* IPP -default attribute */
96 cups_lang_t
*language
; /* Current language */
97 char line
[256], /* Line from PPD file */
98 junk
[256], /* Extra junk to throw away */
99 *ptr
, /* Pointer into line */
100 uri
[1024], /* Printer URI */
101 option
[41], /* Option */
102 choice
[41]; /* Choice */
103 int jcloption
, /* In a JCL option? */
104 jclorder
, /* Next JCL order dependency */
105 linenum
; /* Current line number */
106 time_t curtime
; /* Current time */
107 struct tm
*curdate
; /* Current date */
108 static const char * const pattrs
[] = /* Printer attributes we want */
110 "job-hold-until-supported",
111 "job-hold-until-default",
112 "job-sheets-supported",
113 "job-sheets-default",
114 "job-priority-supported",
115 "job-priority-default"
120 * Range check the input...
127 http
= _cupsConnect();
129 if (!http
|| !dest
|| !buffer
|| bufsize
< 2)
133 * Get the PPD file...
136 if ((src
= cupsGetPPD2(http
, dest
)) == NULL
)
140 * Get the supported banner pages, etc. for the printer...
143 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
145 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
146 "localhost", 0, "/printers/%s", dest
);
147 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
148 "printer-uri", NULL
, uri
);
150 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
151 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
155 * Do the request and get back a response...
158 response
= cupsDoRequest(http
, request
, "/");
159 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
166 * Open the original PPD file...
169 if ((srcfp
= cupsFileOpen(src
, "rb")) == NULL
)
173 * Create a temporary output file using the destination buffer...
176 if ((dstfp
= cupsTempFile2(buffer
, bufsize
)) == NULL
)
178 cupsFileClose(srcfp
);
186 * Write a new header explaining that this isn't the original PPD...
189 cupsFilePuts(dstfp
, "*PPD-Adobe: \"4.3\"\n");
191 curtime
= time(NULL
);
192 curdate
= gmtime(&curtime
);
194 cupsFilePrintf(dstfp
, "*%% Modified on %04d%02d%02d%02d%02d%02d+0000 "
195 "for CUPS Windows Driver\n",
196 curdate
->tm_year
+ 1900, curdate
->tm_mon
+ 1, curdate
->tm_mday
,
197 curdate
->tm_hour
, curdate
->tm_min
, curdate
->tm_sec
);
200 * Read the existing PPD file, converting all PJL commands to CUPS
201 * job ticket comments...
207 language
= cupsLangDefault();
209 while (cupsFileGets(srcfp
, line
, sizeof(line
)))
213 if (!strncmp(line
, "*PPD-Adobe:", 11))
216 * Already wrote the PPD header...
221 else if (!strncmp(line
, "*JCLBegin:", 10) ||
222 !strncmp(line
, "*JCLToPSInterpreter:", 20) ||
223 !strncmp(line
, "*JCLEnd:", 8) ||
224 !strncmp(line
, "*Protocols:", 11))
227 * Don't use existing JCL keywords; we'll create our own, below...
230 cupsFilePrintf(dstfp
, "*%% Commented out for CUPS Windows Driver...\n"
231 "*%%%s\n", line
+ 1);
234 else if (!strncmp(line
, "*JCLOpenUI", 10))
237 cupsFilePrintf(dstfp
, "%s\n", line
);
239 else if (!strncmp(line
, "*JCLCloseUI", 11))
242 cupsFilePrintf(dstfp
, "%s\n", line
);
244 else if (jcloption
&& !strncmp(line
, "*OrderDependency:", 17))
246 for (ptr
= line
+ 17; *ptr
&& isspace(*ptr
& 255); ptr
++);
248 ptr
= strchr(ptr
, ' ');
252 cupsFilePrintf(dstfp
, "*OrderDependency: %d%s\n", jclorder
, ptr
);
256 cupsFilePrintf(dstfp
, "%s\n", line
);
258 else if (jcloption
&&
259 strncmp(line
, "*End", 4) &&
260 strncmp(line
, "*Default", 8))
262 if ((ptr
= strchr(line
, ':')) == NULL
)
264 snprintf(line
, sizeof(line
),
265 _cupsLangString(language
, _("Missing value on line %d!")),
267 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
, 0);
269 cupsFileClose(srcfp
);
270 cupsFileClose(dstfp
);
280 if ((ptr
= strchr(ptr
, '\"')) == NULL
)
282 snprintf(line
, sizeof(line
),
283 _cupsLangString(language
,
284 _("Missing double quote on line %d!")),
286 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
, 0);
288 cupsFileClose(srcfp
);
289 cupsFileClose(dstfp
);
299 if (sscanf(line
, "*%40s%*[ \t]%40[^:/]", option
, choice
) != 2)
301 snprintf(line
, sizeof(line
),
302 _cupsLangString(language
,
303 _("Bad option + choice on line %d!")),
305 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
, 0);
307 cupsFileClose(srcfp
);
308 cupsFileClose(dstfp
);
318 if (strchr(ptr
+ 1, '\"') == NULL
)
324 while (cupsFileGets(srcfp
, junk
, sizeof(junk
)) != NULL
)
328 if (!strncmp(junk
, "*End", 4))
333 snprintf(ptr
+ 1, sizeof(line
) - (ptr
- line
+ 1),
334 "%%cupsJobTicket: %s=%s\n\"\n*End", option
, choice
);
336 cupsFilePrintf(dstfp
, "*%% Changed for CUPS Windows Driver...\n%s\n",
340 cupsFilePrintf(dstfp
, "%s\n", line
);
343 cupsFileClose(srcfp
);
348 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, _("Empty PPD file!"), 1);
350 cupsFileClose(dstfp
);
359 * Now add the CUPS-specific attributes and options...
362 cupsFilePuts(dstfp
, "\n*% CUPS Job Ticket support and options...\n");
363 cupsFilePuts(dstfp
, "*Protocols: PJL\n");
364 cupsFilePuts(dstfp
, "*JCLBegin: \"%!PS-Adobe-3.0<0A>\"\n");
365 cupsFilePuts(dstfp
, "*JCLToPSInterpreter: \"\"\n");
366 cupsFilePuts(dstfp
, "*JCLEnd: \"\"\n");
368 cupsFilePuts(dstfp
, "\n*OpenGroup: CUPS/CUPS Options\n\n");
370 if ((defattr
= ippFindAttribute(response
, "job-hold-until-default",
371 IPP_TAG_ZERO
)) != NULL
&&
372 (suppattr
= ippFindAttribute(response
, "job-hold-until-supported",
373 IPP_TAG_ZERO
)) != NULL
)
374 write_option(dstfp
, jclorder
++, "cupsJobHoldUntil", "Hold Until",
375 "job-hold-until", suppattr
, defattr
, 0, 1);
377 if ((defattr
= ippFindAttribute(response
, "job-priority-default",
378 IPP_TAG_INTEGER
)) != NULL
&&
379 (suppattr
= ippFindAttribute(response
, "job-priority-supported",
380 IPP_TAG_RANGE
)) != NULL
)
381 write_option(dstfp
, jclorder
++, "cupsJobPriority", "Priority",
382 "job-priority", suppattr
, defattr
, 0, 1);
384 if ((defattr
= ippFindAttribute(response
, "job-sheets-default",
385 IPP_TAG_ZERO
)) != NULL
&&
386 (suppattr
= ippFindAttribute(response
, "job-sheets-supported",
387 IPP_TAG_ZERO
)) != NULL
)
389 write_option(dstfp
, jclorder
++, "cupsJobSheetsStart", "Start Banner",
390 "job-sheets", suppattr
, defattr
, 0, 2);
391 write_option(dstfp
, jclorder
, "cupsJobSheetsEnd", "End Banner",
392 "job-sheets", suppattr
, defattr
, 1, 2);
395 cupsFilePuts(dstfp
, "*CloseGroup: CUPS\n");
396 cupsFileClose(dstfp
);
405 * 'cupsAdminExportSamba()' - Export a printer to Samba.
407 * @since CUPS 1.2/Mac OS X 10.5@
410 int /* O - 1 on success, 0 on failure */
411 cupsAdminExportSamba(
412 const char *dest
, /* I - Destination to export */
413 const char *ppd
, /* I - PPD file */
414 const char *samba_server
, /* I - Samba server */
415 const char *samba_user
, /* I - Samba username */
416 const char *samba_password
, /* I - Samba password */
417 FILE *logfile
) /* I - Log file, if any */
419 int status
; /* Status of Samba commands */
420 int have_drivers
; /* Have drivers? */
421 char file
[1024], /* File to test for */
422 authfile
[1024], /* Temporary authentication file */
423 address
[1024], /* Address for command */
424 subcmd
[1024], /* Sub-command */
425 message
[1024]; /* Error message */
426 cups_file_t
*fp
; /* Authentication file */
427 cups_lang_t
*language
; /* Current language */
428 _cups_globals_t
*cg
= _cupsGlobals();
433 * Range check input...
436 if (!dest
|| !ppd
|| !samba_server
|| !samba_user
|| !samba_password
)
438 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
443 * Create a temporary authentication file for Samba...
446 if ((fp
= cupsTempFile2(authfile
, sizeof(authfile
))) == NULL
)
448 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
, 0);
452 cupsFilePrintf(fp
, "username = %s\n", samba_user
);
453 cupsFilePrintf(fp
, "password = %s\n", samba_password
);
457 * See which drivers are available; the new CUPS v6 and Adobe drivers
458 * depend on the Windows 2k PS driver, so copy that driver first:
469 language
= cupsLangDefault();
471 snprintf(file
, sizeof(file
), "%s/drivers/pscript5.dll", cg
->cups_datadir
);
472 if (!access(file
, 0))
477 * Windows 2k driver is installed; do the smbclient commands needed
478 * to copy the Win2k drivers over...
481 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
483 snprintf(subcmd
, sizeof(subcmd
),
485 "put %s W32X86/%s.ppd;"
486 "put %s/drivers/ps5ui.dll W32X86/ps5ui.dll;"
487 "put %s/drivers/pscript.hlp W32X86/pscript.hlp;"
488 "put %s/drivers/pscript.ntf W32X86/pscript.ntf;"
489 "put %s/drivers/pscript5.dll W32X86/pscript5.dll",
490 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
491 cg
->cups_datadir
, cg
->cups_datadir
);
493 if ((status
= do_samba_command("smbclient", address
, subcmd
,
494 authfile
, logfile
)) != 0)
496 snprintf(message
, sizeof(message
),
497 _cupsLangString(language
,
498 _("Unable to copy Windows 2000 printer "
499 "driver files (%d)!")), status
);
501 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
504 _cupsLangPrintf(logfile
, "%s\n", message
);
512 * See if we also have the CUPS driver files; if so, use them!
515 snprintf(file
, sizeof(file
), "%s/drivers/cupsps6.dll", cg
->cups_datadir
);
516 if (!access(file
, 0))
519 * Copy the CUPS driver files over...
522 snprintf(subcmd
, sizeof(subcmd
),
523 "put %s/drivers/cups6.ini W32X86/cups6.ini;"
524 "put %s/drivers/cupsps6.dll W32X86/cupsps6.dll;"
525 "put %s/drivers/cupsui6.dll W32X86/cupsui6.dll",
526 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
528 if ((status
= do_samba_command("smbclient", address
, subcmd
,
529 authfile
, logfile
)) != 0)
531 snprintf(message
, sizeof(message
),
532 _cupsLangString(language
,
533 _("Unable to copy CUPS printer driver "
534 "files (%d)!")), status
);
536 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
539 _cupsLangPrintf(logfile
, "%s\n", message
);
547 * Do the rpcclient command needed for the CUPS drivers...
550 snprintf(subcmd
, sizeof(subcmd
),
551 "adddriver \"Windows NT x86\" \"%s:"
552 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
553 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
554 "cups6.ini,cupsps6.dll,cupsui6.dll\"",
560 * Don't have the CUPS drivers, so just use the standard Windows
564 snprintf(subcmd
, sizeof(subcmd
),
565 "adddriver \"Windows NT x86\" \"%s:"
566 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
567 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
571 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
572 authfile
, logfile
)) != 0)
574 snprintf(message
, sizeof(message
),
575 _cupsLangString(language
,
576 _("Unable to install Windows 2000 printer "
577 "driver files (%d)!")), status
);
579 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
582 _cupsLangPrintf(logfile
, "%s\n", message
);
591 * See if we have the Win9x PS driver...
594 snprintf(file
, sizeof(file
), "%s/drivers/ADOBEPS4.DRV", cg
->cups_datadir
);
595 if (!access(file
, 0))
600 * Do the smbclient commands needed for the Adobe Win9x drivers...
603 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
605 snprintf(subcmd
, sizeof(subcmd
),
607 "put %s WIN40/%s.PPD;"
608 "put %s/drivers/ADFONTS.MFM WIN40/ADFONTS.MFM;"
609 "put %s/drivers/ADOBEPS4.DRV WIN40/ADOBEPS4.DRV;"
610 "put %s/drivers/ADOBEPS4.HLP WIN40/ADOBEPS4.HLP;"
611 "put %s/drivers/ICONLIB.DLL WIN40/ICONLIB.DLL;"
612 "put %s/drivers/PSMON.DLL WIN40/PSMON.DLL;",
613 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
614 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
616 if ((status
= do_samba_command("smbclient", address
, subcmd
,
617 authfile
, logfile
)) != 0)
619 snprintf(message
, sizeof(message
),
620 _cupsLangString(language
,
621 _("Unable to copy Windows 9x printer "
622 "driver files (%d)!")), status
);
624 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
627 _cupsLangPrintf(logfile
, "%s\n", message
);
635 * Do the rpcclient commands needed for the Adobe Win9x drivers...
638 snprintf(subcmd
, sizeof(subcmd
),
639 "adddriver \"Windows 4.0\" \"%s:ADOBEPS4.DRV:%s.PPD:NULL:"
640 "ADOBEPS4.HLP:PSMON.DLL:RAW:"
641 "ADOBEPS4.DRV,%s.PPD,ADOBEPS4.HLP,PSMON.DLL,ADFONTS.MFM,"
645 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
646 authfile
, logfile
)) != 0)
648 snprintf(message
, sizeof(message
),
649 _cupsLangString(language
,
650 _("Unable to install Windows 9x printer "
651 "driver files (%d)!")), status
);
653 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
656 _cupsLangPrintf(logfile
, "%s\n", message
);
665 * See if we have the 64-bit Windows PS driver...
675 snprintf(file
, sizeof(file
), "%s/drivers/x64/pscript5.dll", cg
->cups_datadir
);
676 if (!access(file
, 0))
681 * 64-bit Windows driver is installed; do the smbclient commands needed
682 * to copy the Win64 drivers over...
685 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
687 snprintf(subcmd
, sizeof(subcmd
),
690 "put %s/drivers/x64/ps5ui.dll x64/ps5ui.dll;"
691 "put %s/drivers/x64/pscript.hlp x64/pscript.hlp;"
692 "put %s/drivers/x64/pscript.ntf x64/pscript.ntf;"
693 "put %s/drivers/x64/pscript5.dll x64/pscript5.dll",
694 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
695 cg
->cups_datadir
, cg
->cups_datadir
);
697 if ((status
= do_samba_command("smbclient", address
, subcmd
,
698 authfile
, logfile
)) != 0)
700 snprintf(message
, sizeof(message
),
701 _cupsLangString(language
,
702 _("Unable to copy 64-bit Windows printer "
703 "driver files (%d)!")), status
);
705 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
708 _cupsLangPrintf(logfile
, "%s\n", message
);
716 * See if we also have the CUPS driver files; if so, use them!
719 snprintf(file
, sizeof(file
), "%s/drivers/x64/cupsps6.dll", cg
->cups_datadir
);
720 if (!access(file
, 0))
723 * Copy the CUPS driver files over...
726 snprintf(subcmd
, sizeof(subcmd
),
727 "put %s/drivers/x64/cups6.ini x64/cups6.ini;"
728 "put %s/drivers/x64/cupsps6.dll x64/cupsps6.dll;"
729 "put %s/drivers/x64/cupsui6.dll x64/cupsui6.dll",
730 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
732 if ((status
= do_samba_command("smbclient", address
, subcmd
,
733 authfile
, logfile
)) != 0)
735 snprintf(message
, sizeof(message
),
736 _cupsLangString(language
,
737 _("Unable to copy 64-bit CUPS printer driver "
738 "files (%d)!")), status
);
740 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
743 _cupsLangPrintf(logfile
, "%s\n", message
);
751 * Do the rpcclient command needed for the CUPS drivers...
754 snprintf(subcmd
, sizeof(subcmd
),
755 "adddriver \"Windows x64\" \"%s:"
756 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
757 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
758 "cups6.ini,cupsps6.dll,cupsui6.dll\"",
764 * Don't have the CUPS drivers, so just use the standard Windows
768 snprintf(subcmd
, sizeof(subcmd
),
769 "adddriver \"Windows x64\" \"%s:"
770 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
771 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
775 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
776 authfile
, logfile
)) != 0)
778 snprintf(message
, sizeof(message
),
779 _cupsLangString(language
,
780 _("Unable to install Windows 2000 printer "
781 "driver files (%d)!")), status
);
783 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
786 _cupsLangPrintf(logfile
, "%s\n", message
);
794 if (logfile
&& !(have_drivers
& 1))
798 _cupsLangString(language
,
799 _("No Windows printer drivers are installed!")),
803 _cupsLangString(language
,
804 _("Warning, no Windows 2000 printer drivers "
808 _cupsSetError(IPP_NOT_FOUND
, message
, 0);
809 _cupsLangPrintf(logfile
, "%s\n", message
);
812 if (have_drivers
== 0)
814 _cupsSetError(IPP_NOT_FOUND
, message
, 0);
822 * Finally, associate the drivers we just added with the queue...
825 snprintf(subcmd
, sizeof(subcmd
), "setdriver %s %s", dest
, dest
);
827 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
828 authfile
, logfile
)) != 0)
830 snprintf(message
, sizeof(message
),
831 _cupsLangString(language
,
832 _("Unable to set Windows printer driver (%d)!")),
835 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
838 _cupsLangPrintf(logfile
, "%s\n", message
);
852 * 'cupsAdminGetServerSettings()' - Get settings from the server.
854 * The returned settings should be freed with cupsFreeOptions() when
855 * you are done with them.
857 * @since CUPS 1.3/Mac OS X 10.5@
860 int /* O - 1 on success, 0 on failure */
861 cupsAdminGetServerSettings(
862 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
863 int *num_settings
, /* O - Number of settings */
864 cups_option_t
**settings
) /* O - Settings */
866 return (_cupsAdminGetServerSettings(http
, num_settings
, settings
));
871 * '_cupsAdminGetServerSettings()' - Get settings from the server.
873 * The returned settings should be freed with cupsFreeOptions() when
874 * you are done with them.
879 int /* O - 1 on success, 0 on failure */
880 _cupsAdminGetServerSettings(
881 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
882 int *num_settings
, /* O - Number of settings */
883 cups_option_t
**settings
) /* O - Settings */
885 int i
; /* Looping var */
886 cups_file_t
*cupsd
; /* cupsd.conf file */
887 char cupsdconf
[1024]; /* cupsd.conf filename */
888 int remote
; /* Remote cupsd.conf file? */
889 http_status_t status
; /* Status of getting cupsd.conf */
890 char line
[1024], /* Line from cupsd.conf file */
891 *value
; /* Value on line */
892 cups_option_t
*setting
; /* Current setting */
893 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
897 * Range check input...
903 * See if we are connected to the same server...
909 * Compare the connection hostname, port, and encryption settings to
910 * the cached defaults; these were initialized the first time we
914 if (strcmp(cg
->http
->hostname
, cg
->server
) ||
915 cg
->ipp_port
!= _httpAddrPort(cg
->http
->hostaddr
) ||
916 (cg
->http
->encryption
!= cg
->encryption
&&
917 cg
->http
->encryption
== HTTP_ENCRYPT_NEVER
))
920 * Need to close the current connection because something has changed...
929 * (Re)connect as needed...
934 if ((cg
->http
= _httpCreate(cupsServer(), ippPort(),
935 cupsEncryption())) == NULL
)
938 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, NULL
, 0);
940 _cupsSetError(IPP_SERVICE_UNAVAILABLE
,
941 _("Unable to connect to host."), 1);
954 if (!http
|| !num_settings
|| !settings
)
956 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
971 * Get the cupsd.conf file...
974 if ((status
= get_cupsd_conf(http
, cg
, cg
->cupsd_update
, cupsdconf
,
975 sizeof(cupsdconf
), &remote
)) == HTTP_OK
)
977 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
979 char message
[1024]; /* Message string */
982 snprintf(message
, sizeof(message
),
983 _cupsLangString(cupsLangDefault(), _("Open of %s failed: %s")),
984 cupsdconf
, strerror(errno
));
985 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
994 * Read the file, keeping track of what settings are enabled...
997 int remote_access
= 0, /* Remote access allowed? */
998 remote_admin
= 0, /* Remote administration allowed? */
999 remote_any
= 0, /* Remote access from anywhere allowed? */
1000 browsing
= 1, /* Browsing enabled? */
1001 browse_allow
= 1, /* Browse address set? */
1002 browse_address
= 0, /* Browse address set? */
1003 cancel_policy
= 1, /* Cancel-job policy set? */
1004 debug_logging
= 0; /* LogLevel debug set? */
1005 int linenum
= 0, /* Line number in file */
1006 in_location
= 0, /* In a location section? */
1007 in_policy
= 0, /* In a policy section? */
1008 in_cancel_job
= 0, /* In a cancel-job section? */
1009 in_admin_location
= 0; /* In the /admin location? */
1012 invalidate_cupsd_cache(cg
);
1014 cg
->cupsd_update
= time(NULL
);
1015 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
1017 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
1019 if (!value
&& strncmp(line
, "</", 2))
1020 value
= line
+ strlen(line
);
1022 if ((!strcasecmp(line
, "Port") || !strcasecmp(line
, "Listen")) && value
)
1024 char *port
; /* Pointer to port number, if any */
1027 if ((port
= strrchr(value
, ':')) != NULL
)
1029 else if (isdigit(*value
& 255))
1032 * Listen on a port number implies remote access...
1039 if (strcasecmp(value
, "localhost") && strcmp(value
, "127.0.0.1")
1042 #endif /* AF_LOCAL */
1044 && strcmp(value
, "::1")
1045 #endif /* AF_INET6 */
1049 else if (!strcasecmp(line
, "Browsing"))
1051 browsing
= !strcasecmp(value
, "yes") || !strcasecmp(value
, "on") ||
1052 !strcasecmp(value
, "true");
1054 else if (!strcasecmp(line
, "BrowseAddress"))
1058 else if (!strcasecmp(line
, "BrowseAllow"))
1062 else if (!strcasecmp(line
, "BrowseOrder"))
1064 browse_allow
= !strncasecmp(value
, "deny,", 5);
1066 else if (!strcasecmp(line
, "LogLevel"))
1068 debug_logging
= !strncasecmp(value
, "debug", 5);
1070 else if (!strcasecmp(line
, "<Policy") && !strcasecmp(value
, "default"))
1074 else if (!strcasecmp(line
, "</Policy>"))
1078 else if (!strcasecmp(line
, "<Limit") && in_policy
&& value
)
1081 * See if the policy limit is for the Cancel-Job operation...
1084 char *valptr
; /* Pointer into value */
1089 for (valptr
= value
; !isspace(*valptr
& 255) && *valptr
; valptr
++);
1094 if (!strcasecmp(value
, "cancel-job") || !strcasecmp(value
, "all"))
1100 for (value
= valptr
; isspace(*value
& 255); value
++);
1103 else if (!strcasecmp(line
, "</Limit>"))
1107 else if (!strcasecmp(line
, "Require") && in_cancel_job
)
1111 else if (!strcasecmp(line
, "<Location") && value
)
1113 in_admin_location
= !strcasecmp(value
, "/admin");
1116 else if (!strcasecmp(line
, "</Location>"))
1118 in_admin_location
= 0;
1121 else if (!strcasecmp(line
, "Allow") && value
&&
1122 strcasecmp(value
, "localhost") && strcasecmp(value
, "127.0.0.1")
1125 #endif /* AF_LOCAL */
1127 && strcmp(value
, "::1")
1128 #endif /* AF_INET6 */
1131 if (in_admin_location
)
1133 else if (!strcasecmp(value
, "all"))
1136 else if (line
[0] != '<' && !in_location
&& !in_policy
&&
1137 strcasecmp(line
, "Allow") &&
1138 strcasecmp(line
, "AuthType") &&
1139 strcasecmp(line
, "Deny") &&
1140 strcasecmp(line
, "Order") &&
1141 strcasecmp(line
, "Require") &&
1142 strcasecmp(line
, "Satisfy"))
1143 cg
->cupsd_num_settings
= cupsAddOption(line
, value
,
1144 cg
->cupsd_num_settings
,
1145 &(cg
->cupsd_settings
));
1148 cupsFileClose(cupsd
);
1150 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1151 debug_logging
? "1" : "0",
1152 cg
->cupsd_num_settings
,
1153 &(cg
->cupsd_settings
));
1155 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1156 (remote_access
&& remote_admin
) ?
1158 cg
->cupsd_num_settings
,
1159 &(cg
->cupsd_settings
));
1161 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
1162 remote_any
? "1" : "0",
1163 cg
->cupsd_num_settings
,
1164 &(cg
->cupsd_settings
));
1166 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
1167 (browsing
&& browse_allow
) ?
1169 cg
->cupsd_num_settings
,
1170 &(cg
->cupsd_settings
));
1172 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1173 (remote_access
&& browsing
&&
1174 browse_address
) ? "1" : "0",
1175 cg
->cupsd_num_settings
,
1176 &(cg
->cupsd_settings
));
1178 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1179 cancel_policy
? "1" : "0",
1180 cg
->cupsd_num_settings
,
1181 &(cg
->cupsd_settings
));
1183 else if (status
!= HTTP_NOT_MODIFIED
)
1184 invalidate_cupsd_cache(cg
);
1187 * Remove any temporary files and copy the settings array...
1193 for (i
= cg
->cupsd_num_settings
, setting
= cg
->cupsd_settings
;
1196 *num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1197 *num_settings
, settings
);
1199 return (cg
->cupsd_num_settings
> 0);
1204 * 'cupsAdminSetServerSettings()' - Set settings on the server.
1206 * @since CUPS 1.3/Mac OS X 10.5@
1209 int /* O - 1 on success, 0 on failure */
1210 cupsAdminSetServerSettings(
1211 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1212 int num_settings
, /* I - Number of settings */
1213 cups_option_t
*settings
) /* I - Settings */
1215 return (_cupsAdminSetServerSettings(http
, num_settings
, settings
));
1220 * '_cupsAdminSetServerSettings()' - Set settings on the server.
1225 int /* O - 1 on success, 0 on failure */
1226 _cupsAdminSetServerSettings(
1227 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1228 int num_settings
, /* I - Number of settings */
1229 cups_option_t
*settings
) /* I - Settings */
1231 int i
; /* Looping var */
1232 http_status_t status
; /* GET/PUT status */
1233 const char *server_port_env
; /* SERVER_PORT env var */
1234 int server_port
; /* IPP port for server */
1235 cups_file_t
*cupsd
; /* cupsd.conf file */
1236 char cupsdconf
[1024]; /* cupsd.conf filename */
1237 int remote
; /* Remote cupsd.conf file? */
1238 char tempfile
[1024]; /* Temporary new cupsd.conf */
1239 cups_file_t
*temp
; /* Temporary file */
1240 char line
[1024], /* Line from cupsd.conf file */
1241 *value
; /* Value on line */
1242 int linenum
, /* Line number in file */
1243 in_location
, /* In a location section? */
1244 in_policy
, /* In a policy section? */
1245 in_default_policy
, /* In the default policy section? */
1246 in_cancel_job
, /* In a cancel-job section? */
1247 in_admin_location
, /* In the /admin location? */
1248 in_conf_location
, /* In the /admin/conf location? */
1249 in_root_location
; /* In the / location? */
1250 const char *val
; /* Setting value */
1251 int remote_printers
, /* Show remote printers */
1252 share_printers
, /* Share local printers */
1253 remote_admin
, /* Remote administration allowed? */
1254 remote_any
, /* Remote access from anywhere? */
1255 user_cancel_any
, /* Cancel-job policy set? */
1256 debug_logging
; /* LogLevel debug set? */
1257 int wrote_port_listen
, /* Wrote the port/listen lines? */
1258 wrote_browsing
, /* Wrote the browsing lines? */
1259 wrote_policy
, /* Wrote the policy? */
1260 wrote_loglevel
, /* Wrote the LogLevel line? */
1261 wrote_admin_location
, /* Wrote the /admin location? */
1262 wrote_conf_location
, /* Wrote the /admin/conf location? */
1263 wrote_root_location
; /* Wrote the / location? */
1264 int indent
; /* Indentation */
1265 int cupsd_num_settings
; /* New number of settings */
1266 int old_remote_printers
, /* Show remote printers */
1267 old_share_printers
, /* Share local printers */
1268 old_remote_admin
, /* Remote administration allowed? */
1269 old_user_cancel_any
, /* Cancel-job policy set? */
1270 old_debug_logging
; /* LogLevel debug set? */
1271 cups_option_t
*cupsd_settings
, /* New settings */
1272 *setting
; /* Current setting */
1273 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1277 * Range check input...
1281 http
= _cupsConnect();
1283 if (!http
|| !num_settings
|| !settings
)
1285 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
1291 * Get the cupsd.conf file...
1294 if (get_cupsd_conf(http
, cg
, 0, cupsdconf
, sizeof(cupsdconf
),
1295 &remote
) == HTTP_OK
)
1297 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
1299 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
, 0);
1307 * Get current settings...
1310 if (!_cupsAdminGetServerSettings(http
, &cupsd_num_settings
,
1314 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, cupsd_num_settings
,
1315 cupsd_settings
)) != NULL
)
1316 old_debug_logging
= atoi(val
);
1318 old_debug_logging
= 0;
1320 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, cupsd_num_settings
,
1321 cupsd_settings
)) != NULL
)
1322 old_remote_admin
= atoi(val
);
1324 old_remote_admin
= 0;
1326 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, cupsd_num_settings
,
1327 cupsd_settings
)) != NULL
)
1328 remote_any
= atoi(val
);
1332 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
, cupsd_num_settings
,
1333 cupsd_settings
)) != NULL
)
1334 old_remote_printers
= atoi(val
);
1336 old_remote_printers
= 1;
1338 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, cupsd_num_settings
,
1339 cupsd_settings
)) != NULL
)
1340 old_share_printers
= atoi(val
);
1342 old_share_printers
= 0;
1344 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, cupsd_num_settings
,
1345 cupsd_settings
)) != NULL
)
1346 old_user_cancel_any
= atoi(val
);
1348 old_user_cancel_any
= 0;
1350 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
1353 * Get basic settings...
1356 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, num_settings
,
1359 debug_logging
= atoi(val
);
1361 if (debug_logging
== old_debug_logging
)
1364 * No change to this setting...
1373 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, num_settings
,
1375 remote_any
= atoi(val
);
1377 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, num_settings
,
1380 remote_admin
= atoi(val
);
1382 if (remote_admin
== old_remote_admin
&& remote_any
< 0)
1385 * No change to this setting...
1394 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
, num_settings
,
1397 remote_printers
= atoi(val
);
1399 if (remote_printers
== old_remote_printers
)
1402 * No change to this setting...
1405 remote_printers
= -1;
1409 remote_printers
= -1;
1411 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, num_settings
,
1414 share_printers
= atoi(val
);
1416 if (share_printers
== old_share_printers
&& remote_any
< 0)
1419 * No change to this setting...
1422 share_printers
= -1;
1426 share_printers
= -1;
1428 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, num_settings
,
1431 user_cancel_any
= atoi(val
);
1433 if (user_cancel_any
== old_user_cancel_any
)
1436 * No change to this setting...
1439 user_cancel_any
= -1;
1443 user_cancel_any
= -1;
1446 * Create a temporary file for the new cupsd.conf file...
1449 if ((temp
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1451 cupsFileClose(cupsd
);
1456 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
, 0);
1461 * Copy the old file to the new, making changes along the way...
1464 cupsd_num_settings
= 0;
1465 in_admin_location
= 0;
1467 in_conf_location
= 0;
1468 in_default_policy
= 0;
1471 in_root_location
= 0;
1473 wrote_admin_location
= 0;
1475 wrote_conf_location
= 0;
1478 wrote_port_listen
= 0;
1479 wrote_root_location
= 0;
1482 if ((server_port_env
= getenv("SERVER_PORT")) != NULL
)
1484 if ((server_port
= atoi(server_port_env
)) <= 0)
1485 server_port
= ippPort();
1488 server_port
= ippPort();
1490 if (server_port
<= 0)
1491 server_port
= IPP_PORT
;
1493 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
1495 if ((!strcasecmp(line
, "Port") || !strcasecmp(line
, "Listen")) &&
1496 (share_printers
>= 0 || remote_admin
>= 0))
1498 if (!wrote_port_listen
)
1500 wrote_port_listen
= 1;
1502 if (share_printers
> 0 || remote_admin
> 0)
1504 cupsFilePuts(temp
, "# Allow remote access\n");
1505 cupsFilePrintf(temp
, "Port %d\n", server_port
);
1509 cupsFilePuts(temp
, "# Only listen for connections from the local "
1511 cupsFilePrintf(temp
, "Listen localhost:%d\n", server_port
);
1514 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1515 if ((!value
|| strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)) &&
1516 !access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1517 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1518 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1520 else if (value
&& value
[0] == '/'
1521 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1522 && strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)
1523 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1525 cupsFilePrintf(temp
, "Listen %s\n", value
);
1527 else if ((!strcasecmp(line
, "Browsing") ||
1528 !strcasecmp(line
, "BrowseAddress") ||
1529 !strcasecmp(line
, "BrowseAllow") ||
1530 !strcasecmp(line
, "BrowseDeny") ||
1531 !strcasecmp(line
, "BrowseLocalProtocols") ||
1532 !strcasecmp(line
, "BrowseRemoteProtocols") ||
1533 !strcasecmp(line
, "BrowseOrder")) &&
1534 (remote_printers
>= 0 || share_printers
>= 0))
1536 if (!wrote_browsing
)
1538 int new_remote_printers
= (remote_printers
> 0 ||
1539 (remote_printers
== -1 &&
1540 old_remote_printers
> 0));
1541 int new_share_printers
= (share_printers
> 0 ||
1542 (share_printers
== -1 &&
1543 old_share_printers
> 0));
1547 if (new_remote_printers
|| new_share_printers
)
1549 const char *localp
= cupsGetOption("BrowseLocalProtocols",
1550 num_settings
, settings
);
1551 const char *remotep
= cupsGetOption("BrowseRemoteProtocols",
1552 num_settings
, settings
);
1555 localp
= cupsGetOption("BrowseLocalProtocols", cupsd_num_settings
,
1559 remotep
= cupsGetOption("BrowseRemoteProtocols", cupsd_num_settings
,
1562 if (new_remote_printers
&& new_share_printers
)
1564 "# Enable printer sharing and shared printers.\n");
1565 else if (new_remote_printers
)
1567 "# Show shared printers on the local network.\n");
1570 "# Share local printers on the local network.\n");
1572 cupsFilePuts(temp
, "Browsing On\n");
1573 cupsFilePuts(temp
, "BrowseOrder allow,deny\n");
1575 if (new_remote_printers
)
1577 cupsFilePuts(temp
, "BrowseAllow all\n");
1580 remotep
= CUPS_DEFAULT_BROWSE_REMOTE_PROTOCOLS
;
1582 cupsFilePrintf(temp
, "BrowseRemoteProtocols %s\n", remotep
);
1585 cupsFilePuts(temp
, "BrowseRemoteProtocols\n");
1587 cupsd_num_settings
= cupsAddOption("BrowseRemoteProtocols", remotep
,
1591 if (new_share_printers
)
1593 cupsFilePuts(temp
, "BrowseAddress @LOCAL\n");
1596 localp
= CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS
;
1598 cupsFilePrintf(temp
, "BrowseLocalProtocols %s\n", localp
);
1601 cupsFilePuts(temp
, "BrowseLocalProtocols\n");
1603 cupsd_num_settings
= cupsAddOption("BrowseLocalProtocols", localp
,
1610 "# Disable printer sharing and shared printers.\n");
1611 cupsFilePuts(temp
, "Browsing Off\n");
1615 else if (!strcasecmp(line
, "LogLevel") && debug_logging
>= 0)
1622 "# Show troubleshooting information in error_log.\n");
1623 cupsFilePuts(temp
, "LogLevel debug\n");
1627 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1628 cupsFilePuts(temp
, "LogLevel " CUPS_DEFAULT_LOG_LEVEL
"\n");
1631 else if (!strcasecmp(line
, "<Policy"))
1633 in_default_policy
= !strcasecmp(value
, "default");
1636 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1639 else if (!strcasecmp(line
, "</Policy>"))
1642 if (!wrote_policy
&& in_default_policy
)
1646 if (!user_cancel_any
)
1647 cupsFilePuts(temp
, " # Only the owner or an administrator can "
1649 " <Limit Cancel-Job>\n"
1650 " Order deny,allow\n"
1651 " Require user @OWNER "
1652 CUPS_DEFAULT_PRINTOPERATOR_AUTH
"\n"
1657 in_default_policy
= 0;
1659 cupsFilePuts(temp
, "</Policy>\n");
1661 else if (!strcasecmp(line
, "<Location"))
1665 if (!strcmp(value
, "/admin"))
1666 in_admin_location
= 1;
1667 if (!strcmp(value
, "/admin/conf"))
1668 in_conf_location
= 1;
1669 else if (!strcmp(value
, "/"))
1670 in_root_location
= 1;
1672 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1674 else if (!strcasecmp(line
, "</Location>"))
1678 if (in_admin_location
&& remote_admin
>= 0)
1680 wrote_admin_location
= 1;
1683 cupsFilePuts(temp
, " # Allow remote administration...\n");
1684 else if (remote_admin
== 0)
1685 cupsFilePuts(temp
, " # Restrict access to the admin pages...\n");
1687 cupsFilePuts(temp
, " Order allow,deny\n");
1690 cupsFilePrintf(temp
, " Allow %s\n",
1691 remote_any
> 0 ? "all" : "@LOCAL");
1693 else if (in_conf_location
&& remote_admin
>= 0)
1695 wrote_conf_location
= 1;
1698 cupsFilePuts(temp
, " # Allow remote access to the configuration "
1701 cupsFilePuts(temp
, " # Restrict access to the configuration "
1704 cupsFilePuts(temp
, " Order allow,deny\n");
1707 cupsFilePrintf(temp
, " Allow %s\n",
1708 remote_any
> 0 ? "all" : "@LOCAL");
1710 else if (in_root_location
&& (remote_admin
>= 0 || share_printers
>= 0))
1712 wrote_root_location
= 1;
1714 if (remote_admin
> 0 && share_printers
> 0)
1715 cupsFilePuts(temp
, " # Allow shared printing and remote "
1716 "administration...\n");
1717 else if (remote_admin
> 0)
1718 cupsFilePuts(temp
, " # Allow remote administration...\n");
1719 else if (share_printers
> 0)
1720 cupsFilePuts(temp
, " # Allow shared printing...\n");
1722 cupsFilePuts(temp
, " # Restrict access to the server...\n");
1724 cupsFilePuts(temp
, " Order allow,deny\n");
1726 if (remote_admin
> 0 || share_printers
> 0)
1727 cupsFilePrintf(temp
, " Allow %s\n",
1728 remote_any
> 0 ? "all" : "@LOCAL");
1731 in_admin_location
= 0;
1732 in_conf_location
= 0;
1733 in_root_location
= 0;
1735 cupsFilePuts(temp
, "</Location>\n");
1737 else if (!strcasecmp(line
, "<Limit") && in_default_policy
)
1740 * See if the policy limit is for the Cancel-Job operation...
1743 char *valptr
; /* Pointer into value */
1748 if (!strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1751 * Don't write anything for this limit section...
1758 cupsFilePrintf(temp
, " %s", line
);
1762 for (valptr
= value
; !isspace(*valptr
& 255) && *valptr
; valptr
++);
1767 if (!strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1770 * Write everything except for this definition...
1776 cupsFilePrintf(temp
, " %s", value
);
1778 for (value
= valptr
; isspace(*value
& 255); value
++);
1781 cupsFilePuts(temp
, ">\n");
1784 else if (!strcasecmp(line
, "</Limit>") && in_cancel_job
)
1788 if (in_cancel_job
== 1)
1789 cupsFilePuts(temp
, " </Limit>\n");
1793 if (!user_cancel_any
)
1794 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1796 " <Limit Cancel-Job>\n"
1797 " Order deny,allow\n"
1798 " Require user @OWNER "
1799 CUPS_DEFAULT_PRINTOPERATOR_AUTH
"\n"
1804 else if ((((in_admin_location
|| in_conf_location
|| in_root_location
) &&
1805 remote_admin
>= 0) ||
1806 (in_root_location
&& share_printers
>= 0)) &&
1807 (!strcasecmp(line
, "Allow") || !strcasecmp(line
, "Deny") ||
1808 !strcasecmp(line
, "Order")))
1810 else if (in_cancel_job
== 2)
1812 else if (!strcasecmp(line
, "<Limit") && value
)
1813 cupsFilePrintf(temp
, " %s %s>\n", line
, value
);
1814 else if (line
[0] == '<')
1818 cupsFilePrintf(temp
, "%*s%s %s>\n", indent
, "", line
, value
);
1826 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1829 else if (!in_policy
&& !in_location
&&
1830 (val
= cupsGetOption(line
, num_settings
, settings
)) != NULL
)
1833 * Replace this directive's value with the new one...
1836 cupsd_num_settings
= cupsAddOption(line
, val
, cupsd_num_settings
,
1840 * Write the new value in its place, without indentation since we
1841 * only support setting root directives, not in sections...
1844 cupsFilePrintf(temp
, "%s %s\n", line
, val
);
1848 if (!in_policy
&& !in_location
)
1851 * Record the non-policy, non-location directives that we find
1852 * in the server settings, since we cache this info and record it
1853 * in _cupsAdminGetServerSettings()...
1856 cupsd_num_settings
= cupsAddOption(line
, value
, cupsd_num_settings
,
1860 cupsFilePrintf(temp
, "%*s%s %s\n", indent
, "", line
, value
);
1863 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1867 * Write any missing info...
1870 if (!wrote_browsing
&& (remote_printers
>= 0 || share_printers
>= 0))
1872 if (remote_printers
> 0 || share_printers
> 0)
1874 if (remote_printers
> 0 && share_printers
> 0)
1875 cupsFilePuts(temp
, "# Enable printer sharing and shared printers.\n");
1876 else if (remote_printers
> 0)
1877 cupsFilePuts(temp
, "# Show shared printers on the local network.\n");
1879 cupsFilePuts(temp
, "# Share local printers on the local network.\n");
1881 cupsFilePuts(temp
, "Browsing On\n");
1882 cupsFilePuts(temp
, "BrowseOrder allow,deny\n");
1884 if (remote_printers
> 0)
1885 cupsFilePuts(temp
, "BrowseAllow all\n");
1887 if (share_printers
> 0)
1888 cupsFilePuts(temp
, "BrowseAddress @LOCAL\n");
1892 cupsFilePuts(temp
, "# Disable printer sharing and shared printers.\n");
1893 cupsFilePuts(temp
, "Browsing Off\n");
1897 if (!wrote_loglevel
&& debug_logging
>= 0)
1901 cupsFilePuts(temp
, "# Show troubleshooting information in error_log.\n");
1902 cupsFilePuts(temp
, "LogLevel debug\n");
1906 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1907 cupsFilePuts(temp
, "LogLevel " CUPS_DEFAULT_LOG_LEVEL
"\n");
1911 if (!wrote_port_listen
&& (share_printers
>= 0 || remote_admin
>= 0))
1913 if (share_printers
> 0 || remote_admin
> 0)
1915 cupsFilePuts(temp
, "# Allow remote access\n");
1916 cupsFilePrintf(temp
, "Port %d\n", ippPort());
1921 "# Only listen for connections from the local machine.\n");
1922 cupsFilePrintf(temp
, "Listen localhost:%d\n", ippPort());
1925 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1926 if (!access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1927 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1928 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1931 if (!wrote_root_location
&& (remote_admin
>= 0 || share_printers
>= 0))
1933 if (remote_admin
> 0 && share_printers
> 0)
1935 "# Allow shared printing and remote administration...\n");
1936 else if (remote_admin
> 0)
1937 cupsFilePuts(temp
, "# Allow remote administration...\n");
1938 else if (share_printers
> 0)
1939 cupsFilePuts(temp
, "# Allow shared printing...\n");
1941 cupsFilePuts(temp
, "# Restrict access to the server...\n");
1943 cupsFilePuts(temp
, "<Location />\n"
1944 " Order allow,deny\n");
1946 if (remote_admin
> 0 || share_printers
> 0)
1947 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1949 cupsFilePuts(temp
, "</Location>\n");
1952 if (!wrote_admin_location
&& remote_admin
>= 0)
1955 cupsFilePuts(temp
, "# Allow remote administration...\n");
1957 cupsFilePuts(temp
, "# Restrict access to the admin pages...\n");
1959 cupsFilePuts(temp
, "<Location /admin>\n"
1960 " Order allow,deny\n");
1963 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1965 cupsFilePuts(temp
, "</Location>\n");
1968 if (!wrote_conf_location
&& remote_admin
>= 0)
1972 "# Allow remote access to the configuration files...\n");
1974 cupsFilePuts(temp
, "# Restrict access to the configuration files...\n");
1976 cupsFilePuts(temp
, "<Location /admin/conf>\n"
1977 " AuthType Default\n"
1978 " Require user @SYSTEM\n"
1979 " Order allow,deny\n");
1982 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1984 cupsFilePuts(temp
, "</Location>\n");
1987 if (!wrote_policy
&& user_cancel_any
>= 0)
1989 cupsFilePuts(temp
, "<Policy default>\n"
1990 " # Job-related operations must be done by the owner "
1991 "or an administrator...\n"
1992 " <Limit Send-Document Send-URI Hold-Job Release-Job "
1993 "Restart-Job Purge-Jobs Set-Job-Attributes "
1994 "Create-Job-Subscription Renew-Subscription "
1995 "Cancel-Subscription Get-Notifications Reprocess-Job "
1996 "Cancel-Current-Job Suspend-Current-Job Resume-Job "
1998 " Require user @OWNER @SYSTEM\n"
1999 " Order deny,allow\n"
2001 " # All administration operations require an "
2002 "administrator to authenticate...\n"
2003 " <Limit Pause-Printer Resume-Printer "
2004 "Set-Printer-Attributes Enable-Printer "
2005 "Disable-Printer Pause-Printer-After-Current-Job "
2006 "Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer "
2007 "Activate-Printer Restart-Printer Shutdown-Printer "
2008 "Startup-Printer Promote-Job Schedule-Job-After "
2009 "CUPS-Add-Printer CUPS-Delete-Printer "
2010 "CUPS-Add-Class CUPS-Delete-Class "
2011 "CUPS-Accept-Jobs CUPS-Reject-Jobs "
2012 "CUPS-Set-Default CUPS-Add-Device CUPS-Delete-Device>\n"
2013 " AuthType Default\n"
2014 " Require user @SYSTEM\n"
2015 " Order deny,allow\n"
2018 if (!user_cancel_any
)
2019 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
2021 " <Limit Cancel-Job>\n"
2022 " Order deny,allow\n"
2023 " Require user @OWNER "
2024 CUPS_DEFAULT_PRINTOPERATOR_AUTH
"\n"
2027 cupsFilePuts(temp
, " <Limit All>\n"
2028 " Order deny,allow\n"
2033 for (i
= num_settings
, setting
= settings
; i
> 0; i
--, setting
++)
2034 if (setting
->name
[0] != '_' &&
2035 !cupsGetOption(setting
->name
, cupsd_num_settings
, cupsd_settings
))
2038 * Add this directive to the list of directives we have written...
2041 cupsd_num_settings
= cupsAddOption(setting
->name
, setting
->value
,
2042 cupsd_num_settings
, &cupsd_settings
);
2045 * Write the new value, without indentation since we only support
2046 * setting root directives, not in sections...
2049 cupsFilePrintf(temp
, "%s %s\n", setting
->name
, setting
->value
);
2052 cupsFileClose(cupsd
);
2053 cupsFileClose(temp
);
2056 * Upload the configuration file to the server...
2059 status
= cupsPutFile(http
, "/admin/conf/cupsd.conf", tempfile
);
2061 if (status
== HTTP_CREATED
)
2064 * Updated OK, add the basic settings...
2067 if (debug_logging
>= 0)
2068 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
2069 debug_logging
? "1" : "0",
2070 cupsd_num_settings
, &cupsd_settings
);
2072 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
2073 old_debug_logging
? "1" : "0",
2074 cupsd_num_settings
, &cupsd_settings
);
2076 if (remote_admin
>= 0)
2077 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
2078 remote_admin
? "1" : "0",
2079 cupsd_num_settings
, &cupsd_settings
);
2081 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
2082 old_remote_admin
? "1" : "0",
2083 cupsd_num_settings
, &cupsd_settings
);
2085 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
2086 remote_any
? "1" : "0",
2087 cupsd_num_settings
, &cupsd_settings
);
2089 if (remote_printers
>= 0)
2090 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
2091 remote_printers
? "1" : "0",
2092 cupsd_num_settings
, &cupsd_settings
);
2094 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
2095 old_remote_printers
? "1" : "0",
2096 cupsd_num_settings
, &cupsd_settings
);
2098 if (share_printers
>= 0)
2099 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
2100 share_printers
? "1" : "0",
2101 cupsd_num_settings
, &cupsd_settings
);
2103 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
2104 old_share_printers
? "1" : "0",
2105 cupsd_num_settings
, &cupsd_settings
);
2107 if (user_cancel_any
>= 0)
2108 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
2109 user_cancel_any
? "1" : "0",
2110 cupsd_num_settings
, &cupsd_settings
);
2112 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
2113 old_user_cancel_any
? "1" : "0",
2114 cupsd_num_settings
, &cupsd_settings
);
2117 * Save the new values...
2120 invalidate_cupsd_cache(cg
);
2122 cg
->cupsd_num_settings
= cupsd_num_settings
;
2123 cg
->cupsd_settings
= cupsd_settings
;
2124 cg
->cupsd_update
= time(NULL
);
2126 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
2129 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
2132 * Remote our temp files and return...
2140 return (status
== HTTP_CREATED
);
2145 * 'do_samba_command()' - Do a SAMBA command.
2148 static int /* O - Status of command */
2149 do_samba_command(const char *command
, /* I - Command to run */
2150 const char *address
, /* I - Address for command */
2151 const char *subcmd
, /* I - Sub-command */
2152 const char *authfile
, /* I - Samba authentication file */
2153 FILE *logfile
) /* I - Optional log file */
2156 return (1); /* Always fail on Windows... */
2159 int status
; /* Status of command */
2160 int pid
; /* Process ID of child */
2164 _cupsLangPrintf(logfile
,
2165 _("Running command: %s %s -N -A %s -c \'%s\'\n"),
2166 command
, address
, authfile
, subcmd
);
2168 if ((pid
= fork()) == 0)
2171 * Child goes here, redirect stdin/out/err and execute the command...
2175 open("/dev/null", O_RDONLY
);
2180 dup(fileno(logfile
));
2182 open("/dev/null", O_WRONLY
);
2187 execlp(command
, command
, address
, "-N", "-A", authfile
, "-c", subcmd
,
2196 _cupsLangPrintf(logfile
, _("Unable to run \"%s\": %s\n"),
2197 command
, strerror(errno
));
2202 * Wait for the process to complete...
2205 while (wait(&status
) != pid
);
2209 _cupsLangPuts(logfile
, "\n");
2211 DEBUG_printf(("9do_samba_command: status=%d", status
));
2213 if (WIFEXITED(status
))
2214 return (WEXITSTATUS(status
));
2216 return (-WTERMSIG(status
));
2222 * 'get_cupsd_conf()' - Get the current cupsd.conf file.
2225 static http_status_t
/* O - Status of request */
2227 http_t
*http
, /* I - Connection to server */
2228 _cups_globals_t
*cg
, /* I - Global data */
2229 time_t last_update
, /* I - Last update time for file */
2230 char *name
, /* I - Filename buffer */
2231 int namesize
, /* I - Size of filename buffer */
2232 int *remote
) /* O - Remote file? */
2234 int fd
; /* Temporary file descriptor */
2236 struct stat info
; /* cupsd.conf file information */
2238 http_status_t status
; /* Status of getting cupsd.conf */
2239 char host
[HTTP_MAX_HOST
]; /* Hostname for connection */
2243 * See if we already have the data we need...
2246 httpGetHostname(http
, host
, sizeof(host
));
2248 if (strcasecmp(cg
->cupsd_hostname
, host
))
2249 invalidate_cupsd_cache(cg
);
2251 snprintf(name
, namesize
, "%s/cupsd.conf", cg
->cups_serverroot
);
2255 if (!strcasecmp(host
, "localhost") && !access(name
, R_OK
))
2258 * Read the local file rather than using HTTP...
2261 if (stat(name
, &info
))
2263 char message
[1024]; /* Message string */
2266 snprintf(message
, sizeof(message
),
2267 _cupsLangString(cupsLangDefault(), _("stat of %s failed: %s")),
2268 name
, strerror(errno
));
2269 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
2273 return (HTTP_SERVER_ERROR
);
2275 else if (last_update
&& info
.st_mtime
<= last_update
)
2276 status
= HTTP_NOT_MODIFIED
;
2284 * Read cupsd.conf via a HTTP GET request...
2287 if ((fd
= cupsTempFd(name
, namesize
)) < 0)
2291 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
, 0);
2293 invalidate_cupsd_cache(cg
);
2295 return (HTTP_SERVER_ERROR
);
2300 httpClearFields(http
);
2303 httpSetField(http
, HTTP_FIELD_IF_MODIFIED_SINCE
,
2304 httpGetDateString(last_update
));
2306 status
= cupsGetFd(http
, "/admin/conf/cupsd.conf", fd
);
2310 if (status
!= HTTP_OK
)
2322 * 'invalidate_cupsd_cache()' - Invalidate the cached cupsd.conf settings.
2326 invalidate_cupsd_cache(
2327 _cups_globals_t
*cg
) /* I - Global data */
2329 cupsFreeOptions(cg
->cupsd_num_settings
, cg
->cupsd_settings
);
2331 cg
->cupsd_hostname
[0] = '\0';
2332 cg
->cupsd_update
= 0;
2333 cg
->cupsd_num_settings
= 0;
2334 cg
->cupsd_settings
= NULL
;
2339 * 'write_option()' - Write a CUPS option to a PPD file.
2343 write_option(cups_file_t
*dstfp
, /* I - PPD file */
2344 int order
, /* I - Order dependency */
2345 const char *name
, /* I - Option name */
2346 const char *text
, /* I - Option text */
2347 const char *attrname
, /* I - Attribute name */
2348 ipp_attribute_t
*suppattr
, /* I - IPP -supported attribute */
2349 ipp_attribute_t
*defattr
, /* I - IPP -default attribute */
2350 int defval
, /* I - Default value number */
2351 int valcount
) /* I - Number of values */
2353 int i
; /* Looping var */
2356 cupsFilePrintf(dstfp
, "*JCLOpenUI *%s/%s: PickOne\n"
2357 "*OrderDependency: %d JCLSetup *%s\n",
2358 name
, text
, order
, name
);
2360 if (defattr
->value_tag
== IPP_TAG_INTEGER
)
2363 * Do numeric options with a range or list...
2366 cupsFilePrintf(dstfp
, "*Default%s: %d\n", name
,
2367 defattr
->values
[defval
].integer
);
2369 if (suppattr
->value_tag
== IPP_TAG_RANGE
)
2372 * List each number in the range...
2375 for (i
= suppattr
->values
[0].range
.lower
;
2376 i
<= suppattr
->values
[0].range
.upper
;
2379 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, i
);
2382 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n",
2384 else if (defval
== 0)
2385 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
, i
);
2386 else if (defval
< (valcount
- 1))
2387 cupsFilePrintf(dstfp
, ",%d\"\n", i
);
2389 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", i
);
2395 * List explicit numbers...
2398 for (i
= 0; i
< suppattr
->num_values
; i
++)
2400 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, suppattr
->values
[i
].integer
);
2403 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n", attrname
,
2404 suppattr
->values
[i
].integer
);
2405 else if (defval
== 0)
2406 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
,
2407 suppattr
->values
[i
].integer
);
2408 else if (defval
< (valcount
- 1))
2409 cupsFilePrintf(dstfp
, ",%d\"\n", suppattr
->values
[i
].integer
);
2411 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", suppattr
->values
[i
].integer
);
2418 * Do text options with a list...
2421 cupsFilePrintf(dstfp
, "*Default%s: %s\n", name
,
2422 defattr
->values
[defval
].string
.text
);
2424 for (i
= 0; i
< suppattr
->num_values
; i
++)
2426 cupsFilePrintf(dstfp
, "*%s %s: \"", name
,
2427 suppattr
->values
[i
].string
.text
);
2430 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\n\"\n*End\n", attrname
,
2431 suppattr
->values
[i
].string
.text
);
2432 else if (defval
== 0)
2433 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\"\n", attrname
,
2434 suppattr
->values
[i
].string
.text
);
2435 else if (defval
< (valcount
- 1))
2436 cupsFilePrintf(dstfp
, ",%s\"\n", suppattr
->values
[i
].string
.text
);
2438 cupsFilePrintf(dstfp
, ",%s\n\"\n*End\n",
2439 suppattr
->values
[i
].string
.text
);
2443 cupsFilePrintf(dstfp
, "*JCLCloseUI: *%s\n\n", name
);
2448 * End of "$Id: adminutil.c 7850 2008-08-20 00:07:25Z mike $".