2 * "$Id: ipp.c 6482 2007-04-30 17:05:59Z 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 send_options
; /* Send job options? */
105 int num_options
; /* Number of printer options */
106 cups_option_t
*options
; /* Printer options */
107 char method
[255], /* Method in URI */
108 hostname
[1024], /* Hostname */
109 username
[255], /* Username info */
110 resource
[1024], /* Resource info (printer name) */
111 addrname
[256], /* Address name */
112 *optptr
, /* Pointer to URI options */
113 name
[255], /* Name of option */
114 value
[255], /* Value of option */
115 *ptr
; /* Pointer into name or value */
116 int num_files
; /* Number of files to print */
117 char **files
, /* Files to print */
118 *filename
; /* Pointer to single filename */
119 int port
; /* Port number (not used) */
120 char uri
[HTTP_MAX_URI
]; /* Updated URI without user/pass */
121 ipp_status_t ipp_status
; /* Status of IPP request */
122 http_t
*http
; /* HTTP connection */
123 ipp_t
*request
, /* IPP request */
124 *response
, /* IPP response */
125 *supported
; /* get-printer-attributes response */
126 time_t start_time
; /* Time of first connect */
127 int recoverable
; /* Recoverable error shown? */
128 int contimeout
; /* Connection timeout */
129 int delay
; /* Delay for retries... */
130 int compression
, /* Do compression of the job data? */
131 waitjob
, /* Wait for job complete? */
132 waitprinter
; /* Wait for printer ready? */
133 ipp_attribute_t
*job_id_attr
; /* job-id attribute */
134 int job_id
; /* job-id value */
135 ipp_attribute_t
*job_sheets
; /* job-media-sheets-completed */
136 ipp_attribute_t
*job_state
; /* job-state */
137 ipp_attribute_t
*copies_sup
; /* copies-supported */
138 ipp_attribute_t
*format_sup
; /* document-format-supported */
139 ipp_attribute_t
*printer_state
; /* printer-state attribute */
140 ipp_attribute_t
*printer_accepting
; /* printer-is-accepting-jobs */
141 int copies
, /* Number of copies for job */
142 copies_remaining
; /* Number of copies remaining */
143 const char *content_type
; /* CONTENT_TYPE environment variable */
144 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
145 struct sigaction action
; /* Actions for POSIX signals */
146 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
147 int version
; /* IPP version */
148 static const char * const pattrs
[] =
149 { /* Printer attributes we want */
151 "document-format-supported",
152 "printer-is-accepting-jobs",
154 "printer-state-message",
155 "printer-state-reasons",
157 static const char * const jattrs
[] =
158 { /* Job attributes we want */
159 "job-media-sheets-completed",
165 * Make sure status messages are not buffered...
168 setbuf(stderr
, NULL
);
171 * Ignore SIGPIPE and catch SIGTERM signals...
175 sigset(SIGPIPE
, SIG_IGN
);
176 sigset(SIGTERM
, sigterm_handler
);
177 #elif defined(HAVE_SIGACTION)
178 memset(&action
, 0, sizeof(action
));
179 action
.sa_handler
= SIG_IGN
;
180 sigaction(SIGPIPE
, &action
, NULL
);
182 sigemptyset(&action
.sa_mask
);
183 sigaddset(&action
.sa_mask
, SIGTERM
);
184 action
.sa_handler
= sigterm_handler
;
185 sigaction(SIGTERM
, &action
, NULL
);
187 signal(SIGPIPE
, SIG_IGN
);
188 signal(SIGTERM
, sigterm_handler
);
189 #endif /* HAVE_SIGSET */
192 * Check command-line...
199 if ((s
= strrchr(argv
[0], '/')) != NULL
)
204 printf("network %s \"Unknown\" \"Internet Printing Protocol (%s)\"\n",
206 return (CUPS_BACKEND_OK
);
210 fprintf(stderr
, _("Usage: %s job-id user title copies options [file]\n"),
212 return (CUPS_BACKEND_STOP
);
216 * Get the (final) content type...
219 if ((content_type
= getenv("FINAL_CONTENT_TYPE")) == NULL
)
220 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
221 content_type
= "application/octet-stream";
223 if (!strncmp(content_type
, "printer/", 8))
224 content_type
= "application/vnd.cups-raw";
227 * Extract the hostname and printer name from the URI...
230 if (httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
231 method
, sizeof(method
), username
, sizeof(username
),
232 hostname
, sizeof(hostname
), &port
,
233 resource
, sizeof(resource
)) < HTTP_URI_OK
)
235 fputs(_("ERROR: Missing device URI on command-line and no "
236 "DEVICE_URI environment variable!\n"), stderr
);
237 return (CUPS_BACKEND_STOP
);
240 if (!strcmp(method
, "https"))
241 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
243 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
246 * See if there are any options...
253 contimeout
= 7 * 24 * 60 * 60;
255 if ((optptr
= strchr(resource
, '?')) != NULL
)
258 * Yup, terminate the device name string and move to the first
259 * character of the optptr...
265 * Then parse the optptr...
274 for (ptr
= name
; *optptr
&& *optptr
!= '=';)
275 if (ptr
< (name
+ sizeof(name
) - 1))
287 for (ptr
= value
; *optptr
&& *optptr
!= '+' && *optptr
!= '&';)
288 if (ptr
< (value
+ sizeof(value
) - 1))
292 if (*optptr
== '+' || *optptr
== '&')
299 * Process the option...
302 if (!strcasecmp(name
, "waitjob"))
305 * Wait for job completion?
308 waitjob
= !strcasecmp(value
, "on") ||
309 !strcasecmp(value
, "yes") ||
310 !strcasecmp(value
, "true");
312 else if (!strcasecmp(name
, "waitprinter"))
315 * Wait for printer idle?
318 waitprinter
= !strcasecmp(value
, "on") ||
319 !strcasecmp(value
, "yes") ||
320 !strcasecmp(value
, "true");
322 else if (!strcasecmp(name
, "encryption"))
325 * Enable/disable encryption?
328 if (!strcasecmp(value
, "always"))
329 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
330 else if (!strcasecmp(value
, "required"))
331 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
332 else if (!strcasecmp(value
, "never"))
333 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
334 else if (!strcasecmp(value
, "ifrequested"))
335 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
339 _("ERROR: Unknown encryption option value \"%s\"!\n"),
343 else if (!strcasecmp(name
, "version"))
345 if (!strcmp(value
, "1.0"))
347 else if (!strcmp(value
, "1.1"))
352 _("ERROR: Unknown version option value \"%s\"!\n"),
357 else if (!strcasecmp(name
, "compression"))
359 compression
= !strcasecmp(value
, "true") ||
360 !strcasecmp(value
, "yes") ||
361 !strcasecmp(value
, "on") ||
362 !strcasecmp(value
, "gzip");
364 #endif /* HAVE_LIBZ */
365 else if (!strcasecmp(name
, "contimeout"))
368 * Set the connection timeout...
372 contimeout
= atoi(value
);
380 fprintf(stderr
, _("ERROR: Unknown option \"%s\" with value \"%s\"!\n"),
387 * If we have 7 arguments, print the file named on the command-line.
388 * Otherwise, copy stdin to a temporary file and print the temporary
395 * Copy stdin to a temporary file...
398 int fd
; /* File descriptor */
399 cups_file_t
*fp
; /* Temporary file */
400 char buffer
[8192]; /* Buffer for copying */
401 int bytes
; /* Number of bytes read */
404 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
406 perror("ERROR: unable to create temporary file");
407 return (CUPS_BACKEND_FAILED
);
410 if ((fp
= cupsFileOpenFd(fd
, compression
? "w9" : "w")) == NULL
)
412 perror("ERROR: unable to open temporary file");
415 return (CUPS_BACKEND_FAILED
);
418 while ((bytes
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0)
419 if (cupsFileWrite(fp
, buffer
, bytes
) < bytes
)
421 perror("ERROR: unable to write to temporary file");
424 return (CUPS_BACKEND_FAILED
);
430 * Point to the single file from stdin...
433 filename
= tmpfilename
;
442 * Point to the files on the command-line...
445 num_files
= argc
- 6;
448 send_options
= strncasecmp(content_type
, "application/vnd.cups-", 21) != 0;
452 compress_files(num_files
, files
);
453 #endif /* HAVE_LIBZ */
456 fprintf(stderr
, "DEBUG: %d files to send in job...\n", num_files
);
459 * Set the authentication info, if any...
462 cupsSetPasswordCB(password_cb
);
467 * Use authenticaion information in the device URI...
470 if ((password
= strchr(username
, ':')) != NULL
)
473 cupsSetUser(username
);
478 * Try loading authentication information from the a##### file.
481 const char *request_root
; /* CUPS_REQUESTROOT env var */
482 char afilename
[1024], /* a##### filename */
483 aline
[2048]; /* Line from file */
484 FILE *fp
; /* File pointer */
487 if ((request_root
= getenv("CUPS_REQUESTROOT")) != NULL
)
490 * Try opening authentication cache file...
493 snprintf(afilename
, sizeof(afilename
), "%s/a%05d", request_root
,
495 if ((fp
= fopen(afilename
, "r")) != NULL
)
501 if (fgets(aline
, sizeof(aline
), fp
))
507 i
= sizeof(username
);
508 httpDecode64_2(username
, &i
, aline
);
514 if (fgets(aline
, sizeof(aline
), fp
))
520 i
= sizeof(password
);
521 httpDecode64_2(password
, &i
, aline
);
535 * Try connecting to the remote server...
540 start_time
= time(NULL
);
542 fputs("STATE: +connecting-to-device\n", stderr
);
546 fprintf(stderr
, _("INFO: Connecting to %s on port %d...\n"),
549 if ((http
= httpConnectEncrypt(hostname
, port
, cupsEncryption())) == NULL
)
554 if (getenv("CLASS") != NULL
)
557 * If the CLASS environment variable is set, the job was submitted
558 * to a class and not to a specific queue. In this case, we want
559 * to abort immediately so that the job can be requeued on the next
560 * available printer in the class.
563 fputs(_("INFO: Unable to contact printer, queuing on next "
564 "printer in class...\n"), stderr
);
566 if (argc
== 6 || strcmp(filename
, argv
[6]))
570 * Sleep 5 seconds to keep the job from requeuing too rapidly...
575 return (CUPS_BACKEND_FAILED
);
578 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
579 errno
== EHOSTUNREACH
)
581 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
583 fputs(_("ERROR: Printer not responding!\n"), stderr
);
584 return (CUPS_BACKEND_FAILED
);
590 _("WARNING: recoverable: Network host \'%s\' is busy; will "
591 "retry in %d seconds...\n"),
601 fprintf(stderr
, _("ERROR: Unable to locate printer \'%s\'!\n"),
603 return (CUPS_BACKEND_STOP
);
609 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
610 fputs(_("ERROR: recoverable: Unable to connect to printer; will "
611 "retry in 30 seconds...\n"), stderr
);
619 while (http
== NULL
);
623 if (argc
== 6 || strcmp(filename
, argv
[6]))
626 return (CUPS_BACKEND_FAILED
);
629 fputs("STATE: -connecting-to-device\n", stderr
);
630 fprintf(stderr
, _("INFO: Connected to %s...\n"), hostname
);
633 if (http
->hostaddr
->addr
.sa_family
== AF_INET6
)
634 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
635 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
636 ntohs(http
->hostaddr
->ipv6
.sin6_port
));
638 #endif /* AF_INET6 */
639 if (http
->hostaddr
->addr
.sa_family
== AF_INET
)
640 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4)...\n",
641 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
642 ntohs(http
->hostaddr
->ipv4
.sin_port
));
645 * Build a URI for the printer and fill the standard IPP attributes for
646 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
647 * might contain username:password information...
650 snprintf(uri
, sizeof(uri
), "%s://%s:%d%s", method
, hostname
, port
, resource
);
653 * First validate the destination and see if the device supports multiple
654 * copies. We have to do this because some IPP servers (e.g. HP JetDirect)
655 * don't support the copies attribute...
665 * Build the IPP request...
668 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
669 request
->request
.op
.version
[1] = version
;
671 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
674 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
675 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
682 fputs("DEBUG: Getting supported attributes...\n", stderr
);
684 if ((supported
= cupsDoRequest(http
, request
, resource
)) == NULL
)
685 ipp_status
= cupsLastError();
687 ipp_status
= supported
->request
.status
.status_code
;
689 if (ipp_status
> IPP_OK_CONFLICT
)
691 if (ipp_status
== IPP_PRINTER_BUSY
||
692 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
694 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
696 fputs(_("ERROR: Printer not responding!\n"), stderr
);
697 return (CUPS_BACKEND_FAILED
);
703 _("WARNING: recoverable: Network host \'%s\' is busy; will "
704 "retry in %d seconds...\n"),
707 report_printer_state(supported
);
714 else if ((ipp_status
== IPP_BAD_REQUEST
||
715 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
== 1)
718 * Switch to IPP/1.0...
721 fputs(_("INFO: Printer does not support IPP/1.1, trying IPP/1.0...\n"),
726 else if (ipp_status
== IPP_NOT_FOUND
)
728 fputs(_("ERROR: Destination printer does not exist!\n"), stderr
);
731 ippDelete(supported
);
733 return (CUPS_BACKEND_STOP
);
737 fprintf(stderr
, _("ERROR: Unable to get printer status (%s)!\n"),
738 cupsLastErrorString());
743 ippDelete(supported
);
747 else if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
748 IPP_TAG_RANGE
)) != NULL
)
751 * Has the "copies-supported" attribute - does it have an upper
755 if (copies_sup
->values
[0].range
.upper
<= 1)
756 copies_sup
= NULL
; /* No */
759 format_sup
= ippFindAttribute(supported
, "document-format-supported",
764 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
765 format_sup
->num_values
);
766 for (i
= 0; i
< format_sup
->num_values
; i
++)
767 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
768 format_sup
->values
[i
].string
.text
);
771 report_printer_state(supported
);
773 while (ipp_status
> IPP_OK_CONFLICT
);
776 * See if the printer is accepting jobs and is not stopped; if either
777 * condition is true and we are printing to a class, requeue the job...
780 if (getenv("CLASS") != NULL
)
782 printer_state
= ippFindAttribute(supported
, "printer-state",
784 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
787 if (printer_state
== NULL
||
788 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
790 printer_accepting
== NULL
||
791 !printer_accepting
->values
[0].boolean
)
794 * If the CLASS environment variable is set, the job was submitted
795 * to a class and not to a specific queue. In this case, we want
796 * to abort immediately so that the job can be requeued on the next
797 * available printer in the class.
800 fputs(_("INFO: Unable to contact printer, queuing on next "
801 "printer in class...\n"), stderr
);
803 ippDelete(supported
);
806 if (argc
== 6 || strcmp(filename
, argv
[6]))
810 * Sleep 5 seconds to keep the job from requeuing too rapidly...
815 return (CUPS_BACKEND_FAILED
);
822 * If we've shown a recoverable error make sure the printer proxies
823 * have a chance to see the recovered message. Not pretty but
824 * necessary for now...
827 fputs("INFO: recovered: \n", stderr
);
832 * See if the printer supports multiple copies...
835 copies
= atoi(argv
[4]);
837 if (copies_sup
|| argc
< 7)
839 copies_remaining
= 1;
845 copies_remaining
= copies
;
848 * Then issue the print-job request...
853 while (copies_remaining
> 0)
856 * Build the IPP request...
863 request
= ippNewRequest(IPP_CREATE_JOB
);
865 request
= ippNewRequest(IPP_PRINT_JOB
);
867 request
->request
.op
.version
[1] = version
;
869 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
872 fprintf(stderr
, "DEBUG: printer-uri = \"%s\"\n", uri
);
875 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
876 "requesting-user-name", NULL
, argv
[2]);
878 fprintf(stderr
, "DEBUG: requesting-user-name = \"%s\"\n", argv
[2]);
881 * Only add a "job-name" attribute if the remote server supports
882 * copy generation - some IPP implementations like HP's don't seem
883 * to like UTF-8 job names (STR #1837)...
886 if (argv
[3][0] && copies_sup
)
887 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
890 fprintf(stderr
, "DEBUG: job-name = \"%s\"\n", argv
[3]);
894 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
895 "compression", NULL
, "gzip");
896 #endif /* HAVE_LIBZ */
899 * Handle options on the command-line...
903 num_options
= cupsParseOptions(argv
[5], 0, &options
);
906 if (content_type
!= NULL
&&
907 !strcasecmp(content_type
, "application/pictwps") && num_files
== 1)
909 if (format_sup
!= NULL
)
911 for (i
= 0; i
< format_sup
->num_values
; i
++)
912 if (!strcasecmp(content_type
, format_sup
->values
[i
].string
.text
))
916 if (format_sup
== NULL
|| i
>= format_sup
->num_values
)
919 * Remote doesn't support "application/pictwps" (i.e. it's not MacOS X)
920 * so convert the document to PostScript...
923 if (run_pictwps_filter(argv
, filename
))
924 return (CUPS_BACKEND_FAILED
);
926 filename
= pstmpname
;
929 * Change the MIME type to application/postscript and change the
930 * number of copies to 1...
933 content_type
= "application/postscript";
935 copies_remaining
= 1;
939 #endif /* __APPLE__ */
941 if (content_type
!= NULL
&& format_sup
!= NULL
)
943 for (i
= 0; i
< format_sup
->num_values
; i
++)
944 if (!strcasecmp(content_type
, format_sup
->values
[i
].string
.text
))
947 if (i
< format_sup
->num_values
)
948 num_options
= cupsAddOption("document-format", content_type
,
949 num_options
, &options
);
952 if (copies_sup
&& version
> 0 && send_options
)
955 * Only send options if the destination printer supports the copies
956 * attribute and IPP/1.1. This is a hack for the HP and Lexmark
957 * implementations of IPP, which do not accept extension attributes
958 * and incorrectly report a client-error-bad-request error instead of
959 * the successful-ok-unsupported-attributes status. In short, at least
960 * some HP and Lexmark implementations of IPP are non-compliant.
963 cupsEncodeOptions(request
, num_options
, options
);
965 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies",
969 cupsFreeOptions(num_options
, options
);
972 * If copies aren't supported, then we are likely dealing with an HP
973 * JetDirect. The HP IPP implementation seems to close the connection
974 * after every request - that is, it does *not* implement HTTP Keep-
975 * Alive, which is REQUIRED by HTTP/1.1...
986 response
= cupsDoRequest(http
, request
, resource
);
988 response
= cupsDoFileRequest(http
, request
, resource
, files
[0]);
990 ipp_status
= cupsLastError();
992 if (ipp_status
> IPP_OK_CONFLICT
)
999 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1000 ipp_status
== IPP_PRINTER_BUSY
)
1002 fputs(_("INFO: Printer busy; will retry in 10 seconds...\n"), stderr
);
1005 else if ((ipp_status
== IPP_BAD_REQUEST
||
1006 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
== 1)
1009 * Switch to IPP/1.0...
1012 fputs(_("INFO: Printer does not support IPP/1.1, trying IPP/1.0...\n"),
1015 httpReconnect(http
);
1018 fprintf(stderr
, _("ERROR: Print file was not accepted (%s)!\n"),
1019 cupsLastErrorString());
1021 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
1022 IPP_TAG_INTEGER
)) == NULL
)
1024 fputs(_("NOTICE: Print file accepted - job ID unknown.\n"), stderr
);
1029 job_id
= job_id_attr
->values
[0].integer
;
1030 fprintf(stderr
, _("NOTICE: Print file accepted - job ID %d.\n"), job_id
);
1033 ippDelete(response
);
1038 if (job_id
&& num_files
> 1)
1040 for (i
= 0; i
< num_files
; i
++)
1042 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
1044 request
->request
.op
.version
[1] = version
;
1046 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1049 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1053 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1054 "requesting-user-name", NULL
, argv
[2]);
1056 if ((i
+ 1) == num_files
)
1057 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
1059 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1060 "document-format", NULL
, content_type
);
1062 ippDelete(cupsDoFileRequest(http
, request
, resource
, files
[i
]));
1064 if (cupsLastError() > IPP_OK_CONFLICT
)
1066 ipp_status
= cupsLastError();
1068 fprintf(stderr
, _("ERROR: Unable to add file %d to job: %s\n"),
1069 job_id
, cupsLastErrorString());
1075 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
1077 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
1078 copies_remaining
--;
1080 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1081 ipp_status
== IPP_PRINTER_BUSY
)
1084 copies_remaining
--;
1087 * Wait for the job to complete...
1090 if (!job_id
|| !waitjob
)
1093 fputs(_("INFO: Waiting for job to complete...\n"), stderr
);
1095 for (; !job_cancelled
;)
1098 * Build an IPP_GET_JOB_ATTRIBUTES request...
1101 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1102 request
->request
.op
.version
[1] = version
;
1104 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1107 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1111 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1112 "requesting-user-name", NULL
, argv
[2]);
1114 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1115 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
1123 httpReconnect(http
);
1125 response
= cupsDoRequest(http
, request
, resource
);
1126 ipp_status
= cupsLastError();
1128 if (ipp_status
== IPP_NOT_FOUND
)
1131 * Job has gone away and/or the server has no job history...
1134 ippDelete(response
);
1136 ipp_status
= IPP_OK
;
1140 if (ipp_status
> IPP_OK_CONFLICT
)
1142 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1143 ipp_status
!= IPP_PRINTER_BUSY
)
1145 ippDelete(response
);
1147 fprintf(stderr
, _("ERROR: Unable to get job %d attributes (%s)!\n"),
1148 job_id
, cupsLastErrorString());
1155 if ((job_state
= ippFindAttribute(response
, "job-state",
1156 IPP_TAG_ENUM
)) != NULL
)
1159 * Stop polling if the job is finished or pending-held...
1162 if (job_state
->values
[0].integer
> IPP_JOB_STOPPED
)
1164 if ((job_sheets
= ippFindAttribute(response
,
1165 "job-media-sheets-completed",
1166 IPP_TAG_INTEGER
)) != NULL
)
1167 fprintf(stderr
, "PAGE: total %d\n",
1168 job_sheets
->values
[0].integer
);
1170 ippDelete(response
);
1176 ippDelete(response
);
1179 * Check the printer state and report it if necessary...
1182 check_printer_state(http
, uri
, resource
, argv
[2], version
);
1185 * Wait 10 seconds before polling again...
1193 * Cancel the job as needed...
1196 if (job_cancelled
&& job_id
)
1197 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1200 * Check the printer state and report it if necessary...
1203 check_printer_state(http
, uri
, resource
, argv
[2], version
);
1211 ippDelete(supported
);
1214 * Remove the temporary file(s) if necessary...
1218 unlink(tmpfilename
);
1223 for (i
= 0; i
< num_files
; i
++)
1226 #endif /* HAVE_LIBZ */
1231 #endif /* __APPLE__ */
1234 * Return the queue status...
1237 return (ipp_status
> IPP_OK_CONFLICT
? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
1242 * 'cancel_job()' - Cancel a print job.
1246 cancel_job(http_t
*http
, /* I - HTTP connection */
1247 const char *uri
, /* I - printer-uri */
1248 int id
, /* I - job-id */
1249 const char *resource
, /* I - Resource path */
1250 const char *user
, /* I - requesting-user-name */
1251 int version
) /* I - IPP version */
1253 ipp_t
*request
; /* Cancel-Job request */
1256 fputs(_("INFO: Canceling print job...\n"), stderr
);
1258 request
= ippNewRequest(IPP_CANCEL_JOB
);
1259 request
->request
.op
.version
[1] = version
;
1261 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1263 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1265 if (user
&& user
[0])
1266 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1267 "requesting-user-name", NULL
, user
);
1273 ippDelete(cupsDoRequest(http
, request
, resource
));
1275 if (cupsLastError() > IPP_OK_CONFLICT
)
1276 fprintf(stderr
, _("ERROR: Unable to cancel job %d: %s\n"), id
,
1277 cupsLastErrorString());
1282 * 'check_printer_state()' - Check the printer state...
1286 check_printer_state(
1287 http_t
*http
, /* I - HTTP connection */
1288 const char *uri
, /* I - Printer URI */
1289 const char *resource
, /* I - Resource path */
1290 const char *user
, /* I - Username, if any */
1291 int version
) /* I - IPP version */
1293 ipp_t
*request
, /* IPP request */
1294 *response
; /* IPP response */
1295 static const char * const attrs
[] = /* Attributes we want */
1297 "printer-state-message",
1298 "printer-state-reasons"
1303 * Check on the printer state...
1306 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1307 request
->request
.op
.version
[1] = version
;
1309 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1312 if (user
&& user
[0])
1313 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1314 "requesting-user-name", NULL
, user
);
1316 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1317 "requested-attributes",
1318 (int)(sizeof(attrs
) / sizeof(attrs
[0])), NULL
, attrs
);
1324 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
1326 report_printer_state(response
);
1327 ippDelete(response
);
1334 * 'compress_files()' - Compress print files...
1338 compress_files(int num_files
, /* I - Number of files */
1339 char **files
) /* I - Files */
1341 int i
, /* Looping var */
1342 fd
; /* Temporary file descriptor */
1343 ssize_t bytes
; /* Bytes read/written */
1344 size_t total
; /* Total bytes read */
1345 cups_file_t
*in
, /* Input file */
1346 *out
; /* Output file */
1347 struct stat outinfo
; /* Output file information */
1348 char filename
[1024], /* Temporary filename */
1349 buffer
[32768]; /* Copy buffer */
1352 fprintf(stderr
, "DEBUG: Compressing %d job files...\n", num_files
);
1353 for (i
= 0; i
< num_files
; i
++)
1355 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
1358 _("ERROR: Unable to create temporary compressed print file: "
1361 exit(CUPS_BACKEND_FAILED
);
1364 if ((out
= cupsFileOpenFd(fd
, "w9")) == NULL
)
1367 _("ERROR: Unable to open temporary compressed print file: %s\n"),
1369 exit(CUPS_BACKEND_FAILED
);
1372 if ((in
= cupsFileOpen(files
[i
], "r")) == NULL
)
1374 fprintf(stderr
, _("ERROR: Unable to open print file \"%s\": %s\n"),
1375 files
[i
], strerror(errno
));
1377 exit(CUPS_BACKEND_FAILED
);
1381 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
1382 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
1384 fprintf(stderr
, _("ERROR: Unable to write %d bytes to \"%s\": %s\n"),
1385 (int)bytes
, filename
, strerror(errno
));
1388 exit(CUPS_BACKEND_FAILED
);
1396 files
[i
] = strdup(filename
);
1398 if (!stat(filename
, &outinfo
))
1400 "DEBUG: File %d compressed to %.1f%% of original size, "
1401 CUPS_LLFMT
" bytes...\n",
1402 i
+ 1, 100.0 * outinfo
.st_size
/ total
,
1403 CUPS_LLCAST outinfo
.st_size
);
1406 #endif /* HAVE_LIBZ */
1410 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
1413 static const char * /* O - Password */
1414 password_cb(const char *prompt
) /* I - Prompt (not used) */
1423 * If there is no password set in the device URI, return the
1424 * "authentication required" exit code...
1428 unlink(tmpfilename
);
1433 #endif /* __APPLE__ */
1435 fputs("ATTR: auth-info-required=username,password\n", stderr
);
1437 exit(CUPS_BACKEND_AUTH_REQUIRED
);
1439 return (NULL
); /* Eliminate compiler warning */
1445 * 'report_printer_state()' - Report the printer state.
1448 static int /* O - Number of reasons shown */
1449 report_printer_state(ipp_t
*ipp
) /* I - IPP response */
1451 int i
; /* Looping var */
1452 int count
; /* Count of reasons shown... */
1453 ipp_attribute_t
*psm
, /* pritner-state-message */
1454 *reasons
; /* printer-state-reasons */
1455 const char *reason
; /* Current reason */
1456 const char *message
; /* Message to show */
1457 char unknown
[1024]; /* Unknown message string */
1458 const char *prefix
; /* Prefix for STATE: line */
1459 char state
[1024]; /* State string */
1462 if ((psm
= ippFindAttribute(ipp
, "printer-state-message",
1463 IPP_TAG_TEXT
)) != NULL
)
1464 fprintf(stderr
, "INFO: %s\n", psm
->values
[0].string
.text
);
1466 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
1467 IPP_TAG_KEYWORD
)) == NULL
)
1473 for (i
= 0, count
= 0; i
< reasons
->num_values
; i
++)
1475 reason
= reasons
->values
[i
].string
.text
;
1477 strlcat(state
, prefix
, sizeof(state
));
1478 strlcat(state
, reason
, sizeof(state
));
1483 if (!strncmp(reason
, "media-needed", 12))
1484 message
= _("Media tray needs to be filled.");
1485 else if (!strncmp(reason
, "media-jam", 9))
1486 message
= _("Media jam!");
1487 else if (!strncmp(reason
, "moving-to-paused", 16) ||
1488 !strncmp(reason
, "paused", 6) ||
1489 !strncmp(reason
, "shutdown", 8))
1490 message
= _("Printer off-line.");
1491 else if (!strncmp(reason
, "toner-low", 9))
1492 message
= _("Toner low.");
1493 else if (!strncmp(reason
, "toner-empty", 11))
1494 message
= _("Out of toner!");
1495 else if (!strncmp(reason
, "cover-open", 10))
1496 message
= _("Cover open.");
1497 else if (!strncmp(reason
, "interlock-open", 14))
1498 message
= _("Interlock open.");
1499 else if (!strncmp(reason
, "door-open", 9))
1500 message
= _("Door open.");
1501 else if (!strncmp(reason
, "input-tray-missing", 18))
1502 message
= _("Media tray missing!");
1503 else if (!strncmp(reason
, "media-low", 9))
1504 message
= _("Media tray almost empty.");
1505 else if (!strncmp(reason
, "media-empty", 11))
1506 message
= _("Media tray empty!");
1507 else if (!strncmp(reason
, "output-tray-missing", 19))
1508 message
= _("Output tray missing!");
1509 else if (!strncmp(reason
, "output-area-almost-full", 23))
1510 message
= _("Output bin almost full.");
1511 else if (!strncmp(reason
, "output-area-full", 16))
1512 message
= _("Output bin full!");
1513 else if (!strncmp(reason
, "marker-supply-low", 17))
1514 message
= _("Ink/toner almost empty.");
1515 else if (!strncmp(reason
, "marker-supply-empty", 19))
1516 message
= _("Ink/toner empty!");
1517 else if (!strncmp(reason
, "marker-waste-almost-full", 24))
1518 message
= _("Ink/toner waste bin almost full.");
1519 else if (!strncmp(reason
, "marker-waste-full", 17))
1520 message
= _("Ink/toner waste bin full!");
1521 else if (!strncmp(reason
, "fuser-over-temp", 15))
1522 message
= _("Fuser temperature high!");
1523 else if (!strncmp(reason
, "fuser-under-temp", 16))
1524 message
= _("Fuser temperature low!");
1525 else if (!strncmp(reason
, "opc-near-eol", 12))
1526 message
= _("OPC almost at end-of-life.");
1527 else if (!strncmp(reason
, "opc-life-over", 13))
1528 message
= _("OPC at end-of-life!");
1529 else if (!strncmp(reason
, "developer-low", 13))
1530 message
= _("Developer almost empty.");
1531 else if (!strncmp(reason
, "developer-empty", 15))
1532 message
= _("Developer empty!");
1533 else if (strstr(reason
, "error") != NULL
)
1537 snprintf(unknown
, sizeof(unknown
), _("Unknown printer error (%s)!"),
1544 if (strstr(reasons
->values
[i
].string
.text
, "error"))
1545 fprintf(stderr
, "ERROR: %s\n", message
);
1546 else if (strstr(reasons
->values
[i
].string
.text
, "warning"))
1547 fprintf(stderr
, "WARNING: %s\n", message
);
1549 fprintf(stderr
, "INFO: %s\n", message
);
1553 fprintf(stderr
, "%s\n", state
);
1561 * 'run_pictwps_filter()' - Convert PICT files to PostScript when printing
1564 * This step is required because the PICT format is not documented and
1565 * subject to change, so developing a filter for other OS's is infeasible.
1566 * Also, fonts required by the PICT file need to be embedded on the
1567 * client side (which has the fonts), so we run the filter to get a
1568 * PostScript file for printing...
1571 static int /* O - Exit status of filter */
1572 run_pictwps_filter(char **argv
, /* I - Command-line arguments */
1573 const char *filename
)/* I - Filename */
1575 struct stat fileinfo
; /* Print file information */
1576 const char *ppdfile
; /* PPD file for destination printer */
1577 int pid
; /* Child process ID */
1578 int fd
; /* Temporary file descriptor */
1579 int status
; /* Exit status of filter */
1580 const char *printer
; /* PRINTER env var */
1581 static char ppdenv
[1024]; /* PPD environment variable */
1585 * First get the PPD file for the printer...
1588 printer
= getenv("PRINTER");
1591 fputs(_("ERROR: PRINTER environment variable not defined!\n"), stderr
);
1595 if ((ppdfile
= cupsGetPPD(printer
)) == NULL
)
1598 _("ERROR: Unable to get PPD file for printer \"%s\" - %s.\n"),
1599 printer
, cupsLastErrorString());
1603 snprintf(ppdenv
, sizeof(ppdenv
), "PPD=%s", ppdfile
);
1608 * Then create a temporary file for printing...
1611 if ((fd
= cupsTempFd(pstmpname
, sizeof(pstmpname
))) < 0)
1613 fprintf(stderr
, _("ERROR: Unable to create temporary file - %s.\n"),
1621 * Get the owner of the spool file - it is owned by the user we want to run
1626 stat(argv
[6], &fileinfo
);
1630 * Use the OSX defaults, as an up-stream filter created the PICT
1634 fileinfo
.st_uid
= 1;
1635 fileinfo
.st_gid
= 80;
1639 chown(ppdfile
, fileinfo
.st_uid
, fileinfo
.st_gid
);
1641 fchown(fd
, fileinfo
.st_uid
, fileinfo
.st_gid
);
1644 * Finally, run the filter to convert the file...
1647 if ((pid
= fork()) == 0)
1650 * Child process for pictwpstops... Redirect output of pictwpstops to a
1661 * Change to an unpriviledged user...
1664 setgid(fileinfo
.st_gid
);
1665 setuid(fileinfo
.st_uid
);
1668 execlp("pictwpstops", printer
, argv
[1], argv
[2], argv
[3], argv
[4], argv
[5],
1670 fprintf(stderr
, _("ERROR: Unable to exec pictwpstops: %s\n"),
1683 fprintf(stderr
, _("ERROR: Unable to fork pictwpstops: %s\n"),
1692 * Now wait for the filter to complete...
1695 if (wait(&status
) < 0)
1697 fprintf(stderr
, _("ERROR: Unable to wait for pictwpstops: %s\n"),
1714 fprintf(stderr
, _("ERROR: pictwpstops exited with status %d!\n"),
1717 fprintf(stderr
, _("ERROR: pictwpstops exited on signal %d!\n"),
1725 * Return with no errors..
1730 #endif /* __APPLE__ */
1734 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1738 sigterm_handler(int sig
) /* I - Signal */
1740 (void)sig
; /* remove compiler warnings... */
1745 * Flag that the job should be cancelled...
1753 * The scheduler already tried to cancel us once, now just terminate
1754 * after removing our temp files!
1758 unlink(tmpfilename
);
1763 #endif /* __APPLE__ */
1770 * End of "$Id: ipp.c 6482 2007-04-30 17:05:59Z mike $".