]> git.ipfire.org Git - thirdparty/cups.git/blame - backend/ipp.c
Update some man pages (STR #4372)
[thirdparty/cups.git] / backend / ipp.c
CommitLineData
ef416fc2 1/*
f2d18633 2 * "$Id$"
ef416fc2 3 *
3463c27a 4 * IPP backend for CUPS.
ef416fc2 5 *
7e86f2f6 6 * Copyright 2007-2014 by Apple Inc.
3463c27a 7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
ef416fc2 8 *
3463c27a
MS
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * "LICENSE" which should have been included with this file. If this
13 * file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 14 *
3463c27a 15 * This file is subject to the Apple OS-Developed Software exception.
ef416fc2 16 */
17
18/*
19 * Include necessary headers.
20 */
21
7a14d768 22#include "backend-private.h"
eac3a0a0 23#include <cups/array-private.h>
ef416fc2 24#include <sys/types.h>
25#include <sys/stat.h>
ef416fc2 26#include <sys/wait.h>
eac3a0a0
MS
27#if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
28# include <xpc/xpc.h>
29# define kPMPrintUIToolAgent "com.apple.printuitool.agent"
30# define kPMStartJob 100
31# define kPMWaitForJob 101
82cc1f9a
MS
32# ifdef HAVE_XPC_PRIVATE_H
33# include <xpc/private.h>
34# else
eac3a0a0
MS
35extern void xpc_connection_set_target_uid(xpc_connection_t connection,
36 uid_t uid);
82cc1f9a 37# endif /* HAVE_XPC_PRIVATE_H */
eac3a0a0 38#endif /* HAVE_GSSAPI && HAVE_XPC */
ef416fc2 39
7cf5915e 40
a469f8a5
MS
41/*
42 * Bits for job-state-reasons we care about...
43 */
44
45#define _CUPS_JSR_ACCOUNT_AUTHORIZATION_FAILED 0x01
46#define _CUPS_JSR_ACCOUNT_CLOSED 0x02
47#define _CUPS_JSR_ACCOUNT_INFO_NEEDED 0x04
48#define _CUPS_JSR_ACCOUNT_LIMIT_REACHED 0x08
49#define _CUPS_JSR_JOB_PASSWORD_WAIT 0x10
50#define _CUPS_JSR_JOB_RELEASE_WAIT 0x20
51
52
7cf5915e
MS
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 */
a29fd7dd 65 job_id, /* Job ID for submitted job */
a469f8a5 66 job_reasons, /* Job state reasons bits */
a29fd7dd 67 get_job_attrs; /* Support Get-Job-Attributes? */
dcb445bc 68 const char *job_name; /* Job name for submitted job */
7cf5915e
MS
69 http_encryption_t encryption; /* Use encryption? */
70 ipp_jstate_t job_state; /* Current job state */
71 ipp_pstate_t printer_state; /* Current printer state */
72} _cups_monitor_t;
73
74
ef416fc2 75/*
76 * Globals...
77 */
78
eac3a0a0 79static const char *auth_info_required;
18ecb428 80 /* New auth-info-required value */
eac3a0a0 81#if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
7e86f2f6 82static pid_t child_pid = 0; /* Child process ID */
eac3a0a0 83#endif /* HAVE_GSSAPI && HAVE_XPC */
7cf5915e
MS
84static const char * const jattrs[] = /* Job attributes we want */
85{
3dd9c340 86 "job-id",
eac3a0a0 87 "job-impressions-completed",
7cf5915e 88 "job-media-sheets-completed",
dcb445bc
MS
89 "job-name",
90 "job-originating-user-name",
7cf5915e
MS
91 "job-state",
92 "job-state-reasons"
93};
cf3d4dd6 94static int job_canceled = 0,
eac3a0a0 95 /* Job cancelled? */
cf3d4dd6
MS
96 uri_credentials = 0;
97 /* Credentials supplied in URI? */
3e7fe0ca
MS
98static char username[256] = "",
99 /* Username for device URI */
100 *password = NULL;
eac3a0a0 101 /* Password for device URI */
7cf5915e
MS
102static const char * const pattrs[] = /* Printer attributes we want */
103{
a469f8a5
MS
104#ifdef HAVE_LIBZ
105 "compression-supported",
106#endif /* HAVE_LIBZ */
7cf5915e
MS
107 "copies-supported",
108 "cups-version",
109 "document-format-supported",
110 "marker-colors",
111 "marker-high-levels",
112 "marker-levels",
113 "marker-low-levels",
114 "marker-message",
115 "marker-names",
116 "marker-types",
117 "media-col-supported",
84315f46 118 "multiple-document-handling-supported",
c8fef167 119 "operations-supported",
3463c27a 120 "print-color-mode-supported",
7cf5915e
MS
121 "printer-alert",
122 "printer-alert-description",
123 "printer-is-accepting-jobs",
124 "printer-state",
125 "printer-state-message",
a4845881 126 "printer-state-reasons"
7cf5915e 127};
07ed0e9a
MS
128static const char * const remote_job_states[] =
129{ /* Remote job state keywords */
88f9aafc
MS
130 "+cups-remote-pending",
131 "+cups-remote-pending-held",
132 "+cups-remote-processing",
133 "+cups-remote-stopped",
134 "+cups-remote-canceled",
135 "+cups-remote-aborted",
136 "+cups-remote-completed"
07ed0e9a 137};
321d8d57 138static _cups_mutex_t report_mutex = _CUPS_MUTEX_INITIALIZER;
eac3a0a0 139 /* Mutex to control access */
321d8d57
MS
140static int num_attr_cache = 0;
141 /* Number of cached attributes */
142static cups_option_t *attr_cache = NULL;
143 /* Cached attributes */
144static cups_array_t *state_reasons; /* Array of printe-state-reasons keywords */
eac3a0a0
MS
145static char tmpfilename[1024] = "";
146 /* Temporary spool file name */
ef416fc2 147
148
149/*
150 * Local functions...
151 */
152
7cf5915e
MS
153static void cancel_job(http_t *http, const char *uri, int id,
154 const char *resource, const char *user,
155 int version);
156static ipp_pstate_t check_printer_state(http_t *http, const char *uri,
157 const char *resource,
321d8d57 158 const char *user, int version);
7cf5915e 159static void *monitor_printer(_cups_monitor_t *monitor);
0268488e
MS
160static ipp_t *new_request(ipp_op_t op, int version, const char *uri,
161 const char *user, const char *title,
162 int num_options, cups_option_t *options,
163 const char *compression, int copies,
f14324a7 164 const char *format, _ppd_cache_t *pc,
a469f8a5 165 ppd_file_t *ppd,
84315f46 166 ipp_attribute_t *media_col_sup,
a469f8a5 167 ipp_attribute_t *doc_handling_sup,
f2a7bf2a 168 ipp_attribute_t *print_color_mode_sup);
5a9febac
MS
169static const char *password_cb(const char *prompt, http_t *http,
170 const char *method, const char *resource,
6961465f 171 int *user_data);
5a9febac 172static const char *quote_string(const char *s, char *q, size_t qsize);
7cf5915e 173static void report_attr(ipp_attribute_t *attr);
321d8d57 174static void report_printer_state(ipp_t *ipp);
eac3a0a0 175#if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
37e7e6e0 176static int run_as_user(char *argv[], uid_t uid,
eac3a0a0
MS
177 const char *device_uri, int fd);
178#endif /* HAVE_GSSAPI && HAVE_XPC */
7cf5915e 179static void sigterm_handler(int sig);
f228370c 180static int timeout_cb(http_t *http, void *user_data);
eac3a0a0 181static void update_reasons(ipp_attribute_t *attr, const char *s);
ef416fc2 182
183
184/*
185 * 'main()' - Send a file to the printer or server.
186 *
187 * Usage:
188 *
189 * printer-uri job-id user title copies options [file]
190 */
191
192int /* O - Exit status */
bd7854cb 193main(int argc, /* I - Number of command-line args */
ef416fc2 194 char *argv[]) /* I - Command-line arguments */
195{
196 int i; /* Looping var */
b94498cf 197 int send_options; /* Send job options? */
ef416fc2 198 int num_options; /* Number of printer options */
199 cups_option_t *options; /* Printer options */
1f0275e3 200 const char *device_uri; /* Device URI */
acb056cb 201 char scheme[255], /* Scheme in URI */
ef416fc2 202 hostname[1024], /* Hostname */
ef416fc2 203 resource[1024], /* Resource info (printer name) */
26d47ec6 204 addrname[256], /* Address name */
ef416fc2 205 *optptr, /* Pointer to URI options */
db1f069b
MS
206 *name, /* Name of option */
207 *value, /* Value of option */
208 sep; /* Separator character */
6961465f 209 int password_tries = 0; /* Password tries */
c8fef167 210 http_addrlist_t *addrlist; /* Address of printer */
5a9febac 211 int snmp_enabled = 1; /* Is SNMP enabled? */
7a14d768
MS
212 int snmp_fd, /* SNMP socket */
213 start_count, /* Page count via SNMP at start */
426c6a59
MS
214 page_count, /* Page count via SNMP */
215 have_supplies; /* Printer supports supply levels? */
bd7854cb 216 int num_files; /* Number of files to print */
82f97232
MS
217 char **files, /* Files to print */
218 *compatfile = NULL; /* Compatibility filename */
219 off_t compatsize = 0; /* Size of compatibility file */
ef416fc2 220 int port; /* Port number (not used) */
c8fef167 221 char portname[255]; /* Port name */
ef416fc2 222 char uri[HTTP_MAX_URI]; /* Updated URI without user/pass */
dcb445bc 223 char print_job_name[1024]; /* Update job-name for Print-Job */
cc754834 224 http_status_t http_status; /* Status of HTTP request */
ef416fc2 225 ipp_status_t ipp_status; /* Status of IPP request */
226 http_t *http; /* HTTP connection */
227 ipp_t *request, /* IPP request */
228 *response, /* IPP response */
229 *supported; /* get-printer-attributes response */
c0e1af83 230 time_t start_time; /* Time of first connect */
c0e1af83 231 int contimeout; /* Connection timeout */
f14324a7
MS
232 int delay, /* Delay for retries */
233 prev_delay; /* Previous delay */
0268488e
MS
234 const char *compression; /* Compression mode */
235 int waitjob, /* Wait for job complete? */
a469f8a5 236 waitjob_tries = 0, /* Number of times we've waited */
ef416fc2 237 waitprinter; /* Wait for printer ready? */
7cf5915e 238 _cups_monitor_t monitor; /* Monitoring data */
ef416fc2 239 ipp_attribute_t *job_id_attr; /* job-id attribute */
240 int job_id; /* job-id value */
bd7854cb 241 ipp_attribute_t *job_sheets; /* job-media-sheets-completed */
242 ipp_attribute_t *job_state; /* job-state */
a469f8a5
MS
243#ifdef HAVE_LIBZ
244 ipp_attribute_t *compression_sup; /* compression-supported */
245#endif /* HAVE_LIBZ */
bd7854cb 246 ipp_attribute_t *copies_sup; /* copies-supported */
cc754834 247 ipp_attribute_t *cups_version; /* cups-version */
bd7854cb 248 ipp_attribute_t *format_sup; /* document-format-supported */
c5b24bfa 249 ipp_attribute_t *job_auth; /* job-authorization-uri */
cc754834 250 ipp_attribute_t *media_col_sup; /* media-col-supported */
0268488e 251 ipp_attribute_t *operations_sup; /* operations-supported */
84315f46 252 ipp_attribute_t *doc_handling_sup; /* multiple-document-handling-supported */
ef416fc2 253 ipp_attribute_t *printer_state; /* printer-state attribute */
bd7854cb 254 ipp_attribute_t *printer_accepting; /* printer-is-accepting-jobs */
f2a7bf2a 255 ipp_attribute_t *print_color_mode_sup;/* Does printer support print-color-mode? */
dcb445bc 256 int create_job = 0, /* Does printer support Create-Job? */
a29fd7dd 257 get_job_attrs = 0, /* Does printer support Get-Job-Attributes? */
dcb445bc 258 send_document = 0, /* Does printer support Send-Document? */
a469f8a5 259 validate_job = 0, /* Does printer support Validate-Job? */
f2a7bf2a 260 copies, /* Number of copies for job */
d09495fa 261 copies_remaining; /* Number of copies remaining */
c277e2f8 262 const char *content_type, /* CONTENT_TYPE environment variable */
0268488e
MS
263 *final_content_type, /* FINAL_CONTENT_TYPE environment var */
264 *document_format; /* document-format value */
cc754834 265 int fd; /* File descriptor */
eac3a0a0 266 off_t bytes = 0; /* Bytes copied */
cc754834 267 char buffer[16384]; /* Copy buffer */
ef416fc2 268#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
269 struct sigaction action; /* Actions for POSIX signals */
270#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
271 int version; /* IPP version */
a469f8a5
MS
272 ppd_file_t *ppd = NULL; /* PPD file */
273 _ppd_cache_t *pc = NULL; /* PPD cache and mapping data */
eac3a0a0 274 fd_set input; /* Input set for select() */
ef416fc2 275
276
277 /*
278 * Make sure status messages are not buffered...
279 */
280
281 setbuf(stderr, NULL);
282
283 /*
284 * Ignore SIGPIPE and catch SIGTERM signals...
285 */
286
287#ifdef HAVE_SIGSET
288 sigset(SIGPIPE, SIG_IGN);
289 sigset(SIGTERM, sigterm_handler);
290#elif defined(HAVE_SIGACTION)
291 memset(&action, 0, sizeof(action));
292 action.sa_handler = SIG_IGN;
293 sigaction(SIGPIPE, &action, NULL);
294
295 sigemptyset(&action.sa_mask);
296 sigaddset(&action.sa_mask, SIGTERM);
297 action.sa_handler = sigterm_handler;
298 sigaction(SIGTERM, &action, NULL);
299#else
300 signal(SIGPIPE, SIG_IGN);
301 signal(SIGTERM, sigterm_handler);
302#endif /* HAVE_SIGSET */
303
304 /*
305 * Check command-line...
306 */
307
308 if (argc == 1)
309 {
310 char *s;
311
312 if ((s = strrchr(argv[0], '/')) != NULL)
313 s ++;
314 else
315 s = argv[0];
316
8b450588
MS
317 printf("network %s \"Unknown\" \"%s (%s)\"\n",
318 s, _cupsLangString(cupsLangDefault(),
319 _("Internet Printing Protocol")), s);
ef416fc2 320 return (CUPS_BACKEND_OK);
321 }
bd7854cb 322 else if (argc < 6)
ef416fc2 323 {
db1f069b 324 _cupsLangPrintf(stderr,
0837b7e8 325 _("Usage: %s job-id user title copies options [file]"),
db1f069b 326 argv[0]);
ef416fc2 327 return (CUPS_BACKEND_STOP);
328 }
329
eac3a0a0
MS
330 /*
331 * Get the device URI...
332 */
333
334 while ((device_uri = cupsBackendDeviceURI(argv)) == NULL)
335 {
336 _cupsLangPrintFilter(stderr, "INFO", _("Unable to locate printer."));
337 sleep(10);
338
339 if (getenv("CLASS") != NULL)
340 return (CUPS_BACKEND_FAILED);
341 }
342
343 if ((auth_info_required = getenv("AUTH_INFO_REQUIRED")) == NULL)
344 auth_info_required = "none";
345
5a9febac 346 state_reasons = _cupsArrayNewStrings(getenv("PRINTER_STATE_REASONS"), ',');
eac3a0a0
MS
347
348#ifdef HAVE_GSSAPI
349 /*
350 * For Kerberos, become the printing user (if we can) to get the credentials
351 * that way.
352 */
353
c4aa2975
MS
354 if (!getuid() && (value = getenv("AUTH_UID")) != NULL &&
355 !getenv("AUTH_PASSWORD"))
eac3a0a0
MS
356 {
357 uid_t uid = (uid_t)atoi(value);
358 /* User ID */
359
360# ifdef HAVE_XPC
361 if (uid > 0)
362 {
363 if (argc == 6)
37e7e6e0 364 return (run_as_user(argv, uid, device_uri, 0));
eac3a0a0
MS
365 else
366 {
367 int status = 0; /* Exit status */
368
369 for (i = 6; i < argc && !status && !job_canceled; i ++)
370 {
371 if ((fd = open(argv[i], O_RDONLY)) >= 0)
372 {
37e7e6e0 373 status = run_as_user(argv, uid, device_uri, fd);
eac3a0a0
MS
374 close(fd);
375 }
376 else
377 {
378 _cupsLangPrintError("ERROR", _("Unable to open print file"));
379 status = CUPS_BACKEND_FAILED;
380 }
381 }
382
383 return (status);
384 }
385 }
386
387# else /* No XPC, just try to run as the user ID */
388 if (uid > 0)
389 seteuid(uid);
390# endif /* HAVE_XPC */
391 }
392#endif /* HAVE_GSSAPI */
393
ef416fc2 394 /*
bd7854cb 395 * Get the (final) content type...
ef416fc2 396 */
397
c277e2f8
MS
398 if ((content_type = getenv("CONTENT_TYPE")) == NULL)
399 content_type = "application/octet-stream";
ef416fc2 400
c277e2f8
MS
401 if ((final_content_type = getenv("FINAL_CONTENT_TYPE")) == NULL)
402 {
403 final_content_type = content_type;
404
405 if (!strncmp(final_content_type, "printer/", 8))
406 final_content_type = "application/vnd.cups-raw";
407 }
d09495fa 408
ef416fc2 409 /*
410 * Extract the hostname and printer name from the URI...
411 */
412
acb056cb
MS
413 httpSeparateURI(HTTP_URI_CODING_ALL, device_uri, scheme, sizeof(scheme),
414 username, sizeof(username), hostname, sizeof(hostname), &port,
415 resource, sizeof(resource));
ef416fc2 416
a41f09e2
MS
417 if (!port)
418 port = IPP_PORT; /* Default to port 631 */
419
94436c5a 420 if (!strcmp(scheme, "https") || !strcmp(scheme, "ipps"))
ef416fc2 421 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS);
8ca02f3c 422 else
423 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED);
ef416fc2 424
ef416fc2 425 /*
426 * See if there are any options...
427 */
428
0268488e 429 compression = NULL;
cc754834 430 version = 20;
ef416fc2 431 waitjob = 1;
432 waitprinter = 1;
c0e1af83 433 contimeout = 7 * 24 * 60 * 60;
ef416fc2 434
435 if ((optptr = strchr(resource, '?')) != NULL)
436 {
437 /*
438 * Yup, terminate the device name string and move to the first
439 * character of the optptr...
440 */
441
442 *optptr++ = '\0';
443
444 /*
445 * Then parse the optptr...
446 */
447
448 while (*optptr)
449 {
450 /*
451 * Get the name...
452 */
453
db1f069b 454 name = optptr;
ef416fc2 455
db1f069b
MS
456 while (*optptr && *optptr != '=' && *optptr != '+' && *optptr != '&')
457 optptr ++;
458
459 if ((sep = *optptr) != '\0')
460 *optptr++ = '\0';
461
462 if (sep == '=')
ef416fc2 463 {
464 /*
465 * Get the value...
466 */
467
db1f069b 468 value = optptr;
ef416fc2 469
db1f069b 470 while (*optptr && *optptr != '+' && *optptr != '&')
ef416fc2 471 optptr ++;
db1f069b
MS
472
473 if (*optptr)
474 *optptr++ = '\0';
ef416fc2 475 }
476 else
db1f069b 477 value = (char *)"";
ef416fc2 478
479 /*
480 * Process the option...
481 */
482
88f9aafc 483 if (!_cups_strcasecmp(name, "waitjob"))
ef416fc2 484 {
485 /*
486 * Wait for job completion?
487 */
488
88f9aafc
MS
489 waitjob = !_cups_strcasecmp(value, "on") ||
490 !_cups_strcasecmp(value, "yes") ||
491 !_cups_strcasecmp(value, "true");
ef416fc2 492 }
88f9aafc 493 else if (!_cups_strcasecmp(name, "waitprinter"))
ef416fc2 494 {
495 /*
496 * Wait for printer idle?
497 */
498
88f9aafc
MS
499 waitprinter = !_cups_strcasecmp(value, "on") ||
500 !_cups_strcasecmp(value, "yes") ||
501 !_cups_strcasecmp(value, "true");
ef416fc2 502 }
88f9aafc 503 else if (!_cups_strcasecmp(name, "encryption"))
ef416fc2 504 {
505 /*
506 * Enable/disable encryption?
507 */
508
88f9aafc 509 if (!_cups_strcasecmp(value, "always"))
ef416fc2 510 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS);
88f9aafc 511 else if (!_cups_strcasecmp(value, "required"))
ef416fc2 512 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
88f9aafc 513 else if (!_cups_strcasecmp(value, "never"))
ef416fc2 514 cupsSetEncryption(HTTP_ENCRYPT_NEVER);
88f9aafc 515 else if (!_cups_strcasecmp(value, "ifrequested"))
ef416fc2 516 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED);
517 else
518 {
0837b7e8
MS
519 _cupsLangPrintFilter(stderr, "ERROR",
520 _("Unknown encryption option value: \"%s\"."),
521 value);
ef416fc2 522 }
523 }
5a9febac
MS
524 else if (!_cups_strcasecmp(name, "snmp"))
525 {
526 /*
527 * Enable/disable SNMP stuff...
528 */
529
530 snmp_enabled = !value[0] || !_cups_strcasecmp(value, "on") ||
531 _cups_strcasecmp(value, "yes") ||
532 _cups_strcasecmp(value, "true");
533 }
88f9aafc 534 else if (!_cups_strcasecmp(name, "version"))
ef416fc2 535 {
536 if (!strcmp(value, "1.0"))
c168a833 537 version = 10;
ef416fc2 538 else if (!strcmp(value, "1.1"))
c168a833
MS
539 version = 11;
540 else if (!strcmp(value, "2.0"))
541 version = 20;
542 else if (!strcmp(value, "2.1"))
543 version = 21;
0837b7e8
MS
544 else if (!strcmp(value, "2.2"))
545 version = 22;
ef416fc2 546 else
547 {
0837b7e8
MS
548 _cupsLangPrintFilter(stderr, "ERROR",
549 _("Unknown version option value: \"%s\"."),
550 value);
ef416fc2 551 }
552 }
b423cd4c 553#ifdef HAVE_LIBZ
88f9aafc 554 else if (!_cups_strcasecmp(name, "compression"))
b423cd4c 555 {
88f9aafc
MS
556 if (!_cups_strcasecmp(value, "true") || !_cups_strcasecmp(value, "yes") ||
557 !_cups_strcasecmp(value, "on") || !_cups_strcasecmp(value, "gzip"))
0268488e 558 compression = "gzip";
a469f8a5
MS
559 else if (!_cups_strcasecmp(value, "deflate"))
560 compression = "deflate";
561 else if (!_cups_strcasecmp(value, "false") ||
562 !_cups_strcasecmp(value, "no") ||
563 !_cups_strcasecmp(value, "off") ||
564 !_cups_strcasecmp(value, "none"))
565 compression = "none";
b423cd4c 566 }
567#endif /* HAVE_LIBZ */
88f9aafc 568 else if (!_cups_strcasecmp(name, "contimeout"))
c0e1af83 569 {
570 /*
571 * Set the connection timeout...
572 */
573
574 if (atoi(value) > 0)
575 contimeout = atoi(value);
576 }
ef416fc2 577 else
578 {
579 /*
580 * Unknown option...
581 */
582
0837b7e8
MS
583 _cupsLangPrintFilter(stderr, "ERROR",
584 _("Unknown option \"%s\" with value \"%s\"."),
585 name, value);
ef416fc2 586 }
587 }
588 }
589
b423cd4c 590 /*
591 * If we have 7 arguments, print the file named on the command-line.
592 * Otherwise, copy stdin to a temporary file and print the temporary
593 * file.
594 */
595
596 if (argc == 6)
597 {
cc754834 598 num_files = 0;
f228370c 599 files = NULL;
88f9aafc
MS
600 send_options = !_cups_strcasecmp(final_content_type, "application/pdf") ||
601 !_cups_strcasecmp(final_content_type, "application/vnd.cups-pdf") ||
602 !_cups_strncasecmp(final_content_type, "image/", 6);
cc754834
MS
603
604 fputs("DEBUG: Sending stdin for job...\n", stderr);
b423cd4c 605 }
606 else
607 {
608 /*
609 * Point to the files on the command-line...
610 */
611
c277e2f8
MS
612 num_files = argc - 6;
613 files = argv + 6;
614 send_options = 1;
b94498cf 615
cc754834
MS
616 fprintf(stderr, "DEBUG: %d files to send in job...\n", num_files);
617 }
b423cd4c 618
ef416fc2 619 /*
620 * Set the authentication info, if any...
621 */
622
6961465f 623 cupsSetPasswordCB2((cups_password_cb2_t)password_cb, &password_tries);
ef416fc2 624
625 if (username[0])
626 {
627 /*
5a9febac 628 * Use authentication information in the device URI...
ef416fc2 629 */
630
631 if ((password = strchr(username, ':')) != NULL)
632 *password++ = '\0';
633
634 cupsSetUser(username);
cf3d4dd6 635 uri_credentials = 1;
ef416fc2 636 }
82f97232 637 else
ef416fc2 638 {
639 /*
09a101d6 640 * Try loading authentication information from the environment.
ef416fc2 641 */
642
db1f069b
MS
643 const char *ptr = getenv("AUTH_USERNAME");
644
645 if (ptr)
3e7fe0ca
MS
646 {
647 strlcpy(username, ptr, sizeof(username));
09a101d6 648 cupsSetUser(ptr);
3e7fe0ca 649 }
ef416fc2 650
09a101d6 651 password = getenv("AUTH_PASSWORD");
ef416fc2 652 }
653
654 /*
c8fef167 655 * Try finding the remote server...
ef416fc2 656 */
657
4d301e69 658 start_time = time(NULL);
c0e1af83 659
c8fef167
MS
660 sprintf(portname, "%d", port);
661
eac3a0a0 662 update_reasons(NULL, "+connecting-to-device");
c8fef167
MS
663 fprintf(stderr, "DEBUG: Looking up \"%s\"...\n", hostname);
664
665 while ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL)
666 {
667 _cupsLangPrintFilter(stderr, "INFO",
668 _("Unable to locate printer \"%s\"."), hostname);
669 sleep(10);
670
671 if (getenv("CLASS") != NULL)
672 {
eac3a0a0 673 update_reasons(NULL, "-connecting-to-device");
c8fef167
MS
674 return (CUPS_BACKEND_STOP);
675 }
f3c17241
MS
676
677 if (job_canceled)
678 return (CUPS_BACKEND_OK);
c8fef167
MS
679 }
680
db8b865d
MS
681 http = httpConnect2(hostname, port, addrlist, AF_UNSPEC, cupsEncryption(), 1,
682 0, NULL);
f228370c 683 httpSetTimeout(http, 30.0, timeout_cb, NULL);
c8fef167 684
f51f3773
MS
685 if (httpIsEncrypted(http))
686 {
687 /*
688 * Validate TLS credentials...
689 */
690
691 cups_array_t *creds; /* TLS credentials */
692 http_trust_t trust; /* Trust level */
693 static const char *trusts[] = { NULL, "+cups-pki-invalid", "+cups-pki-changed", "+cups-pki-expired", NULL, "+cups-pki-unknown" };
694 /* Trust keywords */
695
696 if (!httpCopyCredentials(http, &creds))
697 {
698 trust = httpCredentialsGetTrust(creds, hostname);
699
700 update_reasons(NULL, "-cups-pki-invalid,cups-pki-changed,cups-pki-expired,cups-pki-unknown");
701 if (trusts[trust])
702 {
703 update_reasons(NULL, trusts[trust]);
704 return (CUPS_BACKEND_STOP);
705 }
706
707 httpFreeCredentials(creds);
708 }
709 }
710
c8fef167
MS
711 /*
712 * See if the printer supports SNMP...
713 */
714
5a9febac
MS
715 if (snmp_enabled)
716 snmp_fd = _cupsSNMPOpen(addrlist->addr.addr.sa_family);
717 else
718 snmp_fd = -1;
719
720 if (snmp_fd >= 0)
c8fef167
MS
721 have_supplies = !backendSNMPSupplies(snmp_fd, &(addrlist->addr),
722 &start_count, NULL);
c8fef167
MS
723 else
724 have_supplies = start_count = 0;
725
726 /*
727 * Wait for data from the filter...
728 */
729
730 if (num_files == 0)
eac3a0a0 731 {
f14324a7 732 if (!backendWaitLoop(snmp_fd, &(addrlist->addr), 0, backendNetworkSideCB))
c8fef167 733 return (CUPS_BACKEND_OK);
eac3a0a0
MS
734 else if ((bytes = read(0, buffer, sizeof(buffer))) <= 0)
735 return (CUPS_BACKEND_OK);
736 }
c8fef167
MS
737
738 /*
739 * Try connecting to the remote server...
740 */
741
f14324a7 742 delay = _cupsNextDelay(0, &prev_delay);
757d2cad 743
ef416fc2 744 do
745 {
acb056cb 746 fprintf(stderr, "DEBUG: Connecting to %s:%d\n", hostname, port);
0837b7e8 747 _cupsLangPrintFilter(stderr, "INFO", _("Connecting to printer."));
ef416fc2 748
c8fef167 749 if (httpReconnect(http))
ef416fc2 750 {
c7017ecc
MS
751 int error = errno; /* Connection error */
752
c8fef167 753 if (http->status == HTTP_PKI_ERROR)
eac3a0a0 754 update_reasons(NULL, "+cups-certificate-error");
c8fef167 755
cc754834 756 if (job_canceled)
7594b224 757 break;
758
ef416fc2 759 if (getenv("CLASS") != NULL)
760 {
761 /*
762 * If the CLASS environment variable is set, the job was submitted
763 * to a class and not to a specific queue. In this case, we want
764 * to abort immediately so that the job can be requeued on the next
765 * available printer in the class.
766 */
767
0837b7e8
MS
768 _cupsLangPrintFilter(stderr, "INFO",
769 _("Unable to contact printer, queuing on next "
770 "printer in class."));
ef416fc2 771
ef416fc2 772 /*
773 * Sleep 5 seconds to keep the job from requeuing too rapidly...
774 */
775
776 sleep(5);
777
eac3a0a0 778 update_reasons(NULL, "-connecting-to-device");
c8fef167 779
ef416fc2 780 return (CUPS_BACKEND_FAILED);
781 }
782
c7017ecc
MS
783 fprintf(stderr, "DEBUG: Connection error: %s\n", strerror(errno));
784
ef416fc2 785 if (errno == ECONNREFUSED || errno == EHOSTDOWN ||
786 errno == EHOSTUNREACH)
787 {
c0e1af83 788 if (contimeout && (time(NULL) - start_time) > contimeout)
789 {
0837b7e8
MS
790 _cupsLangPrintFilter(stderr, "ERROR",
791 _("The printer is not responding."));
eac3a0a0 792 update_reasons(NULL, "-connecting-to-device");
c0e1af83 793 return (CUPS_BACKEND_FAILED);
794 }
795
c7017ecc
MS
796 switch (error)
797 {
798 case EHOSTDOWN :
0837b7e8 799 _cupsLangPrintFilter(stderr, "WARNING",
22c9029b
MS
800 _("The printer may not exist or "
801 "is unavailable at this time."));
c7017ecc
MS
802 break;
803
804 case EHOSTUNREACH :
0837b7e8 805 _cupsLangPrintFilter(stderr, "WARNING",
22c9029b
MS
806 _("The printer is unreachable at this "
807 "time."));
c7017ecc
MS
808 break;
809
810 case ECONNREFUSED :
811 default :
0837b7e8 812 _cupsLangPrintFilter(stderr, "WARNING",
f3c17241 813 _("The printer is in use."));
c7017ecc
MS
814 break;
815 }
c0e1af83 816
7e86f2f6 817 sleep((unsigned)delay);
c0e1af83 818
f14324a7 819 delay = _cupsNextDelay(delay, &prev_delay);
ef416fc2 820 }
ef416fc2 821 else
822 {
0837b7e8 823 _cupsLangPrintFilter(stderr, "ERROR",
22c9029b 824 _("The printer is not responding."));
ef416fc2 825 sleep(30);
826 }
7594b224 827
cc754834 828 if (job_canceled)
7594b224 829 break;
ef416fc2 830 }
c8fef167 831 else
eac3a0a0 832 update_reasons(NULL, "-cups-certificate-error");
ef416fc2 833 }
c8fef167 834 while (http->fd < 0);
ef416fc2 835
f3c17241
MS
836 if (job_canceled)
837 return (CUPS_BACKEND_OK);
838 else if (!http)
7594b224 839 return (CUPS_BACKEND_FAILED);
7594b224 840
eac3a0a0 841 update_reasons(NULL, "-connecting-to-device");
0837b7e8 842 _cupsLangPrintFilter(stderr, "INFO", _("Connected to printer."));
ef416fc2 843
22c9029b
MS
844 fprintf(stderr, "DEBUG: Connected to %s:%d...\n",
845 httpAddrString(http->hostaddr, addrname, sizeof(addrname)),
a469f8a5 846 httpAddrPort(http->hostaddr));
26d47ec6 847
ef416fc2 848 /*
849 * Build a URI for the printer and fill the standard IPP attributes for
850 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
851 * might contain username:password information...
852 */
853
acb056cb
MS
854 httpAssembleURI(HTTP_URI_CODING_ALL, uri, sizeof(uri), scheme, NULL, hostname,
855 port, resource);
ef416fc2 856
857 /*
858 * First validate the destination and see if the device supports multiple
cc754834 859 * copies...
ef416fc2 860 */
861
a469f8a5 862#ifdef HAVE_LIBZ
7e86f2f6 863 compression_sup = NULL;
a469f8a5 864#endif /* HAVE_LIBZ */
7e86f2f6
MS
865 copies_sup = NULL;
866 cups_version = NULL;
867 format_sup = NULL;
868 media_col_sup = NULL;
869 supported = NULL;
870 operations_sup = NULL;
871 doc_handling_sup = NULL;
872 print_color_mode_sup = NULL;
ef416fc2 873
874 do
875 {
7a14d768
MS
876 /*
877 * Check for side-channel requests...
878 */
879
880 backendCheckSideChannel(snmp_fd, http->hostaddr);
881
ef416fc2 882 /*
883 * Build the IPP request...
884 */
885
fa73b229 886 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
7e86f2f6 887 ippSetVersion(request, version / 10, version % 10);
ef416fc2 888 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
889 NULL, uri);
890
891 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
892 "requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]),
893 NULL, pattrs);
894
895 /*
896 * Do the request...
897 */
898
899 fputs("DEBUG: Getting supported attributes...\n", stderr);
900
97c9a8d7 901 if (http->version < HTTP_1_1)
cc754834 902 {
c7017ecc
MS
903 fprintf(stderr, "DEBUG: Printer responded with HTTP version %d.%d.\n",
904 http->version / 100, http->version % 100);
eac3a0a0
MS
905 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
906 "cups-ipp-wrong-http-version");
cc754834 907 }
97c9a8d7 908
7cf5915e
MS
909 supported = cupsDoRequest(http, request, resource);
910 ipp_status = cupsLastError();
ef416fc2 911
22c9029b
MS
912 fprintf(stderr, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
913 ippErrorString(ipp_status), cupsLastErrorString());
914
3e7fe0ca
MS
915 if (ipp_status <= IPP_OK_CONFLICT)
916 password_tries = 0;
917 else
ef416fc2 918 {
7cf5915e
MS
919 fprintf(stderr, "DEBUG: Get-Printer-Attributes returned %s.\n",
920 ippErrorString(ipp_status));
921
ef416fc2 922 if (ipp_status == IPP_PRINTER_BUSY ||
923 ipp_status == IPP_SERVICE_UNAVAILABLE)
924 {
c0e1af83 925 if (contimeout && (time(NULL) - start_time) > contimeout)
926 {
0837b7e8
MS
927 _cupsLangPrintFilter(stderr, "ERROR",
928 _("The printer is not responding."));
c0e1af83 929 return (CUPS_BACKEND_FAILED);
930 }
931
f3c17241 932 _cupsLangPrintFilter(stderr, "INFO", _("The printer is in use."));
c0e1af83 933
321d8d57 934 report_printer_state(supported);
c0e1af83 935
7e86f2f6 936 sleep((unsigned)delay);
c0e1af83 937
f14324a7 938 delay = _cupsNextDelay(delay, &prev_delay);
ef416fc2 939 }
940 else if ((ipp_status == IPP_BAD_REQUEST ||
c168a833 941 ipp_status == IPP_VERSION_NOT_SUPPORTED) && version > 10)
ef416fc2 942 {
943 /*
cc754834 944 * Switch to IPP/1.1 or IPP/1.0...
ef416fc2 945 */
946
cc754834
MS
947 if (version >= 20)
948 {
a469f8a5
MS
949 _cupsLangPrintFilter(stderr, "INFO", _("Preparing to print."));
950 fprintf(stderr,
951 "DEBUG: The printer does not support IPP/%d.%d, trying "
952 "IPP/1.1.\n", version / 10, version % 10);
cc754834
MS
953 version = 11;
954 }
955 else
956 {
a469f8a5
MS
957 _cupsLangPrintFilter(stderr, "INFO", _("Preparing to print."));
958 fprintf(stderr,
959 "DEBUG: The printer does not support IPP/%d.%d, trying "
960 "IPP/1.0.\n", version / 10, version % 10);
cc754834
MS
961 version = 10;
962 }
963
ef416fc2 964 httpReconnect(http);
965 }
966 else if (ipp_status == IPP_NOT_FOUND)
967 {
0837b7e8 968 _cupsLangPrintFilter(stderr, "ERROR",
a469f8a5
MS
969 _("The printer configuration is incorrect or the "
970 "printer no longer exists."));
ef416fc2 971
22c9029b 972 ippDelete(supported);
ef416fc2 973
974 return (CUPS_BACKEND_STOP);
975 }
3e7fe0ca
MS
976 else if (ipp_status == IPP_FORBIDDEN ||
977 ipp_status == IPP_AUTHENTICATION_CANCELED)
41681883 978 {
12f89d24
MS
979 const char *www_auth = httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE);
980 /* WWW-Authenticate field value */
981
982 if (!strncmp(www_auth, "Negotiate", 9))
41681883 983 auth_info_required = "negotiate";
12f89d24
MS
984 else if (www_auth[0])
985 auth_info_required = "username,password";
41681883
MS
986
987 fprintf(stderr, "ATTR: auth-info-required=%s\n", auth_info_required);
988 return (CUPS_BACKEND_AUTH_REQUIRED);
989 }
3e7fe0ca 990 else if (ipp_status != IPP_NOT_AUTHORIZED)
ef416fc2 991 {
0837b7e8 992 _cupsLangPrintFilter(stderr, "ERROR",
22c9029b 993 _("Unable to get printer status."));
ef416fc2 994 sleep(10);
5a9febac
MS
995
996 httpReconnect(http);
ef416fc2 997 }
998
22c9029b
MS
999 ippDelete(supported);
1000 supported = NULL;
ef416fc2 1001 continue;
1002 }
cc754834 1003
eac3a0a0 1004 if (!getenv("CLASS"))
22c9029b 1005 {
eac3a0a0
MS
1006 /*
1007 * Check printer-is-accepting-jobs = false and printer-state-reasons for the
1008 * "spool-area-full" keyword...
1009 */
22c9029b 1010
eac3a0a0
MS
1011 int busy = 0;
1012
1013 if ((printer_accepting = ippFindAttribute(supported,
1014 "printer-is-accepting-jobs",
1015 IPP_TAG_BOOLEAN)) != NULL &&
1016 !printer_accepting->values[0].boolean)
1017 busy = 1;
1018 else if (!printer_accepting)
1019 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1020 "cups-ipp-missing-printer-is-accepting-jobs");
88f9aafc 1021
eac3a0a0
MS
1022 if ((printer_state = ippFindAttribute(supported,
1023 "printer-state-reasons",
a469f8a5
MS
1024 IPP_TAG_KEYWORD)) == NULL)
1025 {
1026 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1027 "cups-ipp-missing-printer-state-reasons");
1028 }
1029 else if (!busy)
eac3a0a0
MS
1030 {
1031 for (i = 0; i < printer_state->num_values; i ++)
a469f8a5 1032 {
eac3a0a0
MS
1033 if (!strcmp(printer_state->values[0].string.text,
1034 "spool-area-full") ||
1035 !strncmp(printer_state->values[0].string.text, "spool-area-full-",
1036 16))
1037 {
1038 busy = 1;
1039 break;
1040 }
a469f8a5 1041 }
eac3a0a0 1042 }
eac3a0a0
MS
1043
1044 if (busy)
22c9029b 1045 {
f3c17241 1046 _cupsLangPrintFilter(stderr, "INFO", _("The printer is in use."));
22c9029b 1047
321d8d57 1048 report_printer_state(supported);
22c9029b 1049
7e86f2f6 1050 sleep((unsigned)delay);
22c9029b 1051
eac3a0a0 1052 delay = _cupsNextDelay(delay, &prev_delay);
22c9029b
MS
1053
1054 ippDelete(supported);
eb383c31
MS
1055 supported = NULL;
1056 ipp_status = IPP_STATUS_ERROR_BUSY;
22c9029b
MS
1057 continue;
1058 }
1059 }
22c9029b 1060
cc754834
MS
1061 /*
1062 * Check for supported attributes...
1063 */
1064
a469f8a5
MS
1065#ifdef HAVE_LIBZ
1066 if ((compression_sup = ippFindAttribute(supported, "compression-supported",
1067 IPP_TAG_KEYWORD)) != NULL)
1068 {
1069 /*
1070 * Check whether the requested compression is supported and/or default to
1071 * compression if supported...
1072 */
1073
1074 if (compression && !ippContainsString(compression_sup, compression))
1075 {
1076 fprintf(stderr, "DEBUG: Printer does not support the requested "
1077 "compression value \"%s\".\n", compression);
1078 compression = NULL;
1079 }
1080 else if (!compression)
1081 {
0fa6c7fa 1082 if (ippContainsString(compression_sup, "gzip"))
a469f8a5 1083 compression = "gzip";
0fa6c7fa
MS
1084 else if (ippContainsString(compression_sup, "deflate"))
1085 compression = "deflate";
a469f8a5
MS
1086
1087 if (compression)
1088 fprintf(stderr, "DEBUG: Automatically using \"%s\" compression.\n",
1089 compression);
1090 }
1091 }
1092#endif /* HAVE_LIBZ */
1093
cc754834
MS
1094 if ((copies_sup = ippFindAttribute(supported, "copies-supported",
1095 IPP_TAG_RANGE)) != NULL)
ef416fc2 1096 {
1097 /*
1098 * Has the "copies-supported" attribute - does it have an upper
1099 * bound > 1?
1100 */
1101
cc754834
MS
1102 fprintf(stderr, "DEBUG: copies-supported=%d-%d\n",
1103 copies_sup->values[0].range.lower,
1104 copies_sup->values[0].range.upper);
1105
ef416fc2 1106 if (copies_sup->values[0].range.upper <= 1)
1107 copies_sup = NULL; /* No */
1108 }
1109
cc754834 1110 cups_version = ippFindAttribute(supported, "cups-version", IPP_TAG_TEXT);
ef416fc2 1111
cc754834
MS
1112 if ((format_sup = ippFindAttribute(supported, "document-format-supported",
1113 IPP_TAG_MIMETYPE)) != NULL)
ef416fc2 1114 {
1115 fprintf(stderr, "DEBUG: document-format-supported (%d values)\n",
1116 format_sup->num_values);
1117 for (i = 0; i < format_sup->num_values; i ++)
1118 fprintf(stderr, "DEBUG: [%d] = \"%s\"\n", i,
1119 format_sup->values[i].string.text);
1120 }
1121
cc754834
MS
1122 if ((media_col_sup = ippFindAttribute(supported, "media-col-supported",
1123 IPP_TAG_KEYWORD)) != NULL)
1124 {
1125 fprintf(stderr, "DEBUG: media-col-supported (%d values)\n",
1126 media_col_sup->num_values);
1127 for (i = 0; i < media_col_sup->num_values; i ++)
1128 fprintf(stderr, "DEBUG: [%d] = \"%s\"\n", i,
1129 media_col_sup->values[i].string.text);
1130 }
1131
f2a7bf2a 1132 print_color_mode_sup = ippFindAttribute(supported, "print-color-mode-supported", IPP_TAG_KEYWORD);
a469f8a5 1133
0268488e
MS
1134 if ((operations_sup = ippFindAttribute(supported, "operations-supported",
1135 IPP_TAG_ENUM)) != NULL)
1136 {
a469f8a5
MS
1137 fprintf(stderr, "DEBUG: operations-supported (%d values)\n",
1138 operations_sup->num_values);
1139 for (i = 0; i < operations_sup->num_values; i ++)
1140 fprintf(stderr, "DEBUG: [%d] = %s\n", i,
1141 ippOpString(operations_sup->values[i].integer));
1142
eac3a0a0
MS
1143 for (i = 0; i < operations_sup->num_values; i ++)
1144 if (operations_sup->values[i].integer == IPP_PRINT_JOB)
1145 break;
1146
1147 if (i >= operations_sup->num_values)
1148 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1149 "cups-ipp-missing-print-job");
1150
1151 for (i = 0; i < operations_sup->num_values; i ++)
1152 if (operations_sup->values[i].integer == IPP_CANCEL_JOB)
1153 break;
1154
1155 if (i >= operations_sup->num_values)
1156 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1157 "cups-ipp-missing-cancel-job");
1158
1159 for (i = 0; i < operations_sup->num_values; i ++)
1160 if (operations_sup->values[i].integer == IPP_GET_JOB_ATTRIBUTES)
1161 break;
1162
1163 if (i >= operations_sup->num_values)
1164 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1165 "cups-ipp-missing-get-job-attributes");
1166
1167 for (i = 0; i < operations_sup->num_values; i ++)
1168 if (operations_sup->values[i].integer == IPP_GET_PRINTER_ATTRIBUTES)
1169 break;
1170
1171 if (i >= operations_sup->num_values)
1172 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1173 "cups-ipp-missing-get-printer-attributes");
1174
0268488e 1175 for (i = 0; i < operations_sup->num_values; i ++)
dcb445bc 1176 {
0268488e 1177 if (operations_sup->values[i].integer == IPP_VALIDATE_JOB)
0268488e 1178 validate_job = 1;
dcb445bc
MS
1179 else if (operations_sup->values[i].integer == IPP_CREATE_JOB)
1180 create_job = 1;
1181 else if (operations_sup->values[i].integer == IPP_SEND_DOCUMENT)
1182 send_document = 1;
a29fd7dd
MS
1183 else if (operations_sup->values[i].integer == IPP_GET_JOB_ATTRIBUTES)
1184 get_job_attrs = 1;
dcb445bc
MS
1185 }
1186
37e7e6e0 1187 if (create_job && !send_document)
dcb445bc
MS
1188 {
1189 fputs("DEBUG: Printer supports Create-Job but not Send-Document.\n",
1190 stderr);
1191 create_job = 0;
a29fd7dd
MS
1192
1193 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1194 "cups-ipp-missing-send-document");
dcb445bc 1195 }
0268488e
MS
1196
1197 if (!validate_job)
eac3a0a0
MS
1198 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1199 "cups-ipp-missing-validate-job");
0268488e
MS
1200 }
1201 else
eac3a0a0
MS
1202 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1203 "cups-ipp-missing-operations-supported");
0268488e 1204
84315f46
MS
1205 doc_handling_sup = ippFindAttribute(supported,
1206 "multiple-document-handling-supported",
1207 IPP_TAG_KEYWORD);
1208
321d8d57 1209 report_printer_state(supported);
ef416fc2 1210 }
f3c17241
MS
1211 while (!job_canceled && ipp_status > IPP_OK_CONFLICT);
1212
1213 if (job_canceled)
1214 return (CUPS_BACKEND_OK);
ef416fc2 1215
1216 /*
1217 * See if the printer is accepting jobs and is not stopped; if either
1218 * condition is true and we are printing to a class, requeue the job...
1219 */
1220
1221 if (getenv("CLASS") != NULL)
1222 {
1223 printer_state = ippFindAttribute(supported, "printer-state",
1224 IPP_TAG_ENUM);
1225 printer_accepting = ippFindAttribute(supported, "printer-is-accepting-jobs",
1226 IPP_TAG_BOOLEAN);
1227
1228 if (printer_state == NULL ||
bd7854cb 1229 (printer_state->values[0].integer > IPP_PRINTER_PROCESSING &&
1230 waitprinter) ||
ef416fc2 1231 printer_accepting == NULL ||
1232 !printer_accepting->values[0].boolean)
1233 {
1234 /*
1235 * If the CLASS environment variable is set, the job was submitted
1236 * to a class and not to a specific queue. In this case, we want
1237 * to abort immediately so that the job can be requeued on the next
1238 * available printer in the class.
1239 */
1240
0837b7e8
MS
1241 _cupsLangPrintFilter(stderr, "INFO",
1242 _("Unable to contact printer, queuing on next "
1243 "printer in class."));
ef416fc2 1244
1245 ippDelete(supported);
1246 httpClose(http);
1247
ef416fc2 1248 /*
1249 * Sleep 5 seconds to keep the job from requeuing too rapidly...
1250 */
1251
1252 sleep(5);
1253
1254 return (CUPS_BACKEND_FAILED);
1255 }
1256 }
1257
1258 /*
1259 * See if the printer supports multiple copies...
1260 */
1261
d09495fa 1262 copies = atoi(argv[4]);
1263
ef416fc2 1264 if (copies_sup || argc < 7)
d09495fa 1265 copies_remaining = 1;
ef416fc2 1266 else
d09495fa 1267 copies_remaining = copies;
ef416fc2 1268
0268488e
MS
1269 /*
1270 * Prepare remaining printing options...
1271 */
1272
1273 options = NULL;
0268488e
MS
1274
1275 if (send_options)
1276 {
1277 num_options = cupsParseOptions(argv[5], 0, &options);
1278
1279 if (!cups_version && media_col_sup)
1280 {
1281 /*
1282 * Load the PPD file and generate PWG attribute mapping information...
1283 */
1284
1285 ppd = ppdOpenFile(getenv("PPD"));
f14324a7 1286 pc = _ppdCacheCreateWithPPD(ppd);
0268488e 1287
a469f8a5
MS
1288 ppdMarkDefaults(ppd);
1289 cupsMarkOptions(ppd, num_options, options);
0268488e
MS
1290 }
1291 }
1292 else
1293 num_options = 0;
1294
1295 document_format = NULL;
1296
1297 if (format_sup != NULL)
1298 {
1299 for (i = 0; i < format_sup->num_values; i ++)
82cc1f9a
MS
1300 if (!_cups_strcasecmp(final_content_type,
1301 format_sup->values[i].string.text))
0268488e
MS
1302 {
1303 document_format = final_content_type;
1304 break;
1305 }
c8fef167 1306
3e7fe0ca 1307 if (!document_format)
c8fef167
MS
1308 {
1309 for (i = 0; i < format_sup->num_values; i ++)
88f9aafc 1310 if (!_cups_strcasecmp("application/octet-stream",
82cc1f9a 1311 format_sup->values[i].string.text))
c8fef167
MS
1312 {
1313 document_format = "application/octet-stream";
1314 break;
1315 }
1316 }
0268488e
MS
1317 }
1318
82cc1f9a
MS
1319 fprintf(stderr, "DEBUG: final_content_type=\"%s\", document_format=\"%s\"\n",
1320 final_content_type, document_format ? document_format : "(null)");
1321
82f97232
MS
1322 /*
1323 * If the printer does not support HTTP/1.1 (which IPP requires), copy stdin
1324 * to a temporary file so that we can do a HTTP/1.0 submission...
1325 *
1326 * (I hate compatibility hacks!)
1327 */
1328
1329 if (http->version < HTTP_1_1 && num_files == 0)
1330 {
1331 if ((fd = cupsTempFd(tmpfilename, sizeof(tmpfilename))) < 0)
1332 {
1333 perror("DEBUG: Unable to create temporary file");
1334 return (CUPS_BACKEND_FAILED);
1335 }
1336
1337 _cupsLangPrintFilter(stderr, "INFO", _("Copying print data."));
1338
7e86f2f6 1339 if ((compatsize = write(fd, buffer, (size_t)bytes)) < 0)
12f89d24
MS
1340 {
1341 perror("DEBUG: Unable to write temporary file");
1342 return (CUPS_BACKEND_FAILED);
1343 }
1344
1345 if ((bytes = backendRunLoop(-1, fd, snmp_fd, &(addrlist->addr), 0, 0,
1346 backendNetworkSideCB)) < 0)
1347 return (CUPS_BACKEND_FAILED);
1348
1349 compatsize += bytes;
82f97232
MS
1350
1351 close(fd);
1352
1353 compatfile = tmpfilename;
1354 files = &compatfile;
1355 num_files = 1;
1356 }
1357 else if (http->version < HTTP_1_1 && num_files == 1)
1358 {
1359 struct stat fileinfo; /* File information */
1360
1361 if (!stat(files[0], &fileinfo))
1362 compatsize = fileinfo.st_size;
1363 }
1364
5a9febac
MS
1365 /*
1366 * If the printer only claims to support IPP/1.0, or if the user specifically
1367 * included version=1.0 in the URI, then do not try to use Create-Job or
0fa6c7fa
MS
1368 * Send-Document. This is another dreaded compatibility hack, but
1369 * unfortunately there are enough broken printers out there that we need
1370 * this for now...
5a9febac
MS
1371 */
1372
1373 if (version == 10)
1374 create_job = send_document = 0;
1375
7cf5915e
MS
1376 /*
1377 * Start monitoring the printer in the background...
1378 */
1379
1380 monitor.uri = uri;
1381 monitor.hostname = hostname;
1382 monitor.user = argv[2];
1383 monitor.resource = resource;
1384 monitor.port = port;
1385 monitor.version = version;
1386 monitor.job_id = 0;
a29fd7dd 1387 monitor.get_job_attrs = get_job_attrs;
7cf5915e
MS
1388 monitor.encryption = cupsEncryption();
1389 monitor.job_state = IPP_JOB_PENDING;
1390 monitor.printer_state = IPP_PRINTER_IDLE;
1391
dcb445bc
MS
1392 if (create_job)
1393 {
1394 monitor.job_name = argv[3];
1395 }
1396 else
1397 {
1398 snprintf(print_job_name, sizeof(print_job_name), "%s - %s", argv[1],
1399 argv[3]);
1400 monitor.job_name = print_job_name;
1401 }
1402
7cf5915e
MS
1403 _cupsThreadCreate((_cups_thread_func_t)monitor_printer, &monitor);
1404
ef416fc2 1405 /*
0268488e 1406 * Validate access to the printer...
ef416fc2 1407 */
1408
22c9029b 1409 while (!job_canceled && validate_job)
ef416fc2 1410 {
dcb445bc
MS
1411 request = new_request(IPP_VALIDATE_JOB, version, uri, argv[2],
1412 monitor.job_name, num_options, options, compression,
a469f8a5 1413 copies_sup ? copies : 1, document_format, pc, ppd,
f2a7bf2a 1414 media_col_sup, doc_handling_sup, print_color_mode_sup);
b423cd4c 1415
c5b24bfa 1416 response = cupsDoRequest(http, request, resource);
ef416fc2 1417
0268488e 1418 ipp_status = cupsLastError();
ef416fc2 1419
22c9029b
MS
1420 fprintf(stderr, "DEBUG: Validate-Job: %s (%s)\n",
1421 ippErrorString(ipp_status), cupsLastErrorString());
cc754834 1422
c5b24bfa
MS
1423 if ((job_auth = ippFindAttribute(response, "job-authorization-uri",
1424 IPP_TAG_URI)) != NULL)
1425 num_options = cupsAddOption("job-authorization-uri",
1426 ippGetString(job_auth, 0, NULL), num_options,
1427 &options);
1428
1429 ippDelete(response);
1430
22c9029b
MS
1431 if (job_canceled)
1432 break;
ef416fc2 1433
a469f8a5
MS
1434 if (ipp_status == IPP_STATUS_ERROR_SERVICE_UNAVAILABLE ||
1435 ipp_status == IPP_STATUS_ERROR_BUSY)
22c9029b 1436 {
f3c17241 1437 _cupsLangPrintFilter(stderr, "INFO", _("The printer is in use."));
22c9029b
MS
1438 sleep(10);
1439 }
a469f8a5 1440 else if (ipp_status == IPP_STATUS_ERROR_DOCUMENT_FORMAT_NOT_SUPPORTED ||
651e0a22 1441 ipp_status == IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES ||
a469f8a5
MS
1442 ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_INFO_NEEDED ||
1443 ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_CLOSED ||
1444 ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_LIMIT_REACHED ||
1445 ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_AUTHORIZATION_FAILED)
a29fd7dd 1446 goto cleanup;
a469f8a5
MS
1447 else if (ipp_status == IPP_STATUS_ERROR_FORBIDDEN ||
1448 ipp_status == IPP_STATUS_ERROR_CUPS_AUTHENTICATION_CANCELED)
22c9029b 1449 {
12f89d24
MS
1450 const char *www_auth = httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE);
1451 /* WWW-Authenticate field value */
cc754834 1452
12f89d24 1453 if (!strncmp(www_auth, "Negotiate", 9))
22c9029b 1454 auth_info_required = "negotiate";
12f89d24
MS
1455 else if (www_auth[0])
1456 auth_info_required = "username,password";
ef416fc2 1457
22c9029b 1458 goto cleanup;
0268488e 1459 }
a469f8a5 1460 else if (ipp_status == IPP_STATUS_ERROR_OPERATION_NOT_SUPPORTED)
22c9029b 1461 {
eac3a0a0
MS
1462 /*
1463 * This is all too common...
1464 */
1465
1466 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1467 "cups-ipp-missing-validate-job");
22c9029b
MS
1468 break;
1469 }
a29fd7dd
MS
1470 else if (ipp_status < IPP_REDIRECTION_OTHER_SITE ||
1471 ipp_status == IPP_BAD_REQUEST)
0268488e
MS
1472 break;
1473 }
cc754834 1474
0268488e
MS
1475 /*
1476 * Then issue the print-job request...
1477 */
cc754834 1478
0268488e 1479 job_id = 0;
7cf5915e 1480
0268488e
MS
1481 while (!job_canceled && copies_remaining > 0)
1482 {
1483 /*
1484 * Check for side-channel requests...
1485 */
cc754834 1486
0268488e 1487 backendCheckSideChannel(snmp_fd, http->hostaddr);
7cf5915e 1488
0268488e
MS
1489 /*
1490 * Build the IPP job creation request...
1491 */
d09495fa 1492
0268488e
MS
1493 if (job_canceled)
1494 break;
ef416fc2 1495
dcb445bc
MS
1496 request = new_request((num_files > 1 || create_job) ? IPP_CREATE_JOB :
1497 IPP_PRINT_JOB,
1498 version, uri, argv[2], monitor.job_name, num_options,
1499 options, compression, copies_sup ? copies : 1,
a469f8a5 1500 document_format, pc, ppd, media_col_sup,
f2a7bf2a 1501 doc_handling_sup, print_color_mode_sup);
ef416fc2 1502
ef416fc2 1503 /*
1504 * Do the request...
1505 */
1506
dcb445bc 1507 if (num_files > 1 || create_job)
bd7854cb 1508 response = cupsDoRequest(http, request, resource);
ef416fc2 1509 else
cc754834 1510 {
82f97232
MS
1511 size_t length = 0; /* Length of request */
1512
1513 if (compatsize > 0)
1514 {
1515 fputs("DEBUG: Sending file using HTTP/1.0 Content-Length...\n", stderr);
1516 length = ippLength(request) + (size_t)compatsize;
1517 }
1518 else
1519 fputs("DEBUG: Sending file using HTTP/1.1 chunking...\n", stderr);
1520
1521 http_status = cupsSendRequest(http, request, resource, length);
cc754834
MS
1522 if (http_status == HTTP_CONTINUE && request->state == IPP_DATA)
1523 {
c41769ff
MS
1524 if (compression && strcmp(compression, "none"))
1525 httpSetField(http, HTTP_FIELD_CONTENT_ENCODING, compression);
1526
cc754834 1527 if (num_files == 1)
dcb445bc
MS
1528 {
1529 if ((fd = open(files[0], O_RDONLY)) < 0)
1530 {
1531 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1532 return (CUPS_BACKEND_FAILED);
1533 }
1534 }
cc754834 1535 else
eac3a0a0
MS
1536 {
1537 fd = 0;
7e86f2f6 1538 http_status = cupsWriteRequestData(http, buffer, (size_t)bytes);
eac3a0a0 1539 }
cc754834 1540
83e08001
MS
1541 while (http_status == HTTP_CONTINUE &&
1542 (!job_canceled || compatsize > 0))
cc754834 1543 {
eac3a0a0
MS
1544 /*
1545 * Check for side-channel requests and more print data...
1546 */
cc754834 1547
eac3a0a0
MS
1548 FD_ZERO(&input);
1549 FD_SET(fd, &input);
1550 FD_SET(snmp_fd, &input);
cc754834 1551
eac3a0a0
MS
1552 while (select(fd > snmp_fd ? fd + 1 : snmp_fd + 1, &input, NULL, NULL,
1553 NULL) <= 0 && !job_canceled);
1554
1555 if (FD_ISSET(snmp_fd, &input))
cc754834 1556 backendCheckSideChannel(snmp_fd, http->hostaddr);
eac3a0a0
MS
1557
1558 if (FD_ISSET(fd, &input))
1559 {
1560 if ((bytes = read(fd, buffer, sizeof(buffer))) > 0)
1561 {
1562 fprintf(stderr, "DEBUG: Read %d bytes...\n", (int)bytes);
1563
7e86f2f6 1564 if ((http_status = cupsWriteRequestData(http, buffer, (size_t)bytes))
c41769ff 1565 != HTTP_CONTINUE)
eac3a0a0
MS
1566 break;
1567 }
1568 else if (bytes == 0 || (errno != EINTR && errno != EAGAIN))
1569 break;
cc754834
MS
1570 }
1571 }
1572
c41769ff
MS
1573 if (http_status == HTTP_ERROR)
1574 fprintf(stderr, "DEBUG: Error writing document data for "
1575 "Print-Job: %s\n", strerror(httpError(http)));
1576
cc754834
MS
1577 if (num_files == 1)
1578 close(fd);
1579 }
1580
1581 response = cupsGetResponse(http, resource);
1582 ippDelete(request);
1583 }
bd7854cb 1584
1585 ipp_status = cupsLastError();
ef416fc2 1586
22c9029b 1587 fprintf(stderr, "DEBUG: %s: %s (%s)\n",
dcb445bc 1588 (num_files > 1 || create_job) ? "Create-Job" : "Print-Job",
22c9029b
MS
1589 ippErrorString(ipp_status), cupsLastErrorString());
1590
ef416fc2 1591 if (ipp_status > IPP_OK_CONFLICT)
1592 {
1593 job_id = 0;
1594
cc754834 1595 if (job_canceled)
bd7854cb 1596 break;
1597
a469f8a5
MS
1598 if (ipp_status == IPP_STATUS_ERROR_SERVICE_UNAVAILABLE ||
1599 ipp_status == IPP_STATUS_ERROR_NOT_POSSIBLE ||
1600 ipp_status == IPP_STATUS_ERROR_BUSY)
ef416fc2 1601 {
f3c17241 1602 _cupsLangPrintFilter(stderr, "INFO", _("The printer is in use."));
ef416fc2 1603 sleep(10);
22c9029b
MS
1604
1605 if (num_files == 0)
1606 {
1607 /*
1608 * We can't re-submit when we have no files to print, so exit
1609 * immediately with the right status code...
1610 */
1611
1612 goto cleanup;
1613 }
ef416fc2 1614 }
a469f8a5
MS
1615 else if (ipp_status == IPP_STATUS_ERROR_JOB_CANCELED ||
1616 ipp_status == IPP_STATUS_ERROR_NOT_AUTHORIZED ||
6961465f 1617 ipp_status == IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES ||
a469f8a5
MS
1618 ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_INFO_NEEDED ||
1619 ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_CLOSED ||
1620 ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_LIMIT_REACHED ||
1621 ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_AUTHORIZATION_FAILED)
771bd8cb 1622 goto cleanup;
ef416fc2 1623 else
68b10830
MS
1624 {
1625 /*
1626 * Update auth-info-required as needed...
1627 */
1628
0837b7e8 1629 _cupsLangPrintFilter(stderr, "ERROR",
a469f8a5 1630 _("Print job was not accepted."));
68b10830 1631
a469f8a5
MS
1632 if (ipp_status == IPP_STATUS_ERROR_FORBIDDEN ||
1633 ipp_status == IPP_STATUS_ERROR_CUPS_AUTHENTICATION_CANCELED)
68b10830 1634 {
12f89d24
MS
1635 const char *www_auth = httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE);
1636 /* WWW-Authenticate field value */
3e7fe0ca 1637
12f89d24
MS
1638 if (!strncmp(www_auth, "Negotiate", 9))
1639 auth_info_required = "negotiate";
1640 else if (www_auth[0])
1641 auth_info_required = "username,password";
1642 }
1643 else if (ipp_status == IPP_REQUEST_VALUE)
1644 {
1645 /*
1646 * Print file is too large, abort this job...
18ecb428
MS
1647 */
1648
12f89d24 1649 goto cleanup;
68b10830 1650 }
22c9029b
MS
1651 else
1652 sleep(10);
1653
1654 if (num_files == 0)
1655 {
1656 /*
1657 * We can't re-submit when we have no files to print, so exit
1658 * immediately with the right status code...
1659 */
1660
1661 goto cleanup;
1662 }
68b10830 1663 }
ef416fc2 1664 }
1665 else if ((job_id_attr = ippFindAttribute(response, "job-id",
1666 IPP_TAG_INTEGER)) == NULL)
1667 {
a469f8a5 1668 fputs("DEBUG: Print job accepted - job ID unknown.\n", stderr);
eac3a0a0
MS
1669 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1670 "cups-ipp-missing-job-id");
ef416fc2 1671 job_id = 0;
1672 }
1673 else
1674 {
3e7fe0ca 1675 password_tries = 0;
7cf5915e 1676 monitor.job_id = job_id = job_id_attr->values[0].integer;
a469f8a5 1677 fprintf(stderr, "DEBUG: Print job accepted - job ID %d.\n", job_id);
ef416fc2 1678 }
1679
bd7854cb 1680 ippDelete(response);
1681
cc754834 1682 if (job_canceled)
bd7854cb 1683 break;
1684
dcb445bc 1685 if (job_id && (num_files > 1 || create_job))
bd7854cb 1686 {
dcb445bc 1687 for (i = 0; num_files == 0 || i < num_files; i ++)
bd7854cb 1688 {
7a14d768
MS
1689 /*
1690 * Check for side-channel requests...
1691 */
1692
1693 backendCheckSideChannel(snmp_fd, http->hostaddr);
1694
1695 /*
1696 * Send the next file in the job...
1697 */
1698
bd7854cb 1699 request = ippNewRequest(IPP_SEND_DOCUMENT);
7e86f2f6 1700 ippSetVersion(request, version / 10, version % 10);
bd7854cb 1701
1702 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1703 NULL, uri);
1704
1705 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
1706 job_id);
1707
1708 if (argv[2][0])
1709 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1710 "requesting-user-name", NULL, argv[2]);
1711
ee8618f3
MS
1712 ippAddBoolean(request, IPP_TAG_OPERATION, "last-document",
1713 (i + 1) >= num_files);
bd7854cb 1714
82cc1f9a
MS
1715 if (document_format)
1716 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE,
1717 "document-format", NULL, document_format);
bd7854cb 1718
5a9febac
MS
1719 if (compression)
1720 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1721 "compression", NULL, compression);
1722
cc754834
MS
1723 fprintf(stderr, "DEBUG: Sending file %d using chunking...\n", i + 1);
1724 http_status = cupsSendRequest(http, request, resource, 0);
dcb445bc
MS
1725 if (http_status == HTTP_CONTINUE && request->state == IPP_DATA)
1726 {
a469f8a5
MS
1727 if (compression && strcmp(compression, "none"))
1728 httpSetField(http, HTTP_FIELD_CONTENT_ENCODING, compression);
1729
dcb445bc
MS
1730 if (num_files == 0)
1731 {
1732 fd = 0;
7e86f2f6 1733 http_status = cupsWriteRequestData(http, buffer, (size_t)bytes);
dcb445bc
MS
1734 }
1735 else
1736 {
1737 if ((fd = open(files[i], O_RDONLY)) < 0)
1738 {
1739 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1740 return (CUPS_BACKEND_FAILED);
1741 }
1742 }
1743 }
1744 else
1745 fd = -1;
1746
1747 if (fd >= 0)
cc754834 1748 {
82cc1f9a 1749 while (!job_canceled && http_status == HTTP_CONTINUE &&
83e08001 1750 (bytes = read(fd, buffer, sizeof(buffer))) > 0)
cc754834 1751 {
7e86f2f6 1752 if ((http_status = cupsWriteRequestData(http, buffer, (size_t)bytes))
82cc1f9a 1753 != HTTP_CONTINUE)
cc754834
MS
1754 break;
1755 else
1756 {
1757 /*
1758 * Check for side-channel requests...
1759 */
1760
1761 backendCheckSideChannel(snmp_fd, http->hostaddr);
1762 }
1763 }
1764
dcb445bc
MS
1765 if (fd > 0)
1766 close(fd);
cc754834 1767 }
97c9a8d7 1768
c41769ff
MS
1769 if (http_status == HTTP_ERROR)
1770 fprintf(stderr, "DEBUG: Error writing document data for "
1771 "Send-Document: %s\n", strerror(httpError(http)));
1772
cc754834
MS
1773 ippDelete(cupsGetResponse(http, resource));
1774 ippDelete(request);
bd7854cb 1775
22c9029b
MS
1776 fprintf(stderr, "DEBUG: Send-Document: %s (%s)\n",
1777 ippErrorString(cupsLastError()), cupsLastErrorString());
1778
bd7854cb 1779 if (cupsLastError() > IPP_OK_CONFLICT)
1780 {
1781 ipp_status = cupsLastError();
1782
0837b7e8 1783 _cupsLangPrintFilter(stderr, "ERROR",
22c9029b 1784 _("Unable to add document to print job."));
bd7854cb 1785 break;
1786 }
3e7fe0ca
MS
1787 else
1788 {
1789 password_tries = 0;
1790
1791 if (num_files == 0 || fd < 0)
1792 break;
1793 }
bd7854cb 1794 }
1795 }
ef416fc2 1796
1797 if (ipp_status <= IPP_OK_CONFLICT && argc > 6)
1798 {
1799 fprintf(stderr, "PAGE: 1 %d\n", copies_sup ? atoi(argv[4]) : 1);
d09495fa 1800 copies_remaining --;
ef416fc2 1801 }
8e67b75b
MS
1802 else if ((ipp_status == IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR || ipp_status == IPP_STATUS_ERROR_DOCUMENT_UNPRINTABLE) &&
1803 argc == 6 &&
1804 document_format && strcmp(document_format, "image/pwg-raster") && strcmp(document_format, "image/urf"))
1805 {
1806 /*
1807 * Need to reprocess the job as raster...
1808 */
1809
1810 fputs("JOBSTATE: cups-retry-as-raster\n", stderr);
1811 if (job_id > 0)
1812 cancel_job(http, uri, job_id, resource, argv[2], version);
1813
1814 goto cleanup;
1815 }
ef416fc2 1816 else if (ipp_status == IPP_SERVICE_UNAVAILABLE ||
a2326b5b 1817 ipp_status == IPP_NOT_POSSIBLE ||
ef416fc2 1818 ipp_status == IPP_PRINTER_BUSY)
a469f8a5
MS
1819 {
1820 if (argc == 6)
1821 {
1822 /*
1823 * Need to reprocess the entire job; if we have a job ID, cancel the
1824 * job first...
1825 */
1826
1827 if (job_id > 0)
1828 cancel_job(http, uri, job_id, resource, argv[2], version);
1829
1830 goto cleanup;
1831 }
1f6f3dbc 1832 continue;
a469f8a5 1833 }
5a9febac
MS
1834 else if (ipp_status == IPP_REQUEST_VALUE ||
1835 ipp_status == IPP_ERROR_JOB_CANCELED ||
1836 ipp_status == IPP_NOT_AUTHORIZED ||
a469f8a5
MS
1837 ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_INFO_NEEDED ||
1838 ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_CLOSED ||
1839 ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_LIMIT_REACHED ||
1840 ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_AUTHORIZATION_FAILED ||
5a9febac
MS
1841 ipp_status == IPP_INTERNAL_ERROR)
1842 {
1843 /*
1844 * Print file is too large, job was canceled, we need new
1845 * authentication data, or we had some sort of error...
1846 */
1847
1848 goto cleanup;
1849 }
0fa6c7fa
MS
1850 else if (ipp_status == IPP_STATUS_ERROR_CUPS_UPGRADE_REQUIRED)
1851 {
1852 /*
1853 * Server is configured incorrectly; the policy for Create-Job and
1854 * Send-Document has to be the same (auth or no auth, encryption or
1855 * no encryption). Force the queue to stop since printing will never
1856 * work.
1857 */
1858
1859 fputs("DEBUG: The server or printer is configured incorrectly.\n",
1860 stderr);
1861 fputs("DEBUG: The policy for Create-Job and Send-Document must have the "
1862 "same authentication and encryption requirements.\n", stderr);
1863
1864 ipp_status = IPP_STATUS_ERROR_INTERNAL;
1865
1866 if (job_id > 0)
1867 cancel_job(http, uri, job_id, resource, argv[2], version);
1868
1869 goto cleanup;
1870 }
5a9febac 1871 else if (ipp_status == IPP_NOT_FOUND)
12f89d24
MS
1872 {
1873 /*
5a9febac
MS
1874 * Printer does not actually implement support for Create-Job/
1875 * Send-Document, so log the conformance issue and stop the printer.
12f89d24
MS
1876 */
1877
5a9febac
MS
1878 fputs("DEBUG: This printer claims to support Create-Job and "
1879 "Send-Document, but those operations failed.\n", stderr);
1880 fputs("DEBUG: Add '?version=1.0' to the device URI to use legacy "
1881 "compatibility mode.\n", stderr);
1882 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1883 "cups-ipp-missing-send-document");
1884
1885 ipp_status = IPP_INTERNAL_ERROR; /* Force queue to stop */
1886
12f89d24
MS
1887 goto cleanup;
1888 }
ef416fc2 1889 else
d09495fa 1890 copies_remaining --;
ef416fc2 1891
1892 /*
1893 * Wait for the job to complete...
1894 */
1895
a29fd7dd 1896 if (!job_id || !waitjob || !get_job_attrs)
ef416fc2 1897 continue;
1898
1e3e80bb 1899 fputs("STATE: +cups-waiting-for-job-completed\n", stderr);
581dae2d 1900
0837b7e8 1901 _cupsLangPrintFilter(stderr, "INFO", _("Waiting for job to complete."));
ef416fc2 1902
f14324a7 1903 for (delay = _cupsNextDelay(0, &prev_delay); !job_canceled;)
ef416fc2 1904 {
7a14d768
MS
1905 /*
1906 * Check for side-channel requests...
1907 */
1908
1909 backendCheckSideChannel(snmp_fd, http->hostaddr);
1910
0fa6c7fa
MS
1911 /*
1912 * Check printer state...
1913 */
1914
1915 check_printer_state(http, uri, resource, argv[2], version);
1916
6961465f
MS
1917 if (cupsLastError() <= IPP_OK_CONFLICT)
1918 password_tries = 0;
1919
ef416fc2 1920 /*
1921 * Build an IPP_GET_JOB_ATTRIBUTES request...
1922 */
1923
fa73b229 1924 request = ippNewRequest(IPP_GET_JOB_ATTRIBUTES);
7e86f2f6 1925 ippSetVersion(request, version / 10, version % 10);
ef416fc2 1926
1927 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1928 NULL, uri);
1929
1930 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
1931 job_id);
1932
1933 if (argv[2][0])
1934 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1935 "requesting-user-name", NULL, argv[2]);
1936
1937 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1938 "requested-attributes", sizeof(jattrs) / sizeof(jattrs[0]),
1939 NULL, jattrs);
1940
1941 /*
1942 * Do the request...
1943 */
1944
f14324a7 1945 httpReconnect(http);
bd7854cb 1946 response = cupsDoRequest(http, request, resource);
1947 ipp_status = cupsLastError();
ef416fc2 1948
a29fd7dd 1949 if (ipp_status == IPP_NOT_FOUND || ipp_status == IPP_NOT_POSSIBLE)
ef416fc2 1950 {
1951 /*
1952 * Job has gone away and/or the server has no job history...
1953 */
1954
eac3a0a0
MS
1955 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1956 "cups-ipp-missing-job-history");
ef416fc2 1957 ippDelete(response);
1958
1959 ipp_status = IPP_OK;
1960 break;
1961 }
1962
22c9029b
MS
1963 fprintf(stderr, "DEBUG: Get-Job-Attributes: %s (%s)\n",
1964 ippErrorString(ipp_status), cupsLastErrorString());
1965
3e7fe0ca
MS
1966 if (ipp_status <= IPP_OK_CONFLICT)
1967 password_tries = 0;
1968 else
ef416fc2 1969 {
1970 if (ipp_status != IPP_SERVICE_UNAVAILABLE &&
1971 ipp_status != IPP_PRINTER_BUSY)
1972 {
bd7854cb 1973 ippDelete(response);
12f89d24 1974 ipp_status = IPP_OK;
ef416fc2 1975 break;
1976 }
a469f8a5
MS
1977 else if (ipp_status == IPP_INTERNAL_ERROR)
1978 {
1979 waitjob_tries ++;
1980
1981 if (waitjob_tries > 4)
1982 {
1983 ippDelete(response);
1984 ipp_status = IPP_OK;
1985 break;
1986 }
1987 }
ef416fc2 1988 }
1989
bd7854cb 1990 if (response)
ef416fc2 1991 {
1992 if ((job_state = ippFindAttribute(response, "job-state",
1993 IPP_TAG_ENUM)) != NULL)
1994 {
07ed0e9a
MS
1995 /*
1996 * Reflect the remote job state in the local queue...
1997 */
1998
eac3a0a0
MS
1999 if (cups_version &&
2000 job_state->values[0].integer >= IPP_JOB_PENDING &&
07ed0e9a 2001 job_state->values[0].integer <= IPP_JOB_COMPLETED)
eac3a0a0
MS
2002 update_reasons(NULL,
2003 remote_job_states[job_state->values[0].integer -
2004 IPP_JOB_PENDING]);
2005
2006 if ((job_sheets = ippFindAttribute(response,
2007 "job-media-sheets-completed",
2008 IPP_TAG_INTEGER)) == NULL)
2009 job_sheets = ippFindAttribute(response,
2010 "job-impressions-completed",
2011 IPP_TAG_INTEGER);
2012
2013 if (job_sheets)
2014 fprintf(stderr, "PAGE: total %d\n",
2015 job_sheets->values[0].integer);
07ed0e9a 2016
ef416fc2 2017 /*
2018 * Stop polling if the job is finished or pending-held...
2019 */
2020
b94498cf 2021 if (job_state->values[0].integer > IPP_JOB_STOPPED)
ef416fc2 2022 {
ef416fc2 2023 ippDelete(response);
2024 break;
2025 }
2026 }
a2326b5b
MS
2027 else if (ipp_status != IPP_SERVICE_UNAVAILABLE &&
2028 ipp_status != IPP_NOT_POSSIBLE &&
2029 ipp_status != IPP_PRINTER_BUSY)
7a0cbd5e
MS
2030 {
2031 /*
2032 * If the printer does not return a job-state attribute, it does not
2033 * conform to the IPP specification - break out immediately and fail
2034 * the job...
2035 */
2036
eac3a0a0
MS
2037 update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
2038 "cups-ipp-missing-job-state");
7a0cbd5e
MS
2039 ipp_status = IPP_INTERNAL_ERROR;
2040 break;
2041 }
ef416fc2 2042 }
2043
bd7854cb 2044 ippDelete(response);
ef416fc2 2045
2046 /*
f14324a7 2047 * Wait before polling again...
ef416fc2 2048 */
2049
7e86f2f6 2050 sleep((unsigned)delay);
2e4ff8af 2051
f14324a7 2052 delay = _cupsNextDelay(delay, &prev_delay);
ef416fc2 2053 }
2054 }
2055
2056 /*
bd7854cb 2057 * Cancel the job as needed...
ef416fc2 2058 */
2059
f3c17241 2060 if (job_canceled > 0 && job_id > 0)
e7dc514d 2061 {
bd7854cb 2062 cancel_job(http, uri, job_id, resource, argv[2], version);
2063
e7dc514d
MS
2064 if (cupsLastError() > IPP_OK_CONFLICT)
2065 _cupsLangPrintFilter(stderr, "ERROR", _("Unable to cancel print job."));
2066 }
2067
bd7854cb 2068 /*
2069 * Check the printer state and report it if necessary...
2070 */
ef416fc2 2071
321d8d57 2072 check_printer_state(http, uri, resource, argv[2], version);
ef416fc2 2073
6961465f
MS
2074 if (cupsLastError() <= IPP_OK_CONFLICT)
2075 password_tries = 0;
2076
7a14d768
MS
2077 /*
2078 * Collect the final page count as needed...
2079 */
2080
c8fef167 2081 if (have_supplies &&
cb7f98ee
MS
2082 !backendSNMPSupplies(snmp_fd, &(http->addrlist->addr), &page_count,
2083 NULL) &&
7a14d768
MS
2084 page_count > start_count)
2085 fprintf(stderr, "PAGE: total %d\n", page_count - start_count);
2086
18ecb428
MS
2087#ifdef HAVE_GSSAPI
2088 /*
2089 * See if we used Kerberos at all...
2090 */
2091
2092 if (http->gssctx)
2093 auth_info_required = "negotiate";
2094#endif /* HAVE_GSSAPI */
2095
ef416fc2 2096 /*
2097 * Free memory...
2098 */
2099
0268488e
MS
2100 cleanup:
2101
2102 cupsFreeOptions(num_options, options);
f14324a7 2103 _ppdCacheDestroy(pc);
a469f8a5 2104 ppdClose(ppd);
0268488e 2105
ef416fc2 2106 httpClose(http);
2107
bd7854cb 2108 ippDelete(supported);
ef416fc2 2109
2110 /*
2111 * Remove the temporary file(s) if necessary...
2112 */
2113
82f97232
MS
2114 if (tmpfilename[0])
2115 unlink(tmpfilename);
2116
ef416fc2 2117 /*
2118 * Return the queue status...
2119 */
2120
f14324a7 2121 if (ipp_status == IPP_NOT_AUTHORIZED || ipp_status == IPP_FORBIDDEN ||
82f97232 2122 ipp_status == IPP_AUTHENTICATION_CANCELED ||
f14324a7
MS
2123 ipp_status <= IPP_OK_CONFLICT)
2124 fprintf(stderr, "ATTR: auth-info-required=%s\n", auth_info_required);
18ecb428 2125
a469f8a5
MS
2126 if (ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_INFO_NEEDED)
2127 fputs("JOBSTATE: account-info-needed\n", stderr);
2128 else if (ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_CLOSED)
2129 fputs("JOBSTATE: account-closed\n", stderr);
2130 else if (ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_LIMIT_REACHED)
2131 fputs("JOBSTATE: account-limit-reached\n", stderr);
2132 else if (ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_AUTHORIZATION_FAILED)
2133 fputs("JOBSTATE: account-authorization-failed\n", stderr);
2134
82f97232 2135 if (ipp_status == IPP_NOT_AUTHORIZED || ipp_status == IPP_FORBIDDEN ||
6961465f 2136 ipp_status == IPP_AUTHENTICATION_CANCELED)
7ff4fea9 2137 return (CUPS_BACKEND_AUTH_REQUIRED);
6961465f
MS
2138 else if (ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_LIMIT_REACHED ||
2139 ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_INFO_NEEDED ||
2140 ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_CLOSED ||
2141 ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_AUTHORIZATION_FAILED)
2142 return (CUPS_BACKEND_HOLD);
7a0cbd5e
MS
2143 else if (ipp_status == IPP_INTERNAL_ERROR)
2144 return (CUPS_BACKEND_STOP);
a29fd7dd 2145 else if (ipp_status == IPP_CONFLICT)
7ff4fea9 2146 return (CUPS_BACKEND_FAILED);
a29fd7dd 2147 else if (ipp_status == IPP_REQUEST_VALUE ||
6961465f 2148 ipp_status == IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES ||
a29fd7dd 2149 ipp_status == IPP_DOCUMENT_FORMAT || job_canceled < 0)
12f89d24 2150 {
f3c17241
MS
2151 if (ipp_status == IPP_REQUEST_VALUE)
2152 _cupsLangPrintFilter(stderr, "ERROR", _("Print job too large."));
a29fd7dd
MS
2153 else if (ipp_status == IPP_DOCUMENT_FORMAT)
2154 _cupsLangPrintFilter(stderr, "ERROR",
2155 _("Printer cannot print supplied content."));
6961465f
MS
2156 else if (ipp_status == IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES)
2157 _cupsLangPrintFilter(stderr, "ERROR",
2158 _("Printer cannot print with supplied options."));
f3c17241
MS
2159 else
2160 _cupsLangPrintFilter(stderr, "ERROR", _("Print job canceled at printer."));
2161
12f89d24
MS
2162 return (CUPS_BACKEND_CANCEL);
2163 }
771bd8cb 2164 else if (ipp_status > IPP_OK_CONFLICT && ipp_status != IPP_ERROR_JOB_CANCELED)
22c9029b 2165 return (CUPS_BACKEND_RETRY_CURRENT);
7ff4fea9
MS
2166 else
2167 return (CUPS_BACKEND_OK);
ef416fc2 2168}
2169
2170
bd7854cb 2171/*
2172 * 'cancel_job()' - Cancel a print job.
2173 */
2174
2175static void
2176cancel_job(http_t *http, /* I - HTTP connection */
2177 const char *uri, /* I - printer-uri */
2178 int id, /* I - job-id */
2179 const char *resource, /* I - Resource path */
2180 const char *user, /* I - requesting-user-name */
2181 int version) /* I - IPP version */
2182{
2183 ipp_t *request; /* Cancel-Job request */
2184
2185
0837b7e8 2186 _cupsLangPrintFilter(stderr, "INFO", _("Canceling print job."));
bd7854cb 2187
2188 request = ippNewRequest(IPP_CANCEL_JOB);
7e86f2f6 2189 ippSetVersion(request, version / 10, version % 10);
bd7854cb 2190
2191 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
2192 NULL, uri);
2193 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", id);
2194
2195 if (user && user[0])
2196 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
2197 "requesting-user-name", NULL, user);
2198
2199 /*
2200 * Do the request...
2201 */
2202
2203 ippDelete(cupsDoRequest(http, request, resource));
bd7854cb 2204}
2205
2206
ef416fc2 2207/*
7cf5915e 2208 * 'check_printer_state()' - Check the printer state.
ef416fc2 2209 */
2210
7cf5915e 2211static ipp_pstate_t /* O - Current printer-state */
fa73b229 2212check_printer_state(
2213 http_t *http, /* I - HTTP connection */
2214 const char *uri, /* I - Printer URI */
2215 const char *resource, /* I - Resource path */
2216 const char *user, /* I - Username, if any */
321d8d57
MS
2217 int version) /* I - IPP version */
2218 {
7cf5915e
MS
2219 ipp_t *request, /* IPP request */
2220 *response; /* IPP response */
2221 ipp_attribute_t *attr; /* Attribute in response */
2222 ipp_pstate_t printer_state = IPP_PRINTER_STOPPED;
2223 /* Current printer-state */
ef416fc2 2224
2225
2226 /*
7cf5915e 2227 * Send a Get-Printer-Attributes request and log the results...
ef416fc2 2228 */
2229
fa73b229 2230 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
7e86f2f6 2231 ippSetVersion(request, version / 10, version % 10);
ef416fc2 2232
2233 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
7cf5915e 2234 NULL, uri);
ef416fc2 2235
2236 if (user && user[0])
2237 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
7cf5915e 2238 "requesting-user-name", NULL, user);
ef416fc2 2239
323c5de1 2240 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
7cf5915e
MS
2241 "requested-attributes",
2242 (int)(sizeof(pattrs) / sizeof(pattrs[0])), NULL, pattrs);
ef416fc2 2243
2244 if ((response = cupsDoRequest(http, request, resource)) != NULL)
2245 {
321d8d57 2246 report_printer_state(response);
7cf5915e
MS
2247
2248 if ((attr = ippFindAttribute(response, "printer-state",
2249 IPP_TAG_ENUM)) != NULL)
2250 printer_state = (ipp_pstate_t)attr->values[0].integer;
2251
ef416fc2 2252 ippDelete(response);
2253 }
7cf5915e 2254
22c9029b
MS
2255 fprintf(stderr, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
2256 ippErrorString(cupsLastError()), cupsLastErrorString());
2257
7cf5915e
MS
2258 /*
2259 * Return the printer-state value...
2260 */
2261
2262 return (printer_state);
ef416fc2 2263}
2264
2265
7cf5915e 2266/*
eac3a0a0 2267 * 'monitor_printer()' - Monitor the printer state.
7cf5915e
MS
2268 */
2269
2270static void * /* O - Thread exit code */
2271monitor_printer(
2272 _cups_monitor_t *monitor) /* I - Monitoring data */
2273{
2274 http_t *http; /* Connection to printer */
2275 ipp_t *request, /* IPP request */
2276 *response; /* IPP response */
2277 ipp_attribute_t *attr; /* Attribute in response */
2278 int delay, /* Current delay */
f14324a7 2279 prev_delay; /* Previous delay */
dcb445bc
MS
2280 ipp_op_t job_op; /* Operation to use */
2281 int job_id; /* Job ID */
2282 const char *job_name; /* Job name */
2283 ipp_jstate_t job_state; /* Job state */
2284 const char *job_user; /* Job originating user name */
6961465f 2285 int password_tries = 0; /* Password tries */
7cf5915e
MS
2286
2287
2288 /*
2289 * Make a copy of the printer connection...
2290 */
2291
db8b865d
MS
2292 http = httpConnect2(monitor->hostname, monitor->port, NULL, AF_UNSPEC,
2293 monitor->encryption, 1, 0, NULL);
f228370c 2294 httpSetTimeout(http, 30.0, timeout_cb, NULL);
3e7fe0ca
MS
2295 if (username[0])
2296 cupsSetUser(username);
6961465f
MS
2297
2298 cupsSetPasswordCB2((cups_password_cb2_t)password_cb, &password_tries);
7cf5915e
MS
2299
2300 /*
2301 * Loop until the job is canceled, aborted, or completed.
2302 */
2303
f14324a7 2304 delay = _cupsNextDelay(0, &prev_delay);
7cf5915e 2305
a469f8a5
MS
2306 monitor->job_reasons = 0;
2307
7cf5915e
MS
2308 while (monitor->job_state < IPP_JOB_CANCELED && !job_canceled)
2309 {
2310 /*
2311 * Reconnect to the printer...
2312 */
2313
2314 if (!httpReconnect(http))
2315 {
2316 /*
2317 * Connected, so check on the printer state...
2318 */
2319
2320 monitor->printer_state = check_printer_state(http, monitor->uri,
2321 monitor->resource,
2322 monitor->user,
321d8d57 2323 monitor->version);
6961465f
MS
2324 if (cupsLastError() <= IPP_OK_CONFLICT)
2325 password_tries = 0;
7cf5915e 2326
dcb445bc
MS
2327 /*
2328 * Check the status of the job itself...
2329 */
7cf5915e 2330
a29fd7dd
MS
2331 job_op = (monitor->job_id > 0 && monitor->get_job_attrs) ?
2332 IPP_GET_JOB_ATTRIBUTES : IPP_GET_JOBS;
dcb445bc 2333 request = ippNewRequest(job_op);
7e86f2f6 2334 ippSetVersion(request, monitor->version / 10, monitor->version % 10);
7cf5915e 2335
dcb445bc
MS
2336 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
2337 NULL, monitor->uri);
2338 if (job_op == IPP_GET_JOB_ATTRIBUTES)
7cf5915e 2339 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
dcb445bc 2340 monitor->job_id);
7cf5915e 2341
dcb445bc
MS
2342 if (monitor->user && monitor->user[0])
2343 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
2344 "requesting-user-name", NULL, monitor->user);
7cf5915e 2345
dcb445bc
MS
2346 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
2347 "requested-attributes",
2348 (int)(sizeof(jattrs) / sizeof(jattrs[0])), NULL, jattrs);
7cf5915e 2349
dcb445bc
MS
2350 /*
2351 * Do the request...
2352 */
7cf5915e 2353
dcb445bc 2354 response = cupsDoRequest(http, request, monitor->resource);
7cf5915e 2355
f3c17241 2356 fprintf(stderr, "DEBUG: (monitor) %s: %s (%s)\n", ippOpString(job_op),
dcb445bc 2357 ippErrorString(cupsLastError()), cupsLastErrorString());
22c9029b 2358
3e7fe0ca
MS
2359 if (cupsLastError() <= IPP_OK_CONFLICT)
2360 password_tries = 0;
2361
dcb445bc
MS
2362 if (job_op == IPP_GET_JOB_ATTRIBUTES)
2363 {
7cf5915e
MS
2364 if ((attr = ippFindAttribute(response, "job-state",
2365 IPP_TAG_ENUM)) != NULL)
2366 monitor->job_state = (ipp_jstate_t)attr->values[0].integer;
2367 else
2368 monitor->job_state = IPP_JOB_COMPLETED;
dcb445bc 2369 }
12f89d24 2370 else if (response)
dcb445bc
MS
2371 {
2372 for (attr = response->attrs; attr; attr = attr->next)
2373 {
2374 job_id = 0;
2375 job_name = NULL;
2376 job_state = IPP_JOB_PENDING;
2377 job_user = NULL;
2378
2379 while (attr && attr->group_tag != IPP_TAG_JOB)
2380 attr = attr->next;
7cf5915e 2381
dcb445bc
MS
2382 if (!attr)
2383 break;
2384
2385 while (attr && attr->group_tag == IPP_TAG_JOB)
2386 {
2387 if (!strcmp(attr->name, "job-id") &&
2388 attr->value_tag == IPP_TAG_INTEGER)
2389 job_id = attr->values[0].integer;
2390 else if (!strcmp(attr->name, "job-name") &&
2391 (attr->value_tag == IPP_TAG_NAME ||
2392 attr->value_tag == IPP_TAG_NAMELANG))
2393 job_name = attr->values[0].string.text;
2394 else if (!strcmp(attr->name, "job-state") &&
2395 attr->value_tag == IPP_TAG_ENUM)
7e86f2f6 2396 job_state = (ipp_jstate_t)attr->values[0].integer;
dcb445bc
MS
2397 else if (!strcmp(attr->name, "job-originating-user-name") &&
2398 (attr->value_tag == IPP_TAG_NAME ||
2399 attr->value_tag == IPP_TAG_NAMELANG))
2400 job_user = attr->values[0].string.text;
2401
2402 attr = attr->next;
2403 }
2404
2405 if (job_id > 0 && job_name && !strcmp(job_name, monitor->job_name) &&
2406 job_user && monitor->user && !strcmp(job_user, monitor->user))
2407 {
2408 monitor->job_id = job_id;
2409 monitor->job_state = job_state;
2410 break;
2411 }
2412
2413 if (!attr)
2414 break;
2415 }
7cf5915e
MS
2416 }
2417
a469f8a5
MS
2418 if ((attr = ippFindAttribute(response, "job-state-reasons",
2419 IPP_TAG_KEYWORD)) != NULL)
2420 {
2421 int i, new_reasons = 0; /* Looping var, new reasons */
2422
2423 for (i = 0; i < attr->num_values; i ++)
2424 {
2425 if (!strcmp(attr->values[i].string.text,
2426 "account-authorization-failed"))
2427 new_reasons |= _CUPS_JSR_ACCOUNT_AUTHORIZATION_FAILED;
2428 else if (!strcmp(attr->values[i].string.text, "account-closed"))
2429 new_reasons |= _CUPS_JSR_ACCOUNT_CLOSED;
2430 else if (!strcmp(attr->values[i].string.text, "account-info-needed"))
2431 new_reasons |= _CUPS_JSR_ACCOUNT_INFO_NEEDED;
2432 else if (!strcmp(attr->values[i].string.text,
2433 "account-limit-reached"))
2434 new_reasons |= _CUPS_JSR_ACCOUNT_LIMIT_REACHED;
2435 else if (!strcmp(attr->values[i].string.text, "job-password-wait"))
2436 new_reasons |= _CUPS_JSR_JOB_PASSWORD_WAIT;
2437 else if (!strcmp(attr->values[i].string.text, "job-release-wait"))
2438 new_reasons |= _CUPS_JSR_JOB_RELEASE_WAIT;
2439 }
2440
2441 if (new_reasons != monitor->job_reasons)
2442 {
2443 if (new_reasons & _CUPS_JSR_ACCOUNT_AUTHORIZATION_FAILED)
2444 fputs("JOBSTATE: account-authorization-failed\n", stderr);
2445 else if (new_reasons & _CUPS_JSR_ACCOUNT_CLOSED)
2446 fputs("JOBSTATE: account-closed\n", stderr);
2447 else if (new_reasons & _CUPS_JSR_ACCOUNT_INFO_NEEDED)
2448 fputs("JOBSTATE: account-info-needed\n", stderr);
2449 else if (new_reasons & _CUPS_JSR_ACCOUNT_LIMIT_REACHED)
2450 fputs("JOBSTATE: account-limit-reached\n", stderr);
2451 else if (new_reasons & _CUPS_JSR_JOB_PASSWORD_WAIT)
2452 fputs("JOBSTATE: job-password-wait\n", stderr);
2453 else if (new_reasons & _CUPS_JSR_JOB_RELEASE_WAIT)
2454 fputs("JOBSTATE: job-release-wait\n", stderr);
2455 else
2456 fputs("JOBSTATE: job-printing\n", stderr);
2457
2458 monitor->job_reasons = new_reasons;
2459 }
2460 }
2461
dcb445bc
MS
2462 ippDelete(response);
2463
f3c17241
MS
2464 fprintf(stderr, "DEBUG: (monitor) job-state=%s\n",
2465 ippEnumString("job-state", monitor->job_state));
2466
2467 if (!job_canceled &&
2468 (monitor->job_state == IPP_JOB_CANCELED ||
2469 monitor->job_state == IPP_JOB_ABORTED))
2470 job_canceled = -1;
2471
7cf5915e
MS
2472 /*
2473 * Disconnect from the printer - we'll reconnect on the next poll...
2474 */
2475
2476 _httpDisconnect(http);
2477 }
2478
2479 /*
f14324a7 2480 * Sleep for N seconds...
7cf5915e
MS
2481 */
2482
7e86f2f6 2483 sleep((unsigned)delay);
7cf5915e 2484
f14324a7 2485 delay = _cupsNextDelay(delay, &prev_delay);
7cf5915e
MS
2486 }
2487
dcb445bc
MS
2488 /*
2489 * Cancel the job if necessary...
2490 */
2491
f3c17241 2492 if (job_canceled > 0 && monitor->job_id > 0)
e7dc514d 2493 {
dcb445bc 2494 if (!httpReconnect(http))
e7dc514d 2495 {
dcb445bc
MS
2496 cancel_job(http, monitor->uri, monitor->job_id, monitor->resource,
2497 monitor->user, monitor->version);
2498
e7dc514d
MS
2499 if (cupsLastError() > IPP_OK_CONFLICT)
2500 _cupsLangPrintFilter(stderr, "ERROR", _("Unable to cancel print job."));
2501 }
2502 }
2503
7cf5915e
MS
2504 /*
2505 * Cleanup and return...
2506 */
2507
2508 httpClose(http);
2509
2510 return (NULL);
2511}
2512
2513
0268488e
MS
2514/*
2515 * 'new_request()' - Create a new print creation or validation request.
2516 */
2517
2518static ipp_t * /* O - Request data */
2519new_request(
2520 ipp_op_t op, /* I - IPP operation code */
2521 int version, /* I - IPP version number */
2522 const char *uri, /* I - printer-uri value */
2523 const char *user, /* I - requesting-user-name value */
2524 const char *title, /* I - job-name value */
2525 int num_options, /* I - Number of options to send */
2526 cups_option_t *options, /* I - Options to send */
2527 const char *compression, /* I - compression value or NULL */
c8fef167 2528 int copies, /* I - copies value or 0 */
dcb445bc 2529 const char *format, /* I - document-format value or NULL */
f14324a7 2530 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
a469f8a5 2531 ppd_file_t *ppd, /* I - PPD file data */
84315f46 2532 ipp_attribute_t *media_col_sup, /* I - media-col-supported values */
a469f8a5 2533 ipp_attribute_t *doc_handling_sup, /* I - multiple-document-handling-supported values */
f2a7bf2a
MS
2534 ipp_attribute_t *print_color_mode_sup)
2535 /* I - Printer supports print-color-mode */
0268488e
MS
2536{
2537 int i; /* Looping var */
2538 ipp_t *request; /* Request data */
2539 const char *keyword; /* PWG keyword */
2540 _pwg_size_t *size; /* PWG media size */
2541 ipp_t *media_col, /* media-col value */
2542 *media_size; /* media-size value */
2543 const char *media_source, /* media-source value */
84315f46 2544 *media_type, /* media-type value */
5a9febac
MS
2545 *collate_str, /* multiple-document-handling value */
2546 *mandatory; /* Mandatory attributes */
a469f8a5
MS
2547 ipp_tag_t group; /* Current group */
2548 ipp_attribute_t *attr; /* Current attribute */
2549 const char *color_attr_name; /* Supported color attribute */
2550 char buffer[1024]; /* Value buffer */
0268488e
MS
2551
2552
2553 /*
2554 * Create the IPP request...
2555 */
2556
7e86f2f6
MS
2557 request = ippNewRequest(op);
2558 ippSetVersion(request, version / 10, version % 10);
0268488e
MS
2559
2560 fprintf(stderr, "DEBUG: %s IPP/%d.%d\n",
2561 ippOpString(request->request.op.operation_id),
2562 request->request.op.version[0],
2563 request->request.op.version[1]);
2564
2565 /*
2566 * Add standard attributes...
2567 */
2568
2569 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
2570 NULL, uri);
2571 fprintf(stderr, "DEBUG: printer-uri=\"%s\"\n", uri);
2572
2573 if (user && *user)
2574 {
2575 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
2576 "requesting-user-name", NULL, user);
2577 fprintf(stderr, "DEBUG: requesting-user-name=\"%s\"\n", user);
2578 }
2579
2580 if (title && *title)
2581 {
2582 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
2583 title);
2584 fprintf(stderr, "DEBUG: job-name=\"%s\"\n", title);
2585 }
2586
a29fd7dd 2587 if (format && op != IPP_CREATE_JOB)
0268488e
MS
2588 {
2589 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE,
2590 "document-format", NULL, format);
2591 fprintf(stderr, "DEBUG: document-format=\"%s\"\n", format);
2592 }
2593
2594#ifdef HAVE_LIBZ
a469f8a5 2595 if (compression && op != IPP_OP_CREATE_JOB && op != IPP_OP_VALIDATE_JOB)
0268488e
MS
2596 {
2597 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
2598 "compression", NULL, compression);
2599 fprintf(stderr, "DEBUG: compression=\"%s\"\n", compression);
2600 }
2601#endif /* HAVE_LIBZ */
2602
2603 /*
2604 * Handle options on the command-line...
2605 */
2606
2607 if (num_options > 0)
2608 {
f14324a7 2609 if (pc)
0268488e 2610 {
dcb445bc
MS
2611 int num_finishings = 0, /* Number of finishing values */
2612 finishings[10]; /* Finishing enum values */
a469f8a5 2613 ppd_choice_t *choice; /* Marked choice */
dcb445bc 2614
0268488e
MS
2615 /*
2616 * Send standard IPP attributes...
2617 */
2618
a469f8a5
MS
2619 fputs("DEBUG: Adding standard IPP operation/job attributes.\n", stderr);
2620
5a9febac
MS
2621 if (pc->password &&
2622 (keyword = cupsGetOption("job-password", num_options,
2623 options)) != NULL)
2624 {
7e86f2f6 2625 ippAddOctetString(request, IPP_TAG_OPERATION, "job-password", keyword, (int)strlen(keyword));
a469f8a5
MS
2626
2627 if ((keyword = cupsGetOption("job-password-encryption", num_options,
2628 options)) == NULL)
2629 keyword = "none";
2630
5a9febac 2631 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
a469f8a5 2632 "job-password-encryption", NULL, keyword);
5a9febac
MS
2633 }
2634
94446c26
MS
2635 if (pc->account_id)
2636 {
2637 if ((keyword = cupsGetOption("job-account-id", num_options,
2638 options)) == NULL)
2639 keyword = cupsGetOption("job-billing", num_options, options);
5a9febac 2640
94446c26
MS
2641 if (keyword)
2642 ippAddString(request, IPP_TAG_JOB, IPP_TAG_NAME, "job-account-id",
2643 NULL, keyword);
2644 }
2645
2646 if (pc->accounting_user_id)
2647 {
2648 if ((keyword = cupsGetOption("job-accounting-user-id", num_options,
2649 options)) == NULL)
2650 keyword = user;
2651
2652 if (keyword)
2653 ippAddString(request, IPP_TAG_JOB, IPP_TAG_NAME,
2654 "job-accounting-user-id", NULL, keyword);
2655 }
5a9febac
MS
2656
2657 for (mandatory = (char *)cupsArrayFirst(pc->mandatory);
2658 mandatory;
2659 mandatory = (char *)cupsArrayNext(pc->mandatory))
2660 {
2661 if (strcmp(mandatory, "copies") &&
2662 strcmp(mandatory, "destination-uris") &&
2663 strcmp(mandatory, "finishings") &&
2664 strcmp(mandatory, "job-account-id") &&
2665 strcmp(mandatory, "job-accounting-user-id") &&
2666 strcmp(mandatory, "job-password") &&
a469f8a5 2667 strcmp(mandatory, "job-password-encryption") &&
5a9febac
MS
2668 strcmp(mandatory, "media") &&
2669 strncmp(mandatory, "media-col", 9) &&
2670 strcmp(mandatory, "multiple-document-handling") &&
2671 strcmp(mandatory, "output-bin") &&
2672 strcmp(mandatory, "print-color-mode") &&
2673 strcmp(mandatory, "print-quality") &&
2674 strcmp(mandatory, "sides") &&
2675 (keyword = cupsGetOption(mandatory, num_options, options)) != NULL)
2676 {
2677 _ipp_option_t *opt = _ippFindOption(mandatory);
2678 /* Option type */
2679 ipp_tag_t value_tag = opt ? opt->value_tag : IPP_TAG_NAME;
2680 /* Value type */
2681
2682 switch (value_tag)
2683 {
2684 case IPP_TAG_INTEGER :
2685 case IPP_TAG_ENUM :
2686 ippAddInteger(request, IPP_TAG_JOB, value_tag, mandatory,
2687 atoi(keyword));
2688 break;
2689 case IPP_TAG_BOOLEAN :
2690 ippAddBoolean(request, IPP_TAG_JOB, mandatory,
2691 !_cups_strcasecmp(keyword, "true"));
2692 break;
2693 case IPP_TAG_RANGE :
2694 {
2695 int lower, upper; /* Range */
2696
2697 if (sscanf(keyword, "%d-%d", &lower, &upper) != 2)
2698 lower = upper = atoi(keyword);
2699
2700 ippAddRange(request, IPP_TAG_JOB, mandatory, lower, upper);
2701 }
2702 break;
2703 case IPP_TAG_STRING :
7e86f2f6 2704 ippAddOctetString(request, IPP_TAG_JOB, mandatory, keyword, (int)strlen(keyword));
5a9febac
MS
2705 break;
2706 default :
f2a7bf2a
MS
2707 if (!strcmp(mandatory, "print-color-mode") && !strcmp(keyword, "monochrome"))
2708 {
2709 if (ippContainsString(print_color_mode_sup, "auto-monochrome"))
2710 keyword = "auto-monochrome";
2711 else if (ippContainsString(print_color_mode_sup, "process-monochrome") && !ippContainsString(print_color_mode_sup, "monochrome"))
2712 keyword = "process-monochrome";
2713 }
2714
5a9febac
MS
2715 ippAddString(request, IPP_TAG_JOB, value_tag, mandatory,
2716 NULL, keyword);
2717 break;
2718 }
2719 }
2720 }
2721
0268488e
MS
2722 if ((keyword = cupsGetOption("PageSize", num_options, options)) == NULL)
2723 keyword = cupsGetOption("media", num_options, options);
2724
f14324a7 2725 if ((size = _ppdCacheGetSize(pc, keyword)) != NULL)
0268488e
MS
2726 {
2727 /*
2728 * Add a media-col value...
2729 */
2730
2731 media_size = ippNew();
2732 ippAddInteger(media_size, IPP_TAG_ZERO, IPP_TAG_INTEGER,
2733 "x-dimension", size->width);
2734 ippAddInteger(media_size, IPP_TAG_ZERO, IPP_TAG_INTEGER,
2735 "y-dimension", size->length);
2736
2737 media_col = ippNew();
2738 ippAddCollection(media_col, IPP_TAG_ZERO, "media-size", media_size);
2739
f14324a7
MS
2740 media_source = _ppdCacheGetSource(pc, cupsGetOption("InputSlot",
2741 num_options,
2742 options));
2743 media_type = _ppdCacheGetType(pc, cupsGetOption("MediaType",
2744 num_options,
2745 options));
0268488e
MS
2746
2747 for (i = 0; i < media_col_sup->num_values; i ++)
2748 {
2749 if (!strcmp(media_col_sup->values[i].string.text,
2750 "media-left-margin"))
2751 ippAddInteger(media_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
2752 "media-left-margin", size->left);
2753 else if (!strcmp(media_col_sup->values[i].string.text,
2754 "media-bottom-margin"))
2755 ippAddInteger(media_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
85dda01c 2756 "media-bottom-margin", size->bottom);
0268488e
MS
2757 else if (!strcmp(media_col_sup->values[i].string.text,
2758 "media-right-margin"))
2759 ippAddInteger(media_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
85dda01c 2760 "media-right-margin", size->right);
0268488e
MS
2761 else if (!strcmp(media_col_sup->values[i].string.text,
2762 "media-top-margin"))
2763 ippAddInteger(media_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
85dda01c 2764 "media-top-margin", size->top);
0268488e
MS
2765 else if (!strcmp(media_col_sup->values[i].string.text,
2766 "media-source") && media_source)
2767 ippAddString(media_col, IPP_TAG_ZERO, IPP_TAG_KEYWORD,
2768 "media-source", NULL, media_source);
2769 else if (!strcmp(media_col_sup->values[i].string.text,
2770 "media-type") && media_type)
2771 ippAddString(media_col, IPP_TAG_ZERO, IPP_TAG_KEYWORD,
2772 "media-type", NULL, media_type);
2773 }
2774
2775 ippAddCollection(request, IPP_TAG_JOB, "media-col", media_col);
2776 }
2777
2778 if ((keyword = cupsGetOption("output-bin", num_options,
2779 options)) == NULL)
a469f8a5
MS
2780 {
2781 if ((choice = ppdFindMarkedChoice(ppd, "OutputBin")) != NULL)
2782 keyword = _ppdCacheGetBin(pc, choice->choice);
2783 }
0268488e
MS
2784
2785 if (keyword)
2786 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "output-bin",
2787 NULL, keyword);
2788
f2a7bf2a 2789 color_attr_name = print_color_mode_sup ? "print-color-mode" : "output-mode";
a469f8a5 2790
5a9febac 2791 if ((keyword = cupsGetOption("print-color-mode", num_options,
f2a7bf2a 2792 options)) == NULL)
0268488e 2793 {
f2a7bf2a
MS
2794 if ((choice = ppdFindMarkedChoice(ppd, "ColorModel")) != NULL)
2795 {
2796 if (!_cups_strcasecmp(choice->choice, "Gray"))
2797 keyword = "monochrome";
2798 else
2799 keyword = "color";
2800 }
2801 }
2802
2803 if (keyword && !strcmp(keyword, "monochrome"))
2804 {
2805 if (ippContainsString(print_color_mode_sup, "auto-monochrome"))
2806 keyword = "auto-monochrome";
2807 else if (ippContainsString(print_color_mode_sup, "process-monochrome") && !ippContainsString(print_color_mode_sup, "monochrome"))
2808 keyword = "process-monochrome";
0268488e
MS
2809 }
2810
f2a7bf2a
MS
2811 if (keyword)
2812 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, color_attr_name,
2813 NULL, keyword);
2814
0268488e
MS
2815 if ((keyword = cupsGetOption("print-quality", num_options,
2816 options)) != NULL)
2817 ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_ENUM, "print-quality",
2818 atoi(keyword));
a469f8a5 2819 else if ((choice = ppdFindMarkedChoice(ppd, "cupsPrintQuality")) != NULL)
0268488e 2820 {
a469f8a5 2821 if (!_cups_strcasecmp(choice->choice, "draft"))
0268488e
MS
2822 ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_ENUM, "print-quality",
2823 IPP_QUALITY_DRAFT);
a469f8a5 2824 else if (!_cups_strcasecmp(choice->choice, "normal"))
0268488e
MS
2825 ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_ENUM, "print-quality",
2826 IPP_QUALITY_NORMAL);
a469f8a5 2827 else if (!_cups_strcasecmp(choice->choice, "high"))
0268488e
MS
2828 ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_ENUM, "print-quality",
2829 IPP_QUALITY_HIGH);
2830 }
2831
2832 if ((keyword = cupsGetOption("sides", num_options, options)) != NULL)
2833 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "sides",
2834 NULL, keyword);
f14324a7 2835 else if (pc->sides_option &&
a469f8a5 2836 (choice = ppdFindMarkedChoice(ppd, pc->sides_option)) != NULL)
0268488e 2837 {
a469f8a5 2838 if (!_cups_strcasecmp(choice->choice, pc->sides_1sided))
0268488e
MS
2839 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "sides",
2840 NULL, "one-sided");
a469f8a5 2841 else if (!_cups_strcasecmp(choice->choice, pc->sides_2sided_long))
0268488e
MS
2842 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "sides",
2843 NULL, "two-sided-long-edge");
a469f8a5 2844 if (!_cups_strcasecmp(choice->choice, pc->sides_2sided_short))
0268488e
MS
2845 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "sides",
2846 NULL, "two-sided-short-edge");
2847 }
84315f46 2848
82cc1f9a
MS
2849 if ((keyword = cupsGetOption("multiple-document-handling",
2850 num_options, options)) != NULL)
2851 {
2852 if (strstr(keyword, "uncollated"))
2853 keyword = "false";
2854 else
2855 keyword = "true";
2856 }
2857 else if ((keyword = cupsGetOption("collate", num_options,
2858 options)) == NULL)
2859 keyword = "true";
2860
2861 if (format)
2862 {
2863 if (!_cups_strcasecmp(format, "image/gif") ||
2864 !_cups_strcasecmp(format, "image/jp2") ||
2865 !_cups_strcasecmp(format, "image/jpeg") ||
2866 !_cups_strcasecmp(format, "image/png") ||
2867 !_cups_strcasecmp(format, "image/tiff") ||
2868 !_cups_strncasecmp(format, "image/x-", 8))
2869 {
2870 /*
2871 * Collation makes no sense for single page image formats...
2872 */
2873
2874 keyword = "false";
2875 }
2876 else if (!_cups_strncasecmp(format, "image/", 6) ||
2877 !_cups_strcasecmp(format, "application/vnd.cups-raster"))
2878 {
2879 /*
2880 * Multi-page image formats will have copies applied by the upstream
2881 * filters...
2882 */
2883
2884 copies = 1;
2885 }
2886 }
2887
2888 if (doc_handling_sup)
84315f46 2889 {
88f9aafc 2890 if (!_cups_strcasecmp(keyword, "true"))
84315f46
MS
2891 collate_str = "separate-documents-collated-copies";
2892 else
2893 collate_str = "separate-documents-uncollated-copies";
eac3a0a0 2894
84315f46
MS
2895 for (i = 0; i < doc_handling_sup->num_values; i ++)
2896 if (!strcmp(doc_handling_sup->values[i].string.text, collate_str))
2897 {
2898 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD,
2899 "multiple-document-handling", NULL, collate_str);
2900 break;
2901 }
3e7fe0ca
MS
2902
2903 if (i >= doc_handling_sup->num_values)
2904 copies = 1;
84315f46 2905 }
dcb445bc
MS
2906
2907 /*
2908 * Map finishing options...
2909 */
2910
2911 num_finishings = _ppdCacheGetFinishingValues(pc, num_options, options,
2912 (int)(sizeof(finishings) /
2913 sizeof(finishings[0])),
2914 finishings);
2915 if (num_finishings > 0)
2916 ippAddIntegers(request, IPP_TAG_JOB, IPP_TAG_ENUM, "finishings",
2917 num_finishings, finishings);
2918
2919 /*
2920 * Map FaxOut options...
2921 */
2922
2923 if ((keyword = cupsGetOption("phone", num_options, options)) != NULL)
2924 {
2925 ipp_t *destination; /* destination collection */
654051c5 2926 char phone[1024], /* Phone number string */
654051c5
MS
2927 *ptr, /* Pointer into string */
2928 tel_uri[1024]; /* tel: URI */
2929 static const char * const allowed = "0123456789#*-+.()";
2930 /* Allowed characters */
dcb445bc
MS
2931
2932 destination = ippNew();
2933
654051c5
MS
2934 /*
2935 * Unescape and filter out spaces and other characters that are not
2936 * allowed in a tel: URI.
2937 */
2938
3b94e1e1
MS
2939 _httpDecodeURI(phone, keyword, sizeof(phone));
2940 for (ptr = phone; *ptr;)
2941 {
2942 if (!strchr(allowed, *ptr))
2943 _cups_strcpy(ptr, ptr + 1);
2944 else
2945 ptr ++;
654051c5
MS
2946 }
2947
654051c5
MS
2948 httpAssembleURI(HTTP_URI_CODING_ALL, tel_uri, sizeof(tel_uri), "tel", NULL, NULL, 0, phone);
2949 ippAddString(destination, IPP_TAG_JOB, IPP_TAG_URI, "destination-uri", NULL, tel_uri);
dcb445bc
MS
2950
2951 if ((keyword = cupsGetOption("faxPrefix", num_options,
2952 options)) != NULL && *keyword)
3b94e1e1
MS
2953 {
2954 char predial[1024]; /* Pre-dial string */
2955
2956 _httpDecodeURI(predial, keyword, sizeof(predial));
2957 ippAddString(destination, IPP_TAG_JOB, IPP_TAG_TEXT, "pre-dial-string", NULL, predial);
2958 }
dcb445bc
MS
2959
2960 ippAddCollection(request, IPP_TAG_JOB, "destination-uris", destination);
2961 ippDelete(destination);
2962 }
0268488e
MS
2963 }
2964 else
2965 {
2966 /*
2967 * When talking to another CUPS server, send all options...
2968 */
2969
a469f8a5
MS
2970 fputs("DEBUG: Adding all operation/job attributes.\n", stderr);
2971 cupsEncodeOptions2(request, num_options, options, IPP_TAG_OPERATION);
a29fd7dd 2972 cupsEncodeOptions2(request, num_options, options, IPP_TAG_JOB);
0268488e
MS
2973 }
2974
82cc1f9a 2975 if (copies > 1 && (!pc || copies <= pc->max_copies))
0268488e
MS
2976 ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_INTEGER, "copies", copies);
2977 }
2978
a469f8a5
MS
2979 fprintf(stderr, "DEBUG: IPP/%d.%d %s #%d\n", version / 10, version % 10,
2980 ippOpString(ippGetOperation(request)), ippGetRequestId(request));
2981 for (group = IPP_TAG_ZERO, attr = ippFirstAttribute(request);
2982 attr;
2983 attr = ippNextAttribute(request))
2984 {
2985 const char *name = ippGetName(attr);
2986
2987 if (!name)
2988 {
2989 group = IPP_TAG_ZERO;
2990 continue;
2991 }
2992
2993 if (group != ippGetGroupTag(attr))
2994 {
2995 group = ippGetGroupTag(attr);
2996 fprintf(stderr, "DEBUG: ---- %s ----\n", ippTagString(group));
2997 }
2998
2999 ippAttributeString(attr, buffer, sizeof(buffer));
3000 fprintf(stderr, "DEBUG: %s %s%s %s\n", name,
3001 ippGetCount(attr) > 1 ? "1setOf " : "",
3002 ippTagString(ippGetValueTag(attr)), buffer);
3003 }
3004
3005 fprintf(stderr, "DEBUG: ---- %s ----\n", ippTagString(IPP_TAG_END));
3006
0268488e
MS
3007 return (request);
3008}
3009
3010
ef416fc2 3011/*
3012 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
3013 */
3014
bd7854cb 3015static const char * /* O - Password */
5a9febac
MS
3016password_cb(const char *prompt, /* I - Prompt (not used) */
3017 http_t *http, /* I - Connection */
3018 const char *method, /* I - Request method (not used) */
3019 const char *resource, /* I - Resource path (not used) */
6961465f 3020 int *password_tries) /* I - Password tries */
ef416fc2 3021{
5a9febac
MS
3022 char def_username[HTTP_MAX_VALUE]; /* Default username */
3023
3024
6961465f
MS
3025 fprintf(stderr, "DEBUG: password_cb(prompt=\"%s\", http=%p, method=\"%s\", "
3026 "resource=\"%s\", password_tries=%p(%d)), password=%p\n",
3027 prompt, http, method, resource, password_tries, *password_tries,
3028 password);
3e7fe0ca 3029
ef416fc2 3030 (void)prompt;
5a9febac
MS
3031 (void)method;
3032 (void)resource;
ef416fc2 3033
cf3d4dd6
MS
3034 if (!uri_credentials)
3035 {
3036 /*
3037 * Remember that we need to authenticate...
3038 */
18ecb428 3039
cf3d4dd6 3040 auth_info_required = "username,password";
18ecb428 3041
cf3d4dd6
MS
3042 if (httpGetSubField(http, HTTP_FIELD_WWW_AUTHENTICATE, "username",
3043 def_username))
3044 {
3045 char quoted[HTTP_MAX_VALUE * 2 + 4];
3046 /* Quoted string */
5a9febac 3047
cf3d4dd6
MS
3048 fprintf(stderr, "ATTR: auth-info-default=%s,\n",
3049 quote_string(def_username, quoted, sizeof(quoted)));
3050 }
5a9febac
MS
3051 }
3052
6961465f 3053 if (password && *password && *password_tries < 3)
76cd9e37 3054 {
6961465f 3055 (*password_tries) ++;
76cd9e37 3056
4745f485
MS
3057 cupsSetUser(username);
3058
ef416fc2 3059 return (password);
76cd9e37 3060 }
ef416fc2 3061 else
3062 {
3063 /*
18ecb428 3064 * Give up after 3 tries or if we don't have a password to begin with...
ef416fc2 3065 */
3066
18ecb428 3067 return (NULL);
ef416fc2 3068 }
3069}
3070
3071
5a9febac
MS
3072/*
3073 * 'quote_string()' - Quote a string value.
3074 */
3075
3076static const char * /* O - Quoted string */
3077quote_string(const char *s, /* I - String */
3078 char *q, /* I - Quoted string buffer */
3079 size_t qsize) /* I - Size of quoted string buffer */
3080{
3081 char *qptr, /* Pointer into string buffer */
3082 *qend; /* End of string buffer */
3083
3084
3085 qptr = q;
3086 qend = q + qsize - 5;
3087
3088 if (qend < q)
3089 {
3090 *q = '\0';
3091 return (q);
3092 }
3093
3094 *qptr++ = '\'';
3095 *qptr++ = '\"';
3096
3097 while (*s && qptr < qend)
3098 {
3099 if (*s == '\\' || *s == '\"' || *s == '\'')
3100 {
d726db79 3101 if (qptr < (qend - 4))
5a9febac
MS
3102 {
3103 *qptr++ = '\\';
3104 *qptr++ = '\\';
3105 *qptr++ = '\\';
3106 }
3107 else
3108 break;
3109 }
3110
3111 *qptr++ = *s++;
3112 }
3113
3114 *qptr++ = '\"';
3115 *qptr++ = '\'';
3116 *qptr = '\0';
3117
3118 return (q);
3119}
3120
3121
1f0275e3
MS
3122/*
3123 * 'report_attr()' - Report an IPP attribute value.
3124 */
3125
3126static void
3127report_attr(ipp_attribute_t *attr) /* I - Attribute */
3128{
eac3a0a0
MS
3129 int i; /* Looping var */
3130 char value[1024], /* Value string */
5a9febac 3131 *valptr; /* Pointer into value string */
eac3a0a0 3132 const char *cached; /* Cached attribute */
1f0275e3
MS
3133
3134
3135 /*
3136 * Convert the attribute values into quoted strings...
3137 */
3138
3139 for (i = 0, valptr = value;
3140 i < attr->num_values && valptr < (value + sizeof(value) - 10);
3141 i ++)
3142 {
3143 if (i > 0)
3144 *valptr++ = ',';
3145
3146 switch (attr->value_tag)
3147 {
3148 case IPP_TAG_INTEGER :
3149 case IPP_TAG_ENUM :
7e86f2f6 3150 snprintf(valptr, sizeof(value) - (size_t)(valptr - value), "%d", attr->values[i].integer);
1f0275e3
MS
3151 valptr += strlen(valptr);
3152 break;
3153
3154 case IPP_TAG_TEXT :
3155 case IPP_TAG_NAME :
3156 case IPP_TAG_KEYWORD :
7e86f2f6 3157 quote_string(attr->values[i].string.text, valptr, (size_t)(value + sizeof(value) - valptr));
5a9febac 3158 valptr += strlen(valptr);
1f0275e3
MS
3159 break;
3160
3161 default :
3162 /*
3163 * Unsupported value type...
3164 */
3165
3166 return;
3167 }
3168 }
3169
3170 *valptr = '\0';
3171
321d8d57
MS
3172 _cupsMutexLock(&report_mutex);
3173
eac3a0a0
MS
3174 if ((cached = cupsGetOption(attr->name, num_attr_cache,
3175 attr_cache)) == NULL || strcmp(cached, value))
3176 {
3177 /*
3178 * Tell the scheduler about the new values...
3179 */
1f0275e3 3180
eac3a0a0
MS
3181 num_attr_cache = cupsAddOption(attr->name, value, num_attr_cache,
3182 &attr_cache);
3183 fprintf(stderr, "ATTR: %s=%s\n", attr->name, value);
3184 }
321d8d57
MS
3185
3186 _cupsMutexUnlock(&report_mutex);
1f0275e3
MS
3187}
3188
3189
ef416fc2 3190/*
3191 * 'report_printer_state()' - Report the printer state.
3192 */
3193
321d8d57
MS
3194static void
3195report_printer_state(ipp_t *ipp) /* I - IPP response */
ef416fc2 3196{
7cf5915e
MS
3197 ipp_attribute_t *pa, /* printer-alert */
3198 *pam, /* printer-alert-message */
3199 *psm, /* printer-state-message */
1f0275e3
MS
3200 *reasons, /* printer-state-reasons */
3201 *marker; /* marker-* attributes */
7cf5915e
MS
3202 char value[1024], /* State/message string */
3203 *valptr; /* Pointer into string */
c8fef167
MS
3204 static int ipp_supplies = -1;
3205 /* Report supply levels? */
ef416fc2 3206
3207
7cf5915e
MS
3208 /*
3209 * Report alerts and messages...
3210 */
3211
3212 if ((pa = ippFindAttribute(ipp, "printer-alert", IPP_TAG_TEXT)) != NULL)
3213 report_attr(pa);
3214
3215 if ((pam = ippFindAttribute(ipp, "printer-alert-message",
3216 IPP_TAG_TEXT)) != NULL)
3217 report_attr(pam);
3218
323c5de1 3219 if ((psm = ippFindAttribute(ipp, "printer-state-message",
3220 IPP_TAG_TEXT)) != NULL)
7cf5915e
MS
3221 {
3222 char *ptr; /* Pointer into message */
3223
3224
3225 strlcpy(value, "INFO: ", sizeof(value));
3226 for (ptr = psm->values[0].string.text, valptr = value + 6;
3227 *ptr && valptr < (value + sizeof(value) - 6);
3228 ptr ++)
3229 {
3230 if (*ptr < ' ' && *ptr > 0 && *ptr != '\t')
3231 {
3232 /*
3233 * Substitute "<XX>" for the control character; sprintf is safe because
3234 * we always leave 6 chars free at the end...
3235 */
3236
3237 sprintf(valptr, "<%02X>", *ptr);
3238 valptr += 4;
3239 }
3240 else
3241 *valptr++ = *ptr;
3242 }
3243
3244 *valptr++ = '\n';
e60ec91f 3245 *valptr = '\0';
7cf5915e
MS
3246
3247 fputs(value, stderr);
3248 }
3249
3250 /*
3251 * Now report printer-state-reasons, filtering out some of the reasons we never
3252 * want to set...
3253 */
323c5de1 3254
ef416fc2 3255 if ((reasons = ippFindAttribute(ipp, "printer-state-reasons",
3256 IPP_TAG_KEYWORD)) == NULL)
321d8d57 3257 return;
ef416fc2 3258
eac3a0a0 3259 update_reasons(reasons, NULL);
ef416fc2 3260
1f0275e3
MS
3261 /*
3262 * Relay the current marker-* attribute values...
3263 */
3264
c8fef167
MS
3265 if (ipp_supplies < 0)
3266 {
3267 ppd_file_t *ppd; /* PPD file */
3268 ppd_attr_t *ppdattr; /* Attribute in PPD file */
3269
3270 if ((ppd = ppdOpenFile(getenv("PPD"))) != NULL &&
3271 (ppdattr = ppdFindAttr(ppd, "cupsIPPSupplies", NULL)) != NULL &&
88f9aafc 3272 ppdattr->value && _cups_strcasecmp(ppdattr->value, "true"))
c8fef167
MS
3273 ipp_supplies = 0;
3274 else
3275 ipp_supplies = 1;
3276
3277 ppdClose(ppd);
3278 }
3279
3280 if (ipp_supplies > 0)
3281 {
3282 if ((marker = ippFindAttribute(ipp, "marker-colors", IPP_TAG_NAME)) != NULL)
3283 report_attr(marker);
3284 if ((marker = ippFindAttribute(ipp, "marker-high-levels",
3285 IPP_TAG_INTEGER)) != NULL)
3286 report_attr(marker);
3287 if ((marker = ippFindAttribute(ipp, "marker-levels",
3288 IPP_TAG_INTEGER)) != NULL)
3289 report_attr(marker);
3290 if ((marker = ippFindAttribute(ipp, "marker-low-levels",
3291 IPP_TAG_INTEGER)) != NULL)
3292 report_attr(marker);
3293 if ((marker = ippFindAttribute(ipp, "marker-message",
3294 IPP_TAG_TEXT)) != NULL)
3295 report_attr(marker);
3296 if ((marker = ippFindAttribute(ipp, "marker-names", IPP_TAG_NAME)) != NULL)
3297 report_attr(marker);
3298 if ((marker = ippFindAttribute(ipp, "marker-types",
3299 IPP_TAG_KEYWORD)) != NULL)
3300 report_attr(marker);
3301 }
ef416fc2 3302}
3303
3304
eac3a0a0
MS
3305#if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
3306/*
3307 * 'run_as_user()' - Run the IPP backend as the printing user.
3308 *
3309 * This function uses an XPC-based user agent to run the backend as the printing
3310 * user. We need to do this in order to have access to the user's Kerberos
3311 * credentials.
3312 */
3313
3314static int /* O - Exit status */
37e7e6e0 3315run_as_user(char *argv[], /* I - Command-line arguments */
eac3a0a0
MS
3316 uid_t uid, /* I - User ID */
3317 const char *device_uri, /* I - Device URI */
3318 int fd) /* I - File to print */
3319{
88f9aafc 3320 const char *auth_negotiate;/* AUTH_NEGOTIATE env var */
eac3a0a0
MS
3321 xpc_connection_t conn; /* Connection to XPC service */
3322 xpc_object_t request; /* Request message dictionary */
3323 __block xpc_object_t response; /* Response message dictionary */
3324 dispatch_semaphore_t sem; /* Semaphore for waiting for response */
3325 int status = CUPS_BACKEND_FAILED;
3326 /* Status of request */
3327
3328
3329 fprintf(stderr, "DEBUG: Running IPP backend as UID %d.\n", (int)uid);
3330
3331 /*
3332 * Connect to the user agent for the specified UID...
3333 */
3334
3335 conn = xpc_connection_create_mach_service(kPMPrintUIToolAgent,
3336 dispatch_get_global_queue(0, 0), 0);
3337 if (!conn)
3338 {
3339 _cupsLangPrintFilter(stderr, "ERROR",
3340 _("Unable to start backend process."));
3341 fputs("DEBUG: Unable to create connection to agent.\n", stderr);
3342 goto cleanup;
3343 }
3344
3345 xpc_connection_set_event_handler(conn,
3346 ^(xpc_object_t event)
3347 {
3348 xpc_type_t messageType = xpc_get_type(event);
3349
3350 if (messageType == XPC_TYPE_ERROR)
3351 {
3352 if (event == XPC_ERROR_CONNECTION_INTERRUPTED)
3353 fprintf(stderr, "DEBUG: Interrupted connection to service %s.\n",
3354 xpc_connection_get_name(conn));
3355 else if (event == XPC_ERROR_CONNECTION_INVALID)
3356 fprintf(stderr, "DEBUG: Connection invalid for service %s.\n",
3357 xpc_connection_get_name(conn));
3358 else
3359 fprintf(stderr, "DEBUG: Unxpected error for service %s: %s\n",
3360 xpc_connection_get_name(conn),
3361 xpc_dictionary_get_string(event, XPC_ERROR_KEY_DESCRIPTION));
3362 }
3363 });
3364 xpc_connection_set_target_uid(conn, uid);
3365 xpc_connection_resume(conn);
3366
3367 /*
3368 * Try starting the backend...
3369 */
3370
3371 request = xpc_dictionary_create(NULL, NULL, 0);
3372 xpc_dictionary_set_int64(request, "command", kPMStartJob);
3373 xpc_dictionary_set_string(request, "device-uri", device_uri);
3374 xpc_dictionary_set_string(request, "job-id", argv[1]);
3375 xpc_dictionary_set_string(request, "user", argv[2]);
3376 xpc_dictionary_set_string(request, "title", argv[3]);
3377 xpc_dictionary_set_string(request, "copies", argv[4]);
3378 xpc_dictionary_set_string(request, "options", argv[5]);
3379 xpc_dictionary_set_string(request, "auth-info-required",
3380 getenv("AUTH_INFO_REQUIRED"));
88f9aafc
MS
3381 if ((auth_negotiate = getenv("AUTH_NEGOTIATE")) != NULL)
3382 xpc_dictionary_set_string(request, "auth-negotiate", auth_negotiate);
eac3a0a0
MS
3383 xpc_dictionary_set_fd(request, "stdin", fd);
3384 xpc_dictionary_set_fd(request, "stderr", 2);
3385 xpc_dictionary_set_fd(request, "side-channel", CUPS_SC_FD);
3386
3387 sem = dispatch_semaphore_create(0);
3388 response = NULL;
3389
3390 xpc_connection_send_message_with_reply(conn, request,
3391 dispatch_get_global_queue(0,0),
3392 ^(xpc_object_t reply)
3393 {
3394 /* Save the response and wake up */
3395 if (xpc_get_type(reply)
3396 == XPC_TYPE_DICTIONARY)
3397 response = xpc_retain(reply);
3398
3399 dispatch_semaphore_signal(sem);
3400 });
3401
3402 dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
3403 xpc_release(request);
3404 dispatch_release(sem);
3405
3406 if (response)
3407 {
7e86f2f6 3408 child_pid = (pid_t)xpc_dictionary_get_int64(response, "child-pid");
eac3a0a0
MS
3409
3410 xpc_release(response);
3411
3412 if (child_pid)
7e86f2f6 3413 fprintf(stderr, "DEBUG: Child PID=%d.\n", (int)child_pid);
eac3a0a0
MS
3414 else
3415 {
3416 _cupsLangPrintFilter(stderr, "ERROR",
3417 _("Unable to start backend process."));
3418 fputs("DEBUG: No child PID.\n", stderr);
3419 goto cleanup;
3420 }
3421 }
3422 else
3423 {
3424 _cupsLangPrintFilter(stderr, "ERROR",
3425 _("Unable to start backend process."));
3426 fputs("DEBUG: No reply from agent.\n", stderr);
3427 goto cleanup;
3428 }
3429
3430 /*
3431 * Then wait for the backend to finish...
3432 */
3433
3434 request = xpc_dictionary_create(NULL, NULL, 0);
3435 xpc_dictionary_set_int64(request, "command", kPMWaitForJob);
3436 xpc_dictionary_set_fd(request, "stderr", 2);
3437
3438 sem = dispatch_semaphore_create(0);
3439 response = NULL;
3440
3441 xpc_connection_send_message_with_reply(conn, request,
3442 dispatch_get_global_queue(0,0),
3443 ^(xpc_object_t reply)
3444 {
3445 /* Save the response and wake up */
3446 if (xpc_get_type(reply)
3447 == XPC_TYPE_DICTIONARY)
3448 response = xpc_retain(reply);
3449
3450 dispatch_semaphore_signal(sem);
3451 });
3452
3453 dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
3454 xpc_release(request);
3455 dispatch_release(sem);
3456
3457 if (response)
3458 {
7e86f2f6 3459 status = (int)xpc_dictionary_get_int64(response, "status");
eac3a0a0
MS
3460
3461 if (status == SIGTERM || status == SIGKILL || status == SIGPIPE)
3462 {
3463 fprintf(stderr, "DEBUG: Child terminated on signal %d.\n", status);
3464 status = CUPS_BACKEND_FAILED;
3465 }
3466 else if (WIFSIGNALED(status))
3467 {
3468 fprintf(stderr, "DEBUG: Child crashed on signal %d.\n", status);
3469 status = CUPS_BACKEND_STOP;
3470 }
3471 else if (WIFEXITED(status))
3472 {
3473 status = WEXITSTATUS(status);
3474 fprintf(stderr, "DEBUG: Child exited with status %d.\n", status);
3475 }
3476
3477 xpc_release(response);
3478 }
3479 else
3480 _cupsLangPrintFilter(stderr, "ERROR",
3481 _("Unable to get backend exit status."));
3482
3483 cleanup:
3484
3485 if (conn)
3486 {
eac3a0a0
MS
3487 xpc_connection_cancel(conn);
3488 xpc_release(conn);
3489 }
3490
3491 return (status);
3492}
3493#endif /* HAVE_GSSAPI && HAVE_XPC */
3494
3495
ef416fc2 3496/*
3497 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
3498 */
3499
3500static void
3501sigterm_handler(int sig) /* I - Signal */
3502{
3503 (void)sig; /* remove compiler warnings... */
3504
f3c17241
MS
3505 write(2, "DEBUG: Got SIGTERM.\n", 20);
3506
eac3a0a0
MS
3507#if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
3508 if (child_pid)
3509 {
3510 kill(child_pid, sig);
3511 child_pid = 0;
3512 }
3513#endif /* HAVE_GSSAPI && HAVE_XPC */
3514
cc754834 3515 if (!job_canceled)
bd7854cb 3516 {
3517 /*
eac3a0a0 3518 * Flag that the job should be canceled...
bd7854cb 3519 */
3520
f3c17241
MS
3521 write(2, "DEBUG: job_canceled = 1.\n", 25);
3522
cc754834 3523 job_canceled = 1;
bd7854cb 3524 return;
3525 }
3526
82f97232
MS
3527 /*
3528 * The scheduler already tried to cancel us once, now just terminate
eac3a0a0 3529 * after removing our temp file!
82f97232
MS
3530 */
3531
3532 if (tmpfilename[0])
3533 unlink(tmpfilename);
3534
ef416fc2 3535 exit(1);
3536}
3537
3538
f228370c
MS
3539/*
3540 * 'timeout_cb()' - Handle HTTP timeouts.
3541 */
3542
3543static int /* O - 1 to continue, 0 to cancel */
3544timeout_cb(http_t *http, /* I - Connection to server (unused) */
3545 void *user_data) /* I - User data (unused) */
3546{
3547 (void)http;
3548 (void)user_data;
3549
3550 return (!job_canceled);
3551}
3552
3553
ef416fc2 3554/*
eac3a0a0
MS
3555 * 'update_reasons()' - Update the printer-state-reasons values.
3556 */
3557
3558static void
3559update_reasons(ipp_attribute_t *attr, /* I - printer-state-reasons or NULL */
3560 const char *s) /* I - STATE: string or NULL */
3561{
3562 char op; /* Add (+), remove (-), replace (\0) */
3563 cups_array_t *new_reasons; /* New reasons array */
3564 char *reason, /* Current reason */
3565 add[2048], /* Reasons added string */
3566 *addptr, /* Pointer into add string */
3567 rem[2048], /* Reasons removed string */
3568 *remptr; /* Pointer into remove string */
3569 const char *addprefix, /* Current add string prefix */
3570 *remprefix; /* Current remove string prefix */
3571
3572
3573 fprintf(stderr, "DEBUG: update_reasons(attr=%d(%s%s), s=\"%s\")\n",
3574 attr ? attr->num_values : 0, attr ? attr->values[0].string.text : "",
3575 attr && attr->num_values > 1 ? ",..." : "", s ? s : "(null)");
3576
3577 /*
3578 * Create an array of new reason keyword strings...
3579 */
3580
3581 if (attr)
3582 {
3583 int i; /* Looping var */
3584
3585 new_reasons = cupsArrayNew((cups_array_func_t)strcmp, NULL);
3586 op = '\0';
3587
3588 for (i = 0; i < attr->num_values; i ++)
3589 {
3590 reason = attr->values[i].string.text;
3591
3592 if (strcmp(reason, "none") &&
3593 strcmp(reason, "none-report") &&
3594 strcmp(reason, "paused") &&
a4845881 3595 strncmp(reason, "spool-area-full", 15) &&
eac3a0a0
MS
3596 strcmp(reason, "com.apple.print.recoverable-warning") &&
3597 strncmp(reason, "cups-", 5))
3598 cupsArrayAdd(new_reasons, reason);
3599 }
3600 }
3601 else if (s)
3602 {
3603 if (*s == '+' || *s == '-')
3604 op = *s++;
3605 else
3606 op = '\0';
3607
5a9febac 3608 new_reasons = _cupsArrayNewStrings(s, ',');
eac3a0a0
MS
3609 }
3610 else
3611 return;
3612
3613 /*
3614 * Compute the changes...
3615 */
3616
3617 add[0] = '\0';
3618 addprefix = "STATE: +";
3619 addptr = add;
3620 rem[0] = '\0';
3621 remprefix = "STATE: -";
3622 remptr = rem;
3623
3624 fprintf(stderr, "DEBUG2: op='%c', new_reasons=%d, state_reasons=%d\n",
3625 op ? op : ' ', cupsArrayCount(new_reasons),
3626 cupsArrayCount(state_reasons));
3627
321d8d57 3628 _cupsMutexLock(&report_mutex);
eac3a0a0
MS
3629
3630 if (op == '+')
3631 {
3632 /*
3633 * Add reasons...
3634 */
3635
3636 for (reason = (char *)cupsArrayFirst(new_reasons);
3637 reason;
3638 reason = (char *)cupsArrayNext(new_reasons))
3639 {
3640 if (!cupsArrayFind(state_reasons, reason))
3641 {
3642 if (!strncmp(reason, "cups-remote-", 12))
3643 {
3644 /*
3645 * If we are setting cups-remote-xxx, remove all other cups-remote-xxx
3646 * keywords...
3647 */
3648
3649 char *temp; /* Current reason in state_reasons */
3650
3651 cupsArraySave(state_reasons);
3652
3653 for (temp = (char *)cupsArrayFirst(state_reasons);
3654 temp;
3655 temp = (char *)cupsArrayNext(state_reasons))
3656 if (!strncmp(temp, "cups-remote-", 12))
3657 {
7e86f2f6 3658 snprintf(remptr, sizeof(rem) - (size_t)(remptr - rem), "%s%s", remprefix, temp);
eac3a0a0
MS
3659 remptr += strlen(remptr);
3660 remprefix = ",";
3661
3662 cupsArrayRemove(state_reasons, temp);
3663 break;
3664 }
3665
3666 cupsArrayRestore(state_reasons);
3667 }
3668
3669 cupsArrayAdd(state_reasons, reason);
3670
7e86f2f6 3671 snprintf(addptr, sizeof(add) - (size_t)(addptr - add), "%s%s", addprefix, reason);
eac3a0a0
MS
3672 addptr += strlen(addptr);
3673 addprefix = ",";
3674 }
3675 }
3676 }
3677 else if (op == '-')
3678 {
3679 /*
3680 * Remove reasons...
3681 */
3682
3683 for (reason = (char *)cupsArrayFirst(new_reasons);
3684 reason;
3685 reason = (char *)cupsArrayNext(new_reasons))
3686 {
3687 if (cupsArrayFind(state_reasons, reason))
3688 {
7e86f2f6 3689 snprintf(remptr, sizeof(rem) - (size_t)(remptr - rem), "%s%s", remprefix, reason);
eac3a0a0
MS
3690 remptr += strlen(remptr);
3691 remprefix = ",";
3692
3693 cupsArrayRemove(state_reasons, reason);
3694 }
3695 }
3696 }
3697 else
3698 {
3699 /*
3700 * Replace reasons...
3701 */
3702
3703 for (reason = (char *)cupsArrayFirst(state_reasons);
3704 reason;
3705 reason = (char *)cupsArrayNext(state_reasons))
3706 {
3707 if (strncmp(reason, "cups-", 5) && !cupsArrayFind(new_reasons, reason))
3708 {
7e86f2f6 3709 snprintf(remptr, sizeof(rem) - (size_t)(remptr - rem), "%s%s", remprefix, reason);
eac3a0a0
MS
3710 remptr += strlen(remptr);
3711 remprefix = ",";
3712
3713 cupsArrayRemove(state_reasons, reason);
3714 }
3715 }
3716
3717 for (reason = (char *)cupsArrayFirst(new_reasons);
3718 reason;
3719 reason = (char *)cupsArrayNext(new_reasons))
3720 {
3721 if (!cupsArrayFind(state_reasons, reason))
3722 {
3723 cupsArrayAdd(state_reasons, reason);
3724
7e86f2f6 3725 snprintf(addptr, sizeof(add) - (size_t)(addptr - add), "%s%s", addprefix, reason);
eac3a0a0
MS
3726 addptr += strlen(addptr);
3727 addprefix = ",";
3728 }
3729 }
3730 }
3731
321d8d57 3732 _cupsMutexUnlock(&report_mutex);
eac3a0a0
MS
3733
3734 /*
3735 * Report changes and return...
3736 */
3737
3738 if (add[0] && rem[0])
3739 fprintf(stderr, "%s\n%s\n", add, rem);
3740 else if (add[0])
3741 fprintf(stderr, "%s\n", add);
3742 else if (rem[0])
3743 fprintf(stderr, "%s\n", rem);
3744}
3745
3746/*
f2d18633 3747 * End of "$Id$".
ef416fc2 3748 */