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