2 * "$Id: admin.c 7438 2008-04-09 03:27:37Z mike $"
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...
968 cgiCopyTemplateLang("add-printer.tmpl");
978 else if (!file
&& !cgiGetVariable("PPD_NAME"))
983 * Get the PPD file...
986 int fd
; /* PPD file */
987 char filename
[1024]; /* PPD filename */
988 ppd_file_t
*ppd
; /* PPD information */
989 char buffer
[1024]; /* Buffer */
990 int bytes
; /* Number of bytes */
991 http_status_t get_status
; /* Status of GET */
994 /* TODO: Use cupsGetFile() API... */
995 snprintf(uri
, sizeof(uri
), "/printers/%s.ppd", name
);
997 if (httpGet(http
, uri
))
1000 while ((get_status
= httpUpdate(http
)) == HTTP_CONTINUE
);
1002 if (get_status
!= HTTP_OK
)
1004 fprintf(stderr
, "ERROR: Unable to get PPD file %s: %d - %s\n",
1005 uri
, get_status
, httpStatus(get_status
));
1007 else if ((fd
= cupsTempFd(filename
, sizeof(filename
))) >= 0)
1009 while ((bytes
= httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
1010 write(fd
, buffer
, bytes
);
1014 if ((ppd
= ppdOpenFile(filename
)) != NULL
)
1016 if (ppd
->manufacturer
)
1017 cgiSetVariable("CURRENT_MAKE", ppd
->manufacturer
);
1020 cgiSetVariable("CURRENT_MAKE_AND_MODEL", ppd
->nickname
);
1027 fprintf(stderr
, "ERROR: Unable to open PPD file %s: %s\n",
1028 filename
, ppdErrorString(ppdLastError(&bytes
)));
1036 "ERROR: Unable to create temporary file for PPD file: %s\n",
1042 * Build a CUPS_GET_PPDS request, which requires the following
1045 * attributes-charset
1046 * attributes-natural-language
1050 request
= ippNewRequest(CUPS_GET_PPDS
);
1052 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1053 NULL
, "ipp://localhost/printers/");
1055 if ((var
= cgiGetVariable("CURRENT_MAKE")) == NULL
)
1056 var
= cgiGetVariable("PPD_MAKE");
1058 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
1059 "ppd-make", NULL
, var
);
1061 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1062 "requested-attributes", NULL
, "ppd-make");
1065 * Do the request and get back a response...
1068 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1071 * Got the list of PPDs, see if the user has selected a make...
1074 if (cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0) == 0)
1077 * No PPD files with this make, try again with all makes...
1080 ippDelete(response
);
1082 request
= ippNewRequest(CUPS_GET_PPDS
);
1084 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1085 NULL
, "ipp://localhost/printers/");
1087 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1088 "requested-attributes", NULL
, "ppd-make");
1090 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1091 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
1093 cgiStartHTML(title
);
1094 cgiCopyTemplateLang("choose-make.tmpl");
1097 else if (!var
|| cgiGetVariable("SELECT_MAKE"))
1099 cgiStartHTML(title
);
1100 cgiCopyTemplateLang("choose-make.tmpl");
1106 * Let the user choose a model...
1109 const char *make_model
; /* Current make/model string */
1112 if ((make_model
= cgiGetVariable("CURRENT_MAKE_AND_MODEL")) != NULL
)
1115 * Scan for "close" matches...
1118 int match
, /* Current match */
1119 best_match
, /* Best match so far */
1120 count
; /* Number of drivers */
1121 const char *best
, /* Best matching string */
1122 *current
; /* Current string */
1125 count
= cgiGetSize("PPD_MAKE_AND_MODEL");
1127 for (i
= 0, best_match
= 0, best
= NULL
; i
< count
; i
++)
1129 current
= cgiGetArray("PPD_MAKE_AND_MODEL", i
);
1130 match
= match_string(make_model
, current
);
1132 if (match
> best_match
)
1139 if (best_match
> strlen(var
))
1142 * Found a match longer than the make...
1145 cgiSetVariable("CURRENT_MAKE_AND_MODEL", best
);
1149 cgiStartHTML(title
);
1150 cgiCopyTemplateLang("choose-model.tmpl");
1154 ippDelete(response
);
1158 cgiStartHTML(title
);
1159 cgiShowIPPError(_("Unable to get list of printer drivers:"));
1160 cgiCopyTemplateLang("error.tmpl");
1167 * Build a CUPS_ADD_PRINTER request, which requires the following
1170 * attributes-charset
1171 * attributes-natural-language
1177 * printer-is-accepting-jobs
1181 request
= ippNewRequest(CUPS_ADD_PRINTER
);
1183 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1184 "localhost", 0, "/printers/%s",
1185 cgiGetVariable("PRINTER_NAME"));
1186 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1189 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-location",
1190 NULL
, cgiGetVariable("PRINTER_LOCATION"));
1192 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info",
1193 NULL
, cgiGetVariable("PRINTER_INFO"));
1196 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "ppd-name",
1197 NULL
, cgiGetVariable("PPD_NAME"));
1199 strlcpy(uri
, cgiGetVariable("DEVICE_URI"), sizeof(uri
));
1202 * Strip make and model from URI...
1205 if ((uriptr
= strrchr(uri
, '|')) != NULL
)
1208 if (!strncmp(uri
, "serial:", 7))
1211 * Update serial port URI to include baud rate, etc.
1214 if ((uriptr
= strchr(uri
, '?')) == NULL
)
1215 uriptr
= uri
+ strlen(uri
);
1217 snprintf(uriptr
, sizeof(uri
) - (uriptr
- uri
),
1218 "?baud=%s+bits=%s+parity=%s+flow=%s",
1219 cgiGetVariable("BAUDRATE"), cgiGetVariable("BITS"),
1220 cgiGetVariable("PARITY"), cgiGetVariable("FLOW"));
1223 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri",
1226 ippAddBoolean(request
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", 1);
1228 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
1232 * Do the request and get back a response...
1236 ippDelete(cupsDoFileRequest(http
, request
, "/admin/", file
->tempfile
));
1238 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1240 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
1242 puts("Status: 401\n");
1245 else if (cupsLastError() > IPP_OK_CONFLICT
)
1247 cgiStartHTML(title
);
1248 cgiShowIPPError(modify
? _("Unable to modify printer:") :
1249 _("Unable to add printer:"));
1254 * Redirect successful updates back to the printer page...
1257 char refresh
[1024]; /* Refresh URL */
1260 cgiFormEncode(uri
, name
, sizeof(uri
));
1262 snprintf(refresh
, sizeof(refresh
),
1263 "5;/admin/?OP=redirect&URL=/printers/%s", uri
);
1265 cgiSetVariable("refresh_page", refresh
);
1267 cgiStartHTML(title
);
1269 cgiCopyTemplateLang("printer-modified.tmpl");
1274 * Set the printer options...
1277 cgiSetVariable("OP", "set-printer-options");
1278 do_set_options(http
, 0);
1291 * 'do_cancel_subscription()' - Cancel a subscription.
1295 do_cancel_subscription(http_t
*http
)/* I - HTTP connection */
1297 ipp_t
*request
; /* IPP request data */
1298 const char *var
, /* Form variable */
1299 *user
; /* Username */
1300 int id
; /* Subscription ID */
1304 * See if we have all of the required information...
1307 if ((var
= cgiGetVariable("NOTIFY_SUBSCRIPTION_ID")) != NULL
)
1314 cgiSetVariable("ERROR", cgiText(_("Bad subscription ID!")));
1315 cgiStartHTML(_("Cancel RSS Subscription"));
1316 cgiCopyTemplateLang("error.tmpl");
1322 * Require a username...
1325 if ((user
= getenv("REMOTE_USER")) == NULL
)
1327 puts("Status: 401\n");
1332 * Cancel the subscription...
1335 request
= ippNewRequest(IPP_CANCEL_SUBSCRIPTION
);
1337 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1338 NULL
, "ipp://localhost/");
1339 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
1340 "notify-subscription-id", id
);
1342 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1345 ippDelete(cupsDoRequest(http
, request
, "/"));
1347 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
1349 puts("Status: 401\n");
1352 else if (cupsLastError() > IPP_OK_CONFLICT
)
1354 cgiStartHTML(_("Cancel RSS Subscription"));
1355 cgiShowIPPError(_("Unable to cancel RSS subscription:"));
1360 * Redirect successful updates back to the admin page...
1363 cgiSetVariable("refresh_page", "5;URL=/admin");
1364 cgiStartHTML(_("Cancel RSS Subscription"));
1365 cgiCopyTemplateLang("subscription-canceled.tmpl");
1373 * 'do_config_server()' - Configure server settings.
1377 do_config_server(http_t
*http
) /* I - HTTP connection */
1379 if (cgiGetVariable("CHANGESETTINGS"))
1382 * Save basic setting changes...
1385 int num_settings
; /* Number of server settings */
1386 cups_option_t
*settings
; /* Server settings */
1387 const char *debug_logging
, /* DEBUG_LOGGING value */
1388 *remote_admin
, /* REMOTE_ADMIN value */
1389 *remote_any
, /* REMOTE_ANY value */
1391 /* REMOTE_PRINTERS value */
1392 *share_printers
,/* SHARE_PRINTERS value */
1394 /* USER_CANCEL_ANY value */
1396 char default_auth_type
[255];
1397 /* DefaultAuthType value */
1398 #endif /* HAVE_GSSAPI */
1402 * Get the checkbox values from the form...
1405 debug_logging
= cgiGetVariable("DEBUG_LOGGING") ? "1" : "0";
1406 remote_admin
= cgiGetVariable("REMOTE_ADMIN") ? "1" : "0";
1407 remote_any
= cgiGetVariable("REMOTE_ANY") ? "1" : "0";
1408 remote_printers
= cgiGetVariable("REMOTE_PRINTERS") ? "1" : "0";
1409 share_printers
= cgiGetVariable("SHARE_PRINTERS") ? "1" : "0";
1410 user_cancel_any
= cgiGetVariable("USER_CANCEL_ANY") ? "1" : "0";
1413 * Get the current server settings...
1416 if (!cupsAdminGetServerSettings(http
, &num_settings
, &settings
))
1418 cgiStartHTML(cgiText(_("Change Settings")));
1419 cgiSetVariable("MESSAGE",
1420 cgiText(_("Unable to change server settings:")));
1421 cgiSetVariable("ERROR", cupsLastErrorString());
1422 cgiCopyTemplateLang("error.tmpl");
1429 * Get authentication settings...
1432 if (cgiGetVariable("KERBEROS"))
1433 strlcpy(default_auth_type
, "Negotiate", sizeof(default_auth_type
));
1436 const char *val
= cupsGetOption("DefaultAuthType", num_settings
,
1439 if (val
&& !strcasecmp(val
, "Negotiate"))
1440 strlcpy(default_auth_type
, "Basic", sizeof(default_auth_type
));
1442 strlcpy(default_auth_type
, val
, sizeof(default_auth_type
));
1445 fprintf(stderr
, "DEBUG: DefaultAuthType %s\n", default_auth_type
);
1446 #endif /* HAVE_GSSAPI */
1449 * See if the settings have changed...
1452 if (strcmp(debug_logging
, cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
,
1453 num_settings
, settings
)) ||
1454 strcmp(remote_admin
, cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
,
1455 num_settings
, settings
)) ||
1456 strcmp(remote_any
, cupsGetOption(CUPS_SERVER_REMOTE_ANY
,
1457 num_settings
, settings
)) ||
1458 strcmp(remote_printers
, cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
,
1459 num_settings
, settings
)) ||
1460 strcmp(share_printers
, cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
,
1461 num_settings
, settings
)) ||
1463 !cupsGetOption("DefaultAuthType", num_settings
, settings
) ||
1464 strcmp(default_auth_type
, cupsGetOption("DefaultAuthType",
1465 num_settings
, settings
)) ||
1466 #endif /* HAVE_GSSAPI */
1467 strcmp(user_cancel_any
, cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
,
1468 num_settings
, settings
)))
1471 * Settings *have* changed, so save the changes...
1474 cupsFreeOptions(num_settings
, settings
);
1477 num_settings
= cupsAddOption(CUPS_SERVER_DEBUG_LOGGING
,
1478 debug_logging
, num_settings
, &settings
);
1479 num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ADMIN
,
1480 remote_admin
, num_settings
, &settings
);
1481 num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_ANY
,
1482 remote_any
, num_settings
, &settings
);
1483 num_settings
= cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS
,
1484 remote_printers
, num_settings
, &settings
);
1485 num_settings
= cupsAddOption(CUPS_SERVER_SHARE_PRINTERS
,
1486 share_printers
, num_settings
, &settings
);
1487 num_settings
= cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY
,
1488 user_cancel_any
, num_settings
, &settings
);
1490 num_settings
= cupsAddOption("DefaultAuthType", default_auth_type
,
1491 num_settings
, &settings
);
1492 #endif /* HAVE_GSSAPI */
1494 if (!cupsAdminSetServerSettings(http
, num_settings
, settings
))
1496 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
1498 puts("Status: 401\n");
1502 cgiStartHTML(cgiText(_("Change Settings")));
1503 cgiSetVariable("MESSAGE",
1504 cgiText(_("Unable to change server settings:")));
1505 cgiSetVariable("ERROR", cupsLastErrorString());
1506 cgiCopyTemplateLang("error.tmpl");
1510 cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect");
1511 cgiStartHTML(cgiText(_("Change Settings")));
1512 cgiCopyTemplateLang("restart.tmpl");
1521 cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect");
1522 cgiStartHTML(cgiText(_("Change Settings")));
1523 cgiCopyTemplateLang("norestart.tmpl");
1526 cupsFreeOptions(num_settings
, settings
);
1530 else if (cgiGetVariable("SAVECHANGES") && cgiGetVariable("CUPSDCONF"))
1533 * Save hand-edited config file...
1536 http_status_t status
; /* PUT status */
1537 char tempfile
[1024]; /* Temporary new cupsd.conf */
1538 int tempfd
; /* Temporary file descriptor */
1539 cups_file_t
*temp
; /* Temporary file */
1540 const char *start
, /* Start of line */
1541 *end
; /* End of line */
1545 * Create a temporary file for the new cupsd.conf file...
1548 if ((tempfd
= cupsTempFd(tempfile
, sizeof(tempfile
))) < 0)
1550 cgiStartHTML(cgiText(_("Edit Configuration File")));
1551 cgiSetVariable("MESSAGE", cgiText(_("Unable to create temporary file:")));
1552 cgiSetVariable("ERROR", strerror(errno
));
1553 cgiCopyTemplateLang("error.tmpl");
1560 if ((temp
= cupsFileOpenFd(tempfd
, "w")) == NULL
)
1562 cgiStartHTML(cgiText(_("Edit Configuration File")));
1563 cgiSetVariable("MESSAGE", cgiText(_("Unable to create temporary file:")));
1564 cgiSetVariable("ERROR", strerror(errno
));
1565 cgiCopyTemplateLang("error.tmpl");
1575 * Copy the cupsd.conf text from the form variable...
1578 start
= cgiGetVariable("CUPSDCONF");
1581 if ((end
= strstr(start
, "\r\n")) == NULL
)
1582 if ((end
= strstr(start
, "\n")) == NULL
)
1583 end
= start
+ strlen(start
);
1585 cupsFileWrite(temp
, start
, end
- start
);
1586 cupsFilePutChar(temp
, '\n');
1590 else if (*end
== '\n')
1596 cupsFileClose(temp
);
1599 * Upload the configuration file to the server...
1602 status
= cupsPutFile(http
, "/admin/conf/cupsd.conf", tempfile
);
1604 if (status
== HTTP_UNAUTHORIZED
)
1606 puts("Status: 401\n");
1610 else if (status
!= HTTP_CREATED
)
1612 cgiSetVariable("MESSAGE",
1613 cgiText(_("Unable to upload cupsd.conf file:")));
1614 cgiSetVariable("ERROR", httpStatus(status
));
1616 cgiStartHTML(cgiText(_("Edit Configuration File")));
1617 cgiCopyTemplateLang("error.tmpl");
1621 cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect");
1623 cgiStartHTML(cgiText(_("Edit Configuration File")));
1624 cgiCopyTemplateLang("restart.tmpl");
1633 struct stat info
; /* cupsd.conf information */
1634 cups_file_t
*cupsd
; /* cupsd.conf file */
1635 char *buffer
, /* Buffer for entire file */
1636 *bufptr
, /* Pointer into buffer */
1637 *bufend
; /* End of buffer */
1638 int ch
; /* Character from file */
1639 char filename
[1024]; /* Filename */
1640 const char *server_root
; /* Location of config files */
1644 * Locate the cupsd.conf file...
1647 if ((server_root
= getenv("CUPS_SERVERROOT")) == NULL
)
1648 server_root
= CUPS_SERVERROOT
;
1650 snprintf(filename
, sizeof(filename
), "%s/cupsd.conf", server_root
);
1653 * Figure out the size...
1656 if (stat(filename
, &info
))
1658 cgiStartHTML(cgiText(_("Edit Configuration File")));
1659 cgiSetVariable("MESSAGE",
1660 cgiText(_("Unable to access cupsd.conf file:")));
1661 cgiSetVariable("ERROR", strerror(errno
));
1662 cgiCopyTemplateLang("error.tmpl");
1669 if (info
.st_size
> (1024 * 1024))
1671 cgiStartHTML(cgiText(_("Edit Configuration File")));
1672 cgiSetVariable("MESSAGE",
1673 cgiText(_("Unable to access cupsd.conf file:")));
1674 cgiSetVariable("ERROR",
1675 cgiText(_("Unable to edit cupsd.conf files larger than "
1677 cgiCopyTemplateLang("error.tmpl");
1680 fprintf(stderr
, "ERROR: \"%s\" too large (%ld) to edit!\n", filename
,
1681 (long)info
.st_size
);
1686 * Open the cupsd.conf file...
1689 if ((cupsd
= cupsFileOpen(filename
, "r")) == NULL
)
1692 * Unable to open - log an error...
1695 cgiStartHTML(cgiText(_("Edit Configuration File")));
1696 cgiSetVariable("MESSAGE",
1697 cgiText(_("Unable to access cupsd.conf file:")));
1698 cgiSetVariable("ERROR", strerror(errno
));
1699 cgiCopyTemplateLang("error.tmpl");
1707 * Allocate memory and load the file into a string buffer...
1710 if ((buffer
= calloc(1, info
.st_size
+ 1)) != NULL
)
1712 cupsFileRead(cupsd
, buffer
, info
.st_size
);
1713 cgiSetVariable("CUPSDCONF", buffer
);
1717 cupsFileClose(cupsd
);
1720 * Then get the default cupsd.conf file and put that into a string as
1724 strlcat(filename
, ".default", sizeof(filename
));
1726 if (!stat(filename
, &info
) && info
.st_size
< (1024 * 1024) &&
1727 (cupsd
= cupsFileOpen(filename
, "r")) != NULL
)
1729 if ((buffer
= calloc(1, 2 * info
.st_size
+ 1)) != NULL
)
1731 bufend
= buffer
+ 2 * info
.st_size
- 1;
1733 for (bufptr
= buffer
;
1734 bufptr
< bufend
&& (ch
= cupsFileGetChar(cupsd
)) != EOF
;)
1736 if (ch
== '\\' || ch
== '\"')
1741 else if (ch
== '\n')
1746 else if (ch
== '\t')
1757 cgiSetVariable("CUPSDCONF_DEFAULT", buffer
);
1761 cupsFileClose(cupsd
);
1765 * Show the current config file...
1768 cgiStartHTML(cgiText(_("Edit Configuration File")));
1770 cgiCopyTemplateLang("edit-config.tmpl");
1778 * 'do_delete_class()' - Delete a class.
1782 do_delete_class(http_t
*http
) /* I - HTTP connection */
1784 ipp_t
*request
; /* IPP request */
1785 char uri
[HTTP_MAX_URI
]; /* Job URI */
1786 const char *pclass
; /* Printer class name */
1790 * Get form variables...
1793 if (cgiGetVariable("CONFIRM") == NULL
)
1795 cgiStartHTML(cgiText(_("Delete Class")));
1796 cgiCopyTemplateLang("class-confirm.tmpl");
1801 if ((pclass
= cgiGetVariable("PRINTER_NAME")) != NULL
)
1802 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1803 "localhost", 0, "/classes/%s", pclass
);
1806 cgiStartHTML(cgiText(_("Delete Class")));
1807 cgiSetVariable("ERROR", cgiText(_("Missing form variable!")));
1808 cgiCopyTemplateLang("error.tmpl");
1814 * Build a CUPS_DELETE_CLASS request, which requires the following
1817 * attributes-charset
1818 * attributes-natural-language
1822 request
= ippNewRequest(CUPS_DELETE_CLASS
);
1824 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1828 * Do the request and get back a response...
1831 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1834 * Show the results...
1837 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
1839 puts("Status: 401\n");
1842 else if (cupsLastError() <= IPP_OK_CONFLICT
)
1845 * Redirect successful updates back to the classes page...
1848 cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect&URL=/classes");
1851 cgiStartHTML(cgiText(_("Delete Class")));
1853 if (cupsLastError() > IPP_OK_CONFLICT
)
1854 cgiShowIPPError(_("Unable to delete class:"));
1856 cgiCopyTemplateLang("class-deleted.tmpl");
1863 * 'do_delete_printer()' - Delete a printer.
1867 do_delete_printer(http_t
*http
) /* I - HTTP connection */
1869 ipp_t
*request
; /* IPP request */
1870 char uri
[HTTP_MAX_URI
]; /* Job URI */
1871 const char *printer
; /* Printer printer name */
1875 * Get form variables...
1878 if (cgiGetVariable("CONFIRM") == NULL
)
1880 cgiStartHTML(cgiText(_("Delete Printer")));
1881 cgiCopyTemplateLang("printer-confirm.tmpl");
1886 if ((printer
= cgiGetVariable("PRINTER_NAME")) != NULL
)
1887 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1888 "localhost", 0, "/printers/%s", printer
);
1891 cgiStartHTML(cgiText(_("Delete Printer")));
1892 cgiSetVariable("ERROR", cgiText(_("Missing form variable!")));
1893 cgiCopyTemplateLang("error.tmpl");
1899 * Build a CUPS_DELETE_PRINTER request, which requires the following
1902 * attributes-charset
1903 * attributes-natural-language
1907 request
= ippNewRequest(CUPS_DELETE_PRINTER
);
1909 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1913 * Do the request and get back a response...
1916 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1919 * Show the results...
1922 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
1924 puts("Status: 401\n");
1927 else if (cupsLastError() <= IPP_OK_CONFLICT
)
1930 * Redirect successful updates back to the printers page...
1933 cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect&URL=/printers");
1936 cgiStartHTML(cgiText(_("Delete Printer")));
1938 if (cupsLastError() > IPP_OK_CONFLICT
)
1939 cgiShowIPPError(_("Unable to delete printer:"));
1941 cgiCopyTemplateLang("printer-deleted.tmpl");
1948 * 'do_export()' - Export printers to Samba.
1952 do_export(http_t
*http
) /* I - HTTP connection */
1954 int i
, j
; /* Looping vars */
1955 ipp_t
*request
, /* IPP request */
1956 *response
; /* IPP response */
1957 const char *username
, /* Samba username */
1958 *password
, /* Samba password */
1959 *export_all
; /* Export all printers? */
1960 int export_count
, /* Number of printers to export */
1961 printer_count
; /* Number of available printers */
1962 const char *name
, /* What name to pull */
1963 *dest
; /* Current destination */
1964 char ppd
[1024]; /* PPD file */
1971 username
= cgiGetVariable("USERNAME");
1972 password
= cgiGetVariable("PASSWORD");
1973 export_all
= cgiGetVariable("EXPORT_ALL");
1974 export_count
= cgiGetSize("EXPORT_NAME");
1977 * Get list of available printers...
1980 cgiSetSize("PRINTER_NAME", 0);
1981 cgiSetSize("PRINTER_EXPORT", 0);
1983 request
= ippNewRequest(CUPS_GET_PRINTERS
);
1985 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
1988 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
1989 "printer-type-mask", CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
|
1990 CUPS_PRINTER_IMPLICIT
);
1992 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1993 "requested-attributes", NULL
, "printer-name");
1995 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1997 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
1998 ippDelete(response
);
2002 printer_count
= cgiGetSize("PRINTER_NAME");
2004 for (i
= 0; i
< printer_count
; i
++)
2006 dest
= cgiGetArray("PRINTER_NAME", i
);
2008 for (j
= 0; j
< export_count
; j
++)
2009 if (!strcasecmp(dest
, cgiGetArray("EXPORT_NAME", j
)))
2012 cgiSetArray("PRINTER_EXPORT", i
, j
< export_count
? "Y" : "");
2018 * Export or get the printers to export...
2021 if (username
&& *username
&& password
&& *password
&&
2022 (export_all
|| export_count
> 0))
2028 fputs("DEBUG: Export printers...\n", stderr
);
2032 name
= "PRINTER_NAME";
2033 export_count
= cgiGetSize("PRINTER_NAME");
2036 name
= "EXPORT_NAME";
2038 for (i
= 0; i
< export_count
; i
++)
2040 dest
= cgiGetArray(name
, i
);
2042 if (!cupsAdminCreateWindowsPPD(http
, dest
, ppd
, sizeof(ppd
)))
2045 j
= cupsAdminExportSamba(dest
, ppd
, "localhost", username
, password
,
2054 if (i
< export_count
)
2055 cgiSetVariable("ERROR", cupsLastErrorString());
2058 cgiStartHTML(cgiText(_("Export Printers to Samba")));
2059 cgiCopyTemplateLang("samba-exported.tmpl");
2064 else if (username
&& !*username
)
2065 cgiSetVariable("ERROR",
2066 cgiText(_("A Samba username is required to export "
2067 "printer drivers!")));
2068 else if (username
&& (!password
|| !*password
))
2069 cgiSetVariable("ERROR",
2070 cgiText(_("A Samba password is required to export "
2071 "printer drivers!")));
2077 cgiStartHTML(cgiText(_("Export Printers to Samba")));
2078 cgiCopyTemplateLang("samba-export.tmpl");
2084 * 'do_list_printers()' - List available printers.
2088 do_list_printers(http_t
*http
) /* I - HTTP connection */
2090 ipp_t
*request
, /* IPP request */
2091 *response
; /* IPP response */
2092 ipp_attribute_t
*attr
; /* IPP attribute */
2095 cgiStartHTML(cgiText(_("List Available Printers")));
2099 * Get the list of printers and their devices...
2102 request
= ippNewRequest(CUPS_GET_PRINTERS
);
2104 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2105 "requested-attributes", NULL
, "device-uri");
2107 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type",
2108 CUPS_PRINTER_LOCAL
);
2109 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type-mask",
2110 CUPS_PRINTER_LOCAL
);
2112 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
2115 * Got the printer list, now load the devices...
2118 int i
; /* Looping var */
2119 cups_array_t
*printer_devices
; /* Printer devices for local printers */
2120 char *printer_device
; /* Current printer device */
2124 * Allocate an array and copy the device strings...
2127 printer_devices
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
2129 for (attr
= ippFindAttribute(response
, "device-uri", IPP_TAG_URI
);
2131 attr
= ippFindNextAttribute(response
, "device-uri", IPP_TAG_URI
))
2133 cupsArrayAdd(printer_devices
, strdup(attr
->values
[0].string
.text
));
2137 * Free the printer list and get the device list...
2140 ippDelete(response
);
2142 request
= ippNewRequest(CUPS_GET_DEVICES
);
2144 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
2147 * Got the device list, let's parse it...
2150 const char *device_uri
, /* device-uri attribute value */
2151 *device_make_and_model
, /* device-make-and-model value */
2152 *device_info
; /* device-info value */
2155 for (i
= 0, attr
= response
->attrs
; attr
; attr
= attr
->next
)
2158 * Skip leading attributes until we hit a device...
2161 while (attr
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
2168 * Pull the needed attributes from this device...
2172 device_make_and_model
= NULL
;
2175 while (attr
&& attr
->group_tag
== IPP_TAG_PRINTER
)
2177 if (!strcmp(attr
->name
, "device-info") &&
2178 attr
->value_tag
== IPP_TAG_TEXT
)
2179 device_info
= attr
->values
[0].string
.text
;
2181 if (!strcmp(attr
->name
, "device-make-and-model") &&
2182 attr
->value_tag
== IPP_TAG_TEXT
)
2183 device_make_and_model
= attr
->values
[0].string
.text
;
2185 if (!strcmp(attr
->name
, "device-uri") &&
2186 attr
->value_tag
== IPP_TAG_URI
)
2187 device_uri
= attr
->values
[0].string
.text
;
2193 * See if we have everything needed...
2196 if (device_info
&& device_make_and_model
&& device_uri
&&
2197 strcasecmp(device_make_and_model
, "unknown") &&
2198 strchr(device_uri
, ':'))
2201 * Yes, now see if there is already a printer for this
2205 if (!cupsArrayFind(printer_devices
, (void *)device_uri
))
2208 * Not found, so it must be a new printer...
2211 char option
[1024], /* Form variables for this device */
2212 *option_ptr
; /* Pointer into string */
2213 const char *ptr
; /* Pointer into device string */
2217 * Format the printer name variable for this device...
2219 * We use the device-info string first, then device-uri,
2220 * and finally device-make-and-model to come up with a
2224 if (strncasecmp(device_info
, "unknown", 7))
2226 else if ((ptr
= strstr(device_uri
, "://")) != NULL
)
2229 ptr
= device_make_and_model
;
2231 for (option_ptr
= option
;
2232 option_ptr
< (option
+ sizeof(option
) - 1) && *ptr
;
2234 if (isalnum(*ptr
& 255) || *ptr
== '_' || *ptr
== '-' ||
2236 *option_ptr
++ = *ptr
;
2237 else if ((*ptr
== ' ' || *ptr
== '/') && option_ptr
[-1] != '_')
2238 *option_ptr
++ = '_';
2239 else if (*ptr
== '?' || *ptr
== '(')
2244 cgiSetArray("TEMPLATE_NAME", i
, option
);
2247 * Finally, set the form variables for this printer...
2250 cgiSetArray("device_info", i
, device_info
);
2251 cgiSetArray("device_make_and_model", i
, device_make_and_model
);
2252 cgiSetArray("device_uri", i
, device_uri
);
2261 ippDelete(response
);
2264 * Free the device list...
2267 for (printer_device
= (char *)cupsArrayFirst(printer_devices
);
2269 printer_device
= (char *)cupsArrayNext(printer_devices
))
2270 free(printer_device
);
2272 cupsArrayDelete(printer_devices
);
2277 * Finally, show the printer list...
2280 cgiCopyTemplateLang("list-available-printers.tmpl");
2287 * 'do_menu()' - Show the main menu.
2291 do_menu(http_t
*http
) /* I - HTTP connection */
2293 int num_settings
; /* Number of server settings */
2294 cups_option_t
*settings
; /* Server settings */
2295 const char *val
; /* Setting value */
2296 char filename
[1024]; /* Temporary filename */
2297 const char *datadir
; /* Location of data files */
2298 ipp_t
*request
, /* IPP request */
2299 *response
; /* IPP response */
2303 * Get the current server settings...
2306 if (!cupsAdminGetServerSettings(http
, &num_settings
, &settings
))
2308 cgiSetVariable("SETTINGS_MESSAGE",
2309 cgiText(_("Unable to open cupsd.conf file:")));
2310 cgiSetVariable("SETTINGS_ERROR", cupsLastErrorString());
2313 if ((val
= cupsGetOption(CUPS_SERVER_DEBUG_LOGGING
, num_settings
,
2314 settings
)) != NULL
&& atoi(val
))
2315 cgiSetVariable("DEBUG_LOGGING", "CHECKED");
2317 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ADMIN
, num_settings
,
2318 settings
)) != NULL
&& atoi(val
))
2319 cgiSetVariable("REMOTE_ADMIN", "CHECKED");
2321 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_ANY
, num_settings
,
2322 settings
)) != NULL
&& atoi(val
))
2323 cgiSetVariable("REMOTE_ANY", "CHECKED");
2325 if ((val
= cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS
, num_settings
,
2326 settings
)) != NULL
&& atoi(val
))
2327 cgiSetVariable("REMOTE_PRINTERS", "CHECKED");
2329 if ((val
= cupsGetOption(CUPS_SERVER_SHARE_PRINTERS
, num_settings
,
2330 settings
)) != NULL
&& atoi(val
))
2331 cgiSetVariable("SHARE_PRINTERS", "CHECKED");
2333 if ((val
= cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY
, num_settings
,
2334 settings
)) != NULL
&& atoi(val
))
2335 cgiSetVariable("USER_CANCEL_ANY", "CHECKED");
2338 cgiSetVariable("HAVE_GSSAPI", "1");
2340 if ((val
= cupsGetOption("DefaultAuthType", num_settings
,
2341 settings
)) != NULL
&& !strcasecmp(val
, "Negotiate"))
2342 cgiSetVariable("KERBEROS", "CHECKED");
2343 #endif /* HAVE_GSSAPI */
2345 cupsFreeOptions(num_settings
, settings
);
2348 * See if Samba and the Windows drivers are installed...
2351 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
2352 datadir
= CUPS_DATADIR
;
2354 snprintf(filename
, sizeof(filename
), "%s/drivers/pscript5.dll", datadir
);
2355 if (!access(filename
, R_OK
))
2358 * Found Windows 2000 driver file, see if we have smbclient and
2362 if (cupsFileFind("smbclient", getenv("PATH"), 1, filename
,
2363 sizeof(filename
)) &&
2364 cupsFileFind("rpcclient", getenv("PATH"), 1, filename
,
2366 cgiSetVariable("HAVE_SAMBA", "Y");
2369 if (!cupsFileFind("smbclient", getenv("PATH"), 1, filename
,
2371 fputs("ERROR: smbclient not found!\n", stderr
);
2373 if (!cupsFileFind("rpcclient", getenv("PATH"), 1, filename
,
2375 fputs("ERROR: rpcclient not found!\n", stderr
);
2385 request
= ippNewRequest(IPP_GET_SUBSCRIPTIONS
);
2387 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2388 NULL
, "ipp://localhost/");
2390 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
2392 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
2393 ippDelete(response
);
2397 * Finally, show the main menu template...
2400 cgiStartHTML(cgiText(_("Administration")));
2402 cgiCopyTemplateLang("admin.tmpl");
2409 * 'do_printer_op()' - Do a printer operation.
2413 do_printer_op(http_t
*http
, /* I - HTTP connection */
2414 ipp_op_t op
, /* I - Operation to perform */
2415 const char *title
) /* I - Title of page */
2417 ipp_t
*request
; /* IPP request */
2418 char uri
[HTTP_MAX_URI
]; /* Printer URI */
2419 const char *printer
, /* Printer name (purge-jobs) */
2420 *is_class
; /* Is a class? */
2423 is_class
= cgiGetVariable("IS_CLASS");
2424 printer
= cgiGetVariable("PRINTER_NAME");
2428 cgiSetVariable("ERROR", cgiText(_("Missing form variable!")));
2429 cgiStartHTML(title
);
2430 cgiCopyTemplateLang("error.tmpl");
2436 * Build a printer request, which requires the following
2439 * attributes-charset
2440 * attributes-natural-language
2444 request
= ippNewRequest(op
);
2446 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2447 "localhost", 0, is_class
? "/classes/%s" : "/printers/%s",
2449 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2453 * Do the request and get back a response...
2456 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
2458 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
2460 puts("Status: 401\n");
2463 else if (cupsLastError() > IPP_OK_CONFLICT
)
2465 cgiStartHTML(title
);
2466 cgiShowIPPError(_("Unable to change printer:"));
2471 * Redirect successful updates back to the printer page...
2474 char url
[1024], /* Printer/class URL */
2475 refresh
[1024]; /* Refresh URL */
2478 cgiRewriteURL(uri
, url
, sizeof(url
), NULL
);
2479 cgiFormEncode(uri
, url
, sizeof(uri
));
2480 snprintf(refresh
, sizeof(refresh
), "5;URL=/admin/?OP=redirect&URL=%s", uri
);
2481 cgiSetVariable("refresh_page", refresh
);
2483 cgiStartHTML(title
);
2485 if (op
== IPP_PAUSE_PRINTER
)
2486 cgiCopyTemplateLang("printer-stop.tmpl");
2487 else if (op
== IPP_RESUME_PRINTER
)
2488 cgiCopyTemplateLang("printer-start.tmpl");
2489 else if (op
== CUPS_ACCEPT_JOBS
)
2490 cgiCopyTemplateLang("printer-accept.tmpl");
2491 else if (op
== CUPS_REJECT_JOBS
)
2492 cgiCopyTemplateLang("printer-reject.tmpl");
2493 else if (op
== IPP_PURGE_JOBS
)
2494 cgiCopyTemplateLang("printer-purge.tmpl");
2495 else if (op
== CUPS_SET_DEFAULT
)
2496 cgiCopyTemplateLang("printer-default.tmpl");
2504 * 'do_set_allowed_users()' - Set the allowed/denied users for a queue.
2508 do_set_allowed_users(http_t
*http
) /* I - HTTP connection */
2510 int i
; /* Looping var */
2511 ipp_t
*request
, /* IPP request */
2512 *response
; /* IPP response */
2513 char uri
[HTTP_MAX_URI
]; /* Printer URI */
2514 const char *printer
, /* Printer name (purge-jobs) */
2515 *is_class
, /* Is a class? */
2516 *users
, /* List of users or groups */
2517 *type
; /* Allow/deny type */
2518 int num_users
; /* Number of users */
2519 char *ptr
, /* Pointer into users string */
2520 *end
, /* Pointer to end of users string */
2521 quote
; /* Quote character */
2522 ipp_attribute_t
*attr
; /* Attribute */
2523 static const char * const attrs
[] = /* Requested attributes */
2525 "requesting-user-name-allowed",
2526 "requesting-user-name-denied"
2530 is_class
= cgiGetVariable("IS_CLASS");
2531 printer
= cgiGetVariable("PRINTER_NAME");
2535 cgiSetVariable("ERROR", cgiText(_("Missing form variable!")));
2536 cgiStartHTML(cgiText(_("Set Allowed Users")));
2537 cgiCopyTemplateLang("error.tmpl");
2542 users
= cgiGetVariable("users");
2543 type
= cgiGetVariable("type");
2545 if (!users
|| !type
||
2546 (strcmp(type
, "requesting-user-name-allowed") &&
2547 strcmp(type
, "requesting-user-name-denied")))
2550 * Build a Get-Printer-Attributes request, which requires the following
2553 * attributes-charset
2554 * attributes-natural-language
2556 * requested-attributes
2559 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
2561 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2562 "localhost", 0, is_class
? "/classes/%s" : "/printers/%s",
2564 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2567 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2568 "requested-attributes",
2569 (int)(sizeof(attrs
) / sizeof(attrs
[0])), NULL
, attrs
);
2572 * Do the request and get back a response...
2575 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
2577 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
2579 ippDelete(response
);
2582 cgiStartHTML(cgiText(_("Set Allowed Users")));
2584 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
2586 puts("Status: 401\n");
2589 else if (cupsLastError() > IPP_OK_CONFLICT
)
2590 cgiShowIPPError(_("Unable to get printer attributes:"));
2592 cgiCopyTemplateLang("users.tmpl");
2599 * Save the changes...
2602 for (num_users
= 0, ptr
= (char *)users
; *ptr
; num_users
++)
2605 * Skip whitespace and commas...
2608 while (*ptr
== ',' || isspace(*ptr
& 255))
2611 if (*ptr
== '\'' || *ptr
== '\"')
2614 * Scan quoted name...
2619 for (end
= ptr
; *end
; end
++)
2626 * Scan space or comma-delimited name...
2629 for (end
= ptr
; *end
; end
++)
2630 if (isspace(*end
& 255) || *end
== ',')
2635 * Advance to the next name...
2642 * Build a CUPS-Add-Printer/Class request, which requires the following
2645 * attributes-charset
2646 * attributes-natural-language
2648 * requesting-user-name-{allowed,denied}
2651 request
= ippNewRequest(is_class
? CUPS_ADD_CLASS
: CUPS_ADD_PRINTER
);
2653 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2654 "localhost", 0, is_class
? "/classes/%s" : "/printers/%s",
2656 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2660 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2661 "requesting-user-name-allowed", NULL
, "all");
2664 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2665 type
, num_users
, NULL
, NULL
);
2667 for (i
= 0, ptr
= (char *)users
; *ptr
; i
++)
2670 * Skip whitespace and commas...
2673 while (*ptr
== ',' || isspace(*ptr
& 255))
2676 if (*ptr
== '\'' || *ptr
== '\"')
2679 * Scan quoted name...
2684 for (end
= ptr
; *end
; end
++)
2691 * Scan space or comma-delimited name...
2694 for (end
= ptr
; *end
; end
++)
2695 if (isspace(*end
& 255) || *end
== ',')
2700 * Terminate the name...
2710 attr
->values
[i
].string
.text
= strdup(ptr
);
2713 * Advance to the next name...
2721 * Do the request and get back a response...
2724 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
2726 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
2728 puts("Status: 401\n");
2731 else if (cupsLastError() > IPP_OK_CONFLICT
)
2733 cgiStartHTML(cgiText(_("Set Allowed Users")));
2734 cgiShowIPPError(_("Unable to change printer:"));
2739 * Redirect successful updates back to the printer page...
2742 char url
[1024], /* Printer/class URL */
2743 refresh
[1024]; /* Refresh URL */
2746 cgiRewriteURL(uri
, url
, sizeof(url
), NULL
);
2747 cgiFormEncode(uri
, url
, sizeof(uri
));
2748 snprintf(refresh
, sizeof(refresh
), "5;URL=/admin/?OP=redirect&URL=%s",
2750 cgiSetVariable("refresh_page", refresh
);
2752 cgiStartHTML(cgiText(_("Set Allowed Users")));
2754 cgiCopyTemplateLang(is_class
? "class-modified.tmpl" :
2755 "printer-modified.tmpl");
2764 * 'do_set_options()' - Configure the default options for a queue.
2768 do_set_options(http_t
*http
, /* I - HTTP connection */
2769 int is_class
) /* I - Set options for class? */
2771 int i
, j
, k
, m
; /* Looping vars */
2772 int have_options
; /* Have options? */
2773 ipp_t
*request
, /* IPP request */
2774 *response
; /* IPP response */
2775 ipp_attribute_t
*attr
; /* IPP attribute */
2776 char uri
[HTTP_MAX_URI
]; /* Job URI */
2777 const char *var
; /* Variable value */
2778 const char *printer
; /* Printer printer name */
2779 const char *filename
; /* PPD filename */
2780 char tempfile
[1024]; /* Temporary filename */
2781 cups_file_t
*in
, /* Input file */
2782 *out
; /* Output file */
2783 char line
[1024], /* Line from PPD file */
2784 value
[1024], /* Option value */
2785 keyword
[1024], /* Keyword from Default line */
2786 *keyptr
; /* Pointer into keyword... */
2787 ppd_file_t
*ppd
; /* PPD file */
2788 ppd_group_t
*group
; /* Option group */
2789 ppd_option_t
*option
; /* Option */
2790 ppd_coption_t
*coption
; /* Custom option */
2791 ppd_cparam_t
*cparam
; /* Custom parameter */
2792 ppd_attr_t
*protocol
; /* cupsProtocol attribute */
2793 const char *title
; /* Page title */
2796 title
= cgiText(is_class
? _("Set Class Options") : _("Set Printer Options"));
2798 fprintf(stderr
, "DEBUG: do_set_options(http=%p, is_class=%d)\n", http
,
2802 * Get the printer name...
2805 if ((printer
= cgiGetVariable("PRINTER_NAME")) != NULL
)
2806 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2807 "localhost", 0, is_class
? "/classes/%s" : "/printers/%s",
2811 cgiSetVariable("ERROR", cgiText(_("Missing form variable!")));
2812 cgiStartHTML(title
);
2813 cgiCopyTemplateLang("error.tmpl");
2818 fprintf(stderr
, "DEBUG: printer=\"%s\", uri=\"%s\"...\n", printer
, uri
);
2821 * Get the PPD file...
2827 filename
= cupsGetPPD2(http
, printer
);
2831 fprintf(stderr
, "DEBUG: Got PPD file: \"%s\"\n", filename
);
2833 if ((ppd
= ppdOpenFile(filename
)) == NULL
)
2835 cgiSetVariable("ERROR", ppdErrorString(ppdLastError(&i
)));
2836 cgiSetVariable("MESSAGE", cgiText(_("Unable to open PPD file:")));
2837 cgiStartHTML(title
);
2838 cgiCopyTemplateLang("error.tmpl");
2845 fputs("DEBUG: No PPD file\n", stderr
);
2849 if (cgiGetVariable("job_sheets_start") != NULL
||
2850 cgiGetVariable("job_sheets_end") != NULL
)
2857 ppdMarkDefaults(ppd
);
2859 for (option
= ppdFirstOption(ppd
);
2861 option
= ppdNextOption(ppd
))
2862 if ((var
= cgiGetVariable(option
->keyword
)) != NULL
)
2865 ppdMarkOption(ppd
, option
->keyword
, var
);
2869 if (!have_options
|| ppdConflicts(ppd
))
2872 * Show the options to the user...
2875 fputs("DEBUG: Showing options...\n", stderr
);
2877 cgiStartHTML(cgiText(_("Set Printer Options")));
2878 cgiCopyTemplateLang("set-printer-options-header.tmpl");
2884 if (ppdConflicts(ppd
))
2886 for (i
= ppd
->num_groups
, k
= 0, group
= ppd
->groups
;
2889 for (j
= group
->num_options
, option
= group
->options
;
2892 if (option
->conflicted
)
2894 cgiSetArray("ckeyword", k
, option
->keyword
);
2895 cgiSetArray("ckeytext", k
, option
->text
);
2899 cgiCopyTemplateLang("option-conflict.tmpl");
2902 for (i
= ppd
->num_groups
, group
= ppd
->groups
;
2906 if (!strcmp(group
->name
, "InstallableOptions"))
2907 cgiSetVariable("GROUP", cgiText(_("Options Installed")));
2909 cgiSetVariable("GROUP", group
->text
);
2911 cgiCopyTemplateLang("option-header.tmpl");
2913 for (j
= group
->num_options
, option
= group
->options
;
2917 if (!strcmp(option
->keyword
, "PageRegion"))
2920 cgiSetVariable("KEYWORD", option
->keyword
);
2921 cgiSetVariable("KEYTEXT", option
->text
);
2923 if (option
->conflicted
)
2924 cgiSetVariable("CONFLICTED", "1");
2926 cgiSetVariable("CONFLICTED", "0");
2928 cgiSetSize("CHOICES", 0);
2929 cgiSetSize("TEXT", 0);
2930 for (k
= 0, m
= 0; k
< option
->num_choices
; k
++)
2932 cgiSetArray("CHOICES", m
, option
->choices
[k
].choice
);
2933 cgiSetArray("TEXT", m
, option
->choices
[k
].text
);
2937 if (option
->choices
[k
].marked
)
2938 cgiSetVariable("DEFCHOICE", option
->choices
[k
].choice
);
2941 cgiSetSize("PARAMS", 0);
2942 cgiSetSize("PARAMTEXT", 0);
2943 cgiSetSize("PARAMVALUE", 0);
2944 cgiSetSize("INPUTTYPE", 0);
2946 if ((coption
= ppdFindCustomOption(ppd
, option
->keyword
)))
2948 const char *units
= NULL
; /* Units value, if any */
2951 cgiSetVariable("ISCUSTOM", "1");
2953 for (cparam
= ppdFirstCustomParam(coption
), m
= 0;
2955 cparam
= ppdNextCustomParam(coption
), m
++)
2957 if (!strcasecmp(option
->keyword
, "PageSize") &&
2958 strcasecmp(cparam
->name
, "Width") &&
2959 strcasecmp(cparam
->name
, "Height"))
2965 cgiSetArray("PARAMS", m
, cparam
->name
);
2966 cgiSetArray("PARAMTEXT", m
, cparam
->text
);
2967 cgiSetArray("INPUTTYPE", m
, "text");
2969 switch (cparam
->type
)
2971 case PPD_CUSTOM_POINTS
:
2972 if (!strncasecmp(option
->defchoice
, "Custom.", 7))
2974 units
= option
->defchoice
+ strlen(option
->defchoice
) - 2;
2976 if (strcmp(units
, "mm") && strcmp(units
, "cm") &&
2977 strcmp(units
, "in") && strcmp(units
, "ft"))
2979 if (units
[1] == 'm')
2988 if (!strcmp(units
, "mm"))
2989 snprintf(value
, sizeof(value
), "%g",
2990 cparam
->current
.custom_points
/ 72.0 * 25.4);
2991 else if (!strcmp(units
, "cm"))
2992 snprintf(value
, sizeof(value
), "%g",
2993 cparam
->current
.custom_points
/ 72.0 * 2.54);
2994 else if (!strcmp(units
, "in"))
2995 snprintf(value
, sizeof(value
), "%g",
2996 cparam
->current
.custom_points
/ 72.0);
2997 else if (!strcmp(units
, "ft"))
2998 snprintf(value
, sizeof(value
), "%g",
2999 cparam
->current
.custom_points
/ 72.0 / 12.0);
3000 else if (!strcmp(units
, "m"))
3001 snprintf(value
, sizeof(value
), "%g",
3002 cparam
->current
.custom_points
/ 72.0 * 0.0254);
3004 snprintf(value
, sizeof(value
), "%g",
3005 cparam
->current
.custom_points
);
3006 cgiSetArray("PARAMVALUE", m
, value
);
3009 case PPD_CUSTOM_CURVE
:
3010 case PPD_CUSTOM_INVCURVE
:
3011 case PPD_CUSTOM_REAL
:
3012 snprintf(value
, sizeof(value
), "%g",
3013 cparam
->current
.custom_real
);
3014 cgiSetArray("PARAMVALUE", m
, value
);
3017 case PPD_CUSTOM_INT
:
3018 snprintf(value
, sizeof(value
), "%d",
3019 cparam
->current
.custom_int
);
3020 cgiSetArray("PARAMVALUE", m
, value
);
3023 case PPD_CUSTOM_PASSCODE
:
3024 case PPD_CUSTOM_PASSWORD
:
3025 if (cparam
->current
.custom_password
)
3026 cgiSetArray("PARAMVALUE", m
,
3027 cparam
->current
.custom_password
);
3029 cgiSetArray("PARAMVALUE", m
, "");
3030 cgiSetArray("INPUTTYPE", m
, "password");
3033 case PPD_CUSTOM_STRING
:
3034 if (cparam
->current
.custom_string
)
3035 cgiSetArray("PARAMVALUE", m
,
3036 cparam
->current
.custom_string
);
3038 cgiSetArray("PARAMVALUE", m
, "");
3045 cgiSetArray("PARAMS", m
, "Units");
3046 cgiSetArray("PARAMTEXT", m
, cgiText(_("Units")));
3047 cgiSetArray("PARAMVALUE", m
, units
);
3051 cgiSetVariable("ISCUSTOM", "0");
3055 case PPD_UI_BOOLEAN
:
3056 cgiCopyTemplateLang("option-boolean.tmpl");
3058 case PPD_UI_PICKONE
:
3059 cgiCopyTemplateLang("option-pickone.tmpl");
3061 case PPD_UI_PICKMANY
:
3062 cgiCopyTemplateLang("option-pickmany.tmpl");
3067 cgiCopyTemplateLang("option-trailer.tmpl");
3072 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
3073 * following attributes:
3075 * attributes-charset
3076 * attributes-natural-language
3080 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
3082 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3083 "localhost", 0, "/printers/%s", printer
);
3084 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
3088 * Do the request and get back a response...
3091 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
3093 if ((attr
= ippFindAttribute(response
, "job-sheets-supported",
3094 IPP_TAG_ZERO
)) != NULL
)
3097 * Add the job sheets options...
3100 cgiSetVariable("GROUP", cgiText(_("Banners")));
3101 cgiCopyTemplateLang("option-header.tmpl");
3103 cgiSetSize("CHOICES", attr
->num_values
);
3104 cgiSetSize("TEXT", attr
->num_values
);
3105 for (k
= 0; k
< attr
->num_values
; k
++)
3107 cgiSetArray("CHOICES", k
, attr
->values
[k
].string
.text
);
3108 cgiSetArray("TEXT", k
, attr
->values
[k
].string
.text
);
3111 attr
= ippFindAttribute(response
, "job-sheets-default", IPP_TAG_ZERO
);
3113 cgiSetVariable("KEYWORD", "job_sheets_start");
3114 cgiSetVariable("KEYTEXT", cgiText(_("Starting Banner")));
3115 cgiSetVariable("DEFCHOICE", attr
!= NULL
?
3116 attr
->values
[0].string
.text
: "");
3118 cgiCopyTemplateLang("option-pickone.tmpl");
3120 cgiSetVariable("KEYWORD", "job_sheets_end");
3121 cgiSetVariable("KEYTEXT", cgiText(_("Ending Banner")));
3122 cgiSetVariable("DEFCHOICE", attr
!= NULL
&& attr
->num_values
> 1 ?
3123 attr
->values
[1].string
.text
: "");
3125 cgiCopyTemplateLang("option-pickone.tmpl");
3127 cgiCopyTemplateLang("option-trailer.tmpl");
3130 if (ippFindAttribute(response
, "printer-error-policy-supported",
3132 ippFindAttribute(response
, "printer-op-policy-supported",
3136 * Add the error and operation policy options...
3139 cgiSetVariable("GROUP", cgiText(_("Policies")));
3140 cgiCopyTemplateLang("option-header.tmpl");
3146 attr
= ippFindAttribute(response
, "printer-error-policy-supported",
3151 cgiSetSize("CHOICES", attr
->num_values
);
3152 cgiSetSize("TEXT", attr
->num_values
);
3153 for (k
= 0; k
< attr
->num_values
; k
++)
3155 cgiSetArray("CHOICES", k
, attr
->values
[k
].string
.text
);
3156 cgiSetArray("TEXT", k
, attr
->values
[k
].string
.text
);
3159 attr
= ippFindAttribute(response
, "printer-error-policy",
3162 cgiSetVariable("KEYWORD", "printer_error_policy");
3163 cgiSetVariable("KEYTEXT", cgiText(_("Error Policy")));
3164 cgiSetVariable("DEFCHOICE", attr
== NULL
?
3165 "" : attr
->values
[0].string
.text
);
3168 cgiCopyTemplateLang("option-pickone.tmpl");
3171 * Operation policy...
3174 attr
= ippFindAttribute(response
, "printer-op-policy-supported",
3179 cgiSetSize("CHOICES", attr
->num_values
);
3180 cgiSetSize("TEXT", attr
->num_values
);
3181 for (k
= 0; k
< attr
->num_values
; k
++)
3183 cgiSetArray("CHOICES", k
, attr
->values
[k
].string
.text
);
3184 cgiSetArray("TEXT", k
, attr
->values
[k
].string
.text
);
3187 attr
= ippFindAttribute(response
, "printer-op-policy", IPP_TAG_ZERO
);
3189 cgiSetVariable("KEYWORD", "printer_op_policy");
3190 cgiSetVariable("KEYTEXT", cgiText(_("Operation Policy")));
3191 cgiSetVariable("DEFCHOICE", attr
== NULL
?
3192 "" : attr
->values
[0].string
.text
);
3194 cgiCopyTemplateLang("option-pickone.tmpl");
3197 cgiCopyTemplateLang("option-trailer.tmpl");
3200 ippDelete(response
);
3204 * Binary protocol support...
3207 if (ppd
&& ppd
->protocols
&& strstr(ppd
->protocols
, "BCP"))
3209 protocol
= ppdFindAttr(ppd
, "cupsProtocol", NULL
);
3211 cgiSetVariable("GROUP", cgiText(_("PS Binary Protocol")));
3212 cgiCopyTemplateLang("option-header.tmpl");
3214 cgiSetSize("CHOICES", 2);
3215 cgiSetSize("TEXT", 2);
3216 cgiSetArray("CHOICES", 0, "None");
3217 cgiSetArray("TEXT", 0, cgiText(_("None")));
3219 if (strstr(ppd
->protocols
, "TBCP"))
3221 cgiSetArray("CHOICES", 1, "TBCP");
3222 cgiSetArray("TEXT", 1, "TBCP");
3226 cgiSetArray("CHOICES", 1, "BCP");
3227 cgiSetArray("TEXT", 1, "BCP");
3230 cgiSetVariable("KEYWORD", "protocol");
3231 cgiSetVariable("KEYTEXT", cgiText(_("PS Binary Protocol")));
3232 cgiSetVariable("DEFCHOICE", protocol
? protocol
->value
: "None");
3234 cgiCopyTemplateLang("option-pickone.tmpl");
3236 cgiCopyTemplateLang("option-trailer.tmpl");
3239 cgiCopyTemplateLang("set-printer-options-trailer.tmpl");
3245 * Set default options...
3248 fputs("DEBUG: Setting options...\n", stderr
);
3252 out
= cupsTempFile2(tempfile
, sizeof(tempfile
));
3253 in
= cupsFileOpen(filename
, "r");
3257 cgiSetVariable("ERROR", strerror(errno
));
3258 cgiStartHTML(cgiText(_("Set Printer Options")));
3259 cgiCopyTemplateLang("error.tmpl");
3275 while (cupsFileGets(in
, line
, sizeof(line
)))
3277 if (!strncmp(line
, "*cupsProtocol:", 14) && cgiGetVariable("protocol"))
3279 else if (strncmp(line
, "*Default", 8))
3280 cupsFilePrintf(out
, "%s\n", line
);
3284 * Get default option name...
3287 strlcpy(keyword
, line
+ 8, sizeof(keyword
));
3289 for (keyptr
= keyword
; *keyptr
; keyptr
++)
3290 if (*keyptr
== ':' || isspace(*keyptr
& 255))
3295 if (!strcmp(keyword
, "PageRegion") ||
3296 !strcmp(keyword
, "PaperDimension") ||
3297 !strcmp(keyword
, "ImageableArea"))
3298 var
= get_option_value(ppd
, "PageSize", value
, sizeof(value
));
3300 var
= get_option_value(ppd
, keyword
, value
, sizeof(value
));
3303 cupsFilePrintf(out
, "%s\n", line
);
3305 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, var
);
3310 * TODO: We need to set the port-monitor attribute!
3313 if ((var
= cgiGetVariable("protocol")) != NULL
)
3314 cupsFilePrintf(out
, "*cupsProtocol: %s\n", var
);
3322 * Make sure temporary filename is cleared when there is no PPD...
3329 * Build a CUPS_ADD_MODIFY_CLASS/PRINTER request, which requires the
3330 * following attributes:
3332 * attributes-charset
3333 * attributes-natural-language
3335 * job-sheets-default
3336 * printer-error-policy
3341 request
= ippNewRequest(is_class
? CUPS_ADD_MODIFY_CLASS
:
3342 CUPS_ADD_MODIFY_PRINTER
);
3344 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
3347 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
3348 "job-sheets-default", 2, NULL
, NULL
);
3349 attr
->values
[0].string
.text
= _cupsStrAlloc(cgiGetVariable("job_sheets_start"));
3350 attr
->values
[1].string
.text
= _cupsStrAlloc(cgiGetVariable("job_sheets_end"));
3352 if ((var
= cgiGetVariable("printer_error_policy")) != NULL
)
3353 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
3354 "printer-error-policy", NULL
, var
);
3356 if ((var
= cgiGetVariable("printer_op_policy")) != NULL
)
3357 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
3358 "printer-op-policy", NULL
, var
);
3361 * Do the request and get back a response...
3365 ippDelete(cupsDoFileRequest(http
, request
, "/admin/", tempfile
));
3367 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
3369 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
3371 puts("Status: 401\n");
3374 else if (cupsLastError() > IPP_OK_CONFLICT
)
3376 cgiStartHTML(title
);
3377 cgiShowIPPError(_("Unable to set options:"));
3382 * Redirect successful updates back to the printer page...
3385 char refresh
[1024]; /* Refresh URL */
3388 cgiFormEncode(uri
, printer
, sizeof(uri
));
3389 snprintf(refresh
, sizeof(refresh
), "5;URL=/admin/?OP=redirect&URL=/%s/%s",
3390 is_class
? "classes" : "printers", uri
);
3391 cgiSetVariable("refresh_page", refresh
);
3393 cgiStartHTML(title
);
3395 cgiCopyTemplateLang("printer-configured.tmpl");
3410 * 'do_set_sharing()' - Set printer-is-shared value.
3414 do_set_sharing(http_t
*http
) /* I - HTTP connection */
3416 ipp_t
*request
, /* IPP request */
3417 *response
; /* IPP response */
3418 char uri
[HTTP_MAX_URI
]; /* Printer URI */
3419 const char *printer
, /* Printer name */
3420 *is_class
, /* Is a class? */
3421 *shared
; /* Sharing value */
3424 is_class
= cgiGetVariable("IS_CLASS");
3425 printer
= cgiGetVariable("PRINTER_NAME");
3426 shared
= cgiGetVariable("SHARED");
3428 if (!printer
|| !shared
)
3430 cgiSetVariable("ERROR", cgiText(_("Missing form variable!")));
3431 cgiStartHTML(cgiText(_("Set Publishing")));
3432 cgiCopyTemplateLang("error.tmpl");
3438 * Build a CUPS-Add-Printer/CUPS-Add-Class request, which requires the
3439 * following attributes:
3441 * attributes-charset
3442 * attributes-natural-language
3447 request
= ippNewRequest(is_class
? CUPS_ADD_CLASS
: CUPS_ADD_PRINTER
);
3449 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3450 "localhost", 0, is_class
? "/classes/%s" : "/printers/%s",
3452 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
3455 ippAddBoolean(request
, IPP_TAG_OPERATION
, "printer-is-shared", atoi(shared
));
3458 * Do the request and get back a response...
3461 if ((response
= cupsDoRequest(http
, request
, "/admin/")) != NULL
)
3463 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
3465 ippDelete(response
);
3468 if (cupsLastError() == IPP_NOT_AUTHORIZED
)
3470 puts("Status: 401\n");
3473 else if (cupsLastError() > IPP_OK_CONFLICT
)
3475 cgiStartHTML(cgiText(_("Set Publishing")));
3476 cgiShowIPPError(_("Unable to change printer-is-shared attribute:"));
3481 * Redirect successful updates back to the printer page...
3484 char url
[1024], /* Printer/class URL */
3485 refresh
[1024]; /* Refresh URL */
3488 cgiRewriteURL(uri
, url
, sizeof(url
), NULL
);
3489 cgiFormEncode(uri
, url
, sizeof(uri
));
3490 snprintf(refresh
, sizeof(refresh
), "5;URL=/admin/?OP=redirect&URL=%s", uri
);
3491 cgiSetVariable("refresh_page", refresh
);
3493 cgiStartHTML(cgiText(_("Set Publishing")));
3494 cgiCopyTemplateLang(is_class
? "class-modified.tmpl" :
3495 "printer-modified.tmpl");
3503 * 'get_option_value()' - Return the value of an option.
3505 * This function also handles generation of custom option values.
3508 static char * /* O - Value string or NULL on error */
3510 ppd_file_t
*ppd
, /* I - PPD file */
3511 const char *name
, /* I - Option name */
3512 char *buffer
, /* I - String buffer */
3513 size_t bufsize
) /* I - Size of buffer */
3515 char *bufptr
, /* Pointer into buffer */
3516 *bufend
; /* End of buffer */
3517 ppd_coption_t
*coption
; /* Custom option */
3518 ppd_cparam_t
*cparam
; /* Current custom parameter */
3519 char keyword
[256]; /* Parameter name */
3520 const char *val
, /* Parameter value */
3521 *uval
; /* Units value */
3522 long integer
; /* Integer value */
3523 double number
, /* Number value */
3524 number_points
; /* Number in points */
3528 * See if we have a custom option choice...
3531 if ((val
= cgiGetVariable(name
)) == NULL
)
3539 else if (strcasecmp(val
, "Custom") ||
3540 (coption
= ppdFindCustomOption(ppd
, name
)) == NULL
)
3543 * Not a custom choice...
3546 strlcpy(buffer
, val
, bufsize
);
3551 * OK, we have a custom option choice, format it...
3556 if (!strcmp(coption
->keyword
, "PageSize"))
3558 const char *lval
; /* Length string value */
3559 double width
, /* Width value */
3560 width_points
, /* Width in points */
3561 length
, /* Length value */
3562 length_points
; /* Length in points */
3565 val
= cgiGetVariable("PageSize.Width");
3566 lval
= cgiGetVariable("PageSize.Height");
3567 uval
= cgiGetVariable("PageSize.Units");
3569 if (!val
|| !lval
|| !uval
||
3570 (width
= strtod(val
, NULL
)) == 0.0 ||
3571 (length
= strtod(lval
, NULL
)) == 0.0 ||
3572 (strcmp(uval
, "pt") && strcmp(uval
, "in") && strcmp(uval
, "ft") &&
3573 strcmp(uval
, "cm") && strcmp(uval
, "mm") && strcmp(uval
, "m")))
3576 width_points
= get_points(width
, uval
);
3577 length_points
= get_points(length
, uval
);
3579 if (width_points
< ppd
->custom_min
[0] ||
3580 width_points
> ppd
->custom_max
[0] ||
3581 length_points
< ppd
->custom_min
[1] ||
3582 length_points
> ppd
->custom_max
[1])
3585 snprintf(buffer
, bufsize
, "Custom.%gx%g%s", width
, length
, uval
);
3587 else if (cupsArrayCount(coption
->params
) == 1)
3589 cparam
= ppdFirstCustomParam(coption
);
3590 snprintf(keyword
, sizeof(keyword
), "%s.%s", coption
->keyword
, cparam
->name
);
3592 if ((val
= cgiGetVariable(keyword
)) == NULL
)
3595 switch (cparam
->type
)
3597 case PPD_CUSTOM_CURVE
:
3598 case PPD_CUSTOM_INVCURVE
:
3599 case PPD_CUSTOM_REAL
:
3600 if ((number
= strtod(val
, NULL
)) == 0.0 ||
3601 number
< cparam
->minimum
.custom_real
||
3602 number
> cparam
->maximum
.custom_real
)
3605 snprintf(buffer
, bufsize
, "Custom.%g", number
);
3608 case PPD_CUSTOM_INT
:
3609 if (!*val
|| (integer
= strtol(val
, NULL
, 10)) == LONG_MIN
||
3610 integer
== LONG_MAX
||
3611 integer
< cparam
->minimum
.custom_int
||
3612 integer
> cparam
->maximum
.custom_int
)
3615 snprintf(buffer
, bufsize
, "Custom.%ld", integer
);
3618 case PPD_CUSTOM_POINTS
:
3619 snprintf(keyword
, sizeof(keyword
), "%s.Units", coption
->keyword
);
3621 if ((number
= strtod(val
, NULL
)) == 0.0 ||
3622 (uval
= cgiGetVariable(keyword
)) == NULL
||
3623 (strcmp(uval
, "pt") && strcmp(uval
, "in") && strcmp(uval
, "ft") &&
3624 strcmp(uval
, "cm") && strcmp(uval
, "mm") && strcmp(uval
, "m")))
3627 number_points
= get_points(number
, uval
);
3628 if (number_points
< cparam
->minimum
.custom_points
||
3629 number_points
> cparam
->maximum
.custom_points
)
3632 snprintf(buffer
, bufsize
, "Custom.%g%s", number
, uval
);
3635 case PPD_CUSTOM_PASSCODE
:
3636 for (uval
= val
; *uval
; uval
++)
3637 if (!isdigit(*uval
& 255))
3640 case PPD_CUSTOM_PASSWORD
:
3641 case PPD_CUSTOM_STRING
:
3642 integer
= (long)strlen(val
);
3643 if (integer
< cparam
->minimum
.custom_string
||
3644 integer
> cparam
->maximum
.custom_string
)
3647 snprintf(buffer
, bufsize
, "Custom.%s", val
);
3653 const char *prefix
= "{"; /* Prefix string */
3657 bufend
= buffer
+ bufsize
;
3659 for (cparam
= ppdFirstCustomParam(coption
);
3661 cparam
= ppdNextCustomParam(coption
))
3663 snprintf(keyword
, sizeof(keyword
), "%s.%s", coption
->keyword
,
3666 if ((val
= cgiGetVariable(keyword
)) == NULL
)
3669 snprintf(bufptr
, bufend
- bufptr
, "%s%s=", prefix
, cparam
->name
);
3670 bufptr
+= strlen(bufptr
);
3673 switch (cparam
->type
)
3675 case PPD_CUSTOM_CURVE
:
3676 case PPD_CUSTOM_INVCURVE
:
3677 case PPD_CUSTOM_REAL
:
3678 if ((number
= strtod(val
, NULL
)) == 0.0 ||
3679 number
< cparam
->minimum
.custom_real
||
3680 number
> cparam
->maximum
.custom_real
)
3683 snprintf(bufptr
, bufend
- bufptr
, "%g", number
);
3686 case PPD_CUSTOM_INT
:
3687 if (!*val
|| (integer
= strtol(val
, NULL
, 10)) == LONG_MIN
||
3688 integer
== LONG_MAX
||
3689 integer
< cparam
->minimum
.custom_int
||
3690 integer
> cparam
->maximum
.custom_int
)
3693 snprintf(bufptr
, bufend
- bufptr
, "%ld", integer
);
3696 case PPD_CUSTOM_POINTS
:
3697 snprintf(keyword
, sizeof(keyword
), "%s.Units", coption
->keyword
);
3699 if ((number
= strtod(val
, NULL
)) == 0.0 ||
3700 (uval
= cgiGetVariable(keyword
)) == NULL
||
3701 (strcmp(uval
, "pt") && strcmp(uval
, "in") &&
3702 strcmp(uval
, "ft") && strcmp(uval
, "cm") &&
3703 strcmp(uval
, "mm") && strcmp(uval
, "m")))
3706 number_points
= get_points(number
, uval
);
3707 if (number_points
< cparam
->minimum
.custom_points
||
3708 number_points
> cparam
->maximum
.custom_points
)
3711 snprintf(bufptr
, bufend
- bufptr
, "%g%s", number
, uval
);
3714 case PPD_CUSTOM_PASSCODE
:
3715 for (uval
= val
; *uval
; uval
++)
3716 if (!isdigit(*uval
& 255))
3719 case PPD_CUSTOM_PASSWORD
:
3720 case PPD_CUSTOM_STRING
:
3721 integer
= (long)strlen(val
);
3722 if (integer
< cparam
->minimum
.custom_string
||
3723 integer
> cparam
->maximum
.custom_string
)
3726 if ((bufptr
+ 2) > bufend
)
3732 while (*val
&& bufptr
< bufend
)
3734 if (*val
== '\\' || *val
== '\"')
3736 if ((bufptr
+ 1) >= bufend
)
3745 if (bufptr
>= bufend
)
3754 bufptr
+= strlen(bufptr
);
3757 if (bufptr
== buffer
|| (bufend
- bufptr
) < 2)
3760 strcpy(bufptr
, "}");
3768 * 'get_points()' - Get a value in points.
3771 static double /* O - Number in points */
3772 get_points(double number
, /* I - Original number */
3773 const char *uval
) /* I - Units */
3775 if (!strcmp(uval
, "mm")) /* Millimeters */
3776 return (number
* 72.0 / 25.4);
3777 else if (!strcmp(uval
, "cm")) /* Centimeters */
3778 return (number
* 72.0 / 2.54);
3779 else if (!strcmp(uval
, "in")) /* Inches */
3780 return (number
* 72.0);
3781 else if (!strcmp(uval
, "ft")) /* Feet */
3782 return (number
* 72.0 * 12.0);
3783 else if (!strcmp(uval
, "m")) /* Meters */
3784 return (number
* 72.0 / 0.0254);
3791 * 'match_string()' - Return the number of matching characters.
3794 static int /* O - Number of matching characters */
3795 match_string(const char *a
, /* I - First string */
3796 const char *b
) /* I - Second string */
3798 int count
; /* Number of matching characters */
3802 * Loop through both strings until we hit the end of either or we find
3803 * a non-matching character. For the purposes of comparison, we ignore
3804 * whitespace and do a case-insensitive comparison so that we have a
3805 * better chance of finding a match...
3808 for (count
= 0; *a
&& *b
; a
++, b
++, count
++)
3811 * Skip leading whitespace characters...
3814 while (isspace(*a
& 255))
3817 while (isspace(*b
& 255))
3821 * Break out if we run out of characters...
3828 * Do a case-insensitive comparison of the next two chars...
3831 if (tolower(*a
& 255) != tolower(*b
& 255))
3840 * End of "$Id: admin.c 7438 2008-04-09 03:27:37Z mike $".