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