2 * "$Id: ipp.c 7018 2007-10-10 22:14:03Z mike $"
4 * IPP backend for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
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 * "LICENSE" which should have been included with this file. If this
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * main() - Send a file to the printer or server.
20 * cancel_job() - Cancel a print job.
21 * check_printer_state() - Check the printer state...
22 * compress_files() - Compress print files...
23 * password_cb() - Disable the password prompt for
24 * cupsDoFileRequest().
25 * report_printer_state() - Report the printer state.
26 * run_pictwps_filter() - Convert PICT files to PostScript when printing
28 * sigterm_handler() - Handle 'terminate' signals that stop the backend.
32 * Include necessary headers.
35 #include <cups/http-private.h>
39 #include <sys/types.h>
41 #include <cups/backend.h>
42 #include <cups/cups.h>
43 #include <cups/language.h>
44 #include <cups/i18n.h>
45 #include <cups/string.h>
53 static char *password
= NULL
; /* Password for device URI */
54 static int password_tries
= 0; /* Password tries */
56 static char pstmpname
[1024] = ""; /* Temporary PostScript file name */
57 #endif /* __APPLE__ */
58 static char tmpfilename
[1024] = ""; /* Temporary spool file name */
59 static int job_cancelled
= 0; /* Job cancelled? */
66 static void cancel_job(http_t
*http
, const char *uri
, int id
,
67 const char *resource
, const char *user
, int version
);
68 static void check_printer_state(http_t
*http
, const char *uri
,
69 const char *resource
, const char *user
,
70 int version
, int job_id
);
72 static void compress_files(int num_files
, char **files
);
73 #endif /* HAVE_LIBZ */
74 static const char *password_cb(const char *);
75 static int report_printer_state(ipp_t
*ipp
, int job_id
);
78 static int run_pictwps_filter(char **argv
, const char *filename
);
79 #endif /* __APPLE__ */
80 static void sigterm_handler(int sig
);
84 * 'main()' - Send a file to the printer or server.
88 * printer-uri job-id user title copies options [file]
91 int /* O - Exit status */
92 main(int argc
, /* I - Number of command-line args */
93 char *argv
[]) /* I - Command-line arguments */
95 int i
; /* Looping var */
96 int send_options
; /* Send job options? */
97 int num_options
; /* Number of printer options */
98 cups_option_t
*options
; /* Printer options */
99 char method
[255], /* Method in URI */
100 hostname
[1024], /* Hostname */
101 username
[255], /* Username info */
102 resource
[1024], /* Resource info (printer name) */
103 addrname
[256], /* Address name */
104 *optptr
, /* Pointer to URI options */
105 *name
, /* Name of option */
106 *value
, /* Value of option */
107 sep
; /* Separator character */
108 int num_files
; /* Number of files to print */
109 char **files
, /* Files to print */
110 *filename
; /* Pointer to single filename */
111 int port
; /* Port number (not used) */
112 char uri
[HTTP_MAX_URI
]; /* Updated URI without user/pass */
113 ipp_status_t ipp_status
; /* Status of IPP request */
114 http_t
*http
; /* HTTP connection */
115 ipp_t
*request
, /* IPP request */
116 *response
, /* IPP response */
117 *supported
; /* get-printer-attributes response */
118 time_t start_time
; /* Time of first connect */
119 int recoverable
; /* Recoverable error shown? */
120 int contimeout
; /* Connection timeout */
121 int delay
; /* Delay for retries... */
122 int compression
, /* Do compression of the job data? */
123 waitjob
, /* Wait for job complete? */
124 waitprinter
; /* Wait for printer ready? */
125 ipp_attribute_t
*job_id_attr
; /* job-id attribute */
126 int job_id
; /* job-id value */
127 ipp_attribute_t
*job_sheets
; /* job-media-sheets-completed */
128 ipp_attribute_t
*job_state
; /* job-state */
129 ipp_attribute_t
*copies_sup
; /* copies-supported */
130 ipp_attribute_t
*format_sup
; /* document-format-supported */
131 ipp_attribute_t
*printer_state
; /* printer-state attribute */
132 ipp_attribute_t
*printer_accepting
; /* printer-is-accepting-jobs */
133 int copies
, /* Number of copies for job */
134 copies_remaining
; /* Number of copies remaining */
135 const char *content_type
, /* CONTENT_TYPE environment variable */
136 *final_content_type
; /* FINAL_CONTENT_TYPE environment var */
137 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
138 struct sigaction action
; /* Actions for POSIX signals */
139 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
140 int version
; /* IPP version */
141 static const char * const pattrs
[] =
142 { /* Printer attributes we want */
144 "document-format-supported",
145 "printer-is-accepting-jobs",
147 "printer-state-message",
148 "printer-state-reasons",
150 static const char * const jattrs
[] =
151 { /* Job attributes we want */
152 "job-media-sheets-completed",
158 * Make sure status messages are not buffered...
161 setbuf(stderr
, NULL
);
164 * Ignore SIGPIPE and catch SIGTERM signals...
168 sigset(SIGPIPE
, SIG_IGN
);
169 sigset(SIGTERM
, sigterm_handler
);
170 #elif defined(HAVE_SIGACTION)
171 memset(&action
, 0, sizeof(action
));
172 action
.sa_handler
= SIG_IGN
;
173 sigaction(SIGPIPE
, &action
, NULL
);
175 sigemptyset(&action
.sa_mask
);
176 sigaddset(&action
.sa_mask
, SIGTERM
);
177 action
.sa_handler
= sigterm_handler
;
178 sigaction(SIGTERM
, &action
, NULL
);
180 signal(SIGPIPE
, SIG_IGN
);
181 signal(SIGTERM
, sigterm_handler
);
182 #endif /* HAVE_SIGSET */
185 * Check command-line...
192 if ((s
= strrchr(argv
[0], '/')) != NULL
)
197 printf("network %s \"Unknown\" \"Internet Printing Protocol (%s)\"\n",
199 return (CUPS_BACKEND_OK
);
203 _cupsLangPrintf(stderr
,
204 _("Usage: %s job-id user title copies options [file]\n"),
206 return (CUPS_BACKEND_STOP
);
210 * Get the (final) content type...
213 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
214 content_type
= "application/octet-stream";
216 if ((final_content_type
= getenv("FINAL_CONTENT_TYPE")) == NULL
)
218 final_content_type
= content_type
;
220 if (!strncmp(final_content_type
, "printer/", 8))
221 final_content_type
= "application/vnd.cups-raw";
225 * Extract the hostname and printer name from the URI...
228 if (httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
229 method
, sizeof(method
), username
, sizeof(username
),
230 hostname
, sizeof(hostname
), &port
,
231 resource
, sizeof(resource
)) < HTTP_URI_OK
)
233 _cupsLangPuts(stderr
,
234 _("ERROR: Missing device URI on command-line and no "
235 "DEVICE_URI environment variable!\n"));
236 return (CUPS_BACKEND_STOP
);
240 port
= IPP_PORT
; /* Default to port 631 */
242 if (!strcmp(method
, "https"))
243 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
245 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
248 * See if there are any options...
255 contimeout
= 7 * 24 * 60 * 60;
257 if ((optptr
= strchr(resource
, '?')) != NULL
)
260 * Yup, terminate the device name string and move to the first
261 * character of the optptr...
267 * Then parse the optptr...
278 while (*optptr
&& *optptr
!= '=' && *optptr
!= '+' && *optptr
!= '&')
281 if ((sep
= *optptr
) != '\0')
292 while (*optptr
&& *optptr
!= '+' && *optptr
!= '&')
302 * Process the option...
305 if (!strcasecmp(name
, "waitjob"))
308 * Wait for job completion?
311 waitjob
= !strcasecmp(value
, "on") ||
312 !strcasecmp(value
, "yes") ||
313 !strcasecmp(value
, "true");
315 else if (!strcasecmp(name
, "waitprinter"))
318 * Wait for printer idle?
321 waitprinter
= !strcasecmp(value
, "on") ||
322 !strcasecmp(value
, "yes") ||
323 !strcasecmp(value
, "true");
325 else if (!strcasecmp(name
, "encryption"))
328 * Enable/disable encryption?
331 if (!strcasecmp(value
, "always"))
332 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
333 else if (!strcasecmp(value
, "required"))
334 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
335 else if (!strcasecmp(value
, "never"))
336 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
337 else if (!strcasecmp(value
, "ifrequested"))
338 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
341 _cupsLangPrintf(stderr
,
342 _("ERROR: Unknown encryption option value \"%s\"!\n"),
346 else if (!strcasecmp(name
, "version"))
348 if (!strcmp(value
, "1.0"))
350 else if (!strcmp(value
, "1.1"))
354 _cupsLangPrintf(stderr
,
355 _("ERROR: Unknown version option value \"%s\"!\n"),
360 else if (!strcasecmp(name
, "compression"))
362 compression
= !strcasecmp(value
, "true") ||
363 !strcasecmp(value
, "yes") ||
364 !strcasecmp(value
, "on") ||
365 !strcasecmp(value
, "gzip");
367 #endif /* HAVE_LIBZ */
368 else if (!strcasecmp(name
, "contimeout"))
371 * Set the connection timeout...
375 contimeout
= atoi(value
);
383 _cupsLangPrintf(stderr
,
384 _("ERROR: Unknown option \"%s\" with value \"%s\"!\n"),
391 * If we have 7 arguments, print the file named on the command-line.
392 * Otherwise, copy stdin to a temporary file and print the temporary
399 * Copy stdin to a temporary file...
402 int fd
; /* File descriptor */
403 cups_file_t
*fp
; /* Temporary file */
404 char buffer
[8192]; /* Buffer for copying */
405 int bytes
; /* Number of bytes read */
408 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
410 _cupsLangPrintError(_("ERROR: Unable to create temporary file"));
411 return (CUPS_BACKEND_FAILED
);
414 if ((fp
= cupsFileOpenFd(fd
, compression
? "w9" : "w")) == NULL
)
416 _cupsLangPrintError(_("ERROR: Unable to open temporary file"));
419 return (CUPS_BACKEND_FAILED
);
422 while ((bytes
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0)
423 if (cupsFileWrite(fp
, buffer
, bytes
) < bytes
)
425 _cupsLangPrintError(_("ERROR: Unable to write to temporary file"));
428 return (CUPS_BACKEND_FAILED
);
434 * Point to the single file from stdin...
437 filename
= tmpfilename
;
445 * Point to the files on the command-line...
448 num_files
= argc
- 6;
454 compress_files(num_files
, files
);
455 #endif /* HAVE_LIBZ */
458 fprintf(stderr
, "DEBUG: %d files to send in job...\n", num_files
);
461 * Set the authentication info, if any...
464 cupsSetPasswordCB(password_cb
);
469 * Use authenticaion information in the device URI...
472 if ((password
= strchr(username
, ':')) != NULL
)
475 cupsSetUser(username
);
480 * Try loading authentication information from the environment.
483 const char *ptr
= getenv("AUTH_USERNAME");
488 password
= getenv("AUTH_PASSWORD");
492 * Try connecting to the remote server...
497 start_time
= time(NULL
);
499 fputs("STATE: +connecting-to-device\n", stderr
);
503 _cupsLangPrintf(stderr
, _("INFO: Connecting to %s on port %d...\n"),
506 if ((http
= httpConnectEncrypt(hostname
, port
, cupsEncryption())) == NULL
)
511 if (getenv("CLASS") != NULL
)
514 * If the CLASS environment variable is set, the job was submitted
515 * to a class and not to a specific queue. In this case, we want
516 * to abort immediately so that the job can be requeued on the next
517 * available printer in the class.
520 _cupsLangPuts(stderr
,
521 _("INFO: Unable to contact printer, queuing on next "
522 "printer in class...\n"));
524 if (argc
== 6 || strcmp(filename
, argv
[6]))
528 * Sleep 5 seconds to keep the job from requeuing too rapidly...
533 return (CUPS_BACKEND_FAILED
);
536 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
537 errno
== EHOSTUNREACH
)
539 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
541 _cupsLangPuts(stderr
, _("ERROR: Printer not responding!\n"));
542 return (CUPS_BACKEND_FAILED
);
547 _cupsLangPrintf(stderr
,
548 _("WARNING: recoverable: Network host \'%s\' is busy; "
549 "will retry in %d seconds...\n"),
559 _cupsLangPrintf(stderr
, _("ERROR: Unable to locate printer \'%s\'!\n"),
561 return (CUPS_BACKEND_STOP
);
567 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
568 _cupsLangPuts(stderr
,
569 _("ERROR: recoverable: Unable to connect to printer; will "
570 "retry in 30 seconds...\n"));
578 while (http
== NULL
);
582 if (argc
== 6 || strcmp(filename
, argv
[6]))
585 return (CUPS_BACKEND_FAILED
);
588 fputs("STATE: -connecting-to-device\n", stderr
);
589 _cupsLangPrintf(stderr
, _("INFO: Connected to %s...\n"), hostname
);
592 if (http
->hostaddr
->addr
.sa_family
== AF_INET6
)
593 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
594 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
595 ntohs(http
->hostaddr
->ipv6
.sin6_port
));
597 #endif /* AF_INET6 */
598 if (http
->hostaddr
->addr
.sa_family
== AF_INET
)
599 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4)...\n",
600 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
601 ntohs(http
->hostaddr
->ipv4
.sin_port
));
604 * Build a URI for the printer and fill the standard IPP attributes for
605 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
606 * might contain username:password information...
609 snprintf(uri
, sizeof(uri
), "%s://%s:%d%s", method
, hostname
, port
, resource
);
612 * First validate the destination and see if the device supports multiple
613 * copies. We have to do this because some IPP servers (e.g. HP JetDirect)
614 * don't support the copies attribute...
624 * Build the IPP request...
627 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
628 request
->request
.op
.version
[1] = version
;
630 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
633 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
634 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
641 fputs("DEBUG: Getting supported attributes...\n", stderr
);
643 if ((supported
= cupsDoRequest(http
, request
, resource
)) == NULL
)
644 ipp_status
= cupsLastError();
646 ipp_status
= supported
->request
.status
.status_code
;
648 if (ipp_status
> IPP_OK_CONFLICT
)
650 if (ipp_status
== IPP_PRINTER_BUSY
||
651 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
653 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
655 _cupsLangPuts(stderr
, _("ERROR: Printer not responding!\n"));
656 return (CUPS_BACKEND_FAILED
);
661 _cupsLangPrintf(stderr
,
662 _("WARNING: recoverable: Network host \'%s\' is busy; "
663 "will retry in %d seconds...\n"),
666 report_printer_state(supported
, 0);
673 else if ((ipp_status
== IPP_BAD_REQUEST
||
674 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
== 1)
677 * Switch to IPP/1.0...
680 _cupsLangPuts(stderr
,
681 _("INFO: Printer does not support IPP/1.1, trying "
686 else if (ipp_status
== IPP_NOT_FOUND
)
688 _cupsLangPuts(stderr
, _("ERROR: Destination printer does not exist!\n"));
691 ippDelete(supported
);
693 return (CUPS_BACKEND_STOP
);
697 _cupsLangPrintf(stderr
,
698 _("ERROR: Unable to get printer status (%s)!\n"),
699 cupsLastErrorString());
704 ippDelete(supported
);
708 else if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
709 IPP_TAG_RANGE
)) != NULL
)
712 * Has the "copies-supported" attribute - does it have an upper
716 if (copies_sup
->values
[0].range
.upper
<= 1)
717 copies_sup
= NULL
; /* No */
720 format_sup
= ippFindAttribute(supported
, "document-format-supported",
725 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
726 format_sup
->num_values
);
727 for (i
= 0; i
< format_sup
->num_values
; i
++)
728 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
729 format_sup
->values
[i
].string
.text
);
732 report_printer_state(supported
, 0);
734 while (ipp_status
> IPP_OK_CONFLICT
);
737 * See if the printer is accepting jobs and is not stopped; if either
738 * condition is true and we are printing to a class, requeue the job...
741 if (getenv("CLASS") != NULL
)
743 printer_state
= ippFindAttribute(supported
, "printer-state",
745 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
748 if (printer_state
== NULL
||
749 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
751 printer_accepting
== NULL
||
752 !printer_accepting
->values
[0].boolean
)
755 * If the CLASS environment variable is set, the job was submitted
756 * to a class and not to a specific queue. In this case, we want
757 * to abort immediately so that the job can be requeued on the next
758 * available printer in the class.
761 _cupsLangPuts(stderr
,
762 _("INFO: Unable to contact printer, queuing on next "
763 "printer in class...\n"));
765 ippDelete(supported
);
768 if (argc
== 6 || strcmp(filename
, argv
[6]))
772 * Sleep 5 seconds to keep the job from requeuing too rapidly...
777 return (CUPS_BACKEND_FAILED
);
784 * If we've shown a recoverable error make sure the printer proxies
785 * have a chance to see the recovered message. Not pretty but
786 * necessary for now...
789 fputs("INFO: recovered: \n", stderr
);
794 * See if the printer supports multiple copies...
797 copies
= atoi(argv
[4]);
799 if (copies_sup
|| argc
< 7)
801 copies_remaining
= 1;
807 copies_remaining
= copies
;
810 * Then issue the print-job request...
815 while (copies_remaining
> 0)
818 * Build the IPP request...
825 request
= ippNewRequest(IPP_CREATE_JOB
);
827 request
= ippNewRequest(IPP_PRINT_JOB
);
829 request
->request
.op
.version
[1] = version
;
831 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
834 fprintf(stderr
, "DEBUG: printer-uri = \"%s\"\n", uri
);
837 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
838 "requesting-user-name", NULL
, argv
[2]);
840 fprintf(stderr
, "DEBUG: requesting-user-name = \"%s\"\n", argv
[2]);
843 * Only add a "job-name" attribute if the remote server supports
844 * copy generation - some IPP implementations like HP's don't seem
845 * to like UTF-8 job names (STR #1837)...
848 if (argv
[3][0] && copies_sup
)
849 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
852 fprintf(stderr
, "DEBUG: job-name = \"%s\"\n", argv
[3]);
856 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
857 "compression", NULL
, "gzip");
858 #endif /* HAVE_LIBZ */
861 * Handle options on the command-line...
865 num_options
= cupsParseOptions(argv
[5], 0, &options
);
868 if (!strcasecmp(final_content_type
, "application/pictwps") &&
871 if (format_sup
!= NULL
)
873 for (i
= 0; i
< format_sup
->num_values
; i
++)
874 if (!strcasecmp(final_content_type
, format_sup
->values
[i
].string
.text
))
878 if (format_sup
== NULL
|| i
>= format_sup
->num_values
)
881 * Remote doesn't support "application/pictwps" (i.e. it's not MacOS X)
882 * so convert the document to PostScript...
885 if (run_pictwps_filter(argv
, filename
))
886 return (CUPS_BACKEND_FAILED
);
888 filename
= pstmpname
;
891 * Change the MIME type to application/postscript and change the
892 * number of copies to 1...
895 final_content_type
= "application/postscript";
897 copies_remaining
= 1;
901 #endif /* __APPLE__ */
903 if (format_sup
!= NULL
)
905 for (i
= 0; i
< format_sup
->num_values
; i
++)
906 if (!strcasecmp(final_content_type
, format_sup
->values
[i
].string
.text
))
909 if (i
< format_sup
->num_values
)
910 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
911 "document-format", NULL
, final_content_type
);
914 if (copies_sup
&& version
> 0 && send_options
)
917 * Only send options if the destination printer supports the copies
918 * attribute and IPP/1.1. This is a hack for the HP and Lexmark
919 * implementations of IPP, which do not accept extension attributes
920 * and incorrectly report a client-error-bad-request error instead of
921 * the successful-ok-unsupported-attributes status. In short, at least
922 * some HP and Lexmark implementations of IPP are non-compliant.
925 cupsEncodeOptions(request
, num_options
, options
);
927 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies",
931 cupsFreeOptions(num_options
, options
);
934 * If copies aren't supported, then we are likely dealing with an HP
935 * JetDirect. The HP IPP implementation seems to close the connection
936 * after every request - that is, it does *not* implement HTTP Keep-
937 * Alive, which is REQUIRED by HTTP/1.1...
948 response
= cupsDoRequest(http
, request
, resource
);
950 response
= cupsDoFileRequest(http
, request
, resource
, files
[0]);
952 ipp_status
= cupsLastError();
954 if (ipp_status
> IPP_OK_CONFLICT
)
961 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
962 ipp_status
== IPP_PRINTER_BUSY
)
964 _cupsLangPuts(stderr
,
965 _("INFO: Printer busy; will retry in 10 seconds...\n"));
968 else if ((ipp_status
== IPP_BAD_REQUEST
||
969 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
== 1)
972 * Switch to IPP/1.0...
975 _cupsLangPuts(stderr
,
976 _("INFO: Printer does not support IPP/1.1, trying "
982 _cupsLangPrintf(stderr
, _("ERROR: Print file was not accepted (%s)!\n"),
983 cupsLastErrorString());
985 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
986 IPP_TAG_INTEGER
)) == NULL
)
988 _cupsLangPuts(stderr
,
989 _("NOTICE: Print file accepted - job ID unknown.\n"));
994 job_id
= job_id_attr
->values
[0].integer
;
995 _cupsLangPrintf(stderr
, _("NOTICE: Print file accepted - job ID %d.\n"),
1004 if (job_id
&& num_files
> 1)
1006 for (i
= 0; i
< num_files
; i
++)
1008 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
1010 request
->request
.op
.version
[1] = version
;
1012 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1015 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1019 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1020 "requesting-user-name", NULL
, argv
[2]);
1022 if ((i
+ 1) == num_files
)
1023 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
1025 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1026 "document-format", NULL
, content_type
);
1028 ippDelete(cupsDoFileRequest(http
, request
, resource
, files
[i
]));
1030 if (cupsLastError() > IPP_OK_CONFLICT
)
1032 ipp_status
= cupsLastError();
1034 _cupsLangPrintf(stderr
,
1035 _("ERROR: Unable to add file %d to job: %s\n"),
1036 job_id
, cupsLastErrorString());
1042 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
1044 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
1045 copies_remaining
--;
1047 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1048 ipp_status
== IPP_PRINTER_BUSY
)
1051 copies_remaining
--;
1054 * Wait for the job to complete...
1057 if (!job_id
|| !waitjob
)
1060 _cupsLangPuts(stderr
, _("INFO: Waiting for job to complete...\n"));
1062 for (delay
= 1; !job_cancelled
;)
1065 * Build an IPP_GET_JOB_ATTRIBUTES request...
1068 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1069 request
->request
.op
.version
[1] = version
;
1071 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1074 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1078 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1079 "requesting-user-name", NULL
, argv
[2]);
1081 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1082 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
1090 httpReconnect(http
);
1092 response
= cupsDoRequest(http
, request
, resource
);
1093 ipp_status
= cupsLastError();
1095 if (ipp_status
== IPP_NOT_FOUND
)
1098 * Job has gone away and/or the server has no job history...
1101 ippDelete(response
);
1103 ipp_status
= IPP_OK
;
1107 if (ipp_status
> IPP_OK_CONFLICT
)
1109 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1110 ipp_status
!= IPP_PRINTER_BUSY
)
1112 ippDelete(response
);
1114 _cupsLangPrintf(stderr
,
1115 _("ERROR: Unable to get job %d attributes (%s)!\n"),
1116 job_id
, cupsLastErrorString());
1123 if ((job_state
= ippFindAttribute(response
, "job-state",
1124 IPP_TAG_ENUM
)) != NULL
)
1127 * Stop polling if the job is finished or pending-held...
1130 if (job_state
->values
[0].integer
> IPP_JOB_STOPPED
)
1132 if ((job_sheets
= ippFindAttribute(response
,
1133 "job-media-sheets-completed",
1134 IPP_TAG_INTEGER
)) != NULL
)
1135 fprintf(stderr
, "PAGE: total %d\n",
1136 job_sheets
->values
[0].integer
);
1138 ippDelete(response
);
1144 ippDelete(response
);
1147 * Check the printer state and report it if necessary...
1150 check_printer_state(http
, uri
, resource
, argv
[2], version
, job_id
);
1153 * Wait 1-10 seconds before polling again...
1165 * Cancel the job as needed...
1168 if (job_cancelled
&& job_id
)
1169 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1172 * Check the printer state and report it if necessary...
1175 check_printer_state(http
, uri
, resource
, argv
[2], version
, job_id
);
1183 ippDelete(supported
);
1186 * Remove the temporary file(s) if necessary...
1190 unlink(tmpfilename
);
1195 for (i
= 0; i
< num_files
; i
++)
1198 #endif /* HAVE_LIBZ */
1203 #endif /* __APPLE__ */
1206 * Return the queue status...
1209 if (ipp_status
== IPP_NOT_AUTHORIZED
)
1212 * Authorization failures here mean that we need Kerberos. Username +
1213 * password authentication is handled in the password_cb function.
1216 fputs("ATTR: auth-info-required=negotiate\n", stderr
);
1217 return (CUPS_BACKEND_AUTH_REQUIRED
);
1219 else if (ipp_status
> IPP_OK_CONFLICT
)
1220 return (CUPS_BACKEND_FAILED
);
1222 return (CUPS_BACKEND_OK
);
1227 * 'cancel_job()' - Cancel a print job.
1231 cancel_job(http_t
*http
, /* I - HTTP connection */
1232 const char *uri
, /* I - printer-uri */
1233 int id
, /* I - job-id */
1234 const char *resource
, /* I - Resource path */
1235 const char *user
, /* I - requesting-user-name */
1236 int version
) /* I - IPP version */
1238 ipp_t
*request
; /* Cancel-Job request */
1241 _cupsLangPuts(stderr
, _("INFO: Canceling print job...\n"));
1243 request
= ippNewRequest(IPP_CANCEL_JOB
);
1244 request
->request
.op
.version
[1] = version
;
1246 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1248 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1250 if (user
&& user
[0])
1251 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1252 "requesting-user-name", NULL
, user
);
1258 ippDelete(cupsDoRequest(http
, request
, resource
));
1260 if (cupsLastError() > IPP_OK_CONFLICT
)
1261 _cupsLangPrintf(stderr
, _("ERROR: Unable to cancel job %d: %s\n"), id
,
1262 cupsLastErrorString());
1267 * 'check_printer_state()' - Check the printer state...
1271 check_printer_state(
1272 http_t
*http
, /* I - HTTP connection */
1273 const char *uri
, /* I - Printer URI */
1274 const char *resource
, /* I - Resource path */
1275 const char *user
, /* I - Username, if any */
1276 int version
, /* I - IPP version */
1277 int job_id
) /* I - Current job ID */
1279 ipp_t
*request
, /* IPP request */
1280 *response
; /* IPP response */
1281 static const char * const attrs
[] = /* Attributes we want */
1283 "printer-state-message",
1284 "printer-state-reasons"
1289 * Check on the printer state...
1292 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1293 request
->request
.op
.version
[1] = version
;
1295 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1298 if (user
&& user
[0])
1299 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1300 "requesting-user-name", NULL
, user
);
1302 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1303 "requested-attributes",
1304 (int)(sizeof(attrs
) / sizeof(attrs
[0])), NULL
, attrs
);
1310 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
1312 report_printer_state(response
, job_id
);
1313 ippDelete(response
);
1320 * 'compress_files()' - Compress print files...
1324 compress_files(int num_files
, /* I - Number of files */
1325 char **files
) /* I - Files */
1327 int i
, /* Looping var */
1328 fd
; /* Temporary file descriptor */
1329 ssize_t bytes
; /* Bytes read/written */
1330 size_t total
; /* Total bytes read */
1331 cups_file_t
*in
, /* Input file */
1332 *out
; /* Output file */
1333 struct stat outinfo
; /* Output file information */
1334 char filename
[1024], /* Temporary filename */
1335 buffer
[32768]; /* Copy buffer */
1338 fprintf(stderr
, "DEBUG: Compressing %d job files...\n", num_files
);
1339 for (i
= 0; i
< num_files
; i
++)
1341 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
1343 _cupsLangPrintf(stderr
,
1344 _("ERROR: Unable to create temporary compressed print "
1345 "file: %s\n"), strerror(errno
));
1346 exit(CUPS_BACKEND_FAILED
);
1349 if ((out
= cupsFileOpenFd(fd
, "w9")) == NULL
)
1351 _cupsLangPrintf(stderr
,
1352 _("ERROR: Unable to open temporary compressed print "
1353 "file: %s\n"), strerror(errno
));
1354 exit(CUPS_BACKEND_FAILED
);
1357 if ((in
= cupsFileOpen(files
[i
], "r")) == NULL
)
1359 _cupsLangPrintf(stderr
,
1360 _("ERROR: Unable to open print file \"%s\": %s\n"),
1361 files
[i
], strerror(errno
));
1363 exit(CUPS_BACKEND_FAILED
);
1367 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
1368 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
1370 _cupsLangPrintf(stderr
,
1371 _("ERROR: Unable to write %d bytes to \"%s\": %s\n"),
1372 (int)bytes
, filename
, strerror(errno
));
1375 exit(CUPS_BACKEND_FAILED
);
1383 files
[i
] = strdup(filename
);
1385 if (!stat(filename
, &outinfo
))
1387 "DEBUG: File %d compressed to %.1f%% of original size, "
1388 CUPS_LLFMT
" bytes...\n",
1389 i
+ 1, 100.0 * outinfo
.st_size
/ total
,
1390 CUPS_LLCAST outinfo
.st_size
);
1393 #endif /* HAVE_LIBZ */
1397 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
1400 static const char * /* O - Password */
1401 password_cb(const char *prompt
) /* I - Prompt (not used) */
1405 if (password
&& password_tries
< 3)
1414 * If there is no password set in the device URI, return the
1415 * "authentication required" exit code...
1419 unlink(tmpfilename
);
1424 #endif /* __APPLE__ */
1426 fputs("ATTR: auth-info-required=username,password\n", stderr
);
1428 exit(CUPS_BACKEND_AUTH_REQUIRED
);
1430 return (NULL
); /* Eliminate compiler warning */
1436 * 'report_printer_state()' - Report the printer state.
1439 static int /* O - Number of reasons shown */
1440 report_printer_state(ipp_t
*ipp
, /* I - IPP response */
1441 int job_id
) /* I - Current job ID */
1443 int i
; /* Looping var */
1444 int count
; /* Count of reasons shown... */
1445 ipp_attribute_t
*psm
, /* pritner-state-message */
1446 *reasons
; /* printer-state-reasons */
1447 const char *reason
; /* Current reason */
1448 const char *message
; /* Message to show */
1449 char unknown
[1024]; /* Unknown message string */
1450 const char *prefix
; /* Prefix for STATE: line */
1451 char state
[1024]; /* State string */
1452 cups_lang_t
*language
; /* Current localization */
1455 if ((psm
= ippFindAttribute(ipp
, "printer-state-message",
1456 IPP_TAG_TEXT
)) != NULL
)
1457 fprintf(stderr
, "INFO: %s\n", psm
->values
[0].string
.text
);
1459 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
1460 IPP_TAG_KEYWORD
)) == NULL
)
1465 language
= cupsLangDefault();
1467 for (i
= 0, count
= 0; i
< reasons
->num_values
; i
++)
1469 reason
= reasons
->values
[i
].string
.text
;
1471 if (job_id
== 0 || strcmp(reason
, "paused"))
1473 strlcat(state
, prefix
, sizeof(state
));
1474 strlcat(state
, reason
, sizeof(state
));
1481 if (!strncmp(reason
, "media-needed", 12))
1482 message
= _("Media tray needs to be filled.");
1483 else if (!strncmp(reason
, "media-jam", 9))
1484 message
= _("Media jam!");
1485 else if (!strncmp(reason
, "moving-to-paused", 16) ||
1486 !strncmp(reason
, "paused", 6) ||
1487 !strncmp(reason
, "shutdown", 8))
1488 message
= _("Printer offline.");
1489 else if (!strncmp(reason
, "toner-low", 9))
1490 message
= _("Toner low.");
1491 else if (!strncmp(reason
, "toner-empty", 11))
1492 message
= _("Out of toner!");
1493 else if (!strncmp(reason
, "cover-open", 10))
1494 message
= _("Cover open.");
1495 else if (!strncmp(reason
, "interlock-open", 14))
1496 message
= _("Interlock open.");
1497 else if (!strncmp(reason
, "door-open", 9))
1498 message
= _("Door open.");
1499 else if (!strncmp(reason
, "input-tray-missing", 18))
1500 message
= _("Media tray missing!");
1501 else if (!strncmp(reason
, "media-low", 9))
1502 message
= _("Media tray almost empty.");
1503 else if (!strncmp(reason
, "media-empty", 11))
1504 message
= _("Media tray empty!");
1505 else if (!strncmp(reason
, "output-tray-missing", 19))
1506 message
= _("Output tray missing!");
1507 else if (!strncmp(reason
, "output-area-almost-full", 23))
1508 message
= _("Output bin almost full.");
1509 else if (!strncmp(reason
, "output-area-full", 16))
1510 message
= _("Output bin full!");
1511 else if (!strncmp(reason
, "marker-supply-low", 17))
1512 message
= _("Ink/toner almost empty.");
1513 else if (!strncmp(reason
, "marker-supply-empty", 19))
1514 message
= _("Ink/toner empty!");
1515 else if (!strncmp(reason
, "marker-waste-almost-full", 24))
1516 message
= _("Ink/toner waste bin almost full.");
1517 else if (!strncmp(reason
, "marker-waste-full", 17))
1518 message
= _("Ink/toner waste bin full!");
1519 else if (!strncmp(reason
, "fuser-over-temp", 15))
1520 message
= _("Fuser temperature high!");
1521 else if (!strncmp(reason
, "fuser-under-temp", 16))
1522 message
= _("Fuser temperature low!");
1523 else if (!strncmp(reason
, "opc-near-eol", 12))
1524 message
= _("OPC almost at end-of-life.");
1525 else if (!strncmp(reason
, "opc-life-over", 13))
1526 message
= _("OPC at end-of-life!");
1527 else if (!strncmp(reason
, "developer-low", 13))
1528 message
= _("Developer almost empty.");
1529 else if (!strncmp(reason
, "developer-empty", 15))
1530 message
= _("Developer empty!");
1531 else if (strstr(reason
, "error") != NULL
)
1535 snprintf(unknown
, sizeof(unknown
), _("Unknown printer error (%s)!"),
1542 if (strstr(reasons
->values
[i
].string
.text
, "error"))
1543 fprintf(stderr
, "ERROR: %s\n", _cupsLangString(language
, message
));
1544 else if (strstr(reasons
->values
[i
].string
.text
, "warning"))
1545 fprintf(stderr
, "WARNING: %s\n", _cupsLangString(language
, message
));
1547 fprintf(stderr
, "INFO: %s\n", _cupsLangString(language
, message
));
1551 fprintf(stderr
, "%s\n", state
);
1559 * 'run_pictwps_filter()' - Convert PICT files to PostScript when printing
1562 * This step is required because the PICT format is not documented and
1563 * subject to change, so developing a filter for other OS's is infeasible.
1564 * Also, fonts required by the PICT file need to be embedded on the
1565 * client side (which has the fonts), so we run the filter to get a
1566 * PostScript file for printing...
1569 static int /* O - Exit status of filter */
1570 run_pictwps_filter(char **argv
, /* I - Command-line arguments */
1571 const char *filename
)/* I - Filename */
1573 struct stat fileinfo
; /* Print file information */
1574 const char *ppdfile
; /* PPD file for destination printer */
1575 int pid
; /* Child process ID */
1576 int fd
; /* Temporary file descriptor */
1577 int status
; /* Exit status of filter */
1578 const char *printer
; /* PRINTER env var */
1579 static char ppdenv
[1024]; /* PPD environment variable */
1583 * First get the PPD file for the printer...
1586 printer
= getenv("PRINTER");
1589 _cupsLangPuts(stderr
,
1590 _("ERROR: PRINTER environment variable not defined!\n"));
1594 if ((ppdfile
= cupsGetPPD(printer
)) == NULL
)
1596 _cupsLangPrintf(stderr
,
1597 _("ERROR: Unable to get PPD file for printer \"%s\" - "
1598 "%s.\n"), printer
, cupsLastErrorString());
1602 snprintf(ppdenv
, sizeof(ppdenv
), "PPD=%s", ppdfile
);
1607 * Then create a temporary file for printing...
1610 if ((fd
= cupsTempFd(pstmpname
, sizeof(pstmpname
))) < 0)
1612 _cupsLangPrintf(stderr
, _("ERROR: Unable to create temporary file - %s.\n"),
1620 * Get the owner of the spool file - it is owned by the user we want to run
1625 stat(argv
[6], &fileinfo
);
1629 * Use the OSX defaults, as an up-stream filter created the PICT
1633 fileinfo
.st_uid
= 1;
1634 fileinfo
.st_gid
= 80;
1638 chown(ppdfile
, fileinfo
.st_uid
, fileinfo
.st_gid
);
1640 fchown(fd
, fileinfo
.st_uid
, fileinfo
.st_gid
);
1643 * Finally, run the filter to convert the file...
1646 if ((pid
= fork()) == 0)
1649 * Child process for pictwpstops... Redirect output of pictwpstops to a
1660 * Change to an unpriviledged user...
1663 setgid(fileinfo
.st_gid
);
1664 setuid(fileinfo
.st_uid
);
1667 execlp("pictwpstops", printer
, argv
[1], argv
[2], argv
[3], argv
[4], argv
[5],
1669 _cupsLangPrintf(stderr
, _("ERROR: Unable to exec pictwpstops: %s\n"),
1682 _cupsLangPrintf(stderr
, _("ERROR: Unable to fork pictwpstops: %s\n"),
1691 * Now wait for the filter to complete...
1694 if (wait(&status
) < 0)
1696 _cupsLangPrintf(stderr
, _("ERROR: Unable to wait for pictwpstops: %s\n"),
1713 _cupsLangPrintf(stderr
, _("ERROR: pictwpstops exited with status %d!\n"),
1716 _cupsLangPrintf(stderr
, _("ERROR: pictwpstops exited on signal %d!\n"),
1724 * Return with no errors..
1729 #endif /* __APPLE__ */
1733 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1737 sigterm_handler(int sig
) /* I - Signal */
1739 (void)sig
; /* remove compiler warnings... */
1744 * Flag that the job should be cancelled...
1752 * The scheduler already tried to cancel us once, now just terminate
1753 * after removing our temp files!
1757 unlink(tmpfilename
);
1762 #endif /* __APPLE__ */
1769 * End of "$Id: ipp.c 7018 2007-10-10 22:14:03Z mike $".