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