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