]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
58dc1933 | 2 | * "$Id: admin.c 8029 2008-10-08 21:07:45Z mike $" |
ef416fc2 | 3 | * |
0268488e | 4 | * Administration CGI for CUPS. |
ef416fc2 | 5 | * |
0268488e | 6 | * Copyright 2007-2011 by Apple Inc. |
f7deaa1a | 7 | * Copyright 1997-2007 by Easy Software Products. |
ef416fc2 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 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/". | |
ef416fc2 | 14 | * |
15 | * Contents: | |
16 | * | |
17 | * main() - Main entry for CGI. | |
58dc1933 | 18 | * choose_device_cb() - Add a device to the device selection page. |
f7deaa1a | 19 | * do_add_rss_subscription() - Add a RSS subscription. |
ef416fc2 | 20 | * do_am_class() - Add or modify a class. |
21 | * do_am_printer() - Add or modify a printer. | |
f7deaa1a | 22 | * do_cancel_subscription() - Cancel a subscription. |
ef416fc2 | 23 | * do_config_server() - Configure server settings. |
749b1e90 MS |
24 | * do_delete_class() - Delete a class. |
25 | * do_delete_printer() - Delete a printer. | |
26 | * do_export() - Export printers to Samba. | |
27 | * do_list_printers() - List available printers. | |
28 | * do_menu() - Show the main menu. | |
ef416fc2 | 29 | * do_set_allowed_users() - Set the allowed/denied users for a queue. |
58dc1933 | 30 | * do_set_default() - Set the server default printer/class. |
323c5de1 | 31 | * do_set_options() - Configure the default options for a queue. |
749b1e90 MS |
32 | * do_set_sharing() - Set printer-is-shared value. |
33 | * get_option_value() - Return the value of an option. | |
34 | * get_points() - Get a value in points. | |
ef416fc2 | 35 | */ |
36 | ||
37 | /* | |
38 | * Include necessary headers... | |
39 | */ | |
40 | ||
41 | #include "cgi-private.h" | |
757d2cad | 42 | #include <cups/adminutil.h> |
aaf19ab0 | 43 | #include <cups/ppd.h> |
ef416fc2 | 44 | #include <errno.h> |
fa73b229 | 45 | #include <unistd.h> |
46 | #include <fcntl.h> | |
47 | #include <sys/wait.h> | |
749b1e90 | 48 | #include <limits.h> |
ef416fc2 | 49 | |
50 | ||
f8b3a85b MS |
51 | /* |
52 | * Local globals... | |
53 | */ | |
54 | ||
55 | static int current_device = 0; /* Current device shown */ | |
56 | ||
57 | ||
ef416fc2 | 58 | /* |
59 | * Local functions... | |
60 | */ | |
61 | ||
58dc1933 MS |
62 | static void choose_device_cb(const char *device_class, |
63 | const char *device_id, const char *device_info, | |
64 | const char *device_make_and_model, | |
65 | const char *device_uri, | |
66 | const char *device_location, | |
f8b3a85b | 67 | const char *title); |
f7deaa1a | 68 | static void do_add_rss_subscription(http_t *http); |
fa73b229 | 69 | static void do_am_class(http_t *http, int modify); |
70 | static void do_am_printer(http_t *http, int modify); | |
f7deaa1a | 71 | static void do_cancel_subscription(http_t *http); |
fa73b229 | 72 | static void do_config_server(http_t *http); |
73 | static void do_delete_class(http_t *http); | |
74 | static void do_delete_printer(http_t *http); | |
75 | static void do_export(http_t *http); | |
323c5de1 | 76 | static void do_list_printers(http_t *http); |
fa73b229 | 77 | static void do_menu(http_t *http); |
fa73b229 | 78 | static void do_set_allowed_users(http_t *http); |
58dc1933 MS |
79 | static void do_set_default(http_t *http); |
80 | static void do_set_options(http_t *http, int is_class); | |
fa73b229 | 81 | static void do_set_sharing(http_t *http); |
749b1e90 MS |
82 | static char *get_option_value(ppd_file_t *ppd, const char *name, |
83 | char *buffer, size_t bufsize); | |
84 | static double get_points(double number, const char *uval); | |
ef416fc2 | 85 | |
86 | ||
87 | /* | |
88 | * 'main()' - Main entry for CGI. | |
89 | */ | |
90 | ||
91 | int /* O - Exit status */ | |
92 | main(int argc, /* I - Number of command-line arguments */ | |
93 | char *argv[]) /* I - Command-line arguments */ | |
94 | { | |
ef416fc2 | 95 | http_t *http; /* Connection to the server */ |
96 | const char *op; /* Operation name */ | |
97 | ||
98 | ||
ef416fc2 | 99 | /* |
100 | * Connect to the HTTP server... | |
101 | */ | |
102 | ||
bd7854cb | 103 | fputs("DEBUG: admin.cgi started...\n", stderr); |
104 | ||
ef416fc2 | 105 | http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption()); |
106 | ||
a4d04587 | 107 | if (!http) |
108 | { | |
109 | perror("ERROR: Unable to connect to cupsd"); | |
f301802f | 110 | fprintf(stderr, "DEBUG: cupsServer()=\"%s\"\n", |
111 | cupsServer() ? cupsServer() : "(null)"); | |
a4d04587 | 112 | fprintf(stderr, "DEBUG: ippPort()=%d\n", ippPort()); |
113 | fprintf(stderr, "DEBUG: cupsEncryption()=%d\n", cupsEncryption()); | |
114 | exit(1); | |
115 | } | |
116 | ||
bd7854cb | 117 | fprintf(stderr, "DEBUG: http=%p\n", http); |
118 | ||
ef416fc2 | 119 | /* |
120 | * Set the web interface section... | |
121 | */ | |
122 | ||
123 | cgiSetVariable("SECTION", "admin"); | |
ef55b745 | 124 | cgiSetVariable("REFRESH_PAGE", ""); |
ef416fc2 | 125 | |
126 | /* | |
127 | * See if we have form data... | |
128 | */ | |
129 | ||
0af14961 | 130 | if (!cgiInitialize() || !cgiGetVariable("OP")) |
ef416fc2 | 131 | { |
132 | /* | |
133 | * Nope, send the administration menu... | |
134 | */ | |
135 | ||
bd7854cb | 136 | fputs("DEBUG: No form data, showing main menu...\n", stderr); |
137 | ||
fa73b229 | 138 | do_menu(http); |
ef416fc2 | 139 | } |
2e4ff8af | 140 | else if ((op = cgiGetVariable("OP")) != NULL && cgiIsPOST()) |
ef416fc2 | 141 | { |
142 | /* | |
143 | * Do the operation... | |
144 | */ | |
145 | ||
bd7854cb | 146 | fprintf(stderr, "DEBUG: op=\"%s\"...\n", op); |
147 | ||
0268488e MS |
148 | if (!*op) |
149 | { | |
150 | const char *printer = getenv("PRINTER_NAME"), | |
151 | /* Printer or class name */ | |
152 | *server_port = getenv("SERVER_PORT"); | |
153 | /* Port number string */ | |
154 | int port = atoi(server_port ? server_port : "0"); | |
155 | /* Port number */ | |
156 | char uri[1024]; /* URL */ | |
157 | ||
158 | if (printer) | |
159 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), | |
160 | getenv("HTTPS") ? "https" : "http", NULL, | |
161 | getenv("SERVER_NAME"), port, "/%s/%s", | |
162 | cgiGetVariable("IS_CLASS") ? "classes" : "printers", | |
163 | printer); | |
164 | else | |
165 | httpAssembleURI(HTTP_URI_CODING_ALL, uri, sizeof(uri), | |
166 | getenv("HTTPS") ? "https" : "http", NULL, | |
167 | getenv("SERVER_NAME"), port, "/admin"); | |
168 | ||
169 | printf("Location: %s\n\n", uri); | |
170 | } | |
171 | else if (!strcmp(op, "set-allowed-users")) | |
fa73b229 | 172 | do_set_allowed_users(http); |
ef416fc2 | 173 | else if (!strcmp(op, "set-as-default")) |
58dc1933 | 174 | do_set_default(http); |
ef416fc2 | 175 | else if (!strcmp(op, "set-sharing")) |
fa73b229 | 176 | do_set_sharing(http); |
355e94dc MS |
177 | else if (!strcmp(op, "find-new-printers") || |
178 | !strcmp(op, "list-available-printers")) | |
323c5de1 | 179 | do_list_printers(http); |
ef416fc2 | 180 | else if (!strcmp(op, "add-class")) |
fa73b229 | 181 | do_am_class(http, 0); |
ef416fc2 | 182 | else if (!strcmp(op, "add-printer")) |
fa73b229 | 183 | do_am_printer(http, 0); |
ef416fc2 | 184 | else if (!strcmp(op, "modify-class")) |
fa73b229 | 185 | do_am_class(http, 1); |
ef416fc2 | 186 | else if (!strcmp(op, "modify-printer")) |
fa73b229 | 187 | do_am_printer(http, 1); |
ef416fc2 | 188 | else if (!strcmp(op, "delete-class")) |
fa73b229 | 189 | do_delete_class(http); |
ef416fc2 | 190 | else if (!strcmp(op, "delete-printer")) |
fa73b229 | 191 | do_delete_printer(http); |
323c5de1 | 192 | else if (!strcmp(op, "set-class-options")) |
193 | do_set_options(http, 1); | |
ef416fc2 | 194 | else if (!strcmp(op, "set-printer-options")) |
323c5de1 | 195 | do_set_options(http, 0); |
ef416fc2 | 196 | else if (!strcmp(op, "config-server")) |
fa73b229 | 197 | do_config_server(http); |
198 | else if (!strcmp(op, "export-samba")) | |
199 | do_export(http); | |
f7deaa1a | 200 | else if (!strcmp(op, "add-rss-subscription")) |
201 | do_add_rss_subscription(http); | |
202 | else if (!strcmp(op, "cancel-subscription")) | |
203 | do_cancel_subscription(http); | |
ef416fc2 | 204 | else |
205 | { | |
206 | /* | |
749b1e90 | 207 | * Bad operation code - display an error... |
ef416fc2 | 208 | */ |
209 | ||
fa73b229 | 210 | cgiStartHTML(cgiText(_("Administration"))); |
211 | cgiCopyTemplateLang("error-op.tmpl"); | |
ef416fc2 | 212 | cgiEndHTML(); |
213 | } | |
ef416fc2 | 214 | } |
2e4ff8af MS |
215 | else if (op && !strcmp(op, "redirect")) |
216 | { | |
ef55b745 | 217 | const char *url; /* Redirection URL... */ |
2e4ff8af MS |
218 | char prefix[1024]; /* URL prefix */ |
219 | ||
220 | ||
221 | if (getenv("HTTPS")) | |
222 | snprintf(prefix, sizeof(prefix), "https://%s:%s", | |
223 | getenv("SERVER_NAME"), getenv("SERVER_PORT")); | |
224 | else | |
225 | snprintf(prefix, sizeof(prefix), "http://%s:%s", | |
226 | getenv("SERVER_NAME"), getenv("SERVER_PORT")); | |
227 | ||
0af14961 MS |
228 | fprintf(stderr, "DEBUG: redirecting with prefix %s!\n", prefix); |
229 | ||
2e4ff8af | 230 | if ((url = cgiGetVariable("URL")) != NULL) |
ef55b745 MS |
231 | { |
232 | char encoded[1024], /* Encoded URL string */ | |
233 | *ptr; /* Pointer into encoded string */ | |
234 | ||
235 | ||
236 | ptr = encoded; | |
237 | if (*url != '/') | |
238 | *ptr++ = '/'; | |
239 | ||
240 | for (; *url && ptr < (encoded + sizeof(encoded) - 4); url ++) | |
241 | { | |
242 | if (strchr("%@&+ <>#=", *url) || *url < ' ' || *url & 128) | |
243 | { | |
244 | /* | |
245 | * Percent-encode this character; safe because we have at least 4 | |
246 | * bytes left in the array... | |
247 | */ | |
248 | ||
249 | sprintf(ptr, "%%%02X", *url & 255); | |
250 | ptr += 3; | |
251 | } | |
252 | else | |
253 | *ptr++ = *url; | |
254 | } | |
255 | ||
256 | *ptr = '\0'; | |
257 | ||
258 | if (*url) | |
259 | { | |
260 | /* | |
261 | * URL was too long, just redirect to the admin page... | |
262 | */ | |
263 | ||
264 | printf("Location: %s/admin\n\n", prefix); | |
265 | } | |
266 | else | |
267 | { | |
268 | /* | |
269 | * URL is OK, redirect there... | |
270 | */ | |
271 | ||
272 | printf("Location: %s%s\n\n", prefix, encoded); | |
273 | } | |
274 | } | |
2e4ff8af MS |
275 | else |
276 | printf("Location: %s/admin\n\n", prefix); | |
277 | } | |
ef416fc2 | 278 | else |
279 | { | |
280 | /* | |
749b1e90 | 281 | * Form data but no operation code - display an error... |
ef416fc2 | 282 | */ |
283 | ||
fa73b229 | 284 | cgiStartHTML(cgiText(_("Administration"))); |
285 | cgiCopyTemplateLang("error-op.tmpl"); | |
ef416fc2 | 286 | cgiEndHTML(); |
287 | } | |
288 | ||
289 | /* | |
fa73b229 | 290 | * Close the HTTP server connection... |
ef416fc2 | 291 | */ |
292 | ||
fa73b229 | 293 | httpClose(http); |
ef416fc2 | 294 | |
295 | /* | |
296 | * Return with no errors... | |
297 | */ | |
298 | ||
299 | return (0); | |
300 | } | |
301 | ||
302 | ||
58dc1933 MS |
303 | /* |
304 | * 'choose_device_cb()' - Add a device to the device selection page. | |
305 | */ | |
306 | ||
307 | static void | |
308 | choose_device_cb( | |
309 | const char *device_class, /* I - Class */ | |
310 | const char *device_id, /* I - 1284 device ID */ | |
311 | const char *device_info, /* I - Description */ | |
312 | const char *device_make_and_model, /* I - Make and model */ | |
313 | const char *device_uri, /* I - Device URI */ | |
314 | const char *device_location, /* I - Location */ | |
f8b3a85b | 315 | const char *title) /* I - Page title */ |
58dc1933 | 316 | { |
f8b3a85b MS |
317 | /* |
318 | * For modern browsers, start a multi-part page so we can show that something | |
319 | * is happening. Non-modern browsers just get everything at the end... | |
320 | */ | |
321 | ||
322 | if (current_device == 0 && cgiSupportsMultipart()) | |
323 | { | |
324 | cgiStartMultipart(); | |
325 | cgiStartHTML(title); | |
326 | cgiCopyTemplateLang("choose-device.tmpl"); | |
327 | cgiEndHTML(); | |
328 | fflush(stdout); | |
329 | } | |
330 | ||
331 | ||
58dc1933 MS |
332 | /* |
333 | * Add the device to the array... | |
334 | */ | |
335 | ||
f8b3a85b MS |
336 | cgiSetArray("device_class", current_device, device_class); |
337 | cgiSetArray("device_id", current_device, device_id); | |
338 | cgiSetArray("device_info", current_device, device_info); | |
339 | cgiSetArray("device_make_and_model", current_device, device_make_and_model); | |
340 | cgiSetArray("device_uri", current_device, device_uri); | |
341 | cgiSetArray("device_location", current_device, device_location); | |
58dc1933 | 342 | |
f8b3a85b | 343 | current_device ++; |
58dc1933 MS |
344 | } |
345 | ||
346 | ||
f7deaa1a | 347 | /* |
348 | * 'do_add_rss_subscription()' - Add a RSS subscription. | |
349 | */ | |
350 | ||
351 | static void | |
352 | do_add_rss_subscription(http_t *http) /* I - HTTP connection */ | |
353 | { | |
354 | ipp_t *request, /* IPP request data */ | |
355 | *response; /* IPP response data */ | |
356 | char rss_uri[1024]; /* RSS notify-recipient URI */ | |
357 | int num_events; /* Number of events */ | |
358 | const char *events[12], /* Subscribed events */ | |
359 | *subscription_name, /* Subscription name */ | |
360 | *printer_uri, /* Printer URI */ | |
361 | *ptr, /* Pointer into name */ | |
362 | *user; /* Username */ | |
363 | int max_events; /* Maximum number of events */ | |
364 | ||
365 | ||
366 | /* | |
367 | * See if we have all of the required information... | |
368 | */ | |
369 | ||
370 | subscription_name = cgiGetVariable("SUBSCRIPTION_NAME"); | |
371 | printer_uri = cgiGetVariable("PRINTER_URI"); | |
372 | num_events = 0; | |
373 | ||
374 | if (cgiGetVariable("EVENT_JOB_CREATED")) | |
375 | events[num_events ++] = "job-created"; | |
376 | if (cgiGetVariable("EVENT_JOB_COMPLETED")) | |
377 | events[num_events ++] = "job-completed"; | |
378 | if (cgiGetVariable("EVENT_JOB_STOPPED")) | |
379 | events[num_events ++] = "job-stopped"; | |
380 | if (cgiGetVariable("EVENT_JOB_CONFIG_CHANGED")) | |
381 | events[num_events ++] = "job-config-changed"; | |
382 | if (cgiGetVariable("EVENT_PRINTER_STOPPED")) | |
383 | events[num_events ++] = "printer-stopped"; | |
384 | if (cgiGetVariable("EVENT_PRINTER_ADDED")) | |
385 | events[num_events ++] = "printer-added"; | |
386 | if (cgiGetVariable("EVENT_PRINTER_MODIFIED")) | |
387 | events[num_events ++] = "printer-modified"; | |
388 | if (cgiGetVariable("EVENT_PRINTER_DELETED")) | |
389 | events[num_events ++] = "printer-deleted"; | |
390 | if (cgiGetVariable("EVENT_SERVER_STARTED")) | |
391 | events[num_events ++] = "server-started"; | |
392 | if (cgiGetVariable("EVENT_SERVER_STOPPED")) | |
393 | events[num_events ++] = "server-stopped"; | |
394 | if (cgiGetVariable("EVENT_SERVER_RESTARTED")) | |
395 | events[num_events ++] = "server-restarted"; | |
396 | if (cgiGetVariable("EVENT_SERVER_AUDIT")) | |
397 | events[num_events ++] = "server-audit"; | |
398 | ||
399 | if ((ptr = cgiGetVariable("MAX_EVENTS")) != NULL) | |
400 | max_events = atoi(ptr); | |
401 | else | |
402 | max_events = 0; | |
403 | ||
404 | if (!subscription_name || !printer_uri || !num_events || | |
405 | max_events <= 0 || max_events > 9999) | |
406 | { | |
407 | /* | |
408 | * Don't have everything we need, so get the available printers | |
409 | * and classes and (re)show the add page... | |
410 | */ | |
411 | ||
ef55b745 MS |
412 | if (cgiGetVariable("EVENT_JOB_CREATED")) |
413 | cgiSetVariable("EVENT_JOB_CREATED", "CHECKED"); | |
414 | if (cgiGetVariable("EVENT_JOB_COMPLETED")) | |
415 | cgiSetVariable("EVENT_JOB_COMPLETED", "CHECKED"); | |
416 | if (cgiGetVariable("EVENT_JOB_STOPPED")) | |
417 | cgiSetVariable("EVENT_JOB_STOPPED", "CHECKED"); | |
418 | if (cgiGetVariable("EVENT_JOB_CONFIG_CHANGED")) | |
419 | cgiSetVariable("EVENT_JOB_CONFIG_CHANGED", "CHECKED"); | |
420 | if (cgiGetVariable("EVENT_PRINTER_STOPPED")) | |
421 | cgiSetVariable("EVENT_PRINTER_STOPPED", "CHECKED"); | |
422 | if (cgiGetVariable("EVENT_PRINTER_ADDED")) | |
423 | cgiSetVariable("EVENT_PRINTER_ADDED", "CHECKED"); | |
424 | if (cgiGetVariable("EVENT_PRINTER_MODIFIED")) | |
425 | cgiSetVariable("EVENT_PRINTER_MODIFIED", "CHECKED"); | |
426 | if (cgiGetVariable("EVENT_PRINTER_DELETED")) | |
427 | cgiSetVariable("EVENT_PRINTER_DELETED", "CHECKED"); | |
428 | if (cgiGetVariable("EVENT_SERVER_STARTED")) | |
429 | cgiSetVariable("EVENT_SERVER_STARTED", "CHECKED"); | |
430 | if (cgiGetVariable("EVENT_SERVER_STOPPED")) | |
431 | cgiSetVariable("EVENT_SERVER_STOPPED", "CHECKED"); | |
432 | if (cgiGetVariable("EVENT_SERVER_RESTARTED")) | |
433 | cgiSetVariable("EVENT_SERVER_RESTARTED", "CHECKED"); | |
434 | if (cgiGetVariable("EVENT_SERVER_AUDIT")) | |
435 | cgiSetVariable("EVENT_SERVER_AUDIT", "CHECKED"); | |
436 | ||
f7deaa1a | 437 | request = ippNewRequest(CUPS_GET_PRINTERS); |
438 | response = cupsDoRequest(http, request, "/"); | |
439 | ||
440 | cgiSetIPPVars(response, NULL, NULL, NULL, 0); | |
441 | ||
442 | ippDelete(response); | |
443 | ||
444 | cgiStartHTML(cgiText(_("Add RSS Subscription"))); | |
445 | ||
446 | cgiCopyTemplateLang("add-rss-subscription.tmpl"); | |
447 | ||
448 | cgiEndHTML(); | |
449 | return; | |
450 | } | |
451 | ||
839a51c8 MS |
452 | /* |
453 | * Make sure we have a username... | |
454 | */ | |
455 | ||
456 | if ((user = getenv("REMOTE_USER")) == NULL) | |
457 | { | |
458 | puts("Status: 401\n"); | |
459 | exit(0); | |
460 | } | |
461 | ||
f7deaa1a | 462 | /* |
463 | * Validate the subscription name... | |
464 | */ | |
465 | ||
466 | for (ptr = subscription_name; *ptr; ptr ++) | |
467 | if ((*ptr >= 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' || | |
468 | *ptr == '?' || *ptr == '#') | |
469 | break; | |
470 | ||
471 | if (*ptr) | |
472 | { | |
473 | cgiSetVariable("ERROR", | |
474 | cgiText(_("The subscription name may not " | |
475 | "contain spaces, slashes (/), question marks (?), " | |
476 | "or the pound sign (#)."))); | |
477 | cgiStartHTML(_("Add RSS Subscription")); | |
478 | cgiCopyTemplateLang("error.tmpl"); | |
479 | cgiEndHTML(); | |
480 | return; | |
481 | } | |
482 | ||
483 | /* | |
484 | * Add the subscription... | |
485 | */ | |
486 | ||
487 | ptr = subscription_name + strlen(subscription_name) - 4; | |
488 | if (ptr < subscription_name || strcmp(ptr, ".rss")) | |
489 | httpAssembleURIf(HTTP_URI_CODING_ALL, rss_uri, sizeof(rss_uri), "rss", | |
490 | NULL, NULL, 0, "/%s.rss?max_events=%d", subscription_name, | |
491 | max_events); | |
492 | else | |
493 | httpAssembleURIf(HTTP_URI_CODING_ALL, rss_uri, sizeof(rss_uri), "rss", | |
494 | NULL, NULL, 0, "/%s?max_events=%d", subscription_name, | |
495 | max_events); | |
496 | ||
497 | request = ippNewRequest(IPP_CREATE_PRINTER_SUBSCRIPTION); | |
498 | ||
499 | if (!strcasecmp(printer_uri, "#ALL#")) | |
500 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
501 | NULL, "ipp://localhost/"); | |
502 | else | |
503 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
504 | NULL, printer_uri); | |
505 | ||
f7deaa1a | 506 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", |
507 | NULL, user); | |
508 | ||
7594b224 | 509 | ippAddString(request, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI, |
510 | "notify-recipient-uri", NULL, rss_uri); | |
f7deaa1a | 511 | ippAddStrings(request, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD, "notify-events", |
512 | num_events, NULL, events); | |
513 | ippAddInteger(request, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | |
514 | "notify-lease-duration", 0); | |
515 | ||
516 | ippDelete(cupsDoRequest(http, request, "/")); | |
517 | ||
355e94dc MS |
518 | if (cupsLastError() == IPP_NOT_AUTHORIZED) |
519 | { | |
520 | puts("Status: 401\n"); | |
521 | exit(0); | |
522 | } | |
523 | else if (cupsLastError() > IPP_OK_CONFLICT) | |
f7deaa1a | 524 | { |
525 | cgiStartHTML(_("Add RSS Subscription")); | |
526 | cgiShowIPPError(_("Unable to add RSS subscription:")); | |
527 | } | |
528 | else | |
529 | { | |
530 | /* | |
531 | * Redirect successful updates back to the admin page... | |
532 | */ | |
533 | ||
534 | cgiSetVariable("refresh_page", "5;URL=/admin"); | |
535 | cgiStartHTML(_("Add RSS Subscription")); | |
536 | cgiCopyTemplateLang("subscription-added.tmpl"); | |
537 | } | |
538 | ||
539 | cgiEndHTML(); | |
540 | } | |
541 | ||
542 | ||
ef416fc2 | 543 | /* |
544 | * 'do_am_class()' - Add or modify a class. | |
545 | */ | |
546 | ||
547 | static void | |
fa73b229 | 548 | do_am_class(http_t *http, /* I - HTTP connection */ |
549 | int modify) /* I - Modify the printer? */ | |
ef416fc2 | 550 | { |
551 | int i, j; /* Looping vars */ | |
552 | int element; /* Element number */ | |
553 | int num_printers; /* Number of printers */ | |
554 | ipp_t *request, /* IPP request */ | |
555 | *response; /* IPP response */ | |
556 | ipp_attribute_t *attr; /* member-uris attribute */ | |
ef416fc2 | 557 | char uri[HTTP_MAX_URI]; /* Device or printer URI */ |
558 | const char *name, /* Pointer to class name */ | |
ef55b745 | 559 | *op, /* Operation name */ |
ef416fc2 | 560 | *ptr; /* Pointer to CGI variable */ |
561 | const char *title; /* Title of page */ | |
562 | static const char * const pattrs[] = /* Requested printer attributes */ | |
563 | { | |
564 | "member-names", | |
565 | "printer-info", | |
566 | "printer-location" | |
567 | }; | |
568 | ||
569 | ||
fa73b229 | 570 | title = cgiText(modify ? _("Modify Class") : _("Add Class")); |
ef55b745 | 571 | op = cgiGetVariable("OP"); |
ef416fc2 | 572 | name = cgiGetVariable("PRINTER_NAME"); |
573 | ||
574 | if (cgiGetVariable("PRINTER_LOCATION") == NULL) | |
575 | { | |
576 | /* | |
577 | * Build a CUPS_GET_PRINTERS request, which requires the | |
578 | * following attributes: | |
579 | * | |
580 | * attributes-charset | |
581 | * attributes-natural-language | |
ef416fc2 | 582 | */ |
583 | ||
fa73b229 | 584 | request = ippNewRequest(CUPS_GET_PRINTERS); |
ef416fc2 | 585 | |
acb056cb MS |
586 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, "printer-type", |
587 | CUPS_PRINTER_LOCAL); | |
588 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, "printer-type-mask", | |
589 | CUPS_PRINTER_CLASS | CUPS_PRINTER_REMOTE | | |
590 | CUPS_PRINTER_IMPLICIT); | |
591 | ||
ef416fc2 | 592 | /* |
593 | * Do the request and get back a response... | |
594 | */ | |
595 | ||
ef55b745 MS |
596 | cgiClearVariables(); |
597 | if (op) | |
598 | cgiSetVariable("OP", op); | |
599 | if (name) | |
600 | cgiSetVariable("PRINTER_NAME", name); | |
601 | ||
ef416fc2 | 602 | if ((response = cupsDoRequest(http, request, "/")) != NULL) |
603 | { | |
604 | /* | |
605 | * Create MEMBER_URIS and MEMBER_NAMES arrays... | |
606 | */ | |
607 | ||
608 | for (element = 0, attr = response->attrs; | |
609 | attr != NULL; | |
610 | attr = attr->next) | |
611 | if (attr->name && !strcmp(attr->name, "printer-uri-supported")) | |
612 | { | |
613 | if ((ptr = strrchr(attr->values[0].string.text, '/')) != NULL && | |
614 | (!name || strcasecmp(name, ptr + 1))) | |
615 | { | |
616 | /* | |
617 | * Don't show the current class... | |
618 | */ | |
619 | ||
620 | cgiSetArray("MEMBER_URIS", element, attr->values[0].string.text); | |
621 | element ++; | |
622 | } | |
623 | } | |
624 | ||
625 | for (element = 0, attr = response->attrs; | |
626 | attr != NULL; | |
627 | attr = attr->next) | |
628 | if (attr->name && !strcmp(attr->name, "printer-name")) | |
629 | { | |
630 | if (!name || strcasecmp(name, attr->values[0].string.text)) | |
631 | { | |
632 | /* | |
633 | * Don't show the current class... | |
634 | */ | |
635 | ||
636 | cgiSetArray("MEMBER_NAMES", element, attr->values[0].string.text); | |
637 | element ++; | |
638 | } | |
639 | } | |
640 | ||
641 | num_printers = cgiGetSize("MEMBER_URIS"); | |
642 | ||
643 | ippDelete(response); | |
644 | } | |
645 | else | |
646 | num_printers = 0; | |
647 | ||
648 | if (modify) | |
649 | { | |
650 | /* | |
651 | * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the | |
652 | * following attributes: | |
653 | * | |
654 | * attributes-charset | |
655 | * attributes-natural-language | |
656 | * printer-uri | |
657 | */ | |
658 | ||
fa73b229 | 659 | request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES); |
ef416fc2 | 660 | |
a4d04587 | 661 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, |
662 | "localhost", 0, "/classes/%s", name); | |
ef416fc2 | 663 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", |
664 | NULL, uri); | |
665 | ||
666 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
667 | "requested-attributes", | |
668 | (int)(sizeof(pattrs) / sizeof(pattrs[0])), | |
669 | NULL, pattrs); | |
670 | ||
671 | /* | |
672 | * Do the request and get back a response... | |
673 | */ | |
674 | ||
675 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
676 | { | |
480ef0fe | 677 | if ((attr = ippFindAttribute(response, "member-names", |
678 | IPP_TAG_NAME)) != NULL) | |
ef416fc2 | 679 | { |
680 | /* | |
681 | * Mark any current members in the class... | |
682 | */ | |
683 | ||
684 | for (j = 0; j < num_printers; j ++) | |
685 | cgiSetArray("MEMBER_SELECTED", j, ""); | |
686 | ||
687 | for (i = 0; i < attr->num_values; i ++) | |
688 | { | |
689 | for (j = 0; j < num_printers; j ++) | |
690 | { | |
691 | if (!strcasecmp(attr->values[i].string.text, | |
692 | cgiGetArray("MEMBER_NAMES", j))) | |
693 | { | |
694 | cgiSetArray("MEMBER_SELECTED", j, "SELECTED"); | |
695 | break; | |
696 | } | |
697 | } | |
698 | } | |
699 | } | |
700 | ||
701 | if ((attr = ippFindAttribute(response, "printer-info", | |
702 | IPP_TAG_TEXT)) != NULL) | |
703 | cgiSetVariable("PRINTER_INFO", attr->values[0].string.text); | |
704 | ||
705 | if ((attr = ippFindAttribute(response, "printer-location", | |
706 | IPP_TAG_TEXT)) != NULL) | |
707 | cgiSetVariable("PRINTER_LOCATION", attr->values[0].string.text); | |
708 | ||
709 | ippDelete(response); | |
710 | } | |
711 | ||
712 | /* | |
713 | * Update the location and description of an existing printer... | |
714 | */ | |
715 | ||
716 | cgiStartHTML(title); | |
717 | cgiCopyTemplateLang("modify-class.tmpl"); | |
718 | } | |
719 | else | |
720 | { | |
721 | /* | |
722 | * Get the name, location, and description for a new printer... | |
723 | */ | |
724 | ||
725 | cgiStartHTML(title); | |
726 | cgiCopyTemplateLang("add-class.tmpl"); | |
727 | } | |
728 | ||
729 | cgiEndHTML(); | |
730 | ||
731 | return; | |
732 | } | |
733 | ||
7a0cbd5e MS |
734 | if (!name) |
735 | { | |
736 | cgiStartHTML(title); | |
4d301e69 | 737 | cgiSetVariable("ERROR", cgiText(_("Missing form variable"))); |
7a0cbd5e MS |
738 | cgiCopyTemplateLang("error.tmpl"); |
739 | cgiEndHTML(); | |
740 | return; | |
741 | } | |
742 | ||
ef416fc2 | 743 | for (ptr = name; *ptr; ptr ++) |
744 | if ((*ptr >= 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' || *ptr == '#') | |
745 | break; | |
746 | ||
747 | if (*ptr || ptr == name || strlen(name) > 127) | |
748 | { | |
fa73b229 | 749 | cgiSetVariable("ERROR", |
750 | cgiText(_("The class name may only contain up to " | |
751 | "127 printable characters and may not " | |
752 | "contain spaces, slashes (/), or the " | |
753 | "pound sign (#)."))); | |
ef416fc2 | 754 | cgiStartHTML(title); |
755 | cgiCopyTemplateLang("error.tmpl"); | |
756 | cgiEndHTML(); | |
757 | return; | |
758 | } | |
759 | ||
760 | /* | |
761 | * Build a CUPS_ADD_CLASS request, which requires the following | |
762 | * attributes: | |
763 | * | |
764 | * attributes-charset | |
765 | * attributes-natural-language | |
766 | * printer-uri | |
767 | * printer-location | |
768 | * printer-info | |
769 | * printer-is-accepting-jobs | |
770 | * printer-state | |
771 | * member-uris | |
772 | */ | |
773 | ||
fa73b229 | 774 | request = ippNewRequest(CUPS_ADD_CLASS); |
ef416fc2 | 775 | |
a4d04587 | 776 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, |
7a0cbd5e | 777 | "localhost", 0, "/classes/%s", name); |
ef416fc2 | 778 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", |
779 | NULL, uri); | |
780 | ||
781 | ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-location", | |
782 | NULL, cgiGetVariable("PRINTER_LOCATION")); | |
783 | ||
784 | ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-info", | |
785 | NULL, cgiGetVariable("PRINTER_INFO")); | |
786 | ||
787 | ippAddBoolean(request, IPP_TAG_PRINTER, "printer-is-accepting-jobs", 1); | |
788 | ||
789 | ippAddInteger(request, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state", | |
790 | IPP_PRINTER_IDLE); | |
791 | ||
792 | if ((num_printers = cgiGetSize("MEMBER_URIS")) > 0) | |
793 | { | |
794 | attr = ippAddStrings(request, IPP_TAG_PRINTER, IPP_TAG_URI, "member-uris", | |
795 | num_printers, NULL, NULL); | |
796 | for (i = 0; i < num_printers; i ++) | |
cc754834 | 797 | attr->values[i].string.text = _cupsStrAlloc(cgiGetArray("MEMBER_URIS", i)); |
ef416fc2 | 798 | } |
799 | ||
800 | /* | |
801 | * Do the request and get back a response... | |
802 | */ | |
803 | ||
fa73b229 | 804 | ippDelete(cupsDoRequest(http, request, "/admin/")); |
ef416fc2 | 805 | |
355e94dc MS |
806 | if (cupsLastError() == IPP_NOT_AUTHORIZED) |
807 | { | |
808 | puts("Status: 401\n"); | |
809 | exit(0); | |
810 | } | |
811 | else if (cupsLastError() > IPP_OK_CONFLICT) | |
ef416fc2 | 812 | { |
813 | cgiStartHTML(title); | |
fa73b229 | 814 | cgiShowIPPError(modify ? _("Unable to modify class:") : |
815 | _("Unable to add class:")); | |
ef416fc2 | 816 | } |
817 | else | |
818 | { | |
819 | /* | |
820 | * Redirect successful updates back to the class page... | |
821 | */ | |
822 | ||
823 | char refresh[1024]; /* Refresh URL */ | |
824 | ||
825 | cgiFormEncode(uri, name, sizeof(uri)); | |
f301802f | 826 | snprintf(refresh, sizeof(refresh), "5;URL=/admin/?OP=redirect&URL=/classes/%s", |
ef416fc2 | 827 | uri); |
828 | cgiSetVariable("refresh_page", refresh); | |
829 | ||
830 | cgiStartHTML(title); | |
831 | ||
832 | if (modify) | |
833 | cgiCopyTemplateLang("class-modified.tmpl"); | |
834 | else | |
835 | cgiCopyTemplateLang("class-added.tmpl"); | |
836 | } | |
837 | ||
838 | cgiEndHTML(); | |
839 | } | |
840 | ||
841 | ||
842 | /* | |
843 | * 'do_am_printer()' - Add or modify a printer. | |
844 | */ | |
845 | ||
846 | static void | |
fa73b229 | 847 | do_am_printer(http_t *http, /* I - HTTP connection */ |
848 | int modify) /* I - Modify the printer? */ | |
ef416fc2 | 849 | { |
850 | int i; /* Looping var */ | |
89d46774 | 851 | ipp_attribute_t *attr; /* Current attribute */ |
ef416fc2 | 852 | ipp_t *request, /* IPP request */ |
853 | *response, /* IPP response */ | |
854 | *oldinfo; /* Old printer information */ | |
ef416fc2 | 855 | const cgi_file_t *file; /* Uploaded file, if any */ |
856 | const char *var; /* CGI variable */ | |
857 | char uri[HTTP_MAX_URI], /* Device or printer URI */ | |
858 | *uriptr; /* Pointer into URI */ | |
859 | int maxrate; /* Maximum baud rate */ | |
860 | char baudrate[255]; /* Baud rate string */ | |
861 | const char *name, /* Pointer to class name */ | |
862 | *ptr; /* Pointer to CGI variable */ | |
863 | const char *title; /* Title of page */ | |
864 | static int baudrates[] = /* Baud rates */ | |
865 | { | |
866 | 1200, | |
867 | 2400, | |
868 | 4800, | |
869 | 9600, | |
870 | 19200, | |
871 | 38400, | |
872 | 57600, | |
873 | 115200, | |
874 | 230400, | |
875 | 460800 | |
876 | }; | |
877 | ||
878 | ||
f301802f | 879 | ptr = cgiGetVariable("DEVICE_URI"); |
fa73b229 | 880 | fprintf(stderr, "DEBUG: do_am_printer: DEVICE_URI=\"%s\"\n", |
f301802f | 881 | ptr ? ptr : "(null)"); |
fa73b229 | 882 | |
883 | title = cgiText(modify ? _("Modify Printer") : _("Add Printer")); | |
ef416fc2 | 884 | |
885 | if (modify) | |
886 | { | |
887 | /* | |
888 | * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the | |
889 | * following attributes: | |
890 | * | |
891 | * attributes-charset | |
892 | * attributes-natural-language | |
893 | * printer-uri | |
894 | */ | |
895 | ||
fa73b229 | 896 | request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES); |
ef416fc2 | 897 | |
a4d04587 | 898 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, |
899 | "localhost", 0, "/printers/%s", | |
900 | cgiGetVariable("PRINTER_NAME")); | |
ef416fc2 | 901 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", |
902 | NULL, uri); | |
903 | ||
904 | /* | |
905 | * Do the request and get back a response... | |
906 | */ | |
907 | ||
908 | oldinfo = cupsDoRequest(http, request, "/"); | |
909 | } | |
910 | else | |
911 | oldinfo = NULL; | |
912 | ||
2e4ff8af | 913 | file = cgiGetFile(); |
ef416fc2 | 914 | |
2e4ff8af MS |
915 | if (file) |
916 | { | |
917 | fprintf(stderr, "DEBUG: file->tempfile=%s\n", file->tempfile); | |
918 | fprintf(stderr, "DEBUG: file->name=%s\n", file->name); | |
919 | fprintf(stderr, "DEBUG: file->filename=%s\n", file->filename); | |
920 | fprintf(stderr, "DEBUG: file->mimetype=%s\n", file->mimetype); | |
921 | } | |
ef416fc2 | 922 | |
2e4ff8af MS |
923 | if ((name = cgiGetVariable("PRINTER_NAME")) != NULL) |
924 | { | |
925 | for (ptr = name; *ptr; ptr ++) | |
926 | if ((*ptr >= 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' || *ptr == '#') | |
927 | break; | |
ef416fc2 | 928 | |
2e4ff8af MS |
929 | if (*ptr || ptr == name || strlen(name) > 127) |
930 | { | |
931 | cgiSetVariable("ERROR", | |
932 | cgiText(_("The printer name may only contain up to " | |
933 | "127 printable characters and may not " | |
934 | "contain spaces, slashes (/), or the " | |
935 | "pound sign (#)."))); | |
936 | cgiStartHTML(title); | |
937 | cgiCopyTemplateLang("error.tmpl"); | |
938 | cgiEndHTML(); | |
939 | return; | |
ef416fc2 | 940 | } |
2e4ff8af MS |
941 | } |
942 | ||
943 | if ((var = cgiGetVariable("DEVICE_URI")) != NULL) | |
944 | { | |
945 | if ((uriptr = strrchr(var, '|')) != NULL) | |
ef416fc2 | 946 | { |
947 | /* | |
2e4ff8af | 948 | * Extract make and make/model from device URI string... |
ef416fc2 | 949 | */ |
950 | ||
2e4ff8af MS |
951 | char make[1024], /* Make string */ |
952 | *makeptr; /* Pointer into make */ | |
ef416fc2 | 953 | |
ef416fc2 | 954 | |
2e4ff8af | 955 | *uriptr++ = '\0'; |
ef416fc2 | 956 | |
2e4ff8af | 957 | strlcpy(make, uriptr, sizeof(make)); |
ef416fc2 | 958 | |
2e4ff8af MS |
959 | if ((makeptr = strchr(make, ' ')) != NULL) |
960 | *makeptr = '\0'; | |
961 | else if ((makeptr = strchr(make, '-')) != NULL) | |
962 | *makeptr = '\0'; | |
963 | else if (!strncasecmp(make, "laserjet", 8) || | |
964 | !strncasecmp(make, "deskjet", 7) || | |
965 | !strncasecmp(make, "designjet", 9)) | |
966 | strcpy(make, "HP"); | |
967 | else if (!strncasecmp(make, "phaser", 6)) | |
968 | strcpy(make, "Xerox"); | |
969 | else if (!strncasecmp(make, "stylus", 6)) | |
970 | strcpy(make, "Epson"); | |
971 | else | |
972 | strcpy(make, "Generic"); | |
ef416fc2 | 973 | |
2e4ff8af MS |
974 | if (!cgiGetVariable("CURRENT_MAKE")) |
975 | cgiSetVariable("CURRENT_MAKE", make); | |
ef416fc2 | 976 | |
2e4ff8af MS |
977 | if (!cgiGetVariable("CURRENT_MAKE_AND_MODEL")) |
978 | cgiSetVariable("CURRENT_MAKE_AND_MODEL", uriptr); | |
979 | ||
980 | if (!modify) | |
981 | { | |
982 | char template[128], /* Template name */ | |
983 | *tptr; /* Pointer into template name */ | |
984 | ||
985 | cgiSetVariable("PRINTER_INFO", uriptr); | |
986 | ||
987 | for (tptr = template; | |
988 | tptr < (template + sizeof(template) - 1) && *uriptr; | |
989 | uriptr ++) | |
990 | if (isalnum(*uriptr & 255) || *uriptr == '_' || *uriptr == '-' || | |
991 | *uriptr == '.') | |
992 | *tptr++ = *uriptr; | |
41681883 MS |
993 | else if ((*uriptr == ' ' || *uriptr == '/') && tptr > template && |
994 | tptr[-1] != '_') | |
2e4ff8af MS |
995 | *tptr++ = '_'; |
996 | else if (*uriptr == '?' || *uriptr == '(') | |
997 | break; | |
998 | ||
999 | *tptr = '\0'; | |
1000 | ||
1001 | cgiSetVariable("TEMPLATE_NAME", template); | |
1002 | } | |
1003 | } | |
ef416fc2 | 1004 | } |
1005 | ||
2e4ff8af | 1006 | if (!var) |
ef416fc2 | 1007 | { |
1008 | /* | |
58dc1933 | 1009 | * Look for devices so the user can pick something... |
ef416fc2 | 1010 | */ |
1011 | ||
58dc1933 MS |
1012 | if ((attr = ippFindAttribute(oldinfo, "device-uri", IPP_TAG_URI)) != NULL) |
1013 | { | |
1014 | strlcpy(uri, attr->values[0].string.text, sizeof(uri)); | |
1015 | if ((uriptr = strchr(uri, ':')) != NULL && strncmp(uriptr, "://", 3) == 0) | |
1016 | *uriptr = '\0'; | |
a4d04587 | 1017 | |
58dc1933 MS |
1018 | cgiSetVariable("CURRENT_DEVICE_URI", attr->values[0].string.text); |
1019 | cgiSetVariable("CURRENT_DEVICE_SCHEME", uri); | |
1020 | } | |
ef416fc2 | 1021 | |
ef55b745 MS |
1022 | /* |
1023 | * Scan for devices for up to 30 seconds... | |
ef416fc2 | 1024 | */ |
1025 | ||
58dc1933 | 1026 | fputs("DEBUG: Getting list of devices...\n", stderr); |
a4d04587 | 1027 | |
ef55b745 | 1028 | current_device = 0; |
f14324a7 | 1029 | if (cupsGetDevices(http, 5, CUPS_INCLUDE_ALL, CUPS_EXCLUDE_NONE, |
ed6e7faf | 1030 | (cups_device_cb_t)choose_device_cb, |
f8b3a85b | 1031 | (void *)title) == IPP_OK) |
d2354e63 | 1032 | { |
58dc1933 | 1033 | fputs("DEBUG: Got device list!\n", stderr); |
d2354e63 | 1034 | |
ef55b745 MS |
1035 | if (cgiSupportsMultipart()) |
1036 | cgiStartMultipart(); | |
d2354e63 | 1037 | |
ef55b745 MS |
1038 | cgiSetVariable("CUPS_GET_DEVICES_DONE", "1"); |
1039 | cgiStartHTML(title); | |
1040 | cgiCopyTemplateLang("choose-device.tmpl"); | |
1041 | cgiEndHTML(); | |
1042 | ||
1043 | if (cgiSupportsMultipart()) | |
1044 | cgiEndMultipart(); | |
d2354e63 | 1045 | } |
a4d04587 | 1046 | else |
e4572d57 | 1047 | { |
a4d04587 | 1048 | fprintf(stderr, |
1049 | "ERROR: CUPS-Get-Devices request failed with status %x: %s\n", | |
1050 | cupsLastError(), cupsLastErrorString()); | |
e4572d57 MS |
1051 | if (cupsLastError() == IPP_NOT_AUTHORIZED) |
1052 | { | |
1053 | puts("Status: 401\n"); | |
1054 | exit(0); | |
1055 | } | |
1056 | else | |
1057 | { | |
1058 | cgiStartHTML(title); | |
1059 | cgiShowIPPError(modify ? _("Unable to modify printer:") : | |
1060 | _("Unable to add printer:")); | |
1061 | cgiEndHTML(); | |
1062 | return; | |
1063 | } | |
1064 | } | |
ef416fc2 | 1065 | } |
0268488e MS |
1066 | else if (!strchr(var, '/') || |
1067 | (!strncmp(var, "lpd://", 6) && !strchr(var + 6, '/'))) | |
ef416fc2 | 1068 | { |
1069 | if ((attr = ippFindAttribute(oldinfo, "device-uri", IPP_TAG_URI)) != NULL) | |
1070 | { | |
1071 | /* | |
1072 | * Set the current device URI for the form to the old one... | |
1073 | */ | |
1074 | ||
1075 | if (strncmp(attr->values[0].string.text, var, strlen(var)) == 0) | |
1f6f3dbc | 1076 | cgiSetVariable("CURRENT_DEVICE_URI", attr->values[0].string.text); |
ef416fc2 | 1077 | } |
1078 | ||
1079 | /* | |
1080 | * User needs to set the full URI... | |
1081 | */ | |
1082 | ||
1083 | cgiStartHTML(title); | |
1084 | cgiCopyTemplateLang("choose-uri.tmpl"); | |
1085 | cgiEndHTML(); | |
1086 | } | |
1087 | else if (!strncmp(var, "serial:", 7) && !cgiGetVariable("BAUDRATE")) | |
1088 | { | |
1089 | /* | |
1090 | * Need baud rate, parity, etc. | |
1091 | */ | |
1092 | ||
1093 | if ((var = strchr(var, '?')) != NULL && | |
1094 | strncmp(var, "?baud=", 6) == 0) | |
1095 | maxrate = atoi(var + 6); | |
1096 | else | |
1097 | maxrate = 19200; | |
1098 | ||
1099 | for (i = 0; i < 10; i ++) | |
1100 | if (baudrates[i] > maxrate) | |
1101 | break; | |
1102 | else | |
1103 | { | |
1104 | sprintf(baudrate, "%d", baudrates[i]); | |
1105 | cgiSetArray("BAUDRATES", i, baudrate); | |
1106 | } | |
1107 | ||
1108 | cgiStartHTML(title); | |
1109 | cgiCopyTemplateLang("choose-serial.tmpl"); | |
1110 | cgiEndHTML(); | |
1111 | } | |
2e4ff8af MS |
1112 | else if (!name || !cgiGetVariable("PRINTER_LOCATION")) |
1113 | { | |
1114 | cgiStartHTML(title); | |
1115 | ||
1116 | if (modify) | |
1117 | { | |
1118 | /* | |
1119 | * Update the location and description of an existing printer... | |
1120 | */ | |
1121 | ||
1122 | if (oldinfo) | |
1f6f3dbc MS |
1123 | { |
1124 | if ((attr = ippFindAttribute(oldinfo, "printer-info", | |
1125 | IPP_TAG_TEXT)) != NULL) | |
1126 | cgiSetVariable("PRINTER_INFO", attr->values[0].string.text); | |
1127 | ||
1128 | if ((attr = ippFindAttribute(oldinfo, "printer-location", | |
1129 | IPP_TAG_TEXT)) != NULL) | |
1130 | cgiSetVariable("PRINTER_LOCATION", attr->values[0].string.text); | |
ef55b745 MS |
1131 | |
1132 | if ((attr = ippFindAttribute(oldinfo, "printer-is-shared", | |
1133 | IPP_TAG_BOOLEAN)) != NULL) | |
1134 | cgiSetVariable("PRINTER_IS_SHARED", | |
1135 | attr->values[0].boolean ? "1" : "0"); | |
1f6f3dbc | 1136 | } |
2e4ff8af MS |
1137 | |
1138 | cgiCopyTemplateLang("modify-printer.tmpl"); | |
1139 | } | |
1140 | else | |
1141 | { | |
1142 | /* | |
1143 | * Get the name, location, and description for a new printer... | |
1144 | */ | |
1145 | ||
c5571a1d MS |
1146 | #ifdef __APPLE__ |
1147 | if (!strncmp(var, "usb:", 4)) | |
1148 | cgiSetVariable("printer_is_shared", "1"); | |
1149 | else | |
1150 | #endif /* __APPLE__ */ | |
1151 | cgiSetVariable("printer_is_shared", "0"); | |
1152 | ||
2e4ff8af MS |
1153 | cgiCopyTemplateLang("add-printer.tmpl"); |
1154 | } | |
1155 | ||
1156 | cgiEndHTML(); | |
1157 | ||
1158 | if (oldinfo) | |
1159 | ippDelete(oldinfo); | |
1160 | ||
1161 | return; | |
1162 | } | |
bf3816c7 MS |
1163 | else if (!file && |
1164 | (!cgiGetVariable("PPD_NAME") || cgiGetVariable("SELECT_MAKE"))) | |
ef416fc2 | 1165 | { |
bf3816c7 | 1166 | if (modify && !cgiGetVariable("SELECT_MAKE")) |
ef416fc2 | 1167 | { |
1168 | /* | |
1169 | * Get the PPD file... | |
1170 | */ | |
1171 | ||
1172 | int fd; /* PPD file */ | |
1173 | char filename[1024]; /* PPD filename */ | |
1174 | ppd_file_t *ppd; /* PPD information */ | |
1175 | char buffer[1024]; /* Buffer */ | |
1176 | int bytes; /* Number of bytes */ | |
1177 | http_status_t get_status; /* Status of GET */ | |
1178 | ||
1179 | ||
a4d04587 | 1180 | /* TODO: Use cupsGetFile() API... */ |
ef416fc2 | 1181 | snprintf(uri, sizeof(uri), "/printers/%s.ppd", name); |
1182 | ||
1183 | if (httpGet(http, uri)) | |
1184 | httpGet(http, uri); | |
1185 | ||
1186 | while ((get_status = httpUpdate(http)) == HTTP_CONTINUE); | |
1187 | ||
1188 | if (get_status != HTTP_OK) | |
1189 | { | |
178cb736 MS |
1190 | httpFlush(http); |
1191 | ||
ef416fc2 | 1192 | fprintf(stderr, "ERROR: Unable to get PPD file %s: %d - %s\n", |
1193 | uri, get_status, httpStatus(get_status)); | |
1194 | } | |
1195 | else if ((fd = cupsTempFd(filename, sizeof(filename))) >= 0) | |
1196 | { | |
a4d04587 | 1197 | while ((bytes = httpRead2(http, buffer, sizeof(buffer))) > 0) |
ef416fc2 | 1198 | write(fd, buffer, bytes); |
1199 | ||
1200 | close(fd); | |
1201 | ||
1202 | if ((ppd = ppdOpenFile(filename)) != NULL) | |
1203 | { | |
1204 | if (ppd->manufacturer) | |
1205 | cgiSetVariable("CURRENT_MAKE", ppd->manufacturer); | |
1206 | ||
1207 | if (ppd->nickname) | |
1208 | cgiSetVariable("CURRENT_MAKE_AND_MODEL", ppd->nickname); | |
1209 | ||
1210 | ppdClose(ppd); | |
1211 | unlink(filename); | |
1212 | } | |
1213 | else | |
1214 | { | |
1215 | fprintf(stderr, "ERROR: Unable to open PPD file %s: %s\n", | |
1216 | filename, ppdErrorString(ppdLastError(&bytes))); | |
1217 | } | |
1218 | } | |
1219 | else | |
1220 | { | |
1221 | httpFlush(http); | |
1222 | ||
1223 | fprintf(stderr, | |
1224 | "ERROR: Unable to create temporary file for PPD file: %s\n", | |
1225 | strerror(errno)); | |
1226 | } | |
1227 | } | |
ef416fc2 | 1228 | |
1229 | /* | |
1230 | * Build a CUPS_GET_PPDS request, which requires the following | |
1231 | * attributes: | |
1232 | * | |
1233 | * attributes-charset | |
1234 | * attributes-natural-language | |
1235 | * printer-uri | |
1236 | */ | |
1237 | ||
fa73b229 | 1238 | request = ippNewRequest(CUPS_GET_PPDS); |
ef416fc2 | 1239 | |
1240 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
1241 | NULL, "ipp://localhost/printers/"); | |
1242 | ||
c7017ecc MS |
1243 | if ((var = cgiGetVariable("PPD_MAKE")) == NULL) |
1244 | var = cgiGetVariable("CURRENT_MAKE"); | |
bf3816c7 | 1245 | if (var && !cgiGetVariable("SELECT_MAKE")) |
58dc1933 | 1246 | { |
e4572d57 MS |
1247 | const char *make_model; /* Make and model */ |
1248 | ||
1249 | ||
ef416fc2 | 1250 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT, |
1251 | "ppd-make", NULL, var); | |
58dc1933 | 1252 | |
bf3816c7 | 1253 | if ((make_model = cgiGetVariable("CURRENT_MAKE_AND_MODEL")) != NULL) |
58dc1933 | 1254 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT, |
e4572d57 | 1255 | "ppd-make-and-model", NULL, make_model); |
58dc1933 | 1256 | } |
ef416fc2 | 1257 | else |
1258 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
1259 | "requested-attributes", NULL, "ppd-make"); | |
1260 | ||
1261 | /* | |
1262 | * Do the request and get back a response... | |
1263 | */ | |
1264 | ||
1265 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
1266 | { | |
1267 | /* | |
1268 | * Got the list of PPDs, see if the user has selected a make... | |
1269 | */ | |
1270 | ||
ef55b745 | 1271 | if (cgiSetIPPVars(response, NULL, NULL, NULL, 0) == 0 && !modify) |
ef416fc2 | 1272 | { |
1273 | /* | |
89d46774 | 1274 | * No PPD files with this make, try again with all makes... |
ef416fc2 | 1275 | */ |
1276 | ||
89d46774 | 1277 | ippDelete(response); |
1278 | ||
1279 | request = ippNewRequest(CUPS_GET_PPDS); | |
1280 | ||
1281 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
1282 | NULL, "ipp://localhost/printers/"); | |
1283 | ||
1284 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
1285 | "requested-attributes", NULL, "ppd-make"); | |
ef416fc2 | 1286 | |
89d46774 | 1287 | if ((response = cupsDoRequest(http, request, "/")) != NULL) |
1288 | cgiSetIPPVars(response, NULL, NULL, NULL, 0); | |
1289 | ||
1290 | cgiStartHTML(title); | |
1291 | cgiCopyTemplateLang("choose-make.tmpl"); | |
1292 | cgiEndHTML(); | |
1293 | } | |
2e4ff8af | 1294 | else if (!var || cgiGetVariable("SELECT_MAKE")) |
89d46774 | 1295 | { |
ef416fc2 | 1296 | cgiStartHTML(title); |
1297 | cgiCopyTemplateLang("choose-make.tmpl"); | |
1298 | cgiEndHTML(); | |
1299 | } | |
1300 | else | |
1301 | { | |
1302 | /* | |
1303 | * Let the user choose a model... | |
1304 | */ | |
1305 | ||
ef416fc2 | 1306 | cgiStartHTML(title); |
ef55b745 MS |
1307 | if (!cgiGetVariable("PPD_MAKE")) |
1308 | cgiSetVariable("PPD_MAKE", cgiGetVariable("CURRENT_MAKE")); | |
1309 | if (!modify) | |
1310 | cgiSetVariable("CURRENT_MAKE_AND_MODEL", | |
1311 | cgiGetArray("PPD_MAKE_AND_MODEL", 0)); | |
ef416fc2 | 1312 | cgiCopyTemplateLang("choose-model.tmpl"); |
1313 | cgiEndHTML(); | |
1314 | } | |
1315 | ||
ef416fc2 | 1316 | ippDelete(response); |
1317 | } | |
1318 | else | |
1319 | { | |
ef416fc2 | 1320 | cgiStartHTML(title); |
fa73b229 | 1321 | cgiShowIPPError(_("Unable to get list of printer drivers:")); |
ef416fc2 | 1322 | cgiCopyTemplateLang("error.tmpl"); |
1323 | cgiEndHTML(); | |
1324 | } | |
1325 | } | |
1326 | else | |
1327 | { | |
1328 | /* | |
1329 | * Build a CUPS_ADD_PRINTER request, which requires the following | |
1330 | * attributes: | |
1331 | * | |
1332 | * attributes-charset | |
1333 | * attributes-natural-language | |
1334 | * printer-uri | |
1335 | * printer-location | |
1336 | * printer-info | |
1337 | * ppd-name | |
1338 | * device-uri | |
1339 | * printer-is-accepting-jobs | |
c5571a1d | 1340 | * printer-is-shared |
ef416fc2 | 1341 | * printer-state |
1342 | */ | |
1343 | ||
fa73b229 | 1344 | request = ippNewRequest(CUPS_ADD_PRINTER); |
ef416fc2 | 1345 | |
a4d04587 | 1346 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, |
1347 | "localhost", 0, "/printers/%s", | |
1348 | cgiGetVariable("PRINTER_NAME")); | |
ef416fc2 | 1349 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", |
1350 | NULL, uri); | |
1351 | ||
1352 | ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-location", | |
1353 | NULL, cgiGetVariable("PRINTER_LOCATION")); | |
1354 | ||
1355 | ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-info", | |
1356 | NULL, cgiGetVariable("PRINTER_INFO")); | |
1357 | ||
1358 | if (!file) | |
0af14961 MS |
1359 | { |
1360 | var = cgiGetVariable("PPD_NAME"); | |
1361 | if (strcmp(var, "__no_change__")) | |
1362 | ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME, "ppd-name", | |
1363 | NULL, var); | |
1364 | } | |
ef416fc2 | 1365 | |
1366 | strlcpy(uri, cgiGetVariable("DEVICE_URI"), sizeof(uri)); | |
1367 | ||
1368 | /* | |
1369 | * Strip make and model from URI... | |
1370 | */ | |
1371 | ||
1372 | if ((uriptr = strrchr(uri, '|')) != NULL) | |
1373 | *uriptr = '\0'; | |
1374 | ||
fa73b229 | 1375 | if (!strncmp(uri, "serial:", 7)) |
ef416fc2 | 1376 | { |
1377 | /* | |
1378 | * Update serial port URI to include baud rate, etc. | |
1379 | */ | |
1380 | ||
1381 | if ((uriptr = strchr(uri, '?')) == NULL) | |
1382 | uriptr = uri + strlen(uri); | |
1383 | ||
1384 | snprintf(uriptr, sizeof(uri) - (uriptr - uri), | |
1385 | "?baud=%s+bits=%s+parity=%s+flow=%s", | |
1386 | cgiGetVariable("BAUDRATE"), cgiGetVariable("BITS"), | |
1387 | cgiGetVariable("PARITY"), cgiGetVariable("FLOW")); | |
1388 | } | |
1389 | ||
1390 | ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", | |
1391 | NULL, uri); | |
1392 | ||
1393 | ippAddBoolean(request, IPP_TAG_PRINTER, "printer-is-accepting-jobs", 1); | |
1394 | ||
c5571a1d MS |
1395 | var = cgiGetVariable("printer_is_shared"); |
1396 | ippAddBoolean(request, IPP_TAG_PRINTER, "printer-is-shared", | |
1397 | var && (!strcmp(var, "1") || !strcmp(var, "on"))); | |
1398 | ||
ef416fc2 | 1399 | ippAddInteger(request, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state", |
1400 | IPP_PRINTER_IDLE); | |
1401 | ||
1402 | /* | |
1403 | * Do the request and get back a response... | |
1404 | */ | |
1405 | ||
1406 | if (file) | |
fa73b229 | 1407 | ippDelete(cupsDoFileRequest(http, request, "/admin/", file->tempfile)); |
ef416fc2 | 1408 | else |
fa73b229 | 1409 | ippDelete(cupsDoRequest(http, request, "/admin/")); |
ef416fc2 | 1410 | |
355e94dc MS |
1411 | if (cupsLastError() == IPP_NOT_AUTHORIZED) |
1412 | { | |
1413 | puts("Status: 401\n"); | |
1414 | exit(0); | |
1415 | } | |
1416 | else if (cupsLastError() > IPP_OK_CONFLICT) | |
ef416fc2 | 1417 | { |
1418 | cgiStartHTML(title); | |
fa73b229 | 1419 | cgiShowIPPError(modify ? _("Unable to modify printer:") : |
1420 | _("Unable to add printer:")); | |
ef416fc2 | 1421 | } |
2e4ff8af | 1422 | else if (modify) |
ef416fc2 | 1423 | { |
1424 | /* | |
2e4ff8af | 1425 | * Redirect successful updates back to the printer page... |
ef416fc2 | 1426 | */ |
1427 | ||
1428 | char refresh[1024]; /* Refresh URL */ | |
1429 | ||
1430 | ||
1431 | cgiFormEncode(uri, name, sizeof(uri)); | |
1432 | ||
2e4ff8af MS |
1433 | snprintf(refresh, sizeof(refresh), |
1434 | "5;/admin/?OP=redirect&URL=/printers/%s", uri); | |
ef416fc2 | 1435 | |
1436 | cgiSetVariable("refresh_page", refresh); | |
1437 | ||
1438 | cgiStartHTML(title); | |
1439 | ||
2e4ff8af MS |
1440 | cgiCopyTemplateLang("printer-modified.tmpl"); |
1441 | } | |
1442 | else | |
1443 | { | |
1444 | /* | |
1445 | * Set the printer options... | |
1446 | */ | |
1447 | ||
1448 | cgiSetVariable("OP", "set-printer-options"); | |
1449 | do_set_options(http, 0); | |
1450 | return; | |
ef416fc2 | 1451 | } |
1452 | ||
1453 | cgiEndHTML(); | |
1454 | } | |
1455 | ||
1456 | if (oldinfo) | |
1457 | ippDelete(oldinfo); | |
1458 | } | |
1459 | ||
1460 | ||
f7deaa1a | 1461 | /* |
1462 | * 'do_cancel_subscription()' - Cancel a subscription. | |
1463 | */ | |
1464 | ||
1465 | static void | |
1466 | do_cancel_subscription(http_t *http)/* I - HTTP connection */ | |
1467 | { | |
1468 | ipp_t *request; /* IPP request data */ | |
1469 | const char *var, /* Form variable */ | |
1470 | *user; /* Username */ | |
1471 | int id; /* Subscription ID */ | |
1472 | ||
1473 | ||
1474 | /* | |
1475 | * See if we have all of the required information... | |
1476 | */ | |
1477 | ||
1478 | if ((var = cgiGetVariable("NOTIFY_SUBSCRIPTION_ID")) != NULL) | |
1479 | id = atoi(var); | |
1480 | else | |
1481 | id = 0; | |
1482 | ||
1483 | if (id <= 0) | |
1484 | { | |
4d301e69 | 1485 | cgiSetVariable("ERROR", cgiText(_("Bad subscription ID"))); |
f7deaa1a | 1486 | cgiStartHTML(_("Cancel RSS Subscription")); |
1487 | cgiCopyTemplateLang("error.tmpl"); | |
1488 | cgiEndHTML(); | |
1489 | return; | |
1490 | } | |
1491 | ||
839a51c8 MS |
1492 | /* |
1493 | * Require a username... | |
1494 | */ | |
1495 | ||
1496 | if ((user = getenv("REMOTE_USER")) == NULL) | |
1497 | { | |
1498 | puts("Status: 401\n"); | |
1499 | exit(0); | |
1500 | } | |
1501 | ||
f7deaa1a | 1502 | /* |
1503 | * Cancel the subscription... | |
1504 | */ | |
1505 | ||
1506 | request = ippNewRequest(IPP_CANCEL_SUBSCRIPTION); | |
1507 | ||
1508 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
1509 | NULL, "ipp://localhost/"); | |
1510 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, | |
1511 | "notify-subscription-id", id); | |
1512 | ||
f7deaa1a | 1513 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", |
1514 | NULL, user); | |
1515 | ||
1516 | ippDelete(cupsDoRequest(http, request, "/")); | |
1517 | ||
355e94dc MS |
1518 | if (cupsLastError() == IPP_NOT_AUTHORIZED) |
1519 | { | |
1520 | puts("Status: 401\n"); | |
1521 | exit(0); | |
1522 | } | |
1523 | else if (cupsLastError() > IPP_OK_CONFLICT) | |
f7deaa1a | 1524 | { |
1525 | cgiStartHTML(_("Cancel RSS Subscription")); | |
1526 | cgiShowIPPError(_("Unable to cancel RSS subscription:")); | |
1527 | } | |
1528 | else | |
1529 | { | |
1530 | /* | |
1531 | * Redirect successful updates back to the admin page... | |
1532 | */ | |
1533 | ||
1534 | cgiSetVariable("refresh_page", "5;URL=/admin"); | |
1535 | cgiStartHTML(_("Cancel RSS Subscription")); | |
1536 | cgiCopyTemplateLang("subscription-canceled.tmpl"); | |
1537 | } | |
1538 | ||
1539 | cgiEndHTML(); | |
1540 | } | |
1541 | ||
1542 | ||
ef416fc2 | 1543 | /* |
323c5de1 | 1544 | * 'do_config_server()' - Configure server settings. |
ef416fc2 | 1545 | */ |
1546 | ||
1547 | static void | |
323c5de1 | 1548 | do_config_server(http_t *http) /* I - HTTP connection */ |
ef416fc2 | 1549 | { |
2e4ff8af | 1550 | if (cgiGetVariable("CHANGESETTINGS")) |
ef416fc2 | 1551 | { |
323c5de1 | 1552 | /* |
1553 | * Save basic setting changes... | |
1554 | */ | |
ef416fc2 | 1555 | |
323c5de1 | 1556 | int num_settings; /* Number of server settings */ |
1557 | cups_option_t *settings; /* Server settings */ | |
0af14961 MS |
1558 | int advanced, /* Advanced settings shown? */ |
1559 | changed; /* Have settings changed? */ | |
323c5de1 | 1560 | const char *debug_logging, /* DEBUG_LOGGING value */ |
1561 | *remote_admin, /* REMOTE_ADMIN value */ | |
1562 | *remote_any, /* REMOTE_ANY value */ | |
1563 | *remote_printers, | |
1564 | /* REMOTE_PRINTERS value */ | |
1565 | *share_printers,/* SHARE_PRINTERS value */ | |
0af14961 | 1566 | *user_cancel_any, |
ac884b6a | 1567 | /* USER_CANCEL_ANY value */ |
bf3816c7 MS |
1568 | *browse_web_if = NULL, |
1569 | /* BrowseWebIF value */ | |
1570 | *preserve_job_history = NULL, | |
0af14961 | 1571 | /* PreserveJobHistory value */ |
bf3816c7 | 1572 | *preserve_job_files = NULL, |
0af14961 | 1573 | /* PreserveJobFiles value */ |
bf3816c7 MS |
1574 | *max_clients = NULL, |
1575 | /* MaxClients value */ | |
1576 | *max_jobs = NULL, | |
1577 | /* MaxJobs value */ | |
1578 | *max_log_size = NULL; | |
1579 | /* MaxLogSize value */ | |
0af14961 MS |
1580 | char local_protocols[255], |
1581 | /* BrowseLocalProtocols */ | |
1582 | remote_protocols[255]; | |
1583 | /* BrowseRemoteProtocols */ | |
58dc1933 MS |
1584 | const char *current_browse_web_if, |
1585 | /* BrowseWebIF value */ | |
1586 | *current_preserve_job_history, | |
1587 | /* PreserveJobHistory value */ | |
1588 | *current_preserve_job_files, | |
1589 | /* PreserveJobFiles value */ | |
1590 | *current_max_clients, | |
1591 | /* MaxClients value */ | |
1592 | *current_max_jobs, | |
1593 | /* MaxJobs value */ | |
1594 | *current_max_log_size, | |
1595 | /* MaxLogSize value */ | |
1596 | *current_local_protocols, | |
1597 | /* BrowseLocalProtocols */ | |
1598 | *current_remote_protocols; | |
1599 | /* BrowseRemoteProtocols */ | |
323c5de1 | 1600 | #ifdef HAVE_GSSAPI |
ac884b6a | 1601 | char default_auth_type[255]; |
323c5de1 | 1602 | /* DefaultAuthType value */ |
ef55b745 | 1603 | const char *val; /* Setting value */ |
323c5de1 | 1604 | #endif /* HAVE_GSSAPI */ |
bd7854cb | 1605 | |
ef416fc2 | 1606 | |
323c5de1 | 1607 | /* |
1608 | * Get the checkbox values from the form... | |
1609 | */ | |
bd7854cb | 1610 | |
0af14961 MS |
1611 | debug_logging = cgiGetVariable("DEBUG_LOGGING") ? "1" : "0"; |
1612 | remote_admin = cgiGetVariable("REMOTE_ADMIN") ? "1" : "0"; | |
1613 | remote_any = cgiGetVariable("REMOTE_ANY") ? "1" : "0"; | |
1614 | remote_printers = cgiGetVariable("REMOTE_PRINTERS") ? "1" : "0"; | |
1615 | share_printers = cgiGetVariable("SHARE_PRINTERS") ? "1" : "0"; | |
1616 | user_cancel_any = cgiGetVariable("USER_CANCEL_ANY") ? "1" : "0"; | |
1617 | ||
1618 | advanced = cgiGetVariable("ADVANCEDSETTINGS") != NULL; | |
1619 | if (advanced) | |
1620 | { | |
1621 | /* | |
1622 | * Get advanced settings... | |
1623 | */ | |
1624 | ||
1625 | browse_web_if = cgiGetVariable("BROWSE_WEB_IF") ? "Yes" : "No"; | |
1626 | preserve_job_history = cgiGetVariable("PRESERVE_JOB_HISTORY") ? "Yes" : "No"; | |
1627 | preserve_job_files = cgiGetVariable("PRESERVE_JOB_FILES") ? "Yes" : "No"; | |
1f6f3dbc | 1628 | max_clients = cgiGetVariable("MAX_CLIENTS"); |
0af14961 MS |
1629 | max_jobs = cgiGetVariable("MAX_JOBS"); |
1630 | max_log_size = cgiGetVariable("MAX_LOG_SIZE"); | |
1631 | ||
1f6f3dbc MS |
1632 | if (!max_clients || atoi(max_clients) <= 0) |
1633 | max_clients = "100"; | |
1634 | ||
0af14961 MS |
1635 | if (!max_jobs || atoi(max_jobs) <= 0) |
1636 | max_jobs = "500"; | |
1637 | ||
1638 | if (!max_log_size || atof(max_log_size) <= 0.0) | |
1639 | max_log_size = "1m"; | |
1640 | ||
1641 | if (cgiGetVariable("BROWSE_LOCAL_CUPS")) | |
1642 | strcpy(local_protocols, "cups"); | |
1643 | else | |
1644 | local_protocols[0] = '\0'; | |
1645 | ||
1646 | #ifdef HAVE_DNSSD | |
1647 | if (cgiGetVariable("BROWSE_LOCAL_DNSSD")) | |
1648 | { | |
1649 | if (local_protocols[0]) | |
1650 | strcat(local_protocols, " dnssd"); | |
1651 | else | |
1652 | strcat(local_protocols, "dnssd"); | |
1653 | } | |
1654 | #endif /* HAVE_DNSSD */ | |
1655 | ||
1656 | #ifdef HAVE_LDAP | |
1657 | if (cgiGetVariable("BROWSE_LOCAL_LDAP")) | |
1658 | { | |
1659 | if (local_protocols[0]) | |
1660 | strcat(local_protocols, " ldap"); | |
1661 | else | |
1662 | strcat(local_protocols, "ldap"); | |
1663 | } | |
1664 | #endif /* HAVE_LDAP */ | |
1665 | ||
1666 | #ifdef HAVE_LIBSLP | |
1667 | if (cgiGetVariable("BROWSE_LOCAL_SLP")) | |
1668 | { | |
1669 | if (local_protocols[0]) | |
1670 | strcat(local_protocols, " slp"); | |
1671 | else | |
1672 | strcat(local_protocols, "slp"); | |
1673 | } | |
1674 | #endif /* HAVE_SLP */ | |
ef55b745 | 1675 | |
0af14961 MS |
1676 | if (cgiGetVariable("BROWSE_REMOTE_CUPS")) |
1677 | strcpy(remote_protocols, "cups"); | |
1678 | else | |
1679 | remote_protocols[0] = '\0'; | |
1680 | ||
1681 | #ifdef HAVE_LDAP | |
1682 | if (cgiGetVariable("BROWSE_REMOTE_LDAP")) | |
1683 | { | |
1684 | if (remote_protocols[0]) | |
1685 | strcat(remote_protocols, " ldap"); | |
1686 | else | |
1687 | strcat(remote_protocols, "ldap"); | |
1688 | } | |
1689 | #endif /* HAVE_LDAP */ | |
1690 | ||
1691 | #ifdef HAVE_LIBSLP | |
1692 | if (cgiGetVariable("BROWSE_REMOTE_SLP")) | |
1693 | { | |
1694 | if (remote_protocols[0]) | |
1695 | strcat(remote_protocols, " slp"); | |
1696 | else | |
1697 | strcat(remote_protocols, "slp"); | |
1698 | } | |
1699 | #endif /* HAVE_SLP */ | |
1700 | } | |
bd7854cb | 1701 | |
323c5de1 | 1702 | /* |
1703 | * Get the current server settings... | |
1704 | */ | |
ef416fc2 | 1705 | |
323c5de1 | 1706 | if (!cupsAdminGetServerSettings(http, &num_settings, &settings)) |
1707 | { | |
1708 | cgiStartHTML(cgiText(_("Change Settings"))); | |
1709 | cgiSetVariable("MESSAGE", | |
1710 | cgiText(_("Unable to change server settings:"))); | |
1711 | cgiSetVariable("ERROR", cupsLastErrorString()); | |
1712 | cgiCopyTemplateLang("error.tmpl"); | |
1713 | cgiEndHTML(); | |
1714 | return; | |
1715 | } | |
ef416fc2 | 1716 | |
0a682745 MS |
1717 | #ifdef HAVE_GSSAPI |
1718 | /* | |
1719 | * Get authentication settings... | |
1720 | */ | |
1721 | ||
1722 | if (cgiGetVariable("KERBEROS")) | |
ac884b6a | 1723 | strlcpy(default_auth_type, "Negotiate", sizeof(default_auth_type)); |
0a682745 MS |
1724 | else |
1725 | { | |
0af14961 | 1726 | val = cupsGetOption("DefaultAuthType", num_settings, settings); |
ac884b6a | 1727 | |
1f6f3dbc | 1728 | if (!val || !strcasecmp(val, "Negotiate")) |
ac884b6a MS |
1729 | strlcpy(default_auth_type, "Basic", sizeof(default_auth_type)); |
1730 | else | |
1731 | strlcpy(default_auth_type, val, sizeof(default_auth_type)); | |
0a682745 MS |
1732 | } |
1733 | ||
1734 | fprintf(stderr, "DEBUG: DefaultAuthType %s\n", default_auth_type); | |
1735 | #endif /* HAVE_GSSAPI */ | |
1736 | ||
58dc1933 MS |
1737 | if ((current_browse_web_if = cupsGetOption("BrowseWebIF", num_settings, |
1738 | settings)) == NULL) | |
1739 | current_browse_web_if = "No"; | |
1740 | ||
1741 | if ((current_preserve_job_history = cupsGetOption("PreserveJobHistory", | |
1742 | num_settings, | |
1743 | settings)) == NULL) | |
1744 | current_preserve_job_history = "Yes"; | |
1745 | ||
1746 | if ((current_preserve_job_files = cupsGetOption("PreserveJobFiles", | |
1747 | num_settings, | |
1748 | settings)) == NULL) | |
1749 | current_preserve_job_files = "No"; | |
1750 | ||
1751 | if ((current_max_clients = cupsGetOption("MaxClients", num_settings, | |
1752 | settings)) == NULL) | |
1753 | current_max_clients = "100"; | |
1754 | ||
1755 | if ((current_max_jobs = cupsGetOption("MaxJobs", num_settings, | |
1756 | settings)) == NULL) | |
1757 | current_max_jobs = "500"; | |
1758 | ||
1759 | if ((current_max_log_size = cupsGetOption("MaxLogSize", num_settings, | |
1760 | settings)) == NULL) | |
1761 | current_max_log_size = "1m"; | |
1762 | ||
1763 | if ((current_local_protocols = cupsGetOption("BrowseLocalProtocols", | |
1764 | num_settings, | |
1765 | settings)) == NULL) | |
1766 | current_local_protocols = CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS; | |
1767 | ||
1768 | if ((current_remote_protocols = cupsGetOption("BrowseRemoteProtocols", | |
1769 | num_settings, | |
1770 | settings)) == NULL) | |
1771 | current_remote_protocols = CUPS_DEFAULT_BROWSE_REMOTE_PROTOCOLS; | |
1772 | ||
323c5de1 | 1773 | /* |
1774 | * See if the settings have changed... | |
1775 | */ | |
1776 | ||
0af14961 MS |
1777 | changed = strcmp(debug_logging, cupsGetOption(CUPS_SERVER_DEBUG_LOGGING, |
1778 | num_settings, settings)) || | |
1779 | strcmp(remote_admin, cupsGetOption(CUPS_SERVER_REMOTE_ADMIN, | |
1780 | num_settings, settings)) || | |
1781 | strcmp(remote_any, cupsGetOption(CUPS_SERVER_REMOTE_ANY, | |
1782 | num_settings, settings)) || | |
1783 | strcmp(remote_printers, cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS, | |
1784 | num_settings, settings)) || | |
1785 | strcmp(share_printers, cupsGetOption(CUPS_SERVER_SHARE_PRINTERS, | |
1786 | num_settings, settings)) || | |
323c5de1 | 1787 | #ifdef HAVE_GSSAPI |
0af14961 MS |
1788 | !cupsGetOption("DefaultAuthType", num_settings, settings) || |
1789 | strcmp(default_auth_type, cupsGetOption("DefaultAuthType", | |
1790 | num_settings, settings)) || | |
323c5de1 | 1791 | #endif /* HAVE_GSSAPI */ |
0af14961 MS |
1792 | strcmp(user_cancel_any, cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY, |
1793 | num_settings, settings)); | |
1794 | ||
1795 | if (advanced && !changed) | |
58dc1933 MS |
1796 | changed = strcasecmp(local_protocols, current_local_protocols) || |
1797 | strcasecmp(remote_protocols, current_remote_protocols) || | |
1798 | strcasecmp(browse_web_if, current_browse_web_if) || | |
1799 | strcasecmp(preserve_job_history, current_preserve_job_history) || | |
1800 | strcasecmp(preserve_job_files, current_preserve_job_files) || | |
1801 | strcasecmp(max_clients, current_max_clients) || | |
1802 | strcasecmp(max_jobs, current_max_jobs) || | |
1803 | strcasecmp(max_log_size, current_max_log_size); | |
0af14961 MS |
1804 | |
1805 | if (changed) | |
323c5de1 | 1806 | { |
1807 | /* | |
1808 | * Settings *have* changed, so save the changes... | |
1809 | */ | |
ef416fc2 | 1810 | |
323c5de1 | 1811 | cupsFreeOptions(num_settings, settings); |
ef416fc2 | 1812 | |
323c5de1 | 1813 | num_settings = 0; |
1814 | num_settings = cupsAddOption(CUPS_SERVER_DEBUG_LOGGING, | |
1815 | debug_logging, num_settings, &settings); | |
1816 | num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ADMIN, | |
1817 | remote_admin, num_settings, &settings); | |
1818 | num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ANY, | |
1819 | remote_any, num_settings, &settings); | |
1820 | num_settings = cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS, | |
1821 | remote_printers, num_settings, &settings); | |
1822 | num_settings = cupsAddOption(CUPS_SERVER_SHARE_PRINTERS, | |
1823 | share_printers, num_settings, &settings); | |
1824 | num_settings = cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY, | |
1825 | user_cancel_any, num_settings, &settings); | |
1826 | #ifdef HAVE_GSSAPI | |
1827 | num_settings = cupsAddOption("DefaultAuthType", default_auth_type, | |
1828 | num_settings, &settings); | |
1829 | #endif /* HAVE_GSSAPI */ | |
ef416fc2 | 1830 | |
0af14961 MS |
1831 | if (advanced) |
1832 | { | |
1833 | /* | |
1834 | * Add advanced settings... | |
1835 | */ | |
1836 | ||
58dc1933 | 1837 | if (strcasecmp(local_protocols, current_local_protocols)) |
0af14961 MS |
1838 | num_settings = cupsAddOption("BrowseLocalProtocols", local_protocols, |
1839 | num_settings, &settings); | |
58dc1933 | 1840 | if (strcasecmp(remote_protocols, current_remote_protocols)) |
0af14961 MS |
1841 | num_settings = cupsAddOption("BrowseRemoteProtocols", remote_protocols, |
1842 | num_settings, &settings); | |
58dc1933 | 1843 | if (strcasecmp(browse_web_if, current_browse_web_if)) |
0af14961 MS |
1844 | num_settings = cupsAddOption("BrowseWebIF", browse_web_if, |
1845 | num_settings, &settings); | |
58dc1933 | 1846 | if (strcasecmp(preserve_job_history, current_preserve_job_history)) |
0af14961 MS |
1847 | num_settings = cupsAddOption("PreserveJobHistory", |
1848 | preserve_job_history, num_settings, | |
1849 | &settings); | |
58dc1933 | 1850 | if (strcasecmp(preserve_job_files, current_preserve_job_files)) |
0af14961 MS |
1851 | num_settings = cupsAddOption("PreserveJobFiles", preserve_job_files, |
1852 | num_settings, &settings); | |
58dc1933 | 1853 | if (strcasecmp(max_clients, current_max_clients)) |
1f6f3dbc MS |
1854 | num_settings = cupsAddOption("MaxClients", max_clients, num_settings, |
1855 | &settings); | |
58dc1933 | 1856 | if (strcasecmp(max_jobs, current_max_jobs)) |
0af14961 MS |
1857 | num_settings = cupsAddOption("MaxJobs", max_jobs, num_settings, |
1858 | &settings); | |
58dc1933 | 1859 | if (strcasecmp(max_log_size, current_max_log_size)) |
0af14961 MS |
1860 | num_settings = cupsAddOption("MaxLogSize", max_log_size, num_settings, |
1861 | &settings); | |
1862 | } | |
1863 | ||
323c5de1 | 1864 | if (!cupsAdminSetServerSettings(http, num_settings, settings)) |
ef416fc2 | 1865 | { |
355e94dc MS |
1866 | if (cupsLastError() == IPP_NOT_AUTHORIZED) |
1867 | { | |
1868 | puts("Status: 401\n"); | |
1869 | exit(0); | |
1870 | } | |
1871 | ||
323c5de1 | 1872 | cgiStartHTML(cgiText(_("Change Settings"))); |
1873 | cgiSetVariable("MESSAGE", | |
1874 | cgiText(_("Unable to change server settings:"))); | |
1875 | cgiSetVariable("ERROR", cupsLastErrorString()); | |
1876 | cgiCopyTemplateLang("error.tmpl"); | |
ef416fc2 | 1877 | } |
ef416fc2 | 1878 | else |
323c5de1 | 1879 | { |
58dc1933 MS |
1880 | if (advanced) |
1881 | cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect&URL=/admin/?ADVANCEDSETTINGS=YES"); | |
1882 | else | |
1883 | cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect"); | |
323c5de1 | 1884 | cgiStartHTML(cgiText(_("Change Settings"))); |
1885 | cgiCopyTemplateLang("restart.tmpl"); | |
1886 | } | |
1887 | } | |
1888 | else | |
1889 | { | |
1890 | /* | |
1891 | * No changes... | |
1892 | */ | |
ef416fc2 | 1893 | |
323c5de1 | 1894 | cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect"); |
1895 | cgiStartHTML(cgiText(_("Change Settings"))); | |
1896 | cgiCopyTemplateLang("norestart.tmpl"); | |
1897 | } | |
ef416fc2 | 1898 | |
323c5de1 | 1899 | cupsFreeOptions(num_settings, settings); |
ef416fc2 | 1900 | |
323c5de1 | 1901 | cgiEndHTML(); |
1902 | } | |
2e4ff8af | 1903 | else if (cgiGetVariable("SAVECHANGES") && cgiGetVariable("CUPSDCONF")) |
ef416fc2 | 1904 | { |
1905 | /* | |
323c5de1 | 1906 | * Save hand-edited config file... |
ef416fc2 | 1907 | */ |
1908 | ||
323c5de1 | 1909 | http_status_t status; /* PUT status */ |
1910 | char tempfile[1024]; /* Temporary new cupsd.conf */ | |
1911 | int tempfd; /* Temporary file descriptor */ | |
1912 | cups_file_t *temp; /* Temporary file */ | |
1913 | const char *start, /* Start of line */ | |
1914 | *end; /* End of line */ | |
bd7854cb | 1915 | |
fa73b229 | 1916 | |
323c5de1 | 1917 | /* |
1918 | * Create a temporary file for the new cupsd.conf file... | |
1919 | */ | |
ef416fc2 | 1920 | |
323c5de1 | 1921 | if ((tempfd = cupsTempFd(tempfile, sizeof(tempfile))) < 0) |
ef416fc2 | 1922 | { |
323c5de1 | 1923 | cgiStartHTML(cgiText(_("Edit Configuration File"))); |
1924 | cgiSetVariable("MESSAGE", cgiText(_("Unable to create temporary file:"))); | |
1925 | cgiSetVariable("ERROR", strerror(errno)); | |
1926 | cgiCopyTemplateLang("error.tmpl"); | |
1927 | cgiEndHTML(); | |
ef55b745 | 1928 | |
323c5de1 | 1929 | perror(tempfile); |
1930 | return; | |
ef416fc2 | 1931 | } |
1932 | ||
323c5de1 | 1933 | if ((temp = cupsFileOpenFd(tempfd, "w")) == NULL) |
ef416fc2 | 1934 | { |
323c5de1 | 1935 | cgiStartHTML(cgiText(_("Edit Configuration File"))); |
1936 | cgiSetVariable("MESSAGE", cgiText(_("Unable to create temporary file:"))); | |
1937 | cgiSetVariable("ERROR", strerror(errno)); | |
1938 | cgiCopyTemplateLang("error.tmpl"); | |
1939 | cgiEndHTML(); | |
ef55b745 | 1940 | |
323c5de1 | 1941 | perror(tempfile); |
1942 | close(tempfd); | |
1943 | unlink(tempfile); | |
1944 | return; | |
1945 | } | |
ef416fc2 | 1946 | |
323c5de1 | 1947 | /* |
1948 | * Copy the cupsd.conf text from the form variable... | |
1949 | */ | |
ef416fc2 | 1950 | |
323c5de1 | 1951 | start = cgiGetVariable("CUPSDCONF"); |
1952 | while (start) | |
1953 | { | |
1954 | if ((end = strstr(start, "\r\n")) == NULL) | |
1955 | if ((end = strstr(start, "\n")) == NULL) | |
1956 | end = start + strlen(start); | |
ef416fc2 | 1957 | |
323c5de1 | 1958 | cupsFileWrite(temp, start, end - start); |
1959 | cupsFilePutChar(temp, '\n'); | |
ef416fc2 | 1960 | |
323c5de1 | 1961 | if (*end == '\r') |
1962 | start = end + 2; | |
1963 | else if (*end == '\n') | |
1964 | start = end + 1; | |
1965 | else | |
1966 | start = NULL; | |
1967 | } | |
ef416fc2 | 1968 | |
323c5de1 | 1969 | cupsFileClose(temp); |
ef416fc2 | 1970 | |
323c5de1 | 1971 | /* |
1972 | * Upload the configuration file to the server... | |
1973 | */ | |
ef416fc2 | 1974 | |
323c5de1 | 1975 | status = cupsPutFile(http, "/admin/conf/cupsd.conf", tempfile); |
ef416fc2 | 1976 | |
355e94dc MS |
1977 | if (status == HTTP_UNAUTHORIZED) |
1978 | { | |
1979 | puts("Status: 401\n"); | |
1980 | unlink(tempfile); | |
1981 | exit(0); | |
1982 | } | |
1983 | else if (status != HTTP_CREATED) | |
323c5de1 | 1984 | { |
1985 | cgiSetVariable("MESSAGE", | |
1986 | cgiText(_("Unable to upload cupsd.conf file:"))); | |
1987 | cgiSetVariable("ERROR", httpStatus(status)); | |
1988 | ||
1989 | cgiStartHTML(cgiText(_("Edit Configuration File"))); | |
1990 | cgiCopyTemplateLang("error.tmpl"); | |
1991 | } | |
1992 | else | |
1993 | { | |
1994 | cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect"); | |
1995 | ||
1996 | cgiStartHTML(cgiText(_("Edit Configuration File"))); | |
1997 | cgiCopyTemplateLang("restart.tmpl"); | |
ef416fc2 | 1998 | } |
1999 | ||
323c5de1 | 2000 | cgiEndHTML(); |
2001 | ||
2002 | unlink(tempfile); | |
2003 | } | |
2004 | else | |
2005 | { | |
2006 | struct stat info; /* cupsd.conf information */ | |
2007 | cups_file_t *cupsd; /* cupsd.conf file */ | |
355e94dc MS |
2008 | char *buffer, /* Buffer for entire file */ |
2009 | *bufptr, /* Pointer into buffer */ | |
2010 | *bufend; /* End of buffer */ | |
2011 | int ch; /* Character from file */ | |
323c5de1 | 2012 | char filename[1024]; /* Filename */ |
2013 | const char *server_root; /* Location of config files */ | |
2014 | ||
2015 | ||
ef416fc2 | 2016 | /* |
323c5de1 | 2017 | * Locate the cupsd.conf file... |
ef416fc2 | 2018 | */ |
2019 | ||
323c5de1 | 2020 | if ((server_root = getenv("CUPS_SERVERROOT")) == NULL) |
2021 | server_root = CUPS_SERVERROOT; | |
ef416fc2 | 2022 | |
323c5de1 | 2023 | snprintf(filename, sizeof(filename), "%s/cupsd.conf", server_root); |
ef416fc2 | 2024 | |
2025 | /* | |
323c5de1 | 2026 | * Figure out the size... |
ef416fc2 | 2027 | */ |
2028 | ||
323c5de1 | 2029 | if (stat(filename, &info)) |
ef416fc2 | 2030 | { |
323c5de1 | 2031 | cgiStartHTML(cgiText(_("Edit Configuration File"))); |
2032 | cgiSetVariable("MESSAGE", | |
2033 | cgiText(_("Unable to access cupsd.conf file:"))); | |
2034 | cgiSetVariable("ERROR", strerror(errno)); | |
2035 | cgiCopyTemplateLang("error.tmpl"); | |
2036 | cgiEndHTML(); | |
ef416fc2 | 2037 | |
323c5de1 | 2038 | perror(filename); |
2039 | return; | |
2040 | } | |
ef416fc2 | 2041 | |
323c5de1 | 2042 | if (info.st_size > (1024 * 1024)) |
2043 | { | |
2044 | cgiStartHTML(cgiText(_("Edit Configuration File"))); | |
2045 | cgiSetVariable("MESSAGE", | |
2046 | cgiText(_("Unable to access cupsd.conf file:"))); | |
2047 | cgiSetVariable("ERROR", | |
2048 | cgiText(_("Unable to edit cupsd.conf files larger than " | |
4d301e69 | 2049 | "1MB"))); |
323c5de1 | 2050 | cgiCopyTemplateLang("error.tmpl"); |
2051 | cgiEndHTML(); | |
ef416fc2 | 2052 | |
323c5de1 | 2053 | fprintf(stderr, "ERROR: \"%s\" too large (%ld) to edit!\n", filename, |
2054 | (long)info.st_size); | |
2055 | return; | |
2056 | } | |
ef416fc2 | 2057 | |
323c5de1 | 2058 | /* |
2059 | * Open the cupsd.conf file... | |
2060 | */ | |
ef416fc2 | 2061 | |
323c5de1 | 2062 | if ((cupsd = cupsFileOpen(filename, "r")) == NULL) |
2063 | { | |
2064 | /* | |
2065 | * Unable to open - log an error... | |
2066 | */ | |
ef416fc2 | 2067 | |
323c5de1 | 2068 | cgiStartHTML(cgiText(_("Edit Configuration File"))); |
2069 | cgiSetVariable("MESSAGE", | |
2070 | cgiText(_("Unable to access cupsd.conf file:"))); | |
2071 | cgiSetVariable("ERROR", strerror(errno)); | |
2072 | cgiCopyTemplateLang("error.tmpl"); | |
2073 | cgiEndHTML(); | |
ef416fc2 | 2074 | |
323c5de1 | 2075 | perror(filename); |
2076 | return; | |
2077 | } | |
ef416fc2 | 2078 | |
323c5de1 | 2079 | /* |
2080 | * Allocate memory and load the file into a string buffer... | |
2081 | */ | |
ef416fc2 | 2082 | |
91c84a35 MS |
2083 | if ((buffer = calloc(1, info.st_size + 1)) != NULL) |
2084 | { | |
2085 | cupsFileRead(cupsd, buffer, info.st_size); | |
2086 | cgiSetVariable("CUPSDCONF", buffer); | |
2087 | free(buffer); | |
2088 | } | |
ef416fc2 | 2089 | |
323c5de1 | 2090 | cupsFileClose(cupsd); |
ef416fc2 | 2091 | |
355e94dc MS |
2092 | /* |
2093 | * Then get the default cupsd.conf file and put that into a string as | |
2094 | * well... | |
2095 | */ | |
2096 | ||
2097 | strlcat(filename, ".default", sizeof(filename)); | |
2098 | ||
2099 | if (!stat(filename, &info) && info.st_size < (1024 * 1024) && | |
2100 | (cupsd = cupsFileOpen(filename, "r")) != NULL) | |
2101 | { | |
91c84a35 | 2102 | if ((buffer = calloc(1, 2 * info.st_size + 1)) != NULL) |
355e94dc | 2103 | { |
91c84a35 MS |
2104 | bufend = buffer + 2 * info.st_size - 1; |
2105 | ||
2106 | for (bufptr = buffer; | |
2107 | bufptr < bufend && (ch = cupsFileGetChar(cupsd)) != EOF;) | |
355e94dc | 2108 | { |
91c84a35 MS |
2109 | if (ch == '\\' || ch == '\"') |
2110 | { | |
2111 | *bufptr++ = '\\'; | |
2112 | *bufptr++ = ch; | |
2113 | } | |
2114 | else if (ch == '\n') | |
2115 | { | |
2116 | *bufptr++ = '\\'; | |
2117 | *bufptr++ = 'n'; | |
2118 | } | |
2119 | else if (ch == '\t') | |
2120 | { | |
2121 | *bufptr++ = '\\'; | |
2122 | *bufptr++ = 't'; | |
2123 | } | |
2124 | else if (ch >= ' ') | |
2125 | *bufptr++ = ch; | |
355e94dc | 2126 | } |
355e94dc | 2127 | |
91c84a35 | 2128 | *bufptr = '\0'; |
355e94dc | 2129 | |
91c84a35 MS |
2130 | cgiSetVariable("CUPSDCONF_DEFAULT", buffer); |
2131 | free(buffer); | |
2132 | } | |
355e94dc | 2133 | |
91c84a35 | 2134 | cupsFileClose(cupsd); |
355e94dc MS |
2135 | } |
2136 | ||
323c5de1 | 2137 | /* |
2138 | * Show the current config file... | |
2139 | */ | |
ef416fc2 | 2140 | |
323c5de1 | 2141 | cgiStartHTML(cgiText(_("Edit Configuration File"))); |
ef416fc2 | 2142 | |
323c5de1 | 2143 | cgiCopyTemplateLang("edit-config.tmpl"); |
ef416fc2 | 2144 | |
323c5de1 | 2145 | cgiEndHTML(); |
2146 | } | |
2147 | } | |
ef416fc2 | 2148 | |
ef416fc2 | 2149 | |
323c5de1 | 2150 | /* |
749b1e90 | 2151 | * 'do_delete_class()' - Delete a class. |
323c5de1 | 2152 | */ |
ef416fc2 | 2153 | |
323c5de1 | 2154 | static void |
2155 | do_delete_class(http_t *http) /* I - HTTP connection */ | |
2156 | { | |
2157 | ipp_t *request; /* IPP request */ | |
2158 | char uri[HTTP_MAX_URI]; /* Job URI */ | |
2159 | const char *pclass; /* Printer class name */ | |
ef416fc2 | 2160 | |
ef416fc2 | 2161 | |
323c5de1 | 2162 | /* |
2163 | * Get form variables... | |
2164 | */ | |
ef416fc2 | 2165 | |
323c5de1 | 2166 | if (cgiGetVariable("CONFIRM") == NULL) |
2167 | { | |
2168 | cgiStartHTML(cgiText(_("Delete Class"))); | |
2169 | cgiCopyTemplateLang("class-confirm.tmpl"); | |
2170 | cgiEndHTML(); | |
2171 | return; | |
2172 | } | |
ef416fc2 | 2173 | |
323c5de1 | 2174 | if ((pclass = cgiGetVariable("PRINTER_NAME")) != NULL) |
2175 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, | |
2176 | "localhost", 0, "/classes/%s", pclass); | |
2177 | else | |
2178 | { | |
2179 | cgiStartHTML(cgiText(_("Delete Class"))); | |
4d301e69 | 2180 | cgiSetVariable("ERROR", cgiText(_("Missing form variable"))); |
323c5de1 | 2181 | cgiCopyTemplateLang("error.tmpl"); |
2182 | cgiEndHTML(); | |
2183 | return; | |
2184 | } | |
ef416fc2 | 2185 | |
323c5de1 | 2186 | /* |
2187 | * Build a CUPS_DELETE_CLASS request, which requires the following | |
2188 | * attributes: | |
2189 | * | |
2190 | * attributes-charset | |
2191 | * attributes-natural-language | |
2192 | * printer-uri | |
2193 | */ | |
2194 | ||
2195 | request = ippNewRequest(CUPS_DELETE_CLASS); | |
2196 | ||
2197 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
2198 | NULL, uri); | |
2199 | ||
2200 | /* | |
2201 | * Do the request and get back a response... | |
2202 | */ | |
2203 | ||
2204 | ippDelete(cupsDoRequest(http, request, "/admin/")); | |
ef416fc2 | 2205 | |
323c5de1 | 2206 | /* |
2207 | * Show the results... | |
2208 | */ | |
2209 | ||
355e94dc MS |
2210 | if (cupsLastError() == IPP_NOT_AUTHORIZED) |
2211 | { | |
2212 | puts("Status: 401\n"); | |
2213 | exit(0); | |
2214 | } | |
2215 | else if (cupsLastError() <= IPP_OK_CONFLICT) | |
323c5de1 | 2216 | { |
ef416fc2 | 2217 | /* |
323c5de1 | 2218 | * Redirect successful updates back to the classes page... |
ef416fc2 | 2219 | */ |
2220 | ||
323c5de1 | 2221 | cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect&URL=/classes"); |
2222 | } | |
ef416fc2 | 2223 | |
323c5de1 | 2224 | cgiStartHTML(cgiText(_("Delete Class"))); |
ef416fc2 | 2225 | |
323c5de1 | 2226 | if (cupsLastError() > IPP_OK_CONFLICT) |
2227 | cgiShowIPPError(_("Unable to delete class:")); | |
2228 | else | |
2229 | cgiCopyTemplateLang("class-deleted.tmpl"); | |
ef416fc2 | 2230 | |
323c5de1 | 2231 | cgiEndHTML(); |
2232 | } | |
ef416fc2 | 2233 | |
ef416fc2 | 2234 | |
323c5de1 | 2235 | /* |
749b1e90 | 2236 | * 'do_delete_printer()' - Delete a printer. |
323c5de1 | 2237 | */ |
ef416fc2 | 2238 | |
323c5de1 | 2239 | static void |
2240 | do_delete_printer(http_t *http) /* I - HTTP connection */ | |
2241 | { | |
2242 | ipp_t *request; /* IPP request */ | |
2243 | char uri[HTTP_MAX_URI]; /* Job URI */ | |
2244 | const char *printer; /* Printer printer name */ | |
ef416fc2 | 2245 | |
323c5de1 | 2246 | |
2247 | /* | |
2248 | * Get form variables... | |
2249 | */ | |
2250 | ||
2251 | if (cgiGetVariable("CONFIRM") == NULL) | |
2252 | { | |
2253 | cgiStartHTML(cgiText(_("Delete Printer"))); | |
2254 | cgiCopyTemplateLang("printer-confirm.tmpl"); | |
ef416fc2 | 2255 | cgiEndHTML(); |
323c5de1 | 2256 | return; |
ef416fc2 | 2257 | } |
323c5de1 | 2258 | |
2259 | if ((printer = cgiGetVariable("PRINTER_NAME")) != NULL) | |
2260 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, | |
2261 | "localhost", 0, "/printers/%s", printer); | |
ef416fc2 | 2262 | else |
323c5de1 | 2263 | { |
2264 | cgiStartHTML(cgiText(_("Delete Printer"))); | |
4d301e69 | 2265 | cgiSetVariable("ERROR", cgiText(_("Missing form variable"))); |
323c5de1 | 2266 | cgiCopyTemplateLang("error.tmpl"); |
2267 | cgiEndHTML(); | |
2268 | return; | |
2269 | } | |
2270 | ||
2271 | /* | |
2272 | * Build a CUPS_DELETE_PRINTER request, which requires the following | |
2273 | * attributes: | |
2274 | * | |
2275 | * attributes-charset | |
2276 | * attributes-natural-language | |
2277 | * printer-uri | |
2278 | */ | |
2279 | ||
2280 | request = ippNewRequest(CUPS_DELETE_PRINTER); | |
2281 | ||
2282 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
2283 | NULL, uri); | |
2284 | ||
2285 | /* | |
2286 | * Do the request and get back a response... | |
2287 | */ | |
2288 | ||
2289 | ippDelete(cupsDoRequest(http, request, "/admin/")); | |
2290 | ||
2291 | /* | |
2292 | * Show the results... | |
2293 | */ | |
2294 | ||
355e94dc MS |
2295 | if (cupsLastError() == IPP_NOT_AUTHORIZED) |
2296 | { | |
2297 | puts("Status: 401\n"); | |
2298 | exit(0); | |
2299 | } | |
2300 | else if (cupsLastError() <= IPP_OK_CONFLICT) | |
ef416fc2 | 2301 | { |
2302 | /* | |
323c5de1 | 2303 | * Redirect successful updates back to the printers page... |
ef416fc2 | 2304 | */ |
2305 | ||
323c5de1 | 2306 | cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect&URL=/printers"); |
2307 | } | |
bd7854cb | 2308 | |
323c5de1 | 2309 | cgiStartHTML(cgiText(_("Delete Printer"))); |
ef416fc2 | 2310 | |
323c5de1 | 2311 | if (cupsLastError() > IPP_OK_CONFLICT) |
2312 | cgiShowIPPError(_("Unable to delete printer:")); | |
2313 | else | |
2314 | cgiCopyTemplateLang("printer-deleted.tmpl"); | |
ef416fc2 | 2315 | |
323c5de1 | 2316 | cgiEndHTML(); |
2317 | } | |
ef416fc2 | 2318 | |
ef416fc2 | 2319 | |
323c5de1 | 2320 | /* |
749b1e90 | 2321 | * 'do_export()' - Export printers to Samba. |
323c5de1 | 2322 | */ |
ef416fc2 | 2323 | |
323c5de1 | 2324 | static void |
2325 | do_export(http_t *http) /* I - HTTP connection */ | |
2326 | { | |
2327 | int i, j; /* Looping vars */ | |
2328 | ipp_t *request, /* IPP request */ | |
2329 | *response; /* IPP response */ | |
2330 | const char *username, /* Samba username */ | |
2331 | *password, /* Samba password */ | |
2332 | *export_all; /* Export all printers? */ | |
2333 | int export_count, /* Number of printers to export */ | |
2334 | printer_count; /* Number of available printers */ | |
2335 | const char *name, /* What name to pull */ | |
2336 | *dest; /* Current destination */ | |
2337 | char ppd[1024]; /* PPD file */ | |
ef416fc2 | 2338 | |
ef416fc2 | 2339 | |
323c5de1 | 2340 | /* |
2341 | * Get form data... | |
2342 | */ | |
ef416fc2 | 2343 | |
323c5de1 | 2344 | username = cgiGetVariable("USERNAME"); |
2345 | password = cgiGetVariable("PASSWORD"); | |
2346 | export_all = cgiGetVariable("EXPORT_ALL"); | |
2347 | export_count = cgiGetSize("EXPORT_NAME"); | |
ef416fc2 | 2348 | |
323c5de1 | 2349 | /* |
2350 | * Get list of available printers... | |
2351 | */ | |
ef416fc2 | 2352 | |
323c5de1 | 2353 | cgiSetSize("PRINTER_NAME", 0); |
2354 | cgiSetSize("PRINTER_EXPORT", 0); | |
ef416fc2 | 2355 | |
323c5de1 | 2356 | request = ippNewRequest(CUPS_GET_PRINTERS); |
ef416fc2 | 2357 | |
323c5de1 | 2358 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, |
2359 | "printer-type", 0); | |
ef416fc2 | 2360 | |
323c5de1 | 2361 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, |
2362 | "printer-type-mask", CUPS_PRINTER_CLASS | CUPS_PRINTER_REMOTE | | |
2363 | CUPS_PRINTER_IMPLICIT); | |
ef416fc2 | 2364 | |
323c5de1 | 2365 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, |
2366 | "requested-attributes", NULL, "printer-name"); | |
ef416fc2 | 2367 | |
323c5de1 | 2368 | if ((response = cupsDoRequest(http, request, "/")) != NULL) |
2369 | { | |
2370 | cgiSetIPPVars(response, NULL, NULL, NULL, 0); | |
2371 | ippDelete(response); | |
ef416fc2 | 2372 | |
323c5de1 | 2373 | if (!export_all) |
2374 | { | |
2375 | printer_count = cgiGetSize("PRINTER_NAME"); | |
ef416fc2 | 2376 | |
323c5de1 | 2377 | for (i = 0; i < printer_count; i ++) |
2378 | { | |
2379 | dest = cgiGetArray("PRINTER_NAME", i); | |
ef416fc2 | 2380 | |
323c5de1 | 2381 | for (j = 0; j < export_count; j ++) |
2382 | if (!strcasecmp(dest, cgiGetArray("EXPORT_NAME", j))) | |
2383 | break; | |
2384 | ||
2385 | cgiSetArray("PRINTER_EXPORT", i, j < export_count ? "Y" : ""); | |
2386 | } | |
2387 | } | |
2388 | } | |
2389 | ||
2390 | /* | |
2391 | * Export or get the printers to export... | |
2392 | */ | |
ef416fc2 | 2393 | |
323c5de1 | 2394 | if (username && *username && password && *password && |
2395 | (export_all || export_count > 0)) | |
2396 | { | |
ef416fc2 | 2397 | /* |
323c5de1 | 2398 | * Do export... |
ef416fc2 | 2399 | */ |
2400 | ||
323c5de1 | 2401 | fputs("DEBUG: Export printers...\n", stderr); |
ef416fc2 | 2402 | |
323c5de1 | 2403 | if (export_all) |
ef416fc2 | 2404 | { |
323c5de1 | 2405 | name = "PRINTER_NAME"; |
2406 | export_count = cgiGetSize("PRINTER_NAME"); | |
ef416fc2 | 2407 | } |
2408 | else | |
323c5de1 | 2409 | name = "EXPORT_NAME"; |
ef416fc2 | 2410 | |
323c5de1 | 2411 | for (i = 0; i < export_count; i ++) |
2412 | { | |
2413 | dest = cgiGetArray(name, i); | |
ef416fc2 | 2414 | |
323c5de1 | 2415 | if (!cupsAdminCreateWindowsPPD(http, dest, ppd, sizeof(ppd))) |
2416 | break; | |
fa73b229 | 2417 | |
323c5de1 | 2418 | j = cupsAdminExportSamba(dest, ppd, "localhost", username, password, |
2419 | stderr); | |
ef416fc2 | 2420 | |
323c5de1 | 2421 | unlink(ppd); |
ef416fc2 | 2422 | |
323c5de1 | 2423 | if (!j) |
2424 | break; | |
ef416fc2 | 2425 | } |
2426 | ||
323c5de1 | 2427 | if (i < export_count) |
2428 | cgiSetVariable("ERROR", cupsLastErrorString()); | |
2429 | else | |
2430 | { | |
2431 | cgiStartHTML(cgiText(_("Export Printers to Samba"))); | |
2432 | cgiCopyTemplateLang("samba-exported.tmpl"); | |
2433 | cgiEndHTML(); | |
2434 | return; | |
2435 | } | |
ef416fc2 | 2436 | } |
323c5de1 | 2437 | else if (username && !*username) |
2438 | cgiSetVariable("ERROR", | |
2439 | cgiText(_("A Samba username is required to export " | |
4d301e69 | 2440 | "printer drivers"))); |
323c5de1 | 2441 | else if (username && (!password || !*password)) |
2442 | cgiSetVariable("ERROR", | |
2443 | cgiText(_("A Samba password is required to export " | |
4d301e69 | 2444 | "printer drivers"))); |
323c5de1 | 2445 | |
2446 | /* | |
2447 | * Show form... | |
2448 | */ | |
ef416fc2 | 2449 | |
323c5de1 | 2450 | cgiStartHTML(cgiText(_("Export Printers to Samba"))); |
2451 | cgiCopyTemplateLang("samba-export.tmpl"); | |
2452 | cgiEndHTML(); | |
ef416fc2 | 2453 | } |
2454 | ||
2455 | ||
2456 | /* | |
749b1e90 | 2457 | * 'do_list_printers()' - List available printers. |
ef416fc2 | 2458 | */ |
2459 | ||
2460 | static void | |
323c5de1 | 2461 | do_list_printers(http_t *http) /* I - HTTP connection */ |
ef416fc2 | 2462 | { |
323c5de1 | 2463 | ipp_t *request, /* IPP request */ |
2464 | *response; /* IPP response */ | |
2465 | ipp_attribute_t *attr; /* IPP attribute */ | |
ef416fc2 | 2466 | |
757d2cad | 2467 | |
323c5de1 | 2468 | cgiStartHTML(cgiText(_("List Available Printers"))); |
2469 | fflush(stdout); | |
2470 | ||
2471 | /* | |
2472 | * Get the list of printers and their devices... | |
2473 | */ | |
2474 | ||
2475 | request = ippNewRequest(CUPS_GET_PRINTERS); | |
2476 | ||
2477 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
2478 | "requested-attributes", NULL, "device-uri"); | |
2479 | ||
2480 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, "printer-type", | |
2481 | CUPS_PRINTER_LOCAL); | |
2482 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, "printer-type-mask", | |
2483 | CUPS_PRINTER_LOCAL); | |
757d2cad | 2484 | |
323c5de1 | 2485 | if ((response = cupsDoRequest(http, request, "/")) != NULL) |
2486 | { | |
d09495fa | 2487 | /* |
323c5de1 | 2488 | * Got the printer list, now load the devices... |
d09495fa | 2489 | */ |
2490 | ||
323c5de1 | 2491 | int i; /* Looping var */ |
2492 | cups_array_t *printer_devices; /* Printer devices for local printers */ | |
2493 | char *printer_device; /* Current printer device */ | |
7594b224 | 2494 | |
d09495fa | 2495 | |
2496 | /* | |
323c5de1 | 2497 | * Allocate an array and copy the device strings... |
d09495fa | 2498 | */ |
2499 | ||
323c5de1 | 2500 | printer_devices = cupsArrayNew((cups_array_func_t)strcmp, NULL); |
2501 | ||
2502 | for (attr = ippFindAttribute(response, "device-uri", IPP_TAG_URI); | |
2503 | attr; | |
2504 | attr = ippFindNextAttribute(response, "device-uri", IPP_TAG_URI)) | |
ef416fc2 | 2505 | { |
cc754834 | 2506 | cupsArrayAdd(printer_devices, _cupsStrAlloc(attr->values[0].string.text)); |
d09495fa | 2507 | } |
2508 | ||
2509 | /* | |
323c5de1 | 2510 | * Free the printer list and get the device list... |
d09495fa | 2511 | */ |
2512 | ||
323c5de1 | 2513 | ippDelete(response); |
d09495fa | 2514 | |
323c5de1 | 2515 | request = ippNewRequest(CUPS_GET_DEVICES); |
d09495fa | 2516 | |
323c5de1 | 2517 | if ((response = cupsDoRequest(http, request, "/")) != NULL) |
ef416fc2 | 2518 | { |
d09495fa | 2519 | /* |
323c5de1 | 2520 | * Got the device list, let's parse it... |
d09495fa | 2521 | */ |
2522 | ||
323c5de1 | 2523 | const char *device_uri, /* device-uri attribute value */ |
2524 | *device_make_and_model, /* device-make-and-model value */ | |
2525 | *device_info; /* device-info value */ | |
ef416fc2 | 2526 | |
ef416fc2 | 2527 | |
323c5de1 | 2528 | for (i = 0, attr = response->attrs; attr; attr = attr->next) |
2529 | { | |
2530 | /* | |
2531 | * Skip leading attributes until we hit a device... | |
2532 | */ | |
ef416fc2 | 2533 | |
323c5de1 | 2534 | while (attr && attr->group_tag != IPP_TAG_PRINTER) |
2535 | attr = attr->next; | |
ef416fc2 | 2536 | |
323c5de1 | 2537 | if (!attr) |
2538 | break; | |
ef416fc2 | 2539 | |
323c5de1 | 2540 | /* |
2541 | * Pull the needed attributes from this device... | |
2542 | */ | |
ef416fc2 | 2543 | |
323c5de1 | 2544 | device_info = NULL; |
2545 | device_make_and_model = NULL; | |
2546 | device_uri = NULL; | |
ef416fc2 | 2547 | |
323c5de1 | 2548 | while (attr && attr->group_tag == IPP_TAG_PRINTER) |
2549 | { | |
2550 | if (!strcmp(attr->name, "device-info") && | |
2551 | attr->value_tag == IPP_TAG_TEXT) | |
2552 | device_info = attr->values[0].string.text; | |
ef416fc2 | 2553 | |
323c5de1 | 2554 | if (!strcmp(attr->name, "device-make-and-model") && |
2555 | attr->value_tag == IPP_TAG_TEXT) | |
2556 | device_make_and_model = attr->values[0].string.text; | |
ef416fc2 | 2557 | |
323c5de1 | 2558 | if (!strcmp(attr->name, "device-uri") && |
2559 | attr->value_tag == IPP_TAG_URI) | |
2560 | device_uri = attr->values[0].string.text; | |
ef416fc2 | 2561 | |
323c5de1 | 2562 | attr = attr->next; |
2563 | } | |
ef416fc2 | 2564 | |
323c5de1 | 2565 | /* |
2566 | * See if we have everything needed... | |
2567 | */ | |
ef416fc2 | 2568 | |
323c5de1 | 2569 | if (device_info && device_make_and_model && device_uri && |
2570 | strcasecmp(device_make_and_model, "unknown") && | |
2571 | strchr(device_uri, ':')) | |
2572 | { | |
2573 | /* | |
2574 | * Yes, now see if there is already a printer for this | |
2575 | * device... | |
2576 | */ | |
ef416fc2 | 2577 | |
323c5de1 | 2578 | if (!cupsArrayFind(printer_devices, (void *)device_uri)) |
2579 | { | |
2580 | /* | |
2581 | * Not found, so it must be a new printer... | |
2582 | */ | |
ef416fc2 | 2583 | |
2e4ff8af MS |
2584 | char option[1024], /* Form variables for this device */ |
2585 | *option_ptr; /* Pointer into string */ | |
323c5de1 | 2586 | const char *ptr; /* Pointer into device string */ |
ef416fc2 | 2587 | |
fa73b229 | 2588 | |
323c5de1 | 2589 | /* |
2590 | * Format the printer name variable for this device... | |
2591 | * | |
2592 | * We use the device-info string first, then device-uri, | |
2593 | * and finally device-make-and-model to come up with a | |
2594 | * suitable name. | |
2595 | */ | |
ef416fc2 | 2596 | |
323c5de1 | 2597 | if (strncasecmp(device_info, "unknown", 7)) |
2598 | ptr = device_info; | |
2599 | else if ((ptr = strstr(device_uri, "://")) != NULL) | |
2600 | ptr += 3; | |
2601 | else | |
2602 | ptr = device_make_and_model; | |
ef416fc2 | 2603 | |
2e4ff8af MS |
2604 | for (option_ptr = option; |
2605 | option_ptr < (option + sizeof(option) - 1) && *ptr; | |
323c5de1 | 2606 | ptr ++) |
2607 | if (isalnum(*ptr & 255) || *ptr == '_' || *ptr == '-' || | |
2608 | *ptr == '.') | |
2e4ff8af | 2609 | *option_ptr++ = *ptr; |
e60ec91f MS |
2610 | else if ((*ptr == ' ' || *ptr == '/') && option_ptr > option && |
2611 | option_ptr[-1] != '_') | |
2e4ff8af | 2612 | *option_ptr++ = '_'; |
323c5de1 | 2613 | else if (*ptr == '?' || *ptr == '(') |
2614 | break; | |
ef416fc2 | 2615 | |
2e4ff8af | 2616 | *option_ptr = '\0'; |
ef416fc2 | 2617 | |
2e4ff8af | 2618 | cgiSetArray("TEMPLATE_NAME", i, option); |
ef416fc2 | 2619 | |
323c5de1 | 2620 | /* |
2621 | * Finally, set the form variables for this printer... | |
2622 | */ | |
ef416fc2 | 2623 | |
323c5de1 | 2624 | cgiSetArray("device_info", i, device_info); |
2625 | cgiSetArray("device_make_and_model", i, device_make_and_model); | |
323c5de1 | 2626 | cgiSetArray("device_uri", i, device_uri); |
2627 | i ++; | |
2628 | } | |
2629 | } | |
ef416fc2 | 2630 | |
323c5de1 | 2631 | if (!attr) |
2632 | break; | |
2633 | } | |
2634 | ||
2635 | ippDelete(response); | |
ef416fc2 | 2636 | |
ef416fc2 | 2637 | /* |
323c5de1 | 2638 | * Free the device list... |
ef416fc2 | 2639 | */ |
2640 | ||
323c5de1 | 2641 | for (printer_device = (char *)cupsArrayFirst(printer_devices); |
2642 | printer_device; | |
2643 | printer_device = (char *)cupsArrayNext(printer_devices)) | |
cc754834 | 2644 | _cupsStrFree(printer_device); |
ef416fc2 | 2645 | |
323c5de1 | 2646 | cupsArrayDelete(printer_devices); |
ef416fc2 | 2647 | } |
323c5de1 | 2648 | } |
ef416fc2 | 2649 | |
323c5de1 | 2650 | /* |
2651 | * Finally, show the printer list... | |
2652 | */ | |
ef416fc2 | 2653 | |
323c5de1 | 2654 | cgiCopyTemplateLang("list-available-printers.tmpl"); |
ef416fc2 | 2655 | |
323c5de1 | 2656 | cgiEndHTML(); |
ef416fc2 | 2657 | } |
2658 | ||
2659 | ||
2660 | /* | |
749b1e90 | 2661 | * 'do_menu()' - Show the main menu. |
ef416fc2 | 2662 | */ |
2663 | ||
2664 | static void | |
323c5de1 | 2665 | do_menu(http_t *http) /* I - HTTP connection */ |
ef416fc2 | 2666 | { |
323c5de1 | 2667 | int num_settings; /* Number of server settings */ |
2668 | cups_option_t *settings; /* Server settings */ | |
2669 | const char *val; /* Setting value */ | |
2670 | char filename[1024]; /* Temporary filename */ | |
2671 | const char *datadir; /* Location of data files */ | |
2672 | ipp_t *request, /* IPP request */ | |
2673 | *response; /* IPP response */ | |
ef416fc2 | 2674 | |
2675 | ||
89d46774 | 2676 | /* |
323c5de1 | 2677 | * Get the current server settings... |
89d46774 | 2678 | */ |
fa73b229 | 2679 | |
323c5de1 | 2680 | if (!cupsAdminGetServerSettings(http, &num_settings, &settings)) |
ef416fc2 | 2681 | { |
323c5de1 | 2682 | cgiSetVariable("SETTINGS_MESSAGE", |
2683 | cgiText(_("Unable to open cupsd.conf file:"))); | |
2684 | cgiSetVariable("SETTINGS_ERROR", cupsLastErrorString()); | |
ef416fc2 | 2685 | } |
2686 | ||
323c5de1 | 2687 | if ((val = cupsGetOption(CUPS_SERVER_DEBUG_LOGGING, num_settings, |
2688 | settings)) != NULL && atoi(val)) | |
2689 | cgiSetVariable("DEBUG_LOGGING", "CHECKED"); | |
ef416fc2 | 2690 | |
323c5de1 | 2691 | if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ADMIN, num_settings, |
2692 | settings)) != NULL && atoi(val)) | |
2693 | cgiSetVariable("REMOTE_ADMIN", "CHECKED"); | |
ef416fc2 | 2694 | |
323c5de1 | 2695 | if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ANY, num_settings, |
2696 | settings)) != NULL && atoi(val)) | |
2697 | cgiSetVariable("REMOTE_ANY", "CHECKED"); | |
ef416fc2 | 2698 | |
323c5de1 | 2699 | if ((val = cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS, num_settings, |
2700 | settings)) != NULL && atoi(val)) | |
2701 | cgiSetVariable("REMOTE_PRINTERS", "CHECKED"); | |
ef416fc2 | 2702 | |
323c5de1 | 2703 | if ((val = cupsGetOption(CUPS_SERVER_SHARE_PRINTERS, num_settings, |
2704 | settings)) != NULL && atoi(val)) | |
2705 | cgiSetVariable("SHARE_PRINTERS", "CHECKED"); | |
ef416fc2 | 2706 | |
323c5de1 | 2707 | if ((val = cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY, num_settings, |
2708 | settings)) != NULL && atoi(val)) | |
2709 | cgiSetVariable("USER_CANCEL_ANY", "CHECKED"); | |
2710 | ||
2711 | #ifdef HAVE_GSSAPI | |
2712 | cgiSetVariable("HAVE_GSSAPI", "1"); | |
2713 | ||
2714 | if ((val = cupsGetOption("DefaultAuthType", num_settings, | |
2715 | settings)) != NULL && !strcasecmp(val, "Negotiate")) | |
2716 | cgiSetVariable("KERBEROS", "CHECKED"); | |
ef55b745 | 2717 | else |
323c5de1 | 2718 | #endif /* HAVE_GSSAPI */ |
ef55b745 | 2719 | cgiSetVariable("KERBEROS", ""); |
323c5de1 | 2720 | |
0af14961 MS |
2721 | #ifdef HAVE_DNSSD |
2722 | cgiSetVariable("HAVE_DNSSD", "1"); | |
2723 | #endif /* HAVE_DNSSD */ | |
2724 | ||
2725 | #ifdef HAVE_LDAP | |
2726 | cgiSetVariable("HAVE_LDAP", "1"); | |
2727 | #endif /* HAVE_LDAP */ | |
2728 | ||
2729 | #ifdef HAVE_LIBSLP | |
2730 | cgiSetVariable("HAVE_LIBSLP", "1"); | |
2731 | #endif /* HAVE_LIBSLP */ | |
2732 | ||
2733 | if ((val = cupsGetOption("BrowseRemoteProtocols", num_settings, | |
2734 | settings)) == NULL) | |
2735 | if ((val = cupsGetOption("BrowseProtocols", num_settings, | |
2736 | settings)) == NULL) | |
2737 | val = CUPS_DEFAULT_BROWSE_REMOTE_PROTOCOLS; | |
2738 | ||
2739 | if (strstr(val, "cups") || strstr(val, "CUPS")) | |
2740 | cgiSetVariable("BROWSE_REMOTE_CUPS", "CHECKED"); | |
2741 | ||
2742 | if (strstr(val, "ldap") || strstr(val, "LDAP")) | |
2743 | cgiSetVariable("BROWSE_REMOTE_LDAP", "CHECKED"); | |
2744 | ||
2745 | if (strstr(val, "slp") || strstr(val, "SLP")) | |
2746 | cgiSetVariable("BROWSE_REMOTE_SLP", "CHECKED"); | |
2747 | ||
2748 | if ((val = cupsGetOption("BrowseLocalProtocols", num_settings, | |
2749 | settings)) == NULL) | |
2750 | if ((val = cupsGetOption("BrowseProtocols", num_settings, | |
2751 | settings)) == NULL) | |
2752 | val = CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS; | |
2753 | ||
2754 | if (strstr(val, "cups") || strstr(val, "CUPS")) | |
2755 | cgiSetVariable("BROWSE_LOCAL_CUPS", "CHECKED"); | |
2756 | ||
2757 | if (strstr(val, "dnssd") || strstr(val, "DNSSD") || | |
2758 | strstr(val, "dns-sd") || strstr(val, "DNS-SD") || | |
2759 | strstr(val, "bonjour") || strstr(val, "BONJOUR")) | |
2760 | cgiSetVariable("BROWSE_LOCAL_DNSSD", "CHECKED"); | |
2761 | ||
2762 | if (strstr(val, "ldap") || strstr(val, "LDAP")) | |
2763 | cgiSetVariable("BROWSE_LOCAL_LDAP", "CHECKED"); | |
2764 | ||
2765 | if (strstr(val, "slp") || strstr(val, "SLP")) | |
2766 | cgiSetVariable("BROWSE_LOCAL_SLP", "CHECKED"); | |
2767 | ||
2768 | if ((val = cupsGetOption("BrowseWebIF", num_settings, | |
2769 | settings)) == NULL) | |
2770 | val = "No"; | |
2771 | ||
2772 | if (!strcasecmp(val, "yes") || !strcasecmp(val, "on") || | |
2773 | !strcasecmp(val, "true")) | |
2774 | cgiSetVariable("BROWSE_WEB_IF", "CHECKED"); | |
2775 | ||
2776 | if ((val = cupsGetOption("PreserveJobHistory", num_settings, | |
2777 | settings)) == NULL) | |
2778 | val = "Yes"; | |
2779 | ||
2780 | if (!strcasecmp(val, "yes") || !strcasecmp(val, "on") || | |
2781 | !strcasecmp(val, "true")) | |
2782 | { | |
2783 | cgiSetVariable("PRESERVE_JOB_HISTORY", "CHECKED"); | |
2784 | ||
2785 | if ((val = cupsGetOption("PreserveJobFiles", num_settings, | |
2786 | settings)) == NULL) | |
2787 | val = "No"; | |
2788 | ||
2789 | if (!strcasecmp(val, "yes") || !strcasecmp(val, "on") || | |
2790 | !strcasecmp(val, "true")) | |
2791 | cgiSetVariable("PRESERVE_JOB_FILES", "CHECKED"); | |
2792 | } | |
2793 | ||
1f6f3dbc MS |
2794 | if ((val = cupsGetOption("MaxClients", num_settings, settings)) == NULL) |
2795 | val = "100"; | |
2796 | ||
2797 | cgiSetVariable("MAX_CLIENTS", val); | |
2798 | ||
0af14961 MS |
2799 | if ((val = cupsGetOption("MaxJobs", num_settings, settings)) == NULL) |
2800 | val = "500"; | |
2801 | ||
2802 | cgiSetVariable("MAX_JOBS", val); | |
2803 | ||
2804 | if ((val = cupsGetOption("MaxLogSize", num_settings, settings)) == NULL) | |
2805 | val = "1m"; | |
2806 | ||
2807 | cgiSetVariable("MAX_LOG_SIZE", val); | |
2808 | ||
323c5de1 | 2809 | cupsFreeOptions(num_settings, settings); |
ef416fc2 | 2810 | |
89d46774 | 2811 | /* |
323c5de1 | 2812 | * See if Samba and the Windows drivers are installed... |
89d46774 | 2813 | */ |
2814 | ||
323c5de1 | 2815 | if ((datadir = getenv("CUPS_DATADIR")) == NULL) |
2816 | datadir = CUPS_DATADIR; | |
2817 | ||
2818 | snprintf(filename, sizeof(filename), "%s/drivers/pscript5.dll", datadir); | |
2819 | if (!access(filename, R_OK)) | |
f7deaa1a | 2820 | { |
2821 | /* | |
323c5de1 | 2822 | * Found Windows 2000 driver file, see if we have smbclient and |
2823 | * rpcclient... | |
f7deaa1a | 2824 | */ |
2825 | ||
323c5de1 | 2826 | if (cupsFileFind("smbclient", getenv("PATH"), 1, filename, |
2827 | sizeof(filename)) && | |
2828 | cupsFileFind("rpcclient", getenv("PATH"), 1, filename, | |
2829 | sizeof(filename))) | |
2830 | cgiSetVariable("HAVE_SAMBA", "Y"); | |
2831 | else | |
2832 | { | |
2833 | if (!cupsFileFind("smbclient", getenv("PATH"), 1, filename, | |
2834 | sizeof(filename))) | |
2835 | fputs("ERROR: smbclient not found!\n", stderr); | |
2836 | ||
2837 | if (!cupsFileFind("rpcclient", getenv("PATH"), 1, filename, | |
2838 | sizeof(filename))) | |
2839 | fputs("ERROR: rpcclient not found!\n", stderr); | |
2840 | } | |
f7deaa1a | 2841 | } |
323c5de1 | 2842 | else |
2843 | perror(filename); | |
f7deaa1a | 2844 | |
323c5de1 | 2845 | /* |
2846 | * Subscriptions... | |
2847 | */ | |
89d46774 | 2848 | |
323c5de1 | 2849 | request = ippNewRequest(IPP_GET_SUBSCRIPTIONS); |
2850 | ||
2851 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
2852 | NULL, "ipp://localhost/"); | |
2853 | ||
2854 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
2855 | { | |
2856 | cgiSetIPPVars(response, NULL, NULL, NULL, 0); | |
2857 | ippDelete(response); | |
2858 | } | |
2859 | ||
2860 | /* | |
2861 | * Finally, show the main menu template... | |
2862 | */ | |
2863 | ||
2864 | cgiStartHTML(cgiText(_("Administration"))); | |
2865 | ||
2866 | cgiCopyTemplateLang("admin.tmpl"); | |
ef416fc2 | 2867 | |
2868 | cgiEndHTML(); | |
2869 | } | |
2870 | ||
2871 | ||
fa73b229 | 2872 | /* |
323c5de1 | 2873 | * 'do_set_allowed_users()' - Set the allowed/denied users for a queue. |
fa73b229 | 2874 | */ |
2875 | ||
2876 | static void | |
323c5de1 | 2877 | do_set_allowed_users(http_t *http) /* I - HTTP connection */ |
fa73b229 | 2878 | { |
323c5de1 | 2879 | int i; /* Looping var */ |
fa73b229 | 2880 | ipp_t *request, /* IPP request */ |
2881 | *response; /* IPP response */ | |
323c5de1 | 2882 | char uri[HTTP_MAX_URI]; /* Printer URI */ |
2883 | const char *printer, /* Printer name (purge-jobs) */ | |
2884 | *is_class, /* Is a class? */ | |
2885 | *users, /* List of users or groups */ | |
2886 | *type; /* Allow/deny type */ | |
2887 | int num_users; /* Number of users */ | |
2888 | char *ptr, /* Pointer into users string */ | |
2889 | *end, /* Pointer to end of users string */ | |
2890 | quote; /* Quote character */ | |
2891 | ipp_attribute_t *attr; /* Attribute */ | |
2892 | static const char * const attrs[] = /* Requested attributes */ | |
2893 | { | |
2894 | "requesting-user-name-allowed", | |
2895 | "requesting-user-name-denied" | |
2896 | }; | |
fa73b229 | 2897 | |
fa73b229 | 2898 | |
323c5de1 | 2899 | is_class = cgiGetVariable("IS_CLASS"); |
2900 | printer = cgiGetVariable("PRINTER_NAME"); | |
fa73b229 | 2901 | |
323c5de1 | 2902 | if (!printer) |
2903 | { | |
4d301e69 | 2904 | cgiSetVariable("ERROR", cgiText(_("Missing form variable"))); |
323c5de1 | 2905 | cgiStartHTML(cgiText(_("Set Allowed Users"))); |
2906 | cgiCopyTemplateLang("error.tmpl"); | |
2907 | cgiEndHTML(); | |
2908 | return; | |
2909 | } | |
ef416fc2 | 2910 | |
323c5de1 | 2911 | users = cgiGetVariable("users"); |
2912 | type = cgiGetVariable("type"); | |
ef416fc2 | 2913 | |
323c5de1 | 2914 | if (!users || !type || |
2915 | (strcmp(type, "requesting-user-name-allowed") && | |
2916 | strcmp(type, "requesting-user-name-denied"))) | |
2917 | { | |
2918 | /* | |
2919 | * Build a Get-Printer-Attributes request, which requires the following | |
2920 | * attributes: | |
2921 | * | |
2922 | * attributes-charset | |
2923 | * attributes-natural-language | |
2924 | * printer-uri | |
2925 | * requested-attributes | |
2926 | */ | |
fa73b229 | 2927 | |
323c5de1 | 2928 | request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES); |
fa73b229 | 2929 | |
323c5de1 | 2930 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, |
2931 | "localhost", 0, is_class ? "/classes/%s" : "/printers/%s", | |
2932 | printer); | |
2933 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
2934 | NULL, uri); | |
fa73b229 | 2935 | |
323c5de1 | 2936 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, |
2937 | "requested-attributes", | |
2938 | (int)(sizeof(attrs) / sizeof(attrs[0])), NULL, attrs); | |
fa73b229 | 2939 | |
323c5de1 | 2940 | /* |
2941 | * Do the request and get back a response... | |
2942 | */ | |
fa73b229 | 2943 | |
323c5de1 | 2944 | if ((response = cupsDoRequest(http, request, "/")) != NULL) |
fa73b229 | 2945 | { |
323c5de1 | 2946 | cgiSetIPPVars(response, NULL, NULL, NULL, 0); |
fa73b229 | 2947 | |
323c5de1 | 2948 | ippDelete(response); |
fa73b229 | 2949 | } |
ef416fc2 | 2950 | |
323c5de1 | 2951 | cgiStartHTML(cgiText(_("Set Allowed Users"))); |
757d2cad | 2952 | |
355e94dc MS |
2953 | if (cupsLastError() == IPP_NOT_AUTHORIZED) |
2954 | { | |
2955 | puts("Status: 401\n"); | |
2956 | exit(0); | |
2957 | } | |
2958 | else if (cupsLastError() > IPP_OK_CONFLICT) | |
323c5de1 | 2959 | cgiShowIPPError(_("Unable to get printer attributes:")); |
2960 | else | |
2961 | cgiCopyTemplateLang("users.tmpl"); | |
2962 | ||
2963 | cgiEndHTML(); | |
2964 | } | |
2965 | else | |
757d2cad | 2966 | { |
2967 | /* | |
323c5de1 | 2968 | * Save the changes... |
757d2cad | 2969 | */ |
2970 | ||
323c5de1 | 2971 | for (num_users = 0, ptr = (char *)users; *ptr; num_users ++) |
757d2cad | 2972 | { |
323c5de1 | 2973 | /* |
2974 | * Skip whitespace and commas... | |
2975 | */ | |
fa73b229 | 2976 | |
323c5de1 | 2977 | while (*ptr == ',' || isspace(*ptr & 255)) |
2978 | ptr ++; | |
ef416fc2 | 2979 | |
1f6f3dbc MS |
2980 | if (!*ptr) |
2981 | break; | |
2982 | ||
323c5de1 | 2983 | if (*ptr == '\'' || *ptr == '\"') |
2984 | { | |
2985 | /* | |
2986 | * Scan quoted name... | |
2987 | */ | |
ef416fc2 | 2988 | |
323c5de1 | 2989 | quote = *ptr++; |
ef416fc2 | 2990 | |
323c5de1 | 2991 | for (end = ptr; *end; end ++) |
2992 | if (*end == quote) | |
2993 | break; | |
2994 | } | |
2995 | else | |
2996 | { | |
2997 | /* | |
2998 | * Scan space or comma-delimited name... | |
2999 | */ | |
ef416fc2 | 3000 | |
323c5de1 | 3001 | for (end = ptr; *end; end ++) |
3002 | if (isspace(*end & 255) || *end == ',') | |
3003 | break; | |
3004 | } | |
ef416fc2 | 3005 | |
323c5de1 | 3006 | /* |
3007 | * Advance to the next name... | |
3008 | */ | |
ef416fc2 | 3009 | |
323c5de1 | 3010 | ptr = end; |
3011 | } | |
ef416fc2 | 3012 | |
323c5de1 | 3013 | /* |
3014 | * Build a CUPS-Add-Printer/Class request, which requires the following | |
3015 | * attributes: | |
3016 | * | |
3017 | * attributes-charset | |
3018 | * attributes-natural-language | |
3019 | * printer-uri | |
3020 | * requesting-user-name-{allowed,denied} | |
3021 | */ | |
ef416fc2 | 3022 | |
323c5de1 | 3023 | request = ippNewRequest(is_class ? CUPS_ADD_CLASS : CUPS_ADD_PRINTER); |
ef416fc2 | 3024 | |
323c5de1 | 3025 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, |
3026 | "localhost", 0, is_class ? "/classes/%s" : "/printers/%s", | |
3027 | printer); | |
3028 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
3029 | NULL, uri); | |
f7deaa1a | 3030 | |
323c5de1 | 3031 | if (num_users == 0) |
3032 | ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME, | |
3033 | "requesting-user-name-allowed", NULL, "all"); | |
3034 | else | |
3035 | { | |
3036 | attr = ippAddStrings(request, IPP_TAG_PRINTER, IPP_TAG_NAME, | |
3037 | type, num_users, NULL, NULL); | |
ef416fc2 | 3038 | |
323c5de1 | 3039 | for (i = 0, ptr = (char *)users; *ptr; i ++) |
3040 | { | |
3041 | /* | |
3042 | * Skip whitespace and commas... | |
3043 | */ | |
ef416fc2 | 3044 | |
323c5de1 | 3045 | while (*ptr == ',' || isspace(*ptr & 255)) |
3046 | ptr ++; | |
ef416fc2 | 3047 | |
1f6f3dbc MS |
3048 | if (!*ptr) |
3049 | break; | |
3050 | ||
323c5de1 | 3051 | if (*ptr == '\'' || *ptr == '\"') |
3052 | { | |
3053 | /* | |
3054 | * Scan quoted name... | |
3055 | */ | |
7594b224 | 3056 | |
323c5de1 | 3057 | quote = *ptr++; |
7594b224 | 3058 | |
323c5de1 | 3059 | for (end = ptr; *end; end ++) |
3060 | if (*end == quote) | |
3061 | break; | |
3062 | } | |
3063 | else | |
3064 | { | |
3065 | /* | |
3066 | * Scan space or comma-delimited name... | |
3067 | */ | |
480ef0fe | 3068 | |
323c5de1 | 3069 | for (end = ptr; *end; end ++) |
3070 | if (isspace(*end & 255) || *end == ',') | |
3071 | break; | |
3072 | } | |
ef416fc2 | 3073 | |
323c5de1 | 3074 | /* |
3075 | * Terminate the name... | |
3076 | */ | |
ef416fc2 | 3077 | |
323c5de1 | 3078 | if (*end) |
3079 | *end++ = '\0'; | |
ef416fc2 | 3080 | |
323c5de1 | 3081 | /* |
3082 | * Add the name... | |
3083 | */ | |
ef416fc2 | 3084 | |
cc754834 | 3085 | attr->values[i].string.text = _cupsStrAlloc(ptr); |
ef416fc2 | 3086 | |
323c5de1 | 3087 | /* |
3088 | * Advance to the next name... | |
3089 | */ | |
ef416fc2 | 3090 | |
323c5de1 | 3091 | ptr = end; |
3092 | } | |
3093 | } | |
ef416fc2 | 3094 | |
3095 | /* | |
323c5de1 | 3096 | * Do the request and get back a response... |
ef416fc2 | 3097 | */ |
3098 | ||
323c5de1 | 3099 | ippDelete(cupsDoRequest(http, request, "/admin/")); |
ef416fc2 | 3100 | |
355e94dc MS |
3101 | if (cupsLastError() == IPP_NOT_AUTHORIZED) |
3102 | { | |
3103 | puts("Status: 401\n"); | |
3104 | exit(0); | |
3105 | } | |
3106 | else if (cupsLastError() > IPP_OK_CONFLICT) | |
ef416fc2 | 3107 | { |
323c5de1 | 3108 | cgiStartHTML(cgiText(_("Set Allowed Users"))); |
3109 | cgiShowIPPError(_("Unable to change printer:")); | |
ef416fc2 | 3110 | } |
323c5de1 | 3111 | else |
ef416fc2 | 3112 | { |
3113 | /* | |
323c5de1 | 3114 | * Redirect successful updates back to the printer page... |
ef416fc2 | 3115 | */ |
3116 | ||
323c5de1 | 3117 | char url[1024], /* Printer/class URL */ |
3118 | refresh[1024]; /* Refresh URL */ | |
ef416fc2 | 3119 | |
3120 | ||
323c5de1 | 3121 | cgiRewriteURL(uri, url, sizeof(url), NULL); |
3122 | cgiFormEncode(uri, url, sizeof(uri)); | |
3123 | snprintf(refresh, sizeof(refresh), "5;URL=/admin/?OP=redirect&URL=%s", | |
3124 | uri); | |
3125 | cgiSetVariable("refresh_page", refresh); | |
ef416fc2 | 3126 | |
323c5de1 | 3127 | cgiStartHTML(cgiText(_("Set Allowed Users"))); |
ef416fc2 | 3128 | |
323c5de1 | 3129 | cgiCopyTemplateLang(is_class ? "class-modified.tmpl" : |
3130 | "printer-modified.tmpl"); | |
3131 | } | |
ef416fc2 | 3132 | |
323c5de1 | 3133 | cgiEndHTML(); |
3134 | } | |
3135 | } | |
ef416fc2 | 3136 | |
ef416fc2 | 3137 | |
58dc1933 MS |
3138 | /* |
3139 | * 'do_set_default()' - Set the server default printer/class. | |
3140 | */ | |
3141 | ||
3142 | static void | |
3143 | do_set_default(http_t *http) /* I - HTTP connection */ | |
3144 | { | |
3145 | const char *title; /* Page title */ | |
3146 | ipp_t *request; /* IPP request */ | |
3147 | char uri[HTTP_MAX_URI]; /* Printer URI */ | |
3148 | const char *printer, /* Printer name (purge-jobs) */ | |
3149 | *is_class; /* Is a class? */ | |
3150 | ||
3151 | ||
3152 | is_class = cgiGetVariable("IS_CLASS"); | |
3153 | printer = cgiGetVariable("PRINTER_NAME"); | |
3154 | title = cgiText(_("Set As Server Default")); | |
3155 | ||
3156 | if (!printer) | |
3157 | { | |
4d301e69 | 3158 | cgiSetVariable("ERROR", cgiText(_("Missing form variable"))); |
58dc1933 MS |
3159 | cgiStartHTML(title); |
3160 | cgiCopyTemplateLang("error.tmpl"); | |
3161 | cgiEndHTML(); | |
3162 | return; | |
3163 | } | |
3164 | ||
3165 | /* | |
3166 | * Build a printer request, which requires the following | |
3167 | * attributes: | |
3168 | * | |
3169 | * attributes-charset | |
3170 | * attributes-natural-language | |
3171 | * printer-uri | |
3172 | */ | |
3173 | ||
3174 | request = ippNewRequest(CUPS_SET_DEFAULT); | |
3175 | ||
3176 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, | |
3177 | "localhost", 0, is_class ? "/classes/%s" : "/printers/%s", | |
3178 | printer); | |
3179 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
3180 | NULL, uri); | |
3181 | ||
3182 | /* | |
3183 | * Do the request and get back a response... | |
3184 | */ | |
3185 | ||
3186 | ippDelete(cupsDoRequest(http, request, "/admin/")); | |
3187 | ||
3188 | if (cupsLastError() == IPP_NOT_AUTHORIZED) | |
3189 | { | |
3190 | puts("Status: 401\n"); | |
3191 | exit(0); | |
3192 | } | |
3193 | else if (cupsLastError() > IPP_OK_CONFLICT) | |
3194 | { | |
3195 | cgiStartHTML(title); | |
3196 | cgiShowIPPError(_("Unable to set server default:")); | |
3197 | } | |
3198 | else | |
3199 | { | |
3200 | /* | |
3201 | * Redirect successful updates back to the printer page... | |
3202 | */ | |
3203 | ||
3204 | char url[1024], /* Printer/class URL */ | |
3205 | refresh[1024]; /* Refresh URL */ | |
3206 | ||
3207 | ||
3208 | cgiRewriteURL(uri, url, sizeof(url), NULL); | |
3209 | cgiFormEncode(uri, url, sizeof(uri)); | |
3210 | snprintf(refresh, sizeof(refresh), "5;URL=/admin/?OP=redirect&URL=%s", uri); | |
3211 | cgiSetVariable("refresh_page", refresh); | |
3212 | ||
3213 | cgiStartHTML(title); | |
3214 | cgiCopyTemplateLang("printer-default.tmpl"); | |
3215 | } | |
3216 | ||
3217 | cgiEndHTML(); | |
3218 | } | |
3219 | ||
3220 | ||
323c5de1 | 3221 | /* |
3222 | * 'do_set_options()' - Configure the default options for a queue. | |
3223 | */ | |
ef416fc2 | 3224 | |
323c5de1 | 3225 | static void |
3226 | do_set_options(http_t *http, /* I - HTTP connection */ | |
3227 | int is_class) /* I - Set options for class? */ | |
3228 | { | |
3229 | int i, j, k, m; /* Looping vars */ | |
3230 | int have_options; /* Have options? */ | |
3231 | ipp_t *request, /* IPP request */ | |
3232 | *response; /* IPP response */ | |
3233 | ipp_attribute_t *attr; /* IPP attribute */ | |
3234 | char uri[HTTP_MAX_URI]; /* Job URI */ | |
3235 | const char *var; /* Variable value */ | |
3236 | const char *printer; /* Printer printer name */ | |
3237 | const char *filename; /* PPD filename */ | |
3238 | char tempfile[1024]; /* Temporary filename */ | |
3239 | cups_file_t *in, /* Input file */ | |
3240 | *out; /* Output file */ | |
749b1e90 MS |
3241 | char line[1024], /* Line from PPD file */ |
3242 | value[1024], /* Option value */ | |
3243 | keyword[1024], /* Keyword from Default line */ | |
323c5de1 | 3244 | *keyptr; /* Pointer into keyword... */ |
3245 | ppd_file_t *ppd; /* PPD file */ | |
3246 | ppd_group_t *group; /* Option group */ | |
3247 | ppd_option_t *option; /* Option */ | |
749b1e90 MS |
3248 | ppd_coption_t *coption; /* Custom option */ |
3249 | ppd_cparam_t *cparam; /* Custom parameter */ | |
1f6f3dbc | 3250 | ppd_attr_t *ppdattr; /* PPD attribute */ |
323c5de1 | 3251 | const char *title; /* Page title */ |
ef416fc2 | 3252 | |
ef416fc2 | 3253 | |
323c5de1 | 3254 | title = cgiText(is_class ? _("Set Class Options") : _("Set Printer Options")); |
ef416fc2 | 3255 | |
323c5de1 | 3256 | fprintf(stderr, "DEBUG: do_set_options(http=%p, is_class=%d)\n", http, |
3257 | is_class); | |
ef416fc2 | 3258 | |
323c5de1 | 3259 | /* |
3260 | * Get the printer name... | |
3261 | */ | |
ef416fc2 | 3262 | |
323c5de1 | 3263 | if ((printer = cgiGetVariable("PRINTER_NAME")) != NULL) |
3264 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, | |
3265 | "localhost", 0, is_class ? "/classes/%s" : "/printers/%s", | |
3266 | printer); | |
3267 | else | |
3268 | { | |
4d301e69 | 3269 | cgiSetVariable("ERROR", cgiText(_("Missing form variable"))); |
323c5de1 | 3270 | cgiStartHTML(title); |
3271 | cgiCopyTemplateLang("error.tmpl"); | |
3272 | cgiEndHTML(); | |
3273 | return; | |
3274 | } | |
ef416fc2 | 3275 | |
323c5de1 | 3276 | fprintf(stderr, "DEBUG: printer=\"%s\", uri=\"%s\"...\n", printer, uri); |
ef416fc2 | 3277 | |
1f6f3dbc MS |
3278 | /* |
3279 | * If the user clicks on the Auto-Configure button, send an AutoConfigure | |
3280 | * command file to the printer... | |
3281 | */ | |
3282 | ||
3283 | if (cgiGetVariable("AUTOCONFIGURE")) | |
3284 | { | |
58dc1933 | 3285 | cgiPrintCommand(http, printer, "AutoConfigure", "Set Default Options"); |
1f6f3dbc MS |
3286 | return; |
3287 | } | |
3288 | ||
323c5de1 | 3289 | /* |
3290 | * Get the PPD file... | |
3291 | */ | |
ef416fc2 | 3292 | |
323c5de1 | 3293 | if (is_class) |
3294 | filename = NULL; | |
3295 | else | |
3296 | filename = cupsGetPPD2(http, printer); | |
ef416fc2 | 3297 | |
323c5de1 | 3298 | if (filename) |
3299 | { | |
3300 | fprintf(stderr, "DEBUG: Got PPD file: \"%s\"\n", filename); | |
ef416fc2 | 3301 | |
323c5de1 | 3302 | if ((ppd = ppdOpenFile(filename)) == NULL) |
3303 | { | |
3304 | cgiSetVariable("ERROR", ppdErrorString(ppdLastError(&i))); | |
3305 | cgiSetVariable("MESSAGE", cgiText(_("Unable to open PPD file:"))); | |
3306 | cgiStartHTML(title); | |
3307 | cgiCopyTemplateLang("error.tmpl"); | |
3308 | cgiEndHTML(); | |
3309 | return; | |
3310 | } | |
3311 | } | |
3312 | else | |
3313 | { | |
3314 | fputs("DEBUG: No PPD file\n", stderr); | |
3315 | ppd = NULL; | |
3316 | } | |
ef416fc2 | 3317 | |
323c5de1 | 3318 | if (cgiGetVariable("job_sheets_start") != NULL || |
3319 | cgiGetVariable("job_sheets_end") != NULL) | |
3320 | have_options = 1; | |
3321 | else | |
3322 | have_options = 0; | |
ef416fc2 | 3323 | |
323c5de1 | 3324 | if (ppd) |
3325 | { | |
3326 | ppdMarkDefaults(ppd); | |
ef416fc2 | 3327 | |
749b1e90 MS |
3328 | for (option = ppdFirstOption(ppd); |
3329 | option; | |
3330 | option = ppdNextOption(ppd)) | |
ef55b745 | 3331 | { |
749b1e90 MS |
3332 | if ((var = cgiGetVariable(option->keyword)) != NULL) |
3333 | { | |
3334 | have_options = 1; | |
3335 | ppdMarkOption(ppd, option->keyword, var); | |
ef55b745 | 3336 | fprintf(stderr, "DEBUG: Set %s to %s...\n", option->keyword, var); |
749b1e90 | 3337 | } |
ef55b745 MS |
3338 | else |
3339 | fprintf(stderr, "DEBUG: Didn't find %s...\n", option->keyword); | |
3340 | } | |
323c5de1 | 3341 | } |
fa73b229 | 3342 | |
323c5de1 | 3343 | if (!have_options || ppdConflicts(ppd)) |
fa73b229 | 3344 | { |
3345 | /* | |
323c5de1 | 3346 | * Show the options to the user... |
fa73b229 | 3347 | */ |
3348 | ||
323c5de1 | 3349 | fputs("DEBUG: Showing options...\n", stderr); |
fa73b229 | 3350 | |
58dc1933 MS |
3351 | /* |
3352 | * Show auto-configure button if supported... | |
3353 | */ | |
3354 | ||
1f6f3dbc MS |
3355 | if (ppd) |
3356 | { | |
3357 | if (ppd->num_filters == 0 || | |
3358 | ((ppdattr = ppdFindAttr(ppd, "cupsCommands", NULL)) != NULL && | |
3359 | ppdattr->value && strstr(ppdattr->value, "AutoConfigure"))) | |
3360 | cgiSetVariable("HAVE_AUTOCONFIGURE", "YES"); | |
ef55b745 | 3361 | else |
1f6f3dbc MS |
3362 | { |
3363 | for (i = 0; i < ppd->num_filters; i ++) | |
3364 | if (!strncmp(ppd->filters[i], "application/vnd.cups-postscript", 31)) | |
3365 | { | |
3366 | cgiSetVariable("HAVE_AUTOCONFIGURE", "YES"); | |
3367 | break; | |
3368 | } | |
3369 | } | |
3370 | } | |
3371 | ||
58dc1933 MS |
3372 | /* |
3373 | * Get the printer attributes... | |
3374 | */ | |
3375 | ||
3376 | request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES); | |
3377 | ||
3378 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, | |
3379 | "localhost", 0, "/printers/%s", printer); | |
3380 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
3381 | NULL, uri); | |
3382 | ||
3383 | response = cupsDoRequest(http, request, "/"); | |
3384 | ||
3385 | /* | |
3386 | * List the groups used as "tabs"... | |
3387 | */ | |
3388 | ||
3389 | i = 0; | |
3390 | ||
3391 | if (ppd) | |
3392 | { | |
3393 | for (group = ppd->groups; | |
3394 | i < ppd->num_groups; | |
3395 | i ++, group ++) | |
3396 | { | |
3397 | cgiSetArray("GROUP_ID", i, group->name); | |
3398 | ||
3399 | if (!strcmp(group->name, "InstallableOptions")) | |
3400 | cgiSetArray("GROUP", i, cgiText(_("Options Installed"))); | |
3401 | else | |
3402 | cgiSetArray("GROUP", i, group->text); | |
3403 | } | |
3404 | } | |
3405 | ||
3406 | if (ippFindAttribute(response, "job-sheets-supported", IPP_TAG_ZERO)) | |
3407 | { | |
3408 | cgiSetArray("GROUP_ID", i, "CUPS_BANNERS"); | |
3409 | cgiSetArray("GROUP", i ++, cgiText(_("Banners"))); | |
3410 | } | |
3411 | ||
3412 | if (ippFindAttribute(response, "printer-error-policy-supported", | |
3413 | IPP_TAG_ZERO) || | |
3414 | ippFindAttribute(response, "printer-op-policy-supported", | |
3415 | IPP_TAG_ZERO)) | |
3416 | { | |
3417 | cgiSetArray("GROUP_ID", i, "CUPS_POLICIES"); | |
3418 | cgiSetArray("GROUP", i ++, cgiText(_("Policies"))); | |
3419 | } | |
3420 | ||
3421 | if ((attr = ippFindAttribute(response, "port-monitor-supported", | |
3422 | IPP_TAG_NAME)) != NULL && attr->num_values > 1) | |
3423 | { | |
3424 | cgiSetArray("GROUP_ID", i, "CUPS_PORT_MONITOR"); | |
3425 | cgiSetArray("GROUP", i, cgiText(_("Port Monitor"))); | |
3426 | } | |
3427 | ||
323c5de1 | 3428 | cgiStartHTML(cgiText(_("Set Printer Options"))); |
3429 | cgiCopyTemplateLang("set-printer-options-header.tmpl"); | |
fa73b229 | 3430 | |
323c5de1 | 3431 | if (ppd) |
3432 | { | |
3433 | ppdLocalize(ppd); | |
f7deaa1a | 3434 | |
323c5de1 | 3435 | if (ppdConflicts(ppd)) |
3436 | { | |
3437 | for (i = ppd->num_groups, k = 0, group = ppd->groups; | |
3438 | i > 0; | |
3439 | i --, group ++) | |
3440 | for (j = group->num_options, option = group->options; | |
3441 | j > 0; | |
3442 | j --, option ++) | |
3443 | if (option->conflicted) | |
3444 | { | |
3445 | cgiSetArray("ckeyword", k, option->keyword); | |
3446 | cgiSetArray("ckeytext", k, option->text); | |
ef55b745 MS |
3447 | |
3448 | for (m = 0; m < option->num_choices; m ++) | |
3449 | { | |
3450 | if (option->choices[m].marked) | |
3451 | { | |
3452 | cgiSetArray("cchoice", k, option->choices[m].text); | |
3453 | break; | |
3454 | } | |
3455 | } | |
3456 | ||
323c5de1 | 3457 | k ++; |
3458 | } | |
f7deaa1a | 3459 | |
323c5de1 | 3460 | cgiCopyTemplateLang("option-conflict.tmpl"); |
3461 | } | |
f7deaa1a | 3462 | |
323c5de1 | 3463 | for (i = ppd->num_groups, group = ppd->groups; |
3464 | i > 0; | |
3465 | i --, group ++) | |
3466 | { | |
7cf5915e MS |
3467 | for (j = group->num_options, option = group->options; |
3468 | j > 0; | |
3469 | j --, option ++) | |
3470 | { | |
3471 | if (!strcmp(option->keyword, "PageRegion")) | |
3472 | continue; | |
3473 | ||
3474 | if (option->num_choices > 1) | |
3475 | break; | |
3476 | } | |
3477 | ||
3478 | if (j == 0) | |
3479 | continue; | |
3480 | ||
58dc1933 MS |
3481 | cgiSetVariable("GROUP_ID", group->name); |
3482 | ||
323c5de1 | 3483 | if (!strcmp(group->name, "InstallableOptions")) |
3484 | cgiSetVariable("GROUP", cgiText(_("Options Installed"))); | |
3485 | else | |
3486 | cgiSetVariable("GROUP", group->text); | |
f7deaa1a | 3487 | |
323c5de1 | 3488 | cgiCopyTemplateLang("option-header.tmpl"); |
ef55b745 | 3489 | |
323c5de1 | 3490 | for (j = group->num_options, option = group->options; |
3491 | j > 0; | |
3492 | j --, option ++) | |
3493 | { | |
7cf5915e | 3494 | if (!strcmp(option->keyword, "PageRegion") || option->num_choices < 2) |
323c5de1 | 3495 | continue; |
ef416fc2 | 3496 | |
323c5de1 | 3497 | cgiSetVariable("KEYWORD", option->keyword); |
3498 | cgiSetVariable("KEYTEXT", option->text); | |
749b1e90 | 3499 | |
323c5de1 | 3500 | if (option->conflicted) |
3501 | cgiSetVariable("CONFLICTED", "1"); | |
3502 | else | |
3503 | cgiSetVariable("CONFLICTED", "0"); | |
3504 | ||
3505 | cgiSetSize("CHOICES", 0); | |
3506 | cgiSetSize("TEXT", 0); | |
3507 | for (k = 0, m = 0; k < option->num_choices; k ++) | |
3508 | { | |
323c5de1 | 3509 | cgiSetArray("CHOICES", m, option->choices[k].choice); |
3510 | cgiSetArray("TEXT", m, option->choices[k].text); | |
ef416fc2 | 3511 | |
323c5de1 | 3512 | m ++; |
ef416fc2 | 3513 | |
323c5de1 | 3514 | if (option->choices[k].marked) |
3515 | cgiSetVariable("DEFCHOICE", option->choices[k].choice); | |
3516 | } | |
ef416fc2 | 3517 | |
749b1e90 MS |
3518 | cgiSetSize("PARAMS", 0); |
3519 | cgiSetSize("PARAMTEXT", 0); | |
3520 | cgiSetSize("PARAMVALUE", 0); | |
3521 | cgiSetSize("INPUTTYPE", 0); | |
3522 | ||
3523 | if ((coption = ppdFindCustomOption(ppd, option->keyword))) | |
3524 | { | |
3525 | const char *units = NULL; /* Units value, if any */ | |
3526 | ||
749b1e90 MS |
3527 | cgiSetVariable("ISCUSTOM", "1"); |
3528 | ||
3529 | for (cparam = ppdFirstCustomParam(coption), m = 0; | |
3530 | cparam; | |
3531 | cparam = ppdNextCustomParam(coption), m ++) | |
3532 | { | |
3533 | if (!strcasecmp(option->keyword, "PageSize") && | |
3534 | strcasecmp(cparam->name, "Width") && | |
3535 | strcasecmp(cparam->name, "Height")) | |
3536 | { | |
3537 | m --; | |
3538 | continue; | |
3539 | } | |
3540 | ||
3541 | cgiSetArray("PARAMS", m, cparam->name); | |
3542 | cgiSetArray("PARAMTEXT", m, cparam->text); | |
3543 | cgiSetArray("INPUTTYPE", m, "text"); | |
3544 | ||
3545 | switch (cparam->type) | |
3546 | { | |
3547 | case PPD_CUSTOM_POINTS : | |
3548 | if (!strncasecmp(option->defchoice, "Custom.", 7)) | |
3549 | { | |
3550 | units = option->defchoice + strlen(option->defchoice) - 2; | |
3551 | ||
3552 | if (strcmp(units, "mm") && strcmp(units, "cm") && | |
3553 | strcmp(units, "in") && strcmp(units, "ft")) | |
3554 | { | |
3555 | if (units[1] == 'm') | |
3556 | units ++; | |
3557 | else | |
3558 | units = "pt"; | |
3559 | } | |
3560 | } | |
3561 | else | |
3562 | units = "pt"; | |
3563 | ||
3564 | if (!strcmp(units, "mm")) | |
3565 | snprintf(value, sizeof(value), "%g", | |
3566 | cparam->current.custom_points / 72.0 * 25.4); | |
3567 | else if (!strcmp(units, "cm")) | |
3568 | snprintf(value, sizeof(value), "%g", | |
3569 | cparam->current.custom_points / 72.0 * 2.54); | |
3570 | else if (!strcmp(units, "in")) | |
3571 | snprintf(value, sizeof(value), "%g", | |
3572 | cparam->current.custom_points / 72.0); | |
3573 | else if (!strcmp(units, "ft")) | |
3574 | snprintf(value, sizeof(value), "%g", | |
3575 | cparam->current.custom_points / 72.0 / 12.0); | |
3576 | else if (!strcmp(units, "m")) | |
3577 | snprintf(value, sizeof(value), "%g", | |
3578 | cparam->current.custom_points / 72.0 * 0.0254); | |
3579 | else | |
3580 | snprintf(value, sizeof(value), "%g", | |
3581 | cparam->current.custom_points); | |
3582 | cgiSetArray("PARAMVALUE", m, value); | |
3583 | break; | |
3584 | ||
3585 | case PPD_CUSTOM_CURVE : | |
3586 | case PPD_CUSTOM_INVCURVE : | |
3587 | case PPD_CUSTOM_REAL : | |
3588 | snprintf(value, sizeof(value), "%g", | |
3589 | cparam->current.custom_real); | |
3590 | cgiSetArray("PARAMVALUE", m, value); | |
3591 | break; | |
3592 | ||
3593 | case PPD_CUSTOM_INT: | |
3594 | snprintf(value, sizeof(value), "%d", | |
3595 | cparam->current.custom_int); | |
3596 | cgiSetArray("PARAMVALUE", m, value); | |
3597 | break; | |
3598 | ||
3599 | case PPD_CUSTOM_PASSCODE: | |
3600 | case PPD_CUSTOM_PASSWORD: | |
3601 | if (cparam->current.custom_password) | |
3602 | cgiSetArray("PARAMVALUE", m, | |
3603 | cparam->current.custom_password); | |
3604 | else | |
3605 | cgiSetArray("PARAMVALUE", m, ""); | |
3606 | cgiSetArray("INPUTTYPE", m, "password"); | |
3607 | break; | |
3608 | ||
3609 | case PPD_CUSTOM_STRING: | |
3610 | if (cparam->current.custom_string) | |
3611 | cgiSetArray("PARAMVALUE", m, | |
3612 | cparam->current.custom_string); | |
3613 | else | |
3614 | cgiSetArray("PARAMVALUE", m, ""); | |
3615 | break; | |
3616 | } | |
3617 | } | |
3618 | ||
3619 | if (units) | |
3620 | { | |
3621 | cgiSetArray("PARAMS", m, "Units"); | |
3622 | cgiSetArray("PARAMTEXT", m, cgiText(_("Units"))); | |
3623 | cgiSetArray("PARAMVALUE", m, units); | |
3624 | } | |
3625 | } | |
3626 | else | |
3627 | cgiSetVariable("ISCUSTOM", "0"); | |
3628 | ||
323c5de1 | 3629 | switch (option->ui) |
3630 | { | |
3631 | case PPD_UI_BOOLEAN : | |
3632 | cgiCopyTemplateLang("option-boolean.tmpl"); | |
3633 | break; | |
3634 | case PPD_UI_PICKONE : | |
3635 | cgiCopyTemplateLang("option-pickone.tmpl"); | |
3636 | break; | |
3637 | case PPD_UI_PICKMANY : | |
3638 | cgiCopyTemplateLang("option-pickmany.tmpl"); | |
3639 | break; | |
3640 | } | |
3641 | } | |
ef416fc2 | 3642 | |
323c5de1 | 3643 | cgiCopyTemplateLang("option-trailer.tmpl"); |
3644 | } | |
3645 | } | |
ef416fc2 | 3646 | |
58dc1933 MS |
3647 | if ((attr = ippFindAttribute(response, "job-sheets-supported", |
3648 | IPP_TAG_ZERO)) != NULL) | |
3649 | { | |
3650 | /* | |
3651 | * Add the job sheets options... | |
3652 | */ | |
fa73b229 | 3653 | |
58dc1933 MS |
3654 | cgiSetVariable("GROUP_ID", "CUPS_BANNERS"); |
3655 | cgiSetVariable("GROUP", cgiText(_("Banners"))); | |
3656 | cgiCopyTemplateLang("option-header.tmpl"); | |
ef416fc2 | 3657 | |
58dc1933 MS |
3658 | cgiSetSize("CHOICES", attr->num_values); |
3659 | cgiSetSize("TEXT", attr->num_values); | |
3660 | for (k = 0; k < attr->num_values; k ++) | |
3661 | { | |
3662 | cgiSetArray("CHOICES", k, attr->values[k].string.text); | |
3663 | cgiSetArray("TEXT", k, attr->values[k].string.text); | |
3664 | } | |
ef416fc2 | 3665 | |
58dc1933 | 3666 | attr = ippFindAttribute(response, "job-sheets-default", IPP_TAG_ZERO); |
ef416fc2 | 3667 | |
58dc1933 MS |
3668 | cgiSetVariable("KEYWORD", "job_sheets_start"); |
3669 | cgiSetVariable("KEYTEXT", cgiText(_("Starting Banner"))); | |
3670 | cgiSetVariable("DEFCHOICE", attr != NULL ? | |
3671 | attr->values[0].string.text : ""); | |
3672 | ||
3673 | cgiCopyTemplateLang("option-pickone.tmpl"); | |
3674 | ||
3675 | cgiSetVariable("KEYWORD", "job_sheets_end"); | |
3676 | cgiSetVariable("KEYTEXT", cgiText(_("Ending Banner"))); | |
3677 | cgiSetVariable("DEFCHOICE", attr != NULL && attr->num_values > 1 ? | |
3678 | attr->values[1].string.text : ""); | |
3679 | ||
3680 | cgiCopyTemplateLang("option-pickone.tmpl"); | |
3681 | ||
3682 | cgiCopyTemplateLang("option-trailer.tmpl"); | |
3683 | } | |
3684 | ||
3685 | if (ippFindAttribute(response, "printer-error-policy-supported", | |
3686 | IPP_TAG_ZERO) || | |
3687 | ippFindAttribute(response, "printer-op-policy-supported", | |
3688 | IPP_TAG_ZERO)) | |
323c5de1 | 3689 | { |
58dc1933 MS |
3690 | /* |
3691 | * Add the error and operation policy options... | |
3692 | */ | |
ef416fc2 | 3693 | |
58dc1933 MS |
3694 | cgiSetVariable("GROUP_ID", "CUPS_POLICIES"); |
3695 | cgiSetVariable("GROUP", cgiText(_("Policies"))); | |
3696 | cgiCopyTemplateLang("option-header.tmpl"); | |
3697 | ||
3698 | /* | |
3699 | * Error policy... | |
3700 | */ | |
ef416fc2 | 3701 | |
58dc1933 MS |
3702 | attr = ippFindAttribute(response, "printer-error-policy-supported", |
3703 | IPP_TAG_ZERO); | |
3704 | ||
3705 | if (attr) | |
3706 | { | |
323c5de1 | 3707 | cgiSetSize("CHOICES", attr->num_values); |
3708 | cgiSetSize("TEXT", attr->num_values); | |
3709 | for (k = 0; k < attr->num_values; k ++) | |
3710 | { | |
3711 | cgiSetArray("CHOICES", k, attr->values[k].string.text); | |
3712 | cgiSetArray("TEXT", k, attr->values[k].string.text); | |
3713 | } | |
ef416fc2 | 3714 | |
58dc1933 MS |
3715 | attr = ippFindAttribute(response, "printer-error-policy", |
3716 | IPP_TAG_ZERO); | |
ef416fc2 | 3717 | |
58dc1933 MS |
3718 | cgiSetVariable("KEYWORD", "printer_error_policy"); |
3719 | cgiSetVariable("KEYTEXT", cgiText(_("Error Policy"))); | |
3720 | cgiSetVariable("DEFCHOICE", attr == NULL ? | |
3721 | "" : attr->values[0].string.text); | |
323c5de1 | 3722 | } |
ef416fc2 | 3723 | |
58dc1933 | 3724 | cgiCopyTemplateLang("option-pickone.tmpl"); |
ef416fc2 | 3725 | |
58dc1933 MS |
3726 | /* |
3727 | * Operation policy... | |
3728 | */ | |
ef416fc2 | 3729 | |
58dc1933 MS |
3730 | attr = ippFindAttribute(response, "printer-op-policy-supported", |
3731 | IPP_TAG_ZERO); | |
323c5de1 | 3732 | |
58dc1933 MS |
3733 | if (attr) |
3734 | { | |
3735 | cgiSetSize("CHOICES", attr->num_values); | |
3736 | cgiSetSize("TEXT", attr->num_values); | |
3737 | for (k = 0; k < attr->num_values; k ++) | |
323c5de1 | 3738 | { |
58dc1933 MS |
3739 | cgiSetArray("CHOICES", k, attr->values[k].string.text); |
3740 | cgiSetArray("TEXT", k, attr->values[k].string.text); | |
3741 | } | |
ef416fc2 | 3742 | |
58dc1933 | 3743 | attr = ippFindAttribute(response, "printer-op-policy", IPP_TAG_ZERO); |
ef416fc2 | 3744 | |
58dc1933 MS |
3745 | cgiSetVariable("KEYWORD", "printer_op_policy"); |
3746 | cgiSetVariable("KEYTEXT", cgiText(_("Operation Policy"))); | |
3747 | cgiSetVariable("DEFCHOICE", attr == NULL ? | |
3748 | "" : attr->values[0].string.text); | |
fa73b229 | 3749 | |
323c5de1 | 3750 | cgiCopyTemplateLang("option-pickone.tmpl"); |
323c5de1 | 3751 | } |
ef416fc2 | 3752 | |
58dc1933 | 3753 | cgiCopyTemplateLang("option-trailer.tmpl"); |
ef416fc2 | 3754 | } |
ef416fc2 | 3755 | |
ef416fc2 | 3756 | /* |
323c5de1 | 3757 | * Binary protocol support... |
ef416fc2 | 3758 | */ |
3759 | ||
58dc1933 MS |
3760 | if ((attr = ippFindAttribute(response, "port-monitor-supported", |
3761 | IPP_TAG_NAME)) != NULL && attr->num_values > 1) | |
ef416fc2 | 3762 | { |
58dc1933 MS |
3763 | cgiSetVariable("GROUP_ID", "CUPS_PORT_MONITOR"); |
3764 | cgiSetVariable("GROUP", cgiText(_("Port Monitor"))); | |
ef416fc2 | 3765 | |
58dc1933 MS |
3766 | cgiSetSize("CHOICES", attr->num_values); |
3767 | cgiSetSize("TEXT", attr->num_values); | |
ef416fc2 | 3768 | |
58dc1933 | 3769 | for (i = 0; i < attr->num_values; i ++) |
ef416fc2 | 3770 | { |
58dc1933 MS |
3771 | cgiSetArray("CHOICES", i, attr->values[i].string.text); |
3772 | cgiSetArray("TEXT", i, attr->values[i].string.text); | |
ef416fc2 | 3773 | } |
3774 | ||
58dc1933 MS |
3775 | attr = ippFindAttribute(response, "port-monitor", IPP_TAG_NAME); |
3776 | cgiSetVariable("KEYWORD", "port_monitor"); | |
3777 | cgiSetVariable("KEYTEXT", cgiText(_("Port Monitor"))); | |
3778 | cgiSetVariable("DEFCHOICE", attr ? attr->values[0].string.text : "none"); | |
ef416fc2 | 3779 | |
58dc1933 | 3780 | cgiCopyTemplateLang("option-header.tmpl"); |
323c5de1 | 3781 | cgiCopyTemplateLang("option-pickone.tmpl"); |
323c5de1 | 3782 | cgiCopyTemplateLang("option-trailer.tmpl"); |
ef416fc2 | 3783 | } |
3784 | ||
323c5de1 | 3785 | cgiCopyTemplateLang("set-printer-options-trailer.tmpl"); |
3786 | cgiEndHTML(); | |
58dc1933 MS |
3787 | |
3788 | ippDelete(response); | |
323c5de1 | 3789 | } |
3790 | else | |
3791 | { | |
ef416fc2 | 3792 | /* |
323c5de1 | 3793 | * Set default options... |
ef416fc2 | 3794 | */ |
3795 | ||
323c5de1 | 3796 | fputs("DEBUG: Setting options...\n", stderr); |
ef416fc2 | 3797 | |
323c5de1 | 3798 | if (filename) |
ef416fc2 | 3799 | { |
323c5de1 | 3800 | out = cupsTempFile2(tempfile, sizeof(tempfile)); |
3801 | in = cupsFileOpen(filename, "r"); | |
ef416fc2 | 3802 | |
323c5de1 | 3803 | if (!in || !out) |
ef416fc2 | 3804 | { |
323c5de1 | 3805 | cgiSetVariable("ERROR", strerror(errno)); |
3806 | cgiStartHTML(cgiText(_("Set Printer Options"))); | |
3807 | cgiCopyTemplateLang("error.tmpl"); | |
3808 | cgiEndHTML(); | |
ef416fc2 | 3809 | |
323c5de1 | 3810 | if (in) |
3811 | cupsFileClose(in); | |
ef416fc2 | 3812 | |
323c5de1 | 3813 | if (out) |
ef416fc2 | 3814 | { |
323c5de1 | 3815 | cupsFileClose(out); |
3816 | unlink(tempfile); | |
3817 | } | |
ef416fc2 | 3818 | |
323c5de1 | 3819 | unlink(filename); |
3820 | return; | |
3821 | } | |
ef416fc2 | 3822 | |
323c5de1 | 3823 | while (cupsFileGets(in, line, sizeof(line))) |
3824 | { | |
58dc1933 | 3825 | if (!strncmp(line, "*cupsProtocol:", 14)) |
323c5de1 | 3826 | continue; |
3827 | else if (strncmp(line, "*Default", 8)) | |
3828 | cupsFilePrintf(out, "%s\n", line); | |
ef416fc2 | 3829 | else |
3830 | { | |
3831 | /* | |
323c5de1 | 3832 | * Get default option name... |
ef416fc2 | 3833 | */ |
3834 | ||
323c5de1 | 3835 | strlcpy(keyword, line + 8, sizeof(keyword)); |
3836 | ||
3837 | for (keyptr = keyword; *keyptr; keyptr ++) | |
3838 | if (*keyptr == ':' || isspace(*keyptr & 255)) | |
ef416fc2 | 3839 | break; |
ef416fc2 | 3840 | |
323c5de1 | 3841 | *keyptr = '\0'; |
ef416fc2 | 3842 | |
323c5de1 | 3843 | if (!strcmp(keyword, "PageRegion") || |
3844 | !strcmp(keyword, "PaperDimension") || | |
3845 | !strcmp(keyword, "ImageableArea")) | |
749b1e90 | 3846 | var = get_option_value(ppd, "PageSize", value, sizeof(value)); |
323c5de1 | 3847 | else |
749b1e90 | 3848 | var = get_option_value(ppd, keyword, value, sizeof(value)); |
ef416fc2 | 3849 | |
749b1e90 | 3850 | if (!var) |
323c5de1 | 3851 | cupsFilePrintf(out, "%s\n", line); |
749b1e90 MS |
3852 | else |
3853 | cupsFilePrintf(out, "*Default%s: %s\n", keyword, var); | |
323c5de1 | 3854 | } |
3855 | } | |
ef416fc2 | 3856 | |
323c5de1 | 3857 | cupsFileClose(in); |
3858 | cupsFileClose(out); | |
3859 | } | |
3860 | else | |
3861 | { | |
3862 | /* | |
3863 | * Make sure temporary filename is cleared when there is no PPD... | |
3864 | */ | |
ef416fc2 | 3865 | |
323c5de1 | 3866 | tempfile[0] = '\0'; |
ef416fc2 | 3867 | } |
3868 | ||
323c5de1 | 3869 | /* |
3870 | * Build a CUPS_ADD_MODIFY_CLASS/PRINTER request, which requires the | |
3871 | * following attributes: | |
3872 | * | |
3873 | * attributes-charset | |
3874 | * attributes-natural-language | |
3875 | * printer-uri | |
3876 | * job-sheets-default | |
3877 | * printer-error-policy | |
3878 | * printer-op-policy | |
3879 | * [ppd file] | |
3880 | */ | |
3881 | ||
3882 | request = ippNewRequest(is_class ? CUPS_ADD_MODIFY_CLASS : | |
3883 | CUPS_ADD_MODIFY_PRINTER); | |
3884 | ||
3885 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
3886 | NULL, uri); | |
3887 | ||
3888 | attr = ippAddStrings(request, IPP_TAG_PRINTER, IPP_TAG_NAME, | |
3889 | "job-sheets-default", 2, NULL, NULL); | |
3890 | attr->values[0].string.text = _cupsStrAlloc(cgiGetVariable("job_sheets_start")); | |
3891 | attr->values[1].string.text = _cupsStrAlloc(cgiGetVariable("job_sheets_end")); | |
3892 | ||
3893 | if ((var = cgiGetVariable("printer_error_policy")) != NULL) | |
1f0275e3 MS |
3894 | ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME, |
3895 | "printer-error-policy", NULL, var); | |
323c5de1 | 3896 | |
3897 | if ((var = cgiGetVariable("printer_op_policy")) != NULL) | |
1f0275e3 MS |
3898 | ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME, |
3899 | "printer-op-policy", NULL, var); | |
323c5de1 | 3900 | |
58dc1933 MS |
3901 | if ((var = cgiGetVariable("port_monitor")) != NULL) |
3902 | ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME, | |
3903 | "port-monitor", NULL, var); | |
3904 | ||
ef416fc2 | 3905 | /* |
3906 | * Do the request and get back a response... | |
3907 | */ | |
3908 | ||
323c5de1 | 3909 | if (filename) |
3910 | ippDelete(cupsDoFileRequest(http, request, "/admin/", tempfile)); | |
3911 | else | |
3912 | ippDelete(cupsDoRequest(http, request, "/admin/")); | |
ef416fc2 | 3913 | |
355e94dc MS |
3914 | if (cupsLastError() == IPP_NOT_AUTHORIZED) |
3915 | { | |
3916 | puts("Status: 401\n"); | |
3917 | exit(0); | |
3918 | } | |
3919 | else if (cupsLastError() > IPP_OK_CONFLICT) | |
ef416fc2 | 3920 | { |
323c5de1 | 3921 | cgiStartHTML(title); |
3922 | cgiShowIPPError(_("Unable to set options:")); | |
ef416fc2 | 3923 | } |
3924 | else | |
3925 | { | |
3926 | /* | |
3927 | * Redirect successful updates back to the printer page... | |
3928 | */ | |
3929 | ||
323c5de1 | 3930 | char refresh[1024]; /* Refresh URL */ |
ef416fc2 | 3931 | |
fa73b229 | 3932 | |
323c5de1 | 3933 | cgiFormEncode(uri, printer, sizeof(uri)); |
3934 | snprintf(refresh, sizeof(refresh), "5;URL=/admin/?OP=redirect&URL=/%s/%s", | |
3935 | is_class ? "classes" : "printers", uri); | |
ef416fc2 | 3936 | cgiSetVariable("refresh_page", refresh); |
3937 | ||
323c5de1 | 3938 | cgiStartHTML(title); |
ef416fc2 | 3939 | |
323c5de1 | 3940 | cgiCopyTemplateLang("printer-configured.tmpl"); |
ef416fc2 | 3941 | } |
3942 | ||
3943 | cgiEndHTML(); | |
323c5de1 | 3944 | |
3945 | if (filename) | |
3946 | unlink(tempfile); | |
ef416fc2 | 3947 | } |
323c5de1 | 3948 | |
3949 | if (filename) | |
3950 | unlink(filename); | |
ef416fc2 | 3951 | } |
3952 | ||
3953 | ||
3954 | /* | |
749b1e90 | 3955 | * 'do_set_sharing()' - Set printer-is-shared value. |
ef416fc2 | 3956 | */ |
3957 | ||
3958 | static void | |
fa73b229 | 3959 | do_set_sharing(http_t *http) /* I - HTTP connection */ |
ef416fc2 | 3960 | { |
3961 | ipp_t *request, /* IPP request */ | |
3962 | *response; /* IPP response */ | |
3963 | char uri[HTTP_MAX_URI]; /* Printer URI */ | |
3964 | const char *printer, /* Printer name */ | |
fa73b229 | 3965 | *is_class, /* Is a class? */ |
ef416fc2 | 3966 | *shared; /* Sharing value */ |
ef416fc2 | 3967 | |
3968 | ||
fa73b229 | 3969 | is_class = cgiGetVariable("IS_CLASS"); |
3970 | printer = cgiGetVariable("PRINTER_NAME"); | |
3971 | shared = cgiGetVariable("SHARED"); | |
ef416fc2 | 3972 | |
fa73b229 | 3973 | if (!printer || !shared) |
ef416fc2 | 3974 | { |
4d301e69 | 3975 | cgiSetVariable("ERROR", cgiText(_("Missing form variable"))); |
fa73b229 | 3976 | cgiStartHTML(cgiText(_("Set Publishing"))); |
ef416fc2 | 3977 | cgiCopyTemplateLang("error.tmpl"); |
3978 | cgiEndHTML(); | |
3979 | return; | |
3980 | } | |
3981 | ||
3982 | /* | |
fa73b229 | 3983 | * Build a CUPS-Add-Printer/CUPS-Add-Class request, which requires the |
3984 | * following attributes: | |
ef416fc2 | 3985 | * |
3986 | * attributes-charset | |
3987 | * attributes-natural-language | |
3988 | * printer-uri | |
3989 | * printer-is-shared | |
3990 | */ | |
3991 | ||
fa73b229 | 3992 | request = ippNewRequest(is_class ? CUPS_ADD_CLASS : CUPS_ADD_PRINTER); |
ef416fc2 | 3993 | |
a4d04587 | 3994 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, |
3995 | "localhost", 0, is_class ? "/classes/%s" : "/printers/%s", | |
3996 | printer); | |
ef416fc2 | 3997 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", |
3998 | NULL, uri); | |
3999 | ||
4000 | ippAddBoolean(request, IPP_TAG_OPERATION, "printer-is-shared", atoi(shared)); | |
4001 | ||
4002 | /* | |
4003 | * Do the request and get back a response... | |
4004 | */ | |
4005 | ||
4006 | if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) | |
4007 | { | |
ef416fc2 | 4008 | cgiSetIPPVars(response, NULL, NULL, NULL, 0); |
4009 | ||
4010 | ippDelete(response); | |
4011 | } | |
ef416fc2 | 4012 | |
355e94dc MS |
4013 | if (cupsLastError() == IPP_NOT_AUTHORIZED) |
4014 | { | |
4015 | puts("Status: 401\n"); | |
4016 | exit(0); | |
4017 | } | |
4018 | else if (cupsLastError() > IPP_OK_CONFLICT) | |
ef416fc2 | 4019 | { |
fa73b229 | 4020 | cgiStartHTML(cgiText(_("Set Publishing"))); |
4021 | cgiShowIPPError(_("Unable to change printer-is-shared attribute:")); | |
ef416fc2 | 4022 | } |
4023 | else | |
4024 | { | |
4025 | /* | |
4026 | * Redirect successful updates back to the printer page... | |
4027 | */ | |
4028 | ||
fa73b229 | 4029 | char url[1024], /* Printer/class URL */ |
4030 | refresh[1024]; /* Refresh URL */ | |
ef416fc2 | 4031 | |
ef416fc2 | 4032 | |
fa73b229 | 4033 | cgiRewriteURL(uri, url, sizeof(url), NULL); |
4034 | cgiFormEncode(uri, url, sizeof(uri)); | |
f301802f | 4035 | snprintf(refresh, sizeof(refresh), "5;URL=/admin/?OP=redirect&URL=%s", uri); |
fa73b229 | 4036 | cgiSetVariable("refresh_page", refresh); |
ef416fc2 | 4037 | |
fa73b229 | 4038 | cgiStartHTML(cgiText(_("Set Publishing"))); |
4039 | cgiCopyTemplateLang(is_class ? "class-modified.tmpl" : | |
4040 | "printer-modified.tmpl"); | |
ef416fc2 | 4041 | } |
4042 | ||
4043 | cgiEndHTML(); | |
4044 | } | |
4045 | ||
4046 | ||
749b1e90 MS |
4047 | /* |
4048 | * 'get_option_value()' - Return the value of an option. | |
4049 | * | |
4050 | * This function also handles generation of custom option values. | |
4051 | */ | |
4052 | ||
4053 | static char * /* O - Value string or NULL on error */ | |
4054 | get_option_value( | |
4055 | ppd_file_t *ppd, /* I - PPD file */ | |
4056 | const char *name, /* I - Option name */ | |
4057 | char *buffer, /* I - String buffer */ | |
4058 | size_t bufsize) /* I - Size of buffer */ | |
4059 | { | |
4060 | char *bufptr, /* Pointer into buffer */ | |
4061 | *bufend; /* End of buffer */ | |
4062 | ppd_coption_t *coption; /* Custom option */ | |
4063 | ppd_cparam_t *cparam; /* Current custom parameter */ | |
4064 | char keyword[256]; /* Parameter name */ | |
4065 | const char *val, /* Parameter value */ | |
4066 | *uval; /* Units value */ | |
4067 | long integer; /* Integer value */ | |
4068 | double number, /* Number value */ | |
4069 | number_points; /* Number in points */ | |
4070 | ||
4071 | ||
4072 | /* | |
4073 | * See if we have a custom option choice... | |
4074 | */ | |
4075 | ||
4076 | if ((val = cgiGetVariable(name)) == NULL) | |
4077 | { | |
4078 | /* | |
4079 | * Option not found! | |
4080 | */ | |
4081 | ||
4082 | return (NULL); | |
4083 | } | |
4084 | else if (strcasecmp(val, "Custom") || | |
4085 | (coption = ppdFindCustomOption(ppd, name)) == NULL) | |
4086 | { | |
4087 | /* | |
4088 | * Not a custom choice... | |
4089 | */ | |
4090 | ||
4091 | strlcpy(buffer, val, bufsize); | |
4092 | return (buffer); | |
4093 | } | |
4094 | ||
4095 | /* | |
4096 | * OK, we have a custom option choice, format it... | |
4097 | */ | |
4098 | ||
4099 | *buffer = '\0'; | |
4100 | ||
4101 | if (!strcmp(coption->keyword, "PageSize")) | |
4102 | { | |
4103 | const char *lval; /* Length string value */ | |
4104 | double width, /* Width value */ | |
4105 | width_points, /* Width in points */ | |
4106 | length, /* Length value */ | |
4107 | length_points; /* Length in points */ | |
4108 | ||
4109 | ||
4110 | val = cgiGetVariable("PageSize.Width"); | |
4111 | lval = cgiGetVariable("PageSize.Height"); | |
4112 | uval = cgiGetVariable("PageSize.Units"); | |
4113 | ||
4114 | if (!val || !lval || !uval || | |
4115 | (width = strtod(val, NULL)) == 0.0 || | |
4116 | (length = strtod(lval, NULL)) == 0.0 || | |
4117 | (strcmp(uval, "pt") && strcmp(uval, "in") && strcmp(uval, "ft") && | |
4118 | strcmp(uval, "cm") && strcmp(uval, "mm") && strcmp(uval, "m"))) | |
4119 | return (NULL); | |
4120 | ||
4121 | width_points = get_points(width, uval); | |
4122 | length_points = get_points(length, uval); | |
4123 | ||
4124 | if (width_points < ppd->custom_min[0] || | |
4125 | width_points > ppd->custom_max[0] || | |
4126 | length_points < ppd->custom_min[1] || | |
4127 | length_points > ppd->custom_max[1]) | |
4128 | return (NULL); | |
4129 | ||
4130 | snprintf(buffer, bufsize, "Custom.%gx%g%s", width, length, uval); | |
4131 | } | |
ef55b745 | 4132 | else if (cupsArrayCount(coption->params) == 1) |
749b1e90 MS |
4133 | { |
4134 | cparam = ppdFirstCustomParam(coption); | |
4135 | snprintf(keyword, sizeof(keyword), "%s.%s", coption->keyword, cparam->name); | |
4136 | ||
4137 | if ((val = cgiGetVariable(keyword)) == NULL) | |
4138 | return (NULL); | |
4139 | ||
4140 | switch (cparam->type) | |
4141 | { | |
4142 | case PPD_CUSTOM_CURVE : | |
4143 | case PPD_CUSTOM_INVCURVE : | |
4144 | case PPD_CUSTOM_REAL : | |
4145 | if ((number = strtod(val, NULL)) == 0.0 || | |
4146 | number < cparam->minimum.custom_real || | |
4147 | number > cparam->maximum.custom_real) | |
4148 | return (NULL); | |
4149 | ||
4150 | snprintf(buffer, bufsize, "Custom.%g", number); | |
4151 | break; | |
4152 | ||
4153 | case PPD_CUSTOM_INT : | |
4154 | if (!*val || (integer = strtol(val, NULL, 10)) == LONG_MIN || | |
4155 | integer == LONG_MAX || | |
4156 | integer < cparam->minimum.custom_int || | |
4157 | integer > cparam->maximum.custom_int) | |
4158 | return (NULL); | |
4159 | ||
4160 | snprintf(buffer, bufsize, "Custom.%ld", integer); | |
4161 | break; | |
4162 | ||
4163 | case PPD_CUSTOM_POINTS : | |
4164 | snprintf(keyword, sizeof(keyword), "%s.Units", coption->keyword); | |
4165 | ||
4166 | if ((number = strtod(val, NULL)) == 0.0 || | |
4167 | (uval = cgiGetVariable(keyword)) == NULL || | |
4168 | (strcmp(uval, "pt") && strcmp(uval, "in") && strcmp(uval, "ft") && | |
4169 | strcmp(uval, "cm") && strcmp(uval, "mm") && strcmp(uval, "m"))) | |
4170 | return (NULL); | |
4171 | ||
4172 | number_points = get_points(number, uval); | |
4173 | if (number_points < cparam->minimum.custom_points || | |
4174 | number_points > cparam->maximum.custom_points) | |
4175 | return (NULL); | |
4176 | ||
4177 | snprintf(buffer, bufsize, "Custom.%g%s", number, uval); | |
4178 | break; | |
4179 | ||
4180 | case PPD_CUSTOM_PASSCODE : | |
4181 | for (uval = val; *uval; uval ++) | |
4182 | if (!isdigit(*uval & 255)) | |
4183 | return (NULL); | |
4184 | ||
4185 | case PPD_CUSTOM_PASSWORD : | |
4186 | case PPD_CUSTOM_STRING : | |
4187 | integer = (long)strlen(val); | |
4188 | if (integer < cparam->minimum.custom_string || | |
4189 | integer > cparam->maximum.custom_string) | |
4190 | return (NULL); | |
4191 | ||
4192 | snprintf(buffer, bufsize, "Custom.%s", val); | |
4193 | break; | |
4194 | } | |
4195 | } | |
4196 | else | |
4197 | { | |
4198 | const char *prefix = "{"; /* Prefix string */ | |
4199 | ||
4200 | ||
4201 | bufptr = buffer; | |
4202 | bufend = buffer + bufsize; | |
4203 | ||
4204 | for (cparam = ppdFirstCustomParam(coption); | |
4205 | cparam; | |
4206 | cparam = ppdNextCustomParam(coption)) | |
4207 | { | |
4208 | snprintf(keyword, sizeof(keyword), "%s.%s", coption->keyword, | |
4209 | cparam->name); | |
4210 | ||
4211 | if ((val = cgiGetVariable(keyword)) == NULL) | |
4212 | return (NULL); | |
4213 | ||
4214 | snprintf(bufptr, bufend - bufptr, "%s%s=", prefix, cparam->name); | |
4215 | bufptr += strlen(bufptr); | |
4216 | prefix = " "; | |
4217 | ||
4218 | switch (cparam->type) | |
4219 | { | |
4220 | case PPD_CUSTOM_CURVE : | |
4221 | case PPD_CUSTOM_INVCURVE : | |
4222 | case PPD_CUSTOM_REAL : | |
4223 | if ((number = strtod(val, NULL)) == 0.0 || | |
4224 | number < cparam->minimum.custom_real || | |
4225 | number > cparam->maximum.custom_real) | |
4226 | return (NULL); | |
4227 | ||
4228 | snprintf(bufptr, bufend - bufptr, "%g", number); | |
4229 | break; | |
4230 | ||
4231 | case PPD_CUSTOM_INT : | |
4232 | if (!*val || (integer = strtol(val, NULL, 10)) == LONG_MIN || | |
4233 | integer == LONG_MAX || | |
4234 | integer < cparam->minimum.custom_int || | |
4235 | integer > cparam->maximum.custom_int) | |
4236 | return (NULL); | |
4237 | ||
4238 | snprintf(bufptr, bufend - bufptr, "%ld", integer); | |
4239 | break; | |
4240 | ||
4241 | case PPD_CUSTOM_POINTS : | |
4242 | snprintf(keyword, sizeof(keyword), "%s.Units", coption->keyword); | |
4243 | ||
4244 | if ((number = strtod(val, NULL)) == 0.0 || | |
4245 | (uval = cgiGetVariable(keyword)) == NULL || | |
4246 | (strcmp(uval, "pt") && strcmp(uval, "in") && | |
4247 | strcmp(uval, "ft") && strcmp(uval, "cm") && | |
4248 | strcmp(uval, "mm") && strcmp(uval, "m"))) | |
4249 | return (NULL); | |
4250 | ||
4251 | number_points = get_points(number, uval); | |
4252 | if (number_points < cparam->minimum.custom_points || | |
4253 | number_points > cparam->maximum.custom_points) | |
4254 | return (NULL); | |
4255 | ||
4256 | snprintf(bufptr, bufend - bufptr, "%g%s", number, uval); | |
4257 | break; | |
4258 | ||
4259 | case PPD_CUSTOM_PASSCODE : | |
4260 | for (uval = val; *uval; uval ++) | |
4261 | if (!isdigit(*uval & 255)) | |
4262 | return (NULL); | |
4263 | ||
4264 | case PPD_CUSTOM_PASSWORD : | |
4265 | case PPD_CUSTOM_STRING : | |
4266 | integer = (long)strlen(val); | |
4267 | if (integer < cparam->minimum.custom_string || | |
4268 | integer > cparam->maximum.custom_string) | |
4269 | return (NULL); | |
4270 | ||
4271 | if ((bufptr + 2) > bufend) | |
4272 | return (NULL); | |
4273 | ||
4274 | bufend --; | |
4275 | *bufptr++ = '\"'; | |
4276 | ||
4277 | while (*val && bufptr < bufend) | |
4278 | { | |
4279 | if (*val == '\\' || *val == '\"') | |
4280 | { | |
4281 | if ((bufptr + 1) >= bufend) | |
4282 | return (NULL); | |
4283 | ||
4284 | *bufptr++ = '\\'; | |
4285 | } | |
4286 | ||
4287 | *bufptr++ = *val++; | |
4288 | } | |
4289 | ||
4290 | if (bufptr >= bufend) | |
4291 | return (NULL); | |
4292 | ||
4293 | *bufptr++ = '\"'; | |
4294 | *bufptr = '\0'; | |
4295 | bufend ++; | |
4296 | break; | |
4297 | } | |
4298 | ||
4299 | bufptr += strlen(bufptr); | |
4300 | } | |
4301 | ||
4302 | if (bufptr == buffer || (bufend - bufptr) < 2) | |
4303 | return (NULL); | |
4304 | ||
4305 | strcpy(bufptr, "}"); | |
4306 | } | |
4307 | ||
4308 | return (buffer); | |
4309 | } | |
4310 | ||
4311 | ||
4312 | /* | |
4313 | * 'get_points()' - Get a value in points. | |
4314 | */ | |
4315 | ||
4316 | static double /* O - Number in points */ | |
4317 | get_points(double number, /* I - Original number */ | |
4318 | const char *uval) /* I - Units */ | |
4319 | { | |
4320 | if (!strcmp(uval, "mm")) /* Millimeters */ | |
4321 | return (number * 72.0 / 25.4); | |
4322 | else if (!strcmp(uval, "cm")) /* Centimeters */ | |
4323 | return (number * 72.0 / 2.54); | |
4324 | else if (!strcmp(uval, "in")) /* Inches */ | |
4325 | return (number * 72.0); | |
4326 | else if (!strcmp(uval, "ft")) /* Feet */ | |
4327 | return (number * 72.0 * 12.0); | |
4328 | else if (!strcmp(uval, "m")) /* Meters */ | |
4329 | return (number * 72.0 / 0.0254); | |
4330 | else /* Points */ | |
4331 | return (number); | |
4332 | } | |
4333 | ||
4334 | ||
ef416fc2 | 4335 | /* |
58dc1933 | 4336 | * End of "$Id: admin.c 8029 2008-10-08 21:07:45Z mike $". |
ef416fc2 | 4337 | */ |