]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/ipp.c
Load cups into easysw/current.
[thirdparty/cups.git] / backend / ipp.c
1 /*
2 * "$Id: ipp.c 5023 2006-01-29 14:39:44Z mike $"
3 *
4 * IPP backend for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
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" 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 * This file is subject to the Apple OS-Developed Software exception.
25 *
26 * Contents:
27 *
28 * main() - Send a file to the printer or server.
29 * check_printer_state() - Check the printer state...
30 * password_cb() - Disable the password prompt for
31 * cupsDoFileRequest().
32 * report_printer_state() - Report the printer state.
33 * run_pictwps_filter() - Convert PICT files to PostScript when printing
34 * remotely.
35 * sigterm_handler() - Handle 'terminate' signals that stop the backend.
36 */
37
38 /*
39 * Include necessary headers.
40 */
41
42 #include <cups/http-private.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <errno.h>
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 #include <cups/backend.h>
49 #include <cups/cups.h>
50 #include <cups/language.h>
51 #include <cups/string.h>
52 #include <signal.h>
53 #include <sys/wait.h>
54
55
56 /*
57 * Globals...
58 */
59
60 static char *password = NULL; /* Password for device URI */
61 #ifdef __APPLE__
62 static char pstmpname[1024] = ""; /* Temporary PostScript file name */
63 #endif /* __APPLE__ */
64 static char tmpfilename[1024] = ""; /* Temporary spool file name */
65
66
67 /*
68 * Local functions...
69 */
70
71 void check_printer_state(http_t *http, const char *uri,
72 const char *resource, const char *user,
73 int version);
74 const char *password_cb(const char *);
75 int report_printer_state(ipp_t *ipp);
76
77 #ifdef __APPLE__
78 int run_pictwps_filter(char **argv, const char *filename);
79 #endif /* __APPLE__ */
80 static void sigterm_handler(int sig);
81
82
83 /*
84 * 'main()' - Send a file to the printer or server.
85 *
86 * Usage:
87 *
88 * printer-uri job-id user title copies options [file]
89 */
90
91 int /* O - Exit status */
92 main(int argc, /* I - Number of command-line arguments (6 or 7) */
93 char *argv[]) /* I - Command-line arguments */
94 {
95 int i; /* Looping var */
96 int num_options; /* Number of printer options */
97 cups_option_t *options; /* Printer options */
98 char method[255], /* Method in URI */
99 hostname[1024], /* Hostname */
100 username[255], /* Username info */
101 resource[1024], /* Resource info (printer name) */
102 *optptr, /* Pointer to URI options */
103 name[255], /* Name of option */
104 value[255], /* Value of option */
105 *ptr; /* Pointer into name or value */
106 char *filename; /* File to print */
107 int port; /* Port number (not used) */
108 char uri[HTTP_MAX_URI]; /* Updated URI without user/pass */
109 ipp_status_t ipp_status; /* Status of IPP request */
110 http_t *http; /* HTTP connection */
111 ipp_t *request, /* IPP request */
112 *response, /* IPP response */
113 *supported; /* get-printer-attributes response */
114 int waitjob, /* Wait for job complete? */
115 waitprinter; /* Wait for printer ready? */
116 ipp_attribute_t *job_id_attr; /* job-id attribute */
117 int job_id; /* job-id value */
118 ipp_attribute_t *job_sheets; /* job-media-sheets-completed attribute */
119 ipp_attribute_t *job_state; /* job-state attribute */
120 ipp_attribute_t *copies_sup; /* copies-supported attribute */
121 ipp_attribute_t *format_sup; /* document-format-supported attribute */
122 ipp_attribute_t *printer_state; /* printer-state attribute */
123 ipp_attribute_t *printer_accepting; /* printer-is-accepting-jobs attribute */
124 int copies; /* Number of copies remaining */
125 const char *content_type; /* CONTENT_TYPE environment variable */
126 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
127 struct sigaction action; /* Actions for POSIX signals */
128 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
129 int version; /* IPP version */
130 int reasons; /* Number of printer-state-reasons shown */
131 static const char * const pattrs[] =
132 { /* Printer attributes we want */
133 "copies-supported",
134 "document-format-supported",
135 "printer-is-accepting-jobs",
136 "printer-state",
137 "printer-state-reasons",
138 };
139 static const char * const jattrs[] =
140 { /* Job attributes we want */
141 "job-media-sheets-completed",
142 "job-state"
143 };
144
145
146 /*
147 * Make sure status messages are not buffered...
148 */
149
150 setbuf(stderr, NULL);
151
152 /*
153 * Ignore SIGPIPE and catch SIGTERM signals...
154 */
155
156 #ifdef HAVE_SIGSET
157 sigset(SIGPIPE, SIG_IGN);
158 sigset(SIGTERM, sigterm_handler);
159 #elif defined(HAVE_SIGACTION)
160 memset(&action, 0, sizeof(action));
161 action.sa_handler = SIG_IGN;
162 sigaction(SIGPIPE, &action, NULL);
163
164 sigemptyset(&action.sa_mask);
165 sigaddset(&action.sa_mask, SIGTERM);
166 action.sa_handler = sigterm_handler;
167 sigaction(SIGTERM, &action, NULL);
168 #else
169 signal(SIGPIPE, SIG_IGN);
170 signal(SIGTERM, sigterm_handler);
171 #endif /* HAVE_SIGSET */
172
173 /*
174 * Check command-line...
175 */
176
177 if (argc == 1)
178 {
179 char *s;
180
181 if ((s = strrchr(argv[0], '/')) != NULL)
182 s ++;
183 else
184 s = argv[0];
185
186 printf("network %s \"Unknown\" \"Internet Printing Protocol (%s)\"\n", s, s);
187 return (CUPS_BACKEND_OK);
188 }
189 else if (argc < 6 || argc > 7)
190 {
191 fprintf(stderr, "Usage: %s job-id user title copies options [file]\n",
192 argv[0]);
193 return (CUPS_BACKEND_STOP);
194 }
195
196 /*
197 * Get the content type...
198 */
199
200 if (argc > 6)
201 content_type = getenv("CONTENT_TYPE");
202 else
203 content_type = "application/vnd.cups-raw";
204
205 if (content_type == NULL)
206 content_type = "application/octet-stream";
207
208 /*
209 * Extract the hostname and printer name from the URI...
210 */
211
212 if (httpSeparateURI(HTTP_URI_CODING_ALL, cupsBackendDeviceURI(argv),
213 method, sizeof(method), username, sizeof(username),
214 hostname, sizeof(hostname), &port,
215 resource, sizeof(resource)) < HTTP_URI_OK)
216 {
217 fputs("ERROR: Missing device URI on command-line and no DEVICE_URI "
218 "environment variable!\n", stderr);
219 return (CUPS_BACKEND_STOP);
220 }
221
222 if (!strcmp(method, "https"))
223 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS);
224
225 /*
226 * If we have 7 arguments, print the file named on the command-line.
227 * Otherwise, copy stdin to a temporary file and print the temporary
228 * file.
229 */
230
231 if (argc == 6)
232 {
233 /*
234 * Copy stdin to a temporary file...
235 */
236
237 int fd; /* Temporary file */
238 char buffer[8192]; /* Buffer for copying */
239 int bytes; /* Number of bytes read */
240
241
242 if ((fd = cupsTempFd(tmpfilename, sizeof(tmpfilename))) < 0)
243 {
244 perror("ERROR: unable to create temporary file");
245 return (CUPS_BACKEND_FAILED);
246 }
247
248 while ((bytes = fread(buffer, 1, sizeof(buffer), stdin)) > 0)
249 if (write(fd, buffer, bytes) < bytes)
250 {
251 perror("ERROR: unable to write to temporary file");
252 close(fd);
253 unlink(tmpfilename);
254 return (CUPS_BACKEND_FAILED);
255 }
256
257 close(fd);
258 filename = tmpfilename;
259 }
260 else
261 filename = argv[6];
262
263 /*
264 * See if there are any options...
265 */
266
267 version = 1;
268 waitjob = 1;
269 waitprinter = 1;
270
271 if ((optptr = strchr(resource, '?')) != NULL)
272 {
273 /*
274 * Yup, terminate the device name string and move to the first
275 * character of the optptr...
276 */
277
278 *optptr++ = '\0';
279
280 /*
281 * Then parse the optptr...
282 */
283
284 while (*optptr)
285 {
286 /*
287 * Get the name...
288 */
289
290 for (ptr = name; *optptr && *optptr != '=';)
291 if (ptr < (name + sizeof(name) - 1))
292 *ptr++ = *optptr++;
293 *ptr = '\0';
294
295 if (*optptr == '=')
296 {
297 /*
298 * Get the value...
299 */
300
301 optptr ++;
302
303 for (ptr = value; *optptr && *optptr != '+' && *optptr != '&';)
304 if (ptr < (value + sizeof(value) - 1))
305 *ptr++ = *optptr++;
306 *ptr = '\0';
307
308 if (*optptr == '+')
309 optptr ++;
310 }
311 else
312 value[0] = '\0';
313
314 /*
315 * Process the option...
316 */
317
318 if (!strcasecmp(name, "waitjob"))
319 {
320 /*
321 * Wait for job completion?
322 */
323
324 waitjob = !strcasecmp(value, "on") ||
325 !strcasecmp(value, "yes") ||
326 !strcasecmp(value, "true");
327 }
328 else if (!strcasecmp(name, "waitprinter"))
329 {
330 /*
331 * Wait for printer idle?
332 */
333
334 waitprinter = !strcasecmp(value, "on") ||
335 !strcasecmp(value, "yes") ||
336 !strcasecmp(value, "true");
337 }
338 else if (!strcasecmp(name, "encryption"))
339 {
340 /*
341 * Enable/disable encryption?
342 */
343
344 if (!strcasecmp(value, "always"))
345 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS);
346 else if (!strcasecmp(value, "required"))
347 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
348 else if (!strcasecmp(value, "never"))
349 cupsSetEncryption(HTTP_ENCRYPT_NEVER);
350 else if (!strcasecmp(value, "ifrequested"))
351 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED);
352 else
353 {
354 fprintf(stderr, "ERROR: Unknown encryption option value \"%s\"!\n",
355 value);
356 }
357 }
358 else if (!strcasecmp(name, "version"))
359 {
360 if (!strcmp(value, "1.0"))
361 version = 0;
362 else if (!strcmp(value, "1.1"))
363 version = 1;
364 else
365 {
366 fprintf(stderr, "ERROR: Unknown version option value \"%s\"!\n",
367 value);
368 }
369 }
370 else
371 {
372 /*
373 * Unknown option...
374 */
375
376 fprintf(stderr, "ERROR: Unknown option \"%s\" with value \"%s\"!\n",
377 name, value);
378 }
379 }
380 }
381
382 /*
383 * Set the authentication info, if any...
384 */
385
386 cupsSetPasswordCB(password_cb);
387
388 if (username[0])
389 {
390 /*
391 * Use authenticaion information in the device URI...
392 */
393
394 if ((password = strchr(username, ':')) != NULL)
395 *password++ = '\0';
396
397 cupsSetUser(username);
398 }
399 else if (!getuid())
400 {
401 /*
402 * Try loading authentication information from the a##### file.
403 */
404
405 const char *request_root; /* CUPS_REQUESTROOT env var */
406 char afilename[1024], /* a##### filename */
407 aline[1024]; /* Line from file */
408 FILE *fp; /* File pointer */
409
410
411 if ((request_root = getenv("CUPS_REQUESTROOT")) != NULL)
412 {
413 /*
414 * Try opening authentication cache file...
415 */
416
417 snprintf(afilename, sizeof(afilename), "%s/a%05d", request_root,
418 atoi(argv[1]));
419 if ((fp = fopen(afilename, "r")) != NULL)
420 {
421 /*
422 * Read username...
423 */
424
425 if (fgets(aline, sizeof(aline), fp))
426 {
427 /*
428 * Decode username...
429 */
430
431 i = sizeof(username);
432 httpDecode64_2(username, &i, aline);
433
434 /*
435 * Read password...
436 */
437
438 if (fgets(aline, sizeof(aline), fp))
439 {
440 /*
441 * Decode password...
442 */
443
444 i = sizeof(password);
445 httpDecode64_2(password, &i, aline);
446 }
447 }
448
449 /*
450 * Close the file...
451 */
452
453 fclose(fp);
454 }
455 }
456 }
457
458 /*
459 * Try connecting to the remote server...
460 */
461
462 do
463 {
464 fprintf(stderr, "INFO: Connecting to %s on port %d...\n", hostname, port);
465
466 if ((http = httpConnectEncrypt(hostname, port, cupsEncryption())) == NULL)
467 {
468 if (getenv("CLASS") != NULL)
469 {
470 /*
471 * If the CLASS environment variable is set, the job was submitted
472 * to a class and not to a specific queue. In this case, we want
473 * to abort immediately so that the job can be requeued on the next
474 * available printer in the class.
475 */
476
477 fprintf(stderr, "INFO: Unable to connect to %s, queuing on next printer in class...\n",
478 hostname);
479
480 if (argc == 6 || strcmp(filename, argv[6]))
481 unlink(filename);
482
483 /*
484 * Sleep 5 seconds to keep the job from requeuing too rapidly...
485 */
486
487 sleep(5);
488
489 return (CUPS_BACKEND_FAILED);
490 }
491
492 if (errno == ECONNREFUSED || errno == EHOSTDOWN ||
493 errno == EHOSTUNREACH)
494 {
495 fprintf(stderr, "INFO: Network host \'%s\' is busy; will retry in 30 seconds...\n",
496 hostname);
497 sleep(30);
498 }
499 else if (h_errno)
500 {
501 fprintf(stderr, "INFO: Unable to lookup host \'%s\' - %s\n",
502 hostname, hstrerror(h_errno));
503 sleep(30);
504 }
505 else
506 {
507 perror("ERROR: Unable to connect to IPP host");
508 sleep(30);
509 }
510 }
511 }
512 while (http == NULL);
513
514 fprintf(stderr, "INFO: Connected to %s...\n", hostname);
515
516 /*
517 * Build a URI for the printer and fill the standard IPP attributes for
518 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
519 * might contain username:password information...
520 */
521
522 snprintf(uri, sizeof(uri), "%s://%s:%d%s", method, hostname, port, resource);
523
524 /*
525 * First validate the destination and see if the device supports multiple
526 * copies. We have to do this because some IPP servers (e.g. HP JetDirect)
527 * don't support the copies attribute...
528 */
529
530 copies_sup = NULL;
531 format_sup = NULL;
532 supported = NULL;
533
534 do
535 {
536 /*
537 * Build the IPP request...
538 */
539
540 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
541 request->request.op.version[1] = version;
542
543 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
544 NULL, uri);
545
546 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
547 "requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]),
548 NULL, pattrs);
549
550 /*
551 * Do the request...
552 */
553
554 fputs("DEBUG: Getting supported attributes...\n", stderr);
555
556 if ((supported = cupsDoRequest(http, request, resource)) == NULL)
557 ipp_status = cupsLastError();
558 else
559 ipp_status = supported->request.status.status_code;
560
561 if (ipp_status > IPP_OK_CONFLICT)
562 {
563 if (ipp_status == IPP_PRINTER_BUSY ||
564 ipp_status == IPP_SERVICE_UNAVAILABLE)
565 {
566 fputs("INFO: Printer busy; will retry in 10 seconds...\n", stderr);
567 report_printer_state(supported);
568 sleep(10);
569 }
570 else if ((ipp_status == IPP_BAD_REQUEST ||
571 ipp_status == IPP_VERSION_NOT_SUPPORTED) && version == 1)
572 {
573 /*
574 * Switch to IPP/1.0...
575 */
576
577 fputs("INFO: Printer does not support IPP/1.1, trying IPP/1.0...\n", stderr);
578 version = 0;
579 httpReconnect(http);
580 }
581 else if (ipp_status == IPP_NOT_FOUND)
582 {
583 fputs("ERROR: Destination printer does not exist!\n", stderr);
584
585 if (supported)
586 ippDelete(supported);
587
588 return (CUPS_BACKEND_STOP);
589 }
590 else
591 {
592 fprintf(stderr, "ERROR: Unable to get printer status (%s)!\n",
593 ippErrorString(ipp_status));
594 sleep(10);
595 }
596
597 if (supported)
598 ippDelete(supported);
599
600 continue;
601 }
602 else if ((copies_sup = ippFindAttribute(supported, "copies-supported",
603 IPP_TAG_RANGE)) != NULL)
604 {
605 /*
606 * Has the "copies-supported" attribute - does it have an upper
607 * bound > 1?
608 */
609
610 if (copies_sup->values[0].range.upper <= 1)
611 copies_sup = NULL; /* No */
612 }
613
614 format_sup = ippFindAttribute(supported, "document-format-supported",
615 IPP_TAG_MIMETYPE);
616
617 if (format_sup)
618 {
619 fprintf(stderr, "DEBUG: document-format-supported (%d values)\n",
620 format_sup->num_values);
621 for (i = 0; i < format_sup->num_values; i ++)
622 fprintf(stderr, "DEBUG: [%d] = \"%s\"\n", i,
623 format_sup->values[i].string.text);
624 }
625
626 report_printer_state(supported);
627 }
628 while (ipp_status > IPP_OK_CONFLICT);
629
630 /*
631 * See if the printer is accepting jobs and is not stopped; if either
632 * condition is true and we are printing to a class, requeue the job...
633 */
634
635 if (getenv("CLASS") != NULL)
636 {
637 printer_state = ippFindAttribute(supported, "printer-state",
638 IPP_TAG_ENUM);
639 printer_accepting = ippFindAttribute(supported, "printer-is-accepting-jobs",
640 IPP_TAG_BOOLEAN);
641
642 if (printer_state == NULL ||
643 (printer_state->values[0].integer > IPP_PRINTER_PROCESSING && waitprinter) ||
644 printer_accepting == NULL ||
645 !printer_accepting->values[0].boolean)
646 {
647 /*
648 * If the CLASS environment variable is set, the job was submitted
649 * to a class and not to a specific queue. In this case, we want
650 * to abort immediately so that the job can be requeued on the next
651 * available printer in the class.
652 */
653
654 fprintf(stderr, "INFO: Unable to queue job on %s, queuing on next printer in class...\n",
655 hostname);
656
657 ippDelete(supported);
658 httpClose(http);
659
660 if (argc == 6 || strcmp(filename, argv[6]))
661 unlink(filename);
662
663 /*
664 * Sleep 5 seconds to keep the job from requeuing too rapidly...
665 */
666
667 sleep(5);
668
669 return (CUPS_BACKEND_FAILED);
670 }
671 }
672
673 /*
674 * See if the printer supports multiple copies...
675 */
676
677 if (copies_sup || argc < 7)
678 copies = 1;
679 else
680 copies = atoi(argv[4]);
681
682 /*
683 * Then issue the print-job request...
684 */
685
686 reasons = 0;
687
688 while (copies > 0)
689 {
690 /*
691 * Build the IPP request...
692 */
693
694 request = ippNewRequest(IPP_PRINT_JOB);
695 request->request.op.version[1] = version;
696
697 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
698 NULL, uri);
699
700 fprintf(stderr, "DEBUG: printer-uri = \"%s\"\n", uri);
701
702 if (argv[2][0])
703 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
704 "requesting-user-name", NULL, argv[2]);
705
706 fprintf(stderr, "DEBUG: requesting-user-name = \"%s\"\n", argv[2]);
707
708 if (argv[3][0])
709 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
710 argv[3]);
711
712 fprintf(stderr, "DEBUG: job-name = \"%s\"\n", argv[3]);
713
714 /*
715 * Handle options on the command-line...
716 */
717
718 options = NULL;
719 num_options = cupsParseOptions(argv[5], 0, &options);
720
721 #ifdef __APPLE__
722 if (content_type != NULL && strcasecmp(content_type, "application/pictwps") == 0)
723 {
724 if (format_sup != NULL)
725 {
726 for (i = 0; i < format_sup->num_values; i ++)
727 if (strcasecmp(content_type, format_sup->values[i].string.text) == 0)
728 break;
729 }
730
731 if (format_sup == NULL || i >= format_sup->num_values)
732 {
733 /*
734 * Remote doesn't support "application/pictwps" (i.e. it's not MacOS X)
735 * so convert the document to PostScript...
736 */
737
738 if (run_pictwps_filter(argv, filename))
739 return (CUPS_BACKEND_FAILED);
740
741 filename = pstmpname;
742
743 /*
744 * Change the MIME type to application/postscript...
745 */
746
747 content_type = "application/postscript";
748 }
749 }
750 #endif /* __APPLE__ */
751
752 if (content_type != NULL && format_sup != NULL)
753 {
754 for (i = 0; i < format_sup->num_values; i ++)
755 if (strcasecmp(content_type, format_sup->values[i].string.text) == 0)
756 break;
757
758 if (i < format_sup->num_values)
759 num_options = cupsAddOption("document-format", content_type,
760 num_options, &options);
761 }
762
763 if (copies_sup)
764 {
765 /*
766 * Only send options if the destination printer supports the copies
767 * attribute. This is a hack for the HP JetDirect implementation of
768 * IPP, which does not accept extension attributes and incorrectly
769 * reports a client-error-bad-request error instead of the
770 * successful-ok-unsupported-attributes status. In short, at least
771 * some HP implementations of IPP are non-compliant.
772 */
773
774 cupsEncodeOptions(request, num_options, options);
775 ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_INTEGER, "copies",
776 atoi(argv[4]));
777 }
778
779 cupsFreeOptions(num_options, options);
780
781 /*
782 * If copies aren't supported, then we are likely dealing with an HP
783 * JetDirect. The HP IPP implementation seems to close the connection
784 * after every request (that is, it does *not* implement HTTP Keep-
785 * Alive, which is REQUIRED by HTTP/1.1...
786 */
787
788 if (!copies_sup)
789 httpReconnect(http);
790
791 /*
792 * Do the request...
793 */
794
795 if ((response = cupsDoFileRequest(http, request, resource, filename)) == NULL)
796 ipp_status = cupsLastError();
797 else
798 ipp_status = response->request.status.status_code;
799
800 if (ipp_status > IPP_OK_CONFLICT)
801 {
802 job_id = 0;
803
804 if (ipp_status == IPP_SERVICE_UNAVAILABLE ||
805 ipp_status == IPP_PRINTER_BUSY)
806 {
807 fputs("INFO: Printer is busy; retrying print job...\n", stderr);
808 sleep(10);
809 }
810 else
811 fprintf(stderr, "ERROR: Print file was not accepted (%s)!\n",
812 ippErrorString(ipp_status));
813 }
814 else if ((job_id_attr = ippFindAttribute(response, "job-id",
815 IPP_TAG_INTEGER)) == NULL)
816 {
817 fputs("NOTICE: Print file accepted - job ID unknown.\n", stderr);
818 job_id = 0;
819 }
820 else
821 {
822 job_id = job_id_attr->values[0].integer;
823 fprintf(stderr, "NOTICE: Print file accepted - job ID %d.\n", job_id);
824 }
825
826 if (response)
827 ippDelete(response);
828
829 if (ipp_status <= IPP_OK_CONFLICT && argc > 6)
830 {
831 fprintf(stderr, "PAGE: 1 %d\n", copies_sup ? atoi(argv[4]) : 1);
832 copies --;
833 }
834 else if (ipp_status == IPP_SERVICE_UNAVAILABLE ||
835 ipp_status == IPP_PRINTER_BUSY)
836 break;
837 else
838 copies --;
839
840 /*
841 * Wait for the job to complete...
842 */
843
844 if (!job_id || !waitjob)
845 continue;
846
847 fputs("INFO: Waiting for job to complete...\n", stderr);
848
849 for (;;)
850 {
851 /*
852 * Build an IPP_GET_JOB_ATTRIBUTES request...
853 */
854
855 request = ippNewRequest(IPP_GET_JOB_ATTRIBUTES);
856 request->request.op.version[1] = version;
857
858 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
859 NULL, uri);
860
861 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
862 job_id);
863
864 if (argv[2][0])
865 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
866 "requesting-user-name", NULL, argv[2]);
867
868 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
869 "requested-attributes", sizeof(jattrs) / sizeof(jattrs[0]),
870 NULL, jattrs);
871
872 /*
873 * Do the request...
874 */
875
876 if (!copies_sup)
877 httpReconnect(http);
878
879 if ((response = cupsDoRequest(http, request, resource)) == NULL)
880 ipp_status = cupsLastError();
881 else
882 ipp_status = response->request.status.status_code;
883
884 if (ipp_status == IPP_NOT_FOUND)
885 {
886 /*
887 * Job has gone away and/or the server has no job history...
888 */
889
890 ippDelete(response);
891
892 ipp_status = IPP_OK;
893 break;
894 }
895
896 if (ipp_status > IPP_OK_CONFLICT)
897 {
898 if (ipp_status != IPP_SERVICE_UNAVAILABLE &&
899 ipp_status != IPP_PRINTER_BUSY)
900 {
901 if (response)
902 ippDelete(response);
903
904 fprintf(stderr, "ERROR: Unable to get job %d attributes (%s)!\n",
905 job_id, ippErrorString(ipp_status));
906 break;
907 }
908 }
909
910 if (response != NULL)
911 {
912 if ((job_state = ippFindAttribute(response, "job-state",
913 IPP_TAG_ENUM)) != NULL)
914 {
915 /*
916 * Stop polling if the job is finished or pending-held...
917 */
918
919 if (job_state->values[0].integer > IPP_JOB_PROCESSING ||
920 job_state->values[0].integer == IPP_JOB_HELD)
921 {
922 if ((job_sheets = ippFindAttribute(response,
923 "job-media-sheets-completed",
924 IPP_TAG_INTEGER)) != NULL)
925 fprintf(stderr, "PAGE: total %d\n", job_sheets->values[0].integer);
926
927 ippDelete(response);
928 break;
929 }
930 }
931 }
932
933 if (response)
934 ippDelete(response);
935
936 /*
937 * Check the printer state and report it if necessary...
938 */
939
940 check_printer_state(http, uri, resource, argv[2], version);
941
942 /*
943 * Wait 10 seconds before polling again...
944 */
945
946 sleep(10);
947 }
948 }
949
950 /*
951 * Check the printer state and report it if necessary...
952 */
953
954 /* if (!copies_sup)
955 httpReconnect(http);*/
956
957 check_printer_state(http, uri, resource, argv[2], version);
958
959 /*
960 * Free memory...
961 */
962
963 httpClose(http);
964
965 if (supported)
966 ippDelete(supported);
967
968 /*
969 * Remove the temporary file(s) if necessary...
970 */
971
972 if (tmpfilename[0])
973 unlink(tmpfilename);
974
975 #ifdef __APPLE__
976 if (pstmpname[0])
977 unlink(pstmpname);
978 #endif /* __APPLE__ */
979
980 /*
981 * Return the queue status...
982 */
983
984 return (ipp_status > IPP_OK_CONFLICT ? CUPS_BACKEND_FAILED : CUPS_BACKEND_OK);
985 }
986
987
988 /*
989 * 'check_printer_state()' - Check the printer state...
990 */
991
992 void
993 check_printer_state(
994 http_t *http, /* I - HTTP connection */
995 const char *uri, /* I - Printer URI */
996 const char *resource, /* I - Resource path */
997 const char *user, /* I - Username, if any */
998 int version) /* I - IPP version */
999 {
1000 ipp_t *request, /* IPP request */
1001 *response; /* IPP response */
1002
1003
1004 /*
1005 * Check on the printer state...
1006 */
1007
1008 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
1009 request->request.op.version[1] = version;
1010
1011 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1012 NULL, uri);
1013
1014 if (user && user[0])
1015 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1016 "requesting-user-name", NULL, user);
1017
1018 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1019 "requested-attributes", NULL, "printer-state-reasons");
1020
1021 /*
1022 * Do the request...
1023 */
1024
1025 if ((response = cupsDoRequest(http, request, resource)) != NULL)
1026 {
1027 report_printer_state(response);
1028 ippDelete(response);
1029 }
1030 }
1031
1032
1033 /*
1034 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
1035 */
1036
1037 const char * /* O - Password */
1038 password_cb(const char *prompt) /* I - Prompt (not used) */
1039 {
1040 (void)prompt;
1041
1042 if (password)
1043 return (password);
1044 else
1045 {
1046 /*
1047 * If there is no password set in the device URI, return the
1048 * "authentication required" exit code...
1049 */
1050
1051 if (tmpfilename[0])
1052 unlink(tmpfilename);
1053
1054 #ifdef __APPLE__
1055 if (pstmpname[0])
1056 unlink(pstmpname);
1057 #endif /* __APPLE__ */
1058
1059 exit(CUPS_BACKEND_AUTH_REQUIRED);
1060 }
1061 }
1062
1063
1064 /*
1065 * 'report_printer_state()' - Report the printer state.
1066 */
1067
1068 int /* O - Number of reasons shown */
1069 report_printer_state(ipp_t *ipp) /* I - IPP response */
1070 {
1071 int i; /* Looping var */
1072 int count; /* Count of reasons shown... */
1073 ipp_attribute_t *reasons; /* printer-state-reasons */
1074 const char *reason; /* Current reason */
1075 const char *message; /* Message to show */
1076 char unknown[1024]; /* Unknown message string */
1077 const char *prefix; /* Prefix for STATE: line */
1078 char state[1024]; /* State string */
1079
1080
1081 if ((reasons = ippFindAttribute(ipp, "printer-state-reasons",
1082 IPP_TAG_KEYWORD)) == NULL)
1083 return (0);
1084
1085 state[0] = '\0';
1086 prefix = "STATE: ";
1087
1088 for (i = 0, count = 0; i < reasons->num_values; i ++)
1089 {
1090 reason = reasons->values[i].string.text;
1091
1092 strlcat(state, prefix, sizeof(state));
1093 strlcat(state, reason, sizeof(state));
1094
1095 prefix = ",";
1096 message = NULL;
1097
1098 if (!strncmp(reason, "media-needed", 12))
1099 message = "Media tray needs to be filled.";
1100 else if (!strncmp(reason, "media-jam", 9))
1101 message = "Media jam!";
1102 else if (!strncmp(reason, "moving-to-paused", 16) ||
1103 !strncmp(reason, "paused", 6) ||
1104 !strncmp(reason, "shutdown", 8))
1105 message = "Printer off-line.";
1106 else if (!strncmp(reason, "toner-low", 9))
1107 message = "Toner low.";
1108 else if (!strncmp(reason, "toner-empty", 11))
1109 message = "Out of toner!";
1110 else if (!strncmp(reason, "cover-open", 10))
1111 message = "Cover open.";
1112 else if (!strncmp(reason, "interlock-open", 14))
1113 message = "Interlock open.";
1114 else if (!strncmp(reason, "door-open", 9))
1115 message = "Door open.";
1116 else if (!strncmp(reason, "input-tray-missing", 18))
1117 message = "Media tray missing!";
1118 else if (!strncmp(reason, "media-low", 9))
1119 message = "Media tray almost empty.";
1120 else if (!strncmp(reason, "media-empty", 11))
1121 message = "Media tray empty!";
1122 else if (!strncmp(reason, "output-tray-missing", 19))
1123 message = "Output tray missing!";
1124 else if (!strncmp(reason, "output-area-almost-full", 23))
1125 message = "Output bin almost full.";
1126 else if (!strncmp(reason, "output-area-full", 16))
1127 message = "Output bin full!";
1128 else if (!strncmp(reason, "marker-supply-low", 17))
1129 message = "Ink/toner almost empty.";
1130 else if (!strncmp(reason, "marker-supply-empty", 19))
1131 message = "Ink/toner empty!";
1132 else if (!strncmp(reason, "marker-waste-almost-full", 24))
1133 message = "Ink/toner waste bin almost full.";
1134 else if (!strncmp(reason, "marker-waste-full", 17))
1135 message = "Ink/toner waste bin full!";
1136 else if (!strncmp(reason, "fuser-over-temp", 15))
1137 message = "Fuser temperature high!";
1138 else if (!strncmp(reason, "fuser-under-temp", 16))
1139 message = "Fuser temperature low!";
1140 else if (!strncmp(reason, "opc-near-eol", 12))
1141 message = "OPC almost at end-of-life.";
1142 else if (!strncmp(reason, "opc-life-over", 13))
1143 message = "OPC at end-of-life!";
1144 else if (!strncmp(reason, "developer-low", 13))
1145 message = "Developer almost empty.";
1146 else if (!strncmp(reason, "developer-empty", 15))
1147 message = "Developer empty!";
1148 else if (strstr(reason, "error") != NULL)
1149 {
1150 message = unknown;
1151
1152 snprintf(unknown, sizeof(unknown), "Unknown printer error (%s)!",
1153 reason);
1154 }
1155
1156 if (message)
1157 {
1158 count ++;
1159 if (strstr(reasons->values[i].string.text, "error"))
1160 fprintf(stderr, "ERROR: %s\n", message);
1161 else if (strstr(reasons->values[i].string.text, "warning"))
1162 fprintf(stderr, "WARNING: %s\n", message);
1163 else
1164 fprintf(stderr, "INFO: %s\n", message);
1165 }
1166 }
1167
1168 fprintf(stderr, "%s\n", state);
1169
1170 return (count);
1171 }
1172
1173
1174 #ifdef __APPLE__
1175 /*
1176 * 'run_pictwps_filter()' - Convert PICT files to PostScript when printing
1177 * remotely.
1178 *
1179 * This step is required because the PICT format is not documented and
1180 * subject to change, so developing a filter for other OS's is infeasible.
1181 * Also, fonts required by the PICT file need to be embedded on the
1182 * client side (which has the fonts), so we run the filter to get a
1183 * PostScript file for printing...
1184 */
1185
1186 int /* O - Exit status of filter */
1187 run_pictwps_filter(char **argv, /* I - Command-line arguments */
1188 const char *filename)/* I - Filename */
1189 {
1190 struct stat fileinfo; /* Print file information */
1191 const char *ppdfile; /* PPD file for destination printer */
1192 int pid; /* Child process ID */
1193 int fd; /* Temporary file descriptor */
1194 int status; /* Exit status of filter */
1195 const char *printer; /* PRINTER env var */
1196 static char ppdenv[1024]; /* PPD environment variable */
1197
1198
1199 /*
1200 * First get the PPD file for the printer...
1201 */
1202
1203 printer = getenv("PRINTER");
1204 if (!printer)
1205 {
1206 fputs("ERROR: PRINTER environment variable not defined!\n", stderr);
1207 return (-1);
1208 }
1209
1210 if ((ppdfile = cupsGetPPD(printer)) == NULL)
1211 {
1212 fprintf(stderr, "ERROR: Unable to get PPD file for printer \"%s\" - %s.\n",
1213 printer, ippErrorString(cupsLastError()));
1214 /*return (-1);*/
1215 }
1216 else
1217 {
1218 snprintf(ppdenv, sizeof(ppdenv), "PPD=%s", ppdfile);
1219 putenv(ppdenv);
1220 }
1221
1222 /*
1223 * Then create a temporary file for printing...
1224 */
1225
1226 if ((fd = cupsTempFd(pstmpname, sizeof(pstmpname))) < 0)
1227 {
1228 fprintf(stderr, "ERROR: Unable to create temporary file - %s.\n",
1229 strerror(errno));
1230 if (ppdfile)
1231 unlink(ppdfile);
1232 return (-1);
1233 }
1234
1235 /*
1236 * Get the owner of the spool file - it is owned by the user we want to run
1237 * as...
1238 */
1239
1240 if (argv[6])
1241 stat(argv[6], &fileinfo);
1242 else
1243 {
1244 /*
1245 * Use the OSX defaults, as an up-stream filter created the PICT
1246 * file...
1247 */
1248
1249 fileinfo.st_uid = 1;
1250 fileinfo.st_gid = 80;
1251 }
1252
1253 if (ppdfile)
1254 chown(ppdfile, fileinfo.st_uid, fileinfo.st_gid);
1255
1256 fchown(fd, fileinfo.st_uid, fileinfo.st_gid);
1257
1258 /*
1259 * Finally, run the filter to convert the file...
1260 */
1261
1262 if ((pid = fork()) == 0)
1263 {
1264 /*
1265 * Child process for pictwpstops... Redirect output of pictwpstops to a
1266 * file...
1267 */
1268
1269 close(1);
1270 dup(fd);
1271 close(fd);
1272
1273 if (!getuid())
1274 {
1275 /*
1276 * Change to an unpriviledged user...
1277 */
1278
1279 setgid(fileinfo.st_gid);
1280 setuid(fileinfo.st_uid);
1281 }
1282
1283 execlp("pictwpstops", printer, argv[1], argv[2], argv[3], argv[4], argv[5],
1284 filename, NULL);
1285 perror("ERROR: Unable to exec pictwpstops");
1286 return (errno);
1287 }
1288
1289 close(fd);
1290
1291 if (pid < 0)
1292 {
1293 /*
1294 * Error!
1295 */
1296
1297 perror("ERROR: Unable to fork pictwpstops");
1298 unlink(filename);
1299 if (ppdfile)
1300 unlink(ppdfile);
1301 return (-1);
1302 }
1303
1304 /*
1305 * Now wait for the filter to complete...
1306 */
1307
1308 if (wait(&status) < 0)
1309 {
1310 perror("ERROR: Unable to wait for pictwpstops");
1311 close(fd);
1312 unlink(filename);
1313 if (ppdfile)
1314 unlink(ppdfile);
1315 return (-1);
1316 }
1317
1318 if (ppdfile)
1319 unlink(ppdfile);
1320
1321 close(fd);
1322
1323 if (status)
1324 {
1325 if (status >= 256)
1326 fprintf(stderr, "ERROR: pictwpstops exited with status %d!\n",
1327 status / 256);
1328 else
1329 fprintf(stderr, "ERROR: pictwpstops exited on signal %d!\n",
1330 status);
1331
1332 unlink(filename);
1333 return (status);
1334 }
1335
1336 /*
1337 * Return with no errors..
1338 */
1339
1340 return (0);
1341 }
1342 #endif /* __APPLE__ */
1343
1344
1345 /*
1346 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1347 */
1348
1349 static void
1350 sigterm_handler(int sig) /* I - Signal */
1351 {
1352 (void)sig; /* remove compiler warnings... */
1353
1354 /*
1355 * Remove the temporary file(s) if necessary...
1356 */
1357
1358 if (tmpfilename[0])
1359 unlink(tmpfilename);
1360
1361 #ifdef __APPLE__
1362 if (pstmpname[0])
1363 unlink(pstmpname);
1364 #endif /* __APPLE__ */
1365
1366 exit(1);
1367 }
1368
1369
1370 /*
1371 * End of "$Id: ipp.c 5023 2006-01-29 14:39:44Z mike $".
1372 */