2 * Administration utility API definitions for CUPS.
4 * Copyright 2007-2016 by Apple Inc.
5 * Copyright 2001-2007 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include "cups-private.h"
16 #include "adminutil.h"
21 # include <sys/wait.h>
29 static int do_samba_command(const char *command
,
31 const char *subcommand
,
34 static http_status_t
get_cupsd_conf(http_t
*http
, _cups_globals_t
*cg
,
35 time_t last_update
, char *name
,
36 size_t namelen
, int *remote
);
37 static void invalidate_cupsd_cache(_cups_globals_t
*cg
);
38 static void write_option(cups_file_t
*dstfp
, int order
,
39 const char *name
, const char *text
,
41 ipp_attribute_t
*suppattr
,
42 ipp_attribute_t
*defattr
, int defval
,
47 * 'cupsAdminCreateWindowsPPD()' - Create the Windows PPD file for a printer.
52 char * /* O - PPD file or NULL */
53 cupsAdminCreateWindowsPPD(
54 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
55 const char *dest
, /* I - Printer or class */
56 char *buffer
, /* I - Filename buffer */
57 int bufsize
) /* I - Size of filename buffer */
59 const char *src
; /* Source PPD filename */
60 cups_file_t
*srcfp
, /* Source PPD file */
61 *dstfp
; /* Destination PPD file */
62 ipp_t
*request
, /* IPP request */
63 *response
; /* IPP response */
64 ipp_attribute_t
*suppattr
, /* IPP -supported attribute */
65 *defattr
; /* IPP -default attribute */
66 cups_lang_t
*language
; /* Current language */
67 char line
[256], /* Line from PPD file */
68 junk
[256], /* Extra junk to throw away */
69 *ptr
, /* Pointer into line */
70 uri
[1024], /* Printer URI */
71 option
[41], /* Option */
72 choice
[41]; /* Choice */
73 int jcloption
, /* In a JCL option? */
74 jclorder
, /* Next JCL order dependency */
75 linenum
; /* Current line number */
76 time_t curtime
; /* Current time */
77 struct tm
*curdate
; /* Current date */
78 static const char * const pattrs
[] = /* Printer attributes we want */
80 "job-hold-until-supported",
81 "job-hold-until-default",
82 "job-sheets-supported",
84 "job-priority-supported",
85 "job-priority-default"
90 * Range check the input...
97 http
= _cupsConnect();
99 if (!http
|| !dest
|| !buffer
|| bufsize
< 2)
103 * Get the PPD file...
106 if ((src
= cupsGetPPD2(http
, dest
)) == NULL
)
110 * Get the supported banner pages, etc. for the printer...
113 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
115 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
116 "localhost", 0, "/printers/%s", dest
);
117 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
118 "printer-uri", NULL
, uri
);
120 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
121 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
125 * Do the request and get back a response...
128 response
= cupsDoRequest(http
, request
, "/");
129 if (!response
|| cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
136 * Open the original PPD file...
139 if ((srcfp
= cupsFileOpen(src
, "rb")) == NULL
)
143 * Create a temporary output file using the destination buffer...
146 if ((dstfp
= cupsTempFile2(buffer
, bufsize
)) == NULL
)
148 cupsFileClose(srcfp
);
156 * Write a new header explaining that this isn't the original PPD...
159 cupsFilePuts(dstfp
, "*PPD-Adobe: \"4.3\"\n");
161 curtime
= time(NULL
);
162 curdate
= gmtime(&curtime
);
164 cupsFilePrintf(dstfp
, "*%% Modified on %04d%02d%02d%02d%02d%02d+0000 "
165 "for CUPS Windows Driver\n",
166 curdate
->tm_year
+ 1900, curdate
->tm_mon
+ 1, curdate
->tm_mday
,
167 curdate
->tm_hour
, curdate
->tm_min
, curdate
->tm_sec
);
170 * Read the existing PPD file, converting all PJL commands to CUPS
171 * job ticket comments...
177 language
= cupsLangDefault();
179 while (cupsFileGets(srcfp
, line
, sizeof(line
)))
183 if (!strncmp(line
, "*PPD-Adobe:", 11))
186 * Already wrote the PPD header...
191 else if (!strncmp(line
, "*JCLBegin:", 10) ||
192 !strncmp(line
, "*JCLToPSInterpreter:", 20) ||
193 !strncmp(line
, "*JCLEnd:", 8) ||
194 !strncmp(line
, "*Protocols:", 11))
197 * Don't use existing JCL keywords; we'll create our own, below...
200 cupsFilePrintf(dstfp
, "*%% Commented out for CUPS Windows Driver...\n"
201 "*%%%s\n", line
+ 1);
204 else if (!strncmp(line
, "*JCLOpenUI", 10))
207 cupsFilePrintf(dstfp
, "%s\n", line
);
209 else if (!strncmp(line
, "*JCLCloseUI", 11))
212 cupsFilePrintf(dstfp
, "%s\n", line
);
214 else if (jcloption
&& !strncmp(line
, "*OrderDependency:", 17))
216 for (ptr
= line
+ 17; _cups_isspace(*ptr
); ptr
++);
218 ptr
= strchr(ptr
, ' ');
222 cupsFilePrintf(dstfp
, "*OrderDependency: %d%s\n", jclorder
, ptr
);
226 cupsFilePrintf(dstfp
, "%s\n", line
);
228 else if (jcloption
&&
229 strncmp(line
, "*End", 4) &&
230 strncmp(line
, "*Default", 8))
232 if ((ptr
= strchr(line
, ':')) == NULL
)
234 snprintf(line
, sizeof(line
),
235 _cupsLangString(language
, _("Missing value on line %d.")),
237 _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR
, line
, 0);
239 cupsFileClose(srcfp
);
240 cupsFileClose(dstfp
);
250 if ((ptr
= strchr(ptr
, '\"')) == NULL
)
252 snprintf(line
, sizeof(line
),
253 _cupsLangString(language
,
254 _("Missing double quote on line %d.")),
256 _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR
, line
, 0);
258 cupsFileClose(srcfp
);
259 cupsFileClose(dstfp
);
269 if (sscanf(line
, "*%40s%*[ \t]%40[^:/]", option
, choice
) != 2)
271 snprintf(line
, sizeof(line
),
272 _cupsLangString(language
,
273 _("Bad option + choice on line %d.")),
275 _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR
, line
, 0);
277 cupsFileClose(srcfp
);
278 cupsFileClose(dstfp
);
288 if (strchr(ptr
+ 1, '\"') == NULL
)
294 while (cupsFileGets(srcfp
, junk
, sizeof(junk
)) != NULL
)
298 if (!strncmp(junk
, "*End", 4))
303 snprintf(ptr
+ 1, sizeof(line
) - (size_t)(ptr
- line
+ 1),
304 "%%cupsJobTicket: %s=%s\n\"\n*End", option
, choice
);
306 cupsFilePrintf(dstfp
, "*%% Changed for CUPS Windows Driver...\n%s\n",
310 cupsFilePrintf(dstfp
, "%s\n", line
);
313 cupsFileClose(srcfp
);
318 _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR
, _("Empty PPD file."), 1);
320 cupsFileClose(dstfp
);
329 * Now add the CUPS-specific attributes and options...
332 cupsFilePuts(dstfp
, "\n*% CUPS Job Ticket support and options...\n");
333 cupsFilePuts(dstfp
, "*Protocols: PJL\n");
334 cupsFilePuts(dstfp
, "*JCLBegin: \"%!PS-Adobe-3.0<0A>\"\n");
335 cupsFilePuts(dstfp
, "*JCLToPSInterpreter: \"\"\n");
336 cupsFilePuts(dstfp
, "*JCLEnd: \"\"\n");
338 cupsFilePuts(dstfp
, "\n*OpenGroup: CUPS/CUPS Options\n\n");
340 if ((defattr
= ippFindAttribute(response
, "job-hold-until-default",
341 IPP_TAG_ZERO
)) != NULL
&&
342 (suppattr
= ippFindAttribute(response
, "job-hold-until-supported",
343 IPP_TAG_ZERO
)) != NULL
)
344 write_option(dstfp
, jclorder
++, "cupsJobHoldUntil", "Hold Until",
345 "job-hold-until", suppattr
, defattr
, 0, 1);
347 if ((defattr
= ippFindAttribute(response
, "job-priority-default",
348 IPP_TAG_INTEGER
)) != NULL
&&
349 (suppattr
= ippFindAttribute(response
, "job-priority-supported",
350 IPP_TAG_RANGE
)) != NULL
)
351 write_option(dstfp
, jclorder
++, "cupsJobPriority", "Priority",
352 "job-priority", suppattr
, defattr
, 0, 1);
354 if ((defattr
= ippFindAttribute(response
, "job-sheets-default",
355 IPP_TAG_ZERO
)) != NULL
&&
356 (suppattr
= ippFindAttribute(response
, "job-sheets-supported",
357 IPP_TAG_ZERO
)) != NULL
)
359 write_option(dstfp
, jclorder
++, "cupsJobSheetsStart", "Start Banner",
360 "job-sheets", suppattr
, defattr
, 0, 2);
361 write_option(dstfp
, jclorder
, "cupsJobSheetsEnd", "End Banner",
362 "job-sheets", suppattr
, defattr
, 1, 2);
365 cupsFilePuts(dstfp
, "*CloseGroup: CUPS\n");
366 cupsFileClose(dstfp
);
375 * 'cupsAdminExportSamba()' - Export a printer to Samba.
380 int /* O - 1 on success, 0 on failure */
381 cupsAdminExportSamba(
382 const char *dest
, /* I - Destination to export */
383 const char *ppd
, /* I - PPD file */
384 const char *samba_server
, /* I - Samba server */
385 const char *samba_user
, /* I - Samba username */
386 const char *samba_password
, /* I - Samba password */
387 FILE *logfile
) /* I - Log file, if any */
389 int status
; /* Status of Samba commands */
390 int have_drivers
; /* Have drivers? */
391 char file
[1024], /* File to test for */
392 authfile
[1024], /* Temporary authentication file */
393 address
[1024], /* Address for command */
394 subcmd
[1024], /* Sub-command */
395 message
[1024]; /* Error message */
396 cups_file_t
*fp
; /* Authentication file */
397 cups_lang_t
*language
; /* Current language */
398 _cups_globals_t
*cg
= _cupsGlobals();
403 * Range check input...
406 if (!dest
|| !ppd
|| !samba_server
|| !samba_user
|| !samba_password
)
408 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
413 * Create a temporary authentication file for Samba...
416 if ((fp
= cupsTempFile2(authfile
, sizeof(authfile
))) == NULL
)
418 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
422 cupsFilePrintf(fp
, "username = %s\n", samba_user
);
423 cupsFilePrintf(fp
, "password = %s\n", samba_password
);
427 * See which drivers are available; the new CUPS v6 and Adobe drivers
428 * depend on the Windows 2k PS driver, so copy that driver first:
439 language
= cupsLangDefault();
441 snprintf(file
, sizeof(file
), "%s/drivers/pscript5.dll", cg
->cups_datadir
);
442 if (!access(file
, 0))
447 * Windows 2k driver is installed; do the smbclient commands needed
448 * to copy the Win2k drivers over...
451 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
453 snprintf(subcmd
, sizeof(subcmd
),
455 "put %s W32X86/%s.ppd;"
456 "put %s/drivers/ps5ui.dll W32X86/ps5ui.dll;"
457 "put %s/drivers/pscript.hlp W32X86/pscript.hlp;"
458 "put %s/drivers/pscript.ntf W32X86/pscript.ntf;"
459 "put %s/drivers/pscript5.dll W32X86/pscript5.dll",
460 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
461 cg
->cups_datadir
, cg
->cups_datadir
);
463 if ((status
= do_samba_command("smbclient", address
, subcmd
,
464 authfile
, logfile
)) != 0)
466 snprintf(message
, sizeof(message
),
467 _cupsLangString(language
,
468 _("Unable to copy Windows 2000 printer "
469 "driver files (%d).")), status
);
471 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
474 _cupsLangPuts(logfile
, message
);
482 * See if we also have the CUPS driver files; if so, use them!
485 snprintf(file
, sizeof(file
), "%s/drivers/cupsps6.dll", cg
->cups_datadir
);
486 if (!access(file
, 0))
489 * Copy the CUPS driver files over...
492 snprintf(subcmd
, sizeof(subcmd
),
493 "put %s/drivers/cups6.ini W32X86/cups6.ini;"
494 "put %s/drivers/cupsps6.dll W32X86/cupsps6.dll;"
495 "put %s/drivers/cupsui6.dll W32X86/cupsui6.dll",
496 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
498 if ((status
= do_samba_command("smbclient", address
, subcmd
,
499 authfile
, logfile
)) != 0)
501 snprintf(message
, sizeof(message
),
502 _cupsLangString(language
,
503 _("Unable to copy CUPS printer driver "
504 "files (%d).")), status
);
506 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
509 _cupsLangPuts(logfile
, message
);
517 * Do the rpcclient command needed for the CUPS drivers...
520 snprintf(subcmd
, sizeof(subcmd
),
521 "adddriver \"Windows NT x86\" \"%s:"
522 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
523 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
524 "cups6.ini,cupsps6.dll,cupsui6.dll\"",
530 * Don't have the CUPS drivers, so just use the standard Windows
534 snprintf(subcmd
, sizeof(subcmd
),
535 "adddriver \"Windows NT x86\" \"%s:"
536 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
537 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
541 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
542 authfile
, logfile
)) != 0)
544 snprintf(message
, sizeof(message
),
545 _cupsLangString(language
,
546 _("Unable to install Windows 2000 printer "
547 "driver files (%d).")), status
);
549 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
552 _cupsLangPuts(logfile
, message
);
561 * See if we have the Win9x PS driver...
564 snprintf(file
, sizeof(file
), "%s/drivers/ADOBEPS4.DRV", cg
->cups_datadir
);
565 if (!access(file
, 0))
570 * Do the smbclient commands needed for the Adobe Win9x drivers...
573 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
575 snprintf(subcmd
, sizeof(subcmd
),
577 "put %s WIN40/%s.PPD;"
578 "put %s/drivers/ADFONTS.MFM WIN40/ADFONTS.MFM;"
579 "put %s/drivers/ADOBEPS4.DRV WIN40/ADOBEPS4.DRV;"
580 "put %s/drivers/ADOBEPS4.HLP WIN40/ADOBEPS4.HLP;"
581 "put %s/drivers/ICONLIB.DLL WIN40/ICONLIB.DLL;"
582 "put %s/drivers/PSMON.DLL WIN40/PSMON.DLL;",
583 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
584 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
586 if ((status
= do_samba_command("smbclient", address
, subcmd
,
587 authfile
, logfile
)) != 0)
589 snprintf(message
, sizeof(message
),
590 _cupsLangString(language
,
591 _("Unable to copy Windows 9x printer "
592 "driver files (%d).")), status
);
594 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
597 _cupsLangPuts(logfile
, message
);
605 * Do the rpcclient commands needed for the Adobe Win9x drivers...
608 snprintf(subcmd
, sizeof(subcmd
),
609 "adddriver \"Windows 4.0\" \"%s:ADOBEPS4.DRV:%s.PPD:NULL:"
610 "ADOBEPS4.HLP:PSMON.DLL:RAW:"
611 "ADOBEPS4.DRV,%s.PPD,ADOBEPS4.HLP,PSMON.DLL,ADFONTS.MFM,"
615 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
616 authfile
, logfile
)) != 0)
618 snprintf(message
, sizeof(message
),
619 _cupsLangString(language
,
620 _("Unable to install Windows 9x printer "
621 "driver files (%d).")), status
);
623 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
626 _cupsLangPuts(logfile
, message
);
635 * See if we have the 64-bit Windows PS driver...
645 snprintf(file
, sizeof(file
), "%s/drivers/x64/pscript5.dll", cg
->cups_datadir
);
646 if (!access(file
, 0))
651 * 64-bit Windows driver is installed; do the smbclient commands needed
652 * to copy the Win64 drivers over...
655 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
657 snprintf(subcmd
, sizeof(subcmd
),
660 "put %s/drivers/x64/ps5ui.dll x64/ps5ui.dll;"
661 "put %s/drivers/x64/pscript.hlp x64/pscript.hlp;"
662 "put %s/drivers/x64/pscript.ntf x64/pscript.ntf;"
663 "put %s/drivers/x64/pscript5.dll x64/pscript5.dll",
664 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
665 cg
->cups_datadir
, cg
->cups_datadir
);
667 if ((status
= do_samba_command("smbclient", address
, subcmd
,
668 authfile
, logfile
)) != 0)
670 snprintf(message
, sizeof(message
),
671 _cupsLangString(language
,
672 _("Unable to copy 64-bit Windows printer "
673 "driver files (%d).")), status
);
675 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
678 _cupsLangPuts(logfile
, message
);
686 * See if we also have the CUPS driver files; if so, use them!
689 snprintf(file
, sizeof(file
), "%s/drivers/x64/cupsps6.dll", cg
->cups_datadir
);
690 if (!access(file
, 0))
693 * Copy the CUPS driver files over...
696 snprintf(subcmd
, sizeof(subcmd
),
697 "put %s/drivers/x64/cups6.ini x64/cups6.ini;"
698 "put %s/drivers/x64/cupsps6.dll x64/cupsps6.dll;"
699 "put %s/drivers/x64/cupsui6.dll x64/cupsui6.dll",
700 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
702 if ((status
= do_samba_command("smbclient", address
, subcmd
,
703 authfile
, logfile
)) != 0)
705 snprintf(message
, sizeof(message
),
706 _cupsLangString(language
,
707 _("Unable to copy 64-bit CUPS printer driver "
708 "files (%d).")), status
);
710 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
713 _cupsLangPuts(logfile
, message
);
721 * Do the rpcclient command needed for the CUPS drivers...
724 snprintf(subcmd
, sizeof(subcmd
),
725 "adddriver \"Windows x64\" \"%s:"
726 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
727 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
728 "cups6.ini,cupsps6.dll,cupsui6.dll\"",
734 * Don't have the CUPS drivers, so just use the standard Windows
738 snprintf(subcmd
, sizeof(subcmd
),
739 "adddriver \"Windows x64\" \"%s:"
740 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
741 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
745 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
746 authfile
, logfile
)) != 0)
748 snprintf(message
, sizeof(message
),
749 _cupsLangString(language
,
750 _("Unable to install Windows 2000 printer "
751 "driver files (%d).")), status
);
753 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
756 _cupsLangPuts(logfile
, message
);
764 if (logfile
&& !(have_drivers
& 1))
768 _cupsLangString(language
,
769 _("No Windows printer drivers are installed.")),
773 _cupsLangString(language
,
774 _("Warning, no Windows 2000 printer drivers "
778 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, message
, 0);
779 _cupsLangPuts(logfile
, message
);
782 if (have_drivers
== 0)
784 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, message
, 0);
792 * Finally, associate the drivers we just added with the queue...
795 snprintf(subcmd
, sizeof(subcmd
), "setdriver %s %s", dest
, dest
);
797 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
798 authfile
, logfile
)) != 0)
800 snprintf(message
, sizeof(message
),
801 _cupsLangString(language
,
802 _("Unable to set Windows printer driver (%d).")),
805 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
808 _cupsLangPuts(logfile
, message
);
822 * 'cupsAdminGetServerSettings()' - Get settings from the server.
824 * The returned settings should be freed with cupsFreeOptions() when
825 * you are done with them.
827 * @since CUPS 1.3/macOS 10.5@
830 int /* O - 1 on success, 0 on failure */
831 cupsAdminGetServerSettings(
832 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
833 int *num_settings
, /* O - Number of settings */
834 cups_option_t
**settings
) /* O - Settings */
836 int i
; /* Looping var */
837 cups_file_t
*cupsd
; /* cupsd.conf file */
838 char cupsdconf
[1024]; /* cupsd.conf filename */
839 int remote
; /* Remote cupsd.conf file? */
840 http_status_t status
; /* Status of getting cupsd.conf */
841 char line
[1024], /* Line from cupsd.conf file */
842 *value
; /* Value on line */
843 cups_option_t
*setting
; /* Current setting */
844 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
848 * Range check input...
854 * See if we are connected to the same server...
860 * Compare the connection hostname, port, and encryption settings to
861 * the cached defaults; these were initialized the first time we
865 if (strcmp(cg
->http
->hostname
, cg
->server
) ||
866 cg
->ipp_port
!= httpAddrPort(cg
->http
->hostaddr
) ||
867 (cg
->http
->encryption
!= cg
->encryption
&&
868 cg
->http
->encryption
== HTTP_ENCRYPTION_NEVER
))
871 * Need to close the current connection because something has changed...
880 * (Re)connect as needed...
885 if ((cg
->http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
,
886 cupsEncryption(), 1, 0, NULL
)) == NULL
)
889 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
, NULL
, 0);
891 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
,
892 _("Unable to connect to host."), 1);
907 if (!http
|| !num_settings
|| !settings
)
909 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
924 * Get the cupsd.conf file...
927 if ((status
= get_cupsd_conf(http
, cg
, cg
->cupsd_update
, cupsdconf
,
928 sizeof(cupsdconf
), &remote
)) == HTTP_STATUS_OK
)
930 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
932 char message
[1024]; /* Message string */
935 snprintf(message
, sizeof(message
),
936 _cupsLangString(cupsLangDefault(), _("Open of %s failed: %s")),
937 cupsdconf
, strerror(errno
));
938 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
947 * Read the file, keeping track of what settings are enabled...
950 int remote_access
= 0, /* Remote access allowed? */
951 remote_admin
= 0, /* Remote administration allowed? */
952 remote_any
= 0, /* Remote access from anywhere allowed? */
953 browsing
= 1, /* Browsing enabled? */
954 cancel_policy
= 1, /* Cancel-job policy set? */
955 debug_logging
= 0; /* LogLevel debug set? */
956 int linenum
= 0, /* Line number in file */
957 in_location
= 0, /* In a location section? */
958 in_policy
= 0, /* In a policy section? */
959 in_cancel_job
= 0, /* In a cancel-job section? */
960 in_admin_location
= 0; /* In the /admin location? */
963 invalidate_cupsd_cache(cg
);
965 cg
->cupsd_update
= time(NULL
);
966 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
968 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
970 if (!value
&& strncmp(line
, "</", 2))
971 value
= line
+ strlen(line
);
973 if ((!_cups_strcasecmp(line
, "Port") || !_cups_strcasecmp(line
, "Listen")) && value
)
975 char *port
; /* Pointer to port number, if any */
978 if ((port
= strrchr(value
, ':')) != NULL
)
980 else if (isdigit(*value
& 255))
983 * Listen on a port number implies remote access...
990 if (_cups_strcasecmp(value
, "localhost") && strcmp(value
, "127.0.0.1")
993 #endif /* AF_LOCAL */
995 && strcmp(value
, "[::1]")
996 #endif /* AF_INET6 */
1000 else if (!_cups_strcasecmp(line
, "Browsing"))
1002 browsing
= !_cups_strcasecmp(value
, "yes") ||
1003 !_cups_strcasecmp(value
, "on") ||
1004 !_cups_strcasecmp(value
, "true");
1006 else if (!_cups_strcasecmp(line
, "LogLevel"))
1008 debug_logging
= !_cups_strncasecmp(value
, "debug", 5);
1010 else if (!_cups_strcasecmp(line
, "<Policy") &&
1011 !_cups_strcasecmp(value
, "default"))
1015 else if (!_cups_strcasecmp(line
, "</Policy>"))
1019 else if (!_cups_strcasecmp(line
, "<Limit") && in_policy
&& value
)
1022 * See if the policy limit is for the Cancel-Job operation...
1025 char *valptr
; /* Pointer into value */
1030 for (valptr
= value
; *valptr
&& !_cups_isspace(*valptr
); valptr
++);
1035 if (!_cups_strcasecmp(value
, "cancel-job") ||
1036 !_cups_strcasecmp(value
, "all"))
1042 for (value
= valptr
; _cups_isspace(*value
); value
++);
1045 else if (!_cups_strcasecmp(line
, "</Limit>"))
1049 else if (!_cups_strcasecmp(line
, "Require") && in_cancel_job
)
1053 else if (!_cups_strcasecmp(line
, "<Location") && value
)
1055 in_admin_location
= !_cups_strcasecmp(value
, "/admin");
1058 else if (!_cups_strcasecmp(line
, "</Location>"))
1060 in_admin_location
= 0;
1063 else if (!_cups_strcasecmp(line
, "Allow") && value
&&
1064 _cups_strcasecmp(value
, "localhost") &&
1065 _cups_strcasecmp(value
, "127.0.0.1")
1068 #endif /* AF_LOCAL */
1070 && strcmp(value
, "::1")
1071 #endif /* AF_INET6 */
1074 if (in_admin_location
)
1076 else if (!_cups_strcasecmp(value
, "all"))
1079 else if (line
[0] != '<' && !in_location
&& !in_policy
&&
1080 _cups_strcasecmp(line
, "Allow") &&
1081 _cups_strcasecmp(line
, "AuthType") &&
1082 _cups_strcasecmp(line
, "Deny") &&
1083 _cups_strcasecmp(line
, "Order") &&
1084 _cups_strcasecmp(line
, "Require") &&
1085 _cups_strcasecmp(line
, "Satisfy"))
1086 cg
->cupsd_num_settings
= cupsAddOption(line
, value
,
1087 cg
->cupsd_num_settings
,
1088 &(cg
->cupsd_settings
));
1091 cupsFileClose(cupsd
);
1093 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1094 debug_logging
? "1" : "0",
1095 cg
->cupsd_num_settings
,
1096 &(cg
->cupsd_settings
));
1098 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1099 (remote_access
&& remote_admin
) ?
1101 cg
->cupsd_num_settings
,
1102 &(cg
->cupsd_settings
));
1104 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
1105 remote_any
? "1" : "0",
1106 cg
->cupsd_num_settings
,
1107 &(cg
->cupsd_settings
));
1109 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1110 (remote_access
&& browsing
) ? "1" :
1112 cg
->cupsd_num_settings
,
1113 &(cg
->cupsd_settings
));
1115 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1116 cancel_policy
? "1" : "0",
1117 cg
->cupsd_num_settings
,
1118 &(cg
->cupsd_settings
));
1120 else if (status
!= HTTP_STATUS_NOT_MODIFIED
)
1121 invalidate_cupsd_cache(cg
);
1124 * Remove any temporary files and copy the settings array...
1130 for (i
= cg
->cupsd_num_settings
, setting
= cg
->cupsd_settings
;
1133 *num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1134 *num_settings
, settings
);
1136 return (cg
->cupsd_num_settings
> 0);
1141 * 'cupsAdminSetServerSettings()' - Set settings on the server.
1143 * @since CUPS 1.3/macOS 10.5@
1146 int /* O - 1 on success, 0 on failure */
1147 cupsAdminSetServerSettings(
1148 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1149 int num_settings
, /* I - Number of settings */
1150 cups_option_t
*settings
) /* I - Settings */
1152 int i
; /* Looping var */
1153 http_status_t status
; /* GET/PUT status */
1154 const char *server_port_env
; /* SERVER_PORT env var */
1155 int server_port
; /* IPP port for server */
1156 cups_file_t
*cupsd
; /* cupsd.conf file */
1157 char cupsdconf
[1024]; /* cupsd.conf filename */
1158 int remote
; /* Remote cupsd.conf file? */
1159 char tempfile
[1024]; /* Temporary new cupsd.conf */
1160 cups_file_t
*temp
; /* Temporary file */
1161 char line
[1024], /* Line from cupsd.conf file */
1162 *value
; /* Value on line */
1163 int linenum
, /* Line number in file */
1164 in_location
, /* In a location section? */
1165 in_policy
, /* In a policy section? */
1166 in_default_policy
, /* In the default policy section? */
1167 in_cancel_job
, /* In a cancel-job section? */
1168 in_admin_location
, /* In the /admin location? */
1169 in_conf_location
, /* In the /admin/conf location? */
1170 in_log_location
, /* In the /admin/log location? */
1171 in_root_location
; /* In the / location? */
1172 const char *val
; /* Setting value */
1173 int share_printers
, /* Share local printers */
1174 remote_admin
, /* Remote administration allowed? */
1175 remote_any
, /* Remote access from anywhere? */
1176 user_cancel_any
, /* Cancel-job policy set? */
1177 debug_logging
; /* LogLevel debug set? */
1178 int wrote_port_listen
, /* Wrote the port/listen lines? */
1179 wrote_browsing
, /* Wrote the browsing lines? */
1180 wrote_policy
, /* Wrote the policy? */
1181 wrote_loglevel
, /* Wrote the LogLevel line? */
1182 wrote_admin_location
, /* Wrote the /admin location? */
1183 wrote_conf_location
, /* Wrote the /admin/conf location? */
1184 wrote_log_location
, /* Wrote the /admin/log location? */
1185 wrote_root_location
; /* Wrote the / location? */
1186 int indent
; /* Indentation */
1187 int cupsd_num_settings
; /* New number of settings */
1188 int old_share_printers
, /* Share local printers */
1189 old_remote_admin
, /* Remote administration allowed? */
1190 old_remote_any
, /* Remote access from anywhere? */
1191 old_user_cancel_any
, /* Cancel-job policy set? */
1192 old_debug_logging
; /* LogLevel debug set? */
1193 cups_option_t
*cupsd_settings
, /* New settings */
1194 *setting
; /* Current setting */
1195 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1199 * Range check input...
1203 http
= _cupsConnect();
1205 if (!http
|| !num_settings
|| !settings
)
1207 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1213 * Get the cupsd.conf file...
1216 if (get_cupsd_conf(http
, cg
, 0, cupsdconf
, sizeof(cupsdconf
),
1217 &remote
) == HTTP_STATUS_OK
)
1219 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
1221 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
1229 * Get current settings...
1232 if (!cupsAdminGetServerSettings(http
, &cupsd_num_settings
,
1236 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, cupsd_num_settings
,
1237 cupsd_settings
)) != NULL
)
1238 old_debug_logging
= atoi(val
);
1240 old_debug_logging
= 0;
1242 DEBUG_printf(("1cupsAdminSetServerSettings: old debug_logging=%d",
1243 old_debug_logging
));
1245 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, cupsd_num_settings
,
1246 cupsd_settings
)) != NULL
)
1247 old_remote_admin
= atoi(val
);
1249 old_remote_admin
= 0;
1251 DEBUG_printf(("1cupsAdminSetServerSettings: old remote_admin=%d",
1254 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, cupsd_num_settings
,
1255 cupsd_settings
)) != NULL
)
1256 old_remote_any
= atoi(val
);
1260 DEBUG_printf(("1cupsAdminSetServerSettings: old remote_any=%d",
1263 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, cupsd_num_settings
,
1264 cupsd_settings
)) != NULL
)
1265 old_share_printers
= atoi(val
);
1267 old_share_printers
= 0;
1269 DEBUG_printf(("1cupsAdminSetServerSettings: old share_printers=%d",
1270 old_share_printers
));
1272 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, cupsd_num_settings
,
1273 cupsd_settings
)) != NULL
)
1274 old_user_cancel_any
= atoi(val
);
1276 old_user_cancel_any
= 0;
1278 DEBUG_printf(("1cupsAdminSetServerSettings: old user_cancel_any=%d",
1279 old_user_cancel_any
));
1281 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
1284 * Get basic settings...
1287 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, num_settings
,
1290 debug_logging
= atoi(val
);
1292 if (debug_logging
== old_debug_logging
)
1295 * No change to this setting...
1304 DEBUG_printf(("1cupsAdminSetServerSettings: debug_logging=%d",
1307 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, num_settings
, settings
)) != NULL
)
1309 remote_any
= atoi(val
);
1311 if (remote_any
== old_remote_any
)
1314 * No change to this setting...
1323 DEBUG_printf(("1cupsAdminSetServerSettings: remote_any=%d", remote_any
));
1325 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, num_settings
,
1328 remote_admin
= atoi(val
);
1330 if (remote_admin
== old_remote_admin
)
1333 * No change to this setting...
1342 DEBUG_printf(("1cupsAdminSetServerSettings: remote_admin=%d",
1345 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, num_settings
,
1348 share_printers
= atoi(val
);
1350 if (share_printers
== old_share_printers
)
1353 * No change to this setting...
1356 share_printers
= -1;
1360 share_printers
= -1;
1362 DEBUG_printf(("1cupsAdminSetServerSettings: share_printers=%d",
1365 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, num_settings
,
1368 user_cancel_any
= atoi(val
);
1370 if (user_cancel_any
== old_user_cancel_any
)
1373 * No change to this setting...
1376 user_cancel_any
= -1;
1380 user_cancel_any
= -1;
1382 DEBUG_printf(("1cupsAdminSetServerSettings: user_cancel_any=%d",
1386 * Create a temporary file for the new cupsd.conf file...
1389 if ((temp
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1391 cupsFileClose(cupsd
);
1396 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
1401 * Copy the old file to the new, making changes along the way...
1404 cupsd_num_settings
= 0;
1405 in_admin_location
= 0;
1407 in_conf_location
= 0;
1408 in_default_policy
= 0;
1410 in_log_location
= 0;
1412 in_root_location
= 0;
1414 wrote_admin_location
= 0;
1416 wrote_conf_location
= 0;
1417 wrote_log_location
= 0;
1420 wrote_port_listen
= 0;
1421 wrote_root_location
= 0;
1424 if ((server_port_env
= getenv("SERVER_PORT")) != NULL
)
1426 if ((server_port
= atoi(server_port_env
)) <= 0)
1427 server_port
= ippPort();
1430 server_port
= ippPort();
1432 if (server_port
<= 0)
1433 server_port
= IPP_PORT
;
1435 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
1437 if ((!_cups_strcasecmp(line
, "Port") || !_cups_strcasecmp(line
, "Listen")) &&
1438 (remote_admin
>= 0 || remote_any
>= 0 || share_printers
>= 0))
1440 if (!wrote_port_listen
)
1442 wrote_port_listen
= 1;
1444 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1446 cupsFilePuts(temp
, "# Allow remote access\n");
1447 cupsFilePrintf(temp
, "Port %d\n", server_port
);
1451 cupsFilePuts(temp
, "# Only listen for connections from the local "
1453 cupsFilePrintf(temp
, "Listen localhost:%d\n", server_port
);
1456 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1457 if ((!value
|| strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)) &&
1458 !access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1459 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1460 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1462 else if (value
&& value
[0] == '/'
1463 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1464 && strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)
1465 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1467 cupsFilePrintf(temp
, "Listen %s\n", value
);
1469 else if ((!_cups_strcasecmp(line
, "Browsing") ||
1470 !_cups_strcasecmp(line
, "BrowseLocalProtocols")) &&
1471 share_printers
>= 0)
1473 if (!wrote_browsing
)
1475 int new_share_printers
= (share_printers
> 0 ||
1476 (share_printers
== -1 &&
1477 old_share_printers
> 0));
1481 if (new_share_printers
)
1483 const char *localp
= cupsGetOption("BrowseLocalProtocols",
1484 num_settings
, settings
);
1486 if (!localp
|| !localp
[0])
1487 localp
= cupsGetOption("BrowseLocalProtocols", cupsd_num_settings
,
1490 cupsFilePuts(temp
, "# Share local printers on the local network.\n");
1491 cupsFilePuts(temp
, "Browsing On\n");
1494 localp
= CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS
;
1496 cupsFilePrintf(temp
, "BrowseLocalProtocols %s\n", localp
);
1498 cupsd_num_settings
= cupsAddOption("BrowseLocalProtocols", localp
,
1504 cupsFilePuts(temp
, "# Disable printer sharing.\n");
1505 cupsFilePuts(temp
, "Browsing Off\n");
1509 else if (!_cups_strcasecmp(line
, "LogLevel") && debug_logging
>= 0)
1516 "# Show troubleshooting information in error_log.\n");
1517 cupsFilePuts(temp
, "LogLevel debug\n");
1521 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1522 cupsFilePuts(temp
, "LogLevel " CUPS_DEFAULT_LOG_LEVEL
"\n");
1525 else if (!_cups_strcasecmp(line
, "<Policy"))
1527 in_default_policy
= !_cups_strcasecmp(value
, "default");
1530 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1533 else if (!_cups_strcasecmp(line
, "</Policy>"))
1536 if (!wrote_policy
&& in_default_policy
)
1540 if (!user_cancel_any
)
1541 cupsFilePuts(temp
, " # Only the owner or an administrator can "
1543 " <Limit Cancel-Job>\n"
1544 " Order deny,allow\n"
1545 " Require user @OWNER "
1546 CUPS_DEFAULT_PRINTOPERATOR_AUTH
"\n"
1551 in_default_policy
= 0;
1553 cupsFilePuts(temp
, "</Policy>\n");
1555 else if (!_cups_strcasecmp(line
, "<Location"))
1559 if (!strcmp(value
, "/admin"))
1560 in_admin_location
= 1;
1561 else if (!strcmp(value
, "/admin/conf"))
1562 in_conf_location
= 1;
1563 else if (!strcmp(value
, "/admin/log"))
1564 in_log_location
= 1;
1565 else if (!strcmp(value
, "/"))
1566 in_root_location
= 1;
1568 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1570 else if (!_cups_strcasecmp(line
, "</Location>"))
1574 if (in_admin_location
&& remote_admin
>= 0)
1576 wrote_admin_location
= 1;
1579 cupsFilePuts(temp
, " # Allow remote administration...\n");
1580 else if (remote_admin
== 0)
1581 cupsFilePuts(temp
, " # Restrict access to the admin pages...\n");
1583 cupsFilePuts(temp
, " Order allow,deny\n");
1586 cupsFilePrintf(temp
, " Allow %s\n",
1587 remote_any
> 0 ? "all" : "@LOCAL");
1589 else if (in_conf_location
&& remote_admin
>= 0)
1591 wrote_conf_location
= 1;
1594 cupsFilePuts(temp
, " # Allow remote access to the configuration "
1597 cupsFilePuts(temp
, " # Restrict access to the configuration "
1600 cupsFilePuts(temp
, " Order allow,deny\n");
1603 cupsFilePrintf(temp
, " Allow %s\n",
1604 remote_any
> 0 ? "all" : "@LOCAL");
1606 else if (in_log_location
&& remote_admin
>= 0)
1608 wrote_log_location
= 1;
1611 cupsFilePuts(temp
, " # Allow remote access to the log "
1614 cupsFilePuts(temp
, " # Restrict access to the log "
1617 cupsFilePuts(temp
, " Order allow,deny\n");
1620 cupsFilePrintf(temp
, " Allow %s\n",
1621 remote_any
> 0 ? "all" : "@LOCAL");
1623 else if (in_root_location
&&
1624 (remote_admin
>= 0 || remote_any
>= 0 || share_printers
>= 0))
1626 wrote_root_location
= 1;
1628 if (remote_admin
> 0 && share_printers
> 0)
1629 cupsFilePuts(temp
, " # Allow shared printing and remote "
1630 "administration...\n");
1631 else if (remote_admin
> 0)
1632 cupsFilePuts(temp
, " # Allow remote administration...\n");
1633 else if (share_printers
> 0)
1634 cupsFilePuts(temp
, " # Allow shared printing...\n");
1635 else if (remote_any
> 0)
1636 cupsFilePuts(temp
, " # Allow remote access...\n");
1638 cupsFilePuts(temp
, " # Restrict access to the server...\n");
1640 cupsFilePuts(temp
, " Order allow,deny\n");
1642 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1643 cupsFilePrintf(temp
, " Allow %s\n",
1644 remote_any
> 0 ? "all" : "@LOCAL");
1647 in_admin_location
= 0;
1648 in_conf_location
= 0;
1649 in_log_location
= 0;
1650 in_root_location
= 0;
1652 cupsFilePuts(temp
, "</Location>\n");
1654 else if (!_cups_strcasecmp(line
, "<Limit"))
1656 if (in_default_policy
)
1659 * See if the policy limit is for the Cancel-Job operation...
1662 char *valptr
; /* Pointer into value */
1665 if (!_cups_strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1668 * Don't write anything for this limit section...
1675 cupsFilePrintf(temp
, "%*s%s", indent
, "", line
);
1679 for (valptr
= value
; *valptr
&& !_cups_isspace(*valptr
); valptr
++);
1684 if (!_cups_strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1687 * Write everything except for this definition...
1693 cupsFilePrintf(temp
, " %s", value
);
1695 for (value
= valptr
; _cups_isspace(*value
); value
++);
1698 cupsFilePuts(temp
, ">\n");
1702 cupsFilePrintf(temp
, "%*s%s %s>\n", indent
, "", line
, value
);
1706 else if (!_cups_strcasecmp(line
, "</Limit>") && in_cancel_job
)
1710 if (in_cancel_job
== 1)
1711 cupsFilePuts(temp
, " </Limit>\n");
1715 if (!user_cancel_any
)
1716 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1718 " <Limit Cancel-Job>\n"
1719 " Order deny,allow\n"
1720 " Require user @OWNER "
1721 CUPS_DEFAULT_PRINTOPERATOR_AUTH
"\n"
1726 else if ((((in_admin_location
|| in_conf_location
|| in_root_location
) &&
1727 (remote_admin
>= 0 || remote_any
>= 0)) ||
1728 (in_root_location
&& share_printers
>= 0)) &&
1729 (!_cups_strcasecmp(line
, "Allow") || !_cups_strcasecmp(line
, "Deny") ||
1730 !_cups_strcasecmp(line
, "Order")))
1732 else if (in_cancel_job
== 2)
1734 else if (line
[0] == '<')
1738 cupsFilePrintf(temp
, "%*s%s %s>\n", indent
, "", line
, value
);
1746 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1749 else if (!in_policy
&& !in_location
&&
1750 (val
= cupsGetOption(line
, num_settings
, settings
)) != NULL
)
1753 * Replace this directive's value with the new one...
1756 cupsd_num_settings
= cupsAddOption(line
, val
, cupsd_num_settings
,
1760 * Write the new value in its place, without indentation since we
1761 * only support setting root directives, not in sections...
1764 cupsFilePrintf(temp
, "%s %s\n", line
, val
);
1768 if (!in_policy
&& !in_location
)
1771 * Record the non-policy, non-location directives that we find
1772 * in the server settings, since we cache this info and record it
1773 * in cupsAdminGetServerSettings()...
1776 cupsd_num_settings
= cupsAddOption(line
, value
, cupsd_num_settings
,
1780 cupsFilePrintf(temp
, "%*s%s %s\n", indent
, "", line
, value
);
1783 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1787 * Write any missing info...
1790 if (!wrote_browsing
&& share_printers
>= 0)
1792 if (share_printers
> 0)
1794 cupsFilePuts(temp
, "# Share local printers on the local network.\n");
1795 cupsFilePuts(temp
, "Browsing On\n");
1799 cupsFilePuts(temp
, "# Disable printer sharing and shared printers.\n");
1800 cupsFilePuts(temp
, "Browsing Off\n");
1804 if (!wrote_loglevel
&& debug_logging
>= 0)
1808 cupsFilePuts(temp
, "# Show troubleshooting information in error_log.\n");
1809 cupsFilePuts(temp
, "LogLevel debug\n");
1813 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1814 cupsFilePuts(temp
, "LogLevel " CUPS_DEFAULT_LOG_LEVEL
"\n");
1818 if (!wrote_port_listen
&&
1819 (remote_admin
>= 0 || remote_any
>= 0 || share_printers
>= 0))
1821 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1823 cupsFilePuts(temp
, "# Allow remote access\n");
1824 cupsFilePrintf(temp
, "Port %d\n", ippPort());
1829 "# Only listen for connections from the local machine.\n");
1830 cupsFilePrintf(temp
, "Listen localhost:%d\n", ippPort());
1833 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1834 if (!access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1835 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1836 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1839 if (!wrote_root_location
&&
1840 (remote_admin
>= 0 || remote_any
>= 0 || share_printers
>= 0))
1842 if (remote_admin
> 0 && share_printers
> 0)
1844 "# Allow shared printing and remote administration...\n");
1845 else if (remote_admin
> 0)
1846 cupsFilePuts(temp
, "# Allow remote administration...\n");
1847 else if (share_printers
> 0)
1848 cupsFilePuts(temp
, "# Allow shared printing...\n");
1849 else if (remote_any
> 0)
1850 cupsFilePuts(temp
, "# Allow remote access...\n");
1852 cupsFilePuts(temp
, "# Restrict access to the server...\n");
1854 cupsFilePuts(temp
, "<Location />\n"
1855 " Order allow,deny\n");
1857 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1858 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1860 cupsFilePuts(temp
, "</Location>\n");
1863 if (!wrote_admin_location
&& remote_admin
>= 0)
1866 cupsFilePuts(temp
, "# Allow remote administration...\n");
1868 cupsFilePuts(temp
, "# Restrict access to the admin pages...\n");
1870 cupsFilePuts(temp
, "<Location /admin>\n"
1871 " Order allow,deny\n");
1874 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1876 cupsFilePuts(temp
, "</Location>\n");
1879 if (!wrote_conf_location
&& remote_admin
>= 0)
1883 "# Allow remote access to the configuration files...\n");
1885 cupsFilePuts(temp
, "# Restrict access to the configuration files...\n");
1887 cupsFilePuts(temp
, "<Location /admin/conf>\n"
1888 " AuthType Default\n"
1889 " Require user @SYSTEM\n"
1890 " Order allow,deny\n");
1893 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1895 cupsFilePuts(temp
, "</Location>\n");
1898 if (!wrote_log_location
&& remote_admin
>= 0)
1902 "# Allow remote access to the log files...\n");
1904 cupsFilePuts(temp
, "# Restrict access to the log files...\n");
1906 cupsFilePuts(temp
, "<Location /admin/log>\n"
1907 " AuthType Default\n"
1908 " Require user @SYSTEM\n"
1909 " Order allow,deny\n");
1912 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1914 cupsFilePuts(temp
, "</Location>\n");
1917 if (!wrote_policy
&& user_cancel_any
>= 0)
1919 cupsFilePuts(temp
, "<Policy default>\n"
1920 " # Job-related operations must be done by the owner "
1921 "or an administrator...\n"
1922 " <Limit Send-Document Send-URI Hold-Job Release-Job "
1923 "Restart-Job Purge-Jobs Set-Job-Attributes "
1924 "Create-Job-Subscription Renew-Subscription "
1925 "Cancel-Subscription Get-Notifications Reprocess-Job "
1926 "Cancel-Current-Job Suspend-Current-Job Resume-Job "
1928 " Require user @OWNER @SYSTEM\n"
1929 " Order deny,allow\n"
1931 " # All administration operations require an "
1932 "administrator to authenticate...\n"
1933 " <Limit Pause-Printer Resume-Printer "
1934 "Set-Printer-Attributes Enable-Printer "
1935 "Disable-Printer Pause-Printer-After-Current-Job "
1936 "Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer "
1937 "Activate-Printer Restart-Printer Shutdown-Printer "
1938 "Startup-Printer Promote-Job Schedule-Job-After "
1939 "CUPS-Add-Printer CUPS-Delete-Printer "
1940 "CUPS-Add-Class CUPS-Delete-Class "
1941 "CUPS-Accept-Jobs CUPS-Reject-Jobs "
1942 "CUPS-Set-Default CUPS-Add-Device CUPS-Delete-Device>\n"
1943 " AuthType Default\n"
1944 " Require user @SYSTEM\n"
1945 " Order deny,allow\n"
1948 if (!user_cancel_any
)
1949 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1951 " <Limit Cancel-Job>\n"
1952 " Order deny,allow\n"
1953 " Require user @OWNER "
1954 CUPS_DEFAULT_PRINTOPERATOR_AUTH
"\n"
1957 cupsFilePuts(temp
, " <Limit All>\n"
1958 " Order deny,allow\n"
1963 for (i
= num_settings
, setting
= settings
; i
> 0; i
--, setting
++)
1964 if (setting
->name
[0] != '_' &&
1965 _cups_strcasecmp(setting
->name
, "Listen") &&
1966 _cups_strcasecmp(setting
->name
, "Port") &&
1967 !cupsGetOption(setting
->name
, cupsd_num_settings
, cupsd_settings
))
1970 * Add this directive to the list of directives we have written...
1973 cupsd_num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1974 cupsd_num_settings
, &cupsd_settings
);
1977 * Write the new value, without indentation since we only support
1978 * setting root directives, not in sections...
1981 cupsFilePrintf(temp
, "%s %s\n", setting
->name
, setting
->value
);
1984 cupsFileClose(cupsd
);
1985 cupsFileClose(temp
);
1988 * Upload the configuration file to the server...
1991 status
= cupsPutFile(http
, "/admin/conf/cupsd.conf", tempfile
);
1993 if (status
== HTTP_STATUS_CREATED
)
1996 * Updated OK, add the basic settings...
1999 if (debug_logging
>= 0)
2000 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
2001 debug_logging
? "1" : "0",
2002 cupsd_num_settings
, &cupsd_settings
);
2004 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
2005 old_debug_logging
? "1" : "0",
2006 cupsd_num_settings
, &cupsd_settings
);
2008 if (remote_admin
>= 0)
2009 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
2010 remote_admin
? "1" : "0",
2011 cupsd_num_settings
, &cupsd_settings
);
2013 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
2014 old_remote_admin
? "1" : "0",
2015 cupsd_num_settings
, &cupsd_settings
);
2017 if (remote_any
>= 0)
2018 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
2019 remote_any
? "1" : "0",
2020 cupsd_num_settings
, &cupsd_settings
);
2022 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
2023 old_remote_any
? "1" : "0",
2024 cupsd_num_settings
, &cupsd_settings
);
2026 if (share_printers
>= 0)
2027 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
2028 share_printers
? "1" : "0",
2029 cupsd_num_settings
, &cupsd_settings
);
2031 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
2032 old_share_printers
? "1" : "0",
2033 cupsd_num_settings
, &cupsd_settings
);
2035 if (user_cancel_any
>= 0)
2036 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
2037 user_cancel_any
? "1" : "0",
2038 cupsd_num_settings
, &cupsd_settings
);
2040 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
2041 old_user_cancel_any
? "1" : "0",
2042 cupsd_num_settings
, &cupsd_settings
);
2045 * Save the new values...
2048 invalidate_cupsd_cache(cg
);
2050 cg
->cupsd_num_settings
= cupsd_num_settings
;
2051 cg
->cupsd_settings
= cupsd_settings
;
2052 cg
->cupsd_update
= time(NULL
);
2054 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
2057 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
2060 * Remote our temp files and return...
2068 return (status
== HTTP_STATUS_CREATED
);
2073 * 'do_samba_command()' - Do a SAMBA command.
2076 static int /* O - Status of command */
2077 do_samba_command(const char *command
, /* I - Command to run */
2078 const char *address
, /* I - Address for command */
2079 const char *subcmd
, /* I - Sub-command */
2080 const char *authfile
, /* I - Samba authentication file */
2081 FILE *logfile
) /* I - Optional log file */
2084 return (1); /* Always fail on Windows... */
2087 int status
; /* Status of command */
2088 int pid
; /* Process ID of child */
2092 _cupsLangPrintf(logfile
,
2093 _("Running command: %s %s -N -A %s -c \'%s\'"),
2094 command
, address
, authfile
, subcmd
);
2096 if ((pid
= fork()) == 0)
2099 * Child goes here, redirect stdin/out/err and execute the command...
2102 int fd
= open("/dev/null", O_RDONLY
);
2111 dup2(fileno(logfile
), 1);
2112 else if ((fd
= open("/dev/null", O_WRONLY
)) > 1)
2120 execlp(command
, command
, address
, "-N", "-A", authfile
, "-c", subcmd
,
2129 _cupsLangPrintf(logfile
, _("Unable to run \"%s\": %s"),
2130 command
, strerror(errno
));
2135 * Wait for the process to complete...
2138 while (wait(&status
) != pid
);
2142 _cupsLangPuts(logfile
, "");
2144 DEBUG_printf(("9do_samba_command: status=%d", status
));
2146 if (WIFEXITED(status
))
2147 return (WEXITSTATUS(status
));
2149 return (-WTERMSIG(status
));
2155 * 'get_cupsd_conf()' - Get the current cupsd.conf file.
2158 static http_status_t
/* O - Status of request */
2160 http_t
*http
, /* I - Connection to server */
2161 _cups_globals_t
*cg
, /* I - Global data */
2162 time_t last_update
, /* I - Last update time for file */
2163 char *name
, /* I - Filename buffer */
2164 size_t namesize
, /* I - Size of filename buffer */
2165 int *remote
) /* O - Remote file? */
2167 int fd
; /* Temporary file descriptor */
2169 struct stat info
; /* cupsd.conf file information */
2171 http_status_t status
; /* Status of getting cupsd.conf */
2172 char host
[HTTP_MAX_HOST
]; /* Hostname for connection */
2176 * See if we already have the data we need...
2179 httpGetHostname(http
, host
, sizeof(host
));
2181 if (_cups_strcasecmp(cg
->cupsd_hostname
, host
))
2182 invalidate_cupsd_cache(cg
);
2184 snprintf(name
, namesize
, "%s/cupsd.conf", cg
->cups_serverroot
);
2188 if (!_cups_strcasecmp(host
, "localhost") && !access(name
, R_OK
))
2191 * Read the local file rather than using HTTP...
2194 if (stat(name
, &info
))
2196 char message
[1024]; /* Message string */
2199 snprintf(message
, sizeof(message
),
2200 _cupsLangString(cupsLangDefault(), _("stat of %s failed: %s")),
2201 name
, strerror(errno
));
2202 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
2206 return (HTTP_STATUS_SERVER_ERROR
);
2208 else if (last_update
&& info
.st_mtime
<= last_update
)
2209 status
= HTTP_STATUS_NOT_MODIFIED
;
2211 status
= HTTP_STATUS_OK
;
2214 #endif /* !_WIN32 */
2217 * Read cupsd.conf via a HTTP GET request...
2220 if ((fd
= cupsTempFd(name
, (int)namesize
)) < 0)
2224 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
2226 invalidate_cupsd_cache(cg
);
2228 return (HTTP_STATUS_SERVER_ERROR
);
2233 httpClearFields(http
);
2236 httpSetField(http
, HTTP_FIELD_IF_MODIFIED_SINCE
,
2237 httpGetDateString(last_update
));
2239 status
= cupsGetFd(http
, "/admin/conf/cupsd.conf", fd
);
2243 if (status
!= HTTP_STATUS_OK
)
2255 * 'invalidate_cupsd_cache()' - Invalidate the cached cupsd.conf settings.
2259 invalidate_cupsd_cache(
2260 _cups_globals_t
*cg
) /* I - Global data */
2262 cupsFreeOptions(cg
->cupsd_num_settings
, cg
->cupsd_settings
);
2264 cg
->cupsd_hostname
[0] = '\0';
2265 cg
->cupsd_update
= 0;
2266 cg
->cupsd_num_settings
= 0;
2267 cg
->cupsd_settings
= NULL
;
2272 * 'write_option()' - Write a CUPS option to a PPD file.
2276 write_option(cups_file_t
*dstfp
, /* I - PPD file */
2277 int order
, /* I - Order dependency */
2278 const char *name
, /* I - Option name */
2279 const char *text
, /* I - Option text */
2280 const char *attrname
, /* I - Attribute name */
2281 ipp_attribute_t
*suppattr
, /* I - IPP -supported attribute */
2282 ipp_attribute_t
*defattr
, /* I - IPP -default attribute */
2283 int defval
, /* I - Default value number */
2284 int valcount
) /* I - Number of values */
2286 int i
; /* Looping var */
2289 cupsFilePrintf(dstfp
, "*JCLOpenUI *%s/%s: PickOne\n"
2290 "*OrderDependency: %d JCLSetup *%s\n",
2291 name
, text
, order
, name
);
2293 if (defattr
->value_tag
== IPP_TAG_INTEGER
)
2296 * Do numeric options with a range or list...
2299 cupsFilePrintf(dstfp
, "*Default%s: %d\n", name
,
2300 defattr
->values
[defval
].integer
);
2302 if (suppattr
->value_tag
== IPP_TAG_RANGE
)
2305 * List each number in the range...
2308 for (i
= suppattr
->values
[0].range
.lower
;
2309 i
<= suppattr
->values
[0].range
.upper
;
2312 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, i
);
2315 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n",
2317 else if (defval
== 0)
2318 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
, i
);
2319 else if (defval
< (valcount
- 1))
2320 cupsFilePrintf(dstfp
, ",%d\"\n", i
);
2322 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", i
);
2328 * List explicit numbers...
2331 for (i
= 0; i
< suppattr
->num_values
; i
++)
2333 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, suppattr
->values
[i
].integer
);
2336 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n", attrname
,
2337 suppattr
->values
[i
].integer
);
2338 else if (defval
== 0)
2339 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
,
2340 suppattr
->values
[i
].integer
);
2341 else if (defval
< (valcount
- 1))
2342 cupsFilePrintf(dstfp
, ",%d\"\n", suppattr
->values
[i
].integer
);
2344 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", suppattr
->values
[i
].integer
);
2351 * Do text options with a list...
2354 cupsFilePrintf(dstfp
, "*Default%s: %s\n", name
,
2355 defattr
->values
[defval
].string
.text
);
2357 for (i
= 0; i
< suppattr
->num_values
; i
++)
2359 cupsFilePrintf(dstfp
, "*%s %s: \"", name
,
2360 suppattr
->values
[i
].string
.text
);
2363 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\n\"\n*End\n", attrname
,
2364 suppattr
->values
[i
].string
.text
);
2365 else if (defval
== 0)
2366 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\"\n", attrname
,
2367 suppattr
->values
[i
].string
.text
);
2368 else if (defval
< (valcount
- 1))
2369 cupsFilePrintf(dstfp
, ",%s\"\n", suppattr
->values
[i
].string
.text
);
2371 cupsFilePrintf(dstfp
, ",%s\n\"\n*End\n",
2372 suppattr
->values
[i
].string
.text
);
2376 cupsFilePrintf(dstfp
, "*JCLCloseUI: *%s\n\n", name
);