2 * "$Id: adminutil.c 7576 2008-05-15 21:05:14Z mike $"
4 * Administration utility API definitions for the Common UNIX Printing
7 * Copyright 2007-2008 by Apple Inc.
8 * Copyright 2001-2007 by Easy Software Products.
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * file is missing or damaged, see the license at "http://www.cups.org/".
16 * This file is subject to the Apple OS-Developed Software exception.
20 * cupsAdminCreateWindowsPPD() - Create the Windows PPD file for a printer.
21 * cupsAdminExportSamba() - Export a printer to Samba.
22 * cupsAdminGetServerSettings() - Get settings from the server.
23 * _cupsAdminGetServerSettings() - Get settings from the server (private).
24 * cupsAdminSetServerSettings() - Set settings on the server.
25 * _cupsAdminSetServerSettings() - Set settings on the server (private).
26 * do_samba_command() - Do a SAMBA command.
27 * get_cupsd_conf() - Get the current cupsd.conf file.
28 * invalidate_cupsd_cache() - Invalidate the cached cupsd.conf settings.
29 * write_option() - Write a CUPS option to a PPD file.
33 * Include necessary headers...
36 #include "adminutil.h"
45 # include <sys/wait.h>
53 extern int _cupsAdminGetServerSettings(http_t
*http
,
55 cups_option_t
**settings
);
56 extern int _cupsAdminSetServerSettings(http_t
*http
,
58 cups_option_t
*settings
);
59 static int do_samba_command(const char *command
,
61 const char *subcommand
,
64 static http_status_t
get_cupsd_conf(http_t
*http
, _cups_globals_t
*cg
,
65 time_t last_update
, char *name
,
66 int namelen
, int *remote
);
67 static void invalidate_cupsd_cache(_cups_globals_t
*cg
);
68 static void write_option(cups_file_t
*dstfp
, int order
,
69 const char *name
, const char *text
,
71 ipp_attribute_t
*suppattr
,
72 ipp_attribute_t
*defattr
, int defval
,
77 * 'cupsAdminCreateWindowsPPD()' - Create the Windows PPD file for a printer.
80 char * /* O - PPD file or NULL */
81 cupsAdminCreateWindowsPPD(
82 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
83 const char *dest
, /* I - Printer or class */
84 char *buffer
, /* I - Filename buffer */
85 int bufsize
) /* I - Size of filename buffer */
87 const char *src
; /* Source PPD filename */
88 cups_file_t
*srcfp
, /* Source PPD file */
89 *dstfp
; /* Destination PPD file */
90 ipp_t
*request
, /* IPP request */
91 *response
; /* IPP response */
92 ipp_attribute_t
*suppattr
, /* IPP -supported attribute */
93 *defattr
; /* IPP -default attribute */
94 cups_lang_t
*language
; /* Current language */
95 char line
[256], /* Line from PPD file */
96 junk
[256], /* Extra junk to throw away */
97 *ptr
, /* Pointer into line */
98 uri
[1024], /* Printer URI */
99 option
[41], /* Option */
100 choice
[41]; /* Choice */
101 int jcloption
, /* In a JCL option? */
102 jclorder
, /* Next JCL order dependency */
103 linenum
; /* Current line number */
104 time_t curtime
; /* Current time */
105 struct tm
*curdate
; /* Current date */
106 static const char * const pattrs
[] = /* Printer attributes we want */
108 "job-hold-until-supported",
109 "job-hold-until-default",
110 "job-sheets-supported",
111 "job-sheets-default",
112 "job-priority-supported",
113 "job-priority-default"
118 * Range check the input...
125 http
= _cupsConnect();
127 if (!http
|| !dest
|| !buffer
|| bufsize
< 2)
131 * Get the PPD file...
134 if ((src
= cupsGetPPD2(http
, dest
)) == NULL
)
138 * Get the supported banner pages, etc. for the printer...
141 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
143 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
144 "localhost", 0, "/printers/%s", dest
);
145 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
146 "printer-uri", NULL
, uri
);
148 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
149 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
153 * Do the request and get back a response...
156 response
= cupsDoRequest(http
, request
, "/");
157 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
164 * Open the original PPD file...
167 if ((srcfp
= cupsFileOpen(src
, "rb")) == NULL
)
171 * Create a temporary output file using the destination buffer...
174 if ((dstfp
= cupsTempFile2(buffer
, bufsize
)) == NULL
)
176 cupsFileClose(srcfp
);
184 * Write a new header explaining that this isn't the original PPD...
187 cupsFilePuts(dstfp
, "*PPD-Adobe: \"4.3\"\n");
189 curtime
= time(NULL
);
190 curdate
= gmtime(&curtime
);
192 cupsFilePrintf(dstfp
, "*%% Modified on %04d%02d%02d%02d%02d%02d+0000 "
193 "for CUPS Windows Driver\n",
194 curdate
->tm_year
+ 1900, curdate
->tm_mon
+ 1, curdate
->tm_mday
,
195 curdate
->tm_hour
, curdate
->tm_min
, curdate
->tm_sec
);
198 * Read the existing PPD file, converting all PJL commands to CUPS
199 * job ticket comments...
205 language
= cupsLangDefault();
207 while (cupsFileGets(srcfp
, line
, sizeof(line
)))
211 if (!strncmp(line
, "*PPD-Adobe:", 11))
214 * Already wrote the PPD header...
219 else if (!strncmp(line
, "*JCLBegin:", 10) ||
220 !strncmp(line
, "*JCLToPSInterpreter:", 20) ||
221 !strncmp(line
, "*JCLEnd:", 8) ||
222 !strncmp(line
, "*Protocols:", 11))
225 * Don't use existing JCL keywords; we'll create our own, below...
228 cupsFilePrintf(dstfp
, "*%% Commented out for CUPS Windows Driver...\n"
229 "*%%%s\n", line
+ 1);
232 else if (!strncmp(line
, "*JCLOpenUI", 10))
235 cupsFilePrintf(dstfp
, "%s\n", line
);
237 else if (!strncmp(line
, "*JCLCloseUI", 11))
240 cupsFilePrintf(dstfp
, "%s\n", line
);
242 else if (jcloption
&& !strncmp(line
, "*OrderDependency:", 17))
244 for (ptr
= line
+ 17; *ptr
&& isspace(*ptr
& 255); ptr
++);
246 ptr
= strchr(ptr
, ' ');
250 cupsFilePrintf(dstfp
, "*OrderDependency: %d%s\n", jclorder
, ptr
);
254 cupsFilePrintf(dstfp
, "%s\n", line
);
256 else if (jcloption
&&
257 strncmp(line
, "*End", 4) &&
258 strncmp(line
, "*Default", 8))
260 if ((ptr
= strchr(line
, ':')) == NULL
)
262 snprintf(line
, sizeof(line
),
263 _cupsLangString(language
, _("Missing value on line %d!")),
265 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
267 cupsFileClose(srcfp
);
268 cupsFileClose(dstfp
);
278 if ((ptr
= strchr(ptr
, '\"')) == NULL
)
280 snprintf(line
, sizeof(line
),
281 _cupsLangString(language
,
282 _("Missing double quote on line %d!")),
284 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
286 cupsFileClose(srcfp
);
287 cupsFileClose(dstfp
);
297 if (sscanf(line
, "*%40s%*[ \t]%40[^:/]", option
, choice
) != 2)
299 snprintf(line
, sizeof(line
),
300 _cupsLangString(language
,
301 _("Bad option + choice on line %d!")),
303 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
305 cupsFileClose(srcfp
);
306 cupsFileClose(dstfp
);
316 if (strchr(ptr
+ 1, '\"') == NULL
)
322 while (cupsFileGets(srcfp
, junk
, sizeof(junk
)) != NULL
)
326 if (!strncmp(junk
, "*End", 4))
331 snprintf(ptr
+ 1, sizeof(line
) - (ptr
- line
+ 1),
332 "%%cupsJobTicket: %s=%s\n\"\n*End", option
, choice
);
334 cupsFilePrintf(dstfp
, "*%% Changed for CUPS Windows Driver...\n%s\n",
338 cupsFilePrintf(dstfp
, "%s\n", line
);
341 cupsFileClose(srcfp
);
346 snprintf(line
, sizeof(line
),
347 _cupsLangString(language
, _("Empty PPD file!")),
349 _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR
, line
);
351 cupsFileClose(dstfp
);
360 * Now add the CUPS-specific attributes and options...
363 cupsFilePuts(dstfp
, "\n*% CUPS Job Ticket support and options...\n");
364 cupsFilePuts(dstfp
, "*Protocols: PJL\n");
365 cupsFilePuts(dstfp
, "*JCLBegin: \"%!PS-Adobe-3.0<0A>\"\n");
366 cupsFilePuts(dstfp
, "*JCLToPSInterpreter: \"\"\n");
367 cupsFilePuts(dstfp
, "*JCLEnd: \"\"\n");
369 cupsFilePuts(dstfp
, "\n*OpenGroup: CUPS/CUPS Options\n\n");
371 if ((defattr
= ippFindAttribute(response
, "job-hold-until-default",
372 IPP_TAG_ZERO
)) != NULL
&&
373 (suppattr
= ippFindAttribute(response
, "job-hold-until-supported",
374 IPP_TAG_ZERO
)) != NULL
)
375 write_option(dstfp
, jclorder
++, "cupsJobHoldUntil", "Hold Until",
376 "job-hold-until", suppattr
, defattr
, 0, 1);
378 if ((defattr
= ippFindAttribute(response
, "job-priority-default",
379 IPP_TAG_INTEGER
)) != NULL
&&
380 (suppattr
= ippFindAttribute(response
, "job-priority-supported",
381 IPP_TAG_RANGE
)) != NULL
)
382 write_option(dstfp
, jclorder
++, "cupsJobPriority", "Priority",
383 "job-priority", suppattr
, defattr
, 0, 1);
385 if ((defattr
= ippFindAttribute(response
, "job-sheets-default",
386 IPP_TAG_ZERO
)) != NULL
&&
387 (suppattr
= ippFindAttribute(response
, "job-sheets-supported",
388 IPP_TAG_ZERO
)) != NULL
)
390 write_option(dstfp
, jclorder
++, "cupsJobSheetsStart", "Start Banner",
391 "job-sheets", suppattr
, defattr
, 0, 2);
392 write_option(dstfp
, jclorder
, "cupsJobSheetsEnd", "End Banner",
393 "job-sheets", suppattr
, defattr
, 1, 2);
396 cupsFilePuts(dstfp
, "*CloseGroup: CUPS\n");
397 cupsFileClose(dstfp
);
406 * 'cupsAdminExportSamba()' - Export a printer to Samba.
409 int /* O - 1 on success, 0 on failure */
410 cupsAdminExportSamba(
411 const char *dest
, /* I - Destination to export */
412 const char *ppd
, /* I - PPD file */
413 const char *samba_server
, /* I - Samba server */
414 const char *samba_user
, /* I - Samba username */
415 const char *samba_password
, /* I - Samba password */
416 FILE *logfile
) /* I - Log file, if any */
418 int status
; /* Status of Samba commands */
419 int have_drivers
; /* Have drivers? */
420 char file
[1024], /* File to test for */
421 authfile
[1024], /* Temporary authentication file */
422 address
[1024], /* Address for command */
423 subcmd
[1024], /* Sub-command */
424 message
[1024]; /* Error message */
425 cups_file_t
*fp
; /* Authentication file */
426 cups_lang_t
*language
; /* Current language */
427 _cups_globals_t
*cg
= _cupsGlobals();
432 * Range check input...
435 if (!dest
|| !ppd
|| !samba_server
|| !samba_user
|| !samba_password
)
437 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
442 * Create a temporary authentication file for Samba...
445 if ((fp
= cupsTempFile2(authfile
, sizeof(authfile
))) == NULL
)
447 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
451 cupsFilePrintf(fp
, "username = %s\n", samba_user
);
452 cupsFilePrintf(fp
, "password = %s\n", samba_password
);
456 * See which drivers are available; the new CUPS v6 and Adobe drivers
457 * depend on the Windows 2k PS driver, so copy that driver first:
468 language
= cupsLangDefault();
470 snprintf(file
, sizeof(file
), "%s/drivers/pscript5.dll", cg
->cups_datadir
);
471 if (!access(file
, 0))
476 * Windows 2k driver is installed; do the smbclient commands needed
477 * to copy the Win2k drivers over...
480 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
482 snprintf(subcmd
, sizeof(subcmd
),
484 "put %s W32X86/%s.ppd;"
485 "put %s/drivers/ps5ui.dll W32X86/ps5ui.dll;"
486 "put %s/drivers/pscript.hlp W32X86/pscript.hlp;"
487 "put %s/drivers/pscript.ntf W32X86/pscript.ntf;"
488 "put %s/drivers/pscript5.dll W32X86/pscript5.dll",
489 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
490 cg
->cups_datadir
, cg
->cups_datadir
);
492 if ((status
= do_samba_command("smbclient", address
, subcmd
,
493 authfile
, logfile
)) != 0)
495 snprintf(message
, sizeof(message
),
496 _cupsLangString(language
,
497 _("Unable to copy Windows 2000 printer "
498 "driver files (%d)!")), status
);
500 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
503 _cupsLangPrintf(logfile
, "%s\n", message
);
511 * See if we also have the CUPS driver files; if so, use them!
514 snprintf(file
, sizeof(file
), "%s/drivers/cupsps6.dll", cg
->cups_datadir
);
515 if (!access(file
, 0))
518 * Copy the CUPS driver files over...
521 snprintf(subcmd
, sizeof(subcmd
),
522 "put %s/drivers/cups6.ini W32X86/cups6.ini;"
523 "put %s/drivers/cupsps6.dll W32X86/cupsps6.dll;"
524 "put %s/drivers/cupsui6.dll W32X86/cupsui6.dll",
525 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
527 if ((status
= do_samba_command("smbclient", address
, subcmd
,
528 authfile
, logfile
)) != 0)
530 snprintf(message
, sizeof(message
),
531 _cupsLangString(language
,
532 _("Unable to copy CUPS printer driver "
533 "files (%d)!")), status
);
535 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
538 _cupsLangPrintf(logfile
, "%s\n", message
);
546 * Do the rpcclient command needed for the CUPS drivers...
549 snprintf(subcmd
, sizeof(subcmd
),
550 "adddriver \"Windows NT x86\" \"%s:"
551 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
552 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
553 "cups6.ini,cupsps6.dll,cupsui6.dll\"",
559 * Don't have the CUPS drivers, so just use the standard Windows
563 snprintf(subcmd
, sizeof(subcmd
),
564 "adddriver \"Windows NT x86\" \"%s:"
565 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
566 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
570 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
571 authfile
, logfile
)) != 0)
573 snprintf(message
, sizeof(message
),
574 _cupsLangString(language
,
575 _("Unable to install Windows 2000 printer "
576 "driver files (%d)!")), status
);
578 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
581 _cupsLangPrintf(logfile
, "%s\n", message
);
590 * See if we have the Win9x PS driver...
593 snprintf(file
, sizeof(file
), "%s/drivers/ADOBEPS4.DRV", cg
->cups_datadir
);
594 if (!access(file
, 0))
599 * Do the smbclient commands needed for the Adobe Win9x drivers...
602 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
604 snprintf(subcmd
, sizeof(subcmd
),
606 "put %s WIN40/%s.PPD;"
607 "put %s/drivers/ADFONTS.MFM WIN40/ADFONTS.MFM;"
608 "put %s/drivers/ADOBEPS4.DRV WIN40/ADOBEPS4.DRV;"
609 "put %s/drivers/ADOBEPS4.HLP WIN40/ADOBEPS4.HLP;"
610 "put %s/drivers/ICONLIB.DLL WIN40/ICONLIB.DLL;"
611 "put %s/drivers/PSMON.DLL WIN40/PSMON.DLL;",
612 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
613 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
615 if ((status
= do_samba_command("smbclient", address
, subcmd
,
616 authfile
, logfile
)) != 0)
618 snprintf(message
, sizeof(message
),
619 _cupsLangString(language
,
620 _("Unable to copy Windows 9x printer "
621 "driver files (%d)!")), status
);
623 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
626 _cupsLangPrintf(logfile
, "%s\n", message
);
634 * Do the rpcclient commands needed for the Adobe Win9x drivers...
637 snprintf(subcmd
, sizeof(subcmd
),
638 "adddriver \"Windows 4.0\" \"%s:ADOBEPS4.DRV:%s.PPD:NULL:"
639 "ADOBEPS4.HLP:PSMON.DLL:RAW:"
640 "ADOBEPS4.DRV,%s.PPD,ADOBEPS4.HLP,PSMON.DLL,ADFONTS.MFM,"
644 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
645 authfile
, logfile
)) != 0)
647 snprintf(message
, sizeof(message
),
648 _cupsLangString(language
,
649 _("Unable to install Windows 9x printer "
650 "driver files (%d)!")), status
);
652 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
655 _cupsLangPrintf(logfile
, "%s\n", message
);
664 * See if we have the 64-bit Windows PS driver...
674 snprintf(file
, sizeof(file
), "%s/drivers/x64/pscript5.dll", cg
->cups_datadir
);
675 if (!access(file
, 0))
680 * 64-bit Windows driver is installed; do the smbclient commands needed
681 * to copy the Win64 drivers over...
684 snprintf(address
, sizeof(address
), "//%s/print$", samba_server
);
686 snprintf(subcmd
, sizeof(subcmd
),
689 "put %s/drivers/x64/ps5ui.dll x64/ps5ui.dll;"
690 "put %s/drivers/x64/pscript.hlp x64/pscript.hlp;"
691 "put %s/drivers/x64/pscript.ntf x64/pscript.ntf;"
692 "put %s/drivers/x64/pscript5.dll x64/pscript5.dll",
693 ppd
, dest
, cg
->cups_datadir
, cg
->cups_datadir
,
694 cg
->cups_datadir
, cg
->cups_datadir
);
696 if ((status
= do_samba_command("smbclient", address
, subcmd
,
697 authfile
, logfile
)) != 0)
699 snprintf(message
, sizeof(message
),
700 _cupsLangString(language
,
701 _("Unable to copy 64-bit Windows printer "
702 "driver files (%d)!")), status
);
704 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
707 _cupsLangPrintf(logfile
, "%s\n", message
);
715 * See if we also have the CUPS driver files; if so, use them!
718 snprintf(file
, sizeof(file
), "%s/drivers/x64/cupsps6.dll", cg
->cups_datadir
);
719 if (!access(file
, 0))
722 * Copy the CUPS driver files over...
725 snprintf(subcmd
, sizeof(subcmd
),
726 "put %s/drivers/x64/cups6.ini x64/cups6.ini;"
727 "put %s/drivers/x64/cupsps6.dll x64/cupsps6.dll;"
728 "put %s/drivers/x64/cupsui6.dll x64/cupsui6.dll",
729 cg
->cups_datadir
, cg
->cups_datadir
, cg
->cups_datadir
);
731 if ((status
= do_samba_command("smbclient", address
, subcmd
,
732 authfile
, logfile
)) != 0)
734 snprintf(message
, sizeof(message
),
735 _cupsLangString(language
,
736 _("Unable to copy 64-bit CUPS printer driver "
737 "files (%d)!")), status
);
739 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
742 _cupsLangPrintf(logfile
, "%s\n", message
);
750 * Do the rpcclient command needed for the CUPS drivers...
753 snprintf(subcmd
, sizeof(subcmd
),
754 "adddriver \"Windows x64\" \"%s:"
755 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
756 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
757 "cups6.ini,cupsps6.dll,cupsui6.dll\"",
763 * Don't have the CUPS drivers, so just use the standard Windows
767 snprintf(subcmd
, sizeof(subcmd
),
768 "adddriver \"Windows x64\" \"%s:"
769 "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
770 "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
774 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
775 authfile
, logfile
)) != 0)
777 snprintf(message
, sizeof(message
),
778 _cupsLangString(language
,
779 _("Unable to install Windows 2000 printer "
780 "driver files (%d)!")), status
);
782 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
785 _cupsLangPrintf(logfile
, "%s\n", message
);
793 if (logfile
&& !(have_drivers
& 1))
797 _cupsLangString(language
,
798 _("No Windows printer drivers are installed!")),
802 _cupsLangString(language
,
803 _("Warning, no Windows 2000 printer drivers "
807 _cupsSetError(IPP_NOT_FOUND
, message
);
808 _cupsLangPrintf(logfile
, "%s\n", message
);
811 if (have_drivers
== 0)
813 _cupsSetError(IPP_NOT_FOUND
, message
);
821 * Finally, associate the drivers we just added with the queue...
824 snprintf(subcmd
, sizeof(subcmd
), "setdriver %s %s", dest
, dest
);
826 if ((status
= do_samba_command("rpcclient", samba_server
, subcmd
,
827 authfile
, logfile
)) != 0)
829 snprintf(message
, sizeof(message
),
830 _cupsLangString(language
,
831 _("Unable to set Windows printer driver (%d)!")),
834 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
837 _cupsLangPrintf(logfile
, "%s\n", message
);
851 * 'cupsAdminGetServerSettings()' - Get settings from the server.
853 * The returned settings should be freed with cupsFreeOptions() when
854 * you are done with them.
859 int /* O - 1 on success, 0 on failure */
860 cupsAdminGetServerSettings(
861 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
862 int *num_settings
, /* O - Number of settings */
863 cups_option_t
**settings
) /* O - Settings */
865 return (_cupsAdminGetServerSettings(http
, num_settings
, settings
));
870 * '_cupsAdminGetServerSettings()' - Get settings from the server.
872 * The returned settings should be freed with cupsFreeOptions() when
873 * you are done with them.
878 int /* O - 1 on success, 0 on failure */
879 _cupsAdminGetServerSettings(
880 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
881 int *num_settings
, /* O - Number of settings */
882 cups_option_t
**settings
) /* O - Settings */
884 int i
; /* Looping var */
885 cups_file_t
*cupsd
; /* cupsd.conf file */
886 char cupsdconf
[1024]; /* cupsd.conf filename */
887 int remote
; /* Remote cupsd.conf file? */
888 http_status_t status
; /* Status of getting cupsd.conf */
889 char line
[1024], /* Line from cupsd.conf file */
890 *value
; /* Value on line */
891 cups_option_t
*setting
; /* Current setting */
892 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
896 * Range check input...
900 http
= _cupsConnect();
902 if (!http
|| !num_settings
|| !settings
)
904 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
919 * Get the cupsd.conf file...
922 if ((status
= get_cupsd_conf(http
, cg
, cg
->cupsd_update
, cupsdconf
,
923 sizeof(cupsdconf
), &remote
)) == HTTP_OK
)
925 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
927 char message
[1024]; /* Message string */
930 snprintf(message
, sizeof(message
),
931 _cupsLangString(cupsLangDefault(), _("open of %s failed: %s")),
932 cupsdconf
, strerror(errno
));
933 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
942 * Read the file, keeping track of what settings are enabled...
945 int remote_access
= 0, /* Remote access allowed? */
946 remote_admin
= 0, /* Remote administration allowed? */
947 remote_any
= 0, /* Remote access from anywhere allowed? */
948 browsing
= 1, /* Browsing enabled? */
949 browse_allow
= 1, /* Browse address set? */
950 browse_address
= 0, /* Browse address set? */
951 cancel_policy
= 1, /* Cancel-job policy set? */
952 debug_logging
= 0; /* LogLevel debug set? */
953 int linenum
= 0, /* Line number in file */
954 in_location
= 0, /* In a location section? */
955 in_policy
= 0, /* In a policy section? */
956 in_cancel_job
= 0, /* In a cancel-job section? */
957 in_admin_location
= 0; /* In the /admin location? */
960 invalidate_cupsd_cache(cg
);
962 cg
->cupsd_update
= time(NULL
);
963 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
965 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
967 if (!value
&& strncmp(line
, "</", 2))
968 value
= line
+ strlen(line
);
970 if ((!strcasecmp(line
, "Port") || !strcasecmp(line
, "Listen")) && value
)
972 char *port
; /* Pointer to port number, if any */
975 if ((port
= strrchr(value
, ':')) != NULL
)
977 else if (isdigit(*value
& 255))
980 * Listen on a port number implies remote access...
987 if (strcasecmp(value
, "localhost") && strcmp(value
, "127.0.0.1")
990 #endif /* AF_LOCAL */
992 && strcmp(value
, "::1")
993 #endif /* AF_INET6 */
997 else if (!strcasecmp(line
, "Browsing"))
999 browsing
= !strcasecmp(value
, "yes") || !strcasecmp(value
, "on") ||
1000 !strcasecmp(value
, "true");
1002 else if (!strcasecmp(line
, "BrowseAddress"))
1006 else if (!strcasecmp(line
, "BrowseAllow"))
1010 else if (!strcasecmp(line
, "BrowseOrder"))
1012 browse_allow
= !strncasecmp(value
, "deny,", 5);
1014 else if (!strcasecmp(line
, "LogLevel"))
1016 debug_logging
= !strncasecmp(value
, "debug", 5);
1018 else if (!strcasecmp(line
, "<Policy") && !strcasecmp(value
, "default"))
1022 else if (!strcasecmp(line
, "</Policy>"))
1026 else if (!strcasecmp(line
, "<Limit") && in_policy
&& value
)
1029 * See if the policy limit is for the Cancel-Job operation...
1032 char *valptr
; /* Pointer into value */
1037 for (valptr
= value
; !isspace(*valptr
& 255) && *valptr
; valptr
++);
1042 if (!strcasecmp(value
, "cancel-job") || !strcasecmp(value
, "all"))
1048 for (value
= valptr
; isspace(*value
& 255); value
++);
1051 else if (!strcasecmp(line
, "</Limit>"))
1055 else if (!strcasecmp(line
, "Require") && in_cancel_job
)
1059 else if (!strcasecmp(line
, "<Location") && value
)
1061 in_admin_location
= !strcasecmp(value
, "/admin");
1064 else if (!strcasecmp(line
, "</Location>"))
1066 in_admin_location
= 0;
1069 else if (!strcasecmp(line
, "Allow") && value
&&
1070 strcasecmp(value
, "localhost") && strcasecmp(value
, "127.0.0.1")
1073 #endif /* AF_LOCAL */
1075 && strcmp(value
, "::1")
1076 #endif /* AF_INET6 */
1079 if (in_admin_location
)
1081 else if (!strcasecmp(value
, "all"))
1084 else if (line
[0] != '<' && !in_location
&& !in_policy
)
1085 cg
->cupsd_num_settings
= cupsAddOption(line
, value
,
1086 cg
->cupsd_num_settings
,
1087 &(cg
->cupsd_settings
));
1090 cupsFileClose(cupsd
);
1092 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1093 debug_logging
? "1" : "0",
1094 cg
->cupsd_num_settings
,
1095 &(cg
->cupsd_settings
));
1097 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1098 (remote_access
&& remote_admin
) ?
1100 cg
->cupsd_num_settings
,
1101 &(cg
->cupsd_settings
));
1103 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
1104 remote_any
? "1" : "0",
1105 cg
->cupsd_num_settings
,
1106 &(cg
->cupsd_settings
));
1108 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
1109 (browsing
&& browse_allow
) ?
1111 cg
->cupsd_num_settings
,
1112 &(cg
->cupsd_settings
));
1114 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1115 (remote_access
&& browsing
&&
1116 browse_address
) ? "1" : "0",
1117 cg
->cupsd_num_settings
,
1118 &(cg
->cupsd_settings
));
1120 cg
->cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1121 cancel_policy
? "1" : "0",
1122 cg
->cupsd_num_settings
,
1123 &(cg
->cupsd_settings
));
1125 else if (status
!= HTTP_NOT_MODIFIED
)
1126 invalidate_cupsd_cache(cg
);
1129 * Remove any temporary files and copy the settings array...
1135 for (i
= cg
->cupsd_num_settings
, setting
= cg
->cupsd_settings
;
1138 *num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1139 *num_settings
, settings
);
1141 return (cg
->cupsd_num_settings
> 0);
1146 * 'cupsAdminSetServerSettings()' - Set settings on the server.
1151 int /* O - 1 on success, 0 on failure */
1152 cupsAdminSetServerSettings(
1153 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1154 int num_settings
, /* I - Number of settings */
1155 cups_option_t
*settings
) /* I - Settings */
1157 return (_cupsAdminSetServerSettings(http
, num_settings
, settings
));
1162 * '_cupsAdminSetServerSettings()' - Set settings on the server.
1167 int /* O - 1 on success, 0 on failure */
1168 _cupsAdminSetServerSettings(
1169 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1170 int num_settings
, /* I - Number of settings */
1171 cups_option_t
*settings
) /* I - Settings */
1173 int i
; /* Looping var */
1174 http_status_t status
; /* GET/PUT status */
1175 const char *server_port_env
; /* SERVER_PORT env var */
1176 int server_port
; /* IPP port for server */
1177 cups_file_t
*cupsd
; /* cupsd.conf file */
1178 char cupsdconf
[1024]; /* cupsd.conf filename */
1179 int remote
; /* Remote cupsd.conf file? */
1180 char tempfile
[1024]; /* Temporary new cupsd.conf */
1181 cups_file_t
*temp
; /* Temporary file */
1182 char line
[1024], /* Line from cupsd.conf file */
1183 *value
; /* Value on line */
1184 int linenum
, /* Line number in file */
1185 in_location
, /* In a location section? */
1186 in_policy
, /* In a policy section? */
1187 in_default_policy
, /* In the default policy section? */
1188 in_cancel_job
, /* In a cancel-job section? */
1189 in_admin_location
, /* In the /admin location? */
1190 in_conf_location
, /* In the /admin/conf location? */
1191 in_root_location
; /* In the / location? */
1192 const char *val
; /* Setting value */
1193 int remote_printers
, /* Show remote printers */
1194 share_printers
, /* Share local printers */
1195 remote_admin
, /* Remote administration allowed? */
1196 remote_any
, /* Remote access from anywhere? */
1197 user_cancel_any
, /* Cancel-job policy set? */
1198 debug_logging
; /* LogLevel debug set? */
1199 int wrote_port_listen
, /* Wrote the port/listen lines? */
1200 wrote_browsing
, /* Wrote the browsing lines? */
1201 wrote_policy
, /* Wrote the policy? */
1202 wrote_loglevel
, /* Wrote the LogLevel line? */
1203 wrote_admin_location
, /* Wrote the /admin location? */
1204 wrote_conf_location
, /* Wrote the /admin/conf location? */
1205 wrote_root_location
; /* Wrote the / location? */
1206 int indent
; /* Indentation */
1207 int cupsd_num_settings
; /* New number of settings */
1208 int old_remote_printers
, /* Show remote printers */
1209 old_share_printers
, /* Share local printers */
1210 old_remote_admin
, /* Remote administration allowed? */
1211 old_user_cancel_any
, /* Cancel-job policy set? */
1212 old_debug_logging
; /* LogLevel debug set? */
1213 cups_option_t
*cupsd_settings
, /* New settings */
1214 *setting
; /* Current setting */
1215 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1219 * Range check input...
1223 http
= _cupsConnect();
1225 if (!http
|| !num_settings
|| !settings
)
1227 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
1233 * Get the cupsd.conf file...
1236 if (get_cupsd_conf(http
, cg
, 0, cupsdconf
, sizeof(cupsdconf
),
1237 &remote
) == HTTP_OK
)
1239 if ((cupsd
= cupsFileOpen(cupsdconf
, "r")) == NULL
)
1241 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
1249 * Get current settings...
1252 if (!_cupsAdminGetServerSettings(http
, &cupsd_num_settings
,
1256 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, cupsd_num_settings
,
1257 cupsd_settings
)) != NULL
)
1258 old_debug_logging
= atoi(val
);
1260 old_debug_logging
= 0;
1262 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, cupsd_num_settings
,
1263 cupsd_settings
)) != NULL
)
1264 old_remote_admin
= atoi(val
);
1266 old_remote_admin
= 0;
1268 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, cupsd_num_settings
,
1269 cupsd_settings
)) != NULL
)
1270 remote_any
= atoi(val
);
1274 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
, cupsd_num_settings
,
1275 cupsd_settings
)) != NULL
)
1276 old_remote_printers
= atoi(val
);
1278 old_remote_printers
= 1;
1280 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, cupsd_num_settings
,
1281 cupsd_settings
)) != NULL
)
1282 old_share_printers
= atoi(val
);
1284 old_share_printers
= 0;
1286 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, cupsd_num_settings
,
1287 cupsd_settings
)) != NULL
)
1288 old_user_cancel_any
= atoi(val
);
1290 old_user_cancel_any
= 0;
1292 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
1295 * Get basic settings...
1298 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, num_settings
,
1301 debug_logging
= atoi(val
);
1303 if (debug_logging
== old_debug_logging
)
1306 * No change to this setting...
1315 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, num_settings
,
1317 remote_any
= atoi(val
);
1319 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, num_settings
,
1322 remote_admin
= atoi(val
);
1324 if (remote_admin
== old_remote_admin
&& remote_any
< 0)
1327 * No change to this setting...
1336 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
, num_settings
,
1339 remote_printers
= atoi(val
);
1341 if (remote_printers
== old_remote_printers
)
1344 * No change to this setting...
1347 remote_printers
= -1;
1351 remote_printers
= -1;
1353 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, num_settings
,
1356 share_printers
= atoi(val
);
1358 if (share_printers
== old_share_printers
&& remote_any
< 0)
1361 * No change to this setting...
1364 share_printers
= -1;
1368 share_printers
= -1;
1370 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, num_settings
,
1373 user_cancel_any
= atoi(val
);
1375 if (user_cancel_any
== old_user_cancel_any
)
1378 * No change to this setting...
1381 user_cancel_any
= -1;
1385 user_cancel_any
= -1;
1388 * Create a temporary file for the new cupsd.conf file...
1391 if ((temp
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
1393 cupsFileClose(cupsd
);
1398 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
1403 * Copy the old file to the new, making changes along the way...
1406 cupsd_num_settings
= 0;
1407 in_admin_location
= 0;
1409 in_conf_location
= 0;
1410 in_default_policy
= 0;
1413 in_root_location
= 0;
1415 wrote_admin_location
= 0;
1417 wrote_conf_location
= 0;
1420 wrote_port_listen
= 0;
1421 wrote_root_location
= 0;
1424 if ((server_port_env
= getenv("SERVER_PORT")) != NULL
)
1426 if ((server_port
= atoi(server_port_env
)) <= 0)
1427 server_port
= ippPort();
1430 server_port
= ippPort();
1432 if (server_port
<= 0)
1433 server_port
= IPP_PORT
;
1435 while (cupsFileGetConf(cupsd
, line
, sizeof(line
), &value
, &linenum
))
1437 if ((!strcasecmp(line
, "Port") || !strcasecmp(line
, "Listen")) &&
1438 (share_printers
>= 0 || remote_admin
>= 0))
1440 if (!wrote_port_listen
)
1442 wrote_port_listen
= 1;
1444 if (share_printers
> 0 || remote_admin
> 0)
1446 cupsFilePuts(temp
, "# Allow remote access\n");
1447 cupsFilePrintf(temp
, "Port %d\n", server_port
);
1451 cupsFilePuts(temp
, "# Only listen for connections from the local "
1453 cupsFilePrintf(temp
, "Listen localhost:%d\n", server_port
);
1456 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1457 if ((!value
|| strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)) &&
1458 !access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1459 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1460 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1462 else if (value
&& value
[0] == '/'
1463 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1464 && strcmp(CUPS_DEFAULT_DOMAINSOCKET
, value
)
1465 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1467 cupsFilePrintf(temp
, "Listen %s\n", value
);
1469 else if ((!strcasecmp(line
, "Browsing") ||
1470 !strcasecmp(line
, "BrowseAddress") ||
1471 !strcasecmp(line
, "BrowseAllow") ||
1472 !strcasecmp(line
, "BrowseDeny") ||
1473 !strcasecmp(line
, "BrowseLocalProtocols") ||
1474 !strcasecmp(line
, "BrowseRemoteProtocols") ||
1475 !strcasecmp(line
, "BrowseOrder")) &&
1476 (remote_printers
>= 0 || share_printers
>= 0))
1478 if (!wrote_browsing
)
1480 int new_remote_printers
= (remote_printers
> 0 ||
1481 (remote_printers
== -1 &&
1482 old_remote_printers
> 0));
1483 int new_share_printers
= (share_printers
> 0 ||
1484 (share_printers
== -1 &&
1485 old_share_printers
> 0));
1489 if (new_remote_printers
|| new_share_printers
)
1491 const char *localp
= cupsGetOption("BrowseLocalProtocols",
1492 num_settings
, settings
);
1493 const char *remotep
= cupsGetOption("BrowseRemoteProtocols",
1494 num_settings
, settings
);
1497 localp
= cupsGetOption("BrowseLocalProtocols", cupsd_num_settings
,
1501 remotep
= cupsGetOption("BrowseRemoteProtocols", cupsd_num_settings
,
1504 if (new_remote_printers
&& new_share_printers
)
1506 "# Enable printer sharing and shared printers.\n");
1507 else if (new_remote_printers
)
1509 "# Show shared printers on the local network.\n");
1512 "# Share local printers on the local network.\n");
1514 cupsFilePuts(temp
, "Browsing On\n");
1515 cupsFilePuts(temp
, "BrowseOrder allow,deny\n");
1517 if (new_remote_printers
)
1519 cupsFilePuts(temp
, "BrowseAllow all\n");
1521 if (!remotep
|| !*remotep
)
1522 cupsFilePuts(temp
, "BrowseRemoteProtocols "
1523 CUPS_DEFAULT_BROWSE_REMOTE_PROTOCOLS
"\n");
1525 cupsFilePrintf(temp
, "BrowseRemoteProtocols %s\n", remotep
);
1528 cupsFilePuts(temp
, "BrowseRemoteProtocols\n");
1530 if (new_share_printers
)
1532 cupsFilePuts(temp
, "BrowseAddress @LOCAL\n");
1534 if (!localp
|| !*localp
)
1535 cupsFilePuts(temp
, "BrowseLocalProtocols "
1536 CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS
"\n");
1538 cupsFilePrintf(temp
, "BrowseLocalProtocols %s\n", localp
);
1541 cupsFilePuts(temp
, "BrowseLocalProtocols\n");
1546 "# Disable printer sharing and shared printers.\n");
1547 cupsFilePuts(temp
, "Browsing Off\n");
1551 else if (!strcasecmp(line
, "LogLevel") && debug_logging
>= 0)
1558 "# Show troubleshooting information in error_log.\n");
1559 cupsFilePuts(temp
, "LogLevel debug\n");
1563 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1564 cupsFilePuts(temp
, "LogLevel " CUPS_DEFAULT_LOG_LEVEL
"\n");
1567 else if (!strcasecmp(line
, "<Policy"))
1569 in_default_policy
= !strcasecmp(value
, "default");
1572 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1575 else if (!strcasecmp(line
, "</Policy>"))
1578 if (!wrote_policy
&& in_default_policy
)
1582 if (!user_cancel_any
)
1583 cupsFilePuts(temp
, " # Only the owner or an administrator can "
1585 " <Limit Cancel-Job>\n"
1586 " Order deny,allow\n"
1587 " Require user @OWNER "
1588 CUPS_DEFAULT_PRINTADMIN_AUTH
"\n"
1593 in_default_policy
= 0;
1595 cupsFilePuts(temp
, "</Policy>\n");
1597 else if (!strcasecmp(line
, "<Location"))
1601 if (!strcmp(value
, "/admin"))
1602 in_admin_location
= 1;
1603 if (!strcmp(value
, "/admin/conf"))
1604 in_conf_location
= 1;
1605 else if (!strcmp(value
, "/"))
1606 in_root_location
= 1;
1608 cupsFilePrintf(temp
, "%s %s>\n", line
, value
);
1610 else if (!strcasecmp(line
, "</Location>"))
1614 if (in_admin_location
&& remote_admin
>= 0)
1616 wrote_admin_location
= 1;
1619 cupsFilePuts(temp
, " # Allow remote administration...\n");
1620 else if (remote_admin
== 0)
1621 cupsFilePuts(temp
, " # Restrict access to the admin pages...\n");
1623 cupsFilePuts(temp
, " Order allow,deny\n");
1626 cupsFilePrintf(temp
, " Allow %s\n",
1627 remote_any
> 0 ? "all" : "@LOCAL");
1629 else if (in_conf_location
&& remote_admin
>= 0)
1631 wrote_conf_location
= 1;
1634 cupsFilePuts(temp
, " # Allow remote access to the configuration "
1637 cupsFilePuts(temp
, " # Restrict access to the configuration "
1640 cupsFilePuts(temp
, " Order allow,deny\n");
1643 cupsFilePrintf(temp
, " Allow %s\n",
1644 remote_any
> 0 ? "all" : "@LOCAL");
1646 else if (in_root_location
&& (remote_admin
>= 0 || share_printers
>= 0))
1648 wrote_root_location
= 1;
1650 if (remote_admin
> 0 && share_printers
> 0)
1651 cupsFilePuts(temp
, " # Allow shared printing and remote "
1652 "administration...\n");
1653 else if (remote_admin
> 0)
1654 cupsFilePuts(temp
, " # Allow remote administration...\n");
1655 else if (share_printers
> 0)
1656 cupsFilePuts(temp
, " # Allow shared printing...\n");
1658 cupsFilePuts(temp
, " # Restrict access to the server...\n");
1660 cupsFilePuts(temp
, " Order allow,deny\n");
1662 if (remote_admin
> 0 || share_printers
> 0)
1663 cupsFilePrintf(temp
, " Allow %s\n",
1664 remote_any
> 0 ? "all" : "@LOCAL");
1667 in_admin_location
= 0;
1668 in_conf_location
= 0;
1669 in_root_location
= 0;
1671 cupsFilePuts(temp
, "</Location>\n");
1673 else if (!strcasecmp(line
, "<Limit") && in_default_policy
)
1676 * See if the policy limit is for the Cancel-Job operation...
1679 char *valptr
; /* Pointer into value */
1684 if (!strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1687 * Don't write anything for this limit section...
1694 cupsFilePrintf(temp
, " %s", line
);
1698 for (valptr
= value
; !isspace(*valptr
& 255) && *valptr
; valptr
++);
1703 if (!strcasecmp(value
, "cancel-job") && user_cancel_any
>= 0)
1706 * Write everything except for this definition...
1712 cupsFilePrintf(temp
, " %s", value
);
1714 for (value
= valptr
; isspace(*value
& 255); value
++);
1717 cupsFilePuts(temp
, ">\n");
1720 else if (!strcasecmp(line
, "</Limit>") && in_cancel_job
)
1724 if (in_cancel_job
== 1)
1725 cupsFilePuts(temp
, " </Limit>\n");
1729 if (!user_cancel_any
)
1730 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1732 " <Limit Cancel-Job>\n"
1733 " Order deny,allow\n"
1734 " Require user @OWNER "
1735 CUPS_DEFAULT_PRINTADMIN_AUTH
"\n"
1740 else if ((((in_admin_location
|| in_conf_location
|| in_root_location
) &&
1741 remote_admin
>= 0) ||
1742 (in_root_location
&& share_printers
>= 0)) &&
1743 (((!strcasecmp(line
, "Allow") || !strcasecmp(line
, "Deny")) &&
1744 !strcasecmp(value
, "@LOCAL")) ||
1745 !strcasecmp(line
, "Order")))
1747 else if (in_cancel_job
== 2)
1749 else if (!strcasecmp(line
, "<Limit") && value
)
1750 cupsFilePrintf(temp
, " %s %s>\n", line
, value
);
1751 else if (line
[0] == '<')
1755 cupsFilePrintf(temp
, "%*s%s %s>\n", indent
, "", line
, value
);
1763 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1766 else if (!in_policy
&& !in_location
&&
1767 (val
= cupsGetOption(line
, num_settings
, settings
)) != NULL
)
1770 * Replace this directive's value with the new one...
1773 cupsd_num_settings
= cupsAddOption(line
, val
, cupsd_num_settings
,
1777 * Write the new value in its place, without indentation since we
1778 * only support setting root directives, not in sections...
1781 cupsFilePrintf(temp
, "%s %s\n", line
, val
);
1785 if (!in_policy
&& !in_location
)
1788 * Record the non-policy, non-location directives that we find
1789 * in the server settings, since we cache this info and record it
1790 * in _cupsAdminGetServerSettings()...
1793 cupsd_num_settings
= cupsAddOption(line
, value
, cupsd_num_settings
,
1797 cupsFilePrintf(temp
, "%*s%s %s\n", indent
, "", line
, value
);
1800 cupsFilePrintf(temp
, "%*s%s\n", indent
, "", line
);
1804 * Write any missing info...
1807 if (!wrote_browsing
&& (remote_printers
>= 0 || share_printers
>= 0))
1809 if (remote_printers
> 0 || share_printers
> 0)
1811 if (remote_printers
> 0 && share_printers
> 0)
1812 cupsFilePuts(temp
, "# Enable printer sharing and shared printers.\n");
1813 else if (remote_printers
> 0)
1814 cupsFilePuts(temp
, "# Show shared printers on the local network.\n");
1816 cupsFilePuts(temp
, "# Share local printers on the local network.\n");
1818 cupsFilePuts(temp
, "Browsing On\n");
1819 cupsFilePuts(temp
, "BrowseOrder allow,deny\n");
1821 if (remote_printers
> 0)
1822 cupsFilePuts(temp
, "BrowseAllow all\n");
1824 if (share_printers
> 0)
1825 cupsFilePuts(temp
, "BrowseAddress @LOCAL\n");
1829 cupsFilePuts(temp
, "# Disable printer sharing and shared printers.\n");
1830 cupsFilePuts(temp
, "Browsing Off\n");
1834 if (!wrote_loglevel
&& debug_logging
>= 0)
1838 cupsFilePuts(temp
, "# Show troubleshooting information in error_log.\n");
1839 cupsFilePuts(temp
, "LogLevel debug\n");
1843 cupsFilePuts(temp
, "# Show general information in error_log.\n");
1844 cupsFilePuts(temp
, "LogLevel " CUPS_DEFAULT_LOG_LEVEL
"\n");
1848 if (!wrote_port_listen
&& (share_printers
>= 0 || remote_admin
>= 0))
1850 if (share_printers
> 0 || remote_admin
> 0)
1852 cupsFilePuts(temp
, "# Allow remote access\n");
1853 cupsFilePrintf(temp
, "Port %d\n", ippPort());
1858 "# Only listen for connections from the local machine.\n");
1859 cupsFilePrintf(temp
, "Listen localhost:%d\n", ippPort());
1862 #ifdef CUPS_DEFAULT_DOMAINSOCKET
1863 if (!access(CUPS_DEFAULT_DOMAINSOCKET
, 0))
1864 cupsFilePuts(temp
, "Listen " CUPS_DEFAULT_DOMAINSOCKET
"\n");
1865 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
1868 if (!wrote_root_location
&& (remote_admin
>= 0 || share_printers
>= 0))
1870 if (remote_admin
> 0 && share_printers
> 0)
1872 "# Allow shared printing and remote administration...\n");
1873 else if (remote_admin
> 0)
1874 cupsFilePuts(temp
, "# Allow remote administration...\n");
1875 else if (share_printers
> 0)
1876 cupsFilePuts(temp
, "# Allow shared printing...\n");
1878 cupsFilePuts(temp
, "# Restrict access to the server...\n");
1880 cupsFilePuts(temp
, "<Location />\n"
1881 " Order allow,deny\n");
1883 if (remote_admin
> 0 || share_printers
> 0)
1884 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1886 cupsFilePuts(temp
, "</Location>\n");
1889 if (!wrote_admin_location
&& remote_admin
>= 0)
1892 cupsFilePuts(temp
, "# Allow remote administration...\n");
1894 cupsFilePuts(temp
, "# Restrict access to the admin pages...\n");
1896 cupsFilePuts(temp
, "<Location /admin>\n"
1897 " Order allow,deny\n");
1900 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1902 cupsFilePuts(temp
, "</Location>\n");
1905 if (!wrote_conf_location
&& remote_admin
>= 0)
1909 "# Allow remote access to the configuration files...\n");
1911 cupsFilePuts(temp
, "# Restrict access to the configuration files...\n");
1913 cupsFilePuts(temp
, "<Location /admin/conf>\n"
1914 " AuthType Default\n"
1915 " Require user @SYSTEM\n"
1916 " Order allow,deny\n");
1919 cupsFilePrintf(temp
, " Allow %s\n", remote_any
> 0 ? "all" : "@LOCAL");
1921 cupsFilePuts(temp
, "</Location>\n");
1924 if (!wrote_policy
&& user_cancel_any
>= 0)
1926 cupsFilePuts(temp
, "<Policy default>\n"
1927 " # Job-related operations must be done by the owner "
1928 "or an administrator...\n"
1929 " <Limit Send-Document Send-URI Hold-Job Release-Job "
1930 "Restart-Job Purge-Jobs Set-Job-Attributes "
1931 "Create-Job-Subscription Renew-Subscription "
1932 "Cancel-Subscription Get-Notifications Reprocess-Job "
1933 "Cancel-Current-Job Suspend-Current-Job Resume-Job "
1935 " Require user @OWNER @SYSTEM\n"
1936 " Order deny,allow\n"
1938 " # All administration operations require an "
1939 "administrator to authenticate...\n"
1940 " <Limit Pause-Printer Resume-Printer "
1941 "Set-Printer-Attributes Enable-Printer "
1942 "Disable-Printer Pause-Printer-After-Current-Job "
1943 "Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer "
1944 "Activate-Printer Restart-Printer Shutdown-Printer "
1945 "Startup-Printer Promote-Job Schedule-Job-After "
1946 "CUPS-Add-Printer CUPS-Delete-Printer "
1947 "CUPS-Add-Class CUPS-Delete-Class "
1948 "CUPS-Accept-Jobs CUPS-Reject-Jobs "
1949 "CUPS-Set-Default CUPS-Add-Device CUPS-Delete-Device>\n"
1950 " AuthType Default\n"
1951 " Require user @SYSTEM\n"
1952 " Order deny,allow\n"
1955 if (!user_cancel_any
)
1956 cupsFilePuts(temp
, " # Only the owner or an administrator can cancel "
1958 " <Limit Cancel-Job>\n"
1959 " Order deny,allow\n"
1960 " Require user @OWNER "
1961 CUPS_DEFAULT_PRINTADMIN_AUTH
"\n"
1964 cupsFilePuts(temp
, " <Limit All>\n"
1965 " Order deny,allow\n"
1970 for (i
= num_settings
, setting
= settings
; i
> 0; i
--, setting
++)
1971 if (setting
->name
[0] != '_' &&
1972 !cupsGetOption(setting
->name
, cupsd_num_settings
, cupsd_settings
))
1975 * Add this directive to the list of directives we have written...
1978 cupsd_num_settings
= cupsAddOption(setting
->name
, setting
->value
,
1979 cupsd_num_settings
, &cupsd_settings
);
1982 * Write the new value, without indentation since we only support
1983 * setting root directives, not in sections...
1986 cupsFilePrintf(temp
, "%s %s\n", setting
->name
, setting
->value
);
1989 cupsFileClose(cupsd
);
1990 cupsFileClose(temp
);
1993 * Upload the configuration file to the server...
1996 status
= cupsPutFile(http
, "/admin/conf/cupsd.conf", tempfile
);
1998 if (status
== HTTP_CREATED
)
2001 * Updated OK, add the basic settings...
2004 if (debug_logging
>= 0)
2005 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
2006 debug_logging
? "1" : "0",
2007 cupsd_num_settings
, &cupsd_settings
);
2009 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
2010 old_debug_logging
? "1" : "0",
2011 cupsd_num_settings
, &cupsd_settings
);
2013 if (remote_admin
>= 0)
2014 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
2015 remote_admin
? "1" : "0",
2016 cupsd_num_settings
, &cupsd_settings
);
2018 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
2019 old_remote_admin
? "1" : "0",
2020 cupsd_num_settings
, &cupsd_settings
);
2022 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
2023 remote_any
? "1" : "0",
2024 cupsd_num_settings
, &cupsd_settings
);
2026 if (remote_printers
>= 0)
2027 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
2028 remote_printers
? "1" : "0",
2029 cupsd_num_settings
, &cupsd_settings
);
2031 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
2032 old_remote_printers
? "1" : "0",
2033 cupsd_num_settings
, &cupsd_settings
);
2035 if (share_printers
>= 0)
2036 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
2037 share_printers
? "1" : "0",
2038 cupsd_num_settings
, &cupsd_settings
);
2040 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
2041 old_share_printers
? "1" : "0",
2042 cupsd_num_settings
, &cupsd_settings
);
2044 if (user_cancel_any
>= 0)
2045 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
2046 user_cancel_any
? "1" : "0",
2047 cupsd_num_settings
, &cupsd_settings
);
2049 cupsd_num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
2050 old_user_cancel_any
? "1" : "0",
2051 cupsd_num_settings
, &cupsd_settings
);
2054 * Save the new values...
2057 invalidate_cupsd_cache(cg
);
2059 cg
->cupsd_num_settings
= cupsd_num_settings
;
2060 cg
->cupsd_settings
= cupsd_settings
;
2061 cg
->cupsd_update
= time(NULL
);
2063 httpGetHostname(http
, cg
->cupsd_hostname
, sizeof(cg
->cupsd_hostname
));
2066 cupsFreeOptions(cupsd_num_settings
, cupsd_settings
);
2069 * Remote our temp files and return...
2077 return (status
== HTTP_CREATED
);
2082 * 'do_samba_command()' - Do a SAMBA command.
2085 static int /* O - Status of command */
2086 do_samba_command(const char *command
, /* I - Command to run */
2087 const char *address
, /* I - Address for command */
2088 const char *subcmd
, /* I - Sub-command */
2089 const char *authfile
, /* I - Samba authentication file */
2090 FILE *logfile
) /* I - Optional log file */
2093 return (1); /* Always fail on Windows... */
2096 int status
; /* Status of command */
2097 int pid
; /* Process ID of child */
2101 _cupsLangPrintf(logfile
,
2102 _("Running command: %s %s -N -A %s -c \'%s\'\n"),
2103 command
, address
, authfile
, subcmd
);
2105 if ((pid
= fork()) == 0)
2108 * Child goes here, redirect stdin/out/err and execute the command...
2112 open("/dev/null", O_RDONLY
);
2117 dup(fileno(logfile
));
2119 open("/dev/null", O_WRONLY
);
2124 execlp(command
, command
, address
, "-N", "-A", authfile
, "-c", subcmd
,
2133 _cupsLangPrintf(logfile
, _("Unable to run \"%s\": %s\n"),
2134 command
, strerror(errno
));
2139 * Wait for the process to complete...
2142 while (wait(&status
) != pid
);
2146 _cupsLangPuts(logfile
, "\n");
2148 DEBUG_printf(("status=%d\n", status
));
2150 if (WIFEXITED(status
))
2151 return (WEXITSTATUS(status
));
2153 return (-WTERMSIG(status
));
2159 * 'get_cupsd_conf()' - Get the current cupsd.conf file.
2162 static http_status_t
/* O - Status of request */
2164 http_t
*http
, /* I - Connection to server */
2165 _cups_globals_t
*cg
, /* I - Global data */
2166 time_t last_update
, /* I - Last update time for file */
2167 char *name
, /* I - Filename buffer */
2168 int namesize
, /* I - Size of filename buffer */
2169 int *remote
) /* O - Remote file? */
2171 int fd
; /* Temporary file descriptor */
2173 struct stat info
; /* cupsd.conf file information */
2175 http_status_t status
; /* Status of getting cupsd.conf */
2176 char host
[HTTP_MAX_HOST
]; /* Hostname for connection */
2180 * See if we already have the data we need...
2183 httpGetHostname(http
, host
, sizeof(host
));
2185 if (strcasecmp(cg
->cupsd_hostname
, host
))
2186 invalidate_cupsd_cache(cg
);
2188 snprintf(name
, namesize
, "%s/cupsd.conf", cg
->cups_serverroot
);
2192 if (!strcasecmp(host
, "localhost") && !access(name
, R_OK
))
2195 * Read the local file rather than using HTTP...
2198 if (stat(name
, &info
))
2200 char message
[1024]; /* Message string */
2203 snprintf(message
, sizeof(message
),
2204 _cupsLangString(cupsLangDefault(), _("stat of %s failed: %s")),
2205 name
, strerror(errno
));
2206 _cupsSetError(IPP_INTERNAL_ERROR
, message
);
2210 return (HTTP_SERVER_ERROR
);
2212 else if (last_update
&& info
.st_mtime
<= last_update
)
2213 status
= HTTP_NOT_MODIFIED
;
2221 * Read cupsd.conf via a HTTP GET request...
2224 if ((fd
= cupsTempFd(name
, namesize
)) < 0)
2228 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
2230 invalidate_cupsd_cache(cg
);
2232 return (HTTP_SERVER_ERROR
);
2237 httpClearFields(http
);
2240 httpSetField(http
, HTTP_FIELD_IF_MODIFIED_SINCE
,
2241 httpGetDateString(last_update
));
2243 status
= cupsGetFd(http
, "/admin/conf/cupsd.conf", fd
);
2247 if (status
!= HTTP_OK
)
2259 * 'invalidate_cupsd_cache()' - Invalidate the cached cupsd.conf settings.
2263 invalidate_cupsd_cache(
2264 _cups_globals_t
*cg
) /* I - Global data */
2266 cupsFreeOptions(cg
->cupsd_num_settings
, cg
->cupsd_settings
);
2268 cg
->cupsd_hostname
[0] = '\0';
2269 cg
->cupsd_update
= 0;
2270 cg
->cupsd_num_settings
= 0;
2271 cg
->cupsd_settings
= NULL
;
2276 * 'write_option()' - Write a CUPS option to a PPD file.
2280 write_option(cups_file_t
*dstfp
, /* I - PPD file */
2281 int order
, /* I - Order dependency */
2282 const char *name
, /* I - Option name */
2283 const char *text
, /* I - Option text */
2284 const char *attrname
, /* I - Attribute name */
2285 ipp_attribute_t
*suppattr
, /* I - IPP -supported attribute */
2286 ipp_attribute_t
*defattr
, /* I - IPP -default attribute */
2287 int defval
, /* I - Default value number */
2288 int valcount
) /* I - Number of values */
2290 int i
; /* Looping var */
2293 cupsFilePrintf(dstfp
, "*JCLOpenUI *%s/%s: PickOne\n"
2294 "*OrderDependency: %d JCLSetup *%s\n",
2295 name
, text
, order
, name
);
2297 if (defattr
->value_tag
== IPP_TAG_INTEGER
)
2300 * Do numeric options with a range or list...
2303 cupsFilePrintf(dstfp
, "*Default%s: %d\n", name
,
2304 defattr
->values
[defval
].integer
);
2306 if (suppattr
->value_tag
== IPP_TAG_RANGE
)
2309 * List each number in the range...
2312 for (i
= suppattr
->values
[0].range
.lower
;
2313 i
<= suppattr
->values
[0].range
.upper
;
2316 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, i
);
2319 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n",
2321 else if (defval
== 0)
2322 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
, i
);
2323 else if (defval
< (valcount
- 1))
2324 cupsFilePrintf(dstfp
, ",%d\"\n", i
);
2326 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", i
);
2332 * List explicit numbers...
2335 for (i
= 0; i
< suppattr
->num_values
; i
++)
2337 cupsFilePrintf(dstfp
, "*%s %d: \"", name
, suppattr
->values
[i
].integer
);
2340 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\n\"\n*End\n", attrname
,
2341 suppattr
->values
[i
].integer
);
2342 else if (defval
== 0)
2343 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%d\"\n", attrname
,
2344 suppattr
->values
[i
].integer
);
2345 else if (defval
< (valcount
- 1))
2346 cupsFilePrintf(dstfp
, ",%d\"\n", suppattr
->values
[i
].integer
);
2348 cupsFilePrintf(dstfp
, ",%d\n\"\n*End\n", suppattr
->values
[i
].integer
);
2355 * Do text options with a list...
2358 cupsFilePrintf(dstfp
, "*Default%s: %s\n", name
,
2359 defattr
->values
[defval
].string
.text
);
2361 for (i
= 0; i
< suppattr
->num_values
; i
++)
2363 cupsFilePrintf(dstfp
, "*%s %s: \"", name
,
2364 suppattr
->values
[i
].string
.text
);
2367 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\n\"\n*End\n", attrname
,
2368 suppattr
->values
[i
].string
.text
);
2369 else if (defval
== 0)
2370 cupsFilePrintf(dstfp
, "%%cupsJobTicket: %s=%s\"\n", attrname
,
2371 suppattr
->values
[i
].string
.text
);
2372 else if (defval
< (valcount
- 1))
2373 cupsFilePrintf(dstfp
, ",%s\"\n", suppattr
->values
[i
].string
.text
);
2375 cupsFilePrintf(dstfp
, ",%s\n\"\n*End\n",
2376 suppattr
->values
[i
].string
.text
);
2380 cupsFilePrintf(dstfp
, "*JCLCloseUI: *%s\n\n", name
);
2385 * End of "$Id: adminutil.c 7576 2008-05-15 21:05:14Z mike $".