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