4 * Administration CGI for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 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 * do_add_rss_subscription() - Add a RSS subscription.
19 * do_am_class() - Add or modify a class.
20 * do_am_printer() - Add or modify a printer.
21 * do_cancel_subscription() - Cancel a subscription.
22 * do_config_server() - Configure server settings.
23 * do_delete_class() - Delete a class.
24 * do_delete_printer() - Delete a printer.
25 * do_export() - Export printers to Samba.
26 * do_list_printers() - List available printers.
27 * do_menu() - Show the main menu.
28 * do_printer_op() - Do a printer operation.
29 * do_set_allowed_users() - Set the allowed/denied users for a queue.
30 * do_set_options() - Configure the default options for a queue.
31 * do_set_sharing() - Set printer-is-shared value.
32 * get_option_value() - Return the value of an option.
33 * get_points() - Get a value in points.
34 * match_string() - Return the number of matching characters.
38 * Include necessary headers...
41 #include "cgi-private.h"
42 #include <cups/adminutil.h>
43 #include <cups/file.h>
55 static void do_add_rss_subscription(http_t
*http
);
56 static void do_am_class(http_t
*http
, int modify
);
57 static void do_am_printer(http_t
*http
, int modify
);
58 static void do_cancel_subscription(http_t
*http
);
59 static void do_set_options(http_t
*http
, int is_class
);
60 static void do_config_server(http_t
*http
);
61 static void do_delete_class(http_t
*http
);
62 static void do_delete_printer(http_t
*http
);
63 static void do_export(http_t
*http
);
64 static void do_list_printers(http_t
*http
);
65 static void do_menu(http_t
*http
);
66 static void do_printer_op(http_t
*http
,
67 ipp_op_t op
, const char *title
);
68 static void do_set_allowed_users(http_t
*http
);
69 static void do_set_sharing(http_t
*http
);
70 static char *get_option_value(ppd_file_t
*ppd
, const char *name
,
71 char *buffer
, size_t bufsize
);
72 static double get_points(double number
, const char *uval
);
73 static int match_string(const char *a
, const char *b
);
77 * 'main()' - Main entry for CGI.
80 int /* O - Exit status */
81 main(int argc
, /* I - Number of command-line arguments */
82 char *argv
[]) /* I - Command-line arguments */
84 http_t
*http
; /* Connection to the server */
85 const char *op
; /* Operation name */
89 * Connect to the HTTP server...
92 fputs("DEBUG: admin.cgi started...\n", stderr
);
94 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
98 perror("ERROR: Unable to connect to cupsd");
99 fprintf(stderr
, "DEBUG: cupsServer()=\"%s\"\n",
100 cupsServer() ? cupsServer() : "(null)");
101 fprintf(stderr
, "DEBUG: ippPort()=%d\n", ippPort());
102 fprintf(stderr
, "DEBUG: cupsEncryption()=%d\n", cupsEncryption());
106 fprintf(stderr
, "DEBUG: http=%p\n", http
);
109 * Set the web interface section...
112 cgiSetVariable("SECTION", "admin");
115 * See if we have form data...
118 if (!cgiInitialize())
121 * Nope, send the administration menu...
124 fputs("DEBUG: No form data, showing main menu...\n", stderr
);
128 else if ((op
= cgiGetVariable("OP")) != NULL
&& cgiIsPOST())
131 * Do the operation...
134 fprintf(stderr
, "DEBUG: op=\"%s\"...\n", op
);
136 if (!strcmp(op
, "start-printer"))
137 do_printer_op(http
, IPP_RESUME_PRINTER
, cgiText(_("Start Printer")));
138 else if (!strcmp(op
, "stop-printer"))
139 do_printer_op(http
, IPP_PAUSE_PRINTER
, cgiText(_("Stop Printer")));
140 else if (!strcmp(op
, "start-class"))
141 do_printer_op(http
, IPP_RESUME_PRINTER
, cgiText(_("Start Class")));
142 else if (!strcmp(op
, "stop-class"))
143 do_printer_op(http
, IPP_PAUSE_PRINTER
, cgiText(_("Stop Class")));
144 else if (!strcmp(op
, "accept-jobs"))
145 do_printer_op(http
, CUPS_ACCEPT_JOBS
, cgiText(_("Accept Jobs")));
146 else if (!strcmp(op
, "reject-jobs"))
147 do_printer_op(http
, CUPS_REJECT_JOBS
, cgiText(_("Reject Jobs")));
148 else if (!strcmp(op
, "purge-jobs"))
149 do_printer_op(http
, IPP_PURGE_JOBS
, cgiText(_("Purge Jobs")));
150 else if (!strcmp(op
, "set-allowed-users"))
151 do_set_allowed_users(http
);
152 else if (!strcmp(op
, "set-as-default"))
153 do_printer_op(http
, CUPS_SET_DEFAULT
, cgiText(_("Set As Default")));
154 else if (!strcmp(op
, "set-sharing"))
155 do_set_sharing(http
);
156 else if (!strcmp(op
, "find-new-printers") ||
157 !strcmp(op
, "list-available-printers"))
158 do_list_printers(http
);
159 else if (!strcmp(op
, "add-class"))
160 do_am_class(http
, 0);
161 else if (!strcmp(op
, "add-printer"))
162 do_am_printer(http
, 0);
163 else if (!strcmp(op
, "modify-class"))
164 do_am_class(http
, 1);
165 else if (!strcmp(op
, "modify-printer"))
166 do_am_printer(http
, 1);
167 else if (!strcmp(op
, "delete-class"))
168 do_delete_class(http
);
169 else if (!strcmp(op
, "delete-printer"))
170 do_delete_printer(http
);
171 else if (!strcmp(op
, "set-class-options"))
172 do_set_options(http
, 1);
173 else if (!strcmp(op
, "set-printer-options"))
174 do_set_options(http
, 0);
175 else if (!strcmp(op
, "config-server"))
176 do_config_server(http
);
177 else if (!strcmp(op
, "export-samba"))
179 else if (!strcmp(op
, "add-rss-subscription"))
180 do_add_rss_subscription(http
);
181 else if (!strcmp(op
, "cancel-subscription"))
182 do_cancel_subscription(http
);
186 * Bad operation code - display an error...
189 cgiStartHTML(cgiText(_("Administration")));
190 cgiCopyTemplateLang("error-op.tmpl");
194 else if (op
&& !strcmp(op
, "redirect"))
196 const char *url
; /* Redirection URL... */
197 char prefix
[1024]; /* URL prefix */
201 snprintf(prefix
, sizeof(prefix
), "https://%s:%s",
202 getenv("SERVER_NAME"), getenv("SERVER_PORT"));
204 snprintf(prefix
, sizeof(prefix
), "http://%s:%s",
205 getenv("SERVER_NAME"), getenv("SERVER_PORT"));
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 * 'do_add_rss_subscription()' - Add a RSS subscription.
242 do_add_rss_subscription(http_t
*http
) /* I - HTTP connection */
244 ipp_t
*request
, /* IPP request data */
245 *response
; /* IPP response data */
246 char rss_uri
[1024]; /* RSS notify-recipient URI */
247 int num_events
; /* Number of events */
248 const char *events
[12], /* Subscribed events */
249 *subscription_name
, /* Subscription name */
250 *printer_uri
, /* Printer URI */
251 *ptr
, /* Pointer into name */
252 *user
; /* Username */
253 int max_events
; /* Maximum number of events */
257 * See if we have all of the required information...
260 subscription_name
= cgiGetVariable("SUBSCRIPTION_NAME");
261 printer_uri
= cgiGetVariable("PRINTER_URI");
264 if (cgiGetVariable("EVENT_JOB_CREATED"))
265 events
[num_events
++] = "job-created";
266 if (cgiGetVariable("EVENT_JOB_COMPLETED"))
267 events
[num_events
++] = "job-completed";
268 if (cgiGetVariable("EVENT_JOB_STOPPED"))
269 events
[num_events
++] = "job-stopped";
270 if (cgiGetVariable("EVENT_JOB_CONFIG_CHANGED"))
271 events
[num_events
++] = "job-config-changed";
272 if (cgiGetVariable("EVENT_PRINTER_STOPPED"))
273 events
[num_events
++] = "printer-stopped";
274 if (cgiGetVariable("EVENT_PRINTER_ADDED"))
275 events
[num_events
++] = "printer-added";
276 if (cgiGetVariable("EVENT_PRINTER_MODIFIED"))
277 events
[num_events
++] = "printer-modified";
278 if (cgiGetVariable("EVENT_PRINTER_DELETED"))
279 events
[num_events
++] = "printer-deleted";
280 if (cgiGetVariable("EVENT_SERVER_STARTED"))
281 events
[num_events
++] = "server-started";
282 if (cgiGetVariable("EVENT_SERVER_STOPPED"))
283 events
[num_events
++] = "server-stopped";
284 if (cgiGetVariable("EVENT_SERVER_RESTARTED"))
285 events
[num_events
++] = "server-restarted";
286 if (cgiGetVariable("EVENT_SERVER_AUDIT"))
287 events
[num_events
++] = "server-audit";
289 if ((ptr
= cgiGetVariable("MAX_EVENTS")) != NULL
)
290 max_events
= atoi(ptr
);
294 if (!subscription_name
|| !printer_uri
|| !num_events
||
295 max_events
<= 0 || max_events
> 9999)
298 * Don't have everything we need, so get the available printers
299 * and classes and (re)show the add page...
302 request
= ippNewRequest(CUPS_GET_PRINTERS
);
303 response
= cupsDoRequest(http
, request
, "/");
305 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
309 cgiStartHTML(cgiText(_("Add RSS Subscription")));
311 cgiCopyTemplateLang("add-rss-subscription.tmpl");
318 * Make sure we have a username...
321 if ((user
= getenv("REMOTE_USER")) == NULL
)
323 puts("Status: 401\n");
328 * Validate the subscription name...
331 for (ptr
= subscription_name
; *ptr
; ptr
++)
332 if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' ||
333 *ptr
== '?' || *ptr
== '#')
338 cgiSetVariable("ERROR",
339 cgiText(_("The subscription name may not "
340 "contain spaces, slashes (/), question marks (?), "
341 "or the pound sign (#).")));
342 cgiStartHTML(_("Add RSS Subscription"));
343 cgiCopyTemplateLang("error.tmpl");
349 * Add the subscription...
352 ptr
= subscription_name
+ strlen(subscription_name
) - 4;
353 if (ptr
< subscription_name
|| strcmp(ptr
, ".rss"))
354 httpAssembleURIf(HTTP_URI_CODING_ALL
, rss_uri
, sizeof(rss_uri
), "rss",
355 NULL
, NULL
, 0, "/%s.rss?max_events=%d", subscription_name
,
358 httpAssembleURIf(HTTP_URI_CODING_ALL
, rss_uri
, sizeof(rss_uri
), "rss",
359 NULL
, NULL
, 0, "/%s?max_events=%d", subscription_name
,
362 request
= ippNewRequest(IPP_CREATE_PRINTER_SUBSCRIPTION
);
364 if (!strcasecmp(printer_uri
, "#ALL#"))
365 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
366 NULL
, "ipp://localhost/");
368 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
371 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
374 ippAddString(request
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
375 "notify-recipient-uri", NULL
, rss_uri
);
376 ippAddStrings(request
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
, "notify-events",
377 num_events
, NULL
, events
);
378 ippAddInteger(request
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
379 "notify-lease-duration", 0);
381 ippDelete(cupsDoRequest(http
, request
, "/"));
383 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
385 puts("Status: 401\n");
388 else if (cupsLastError() > IPP_OK_CONFLICT
)
390 cgiStartHTML(_("Add RSS Subscription"));
391 cgiShowIPPError(_("Unable to add RSS subscription:"));
396 * Redirect successful updates back to the admin page...
399 cgiSetVariable("refresh_page", "5;URL=/admin");
400 cgiStartHTML(_("Add RSS Subscription"));
401 cgiCopyTemplateLang("subscription-added.tmpl");
409 * 'do_am_class()' - Add or modify a class.
413 do_am_class(http_t
*http
, /* I - HTTP connection */
414 int modify
) /* I - Modify the printer? */
416 int i
, j
; /* Looping vars */
417 int element
; /* Element number */
418 int num_printers
; /* Number of printers */
419 ipp_t
*request
, /* IPP request */
420 *response
; /* IPP response */
421 ipp_attribute_t
*attr
; /* member-uris attribute */
422 char uri
[HTTP_MAX_URI
]; /* Device or printer URI */
423 const char *name
, /* Pointer to class name */
424 *ptr
; /* Pointer to CGI variable */
425 const char *title
; /* Title of page */
426 static const char * const pattrs
[] = /* Requested printer attributes */
434 title
= cgiText(modify
? _("Modify Class") : _("Add Class"));
435 name
= cgiGetVariable("PRINTER_NAME");
437 if (cgiGetVariable("PRINTER_LOCATION") == NULL
)
440 * Build a CUPS_GET_PRINTERS request, which requires the
441 * following attributes:
444 * attributes-natural-language
447 request
= ippNewRequest(CUPS_GET_PRINTERS
);
450 * Do the request and get back a response...
453 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
456 * Create MEMBER_URIS and MEMBER_NAMES arrays...
459 for (element
= 0, attr
= response
->attrs
;
462 if (attr
->name
&& !strcmp(attr
->name
, "printer-uri-supported"))
464 if ((ptr
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
&&
465 (!name
|| strcasecmp(name
, ptr
+ 1)))
468 * Don't show the current class...
471 cgiSetArray("MEMBER_URIS", element
, attr
->values
[0].string
.text
);
476 for (element
= 0, attr
= response
->attrs
;
479 if (attr
->name
&& !strcmp(attr
->name
, "printer-name"))
481 if (!name
|| strcasecmp(name
, attr
->values
[0].string
.text
))
484 * Don't show the current class...
487 cgiSetArray("MEMBER_NAMES", element
, attr
->values
[0].string
.text
);
492 num_printers
= cgiGetSize("MEMBER_URIS");
502 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
503 * following attributes:
506 * attributes-natural-language
510 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
512 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
513 "localhost", 0, "/classes/%s", name
);
514 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
517 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
518 "requested-attributes",
519 (int)(sizeof(pattrs
) / sizeof(pattrs
[0])),
523 * Do the request and get back a response...
526 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
528 if ((attr
= ippFindAttribute(response
, "member-names",
529 IPP_TAG_NAME
)) != NULL
)
532 * Mark any current members in the class...
535 for (j
= 0; j
< num_printers
; j
++)
536 cgiSetArray("MEMBER_SELECTED", j
, "");
538 for (i
= 0; i
< attr
->num_values
; i
++)
540 for (j
= 0; j
< num_printers
; j
++)
542 if (!strcasecmp(attr
->values
[i
].string
.text
,
543 cgiGetArray("MEMBER_NAMES", j
)))
545 cgiSetArray("MEMBER_SELECTED", j
, "SELECTED");
552 if ((attr
= ippFindAttribute(response
, "printer-info",
553 IPP_TAG_TEXT
)) != NULL
)
554 cgiSetVariable("PRINTER_INFO", attr
->values
[0].string
.text
);
556 if ((attr
= ippFindAttribute(response
, "printer-location",
557 IPP_TAG_TEXT
)) != NULL
)
558 cgiSetVariable("PRINTER_LOCATION", attr
->values
[0].string
.text
);
564 * Update the location and description of an existing printer...
568 cgiCopyTemplateLang("modify-class.tmpl");
573 * Get the name, location, and description for a new printer...
577 cgiCopyTemplateLang("add-class.tmpl");
585 for (ptr
= name
; *ptr
; ptr
++)
586 if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
589 if (*ptr
|| ptr
== name
|| strlen(name
) > 127)
591 cgiSetVariable("ERROR",
592 cgiText(_("The class name may only contain up to "
593 "127 printable characters and may not "
594 "contain spaces, slashes (/), or the "
595 "pound sign (#).")));
597 cgiCopyTemplateLang("error.tmpl");
603 * Build a CUPS_ADD_CLASS request, which requires the following
607 * attributes-natural-language
611 * printer-is-accepting-jobs
616 request
= ippNewRequest(CUPS_ADD_CLASS
);
618 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
619 "localhost", 0, "/classes/%s",
620 cgiGetVariable("PRINTER_NAME"));
621 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
624 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-location",
625 NULL
, cgiGetVariable("PRINTER_LOCATION"));
627 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info",
628 NULL
, cgiGetVariable("PRINTER_INFO"));
630 ippAddBoolean(request
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", 1);
632 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
635 if ((num_printers
= cgiGetSize("MEMBER_URIS")) > 0)
637 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "member-uris",
638 num_printers
, NULL
, NULL
);
639 for (i
= 0; i
< num_printers
; i
++)
640 attr
->values
[i
].string
.text
= strdup(cgiGetArray("MEMBER_URIS", i
));
644 * Do the request and get back a response...
647 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
649 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
651 puts("Status: 401\n");
654 else if (cupsLastError() > IPP_OK_CONFLICT
)
657 cgiShowIPPError(modify
? _("Unable to modify class:") :
658 _("Unable to add class:"));
663 * Redirect successful updates back to the class page...
666 char refresh
[1024]; /* Refresh URL */
668 cgiFormEncode(uri
, name
, sizeof(uri
));
669 snprintf(refresh
, sizeof(refresh
), "5;URL=/admin/?OP=redirect&URL=/classes/%s",
671 cgiSetVariable("refresh_page", refresh
);
676 cgiCopyTemplateLang("class-modified.tmpl");
678 cgiCopyTemplateLang("class-added.tmpl");
686 * 'do_am_printer()' - Add or modify a printer.
690 do_am_printer(http_t
*http
, /* I - HTTP connection */
691 int modify
) /* I - Modify the printer? */
693 int i
; /* Looping var */
694 ipp_attribute_t
*attr
; /* Current attribute */
695 ipp_t
*request
, /* IPP request */
696 *response
, /* IPP response */
697 *oldinfo
; /* Old printer information */
698 const cgi_file_t
*file
; /* Uploaded file, if any */
699 const char *var
; /* CGI variable */
700 char uri
[HTTP_MAX_URI
], /* Device or printer URI */
701 *uriptr
; /* Pointer into URI */
702 int maxrate
; /* Maximum baud rate */
703 char baudrate
[255]; /* Baud rate string */
704 const char *name
, /* Pointer to class name */
705 *ptr
; /* Pointer to CGI variable */
706 const char *title
; /* Title of page */
707 static int baudrates
[] = /* Baud rates */
722 ptr
= cgiGetVariable("DEVICE_URI");
723 fprintf(stderr
, "DEBUG: do_am_printer: DEVICE_URI=\"%s\"\n",
724 ptr
? ptr
: "(null)");
726 title
= cgiText(modify
? _("Modify Printer") : _("Add Printer"));
731 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
732 * following attributes:
735 * attributes-natural-language
739 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
741 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
742 "localhost", 0, "/printers/%s",
743 cgiGetVariable("PRINTER_NAME"));
744 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
748 * Do the request and get back a response...
751 oldinfo
= cupsDoRequest(http
, request
, "/");
760 fprintf(stderr
, "DEBUG: file->tempfile=%s\n", file
->tempfile
);
761 fprintf(stderr
, "DEBUG: file->name=%s\n", file
->name
);
762 fprintf(stderr
, "DEBUG: file->filename=%s\n", file
->filename
);
763 fprintf(stderr
, "DEBUG: file->mimetype=%s\n", file
->mimetype
);
766 if ((name
= cgiGetVariable("PRINTER_NAME")) != NULL
)
768 for (ptr
= name
; *ptr
; ptr
++)
769 if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
772 if (*ptr
|| ptr
== name
|| strlen(name
) > 127)
774 cgiSetVariable("ERROR",
775 cgiText(_("The printer name may only contain up to "
776 "127 printable characters and may not "
777 "contain spaces, slashes (/), or the "
778 "pound sign (#).")));
780 cgiCopyTemplateLang("error.tmpl");
786 if ((var
= cgiGetVariable("DEVICE_URI")) != NULL
)
788 if ((uriptr
= strrchr(var
, '|')) != NULL
)
791 * Extract make and make/model from device URI string...
794 char make
[1024], /* Make string */
795 *makeptr
; /* Pointer into make */
800 strlcpy(make
, uriptr
, sizeof(make
));
802 if ((makeptr
= strchr(make
, ' ')) != NULL
)
804 else if ((makeptr
= strchr(make
, '-')) != NULL
)
806 else if (!strncasecmp(make
, "laserjet", 8) ||
807 !strncasecmp(make
, "deskjet", 7) ||
808 !strncasecmp(make
, "designjet", 9))
810 else if (!strncasecmp(make
, "phaser", 6))
811 strcpy(make
, "Xerox");
812 else if (!strncasecmp(make
, "stylus", 6))
813 strcpy(make
, "Epson");
815 strcpy(make
, "Generic");
817 if (!cgiGetVariable("CURRENT_MAKE"))
818 cgiSetVariable("CURRENT_MAKE", make
);
820 cgiSetVariable("PPD_MAKE", make
);
822 if (!cgiGetVariable("CURRENT_MAKE_AND_MODEL"))
823 cgiSetVariable("CURRENT_MAKE_AND_MODEL", uriptr
);
827 char template[128], /* Template name */
828 *tptr
; /* Pointer into template name */
830 cgiSetVariable("PRINTER_INFO", uriptr
);
832 for (tptr
= template;
833 tptr
< (template + sizeof(template) - 1) && *uriptr
;
835 if (isalnum(*uriptr
& 255) || *uriptr
== '_' || *uriptr
== '-' ||
838 else if ((*uriptr
== ' ' || *uriptr
== '/') && tptr
[-1] != '_')
840 else if (*uriptr
== '?' || *uriptr
== '(')
845 cgiSetVariable("TEMPLATE_NAME", template);
853 * Build a CUPS_GET_DEVICES request, which requires the following
857 * attributes-natural-language
861 fputs("DEBUG: Getting list of devices...\n", stderr
);
863 request
= ippNewRequest(CUPS_GET_DEVICES
);
865 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
866 NULL
, "ipp://localhost/printers/");
869 * Do the request and get back a response...
872 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
874 fputs("DEBUG: Got device list!\n", stderr
);
876 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
881 "ERROR: CUPS-Get-Devices request failed with status %x: %s\n",
882 cupsLastError(), cupsLastErrorString());
885 * Let the user choose...
888 if ((attr
= ippFindAttribute(oldinfo
, "device-uri", IPP_TAG_URI
)) != NULL
)
890 strlcpy(uri
, attr
->values
[0].string
.text
, sizeof(uri
));
891 if ((uriptr
= strchr(uri
, ':')) != NULL
&& strncmp(uriptr
, "://", 3) == 0)
894 cgiSetVariable("CURRENT_DEVICE_URI", attr
->values
[0].string
.text
);
895 cgiSetVariable("CURRENT_DEVICE_SCHEME", uri
);
899 cgiCopyTemplateLang("choose-device.tmpl");
902 else if (strchr(var
, '/') == NULL
)
904 if ((attr
= ippFindAttribute(oldinfo
, "device-uri", IPP_TAG_URI
)) != NULL
)
907 * Set the current device URI for the form to the old one...
910 if (strncmp(attr
->values
[0].string
.text
, var
, strlen(var
)) == 0)
911 cgiSetVariable("DEVICE_URI", attr
->values
[0].string
.text
);
915 * User needs to set the full URI...
919 cgiCopyTemplateLang("choose-uri.tmpl");
922 else if (!strncmp(var
, "serial:", 7) && !cgiGetVariable("BAUDRATE"))
925 * Need baud rate, parity, etc.
928 if ((var
= strchr(var
, '?')) != NULL
&&
929 strncmp(var
, "?baud=", 6) == 0)
930 maxrate
= atoi(var
+ 6);
934 for (i
= 0; i
< 10; i
++)
935 if (baudrates
[i
] > maxrate
)
939 sprintf(baudrate
, "%d", baudrates
[i
]);
940 cgiSetArray("BAUDRATES", i
, baudrate
);
944 cgiCopyTemplateLang("choose-serial.tmpl");
947 else if (!name
|| !cgiGetVariable("PRINTER_LOCATION"))
954 * Update the location and description of an existing printer...
958 cgiSetIPPVars(oldinfo
, NULL
, NULL
, NULL
, 0);
960 cgiCopyTemplateLang("modify-printer.tmpl");
965 * Get the name, location, and description for a new printer...
969 if (!strncmp(var
, "usb:", 4))
970 cgiSetVariable("printer_is_shared", "1");
972 #endif /* __APPLE__ */
973 cgiSetVariable("printer_is_shared", "0");
975 cgiCopyTemplateLang("add-printer.tmpl");
985 else if (!file
&& !cgiGetVariable("PPD_NAME"))
990 * Get the PPD file...
993 int fd
; /* PPD file */
994 char filename
[1024]; /* PPD filename */
995 ppd_file_t
*ppd
; /* PPD information */
996 char buffer
[1024]; /* Buffer */
997 int bytes
; /* Number of bytes */
998 http_status_t get_status
; /* Status of GET */
1001 /* TODO: Use cupsGetFile() API... */
1002 snprintf(uri
, sizeof(uri
), "/printers/%s.ppd", name
);
1004 if (httpGet(http
, uri
))
1007 while ((get_status
= httpUpdate(http
)) == HTTP_CONTINUE
);
1009 if (get_status
!= HTTP_OK
)
1011 fprintf(stderr
, "ERROR: Unable to get PPD file %s: %d - %s\n",
1012 uri
, get_status
, httpStatus(get_status
));
1014 else if ((fd
= cupsTempFd(filename
, sizeof(filename
))) >= 0)
1016 while ((bytes
= httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
1017 write(fd
, buffer
, bytes
);
1021 if ((ppd
= ppdOpenFile(filename
)) != NULL
)
1023 if (ppd
->manufacturer
)
1024 cgiSetVariable("CURRENT_MAKE", ppd
->manufacturer
);
1027 cgiSetVariable("CURRENT_MAKE_AND_MODEL", ppd
->nickname
);
1034 fprintf(stderr
, "ERROR: Unable to open PPD file %s: %s\n",
1035 filename
, ppdErrorString(ppdLastError(&bytes
)));
1043 "ERROR: Unable to create temporary file for PPD file: %s\n",
1049 * Build a CUPS_GET_PPDS request, which requires the following
1052 * attributes-charset
1053 * attributes-natural-language
1057 request
= ippNewRequest(CUPS_GET_PPDS
);
1059 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1060 NULL
, "ipp://localhost/printers/");
1062 if ((var
= cgiGetVariable("CURRENT_MAKE")) == NULL
)
1063 var
= cgiGetVariable("PPD_MAKE");
1065 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
1066 "ppd-make", NULL
, var
);
1068 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1069 "requested-attributes", NULL
, "ppd-make");
1072 * Do the request and get back a response...
1075 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1078 * Got the list of PPDs, see if the user has selected a make...
1081 if (cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0) == 0)
1084 * No PPD files with this make, try again with all makes...
1087 ippDelete(response
);
1089 request
= ippNewRequest(CUPS_GET_PPDS
);
1091 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1092 NULL
, "ipp://localhost/printers/");
1094 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1095 "requested-attributes", NULL
, "ppd-make");
1097 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1098 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
1100 cgiStartHTML(title
);
1101 cgiCopyTemplateLang("choose-make.tmpl");
1104 else if (!var
|| cgiGetVariable("SELECT_MAKE"))
1106 cgiStartHTML(title
);
1107 cgiCopyTemplateLang("choose-make.tmpl");
1113 * Let the user choose a model...
1116 const char *make_model
; /* Current make/model string */
1119 if ((make_model
= cgiGetVariable("CURRENT_MAKE_AND_MODEL")) != NULL
)
1122 * Scan for "close" matches...
1125 int match
, /* Current match */
1126 best_match
, /* Best match so far */
1127 count
; /* Number of drivers */
1128 const char *best
, /* Best matching string */
1129 *current
; /* Current string */
1132 count
= cgiGetSize("PPD_MAKE_AND_MODEL");
1134 for (i
= 0, best_match
= 0, best
= NULL
; i
< count
; i
++)
1136 current
= cgiGetArray("PPD_MAKE_AND_MODEL", i
);
1137 match
= match_string(make_model
, current
);
1139 if (match
> best_match
)
1146 if (best_match
> strlen(var
))
1149 * Found a match longer than the make...
1152 cgiSetVariable("CURRENT_MAKE_AND_MODEL", best
);
1156 cgiStartHTML(title
);
1157 cgiCopyTemplateLang("choose-model.tmpl");
1161 ippDelete(response
);
1165 cgiStartHTML(title
);
1166 cgiShowIPPError(_("Unable to get list of printer drivers:"));
1167 cgiCopyTemplateLang("error.tmpl");
1174 * Build a CUPS_ADD_PRINTER request, which requires the following
1177 * attributes-charset
1178 * attributes-natural-language
1184 * printer-is-accepting-jobs
1189 request
= ippNewRequest(CUPS_ADD_PRINTER
);
1191 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1192 "localhost", 0, "/printers/%s",
1193 cgiGetVariable("PRINTER_NAME"));
1194 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1197 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-location",
1198 NULL
, cgiGetVariable("PRINTER_LOCATION"));
1200 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info",
1201 NULL
, cgiGetVariable("PRINTER_INFO"));
1205 var
= cgiGetVariable("PPD_NAME");
1206 if (strcmp(var
, "__no_change__"))
1207 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "ppd-name",
1211 strlcpy(uri
, cgiGetVariable("DEVICE_URI"), sizeof(uri
));
1214 * Strip make and model from URI...
1217 if ((uriptr
= strrchr(uri
, '|')) != NULL
)
1220 if (!strncmp(uri
, "serial:", 7))
1223 * Update serial port URI to include baud rate, etc.
1226 if ((uriptr
= strchr(uri
, '?')) == NULL
)
1227 uriptr
= uri
+ strlen(uri
);
1229 snprintf(uriptr
, sizeof(uri
) - (uriptr
- uri
),
1230 "?baud=%s+bits=%s+parity=%s+flow=%s",
1231 cgiGetVariable("BAUDRATE"), cgiGetVariable("BITS"),
1232 cgiGetVariable("PARITY"), cgiGetVariable("FLOW"));
1235 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri",
1238 ippAddBoolean(request
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", 1);
1240 var
= cgiGetVariable("printer_is_shared");
1241 ippAddBoolean(request
, IPP_TAG_PRINTER
, "printer-is-shared",
1242 var
&& (!strcmp(var
, "1") || !strcmp(var
, "on")));
1244 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
1248 * Do the request and get back a response...
1252 ippDelete(cupsDoFileRequest(http
, request
, "/admin/", file
->tempfile
));
1254 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1256 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
1258 puts("Status: 401\n");
1261 else if (cupsLastError() > IPP_OK_CONFLICT
)
1263 cgiStartHTML(title
);
1264 cgiShowIPPError(modify
? _("Unable to modify printer:") :
1265 _("Unable to add printer:"));
1270 * Redirect successful updates back to the printer page...
1273 char refresh
[1024]; /* Refresh URL */
1276 cgiFormEncode(uri
, name
, sizeof(uri
));
1278 snprintf(refresh
, sizeof(refresh
),
1279 "5;/admin/?OP=redirect&URL=/printers/%s", uri
);
1281 cgiSetVariable("refresh_page", refresh
);
1283 cgiStartHTML(title
);
1285 cgiCopyTemplateLang("printer-modified.tmpl");
1290 * Set the printer options...
1293 cgiSetVariable("OP", "set-printer-options");
1294 do_set_options(http
, 0);
1307 * 'do_cancel_subscription()' - Cancel a subscription.
1311 do_cancel_subscription(http_t
*http
)/* I - HTTP connection */
1313 ipp_t
*request
; /* IPP request data */
1314 const char *var
, /* Form variable */
1315 *user
; /* Username */
1316 int id
; /* Subscription ID */
1320 * See if we have all of the required information...
1323 if ((var
= cgiGetVariable("NOTIFY_SUBSCRIPTION_ID")) != NULL
)
1330 cgiSetVariable("ERROR", cgiText(_("Bad subscription ID!")));
1331 cgiStartHTML(_("Cancel RSS Subscription"));
1332 cgiCopyTemplateLang("error.tmpl");
1338 * Require a username...
1341 if ((user
= getenv("REMOTE_USER")) == NULL
)
1343 puts("Status: 401\n");
1348 * Cancel the subscription...
1351 request
= ippNewRequest(IPP_CANCEL_SUBSCRIPTION
);
1353 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1354 NULL
, "ipp://localhost/");
1355 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
1356 "notify-subscription-id", id
);
1358 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1361 ippDelete(cupsDoRequest(http
, request
, "/"));
1363 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
1365 puts("Status: 401\n");
1368 else if (cupsLastError() > IPP_OK_CONFLICT
)
1370 cgiStartHTML(_("Cancel RSS Subscription"));
1371 cgiShowIPPError(_("Unable to cancel RSS subscription:"));
1376 * Redirect successful updates back to the admin page...
1379 cgiSetVariable("refresh_page", "5;URL=/admin");
1380 cgiStartHTML(_("Cancel RSS Subscription"));
1381 cgiCopyTemplateLang("subscription-canceled.tmpl");
1389 * 'do_config_server()' - Configure server settings.
1393 do_config_server(http_t
*http
) /* I - HTTP connection */
1395 if (cgiGetVariable("CHANGESETTINGS"))
1398 * Save basic setting changes...
1401 int num_settings
; /* Number of server settings */
1402 cups_option_t
*settings
; /* Server settings */
1403 const char *debug_logging
, /* DEBUG_LOGGING value */
1404 *remote_admin
, /* REMOTE_ADMIN value */
1405 *remote_any
, /* REMOTE_ANY value */
1407 /* REMOTE_PRINTERS value */
1408 *share_printers
,/* SHARE_PRINTERS value */
1410 /* USER_CANCEL_ANY value */
1412 char default_auth_type
[255];
1413 /* DefaultAuthType value */
1414 #endif /* HAVE_GSSAPI */
1418 * Get the checkbox values from the form...
1421 debug_logging
= cgiGetVariable("DEBUG_LOGGING") ? "1" : "0";
1422 remote_admin
= cgiGetVariable("REMOTE_ADMIN") ? "1" : "0";
1423 remote_any
= cgiGetVariable("REMOTE_ANY") ? "1" : "0";
1424 remote_printers
= cgiGetVariable("REMOTE_PRINTERS") ? "1" : "0";
1425 share_printers
= cgiGetVariable("SHARE_PRINTERS") ? "1" : "0";
1426 user_cancel_any
= cgiGetVariable("USER_CANCEL_ANY") ? "1" : "0";
1429 * Get the current server settings...
1432 if (!cupsAdminGetServerSettings(http
, &num_settings
, &settings
))
1434 cgiStartHTML(cgiText(_("Change Settings")));
1435 cgiSetVariable("MESSAGE",
1436 cgiText(_("Unable to change server settings:")));
1437 cgiSetVariable("ERROR", cupsLastErrorString());
1438 cgiCopyTemplateLang("error.tmpl");
1445 * Get authentication settings...
1448 if (cgiGetVariable("KERBEROS"))
1449 strlcpy(default_auth_type
, "Negotiate", sizeof(default_auth_type
));
1452 const char *val
= cupsGetOption("DefaultAuthType", num_settings
,
1455 if (val
&& !strcasecmp(val
, "Negotiate"))
1456 strlcpy(default_auth_type
, "Basic", sizeof(default_auth_type
));
1458 strlcpy(default_auth_type
, val
, sizeof(default_auth_type
));
1461 fprintf(stderr
, "DEBUG: DefaultAuthType %s\n", default_auth_type
);
1462 #endif /* HAVE_GSSAPI */
1465 * See if the settings have changed...
1468 if (strcmp(debug_logging
, cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
,
1469 num_settings
, settings
)) ||
1470 strcmp(remote_admin
, cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
,
1471 num_settings
, settings
)) ||
1472 strcmp(remote_any
, cupsGetOption(CUPS_SERVER_REMOTE_ANY
,
1473 num_settings
, settings
)) ||
1474 strcmp(remote_printers
, cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
,
1475 num_settings
, settings
)) ||
1476 strcmp(share_printers
, cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
,
1477 num_settings
, settings
)) ||
1479 !cupsGetOption("DefaultAuthType", num_settings
, settings
) ||
1480 strcmp(default_auth_type
, cupsGetOption("DefaultAuthType",
1481 num_settings
, settings
)) ||
1482 #endif /* HAVE_GSSAPI */
1483 strcmp(user_cancel_any
, cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
,
1484 num_settings
, settings
)))
1487 * Settings *have* changed, so save the changes...
1490 cupsFreeOptions(num_settings
, settings
);
1493 num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1494 debug_logging
, num_settings
, &settings
);
1495 num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1496 remote_admin
, num_settings
, &settings
);
1497 num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
1498 remote_any
, num_settings
, &settings
);
1499 num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
1500 remote_printers
, num_settings
, &settings
);
1501 num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1502 share_printers
, num_settings
, &settings
);
1503 num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1504 user_cancel_any
, num_settings
, &settings
);
1506 num_settings
= cupsAddOption("DefaultAuthType", default_auth_type
,
1507 num_settings
, &settings
);
1508 #endif /* HAVE_GSSAPI */
1510 if (!cupsAdminSetServerSettings(http
, num_settings
, settings
))
1512 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
1514 puts("Status: 401\n");
1518 cgiStartHTML(cgiText(_("Change Settings")));
1519 cgiSetVariable("MESSAGE",
1520 cgiText(_("Unable to change server settings:")));
1521 cgiSetVariable("ERROR", cupsLastErrorString());
1522 cgiCopyTemplateLang("error.tmpl");
1526 cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect");
1527 cgiStartHTML(cgiText(_("Change Settings")));
1528 cgiCopyTemplateLang("restart.tmpl");
1537 cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect");
1538 cgiStartHTML(cgiText(_("Change Settings")));
1539 cgiCopyTemplateLang("norestart.tmpl");
1542 cupsFreeOptions(num_settings
, settings
);
1546 else if (cgiGetVariable("SAVECHANGES") && cgiGetVariable("CUPSDCONF"))
1549 * Save hand-edited config file...
1552 http_status_t status
; /* PUT status */
1553 char tempfile
[1024]; /* Temporary new cupsd.conf */
1554 int tempfd
; /* Temporary file descriptor */
1555 cups_file_t
*temp
; /* Temporary file */
1556 const char *start
, /* Start of line */
1557 *end
; /* End of line */
1561 * Create a temporary file for the new cupsd.conf file...
1564 if ((tempfd
= cupsTempFd(tempfile
, sizeof(tempfile
))) < 0)
1566 cgiStartHTML(cgiText(_("Edit Configuration File")));
1567 cgiSetVariable("MESSAGE", cgiText(_("Unable to create temporary file:")));
1568 cgiSetVariable("ERROR", strerror(errno
));
1569 cgiCopyTemplateLang("error.tmpl");
1576 if ((temp
= cupsFileOpenFd(tempfd
, "w")) == NULL
)
1578 cgiStartHTML(cgiText(_("Edit Configuration File")));
1579 cgiSetVariable("MESSAGE", cgiText(_("Unable to create temporary file:")));
1580 cgiSetVariable("ERROR", strerror(errno
));
1581 cgiCopyTemplateLang("error.tmpl");
1591 * Copy the cupsd.conf text from the form variable...
1594 start
= cgiGetVariable("CUPSDCONF");
1597 if ((end
= strstr(start
, "\r\n")) == NULL
)
1598 if ((end
= strstr(start
, "\n")) == NULL
)
1599 end
= start
+ strlen(start
);
1601 cupsFileWrite(temp
, start
, end
- start
);
1602 cupsFilePutChar(temp
, '\n');
1606 else if (*end
== '\n')
1612 cupsFileClose(temp
);
1615 * Upload the configuration file to the server...
1618 status
= cupsPutFile(http
, "/admin/conf/cupsd.conf", tempfile
);
1620 if (status
== HTTP_UNAUTHORIZED
)
1622 puts("Status: 401\n");
1626 else if (status
!= HTTP_CREATED
)
1628 cgiSetVariable("MESSAGE",
1629 cgiText(_("Unable to upload cupsd.conf file:")));
1630 cgiSetVariable("ERROR", httpStatus(status
));
1632 cgiStartHTML(cgiText(_("Edit Configuration File")));
1633 cgiCopyTemplateLang("error.tmpl");
1637 cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect");
1639 cgiStartHTML(cgiText(_("Edit Configuration File")));
1640 cgiCopyTemplateLang("restart.tmpl");
1649 struct stat info
; /* cupsd.conf information */
1650 cups_file_t
*cupsd
; /* cupsd.conf file */
1651 char *buffer
, /* Buffer for entire file */
1652 *bufptr
, /* Pointer into buffer */
1653 *bufend
; /* End of buffer */
1654 int ch
; /* Character from file */
1655 char filename
[1024]; /* Filename */
1656 const char *server_root
; /* Location of config files */
1660 * Locate the cupsd.conf file...
1663 if ((server_root
= getenv("CUPS_SERVERROOT")) == NULL
)
1664 server_root
= CUPS_SERVERROOT
;
1666 snprintf(filename
, sizeof(filename
), "%s/cupsd.conf", server_root
);
1669 * Figure out the size...
1672 if (stat(filename
, &info
))
1674 cgiStartHTML(cgiText(_("Edit Configuration File")));
1675 cgiSetVariable("MESSAGE",
1676 cgiText(_("Unable to access cupsd.conf file:")));
1677 cgiSetVariable("ERROR", strerror(errno
));
1678 cgiCopyTemplateLang("error.tmpl");
1685 if (info
.st_size
> (1024 * 1024))
1687 cgiStartHTML(cgiText(_("Edit Configuration File")));
1688 cgiSetVariable("MESSAGE",
1689 cgiText(_("Unable to access cupsd.conf file:")));
1690 cgiSetVariable("ERROR",
1691 cgiText(_("Unable to edit cupsd.conf files larger than "
1693 cgiCopyTemplateLang("error.tmpl");
1696 fprintf(stderr
, "ERROR: \"%s\" too large (%ld) to edit!\n", filename
,
1697 (long)info
.st_size
);
1702 * Open the cupsd.conf file...
1705 if ((cupsd
= cupsFileOpen(filename
, "r")) == NULL
)
1708 * Unable to open - log an error...
1711 cgiStartHTML(cgiText(_("Edit Configuration File")));
1712 cgiSetVariable("MESSAGE",
1713 cgiText(_("Unable to access cupsd.conf file:")));
1714 cgiSetVariable("ERROR", strerror(errno
));
1715 cgiCopyTemplateLang("error.tmpl");
1723 * Allocate memory and load the file into a string buffer...
1726 if ((buffer
= calloc(1, info
.st_size
+ 1)) != NULL
)
1728 cupsFileRead(cupsd
, buffer
, info
.st_size
);
1729 cgiSetVariable("CUPSDCONF", buffer
);
1733 cupsFileClose(cupsd
);
1736 * Then get the default cupsd.conf file and put that into a string as
1740 strlcat(filename
, ".default", sizeof(filename
));
1742 if (!stat(filename
, &info
) && info
.st_size
< (1024 * 1024) &&
1743 (cupsd
= cupsFileOpen(filename
, "r")) != NULL
)
1745 if ((buffer
= calloc(1, 2 * info
.st_size
+ 1)) != NULL
)
1747 bufend
= buffer
+ 2 * info
.st_size
- 1;
1749 for (bufptr
= buffer
;
1750 bufptr
< bufend
&& (ch
= cupsFileGetChar(cupsd
)) != EOF
;)
1752 if (ch
== '\\' || ch
== '\"')
1757 else if (ch
== '\n')
1762 else if (ch
== '\t')
1773 cgiSetVariable("CUPSDCONF_DEFAULT", buffer
);
1777 cupsFileClose(cupsd
);
1781 * Show the current config file...
1784 cgiStartHTML(cgiText(_("Edit Configuration File")));
1786 cgiCopyTemplateLang("edit-config.tmpl");
1794 * 'do_delete_class()' - Delete a class.
1798 do_delete_class(http_t
*http
) /* I - HTTP connection */
1800 ipp_t
*request
; /* IPP request */
1801 char uri
[HTTP_MAX_URI
]; /* Job URI */
1802 const char *pclass
; /* Printer class name */
1806 * Get form variables...
1809 if (cgiGetVariable("CONFIRM") == NULL
)
1811 cgiStartHTML(cgiText(_("Delete Class")));
1812 cgiCopyTemplateLang("class-confirm.tmpl");
1817 if ((pclass
= cgiGetVariable("PRINTER_NAME")) != NULL
)
1818 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1819 "localhost", 0, "/classes/%s", pclass
);
1822 cgiStartHTML(cgiText(_("Delete Class")));
1823 cgiSetVariable("ERROR", cgiText(_("Missing form variable!")));
1824 cgiCopyTemplateLang("error.tmpl");
1830 * Build a CUPS_DELETE_CLASS request, which requires the following
1833 * attributes-charset
1834 * attributes-natural-language
1838 request
= ippNewRequest(CUPS_DELETE_CLASS
);
1840 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1844 * Do the request and get back a response...
1847 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1850 * Show the results...
1853 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
1855 puts("Status: 401\n");
1858 else if (cupsLastError() <= IPP_OK_CONFLICT
)
1861 * Redirect successful updates back to the classes page...
1864 cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect&URL=/classes");
1867 cgiStartHTML(cgiText(_("Delete Class")));
1869 if (cupsLastError() > IPP_OK_CONFLICT
)
1870 cgiShowIPPError(_("Unable to delete class:"));
1872 cgiCopyTemplateLang("class-deleted.tmpl");
1879 * 'do_delete_printer()' - Delete a printer.
1883 do_delete_printer(http_t
*http
) /* I - HTTP connection */
1885 ipp_t
*request
; /* IPP request */
1886 char uri
[HTTP_MAX_URI
]; /* Job URI */
1887 const char *printer
; /* Printer printer name */
1891 * Get form variables...
1894 if (cgiGetVariable("CONFIRM") == NULL
)
1896 cgiStartHTML(cgiText(_("Delete Printer")));
1897 cgiCopyTemplateLang("printer-confirm.tmpl");
1902 if ((printer
= cgiGetVariable("PRINTER_NAME")) != NULL
)
1903 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1904 "localhost", 0, "/printers/%s", printer
);
1907 cgiStartHTML(cgiText(_("Delete Printer")));
1908 cgiSetVariable("ERROR", cgiText(_("Missing form variable!")));
1909 cgiCopyTemplateLang("error.tmpl");
1915 * Build a CUPS_DELETE_PRINTER request, which requires the following
1918 * attributes-charset
1919 * attributes-natural-language
1923 request
= ippNewRequest(CUPS_DELETE_PRINTER
);
1925 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1929 * Do the request and get back a response...
1932 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1935 * Show the results...
1938 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
1940 puts("Status: 401\n");
1943 else if (cupsLastError() <= IPP_OK_CONFLICT
)
1946 * Redirect successful updates back to the printers page...
1949 cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect&URL=/printers");
1952 cgiStartHTML(cgiText(_("Delete Printer")));
1954 if (cupsLastError() > IPP_OK_CONFLICT
)
1955 cgiShowIPPError(_("Unable to delete printer:"));
1957 cgiCopyTemplateLang("printer-deleted.tmpl");
1964 * 'do_export()' - Export printers to Samba.
1968 do_export(http_t
*http
) /* I - HTTP connection */
1970 int i
, j
; /* Looping vars */
1971 ipp_t
*request
, /* IPP request */
1972 *response
; /* IPP response */
1973 const char *username
, /* Samba username */
1974 *password
, /* Samba password */
1975 *export_all
; /* Export all printers? */
1976 int export_count
, /* Number of printers to export */
1977 printer_count
; /* Number of available printers */
1978 const char *name
, /* What name to pull */
1979 *dest
; /* Current destination */
1980 char ppd
[1024]; /* PPD file */
1987 username
= cgiGetVariable("USERNAME");
1988 password
= cgiGetVariable("PASSWORD");
1989 export_all
= cgiGetVariable("EXPORT_ALL");
1990 export_count
= cgiGetSize("EXPORT_NAME");
1993 * Get list of available printers...
1996 cgiSetSize("PRINTER_NAME", 0);
1997 cgiSetSize("PRINTER_EXPORT", 0);
1999 request
= ippNewRequest(CUPS_GET_PRINTERS
);
2001 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
2004 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
2005 "printer-type-mask", CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
2006 CUPS_PRINTER_IMPLICIT
);
2008 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2009 "requested-attributes", NULL
, "printer-name");
2011 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
2013 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
2014 ippDelete(response
);
2018 printer_count
= cgiGetSize("PRINTER_NAME");
2020 for (i
= 0; i
< printer_count
; i
++)
2022 dest
= cgiGetArray("PRINTER_NAME", i
);
2024 for (j
= 0; j
< export_count
; j
++)
2025 if (!strcasecmp(dest
, cgiGetArray("EXPORT_NAME", j
)))
2028 cgiSetArray("PRINTER_EXPORT", i
, j
< export_count
? "Y" : "");
2034 * Export or get the printers to export...
2037 if (username
&& *username
&& password
&& *password
&&
2038 (export_all
|| export_count
> 0))
2044 fputs("DEBUG: Export printers...\n", stderr
);
2048 name
= "PRINTER_NAME";
2049 export_count
= cgiGetSize("PRINTER_NAME");
2052 name
= "EXPORT_NAME";
2054 for (i
= 0; i
< export_count
; i
++)
2056 dest
= cgiGetArray(name
, i
);
2058 if (!cupsAdminCreateWindowsPPD(http
, dest
, ppd
, sizeof(ppd
)))
2061 j
= cupsAdminExportSamba(dest
, ppd
, "localhost", username
, password
,
2070 if (i
< export_count
)
2071 cgiSetVariable("ERROR", cupsLastErrorString());
2074 cgiStartHTML(cgiText(_("Export Printers to Samba")));
2075 cgiCopyTemplateLang("samba-exported.tmpl");
2080 else if (username
&& !*username
)
2081 cgiSetVariable("ERROR",
2082 cgiText(_("A Samba username is required to export "
2083 "printer drivers!")));
2084 else if (username
&& (!password
|| !*password
))
2085 cgiSetVariable("ERROR",
2086 cgiText(_("A Samba password is required to export "
2087 "printer drivers!")));
2093 cgiStartHTML(cgiText(_("Export Printers to Samba")));
2094 cgiCopyTemplateLang("samba-export.tmpl");
2100 * 'do_list_printers()' - List available printers.
2104 do_list_printers(http_t
*http
) /* I - HTTP connection */
2106 ipp_t
*request
, /* IPP request */
2107 *response
; /* IPP response */
2108 ipp_attribute_t
*attr
; /* IPP attribute */
2111 cgiStartHTML(cgiText(_("List Available Printers")));
2115 * Get the list of printers and their devices...
2118 request
= ippNewRequest(CUPS_GET_PRINTERS
);
2120 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2121 "requested-attributes", NULL
, "device-uri");
2123 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type",
2124 CUPS_PRINTER_LOCAL
);
2125 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type-mask",
2126 CUPS_PRINTER_LOCAL
);
2128 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
2131 * Got the printer list, now load the devices...
2134 int i
; /* Looping var */
2135 cups_array_t
*printer_devices
; /* Printer devices for local printers */
2136 char *printer_device
; /* Current printer device */
2140 * Allocate an array and copy the device strings...
2143 printer_devices
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
2145 for (attr
= ippFindAttribute(response
, "device-uri", IPP_TAG_URI
);
2147 attr
= ippFindNextAttribute(response
, "device-uri", IPP_TAG_URI
))
2149 cupsArrayAdd(printer_devices
, strdup(attr
->values
[0].string
.text
));
2153 * Free the printer list and get the device list...
2156 ippDelete(response
);
2158 request
= ippNewRequest(CUPS_GET_DEVICES
);
2160 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
2163 * Got the device list, let's parse it...
2166 const char *device_uri
, /* device-uri attribute value */
2167 *device_make_and_model
, /* device-make-and-model value */
2168 *device_info
; /* device-info value */
2171 for (i
= 0, attr
= response
->attrs
; attr
; attr
= attr
->next
)
2174 * Skip leading attributes until we hit a device...
2177 while (attr
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
2184 * Pull the needed attributes from this device...
2188 device_make_and_model
= NULL
;
2191 while (attr
&& attr
->group_tag
== IPP_TAG_PRINTER
)
2193 if (!strcmp(attr
->name
, "device-info") &&
2194 attr
->value_tag
== IPP_TAG_TEXT
)
2195 device_info
= attr
->values
[0].string
.text
;
2197 if (!strcmp(attr
->name
, "device-make-and-model") &&
2198 attr
->value_tag
== IPP_TAG_TEXT
)
2199 device_make_and_model
= attr
->values
[0].string
.text
;
2201 if (!strcmp(attr
->name
, "device-uri") &&
2202 attr
->value_tag
== IPP_TAG_URI
)
2203 device_uri
= attr
->values
[0].string
.text
;
2209 * See if we have everything needed...
2212 if (device_info
&& device_make_and_model
&& device_uri
&&
2213 strcasecmp(device_make_and_model
, "unknown") &&
2214 strchr(device_uri
, ':'))
2217 * Yes, now see if there is already a printer for this
2221 if (!cupsArrayFind(printer_devices
, (void *)device_uri
))
2224 * Not found, so it must be a new printer...
2227 char option
[1024], /* Form variables for this device */
2228 *option_ptr
; /* Pointer into string */
2229 const char *ptr
; /* Pointer into device string */
2233 * Format the printer name variable for this device...
2235 * We use the device-info string first, then device-uri,
2236 * and finally device-make-and-model to come up with a
2240 if (strncasecmp(device_info
, "unknown", 7))
2242 else if ((ptr
= strstr(device_uri
, "://")) != NULL
)
2245 ptr
= device_make_and_model
;
2247 for (option_ptr
= option
;
2248 option_ptr
< (option
+ sizeof(option
) - 1) && *ptr
;
2250 if (isalnum(*ptr
& 255) || *ptr
== '_' || *ptr
== '-' ||
2252 *option_ptr
++ = *ptr
;
2253 else if ((*ptr
== ' ' || *ptr
== '/') && option_ptr
[-1] != '_')
2254 *option_ptr
++ = '_';
2255 else if (*ptr
== '?' || *ptr
== '(')
2260 cgiSetArray("TEMPLATE_NAME", i
, option
);
2263 * Finally, set the form variables for this printer...
2266 cgiSetArray("device_info", i
, device_info
);
2267 cgiSetArray("device_make_and_model", i
, device_make_and_model
);
2268 cgiSetArray("device_uri", i
, device_uri
);
2277 ippDelete(response
);
2280 * Free the device list...
2283 for (printer_device
= (char *)cupsArrayFirst(printer_devices
);
2285 printer_device
= (char *)cupsArrayNext(printer_devices
))
2286 free(printer_device
);
2288 cupsArrayDelete(printer_devices
);
2293 * Finally, show the printer list...
2296 cgiCopyTemplateLang("list-available-printers.tmpl");
2303 * 'do_menu()' - Show the main menu.
2307 do_menu(http_t
*http
) /* I - HTTP connection */
2309 int num_settings
; /* Number of server settings */
2310 cups_option_t
*settings
; /* Server settings */
2311 const char *val
; /* Setting value */
2312 char filename
[1024]; /* Temporary filename */
2313 const char *datadir
; /* Location of data files */
2314 ipp_t
*request
, /* IPP request */
2315 *response
; /* IPP response */
2319 * Get the current server settings...
2322 if (!cupsAdminGetServerSettings(http
, &num_settings
, &settings
))
2324 cgiSetVariable("SETTINGS_MESSAGE",
2325 cgiText(_("Unable to open cupsd.conf file:")));
2326 cgiSetVariable("SETTINGS_ERROR", cupsLastErrorString());
2329 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, num_settings
,
2330 settings
)) != NULL
&& atoi(val
))
2331 cgiSetVariable("DEBUG_LOGGING", "CHECKED");
2333 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, num_settings
,
2334 settings
)) != NULL
&& atoi(val
))
2335 cgiSetVariable("REMOTE_ADMIN", "CHECKED");
2337 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, num_settings
,
2338 settings
)) != NULL
&& atoi(val
))
2339 cgiSetVariable("REMOTE_ANY", "CHECKED");
2341 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
, num_settings
,
2342 settings
)) != NULL
&& atoi(val
))
2343 cgiSetVariable("REMOTE_PRINTERS", "CHECKED");
2345 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, num_settings
,
2346 settings
)) != NULL
&& atoi(val
))
2347 cgiSetVariable("SHARE_PRINTERS", "CHECKED");
2349 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, num_settings
,
2350 settings
)) != NULL
&& atoi(val
))
2351 cgiSetVariable("USER_CANCEL_ANY", "CHECKED");
2354 cgiSetVariable("HAVE_GSSAPI", "1");
2356 if ((val
= cupsGetOption("DefaultAuthType", num_settings
,
2357 settings
)) != NULL
&& !strcasecmp(val
, "Negotiate"))
2358 cgiSetVariable("KERBEROS", "CHECKED");
2359 #endif /* HAVE_GSSAPI */
2361 cupsFreeOptions(num_settings
, settings
);
2364 * See if Samba and the Windows drivers are installed...
2367 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
2368 datadir
= CUPS_DATADIR
;
2370 snprintf(filename
, sizeof(filename
), "%s/drivers/pscript5.dll", datadir
);
2371 if (!access(filename
, R_OK
))
2374 * Found Windows 2000 driver file, see if we have smbclient and
2378 if (cupsFileFind("smbclient", getenv("PATH"), 1, filename
,
2379 sizeof(filename
)) &&
2380 cupsFileFind("rpcclient", getenv("PATH"), 1, filename
,
2382 cgiSetVariable("HAVE_SAMBA", "Y");
2385 if (!cupsFileFind("smbclient", getenv("PATH"), 1, filename
,
2387 fputs("ERROR: smbclient not found!\n", stderr
);
2389 if (!cupsFileFind("rpcclient", getenv("PATH"), 1, filename
,
2391 fputs("ERROR: rpcclient not found!\n", stderr
);
2401 request
= ippNewRequest(IPP_GET_SUBSCRIPTIONS
);
2403 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2404 NULL
, "ipp://localhost/");
2406 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
2408 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
2409 ippDelete(response
);
2413 * Finally, show the main menu template...
2416 cgiStartHTML(cgiText(_("Administration")));
2418 cgiCopyTemplateLang("admin.tmpl");
2425 * 'do_printer_op()' - Do a printer operation.
2429 do_printer_op(http_t
*http
, /* I - HTTP connection */
2430 ipp_op_t op
, /* I - Operation to perform */
2431 const char *title
) /* I - Title of page */
2433 ipp_t
*request
; /* IPP request */
2434 char uri
[HTTP_MAX_URI
]; /* Printer URI */
2435 const char *printer
, /* Printer name (purge-jobs) */
2436 *is_class
; /* Is a class? */
2439 is_class
= cgiGetVariable("IS_CLASS");
2440 printer
= cgiGetVariable("PRINTER_NAME");
2444 cgiSetVariable("ERROR", cgiText(_("Missing form variable!")));
2445 cgiStartHTML(title
);
2446 cgiCopyTemplateLang("error.tmpl");
2452 * Build a printer request, which requires the following
2455 * attributes-charset
2456 * attributes-natural-language
2460 request
= ippNewRequest(op
);
2462 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2463 "localhost", 0, is_class
? "/classes/%s" : "/printers/%s",
2465 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2469 * Do the request and get back a response...
2472 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
2474 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
2476 puts("Status: 401\n");
2479 else if (cupsLastError() > IPP_OK_CONFLICT
)
2481 cgiStartHTML(title
);
2482 cgiShowIPPError(_("Unable to change printer:"));
2487 * Redirect successful updates back to the printer page...
2490 char url
[1024], /* Printer/class URL */
2491 refresh
[1024]; /* Refresh URL */
2494 cgiRewriteURL(uri
, url
, sizeof(url
), NULL
);
2495 cgiFormEncode(uri
, url
, sizeof(uri
));
2496 snprintf(refresh
, sizeof(refresh
), "5;URL=/admin/?OP=redirect&URL=%s", uri
);
2497 cgiSetVariable("refresh_page", refresh
);
2499 cgiStartHTML(title
);
2501 if (op
== IPP_PAUSE_PRINTER
)
2502 cgiCopyTemplateLang("printer-stop.tmpl");
2503 else if (op
== IPP_RESUME_PRINTER
)
2504 cgiCopyTemplateLang("printer-start.tmpl");
2505 else if (op
== CUPS_ACCEPT_JOBS
)
2506 cgiCopyTemplateLang("printer-accept.tmpl");
2507 else if (op
== CUPS_REJECT_JOBS
)
2508 cgiCopyTemplateLang("printer-reject.tmpl");
2509 else if (op
== IPP_PURGE_JOBS
)
2510 cgiCopyTemplateLang("printer-purge.tmpl");
2511 else if (op
== CUPS_SET_DEFAULT
)
2512 cgiCopyTemplateLang("printer-default.tmpl");
2520 * 'do_set_allowed_users()' - Set the allowed/denied users for a queue.
2524 do_set_allowed_users(http_t
*http
) /* I - HTTP connection */
2526 int i
; /* Looping var */
2527 ipp_t
*request
, /* IPP request */
2528 *response
; /* IPP response */
2529 char uri
[HTTP_MAX_URI
]; /* Printer URI */
2530 const char *printer
, /* Printer name (purge-jobs) */
2531 *is_class
, /* Is a class? */
2532 *users
, /* List of users or groups */
2533 *type
; /* Allow/deny type */
2534 int num_users
; /* Number of users */
2535 char *ptr
, /* Pointer into users string */
2536 *end
, /* Pointer to end of users string */
2537 quote
; /* Quote character */
2538 ipp_attribute_t
*attr
; /* Attribute */
2539 static const char * const attrs
[] = /* Requested attributes */
2541 "requesting-user-name-allowed",
2542 "requesting-user-name-denied"
2546 is_class
= cgiGetVariable("IS_CLASS");
2547 printer
= cgiGetVariable("PRINTER_NAME");
2551 cgiSetVariable("ERROR", cgiText(_("Missing form variable!")));
2552 cgiStartHTML(cgiText(_("Set Allowed Users")));
2553 cgiCopyTemplateLang("error.tmpl");
2558 users
= cgiGetVariable("users");
2559 type
= cgiGetVariable("type");
2561 if (!users
|| !type
||
2562 (strcmp(type
, "requesting-user-name-allowed") &&
2563 strcmp(type
, "requesting-user-name-denied")))
2566 * Build a Get-Printer-Attributes request, which requires the following
2569 * attributes-charset
2570 * attributes-natural-language
2572 * requested-attributes
2575 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
2577 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2578 "localhost", 0, is_class
? "/classes/%s" : "/printers/%s",
2580 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2583 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2584 "requested-attributes",
2585 (int)(sizeof(attrs
) / sizeof(attrs
[0])), NULL
, attrs
);
2588 * Do the request and get back a response...
2591 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
2593 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
2595 ippDelete(response
);
2598 cgiStartHTML(cgiText(_("Set Allowed Users")));
2600 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
2602 puts("Status: 401\n");
2605 else if (cupsLastError() > IPP_OK_CONFLICT
)
2606 cgiShowIPPError(_("Unable to get printer attributes:"));
2608 cgiCopyTemplateLang("users.tmpl");
2615 * Save the changes...
2618 for (num_users
= 0, ptr
= (char *)users
; *ptr
; num_users
++)
2621 * Skip whitespace and commas...
2624 while (*ptr
== ',' || isspace(*ptr
& 255))
2627 if (*ptr
== '\'' || *ptr
== '\"')
2630 * Scan quoted name...
2635 for (end
= ptr
; *end
; end
++)
2642 * Scan space or comma-delimited name...
2645 for (end
= ptr
; *end
; end
++)
2646 if (isspace(*end
& 255) || *end
== ',')
2651 * Advance to the next name...
2658 * Build a CUPS-Add-Printer/Class request, which requires the following
2661 * attributes-charset
2662 * attributes-natural-language
2664 * requesting-user-name-{allowed,denied}
2667 request
= ippNewRequest(is_class
? CUPS_ADD_CLASS
: CUPS_ADD_PRINTER
);
2669 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2670 "localhost", 0, is_class
? "/classes/%s" : "/printers/%s",
2672 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2676 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2677 "requesting-user-name-allowed", NULL
, "all");
2680 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2681 type
, num_users
, NULL
, NULL
);
2683 for (i
= 0, ptr
= (char *)users
; *ptr
; i
++)
2686 * Skip whitespace and commas...
2689 while (*ptr
== ',' || isspace(*ptr
& 255))
2692 if (*ptr
== '\'' || *ptr
== '\"')
2695 * Scan quoted name...
2700 for (end
= ptr
; *end
; end
++)
2707 * Scan space or comma-delimited name...
2710 for (end
= ptr
; *end
; end
++)
2711 if (isspace(*end
& 255) || *end
== ',')
2716 * Terminate the name...
2726 attr
->values
[i
].string
.text
= strdup(ptr
);
2729 * Advance to the next name...
2737 * Do the request and get back a response...
2740 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
2742 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
2744 puts("Status: 401\n");
2747 else if (cupsLastError() > IPP_OK_CONFLICT
)
2749 cgiStartHTML(cgiText(_("Set Allowed Users")));
2750 cgiShowIPPError(_("Unable to change printer:"));
2755 * Redirect successful updates back to the printer page...
2758 char url
[1024], /* Printer/class URL */
2759 refresh
[1024]; /* Refresh URL */
2762 cgiRewriteURL(uri
, url
, sizeof(url
), NULL
);
2763 cgiFormEncode(uri
, url
, sizeof(uri
));
2764 snprintf(refresh
, sizeof(refresh
), "5;URL=/admin/?OP=redirect&URL=%s",
2766 cgiSetVariable("refresh_page", refresh
);
2768 cgiStartHTML(cgiText(_("Set Allowed Users")));
2770 cgiCopyTemplateLang(is_class
? "class-modified.tmpl" :
2771 "printer-modified.tmpl");
2780 * 'do_set_options()' - Configure the default options for a queue.
2784 do_set_options(http_t
*http
, /* I - HTTP connection */
2785 int is_class
) /* I - Set options for class? */
2787 int i
, j
, k
, m
; /* Looping vars */
2788 int have_options
; /* Have options? */
2789 ipp_t
*request
, /* IPP request */
2790 *response
; /* IPP response */
2791 ipp_attribute_t
*attr
; /* IPP attribute */
2792 char uri
[HTTP_MAX_URI
]; /* Job URI */
2793 const char *var
; /* Variable value */
2794 const char *printer
; /* Printer printer name */
2795 const char *filename
; /* PPD filename */
2796 char tempfile
[1024]; /* Temporary filename */
2797 cups_file_t
*in
, /* Input file */
2798 *out
; /* Output file */
2799 char line
[1024], /* Line from PPD file */
2800 value
[1024], /* Option value */
2801 keyword
[1024], /* Keyword from Default line */
2802 *keyptr
; /* Pointer into keyword... */
2803 ppd_file_t
*ppd
; /* PPD file */
2804 ppd_group_t
*group
; /* Option group */
2805 ppd_option_t
*option
; /* Option */
2806 ppd_coption_t
*coption
; /* Custom option */
2807 ppd_cparam_t
*cparam
; /* Custom parameter */
2808 ppd_attr_t
*protocol
; /* cupsProtocol attribute */
2809 const char *title
; /* Page title */
2812 title
= cgiText(is_class
? _("Set Class Options") : _("Set Printer Options"));
2814 fprintf(stderr
, "DEBUG: do_set_options(http=%p, is_class=%d)\n", http
,
2818 * Get the printer name...
2821 if ((printer
= cgiGetVariable("PRINTER_NAME")) != NULL
)
2822 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2823 "localhost", 0, is_class
? "/classes/%s" : "/printers/%s",
2827 cgiSetVariable("ERROR", cgiText(_("Missing form variable!")));
2828 cgiStartHTML(title
);
2829 cgiCopyTemplateLang("error.tmpl");
2834 fprintf(stderr
, "DEBUG: printer=\"%s\", uri=\"%s\"...\n", printer
, uri
);
2837 * Get the PPD file...
2843 filename
= cupsGetPPD2(http
, printer
);
2847 fprintf(stderr
, "DEBUG: Got PPD file: \"%s\"\n", filename
);
2849 if ((ppd
= ppdOpenFile(filename
)) == NULL
)
2851 cgiSetVariable("ERROR", ppdErrorString(ppdLastError(&i
)));
2852 cgiSetVariable("MESSAGE", cgiText(_("Unable to open PPD file:")));
2853 cgiStartHTML(title
);
2854 cgiCopyTemplateLang("error.tmpl");
2861 fputs("DEBUG: No PPD file\n", stderr
);
2865 if (cgiGetVariable("job_sheets_start") != NULL
||
2866 cgiGetVariable("job_sheets_end") != NULL
)
2873 ppdMarkDefaults(ppd
);
2875 for (option
= ppdFirstOption(ppd
);
2877 option
= ppdNextOption(ppd
))
2878 if ((var
= cgiGetVariable(option
->keyword
)) != NULL
)
2881 ppdMarkOption(ppd
, option
->keyword
, var
);
2885 if (!have_options
|| ppdConflicts(ppd
))
2888 * Show the options to the user...
2891 fputs("DEBUG: Showing options...\n", stderr
);
2893 cgiStartHTML(cgiText(_("Set Printer Options")));
2894 cgiCopyTemplateLang("set-printer-options-header.tmpl");
2900 if (ppdConflicts(ppd
))
2902 for (i
= ppd
->num_groups
, k
= 0, group
= ppd
->groups
;
2905 for (j
= group
->num_options
, option
= group
->options
;
2908 if (option
->conflicted
)
2910 cgiSetArray("ckeyword", k
, option
->keyword
);
2911 cgiSetArray("ckeytext", k
, option
->text
);
2915 cgiCopyTemplateLang("option-conflict.tmpl");
2918 for (i
= ppd
->num_groups
, group
= ppd
->groups
;
2922 if (!strcmp(group
->name
, "InstallableOptions"))
2923 cgiSetVariable("GROUP", cgiText(_("Options Installed")));
2925 cgiSetVariable("GROUP", group
->text
);
2927 cgiCopyTemplateLang("option-header.tmpl");
2929 for (j
= group
->num_options
, option
= group
->options
;
2933 if (!strcmp(option
->keyword
, "PageRegion"))
2936 cgiSetVariable("KEYWORD", option
->keyword
);
2937 cgiSetVariable("KEYTEXT", option
->text
);
2939 if (option
->conflicted
)
2940 cgiSetVariable("CONFLICTED", "1");
2942 cgiSetVariable("CONFLICTED", "0");
2944 cgiSetSize("CHOICES", 0);
2945 cgiSetSize("TEXT", 0);
2946 for (k
= 0, m
= 0; k
< option
->num_choices
; k
++)
2948 cgiSetArray("CHOICES", m
, option
->choices
[k
].choice
);
2949 cgiSetArray("TEXT", m
, option
->choices
[k
].text
);
2953 if (option
->choices
[k
].marked
)
2954 cgiSetVariable("DEFCHOICE", option
->choices
[k
].choice
);
2957 cgiSetSize("PARAMS", 0);
2958 cgiSetSize("PARAMTEXT", 0);
2959 cgiSetSize("PARAMVALUE", 0);
2960 cgiSetSize("INPUTTYPE", 0);
2962 if ((coption
= ppdFindCustomOption(ppd
, option
->keyword
)))
2964 const char *units
= NULL
; /* Units value, if any */
2967 cgiSetVariable("ISCUSTOM", "1");
2969 for (cparam
= ppdFirstCustomParam(coption
), m
= 0;
2971 cparam
= ppdNextCustomParam(coption
), m
++)
2973 if (!strcasecmp(option
->keyword
, "PageSize") &&
2974 strcasecmp(cparam
->name
, "Width") &&
2975 strcasecmp(cparam
->name
, "Height"))
2981 cgiSetArray("PARAMS", m
, cparam
->name
);
2982 cgiSetArray("PARAMTEXT", m
, cparam
->text
);
2983 cgiSetArray("INPUTTYPE", m
, "text");
2985 switch (cparam
->type
)
2987 case PPD_CUSTOM_POINTS
:
2988 if (!strncasecmp(option
->defchoice
, "Custom.", 7))
2990 units
= option
->defchoice
+ strlen(option
->defchoice
) - 2;
2992 if (strcmp(units
, "mm") && strcmp(units
, "cm") &&
2993 strcmp(units
, "in") && strcmp(units
, "ft"))
2995 if (units
[1] == 'm')
3004 if (!strcmp(units
, "mm"))
3005 snprintf(value
, sizeof(value
), "%g",
3006 cparam
->current
.custom_points
/ 72.0 * 25.4);
3007 else if (!strcmp(units
, "cm"))
3008 snprintf(value
, sizeof(value
), "%g",
3009 cparam
->current
.custom_points
/ 72.0 * 2.54);
3010 else if (!strcmp(units
, "in"))
3011 snprintf(value
, sizeof(value
), "%g",
3012 cparam
->current
.custom_points
/ 72.0);
3013 else if (!strcmp(units
, "ft"))
3014 snprintf(value
, sizeof(value
), "%g",
3015 cparam
->current
.custom_points
/ 72.0 / 12.0);
3016 else if (!strcmp(units
, "m"))
3017 snprintf(value
, sizeof(value
), "%g",
3018 cparam
->current
.custom_points
/ 72.0 * 0.0254);
3020 snprintf(value
, sizeof(value
), "%g",
3021 cparam
->current
.custom_points
);
3022 cgiSetArray("PARAMVALUE", m
, value
);
3025 case PPD_CUSTOM_CURVE
:
3026 case PPD_CUSTOM_INVCURVE
:
3027 case PPD_CUSTOM_REAL
:
3028 snprintf(value
, sizeof(value
), "%g",
3029 cparam
->current
.custom_real
);
3030 cgiSetArray("PARAMVALUE", m
, value
);
3033 case PPD_CUSTOM_INT
:
3034 snprintf(value
, sizeof(value
), "%d",
3035 cparam
->current
.custom_int
);
3036 cgiSetArray("PARAMVALUE", m
, value
);
3039 case PPD_CUSTOM_PASSCODE
:
3040 case PPD_CUSTOM_PASSWORD
:
3041 if (cparam
->current
.custom_password
)
3042 cgiSetArray("PARAMVALUE", m
,
3043 cparam
->current
.custom_password
);
3045 cgiSetArray("PARAMVALUE", m
, "");
3046 cgiSetArray("INPUTTYPE", m
, "password");
3049 case PPD_CUSTOM_STRING
:
3050 if (cparam
->current
.custom_string
)
3051 cgiSetArray("PARAMVALUE", m
,
3052 cparam
->current
.custom_string
);
3054 cgiSetArray("PARAMVALUE", m
, "");
3061 cgiSetArray("PARAMS", m
, "Units");
3062 cgiSetArray("PARAMTEXT", m
, cgiText(_("Units")));
3063 cgiSetArray("PARAMVALUE", m
, units
);
3067 cgiSetVariable("ISCUSTOM", "0");
3071 case PPD_UI_BOOLEAN
:
3072 cgiCopyTemplateLang("option-boolean.tmpl");
3074 case PPD_UI_PICKONE
:
3075 cgiCopyTemplateLang("option-pickone.tmpl");
3077 case PPD_UI_PICKMANY
:
3078 cgiCopyTemplateLang("option-pickmany.tmpl");
3083 cgiCopyTemplateLang("option-trailer.tmpl");
3088 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
3089 * following attributes:
3091 * attributes-charset
3092 * attributes-natural-language
3096 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
3098 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3099 "localhost", 0, "/printers/%s", printer
);
3100 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
3104 * Do the request and get back a response...
3107 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
3109 if ((attr
= ippFindAttribute(response
, "job-sheets-supported",
3110 IPP_TAG_ZERO
)) != NULL
)
3113 * Add the job sheets options...
3116 cgiSetVariable("GROUP", cgiText(_("Banners")));
3117 cgiCopyTemplateLang("option-header.tmpl");
3119 cgiSetSize("CHOICES", attr
->num_values
);
3120 cgiSetSize("TEXT", attr
->num_values
);
3121 for (k
= 0; k
< attr
->num_values
; k
++)
3123 cgiSetArray("CHOICES", k
, attr
->values
[k
].string
.text
);
3124 cgiSetArray("TEXT", k
, attr
->values
[k
].string
.text
);
3127 attr
= ippFindAttribute(response
, "job-sheets-default", IPP_TAG_ZERO
);
3129 cgiSetVariable("KEYWORD", "job_sheets_start");
3130 cgiSetVariable("KEYTEXT", cgiText(_("Starting Banner")));
3131 cgiSetVariable("DEFCHOICE", attr
!= NULL
?
3132 attr
->values
[0].string
.text
: "");
3134 cgiCopyTemplateLang("option-pickone.tmpl");
3136 cgiSetVariable("KEYWORD", "job_sheets_end");
3137 cgiSetVariable("KEYTEXT", cgiText(_("Ending Banner")));
3138 cgiSetVariable("DEFCHOICE", attr
!= NULL
&& attr
->num_values
> 1 ?
3139 attr
->values
[1].string
.text
: "");
3141 cgiCopyTemplateLang("option-pickone.tmpl");
3143 cgiCopyTemplateLang("option-trailer.tmpl");
3146 if (ippFindAttribute(response
, "printer-error-policy-supported",
3148 ippFindAttribute(response
, "printer-op-policy-supported",
3152 * Add the error and operation policy options...
3155 cgiSetVariable("GROUP", cgiText(_("Policies")));
3156 cgiCopyTemplateLang("option-header.tmpl");
3162 attr
= ippFindAttribute(response
, "printer-error-policy-supported",
3167 cgiSetSize("CHOICES", attr
->num_values
);
3168 cgiSetSize("TEXT", attr
->num_values
);
3169 for (k
= 0; k
< attr
->num_values
; k
++)
3171 cgiSetArray("CHOICES", k
, attr
->values
[k
].string
.text
);
3172 cgiSetArray("TEXT", k
, attr
->values
[k
].string
.text
);
3175 attr
= ippFindAttribute(response
, "printer-error-policy",
3178 cgiSetVariable("KEYWORD", "printer_error_policy");
3179 cgiSetVariable("KEYTEXT", cgiText(_("Error Policy")));
3180 cgiSetVariable("DEFCHOICE", attr
== NULL
?
3181 "" : attr
->values
[0].string
.text
);
3184 cgiCopyTemplateLang("option-pickone.tmpl");
3187 * Operation policy...
3190 attr
= ippFindAttribute(response
, "printer-op-policy-supported",
3195 cgiSetSize("CHOICES", attr
->num_values
);
3196 cgiSetSize("TEXT", attr
->num_values
);
3197 for (k
= 0; k
< attr
->num_values
; k
++)
3199 cgiSetArray("CHOICES", k
, attr
->values
[k
].string
.text
);
3200 cgiSetArray("TEXT", k
, attr
->values
[k
].string
.text
);
3203 attr
= ippFindAttribute(response
, "printer-op-policy", IPP_TAG_ZERO
);
3205 cgiSetVariable("KEYWORD", "printer_op_policy");
3206 cgiSetVariable("KEYTEXT", cgiText(_("Operation Policy")));
3207 cgiSetVariable("DEFCHOICE", attr
== NULL
?
3208 "" : attr
->values
[0].string
.text
);
3210 cgiCopyTemplateLang("option-pickone.tmpl");
3213 cgiCopyTemplateLang("option-trailer.tmpl");
3216 ippDelete(response
);
3220 * Binary protocol support...
3223 if (ppd
&& ppd
->protocols
&& strstr(ppd
->protocols
, "BCP"))
3225 protocol
= ppdFindAttr(ppd
, "cupsProtocol", NULL
);
3227 cgiSetVariable("GROUP", cgiText(_("PS Binary Protocol")));
3228 cgiCopyTemplateLang("option-header.tmpl");
3230 cgiSetSize("CHOICES", 2);
3231 cgiSetSize("TEXT", 2);
3232 cgiSetArray("CHOICES", 0, "None");
3233 cgiSetArray("TEXT", 0, cgiText(_("None")));
3235 if (strstr(ppd
->protocols
, "TBCP"))
3237 cgiSetArray("CHOICES", 1, "TBCP");
3238 cgiSetArray("TEXT", 1, "TBCP");
3242 cgiSetArray("CHOICES", 1, "BCP");
3243 cgiSetArray("TEXT", 1, "BCP");
3246 cgiSetVariable("KEYWORD", "protocol");
3247 cgiSetVariable("KEYTEXT", cgiText(_("PS Binary Protocol")));
3248 cgiSetVariable("DEFCHOICE", protocol
? protocol
->value
: "None");
3250 cgiCopyTemplateLang("option-pickone.tmpl");
3252 cgiCopyTemplateLang("option-trailer.tmpl");
3255 cgiCopyTemplateLang("set-printer-options-trailer.tmpl");
3261 * Set default options...
3264 fputs("DEBUG: Setting options...\n", stderr
);
3268 out
= cupsTempFile2(tempfile
, sizeof(tempfile
));
3269 in
= cupsFileOpen(filename
, "r");
3273 cgiSetVariable("ERROR", strerror(errno
));
3274 cgiStartHTML(cgiText(_("Set Printer Options")));
3275 cgiCopyTemplateLang("error.tmpl");
3291 while (cupsFileGets(in
, line
, sizeof(line
)))
3293 if (!strncmp(line
, "*cupsProtocol:", 14) && cgiGetVariable("protocol"))
3295 else if (strncmp(line
, "*Default", 8))
3296 cupsFilePrintf(out
, "%s\n", line
);
3300 * Get default option name...
3303 strlcpy(keyword
, line
+ 8, sizeof(keyword
));
3305 for (keyptr
= keyword
; *keyptr
; keyptr
++)
3306 if (*keyptr
== ':' || isspace(*keyptr
& 255))
3311 if (!strcmp(keyword
, "PageRegion") ||
3312 !strcmp(keyword
, "PaperDimension") ||
3313 !strcmp(keyword
, "ImageableArea"))
3314 var
= get_option_value(ppd
, "PageSize", value
, sizeof(value
));
3316 var
= get_option_value(ppd
, keyword
, value
, sizeof(value
));
3319 cupsFilePrintf(out
, "%s\n", line
);
3321 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, var
);
3326 * TODO: We need to set the port-monitor attribute!
3329 if ((var
= cgiGetVariable("protocol")) != NULL
)
3330 cupsFilePrintf(out
, "*cupsProtocol: %s\n", var
);
3338 * Make sure temporary filename is cleared when there is no PPD...
3345 * Build a CUPS_ADD_MODIFY_CLASS/PRINTER request, which requires the
3346 * following attributes:
3348 * attributes-charset
3349 * attributes-natural-language
3351 * job-sheets-default
3352 * printer-error-policy
3357 request
= ippNewRequest(is_class
? CUPS_ADD_MODIFY_CLASS
:
3358 CUPS_ADD_MODIFY_PRINTER
);
3360 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
3363 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
3364 "job-sheets-default", 2, NULL
, NULL
);
3365 attr
->values
[0].string
.text
= _cupsStrAlloc(cgiGetVariable("job_sheets_start"));
3366 attr
->values
[1].string
.text
= _cupsStrAlloc(cgiGetVariable("job_sheets_end"));
3368 if ((var
= cgiGetVariable("printer_error_policy")) != NULL
)
3369 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
3370 "printer-error-policy", NULL
, var
);
3372 if ((var
= cgiGetVariable("printer_op_policy")) != NULL
)
3373 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
3374 "printer-op-policy", NULL
, var
);
3377 * Do the request and get back a response...
3381 ippDelete(cupsDoFileRequest(http
, request
, "/admin/", tempfile
));
3383 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
3385 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
3387 puts("Status: 401\n");
3390 else if (cupsLastError() > IPP_OK_CONFLICT
)
3392 cgiStartHTML(title
);
3393 cgiShowIPPError(_("Unable to set options:"));
3398 * Redirect successful updates back to the printer page...
3401 char refresh
[1024]; /* Refresh URL */
3404 cgiFormEncode(uri
, printer
, sizeof(uri
));
3405 snprintf(refresh
, sizeof(refresh
), "5;URL=/admin/?OP=redirect&URL=/%s/%s",
3406 is_class
? "classes" : "printers", uri
);
3407 cgiSetVariable("refresh_page", refresh
);
3409 cgiStartHTML(title
);
3411 cgiCopyTemplateLang("printer-configured.tmpl");
3426 * 'do_set_sharing()' - Set printer-is-shared value.
3430 do_set_sharing(http_t
*http
) /* I - HTTP connection */
3432 ipp_t
*request
, /* IPP request */
3433 *response
; /* IPP response */
3434 char uri
[HTTP_MAX_URI
]; /* Printer URI */
3435 const char *printer
, /* Printer name */
3436 *is_class
, /* Is a class? */
3437 *shared
; /* Sharing value */
3440 is_class
= cgiGetVariable("IS_CLASS");
3441 printer
= cgiGetVariable("PRINTER_NAME");
3442 shared
= cgiGetVariable("SHARED");
3444 if (!printer
|| !shared
)
3446 cgiSetVariable("ERROR", cgiText(_("Missing form variable!")));
3447 cgiStartHTML(cgiText(_("Set Publishing")));
3448 cgiCopyTemplateLang("error.tmpl");
3454 * Build a CUPS-Add-Printer/CUPS-Add-Class request, which requires the
3455 * following attributes:
3457 * attributes-charset
3458 * attributes-natural-language
3463 request
= ippNewRequest(is_class
? CUPS_ADD_CLASS
: CUPS_ADD_PRINTER
);
3465 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3466 "localhost", 0, is_class
? "/classes/%s" : "/printers/%s",
3468 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
3471 ippAddBoolean(request
, IPP_TAG_OPERATION
, "printer-is-shared", atoi(shared
));
3474 * Do the request and get back a response...
3477 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
)
3479 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
3481 ippDelete(response
);
3484 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
3486 puts("Status: 401\n");
3489 else if (cupsLastError() > IPP_OK_CONFLICT
)
3491 cgiStartHTML(cgiText(_("Set Publishing")));
3492 cgiShowIPPError(_("Unable to change printer-is-shared attribute:"));
3497 * Redirect successful updates back to the printer page...
3500 char url
[1024], /* Printer/class URL */
3501 refresh
[1024]; /* Refresh URL */
3504 cgiRewriteURL(uri
, url
, sizeof(url
), NULL
);
3505 cgiFormEncode(uri
, url
, sizeof(uri
));
3506 snprintf(refresh
, sizeof(refresh
), "5;URL=/admin/?OP=redirect&URL=%s", uri
);
3507 cgiSetVariable("refresh_page", refresh
);
3509 cgiStartHTML(cgiText(_("Set Publishing")));
3510 cgiCopyTemplateLang(is_class
? "class-modified.tmpl" :
3511 "printer-modified.tmpl");
3519 * 'get_option_value()' - Return the value of an option.
3521 * This function also handles generation of custom option values.
3524 static char * /* O - Value string or NULL on error */
3526 ppd_file_t
*ppd
, /* I - PPD file */
3527 const char *name
, /* I - Option name */
3528 char *buffer
, /* I - String buffer */
3529 size_t bufsize
) /* I - Size of buffer */
3531 char *bufptr
, /* Pointer into buffer */
3532 *bufend
; /* End of buffer */
3533 ppd_coption_t
*coption
; /* Custom option */
3534 ppd_cparam_t
*cparam
; /* Current custom parameter */
3535 char keyword
[256]; /* Parameter name */
3536 const char *val
, /* Parameter value */
3537 *uval
; /* Units value */
3538 long integer
; /* Integer value */
3539 double number
, /* Number value */
3540 number_points
; /* Number in points */
3544 * See if we have a custom option choice...
3547 if ((val
= cgiGetVariable(name
)) == NULL
)
3555 else if (strcasecmp(val
, "Custom") ||
3556 (coption
= ppdFindCustomOption(ppd
, name
)) == NULL
)
3559 * Not a custom choice...
3562 strlcpy(buffer
, val
, bufsize
);
3567 * OK, we have a custom option choice, format it...
3572 if (!strcmp(coption
->keyword
, "PageSize"))
3574 const char *lval
; /* Length string value */
3575 double width
, /* Width value */
3576 width_points
, /* Width in points */
3577 length
, /* Length value */
3578 length_points
; /* Length in points */
3581 val
= cgiGetVariable("PageSize.Width");
3582 lval
= cgiGetVariable("PageSize.Height");
3583 uval
= cgiGetVariable("PageSize.Units");
3585 if (!val
|| !lval
|| !uval
||
3586 (width
= strtod(val
, NULL
)) == 0.0 ||
3587 (length
= strtod(lval
, NULL
)) == 0.0 ||
3588 (strcmp(uval
, "pt") && strcmp(uval
, "in") && strcmp(uval
, "ft") &&
3589 strcmp(uval
, "cm") && strcmp(uval
, "mm") && strcmp(uval
, "m")))
3592 width_points
= get_points(width
, uval
);
3593 length_points
= get_points(length
, uval
);
3595 if (width_points
< ppd
->custom_min
[0] ||
3596 width_points
> ppd
->custom_max
[0] ||
3597 length_points
< ppd
->custom_min
[1] ||
3598 length_points
> ppd
->custom_max
[1])
3601 snprintf(buffer
, bufsize
, "Custom.%gx%g%s", width
, length
, uval
);
3603 else if (cupsArrayCount(coption
->params
) == 1)
3605 cparam
= ppdFirstCustomParam(coption
);
3606 snprintf(keyword
, sizeof(keyword
), "%s.%s", coption
->keyword
, cparam
->name
);
3608 if ((val
= cgiGetVariable(keyword
)) == NULL
)
3611 switch (cparam
->type
)
3613 case PPD_CUSTOM_CURVE
:
3614 case PPD_CUSTOM_INVCURVE
:
3615 case PPD_CUSTOM_REAL
:
3616 if ((number
= strtod(val
, NULL
)) == 0.0 ||
3617 number
< cparam
->minimum
.custom_real
||
3618 number
> cparam
->maximum
.custom_real
)
3621 snprintf(buffer
, bufsize
, "Custom.%g", number
);
3624 case PPD_CUSTOM_INT
:
3625 if (!*val
|| (integer
= strtol(val
, NULL
, 10)) == LONG_MIN
||
3626 integer
== LONG_MAX
||
3627 integer
< cparam
->minimum
.custom_int
||
3628 integer
> cparam
->maximum
.custom_int
)
3631 snprintf(buffer
, bufsize
, "Custom.%ld", integer
);
3634 case PPD_CUSTOM_POINTS
:
3635 snprintf(keyword
, sizeof(keyword
), "%s.Units", coption
->keyword
);
3637 if ((number
= strtod(val
, NULL
)) == 0.0 ||
3638 (uval
= cgiGetVariable(keyword
)) == NULL
||
3639 (strcmp(uval
, "pt") && strcmp(uval
, "in") && strcmp(uval
, "ft") &&
3640 strcmp(uval
, "cm") && strcmp(uval
, "mm") && strcmp(uval
, "m")))
3643 number_points
= get_points(number
, uval
);
3644 if (number_points
< cparam
->minimum
.custom_points
||
3645 number_points
> cparam
->maximum
.custom_points
)
3648 snprintf(buffer
, bufsize
, "Custom.%g%s", number
, uval
);
3651 case PPD_CUSTOM_PASSCODE
:
3652 for (uval
= val
; *uval
; uval
++)
3653 if (!isdigit(*uval
& 255))
3656 case PPD_CUSTOM_PASSWORD
:
3657 case PPD_CUSTOM_STRING
:
3658 integer
= (long)strlen(val
);
3659 if (integer
< cparam
->minimum
.custom_string
||
3660 integer
> cparam
->maximum
.custom_string
)
3663 snprintf(buffer
, bufsize
, "Custom.%s", val
);
3669 const char *prefix
= "{"; /* Prefix string */
3673 bufend
= buffer
+ bufsize
;
3675 for (cparam
= ppdFirstCustomParam(coption
);
3677 cparam
= ppdNextCustomParam(coption
))
3679 snprintf(keyword
, sizeof(keyword
), "%s.%s", coption
->keyword
,
3682 if ((val
= cgiGetVariable(keyword
)) == NULL
)
3685 snprintf(bufptr
, bufend
- bufptr
, "%s%s=", prefix
, cparam
->name
);
3686 bufptr
+= strlen(bufptr
);
3689 switch (cparam
->type
)
3691 case PPD_CUSTOM_CURVE
:
3692 case PPD_CUSTOM_INVCURVE
:
3693 case PPD_CUSTOM_REAL
:
3694 if ((number
= strtod(val
, NULL
)) == 0.0 ||
3695 number
< cparam
->minimum
.custom_real
||
3696 number
> cparam
->maximum
.custom_real
)
3699 snprintf(bufptr
, bufend
- bufptr
, "%g", number
);
3702 case PPD_CUSTOM_INT
:
3703 if (!*val
|| (integer
= strtol(val
, NULL
, 10)) == LONG_MIN
||
3704 integer
== LONG_MAX
||
3705 integer
< cparam
->minimum
.custom_int
||
3706 integer
> cparam
->maximum
.custom_int
)
3709 snprintf(bufptr
, bufend
- bufptr
, "%ld", integer
);
3712 case PPD_CUSTOM_POINTS
:
3713 snprintf(keyword
, sizeof(keyword
), "%s.Units", coption
->keyword
);
3715 if ((number
= strtod(val
, NULL
)) == 0.0 ||
3716 (uval
= cgiGetVariable(keyword
)) == NULL
||
3717 (strcmp(uval
, "pt") && strcmp(uval
, "in") &&
3718 strcmp(uval
, "ft") && strcmp(uval
, "cm") &&
3719 strcmp(uval
, "mm") && strcmp(uval
, "m")))
3722 number_points
= get_points(number
, uval
);
3723 if (number_points
< cparam
->minimum
.custom_points
||
3724 number_points
> cparam
->maximum
.custom_points
)
3727 snprintf(bufptr
, bufend
- bufptr
, "%g%s", number
, uval
);
3730 case PPD_CUSTOM_PASSCODE
:
3731 for (uval
= val
; *uval
; uval
++)
3732 if (!isdigit(*uval
& 255))
3735 case PPD_CUSTOM_PASSWORD
:
3736 case PPD_CUSTOM_STRING
:
3737 integer
= (long)strlen(val
);
3738 if (integer
< cparam
->minimum
.custom_string
||
3739 integer
> cparam
->maximum
.custom_string
)
3742 if ((bufptr
+ 2) > bufend
)
3748 while (*val
&& bufptr
< bufend
)
3750 if (*val
== '\\' || *val
== '\"')
3752 if ((bufptr
+ 1) >= bufend
)
3761 if (bufptr
>= bufend
)
3770 bufptr
+= strlen(bufptr
);
3773 if (bufptr
== buffer
|| (bufend
- bufptr
) < 2)
3776 strcpy(bufptr
, "}");
3784 * 'get_points()' - Get a value in points.
3787 static double /* O - Number in points */
3788 get_points(double number
, /* I - Original number */
3789 const char *uval
) /* I - Units */
3791 if (!strcmp(uval
, "mm")) /* Millimeters */
3792 return (number
* 72.0 / 25.4);
3793 else if (!strcmp(uval
, "cm")) /* Centimeters */
3794 return (number
* 72.0 / 2.54);
3795 else if (!strcmp(uval
, "in")) /* Inches */
3796 return (number
* 72.0);
3797 else if (!strcmp(uval
, "ft")) /* Feet */
3798 return (number
* 72.0 * 12.0);
3799 else if (!strcmp(uval
, "m")) /* Meters */
3800 return (number
* 72.0 / 0.0254);
3807 * 'match_string()' - Return the number of matching characters.
3810 static int /* O - Number of matching characters */
3811 match_string(const char *a
, /* I - First string */
3812 const char *b
) /* I - Second string */
3814 int count
; /* Number of matching characters */
3818 * Loop through both strings until we hit the end of either or we find
3819 * a non-matching character. For the purposes of comparison, we ignore
3820 * whitespace and do a case-insensitive comparison so that we have a
3821 * better chance of finding a match...
3824 for (count
= 0; *a
&& *b
; a
++, b
++, count
++)
3827 * Skip leading whitespace characters...
3830 while (isspace(*a
& 255))
3833 while (isspace(*b
& 255))
3837 * Break out if we run out of characters...
3844 * Do a case-insensitive comparison of the next two chars...
3847 if (tolower(*a
& 255) != tolower(*b
& 255))