2 * "$Id: adminutil.c 6930 2007-09-08 00:28:06Z mike $"
4 * Administration utility API definitions for the Common UNIX Printing
7 * Copyright 2007 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.
80 char * /* O - PPD file or NULL */
81 cupsAdminCreateWindowsPPD(
82 http_t
*http
, /* I - Connection to server */
83 const char *dest
, /* I - Printer or class */
84 char *buffer
, /* I - Filename buffer */
85 int bufsize
) /* I - Size of filename buffer */
87 const char *src
; /* Source PPD filename */
88 cups_file_t
*srcfp
, /* Source PPD file */
89 *dstfp
; /* Destination PPD file */
90 ipp_t
*request
, /* IPP request */
91 *response
; /* IPP response */
92 ipp_attribute_t
*suppattr
, /* IPP -supported attribute */
93 *defattr
; /* IPP -default attribute */
94 cups_lang_t
*language
; /* Current language */
95 char line
[256], /* Line from PPD file */
96 junk
[256], /* Extra junk to throw away */
97 *ptr
, /* Pointer into line */
98 uri
[1024], /* Printer URI */
99 option
[41], /* Option */
100 choice
[41]; /* Choice */
101 int jcloption
, /* In a JCL option? */
102 jclorder
, /* Next JCL order dependency */
103 linenum
; /* Current line number */
104 time_t curtime
; /* Current time */
105 struct tm
*curdate
; /* Current date */
106 static const char * const pattrs
[] = /* Printer attributes we want */
108 "job-hold-until-supported",
109 "job-hold-until-default",
110 "job-sheets-supported",
111 "job-sheets-default",
112 "job-priority-supported",
113 "job-priority-default"
118 * Range check the input...
124 if (!http
|| !dest
|| !buffer
|| bufsize
< 2)
128 * Get the PPD file...
131 if ((src
= cupsGetPPD2(http
, dest
)) == NULL
)
135 * Get the supported banner pages, etc. for the printer...
138 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
140 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
141 "localhost", 0, "/printers/%s", dest
);
142 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
143 "printer-uri", NULL
, uri
);
145 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
146 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
150 * Do the request and get back a response...
153 response
= cupsDoRequest(http
, request
, "/");
154 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
161 * Open the original PPD file...
164 if ((srcfp
= cupsFileOpen(src
, "rb")) == NULL
)
168 * Create a temporary output file using the destination buffer...
171 if ((dstfp
= cupsTempFile2(buffer
, bufsize
)) == NULL
)
173 cupsFileClose(srcfp
);
181 * Write a new header explaining that this isn't the original PPD...
184 cupsFilePuts(dstfp
, "*PPD-Adobe: \"4.3\"\n");
186 curtime
= time(NULL
);
187 curdate
= gmtime(&curtime
);
189 cupsFilePrintf(dstfp
, "*%% Modified on %04d%02d%02d%02d%02d%02d+0000 "
190 "for CUPS Windows Driver\n",
191 curdate
->tm_year
+ 1900, curdate
->tm_mon
+ 1, curdate
->tm_mday
,
192 curdate
->tm_hour
, curdate
->tm_min
, curdate
->tm_sec
);
195 * Read the existing PPD file, converting all PJL commands to CUPS
196 * job ticket comments...
202 language
= cupsLangDefault();
204 while (cupsFileGets(srcfp
, line
, sizeof(line
)))
208 if (!strncmp(line
, "*PPD-Adobe:", 11))
211 * Already wrote the PPD header...
216 else if (!strncmp(line
, "*JCLBegin:", 10) ||
217 !strncmp(line
, "*JCLToPSInterpreter:", 20) ||
218 !strncmp(line
, "*JCLEnd:", 8) ||
219 !strncmp(line
, "*Protocols:", 11))
222 * Don't use existing JCL keywords; we'll create our own, below...
225 cupsFilePrintf(dstfp
, "*%% Commented out for CUPS Windows Driver...\n"
226 "*%%%s\n", line
+ 1);
229 else if (!strncmp(line
, "*JCLOpenUI", 10))
232 cupsFilePrintf(dstfp
, "%s\n", line
);
234 else if (!strncmp(line
, "*JCLCloseUI", 11))
237 cupsFilePrintf(dstfp
, "%s\n", line
);
239 else if (jcloption
&& !strncmp(line
, "*OrderDependency:", 17))
241 for (ptr
= line
+ 17; *ptr
&& isspace(*ptr
& 255); ptr
++);
243 ptr
= strchr(ptr
, ' ');
247 cupsFilePrintf(dstfp
, "*OrderDependency: %d%s\n", jclorder
, ptr
);
251 cupsFilePrintf(dstfp
, "%s\n", line
);
253 else if (jcloption
&&
254 strncmp(line
, "*End", 4) &&
255 strncmp(line
, "*Default", 8))
257 if ((ptr
= strchr(line
, ':')) == NULL
)
259 snprintf(line
, sizeof(line
),
260 _cupsLangString(language
, _("Missing value on line %d!")),
262 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
264 cupsFileClose(srcfp
);
265 cupsFileClose(dstfp
);
275 if ((ptr
= strchr(ptr
, '\"')) == NULL
)
277 snprintf(line
, sizeof(line
),
278 _cupsLangString(language
,
279 _("Missing double quote on line %d!")),
281 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
283 cupsFileClose(srcfp
);
284 cupsFileClose(dstfp
);
294 if (sscanf(line
, "*%40s%*[ \t]%40[^/]", option
, choice
) != 2)
296 snprintf(line
, sizeof(line
),
297 _cupsLangString(language
,
298 _("Bad option + choice on line %d!")),
300 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
302 cupsFileClose(srcfp
);
303 cupsFileClose(dstfp
);
313 if (strchr(ptr
+ 1, '\"') == NULL
)
319 while (cupsFileGets(srcfp
, junk
, sizeof(junk
)) != NULL
)
323 if (!strncmp(junk
, "*End", 4))
328 snprintf(ptr
+ 1, sizeof(line
) - (ptr
- line
+ 1),
329 "%%cupsJobTicket: %s=%s\n\"\n*End", option
, choice
);
331 cupsFilePrintf(dstfp
, "*%% Changed for CUPS Windows Driver...\n%s\n",
335 cupsFilePrintf(dstfp
, "%s\n", line
);
338 cupsFileClose(srcfp
);
343 snprintf(line
, sizeof(line
),
344 _cupsLangString(language
, _("Empty PPD file!")),
346 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
348 cupsFileClose(dstfp
);
357 * Now add the CUPS-specific attributes and options...
360 cupsFilePuts(dstfp
, "\n*% CUPS Job Ticket support and options...\n");
361 cupsFilePuts(dstfp
, "*Protocols: PJL\n");
362 cupsFilePuts(dstfp
, "*JCLBegin: \"%!PS-Adobe-3.0<0A>\"\n");
363 cupsFilePuts(dstfp
, "*JCLToPSInterpreter: \"\"\n");
364 cupsFilePuts(dstfp
, "*JCLEnd: \"\"\n");
366 cupsFilePuts(dstfp
, "\n*OpenGroup: CUPS/CUPS Options\n\n");
368 if ((defattr
= ippFindAttribute(response
, "job-hold-until-default",
369 IPP_TAG_ZERO
)) != NULL
&&
370 (suppattr
= ippFindAttribute(response
, "job-hold-until-supported",
371 IPP_TAG_ZERO
)) != NULL
)
372 write_option(dstfp
, jclorder
++, "cupsJobHoldUntil", "Hold Until",
373 "job-hold-until", suppattr
, defattr
, 0, 1);
375 if ((defattr
= ippFindAttribute(response
, "job-priority-default",
376 IPP_TAG_INTEGER
)) != NULL
&&
377 (suppattr
= ippFindAttribute(response
, "job-priority-supported",
378 IPP_TAG_RANGE
)) != NULL
)
379 write_option(dstfp
, jclorder
++, "cupsJobPriority", "Priority",
380 "job-priority", suppattr
, defattr
, 0, 1);
382 if ((defattr
= ippFindAttribute(response
, "job-sheets-default",
383 IPP_TAG_ZERO
)) != NULL
&&
384 (suppattr
= ippFindAttribute(response
, "job-sheets-supported",
385 IPP_TAG_ZERO
)) != NULL
)
387 write_option(dstfp
, jclorder
++, "cupsJobSheetsStart", "Start Banner",
388 "job-sheets", suppattr
, defattr
, 0, 2);
389 write_option(dstfp
, jclorder
++, "cupsJobSheetsEnd", "End Banner",
390 "job-sheets", suppattr
, defattr
, 1, 2);
393 cupsFilePuts(dstfp
, "*CloseGroup: CUPS\n");
394 cupsFileClose(dstfp
);
403 * 'cupsAdminExportSamba()' - Export a printer to Samba.
406 int /* O - 1 on success, 0 on failure */
407 cupsAdminExportSamba(
408 const char *dest
, /* I - Destination to export */
409 const char *ppd
, /* I - PPD file */
410 const char *samba_server
, /* I - Samba server */
411 const char *samba_user
, /* I - Samba username */
412 const char *samba_password
, /* I - Samba password */
413 FILE *logfile
) /* I - Log file, if any */
415 int status
; /* Status of Samba commands */
416 int have_drivers
; /* Have drivers? */
417 char file
[1024], /* File to test for */
418 authfile
[1024], /* Temporary authentication file */
419 address
[1024], /* Address for command */
420 subcmd
[1024], /* Sub-command */
421 message
[1024]; /* Error message */
422 cups_file_t
*fp
; /* Authentication file */
423 cups_lang_t
*language
; /* Current language */
424 _cups_globals_t
*cg
= _cupsGlobals();
429 * Range check input...
432 if (!dest
|| !ppd
|| !samba_server
|| !samba_user
|| !samba_password
)
434 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
439 * Create a temporary authentication file for Samba...
442 if ((fp
= cupsTempFile2(authfile
, sizeof(authfile
))) == NULL
)
444 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
448 cupsFilePrintf(fp
, "username = %s\n", samba_user
);
449 cupsFilePrintf(fp
, "password = %s\n", samba_password
);
453 * See which drivers are available; the new CUPS v6 and Adobe drivers
454 * depend on the Windows 2k PS driver, so copy that driver first:
465 language
= cupsLangDefault();
467 snprintf(file
, sizeof(file
), "%s/drivers/pscript5.dll", cg
->cups_datadir
);
468 if (!access(file
, 0))
473 * Windows 2k driver is installed; do the smbclient commands needed
474 * to copy the Win2k drivers over...
477 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
479 snprintf(subcmd
, sizeof(subcmd
),
481 "put %s W32X86/%s.ppd;"
482 "put %s/drivers/ps5ui.dll W32X86/ps5ui.dll;"
483 "put %s/drivers/pscript.hlp W32X86/pscript.hlp;"
484 "put %s/drivers/pscript.ntf W32X86/pscript.ntf;"
485 "put %s/drivers/pscript5.dll W32X86/pscript5.dll",
486 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
487 cg
->cups_datadir
, cg
->cups_datadir
);
489 if ((status
= do_samba_command("smbclient", address
, subcmd
,
490 authfile
, logfile
)) != 0)
492 snprintf(message
, sizeof(message
),
493 _cupsLangString(language
,
494 _("Unable to copy Windows 2000 printer "
495 "driver files (%d)!")), status
);
497 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
500 _cupsLangPrintf(logfile
, "%s\n", message
);
508 * See if we also have the CUPS driver files; if so, use them!
511 snprintf(file
, sizeof(file
), "%s/drivers/cupsps6.dll", cg
->cups_datadir
);
512 if (!access(file
, 0))
515 * Copy the CUPS driver files over...
518 snprintf(subcmd
, sizeof(subcmd
),
519 "put %s/drivers/cups6.ini W32X86/cups6.ini;"
520 "put %s/drivers/cupsps6.dll W32X86/cupsps6.dll;"
521 "put %s/drivers/cupsui6.dll W32X86/cupsui6.dll",
522 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
524 if ((status
= do_samba_command("smbclient", address
, subcmd
,
525 authfile
, logfile
)) != 0)
527 snprintf(message
, sizeof(message
),
528 _cupsLangString(language
,
529 _("Unable to copy CUPS printer driver "
530 "files (%d)!")), status
);
532 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
535 _cupsLangPrintf(logfile
, "%s\n", message
);
543 * Do the rpcclient command needed for the CUPS drivers...
546 snprintf(subcmd
, sizeof(subcmd
),
547 "adddriver \"Windows NT x86\" \"%s:"
548 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
549 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
550 "cups6.ini,cupsps6.dll,cupsui6.dll\"",
556 * Don't have the CUPS drivers, so just use the standard Windows
560 snprintf(subcmd
, sizeof(subcmd
),
561 "adddriver \"Windows NT x86\" \"%s:"
562 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
563 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
567 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
568 authfile
, logfile
)) != 0)
570 snprintf(message
, sizeof(message
),
571 _cupsLangString(language
,
572 _("Unable to install Windows 2000 printer "
573 "driver files (%d)!")), status
);
575 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
578 _cupsLangPrintf(logfile
, "%s\n", message
);
587 * See if we have the Win9x PS driver...
590 snprintf(file
, sizeof(file
), "%s/drivers/ADOBEPS4.DRV", cg
->cups_datadir
);
591 if (!access(file
, 0))
596 * Do the smbclient commands needed for the Adobe Win9x drivers...
599 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
601 snprintf(subcmd
, sizeof(subcmd
),
603 "put %s WIN40/%s.PPD;"
604 "put %s/drivers/ADFONTS.MFM WIN40/ADFONTS.MFM;"
605 "put %s/drivers/ADOBEPS4.DRV WIN40/ADOBEPS4.DRV;"
606 "put %s/drivers/ADOBEPS4.HLP WIN40/ADOBEPS4.HLP;"
607 "put %s/drivers/ICONLIB.DLL WIN40/ICONLIB.DLL;"
608 "put %s/drivers/PSMON.DLL WIN40/PSMON.DLL;",
609 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
610 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
612 if ((status
= do_samba_command("smbclient", address
, subcmd
,
613 authfile
, logfile
)) != 0)
615 snprintf(message
, sizeof(message
),
616 _cupsLangString(language
,
617 _("Unable to copy Windows 9x printer "
618 "driver files (%d)!")), status
);
620 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
623 _cupsLangPrintf(logfile
, "%s\n", message
);
631 * Do the rpcclient commands needed for the Adobe Win9x drivers...
634 snprintf(subcmd
, sizeof(subcmd
),
635 "adddriver \"Windows 4.0\" \"%s:ADOBEPS4.DRV:%s.PPD:NULL:"
636 "ADOBEPS4.HLP:PSMON.DLL:RAW:"
637 "ADOBEPS4.DRV,%s.PPD,ADOBEPS4.HLP,PSMON.DLL,ADFONTS.MFM,"
641 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
642 authfile
, logfile
)) != 0)
644 snprintf(message
, sizeof(message
),
645 _cupsLangString(language
,
646 _("Unable to install Windows 9x printer "
647 "driver files (%d)!")), status
);
649 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
652 _cupsLangPrintf(logfile
, "%s\n", message
);
661 * See if we have the 64-bit Windows PS driver...
671 snprintf(file
, sizeof(file
), "%s/drivers/x64/pscript5.dll", cg
->cups_datadir
);
672 if (!access(file
, 0))
677 * 64-bit Windows driver is installed; do the smbclient commands needed
678 * to copy the Win64 drivers over...
681 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
683 snprintf(subcmd
, sizeof(subcmd
),
686 "put %s/drivers/x64/ps5ui.dll x64/ps5ui.dll;"
687 "put %s/drivers/x64/pscript.hlp x64/pscript.hlp;"
688 "put %s/drivers/x64/pscript.ntf x64/pscript.ntf;"
689 "put %s/drivers/x64/pscript5.dll x64/pscript5.dll",
690 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
691 cg
->cups_datadir
, cg
->cups_datadir
);
693 if ((status
= do_samba_command("smbclient", address
, subcmd
,
694 authfile
, logfile
)) != 0)
696 snprintf(message
, sizeof(message
),
697 _cupsLangString(language
,
698 _("Unable to copy 64-bit Windows printer "
699 "driver files (%d)!")), status
);
701 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
704 _cupsLangPrintf(logfile
, "%s\n", message
);
712 * See if we also have the CUPS driver files; if so, use them!
715 snprintf(file
, sizeof(file
), "%s/drivers/x64/cupsps6.dll", cg
->cups_datadir
);
716 if (!access(file
, 0))
719 * Copy the CUPS driver files over...
722 snprintf(subcmd
, sizeof(subcmd
),
723 "put %s/drivers/x64/cups6.ini x64/cups6.ini;"
724 "put %s/drivers/x64/cupsps6.dll x64/cupsps6.dll;"
725 "put %s/drivers/x64/cupsui6.dll x64/cupsui6.dll",
726 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
728 if ((status
= do_samba_command("smbclient", address
, subcmd
,
729 authfile
, logfile
)) != 0)
731 snprintf(message
, sizeof(message
),
732 _cupsLangString(language
,
733 _("Unable to copy 64-bit CUPS printer driver "
734 "files (%d)!")), status
);
736 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
739 _cupsLangPrintf(logfile
, "%s\n", message
);
747 * Do the rpcclient command needed for the CUPS drivers...
750 snprintf(subcmd
, sizeof(subcmd
),
751 "adddriver \"Windows x64\" \"%s:"
752 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
753 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
754 "cups6.ini,cupsps6.dll,cupsui6.dll\"",
760 * Don't have the CUPS drivers, so just use the standard Windows
764 snprintf(subcmd
, sizeof(subcmd
),
765 "adddriver \"Windows x64\" \"%s:"
766 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
767 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
771 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
772 authfile
, logfile
)) != 0)
774 snprintf(message
, sizeof(message
),
775 _cupsLangString(language
,
776 _("Unable to install Windows 2000 printer "
777 "driver files (%d)!")), status
);
779 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
782 _cupsLangPrintf(logfile
, "%s\n", message
);
790 if (logfile
&& !(have_drivers
& 1))
794 _cupsLangString(language
,
795 _("No Windows printer drivers are installed!")),
799 _cupsLangString(language
,
800 _("Warning, no Windows 2000 printer drivers "
804 _cupsSetError(IPP_NOT_FOUND
, message
);
805 _cupsLangPrintf(logfile
, "%s\n", message
);
808 if (have_drivers
== 0)
810 _cupsSetError(IPP_NOT_FOUND
, message
);
815 * Finally, associate the drivers we just added with the queue...
818 snprintf(subcmd
, sizeof(subcmd
), "setdriver %s %s", dest
, dest
);
820 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
821 authfile
, logfile
)) != 0)
823 snprintf(message
, sizeof(message
),
824 _cupsLangString(language
,
825 _("Unable to set Windows printer driver (%d)!")),
828 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
831 _cupsLangPrintf(logfile
, "%s\n", message
);
845 * 'cupsAdminGetServerSettings()' - Get settings from the server.
847 * The returned settings should be freed with cupsFreeOptions() when
848 * you are done with them.
853 int /* O - 1 on success, 0 on failure */
854 cupsAdminGetServerSettings(
855 http_t
*http
, /* I - Connection to server */
856 int *num_settings
, /* O - Number of settings */
857 cups_option_t
**settings
) /* O - Settings */
859 return (_cupsAdminGetServerSettings(http
, num_settings
, settings
));
864 * '_cupsAdminGetServerSettings()' - Get settings from the server.
866 * The returned settings should be freed with cupsFreeOptions() when
867 * you are done with them.
872 int /* O - 1 on success, 0 on failure */
873 _cupsAdminGetServerSettings(
874 http_t
*http
, /* I - Connection to server */
875 int *num_settings
, /* O - Number of settings */
876 cups_option_t
**settings
) /* O - Settings */
878 int i
; /* Looping var */
879 cups_file_t
*cupsd
; /* cupsd.conf file */
880 char cupsdconf
[1024]; /* cupsd.conf filename */
881 int remote
; /* Remote cupsd.conf file? */
882 http_status_t status
; /* Status of getting cupsd.conf */
883 char line
[1024], /* Line from cupsd.conf file */
884 *value
; /* Value on line */
885 cups_option_t
*setting
; /* Current setting */
886 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
890 * Range check input...
893 if (!http
|| !num_settings
|| !settings
)
895 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
910 * Get the cupsd.conf file...
913 if ((status
= get_cupsd_conf(http
, cg
, cg
->cupsd_update
, cupsdconf
,
914 sizeof(cupsdconf
), &remote
)) == HTTP_OK
)
916 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
918 char message
[1024]; /* Message string */
921 snprintf(message
, sizeof(message
),
922 _cupsLangString(cupsLangDefault(), _("open of %s failed: %s")),
923 cupsdconf
, strerror(errno
));
924 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
933 * Read the file, keeping track of what settings are enabled...
936 int remote_access
= 0, /* Remote access allowed? */
937 remote_admin
= 0, /* Remote administration allowed? */
938 remote_any
= 0, /* Remote access from anywhere allowed? */
939 browsing
= 1, /* Browsing enabled? */
940 browse_allow
= 1, /* Browse address set? */
941 browse_address
= 0, /* Browse address set? */
942 cancel_policy
= 1, /* Cancel-job policy set? */
943 debug_logging
= 0; /* LogLevel debug set? */
944 int linenum
= 0, /* Line number in file */
945 in_location
= 0, /* In a location section? */
946 in_policy
= 0, /* In a policy section? */
947 in_cancel_job
= 0, /* In a cancel-job section? */
948 in_admin_location
= 0; /* In the /admin location? */
951 invalidate_cupsd_cache(cg
);
953 cg
->cupsd_update
= time(NULL
);
954 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
956 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
958 if (!value
&& strncmp(line
, "</", 2))
961 if (!strcasecmp(line
, "Port") || !strcasecmp(line
, "Listen"))
963 char *port
; /* Pointer to port number, if any */
966 if ((port
= strrchr(value
, ':')) != NULL
)
968 else if (isdigit(*value
& 255))
971 * Listen on a port number implies remote access...
978 if (strcasecmp(value
, "localhost") && strcmp(value
, "127.0.0.1")
981 #endif /* AF_LOCAL */
983 && strcmp(value
, "::1")
984 #endif /* AF_INET6 */
988 else if (!strcasecmp(line
, "Browsing"))
990 browsing
= !strcasecmp(value
, "yes") || !strcasecmp(value
, "on") ||
991 !strcasecmp(value
, "true");
993 else if (!strcasecmp(line
, "BrowseAddress"))
997 else if (!strcasecmp(line
, "BrowseAllow"))
1001 else if (!strcasecmp(line
, "BrowseOrder"))
1003 browse_allow
= !strncasecmp(value
, "deny,", 5);
1005 else if (!strcasecmp(line
, "LogLevel"))
1007 debug_logging
= !strncasecmp(value
, "debug", 5);
1009 else if (!strcasecmp(line
, "<Policy") && !strcasecmp(value
, "default"))
1013 else if (!strcasecmp(line
, "</Policy>"))
1017 else if (!strcasecmp(line
, "<Limit") && in_policy
)
1020 * See if the policy limit is for the Cancel-Job operation...
1023 char *valptr
; /* Pointer into value */
1028 for (valptr
= value
; !isspace(*valptr
& 255) && *valptr
; valptr
++);
1033 if (!strcasecmp(value
, "cancel-job") || !strcasecmp(value
, "all"))
1039 for (value
= valptr
; isspace(*value
& 255); value
++);
1042 else if (!strcasecmp(line
, "</Limit>"))
1046 else if (!strcasecmp(line
, "Require") && in_cancel_job
)
1050 else if (!strcasecmp(line
, "<Location"))
1052 in_admin_location
= !strcasecmp(value
, "/admin");
1055 else if (!strcasecmp(line
, "</Location>"))
1057 in_admin_location
= 0;
1060 else if (!strcasecmp(line
, "Allow") && in_admin_location
&&
1061 strcasecmp(value
, "localhost") && strcasecmp(value
, "127.0.0.1")
1064 #endif /* AF_LOCAL */
1066 && strcmp(value
, "::1")
1067 #endif /* AF_INET6 */
1072 if (!strcasecmp(value
, "all"))
1075 else if (line
[0] != '<' && !in_location
&& !in_policy
)
1076 cg
->cupsd_num_settings
= cupsAddOption(line
, value
,
1077 cg
->cupsd_num_settings
,
1078 &(cg
->cupsd_settings
));
1081 cupsFileClose(cupsd
);
1083 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1084 debug_logging
? "1" : "0",
1085 cg
->cupsd_num_settings
,
1086 &(cg
->cupsd_settings
));
1088 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1089 (remote_access
&& remote_admin
) ?
1091 cg
->cupsd_num_settings
,
1092 &(cg
->cupsd_settings
));
1094 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
1095 remote_any
? "1" : "0",
1096 cg
->cupsd_num_settings
,
1097 &(cg
->cupsd_settings
));
1099 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
1100 (browsing
&& browse_allow
) ?
1102 cg
->cupsd_num_settings
,
1103 &(cg
->cupsd_settings
));
1105 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1106 (remote_access
&& browsing
&&
1107 browse_address
) ? "1" : "0",
1108 cg
->cupsd_num_settings
,
1109 &(cg
->cupsd_settings
));
1111 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1112 cancel_policy
? "1" : "0",
1113 cg
->cupsd_num_settings
,
1114 &(cg
->cupsd_settings
));
1116 else if (status
!= HTTP_NOT_MODIFIED
)
1117 invalidate_cupsd_cache(cg
);
1120 * Remove any temporary files and copy the settings array...
1126 for (i
= cg
->cupsd_num_settings
, setting
= cg
->cupsd_settings
;
1129 *num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1130 *num_settings
, settings
);
1132 return (cg
->cupsd_num_settings
> 0);
1137 * 'cupsAdminSetServerSettings()' - Set settings on the server.
1142 int /* O - 1 on success, 0 on failure */
1143 cupsAdminSetServerSettings(
1144 http_t
*http
, /* I - Connection to server */
1145 int num_settings
, /* I - Number of settings */
1146 cups_option_t
*settings
) /* I - Settings */
1148 return (_cupsAdminSetServerSettings(http
, num_settings
, settings
));
1153 * '_cupsAdminSetServerSettings()' - Set settings on the server.
1158 int /* O - 1 on success, 0 on failure */
1159 _cupsAdminSetServerSettings(
1160 http_t
*http
, /* I - Connection to server */
1161 int num_settings
, /* I - Number of settings */
1162 cups_option_t
*settings
) /* I - Settings */
1164 int i
; /* Looping var */
1165 http_status_t status
; /* GET/PUT status */
1166 const char *server_port_env
; /* SERVER_PORT env var */
1167 int server_port
; /* IPP port for server */
1168 cups_file_t
*cupsd
; /* cupsd.conf file */
1169 char cupsdconf
[1024]; /* cupsd.conf filename */
1170 int remote
; /* Remote cupsd.conf file? */
1171 char tempfile
[1024]; /* Temporary new cupsd.conf */
1172 cups_file_t
*temp
; /* Temporary file */
1173 char line
[1024], /* Line from cupsd.conf file */
1174 *value
; /* Value on line */
1175 int linenum
, /* Line number in file */
1176 in_location
, /* In a location section? */
1177 in_policy
, /* In a policy section? */
1178 in_default_policy
, /* In the default policy section? */
1179 in_cancel_job
, /* In a cancel-job section? */
1180 in_admin_location
, /* In the /admin location? */
1181 in_conf_location
, /* In the /admin/conf location? */
1182 in_root_location
; /* In the / location? */
1183 const char *val
; /* Setting value */
1184 int remote_printers
, /* Show remote printers */
1185 share_printers
, /* Share local printers */
1186 remote_admin
, /* Remote administration allowed? */
1187 remote_any
, /* Remote access from anywhere? */
1188 user_cancel_any
, /* Cancel-job policy set? */
1189 debug_logging
; /* LogLevel debug set? */
1190 int wrote_port_listen
, /* Wrote the port/listen lines? */
1191 wrote_browsing
, /* Wrote the browsing lines? */
1192 wrote_policy
, /* Wrote the policy? */
1193 wrote_loglevel
, /* Wrote the LogLevel line? */
1194 wrote_admin_location
, /* Wrote the /admin location? */
1195 wrote_conf_location
, /* Wrote the /admin/conf location? */
1196 wrote_root_location
; /* Wrote the / location? */
1197 int indent
; /* Indentation */
1198 int cupsd_num_settings
; /* New number of settings */
1199 int old_remote_printers
, /* Show remote printers */
1200 old_share_printers
, /* Share local printers */
1201 old_remote_admin
, /* Remote administration allowed? */
1202 old_user_cancel_any
, /* Cancel-job policy set? */
1203 old_debug_logging
; /* LogLevel debug set? */
1204 cups_option_t
*cupsd_settings
, /* New settings */
1205 *setting
; /* Current setting */
1206 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1210 * Range check input...
1213 if (!http
|| !num_settings
|| !settings
)
1215 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
1221 * Get the cupsd.conf file...
1224 if ((status
= get_cupsd_conf(http
, cg
, 0, cupsdconf
, sizeof(cupsdconf
),
1225 &remote
)) == HTTP_OK
)
1227 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
1229 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
1237 * Get current settings...
1240 if (!_cupsAdminGetServerSettings(http
, &cupsd_num_settings
,
1244 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, cupsd_num_settings
,
1245 cupsd_settings
)) != NULL
)
1246 old_debug_logging
= atoi(val
);
1248 old_debug_logging
= 0;
1250 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, cupsd_num_settings
,
1251 cupsd_settings
)) != NULL
)
1252 old_remote_admin
= atoi(val
);
1254 old_remote_admin
= 0;
1256 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, cupsd_num_settings
,
1257 cupsd_settings
)) != NULL
)
1258 remote_any
= atoi(val
);
1262 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
, cupsd_num_settings
,
1263 cupsd_settings
)) != NULL
)
1264 old_remote_printers
= atoi(val
);
1266 old_remote_printers
= 1;
1268 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, cupsd_num_settings
,
1269 cupsd_settings
)) != NULL
)
1270 old_share_printers
= atoi(val
);
1272 old_share_printers
= 0;
1274 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, cupsd_num_settings
,
1275 cupsd_settings
)) != NULL
)
1276 old_user_cancel_any
= atoi(val
);
1278 old_user_cancel_any
= 0;
1280 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
1283 * Get basic settings...
1286 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, num_settings
,
1289 debug_logging
= atoi(val
);
1291 if (debug_logging
== old_debug_logging
)
1294 * No change to this setting...
1303 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, num_settings
,
1305 remote_any
= atoi(val
);
1307 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, num_settings
,
1310 remote_admin
= atoi(val
);
1312 if (remote_admin
== old_remote_admin
&& remote_any
< 0)
1315 * No change to this setting...
1324 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
, num_settings
,
1327 remote_printers
= atoi(val
);
1329 if (remote_printers
== old_remote_printers
)
1332 * No change to this setting...
1335 remote_printers
= -1;
1339 remote_printers
= -1;
1341 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, num_settings
,
1344 share_printers
= atoi(val
);
1346 if (share_printers
== old_share_printers
&& remote_any
< 0)
1349 * No change to this setting...
1352 share_printers
= -1;
1356 share_printers
= -1;
1358 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, num_settings
,
1361 user_cancel_any
= atoi(val
);
1363 if (user_cancel_any
== old_user_cancel_any
)
1366 * No change to this setting...
1369 user_cancel_any
= -1;
1373 user_cancel_any
= -1;
1376 * Create a temporary file for the new cupsd.conf file...
1379 if ((temp
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1381 cupsFileClose(cupsd
);
1386 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
1391 * Copy the old file to the new, making changes along the way...
1394 cupsd_num_settings
= 0;
1395 in_admin_location
= 0;
1397 in_conf_location
= 0;
1398 in_default_policy
= 0;
1401 in_root_location
= 0;
1403 wrote_admin_location
= 0;
1405 wrote_conf_location
= 0;
1408 wrote_port_listen
= 0;
1409 wrote_root_location
= 0;
1412 if ((server_port_env
= getenv("SERVER_PORT")) != NULL
)
1414 if ((server_port
= atoi(server_port_env
)) <= 0)
1415 server_port
= ippPort();
1418 server_port
= ippPort();
1420 if (server_port
<= 0)
1421 server_port
= IPP_PORT
;
1423 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
1425 if ((!strcasecmp(line
, "Port") || !strcasecmp(line
, "Listen")) &&
1426 (share_printers
>= 0 || remote_admin
>= 0))
1428 if (!wrote_port_listen
)
1430 wrote_port_listen
= 1;
1432 if (share_printers
> 0 || remote_admin
> 0)
1434 cupsFilePuts(temp
, "# Allow remote access\n");
1435 cupsFilePrintf(temp
, "Port %d\n", server_port
);
1439 cupsFilePuts(temp
, "# Only listen for connections from the local "
1441 cupsFilePrintf(temp
, "Listen localhost:%d\n", server_port
);
1444 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1445 if ((!value
|| strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)) &&
1446 !access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1447 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1448 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1450 else if (value
&& value
[0] == '/'
1451 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1452 && strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)
1453 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1455 cupsFilePrintf(temp
, "Listen %s\n", value
);
1457 else if ((!strcasecmp(line
, "Browsing") ||
1458 !strcasecmp(line
, "BrowseAddress") ||
1459 !strcasecmp(line
, "BrowseAllow") ||
1460 !strcasecmp(line
, "BrowseDeny") ||
1461 !strcasecmp(line
, "BrowseLocalProtocols") ||
1462 !strcasecmp(line
, "BrowseRemoteProtocols") ||
1463 !strcasecmp(line
, "BrowseOrder")) &&
1464 (remote_printers
>= 0 || share_printers
>= 0))
1466 if (!wrote_browsing
)
1468 int new_remote_printers
= (remote_printers
> 0 ||
1469 (remote_printers
== -1 &&
1470 old_remote_printers
> 0));
1471 int new_share_printers
= (share_printers
> 0 ||
1472 (share_printers
== -1 &&
1473 old_share_printers
> 0));
1477 if (new_remote_printers
|| new_share_printers
)
1479 const char *localp
= cupsGetOption("BrowseLocalProtocols",
1480 num_settings
, settings
);
1481 const char *remotep
= cupsGetOption("BrowseRemoteProtocols",
1482 num_settings
, settings
);
1484 if (new_remote_printers
&& new_share_printers
)
1486 "# Enable printer sharing and shared printers.\n");
1487 else if (new_remote_printers
)
1489 "# Show shared printers on the local network.\n");
1492 "# Share local printers on the local network.\n");
1494 cupsFilePuts(temp
, "Browsing On\n");
1495 cupsFilePuts(temp
, "BrowseOrder allow,deny\n");
1497 if (new_remote_printers
)
1499 cupsFilePuts(temp
, "BrowseAllow all\n");
1501 if (!remotep
|| !*remotep
)
1502 cupsFilePuts(temp
, "BrowseRemoteProtocols "
1503 CUPS_DEFAULT_BROWSE_REMOTE_PROTOCOLS
"\n");
1505 cupsFilePrintf(temp
, "BrowseRemoteProtocols %s\n", remotep
);
1508 cupsFilePuts(temp
, "BrowseRemoteProtocols\n");
1510 if (new_share_printers
)
1512 cupsFilePuts(temp
, "BrowseAddress @LOCAL\n");
1514 if (!localp
|| !*localp
)
1515 cupsFilePuts(temp
, "BrowseLocalProtocols "
1516 CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS
"\n");
1518 cupsFilePrintf(temp
, "BrowseLocalProtocols %s\n", localp
);
1521 cupsFilePuts(temp
, "BrowseLocalProtocols\n");
1526 "# Disable printer sharing and shared printers.\n");
1527 cupsFilePuts(temp
, "Browsing Off\n");
1531 else if (!strcasecmp(line
, "LogLevel") && debug_logging
>= 0)
1538 "# Show troubleshooting information in error_log.\n");
1539 cupsFilePuts(temp
, "LogLevel debug\n");
1543 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1544 cupsFilePuts(temp
, "LogLevel info\n");
1547 else if (!strcasecmp(line
, "<Policy"))
1549 in_default_policy
= !strcasecmp(value
, "default");
1552 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1555 else if (!strcasecmp(line
, "</Policy>"))
1558 if (!wrote_policy
&& in_default_policy
)
1562 if (!user_cancel_any
)
1563 cupsFilePuts(temp
, " # Only the owner or an administrator can "
1565 " <Limit Cancel-Job>\n"
1566 " Order deny,allow\n"
1567 " Require user @OWNER "
1568 CUPS_DEFAULT_PRINTADMIN_AUTH
"\n"
1573 in_default_policy
= 0;
1575 cupsFilePuts(temp
, "</Policy>\n");
1577 else if (!strcasecmp(line
, "<Location"))
1581 if (!strcmp(value
, "/admin"))
1582 in_admin_location
= 1;
1583 if (!strcmp(value
, "/admin/conf"))
1584 in_conf_location
= 1;
1585 else if (!strcmp(value
, "/"))
1586 in_root_location
= 1;
1588 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1590 else if (!strcasecmp(line
, "</Location>"))
1594 if (in_admin_location
&& remote_admin
>= 0)
1596 wrote_admin_location
= 1;
1599 cupsFilePuts(temp
, " # Allow remote administration...\n");
1600 else if (remote_admin
== 0)
1601 cupsFilePuts(temp
, " # Restrict access to the admin pages...\n");
1603 cupsFilePuts(temp
, " Order allow,deny\n");
1606 cupsFilePrintf(temp
, " Allow %s\n",
1607 remote_any
> 0 ? "all" : "@LOCAL");
1609 cupsFilePuts(temp
, " Allow localhost\n");
1611 else if (in_conf_location
&& remote_admin
>= 0)
1613 wrote_conf_location
= 1;
1616 cupsFilePuts(temp
, " # Allow remote access to the configuration "
1619 cupsFilePuts(temp
, " # Restrict access to the configuration "
1622 cupsFilePuts(temp
, " Order allow,deny\n");
1625 cupsFilePrintf(temp
, " Allow %s\n",
1626 remote_any
> 0 ? "all" : "@LOCAL");
1628 cupsFilePuts(temp
, " Allow localhost\n");
1630 else if (in_root_location
&& (remote_admin
>= 0 || share_printers
>= 0))
1632 wrote_root_location
= 1;
1634 if (remote_admin
> 0 && share_printers
> 0)
1635 cupsFilePuts(temp
, " # Allow shared printing and remote "
1636 "administration...\n");
1637 else if (remote_admin
> 0)
1638 cupsFilePuts(temp
, " # Allow remote administration...\n");
1639 else if (share_printers
> 0)
1640 cupsFilePuts(temp
, " # Allow shared printing...\n");
1642 cupsFilePuts(temp
, " # Restrict access to the server...\n");
1644 cupsFilePuts(temp
, " Order allow,deny\n");
1646 if (remote_admin
> 0 || share_printers
> 0)
1647 cupsFilePrintf(temp
, " Allow %s\n",
1648 remote_any
> 0 ? "all" : "@LOCAL");
1650 cupsFilePuts(temp
, " Allow localhost\n");
1653 in_admin_location
= 0;
1654 in_conf_location
= 0;
1655 in_root_location
= 0;
1657 cupsFilePuts(temp
, "</Location>\n");
1659 else if (!strcasecmp(line
, "<Limit") && in_default_policy
)
1662 * See if the policy limit is for the Cancel-Job operation...
1665 char *valptr
; /* Pointer into value */
1670 if (!strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1673 * Don't write anything for this limit section...
1680 cupsFilePrintf(temp
, " %s", line
);
1684 for (valptr
= value
; !isspace(*valptr
& 255) && *valptr
; valptr
++);
1689 if (!strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1692 * Write everything except for this definition...
1698 cupsFilePrintf(temp
, " %s", value
);
1700 for (value
= valptr
; isspace(*value
& 255); value
++);
1703 cupsFilePuts(temp
, ">\n");
1706 else if (!strcasecmp(line
, "</Limit>") && in_cancel_job
)
1710 if (in_cancel_job
== 1)
1711 cupsFilePuts(temp
, " </Limit>\n");
1715 if (!user_cancel_any
)
1716 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1718 " <Limit Cancel-Job>\n"
1719 " Order deny,allow\n"
1720 " Require user @OWNER "
1721 CUPS_DEFAULT_PRINTADMIN_AUTH
"\n"
1726 else if ((((in_admin_location
|| in_conf_location
|| in_root_location
) &&
1727 remote_admin
>= 0) ||
1728 (in_root_location
&& share_printers
>= 0)) &&
1729 (!strcasecmp(line
, "Allow") || !strcasecmp(line
, "Deny") ||
1730 !strcasecmp(line
, "Order")))
1732 else if (in_cancel_job
== 2)
1734 else if (!strcasecmp(line
, "<Limit") && value
)
1735 cupsFilePrintf(temp
, " %s %s>\n", line
, value
);
1736 else if (line
[0] == '<')
1740 cupsFilePrintf(temp
, "%*s%s %s>\n", indent
, "", line
, value
);
1748 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1751 else if (!in_policy
&& !in_location
&&
1752 (val
= cupsGetOption(line
, num_settings
, settings
)) != NULL
)
1755 * Replace this directive's value with the new one...
1758 cupsd_num_settings
= cupsAddOption(line
, val
, cupsd_num_settings
,
1762 * Write the new value in its place, without indentation since we
1763 * only support setting root directives, not in sections...
1766 cupsFilePrintf(temp
, "%s %s\n", line
, val
);
1770 if (!in_policy
&& !in_location
)
1773 * Record the non-policy, non-location directives that we find
1774 * in the server settings, since we cache this info and record it
1775 * in _cupsAdminGetServerSettings()...
1778 cupsd_num_settings
= cupsAddOption(line
, value
, cupsd_num_settings
,
1782 cupsFilePrintf(temp
, "%*s%s %s\n", indent
, "", line
, value
);
1785 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1789 * Write any missing info...
1792 if (!wrote_browsing
&& (remote_printers
>= 0 || share_printers
>= 0))
1794 if (remote_printers
> 0 || share_printers
> 0)
1796 if (remote_printers
> 0 && share_printers
> 0)
1797 cupsFilePuts(temp
, "# Enable printer sharing and shared printers.\n");
1798 else if (remote_printers
> 0)
1799 cupsFilePuts(temp
, "# Show shared printers on the local network.\n");
1801 cupsFilePuts(temp
, "# Share local printers on the local network.\n");
1803 cupsFilePuts(temp
, "Browsing On\n");
1804 cupsFilePuts(temp
, "BrowseOrder allow,deny\n");
1806 if (remote_printers
> 0)
1807 cupsFilePuts(temp
, "BrowseAllow all\n");
1809 if (share_printers
> 0)
1810 cupsFilePuts(temp
, "BrowseAddress @LOCAL\n");
1814 cupsFilePuts(temp
, "# Disable printer sharing and shared printers.\n");
1815 cupsFilePuts(temp
, "Browsing Off\n");
1819 if (!wrote_loglevel
&& debug_logging
>= 0)
1823 cupsFilePuts(temp
, "# Show troubleshooting information in error_log.\n");
1824 cupsFilePuts(temp
, "LogLevel debug\n");
1828 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1829 cupsFilePuts(temp
, "LogLevel info\n");
1833 if (!wrote_port_listen
&& (share_printers
>= 0 || remote_admin
>= 0))
1835 if (share_printers
> 0 || remote_admin
> 0)
1837 cupsFilePuts(temp
, "# Allow remote access\n");
1838 cupsFilePrintf(temp
, "Port %d\n", ippPort());
1843 "# Only listen for connections from the local machine.\n");
1844 cupsFilePrintf(temp
, "Listen localhost:%d\n", ippPort());
1847 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1848 if (!access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1849 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1850 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1853 if (!wrote_root_location
&& (remote_admin
>= 0 || share_printers
>= 0))
1855 if (remote_admin
> 0 && share_printers
> 0)
1857 "# Allow shared printing and remote administration...\n");
1858 else if (remote_admin
> 0)
1859 cupsFilePuts(temp
, "# Allow remote administration...\n");
1860 else if (share_printers
> 0)
1861 cupsFilePuts(temp
, "# Allow shared printing...\n");
1863 cupsFilePuts(temp
, "# Restrict access to the server...\n");
1865 cupsFilePuts(temp
, "<Location />\n"
1866 " Order allow,deny\n");
1868 if (remote_admin
> 0 || share_printers
> 0)
1869 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1871 cupsFilePuts(temp
, " Allow localhost\n");
1873 cupsFilePuts(temp
, "</Location>\n");
1876 if (!wrote_admin_location
&& remote_admin
>= 0)
1879 cupsFilePuts(temp
, "# Allow remote administration...\n");
1881 cupsFilePuts(temp
, "# Restrict access to the admin pages...\n");
1883 cupsFilePuts(temp
, "<Location /admin>\n"
1884 " Order allow,deny\n");
1887 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1889 cupsFilePuts(temp
, " Allow localhost\n");
1891 cupsFilePuts(temp
, "</Location>\n");
1894 if (!wrote_conf_location
&& remote_admin
>= 0)
1898 "# Allow remote access to the configuration files...\n");
1900 cupsFilePuts(temp
, "# Restrict access to the configuration files...\n");
1902 cupsFilePuts(temp
, "<Location /admin/conf>\n"
1904 " Require user @SYSTEM\n"
1905 " Order allow,deny\n");
1908 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1910 cupsFilePuts(temp
, " Allow localhost\n");
1912 cupsFilePuts(temp
, "</Location>\n");
1915 if (!wrote_policy
&& user_cancel_any
>= 0)
1917 cupsFilePuts(temp
, "<Policy default>\n"
1918 " # Job-related operations must be done by the owner "
1919 "or an administrator...\n"
1920 " <Limit Send-Document Send-URI Hold-Job Release-Job "
1921 "Restart-Job Purge-Jobs Set-Job-Attributes "
1922 "Create-Job-Subscription Renew-Subscription "
1923 "Cancel-Subscription Get-Notifications Reprocess-Job "
1924 "Cancel-Current-Job Suspend-Current-Job Resume-Job "
1926 " Require user @OWNER @SYSTEM\n"
1927 " Order deny,allow\n"
1929 " # All administration operations require an "
1930 "administrator to authenticate...\n"
1931 " <Limit Pause-Printer Resume-Printer "
1932 "Set-Printer-Attributes Enable-Printer "
1933 "Disable-Printer Pause-Printer-After-Current-Job "
1934 "Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer "
1935 "Activate-Printer Restart-Printer Shutdown-Printer "
1936 "Startup-Printer Promote-Job Schedule-Job-After "
1937 "CUPS-Add-Printer CUPS-Delete-Printer "
1938 "CUPS-Add-Class CUPS-Delete-Class "
1939 "CUPS-Accept-Jobs CUPS-Reject-Jobs "
1940 "CUPS-Set-Default CUPS-Add-Device CUPS-Delete-Device>\n"
1942 " Require user @SYSTEM\n"
1943 " Order deny,allow\n"
1946 if (!user_cancel_any
)
1947 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1949 " <Limit Cancel-Job>\n"
1950 " Order deny,allow\n"
1951 " Require user @OWNER "
1952 CUPS_DEFAULT_PRINTADMIN_AUTH
"\n"
1955 cupsFilePuts(temp
, " <Limit All>\n"
1956 " Order deny,allow\n"
1961 for (i
= num_settings
, setting
= settings
; i
> 0; i
--, setting
++)
1962 if (setting
->name
[0] != '_' &&
1963 !cupsGetOption(setting
->name
, cupsd_num_settings
, cupsd_settings
))
1966 * Add this directive to the list of directives we have written...
1969 cupsd_num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1970 cupsd_num_settings
, &cupsd_settings
);
1973 * Write the new value, without indentation since we only support
1974 * setting root directives, not in sections...
1977 cupsFilePrintf(temp
, "%s %s\n", setting
->name
, setting
->value
);
1980 cupsFileClose(cupsd
);
1981 cupsFileClose(temp
);
1984 * Upload the configuration file to the server...
1987 status
= cupsPutFile(http
, "/admin/conf/cupsd.conf", tempfile
);
1989 if (status
== HTTP_CREATED
)
1992 * Updated OK, add the basic settings...
1995 if (debug_logging
>= 0)
1996 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1997 debug_logging
? "1" : "0",
1998 cupsd_num_settings
, &cupsd_settings
);
2000 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
2001 old_debug_logging
? "1" : "0",
2002 cupsd_num_settings
, &cupsd_settings
);
2004 if (remote_admin
>= 0)
2005 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
2006 remote_admin
? "1" : "0",
2007 cupsd_num_settings
, &cupsd_settings
);
2009 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
2010 old_remote_admin
? "1" : "0",
2011 cupsd_num_settings
, &cupsd_settings
);
2013 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
2014 remote_any
? "1" : "0",
2015 cupsd_num_settings
, &cupsd_settings
);
2017 if (remote_printers
>= 0)
2018 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
2019 remote_printers
? "1" : "0",
2020 cupsd_num_settings
, &cupsd_settings
);
2022 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
2023 old_remote_printers
? "1" : "0",
2024 cupsd_num_settings
, &cupsd_settings
);
2026 if (share_printers
>= 0)
2027 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
2028 share_printers
? "1" : "0",
2029 cupsd_num_settings
, &cupsd_settings
);
2031 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
2032 old_share_printers
? "1" : "0",
2033 cupsd_num_settings
, &cupsd_settings
);
2035 if (user_cancel_any
>= 0)
2036 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
2037 user_cancel_any
? "1" : "0",
2038 cupsd_num_settings
, &cupsd_settings
);
2040 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
2041 old_user_cancel_any
? "1" : "0",
2042 cupsd_num_settings
, &cupsd_settings
);
2045 * Save the new values...
2048 invalidate_cupsd_cache(cg
);
2050 cg
->cupsd_num_settings
= cupsd_num_settings
;
2051 cg
->cupsd_settings
= cupsd_settings
;
2052 cg
->cupsd_update
= time(NULL
);
2054 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
2057 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
2060 * Remote our temp files and return...
2068 return (status
== HTTP_CREATED
);
2073 * 'do_samba_command()' - Do a SAMBA command.
2076 static int /* O - Status of command */
2077 do_samba_command(const char *command
, /* I - Command to run */
2078 const char *address
, /* I - Address for command */
2079 const char *subcmd
, /* I - Sub-command */
2080 const char *authfile
, /* I - Samba authentication file */
2081 FILE *logfile
) /* I - Optional log file */
2084 return (1); /* Always fail on Windows... */
2087 int status
; /* Status of command */
2088 int pid
; /* Process ID of child */
2092 _cupsLangPrintf(logfile
,
2093 _("Running command: %s %s -N -A %s -c \'%s\'\n"),
2094 command
, address
, authfile
, subcmd
);
2096 if ((pid
= fork()) == 0)
2099 * Child goes here, redirect stdin/out/err and execute the command...
2103 open("/dev/null", O_RDONLY
);
2108 dup(fileno(logfile
));
2110 open("/dev/null", O_WRONLY
);
2115 execlp(command
, command
, address
, "-N", "-A", authfile
, "-c", subcmd
,
2124 _cupsLangPrintf(logfile
, _("Unable to run \"%s\": %s\n"),
2125 command
, strerror(errno
));
2130 * Wait for the process to complete...
2133 while (wait(&status
) != pid
);
2137 _cupsLangPuts(logfile
, "\n");
2139 DEBUG_printf(("status=%d\n", status
));
2141 if (WIFEXITED(status
))
2142 return (WEXITSTATUS(status
));
2144 return (-WTERMSIG(status
));
2150 * 'get_cupsd_conf()' - Get the current cupsd.conf file.
2153 static http_status_t
/* O - Status of request */
2155 http_t
*http
, /* I - Connection to server */
2156 _cups_globals_t
*cg
, /* I - Global data */
2157 time_t last_update
, /* I - Last update time for file */
2158 char *name
, /* I - Filename buffer */
2159 int namesize
, /* I - Size of filename buffer */
2160 int *remote
) /* O - Remote file? */
2162 int fd
; /* Temporary file descriptor */
2164 struct stat info
; /* cupsd.conf file information */
2166 http_status_t status
; /* Status of getting cupsd.conf */
2167 char host
[HTTP_MAX_HOST
]; /* Hostname for connection */
2171 * See if we already have the data we need...
2174 httpGetHostname(http
, host
, sizeof(host
));
2176 if (strcasecmp(cg
->cupsd_hostname
, host
))
2177 invalidate_cupsd_cache(cg
);
2179 snprintf(name
, namesize
, "%s/cupsd.conf", cg
->cups_serverroot
);
2183 if (!strcasecmp(host
, "localhost") && !access(name
, R_OK
))
2186 * Read the local file rather than using HTTP...
2189 if (stat(name
, &info
))
2191 char message
[1024]; /* Message string */
2194 snprintf(message
, sizeof(message
),
2195 _cupsLangString(cupsLangDefault(), _("stat of %s failed: %s")),
2196 name
, strerror(errno
));
2197 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
2201 return (HTTP_SERVER_ERROR
);
2203 else if (last_update
&& info
.st_mtime
<= last_update
)
2204 status
= HTTP_NOT_MODIFIED
;
2212 * Read cupsd.conf via a HTTP GET request...
2215 if ((fd
= cupsTempFd(name
, namesize
)) < 0)
2219 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
2221 invalidate_cupsd_cache(cg
);
2223 return (HTTP_SERVER_ERROR
);
2228 httpClearFields(http
);
2231 httpSetField(http
, HTTP_FIELD_IF_MODIFIED_SINCE
,
2232 httpGetDateString(last_update
));
2234 status
= cupsGetFd(http
, "/admin/conf/cupsd.conf", fd
);
2238 if (status
!= HTTP_OK
)
2250 * 'invalidate_cupsd_cache()' - Invalidate the cached cupsd.conf settings.
2254 invalidate_cupsd_cache(
2255 _cups_globals_t
*cg
) /* I - Global data */
2257 cupsFreeOptions(cg
->cupsd_num_settings
, cg
->cupsd_settings
);
2259 cg
->cupsd_hostname
[0] = '\0';
2260 cg
->cupsd_update
= 0;
2261 cg
->cupsd_num_settings
= 0;
2262 cg
->cupsd_settings
= NULL
;
2267 * 'write_option()' - Write a CUPS option to a PPD file.
2271 write_option(cups_file_t
*dstfp
, /* I - PPD file */
2272 int order
, /* I - Order dependency */
2273 const char *name
, /* I - Option name */
2274 const char *text
, /* I - Option text */
2275 const char *attrname
, /* I - Attribute name */
2276 ipp_attribute_t
*suppattr
, /* I - IPP -supported attribute */
2277 ipp_attribute_t
*defattr
, /* I - IPP -default attribute */
2278 int defval
, /* I - Default value number */
2279 int valcount
) /* I - Number of values */
2281 int i
; /* Looping var */
2284 cupsFilePrintf(dstfp
, "*JCLOpenUI *%s/%s: PickOne\n"
2285 "*OrderDependency: %d JCLSetup *%s\n",
2286 name
, text
, order
, name
);
2288 if (defattr
->value_tag
== IPP_TAG_INTEGER
)
2291 * Do numeric options with a range or list...
2294 cupsFilePrintf(dstfp
, "*Default%s: %d\n", name
,
2295 defattr
->values
[defval
].integer
);
2297 if (suppattr
->value_tag
== IPP_TAG_RANGE
)
2300 * List each number in the range...
2303 for (i
= suppattr
->values
[0].range
.lower
;
2304 i
<= suppattr
->values
[0].range
.upper
;
2307 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, i
);
2310 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n",
2312 else if (defval
== 0)
2313 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
, i
);
2314 else if (defval
< (valcount
- 1))
2315 cupsFilePrintf(dstfp
, ",%d\"\n", i
);
2317 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", i
);
2323 * List explicit numbers...
2326 for (i
= 0; i
< suppattr
->num_values
; i
++)
2328 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, suppattr
->values
[i
].integer
);
2331 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n", attrname
,
2332 suppattr
->values
[i
].integer
);
2333 else if (defval
== 0)
2334 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
,
2335 suppattr
->values
[i
].integer
);
2336 else if (defval
< (valcount
- 1))
2337 cupsFilePrintf(dstfp
, ",%d\"\n", suppattr
->values
[i
].integer
);
2339 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", suppattr
->values
[i
].integer
);
2346 * Do text options with a list...
2349 cupsFilePrintf(dstfp
, "*Default%s: %s\n", name
,
2350 defattr
->values
[defval
].string
.text
);
2352 for (i
= 0; i
< suppattr
->num_values
; i
++)
2354 cupsFilePrintf(dstfp
, "*%s %s: \"", name
,
2355 suppattr
->values
[i
].string
.text
);
2358 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\n\"\n*End\n", attrname
,
2359 suppattr
->values
[i
].string
.text
);
2360 else if (defval
== 0)
2361 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\"\n", attrname
,
2362 suppattr
->values
[i
].string
.text
);
2363 else if (defval
< (valcount
- 1))
2364 cupsFilePrintf(dstfp
, ",%s\"\n", suppattr
->values
[i
].string
.text
);
2366 cupsFilePrintf(dstfp
, ",%s\n\"\n*End\n",
2367 suppattr
->values
[i
].string
.text
);
2371 cupsFilePrintf(dstfp
, "*JCLCloseUI: *%s\n\n", name
);
2376 * End of "$Id: adminutil.c 6930 2007-09-08 00:28:06Z mike $".