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