]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
b19ccc9e | 2 | * "$Id: ipp.c 7948 2008-09-17 00:04:12Z mike $" |
ef416fc2 | 3 | * |
4 | * IPP backend for the Common UNIX Printing System (CUPS). | |
5 | * | |
c168a833 | 6 | * Copyright 2007-2009 by Apple Inc. |
7594b224 | 7 | * Copyright 1997-2007 by Easy Software Products, all rights reserved. |
ef416fc2 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 10 | * property of Apple Inc. and are protected by Federal copyright |
11 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
ef416fc2 | 12 | * "LICENSE" which should have been included with this file. If this |
bc44d920 | 13 | * file is missing or damaged, see the license at "http://www.cups.org/". |
ef416fc2 | 14 | * |
15 | * This file is subject to the Apple OS-Developed Software exception. | |
16 | * | |
17 | * Contents: | |
18 | * | |
19 | * main() - Send a file to the printer or server. | |
b423cd4c | 20 | * cancel_job() - Cancel a print job. |
ef416fc2 | 21 | * check_printer_state() - Check the printer state... |
b423cd4c | 22 | * compress_files() - Compress print files... |
ef416fc2 | 23 | * password_cb() - Disable the password prompt for |
24 | * cupsDoFileRequest(). | |
1f0275e3 | 25 | * report_attr() - Report an IPP attribute value. |
ef416fc2 | 26 | * report_printer_state() - Report the printer state. |
27 | * run_pictwps_filter() - Convert PICT files to PostScript when printing | |
28 | * remotely. | |
29 | * sigterm_handler() - Handle 'terminate' signals that stop the backend. | |
30 | */ | |
31 | ||
32 | /* | |
33 | * Include necessary headers. | |
34 | */ | |
35 | ||
36 | #include <cups/http-private.h> | |
7a14d768 | 37 | #include "backend-private.h" |
ef416fc2 | 38 | #include <sys/types.h> |
39 | #include <sys/stat.h> | |
ef416fc2 | 40 | #include <sys/wait.h> |
41 | ||
ef416fc2 | 42 | /* |
43 | * Globals... | |
44 | */ | |
45 | ||
46 | static char *password = NULL; /* Password for device URI */ | |
76cd9e37 | 47 | static int password_tries = 0; /* Password tries */ |
18ecb428 MS |
48 | static const char *auth_info_required = "none"; |
49 | /* New auth-info-required value */ | |
ef416fc2 | 50 | #ifdef __APPLE__ |
51 | static char pstmpname[1024] = ""; /* Temporary PostScript file name */ | |
52 | #endif /* __APPLE__ */ | |
53 | static char tmpfilename[1024] = ""; /* Temporary spool file name */ | |
bd7854cb | 54 | static int job_cancelled = 0; /* Job cancelled? */ |
ef416fc2 | 55 | |
56 | ||
57 | /* | |
58 | * Local functions... | |
59 | */ | |
60 | ||
bd7854cb | 61 | static void cancel_job(http_t *http, const char *uri, int id, |
62 | const char *resource, const char *user, int version); | |
63 | static void check_printer_state(http_t *http, const char *uri, | |
ef416fc2 | 64 | const char *resource, const char *user, |
db1f069b | 65 | int version, int job_id); |
b423cd4c | 66 | #ifdef HAVE_LIBZ |
67 | static void compress_files(int num_files, char **files); | |
68 | #endif /* HAVE_LIBZ */ | |
bd7854cb | 69 | static const char *password_cb(const char *); |
1f0275e3 | 70 | static void report_attr(ipp_attribute_t *attr); |
db1f069b | 71 | static int report_printer_state(ipp_t *ipp, int job_id); |
ef416fc2 | 72 | |
73 | #ifdef __APPLE__ | |
bd7854cb | 74 | static int run_pictwps_filter(char **argv, const char *filename); |
ef416fc2 | 75 | #endif /* __APPLE__ */ |
76 | static void sigterm_handler(int sig); | |
77 | ||
78 | ||
79 | /* | |
80 | * 'main()' - Send a file to the printer or server. | |
81 | * | |
82 | * Usage: | |
83 | * | |
84 | * printer-uri job-id user title copies options [file] | |
85 | */ | |
86 | ||
87 | int /* O - Exit status */ | |
bd7854cb | 88 | main(int argc, /* I - Number of command-line args */ |
ef416fc2 | 89 | char *argv[]) /* I - Command-line arguments */ |
90 | { | |
91 | int i; /* Looping var */ | |
b94498cf | 92 | int send_options; /* Send job options? */ |
ef416fc2 | 93 | int num_options; /* Number of printer options */ |
94 | cups_option_t *options; /* Printer options */ | |
1f0275e3 | 95 | const char *device_uri; /* Device URI */ |
acb056cb | 96 | char scheme[255], /* Scheme in URI */ |
ef416fc2 | 97 | hostname[1024], /* Hostname */ |
98 | username[255], /* Username info */ | |
99 | resource[1024], /* Resource info (printer name) */ | |
26d47ec6 | 100 | addrname[256], /* Address name */ |
ef416fc2 | 101 | *optptr, /* Pointer to URI options */ |
db1f069b MS |
102 | *name, /* Name of option */ |
103 | *value, /* Value of option */ | |
104 | sep; /* Separator character */ | |
7a14d768 MS |
105 | int snmp_fd, /* SNMP socket */ |
106 | start_count, /* Page count via SNMP at start */ | |
426c6a59 MS |
107 | page_count, /* Page count via SNMP */ |
108 | have_supplies; /* Printer supports supply levels? */ | |
bd7854cb | 109 | int num_files; /* Number of files to print */ |
110 | char **files, /* Files to print */ | |
111 | *filename; /* Pointer to single filename */ | |
ef416fc2 | 112 | int port; /* Port number (not used) */ |
113 | char uri[HTTP_MAX_URI]; /* Updated URI without user/pass */ | |
114 | ipp_status_t ipp_status; /* Status of IPP request */ | |
115 | http_t *http; /* HTTP connection */ | |
116 | ipp_t *request, /* IPP request */ | |
117 | *response, /* IPP response */ | |
118 | *supported; /* get-printer-attributes response */ | |
c0e1af83 | 119 | time_t start_time; /* Time of first connect */ |
c0e1af83 | 120 | int contimeout; /* Connection timeout */ |
121 | int delay; /* Delay for retries... */ | |
b423cd4c | 122 | int compression, /* Do compression of the job data? */ |
123 | waitjob, /* Wait for job complete? */ | |
ef416fc2 | 124 | waitprinter; /* Wait for printer ready? */ |
125 | ipp_attribute_t *job_id_attr; /* job-id attribute */ | |
126 | int job_id; /* job-id value */ | |
bd7854cb | 127 | ipp_attribute_t *job_sheets; /* job-media-sheets-completed */ |
128 | ipp_attribute_t *job_state; /* job-state */ | |
129 | ipp_attribute_t *copies_sup; /* copies-supported */ | |
130 | ipp_attribute_t *format_sup; /* document-format-supported */ | |
ef416fc2 | 131 | ipp_attribute_t *printer_state; /* printer-state attribute */ |
bd7854cb | 132 | ipp_attribute_t *printer_accepting; /* printer-is-accepting-jobs */ |
d09495fa | 133 | int copies, /* Number of copies for job */ |
134 | copies_remaining; /* Number of copies remaining */ | |
c277e2f8 MS |
135 | const char *content_type, /* CONTENT_TYPE environment variable */ |
136 | *final_content_type; /* FINAL_CONTENT_TYPE environment var */ | |
ef416fc2 | 137 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) |
138 | struct sigaction action; /* Actions for POSIX signals */ | |
139 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ | |
140 | int version; /* IPP version */ | |
ef416fc2 | 141 | static const char * const pattrs[] = |
142 | { /* Printer attributes we want */ | |
143 | "copies-supported", | |
ef416fc2 | 144 | "document-format-supported", |
1f0275e3 | 145 | "marker-colors", |
f11a948a | 146 | "marker-high-levels", |
1f0275e3 | 147 | "marker-levels", |
f11a948a | 148 | "marker-low-levels", |
1f0275e3 MS |
149 | "marker-message", |
150 | "marker-names", | |
151 | "marker-types", | |
ef416fc2 | 152 | "printer-is-accepting-jobs", |
153 | "printer-state", | |
323c5de1 | 154 | "printer-state-message", |
ef416fc2 | 155 | "printer-state-reasons", |
156 | }; | |
157 | static const char * const jattrs[] = | |
158 | { /* Job attributes we want */ | |
159 | "job-media-sheets-completed", | |
160 | "job-state" | |
161 | }; | |
162 | ||
163 | ||
164 | /* | |
165 | * Make sure status messages are not buffered... | |
166 | */ | |
167 | ||
168 | setbuf(stderr, NULL); | |
169 | ||
170 | /* | |
171 | * Ignore SIGPIPE and catch SIGTERM signals... | |
172 | */ | |
173 | ||
174 | #ifdef HAVE_SIGSET | |
175 | sigset(SIGPIPE, SIG_IGN); | |
176 | sigset(SIGTERM, sigterm_handler); | |
177 | #elif defined(HAVE_SIGACTION) | |
178 | memset(&action, 0, sizeof(action)); | |
179 | action.sa_handler = SIG_IGN; | |
180 | sigaction(SIGPIPE, &action, NULL); | |
181 | ||
182 | sigemptyset(&action.sa_mask); | |
183 | sigaddset(&action.sa_mask, SIGTERM); | |
184 | action.sa_handler = sigterm_handler; | |
185 | sigaction(SIGTERM, &action, NULL); | |
186 | #else | |
187 | signal(SIGPIPE, SIG_IGN); | |
188 | signal(SIGTERM, sigterm_handler); | |
189 | #endif /* HAVE_SIGSET */ | |
190 | ||
191 | /* | |
192 | * Check command-line... | |
193 | */ | |
194 | ||
195 | if (argc == 1) | |
196 | { | |
197 | char *s; | |
198 | ||
199 | if ((s = strrchr(argv[0], '/')) != NULL) | |
200 | s ++; | |
201 | else | |
202 | s = argv[0]; | |
203 | ||
8b450588 MS |
204 | printf("network %s \"Unknown\" \"%s (%s)\"\n", |
205 | s, _cupsLangString(cupsLangDefault(), | |
206 | _("Internet Printing Protocol")), s); | |
ef416fc2 | 207 | return (CUPS_BACKEND_OK); |
208 | } | |
bd7854cb | 209 | else if (argc < 6) |
ef416fc2 | 210 | { |
db1f069b MS |
211 | _cupsLangPrintf(stderr, |
212 | _("Usage: %s job-id user title copies options [file]\n"), | |
213 | argv[0]); | |
ef416fc2 | 214 | return (CUPS_BACKEND_STOP); |
215 | } | |
216 | ||
217 | /* | |
bd7854cb | 218 | * Get the (final) content type... |
ef416fc2 | 219 | */ |
220 | ||
c277e2f8 MS |
221 | if ((content_type = getenv("CONTENT_TYPE")) == NULL) |
222 | content_type = "application/octet-stream"; | |
ef416fc2 | 223 | |
c277e2f8 MS |
224 | if ((final_content_type = getenv("FINAL_CONTENT_TYPE")) == NULL) |
225 | { | |
226 | final_content_type = content_type; | |
227 | ||
228 | if (!strncmp(final_content_type, "printer/", 8)) | |
229 | final_content_type = "application/vnd.cups-raw"; | |
230 | } | |
d09495fa | 231 | |
ef416fc2 | 232 | /* |
233 | * Extract the hostname and printer name from the URI... | |
234 | */ | |
235 | ||
1f0275e3 MS |
236 | if ((device_uri = cupsBackendDeviceURI(argv)) == NULL) |
237 | return (CUPS_BACKEND_FAILED); | |
238 | ||
acb056cb MS |
239 | httpSeparateURI(HTTP_URI_CODING_ALL, device_uri, scheme, sizeof(scheme), |
240 | username, sizeof(username), hostname, sizeof(hostname), &port, | |
241 | resource, sizeof(resource)); | |
ef416fc2 | 242 | |
a41f09e2 MS |
243 | if (!port) |
244 | port = IPP_PORT; /* Default to port 631 */ | |
245 | ||
acb056cb | 246 | if (!strcmp(scheme, "https")) |
ef416fc2 | 247 | cupsSetEncryption(HTTP_ENCRYPT_ALWAYS); |
8ca02f3c | 248 | else |
249 | cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED); | |
ef416fc2 | 250 | |
ef416fc2 | 251 | /* |
252 | * See if there are any options... | |
253 | */ | |
254 | ||
b423cd4c | 255 | compression = 0; |
c168a833 | 256 | version = 11; |
ef416fc2 | 257 | waitjob = 1; |
258 | waitprinter = 1; | |
c0e1af83 | 259 | contimeout = 7 * 24 * 60 * 60; |
ef416fc2 | 260 | |
261 | if ((optptr = strchr(resource, '?')) != NULL) | |
262 | { | |
263 | /* | |
264 | * Yup, terminate the device name string and move to the first | |
265 | * character of the optptr... | |
266 | */ | |
267 | ||
268 | *optptr++ = '\0'; | |
269 | ||
270 | /* | |
271 | * Then parse the optptr... | |
272 | */ | |
273 | ||
274 | while (*optptr) | |
275 | { | |
276 | /* | |
277 | * Get the name... | |
278 | */ | |
279 | ||
db1f069b | 280 | name = optptr; |
ef416fc2 | 281 | |
db1f069b MS |
282 | while (*optptr && *optptr != '=' && *optptr != '+' && *optptr != '&') |
283 | optptr ++; | |
284 | ||
285 | if ((sep = *optptr) != '\0') | |
286 | *optptr++ = '\0'; | |
287 | ||
288 | if (sep == '=') | |
ef416fc2 | 289 | { |
290 | /* | |
291 | * Get the value... | |
292 | */ | |
293 | ||
db1f069b | 294 | value = optptr; |
ef416fc2 | 295 | |
db1f069b | 296 | while (*optptr && *optptr != '+' && *optptr != '&') |
ef416fc2 | 297 | optptr ++; |
db1f069b MS |
298 | |
299 | if (*optptr) | |
300 | *optptr++ = '\0'; | |
ef416fc2 | 301 | } |
302 | else | |
db1f069b | 303 | value = (char *)""; |
ef416fc2 | 304 | |
305 | /* | |
306 | * Process the option... | |
307 | */ | |
308 | ||
309 | if (!strcasecmp(name, "waitjob")) | |
310 | { | |
311 | /* | |
312 | * Wait for job completion? | |
313 | */ | |
314 | ||
315 | waitjob = !strcasecmp(value, "on") || | |
316 | !strcasecmp(value, "yes") || | |
317 | !strcasecmp(value, "true"); | |
318 | } | |
319 | else if (!strcasecmp(name, "waitprinter")) | |
320 | { | |
321 | /* | |
322 | * Wait for printer idle? | |
323 | */ | |
324 | ||
325 | waitprinter = !strcasecmp(value, "on") || | |
326 | !strcasecmp(value, "yes") || | |
327 | !strcasecmp(value, "true"); | |
328 | } | |
329 | else if (!strcasecmp(name, "encryption")) | |
330 | { | |
331 | /* | |
332 | * Enable/disable encryption? | |
333 | */ | |
334 | ||
335 | if (!strcasecmp(value, "always")) | |
336 | cupsSetEncryption(HTTP_ENCRYPT_ALWAYS); | |
337 | else if (!strcasecmp(value, "required")) | |
338 | cupsSetEncryption(HTTP_ENCRYPT_REQUIRED); | |
339 | else if (!strcasecmp(value, "never")) | |
340 | cupsSetEncryption(HTTP_ENCRYPT_NEVER); | |
341 | else if (!strcasecmp(value, "ifrequested")) | |
342 | cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED); | |
343 | else | |
344 | { | |
db1f069b | 345 | _cupsLangPrintf(stderr, |
4d301e69 | 346 | _("ERROR: Unknown encryption option value \"%s\"\n"), |
c0e1af83 | 347 | value); |
ef416fc2 | 348 | } |
349 | } | |
350 | else if (!strcasecmp(name, "version")) | |
351 | { | |
352 | if (!strcmp(value, "1.0")) | |
c168a833 | 353 | version = 10; |
ef416fc2 | 354 | else if (!strcmp(value, "1.1")) |
c168a833 MS |
355 | version = 11; |
356 | else if (!strcmp(value, "2.0")) | |
357 | version = 20; | |
358 | else if (!strcmp(value, "2.1")) | |
359 | version = 21; | |
ef416fc2 | 360 | else |
361 | { | |
db1f069b | 362 | _cupsLangPrintf(stderr, |
4d301e69 | 363 | _("ERROR: Unknown version option value \"%s\"\n"), |
c0e1af83 | 364 | value); |
ef416fc2 | 365 | } |
366 | } | |
b423cd4c | 367 | #ifdef HAVE_LIBZ |
368 | else if (!strcasecmp(name, "compression")) | |
369 | { | |
370 | compression = !strcasecmp(value, "true") || | |
371 | !strcasecmp(value, "yes") || | |
372 | !strcasecmp(value, "on") || | |
373 | !strcasecmp(value, "gzip"); | |
374 | } | |
375 | #endif /* HAVE_LIBZ */ | |
c0e1af83 | 376 | else if (!strcasecmp(name, "contimeout")) |
377 | { | |
378 | /* | |
379 | * Set the connection timeout... | |
380 | */ | |
381 | ||
382 | if (atoi(value) > 0) | |
383 | contimeout = atoi(value); | |
384 | } | |
ef416fc2 | 385 | else |
386 | { | |
387 | /* | |
388 | * Unknown option... | |
389 | */ | |
390 | ||
db1f069b | 391 | _cupsLangPrintf(stderr, |
4d301e69 | 392 | _("ERROR: Unknown option \"%s\" with value \"%s\"\n"), |
db1f069b | 393 | name, value); |
ef416fc2 | 394 | } |
395 | } | |
396 | } | |
397 | ||
b423cd4c | 398 | /* |
399 | * If we have 7 arguments, print the file named on the command-line. | |
400 | * Otherwise, copy stdin to a temporary file and print the temporary | |
401 | * file. | |
402 | */ | |
403 | ||
404 | if (argc == 6) | |
405 | { | |
406 | /* | |
407 | * Copy stdin to a temporary file... | |
408 | */ | |
409 | ||
e07d4801 MS |
410 | int fd; /* File descriptor */ |
411 | http_addrlist_t *addrlist; /* Address list */ | |
e07d4801 | 412 | off_t tbytes; /* Total bytes copied */ |
b423cd4c | 413 | |
414 | ||
acb056cb MS |
415 | fputs("STATE: +connecting-to-device\n", stderr); |
416 | fprintf(stderr, "DEBUG: Looking up \"%s\"...\n", hostname); | |
417 | ||
e07d4801 MS |
418 | if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, "1")) == NULL) |
419 | { | |
4d301e69 | 420 | _cupsLangPrintf(stderr, _("ERROR: Unable to locate printer \'%s\'\n"), |
e07d4801 MS |
421 | hostname); |
422 | return (CUPS_BACKEND_STOP); | |
423 | } | |
424 | ||
425 | snmp_fd = _cupsSNMPOpen(addrlist->addr.addr.sa_family); | |
426 | ||
b423cd4c | 427 | if ((fd = cupsTempFd(tmpfilename, sizeof(tmpfilename))) < 0) |
428 | { | |
080811b1 | 429 | _cupsLangPrintError(_("ERROR: Unable to create temporary file")); |
b423cd4c | 430 | return (CUPS_BACKEND_FAILED); |
431 | } | |
432 | ||
8b116e60 | 433 | _cupsLangPuts(stderr, _("INFO: Copying print data...\n")); |
b423cd4c | 434 | |
ef55b745 | 435 | tbytes = backendRunLoop(-1, fd, snmp_fd, &(addrlist->addr), 0, 0, |
4e6f60f0 | 436 | backendNetworkSideCB); |
e07d4801 MS |
437 | |
438 | if (snmp_fd >= 0) | |
439 | _cupsSNMPClose(snmp_fd); | |
440 | ||
441 | httpAddrFreeList(addrlist); | |
442 | ||
8b116e60 | 443 | close(fd); |
b423cd4c | 444 | |
1f0275e3 MS |
445 | /* |
446 | * Don't try printing files less than 2 bytes... | |
447 | */ | |
448 | ||
449 | if (tbytes <= 1) | |
450 | { | |
4d301e69 | 451 | _cupsLangPuts(stderr, _("ERROR: Empty print file\n")); |
1f0275e3 MS |
452 | unlink(tmpfilename); |
453 | return (CUPS_BACKEND_FAILED); | |
454 | } | |
455 | ||
b423cd4c | 456 | /* |
457 | * Point to the single file from stdin... | |
458 | */ | |
459 | ||
c277e2f8 MS |
460 | filename = tmpfilename; |
461 | num_files = 1; | |
462 | files = &filename; | |
b94498cf | 463 | send_options = 0; |
b423cd4c | 464 | } |
465 | else | |
466 | { | |
467 | /* | |
468 | * Point to the files on the command-line... | |
469 | */ | |
470 | ||
c277e2f8 MS |
471 | num_files = argc - 6; |
472 | files = argv + 6; | |
473 | send_options = 1; | |
b94498cf | 474 | |
b423cd4c | 475 | #ifdef HAVE_LIBZ |
476 | if (compression) | |
477 | compress_files(num_files, files); | |
478 | #endif /* HAVE_LIBZ */ | |
479 | } | |
480 | ||
481 | fprintf(stderr, "DEBUG: %d files to send in job...\n", num_files); | |
482 | ||
ef416fc2 | 483 | /* |
484 | * Set the authentication info, if any... | |
485 | */ | |
486 | ||
487 | cupsSetPasswordCB(password_cb); | |
488 | ||
489 | if (username[0]) | |
490 | { | |
491 | /* | |
492 | * Use authenticaion information in the device URI... | |
493 | */ | |
494 | ||
495 | if ((password = strchr(username, ':')) != NULL) | |
496 | *password++ = '\0'; | |
497 | ||
498 | cupsSetUser(username); | |
499 | } | |
500 | else if (!getuid()) | |
501 | { | |
502 | /* | |
09a101d6 | 503 | * Try loading authentication information from the environment. |
ef416fc2 | 504 | */ |
505 | ||
db1f069b MS |
506 | const char *ptr = getenv("AUTH_USERNAME"); |
507 | ||
508 | if (ptr) | |
09a101d6 | 509 | cupsSetUser(ptr); |
ef416fc2 | 510 | |
09a101d6 | 511 | password = getenv("AUTH_PASSWORD"); |
ef416fc2 | 512 | } |
513 | ||
514 | /* | |
515 | * Try connecting to the remote server... | |
516 | */ | |
517 | ||
4d301e69 MS |
518 | delay = 5; |
519 | start_time = time(NULL); | |
c0e1af83 | 520 | |
757d2cad | 521 | fputs("STATE: +connecting-to-device\n", stderr); |
522 | ||
ef416fc2 | 523 | do |
524 | { | |
acb056cb | 525 | fprintf(stderr, "DEBUG: Connecting to %s:%d\n", hostname, port); |
8b450588 | 526 | _cupsLangPuts(stderr, _("INFO: Connecting to printer...\n")); |
ef416fc2 | 527 | |
528 | if ((http = httpConnectEncrypt(hostname, port, cupsEncryption())) == NULL) | |
529 | { | |
7594b224 | 530 | if (job_cancelled) |
531 | break; | |
532 | ||
ef416fc2 | 533 | if (getenv("CLASS") != NULL) |
534 | { | |
535 | /* | |
536 | * If the CLASS environment variable is set, the job was submitted | |
537 | * to a class and not to a specific queue. In this case, we want | |
538 | * to abort immediately so that the job can be requeued on the next | |
539 | * available printer in the class. | |
540 | */ | |
541 | ||
db1f069b MS |
542 | _cupsLangPuts(stderr, |
543 | _("INFO: Unable to contact printer, queuing on next " | |
544 | "printer in class...\n")); | |
ef416fc2 | 545 | |
d9bca400 MS |
546 | if (tmpfilename[0]) |
547 | unlink(tmpfilename); | |
ef416fc2 | 548 | |
549 | /* | |
550 | * Sleep 5 seconds to keep the job from requeuing too rapidly... | |
551 | */ | |
552 | ||
553 | sleep(5); | |
554 | ||
555 | return (CUPS_BACKEND_FAILED); | |
556 | } | |
557 | ||
558 | if (errno == ECONNREFUSED || errno == EHOSTDOWN || | |
559 | errno == EHOSTUNREACH) | |
560 | { | |
c0e1af83 | 561 | if (contimeout && (time(NULL) - start_time) > contimeout) |
562 | { | |
4d301e69 | 563 | _cupsLangPuts(stderr, _("ERROR: Printer not responding\n")); |
c0e1af83 | 564 | return (CUPS_BACKEND_FAILED); |
565 | } | |
566 | ||
db1f069b | 567 | _cupsLangPrintf(stderr, |
4d301e69 | 568 | _("WARNING: Network host \'%s\' is busy; " |
db1f069b MS |
569 | "will retry in %d seconds...\n"), |
570 | hostname, delay); | |
c0e1af83 | 571 | |
572 | sleep(delay); | |
573 | ||
574 | if (delay < 30) | |
575 | delay += 5; | |
ef416fc2 | 576 | } |
577 | else if (h_errno) | |
578 | { | |
4d301e69 | 579 | _cupsLangPrintf(stderr, _("ERROR: Unable to locate printer \'%s\'\n"), |
db1f069b | 580 | hostname); |
c0e1af83 | 581 | return (CUPS_BACKEND_STOP); |
ef416fc2 | 582 | } |
583 | else | |
584 | { | |
c0e1af83 | 585 | fprintf(stderr, "DEBUG: Connection error: %s\n", strerror(errno)); |
db1f069b | 586 | _cupsLangPuts(stderr, |
4d301e69 MS |
587 | _("ERROR: Unable to connect to printer; will retry in 30 " |
588 | "seconds...\n")); | |
ef416fc2 | 589 | sleep(30); |
590 | } | |
7594b224 | 591 | |
592 | if (job_cancelled) | |
593 | break; | |
ef416fc2 | 594 | } |
595 | } | |
596 | while (http == NULL); | |
597 | ||
68b10830 | 598 | if (job_cancelled || !http) |
7594b224 | 599 | { |
d9bca400 MS |
600 | if (tmpfilename[0]) |
601 | unlink(tmpfilename); | |
7594b224 | 602 | |
603 | return (CUPS_BACKEND_FAILED); | |
604 | } | |
605 | ||
757d2cad | 606 | fputs("STATE: -connecting-to-device\n", stderr); |
8b450588 | 607 | _cupsLangPuts(stderr, _("INFO: Connected to printer...\n")); |
ef416fc2 | 608 | |
26d47ec6 | 609 | #ifdef AF_INET6 |
610 | if (http->hostaddr->addr.sa_family == AF_INET6) | |
c0e1af83 | 611 | fprintf(stderr, "DEBUG: Connected to [%s]:%d (IPv6)...\n", |
612 | httpAddrString(http->hostaddr, addrname, sizeof(addrname)), | |
613 | ntohs(http->hostaddr->ipv6.sin6_port)); | |
26d47ec6 | 614 | else |
615 | #endif /* AF_INET6 */ | |
616 | if (http->hostaddr->addr.sa_family == AF_INET) | |
617 | fprintf(stderr, "DEBUG: Connected to %s:%d (IPv4)...\n", | |
c0e1af83 | 618 | httpAddrString(http->hostaddr, addrname, sizeof(addrname)), |
619 | ntohs(http->hostaddr->ipv4.sin_port)); | |
26d47ec6 | 620 | |
7a14d768 MS |
621 | /* |
622 | * See if the printer supports SNMP... | |
623 | */ | |
624 | ||
625 | if ((snmp_fd = _cupsSNMPOpen(http->hostaddr->addr.sa_family)) >= 0) | |
426c6a59 MS |
626 | have_supplies = !backendSNMPSupplies(snmp_fd, http->hostaddr, &start_count, |
627 | NULL); | |
1f0275e3 | 628 | else |
426c6a59 | 629 | have_supplies = start_count = 0; |
7a14d768 | 630 | |
ef416fc2 | 631 | /* |
632 | * Build a URI for the printer and fill the standard IPP attributes for | |
633 | * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it | |
634 | * might contain username:password information... | |
635 | */ | |
636 | ||
acb056cb MS |
637 | httpAssembleURI(HTTP_URI_CODING_ALL, uri, sizeof(uri), scheme, NULL, hostname, |
638 | port, resource); | |
ef416fc2 | 639 | |
640 | /* | |
641 | * First validate the destination and see if the device supports multiple | |
642 | * copies. We have to do this because some IPP servers (e.g. HP JetDirect) | |
643 | * don't support the copies attribute... | |
644 | */ | |
645 | ||
fa73b229 | 646 | copies_sup = NULL; |
647 | format_sup = NULL; | |
648 | supported = NULL; | |
ef416fc2 | 649 | |
650 | do | |
651 | { | |
7a14d768 MS |
652 | /* |
653 | * Check for side-channel requests... | |
654 | */ | |
655 | ||
656 | backendCheckSideChannel(snmp_fd, http->hostaddr); | |
657 | ||
ef416fc2 | 658 | /* |
659 | * Build the IPP request... | |
660 | */ | |
661 | ||
fa73b229 | 662 | request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES); |
c168a833 MS |
663 | request->request.op.version[0] = version / 10; |
664 | request->request.op.version[1] = version % 10; | |
ef416fc2 | 665 | |
666 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
667 | NULL, uri); | |
668 | ||
669 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
670 | "requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]), | |
671 | NULL, pattrs); | |
672 | ||
673 | /* | |
674 | * Do the request... | |
675 | */ | |
676 | ||
677 | fputs("DEBUG: Getting supported attributes...\n", stderr); | |
678 | ||
97c9a8d7 MS |
679 | if (http->version < HTTP_1_1) |
680 | httpReconnect(http); | |
681 | ||
ef416fc2 | 682 | if ((supported = cupsDoRequest(http, request, resource)) == NULL) |
683 | ipp_status = cupsLastError(); | |
684 | else | |
685 | ipp_status = supported->request.status.status_code; | |
686 | ||
687 | if (ipp_status > IPP_OK_CONFLICT) | |
688 | { | |
689 | if (ipp_status == IPP_PRINTER_BUSY || | |
690 | ipp_status == IPP_SERVICE_UNAVAILABLE) | |
691 | { | |
c0e1af83 | 692 | if (contimeout && (time(NULL) - start_time) > contimeout) |
693 | { | |
4d301e69 | 694 | _cupsLangPuts(stderr, _("ERROR: Printer not responding\n")); |
c0e1af83 | 695 | return (CUPS_BACKEND_FAILED); |
696 | } | |
697 | ||
db1f069b | 698 | _cupsLangPrintf(stderr, |
4d301e69 MS |
699 | _("WARNING: Network host \'%s\' is busy; will retry in " |
700 | "%d seconds...\n"), hostname, delay); | |
c0e1af83 | 701 | |
db1f069b | 702 | report_printer_state(supported, 0); |
c0e1af83 | 703 | |
704 | sleep(delay); | |
705 | ||
706 | if (delay < 30) | |
707 | delay += 5; | |
ef416fc2 | 708 | } |
709 | else if ((ipp_status == IPP_BAD_REQUEST || | |
c168a833 | 710 | ipp_status == IPP_VERSION_NOT_SUPPORTED) && version > 10) |
ef416fc2 | 711 | { |
712 | /* | |
713 | * Switch to IPP/1.0... | |
714 | */ | |
715 | ||
c168a833 MS |
716 | _cupsLangPrintf(stderr, |
717 | _("INFO: Printer does not support IPP/%d.%d, trying " | |
718 | "IPP/1.0...\n"), version / 10, version % 10); | |
719 | version = 10; | |
ef416fc2 | 720 | httpReconnect(http); |
721 | } | |
722 | else if (ipp_status == IPP_NOT_FOUND) | |
723 | { | |
4d301e69 | 724 | _cupsLangPuts(stderr, _("ERROR: Destination printer does not exist\n")); |
ef416fc2 | 725 | |
726 | if (supported) | |
727 | ippDelete(supported); | |
728 | ||
729 | return (CUPS_BACKEND_STOP); | |
730 | } | |
731 | else | |
732 | { | |
db1f069b | 733 | _cupsLangPrintf(stderr, |
4d301e69 | 734 | _("ERROR: Unable to get printer status (%s)\n"), |
db1f069b | 735 | cupsLastErrorString()); |
ef416fc2 | 736 | sleep(10); |
737 | } | |
738 | ||
739 | if (supported) | |
740 | ippDelete(supported); | |
741 | ||
742 | continue; | |
743 | } | |
744 | else if ((copies_sup = ippFindAttribute(supported, "copies-supported", | |
745 | IPP_TAG_RANGE)) != NULL) | |
746 | { | |
747 | /* | |
748 | * Has the "copies-supported" attribute - does it have an upper | |
749 | * bound > 1? | |
750 | */ | |
751 | ||
752 | if (copies_sup->values[0].range.upper <= 1) | |
753 | copies_sup = NULL; /* No */ | |
754 | } | |
755 | ||
fa73b229 | 756 | format_sup = ippFindAttribute(supported, "document-format-supported", |
757 | IPP_TAG_MIMETYPE); | |
ef416fc2 | 758 | |
759 | if (format_sup) | |
760 | { | |
761 | fprintf(stderr, "DEBUG: document-format-supported (%d values)\n", | |
762 | format_sup->num_values); | |
763 | for (i = 0; i < format_sup->num_values; i ++) | |
764 | fprintf(stderr, "DEBUG: [%d] = \"%s\"\n", i, | |
765 | format_sup->values[i].string.text); | |
766 | } | |
767 | ||
db1f069b | 768 | report_printer_state(supported, 0); |
ef416fc2 | 769 | } |
770 | while (ipp_status > IPP_OK_CONFLICT); | |
771 | ||
772 | /* | |
773 | * See if the printer is accepting jobs and is not stopped; if either | |
774 | * condition is true and we are printing to a class, requeue the job... | |
775 | */ | |
776 | ||
777 | if (getenv("CLASS") != NULL) | |
778 | { | |
779 | printer_state = ippFindAttribute(supported, "printer-state", | |
780 | IPP_TAG_ENUM); | |
781 | printer_accepting = ippFindAttribute(supported, "printer-is-accepting-jobs", | |
782 | IPP_TAG_BOOLEAN); | |
783 | ||
784 | if (printer_state == NULL || | |
bd7854cb | 785 | (printer_state->values[0].integer > IPP_PRINTER_PROCESSING && |
786 | waitprinter) || | |
ef416fc2 | 787 | printer_accepting == NULL || |
788 | !printer_accepting->values[0].boolean) | |
789 | { | |
790 | /* | |
791 | * If the CLASS environment variable is set, the job was submitted | |
792 | * to a class and not to a specific queue. In this case, we want | |
793 | * to abort immediately so that the job can be requeued on the next | |
794 | * available printer in the class. | |
795 | */ | |
796 | ||
db1f069b MS |
797 | _cupsLangPuts(stderr, |
798 | _("INFO: Unable to contact printer, queuing on next " | |
799 | "printer in class...\n")); | |
ef416fc2 | 800 | |
801 | ippDelete(supported); | |
802 | httpClose(http); | |
803 | ||
d9bca400 MS |
804 | if (tmpfilename[0]) |
805 | unlink(tmpfilename); | |
ef416fc2 | 806 | |
807 | /* | |
808 | * Sleep 5 seconds to keep the job from requeuing too rapidly... | |
809 | */ | |
810 | ||
811 | sleep(5); | |
812 | ||
813 | return (CUPS_BACKEND_FAILED); | |
814 | } | |
815 | } | |
816 | ||
817 | /* | |
818 | * See if the printer supports multiple copies... | |
819 | */ | |
820 | ||
d09495fa | 821 | copies = atoi(argv[4]); |
822 | ||
ef416fc2 | 823 | if (copies_sup || argc < 7) |
d09495fa | 824 | { |
825 | copies_remaining = 1; | |
826 | ||
827 | if (argc < 7) | |
828 | copies = 1; | |
829 | } | |
ef416fc2 | 830 | else |
d09495fa | 831 | copies_remaining = copies; |
ef416fc2 | 832 | |
ef416fc2 | 833 | /* |
834 | * Then issue the print-job request... | |
835 | */ | |
836 | ||
bd7854cb | 837 | job_id = 0; |
ef416fc2 | 838 | |
d09495fa | 839 | while (copies_remaining > 0) |
ef416fc2 | 840 | { |
7a14d768 MS |
841 | /* |
842 | * Check for side-channel requests... | |
843 | */ | |
844 | ||
845 | backendCheckSideChannel(snmp_fd, http->hostaddr); | |
846 | ||
ef416fc2 | 847 | /* |
848 | * Build the IPP request... | |
849 | */ | |
850 | ||
bd7854cb | 851 | if (job_cancelled) |
852 | break; | |
853 | ||
854 | if (num_files > 1) | |
855 | request = ippNewRequest(IPP_CREATE_JOB); | |
856 | else | |
857 | request = ippNewRequest(IPP_PRINT_JOB); | |
858 | ||
c168a833 MS |
859 | request->request.op.version[0] = version / 10; |
860 | request->request.op.version[1] = version % 10; | |
ef416fc2 | 861 | |
862 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
863 | NULL, uri); | |
864 | ||
865 | fprintf(stderr, "DEBUG: printer-uri = \"%s\"\n", uri); | |
866 | ||
867 | if (argv[2][0]) | |
868 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
869 | "requesting-user-name", NULL, argv[2]); | |
870 | ||
871 | fprintf(stderr, "DEBUG: requesting-user-name = \"%s\"\n", argv[2]); | |
872 | ||
d09495fa | 873 | /* |
874 | * Only add a "job-name" attribute if the remote server supports | |
875 | * copy generation - some IPP implementations like HP's don't seem | |
876 | * to like UTF-8 job names (STR #1837)... | |
877 | */ | |
878 | ||
879 | if (argv[3][0] && copies_sup) | |
ef416fc2 | 880 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL, |
881 | argv[3]); | |
882 | ||
883 | fprintf(stderr, "DEBUG: job-name = \"%s\"\n", argv[3]); | |
884 | ||
b423cd4c | 885 | #ifdef HAVE_LIBZ |
886 | if (compression) | |
887 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
888 | "compression", NULL, "gzip"); | |
889 | #endif /* HAVE_LIBZ */ | |
890 | ||
ef416fc2 | 891 | /* |
892 | * Handle options on the command-line... | |
893 | */ | |
894 | ||
895 | options = NULL; | |
896 | num_options = cupsParseOptions(argv[5], 0, &options); | |
897 | ||
898 | #ifdef __APPLE__ | |
3d052e43 MS |
899 | if (!strcasecmp(final_content_type, "application/pictwps") && |
900 | num_files == 1) | |
ef416fc2 | 901 | { |
902 | if (format_sup != NULL) | |
903 | { | |
904 | for (i = 0; i < format_sup->num_values; i ++) | |
3d052e43 | 905 | if (!strcasecmp(final_content_type, format_sup->values[i].string.text)) |
ef416fc2 | 906 | break; |
907 | } | |
908 | ||
909 | if (format_sup == NULL || i >= format_sup->num_values) | |
910 | { | |
911 | /* | |
912 | * Remote doesn't support "application/pictwps" (i.e. it's not MacOS X) | |
913 | * so convert the document to PostScript... | |
914 | */ | |
915 | ||
d9bca400 MS |
916 | if (run_pictwps_filter(argv, files[0])) |
917 | { | |
918 | if (pstmpname[0]) | |
919 | unlink(pstmpname); | |
920 | ||
921 | if (tmpfilename[0]) | |
922 | unlink(tmpfilename); | |
923 | ||
ef416fc2 | 924 | return (CUPS_BACKEND_FAILED); |
d9bca400 | 925 | } |
ef416fc2 | 926 | |
d9bca400 | 927 | files[0] = pstmpname; |
ef416fc2 | 928 | |
929 | /* | |
bd7854cb | 930 | * Change the MIME type to application/postscript and change the |
931 | * number of copies to 1... | |
ef416fc2 | 932 | */ |
933 | ||
c277e2f8 MS |
934 | final_content_type = "application/postscript"; |
935 | copies = 1; | |
936 | copies_remaining = 1; | |
937 | send_options = 0; | |
ef416fc2 | 938 | } |
939 | } | |
940 | #endif /* __APPLE__ */ | |
941 | ||
c277e2f8 | 942 | if (format_sup != NULL) |
ef416fc2 | 943 | { |
944 | for (i = 0; i < format_sup->num_values; i ++) | |
c277e2f8 | 945 | if (!strcasecmp(final_content_type, format_sup->values[i].string.text)) |
ef416fc2 | 946 | break; |
947 | ||
948 | if (i < format_sup->num_values) | |
09a101d6 | 949 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, |
c277e2f8 | 950 | "document-format", NULL, final_content_type); |
ef416fc2 | 951 | } |
952 | ||
c168a833 | 953 | if (copies_sup && version > 10 && send_options) |
ef416fc2 | 954 | { |
955 | /* | |
956 | * Only send options if the destination printer supports the copies | |
07725fee | 957 | * attribute and IPP/1.1. This is a hack for the HP and Lexmark |
958 | * implementations of IPP, which do not accept extension attributes | |
959 | * and incorrectly report a client-error-bad-request error instead of | |
960 | * the successful-ok-unsupported-attributes status. In short, at least | |
961 | * some HP and Lexmark implementations of IPP are non-compliant. | |
ef416fc2 | 962 | */ |
963 | ||
964 | cupsEncodeOptions(request, num_options, options); | |
d09495fa | 965 | |
ef416fc2 | 966 | ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_INTEGER, "copies", |
d09495fa | 967 | copies); |
ef416fc2 | 968 | } |
969 | ||
970 | cupsFreeOptions(num_options, options); | |
971 | ||
972 | /* | |
973 | * If copies aren't supported, then we are likely dealing with an HP | |
974 | * JetDirect. The HP IPP implementation seems to close the connection | |
07725fee | 975 | * after every request - that is, it does *not* implement HTTP Keep- |
ef416fc2 | 976 | * Alive, which is REQUIRED by HTTP/1.1... |
977 | */ | |
978 | ||
979 | if (!copies_sup) | |
980 | httpReconnect(http); | |
981 | ||
982 | /* | |
983 | * Do the request... | |
984 | */ | |
985 | ||
97c9a8d7 MS |
986 | if (http->version < HTTP_1_1) |
987 | httpReconnect(http); | |
988 | ||
bd7854cb | 989 | if (num_files > 1) |
990 | response = cupsDoRequest(http, request, resource); | |
ef416fc2 | 991 | else |
bd7854cb | 992 | response = cupsDoFileRequest(http, request, resource, files[0]); |
993 | ||
994 | ipp_status = cupsLastError(); | |
ef416fc2 | 995 | |
996 | if (ipp_status > IPP_OK_CONFLICT) | |
997 | { | |
998 | job_id = 0; | |
999 | ||
bd7854cb | 1000 | if (job_cancelled) |
1001 | break; | |
1002 | ||
ef416fc2 | 1003 | if (ipp_status == IPP_SERVICE_UNAVAILABLE || |
1004 | ipp_status == IPP_PRINTER_BUSY) | |
1005 | { | |
db1f069b MS |
1006 | _cupsLangPuts(stderr, |
1007 | _("INFO: Printer busy; will retry in 10 seconds...\n")); | |
ef416fc2 | 1008 | sleep(10); |
1009 | } | |
07725fee | 1010 | else if ((ipp_status == IPP_BAD_REQUEST || |
c168a833 | 1011 | ipp_status == IPP_VERSION_NOT_SUPPORTED) && version > 10) |
07725fee | 1012 | { |
1013 | /* | |
1014 | * Switch to IPP/1.0... | |
1015 | */ | |
1016 | ||
c168a833 MS |
1017 | _cupsLangPrintf(stderr, |
1018 | _("INFO: Printer does not support IPP/%d.%d, trying " | |
1019 | "IPP/1.0...\n"), version / 10, version % 10); | |
1020 | version = 10; | |
07725fee | 1021 | httpReconnect(http); |
1022 | } | |
ef416fc2 | 1023 | else |
68b10830 MS |
1024 | { |
1025 | /* | |
1026 | * Update auth-info-required as needed... | |
1027 | */ | |
1028 | ||
4d301e69 | 1029 | _cupsLangPrintf(stderr, _("ERROR: Print file was not accepted (%s)\n"), |
db1f069b | 1030 | cupsLastErrorString()); |
68b10830 | 1031 | |
18ecb428 | 1032 | if (ipp_status == IPP_NOT_AUTHORIZED || ipp_status == IPP_FORBIDDEN) |
68b10830 MS |
1033 | { |
1034 | fprintf(stderr, "DEBUG: WWW-Authenticate=\"%s\"\n", | |
1035 | httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE)); | |
1036 | ||
18ecb428 MS |
1037 | /* |
1038 | * Normal authentication goes through the password callback, which sets | |
1039 | * auth_info_required to "username,password". Kerberos goes directly | |
1040 | * through GSSAPI, so look for Negotiate in the WWW-Authenticate header | |
1041 | * here and set auth_info_required as needed... | |
1042 | */ | |
1043 | ||
68b10830 MS |
1044 | if (!strncmp(httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE), |
1045 | "Negotiate", 9)) | |
18ecb428 | 1046 | auth_info_required = "negotiate"; |
68b10830 MS |
1047 | } |
1048 | } | |
ef416fc2 | 1049 | } |
1050 | else if ((job_id_attr = ippFindAttribute(response, "job-id", | |
1051 | IPP_TAG_INTEGER)) == NULL) | |
1052 | { | |
db1f069b MS |
1053 | _cupsLangPuts(stderr, |
1054 | _("NOTICE: Print file accepted - job ID unknown.\n")); | |
ef416fc2 | 1055 | job_id = 0; |
1056 | } | |
1057 | else | |
1058 | { | |
1059 | job_id = job_id_attr->values[0].integer; | |
db1f069b MS |
1060 | _cupsLangPrintf(stderr, _("NOTICE: Print file accepted - job ID %d.\n"), |
1061 | job_id); | |
ef416fc2 | 1062 | } |
1063 | ||
bd7854cb | 1064 | ippDelete(response); |
1065 | ||
1066 | if (job_cancelled) | |
1067 | break; | |
1068 | ||
1069 | if (job_id && num_files > 1) | |
1070 | { | |
1071 | for (i = 0; i < num_files; i ++) | |
1072 | { | |
7a14d768 MS |
1073 | /* |
1074 | * Check for side-channel requests... | |
1075 | */ | |
1076 | ||
1077 | backendCheckSideChannel(snmp_fd, http->hostaddr); | |
1078 | ||
1079 | /* | |
1080 | * Send the next file in the job... | |
1081 | */ | |
1082 | ||
bd7854cb | 1083 | request = ippNewRequest(IPP_SEND_DOCUMENT); |
c168a833 MS |
1084 | request->request.op.version[0] = version / 10; |
1085 | request->request.op.version[1] = version % 10; | |
bd7854cb | 1086 | |
1087 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
1088 | NULL, uri); | |
1089 | ||
1090 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", | |
1091 | job_id); | |
1092 | ||
1093 | if (argv[2][0]) | |
1094 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
1095 | "requesting-user-name", NULL, argv[2]); | |
1096 | ||
1097 | if ((i + 1) == num_files) | |
1098 | ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", 1); | |
1099 | ||
1100 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, | |
1101 | "document-format", NULL, content_type); | |
1102 | ||
97c9a8d7 MS |
1103 | if (http->version < HTTP_1_1) |
1104 | httpReconnect(http); | |
1105 | ||
bd7854cb | 1106 | ippDelete(cupsDoFileRequest(http, request, resource, files[i])); |
1107 | ||
1108 | if (cupsLastError() > IPP_OK_CONFLICT) | |
1109 | { | |
1110 | ipp_status = cupsLastError(); | |
1111 | ||
db1f069b MS |
1112 | _cupsLangPrintf(stderr, |
1113 | _("ERROR: Unable to add file %d to job: %s\n"), | |
1114 | job_id, cupsLastErrorString()); | |
bd7854cb | 1115 | break; |
1116 | } | |
1117 | } | |
1118 | } | |
ef416fc2 | 1119 | |
1120 | if (ipp_status <= IPP_OK_CONFLICT && argc > 6) | |
1121 | { | |
1122 | fprintf(stderr, "PAGE: 1 %d\n", copies_sup ? atoi(argv[4]) : 1); | |
d09495fa | 1123 | copies_remaining --; |
ef416fc2 | 1124 | } |
1125 | else if (ipp_status == IPP_SERVICE_UNAVAILABLE || | |
1126 | ipp_status == IPP_PRINTER_BUSY) | |
1f6f3dbc | 1127 | continue; |
ef416fc2 | 1128 | else |
d09495fa | 1129 | copies_remaining --; |
ef416fc2 | 1130 | |
1131 | /* | |
1132 | * Wait for the job to complete... | |
1133 | */ | |
1134 | ||
1135 | if (!job_id || !waitjob) | |
1136 | continue; | |
1137 | ||
db1f069b | 1138 | _cupsLangPuts(stderr, _("INFO: Waiting for job to complete...\n")); |
ef416fc2 | 1139 | |
2e4ff8af | 1140 | for (delay = 1; !job_cancelled;) |
ef416fc2 | 1141 | { |
7a14d768 MS |
1142 | /* |
1143 | * Check for side-channel requests... | |
1144 | */ | |
1145 | ||
1146 | backendCheckSideChannel(snmp_fd, http->hostaddr); | |
1147 | ||
ef416fc2 | 1148 | /* |
1149 | * Build an IPP_GET_JOB_ATTRIBUTES request... | |
1150 | */ | |
1151 | ||
fa73b229 | 1152 | request = ippNewRequest(IPP_GET_JOB_ATTRIBUTES); |
c168a833 MS |
1153 | request->request.op.version[0] = version / 10; |
1154 | request->request.op.version[1] = version % 10; | |
ef416fc2 | 1155 | |
1156 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
1157 | NULL, uri); | |
1158 | ||
1159 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", | |
1160 | job_id); | |
1161 | ||
1162 | if (argv[2][0]) | |
1163 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
1164 | "requesting-user-name", NULL, argv[2]); | |
1165 | ||
1166 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
1167 | "requested-attributes", sizeof(jattrs) / sizeof(jattrs[0]), | |
1168 | NULL, jattrs); | |
1169 | ||
1170 | /* | |
1171 | * Do the request... | |
1172 | */ | |
1173 | ||
97c9a8d7 | 1174 | if (!copies_sup || http->version < HTTP_1_1) |
ef416fc2 | 1175 | httpReconnect(http); |
1176 | ||
bd7854cb | 1177 | response = cupsDoRequest(http, request, resource); |
1178 | ipp_status = cupsLastError(); | |
ef416fc2 | 1179 | |
1180 | if (ipp_status == IPP_NOT_FOUND) | |
1181 | { | |
1182 | /* | |
1183 | * Job has gone away and/or the server has no job history... | |
1184 | */ | |
1185 | ||
1186 | ippDelete(response); | |
1187 | ||
1188 | ipp_status = IPP_OK; | |
1189 | break; | |
1190 | } | |
1191 | ||
1192 | if (ipp_status > IPP_OK_CONFLICT) | |
1193 | { | |
1194 | if (ipp_status != IPP_SERVICE_UNAVAILABLE && | |
1195 | ipp_status != IPP_PRINTER_BUSY) | |
1196 | { | |
bd7854cb | 1197 | ippDelete(response); |
ef416fc2 | 1198 | |
db1f069b | 1199 | _cupsLangPrintf(stderr, |
4d301e69 | 1200 | _("ERROR: Unable to get job %d attributes (%s)\n"), |
db1f069b | 1201 | job_id, cupsLastErrorString()); |
ef416fc2 | 1202 | break; |
1203 | } | |
1204 | } | |
1205 | ||
bd7854cb | 1206 | if (response) |
ef416fc2 | 1207 | { |
1208 | if ((job_state = ippFindAttribute(response, "job-state", | |
1209 | IPP_TAG_ENUM)) != NULL) | |
1210 | { | |
1211 | /* | |
1212 | * Stop polling if the job is finished or pending-held... | |
1213 | */ | |
1214 | ||
b94498cf | 1215 | if (job_state->values[0].integer > IPP_JOB_STOPPED) |
ef416fc2 | 1216 | { |
fa73b229 | 1217 | if ((job_sheets = ippFindAttribute(response, |
1218 | "job-media-sheets-completed", | |
1219 | IPP_TAG_INTEGER)) != NULL) | |
bd7854cb | 1220 | fprintf(stderr, "PAGE: total %d\n", |
1221 | job_sheets->values[0].integer); | |
ef416fc2 | 1222 | |
1223 | ippDelete(response); | |
1224 | break; | |
1225 | } | |
1226 | } | |
7a0cbd5e MS |
1227 | else |
1228 | { | |
1229 | /* | |
1230 | * If the printer does not return a job-state attribute, it does not | |
1231 | * conform to the IPP specification - break out immediately and fail | |
1232 | * the job... | |
1233 | */ | |
1234 | ||
1235 | fputs("DEBUG: No job-state available from printer - stopping queue.\n", | |
1236 | stderr); | |
1237 | ipp_status = IPP_INTERNAL_ERROR; | |
1238 | break; | |
1239 | } | |
ef416fc2 | 1240 | } |
1241 | ||
bd7854cb | 1242 | ippDelete(response); |
ef416fc2 | 1243 | |
1244 | /* | |
1245 | * Check the printer state and report it if necessary... | |
1246 | */ | |
1247 | ||
db1f069b | 1248 | check_printer_state(http, uri, resource, argv[2], version, job_id); |
ef416fc2 | 1249 | |
1250 | /* | |
2e4ff8af | 1251 | * Wait 1-10 seconds before polling again... |
ef416fc2 | 1252 | */ |
1253 | ||
2e4ff8af MS |
1254 | sleep(delay); |
1255 | ||
1256 | delay ++; | |
1257 | if (delay > 10) | |
1258 | delay = 1; | |
ef416fc2 | 1259 | } |
1260 | } | |
1261 | ||
1262 | /* | |
bd7854cb | 1263 | * Cancel the job as needed... |
ef416fc2 | 1264 | */ |
1265 | ||
bd7854cb | 1266 | if (job_cancelled && job_id) |
1267 | cancel_job(http, uri, job_id, resource, argv[2], version); | |
1268 | ||
1269 | /* | |
1270 | * Check the printer state and report it if necessary... | |
1271 | */ | |
ef416fc2 | 1272 | |
db1f069b | 1273 | check_printer_state(http, uri, resource, argv[2], version, job_id); |
ef416fc2 | 1274 | |
7a14d768 MS |
1275 | /* |
1276 | * Collect the final page count as needed... | |
1277 | */ | |
1278 | ||
426c6a59 | 1279 | if (have_supplies && |
7a14d768 MS |
1280 | !backendSNMPSupplies(snmp_fd, http->hostaddr, &page_count, NULL) && |
1281 | page_count > start_count) | |
1282 | fprintf(stderr, "PAGE: total %d\n", page_count - start_count); | |
1283 | ||
18ecb428 MS |
1284 | #ifdef HAVE_GSSAPI |
1285 | /* | |
1286 | * See if we used Kerberos at all... | |
1287 | */ | |
1288 | ||
1289 | if (http->gssctx) | |
1290 | auth_info_required = "negotiate"; | |
1291 | #endif /* HAVE_GSSAPI */ | |
1292 | ||
ef416fc2 | 1293 | /* |
1294 | * Free memory... | |
1295 | */ | |
1296 | ||
1297 | httpClose(http); | |
1298 | ||
bd7854cb | 1299 | ippDelete(supported); |
ef416fc2 | 1300 | |
1301 | /* | |
1302 | * Remove the temporary file(s) if necessary... | |
1303 | */ | |
1304 | ||
1305 | if (tmpfilename[0]) | |
1306 | unlink(tmpfilename); | |
1307 | ||
b423cd4c | 1308 | #ifdef HAVE_LIBZ |
1309 | if (compression) | |
1310 | { | |
1311 | for (i = 0; i < num_files; i ++) | |
1312 | unlink(files[i]); | |
1313 | } | |
1314 | #endif /* HAVE_LIBZ */ | |
1315 | ||
ef416fc2 | 1316 | #ifdef __APPLE__ |
1317 | if (pstmpname[0]) | |
1318 | unlink(pstmpname); | |
1319 | #endif /* __APPLE__ */ | |
1320 | ||
1321 | /* | |
1322 | * Return the queue status... | |
1323 | */ | |
1324 | ||
18ecb428 MS |
1325 | fprintf(stderr, "ATTR: auth-info-required=%s\n", auth_info_required); |
1326 | ||
1327 | if (ipp_status == IPP_NOT_AUTHORIZED || ipp_status == IPP_FORBIDDEN) | |
7ff4fea9 | 1328 | return (CUPS_BACKEND_AUTH_REQUIRED); |
7a0cbd5e MS |
1329 | else if (ipp_status == IPP_INTERNAL_ERROR) |
1330 | return (CUPS_BACKEND_STOP); | |
7ff4fea9 MS |
1331 | else if (ipp_status > IPP_OK_CONFLICT) |
1332 | return (CUPS_BACKEND_FAILED); | |
1333 | else | |
1334 | return (CUPS_BACKEND_OK); | |
ef416fc2 | 1335 | } |
1336 | ||
1337 | ||
bd7854cb | 1338 | /* |
1339 | * 'cancel_job()' - Cancel a print job. | |
1340 | */ | |
1341 | ||
1342 | static void | |
1343 | cancel_job(http_t *http, /* I - HTTP connection */ | |
1344 | const char *uri, /* I - printer-uri */ | |
1345 | int id, /* I - job-id */ | |
1346 | const char *resource, /* I - Resource path */ | |
1347 | const char *user, /* I - requesting-user-name */ | |
1348 | int version) /* I - IPP version */ | |
1349 | { | |
1350 | ipp_t *request; /* Cancel-Job request */ | |
1351 | ||
1352 | ||
db1f069b | 1353 | _cupsLangPuts(stderr, _("INFO: Canceling print job...\n")); |
bd7854cb | 1354 | |
1355 | request = ippNewRequest(IPP_CANCEL_JOB); | |
c168a833 MS |
1356 | request->request.op.version[0] = version / 10; |
1357 | request->request.op.version[1] = version % 10; | |
bd7854cb | 1358 | |
1359 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
1360 | NULL, uri); | |
1361 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", id); | |
1362 | ||
1363 | if (user && user[0]) | |
1364 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
1365 | "requesting-user-name", NULL, user); | |
1366 | ||
1367 | /* | |
1368 | * Do the request... | |
1369 | */ | |
1370 | ||
97c9a8d7 MS |
1371 | if (http->version < HTTP_1_1) |
1372 | httpReconnect(http); | |
1373 | ||
bd7854cb | 1374 | ippDelete(cupsDoRequest(http, request, resource)); |
1375 | ||
1376 | if (cupsLastError() > IPP_OK_CONFLICT) | |
db1f069b MS |
1377 | _cupsLangPrintf(stderr, _("ERROR: Unable to cancel job %d: %s\n"), id, |
1378 | cupsLastErrorString()); | |
bd7854cb | 1379 | } |
1380 | ||
1381 | ||
ef416fc2 | 1382 | /* |
1383 | * 'check_printer_state()' - Check the printer state... | |
1384 | */ | |
1385 | ||
bd7854cb | 1386 | static void |
fa73b229 | 1387 | check_printer_state( |
1388 | http_t *http, /* I - HTTP connection */ | |
1389 | const char *uri, /* I - Printer URI */ | |
1390 | const char *resource, /* I - Resource path */ | |
1391 | const char *user, /* I - Username, if any */ | |
db1f069b MS |
1392 | int version, /* I - IPP version */ |
1393 | int job_id) /* I - Current job ID */ | |
ef416fc2 | 1394 | { |
1395 | ipp_t *request, /* IPP request */ | |
1396 | *response; /* IPP response */ | |
323c5de1 | 1397 | static const char * const attrs[] = /* Attributes we want */ |
1398 | { | |
1f0275e3 MS |
1399 | "marker-colors", |
1400 | "marker-levels", | |
1401 | "marker-message", | |
1402 | "marker-names", | |
1403 | "marker-types", | |
323c5de1 | 1404 | "printer-state-message", |
1405 | "printer-state-reasons" | |
1406 | }; | |
ef416fc2 | 1407 | |
1408 | ||
1409 | /* | |
1410 | * Check on the printer state... | |
1411 | */ | |
1412 | ||
fa73b229 | 1413 | request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES); |
c168a833 MS |
1414 | request->request.op.version[0] = version / 10; |
1415 | request->request.op.version[1] = version % 10; | |
ef416fc2 | 1416 | |
1417 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
1418 | NULL, uri); | |
1419 | ||
1420 | if (user && user[0]) | |
1421 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
1422 | "requesting-user-name", NULL, user); | |
1423 | ||
323c5de1 | 1424 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, |
1425 | "requested-attributes", | |
1426 | (int)(sizeof(attrs) / sizeof(attrs[0])), NULL, attrs); | |
ef416fc2 | 1427 | |
1428 | /* | |
1429 | * Do the request... | |
1430 | */ | |
1431 | ||
97c9a8d7 MS |
1432 | if (http->version < HTTP_1_1) |
1433 | httpReconnect(http); | |
1434 | ||
ef416fc2 | 1435 | if ((response = cupsDoRequest(http, request, resource)) != NULL) |
1436 | { | |
db1f069b | 1437 | report_printer_state(response, job_id); |
ef416fc2 | 1438 | ippDelete(response); |
1439 | } | |
1440 | } | |
1441 | ||
1442 | ||
b423cd4c | 1443 | #ifdef HAVE_LIBZ |
1444 | /* | |
1445 | * 'compress_files()' - Compress print files... | |
1446 | */ | |
1447 | ||
1448 | static void | |
1449 | compress_files(int num_files, /* I - Number of files */ | |
1450 | char **files) /* I - Files */ | |
1451 | { | |
1452 | int i, /* Looping var */ | |
1453 | fd; /* Temporary file descriptor */ | |
1454 | ssize_t bytes; /* Bytes read/written */ | |
1455 | size_t total; /* Total bytes read */ | |
1456 | cups_file_t *in, /* Input file */ | |
1457 | *out; /* Output file */ | |
1458 | struct stat outinfo; /* Output file information */ | |
1459 | char filename[1024], /* Temporary filename */ | |
a74454a7 | 1460 | buffer[32768]; /* Copy buffer */ |
b423cd4c | 1461 | |
1462 | ||
1463 | fprintf(stderr, "DEBUG: Compressing %d job files...\n", num_files); | |
1464 | for (i = 0; i < num_files; i ++) | |
1465 | { | |
1466 | if ((fd = cupsTempFd(filename, sizeof(filename))) < 0) | |
1467 | { | |
db1f069b MS |
1468 | _cupsLangPrintf(stderr, |
1469 | _("ERROR: Unable to create temporary compressed print " | |
1470 | "file: %s\n"), strerror(errno)); | |
b423cd4c | 1471 | exit(CUPS_BACKEND_FAILED); |
1472 | } | |
1473 | ||
1474 | if ((out = cupsFileOpenFd(fd, "w9")) == NULL) | |
1475 | { | |
db1f069b MS |
1476 | _cupsLangPrintf(stderr, |
1477 | _("ERROR: Unable to open temporary compressed print " | |
1478 | "file: %s\n"), strerror(errno)); | |
b423cd4c | 1479 | exit(CUPS_BACKEND_FAILED); |
1480 | } | |
1481 | ||
1482 | if ((in = cupsFileOpen(files[i], "r")) == NULL) | |
1483 | { | |
db1f069b MS |
1484 | _cupsLangPrintf(stderr, |
1485 | _("ERROR: Unable to open print file \"%s\": %s\n"), | |
1486 | files[i], strerror(errno)); | |
b423cd4c | 1487 | cupsFileClose(out); |
1488 | exit(CUPS_BACKEND_FAILED); | |
1489 | } | |
1490 | ||
1491 | total = 0; | |
1492 | while ((bytes = cupsFileRead(in, buffer, sizeof(buffer))) > 0) | |
1493 | if (cupsFileWrite(out, buffer, bytes) < bytes) | |
1494 | { | |
db1f069b MS |
1495 | _cupsLangPrintf(stderr, |
1496 | _("ERROR: Unable to write %d bytes to \"%s\": %s\n"), | |
1497 | (int)bytes, filename, strerror(errno)); | |
b423cd4c | 1498 | cupsFileClose(in); |
1499 | cupsFileClose(out); | |
1500 | exit(CUPS_BACKEND_FAILED); | |
1501 | } | |
1502 | else | |
1503 | total += bytes; | |
1504 | ||
1505 | cupsFileClose(out); | |
1506 | cupsFileClose(in); | |
1507 | ||
1508 | files[i] = strdup(filename); | |
1509 | ||
1510 | if (!stat(filename, &outinfo)) | |
1511 | fprintf(stderr, | |
1512 | "DEBUG: File %d compressed to %.1f%% of original size, " | |
1513 | CUPS_LLFMT " bytes...\n", | |
e1d6a774 | 1514 | i + 1, 100.0 * outinfo.st_size / total, |
1515 | CUPS_LLCAST outinfo.st_size); | |
b423cd4c | 1516 | } |
1517 | } | |
1518 | #endif /* HAVE_LIBZ */ | |
1519 | ||
1520 | ||
ef416fc2 | 1521 | /* |
1522 | * 'password_cb()' - Disable the password prompt for cupsDoFileRequest(). | |
1523 | */ | |
1524 | ||
bd7854cb | 1525 | static const char * /* O - Password */ |
fa73b229 | 1526 | password_cb(const char *prompt) /* I - Prompt (not used) */ |
ef416fc2 | 1527 | { |
1528 | (void)prompt; | |
1529 | ||
18ecb428 MS |
1530 | /* |
1531 | * Remember that we need to authenticate... | |
1532 | */ | |
1533 | ||
1534 | auth_info_required = "username,password"; | |
1535 | ||
3dfe78b3 | 1536 | if (password && *password && password_tries < 3) |
76cd9e37 MS |
1537 | { |
1538 | password_tries ++; | |
1539 | ||
ef416fc2 | 1540 | return (password); |
76cd9e37 | 1541 | } |
ef416fc2 | 1542 | else |
1543 | { | |
1544 | /* | |
18ecb428 | 1545 | * Give up after 3 tries or if we don't have a password to begin with... |
ef416fc2 | 1546 | */ |
1547 | ||
18ecb428 | 1548 | return (NULL); |
ef416fc2 | 1549 | } |
1550 | } | |
1551 | ||
1552 | ||
1f0275e3 MS |
1553 | /* |
1554 | * 'report_attr()' - Report an IPP attribute value. | |
1555 | */ | |
1556 | ||
1557 | static void | |
1558 | report_attr(ipp_attribute_t *attr) /* I - Attribute */ | |
1559 | { | |
1560 | int i; /* Looping var */ | |
1561 | char value[1024], /* Value string */ | |
1562 | *valptr, /* Pointer into value string */ | |
1563 | *attrptr; /* Pointer into attribute value */ | |
1564 | ||
1565 | ||
1566 | /* | |
1567 | * Convert the attribute values into quoted strings... | |
1568 | */ | |
1569 | ||
1570 | for (i = 0, valptr = value; | |
1571 | i < attr->num_values && valptr < (value + sizeof(value) - 10); | |
1572 | i ++) | |
1573 | { | |
1574 | if (i > 0) | |
1575 | *valptr++ = ','; | |
1576 | ||
1577 | switch (attr->value_tag) | |
1578 | { | |
1579 | case IPP_TAG_INTEGER : | |
1580 | case IPP_TAG_ENUM : | |
1581 | snprintf(valptr, sizeof(value) - (valptr - value), "%d", | |
1582 | attr->values[i].integer); | |
1583 | valptr += strlen(valptr); | |
1584 | break; | |
1585 | ||
1586 | case IPP_TAG_TEXT : | |
1587 | case IPP_TAG_NAME : | |
1588 | case IPP_TAG_KEYWORD : | |
1589 | *valptr++ = '\"'; | |
1590 | for (attrptr = attr->values[i].string.text; | |
1591 | *attrptr && valptr < (value + sizeof(value) - 10); | |
1592 | attrptr ++) | |
1593 | { | |
1594 | if (*attrptr == '\\' || *attrptr == '\"') | |
1595 | *valptr++ = '\\'; | |
1596 | ||
1597 | *valptr++ = *attrptr; | |
1598 | } | |
1599 | *valptr++ = '\"'; | |
1600 | break; | |
1601 | ||
1602 | default : | |
1603 | /* | |
1604 | * Unsupported value type... | |
1605 | */ | |
1606 | ||
1607 | return; | |
1608 | } | |
1609 | } | |
1610 | ||
1611 | *valptr = '\0'; | |
1612 | ||
1613 | /* | |
1614 | * Tell the scheduler about the new values... | |
1615 | */ | |
1616 | ||
1617 | fprintf(stderr, "ATTR: %s=%s\n", attr->name, value); | |
1618 | } | |
1619 | ||
1620 | ||
ef416fc2 | 1621 | /* |
1622 | * 'report_printer_state()' - Report the printer state. | |
1623 | */ | |
1624 | ||
bd7854cb | 1625 | static int /* O - Number of reasons shown */ |
db1f069b MS |
1626 | report_printer_state(ipp_t *ipp, /* I - IPP response */ |
1627 | int job_id) /* I - Current job ID */ | |
ef416fc2 | 1628 | { |
1629 | int i; /* Looping var */ | |
1630 | int count; /* Count of reasons shown... */ | |
4d301e69 | 1631 | ipp_attribute_t *psm, /* printer-state-message */ |
1f0275e3 MS |
1632 | *reasons, /* printer-state-reasons */ |
1633 | *marker; /* marker-* attributes */ | |
ef416fc2 | 1634 | const char *reason; /* Current reason */ |
ef416fc2 | 1635 | const char *prefix; /* Prefix for STATE: line */ |
1636 | char state[1024]; /* State string */ | |
1637 | ||
1638 | ||
323c5de1 | 1639 | if ((psm = ippFindAttribute(ipp, "printer-state-message", |
1640 | IPP_TAG_TEXT)) != NULL) | |
1641 | fprintf(stderr, "INFO: %s\n", psm->values[0].string.text); | |
1642 | ||
ef416fc2 | 1643 | if ((reasons = ippFindAttribute(ipp, "printer-state-reasons", |
1644 | IPP_TAG_KEYWORD)) == NULL) | |
1645 | return (0); | |
1646 | ||
4d301e69 MS |
1647 | state[0] = '\0'; |
1648 | prefix = "STATE: "; | |
ef416fc2 | 1649 | |
1650 | for (i = 0, count = 0; i < reasons->num_values; i ++) | |
1651 | { | |
1652 | reason = reasons->values[i].string.text; | |
1653 | ||
4d301e69 MS |
1654 | if (strcmp(reason, "paused") && |
1655 | strcmp(reason, "com.apple.print.recoverable-warning")) | |
db1f069b MS |
1656 | { |
1657 | strlcat(state, prefix, sizeof(state)); | |
1658 | strlcat(state, reason, sizeof(state)); | |
1659 | ||
1660 | prefix = ","; | |
1661 | } | |
ef416fc2 | 1662 | } |
1663 | ||
f11a948a MS |
1664 | if (state[0]) |
1665 | fprintf(stderr, "%s\n", state); | |
ef416fc2 | 1666 | |
1f0275e3 MS |
1667 | /* |
1668 | * Relay the current marker-* attribute values... | |
1669 | */ | |
1670 | ||
1671 | if ((marker = ippFindAttribute(ipp, "marker-colors", IPP_TAG_NAME)) != NULL) | |
1672 | report_attr(marker); | |
f11a948a MS |
1673 | if ((marker = ippFindAttribute(ipp, "marker-high-levels", |
1674 | IPP_TAG_INTEGER)) != NULL) | |
1675 | report_attr(marker); | |
1f0275e3 MS |
1676 | if ((marker = ippFindAttribute(ipp, "marker-levels", |
1677 | IPP_TAG_INTEGER)) != NULL) | |
1678 | report_attr(marker); | |
f11a948a MS |
1679 | if ((marker = ippFindAttribute(ipp, "marker-low-levels", |
1680 | IPP_TAG_INTEGER)) != NULL) | |
1681 | report_attr(marker); | |
1f0275e3 MS |
1682 | if ((marker = ippFindAttribute(ipp, "marker-message", IPP_TAG_TEXT)) != NULL) |
1683 | report_attr(marker); | |
1684 | if ((marker = ippFindAttribute(ipp, "marker-names", IPP_TAG_NAME)) != NULL) | |
1685 | report_attr(marker); | |
1686 | if ((marker = ippFindAttribute(ipp, "marker-types", IPP_TAG_KEYWORD)) != NULL) | |
1687 | report_attr(marker); | |
1688 | ||
ef416fc2 | 1689 | return (count); |
1690 | } | |
1691 | ||
1692 | ||
1693 | #ifdef __APPLE__ | |
1694 | /* | |
1695 | * 'run_pictwps_filter()' - Convert PICT files to PostScript when printing | |
1696 | * remotely. | |
1697 | * | |
1698 | * This step is required because the PICT format is not documented and | |
1699 | * subject to change, so developing a filter for other OS's is infeasible. | |
1700 | * Also, fonts required by the PICT file need to be embedded on the | |
1701 | * client side (which has the fonts), so we run the filter to get a | |
1702 | * PostScript file for printing... | |
1703 | */ | |
1704 | ||
bd7854cb | 1705 | static int /* O - Exit status of filter */ |
fa73b229 | 1706 | run_pictwps_filter(char **argv, /* I - Command-line arguments */ |
1707 | const char *filename)/* I - Filename */ | |
ef416fc2 | 1708 | { |
fa73b229 | 1709 | struct stat fileinfo; /* Print file information */ |
1710 | const char *ppdfile; /* PPD file for destination printer */ | |
1711 | int pid; /* Child process ID */ | |
1712 | int fd; /* Temporary file descriptor */ | |
1713 | int status; /* Exit status of filter */ | |
1714 | const char *printer; /* PRINTER env var */ | |
1715 | static char ppdenv[1024]; /* PPD environment variable */ | |
ef416fc2 | 1716 | |
1717 | ||
1718 | /* | |
1719 | * First get the PPD file for the printer... | |
1720 | */ | |
1721 | ||
1722 | printer = getenv("PRINTER"); | |
1723 | if (!printer) | |
1724 | { | |
db1f069b | 1725 | _cupsLangPuts(stderr, |
4d301e69 | 1726 | _("ERROR: PRINTER environment variable not defined\n")); |
ef416fc2 | 1727 | return (-1); |
1728 | } | |
1729 | ||
1730 | if ((ppdfile = cupsGetPPD(printer)) == NULL) | |
1731 | { | |
db1f069b MS |
1732 | _cupsLangPrintf(stderr, |
1733 | _("ERROR: Unable to get PPD file for printer \"%s\" - " | |
1734 | "%s.\n"), printer, cupsLastErrorString()); | |
ef416fc2 | 1735 | } |
1736 | else | |
1737 | { | |
1738 | snprintf(ppdenv, sizeof(ppdenv), "PPD=%s", ppdfile); | |
1739 | putenv(ppdenv); | |
1740 | } | |
1741 | ||
1742 | /* | |
1743 | * Then create a temporary file for printing... | |
1744 | */ | |
1745 | ||
1746 | if ((fd = cupsTempFd(pstmpname, sizeof(pstmpname))) < 0) | |
1747 | { | |
f0ab5bff | 1748 | _cupsLangPrintError(_("ERROR: Unable to create temporary file")); |
ef416fc2 | 1749 | if (ppdfile) |
1750 | unlink(ppdfile); | |
1751 | return (-1); | |
1752 | } | |
1753 | ||
1754 | /* | |
1755 | * Get the owner of the spool file - it is owned by the user we want to run | |
1756 | * as... | |
1757 | */ | |
1758 | ||
1759 | if (argv[6]) | |
1760 | stat(argv[6], &fileinfo); | |
1761 | else | |
1762 | { | |
1763 | /* | |
1764 | * Use the OSX defaults, as an up-stream filter created the PICT | |
1765 | * file... | |
1766 | */ | |
1767 | ||
1768 | fileinfo.st_uid = 1; | |
1769 | fileinfo.st_gid = 80; | |
1770 | } | |
1771 | ||
1772 | if (ppdfile) | |
1773 | chown(ppdfile, fileinfo.st_uid, fileinfo.st_gid); | |
1774 | ||
1775 | fchown(fd, fileinfo.st_uid, fileinfo.st_gid); | |
1776 | ||
1777 | /* | |
1778 | * Finally, run the filter to convert the file... | |
1779 | */ | |
1780 | ||
1781 | if ((pid = fork()) == 0) | |
1782 | { | |
1783 | /* | |
1784 | * Child process for pictwpstops... Redirect output of pictwpstops to a | |
1785 | * file... | |
1786 | */ | |
1787 | ||
97c9a8d7 | 1788 | dup2(fd, 1); |
ef416fc2 | 1789 | close(fd); |
1790 | ||
1791 | if (!getuid()) | |
1792 | { | |
1793 | /* | |
1794 | * Change to an unpriviledged user... | |
1795 | */ | |
1796 | ||
1797 | setgid(fileinfo.st_gid); | |
1798 | setuid(fileinfo.st_uid); | |
1799 | } | |
1800 | ||
1801 | execlp("pictwpstops", printer, argv[1], argv[2], argv[3], argv[4], argv[5], | |
1802 | filename, NULL); | |
db1f069b MS |
1803 | _cupsLangPrintf(stderr, _("ERROR: Unable to exec pictwpstops: %s\n"), |
1804 | strerror(errno)); | |
ef416fc2 | 1805 | return (errno); |
1806 | } | |
1807 | ||
1808 | close(fd); | |
1809 | ||
1810 | if (pid < 0) | |
1811 | { | |
1812 | /* | |
1813 | * Error! | |
1814 | */ | |
1815 | ||
db1f069b MS |
1816 | _cupsLangPrintf(stderr, _("ERROR: Unable to fork pictwpstops: %s\n"), |
1817 | strerror(errno)); | |
ef416fc2 | 1818 | if (ppdfile) |
1819 | unlink(ppdfile); | |
1820 | return (-1); | |
1821 | } | |
1822 | ||
1823 | /* | |
1824 | * Now wait for the filter to complete... | |
1825 | */ | |
1826 | ||
1827 | if (wait(&status) < 0) | |
1828 | { | |
db1f069b MS |
1829 | _cupsLangPrintf(stderr, _("ERROR: Unable to wait for pictwpstops: %s\n"), |
1830 | strerror(errno)); | |
ef416fc2 | 1831 | close(fd); |
ef416fc2 | 1832 | if (ppdfile) |
1833 | unlink(ppdfile); | |
1834 | return (-1); | |
1835 | } | |
1836 | ||
1837 | if (ppdfile) | |
1838 | unlink(ppdfile); | |
1839 | ||
1840 | close(fd); | |
1841 | ||
1842 | if (status) | |
1843 | { | |
1844 | if (status >= 256) | |
4d301e69 | 1845 | _cupsLangPrintf(stderr, _("ERROR: pictwpstops exited with status %d\n"), |
db1f069b | 1846 | status / 256); |
ef416fc2 | 1847 | else |
4d301e69 | 1848 | _cupsLangPrintf(stderr, _("ERROR: pictwpstops exited on signal %d\n"), |
db1f069b | 1849 | status); |
ef416fc2 | 1850 | |
ef416fc2 | 1851 | return (status); |
1852 | } | |
1853 | ||
1854 | /* | |
1855 | * Return with no errors.. | |
1856 | */ | |
1857 | ||
1858 | return (0); | |
1859 | } | |
1860 | #endif /* __APPLE__ */ | |
1861 | ||
1862 | ||
1863 | /* | |
1864 | * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend. | |
1865 | */ | |
1866 | ||
1867 | static void | |
1868 | sigterm_handler(int sig) /* I - Signal */ | |
1869 | { | |
1870 | (void)sig; /* remove compiler warnings... */ | |
1871 | ||
bd7854cb | 1872 | if (!job_cancelled) |
1873 | { | |
1874 | /* | |
1875 | * Flag that the job should be cancelled... | |
1876 | */ | |
1877 | ||
1878 | job_cancelled = 1; | |
1879 | return; | |
1880 | } | |
1881 | ||
ef416fc2 | 1882 | /* |
bd7854cb | 1883 | * The scheduler already tried to cancel us once, now just terminate |
1884 | * after removing our temp files! | |
ef416fc2 | 1885 | */ |
1886 | ||
1887 | if (tmpfilename[0]) | |
1888 | unlink(tmpfilename); | |
1889 | ||
1890 | #ifdef __APPLE__ | |
1891 | if (pstmpname[0]) | |
1892 | unlink(pstmpname); | |
1893 | #endif /* __APPLE__ */ | |
1894 | ||
1895 | exit(1); | |
1896 | } | |
1897 | ||
1898 | ||
1899 | /* | |
b19ccc9e | 1900 | * End of "$Id: ipp.c 7948 2008-09-17 00:04:12Z mike $". |
ef416fc2 | 1901 | */ |