]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/ipp.c
Merge changes from CUPS 1.5b1-r9798.
[thirdparty/cups.git] / backend / ipp.c
1 /*
2 * "$Id: ipp.c 9759 2011-05-11 03:24:33Z 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 * run_as_user() - Run the IPP backend as the printing user.
30 * sigterm_handler() - Handle 'terminate' signals that stop the backend.
31 */
32
33 /*
34 * Include necessary headers.
35 */
36
37 #include "backend-private.h"
38 #include <cups/array-private.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <sys/wait.h>
42 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
43 # include <xpc/xpc.h>
44 # define kPMPrintUIToolAgent "com.apple.printuitool.agent"
45 # define kPMStartJob 100
46 # define kPMWaitForJob 101
47 extern void xpc_connection_set_target_uid(xpc_connection_t connection,
48 uid_t uid);
49 #endif /* HAVE_GSSAPI && HAVE_XPC */
50
51
52 /*
53 * Types...
54 */
55
56 typedef struct _cups_monitor_s /**** Monitoring data ****/
57 {
58 const char *uri, /* Printer URI */
59 *hostname, /* Hostname */
60 *user, /* Username */
61 *resource; /* Resource path */
62 int port, /* Port number */
63 version, /* IPP version */
64 job_id; /* Job ID for submitted job */
65 http_encryption_t encryption; /* Use encryption? */
66 ipp_jstate_t job_state; /* Current job state */
67 ipp_pstate_t printer_state; /* Current printer state */
68 } _cups_monitor_t;
69
70
71 /*
72 * Globals...
73 */
74
75 static const char *auth_info_required;
76 /* New auth-info-required value */
77 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
78 static int child_pid = 0; /* Child process ID */
79 #endif /* HAVE_GSSAPI && HAVE_XPC */
80 static const char * const jattrs[] = /* Job attributes we want */
81 {
82 "job-impressions-completed",
83 "job-media-sheets-completed",
84 "job-state",
85 "job-state-reasons"
86 };
87 static int job_canceled = 0;
88 /* Job cancelled? */
89 static char *password = NULL;
90 /* Password for device URI */
91 static int password_tries = 0;
92 /* Password tries */
93 static const char * const pattrs[] = /* Printer attributes we want */
94 {
95 "copies-supported",
96 "cups-version",
97 "document-format-supported",
98 "marker-colors",
99 "marker-high-levels",
100 "marker-levels",
101 "marker-low-levels",
102 "marker-message",
103 "marker-names",
104 "marker-types",
105 "media-col-supported",
106 "multiple-document-handling-supported",
107 "operations-supported",
108 "printer-alert",
109 "printer-alert-description",
110 "printer-is-accepting-jobs",
111 "printer-state",
112 "printer-state-message",
113 "printer-state-reasons",
114 };
115 static const char * const remote_job_states[] =
116 { /* Remote job state keywords */
117 "+cups-remote-pending",
118 "+cups-remote-pending-held",
119 "+cups-remote-processing",
120 "+cups-remote-stopped",
121 "+cups-remote-canceled",
122 "+cups-remote-aborted",
123 "+cups-remote-completed"
124 };
125 static _cups_mutex_t report_mutex = _CUPS_MUTEX_INITIALIZER;
126 /* Mutex to control access */
127 static int num_attr_cache = 0;
128 /* Number of cached attributes */
129 static cups_option_t *attr_cache = NULL;
130 /* Cached attributes */
131 static cups_array_t *state_reasons; /* Array of printe-state-reasons keywords */
132 static char tmpfilename[1024] = "";
133 /* Temporary spool file name */
134
135
136 /*
137 * Local functions...
138 */
139
140 static void cancel_job(http_t *http, const char *uri, int id,
141 const char *resource, const char *user,
142 int version);
143 static ipp_pstate_t check_printer_state(http_t *http, const char *uri,
144 const char *resource,
145 const char *user, int version);
146 #ifdef HAVE_LIBZ
147 static void compress_files(int num_files, char **files);
148 #endif /* HAVE_LIBZ */
149 static void *monitor_printer(_cups_monitor_t *monitor);
150 static ipp_t *new_request(ipp_op_t op, int version, const char *uri,
151 const char *user, const char *title,
152 int num_options, cups_option_t *options,
153 const char *compression, int copies,
154 const char *format, _ppd_cache_t *pc,
155 ipp_attribute_t *media_col_sup,
156 ipp_attribute_t *doc_handling_sup);
157 static const char *password_cb(const char *);
158 static void report_attr(ipp_attribute_t *attr);
159 static void report_printer_state(ipp_t *ipp);
160 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
161 static int run_as_user(int argc, char *argv[], uid_t uid,
162 const char *device_uri, int fd);
163 #endif /* HAVE_GSSAPI && HAVE_XPC */
164 static void sigterm_handler(int sig);
165 static void update_reasons(ipp_attribute_t *attr, const char *s);
166
167
168 /*
169 * 'main()' - Send a file to the printer or server.
170 *
171 * Usage:
172 *
173 * printer-uri job-id user title copies options [file]
174 */
175
176 int /* O - Exit status */
177 main(int argc, /* I - Number of command-line args */
178 char *argv[]) /* I - Command-line arguments */
179 {
180 int i; /* Looping var */
181 int send_options; /* Send job options? */
182 int num_options; /* Number of printer options */
183 cups_option_t *options; /* Printer options */
184 const char *device_uri; /* Device URI */
185 char scheme[255], /* Scheme in URI */
186 hostname[1024], /* Hostname */
187 username[255], /* Username info */
188 resource[1024], /* Resource info (printer name) */
189 addrname[256], /* Address name */
190 *optptr, /* Pointer to URI options */
191 *name, /* Name of option */
192 *value, /* Value of option */
193 sep; /* Separator character */
194 http_addrlist_t *addrlist; /* Address of printer */
195 int snmp_fd, /* SNMP socket */
196 start_count, /* Page count via SNMP at start */
197 page_count, /* Page count via SNMP */
198 have_supplies; /* Printer supports supply levels? */
199 int num_files; /* Number of files to print */
200 char **files, /* Files to print */
201 *compatfile = NULL; /* Compatibility filename */
202 off_t compatsize = 0; /* Size of compatibility file */
203 int port; /* Port number (not used) */
204 char portname[255]; /* Port name */
205 char uri[HTTP_MAX_URI]; /* Updated URI without user/pass */
206 http_status_t http_status; /* Status of HTTP request */
207 ipp_status_t ipp_status; /* Status of IPP request */
208 http_t *http; /* HTTP connection */
209 ipp_t *request, /* IPP request */
210 *response, /* IPP response */
211 *supported; /* get-printer-attributes response */
212 time_t start_time; /* Time of first connect */
213 int contimeout; /* Connection timeout */
214 int delay, /* Delay for retries */
215 prev_delay; /* Previous delay */
216 const char *compression; /* Compression mode */
217 int waitjob, /* Wait for job complete? */
218 waitprinter; /* Wait for printer ready? */
219 _cups_monitor_t monitor; /* Monitoring data */
220 ipp_attribute_t *job_id_attr; /* job-id attribute */
221 int job_id; /* job-id value */
222 ipp_attribute_t *job_sheets; /* job-media-sheets-completed */
223 ipp_attribute_t *job_state; /* job-state */
224 ipp_attribute_t *copies_sup; /* copies-supported */
225 ipp_attribute_t *cups_version; /* cups-version */
226 ipp_attribute_t *format_sup; /* document-format-supported */
227 ipp_attribute_t *media_col_sup; /* media-col-supported */
228 ipp_attribute_t *operations_sup; /* operations-supported */
229 ipp_attribute_t *doc_handling_sup; /* multiple-document-handling-supported */
230 ipp_attribute_t *printer_state; /* printer-state attribute */
231 ipp_attribute_t *printer_accepting; /* printer-is-accepting-jobs */
232 int validate_job; /* Does printer support Validate-Job? */
233 int copies, /* Number of copies for job */
234 copies_remaining; /* Number of copies remaining */
235 const char *content_type, /* CONTENT_TYPE environment variable */
236 *final_content_type, /* FINAL_CONTENT_TYPE environment var */
237 *document_format; /* document-format value */
238 int fd; /* File descriptor */
239 off_t bytes = 0; /* Bytes copied */
240 char buffer[16384]; /* Copy buffer */
241 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
242 struct sigaction action; /* Actions for POSIX signals */
243 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
244 int version; /* IPP version */
245 ppd_file_t *ppd; /* PPD file */
246 _ppd_cache_t *pc; /* PPD cache and mapping data */
247 fd_set input; /* Input set for select() */
248
249
250 /*
251 * Make sure status messages are not buffered...
252 */
253
254 setbuf(stderr, NULL);
255
256 /*
257 * Ignore SIGPIPE and catch SIGTERM signals...
258 */
259
260 #ifdef HAVE_SIGSET
261 sigset(SIGPIPE, SIG_IGN);
262 sigset(SIGTERM, sigterm_handler);
263 #elif defined(HAVE_SIGACTION)
264 memset(&action, 0, sizeof(action));
265 action.sa_handler = SIG_IGN;
266 sigaction(SIGPIPE, &action, NULL);
267
268 sigemptyset(&action.sa_mask);
269 sigaddset(&action.sa_mask, SIGTERM);
270 action.sa_handler = sigterm_handler;
271 sigaction(SIGTERM, &action, NULL);
272 #else
273 signal(SIGPIPE, SIG_IGN);
274 signal(SIGTERM, sigterm_handler);
275 #endif /* HAVE_SIGSET */
276
277 /*
278 * Check command-line...
279 */
280
281 if (argc == 1)
282 {
283 char *s;
284
285 if ((s = strrchr(argv[0], '/')) != NULL)
286 s ++;
287 else
288 s = argv[0];
289
290 printf("network %s \"Unknown\" \"%s (%s)\"\n",
291 s, _cupsLangString(cupsLangDefault(),
292 _("Internet Printing Protocol")), s);
293 return (CUPS_BACKEND_OK);
294 }
295 else if (argc < 6)
296 {
297 _cupsLangPrintf(stderr,
298 _("Usage: %s job-id user title copies options [file]"),
299 argv[0]);
300 return (CUPS_BACKEND_STOP);
301 }
302
303 /*
304 * Get the device URI...
305 */
306
307 while ((device_uri = cupsBackendDeviceURI(argv)) == NULL)
308 {
309 _cupsLangPrintFilter(stderr, "INFO", _("Unable to locate printer."));
310 sleep(10);
311
312 if (getenv("CLASS") != NULL)
313 return (CUPS_BACKEND_FAILED);
314 }
315
316 if ((auth_info_required = getenv("AUTH_INFO_REQUIRED")) == NULL)
317 auth_info_required = "none";
318
319 state_reasons = _cupsArrayNewStrings(getenv("PRINTER_STATE_REASONS"));
320
321 #ifdef HAVE_GSSAPI
322 /*
323 * For Kerberos, become the printing user (if we can) to get the credentials
324 * that way.
325 */
326
327 if (!getuid() && (value = getenv("AUTH_UID")) != NULL)
328 {
329 uid_t uid = (uid_t)atoi(value);
330 /* User ID */
331
332 # ifdef HAVE_XPC
333 if (uid > 0)
334 {
335 if (argc == 6)
336 return (run_as_user(argc, argv, uid, device_uri, 0));
337 else
338 {
339 int status = 0; /* Exit status */
340
341 for (i = 6; i < argc && !status && !job_canceled; i ++)
342 {
343 if ((fd = open(argv[i], O_RDONLY)) >= 0)
344 {
345 status = run_as_user(argc, argv, uid, device_uri, fd);
346 close(fd);
347 }
348 else
349 {
350 _cupsLangPrintError("ERROR", _("Unable to open print file"));
351 status = CUPS_BACKEND_FAILED;
352 }
353 }
354
355 return (status);
356 }
357 }
358
359 # else /* No XPC, just try to run as the user ID */
360 if (uid > 0)
361 seteuid(uid);
362 # endif /* HAVE_XPC */
363 }
364 #endif /* HAVE_GSSAPI */
365
366 /*
367 * Get the (final) content type...
368 */
369
370 if ((content_type = getenv("CONTENT_TYPE")) == NULL)
371 content_type = "application/octet-stream";
372
373 if ((final_content_type = getenv("FINAL_CONTENT_TYPE")) == NULL)
374 {
375 final_content_type = content_type;
376
377 if (!strncmp(final_content_type, "printer/", 8))
378 final_content_type = "application/vnd.cups-raw";
379 }
380
381 /*
382 * Extract the hostname and printer name from the URI...
383 */
384
385 httpSeparateURI(HTTP_URI_CODING_ALL, device_uri, scheme, sizeof(scheme),
386 username, sizeof(username), hostname, sizeof(hostname), &port,
387 resource, sizeof(resource));
388
389 if (!port)
390 port = IPP_PORT; /* Default to port 631 */
391
392 if (!strcmp(scheme, "https"))
393 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS);
394 else
395 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED);
396
397 /*
398 * See if there are any options...
399 */
400
401 compression = NULL;
402 version = 20;
403 waitjob = 1;
404 waitprinter = 1;
405 contimeout = 7 * 24 * 60 * 60;
406
407 if ((optptr = strchr(resource, '?')) != NULL)
408 {
409 /*
410 * Yup, terminate the device name string and move to the first
411 * character of the optptr...
412 */
413
414 *optptr++ = '\0';
415
416 /*
417 * Then parse the optptr...
418 */
419
420 while (*optptr)
421 {
422 /*
423 * Get the name...
424 */
425
426 name = optptr;
427
428 while (*optptr && *optptr != '=' && *optptr != '+' && *optptr != '&')
429 optptr ++;
430
431 if ((sep = *optptr) != '\0')
432 *optptr++ = '\0';
433
434 if (sep == '=')
435 {
436 /*
437 * Get the value...
438 */
439
440 value = optptr;
441
442 while (*optptr && *optptr != '+' && *optptr != '&')
443 optptr ++;
444
445 if (*optptr)
446 *optptr++ = '\0';
447 }
448 else
449 value = (char *)"";
450
451 /*
452 * Process the option...
453 */
454
455 if (!_cups_strcasecmp(name, "waitjob"))
456 {
457 /*
458 * Wait for job completion?
459 */
460
461 waitjob = !_cups_strcasecmp(value, "on") ||
462 !_cups_strcasecmp(value, "yes") ||
463 !_cups_strcasecmp(value, "true");
464 }
465 else if (!_cups_strcasecmp(name, "waitprinter"))
466 {
467 /*
468 * Wait for printer idle?
469 */
470
471 waitprinter = !_cups_strcasecmp(value, "on") ||
472 !_cups_strcasecmp(value, "yes") ||
473 !_cups_strcasecmp(value, "true");
474 }
475 else if (!_cups_strcasecmp(name, "encryption"))
476 {
477 /*
478 * Enable/disable encryption?
479 */
480
481 if (!_cups_strcasecmp(value, "always"))
482 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS);
483 else if (!_cups_strcasecmp(value, "required"))
484 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
485 else if (!_cups_strcasecmp(value, "never"))
486 cupsSetEncryption(HTTP_ENCRYPT_NEVER);
487 else if (!_cups_strcasecmp(value, "ifrequested"))
488 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED);
489 else
490 {
491 _cupsLangPrintFilter(stderr, "ERROR",
492 _("Unknown encryption option value: \"%s\"."),
493 value);
494 }
495 }
496 else if (!_cups_strcasecmp(name, "version"))
497 {
498 if (!strcmp(value, "1.0"))
499 version = 10;
500 else if (!strcmp(value, "1.1"))
501 version = 11;
502 else if (!strcmp(value, "2.0"))
503 version = 20;
504 else if (!strcmp(value, "2.1"))
505 version = 21;
506 else if (!strcmp(value, "2.2"))
507 version = 22;
508 else
509 {
510 _cupsLangPrintFilter(stderr, "ERROR",
511 _("Unknown version option value: \"%s\"."),
512 value);
513 }
514 }
515 #ifdef HAVE_LIBZ
516 else if (!_cups_strcasecmp(name, "compression"))
517 {
518 if (!_cups_strcasecmp(value, "true") || !_cups_strcasecmp(value, "yes") ||
519 !_cups_strcasecmp(value, "on") || !_cups_strcasecmp(value, "gzip"))
520 compression = "gzip";
521 }
522 #endif /* HAVE_LIBZ */
523 else if (!_cups_strcasecmp(name, "contimeout"))
524 {
525 /*
526 * Set the connection timeout...
527 */
528
529 if (atoi(value) > 0)
530 contimeout = atoi(value);
531 }
532 else
533 {
534 /*
535 * Unknown option...
536 */
537
538 _cupsLangPrintFilter(stderr, "ERROR",
539 _("Unknown option \"%s\" with value \"%s\"."),
540 name, value);
541 }
542 }
543 }
544
545 /*
546 * If we have 7 arguments, print the file named on the command-line.
547 * Otherwise, copy stdin to a temporary file and print the temporary
548 * file.
549 */
550
551 if (argc == 6)
552 {
553 num_files = 0;
554 send_options = !_cups_strcasecmp(final_content_type, "application/pdf") ||
555 !_cups_strcasecmp(final_content_type, "application/vnd.cups-pdf") ||
556 !_cups_strncasecmp(final_content_type, "image/", 6);
557
558 fputs("DEBUG: Sending stdin for job...\n", stderr);
559 }
560 else
561 {
562 /*
563 * Point to the files on the command-line...
564 */
565
566 num_files = argc - 6;
567 files = argv + 6;
568 send_options = 1;
569
570 #ifdef HAVE_LIBZ
571 if (compression)
572 compress_files(num_files, files);
573 #endif /* HAVE_LIBZ */
574
575 fprintf(stderr, "DEBUG: %d files to send in job...\n", num_files);
576 }
577
578 /*
579 * Set the authentication info, if any...
580 */
581
582 cupsSetPasswordCB(password_cb);
583
584 if (username[0])
585 {
586 /*
587 * Use authenticaion information in the device URI...
588 */
589
590 if ((password = strchr(username, ':')) != NULL)
591 *password++ = '\0';
592
593 cupsSetUser(username);
594 }
595 else
596 {
597 /*
598 * Try loading authentication information from the environment.
599 */
600
601 const char *ptr = getenv("AUTH_USERNAME");
602
603 if (ptr)
604 cupsSetUser(ptr);
605
606 password = getenv("AUTH_PASSWORD");
607 }
608
609 /*
610 * Try finding the remote server...
611 */
612
613 start_time = time(NULL);
614
615 sprintf(portname, "%d", port);
616
617 update_reasons(NULL, "+connecting-to-device");
618 fprintf(stderr, "DEBUG: Looking up \"%s\"...\n", hostname);
619
620 while ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL)
621 {
622 _cupsLangPrintFilter(stderr, "INFO",
623 _("Unable to locate printer \"%s\"."), hostname);
624 sleep(10);
625
626 if (getenv("CLASS") != NULL)
627 {
628 update_reasons(NULL, "-connecting-to-device");
629 return (CUPS_BACKEND_STOP);
630 }
631 }
632
633 http = _httpCreate(hostname, port, addrlist, cupsEncryption(), AF_UNSPEC);
634
635 /*
636 * See if the printer supports SNMP...
637 */
638
639 if ((snmp_fd = _cupsSNMPOpen(addrlist->addr.addr.sa_family)) >= 0)
640 {
641 have_supplies = !backendSNMPSupplies(snmp_fd, &(addrlist->addr),
642 &start_count, NULL);
643 }
644 else
645 have_supplies = start_count = 0;
646
647 /*
648 * Wait for data from the filter...
649 */
650
651 if (num_files == 0)
652 {
653 if (!backendWaitLoop(snmp_fd, &(addrlist->addr), 0, backendNetworkSideCB))
654 return (CUPS_BACKEND_OK);
655 else if ((bytes = read(0, buffer, sizeof(buffer))) <= 0)
656 return (CUPS_BACKEND_OK);
657 }
658
659 /*
660 * Try connecting to the remote server...
661 */
662
663 delay = _cupsNextDelay(0, &prev_delay);
664
665 do
666 {
667 fprintf(stderr, "DEBUG: Connecting to %s:%d\n", hostname, port);
668 _cupsLangPrintFilter(stderr, "INFO", _("Connecting to printer."));
669
670 if (httpReconnect(http))
671 {
672 int error = errno; /* Connection error */
673
674 if (http->status == HTTP_PKI_ERROR)
675 update_reasons(NULL, "+cups-certificate-error");
676
677 if (job_canceled)
678 break;
679
680 if (getenv("CLASS") != NULL)
681 {
682 /*
683 * If the CLASS environment variable is set, the job was submitted
684 * to a class and not to a specific queue. In this case, we want
685 * to abort immediately so that the job can be requeued on the next
686 * available printer in the class.
687 */
688
689 _cupsLangPrintFilter(stderr, "INFO",
690 _("Unable to contact printer, queuing on next "
691 "printer in class."));
692
693 /*
694 * Sleep 5 seconds to keep the job from requeuing too rapidly...
695 */
696
697 sleep(5);
698
699 update_reasons(NULL, "-connecting-to-device");
700
701 return (CUPS_BACKEND_FAILED);
702 }
703
704 fprintf(stderr, "DEBUG: Connection error: %s\n", strerror(errno));
705
706 if (errno == ECONNREFUSED || errno == EHOSTDOWN ||
707 errno == EHOSTUNREACH)
708 {
709 if (contimeout && (time(NULL) - start_time) > contimeout)
710 {
711 _cupsLangPrintFilter(stderr, "ERROR",
712 _("The printer is not responding."));
713 update_reasons(NULL, "-connecting-to-device");
714 return (CUPS_BACKEND_FAILED);
715 }
716
717 switch (error)
718 {
719 case EHOSTDOWN :
720 _cupsLangPrintFilter(stderr, "WARNING",
721 _("The printer may not exist or "
722 "is unavailable at this time."));
723 break;
724
725 case EHOSTUNREACH :
726 _cupsLangPrintFilter(stderr, "WARNING",
727 _("The printer is unreachable at this "
728 "time."));
729 break;
730
731 case ECONNREFUSED :
732 default :
733 _cupsLangPrintFilter(stderr, "WARNING",
734 _("The printer is busy."));
735 break;
736 }
737
738 sleep(delay);
739
740 delay = _cupsNextDelay(delay, &prev_delay);
741 }
742 else
743 {
744 _cupsLangPrintFilter(stderr, "ERROR",
745 _("The printer is not responding."));
746 sleep(30);
747 }
748
749 if (job_canceled)
750 break;
751 }
752 else
753 update_reasons(NULL, "-cups-certificate-error");
754 }
755 while (http->fd < 0);
756
757 if (job_canceled || !http)
758 return (CUPS_BACKEND_FAILED);
759
760 update_reasons(NULL, "-connecting-to-device");
761 _cupsLangPrintFilter(stderr, "INFO", _("Connected to printer."));
762
763 fprintf(stderr, "DEBUG: Connected to %s:%d...\n",
764 httpAddrString(http->hostaddr, addrname, sizeof(addrname)),
765 _httpAddrPort(http->hostaddr));
766
767 /*
768 * Build a URI for the printer and fill the standard IPP attributes for
769 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
770 * might contain username:password information...
771 */
772
773 httpAssembleURI(HTTP_URI_CODING_ALL, uri, sizeof(uri), scheme, NULL, hostname,
774 port, resource);
775
776 /*
777 * First validate the destination and see if the device supports multiple
778 * copies...
779 */
780
781 copies_sup = NULL;
782 cups_version = NULL;
783 format_sup = NULL;
784 media_col_sup = NULL;
785 supported = NULL;
786 operations_sup = NULL;
787 doc_handling_sup = NULL;
788 validate_job = 0;
789
790 do
791 {
792 /*
793 * Check for side-channel requests...
794 */
795
796 backendCheckSideChannel(snmp_fd, http->hostaddr);
797
798 /*
799 * Build the IPP request...
800 */
801
802 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
803 request->request.op.version[0] = version / 10;
804 request->request.op.version[1] = version % 10;
805
806 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
807 NULL, uri);
808
809 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
810 "requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]),
811 NULL, pattrs);
812
813 /*
814 * Do the request...
815 */
816
817 fputs("DEBUG: Getting supported attributes...\n", stderr);
818
819 if (http->version < HTTP_1_1)
820 {
821 fprintf(stderr, "DEBUG: Printer responded with HTTP version %d.%d.\n",
822 http->version / 100, http->version % 100);
823 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
824 "cups-ipp-wrong-http-version");
825 }
826
827 supported = cupsDoRequest(http, request, resource);
828 ipp_status = cupsLastError();
829
830 fprintf(stderr, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
831 ippErrorString(ipp_status), cupsLastErrorString());
832
833 if (ipp_status > IPP_OK_CONFLICT)
834 {
835 fprintf(stderr, "DEBUG: Get-Printer-Attributes returned %s.\n",
836 ippErrorString(ipp_status));
837
838 if (ipp_status == IPP_PRINTER_BUSY ||
839 ipp_status == IPP_SERVICE_UNAVAILABLE)
840 {
841 if (contimeout && (time(NULL) - start_time) > contimeout)
842 {
843 _cupsLangPrintFilter(stderr, "ERROR",
844 _("The printer is not responding."));
845 return (CUPS_BACKEND_FAILED);
846 }
847
848 _cupsLangPrintFilter(stderr, "INFO", _("The printer is busy."));
849
850 report_printer_state(supported);
851
852 sleep(delay);
853
854 delay = _cupsNextDelay(delay, &prev_delay);
855 }
856 else if ((ipp_status == IPP_BAD_REQUEST ||
857 ipp_status == IPP_VERSION_NOT_SUPPORTED) && version > 10)
858 {
859 /*
860 * Switch to IPP/1.1 or IPP/1.0...
861 */
862
863 if (version >= 20)
864 {
865 _cupsLangPrintFilter(stderr, "INFO",
866 _("Printer does not support IPP/%d.%d, trying "
867 "IPP/%s."), version / 10, version % 10, "1.1");
868 version = 11;
869 }
870 else
871 {
872 _cupsLangPrintFilter(stderr, "INFO",
873 _("Printer does not support IPP/%d.%d, trying "
874 "IPP/%s."), version / 10, version % 10, "1.0");
875 version = 10;
876 }
877
878 httpReconnect(http);
879 }
880 else if (ipp_status == IPP_NOT_FOUND)
881 {
882 _cupsLangPrintFilter(stderr, "ERROR",
883 _("The printer URI is incorrect or no longer "
884 "exists."));
885
886 ippDelete(supported);
887
888 return (CUPS_BACKEND_STOP);
889 }
890 else if (ipp_status == IPP_NOT_AUTHORIZED || ipp_status == IPP_FORBIDDEN)
891 {
892 if (!strncmp(httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE),
893 "Negotiate", 9))
894 auth_info_required = "negotiate";
895
896 fprintf(stderr, "ATTR: auth-info-required=%s\n", auth_info_required);
897 return (CUPS_BACKEND_AUTH_REQUIRED);
898 }
899 else
900 {
901 _cupsLangPrintFilter(stderr, "ERROR",
902 _("Unable to get printer status."));
903 sleep(10);
904 }
905
906 ippDelete(supported);
907 supported = NULL;
908 continue;
909 }
910
911 if (!getenv("CLASS"))
912 {
913 /*
914 * Check printer-is-accepting-jobs = false and printer-state-reasons for the
915 * "spool-area-full" keyword...
916 */
917
918 int busy = 0;
919
920 if ((printer_accepting = ippFindAttribute(supported,
921 "printer-is-accepting-jobs",
922 IPP_TAG_BOOLEAN)) != NULL &&
923 !printer_accepting->values[0].boolean)
924 busy = 1;
925 else if (!printer_accepting)
926 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
927 "cups-ipp-missing-printer-is-accepting-jobs");
928
929 if ((printer_state = ippFindAttribute(supported,
930 "printer-state-reasons",
931 IPP_TAG_KEYWORD)) != NULL && !busy)
932 {
933 for (i = 0; i < printer_state->num_values; i ++)
934 if (!strcmp(printer_state->values[0].string.text,
935 "spool-area-full") ||
936 !strncmp(printer_state->values[0].string.text, "spool-area-full-",
937 16))
938 {
939 busy = 1;
940 break;
941 }
942 }
943 else
944 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
945 "cups-ipp-missing-printer-state-reasons");
946
947 if (busy)
948 {
949 _cupsLangPrintFilter(stderr, "INFO", _("The printer is busy."));
950
951 report_printer_state(supported);
952
953 sleep(delay);
954
955 delay = _cupsNextDelay(delay, &prev_delay);
956
957 ippDelete(supported);
958 supported = NULL;
959 continue;
960 }
961 }
962
963 /*
964 * Check for supported attributes...
965 */
966
967 if ((copies_sup = ippFindAttribute(supported, "copies-supported",
968 IPP_TAG_RANGE)) != NULL)
969 {
970 /*
971 * Has the "copies-supported" attribute - does it have an upper
972 * bound > 1?
973 */
974
975 fprintf(stderr, "DEBUG: copies-supported=%d-%d\n",
976 copies_sup->values[0].range.lower,
977 copies_sup->values[0].range.upper);
978
979 if (copies_sup->values[0].range.upper <= 1)
980 copies_sup = NULL; /* No */
981 }
982
983 cups_version = ippFindAttribute(supported, "cups-version", IPP_TAG_TEXT);
984
985 if ((format_sup = ippFindAttribute(supported, "document-format-supported",
986 IPP_TAG_MIMETYPE)) != NULL)
987 {
988 fprintf(stderr, "DEBUG: document-format-supported (%d values)\n",
989 format_sup->num_values);
990 for (i = 0; i < format_sup->num_values; i ++)
991 fprintf(stderr, "DEBUG: [%d] = \"%s\"\n", i,
992 format_sup->values[i].string.text);
993 }
994
995 if ((media_col_sup = ippFindAttribute(supported, "media-col-supported",
996 IPP_TAG_KEYWORD)) != NULL)
997 {
998 fprintf(stderr, "DEBUG: media-col-supported (%d values)\n",
999 media_col_sup->num_values);
1000 for (i = 0; i < media_col_sup->num_values; i ++)
1001 fprintf(stderr, "DEBUG: [%d] = \"%s\"\n", i,
1002 media_col_sup->values[i].string.text);
1003 }
1004
1005 if ((operations_sup = ippFindAttribute(supported, "operations-supported",
1006 IPP_TAG_ENUM)) != NULL)
1007 {
1008 for (i = 0; i < operations_sup->num_values; i ++)
1009 if (operations_sup->values[i].integer == IPP_PRINT_JOB)
1010 break;
1011
1012 if (i >= operations_sup->num_values)
1013 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1014 "cups-ipp-missing-print-job");
1015
1016 for (i = 0; i < operations_sup->num_values; i ++)
1017 if (operations_sup->values[i].integer == IPP_CANCEL_JOB)
1018 break;
1019
1020 if (i >= operations_sup->num_values)
1021 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1022 "cups-ipp-missing-cancel-job");
1023
1024 for (i = 0; i < operations_sup->num_values; i ++)
1025 if (operations_sup->values[i].integer == IPP_GET_JOB_ATTRIBUTES)
1026 break;
1027
1028 if (i >= operations_sup->num_values)
1029 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1030 "cups-ipp-missing-get-job-attributes");
1031
1032 for (i = 0; i < operations_sup->num_values; i ++)
1033 if (operations_sup->values[i].integer == IPP_GET_PRINTER_ATTRIBUTES)
1034 break;
1035
1036 if (i >= operations_sup->num_values)
1037 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1038 "cups-ipp-missing-get-printer-attributes");
1039
1040 for (i = 0; i < operations_sup->num_values; i ++)
1041 if (operations_sup->values[i].integer == IPP_VALIDATE_JOB)
1042 {
1043 validate_job = 1;
1044 break;
1045 }
1046
1047 if (!validate_job)
1048 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1049 "cups-ipp-missing-validate-job");
1050 }
1051 else
1052 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1053 "cups-ipp-missing-operations-supported");
1054
1055 doc_handling_sup = ippFindAttribute(supported,
1056 "multiple-document-handling-supported",
1057 IPP_TAG_KEYWORD);
1058
1059 report_printer_state(supported);
1060 }
1061 while (ipp_status > IPP_OK_CONFLICT);
1062
1063 /*
1064 * See if the printer is accepting jobs and is not stopped; if either
1065 * condition is true and we are printing to a class, requeue the job...
1066 */
1067
1068 if (getenv("CLASS") != NULL)
1069 {
1070 printer_state = ippFindAttribute(supported, "printer-state",
1071 IPP_TAG_ENUM);
1072 printer_accepting = ippFindAttribute(supported, "printer-is-accepting-jobs",
1073 IPP_TAG_BOOLEAN);
1074
1075 if (printer_state == NULL ||
1076 (printer_state->values[0].integer > IPP_PRINTER_PROCESSING &&
1077 waitprinter) ||
1078 printer_accepting == NULL ||
1079 !printer_accepting->values[0].boolean)
1080 {
1081 /*
1082 * If the CLASS environment variable is set, the job was submitted
1083 * to a class and not to a specific queue. In this case, we want
1084 * to abort immediately so that the job can be requeued on the next
1085 * available printer in the class.
1086 */
1087
1088 _cupsLangPrintFilter(stderr, "INFO",
1089 _("Unable to contact printer, queuing on next "
1090 "printer in class."));
1091
1092 ippDelete(supported);
1093 httpClose(http);
1094
1095 /*
1096 * Sleep 5 seconds to keep the job from requeuing too rapidly...
1097 */
1098
1099 sleep(5);
1100
1101 return (CUPS_BACKEND_FAILED);
1102 }
1103 }
1104
1105 /*
1106 * See if the printer supports multiple copies...
1107 */
1108
1109 copies = atoi(argv[4]);
1110
1111 if (copies_sup || argc < 7)
1112 {
1113 copies_remaining = 1;
1114
1115 if (argc < 7 && !send_options)
1116 copies = 1;
1117 }
1118 else
1119 copies_remaining = copies;
1120
1121 /*
1122 * Prepare remaining printing options...
1123 */
1124
1125 options = NULL;
1126 pc = NULL;
1127
1128 if (send_options)
1129 {
1130 num_options = cupsParseOptions(argv[5], 0, &options);
1131
1132 if (!cups_version && media_col_sup)
1133 {
1134 /*
1135 * Load the PPD file and generate PWG attribute mapping information...
1136 */
1137
1138 ppd = ppdOpenFile(getenv("PPD"));
1139 pc = _ppdCacheCreateWithPPD(ppd);
1140
1141 ppdClose(ppd);
1142 }
1143 }
1144 else
1145 num_options = 0;
1146
1147 document_format = NULL;
1148
1149 if (format_sup != NULL)
1150 {
1151 for (i = 0; i < format_sup->num_values; i ++)
1152 if (!_cups_strcasecmp(final_content_type, format_sup->values[i].string.text))
1153 {
1154 document_format = final_content_type;
1155 break;
1156 }
1157
1158 if (!document_format)
1159 {
1160 for (i = 0; i < format_sup->num_values; i ++)
1161 if (!_cups_strcasecmp("application/octet-stream",
1162 format_sup->values[i].string.text))
1163 {
1164 document_format = "application/octet-stream";
1165 break;
1166 }
1167 }
1168 }
1169
1170 /*
1171 * If the printer does not support HTTP/1.1 (which IPP requires), copy stdin
1172 * to a temporary file so that we can do a HTTP/1.0 submission...
1173 *
1174 * (I hate compatibility hacks!)
1175 */
1176
1177 if (http->version < HTTP_1_1 && num_files == 0)
1178 {
1179 if ((fd = cupsTempFd(tmpfilename, sizeof(tmpfilename))) < 0)
1180 {
1181 perror("DEBUG: Unable to create temporary file");
1182 return (CUPS_BACKEND_FAILED);
1183 }
1184
1185 _cupsLangPrintFilter(stderr, "INFO", _("Copying print data."));
1186
1187 compatsize = backendRunLoop(-1, fd, snmp_fd, &(addrlist->addr), 0, 0,
1188 backendNetworkSideCB);
1189
1190 close(fd);
1191
1192 compatfile = tmpfilename;
1193 files = &compatfile;
1194 num_files = 1;
1195 }
1196 else if (http->version < HTTP_1_1 && num_files == 1)
1197 {
1198 struct stat fileinfo; /* File information */
1199
1200 if (!stat(files[0], &fileinfo))
1201 compatsize = fileinfo.st_size;
1202 }
1203
1204 /*
1205 * Start monitoring the printer in the background...
1206 */
1207
1208 monitor.uri = uri;
1209 monitor.hostname = hostname;
1210 monitor.user = argv[2];
1211 monitor.resource = resource;
1212 monitor.port = port;
1213 monitor.version = version;
1214 monitor.job_id = 0;
1215 monitor.encryption = cupsEncryption();
1216 monitor.job_state = IPP_JOB_PENDING;
1217 monitor.printer_state = IPP_PRINTER_IDLE;
1218
1219 _cupsThreadCreate((_cups_thread_func_t)monitor_printer, &monitor);
1220
1221 /*
1222 * Validate access to the printer...
1223 */
1224
1225 while (!job_canceled && validate_job)
1226 {
1227 request = new_request(IPP_VALIDATE_JOB, version, uri, argv[2], argv[3],
1228 num_options, options, compression,
1229 copies_sup ? copies : 1, document_format, pc,
1230 media_col_sup, doc_handling_sup);
1231
1232 ippDelete(cupsDoRequest(http, request, resource));
1233
1234 ipp_status = cupsLastError();
1235
1236 fprintf(stderr, "DEBUG: Validate-Job: %s (%s)\n",
1237 ippErrorString(ipp_status), cupsLastErrorString());
1238
1239 if (job_canceled)
1240 break;
1241
1242 if (ipp_status == IPP_SERVICE_UNAVAILABLE || ipp_status == IPP_PRINTER_BUSY)
1243 {
1244 _cupsLangPrintFilter(stderr, "INFO", _("The printer is busy."));
1245 sleep(10);
1246 }
1247 else if (ipp_status == IPP_NOT_AUTHORIZED || ipp_status == IPP_FORBIDDEN ||
1248 ipp_status == IPP_AUTHENTICATION_CANCELED)
1249 {
1250 /*
1251 * Update auth-info-required as needed...
1252 */
1253
1254 fprintf(stderr, "DEBUG: WWW-Authenticate=\"%s\"\n",
1255 httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE));
1256
1257 /*
1258 * Normal authentication goes through the password callback, which sets
1259 * auth_info_required to "username,password". Kerberos goes directly
1260 * through GSSAPI, so look for Negotiate in the WWW-Authenticate header
1261 * here and set auth_info_required as needed...
1262 */
1263
1264 if (!strncmp(httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE),
1265 "Negotiate", 9))
1266 auth_info_required = "negotiate";
1267
1268 goto cleanup;
1269 }
1270 else if (ipp_status == IPP_OPERATION_NOT_SUPPORTED)
1271 {
1272 /*
1273 * This is all too common...
1274 */
1275
1276 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1277 "cups-ipp-missing-validate-job");
1278 break;
1279 }
1280 else if (ipp_status < IPP_REDIRECTION_OTHER_SITE)
1281 break;
1282 }
1283
1284 /*
1285 * Then issue the print-job request...
1286 */
1287
1288 job_id = 0;
1289
1290 while (!job_canceled && copies_remaining > 0)
1291 {
1292 /*
1293 * Check for side-channel requests...
1294 */
1295
1296 backendCheckSideChannel(snmp_fd, http->hostaddr);
1297
1298 /*
1299 * Build the IPP job creation request...
1300 */
1301
1302 if (job_canceled)
1303 break;
1304
1305 request = new_request(num_files > 1 ? IPP_CREATE_JOB : IPP_PRINT_JOB,
1306 version, uri, argv[2], argv[3], num_options, options,
1307 compression, copies_sup ? copies : 1, document_format,
1308 pc, media_col_sup, doc_handling_sup);
1309
1310 /*
1311 * Do the request...
1312 */
1313
1314 if (num_files > 1)
1315 response = cupsDoRequest(http, request, resource);
1316 else
1317 {
1318 size_t length = 0; /* Length of request */
1319
1320 if (compatsize > 0)
1321 {
1322 fputs("DEBUG: Sending file using HTTP/1.0 Content-Length...\n", stderr);
1323 length = ippLength(request) + (size_t)compatsize;
1324 }
1325 else
1326 fputs("DEBUG: Sending file using HTTP/1.1 chunking...\n", stderr);
1327
1328 http_status = cupsSendRequest(http, request, resource, length);
1329 if (http_status == HTTP_CONTINUE && request->state == IPP_DATA)
1330 {
1331 if (num_files == 1)
1332 fd = open(files[0], O_RDONLY);
1333 else
1334 {
1335 fd = 0;
1336 http_status = cupsWriteRequestData(http, buffer, bytes);
1337 }
1338
1339 while (http_status == HTTP_CONTINUE)
1340 {
1341 /*
1342 * Check for side-channel requests and more print data...
1343 */
1344
1345 FD_ZERO(&input);
1346 FD_SET(fd, &input);
1347 FD_SET(snmp_fd, &input);
1348
1349 while (select(fd > snmp_fd ? fd + 1 : snmp_fd + 1, &input, NULL, NULL,
1350 NULL) <= 0 && !job_canceled);
1351
1352 if (FD_ISSET(snmp_fd, &input))
1353 backendCheckSideChannel(snmp_fd, http->hostaddr);
1354
1355 if (FD_ISSET(fd, &input))
1356 {
1357 if ((bytes = read(fd, buffer, sizeof(buffer))) > 0)
1358 {
1359 fprintf(stderr, "DEBUG: Read %d bytes...\n", (int)bytes);
1360
1361 if (cupsWriteRequestData(http, buffer, bytes) != HTTP_CONTINUE)
1362 break;
1363 }
1364 else if (bytes == 0 || (errno != EINTR && errno != EAGAIN))
1365 break;
1366 }
1367 }
1368
1369 if (num_files == 1)
1370 close(fd);
1371 }
1372
1373 response = cupsGetResponse(http, resource);
1374 ippDelete(request);
1375 }
1376
1377 ipp_status = cupsLastError();
1378
1379 fprintf(stderr, "DEBUG: %s: %s (%s)\n",
1380 num_files > 1 ? "Create-Job" : "Print-Job",
1381 ippErrorString(ipp_status), cupsLastErrorString());
1382
1383 if (ipp_status > IPP_OK_CONFLICT)
1384 {
1385 job_id = 0;
1386
1387 if (job_canceled)
1388 break;
1389
1390 if (ipp_status == IPP_SERVICE_UNAVAILABLE ||
1391 ipp_status == IPP_PRINTER_BUSY)
1392 {
1393 _cupsLangPrintFilter(stderr, "INFO", _("The printer is busy."));
1394 sleep(10);
1395
1396 if (num_files == 0)
1397 {
1398 /*
1399 * We can't re-submit when we have no files to print, so exit
1400 * immediately with the right status code...
1401 */
1402
1403 goto cleanup;
1404 }
1405 }
1406 else
1407 {
1408 /*
1409 * Update auth-info-required as needed...
1410 */
1411
1412 _cupsLangPrintFilter(stderr, "ERROR",
1413 _("Print file was not accepted."));
1414
1415 if (ipp_status == IPP_NOT_AUTHORIZED || ipp_status == IPP_FORBIDDEN)
1416 {
1417 fprintf(stderr, "DEBUG: WWW-Authenticate=\"%s\"\n",
1418 httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE));
1419
1420 /*
1421 * Normal authentication goes through the password callback, which sets
1422 * auth_info_required to "username,password". Kerberos goes directly
1423 * through GSSAPI, so look for Negotiate in the WWW-Authenticate header
1424 * here and set auth_info_required as needed...
1425 */
1426
1427 if (!strncmp(httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE),
1428 "Negotiate", 9))
1429 auth_info_required = "negotiate";
1430 }
1431 else
1432 sleep(10);
1433
1434 if (num_files == 0)
1435 {
1436 /*
1437 * We can't re-submit when we have no files to print, so exit
1438 * immediately with the right status code...
1439 */
1440
1441 goto cleanup;
1442 }
1443 }
1444 }
1445 else if ((job_id_attr = ippFindAttribute(response, "job-id",
1446 IPP_TAG_INTEGER)) == NULL)
1447 {
1448 _cupsLangPrintFilter(stderr, "INFO",
1449 _("Print file accepted - job ID unknown."));
1450 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1451 "cups-ipp-missing-job-id");
1452 job_id = 0;
1453 }
1454 else
1455 {
1456 monitor.job_id = job_id = job_id_attr->values[0].integer;
1457 _cupsLangPrintFilter(stderr, "INFO",
1458 _("Print file accepted - job ID %d."), job_id);
1459 }
1460
1461 ippDelete(response);
1462
1463 if (job_canceled)
1464 break;
1465
1466 if (job_id && num_files > 1)
1467 {
1468 for (i = 0; i < num_files; i ++)
1469 {
1470 /*
1471 * Check for side-channel requests...
1472 */
1473
1474 backendCheckSideChannel(snmp_fd, http->hostaddr);
1475
1476 /*
1477 * Send the next file in the job...
1478 */
1479
1480 request = ippNewRequest(IPP_SEND_DOCUMENT);
1481 request->request.op.version[0] = version / 10;
1482 request->request.op.version[1] = version % 10;
1483
1484 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1485 NULL, uri);
1486
1487 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
1488 job_id);
1489
1490 if (argv[2][0])
1491 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1492 "requesting-user-name", NULL, argv[2]);
1493
1494 if ((i + 1) == num_files)
1495 ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", 1);
1496
1497 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE,
1498 "document-format", NULL, content_type);
1499
1500 fprintf(stderr, "DEBUG: Sending file %d using chunking...\n", i + 1);
1501 http_status = cupsSendRequest(http, request, resource, 0);
1502 if (http_status == HTTP_CONTINUE && request->state == IPP_DATA &&
1503 (fd = open(files[i], O_RDONLY)) >= 0)
1504 {
1505 while ((bytes = read(fd, buffer, sizeof(buffer))) > 0)
1506 {
1507 if (cupsWriteRequestData(http, buffer, bytes) != HTTP_CONTINUE)
1508 break;
1509 else
1510 {
1511 /*
1512 * Check for side-channel requests...
1513 */
1514
1515 backendCheckSideChannel(snmp_fd, http->hostaddr);
1516 }
1517 }
1518
1519 close(fd);
1520 }
1521
1522 ippDelete(cupsGetResponse(http, resource));
1523 ippDelete(request);
1524
1525 fprintf(stderr, "DEBUG: Send-Document: %s (%s)\n",
1526 ippErrorString(cupsLastError()), cupsLastErrorString());
1527
1528 if (cupsLastError() > IPP_OK_CONFLICT)
1529 {
1530 ipp_status = cupsLastError();
1531
1532 _cupsLangPrintFilter(stderr, "ERROR",
1533 _("Unable to add document to print job."));
1534 break;
1535 }
1536 }
1537 }
1538
1539 if (ipp_status <= IPP_OK_CONFLICT && argc > 6)
1540 {
1541 fprintf(stderr, "PAGE: 1 %d\n", copies_sup ? atoi(argv[4]) : 1);
1542 copies_remaining --;
1543 }
1544 else if (ipp_status == IPP_SERVICE_UNAVAILABLE ||
1545 ipp_status == IPP_PRINTER_BUSY)
1546 continue;
1547 else
1548 copies_remaining --;
1549
1550 /*
1551 * Wait for the job to complete...
1552 */
1553
1554 if (!job_id || !waitjob)
1555 continue;
1556
1557 _cupsLangPrintFilter(stderr, "INFO", _("Waiting for job to complete."));
1558
1559 for (delay = _cupsNextDelay(0, &prev_delay); !job_canceled;)
1560 {
1561 /*
1562 * Check for side-channel requests...
1563 */
1564
1565 backendCheckSideChannel(snmp_fd, http->hostaddr);
1566
1567 /*
1568 * Build an IPP_GET_JOB_ATTRIBUTES request...
1569 */
1570
1571 request = ippNewRequest(IPP_GET_JOB_ATTRIBUTES);
1572 request->request.op.version[0] = version / 10;
1573 request->request.op.version[1] = version % 10;
1574
1575 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1576 NULL, uri);
1577
1578 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
1579 job_id);
1580
1581 if (argv[2][0])
1582 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1583 "requesting-user-name", NULL, argv[2]);
1584
1585 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1586 "requested-attributes", sizeof(jattrs) / sizeof(jattrs[0]),
1587 NULL, jattrs);
1588
1589 /*
1590 * Do the request...
1591 */
1592
1593 httpReconnect(http);
1594 response = cupsDoRequest(http, request, resource);
1595 ipp_status = cupsLastError();
1596
1597 if (ipp_status == IPP_NOT_FOUND)
1598 {
1599 /*
1600 * Job has gone away and/or the server has no job history...
1601 */
1602
1603 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1604 "cups-ipp-missing-job-history");
1605 ippDelete(response);
1606
1607 ipp_status = IPP_OK;
1608 break;
1609 }
1610
1611 fprintf(stderr, "DEBUG: Get-Job-Attributes: %s (%s)\n",
1612 ippErrorString(ipp_status), cupsLastErrorString());
1613
1614 if (ipp_status > IPP_OK_CONFLICT)
1615 {
1616 if (ipp_status != IPP_SERVICE_UNAVAILABLE &&
1617 ipp_status != IPP_PRINTER_BUSY)
1618 {
1619 ippDelete(response);
1620
1621 _cupsLangPrintFilter(stderr, "ERROR",
1622 _("Unable to get print job status."));
1623 break;
1624 }
1625 }
1626
1627 if (response)
1628 {
1629 if ((job_state = ippFindAttribute(response, "job-state",
1630 IPP_TAG_ENUM)) != NULL)
1631 {
1632 /*
1633 * Reflect the remote job state in the local queue...
1634 */
1635
1636 if (cups_version &&
1637 job_state->values[0].integer >= IPP_JOB_PENDING &&
1638 job_state->values[0].integer <= IPP_JOB_COMPLETED)
1639 update_reasons(NULL,
1640 remote_job_states[job_state->values[0].integer -
1641 IPP_JOB_PENDING]);
1642
1643 if ((job_sheets = ippFindAttribute(response,
1644 "job-media-sheets-completed",
1645 IPP_TAG_INTEGER)) == NULL)
1646 job_sheets = ippFindAttribute(response,
1647 "job-impressions-completed",
1648 IPP_TAG_INTEGER);
1649
1650 if (job_sheets)
1651 fprintf(stderr, "PAGE: total %d\n",
1652 job_sheets->values[0].integer);
1653
1654 /*
1655 * Stop polling if the job is finished or pending-held...
1656 */
1657
1658 if (job_state->values[0].integer > IPP_JOB_STOPPED)
1659 {
1660 ippDelete(response);
1661 break;
1662 }
1663 }
1664 else
1665 {
1666 /*
1667 * If the printer does not return a job-state attribute, it does not
1668 * conform to the IPP specification - break out immediately and fail
1669 * the job...
1670 */
1671
1672 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1673 "cups-ipp-missing-job-state");
1674 ipp_status = IPP_INTERNAL_ERROR;
1675 break;
1676 }
1677 }
1678
1679 ippDelete(response);
1680
1681 /*
1682 * Wait before polling again...
1683 */
1684
1685 sleep(delay);
1686
1687 delay = _cupsNextDelay(delay, &prev_delay);
1688 }
1689 }
1690
1691 /*
1692 * Cancel the job as needed...
1693 */
1694
1695 if (job_canceled && job_id)
1696 cancel_job(http, uri, job_id, resource, argv[2], version);
1697
1698 /*
1699 * Check the printer state and report it if necessary...
1700 */
1701
1702 check_printer_state(http, uri, resource, argv[2], version);
1703
1704 /*
1705 * Collect the final page count as needed...
1706 */
1707
1708 if (have_supplies &&
1709 !backendSNMPSupplies(snmp_fd, http->hostaddr, &page_count, NULL) &&
1710 page_count > start_count)
1711 fprintf(stderr, "PAGE: total %d\n", page_count - start_count);
1712
1713 #ifdef HAVE_GSSAPI
1714 /*
1715 * See if we used Kerberos at all...
1716 */
1717
1718 if (http->gssctx)
1719 auth_info_required = "negotiate";
1720 #endif /* HAVE_GSSAPI */
1721
1722 /*
1723 * Free memory...
1724 */
1725
1726 cleanup:
1727
1728 cupsFreeOptions(num_options, options);
1729 _ppdCacheDestroy(pc);
1730
1731 httpClose(http);
1732
1733 ippDelete(supported);
1734
1735 /*
1736 * Remove the temporary file(s) if necessary...
1737 */
1738
1739 if (tmpfilename[0])
1740 unlink(tmpfilename);
1741
1742 #ifdef HAVE_LIBZ
1743 if (compression)
1744 {
1745 for (i = 0; i < num_files; i ++)
1746 unlink(files[i]);
1747 }
1748 #endif /* HAVE_LIBZ */
1749
1750 /*
1751 * Return the queue status...
1752 */
1753
1754 if (ipp_status == IPP_NOT_AUTHORIZED || ipp_status == IPP_FORBIDDEN ||
1755 ipp_status == IPP_AUTHENTICATION_CANCELED ||
1756 ipp_status <= IPP_OK_CONFLICT)
1757 fprintf(stderr, "ATTR: auth-info-required=%s\n", auth_info_required);
1758
1759 if (ipp_status == IPP_NOT_AUTHORIZED || ipp_status == IPP_FORBIDDEN ||
1760 ipp_status == IPP_AUTHENTICATION_CANCELED)
1761 return (CUPS_BACKEND_AUTH_REQUIRED);
1762 else if (ipp_status == IPP_INTERNAL_ERROR)
1763 return (CUPS_BACKEND_STOP);
1764 else if (ipp_status == IPP_DOCUMENT_FORMAT ||
1765 ipp_status == IPP_CONFLICT)
1766 return (CUPS_BACKEND_FAILED);
1767 else if (ipp_status > IPP_OK_CONFLICT)
1768 return (CUPS_BACKEND_RETRY_CURRENT);
1769 else
1770 {
1771 _cupsLangPrintFilter(stderr, "INFO", _("Ready to print."));
1772 return (CUPS_BACKEND_OK);
1773 }
1774 }
1775
1776
1777 /*
1778 * 'cancel_job()' - Cancel a print job.
1779 */
1780
1781 static void
1782 cancel_job(http_t *http, /* I - HTTP connection */
1783 const char *uri, /* I - printer-uri */
1784 int id, /* I - job-id */
1785 const char *resource, /* I - Resource path */
1786 const char *user, /* I - requesting-user-name */
1787 int version) /* I - IPP version */
1788 {
1789 ipp_t *request; /* Cancel-Job request */
1790
1791
1792 _cupsLangPrintFilter(stderr, "INFO", _("Canceling print job."));
1793
1794 request = ippNewRequest(IPP_CANCEL_JOB);
1795 request->request.op.version[0] = version / 10;
1796 request->request.op.version[1] = version % 10;
1797
1798 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1799 NULL, uri);
1800 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", id);
1801
1802 if (user && user[0])
1803 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1804 "requesting-user-name", NULL, user);
1805
1806 /*
1807 * Do the request...
1808 */
1809
1810 ippDelete(cupsDoRequest(http, request, resource));
1811
1812 if (cupsLastError() > IPP_OK_CONFLICT)
1813 _cupsLangPrintFilter(stderr, "ERROR", _("Unable to cancel print job."));
1814 }
1815
1816
1817 /*
1818 * 'check_printer_state()' - Check the printer state.
1819 */
1820
1821 static ipp_pstate_t /* O - Current printer-state */
1822 check_printer_state(
1823 http_t *http, /* I - HTTP connection */
1824 const char *uri, /* I - Printer URI */
1825 const char *resource, /* I - Resource path */
1826 const char *user, /* I - Username, if any */
1827 int version) /* I - IPP version */
1828 {
1829 ipp_t *request, /* IPP request */
1830 *response; /* IPP response */
1831 ipp_attribute_t *attr; /* Attribute in response */
1832 ipp_pstate_t printer_state = IPP_PRINTER_STOPPED;
1833 /* Current printer-state */
1834
1835
1836 /*
1837 * Send a Get-Printer-Attributes request and log the results...
1838 */
1839
1840 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
1841 request->request.op.version[0] = version / 10;
1842 request->request.op.version[1] = version % 10;
1843
1844 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1845 NULL, uri);
1846
1847 if (user && user[0])
1848 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1849 "requesting-user-name", NULL, user);
1850
1851 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1852 "requested-attributes",
1853 (int)(sizeof(pattrs) / sizeof(pattrs[0])), NULL, pattrs);
1854
1855 if ((response = cupsDoRequest(http, request, resource)) != NULL)
1856 {
1857 report_printer_state(response);
1858
1859 if ((attr = ippFindAttribute(response, "printer-state",
1860 IPP_TAG_ENUM)) != NULL)
1861 printer_state = (ipp_pstate_t)attr->values[0].integer;
1862
1863 ippDelete(response);
1864 }
1865
1866 fprintf(stderr, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
1867 ippErrorString(cupsLastError()), cupsLastErrorString());
1868
1869 /*
1870 * Return the printer-state value...
1871 */
1872
1873 return (printer_state);
1874 }
1875
1876
1877 #ifdef HAVE_LIBZ
1878 /*
1879 * 'compress_files()' - Compress print files.
1880 */
1881
1882 static void
1883 compress_files(int num_files, /* I - Number of files */
1884 char **files) /* I - Files */
1885 {
1886 int i, /* Looping var */
1887 fd; /* Temporary file descriptor */
1888 ssize_t bytes; /* Bytes read/written */
1889 size_t total; /* Total bytes read */
1890 cups_file_t *in, /* Input file */
1891 *out; /* Output file */
1892 struct stat outinfo; /* Output file information */
1893 char filename[1024], /* Temporary filename */
1894 buffer[32768]; /* Copy buffer */
1895
1896
1897 fprintf(stderr, "DEBUG: Compressing %d job files...\n", num_files);
1898 for (i = 0; i < num_files; i ++)
1899 {
1900 if ((fd = cupsTempFd(filename, sizeof(filename))) < 0)
1901 {
1902 _cupsLangPrintError("ERROR", _("Unable to create compressed print file"));
1903 exit(CUPS_BACKEND_FAILED);
1904 }
1905
1906 if ((out = cupsFileOpenFd(fd, "w9")) == NULL)
1907 {
1908 _cupsLangPrintError("ERROR", _("Unable to open compressed print file"));
1909 exit(CUPS_BACKEND_FAILED);
1910 }
1911
1912 if ((in = cupsFileOpen(files[i], "r")) == NULL)
1913 {
1914 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1915 cupsFileClose(out);
1916 exit(CUPS_BACKEND_FAILED);
1917 }
1918
1919 total = 0;
1920 while ((bytes = cupsFileRead(in, buffer, sizeof(buffer))) > 0)
1921 if (cupsFileWrite(out, buffer, bytes) < bytes)
1922 {
1923 _cupsLangPrintError("ERROR",
1924 _("Unable to generate compressed print file"));
1925 cupsFileClose(in);
1926 cupsFileClose(out);
1927 exit(CUPS_BACKEND_FAILED);
1928 }
1929 else
1930 total += bytes;
1931
1932 cupsFileClose(out);
1933 cupsFileClose(in);
1934
1935 files[i] = strdup(filename);
1936
1937 if (!stat(filename, &outinfo))
1938 fprintf(stderr,
1939 "DEBUG: File %d compressed to %.1f%% of original size, "
1940 CUPS_LLFMT " bytes...\n",
1941 i + 1, 100.0 * outinfo.st_size / total,
1942 CUPS_LLCAST outinfo.st_size);
1943 }
1944 }
1945 #endif /* HAVE_LIBZ */
1946
1947
1948 /*
1949 * 'monitor_printer()' - Monitor the printer state.
1950 */
1951
1952 static void * /* O - Thread exit code */
1953 monitor_printer(
1954 _cups_monitor_t *monitor) /* I - Monitoring data */
1955 {
1956 http_t *http; /* Connection to printer */
1957 ipp_t *request, /* IPP request */
1958 *response; /* IPP response */
1959 ipp_attribute_t *attr; /* Attribute in response */
1960 int delay, /* Current delay */
1961 prev_delay; /* Previous delay */
1962
1963
1964 /*
1965 * Make a copy of the printer connection...
1966 */
1967
1968 http = _httpCreate(monitor->hostname, monitor->port, NULL, monitor->encryption,
1969 AF_UNSPEC);
1970 cupsSetPasswordCB(password_cb);
1971
1972 /*
1973 * Loop until the job is canceled, aborted, or completed.
1974 */
1975
1976 delay = _cupsNextDelay(0, &prev_delay);
1977
1978 while (monitor->job_state < IPP_JOB_CANCELED && !job_canceled)
1979 {
1980 /*
1981 * Reconnect to the printer...
1982 */
1983
1984 if (!httpReconnect(http))
1985 {
1986 /*
1987 * Connected, so check on the printer state...
1988 */
1989
1990 monitor->printer_state = check_printer_state(http, monitor->uri,
1991 monitor->resource,
1992 monitor->user,
1993 monitor->version);
1994
1995 if (monitor->job_id > 0)
1996 {
1997 /*
1998 * Check the status of the job itself...
1999 */
2000
2001 request = ippNewRequest(IPP_GET_JOB_ATTRIBUTES);
2002 request->request.op.version[0] = monitor->version / 10;
2003 request->request.op.version[1] = monitor->version % 10;
2004
2005 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
2006 NULL, monitor->uri);
2007 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
2008 monitor->job_id);
2009
2010 if (monitor->user && monitor->user[0])
2011 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
2012 "requesting-user-name", NULL, monitor->user);
2013
2014 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
2015 "requested-attributes",
2016 (int)(sizeof(jattrs) / sizeof(jattrs[0])), NULL, jattrs);
2017
2018 /*
2019 * Do the request...
2020 */
2021
2022 response = cupsDoRequest(http, request, monitor->resource);
2023
2024 fprintf(stderr, "DEBUG: Get-Job-Attributes: %s (%s)\n",
2025 ippErrorString(cupsLastError()), cupsLastErrorString());
2026
2027 if ((attr = ippFindAttribute(response, "job-state",
2028 IPP_TAG_ENUM)) != NULL)
2029 monitor->job_state = (ipp_jstate_t)attr->values[0].integer;
2030 else
2031 monitor->job_state = IPP_JOB_COMPLETED;
2032
2033 ippDelete(response);
2034 }
2035
2036 /*
2037 * Disconnect from the printer - we'll reconnect on the next poll...
2038 */
2039
2040 _httpDisconnect(http);
2041 }
2042
2043 /*
2044 * Sleep for N seconds...
2045 */
2046
2047 sleep(delay);
2048
2049 delay = _cupsNextDelay(delay, &prev_delay);
2050 }
2051
2052 /*
2053 * Cleanup and return...
2054 */
2055
2056 httpClose(http);
2057
2058 return (NULL);
2059 }
2060
2061
2062 /*
2063 * 'new_request()' - Create a new print creation or validation request.
2064 */
2065
2066 static ipp_t * /* O - Request data */
2067 new_request(
2068 ipp_op_t op, /* I - IPP operation code */
2069 int version, /* I - IPP version number */
2070 const char *uri, /* I - printer-uri value */
2071 const char *user, /* I - requesting-user-name value */
2072 const char *title, /* I - job-name value */
2073 int num_options, /* I - Number of options to send */
2074 cups_option_t *options, /* I - Options to send */
2075 const char *compression, /* I - compression value or NULL */
2076 int copies, /* I - copies value or 0 */
2077 const char *format, /* I - documet-format value or NULL */
2078 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
2079 ipp_attribute_t *media_col_sup, /* I - media-col-supported values */
2080 ipp_attribute_t *doc_handling_sup) /* I - multiple-document-handling-supported values */
2081 {
2082 int i; /* Looping var */
2083 ipp_t *request; /* Request data */
2084 const char *keyword; /* PWG keyword */
2085 _pwg_size_t *size; /* PWG media size */
2086 ipp_t *media_col, /* media-col value */
2087 *media_size; /* media-size value */
2088 const char *media_source, /* media-source value */
2089 *media_type, /* media-type value */
2090 *collate_str; /* multiple-document-handling value */
2091
2092
2093 /*
2094 * Create the IPP request...
2095 */
2096
2097 request = ippNewRequest(op);
2098 request->request.op.version[0] = version / 10;
2099 request->request.op.version[1] = version % 10;
2100
2101 fprintf(stderr, "DEBUG: %s IPP/%d.%d\n",
2102 ippOpString(request->request.op.operation_id),
2103 request->request.op.version[0],
2104 request->request.op.version[1]);
2105
2106 /*
2107 * Add standard attributes...
2108 */
2109
2110 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
2111 NULL, uri);
2112 fprintf(stderr, "DEBUG: printer-uri=\"%s\"\n", uri);
2113
2114 if (user && *user)
2115 {
2116 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
2117 "requesting-user-name", NULL, user);
2118 fprintf(stderr, "DEBUG: requesting-user-name=\"%s\"\n", user);
2119 }
2120
2121 if (title && *title)
2122 {
2123 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
2124 title);
2125 fprintf(stderr, "DEBUG: job-name=\"%s\"\n", title);
2126 }
2127
2128 if (format)
2129 {
2130 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE,
2131 "document-format", NULL, format);
2132 fprintf(stderr, "DEBUG: document-format=\"%s\"\n", format);
2133 }
2134
2135 #ifdef HAVE_LIBZ
2136 if (compression)
2137 {
2138 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
2139 "compression", NULL, compression);
2140 fprintf(stderr, "DEBUG: compression=\"%s\"\n", compression);
2141 }
2142 #endif /* HAVE_LIBZ */
2143
2144 /*
2145 * Handle options on the command-line...
2146 */
2147
2148 if (num_options > 0)
2149 {
2150 if (pc)
2151 {
2152 /*
2153 * Send standard IPP attributes...
2154 */
2155
2156 if ((keyword = cupsGetOption("PageSize", num_options, options)) == NULL)
2157 keyword = cupsGetOption("media", num_options, options);
2158
2159 if ((size = _ppdCacheGetSize(pc, keyword)) != NULL)
2160 {
2161 /*
2162 * Add a media-col value...
2163 */
2164
2165 media_size = ippNew();
2166 ippAddInteger(media_size, IPP_TAG_ZERO, IPP_TAG_INTEGER,
2167 "x-dimension", size->width);
2168 ippAddInteger(media_size, IPP_TAG_ZERO, IPP_TAG_INTEGER,
2169 "y-dimension", size->length);
2170
2171 media_col = ippNew();
2172 ippAddCollection(media_col, IPP_TAG_ZERO, "media-size", media_size);
2173
2174 media_source = _ppdCacheGetSource(pc, cupsGetOption("InputSlot",
2175 num_options,
2176 options));
2177 media_type = _ppdCacheGetType(pc, cupsGetOption("MediaType",
2178 num_options,
2179 options));
2180
2181 for (i = 0; i < media_col_sup->num_values; i ++)
2182 {
2183 if (!strcmp(media_col_sup->values[i].string.text,
2184 "media-left-margin"))
2185 ippAddInteger(media_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
2186 "media-left-margin", size->left);
2187 else if (!strcmp(media_col_sup->values[i].string.text,
2188 "media-bottom-margin"))
2189 ippAddInteger(media_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
2190 "media-bottom-margin", size->left);
2191 else if (!strcmp(media_col_sup->values[i].string.text,
2192 "media-right-margin"))
2193 ippAddInteger(media_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
2194 "media-right-margin", size->left);
2195 else if (!strcmp(media_col_sup->values[i].string.text,
2196 "media-top-margin"))
2197 ippAddInteger(media_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
2198 "media-top-margin", size->left);
2199 else if (!strcmp(media_col_sup->values[i].string.text,
2200 "media-source") && media_source)
2201 ippAddString(media_col, IPP_TAG_ZERO, IPP_TAG_KEYWORD,
2202 "media-source", NULL, media_source);
2203 else if (!strcmp(media_col_sup->values[i].string.text,
2204 "media-type") && media_type)
2205 ippAddString(media_col, IPP_TAG_ZERO, IPP_TAG_KEYWORD,
2206 "media-type", NULL, media_type);
2207 }
2208
2209 ippAddCollection(request, IPP_TAG_JOB, "media-col", media_col);
2210 }
2211
2212 if ((keyword = cupsGetOption("output-bin", num_options,
2213 options)) == NULL)
2214 keyword = _ppdCacheGetBin(pc, cupsGetOption("OutputBin", num_options,
2215 options));
2216
2217 if (keyword)
2218 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "output-bin",
2219 NULL, keyword);
2220
2221 if ((keyword = cupsGetOption("output-mode", num_options,
2222 options)) != NULL)
2223 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "output-mode",
2224 NULL, keyword);
2225 else if ((keyword = cupsGetOption("ColorModel", num_options,
2226 options)) != NULL)
2227 {
2228 if (!_cups_strcasecmp(keyword, "Gray"))
2229 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "output-mode",
2230 NULL, "monochrome");
2231 else
2232 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "output-mode",
2233 NULL, "color");
2234 }
2235
2236 if ((keyword = cupsGetOption("print-quality", num_options,
2237 options)) != NULL)
2238 ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_ENUM, "print-quality",
2239 atoi(keyword));
2240 else if ((keyword = cupsGetOption("cupsPrintQuality", num_options,
2241 options)) != NULL)
2242 {
2243 if (!_cups_strcasecmp(keyword, "draft"))
2244 ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_ENUM, "print-quality",
2245 IPP_QUALITY_DRAFT);
2246 else if (!_cups_strcasecmp(keyword, "normal"))
2247 ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_ENUM, "print-quality",
2248 IPP_QUALITY_NORMAL);
2249 else if (!_cups_strcasecmp(keyword, "high"))
2250 ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_ENUM, "print-quality",
2251 IPP_QUALITY_HIGH);
2252 }
2253
2254 if ((keyword = cupsGetOption("sides", num_options, options)) != NULL)
2255 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "sides",
2256 NULL, keyword);
2257 else if (pc->sides_option &&
2258 (keyword = cupsGetOption(pc->sides_option, num_options,
2259 options)) != NULL)
2260 {
2261 if (!_cups_strcasecmp(keyword, pc->sides_1sided))
2262 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "sides",
2263 NULL, "one-sided");
2264 else if (!_cups_strcasecmp(keyword, pc->sides_2sided_long))
2265 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "sides",
2266 NULL, "two-sided-long-edge");
2267 if (!_cups_strcasecmp(keyword, pc->sides_2sided_short))
2268 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "sides",
2269 NULL, "two-sided-short-edge");
2270 }
2271
2272 if (doc_handling_sup &&
2273 (keyword = cupsGetOption("collate", num_options, options)) != NULL)
2274 {
2275 if (!_cups_strcasecmp(keyword, "true"))
2276 collate_str = "separate-documents-collated-copies";
2277 else
2278 collate_str = "separate-documents-uncollated-copies";
2279
2280 for (i = 0; i < doc_handling_sup->num_values; i ++)
2281 if (!strcmp(doc_handling_sup->values[i].string.text, collate_str))
2282 {
2283 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD,
2284 "multiple-document-handling", NULL, collate_str);
2285 break;
2286 }
2287 }
2288 }
2289 else
2290 {
2291 /*
2292 * When talking to another CUPS server, send all options...
2293 */
2294
2295 cupsEncodeOptions(request, num_options, options);
2296 }
2297
2298 if (copies > 1)
2299 ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_INTEGER, "copies", copies);
2300 }
2301
2302 return (request);
2303 }
2304
2305
2306 /*
2307 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
2308 */
2309
2310 static const char * /* O - Password */
2311 password_cb(const char *prompt) /* I - Prompt (not used) */
2312 {
2313 (void)prompt;
2314
2315 /*
2316 * Remember that we need to authenticate...
2317 */
2318
2319 auth_info_required = "username,password";
2320
2321 if (password && *password && password_tries < 3)
2322 {
2323 password_tries ++;
2324
2325 return (password);
2326 }
2327 else
2328 {
2329 /*
2330 * Give up after 3 tries or if we don't have a password to begin with...
2331 */
2332
2333 return (NULL);
2334 }
2335 }
2336
2337
2338 /*
2339 * 'report_attr()' - Report an IPP attribute value.
2340 */
2341
2342 static void
2343 report_attr(ipp_attribute_t *attr) /* I - Attribute */
2344 {
2345 int i; /* Looping var */
2346 char value[1024], /* Value string */
2347 *valptr, /* Pointer into value string */
2348 *attrptr; /* Pointer into attribute value */
2349 const char *cached; /* Cached attribute */
2350
2351
2352 /*
2353 * Convert the attribute values into quoted strings...
2354 */
2355
2356 for (i = 0, valptr = value;
2357 i < attr->num_values && valptr < (value + sizeof(value) - 10);
2358 i ++)
2359 {
2360 if (i > 0)
2361 *valptr++ = ',';
2362
2363 switch (attr->value_tag)
2364 {
2365 case IPP_TAG_INTEGER :
2366 case IPP_TAG_ENUM :
2367 snprintf(valptr, sizeof(value) - (valptr - value), "%d",
2368 attr->values[i].integer);
2369 valptr += strlen(valptr);
2370 break;
2371
2372 case IPP_TAG_TEXT :
2373 case IPP_TAG_NAME :
2374 case IPP_TAG_KEYWORD :
2375 *valptr++ = '\"';
2376 for (attrptr = attr->values[i].string.text;
2377 *attrptr && valptr < (value + sizeof(value) - 10);
2378 attrptr ++)
2379 {
2380 if (*attrptr == '\\' || *attrptr == '\"')
2381 *valptr++ = '\\';
2382
2383 *valptr++ = *attrptr;
2384 }
2385 *valptr++ = '\"';
2386 break;
2387
2388 default :
2389 /*
2390 * Unsupported value type...
2391 */
2392
2393 return;
2394 }
2395 }
2396
2397 *valptr = '\0';
2398
2399 _cupsMutexLock(&report_mutex);
2400
2401 if ((cached = cupsGetOption(attr->name, num_attr_cache,
2402 attr_cache)) == NULL || strcmp(cached, value))
2403 {
2404 /*
2405 * Tell the scheduler about the new values...
2406 */
2407
2408 num_attr_cache = cupsAddOption(attr->name, value, num_attr_cache,
2409 &attr_cache);
2410 fprintf(stderr, "ATTR: %s=%s\n", attr->name, value);
2411 }
2412
2413 _cupsMutexUnlock(&report_mutex);
2414 }
2415
2416
2417 /*
2418 * 'report_printer_state()' - Report the printer state.
2419 */
2420
2421 static void
2422 report_printer_state(ipp_t *ipp) /* I - IPP response */
2423 {
2424 ipp_attribute_t *pa, /* printer-alert */
2425 *pam, /* printer-alert-message */
2426 *psm, /* printer-state-message */
2427 *reasons, /* printer-state-reasons */
2428 *marker; /* marker-* attributes */
2429 char value[1024], /* State/message string */
2430 *valptr; /* Pointer into string */
2431 static int ipp_supplies = -1;
2432 /* Report supply levels? */
2433
2434
2435 /*
2436 * Report alerts and messages...
2437 */
2438
2439 if ((pa = ippFindAttribute(ipp, "printer-alert", IPP_TAG_TEXT)) != NULL)
2440 report_attr(pa);
2441
2442 if ((pam = ippFindAttribute(ipp, "printer-alert-message",
2443 IPP_TAG_TEXT)) != NULL)
2444 report_attr(pam);
2445
2446 if ((psm = ippFindAttribute(ipp, "printer-state-message",
2447 IPP_TAG_TEXT)) != NULL)
2448 {
2449 char *ptr; /* Pointer into message */
2450
2451
2452 strlcpy(value, "INFO: ", sizeof(value));
2453 for (ptr = psm->values[0].string.text, valptr = value + 6;
2454 *ptr && valptr < (value + sizeof(value) - 6);
2455 ptr ++)
2456 {
2457 if (*ptr < ' ' && *ptr > 0 && *ptr != '\t')
2458 {
2459 /*
2460 * Substitute "<XX>" for the control character; sprintf is safe because
2461 * we always leave 6 chars free at the end...
2462 */
2463
2464 sprintf(valptr, "<%02X>", *ptr);
2465 valptr += 4;
2466 }
2467 else
2468 *valptr++ = *ptr;
2469 }
2470
2471 *valptr++ = '\n';
2472 *valptr = '\0';
2473
2474 fputs(value, stderr);
2475 }
2476
2477 /*
2478 * Now report printer-state-reasons, filtering out some of the reasons we never
2479 * want to set...
2480 */
2481
2482 if ((reasons = ippFindAttribute(ipp, "printer-state-reasons",
2483 IPP_TAG_KEYWORD)) == NULL)
2484 return;
2485
2486 update_reasons(reasons, NULL);
2487
2488 /*
2489 * Relay the current marker-* attribute values...
2490 */
2491
2492 if (ipp_supplies < 0)
2493 {
2494 ppd_file_t *ppd; /* PPD file */
2495 ppd_attr_t *ppdattr; /* Attribute in PPD file */
2496
2497 if ((ppd = ppdOpenFile(getenv("PPD"))) != NULL &&
2498 (ppdattr = ppdFindAttr(ppd, "cupsIPPSupplies", NULL)) != NULL &&
2499 ppdattr->value && _cups_strcasecmp(ppdattr->value, "true"))
2500 ipp_supplies = 0;
2501 else
2502 ipp_supplies = 1;
2503
2504 ppdClose(ppd);
2505 }
2506
2507 if (ipp_supplies > 0)
2508 {
2509 if ((marker = ippFindAttribute(ipp, "marker-colors", IPP_TAG_NAME)) != NULL)
2510 report_attr(marker);
2511 if ((marker = ippFindAttribute(ipp, "marker-high-levels",
2512 IPP_TAG_INTEGER)) != NULL)
2513 report_attr(marker);
2514 if ((marker = ippFindAttribute(ipp, "marker-levels",
2515 IPP_TAG_INTEGER)) != NULL)
2516 report_attr(marker);
2517 if ((marker = ippFindAttribute(ipp, "marker-low-levels",
2518 IPP_TAG_INTEGER)) != NULL)
2519 report_attr(marker);
2520 if ((marker = ippFindAttribute(ipp, "marker-message",
2521 IPP_TAG_TEXT)) != NULL)
2522 report_attr(marker);
2523 if ((marker = ippFindAttribute(ipp, "marker-names", IPP_TAG_NAME)) != NULL)
2524 report_attr(marker);
2525 if ((marker = ippFindAttribute(ipp, "marker-types",
2526 IPP_TAG_KEYWORD)) != NULL)
2527 report_attr(marker);
2528 }
2529 }
2530
2531
2532 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
2533 /*
2534 * 'run_as_user()' - Run the IPP backend as the printing user.
2535 *
2536 * This function uses an XPC-based user agent to run the backend as the printing
2537 * user. We need to do this in order to have access to the user's Kerberos
2538 * credentials.
2539 */
2540
2541 static int /* O - Exit status */
2542 run_as_user(int argc, /* I - Number of command-line args */
2543 char *argv[], /* I - Command-line arguments */
2544 uid_t uid, /* I - User ID */
2545 const char *device_uri, /* I - Device URI */
2546 int fd) /* I - File to print */
2547 {
2548 const char *auth_negotiate;/* AUTH_NEGOTIATE env var */
2549 xpc_connection_t conn; /* Connection to XPC service */
2550 xpc_object_t request; /* Request message dictionary */
2551 __block xpc_object_t response; /* Response message dictionary */
2552 dispatch_semaphore_t sem; /* Semaphore for waiting for response */
2553 int status = CUPS_BACKEND_FAILED;
2554 /* Status of request */
2555
2556
2557 fprintf(stderr, "DEBUG: Running IPP backend as UID %d.\n", (int)uid);
2558
2559 /*
2560 * Connect to the user agent for the specified UID...
2561 */
2562
2563 conn = xpc_connection_create_mach_service(kPMPrintUIToolAgent,
2564 dispatch_get_global_queue(0, 0), 0);
2565 if (!conn)
2566 {
2567 _cupsLangPrintFilter(stderr, "ERROR",
2568 _("Unable to start backend process."));
2569 fputs("DEBUG: Unable to create connection to agent.\n", stderr);
2570 goto cleanup;
2571 }
2572
2573 xpc_connection_set_event_handler(conn,
2574 ^(xpc_object_t event)
2575 {
2576 xpc_type_t messageType = xpc_get_type(event);
2577
2578 if (messageType == XPC_TYPE_ERROR)
2579 {
2580 if (event == XPC_ERROR_CONNECTION_INTERRUPTED)
2581 fprintf(stderr, "DEBUG: Interrupted connection to service %s.\n",
2582 xpc_connection_get_name(conn));
2583 else if (event == XPC_ERROR_CONNECTION_INVALID)
2584 fprintf(stderr, "DEBUG: Connection invalid for service %s.\n",
2585 xpc_connection_get_name(conn));
2586 else
2587 fprintf(stderr, "DEBUG: Unxpected error for service %s: %s\n",
2588 xpc_connection_get_name(conn),
2589 xpc_dictionary_get_string(event, XPC_ERROR_KEY_DESCRIPTION));
2590 }
2591 });
2592 xpc_connection_set_target_uid(conn, uid);
2593 xpc_connection_resume(conn);
2594
2595 /*
2596 * Try starting the backend...
2597 */
2598
2599 request = xpc_dictionary_create(NULL, NULL, 0);
2600 xpc_dictionary_set_int64(request, "command", kPMStartJob);
2601 xpc_dictionary_set_string(request, "device-uri", device_uri);
2602 xpc_dictionary_set_string(request, "job-id", argv[1]);
2603 xpc_dictionary_set_string(request, "user", argv[2]);
2604 xpc_dictionary_set_string(request, "title", argv[3]);
2605 xpc_dictionary_set_string(request, "copies", argv[4]);
2606 xpc_dictionary_set_string(request, "options", argv[5]);
2607 xpc_dictionary_set_string(request, "auth-info-required",
2608 getenv("AUTH_INFO_REQUIRED"));
2609 if ((auth_negotiate = getenv("AUTH_NEGOTIATE")) != NULL)
2610 xpc_dictionary_set_string(request, "auth-negotiate", auth_negotiate);
2611 xpc_dictionary_set_fd(request, "stdin", fd);
2612 xpc_dictionary_set_fd(request, "stderr", 2);
2613 xpc_dictionary_set_fd(request, "side-channel", CUPS_SC_FD);
2614
2615 sem = dispatch_semaphore_create(0);
2616 response = NULL;
2617
2618 xpc_connection_send_message_with_reply(conn, request,
2619 dispatch_get_global_queue(0,0),
2620 ^(xpc_object_t reply)
2621 {
2622 /* Save the response and wake up */
2623 if (xpc_get_type(reply)
2624 == XPC_TYPE_DICTIONARY)
2625 response = xpc_retain(reply);
2626
2627 dispatch_semaphore_signal(sem);
2628 });
2629
2630 dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
2631 xpc_release(request);
2632 dispatch_release(sem);
2633
2634 if (response)
2635 {
2636 child_pid = xpc_dictionary_get_int64(response, "child-pid");
2637
2638 xpc_release(response);
2639
2640 if (child_pid)
2641 fprintf(stderr, "DEBUG: Child PID=%d.\n", child_pid);
2642 else
2643 {
2644 _cupsLangPrintFilter(stderr, "ERROR",
2645 _("Unable to start backend process."));
2646 fputs("DEBUG: No child PID.\n", stderr);
2647 goto cleanup;
2648 }
2649 }
2650 else
2651 {
2652 _cupsLangPrintFilter(stderr, "ERROR",
2653 _("Unable to start backend process."));
2654 fputs("DEBUG: No reply from agent.\n", stderr);
2655 goto cleanup;
2656 }
2657
2658 /*
2659 * Then wait for the backend to finish...
2660 */
2661
2662 request = xpc_dictionary_create(NULL, NULL, 0);
2663 xpc_dictionary_set_int64(request, "command", kPMWaitForJob);
2664 xpc_dictionary_set_fd(request, "stderr", 2);
2665
2666 sem = dispatch_semaphore_create(0);
2667 response = NULL;
2668
2669 xpc_connection_send_message_with_reply(conn, request,
2670 dispatch_get_global_queue(0,0),
2671 ^(xpc_object_t reply)
2672 {
2673 /* Save the response and wake up */
2674 if (xpc_get_type(reply)
2675 == XPC_TYPE_DICTIONARY)
2676 response = xpc_retain(reply);
2677
2678 dispatch_semaphore_signal(sem);
2679 });
2680
2681 dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
2682 xpc_release(request);
2683 dispatch_release(sem);
2684
2685 if (response)
2686 {
2687 status = xpc_dictionary_get_int64(response, "status");
2688
2689 if (status == SIGTERM || status == SIGKILL || status == SIGPIPE)
2690 {
2691 fprintf(stderr, "DEBUG: Child terminated on signal %d.\n", status);
2692 status = CUPS_BACKEND_FAILED;
2693 }
2694 else if (WIFSIGNALED(status))
2695 {
2696 fprintf(stderr, "DEBUG: Child crashed on signal %d.\n", status);
2697 status = CUPS_BACKEND_STOP;
2698 }
2699 else if (WIFEXITED(status))
2700 {
2701 status = WEXITSTATUS(status);
2702 fprintf(stderr, "DEBUG: Child exited with status %d.\n", status);
2703 }
2704
2705 xpc_release(response);
2706 }
2707 else
2708 _cupsLangPrintFilter(stderr, "ERROR",
2709 _("Unable to get backend exit status."));
2710
2711 cleanup:
2712
2713 if (conn)
2714 {
2715 xpc_connection_suspend(conn);
2716 xpc_connection_cancel(conn);
2717 xpc_release(conn);
2718 }
2719
2720 return (status);
2721 }
2722 #endif /* HAVE_GSSAPI && HAVE_XPC */
2723
2724
2725 /*
2726 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
2727 */
2728
2729 static void
2730 sigterm_handler(int sig) /* I - Signal */
2731 {
2732 (void)sig; /* remove compiler warnings... */
2733
2734 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
2735 if (child_pid)
2736 {
2737 kill(child_pid, sig);
2738 child_pid = 0;
2739 }
2740 #endif /* HAVE_GSSAPI && HAVE_XPC */
2741
2742 if (!job_canceled)
2743 {
2744 /*
2745 * Flag that the job should be canceled...
2746 */
2747
2748 job_canceled = 1;
2749 return;
2750 }
2751
2752 /*
2753 * The scheduler already tried to cancel us once, now just terminate
2754 * after removing our temp file!
2755 */
2756
2757 if (tmpfilename[0])
2758 unlink(tmpfilename);
2759
2760 exit(1);
2761 }
2762
2763
2764 /*
2765 * 'update_reasons()' - Update the printer-state-reasons values.
2766 */
2767
2768 static void
2769 update_reasons(ipp_attribute_t *attr, /* I - printer-state-reasons or NULL */
2770 const char *s) /* I - STATE: string or NULL */
2771 {
2772 char op; /* Add (+), remove (-), replace (\0) */
2773 cups_array_t *new_reasons; /* New reasons array */
2774 char *reason, /* Current reason */
2775 add[2048], /* Reasons added string */
2776 *addptr, /* Pointer into add string */
2777 rem[2048], /* Reasons removed string */
2778 *remptr; /* Pointer into remove string */
2779 const char *addprefix, /* Current add string prefix */
2780 *remprefix; /* Current remove string prefix */
2781
2782
2783 fprintf(stderr, "DEBUG: update_reasons(attr=%d(%s%s), s=\"%s\")\n",
2784 attr ? attr->num_values : 0, attr ? attr->values[0].string.text : "",
2785 attr && attr->num_values > 1 ? ",..." : "", s ? s : "(null)");
2786
2787 /*
2788 * Create an array of new reason keyword strings...
2789 */
2790
2791 if (attr)
2792 {
2793 int i; /* Looping var */
2794
2795 new_reasons = cupsArrayNew((cups_array_func_t)strcmp, NULL);
2796 op = '\0';
2797
2798 for (i = 0; i < attr->num_values; i ++)
2799 {
2800 reason = attr->values[i].string.text;
2801
2802 if (strcmp(reason, "none") &&
2803 strcmp(reason, "none-report") &&
2804 strcmp(reason, "paused") &&
2805 strcmp(reason, "com.apple.print.recoverable-warning") &&
2806 strncmp(reason, "cups-", 5))
2807 cupsArrayAdd(new_reasons, reason);
2808 }
2809 }
2810 else if (s)
2811 {
2812 if (*s == '+' || *s == '-')
2813 op = *s++;
2814 else
2815 op = '\0';
2816
2817 new_reasons = _cupsArrayNewStrings(s);
2818 }
2819 else
2820 return;
2821
2822 /*
2823 * Compute the changes...
2824 */
2825
2826 add[0] = '\0';
2827 addprefix = "STATE: +";
2828 addptr = add;
2829 rem[0] = '\0';
2830 remprefix = "STATE: -";
2831 remptr = rem;
2832
2833 fprintf(stderr, "DEBUG2: op='%c', new_reasons=%d, state_reasons=%d\n",
2834 op ? op : ' ', cupsArrayCount(new_reasons),
2835 cupsArrayCount(state_reasons));
2836
2837 _cupsMutexLock(&report_mutex);
2838
2839 if (op == '+')
2840 {
2841 /*
2842 * Add reasons...
2843 */
2844
2845 for (reason = (char *)cupsArrayFirst(new_reasons);
2846 reason;
2847 reason = (char *)cupsArrayNext(new_reasons))
2848 {
2849 if (!cupsArrayFind(state_reasons, reason))
2850 {
2851 if (!strncmp(reason, "cups-remote-", 12))
2852 {
2853 /*
2854 * If we are setting cups-remote-xxx, remove all other cups-remote-xxx
2855 * keywords...
2856 */
2857
2858 char *temp; /* Current reason in state_reasons */
2859
2860 cupsArraySave(state_reasons);
2861
2862 for (temp = (char *)cupsArrayFirst(state_reasons);
2863 temp;
2864 temp = (char *)cupsArrayNext(state_reasons))
2865 if (!strncmp(temp, "cups-remote-", 12))
2866 {
2867 snprintf(remptr, sizeof(rem) - (remptr - rem), "%s%s", remprefix,
2868 temp);
2869 remptr += strlen(remptr);
2870 remprefix = ",";
2871
2872 cupsArrayRemove(state_reasons, temp);
2873 break;
2874 }
2875
2876 cupsArrayRestore(state_reasons);
2877 }
2878
2879 cupsArrayAdd(state_reasons, reason);
2880
2881 snprintf(addptr, sizeof(add) - (addptr - add), "%s%s", addprefix,
2882 reason);
2883 addptr += strlen(addptr);
2884 addprefix = ",";
2885 }
2886 }
2887 }
2888 else if (op == '-')
2889 {
2890 /*
2891 * Remove reasons...
2892 */
2893
2894 for (reason = (char *)cupsArrayFirst(new_reasons);
2895 reason;
2896 reason = (char *)cupsArrayNext(new_reasons))
2897 {
2898 if (cupsArrayFind(state_reasons, reason))
2899 {
2900 snprintf(remptr, sizeof(rem) - (remptr - rem), "%s%s", remprefix,
2901 reason);
2902 remptr += strlen(remptr);
2903 remprefix = ",";
2904
2905 cupsArrayRemove(state_reasons, reason);
2906 }
2907 }
2908 }
2909 else
2910 {
2911 /*
2912 * Replace reasons...
2913 */
2914
2915 for (reason = (char *)cupsArrayFirst(state_reasons);
2916 reason;
2917 reason = (char *)cupsArrayNext(state_reasons))
2918 {
2919 if (strncmp(reason, "cups-", 5) && !cupsArrayFind(new_reasons, reason))
2920 {
2921 snprintf(remptr, sizeof(rem) - (remptr - rem), "%s%s", remprefix,
2922 reason);
2923 remptr += strlen(remptr);
2924 remprefix = ",";
2925
2926 cupsArrayRemove(state_reasons, reason);
2927 }
2928 }
2929
2930 for (reason = (char *)cupsArrayFirst(new_reasons);
2931 reason;
2932 reason = (char *)cupsArrayNext(new_reasons))
2933 {
2934 if (!cupsArrayFind(state_reasons, reason))
2935 {
2936 cupsArrayAdd(state_reasons, reason);
2937
2938 snprintf(addptr, sizeof(add) - (addptr - add), "%s%s", addprefix,
2939 reason);
2940 addptr += strlen(addptr);
2941 addprefix = ",";
2942 }
2943 }
2944 }
2945
2946 _cupsMutexUnlock(&report_mutex);
2947
2948 /*
2949 * Report changes and return...
2950 */
2951
2952 if (add[0] && rem[0])
2953 fprintf(stderr, "%s\n%s\n", add, rem);
2954 else if (add[0])
2955 fprintf(stderr, "%s\n", add);
2956 else if (rem[0])
2957 fprintf(stderr, "%s\n", rem);
2958 }
2959
2960 /*
2961 * End of "$Id: ipp.c 9759 2011-05-11 03:24:33Z mike $".
2962 */