4 * Administration utility API definitions for CUPS.
6 * Copyright 2007-2014 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 * Include necessary headers...
22 #include "cups-private.h"
23 #include "adminutil.h"
29 # include <sys/wait.h>
37 static int do_samba_command(const char *command
,
39 const char *subcommand
,
42 static http_status_t
get_cupsd_conf(http_t
*http
, _cups_globals_t
*cg
,
43 time_t last_update
, char *name
,
44 int namelen
, int *remote
);
45 static void invalidate_cupsd_cache(_cups_globals_t
*cg
);
46 static void write_option(cups_file_t
*dstfp
, int order
,
47 const char *name
, const char *text
,
49 ipp_attribute_t
*suppattr
,
50 ipp_attribute_t
*defattr
, int defval
,
55 * 'cupsAdminCreateWindowsPPD()' - Create the Windows PPD file for a printer.
60 char * /* O - PPD file or NULL */
61 cupsAdminCreateWindowsPPD(
62 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
63 const char *dest
, /* I - Printer or class */
64 char *buffer
, /* I - Filename buffer */
65 int bufsize
) /* I - Size of filename buffer */
67 const char *src
; /* Source PPD filename */
68 cups_file_t
*srcfp
, /* Source PPD file */
69 *dstfp
; /* Destination PPD file */
70 ipp_t
*request
, /* IPP request */
71 *response
; /* IPP response */
72 ipp_attribute_t
*suppattr
, /* IPP -supported attribute */
73 *defattr
; /* IPP -default attribute */
74 cups_lang_t
*language
; /* Current language */
75 char line
[256], /* Line from PPD file */
76 junk
[256], /* Extra junk to throw away */
77 *ptr
, /* Pointer into line */
78 uri
[1024], /* Printer URI */
79 option
[41], /* Option */
80 choice
[41]; /* Choice */
81 int jcloption
, /* In a JCL option? */
82 jclorder
, /* Next JCL order dependency */
83 linenum
; /* Current line number */
84 time_t curtime
; /* Current time */
85 struct tm
*curdate
; /* Current date */
86 static const char * const pattrs
[] = /* Printer attributes we want */
88 "job-hold-until-supported",
89 "job-hold-until-default",
90 "job-sheets-supported",
92 "job-priority-supported",
93 "job-priority-default"
98 * Range check the input...
105 http
= _cupsConnect();
107 if (!http
|| !dest
|| !buffer
|| bufsize
< 2)
111 * Get the PPD file...
114 if ((src
= cupsGetPPD2(http
, dest
)) == NULL
)
118 * Get the supported banner pages, etc. for the printer...
121 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
123 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
124 "localhost", 0, "/printers/%s", dest
);
125 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
126 "printer-uri", NULL
, uri
);
128 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
129 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
133 * Do the request and get back a response...
136 response
= cupsDoRequest(http
, request
, "/");
137 if (!response
|| cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
144 * Open the original PPD file...
147 if ((srcfp
= cupsFileOpen(src
, "rb")) == NULL
)
151 * Create a temporary output file using the destination buffer...
154 if ((dstfp
= cupsTempFile2(buffer
, bufsize
)) == NULL
)
156 cupsFileClose(srcfp
);
164 * Write a new header explaining that this isn't the original PPD...
167 cupsFilePuts(dstfp
, "*PPD-Adobe: \"4.3\"\n");
169 curtime
= time(NULL
);
170 curdate
= gmtime(&curtime
);
172 cupsFilePrintf(dstfp
, "*%% Modified on %04d%02d%02d%02d%02d%02d+0000 "
173 "for CUPS Windows Driver\n",
174 curdate
->tm_year
+ 1900, curdate
->tm_mon
+ 1, curdate
->tm_mday
,
175 curdate
->tm_hour
, curdate
->tm_min
, curdate
->tm_sec
);
178 * Read the existing PPD file, converting all PJL commands to CUPS
179 * job ticket comments...
185 language
= cupsLangDefault();
187 while (cupsFileGets(srcfp
, line
, sizeof(line
)))
191 if (!strncmp(line
, "*PPD-Adobe:", 11))
194 * Already wrote the PPD header...
199 else if (!strncmp(line
, "*JCLBegin:", 10) ||
200 !strncmp(line
, "*JCLToPSInterpreter:", 20) ||
201 !strncmp(line
, "*JCLEnd:", 8) ||
202 !strncmp(line
, "*Protocols:", 11))
205 * Don't use existing JCL keywords; we'll create our own, below...
208 cupsFilePrintf(dstfp
, "*%% Commented out for CUPS Windows Driver...\n"
209 "*%%%s\n", line
+ 1);
212 else if (!strncmp(line
, "*JCLOpenUI", 10))
215 cupsFilePrintf(dstfp
, "%s\n", line
);
217 else if (!strncmp(line
, "*JCLCloseUI", 11))
220 cupsFilePrintf(dstfp
, "%s\n", line
);
222 else if (jcloption
&& !strncmp(line
, "*OrderDependency:", 17))
224 for (ptr
= line
+ 17; _cups_isspace(*ptr
); ptr
++);
226 ptr
= strchr(ptr
, ' ');
230 cupsFilePrintf(dstfp
, "*OrderDependency: %d%s\n", jclorder
, ptr
);
234 cupsFilePrintf(dstfp
, "%s\n", line
);
236 else if (jcloption
&&
237 strncmp(line
, "*End", 4) &&
238 strncmp(line
, "*Default", 8))
240 if ((ptr
= strchr(line
, ':')) == NULL
)
242 snprintf(line
, sizeof(line
),
243 _cupsLangString(language
, _("Missing value on line %d.")),
245 _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR
, line
, 0);
247 cupsFileClose(srcfp
);
248 cupsFileClose(dstfp
);
258 if ((ptr
= strchr(ptr
, '\"')) == NULL
)
260 snprintf(line
, sizeof(line
),
261 _cupsLangString(language
,
262 _("Missing double quote on line %d.")),
264 _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR
, line
, 0);
266 cupsFileClose(srcfp
);
267 cupsFileClose(dstfp
);
277 if (sscanf(line
, "*%40s%*[ \t]%40[^:/]", option
, choice
) != 2)
279 snprintf(line
, sizeof(line
),
280 _cupsLangString(language
,
281 _("Bad option + choice on line %d.")),
283 _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR
, line
, 0);
285 cupsFileClose(srcfp
);
286 cupsFileClose(dstfp
);
296 if (strchr(ptr
+ 1, '\"') == NULL
)
302 while (cupsFileGets(srcfp
, junk
, sizeof(junk
)) != NULL
)
306 if (!strncmp(junk
, "*End", 4))
311 snprintf(ptr
+ 1, sizeof(line
) - (size_t)(ptr
- line
+ 1),
312 "%%cupsJobTicket: %s=%s\n\"\n*End", option
, choice
);
314 cupsFilePrintf(dstfp
, "*%% Changed for CUPS Windows Driver...\n%s\n",
318 cupsFilePrintf(dstfp
, "%s\n", line
);
321 cupsFileClose(srcfp
);
326 _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR
, _("Empty PPD file."), 1);
328 cupsFileClose(dstfp
);
337 * Now add the CUPS-specific attributes and options...
340 cupsFilePuts(dstfp
, "\n*% CUPS Job Ticket support and options...\n");
341 cupsFilePuts(dstfp
, "*Protocols: PJL\n");
342 cupsFilePuts(dstfp
, "*JCLBegin: \"%!PS-Adobe-3.0<0A>\"\n");
343 cupsFilePuts(dstfp
, "*JCLToPSInterpreter: \"\"\n");
344 cupsFilePuts(dstfp
, "*JCLEnd: \"\"\n");
346 cupsFilePuts(dstfp
, "\n*OpenGroup: CUPS/CUPS Options\n\n");
348 if ((defattr
= ippFindAttribute(response
, "job-hold-until-default",
349 IPP_TAG_ZERO
)) != NULL
&&
350 (suppattr
= ippFindAttribute(response
, "job-hold-until-supported",
351 IPP_TAG_ZERO
)) != NULL
)
352 write_option(dstfp
, jclorder
++, "cupsJobHoldUntil", "Hold Until",
353 "job-hold-until", suppattr
, defattr
, 0, 1);
355 if ((defattr
= ippFindAttribute(response
, "job-priority-default",
356 IPP_TAG_INTEGER
)) != NULL
&&
357 (suppattr
= ippFindAttribute(response
, "job-priority-supported",
358 IPP_TAG_RANGE
)) != NULL
)
359 write_option(dstfp
, jclorder
++, "cupsJobPriority", "Priority",
360 "job-priority", suppattr
, defattr
, 0, 1);
362 if ((defattr
= ippFindAttribute(response
, "job-sheets-default",
363 IPP_TAG_ZERO
)) != NULL
&&
364 (suppattr
= ippFindAttribute(response
, "job-sheets-supported",
365 IPP_TAG_ZERO
)) != NULL
)
367 write_option(dstfp
, jclorder
++, "cupsJobSheetsStart", "Start Banner",
368 "job-sheets", suppattr
, defattr
, 0, 2);
369 write_option(dstfp
, jclorder
, "cupsJobSheetsEnd", "End Banner",
370 "job-sheets", suppattr
, defattr
, 1, 2);
373 cupsFilePuts(dstfp
, "*CloseGroup: CUPS\n");
374 cupsFileClose(dstfp
);
383 * 'cupsAdminExportSamba()' - Export a printer to Samba.
388 int /* O - 1 on success, 0 on failure */
389 cupsAdminExportSamba(
390 const char *dest
, /* I - Destination to export */
391 const char *ppd
, /* I - PPD file */
392 const char *samba_server
, /* I - Samba server */
393 const char *samba_user
, /* I - Samba username */
394 const char *samba_password
, /* I - Samba password */
395 FILE *logfile
) /* I - Log file, if any */
397 int status
; /* Status of Samba commands */
398 int have_drivers
; /* Have drivers? */
399 char file
[1024], /* File to test for */
400 authfile
[1024], /* Temporary authentication file */
401 address
[1024], /* Address for command */
402 subcmd
[1024], /* Sub-command */
403 message
[1024]; /* Error message */
404 cups_file_t
*fp
; /* Authentication file */
405 cups_lang_t
*language
; /* Current language */
406 _cups_globals_t
*cg
= _cupsGlobals();
411 * Range check input...
414 if (!dest
|| !ppd
|| !samba_server
|| !samba_user
|| !samba_password
)
416 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
421 * Create a temporary authentication file for Samba...
424 if ((fp
= cupsTempFile2(authfile
, sizeof(authfile
))) == NULL
)
426 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
430 cupsFilePrintf(fp
, "username = %s\n", samba_user
);
431 cupsFilePrintf(fp
, "password = %s\n", samba_password
);
435 * See which drivers are available; the new CUPS v6 and Adobe drivers
436 * depend on the Windows 2k PS driver, so copy that driver first:
447 language
= cupsLangDefault();
449 snprintf(file
, sizeof(file
), "%s/drivers/pscript5.dll", cg
->cups_datadir
);
450 if (!access(file
, 0))
455 * Windows 2k driver is installed; do the smbclient commands needed
456 * to copy the Win2k drivers over...
459 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
461 snprintf(subcmd
, sizeof(subcmd
),
463 "put %s W32X86/%s.ppd;"
464 "put %s/drivers/ps5ui.dll W32X86/ps5ui.dll;"
465 "put %s/drivers/pscript.hlp W32X86/pscript.hlp;"
466 "put %s/drivers/pscript.ntf W32X86/pscript.ntf;"
467 "put %s/drivers/pscript5.dll W32X86/pscript5.dll",
468 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
469 cg
->cups_datadir
, cg
->cups_datadir
);
471 if ((status
= do_samba_command("smbclient", address
, subcmd
,
472 authfile
, logfile
)) != 0)
474 snprintf(message
, sizeof(message
),
475 _cupsLangString(language
,
476 _("Unable to copy Windows 2000 printer "
477 "driver files (%d).")), status
);
479 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
482 _cupsLangPuts(logfile
, message
);
490 * See if we also have the CUPS driver files; if so, use them!
493 snprintf(file
, sizeof(file
), "%s/drivers/cupsps6.dll", cg
->cups_datadir
);
494 if (!access(file
, 0))
497 * Copy the CUPS driver files over...
500 snprintf(subcmd
, sizeof(subcmd
),
501 "put %s/drivers/cups6.ini W32X86/cups6.ini;"
502 "put %s/drivers/cupsps6.dll W32X86/cupsps6.dll;"
503 "put %s/drivers/cupsui6.dll W32X86/cupsui6.dll",
504 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
506 if ((status
= do_samba_command("smbclient", address
, subcmd
,
507 authfile
, logfile
)) != 0)
509 snprintf(message
, sizeof(message
),
510 _cupsLangString(language
,
511 _("Unable to copy CUPS printer driver "
512 "files (%d).")), status
);
514 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
517 _cupsLangPuts(logfile
, message
);
525 * Do the rpcclient command needed for the CUPS drivers...
528 snprintf(subcmd
, sizeof(subcmd
),
529 "adddriver \"Windows NT x86\" \"%s:"
530 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
531 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
532 "cups6.ini,cupsps6.dll,cupsui6.dll\"",
538 * Don't have the CUPS drivers, so just use the standard Windows
542 snprintf(subcmd
, sizeof(subcmd
),
543 "adddriver \"Windows NT x86\" \"%s:"
544 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
545 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
549 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
550 authfile
, logfile
)) != 0)
552 snprintf(message
, sizeof(message
),
553 _cupsLangString(language
,
554 _("Unable to install Windows 2000 printer "
555 "driver files (%d).")), status
);
557 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
560 _cupsLangPuts(logfile
, message
);
569 * See if we have the Win9x PS driver...
572 snprintf(file
, sizeof(file
), "%s/drivers/ADOBEPS4.DRV", cg
->cups_datadir
);
573 if (!access(file
, 0))
578 * Do the smbclient commands needed for the Adobe Win9x drivers...
581 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
583 snprintf(subcmd
, sizeof(subcmd
),
585 "put %s WIN40/%s.PPD;"
586 "put %s/drivers/ADFONTS.MFM WIN40/ADFONTS.MFM;"
587 "put %s/drivers/ADOBEPS4.DRV WIN40/ADOBEPS4.DRV;"
588 "put %s/drivers/ADOBEPS4.HLP WIN40/ADOBEPS4.HLP;"
589 "put %s/drivers/ICONLIB.DLL WIN40/ICONLIB.DLL;"
590 "put %s/drivers/PSMON.DLL WIN40/PSMON.DLL;",
591 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
592 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
594 if ((status
= do_samba_command("smbclient", address
, subcmd
,
595 authfile
, logfile
)) != 0)
597 snprintf(message
, sizeof(message
),
598 _cupsLangString(language
,
599 _("Unable to copy Windows 9x printer "
600 "driver files (%d).")), status
);
602 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
605 _cupsLangPuts(logfile
, message
);
613 * Do the rpcclient commands needed for the Adobe Win9x drivers...
616 snprintf(subcmd
, sizeof(subcmd
),
617 "adddriver \"Windows 4.0\" \"%s:ADOBEPS4.DRV:%s.PPD:NULL:"
618 "ADOBEPS4.HLP:PSMON.DLL:RAW:"
619 "ADOBEPS4.DRV,%s.PPD,ADOBEPS4.HLP,PSMON.DLL,ADFONTS.MFM,"
623 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
624 authfile
, logfile
)) != 0)
626 snprintf(message
, sizeof(message
),
627 _cupsLangString(language
,
628 _("Unable to install Windows 9x printer "
629 "driver files (%d).")), status
);
631 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
634 _cupsLangPuts(logfile
, message
);
643 * See if we have the 64-bit Windows PS driver...
653 snprintf(file
, sizeof(file
), "%s/drivers/x64/pscript5.dll", cg
->cups_datadir
);
654 if (!access(file
, 0))
659 * 64-bit Windows driver is installed; do the smbclient commands needed
660 * to copy the Win64 drivers over...
663 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
665 snprintf(subcmd
, sizeof(subcmd
),
668 "put %s/drivers/x64/ps5ui.dll x64/ps5ui.dll;"
669 "put %s/drivers/x64/pscript.hlp x64/pscript.hlp;"
670 "put %s/drivers/x64/pscript.ntf x64/pscript.ntf;"
671 "put %s/drivers/x64/pscript5.dll x64/pscript5.dll",
672 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
673 cg
->cups_datadir
, cg
->cups_datadir
);
675 if ((status
= do_samba_command("smbclient", address
, subcmd
,
676 authfile
, logfile
)) != 0)
678 snprintf(message
, sizeof(message
),
679 _cupsLangString(language
,
680 _("Unable to copy 64-bit Windows printer "
681 "driver files (%d).")), status
);
683 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
686 _cupsLangPuts(logfile
, message
);
694 * See if we also have the CUPS driver files; if so, use them!
697 snprintf(file
, sizeof(file
), "%s/drivers/x64/cupsps6.dll", cg
->cups_datadir
);
698 if (!access(file
, 0))
701 * Copy the CUPS driver files over...
704 snprintf(subcmd
, sizeof(subcmd
),
705 "put %s/drivers/x64/cups6.ini x64/cups6.ini;"
706 "put %s/drivers/x64/cupsps6.dll x64/cupsps6.dll;"
707 "put %s/drivers/x64/cupsui6.dll x64/cupsui6.dll",
708 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
710 if ((status
= do_samba_command("smbclient", address
, subcmd
,
711 authfile
, logfile
)) != 0)
713 snprintf(message
, sizeof(message
),
714 _cupsLangString(language
,
715 _("Unable to copy 64-bit CUPS printer driver "
716 "files (%d).")), status
);
718 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
721 _cupsLangPuts(logfile
, message
);
729 * Do the rpcclient command needed for the CUPS drivers...
732 snprintf(subcmd
, sizeof(subcmd
),
733 "adddriver \"Windows x64\" \"%s:"
734 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
735 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
736 "cups6.ini,cupsps6.dll,cupsui6.dll\"",
742 * Don't have the CUPS drivers, so just use the standard Windows
746 snprintf(subcmd
, sizeof(subcmd
),
747 "adddriver \"Windows x64\" \"%s:"
748 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
749 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
753 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
754 authfile
, logfile
)) != 0)
756 snprintf(message
, sizeof(message
),
757 _cupsLangString(language
,
758 _("Unable to install Windows 2000 printer "
759 "driver files (%d).")), status
);
761 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
764 _cupsLangPuts(logfile
, message
);
772 if (logfile
&& !(have_drivers
& 1))
776 _cupsLangString(language
,
777 _("No Windows printer drivers are installed.")),
781 _cupsLangString(language
,
782 _("Warning, no Windows 2000 printer drivers "
786 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, message
, 0);
787 _cupsLangPuts(logfile
, message
);
790 if (have_drivers
== 0)
792 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, message
, 0);
800 * Finally, associate the drivers we just added with the queue...
803 snprintf(subcmd
, sizeof(subcmd
), "setdriver %s %s", dest
, dest
);
805 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
806 authfile
, logfile
)) != 0)
808 snprintf(message
, sizeof(message
),
809 _cupsLangString(language
,
810 _("Unable to set Windows printer driver (%d).")),
813 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
816 _cupsLangPuts(logfile
, message
);
830 * 'cupsAdminGetServerSettings()' - Get settings from the server.
832 * The returned settings should be freed with cupsFreeOptions() when
833 * you are done with them.
835 * @since CUPS 1.3/OS X 10.5@
838 int /* O - 1 on success, 0 on failure */
839 cupsAdminGetServerSettings(
840 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
841 int *num_settings
, /* O - Number of settings */
842 cups_option_t
**settings
) /* O - Settings */
844 int i
; /* Looping var */
845 cups_file_t
*cupsd
; /* cupsd.conf file */
846 char cupsdconf
[1024]; /* cupsd.conf filename */
847 int remote
; /* Remote cupsd.conf file? */
848 http_status_t status
; /* Status of getting cupsd.conf */
849 char line
[1024], /* Line from cupsd.conf file */
850 *value
; /* Value on line */
851 cups_option_t
*setting
; /* Current setting */
852 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
856 * Range check input...
862 * See if we are connected to the same server...
868 * Compare the connection hostname, port, and encryption settings to
869 * the cached defaults; these were initialized the first time we
873 if (strcmp(cg
->http
->hostname
, cg
->server
) ||
874 cg
->ipp_port
!= httpAddrPort(cg
->http
->hostaddr
) ||
875 (cg
->http
->encryption
!= cg
->encryption
&&
876 cg
->http
->encryption
== HTTP_ENCRYPTION_NEVER
))
879 * Need to close the current connection because something has changed...
888 * (Re)connect as needed...
893 if ((cg
->http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
,
894 cupsEncryption(), 1, 0, NULL
)) == NULL
)
897 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
, NULL
, 0);
899 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
,
900 _("Unable to connect to host."), 1);
915 if (!http
|| !num_settings
|| !settings
)
917 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
932 * Get the cupsd.conf file...
935 if ((status
= get_cupsd_conf(http
, cg
, cg
->cupsd_update
, cupsdconf
,
936 sizeof(cupsdconf
), &remote
)) == HTTP_STATUS_OK
)
938 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
940 char message
[1024]; /* Message string */
943 snprintf(message
, sizeof(message
),
944 _cupsLangString(cupsLangDefault(), _("Open of %s failed: %s")),
945 cupsdconf
, strerror(errno
));
946 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
955 * Read the file, keeping track of what settings are enabled...
958 int remote_access
= 0, /* Remote access allowed? */
959 remote_admin
= 0, /* Remote administration allowed? */
960 remote_any
= 0, /* Remote access from anywhere allowed? */
961 browsing
= 1, /* Browsing enabled? */
962 cancel_policy
= 1, /* Cancel-job policy set? */
963 debug_logging
= 0; /* LogLevel debug set? */
964 int linenum
= 0, /* Line number in file */
965 in_location
= 0, /* In a location section? */
966 in_policy
= 0, /* In a policy section? */
967 in_cancel_job
= 0, /* In a cancel-job section? */
968 in_admin_location
= 0; /* In the /admin location? */
971 invalidate_cupsd_cache(cg
);
973 cg
->cupsd_update
= time(NULL
);
974 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
976 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
978 if (!value
&& strncmp(line
, "</", 2))
979 value
= line
+ strlen(line
);
981 if ((!_cups_strcasecmp(line
, "Port") || !_cups_strcasecmp(line
, "Listen")) && value
)
983 char *port
; /* Pointer to port number, if any */
986 if ((port
= strrchr(value
, ':')) != NULL
)
988 else if (isdigit(*value
& 255))
991 * Listen on a port number implies remote access...
998 if (_cups_strcasecmp(value
, "localhost") && strcmp(value
, "127.0.0.1")
1001 #endif /* AF_LOCAL */
1003 && strcmp(value
, "[::1]")
1004 #endif /* AF_INET6 */
1008 else if (!_cups_strcasecmp(line
, "Browsing"))
1010 browsing
= !_cups_strcasecmp(value
, "yes") ||
1011 !_cups_strcasecmp(value
, "on") ||
1012 !_cups_strcasecmp(value
, "true");
1014 else if (!_cups_strcasecmp(line
, "LogLevel"))
1016 debug_logging
= !_cups_strncasecmp(value
, "debug", 5);
1018 else if (!_cups_strcasecmp(line
, "<Policy") &&
1019 !_cups_strcasecmp(value
, "default"))
1023 else if (!_cups_strcasecmp(line
, "</Policy>"))
1027 else if (!_cups_strcasecmp(line
, "<Limit") && in_policy
&& value
)
1030 * See if the policy limit is for the Cancel-Job operation...
1033 char *valptr
; /* Pointer into value */
1038 for (valptr
= value
; *valptr
&& !_cups_isspace(*valptr
); valptr
++);
1043 if (!_cups_strcasecmp(value
, "cancel-job") ||
1044 !_cups_strcasecmp(value
, "all"))
1050 for (value
= valptr
; _cups_isspace(*value
); value
++);
1053 else if (!_cups_strcasecmp(line
, "</Limit>"))
1057 else if (!_cups_strcasecmp(line
, "Require") && in_cancel_job
)
1061 else if (!_cups_strcasecmp(line
, "<Location") && value
)
1063 in_admin_location
= !_cups_strcasecmp(value
, "/admin");
1066 else if (!_cups_strcasecmp(line
, "</Location>"))
1068 in_admin_location
= 0;
1071 else if (!_cups_strcasecmp(line
, "Allow") && value
&&
1072 _cups_strcasecmp(value
, "localhost") &&
1073 _cups_strcasecmp(value
, "127.0.0.1")
1076 #endif /* AF_LOCAL */
1078 && strcmp(value
, "::1")
1079 #endif /* AF_INET6 */
1082 if (in_admin_location
)
1084 else if (!_cups_strcasecmp(value
, "all"))
1087 else if (line
[0] != '<' && !in_location
&& !in_policy
&&
1088 _cups_strcasecmp(line
, "Allow") &&
1089 _cups_strcasecmp(line
, "AuthType") &&
1090 _cups_strcasecmp(line
, "Deny") &&
1091 _cups_strcasecmp(line
, "Order") &&
1092 _cups_strcasecmp(line
, "Require") &&
1093 _cups_strcasecmp(line
, "Satisfy"))
1094 cg
->cupsd_num_settings
= cupsAddOption(line
, value
,
1095 cg
->cupsd_num_settings
,
1096 &(cg
->cupsd_settings
));
1099 cupsFileClose(cupsd
);
1101 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1102 debug_logging
? "1" : "0",
1103 cg
->cupsd_num_settings
,
1104 &(cg
->cupsd_settings
));
1106 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1107 (remote_access
&& remote_admin
) ?
1109 cg
->cupsd_num_settings
,
1110 &(cg
->cupsd_settings
));
1112 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
1113 remote_any
? "1" : "0",
1114 cg
->cupsd_num_settings
,
1115 &(cg
->cupsd_settings
));
1117 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1118 (remote_access
&& browsing
) ? "1" :
1120 cg
->cupsd_num_settings
,
1121 &(cg
->cupsd_settings
));
1123 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1124 cancel_policy
? "1" : "0",
1125 cg
->cupsd_num_settings
,
1126 &(cg
->cupsd_settings
));
1128 else if (status
!= HTTP_STATUS_NOT_MODIFIED
)
1129 invalidate_cupsd_cache(cg
);
1132 * Remove any temporary files and copy the settings array...
1138 for (i
= cg
->cupsd_num_settings
, setting
= cg
->cupsd_settings
;
1141 *num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1142 *num_settings
, settings
);
1144 return (cg
->cupsd_num_settings
> 0);
1149 * 'cupsAdminSetServerSettings()' - Set settings on the server.
1151 * @since CUPS 1.3/OS X 10.5@
1154 int /* O - 1 on success, 0 on failure */
1155 cupsAdminSetServerSettings(
1156 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1157 int num_settings
, /* I - Number of settings */
1158 cups_option_t
*settings
) /* I - Settings */
1160 int i
; /* Looping var */
1161 http_status_t status
; /* GET/PUT status */
1162 const char *server_port_env
; /* SERVER_PORT env var */
1163 int server_port
; /* IPP port for server */
1164 cups_file_t
*cupsd
; /* cupsd.conf file */
1165 char cupsdconf
[1024]; /* cupsd.conf filename */
1166 int remote
; /* Remote cupsd.conf file? */
1167 char tempfile
[1024]; /* Temporary new cupsd.conf */
1168 cups_file_t
*temp
; /* Temporary file */
1169 char line
[1024], /* Line from cupsd.conf file */
1170 *value
; /* Value on line */
1171 int linenum
, /* Line number in file */
1172 in_location
, /* In a location section? */
1173 in_policy
, /* In a policy section? */
1174 in_default_policy
, /* In the default policy section? */
1175 in_cancel_job
, /* In a cancel-job section? */
1176 in_admin_location
, /* In the /admin location? */
1177 in_conf_location
, /* In the /admin/conf location? */
1178 in_root_location
; /* In the / location? */
1179 const char *val
; /* Setting value */
1180 int share_printers
, /* Share local printers */
1181 remote_admin
, /* Remote administration allowed? */
1182 remote_any
, /* Remote access from anywhere? */
1183 user_cancel_any
, /* Cancel-job policy set? */
1184 debug_logging
; /* LogLevel debug set? */
1185 int wrote_port_listen
, /* Wrote the port/listen lines? */
1186 wrote_browsing
, /* Wrote the browsing lines? */
1187 wrote_policy
, /* Wrote the policy? */
1188 wrote_loglevel
, /* Wrote the LogLevel line? */
1189 wrote_admin_location
, /* Wrote the /admin location? */
1190 wrote_conf_location
, /* Wrote the /admin/conf location? */
1191 wrote_root_location
; /* Wrote the / location? */
1192 int indent
; /* Indentation */
1193 int cupsd_num_settings
; /* New number of settings */
1194 int old_share_printers
, /* Share local printers */
1195 old_remote_admin
, /* Remote administration allowed? */
1196 old_user_cancel_any
, /* Cancel-job policy set? */
1197 old_debug_logging
; /* LogLevel debug set? */
1198 cups_option_t
*cupsd_settings
, /* New settings */
1199 *setting
; /* Current setting */
1200 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1204 * Range check input...
1208 http
= _cupsConnect();
1210 if (!http
|| !num_settings
|| !settings
)
1212 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1218 * Get the cupsd.conf file...
1221 if (get_cupsd_conf(http
, cg
, 0, cupsdconf
, sizeof(cupsdconf
),
1222 &remote
) == HTTP_STATUS_OK
)
1224 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
1226 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
1234 * Get current settings...
1237 if (!cupsAdminGetServerSettings(http
, &cupsd_num_settings
,
1241 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, cupsd_num_settings
,
1242 cupsd_settings
)) != NULL
)
1243 old_debug_logging
= atoi(val
);
1245 old_debug_logging
= 0;
1247 DEBUG_printf(("1cupsAdminSetServerSettings: old debug_logging=%d",
1248 old_debug_logging
));
1250 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, cupsd_num_settings
,
1251 cupsd_settings
)) != NULL
)
1252 old_remote_admin
= atoi(val
);
1254 old_remote_admin
= 0;
1256 DEBUG_printf(("1cupsAdminSetServerSettings: old remote_admin=%d",
1259 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, cupsd_num_settings
,
1260 cupsd_settings
)) != NULL
)
1261 remote_any
= atoi(val
);
1265 DEBUG_printf(("1cupsAdminSetServerSettings: old remote_any=%d",
1268 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, cupsd_num_settings
,
1269 cupsd_settings
)) != NULL
)
1270 old_share_printers
= atoi(val
);
1272 old_share_printers
= 0;
1274 DEBUG_printf(("1cupsAdminSetServerSettings: old share_printers=%d",
1275 old_share_printers
));
1277 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, cupsd_num_settings
,
1278 cupsd_settings
)) != NULL
)
1279 old_user_cancel_any
= atoi(val
);
1281 old_user_cancel_any
= 0;
1283 DEBUG_printf(("1cupsAdminSetServerSettings: old user_cancel_any=%d",
1284 old_user_cancel_any
));
1286 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
1289 * Get basic settings...
1292 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, num_settings
,
1295 debug_logging
= atoi(val
);
1297 if (debug_logging
== old_debug_logging
)
1300 * No change to this setting...
1309 DEBUG_printf(("1cupsAdminSetServerSettings: debug_logging=%d",
1312 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, num_settings
,
1314 remote_any
= atoi(val
);
1316 DEBUG_printf(("1cupsAdminSetServerSettings: remote_any=%d",
1319 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, num_settings
,
1322 remote_admin
= atoi(val
);
1324 if (remote_admin
== old_remote_admin
)
1327 * No change to this setting...
1336 DEBUG_printf(("1cupsAdminSetServerSettings: remote_admin=%d",
1339 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, num_settings
,
1342 share_printers
= atoi(val
);
1344 if (share_printers
== old_share_printers
)
1347 * No change to this setting...
1350 share_printers
= -1;
1354 share_printers
= -1;
1356 DEBUG_printf(("1cupsAdminSetServerSettings: share_printers=%d",
1359 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, num_settings
,
1362 user_cancel_any
= atoi(val
);
1364 if (user_cancel_any
== old_user_cancel_any
)
1367 * No change to this setting...
1370 user_cancel_any
= -1;
1374 user_cancel_any
= -1;
1376 DEBUG_printf(("1cupsAdminSetServerSettings: user_cancel_any=%d",
1380 * Create a temporary file for the new cupsd.conf file...
1383 if ((temp
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1385 cupsFileClose(cupsd
);
1390 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
1395 * Copy the old file to the new, making changes along the way...
1398 cupsd_num_settings
= 0;
1399 in_admin_location
= 0;
1401 in_conf_location
= 0;
1402 in_default_policy
= 0;
1405 in_root_location
= 0;
1407 wrote_admin_location
= 0;
1409 wrote_conf_location
= 0;
1412 wrote_port_listen
= 0;
1413 wrote_root_location
= 0;
1416 if ((server_port_env
= getenv("SERVER_PORT")) != NULL
)
1418 if ((server_port
= atoi(server_port_env
)) <= 0)
1419 server_port
= ippPort();
1422 server_port
= ippPort();
1424 if (server_port
<= 0)
1425 server_port
= IPP_PORT
;
1427 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
1429 if ((!_cups_strcasecmp(line
, "Port") || !_cups_strcasecmp(line
, "Listen")) &&
1430 (remote_admin
>= 0 || remote_any
> 0 || share_printers
>= 0))
1432 if (!wrote_port_listen
)
1434 wrote_port_listen
= 1;
1436 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1438 cupsFilePuts(temp
, "# Allow remote access\n");
1439 cupsFilePrintf(temp
, "Port %d\n", server_port
);
1443 cupsFilePuts(temp
, "# Only listen for connections from the local "
1445 cupsFilePrintf(temp
, "Listen localhost:%d\n", server_port
);
1448 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1449 if ((!value
|| strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)) &&
1450 !access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1451 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1452 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1454 else if (value
&& value
[0] == '/'
1455 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1456 && strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)
1457 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1459 cupsFilePrintf(temp
, "Listen %s\n", value
);
1461 else if ((!_cups_strcasecmp(line
, "Browsing") ||
1462 !_cups_strcasecmp(line
, "BrowseLocalProtocols")) &&
1463 share_printers
>= 0)
1465 if (!wrote_browsing
)
1467 int new_share_printers
= (share_printers
> 0 ||
1468 (share_printers
== -1 &&
1469 old_share_printers
> 0));
1473 if (new_share_printers
)
1475 const char *localp
= cupsGetOption("BrowseLocalProtocols",
1476 num_settings
, settings
);
1478 if (!localp
|| !localp
[0])
1479 localp
= cupsGetOption("BrowseLocalProtocols", cupsd_num_settings
,
1482 cupsFilePuts(temp
, "# Share local printers on the local network.\n");
1483 cupsFilePuts(temp
, "Browsing On\n");
1486 localp
= CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS
;
1488 cupsFilePrintf(temp
, "BrowseLocalProtocols %s\n", localp
);
1490 cupsd_num_settings
= cupsAddOption("BrowseLocalProtocols", localp
,
1496 cupsFilePuts(temp
, "# Disable printer sharing.\n");
1497 cupsFilePuts(temp
, "Browsing Off\n");
1501 else if (!_cups_strcasecmp(line
, "LogLevel") && debug_logging
>= 0)
1508 "# Show troubleshooting information in error_log.\n");
1509 cupsFilePuts(temp
, "LogLevel debug\n");
1513 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1514 cupsFilePuts(temp
, "LogLevel " CUPS_DEFAULT_LOG_LEVEL
"\n");
1517 else if (!_cups_strcasecmp(line
, "<Policy"))
1519 in_default_policy
= !_cups_strcasecmp(value
, "default");
1522 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1525 else if (!_cups_strcasecmp(line
, "</Policy>"))
1528 if (!wrote_policy
&& in_default_policy
)
1532 if (!user_cancel_any
)
1533 cupsFilePuts(temp
, " # Only the owner or an administrator can "
1535 " <Limit Cancel-Job>\n"
1536 " Order deny,allow\n"
1537 " Require user @OWNER "
1538 CUPS_DEFAULT_PRINTOPERATOR_AUTH
"\n"
1543 in_default_policy
= 0;
1545 cupsFilePuts(temp
, "</Policy>\n");
1547 else if (!_cups_strcasecmp(line
, "<Location"))
1551 if (!strcmp(value
, "/admin"))
1552 in_admin_location
= 1;
1553 if (!strcmp(value
, "/admin/conf"))
1554 in_conf_location
= 1;
1555 else if (!strcmp(value
, "/"))
1556 in_root_location
= 1;
1558 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1560 else if (!_cups_strcasecmp(line
, "</Location>"))
1564 if (in_admin_location
&& remote_admin
>= 0)
1566 wrote_admin_location
= 1;
1569 cupsFilePuts(temp
, " # Allow remote administration...\n");
1570 else if (remote_admin
== 0)
1571 cupsFilePuts(temp
, " # Restrict access to the admin pages...\n");
1573 cupsFilePuts(temp
, " Order allow,deny\n");
1576 cupsFilePrintf(temp
, " Allow %s\n",
1577 remote_any
> 0 ? "all" : "@LOCAL");
1579 else if (in_conf_location
&& remote_admin
>= 0)
1581 wrote_conf_location
= 1;
1584 cupsFilePuts(temp
, " # Allow remote access to the configuration "
1587 cupsFilePuts(temp
, " # Restrict access to the configuration "
1590 cupsFilePuts(temp
, " Order allow,deny\n");
1593 cupsFilePrintf(temp
, " Allow %s\n",
1594 remote_any
> 0 ? "all" : "@LOCAL");
1596 else if (in_root_location
&&
1597 (remote_admin
>= 0 || remote_any
> 0 || share_printers
>= 0))
1599 wrote_root_location
= 1;
1601 if (remote_admin
> 0 && share_printers
> 0)
1602 cupsFilePuts(temp
, " # Allow shared printing and remote "
1603 "administration...\n");
1604 else if (remote_admin
> 0)
1605 cupsFilePuts(temp
, " # Allow remote administration...\n");
1606 else if (share_printers
> 0)
1607 cupsFilePuts(temp
, " # Allow shared printing...\n");
1608 else if (remote_any
> 0)
1609 cupsFilePuts(temp
, " # Allow remote access...\n");
1611 cupsFilePuts(temp
, " # Restrict access to the server...\n");
1613 cupsFilePuts(temp
, " Order allow,deny\n");
1615 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1616 cupsFilePrintf(temp
, " Allow %s\n",
1617 remote_any
> 0 ? "all" : "@LOCAL");
1620 in_admin_location
= 0;
1621 in_conf_location
= 0;
1622 in_root_location
= 0;
1624 cupsFilePuts(temp
, "</Location>\n");
1626 else if (!_cups_strcasecmp(line
, "<Limit"))
1628 if (in_default_policy
)
1631 * See if the policy limit is for the Cancel-Job operation...
1634 char *valptr
; /* Pointer into value */
1637 if (!_cups_strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1640 * Don't write anything for this limit section...
1647 cupsFilePrintf(temp
, "%*s%s", indent
, "", line
);
1651 for (valptr
= value
; *valptr
&& !_cups_isspace(*valptr
); valptr
++);
1656 if (!_cups_strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1659 * Write everything except for this definition...
1665 cupsFilePrintf(temp
, " %s", value
);
1667 for (value
= valptr
; _cups_isspace(*value
); value
++);
1670 cupsFilePuts(temp
, ">\n");
1674 cupsFilePrintf(temp
, "%*s%s %s>\n", indent
, "", line
, value
);
1678 else if (!_cups_strcasecmp(line
, "</Limit>") && in_cancel_job
)
1682 if (in_cancel_job
== 1)
1683 cupsFilePuts(temp
, " </Limit>\n");
1687 if (!user_cancel_any
)
1688 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1690 " <Limit Cancel-Job>\n"
1691 " Order deny,allow\n"
1692 " Require user @OWNER "
1693 CUPS_DEFAULT_PRINTOPERATOR_AUTH
"\n"
1698 else if ((((in_admin_location
|| in_conf_location
|| in_root_location
) &&
1699 (remote_admin
>= 0 || remote_any
> 0)) ||
1700 (in_root_location
&& share_printers
>= 0)) &&
1701 (!_cups_strcasecmp(line
, "Allow") || !_cups_strcasecmp(line
, "Deny") ||
1702 !_cups_strcasecmp(line
, "Order")))
1704 else if (in_cancel_job
== 2)
1706 else if (line
[0] == '<')
1710 cupsFilePrintf(temp
, "%*s%s %s>\n", indent
, "", line
, value
);
1718 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1721 else if (!in_policy
&& !in_location
&&
1722 (val
= cupsGetOption(line
, num_settings
, settings
)) != NULL
)
1725 * Replace this directive's value with the new one...
1728 cupsd_num_settings
= cupsAddOption(line
, val
, cupsd_num_settings
,
1732 * Write the new value in its place, without indentation since we
1733 * only support setting root directives, not in sections...
1736 cupsFilePrintf(temp
, "%s %s\n", line
, val
);
1740 if (!in_policy
&& !in_location
)
1743 * Record the non-policy, non-location directives that we find
1744 * in the server settings, since we cache this info and record it
1745 * in cupsAdminGetServerSettings()...
1748 cupsd_num_settings
= cupsAddOption(line
, value
, cupsd_num_settings
,
1752 cupsFilePrintf(temp
, "%*s%s %s\n", indent
, "", line
, value
);
1755 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1759 * Write any missing info...
1762 if (!wrote_browsing
&& share_printers
>= 0)
1764 if (share_printers
> 0)
1766 cupsFilePuts(temp
, "# Share local printers on the local network.\n");
1767 cupsFilePuts(temp
, "Browsing On\n");
1771 cupsFilePuts(temp
, "# Disable printer sharing and shared printers.\n");
1772 cupsFilePuts(temp
, "Browsing Off\n");
1776 if (!wrote_loglevel
&& debug_logging
>= 0)
1780 cupsFilePuts(temp
, "# Show troubleshooting information in error_log.\n");
1781 cupsFilePuts(temp
, "LogLevel debug\n");
1785 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1786 cupsFilePuts(temp
, "LogLevel " CUPS_DEFAULT_LOG_LEVEL
"\n");
1790 if (!wrote_port_listen
&&
1791 (remote_admin
>= 0 || remote_any
> 0 || share_printers
>= 0))
1793 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1795 cupsFilePuts(temp
, "# Allow remote access\n");
1796 cupsFilePrintf(temp
, "Port %d\n", ippPort());
1801 "# Only listen for connections from the local machine.\n");
1802 cupsFilePrintf(temp
, "Listen localhost:%d\n", ippPort());
1805 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1806 if (!access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1807 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1808 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1811 if (!wrote_root_location
&&
1812 (remote_admin
>= 0 || remote_any
> 0 || share_printers
>= 0))
1814 if (remote_admin
> 0 && share_printers
> 0)
1816 "# Allow shared printing and remote administration...\n");
1817 else if (remote_admin
> 0)
1818 cupsFilePuts(temp
, "# Allow remote administration...\n");
1819 else if (share_printers
> 0)
1820 cupsFilePuts(temp
, "# Allow shared printing...\n");
1821 else if (remote_any
> 0)
1822 cupsFilePuts(temp
, "# Allow remote access...\n");
1824 cupsFilePuts(temp
, "# Restrict access to the server...\n");
1826 cupsFilePuts(temp
, "<Location />\n"
1827 " Order allow,deny\n");
1829 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1830 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1832 cupsFilePuts(temp
, "</Location>\n");
1835 if (!wrote_admin_location
&& remote_admin
>= 0)
1838 cupsFilePuts(temp
, "# Allow remote administration...\n");
1840 cupsFilePuts(temp
, "# Restrict access to the admin pages...\n");
1842 cupsFilePuts(temp
, "<Location /admin>\n"
1843 " Order allow,deny\n");
1846 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1848 cupsFilePuts(temp
, "</Location>\n");
1851 if (!wrote_conf_location
&& remote_admin
>= 0)
1855 "# Allow remote access to the configuration files...\n");
1857 cupsFilePuts(temp
, "# Restrict access to the configuration files...\n");
1859 cupsFilePuts(temp
, "<Location /admin/conf>\n"
1860 " AuthType Default\n"
1861 " Require user @SYSTEM\n"
1862 " Order allow,deny\n");
1865 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1867 cupsFilePuts(temp
, "</Location>\n");
1870 if (!wrote_policy
&& user_cancel_any
>= 0)
1872 cupsFilePuts(temp
, "<Policy default>\n"
1873 " # Job-related operations must be done by the owner "
1874 "or an administrator...\n"
1875 " <Limit Send-Document Send-URI Hold-Job Release-Job "
1876 "Restart-Job Purge-Jobs Set-Job-Attributes "
1877 "Create-Job-Subscription Renew-Subscription "
1878 "Cancel-Subscription Get-Notifications Reprocess-Job "
1879 "Cancel-Current-Job Suspend-Current-Job Resume-Job "
1881 " Require user @OWNER @SYSTEM\n"
1882 " Order deny,allow\n"
1884 " # All administration operations require an "
1885 "administrator to authenticate...\n"
1886 " <Limit Pause-Printer Resume-Printer "
1887 "Set-Printer-Attributes Enable-Printer "
1888 "Disable-Printer Pause-Printer-After-Current-Job "
1889 "Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer "
1890 "Activate-Printer Restart-Printer Shutdown-Printer "
1891 "Startup-Printer Promote-Job Schedule-Job-After "
1892 "CUPS-Add-Printer CUPS-Delete-Printer "
1893 "CUPS-Add-Class CUPS-Delete-Class "
1894 "CUPS-Accept-Jobs CUPS-Reject-Jobs "
1895 "CUPS-Set-Default CUPS-Add-Device CUPS-Delete-Device>\n"
1896 " AuthType Default\n"
1897 " Require user @SYSTEM\n"
1898 " Order deny,allow\n"
1901 if (!user_cancel_any
)
1902 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1904 " <Limit Cancel-Job>\n"
1905 " Order deny,allow\n"
1906 " Require user @OWNER "
1907 CUPS_DEFAULT_PRINTOPERATOR_AUTH
"\n"
1910 cupsFilePuts(temp
, " <Limit All>\n"
1911 " Order deny,allow\n"
1916 for (i
= num_settings
, setting
= settings
; i
> 0; i
--, setting
++)
1917 if (setting
->name
[0] != '_' &&
1918 _cups_strcasecmp(setting
->name
, "Listen") &&
1919 _cups_strcasecmp(setting
->name
, "Port") &&
1920 !cupsGetOption(setting
->name
, cupsd_num_settings
, cupsd_settings
))
1923 * Add this directive to the list of directives we have written...
1926 cupsd_num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1927 cupsd_num_settings
, &cupsd_settings
);
1930 * Write the new value, without indentation since we only support
1931 * setting root directives, not in sections...
1934 cupsFilePrintf(temp
, "%s %s\n", setting
->name
, setting
->value
);
1937 cupsFileClose(cupsd
);
1938 cupsFileClose(temp
);
1941 * Upload the configuration file to the server...
1944 status
= cupsPutFile(http
, "/admin/conf/cupsd.conf", tempfile
);
1946 if (status
== HTTP_STATUS_CREATED
)
1949 * Updated OK, add the basic settings...
1952 if (debug_logging
>= 0)
1953 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1954 debug_logging
? "1" : "0",
1955 cupsd_num_settings
, &cupsd_settings
);
1957 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1958 old_debug_logging
? "1" : "0",
1959 cupsd_num_settings
, &cupsd_settings
);
1961 if (remote_admin
>= 0)
1962 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1963 remote_admin
? "1" : "0",
1964 cupsd_num_settings
, &cupsd_settings
);
1966 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1967 old_remote_admin
? "1" : "0",
1968 cupsd_num_settings
, &cupsd_settings
);
1970 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
1971 remote_any
? "1" : "0",
1972 cupsd_num_settings
, &cupsd_settings
);
1974 if (share_printers
>= 0)
1975 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1976 share_printers
? "1" : "0",
1977 cupsd_num_settings
, &cupsd_settings
);
1979 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1980 old_share_printers
? "1" : "0",
1981 cupsd_num_settings
, &cupsd_settings
);
1983 if (user_cancel_any
>= 0)
1984 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1985 user_cancel_any
? "1" : "0",
1986 cupsd_num_settings
, &cupsd_settings
);
1988 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1989 old_user_cancel_any
? "1" : "0",
1990 cupsd_num_settings
, &cupsd_settings
);
1993 * Save the new values...
1996 invalidate_cupsd_cache(cg
);
1998 cg
->cupsd_num_settings
= cupsd_num_settings
;
1999 cg
->cupsd_settings
= cupsd_settings
;
2000 cg
->cupsd_update
= time(NULL
);
2002 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
2005 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
2008 * Remote our temp files and return...
2016 return (status
== HTTP_STATUS_CREATED
);
2021 * 'do_samba_command()' - Do a SAMBA command.
2024 static int /* O - Status of command */
2025 do_samba_command(const char *command
, /* I - Command to run */
2026 const char *address
, /* I - Address for command */
2027 const char *subcmd
, /* I - Sub-command */
2028 const char *authfile
, /* I - Samba authentication file */
2029 FILE *logfile
) /* I - Optional log file */
2032 return (1); /* Always fail on Windows... */
2035 int status
; /* Status of command */
2036 int pid
; /* Process ID of child */
2040 _cupsLangPrintf(logfile
,
2041 _("Running command: %s %s -N -A %s -c \'%s\'"),
2042 command
, address
, authfile
, subcmd
);
2044 if ((pid
= fork()) == 0)
2047 * Child goes here, redirect stdin/out/err and execute the command...
2050 int fd
= open("/dev/null", O_RDONLY
);
2059 dup2(fileno(logfile
), 1);
2060 else if ((fd
= open("/dev/null", O_WRONLY
)) > 1)
2068 execlp(command
, command
, address
, "-N", "-A", authfile
, "-c", subcmd
,
2077 _cupsLangPrintf(logfile
, _("Unable to run \"%s\": %s"),
2078 command
, strerror(errno
));
2083 * Wait for the process to complete...
2086 while (wait(&status
) != pid
);
2090 _cupsLangPuts(logfile
, "");
2092 DEBUG_printf(("9do_samba_command: status=%d", status
));
2094 if (WIFEXITED(status
))
2095 return (WEXITSTATUS(status
));
2097 return (-WTERMSIG(status
));
2103 * 'get_cupsd_conf()' - Get the current cupsd.conf file.
2106 static http_status_t
/* O - Status of request */
2108 http_t
*http
, /* I - Connection to server */
2109 _cups_globals_t
*cg
, /* I - Global data */
2110 time_t last_update
, /* I - Last update time for file */
2111 char *name
, /* I - Filename buffer */
2112 int namesize
, /* I - Size of filename buffer */
2113 int *remote
) /* O - Remote file? */
2115 int fd
; /* Temporary file descriptor */
2117 struct stat info
; /* cupsd.conf file information */
2119 http_status_t status
; /* Status of getting cupsd.conf */
2120 char host
[HTTP_MAX_HOST
]; /* Hostname for connection */
2124 * See if we already have the data we need...
2127 httpGetHostname(http
, host
, sizeof(host
));
2129 if (_cups_strcasecmp(cg
->cupsd_hostname
, host
))
2130 invalidate_cupsd_cache(cg
);
2132 snprintf(name
, namesize
, "%s/cupsd.conf", cg
->cups_serverroot
);
2136 if (!_cups_strcasecmp(host
, "localhost") && !access(name
, R_OK
))
2139 * Read the local file rather than using HTTP...
2142 if (stat(name
, &info
))
2144 char message
[1024]; /* Message string */
2147 snprintf(message
, sizeof(message
),
2148 _cupsLangString(cupsLangDefault(), _("stat of %s failed: %s")),
2149 name
, strerror(errno
));
2150 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
2154 return (HTTP_STATUS_SERVER_ERROR
);
2156 else if (last_update
&& info
.st_mtime
<= last_update
)
2157 status
= HTTP_STATUS_NOT_MODIFIED
;
2159 status
= HTTP_STATUS_OK
;
2165 * Read cupsd.conf via a HTTP GET request...
2168 if ((fd
= cupsTempFd(name
, namesize
)) < 0)
2172 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
2174 invalidate_cupsd_cache(cg
);
2176 return (HTTP_STATUS_SERVER_ERROR
);
2181 httpClearFields(http
);
2184 httpSetField(http
, HTTP_FIELD_IF_MODIFIED_SINCE
,
2185 httpGetDateString(last_update
));
2187 status
= cupsGetFd(http
, "/admin/conf/cupsd.conf", fd
);
2191 if (status
!= HTTP_STATUS_OK
)
2203 * 'invalidate_cupsd_cache()' - Invalidate the cached cupsd.conf settings.
2207 invalidate_cupsd_cache(
2208 _cups_globals_t
*cg
) /* I - Global data */
2210 cupsFreeOptions(cg
->cupsd_num_settings
, cg
->cupsd_settings
);
2212 cg
->cupsd_hostname
[0] = '\0';
2213 cg
->cupsd_update
= 0;
2214 cg
->cupsd_num_settings
= 0;
2215 cg
->cupsd_settings
= NULL
;
2220 * 'write_option()' - Write a CUPS option to a PPD file.
2224 write_option(cups_file_t
*dstfp
, /* I - PPD file */
2225 int order
, /* I - Order dependency */
2226 const char *name
, /* I - Option name */
2227 const char *text
, /* I - Option text */
2228 const char *attrname
, /* I - Attribute name */
2229 ipp_attribute_t
*suppattr
, /* I - IPP -supported attribute */
2230 ipp_attribute_t
*defattr
, /* I - IPP -default attribute */
2231 int defval
, /* I - Default value number */
2232 int valcount
) /* I - Number of values */
2234 int i
; /* Looping var */
2237 cupsFilePrintf(dstfp
, "*JCLOpenUI *%s/%s: PickOne\n"
2238 "*OrderDependency: %d JCLSetup *%s\n",
2239 name
, text
, order
, name
);
2241 if (defattr
->value_tag
== IPP_TAG_INTEGER
)
2244 * Do numeric options with a range or list...
2247 cupsFilePrintf(dstfp
, "*Default%s: %d\n", name
,
2248 defattr
->values
[defval
].integer
);
2250 if (suppattr
->value_tag
== IPP_TAG_RANGE
)
2253 * List each number in the range...
2256 for (i
= suppattr
->values
[0].range
.lower
;
2257 i
<= suppattr
->values
[0].range
.upper
;
2260 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, i
);
2263 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n",
2265 else if (defval
== 0)
2266 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
, i
);
2267 else if (defval
< (valcount
- 1))
2268 cupsFilePrintf(dstfp
, ",%d\"\n", i
);
2270 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", i
);
2276 * List explicit numbers...
2279 for (i
= 0; i
< suppattr
->num_values
; i
++)
2281 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, suppattr
->values
[i
].integer
);
2284 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n", attrname
,
2285 suppattr
->values
[i
].integer
);
2286 else if (defval
== 0)
2287 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
,
2288 suppattr
->values
[i
].integer
);
2289 else if (defval
< (valcount
- 1))
2290 cupsFilePrintf(dstfp
, ",%d\"\n", suppattr
->values
[i
].integer
);
2292 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", suppattr
->values
[i
].integer
);
2299 * Do text options with a list...
2302 cupsFilePrintf(dstfp
, "*Default%s: %s\n", name
,
2303 defattr
->values
[defval
].string
.text
);
2305 for (i
= 0; i
< suppattr
->num_values
; i
++)
2307 cupsFilePrintf(dstfp
, "*%s %s: \"", name
,
2308 suppattr
->values
[i
].string
.text
);
2311 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\n\"\n*End\n", attrname
,
2312 suppattr
->values
[i
].string
.text
);
2313 else if (defval
== 0)
2314 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\"\n", attrname
,
2315 suppattr
->values
[i
].string
.text
);
2316 else if (defval
< (valcount
- 1))
2317 cupsFilePrintf(dstfp
, ",%s\"\n", suppattr
->values
[i
].string
.text
);
2319 cupsFilePrintf(dstfp
, ",%s\n\"\n*End\n",
2320 suppattr
->values
[i
].string
.text
);
2324 cupsFilePrintf(dstfp
, "*JCLCloseUI: *%s\n\n", name
);