2 * "$Id: ipp.c 7948 2008-09-17 00:04:12Z mike $"
4 * IPP backend for CUPS.
6 * Copyright 2007-2011 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 * monitor_printer() - Monitor the printer state...
24 * new_request() - Create a new print creation or validation request.
25 * password_cb() - Disable the password prompt for
26 * cupsDoFileRequest().
27 * report_attr() - Report an IPP attribute value.
28 * report_printer_state() - Report the printer state.
29 * sigterm_handler() - Handle 'terminate' signals that stop the backend.
33 * Include necessary headers.
36 #include "backend-private.h"
37 #include <sys/types.h>
46 typedef struct _cups_monitor_s
/**** Monitoring data ****/
48 const char *uri
, /* Printer URI */
49 *hostname
, /* Hostname */
51 *resource
; /* Resource path */
52 int port
, /* Port number */
53 version
, /* IPP version */
54 job_id
; /* Job ID for submitted job */
55 http_encryption_t encryption
; /* Use encryption? */
56 ipp_jstate_t job_state
; /* Current job state */
57 ipp_pstate_t printer_state
; /* Current printer state */
65 static const char *auth_info_required
= "none";
66 /* New auth-info-required value */
67 static const char * const jattrs
[] = /* Job attributes we want */
69 "job-media-sheets-completed",
73 static int job_canceled
= 0; /* Job cancelled? */
74 static char *password
= NULL
; /* Password for device URI */
75 static int password_tries
= 0; /* Password tries */
76 static const char * const pattrs
[] = /* Printer attributes we want */
80 "document-format-supported",
88 "media-col-supported",
89 "multiple-document-handling-supported",
90 "operations-supported",
92 "printer-alert-description",
93 "printer-is-accepting-jobs",
95 "printer-state-message",
96 "printer-state-reasons",
98 static char tmpfilename
[1024] = ""; /* Temporary spool file name */
105 static void cancel_job(http_t
*http
, const char *uri
, int id
,
106 const char *resource
, const char *user
,
108 static ipp_pstate_t
check_printer_state(http_t
*http
, const char *uri
,
109 const char *resource
,
110 const char *user
, int version
,
113 static void compress_files(int num_files
, char **files
);
114 #endif /* HAVE_LIBZ */
115 static void *monitor_printer(_cups_monitor_t
*monitor
);
116 static ipp_t
*new_request(ipp_op_t op
, int version
, const char *uri
,
117 const char *user
, const char *title
,
118 int num_options
, cups_option_t
*options
,
119 const char *compression
, int copies
,
120 const char *format
, _ppd_cache_t
*pc
,
121 ipp_attribute_t
*media_col_sup
,
122 ipp_attribute_t
*doc_handling_sup
);
123 static const char *password_cb(const char *);
124 static void report_attr(ipp_attribute_t
*attr
);
125 static int report_printer_state(ipp_t
*ipp
, int job_id
);
126 static void sigterm_handler(int sig
);
130 * 'main()' - Send a file to the printer or server.
134 * printer-uri job-id user title copies options [file]
137 int /* O - Exit status */
138 main(int argc
, /* I - Number of command-line args */
139 char *argv
[]) /* I - Command-line arguments */
141 int i
; /* Looping var */
142 int send_options
; /* Send job options? */
143 int num_options
; /* Number of printer options */
144 cups_option_t
*options
; /* Printer options */
145 const char *device_uri
; /* Device URI */
146 char scheme
[255], /* Scheme in URI */
147 hostname
[1024], /* Hostname */
148 username
[255], /* Username info */
149 resource
[1024], /* Resource info (printer name) */
150 addrname
[256], /* Address name */
151 *optptr
, /* Pointer to URI options */
152 *name
, /* Name of option */
153 *value
, /* Value of option */
154 sep
; /* Separator character */
155 http_addrlist_t
*addrlist
; /* Address of printer */
156 int snmp_fd
, /* SNMP socket */
157 start_count
, /* Page count via SNMP at start */
158 page_count
, /* Page count via SNMP */
159 have_supplies
; /* Printer supports supply levels? */
160 int num_files
; /* Number of files to print */
161 char **files
, /* Files to print */
162 *compatfile
= NULL
; /* Compatibility filename */
163 off_t compatsize
= 0; /* Size of compatibility file */
164 int port
; /* Port number (not used) */
165 char portname
[255]; /* Port name */
166 char uri
[HTTP_MAX_URI
]; /* Updated URI without user/pass */
167 http_status_t http_status
; /* Status of HTTP request */
168 ipp_status_t ipp_status
; /* Status of IPP request */
169 http_t
*http
; /* HTTP connection */
170 ipp_t
*request
, /* IPP request */
171 *response
, /* IPP response */
172 *supported
; /* get-printer-attributes response */
173 time_t start_time
; /* Time of first connect */
174 int contimeout
; /* Connection timeout */
175 int delay
, /* Delay for retries */
176 prev_delay
; /* Previous delay */
177 const char *compression
; /* Compression mode */
178 int waitjob
, /* Wait for job complete? */
179 waitprinter
; /* Wait for printer ready? */
180 _cups_monitor_t monitor
; /* Monitoring data */
181 ipp_attribute_t
*job_id_attr
; /* job-id attribute */
182 int job_id
; /* job-id value */
183 ipp_attribute_t
*job_sheets
; /* job-media-sheets-completed */
184 ipp_attribute_t
*job_state
; /* job-state */
185 ipp_attribute_t
*copies_sup
; /* copies-supported */
186 ipp_attribute_t
*cups_version
; /* cups-version */
187 ipp_attribute_t
*format_sup
; /* document-format-supported */
188 ipp_attribute_t
*media_col_sup
; /* media-col-supported */
189 ipp_attribute_t
*operations_sup
; /* operations-supported */
190 ipp_attribute_t
*doc_handling_sup
; /* multiple-document-handling-supported */
191 ipp_attribute_t
*printer_state
; /* printer-state attribute */
192 ipp_attribute_t
*printer_accepting
; /* printer-is-accepting-jobs */
193 int validate_job
; /* Does printer support Validate-Job? */
194 int copies
, /* Number of copies for job */
195 copies_remaining
; /* Number of copies remaining */
196 const char *content_type
, /* CONTENT_TYPE environment variable */
197 *final_content_type
, /* FINAL_CONTENT_TYPE environment var */
198 *document_format
; /* document-format value */
199 int fd
; /* File descriptor */
200 off_t bytes
; /* Bytes copied */
201 char buffer
[16384]; /* Copy buffer */
202 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
203 struct sigaction action
; /* Actions for POSIX signals */
204 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
205 int version
; /* IPP version */
206 ppd_file_t
*ppd
; /* PPD file */
207 _ppd_cache_t
*pc
; /* PPD cache and mapping data */
211 * Make sure status messages are not buffered...
214 setbuf(stderr
, NULL
);
217 * Ignore SIGPIPE and catch SIGTERM signals...
221 sigset(SIGPIPE
, SIG_IGN
);
222 sigset(SIGTERM
, sigterm_handler
);
223 #elif defined(HAVE_SIGACTION)
224 memset(&action
, 0, sizeof(action
));
225 action
.sa_handler
= SIG_IGN
;
226 sigaction(SIGPIPE
, &action
, NULL
);
228 sigemptyset(&action
.sa_mask
);
229 sigaddset(&action
.sa_mask
, SIGTERM
);
230 action
.sa_handler
= sigterm_handler
;
231 sigaction(SIGTERM
, &action
, NULL
);
233 signal(SIGPIPE
, SIG_IGN
);
234 signal(SIGTERM
, sigterm_handler
);
235 #endif /* HAVE_SIGSET */
238 * Check command-line...
245 if ((s
= strrchr(argv
[0], '/')) != NULL
)
250 printf("network %s \"Unknown\" \"%s (%s)\"\n",
251 s
, _cupsLangString(cupsLangDefault(),
252 _("Internet Printing Protocol")), s
);
253 return (CUPS_BACKEND_OK
);
257 _cupsLangPrintf(stderr
,
258 _("Usage: %s job-id user title copies options [file]"),
260 return (CUPS_BACKEND_STOP
);
264 * Get the (final) content type...
267 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
268 content_type
= "application/octet-stream";
270 if ((final_content_type
= getenv("FINAL_CONTENT_TYPE")) == NULL
)
272 final_content_type
= content_type
;
274 if (!strncmp(final_content_type
, "printer/", 8))
275 final_content_type
= "application/vnd.cups-raw";
279 * Extract the hostname and printer name from the URI...
282 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
284 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
287 if (getenv("CLASS") != NULL
)
288 return (CUPS_BACKEND_FAILED
);
291 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
292 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
293 resource
, sizeof(resource
));
296 port
= IPP_PORT
; /* Default to port 631 */
298 if (!strcmp(scheme
, "https"))
299 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
301 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
304 * See if there are any options...
311 contimeout
= 7 * 24 * 60 * 60;
313 if ((optptr
= strchr(resource
, '?')) != NULL
)
316 * Yup, terminate the device name string and move to the first
317 * character of the optptr...
323 * Then parse the optptr...
334 while (*optptr
&& *optptr
!= '=' && *optptr
!= '+' && *optptr
!= '&')
337 if ((sep
= *optptr
) != '\0')
348 while (*optptr
&& *optptr
!= '+' && *optptr
!= '&')
358 * Process the option...
361 if (!strcasecmp(name
, "waitjob"))
364 * Wait for job completion?
367 waitjob
= !strcasecmp(value
, "on") ||
368 !strcasecmp(value
, "yes") ||
369 !strcasecmp(value
, "true");
371 else if (!strcasecmp(name
, "waitprinter"))
374 * Wait for printer idle?
377 waitprinter
= !strcasecmp(value
, "on") ||
378 !strcasecmp(value
, "yes") ||
379 !strcasecmp(value
, "true");
381 else if (!strcasecmp(name
, "encryption"))
384 * Enable/disable encryption?
387 if (!strcasecmp(value
, "always"))
388 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
389 else if (!strcasecmp(value
, "required"))
390 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
391 else if (!strcasecmp(value
, "never"))
392 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
393 else if (!strcasecmp(value
, "ifrequested"))
394 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
397 _cupsLangPrintFilter(stderr
, "ERROR",
398 _("Unknown encryption option value: \"%s\"."),
402 else if (!strcasecmp(name
, "version"))
404 if (!strcmp(value
, "1.0"))
406 else if (!strcmp(value
, "1.1"))
408 else if (!strcmp(value
, "2.0"))
410 else if (!strcmp(value
, "2.1"))
412 else if (!strcmp(value
, "2.2"))
416 _cupsLangPrintFilter(stderr
, "ERROR",
417 _("Unknown version option value: \"%s\"."),
422 else if (!strcasecmp(name
, "compression"))
424 if (!strcasecmp(value
, "true") || !strcasecmp(value
, "yes") ||
425 !strcasecmp(value
, "on") || !strcasecmp(value
, "gzip"))
426 compression
= "gzip";
428 #endif /* HAVE_LIBZ */
429 else if (!strcasecmp(name
, "contimeout"))
432 * Set the connection timeout...
436 contimeout
= atoi(value
);
444 _cupsLangPrintFilter(stderr
, "ERROR",
445 _("Unknown option \"%s\" with value \"%s\"."),
452 * If we have 7 arguments, print the file named on the command-line.
453 * Otherwise, copy stdin to a temporary file and print the temporary
460 send_options
= !strcasecmp(final_content_type
, "application/pdf") ||
461 !strcasecmp(final_content_type
, "application/vnd.cups-pdf") ||
462 !strncasecmp(final_content_type
, "image/", 6);
464 fputs("DEBUG: Sending stdin for job...\n", stderr
);
469 * Point to the files on the command-line...
472 num_files
= argc
- 6;
478 compress_files(num_files
, files
);
479 #endif /* HAVE_LIBZ */
481 fprintf(stderr
, "DEBUG: %d files to send in job...\n", num_files
);
485 * Set the authentication info, if any...
488 cupsSetPasswordCB(password_cb
);
493 * Use authenticaion information in the device URI...
496 if ((password
= strchr(username
, ':')) != NULL
)
499 cupsSetUser(username
);
504 * Try loading authentication information from the environment.
507 const char *ptr
= getenv("AUTH_USERNAME");
512 password
= getenv("AUTH_PASSWORD");
516 * Try finding the remote server...
519 start_time
= time(NULL
);
521 sprintf(portname
, "%d", port
);
523 fputs("STATE: +connecting-to-device\n", stderr
);
524 fprintf(stderr
, "DEBUG: Looking up \"%s\"...\n", hostname
);
526 while ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
528 _cupsLangPrintFilter(stderr
, "INFO",
529 _("Unable to locate printer \"%s\"."), hostname
);
532 if (getenv("CLASS") != NULL
)
534 fputs("STATE: -connecting-to-device\n", stderr
);
535 return (CUPS_BACKEND_STOP
);
539 http
= _httpCreate(hostname
, port
, addrlist
, cupsEncryption(), AF_UNSPEC
);
542 * See if the printer supports SNMP...
545 if ((snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
)) >= 0)
547 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
),
551 have_supplies
= start_count
= 0;
554 * Wait for data from the filter...
558 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), 0, backendNetworkSideCB
))
559 return (CUPS_BACKEND_OK
);
562 * Try connecting to the remote server...
565 delay
= _cupsNextDelay(0, &prev_delay
);
569 fprintf(stderr
, "DEBUG: Connecting to %s:%d\n", hostname
, port
);
570 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
572 if (httpReconnect(http
))
574 int error
= errno
; /* Connection error */
576 if (http
->status
== HTTP_PKI_ERROR
)
577 fputs("STATE: +cups-certificate-error\n", stderr
);
582 if (getenv("CLASS") != NULL
)
585 * If the CLASS environment variable is set, the job was submitted
586 * to a class and not to a specific queue. In this case, we want
587 * to abort immediately so that the job can be requeued on the next
588 * available printer in the class.
591 _cupsLangPrintFilter(stderr
, "INFO",
592 _("Unable to contact printer, queuing on next "
593 "printer in class."));
596 * Sleep 5 seconds to keep the job from requeuing too rapidly...
601 fputs("STATE: -connecting-to-device\n", stderr
);
603 return (CUPS_BACKEND_FAILED
);
606 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
608 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
609 errno
== EHOSTUNREACH
)
611 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
613 _cupsLangPrintFilter(stderr
, "ERROR",
614 _("The printer is not responding."));
615 fputs("STATE: -connecting-to-device\n", stderr
);
616 return (CUPS_BACKEND_FAILED
);
622 _cupsLangPrintFilter(stderr
, "WARNING",
623 _("Network printer \"%s\" may not exist or "
624 "is unavailable at this time."),
629 _cupsLangPrintFilter(stderr
, "WARNING",
630 _("Network printer \"%s\" is unreachable at "
631 "this time."), hostname
);
636 _cupsLangPrintFilter(stderr
, "WARNING",
637 _("Network printer \"%s\" is busy."),
644 delay
= _cupsNextDelay(delay
, &prev_delay
);
648 _cupsLangPrintFilter(stderr
, "ERROR",
649 _("Network printer \"%s\" is not responding."),
658 fputs("STATE: -cups-certificate-error\n", stderr
);
660 while (http
->fd
< 0);
662 if (job_canceled
|| !http
)
663 return (CUPS_BACKEND_FAILED
);
665 fputs("STATE: -connecting-to-device\n", stderr
);
666 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
669 if (http
->hostaddr
->addr
.sa_family
== AF_INET6
)
670 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
671 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
672 ntohs(http
->hostaddr
->ipv6
.sin6_port
));
674 #endif /* AF_INET6 */
675 if (http
->hostaddr
->addr
.sa_family
== AF_INET
)
676 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4)...\n",
677 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
678 ntohs(http
->hostaddr
->ipv4
.sin_port
));
681 * Build a URI for the printer and fill the standard IPP attributes for
682 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
683 * might contain username:password information...
686 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), scheme
, NULL
, hostname
,
690 * First validate the destination and see if the device supports multiple
697 media_col_sup
= NULL
;
699 operations_sup
= NULL
;
700 doc_handling_sup
= NULL
;
706 * Check for side-channel requests...
709 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
712 * Build the IPP request...
715 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
716 request
->request
.op
.version
[0] = version
/ 10;
717 request
->request
.op
.version
[1] = version
% 10;
719 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
722 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
723 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
730 fputs("DEBUG: Getting supported attributes...\n", stderr
);
732 if (http
->version
< HTTP_1_1
)
734 fprintf(stderr
, "DEBUG: Printer responded with HTTP version %d.%d.\n",
735 http
->version
/ 100, http
->version
% 100);
737 _cupsLangPrintFilter(stderr
, "ERROR",
738 _("This printer does not conform to the IPP "
739 "standard. Please contact the manufacturer of "
740 "your printer for assistance."));
743 supported
= cupsDoRequest(http
, request
, resource
);
744 ipp_status
= cupsLastError();
746 if (ipp_status
> IPP_OK_CONFLICT
)
748 fprintf(stderr
, "DEBUG: Get-Printer-Attributes returned %s.\n",
749 ippErrorString(ipp_status
));
751 if (ipp_status
== IPP_PRINTER_BUSY
||
752 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
754 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
756 _cupsLangPrintFilter(stderr
, "ERROR",
757 _("The printer is not responding."));
758 return (CUPS_BACKEND_FAILED
);
761 _cupsLangPrintFilter(stderr
, "WARNING",
762 _("Network host \"%s\" is busy; will retry in %d "
763 "seconds."), hostname
, delay
);
765 report_printer_state(supported
, 0);
769 delay
= _cupsNextDelay(delay
, &prev_delay
);
771 else if ((ipp_status
== IPP_BAD_REQUEST
||
772 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
> 10)
775 * Switch to IPP/1.1 or IPP/1.0...
780 _cupsLangPrintFilter(stderr
, "INFO",
781 _("Printer does not support IPP/%d.%d, trying "
782 "IPP/%s."), version
/ 10, version
% 10, "1.1");
787 _cupsLangPrintFilter(stderr
, "INFO",
788 _("Printer does not support IPP/%d.%d, trying "
789 "IPP/%s."), version
/ 10, version
% 10, "1.0");
795 else if (ipp_status
== IPP_NOT_FOUND
)
797 _cupsLangPrintFilter(stderr
, "ERROR",
798 _("The printer URI is incorrect or no longer "
802 ippDelete(supported
);
804 return (CUPS_BACKEND_STOP
);
806 else if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
)
808 if (!strncmp(httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
),
810 auth_info_required
= "negotiate";
812 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
813 return (CUPS_BACKEND_AUTH_REQUIRED
);
817 _cupsLangPrintFilter(stderr
, "ERROR",
818 _("Unable to get printer status: %s"),
819 cupsLastErrorString());
824 ippDelete(supported
);
830 * Check for supported attributes...
833 if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
834 IPP_TAG_RANGE
)) != NULL
)
837 * Has the "copies-supported" attribute - does it have an upper
841 fprintf(stderr
, "DEBUG: copies-supported=%d-%d\n",
842 copies_sup
->values
[0].range
.lower
,
843 copies_sup
->values
[0].range
.upper
);
845 if (copies_sup
->values
[0].range
.upper
<= 1)
846 copies_sup
= NULL
; /* No */
849 cups_version
= ippFindAttribute(supported
, "cups-version", IPP_TAG_TEXT
);
851 if ((format_sup
= ippFindAttribute(supported
, "document-format-supported",
852 IPP_TAG_MIMETYPE
)) != NULL
)
854 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
855 format_sup
->num_values
);
856 for (i
= 0; i
< format_sup
->num_values
; i
++)
857 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
858 format_sup
->values
[i
].string
.text
);
861 if ((media_col_sup
= ippFindAttribute(supported
, "media-col-supported",
862 IPP_TAG_KEYWORD
)) != NULL
)
864 fprintf(stderr
, "DEBUG: media-col-supported (%d values)\n",
865 media_col_sup
->num_values
);
866 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
867 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
868 media_col_sup
->values
[i
].string
.text
);
871 if ((operations_sup
= ippFindAttribute(supported
, "operations-supported",
872 IPP_TAG_ENUM
)) != NULL
)
874 for (i
= 0; i
< operations_sup
->num_values
; i
++)
875 if (operations_sup
->values
[i
].integer
== IPP_VALIDATE_JOB
)
883 _cupsLangPrintFilter(stderr
, "WARNING",
884 _("This printer does not conform to the IPP "
885 "standard and may not work."));
886 fputs("DEBUG: operations-supported does not list Validate-Job.\n",
892 _cupsLangPrintFilter(stderr
, "WARNING",
893 _("This printer does not conform to the IPP "
894 "standard and may not work."));
895 fputs("DEBUG: operations-supported not returned in "
896 "Get-Printer-Attributes request.\n", stderr
);
899 doc_handling_sup
= ippFindAttribute(supported
,
900 "multiple-document-handling-supported",
903 report_printer_state(supported
, 0);
905 while (ipp_status
> IPP_OK_CONFLICT
);
908 * See if the printer is accepting jobs and is not stopped; if either
909 * condition is true and we are printing to a class, requeue the job...
912 if (getenv("CLASS") != NULL
)
914 printer_state
= ippFindAttribute(supported
, "printer-state",
916 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
919 if (printer_state
== NULL
||
920 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
922 printer_accepting
== NULL
||
923 !printer_accepting
->values
[0].boolean
)
926 * If the CLASS environment variable is set, the job was submitted
927 * to a class and not to a specific queue. In this case, we want
928 * to abort immediately so that the job can be requeued on the next
929 * available printer in the class.
932 _cupsLangPrintFilter(stderr
, "INFO",
933 _("Unable to contact printer, queuing on next "
934 "printer in class."));
936 ippDelete(supported
);
940 * Sleep 5 seconds to keep the job from requeuing too rapidly...
945 return (CUPS_BACKEND_FAILED
);
950 * See if the printer supports multiple copies...
953 copies
= atoi(argv
[4]);
955 if (copies_sup
|| argc
< 7)
957 copies_remaining
= 1;
959 if (argc
< 7 && !send_options
)
963 copies_remaining
= copies
;
966 * Prepare remaining printing options...
974 num_options
= cupsParseOptions(argv
[5], 0, &options
);
976 if (!cups_version
&& media_col_sup
)
979 * Load the PPD file and generate PWG attribute mapping information...
982 ppd
= ppdOpenFile(getenv("PPD"));
983 pc
= _ppdCacheCreateWithPPD(ppd
);
991 document_format
= NULL
;
993 if (format_sup
!= NULL
)
995 for (i
= 0; i
< format_sup
->num_values
; i
++)
996 if (!strcasecmp(final_content_type
, format_sup
->values
[i
].string
.text
))
998 document_format
= final_content_type
;
1002 if (!document_format
)
1004 for (i
= 0; i
< format_sup
->num_values
; i
++)
1005 if (!strcasecmp("application/octet-stream",
1006 format_sup
->values
[i
].string
.text
))
1008 document_format
= "application/octet-stream";
1015 * If the printer does not support HTTP/1.1 (which IPP requires), copy stdin
1016 * to a temporary file so that we can do a HTTP/1.0 submission...
1018 * (I hate compatibility hacks!)
1021 if (http
->version
< HTTP_1_1
&& num_files
== 0)
1023 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
1025 perror("DEBUG: Unable to create temporary file");
1026 return (CUPS_BACKEND_FAILED
);
1029 _cupsLangPrintFilter(stderr
, "INFO", _("Copying print data."));
1031 compatsize
= backendRunLoop(-1, fd
, snmp_fd
, &(addrlist
->addr
), 0, 0,
1032 backendNetworkSideCB
);
1036 compatfile
= tmpfilename
;
1037 files
= &compatfile
;
1040 else if (http
->version
< HTTP_1_1
&& num_files
== 1)
1042 struct stat fileinfo
; /* File information */
1044 if (!stat(files
[0], &fileinfo
))
1045 compatsize
= fileinfo
.st_size
;
1049 * Start monitoring the printer in the background...
1053 monitor
.hostname
= hostname
;
1054 monitor
.user
= argv
[2];
1055 monitor
.resource
= resource
;
1056 monitor
.port
= port
;
1057 monitor
.version
= version
;
1059 monitor
.encryption
= cupsEncryption();
1060 monitor
.job_state
= IPP_JOB_PENDING
;
1061 monitor
.printer_state
= IPP_PRINTER_IDLE
;
1063 _cupsThreadCreate((_cups_thread_func_t
)monitor_printer
, &monitor
);
1066 * Validate access to the printer...
1069 while (!job_canceled
)
1071 request
= new_request(IPP_VALIDATE_JOB
, version
, uri
, argv
[2], argv
[3],
1072 num_options
, options
, compression
,
1073 copies_sup
? copies
: 1, document_format
, pc
,
1074 media_col_sup
, doc_handling_sup
);
1076 ippDelete(cupsDoRequest(http
, request
, resource
));
1078 ipp_status
= cupsLastError();
1080 if (ipp_status
> IPP_OK_CONFLICT
&&
1081 ipp_status
!= IPP_OPERATION_NOT_SUPPORTED
)
1086 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1087 ipp_status
== IPP_PRINTER_BUSY
)
1089 _cupsLangPrintFilter(stderr
, "INFO",
1090 _("Printer busy; will retry in 10 seconds."));
1096 * Update auth-info-required as needed...
1099 _cupsLangPrintFilter(stderr
, "ERROR", "%s", cupsLastErrorString());
1101 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
)
1103 fprintf(stderr
, "DEBUG: WWW-Authenticate=\"%s\"\n",
1104 httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
));
1107 * Normal authentication goes through the password callback, which sets
1108 * auth_info_required to "username,password". Kerberos goes directly
1109 * through GSSAPI, so look for Negotiate in the WWW-Authenticate header
1110 * here and set auth_info_required as needed...
1113 if (!strncmp(httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
),
1115 auth_info_required
= "negotiate";
1126 * Then issue the print-job request...
1131 while (!job_canceled
&& copies_remaining
> 0)
1134 * Check for side-channel requests...
1137 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1140 * Build the IPP job creation request...
1146 request
= new_request(num_files
> 1 ? IPP_CREATE_JOB
: IPP_PRINT_JOB
,
1147 version
, uri
, argv
[2], argv
[3], num_options
, options
,
1148 compression
, copies_sup
? copies
: 1, document_format
,
1149 pc
, media_col_sup
, doc_handling_sup
);
1156 response
= cupsDoRequest(http
, request
, resource
);
1159 size_t length
= 0; /* Length of request */
1163 fputs("DEBUG: Sending file using HTTP/1.0 Content-Length...\n", stderr
);
1164 length
= ippLength(request
) + (size_t)compatsize
;
1167 fputs("DEBUG: Sending file using HTTP/1.1 chunking...\n", stderr
);
1169 http_status
= cupsSendRequest(http
, request
, resource
, length
);
1170 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
)
1173 fd
= open(files
[0], O_RDONLY
);
1177 while ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1179 fprintf(stderr
, "DEBUG: Read %d bytes...\n", (int)bytes
);
1181 if (cupsWriteRequestData(http
, buffer
, bytes
) != HTTP_CONTINUE
)
1186 * Check for side-channel requests...
1189 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1197 response
= cupsGetResponse(http
, resource
);
1201 ipp_status
= cupsLastError();
1203 if (ipp_status
> IPP_OK_CONFLICT
)
1210 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1211 ipp_status
== IPP_PRINTER_BUSY
)
1213 _cupsLangPrintFilter(stderr
, "INFO",
1214 _("Printer busy; will retry in 10 seconds."));
1220 * Update auth-info-required as needed...
1223 _cupsLangPrintFilter(stderr
, "ERROR",
1224 _("Print file was not accepted: %s"),
1225 cupsLastErrorString());
1227 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
)
1229 fprintf(stderr
, "DEBUG: WWW-Authenticate=\"%s\"\n",
1230 httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
));
1233 * Normal authentication goes through the password callback, which sets
1234 * auth_info_required to "username,password". Kerberos goes directly
1235 * through GSSAPI, so look for Negotiate in the WWW-Authenticate header
1236 * here and set auth_info_required as needed...
1239 if (!strncmp(httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
),
1241 auth_info_required
= "negotiate";
1245 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
1246 IPP_TAG_INTEGER
)) == NULL
)
1248 _cupsLangPrintFilter(stderr
, "INFO",
1249 _("Print file accepted - job ID unknown."));
1254 monitor
.job_id
= job_id
= job_id_attr
->values
[0].integer
;
1255 _cupsLangPrintFilter(stderr
, "INFO",
1256 _("Print file accepted - job ID %d."), job_id
);
1259 ippDelete(response
);
1264 if (job_id
&& num_files
> 1)
1266 for (i
= 0; i
< num_files
; i
++)
1269 * Check for side-channel requests...
1272 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1275 * Send the next file in the job...
1278 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
1279 request
->request
.op
.version
[0] = version
/ 10;
1280 request
->request
.op
.version
[1] = version
% 10;
1282 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1285 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1289 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1290 "requesting-user-name", NULL
, argv
[2]);
1292 if ((i
+ 1) == num_files
)
1293 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
1295 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1296 "document-format", NULL
, content_type
);
1298 fprintf(stderr
, "DEBUG: Sending file %d using chunking...\n", i
+ 1);
1299 http_status
= cupsSendRequest(http
, request
, resource
, 0);
1300 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
&&
1301 (fd
= open(files
[i
], O_RDONLY
)) >= 0)
1303 while ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1305 if (cupsWriteRequestData(http
, buffer
, bytes
) != HTTP_CONTINUE
)
1310 * Check for side-channel requests...
1313 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1320 ippDelete(cupsGetResponse(http
, resource
));
1323 if (cupsLastError() > IPP_OK_CONFLICT
)
1325 ipp_status
= cupsLastError();
1327 _cupsLangPrintFilter(stderr
, "ERROR",
1328 _("Unable to add file to job: %s"),
1329 cupsLastErrorString());
1335 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
1337 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
1338 copies_remaining
--;
1340 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1341 ipp_status
== IPP_PRINTER_BUSY
)
1344 copies_remaining
--;
1347 * Wait for the job to complete...
1350 if (!job_id
|| !waitjob
)
1353 _cupsLangPrintFilter(stderr
, "INFO", _("Waiting for job to complete."));
1355 for (delay
= _cupsNextDelay(0, &prev_delay
); !job_canceled
;)
1358 * Check for side-channel requests...
1361 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1364 * Build an IPP_GET_JOB_ATTRIBUTES request...
1367 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1368 request
->request
.op
.version
[0] = version
/ 10;
1369 request
->request
.op
.version
[1] = version
% 10;
1371 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1374 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1378 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1379 "requesting-user-name", NULL
, argv
[2]);
1381 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1382 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
1389 httpReconnect(http
);
1390 response
= cupsDoRequest(http
, request
, resource
);
1391 ipp_status
= cupsLastError();
1393 if (ipp_status
== IPP_NOT_FOUND
)
1396 * Job has gone away and/or the server has no job history...
1399 ippDelete(response
);
1401 ipp_status
= IPP_OK
;
1405 if (ipp_status
> IPP_OK_CONFLICT
)
1407 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1408 ipp_status
!= IPP_PRINTER_BUSY
)
1410 ippDelete(response
);
1412 _cupsLangPrintFilter(stderr
, "ERROR",
1413 _("Unable to get job attributes: %s"),
1414 cupsLastErrorString());
1421 if ((job_state
= ippFindAttribute(response
, "job-state",
1422 IPP_TAG_ENUM
)) != NULL
)
1425 * Stop polling if the job is finished or pending-held...
1428 if (job_state
->values
[0].integer
> IPP_JOB_STOPPED
)
1430 if ((job_sheets
= ippFindAttribute(response
,
1431 "job-media-sheets-completed",
1432 IPP_TAG_INTEGER
)) != NULL
)
1433 fprintf(stderr
, "PAGE: total %d\n",
1434 job_sheets
->values
[0].integer
);
1436 ippDelete(response
);
1443 * If the printer does not return a job-state attribute, it does not
1444 * conform to the IPP specification - break out immediately and fail
1448 fputs("DEBUG: No job-state available from printer - stopping queue.\n",
1450 ipp_status
= IPP_INTERNAL_ERROR
;
1455 ippDelete(response
);
1458 * Wait before polling again...
1463 delay
= _cupsNextDelay(delay
, &prev_delay
);
1468 * Cancel the job as needed...
1471 if (job_canceled
&& job_id
)
1472 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1475 * Check the printer state and report it if necessary...
1478 check_printer_state(http
, uri
, resource
, argv
[2], version
, job_id
);
1481 * Collect the final page count as needed...
1484 if (have_supplies
&&
1485 !backendSNMPSupplies(snmp_fd
, http
->hostaddr
, &page_count
, NULL
) &&
1486 page_count
> start_count
)
1487 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
1491 * See if we used Kerberos at all...
1495 auth_info_required
= "negotiate";
1496 #endif /* HAVE_GSSAPI */
1504 cupsFreeOptions(num_options
, options
);
1505 _ppdCacheDestroy(pc
);
1509 ippDelete(supported
);
1512 * Remove the temporary file(s) if necessary...
1516 unlink(tmpfilename
);
1521 for (i
= 0; i
< num_files
; i
++)
1524 #endif /* HAVE_LIBZ */
1527 * Return the queue status...
1530 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
1531 ipp_status
== IPP_AUTHENTICATION_CANCELED
||
1532 ipp_status
<= IPP_OK_CONFLICT
)
1533 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
1535 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
1536 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1537 return (CUPS_BACKEND_AUTH_REQUIRED
);
1538 else if (ipp_status
== IPP_INTERNAL_ERROR
)
1539 return (CUPS_BACKEND_STOP
);
1540 else if (ipp_status
> IPP_OK_CONFLICT
)
1541 return (CUPS_BACKEND_FAILED
);
1544 _cupsLangPrintFilter(stderr
, "INFO", _("Ready to print."));
1545 return (CUPS_BACKEND_OK
);
1551 * 'cancel_job()' - Cancel a print job.
1555 cancel_job(http_t
*http
, /* I - HTTP connection */
1556 const char *uri
, /* I - printer-uri */
1557 int id
, /* I - job-id */
1558 const char *resource
, /* I - Resource path */
1559 const char *user
, /* I - requesting-user-name */
1560 int version
) /* I - IPP version */
1562 ipp_t
*request
; /* Cancel-Job request */
1565 _cupsLangPrintFilter(stderr
, "INFO", _("Canceling print job."));
1567 request
= ippNewRequest(IPP_CANCEL_JOB
);
1568 request
->request
.op
.version
[0] = version
/ 10;
1569 request
->request
.op
.version
[1] = version
% 10;
1571 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1573 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1575 if (user
&& user
[0])
1576 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1577 "requesting-user-name", NULL
, user
);
1583 ippDelete(cupsDoRequest(http
, request
, resource
));
1585 if (cupsLastError() > IPP_OK_CONFLICT
)
1586 _cupsLangPrintFilter(stderr
, "ERROR", _("Unable to cancel job: %s"),
1587 cupsLastErrorString());
1592 * 'check_printer_state()' - Check the printer state.
1595 static ipp_pstate_t
/* O - Current printer-state */
1596 check_printer_state(
1597 http_t
*http
, /* I - HTTP connection */
1598 const char *uri
, /* I - Printer URI */
1599 const char *resource
, /* I - Resource path */
1600 const char *user
, /* I - Username, if any */
1601 int version
, /* I - IPP version */
1604 ipp_t
*request
, /* IPP request */
1605 *response
; /* IPP response */
1606 ipp_attribute_t
*attr
; /* Attribute in response */
1607 ipp_pstate_t printer_state
= IPP_PRINTER_STOPPED
;
1608 /* Current printer-state */
1612 * Send a Get-Printer-Attributes request and log the results...
1615 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1616 request
->request
.op
.version
[0] = version
/ 10;
1617 request
->request
.op
.version
[1] = version
% 10;
1619 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1622 if (user
&& user
[0])
1623 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1624 "requesting-user-name", NULL
, user
);
1626 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1627 "requested-attributes",
1628 (int)(sizeof(pattrs
) / sizeof(pattrs
[0])), NULL
, pattrs
);
1630 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
1632 report_printer_state(response
, job_id
);
1634 if ((attr
= ippFindAttribute(response
, "printer-state",
1635 IPP_TAG_ENUM
)) != NULL
)
1636 printer_state
= (ipp_pstate_t
)attr
->values
[0].integer
;
1638 ippDelete(response
);
1642 * Return the printer-state value...
1645 return (printer_state
);
1651 * 'compress_files()' - Compress print files...
1655 compress_files(int num_files
, /* I - Number of files */
1656 char **files
) /* I - Files */
1658 int i
, /* Looping var */
1659 fd
; /* Temporary file descriptor */
1660 ssize_t bytes
; /* Bytes read/written */
1661 size_t total
; /* Total bytes read */
1662 cups_file_t
*in
, /* Input file */
1663 *out
; /* Output file */
1664 struct stat outinfo
; /* Output file information */
1665 char filename
[1024], /* Temporary filename */
1666 buffer
[32768]; /* Copy buffer */
1669 fprintf(stderr
, "DEBUG: Compressing %d job files...\n", num_files
);
1670 for (i
= 0; i
< num_files
; i
++)
1672 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
1674 _cupsLangPrintError("ERROR", _("Unable to create compressed print file"));
1675 exit(CUPS_BACKEND_FAILED
);
1678 if ((out
= cupsFileOpenFd(fd
, "w9")) == NULL
)
1680 _cupsLangPrintError("ERROR", _("Unable to open compressed print file"));
1681 exit(CUPS_BACKEND_FAILED
);
1684 if ((in
= cupsFileOpen(files
[i
], "r")) == NULL
)
1686 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1688 exit(CUPS_BACKEND_FAILED
);
1692 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
1693 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
1695 _cupsLangPrintError("ERROR",
1696 _("Unable to generate compressed print file"));
1699 exit(CUPS_BACKEND_FAILED
);
1707 files
[i
] = strdup(filename
);
1709 if (!stat(filename
, &outinfo
))
1711 "DEBUG: File %d compressed to %.1f%% of original size, "
1712 CUPS_LLFMT
" bytes...\n",
1713 i
+ 1, 100.0 * outinfo
.st_size
/ total
,
1714 CUPS_LLCAST outinfo
.st_size
);
1717 #endif /* HAVE_LIBZ */
1721 * 'monitor_printer()' - Monitor the printer state...
1724 static void * /* O - Thread exit code */
1726 _cups_monitor_t
*monitor
) /* I - Monitoring data */
1728 http_t
*http
; /* Connection to printer */
1729 ipp_t
*request
, /* IPP request */
1730 *response
; /* IPP response */
1731 ipp_attribute_t
*attr
; /* Attribute in response */
1732 int delay
, /* Current delay */
1733 prev_delay
; /* Previous delay */
1737 * Make a copy of the printer connection...
1740 http
= _httpCreate(monitor
->hostname
, monitor
->port
, NULL
, monitor
->encryption
,
1742 cupsSetPasswordCB(password_cb
);
1745 * Loop until the job is canceled, aborted, or completed.
1748 delay
= _cupsNextDelay(0, &prev_delay
);
1750 while (monitor
->job_state
< IPP_JOB_CANCELED
&& !job_canceled
)
1753 * Reconnect to the printer...
1756 if (!httpReconnect(http
))
1759 * Connected, so check on the printer state...
1762 monitor
->printer_state
= check_printer_state(http
, monitor
->uri
,
1768 if (monitor
->job_id
> 0)
1771 * Check the status of the job itself...
1774 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1775 request
->request
.op
.version
[0] = monitor
->version
/ 10;
1776 request
->request
.op
.version
[1] = monitor
->version
% 10;
1778 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1779 NULL
, monitor
->uri
);
1780 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1783 if (monitor
->user
&& monitor
->user
[0])
1784 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1785 "requesting-user-name", NULL
, monitor
->user
);
1787 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1788 "requested-attributes",
1789 (int)(sizeof(jattrs
) / sizeof(jattrs
[0])), NULL
, jattrs
);
1795 response
= cupsDoRequest(http
, request
, monitor
->resource
);
1797 if ((attr
= ippFindAttribute(response
, "job-state",
1798 IPP_TAG_ENUM
)) != NULL
)
1799 monitor
->job_state
= (ipp_jstate_t
)attr
->values
[0].integer
;
1801 monitor
->job_state
= IPP_JOB_COMPLETED
;
1803 ippDelete(response
);
1807 * Disconnect from the printer - we'll reconnect on the next poll...
1810 _httpDisconnect(http
);
1814 * Sleep for N seconds...
1819 delay
= _cupsNextDelay(delay
, &prev_delay
);
1823 * Cleanup and return...
1833 * 'new_request()' - Create a new print creation or validation request.
1836 static ipp_t
* /* O - Request data */
1838 ipp_op_t op
, /* I - IPP operation code */
1839 int version
, /* I - IPP version number */
1840 const char *uri
, /* I - printer-uri value */
1841 const char *user
, /* I - requesting-user-name value */
1842 const char *title
, /* I - job-name value */
1843 int num_options
, /* I - Number of options to send */
1844 cups_option_t
*options
, /* I - Options to send */
1845 const char *compression
, /* I - compression value or NULL */
1846 int copies
, /* I - copies value or 0 */
1847 const char *format
, /* I - documet-format value or NULL */
1848 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
1849 ipp_attribute_t
*media_col_sup
, /* I - media-col-supported values */
1850 ipp_attribute_t
*doc_handling_sup
) /* I - multiple-document-handling-supported values */
1852 int i
; /* Looping var */
1853 ipp_t
*request
; /* Request data */
1854 const char *keyword
; /* PWG keyword */
1855 _pwg_size_t
*size
; /* PWG media size */
1856 ipp_t
*media_col
, /* media-col value */
1857 *media_size
; /* media-size value */
1858 const char *media_source
, /* media-source value */
1859 *media_type
, /* media-type value */
1860 *collate_str
; /* multiple-document-handling value */
1864 * Create the IPP request...
1867 request
= ippNewRequest(op
);
1868 request
->request
.op
.version
[0] = version
/ 10;
1869 request
->request
.op
.version
[1] = version
% 10;
1871 fprintf(stderr
, "DEBUG: %s IPP/%d.%d\n",
1872 ippOpString(request
->request
.op
.operation_id
),
1873 request
->request
.op
.version
[0],
1874 request
->request
.op
.version
[1]);
1877 * Add standard attributes...
1880 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1882 fprintf(stderr
, "DEBUG: printer-uri=\"%s\"\n", uri
);
1886 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1887 "requesting-user-name", NULL
, user
);
1888 fprintf(stderr
, "DEBUG: requesting-user-name=\"%s\"\n", user
);
1891 if (title
&& *title
)
1893 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
1895 fprintf(stderr
, "DEBUG: job-name=\"%s\"\n", title
);
1900 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1901 "document-format", NULL
, format
);
1902 fprintf(stderr
, "DEBUG: document-format=\"%s\"\n", format
);
1908 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1909 "compression", NULL
, compression
);
1910 fprintf(stderr
, "DEBUG: compression=\"%s\"\n", compression
);
1912 #endif /* HAVE_LIBZ */
1915 * Handle options on the command-line...
1918 if (num_options
> 0)
1923 * Send standard IPP attributes...
1926 if ((keyword
= cupsGetOption("PageSize", num_options
, options
)) == NULL
)
1927 keyword
= cupsGetOption("media", num_options
, options
);
1929 if ((size
= _ppdCacheGetSize(pc
, keyword
)) != NULL
)
1932 * Add a media-col value...
1935 media_size
= ippNew();
1936 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1937 "x-dimension", size
->width
);
1938 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1939 "y-dimension", size
->length
);
1941 media_col
= ippNew();
1942 ippAddCollection(media_col
, IPP_TAG_ZERO
, "media-size", media_size
);
1944 media_source
= _ppdCacheGetSource(pc
, cupsGetOption("InputSlot",
1947 media_type
= _ppdCacheGetType(pc
, cupsGetOption("MediaType",
1951 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
1953 if (!strcmp(media_col_sup
->values
[i
].string
.text
,
1954 "media-left-margin"))
1955 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1956 "media-left-margin", size
->left
);
1957 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
1958 "media-bottom-margin"))
1959 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1960 "media-bottom-margin", size
->left
);
1961 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
1962 "media-right-margin"))
1963 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1964 "media-right-margin", size
->left
);
1965 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
1966 "media-top-margin"))
1967 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1968 "media-top-margin", size
->left
);
1969 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
1970 "media-source") && media_source
)
1971 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
1972 "media-source", NULL
, media_source
);
1973 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
1974 "media-type") && media_type
)
1975 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
1976 "media-type", NULL
, media_type
);
1979 ippAddCollection(request
, IPP_TAG_JOB
, "media-col", media_col
);
1982 if ((keyword
= cupsGetOption("output-bin", num_options
,
1984 keyword
= _ppdCacheGetBin(pc
, cupsGetOption("OutputBin", num_options
,
1988 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-bin",
1991 if ((keyword
= cupsGetOption("output-mode", num_options
,
1993 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
1995 else if ((keyword
= cupsGetOption("ColorModel", num_options
,
1998 if (!strcasecmp(keyword
, "Gray"))
1999 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
2000 NULL
, "monochrome");
2002 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
2006 if ((keyword
= cupsGetOption("print-quality", num_options
,
2008 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2010 else if ((keyword
= cupsGetOption("cupsPrintQuality", num_options
,
2013 if (!strcasecmp(keyword
, "draft"))
2014 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2016 else if (!strcasecmp(keyword
, "normal"))
2017 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2018 IPP_QUALITY_NORMAL
);
2019 else if (!strcasecmp(keyword
, "high"))
2020 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2024 if ((keyword
= cupsGetOption("sides", num_options
, options
)) != NULL
)
2025 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2027 else if (pc
->sides_option
&&
2028 (keyword
= cupsGetOption(pc
->sides_option
, num_options
,
2031 if (!strcasecmp(keyword
, pc
->sides_1sided
))
2032 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2034 else if (!strcasecmp(keyword
, pc
->sides_2sided_long
))
2035 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2036 NULL
, "two-sided-long-edge");
2037 if (!strcasecmp(keyword
, pc
->sides_2sided_short
))
2038 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2039 NULL
, "two-sided-short-edge");
2042 if (doc_handling_sup
&&
2043 (keyword
= cupsGetOption("collate", num_options
, options
)) != NULL
)
2045 if (!strcasecmp(keyword
, "true"))
2046 collate_str
= "separate-documents-collated-copies";
2048 collate_str
= "separate-documents-uncollated-copies";
2050 for (i
= 0; i
< doc_handling_sup
->num_values
; i
++)
2051 if (!strcmp(doc_handling_sup
->values
[i
].string
.text
, collate_str
))
2053 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2054 "multiple-document-handling", NULL
, collate_str
);
2062 * When talking to another CUPS server, send all options...
2065 cupsEncodeOptions(request
, num_options
, options
);
2069 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies", copies
);
2077 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
2080 static const char * /* O - Password */
2081 password_cb(const char *prompt
) /* I - Prompt (not used) */
2086 * Remember that we need to authenticate...
2089 auth_info_required
= "username,password";
2091 if (password
&& *password
&& password_tries
< 3)
2100 * Give up after 3 tries or if we don't have a password to begin with...
2109 * 'report_attr()' - Report an IPP attribute value.
2113 report_attr(ipp_attribute_t
*attr
) /* I - Attribute */
2115 int i
; /* Looping var */
2116 char value
[1024], /* Value string */
2117 *valptr
, /* Pointer into value string */
2118 *attrptr
; /* Pointer into attribute value */
2122 * Convert the attribute values into quoted strings...
2125 for (i
= 0, valptr
= value
;
2126 i
< attr
->num_values
&& valptr
< (value
+ sizeof(value
) - 10);
2132 switch (attr
->value_tag
)
2134 case IPP_TAG_INTEGER
:
2136 snprintf(valptr
, sizeof(value
) - (valptr
- value
), "%d",
2137 attr
->values
[i
].integer
);
2138 valptr
+= strlen(valptr
);
2143 case IPP_TAG_KEYWORD
:
2145 for (attrptr
= attr
->values
[i
].string
.text
;
2146 *attrptr
&& valptr
< (value
+ sizeof(value
) - 10);
2149 if (*attrptr
== '\\' || *attrptr
== '\"')
2152 *valptr
++ = *attrptr
;
2159 * Unsupported value type...
2169 * Tell the scheduler about the new values...
2172 fprintf(stderr
, "ATTR: %s=%s\n", attr
->name
, value
);
2177 * 'report_printer_state()' - Report the printer state.
2180 static int /* O - Number of reasons shown */
2181 report_printer_state(ipp_t
*ipp
, /* I - IPP response */
2182 int job_id
) /* I - Current job ID */
2184 int i
; /* Looping var */
2185 int count
; /* Count of reasons shown... */
2186 ipp_attribute_t
*pa
, /* printer-alert */
2187 *pam
, /* printer-alert-message */
2188 *psm
, /* printer-state-message */
2189 *reasons
, /* printer-state-reasons */
2190 *marker
; /* marker-* attributes */
2191 const char *reason
; /* Current reason */
2192 const char *prefix
; /* Prefix for STATE: line */
2193 char value
[1024], /* State/message string */
2194 *valptr
; /* Pointer into string */
2195 static int ipp_supplies
= -1;
2196 /* Report supply levels? */
2200 * Report alerts and messages...
2203 if ((pa
= ippFindAttribute(ipp
, "printer-alert", IPP_TAG_TEXT
)) != NULL
)
2206 if ((pam
= ippFindAttribute(ipp
, "printer-alert-message",
2207 IPP_TAG_TEXT
)) != NULL
)
2210 if ((psm
= ippFindAttribute(ipp
, "printer-state-message",
2211 IPP_TAG_TEXT
)) != NULL
)
2213 char *ptr
; /* Pointer into message */
2216 strlcpy(value
, "INFO: ", sizeof(value
));
2217 for (ptr
= psm
->values
[0].string
.text
, valptr
= value
+ 6;
2218 *ptr
&& valptr
< (value
+ sizeof(value
) - 6);
2221 if (*ptr
< ' ' && *ptr
> 0 && *ptr
!= '\t')
2224 * Substitute "<XX>" for the control character; sprintf is safe because
2225 * we always leave 6 chars free at the end...
2228 sprintf(valptr
, "<%02X>", *ptr
);
2238 fputs(value
, stderr
);
2242 * Now report printer-state-reasons, filtering out some of the reasons we never
2246 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
2247 IPP_TAG_KEYWORD
)) == NULL
)
2253 for (i
= 0, count
= 0, valptr
= value
; i
< reasons
->num_values
; i
++)
2255 reason
= reasons
->values
[i
].string
.text
;
2257 if (strcmp(reason
, "paused") &&
2258 strcmp(reason
, "com.apple.print.recoverable-warning"))
2260 strlcpy(valptr
, prefix
, sizeof(value
) - (valptr
- value
) - 1);
2261 valptr
+= strlen(valptr
);
2262 strlcpy(valptr
, reason
, sizeof(value
) - (valptr
- value
) - 1);
2263 valptr
+= strlen(valptr
);
2273 fputs(value
, stderr
);
2277 * Relay the current marker-* attribute values...
2280 if (ipp_supplies
< 0)
2282 ppd_file_t
*ppd
; /* PPD file */
2283 ppd_attr_t
*ppdattr
; /* Attribute in PPD file */
2285 if ((ppd
= ppdOpenFile(getenv("PPD"))) != NULL
&&
2286 (ppdattr
= ppdFindAttr(ppd
, "cupsIPPSupplies", NULL
)) != NULL
&&
2287 ppdattr
->value
&& strcasecmp(ppdattr
->value
, "true"))
2295 if (ipp_supplies
> 0)
2297 if ((marker
= ippFindAttribute(ipp
, "marker-colors", IPP_TAG_NAME
)) != NULL
)
2298 report_attr(marker
);
2299 if ((marker
= ippFindAttribute(ipp
, "marker-high-levels",
2300 IPP_TAG_INTEGER
)) != NULL
)
2301 report_attr(marker
);
2302 if ((marker
= ippFindAttribute(ipp
, "marker-levels",
2303 IPP_TAG_INTEGER
)) != NULL
)
2304 report_attr(marker
);
2305 if ((marker
= ippFindAttribute(ipp
, "marker-low-levels",
2306 IPP_TAG_INTEGER
)) != NULL
)
2307 report_attr(marker
);
2308 if ((marker
= ippFindAttribute(ipp
, "marker-message",
2309 IPP_TAG_TEXT
)) != NULL
)
2310 report_attr(marker
);
2311 if ((marker
= ippFindAttribute(ipp
, "marker-names", IPP_TAG_NAME
)) != NULL
)
2312 report_attr(marker
);
2313 if ((marker
= ippFindAttribute(ipp
, "marker-types",
2314 IPP_TAG_KEYWORD
)) != NULL
)
2315 report_attr(marker
);
2323 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
2327 sigterm_handler(int sig
) /* I - Signal */
2329 (void)sig
; /* remove compiler warnings... */
2334 * Flag that the job should be cancelled...
2342 * The scheduler already tried to cancel us once, now just terminate
2343 * after removing our temp files!
2347 unlink(tmpfilename
);
2354 * End of "$Id: ipp.c 7948 2008-09-17 00:04:12Z mike $".