2 * "$Id: adminutil.c 7850 2008-08-20 00:07:25Z mike $"
4 * Administration utility API definitions for CUPS.
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 2001-2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * cupsAdminCreateWindowsPPD() - Create the Windows PPD file for a printer.
20 * cupsAdminExportSamba() - Export a printer to Samba.
21 * cupsAdminGetServerSettings() - Get settings from the server.
22 * cupsAdminSetServerSettings() - Set settings on the server.
23 * do_samba_command() - Do a SAMBA command.
24 * get_cupsd_conf() - Get the current cupsd.conf file.
25 * invalidate_cupsd_cache() - Invalidate the cached cupsd.conf settings.
26 * write_option() - Write a CUPS option to a PPD file.
30 * Include necessary headers...
33 #include "cups-private.h"
34 #include "adminutil.h"
40 # include <sys/wait.h>
48 static int do_samba_command(const char *command
,
50 const char *subcommand
,
53 static http_status_t
get_cupsd_conf(http_t
*http
, _cups_globals_t
*cg
,
54 time_t last_update
, char *name
,
55 int namelen
, int *remote
);
56 static void invalidate_cupsd_cache(_cups_globals_t
*cg
);
57 static void write_option(cups_file_t
*dstfp
, int order
,
58 const char *name
, const char *text
,
60 ipp_attribute_t
*suppattr
,
61 ipp_attribute_t
*defattr
, int defval
,
66 * 'cupsAdminCreateWindowsPPD()' - Create the Windows PPD file for a printer.
68 * @since CUPS 1.2/Mac OS X 10.5@
71 char * /* O - PPD file or NULL */
72 cupsAdminCreateWindowsPPD(
73 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
74 const char *dest
, /* I - Printer or class */
75 char *buffer
, /* I - Filename buffer */
76 int bufsize
) /* I - Size of filename buffer */
78 const char *src
; /* Source PPD filename */
79 cups_file_t
*srcfp
, /* Source PPD file */
80 *dstfp
; /* Destination PPD file */
81 ipp_t
*request
, /* IPP request */
82 *response
; /* IPP response */
83 ipp_attribute_t
*suppattr
, /* IPP -supported attribute */
84 *defattr
; /* IPP -default attribute */
85 cups_lang_t
*language
; /* Current language */
86 char line
[256], /* Line from PPD file */
87 junk
[256], /* Extra junk to throw away */
88 *ptr
, /* Pointer into line */
89 uri
[1024], /* Printer URI */
90 option
[41], /* Option */
91 choice
[41]; /* Choice */
92 int jcloption
, /* In a JCL option? */
93 jclorder
, /* Next JCL order dependency */
94 linenum
; /* Current line number */
95 time_t curtime
; /* Current time */
96 struct tm
*curdate
; /* Current date */
97 static const char * const pattrs
[] = /* Printer attributes we want */
99 "job-hold-until-supported",
100 "job-hold-until-default",
101 "job-sheets-supported",
102 "job-sheets-default",
103 "job-priority-supported",
104 "job-priority-default"
109 * Range check the input...
116 http
= _cupsConnect();
118 if (!http
|| !dest
|| !buffer
|| bufsize
< 2)
122 * Get the PPD file...
125 if ((src
= cupsGetPPD2(http
, dest
)) == NULL
)
129 * Get the supported banner pages, etc. for the printer...
132 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
134 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
135 "localhost", 0, "/printers/%s", dest
);
136 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
137 "printer-uri", NULL
, uri
);
139 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
140 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
144 * Do the request and get back a response...
147 response
= cupsDoRequest(http
, request
, "/");
148 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
155 * Open the original PPD file...
158 if ((srcfp
= cupsFileOpen(src
, "rb")) == NULL
)
162 * Create a temporary output file using the destination buffer...
165 if ((dstfp
= cupsTempFile2(buffer
, bufsize
)) == NULL
)
167 cupsFileClose(srcfp
);
175 * Write a new header explaining that this isn't the original PPD...
178 cupsFilePuts(dstfp
, "*PPD-Adobe: \"4.3\"\n");
180 curtime
= time(NULL
);
181 curdate
= gmtime(&curtime
);
183 cupsFilePrintf(dstfp
, "*%% Modified on %04d%02d%02d%02d%02d%02d+0000 "
184 "for CUPS Windows Driver\n",
185 curdate
->tm_year
+ 1900, curdate
->tm_mon
+ 1, curdate
->tm_mday
,
186 curdate
->tm_hour
, curdate
->tm_min
, curdate
->tm_sec
);
189 * Read the existing PPD file, converting all PJL commands to CUPS
190 * job ticket comments...
196 language
= cupsLangDefault();
198 while (cupsFileGets(srcfp
, line
, sizeof(line
)))
202 if (!strncmp(line
, "*PPD-Adobe:", 11))
205 * Already wrote the PPD header...
210 else if (!strncmp(line
, "*JCLBegin:", 10) ||
211 !strncmp(line
, "*JCLToPSInterpreter:", 20) ||
212 !strncmp(line
, "*JCLEnd:", 8) ||
213 !strncmp(line
, "*Protocols:", 11))
216 * Don't use existing JCL keywords; we'll create our own, below...
219 cupsFilePrintf(dstfp
, "*%% Commented out for CUPS Windows Driver...\n"
220 "*%%%s\n", line
+ 1);
223 else if (!strncmp(line
, "*JCLOpenUI", 10))
226 cupsFilePrintf(dstfp
, "%s\n", line
);
228 else if (!strncmp(line
, "*JCLCloseUI", 11))
231 cupsFilePrintf(dstfp
, "%s\n", line
);
233 else if (jcloption
&& !strncmp(line
, "*OrderDependency:", 17))
235 for (ptr
= line
+ 17; _cups_isspace(*ptr
); ptr
++);
237 ptr
= strchr(ptr
, ' ');
241 cupsFilePrintf(dstfp
, "*OrderDependency: %d%s\n", jclorder
, ptr
);
245 cupsFilePrintf(dstfp
, "%s\n", line
);
247 else if (jcloption
&&
248 strncmp(line
, "*End", 4) &&
249 strncmp(line
, "*Default", 8))
251 if ((ptr
= strchr(line
, ':')) == NULL
)
253 snprintf(line
, sizeof(line
),
254 _cupsLangString(language
, _("Missing value on line %d")),
256 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
, 0);
258 cupsFileClose(srcfp
);
259 cupsFileClose(dstfp
);
269 if ((ptr
= strchr(ptr
, '\"')) == NULL
)
271 snprintf(line
, sizeof(line
),
272 _cupsLangString(language
,
273 _("Missing double quote on line %d")),
275 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
, 0);
277 cupsFileClose(srcfp
);
278 cupsFileClose(dstfp
);
288 if (sscanf(line
, "*%40s%*[ \t]%40[^:/]", option
, choice
) != 2)
290 snprintf(line
, sizeof(line
),
291 _cupsLangString(language
,
292 _("Bad option + choice on line %d")),
294 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
, 0);
296 cupsFileClose(srcfp
);
297 cupsFileClose(dstfp
);
307 if (strchr(ptr
+ 1, '\"') == NULL
)
313 while (cupsFileGets(srcfp
, junk
, sizeof(junk
)) != NULL
)
317 if (!strncmp(junk
, "*End", 4))
322 snprintf(ptr
+ 1, sizeof(line
) - (ptr
- line
+ 1),
323 "%%cupsJobTicket: %s=%s\n\"\n*End", option
, choice
);
325 cupsFilePrintf(dstfp
, "*%% Changed for CUPS Windows Driver...\n%s\n",
329 cupsFilePrintf(dstfp
, "%s\n", line
);
332 cupsFileClose(srcfp
);
337 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, _("Empty PPD file"), 1);
339 cupsFileClose(dstfp
);
348 * Now add the CUPS-specific attributes and options...
351 cupsFilePuts(dstfp
, "\n*% CUPS Job Ticket support and options...\n");
352 cupsFilePuts(dstfp
, "*Protocols: PJL\n");
353 cupsFilePuts(dstfp
, "*JCLBegin: \"%!PS-Adobe-3.0<0A>\"\n");
354 cupsFilePuts(dstfp
, "*JCLToPSInterpreter: \"\"\n");
355 cupsFilePuts(dstfp
, "*JCLEnd: \"\"\n");
357 cupsFilePuts(dstfp
, "\n*OpenGroup: CUPS/CUPS Options\n\n");
359 if ((defattr
= ippFindAttribute(response
, "job-hold-until-default",
360 IPP_TAG_ZERO
)) != NULL
&&
361 (suppattr
= ippFindAttribute(response
, "job-hold-until-supported",
362 IPP_TAG_ZERO
)) != NULL
)
363 write_option(dstfp
, jclorder
++, "cupsJobHoldUntil", "Hold Until",
364 "job-hold-until", suppattr
, defattr
, 0, 1);
366 if ((defattr
= ippFindAttribute(response
, "job-priority-default",
367 IPP_TAG_INTEGER
)) != NULL
&&
368 (suppattr
= ippFindAttribute(response
, "job-priority-supported",
369 IPP_TAG_RANGE
)) != NULL
)
370 write_option(dstfp
, jclorder
++, "cupsJobPriority", "Priority",
371 "job-priority", suppattr
, defattr
, 0, 1);
373 if ((defattr
= ippFindAttribute(response
, "job-sheets-default",
374 IPP_TAG_ZERO
)) != NULL
&&
375 (suppattr
= ippFindAttribute(response
, "job-sheets-supported",
376 IPP_TAG_ZERO
)) != NULL
)
378 write_option(dstfp
, jclorder
++, "cupsJobSheetsStart", "Start Banner",
379 "job-sheets", suppattr
, defattr
, 0, 2);
380 write_option(dstfp
, jclorder
, "cupsJobSheetsEnd", "End Banner",
381 "job-sheets", suppattr
, defattr
, 1, 2);
384 cupsFilePuts(dstfp
, "*CloseGroup: CUPS\n");
385 cupsFileClose(dstfp
);
394 * 'cupsAdminExportSamba()' - Export a printer to Samba.
396 * @since CUPS 1.2/Mac OS X 10.5@
399 int /* O - 1 on success, 0 on failure */
400 cupsAdminExportSamba(
401 const char *dest
, /* I - Destination to export */
402 const char *ppd
, /* I - PPD file */
403 const char *samba_server
, /* I - Samba server */
404 const char *samba_user
, /* I - Samba username */
405 const char *samba_password
, /* I - Samba password */
406 FILE *logfile
) /* I - Log file, if any */
408 int status
; /* Status of Samba commands */
409 int have_drivers
; /* Have drivers? */
410 char file
[1024], /* File to test for */
411 authfile
[1024], /* Temporary authentication file */
412 address
[1024], /* Address for command */
413 subcmd
[1024], /* Sub-command */
414 message
[1024]; /* Error message */
415 cups_file_t
*fp
; /* Authentication file */
416 cups_lang_t
*language
; /* Current language */
417 _cups_globals_t
*cg
= _cupsGlobals();
422 * Range check input...
425 if (!dest
|| !ppd
|| !samba_server
|| !samba_user
|| !samba_password
)
427 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
432 * Create a temporary authentication file for Samba...
435 if ((fp
= cupsTempFile2(authfile
, sizeof(authfile
))) == NULL
)
437 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
, 0);
441 cupsFilePrintf(fp
, "username = %s\n", samba_user
);
442 cupsFilePrintf(fp
, "password = %s\n", samba_password
);
446 * See which drivers are available; the new CUPS v6 and Adobe drivers
447 * depend on the Windows 2k PS driver, so copy that driver first:
458 language
= cupsLangDefault();
460 snprintf(file
, sizeof(file
), "%s/drivers/pscript5.dll", cg
->cups_datadir
);
461 if (!access(file
, 0))
466 * Windows 2k driver is installed; do the smbclient commands needed
467 * to copy the Win2k drivers over...
470 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
472 snprintf(subcmd
, sizeof(subcmd
),
474 "put %s W32X86/%s.ppd;"
475 "put %s/drivers/ps5ui.dll W32X86/ps5ui.dll;"
476 "put %s/drivers/pscript.hlp W32X86/pscript.hlp;"
477 "put %s/drivers/pscript.ntf W32X86/pscript.ntf;"
478 "put %s/drivers/pscript5.dll W32X86/pscript5.dll",
479 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
480 cg
->cups_datadir
, cg
->cups_datadir
);
482 if ((status
= do_samba_command("smbclient", address
, subcmd
,
483 authfile
, logfile
)) != 0)
485 snprintf(message
, sizeof(message
),
486 _cupsLangString(language
,
487 _("Unable to copy Windows 2000 printer "
488 "driver files (%d)")), status
);
490 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
493 _cupsLangPuts(logfile
, message
);
501 * See if we also have the CUPS driver files; if so, use them!
504 snprintf(file
, sizeof(file
), "%s/drivers/cupsps6.dll", cg
->cups_datadir
);
505 if (!access(file
, 0))
508 * Copy the CUPS driver files over...
511 snprintf(subcmd
, sizeof(subcmd
),
512 "put %s/drivers/cups6.ini W32X86/cups6.ini;"
513 "put %s/drivers/cupsps6.dll W32X86/cupsps6.dll;"
514 "put %s/drivers/cupsui6.dll W32X86/cupsui6.dll",
515 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
517 if ((status
= do_samba_command("smbclient", address
, subcmd
,
518 authfile
, logfile
)) != 0)
520 snprintf(message
, sizeof(message
),
521 _cupsLangString(language
,
522 _("Unable to copy CUPS printer driver "
523 "files (%d)")), status
);
525 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
528 _cupsLangPuts(logfile
, message
);
536 * Do the rpcclient command needed for the CUPS drivers...
539 snprintf(subcmd
, sizeof(subcmd
),
540 "adddriver \"Windows NT x86\" \"%s:"
541 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
542 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
543 "cups6.ini,cupsps6.dll,cupsui6.dll\"",
549 * Don't have the CUPS drivers, so just use the standard Windows
553 snprintf(subcmd
, sizeof(subcmd
),
554 "adddriver \"Windows NT x86\" \"%s:"
555 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
556 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
560 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
561 authfile
, logfile
)) != 0)
563 snprintf(message
, sizeof(message
),
564 _cupsLangString(language
,
565 _("Unable to install Windows 2000 printer "
566 "driver files (%d)")), status
);
568 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
571 _cupsLangPuts(logfile
, message
);
580 * See if we have the Win9x PS driver...
583 snprintf(file
, sizeof(file
), "%s/drivers/ADOBEPS4.DRV", cg
->cups_datadir
);
584 if (!access(file
, 0))
589 * Do the smbclient commands needed for the Adobe Win9x drivers...
592 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
594 snprintf(subcmd
, sizeof(subcmd
),
596 "put %s WIN40/%s.PPD;"
597 "put %s/drivers/ADFONTS.MFM WIN40/ADFONTS.MFM;"
598 "put %s/drivers/ADOBEPS4.DRV WIN40/ADOBEPS4.DRV;"
599 "put %s/drivers/ADOBEPS4.HLP WIN40/ADOBEPS4.HLP;"
600 "put %s/drivers/ICONLIB.DLL WIN40/ICONLIB.DLL;"
601 "put %s/drivers/PSMON.DLL WIN40/PSMON.DLL;",
602 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
603 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
605 if ((status
= do_samba_command("smbclient", address
, subcmd
,
606 authfile
, logfile
)) != 0)
608 snprintf(message
, sizeof(message
),
609 _cupsLangString(language
,
610 _("Unable to copy Windows 9x printer "
611 "driver files (%d)")), status
);
613 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
616 _cupsLangPuts(logfile
, message
);
624 * Do the rpcclient commands needed for the Adobe Win9x drivers...
627 snprintf(subcmd
, sizeof(subcmd
),
628 "adddriver \"Windows 4.0\" \"%s:ADOBEPS4.DRV:%s.PPD:NULL:"
629 "ADOBEPS4.HLP:PSMON.DLL:RAW:"
630 "ADOBEPS4.DRV,%s.PPD,ADOBEPS4.HLP,PSMON.DLL,ADFONTS.MFM,"
634 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
635 authfile
, logfile
)) != 0)
637 snprintf(message
, sizeof(message
),
638 _cupsLangString(language
,
639 _("Unable to install Windows 9x printer "
640 "driver files (%d)")), status
);
642 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
645 _cupsLangPuts(logfile
, message
);
654 * See if we have the 64-bit Windows PS driver...
664 snprintf(file
, sizeof(file
), "%s/drivers/x64/pscript5.dll", cg
->cups_datadir
);
665 if (!access(file
, 0))
670 * 64-bit Windows driver is installed; do the smbclient commands needed
671 * to copy the Win64 drivers over...
674 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
676 snprintf(subcmd
, sizeof(subcmd
),
679 "put %s/drivers/x64/ps5ui.dll x64/ps5ui.dll;"
680 "put %s/drivers/x64/pscript.hlp x64/pscript.hlp;"
681 "put %s/drivers/x64/pscript.ntf x64/pscript.ntf;"
682 "put %s/drivers/x64/pscript5.dll x64/pscript5.dll",
683 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
684 cg
->cups_datadir
, cg
->cups_datadir
);
686 if ((status
= do_samba_command("smbclient", address
, subcmd
,
687 authfile
, logfile
)) != 0)
689 snprintf(message
, sizeof(message
),
690 _cupsLangString(language
,
691 _("Unable to copy 64-bit Windows printer "
692 "driver files (%d)")), status
);
694 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
697 _cupsLangPuts(logfile
, message
);
705 * See if we also have the CUPS driver files; if so, use them!
708 snprintf(file
, sizeof(file
), "%s/drivers/x64/cupsps6.dll", cg
->cups_datadir
);
709 if (!access(file
, 0))
712 * Copy the CUPS driver files over...
715 snprintf(subcmd
, sizeof(subcmd
),
716 "put %s/drivers/x64/cups6.ini x64/cups6.ini;"
717 "put %s/drivers/x64/cupsps6.dll x64/cupsps6.dll;"
718 "put %s/drivers/x64/cupsui6.dll x64/cupsui6.dll",
719 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
721 if ((status
= do_samba_command("smbclient", address
, subcmd
,
722 authfile
, logfile
)) != 0)
724 snprintf(message
, sizeof(message
),
725 _cupsLangString(language
,
726 _("Unable to copy 64-bit CUPS printer driver "
727 "files (%d)")), status
);
729 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
732 _cupsLangPuts(logfile
, message
);
740 * Do the rpcclient command needed for the CUPS drivers...
743 snprintf(subcmd
, sizeof(subcmd
),
744 "adddriver \"Windows x64\" \"%s:"
745 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
746 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
747 "cups6.ini,cupsps6.dll,cupsui6.dll\"",
753 * Don't have the CUPS drivers, so just use the standard Windows
757 snprintf(subcmd
, sizeof(subcmd
),
758 "adddriver \"Windows x64\" \"%s:"
759 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
760 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
764 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
765 authfile
, logfile
)) != 0)
767 snprintf(message
, sizeof(message
),
768 _cupsLangString(language
,
769 _("Unable to install Windows 2000 printer "
770 "driver files (%d)")), status
);
772 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
775 _cupsLangPuts(logfile
, message
);
783 if (logfile
&& !(have_drivers
& 1))
787 _cupsLangString(language
,
788 _("No Windows printer drivers are installed")),
792 _cupsLangString(language
,
793 _("Warning, no Windows 2000 printer drivers "
797 _cupsSetError(IPP_NOT_FOUND
, message
, 0);
798 _cupsLangPuts(logfile
, message
);
801 if (have_drivers
== 0)
803 _cupsSetError(IPP_NOT_FOUND
, message
, 0);
811 * Finally, associate the drivers we just added with the queue...
814 snprintf(subcmd
, sizeof(subcmd
), "setdriver %s %s", dest
, dest
);
816 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
817 authfile
, logfile
)) != 0)
819 snprintf(message
, sizeof(message
),
820 _cupsLangString(language
,
821 _("Unable to set Windows printer driver (%d)")),
824 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
827 _cupsLangPuts(logfile
, message
);
841 * 'cupsAdminGetServerSettings()' - Get settings from the server.
843 * The returned settings should be freed with cupsFreeOptions() when
844 * you are done with them.
846 * @since CUPS 1.3/Mac OS X 10.5@
849 int /* O - 1 on success, 0 on failure */
850 cupsAdminGetServerSettings(
851 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
852 int *num_settings
, /* O - Number of settings */
853 cups_option_t
**settings
) /* O - Settings */
855 int i
; /* Looping var */
856 cups_file_t
*cupsd
; /* cupsd.conf file */
857 char cupsdconf
[1024]; /* cupsd.conf filename */
858 int remote
; /* Remote cupsd.conf file? */
859 http_status_t status
; /* Status of getting cupsd.conf */
860 char line
[1024], /* Line from cupsd.conf file */
861 *value
; /* Value on line */
862 cups_option_t
*setting
; /* Current setting */
863 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
867 * Range check input...
873 * See if we are connected to the same server...
879 * Compare the connection hostname, port, and encryption settings to
880 * the cached defaults; these were initialized the first time we
884 if (strcmp(cg
->http
->hostname
, cg
->server
) ||
885 cg
->ipp_port
!= _httpAddrPort(cg
->http
->hostaddr
) ||
886 (cg
->http
->encryption
!= cg
->encryption
&&
887 cg
->http
->encryption
== HTTP_ENCRYPT_NEVER
))
890 * Need to close the current connection because something has changed...
899 * (Re)connect as needed...
904 if ((cg
->http
= _httpCreate(cupsServer(), ippPort(), NULL
,
905 cupsEncryption(), AF_UNSPEC
)) == NULL
)
908 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, NULL
, 0);
910 _cupsSetError(IPP_SERVICE_UNAVAILABLE
,
911 _("Unable to connect to host."), 1);
926 if (!http
|| !num_settings
|| !settings
)
928 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
943 * Get the cupsd.conf file...
946 if ((status
= get_cupsd_conf(http
, cg
, cg
->cupsd_update
, cupsdconf
,
947 sizeof(cupsdconf
), &remote
)) == HTTP_OK
)
949 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
951 char message
[1024]; /* Message string */
954 snprintf(message
, sizeof(message
),
955 _cupsLangString(cupsLangDefault(), _("Open of %s failed: %s")),
956 cupsdconf
, strerror(errno
));
957 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
966 * Read the file, keeping track of what settings are enabled...
969 int remote_access
= 0, /* Remote access allowed? */
970 remote_admin
= 0, /* Remote administration allowed? */
971 remote_any
= 0, /* Remote access from anywhere allowed? */
972 browsing
= 1, /* Browsing enabled? */
973 browse_allow
= 1, /* Browse address set? */
974 browse_address
= 0, /* Browse address set? */
975 cancel_policy
= 1, /* Cancel-job policy set? */
976 debug_logging
= 0; /* LogLevel debug set? */
977 int linenum
= 0, /* Line number in file */
978 in_location
= 0, /* In a location section? */
979 in_policy
= 0, /* In a policy section? */
980 in_cancel_job
= 0, /* In a cancel-job section? */
981 in_admin_location
= 0; /* In the /admin location? */
984 invalidate_cupsd_cache(cg
);
986 cg
->cupsd_update
= time(NULL
);
987 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
989 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
991 if (!value
&& strncmp(line
, "</", 2))
992 value
= line
+ strlen(line
);
994 if ((!strcasecmp(line
, "Port") || !strcasecmp(line
, "Listen")) && value
)
996 char *port
; /* Pointer to port number, if any */
999 if ((port
= strrchr(value
, ':')) != NULL
)
1001 else if (isdigit(*value
& 255))
1004 * Listen on a port number implies remote access...
1011 if (strcasecmp(value
, "localhost") && strcmp(value
, "127.0.0.1")
1014 #endif /* AF_LOCAL */
1016 && strcmp(value
, "[::1]")
1017 #endif /* AF_INET6 */
1021 else if (!strcasecmp(line
, "Browsing"))
1023 browsing
= !strcasecmp(value
, "yes") || !strcasecmp(value
, "on") ||
1024 !strcasecmp(value
, "true");
1026 else if (!strcasecmp(line
, "BrowseAddress"))
1030 else if (!strcasecmp(line
, "BrowseAllow"))
1034 else if (!strcasecmp(line
, "BrowseOrder"))
1036 browse_allow
= !strncasecmp(value
, "deny,", 5);
1038 else if (!strcasecmp(line
, "LogLevel"))
1040 debug_logging
= !strncasecmp(value
, "debug", 5);
1042 else if (!strcasecmp(line
, "<Policy") && !strcasecmp(value
, "default"))
1046 else if (!strcasecmp(line
, "</Policy>"))
1050 else if (!strcasecmp(line
, "<Limit") && in_policy
&& value
)
1053 * See if the policy limit is for the Cancel-Job operation...
1056 char *valptr
; /* Pointer into value */
1061 for (valptr
= value
; *valptr
&& !_cups_isspace(*valptr
); valptr
++);
1066 if (!strcasecmp(value
, "cancel-job") || !strcasecmp(value
, "all"))
1072 for (value
= valptr
; _cups_isspace(*value
); value
++);
1075 else if (!strcasecmp(line
, "</Limit>"))
1079 else if (!strcasecmp(line
, "Require") && in_cancel_job
)
1083 else if (!strcasecmp(line
, "<Location") && value
)
1085 in_admin_location
= !strcasecmp(value
, "/admin");
1088 else if (!strcasecmp(line
, "</Location>"))
1090 in_admin_location
= 0;
1093 else if (!strcasecmp(line
, "Allow") && value
&&
1094 strcasecmp(value
, "localhost") && strcasecmp(value
, "127.0.0.1")
1097 #endif /* AF_LOCAL */
1099 && strcmp(value
, "::1")
1100 #endif /* AF_INET6 */
1103 if (in_admin_location
)
1105 else if (!strcasecmp(value
, "all"))
1108 else if (line
[0] != '<' && !in_location
&& !in_policy
&&
1109 strcasecmp(line
, "Allow") &&
1110 strcasecmp(line
, "AuthType") &&
1111 strcasecmp(line
, "Deny") &&
1112 strcasecmp(line
, "Order") &&
1113 strcasecmp(line
, "Require") &&
1114 strcasecmp(line
, "Satisfy"))
1115 cg
->cupsd_num_settings
= cupsAddOption(line
, value
,
1116 cg
->cupsd_num_settings
,
1117 &(cg
->cupsd_settings
));
1120 cupsFileClose(cupsd
);
1122 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1123 debug_logging
? "1" : "0",
1124 cg
->cupsd_num_settings
,
1125 &(cg
->cupsd_settings
));
1127 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1128 (remote_access
&& remote_admin
) ?
1130 cg
->cupsd_num_settings
,
1131 &(cg
->cupsd_settings
));
1133 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
1134 remote_any
? "1" : "0",
1135 cg
->cupsd_num_settings
,
1136 &(cg
->cupsd_settings
));
1138 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
1139 (browsing
&& browse_allow
) ?
1141 cg
->cupsd_num_settings
,
1142 &(cg
->cupsd_settings
));
1144 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1145 (remote_access
&& browsing
&&
1146 browse_address
) ? "1" : "0",
1147 cg
->cupsd_num_settings
,
1148 &(cg
->cupsd_settings
));
1150 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1151 cancel_policy
? "1" : "0",
1152 cg
->cupsd_num_settings
,
1153 &(cg
->cupsd_settings
));
1155 else if (status
!= HTTP_NOT_MODIFIED
)
1156 invalidate_cupsd_cache(cg
);
1159 * Remove any temporary files and copy the settings array...
1165 for (i
= cg
->cupsd_num_settings
, setting
= cg
->cupsd_settings
;
1168 *num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1169 *num_settings
, settings
);
1171 return (cg
->cupsd_num_settings
> 0);
1176 * 'cupsAdminSetServerSettings()' - Set settings on the server.
1178 * @since CUPS 1.3/Mac OS X 10.5@
1181 int /* O - 1 on success, 0 on failure */
1182 cupsAdminSetServerSettings(
1183 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1184 int num_settings
, /* I - Number of settings */
1185 cups_option_t
*settings
) /* I - Settings */
1187 int i
; /* Looping var */
1188 http_status_t status
; /* GET/PUT status */
1189 const char *server_port_env
; /* SERVER_PORT env var */
1190 int server_port
; /* IPP port for server */
1191 cups_file_t
*cupsd
; /* cupsd.conf file */
1192 char cupsdconf
[1024]; /* cupsd.conf filename */
1193 int remote
; /* Remote cupsd.conf file? */
1194 char tempfile
[1024]; /* Temporary new cupsd.conf */
1195 cups_file_t
*temp
; /* Temporary file */
1196 char line
[1024], /* Line from cupsd.conf file */
1197 *value
; /* Value on line */
1198 int linenum
, /* Line number in file */
1199 in_location
, /* In a location section? */
1200 in_policy
, /* In a policy section? */
1201 in_default_policy
, /* In the default policy section? */
1202 in_cancel_job
, /* In a cancel-job section? */
1203 in_admin_location
, /* In the /admin location? */
1204 in_conf_location
, /* In the /admin/conf location? */
1205 in_root_location
; /* In the / location? */
1206 const char *val
; /* Setting value */
1207 int remote_printers
, /* Show remote printers */
1208 share_printers
, /* Share local printers */
1209 remote_admin
, /* Remote administration allowed? */
1210 remote_any
, /* Remote access from anywhere? */
1211 user_cancel_any
, /* Cancel-job policy set? */
1212 debug_logging
; /* LogLevel debug set? */
1213 int wrote_port_listen
, /* Wrote the port/listen lines? */
1214 wrote_browsing
, /* Wrote the browsing lines? */
1215 wrote_policy
, /* Wrote the policy? */
1216 wrote_loglevel
, /* Wrote the LogLevel line? */
1217 wrote_admin_location
, /* Wrote the /admin location? */
1218 wrote_conf_location
, /* Wrote the /admin/conf location? */
1219 wrote_root_location
; /* Wrote the / location? */
1220 int indent
; /* Indentation */
1221 int cupsd_num_settings
; /* New number of settings */
1222 int old_remote_printers
, /* Show remote printers */
1223 old_share_printers
, /* Share local printers */
1224 old_remote_admin
, /* Remote administration allowed? */
1225 old_user_cancel_any
, /* Cancel-job policy set? */
1226 old_debug_logging
; /* LogLevel debug set? */
1227 cups_option_t
*cupsd_settings
, /* New settings */
1228 *setting
; /* Current setting */
1229 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1233 * Range check input...
1237 http
= _cupsConnect();
1239 if (!http
|| !num_settings
|| !settings
)
1241 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
1247 * Get the cupsd.conf file...
1250 if (get_cupsd_conf(http
, cg
, 0, cupsdconf
, sizeof(cupsdconf
),
1251 &remote
) == HTTP_OK
)
1253 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
1255 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
, 0);
1263 * Get current settings...
1266 if (!cupsAdminGetServerSettings(http
, &cupsd_num_settings
,
1270 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, cupsd_num_settings
,
1271 cupsd_settings
)) != NULL
)
1272 old_debug_logging
= atoi(val
);
1274 old_debug_logging
= 0;
1276 DEBUG_printf(("1cupsAdminSetServerSettings: old debug_logging=%d",
1277 old_debug_logging
));
1279 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, cupsd_num_settings
,
1280 cupsd_settings
)) != NULL
)
1281 old_remote_admin
= atoi(val
);
1283 old_remote_admin
= 0;
1285 DEBUG_printf(("1cupsAdminSetServerSettings: old remote_admin=%d",
1288 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, cupsd_num_settings
,
1289 cupsd_settings
)) != NULL
)
1290 remote_any
= atoi(val
);
1294 DEBUG_printf(("1cupsAdminSetServerSettings: old remote_any=%d",
1297 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
, cupsd_num_settings
,
1298 cupsd_settings
)) != NULL
)
1299 old_remote_printers
= atoi(val
);
1301 old_remote_printers
= 1;
1303 DEBUG_printf(("1cupsAdminSetServerSettings: old remote_printers=%d",
1304 old_remote_printers
));
1306 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, cupsd_num_settings
,
1307 cupsd_settings
)) != NULL
)
1308 old_share_printers
= atoi(val
);
1310 old_share_printers
= 0;
1312 DEBUG_printf(("1cupsAdminSetServerSettings: old share_printers=%d",
1313 old_share_printers
));
1315 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, cupsd_num_settings
,
1316 cupsd_settings
)) != NULL
)
1317 old_user_cancel_any
= atoi(val
);
1319 old_user_cancel_any
= 0;
1321 DEBUG_printf(("1cupsAdminSetServerSettings: old user_cancel_any=%d",
1322 old_user_cancel_any
));
1324 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
1327 * Get basic settings...
1330 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, num_settings
,
1333 debug_logging
= atoi(val
);
1335 if (debug_logging
== old_debug_logging
)
1338 * No change to this setting...
1347 DEBUG_printf(("1cupsAdminSetServerSettings: debug_logging=%d",
1350 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, num_settings
,
1352 remote_any
= atoi(val
);
1354 DEBUG_printf(("1cupsAdminSetServerSettings: remote_any=%d",
1357 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, num_settings
,
1360 remote_admin
= atoi(val
);
1362 if (remote_admin
== old_remote_admin
)
1365 * No change to this setting...
1374 DEBUG_printf(("1cupsAdminSetServerSettings: remote_admin=%d",
1377 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
, num_settings
,
1380 remote_printers
= atoi(val
);
1382 if (remote_printers
== old_remote_printers
)
1385 * No change to this setting...
1388 remote_printers
= -1;
1392 remote_printers
= -1;
1394 DEBUG_printf(("1cupsAdminSetServerSettings: remote_printers=%d",
1397 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, num_settings
,
1400 share_printers
= atoi(val
);
1402 if (share_printers
== old_share_printers
)
1405 * No change to this setting...
1408 share_printers
= -1;
1412 share_printers
= -1;
1414 DEBUG_printf(("1cupsAdminSetServerSettings: share_printers=%d",
1417 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, num_settings
,
1420 user_cancel_any
= atoi(val
);
1422 if (user_cancel_any
== old_user_cancel_any
)
1425 * No change to this setting...
1428 user_cancel_any
= -1;
1432 user_cancel_any
= -1;
1434 DEBUG_printf(("1cupsAdminSetServerSettings: user_cancel_any=%d",
1438 * Create a temporary file for the new cupsd.conf file...
1441 if ((temp
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1443 cupsFileClose(cupsd
);
1448 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
, 0);
1453 * Copy the old file to the new, making changes along the way...
1456 cupsd_num_settings
= 0;
1457 in_admin_location
= 0;
1459 in_conf_location
= 0;
1460 in_default_policy
= 0;
1463 in_root_location
= 0;
1465 wrote_admin_location
= 0;
1467 wrote_conf_location
= 0;
1470 wrote_port_listen
= 0;
1471 wrote_root_location
= 0;
1474 if ((server_port_env
= getenv("SERVER_PORT")) != NULL
)
1476 if ((server_port
= atoi(server_port_env
)) <= 0)
1477 server_port
= ippPort();
1480 server_port
= ippPort();
1482 if (server_port
<= 0)
1483 server_port
= IPP_PORT
;
1485 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
1487 if ((!strcasecmp(line
, "Port") || !strcasecmp(line
, "Listen")) &&
1488 (remote_admin
>= 0 || remote_any
> 0 || share_printers
>= 0))
1490 if (!wrote_port_listen
)
1492 wrote_port_listen
= 1;
1494 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1496 cupsFilePuts(temp
, "# Allow remote access\n");
1497 cupsFilePrintf(temp
, "Port %d\n", server_port
);
1501 cupsFilePuts(temp
, "# Only listen for connections from the local "
1503 cupsFilePrintf(temp
, "Listen localhost:%d\n", server_port
);
1506 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1507 if ((!value
|| strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)) &&
1508 !access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1509 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1510 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1512 else if (value
&& value
[0] == '/'
1513 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1514 && strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)
1515 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1517 cupsFilePrintf(temp
, "Listen %s\n", value
);
1519 else if ((!strcasecmp(line
, "Browsing") ||
1520 !strcasecmp(line
, "BrowseAddress") ||
1521 !strcasecmp(line
, "BrowseAllow") ||
1522 !strcasecmp(line
, "BrowseDeny") ||
1523 !strcasecmp(line
, "BrowseLocalProtocols") ||
1524 !strcasecmp(line
, "BrowseRemoteProtocols") ||
1525 !strcasecmp(line
, "BrowseOrder")) &&
1526 (remote_printers
>= 0 || share_printers
>= 0))
1528 if (!wrote_browsing
)
1530 int new_remote_printers
= (remote_printers
> 0 ||
1531 (remote_printers
== -1 &&
1532 old_remote_printers
> 0));
1533 int new_share_printers
= (share_printers
> 0 ||
1534 (share_printers
== -1 &&
1535 old_share_printers
> 0));
1539 if (new_remote_printers
|| new_share_printers
)
1541 const char *localp
= cupsGetOption("BrowseLocalProtocols",
1542 num_settings
, settings
);
1543 const char *remotep
= cupsGetOption("BrowseRemoteProtocols",
1544 num_settings
, settings
);
1546 if (!localp
|| !localp
[0])
1547 localp
= cupsGetOption("BrowseLocalProtocols", cupsd_num_settings
,
1551 remotep
= cupsGetOption("BrowseRemoteProtocols", cupsd_num_settings
,
1554 if (new_remote_printers
&& new_share_printers
)
1556 "# Enable printer sharing and shared printers.\n");
1557 else if (new_remote_printers
)
1559 "# Show shared printers on the local network.\n");
1562 "# Share local printers on the local network.\n");
1564 cupsFilePuts(temp
, "Browsing On\n");
1565 cupsFilePuts(temp
, "BrowseOrder allow,deny\n");
1567 if (new_remote_printers
)
1569 cupsFilePuts(temp
, "BrowseAllow all\n");
1572 remotep
= CUPS_DEFAULT_BROWSE_REMOTE_PROTOCOLS
;
1574 cupsFilePrintf(temp
, "BrowseRemoteProtocols %s\n", remotep
);
1577 cupsFilePuts(temp
, "BrowseRemoteProtocols\n");
1579 cupsd_num_settings
= cupsAddOption("BrowseRemoteProtocols", remotep
,
1583 if (new_share_printers
)
1585 cupsFilePuts(temp
, "BrowseAddress @LOCAL\n");
1588 localp
= CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS
;
1590 cupsFilePrintf(temp
, "BrowseLocalProtocols %s\n", localp
);
1593 cupsFilePuts(temp
, "BrowseLocalProtocols\n");
1595 cupsd_num_settings
= cupsAddOption("BrowseLocalProtocols", localp
,
1602 "# Disable printer sharing and shared printers.\n");
1603 cupsFilePuts(temp
, "Browsing Off\n");
1607 else if (!strcasecmp(line
, "LogLevel") && debug_logging
>= 0)
1614 "# Show troubleshooting information in error_log.\n");
1615 cupsFilePuts(temp
, "LogLevel debug\n");
1619 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1620 cupsFilePuts(temp
, "LogLevel " CUPS_DEFAULT_LOG_LEVEL
"\n");
1623 else if (!strcasecmp(line
, "<Policy"))
1625 in_default_policy
= !strcasecmp(value
, "default");
1628 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1631 else if (!strcasecmp(line
, "</Policy>"))
1634 if (!wrote_policy
&& in_default_policy
)
1638 if (!user_cancel_any
)
1639 cupsFilePuts(temp
, " # Only the owner or an administrator can "
1641 " <Limit Cancel-Job>\n"
1642 " Order deny,allow\n"
1643 " Require user @OWNER "
1644 CUPS_DEFAULT_PRINTOPERATOR_AUTH
"\n"
1649 in_default_policy
= 0;
1651 cupsFilePuts(temp
, "</Policy>\n");
1653 else if (!strcasecmp(line
, "<Location"))
1657 if (!strcmp(value
, "/admin"))
1658 in_admin_location
= 1;
1659 if (!strcmp(value
, "/admin/conf"))
1660 in_conf_location
= 1;
1661 else if (!strcmp(value
, "/"))
1662 in_root_location
= 1;
1664 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1666 else if (!strcasecmp(line
, "</Location>"))
1670 if (in_admin_location
&& remote_admin
>= 0)
1672 wrote_admin_location
= 1;
1675 cupsFilePuts(temp
, " # Allow remote administration...\n");
1676 else if (remote_admin
== 0)
1677 cupsFilePuts(temp
, " # Restrict access to the admin pages...\n");
1679 cupsFilePuts(temp
, " Order allow,deny\n");
1682 cupsFilePrintf(temp
, " Allow %s\n",
1683 remote_any
> 0 ? "all" : "@LOCAL");
1685 else if (in_conf_location
&& remote_admin
>= 0)
1687 wrote_conf_location
= 1;
1690 cupsFilePuts(temp
, " # Allow remote access to the configuration "
1693 cupsFilePuts(temp
, " # Restrict access to the configuration "
1696 cupsFilePuts(temp
, " Order allow,deny\n");
1699 cupsFilePrintf(temp
, " Allow %s\n",
1700 remote_any
> 0 ? "all" : "@LOCAL");
1702 else if (in_root_location
&&
1703 (remote_admin
>= 0 || remote_any
> 0 || share_printers
>= 0))
1705 wrote_root_location
= 1;
1707 if (remote_admin
> 0 && share_printers
> 0)
1708 cupsFilePuts(temp
, " # Allow shared printing and remote "
1709 "administration...\n");
1710 else if (remote_admin
> 0)
1711 cupsFilePuts(temp
, " # Allow remote administration...\n");
1712 else if (share_printers
> 0)
1713 cupsFilePuts(temp
, " # Allow shared printing...\n");
1714 else if (remote_any
> 0)
1715 cupsFilePuts(temp
, " # Allow remote access...\n");
1717 cupsFilePuts(temp
, " # Restrict access to the server...\n");
1719 cupsFilePuts(temp
, " Order allow,deny\n");
1721 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1722 cupsFilePrintf(temp
, " Allow %s\n",
1723 remote_any
> 0 ? "all" : "@LOCAL");
1726 in_admin_location
= 0;
1727 in_conf_location
= 0;
1728 in_root_location
= 0;
1730 cupsFilePuts(temp
, "</Location>\n");
1732 else if (!strcasecmp(line
, "<Limit"))
1734 if (in_default_policy
)
1737 * See if the policy limit is for the Cancel-Job operation...
1740 char *valptr
; /* Pointer into value */
1743 if (!strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1746 * Don't write anything for this limit section...
1753 cupsFilePrintf(temp
, "%*s%s", indent
, "", line
);
1757 for (valptr
= value
; *valptr
&& !_cups_isspace(*valptr
); valptr
++);
1762 if (!strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1765 * Write everything except for this definition...
1771 cupsFilePrintf(temp
, " %s", value
);
1773 for (value
= valptr
; _cups_isspace(*value
); value
++);
1776 cupsFilePuts(temp
, ">\n");
1780 cupsFilePrintf(temp
, "%*s%s %s>\n", indent
, "", line
, value
);
1784 else if (!strcasecmp(line
, "</Limit>") && in_cancel_job
)
1788 if (in_cancel_job
== 1)
1789 cupsFilePuts(temp
, " </Limit>\n");
1793 if (!user_cancel_any
)
1794 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1796 " <Limit Cancel-Job>\n"
1797 " Order deny,allow\n"
1798 " Require user @OWNER "
1799 CUPS_DEFAULT_PRINTOPERATOR_AUTH
"\n"
1804 else if ((((in_admin_location
|| in_conf_location
|| in_root_location
) &&
1805 (remote_admin
>= 0 || remote_any
> 0)) ||
1806 (in_root_location
&& share_printers
>= 0)) &&
1807 (!strcasecmp(line
, "Allow") || !strcasecmp(line
, "Deny") ||
1808 !strcasecmp(line
, "Order")))
1810 else if (in_cancel_job
== 2)
1812 else if (line
[0] == '<')
1816 cupsFilePrintf(temp
, "%*s%s %s>\n", indent
, "", line
, value
);
1824 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1827 else if (!in_policy
&& !in_location
&&
1828 (val
= cupsGetOption(line
, num_settings
, settings
)) != NULL
)
1831 * Replace this directive's value with the new one...
1834 cupsd_num_settings
= cupsAddOption(line
, val
, cupsd_num_settings
,
1838 * Write the new value in its place, without indentation since we
1839 * only support setting root directives, not in sections...
1842 cupsFilePrintf(temp
, "%s %s\n", line
, val
);
1846 if (!in_policy
&& !in_location
)
1849 * Record the non-policy, non-location directives that we find
1850 * in the server settings, since we cache this info and record it
1851 * in cupsAdminGetServerSettings()...
1854 cupsd_num_settings
= cupsAddOption(line
, value
, cupsd_num_settings
,
1858 cupsFilePrintf(temp
, "%*s%s %s\n", indent
, "", line
, value
);
1861 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1865 * Write any missing info...
1868 if (!wrote_browsing
&& (remote_printers
>= 0 || share_printers
>= 0))
1870 if (remote_printers
> 0 || share_printers
> 0)
1872 if (remote_printers
> 0 && share_printers
> 0)
1873 cupsFilePuts(temp
, "# Enable printer sharing and shared printers.\n");
1874 else if (remote_printers
> 0)
1875 cupsFilePuts(temp
, "# Show shared printers on the local network.\n");
1877 cupsFilePuts(temp
, "# Share local printers on the local network.\n");
1879 cupsFilePuts(temp
, "Browsing On\n");
1880 cupsFilePuts(temp
, "BrowseOrder allow,deny\n");
1882 if (remote_printers
> 0)
1883 cupsFilePuts(temp
, "BrowseAllow all\n");
1885 if (share_printers
> 0)
1886 cupsFilePuts(temp
, "BrowseAddress @LOCAL\n");
1890 cupsFilePuts(temp
, "# Disable printer sharing and shared printers.\n");
1891 cupsFilePuts(temp
, "Browsing Off\n");
1895 if (!wrote_loglevel
&& debug_logging
>= 0)
1899 cupsFilePuts(temp
, "# Show troubleshooting information in error_log.\n");
1900 cupsFilePuts(temp
, "LogLevel debug\n");
1904 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1905 cupsFilePuts(temp
, "LogLevel " CUPS_DEFAULT_LOG_LEVEL
"\n");
1909 if (!wrote_port_listen
&&
1910 (remote_admin
>= 0 || remote_any
> 0 || share_printers
>= 0))
1912 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1914 cupsFilePuts(temp
, "# Allow remote access\n");
1915 cupsFilePrintf(temp
, "Port %d\n", ippPort());
1920 "# Only listen for connections from the local machine.\n");
1921 cupsFilePrintf(temp
, "Listen localhost:%d\n", ippPort());
1924 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1925 if (!access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1926 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1927 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1930 if (!wrote_root_location
&&
1931 (remote_admin
>= 0 || remote_any
> 0 || share_printers
>= 0))
1933 if (remote_admin
> 0 && share_printers
> 0)
1935 "# Allow shared printing and remote administration...\n");
1936 else if (remote_admin
> 0)
1937 cupsFilePuts(temp
, "# Allow remote administration...\n");
1938 else if (share_printers
> 0)
1939 cupsFilePuts(temp
, "# Allow shared printing...\n");
1940 else if (remote_any
> 0)
1941 cupsFilePuts(temp
, "# Allow remote access...\n");
1943 cupsFilePuts(temp
, "# Restrict access to the server...\n");
1945 cupsFilePuts(temp
, "<Location />\n"
1946 " Order allow,deny\n");
1948 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1949 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1951 cupsFilePuts(temp
, "</Location>\n");
1954 if (!wrote_admin_location
&& remote_admin
>= 0)
1957 cupsFilePuts(temp
, "# Allow remote administration...\n");
1959 cupsFilePuts(temp
, "# Restrict access to the admin pages...\n");
1961 cupsFilePuts(temp
, "<Location /admin>\n"
1962 " Order allow,deny\n");
1965 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1967 cupsFilePuts(temp
, "</Location>\n");
1970 if (!wrote_conf_location
&& remote_admin
>= 0)
1974 "# Allow remote access to the configuration files...\n");
1976 cupsFilePuts(temp
, "# Restrict access to the configuration files...\n");
1978 cupsFilePuts(temp
, "<Location /admin/conf>\n"
1979 " AuthType Default\n"
1980 " Require user @SYSTEM\n"
1981 " Order allow,deny\n");
1984 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1986 cupsFilePuts(temp
, "</Location>\n");
1989 if (!wrote_policy
&& user_cancel_any
>= 0)
1991 cupsFilePuts(temp
, "<Policy default>\n"
1992 " # Job-related operations must be done by the owner "
1993 "or an administrator...\n"
1994 " <Limit Send-Document Send-URI Hold-Job Release-Job "
1995 "Restart-Job Purge-Jobs Set-Job-Attributes "
1996 "Create-Job-Subscription Renew-Subscription "
1997 "Cancel-Subscription Get-Notifications Reprocess-Job "
1998 "Cancel-Current-Job Suspend-Current-Job Resume-Job "
2000 " Require user @OWNER @SYSTEM\n"
2001 " Order deny,allow\n"
2003 " # All administration operations require an "
2004 "administrator to authenticate...\n"
2005 " <Limit Pause-Printer Resume-Printer "
2006 "Set-Printer-Attributes Enable-Printer "
2007 "Disable-Printer Pause-Printer-After-Current-Job "
2008 "Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer "
2009 "Activate-Printer Restart-Printer Shutdown-Printer "
2010 "Startup-Printer Promote-Job Schedule-Job-After "
2011 "CUPS-Add-Printer CUPS-Delete-Printer "
2012 "CUPS-Add-Class CUPS-Delete-Class "
2013 "CUPS-Accept-Jobs CUPS-Reject-Jobs "
2014 "CUPS-Set-Default CUPS-Add-Device CUPS-Delete-Device>\n"
2015 " AuthType Default\n"
2016 " Require user @SYSTEM\n"
2017 " Order deny,allow\n"
2020 if (!user_cancel_any
)
2021 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
2023 " <Limit Cancel-Job>\n"
2024 " Order deny,allow\n"
2025 " Require user @OWNER "
2026 CUPS_DEFAULT_PRINTOPERATOR_AUTH
"\n"
2029 cupsFilePuts(temp
, " <Limit All>\n"
2030 " Order deny,allow\n"
2035 for (i
= num_settings
, setting
= settings
; i
> 0; i
--, setting
++)
2036 if (setting
->name
[0] != '_' &&
2037 strcasecmp(setting
->name
, "Listen") &&
2038 strcasecmp(setting
->name
, "Port") &&
2039 !cupsGetOption(setting
->name
, cupsd_num_settings
, cupsd_settings
))
2042 * Add this directive to the list of directives we have written...
2045 cupsd_num_settings
= cupsAddOption(setting
->name
, setting
->value
,
2046 cupsd_num_settings
, &cupsd_settings
);
2049 * Write the new value, without indentation since we only support
2050 * setting root directives, not in sections...
2053 cupsFilePrintf(temp
, "%s %s\n", setting
->name
, setting
->value
);
2056 cupsFileClose(cupsd
);
2057 cupsFileClose(temp
);
2060 * Upload the configuration file to the server...
2063 status
= cupsPutFile(http
, "/admin/conf/cupsd.conf", tempfile
);
2065 if (status
== HTTP_CREATED
)
2068 * Updated OK, add the basic settings...
2071 if (debug_logging
>= 0)
2072 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
2073 debug_logging
? "1" : "0",
2074 cupsd_num_settings
, &cupsd_settings
);
2076 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
2077 old_debug_logging
? "1" : "0",
2078 cupsd_num_settings
, &cupsd_settings
);
2080 if (remote_admin
>= 0)
2081 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
2082 remote_admin
? "1" : "0",
2083 cupsd_num_settings
, &cupsd_settings
);
2085 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
2086 old_remote_admin
? "1" : "0",
2087 cupsd_num_settings
, &cupsd_settings
);
2089 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
2090 remote_any
? "1" : "0",
2091 cupsd_num_settings
, &cupsd_settings
);
2093 if (remote_printers
>= 0)
2094 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
2095 remote_printers
? "1" : "0",
2096 cupsd_num_settings
, &cupsd_settings
);
2098 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
2099 old_remote_printers
? "1" : "0",
2100 cupsd_num_settings
, &cupsd_settings
);
2102 if (share_printers
>= 0)
2103 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
2104 share_printers
? "1" : "0",
2105 cupsd_num_settings
, &cupsd_settings
);
2107 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
2108 old_share_printers
? "1" : "0",
2109 cupsd_num_settings
, &cupsd_settings
);
2111 if (user_cancel_any
>= 0)
2112 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
2113 user_cancel_any
? "1" : "0",
2114 cupsd_num_settings
, &cupsd_settings
);
2116 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
2117 old_user_cancel_any
? "1" : "0",
2118 cupsd_num_settings
, &cupsd_settings
);
2121 * Save the new values...
2124 invalidate_cupsd_cache(cg
);
2126 cg
->cupsd_num_settings
= cupsd_num_settings
;
2127 cg
->cupsd_settings
= cupsd_settings
;
2128 cg
->cupsd_update
= time(NULL
);
2130 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
2133 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
2136 * Remote our temp files and return...
2144 return (status
== HTTP_CREATED
);
2149 * 'do_samba_command()' - Do a SAMBA command.
2152 static int /* O - Status of command */
2153 do_samba_command(const char *command
, /* I - Command to run */
2154 const char *address
, /* I - Address for command */
2155 const char *subcmd
, /* I - Sub-command */
2156 const char *authfile
, /* I - Samba authentication file */
2157 FILE *logfile
) /* I - Optional log file */
2160 return (1); /* Always fail on Windows... */
2163 int status
; /* Status of command */
2164 int pid
; /* Process ID of child */
2168 _cupsLangPrintf(logfile
,
2169 _("Running command: %s %s -N -A %s -c \'%s\'"),
2170 command
, address
, authfile
, subcmd
);
2172 if ((pid
= fork()) == 0)
2175 * Child goes here, redirect stdin/out/err and execute the command...
2178 int fd
= open("/dev/null", O_RDONLY
);
2187 dup2(fileno(logfile
), 1);
2188 else if ((fd
= open("/dev/null", O_WRONLY
)) > 1)
2196 execlp(command
, command
, address
, "-N", "-A", authfile
, "-c", subcmd
,
2205 _cupsLangPrintf(logfile
, _("Unable to run \"%s\": %s"),
2206 command
, strerror(errno
));
2211 * Wait for the process to complete...
2214 while (wait(&status
) != pid
);
2218 _cupsLangPuts(logfile
, "");
2220 DEBUG_printf(("9do_samba_command: status=%d", status
));
2222 if (WIFEXITED(status
))
2223 return (WEXITSTATUS(status
));
2225 return (-WTERMSIG(status
));
2231 * 'get_cupsd_conf()' - Get the current cupsd.conf file.
2234 static http_status_t
/* O - Status of request */
2236 http_t
*http
, /* I - Connection to server */
2237 _cups_globals_t
*cg
, /* I - Global data */
2238 time_t last_update
, /* I - Last update time for file */
2239 char *name
, /* I - Filename buffer */
2240 int namesize
, /* I - Size of filename buffer */
2241 int *remote
) /* O - Remote file? */
2243 int fd
; /* Temporary file descriptor */
2245 struct stat info
; /* cupsd.conf file information */
2247 http_status_t status
; /* Status of getting cupsd.conf */
2248 char host
[HTTP_MAX_HOST
]; /* Hostname for connection */
2252 * See if we already have the data we need...
2255 httpGetHostname(http
, host
, sizeof(host
));
2257 if (strcasecmp(cg
->cupsd_hostname
, host
))
2258 invalidate_cupsd_cache(cg
);
2260 snprintf(name
, namesize
, "%s/cupsd.conf", cg
->cups_serverroot
);
2264 if (!strcasecmp(host
, "localhost") && !access(name
, R_OK
))
2267 * Read the local file rather than using HTTP...
2270 if (stat(name
, &info
))
2272 char message
[1024]; /* Message string */
2275 snprintf(message
, sizeof(message
),
2276 _cupsLangString(cupsLangDefault(), _("stat of %s failed: %s")),
2277 name
, strerror(errno
));
2278 _cupsSetError(IPP_INTERNAL_ERROR
, message
, 0);
2282 return (HTTP_SERVER_ERROR
);
2284 else if (last_update
&& info
.st_mtime
<= last_update
)
2285 status
= HTTP_NOT_MODIFIED
;
2293 * Read cupsd.conf via a HTTP GET request...
2296 if ((fd
= cupsTempFd(name
, namesize
)) < 0)
2300 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
, 0);
2302 invalidate_cupsd_cache(cg
);
2304 return (HTTP_SERVER_ERROR
);
2309 httpClearFields(http
);
2312 httpSetField(http
, HTTP_FIELD_IF_MODIFIED_SINCE
,
2313 httpGetDateString(last_update
));
2315 status
= cupsGetFd(http
, "/admin/conf/cupsd.conf", fd
);
2319 if (status
!= HTTP_OK
)
2331 * 'invalidate_cupsd_cache()' - Invalidate the cached cupsd.conf settings.
2335 invalidate_cupsd_cache(
2336 _cups_globals_t
*cg
) /* I - Global data */
2338 cupsFreeOptions(cg
->cupsd_num_settings
, cg
->cupsd_settings
);
2340 cg
->cupsd_hostname
[0] = '\0';
2341 cg
->cupsd_update
= 0;
2342 cg
->cupsd_num_settings
= 0;
2343 cg
->cupsd_settings
= NULL
;
2348 * 'write_option()' - Write a CUPS option to a PPD file.
2352 write_option(cups_file_t
*dstfp
, /* I - PPD file */
2353 int order
, /* I - Order dependency */
2354 const char *name
, /* I - Option name */
2355 const char *text
, /* I - Option text */
2356 const char *attrname
, /* I - Attribute name */
2357 ipp_attribute_t
*suppattr
, /* I - IPP -supported attribute */
2358 ipp_attribute_t
*defattr
, /* I - IPP -default attribute */
2359 int defval
, /* I - Default value number */
2360 int valcount
) /* I - Number of values */
2362 int i
; /* Looping var */
2365 cupsFilePrintf(dstfp
, "*JCLOpenUI *%s/%s: PickOne\n"
2366 "*OrderDependency: %d JCLSetup *%s\n",
2367 name
, text
, order
, name
);
2369 if (defattr
->value_tag
== IPP_TAG_INTEGER
)
2372 * Do numeric options with a range or list...
2375 cupsFilePrintf(dstfp
, "*Default%s: %d\n", name
,
2376 defattr
->values
[defval
].integer
);
2378 if (suppattr
->value_tag
== IPP_TAG_RANGE
)
2381 * List each number in the range...
2384 for (i
= suppattr
->values
[0].range
.lower
;
2385 i
<= suppattr
->values
[0].range
.upper
;
2388 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, i
);
2391 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n",
2393 else if (defval
== 0)
2394 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
, i
);
2395 else if (defval
< (valcount
- 1))
2396 cupsFilePrintf(dstfp
, ",%d\"\n", i
);
2398 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", i
);
2404 * List explicit numbers...
2407 for (i
= 0; i
< suppattr
->num_values
; i
++)
2409 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, suppattr
->values
[i
].integer
);
2412 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n", attrname
,
2413 suppattr
->values
[i
].integer
);
2414 else if (defval
== 0)
2415 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
,
2416 suppattr
->values
[i
].integer
);
2417 else if (defval
< (valcount
- 1))
2418 cupsFilePrintf(dstfp
, ",%d\"\n", suppattr
->values
[i
].integer
);
2420 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", suppattr
->values
[i
].integer
);
2427 * Do text options with a list...
2430 cupsFilePrintf(dstfp
, "*Default%s: %s\n", name
,
2431 defattr
->values
[defval
].string
.text
);
2433 for (i
= 0; i
< suppattr
->num_values
; i
++)
2435 cupsFilePrintf(dstfp
, "*%s %s: \"", name
,
2436 suppattr
->values
[i
].string
.text
);
2439 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\n\"\n*End\n", attrname
,
2440 suppattr
->values
[i
].string
.text
);
2441 else if (defval
== 0)
2442 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\"\n", attrname
,
2443 suppattr
->values
[i
].string
.text
);
2444 else if (defval
< (valcount
- 1))
2445 cupsFilePrintf(dstfp
, ",%s\"\n", suppattr
->values
[i
].string
.text
);
2447 cupsFilePrintf(dstfp
, ",%s\n\"\n*End\n",
2448 suppattr
->values
[i
].string
.text
);
2452 cupsFilePrintf(dstfp
, "*JCLCloseUI: *%s\n\n", name
);
2457 * End of "$Id: adminutil.c 7850 2008-08-20 00:07:25Z mike $".