2 * "$Id: admin.c 8029 2008-10-08 21:07:45Z mike $"
4 * Administration CGI for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2009 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * main() - Main entry for CGI.
18 * choose_device_cb() - Add a device to the device selection page.
19 * do_add_rss_subscription() - Add a RSS subscription.
20 * do_am_class() - Add or modify a class.
21 * do_am_printer() - Add or modify a printer.
22 * do_cancel_subscription() - Cancel a subscription.
23 * do_config_server() - Configure server settings.
24 * do_delete_class() - Delete a class.
25 * do_delete_printer() - Delete a printer.
26 * do_export() - Export printers to Samba.
27 * do_list_printers() - List available printers.
28 * do_menu() - Show the main menu.
29 * do_set_allowed_users() - Set the allowed/denied users for a queue.
30 * do_set_default() - Set the server default printer/class.
31 * do_set_options() - Configure the default options for a queue.
32 * do_set_sharing() - Set printer-is-shared value.
33 * get_option_value() - Return the value of an option.
34 * get_points() - Get a value in points.
38 * Include necessary headers...
41 #include "cgi-private.h"
42 #include <cups/adminutil.h>
43 #include <cups/file.h>
55 static int current_device
; /* Current device for add/modify */
56 static time_t last_device_time
; /* Last update time for device list */
63 static void choose_device_cb(const char *device_class
,
64 const char *device_id
, const char *device_info
,
65 const char *device_make_and_model
,
66 const char *device_uri
,
67 const char *device_location
,
69 static void do_add_rss_subscription(http_t
*http
);
70 static void do_am_class(http_t
*http
, int modify
);
71 static void do_am_printer(http_t
*http
, int modify
);
72 static void do_cancel_subscription(http_t
*http
);
73 static void do_config_server(http_t
*http
);
74 static void do_delete_class(http_t
*http
);
75 static void do_delete_printer(http_t
*http
);
76 static void do_export(http_t
*http
);
77 static void do_list_printers(http_t
*http
);
78 static void do_menu(http_t
*http
);
79 static void do_set_allowed_users(http_t
*http
);
80 static void do_set_default(http_t
*http
);
81 static void do_set_options(http_t
*http
, int is_class
);
82 static void do_set_sharing(http_t
*http
);
83 static char *get_option_value(ppd_file_t
*ppd
, const char *name
,
84 char *buffer
, size_t bufsize
);
85 static double get_points(double number
, const char *uval
);
89 * 'main()' - Main entry for CGI.
92 int /* O - Exit status */
93 main(int argc
, /* I - Number of command-line arguments */
94 char *argv
[]) /* I - Command-line arguments */
96 http_t
*http
; /* Connection to the server */
97 const char *op
; /* Operation name */
101 * Connect to the HTTP server...
104 fputs("DEBUG: admin.cgi started...\n", stderr
);
106 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
110 perror("ERROR: Unable to connect to cupsd");
111 fprintf(stderr
, "DEBUG: cupsServer()=\"%s\"\n",
112 cupsServer() ? cupsServer() : "(null)");
113 fprintf(stderr
, "DEBUG: ippPort()=%d\n", ippPort());
114 fprintf(stderr
, "DEBUG: cupsEncryption()=%d\n", cupsEncryption());
118 fprintf(stderr
, "DEBUG: http=%p\n", http
);
121 * Set the web interface section...
124 cgiSetVariable("SECTION", "admin");
127 * See if we have form data...
130 if (!cgiInitialize() || !cgiGetVariable("OP"))
133 * Nope, send the administration menu...
136 fputs("DEBUG: No form data, showing main menu...\n", stderr
);
140 else if ((op
= cgiGetVariable("OP")) != NULL
&& cgiIsPOST())
143 * Do the operation...
146 fprintf(stderr
, "DEBUG: op=\"%s\"...\n", op
);
148 if (!strcmp(op
, "set-allowed-users"))
149 do_set_allowed_users(http
);
150 else if (!strcmp(op
, "set-as-default"))
151 do_set_default(http
);
152 else if (!strcmp(op
, "set-sharing"))
153 do_set_sharing(http
);
154 else if (!strcmp(op
, "find-new-printers") ||
155 !strcmp(op
, "list-available-printers"))
156 do_list_printers(http
);
157 else if (!strcmp(op
, "add-class"))
158 do_am_class(http
, 0);
159 else if (!strcmp(op
, "add-printer"))
160 do_am_printer(http
, 0);
161 else if (!strcmp(op
, "modify-class"))
162 do_am_class(http
, 1);
163 else if (!strcmp(op
, "modify-printer"))
164 do_am_printer(http
, 1);
165 else if (!strcmp(op
, "delete-class"))
166 do_delete_class(http
);
167 else if (!strcmp(op
, "delete-printer"))
168 do_delete_printer(http
);
169 else if (!strcmp(op
, "set-class-options"))
170 do_set_options(http
, 1);
171 else if (!strcmp(op
, "set-printer-options"))
172 do_set_options(http
, 0);
173 else if (!strcmp(op
, "config-server"))
174 do_config_server(http
);
175 else if (!strcmp(op
, "export-samba"))
177 else if (!strcmp(op
, "add-rss-subscription"))
178 do_add_rss_subscription(http
);
179 else if (!strcmp(op
, "cancel-subscription"))
180 do_cancel_subscription(http
);
184 * Bad operation code - display an error...
187 cgiStartHTML(cgiText(_("Administration")));
188 cgiCopyTemplateLang("error-op.tmpl");
192 else if (op
&& !strcmp(op
, "redirect"))
194 const char *url
; /* Redirection URL... */
195 char prefix
[1024]; /* URL prefix */
199 snprintf(prefix
, sizeof(prefix
), "https://%s:%s",
200 getenv("SERVER_NAME"), getenv("SERVER_PORT"));
202 snprintf(prefix
, sizeof(prefix
), "http://%s:%s",
203 getenv("SERVER_NAME"), getenv("SERVER_PORT"));
205 fprintf(stderr
, "DEBUG: redirecting with prefix %s!\n", prefix
);
207 if ((url
= cgiGetVariable("URL")) != NULL
)
208 printf("Location: %s%s\n\n", prefix
, url
);
210 printf("Location: %s/admin\n\n", prefix
);
215 * Form data but no operation code - display an error...
218 cgiStartHTML(cgiText(_("Administration")));
219 cgiCopyTemplateLang("error-op.tmpl");
224 * Close the HTTP server connection...
230 * Return with no errors...
238 * 'choose_device_cb()' - Add a device to the device selection page.
243 const char *device_class
, /* I - Class */
244 const char *device_id
, /* I - 1284 device ID */
245 const char *device_info
, /* I - Description */
246 const char *device_make_and_model
, /* I - Make and model */
247 const char *device_uri
, /* I - Device URI */
248 const char *device_location
, /* I - Location */
249 const char *title
) /* I - Page title */
252 * Add the device to the array...
255 cgiSetArray("device_class", current_device
, device_class
);
256 cgiSetArray("device_id", current_device
, device_id
);
257 cgiSetArray("device_info", current_device
, device_info
);
258 cgiSetArray("device_make_and_model", current_device
, device_make_and_model
);
259 cgiSetArray("device_uri", current_device
, device_uri
);
260 cgiSetArray("device_location", current_device
, device_location
);
264 if (time(NULL
) > last_device_time
&& cgiSupportsMultipart())
270 if (!last_device_time
)
274 cgiCopyTemplateLang("choose-device.tmpl");
278 time(&last_device_time
);
284 * 'do_add_rss_subscription()' - Add a RSS subscription.
288 do_add_rss_subscription(http_t
*http
) /* I - HTTP connection */
290 ipp_t
*request
, /* IPP request data */
291 *response
; /* IPP response data */
292 char rss_uri
[1024]; /* RSS notify-recipient URI */
293 int num_events
; /* Number of events */
294 const char *events
[12], /* Subscribed events */
295 *subscription_name
, /* Subscription name */
296 *printer_uri
, /* Printer URI */
297 *ptr
, /* Pointer into name */
298 *user
; /* Username */
299 int max_events
; /* Maximum number of events */
303 * See if we have all of the required information...
306 subscription_name
= cgiGetVariable("SUBSCRIPTION_NAME");
307 printer_uri
= cgiGetVariable("PRINTER_URI");
310 if (cgiGetVariable("EVENT_JOB_CREATED"))
311 events
[num_events
++] = "job-created";
312 if (cgiGetVariable("EVENT_JOB_COMPLETED"))
313 events
[num_events
++] = "job-completed";
314 if (cgiGetVariable("EVENT_JOB_STOPPED"))
315 events
[num_events
++] = "job-stopped";
316 if (cgiGetVariable("EVENT_JOB_CONFIG_CHANGED"))
317 events
[num_events
++] = "job-config-changed";
318 if (cgiGetVariable("EVENT_PRINTER_STOPPED"))
319 events
[num_events
++] = "printer-stopped";
320 if (cgiGetVariable("EVENT_PRINTER_ADDED"))
321 events
[num_events
++] = "printer-added";
322 if (cgiGetVariable("EVENT_PRINTER_MODIFIED"))
323 events
[num_events
++] = "printer-modified";
324 if (cgiGetVariable("EVENT_PRINTER_DELETED"))
325 events
[num_events
++] = "printer-deleted";
326 if (cgiGetVariable("EVENT_SERVER_STARTED"))
327 events
[num_events
++] = "server-started";
328 if (cgiGetVariable("EVENT_SERVER_STOPPED"))
329 events
[num_events
++] = "server-stopped";
330 if (cgiGetVariable("EVENT_SERVER_RESTARTED"))
331 events
[num_events
++] = "server-restarted";
332 if (cgiGetVariable("EVENT_SERVER_AUDIT"))
333 events
[num_events
++] = "server-audit";
335 if ((ptr
= cgiGetVariable("MAX_EVENTS")) != NULL
)
336 max_events
= atoi(ptr
);
340 if (!subscription_name
|| !printer_uri
|| !num_events
||
341 max_events
<= 0 || max_events
> 9999)
344 * Don't have everything we need, so get the available printers
345 * and classes and (re)show the add page...
348 request
= ippNewRequest(CUPS_GET_PRINTERS
);
349 response
= cupsDoRequest(http
, request
, "/");
351 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
355 cgiStartHTML(cgiText(_("Add RSS Subscription")));
357 cgiCopyTemplateLang("add-rss-subscription.tmpl");
364 * Make sure we have a username...
367 if ((user
= getenv("REMOTE_USER")) == NULL
)
369 puts("Status: 401\n");
374 * Validate the subscription name...
377 for (ptr
= subscription_name
; *ptr
; ptr
++)
378 if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' ||
379 *ptr
== '?' || *ptr
== '#')
384 cgiSetVariable("ERROR",
385 cgiText(_("The subscription name may not "
386 "contain spaces, slashes (/), question marks (?), "
387 "or the pound sign (#).")));
388 cgiStartHTML(_("Add RSS Subscription"));
389 cgiCopyTemplateLang("error.tmpl");
395 * Add the subscription...
398 ptr
= subscription_name
+ strlen(subscription_name
) - 4;
399 if (ptr
< subscription_name
|| strcmp(ptr
, ".rss"))
400 httpAssembleURIf(HTTP_URI_CODING_ALL
, rss_uri
, sizeof(rss_uri
), "rss",
401 NULL
, NULL
, 0, "/%s.rss?max_events=%d", subscription_name
,
404 httpAssembleURIf(HTTP_URI_CODING_ALL
, rss_uri
, sizeof(rss_uri
), "rss",
405 NULL
, NULL
, 0, "/%s?max_events=%d", subscription_name
,
408 request
= ippNewRequest(IPP_CREATE_PRINTER_SUBSCRIPTION
);
410 if (!strcasecmp(printer_uri
, "#ALL#"))
411 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
412 NULL
, "ipp://localhost/");
414 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
417 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
420 ippAddString(request
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
421 "notify-recipient-uri", NULL
, rss_uri
);
422 ippAddStrings(request
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
, "notify-events",
423 num_events
, NULL
, events
);
424 ippAddInteger(request
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
425 "notify-lease-duration", 0);
427 ippDelete(cupsDoRequest(http
, request
, "/"));
429 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
431 puts("Status: 401\n");
434 else if (cupsLastError() > IPP_OK_CONFLICT
)
436 cgiStartHTML(_("Add RSS Subscription"));
437 cgiShowIPPError(_("Unable to add RSS subscription:"));
442 * Redirect successful updates back to the admin page...
445 cgiSetVariable("refresh_page", "5;URL=/admin");
446 cgiStartHTML(_("Add RSS Subscription"));
447 cgiCopyTemplateLang("subscription-added.tmpl");
455 * 'do_am_class()' - Add or modify a class.
459 do_am_class(http_t
*http
, /* I - HTTP connection */
460 int modify
) /* I - Modify the printer? */
462 int i
, j
; /* Looping vars */
463 int element
; /* Element number */
464 int num_printers
; /* Number of printers */
465 ipp_t
*request
, /* IPP request */
466 *response
; /* IPP response */
467 ipp_attribute_t
*attr
; /* member-uris attribute */
468 char uri
[HTTP_MAX_URI
]; /* Device or printer URI */
469 const char *name
, /* Pointer to class name */
470 *ptr
; /* Pointer to CGI variable */
471 const char *title
; /* Title of page */
472 static const char * const pattrs
[] = /* Requested printer attributes */
480 title
= cgiText(modify
? _("Modify Class") : _("Add Class"));
481 name
= cgiGetVariable("PRINTER_NAME");
483 if (cgiGetVariable("PRINTER_LOCATION") == NULL
)
486 * Build a CUPS_GET_PRINTERS request, which requires the
487 * following attributes:
490 * attributes-natural-language
493 request
= ippNewRequest(CUPS_GET_PRINTERS
);
496 * Do the request and get back a response...
499 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
502 * Create MEMBER_URIS and MEMBER_NAMES arrays...
505 for (element
= 0, attr
= response
->attrs
;
508 if (attr
->name
&& !strcmp(attr
->name
, "printer-uri-supported"))
510 if ((ptr
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
&&
511 (!name
|| strcasecmp(name
, ptr
+ 1)))
514 * Don't show the current class...
517 cgiSetArray("MEMBER_URIS", element
, attr
->values
[0].string
.text
);
522 for (element
= 0, attr
= response
->attrs
;
525 if (attr
->name
&& !strcmp(attr
->name
, "printer-name"))
527 if (!name
|| strcasecmp(name
, attr
->values
[0].string
.text
))
530 * Don't show the current class...
533 cgiSetArray("MEMBER_NAMES", element
, attr
->values
[0].string
.text
);
538 num_printers
= cgiGetSize("MEMBER_URIS");
548 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
549 * following attributes:
552 * attributes-natural-language
556 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
558 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
559 "localhost", 0, "/classes/%s", name
);
560 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
563 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
564 "requested-attributes",
565 (int)(sizeof(pattrs
) / sizeof(pattrs
[0])),
569 * Do the request and get back a response...
572 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
574 if ((attr
= ippFindAttribute(response
, "member-names",
575 IPP_TAG_NAME
)) != NULL
)
578 * Mark any current members in the class...
581 for (j
= 0; j
< num_printers
; j
++)
582 cgiSetArray("MEMBER_SELECTED", j
, "");
584 for (i
= 0; i
< attr
->num_values
; i
++)
586 for (j
= 0; j
< num_printers
; j
++)
588 if (!strcasecmp(attr
->values
[i
].string
.text
,
589 cgiGetArray("MEMBER_NAMES", j
)))
591 cgiSetArray("MEMBER_SELECTED", j
, "SELECTED");
598 if ((attr
= ippFindAttribute(response
, "printer-info",
599 IPP_TAG_TEXT
)) != NULL
)
600 cgiSetVariable("PRINTER_INFO", attr
->values
[0].string
.text
);
602 if ((attr
= ippFindAttribute(response
, "printer-location",
603 IPP_TAG_TEXT
)) != NULL
)
604 cgiSetVariable("PRINTER_LOCATION", attr
->values
[0].string
.text
);
610 * Update the location and description of an existing printer...
614 cgiCopyTemplateLang("modify-class.tmpl");
619 * Get the name, location, and description for a new printer...
623 cgiCopyTemplateLang("add-class.tmpl");
631 for (ptr
= name
; *ptr
; ptr
++)
632 if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
635 if (*ptr
|| ptr
== name
|| strlen(name
) > 127)
637 cgiSetVariable("ERROR",
638 cgiText(_("The class name may only contain up to "
639 "127 printable characters and may not "
640 "contain spaces, slashes (/), or the "
641 "pound sign (#).")));
643 cgiCopyTemplateLang("error.tmpl");
649 * Build a CUPS_ADD_CLASS request, which requires the following
653 * attributes-natural-language
657 * printer-is-accepting-jobs
662 request
= ippNewRequest(CUPS_ADD_CLASS
);
664 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
665 "localhost", 0, "/classes/%s",
666 cgiGetVariable("PRINTER_NAME"));
667 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
670 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-location",
671 NULL
, cgiGetVariable("PRINTER_LOCATION"));
673 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info",
674 NULL
, cgiGetVariable("PRINTER_INFO"));
676 ippAddBoolean(request
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", 1);
678 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
681 if ((num_printers
= cgiGetSize("MEMBER_URIS")) > 0)
683 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "member-uris",
684 num_printers
, NULL
, NULL
);
685 for (i
= 0; i
< num_printers
; i
++)
686 attr
->values
[i
].string
.text
= strdup(cgiGetArray("MEMBER_URIS", i
));
690 * Do the request and get back a response...
693 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
695 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
697 puts("Status: 401\n");
700 else if (cupsLastError() > IPP_OK_CONFLICT
)
703 cgiShowIPPError(modify
? _("Unable to modify class:") :
704 _("Unable to add class:"));
709 * Redirect successful updates back to the class page...
712 char refresh
[1024]; /* Refresh URL */
714 cgiFormEncode(uri
, name
, sizeof(uri
));
715 snprintf(refresh
, sizeof(refresh
), "5;URL=/admin/?OP=redirect&URL=/classes/%s",
717 cgiSetVariable("refresh_page", refresh
);
722 cgiCopyTemplateLang("class-modified.tmpl");
724 cgiCopyTemplateLang("class-added.tmpl");
732 * 'do_am_printer()' - Add or modify a printer.
736 do_am_printer(http_t
*http
, /* I - HTTP connection */
737 int modify
) /* I - Modify the printer? */
739 int i
; /* Looping var */
740 ipp_attribute_t
*attr
; /* Current attribute */
741 ipp_t
*request
, /* IPP request */
742 *response
, /* IPP response */
743 *oldinfo
; /* Old printer information */
744 const cgi_file_t
*file
; /* Uploaded file, if any */
745 const char *var
; /* CGI variable */
746 char uri
[HTTP_MAX_URI
], /* Device or printer URI */
747 *uriptr
; /* Pointer into URI */
748 int maxrate
; /* Maximum baud rate */
749 char baudrate
[255]; /* Baud rate string */
750 const char *name
, /* Pointer to class name */
751 *ptr
; /* Pointer to CGI variable */
752 const char *title
; /* Title of page */
753 static int baudrates
[] = /* Baud rates */
768 ptr
= cgiGetVariable("DEVICE_URI");
769 fprintf(stderr
, "DEBUG: do_am_printer: DEVICE_URI=\"%s\"\n",
770 ptr
? ptr
: "(null)");
772 title
= cgiText(modify
? _("Modify Printer") : _("Add Printer"));
777 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
778 * following attributes:
781 * attributes-natural-language
785 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
787 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
788 "localhost", 0, "/printers/%s",
789 cgiGetVariable("PRINTER_NAME"));
790 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
794 * Do the request and get back a response...
797 oldinfo
= cupsDoRequest(http
, request
, "/");
806 fprintf(stderr
, "DEBUG: file->tempfile=%s\n", file
->tempfile
);
807 fprintf(stderr
, "DEBUG: file->name=%s\n", file
->name
);
808 fprintf(stderr
, "DEBUG: file->filename=%s\n", file
->filename
);
809 fprintf(stderr
, "DEBUG: file->mimetype=%s\n", file
->mimetype
);
812 if ((name
= cgiGetVariable("PRINTER_NAME")) != NULL
)
814 for (ptr
= name
; *ptr
; ptr
++)
815 if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
818 if (*ptr
|| ptr
== name
|| strlen(name
) > 127)
820 cgiSetVariable("ERROR",
821 cgiText(_("The printer name may only contain up to "
822 "127 printable characters and may not "
823 "contain spaces, slashes (/), or the "
824 "pound sign (#).")));
826 cgiCopyTemplateLang("error.tmpl");
832 if ((var
= cgiGetVariable("DEVICE_URI")) != NULL
)
834 if ((uriptr
= strrchr(var
, '|')) != NULL
)
837 * Extract make and make/model from device URI string...
840 char make
[1024], /* Make string */
841 *makeptr
; /* Pointer into make */
846 strlcpy(make
, uriptr
, sizeof(make
));
848 if ((makeptr
= strchr(make
, ' ')) != NULL
)
850 else if ((makeptr
= strchr(make
, '-')) != NULL
)
852 else if (!strncasecmp(make
, "laserjet", 8) ||
853 !strncasecmp(make
, "deskjet", 7) ||
854 !strncasecmp(make
, "designjet", 9))
856 else if (!strncasecmp(make
, "phaser", 6))
857 strcpy(make
, "Xerox");
858 else if (!strncasecmp(make
, "stylus", 6))
859 strcpy(make
, "Epson");
861 strcpy(make
, "Generic");
863 if (!cgiGetVariable("CURRENT_MAKE"))
864 cgiSetVariable("CURRENT_MAKE", make
);
866 cgiSetVariable("PPD_MAKE", make
);
868 if (!cgiGetVariable("CURRENT_MAKE_AND_MODEL"))
869 cgiSetVariable("CURRENT_MAKE_AND_MODEL", uriptr
);
873 char template[128], /* Template name */
874 *tptr
; /* Pointer into template name */
876 cgiSetVariable("PRINTER_INFO", uriptr
);
878 for (tptr
= template;
879 tptr
< (template + sizeof(template) - 1) && *uriptr
;
881 if (isalnum(*uriptr
& 255) || *uriptr
== '_' || *uriptr
== '-' ||
884 else if ((*uriptr
== ' ' || *uriptr
== '/') && tptr
[-1] != '_')
886 else if (*uriptr
== '?' || *uriptr
== '(')
891 cgiSetVariable("TEMPLATE_NAME", template);
899 * Look for devices so the user can pick something...
902 if ((attr
= ippFindAttribute(oldinfo
, "device-uri", IPP_TAG_URI
)) != NULL
)
904 strlcpy(uri
, attr
->values
[0].string
.text
, sizeof(uri
));
905 if ((uriptr
= strchr(uri
, ':')) != NULL
&& strncmp(uriptr
, "://", 3) == 0)
908 cgiSetVariable("CURRENT_DEVICE_URI", attr
->values
[0].string
.text
);
909 cgiSetVariable("CURRENT_DEVICE_SCHEME", uri
);
913 * Scan for devices for up to 30 seconds, updating the page as we find
917 fputs("DEBUG: Getting list of devices...\n", stderr
);
919 last_device_time
= 0;
921 if (cupsGetDevices(http
, 30, CUPS_INCLUDE_ALL
, CUPS_EXCLUDE_NONE
,
922 (cups_device_cb_t
)choose_device_cb
,
923 (void *)title
) == IPP_OK
)
925 fputs("DEBUG: Got device list!\n", stderr
);
927 if (!cgiSupportsMultipart())
930 * Non-modern browsers that don't support multi-part documents get
931 * everything at the end...
935 cgiCopyTemplateLang("choose-device.tmpl");
942 "ERROR: CUPS-Get-Devices request failed with status %x: %s\n",
943 cupsLastError(), cupsLastErrorString());
944 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
946 puts("Status: 401\n");
952 cgiShowIPPError(modify
? _("Unable to modify printer:") :
953 _("Unable to add printer:"));
960 * Show the final selection page...
963 cgiSetVariable("CUPS_GET_DEVICES_DONE", "1");
965 cgiCopyTemplateLang("choose-device.tmpl");
969 else if (strchr(var
, '/') == NULL
)
971 if ((attr
= ippFindAttribute(oldinfo
, "device-uri", IPP_TAG_URI
)) != NULL
)
974 * Set the current device URI for the form to the old one...
977 if (strncmp(attr
->values
[0].string
.text
, var
, strlen(var
)) == 0)
978 cgiSetVariable("CURRENT_DEVICE_URI", attr
->values
[0].string
.text
);
982 * User needs to set the full URI...
986 cgiCopyTemplateLang("choose-uri.tmpl");
989 else if (!strncmp(var
, "serial:", 7) && !cgiGetVariable("BAUDRATE"))
992 * Need baud rate, parity, etc.
995 if ((var
= strchr(var
, '?')) != NULL
&&
996 strncmp(var
, "?baud=", 6) == 0)
997 maxrate
= atoi(var
+ 6);
1001 for (i
= 0; i
< 10; i
++)
1002 if (baudrates
[i
] > maxrate
)
1006 sprintf(baudrate
, "%d", baudrates
[i
]);
1007 cgiSetArray("BAUDRATES", i
, baudrate
);
1010 cgiStartHTML(title
);
1011 cgiCopyTemplateLang("choose-serial.tmpl");
1014 else if (!name
|| !cgiGetVariable("PRINTER_LOCATION"))
1016 cgiStartHTML(title
);
1021 * Update the location and description of an existing printer...
1026 if ((attr
= ippFindAttribute(oldinfo
, "printer-info",
1027 IPP_TAG_TEXT
)) != NULL
)
1028 cgiSetVariable("PRINTER_INFO", attr
->values
[0].string
.text
);
1030 if ((attr
= ippFindAttribute(oldinfo
, "printer-location",
1031 IPP_TAG_TEXT
)) != NULL
)
1032 cgiSetVariable("PRINTER_LOCATION", attr
->values
[0].string
.text
);
1035 cgiCopyTemplateLang("modify-printer.tmpl");
1040 * Get the name, location, and description for a new printer...
1044 if (!strncmp(var
, "usb:", 4))
1045 cgiSetVariable("printer_is_shared", "1");
1047 #endif /* __APPLE__ */
1048 cgiSetVariable("printer_is_shared", "0");
1050 cgiCopyTemplateLang("add-printer.tmpl");
1061 (!cgiGetVariable("PPD_NAME") || cgiGetVariable("SELECT_MAKE")))
1063 if (modify
&& !cgiGetVariable("SELECT_MAKE"))
1066 * Get the PPD file...
1069 int fd
; /* PPD file */
1070 char filename
[1024]; /* PPD filename */
1071 ppd_file_t
*ppd
; /* PPD information */
1072 char buffer
[1024]; /* Buffer */
1073 int bytes
; /* Number of bytes */
1074 http_status_t get_status
; /* Status of GET */
1077 /* TODO: Use cupsGetFile() API... */
1078 snprintf(uri
, sizeof(uri
), "/printers/%s.ppd", name
);
1080 if (httpGet(http
, uri
))
1083 while ((get_status
= httpUpdate(http
)) == HTTP_CONTINUE
);
1085 if (get_status
!= HTTP_OK
)
1089 fprintf(stderr
, "ERROR: Unable to get PPD file %s: %d - %s\n",
1090 uri
, get_status
, httpStatus(get_status
));
1092 else if ((fd
= cupsTempFd(filename
, sizeof(filename
))) >= 0)
1094 while ((bytes
= httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
1095 write(fd
, buffer
, bytes
);
1099 if ((ppd
= ppdOpenFile(filename
)) != NULL
)
1101 if (ppd
->manufacturer
)
1102 cgiSetVariable("CURRENT_MAKE", ppd
->manufacturer
);
1105 cgiSetVariable("CURRENT_MAKE_AND_MODEL", ppd
->nickname
);
1112 fprintf(stderr
, "ERROR: Unable to open PPD file %s: %s\n",
1113 filename
, ppdErrorString(ppdLastError(&bytes
)));
1121 "ERROR: Unable to create temporary file for PPD file: %s\n",
1127 * Build a CUPS_GET_PPDS request, which requires the following
1130 * attributes-charset
1131 * attributes-natural-language
1135 request
= ippNewRequest(CUPS_GET_PPDS
);
1137 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1138 NULL
, "ipp://localhost/printers/");
1140 if ((var
= cgiGetVariable("CURRENT_MAKE")) == NULL
)
1141 var
= cgiGetVariable("PPD_MAKE");
1142 if (var
&& !cgiGetVariable("SELECT_MAKE"))
1144 const char *make_model
; /* Make and model */
1147 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
1148 "ppd-make", NULL
, var
);
1150 if ((make_model
= cgiGetVariable("CURRENT_MAKE_AND_MODEL")) != NULL
)
1151 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
1152 "ppd-make-and-model", NULL
, make_model
);
1155 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1156 "requested-attributes", NULL
, "ppd-make");
1159 * Do the request and get back a response...
1162 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1165 * Got the list of PPDs, see if the user has selected a make...
1168 if (cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0) == 0)
1171 * No PPD files with this make, try again with all makes...
1174 ippDelete(response
);
1176 request
= ippNewRequest(CUPS_GET_PPDS
);
1178 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1179 NULL
, "ipp://localhost/printers/");
1181 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1182 "requested-attributes", NULL
, "ppd-make");
1184 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1185 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
1187 cgiStartHTML(title
);
1188 cgiCopyTemplateLang("choose-make.tmpl");
1191 else if (!var
|| cgiGetVariable("SELECT_MAKE"))
1193 cgiStartHTML(title
);
1194 cgiCopyTemplateLang("choose-make.tmpl");
1200 * Let the user choose a model...
1203 cgiStartHTML(title
);
1204 cgiSetVariable("CURRENT_MAKE_AND_MODEL",
1205 cgiGetArray("PPD_MAKE_AND_MODEL", 0));
1206 cgiCopyTemplateLang("choose-model.tmpl");
1210 ippDelete(response
);
1214 cgiStartHTML(title
);
1215 cgiShowIPPError(_("Unable to get list of printer drivers:"));
1216 cgiCopyTemplateLang("error.tmpl");
1223 * Build a CUPS_ADD_PRINTER request, which requires the following
1226 * attributes-charset
1227 * attributes-natural-language
1233 * printer-is-accepting-jobs
1238 request
= ippNewRequest(CUPS_ADD_PRINTER
);
1240 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1241 "localhost", 0, "/printers/%s",
1242 cgiGetVariable("PRINTER_NAME"));
1243 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1246 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-location",
1247 NULL
, cgiGetVariable("PRINTER_LOCATION"));
1249 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info",
1250 NULL
, cgiGetVariable("PRINTER_INFO"));
1254 var
= cgiGetVariable("PPD_NAME");
1255 if (strcmp(var
, "__no_change__"))
1256 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "ppd-name",
1260 strlcpy(uri
, cgiGetVariable("DEVICE_URI"), sizeof(uri
));
1263 * Strip make and model from URI...
1266 if ((uriptr
= strrchr(uri
, '|')) != NULL
)
1269 if (!strncmp(uri
, "serial:", 7))
1272 * Update serial port URI to include baud rate, etc.
1275 if ((uriptr
= strchr(uri
, '?')) == NULL
)
1276 uriptr
= uri
+ strlen(uri
);
1278 snprintf(uriptr
, sizeof(uri
) - (uriptr
- uri
),
1279 "?baud=%s+bits=%s+parity=%s+flow=%s",
1280 cgiGetVariable("BAUDRATE"), cgiGetVariable("BITS"),
1281 cgiGetVariable("PARITY"), cgiGetVariable("FLOW"));
1284 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri",
1287 ippAddBoolean(request
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", 1);
1289 var
= cgiGetVariable("printer_is_shared");
1290 ippAddBoolean(request
, IPP_TAG_PRINTER
, "printer-is-shared",
1291 var
&& (!strcmp(var
, "1") || !strcmp(var
, "on")));
1293 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
1297 * Do the request and get back a response...
1301 ippDelete(cupsDoFileRequest(http
, request
, "/admin/", file
->tempfile
));
1303 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1305 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
1307 puts("Status: 401\n");
1310 else if (cupsLastError() > IPP_OK_CONFLICT
)
1312 cgiStartHTML(title
);
1313 cgiShowIPPError(modify
? _("Unable to modify printer:") :
1314 _("Unable to add printer:"));
1319 * Redirect successful updates back to the printer page...
1322 char refresh
[1024]; /* Refresh URL */
1325 cgiFormEncode(uri
, name
, sizeof(uri
));
1327 snprintf(refresh
, sizeof(refresh
),
1328 "5;/admin/?OP=redirect&URL=/printers/%s", uri
);
1330 cgiSetVariable("refresh_page", refresh
);
1332 cgiStartHTML(title
);
1334 cgiCopyTemplateLang("printer-modified.tmpl");
1339 * Set the printer options...
1342 cgiSetVariable("OP", "set-printer-options");
1343 do_set_options(http
, 0);
1356 * 'do_cancel_subscription()' - Cancel a subscription.
1360 do_cancel_subscription(http_t
*http
)/* I - HTTP connection */
1362 ipp_t
*request
; /* IPP request data */
1363 const char *var
, /* Form variable */
1364 *user
; /* Username */
1365 int id
; /* Subscription ID */
1369 * See if we have all of the required information...
1372 if ((var
= cgiGetVariable("NOTIFY_SUBSCRIPTION_ID")) != NULL
)
1379 cgiSetVariable("ERROR", cgiText(_("Bad subscription ID!")));
1380 cgiStartHTML(_("Cancel RSS Subscription"));
1381 cgiCopyTemplateLang("error.tmpl");
1387 * Require a username...
1390 if ((user
= getenv("REMOTE_USER")) == NULL
)
1392 puts("Status: 401\n");
1397 * Cancel the subscription...
1400 request
= ippNewRequest(IPP_CANCEL_SUBSCRIPTION
);
1402 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1403 NULL
, "ipp://localhost/");
1404 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
1405 "notify-subscription-id", id
);
1407 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1410 ippDelete(cupsDoRequest(http
, request
, "/"));
1412 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
1414 puts("Status: 401\n");
1417 else if (cupsLastError() > IPP_OK_CONFLICT
)
1419 cgiStartHTML(_("Cancel RSS Subscription"));
1420 cgiShowIPPError(_("Unable to cancel RSS subscription:"));
1425 * Redirect successful updates back to the admin page...
1428 cgiSetVariable("refresh_page", "5;URL=/admin");
1429 cgiStartHTML(_("Cancel RSS Subscription"));
1430 cgiCopyTemplateLang("subscription-canceled.tmpl");
1438 * 'do_config_server()' - Configure server settings.
1442 do_config_server(http_t
*http
) /* I - HTTP connection */
1444 if (cgiGetVariable("CHANGESETTINGS"))
1447 * Save basic setting changes...
1450 int num_settings
; /* Number of server settings */
1451 cups_option_t
*settings
; /* Server settings */
1452 int advanced
, /* Advanced settings shown? */
1453 changed
; /* Have settings changed? */
1454 const char *debug_logging
, /* DEBUG_LOGGING value */
1455 *remote_admin
, /* REMOTE_ADMIN value */
1456 *remote_any
, /* REMOTE_ANY value */
1458 /* REMOTE_PRINTERS value */
1459 *share_printers
,/* SHARE_PRINTERS value */
1461 /* USER_CANCEL_ANY value */
1462 *browse_web_if
= NULL
,
1463 /* BrowseWebIF value */
1464 *preserve_job_history
= NULL
,
1465 /* PreserveJobHistory value */
1466 *preserve_job_files
= NULL
,
1467 /* PreserveJobFiles value */
1468 *max_clients
= NULL
,
1469 /* MaxClients value */
1472 *max_log_size
= NULL
;
1473 /* MaxLogSize value */
1474 char local_protocols
[255],
1475 /* BrowseLocalProtocols */
1476 remote_protocols
[255];
1477 /* BrowseRemoteProtocols */
1478 const char *current_browse_web_if
,
1479 /* BrowseWebIF value */
1480 *current_preserve_job_history
,
1481 /* PreserveJobHistory value */
1482 *current_preserve_job_files
,
1483 /* PreserveJobFiles value */
1484 *current_max_clients
,
1485 /* MaxClients value */
1488 *current_max_log_size
,
1489 /* MaxLogSize value */
1490 *current_local_protocols
,
1491 /* BrowseLocalProtocols */
1492 *current_remote_protocols
;
1493 /* BrowseRemoteProtocols */
1495 char default_auth_type
[255];
1496 /* DefaultAuthType value */
1497 const char *val
; /* Setting value */
1498 #endif /* HAVE_GSSAPI */
1502 * Get the checkbox values from the form...
1505 debug_logging
= cgiGetVariable("DEBUG_LOGGING") ? "1" : "0";
1506 remote_admin
= cgiGetVariable("REMOTE_ADMIN") ? "1" : "0";
1507 remote_any
= cgiGetVariable("REMOTE_ANY") ? "1" : "0";
1508 remote_printers
= cgiGetVariable("REMOTE_PRINTERS") ? "1" : "0";
1509 share_printers
= cgiGetVariable("SHARE_PRINTERS") ? "1" : "0";
1510 user_cancel_any
= cgiGetVariable("USER_CANCEL_ANY") ? "1" : "0";
1512 advanced
= cgiGetVariable("ADVANCEDSETTINGS") != NULL
;
1516 * Get advanced settings...
1519 browse_web_if
= cgiGetVariable("BROWSE_WEB_IF") ? "Yes" : "No";
1520 preserve_job_history
= cgiGetVariable("PRESERVE_JOB_HISTORY") ? "Yes" : "No";
1521 preserve_job_files
= cgiGetVariable("PRESERVE_JOB_FILES") ? "Yes" : "No";
1522 max_clients
= cgiGetVariable("MAX_CLIENTS");
1523 max_jobs
= cgiGetVariable("MAX_JOBS");
1524 max_log_size
= cgiGetVariable("MAX_LOG_SIZE");
1526 if (!max_clients
|| atoi(max_clients
) <= 0)
1527 max_clients
= "100";
1529 if (!max_jobs
|| atoi(max_jobs
) <= 0)
1532 if (!max_log_size
|| atof(max_log_size
) <= 0.0)
1533 max_log_size
= "1m";
1535 if (cgiGetVariable("BROWSE_LOCAL_CUPS"))
1536 strcpy(local_protocols
, "cups");
1538 local_protocols
[0] = '\0';
1541 if (cgiGetVariable("BROWSE_LOCAL_DNSSD"))
1543 if (local_protocols
[0])
1544 strcat(local_protocols
, " dnssd");
1546 strcat(local_protocols
, "dnssd");
1548 #endif /* HAVE_DNSSD */
1551 if (cgiGetVariable("BROWSE_LOCAL_LDAP"))
1553 if (local_protocols
[0])
1554 strcat(local_protocols
, " ldap");
1556 strcat(local_protocols
, "ldap");
1558 #endif /* HAVE_LDAP */
1561 if (cgiGetVariable("BROWSE_LOCAL_SLP"))
1563 if (local_protocols
[0])
1564 strcat(local_protocols
, " slp");
1566 strcat(local_protocols
, "slp");
1568 #endif /* HAVE_SLP */
1570 if (cgiGetVariable("BROWSE_REMOTE_CUPS"))
1571 strcpy(remote_protocols
, "cups");
1573 remote_protocols
[0] = '\0';
1576 if (cgiGetVariable("BROWSE_REMOTE_LDAP"))
1578 if (remote_protocols
[0])
1579 strcat(remote_protocols
, " ldap");
1581 strcat(remote_protocols
, "ldap");
1583 #endif /* HAVE_LDAP */
1586 if (cgiGetVariable("BROWSE_REMOTE_SLP"))
1588 if (remote_protocols
[0])
1589 strcat(remote_protocols
, " slp");
1591 strcat(remote_protocols
, "slp");
1593 #endif /* HAVE_SLP */
1597 * Get the current server settings...
1600 if (!cupsAdminGetServerSettings(http
, &num_settings
, &settings
))
1602 cgiStartHTML(cgiText(_("Change Settings")));
1603 cgiSetVariable("MESSAGE",
1604 cgiText(_("Unable to change server settings:")));
1605 cgiSetVariable("ERROR", cupsLastErrorString());
1606 cgiCopyTemplateLang("error.tmpl");
1613 * Get authentication settings...
1616 if (cgiGetVariable("KERBEROS"))
1617 strlcpy(default_auth_type
, "Negotiate", sizeof(default_auth_type
));
1620 val
= cupsGetOption("DefaultAuthType", num_settings
, settings
);
1622 if (!val
|| !strcasecmp(val
, "Negotiate"))
1623 strlcpy(default_auth_type
, "Basic", sizeof(default_auth_type
));
1625 strlcpy(default_auth_type
, val
, sizeof(default_auth_type
));
1628 fprintf(stderr
, "DEBUG: DefaultAuthType %s\n", default_auth_type
);
1629 #endif /* HAVE_GSSAPI */
1631 if ((current_browse_web_if
= cupsGetOption("BrowseWebIF", num_settings
,
1633 current_browse_web_if
= "No";
1635 if ((current_preserve_job_history
= cupsGetOption("PreserveJobHistory",
1638 current_preserve_job_history
= "Yes";
1640 if ((current_preserve_job_files
= cupsGetOption("PreserveJobFiles",
1643 current_preserve_job_files
= "No";
1645 if ((current_max_clients
= cupsGetOption("MaxClients", num_settings
,
1647 current_max_clients
= "100";
1649 if ((current_max_jobs
= cupsGetOption("MaxJobs", num_settings
,
1651 current_max_jobs
= "500";
1653 if ((current_max_log_size
= cupsGetOption("MaxLogSize", num_settings
,
1655 current_max_log_size
= "1m";
1657 if ((current_local_protocols
= cupsGetOption("BrowseLocalProtocols",
1660 current_local_protocols
= CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS
;
1662 if ((current_remote_protocols
= cupsGetOption("BrowseRemoteProtocols",
1665 current_remote_protocols
= CUPS_DEFAULT_BROWSE_REMOTE_PROTOCOLS
;
1668 * See if the settings have changed...
1671 changed
= strcmp(debug_logging
, cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
,
1672 num_settings
, settings
)) ||
1673 strcmp(remote_admin
, cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
,
1674 num_settings
, settings
)) ||
1675 strcmp(remote_any
, cupsGetOption(CUPS_SERVER_REMOTE_ANY
,
1676 num_settings
, settings
)) ||
1677 strcmp(remote_printers
, cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
,
1678 num_settings
, settings
)) ||
1679 strcmp(share_printers
, cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
,
1680 num_settings
, settings
)) ||
1682 !cupsGetOption("DefaultAuthType", num_settings
, settings
) ||
1683 strcmp(default_auth_type
, cupsGetOption("DefaultAuthType",
1684 num_settings
, settings
)) ||
1685 #endif /* HAVE_GSSAPI */
1686 strcmp(user_cancel_any
, cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
,
1687 num_settings
, settings
));
1689 if (advanced
&& !changed
)
1690 changed
= strcasecmp(local_protocols
, current_local_protocols
) ||
1691 strcasecmp(remote_protocols
, current_remote_protocols
) ||
1692 strcasecmp(browse_web_if
, current_browse_web_if
) ||
1693 strcasecmp(preserve_job_history
, current_preserve_job_history
) ||
1694 strcasecmp(preserve_job_files
, current_preserve_job_files
) ||
1695 strcasecmp(max_clients
, current_max_clients
) ||
1696 strcasecmp(max_jobs
, current_max_jobs
) ||
1697 strcasecmp(max_log_size
, current_max_log_size
);
1702 * Settings *have* changed, so save the changes...
1705 cupsFreeOptions(num_settings
, settings
);
1708 num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1709 debug_logging
, num_settings
, &settings
);
1710 num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1711 remote_admin
, num_settings
, &settings
);
1712 num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
1713 remote_any
, num_settings
, &settings
);
1714 num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
1715 remote_printers
, num_settings
, &settings
);
1716 num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1717 share_printers
, num_settings
, &settings
);
1718 num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1719 user_cancel_any
, num_settings
, &settings
);
1721 num_settings
= cupsAddOption("DefaultAuthType", default_auth_type
,
1722 num_settings
, &settings
);
1723 #endif /* HAVE_GSSAPI */
1728 * Add advanced settings...
1731 if (strcasecmp(local_protocols
, current_local_protocols
))
1732 num_settings
= cupsAddOption("BrowseLocalProtocols", local_protocols
,
1733 num_settings
, &settings
);
1734 if (strcasecmp(remote_protocols
, current_remote_protocols
))
1735 num_settings
= cupsAddOption("BrowseRemoteProtocols", remote_protocols
,
1736 num_settings
, &settings
);
1737 if (strcasecmp(browse_web_if
, current_browse_web_if
))
1738 num_settings
= cupsAddOption("BrowseWebIF", browse_web_if
,
1739 num_settings
, &settings
);
1740 if (strcasecmp(preserve_job_history
, current_preserve_job_history
))
1741 num_settings
= cupsAddOption("PreserveJobHistory",
1742 preserve_job_history
, num_settings
,
1744 if (strcasecmp(preserve_job_files
, current_preserve_job_files
))
1745 num_settings
= cupsAddOption("PreserveJobFiles", preserve_job_files
,
1746 num_settings
, &settings
);
1747 if (strcasecmp(max_clients
, current_max_clients
))
1748 num_settings
= cupsAddOption("MaxClients", max_clients
, num_settings
,
1750 if (strcasecmp(max_jobs
, current_max_jobs
))
1751 num_settings
= cupsAddOption("MaxJobs", max_jobs
, num_settings
,
1753 if (strcasecmp(max_log_size
, current_max_log_size
))
1754 num_settings
= cupsAddOption("MaxLogSize", max_log_size
, num_settings
,
1758 if (!cupsAdminSetServerSettings(http
, num_settings
, settings
))
1760 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
1762 puts("Status: 401\n");
1766 cgiStartHTML(cgiText(_("Change Settings")));
1767 cgiSetVariable("MESSAGE",
1768 cgiText(_("Unable to change server settings:")));
1769 cgiSetVariable("ERROR", cupsLastErrorString());
1770 cgiCopyTemplateLang("error.tmpl");
1775 cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect&URL=/admin/?ADVANCEDSETTINGS=YES");
1777 cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect");
1778 cgiStartHTML(cgiText(_("Change Settings")));
1779 cgiCopyTemplateLang("restart.tmpl");
1788 cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect");
1789 cgiStartHTML(cgiText(_("Change Settings")));
1790 cgiCopyTemplateLang("norestart.tmpl");
1793 cupsFreeOptions(num_settings
, settings
);
1797 else if (cgiGetVariable("SAVECHANGES") && cgiGetVariable("CUPSDCONF"))
1800 * Save hand-edited config file...
1803 http_status_t status
; /* PUT status */
1804 char tempfile
[1024]; /* Temporary new cupsd.conf */
1805 int tempfd
; /* Temporary file descriptor */
1806 cups_file_t
*temp
; /* Temporary file */
1807 const char *start
, /* Start of line */
1808 *end
; /* End of line */
1812 * Create a temporary file for the new cupsd.conf file...
1815 if ((tempfd
= cupsTempFd(tempfile
, sizeof(tempfile
))) < 0)
1817 cgiStartHTML(cgiText(_("Edit Configuration File")));
1818 cgiSetVariable("MESSAGE", cgiText(_("Unable to create temporary file:")));
1819 cgiSetVariable("ERROR", strerror(errno
));
1820 cgiCopyTemplateLang("error.tmpl");
1827 if ((temp
= cupsFileOpenFd(tempfd
, "w")) == NULL
)
1829 cgiStartHTML(cgiText(_("Edit Configuration File")));
1830 cgiSetVariable("MESSAGE", cgiText(_("Unable to create temporary file:")));
1831 cgiSetVariable("ERROR", strerror(errno
));
1832 cgiCopyTemplateLang("error.tmpl");
1842 * Copy the cupsd.conf text from the form variable...
1845 start
= cgiGetVariable("CUPSDCONF");
1848 if ((end
= strstr(start
, "\r\n")) == NULL
)
1849 if ((end
= strstr(start
, "\n")) == NULL
)
1850 end
= start
+ strlen(start
);
1852 cupsFileWrite(temp
, start
, end
- start
);
1853 cupsFilePutChar(temp
, '\n');
1857 else if (*end
== '\n')
1863 cupsFileClose(temp
);
1866 * Upload the configuration file to the server...
1869 status
= cupsPutFile(http
, "/admin/conf/cupsd.conf", tempfile
);
1871 if (status
== HTTP_UNAUTHORIZED
)
1873 puts("Status: 401\n");
1877 else if (status
!= HTTP_CREATED
)
1879 cgiSetVariable("MESSAGE",
1880 cgiText(_("Unable to upload cupsd.conf file:")));
1881 cgiSetVariable("ERROR", httpStatus(status
));
1883 cgiStartHTML(cgiText(_("Edit Configuration File")));
1884 cgiCopyTemplateLang("error.tmpl");
1888 cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect");
1890 cgiStartHTML(cgiText(_("Edit Configuration File")));
1891 cgiCopyTemplateLang("restart.tmpl");
1900 struct stat info
; /* cupsd.conf information */
1901 cups_file_t
*cupsd
; /* cupsd.conf file */
1902 char *buffer
, /* Buffer for entire file */
1903 *bufptr
, /* Pointer into buffer */
1904 *bufend
; /* End of buffer */
1905 int ch
; /* Character from file */
1906 char filename
[1024]; /* Filename */
1907 const char *server_root
; /* Location of config files */
1911 * Locate the cupsd.conf file...
1914 if ((server_root
= getenv("CUPS_SERVERROOT")) == NULL
)
1915 server_root
= CUPS_SERVERROOT
;
1917 snprintf(filename
, sizeof(filename
), "%s/cupsd.conf", server_root
);
1920 * Figure out the size...
1923 if (stat(filename
, &info
))
1925 cgiStartHTML(cgiText(_("Edit Configuration File")));
1926 cgiSetVariable("MESSAGE",
1927 cgiText(_("Unable to access cupsd.conf file:")));
1928 cgiSetVariable("ERROR", strerror(errno
));
1929 cgiCopyTemplateLang("error.tmpl");
1936 if (info
.st_size
> (1024 * 1024))
1938 cgiStartHTML(cgiText(_("Edit Configuration File")));
1939 cgiSetVariable("MESSAGE",
1940 cgiText(_("Unable to access cupsd.conf file:")));
1941 cgiSetVariable("ERROR",
1942 cgiText(_("Unable to edit cupsd.conf files larger than "
1944 cgiCopyTemplateLang("error.tmpl");
1947 fprintf(stderr
, "ERROR: \"%s\" too large (%ld) to edit!\n", filename
,
1948 (long)info
.st_size
);
1953 * Open the cupsd.conf file...
1956 if ((cupsd
= cupsFileOpen(filename
, "r")) == NULL
)
1959 * Unable to open - log an error...
1962 cgiStartHTML(cgiText(_("Edit Configuration File")));
1963 cgiSetVariable("MESSAGE",
1964 cgiText(_("Unable to access cupsd.conf file:")));
1965 cgiSetVariable("ERROR", strerror(errno
));
1966 cgiCopyTemplateLang("error.tmpl");
1974 * Allocate memory and load the file into a string buffer...
1977 if ((buffer
= calloc(1, info
.st_size
+ 1)) != NULL
)
1979 cupsFileRead(cupsd
, buffer
, info
.st_size
);
1980 cgiSetVariable("CUPSDCONF", buffer
);
1984 cupsFileClose(cupsd
);
1987 * Then get the default cupsd.conf file and put that into a string as
1991 strlcat(filename
, ".default", sizeof(filename
));
1993 if (!stat(filename
, &info
) && info
.st_size
< (1024 * 1024) &&
1994 (cupsd
= cupsFileOpen(filename
, "r")) != NULL
)
1996 if ((buffer
= calloc(1, 2 * info
.st_size
+ 1)) != NULL
)
1998 bufend
= buffer
+ 2 * info
.st_size
- 1;
2000 for (bufptr
= buffer
;
2001 bufptr
< bufend
&& (ch
= cupsFileGetChar(cupsd
)) != EOF
;)
2003 if (ch
== '\\' || ch
== '\"')
2008 else if (ch
== '\n')
2013 else if (ch
== '\t')
2024 cgiSetVariable("CUPSDCONF_DEFAULT", buffer
);
2028 cupsFileClose(cupsd
);
2032 * Show the current config file...
2035 cgiStartHTML(cgiText(_("Edit Configuration File")));
2037 cgiCopyTemplateLang("edit-config.tmpl");
2045 * 'do_delete_class()' - Delete a class.
2049 do_delete_class(http_t
*http
) /* I - HTTP connection */
2051 ipp_t
*request
; /* IPP request */
2052 char uri
[HTTP_MAX_URI
]; /* Job URI */
2053 const char *pclass
; /* Printer class name */
2057 * Get form variables...
2060 if (cgiGetVariable("CONFIRM") == NULL
)
2062 cgiStartHTML(cgiText(_("Delete Class")));
2063 cgiCopyTemplateLang("class-confirm.tmpl");
2068 if ((pclass
= cgiGetVariable("PRINTER_NAME")) != NULL
)
2069 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2070 "localhost", 0, "/classes/%s", pclass
);
2073 cgiStartHTML(cgiText(_("Delete Class")));
2074 cgiSetVariable("ERROR", cgiText(_("Missing form variable!")));
2075 cgiCopyTemplateLang("error.tmpl");
2081 * Build a CUPS_DELETE_CLASS request, which requires the following
2084 * attributes-charset
2085 * attributes-natural-language
2089 request
= ippNewRequest(CUPS_DELETE_CLASS
);
2091 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2095 * Do the request and get back a response...
2098 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
2101 * Show the results...
2104 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
2106 puts("Status: 401\n");
2109 else if (cupsLastError() <= IPP_OK_CONFLICT
)
2112 * Redirect successful updates back to the classes page...
2115 cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect&URL=/classes");
2118 cgiStartHTML(cgiText(_("Delete Class")));
2120 if (cupsLastError() > IPP_OK_CONFLICT
)
2121 cgiShowIPPError(_("Unable to delete class:"));
2123 cgiCopyTemplateLang("class-deleted.tmpl");
2130 * 'do_delete_printer()' - Delete a printer.
2134 do_delete_printer(http_t
*http
) /* I - HTTP connection */
2136 ipp_t
*request
; /* IPP request */
2137 char uri
[HTTP_MAX_URI
]; /* Job URI */
2138 const char *printer
; /* Printer printer name */
2142 * Get form variables...
2145 if (cgiGetVariable("CONFIRM") == NULL
)
2147 cgiStartHTML(cgiText(_("Delete Printer")));
2148 cgiCopyTemplateLang("printer-confirm.tmpl");
2153 if ((printer
= cgiGetVariable("PRINTER_NAME")) != NULL
)
2154 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2155 "localhost", 0, "/printers/%s", printer
);
2158 cgiStartHTML(cgiText(_("Delete Printer")));
2159 cgiSetVariable("ERROR", cgiText(_("Missing form variable!")));
2160 cgiCopyTemplateLang("error.tmpl");
2166 * Build a CUPS_DELETE_PRINTER request, which requires the following
2169 * attributes-charset
2170 * attributes-natural-language
2174 request
= ippNewRequest(CUPS_DELETE_PRINTER
);
2176 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2180 * Do the request and get back a response...
2183 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
2186 * Show the results...
2189 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
2191 puts("Status: 401\n");
2194 else if (cupsLastError() <= IPP_OK_CONFLICT
)
2197 * Redirect successful updates back to the printers page...
2200 cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect&URL=/printers");
2203 cgiStartHTML(cgiText(_("Delete Printer")));
2205 if (cupsLastError() > IPP_OK_CONFLICT
)
2206 cgiShowIPPError(_("Unable to delete printer:"));
2208 cgiCopyTemplateLang("printer-deleted.tmpl");
2215 * 'do_export()' - Export printers to Samba.
2219 do_export(http_t
*http
) /* I - HTTP connection */
2221 int i
, j
; /* Looping vars */
2222 ipp_t
*request
, /* IPP request */
2223 *response
; /* IPP response */
2224 const char *username
, /* Samba username */
2225 *password
, /* Samba password */
2226 *export_all
; /* Export all printers? */
2227 int export_count
, /* Number of printers to export */
2228 printer_count
; /* Number of available printers */
2229 const char *name
, /* What name to pull */
2230 *dest
; /* Current destination */
2231 char ppd
[1024]; /* PPD file */
2238 username
= cgiGetVariable("USERNAME");
2239 password
= cgiGetVariable("PASSWORD");
2240 export_all
= cgiGetVariable("EXPORT_ALL");
2241 export_count
= cgiGetSize("EXPORT_NAME");
2244 * Get list of available printers...
2247 cgiSetSize("PRINTER_NAME", 0);
2248 cgiSetSize("PRINTER_EXPORT", 0);
2250 request
= ippNewRequest(CUPS_GET_PRINTERS
);
2252 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
2255 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
2256 "printer-type-mask", CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
2257 CUPS_PRINTER_IMPLICIT
);
2259 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2260 "requested-attributes", NULL
, "printer-name");
2262 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
2264 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
2265 ippDelete(response
);
2269 printer_count
= cgiGetSize("PRINTER_NAME");
2271 for (i
= 0; i
< printer_count
; i
++)
2273 dest
= cgiGetArray("PRINTER_NAME", i
);
2275 for (j
= 0; j
< export_count
; j
++)
2276 if (!strcasecmp(dest
, cgiGetArray("EXPORT_NAME", j
)))
2279 cgiSetArray("PRINTER_EXPORT", i
, j
< export_count
? "Y" : "");
2285 * Export or get the printers to export...
2288 if (username
&& *username
&& password
&& *password
&&
2289 (export_all
|| export_count
> 0))
2295 fputs("DEBUG: Export printers...\n", stderr
);
2299 name
= "PRINTER_NAME";
2300 export_count
= cgiGetSize("PRINTER_NAME");
2303 name
= "EXPORT_NAME";
2305 for (i
= 0; i
< export_count
; i
++)
2307 dest
= cgiGetArray(name
, i
);
2309 if (!cupsAdminCreateWindowsPPD(http
, dest
, ppd
, sizeof(ppd
)))
2312 j
= cupsAdminExportSamba(dest
, ppd
, "localhost", username
, password
,
2321 if (i
< export_count
)
2322 cgiSetVariable("ERROR", cupsLastErrorString());
2325 cgiStartHTML(cgiText(_("Export Printers to Samba")));
2326 cgiCopyTemplateLang("samba-exported.tmpl");
2331 else if (username
&& !*username
)
2332 cgiSetVariable("ERROR",
2333 cgiText(_("A Samba username is required to export "
2334 "printer drivers!")));
2335 else if (username
&& (!password
|| !*password
))
2336 cgiSetVariable("ERROR",
2337 cgiText(_("A Samba password is required to export "
2338 "printer drivers!")));
2344 cgiStartHTML(cgiText(_("Export Printers to Samba")));
2345 cgiCopyTemplateLang("samba-export.tmpl");
2351 * 'do_list_printers()' - List available printers.
2355 do_list_printers(http_t
*http
) /* I - HTTP connection */
2357 ipp_t
*request
, /* IPP request */
2358 *response
; /* IPP response */
2359 ipp_attribute_t
*attr
; /* IPP attribute */
2362 cgiStartHTML(cgiText(_("List Available Printers")));
2366 * Get the list of printers and their devices...
2369 request
= ippNewRequest(CUPS_GET_PRINTERS
);
2371 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2372 "requested-attributes", NULL
, "device-uri");
2374 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type",
2375 CUPS_PRINTER_LOCAL
);
2376 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type-mask",
2377 CUPS_PRINTER_LOCAL
);
2379 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
2382 * Got the printer list, now load the devices...
2385 int i
; /* Looping var */
2386 cups_array_t
*printer_devices
; /* Printer devices for local printers */
2387 char *printer_device
; /* Current printer device */
2391 * Allocate an array and copy the device strings...
2394 printer_devices
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
2396 for (attr
= ippFindAttribute(response
, "device-uri", IPP_TAG_URI
);
2398 attr
= ippFindNextAttribute(response
, "device-uri", IPP_TAG_URI
))
2400 cupsArrayAdd(printer_devices
, strdup(attr
->values
[0].string
.text
));
2404 * Free the printer list and get the device list...
2407 ippDelete(response
);
2409 request
= ippNewRequest(CUPS_GET_DEVICES
);
2411 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
2414 * Got the device list, let's parse it...
2417 const char *device_uri
, /* device-uri attribute value */
2418 *device_make_and_model
, /* device-make-and-model value */
2419 *device_info
; /* device-info value */
2422 for (i
= 0, attr
= response
->attrs
; attr
; attr
= attr
->next
)
2425 * Skip leading attributes until we hit a device...
2428 while (attr
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
2435 * Pull the needed attributes from this device...
2439 device_make_and_model
= NULL
;
2442 while (attr
&& attr
->group_tag
== IPP_TAG_PRINTER
)
2444 if (!strcmp(attr
->name
, "device-info") &&
2445 attr
->value_tag
== IPP_TAG_TEXT
)
2446 device_info
= attr
->values
[0].string
.text
;
2448 if (!strcmp(attr
->name
, "device-make-and-model") &&
2449 attr
->value_tag
== IPP_TAG_TEXT
)
2450 device_make_and_model
= attr
->values
[0].string
.text
;
2452 if (!strcmp(attr
->name
, "device-uri") &&
2453 attr
->value_tag
== IPP_TAG_URI
)
2454 device_uri
= attr
->values
[0].string
.text
;
2460 * See if we have everything needed...
2463 if (device_info
&& device_make_and_model
&& device_uri
&&
2464 strcasecmp(device_make_and_model
, "unknown") &&
2465 strchr(device_uri
, ':'))
2468 * Yes, now see if there is already a printer for this
2472 if (!cupsArrayFind(printer_devices
, (void *)device_uri
))
2475 * Not found, so it must be a new printer...
2478 char option
[1024], /* Form variables for this device */
2479 *option_ptr
; /* Pointer into string */
2480 const char *ptr
; /* Pointer into device string */
2484 * Format the printer name variable for this device...
2486 * We use the device-info string first, then device-uri,
2487 * and finally device-make-and-model to come up with a
2491 if (strncasecmp(device_info
, "unknown", 7))
2493 else if ((ptr
= strstr(device_uri
, "://")) != NULL
)
2496 ptr
= device_make_and_model
;
2498 for (option_ptr
= option
;
2499 option_ptr
< (option
+ sizeof(option
) - 1) && *ptr
;
2501 if (isalnum(*ptr
& 255) || *ptr
== '_' || *ptr
== '-' ||
2503 *option_ptr
++ = *ptr
;
2504 else if ((*ptr
== ' ' || *ptr
== '/') && option_ptr
[-1] != '_')
2505 *option_ptr
++ = '_';
2506 else if (*ptr
== '?' || *ptr
== '(')
2511 cgiSetArray("TEMPLATE_NAME", i
, option
);
2514 * Finally, set the form variables for this printer...
2517 cgiSetArray("device_info", i
, device_info
);
2518 cgiSetArray("device_make_and_model", i
, device_make_and_model
);
2519 cgiSetArray("device_uri", i
, device_uri
);
2528 ippDelete(response
);
2531 * Free the device list...
2534 for (printer_device
= (char *)cupsArrayFirst(printer_devices
);
2536 printer_device
= (char *)cupsArrayNext(printer_devices
))
2537 free(printer_device
);
2539 cupsArrayDelete(printer_devices
);
2544 * Finally, show the printer list...
2547 cgiCopyTemplateLang("list-available-printers.tmpl");
2554 * 'do_menu()' - Show the main menu.
2558 do_menu(http_t
*http
) /* I - HTTP connection */
2560 int num_settings
; /* Number of server settings */
2561 cups_option_t
*settings
; /* Server settings */
2562 const char *val
; /* Setting value */
2563 char filename
[1024]; /* Temporary filename */
2564 const char *datadir
; /* Location of data files */
2565 ipp_t
*request
, /* IPP request */
2566 *response
; /* IPP response */
2570 * Get the current server settings...
2573 if (!cupsAdminGetServerSettings(http
, &num_settings
, &settings
))
2575 cgiSetVariable("SETTINGS_MESSAGE",
2576 cgiText(_("Unable to open cupsd.conf file:")));
2577 cgiSetVariable("SETTINGS_ERROR", cupsLastErrorString());
2580 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, num_settings
,
2581 settings
)) != NULL
&& atoi(val
))
2582 cgiSetVariable("DEBUG_LOGGING", "CHECKED");
2584 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, num_settings
,
2585 settings
)) != NULL
&& atoi(val
))
2586 cgiSetVariable("REMOTE_ADMIN", "CHECKED");
2588 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, num_settings
,
2589 settings
)) != NULL
&& atoi(val
))
2590 cgiSetVariable("REMOTE_ANY", "CHECKED");
2592 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
, num_settings
,
2593 settings
)) != NULL
&& atoi(val
))
2594 cgiSetVariable("REMOTE_PRINTERS", "CHECKED");
2596 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, num_settings
,
2597 settings
)) != NULL
&& atoi(val
))
2598 cgiSetVariable("SHARE_PRINTERS", "CHECKED");
2600 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, num_settings
,
2601 settings
)) != NULL
&& atoi(val
))
2602 cgiSetVariable("USER_CANCEL_ANY", "CHECKED");
2605 cgiSetVariable("HAVE_GSSAPI", "1");
2607 if ((val
= cupsGetOption("DefaultAuthType", num_settings
,
2608 settings
)) != NULL
&& !strcasecmp(val
, "Negotiate"))
2609 cgiSetVariable("KERBEROS", "CHECKED");
2610 #endif /* HAVE_GSSAPI */
2613 cgiSetVariable("HAVE_DNSSD", "1");
2614 #endif /* HAVE_DNSSD */
2617 cgiSetVariable("HAVE_LDAP", "1");
2618 #endif /* HAVE_LDAP */
2621 cgiSetVariable("HAVE_LIBSLP", "1");
2622 #endif /* HAVE_LIBSLP */
2624 if ((val
= cupsGetOption("BrowseRemoteProtocols", num_settings
,
2626 if ((val
= cupsGetOption("BrowseProtocols", num_settings
,
2628 val
= CUPS_DEFAULT_BROWSE_REMOTE_PROTOCOLS
;
2630 if (strstr(val
, "cups") || strstr(val
, "CUPS"))
2631 cgiSetVariable("BROWSE_REMOTE_CUPS", "CHECKED");
2633 if (strstr(val
, "ldap") || strstr(val
, "LDAP"))
2634 cgiSetVariable("BROWSE_REMOTE_LDAP", "CHECKED");
2636 if (strstr(val
, "slp") || strstr(val
, "SLP"))
2637 cgiSetVariable("BROWSE_REMOTE_SLP", "CHECKED");
2639 if ((val
= cupsGetOption("BrowseLocalProtocols", num_settings
,
2641 if ((val
= cupsGetOption("BrowseProtocols", num_settings
,
2643 val
= CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS
;
2645 if (strstr(val
, "cups") || strstr(val
, "CUPS"))
2646 cgiSetVariable("BROWSE_LOCAL_CUPS", "CHECKED");
2648 if (strstr(val
, "dnssd") || strstr(val
, "DNSSD") ||
2649 strstr(val
, "dns-sd") || strstr(val
, "DNS-SD") ||
2650 strstr(val
, "bonjour") || strstr(val
, "BONJOUR"))
2651 cgiSetVariable("BROWSE_LOCAL_DNSSD", "CHECKED");
2653 if (strstr(val
, "ldap") || strstr(val
, "LDAP"))
2654 cgiSetVariable("BROWSE_LOCAL_LDAP", "CHECKED");
2656 if (strstr(val
, "slp") || strstr(val
, "SLP"))
2657 cgiSetVariable("BROWSE_LOCAL_SLP", "CHECKED");
2659 if ((val
= cupsGetOption("BrowseWebIF", num_settings
,
2663 if (!strcasecmp(val
, "yes") || !strcasecmp(val
, "on") ||
2664 !strcasecmp(val
, "true"))
2665 cgiSetVariable("BROWSE_WEB_IF", "CHECKED");
2667 if ((val
= cupsGetOption("PreserveJobHistory", num_settings
,
2671 if (!strcasecmp(val
, "yes") || !strcasecmp(val
, "on") ||
2672 !strcasecmp(val
, "true"))
2674 cgiSetVariable("PRESERVE_JOB_HISTORY", "CHECKED");
2676 if ((val
= cupsGetOption("PreserveJobFiles", num_settings
,
2680 if (!strcasecmp(val
, "yes") || !strcasecmp(val
, "on") ||
2681 !strcasecmp(val
, "true"))
2682 cgiSetVariable("PRESERVE_JOB_FILES", "CHECKED");
2685 if ((val
= cupsGetOption("MaxClients", num_settings
, settings
)) == NULL
)
2688 cgiSetVariable("MAX_CLIENTS", val
);
2690 if ((val
= cupsGetOption("MaxJobs", num_settings
, settings
)) == NULL
)
2693 cgiSetVariable("MAX_JOBS", val
);
2695 if ((val
= cupsGetOption("MaxLogSize", num_settings
, settings
)) == NULL
)
2698 cgiSetVariable("MAX_LOG_SIZE", val
);
2700 cupsFreeOptions(num_settings
, settings
);
2703 * See if Samba and the Windows drivers are installed...
2706 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
2707 datadir
= CUPS_DATADIR
;
2709 snprintf(filename
, sizeof(filename
), "%s/drivers/pscript5.dll", datadir
);
2710 if (!access(filename
, R_OK
))
2713 * Found Windows 2000 driver file, see if we have smbclient and
2717 if (cupsFileFind("smbclient", getenv("PATH"), 1, filename
,
2718 sizeof(filename
)) &&
2719 cupsFileFind("rpcclient", getenv("PATH"), 1, filename
,
2721 cgiSetVariable("HAVE_SAMBA", "Y");
2724 if (!cupsFileFind("smbclient", getenv("PATH"), 1, filename
,
2726 fputs("ERROR: smbclient not found!\n", stderr
);
2728 if (!cupsFileFind("rpcclient", getenv("PATH"), 1, filename
,
2730 fputs("ERROR: rpcclient not found!\n", stderr
);
2740 request
= ippNewRequest(IPP_GET_SUBSCRIPTIONS
);
2742 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2743 NULL
, "ipp://localhost/");
2745 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
2747 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
2748 ippDelete(response
);
2752 * Finally, show the main menu template...
2755 cgiStartHTML(cgiText(_("Administration")));
2757 cgiCopyTemplateLang("admin.tmpl");
2764 * 'do_set_allowed_users()' - Set the allowed/denied users for a queue.
2768 do_set_allowed_users(http_t
*http
) /* I - HTTP connection */
2770 int i
; /* Looping var */
2771 ipp_t
*request
, /* IPP request */
2772 *response
; /* IPP response */
2773 char uri
[HTTP_MAX_URI
]; /* Printer URI */
2774 const char *printer
, /* Printer name (purge-jobs) */
2775 *is_class
, /* Is a class? */
2776 *users
, /* List of users or groups */
2777 *type
; /* Allow/deny type */
2778 int num_users
; /* Number of users */
2779 char *ptr
, /* Pointer into users string */
2780 *end
, /* Pointer to end of users string */
2781 quote
; /* Quote character */
2782 ipp_attribute_t
*attr
; /* Attribute */
2783 static const char * const attrs
[] = /* Requested attributes */
2785 "requesting-user-name-allowed",
2786 "requesting-user-name-denied"
2790 is_class
= cgiGetVariable("IS_CLASS");
2791 printer
= cgiGetVariable("PRINTER_NAME");
2795 cgiSetVariable("ERROR", cgiText(_("Missing form variable!")));
2796 cgiStartHTML(cgiText(_("Set Allowed Users")));
2797 cgiCopyTemplateLang("error.tmpl");
2802 users
= cgiGetVariable("users");
2803 type
= cgiGetVariable("type");
2805 if (!users
|| !type
||
2806 (strcmp(type
, "requesting-user-name-allowed") &&
2807 strcmp(type
, "requesting-user-name-denied")))
2810 * Build a Get-Printer-Attributes request, which requires the following
2813 * attributes-charset
2814 * attributes-natural-language
2816 * requested-attributes
2819 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
2821 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2822 "localhost", 0, is_class
? "/classes/%s" : "/printers/%s",
2824 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2827 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2828 "requested-attributes",
2829 (int)(sizeof(attrs
) / sizeof(attrs
[0])), NULL
, attrs
);
2832 * Do the request and get back a response...
2835 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
2837 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
2839 ippDelete(response
);
2842 cgiStartHTML(cgiText(_("Set Allowed Users")));
2844 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
2846 puts("Status: 401\n");
2849 else if (cupsLastError() > IPP_OK_CONFLICT
)
2850 cgiShowIPPError(_("Unable to get printer attributes:"));
2852 cgiCopyTemplateLang("users.tmpl");
2859 * Save the changes...
2862 for (num_users
= 0, ptr
= (char *)users
; *ptr
; num_users
++)
2865 * Skip whitespace and commas...
2868 while (*ptr
== ',' || isspace(*ptr
& 255))
2874 if (*ptr
== '\'' || *ptr
== '\"')
2877 * Scan quoted name...
2882 for (end
= ptr
; *end
; end
++)
2889 * Scan space or comma-delimited name...
2892 for (end
= ptr
; *end
; end
++)
2893 if (isspace(*end
& 255) || *end
== ',')
2898 * Advance to the next name...
2905 * Build a CUPS-Add-Printer/Class request, which requires the following
2908 * attributes-charset
2909 * attributes-natural-language
2911 * requesting-user-name-{allowed,denied}
2914 request
= ippNewRequest(is_class
? CUPS_ADD_CLASS
: CUPS_ADD_PRINTER
);
2916 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2917 "localhost", 0, is_class
? "/classes/%s" : "/printers/%s",
2919 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2923 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2924 "requesting-user-name-allowed", NULL
, "all");
2927 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2928 type
, num_users
, NULL
, NULL
);
2930 for (i
= 0, ptr
= (char *)users
; *ptr
; i
++)
2933 * Skip whitespace and commas...
2936 while (*ptr
== ',' || isspace(*ptr
& 255))
2942 if (*ptr
== '\'' || *ptr
== '\"')
2945 * Scan quoted name...
2950 for (end
= ptr
; *end
; end
++)
2957 * Scan space or comma-delimited name...
2960 for (end
= ptr
; *end
; end
++)
2961 if (isspace(*end
& 255) || *end
== ',')
2966 * Terminate the name...
2976 attr
->values
[i
].string
.text
= strdup(ptr
);
2979 * Advance to the next name...
2987 * Do the request and get back a response...
2990 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
2992 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
2994 puts("Status: 401\n");
2997 else if (cupsLastError() > IPP_OK_CONFLICT
)
2999 cgiStartHTML(cgiText(_("Set Allowed Users")));
3000 cgiShowIPPError(_("Unable to change printer:"));
3005 * Redirect successful updates back to the printer page...
3008 char url
[1024], /* Printer/class URL */
3009 refresh
[1024]; /* Refresh URL */
3012 cgiRewriteURL(uri
, url
, sizeof(url
), NULL
);
3013 cgiFormEncode(uri
, url
, sizeof(uri
));
3014 snprintf(refresh
, sizeof(refresh
), "5;URL=/admin/?OP=redirect&URL=%s",
3016 cgiSetVariable("refresh_page", refresh
);
3018 cgiStartHTML(cgiText(_("Set Allowed Users")));
3020 cgiCopyTemplateLang(is_class
? "class-modified.tmpl" :
3021 "printer-modified.tmpl");
3030 * 'do_set_default()' - Set the server default printer/class.
3034 do_set_default(http_t
*http
) /* I - HTTP connection */
3036 const char *title
; /* Page title */
3037 ipp_t
*request
; /* IPP request */
3038 char uri
[HTTP_MAX_URI
]; /* Printer URI */
3039 const char *printer
, /* Printer name (purge-jobs) */
3040 *is_class
; /* Is a class? */
3043 is_class
= cgiGetVariable("IS_CLASS");
3044 printer
= cgiGetVariable("PRINTER_NAME");
3045 title
= cgiText(_("Set As Server Default"));
3049 cgiSetVariable("ERROR", cgiText(_("Missing form variable!")));
3050 cgiStartHTML(title
);
3051 cgiCopyTemplateLang("error.tmpl");
3057 * Build a printer request, which requires the following
3060 * attributes-charset
3061 * attributes-natural-language
3065 request
= ippNewRequest(CUPS_SET_DEFAULT
);
3067 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3068 "localhost", 0, is_class
? "/classes/%s" : "/printers/%s",
3070 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
3074 * Do the request and get back a response...
3077 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
3079 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
3081 puts("Status: 401\n");
3084 else if (cupsLastError() > IPP_OK_CONFLICT
)
3086 cgiStartHTML(title
);
3087 cgiShowIPPError(_("Unable to set server default:"));
3092 * Redirect successful updates back to the printer page...
3095 char url
[1024], /* Printer/class URL */
3096 refresh
[1024]; /* Refresh URL */
3099 cgiRewriteURL(uri
, url
, sizeof(url
), NULL
);
3100 cgiFormEncode(uri
, url
, sizeof(uri
));
3101 snprintf(refresh
, sizeof(refresh
), "5;URL=/admin/?OP=redirect&URL=%s", uri
);
3102 cgiSetVariable("refresh_page", refresh
);
3104 cgiStartHTML(title
);
3105 cgiCopyTemplateLang("printer-default.tmpl");
3113 * 'do_set_options()' - Configure the default options for a queue.
3117 do_set_options(http_t
*http
, /* I - HTTP connection */
3118 int is_class
) /* I - Set options for class? */
3120 int i
, j
, k
, m
; /* Looping vars */
3121 int have_options
; /* Have options? */
3122 ipp_t
*request
, /* IPP request */
3123 *response
; /* IPP response */
3124 ipp_attribute_t
*attr
; /* IPP attribute */
3125 char uri
[HTTP_MAX_URI
]; /* Job URI */
3126 const char *var
; /* Variable value */
3127 const char *printer
; /* Printer printer name */
3128 const char *filename
; /* PPD filename */
3129 char tempfile
[1024]; /* Temporary filename */
3130 cups_file_t
*in
, /* Input file */
3131 *out
; /* Output file */
3132 char line
[1024], /* Line from PPD file */
3133 value
[1024], /* Option value */
3134 keyword
[1024], /* Keyword from Default line */
3135 *keyptr
; /* Pointer into keyword... */
3136 ppd_file_t
*ppd
; /* PPD file */
3137 ppd_group_t
*group
; /* Option group */
3138 ppd_option_t
*option
; /* Option */
3139 ppd_coption_t
*coption
; /* Custom option */
3140 ppd_cparam_t
*cparam
; /* Custom parameter */
3141 ppd_attr_t
*ppdattr
; /* PPD attribute */
3142 const char *title
; /* Page title */
3145 title
= cgiText(is_class
? _("Set Class Options") : _("Set Printer Options"));
3147 fprintf(stderr
, "DEBUG: do_set_options(http=%p, is_class=%d)\n", http
,
3151 * Get the printer name...
3154 if ((printer
= cgiGetVariable("PRINTER_NAME")) != NULL
)
3155 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3156 "localhost", 0, is_class
? "/classes/%s" : "/printers/%s",
3160 cgiSetVariable("ERROR", cgiText(_("Missing form variable!")));
3161 cgiStartHTML(title
);
3162 cgiCopyTemplateLang("error.tmpl");
3167 fprintf(stderr
, "DEBUG: printer=\"%s\", uri=\"%s\"...\n", printer
, uri
);
3170 * If the user clicks on the Auto-Configure button, send an AutoConfigure
3171 * command file to the printer...
3174 if (cgiGetVariable("AUTOCONFIGURE"))
3176 cgiPrintCommand(http
, printer
, "AutoConfigure", "Set Default Options");
3181 * Get the PPD file...
3187 filename
= cupsGetPPD2(http
, printer
);
3191 fprintf(stderr
, "DEBUG: Got PPD file: \"%s\"\n", filename
);
3193 if ((ppd
= ppdOpenFile(filename
)) == NULL
)
3195 cgiSetVariable("ERROR", ppdErrorString(ppdLastError(&i
)));
3196 cgiSetVariable("MESSAGE", cgiText(_("Unable to open PPD file:")));
3197 cgiStartHTML(title
);
3198 cgiCopyTemplateLang("error.tmpl");
3205 fputs("DEBUG: No PPD file\n", stderr
);
3209 if (cgiGetVariable("job_sheets_start") != NULL
||
3210 cgiGetVariable("job_sheets_end") != NULL
)
3217 ppdMarkDefaults(ppd
);
3219 for (option
= ppdFirstOption(ppd
);
3221 option
= ppdNextOption(ppd
))
3222 if ((var
= cgiGetVariable(option
->keyword
)) != NULL
)
3225 ppdMarkOption(ppd
, option
->keyword
, var
);
3229 if (!have_options
|| ppdConflicts(ppd
))
3232 * Show the options to the user...
3235 fputs("DEBUG: Showing options...\n", stderr
);
3238 * Show auto-configure button if supported...
3243 if (ppd
->num_filters
== 0 ||
3244 ((ppdattr
= ppdFindAttr(ppd
, "cupsCommands", NULL
)) != NULL
&&
3245 ppdattr
->value
&& strstr(ppdattr
->value
, "AutoConfigure")))
3246 cgiSetVariable("HAVE_AUTOCONFIGURE", "YES");
3249 for (i
= 0; i
< ppd
->num_filters
; i
++)
3250 if (!strncmp(ppd
->filters
[i
], "application/vnd.cups-postscript", 31))
3252 cgiSetVariable("HAVE_AUTOCONFIGURE", "YES");
3259 * Get the printer attributes...
3262 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
3264 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3265 "localhost", 0, "/printers/%s", printer
);
3266 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
3269 response
= cupsDoRequest(http
, request
, "/");
3272 * List the groups used as "tabs"...
3279 for (group
= ppd
->groups
;
3280 i
< ppd
->num_groups
;
3283 cgiSetArray("GROUP_ID", i
, group
->name
);
3285 if (!strcmp(group
->name
, "InstallableOptions"))
3286 cgiSetArray("GROUP", i
, cgiText(_("Options Installed")));
3288 cgiSetArray("GROUP", i
, group
->text
);
3292 if (ippFindAttribute(response
, "job-sheets-supported", IPP_TAG_ZERO
))
3294 cgiSetArray("GROUP_ID", i
, "CUPS_BANNERS");
3295 cgiSetArray("GROUP", i
++, cgiText(_("Banners")));
3298 if (ippFindAttribute(response
, "printer-error-policy-supported",
3300 ippFindAttribute(response
, "printer-op-policy-supported",
3303 cgiSetArray("GROUP_ID", i
, "CUPS_POLICIES");
3304 cgiSetArray("GROUP", i
++, cgiText(_("Policies")));
3307 if ((attr
= ippFindAttribute(response
, "port-monitor-supported",
3308 IPP_TAG_NAME
)) != NULL
&& attr
->num_values
> 1)
3310 cgiSetArray("GROUP_ID", i
, "CUPS_PORT_MONITOR");
3311 cgiSetArray("GROUP", i
, cgiText(_("Port Monitor")));
3314 cgiStartHTML(cgiText(_("Set Printer Options")));
3315 cgiCopyTemplateLang("set-printer-options-header.tmpl");
3321 if (ppdConflicts(ppd
))
3323 for (i
= ppd
->num_groups
, k
= 0, group
= ppd
->groups
;
3326 for (j
= group
->num_options
, option
= group
->options
;
3329 if (option
->conflicted
)
3331 cgiSetArray("ckeyword", k
, option
->keyword
);
3332 cgiSetArray("ckeytext", k
, option
->text
);
3336 cgiCopyTemplateLang("option-conflict.tmpl");
3339 for (i
= ppd
->num_groups
, group
= ppd
->groups
;
3343 cgiSetVariable("GROUP_ID", group
->name
);
3345 if (!strcmp(group
->name
, "InstallableOptions"))
3346 cgiSetVariable("GROUP", cgiText(_("Options Installed")));
3348 cgiSetVariable("GROUP", group
->text
);
3350 cgiCopyTemplateLang("option-header.tmpl");
3352 for (j
= group
->num_options
, option
= group
->options
;
3356 if (!strcmp(option
->keyword
, "PageRegion"))
3359 cgiSetVariable("KEYWORD", option
->keyword
);
3360 cgiSetVariable("KEYTEXT", option
->text
);
3362 if (option
->conflicted
)
3363 cgiSetVariable("CONFLICTED", "1");
3365 cgiSetVariable("CONFLICTED", "0");
3367 cgiSetSize("CHOICES", 0);
3368 cgiSetSize("TEXT", 0);
3369 for (k
= 0, m
= 0; k
< option
->num_choices
; k
++)
3371 cgiSetArray("CHOICES", m
, option
->choices
[k
].choice
);
3372 cgiSetArray("TEXT", m
, option
->choices
[k
].text
);
3376 if (option
->choices
[k
].marked
)
3377 cgiSetVariable("DEFCHOICE", option
->choices
[k
].choice
);
3380 cgiSetSize("PARAMS", 0);
3381 cgiSetSize("PARAMTEXT", 0);
3382 cgiSetSize("PARAMVALUE", 0);
3383 cgiSetSize("INPUTTYPE", 0);
3385 if ((coption
= ppdFindCustomOption(ppd
, option
->keyword
)))
3387 const char *units
= NULL
; /* Units value, if any */
3390 cgiSetVariable("ISCUSTOM", "1");
3392 for (cparam
= ppdFirstCustomParam(coption
), m
= 0;
3394 cparam
= ppdNextCustomParam(coption
), m
++)
3396 if (!strcasecmp(option
->keyword
, "PageSize") &&
3397 strcasecmp(cparam
->name
, "Width") &&
3398 strcasecmp(cparam
->name
, "Height"))
3404 cgiSetArray("PARAMS", m
, cparam
->name
);
3405 cgiSetArray("PARAMTEXT", m
, cparam
->text
);
3406 cgiSetArray("INPUTTYPE", m
, "text");
3408 switch (cparam
->type
)
3410 case PPD_CUSTOM_POINTS
:
3411 if (!strncasecmp(option
->defchoice
, "Custom.", 7))
3413 units
= option
->defchoice
+ strlen(option
->defchoice
) - 2;
3415 if (strcmp(units
, "mm") && strcmp(units
, "cm") &&
3416 strcmp(units
, "in") && strcmp(units
, "ft"))
3418 if (units
[1] == 'm')
3427 if (!strcmp(units
, "mm"))
3428 snprintf(value
, sizeof(value
), "%g",
3429 cparam
->current
.custom_points
/ 72.0 * 25.4);
3430 else if (!strcmp(units
, "cm"))
3431 snprintf(value
, sizeof(value
), "%g",
3432 cparam
->current
.custom_points
/ 72.0 * 2.54);
3433 else if (!strcmp(units
, "in"))
3434 snprintf(value
, sizeof(value
), "%g",
3435 cparam
->current
.custom_points
/ 72.0);
3436 else if (!strcmp(units
, "ft"))
3437 snprintf(value
, sizeof(value
), "%g",
3438 cparam
->current
.custom_points
/ 72.0 / 12.0);
3439 else if (!strcmp(units
, "m"))
3440 snprintf(value
, sizeof(value
), "%g",
3441 cparam
->current
.custom_points
/ 72.0 * 0.0254);
3443 snprintf(value
, sizeof(value
), "%g",
3444 cparam
->current
.custom_points
);
3445 cgiSetArray("PARAMVALUE", m
, value
);
3448 case PPD_CUSTOM_CURVE
:
3449 case PPD_CUSTOM_INVCURVE
:
3450 case PPD_CUSTOM_REAL
:
3451 snprintf(value
, sizeof(value
), "%g",
3452 cparam
->current
.custom_real
);
3453 cgiSetArray("PARAMVALUE", m
, value
);
3456 case PPD_CUSTOM_INT
:
3457 snprintf(value
, sizeof(value
), "%d",
3458 cparam
->current
.custom_int
);
3459 cgiSetArray("PARAMVALUE", m
, value
);
3462 case PPD_CUSTOM_PASSCODE
:
3463 case PPD_CUSTOM_PASSWORD
:
3464 if (cparam
->current
.custom_password
)
3465 cgiSetArray("PARAMVALUE", m
,
3466 cparam
->current
.custom_password
);
3468 cgiSetArray("PARAMVALUE", m
, "");
3469 cgiSetArray("INPUTTYPE", m
, "password");
3472 case PPD_CUSTOM_STRING
:
3473 if (cparam
->current
.custom_string
)
3474 cgiSetArray("PARAMVALUE", m
,
3475 cparam
->current
.custom_string
);
3477 cgiSetArray("PARAMVALUE", m
, "");
3484 cgiSetArray("PARAMS", m
, "Units");
3485 cgiSetArray("PARAMTEXT", m
, cgiText(_("Units")));
3486 cgiSetArray("PARAMVALUE", m
, units
);
3490 cgiSetVariable("ISCUSTOM", "0");
3494 case PPD_UI_BOOLEAN
:
3495 cgiCopyTemplateLang("option-boolean.tmpl");
3497 case PPD_UI_PICKONE
:
3498 cgiCopyTemplateLang("option-pickone.tmpl");
3500 case PPD_UI_PICKMANY
:
3501 cgiCopyTemplateLang("option-pickmany.tmpl");
3506 cgiCopyTemplateLang("option-trailer.tmpl");
3510 if ((attr
= ippFindAttribute(response
, "job-sheets-supported",
3511 IPP_TAG_ZERO
)) != NULL
)
3514 * Add the job sheets options...
3517 cgiSetVariable("GROUP_ID", "CUPS_BANNERS");
3518 cgiSetVariable("GROUP", cgiText(_("Banners")));
3519 cgiCopyTemplateLang("option-header.tmpl");
3521 cgiSetSize("CHOICES", attr
->num_values
);
3522 cgiSetSize("TEXT", attr
->num_values
);
3523 for (k
= 0; k
< attr
->num_values
; k
++)
3525 cgiSetArray("CHOICES", k
, attr
->values
[k
].string
.text
);
3526 cgiSetArray("TEXT", k
, attr
->values
[k
].string
.text
);
3529 attr
= ippFindAttribute(response
, "job-sheets-default", IPP_TAG_ZERO
);
3531 cgiSetVariable("KEYWORD", "job_sheets_start");
3532 cgiSetVariable("KEYTEXT", cgiText(_("Starting Banner")));
3533 cgiSetVariable("DEFCHOICE", attr
!= NULL
?
3534 attr
->values
[0].string
.text
: "");
3536 cgiCopyTemplateLang("option-pickone.tmpl");
3538 cgiSetVariable("KEYWORD", "job_sheets_end");
3539 cgiSetVariable("KEYTEXT", cgiText(_("Ending Banner")));
3540 cgiSetVariable("DEFCHOICE", attr
!= NULL
&& attr
->num_values
> 1 ?
3541 attr
->values
[1].string
.text
: "");
3543 cgiCopyTemplateLang("option-pickone.tmpl");
3545 cgiCopyTemplateLang("option-trailer.tmpl");
3548 if (ippFindAttribute(response
, "printer-error-policy-supported",
3550 ippFindAttribute(response
, "printer-op-policy-supported",
3554 * Add the error and operation policy options...
3557 cgiSetVariable("GROUP_ID", "CUPS_POLICIES");
3558 cgiSetVariable("GROUP", cgiText(_("Policies")));
3559 cgiCopyTemplateLang("option-header.tmpl");
3565 attr
= ippFindAttribute(response
, "printer-error-policy-supported",
3570 cgiSetSize("CHOICES", attr
->num_values
);
3571 cgiSetSize("TEXT", attr
->num_values
);
3572 for (k
= 0; k
< attr
->num_values
; k
++)
3574 cgiSetArray("CHOICES", k
, attr
->values
[k
].string
.text
);
3575 cgiSetArray("TEXT", k
, attr
->values
[k
].string
.text
);
3578 attr
= ippFindAttribute(response
, "printer-error-policy",
3581 cgiSetVariable("KEYWORD", "printer_error_policy");
3582 cgiSetVariable("KEYTEXT", cgiText(_("Error Policy")));
3583 cgiSetVariable("DEFCHOICE", attr
== NULL
?
3584 "" : attr
->values
[0].string
.text
);
3587 cgiCopyTemplateLang("option-pickone.tmpl");
3590 * Operation policy...
3593 attr
= ippFindAttribute(response
, "printer-op-policy-supported",
3598 cgiSetSize("CHOICES", attr
->num_values
);
3599 cgiSetSize("TEXT", attr
->num_values
);
3600 for (k
= 0; k
< attr
->num_values
; k
++)
3602 cgiSetArray("CHOICES", k
, attr
->values
[k
].string
.text
);
3603 cgiSetArray("TEXT", k
, attr
->values
[k
].string
.text
);
3606 attr
= ippFindAttribute(response
, "printer-op-policy", IPP_TAG_ZERO
);
3608 cgiSetVariable("KEYWORD", "printer_op_policy");
3609 cgiSetVariable("KEYTEXT", cgiText(_("Operation Policy")));
3610 cgiSetVariable("DEFCHOICE", attr
== NULL
?
3611 "" : attr
->values
[0].string
.text
);
3613 cgiCopyTemplateLang("option-pickone.tmpl");
3616 cgiCopyTemplateLang("option-trailer.tmpl");
3620 * Binary protocol support...
3623 if ((attr
= ippFindAttribute(response
, "port-monitor-supported",
3624 IPP_TAG_NAME
)) != NULL
&& attr
->num_values
> 1)
3626 cgiSetVariable("GROUP_ID", "CUPS_PORT_MONITOR");
3627 cgiSetVariable("GROUP", cgiText(_("Port Monitor")));
3629 cgiSetSize("CHOICES", attr
->num_values
);
3630 cgiSetSize("TEXT", attr
->num_values
);
3632 for (i
= 0; i
< attr
->num_values
; i
++)
3634 cgiSetArray("CHOICES", i
, attr
->values
[i
].string
.text
);
3635 cgiSetArray("TEXT", i
, attr
->values
[i
].string
.text
);
3638 attr
= ippFindAttribute(response
, "port-monitor", IPP_TAG_NAME
);
3639 cgiSetVariable("KEYWORD", "port_monitor");
3640 cgiSetVariable("KEYTEXT", cgiText(_("Port Monitor")));
3641 cgiSetVariable("DEFCHOICE", attr
? attr
->values
[0].string
.text
: "none");
3643 cgiCopyTemplateLang("option-header.tmpl");
3644 cgiCopyTemplateLang("option-pickone.tmpl");
3645 cgiCopyTemplateLang("option-trailer.tmpl");
3648 cgiCopyTemplateLang("set-printer-options-trailer.tmpl");
3651 ippDelete(response
);
3656 * Set default options...
3659 fputs("DEBUG: Setting options...\n", stderr
);
3663 out
= cupsTempFile2(tempfile
, sizeof(tempfile
));
3664 in
= cupsFileOpen(filename
, "r");
3668 cgiSetVariable("ERROR", strerror(errno
));
3669 cgiStartHTML(cgiText(_("Set Printer Options")));
3670 cgiCopyTemplateLang("error.tmpl");
3686 while (cupsFileGets(in
, line
, sizeof(line
)))
3688 if (!strncmp(line
, "*cupsProtocol:", 14))
3690 else if (strncmp(line
, "*Default", 8))
3691 cupsFilePrintf(out
, "%s\n", line
);
3695 * Get default option name...
3698 strlcpy(keyword
, line
+ 8, sizeof(keyword
));
3700 for (keyptr
= keyword
; *keyptr
; keyptr
++)
3701 if (*keyptr
== ':' || isspace(*keyptr
& 255))
3706 if (!strcmp(keyword
, "PageRegion") ||
3707 !strcmp(keyword
, "PaperDimension") ||
3708 !strcmp(keyword
, "ImageableArea"))
3709 var
= get_option_value(ppd
, "PageSize", value
, sizeof(value
));
3711 var
= get_option_value(ppd
, keyword
, value
, sizeof(value
));
3714 cupsFilePrintf(out
, "%s\n", line
);
3716 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, var
);
3726 * Make sure temporary filename is cleared when there is no PPD...
3733 * Build a CUPS_ADD_MODIFY_CLASS/PRINTER request, which requires the
3734 * following attributes:
3736 * attributes-charset
3737 * attributes-natural-language
3739 * job-sheets-default
3740 * printer-error-policy
3745 request
= ippNewRequest(is_class
? CUPS_ADD_MODIFY_CLASS
:
3746 CUPS_ADD_MODIFY_PRINTER
);
3748 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
3751 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
3752 "job-sheets-default", 2, NULL
, NULL
);
3753 attr
->values
[0].string
.text
= _cupsStrAlloc(cgiGetVariable("job_sheets_start"));
3754 attr
->values
[1].string
.text
= _cupsStrAlloc(cgiGetVariable("job_sheets_end"));
3756 if ((var
= cgiGetVariable("printer_error_policy")) != NULL
)
3757 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
3758 "printer-error-policy", NULL
, var
);
3760 if ((var
= cgiGetVariable("printer_op_policy")) != NULL
)
3761 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
3762 "printer-op-policy", NULL
, var
);
3764 if ((var
= cgiGetVariable("port_monitor")) != NULL
)
3765 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
3766 "port-monitor", NULL
, var
);
3769 * Do the request and get back a response...
3773 ippDelete(cupsDoFileRequest(http
, request
, "/admin/", tempfile
));
3775 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
3777 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
3779 puts("Status: 401\n");
3782 else if (cupsLastError() > IPP_OK_CONFLICT
)
3784 cgiStartHTML(title
);
3785 cgiShowIPPError(_("Unable to set options:"));
3790 * Redirect successful updates back to the printer page...
3793 char refresh
[1024]; /* Refresh URL */
3796 cgiFormEncode(uri
, printer
, sizeof(uri
));
3797 snprintf(refresh
, sizeof(refresh
), "5;URL=/admin/?OP=redirect&URL=/%s/%s",
3798 is_class
? "classes" : "printers", uri
);
3799 cgiSetVariable("refresh_page", refresh
);
3801 cgiStartHTML(title
);
3803 cgiCopyTemplateLang("printer-configured.tmpl");
3818 * 'do_set_sharing()' - Set printer-is-shared value.
3822 do_set_sharing(http_t
*http
) /* I - HTTP connection */
3824 ipp_t
*request
, /* IPP request */
3825 *response
; /* IPP response */
3826 char uri
[HTTP_MAX_URI
]; /* Printer URI */
3827 const char *printer
, /* Printer name */
3828 *is_class
, /* Is a class? */
3829 *shared
; /* Sharing value */
3832 is_class
= cgiGetVariable("IS_CLASS");
3833 printer
= cgiGetVariable("PRINTER_NAME");
3834 shared
= cgiGetVariable("SHARED");
3836 if (!printer
|| !shared
)
3838 cgiSetVariable("ERROR", cgiText(_("Missing form variable!")));
3839 cgiStartHTML(cgiText(_("Set Publishing")));
3840 cgiCopyTemplateLang("error.tmpl");
3846 * Build a CUPS-Add-Printer/CUPS-Add-Class request, which requires the
3847 * following attributes:
3849 * attributes-charset
3850 * attributes-natural-language
3855 request
= ippNewRequest(is_class
? CUPS_ADD_CLASS
: CUPS_ADD_PRINTER
);
3857 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3858 "localhost", 0, is_class
? "/classes/%s" : "/printers/%s",
3860 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
3863 ippAddBoolean(request
, IPP_TAG_OPERATION
, "printer-is-shared", atoi(shared
));
3866 * Do the request and get back a response...
3869 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
)
3871 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
3873 ippDelete(response
);
3876 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
3878 puts("Status: 401\n");
3881 else if (cupsLastError() > IPP_OK_CONFLICT
)
3883 cgiStartHTML(cgiText(_("Set Publishing")));
3884 cgiShowIPPError(_("Unable to change printer-is-shared attribute:"));
3889 * Redirect successful updates back to the printer page...
3892 char url
[1024], /* Printer/class URL */
3893 refresh
[1024]; /* Refresh URL */
3896 cgiRewriteURL(uri
, url
, sizeof(url
), NULL
);
3897 cgiFormEncode(uri
, url
, sizeof(uri
));
3898 snprintf(refresh
, sizeof(refresh
), "5;URL=/admin/?OP=redirect&URL=%s", uri
);
3899 cgiSetVariable("refresh_page", refresh
);
3901 cgiStartHTML(cgiText(_("Set Publishing")));
3902 cgiCopyTemplateLang(is_class
? "class-modified.tmpl" :
3903 "printer-modified.tmpl");
3911 * 'get_option_value()' - Return the value of an option.
3913 * This function also handles generation of custom option values.
3916 static char * /* O - Value string or NULL on error */
3918 ppd_file_t
*ppd
, /* I - PPD file */
3919 const char *name
, /* I - Option name */
3920 char *buffer
, /* I - String buffer */
3921 size_t bufsize
) /* I - Size of buffer */
3923 char *bufptr
, /* Pointer into buffer */
3924 *bufend
; /* End of buffer */
3925 ppd_coption_t
*coption
; /* Custom option */
3926 ppd_cparam_t
*cparam
; /* Current custom parameter */
3927 char keyword
[256]; /* Parameter name */
3928 const char *val
, /* Parameter value */
3929 *uval
; /* Units value */
3930 long integer
; /* Integer value */
3931 double number
, /* Number value */
3932 number_points
; /* Number in points */
3936 * See if we have a custom option choice...
3939 if ((val
= cgiGetVariable(name
)) == NULL
)
3947 else if (strcasecmp(val
, "Custom") ||
3948 (coption
= ppdFindCustomOption(ppd
, name
)) == NULL
)
3951 * Not a custom choice...
3954 strlcpy(buffer
, val
, bufsize
);
3959 * OK, we have a custom option choice, format it...
3964 if (!strcmp(coption
->keyword
, "PageSize"))
3966 const char *lval
; /* Length string value */
3967 double width
, /* Width value */
3968 width_points
, /* Width in points */
3969 length
, /* Length value */
3970 length_points
; /* Length in points */
3973 val
= cgiGetVariable("PageSize.Width");
3974 lval
= cgiGetVariable("PageSize.Height");
3975 uval
= cgiGetVariable("PageSize.Units");
3977 if (!val
|| !lval
|| !uval
||
3978 (width
= strtod(val
, NULL
)) == 0.0 ||
3979 (length
= strtod(lval
, NULL
)) == 0.0 ||
3980 (strcmp(uval
, "pt") && strcmp(uval
, "in") && strcmp(uval
, "ft") &&
3981 strcmp(uval
, "cm") && strcmp(uval
, "mm") && strcmp(uval
, "m")))
3984 width_points
= get_points(width
, uval
);
3985 length_points
= get_points(length
, uval
);
3987 if (width_points
< ppd
->custom_min
[0] ||
3988 width_points
> ppd
->custom_max
[0] ||
3989 length_points
< ppd
->custom_min
[1] ||
3990 length_points
> ppd
->custom_max
[1])
3993 snprintf(buffer
, bufsize
, "Custom.%gx%g%s", width
, length
, uval
);
3995 else if (cupsArrayCount(coption
->params
) == 1)
3997 cparam
= ppdFirstCustomParam(coption
);
3998 snprintf(keyword
, sizeof(keyword
), "%s.%s", coption
->keyword
, cparam
->name
);
4000 if ((val
= cgiGetVariable(keyword
)) == NULL
)
4003 switch (cparam
->type
)
4005 case PPD_CUSTOM_CURVE
:
4006 case PPD_CUSTOM_INVCURVE
:
4007 case PPD_CUSTOM_REAL
:
4008 if ((number
= strtod(val
, NULL
)) == 0.0 ||
4009 number
< cparam
->minimum
.custom_real
||
4010 number
> cparam
->maximum
.custom_real
)
4013 snprintf(buffer
, bufsize
, "Custom.%g", number
);
4016 case PPD_CUSTOM_INT
:
4017 if (!*val
|| (integer
= strtol(val
, NULL
, 10)) == LONG_MIN
||
4018 integer
== LONG_MAX
||
4019 integer
< cparam
->minimum
.custom_int
||
4020 integer
> cparam
->maximum
.custom_int
)
4023 snprintf(buffer
, bufsize
, "Custom.%ld", integer
);
4026 case PPD_CUSTOM_POINTS
:
4027 snprintf(keyword
, sizeof(keyword
), "%s.Units", coption
->keyword
);
4029 if ((number
= strtod(val
, NULL
)) == 0.0 ||
4030 (uval
= cgiGetVariable(keyword
)) == NULL
||
4031 (strcmp(uval
, "pt") && strcmp(uval
, "in") && strcmp(uval
, "ft") &&
4032 strcmp(uval
, "cm") && strcmp(uval
, "mm") && strcmp(uval
, "m")))
4035 number_points
= get_points(number
, uval
);
4036 if (number_points
< cparam
->minimum
.custom_points
||
4037 number_points
> cparam
->maximum
.custom_points
)
4040 snprintf(buffer
, bufsize
, "Custom.%g%s", number
, uval
);
4043 case PPD_CUSTOM_PASSCODE
:
4044 for (uval
= val
; *uval
; uval
++)
4045 if (!isdigit(*uval
& 255))
4048 case PPD_CUSTOM_PASSWORD
:
4049 case PPD_CUSTOM_STRING
:
4050 integer
= (long)strlen(val
);
4051 if (integer
< cparam
->minimum
.custom_string
||
4052 integer
> cparam
->maximum
.custom_string
)
4055 snprintf(buffer
, bufsize
, "Custom.%s", val
);
4061 const char *prefix
= "{"; /* Prefix string */
4065 bufend
= buffer
+ bufsize
;
4067 for (cparam
= ppdFirstCustomParam(coption
);
4069 cparam
= ppdNextCustomParam(coption
))
4071 snprintf(keyword
, sizeof(keyword
), "%s.%s", coption
->keyword
,
4074 if ((val
= cgiGetVariable(keyword
)) == NULL
)
4077 snprintf(bufptr
, bufend
- bufptr
, "%s%s=", prefix
, cparam
->name
);
4078 bufptr
+= strlen(bufptr
);
4081 switch (cparam
->type
)
4083 case PPD_CUSTOM_CURVE
:
4084 case PPD_CUSTOM_INVCURVE
:
4085 case PPD_CUSTOM_REAL
:
4086 if ((number
= strtod(val
, NULL
)) == 0.0 ||
4087 number
< cparam
->minimum
.custom_real
||
4088 number
> cparam
->maximum
.custom_real
)
4091 snprintf(bufptr
, bufend
- bufptr
, "%g", number
);
4094 case PPD_CUSTOM_INT
:
4095 if (!*val
|| (integer
= strtol(val
, NULL
, 10)) == LONG_MIN
||
4096 integer
== LONG_MAX
||
4097 integer
< cparam
->minimum
.custom_int
||
4098 integer
> cparam
->maximum
.custom_int
)
4101 snprintf(bufptr
, bufend
- bufptr
, "%ld", integer
);
4104 case PPD_CUSTOM_POINTS
:
4105 snprintf(keyword
, sizeof(keyword
), "%s.Units", coption
->keyword
);
4107 if ((number
= strtod(val
, NULL
)) == 0.0 ||
4108 (uval
= cgiGetVariable(keyword
)) == NULL
||
4109 (strcmp(uval
, "pt") && strcmp(uval
, "in") &&
4110 strcmp(uval
, "ft") && strcmp(uval
, "cm") &&
4111 strcmp(uval
, "mm") && strcmp(uval
, "m")))
4114 number_points
= get_points(number
, uval
);
4115 if (number_points
< cparam
->minimum
.custom_points
||
4116 number_points
> cparam
->maximum
.custom_points
)
4119 snprintf(bufptr
, bufend
- bufptr
, "%g%s", number
, uval
);
4122 case PPD_CUSTOM_PASSCODE
:
4123 for (uval
= val
; *uval
; uval
++)
4124 if (!isdigit(*uval
& 255))
4127 case PPD_CUSTOM_PASSWORD
:
4128 case PPD_CUSTOM_STRING
:
4129 integer
= (long)strlen(val
);
4130 if (integer
< cparam
->minimum
.custom_string
||
4131 integer
> cparam
->maximum
.custom_string
)
4134 if ((bufptr
+ 2) > bufend
)
4140 while (*val
&& bufptr
< bufend
)
4142 if (*val
== '\\' || *val
== '\"')
4144 if ((bufptr
+ 1) >= bufend
)
4153 if (bufptr
>= bufend
)
4162 bufptr
+= strlen(bufptr
);
4165 if (bufptr
== buffer
|| (bufend
- bufptr
) < 2)
4168 strcpy(bufptr
, "}");
4176 * 'get_points()' - Get a value in points.
4179 static double /* O - Number in points */
4180 get_points(double number
, /* I - Original number */
4181 const char *uval
) /* I - Units */
4183 if (!strcmp(uval
, "mm")) /* Millimeters */
4184 return (number
* 72.0 / 25.4);
4185 else if (!strcmp(uval
, "cm")) /* Centimeters */
4186 return (number
* 72.0 / 2.54);
4187 else if (!strcmp(uval
, "in")) /* Inches */
4188 return (number
* 72.0);
4189 else if (!strcmp(uval
, "ft")) /* Feet */
4190 return (number
* 72.0 * 12.0);
4191 else if (!strcmp(uval
, "m")) /* Meters */
4192 return (number
* 72.0 / 0.0254);
4199 * End of "$Id: admin.c 8029 2008-10-08 21:07:45Z mike $".