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