]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * "$Id: printers.c 177 2006-06-21 00:20:03Z jlovell $" | |
3 | * | |
4 | * Printer status CGI for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 1997-2006 by Easy Software Products. | |
7 | * | |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
26 | * main() - Main entry for CGI. | |
27 | * print_command() - Send a print command to the printer. | |
28 | * show_all_printers() - Show all printers... | |
29 | * show_printer() - Show a single printer. | |
30 | */ | |
31 | ||
32 | /* | |
33 | * Include necessary headers... | |
34 | */ | |
35 | ||
36 | #include "cgi-private.h" | |
37 | #include <errno.h> | |
38 | ||
39 | ||
40 | /* | |
41 | * Local functions... | |
42 | */ | |
43 | ||
44 | void print_command(http_t *http, const char *printer, const char *command); | |
45 | void show_all_printers(http_t *http, const char *username); | |
46 | void show_printer(http_t *http, const char *printer); | |
47 | ||
48 | ||
49 | /* | |
50 | * 'main()' - Main entry for CGI. | |
51 | */ | |
52 | ||
53 | int /* O - Exit status */ | |
54 | main(int argc, /* I - Number of command-line arguments */ | |
55 | char *argv[]) /* I - Command-line arguments */ | |
56 | { | |
57 | const char *printer; /* Printer name */ | |
58 | const char *user; /* Username */ | |
59 | http_t *http; /* Connection to the server */ | |
60 | ipp_t *request, /* IPP request */ | |
61 | *response; /* IPP response */ | |
62 | ipp_attribute_t *attr; /* IPP attribute */ | |
63 | const char *op; /* Operation to perform, if any */ | |
64 | static const char *def_attrs[] = /* Attributes for default printer */ | |
65 | { | |
66 | "printer-name", | |
67 | "printer-uri-supported" | |
68 | }; | |
69 | ||
70 | ||
71 | /* | |
72 | * Get any form variables... | |
73 | */ | |
74 | ||
75 | cgiInitialize(); | |
76 | ||
77 | op = cgiGetVariable("OP"); | |
78 | ||
79 | /* | |
80 | * Set the web interface section... | |
81 | */ | |
82 | ||
83 | cgiSetVariable("SECTION", "printers"); | |
84 | ||
85 | /* | |
86 | * See if we are displaying a printer or all printers... | |
87 | */ | |
88 | ||
89 | if ((printer = getenv("PATH_INFO")) != NULL) | |
90 | { | |
91 | printer ++; | |
92 | ||
93 | if (!*printer) | |
94 | printer = NULL; | |
95 | } | |
96 | ||
97 | /* | |
98 | * See who is logged in... | |
99 | */ | |
100 | ||
101 | user = getenv("REMOTE_USER"); | |
102 | ||
103 | /* | |
104 | * Connect to the HTTP server... | |
105 | */ | |
106 | ||
107 | http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption()); | |
108 | ||
109 | /* | |
110 | * Get the default printer... | |
111 | */ | |
112 | ||
113 | if (!op) | |
114 | { | |
115 | /* | |
116 | * Get the default destination... | |
117 | */ | |
118 | ||
119 | request = ippNewRequest(CUPS_GET_DEFAULT); | |
120 | ||
121 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
122 | "requested-attributes", | |
123 | sizeof(def_attrs) / sizeof(def_attrs[0]), NULL, def_attrs); | |
124 | ||
125 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
126 | { | |
127 | if ((attr = ippFindAttribute(response, "printer-name", IPP_TAG_NAME)) != NULL) | |
128 | cgiSetVariable("DEFAULT_NAME", attr->values[0].string.text); | |
129 | ||
130 | if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL) | |
131 | { | |
132 | char url[HTTP_MAX_URI]; /* New URL */ | |
133 | ||
134 | ||
135 | cgiSetVariable("DEFAULT_URI", | |
136 | cgiRewriteURL(attr->values[0].string.text, | |
137 | url, sizeof(url), NULL)); | |
138 | } | |
139 | ||
140 | ippDelete(response); | |
141 | } | |
142 | ||
143 | /* | |
144 | * See if we need to show a list of printers or the status of a | |
145 | * single printer... | |
146 | */ | |
147 | ||
148 | if (!printer) | |
149 | show_all_printers(http, user); | |
150 | else | |
151 | show_printer(http, printer); | |
152 | } | |
153 | else if (!strcasecmp(op, "print-self-test-page") && printer) | |
154 | print_command(http, printer, "PrintSelfTestPage"); | |
155 | else if (!strcasecmp(op, "clean-print-heads") && printer) | |
156 | print_command(http, printer, "Clean all"); | |
157 | else if (!strcasecmp(op, "print-test-page") && printer) | |
158 | cgiPrintTestPage(http, printer); | |
159 | else if (!strcasecmp(op, "move-jobs") && printer) | |
160 | cgiMoveJobs(http, printer, 0); | |
161 | else | |
162 | { | |
163 | /* | |
164 | * Unknown/bad operation... | |
165 | */ | |
166 | ||
167 | if (printer) | |
168 | cgiStartHTML(printer); | |
169 | else | |
170 | cgiStartHTML(cgiText(_("Printers"))); | |
171 | ||
172 | cgiCopyTemplateLang("error-op.tmpl"); | |
173 | cgiEndHTML(); | |
174 | } | |
175 | ||
176 | /* | |
177 | * Close the HTTP server connection... | |
178 | */ | |
179 | ||
180 | httpClose(http); | |
181 | ||
182 | /* | |
183 | * Return with no errors... | |
184 | */ | |
185 | ||
186 | return (0); | |
187 | } | |
188 | ||
189 | ||
190 | /* | |
191 | * 'print_command()' - Send a print command to the printer. | |
192 | */ | |
193 | ||
194 | void | |
195 | print_command(http_t *http, /* I - Connection to server */ | |
196 | const char *printer, /* I - Printer */ | |
197 | const char *command) /* I - Command to send */ | |
198 | { | |
199 | cups_file_t *fp; /* File pointer */ | |
200 | char filename[1024]; /* Temporary file */ | |
201 | ipp_t *request, /* IPP request */ | |
202 | *response; /* IPP response */ | |
203 | char uri[HTTP_MAX_URI], /* Printer URI */ | |
204 | resource[1024], /* POST resource path */ | |
205 | refresh[1024]; /* Refresh URL */ | |
206 | const char *user; /* Username */ | |
207 | ||
208 | ||
209 | /* | |
210 | * See who is logged in... | |
211 | */ | |
212 | ||
213 | if ((user = getenv("REMOTE_USER")) == NULL) | |
214 | user = "guest"; | |
215 | ||
216 | /* | |
217 | * Create the CUPS command file to print... | |
218 | */ | |
219 | ||
220 | if ((fp = cupsTempFile2(filename, sizeof(filename))) == NULL) | |
221 | { | |
222 | cgiStartHTML(cgiText(_("Printer Maintenance"))); | |
223 | cgiSetVariable("MESSAGE", _("Unable to create temporary file:")); | |
224 | cgiSetVariable("ERROR", strerror(errno)); | |
225 | cgiCopyTemplateLang("error.tmpl"); | |
226 | cgiEndHTML(); | |
227 | return; | |
228 | } | |
229 | ||
230 | cupsFilePuts(fp, "#CUPS-COMMAND\n"); | |
231 | cupsFilePrintf(fp, "%s\n", command); | |
232 | cupsFileClose(fp); | |
233 | ||
234 | /* | |
235 | * Point to the printer... | |
236 | */ | |
237 | ||
238 | snprintf(resource, sizeof(resource), "/printers/%s", printer); | |
239 | ||
240 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, | |
241 | "localhost", ippPort(), "/printers/%s", printer); | |
242 | ||
243 | /* | |
244 | * Build an IPP_PRINT_JOB request, which requires the following | |
245 | * attributes: | |
246 | * | |
247 | * attributes-charset | |
248 | * attributes-natural-language | |
249 | * printer-uri | |
250 | * requesting-user-name | |
251 | * document-format | |
252 | */ | |
253 | ||
254 | request = ippNewRequest(IPP_PRINT_JOB); | |
255 | ||
256 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
257 | NULL, uri); | |
258 | ||
259 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
260 | "requesting-user-name", NULL, user); | |
261 | ||
262 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", | |
263 | NULL, "Printer Maintenance"); | |
264 | ||
265 | ippAddString(request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format", | |
266 | NULL, "application/postscript"); | |
267 | ||
268 | /* | |
269 | * Do the request and get back a response... | |
270 | */ | |
271 | ||
272 | if ((response = cupsDoFileRequest(http, request, resource, | |
273 | filename)) != NULL) | |
274 | { | |
275 | cgiSetIPPVars(response, NULL, NULL, NULL, 0); | |
276 | ||
277 | ippDelete(response); | |
278 | } | |
279 | ||
280 | unlink(filename); | |
281 | ||
282 | if (cupsLastError() <= IPP_OK_CONFLICT) | |
283 | { | |
284 | /* | |
285 | * Automatically reload the printer status page... | |
286 | */ | |
287 | ||
288 | cgiFormEncode(uri, resource, sizeof(uri)); | |
289 | snprintf(refresh, sizeof(refresh), "2;URL=%s", uri); | |
290 | cgiSetVariable("refresh_page", refresh); | |
291 | } | |
292 | ||
293 | cgiStartHTML(cgiText(_("Printer Maintenance"))); | |
294 | ||
295 | if (cupsLastError() > IPP_OK_CONFLICT) | |
296 | cgiShowIPPError(_("Unable to send maintenance job:")); | |
297 | else | |
298 | { | |
299 | cgiSetVariable("PRINTER_NAME", printer); | |
300 | ||
301 | cgiCopyTemplateLang("maintenance.tmpl"); | |
302 | } | |
303 | ||
304 | cgiEndHTML(); | |
305 | } | |
306 | ||
307 | ||
308 | /* | |
309 | * 'show_all_printers()' - Show all printers... | |
310 | */ | |
311 | ||
312 | void | |
313 | show_all_printers(http_t *http, /* I - Connection to server */ | |
314 | const char *user) /* I - Username */ | |
315 | { | |
316 | int i; /* Looping var */ | |
317 | ipp_t *request, /* IPP request */ | |
318 | *response; /* IPP response */ | |
319 | cups_array_t *printers; /* Array of printer objects */ | |
320 | ipp_attribute_t *printer, /* Printer object */ | |
321 | *attr; /* Current attribute */ | |
322 | int ascending, /* Order of printers (0 = descending) */ | |
323 | first, /* First printer to show */ | |
324 | count; /* Number of printers */ | |
325 | const char *var; /* Form variable */ | |
326 | void *search; /* Search data */ | |
327 | char url[1024], /* URL for prev/next/this */ | |
328 | *urlptr, /* Position in URL */ | |
329 | *urlend; /* End of URL */ | |
330 | ||
331 | ||
332 | fprintf(stderr, "DEBUG: show_all_printers(http=%p, user=\"%s\")\n", | |
333 | http, user ? user : "(null)"); | |
334 | ||
335 | /* | |
336 | * Show the standard header... | |
337 | */ | |
338 | ||
339 | cgiStartHTML(cgiText(_("Printers"))); | |
340 | ||
341 | /* | |
342 | * Build a CUPS_GET_PRINTERS request, which requires the following | |
343 | * attributes: | |
344 | * | |
345 | * attributes-charset | |
346 | * attributes-natural-language | |
347 | * printer-type | |
348 | * printer-type-mask | |
349 | * requesting-user-name | |
350 | */ | |
351 | ||
352 | request = ippNewRequest(CUPS_GET_PRINTERS); | |
353 | ||
354 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, | |
355 | "printer-type", 0); | |
356 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, | |
357 | "printer-type-mask", CUPS_PRINTER_CLASS); | |
358 | ||
359 | if (user) | |
360 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
361 | "requesting-user-name", NULL, user); | |
362 | ||
363 | cgiGetAttributes(request, "printers.tmpl"); | |
364 | ||
365 | /* | |
366 | * Do the request and get back a response... | |
367 | */ | |
368 | ||
369 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
370 | { | |
371 | /* | |
372 | * Get a list of matching job objects. | |
373 | */ | |
374 | ||
375 | if ((var = cgiGetVariable("QUERY")) != NULL) | |
376 | search = cgiCompileSearch(var); | |
377 | else | |
378 | search = NULL; | |
379 | ||
380 | printers = cgiGetIPPObjects(response, search); | |
381 | count = cupsArrayCount(printers); | |
382 | ||
383 | if (search) | |
384 | cgiFreeSearch(search); | |
385 | ||
386 | /* | |
387 | * Figure out which printers to display... | |
388 | */ | |
389 | ||
390 | if ((var = cgiGetVariable("FIRST")) != NULL) | |
391 | first = atoi(var); | |
392 | else | |
393 | first = 0; | |
394 | ||
395 | if (first >= count) | |
396 | first = count - CUPS_PAGE_MAX; | |
397 | ||
398 | first = (first / CUPS_PAGE_MAX) * CUPS_PAGE_MAX; | |
399 | ||
400 | if (first < 0) | |
401 | first = 0; | |
402 | ||
403 | sprintf(url, "%d", count); | |
404 | cgiSetVariable("TOTAL", url); | |
405 | ||
406 | if ((var = cgiGetVariable("ORDER")) != NULL) | |
407 | ascending = !strcasecmp(var, "asc"); | |
408 | else | |
409 | ascending = 1; | |
410 | ||
411 | if (ascending) | |
412 | { | |
413 | for (i = 0, printer = (ipp_attribute_t *)cupsArrayIndex(printers, first); | |
414 | i < CUPS_PAGE_MAX && printer; | |
415 | i ++, printer = (ipp_attribute_t *)cupsArrayNext(printers)) | |
416 | { | |
417 | cgiSetIPPObjectVars(printer, NULL, i); | |
418 | ||
419 | cgiSetArray("cupscommand", i, "0"); | |
420 | ||
421 | for (attr = printer; attr; attr = attr->next) | |
422 | if (attr->group_tag != IPP_TAG_PRINTER || !attr->name) | |
423 | break; | |
424 | else if (!strcmp(attr->name, "printer-type")) | |
425 | { | |
426 | if (attr->values[0].integer & CUPS_PRINTER_COMMANDS) | |
427 | cgiSetArray("cupscommand", i, "1"); | |
428 | break; | |
429 | } | |
430 | } | |
431 | } | |
432 | else | |
433 | { | |
434 | for (i = 0, printer = (ipp_attribute_t *)cupsArrayIndex(printers, count - first - 1); | |
435 | i < CUPS_PAGE_MAX && printer; | |
436 | i ++, printer = (ipp_attribute_t *)cupsArrayPrev(printers)) | |
437 | { | |
438 | cgiSetIPPObjectVars(printer, NULL, i); | |
439 | ||
440 | cgiSetArray("cupscommand", i, "0"); | |
441 | ||
442 | for (attr = printer; attr; attr = attr->next) | |
443 | if (attr->group_tag == IPP_TAG_ZERO || !attr->name) | |
444 | break; | |
445 | else if (!strcmp(attr->name, "printer-type")) | |
446 | { | |
447 | if (attr->values[0].integer & CUPS_PRINTER_COMMANDS) | |
448 | cgiSetArray("cupscommand", i, "1"); | |
449 | break; | |
450 | } | |
451 | } | |
452 | } | |
453 | ||
454 | /* | |
455 | * Save navigation URLs... | |
456 | */ | |
457 | ||
458 | urlend = url + sizeof(url); | |
459 | ||
460 | if ((var = cgiGetVariable("QUERY")) != NULL) | |
461 | { | |
462 | strlcpy(url, "/printers/?QUERY=", sizeof(url)); | |
463 | urlptr = url + strlen(url); | |
464 | ||
465 | cgiFormEncode(urlptr, var, urlend - urlptr); | |
466 | urlptr += strlen(urlptr); | |
467 | ||
468 | strlcpy(urlptr, "&", urlend - urlptr); | |
469 | urlptr += strlen(urlptr); | |
470 | } | |
471 | else | |
472 | { | |
473 | strlcpy(url, "/printers/?", sizeof(url)); | |
474 | urlptr = url + strlen(url); | |
475 | } | |
476 | ||
477 | snprintf(urlptr, urlend - urlptr, "FIRST=%d", first); | |
478 | cgiSetVariable("THISURL", url); | |
479 | ||
480 | if (first > 0) | |
481 | { | |
482 | snprintf(urlptr, urlend - urlptr, "FIRST=%d&ORDER=%s", | |
483 | first - CUPS_PAGE_MAX, ascending ? "asc" : "dec"); | |
484 | cgiSetVariable("PREVURL", url); | |
485 | } | |
486 | ||
487 | if ((first + CUPS_PAGE_MAX) < count) | |
488 | { | |
489 | snprintf(urlptr, urlend - urlptr, "FIRST=%d&ORDER=%s", | |
490 | first + CUPS_PAGE_MAX, ascending ? "asc" : "dec"); | |
491 | cgiSetVariable("NEXTURL", url); | |
492 | } | |
493 | ||
494 | /* | |
495 | * Then show everything... | |
496 | */ | |
497 | ||
498 | cgiCopyTemplateLang("search.tmpl"); | |
499 | ||
500 | cgiCopyTemplateLang("printers-header.tmpl"); | |
501 | ||
502 | if (count > 0) | |
503 | cgiCopyTemplateLang("pager.tmpl"); | |
504 | ||
505 | cgiCopyTemplateLang("printers.tmpl"); | |
506 | ||
507 | if (count > 0) | |
508 | cgiCopyTemplateLang("pager.tmpl"); | |
509 | ||
510 | /* | |
511 | * Delete the response... | |
512 | */ | |
513 | ||
514 | cupsArrayDelete(printers); | |
515 | ippDelete(response); | |
516 | } | |
517 | else | |
518 | { | |
519 | /* | |
520 | * Show the error... | |
521 | */ | |
522 | ||
523 | cgiShowIPPError(_("Unable to get printer list:")); | |
524 | } | |
525 | ||
526 | cgiEndHTML(); | |
527 | } | |
528 | ||
529 | ||
530 | /* | |
531 | * 'show_printer()' - Show a single printer. | |
532 | */ | |
533 | ||
534 | void | |
535 | show_printer(http_t *http, /* I - Connection to server */ | |
536 | const char *printer) /* I - Name of printer */ | |
537 | { | |
538 | ipp_t *request, /* IPP request */ | |
539 | *response; /* IPP response */ | |
540 | ipp_attribute_t *attr; /* IPP attribute */ | |
541 | char uri[HTTP_MAX_URI]; /* Printer URI */ | |
542 | char refresh[1024]; /* Refresh URL */ | |
543 | ||
544 | ||
545 | fprintf(stderr, "DEBUG: show_printer(http=%p, printer=\"%s\")\n", | |
546 | http, printer ? printer : "(null)"); | |
547 | ||
548 | /* | |
549 | * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following | |
550 | * attributes: | |
551 | * | |
552 | * attributes-charset | |
553 | * attributes-natural-language | |
554 | * printer-uri | |
555 | */ | |
556 | ||
557 | request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES); | |
558 | ||
559 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, | |
560 | "localhost", 0, "/printers/%s", printer); | |
561 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, | |
562 | uri); | |
563 | ||
564 | cgiGetAttributes(request, "printers.tmpl"); | |
565 | ||
566 | /* | |
567 | * Do the request and get back a response... | |
568 | */ | |
569 | ||
570 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
571 | { | |
572 | /* | |
573 | * Got the result; set the CGI variables and check the status of a | |
574 | * single-queue request... | |
575 | */ | |
576 | ||
577 | cgiSetIPPVars(response, NULL, NULL, NULL, 0); | |
578 | ||
579 | if ((attr = ippFindAttribute(response, "printer-type", | |
580 | IPP_TAG_ENUM)) != NULL) | |
581 | { | |
582 | cgiSetVariable("cupscommand", | |
583 | (attr->values[0].integer & CUPS_PRINTER_COMMANDS) ? | |
584 | "1" : "0"); | |
585 | } | |
586 | ||
587 | if (printer && (attr = ippFindAttribute(response, "printer-state", | |
588 | IPP_TAG_ENUM)) != NULL && | |
589 | attr->values[0].integer == IPP_PRINTER_PROCESSING) | |
590 | { | |
591 | /* | |
592 | * Printer is processing - automatically refresh the page until we | |
593 | * are done printing... | |
594 | */ | |
595 | ||
596 | cgiFormEncode(uri, printer, sizeof(uri)); | |
597 | snprintf(refresh, sizeof(refresh), "10;URL=/printers/%s", uri); | |
598 | cgiSetVariable("refresh_page", refresh); | |
599 | } | |
600 | ||
601 | /* | |
602 | * Delete the response... | |
603 | */ | |
604 | ||
605 | ippDelete(response); | |
606 | ||
607 | /* | |
608 | * Show the standard header... | |
609 | */ | |
610 | ||
611 | cgiStartHTML(printer); | |
612 | ||
613 | /* | |
614 | * Show the printer status... | |
615 | */ | |
616 | ||
617 | cgiCopyTemplateLang("printers.tmpl"); | |
618 | ||
619 | /* | |
620 | * Show jobs for the specified printer... | |
621 | */ | |
622 | ||
623 | cgiCopyTemplateLang("printer-jobs-header.tmpl"); | |
624 | cgiShowJobs(http, printer); | |
625 | } | |
626 | else | |
627 | { | |
628 | /* | |
629 | * Show the IPP error... | |
630 | */ | |
631 | ||
632 | cgiStartHTML(printer); | |
633 | cgiShowIPPError(_("Unable to get printer status:")); | |
634 | } | |
635 | ||
636 | cgiEndHTML(); | |
637 | } | |
638 | ||
639 | ||
640 | /* | |
641 | * End of "$Id: printers.c 177 2006-06-21 00:20:03Z jlovell $". | |
642 | */ |