2 * Administration utility API definitions for CUPS.
4 * Copyright 2007-2015 by Apple Inc.
5 * Copyright 2001-2007 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
20 #include "cups-private.h"
22 #include "adminutil.h"
28 # include <sys/wait.h>
36 static int do_samba_command(const char *command
,
38 const char *subcommand
,
41 static http_status_t
get_cupsd_conf(http_t
*http
, _cups_globals_t
*cg
,
42 time_t last_update
, char *name
,
43 size_t namelen
, int *remote
);
44 static void invalidate_cupsd_cache(_cups_globals_t
*cg
);
45 static void write_option(cups_file_t
*dstfp
, int order
,
46 const char *name
, const char *text
,
48 ipp_attribute_t
*suppattr
,
49 ipp_attribute_t
*defattr
, int defval
,
54 * 'cupsAdminCreateWindowsPPD()' - Create the Windows PPD file for a printer.
59 char * /* O - PPD file or NULL */
60 cupsAdminCreateWindowsPPD(
61 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
62 const char *dest
, /* I - Printer or class */
63 char *buffer
, /* I - Filename buffer */
64 int bufsize
) /* I - Size of filename buffer */
66 const char *src
; /* Source PPD filename */
67 cups_file_t
*srcfp
, /* Source PPD file */
68 *dstfp
; /* Destination PPD file */
69 ipp_t
*request
, /* IPP request */
70 *response
; /* IPP response */
71 ipp_attribute_t
*suppattr
, /* IPP -supported attribute */
72 *defattr
; /* IPP -default attribute */
73 cups_lang_t
*language
; /* Current language */
74 char line
[256], /* Line from PPD file */
75 junk
[256], /* Extra junk to throw away */
76 *ptr
, /* Pointer into line */
77 uri
[1024], /* Printer URI */
78 option
[41], /* Option */
79 choice
[41]; /* Choice */
80 int jcloption
, /* In a JCL option? */
81 jclorder
, /* Next JCL order dependency */
82 linenum
; /* Current line number */
83 time_t curtime
; /* Current time */
84 struct tm
*curdate
; /* Current date */
85 static const char * const pattrs
[] = /* Printer attributes we want */
87 "job-hold-until-supported",
88 "job-hold-until-default",
89 "job-sheets-supported",
91 "job-priority-supported",
92 "job-priority-default"
97 * Range check the input...
104 http
= _cupsConnect();
106 if (!http
|| !dest
|| !buffer
|| bufsize
< 2)
110 * Get the PPD file...
113 if ((src
= cupsGetPPD2(http
, dest
)) == NULL
)
117 * Get the supported banner pages, etc. for the printer...
120 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
122 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
123 "localhost", 0, "/printers/%s", dest
);
124 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
125 "printer-uri", NULL
, uri
);
127 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
128 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
132 * Do the request and get back a response...
135 response
= cupsDoRequest(http
, request
, "/");
136 if (!response
|| cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
143 * Open the original PPD file...
146 if ((srcfp
= cupsFileOpen(src
, "rb")) == NULL
)
150 * Create a temporary output file using the destination buffer...
153 if ((dstfp
= cupsTempFile2(buffer
, bufsize
)) == NULL
)
155 cupsFileClose(srcfp
);
163 * Write a new header explaining that this isn't the original PPD...
166 cupsFilePuts(dstfp
, "*PPD-Adobe: \"4.3\"\n");
168 curtime
= time(NULL
);
169 curdate
= gmtime(&curtime
);
171 cupsFilePrintf(dstfp
, "*%% Modified on %04d%02d%02d%02d%02d%02d+0000 "
172 "for CUPS Windows Driver\n",
173 curdate
->tm_year
+ 1900, curdate
->tm_mon
+ 1, curdate
->tm_mday
,
174 curdate
->tm_hour
, curdate
->tm_min
, curdate
->tm_sec
);
177 * Read the existing PPD file, converting all PJL commands to CUPS
178 * job ticket comments...
184 language
= cupsLangDefault();
186 while (cupsFileGets(srcfp
, line
, sizeof(line
)))
190 if (!strncmp(line
, "*PPD-Adobe:", 11))
193 * Already wrote the PPD header...
198 else if (!strncmp(line
, "*JCLBegin:", 10) ||
199 !strncmp(line
, "*JCLToPSInterpreter:", 20) ||
200 !strncmp(line
, "*JCLEnd:", 8) ||
201 !strncmp(line
, "*Protocols:", 11))
204 * Don't use existing JCL keywords; we'll create our own, below...
207 cupsFilePrintf(dstfp
, "*%% Commented out for CUPS Windows Driver...\n"
208 "*%%%s\n", line
+ 1);
211 else if (!strncmp(line
, "*JCLOpenUI", 10))
214 cupsFilePrintf(dstfp
, "%s\n", line
);
216 else if (!strncmp(line
, "*JCLCloseUI", 11))
219 cupsFilePrintf(dstfp
, "%s\n", line
);
221 else if (jcloption
&& !strncmp(line
, "*OrderDependency:", 17))
223 for (ptr
= line
+ 17; _cups_isspace(*ptr
); ptr
++);
225 ptr
= strchr(ptr
, ' ');
229 cupsFilePrintf(dstfp
, "*OrderDependency: %d%s\n", jclorder
, ptr
);
233 cupsFilePrintf(dstfp
, "%s\n", line
);
235 else if (jcloption
&&
236 strncmp(line
, "*End", 4) &&
237 strncmp(line
, "*Default", 8))
239 if ((ptr
= strchr(line
, ':')) == NULL
)
241 snprintf(line
, sizeof(line
),
242 _cupsLangString(language
, _("Missing value on line %d.")),
244 _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR
, line
, 0);
246 cupsFileClose(srcfp
);
247 cupsFileClose(dstfp
);
257 if ((ptr
= strchr(ptr
, '\"')) == NULL
)
259 snprintf(line
, sizeof(line
),
260 _cupsLangString(language
,
261 _("Missing double quote on line %d.")),
263 _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR
, line
, 0);
265 cupsFileClose(srcfp
);
266 cupsFileClose(dstfp
);
276 if (sscanf(line
, "*%40s%*[ \t]%40[^:/]", option
, choice
) != 2)
278 snprintf(line
, sizeof(line
),
279 _cupsLangString(language
,
280 _("Bad option + choice on line %d.")),
282 _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR
, line
, 0);
284 cupsFileClose(srcfp
);
285 cupsFileClose(dstfp
);
295 if (strchr(ptr
+ 1, '\"') == NULL
)
301 while (cupsFileGets(srcfp
, junk
, sizeof(junk
)) != NULL
)
305 if (!strncmp(junk
, "*End", 4))
310 snprintf(ptr
+ 1, sizeof(line
) - (size_t)(ptr
- line
+ 1),
311 "%%cupsJobTicket: %s=%s\n\"\n*End", option
, choice
);
313 cupsFilePrintf(dstfp
, "*%% Changed for CUPS Windows Driver...\n%s\n",
317 cupsFilePrintf(dstfp
, "%s\n", line
);
320 cupsFileClose(srcfp
);
325 _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR
, _("Empty PPD file."), 1);
327 cupsFileClose(dstfp
);
336 * Now add the CUPS-specific attributes and options...
339 cupsFilePuts(dstfp
, "\n*% CUPS Job Ticket support and options...\n");
340 cupsFilePuts(dstfp
, "*Protocols: PJL\n");
341 cupsFilePuts(dstfp
, "*JCLBegin: \"%!PS-Adobe-3.0<0A>\"\n");
342 cupsFilePuts(dstfp
, "*JCLToPSInterpreter: \"\"\n");
343 cupsFilePuts(dstfp
, "*JCLEnd: \"\"\n");
345 cupsFilePuts(dstfp
, "\n*OpenGroup: CUPS/CUPS Options\n\n");
347 if ((defattr
= ippFindAttribute(response
, "job-hold-until-default",
348 IPP_TAG_ZERO
)) != NULL
&&
349 (suppattr
= ippFindAttribute(response
, "job-hold-until-supported",
350 IPP_TAG_ZERO
)) != NULL
)
351 write_option(dstfp
, jclorder
++, "cupsJobHoldUntil", "Hold Until",
352 "job-hold-until", suppattr
, defattr
, 0, 1);
354 if ((defattr
= ippFindAttribute(response
, "job-priority-default",
355 IPP_TAG_INTEGER
)) != NULL
&&
356 (suppattr
= ippFindAttribute(response
, "job-priority-supported",
357 IPP_TAG_RANGE
)) != NULL
)
358 write_option(dstfp
, jclorder
++, "cupsJobPriority", "Priority",
359 "job-priority", suppattr
, defattr
, 0, 1);
361 if ((defattr
= ippFindAttribute(response
, "job-sheets-default",
362 IPP_TAG_ZERO
)) != NULL
&&
363 (suppattr
= ippFindAttribute(response
, "job-sheets-supported",
364 IPP_TAG_ZERO
)) != NULL
)
366 write_option(dstfp
, jclorder
++, "cupsJobSheetsStart", "Start Banner",
367 "job-sheets", suppattr
, defattr
, 0, 2);
368 write_option(dstfp
, jclorder
, "cupsJobSheetsEnd", "End Banner",
369 "job-sheets", suppattr
, defattr
, 1, 2);
372 cupsFilePuts(dstfp
, "*CloseGroup: CUPS\n");
373 cupsFileClose(dstfp
);
382 * 'cupsAdminExportSamba()' - Export a printer to Samba.
387 int /* O - 1 on success, 0 on failure */
388 cupsAdminExportSamba(
389 const char *dest
, /* I - Destination to export */
390 const char *ppd
, /* I - PPD file */
391 const char *samba_server
, /* I - Samba server */
392 const char *samba_user
, /* I - Samba username */
393 const char *samba_password
, /* I - Samba password */
394 FILE *logfile
) /* I - Log file, if any */
396 int status
; /* Status of Samba commands */
397 int have_drivers
; /* Have drivers? */
398 char file
[1024], /* File to test for */
399 authfile
[1024], /* Temporary authentication file */
400 address
[1024], /* Address for command */
401 subcmd
[1024], /* Sub-command */
402 message
[1024]; /* Error message */
403 cups_file_t
*fp
; /* Authentication file */
404 cups_lang_t
*language
; /* Current language */
405 _cups_globals_t
*cg
= _cupsGlobals();
410 * Range check input...
413 if (!dest
|| !ppd
|| !samba_server
|| !samba_user
|| !samba_password
)
415 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
420 * Create a temporary authentication file for Samba...
423 if ((fp
= cupsTempFile2(authfile
, sizeof(authfile
))) == NULL
)
425 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
429 cupsFilePrintf(fp
, "username = %s\n", samba_user
);
430 cupsFilePrintf(fp
, "password = %s\n", samba_password
);
434 * See which drivers are available; the new CUPS v6 and Adobe drivers
435 * depend on the Windows 2k PS driver, so copy that driver first:
446 language
= cupsLangDefault();
448 snprintf(file
, sizeof(file
), "%s/drivers/pscript5.dll", cg
->cups_datadir
);
449 if (!access(file
, 0))
454 * Windows 2k driver is installed; do the smbclient commands needed
455 * to copy the Win2k drivers over...
458 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
460 snprintf(subcmd
, sizeof(subcmd
),
462 "put %s W32X86/%s.ppd;"
463 "put %s/drivers/ps5ui.dll W32X86/ps5ui.dll;"
464 "put %s/drivers/pscript.hlp W32X86/pscript.hlp;"
465 "put %s/drivers/pscript.ntf W32X86/pscript.ntf;"
466 "put %s/drivers/pscript5.dll W32X86/pscript5.dll",
467 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
468 cg
->cups_datadir
, cg
->cups_datadir
);
470 if ((status
= do_samba_command("smbclient", address
, subcmd
,
471 authfile
, logfile
)) != 0)
473 snprintf(message
, sizeof(message
),
474 _cupsLangString(language
,
475 _("Unable to copy Windows 2000 printer "
476 "driver files (%d).")), status
);
478 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
481 _cupsLangPuts(logfile
, message
);
489 * See if we also have the CUPS driver files; if so, use them!
492 snprintf(file
, sizeof(file
), "%s/drivers/cupsps6.dll", cg
->cups_datadir
);
493 if (!access(file
, 0))
496 * Copy the CUPS driver files over...
499 snprintf(subcmd
, sizeof(subcmd
),
500 "put %s/drivers/cups6.ini W32X86/cups6.ini;"
501 "put %s/drivers/cupsps6.dll W32X86/cupsps6.dll;"
502 "put %s/drivers/cupsui6.dll W32X86/cupsui6.dll",
503 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
505 if ((status
= do_samba_command("smbclient", address
, subcmd
,
506 authfile
, logfile
)) != 0)
508 snprintf(message
, sizeof(message
),
509 _cupsLangString(language
,
510 _("Unable to copy CUPS printer driver "
511 "files (%d).")), status
);
513 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
516 _cupsLangPuts(logfile
, message
);
524 * Do the rpcclient command needed for the CUPS drivers...
527 snprintf(subcmd
, sizeof(subcmd
),
528 "adddriver \"Windows NT x86\" \"%s:"
529 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
530 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
531 "cups6.ini,cupsps6.dll,cupsui6.dll\"",
537 * Don't have the CUPS drivers, so just use the standard Windows
541 snprintf(subcmd
, sizeof(subcmd
),
542 "adddriver \"Windows NT x86\" \"%s:"
543 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
544 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
548 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
549 authfile
, logfile
)) != 0)
551 snprintf(message
, sizeof(message
),
552 _cupsLangString(language
,
553 _("Unable to install Windows 2000 printer "
554 "driver files (%d).")), status
);
556 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
559 _cupsLangPuts(logfile
, message
);
568 * See if we have the Win9x PS driver...
571 snprintf(file
, sizeof(file
), "%s/drivers/ADOBEPS4.DRV", cg
->cups_datadir
);
572 if (!access(file
, 0))
577 * Do the smbclient commands needed for the Adobe Win9x drivers...
580 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
582 snprintf(subcmd
, sizeof(subcmd
),
584 "put %s WIN40/%s.PPD;"
585 "put %s/drivers/ADFONTS.MFM WIN40/ADFONTS.MFM;"
586 "put %s/drivers/ADOBEPS4.DRV WIN40/ADOBEPS4.DRV;"
587 "put %s/drivers/ADOBEPS4.HLP WIN40/ADOBEPS4.HLP;"
588 "put %s/drivers/ICONLIB.DLL WIN40/ICONLIB.DLL;"
589 "put %s/drivers/PSMON.DLL WIN40/PSMON.DLL;",
590 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
591 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
593 if ((status
= do_samba_command("smbclient", address
, subcmd
,
594 authfile
, logfile
)) != 0)
596 snprintf(message
, sizeof(message
),
597 _cupsLangString(language
,
598 _("Unable to copy Windows 9x printer "
599 "driver files (%d).")), status
);
601 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
604 _cupsLangPuts(logfile
, message
);
612 * Do the rpcclient commands needed for the Adobe Win9x drivers...
615 snprintf(subcmd
, sizeof(subcmd
),
616 "adddriver \"Windows 4.0\" \"%s:ADOBEPS4.DRV:%s.PPD:NULL:"
617 "ADOBEPS4.HLP:PSMON.DLL:RAW:"
618 "ADOBEPS4.DRV,%s.PPD,ADOBEPS4.HLP,PSMON.DLL,ADFONTS.MFM,"
622 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
623 authfile
, logfile
)) != 0)
625 snprintf(message
, sizeof(message
),
626 _cupsLangString(language
,
627 _("Unable to install Windows 9x printer "
628 "driver files (%d).")), status
);
630 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
633 _cupsLangPuts(logfile
, message
);
642 * See if we have the 64-bit Windows PS driver...
652 snprintf(file
, sizeof(file
), "%s/drivers/x64/pscript5.dll", cg
->cups_datadir
);
653 if (!access(file
, 0))
658 * 64-bit Windows driver is installed; do the smbclient commands needed
659 * to copy the Win64 drivers over...
662 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
664 snprintf(subcmd
, sizeof(subcmd
),
667 "put %s/drivers/x64/ps5ui.dll x64/ps5ui.dll;"
668 "put %s/drivers/x64/pscript.hlp x64/pscript.hlp;"
669 "put %s/drivers/x64/pscript.ntf x64/pscript.ntf;"
670 "put %s/drivers/x64/pscript5.dll x64/pscript5.dll",
671 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
672 cg
->cups_datadir
, cg
->cups_datadir
);
674 if ((status
= do_samba_command("smbclient", address
, subcmd
,
675 authfile
, logfile
)) != 0)
677 snprintf(message
, sizeof(message
),
678 _cupsLangString(language
,
679 _("Unable to copy 64-bit Windows printer "
680 "driver files (%d).")), status
);
682 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
685 _cupsLangPuts(logfile
, message
);
693 * See if we also have the CUPS driver files; if so, use them!
696 snprintf(file
, sizeof(file
), "%s/drivers/x64/cupsps6.dll", cg
->cups_datadir
);
697 if (!access(file
, 0))
700 * Copy the CUPS driver files over...
703 snprintf(subcmd
, sizeof(subcmd
),
704 "put %s/drivers/x64/cups6.ini x64/cups6.ini;"
705 "put %s/drivers/x64/cupsps6.dll x64/cupsps6.dll;"
706 "put %s/drivers/x64/cupsui6.dll x64/cupsui6.dll",
707 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
709 if ((status
= do_samba_command("smbclient", address
, subcmd
,
710 authfile
, logfile
)) != 0)
712 snprintf(message
, sizeof(message
),
713 _cupsLangString(language
,
714 _("Unable to copy 64-bit CUPS printer driver "
715 "files (%d).")), status
);
717 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
720 _cupsLangPuts(logfile
, message
);
728 * Do the rpcclient command needed for the CUPS drivers...
731 snprintf(subcmd
, sizeof(subcmd
),
732 "adddriver \"Windows x64\" \"%s:"
733 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
734 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
735 "cups6.ini,cupsps6.dll,cupsui6.dll\"",
741 * Don't have the CUPS drivers, so just use the standard Windows
745 snprintf(subcmd
, sizeof(subcmd
),
746 "adddriver \"Windows x64\" \"%s:"
747 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
748 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
752 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
753 authfile
, logfile
)) != 0)
755 snprintf(message
, sizeof(message
),
756 _cupsLangString(language
,
757 _("Unable to install Windows 2000 printer "
758 "driver files (%d).")), status
);
760 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
763 _cupsLangPuts(logfile
, message
);
771 if (logfile
&& !(have_drivers
& 1))
775 _cupsLangString(language
,
776 _("No Windows printer drivers are installed.")),
780 _cupsLangString(language
,
781 _("Warning, no Windows 2000 printer drivers "
785 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, message
, 0);
786 _cupsLangPuts(logfile
, message
);
789 if (have_drivers
== 0)
791 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, message
, 0);
799 * Finally, associate the drivers we just added with the queue...
802 snprintf(subcmd
, sizeof(subcmd
), "setdriver %s %s", dest
, dest
);
804 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
805 authfile
, logfile
)) != 0)
807 snprintf(message
, sizeof(message
),
808 _cupsLangString(language
,
809 _("Unable to set Windows printer driver (%d).")),
812 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
815 _cupsLangPuts(logfile
, message
);
829 * 'cupsAdminGetServerSettings()' - Get settings from the server.
831 * The returned settings should be freed with cupsFreeOptions() when
832 * you are done with them.
834 * @since CUPS 1.3/macOS 10.5@
837 int /* O - 1 on success, 0 on failure */
838 cupsAdminGetServerSettings(
839 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
840 int *num_settings
, /* O - Number of settings */
841 cups_option_t
**settings
) /* O - Settings */
843 int i
; /* Looping var */
844 cups_file_t
*cupsd
; /* cupsd.conf file */
845 char cupsdconf
[1024]; /* cupsd.conf filename */
846 int remote
; /* Remote cupsd.conf file? */
847 http_status_t status
; /* Status of getting cupsd.conf */
848 char line
[1024], /* Line from cupsd.conf file */
849 *value
; /* Value on line */
850 cups_option_t
*setting
; /* Current setting */
851 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
855 * Range check input...
861 * See if we are connected to the same server...
867 * Compare the connection hostname, port, and encryption settings to
868 * the cached defaults; these were initialized the first time we
872 if (strcmp(cg
->http
->hostname
, cg
->server
) ||
873 cg
->ipp_port
!= httpAddrPort(cg
->http
->hostaddr
) ||
874 (cg
->http
->encryption
!= cg
->encryption
&&
875 cg
->http
->encryption
== HTTP_ENCRYPTION_NEVER
))
878 * Need to close the current connection because something has changed...
887 * (Re)connect as needed...
892 if ((cg
->http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
,
893 cupsEncryption(), 1, 0, NULL
)) == NULL
)
896 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
, NULL
, 0);
898 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
,
899 _("Unable to connect to host."), 1);
914 if (!http
|| !num_settings
|| !settings
)
916 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
931 * Get the cupsd.conf file...
934 if ((status
= get_cupsd_conf(http
, cg
, cg
->cupsd_update
, cupsdconf
,
935 sizeof(cupsdconf
), &remote
)) == HTTP_STATUS_OK
)
937 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
939 char message
[1024]; /* Message string */
942 snprintf(message
, sizeof(message
),
943 _cupsLangString(cupsLangDefault(), _("Open of %s failed: %s")),
944 cupsdconf
, strerror(errno
));
945 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
954 * Read the file, keeping track of what settings are enabled...
957 int remote_access
= 0, /* Remote access allowed? */
958 remote_admin
= 0, /* Remote administration allowed? */
959 remote_any
= 0, /* Remote access from anywhere allowed? */
960 browsing
= 1, /* Browsing enabled? */
961 cancel_policy
= 1, /* Cancel-job policy set? */
962 debug_logging
= 0; /* LogLevel debug set? */
963 int linenum
= 0, /* Line number in file */
964 in_location
= 0, /* In a location section? */
965 in_policy
= 0, /* In a policy section? */
966 in_cancel_job
= 0, /* In a cancel-job section? */
967 in_admin_location
= 0; /* In the /admin location? */
970 invalidate_cupsd_cache(cg
);
972 cg
->cupsd_update
= time(NULL
);
973 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
975 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
977 if (!value
&& strncmp(line
, "</", 2))
978 value
= line
+ strlen(line
);
980 if ((!_cups_strcasecmp(line
, "Port") || !_cups_strcasecmp(line
, "Listen")) && value
)
982 char *port
; /* Pointer to port number, if any */
985 if ((port
= strrchr(value
, ':')) != NULL
)
987 else if (isdigit(*value
& 255))
990 * Listen on a port number implies remote access...
997 if (_cups_strcasecmp(value
, "localhost") && strcmp(value
, "127.0.0.1")
1000 #endif /* AF_LOCAL */
1002 && strcmp(value
, "[::1]")
1003 #endif /* AF_INET6 */
1007 else if (!_cups_strcasecmp(line
, "Browsing"))
1009 browsing
= !_cups_strcasecmp(value
, "yes") ||
1010 !_cups_strcasecmp(value
, "on") ||
1011 !_cups_strcasecmp(value
, "true");
1013 else if (!_cups_strcasecmp(line
, "LogLevel"))
1015 debug_logging
= !_cups_strncasecmp(value
, "debug", 5);
1017 else if (!_cups_strcasecmp(line
, "<Policy") &&
1018 !_cups_strcasecmp(value
, "default"))
1022 else if (!_cups_strcasecmp(line
, "</Policy>"))
1026 else if (!_cups_strcasecmp(line
, "<Limit") && in_policy
&& value
)
1029 * See if the policy limit is for the Cancel-Job operation...
1032 char *valptr
; /* Pointer into value */
1037 for (valptr
= value
; *valptr
&& !_cups_isspace(*valptr
); valptr
++);
1042 if (!_cups_strcasecmp(value
, "cancel-job") ||
1043 !_cups_strcasecmp(value
, "all"))
1049 for (value
= valptr
; _cups_isspace(*value
); value
++);
1052 else if (!_cups_strcasecmp(line
, "</Limit>"))
1056 else if (!_cups_strcasecmp(line
, "Require") && in_cancel_job
)
1060 else if (!_cups_strcasecmp(line
, "<Location") && value
)
1062 in_admin_location
= !_cups_strcasecmp(value
, "/admin");
1065 else if (!_cups_strcasecmp(line
, "</Location>"))
1067 in_admin_location
= 0;
1070 else if (!_cups_strcasecmp(line
, "Allow") && value
&&
1071 _cups_strcasecmp(value
, "localhost") &&
1072 _cups_strcasecmp(value
, "127.0.0.1")
1075 #endif /* AF_LOCAL */
1077 && strcmp(value
, "::1")
1078 #endif /* AF_INET6 */
1081 if (in_admin_location
)
1083 else if (!_cups_strcasecmp(value
, "all"))
1086 else if (line
[0] != '<' && !in_location
&& !in_policy
&&
1087 _cups_strcasecmp(line
, "Allow") &&
1088 _cups_strcasecmp(line
, "AuthType") &&
1089 _cups_strcasecmp(line
, "Deny") &&
1090 _cups_strcasecmp(line
, "Order") &&
1091 _cups_strcasecmp(line
, "Require") &&
1092 _cups_strcasecmp(line
, "Satisfy"))
1093 cg
->cupsd_num_settings
= cupsAddOption(line
, value
,
1094 cg
->cupsd_num_settings
,
1095 &(cg
->cupsd_settings
));
1098 cupsFileClose(cupsd
);
1100 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1101 debug_logging
? "1" : "0",
1102 cg
->cupsd_num_settings
,
1103 &(cg
->cupsd_settings
));
1105 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1106 (remote_access
&& remote_admin
) ?
1108 cg
->cupsd_num_settings
,
1109 &(cg
->cupsd_settings
));
1111 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
1112 remote_any
? "1" : "0",
1113 cg
->cupsd_num_settings
,
1114 &(cg
->cupsd_settings
));
1116 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1117 (remote_access
&& browsing
) ? "1" :
1119 cg
->cupsd_num_settings
,
1120 &(cg
->cupsd_settings
));
1122 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1123 cancel_policy
? "1" : "0",
1124 cg
->cupsd_num_settings
,
1125 &(cg
->cupsd_settings
));
1127 else if (status
!= HTTP_STATUS_NOT_MODIFIED
)
1128 invalidate_cupsd_cache(cg
);
1131 * Remove any temporary files and copy the settings array...
1137 for (i
= cg
->cupsd_num_settings
, setting
= cg
->cupsd_settings
;
1140 *num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1141 *num_settings
, settings
);
1143 return (cg
->cupsd_num_settings
> 0);
1148 * 'cupsAdminSetServerSettings()' - Set settings on the server.
1150 * @since CUPS 1.3/macOS 10.5@
1153 int /* O - 1 on success, 0 on failure */
1154 cupsAdminSetServerSettings(
1155 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1156 int num_settings
, /* I - Number of settings */
1157 cups_option_t
*settings
) /* I - Settings */
1159 int i
; /* Looping var */
1160 http_status_t status
; /* GET/PUT status */
1161 const char *server_port_env
; /* SERVER_PORT env var */
1162 int server_port
; /* IPP port for server */
1163 cups_file_t
*cupsd
; /* cupsd.conf file */
1164 char cupsdconf
[1024]; /* cupsd.conf filename */
1165 int remote
; /* Remote cupsd.conf file? */
1166 char tempfile
[1024]; /* Temporary new cupsd.conf */
1167 cups_file_t
*temp
; /* Temporary file */
1168 char line
[1024], /* Line from cupsd.conf file */
1169 *value
; /* Value on line */
1170 int linenum
, /* Line number in file */
1171 in_location
, /* In a location section? */
1172 in_policy
, /* In a policy section? */
1173 in_default_policy
, /* In the default policy section? */
1174 in_cancel_job
, /* In a cancel-job section? */
1175 in_admin_location
, /* In the /admin location? */
1176 in_conf_location
, /* In the /admin/conf location? */
1177 in_log_location
, /* In the /admin/log 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_log_location
, /* Wrote the /admin/log location? */
1192 wrote_root_location
; /* Wrote the / location? */
1193 int indent
; /* Indentation */
1194 int cupsd_num_settings
; /* New number of settings */
1195 int old_share_printers
, /* Share local printers */
1196 old_remote_admin
, /* Remote administration allowed? */
1197 old_user_cancel_any
, /* Cancel-job policy set? */
1198 old_debug_logging
; /* LogLevel debug set? */
1199 cups_option_t
*cupsd_settings
, /* New settings */
1200 *setting
; /* Current setting */
1201 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1205 * Range check input...
1209 http
= _cupsConnect();
1211 if (!http
|| !num_settings
|| !settings
)
1213 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1219 * Get the cupsd.conf file...
1222 if (get_cupsd_conf(http
, cg
, 0, cupsdconf
, sizeof(cupsdconf
),
1223 &remote
) == HTTP_STATUS_OK
)
1225 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
1227 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
1235 * Get current settings...
1238 if (!cupsAdminGetServerSettings(http
, &cupsd_num_settings
,
1242 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, cupsd_num_settings
,
1243 cupsd_settings
)) != NULL
)
1244 old_debug_logging
= atoi(val
);
1246 old_debug_logging
= 0;
1248 DEBUG_printf(("1cupsAdminSetServerSettings: old debug_logging=%d",
1249 old_debug_logging
));
1251 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, cupsd_num_settings
,
1252 cupsd_settings
)) != NULL
)
1253 old_remote_admin
= atoi(val
);
1255 old_remote_admin
= 0;
1257 DEBUG_printf(("1cupsAdminSetServerSettings: old remote_admin=%d",
1260 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, cupsd_num_settings
,
1261 cupsd_settings
)) != NULL
)
1262 remote_any
= atoi(val
);
1266 DEBUG_printf(("1cupsAdminSetServerSettings: old remote_any=%d",
1269 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, cupsd_num_settings
,
1270 cupsd_settings
)) != NULL
)
1271 old_share_printers
= atoi(val
);
1273 old_share_printers
= 0;
1275 DEBUG_printf(("1cupsAdminSetServerSettings: old share_printers=%d",
1276 old_share_printers
));
1278 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, cupsd_num_settings
,
1279 cupsd_settings
)) != NULL
)
1280 old_user_cancel_any
= atoi(val
);
1282 old_user_cancel_any
= 0;
1284 DEBUG_printf(("1cupsAdminSetServerSettings: old user_cancel_any=%d",
1285 old_user_cancel_any
));
1287 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
1290 * Get basic settings...
1293 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, num_settings
,
1296 debug_logging
= atoi(val
);
1298 if (debug_logging
== old_debug_logging
)
1301 * No change to this setting...
1310 DEBUG_printf(("1cupsAdminSetServerSettings: debug_logging=%d",
1313 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, num_settings
,
1315 remote_any
= atoi(val
);
1317 DEBUG_printf(("1cupsAdminSetServerSettings: remote_any=%d",
1320 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, num_settings
,
1323 remote_admin
= atoi(val
);
1325 if (remote_admin
== old_remote_admin
)
1328 * No change to this setting...
1337 DEBUG_printf(("1cupsAdminSetServerSettings: remote_admin=%d",
1340 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, num_settings
,
1343 share_printers
= atoi(val
);
1345 if (share_printers
== old_share_printers
)
1348 * No change to this setting...
1351 share_printers
= -1;
1355 share_printers
= -1;
1357 DEBUG_printf(("1cupsAdminSetServerSettings: share_printers=%d",
1360 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, num_settings
,
1363 user_cancel_any
= atoi(val
);
1365 if (user_cancel_any
== old_user_cancel_any
)
1368 * No change to this setting...
1371 user_cancel_any
= -1;
1375 user_cancel_any
= -1;
1377 DEBUG_printf(("1cupsAdminSetServerSettings: user_cancel_any=%d",
1381 * Create a temporary file for the new cupsd.conf file...
1384 if ((temp
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1386 cupsFileClose(cupsd
);
1391 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
1396 * Copy the old file to the new, making changes along the way...
1399 cupsd_num_settings
= 0;
1400 in_admin_location
= 0;
1402 in_conf_location
= 0;
1403 in_default_policy
= 0;
1405 in_log_location
= 0;
1407 in_root_location
= 0;
1409 wrote_admin_location
= 0;
1411 wrote_conf_location
= 0;
1412 wrote_log_location
= 0;
1415 wrote_port_listen
= 0;
1416 wrote_root_location
= 0;
1419 if ((server_port_env
= getenv("SERVER_PORT")) != NULL
)
1421 if ((server_port
= atoi(server_port_env
)) <= 0)
1422 server_port
= ippPort();
1425 server_port
= ippPort();
1427 if (server_port
<= 0)
1428 server_port
= IPP_PORT
;
1430 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
1432 if ((!_cups_strcasecmp(line
, "Port") || !_cups_strcasecmp(line
, "Listen")) &&
1433 (remote_admin
>= 0 || remote_any
> 0 || share_printers
>= 0))
1435 if (!wrote_port_listen
)
1437 wrote_port_listen
= 1;
1439 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1441 cupsFilePuts(temp
, "# Allow remote access\n");
1442 cupsFilePrintf(temp
, "Port %d\n", server_port
);
1446 cupsFilePuts(temp
, "# Only listen for connections from the local "
1448 cupsFilePrintf(temp
, "Listen localhost:%d\n", server_port
);
1451 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1452 if ((!value
|| strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)) &&
1453 !access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1454 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1455 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1457 else if (value
&& value
[0] == '/'
1458 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1459 && strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)
1460 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1462 cupsFilePrintf(temp
, "Listen %s\n", value
);
1464 else if ((!_cups_strcasecmp(line
, "Browsing") ||
1465 !_cups_strcasecmp(line
, "BrowseLocalProtocols")) &&
1466 share_printers
>= 0)
1468 if (!wrote_browsing
)
1470 int new_share_printers
= (share_printers
> 0 ||
1471 (share_printers
== -1 &&
1472 old_share_printers
> 0));
1476 if (new_share_printers
)
1478 const char *localp
= cupsGetOption("BrowseLocalProtocols",
1479 num_settings
, settings
);
1481 if (!localp
|| !localp
[0])
1482 localp
= cupsGetOption("BrowseLocalProtocols", cupsd_num_settings
,
1485 cupsFilePuts(temp
, "# Share local printers on the local network.\n");
1486 cupsFilePuts(temp
, "Browsing On\n");
1489 localp
= CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS
;
1491 cupsFilePrintf(temp
, "BrowseLocalProtocols %s\n", localp
);
1493 cupsd_num_settings
= cupsAddOption("BrowseLocalProtocols", localp
,
1499 cupsFilePuts(temp
, "# Disable printer sharing.\n");
1500 cupsFilePuts(temp
, "Browsing Off\n");
1504 else if (!_cups_strcasecmp(line
, "LogLevel") && debug_logging
>= 0)
1511 "# Show troubleshooting information in error_log.\n");
1512 cupsFilePuts(temp
, "LogLevel debug\n");
1516 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1517 cupsFilePuts(temp
, "LogLevel " CUPS_DEFAULT_LOG_LEVEL
"\n");
1520 else if (!_cups_strcasecmp(line
, "<Policy"))
1522 in_default_policy
= !_cups_strcasecmp(value
, "default");
1525 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1528 else if (!_cups_strcasecmp(line
, "</Policy>"))
1531 if (!wrote_policy
&& in_default_policy
)
1535 if (!user_cancel_any
)
1536 cupsFilePuts(temp
, " # Only the owner or an administrator can "
1538 " <Limit Cancel-Job>\n"
1539 " Order deny,allow\n"
1540 " Require user @OWNER "
1541 CUPS_DEFAULT_PRINTOPERATOR_AUTH
"\n"
1546 in_default_policy
= 0;
1548 cupsFilePuts(temp
, "</Policy>\n");
1550 else if (!_cups_strcasecmp(line
, "<Location"))
1554 if (!strcmp(value
, "/admin"))
1555 in_admin_location
= 1;
1556 else if (!strcmp(value
, "/admin/conf"))
1557 in_conf_location
= 1;
1558 else if (!strcmp(value
, "/admin/log"))
1559 in_log_location
= 1;
1560 else if (!strcmp(value
, "/"))
1561 in_root_location
= 1;
1563 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1565 else if (!_cups_strcasecmp(line
, "</Location>"))
1569 if (in_admin_location
&& remote_admin
>= 0)
1571 wrote_admin_location
= 1;
1574 cupsFilePuts(temp
, " # Allow remote administration...\n");
1575 else if (remote_admin
== 0)
1576 cupsFilePuts(temp
, " # Restrict access to the admin pages...\n");
1578 cupsFilePuts(temp
, " Order allow,deny\n");
1581 cupsFilePrintf(temp
, " Allow %s\n",
1582 remote_any
> 0 ? "all" : "@LOCAL");
1584 else if (in_conf_location
&& remote_admin
>= 0)
1586 wrote_conf_location
= 1;
1589 cupsFilePuts(temp
, " # Allow remote access to the configuration "
1592 cupsFilePuts(temp
, " # Restrict access to the configuration "
1595 cupsFilePuts(temp
, " Order allow,deny\n");
1598 cupsFilePrintf(temp
, " Allow %s\n",
1599 remote_any
> 0 ? "all" : "@LOCAL");
1601 else if (in_log_location
&& remote_admin
>= 0)
1603 wrote_log_location
= 1;
1606 cupsFilePuts(temp
, " # Allow remote access to the log "
1609 cupsFilePuts(temp
, " # Restrict access to the log "
1612 cupsFilePuts(temp
, " Order allow,deny\n");
1615 cupsFilePrintf(temp
, " Allow %s\n",
1616 remote_any
> 0 ? "all" : "@LOCAL");
1618 else if (in_root_location
&&
1619 (remote_admin
>= 0 || remote_any
> 0 || share_printers
>= 0))
1621 wrote_root_location
= 1;
1623 if (remote_admin
> 0 && share_printers
> 0)
1624 cupsFilePuts(temp
, " # Allow shared printing and remote "
1625 "administration...\n");
1626 else if (remote_admin
> 0)
1627 cupsFilePuts(temp
, " # Allow remote administration...\n");
1628 else if (share_printers
> 0)
1629 cupsFilePuts(temp
, " # Allow shared printing...\n");
1630 else if (remote_any
> 0)
1631 cupsFilePuts(temp
, " # Allow remote access...\n");
1633 cupsFilePuts(temp
, " # Restrict access to the server...\n");
1635 cupsFilePuts(temp
, " Order allow,deny\n");
1637 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1638 cupsFilePrintf(temp
, " Allow %s\n",
1639 remote_any
> 0 ? "all" : "@LOCAL");
1642 in_admin_location
= 0;
1643 in_conf_location
= 0;
1644 in_log_location
= 0;
1645 in_root_location
= 0;
1647 cupsFilePuts(temp
, "</Location>\n");
1649 else if (!_cups_strcasecmp(line
, "<Limit"))
1651 if (in_default_policy
)
1654 * See if the policy limit is for the Cancel-Job operation...
1657 char *valptr
; /* Pointer into value */
1660 if (!_cups_strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1663 * Don't write anything for this limit section...
1670 cupsFilePrintf(temp
, "%*s%s", indent
, "", line
);
1674 for (valptr
= value
; *valptr
&& !_cups_isspace(*valptr
); valptr
++);
1679 if (!_cups_strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1682 * Write everything except for this definition...
1688 cupsFilePrintf(temp
, " %s", value
);
1690 for (value
= valptr
; _cups_isspace(*value
); value
++);
1693 cupsFilePuts(temp
, ">\n");
1697 cupsFilePrintf(temp
, "%*s%s %s>\n", indent
, "", line
, value
);
1701 else if (!_cups_strcasecmp(line
, "</Limit>") && in_cancel_job
)
1705 if (in_cancel_job
== 1)
1706 cupsFilePuts(temp
, " </Limit>\n");
1710 if (!user_cancel_any
)
1711 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1713 " <Limit Cancel-Job>\n"
1714 " Order deny,allow\n"
1715 " Require user @OWNER "
1716 CUPS_DEFAULT_PRINTOPERATOR_AUTH
"\n"
1721 else if ((((in_admin_location
|| in_conf_location
|| in_root_location
) &&
1722 (remote_admin
>= 0 || remote_any
> 0)) ||
1723 (in_root_location
&& share_printers
>= 0)) &&
1724 (!_cups_strcasecmp(line
, "Allow") || !_cups_strcasecmp(line
, "Deny") ||
1725 !_cups_strcasecmp(line
, "Order")))
1727 else if (in_cancel_job
== 2)
1729 else if (line
[0] == '<')
1733 cupsFilePrintf(temp
, "%*s%s %s>\n", indent
, "", line
, value
);
1741 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1744 else if (!in_policy
&& !in_location
&&
1745 (val
= cupsGetOption(line
, num_settings
, settings
)) != NULL
)
1748 * Replace this directive's value with the new one...
1751 cupsd_num_settings
= cupsAddOption(line
, val
, cupsd_num_settings
,
1755 * Write the new value in its place, without indentation since we
1756 * only support setting root directives, not in sections...
1759 cupsFilePrintf(temp
, "%s %s\n", line
, val
);
1763 if (!in_policy
&& !in_location
)
1766 * Record the non-policy, non-location directives that we find
1767 * in the server settings, since we cache this info and record it
1768 * in cupsAdminGetServerSettings()...
1771 cupsd_num_settings
= cupsAddOption(line
, value
, cupsd_num_settings
,
1775 cupsFilePrintf(temp
, "%*s%s %s\n", indent
, "", line
, value
);
1778 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1782 * Write any missing info...
1785 if (!wrote_browsing
&& share_printers
>= 0)
1787 if (share_printers
> 0)
1789 cupsFilePuts(temp
, "# Share local printers on the local network.\n");
1790 cupsFilePuts(temp
, "Browsing On\n");
1794 cupsFilePuts(temp
, "# Disable printer sharing and shared printers.\n");
1795 cupsFilePuts(temp
, "Browsing Off\n");
1799 if (!wrote_loglevel
&& debug_logging
>= 0)
1803 cupsFilePuts(temp
, "# Show troubleshooting information in error_log.\n");
1804 cupsFilePuts(temp
, "LogLevel debug\n");
1808 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1809 cupsFilePuts(temp
, "LogLevel " CUPS_DEFAULT_LOG_LEVEL
"\n");
1813 if (!wrote_port_listen
&&
1814 (remote_admin
>= 0 || remote_any
> 0 || share_printers
>= 0))
1816 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1818 cupsFilePuts(temp
, "# Allow remote access\n");
1819 cupsFilePrintf(temp
, "Port %d\n", ippPort());
1824 "# Only listen for connections from the local machine.\n");
1825 cupsFilePrintf(temp
, "Listen localhost:%d\n", ippPort());
1828 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1829 if (!access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1830 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1831 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1834 if (!wrote_root_location
&&
1835 (remote_admin
>= 0 || remote_any
> 0 || share_printers
>= 0))
1837 if (remote_admin
> 0 && share_printers
> 0)
1839 "# Allow shared printing and remote administration...\n");
1840 else if (remote_admin
> 0)
1841 cupsFilePuts(temp
, "# Allow remote administration...\n");
1842 else if (share_printers
> 0)
1843 cupsFilePuts(temp
, "# Allow shared printing...\n");
1844 else if (remote_any
> 0)
1845 cupsFilePuts(temp
, "# Allow remote access...\n");
1847 cupsFilePuts(temp
, "# Restrict access to the server...\n");
1849 cupsFilePuts(temp
, "<Location />\n"
1850 " Order allow,deny\n");
1852 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1853 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1855 cupsFilePuts(temp
, "</Location>\n");
1858 if (!wrote_admin_location
&& remote_admin
>= 0)
1861 cupsFilePuts(temp
, "# Allow remote administration...\n");
1863 cupsFilePuts(temp
, "# Restrict access to the admin pages...\n");
1865 cupsFilePuts(temp
, "<Location /admin>\n"
1866 " Order allow,deny\n");
1869 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1871 cupsFilePuts(temp
, "</Location>\n");
1874 if (!wrote_conf_location
&& remote_admin
>= 0)
1878 "# Allow remote access to the configuration files...\n");
1880 cupsFilePuts(temp
, "# Restrict access to the configuration files...\n");
1882 cupsFilePuts(temp
, "<Location /admin/conf>\n"
1883 " AuthType Default\n"
1884 " Require user @SYSTEM\n"
1885 " Order allow,deny\n");
1888 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1890 cupsFilePuts(temp
, "</Location>\n");
1893 if (!wrote_log_location
&& remote_admin
>= 0)
1897 "# Allow remote access to the log files...\n");
1899 cupsFilePuts(temp
, "# Restrict access to the log files...\n");
1901 cupsFilePuts(temp
, "<Location /admin/log>\n"
1902 " AuthType Default\n"
1903 " Require user @SYSTEM\n"
1904 " Order allow,deny\n");
1907 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1909 cupsFilePuts(temp
, "</Location>\n");
1912 if (!wrote_policy
&& user_cancel_any
>= 0)
1914 cupsFilePuts(temp
, "<Policy default>\n"
1915 " # Job-related operations must be done by the owner "
1916 "or an administrator...\n"
1917 " <Limit Send-Document Send-URI Hold-Job Release-Job "
1918 "Restart-Job Purge-Jobs Set-Job-Attributes "
1919 "Create-Job-Subscription Renew-Subscription "
1920 "Cancel-Subscription Get-Notifications Reprocess-Job "
1921 "Cancel-Current-Job Suspend-Current-Job Resume-Job "
1923 " Require user @OWNER @SYSTEM\n"
1924 " Order deny,allow\n"
1926 " # All administration operations require an "
1927 "administrator to authenticate...\n"
1928 " <Limit Pause-Printer Resume-Printer "
1929 "Set-Printer-Attributes Enable-Printer "
1930 "Disable-Printer Pause-Printer-After-Current-Job "
1931 "Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer "
1932 "Activate-Printer Restart-Printer Shutdown-Printer "
1933 "Startup-Printer Promote-Job Schedule-Job-After "
1934 "CUPS-Add-Printer CUPS-Delete-Printer "
1935 "CUPS-Add-Class CUPS-Delete-Class "
1936 "CUPS-Accept-Jobs CUPS-Reject-Jobs "
1937 "CUPS-Set-Default CUPS-Add-Device CUPS-Delete-Device>\n"
1938 " AuthType Default\n"
1939 " Require user @SYSTEM\n"
1940 " Order deny,allow\n"
1943 if (!user_cancel_any
)
1944 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1946 " <Limit Cancel-Job>\n"
1947 " Order deny,allow\n"
1948 " Require user @OWNER "
1949 CUPS_DEFAULT_PRINTOPERATOR_AUTH
"\n"
1952 cupsFilePuts(temp
, " <Limit All>\n"
1953 " Order deny,allow\n"
1958 for (i
= num_settings
, setting
= settings
; i
> 0; i
--, setting
++)
1959 if (setting
->name
[0] != '_' &&
1960 _cups_strcasecmp(setting
->name
, "Listen") &&
1961 _cups_strcasecmp(setting
->name
, "Port") &&
1962 !cupsGetOption(setting
->name
, cupsd_num_settings
, cupsd_settings
))
1965 * Add this directive to the list of directives we have written...
1968 cupsd_num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1969 cupsd_num_settings
, &cupsd_settings
);
1972 * Write the new value, without indentation since we only support
1973 * setting root directives, not in sections...
1976 cupsFilePrintf(temp
, "%s %s\n", setting
->name
, setting
->value
);
1979 cupsFileClose(cupsd
);
1980 cupsFileClose(temp
);
1983 * Upload the configuration file to the server...
1986 status
= cupsPutFile(http
, "/admin/conf/cupsd.conf", tempfile
);
1988 if (status
== HTTP_STATUS_CREATED
)
1991 * Updated OK, add the basic settings...
1994 if (debug_logging
>= 0)
1995 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1996 debug_logging
? "1" : "0",
1997 cupsd_num_settings
, &cupsd_settings
);
1999 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
2000 old_debug_logging
? "1" : "0",
2001 cupsd_num_settings
, &cupsd_settings
);
2003 if (remote_admin
>= 0)
2004 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
2005 remote_admin
? "1" : "0",
2006 cupsd_num_settings
, &cupsd_settings
);
2008 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
2009 old_remote_admin
? "1" : "0",
2010 cupsd_num_settings
, &cupsd_settings
);
2012 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
2013 remote_any
? "1" : "0",
2014 cupsd_num_settings
, &cupsd_settings
);
2016 if (share_printers
>= 0)
2017 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
2018 share_printers
? "1" : "0",
2019 cupsd_num_settings
, &cupsd_settings
);
2021 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
2022 old_share_printers
? "1" : "0",
2023 cupsd_num_settings
, &cupsd_settings
);
2025 if (user_cancel_any
>= 0)
2026 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
2027 user_cancel_any
? "1" : "0",
2028 cupsd_num_settings
, &cupsd_settings
);
2030 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
2031 old_user_cancel_any
? "1" : "0",
2032 cupsd_num_settings
, &cupsd_settings
);
2035 * Save the new values...
2038 invalidate_cupsd_cache(cg
);
2040 cg
->cupsd_num_settings
= cupsd_num_settings
;
2041 cg
->cupsd_settings
= cupsd_settings
;
2042 cg
->cupsd_update
= time(NULL
);
2044 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
2047 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
2050 * Remote our temp files and return...
2058 return (status
== HTTP_STATUS_CREATED
);
2063 * 'do_samba_command()' - Do a SAMBA command.
2066 static int /* O - Status of command */
2067 do_samba_command(const char *command
, /* I - Command to run */
2068 const char *address
, /* I - Address for command */
2069 const char *subcmd
, /* I - Sub-command */
2070 const char *authfile
, /* I - Samba authentication file */
2071 FILE *logfile
) /* I - Optional log file */
2074 return (1); /* Always fail on Windows... */
2077 int status
; /* Status of command */
2078 int pid
; /* Process ID of child */
2082 _cupsLangPrintf(logfile
,
2083 _("Running command: %s %s -N -A %s -c \'%s\'"),
2084 command
, address
, authfile
, subcmd
);
2086 if ((pid
= fork()) == 0)
2089 * Child goes here, redirect stdin/out/err and execute the command...
2092 int fd
= open("/dev/null", O_RDONLY
);
2101 dup2(fileno(logfile
), 1);
2102 else if ((fd
= open("/dev/null", O_WRONLY
)) > 1)
2110 execlp(command
, command
, address
, "-N", "-A", authfile
, "-c", subcmd
,
2119 _cupsLangPrintf(logfile
, _("Unable to run \"%s\": %s"),
2120 command
, strerror(errno
));
2125 * Wait for the process to complete...
2128 while (wait(&status
) != pid
);
2132 _cupsLangPuts(logfile
, "");
2134 DEBUG_printf(("9do_samba_command: status=%d", status
));
2136 if (WIFEXITED(status
))
2137 return (WEXITSTATUS(status
));
2139 return (-WTERMSIG(status
));
2145 * 'get_cupsd_conf()' - Get the current cupsd.conf file.
2148 static http_status_t
/* O - Status of request */
2150 http_t
*http
, /* I - Connection to server */
2151 _cups_globals_t
*cg
, /* I - Global data */
2152 time_t last_update
, /* I - Last update time for file */
2153 char *name
, /* I - Filename buffer */
2154 size_t namesize
, /* I - Size of filename buffer */
2155 int *remote
) /* O - Remote file? */
2157 int fd
; /* Temporary file descriptor */
2159 struct stat info
; /* cupsd.conf file information */
2161 http_status_t status
; /* Status of getting cupsd.conf */
2162 char host
[HTTP_MAX_HOST
]; /* Hostname for connection */
2166 * See if we already have the data we need...
2169 httpGetHostname(http
, host
, sizeof(host
));
2171 if (_cups_strcasecmp(cg
->cupsd_hostname
, host
))
2172 invalidate_cupsd_cache(cg
);
2174 snprintf(name
, namesize
, "%s/cupsd.conf", cg
->cups_serverroot
);
2178 if (!_cups_strcasecmp(host
, "localhost") && !access(name
, R_OK
))
2181 * Read the local file rather than using HTTP...
2184 if (stat(name
, &info
))
2186 char message
[1024]; /* Message string */
2189 snprintf(message
, sizeof(message
),
2190 _cupsLangString(cupsLangDefault(), _("stat of %s failed: %s")),
2191 name
, strerror(errno
));
2192 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
2196 return (HTTP_STATUS_SERVER_ERROR
);
2198 else if (last_update
&& info
.st_mtime
<= last_update
)
2199 status
= HTTP_STATUS_NOT_MODIFIED
;
2201 status
= HTTP_STATUS_OK
;
2207 * Read cupsd.conf via a HTTP GET request...
2210 if ((fd
= cupsTempFd(name
, (int)namesize
)) < 0)
2214 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
2216 invalidate_cupsd_cache(cg
);
2218 return (HTTP_STATUS_SERVER_ERROR
);
2223 httpClearFields(http
);
2226 httpSetField(http
, HTTP_FIELD_IF_MODIFIED_SINCE
,
2227 httpGetDateString(last_update
));
2229 status
= cupsGetFd(http
, "/admin/conf/cupsd.conf", fd
);
2233 if (status
!= HTTP_STATUS_OK
)
2245 * 'invalidate_cupsd_cache()' - Invalidate the cached cupsd.conf settings.
2249 invalidate_cupsd_cache(
2250 _cups_globals_t
*cg
) /* I - Global data */
2252 cupsFreeOptions(cg
->cupsd_num_settings
, cg
->cupsd_settings
);
2254 cg
->cupsd_hostname
[0] = '\0';
2255 cg
->cupsd_update
= 0;
2256 cg
->cupsd_num_settings
= 0;
2257 cg
->cupsd_settings
= NULL
;
2262 * 'write_option()' - Write a CUPS option to a PPD file.
2266 write_option(cups_file_t
*dstfp
, /* I - PPD file */
2267 int order
, /* I - Order dependency */
2268 const char *name
, /* I - Option name */
2269 const char *text
, /* I - Option text */
2270 const char *attrname
, /* I - Attribute name */
2271 ipp_attribute_t
*suppattr
, /* I - IPP -supported attribute */
2272 ipp_attribute_t
*defattr
, /* I - IPP -default attribute */
2273 int defval
, /* I - Default value number */
2274 int valcount
) /* I - Number of values */
2276 int i
; /* Looping var */
2279 cupsFilePrintf(dstfp
, "*JCLOpenUI *%s/%s: PickOne\n"
2280 "*OrderDependency: %d JCLSetup *%s\n",
2281 name
, text
, order
, name
);
2283 if (defattr
->value_tag
== IPP_TAG_INTEGER
)
2286 * Do numeric options with a range or list...
2289 cupsFilePrintf(dstfp
, "*Default%s: %d\n", name
,
2290 defattr
->values
[defval
].integer
);
2292 if (suppattr
->value_tag
== IPP_TAG_RANGE
)
2295 * List each number in the range...
2298 for (i
= suppattr
->values
[0].range
.lower
;
2299 i
<= suppattr
->values
[0].range
.upper
;
2302 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, i
);
2305 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n",
2307 else if (defval
== 0)
2308 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
, i
);
2309 else if (defval
< (valcount
- 1))
2310 cupsFilePrintf(dstfp
, ",%d\"\n", i
);
2312 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", i
);
2318 * List explicit numbers...
2321 for (i
= 0; i
< suppattr
->num_values
; i
++)
2323 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, suppattr
->values
[i
].integer
);
2326 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n", attrname
,
2327 suppattr
->values
[i
].integer
);
2328 else if (defval
== 0)
2329 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
,
2330 suppattr
->values
[i
].integer
);
2331 else if (defval
< (valcount
- 1))
2332 cupsFilePrintf(dstfp
, ",%d\"\n", suppattr
->values
[i
].integer
);
2334 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", suppattr
->values
[i
].integer
);
2341 * Do text options with a list...
2344 cupsFilePrintf(dstfp
, "*Default%s: %s\n", name
,
2345 defattr
->values
[defval
].string
.text
);
2347 for (i
= 0; i
< suppattr
->num_values
; i
++)
2349 cupsFilePrintf(dstfp
, "*%s %s: \"", name
,
2350 suppattr
->values
[i
].string
.text
);
2353 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\n\"\n*End\n", attrname
,
2354 suppattr
->values
[i
].string
.text
);
2355 else if (defval
== 0)
2356 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\"\n", attrname
,
2357 suppattr
->values
[i
].string
.text
);
2358 else if (defval
< (valcount
- 1))
2359 cupsFilePrintf(dstfp
, ",%s\"\n", suppattr
->values
[i
].string
.text
);
2361 cupsFilePrintf(dstfp
, ",%s\n\"\n*End\n",
2362 suppattr
->values
[i
].string
.text
);
2366 cupsFilePrintf(dstfp
, "*JCLCloseUI: *%s\n\n", name
);