2 * Administration utility API definitions for CUPS.
4 * Copyright 2007-2016 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 * 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_remote_any
, /* Remote access from anywhere? */
1198 old_user_cancel_any
, /* Cancel-job policy set? */
1199 old_debug_logging
; /* LogLevel debug set? */
1200 cups_option_t
*cupsd_settings
, /* New settings */
1201 *setting
; /* Current setting */
1202 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1206 * Range check input...
1210 http
= _cupsConnect();
1212 if (!http
|| !num_settings
|| !settings
)
1214 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1220 * Get the cupsd.conf file...
1223 if (get_cupsd_conf(http
, cg
, 0, cupsdconf
, sizeof(cupsdconf
),
1224 &remote
) == HTTP_STATUS_OK
)
1226 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
1228 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
1236 * Get current settings...
1239 if (!cupsAdminGetServerSettings(http
, &cupsd_num_settings
,
1243 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, cupsd_num_settings
,
1244 cupsd_settings
)) != NULL
)
1245 old_debug_logging
= atoi(val
);
1247 old_debug_logging
= 0;
1249 DEBUG_printf(("1cupsAdminSetServerSettings: old debug_logging=%d",
1250 old_debug_logging
));
1252 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, cupsd_num_settings
,
1253 cupsd_settings
)) != NULL
)
1254 old_remote_admin
= atoi(val
);
1256 old_remote_admin
= 0;
1258 DEBUG_printf(("1cupsAdminSetServerSettings: old remote_admin=%d",
1261 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, cupsd_num_settings
,
1262 cupsd_settings
)) != NULL
)
1263 old_remote_any
= atoi(val
);
1267 DEBUG_printf(("1cupsAdminSetServerSettings: old remote_any=%d",
1270 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, cupsd_num_settings
,
1271 cupsd_settings
)) != NULL
)
1272 old_share_printers
= atoi(val
);
1274 old_share_printers
= 0;
1276 DEBUG_printf(("1cupsAdminSetServerSettings: old share_printers=%d",
1277 old_share_printers
));
1279 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, cupsd_num_settings
,
1280 cupsd_settings
)) != NULL
)
1281 old_user_cancel_any
= atoi(val
);
1283 old_user_cancel_any
= 0;
1285 DEBUG_printf(("1cupsAdminSetServerSettings: old user_cancel_any=%d",
1286 old_user_cancel_any
));
1288 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
1291 * Get basic settings...
1294 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, num_settings
,
1297 debug_logging
= atoi(val
);
1299 if (debug_logging
== old_debug_logging
)
1302 * No change to this setting...
1311 DEBUG_printf(("1cupsAdminSetServerSettings: debug_logging=%d",
1314 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, num_settings
, settings
)) != NULL
)
1316 remote_any
= atoi(val
);
1318 if (remote_any
== old_remote_any
)
1321 * No change to this setting...
1330 DEBUG_printf(("1cupsAdminSetServerSettings: remote_any=%d", remote_any
));
1332 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, num_settings
,
1335 remote_admin
= atoi(val
);
1337 if (remote_admin
== old_remote_admin
)
1340 * No change to this setting...
1349 DEBUG_printf(("1cupsAdminSetServerSettings: remote_admin=%d",
1352 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, num_settings
,
1355 share_printers
= atoi(val
);
1357 if (share_printers
== old_share_printers
)
1360 * No change to this setting...
1363 share_printers
= -1;
1367 share_printers
= -1;
1369 DEBUG_printf(("1cupsAdminSetServerSettings: share_printers=%d",
1372 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, num_settings
,
1375 user_cancel_any
= atoi(val
);
1377 if (user_cancel_any
== old_user_cancel_any
)
1380 * No change to this setting...
1383 user_cancel_any
= -1;
1387 user_cancel_any
= -1;
1389 DEBUG_printf(("1cupsAdminSetServerSettings: user_cancel_any=%d",
1393 * Create a temporary file for the new cupsd.conf file...
1396 if ((temp
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1398 cupsFileClose(cupsd
);
1403 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
1408 * Copy the old file to the new, making changes along the way...
1411 cupsd_num_settings
= 0;
1412 in_admin_location
= 0;
1414 in_conf_location
= 0;
1415 in_default_policy
= 0;
1417 in_log_location
= 0;
1419 in_root_location
= 0;
1421 wrote_admin_location
= 0;
1423 wrote_conf_location
= 0;
1424 wrote_log_location
= 0;
1427 wrote_port_listen
= 0;
1428 wrote_root_location
= 0;
1431 if ((server_port_env
= getenv("SERVER_PORT")) != NULL
)
1433 if ((server_port
= atoi(server_port_env
)) <= 0)
1434 server_port
= ippPort();
1437 server_port
= ippPort();
1439 if (server_port
<= 0)
1440 server_port
= IPP_PORT
;
1442 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
1444 if ((!_cups_strcasecmp(line
, "Port") || !_cups_strcasecmp(line
, "Listen")) &&
1445 (remote_admin
>= 0 || remote_any
>= 0 || share_printers
>= 0))
1447 if (!wrote_port_listen
)
1449 wrote_port_listen
= 1;
1451 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1453 cupsFilePuts(temp
, "# Allow remote access\n");
1454 cupsFilePrintf(temp
, "Port %d\n", server_port
);
1458 cupsFilePuts(temp
, "# Only listen for connections from the local "
1460 cupsFilePrintf(temp
, "Listen localhost:%d\n", server_port
);
1463 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1464 if ((!value
|| strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)) &&
1465 !access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1466 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1467 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1469 else if (value
&& value
[0] == '/'
1470 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1471 && strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)
1472 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1474 cupsFilePrintf(temp
, "Listen %s\n", value
);
1476 else if ((!_cups_strcasecmp(line
, "Browsing") ||
1477 !_cups_strcasecmp(line
, "BrowseLocalProtocols")) &&
1478 share_printers
>= 0)
1480 if (!wrote_browsing
)
1482 int new_share_printers
= (share_printers
> 0 ||
1483 (share_printers
== -1 &&
1484 old_share_printers
> 0));
1488 if (new_share_printers
)
1490 const char *localp
= cupsGetOption("BrowseLocalProtocols",
1491 num_settings
, settings
);
1493 if (!localp
|| !localp
[0])
1494 localp
= cupsGetOption("BrowseLocalProtocols", cupsd_num_settings
,
1497 cupsFilePuts(temp
, "# Share local printers on the local network.\n");
1498 cupsFilePuts(temp
, "Browsing On\n");
1501 localp
= CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS
;
1503 cupsFilePrintf(temp
, "BrowseLocalProtocols %s\n", localp
);
1505 cupsd_num_settings
= cupsAddOption("BrowseLocalProtocols", localp
,
1511 cupsFilePuts(temp
, "# Disable printer sharing.\n");
1512 cupsFilePuts(temp
, "Browsing Off\n");
1516 else if (!_cups_strcasecmp(line
, "LogLevel") && debug_logging
>= 0)
1523 "# Show troubleshooting information in error_log.\n");
1524 cupsFilePuts(temp
, "LogLevel debug\n");
1528 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1529 cupsFilePuts(temp
, "LogLevel " CUPS_DEFAULT_LOG_LEVEL
"\n");
1532 else if (!_cups_strcasecmp(line
, "<Policy"))
1534 in_default_policy
= !_cups_strcasecmp(value
, "default");
1537 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1540 else if (!_cups_strcasecmp(line
, "</Policy>"))
1543 if (!wrote_policy
&& in_default_policy
)
1547 if (!user_cancel_any
)
1548 cupsFilePuts(temp
, " # Only the owner or an administrator can "
1550 " <Limit Cancel-Job>\n"
1551 " Order deny,allow\n"
1552 " Require user @OWNER "
1553 CUPS_DEFAULT_PRINTOPERATOR_AUTH
"\n"
1558 in_default_policy
= 0;
1560 cupsFilePuts(temp
, "</Policy>\n");
1562 else if (!_cups_strcasecmp(line
, "<Location"))
1566 if (!strcmp(value
, "/admin"))
1567 in_admin_location
= 1;
1568 else if (!strcmp(value
, "/admin/conf"))
1569 in_conf_location
= 1;
1570 else if (!strcmp(value
, "/admin/log"))
1571 in_log_location
= 1;
1572 else if (!strcmp(value
, "/"))
1573 in_root_location
= 1;
1575 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1577 else if (!_cups_strcasecmp(line
, "</Location>"))
1581 if (in_admin_location
&& remote_admin
>= 0)
1583 wrote_admin_location
= 1;
1586 cupsFilePuts(temp
, " # Allow remote administration...\n");
1587 else if (remote_admin
== 0)
1588 cupsFilePuts(temp
, " # Restrict access to the admin pages...\n");
1590 cupsFilePuts(temp
, " Order allow,deny\n");
1593 cupsFilePrintf(temp
, " Allow %s\n",
1594 remote_any
> 0 ? "all" : "@LOCAL");
1596 else if (in_conf_location
&& remote_admin
>= 0)
1598 wrote_conf_location
= 1;
1601 cupsFilePuts(temp
, " # Allow remote access to the configuration "
1604 cupsFilePuts(temp
, " # Restrict access to the configuration "
1607 cupsFilePuts(temp
, " Order allow,deny\n");
1610 cupsFilePrintf(temp
, " Allow %s\n",
1611 remote_any
> 0 ? "all" : "@LOCAL");
1613 else if (in_log_location
&& remote_admin
>= 0)
1615 wrote_log_location
= 1;
1618 cupsFilePuts(temp
, " # Allow remote access to the log "
1621 cupsFilePuts(temp
, " # Restrict access to the log "
1624 cupsFilePuts(temp
, " Order allow,deny\n");
1627 cupsFilePrintf(temp
, " Allow %s\n",
1628 remote_any
> 0 ? "all" : "@LOCAL");
1630 else if (in_root_location
&&
1631 (remote_admin
>= 0 || remote_any
>= 0 || share_printers
>= 0))
1633 wrote_root_location
= 1;
1635 if (remote_admin
> 0 && share_printers
> 0)
1636 cupsFilePuts(temp
, " # Allow shared printing and remote "
1637 "administration...\n");
1638 else if (remote_admin
> 0)
1639 cupsFilePuts(temp
, " # Allow remote administration...\n");
1640 else if (share_printers
> 0)
1641 cupsFilePuts(temp
, " # Allow shared printing...\n");
1642 else if (remote_any
> 0)
1643 cupsFilePuts(temp
, " # Allow remote access...\n");
1645 cupsFilePuts(temp
, " # Restrict access to the server...\n");
1647 cupsFilePuts(temp
, " Order allow,deny\n");
1649 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1650 cupsFilePrintf(temp
, " Allow %s\n",
1651 remote_any
> 0 ? "all" : "@LOCAL");
1654 in_admin_location
= 0;
1655 in_conf_location
= 0;
1656 in_log_location
= 0;
1657 in_root_location
= 0;
1659 cupsFilePuts(temp
, "</Location>\n");
1661 else if (!_cups_strcasecmp(line
, "<Limit"))
1663 if (in_default_policy
)
1666 * See if the policy limit is for the Cancel-Job operation...
1669 char *valptr
; /* Pointer into value */
1672 if (!_cups_strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1675 * Don't write anything for this limit section...
1682 cupsFilePrintf(temp
, "%*s%s", indent
, "", line
);
1686 for (valptr
= value
; *valptr
&& !_cups_isspace(*valptr
); valptr
++);
1691 if (!_cups_strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1694 * Write everything except for this definition...
1700 cupsFilePrintf(temp
, " %s", value
);
1702 for (value
= valptr
; _cups_isspace(*value
); value
++);
1705 cupsFilePuts(temp
, ">\n");
1709 cupsFilePrintf(temp
, "%*s%s %s>\n", indent
, "", line
, value
);
1713 else if (!_cups_strcasecmp(line
, "</Limit>") && in_cancel_job
)
1717 if (in_cancel_job
== 1)
1718 cupsFilePuts(temp
, " </Limit>\n");
1722 if (!user_cancel_any
)
1723 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1725 " <Limit Cancel-Job>\n"
1726 " Order deny,allow\n"
1727 " Require user @OWNER "
1728 CUPS_DEFAULT_PRINTOPERATOR_AUTH
"\n"
1733 else if ((((in_admin_location
|| in_conf_location
|| in_root_location
) &&
1734 (remote_admin
>= 0 || remote_any
>= 0)) ||
1735 (in_root_location
&& share_printers
>= 0)) &&
1736 (!_cups_strcasecmp(line
, "Allow") || !_cups_strcasecmp(line
, "Deny") ||
1737 !_cups_strcasecmp(line
, "Order")))
1739 else if (in_cancel_job
== 2)
1741 else if (line
[0] == '<')
1745 cupsFilePrintf(temp
, "%*s%s %s>\n", indent
, "", line
, value
);
1753 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1756 else if (!in_policy
&& !in_location
&&
1757 (val
= cupsGetOption(line
, num_settings
, settings
)) != NULL
)
1760 * Replace this directive's value with the new one...
1763 cupsd_num_settings
= cupsAddOption(line
, val
, cupsd_num_settings
,
1767 * Write the new value in its place, without indentation since we
1768 * only support setting root directives, not in sections...
1771 cupsFilePrintf(temp
, "%s %s\n", line
, val
);
1775 if (!in_policy
&& !in_location
)
1778 * Record the non-policy, non-location directives that we find
1779 * in the server settings, since we cache this info and record it
1780 * in cupsAdminGetServerSettings()...
1783 cupsd_num_settings
= cupsAddOption(line
, value
, cupsd_num_settings
,
1787 cupsFilePrintf(temp
, "%*s%s %s\n", indent
, "", line
, value
);
1790 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1794 * Write any missing info...
1797 if (!wrote_browsing
&& share_printers
>= 0)
1799 if (share_printers
> 0)
1801 cupsFilePuts(temp
, "# Share local printers on the local network.\n");
1802 cupsFilePuts(temp
, "Browsing On\n");
1806 cupsFilePuts(temp
, "# Disable printer sharing and shared printers.\n");
1807 cupsFilePuts(temp
, "Browsing Off\n");
1811 if (!wrote_loglevel
&& debug_logging
>= 0)
1815 cupsFilePuts(temp
, "# Show troubleshooting information in error_log.\n");
1816 cupsFilePuts(temp
, "LogLevel debug\n");
1820 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1821 cupsFilePuts(temp
, "LogLevel " CUPS_DEFAULT_LOG_LEVEL
"\n");
1825 if (!wrote_port_listen
&&
1826 (remote_admin
>= 0 || remote_any
>= 0 || share_printers
>= 0))
1828 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1830 cupsFilePuts(temp
, "# Allow remote access\n");
1831 cupsFilePrintf(temp
, "Port %d\n", ippPort());
1836 "# Only listen for connections from the local machine.\n");
1837 cupsFilePrintf(temp
, "Listen localhost:%d\n", ippPort());
1840 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1841 if (!access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1842 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1843 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1846 if (!wrote_root_location
&&
1847 (remote_admin
>= 0 || remote_any
>= 0 || share_printers
>= 0))
1849 if (remote_admin
> 0 && share_printers
> 0)
1851 "# Allow shared printing and remote administration...\n");
1852 else if (remote_admin
> 0)
1853 cupsFilePuts(temp
, "# Allow remote administration...\n");
1854 else if (share_printers
> 0)
1855 cupsFilePuts(temp
, "# Allow shared printing...\n");
1856 else if (remote_any
> 0)
1857 cupsFilePuts(temp
, "# Allow remote access...\n");
1859 cupsFilePuts(temp
, "# Restrict access to the server...\n");
1861 cupsFilePuts(temp
, "<Location />\n"
1862 " Order allow,deny\n");
1864 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1865 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1867 cupsFilePuts(temp
, "</Location>\n");
1870 if (!wrote_admin_location
&& remote_admin
>= 0)
1873 cupsFilePuts(temp
, "# Allow remote administration...\n");
1875 cupsFilePuts(temp
, "# Restrict access to the admin pages...\n");
1877 cupsFilePuts(temp
, "<Location /admin>\n"
1878 " Order allow,deny\n");
1881 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1883 cupsFilePuts(temp
, "</Location>\n");
1886 if (!wrote_conf_location
&& remote_admin
>= 0)
1890 "# Allow remote access to the configuration files...\n");
1892 cupsFilePuts(temp
, "# Restrict access to the configuration files...\n");
1894 cupsFilePuts(temp
, "<Location /admin/conf>\n"
1895 " AuthType Default\n"
1896 " Require user @SYSTEM\n"
1897 " Order allow,deny\n");
1900 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1902 cupsFilePuts(temp
, "</Location>\n");
1905 if (!wrote_log_location
&& remote_admin
>= 0)
1909 "# Allow remote access to the log files...\n");
1911 cupsFilePuts(temp
, "# Restrict access to the log files...\n");
1913 cupsFilePuts(temp
, "<Location /admin/log>\n"
1914 " AuthType Default\n"
1915 " Require user @SYSTEM\n"
1916 " Order allow,deny\n");
1919 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1921 cupsFilePuts(temp
, "</Location>\n");
1924 if (!wrote_policy
&& user_cancel_any
>= 0)
1926 cupsFilePuts(temp
, "<Policy default>\n"
1927 " # Job-related operations must be done by the owner "
1928 "or an administrator...\n"
1929 " <Limit Send-Document Send-URI Hold-Job Release-Job "
1930 "Restart-Job Purge-Jobs Set-Job-Attributes "
1931 "Create-Job-Subscription Renew-Subscription "
1932 "Cancel-Subscription Get-Notifications Reprocess-Job "
1933 "Cancel-Current-Job Suspend-Current-Job Resume-Job "
1935 " Require user @OWNER @SYSTEM\n"
1936 " Order deny,allow\n"
1938 " # All administration operations require an "
1939 "administrator to authenticate...\n"
1940 " <Limit Pause-Printer Resume-Printer "
1941 "Set-Printer-Attributes Enable-Printer "
1942 "Disable-Printer Pause-Printer-After-Current-Job "
1943 "Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer "
1944 "Activate-Printer Restart-Printer Shutdown-Printer "
1945 "Startup-Printer Promote-Job Schedule-Job-After "
1946 "CUPS-Add-Printer CUPS-Delete-Printer "
1947 "CUPS-Add-Class CUPS-Delete-Class "
1948 "CUPS-Accept-Jobs CUPS-Reject-Jobs "
1949 "CUPS-Set-Default CUPS-Add-Device CUPS-Delete-Device>\n"
1950 " AuthType Default\n"
1951 " Require user @SYSTEM\n"
1952 " Order deny,allow\n"
1955 if (!user_cancel_any
)
1956 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1958 " <Limit Cancel-Job>\n"
1959 " Order deny,allow\n"
1960 " Require user @OWNER "
1961 CUPS_DEFAULT_PRINTOPERATOR_AUTH
"\n"
1964 cupsFilePuts(temp
, " <Limit All>\n"
1965 " Order deny,allow\n"
1970 for (i
= num_settings
, setting
= settings
; i
> 0; i
--, setting
++)
1971 if (setting
->name
[0] != '_' &&
1972 _cups_strcasecmp(setting
->name
, "Listen") &&
1973 _cups_strcasecmp(setting
->name
, "Port") &&
1974 !cupsGetOption(setting
->name
, cupsd_num_settings
, cupsd_settings
))
1977 * Add this directive to the list of directives we have written...
1980 cupsd_num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1981 cupsd_num_settings
, &cupsd_settings
);
1984 * Write the new value, without indentation since we only support
1985 * setting root directives, not in sections...
1988 cupsFilePrintf(temp
, "%s %s\n", setting
->name
, setting
->value
);
1991 cupsFileClose(cupsd
);
1992 cupsFileClose(temp
);
1995 * Upload the configuration file to the server...
1998 status
= cupsPutFile(http
, "/admin/conf/cupsd.conf", tempfile
);
2000 if (status
== HTTP_STATUS_CREATED
)
2003 * Updated OK, add the basic settings...
2006 if (debug_logging
>= 0)
2007 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
2008 debug_logging
? "1" : "0",
2009 cupsd_num_settings
, &cupsd_settings
);
2011 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
2012 old_debug_logging
? "1" : "0",
2013 cupsd_num_settings
, &cupsd_settings
);
2015 if (remote_admin
>= 0)
2016 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
2017 remote_admin
? "1" : "0",
2018 cupsd_num_settings
, &cupsd_settings
);
2020 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
2021 old_remote_admin
? "1" : "0",
2022 cupsd_num_settings
, &cupsd_settings
);
2024 if (remote_any
>= 0)
2025 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
2026 remote_any
? "1" : "0",
2027 cupsd_num_settings
, &cupsd_settings
);
2029 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
2030 old_remote_any
? "1" : "0",
2031 cupsd_num_settings
, &cupsd_settings
);
2033 if (share_printers
>= 0)
2034 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
2035 share_printers
? "1" : "0",
2036 cupsd_num_settings
, &cupsd_settings
);
2038 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
2039 old_share_printers
? "1" : "0",
2040 cupsd_num_settings
, &cupsd_settings
);
2042 if (user_cancel_any
>= 0)
2043 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
2044 user_cancel_any
? "1" : "0",
2045 cupsd_num_settings
, &cupsd_settings
);
2047 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
2048 old_user_cancel_any
? "1" : "0",
2049 cupsd_num_settings
, &cupsd_settings
);
2052 * Save the new values...
2055 invalidate_cupsd_cache(cg
);
2057 cg
->cupsd_num_settings
= cupsd_num_settings
;
2058 cg
->cupsd_settings
= cupsd_settings
;
2059 cg
->cupsd_update
= time(NULL
);
2061 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
2064 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
2067 * Remote our temp files and return...
2075 return (status
== HTTP_STATUS_CREATED
);
2080 * 'do_samba_command()' - Do a SAMBA command.
2083 static int /* O - Status of command */
2084 do_samba_command(const char *command
, /* I - Command to run */
2085 const char *address
, /* I - Address for command */
2086 const char *subcmd
, /* I - Sub-command */
2087 const char *authfile
, /* I - Samba authentication file */
2088 FILE *logfile
) /* I - Optional log file */
2091 return (1); /* Always fail on Windows... */
2094 int status
; /* Status of command */
2095 int pid
; /* Process ID of child */
2099 _cupsLangPrintf(logfile
,
2100 _("Running command: %s %s -N -A %s -c \'%s\'"),
2101 command
, address
, authfile
, subcmd
);
2103 if ((pid
= fork()) == 0)
2106 * Child goes here, redirect stdin/out/err and execute the command...
2109 int fd
= open("/dev/null", O_RDONLY
);
2118 dup2(fileno(logfile
), 1);
2119 else if ((fd
= open("/dev/null", O_WRONLY
)) > 1)
2127 execlp(command
, command
, address
, "-N", "-A", authfile
, "-c", subcmd
,
2136 _cupsLangPrintf(logfile
, _("Unable to run \"%s\": %s"),
2137 command
, strerror(errno
));
2142 * Wait for the process to complete...
2145 while (wait(&status
) != pid
);
2149 _cupsLangPuts(logfile
, "");
2151 DEBUG_printf(("9do_samba_command: status=%d", status
));
2153 if (WIFEXITED(status
))
2154 return (WEXITSTATUS(status
));
2156 return (-WTERMSIG(status
));
2162 * 'get_cupsd_conf()' - Get the current cupsd.conf file.
2165 static http_status_t
/* O - Status of request */
2167 http_t
*http
, /* I - Connection to server */
2168 _cups_globals_t
*cg
, /* I - Global data */
2169 time_t last_update
, /* I - Last update time for file */
2170 char *name
, /* I - Filename buffer */
2171 size_t namesize
, /* I - Size of filename buffer */
2172 int *remote
) /* O - Remote file? */
2174 int fd
; /* Temporary file descriptor */
2176 struct stat info
; /* cupsd.conf file information */
2178 http_status_t status
; /* Status of getting cupsd.conf */
2179 char host
[HTTP_MAX_HOST
]; /* Hostname for connection */
2183 * See if we already have the data we need...
2186 httpGetHostname(http
, host
, sizeof(host
));
2188 if (_cups_strcasecmp(cg
->cupsd_hostname
, host
))
2189 invalidate_cupsd_cache(cg
);
2191 snprintf(name
, namesize
, "%s/cupsd.conf", cg
->cups_serverroot
);
2195 if (!_cups_strcasecmp(host
, "localhost") && !access(name
, R_OK
))
2198 * Read the local file rather than using HTTP...
2201 if (stat(name
, &info
))
2203 char message
[1024]; /* Message string */
2206 snprintf(message
, sizeof(message
),
2207 _cupsLangString(cupsLangDefault(), _("stat of %s failed: %s")),
2208 name
, strerror(errno
));
2209 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
2213 return (HTTP_STATUS_SERVER_ERROR
);
2215 else if (last_update
&& info
.st_mtime
<= last_update
)
2216 status
= HTTP_STATUS_NOT_MODIFIED
;
2218 status
= HTTP_STATUS_OK
;
2224 * Read cupsd.conf via a HTTP GET request...
2227 if ((fd
= cupsTempFd(name
, (int)namesize
)) < 0)
2231 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
2233 invalidate_cupsd_cache(cg
);
2235 return (HTTP_STATUS_SERVER_ERROR
);
2240 httpClearFields(http
);
2243 httpSetField(http
, HTTP_FIELD_IF_MODIFIED_SINCE
,
2244 httpGetDateString(last_update
));
2246 status
= cupsGetFd(http
, "/admin/conf/cupsd.conf", fd
);
2250 if (status
!= HTTP_STATUS_OK
)
2262 * 'invalidate_cupsd_cache()' - Invalidate the cached cupsd.conf settings.
2266 invalidate_cupsd_cache(
2267 _cups_globals_t
*cg
) /* I - Global data */
2269 cupsFreeOptions(cg
->cupsd_num_settings
, cg
->cupsd_settings
);
2271 cg
->cupsd_hostname
[0] = '\0';
2272 cg
->cupsd_update
= 0;
2273 cg
->cupsd_num_settings
= 0;
2274 cg
->cupsd_settings
= NULL
;
2279 * 'write_option()' - Write a CUPS option to a PPD file.
2283 write_option(cups_file_t
*dstfp
, /* I - PPD file */
2284 int order
, /* I - Order dependency */
2285 const char *name
, /* I - Option name */
2286 const char *text
, /* I - Option text */
2287 const char *attrname
, /* I - Attribute name */
2288 ipp_attribute_t
*suppattr
, /* I - IPP -supported attribute */
2289 ipp_attribute_t
*defattr
, /* I - IPP -default attribute */
2290 int defval
, /* I - Default value number */
2291 int valcount
) /* I - Number of values */
2293 int i
; /* Looping var */
2296 cupsFilePrintf(dstfp
, "*JCLOpenUI *%s/%s: PickOne\n"
2297 "*OrderDependency: %d JCLSetup *%s\n",
2298 name
, text
, order
, name
);
2300 if (defattr
->value_tag
== IPP_TAG_INTEGER
)
2303 * Do numeric options with a range or list...
2306 cupsFilePrintf(dstfp
, "*Default%s: %d\n", name
,
2307 defattr
->values
[defval
].integer
);
2309 if (suppattr
->value_tag
== IPP_TAG_RANGE
)
2312 * List each number in the range...
2315 for (i
= suppattr
->values
[0].range
.lower
;
2316 i
<= suppattr
->values
[0].range
.upper
;
2319 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, i
);
2322 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n",
2324 else if (defval
== 0)
2325 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
, i
);
2326 else if (defval
< (valcount
- 1))
2327 cupsFilePrintf(dstfp
, ",%d\"\n", i
);
2329 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", i
);
2335 * List explicit numbers...
2338 for (i
= 0; i
< suppattr
->num_values
; i
++)
2340 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, suppattr
->values
[i
].integer
);
2343 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n", attrname
,
2344 suppattr
->values
[i
].integer
);
2345 else if (defval
== 0)
2346 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
,
2347 suppattr
->values
[i
].integer
);
2348 else if (defval
< (valcount
- 1))
2349 cupsFilePrintf(dstfp
, ",%d\"\n", suppattr
->values
[i
].integer
);
2351 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", suppattr
->values
[i
].integer
);
2358 * Do text options with a list...
2361 cupsFilePrintf(dstfp
, "*Default%s: %s\n", name
,
2362 defattr
->values
[defval
].string
.text
);
2364 for (i
= 0; i
< suppattr
->num_values
; i
++)
2366 cupsFilePrintf(dstfp
, "*%s %s: \"", name
,
2367 suppattr
->values
[i
].string
.text
);
2370 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\n\"\n*End\n", attrname
,
2371 suppattr
->values
[i
].string
.text
);
2372 else if (defval
== 0)
2373 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\"\n", attrname
,
2374 suppattr
->values
[i
].string
.text
);
2375 else if (defval
< (valcount
- 1))
2376 cupsFilePrintf(dstfp
, ",%s\"\n", suppattr
->values
[i
].string
.text
);
2378 cupsFilePrintf(dstfp
, ",%s\n\"\n*End\n",
2379 suppattr
->values
[i
].string
.text
);
2383 cupsFilePrintf(dstfp
, "*JCLCloseUI: *%s\n\n", name
);