2 * Administration utility API definitions for CUPS.
4 * Copyright 2007-2016 by Apple Inc.
5 * Copyright 2001-2007 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include "cups-private.h"
16 #include "adminutil.h"
22 # include <sys/wait.h>
30 static int do_samba_command(const char *command
,
32 const char *subcommand
,
35 static http_status_t
get_cupsd_conf(http_t
*http
, _cups_globals_t
*cg
,
36 time_t last_update
, char *name
,
37 size_t namelen
, int *remote
);
38 static void invalidate_cupsd_cache(_cups_globals_t
*cg
);
39 static void write_option(cups_file_t
*dstfp
, int order
,
40 const char *name
, const char *text
,
42 ipp_attribute_t
*suppattr
,
43 ipp_attribute_t
*defattr
, int defval
,
48 * 'cupsAdminCreateWindowsPPD()' - Create the Windows PPD file for a printer.
53 char * /* O - PPD file or NULL */
54 cupsAdminCreateWindowsPPD(
55 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
56 const char *dest
, /* I - Printer or class */
57 char *buffer
, /* I - Filename buffer */
58 int bufsize
) /* I - Size of filename buffer */
60 const char *src
; /* Source PPD filename */
61 cups_file_t
*srcfp
, /* Source PPD file */
62 *dstfp
; /* Destination PPD file */
63 ipp_t
*request
, /* IPP request */
64 *response
; /* IPP response */
65 ipp_attribute_t
*suppattr
, /* IPP -supported attribute */
66 *defattr
; /* IPP -default attribute */
67 cups_lang_t
*language
; /* Current language */
68 char line
[256], /* Line from PPD file */
69 junk
[256], /* Extra junk to throw away */
70 *ptr
, /* Pointer into line */
71 uri
[1024], /* Printer URI */
72 option
[41], /* Option */
73 choice
[41]; /* Choice */
74 int jcloption
, /* In a JCL option? */
75 jclorder
, /* Next JCL order dependency */
76 linenum
; /* Current line number */
77 time_t curtime
; /* Current time */
78 struct tm
*curdate
; /* Current date */
79 static const char * const pattrs
[] = /* Printer attributes we want */
81 "job-hold-until-supported",
82 "job-hold-until-default",
83 "job-sheets-supported",
85 "job-priority-supported",
86 "job-priority-default"
91 * Range check the input...
98 http
= _cupsConnect();
100 if (!http
|| !dest
|| !buffer
|| bufsize
< 2)
104 * Get the PPD file...
107 if ((src
= cupsGetPPD2(http
, dest
)) == NULL
)
111 * Get the supported banner pages, etc. for the printer...
114 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
116 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
117 "localhost", 0, "/printers/%s", dest
);
118 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
119 "printer-uri", NULL
, uri
);
121 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
122 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
126 * Do the request and get back a response...
129 response
= cupsDoRequest(http
, request
, "/");
130 if (!response
|| cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
137 * Open the original PPD file...
140 if ((srcfp
= cupsFileOpen(src
, "rb")) == NULL
)
144 * Create a temporary output file using the destination buffer...
147 if ((dstfp
= cupsTempFile2(buffer
, bufsize
)) == NULL
)
149 cupsFileClose(srcfp
);
157 * Write a new header explaining that this isn't the original PPD...
160 cupsFilePuts(dstfp
, "*PPD-Adobe: \"4.3\"\n");
162 curtime
= time(NULL
);
163 curdate
= gmtime(&curtime
);
165 cupsFilePrintf(dstfp
, "*%% Modified on %04d%02d%02d%02d%02d%02d+0000 "
166 "for CUPS Windows Driver\n",
167 curdate
->tm_year
+ 1900, curdate
->tm_mon
+ 1, curdate
->tm_mday
,
168 curdate
->tm_hour
, curdate
->tm_min
, curdate
->tm_sec
);
171 * Read the existing PPD file, converting all PJL commands to CUPS
172 * job ticket comments...
178 language
= cupsLangDefault();
180 while (cupsFileGets(srcfp
, line
, sizeof(line
)))
184 if (!strncmp(line
, "*PPD-Adobe:", 11))
187 * Already wrote the PPD header...
192 else if (!strncmp(line
, "*JCLBegin:", 10) ||
193 !strncmp(line
, "*JCLToPSInterpreter:", 20) ||
194 !strncmp(line
, "*JCLEnd:", 8) ||
195 !strncmp(line
, "*Protocols:", 11))
198 * Don't use existing JCL keywords; we'll create our own, below...
201 cupsFilePrintf(dstfp
, "*%% Commented out for CUPS Windows Driver...\n"
202 "*%%%s\n", line
+ 1);
205 else if (!strncmp(line
, "*JCLOpenUI", 10))
208 cupsFilePrintf(dstfp
, "%s\n", line
);
210 else if (!strncmp(line
, "*JCLCloseUI", 11))
213 cupsFilePrintf(dstfp
, "%s\n", line
);
215 else if (jcloption
&& !strncmp(line
, "*OrderDependency:", 17))
217 for (ptr
= line
+ 17; _cups_isspace(*ptr
); ptr
++);
219 ptr
= strchr(ptr
, ' ');
223 cupsFilePrintf(dstfp
, "*OrderDependency: %d%s\n", jclorder
, ptr
);
227 cupsFilePrintf(dstfp
, "%s\n", line
);
229 else if (jcloption
&&
230 strncmp(line
, "*End", 4) &&
231 strncmp(line
, "*Default", 8))
233 if ((ptr
= strchr(line
, ':')) == NULL
)
235 snprintf(line
, sizeof(line
),
236 _cupsLangString(language
, _("Missing value on line %d.")),
238 _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR
, line
, 0);
240 cupsFileClose(srcfp
);
241 cupsFileClose(dstfp
);
251 if ((ptr
= strchr(ptr
, '\"')) == NULL
)
253 snprintf(line
, sizeof(line
),
254 _cupsLangString(language
,
255 _("Missing double quote on line %d.")),
257 _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR
, line
, 0);
259 cupsFileClose(srcfp
);
260 cupsFileClose(dstfp
);
270 if (sscanf(line
, "*%40s%*[ \t]%40[^:/]", option
, choice
) != 2)
272 snprintf(line
, sizeof(line
),
273 _cupsLangString(language
,
274 _("Bad option + choice on line %d.")),
276 _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR
, line
, 0);
278 cupsFileClose(srcfp
);
279 cupsFileClose(dstfp
);
289 if (strchr(ptr
+ 1, '\"') == NULL
)
295 while (cupsFileGets(srcfp
, junk
, sizeof(junk
)) != NULL
)
299 if (!strncmp(junk
, "*End", 4))
304 snprintf(ptr
+ 1, sizeof(line
) - (size_t)(ptr
- line
+ 1),
305 "%%cupsJobTicket: %s=%s\n\"\n*End", option
, choice
);
307 cupsFilePrintf(dstfp
, "*%% Changed for CUPS Windows Driver...\n%s\n",
311 cupsFilePrintf(dstfp
, "%s\n", line
);
314 cupsFileClose(srcfp
);
319 _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR
, _("Empty PPD file."), 1);
321 cupsFileClose(dstfp
);
330 * Now add the CUPS-specific attributes and options...
333 cupsFilePuts(dstfp
, "\n*% CUPS Job Ticket support and options...\n");
334 cupsFilePuts(dstfp
, "*Protocols: PJL\n");
335 cupsFilePuts(dstfp
, "*JCLBegin: \"%!PS-Adobe-3.0<0A>\"\n");
336 cupsFilePuts(dstfp
, "*JCLToPSInterpreter: \"\"\n");
337 cupsFilePuts(dstfp
, "*JCLEnd: \"\"\n");
339 cupsFilePuts(dstfp
, "\n*OpenGroup: CUPS/CUPS Options\n\n");
341 if ((defattr
= ippFindAttribute(response
, "job-hold-until-default",
342 IPP_TAG_ZERO
)) != NULL
&&
343 (suppattr
= ippFindAttribute(response
, "job-hold-until-supported",
344 IPP_TAG_ZERO
)) != NULL
)
345 write_option(dstfp
, jclorder
++, "cupsJobHoldUntil", "Hold Until",
346 "job-hold-until", suppattr
, defattr
, 0, 1);
348 if ((defattr
= ippFindAttribute(response
, "job-priority-default",
349 IPP_TAG_INTEGER
)) != NULL
&&
350 (suppattr
= ippFindAttribute(response
, "job-priority-supported",
351 IPP_TAG_RANGE
)) != NULL
)
352 write_option(dstfp
, jclorder
++, "cupsJobPriority", "Priority",
353 "job-priority", suppattr
, defattr
, 0, 1);
355 if ((defattr
= ippFindAttribute(response
, "job-sheets-default",
356 IPP_TAG_ZERO
)) != NULL
&&
357 (suppattr
= ippFindAttribute(response
, "job-sheets-supported",
358 IPP_TAG_ZERO
)) != NULL
)
360 write_option(dstfp
, jclorder
++, "cupsJobSheetsStart", "Start Banner",
361 "job-sheets", suppattr
, defattr
, 0, 2);
362 write_option(dstfp
, jclorder
, "cupsJobSheetsEnd", "End Banner",
363 "job-sheets", suppattr
, defattr
, 1, 2);
366 cupsFilePuts(dstfp
, "*CloseGroup: CUPS\n");
367 cupsFileClose(dstfp
);
376 * 'cupsAdminExportSamba()' - Export a printer to Samba.
381 int /* O - 1 on success, 0 on failure */
382 cupsAdminExportSamba(
383 const char *dest
, /* I - Destination to export */
384 const char *ppd
, /* I - PPD file */
385 const char *samba_server
, /* I - Samba server */
386 const char *samba_user
, /* I - Samba username */
387 const char *samba_password
, /* I - Samba password */
388 FILE *logfile
) /* I - Log file, if any */
390 int status
; /* Status of Samba commands */
391 int have_drivers
; /* Have drivers? */
392 char file
[1024], /* File to test for */
393 authfile
[1024], /* Temporary authentication file */
394 address
[1024], /* Address for command */
395 subcmd
[1024], /* Sub-command */
396 message
[1024]; /* Error message */
397 cups_file_t
*fp
; /* Authentication file */
398 cups_lang_t
*language
; /* Current language */
399 _cups_globals_t
*cg
= _cupsGlobals();
404 * Range check input...
407 if (!dest
|| !ppd
|| !samba_server
|| !samba_user
|| !samba_password
)
409 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
414 * Create a temporary authentication file for Samba...
417 if ((fp
= cupsTempFile2(authfile
, sizeof(authfile
))) == NULL
)
419 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
423 cupsFilePrintf(fp
, "username = %s\n", samba_user
);
424 cupsFilePrintf(fp
, "password = %s\n", samba_password
);
428 * See which drivers are available; the new CUPS v6 and Adobe drivers
429 * depend on the Windows 2k PS driver, so copy that driver first:
440 language
= cupsLangDefault();
442 snprintf(file
, sizeof(file
), "%s/drivers/pscript5.dll", cg
->cups_datadir
);
443 if (!access(file
, 0))
448 * Windows 2k driver is installed; do the smbclient commands needed
449 * to copy the Win2k drivers over...
452 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
454 snprintf(subcmd
, sizeof(subcmd
),
456 "put %s W32X86/%s.ppd;"
457 "put %s/drivers/ps5ui.dll W32X86/ps5ui.dll;"
458 "put %s/drivers/pscript.hlp W32X86/pscript.hlp;"
459 "put %s/drivers/pscript.ntf W32X86/pscript.ntf;"
460 "put %s/drivers/pscript5.dll W32X86/pscript5.dll",
461 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
462 cg
->cups_datadir
, cg
->cups_datadir
);
464 if ((status
= do_samba_command("smbclient", address
, subcmd
,
465 authfile
, logfile
)) != 0)
467 snprintf(message
, sizeof(message
),
468 _cupsLangString(language
,
469 _("Unable to copy Windows 2000 printer "
470 "driver files (%d).")), status
);
472 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
475 _cupsLangPuts(logfile
, message
);
483 * See if we also have the CUPS driver files; if so, use them!
486 snprintf(file
, sizeof(file
), "%s/drivers/cupsps6.dll", cg
->cups_datadir
);
487 if (!access(file
, 0))
490 * Copy the CUPS driver files over...
493 snprintf(subcmd
, sizeof(subcmd
),
494 "put %s/drivers/cups6.ini W32X86/cups6.ini;"
495 "put %s/drivers/cupsps6.dll W32X86/cupsps6.dll;"
496 "put %s/drivers/cupsui6.dll W32X86/cupsui6.dll",
497 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
499 if ((status
= do_samba_command("smbclient", address
, subcmd
,
500 authfile
, logfile
)) != 0)
502 snprintf(message
, sizeof(message
),
503 _cupsLangString(language
,
504 _("Unable to copy CUPS printer driver "
505 "files (%d).")), status
);
507 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
510 _cupsLangPuts(logfile
, message
);
518 * Do the rpcclient command needed for the CUPS drivers...
521 snprintf(subcmd
, sizeof(subcmd
),
522 "adddriver \"Windows NT x86\" \"%s:"
523 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
524 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
525 "cups6.ini,cupsps6.dll,cupsui6.dll\"",
531 * Don't have the CUPS drivers, so just use the standard Windows
535 snprintf(subcmd
, sizeof(subcmd
),
536 "adddriver \"Windows NT x86\" \"%s:"
537 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
538 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
542 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
543 authfile
, logfile
)) != 0)
545 snprintf(message
, sizeof(message
),
546 _cupsLangString(language
,
547 _("Unable to install Windows 2000 printer "
548 "driver files (%d).")), status
);
550 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
553 _cupsLangPuts(logfile
, message
);
562 * See if we have the Win9x PS driver...
565 snprintf(file
, sizeof(file
), "%s/drivers/ADOBEPS4.DRV", cg
->cups_datadir
);
566 if (!access(file
, 0))
571 * Do the smbclient commands needed for the Adobe Win9x drivers...
574 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
576 snprintf(subcmd
, sizeof(subcmd
),
578 "put %s WIN40/%s.PPD;"
579 "put %s/drivers/ADFONTS.MFM WIN40/ADFONTS.MFM;"
580 "put %s/drivers/ADOBEPS4.DRV WIN40/ADOBEPS4.DRV;"
581 "put %s/drivers/ADOBEPS4.HLP WIN40/ADOBEPS4.HLP;"
582 "put %s/drivers/ICONLIB.DLL WIN40/ICONLIB.DLL;"
583 "put %s/drivers/PSMON.DLL WIN40/PSMON.DLL;",
584 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
585 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
587 if ((status
= do_samba_command("smbclient", address
, subcmd
,
588 authfile
, logfile
)) != 0)
590 snprintf(message
, sizeof(message
),
591 _cupsLangString(language
,
592 _("Unable to copy Windows 9x printer "
593 "driver files (%d).")), status
);
595 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
598 _cupsLangPuts(logfile
, message
);
606 * Do the rpcclient commands needed for the Adobe Win9x drivers...
609 snprintf(subcmd
, sizeof(subcmd
),
610 "adddriver \"Windows 4.0\" \"%s:ADOBEPS4.DRV:%s.PPD:NULL:"
611 "ADOBEPS4.HLP:PSMON.DLL:RAW:"
612 "ADOBEPS4.DRV,%s.PPD,ADOBEPS4.HLP,PSMON.DLL,ADFONTS.MFM,"
616 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
617 authfile
, logfile
)) != 0)
619 snprintf(message
, sizeof(message
),
620 _cupsLangString(language
,
621 _("Unable to install Windows 9x printer "
622 "driver files (%d).")), status
);
624 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
627 _cupsLangPuts(logfile
, message
);
636 * See if we have the 64-bit Windows PS driver...
646 snprintf(file
, sizeof(file
), "%s/drivers/x64/pscript5.dll", cg
->cups_datadir
);
647 if (!access(file
, 0))
652 * 64-bit Windows driver is installed; do the smbclient commands needed
653 * to copy the Win64 drivers over...
656 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
658 snprintf(subcmd
, sizeof(subcmd
),
661 "put %s/drivers/x64/ps5ui.dll x64/ps5ui.dll;"
662 "put %s/drivers/x64/pscript.hlp x64/pscript.hlp;"
663 "put %s/drivers/x64/pscript.ntf x64/pscript.ntf;"
664 "put %s/drivers/x64/pscript5.dll x64/pscript5.dll",
665 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
666 cg
->cups_datadir
, cg
->cups_datadir
);
668 if ((status
= do_samba_command("smbclient", address
, subcmd
,
669 authfile
, logfile
)) != 0)
671 snprintf(message
, sizeof(message
),
672 _cupsLangString(language
,
673 _("Unable to copy 64-bit Windows printer "
674 "driver files (%d).")), status
);
676 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
679 _cupsLangPuts(logfile
, message
);
687 * See if we also have the CUPS driver files; if so, use them!
690 snprintf(file
, sizeof(file
), "%s/drivers/x64/cupsps6.dll", cg
->cups_datadir
);
691 if (!access(file
, 0))
694 * Copy the CUPS driver files over...
697 snprintf(subcmd
, sizeof(subcmd
),
698 "put %s/drivers/x64/cups6.ini x64/cups6.ini;"
699 "put %s/drivers/x64/cupsps6.dll x64/cupsps6.dll;"
700 "put %s/drivers/x64/cupsui6.dll x64/cupsui6.dll",
701 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
703 if ((status
= do_samba_command("smbclient", address
, subcmd
,
704 authfile
, logfile
)) != 0)
706 snprintf(message
, sizeof(message
),
707 _cupsLangString(language
,
708 _("Unable to copy 64-bit CUPS printer driver "
709 "files (%d).")), status
);
711 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
714 _cupsLangPuts(logfile
, message
);
722 * Do the rpcclient command needed for the CUPS drivers...
725 snprintf(subcmd
, sizeof(subcmd
),
726 "adddriver \"Windows x64\" \"%s:"
727 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
728 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
729 "cups6.ini,cupsps6.dll,cupsui6.dll\"",
735 * Don't have the CUPS drivers, so just use the standard Windows
739 snprintf(subcmd
, sizeof(subcmd
),
740 "adddriver \"Windows x64\" \"%s:"
741 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
742 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
746 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
747 authfile
, logfile
)) != 0)
749 snprintf(message
, sizeof(message
),
750 _cupsLangString(language
,
751 _("Unable to install Windows 2000 printer "
752 "driver files (%d).")), status
);
754 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
757 _cupsLangPuts(logfile
, message
);
765 if (logfile
&& !(have_drivers
& 1))
769 _cupsLangString(language
,
770 _("No Windows printer drivers are installed.")),
774 _cupsLangString(language
,
775 _("Warning, no Windows 2000 printer drivers "
779 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, message
, 0);
780 _cupsLangPuts(logfile
, message
);
783 if (have_drivers
== 0)
785 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, message
, 0);
793 * Finally, associate the drivers we just added with the queue...
796 snprintf(subcmd
, sizeof(subcmd
), "setdriver %s %s", dest
, dest
);
798 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
799 authfile
, logfile
)) != 0)
801 snprintf(message
, sizeof(message
),
802 _cupsLangString(language
,
803 _("Unable to set Windows printer driver (%d).")),
806 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
809 _cupsLangPuts(logfile
, message
);
823 * 'cupsAdminGetServerSettings()' - Get settings from the server.
825 * The returned settings should be freed with cupsFreeOptions() when
826 * you are done with them.
828 * @since CUPS 1.3/macOS 10.5@
831 int /* O - 1 on success, 0 on failure */
832 cupsAdminGetServerSettings(
833 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
834 int *num_settings
, /* O - Number of settings */
835 cups_option_t
**settings
) /* O - Settings */
837 int i
; /* Looping var */
838 cups_file_t
*cupsd
; /* cupsd.conf file */
839 char cupsdconf
[1024]; /* cupsd.conf filename */
840 int remote
; /* Remote cupsd.conf file? */
841 http_status_t status
; /* Status of getting cupsd.conf */
842 char line
[1024], /* Line from cupsd.conf file */
843 *value
; /* Value on line */
844 cups_option_t
*setting
; /* Current setting */
845 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
849 * Range check input...
855 * See if we are connected to the same server...
861 * Compare the connection hostname, port, and encryption settings to
862 * the cached defaults; these were initialized the first time we
866 if (strcmp(cg
->http
->hostname
, cg
->server
) ||
867 cg
->ipp_port
!= httpAddrPort(cg
->http
->hostaddr
) ||
868 (cg
->http
->encryption
!= cg
->encryption
&&
869 cg
->http
->encryption
== HTTP_ENCRYPTION_NEVER
))
872 * Need to close the current connection because something has changed...
881 * (Re)connect as needed...
886 if ((cg
->http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
,
887 cupsEncryption(), 1, 0, NULL
)) == NULL
)
890 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
, NULL
, 0);
892 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
,
893 _("Unable to connect to host."), 1);
908 if (!http
|| !num_settings
|| !settings
)
910 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
925 * Get the cupsd.conf file...
928 if ((status
= get_cupsd_conf(http
, cg
, cg
->cupsd_update
, cupsdconf
,
929 sizeof(cupsdconf
), &remote
)) == HTTP_STATUS_OK
)
931 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
933 char message
[1024]; /* Message string */
936 snprintf(message
, sizeof(message
),
937 _cupsLangString(cupsLangDefault(), _("Open of %s failed: %s")),
938 cupsdconf
, strerror(errno
));
939 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
948 * Read the file, keeping track of what settings are enabled...
951 int remote_access
= 0, /* Remote access allowed? */
952 remote_admin
= 0, /* Remote administration allowed? */
953 remote_any
= 0, /* Remote access from anywhere allowed? */
954 browsing
= 1, /* Browsing enabled? */
955 cancel_policy
= 1, /* Cancel-job policy set? */
956 debug_logging
= 0; /* LogLevel debug set? */
957 int linenum
= 0, /* Line number in file */
958 in_location
= 0, /* In a location section? */
959 in_policy
= 0, /* In a policy section? */
960 in_cancel_job
= 0, /* In a cancel-job section? */
961 in_admin_location
= 0; /* In the /admin location? */
964 invalidate_cupsd_cache(cg
);
966 cg
->cupsd_update
= time(NULL
);
967 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
969 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
971 if (!value
&& strncmp(line
, "</", 2))
972 value
= line
+ strlen(line
);
974 if ((!_cups_strcasecmp(line
, "Port") || !_cups_strcasecmp(line
, "Listen")) && value
)
976 char *port
; /* Pointer to port number, if any */
979 if ((port
= strrchr(value
, ':')) != NULL
)
981 else if (isdigit(*value
& 255))
984 * Listen on a port number implies remote access...
991 if (_cups_strcasecmp(value
, "localhost") && strcmp(value
, "127.0.0.1")
994 #endif /* AF_LOCAL */
996 && strcmp(value
, "[::1]")
997 #endif /* AF_INET6 */
1001 else if (!_cups_strcasecmp(line
, "Browsing"))
1003 browsing
= !_cups_strcasecmp(value
, "yes") ||
1004 !_cups_strcasecmp(value
, "on") ||
1005 !_cups_strcasecmp(value
, "true");
1007 else if (!_cups_strcasecmp(line
, "LogLevel"))
1009 debug_logging
= !_cups_strncasecmp(value
, "debug", 5);
1011 else if (!_cups_strcasecmp(line
, "<Policy") &&
1012 !_cups_strcasecmp(value
, "default"))
1016 else if (!_cups_strcasecmp(line
, "</Policy>"))
1020 else if (!_cups_strcasecmp(line
, "<Limit") && in_policy
&& value
)
1023 * See if the policy limit is for the Cancel-Job operation...
1026 char *valptr
; /* Pointer into value */
1031 for (valptr
= value
; *valptr
&& !_cups_isspace(*valptr
); valptr
++);
1036 if (!_cups_strcasecmp(value
, "cancel-job") ||
1037 !_cups_strcasecmp(value
, "all"))
1043 for (value
= valptr
; _cups_isspace(*value
); value
++);
1046 else if (!_cups_strcasecmp(line
, "</Limit>"))
1050 else if (!_cups_strcasecmp(line
, "Require") && in_cancel_job
)
1054 else if (!_cups_strcasecmp(line
, "<Location") && value
)
1056 in_admin_location
= !_cups_strcasecmp(value
, "/admin");
1059 else if (!_cups_strcasecmp(line
, "</Location>"))
1061 in_admin_location
= 0;
1064 else if (!_cups_strcasecmp(line
, "Allow") && value
&&
1065 _cups_strcasecmp(value
, "localhost") &&
1066 _cups_strcasecmp(value
, "127.0.0.1")
1069 #endif /* AF_LOCAL */
1071 && strcmp(value
, "::1")
1072 #endif /* AF_INET6 */
1075 if (in_admin_location
)
1077 else if (!_cups_strcasecmp(value
, "all"))
1080 else if (line
[0] != '<' && !in_location
&& !in_policy
&&
1081 _cups_strcasecmp(line
, "Allow") &&
1082 _cups_strcasecmp(line
, "AuthType") &&
1083 _cups_strcasecmp(line
, "Deny") &&
1084 _cups_strcasecmp(line
, "Order") &&
1085 _cups_strcasecmp(line
, "Require") &&
1086 _cups_strcasecmp(line
, "Satisfy"))
1087 cg
->cupsd_num_settings
= cupsAddOption(line
, value
,
1088 cg
->cupsd_num_settings
,
1089 &(cg
->cupsd_settings
));
1092 cupsFileClose(cupsd
);
1094 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1095 debug_logging
? "1" : "0",
1096 cg
->cupsd_num_settings
,
1097 &(cg
->cupsd_settings
));
1099 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1100 (remote_access
&& remote_admin
) ?
1102 cg
->cupsd_num_settings
,
1103 &(cg
->cupsd_settings
));
1105 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
1106 remote_any
? "1" : "0",
1107 cg
->cupsd_num_settings
,
1108 &(cg
->cupsd_settings
));
1110 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1111 (remote_access
&& browsing
) ? "1" :
1113 cg
->cupsd_num_settings
,
1114 &(cg
->cupsd_settings
));
1116 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1117 cancel_policy
? "1" : "0",
1118 cg
->cupsd_num_settings
,
1119 &(cg
->cupsd_settings
));
1121 else if (status
!= HTTP_STATUS_NOT_MODIFIED
)
1122 invalidate_cupsd_cache(cg
);
1125 * Remove any temporary files and copy the settings array...
1131 for (i
= cg
->cupsd_num_settings
, setting
= cg
->cupsd_settings
;
1134 *num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1135 *num_settings
, settings
);
1137 return (cg
->cupsd_num_settings
> 0);
1142 * 'cupsAdminSetServerSettings()' - Set settings on the server.
1144 * @since CUPS 1.3/macOS 10.5@
1147 int /* O - 1 on success, 0 on failure */
1148 cupsAdminSetServerSettings(
1149 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1150 int num_settings
, /* I - Number of settings */
1151 cups_option_t
*settings
) /* I - Settings */
1153 int i
; /* Looping var */
1154 http_status_t status
; /* GET/PUT status */
1155 const char *server_port_env
; /* SERVER_PORT env var */
1156 int server_port
; /* IPP port for server */
1157 cups_file_t
*cupsd
; /* cupsd.conf file */
1158 char cupsdconf
[1024]; /* cupsd.conf filename */
1159 int remote
; /* Remote cupsd.conf file? */
1160 char tempfile
[1024]; /* Temporary new cupsd.conf */
1161 cups_file_t
*temp
; /* Temporary file */
1162 char line
[1024], /* Line from cupsd.conf file */
1163 *value
; /* Value on line */
1164 int linenum
, /* Line number in file */
1165 in_location
, /* In a location section? */
1166 in_policy
, /* In a policy section? */
1167 in_default_policy
, /* In the default policy section? */
1168 in_cancel_job
, /* In a cancel-job section? */
1169 in_admin_location
, /* In the /admin location? */
1170 in_conf_location
, /* In the /admin/conf location? */
1171 in_log_location
, /* In the /admin/log location? */
1172 in_root_location
; /* In the / location? */
1173 const char *val
; /* Setting value */
1174 int share_printers
, /* Share local printers */
1175 remote_admin
, /* Remote administration allowed? */
1176 remote_any
, /* Remote access from anywhere? */
1177 user_cancel_any
, /* Cancel-job policy set? */
1178 debug_logging
; /* LogLevel debug set? */
1179 int wrote_port_listen
, /* Wrote the port/listen lines? */
1180 wrote_browsing
, /* Wrote the browsing lines? */
1181 wrote_policy
, /* Wrote the policy? */
1182 wrote_loglevel
, /* Wrote the LogLevel line? */
1183 wrote_admin_location
, /* Wrote the /admin location? */
1184 wrote_conf_location
, /* Wrote the /admin/conf location? */
1185 wrote_log_location
, /* Wrote the /admin/log location? */
1186 wrote_root_location
; /* Wrote the / location? */
1187 int indent
; /* Indentation */
1188 int cupsd_num_settings
; /* New number of settings */
1189 int old_share_printers
, /* Share local printers */
1190 old_remote_admin
, /* Remote administration allowed? */
1191 old_remote_any
, /* Remote access from anywhere? */
1192 old_user_cancel_any
, /* Cancel-job policy set? */
1193 old_debug_logging
; /* LogLevel debug set? */
1194 cups_option_t
*cupsd_settings
, /* New settings */
1195 *setting
; /* Current setting */
1196 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1200 * Range check input...
1204 http
= _cupsConnect();
1206 if (!http
|| !num_settings
|| !settings
)
1208 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1214 * Get the cupsd.conf file...
1217 if (get_cupsd_conf(http
, cg
, 0, cupsdconf
, sizeof(cupsdconf
),
1218 &remote
) == HTTP_STATUS_OK
)
1220 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
1222 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
1230 * Get current settings...
1233 if (!cupsAdminGetServerSettings(http
, &cupsd_num_settings
,
1237 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, cupsd_num_settings
,
1238 cupsd_settings
)) != NULL
)
1239 old_debug_logging
= atoi(val
);
1241 old_debug_logging
= 0;
1243 DEBUG_printf(("1cupsAdminSetServerSettings: old debug_logging=%d",
1244 old_debug_logging
));
1246 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, cupsd_num_settings
,
1247 cupsd_settings
)) != NULL
)
1248 old_remote_admin
= atoi(val
);
1250 old_remote_admin
= 0;
1252 DEBUG_printf(("1cupsAdminSetServerSettings: old remote_admin=%d",
1255 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, cupsd_num_settings
,
1256 cupsd_settings
)) != NULL
)
1257 old_remote_any
= atoi(val
);
1261 DEBUG_printf(("1cupsAdminSetServerSettings: old remote_any=%d",
1264 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, cupsd_num_settings
,
1265 cupsd_settings
)) != NULL
)
1266 old_share_printers
= atoi(val
);
1268 old_share_printers
= 0;
1270 DEBUG_printf(("1cupsAdminSetServerSettings: old share_printers=%d",
1271 old_share_printers
));
1273 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, cupsd_num_settings
,
1274 cupsd_settings
)) != NULL
)
1275 old_user_cancel_any
= atoi(val
);
1277 old_user_cancel_any
= 0;
1279 DEBUG_printf(("1cupsAdminSetServerSettings: old user_cancel_any=%d",
1280 old_user_cancel_any
));
1282 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
1285 * Get basic settings...
1288 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, num_settings
,
1291 debug_logging
= atoi(val
);
1293 if (debug_logging
== old_debug_logging
)
1296 * No change to this setting...
1305 DEBUG_printf(("1cupsAdminSetServerSettings: debug_logging=%d",
1308 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, num_settings
, settings
)) != NULL
)
1310 remote_any
= atoi(val
);
1312 if (remote_any
== old_remote_any
)
1315 * No change to this setting...
1324 DEBUG_printf(("1cupsAdminSetServerSettings: remote_any=%d", remote_any
));
1326 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, num_settings
,
1329 remote_admin
= atoi(val
);
1331 if (remote_admin
== old_remote_admin
)
1334 * No change to this setting...
1343 DEBUG_printf(("1cupsAdminSetServerSettings: remote_admin=%d",
1346 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, num_settings
,
1349 share_printers
= atoi(val
);
1351 if (share_printers
== old_share_printers
)
1354 * No change to this setting...
1357 share_printers
= -1;
1361 share_printers
= -1;
1363 DEBUG_printf(("1cupsAdminSetServerSettings: share_printers=%d",
1366 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, num_settings
,
1369 user_cancel_any
= atoi(val
);
1371 if (user_cancel_any
== old_user_cancel_any
)
1374 * No change to this setting...
1377 user_cancel_any
= -1;
1381 user_cancel_any
= -1;
1383 DEBUG_printf(("1cupsAdminSetServerSettings: user_cancel_any=%d",
1387 * Create a temporary file for the new cupsd.conf file...
1390 if ((temp
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1392 cupsFileClose(cupsd
);
1397 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
1402 * Copy the old file to the new, making changes along the way...
1405 cupsd_num_settings
= 0;
1406 in_admin_location
= 0;
1408 in_conf_location
= 0;
1409 in_default_policy
= 0;
1411 in_log_location
= 0;
1413 in_root_location
= 0;
1415 wrote_admin_location
= 0;
1417 wrote_conf_location
= 0;
1418 wrote_log_location
= 0;
1421 wrote_port_listen
= 0;
1422 wrote_root_location
= 0;
1425 if ((server_port_env
= getenv("SERVER_PORT")) != NULL
)
1427 if ((server_port
= atoi(server_port_env
)) <= 0)
1428 server_port
= ippPort();
1431 server_port
= ippPort();
1433 if (server_port
<= 0)
1434 server_port
= IPP_PORT
;
1436 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
1438 if ((!_cups_strcasecmp(line
, "Port") || !_cups_strcasecmp(line
, "Listen")) &&
1439 (remote_admin
>= 0 || remote_any
>= 0 || share_printers
>= 0))
1441 if (!wrote_port_listen
)
1443 wrote_port_listen
= 1;
1445 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1447 cupsFilePuts(temp
, "# Allow remote access\n");
1448 cupsFilePrintf(temp
, "Port %d\n", server_port
);
1452 cupsFilePuts(temp
, "# Only listen for connections from the local "
1454 cupsFilePrintf(temp
, "Listen localhost:%d\n", server_port
);
1457 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1458 if ((!value
|| strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)) &&
1459 !access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1460 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1461 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1463 else if (value
&& value
[0] == '/'
1464 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1465 && strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)
1466 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1468 cupsFilePrintf(temp
, "Listen %s\n", value
);
1470 else if ((!_cups_strcasecmp(line
, "Browsing") ||
1471 !_cups_strcasecmp(line
, "BrowseLocalProtocols")) &&
1472 share_printers
>= 0)
1474 if (!wrote_browsing
)
1476 int new_share_printers
= (share_printers
> 0 ||
1477 (share_printers
== -1 &&
1478 old_share_printers
> 0));
1482 if (new_share_printers
)
1484 const char *localp
= cupsGetOption("BrowseLocalProtocols",
1485 num_settings
, settings
);
1487 if (!localp
|| !localp
[0])
1488 localp
= cupsGetOption("BrowseLocalProtocols", cupsd_num_settings
,
1491 cupsFilePuts(temp
, "# Share local printers on the local network.\n");
1492 cupsFilePuts(temp
, "Browsing On\n");
1495 localp
= CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS
;
1497 cupsFilePrintf(temp
, "BrowseLocalProtocols %s\n", localp
);
1499 cupsd_num_settings
= cupsAddOption("BrowseLocalProtocols", localp
,
1505 cupsFilePuts(temp
, "# Disable printer sharing.\n");
1506 cupsFilePuts(temp
, "Browsing Off\n");
1510 else if (!_cups_strcasecmp(line
, "LogLevel") && debug_logging
>= 0)
1517 "# Show troubleshooting information in error_log.\n");
1518 cupsFilePuts(temp
, "LogLevel debug\n");
1522 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1523 cupsFilePuts(temp
, "LogLevel " CUPS_DEFAULT_LOG_LEVEL
"\n");
1526 else if (!_cups_strcasecmp(line
, "<Policy"))
1528 in_default_policy
= !_cups_strcasecmp(value
, "default");
1531 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1534 else if (!_cups_strcasecmp(line
, "</Policy>"))
1537 if (!wrote_policy
&& in_default_policy
)
1541 if (!user_cancel_any
)
1542 cupsFilePuts(temp
, " # Only the owner or an administrator can "
1544 " <Limit Cancel-Job>\n"
1545 " Order deny,allow\n"
1546 " Require user @OWNER "
1547 CUPS_DEFAULT_PRINTOPERATOR_AUTH
"\n"
1552 in_default_policy
= 0;
1554 cupsFilePuts(temp
, "</Policy>\n");
1556 else if (!_cups_strcasecmp(line
, "<Location"))
1560 if (!strcmp(value
, "/admin"))
1561 in_admin_location
= 1;
1562 else if (!strcmp(value
, "/admin/conf"))
1563 in_conf_location
= 1;
1564 else if (!strcmp(value
, "/admin/log"))
1565 in_log_location
= 1;
1566 else if (!strcmp(value
, "/"))
1567 in_root_location
= 1;
1569 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1571 else if (!_cups_strcasecmp(line
, "</Location>"))
1575 if (in_admin_location
&& remote_admin
>= 0)
1577 wrote_admin_location
= 1;
1580 cupsFilePuts(temp
, " # Allow remote administration...\n");
1581 else if (remote_admin
== 0)
1582 cupsFilePuts(temp
, " # Restrict access to the admin pages...\n");
1584 cupsFilePuts(temp
, " Order allow,deny\n");
1587 cupsFilePrintf(temp
, " Allow %s\n",
1588 remote_any
> 0 ? "all" : "@LOCAL");
1590 else if (in_conf_location
&& remote_admin
>= 0)
1592 wrote_conf_location
= 1;
1595 cupsFilePuts(temp
, " # Allow remote access to the configuration "
1598 cupsFilePuts(temp
, " # Restrict access to the configuration "
1601 cupsFilePuts(temp
, " Order allow,deny\n");
1604 cupsFilePrintf(temp
, " Allow %s\n",
1605 remote_any
> 0 ? "all" : "@LOCAL");
1607 else if (in_log_location
&& remote_admin
>= 0)
1609 wrote_log_location
= 1;
1612 cupsFilePuts(temp
, " # Allow remote access to the log "
1615 cupsFilePuts(temp
, " # Restrict access to the log "
1618 cupsFilePuts(temp
, " Order allow,deny\n");
1621 cupsFilePrintf(temp
, " Allow %s\n",
1622 remote_any
> 0 ? "all" : "@LOCAL");
1624 else if (in_root_location
&&
1625 (remote_admin
>= 0 || remote_any
>= 0 || share_printers
>= 0))
1627 wrote_root_location
= 1;
1629 if (remote_admin
> 0 && share_printers
> 0)
1630 cupsFilePuts(temp
, " # Allow shared printing and remote "
1631 "administration...\n");
1632 else if (remote_admin
> 0)
1633 cupsFilePuts(temp
, " # Allow remote administration...\n");
1634 else if (share_printers
> 0)
1635 cupsFilePuts(temp
, " # Allow shared printing...\n");
1636 else if (remote_any
> 0)
1637 cupsFilePuts(temp
, " # Allow remote access...\n");
1639 cupsFilePuts(temp
, " # Restrict access to the server...\n");
1641 cupsFilePuts(temp
, " Order allow,deny\n");
1643 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1644 cupsFilePrintf(temp
, " Allow %s\n",
1645 remote_any
> 0 ? "all" : "@LOCAL");
1648 in_admin_location
= 0;
1649 in_conf_location
= 0;
1650 in_log_location
= 0;
1651 in_root_location
= 0;
1653 cupsFilePuts(temp
, "</Location>\n");
1655 else if (!_cups_strcasecmp(line
, "<Limit"))
1657 if (in_default_policy
)
1660 * See if the policy limit is for the Cancel-Job operation...
1663 char *valptr
; /* Pointer into value */
1666 if (!_cups_strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1669 * Don't write anything for this limit section...
1676 cupsFilePrintf(temp
, "%*s%s", indent
, "", line
);
1680 for (valptr
= value
; *valptr
&& !_cups_isspace(*valptr
); valptr
++);
1685 if (!_cups_strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1688 * Write everything except for this definition...
1694 cupsFilePrintf(temp
, " %s", value
);
1696 for (value
= valptr
; _cups_isspace(*value
); value
++);
1699 cupsFilePuts(temp
, ">\n");
1703 cupsFilePrintf(temp
, "%*s%s %s>\n", indent
, "", line
, value
);
1707 else if (!_cups_strcasecmp(line
, "</Limit>") && in_cancel_job
)
1711 if (in_cancel_job
== 1)
1712 cupsFilePuts(temp
, " </Limit>\n");
1716 if (!user_cancel_any
)
1717 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1719 " <Limit Cancel-Job>\n"
1720 " Order deny,allow\n"
1721 " Require user @OWNER "
1722 CUPS_DEFAULT_PRINTOPERATOR_AUTH
"\n"
1727 else if ((((in_admin_location
|| in_conf_location
|| in_root_location
) &&
1728 (remote_admin
>= 0 || remote_any
>= 0)) ||
1729 (in_root_location
&& share_printers
>= 0)) &&
1730 (!_cups_strcasecmp(line
, "Allow") || !_cups_strcasecmp(line
, "Deny") ||
1731 !_cups_strcasecmp(line
, "Order")))
1733 else if (in_cancel_job
== 2)
1735 else if (line
[0] == '<')
1739 cupsFilePrintf(temp
, "%*s%s %s>\n", indent
, "", line
, value
);
1747 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1750 else if (!in_policy
&& !in_location
&&
1751 (val
= cupsGetOption(line
, num_settings
, settings
)) != NULL
)
1754 * Replace this directive's value with the new one...
1757 cupsd_num_settings
= cupsAddOption(line
, val
, cupsd_num_settings
,
1761 * Write the new value in its place, without indentation since we
1762 * only support setting root directives, not in sections...
1765 cupsFilePrintf(temp
, "%s %s\n", line
, val
);
1769 if (!in_policy
&& !in_location
)
1772 * Record the non-policy, non-location directives that we find
1773 * in the server settings, since we cache this info and record it
1774 * in cupsAdminGetServerSettings()...
1777 cupsd_num_settings
= cupsAddOption(line
, value
, cupsd_num_settings
,
1781 cupsFilePrintf(temp
, "%*s%s %s\n", indent
, "", line
, value
);
1784 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1788 * Write any missing info...
1791 if (!wrote_browsing
&& share_printers
>= 0)
1793 if (share_printers
> 0)
1795 cupsFilePuts(temp
, "# Share local printers on the local network.\n");
1796 cupsFilePuts(temp
, "Browsing On\n");
1800 cupsFilePuts(temp
, "# Disable printer sharing and shared printers.\n");
1801 cupsFilePuts(temp
, "Browsing Off\n");
1805 if (!wrote_loglevel
&& debug_logging
>= 0)
1809 cupsFilePuts(temp
, "# Show troubleshooting information in error_log.\n");
1810 cupsFilePuts(temp
, "LogLevel debug\n");
1814 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1815 cupsFilePuts(temp
, "LogLevel " CUPS_DEFAULT_LOG_LEVEL
"\n");
1819 if (!wrote_port_listen
&&
1820 (remote_admin
>= 0 || remote_any
>= 0 || share_printers
>= 0))
1822 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1824 cupsFilePuts(temp
, "# Allow remote access\n");
1825 cupsFilePrintf(temp
, "Port %d\n", ippPort());
1830 "# Only listen for connections from the local machine.\n");
1831 cupsFilePrintf(temp
, "Listen localhost:%d\n", ippPort());
1834 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1835 if (!access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1836 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1837 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1840 if (!wrote_root_location
&&
1841 (remote_admin
>= 0 || remote_any
>= 0 || share_printers
>= 0))
1843 if (remote_admin
> 0 && share_printers
> 0)
1845 "# Allow shared printing and remote administration...\n");
1846 else if (remote_admin
> 0)
1847 cupsFilePuts(temp
, "# Allow remote administration...\n");
1848 else if (share_printers
> 0)
1849 cupsFilePuts(temp
, "# Allow shared printing...\n");
1850 else if (remote_any
> 0)
1851 cupsFilePuts(temp
, "# Allow remote access...\n");
1853 cupsFilePuts(temp
, "# Restrict access to the server...\n");
1855 cupsFilePuts(temp
, "<Location />\n"
1856 " Order allow,deny\n");
1858 if (remote_admin
> 0 || remote_any
> 0 || share_printers
> 0)
1859 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1861 cupsFilePuts(temp
, "</Location>\n");
1864 if (!wrote_admin_location
&& remote_admin
>= 0)
1867 cupsFilePuts(temp
, "# Allow remote administration...\n");
1869 cupsFilePuts(temp
, "# Restrict access to the admin pages...\n");
1871 cupsFilePuts(temp
, "<Location /admin>\n"
1872 " Order allow,deny\n");
1875 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1877 cupsFilePuts(temp
, "</Location>\n");
1880 if (!wrote_conf_location
&& remote_admin
>= 0)
1884 "# Allow remote access to the configuration files...\n");
1886 cupsFilePuts(temp
, "# Restrict access to the configuration files...\n");
1888 cupsFilePuts(temp
, "<Location /admin/conf>\n"
1889 " AuthType Default\n"
1890 " Require user @SYSTEM\n"
1891 " Order allow,deny\n");
1894 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1896 cupsFilePuts(temp
, "</Location>\n");
1899 if (!wrote_log_location
&& remote_admin
>= 0)
1903 "# Allow remote access to the log files...\n");
1905 cupsFilePuts(temp
, "# Restrict access to the log files...\n");
1907 cupsFilePuts(temp
, "<Location /admin/log>\n"
1908 " AuthType Default\n"
1909 " Require user @SYSTEM\n"
1910 " Order allow,deny\n");
1913 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1915 cupsFilePuts(temp
, "</Location>\n");
1918 if (!wrote_policy
&& user_cancel_any
>= 0)
1920 cupsFilePuts(temp
, "<Policy default>\n"
1921 " # Job-related operations must be done by the owner "
1922 "or an administrator...\n"
1923 " <Limit Send-Document Send-URI Hold-Job Release-Job "
1924 "Restart-Job Purge-Jobs Set-Job-Attributes "
1925 "Create-Job-Subscription Renew-Subscription "
1926 "Cancel-Subscription Get-Notifications Reprocess-Job "
1927 "Cancel-Current-Job Suspend-Current-Job Resume-Job "
1929 " Require user @OWNER @SYSTEM\n"
1930 " Order deny,allow\n"
1932 " # All administration operations require an "
1933 "administrator to authenticate...\n"
1934 " <Limit Pause-Printer Resume-Printer "
1935 "Set-Printer-Attributes Enable-Printer "
1936 "Disable-Printer Pause-Printer-After-Current-Job "
1937 "Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer "
1938 "Activate-Printer Restart-Printer Shutdown-Printer "
1939 "Startup-Printer Promote-Job Schedule-Job-After "
1940 "CUPS-Add-Printer CUPS-Delete-Printer "
1941 "CUPS-Add-Class CUPS-Delete-Class "
1942 "CUPS-Accept-Jobs CUPS-Reject-Jobs "
1943 "CUPS-Set-Default CUPS-Add-Device CUPS-Delete-Device>\n"
1944 " AuthType Default\n"
1945 " Require user @SYSTEM\n"
1946 " Order deny,allow\n"
1949 if (!user_cancel_any
)
1950 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1952 " <Limit Cancel-Job>\n"
1953 " Order deny,allow\n"
1954 " Require user @OWNER "
1955 CUPS_DEFAULT_PRINTOPERATOR_AUTH
"\n"
1958 cupsFilePuts(temp
, " <Limit All>\n"
1959 " Order deny,allow\n"
1964 for (i
= num_settings
, setting
= settings
; i
> 0; i
--, setting
++)
1965 if (setting
->name
[0] != '_' &&
1966 _cups_strcasecmp(setting
->name
, "Listen") &&
1967 _cups_strcasecmp(setting
->name
, "Port") &&
1968 !cupsGetOption(setting
->name
, cupsd_num_settings
, cupsd_settings
))
1971 * Add this directive to the list of directives we have written...
1974 cupsd_num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1975 cupsd_num_settings
, &cupsd_settings
);
1978 * Write the new value, without indentation since we only support
1979 * setting root directives, not in sections...
1982 cupsFilePrintf(temp
, "%s %s\n", setting
->name
, setting
->value
);
1985 cupsFileClose(cupsd
);
1986 cupsFileClose(temp
);
1989 * Upload the configuration file to the server...
1992 status
= cupsPutFile(http
, "/admin/conf/cupsd.conf", tempfile
);
1994 if (status
== HTTP_STATUS_CREATED
)
1997 * Updated OK, add the basic settings...
2000 if (debug_logging
>= 0)
2001 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
2002 debug_logging
? "1" : "0",
2003 cupsd_num_settings
, &cupsd_settings
);
2005 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
2006 old_debug_logging
? "1" : "0",
2007 cupsd_num_settings
, &cupsd_settings
);
2009 if (remote_admin
>= 0)
2010 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
2011 remote_admin
? "1" : "0",
2012 cupsd_num_settings
, &cupsd_settings
);
2014 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
2015 old_remote_admin
? "1" : "0",
2016 cupsd_num_settings
, &cupsd_settings
);
2018 if (remote_any
>= 0)
2019 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
2020 remote_any
? "1" : "0",
2021 cupsd_num_settings
, &cupsd_settings
);
2023 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
2024 old_remote_any
? "1" : "0",
2025 cupsd_num_settings
, &cupsd_settings
);
2027 if (share_printers
>= 0)
2028 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
2029 share_printers
? "1" : "0",
2030 cupsd_num_settings
, &cupsd_settings
);
2032 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
2033 old_share_printers
? "1" : "0",
2034 cupsd_num_settings
, &cupsd_settings
);
2036 if (user_cancel_any
>= 0)
2037 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
2038 user_cancel_any
? "1" : "0",
2039 cupsd_num_settings
, &cupsd_settings
);
2041 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
2042 old_user_cancel_any
? "1" : "0",
2043 cupsd_num_settings
, &cupsd_settings
);
2046 * Save the new values...
2049 invalidate_cupsd_cache(cg
);
2051 cg
->cupsd_num_settings
= cupsd_num_settings
;
2052 cg
->cupsd_settings
= cupsd_settings
;
2053 cg
->cupsd_update
= time(NULL
);
2055 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
2058 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
2061 * Remote our temp files and return...
2069 return (status
== HTTP_STATUS_CREATED
);
2074 * 'do_samba_command()' - Do a SAMBA command.
2077 static int /* O - Status of command */
2078 do_samba_command(const char *command
, /* I - Command to run */
2079 const char *address
, /* I - Address for command */
2080 const char *subcmd
, /* I - Sub-command */
2081 const char *authfile
, /* I - Samba authentication file */
2082 FILE *logfile
) /* I - Optional log file */
2085 return (1); /* Always fail on Windows... */
2088 int status
; /* Status of command */
2089 int pid
; /* Process ID of child */
2093 _cupsLangPrintf(logfile
,
2094 _("Running command: %s %s -N -A %s -c \'%s\'"),
2095 command
, address
, authfile
, subcmd
);
2097 if ((pid
= fork()) == 0)
2100 * Child goes here, redirect stdin/out/err and execute the command...
2103 int fd
= open("/dev/null", O_RDONLY
);
2112 dup2(fileno(logfile
), 1);
2113 else if ((fd
= open("/dev/null", O_WRONLY
)) > 1)
2121 execlp(command
, command
, address
, "-N", "-A", authfile
, "-c", subcmd
,
2130 _cupsLangPrintf(logfile
, _("Unable to run \"%s\": %s"),
2131 command
, strerror(errno
));
2136 * Wait for the process to complete...
2139 while (wait(&status
) != pid
);
2143 _cupsLangPuts(logfile
, "");
2145 DEBUG_printf(("9do_samba_command: status=%d", status
));
2147 if (WIFEXITED(status
))
2148 return (WEXITSTATUS(status
));
2150 return (-WTERMSIG(status
));
2156 * 'get_cupsd_conf()' - Get the current cupsd.conf file.
2159 static http_status_t
/* O - Status of request */
2161 http_t
*http
, /* I - Connection to server */
2162 _cups_globals_t
*cg
, /* I - Global data */
2163 time_t last_update
, /* I - Last update time for file */
2164 char *name
, /* I - Filename buffer */
2165 size_t namesize
, /* I - Size of filename buffer */
2166 int *remote
) /* O - Remote file? */
2168 int fd
; /* Temporary file descriptor */
2170 struct stat info
; /* cupsd.conf file information */
2172 http_status_t status
; /* Status of getting cupsd.conf */
2173 char host
[HTTP_MAX_HOST
]; /* Hostname for connection */
2177 * See if we already have the data we need...
2180 httpGetHostname(http
, host
, sizeof(host
));
2182 if (_cups_strcasecmp(cg
->cupsd_hostname
, host
))
2183 invalidate_cupsd_cache(cg
);
2185 snprintf(name
, namesize
, "%s/cupsd.conf", cg
->cups_serverroot
);
2189 if (!_cups_strcasecmp(host
, "localhost") && !access(name
, R_OK
))
2192 * Read the local file rather than using HTTP...
2195 if (stat(name
, &info
))
2197 char message
[1024]; /* Message string */
2200 snprintf(message
, sizeof(message
),
2201 _cupsLangString(cupsLangDefault(), _("stat of %s failed: %s")),
2202 name
, strerror(errno
));
2203 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, message
, 0);
2207 return (HTTP_STATUS_SERVER_ERROR
);
2209 else if (last_update
&& info
.st_mtime
<= last_update
)
2210 status
= HTTP_STATUS_NOT_MODIFIED
;
2212 status
= HTTP_STATUS_OK
;
2218 * Read cupsd.conf via a HTTP GET request...
2221 if ((fd
= cupsTempFd(name
, (int)namesize
)) < 0)
2225 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
2227 invalidate_cupsd_cache(cg
);
2229 return (HTTP_STATUS_SERVER_ERROR
);
2234 httpClearFields(http
);
2237 httpSetField(http
, HTTP_FIELD_IF_MODIFIED_SINCE
,
2238 httpGetDateString(last_update
));
2240 status
= cupsGetFd(http
, "/admin/conf/cupsd.conf", fd
);
2244 if (status
!= HTTP_STATUS_OK
)
2256 * 'invalidate_cupsd_cache()' - Invalidate the cached cupsd.conf settings.
2260 invalidate_cupsd_cache(
2261 _cups_globals_t
*cg
) /* I - Global data */
2263 cupsFreeOptions(cg
->cupsd_num_settings
, cg
->cupsd_settings
);
2265 cg
->cupsd_hostname
[0] = '\0';
2266 cg
->cupsd_update
= 0;
2267 cg
->cupsd_num_settings
= 0;
2268 cg
->cupsd_settings
= NULL
;
2273 * 'write_option()' - Write a CUPS option to a PPD file.
2277 write_option(cups_file_t
*dstfp
, /* I - PPD file */
2278 int order
, /* I - Order dependency */
2279 const char *name
, /* I - Option name */
2280 const char *text
, /* I - Option text */
2281 const char *attrname
, /* I - Attribute name */
2282 ipp_attribute_t
*suppattr
, /* I - IPP -supported attribute */
2283 ipp_attribute_t
*defattr
, /* I - IPP -default attribute */
2284 int defval
, /* I - Default value number */
2285 int valcount
) /* I - Number of values */
2287 int i
; /* Looping var */
2290 cupsFilePrintf(dstfp
, "*JCLOpenUI *%s/%s: PickOne\n"
2291 "*OrderDependency: %d JCLSetup *%s\n",
2292 name
, text
, order
, name
);
2294 if (defattr
->value_tag
== IPP_TAG_INTEGER
)
2297 * Do numeric options with a range or list...
2300 cupsFilePrintf(dstfp
, "*Default%s: %d\n", name
,
2301 defattr
->values
[defval
].integer
);
2303 if (suppattr
->value_tag
== IPP_TAG_RANGE
)
2306 * List each number in the range...
2309 for (i
= suppattr
->values
[0].range
.lower
;
2310 i
<= suppattr
->values
[0].range
.upper
;
2313 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, i
);
2316 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n",
2318 else if (defval
== 0)
2319 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
, i
);
2320 else if (defval
< (valcount
- 1))
2321 cupsFilePrintf(dstfp
, ",%d\"\n", i
);
2323 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", i
);
2329 * List explicit numbers...
2332 for (i
= 0; i
< suppattr
->num_values
; i
++)
2334 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, suppattr
->values
[i
].integer
);
2337 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n", attrname
,
2338 suppattr
->values
[i
].integer
);
2339 else if (defval
== 0)
2340 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
,
2341 suppattr
->values
[i
].integer
);
2342 else if (defval
< (valcount
- 1))
2343 cupsFilePrintf(dstfp
, ",%d\"\n", suppattr
->values
[i
].integer
);
2345 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", suppattr
->values
[i
].integer
);
2352 * Do text options with a list...
2355 cupsFilePrintf(dstfp
, "*Default%s: %s\n", name
,
2356 defattr
->values
[defval
].string
.text
);
2358 for (i
= 0; i
< suppattr
->num_values
; i
++)
2360 cupsFilePrintf(dstfp
, "*%s %s: \"", name
,
2361 suppattr
->values
[i
].string
.text
);
2364 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\n\"\n*End\n", attrname
,
2365 suppattr
->values
[i
].string
.text
);
2366 else if (defval
== 0)
2367 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\"\n", attrname
,
2368 suppattr
->values
[i
].string
.text
);
2369 else if (defval
< (valcount
- 1))
2370 cupsFilePrintf(dstfp
, ",%s\"\n", suppattr
->values
[i
].string
.text
);
2372 cupsFilePrintf(dstfp
, ",%s\n\"\n*End\n",
2373 suppattr
->values
[i
].string
.text
);
2377 cupsFilePrintf(dstfp
, "*JCLCloseUI: *%s\n\n", name
);