]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/ipp.c
Merge changes from CUPS 1.5svn-r9641
[thirdparty/cups.git] / backend / ipp.c
1 /*
2 * "$Id: ipp.c 7948 2008-09-17 00:04:12Z mike $"
3 *
4 * IPP backend for CUPS.
5 *
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * "LICENSE" which should have been included with this file. If this
13 * file is missing or damaged, see the license at "http://www.cups.org/".
14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
19 * main() - Send a file to the printer or server.
20 * cancel_job() - Cancel a print job.
21 * check_printer_state() - Check the printer state.
22 * compress_files() - Compress print files...
23 * monitor_printer() - Monitor the printer state...
24 * new_request() - Create a new print creation or validation request.
25 * password_cb() - Disable the password prompt for
26 * cupsDoFileRequest().
27 * report_attr() - Report an IPP attribute value.
28 * report_printer_state() - Report the printer state.
29 * sigterm_handler() - Handle 'terminate' signals that stop the backend.
30 */
31
32 /*
33 * Include necessary headers.
34 */
35
36 #include "backend-private.h"
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/wait.h>
40
41
42 /*
43 * Types...
44 */
45
46 typedef struct _cups_monitor_s /**** Monitoring data ****/
47 {
48 const char *uri, /* Printer URI */
49 *hostname, /* Hostname */
50 *user, /* Username */
51 *resource; /* Resource path */
52 int port, /* Port number */
53 version, /* IPP version */
54 job_id; /* Job ID for submitted job */
55 http_encryption_t encryption; /* Use encryption? */
56 ipp_jstate_t job_state; /* Current job state */
57 ipp_pstate_t printer_state; /* Current printer state */
58 } _cups_monitor_t;
59
60
61 /*
62 * Globals...
63 */
64
65 static const char *auth_info_required = "none";
66 /* New auth-info-required value */
67 static const char * const jattrs[] = /* Job attributes we want */
68 {
69 "job-media-sheets-completed",
70 "job-state",
71 "job-state-reasons"
72 };
73 static int job_canceled = 0; /* Job cancelled? */
74 static char *password = NULL; /* Password for device URI */
75 static int password_tries = 0; /* Password tries */
76 static const char * const pattrs[] = /* Printer attributes we want */
77 {
78 "copies-supported",
79 "cups-version",
80 "document-format-supported",
81 "marker-colors",
82 "marker-high-levels",
83 "marker-levels",
84 "marker-low-levels",
85 "marker-message",
86 "marker-names",
87 "marker-types",
88 "media-col-supported",
89 "multiple-document-handling-supported",
90 "operations-supported",
91 "printer-alert",
92 "printer-alert-description",
93 "printer-is-accepting-jobs",
94 "printer-state",
95 "printer-state-message",
96 "printer-state-reasons",
97 };
98 static char tmpfilename[1024] = ""; /* Temporary spool file name */
99
100
101 /*
102 * Local functions...
103 */
104
105 static void cancel_job(http_t *http, const char *uri, int id,
106 const char *resource, const char *user,
107 int version);
108 static ipp_pstate_t check_printer_state(http_t *http, const char *uri,
109 const char *resource,
110 const char *user, int version,
111 int job_id);
112 #ifdef HAVE_LIBZ
113 static void compress_files(int num_files, char **files);
114 #endif /* HAVE_LIBZ */
115 static void *monitor_printer(_cups_monitor_t *monitor);
116 static ipp_t *new_request(ipp_op_t op, int version, const char *uri,
117 const char *user, const char *title,
118 int num_options, cups_option_t *options,
119 const char *compression, int copies,
120 const char *format, _ppd_cache_t *pc,
121 ipp_attribute_t *media_col_sup,
122 ipp_attribute_t *doc_handling_sup);
123 static const char *password_cb(const char *);
124 static void report_attr(ipp_attribute_t *attr);
125 static int report_printer_state(ipp_t *ipp, int job_id);
126 static void sigterm_handler(int sig);
127
128
129 /*
130 * 'main()' - Send a file to the printer or server.
131 *
132 * Usage:
133 *
134 * printer-uri job-id user title copies options [file]
135 */
136
137 int /* O - Exit status */
138 main(int argc, /* I - Number of command-line args */
139 char *argv[]) /* I - Command-line arguments */
140 {
141 int i; /* Looping var */
142 int send_options; /* Send job options? */
143 int num_options; /* Number of printer options */
144 cups_option_t *options; /* Printer options */
145 const char *device_uri; /* Device URI */
146 char scheme[255], /* Scheme in URI */
147 hostname[1024], /* Hostname */
148 username[255], /* Username info */
149 resource[1024], /* Resource info (printer name) */
150 addrname[256], /* Address name */
151 *optptr, /* Pointer to URI options */
152 *name, /* Name of option */
153 *value, /* Value of option */
154 sep; /* Separator character */
155 http_addrlist_t *addrlist; /* Address of printer */
156 int snmp_fd, /* SNMP socket */
157 start_count, /* Page count via SNMP at start */
158 page_count, /* Page count via SNMP */
159 have_supplies; /* Printer supports supply levels? */
160 int num_files; /* Number of files to print */
161 char **files, /* Files to print */
162 *compatfile = NULL; /* Compatibility filename */
163 off_t compatsize = 0; /* Size of compatibility file */
164 int port; /* Port number (not used) */
165 char portname[255]; /* Port name */
166 char uri[HTTP_MAX_URI]; /* Updated URI without user/pass */
167 http_status_t http_status; /* Status of HTTP request */
168 ipp_status_t ipp_status; /* Status of IPP request */
169 http_t *http; /* HTTP connection */
170 ipp_t *request, /* IPP request */
171 *response, /* IPP response */
172 *supported; /* get-printer-attributes response */
173 time_t start_time; /* Time of first connect */
174 int contimeout; /* Connection timeout */
175 int delay, /* Delay for retries */
176 prev_delay; /* Previous delay */
177 const char *compression; /* Compression mode */
178 int waitjob, /* Wait for job complete? */
179 waitprinter; /* Wait for printer ready? */
180 _cups_monitor_t monitor; /* Monitoring data */
181 ipp_attribute_t *job_id_attr; /* job-id attribute */
182 int job_id; /* job-id value */
183 ipp_attribute_t *job_sheets; /* job-media-sheets-completed */
184 ipp_attribute_t *job_state; /* job-state */
185 ipp_attribute_t *copies_sup; /* copies-supported */
186 ipp_attribute_t *cups_version; /* cups-version */
187 ipp_attribute_t *format_sup; /* document-format-supported */
188 ipp_attribute_t *media_col_sup; /* media-col-supported */
189 ipp_attribute_t *operations_sup; /* operations-supported */
190 ipp_attribute_t *doc_handling_sup; /* multiple-document-handling-supported */
191 ipp_attribute_t *printer_state; /* printer-state attribute */
192 ipp_attribute_t *printer_accepting; /* printer-is-accepting-jobs */
193 int validate_job; /* Does printer support Validate-Job? */
194 int copies, /* Number of copies for job */
195 copies_remaining; /* Number of copies remaining */
196 const char *content_type, /* CONTENT_TYPE environment variable */
197 *final_content_type, /* FINAL_CONTENT_TYPE environment var */
198 *document_format; /* document-format value */
199 int fd; /* File descriptor */
200 off_t bytes; /* Bytes copied */
201 char buffer[16384]; /* Copy buffer */
202 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
203 struct sigaction action; /* Actions for POSIX signals */
204 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
205 int version; /* IPP version */
206 ppd_file_t *ppd; /* PPD file */
207 _ppd_cache_t *pc; /* PPD cache and mapping data */
208
209
210 /*
211 * Make sure status messages are not buffered...
212 */
213
214 setbuf(stderr, NULL);
215
216 /*
217 * Ignore SIGPIPE and catch SIGTERM signals...
218 */
219
220 #ifdef HAVE_SIGSET
221 sigset(SIGPIPE, SIG_IGN);
222 sigset(SIGTERM, sigterm_handler);
223 #elif defined(HAVE_SIGACTION)
224 memset(&action, 0, sizeof(action));
225 action.sa_handler = SIG_IGN;
226 sigaction(SIGPIPE, &action, NULL);
227
228 sigemptyset(&action.sa_mask);
229 sigaddset(&action.sa_mask, SIGTERM);
230 action.sa_handler = sigterm_handler;
231 sigaction(SIGTERM, &action, NULL);
232 #else
233 signal(SIGPIPE, SIG_IGN);
234 signal(SIGTERM, sigterm_handler);
235 #endif /* HAVE_SIGSET */
236
237 /*
238 * Check command-line...
239 */
240
241 if (argc == 1)
242 {
243 char *s;
244
245 if ((s = strrchr(argv[0], '/')) != NULL)
246 s ++;
247 else
248 s = argv[0];
249
250 printf("network %s \"Unknown\" \"%s (%s)\"\n",
251 s, _cupsLangString(cupsLangDefault(),
252 _("Internet Printing Protocol")), s);
253 return (CUPS_BACKEND_OK);
254 }
255 else if (argc < 6)
256 {
257 _cupsLangPrintf(stderr,
258 _("Usage: %s job-id user title copies options [file]"),
259 argv[0]);
260 return (CUPS_BACKEND_STOP);
261 }
262
263 /*
264 * Get the (final) content type...
265 */
266
267 if ((content_type = getenv("CONTENT_TYPE")) == NULL)
268 content_type = "application/octet-stream";
269
270 if ((final_content_type = getenv("FINAL_CONTENT_TYPE")) == NULL)
271 {
272 final_content_type = content_type;
273
274 if (!strncmp(final_content_type, "printer/", 8))
275 final_content_type = "application/vnd.cups-raw";
276 }
277
278 /*
279 * Extract the hostname and printer name from the URI...
280 */
281
282 while ((device_uri = cupsBackendDeviceURI(argv)) == NULL)
283 {
284 _cupsLangPrintFilter(stderr, "INFO", _("Unable to locate printer."));
285 sleep(10);
286
287 if (getenv("CLASS") != NULL)
288 return (CUPS_BACKEND_FAILED);
289 }
290
291 httpSeparateURI(HTTP_URI_CODING_ALL, device_uri, scheme, sizeof(scheme),
292 username, sizeof(username), hostname, sizeof(hostname), &port,
293 resource, sizeof(resource));
294
295 if (!port)
296 port = IPP_PORT; /* Default to port 631 */
297
298 if (!strcmp(scheme, "https"))
299 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS);
300 else
301 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED);
302
303 /*
304 * See if there are any options...
305 */
306
307 compression = NULL;
308 version = 20;
309 waitjob = 1;
310 waitprinter = 1;
311 contimeout = 7 * 24 * 60 * 60;
312
313 if ((optptr = strchr(resource, '?')) != NULL)
314 {
315 /*
316 * Yup, terminate the device name string and move to the first
317 * character of the optptr...
318 */
319
320 *optptr++ = '\0';
321
322 /*
323 * Then parse the optptr...
324 */
325
326 while (*optptr)
327 {
328 /*
329 * Get the name...
330 */
331
332 name = optptr;
333
334 while (*optptr && *optptr != '=' && *optptr != '+' && *optptr != '&')
335 optptr ++;
336
337 if ((sep = *optptr) != '\0')
338 *optptr++ = '\0';
339
340 if (sep == '=')
341 {
342 /*
343 * Get the value...
344 */
345
346 value = optptr;
347
348 while (*optptr && *optptr != '+' && *optptr != '&')
349 optptr ++;
350
351 if (*optptr)
352 *optptr++ = '\0';
353 }
354 else
355 value = (char *)"";
356
357 /*
358 * Process the option...
359 */
360
361 if (!strcasecmp(name, "waitjob"))
362 {
363 /*
364 * Wait for job completion?
365 */
366
367 waitjob = !strcasecmp(value, "on") ||
368 !strcasecmp(value, "yes") ||
369 !strcasecmp(value, "true");
370 }
371 else if (!strcasecmp(name, "waitprinter"))
372 {
373 /*
374 * Wait for printer idle?
375 */
376
377 waitprinter = !strcasecmp(value, "on") ||
378 !strcasecmp(value, "yes") ||
379 !strcasecmp(value, "true");
380 }
381 else if (!strcasecmp(name, "encryption"))
382 {
383 /*
384 * Enable/disable encryption?
385 */
386
387 if (!strcasecmp(value, "always"))
388 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS);
389 else if (!strcasecmp(value, "required"))
390 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
391 else if (!strcasecmp(value, "never"))
392 cupsSetEncryption(HTTP_ENCRYPT_NEVER);
393 else if (!strcasecmp(value, "ifrequested"))
394 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED);
395 else
396 {
397 _cupsLangPrintFilter(stderr, "ERROR",
398 _("Unknown encryption option value: \"%s\"."),
399 value);
400 }
401 }
402 else if (!strcasecmp(name, "version"))
403 {
404 if (!strcmp(value, "1.0"))
405 version = 10;
406 else if (!strcmp(value, "1.1"))
407 version = 11;
408 else if (!strcmp(value, "2.0"))
409 version = 20;
410 else if (!strcmp(value, "2.1"))
411 version = 21;
412 else if (!strcmp(value, "2.2"))
413 version = 22;
414 else
415 {
416 _cupsLangPrintFilter(stderr, "ERROR",
417 _("Unknown version option value: \"%s\"."),
418 value);
419 }
420 }
421 #ifdef HAVE_LIBZ
422 else if (!strcasecmp(name, "compression"))
423 {
424 if (!strcasecmp(value, "true") || !strcasecmp(value, "yes") ||
425 !strcasecmp(value, "on") || !strcasecmp(value, "gzip"))
426 compression = "gzip";
427 }
428 #endif /* HAVE_LIBZ */
429 else if (!strcasecmp(name, "contimeout"))
430 {
431 /*
432 * Set the connection timeout...
433 */
434
435 if (atoi(value) > 0)
436 contimeout = atoi(value);
437 }
438 else
439 {
440 /*
441 * Unknown option...
442 */
443
444 _cupsLangPrintFilter(stderr, "ERROR",
445 _("Unknown option \"%s\" with value \"%s\"."),
446 name, value);
447 }
448 }
449 }
450
451 /*
452 * If we have 7 arguments, print the file named on the command-line.
453 * Otherwise, copy stdin to a temporary file and print the temporary
454 * file.
455 */
456
457 if (argc == 6)
458 {
459 num_files = 0;
460 send_options = !strcasecmp(final_content_type, "application/pdf") ||
461 !strcasecmp(final_content_type, "application/vnd.cups-pdf") ||
462 !strncasecmp(final_content_type, "image/", 6);
463
464 fputs("DEBUG: Sending stdin for job...\n", stderr);
465 }
466 else
467 {
468 /*
469 * Point to the files on the command-line...
470 */
471
472 num_files = argc - 6;
473 files = argv + 6;
474 send_options = 1;
475
476 #ifdef HAVE_LIBZ
477 if (compression)
478 compress_files(num_files, files);
479 #endif /* HAVE_LIBZ */
480
481 fprintf(stderr, "DEBUG: %d files to send in job...\n", num_files);
482 }
483
484 /*
485 * Set the authentication info, if any...
486 */
487
488 cupsSetPasswordCB(password_cb);
489
490 if (username[0])
491 {
492 /*
493 * Use authenticaion information in the device URI...
494 */
495
496 if ((password = strchr(username, ':')) != NULL)
497 *password++ = '\0';
498
499 cupsSetUser(username);
500 }
501 else
502 {
503 /*
504 * Try loading authentication information from the environment.
505 */
506
507 const char *ptr = getenv("AUTH_USERNAME");
508
509 if (ptr)
510 cupsSetUser(ptr);
511
512 password = getenv("AUTH_PASSWORD");
513 }
514
515 /*
516 * Try finding the remote server...
517 */
518
519 start_time = time(NULL);
520
521 sprintf(portname, "%d", port);
522
523 fputs("STATE: +connecting-to-device\n", stderr);
524 fprintf(stderr, "DEBUG: Looking up \"%s\"...\n", hostname);
525
526 while ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL)
527 {
528 _cupsLangPrintFilter(stderr, "INFO",
529 _("Unable to locate printer \"%s\"."), hostname);
530 sleep(10);
531
532 if (getenv("CLASS") != NULL)
533 {
534 fputs("STATE: -connecting-to-device\n", stderr);
535 return (CUPS_BACKEND_STOP);
536 }
537 }
538
539 http = _httpCreate(hostname, port, addrlist, cupsEncryption(), AF_UNSPEC);
540
541 /*
542 * See if the printer supports SNMP...
543 */
544
545 if ((snmp_fd = _cupsSNMPOpen(addrlist->addr.addr.sa_family)) >= 0)
546 {
547 have_supplies = !backendSNMPSupplies(snmp_fd, &(addrlist->addr),
548 &start_count, NULL);
549 }
550 else
551 have_supplies = start_count = 0;
552
553 /*
554 * Wait for data from the filter...
555 */
556
557 if (num_files == 0)
558 if (!backendWaitLoop(snmp_fd, &(addrlist->addr), 0, backendNetworkSideCB))
559 return (CUPS_BACKEND_OK);
560
561 /*
562 * Try connecting to the remote server...
563 */
564
565 delay = _cupsNextDelay(0, &prev_delay);
566
567 do
568 {
569 fprintf(stderr, "DEBUG: Connecting to %s:%d\n", hostname, port);
570 _cupsLangPrintFilter(stderr, "INFO", _("Connecting to printer."));
571
572 if (httpReconnect(http))
573 {
574 int error = errno; /* Connection error */
575
576 if (http->status == HTTP_PKI_ERROR)
577 fputs("STATE: +cups-certificate-error\n", stderr);
578
579 if (job_canceled)
580 break;
581
582 if (getenv("CLASS") != NULL)
583 {
584 /*
585 * If the CLASS environment variable is set, the job was submitted
586 * to a class and not to a specific queue. In this case, we want
587 * to abort immediately so that the job can be requeued on the next
588 * available printer in the class.
589 */
590
591 _cupsLangPrintFilter(stderr, "INFO",
592 _("Unable to contact printer, queuing on next "
593 "printer in class."));
594
595 /*
596 * Sleep 5 seconds to keep the job from requeuing too rapidly...
597 */
598
599 sleep(5);
600
601 fputs("STATE: -connecting-to-device\n", stderr);
602
603 return (CUPS_BACKEND_FAILED);
604 }
605
606 fprintf(stderr, "DEBUG: Connection error: %s\n", strerror(errno));
607
608 if (errno == ECONNREFUSED || errno == EHOSTDOWN ||
609 errno == EHOSTUNREACH)
610 {
611 if (contimeout && (time(NULL) - start_time) > contimeout)
612 {
613 _cupsLangPrintFilter(stderr, "ERROR",
614 _("The printer is not responding."));
615 fputs("STATE: -connecting-to-device\n", stderr);
616 return (CUPS_BACKEND_FAILED);
617 }
618
619 switch (error)
620 {
621 case EHOSTDOWN :
622 _cupsLangPrintFilter(stderr, "WARNING",
623 _("Network printer \"%s\" may not exist or "
624 "is unavailable at this time."),
625 hostname);
626 break;
627
628 case EHOSTUNREACH :
629 _cupsLangPrintFilter(stderr, "WARNING",
630 _("Network printer \"%s\" is unreachable at "
631 "this time."), hostname);
632 break;
633
634 case ECONNREFUSED :
635 default :
636 _cupsLangPrintFilter(stderr, "WARNING",
637 _("Network printer \"%s\" is busy."),
638 hostname);
639 break;
640 }
641
642 sleep(delay);
643
644 delay = _cupsNextDelay(delay, &prev_delay);
645 }
646 else
647 {
648 _cupsLangPrintFilter(stderr, "ERROR",
649 _("Network printer \"%s\" is not responding."),
650 hostname);
651 sleep(30);
652 }
653
654 if (job_canceled)
655 break;
656 }
657 else
658 fputs("STATE: -cups-certificate-error\n", stderr);
659 }
660 while (http->fd < 0);
661
662 if (job_canceled || !http)
663 return (CUPS_BACKEND_FAILED);
664
665 fputs("STATE: -connecting-to-device\n", stderr);
666 _cupsLangPrintFilter(stderr, "INFO", _("Connected to printer."));
667
668 #ifdef AF_INET6
669 if (http->hostaddr->addr.sa_family == AF_INET6)
670 fprintf(stderr, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
671 httpAddrString(http->hostaddr, addrname, sizeof(addrname)),
672 ntohs(http->hostaddr->ipv6.sin6_port));
673 else
674 #endif /* AF_INET6 */
675 if (http->hostaddr->addr.sa_family == AF_INET)
676 fprintf(stderr, "DEBUG: Connected to %s:%d (IPv4)...\n",
677 httpAddrString(http->hostaddr, addrname, sizeof(addrname)),
678 ntohs(http->hostaddr->ipv4.sin_port));
679
680 /*
681 * Build a URI for the printer and fill the standard IPP attributes for
682 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
683 * might contain username:password information...
684 */
685
686 httpAssembleURI(HTTP_URI_CODING_ALL, uri, sizeof(uri), scheme, NULL, hostname,
687 port, resource);
688
689 /*
690 * First validate the destination and see if the device supports multiple
691 * copies...
692 */
693
694 copies_sup = NULL;
695 cups_version = NULL;
696 format_sup = NULL;
697 media_col_sup = NULL;
698 supported = NULL;
699 operations_sup = NULL;
700 doc_handling_sup = NULL;
701 validate_job = 0;
702
703 do
704 {
705 /*
706 * Check for side-channel requests...
707 */
708
709 backendCheckSideChannel(snmp_fd, http->hostaddr);
710
711 /*
712 * Build the IPP request...
713 */
714
715 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
716 request->request.op.version[0] = version / 10;
717 request->request.op.version[1] = version % 10;
718
719 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
720 NULL, uri);
721
722 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
723 "requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]),
724 NULL, pattrs);
725
726 /*
727 * Do the request...
728 */
729
730 fputs("DEBUG: Getting supported attributes...\n", stderr);
731
732 if (http->version < HTTP_1_1)
733 {
734 fprintf(stderr, "DEBUG: Printer responded with HTTP version %d.%d.\n",
735 http->version / 100, http->version % 100);
736
737 _cupsLangPrintFilter(stderr, "ERROR",
738 _("This printer does not conform to the IPP "
739 "standard. Please contact the manufacturer of "
740 "your printer for assistance."));
741 }
742
743 supported = cupsDoRequest(http, request, resource);
744 ipp_status = cupsLastError();
745
746 if (ipp_status > IPP_OK_CONFLICT)
747 {
748 fprintf(stderr, "DEBUG: Get-Printer-Attributes returned %s.\n",
749 ippErrorString(ipp_status));
750
751 if (ipp_status == IPP_PRINTER_BUSY ||
752 ipp_status == IPP_SERVICE_UNAVAILABLE)
753 {
754 if (contimeout && (time(NULL) - start_time) > contimeout)
755 {
756 _cupsLangPrintFilter(stderr, "ERROR",
757 _("The printer is not responding."));
758 return (CUPS_BACKEND_FAILED);
759 }
760
761 _cupsLangPrintFilter(stderr, "WARNING",
762 _("Network host \"%s\" is busy; will retry in %d "
763 "seconds."), hostname, delay);
764
765 report_printer_state(supported, 0);
766
767 sleep(delay);
768
769 delay = _cupsNextDelay(delay, &prev_delay);
770 }
771 else if ((ipp_status == IPP_BAD_REQUEST ||
772 ipp_status == IPP_VERSION_NOT_SUPPORTED) && version > 10)
773 {
774 /*
775 * Switch to IPP/1.1 or IPP/1.0...
776 */
777
778 if (version >= 20)
779 {
780 _cupsLangPrintFilter(stderr, "INFO",
781 _("Printer does not support IPP/%d.%d, trying "
782 "IPP/%s."), version / 10, version % 10, "1.1");
783 version = 11;
784 }
785 else
786 {
787 _cupsLangPrintFilter(stderr, "INFO",
788 _("Printer does not support IPP/%d.%d, trying "
789 "IPP/%s."), version / 10, version % 10, "1.0");
790 version = 10;
791 }
792
793 httpReconnect(http);
794 }
795 else if (ipp_status == IPP_NOT_FOUND)
796 {
797 _cupsLangPrintFilter(stderr, "ERROR",
798 _("The printer URI is incorrect or no longer "
799 "exists."));
800
801 if (supported)
802 ippDelete(supported);
803
804 return (CUPS_BACKEND_STOP);
805 }
806 else if (ipp_status == IPP_NOT_AUTHORIZED || ipp_status == IPP_FORBIDDEN)
807 {
808 if (!strncmp(httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE),
809 "Negotiate", 9))
810 auth_info_required = "negotiate";
811
812 fprintf(stderr, "ATTR: auth-info-required=%s\n", auth_info_required);
813 return (CUPS_BACKEND_AUTH_REQUIRED);
814 }
815 else
816 {
817 _cupsLangPrintFilter(stderr, "ERROR",
818 _("Unable to get printer status: %s"),
819 cupsLastErrorString());
820 sleep(10);
821 }
822
823 if (supported)
824 ippDelete(supported);
825
826 continue;
827 }
828
829 /*
830 * Check for supported attributes...
831 */
832
833 if ((copies_sup = ippFindAttribute(supported, "copies-supported",
834 IPP_TAG_RANGE)) != NULL)
835 {
836 /*
837 * Has the "copies-supported" attribute - does it have an upper
838 * bound > 1?
839 */
840
841 fprintf(stderr, "DEBUG: copies-supported=%d-%d\n",
842 copies_sup->values[0].range.lower,
843 copies_sup->values[0].range.upper);
844
845 if (copies_sup->values[0].range.upper <= 1)
846 copies_sup = NULL; /* No */
847 }
848
849 cups_version = ippFindAttribute(supported, "cups-version", IPP_TAG_TEXT);
850
851 if ((format_sup = ippFindAttribute(supported, "document-format-supported",
852 IPP_TAG_MIMETYPE)) != NULL)
853 {
854 fprintf(stderr, "DEBUG: document-format-supported (%d values)\n",
855 format_sup->num_values);
856 for (i = 0; i < format_sup->num_values; i ++)
857 fprintf(stderr, "DEBUG: [%d] = \"%s\"\n", i,
858 format_sup->values[i].string.text);
859 }
860
861 if ((media_col_sup = ippFindAttribute(supported, "media-col-supported",
862 IPP_TAG_KEYWORD)) != NULL)
863 {
864 fprintf(stderr, "DEBUG: media-col-supported (%d values)\n",
865 media_col_sup->num_values);
866 for (i = 0; i < media_col_sup->num_values; i ++)
867 fprintf(stderr, "DEBUG: [%d] = \"%s\"\n", i,
868 media_col_sup->values[i].string.text);
869 }
870
871 if ((operations_sup = ippFindAttribute(supported, "operations-supported",
872 IPP_TAG_ENUM)) != NULL)
873 {
874 for (i = 0; i < operations_sup->num_values; i ++)
875 if (operations_sup->values[i].integer == IPP_VALIDATE_JOB)
876 {
877 validate_job = 1;
878 break;
879 }
880
881 if (!validate_job)
882 {
883 _cupsLangPrintFilter(stderr, "WARNING",
884 _("This printer does not conform to the IPP "
885 "standard and may not work."));
886 fputs("DEBUG: operations-supported does not list Validate-Job.\n",
887 stderr);
888 }
889 }
890 else
891 {
892 _cupsLangPrintFilter(stderr, "WARNING",
893 _("This printer does not conform to the IPP "
894 "standard and may not work."));
895 fputs("DEBUG: operations-supported not returned in "
896 "Get-Printer-Attributes request.\n", stderr);
897 }
898
899 doc_handling_sup = ippFindAttribute(supported,
900 "multiple-document-handling-supported",
901 IPP_TAG_KEYWORD);
902
903 report_printer_state(supported, 0);
904 }
905 while (ipp_status > IPP_OK_CONFLICT);
906
907 /*
908 * See if the printer is accepting jobs and is not stopped; if either
909 * condition is true and we are printing to a class, requeue the job...
910 */
911
912 if (getenv("CLASS") != NULL)
913 {
914 printer_state = ippFindAttribute(supported, "printer-state",
915 IPP_TAG_ENUM);
916 printer_accepting = ippFindAttribute(supported, "printer-is-accepting-jobs",
917 IPP_TAG_BOOLEAN);
918
919 if (printer_state == NULL ||
920 (printer_state->values[0].integer > IPP_PRINTER_PROCESSING &&
921 waitprinter) ||
922 printer_accepting == NULL ||
923 !printer_accepting->values[0].boolean)
924 {
925 /*
926 * If the CLASS environment variable is set, the job was submitted
927 * to a class and not to a specific queue. In this case, we want
928 * to abort immediately so that the job can be requeued on the next
929 * available printer in the class.
930 */
931
932 _cupsLangPrintFilter(stderr, "INFO",
933 _("Unable to contact printer, queuing on next "
934 "printer in class."));
935
936 ippDelete(supported);
937 httpClose(http);
938
939 /*
940 * Sleep 5 seconds to keep the job from requeuing too rapidly...
941 */
942
943 sleep(5);
944
945 return (CUPS_BACKEND_FAILED);
946 }
947 }
948
949 /*
950 * See if the printer supports multiple copies...
951 */
952
953 copies = atoi(argv[4]);
954
955 if (copies_sup || argc < 7)
956 {
957 copies_remaining = 1;
958
959 if (argc < 7 && !send_options)
960 copies = 1;
961 }
962 else
963 copies_remaining = copies;
964
965 /*
966 * Prepare remaining printing options...
967 */
968
969 options = NULL;
970 pc = NULL;
971
972 if (send_options)
973 {
974 num_options = cupsParseOptions(argv[5], 0, &options);
975
976 if (!cups_version && media_col_sup)
977 {
978 /*
979 * Load the PPD file and generate PWG attribute mapping information...
980 */
981
982 ppd = ppdOpenFile(getenv("PPD"));
983 pc = _ppdCacheCreateWithPPD(ppd);
984
985 ppdClose(ppd);
986 }
987 }
988 else
989 num_options = 0;
990
991 document_format = NULL;
992
993 if (format_sup != NULL)
994 {
995 for (i = 0; i < format_sup->num_values; i ++)
996 if (!strcasecmp(final_content_type, format_sup->values[i].string.text))
997 {
998 document_format = final_content_type;
999 break;
1000 }
1001
1002 if (!document_format)
1003 {
1004 for (i = 0; i < format_sup->num_values; i ++)
1005 if (!strcasecmp("application/octet-stream",
1006 format_sup->values[i].string.text))
1007 {
1008 document_format = "application/octet-stream";
1009 break;
1010 }
1011 }
1012 }
1013
1014 /*
1015 * If the printer does not support HTTP/1.1 (which IPP requires), copy stdin
1016 * to a temporary file so that we can do a HTTP/1.0 submission...
1017 *
1018 * (I hate compatibility hacks!)
1019 */
1020
1021 if (http->version < HTTP_1_1 && num_files == 0)
1022 {
1023 if ((fd = cupsTempFd(tmpfilename, sizeof(tmpfilename))) < 0)
1024 {
1025 perror("DEBUG: Unable to create temporary file");
1026 return (CUPS_BACKEND_FAILED);
1027 }
1028
1029 _cupsLangPrintFilter(stderr, "INFO", _("Copying print data."));
1030
1031 compatsize = backendRunLoop(-1, fd, snmp_fd, &(addrlist->addr), 0, 0,
1032 backendNetworkSideCB);
1033
1034 close(fd);
1035
1036 compatfile = tmpfilename;
1037 files = &compatfile;
1038 num_files = 1;
1039 }
1040 else if (http->version < HTTP_1_1 && num_files == 1)
1041 {
1042 struct stat fileinfo; /* File information */
1043
1044 if (!stat(files[0], &fileinfo))
1045 compatsize = fileinfo.st_size;
1046 }
1047
1048 /*
1049 * Start monitoring the printer in the background...
1050 */
1051
1052 monitor.uri = uri;
1053 monitor.hostname = hostname;
1054 monitor.user = argv[2];
1055 monitor.resource = resource;
1056 monitor.port = port;
1057 monitor.version = version;
1058 monitor.job_id = 0;
1059 monitor.encryption = cupsEncryption();
1060 monitor.job_state = IPP_JOB_PENDING;
1061 monitor.printer_state = IPP_PRINTER_IDLE;
1062
1063 _cupsThreadCreate((_cups_thread_func_t)monitor_printer, &monitor);
1064
1065 /*
1066 * Validate access to the printer...
1067 */
1068
1069 while (!job_canceled)
1070 {
1071 request = new_request(IPP_VALIDATE_JOB, version, uri, argv[2], argv[3],
1072 num_options, options, compression,
1073 copies_sup ? copies : 1, document_format, pc,
1074 media_col_sup, doc_handling_sup);
1075
1076 ippDelete(cupsDoRequest(http, request, resource));
1077
1078 ipp_status = cupsLastError();
1079
1080 if (ipp_status > IPP_OK_CONFLICT &&
1081 ipp_status != IPP_OPERATION_NOT_SUPPORTED)
1082 {
1083 if (job_canceled)
1084 break;
1085
1086 if (ipp_status == IPP_SERVICE_UNAVAILABLE ||
1087 ipp_status == IPP_PRINTER_BUSY)
1088 {
1089 _cupsLangPrintFilter(stderr, "INFO",
1090 _("Printer busy; will retry in 10 seconds."));
1091 sleep(10);
1092 }
1093 else
1094 {
1095 /*
1096 * Update auth-info-required as needed...
1097 */
1098
1099 _cupsLangPrintFilter(stderr, "ERROR", "%s", cupsLastErrorString());
1100
1101 if (ipp_status == IPP_NOT_AUTHORIZED || ipp_status == IPP_FORBIDDEN)
1102 {
1103 fprintf(stderr, "DEBUG: WWW-Authenticate=\"%s\"\n",
1104 httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE));
1105
1106 /*
1107 * Normal authentication goes through the password callback, which sets
1108 * auth_info_required to "username,password". Kerberos goes directly
1109 * through GSSAPI, so look for Negotiate in the WWW-Authenticate header
1110 * here and set auth_info_required as needed...
1111 */
1112
1113 if (!strncmp(httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE),
1114 "Negotiate", 9))
1115 auth_info_required = "negotiate";
1116 }
1117
1118 goto cleanup;
1119 }
1120 }
1121 else
1122 break;
1123 }
1124
1125 /*
1126 * Then issue the print-job request...
1127 */
1128
1129 job_id = 0;
1130
1131 while (!job_canceled && copies_remaining > 0)
1132 {
1133 /*
1134 * Check for side-channel requests...
1135 */
1136
1137 backendCheckSideChannel(snmp_fd, http->hostaddr);
1138
1139 /*
1140 * Build the IPP job creation request...
1141 */
1142
1143 if (job_canceled)
1144 break;
1145
1146 request = new_request(num_files > 1 ? IPP_CREATE_JOB : IPP_PRINT_JOB,
1147 version, uri, argv[2], argv[3], num_options, options,
1148 compression, copies_sup ? copies : 1, document_format,
1149 pc, media_col_sup, doc_handling_sup);
1150
1151 /*
1152 * Do the request...
1153 */
1154
1155 if (num_files > 1)
1156 response = cupsDoRequest(http, request, resource);
1157 else
1158 {
1159 size_t length = 0; /* Length of request */
1160
1161 if (compatsize > 0)
1162 {
1163 fputs("DEBUG: Sending file using HTTP/1.0 Content-Length...\n", stderr);
1164 length = ippLength(request) + (size_t)compatsize;
1165 }
1166 else
1167 fputs("DEBUG: Sending file using HTTP/1.1 chunking...\n", stderr);
1168
1169 http_status = cupsSendRequest(http, request, resource, length);
1170 if (http_status == HTTP_CONTINUE && request->state == IPP_DATA)
1171 {
1172 if (num_files == 1)
1173 fd = open(files[0], O_RDONLY);
1174 else
1175 fd = 0;
1176
1177 while ((bytes = read(fd, buffer, sizeof(buffer))) > 0)
1178 {
1179 fprintf(stderr, "DEBUG: Read %d bytes...\n", (int)bytes);
1180
1181 if (cupsWriteRequestData(http, buffer, bytes) != HTTP_CONTINUE)
1182 break;
1183 else
1184 {
1185 /*
1186 * Check for side-channel requests...
1187 */
1188
1189 backendCheckSideChannel(snmp_fd, http->hostaddr);
1190 }
1191 }
1192
1193 if (num_files == 1)
1194 close(fd);
1195 }
1196
1197 response = cupsGetResponse(http, resource);
1198 ippDelete(request);
1199 }
1200
1201 ipp_status = cupsLastError();
1202
1203 if (ipp_status > IPP_OK_CONFLICT)
1204 {
1205 job_id = 0;
1206
1207 if (job_canceled)
1208 break;
1209
1210 if (ipp_status == IPP_SERVICE_UNAVAILABLE ||
1211 ipp_status == IPP_PRINTER_BUSY)
1212 {
1213 _cupsLangPrintFilter(stderr, "INFO",
1214 _("Printer busy; will retry in 10 seconds."));
1215 sleep(10);
1216 }
1217 else
1218 {
1219 /*
1220 * Update auth-info-required as needed...
1221 */
1222
1223 _cupsLangPrintFilter(stderr, "ERROR",
1224 _("Print file was not accepted: %s"),
1225 cupsLastErrorString());
1226
1227 if (ipp_status == IPP_NOT_AUTHORIZED || ipp_status == IPP_FORBIDDEN)
1228 {
1229 fprintf(stderr, "DEBUG: WWW-Authenticate=\"%s\"\n",
1230 httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE));
1231
1232 /*
1233 * Normal authentication goes through the password callback, which sets
1234 * auth_info_required to "username,password". Kerberos goes directly
1235 * through GSSAPI, so look for Negotiate in the WWW-Authenticate header
1236 * here and set auth_info_required as needed...
1237 */
1238
1239 if (!strncmp(httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE),
1240 "Negotiate", 9))
1241 auth_info_required = "negotiate";
1242 }
1243 }
1244 }
1245 else if ((job_id_attr = ippFindAttribute(response, "job-id",
1246 IPP_TAG_INTEGER)) == NULL)
1247 {
1248 _cupsLangPrintFilter(stderr, "INFO",
1249 _("Print file accepted - job ID unknown."));
1250 job_id = 0;
1251 }
1252 else
1253 {
1254 monitor.job_id = job_id = job_id_attr->values[0].integer;
1255 _cupsLangPrintFilter(stderr, "INFO",
1256 _("Print file accepted - job ID %d."), job_id);
1257 }
1258
1259 ippDelete(response);
1260
1261 if (job_canceled)
1262 break;
1263
1264 if (job_id && num_files > 1)
1265 {
1266 for (i = 0; i < num_files; i ++)
1267 {
1268 /*
1269 * Check for side-channel requests...
1270 */
1271
1272 backendCheckSideChannel(snmp_fd, http->hostaddr);
1273
1274 /*
1275 * Send the next file in the job...
1276 */
1277
1278 request = ippNewRequest(IPP_SEND_DOCUMENT);
1279 request->request.op.version[0] = version / 10;
1280 request->request.op.version[1] = version % 10;
1281
1282 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1283 NULL, uri);
1284
1285 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
1286 job_id);
1287
1288 if (argv[2][0])
1289 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1290 "requesting-user-name", NULL, argv[2]);
1291
1292 if ((i + 1) == num_files)
1293 ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", 1);
1294
1295 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE,
1296 "document-format", NULL, content_type);
1297
1298 fprintf(stderr, "DEBUG: Sending file %d using chunking...\n", i + 1);
1299 http_status = cupsSendRequest(http, request, resource, 0);
1300 if (http_status == HTTP_CONTINUE && request->state == IPP_DATA &&
1301 (fd = open(files[i], O_RDONLY)) >= 0)
1302 {
1303 while ((bytes = read(fd, buffer, sizeof(buffer))) > 0)
1304 {
1305 if (cupsWriteRequestData(http, buffer, bytes) != HTTP_CONTINUE)
1306 break;
1307 else
1308 {
1309 /*
1310 * Check for side-channel requests...
1311 */
1312
1313 backendCheckSideChannel(snmp_fd, http->hostaddr);
1314 }
1315 }
1316
1317 close(fd);
1318 }
1319
1320 ippDelete(cupsGetResponse(http, resource));
1321 ippDelete(request);
1322
1323 if (cupsLastError() > IPP_OK_CONFLICT)
1324 {
1325 ipp_status = cupsLastError();
1326
1327 _cupsLangPrintFilter(stderr, "ERROR",
1328 _("Unable to add file to job: %s"),
1329 cupsLastErrorString());
1330 break;
1331 }
1332 }
1333 }
1334
1335 if (ipp_status <= IPP_OK_CONFLICT && argc > 6)
1336 {
1337 fprintf(stderr, "PAGE: 1 %d\n", copies_sup ? atoi(argv[4]) : 1);
1338 copies_remaining --;
1339 }
1340 else if (ipp_status == IPP_SERVICE_UNAVAILABLE ||
1341 ipp_status == IPP_PRINTER_BUSY)
1342 continue;
1343 else
1344 copies_remaining --;
1345
1346 /*
1347 * Wait for the job to complete...
1348 */
1349
1350 if (!job_id || !waitjob)
1351 continue;
1352
1353 _cupsLangPrintFilter(stderr, "INFO", _("Waiting for job to complete."));
1354
1355 for (delay = _cupsNextDelay(0, &prev_delay); !job_canceled;)
1356 {
1357 /*
1358 * Check for side-channel requests...
1359 */
1360
1361 backendCheckSideChannel(snmp_fd, http->hostaddr);
1362
1363 /*
1364 * Build an IPP_GET_JOB_ATTRIBUTES request...
1365 */
1366
1367 request = ippNewRequest(IPP_GET_JOB_ATTRIBUTES);
1368 request->request.op.version[0] = version / 10;
1369 request->request.op.version[1] = version % 10;
1370
1371 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1372 NULL, uri);
1373
1374 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
1375 job_id);
1376
1377 if (argv[2][0])
1378 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1379 "requesting-user-name", NULL, argv[2]);
1380
1381 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1382 "requested-attributes", sizeof(jattrs) / sizeof(jattrs[0]),
1383 NULL, jattrs);
1384
1385 /*
1386 * Do the request...
1387 */
1388
1389 httpReconnect(http);
1390 response = cupsDoRequest(http, request, resource);
1391 ipp_status = cupsLastError();
1392
1393 if (ipp_status == IPP_NOT_FOUND)
1394 {
1395 /*
1396 * Job has gone away and/or the server has no job history...
1397 */
1398
1399 ippDelete(response);
1400
1401 ipp_status = IPP_OK;
1402 break;
1403 }
1404
1405 if (ipp_status > IPP_OK_CONFLICT)
1406 {
1407 if (ipp_status != IPP_SERVICE_UNAVAILABLE &&
1408 ipp_status != IPP_PRINTER_BUSY)
1409 {
1410 ippDelete(response);
1411
1412 _cupsLangPrintFilter(stderr, "ERROR",
1413 _("Unable to get job attributes: %s"),
1414 cupsLastErrorString());
1415 break;
1416 }
1417 }
1418
1419 if (response)
1420 {
1421 if ((job_state = ippFindAttribute(response, "job-state",
1422 IPP_TAG_ENUM)) != NULL)
1423 {
1424 /*
1425 * Stop polling if the job is finished or pending-held...
1426 */
1427
1428 if (job_state->values[0].integer > IPP_JOB_STOPPED)
1429 {
1430 if ((job_sheets = ippFindAttribute(response,
1431 "job-media-sheets-completed",
1432 IPP_TAG_INTEGER)) != NULL)
1433 fprintf(stderr, "PAGE: total %d\n",
1434 job_sheets->values[0].integer);
1435
1436 ippDelete(response);
1437 break;
1438 }
1439 }
1440 else
1441 {
1442 /*
1443 * If the printer does not return a job-state attribute, it does not
1444 * conform to the IPP specification - break out immediately and fail
1445 * the job...
1446 */
1447
1448 fputs("DEBUG: No job-state available from printer - stopping queue.\n",
1449 stderr);
1450 ipp_status = IPP_INTERNAL_ERROR;
1451 break;
1452 }
1453 }
1454
1455 ippDelete(response);
1456
1457 /*
1458 * Wait before polling again...
1459 */
1460
1461 sleep(delay);
1462
1463 delay = _cupsNextDelay(delay, &prev_delay);
1464 }
1465 }
1466
1467 /*
1468 * Cancel the job as needed...
1469 */
1470
1471 if (job_canceled && job_id)
1472 cancel_job(http, uri, job_id, resource, argv[2], version);
1473
1474 /*
1475 * Check the printer state and report it if necessary...
1476 */
1477
1478 check_printer_state(http, uri, resource, argv[2], version, job_id);
1479
1480 /*
1481 * Collect the final page count as needed...
1482 */
1483
1484 if (have_supplies &&
1485 !backendSNMPSupplies(snmp_fd, http->hostaddr, &page_count, NULL) &&
1486 page_count > start_count)
1487 fprintf(stderr, "PAGE: total %d\n", page_count - start_count);
1488
1489 #ifdef HAVE_GSSAPI
1490 /*
1491 * See if we used Kerberos at all...
1492 */
1493
1494 if (http->gssctx)
1495 auth_info_required = "negotiate";
1496 #endif /* HAVE_GSSAPI */
1497
1498 /*
1499 * Free memory...
1500 */
1501
1502 cleanup:
1503
1504 cupsFreeOptions(num_options, options);
1505 _ppdCacheDestroy(pc);
1506
1507 httpClose(http);
1508
1509 ippDelete(supported);
1510
1511 /*
1512 * Remove the temporary file(s) if necessary...
1513 */
1514
1515 if (tmpfilename[0])
1516 unlink(tmpfilename);
1517
1518 #ifdef HAVE_LIBZ
1519 if (compression)
1520 {
1521 for (i = 0; i < num_files; i ++)
1522 unlink(files[i]);
1523 }
1524 #endif /* HAVE_LIBZ */
1525
1526 /*
1527 * Return the queue status...
1528 */
1529
1530 if (ipp_status == IPP_NOT_AUTHORIZED || ipp_status == IPP_FORBIDDEN ||
1531 ipp_status == IPP_AUTHENTICATION_CANCELED ||
1532 ipp_status <= IPP_OK_CONFLICT)
1533 fprintf(stderr, "ATTR: auth-info-required=%s\n", auth_info_required);
1534
1535 if (ipp_status == IPP_NOT_AUTHORIZED || ipp_status == IPP_FORBIDDEN ||
1536 ipp_status == IPP_AUTHENTICATION_CANCELED)
1537 return (CUPS_BACKEND_AUTH_REQUIRED);
1538 else if (ipp_status == IPP_INTERNAL_ERROR)
1539 return (CUPS_BACKEND_STOP);
1540 else if (ipp_status > IPP_OK_CONFLICT)
1541 return (CUPS_BACKEND_FAILED);
1542 else
1543 {
1544 _cupsLangPrintFilter(stderr, "INFO", _("Ready to print."));
1545 return (CUPS_BACKEND_OK);
1546 }
1547 }
1548
1549
1550 /*
1551 * 'cancel_job()' - Cancel a print job.
1552 */
1553
1554 static void
1555 cancel_job(http_t *http, /* I - HTTP connection */
1556 const char *uri, /* I - printer-uri */
1557 int id, /* I - job-id */
1558 const char *resource, /* I - Resource path */
1559 const char *user, /* I - requesting-user-name */
1560 int version) /* I - IPP version */
1561 {
1562 ipp_t *request; /* Cancel-Job request */
1563
1564
1565 _cupsLangPrintFilter(stderr, "INFO", _("Canceling print job."));
1566
1567 request = ippNewRequest(IPP_CANCEL_JOB);
1568 request->request.op.version[0] = version / 10;
1569 request->request.op.version[1] = version % 10;
1570
1571 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1572 NULL, uri);
1573 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", id);
1574
1575 if (user && user[0])
1576 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1577 "requesting-user-name", NULL, user);
1578
1579 /*
1580 * Do the request...
1581 */
1582
1583 ippDelete(cupsDoRequest(http, request, resource));
1584
1585 if (cupsLastError() > IPP_OK_CONFLICT)
1586 _cupsLangPrintFilter(stderr, "ERROR", _("Unable to cancel job: %s"),
1587 cupsLastErrorString());
1588 }
1589
1590
1591 /*
1592 * 'check_printer_state()' - Check the printer state.
1593 */
1594
1595 static ipp_pstate_t /* O - Current printer-state */
1596 check_printer_state(
1597 http_t *http, /* I - HTTP connection */
1598 const char *uri, /* I - Printer URI */
1599 const char *resource, /* I - Resource path */
1600 const char *user, /* I - Username, if any */
1601 int version, /* I - IPP version */
1602 int job_id)
1603 {
1604 ipp_t *request, /* IPP request */
1605 *response; /* IPP response */
1606 ipp_attribute_t *attr; /* Attribute in response */
1607 ipp_pstate_t printer_state = IPP_PRINTER_STOPPED;
1608 /* Current printer-state */
1609
1610
1611 /*
1612 * Send a Get-Printer-Attributes request and log the results...
1613 */
1614
1615 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
1616 request->request.op.version[0] = version / 10;
1617 request->request.op.version[1] = version % 10;
1618
1619 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1620 NULL, uri);
1621
1622 if (user && user[0])
1623 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1624 "requesting-user-name", NULL, user);
1625
1626 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1627 "requested-attributes",
1628 (int)(sizeof(pattrs) / sizeof(pattrs[0])), NULL, pattrs);
1629
1630 if ((response = cupsDoRequest(http, request, resource)) != NULL)
1631 {
1632 report_printer_state(response, job_id);
1633
1634 if ((attr = ippFindAttribute(response, "printer-state",
1635 IPP_TAG_ENUM)) != NULL)
1636 printer_state = (ipp_pstate_t)attr->values[0].integer;
1637
1638 ippDelete(response);
1639 }
1640
1641 /*
1642 * Return the printer-state value...
1643 */
1644
1645 return (printer_state);
1646 }
1647
1648
1649 #ifdef HAVE_LIBZ
1650 /*
1651 * 'compress_files()' - Compress print files...
1652 */
1653
1654 static void
1655 compress_files(int num_files, /* I - Number of files */
1656 char **files) /* I - Files */
1657 {
1658 int i, /* Looping var */
1659 fd; /* Temporary file descriptor */
1660 ssize_t bytes; /* Bytes read/written */
1661 size_t total; /* Total bytes read */
1662 cups_file_t *in, /* Input file */
1663 *out; /* Output file */
1664 struct stat outinfo; /* Output file information */
1665 char filename[1024], /* Temporary filename */
1666 buffer[32768]; /* Copy buffer */
1667
1668
1669 fprintf(stderr, "DEBUG: Compressing %d job files...\n", num_files);
1670 for (i = 0; i < num_files; i ++)
1671 {
1672 if ((fd = cupsTempFd(filename, sizeof(filename))) < 0)
1673 {
1674 _cupsLangPrintError("ERROR", _("Unable to create compressed print file"));
1675 exit(CUPS_BACKEND_FAILED);
1676 }
1677
1678 if ((out = cupsFileOpenFd(fd, "w9")) == NULL)
1679 {
1680 _cupsLangPrintError("ERROR", _("Unable to open compressed print file"));
1681 exit(CUPS_BACKEND_FAILED);
1682 }
1683
1684 if ((in = cupsFileOpen(files[i], "r")) == NULL)
1685 {
1686 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1687 cupsFileClose(out);
1688 exit(CUPS_BACKEND_FAILED);
1689 }
1690
1691 total = 0;
1692 while ((bytes = cupsFileRead(in, buffer, sizeof(buffer))) > 0)
1693 if (cupsFileWrite(out, buffer, bytes) < bytes)
1694 {
1695 _cupsLangPrintError("ERROR",
1696 _("Unable to generate compressed print file"));
1697 cupsFileClose(in);
1698 cupsFileClose(out);
1699 exit(CUPS_BACKEND_FAILED);
1700 }
1701 else
1702 total += bytes;
1703
1704 cupsFileClose(out);
1705 cupsFileClose(in);
1706
1707 files[i] = strdup(filename);
1708
1709 if (!stat(filename, &outinfo))
1710 fprintf(stderr,
1711 "DEBUG: File %d compressed to %.1f%% of original size, "
1712 CUPS_LLFMT " bytes...\n",
1713 i + 1, 100.0 * outinfo.st_size / total,
1714 CUPS_LLCAST outinfo.st_size);
1715 }
1716 }
1717 #endif /* HAVE_LIBZ */
1718
1719
1720 /*
1721 * 'monitor_printer()' - Monitor the printer state...
1722 */
1723
1724 static void * /* O - Thread exit code */
1725 monitor_printer(
1726 _cups_monitor_t *monitor) /* I - Monitoring data */
1727 {
1728 http_t *http; /* Connection to printer */
1729 ipp_t *request, /* IPP request */
1730 *response; /* IPP response */
1731 ipp_attribute_t *attr; /* Attribute in response */
1732 int delay, /* Current delay */
1733 prev_delay; /* Previous delay */
1734
1735
1736 /*
1737 * Make a copy of the printer connection...
1738 */
1739
1740 http = _httpCreate(monitor->hostname, monitor->port, NULL, monitor->encryption,
1741 AF_UNSPEC);
1742 cupsSetPasswordCB(password_cb);
1743
1744 /*
1745 * Loop until the job is canceled, aborted, or completed.
1746 */
1747
1748 delay = _cupsNextDelay(0, &prev_delay);
1749
1750 while (monitor->job_state < IPP_JOB_CANCELED && !job_canceled)
1751 {
1752 /*
1753 * Reconnect to the printer...
1754 */
1755
1756 if (!httpReconnect(http))
1757 {
1758 /*
1759 * Connected, so check on the printer state...
1760 */
1761
1762 monitor->printer_state = check_printer_state(http, monitor->uri,
1763 monitor->resource,
1764 monitor->user,
1765 monitor->version,
1766 monitor->job_id);
1767
1768 if (monitor->job_id > 0)
1769 {
1770 /*
1771 * Check the status of the job itself...
1772 */
1773
1774 request = ippNewRequest(IPP_GET_JOB_ATTRIBUTES);
1775 request->request.op.version[0] = monitor->version / 10;
1776 request->request.op.version[1] = monitor->version % 10;
1777
1778 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1779 NULL, monitor->uri);
1780 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
1781 monitor->job_id);
1782
1783 if (monitor->user && monitor->user[0])
1784 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1785 "requesting-user-name", NULL, monitor->user);
1786
1787 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1788 "requested-attributes",
1789 (int)(sizeof(jattrs) / sizeof(jattrs[0])), NULL, jattrs);
1790
1791 /*
1792 * Do the request...
1793 */
1794
1795 response = cupsDoRequest(http, request, monitor->resource);
1796
1797 if ((attr = ippFindAttribute(response, "job-state",
1798 IPP_TAG_ENUM)) != NULL)
1799 monitor->job_state = (ipp_jstate_t)attr->values[0].integer;
1800 else
1801 monitor->job_state = IPP_JOB_COMPLETED;
1802
1803 ippDelete(response);
1804 }
1805
1806 /*
1807 * Disconnect from the printer - we'll reconnect on the next poll...
1808 */
1809
1810 _httpDisconnect(http);
1811 }
1812
1813 /*
1814 * Sleep for N seconds...
1815 */
1816
1817 sleep(delay);
1818
1819 delay = _cupsNextDelay(delay, &prev_delay);
1820 }
1821
1822 /*
1823 * Cleanup and return...
1824 */
1825
1826 httpClose(http);
1827
1828 return (NULL);
1829 }
1830
1831
1832 /*
1833 * 'new_request()' - Create a new print creation or validation request.
1834 */
1835
1836 static ipp_t * /* O - Request data */
1837 new_request(
1838 ipp_op_t op, /* I - IPP operation code */
1839 int version, /* I - IPP version number */
1840 const char *uri, /* I - printer-uri value */
1841 const char *user, /* I - requesting-user-name value */
1842 const char *title, /* I - job-name value */
1843 int num_options, /* I - Number of options to send */
1844 cups_option_t *options, /* I - Options to send */
1845 const char *compression, /* I - compression value or NULL */
1846 int copies, /* I - copies value or 0 */
1847 const char *format, /* I - documet-format value or NULL */
1848 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
1849 ipp_attribute_t *media_col_sup, /* I - media-col-supported values */
1850 ipp_attribute_t *doc_handling_sup) /* I - multiple-document-handling-supported values */
1851 {
1852 int i; /* Looping var */
1853 ipp_t *request; /* Request data */
1854 const char *keyword; /* PWG keyword */
1855 _pwg_size_t *size; /* PWG media size */
1856 ipp_t *media_col, /* media-col value */
1857 *media_size; /* media-size value */
1858 const char *media_source, /* media-source value */
1859 *media_type, /* media-type value */
1860 *collate_str; /* multiple-document-handling value */
1861
1862
1863 /*
1864 * Create the IPP request...
1865 */
1866
1867 request = ippNewRequest(op);
1868 request->request.op.version[0] = version / 10;
1869 request->request.op.version[1] = version % 10;
1870
1871 fprintf(stderr, "DEBUG: %s IPP/%d.%d\n",
1872 ippOpString(request->request.op.operation_id),
1873 request->request.op.version[0],
1874 request->request.op.version[1]);
1875
1876 /*
1877 * Add standard attributes...
1878 */
1879
1880 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1881 NULL, uri);
1882 fprintf(stderr, "DEBUG: printer-uri=\"%s\"\n", uri);
1883
1884 if (user && *user)
1885 {
1886 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1887 "requesting-user-name", NULL, user);
1888 fprintf(stderr, "DEBUG: requesting-user-name=\"%s\"\n", user);
1889 }
1890
1891 if (title && *title)
1892 {
1893 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
1894 title);
1895 fprintf(stderr, "DEBUG: job-name=\"%s\"\n", title);
1896 }
1897
1898 if (format)
1899 {
1900 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE,
1901 "document-format", NULL, format);
1902 fprintf(stderr, "DEBUG: document-format=\"%s\"\n", format);
1903 }
1904
1905 #ifdef HAVE_LIBZ
1906 if (compression)
1907 {
1908 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1909 "compression", NULL, compression);
1910 fprintf(stderr, "DEBUG: compression=\"%s\"\n", compression);
1911 }
1912 #endif /* HAVE_LIBZ */
1913
1914 /*
1915 * Handle options on the command-line...
1916 */
1917
1918 if (num_options > 0)
1919 {
1920 if (pc)
1921 {
1922 /*
1923 * Send standard IPP attributes...
1924 */
1925
1926 if ((keyword = cupsGetOption("PageSize", num_options, options)) == NULL)
1927 keyword = cupsGetOption("media", num_options, options);
1928
1929 if ((size = _ppdCacheGetSize(pc, keyword)) != NULL)
1930 {
1931 /*
1932 * Add a media-col value...
1933 */
1934
1935 media_size = ippNew();
1936 ippAddInteger(media_size, IPP_TAG_ZERO, IPP_TAG_INTEGER,
1937 "x-dimension", size->width);
1938 ippAddInteger(media_size, IPP_TAG_ZERO, IPP_TAG_INTEGER,
1939 "y-dimension", size->length);
1940
1941 media_col = ippNew();
1942 ippAddCollection(media_col, IPP_TAG_ZERO, "media-size", media_size);
1943
1944 media_source = _ppdCacheGetSource(pc, cupsGetOption("InputSlot",
1945 num_options,
1946 options));
1947 media_type = _ppdCacheGetType(pc, cupsGetOption("MediaType",
1948 num_options,
1949 options));
1950
1951 for (i = 0; i < media_col_sup->num_values; i ++)
1952 {
1953 if (!strcmp(media_col_sup->values[i].string.text,
1954 "media-left-margin"))
1955 ippAddInteger(media_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
1956 "media-left-margin", size->left);
1957 else if (!strcmp(media_col_sup->values[i].string.text,
1958 "media-bottom-margin"))
1959 ippAddInteger(media_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
1960 "media-bottom-margin", size->left);
1961 else if (!strcmp(media_col_sup->values[i].string.text,
1962 "media-right-margin"))
1963 ippAddInteger(media_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
1964 "media-right-margin", size->left);
1965 else if (!strcmp(media_col_sup->values[i].string.text,
1966 "media-top-margin"))
1967 ippAddInteger(media_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
1968 "media-top-margin", size->left);
1969 else if (!strcmp(media_col_sup->values[i].string.text,
1970 "media-source") && media_source)
1971 ippAddString(media_col, IPP_TAG_ZERO, IPP_TAG_KEYWORD,
1972 "media-source", NULL, media_source);
1973 else if (!strcmp(media_col_sup->values[i].string.text,
1974 "media-type") && media_type)
1975 ippAddString(media_col, IPP_TAG_ZERO, IPP_TAG_KEYWORD,
1976 "media-type", NULL, media_type);
1977 }
1978
1979 ippAddCollection(request, IPP_TAG_JOB, "media-col", media_col);
1980 }
1981
1982 if ((keyword = cupsGetOption("output-bin", num_options,
1983 options)) == NULL)
1984 keyword = _ppdCacheGetBin(pc, cupsGetOption("OutputBin", num_options,
1985 options));
1986
1987 if (keyword)
1988 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "output-bin",
1989 NULL, keyword);
1990
1991 if ((keyword = cupsGetOption("output-mode", num_options,
1992 options)) != NULL)
1993 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "output-mode",
1994 NULL, keyword);
1995 else if ((keyword = cupsGetOption("ColorModel", num_options,
1996 options)) != NULL)
1997 {
1998 if (!strcasecmp(keyword, "Gray"))
1999 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "output-mode",
2000 NULL, "monochrome");
2001 else
2002 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "output-mode",
2003 NULL, "color");
2004 }
2005
2006 if ((keyword = cupsGetOption("print-quality", num_options,
2007 options)) != NULL)
2008 ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_ENUM, "print-quality",
2009 atoi(keyword));
2010 else if ((keyword = cupsGetOption("cupsPrintQuality", num_options,
2011 options)) != NULL)
2012 {
2013 if (!strcasecmp(keyword, "draft"))
2014 ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_ENUM, "print-quality",
2015 IPP_QUALITY_DRAFT);
2016 else if (!strcasecmp(keyword, "normal"))
2017 ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_ENUM, "print-quality",
2018 IPP_QUALITY_NORMAL);
2019 else if (!strcasecmp(keyword, "high"))
2020 ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_ENUM, "print-quality",
2021 IPP_QUALITY_HIGH);
2022 }
2023
2024 if ((keyword = cupsGetOption("sides", num_options, options)) != NULL)
2025 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "sides",
2026 NULL, keyword);
2027 else if (pc->sides_option &&
2028 (keyword = cupsGetOption(pc->sides_option, num_options,
2029 options)) != NULL)
2030 {
2031 if (!strcasecmp(keyword, pc->sides_1sided))
2032 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "sides",
2033 NULL, "one-sided");
2034 else if (!strcasecmp(keyword, pc->sides_2sided_long))
2035 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "sides",
2036 NULL, "two-sided-long-edge");
2037 if (!strcasecmp(keyword, pc->sides_2sided_short))
2038 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "sides",
2039 NULL, "two-sided-short-edge");
2040 }
2041
2042 if (doc_handling_sup &&
2043 (keyword = cupsGetOption("collate", num_options, options)) != NULL)
2044 {
2045 if (!strcasecmp(keyword, "true"))
2046 collate_str = "separate-documents-collated-copies";
2047 else
2048 collate_str = "separate-documents-uncollated-copies";
2049
2050 for (i = 0; i < doc_handling_sup->num_values; i ++)
2051 if (!strcmp(doc_handling_sup->values[i].string.text, collate_str))
2052 {
2053 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD,
2054 "multiple-document-handling", NULL, collate_str);
2055 break;
2056 }
2057 }
2058 }
2059 else
2060 {
2061 /*
2062 * When talking to another CUPS server, send all options...
2063 */
2064
2065 cupsEncodeOptions(request, num_options, options);
2066 }
2067
2068 if (copies > 1)
2069 ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_INTEGER, "copies", copies);
2070 }
2071
2072 return (request);
2073 }
2074
2075
2076 /*
2077 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
2078 */
2079
2080 static const char * /* O - Password */
2081 password_cb(const char *prompt) /* I - Prompt (not used) */
2082 {
2083 (void)prompt;
2084
2085 /*
2086 * Remember that we need to authenticate...
2087 */
2088
2089 auth_info_required = "username,password";
2090
2091 if (password && *password && password_tries < 3)
2092 {
2093 password_tries ++;
2094
2095 return (password);
2096 }
2097 else
2098 {
2099 /*
2100 * Give up after 3 tries or if we don't have a password to begin with...
2101 */
2102
2103 return (NULL);
2104 }
2105 }
2106
2107
2108 /*
2109 * 'report_attr()' - Report an IPP attribute value.
2110 */
2111
2112 static void
2113 report_attr(ipp_attribute_t *attr) /* I - Attribute */
2114 {
2115 int i; /* Looping var */
2116 char value[1024], /* Value string */
2117 *valptr, /* Pointer into value string */
2118 *attrptr; /* Pointer into attribute value */
2119
2120
2121 /*
2122 * Convert the attribute values into quoted strings...
2123 */
2124
2125 for (i = 0, valptr = value;
2126 i < attr->num_values && valptr < (value + sizeof(value) - 10);
2127 i ++)
2128 {
2129 if (i > 0)
2130 *valptr++ = ',';
2131
2132 switch (attr->value_tag)
2133 {
2134 case IPP_TAG_INTEGER :
2135 case IPP_TAG_ENUM :
2136 snprintf(valptr, sizeof(value) - (valptr - value), "%d",
2137 attr->values[i].integer);
2138 valptr += strlen(valptr);
2139 break;
2140
2141 case IPP_TAG_TEXT :
2142 case IPP_TAG_NAME :
2143 case IPP_TAG_KEYWORD :
2144 *valptr++ = '\"';
2145 for (attrptr = attr->values[i].string.text;
2146 *attrptr && valptr < (value + sizeof(value) - 10);
2147 attrptr ++)
2148 {
2149 if (*attrptr == '\\' || *attrptr == '\"')
2150 *valptr++ = '\\';
2151
2152 *valptr++ = *attrptr;
2153 }
2154 *valptr++ = '\"';
2155 break;
2156
2157 default :
2158 /*
2159 * Unsupported value type...
2160 */
2161
2162 return;
2163 }
2164 }
2165
2166 *valptr = '\0';
2167
2168 /*
2169 * Tell the scheduler about the new values...
2170 */
2171
2172 fprintf(stderr, "ATTR: %s=%s\n", attr->name, value);
2173 }
2174
2175
2176 /*
2177 * 'report_printer_state()' - Report the printer state.
2178 */
2179
2180 static int /* O - Number of reasons shown */
2181 report_printer_state(ipp_t *ipp, /* I - IPP response */
2182 int job_id) /* I - Current job ID */
2183 {
2184 int i; /* Looping var */
2185 int count; /* Count of reasons shown... */
2186 ipp_attribute_t *pa, /* printer-alert */
2187 *pam, /* printer-alert-message */
2188 *psm, /* printer-state-message */
2189 *reasons, /* printer-state-reasons */
2190 *marker; /* marker-* attributes */
2191 const char *reason; /* Current reason */
2192 const char *prefix; /* Prefix for STATE: line */
2193 char value[1024], /* State/message string */
2194 *valptr; /* Pointer into string */
2195 static int ipp_supplies = -1;
2196 /* Report supply levels? */
2197
2198
2199 /*
2200 * Report alerts and messages...
2201 */
2202
2203 if ((pa = ippFindAttribute(ipp, "printer-alert", IPP_TAG_TEXT)) != NULL)
2204 report_attr(pa);
2205
2206 if ((pam = ippFindAttribute(ipp, "printer-alert-message",
2207 IPP_TAG_TEXT)) != NULL)
2208 report_attr(pam);
2209
2210 if ((psm = ippFindAttribute(ipp, "printer-state-message",
2211 IPP_TAG_TEXT)) != NULL)
2212 {
2213 char *ptr; /* Pointer into message */
2214
2215
2216 strlcpy(value, "INFO: ", sizeof(value));
2217 for (ptr = psm->values[0].string.text, valptr = value + 6;
2218 *ptr && valptr < (value + sizeof(value) - 6);
2219 ptr ++)
2220 {
2221 if (*ptr < ' ' && *ptr > 0 && *ptr != '\t')
2222 {
2223 /*
2224 * Substitute "<XX>" for the control character; sprintf is safe because
2225 * we always leave 6 chars free at the end...
2226 */
2227
2228 sprintf(valptr, "<%02X>", *ptr);
2229 valptr += 4;
2230 }
2231 else
2232 *valptr++ = *ptr;
2233 }
2234
2235 *valptr++ = '\n';
2236 *valptr = '\0';
2237
2238 fputs(value, stderr);
2239 }
2240
2241 /*
2242 * Now report printer-state-reasons, filtering out some of the reasons we never
2243 * want to set...
2244 */
2245
2246 if ((reasons = ippFindAttribute(ipp, "printer-state-reasons",
2247 IPP_TAG_KEYWORD)) == NULL)
2248 return (0);
2249
2250 value[0] = '\0';
2251 prefix = "STATE: ";
2252
2253 for (i = 0, count = 0, valptr = value; i < reasons->num_values; i ++)
2254 {
2255 reason = reasons->values[i].string.text;
2256
2257 if (strcmp(reason, "paused") &&
2258 strcmp(reason, "com.apple.print.recoverable-warning"))
2259 {
2260 strlcpy(valptr, prefix, sizeof(value) - (valptr - value) - 1);
2261 valptr += strlen(valptr);
2262 strlcpy(valptr, reason, sizeof(value) - (valptr - value) - 1);
2263 valptr += strlen(valptr);
2264
2265 prefix = ",";
2266 }
2267 }
2268
2269 if (value[0])
2270 {
2271 *valptr++ = '\n';
2272 *valptr = '\0';
2273 fputs(value, stderr);
2274 }
2275
2276 /*
2277 * Relay the current marker-* attribute values...
2278 */
2279
2280 if (ipp_supplies < 0)
2281 {
2282 ppd_file_t *ppd; /* PPD file */
2283 ppd_attr_t *ppdattr; /* Attribute in PPD file */
2284
2285 if ((ppd = ppdOpenFile(getenv("PPD"))) != NULL &&
2286 (ppdattr = ppdFindAttr(ppd, "cupsIPPSupplies", NULL)) != NULL &&
2287 ppdattr->value && strcasecmp(ppdattr->value, "true"))
2288 ipp_supplies = 0;
2289 else
2290 ipp_supplies = 1;
2291
2292 ppdClose(ppd);
2293 }
2294
2295 if (ipp_supplies > 0)
2296 {
2297 if ((marker = ippFindAttribute(ipp, "marker-colors", IPP_TAG_NAME)) != NULL)
2298 report_attr(marker);
2299 if ((marker = ippFindAttribute(ipp, "marker-high-levels",
2300 IPP_TAG_INTEGER)) != NULL)
2301 report_attr(marker);
2302 if ((marker = ippFindAttribute(ipp, "marker-levels",
2303 IPP_TAG_INTEGER)) != NULL)
2304 report_attr(marker);
2305 if ((marker = ippFindAttribute(ipp, "marker-low-levels",
2306 IPP_TAG_INTEGER)) != NULL)
2307 report_attr(marker);
2308 if ((marker = ippFindAttribute(ipp, "marker-message",
2309 IPP_TAG_TEXT)) != NULL)
2310 report_attr(marker);
2311 if ((marker = ippFindAttribute(ipp, "marker-names", IPP_TAG_NAME)) != NULL)
2312 report_attr(marker);
2313 if ((marker = ippFindAttribute(ipp, "marker-types",
2314 IPP_TAG_KEYWORD)) != NULL)
2315 report_attr(marker);
2316 }
2317
2318 return (count);
2319 }
2320
2321
2322 /*
2323 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
2324 */
2325
2326 static void
2327 sigterm_handler(int sig) /* I - Signal */
2328 {
2329 (void)sig; /* remove compiler warnings... */
2330
2331 if (!job_canceled)
2332 {
2333 /*
2334 * Flag that the job should be cancelled...
2335 */
2336
2337 job_canceled = 1;
2338 return;
2339 }
2340
2341 /*
2342 * The scheduler already tried to cancel us once, now just terminate
2343 * after removing our temp files!
2344 */
2345
2346 if (tmpfilename[0])
2347 unlink(tmpfilename);
2348
2349 exit(1);
2350 }
2351
2352
2353 /*
2354 * End of "$Id: ipp.c 7948 2008-09-17 00:04:12Z mike $".
2355 */