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