2 * "$Id: adminutil.c 7576 2008-05-15 21:05:14Z mike $"
4 * Administration utility API definitions for the Common UNIX Printing
7 * Copyright 2007-2008 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 or @code CUPS_HTTP_DEFAULT@ */
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...
125 http
= _cupsConnect();
127 if (!http
|| !dest
|| !buffer
|| bufsize
< 2)
131 * Get the PPD file...
134 if ((src
= cupsGetPPD2(http
, dest
)) == NULL
)
138 * Get the supported banner pages, etc. for the printer...
141 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
143 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
144 "localhost", 0, "/printers/%s", dest
);
145 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
146 "printer-uri", NULL
, uri
);
148 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
149 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
153 * Do the request and get back a response...
156 response
= cupsDoRequest(http
, request
, "/");
157 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
164 * Open the original PPD file...
167 if ((srcfp
= cupsFileOpen(src
, "rb")) == NULL
)
171 * Create a temporary output file using the destination buffer...
174 if ((dstfp
= cupsTempFile2(buffer
, bufsize
)) == NULL
)
176 cupsFileClose(srcfp
);
184 * Write a new header explaining that this isn't the original PPD...
187 cupsFilePuts(dstfp
, "*PPD-Adobe: \"4.3\"\n");
189 curtime
= time(NULL
);
190 curdate
= gmtime(&curtime
);
192 cupsFilePrintf(dstfp
, "*%% Modified on %04d%02d%02d%02d%02d%02d+0000 "
193 "for CUPS Windows Driver\n",
194 curdate
->tm_year
+ 1900, curdate
->tm_mon
+ 1, curdate
->tm_mday
,
195 curdate
->tm_hour
, curdate
->tm_min
, curdate
->tm_sec
);
198 * Read the existing PPD file, converting all PJL commands to CUPS
199 * job ticket comments...
205 language
= cupsLangDefault();
207 while (cupsFileGets(srcfp
, line
, sizeof(line
)))
211 if (!strncmp(line
, "*PPD-Adobe:", 11))
214 * Already wrote the PPD header...
219 else if (!strncmp(line
, "*JCLBegin:", 10) ||
220 !strncmp(line
, "*JCLToPSInterpreter:", 20) ||
221 !strncmp(line
, "*JCLEnd:", 8) ||
222 !strncmp(line
, "*Protocols:", 11))
225 * Don't use existing JCL keywords; we'll create our own, below...
228 cupsFilePrintf(dstfp
, "*%% Commented out for CUPS Windows Driver...\n"
229 "*%%%s\n", line
+ 1);
232 else if (!strncmp(line
, "*JCLOpenUI", 10))
235 cupsFilePrintf(dstfp
, "%s\n", line
);
237 else if (!strncmp(line
, "*JCLCloseUI", 11))
240 cupsFilePrintf(dstfp
, "%s\n", line
);
242 else if (jcloption
&& !strncmp(line
, "*OrderDependency:", 17))
244 for (ptr
= line
+ 17; *ptr
&& isspace(*ptr
& 255); ptr
++);
246 ptr
= strchr(ptr
, ' ');
250 cupsFilePrintf(dstfp
, "*OrderDependency: %d%s\n", jclorder
, ptr
);
254 cupsFilePrintf(dstfp
, "%s\n", line
);
256 else if (jcloption
&&
257 strncmp(line
, "*End", 4) &&
258 strncmp(line
, "*Default", 8))
260 if ((ptr
= strchr(line
, ':')) == NULL
)
262 snprintf(line
, sizeof(line
),
263 _cupsLangString(language
, _("Missing value on line %d!")),
265 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
, 0);
267 cupsFileClose(srcfp
);
268 cupsFileClose(dstfp
);
278 if ((ptr
= strchr(ptr
, '\"')) == NULL
)
280 snprintf(line
, sizeof(line
),
281 _cupsLangString(language
,
282 _("Missing double quote on line %d!")),
284 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
, 0);
286 cupsFileClose(srcfp
);
287 cupsFileClose(dstfp
);
297 if (sscanf(line
, "*%40s%*[ \t]%40[^:/]", option
, choice
) != 2)
299 snprintf(line
, sizeof(line
),
300 _cupsLangString(language
,
301 _("Bad option + choice on line %d!")),
303 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
, 0);
305 cupsFileClose(srcfp
);
306 cupsFileClose(dstfp
);
316 if (strchr(ptr
+ 1, '\"') == NULL
)
322 while (cupsFileGets(srcfp
, junk
, sizeof(junk
)) != NULL
)
326 if (!strncmp(junk
, "*End", 4))
331 snprintf(ptr
+ 1, sizeof(line
) - (ptr
- line
+ 1),
332 "%%cupsJobTicket: %s=%s\n\"\n*End", option
, choice
);
334 cupsFilePrintf(dstfp
, "*%% Changed for CUPS Windows Driver...\n%s\n",
338 cupsFilePrintf(dstfp
, "%s\n", line
);
341 cupsFileClose(srcfp
);
346 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, _("Empty PPD file!"), 1);
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
, strerror(EINVAL
), 0);
439 * Create a temporary authentication file for Samba...
442 if ((fp
= cupsTempFile2(authfile
, sizeof(authfile
))) == NULL
)
444 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
, 0);
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
, 0);
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
, 0);
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
, 0);
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
, 0);
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
, 0);
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
, 0);
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
, 0);
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
, 0);
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
, 0);
805 _cupsLangPrintf(logfile
, "%s\n", message
);
808 if (have_drivers
== 0)
810 _cupsSetError(IPP_NOT_FOUND
, message
, 0);
818 * Finally, associate the drivers we just added with the queue...
821 snprintf(subcmd
, sizeof(subcmd
), "setdriver %s %s", dest
, dest
);
823 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
824 authfile
, logfile
)) != 0)
826 snprintf(message
, sizeof(message
),
827 _cupsLangString(language
,
828 _("Unable to set Windows printer driver (%d)!")),
831 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
834 _cupsLangPrintf(logfile
, "%s\n", message
);
848 * 'cupsAdminGetServerSettings()' - Get settings from the server.
850 * The returned settings should be freed with cupsFreeOptions() when
851 * you are done with them.
856 int /* O - 1 on success, 0 on failure */
857 cupsAdminGetServerSettings(
858 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
859 int *num_settings
, /* O - Number of settings */
860 cups_option_t
**settings
) /* O - Settings */
862 return (_cupsAdminGetServerSettings(http
, num_settings
, settings
));
867 * '_cupsAdminGetServerSettings()' - Get settings from the server.
869 * The returned settings should be freed with cupsFreeOptions() when
870 * you are done with them.
875 int /* O - 1 on success, 0 on failure */
876 _cupsAdminGetServerSettings(
877 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
878 int *num_settings
, /* O - Number of settings */
879 cups_option_t
**settings
) /* O - Settings */
881 int i
; /* Looping var */
882 cups_file_t
*cupsd
; /* cupsd.conf file */
883 char cupsdconf
[1024]; /* cupsd.conf filename */
884 int remote
; /* Remote cupsd.conf file? */
885 http_status_t status
; /* Status of getting cupsd.conf */
886 char line
[1024], /* Line from cupsd.conf file */
887 *value
; /* Value on line */
888 cups_option_t
*setting
; /* Current setting */
889 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
893 * Range check input...
897 http
= _cupsConnect();
899 if (!http
|| !num_settings
|| !settings
)
901 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
916 * Get the cupsd.conf file...
919 if ((status
= get_cupsd_conf(http
, cg
, cg
->cupsd_update
, cupsdconf
,
920 sizeof(cupsdconf
), &remote
)) == HTTP_OK
)
922 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
924 char message
[1024]; /* Message string */
927 snprintf(message
, sizeof(message
),
928 _cupsLangString(cupsLangDefault(), _("Open of %s failed: %s")),
929 cupsdconf
, strerror(errno
));
930 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
939 * Read the file, keeping track of what settings are enabled...
942 int remote_access
= 0, /* Remote access allowed? */
943 remote_admin
= 0, /* Remote administration allowed? */
944 remote_any
= 0, /* Remote access from anywhere allowed? */
945 browsing
= 1, /* Browsing enabled? */
946 browse_allow
= 1, /* Browse address set? */
947 browse_address
= 0, /* Browse address set? */
948 cancel_policy
= 1, /* Cancel-job policy set? */
949 debug_logging
= 0; /* LogLevel debug set? */
950 int linenum
= 0, /* Line number in file */
951 in_location
= 0, /* In a location section? */
952 in_policy
= 0, /* In a policy section? */
953 in_cancel_job
= 0, /* In a cancel-job section? */
954 in_admin_location
= 0; /* In the /admin location? */
957 invalidate_cupsd_cache(cg
);
959 cg
->cupsd_update
= time(NULL
);
960 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
962 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
964 if (!value
&& strncmp(line
, "</", 2))
965 value
= line
+ strlen(line
);
967 if ((!strcasecmp(line
, "Port") || !strcasecmp(line
, "Listen")) && value
)
969 char *port
; /* Pointer to port number, if any */
972 if ((port
= strrchr(value
, ':')) != NULL
)
974 else if (isdigit(*value
& 255))
977 * Listen on a port number implies remote access...
984 if (strcasecmp(value
, "localhost") && strcmp(value
, "127.0.0.1")
987 #endif /* AF_LOCAL */
989 && strcmp(value
, "::1")
990 #endif /* AF_INET6 */
994 else if (!strcasecmp(line
, "Browsing"))
996 browsing
= !strcasecmp(value
, "yes") || !strcasecmp(value
, "on") ||
997 !strcasecmp(value
, "true");
999 else if (!strcasecmp(line
, "BrowseAddress"))
1003 else if (!strcasecmp(line
, "BrowseAllow"))
1007 else if (!strcasecmp(line
, "BrowseOrder"))
1009 browse_allow
= !strncasecmp(value
, "deny,", 5);
1011 else if (!strcasecmp(line
, "LogLevel"))
1013 debug_logging
= !strncasecmp(value
, "debug", 5);
1015 else if (!strcasecmp(line
, "<Policy") && !strcasecmp(value
, "default"))
1019 else if (!strcasecmp(line
, "</Policy>"))
1023 else if (!strcasecmp(line
, "<Limit") && in_policy
&& value
)
1026 * See if the policy limit is for the Cancel-Job operation...
1029 char *valptr
; /* Pointer into value */
1034 for (valptr
= value
; !isspace(*valptr
& 255) && *valptr
; valptr
++);
1039 if (!strcasecmp(value
, "cancel-job") || !strcasecmp(value
, "all"))
1045 for (value
= valptr
; isspace(*value
& 255); value
++);
1048 else if (!strcasecmp(line
, "</Limit>"))
1052 else if (!strcasecmp(line
, "Require") && in_cancel_job
)
1056 else if (!strcasecmp(line
, "<Location") && value
)
1058 in_admin_location
= !strcasecmp(value
, "/admin");
1061 else if (!strcasecmp(line
, "</Location>"))
1063 in_admin_location
= 0;
1066 else if (!strcasecmp(line
, "Allow") && value
&&
1067 strcasecmp(value
, "localhost") && strcasecmp(value
, "127.0.0.1")
1070 #endif /* AF_LOCAL */
1072 && strcmp(value
, "::1")
1073 #endif /* AF_INET6 */
1076 if (in_admin_location
)
1078 else if (!strcasecmp(value
, "all"))
1081 else if (line
[0] != '<' && !in_location
&& !in_policy
)
1082 cg
->cupsd_num_settings
= cupsAddOption(line
, value
,
1083 cg
->cupsd_num_settings
,
1084 &(cg
->cupsd_settings
));
1087 cupsFileClose(cupsd
);
1089 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1090 debug_logging
? "1" : "0",
1091 cg
->cupsd_num_settings
,
1092 &(cg
->cupsd_settings
));
1094 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1095 (remote_access
&& remote_admin
) ?
1097 cg
->cupsd_num_settings
,
1098 &(cg
->cupsd_settings
));
1100 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
1101 remote_any
? "1" : "0",
1102 cg
->cupsd_num_settings
,
1103 &(cg
->cupsd_settings
));
1105 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
1106 (browsing
&& browse_allow
) ?
1108 cg
->cupsd_num_settings
,
1109 &(cg
->cupsd_settings
));
1111 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1112 (remote_access
&& browsing
&&
1113 browse_address
) ? "1" : "0",
1114 cg
->cupsd_num_settings
,
1115 &(cg
->cupsd_settings
));
1117 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1118 cancel_policy
? "1" : "0",
1119 cg
->cupsd_num_settings
,
1120 &(cg
->cupsd_settings
));
1122 else if (status
!= HTTP_NOT_MODIFIED
)
1123 invalidate_cupsd_cache(cg
);
1126 * Remove any temporary files and copy the settings array...
1132 for (i
= cg
->cupsd_num_settings
, setting
= cg
->cupsd_settings
;
1135 *num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1136 *num_settings
, settings
);
1138 return (cg
->cupsd_num_settings
> 0);
1143 * 'cupsAdminSetServerSettings()' - Set settings on the server.
1148 int /* O - 1 on success, 0 on failure */
1149 cupsAdminSetServerSettings(
1150 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1151 int num_settings
, /* I - Number of settings */
1152 cups_option_t
*settings
) /* I - Settings */
1154 return (_cupsAdminSetServerSettings(http
, num_settings
, settings
));
1159 * '_cupsAdminSetServerSettings()' - Set settings on the server.
1164 int /* O - 1 on success, 0 on failure */
1165 _cupsAdminSetServerSettings(
1166 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1167 int num_settings
, /* I - Number of settings */
1168 cups_option_t
*settings
) /* I - Settings */
1170 int i
; /* Looping var */
1171 http_status_t status
; /* GET/PUT status */
1172 const char *server_port_env
; /* SERVER_PORT env var */
1173 int server_port
; /* IPP port for server */
1174 cups_file_t
*cupsd
; /* cupsd.conf file */
1175 char cupsdconf
[1024]; /* cupsd.conf filename */
1176 int remote
; /* Remote cupsd.conf file? */
1177 char tempfile
[1024]; /* Temporary new cupsd.conf */
1178 cups_file_t
*temp
; /* Temporary file */
1179 char line
[1024], /* Line from cupsd.conf file */
1180 *value
; /* Value on line */
1181 int linenum
, /* Line number in file */
1182 in_location
, /* In a location section? */
1183 in_policy
, /* In a policy section? */
1184 in_default_policy
, /* In the default policy section? */
1185 in_cancel_job
, /* In a cancel-job section? */
1186 in_admin_location
, /* In the /admin location? */
1187 in_conf_location
, /* In the /admin/conf location? */
1188 in_root_location
; /* In the / location? */
1189 const char *val
; /* Setting value */
1190 int remote_printers
, /* Show remote printers */
1191 share_printers
, /* Share local printers */
1192 remote_admin
, /* Remote administration allowed? */
1193 remote_any
, /* Remote access from anywhere? */
1194 user_cancel_any
, /* Cancel-job policy set? */
1195 debug_logging
; /* LogLevel debug set? */
1196 int wrote_port_listen
, /* Wrote the port/listen lines? */
1197 wrote_browsing
, /* Wrote the browsing lines? */
1198 wrote_policy
, /* Wrote the policy? */
1199 wrote_loglevel
, /* Wrote the LogLevel line? */
1200 wrote_admin_location
, /* Wrote the /admin location? */
1201 wrote_conf_location
, /* Wrote the /admin/conf location? */
1202 wrote_root_location
; /* Wrote the / location? */
1203 int indent
; /* Indentation */
1204 int cupsd_num_settings
; /* New number of settings */
1205 int old_remote_printers
, /* Show remote printers */
1206 old_share_printers
, /* Share local printers */
1207 old_remote_admin
, /* Remote administration allowed? */
1208 old_user_cancel_any
, /* Cancel-job policy set? */
1209 old_debug_logging
; /* LogLevel debug set? */
1210 cups_option_t
*cupsd_settings
, /* New settings */
1211 *setting
; /* Current setting */
1212 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1216 * Range check input...
1220 http
= _cupsConnect();
1222 if (!http
|| !num_settings
|| !settings
)
1224 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
1230 * Get the cupsd.conf file...
1233 if (get_cupsd_conf(http
, cg
, 0, cupsdconf
, sizeof(cupsdconf
),
1234 &remote
) == HTTP_OK
)
1236 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
1238 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
, 0);
1246 * Get current settings...
1249 if (!_cupsAdminGetServerSettings(http
, &cupsd_num_settings
,
1253 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, cupsd_num_settings
,
1254 cupsd_settings
)) != NULL
)
1255 old_debug_logging
= atoi(val
);
1257 old_debug_logging
= 0;
1259 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, cupsd_num_settings
,
1260 cupsd_settings
)) != NULL
)
1261 old_remote_admin
= atoi(val
);
1263 old_remote_admin
= 0;
1265 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, cupsd_num_settings
,
1266 cupsd_settings
)) != NULL
)
1267 remote_any
= atoi(val
);
1271 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
, cupsd_num_settings
,
1272 cupsd_settings
)) != NULL
)
1273 old_remote_printers
= atoi(val
);
1275 old_remote_printers
= 1;
1277 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, cupsd_num_settings
,
1278 cupsd_settings
)) != NULL
)
1279 old_share_printers
= atoi(val
);
1281 old_share_printers
= 0;
1283 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, cupsd_num_settings
,
1284 cupsd_settings
)) != NULL
)
1285 old_user_cancel_any
= atoi(val
);
1287 old_user_cancel_any
= 0;
1289 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
1292 * Get basic settings...
1295 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, num_settings
,
1298 debug_logging
= atoi(val
);
1300 if (debug_logging
== old_debug_logging
)
1303 * No change to this setting...
1312 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, num_settings
,
1314 remote_any
= atoi(val
);
1316 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, num_settings
,
1319 remote_admin
= atoi(val
);
1321 if (remote_admin
== old_remote_admin
&& remote_any
< 0)
1324 * No change to this setting...
1333 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
, num_settings
,
1336 remote_printers
= atoi(val
);
1338 if (remote_printers
== old_remote_printers
)
1341 * No change to this setting...
1344 remote_printers
= -1;
1348 remote_printers
= -1;
1350 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, num_settings
,
1353 share_printers
= atoi(val
);
1355 if (share_printers
== old_share_printers
&& remote_any
< 0)
1358 * No change to this setting...
1361 share_printers
= -1;
1365 share_printers
= -1;
1367 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, num_settings
,
1370 user_cancel_any
= atoi(val
);
1372 if (user_cancel_any
== old_user_cancel_any
)
1375 * No change to this setting...
1378 user_cancel_any
= -1;
1382 user_cancel_any
= -1;
1385 * Create a temporary file for the new cupsd.conf file...
1388 if ((temp
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1390 cupsFileClose(cupsd
);
1395 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
, 0);
1400 * Copy the old file to the new, making changes along the way...
1403 cupsd_num_settings
= 0;
1404 in_admin_location
= 0;
1406 in_conf_location
= 0;
1407 in_default_policy
= 0;
1410 in_root_location
= 0;
1412 wrote_admin_location
= 0;
1414 wrote_conf_location
= 0;
1417 wrote_port_listen
= 0;
1418 wrote_root_location
= 0;
1421 if ((server_port_env
= getenv("SERVER_PORT")) != NULL
)
1423 if ((server_port
= atoi(server_port_env
)) <= 0)
1424 server_port
= ippPort();
1427 server_port
= ippPort();
1429 if (server_port
<= 0)
1430 server_port
= IPP_PORT
;
1432 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
1434 if ((!strcasecmp(line
, "Port") || !strcasecmp(line
, "Listen")) &&
1435 (share_printers
>= 0 || remote_admin
>= 0))
1437 if (!wrote_port_listen
)
1439 wrote_port_listen
= 1;
1441 if (share_printers
> 0 || remote_admin
> 0)
1443 cupsFilePuts(temp
, "# Allow remote access\n");
1444 cupsFilePrintf(temp
, "Port %d\n", server_port
);
1448 cupsFilePuts(temp
, "# Only listen for connections from the local "
1450 cupsFilePrintf(temp
, "Listen localhost:%d\n", server_port
);
1453 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1454 if ((!value
|| strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)) &&
1455 !access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1456 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1457 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1459 else if (value
&& value
[0] == '/'
1460 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1461 && strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)
1462 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1464 cupsFilePrintf(temp
, "Listen %s\n", value
);
1466 else if ((!strcasecmp(line
, "Browsing") ||
1467 !strcasecmp(line
, "BrowseAddress") ||
1468 !strcasecmp(line
, "BrowseAllow") ||
1469 !strcasecmp(line
, "BrowseDeny") ||
1470 !strcasecmp(line
, "BrowseLocalProtocols") ||
1471 !strcasecmp(line
, "BrowseRemoteProtocols") ||
1472 !strcasecmp(line
, "BrowseOrder")) &&
1473 (remote_printers
>= 0 || share_printers
>= 0))
1475 if (!wrote_browsing
)
1477 int new_remote_printers
= (remote_printers
> 0 ||
1478 (remote_printers
== -1 &&
1479 old_remote_printers
> 0));
1480 int new_share_printers
= (share_printers
> 0 ||
1481 (share_printers
== -1 &&
1482 old_share_printers
> 0));
1486 if (new_remote_printers
|| new_share_printers
)
1488 const char *localp
= cupsGetOption("BrowseLocalProtocols",
1489 num_settings
, settings
);
1490 const char *remotep
= cupsGetOption("BrowseRemoteProtocols",
1491 num_settings
, settings
);
1494 localp
= cupsGetOption("BrowseLocalProtocols", cupsd_num_settings
,
1498 remotep
= cupsGetOption("BrowseRemoteProtocols", cupsd_num_settings
,
1501 if (new_remote_printers
&& new_share_printers
)
1503 "# Enable printer sharing and shared printers.\n");
1504 else if (new_remote_printers
)
1506 "# Show shared printers on the local network.\n");
1509 "# Share local printers on the local network.\n");
1511 cupsFilePuts(temp
, "Browsing On\n");
1512 cupsFilePuts(temp
, "BrowseOrder allow,deny\n");
1514 if (new_remote_printers
)
1516 cupsFilePuts(temp
, "BrowseAllow all\n");
1518 if (!remotep
|| !*remotep
)
1519 cupsFilePuts(temp
, "BrowseRemoteProtocols "
1520 CUPS_DEFAULT_BROWSE_REMOTE_PROTOCOLS
"\n");
1522 cupsFilePrintf(temp
, "BrowseRemoteProtocols %s\n", remotep
);
1525 cupsFilePuts(temp
, "BrowseRemoteProtocols\n");
1527 if (new_share_printers
)
1529 cupsFilePuts(temp
, "BrowseAddress @LOCAL\n");
1531 if (!localp
|| !*localp
)
1532 cupsFilePuts(temp
, "BrowseLocalProtocols "
1533 CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS
"\n");
1535 cupsFilePrintf(temp
, "BrowseLocalProtocols %s\n", localp
);
1538 cupsFilePuts(temp
, "BrowseLocalProtocols\n");
1543 "# Disable printer sharing and shared printers.\n");
1544 cupsFilePuts(temp
, "Browsing Off\n");
1548 else if (!strcasecmp(line
, "LogLevel") && debug_logging
>= 0)
1555 "# Show troubleshooting information in error_log.\n");
1556 cupsFilePuts(temp
, "LogLevel debug\n");
1560 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1561 cupsFilePuts(temp
, "LogLevel " CUPS_DEFAULT_LOG_LEVEL
"\n");
1564 else if (!strcasecmp(line
, "<Policy"))
1566 in_default_policy
= !strcasecmp(value
, "default");
1569 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1572 else if (!strcasecmp(line
, "</Policy>"))
1575 if (!wrote_policy
&& in_default_policy
)
1579 if (!user_cancel_any
)
1580 cupsFilePuts(temp
, " # Only the owner or an administrator can "
1582 " <Limit Cancel-Job>\n"
1583 " Order deny,allow\n"
1584 " Require user @OWNER "
1585 CUPS_DEFAULT_PRINTADMIN_AUTH
"\n"
1590 in_default_policy
= 0;
1592 cupsFilePuts(temp
, "</Policy>\n");
1594 else if (!strcasecmp(line
, "<Location"))
1598 if (!strcmp(value
, "/admin"))
1599 in_admin_location
= 1;
1600 if (!strcmp(value
, "/admin/conf"))
1601 in_conf_location
= 1;
1602 else if (!strcmp(value
, "/"))
1603 in_root_location
= 1;
1605 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1607 else if (!strcasecmp(line
, "</Location>"))
1611 if (in_admin_location
&& remote_admin
>= 0)
1613 wrote_admin_location
= 1;
1616 cupsFilePuts(temp
, " # Allow remote administration...\n");
1617 else if (remote_admin
== 0)
1618 cupsFilePuts(temp
, " # Restrict access to the admin pages...\n");
1620 cupsFilePuts(temp
, " Order allow,deny\n");
1623 cupsFilePrintf(temp
, " Allow %s\n",
1624 remote_any
> 0 ? "all" : "@LOCAL");
1626 else if (in_conf_location
&& remote_admin
>= 0)
1628 wrote_conf_location
= 1;
1631 cupsFilePuts(temp
, " # Allow remote access to the configuration "
1634 cupsFilePuts(temp
, " # Restrict access to the configuration "
1637 cupsFilePuts(temp
, " Order allow,deny\n");
1640 cupsFilePrintf(temp
, " Allow %s\n",
1641 remote_any
> 0 ? "all" : "@LOCAL");
1643 else if (in_root_location
&& (remote_admin
>= 0 || share_printers
>= 0))
1645 wrote_root_location
= 1;
1647 if (remote_admin
> 0 && share_printers
> 0)
1648 cupsFilePuts(temp
, " # Allow shared printing and remote "
1649 "administration...\n");
1650 else if (remote_admin
> 0)
1651 cupsFilePuts(temp
, " # Allow remote administration...\n");
1652 else if (share_printers
> 0)
1653 cupsFilePuts(temp
, " # Allow shared printing...\n");
1655 cupsFilePuts(temp
, " # Restrict access to the server...\n");
1657 cupsFilePuts(temp
, " Order allow,deny\n");
1659 if (remote_admin
> 0 || share_printers
> 0)
1660 cupsFilePrintf(temp
, " Allow %s\n",
1661 remote_any
> 0 ? "all" : "@LOCAL");
1664 in_admin_location
= 0;
1665 in_conf_location
= 0;
1666 in_root_location
= 0;
1668 cupsFilePuts(temp
, "</Location>\n");
1670 else if (!strcasecmp(line
, "<Limit") && in_default_policy
)
1673 * See if the policy limit is for the Cancel-Job operation...
1676 char *valptr
; /* Pointer into value */
1681 if (!strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1684 * Don't write anything for this limit section...
1691 cupsFilePrintf(temp
, " %s", line
);
1695 for (valptr
= value
; !isspace(*valptr
& 255) && *valptr
; valptr
++);
1700 if (!strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1703 * Write everything except for this definition...
1709 cupsFilePrintf(temp
, " %s", value
);
1711 for (value
= valptr
; isspace(*value
& 255); value
++);
1714 cupsFilePuts(temp
, ">\n");
1717 else if (!strcasecmp(line
, "</Limit>") && in_cancel_job
)
1721 if (in_cancel_job
== 1)
1722 cupsFilePuts(temp
, " </Limit>\n");
1726 if (!user_cancel_any
)
1727 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1729 " <Limit Cancel-Job>\n"
1730 " Order deny,allow\n"
1731 " Require user @OWNER "
1732 CUPS_DEFAULT_PRINTADMIN_AUTH
"\n"
1737 else if ((((in_admin_location
|| in_conf_location
|| in_root_location
) &&
1738 remote_admin
>= 0) ||
1739 (in_root_location
&& share_printers
>= 0)) &&
1740 (((!strcasecmp(line
, "Allow") || !strcasecmp(line
, "Deny")) &&
1741 !strcasecmp(value
, "@LOCAL")) ||
1742 !strcasecmp(line
, "Order")))
1744 else if (in_cancel_job
== 2)
1746 else if (!strcasecmp(line
, "<Limit") && value
)
1747 cupsFilePrintf(temp
, " %s %s>\n", line
, value
);
1748 else if (line
[0] == '<')
1752 cupsFilePrintf(temp
, "%*s%s %s>\n", indent
, "", line
, value
);
1760 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1763 else if (!in_policy
&& !in_location
&&
1764 (val
= cupsGetOption(line
, num_settings
, settings
)) != NULL
)
1767 * Replace this directive's value with the new one...
1770 cupsd_num_settings
= cupsAddOption(line
, val
, cupsd_num_settings
,
1774 * Write the new value in its place, without indentation since we
1775 * only support setting root directives, not in sections...
1778 cupsFilePrintf(temp
, "%s %s\n", line
, val
);
1782 if (!in_policy
&& !in_location
)
1785 * Record the non-policy, non-location directives that we find
1786 * in the server settings, since we cache this info and record it
1787 * in _cupsAdminGetServerSettings()...
1790 cupsd_num_settings
= cupsAddOption(line
, value
, cupsd_num_settings
,
1794 cupsFilePrintf(temp
, "%*s%s %s\n", indent
, "", line
, value
);
1797 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1801 * Write any missing info...
1804 if (!wrote_browsing
&& (remote_printers
>= 0 || share_printers
>= 0))
1806 if (remote_printers
> 0 || share_printers
> 0)
1808 if (remote_printers
> 0 && share_printers
> 0)
1809 cupsFilePuts(temp
, "# Enable printer sharing and shared printers.\n");
1810 else if (remote_printers
> 0)
1811 cupsFilePuts(temp
, "# Show shared printers on the local network.\n");
1813 cupsFilePuts(temp
, "# Share local printers on the local network.\n");
1815 cupsFilePuts(temp
, "Browsing On\n");
1816 cupsFilePuts(temp
, "BrowseOrder allow,deny\n");
1818 if (remote_printers
> 0)
1819 cupsFilePuts(temp
, "BrowseAllow all\n");
1821 if (share_printers
> 0)
1822 cupsFilePuts(temp
, "BrowseAddress @LOCAL\n");
1826 cupsFilePuts(temp
, "# Disable printer sharing and shared printers.\n");
1827 cupsFilePuts(temp
, "Browsing Off\n");
1831 if (!wrote_loglevel
&& debug_logging
>= 0)
1835 cupsFilePuts(temp
, "# Show troubleshooting information in error_log.\n");
1836 cupsFilePuts(temp
, "LogLevel debug\n");
1840 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1841 cupsFilePuts(temp
, "LogLevel " CUPS_DEFAULT_LOG_LEVEL
"\n");
1845 if (!wrote_port_listen
&& (share_printers
>= 0 || remote_admin
>= 0))
1847 if (share_printers
> 0 || remote_admin
> 0)
1849 cupsFilePuts(temp
, "# Allow remote access\n");
1850 cupsFilePrintf(temp
, "Port %d\n", ippPort());
1855 "# Only listen for connections from the local machine.\n");
1856 cupsFilePrintf(temp
, "Listen localhost:%d\n", ippPort());
1859 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1860 if (!access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1861 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1862 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1865 if (!wrote_root_location
&& (remote_admin
>= 0 || share_printers
>= 0))
1867 if (remote_admin
> 0 && share_printers
> 0)
1869 "# Allow shared printing and remote administration...\n");
1870 else if (remote_admin
> 0)
1871 cupsFilePuts(temp
, "# Allow remote administration...\n");
1872 else if (share_printers
> 0)
1873 cupsFilePuts(temp
, "# Allow shared printing...\n");
1875 cupsFilePuts(temp
, "# Restrict access to the server...\n");
1877 cupsFilePuts(temp
, "<Location />\n"
1878 " Order allow,deny\n");
1880 if (remote_admin
> 0 || share_printers
> 0)
1881 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1883 cupsFilePuts(temp
, "</Location>\n");
1886 if (!wrote_admin_location
&& remote_admin
>= 0)
1889 cupsFilePuts(temp
, "# Allow remote administration...\n");
1891 cupsFilePuts(temp
, "# Restrict access to the admin pages...\n");
1893 cupsFilePuts(temp
, "<Location /admin>\n"
1894 " Order allow,deny\n");
1897 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1899 cupsFilePuts(temp
, "</Location>\n");
1902 if (!wrote_conf_location
&& remote_admin
>= 0)
1906 "# Allow remote access to the configuration files...\n");
1908 cupsFilePuts(temp
, "# Restrict access to the configuration files...\n");
1910 cupsFilePuts(temp
, "<Location /admin/conf>\n"
1911 " AuthType Default\n"
1912 " Require user @SYSTEM\n"
1913 " Order allow,deny\n");
1916 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1918 cupsFilePuts(temp
, "</Location>\n");
1921 if (!wrote_policy
&& user_cancel_any
>= 0)
1923 cupsFilePuts(temp
, "<Policy default>\n"
1924 " # Job-related operations must be done by the owner "
1925 "or an administrator...\n"
1926 " <Limit Send-Document Send-URI Hold-Job Release-Job "
1927 "Restart-Job Purge-Jobs Set-Job-Attributes "
1928 "Create-Job-Subscription Renew-Subscription "
1929 "Cancel-Subscription Get-Notifications Reprocess-Job "
1930 "Cancel-Current-Job Suspend-Current-Job Resume-Job "
1932 " Require user @OWNER @SYSTEM\n"
1933 " Order deny,allow\n"
1935 " # All administration operations require an "
1936 "administrator to authenticate...\n"
1937 " <Limit Pause-Printer Resume-Printer "
1938 "Set-Printer-Attributes Enable-Printer "
1939 "Disable-Printer Pause-Printer-After-Current-Job "
1940 "Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer "
1941 "Activate-Printer Restart-Printer Shutdown-Printer "
1942 "Startup-Printer Promote-Job Schedule-Job-After "
1943 "CUPS-Add-Printer CUPS-Delete-Printer "
1944 "CUPS-Add-Class CUPS-Delete-Class "
1945 "CUPS-Accept-Jobs CUPS-Reject-Jobs "
1946 "CUPS-Set-Default CUPS-Add-Device CUPS-Delete-Device>\n"
1947 " AuthType Default\n"
1948 " Require user @SYSTEM\n"
1949 " Order deny,allow\n"
1952 if (!user_cancel_any
)
1953 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1955 " <Limit Cancel-Job>\n"
1956 " Order deny,allow\n"
1957 " Require user @OWNER "
1958 CUPS_DEFAULT_PRINTADMIN_AUTH
"\n"
1961 cupsFilePuts(temp
, " <Limit All>\n"
1962 " Order deny,allow\n"
1967 for (i
= num_settings
, setting
= settings
; i
> 0; i
--, setting
++)
1968 if (setting
->name
[0] != '_' &&
1969 !cupsGetOption(setting
->name
, cupsd_num_settings
, cupsd_settings
))
1972 * Add this directive to the list of directives we have written...
1975 cupsd_num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1976 cupsd_num_settings
, &cupsd_settings
);
1979 * Write the new value, without indentation since we only support
1980 * setting root directives, not in sections...
1983 cupsFilePrintf(temp
, "%s %s\n", setting
->name
, setting
->value
);
1986 cupsFileClose(cupsd
);
1987 cupsFileClose(temp
);
1990 * Upload the configuration file to the server...
1993 status
= cupsPutFile(http
, "/admin/conf/cupsd.conf", tempfile
);
1995 if (status
== HTTP_CREATED
)
1998 * Updated OK, add the basic settings...
2001 if (debug_logging
>= 0)
2002 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
2003 debug_logging
? "1" : "0",
2004 cupsd_num_settings
, &cupsd_settings
);
2006 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
2007 old_debug_logging
? "1" : "0",
2008 cupsd_num_settings
, &cupsd_settings
);
2010 if (remote_admin
>= 0)
2011 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
2012 remote_admin
? "1" : "0",
2013 cupsd_num_settings
, &cupsd_settings
);
2015 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
2016 old_remote_admin
? "1" : "0",
2017 cupsd_num_settings
, &cupsd_settings
);
2019 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
2020 remote_any
? "1" : "0",
2021 cupsd_num_settings
, &cupsd_settings
);
2023 if (remote_printers
>= 0)
2024 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
2025 remote_printers
? "1" : "0",
2026 cupsd_num_settings
, &cupsd_settings
);
2028 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
2029 old_remote_printers
? "1" : "0",
2030 cupsd_num_settings
, &cupsd_settings
);
2032 if (share_printers
>= 0)
2033 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
2034 share_printers
? "1" : "0",
2035 cupsd_num_settings
, &cupsd_settings
);
2037 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
2038 old_share_printers
? "1" : "0",
2039 cupsd_num_settings
, &cupsd_settings
);
2041 if (user_cancel_any
>= 0)
2042 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
2043 user_cancel_any
? "1" : "0",
2044 cupsd_num_settings
, &cupsd_settings
);
2046 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
2047 old_user_cancel_any
? "1" : "0",
2048 cupsd_num_settings
, &cupsd_settings
);
2051 * Save the new values...
2054 invalidate_cupsd_cache(cg
);
2056 cg
->cupsd_num_settings
= cupsd_num_settings
;
2057 cg
->cupsd_settings
= cupsd_settings
;
2058 cg
->cupsd_update
= time(NULL
);
2060 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
2063 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
2066 * Remote our temp files and return...
2074 return (status
== HTTP_CREATED
);
2079 * 'do_samba_command()' - Do a SAMBA command.
2082 static int /* O - Status of command */
2083 do_samba_command(const char *command
, /* I - Command to run */
2084 const char *address
, /* I - Address for command */
2085 const char *subcmd
, /* I - Sub-command */
2086 const char *authfile
, /* I - Samba authentication file */
2087 FILE *logfile
) /* I - Optional log file */
2090 return (1); /* Always fail on Windows... */
2093 int status
; /* Status of command */
2094 int pid
; /* Process ID of child */
2098 _cupsLangPrintf(logfile
,
2099 _("Running command: %s %s -N -A %s -c \'%s\'\n"),
2100 command
, address
, authfile
, subcmd
);
2102 if ((pid
= fork()) == 0)
2105 * Child goes here, redirect stdin/out/err and execute the command...
2109 open("/dev/null", O_RDONLY
);
2114 dup(fileno(logfile
));
2116 open("/dev/null", O_WRONLY
);
2121 execlp(command
, command
, address
, "-N", "-A", authfile
, "-c", subcmd
,
2130 _cupsLangPrintf(logfile
, _("Unable to run \"%s\": %s\n"),
2131 command
, strerror(errno
));
2136 * Wait for the process to complete...
2139 while (wait(&status
) != pid
);
2143 _cupsLangPuts(logfile
, "\n");
2145 DEBUG_printf(("status=%d\n", status
));
2147 if (WIFEXITED(status
))
2148 return (WEXITSTATUS(status
));
2150 return (-WTERMSIG(status
));
2156 * 'get_cupsd_conf()' - Get the current cupsd.conf file.
2159 static http_status_t
/* O - Status of request */
2161 http_t
*http
, /* I - Connection to server */
2162 _cups_globals_t
*cg
, /* I - Global data */
2163 time_t last_update
, /* I - Last update time for file */
2164 char *name
, /* I - Filename buffer */
2165 int namesize
, /* I - Size of filename buffer */
2166 int *remote
) /* O - Remote file? */
2168 int fd
; /* Temporary file descriptor */
2170 struct stat info
; /* cupsd.conf file information */
2172 http_status_t status
; /* Status of getting cupsd.conf */
2173 char host
[HTTP_MAX_HOST
]; /* Hostname for connection */
2177 * See if we already have the data we need...
2180 httpGetHostname(http
, host
, sizeof(host
));
2182 if (strcasecmp(cg
->cupsd_hostname
, host
))
2183 invalidate_cupsd_cache(cg
);
2185 snprintf(name
, namesize
, "%s/cupsd.conf", cg
->cups_serverroot
);
2189 if (!strcasecmp(host
, "localhost") && !access(name
, R_OK
))
2192 * Read the local file rather than using HTTP...
2195 if (stat(name
, &info
))
2197 char message
[1024]; /* Message string */
2200 snprintf(message
, sizeof(message
),
2201 _cupsLangString(cupsLangDefault(), _("stat of %s failed: %s")),
2202 name
, strerror(errno
));
2203 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
2207 return (HTTP_SERVER_ERROR
);
2209 else if (last_update
&& info
.st_mtime
<= last_update
)
2210 status
= HTTP_NOT_MODIFIED
;
2218 * Read cupsd.conf via a HTTP GET request...
2221 if ((fd
= cupsTempFd(name
, namesize
)) < 0)
2225 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
, 0);
2227 invalidate_cupsd_cache(cg
);
2229 return (HTTP_SERVER_ERROR
);
2234 httpClearFields(http
);
2237 httpSetField(http
, HTTP_FIELD_IF_MODIFIED_SINCE
,
2238 httpGetDateString(last_update
));
2240 status
= cupsGetFd(http
, "/admin/conf/cupsd.conf", fd
);
2244 if (status
!= HTTP_OK
)
2256 * 'invalidate_cupsd_cache()' - Invalidate the cached cupsd.conf settings.
2260 invalidate_cupsd_cache(
2261 _cups_globals_t
*cg
) /* I - Global data */
2263 cupsFreeOptions(cg
->cupsd_num_settings
, cg
->cupsd_settings
);
2265 cg
->cupsd_hostname
[0] = '\0';
2266 cg
->cupsd_update
= 0;
2267 cg
->cupsd_num_settings
= 0;
2268 cg
->cupsd_settings
= NULL
;
2273 * 'write_option()' - Write a CUPS option to a PPD file.
2277 write_option(cups_file_t
*dstfp
, /* I - PPD file */
2278 int order
, /* I - Order dependency */
2279 const char *name
, /* I - Option name */
2280 const char *text
, /* I - Option text */
2281 const char *attrname
, /* I - Attribute name */
2282 ipp_attribute_t
*suppattr
, /* I - IPP -supported attribute */
2283 ipp_attribute_t
*defattr
, /* I - IPP -default attribute */
2284 int defval
, /* I - Default value number */
2285 int valcount
) /* I - Number of values */
2287 int i
; /* Looping var */
2290 cupsFilePrintf(dstfp
, "*JCLOpenUI *%s/%s: PickOne\n"
2291 "*OrderDependency: %d JCLSetup *%s\n",
2292 name
, text
, order
, name
);
2294 if (defattr
->value_tag
== IPP_TAG_INTEGER
)
2297 * Do numeric options with a range or list...
2300 cupsFilePrintf(dstfp
, "*Default%s: %d\n", name
,
2301 defattr
->values
[defval
].integer
);
2303 if (suppattr
->value_tag
== IPP_TAG_RANGE
)
2306 * List each number in the range...
2309 for (i
= suppattr
->values
[0].range
.lower
;
2310 i
<= suppattr
->values
[0].range
.upper
;
2313 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, i
);
2316 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n",
2318 else if (defval
== 0)
2319 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
, i
);
2320 else if (defval
< (valcount
- 1))
2321 cupsFilePrintf(dstfp
, ",%d\"\n", i
);
2323 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", i
);
2329 * List explicit numbers...
2332 for (i
= 0; i
< suppattr
->num_values
; i
++)
2334 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, suppattr
->values
[i
].integer
);
2337 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n", attrname
,
2338 suppattr
->values
[i
].integer
);
2339 else if (defval
== 0)
2340 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
,
2341 suppattr
->values
[i
].integer
);
2342 else if (defval
< (valcount
- 1))
2343 cupsFilePrintf(dstfp
, ",%d\"\n", suppattr
->values
[i
].integer
);
2345 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", suppattr
->values
[i
].integer
);
2352 * Do text options with a list...
2355 cupsFilePrintf(dstfp
, "*Default%s: %s\n", name
,
2356 defattr
->values
[defval
].string
.text
);
2358 for (i
= 0; i
< suppattr
->num_values
; i
++)
2360 cupsFilePrintf(dstfp
, "*%s %s: \"", name
,
2361 suppattr
->values
[i
].string
.text
);
2364 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\n\"\n*End\n", attrname
,
2365 suppattr
->values
[i
].string
.text
);
2366 else if (defval
== 0)
2367 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\"\n", attrname
,
2368 suppattr
->values
[i
].string
.text
);
2369 else if (defval
< (valcount
- 1))
2370 cupsFilePrintf(dstfp
, ",%s\"\n", suppattr
->values
[i
].string
.text
);
2372 cupsFilePrintf(dstfp
, ",%s\n\"\n*End\n",
2373 suppattr
->values
[i
].string
.text
);
2377 cupsFilePrintf(dstfp
, "*JCLCloseUI: *%s\n\n", name
);
2382 * End of "$Id: adminutil.c 7576 2008-05-15 21:05:14Z mike $".