]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
2 | * "$Id: ipp.c 4906 2006-01-10 20:53:28Z mike $" | |
3 | * | |
4 | * IPP backend for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 1997-2006 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 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
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 | * check_printer_state() - Check the printer state... | |
30 | * password_cb() - Disable the password prompt for | |
31 | * cupsDoFileRequest(). | |
32 | * report_printer_state() - Report the printer state. | |
33 | * run_pictwps_filter() - Convert PICT files to PostScript when printing | |
34 | * remotely. | |
35 | * sigterm_handler() - Handle 'terminate' signals that stop the backend. | |
36 | */ | |
37 | ||
38 | /* | |
39 | * Include necessary headers. | |
40 | */ | |
41 | ||
42 | #include <cups/http-private.h> | |
43 | #include <stdio.h> | |
44 | #include <stdlib.h> | |
45 | #include <errno.h> | |
46 | #include <sys/types.h> | |
47 | #include <sys/stat.h> | |
48 | #include <cups/backend.h> | |
49 | #include <cups/cups.h> | |
50 | #include <cups/language.h> | |
51 | #include <cups/string.h> | |
52 | #include <signal.h> | |
53 | #include <sys/wait.h> | |
54 | ||
55 | ||
56 | /* | |
57 | * Globals... | |
58 | */ | |
59 | ||
60 | static char *password = NULL; /* Password for device URI */ | |
61 | #ifdef __APPLE__ | |
62 | static char pstmpname[1024] = ""; /* Temporary PostScript file name */ | |
63 | #endif /* __APPLE__ */ | |
64 | static char tmpfilename[1024] = ""; /* Temporary spool file name */ | |
65 | ||
66 | ||
67 | /* | |
68 | * Local functions... | |
69 | */ | |
70 | ||
71 | void check_printer_state(http_t *http, cups_lang_t *language, | |
72 | const char *charset, const char *uri, /* I - Printer URI */ | |
73 | const char *resource, const char *user, | |
74 | int version); | |
75 | const char *password_cb(const char *); | |
76 | int report_printer_state(ipp_t *ipp); | |
77 | ||
78 | #ifdef __APPLE__ | |
79 | int run_pictwps_filter(char **argv, const char *filename); | |
80 | #endif /* __APPLE__ */ | |
81 | static void sigterm_handler(int sig); | |
82 | ||
83 | ||
84 | /* | |
85 | * 'main()' - Send a file to the printer or server. | |
86 | * | |
87 | * Usage: | |
88 | * | |
89 | * printer-uri job-id user title copies options [file] | |
90 | */ | |
91 | ||
92 | int /* O - Exit status */ | |
93 | main(int argc, /* I - Number of command-line arguments (6 or 7) */ | |
94 | char *argv[]) /* I - Command-line arguments */ | |
95 | { | |
96 | int i; /* Looping var */ | |
97 | int num_options; /* Number of printer options */ | |
98 | cups_option_t *options; /* Printer options */ | |
99 | char method[255], /* Method in URI */ | |
100 | hostname[1024], /* Hostname */ | |
101 | username[255], /* Username info */ | |
102 | resource[1024], /* Resource info (printer name) */ | |
103 | *optptr, /* Pointer to URI options */ | |
104 | name[255], /* Name of option */ | |
105 | value[255], /* Value of option */ | |
106 | *ptr; /* Pointer into name or value */ | |
107 | char *filename; /* File to print */ | |
108 | int port; /* Port number (not used) */ | |
109 | char uri[HTTP_MAX_URI]; /* Updated URI without user/pass */ | |
110 | ipp_status_t ipp_status; /* Status of IPP request */ | |
111 | http_t *http; /* HTTP connection */ | |
112 | ipp_t *request, /* IPP request */ | |
113 | *response, /* IPP response */ | |
114 | *supported; /* get-printer-attributes response */ | |
115 | int waitjob, /* Wait for job complete? */ | |
116 | waitprinter; /* Wait for printer ready? */ | |
117 | ipp_attribute_t *job_id_attr; /* job-id attribute */ | |
118 | int job_id; /* job-id value */ | |
119 | ipp_attribute_t *job_sheets; /* job-media-sheets-completed attribute */ | |
120 | ipp_attribute_t *job_state; /* job-state attribute */ | |
121 | ipp_attribute_t *copies_sup; /* copies-supported attribute */ | |
122 | ipp_attribute_t *charset_sup; /* charset-supported attribute */ | |
123 | ipp_attribute_t *format_sup; /* document-format-supported attribute */ | |
124 | ipp_attribute_t *printer_state; /* printer-state attribute */ | |
125 | ipp_attribute_t *printer_accepting; /* printer-is-accepting-jobs attribute */ | |
126 | const char *charset; /* Character set to use */ | |
127 | cups_lang_t *language; /* Default language */ | |
128 | int copies; /* Number of copies remaining */ | |
129 | const char *content_type; /* CONTENT_TYPE environment variable */ | |
130 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) | |
131 | struct sigaction action; /* Actions for POSIX signals */ | |
132 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ | |
133 | int version; /* IPP version */ | |
134 | int reasons; /* Number of printer-state-reasons shown */ | |
135 | static const char * const pattrs[] = | |
136 | { /* Printer attributes we want */ | |
137 | "copies-supported", | |
138 | "charset-supported", | |
139 | "document-format-supported", | |
140 | "printer-is-accepting-jobs", | |
141 | "printer-state", | |
142 | "printer-state-reasons", | |
143 | }; | |
144 | static const char * const jattrs[] = | |
145 | { /* Job attributes we want */ | |
146 | "job-media-sheets-completed", | |
147 | "job-state" | |
148 | }; | |
149 | ||
150 | ||
151 | /* | |
152 | * Make sure status messages are not buffered... | |
153 | */ | |
154 | ||
155 | setbuf(stderr, NULL); | |
156 | ||
157 | /* | |
158 | * Ignore SIGPIPE and catch SIGTERM signals... | |
159 | */ | |
160 | ||
161 | #ifdef HAVE_SIGSET | |
162 | sigset(SIGPIPE, SIG_IGN); | |
163 | sigset(SIGTERM, sigterm_handler); | |
164 | #elif defined(HAVE_SIGACTION) | |
165 | memset(&action, 0, sizeof(action)); | |
166 | action.sa_handler = SIG_IGN; | |
167 | sigaction(SIGPIPE, &action, NULL); | |
168 | ||
169 | sigemptyset(&action.sa_mask); | |
170 | sigaddset(&action.sa_mask, SIGTERM); | |
171 | action.sa_handler = sigterm_handler; | |
172 | sigaction(SIGTERM, &action, NULL); | |
173 | #else | |
174 | signal(SIGPIPE, SIG_IGN); | |
175 | signal(SIGTERM, sigterm_handler); | |
176 | #endif /* HAVE_SIGSET */ | |
177 | ||
178 | /* | |
179 | * Check command-line... | |
180 | */ | |
181 | ||
182 | if (argc == 1) | |
183 | { | |
184 | char *s; | |
185 | ||
186 | if ((s = strrchr(argv[0], '/')) != NULL) | |
187 | s ++; | |
188 | else | |
189 | s = argv[0]; | |
190 | ||
191 | printf("network %s \"Unknown\" \"Internet Printing Protocol (%s)\"\n", s, s); | |
192 | return (CUPS_BACKEND_OK); | |
193 | } | |
194 | else if (argc < 6 || argc > 7) | |
195 | { | |
196 | fprintf(stderr, "Usage: %s job-id user title copies options [file]\n", | |
197 | argv[0]); | |
198 | return (CUPS_BACKEND_STOP); | |
199 | } | |
200 | ||
201 | /* | |
202 | * Get the content type... | |
203 | */ | |
204 | ||
205 | if (argc > 6) | |
206 | content_type = getenv("CONTENT_TYPE"); | |
207 | else | |
208 | content_type = "application/vnd.cups-raw"; | |
209 | ||
210 | if (content_type == NULL) | |
211 | content_type = "application/octet-stream"; | |
212 | ||
213 | /* | |
214 | * Extract the hostname and printer name from the URI... | |
215 | */ | |
216 | ||
217 | if (getenv("DEVICE_URI") != NULL) | |
218 | /* authentication information is only available in the env var */ | |
219 | httpSeparateURI(getenv("DEVICE_URI"), method, sizeof(method), | |
220 | username, sizeof(username), | |
221 | hostname, sizeof(hostname), &port, | |
222 | resource, sizeof(resource)); | |
223 | else if (strchr(argv[0], ':') != NULL) | |
224 | httpSeparateURI(argv[0], method, sizeof(method), username, sizeof(username), | |
225 | hostname, sizeof(hostname), &port, | |
226 | resource, sizeof(resource)); | |
227 | else | |
228 | { | |
229 | fputs("ERROR: Missing device URI on command-line and no DEVICE_URI environment variable!\n", | |
230 | stderr); | |
231 | return (CUPS_BACKEND_STOP); | |
232 | } | |
233 | ||
234 | if (!strcmp(method, "https")) | |
235 | cupsSetEncryption(HTTP_ENCRYPT_ALWAYS); | |
236 | ||
237 | /* | |
238 | * If we have 7 arguments, print the file named on the command-line. | |
239 | * Otherwise, copy stdin to a temporary file and print the temporary | |
240 | * file. | |
241 | */ | |
242 | ||
243 | if (argc == 6) | |
244 | { | |
245 | /* | |
246 | * Copy stdin to a temporary file... | |
247 | */ | |
248 | ||
249 | int fd; /* Temporary file */ | |
250 | char buffer[8192]; /* Buffer for copying */ | |
251 | int bytes; /* Number of bytes read */ | |
252 | ||
253 | ||
254 | if ((fd = cupsTempFd(tmpfilename, sizeof(tmpfilename))) < 0) | |
255 | { | |
256 | perror("ERROR: unable to create temporary file"); | |
257 | return (CUPS_BACKEND_FAILED); | |
258 | } | |
259 | ||
260 | while ((bytes = fread(buffer, 1, sizeof(buffer), stdin)) > 0) | |
261 | if (write(fd, buffer, bytes) < bytes) | |
262 | { | |
263 | perror("ERROR: unable to write to temporary file"); | |
264 | close(fd); | |
265 | unlink(tmpfilename); | |
266 | return (CUPS_BACKEND_FAILED); | |
267 | } | |
268 | ||
269 | close(fd); | |
270 | filename = tmpfilename; | |
271 | } | |
272 | else | |
273 | filename = argv[6]; | |
274 | ||
275 | /* | |
276 | * See if there are any options... | |
277 | */ | |
278 | ||
279 | version = 1; | |
280 | waitjob = 1; | |
281 | waitprinter = 1; | |
282 | ||
283 | if ((optptr = strchr(resource, '?')) != NULL) | |
284 | { | |
285 | /* | |
286 | * Yup, terminate the device name string and move to the first | |
287 | * character of the optptr... | |
288 | */ | |
289 | ||
290 | *optptr++ = '\0'; | |
291 | ||
292 | /* | |
293 | * Then parse the optptr... | |
294 | */ | |
295 | ||
296 | while (*optptr) | |
297 | { | |
298 | /* | |
299 | * Get the name... | |
300 | */ | |
301 | ||
302 | for (ptr = name; *optptr && *optptr != '=';) | |
303 | if (ptr < (name + sizeof(name) - 1)) | |
304 | *ptr++ = *optptr++; | |
305 | *ptr = '\0'; | |
306 | ||
307 | if (*optptr == '=') | |
308 | { | |
309 | /* | |
310 | * Get the value... | |
311 | */ | |
312 | ||
313 | optptr ++; | |
314 | ||
315 | for (ptr = value; *optptr && *optptr != '+' && *optptr != '&';) | |
316 | if (ptr < (value + sizeof(value) - 1)) | |
317 | *ptr++ = *optptr++; | |
318 | *ptr = '\0'; | |
319 | ||
320 | if (*optptr == '+') | |
321 | optptr ++; | |
322 | } | |
323 | else | |
324 | value[0] = '\0'; | |
325 | ||
326 | /* | |
327 | * Process the option... | |
328 | */ | |
329 | ||
330 | if (!strcasecmp(name, "waitjob")) | |
331 | { | |
332 | /* | |
333 | * Wait for job completion? | |
334 | */ | |
335 | ||
336 | waitjob = !strcasecmp(value, "on") || | |
337 | !strcasecmp(value, "yes") || | |
338 | !strcasecmp(value, "true"); | |
339 | } | |
340 | else if (!strcasecmp(name, "waitprinter")) | |
341 | { | |
342 | /* | |
343 | * Wait for printer idle? | |
344 | */ | |
345 | ||
346 | waitprinter = !strcasecmp(value, "on") || | |
347 | !strcasecmp(value, "yes") || | |
348 | !strcasecmp(value, "true"); | |
349 | } | |
350 | else if (!strcasecmp(name, "encryption")) | |
351 | { | |
352 | /* | |
353 | * Enable/disable encryption? | |
354 | */ | |
355 | ||
356 | if (!strcasecmp(value, "always")) | |
357 | cupsSetEncryption(HTTP_ENCRYPT_ALWAYS); | |
358 | else if (!strcasecmp(value, "required")) | |
359 | cupsSetEncryption(HTTP_ENCRYPT_REQUIRED); | |
360 | else if (!strcasecmp(value, "never")) | |
361 | cupsSetEncryption(HTTP_ENCRYPT_NEVER); | |
362 | else if (!strcasecmp(value, "ifrequested")) | |
363 | cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED); | |
364 | else | |
365 | { | |
366 | fprintf(stderr, "ERROR: Unknown encryption option value \"%s\"!\n", | |
367 | value); | |
368 | } | |
369 | } | |
370 | else if (!strcasecmp(name, "version")) | |
371 | { | |
372 | if (!strcmp(value, "1.0")) | |
373 | version = 0; | |
374 | else if (!strcmp(value, "1.1")) | |
375 | version = 1; | |
376 | else | |
377 | { | |
378 | fprintf(stderr, "ERROR: Unknown version option value \"%s\"!\n", | |
379 | value); | |
380 | } | |
381 | } | |
382 | else | |
383 | { | |
384 | /* | |
385 | * Unknown option... | |
386 | */ | |
387 | ||
388 | fprintf(stderr, "ERROR: Unknown option \"%s\" with value \"%s\"!\n", | |
389 | name, value); | |
390 | } | |
391 | } | |
392 | } | |
393 | ||
394 | /* | |
395 | * Set the authentication info, if any... | |
396 | */ | |
397 | ||
398 | cupsSetPasswordCB(password_cb); | |
399 | ||
400 | if (username[0]) | |
401 | { | |
402 | /* | |
403 | * Use authenticaion information in the device URI... | |
404 | */ | |
405 | ||
406 | if ((password = strchr(username, ':')) != NULL) | |
407 | *password++ = '\0'; | |
408 | ||
409 | cupsSetUser(username); | |
410 | } | |
411 | else if (!getuid()) | |
412 | { | |
413 | /* | |
414 | * Try loading authentication information from the a##### file. | |
415 | */ | |
416 | ||
417 | const char *request_root; /* CUPS_REQUESTROOT env var */ | |
418 | char afilename[1024], /* a##### filename */ | |
419 | aline[1024]; /* Line from file */ | |
420 | FILE *fp; /* File pointer */ | |
421 | ||
422 | ||
423 | if ((request_root = getenv("CUPS_REQUESTROOT")) != NULL) | |
424 | { | |
425 | /* | |
426 | * Try opening authentication cache file... | |
427 | */ | |
428 | ||
429 | snprintf(afilename, sizeof(afilename), "%s/a%05d", request_root, | |
430 | atoi(argv[1])); | |
431 | if ((fp = fopen(afilename, "r")) != NULL) | |
432 | { | |
433 | /* | |
434 | * Read username... | |
435 | */ | |
436 | ||
437 | if (fgets(aline, sizeof(aline), fp)) | |
438 | { | |
439 | /* | |
440 | * Decode username... | |
441 | */ | |
442 | ||
443 | i = sizeof(username); | |
444 | httpDecode64_2(username, &i, aline); | |
445 | ||
446 | /* | |
447 | * Read password... | |
448 | */ | |
449 | ||
450 | if (fgets(aline, sizeof(aline), fp)) | |
451 | { | |
452 | /* | |
453 | * Decode password... | |
454 | */ | |
455 | ||
456 | i = sizeof(password); | |
457 | httpDecode64_2(password, &i, aline); | |
458 | } | |
459 | } | |
460 | ||
461 | /* | |
462 | * Close the file... | |
463 | */ | |
464 | ||
465 | fclose(fp); | |
466 | } | |
467 | } | |
468 | } | |
469 | ||
470 | /* | |
471 | * Try connecting to the remote server... | |
472 | */ | |
473 | ||
474 | do | |
475 | { | |
476 | fprintf(stderr, "INFO: Connecting to %s on port %d...\n", hostname, port); | |
477 | ||
478 | if ((http = httpConnectEncrypt(hostname, port, cupsEncryption())) == NULL) | |
479 | { | |
480 | if (getenv("CLASS") != NULL) | |
481 | { | |
482 | /* | |
483 | * If the CLASS environment variable is set, the job was submitted | |
484 | * to a class and not to a specific queue. In this case, we want | |
485 | * to abort immediately so that the job can be requeued on the next | |
486 | * available printer in the class. | |
487 | */ | |
488 | ||
489 | fprintf(stderr, "INFO: Unable to connect to %s, queuing on next printer in class...\n", | |
490 | hostname); | |
491 | ||
492 | if (argc == 6 || strcmp(filename, argv[6])) | |
493 | unlink(filename); | |
494 | ||
495 | /* | |
496 | * Sleep 5 seconds to keep the job from requeuing too rapidly... | |
497 | */ | |
498 | ||
499 | sleep(5); | |
500 | ||
501 | return (CUPS_BACKEND_FAILED); | |
502 | } | |
503 | ||
504 | if (errno == ECONNREFUSED || errno == EHOSTDOWN || | |
505 | errno == EHOSTUNREACH) | |
506 | { | |
507 | fprintf(stderr, "INFO: Network host \'%s\' is busy; will retry in 30 seconds...\n", | |
508 | hostname); | |
509 | sleep(30); | |
510 | } | |
511 | else if (h_errno) | |
512 | { | |
513 | fprintf(stderr, "INFO: Unable to lookup host \'%s\' - %s\n", | |
514 | hostname, hstrerror(h_errno)); | |
515 | sleep(30); | |
516 | } | |
517 | else | |
518 | { | |
519 | perror("ERROR: Unable to connect to IPP host"); | |
520 | sleep(30); | |
521 | } | |
522 | } | |
523 | } | |
524 | while (http == NULL); | |
525 | ||
526 | fprintf(stderr, "INFO: Connected to %s...\n", hostname); | |
527 | ||
528 | /* | |
529 | * Build a URI for the printer and fill the standard IPP attributes for | |
530 | * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it | |
531 | * might contain username:password information... | |
532 | */ | |
533 | ||
534 | snprintf(uri, sizeof(uri), "%s://%s:%d%s", method, hostname, port, resource); | |
535 | ||
536 | /* | |
537 | * First validate the destination and see if the device supports multiple | |
538 | * copies. We have to do this because some IPP servers (e.g. HP JetDirect) | |
539 | * don't support the copies attribute... | |
540 | */ | |
541 | ||
542 | language = cupsLangDefault(); | |
543 | charset_sup = NULL; | |
544 | copies_sup = NULL; | |
545 | format_sup = NULL; | |
546 | supported = NULL; | |
547 | ||
548 | do | |
549 | { | |
550 | /* | |
551 | * Build the IPP request... | |
552 | */ | |
553 | ||
554 | request = ippNew(); | |
555 | request->request.op.version[1] = version; | |
556 | request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES; | |
557 | request->request.op.request_id = 1; | |
558 | ||
559 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
560 | "attributes-charset", NULL, "utf-8"); | |
561 | ||
562 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
563 | "attributes-natural-language", NULL, | |
564 | language != NULL ? language->language : "en"); | |
565 | ||
566 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
567 | NULL, uri); | |
568 | ||
569 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
570 | "requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]), | |
571 | NULL, pattrs); | |
572 | ||
573 | /* | |
574 | * Do the request... | |
575 | */ | |
576 | ||
577 | fputs("DEBUG: Getting supported attributes...\n", stderr); | |
578 | ||
579 | if ((supported = cupsDoRequest(http, request, resource)) == NULL) | |
580 | ipp_status = cupsLastError(); | |
581 | else | |
582 | ipp_status = supported->request.status.status_code; | |
583 | ||
584 | if (ipp_status > IPP_OK_CONFLICT) | |
585 | { | |
586 | if (ipp_status == IPP_PRINTER_BUSY || | |
587 | ipp_status == IPP_SERVICE_UNAVAILABLE) | |
588 | { | |
589 | fputs("INFO: Printer busy; will retry in 10 seconds...\n", stderr); | |
590 | report_printer_state(supported); | |
591 | sleep(10); | |
592 | } | |
593 | else if ((ipp_status == IPP_BAD_REQUEST || | |
594 | ipp_status == IPP_VERSION_NOT_SUPPORTED) && version == 1) | |
595 | { | |
596 | /* | |
597 | * Switch to IPP/1.0... | |
598 | */ | |
599 | ||
600 | fputs("INFO: Printer does not support IPP/1.1, trying IPP/1.0...\n", stderr); | |
601 | version = 0; | |
602 | httpReconnect(http); | |
603 | } | |
604 | else if (ipp_status == IPP_NOT_FOUND) | |
605 | { | |
606 | fputs("ERROR: Destination printer does not exist!\n", stderr); | |
607 | ||
608 | if (supported) | |
609 | ippDelete(supported); | |
610 | ||
611 | return (CUPS_BACKEND_STOP); | |
612 | } | |
613 | else | |
614 | { | |
615 | fprintf(stderr, "ERROR: Unable to get printer status (%s)!\n", | |
616 | ippErrorString(ipp_status)); | |
617 | sleep(10); | |
618 | } | |
619 | ||
620 | if (supported) | |
621 | ippDelete(supported); | |
622 | ||
623 | continue; | |
624 | } | |
625 | else if ((copies_sup = ippFindAttribute(supported, "copies-supported", | |
626 | IPP_TAG_RANGE)) != NULL) | |
627 | { | |
628 | /* | |
629 | * Has the "copies-supported" attribute - does it have an upper | |
630 | * bound > 1? | |
631 | */ | |
632 | ||
633 | if (copies_sup->values[0].range.upper <= 1) | |
634 | copies_sup = NULL; /* No */ | |
635 | } | |
636 | ||
637 | charset_sup = ippFindAttribute(supported, "charset-supported", | |
638 | IPP_TAG_CHARSET); | |
639 | format_sup = ippFindAttribute(supported, "document-format-supported", | |
640 | IPP_TAG_MIMETYPE); | |
641 | ||
642 | if (format_sup) | |
643 | { | |
644 | fprintf(stderr, "DEBUG: document-format-supported (%d values)\n", | |
645 | format_sup->num_values); | |
646 | for (i = 0; i < format_sup->num_values; i ++) | |
647 | fprintf(stderr, "DEBUG: [%d] = \"%s\"\n", i, | |
648 | format_sup->values[i].string.text); | |
649 | } | |
650 | ||
651 | report_printer_state(supported); | |
652 | } | |
653 | while (ipp_status > IPP_OK_CONFLICT); | |
654 | ||
655 | /* | |
656 | * See if the printer is accepting jobs and is not stopped; if either | |
657 | * condition is true and we are printing to a class, requeue the job... | |
658 | */ | |
659 | ||
660 | if (getenv("CLASS") != NULL) | |
661 | { | |
662 | printer_state = ippFindAttribute(supported, "printer-state", | |
663 | IPP_TAG_ENUM); | |
664 | printer_accepting = ippFindAttribute(supported, "printer-is-accepting-jobs", | |
665 | IPP_TAG_BOOLEAN); | |
666 | ||
667 | if (printer_state == NULL || | |
668 | (printer_state->values[0].integer > IPP_PRINTER_PROCESSING && waitprinter) || | |
669 | printer_accepting == NULL || | |
670 | !printer_accepting->values[0].boolean) | |
671 | { | |
672 | /* | |
673 | * If the CLASS environment variable is set, the job was submitted | |
674 | * to a class and not to a specific queue. In this case, we want | |
675 | * to abort immediately so that the job can be requeued on the next | |
676 | * available printer in the class. | |
677 | */ | |
678 | ||
679 | fprintf(stderr, "INFO: Unable to queue job on %s, queuing on next printer in class...\n", | |
680 | hostname); | |
681 | ||
682 | ippDelete(supported); | |
683 | httpClose(http); | |
684 | ||
685 | if (argc == 6 || strcmp(filename, argv[6])) | |
686 | unlink(filename); | |
687 | ||
688 | /* | |
689 | * Sleep 5 seconds to keep the job from requeuing too rapidly... | |
690 | */ | |
691 | ||
692 | sleep(5); | |
693 | ||
694 | return (CUPS_BACKEND_FAILED); | |
695 | } | |
696 | } | |
697 | ||
698 | /* | |
699 | * See if the printer supports multiple copies... | |
700 | */ | |
701 | ||
702 | if (copies_sup || argc < 7) | |
703 | copies = 1; | |
704 | else | |
705 | copies = atoi(argv[4]); | |
706 | ||
707 | /* | |
708 | * Figure out the character set to use... | |
709 | */ | |
710 | ||
711 | charset = language ? cupsLangEncoding(language) : "us-ascii"; | |
712 | ||
713 | if (charset_sup) | |
714 | { | |
715 | /* | |
716 | * See if IPP server supports the requested character set... | |
717 | */ | |
718 | ||
719 | for (i = 0; i < charset_sup->num_values; i ++) | |
720 | if (strcasecmp(charset, charset_sup->values[i].string.text) == 0) | |
721 | break; | |
722 | ||
723 | /* | |
724 | * If not, choose us-ascii or utf-8... | |
725 | */ | |
726 | ||
727 | if (i >= charset_sup->num_values) | |
728 | { | |
729 | /* | |
730 | * See if us-ascii is supported... | |
731 | */ | |
732 | ||
733 | for (i = 0; i < charset_sup->num_values; i ++) | |
734 | if (strcasecmp("us-ascii", charset_sup->values[i].string.text) == 0) | |
735 | break; | |
736 | ||
737 | if (i < charset_sup->num_values) | |
738 | charset = "us-ascii"; | |
739 | else | |
740 | charset = "utf-8"; | |
741 | } | |
742 | } | |
743 | ||
744 | /* | |
745 | * Then issue the print-job request... | |
746 | */ | |
747 | ||
748 | reasons = 0; | |
749 | ||
750 | while (copies > 0) | |
751 | { | |
752 | /* | |
753 | * Build the IPP request... | |
754 | */ | |
755 | ||
756 | request = ippNew(); | |
757 | request->request.op.version[1] = version; | |
758 | request->request.op.operation_id = IPP_PRINT_JOB; | |
759 | request->request.op.request_id = 1; | |
760 | ||
761 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
762 | "attributes-charset", NULL, charset); | |
763 | ||
764 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
765 | "attributes-natural-language", NULL, | |
766 | language != NULL ? language->language : "en"); | |
767 | ||
768 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
769 | NULL, uri); | |
770 | ||
771 | fprintf(stderr, "DEBUG: printer-uri = \"%s\"\n", uri); | |
772 | ||
773 | if (argv[2][0]) | |
774 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
775 | "requesting-user-name", NULL, argv[2]); | |
776 | ||
777 | fprintf(stderr, "DEBUG: requesting-user-name = \"%s\"\n", argv[2]); | |
778 | ||
779 | if (argv[3][0]) | |
780 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL, | |
781 | argv[3]); | |
782 | ||
783 | fprintf(stderr, "DEBUG: job-name = \"%s\"\n", argv[3]); | |
784 | ||
785 | /* | |
786 | * Handle options on the command-line... | |
787 | */ | |
788 | ||
789 | options = NULL; | |
790 | num_options = cupsParseOptions(argv[5], 0, &options); | |
791 | ||
792 | #ifdef __APPLE__ | |
793 | if (content_type != NULL && strcasecmp(content_type, "application/pictwps") == 0) | |
794 | { | |
795 | if (format_sup != NULL) | |
796 | { | |
797 | for (i = 0; i < format_sup->num_values; i ++) | |
798 | if (strcasecmp(content_type, format_sup->values[i].string.text) == 0) | |
799 | break; | |
800 | } | |
801 | ||
802 | if (format_sup == NULL || i >= format_sup->num_values) | |
803 | { | |
804 | /* | |
805 | * Remote doesn't support "application/pictwps" (i.e. it's not MacOS X) | |
806 | * so convert the document to PostScript... | |
807 | */ | |
808 | ||
809 | if (run_pictwps_filter(argv, filename)) | |
810 | return (CUPS_BACKEND_FAILED); | |
811 | ||
812 | filename = pstmpname; | |
813 | ||
814 | /* | |
815 | * Change the MIME type to application/postscript... | |
816 | */ | |
817 | ||
818 | content_type = "application/postscript"; | |
819 | } | |
820 | } | |
821 | #endif /* __APPLE__ */ | |
822 | ||
823 | if (content_type != NULL && format_sup != NULL) | |
824 | { | |
825 | for (i = 0; i < format_sup->num_values; i ++) | |
826 | if (strcasecmp(content_type, format_sup->values[i].string.text) == 0) | |
827 | break; | |
828 | ||
829 | if (i < format_sup->num_values) | |
830 | num_options = cupsAddOption("document-format", content_type, | |
831 | num_options, &options); | |
832 | } | |
833 | ||
834 | if (copies_sup) | |
835 | { | |
836 | /* | |
837 | * Only send options if the destination printer supports the copies | |
838 | * attribute. This is a hack for the HP JetDirect implementation of | |
839 | * IPP, which does not accept extension attributes and incorrectly | |
840 | * reports a client-error-bad-request error instead of the | |
841 | * successful-ok-unsupported-attributes status. In short, at least | |
842 | * some HP implementations of IPP are non-compliant. | |
843 | */ | |
844 | ||
845 | cupsEncodeOptions(request, num_options, options); | |
846 | ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_INTEGER, "copies", | |
847 | atoi(argv[4])); | |
848 | } | |
849 | ||
850 | cupsFreeOptions(num_options, options); | |
851 | ||
852 | /* | |
853 | * If copies aren't supported, then we are likely dealing with an HP | |
854 | * JetDirect. The HP IPP implementation seems to close the connection | |
855 | * after every request (that is, it does *not* implement HTTP Keep- | |
856 | * Alive, which is REQUIRED by HTTP/1.1... | |
857 | */ | |
858 | ||
859 | if (!copies_sup) | |
860 | httpReconnect(http); | |
861 | ||
862 | /* | |
863 | * Do the request... | |
864 | */ | |
865 | ||
866 | if ((response = cupsDoFileRequest(http, request, resource, filename)) == NULL) | |
867 | ipp_status = cupsLastError(); | |
868 | else | |
869 | ipp_status = response->request.status.status_code; | |
870 | ||
871 | if (ipp_status > IPP_OK_CONFLICT) | |
872 | { | |
873 | job_id = 0; | |
874 | ||
875 | if (ipp_status == IPP_SERVICE_UNAVAILABLE || | |
876 | ipp_status == IPP_PRINTER_BUSY) | |
877 | { | |
878 | fputs("INFO: Printer is busy; retrying print job...\n", stderr); | |
879 | sleep(10); | |
880 | } | |
881 | else | |
882 | fprintf(stderr, "ERROR: Print file was not accepted (%s)!\n", | |
883 | ippErrorString(ipp_status)); | |
884 | } | |
885 | else if ((job_id_attr = ippFindAttribute(response, "job-id", | |
886 | IPP_TAG_INTEGER)) == NULL) | |
887 | { | |
888 | fputs("NOTICE: Print file accepted - job ID unknown.\n", stderr); | |
889 | job_id = 0; | |
890 | } | |
891 | else | |
892 | { | |
893 | job_id = job_id_attr->values[0].integer; | |
894 | fprintf(stderr, "NOTICE: Print file accepted - job ID %d.\n", job_id); | |
895 | } | |
896 | ||
897 | if (response) | |
898 | ippDelete(response); | |
899 | ||
900 | if (ipp_status <= IPP_OK_CONFLICT && argc > 6) | |
901 | { | |
902 | fprintf(stderr, "PAGE: 1 %d\n", copies_sup ? atoi(argv[4]) : 1); | |
903 | copies --; | |
904 | } | |
905 | else if (ipp_status == IPP_SERVICE_UNAVAILABLE || | |
906 | ipp_status == IPP_PRINTER_BUSY) | |
907 | break; | |
908 | else | |
909 | copies --; | |
910 | ||
911 | /* | |
912 | * Wait for the job to complete... | |
913 | */ | |
914 | ||
915 | if (!job_id || !waitjob) | |
916 | continue; | |
917 | ||
918 | fputs("INFO: Waiting for job to complete...\n", stderr); | |
919 | ||
920 | for (;;) | |
921 | { | |
922 | /* | |
923 | * Build an IPP_GET_JOB_ATTRIBUTES request... | |
924 | */ | |
925 | ||
926 | request = ippNew(); | |
927 | request->request.op.version[1] = version; | |
928 | request->request.op.operation_id = IPP_GET_JOB_ATTRIBUTES; | |
929 | request->request.op.request_id = 1; | |
930 | ||
931 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
932 | "attributes-charset", NULL, charset); | |
933 | ||
934 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
935 | "attributes-natural-language", NULL, | |
936 | language != NULL ? language->language : "en"); | |
937 | ||
938 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
939 | NULL, uri); | |
940 | ||
941 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", | |
942 | job_id); | |
943 | ||
944 | if (argv[2][0]) | |
945 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
946 | "requesting-user-name", NULL, argv[2]); | |
947 | ||
948 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
949 | "requested-attributes", sizeof(jattrs) / sizeof(jattrs[0]), | |
950 | NULL, jattrs); | |
951 | ||
952 | /* | |
953 | * Do the request... | |
954 | */ | |
955 | ||
956 | if (!copies_sup) | |
957 | httpReconnect(http); | |
958 | ||
959 | if ((response = cupsDoRequest(http, request, resource)) == NULL) | |
960 | ipp_status = cupsLastError(); | |
961 | else | |
962 | ipp_status = response->request.status.status_code; | |
963 | ||
964 | if (ipp_status == IPP_NOT_FOUND) | |
965 | { | |
966 | /* | |
967 | * Job has gone away and/or the server has no job history... | |
968 | */ | |
969 | ||
970 | ippDelete(response); | |
971 | ||
972 | ipp_status = IPP_OK; | |
973 | break; | |
974 | } | |
975 | ||
976 | if (ipp_status > IPP_OK_CONFLICT) | |
977 | { | |
978 | if (ipp_status != IPP_SERVICE_UNAVAILABLE && | |
979 | ipp_status != IPP_PRINTER_BUSY) | |
980 | { | |
981 | if (response) | |
982 | ippDelete(response); | |
983 | ||
984 | fprintf(stderr, "ERROR: Unable to get job %d attributes (%s)!\n", | |
985 | job_id, ippErrorString(ipp_status)); | |
986 | break; | |
987 | } | |
988 | } | |
989 | ||
990 | if (response != NULL) | |
991 | { | |
992 | if ((job_state = ippFindAttribute(response, "job-state", | |
993 | IPP_TAG_ENUM)) != NULL) | |
994 | { | |
995 | /* | |
996 | * Stop polling if the job is finished or pending-held... | |
997 | */ | |
998 | ||
999 | if (job_state->values[0].integer > IPP_JOB_PROCESSING || | |
1000 | job_state->values[0].integer == IPP_JOB_HELD) | |
1001 | { | |
1002 | if ((job_sheets = ippFindAttribute(response, "job-media-sheets-completed", | |
1003 | IPP_TAG_INTEGER)) != NULL) | |
1004 | fprintf(stderr, "PAGE: total %d\n", job_sheets->values[0].integer); | |
1005 | ||
1006 | ippDelete(response); | |
1007 | break; | |
1008 | } | |
1009 | } | |
1010 | } | |
1011 | ||
1012 | if (response) | |
1013 | ippDelete(response); | |
1014 | ||
1015 | /* | |
1016 | * Check the printer state and report it if necessary... | |
1017 | */ | |
1018 | ||
1019 | /* if (!copies_sup) | |
1020 | httpReconnect(http);*/ | |
1021 | ||
1022 | check_printer_state(http, language, charset, uri, resource, argv[2], | |
1023 | version); | |
1024 | ||
1025 | /* | |
1026 | * Wait 10 seconds before polling again... | |
1027 | */ | |
1028 | ||
1029 | sleep(10); | |
1030 | } | |
1031 | } | |
1032 | ||
1033 | /* | |
1034 | * Check the printer state and report it if necessary... | |
1035 | */ | |
1036 | ||
1037 | /* if (!copies_sup) | |
1038 | httpReconnect(http);*/ | |
1039 | ||
1040 | check_printer_state(http, language, charset, uri, resource, argv[2], version); | |
1041 | ||
1042 | /* | |
1043 | * Free memory... | |
1044 | */ | |
1045 | ||
1046 | httpClose(http); | |
1047 | ||
1048 | if (supported) | |
1049 | ippDelete(supported); | |
1050 | ||
1051 | /* | |
1052 | * Remove the temporary file(s) if necessary... | |
1053 | */ | |
1054 | ||
1055 | if (tmpfilename[0]) | |
1056 | unlink(tmpfilename); | |
1057 | ||
1058 | #ifdef __APPLE__ | |
1059 | if (pstmpname[0]) | |
1060 | unlink(pstmpname); | |
1061 | #endif /* __APPLE__ */ | |
1062 | ||
1063 | /* | |
1064 | * Return the queue status... | |
1065 | */ | |
1066 | ||
1067 | return (ipp_status > IPP_OK_CONFLICT ? CUPS_BACKEND_FAILED : CUPS_BACKEND_OK); | |
1068 | } | |
1069 | ||
1070 | ||
1071 | /* | |
1072 | * 'check_printer_state()' - Check the printer state... | |
1073 | */ | |
1074 | ||
1075 | void | |
1076 | check_printer_state(http_t *http, /* I - HTTP connection */ | |
1077 | cups_lang_t *language, | |
1078 | /* I - Language */ | |
1079 | const char *charset, | |
1080 | /* I - Charset */ | |
1081 | const char *uri, /* I - Printer URI */ | |
1082 | const char *resource, | |
1083 | /* I - Resource path */ | |
1084 | const char *user, /* I - Username, if any */ | |
1085 | int version)/* I - IPP version */ | |
1086 | { | |
1087 | ipp_t *request, /* IPP request */ | |
1088 | *response; /* IPP response */ | |
1089 | ||
1090 | ||
1091 | /* | |
1092 | * Check on the printer state... | |
1093 | */ | |
1094 | ||
1095 | request = ippNew(); | |
1096 | request->request.op.version[1] = version; | |
1097 | request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES; | |
1098 | request->request.op.request_id = 1; | |
1099 | ||
1100 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
1101 | "attributes-charset", NULL, charset); | |
1102 | ||
1103 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
1104 | "attributes-natural-language", NULL, | |
1105 | language != NULL ? language->language : "en"); | |
1106 | ||
1107 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
1108 | NULL, uri); | |
1109 | ||
1110 | if (user && user[0]) | |
1111 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
1112 | "requesting-user-name", NULL, user); | |
1113 | ||
1114 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
1115 | "requested-attributes", NULL, "printer-state-reasons"); | |
1116 | ||
1117 | /* | |
1118 | * Do the request... | |
1119 | */ | |
1120 | ||
1121 | if ((response = cupsDoRequest(http, request, resource)) != NULL) | |
1122 | { | |
1123 | report_printer_state(response); | |
1124 | ippDelete(response); | |
1125 | } | |
1126 | } | |
1127 | ||
1128 | ||
1129 | /* | |
1130 | * 'password_cb()' - Disable the password prompt for cupsDoFileRequest(). | |
1131 | */ | |
1132 | ||
1133 | const char * /* O - Password */ | |
1134 | password_cb(const char *prompt) /* I - Prompt (not used) */ | |
1135 | { | |
1136 | (void)prompt; | |
1137 | ||
1138 | if (password) | |
1139 | return (password); | |
1140 | else | |
1141 | { | |
1142 | /* | |
1143 | * If there is no password set in the device URI, return the | |
1144 | * "authentication required" exit code... | |
1145 | */ | |
1146 | ||
1147 | if (tmpfilename[0]) | |
1148 | unlink(tmpfilename); | |
1149 | ||
1150 | #ifdef __APPLE__ | |
1151 | if (pstmpname[0]) | |
1152 | unlink(pstmpname); | |
1153 | #endif /* __APPLE__ */ | |
1154 | ||
1155 | exit(CUPS_BACKEND_AUTH_REQUIRED); | |
1156 | } | |
1157 | } | |
1158 | ||
1159 | ||
1160 | /* | |
1161 | * 'report_printer_state()' - Report the printer state. | |
1162 | */ | |
1163 | ||
1164 | int /* O - Number of reasons shown */ | |
1165 | report_printer_state(ipp_t *ipp) /* I - IPP response */ | |
1166 | { | |
1167 | int i; /* Looping var */ | |
1168 | int count; /* Count of reasons shown... */ | |
1169 | ipp_attribute_t *reasons; /* printer-state-reasons */ | |
1170 | const char *reason; /* Current reason */ | |
1171 | const char *message; /* Message to show */ | |
1172 | char unknown[1024]; /* Unknown message string */ | |
1173 | const char *prefix; /* Prefix for STATE: line */ | |
1174 | char state[1024]; /* State string */ | |
1175 | ||
1176 | ||
1177 | if ((reasons = ippFindAttribute(ipp, "printer-state-reasons", | |
1178 | IPP_TAG_KEYWORD)) == NULL) | |
1179 | return (0); | |
1180 | ||
1181 | state[0] = '\0'; | |
1182 | prefix = "STATE: "; | |
1183 | ||
1184 | for (i = 0, count = 0; i < reasons->num_values; i ++) | |
1185 | { | |
1186 | reason = reasons->values[i].string.text; | |
1187 | ||
1188 | strlcat(state, prefix, sizeof(state)); | |
1189 | strlcat(state, reason, sizeof(state)); | |
1190 | ||
1191 | prefix = ","; | |
1192 | message = NULL; | |
1193 | ||
1194 | if (!strncmp(reason, "media-needed", 12)) | |
1195 | message = "Media tray needs to be filled."; | |
1196 | else if (!strncmp(reason, "media-jam", 9)) | |
1197 | message = "Media jam!"; | |
1198 | else if (!strncmp(reason, "moving-to-paused", 16) || | |
1199 | !strncmp(reason, "paused", 6) || | |
1200 | !strncmp(reason, "shutdown", 8)) | |
1201 | message = "Printer off-line."; | |
1202 | else if (!strncmp(reason, "toner-low", 9)) | |
1203 | message = "Toner low."; | |
1204 | else if (!strncmp(reason, "toner-empty", 11)) | |
1205 | message = "Out of toner!"; | |
1206 | else if (!strncmp(reason, "cover-open", 10)) | |
1207 | message = "Cover open."; | |
1208 | else if (!strncmp(reason, "interlock-open", 14)) | |
1209 | message = "Interlock open."; | |
1210 | else if (!strncmp(reason, "door-open", 9)) | |
1211 | message = "Door open."; | |
1212 | else if (!strncmp(reason, "input-tray-missing", 18)) | |
1213 | message = "Media tray missing!"; | |
1214 | else if (!strncmp(reason, "media-low", 9)) | |
1215 | message = "Media tray almost empty."; | |
1216 | else if (!strncmp(reason, "media-empty", 11)) | |
1217 | message = "Media tray empty!"; | |
1218 | else if (!strncmp(reason, "output-tray-missing", 19)) | |
1219 | message = "Output tray missing!"; | |
1220 | else if (!strncmp(reason, "output-area-almost-full", 23)) | |
1221 | message = "Output bin almost full."; | |
1222 | else if (!strncmp(reason, "output-area-full", 16)) | |
1223 | message = "Output bin full!"; | |
1224 | else if (!strncmp(reason, "marker-supply-low", 17)) | |
1225 | message = "Ink/toner almost empty."; | |
1226 | else if (!strncmp(reason, "marker-supply-empty", 19)) | |
1227 | message = "Ink/toner empty!"; | |
1228 | else if (!strncmp(reason, "marker-waste-almost-full", 24)) | |
1229 | message = "Ink/toner waste bin almost full."; | |
1230 | else if (!strncmp(reason, "marker-waste-full", 17)) | |
1231 | message = "Ink/toner waste bin full!"; | |
1232 | else if (!strncmp(reason, "fuser-over-temp", 15)) | |
1233 | message = "Fuser temperature high!"; | |
1234 | else if (!strncmp(reason, "fuser-under-temp", 16)) | |
1235 | message = "Fuser temperature low!"; | |
1236 | else if (!strncmp(reason, "opc-near-eol", 12)) | |
1237 | message = "OPC almost at end-of-life."; | |
1238 | else if (!strncmp(reason, "opc-life-over", 13)) | |
1239 | message = "OPC at end-of-life!"; | |
1240 | else if (!strncmp(reason, "developer-low", 13)) | |
1241 | message = "Developer almost empty."; | |
1242 | else if (!strncmp(reason, "developer-empty", 15)) | |
1243 | message = "Developer empty!"; | |
1244 | else if (strstr(reason, "error") != NULL) | |
1245 | { | |
1246 | message = unknown; | |
1247 | ||
1248 | snprintf(unknown, sizeof(unknown), "Unknown printer error (%s)!", | |
1249 | reason); | |
1250 | } | |
1251 | ||
1252 | if (message) | |
1253 | { | |
1254 | count ++; | |
1255 | if (strstr(reasons->values[i].string.text, "error")) | |
1256 | fprintf(stderr, "ERROR: %s\n", message); | |
1257 | else if (strstr(reasons->values[i].string.text, "warning")) | |
1258 | fprintf(stderr, "WARNING: %s\n", message); | |
1259 | else | |
1260 | fprintf(stderr, "INFO: %s\n", message); | |
1261 | } | |
1262 | } | |
1263 | ||
1264 | fprintf(stderr, "%s\n", state); | |
1265 | ||
1266 | return (count); | |
1267 | } | |
1268 | ||
1269 | ||
1270 | #ifdef __APPLE__ | |
1271 | /* | |
1272 | * 'run_pictwps_filter()' - Convert PICT files to PostScript when printing | |
1273 | * remotely. | |
1274 | * | |
1275 | * This step is required because the PICT format is not documented and | |
1276 | * subject to change, so developing a filter for other OS's is infeasible. | |
1277 | * Also, fonts required by the PICT file need to be embedded on the | |
1278 | * client side (which has the fonts), so we run the filter to get a | |
1279 | * PostScript file for printing... | |
1280 | */ | |
1281 | ||
1282 | int /* O - Exit status of filter */ | |
1283 | run_pictwps_filter(char **argv, /* I - Command-line arguments */ | |
1284 | const char *filename) /* I - Filename */ | |
1285 | { | |
1286 | struct stat fileinfo; /* Print file information */ | |
1287 | const char *ppdfile; /* PPD file for destination printer */ | |
1288 | int pid; /* Child process ID */ | |
1289 | int fd; /* Temporary file descriptor */ | |
1290 | int status; /* Exit status of filter */ | |
1291 | const char *printer; /* PRINTER env var */ | |
1292 | static char ppdenv[1024]; /* PPD environment variable */ | |
1293 | ||
1294 | ||
1295 | /* | |
1296 | * First get the PPD file for the printer... | |
1297 | */ | |
1298 | ||
1299 | printer = getenv("PRINTER"); | |
1300 | if (!printer) | |
1301 | { | |
1302 | fputs("ERROR: PRINTER environment variable not defined!\n", stderr); | |
1303 | return (-1); | |
1304 | } | |
1305 | ||
1306 | if ((ppdfile = cupsGetPPD(printer)) == NULL) | |
1307 | { | |
1308 | fprintf(stderr, "ERROR: Unable to get PPD file for printer \"%s\" - %s.\n", | |
1309 | printer, ippErrorString(cupsLastError())); | |
1310 | /*return (-1);*/ | |
1311 | } | |
1312 | else | |
1313 | { | |
1314 | snprintf(ppdenv, sizeof(ppdenv), "PPD=%s", ppdfile); | |
1315 | putenv(ppdenv); | |
1316 | } | |
1317 | ||
1318 | /* | |
1319 | * Then create a temporary file for printing... | |
1320 | */ | |
1321 | ||
1322 | if ((fd = cupsTempFd(pstmpname, sizeof(pstmpname))) < 0) | |
1323 | { | |
1324 | fprintf(stderr, "ERROR: Unable to create temporary file - %s.\n", | |
1325 | strerror(errno)); | |
1326 | if (ppdfile) | |
1327 | unlink(ppdfile); | |
1328 | return (-1); | |
1329 | } | |
1330 | ||
1331 | /* | |
1332 | * Get the owner of the spool file - it is owned by the user we want to run | |
1333 | * as... | |
1334 | */ | |
1335 | ||
1336 | if (argv[6]) | |
1337 | stat(argv[6], &fileinfo); | |
1338 | else | |
1339 | { | |
1340 | /* | |
1341 | * Use the OSX defaults, as an up-stream filter created the PICT | |
1342 | * file... | |
1343 | */ | |
1344 | ||
1345 | fileinfo.st_uid = 1; | |
1346 | fileinfo.st_gid = 80; | |
1347 | } | |
1348 | ||
1349 | if (ppdfile) | |
1350 | chown(ppdfile, fileinfo.st_uid, fileinfo.st_gid); | |
1351 | ||
1352 | fchown(fd, fileinfo.st_uid, fileinfo.st_gid); | |
1353 | ||
1354 | /* | |
1355 | * Finally, run the filter to convert the file... | |
1356 | */ | |
1357 | ||
1358 | if ((pid = fork()) == 0) | |
1359 | { | |
1360 | /* | |
1361 | * Child process for pictwpstops... Redirect output of pictwpstops to a | |
1362 | * file... | |
1363 | */ | |
1364 | ||
1365 | close(1); | |
1366 | dup(fd); | |
1367 | close(fd); | |
1368 | ||
1369 | if (!getuid()) | |
1370 | { | |
1371 | /* | |
1372 | * Change to an unpriviledged user... | |
1373 | */ | |
1374 | ||
1375 | setgid(fileinfo.st_gid); | |
1376 | setuid(fileinfo.st_uid); | |
1377 | } | |
1378 | ||
1379 | execlp("pictwpstops", printer, argv[1], argv[2], argv[3], argv[4], argv[5], | |
1380 | filename, NULL); | |
1381 | perror("ERROR: Unable to exec pictwpstops"); | |
1382 | return (errno); | |
1383 | } | |
1384 | ||
1385 | close(fd); | |
1386 | ||
1387 | if (pid < 0) | |
1388 | { | |
1389 | /* | |
1390 | * Error! | |
1391 | */ | |
1392 | ||
1393 | perror("ERROR: Unable to fork pictwpstops"); | |
1394 | unlink(filename); | |
1395 | if (ppdfile) | |
1396 | unlink(ppdfile); | |
1397 | return (-1); | |
1398 | } | |
1399 | ||
1400 | /* | |
1401 | * Now wait for the filter to complete... | |
1402 | */ | |
1403 | ||
1404 | if (wait(&status) < 0) | |
1405 | { | |
1406 | perror("ERROR: Unable to wait for pictwpstops"); | |
1407 | close(fd); | |
1408 | unlink(filename); | |
1409 | if (ppdfile) | |
1410 | unlink(ppdfile); | |
1411 | return (-1); | |
1412 | } | |
1413 | ||
1414 | if (ppdfile) | |
1415 | unlink(ppdfile); | |
1416 | ||
1417 | close(fd); | |
1418 | ||
1419 | if (status) | |
1420 | { | |
1421 | if (status >= 256) | |
1422 | fprintf(stderr, "ERROR: pictwpstops exited with status %d!\n", | |
1423 | status / 256); | |
1424 | else | |
1425 | fprintf(stderr, "ERROR: pictwpstops exited on signal %d!\n", | |
1426 | status); | |
1427 | ||
1428 | unlink(filename); | |
1429 | return (status); | |
1430 | } | |
1431 | ||
1432 | /* | |
1433 | * Return with no errors.. | |
1434 | */ | |
1435 | ||
1436 | return (0); | |
1437 | } | |
1438 | #endif /* __APPLE__ */ | |
1439 | ||
1440 | ||
1441 | /* | |
1442 | * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend. | |
1443 | */ | |
1444 | ||
1445 | static void | |
1446 | sigterm_handler(int sig) /* I - Signal */ | |
1447 | { | |
1448 | (void)sig; /* remove compiler warnings... */ | |
1449 | ||
1450 | /* | |
1451 | * Remove the temporary file(s) if necessary... | |
1452 | */ | |
1453 | ||
1454 | if (tmpfilename[0]) | |
1455 | unlink(tmpfilename); | |
1456 | ||
1457 | #ifdef __APPLE__ | |
1458 | if (pstmpname[0]) | |
1459 | unlink(pstmpname); | |
1460 | #endif /* __APPLE__ */ | |
1461 | ||
1462 | exit(1); | |
1463 | } | |
1464 | ||
1465 | ||
1466 | /* | |
1467 | * End of "$Id: ipp.c 4906 2006-01-10 20:53:28Z mike $". | |
1468 | */ |