2 * "$Id: ipp.c 6434 2007-04-02 22:07:10Z mike $"
4 * IPP backend for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * main() - Send a file to the printer or server.
29 * cancel_job() - Cancel a print job.
30 * check_printer_state() - Check the printer state...
31 * compress_files() - Compress print files...
32 * password_cb() - Disable the password prompt for
33 * cupsDoFileRequest().
34 * report_printer_state() - Report the printer state.
35 * run_pictwps_filter() - Convert PICT files to PostScript when printing
37 * sigterm_handler() - Handle 'terminate' signals that stop the backend.
41 * Include necessary headers.
44 #include <cups/http-private.h>
48 #include <sys/types.h>
50 #include <cups/backend.h>
51 #include <cups/cups.h>
52 #include <cups/language.h>
53 #include <cups/i18n.h>
54 #include <cups/string.h>
62 static char *password
= NULL
; /* Password for device URI */
64 static char pstmpname
[1024] = ""; /* Temporary PostScript file name */
65 #endif /* __APPLE__ */
66 static char tmpfilename
[1024] = ""; /* Temporary spool file name */
67 static int job_cancelled
= 0; /* Job cancelled? */
74 static void cancel_job(http_t
*http
, const char *uri
, int id
,
75 const char *resource
, const char *user
, int version
);
76 static void check_printer_state(http_t
*http
, const char *uri
,
77 const char *resource
, const char *user
,
80 static void compress_files(int num_files
, char **files
);
81 #endif /* HAVE_LIBZ */
82 static const char *password_cb(const char *);
83 static int report_printer_state(ipp_t
*ipp
);
86 static int run_pictwps_filter(char **argv
, const char *filename
);
87 #endif /* __APPLE__ */
88 static void sigterm_handler(int sig
);
92 * 'main()' - Send a file to the printer or server.
96 * printer-uri job-id user title copies options [file]
99 int /* O - Exit status */
100 main(int argc
, /* I - Number of command-line args */
101 char *argv
[]) /* I - Command-line arguments */
103 int i
; /* Looping var */
104 int num_options
; /* Number of printer options */
105 cups_option_t
*options
; /* Printer options */
106 char method
[255], /* Method in URI */
107 hostname
[1024], /* Hostname */
108 username
[255], /* Username info */
109 resource
[1024], /* Resource info (printer name) */
110 addrname
[256], /* Address name */
111 *optptr
, /* Pointer to URI options */
112 name
[255], /* Name of option */
113 value
[255], /* Value of option */
114 *ptr
; /* Pointer into name or value */
115 int num_files
; /* Number of files to print */
116 char **files
, /* Files to print */
117 *filename
; /* Pointer to single filename */
118 int port
; /* Port number (not used) */
119 char uri
[HTTP_MAX_URI
]; /* Updated URI without user/pass */
120 ipp_status_t ipp_status
; /* Status of IPP request */
121 http_t
*http
; /* HTTP connection */
122 ipp_t
*request
, /* IPP request */
123 *response
, /* IPP response */
124 *supported
; /* get-printer-attributes response */
125 time_t start_time
; /* Time of first connect */
126 int recoverable
; /* Recoverable error shown? */
127 int contimeout
; /* Connection timeout */
128 int delay
; /* Delay for retries... */
129 int compression
, /* Do compression of the job data? */
130 waitjob
, /* Wait for job complete? */
131 waitprinter
; /* Wait for printer ready? */
132 ipp_attribute_t
*job_id_attr
; /* job-id attribute */
133 int job_id
; /* job-id value */
134 ipp_attribute_t
*job_sheets
; /* job-media-sheets-completed */
135 ipp_attribute_t
*job_state
; /* job-state */
136 ipp_attribute_t
*copies_sup
; /* copies-supported */
137 ipp_attribute_t
*format_sup
; /* document-format-supported */
138 ipp_attribute_t
*printer_state
; /* printer-state attribute */
139 ipp_attribute_t
*printer_accepting
; /* printer-is-accepting-jobs */
140 int copies
, /* Number of copies for job */
141 copies_remaining
; /* Number of copies remaining */
142 const char *content_type
; /* CONTENT_TYPE environment variable */
143 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
144 struct sigaction action
; /* Actions for POSIX signals */
145 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
146 int version
; /* IPP version */
147 static const char * const pattrs
[] =
148 { /* Printer attributes we want */
150 "document-format-supported",
151 "printer-is-accepting-jobs",
153 "printer-state-message",
154 "printer-state-reasons",
156 static const char * const jattrs
[] =
157 { /* Job attributes we want */
158 "job-media-sheets-completed",
164 * Make sure status messages are not buffered...
167 setbuf(stderr
, NULL
);
170 * Ignore SIGPIPE and catch SIGTERM signals...
174 sigset(SIGPIPE
, SIG_IGN
);
175 sigset(SIGTERM
, sigterm_handler
);
176 #elif defined(HAVE_SIGACTION)
177 memset(&action
, 0, sizeof(action
));
178 action
.sa_handler
= SIG_IGN
;
179 sigaction(SIGPIPE
, &action
, NULL
);
181 sigemptyset(&action
.sa_mask
);
182 sigaddset(&action
.sa_mask
, SIGTERM
);
183 action
.sa_handler
= sigterm_handler
;
184 sigaction(SIGTERM
, &action
, NULL
);
186 signal(SIGPIPE
, SIG_IGN
);
187 signal(SIGTERM
, sigterm_handler
);
188 #endif /* HAVE_SIGSET */
191 * Check command-line...
198 if ((s
= strrchr(argv
[0], '/')) != NULL
)
203 printf("network %s \"Unknown\" \"Internet Printing Protocol (%s)\"\n",
205 return (CUPS_BACKEND_OK
);
209 fprintf(stderr
, _("Usage: %s job-id user title copies options [file]\n"),
211 return (CUPS_BACKEND_STOP
);
215 * Get the (final) content type...
218 if ((content_type
= getenv("FINAL_CONTENT_TYPE")) == NULL
)
219 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
220 content_type
= "application/octet-stream";
222 if (!strncmp(content_type
, "printer/", 8))
223 content_type
= "application/vnd.cups-raw";
226 * Extract the hostname and printer name from the URI...
229 if (httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
230 method
, sizeof(method
), username
, sizeof(username
),
231 hostname
, sizeof(hostname
), &port
,
232 resource
, sizeof(resource
)) < HTTP_URI_OK
)
234 fputs(_("ERROR: Missing device URI on command-line and no "
235 "DEVICE_URI environment variable!\n"), stderr
);
236 return (CUPS_BACKEND_STOP
);
239 if (!strcmp(method
, "https"))
240 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
242 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
245 * See if there are any options...
252 contimeout
= 7 * 24 * 60 * 60;
254 if ((optptr
= strchr(resource
, '?')) != NULL
)
257 * Yup, terminate the device name string and move to the first
258 * character of the optptr...
264 * Then parse the optptr...
273 for (ptr
= name
; *optptr
&& *optptr
!= '=';)
274 if (ptr
< (name
+ sizeof(name
) - 1))
286 for (ptr
= value
; *optptr
&& *optptr
!= '+' && *optptr
!= '&';)
287 if (ptr
< (value
+ sizeof(value
) - 1))
291 if (*optptr
== '+' || *optptr
== '&')
298 * Process the option...
301 if (!strcasecmp(name
, "waitjob"))
304 * Wait for job completion?
307 waitjob
= !strcasecmp(value
, "on") ||
308 !strcasecmp(value
, "yes") ||
309 !strcasecmp(value
, "true");
311 else if (!strcasecmp(name
, "waitprinter"))
314 * Wait for printer idle?
317 waitprinter
= !strcasecmp(value
, "on") ||
318 !strcasecmp(value
, "yes") ||
319 !strcasecmp(value
, "true");
321 else if (!strcasecmp(name
, "encryption"))
324 * Enable/disable encryption?
327 if (!strcasecmp(value
, "always"))
328 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
329 else if (!strcasecmp(value
, "required"))
330 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
331 else if (!strcasecmp(value
, "never"))
332 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
333 else if (!strcasecmp(value
, "ifrequested"))
334 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
338 _("ERROR: Unknown encryption option value \"%s\"!\n"),
342 else if (!strcasecmp(name
, "version"))
344 if (!strcmp(value
, "1.0"))
346 else if (!strcmp(value
, "1.1"))
351 _("ERROR: Unknown version option value \"%s\"!\n"),
356 else if (!strcasecmp(name
, "compression"))
358 compression
= !strcasecmp(value
, "true") ||
359 !strcasecmp(value
, "yes") ||
360 !strcasecmp(value
, "on") ||
361 !strcasecmp(value
, "gzip");
363 #endif /* HAVE_LIBZ */
364 else if (!strcasecmp(name
, "contimeout"))
367 * Set the connection timeout...
371 contimeout
= atoi(value
);
379 fprintf(stderr
, _("ERROR: Unknown option \"%s\" with value \"%s\"!\n"),
386 * If we have 7 arguments, print the file named on the command-line.
387 * Otherwise, copy stdin to a temporary file and print the temporary
394 * Copy stdin to a temporary file...
397 int fd
; /* File descriptor */
398 cups_file_t
*fp
; /* Temporary file */
399 char buffer
[8192]; /* Buffer for copying */
400 int bytes
; /* Number of bytes read */
403 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
405 perror("ERROR: unable to create temporary file");
406 return (CUPS_BACKEND_FAILED
);
409 if ((fp
= cupsFileOpenFd(fd
, compression
? "w9" : "w")) == NULL
)
411 perror("ERROR: unable to open temporary file");
414 return (CUPS_BACKEND_FAILED
);
417 while ((bytes
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0)
418 if (cupsFileWrite(fp
, buffer
, bytes
) < bytes
)
420 perror("ERROR: unable to write to temporary file");
423 return (CUPS_BACKEND_FAILED
);
429 * Point to the single file from stdin...
432 filename
= tmpfilename
;
439 * Point to the files on the command-line...
442 num_files
= argc
- 6;
447 compress_files(num_files
, files
);
448 #endif /* HAVE_LIBZ */
451 fprintf(stderr
, "DEBUG: %d files to send in job...\n", num_files
);
454 * Set the authentication info, if any...
457 cupsSetPasswordCB(password_cb
);
462 * Use authenticaion information in the device URI...
465 if ((password
= strchr(username
, ':')) != NULL
)
468 cupsSetUser(username
);
473 * Try loading authentication information from the a##### file.
476 const char *request_root
; /* CUPS_REQUESTROOT env var */
477 char afilename
[1024], /* a##### filename */
478 aline
[2048]; /* Line from file */
479 FILE *fp
; /* File pointer */
482 if ((request_root
= getenv("CUPS_REQUESTROOT")) != NULL
)
485 * Try opening authentication cache file...
488 snprintf(afilename
, sizeof(afilename
), "%s/a%05d", request_root
,
490 if ((fp
= fopen(afilename
, "r")) != NULL
)
496 if (fgets(aline
, sizeof(aline
), fp
))
502 i
= sizeof(username
);
503 httpDecode64_2(username
, &i
, aline
);
509 if (fgets(aline
, sizeof(aline
), fp
))
515 i
= sizeof(password
);
516 httpDecode64_2(password
, &i
, aline
);
530 * Try connecting to the remote server...
535 start_time
= time(NULL
);
537 fputs("STATE: +connecting-to-device\n", stderr
);
541 fprintf(stderr
, _("INFO: Connecting to %s on port %d...\n"),
544 if ((http
= httpConnectEncrypt(hostname
, port
, cupsEncryption())) == NULL
)
549 if (getenv("CLASS") != NULL
)
552 * If the CLASS environment variable is set, the job was submitted
553 * to a class and not to a specific queue. In this case, we want
554 * to abort immediately so that the job can be requeued on the next
555 * available printer in the class.
558 fputs(_("INFO: Unable to contact printer, queuing on next "
559 "printer in class...\n"), stderr
);
561 if (argc
== 6 || strcmp(filename
, argv
[6]))
565 * Sleep 5 seconds to keep the job from requeuing too rapidly...
570 return (CUPS_BACKEND_FAILED
);
573 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
574 errno
== EHOSTUNREACH
)
576 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
578 fputs(_("ERROR: Printer not responding!\n"), stderr
);
579 return (CUPS_BACKEND_FAILED
);
585 _("WARNING: recoverable: Network host \'%s\' is busy; will "
586 "retry in %d seconds...\n"),
596 fprintf(stderr
, _("ERROR: Unable to locate printer \'%s\'!\n"),
598 return (CUPS_BACKEND_STOP
);
604 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
605 fputs(_("ERROR: recoverable: Unable to connect to printer; will "
606 "retry in 30 seconds...\n"), stderr
);
614 while (http
== NULL
);
618 if (argc
== 6 || strcmp(filename
, argv
[6]))
621 return (CUPS_BACKEND_FAILED
);
624 fputs("STATE: -connecting-to-device\n", stderr
);
625 fprintf(stderr
, _("INFO: Connected to %s...\n"), hostname
);
628 if (http
->hostaddr
->addr
.sa_family
== AF_INET6
)
629 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
630 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
631 ntohs(http
->hostaddr
->ipv6
.sin6_port
));
633 #endif /* AF_INET6 */
634 if (http
->hostaddr
->addr
.sa_family
== AF_INET
)
635 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4)...\n",
636 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
637 ntohs(http
->hostaddr
->ipv4
.sin_port
));
640 * Build a URI for the printer and fill the standard IPP attributes for
641 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
642 * might contain username:password information...
645 snprintf(uri
, sizeof(uri
), "%s://%s:%d%s", method
, hostname
, port
, resource
);
648 * First validate the destination and see if the device supports multiple
649 * copies. We have to do this because some IPP servers (e.g. HP JetDirect)
650 * don't support the copies attribute...
660 * Build the IPP request...
663 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
664 request
->request
.op
.version
[1] = version
;
666 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
669 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
670 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
677 fputs("DEBUG: Getting supported attributes...\n", stderr
);
679 if ((supported
= cupsDoRequest(http
, request
, resource
)) == NULL
)
680 ipp_status
= cupsLastError();
682 ipp_status
= supported
->request
.status
.status_code
;
684 if (ipp_status
> IPP_OK_CONFLICT
)
686 if (ipp_status
== IPP_PRINTER_BUSY
||
687 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
689 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
691 fputs(_("ERROR: Printer not responding!\n"), stderr
);
692 return (CUPS_BACKEND_FAILED
);
698 _("WARNING: recoverable: Network host \'%s\' is busy; will "
699 "retry in %d seconds...\n"),
702 report_printer_state(supported
);
709 else if ((ipp_status
== IPP_BAD_REQUEST
||
710 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
== 1)
713 * Switch to IPP/1.0...
716 fputs(_("INFO: Printer does not support IPP/1.1, trying IPP/1.0...\n"),
721 else if (ipp_status
== IPP_NOT_FOUND
)
723 fputs(_("ERROR: Destination printer does not exist!\n"), stderr
);
726 ippDelete(supported
);
728 return (CUPS_BACKEND_STOP
);
732 fprintf(stderr
, _("ERROR: Unable to get printer status (%s)!\n"),
733 cupsLastErrorString());
738 ippDelete(supported
);
742 else if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
743 IPP_TAG_RANGE
)) != NULL
)
746 * Has the "copies-supported" attribute - does it have an upper
750 if (copies_sup
->values
[0].range
.upper
<= 1)
751 copies_sup
= NULL
; /* No */
754 format_sup
= ippFindAttribute(supported
, "document-format-supported",
759 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
760 format_sup
->num_values
);
761 for (i
= 0; i
< format_sup
->num_values
; i
++)
762 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
763 format_sup
->values
[i
].string
.text
);
766 report_printer_state(supported
);
768 while (ipp_status
> IPP_OK_CONFLICT
);
771 * See if the printer is accepting jobs and is not stopped; if either
772 * condition is true and we are printing to a class, requeue the job...
775 if (getenv("CLASS") != NULL
)
777 printer_state
= ippFindAttribute(supported
, "printer-state",
779 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
782 if (printer_state
== NULL
||
783 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
785 printer_accepting
== NULL
||
786 !printer_accepting
->values
[0].boolean
)
789 * If the CLASS environment variable is set, the job was submitted
790 * to a class and not to a specific queue. In this case, we want
791 * to abort immediately so that the job can be requeued on the next
792 * available printer in the class.
795 fputs(_("INFO: Unable to contact printer, queuing on next "
796 "printer in class...\n"), stderr
);
798 ippDelete(supported
);
801 if (argc
== 6 || strcmp(filename
, argv
[6]))
805 * Sleep 5 seconds to keep the job from requeuing too rapidly...
810 return (CUPS_BACKEND_FAILED
);
817 * If we've shown a recoverable error make sure the printer proxies
818 * have a chance to see the recovered message. Not pretty but
819 * necessary for now...
822 fputs("INFO: recovered: \n", stderr
);
827 * See if the printer supports multiple copies...
830 copies
= atoi(argv
[4]);
832 if (copies_sup
|| argc
< 7)
834 copies_remaining
= 1;
840 copies_remaining
= copies
;
843 * Then issue the print-job request...
848 while (copies_remaining
> 0)
851 * Build the IPP request...
858 request
= ippNewRequest(IPP_CREATE_JOB
);
860 request
= ippNewRequest(IPP_PRINT_JOB
);
862 request
->request
.op
.version
[1] = version
;
864 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
867 fprintf(stderr
, "DEBUG: printer-uri = \"%s\"\n", uri
);
870 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
871 "requesting-user-name", NULL
, argv
[2]);
873 fprintf(stderr
, "DEBUG: requesting-user-name = \"%s\"\n", argv
[2]);
876 * Only add a "job-name" attribute if the remote server supports
877 * copy generation - some IPP implementations like HP's don't seem
878 * to like UTF-8 job names (STR #1837)...
881 if (argv
[3][0] && copies_sup
)
882 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
885 fprintf(stderr
, "DEBUG: job-name = \"%s\"\n", argv
[3]);
889 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
890 "compression", NULL
, "gzip");
891 #endif /* HAVE_LIBZ */
894 * Handle options on the command-line...
898 num_options
= cupsParseOptions(argv
[5], 0, &options
);
901 if (content_type
!= NULL
&&
902 !strcasecmp(content_type
, "application/pictwps") && num_files
== 1)
904 if (format_sup
!= NULL
)
906 for (i
= 0; i
< format_sup
->num_values
; i
++)
907 if (!strcasecmp(content_type
, format_sup
->values
[i
].string
.text
))
911 if (format_sup
== NULL
|| i
>= format_sup
->num_values
)
914 * Remote doesn't support "application/pictwps" (i.e. it's not MacOS X)
915 * so convert the document to PostScript...
918 if (run_pictwps_filter(argv
, filename
))
919 return (CUPS_BACKEND_FAILED
);
921 filename
= pstmpname
;
924 * Change the MIME type to application/postscript and change the
925 * number of copies to 1...
928 content_type
= "application/postscript";
930 copies_remaining
= 1;
933 #endif /* __APPLE__ */
935 if (content_type
!= NULL
&& format_sup
!= NULL
)
937 for (i
= 0; i
< format_sup
->num_values
; i
++)
938 if (!strcasecmp(content_type
, format_sup
->values
[i
].string
.text
))
941 if (i
< format_sup
->num_values
)
942 num_options
= cupsAddOption("document-format", content_type
,
943 num_options
, &options
);
946 if (copies_sup
&& version
> 0)
949 * Only send options if the destination printer supports the copies
950 * attribute and IPP/1.1. This is a hack for the HP and Lexmark
951 * implementations of IPP, which do not accept extension attributes
952 * and incorrectly report a client-error-bad-request error instead of
953 * the successful-ok-unsupported-attributes status. In short, at least
954 * some HP and Lexmark implementations of IPP are non-compliant.
957 cupsEncodeOptions(request
, num_options
, options
);
959 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies",
963 cupsFreeOptions(num_options
, options
);
966 * If copies aren't supported, then we are likely dealing with an HP
967 * JetDirect. The HP IPP implementation seems to close the connection
968 * after every request - that is, it does *not* implement HTTP Keep-
969 * Alive, which is REQUIRED by HTTP/1.1...
980 response
= cupsDoRequest(http
, request
, resource
);
982 response
= cupsDoFileRequest(http
, request
, resource
, files
[0]);
984 ipp_status
= cupsLastError();
986 if (ipp_status
> IPP_OK_CONFLICT
)
993 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
994 ipp_status
== IPP_PRINTER_BUSY
)
996 fputs(_("INFO: Printer busy; will retry in 10 seconds...\n"), stderr
);
999 else if ((ipp_status
== IPP_BAD_REQUEST
||
1000 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
== 1)
1003 * Switch to IPP/1.0...
1006 fputs(_("INFO: Printer does not support IPP/1.1, trying IPP/1.0...\n"),
1009 httpReconnect(http
);
1012 fprintf(stderr
, _("ERROR: Print file was not accepted (%s)!\n"),
1013 cupsLastErrorString());
1015 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
1016 IPP_TAG_INTEGER
)) == NULL
)
1018 fputs(_("NOTICE: Print file accepted - job ID unknown.\n"), stderr
);
1023 job_id
= job_id_attr
->values
[0].integer
;
1024 fprintf(stderr
, _("NOTICE: Print file accepted - job ID %d.\n"), job_id
);
1027 ippDelete(response
);
1032 if (job_id
&& num_files
> 1)
1034 for (i
= 0; i
< num_files
; i
++)
1036 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
1038 request
->request
.op
.version
[1] = version
;
1040 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1043 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1047 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1048 "requesting-user-name", NULL
, argv
[2]);
1050 if ((i
+ 1) == num_files
)
1051 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
1053 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1054 "document-format", NULL
, content_type
);
1056 ippDelete(cupsDoFileRequest(http
, request
, resource
, files
[i
]));
1058 if (cupsLastError() > IPP_OK_CONFLICT
)
1060 ipp_status
= cupsLastError();
1062 fprintf(stderr
, _("ERROR: Unable to add file %d to job: %s\n"),
1063 job_id
, cupsLastErrorString());
1069 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
1071 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
1072 copies_remaining
--;
1074 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1075 ipp_status
== IPP_PRINTER_BUSY
)
1078 copies_remaining
--;
1081 * Wait for the job to complete...
1084 if (!job_id
|| !waitjob
)
1087 fputs(_("INFO: Waiting for job to complete...\n"), stderr
);
1089 for (; !job_cancelled
;)
1092 * Build an IPP_GET_JOB_ATTRIBUTES request...
1095 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1096 request
->request
.op
.version
[1] = version
;
1098 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1101 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1105 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1106 "requesting-user-name", NULL
, argv
[2]);
1108 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1109 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
1117 httpReconnect(http
);
1119 response
= cupsDoRequest(http
, request
, resource
);
1120 ipp_status
= cupsLastError();
1122 if (ipp_status
== IPP_NOT_FOUND
)
1125 * Job has gone away and/or the server has no job history...
1128 ippDelete(response
);
1130 ipp_status
= IPP_OK
;
1134 if (ipp_status
> IPP_OK_CONFLICT
)
1136 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1137 ipp_status
!= IPP_PRINTER_BUSY
)
1139 ippDelete(response
);
1141 fprintf(stderr
, _("ERROR: Unable to get job %d attributes (%s)!\n"),
1142 job_id
, cupsLastErrorString());
1149 if ((job_state
= ippFindAttribute(response
, "job-state",
1150 IPP_TAG_ENUM
)) != NULL
)
1153 * Stop polling if the job is finished or pending-held...
1156 if (job_state
->values
[0].integer
> IPP_JOB_PROCESSING
||
1157 job_state
->values
[0].integer
== IPP_JOB_HELD
)
1159 if ((job_sheets
= ippFindAttribute(response
,
1160 "job-media-sheets-completed",
1161 IPP_TAG_INTEGER
)) != NULL
)
1162 fprintf(stderr
, "PAGE: total %d\n",
1163 job_sheets
->values
[0].integer
);
1165 ippDelete(response
);
1171 ippDelete(response
);
1174 * Check the printer state and report it if necessary...
1177 check_printer_state(http
, uri
, resource
, argv
[2], version
);
1180 * Wait 10 seconds before polling again...
1188 * Cancel the job as needed...
1191 if (job_cancelled
&& job_id
)
1192 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1195 * Check the printer state and report it if necessary...
1198 check_printer_state(http
, uri
, resource
, argv
[2], version
);
1206 ippDelete(supported
);
1209 * Remove the temporary file(s) if necessary...
1213 unlink(tmpfilename
);
1218 for (i
= 0; i
< num_files
; i
++)
1221 #endif /* HAVE_LIBZ */
1226 #endif /* __APPLE__ */
1229 * Return the queue status...
1232 return (ipp_status
> IPP_OK_CONFLICT
? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
1237 * 'cancel_job()' - Cancel a print job.
1241 cancel_job(http_t
*http
, /* I - HTTP connection */
1242 const char *uri
, /* I - printer-uri */
1243 int id
, /* I - job-id */
1244 const char *resource
, /* I - Resource path */
1245 const char *user
, /* I - requesting-user-name */
1246 int version
) /* I - IPP version */
1248 ipp_t
*request
; /* Cancel-Job request */
1251 fputs(_("INFO: Canceling print job...\n"), stderr
);
1253 request
= ippNewRequest(IPP_CANCEL_JOB
);
1254 request
->request
.op
.version
[1] = version
;
1256 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1258 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1260 if (user
&& user
[0])
1261 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1262 "requesting-user-name", NULL
, user
);
1268 ippDelete(cupsDoRequest(http
, request
, resource
));
1270 if (cupsLastError() > IPP_OK_CONFLICT
)
1271 fprintf(stderr
, _("ERROR: Unable to cancel job %d: %s\n"), id
,
1272 cupsLastErrorString());
1277 * 'check_printer_state()' - Check the printer state...
1281 check_printer_state(
1282 http_t
*http
, /* I - HTTP connection */
1283 const char *uri
, /* I - Printer URI */
1284 const char *resource
, /* I - Resource path */
1285 const char *user
, /* I - Username, if any */
1286 int version
) /* I - IPP version */
1288 ipp_t
*request
, /* IPP request */
1289 *response
; /* IPP response */
1290 static const char * const attrs
[] = /* Attributes we want */
1292 "printer-state-message",
1293 "printer-state-reasons"
1298 * Check on the printer state...
1301 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1302 request
->request
.op
.version
[1] = version
;
1304 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1307 if (user
&& user
[0])
1308 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1309 "requesting-user-name", NULL
, user
);
1311 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1312 "requested-attributes",
1313 (int)(sizeof(attrs
) / sizeof(attrs
[0])), NULL
, attrs
);
1319 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
1321 report_printer_state(response
);
1322 ippDelete(response
);
1329 * 'compress_files()' - Compress print files...
1333 compress_files(int num_files
, /* I - Number of files */
1334 char **files
) /* I - Files */
1336 int i
, /* Looping var */
1337 fd
; /* Temporary file descriptor */
1338 ssize_t bytes
; /* Bytes read/written */
1339 size_t total
; /* Total bytes read */
1340 cups_file_t
*in
, /* Input file */
1341 *out
; /* Output file */
1342 struct stat outinfo
; /* Output file information */
1343 char filename
[1024], /* Temporary filename */
1344 buffer
[32768]; /* Copy buffer */
1347 fprintf(stderr
, "DEBUG: Compressing %d job files...\n", num_files
);
1348 for (i
= 0; i
< num_files
; i
++)
1350 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
1353 _("ERROR: Unable to create temporary compressed print file: %s\n"),
1355 exit(CUPS_BACKEND_FAILED
);
1358 if ((out
= cupsFileOpenFd(fd
, "w9")) == NULL
)
1361 _("ERROR: Unable to open temporary compressed print file: %s\n"),
1363 exit(CUPS_BACKEND_FAILED
);
1366 if ((in
= cupsFileOpen(files
[i
], "r")) == NULL
)
1368 fprintf(stderr
, _("ERROR: Unable to open print file \"%s\": %s\n"),
1369 files
[i
], strerror(errno
));
1371 exit(CUPS_BACKEND_FAILED
);
1375 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
1376 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
1378 fprintf(stderr
, _("ERROR: Unable to write %d bytes to \"%s\": %s\n"),
1379 (int)bytes
, filename
, strerror(errno
));
1382 exit(CUPS_BACKEND_FAILED
);
1390 files
[i
] = strdup(filename
);
1392 if (!stat(filename
, &outinfo
))
1394 "DEBUG: File %d compressed to %.1f%% of original size, "
1395 CUPS_LLFMT
" bytes...\n",
1396 i
+ 1, 100.0 * outinfo
.st_size
/ total
,
1397 CUPS_LLCAST outinfo
.st_size
);
1400 #endif /* HAVE_LIBZ */
1404 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
1407 static const char * /* O - Password */
1408 password_cb(const char *prompt
) /* I - Prompt (not used) */
1417 * If there is no password set in the device URI, return the
1418 * "authentication required" exit code...
1422 unlink(tmpfilename
);
1427 #endif /* __APPLE__ */
1429 fputs("ATTR: auth-info-required=username,password\n", stderr
);
1431 exit(CUPS_BACKEND_AUTH_REQUIRED
);
1433 return (NULL
); /* Eliminate compiler warning */
1439 * 'report_printer_state()' - Report the printer state.
1442 static int /* O - Number of reasons shown */
1443 report_printer_state(ipp_t
*ipp
) /* I - IPP response */
1445 int i
; /* Looping var */
1446 int count
; /* Count of reasons shown... */
1447 ipp_attribute_t
*psm
, /* pritner-state-message */
1448 *reasons
; /* printer-state-reasons */
1449 const char *reason
; /* Current reason */
1450 const char *message
; /* Message to show */
1451 char unknown
[1024]; /* Unknown message string */
1452 const char *prefix
; /* Prefix for STATE: line */
1453 char state
[1024]; /* State string */
1456 if ((psm
= ippFindAttribute(ipp
, "printer-state-message",
1457 IPP_TAG_TEXT
)) != NULL
)
1458 fprintf(stderr
, "INFO: %s\n", psm
->values
[0].string
.text
);
1460 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
1461 IPP_TAG_KEYWORD
)) == NULL
)
1467 for (i
= 0, count
= 0; i
< reasons
->num_values
; i
++)
1469 reason
= reasons
->values
[i
].string
.text
;
1471 strlcat(state
, prefix
, sizeof(state
));
1472 strlcat(state
, reason
, sizeof(state
));
1477 if (!strncmp(reason
, "media-needed", 12))
1478 message
= _("Media tray needs to be filled.");
1479 else if (!strncmp(reason
, "media-jam", 9))
1480 message
= _("Media jam!");
1481 else if (!strncmp(reason
, "moving-to-paused", 16) ||
1482 !strncmp(reason
, "paused", 6) ||
1483 !strncmp(reason
, "shutdown", 8))
1484 message
= _("Printer off-line.");
1485 else if (!strncmp(reason
, "toner-low", 9))
1486 message
= _("Toner low.");
1487 else if (!strncmp(reason
, "toner-empty", 11))
1488 message
= _("Out of toner!");
1489 else if (!strncmp(reason
, "cover-open", 10))
1490 message
= _("Cover open.");
1491 else if (!strncmp(reason
, "interlock-open", 14))
1492 message
= _("Interlock open.");
1493 else if (!strncmp(reason
, "door-open", 9))
1494 message
= _("Door open.");
1495 else if (!strncmp(reason
, "input-tray-missing", 18))
1496 message
= _("Media tray missing!");
1497 else if (!strncmp(reason
, "media-low", 9))
1498 message
= _("Media tray almost empty.");
1499 else if (!strncmp(reason
, "media-empty", 11))
1500 message
= _("Media tray empty!");
1501 else if (!strncmp(reason
, "output-tray-missing", 19))
1502 message
= _("Output tray missing!");
1503 else if (!strncmp(reason
, "output-area-almost-full", 23))
1504 message
= _("Output bin almost full.");
1505 else if (!strncmp(reason
, "output-area-full", 16))
1506 message
= _("Output bin full!");
1507 else if (!strncmp(reason
, "marker-supply-low", 17))
1508 message
= _("Ink/toner almost empty.");
1509 else if (!strncmp(reason
, "marker-supply-empty", 19))
1510 message
= _("Ink/toner empty!");
1511 else if (!strncmp(reason
, "marker-waste-almost-full", 24))
1512 message
= _("Ink/toner waste bin almost full.");
1513 else if (!strncmp(reason
, "marker-waste-full", 17))
1514 message
= _("Ink/toner waste bin full!");
1515 else if (!strncmp(reason
, "fuser-over-temp", 15))
1516 message
= _("Fuser temperature high!");
1517 else if (!strncmp(reason
, "fuser-under-temp", 16))
1518 message
= _("Fuser temperature low!");
1519 else if (!strncmp(reason
, "opc-near-eol", 12))
1520 message
= _("OPC almost at end-of-life.");
1521 else if (!strncmp(reason
, "opc-life-over", 13))
1522 message
= _("OPC at end-of-life!");
1523 else if (!strncmp(reason
, "developer-low", 13))
1524 message
= _("Developer almost empty.");
1525 else if (!strncmp(reason
, "developer-empty", 15))
1526 message
= _("Developer empty!");
1527 else if (strstr(reason
, "error") != NULL
)
1531 snprintf(unknown
, sizeof(unknown
), _("Unknown printer error (%s)!"),
1538 if (strstr(reasons
->values
[i
].string
.text
, "error"))
1539 fprintf(stderr
, "ERROR: %s\n", message
);
1540 else if (strstr(reasons
->values
[i
].string
.text
, "warning"))
1541 fprintf(stderr
, "WARNING: %s\n", message
);
1543 fprintf(stderr
, "INFO: %s\n", message
);
1547 fprintf(stderr
, "%s\n", state
);
1555 * 'run_pictwps_filter()' - Convert PICT files to PostScript when printing
1558 * This step is required because the PICT format is not documented and
1559 * subject to change, so developing a filter for other OS's is infeasible.
1560 * Also, fonts required by the PICT file need to be embedded on the
1561 * client side (which has the fonts), so we run the filter to get a
1562 * PostScript file for printing...
1565 static int /* O - Exit status of filter */
1566 run_pictwps_filter(char **argv
, /* I - Command-line arguments */
1567 const char *filename
)/* I - Filename */
1569 struct stat fileinfo
; /* Print file information */
1570 const char *ppdfile
; /* PPD file for destination printer */
1571 int pid
; /* Child process ID */
1572 int fd
; /* Temporary file descriptor */
1573 int status
; /* Exit status of filter */
1574 const char *printer
; /* PRINTER env var */
1575 static char ppdenv
[1024]; /* PPD environment variable */
1579 * First get the PPD file for the printer...
1582 printer
= getenv("PRINTER");
1585 fputs(_("ERROR: PRINTER environment variable not defined!\n"), stderr
);
1589 if ((ppdfile
= cupsGetPPD(printer
)) == NULL
)
1592 _("ERROR: Unable to get PPD file for printer \"%s\" - %s.\n"),
1593 printer
, cupsLastErrorString());
1597 snprintf(ppdenv
, sizeof(ppdenv
), "PPD=%s", ppdfile
);
1602 * Then create a temporary file for printing...
1605 if ((fd
= cupsTempFd(pstmpname
, sizeof(pstmpname
))) < 0)
1607 fprintf(stderr
, _("ERROR: Unable to create temporary file - %s.\n"),
1615 * Get the owner of the spool file - it is owned by the user we want to run
1620 stat(argv
[6], &fileinfo
);
1624 * Use the OSX defaults, as an up-stream filter created the PICT
1628 fileinfo
.st_uid
= 1;
1629 fileinfo
.st_gid
= 80;
1633 chown(ppdfile
, fileinfo
.st_uid
, fileinfo
.st_gid
);
1635 fchown(fd
, fileinfo
.st_uid
, fileinfo
.st_gid
);
1638 * Finally, run the filter to convert the file...
1641 if ((pid
= fork()) == 0)
1644 * Child process for pictwpstops... Redirect output of pictwpstops to a
1655 * Change to an unpriviledged user...
1658 setgid(fileinfo
.st_gid
);
1659 setuid(fileinfo
.st_uid
);
1662 execlp("pictwpstops", printer
, argv
[1], argv
[2], argv
[3], argv
[4], argv
[5],
1664 fprintf(stderr
, _("ERROR: Unable to exec pictwpstops: %s\n"),
1677 fprintf(stderr
, _("ERROR: Unable to fork pictwpstops: %s\n"),
1686 * Now wait for the filter to complete...
1689 if (wait(&status
) < 0)
1691 fprintf(stderr
, _("ERROR: Unable to wait for pictwpstops: %s\n"),
1708 fprintf(stderr
, _("ERROR: pictwpstops exited with status %d!\n"),
1711 fprintf(stderr
, _("ERROR: pictwpstops exited on signal %d!\n"),
1719 * Return with no errors..
1724 #endif /* __APPLE__ */
1728 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1732 sigterm_handler(int sig
) /* I - Signal */
1734 (void)sig
; /* remove compiler warnings... */
1739 * Flag that the job should be cancelled...
1747 * The scheduler already tried to cancel us once, now just terminate
1748 * after removing our temp files!
1752 unlink(tmpfilename
);
1757 #endif /* __APPLE__ */
1764 * End of "$Id: ipp.c 6434 2007-04-02 22:07:10Z mike $".