]>
Commit | Line | Data |
---|---|---|
bc44d920 | 1 | /* |
2e4ff8af | 2 | * "$Id: cupsfilter.c 6879 2007-08-29 20:26:50Z mike $" |
bc44d920 | 3 | * |
4 | * CUPS filtering program for the Common UNIX Printing System (CUPS). | |
5 | * | |
91c84a35 | 6 | * Copyright 2007-2008 by Apple Inc. |
bc44d920 | 7 | * Copyright 1997-2006 by Easy Software Products, all rights reserved. |
8 | * | |
9 | * These coded instructions, statements, and computer programs are the | |
10 | * property of Apple Inc. and are protected by Federal copyright | |
11 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
12 | * which should have been included with this file. If this file is | |
13 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
14 | * | |
15 | * Contents: | |
16 | * | |
17 | * main() - Main entry for the test program. | |
18 | * compare_pids() - Compare two filter PIDs... | |
19 | * escape_options() - Convert an options array to a string. | |
20 | * exec_filter() - Execute a single filter. | |
21 | * exec_filters() - Execute filters for the given file and options. | |
2e4ff8af | 22 | * get_job_file() - Get the specified job file. |
bc44d920 | 23 | * open_pipe() - Create a pipe which is closed on exec. |
24 | * read_cupsd_conf() - Read the cupsd.conf file to get the filter settings. | |
25 | * set_string() - Copy and set a string. | |
26 | * usage() - Show program usage... | |
27 | */ | |
28 | ||
29 | /* | |
30 | * Include necessary headers... | |
31 | */ | |
32 | ||
33 | #include <cups/cups.h> | |
34 | #include <cups/i18n.h> | |
35 | #include <cups/string.h> | |
36 | #include <errno.h> | |
37 | #include "mime.h" | |
38 | #include <stdlib.h> | |
2e4ff8af | 39 | #include <limits.h> |
bc44d920 | 40 | #include <unistd.h> |
41 | #include <fcntl.h> | |
2e4ff8af | 42 | #include <signal.h> |
bc44d920 | 43 | #include <sys/wait.h> |
44 | #if defined(__APPLE__) | |
45 | # include <libgen.h> | |
46 | #endif /* __APPLE__ */ | |
47 | ||
48 | ||
49 | /* | |
50 | * Local globals... | |
51 | */ | |
52 | ||
53 | static char *DataDir = NULL;/* CUPS_DATADIR environment variable */ | |
54 | static char *FontPath = NULL; | |
55 | /* CUPS_FONTPATH environment variable */ | |
56 | static mime_filter_t GZIPFilter = /* gziptoany filter */ | |
57 | { | |
58 | NULL, /* Source type */ | |
59 | NULL, /* Destination type */ | |
60 | 0, /* Cost */ | |
61 | "gziptoany" /* Filter program to run */ | |
62 | }; | |
63 | static char *Path = NULL; /* PATH environment variable */ | |
64 | static char *ServerBin = NULL; | |
65 | /* CUPS_SERVERBIN environment variable */ | |
66 | static char *ServerRoot = NULL; | |
67 | /* CUPS_SERVERROOT environment variable */ | |
68 | static char *RIPCache = NULL; | |
69 | /* RIP_CACHE environment variable */ | |
2e4ff8af MS |
70 | static char TempFile[1024] = ""; |
71 | /* Temporary file */ | |
bc44d920 | 72 | |
73 | ||
74 | /* | |
75 | * Local functions... | |
76 | */ | |
77 | ||
78 | static int compare_pids(mime_filter_t *a, mime_filter_t *b); | |
79 | static char *escape_options(int num_options, cups_option_t *options); | |
80 | static int exec_filter(const char *filter, char **argv, char **envp, | |
81 | int infd, int outfd); | |
cc0d019f MS |
82 | static int exec_filters(cups_array_t *filters, const char *infile, |
83 | const char *outfile, const char *ppdfile, | |
db1f069b MS |
84 | const char *printer, const char *user, |
85 | const char *title, int num_options, | |
86 | cups_option_t *options); | |
2e4ff8af | 87 | static void get_job_file(const char *job); |
bc44d920 | 88 | static int open_pipe(int *fds); |
89 | static int read_cupsd_conf(const char *filename); | |
90 | static void set_string(char **s, const char *val); | |
2e4ff8af | 91 | static void sighandler(int sig); |
cc0d019f | 92 | static void usage(const char *command, const char *opt); |
bc44d920 | 93 | |
94 | ||
95 | /* | |
96 | * 'main()' - Main entry for the test program. | |
97 | */ | |
98 | ||
99 | int /* O - Exit status */ | |
100 | main(int argc, /* I - Number of command-line args */ | |
101 | char *argv[]) /* I - Command-line arguments */ | |
102 | { | |
103 | int i; /* Looping vars */ | |
cc0d019f MS |
104 | const char *command, /* Command name */ |
105 | *opt; /* Current option */ | |
106 | char *srctype, /* Source type */ | |
107 | *dsttype, /* Destination type */ | |
108 | super[MIME_MAX_SUPER], /* Super-type name */ | |
bc44d920 | 109 | type[MIME_MAX_TYPE]; /* Type name */ |
110 | int compression; /* Compression of file */ | |
111 | int cost; /* Cost of filters */ | |
112 | mime_t *mime; /* MIME database */ | |
dd1abb6b | 113 | char mimedir[1024]; /* MIME directory */ |
cc0d019f MS |
114 | char *infile, /* File to filter */ |
115 | *outfile; /* File to create */ | |
bc44d920 | 116 | char cupsdconf[1024]; /* cupsd.conf file */ |
117 | const char *server_root; /* CUPS_SERVERROOT environment variable */ | |
118 | mime_type_t *src, /* Source type */ | |
119 | *dst; /* Destination type */ | |
120 | cups_array_t *filters; /* Filters for the file */ | |
121 | int num_options; /* Number of options */ | |
122 | cups_option_t *options; /* Options */ | |
123 | const char *ppdfile; /* PPD file */ | |
cc0d019f MS |
124 | const char *title, /* Title string */ |
125 | *user; /* Username */ | |
126 | int removeppd, /* Remove PPD file */ | |
127 | removeinfile; /* Remove input file */ | |
128 | int status; /* Execution status */ | |
bc44d920 | 129 | |
130 | ||
131 | /* | |
132 | * Setup defaults... | |
133 | */ | |
134 | ||
cc0d019f MS |
135 | if ((command = strrchr(argv[0], '/')) != NULL) |
136 | command ++; | |
137 | else | |
138 | command = argv[0]; | |
139 | ||
140 | mime = NULL; | |
141 | srctype = NULL; | |
91c84a35 | 142 | compression = 0; |
cc0d019f MS |
143 | dsttype = "application/pdf"; |
144 | infile = NULL; | |
145 | outfile = NULL; | |
146 | num_options = 0; | |
147 | options = NULL; | |
148 | ppdfile = NULL; | |
149 | title = NULL; | |
150 | user = cupsUser(); | |
151 | removeppd = 0; | |
152 | removeinfile = 0; | |
bc44d920 | 153 | |
154 | if ((server_root = getenv("CUPS_SERVERROOT")) == NULL) | |
155 | server_root = CUPS_SERVERROOT; | |
156 | ||
157 | snprintf(cupsdconf, sizeof(cupsdconf), "%s/cupsd.conf", server_root); | |
158 | ||
159 | /* | |
160 | * Process command-line arguments... | |
161 | */ | |
162 | ||
163 | _cupsSetLocale(argv); | |
164 | ||
165 | for (i = 1; i < argc; i ++) | |
166 | if (argv[i][0] == '-') | |
167 | { | |
168 | for (opt = argv[i] + 1; *opt; opt ++) | |
169 | switch (*opt) | |
170 | { | |
171 | case '-' : /* Next argument is a filename... */ | |
172 | i ++; | |
cc0d019f MS |
173 | if (i < argc && !infile) |
174 | infile = argv[i]; | |
bc44d920 | 175 | else |
cc0d019f MS |
176 | usage(command, opt); |
177 | break; | |
178 | ||
179 | case 'a' : /* Specify option... */ | |
180 | i ++; | |
181 | if (i < argc) | |
182 | num_options = cupsParseOptions(argv[i], num_options, &options); | |
183 | else | |
184 | usage(command, opt); | |
bc44d920 | 185 | break; |
186 | ||
187 | case 'c' : /* Specify cupsd.conf file location... */ | |
188 | i ++; | |
189 | if (i < argc) | |
cc0d019f MS |
190 | { |
191 | if (!strcmp(command, "convert")) | |
192 | num_options = cupsAddOption("copies", argv[i], num_options, | |
193 | &options); | |
194 | else | |
195 | strlcpy(cupsdconf, argv[i], sizeof(cupsdconf)); | |
196 | } | |
197 | else | |
198 | usage(command, opt); | |
199 | break; | |
200 | ||
201 | case 'D' : /* Delete input file after conversion */ | |
202 | removeinfile = 1; | |
203 | break; | |
204 | ||
205 | case 'f' : /* Specify input file... */ | |
206 | i ++; | |
207 | if (i < argc && !infile) | |
208 | infile = argv[i]; | |
209 | else | |
210 | usage(command, opt); | |
211 | break; | |
212 | ||
213 | case 'i' : /* Specify source MIME type... */ | |
214 | i ++; | |
215 | if (i < argc) | |
216 | { | |
217 | if (sscanf(argv[i], "%15[^/]/%255s", super, type) != 2) | |
218 | usage(command, opt); | |
219 | ||
220 | srctype = argv[i]; | |
221 | } | |
bc44d920 | 222 | else |
cc0d019f | 223 | usage(command, opt); |
bc44d920 | 224 | break; |
225 | ||
2e4ff8af MS |
226 | case 'j' : /* Get job file or specify destination MIME type... */ |
227 | if (strcmp(command, "convert")) | |
228 | { | |
229 | i ++; | |
230 | if (i < argc) | |
231 | { | |
232 | get_job_file(argv[i]); | |
233 | infile = TempFile; | |
234 | } | |
235 | else | |
236 | usage(command, opt); | |
237 | ||
238 | break; | |
239 | } | |
240 | ||
bc44d920 | 241 | case 'm' : /* Specify destination MIME type... */ |
242 | i ++; | |
243 | if (i < argc) | |
244 | { | |
245 | if (sscanf(argv[i], "%15[^/]/%255s", super, type) != 2) | |
cc0d019f MS |
246 | usage(command, opt); |
247 | ||
248 | dsttype = argv[i]; | |
bc44d920 | 249 | } |
250 | else | |
cc0d019f | 251 | usage(command, opt); |
bc44d920 | 252 | break; |
253 | ||
254 | case 'n' : /* Specify number of copies... */ | |
255 | i ++; | |
256 | if (i < argc) | |
257 | num_options = cupsAddOption("copies", argv[i], num_options, | |
258 | &options); | |
259 | else | |
cc0d019f | 260 | usage(command, opt); |
bc44d920 | 261 | break; |
262 | ||
cc0d019f | 263 | case 'o' : /* Specify option(s) or output filename */ |
bc44d920 | 264 | i ++; |
265 | if (i < argc) | |
cc0d019f MS |
266 | { |
267 | if (!strcmp(command, "convert")) | |
268 | { | |
269 | if (outfile) | |
270 | usage(command, NULL); | |
271 | else | |
272 | outfile = argv[i]; | |
273 | } | |
274 | else | |
275 | num_options = cupsParseOptions(argv[i], num_options, | |
276 | &options); | |
277 | } | |
bc44d920 | 278 | else |
cc0d019f | 279 | usage(command, opt); |
bc44d920 | 280 | break; |
281 | ||
282 | case 'p' : /* Specify PPD file... */ | |
cc0d019f | 283 | case 'P' : /* Specify PPD file... */ |
bc44d920 | 284 | i ++; |
285 | if (i < argc) | |
286 | ppdfile = argv[i]; | |
287 | else | |
cc0d019f | 288 | usage(command, opt); |
bc44d920 | 289 | break; |
290 | ||
cc0d019f MS |
291 | case 't' : /* Specify title... */ |
292 | case 'J' : /* Specify title... */ | |
bc44d920 | 293 | i ++; |
294 | if (i < argc) | |
295 | title = argv[i]; | |
296 | else | |
cc0d019f MS |
297 | usage(command, opt); |
298 | break; | |
299 | ||
300 | case 'u' : /* Delete PPD file after conversion */ | |
301 | removeinfile = 1; | |
302 | break; | |
303 | ||
304 | case 'U' : /* Specify username... */ | |
305 | i ++; | |
306 | if (i < argc) | |
307 | user = argv[i]; | |
308 | else | |
309 | usage(command, opt); | |
bc44d920 | 310 | break; |
311 | ||
312 | default : /* Something we don't understand... */ | |
cc0d019f | 313 | usage(command, opt); |
bc44d920 | 314 | break; |
315 | } | |
316 | } | |
cc0d019f MS |
317 | else if (!infile) |
318 | { | |
319 | if (strcmp(command, "convert")) | |
320 | infile = argv[i]; | |
321 | else | |
322 | { | |
323 | _cupsLangPuts(stderr, | |
324 | _("convert: Use the -f option to specify a file to " | |
325 | "convert.\n")); | |
326 | usage(command, NULL); | |
327 | } | |
328 | } | |
bc44d920 | 329 | else |
330 | { | |
331 | _cupsLangPuts(stderr, | |
332 | _("cupsfilter: Only one filename can be specified!\n")); | |
cc0d019f | 333 | usage(command, NULL); |
bc44d920 | 334 | } |
335 | ||
cc0d019f MS |
336 | if (!infile && !srctype) |
337 | usage(command, NULL); | |
bc44d920 | 338 | |
339 | if (!title) | |
340 | { | |
cc0d019f MS |
341 | if (!infile) |
342 | title = "(stdin)"; | |
343 | else if ((title = strrchr(infile, '/')) != NULL) | |
bc44d920 | 344 | title ++; |
345 | else | |
cc0d019f | 346 | title = infile; |
bc44d920 | 347 | } |
348 | ||
349 | /* | |
350 | * Load the cupsd.conf file and create the MIME database... | |
351 | */ | |
352 | ||
353 | if (read_cupsd_conf(cupsdconf)) | |
354 | return (1); | |
355 | ||
356 | if ((mime = mimeLoad(ServerRoot, Path)) == NULL) | |
357 | { | |
358 | _cupsLangPrintf(stderr, | |
cc0d019f MS |
359 | _("%s: Unable to read MIME database from \"%s\"!\n"), |
360 | command, ServerRoot); | |
bc44d920 | 361 | return (1); |
362 | } | |
363 | ||
dd1abb6b MS |
364 | snprintf(mimedir, sizeof(mimedir), "%s/mime", DataDir); |
365 | if (!access(mimedir, 0)) | |
366 | mime = mimeMerge(mime, mimedir, Path); | |
367 | ||
bc44d920 | 368 | /* |
369 | * Get the source and destination types... | |
370 | */ | |
371 | ||
cc0d019f MS |
372 | if (srctype) |
373 | { | |
374 | sscanf(srctype, "%15[^/]/%255s", super, type); | |
375 | if ((src = mimeType(mime, super, type)) == NULL) | |
376 | { | |
377 | _cupsLangPrintf(stderr, | |
378 | _("%s: Unknown source MIME type %s/%s!\n"), | |
379 | command, super, type); | |
380 | return (1); | |
381 | } | |
382 | } | |
383 | else if ((src = mimeFileType(mime, infile, infile, &compression)) == NULL) | |
bc44d920 | 384 | { |
385 | _cupsLangPrintf(stderr, | |
cc0d019f MS |
386 | _("%s: Unable to determine MIME type of \"%s\"!\n"), |
387 | command, infile); | |
bc44d920 | 388 | return (1); |
389 | } | |
390 | ||
cc0d019f | 391 | sscanf(dsttype, "%15[^/]/%255s", super, type); |
bc44d920 | 392 | if ((dst = mimeType(mime, super, type)) == NULL) |
393 | { | |
394 | _cupsLangPrintf(stderr, | |
cc0d019f MS |
395 | _("%s: Unknown destination MIME type %s/%s!\n"), |
396 | command, super, type); | |
bc44d920 | 397 | return (1); |
398 | } | |
399 | ||
400 | /* | |
401 | * Figure out how to filter the file... | |
402 | */ | |
403 | ||
404 | if (src == dst) | |
405 | { | |
406 | /* | |
407 | * Special case - no filtering needed... | |
408 | */ | |
409 | ||
410 | filters = cupsArrayNew(NULL, NULL); | |
411 | cupsArrayAdd(filters, &GZIPFilter); | |
412 | } | |
413 | else if ((filters = mimeFilter(mime, src, dst, &cost)) == NULL) | |
414 | { | |
415 | _cupsLangPrintf(stderr, | |
cc0d019f MS |
416 | _("%s: No filter to convert from %s/%s to %s/%s!\n"), |
417 | command, src->super, src->type, dst->super, dst->type); | |
bc44d920 | 418 | return (1); |
419 | } | |
420 | else if (compression) | |
421 | cupsArrayInsert(filters, &GZIPFilter); | |
422 | ||
423 | /* | |
424 | * Do it! | |
425 | */ | |
426 | ||
db1f069b MS |
427 | status = exec_filters(filters, infile, outfile, ppdfile, |
428 | !strcmp(command, "convert") ? "tofile" : "cupsfilter", | |
429 | user, title, num_options, options); | |
cc0d019f MS |
430 | |
431 | /* | |
432 | * Remove files as needed, then exit... | |
433 | */ | |
434 | ||
2e4ff8af MS |
435 | if (TempFile[0]) |
436 | unlink(TempFile); | |
437 | ||
cc0d019f MS |
438 | if (removeppd && ppdfile) |
439 | unlink(ppdfile); | |
440 | ||
441 | if (removeinfile && infile) | |
442 | unlink(infile); | |
443 | ||
444 | return (status); | |
bc44d920 | 445 | } |
446 | ||
447 | ||
448 | /* | |
449 | * 'compare_pids()' - Compare two filter PIDs... | |
450 | */ | |
451 | ||
452 | static int /* O - Result of comparison */ | |
453 | compare_pids(mime_filter_t *a, /* I - First filter */ | |
454 | mime_filter_t *b) /* I - Second filter */ | |
455 | { | |
456 | /* | |
457 | * Because we're particularly lazy, we store the process ID in the "cost" | |
458 | * variable... | |
459 | */ | |
460 | ||
461 | return (a->cost - b->cost); | |
462 | } | |
463 | ||
464 | ||
465 | /* | |
466 | * 'escape_options()' - Convert an options array to a string. | |
467 | */ | |
468 | ||
469 | static char * /* O - Option string */ | |
470 | escape_options( | |
471 | int num_options, /* I - Number of options */ | |
472 | cups_option_t *options) /* I - Options */ | |
473 | { | |
474 | int i; /* Looping var */ | |
475 | cups_option_t *option; /* Current option */ | |
476 | int bytes; /* Number of bytes needed */ | |
477 | char *s, /* Option string */ | |
478 | *sptr, /* Pointer into string */ | |
479 | *vptr; /* Pointer into value */ | |
480 | ||
481 | ||
482 | /* | |
483 | * Figure out the worst-case number of bytes we need for the option string. | |
484 | */ | |
485 | ||
486 | for (i = num_options, option = options, bytes = 1; i > 0; i --, option ++) | |
487 | bytes += 2 * (strlen(option->name) + strlen(option->value)) + 2; | |
488 | ||
91c84a35 MS |
489 | if ((s = malloc(bytes)) == NULL) |
490 | return (NULL); | |
bc44d920 | 491 | |
492 | /* | |
493 | * Copy the options to the string... | |
494 | */ | |
495 | ||
496 | for (i = num_options, option = options, sptr = s; i > 0; i --, option ++) | |
497 | { | |
498 | if (!strcmp(option->name, "copies")) | |
499 | continue; | |
500 | ||
501 | if (sptr > s) | |
502 | *sptr++ = ' '; | |
503 | ||
504 | strcpy(sptr, option->name); | |
505 | sptr += strlen(sptr); | |
506 | *sptr++ = '='; | |
507 | ||
508 | for (vptr = option->value; *vptr;) | |
509 | { | |
510 | if (strchr("\\ \t\n", *vptr)) | |
511 | *sptr++ = '\\'; | |
512 | ||
513 | *sptr++ = *vptr++; | |
514 | } | |
515 | } | |
516 | ||
517 | *sptr = '\0'; | |
518 | ||
bc44d920 | 519 | return (s); |
520 | } | |
521 | ||
522 | ||
523 | /* | |
524 | * 'exec_filter()' - Execute a single filter. | |
525 | */ | |
526 | ||
527 | static int /* O - Process ID or -1 on error */ | |
528 | exec_filter(const char *filter, /* I - Filter to execute */ | |
529 | char **argv, /* I - Argument list */ | |
530 | char **envp, /* I - Environment list */ | |
531 | int infd, /* I - Stdin file descriptor */ | |
532 | int outfd) /* I - Stdout file descriptor */ | |
533 | { | |
534 | int pid; /* Process ID */ | |
535 | #if defined(__APPLE__) | |
536 | char processPath[1024], /* CFProcessPath environment variable */ | |
537 | linkpath[1024]; /* Link path for symlinks... */ | |
538 | int linkbytes; /* Bytes for link path */ | |
539 | ||
540 | ||
541 | /* | |
542 | * Add special voodoo magic for MacOS X - this allows MacOS X | |
543 | * programs to access their bundle resources properly... | |
544 | */ | |
545 | ||
546 | if ((linkbytes = readlink(filter, linkpath, sizeof(linkpath) - 1)) > 0) | |
547 | { | |
548 | /* | |
549 | * Yes, this is a symlink to the actual program, nul-terminate and | |
550 | * use it... | |
551 | */ | |
552 | ||
553 | linkpath[linkbytes] = '\0'; | |
554 | ||
555 | if (linkpath[0] == '/') | |
556 | snprintf(processPath, sizeof(processPath), "CFProcessPath=%s", | |
557 | linkpath); | |
558 | else | |
559 | snprintf(processPath, sizeof(processPath), "CFProcessPath=%s/%s", | |
560 | dirname((char *)filter), linkpath); | |
561 | } | |
562 | else | |
563 | snprintf(processPath, sizeof(processPath), "CFProcessPath=%s", filter); | |
564 | ||
565 | envp[0] = processPath; /* Replace <CFProcessPath> string */ | |
566 | #endif /* __APPLE__ */ | |
567 | ||
568 | if ((pid = fork()) == 0) | |
569 | { | |
570 | /* | |
571 | * Child process goes here... | |
572 | * | |
573 | * Update stdin/stdout/stderr as needed... | |
574 | */ | |
575 | ||
576 | if (infd != 0) | |
577 | { | |
578 | close(0); | |
579 | if (infd > 0) | |
580 | dup(infd); | |
581 | else | |
582 | open("/dev/null", O_RDONLY); | |
583 | } | |
584 | ||
585 | if (outfd != 1) | |
586 | { | |
587 | close(1); | |
588 | if (outfd > 0) | |
589 | dup(outfd); | |
590 | else | |
591 | open("/dev/null", O_WRONLY); | |
592 | } | |
593 | ||
594 | close(3); | |
595 | open("/dev/null", O_RDWR); | |
596 | fcntl(3, F_SETFL, O_NDELAY); | |
597 | ||
598 | close(4); | |
599 | open("/dev/null", O_RDWR); | |
600 | fcntl(4, F_SETFL, O_NDELAY); | |
601 | ||
602 | /* | |
603 | * Execute command... | |
604 | */ | |
605 | ||
606 | execve(filter, argv, envp); | |
607 | ||
608 | perror(filter); | |
609 | ||
610 | exit(errno); | |
611 | } | |
612 | ||
613 | return (pid); | |
614 | } | |
615 | ||
616 | ||
617 | /* | |
618 | * 'exec_filters()' - Execute filters for the given file and options. | |
619 | */ | |
620 | ||
621 | static int /* O - 0 on success, 1 on error */ | |
622 | exec_filters(cups_array_t *filters, /* I - Array of filters to run */ | |
cc0d019f MS |
623 | const char *infile, /* I - File to filter */ |
624 | const char *outfile, /* I - File to create */ | |
bc44d920 | 625 | const char *ppdfile, /* I - PPD file, if any */ |
db1f069b | 626 | const char *printer, /* I - Printer name */ |
cc0d019f | 627 | const char *user, /* I - Username */ |
bc44d920 | 628 | const char *title, /* I - Job title */ |
629 | int num_options, /* I - Number of filter options */ | |
630 | cups_option_t *options) /* I - Filter options */ | |
631 | { | |
cc0d019f | 632 | int i; /* Looping var */ |
bc44d920 | 633 | const char *argv[8], /* Command-line arguments */ |
634 | *envp[11], /* Environment variables */ | |
635 | *temp; /* Temporary string */ | |
636 | char *optstr, /* Filter options */ | |
637 | cups_datadir[1024], /* CUPS_DATADIR */ | |
638 | cups_fontpath[1024], /* CUPS_FONTPATH */ | |
639 | cups_serverbin[1024], /* CUPS_SERVERBIN */ | |
640 | cups_serverroot[1024], /* CUPS_SERVERROOT */ | |
641 | lang[1024], /* LANG */ | |
642 | path[1024], /* PATH */ | |
643 | ppd[1024], /* PPD */ | |
644 | rip_cache[1024], /* RIP_CACHE */ | |
cc0d019f | 645 | userenv[1024], /* USER */ |
bc44d920 | 646 | program[1024]; /* Program to run */ |
647 | mime_filter_t *filter, /* Current filter */ | |
648 | *next; /* Next filter */ | |
649 | int current, /* Current filter */ | |
650 | filterfds[2][2], /* Pipes for filters */ | |
651 | pid, /* Process ID of filter */ | |
652 | status, /* Exit status */ | |
653 | retval; /* Return value */ | |
654 | cups_array_t *pids; /* Executed filters array */ | |
655 | mime_filter_t key; /* Search key for filters */ | |
cc0d019f | 656 | cups_lang_t *language; /* Current language */ |
bc44d920 | 657 | |
658 | ||
659 | /* | |
660 | * Setup the filter environment and command-line... | |
661 | */ | |
662 | ||
663 | optstr = escape_options(num_options, options); | |
664 | ||
665 | snprintf(cups_datadir, sizeof(cups_datadir), "CUPS_DATADIR=%s", DataDir); | |
666 | snprintf(cups_fontpath, sizeof(cups_fontpath), "CUPS_FONTPATH=%s", FontPath); | |
667 | snprintf(cups_serverbin, sizeof(cups_serverbin), "CUPS_SERVERBIN=%s", | |
668 | ServerBin); | |
669 | snprintf(cups_serverroot, sizeof(cups_serverroot), "CUPS_SERVERROOT=%s", | |
670 | ServerRoot); | |
cc0d019f MS |
671 | language = cupsLangDefault(); |
672 | snprintf(lang, sizeof(lang), "LANG=%s.UTF8", language->language); | |
bc44d920 | 673 | snprintf(path, sizeof(path), "PATH=%s", Path); |
674 | if (ppdfile) | |
675 | snprintf(ppd, sizeof(ppd), "PPD=%s", ppdfile); | |
676 | else if ((temp = getenv("PPD")) != NULL) | |
677 | snprintf(ppd, sizeof(ppd), "PPD=%s", temp); | |
678 | else | |
cc0d019f | 679 | #ifdef __APPLE__ |
db1f069b MS |
680 | if (!access("/System/Library/Frameworks/ApplicationServices.framework/" |
681 | "Versions/A/Frameworks/PrintCore.framework/Versions/A/" | |
682 | "Resources/English.lproj/Generic.ppd", 0)) | |
cc0d019f MS |
683 | strlcpy(ppd, "PPD=/System/Library/Frameworks/ApplicationServices.framework/" |
684 | "Versions/A/Frameworks/PrintCore.framework/Versions/A/" | |
685 | "Resources/English.lproj/Generic.ppd", sizeof(ppd)); | |
db1f069b MS |
686 | else |
687 | strlcpy(ppd, "PPD=/System/Library/Frameworks/ApplicationServices.framework/" | |
688 | "Versions/A/Frameworks/PrintCore.framework/Versions/A/" | |
689 | "Resources/Generic.ppd", sizeof(ppd)); | |
cc0d019f | 690 | #else |
bc44d920 | 691 | snprintf(ppd, sizeof(ppd), "PPD=%s/model/laserjet.ppd", DataDir); |
cc0d019f | 692 | #endif /* __APPLE__ */ |
bc44d920 | 693 | snprintf(rip_cache, sizeof(rip_cache), "RIP_CACHE=%s", RIPCache); |
cc0d019f | 694 | snprintf(userenv, sizeof(userenv), "USER=%s", user); |
bc44d920 | 695 | |
db1f069b MS |
696 | argv[0] = (char *)printer; |
697 | argv[1] = "1"; | |
cc0d019f | 698 | argv[2] = user; |
bc44d920 | 699 | argv[3] = title; |
700 | argv[4] = cupsGetOption("copies", num_options, options); | |
701 | argv[5] = optstr; | |
cc0d019f | 702 | argv[6] = infile; |
bc44d920 | 703 | argv[7] = NULL; |
704 | ||
705 | if (!argv[4]) | |
706 | argv[4] = "1"; | |
707 | ||
708 | envp[0] = "<CFProcessPath>"; | |
709 | envp[1] = cups_datadir; | |
710 | envp[2] = cups_fontpath; | |
711 | envp[3] = cups_serverbin; | |
712 | envp[4] = cups_serverroot; | |
713 | envp[5] = lang; | |
714 | envp[6] = path; | |
715 | envp[7] = ppd; | |
716 | envp[8] = rip_cache; | |
cc0d019f | 717 | envp[9] = userenv; |
bc44d920 | 718 | envp[10] = NULL; |
719 | ||
cc0d019f MS |
720 | for (i = 0; argv[i]; i ++) |
721 | fprintf(stderr, "DEBUG: argv[%d]=\"%s\"\n", i, argv[i]); | |
722 | ||
723 | for (i = 0; envp[i]; i ++) | |
724 | fprintf(stderr, "DEBUG: envp[%d]=\"%s\"\n", i, envp[i]); | |
725 | ||
bc44d920 | 726 | /* |
727 | * Execute all of the filters... | |
728 | */ | |
729 | ||
730 | pids = cupsArrayNew((cups_array_func_t)compare_pids, NULL); | |
731 | current = 0; | |
732 | filterfds[0][0] = -1; | |
733 | filterfds[0][1] = -1; | |
734 | filterfds[1][0] = -1; | |
735 | filterfds[1][1] = -1; | |
736 | ||
cc0d019f MS |
737 | if (!infile) |
738 | filterfds[0][0] = 0; | |
739 | ||
bc44d920 | 740 | for (filter = (mime_filter_t *)cupsArrayFirst(filters); |
741 | filter; | |
742 | filter = next, current = 1 - current) | |
743 | { | |
744 | next = (mime_filter_t *)cupsArrayNext(filters); | |
745 | ||
746 | if (filter->filter[0] == '/') | |
747 | strlcpy(program, filter->filter, sizeof(program)); | |
748 | else | |
749 | snprintf(program, sizeof(program), "%s/filter/%s", ServerBin, | |
750 | filter->filter); | |
751 | ||
752 | if (filterfds[!current][1] > 1) | |
753 | { | |
754 | close(filterfds[1 - current][0]); | |
755 | close(filterfds[1 - current][1]); | |
756 | ||
757 | filterfds[1 - current][0] = -1; | |
758 | filterfds[1 - current][0] = -1; | |
759 | } | |
760 | ||
761 | if (next) | |
762 | open_pipe(filterfds[1 - current]); | |
cc0d019f MS |
763 | else if (outfile) |
764 | { | |
765 | filterfds[1 - current][1] = open(outfile, O_CREAT | O_TRUNC | O_WRONLY, | |
766 | 0666); | |
767 | ||
768 | if (filterfds[1 - current][1] < 0) | |
769 | fprintf(stderr, "ERROR: Unable to create \"%s\" - %s\n", outfile, | |
770 | strerror(errno)); | |
771 | } | |
bc44d920 | 772 | else |
773 | filterfds[1 - current][1] = 1; | |
774 | ||
775 | pid = exec_filter(program, (char **)argv, (char **)envp, | |
776 | filterfds[current][0], filterfds[1 - current][1]); | |
777 | ||
778 | if (pid > 0) | |
779 | { | |
780 | fprintf(stderr, "INFO: %s (PID %d) started.\n", filter->filter, pid); | |
781 | ||
782 | filter->cost = pid; | |
783 | cupsArrayAdd(pids, filter); | |
784 | } | |
785 | else | |
786 | break; | |
787 | ||
788 | argv[6] = NULL; | |
789 | } | |
790 | ||
791 | /* | |
792 | * Close remaining pipes... | |
793 | */ | |
794 | ||
795 | if (filterfds[0][1] > 1) | |
796 | { | |
797 | close(filterfds[0][0]); | |
798 | close(filterfds[0][1]); | |
799 | } | |
800 | ||
801 | if (filterfds[1][1] > 1) | |
802 | { | |
803 | close(filterfds[1][0]); | |
804 | close(filterfds[1][1]); | |
805 | } | |
806 | ||
807 | /* | |
808 | * Wait for the children to exit... | |
809 | */ | |
810 | ||
811 | retval = 0; | |
812 | ||
813 | while (cupsArrayCount(pids) > 0) | |
814 | { | |
815 | if ((pid = wait(&status)) < 0) | |
816 | continue; | |
817 | ||
818 | key.cost = pid; | |
819 | if ((filter = (mime_filter_t *)cupsArrayFind(pids, &key)) != NULL) | |
820 | { | |
821 | cupsArrayRemove(pids, filter); | |
822 | ||
823 | if (status) | |
824 | { | |
825 | if (WIFEXITED(status)) | |
826 | fprintf(stderr, "ERROR: %s (PID %d) stopped with status %d!\n", | |
827 | filter->filter, pid, WEXITSTATUS(status)); | |
828 | else | |
829 | fprintf(stderr, "ERROR: %s (PID %d) crashed on signal %d!\n", | |
830 | filter->filter, pid, WTERMSIG(status)); | |
831 | ||
832 | retval = 1; | |
833 | } | |
834 | else | |
835 | fprintf(stderr, "INFO: %s (PID %d) exited with no errors.\n", | |
836 | filter->filter, pid); | |
837 | } | |
838 | } | |
839 | ||
91c84a35 MS |
840 | cupsArrayDelete(pids); |
841 | ||
bc44d920 | 842 | return (retval); |
843 | } | |
844 | ||
845 | ||
2e4ff8af MS |
846 | /* |
847 | * 'get_job_file()' - Get the specified job file. | |
848 | */ | |
849 | ||
850 | static void | |
851 | get_job_file(const char *job) /* I - Job ID */ | |
852 | { | |
853 | long jobid, /* Job ID */ | |
854 | docnum; /* Document number */ | |
855 | const char *jobptr; /* Pointer into job ID string */ | |
856 | char uri[1024]; /* job-uri */ | |
857 | http_t *http; /* Connection to server */ | |
858 | ipp_t *request; /* Request data */ | |
859 | int tempfd; /* Temporary file */ | |
860 | ||
861 | ||
862 | /* | |
863 | * Get the job ID and document number, if any... | |
864 | */ | |
865 | ||
866 | if ((jobptr = strrchr(job, '-')) != NULL) | |
867 | jobptr ++; | |
868 | else | |
869 | jobptr = job; | |
870 | ||
871 | jobid = strtol(jobptr, (char **)&jobptr, 10); | |
872 | ||
873 | if (*jobptr == ',') | |
874 | docnum = strtol(jobptr + 1, NULL, 10); | |
875 | else | |
876 | docnum = 1; | |
877 | ||
878 | if (jobid < 1 || jobid > INT_MAX) | |
879 | { | |
880 | _cupsLangPrintf(stderr, _("cupsfilter: Invalid job ID %d!\n"), (int)jobid); | |
881 | exit(1); | |
882 | } | |
883 | ||
884 | if (docnum < 1 || docnum > INT_MAX) | |
885 | { | |
886 | _cupsLangPrintf(stderr, _("cupsfilter: Invalid document number %d!\n"), | |
887 | (int)docnum); | |
888 | exit(1); | |
889 | } | |
890 | ||
891 | /* | |
892 | * Ask the server for the document file... | |
893 | */ | |
894 | ||
895 | if ((http = httpConnectEncrypt(cupsServer(), ippPort(), | |
896 | cupsEncryption())) == NULL) | |
897 | { | |
898 | _cupsLangPrintf(stderr, _("%s: Unable to connect to server\n"), | |
899 | "cupsfilter"); | |
900 | exit(1); | |
901 | } | |
902 | ||
903 | request = ippNewRequest(CUPS_GET_DOCUMENT); | |
904 | ||
905 | snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", (int)jobid); | |
906 | ||
907 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri); | |
908 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "document-number", | |
909 | (int)docnum); | |
910 | ||
911 | if ((tempfd = cupsTempFd(TempFile, sizeof(TempFile))) == -1) | |
912 | { | |
913 | _cupsLangPrintf(stderr, | |
914 | _("cupsfilter: Unable to create temporary file: %s\n"), | |
915 | strerror(errno)); | |
916 | httpClose(http); | |
917 | exit(1); | |
918 | } | |
919 | ||
920 | signal(SIGTERM, sighandler); | |
921 | ||
922 | ippDelete(cupsDoIORequest(http, request, "/", -1, tempfd)); | |
923 | ||
924 | close(tempfd); | |
925 | ||
926 | httpClose(http); | |
927 | ||
928 | if (cupsLastError() != IPP_OK) | |
929 | { | |
930 | _cupsLangPrintf(stderr, _("cupsfilter: Unable to get job file - %s\n"), | |
931 | cupsLastErrorString()); | |
932 | unlink(TempFile); | |
933 | exit(1); | |
934 | } | |
935 | } | |
936 | ||
937 | ||
bc44d920 | 938 | /* |
939 | * 'open_pipe()' - Create a pipe which is closed on exec. | |
940 | */ | |
941 | ||
2e4ff8af | 942 | static int /* O - 0 on success, -1 on error */ |
bc44d920 | 943 | open_pipe(int *fds) /* O - Pipe file descriptors (2) */ |
944 | { | |
945 | /* | |
946 | * Create the pipe... | |
947 | */ | |
948 | ||
949 | if (pipe(fds)) | |
950 | { | |
951 | fds[0] = -1; | |
952 | fds[1] = -1; | |
953 | ||
954 | return (-1); | |
955 | } | |
956 | ||
957 | /* | |
958 | * Set the "close on exec" flag on each end of the pipe... | |
959 | */ | |
960 | ||
961 | if (fcntl(fds[0], F_SETFD, fcntl(fds[0], F_GETFD) | FD_CLOEXEC)) | |
962 | { | |
963 | close(fds[0]); | |
964 | close(fds[1]); | |
965 | ||
966 | fds[0] = -1; | |
967 | fds[1] = -1; | |
968 | ||
969 | return (-1); | |
970 | } | |
971 | ||
972 | if (fcntl(fds[1], F_SETFD, fcntl(fds[1], F_GETFD) | FD_CLOEXEC)) | |
973 | { | |
974 | close(fds[0]); | |
975 | close(fds[1]); | |
976 | ||
977 | fds[0] = -1; | |
978 | fds[1] = -1; | |
979 | ||
980 | return (-1); | |
981 | } | |
982 | ||
983 | /* | |
984 | * Return 0 indicating success... | |
985 | */ | |
986 | ||
987 | return (0); | |
988 | } | |
989 | ||
990 | ||
991 | /* | |
992 | * 'read_cupsd_conf()' - Read the cupsd.conf file to get the filter settings. | |
993 | */ | |
994 | ||
995 | static int /* O - 0 on success, 1 on error */ | |
996 | read_cupsd_conf(const char *filename) /* I - File to read */ | |
997 | { | |
cc0d019f | 998 | cups_file_t *fp; /* cupsd.conf file */ |
bc44d920 | 999 | const char *temp; /* Temporary string */ |
1000 | char line[1024], /* Line from file */ | |
1001 | *ptr; /* Pointer into line */ | |
cc0d019f | 1002 | int linenum; /* Current line number */ |
bc44d920 | 1003 | |
1004 | ||
1005 | if ((temp = getenv("CUPS_DATADIR")) != NULL) | |
1006 | set_string(&DataDir, temp); | |
1007 | else | |
1008 | set_string(&DataDir, CUPS_DATADIR); | |
1009 | ||
1010 | if ((temp = getenv("CUPS_FONTPATH")) != NULL) | |
1011 | set_string(&FontPath, temp); | |
1012 | else | |
1013 | set_string(&FontPath, CUPS_FONTPATH); | |
1014 | ||
cc0d019f MS |
1015 | set_string(&RIPCache, "8m"); |
1016 | ||
bc44d920 | 1017 | if ((temp = getenv("CUPS_SERVERBIN")) != NULL) |
1018 | set_string(&ServerBin, temp); | |
1019 | else | |
1020 | set_string(&ServerBin, CUPS_SERVERBIN); | |
1021 | ||
1022 | strlcpy(line, filename, sizeof(line)); | |
1023 | if ((ptr = strrchr(line, '/')) != NULL) | |
1024 | *ptr = '\0'; | |
1025 | else | |
1026 | getcwd(line, sizeof(line)); | |
1027 | ||
1028 | set_string(&ServerRoot, line); | |
1029 | ||
cc0d019f MS |
1030 | if ((fp = cupsFileOpen(filename, "r")) != NULL) |
1031 | { | |
1032 | linenum = 0; | |
1033 | ||
1034 | while (cupsFileGetConf(fp, line, sizeof(line), &ptr, &linenum)) | |
1035 | { | |
1036 | if (!strcasecmp(line, "DataDir")) | |
1037 | set_string(&DataDir, ptr); | |
1038 | else if (!strcasecmp(line, "FontPath")) | |
1039 | set_string(&FontPath, ptr); | |
1040 | else if (!strcasecmp(line, "RIPCache")) | |
1041 | set_string(&RIPCache, ptr); | |
1042 | else if (!strcasecmp(line, "ServerBin")) | |
1043 | set_string(&ServerBin, ptr); | |
1044 | else if (!strcasecmp(line, "ServerRoot")) | |
1045 | set_string(&ServerRoot, ptr); | |
1046 | } | |
1047 | ||
1048 | cupsFileClose(fp); | |
1049 | } | |
1050 | ||
bc44d920 | 1051 | snprintf(line, sizeof(line), |
1052 | "%s/filter:" CUPS_BINDIR ":" CUPS_SBINDIR ":/bin/usr/bin", | |
1053 | ServerBin); | |
1054 | set_string(&Path, line); | |
1055 | ||
1056 | return (0); | |
1057 | } | |
1058 | ||
1059 | ||
1060 | /* | |
1061 | * 'set_string()' - Copy and set a string. | |
1062 | */ | |
1063 | ||
1064 | static void | |
1065 | set_string(char **s, /* O - Copy of string */ | |
1066 | const char *val) /* I - String to copy */ | |
1067 | { | |
1068 | if (*s) | |
1069 | free(*s); | |
1070 | ||
1071 | *s = strdup(val); | |
1072 | } | |
1073 | ||
1074 | ||
2e4ff8af MS |
1075 | /* |
1076 | * 'sighandler()' - Signal catcher for when we print from stdin... | |
1077 | */ | |
1078 | ||
1079 | static void | |
1080 | sighandler(int s) /* I - Signal number */ | |
1081 | { | |
1082 | /* | |
1083 | * Remove the temporary file we're using to print a job file... | |
1084 | */ | |
1085 | ||
1086 | if (TempFile[0]) | |
1087 | unlink(TempFile); | |
1088 | ||
1089 | /* | |
1090 | * Exit... | |
1091 | */ | |
1092 | ||
1093 | exit(s); | |
1094 | } | |
1095 | ||
1096 | ||
bc44d920 | 1097 | /* |
1098 | * 'usage()' - Show program usage... | |
1099 | */ | |
1100 | ||
1101 | static void | |
cc0d019f MS |
1102 | usage(const char *command, /* I - Command name */ |
1103 | const char *opt) /* I - Incorrect option, if any */ | |
bc44d920 | 1104 | { |
1105 | if (opt) | |
cc0d019f MS |
1106 | _cupsLangPrintf(stderr, _("%s: Unknown option '%c'!\n"), command, *opt); |
1107 | ||
1108 | if (!strcmp(command, "cupsfilter")) | |
1109 | _cupsLangPuts(stdout, | |
1110 | _("Usage: cupsfilter -m mime/type [ options ] filename\n" | |
1111 | "\n" | |
1112 | "Options:\n" | |
1113 | "\n" | |
1114 | " -c cupsd.conf Set cupsd.conf file to use\n" | |
2e4ff8af | 1115 | " -j job-id[,N] Filter file N from the specified job (default is file 1)\n" |
cc0d019f MS |
1116 | " -n copies Set number of copies\n" |
1117 | " -o name=value Set option(s)\n" | |
1118 | " -p filename.ppd Set PPD file\n" | |
1119 | " -t title Set title\n")); | |
1120 | else | |
1121 | _cupsLangPuts(stdout, | |
1122 | _("Usage: convert [ options ]\n" | |
1123 | "\n" | |
1124 | "Options:\n" | |
1125 | "\n" | |
1126 | " -f filename Set file to be converted (otherwise stdin)\n" | |
1127 | " -o filename Set file to be generated (otherwise stdout)\n" | |
1128 | " -i mime/type Set input MIME type (otherwise auto-typed)\n" | |
1129 | " -j mime/type Set output MIME type (otherwise application/pdf)\n" | |
1130 | " -P filename.ppd Set PPD file\n" | |
1131 | " -a 'name=value ...' Set option(s)\n" | |
1132 | " -U username Set username for job\n" | |
1133 | " -J title Set title\n" | |
1134 | " -c copies Set number of copies\n" | |
1135 | " -u Remove the PPD file when finished\n" | |
1136 | " -D Remove the input file when finished\n")); | |
bc44d920 | 1137 | |
1138 | exit(1); | |
1139 | } | |
1140 | ||
1141 | ||
1142 | /* | |
2e4ff8af | 1143 | * End of "$Id: cupsfilter.c 6879 2007-08-29 20:26:50Z mike $". |
bc44d920 | 1144 | */ |