]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * "$Id: ipp.c,v 1.38.2.23 2003/02/07 12:05:00 mike Exp $" | |
3 | * | |
4 | * IPP backend for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 1997-2003 by Easy Software Products, all rights reserved. | |
7 | * | |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636-3111 USA | |
19 | * | |
20 | * Voice: (301) 373-9603 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * This file is subject to the Apple OS-Developed Software exception. | |
25 | * | |
26 | * Contents: | |
27 | * | |
28 | * main() - Send a file to the printer or server. | |
29 | * password_cb() - Disable the password prompt for | |
30 | * cupsDoFileRequest(). | |
31 | * report_printer_state() - Report the printer state. | |
32 | */ | |
33 | ||
34 | /* | |
35 | * Include necessary headers. | |
36 | */ | |
37 | ||
38 | #include <stdio.h> | |
39 | #include <stdlib.h> | |
40 | #include <errno.h> | |
41 | #include <sys/types.h> | |
42 | #include <sys/stat.h> | |
43 | #include <cups/cups.h> | |
44 | #include <cups/language.h> | |
45 | #include <cups/string.h> | |
46 | #include <signal.h> | |
47 | #include <sys/wait.h> | |
48 | ||
49 | ||
50 | /* | |
51 | * Some OS's don't have hstrerror(), most notably Solaris... | |
52 | */ | |
53 | ||
54 | #ifndef HAVE_HSTRERROR | |
55 | # define hstrerror cups_hstrerror | |
56 | ||
57 | const char * /* O - Error string */ | |
58 | cups_hstrerror(int error) /* I - Error number */ | |
59 | { | |
60 | static const char * const errors[] = | |
61 | { | |
62 | "OK", | |
63 | "Host not found.", | |
64 | "Try again.", | |
65 | "Unrecoverable lookup error.", | |
66 | "No data associated with name." | |
67 | }; | |
68 | ||
69 | ||
70 | if (error < 0 || error > 4) | |
71 | return ("Unknown hostname lookup error."); | |
72 | else | |
73 | return (errors[error]); | |
74 | } | |
75 | #endif /* !HAVE_HSTRERROR */ | |
76 | ||
77 | ||
78 | /* | |
79 | * Local functions... | |
80 | */ | |
81 | ||
82 | const char *password_cb(const char *); | |
83 | int report_printer_state(ipp_t *ipp); | |
84 | ||
85 | #ifdef __APPLE__ | |
86 | int run_pictwps_filter(char **argv, char *filename, int length); | |
87 | #endif /* __APPLE__ */ | |
88 | ||
89 | ||
90 | /* | |
91 | * Local globals... | |
92 | */ | |
93 | ||
94 | char *password = NULL; | |
95 | ||
96 | ||
97 | /* | |
98 | * 'main()' - Send a file to the printer or server. | |
99 | * | |
100 | * Usage: | |
101 | * | |
102 | * printer-uri job-id user title copies options [file] | |
103 | */ | |
104 | ||
105 | int /* O - Exit status */ | |
106 | main(int argc, /* I - Number of command-line arguments (6 or 7) */ | |
107 | char *argv[]) /* I - Command-line arguments */ | |
108 | { | |
109 | int i; /* Looping var */ | |
110 | int num_options; /* Number of printer options */ | |
111 | cups_option_t *options; /* Printer options */ | |
112 | char method[255], /* Method in URI */ | |
113 | hostname[1024], /* Hostname */ | |
114 | username[255], /* Username info */ | |
115 | resource[1024], /* Resource info (printer name) */ | |
116 | filename[1024]; /* File to print */ | |
117 | int port; /* Port number (not used) */ | |
118 | char uri[HTTP_MAX_URI];/* Updated URI without user/pass */ | |
119 | ipp_status_t ipp_status; /* Status of IPP request */ | |
120 | http_t *http; /* HTTP connection */ | |
121 | ipp_t *request, /* IPP request */ | |
122 | *response, /* IPP response */ | |
123 | *supported; /* get-printer-attributes response */ | |
124 | ipp_attribute_t *job_id_attr; /* job-id attribute */ | |
125 | int job_id; /* job-id value */ | |
126 | ipp_attribute_t *job_state; /* job-state attribute */ | |
127 | ipp_attribute_t *copies_sup; /* copies-supported attribute */ | |
128 | ipp_attribute_t *charset_sup; /* charset-supported attribute */ | |
129 | ipp_attribute_t *format_sup; /* document-format-supported attribute */ | |
130 | ipp_attribute_t *printer_state; | |
131 | /* printer-state attribute */ | |
132 | ipp_attribute_t *printer_accepting; | |
133 | /* printer-is-accepting-jobs attribute */ | |
134 | const char *charset; /* Character set to use */ | |
135 | cups_lang_t *language; /* Default language */ | |
136 | int copies; /* Number of copies remaining */ | |
137 | const char *content_type; /* CONTENT_TYPE environment variable */ | |
138 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) | |
139 | struct sigaction action; /* Actions for POSIX signals */ | |
140 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ | |
141 | int version; /* IPP version */ | |
142 | int reasons; /* Number of printer-state-reasons shown */ | |
143 | ||
144 | ||
145 | /* | |
146 | * Make sure status messages are not buffered... | |
147 | */ | |
148 | ||
149 | setbuf(stderr, NULL); | |
150 | ||
151 | /* | |
152 | * Ignore SIGPIPE signals... | |
153 | */ | |
154 | ||
155 | #ifdef HAVE_SIGSET | |
156 | sigset(SIGPIPE, SIG_IGN); | |
157 | #elif defined(HAVE_SIGACTION) | |
158 | memset(&action, 0, sizeof(action)); | |
159 | action.sa_handler = SIG_IGN; | |
160 | sigaction(SIGPIPE, &action, NULL); | |
161 | #else | |
162 | signal(SIGPIPE, SIG_IGN); | |
163 | #endif /* HAVE_SIGSET */ | |
164 | ||
165 | /* | |
166 | * Check command-line... | |
167 | */ | |
168 | ||
169 | if (argc == 1) | |
170 | { | |
171 | char *s; | |
172 | ||
173 | if ((s = strrchr(argv[0], '/')) != NULL) | |
174 | s ++; | |
175 | else | |
176 | s = argv[0]; | |
177 | ||
178 | printf("network %s \"Unknown\" \"Internet Printing Protocol (%s)\"\n", s, s); | |
179 | return (0); | |
180 | } | |
181 | else if (argc < 6 || argc > 7) | |
182 | { | |
183 | fprintf(stderr, "Usage: %s job-id user title copies options [file]\n", | |
184 | argv[0]); | |
185 | return (1); | |
186 | } | |
187 | ||
188 | /* | |
189 | * Get the content type... | |
190 | */ | |
191 | ||
192 | if (argc > 6) | |
193 | content_type = getenv("CONTENT_TYPE"); | |
194 | else | |
195 | content_type = "application/vnd.cups-raw"; | |
196 | ||
197 | if (content_type == NULL) | |
198 | content_type = "application/octet-stream"; | |
199 | ||
200 | /* | |
201 | * If we have 7 arguments, print the file named on the command-line. | |
202 | * Otherwise, copy stdin to a temporary file and print the temporary | |
203 | * file. | |
204 | */ | |
205 | ||
206 | if (argc == 6) | |
207 | { | |
208 | /* | |
209 | * Copy stdin to a temporary file... | |
210 | */ | |
211 | ||
212 | int fd; /* Temporary file */ | |
213 | char buffer[8192]; /* Buffer for copying */ | |
214 | int bytes; /* Number of bytes read */ | |
215 | ||
216 | ||
217 | if ((fd = cupsTempFd(filename, sizeof(filename))) < 0) | |
218 | { | |
219 | perror("ERROR: unable to create temporary file"); | |
220 | return (1); | |
221 | } | |
222 | ||
223 | while ((bytes = fread(buffer, 1, sizeof(buffer), stdin)) > 0) | |
224 | if (write(fd, buffer, bytes) < bytes) | |
225 | { | |
226 | perror("ERROR: unable to write to temporary file"); | |
227 | close(fd); | |
228 | unlink(filename); | |
229 | return (1); | |
230 | } | |
231 | ||
232 | close(fd); | |
233 | } | |
234 | #ifdef __APPLE__ | |
235 | else if (strcasecmp(content_type, "application/pictwps") == 0) | |
236 | { | |
237 | /* | |
238 | * Convert to PostScript... | |
239 | */ | |
240 | ||
241 | if (run_pictwps_filter(argv, filename, sizeof(filename))) | |
242 | return (1); | |
243 | ||
244 | /* | |
245 | * Change the MIME type to application/vnd.cups-postscript... | |
246 | */ | |
247 | ||
248 | content_type = "application/postscript"; | |
249 | } | |
250 | #endif /* __APPLE__ */ | |
251 | else | |
252 | strlcpy(filename, argv[6], sizeof(filename)); | |
253 | ||
254 | /* | |
255 | * Extract the hostname and printer name from the URI... | |
256 | */ | |
257 | ||
258 | httpSeparate(argv[0], method, username, hostname, &port, resource); | |
259 | ||
260 | /* | |
261 | * Set the authentication info, if any... | |
262 | */ | |
263 | ||
264 | cupsSetPasswordCB(password_cb); | |
265 | ||
266 | if (username[0]) | |
267 | { | |
268 | if ((password = strchr(username, ':')) != NULL) | |
269 | *password++ = '\0'; | |
270 | ||
271 | cupsSetUser(username); | |
272 | } | |
273 | ||
274 | /* | |
275 | * Try connecting to the remote server... | |
276 | */ | |
277 | ||
278 | do | |
279 | { | |
280 | fprintf(stderr, "INFO: Connecting to %s on port %d...\n", hostname, port); | |
281 | ||
282 | if ((http = httpConnectEncrypt(hostname, port, cupsEncryption())) == NULL) | |
283 | { | |
284 | if (getenv("CLASS") != NULL) | |
285 | { | |
286 | /* | |
287 | * If the CLASS environment variable is set, the job was submitted | |
288 | * to a class and not to a specific queue. In this case, we want | |
289 | * to abort immediately so that the job can be requeued on the next | |
290 | * available printer in the class. | |
291 | */ | |
292 | ||
293 | fprintf(stderr, "INFO: Unable to queue job on %s, queuing on next printer in class...\n", | |
294 | hostname); | |
295 | ||
296 | if (argc == 6 || strcmp(filename, argv[6])) | |
297 | unlink(filename); | |
298 | ||
299 | /* | |
300 | * Sleep 5 seconds to keep the job from requeuing too rapidly... | |
301 | */ | |
302 | ||
303 | sleep(5); | |
304 | ||
305 | return (1); | |
306 | } | |
307 | ||
308 | if (errno == ECONNREFUSED || errno == EHOSTDOWN || | |
309 | errno == EHOSTUNREACH) | |
310 | { | |
311 | fprintf(stderr, "INFO: Network host \'%s\' is busy; will retry in 30 seconds...", | |
312 | hostname); | |
313 | sleep(30); | |
314 | } | |
315 | else if (h_errno) | |
316 | { | |
317 | fprintf(stderr, "INFO: Unable to lookup host \'%s\' - %s\n", | |
318 | hostname, hstrerror(h_errno)); | |
319 | sleep(30); | |
320 | } | |
321 | else | |
322 | { | |
323 | perror("ERROR: Unable to connect to IPP host"); | |
324 | sleep(30); | |
325 | } | |
326 | } | |
327 | } | |
328 | while (http == NULL); | |
329 | ||
330 | fprintf(stderr, "INFO: Connected to %s...\n", hostname); | |
331 | ||
332 | /* | |
333 | * Build a URI for the printer and fill the standard IPP attributes for | |
334 | * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it | |
335 | * might contain username:password information... | |
336 | */ | |
337 | ||
338 | snprintf(uri, sizeof(uri), "%s://%s:%d%s", method, hostname, port, resource); | |
339 | ||
340 | /* | |
341 | * First validate the destination and see if the device supports multiple | |
342 | * copies. We have to do this because some IPP servers (e.g. HP JetDirect) | |
343 | * don't support the copies attribute... | |
344 | */ | |
345 | ||
346 | language = cupsLangDefault(); | |
347 | charset_sup = NULL; | |
348 | copies_sup = NULL; | |
349 | format_sup = NULL; | |
350 | version = 1; | |
351 | supported = NULL; | |
352 | ||
353 | do | |
354 | { | |
355 | /* | |
356 | * Build the IPP request... | |
357 | */ | |
358 | ||
359 | request = ippNew(); | |
360 | request->request.op.version[1] = version; | |
361 | request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES; | |
362 | request->request.op.request_id = 1; | |
363 | ||
364 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
365 | "attributes-charset", NULL, "utf-8"); | |
366 | ||
367 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
368 | "attributes-natural-language", NULL, | |
369 | language != NULL ? language->language : "en"); | |
370 | ||
371 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
372 | NULL, uri); | |
373 | ||
374 | /* | |
375 | * Do the request... | |
376 | */ | |
377 | ||
378 | fputs("DEBUG: Getting supported attributes...\n", stderr); | |
379 | ||
380 | if ((supported = cupsDoRequest(http, request, resource)) == NULL) | |
381 | ipp_status = cupsLastError(); | |
382 | else | |
383 | ipp_status = supported->request.status.status_code; | |
384 | ||
385 | if (ipp_status > IPP_OK_CONFLICT) | |
386 | { | |
387 | if (ipp_status == IPP_PRINTER_BUSY || | |
388 | ipp_status == IPP_SERVICE_UNAVAILABLE) | |
389 | { | |
390 | fputs("INFO: Printer busy; will retry in 10 seconds...\n", stderr); | |
391 | report_printer_state(supported); | |
392 | sleep(10); | |
393 | } | |
394 | else if ((ipp_status == IPP_BAD_REQUEST || | |
395 | ipp_status == IPP_VERSION_NOT_SUPPORTED) && version == 1) | |
396 | { | |
397 | /* | |
398 | * Switch to IPP/1.0... | |
399 | */ | |
400 | ||
401 | fputs("INFO: Printer does not support IPP/1.1, trying IPP/1.0...\n", stderr); | |
402 | version = 0; | |
403 | httpReconnect(http); | |
404 | } | |
405 | else if (ipp_status == IPP_NOT_FOUND) | |
406 | { | |
407 | fputs("ERROR: Destination printer does not exist!\n", stderr); | |
408 | ||
409 | if (supported) | |
410 | ippDelete(supported); | |
411 | ||
412 | return (1); | |
413 | } | |
414 | else | |
415 | { | |
416 | fprintf(stderr, "ERROR: Unable to get printer status (%s)!\n", | |
417 | ippErrorString(ipp_status)); | |
418 | sleep(10); | |
419 | } | |
420 | ||
421 | if (supported) | |
422 | ippDelete(supported); | |
423 | ||
424 | continue; | |
425 | } | |
426 | else if ((copies_sup = ippFindAttribute(supported, "copies-supported", | |
427 | IPP_TAG_RANGE)) != NULL) | |
428 | { | |
429 | /* | |
430 | * Has the "copies-supported" attribute - does it have an upper | |
431 | * bound > 1? | |
432 | */ | |
433 | ||
434 | if (copies_sup->values[0].range.upper <= 1) | |
435 | copies_sup = NULL; /* No */ | |
436 | } | |
437 | ||
438 | charset_sup = ippFindAttribute(supported, "charset-supported", | |
439 | IPP_TAG_CHARSET); | |
440 | format_sup = ippFindAttribute(supported, "document-format-supported", | |
441 | IPP_TAG_MIMETYPE); | |
442 | ||
443 | if (format_sup) | |
444 | { | |
445 | fprintf(stderr, "DEBUG: document-format-supported (%d values)\n", | |
446 | format_sup->num_values); | |
447 | for (i = 0; i < format_sup->num_values; i ++) | |
448 | fprintf(stderr, "DEBUG: [%d] = \"%s\"\n", i, | |
449 | format_sup->values[i].string.text); | |
450 | } | |
451 | ||
452 | report_printer_state(supported); | |
453 | } | |
454 | while (ipp_status > IPP_OK_CONFLICT); | |
455 | ||
456 | /* | |
457 | * See if the printer is accepting jobs and is not stopped; if either | |
458 | * condition is true and we are printing to a class, requeue the job... | |
459 | */ | |
460 | ||
461 | if (getenv("CLASS") != NULL) | |
462 | { | |
463 | printer_state = ippFindAttribute(supported, "printer-state", | |
464 | IPP_TAG_ENUM); | |
465 | printer_accepting = ippFindAttribute(supported, "printer-is-accepting-jobs", | |
466 | IPP_TAG_BOOLEAN); | |
467 | ||
468 | if (printer_state == NULL || | |
469 | printer_state->values[0].integer > IPP_PRINTER_PROCESSING || | |
470 | printer_accepting == NULL || | |
471 | !printer_accepting->values[0].boolean) | |
472 | { | |
473 | /* | |
474 | * If the CLASS environment variable is set, the job was submitted | |
475 | * to a class and not to a specific queue. In this case, we want | |
476 | * to abort immediately so that the job can be requeued on the next | |
477 | * available printer in the class. | |
478 | */ | |
479 | ||
480 | fprintf(stderr, "INFO: Unable to queue job on %s, queuing on next printer in class...\n", | |
481 | hostname); | |
482 | ||
483 | ippDelete(supported); | |
484 | httpClose(http); | |
485 | ||
486 | if (argc == 6 || strcmp(filename, argv[6])) | |
487 | unlink(filename); | |
488 | ||
489 | /* | |
490 | * Sleep 5 seconds to keep the job from requeuing too rapidly... | |
491 | */ | |
492 | ||
493 | sleep(5); | |
494 | ||
495 | return (1); | |
496 | } | |
497 | } | |
498 | ||
499 | /* | |
500 | * Now that we are "connected" to the port, ignore SIGTERM so that we | |
501 | * can finish out any page data the driver sends (e.g. to eject the | |
502 | * current page... Only ignore SIGTERM if we are printing data from | |
503 | * stdin (otherwise you can't cancel raw jobs...) | |
504 | */ | |
505 | ||
506 | if (argc < 7) | |
507 | { | |
508 | #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ | |
509 | sigset(SIGTERM, SIG_IGN); | |
510 | #elif defined(HAVE_SIGACTION) | |
511 | memset(&action, 0, sizeof(action)); | |
512 | ||
513 | sigemptyset(&action.sa_mask); | |
514 | action.sa_handler = SIG_IGN; | |
515 | sigaction(SIGTERM, &action, NULL); | |
516 | #else | |
517 | signal(SIGTERM, SIG_IGN); | |
518 | #endif /* HAVE_SIGSET */ | |
519 | } | |
520 | ||
521 | /* | |
522 | * See if the printer supports multiple copies... | |
523 | */ | |
524 | ||
525 | if (copies_sup || argc < 7) | |
526 | copies = 1; | |
527 | else | |
528 | copies = atoi(argv[4]); | |
529 | ||
530 | /* | |
531 | * Figure out the character set to use... | |
532 | */ | |
533 | ||
534 | charset = language ? cupsLangEncoding(language) : "us-ascii"; | |
535 | ||
536 | if (charset_sup) | |
537 | { | |
538 | /* | |
539 | * See if IPP server supports the requested character set... | |
540 | */ | |
541 | ||
542 | for (i = 0; i < charset_sup->num_values; i ++) | |
543 | if (strcasecmp(charset, charset_sup->values[i].string.text) == 0) | |
544 | break; | |
545 | ||
546 | /* | |
547 | * If not, choose us-ascii or utf-8... | |
548 | */ | |
549 | ||
550 | if (i >= charset_sup->num_values) | |
551 | { | |
552 | /* | |
553 | * See if us-ascii is supported... | |
554 | */ | |
555 | ||
556 | for (i = 0; i < charset_sup->num_values; i ++) | |
557 | if (strcasecmp("us-ascii", charset_sup->values[i].string.text) == 0) | |
558 | break; | |
559 | ||
560 | if (i < charset_sup->num_values) | |
561 | charset = "us-ascii"; | |
562 | else | |
563 | charset = "utf-8"; | |
564 | } | |
565 | } | |
566 | ||
567 | /* | |
568 | * Then issue the print-job request... | |
569 | */ | |
570 | ||
571 | reasons = 0; | |
572 | ||
573 | while (copies > 0) | |
574 | { | |
575 | /* | |
576 | * Build the IPP request... | |
577 | */ | |
578 | ||
579 | request = ippNew(); | |
580 | request->request.op.version[1] = version; | |
581 | request->request.op.operation_id = IPP_PRINT_JOB; | |
582 | request->request.op.request_id = 1; | |
583 | ||
584 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
585 | "attributes-charset", NULL, charset); | |
586 | ||
587 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
588 | "attributes-natural-language", NULL, | |
589 | language != NULL ? language->language : "en"); | |
590 | ||
591 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
592 | NULL, uri); | |
593 | ||
594 | fprintf(stderr, "DEBUG: printer-uri = \"%s\"\n", uri); | |
595 | ||
596 | if (argv[2][0]) | |
597 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", | |
598 | NULL, argv[2]); | |
599 | ||
600 | fprintf(stderr, "DEBUG: requesting-user-name = \"%s\"\n", argv[2]); | |
601 | ||
602 | if (argv[3][0]) | |
603 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL, | |
604 | argv[3]); | |
605 | ||
606 | fprintf(stderr, "DEBUG: job-name = \"%s\"\n", argv[3]); | |
607 | ||
608 | /* | |
609 | * Handle options on the command-line... | |
610 | */ | |
611 | ||
612 | options = NULL; | |
613 | num_options = cupsParseOptions(argv[5], 0, &options); | |
614 | ||
615 | if (content_type != NULL && format_sup != NULL) | |
616 | { | |
617 | for (i = 0; i < format_sup->num_values; i ++) | |
618 | if (strcasecmp(content_type, format_sup->values[i].string.text) == 0) | |
619 | break; | |
620 | ||
621 | if (i < format_sup->num_values) | |
622 | num_options = cupsAddOption("document-format", content_type, | |
623 | num_options, &options); | |
624 | } | |
625 | ||
626 | if (copies_sup) | |
627 | { | |
628 | /* | |
629 | * Only send options if the destination printer supports the copies | |
630 | * attribute. This is a hack for the HP JetDirect implementation of | |
631 | * IPP, which does not accept extension attributes and incorrectly | |
632 | * reports a client-error-bad-request error instead of the | |
633 | * successful-ok-unsupported-attributes status. In short, at least | |
634 | * some HP implementations of IPP are non-compliant. | |
635 | */ | |
636 | ||
637 | cupsEncodeOptions(request, num_options, options); | |
638 | ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_INTEGER, "copies", | |
639 | atoi(argv[4])); | |
640 | } | |
641 | ||
642 | cupsFreeOptions(num_options, options); | |
643 | ||
644 | /* | |
645 | * If copies aren't supported, then we are likely dealing with an HP | |
646 | * JetDirect. The HP IPP implementation seems to close the connection | |
647 | * after every request (that is, it does *not* implement HTTP Keep- | |
648 | * Alive, which is REQUIRED by HTTP/1.1... | |
649 | */ | |
650 | ||
651 | if (!copies_sup) | |
652 | httpReconnect(http); | |
653 | ||
654 | /* | |
655 | * Do the request... | |
656 | */ | |
657 | ||
658 | if ((response = cupsDoFileRequest(http, request, resource, filename)) == NULL) | |
659 | ipp_status = cupsLastError(); | |
660 | else | |
661 | ipp_status = response->request.status.status_code; | |
662 | ||
663 | if (ipp_status > IPP_OK_CONFLICT) | |
664 | { | |
665 | job_id = 0; | |
666 | ||
667 | if (ipp_status == IPP_SERVICE_UNAVAILABLE || | |
668 | ipp_status == IPP_PRINTER_BUSY) | |
669 | { | |
670 | fputs("INFO: Printer is busy; retrying print job...\n", stderr); | |
671 | sleep(10); | |
672 | } | |
673 | else | |
674 | fprintf(stderr, "ERROR: Print file was not accepted (%s)!\n", | |
675 | ippErrorString(ipp_status)); | |
676 | } | |
677 | else if ((job_id_attr = ippFindAttribute(response, "job-id", | |
678 | IPP_TAG_INTEGER)) == NULL) | |
679 | { | |
680 | fputs("INFO: Print file accepted - job ID unknown.\n", stderr); | |
681 | job_id = 0; | |
682 | } | |
683 | else | |
684 | { | |
685 | job_id = job_id_attr->values[0].integer; | |
686 | fprintf(stderr, "INFO: Print file accepted - job ID %d.\n", job_id); | |
687 | } | |
688 | ||
689 | if (response) | |
690 | ippDelete(response); | |
691 | ||
692 | if (ipp_status <= IPP_OK_CONFLICT && argc > 6) | |
693 | { | |
694 | fprintf(stderr, "PAGE: 1 %d\n", copies_sup ? atoi(argv[4]) : 1); | |
695 | copies --; | |
696 | } | |
697 | else if (ipp_status != IPP_SERVICE_UNAVAILABLE && | |
698 | ipp_status != IPP_PRINTER_BUSY) | |
699 | break; | |
700 | ||
701 | /* | |
702 | * Wait for the job to complete... | |
703 | */ | |
704 | ||
705 | if (!job_id) | |
706 | continue; | |
707 | ||
708 | fputs("INFO: Waiting for job to complete...\n", stderr); | |
709 | ||
710 | for (;;) | |
711 | { | |
712 | /* | |
713 | * Build an IPP_GET_JOB_ATTRIBUTES request... | |
714 | */ | |
715 | ||
716 | request = ippNew(); | |
717 | request->request.op.version[1] = version; | |
718 | request->request.op.operation_id = IPP_GET_JOB_ATTRIBUTES; | |
719 | request->request.op.request_id = 1; | |
720 | ||
721 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
722 | "attributes-charset", NULL, charset); | |
723 | ||
724 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
725 | "attributes-natural-language", NULL, | |
726 | language != NULL ? language->language : "en"); | |
727 | ||
728 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
729 | NULL, uri); | |
730 | ||
731 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", | |
732 | job_id); | |
733 | ||
734 | if (argv[2][0]) | |
735 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", | |
736 | NULL, argv[2]); | |
737 | ||
738 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
739 | "requested-attributes", NULL, "job-state"); | |
740 | ||
741 | /* | |
742 | * Do the request... | |
743 | */ | |
744 | ||
745 | if (!copies_sup) | |
746 | httpReconnect(http); | |
747 | ||
748 | if ((response = cupsDoRequest(http, request, resource)) == NULL) | |
749 | ipp_status = cupsLastError(); | |
750 | else | |
751 | ipp_status = response->request.status.status_code; | |
752 | ||
753 | if (ipp_status == IPP_NOT_FOUND) | |
754 | { | |
755 | /* | |
756 | * Job has gone away and/or the server has no job history... | |
757 | */ | |
758 | ||
759 | ippDelete(response); | |
760 | ||
761 | ipp_status = IPP_OK; | |
762 | break; | |
763 | } | |
764 | ||
765 | if (ipp_status > IPP_OK_CONFLICT) | |
766 | { | |
767 | if (ipp_status != IPP_SERVICE_UNAVAILABLE && | |
768 | ipp_status != IPP_PRINTER_BUSY) | |
769 | { | |
770 | if (response) | |
771 | ippDelete(response); | |
772 | ||
773 | fprintf(stderr, "ERROR: Unable to get job %d attributes (%s)!\n", | |
774 | job_id, ippErrorString(ipp_status)); | |
775 | break; | |
776 | } | |
777 | } | |
778 | else if ((job_state = ippFindAttribute(response, "job-state", IPP_TAG_ENUM)) != NULL) | |
779 | { | |
780 | /* | |
781 | * Stop polling if the job is finished or pending-held... | |
782 | */ | |
783 | ||
784 | if (job_state->values[0].integer > IPP_JOB_PROCESSING || | |
785 | job_state->values[0].integer == IPP_JOB_HELD) | |
786 | { | |
787 | ippDelete(response); | |
788 | break; | |
789 | } | |
790 | } | |
791 | ||
792 | if (response) | |
793 | ippDelete(response); | |
794 | ||
795 | /* | |
796 | * Now check on the printer state... | |
797 | */ | |
798 | ||
799 | request = ippNew(); | |
800 | request->request.op.version[1] = version; | |
801 | request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES; | |
802 | request->request.op.request_id = 1; | |
803 | ||
804 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
805 | "attributes-charset", NULL, charset); | |
806 | ||
807 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
808 | "attributes-natural-language", NULL, | |
809 | language != NULL ? language->language : "en"); | |
810 | ||
811 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
812 | NULL, uri); | |
813 | ||
814 | if (argv[2][0]) | |
815 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", | |
816 | NULL, argv[2]); | |
817 | ||
818 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
819 | "requested-attributes", NULL, "printer-state-reasons"); | |
820 | ||
821 | /* | |
822 | * Do the request... | |
823 | */ | |
824 | ||
825 | if (!copies_sup) | |
826 | httpReconnect(http); | |
827 | ||
828 | if ((response = cupsDoRequest(http, request, resource)) != NULL) | |
829 | { | |
830 | reasons = report_printer_state(response); | |
831 | ippDelete(response); | |
832 | } | |
833 | ||
834 | /* | |
835 | * Wait 10 seconds before polling again... | |
836 | */ | |
837 | ||
838 | sleep(10); | |
839 | } | |
840 | } | |
841 | ||
842 | /* | |
843 | * Free memory... | |
844 | */ | |
845 | ||
846 | httpClose(http); | |
847 | ||
848 | if (supported) | |
849 | ippDelete(supported); | |
850 | ||
851 | /* | |
852 | * Close and remove the temporary file if necessary... | |
853 | */ | |
854 | ||
855 | if (argc == 6 || strcmp(filename, argv[6])) | |
856 | unlink(filename); | |
857 | ||
858 | /* | |
859 | * Return the queue status... | |
860 | */ | |
861 | ||
862 | if (ipp_status <= IPP_OK_CONFLICT && reasons == 0) | |
863 | fputs("INFO: Ready to print.\n", stderr); | |
864 | ||
865 | return (ipp_status > IPP_OK_CONFLICT); | |
866 | } | |
867 | ||
868 | ||
869 | /* | |
870 | * 'password_cb()' - Disable the password prompt for cupsDoFileRequest(). | |
871 | */ | |
872 | ||
873 | const char * /* O - Password */ | |
874 | password_cb(const char *prompt) /* I - Prompt (not used) */ | |
875 | { | |
876 | (void)prompt; | |
877 | ||
878 | return (password); | |
879 | } | |
880 | ||
881 | ||
882 | /* | |
883 | * 'report_printer_state()' - Report the printer state. | |
884 | */ | |
885 | ||
886 | int /* O - Number of reasons shown */ | |
887 | report_printer_state(ipp_t *ipp) /* I - IPP response */ | |
888 | { | |
889 | int i; /* Looping var */ | |
890 | int count; /* Count of reasons shown... */ | |
891 | ipp_attribute_t *reasons; /* printer-state-reasons */ | |
892 | const char *message; /* Message to show */ | |
893 | char unknown[1024]; /* Unknown message string */ | |
894 | ||
895 | ||
896 | if ((reasons = ippFindAttribute(ipp, "printer-state-reasons", | |
897 | IPP_TAG_KEYWORD)) == NULL) | |
898 | return (0); | |
899 | ||
900 | for (i = 0, count = 0; i < reasons->num_values; i ++) | |
901 | { | |
902 | message = NULL; | |
903 | ||
904 | if (strncmp(reasons->values[i].string.text, "media-needed", 12) == 0) | |
905 | message = "Media tray needs to be filled."; | |
906 | else if (strncmp(reasons->values[i].string.text, "media-jam", 9) == 0) | |
907 | message = "Media jam!"; | |
908 | else if (strncmp(reasons->values[i].string.text, "moving-to-paused", 16) == 0 || | |
909 | strncmp(reasons->values[i].string.text, "paused", 6) == 0 || | |
910 | strncmp(reasons->values[i].string.text, "shutdown", 8) == 0) | |
911 | message = "Printer off-line."; | |
912 | else if (strncmp(reasons->values[i].string.text, "toner-low", 9) == 0) | |
913 | message = "Toner low."; | |
914 | else if (strncmp(reasons->values[i].string.text, "toner-empty", 11) == 0) | |
915 | message = "Out of toner!"; | |
916 | else if (strncmp(reasons->values[i].string.text, "cover-open", 10) == 0) | |
917 | message = "Cover open."; | |
918 | else if (strncmp(reasons->values[i].string.text, "interlock-open", 14) == 0) | |
919 | message = "Interlock open."; | |
920 | else if (strncmp(reasons->values[i].string.text, "door-open", 9) == 0) | |
921 | message = "Door open."; | |
922 | else if (strncmp(reasons->values[i].string.text, "input-tray-missing", 18) == 0) | |
923 | message = "Media tray missing!"; | |
924 | else if (strncmp(reasons->values[i].string.text, "media-low", 9) == 0) | |
925 | message = "Media tray almost empty."; | |
926 | else if (strncmp(reasons->values[i].string.text, "media-empty", 11) == 0) | |
927 | message = "Media tray empty!"; | |
928 | else if (strncmp(reasons->values[i].string.text, "output-tray-missing", 19) == 0) | |
929 | message = "Output tray missing!"; | |
930 | else if (strncmp(reasons->values[i].string.text, "output-area-almost-full", 23) == 0) | |
931 | message = "Output bin almost full."; | |
932 | else if (strncmp(reasons->values[i].string.text, "output-area-full", 16) == 0) | |
933 | message = "Output bin full!"; | |
934 | else if (strncmp(reasons->values[i].string.text, "marker-supply-low", 17) == 0) | |
935 | message = "Ink/toner almost empty."; | |
936 | else if (strncmp(reasons->values[i].string.text, "marker-supply-empty", 19) == 0) | |
937 | message = "Ink/toner empty!"; | |
938 | else if (strncmp(reasons->values[i].string.text, "marker-waste-almost-full", 24) == 0) | |
939 | message = "Ink/toner waste bin almost full."; | |
940 | else if (strncmp(reasons->values[i].string.text, "marker-waste-full", 17) == 0) | |
941 | message = "Ink/toner waste bin full!"; | |
942 | else if (strncmp(reasons->values[i].string.text, "fuser-over-temp", 15) == 0) | |
943 | message = "Fuser temperature high!"; | |
944 | else if (strncmp(reasons->values[i].string.text, "fuser-under-temp", 16) == 0) | |
945 | message = "Fuser temperature low!"; | |
946 | else if (strncmp(reasons->values[i].string.text, "opc-near-eol", 12) == 0) | |
947 | message = "OPC almost at end-of-life."; | |
948 | else if (strncmp(reasons->values[i].string.text, "opc-life-over", 13) == 0) | |
949 | message = "OPC at end-of-life!"; | |
950 | else if (strncmp(reasons->values[i].string.text, "developer-low", 13) == 0) | |
951 | message = "Developer almost empty."; | |
952 | else if (strncmp(reasons->values[i].string.text, "developer-empty", 15) == 0) | |
953 | message = "Developer empty!"; | |
954 | else if (strstr(reasons->values[i].string.text, "error") != NULL) | |
955 | { | |
956 | message = unknown; | |
957 | ||
958 | snprintf(unknown, sizeof(unknown), "Unknown printer error (%s)!", | |
959 | reasons->values[i].string.text); | |
960 | } | |
961 | ||
962 | if (message) | |
963 | { | |
964 | count ++; | |
965 | if (strstr(reasons->values[i].string.text, "error")) | |
966 | fprintf(stderr, "ERROR: %s\n", message); | |
967 | else if (strstr(reasons->values[i].string.text, "warning")) | |
968 | fprintf(stderr, "WARNING: %s\n", message); | |
969 | else | |
970 | fprintf(stderr, "INFO: %s\n", message); | |
971 | } | |
972 | } | |
973 | ||
974 | return (count); | |
975 | } | |
976 | ||
977 | ||
978 | #ifdef __APPLE__ | |
979 | /* | |
980 | * 'run_pictwps_filter()' - Convert PICT files to PostScript when printing | |
981 | * remotely. | |
982 | * | |
983 | * This step is required because the PICT format is not documented and | |
984 | * subject to change, so developing a filter for other OS's is infeasible. | |
985 | * Also, fonts required by the PICT file need to be embedded on the | |
986 | * client side (which has the fonts), so we run the filter to get a | |
987 | * PostScript file for printing... | |
988 | */ | |
989 | ||
990 | int /* O - Exit status of filter */ | |
991 | run_pictwps_filter(char **argv, /* I - Command-line arguments */ | |
992 | char *filename, /* I - Filename buffer */ | |
993 | int length) /* I - Size of filename buffer */ | |
994 | { | |
995 | struct stat fileinfo; /* Print file information */ | |
996 | const char *ppdfile; /* PPD file for destination printer */ | |
997 | int pid; /* Child process ID */ | |
998 | int fd; /* Temporary file descriptor */ | |
999 | int status; /* Exit status of filter */ | |
1000 | const char *printer; /* PRINTER env var */ | |
1001 | static char ppdenv[1024]; /* PPD environment variable */ | |
1002 | ||
1003 | ||
1004 | /* | |
1005 | * First get the PPD file for the printer... | |
1006 | */ | |
1007 | ||
1008 | printer = getenv("PRINTER"); | |
1009 | if (!printer) | |
1010 | { | |
1011 | fputs("ERROR: PRINTER environment variable not defined!\n", stderr); | |
1012 | return (-1); | |
1013 | } | |
1014 | ||
1015 | if ((ppdfile = cupsGetPPD(printer)) == NULL) | |
1016 | { | |
1017 | fprintf(stderr, "ERROR: Unable to get PPD file for printer \"%s\" - %s.\n", | |
1018 | printer, ippErrorString(cupsLastError())); | |
1019 | /*return (-1);*/ | |
1020 | } | |
1021 | else | |
1022 | { | |
1023 | snprintf(ppdenv, sizeof(ppdenv), "PPD=%s", ppdfile); | |
1024 | putenv(ppdenv); | |
1025 | } | |
1026 | ||
1027 | /* | |
1028 | * Then create a temporary file for printing... | |
1029 | */ | |
1030 | ||
1031 | if ((fd = cupsTempFd(filename, length)) < 0) | |
1032 | { | |
1033 | fprintf(stderr, "ERROR: Unable to create temporary file - %s.\n", | |
1034 | strerror(errno)); | |
1035 | if (ppdfile) | |
1036 | unlink(ppdfile); | |
1037 | return (-1); | |
1038 | } | |
1039 | ||
1040 | /* | |
1041 | * Get the owner of the spool file - it is owned by the user we want to run | |
1042 | * as... | |
1043 | */ | |
1044 | ||
1045 | stat(argv[6], &fileinfo); | |
1046 | ||
1047 | if (ppdfile) | |
1048 | chown(ppdfile, fileinfo.st_uid, fileinfo.st_gid); | |
1049 | ||
1050 | fchown(fd, fileinfo.st_uid, fileinfo.st_gid); | |
1051 | ||
1052 | /* | |
1053 | * Finally, run the filter to convert the file... | |
1054 | */ | |
1055 | ||
1056 | if ((pid = fork()) == 0) | |
1057 | { | |
1058 | /* | |
1059 | * Child process for pictwpstops... Redirect output of pictwpstops to a | |
1060 | * file... | |
1061 | */ | |
1062 | ||
1063 | close(1); | |
1064 | dup(fd); | |
1065 | close(fd); | |
1066 | ||
1067 | if (!getuid()) | |
1068 | { | |
1069 | /* | |
1070 | * Change to an unpriviledged user... | |
1071 | */ | |
1072 | ||
1073 | setgid(fileinfo.st_gid); | |
1074 | setuid(fileinfo.st_uid); | |
1075 | } | |
1076 | ||
1077 | execlp("pictwpstops", printer, argv[1], argv[2], argv[3], argv[4], argv[5], | |
1078 | argv[6], NULL); | |
1079 | perror("ERROR: Unable to exec pictwpstops"); | |
1080 | return (errno); | |
1081 | } | |
1082 | ||
1083 | close(fd); | |
1084 | ||
1085 | if (pid < 0) | |
1086 | { | |
1087 | /* | |
1088 | * Error! | |
1089 | */ | |
1090 | ||
1091 | perror("ERROR: Unable to fork pictwpstops"); | |
1092 | unlink(filename); | |
1093 | if (ppdfile) | |
1094 | unlink(ppdfile); | |
1095 | return (-1); | |
1096 | } | |
1097 | ||
1098 | /* | |
1099 | * Now wait for the filter to complete... | |
1100 | */ | |
1101 | ||
1102 | if (wait(&status) < 0) | |
1103 | { | |
1104 | perror("ERROR: Unable to wait for pictwpstops"); | |
1105 | close(fd); | |
1106 | unlink(filename); | |
1107 | if (ppdfile) | |
1108 | unlink(ppdfile); | |
1109 | return (-1); | |
1110 | } | |
1111 | ||
1112 | if (ppdfile) | |
1113 | unlink(ppdfile); | |
1114 | ||
1115 | close(fd); | |
1116 | ||
1117 | if (status) | |
1118 | { | |
1119 | if (status >= 256) | |
1120 | fprintf(stderr, "ERROR: pictwpstops exited with status %d!\n", | |
1121 | status / 256); | |
1122 | else | |
1123 | fprintf(stderr, "ERROR: pictwpstops exited on signal %d!\n", | |
1124 | status); | |
1125 | ||
1126 | unlink(filename); | |
1127 | return (status); | |
1128 | } | |
1129 | ||
1130 | /* | |
1131 | * Return with no errors.. | |
1132 | */ | |
1133 | ||
1134 | return (0); | |
1135 | } | |
1136 | #endif /* __APPLE__ */ | |
1137 | ||
1138 | ||
1139 | /* | |
1140 | * End of "$Id: ipp.c,v 1.38.2.23 2003/02/07 12:05:00 mike Exp $". | |
1141 | */ |