4 * Administration utility API definitions for CUPS.
6 * Copyright 2007-2015 by Apple Inc.
7 * Copyright 2001-2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * Include necessary headers...
22 #include "cups-private.h"
24 #include "adminutil.h"
30 # include <sys/wait.h>
38 static int do_samba_command(const char *command
,
40 const char *subcommand
,
43 static http_status_t
get_cupsd_conf(http_t
*http
, _cups_globals_t
*cg
,
44 time_t last_update
, char *name
,
45 size_t namelen
, int *remote
);
46 static void invalidate_cupsd_cache(_cups_globals_t
*cg
);
47 static void write_option(cups_file_t
*dstfp
, int order
,
48 const char *name
, const char *text
,
50 ipp_attribute_t
*suppattr
,
51 ipp_attribute_t
*defattr
, int defval
,
56 * 'cupsAdminCreateWindowsPPD()' - Create the Windows PPD file for a printer.
61 char * /* O - PPD file or NULL */
62 cupsAdminCreateWindowsPPD(
63 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
64 const char *dest
, /* I - Printer or class */
65 char *buffer
, /* I - Filename buffer */
66 int bufsize
) /* I - Size of filename buffer */
68 const char *src
; /* Source PPD filename */
69 cups_file_t
*srcfp
, /* Source PPD file */
70 *dstfp
; /* Destination PPD file */
71 ipp_t
*request
, /* IPP request */
72 *response
; /* IPP response */
73 ipp_attribute_t
*suppattr
, /* IPP -supported attribute */
74 *defattr
; /* IPP -default attribute */
75 cups_lang_t
*language
; /* Current language */
76 char line
[256], /* Line from PPD file */
77 junk
[256], /* Extra junk to throw away */
78 *ptr
, /* Pointer into line */
79 uri
[1024], /* Printer URI */
80 option
[41], /* Option */
81 choice
[41]; /* Choice */
82 int jcloption
, /* In a JCL option? */
83 jclorder
, /* Next JCL order dependency */
84 linenum
; /* Current line number */
85 time_t curtime
; /* Current time */
86 struct tm
*curdate
; /* Current date */
87 static const char * const pattrs
[] = /* Printer attributes we want */
89 "job-hold-until-supported",
90 "job-hold-until-default",
91 "job-sheets-supported",
93 "job-priority-supported",
94 "job-priority-default"
99 * Range check the input...
106 http
= _cupsConnect();
108 if (!http
|| !dest
|| !buffer
|| bufsize
< 2)
112 * Get the PPD file...
115 if ((src
= cupsGetPPD2(http
, dest
)) == NULL
)
119 * Get the supported banner pages, etc. for the printer...
122 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
124 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
125 "localhost", 0, "/printers/%s", dest
);
126 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
127 "printer-uri", NULL
, uri
);
129 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
130 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
134 * Do the request and get back a response...
137 response
= cupsDoRequest(http
, request
, "/");
138 if (!response
|| cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
145 * Open the original PPD file...
148 if ((srcfp
= cupsFileOpen(src
, "rb")) == NULL
)
152 * Create a temporary output file using the destination buffer...
155 if ((dstfp
= cupsTempFile2(buffer
, bufsize
)) == NULL
)
157 cupsFileClose(srcfp
);
165 * Write a new header explaining that this isn't the original PPD...
168 cupsFilePuts(dstfp
, "*PPD-Adobe: \"4.3\"\n");
170 curtime
= time(NULL
);
171 curdate
= gmtime(&curtime
);
173 cupsFilePrintf(dstfp
, "*%% Modified on %04d%02d%02d%02d%02d%02d+0000 "
174 "for CUPS Windows Driver\n",
175 curdate
->tm_year
+ 1900, curdate
->tm_mon
+ 1, curdate
->tm_mday
,
176 curdate
->tm_hour
, curdate
->tm_min
, curdate
->tm_sec
);
179 * Read the existing PPD file, converting all PJL commands to CUPS
180 * job ticket comments...
186 language
= cupsLangDefault();
188 while (cupsFileGets(srcfp
, line
, sizeof(line
)))
192 if (!strncmp(line
, "*PPD-Adobe:", 11))
195 * Already wrote the PPD header...
200 else if (!strncmp(line
, "*JCLBegin:", 10) ||
201 !strncmp(line
, "*JCLToPSInterpreter:", 20) ||
202 !strncmp(line
, "*JCLEnd:", 8) ||
203 !strncmp(line
, "*Protocols:", 11))
206 * Don't use existing JCL keywords; we'll create our own, below...
209 cupsFilePrintf(dstfp
, "*%% Commented out for CUPS Windows Driver...\n"
210 "*%%%s\n", line
+ 1);
213 else if (!strncmp(line
, "*JCLOpenUI", 10))
216 cupsFilePrintf(dstfp
, "%s\n", line
);
218 else if (!strncmp(line
, "*JCLCloseUI", 11))
221 cupsFilePrintf(dstfp
, "%s\n", line
);
223 else if (jcloption
&& !strncmp(line
, "*OrderDependency:", 17))
225 for (ptr
= line
+ 17; _cups_isspace(*ptr
); ptr
++);
227 ptr
= strchr(ptr
, ' ');
231 cupsFilePrintf(dstfp
, "*OrderDependency: %d%s\n", jclorder
, ptr
);
235 cupsFilePrintf(dstfp
, "%s\n", line
);
237 else if (jcloption
&&
238 strncmp(line
, "*End", 4) &&
239 strncmp(line
, "*Default", 8))
241 if ((ptr
= strchr(line
, ':')) == NULL
)
243 snprintf(line
, sizeof(line
),
244 _cupsLangString(language
, _("Missing value on line %d.")),
246 _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR
, line
, 0);
248 cupsFileClose(srcfp
);
249 cupsFileClose(dstfp
);
259 if ((ptr
= strchr(ptr
, '\"')) == NULL
)
261 snprintf(line
, sizeof(line
),
262 _cupsLangString(language
,
263 _("Missing double quote on line %d.")),
265 _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR
, line
, 0);
267 cupsFileClose(srcfp
);
268 cupsFileClose(dstfp
);
278 if (sscanf(line
, "*%40s%*[ \t]%40[^:/]", option
, choice
) != 2)
280 snprintf(line
, sizeof(line
),
281 _cupsLangString(language
,
282 _("Bad option + choice on line %d.")),
284 _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR
, line
, 0);
286 cupsFileClose(srcfp
);
287 cupsFileClose(dstfp
);
297 if (strchr(ptr
+ 1, '\"') == NULL
)
303 while (cupsFileGets(srcfp
, junk
, sizeof(junk
)) != NULL
)
307 if (!strncmp(junk
, "*End", 4))
312 snprintf(ptr
+ 1, sizeof(line
) - (size_t)(ptr
- line
+ 1),
313 "%%cupsJobTicket: %s=%s\n\"\n*End", option
, choice
);
315 cupsFilePrintf(dstfp
, "*%% Changed for CUPS Windows Driver...\n%s\n",
319 cupsFilePrintf(dstfp
, "%s\n", line
);
322 cupsFileClose(srcfp
);
327 _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR
, _("Empty PPD file."), 1);
329 cupsFileClose(dstfp
);
338 * Now add the CUPS-specific attributes and options...
341 cupsFilePuts(dstfp
, "\n*% CUPS Job Ticket support and options...\n");
342 cupsFilePuts(dstfp
, "*Protocols: PJL\n");
343 cupsFilePuts(dstfp
, "*JCLBegin: \"%!PS-Adobe-3.0<0A>\"\n");
344 cupsFilePuts(dstfp
, "*JCLToPSInterpreter: \"\"\n");
345 cupsFilePuts(dstfp
, "*JCLEnd: \"\"\n");
347 cupsFilePuts(dstfp
, "\n*OpenGroup: CUPS/CUPS Options\n\n");
349 if ((defattr
= ippFindAttribute(response
, "job-hold-until-default",
350 IPP_TAG_ZERO
)) != NULL
&&
351 (suppattr
= ippFindAttribute(response
, "job-hold-until-supported",
352 IPP_TAG_ZERO
)) != NULL
)
353 write_option(dstfp
, jclorder
++, "cupsJobHoldUntil", "Hold Until",
354 "job-hold-until", suppattr
, defattr
, 0, 1);
356 if ((defattr
= ippFindAttribute(response
, "job-priority-default",
357 IPP_TAG_INTEGER
)) != NULL
&&
358 (suppattr
= ippFindAttribute(response
, "job-priority-supported",
359 IPP_TAG_RANGE
)) != NULL
)
360 write_option(dstfp
, jclorder
++, "cupsJobPriority", "Priority",
361 "job-priority", suppattr
, defattr
, 0, 1);
363 if ((defattr
= ippFindAttribute(response
, "job-sheets-default",
364 IPP_TAG_ZERO
)) != NULL
&&
365 (suppattr
= ippFindAttribute(response
, "job-sheets-supported",
366 IPP_TAG_ZERO
)) != NULL
)
368 write_option(dstfp
, jclorder
++, "cupsJobSheetsStart", "Start Banner",
369 "job-sheets", suppattr
, defattr
, 0, 2);
370 write_option(dstfp
, jclorder
, "cupsJobSheetsEnd", "End Banner",
371 "job-sheets", suppattr
, defattr
, 1, 2);
374 cupsFilePuts(dstfp
, "*CloseGroup: CUPS\n");
375 cupsFileClose(dstfp
);
384 * 'cupsAdminExportSamba()' - Export a printer to Samba.
389 int /* O - 1 on success, 0 on failure */
390 cupsAdminExportSamba(
391 const char *dest
, /* I - Destination to export */
392 const char *ppd
, /* I - PPD file */
393 const char *samba_server
, /* I - Samba server */
394 const char *samba_user
, /* I - Samba username */
395 const char *samba_password
, /* I - Samba password */
396 FILE *logfile
) /* I - Log file, if any */
398 int status
; /* Status of Samba commands */
399 int have_drivers
; /* Have drivers? */
400 char file
[1024], /* File to test for */
401 authfile
[1024], /* Temporary authentication file */
402 address
[1024], /* Address for command */
403 subcmd
[1024], /* Sub-command */
404 message
[1024]; /* Error message */
405 cups_file_t
*fp
; /* Authentication file */
406 cups_lang_t
*language
; /* Current language */
407 _cups_globals_t
*cg
= _cupsGlobals();
412 * Range check input...
415 if (!dest
|| !ppd
|| !samba_server
|| !samba_user
|| !samba_password
)
417 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
422 * Create a temporary authentication file for Samba...
425 if ((fp
= cupsTempFile2(authfile
, sizeof(authfile
))) == NULL
)
427 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
431 cupsFilePrintf(fp
, "username = %s\n", samba_user
);
432 cupsFilePrintf(fp
, "password = %s\n", samba_password
);
436 * See which drivers are available; the new CUPS v6 and Adobe drivers
437 * depend on the Windows 2k PS driver, so copy that driver first:
448 language
= cupsLangDefault();
450 snprintf(file
, sizeof(file
), "%s/drivers/pscript5.dll", cg
->cups_datadir
);
451 if (!access(file
, 0))
456 * Windows 2k driver is installed; do the smbclient commands needed
457 * to copy the Win2k drivers over...
460 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
462 snprintf(subcmd
, sizeof(subcmd
),
464 "put %s W32X86/%s.ppd;"
465 "put %s/drivers/ps5ui.dll W32X86/ps5ui.dll;"
466 "put %s/drivers/pscript.hlp W32X86/pscript.hlp;"
467 "put %s/drivers/pscript.ntf W32X86/pscript.ntf;"
468 "put %s/drivers/pscript5.dll W32X86/pscript5.dll",
469 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
470 cg
->cups_datadir
, cg
->cups_datadir
);
472 if ((status
= do_samba_command("smbclient", address
, subcmd
,
473 authfile
, logfile
)) != 0)
475 snprintf(message
, sizeof(message
),
476 _cupsLangString(language
,
477 _("Unable to copy Windows 2000 printer "
478 "driver files (%d).")), status
);
480 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
483 _cupsLangPuts(logfile
, message
);
491 * See if we also have the CUPS driver files; if so, use them!
494 snprintf(file
, sizeof(file
), "%s/drivers/cupsps6.dll", cg
->cups_datadir
);
495 if (!access(file
, 0))
498 * Copy the CUPS driver files over...
501 snprintf(subcmd
, sizeof(subcmd
),
502 "put %s/drivers/cups6.ini W32X86/cups6.ini;"
503 "put %s/drivers/cupsps6.dll W32X86/cupsps6.dll;"
504 "put %s/drivers/cupsui6.dll W32X86/cupsui6.dll",
505 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
507 if ((status
= do_samba_command("smbclient", address
, subcmd
,
508 authfile
, logfile
)) != 0)
510 snprintf(message
, sizeof(message
),
511 _cupsLangString(language
,
512 _("Unable to copy CUPS printer driver "
513 "files (%d).")), status
);
515 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
518 _cupsLangPuts(logfile
, message
);
526 * Do the rpcclient command needed for the CUPS drivers...
529 snprintf(subcmd
, sizeof(subcmd
),
530 "adddriver \"Windows NT x86\" \"%s:"
531 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
532 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
533 "cups6.ini,cupsps6.dll,cupsui6.dll\"",
539 * Don't have the CUPS drivers, so just use the standard Windows
543 snprintf(subcmd
, sizeof(subcmd
),
544 "adddriver \"Windows NT x86\" \"%s:"
545 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
546 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
550 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
551 authfile
, logfile
)) != 0)
553 snprintf(message
, sizeof(message
),
554 _cupsLangString(language
,
555 _("Unable to install Windows 2000 printer "
556 "driver files (%d).")), status
);
558 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
561 _cupsLangPuts(logfile
, message
);
570 * See if we have the Win9x PS driver...
573 snprintf(file
, sizeof(file
), "%s/drivers/ADOBEPS4.DRV", cg
->cups_datadir
);
574 if (!access(file
, 0))
579 * Do the smbclient commands needed for the Adobe Win9x drivers...
582 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
584 snprintf(subcmd
, sizeof(subcmd
),
586 "put %s WIN40/%s.PPD;"
587 "put %s/drivers/ADFONTS.MFM WIN40/ADFONTS.MFM;"
588 "put %s/drivers/ADOBEPS4.DRV WIN40/ADOBEPS4.DRV;"
589 "put %s/drivers/ADOBEPS4.HLP WIN40/ADOBEPS4.HLP;"
590 "put %s/drivers/ICONLIB.DLL WIN40/ICONLIB.DLL;"
591 "put %s/drivers/PSMON.DLL WIN40/PSMON.DLL;",
592 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
593 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
595 if ((status
= do_samba_command("smbclient", address
, subcmd
,
596 authfile
, logfile
)) != 0)
598 snprintf(message
, sizeof(message
),
599 _cupsLangString(language
,
600 _("Unable to copy Windows 9x printer "
601 "driver files (%d).")), status
);
603 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
606 _cupsLangPuts(logfile
, message
);
614 * Do the rpcclient commands needed for the Adobe Win9x drivers...
617 snprintf(subcmd
, sizeof(subcmd
),
618 "adddriver \"Windows 4.0\" \"%s:ADOBEPS4.DRV:%s.PPD:NULL:"
619 "ADOBEPS4.HLP:PSMON.DLL:RAW:"
620 "ADOBEPS4.DRV,%s.PPD,ADOBEPS4.HLP,PSMON.DLL,ADFONTS.MFM,"
624 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
625 authfile
, logfile
)) != 0)
627 snprintf(message
, sizeof(message
),
628 _cupsLangString(language
,
629 _("Unable to install Windows 9x printer "
630 "driver files (%d).")), status
);
632 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
635 _cupsLangPuts(logfile
, message
);
644 * See if we have the 64-bit Windows PS driver...
654 snprintf(file
, sizeof(file
), "%s/drivers/x64/pscript5.dll", cg
->cups_datadir
);
655 if (!access(file
, 0))
660 * 64-bit Windows driver is installed; do the smbclient commands needed
661 * to copy the Win64 drivers over...
664 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
666 snprintf(subcmd
, sizeof(subcmd
),
669 "put %s/drivers/x64/ps5ui.dll x64/ps5ui.dll;"
670 "put %s/drivers/x64/pscript.hlp x64/pscript.hlp;"
671 "put %s/drivers/x64/pscript.ntf x64/pscript.ntf;"
672 "put %s/drivers/x64/pscript5.dll x64/pscript5.dll",
673 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
674 cg
->cups_datadir
, cg
->cups_datadir
);
676 if ((status
= do_samba_command("smbclient", address
, subcmd
,
677 authfile
, logfile
)) != 0)
679 snprintf(message
, sizeof(message
),
680 _cupsLangString(language
,
681 _("Unable to copy 64-bit Windows printer "
682 "driver files (%d).")), status
);
684 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
687 _cupsLangPuts(logfile
, message
);
695 * See if we also have the CUPS driver files; if so, use them!
698 snprintf(file
, sizeof(file
), "%s/drivers/x64/cupsps6.dll", cg
->cups_datadir
);
699 if (!access(file
, 0))
702 * Copy the CUPS driver files over...
705 snprintf(subcmd
, sizeof(subcmd
),
706 "put %s/drivers/x64/cups6.ini x64/cups6.ini;"
707 "put %s/drivers/x64/cupsps6.dll x64/cupsps6.dll;"
708 "put %s/drivers/x64/cupsui6.dll x64/cupsui6.dll",
709 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
711 if ((status
= do_samba_command("smbclient", address
, subcmd
,
712 authfile
, logfile
)) != 0)
714 snprintf(message
, sizeof(message
),
715 _cupsLangString(language
,
716 _("Unable to copy 64-bit CUPS printer driver "
717 "files (%d).")), status
);
719 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
722 _cupsLangPuts(logfile
, message
);
730 * Do the rpcclient command needed for the CUPS drivers...
733 snprintf(subcmd
, sizeof(subcmd
),
734 "adddriver \"Windows x64\" \"%s:"
735 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
736 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
737 "cups6.ini,cupsps6.dll,cupsui6.dll\"",
743 * Don't have the CUPS drivers, so just use the standard Windows
747 snprintf(subcmd
, sizeof(subcmd
),
748 "adddriver \"Windows x64\" \"%s:"
749 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
750 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
754 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
755 authfile
, logfile
)) != 0)
757 snprintf(message
, sizeof(message
),
758 _cupsLangString(language
,
759 _("Unable to install Windows 2000 printer "
760 "driver files (%d).")), status
);
762 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
765 _cupsLangPuts(logfile
, message
);
773 if (logfile
&& !(have_drivers
& 1))
777 _cupsLangString(language
,
778 _("No Windows printer drivers are installed.")),
782 _cupsLangString(language
,
783 _("Warning, no Windows 2000 printer drivers "
787 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, message
, 0);
788 _cupsLangPuts(logfile
, message
);
791 if (have_drivers
== 0)
793 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, message
, 0);
801 * Finally, associate the drivers we just added with the queue...
804 snprintf(subcmd
, sizeof(subcmd
), "setdriver %s %s", dest
, dest
);
806 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
807 authfile
, logfile
)) != 0)
809 snprintf(message
, sizeof(message
),
810 _cupsLangString(language
,
811 _("Unable to set Windows printer driver (%d).")),
814 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
817 _cupsLangPuts(logfile
, message
);
831 * 'cupsAdminGetServerSettings()' - Get settings from the server.
833 * The returned settings should be freed with cupsFreeOptions() when
834 * you are done with them.
836 * @since CUPS 1.3/OS X 10.5@
839 int /* O - 1 on success, 0 on failure */
840 cupsAdminGetServerSettings(
841 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
842 int *num_settings
, /* O - Number of settings */
843 cups_option_t
**settings
) /* O - Settings */
845 int i
; /* Looping var */
846 cups_file_t
*cupsd
; /* cupsd.conf file */
847 char cupsdconf
[1024]; /* cupsd.conf filename */
848 int remote
; /* Remote cupsd.conf file? */
849 http_status_t status
; /* Status of getting cupsd.conf */
850 char line
[1024], /* Line from cupsd.conf file */
851 *value
; /* Value on line */
852 cups_option_t
*setting
; /* Current setting */
853 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
857 * Range check input...
863 * See if we are connected to the same server...
869 * Compare the connection hostname, port, and encryption settings to
870 * the cached defaults; these were initialized the first time we
874 if (strcmp(cg
->http
->hostname
, cg
->server
) ||
875 cg
->ipp_port
!= httpAddrPort(cg
->http
->hostaddr
) ||
876 (cg
->http
->encryption
!= cg
->encryption
&&
877 cg
->http
->encryption
== HTTP_ENCRYPTION_NEVER
))
880 * Need to close the current connection because something has changed...
889 * (Re)connect as needed...
894 if ((cg
->http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
,
895 cupsEncryption(), 1, 0, NULL
)) == NULL
)
898 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
, NULL
, 0);
900 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
,
901 _("Unable to connect to host."), 1);
916 if (!http
|| !num_settings
|| !settings
)
918 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
933 * Get the cupsd.conf file...
936 if ((status
= get_cupsd_conf(http
, cg
, cg
->cupsd_update
, cupsdconf
,
937 sizeof(cupsdconf
), &remote
)) == HTTP_STATUS_OK
)
939 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
941 char message
[1024]; /* Message string */
944 snprintf(message
, sizeof(message
),
945 _cupsLangString(cupsLangDefault(), _("Open of %s failed: %s")),
946 cupsdconf
, strerror(errno
));
947 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
956 * Read the file, keeping track of what settings are enabled...
959 int remote_access
= 0, /* Remote access allowed? */
960 remote_admin
= 0, /* Remote administration allowed? */
961 remote_any
= 0, /* Remote access from anywhere allowed? */
962 browsing
= 1, /* Browsing enabled? */
963 cancel_policy
= 1, /* Cancel-job policy set? */
964 debug_logging
= 0; /* LogLevel debug set? */
965 int linenum
= 0, /* Line number in file */
966 in_location
= 0, /* In a location section? */
967 in_policy
= 0, /* In a policy section? */
968 in_cancel_job
= 0, /* In a cancel-job section? */
969 in_admin_location
= 0; /* In the /admin location? */
972 invalidate_cupsd_cache(cg
);
974 cg
->cupsd_update
= time(NULL
);
975 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
977 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
979 if (!value
&& strncmp(line
, "</", 2))
980 value
= line
+ strlen(line
);
982 if ((!_cups_strcasecmp(line
, "Port") || !_cups_strcasecmp(line
, "Listen")) && value
)
984 char *port
; /* Pointer to port number, if any */
987 if ((port
= strrchr(value
, ':')) != NULL
)
989 else if (isdigit(*value
& 255))
992 * Listen on a port number implies remote access...
999 if (_cups_strcasecmp(value
, "localhost") && strcmp(value
, "127.0.0.1")
1002 #endif /* AF_LOCAL */
1004 && strcmp(value
, "[::1]")
1005 #endif /* AF_INET6 */
1009 else if (!_cups_strcasecmp(line
, "Browsing"))
1011 browsing
= !_cups_strcasecmp(value
, "yes") ||
1012 !_cups_strcasecmp(value
, "on") ||
1013 !_cups_strcasecmp(value
, "true");
1015 else if (!_cups_strcasecmp(line
, "LogLevel"))
1017 debug_logging
= !_cups_strncasecmp(value
, "debug", 5);
1019 else if (!_cups_strcasecmp(line
, "<Policy") &&
1020 !_cups_strcasecmp(value
, "default"))
1024 else if (!_cups_strcasecmp(line
, "</Policy>"))
1028 else if (!_cups_strcasecmp(line
, "<Limit") && in_policy
&& value
)
1031 * See if the policy limit is for the Cancel-Job operation...
1034 char *valptr
; /* Pointer into value */
1039 for (valptr
= value
; *valptr
&& !_cups_isspace(*valptr
); valptr
++);
1044 if (!_cups_strcasecmp(value
, "cancel-job") ||
1045 !_cups_strcasecmp(value
, "all"))
1051 for (value
= valptr
; _cups_isspace(*value
); value
++);
1054 else if (!_cups_strcasecmp(line
, "</Limit>"))
1058 else if (!_cups_strcasecmp(line
, "Require") && in_cancel_job
)
1062 else if (!_cups_strcasecmp(line
, "<Location") && value
)
1064 in_admin_location
= !_cups_strcasecmp(value
, "/admin");
1067 else if (!_cups_strcasecmp(line
, "</Location>"))
1069 in_admin_location
= 0;
1072 else if (!_cups_strcasecmp(line
, "Allow") && value
&&
1073 _cups_strcasecmp(value
, "localhost") &&
1074 _cups_strcasecmp(value
, "127.0.0.1")
1077 #endif /* AF_LOCAL */
1079 && strcmp(value
, "::1")
1080 #endif /* AF_INET6 */
1083 if (in_admin_location
)
1085 else if (!_cups_strcasecmp(value
, "all"))
1088 else if (line
[0] != '<' && !in_location
&& !in_policy
&&
1089 _cups_strcasecmp(line
, "Allow") &&
1090 _cups_strcasecmp(line
, "AuthType") &&
1091 _cups_strcasecmp(line
, "Deny") &&
1092 _cups_strcasecmp(line
, "Order") &&
1093 _cups_strcasecmp(line
, "Require") &&
1094 _cups_strcasecmp(line
, "Satisfy"))
1095 cg
->cupsd_num_settings
= cupsAddOption(line
, value
,
1096 cg
->cupsd_num_settings
,
1097 &(cg
->cupsd_settings
));
1100 cupsFileClose(cupsd
);
1102 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1103 debug_logging
? "1" : "0",
1104 cg
->cupsd_num_settings
,
1105 &(cg
->cupsd_settings
));
1107 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1108 (remote_access
&& remote_admin
) ?
1110 cg
->cupsd_num_settings
,
1111 &(cg
->cupsd_settings
));
1113 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
1114 remote_any
? "1" : "0",
1115 cg
->cupsd_num_settings
,
1116 &(cg
->cupsd_settings
));
1118 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1119 (remote_access
&& browsing
) ? "1" :
1121 cg
->cupsd_num_settings
,
1122 &(cg
->cupsd_settings
));
1124 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1125 cancel_policy
? "1" : "0",
1126 cg
->cupsd_num_settings
,
1127 &(cg
->cupsd_settings
));
1129 else if (status
!= HTTP_STATUS_NOT_MODIFIED
)
1130 invalidate_cupsd_cache(cg
);
1133 * Remove any temporary files and copy the settings array...
1139 for (i
= cg
->cupsd_num_settings
, setting
= cg
->cupsd_settings
;
1142 *num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1143 *num_settings
, settings
);
1145 return (cg
->cupsd_num_settings
> 0);
1150 * 'cupsAdminSetServerSettings()' - Set settings on the server.
1152 * @since CUPS 1.3/OS X 10.5@
1155 int /* O - 1 on success, 0 on failure */
1156 cupsAdminSetServerSettings(
1157 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1158 int num_settings
, /* I - Number of settings */
1159 cups_option_t
*settings
) /* I - Settings */
1161 int i
; /* Looping var */
1162 http_status_t status
; /* GET/PUT status */
1163 const char *server_port_env
; /* SERVER_PORT env var */
1164 int server_port
; /* IPP port for server */
1165 cups_file_t
*cupsd
; /* cupsd.conf file */
1166 char cupsdconf
[1024]; /* cupsd.conf filename */
1167 int remote
; /* Remote cupsd.conf file? */
1168 char tempfile
[1024]; /* Temporary new cupsd.conf */
1169 cups_file_t
*temp
; /* Temporary file */
1170 char line
[1024], /* Line from cupsd.conf file */
1171 *value
; /* Value on line */
1172 int linenum
, /* Line number in file */
1173 in_location
, /* In a location section? */
1174 in_policy
, /* In a policy section? */
1175 in_default_policy
, /* In the default policy section? */
1176 in_cancel_job
, /* In a cancel-job section? */
1177 in_admin_location
, /* In the /admin location? */
1178 in_conf_location
, /* In the /admin/conf location? */
1179 in_log_location
, /* In the /admin/log location? */
1180 in_root_location
; /* In the / location? */
1181 const char *val
; /* Setting value */
1182 int share_printers
, /* Share local printers */
1183 remote_admin
, /* Remote administration allowed? */
1184 remote_any
, /* Remote access from anywhere? */
1185 user_cancel_any
, /* Cancel-job policy set? */
1186 debug_logging
; /* LogLevel debug set? */
1187 int wrote_port_listen
, /* Wrote the port/listen lines? */
1188 wrote_browsing
, /* Wrote the browsing lines? */
1189 wrote_policy
, /* Wrote the policy? */
1190 wrote_loglevel
, /* Wrote the LogLevel line? */
1191 wrote_admin_location
, /* Wrote the /admin location? */
1192 wrote_conf_location
, /* Wrote the /admin/conf location? */
1193 wrote_log_location
, /* Wrote the /admin/log location? */
1194 wrote_root_location
; /* Wrote the / location? */
1195 int indent
; /* Indentation */
1196 int cupsd_num_settings
; /* New number of settings */
1197 int old_share_printers
, /* Share local printers */
1198 old_remote_admin
, /* Remote administration allowed? */
1199 old_user_cancel_any
, /* Cancel-job policy set? */
1200 old_debug_logging
; /* LogLevel debug set? */
1201 cups_option_t
*cupsd_settings
, /* New settings */
1202 *setting
; /* Current setting */
1203 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1207 * Range check input...
1211 http
= _cupsConnect();
1213 if (!http
|| !num_settings
|| !settings
)
1215 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1221 * Get the cupsd.conf file...
1224 if (get_cupsd_conf(http
, cg
, 0, cupsdconf
, sizeof(cupsdconf
),
1225 &remote
) == HTTP_STATUS_OK
)
1227 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
1229 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
1237 * Get current settings...
1240 if (!cupsAdminGetServerSettings(http
, &cupsd_num_settings
,
1244 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, cupsd_num_settings
,
1245 cupsd_settings
)) != NULL
)
1246 old_debug_logging
= atoi(val
);
1248 old_debug_logging
= 0;
1250 DEBUG_printf(("1cupsAdminSetServerSettings: old debug_logging=%d",
1251 old_debug_logging
));
1253 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, cupsd_num_settings
,
1254 cupsd_settings
)) != NULL
)
1255 old_remote_admin
= atoi(val
);
1257 old_remote_admin
= 0;
1259 DEBUG_printf(("1cupsAdminSetServerSettings: old remote_admin=%d",
1262 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, cupsd_num_settings
,
1263 cupsd_settings
)) != NULL
)
1264 remote_any
= atoi(val
);
1268 DEBUG_printf(("1cupsAdminSetServerSettings: old remote_any=%d",
1271 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, cupsd_num_settings
,
1272 cupsd_settings
)) != NULL
)
1273 old_share_printers
= atoi(val
);
1275 old_share_printers
= 0;
1277 DEBUG_printf(("1cupsAdminSetServerSettings: old share_printers=%d",
1278 old_share_printers
));
1280 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, cupsd_num_settings
,
1281 cupsd_settings
)) != NULL
)
1282 old_user_cancel_any
= atoi(val
);
1284 old_user_cancel_any
= 0;
1286 DEBUG_printf(("1cupsAdminSetServerSettings: old user_cancel_any=%d",
1287 old_user_cancel_any
));
1289 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
1292 * Get basic settings...
1295 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, num_settings
,
1298 debug_logging
= atoi(val
);
1300 if (debug_logging
== old_debug_logging
)
1303 * No change to this setting...
1312 DEBUG_printf(("1cupsAdminSetServerSettings: debug_logging=%d",
1315 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, num_settings
,
1317 remote_any
= atoi(val
);
1319 DEBUG_printf(("1cupsAdminSetServerSettings: remote_any=%d",
1322 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, num_settings
,
1325 remote_admin
= atoi(val
);
1327 if (remote_admin
== old_remote_admin
)
1330 * No change to this setting...
1339 DEBUG_printf(("1cupsAdminSetServerSettings: remote_admin=%d",
1342 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, num_settings
,
1345 share_printers
= atoi(val
);
1347 if (share_printers
== old_share_printers
)
1350 * No change to this setting...
1353 share_printers
= -1;
1357 share_printers
= -1;
1359 DEBUG_printf(("1cupsAdminSetServerSettings: share_printers=%d",
1362 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, num_settings
,
1365 user_cancel_any
= atoi(val
);
1367 if (user_cancel_any
== old_user_cancel_any
)
1370 * No change to this setting...
1373 user_cancel_any
= -1;
1377 user_cancel_any
= -1;
1379 DEBUG_printf(("1cupsAdminSetServerSettings: user_cancel_any=%d",
1383 * Create a temporary file for the new cupsd.conf file...
1386 if ((temp
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1388 cupsFileClose(cupsd
);
1393 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
1398 * Copy the old file to the new, making changes along the way...
1401 cupsd_num_settings
= 0;
1402 in_admin_location
= 0;
1404 in_conf_location
= 0;
1405 in_default_policy
= 0;
1407 in_log_location
= 0;
1409 in_root_location
= 0;
1411 wrote_admin_location
= 0;
1413 wrote_conf_location
= 0;
1414 wrote_log_location
= 0;
1417 wrote_port_listen
= 0;
1418 wrote_root_location
= 0;
1421 if ((server_port_env
= getenv("SERVER_PORT")) != NULL
)
1423 if ((server_port
= atoi(server_port_env
)) <= 0)
1424 server_port
= ippPort();
1427 server_port
= ippPort();
1429 if (server_port
<= 0)
1430 server_port
= IPP_PORT
;
1432 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
1434 if ((!_cups_strcasecmp(line
, "Port") || !_cups_strcasecmp(line
, "Listen")) &&
1435 (remote_admin
>= 0 || remote_any
> 0 || share_printers
>= 0))
1437 if (!wrote_port_listen
)
1439 wrote_port_listen
= 1;
1441 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1443 cupsFilePuts(temp
, "# Allow remote access\n");
1444 cupsFilePrintf(temp
, "Port %d\n", server_port
);
1448 cupsFilePuts(temp
, "# Only listen for connections from the local "
1450 cupsFilePrintf(temp
, "Listen localhost:%d\n", server_port
);
1453 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1454 if ((!value
|| strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)) &&
1455 !access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1456 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1457 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1459 else if (value
&& value
[0] == '/'
1460 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1461 && strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)
1462 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1464 cupsFilePrintf(temp
, "Listen %s\n", value
);
1466 else if ((!_cups_strcasecmp(line
, "Browsing") ||
1467 !_cups_strcasecmp(line
, "BrowseLocalProtocols")) &&
1468 share_printers
>= 0)
1470 if (!wrote_browsing
)
1472 int new_share_printers
= (share_printers
> 0 ||
1473 (share_printers
== -1 &&
1474 old_share_printers
> 0));
1478 if (new_share_printers
)
1480 const char *localp
= cupsGetOption("BrowseLocalProtocols",
1481 num_settings
, settings
);
1483 if (!localp
|| !localp
[0])
1484 localp
= cupsGetOption("BrowseLocalProtocols", cupsd_num_settings
,
1487 cupsFilePuts(temp
, "# Share local printers on the local network.\n");
1488 cupsFilePuts(temp
, "Browsing On\n");
1491 localp
= CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS
;
1493 cupsFilePrintf(temp
, "BrowseLocalProtocols %s\n", localp
);
1495 cupsd_num_settings
= cupsAddOption("BrowseLocalProtocols", localp
,
1501 cupsFilePuts(temp
, "# Disable printer sharing.\n");
1502 cupsFilePuts(temp
, "Browsing Off\n");
1506 else if (!_cups_strcasecmp(line
, "LogLevel") && debug_logging
>= 0)
1513 "# Show troubleshooting information in error_log.\n");
1514 cupsFilePuts(temp
, "LogLevel debug\n");
1518 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1519 cupsFilePuts(temp
, "LogLevel " CUPS_DEFAULT_LOG_LEVEL
"\n");
1522 else if (!_cups_strcasecmp(line
, "<Policy"))
1524 in_default_policy
= !_cups_strcasecmp(value
, "default");
1527 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1530 else if (!_cups_strcasecmp(line
, "</Policy>"))
1533 if (!wrote_policy
&& in_default_policy
)
1537 if (!user_cancel_any
)
1538 cupsFilePuts(temp
, " # Only the owner or an administrator can "
1540 " <Limit Cancel-Job>\n"
1541 " Order deny,allow\n"
1542 " Require user @OWNER "
1543 CUPS_DEFAULT_PRINTOPERATOR_AUTH
"\n"
1548 in_default_policy
= 0;
1550 cupsFilePuts(temp
, "</Policy>\n");
1552 else if (!_cups_strcasecmp(line
, "<Location"))
1556 if (!strcmp(value
, "/admin"))
1557 in_admin_location
= 1;
1558 else if (!strcmp(value
, "/admin/conf"))
1559 in_conf_location
= 1;
1560 else if (!strcmp(value
, "/admin/log"))
1561 in_log_location
= 1;
1562 else if (!strcmp(value
, "/"))
1563 in_root_location
= 1;
1565 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1567 else if (!_cups_strcasecmp(line
, "</Location>"))
1571 if (in_admin_location
&& remote_admin
>= 0)
1573 wrote_admin_location
= 1;
1576 cupsFilePuts(temp
, " # Allow remote administration...\n");
1577 else if (remote_admin
== 0)
1578 cupsFilePuts(temp
, " # Restrict access to the admin pages...\n");
1580 cupsFilePuts(temp
, " Order allow,deny\n");
1583 cupsFilePrintf(temp
, " Allow %s\n",
1584 remote_any
> 0 ? "all" : "@LOCAL");
1586 else if (in_conf_location
&& remote_admin
>= 0)
1588 wrote_conf_location
= 1;
1591 cupsFilePuts(temp
, " # Allow remote access to the configuration "
1594 cupsFilePuts(temp
, " # Restrict access to the configuration "
1597 cupsFilePuts(temp
, " Order allow,deny\n");
1600 cupsFilePrintf(temp
, " Allow %s\n",
1601 remote_any
> 0 ? "all" : "@LOCAL");
1603 else if (in_log_location
&& remote_admin
>= 0)
1605 wrote_log_location
= 1;
1608 cupsFilePuts(temp
, " # Allow remote access to the log "
1611 cupsFilePuts(temp
, " # Restrict access to the log "
1614 cupsFilePuts(temp
, " Order allow,deny\n");
1617 cupsFilePrintf(temp
, " Allow %s\n",
1618 remote_any
> 0 ? "all" : "@LOCAL");
1620 else if (in_root_location
&&
1621 (remote_admin
>= 0 || remote_any
> 0 || share_printers
>= 0))
1623 wrote_root_location
= 1;
1625 if (remote_admin
> 0 && share_printers
> 0)
1626 cupsFilePuts(temp
, " # Allow shared printing and remote "
1627 "administration...\n");
1628 else if (remote_admin
> 0)
1629 cupsFilePuts(temp
, " # Allow remote administration...\n");
1630 else if (share_printers
> 0)
1631 cupsFilePuts(temp
, " # Allow shared printing...\n");
1632 else if (remote_any
> 0)
1633 cupsFilePuts(temp
, " # Allow remote access...\n");
1635 cupsFilePuts(temp
, " # Restrict access to the server...\n");
1637 cupsFilePuts(temp
, " Order allow,deny\n");
1639 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1640 cupsFilePrintf(temp
, " Allow %s\n",
1641 remote_any
> 0 ? "all" : "@LOCAL");
1644 in_admin_location
= 0;
1645 in_conf_location
= 0;
1646 in_log_location
= 0;
1647 in_root_location
= 0;
1649 cupsFilePuts(temp
, "</Location>\n");
1651 else if (!_cups_strcasecmp(line
, "<Limit"))
1653 if (in_default_policy
)
1656 * See if the policy limit is for the Cancel-Job operation...
1659 char *valptr
; /* Pointer into value */
1662 if (!_cups_strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1665 * Don't write anything for this limit section...
1672 cupsFilePrintf(temp
, "%*s%s", indent
, "", line
);
1676 for (valptr
= value
; *valptr
&& !_cups_isspace(*valptr
); valptr
++);
1681 if (!_cups_strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1684 * Write everything except for this definition...
1690 cupsFilePrintf(temp
, " %s", value
);
1692 for (value
= valptr
; _cups_isspace(*value
); value
++);
1695 cupsFilePuts(temp
, ">\n");
1699 cupsFilePrintf(temp
, "%*s%s %s>\n", indent
, "", line
, value
);
1703 else if (!_cups_strcasecmp(line
, "</Limit>") && in_cancel_job
)
1707 if (in_cancel_job
== 1)
1708 cupsFilePuts(temp
, " </Limit>\n");
1712 if (!user_cancel_any
)
1713 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1715 " <Limit Cancel-Job>\n"
1716 " Order deny,allow\n"
1717 " Require user @OWNER "
1718 CUPS_DEFAULT_PRINTOPERATOR_AUTH
"\n"
1723 else if ((((in_admin_location
|| in_conf_location
|| in_root_location
) &&
1724 (remote_admin
>= 0 || remote_any
> 0)) ||
1725 (in_root_location
&& share_printers
>= 0)) &&
1726 (!_cups_strcasecmp(line
, "Allow") || !_cups_strcasecmp(line
, "Deny") ||
1727 !_cups_strcasecmp(line
, "Order")))
1729 else if (in_cancel_job
== 2)
1731 else if (line
[0] == '<')
1735 cupsFilePrintf(temp
, "%*s%s %s>\n", indent
, "", line
, value
);
1743 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1746 else if (!in_policy
&& !in_location
&&
1747 (val
= cupsGetOption(line
, num_settings
, settings
)) != NULL
)
1750 * Replace this directive's value with the new one...
1753 cupsd_num_settings
= cupsAddOption(line
, val
, cupsd_num_settings
,
1757 * Write the new value in its place, without indentation since we
1758 * only support setting root directives, not in sections...
1761 cupsFilePrintf(temp
, "%s %s\n", line
, val
);
1765 if (!in_policy
&& !in_location
)
1768 * Record the non-policy, non-location directives that we find
1769 * in the server settings, since we cache this info and record it
1770 * in cupsAdminGetServerSettings()...
1773 cupsd_num_settings
= cupsAddOption(line
, value
, cupsd_num_settings
,
1777 cupsFilePrintf(temp
, "%*s%s %s\n", indent
, "", line
, value
);
1780 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1784 * Write any missing info...
1787 if (!wrote_browsing
&& share_printers
>= 0)
1789 if (share_printers
> 0)
1791 cupsFilePuts(temp
, "# Share local printers on the local network.\n");
1792 cupsFilePuts(temp
, "Browsing On\n");
1796 cupsFilePuts(temp
, "# Disable printer sharing and shared printers.\n");
1797 cupsFilePuts(temp
, "Browsing Off\n");
1801 if (!wrote_loglevel
&& debug_logging
>= 0)
1805 cupsFilePuts(temp
, "# Show troubleshooting information in error_log.\n");
1806 cupsFilePuts(temp
, "LogLevel debug\n");
1810 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1811 cupsFilePuts(temp
, "LogLevel " CUPS_DEFAULT_LOG_LEVEL
"\n");
1815 if (!wrote_port_listen
&&
1816 (remote_admin
>= 0 || remote_any
> 0 || share_printers
>= 0))
1818 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1820 cupsFilePuts(temp
, "# Allow remote access\n");
1821 cupsFilePrintf(temp
, "Port %d\n", ippPort());
1826 "# Only listen for connections from the local machine.\n");
1827 cupsFilePrintf(temp
, "Listen localhost:%d\n", ippPort());
1830 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1831 if (!access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1832 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1833 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1836 if (!wrote_root_location
&&
1837 (remote_admin
>= 0 || remote_any
> 0 || share_printers
>= 0))
1839 if (remote_admin
> 0 && share_printers
> 0)
1841 "# Allow shared printing and remote administration...\n");
1842 else if (remote_admin
> 0)
1843 cupsFilePuts(temp
, "# Allow remote administration...\n");
1844 else if (share_printers
> 0)
1845 cupsFilePuts(temp
, "# Allow shared printing...\n");
1846 else if (remote_any
> 0)
1847 cupsFilePuts(temp
, "# Allow remote access...\n");
1849 cupsFilePuts(temp
, "# Restrict access to the server...\n");
1851 cupsFilePuts(temp
, "<Location />\n"
1852 " Order allow,deny\n");
1854 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1855 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1857 cupsFilePuts(temp
, "</Location>\n");
1860 if (!wrote_admin_location
&& remote_admin
>= 0)
1863 cupsFilePuts(temp
, "# Allow remote administration...\n");
1865 cupsFilePuts(temp
, "# Restrict access to the admin pages...\n");
1867 cupsFilePuts(temp
, "<Location /admin>\n"
1868 " Order allow,deny\n");
1871 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1873 cupsFilePuts(temp
, "</Location>\n");
1876 if (!wrote_conf_location
&& remote_admin
>= 0)
1880 "# Allow remote access to the configuration files...\n");
1882 cupsFilePuts(temp
, "# Restrict access to the configuration files...\n");
1884 cupsFilePuts(temp
, "<Location /admin/conf>\n"
1885 " AuthType Default\n"
1886 " Require user @SYSTEM\n"
1887 " Order allow,deny\n");
1890 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1892 cupsFilePuts(temp
, "</Location>\n");
1895 if (!wrote_log_location
&& remote_admin
>= 0)
1899 "# Allow remote access to the log files...\n");
1901 cupsFilePuts(temp
, "# Restrict access to the log files...\n");
1903 cupsFilePuts(temp
, "<Location /admin/log>\n"
1904 " AuthType Default\n"
1905 " Require user @SYSTEM\n"
1906 " Order allow,deny\n");
1909 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1911 cupsFilePuts(temp
, "</Location>\n");
1914 if (!wrote_policy
&& user_cancel_any
>= 0)
1916 cupsFilePuts(temp
, "<Policy default>\n"
1917 " # Job-related operations must be done by the owner "
1918 "or an administrator...\n"
1919 " <Limit Send-Document Send-URI Hold-Job Release-Job "
1920 "Restart-Job Purge-Jobs Set-Job-Attributes "
1921 "Create-Job-Subscription Renew-Subscription "
1922 "Cancel-Subscription Get-Notifications Reprocess-Job "
1923 "Cancel-Current-Job Suspend-Current-Job Resume-Job "
1925 " Require user @OWNER @SYSTEM\n"
1926 " Order deny,allow\n"
1928 " # All administration operations require an "
1929 "administrator to authenticate...\n"
1930 " <Limit Pause-Printer Resume-Printer "
1931 "Set-Printer-Attributes Enable-Printer "
1932 "Disable-Printer Pause-Printer-After-Current-Job "
1933 "Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer "
1934 "Activate-Printer Restart-Printer Shutdown-Printer "
1935 "Startup-Printer Promote-Job Schedule-Job-After "
1936 "CUPS-Add-Printer CUPS-Delete-Printer "
1937 "CUPS-Add-Class CUPS-Delete-Class "
1938 "CUPS-Accept-Jobs CUPS-Reject-Jobs "
1939 "CUPS-Set-Default CUPS-Add-Device CUPS-Delete-Device>\n"
1940 " AuthType Default\n"
1941 " Require user @SYSTEM\n"
1942 " Order deny,allow\n"
1945 if (!user_cancel_any
)
1946 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1948 " <Limit Cancel-Job>\n"
1949 " Order deny,allow\n"
1950 " Require user @OWNER "
1951 CUPS_DEFAULT_PRINTOPERATOR_AUTH
"\n"
1954 cupsFilePuts(temp
, " <Limit All>\n"
1955 " Order deny,allow\n"
1960 for (i
= num_settings
, setting
= settings
; i
> 0; i
--, setting
++)
1961 if (setting
->name
[0] != '_' &&
1962 _cups_strcasecmp(setting
->name
, "Listen") &&
1963 _cups_strcasecmp(setting
->name
, "Port") &&
1964 !cupsGetOption(setting
->name
, cupsd_num_settings
, cupsd_settings
))
1967 * Add this directive to the list of directives we have written...
1970 cupsd_num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1971 cupsd_num_settings
, &cupsd_settings
);
1974 * Write the new value, without indentation since we only support
1975 * setting root directives, not in sections...
1978 cupsFilePrintf(temp
, "%s %s\n", setting
->name
, setting
->value
);
1981 cupsFileClose(cupsd
);
1982 cupsFileClose(temp
);
1985 * Upload the configuration file to the server...
1988 status
= cupsPutFile(http
, "/admin/conf/cupsd.conf", tempfile
);
1990 if (status
== HTTP_STATUS_CREATED
)
1993 * Updated OK, add the basic settings...
1996 if (debug_logging
>= 0)
1997 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1998 debug_logging
? "1" : "0",
1999 cupsd_num_settings
, &cupsd_settings
);
2001 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
2002 old_debug_logging
? "1" : "0",
2003 cupsd_num_settings
, &cupsd_settings
);
2005 if (remote_admin
>= 0)
2006 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
2007 remote_admin
? "1" : "0",
2008 cupsd_num_settings
, &cupsd_settings
);
2010 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
2011 old_remote_admin
? "1" : "0",
2012 cupsd_num_settings
, &cupsd_settings
);
2014 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
2015 remote_any
? "1" : "0",
2016 cupsd_num_settings
, &cupsd_settings
);
2018 if (share_printers
>= 0)
2019 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
2020 share_printers
? "1" : "0",
2021 cupsd_num_settings
, &cupsd_settings
);
2023 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
2024 old_share_printers
? "1" : "0",
2025 cupsd_num_settings
, &cupsd_settings
);
2027 if (user_cancel_any
>= 0)
2028 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
2029 user_cancel_any
? "1" : "0",
2030 cupsd_num_settings
, &cupsd_settings
);
2032 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
2033 old_user_cancel_any
? "1" : "0",
2034 cupsd_num_settings
, &cupsd_settings
);
2037 * Save the new values...
2040 invalidate_cupsd_cache(cg
);
2042 cg
->cupsd_num_settings
= cupsd_num_settings
;
2043 cg
->cupsd_settings
= cupsd_settings
;
2044 cg
->cupsd_update
= time(NULL
);
2046 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
2049 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
2052 * Remote our temp files and return...
2060 return (status
== HTTP_STATUS_CREATED
);
2065 * 'do_samba_command()' - Do a SAMBA command.
2068 static int /* O - Status of command */
2069 do_samba_command(const char *command
, /* I - Command to run */
2070 const char *address
, /* I - Address for command */
2071 const char *subcmd
, /* I - Sub-command */
2072 const char *authfile
, /* I - Samba authentication file */
2073 FILE *logfile
) /* I - Optional log file */
2076 return (1); /* Always fail on Windows... */
2079 int status
; /* Status of command */
2080 int pid
; /* Process ID of child */
2084 _cupsLangPrintf(logfile
,
2085 _("Running command: %s %s -N -A %s -c \'%s\'"),
2086 command
, address
, authfile
, subcmd
);
2088 if ((pid
= fork()) == 0)
2091 * Child goes here, redirect stdin/out/err and execute the command...
2094 int fd
= open("/dev/null", O_RDONLY
);
2103 dup2(fileno(logfile
), 1);
2104 else if ((fd
= open("/dev/null", O_WRONLY
)) > 1)
2112 execlp(command
, command
, address
, "-N", "-A", authfile
, "-c", subcmd
,
2121 _cupsLangPrintf(logfile
, _("Unable to run \"%s\": %s"),
2122 command
, strerror(errno
));
2127 * Wait for the process to complete...
2130 while (wait(&status
) != pid
);
2134 _cupsLangPuts(logfile
, "");
2136 DEBUG_printf(("9do_samba_command: status=%d", status
));
2138 if (WIFEXITED(status
))
2139 return (WEXITSTATUS(status
));
2141 return (-WTERMSIG(status
));
2147 * 'get_cupsd_conf()' - Get the current cupsd.conf file.
2150 static http_status_t
/* O - Status of request */
2152 http_t
*http
, /* I - Connection to server */
2153 _cups_globals_t
*cg
, /* I - Global data */
2154 time_t last_update
, /* I - Last update time for file */
2155 char *name
, /* I - Filename buffer */
2156 size_t namesize
, /* I - Size of filename buffer */
2157 int *remote
) /* O - Remote file? */
2159 int fd
; /* Temporary file descriptor */
2161 struct stat info
; /* cupsd.conf file information */
2163 http_status_t status
; /* Status of getting cupsd.conf */
2164 char host
[HTTP_MAX_HOST
]; /* Hostname for connection */
2168 * See if we already have the data we need...
2171 httpGetHostname(http
, host
, sizeof(host
));
2173 if (_cups_strcasecmp(cg
->cupsd_hostname
, host
))
2174 invalidate_cupsd_cache(cg
);
2176 snprintf(name
, namesize
, "%s/cupsd.conf", cg
->cups_serverroot
);
2180 if (!_cups_strcasecmp(host
, "localhost") && !access(name
, R_OK
))
2183 * Read the local file rather than using HTTP...
2186 if (stat(name
, &info
))
2188 char message
[1024]; /* Message string */
2191 snprintf(message
, sizeof(message
),
2192 _cupsLangString(cupsLangDefault(), _("stat of %s failed: %s")),
2193 name
, strerror(errno
));
2194 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
2198 return (HTTP_STATUS_SERVER_ERROR
);
2200 else if (last_update
&& info
.st_mtime
<= last_update
)
2201 status
= HTTP_STATUS_NOT_MODIFIED
;
2203 status
= HTTP_STATUS_OK
;
2209 * Read cupsd.conf via a HTTP GET request...
2212 if ((fd
= cupsTempFd(name
, (int)namesize
)) < 0)
2216 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
2218 invalidate_cupsd_cache(cg
);
2220 return (HTTP_STATUS_SERVER_ERROR
);
2225 httpClearFields(http
);
2228 httpSetField(http
, HTTP_FIELD_IF_MODIFIED_SINCE
,
2229 httpGetDateString(last_update
));
2231 status
= cupsGetFd(http
, "/admin/conf/cupsd.conf", fd
);
2235 if (status
!= HTTP_STATUS_OK
)
2247 * 'invalidate_cupsd_cache()' - Invalidate the cached cupsd.conf settings.
2251 invalidate_cupsd_cache(
2252 _cups_globals_t
*cg
) /* I - Global data */
2254 cupsFreeOptions(cg
->cupsd_num_settings
, cg
->cupsd_settings
);
2256 cg
->cupsd_hostname
[0] = '\0';
2257 cg
->cupsd_update
= 0;
2258 cg
->cupsd_num_settings
= 0;
2259 cg
->cupsd_settings
= NULL
;
2264 * 'write_option()' - Write a CUPS option to a PPD file.
2268 write_option(cups_file_t
*dstfp
, /* I - PPD file */
2269 int order
, /* I - Order dependency */
2270 const char *name
, /* I - Option name */
2271 const char *text
, /* I - Option text */
2272 const char *attrname
, /* I - Attribute name */
2273 ipp_attribute_t
*suppattr
, /* I - IPP -supported attribute */
2274 ipp_attribute_t
*defattr
, /* I - IPP -default attribute */
2275 int defval
, /* I - Default value number */
2276 int valcount
) /* I - Number of values */
2278 int i
; /* Looping var */
2281 cupsFilePrintf(dstfp
, "*JCLOpenUI *%s/%s: PickOne\n"
2282 "*OrderDependency: %d JCLSetup *%s\n",
2283 name
, text
, order
, name
);
2285 if (defattr
->value_tag
== IPP_TAG_INTEGER
)
2288 * Do numeric options with a range or list...
2291 cupsFilePrintf(dstfp
, "*Default%s: %d\n", name
,
2292 defattr
->values
[defval
].integer
);
2294 if (suppattr
->value_tag
== IPP_TAG_RANGE
)
2297 * List each number in the range...
2300 for (i
= suppattr
->values
[0].range
.lower
;
2301 i
<= suppattr
->values
[0].range
.upper
;
2304 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, i
);
2307 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n",
2309 else if (defval
== 0)
2310 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
, i
);
2311 else if (defval
< (valcount
- 1))
2312 cupsFilePrintf(dstfp
, ",%d\"\n", i
);
2314 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", i
);
2320 * List explicit numbers...
2323 for (i
= 0; i
< suppattr
->num_values
; i
++)
2325 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, suppattr
->values
[i
].integer
);
2328 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n", attrname
,
2329 suppattr
->values
[i
].integer
);
2330 else if (defval
== 0)
2331 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
,
2332 suppattr
->values
[i
].integer
);
2333 else if (defval
< (valcount
- 1))
2334 cupsFilePrintf(dstfp
, ",%d\"\n", suppattr
->values
[i
].integer
);
2336 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", suppattr
->values
[i
].integer
);
2343 * Do text options with a list...
2346 cupsFilePrintf(dstfp
, "*Default%s: %s\n", name
,
2347 defattr
->values
[defval
].string
.text
);
2349 for (i
= 0; i
< suppattr
->num_values
; i
++)
2351 cupsFilePrintf(dstfp
, "*%s %s: \"", name
,
2352 suppattr
->values
[i
].string
.text
);
2355 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\n\"\n*End\n", attrname
,
2356 suppattr
->values
[i
].string
.text
);
2357 else if (defval
== 0)
2358 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\"\n", attrname
,
2359 suppattr
->values
[i
].string
.text
);
2360 else if (defval
< (valcount
- 1))
2361 cupsFilePrintf(dstfp
, ",%s\"\n", suppattr
->values
[i
].string
.text
);
2363 cupsFilePrintf(dstfp
, ",%s\n\"\n*End\n",
2364 suppattr
->values
[i
].string
.text
);
2368 cupsFilePrintf(dstfp
, "*JCLCloseUI: *%s\n\n", name
);