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