4 * IPP backend for CUPS.
6 * Copyright 2007-2012 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 * run_as_user() - Run the IPP backend as the printing user.
30 * timeout_cb() - Handle HTTP timeouts.
31 * sigterm_handler() - Handle 'terminate' signals that stop the backend.
35 * Include necessary headers.
38 #include "backend-private.h"
39 #include <cups/array-private.h>
40 #include <sys/types.h>
43 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
45 # define kPMPrintUIToolAgent "com.apple.printuitool.agent"
46 # define kPMStartJob 100
47 # define kPMWaitForJob 101
48 # ifdef HAVE_XPC_PRIVATE_H
49 # include <xpc/private.h>
51 extern void xpc_connection_set_target_uid(xpc_connection_t connection
,
53 # endif /* HAVE_XPC_PRIVATE_H */
54 #endif /* HAVE_GSSAPI && HAVE_XPC */
61 typedef struct _cups_monitor_s
/**** Monitoring data ****/
63 const char *uri
, /* Printer URI */
64 *hostname
, /* Hostname */
66 *resource
; /* Resource path */
67 int port
, /* Port number */
68 version
, /* IPP version */
69 job_id
, /* Job ID for submitted job */
70 get_job_attrs
; /* Support Get-Job-Attributes? */
71 const char *job_name
; /* Job name for submitted job */
72 http_encryption_t encryption
; /* Use encryption? */
73 ipp_jstate_t job_state
; /* Current job state */
74 ipp_pstate_t printer_state
; /* Current printer state */
82 static const char *auth_info_required
;
83 /* New auth-info-required value */
84 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
85 static int child_pid
= 0; /* Child process ID */
86 #endif /* HAVE_GSSAPI && HAVE_XPC */
87 static const char * const jattrs
[] = /* Job attributes we want */
89 "job-impressions-completed",
90 "job-media-sheets-completed",
92 "job-originating-user-name",
96 static int job_canceled
= 0;
98 static char username
[256] = "",
99 /* Username for device URI */
101 /* Password for device URI */
102 static int password_tries
= 0;
104 static const char * const pattrs
[] = /* Printer attributes we want */
108 "document-format-supported",
110 "marker-high-levels",
116 "media-col-supported",
117 "multiple-document-handling-supported",
118 "operations-supported",
120 "printer-alert-description",
121 "printer-is-accepting-jobs",
123 "printer-state-message",
124 "printer-state-reasons"
126 static const char * const remote_job_states
[] =
127 { /* Remote job state keywords */
128 "+cups-remote-pending",
129 "+cups-remote-pending-held",
130 "+cups-remote-processing",
131 "+cups-remote-stopped",
132 "+cups-remote-canceled",
133 "+cups-remote-aborted",
134 "+cups-remote-completed"
136 static _cups_mutex_t report_mutex
= _CUPS_MUTEX_INITIALIZER
;
137 /* Mutex to control access */
138 static int num_attr_cache
= 0;
139 /* Number of cached attributes */
140 static cups_option_t
*attr_cache
= NULL
;
141 /* Cached attributes */
142 static cups_array_t
*state_reasons
; /* Array of printe-state-reasons keywords */
143 static char tmpfilename
[1024] = "";
144 /* Temporary spool file name */
151 static void cancel_job(http_t
*http
, const char *uri
, int id
,
152 const char *resource
, const char *user
,
154 static ipp_pstate_t
check_printer_state(http_t
*http
, const char *uri
,
155 const char *resource
,
156 const char *user
, int version
);
158 static void compress_files(int num_files
, char **files
);
159 #endif /* HAVE_LIBZ */
160 static void *monitor_printer(_cups_monitor_t
*monitor
);
161 static ipp_t
*new_request(ipp_op_t op
, int version
, const char *uri
,
162 const char *user
, const char *title
,
163 int num_options
, cups_option_t
*options
,
164 const char *compression
, int copies
,
165 const char *format
, _ppd_cache_t
*pc
,
166 ipp_attribute_t
*media_col_sup
,
167 ipp_attribute_t
*doc_handling_sup
);
168 static const char *password_cb(const char *);
169 static void report_attr(ipp_attribute_t
*attr
);
170 static void report_printer_state(ipp_t
*ipp
);
171 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
172 static int run_as_user(char *argv
[], uid_t uid
,
173 const char *device_uri
, int fd
);
174 #endif /* HAVE_GSSAPI && HAVE_XPC */
175 static void sigterm_handler(int sig
);
176 static int timeout_cb(http_t
*http
, void *user_data
);
177 static void update_reasons(ipp_attribute_t
*attr
, const char *s
);
181 * 'main()' - Send a file to the printer or server.
185 * printer-uri job-id user title copies options [file]
188 int /* O - Exit status */
189 main(int argc
, /* I - Number of command-line args */
190 char *argv
[]) /* I - Command-line arguments */
192 int i
; /* Looping var */
193 int send_options
; /* Send job options? */
194 int num_options
; /* Number of printer options */
195 cups_option_t
*options
; /* Printer options */
196 const char *device_uri
; /* Device URI */
197 char scheme
[255], /* Scheme in URI */
198 hostname
[1024], /* Hostname */
199 resource
[1024], /* Resource info (printer name) */
200 addrname
[256], /* Address name */
201 *optptr
, /* Pointer to URI options */
202 *name
, /* Name of option */
203 *value
, /* Value of option */
204 sep
; /* Separator character */
205 http_addrlist_t
*addrlist
; /* Address of printer */
206 int snmp_fd
, /* SNMP socket */
207 start_count
, /* Page count via SNMP at start */
208 page_count
, /* Page count via SNMP */
209 have_supplies
; /* Printer supports supply levels? */
210 int num_files
; /* Number of files to print */
211 char **files
, /* Files to print */
212 *compatfile
= NULL
; /* Compatibility filename */
213 off_t compatsize
= 0; /* Size of compatibility file */
214 int port
; /* Port number (not used) */
215 char portname
[255]; /* Port name */
216 char uri
[HTTP_MAX_URI
]; /* Updated URI without user/pass */
217 char print_job_name
[1024]; /* Update job-name for Print-Job */
218 http_status_t http_status
; /* Status of HTTP request */
219 ipp_status_t ipp_status
; /* Status of IPP request */
220 http_t
*http
; /* HTTP connection */
221 ipp_t
*request
, /* IPP request */
222 *response
, /* IPP response */
223 *supported
; /* get-printer-attributes response */
224 time_t start_time
; /* Time of first connect */
225 int contimeout
; /* Connection timeout */
226 int delay
, /* Delay for retries */
227 prev_delay
; /* Previous delay */
228 const char *compression
; /* Compression mode */
229 int waitjob
, /* Wait for job complete? */
230 waitprinter
; /* Wait for printer ready? */
231 _cups_monitor_t monitor
; /* Monitoring data */
232 ipp_attribute_t
*job_id_attr
; /* job-id attribute */
233 int job_id
; /* job-id value */
234 ipp_attribute_t
*job_sheets
; /* job-media-sheets-completed */
235 ipp_attribute_t
*job_state
; /* job-state */
236 ipp_attribute_t
*copies_sup
; /* copies-supported */
237 ipp_attribute_t
*cups_version
; /* cups-version */
238 ipp_attribute_t
*format_sup
; /* document-format-supported */
239 ipp_attribute_t
*media_col_sup
; /* media-col-supported */
240 ipp_attribute_t
*operations_sup
; /* operations-supported */
241 ipp_attribute_t
*doc_handling_sup
; /* multiple-document-handling-supported */
242 ipp_attribute_t
*printer_state
; /* printer-state attribute */
243 ipp_attribute_t
*printer_accepting
; /* printer-is-accepting-jobs */
244 int create_job
= 0, /* Does printer support Create-Job? */
245 get_job_attrs
= 0, /* Does printer support Get-Job-Attributes? */
246 send_document
= 0, /* Does printer support Send-Document? */
247 validate_job
= 0; /* Does printer support Validate-Job? */
248 int copies
, /* Number of copies for job */
249 copies_remaining
; /* Number of copies remaining */
250 const char *content_type
, /* CONTENT_TYPE environment variable */
251 *final_content_type
, /* FINAL_CONTENT_TYPE environment var */
252 *document_format
; /* document-format value */
253 int fd
; /* File descriptor */
254 off_t bytes
= 0; /* Bytes copied */
255 char buffer
[16384]; /* Copy buffer */
256 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
257 struct sigaction action
; /* Actions for POSIX signals */
258 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
259 int version
; /* IPP version */
260 ppd_file_t
*ppd
; /* PPD file */
261 _ppd_cache_t
*pc
; /* PPD cache and mapping data */
262 fd_set input
; /* Input set for select() */
266 * Make sure status messages are not buffered...
269 setbuf(stderr
, NULL
);
272 * Ignore SIGPIPE and catch SIGTERM signals...
276 sigset(SIGPIPE
, SIG_IGN
);
277 sigset(SIGTERM
, sigterm_handler
);
278 #elif defined(HAVE_SIGACTION)
279 memset(&action
, 0, sizeof(action
));
280 action
.sa_handler
= SIG_IGN
;
281 sigaction(SIGPIPE
, &action
, NULL
);
283 sigemptyset(&action
.sa_mask
);
284 sigaddset(&action
.sa_mask
, SIGTERM
);
285 action
.sa_handler
= sigterm_handler
;
286 sigaction(SIGTERM
, &action
, NULL
);
288 signal(SIGPIPE
, SIG_IGN
);
289 signal(SIGTERM
, sigterm_handler
);
290 #endif /* HAVE_SIGSET */
293 * Check command-line...
300 if ((s
= strrchr(argv
[0], '/')) != NULL
)
305 printf("network %s \"Unknown\" \"%s (%s)\"\n",
306 s
, _cupsLangString(cupsLangDefault(),
307 _("Internet Printing Protocol")), s
);
308 return (CUPS_BACKEND_OK
);
312 _cupsLangPrintf(stderr
,
313 _("Usage: %s job-id user title copies options [file]"),
315 return (CUPS_BACKEND_STOP
);
319 * Get the device URI...
322 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
324 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
327 if (getenv("CLASS") != NULL
)
328 return (CUPS_BACKEND_FAILED
);
331 if ((auth_info_required
= getenv("AUTH_INFO_REQUIRED")) == NULL
)
332 auth_info_required
= "none";
334 state_reasons
= _cupsArrayNewStrings(getenv("PRINTER_STATE_REASONS"));
338 * For Kerberos, become the printing user (if we can) to get the credentials
342 if (!getuid() && (value
= getenv("AUTH_UID")) != NULL
)
344 uid_t uid
= (uid_t
)atoi(value
);
351 return (run_as_user(argv
, uid
, device_uri
, 0));
354 int status
= 0; /* Exit status */
356 for (i
= 6; i
< argc
&& !status
&& !job_canceled
; i
++)
358 if ((fd
= open(argv
[i
], O_RDONLY
)) >= 0)
360 status
= run_as_user(argv
, uid
, device_uri
, fd
);
365 _cupsLangPrintError("ERROR", _("Unable to open print file"));
366 status
= CUPS_BACKEND_FAILED
;
374 # else /* No XPC, just try to run as the user ID */
377 # endif /* HAVE_XPC */
379 #endif /* HAVE_GSSAPI */
382 * Get the (final) content type...
385 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
386 content_type
= "application/octet-stream";
388 if ((final_content_type
= getenv("FINAL_CONTENT_TYPE")) == NULL
)
390 final_content_type
= content_type
;
392 if (!strncmp(final_content_type
, "printer/", 8))
393 final_content_type
= "application/vnd.cups-raw";
397 * Extract the hostname and printer name from the URI...
400 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
401 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
402 resource
, sizeof(resource
));
405 port
= IPP_PORT
; /* Default to port 631 */
407 if (!strcmp(scheme
, "https"))
408 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
410 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
413 * See if there are any options...
420 contimeout
= 7 * 24 * 60 * 60;
422 if ((optptr
= strchr(resource
, '?')) != NULL
)
425 * Yup, terminate the device name string and move to the first
426 * character of the optptr...
432 * Then parse the optptr...
443 while (*optptr
&& *optptr
!= '=' && *optptr
!= '+' && *optptr
!= '&')
446 if ((sep
= *optptr
) != '\0')
457 while (*optptr
&& *optptr
!= '+' && *optptr
!= '&')
467 * Process the option...
470 if (!_cups_strcasecmp(name
, "waitjob"))
473 * Wait for job completion?
476 waitjob
= !_cups_strcasecmp(value
, "on") ||
477 !_cups_strcasecmp(value
, "yes") ||
478 !_cups_strcasecmp(value
, "true");
480 else if (!_cups_strcasecmp(name
, "waitprinter"))
483 * Wait for printer idle?
486 waitprinter
= !_cups_strcasecmp(value
, "on") ||
487 !_cups_strcasecmp(value
, "yes") ||
488 !_cups_strcasecmp(value
, "true");
490 else if (!_cups_strcasecmp(name
, "encryption"))
493 * Enable/disable encryption?
496 if (!_cups_strcasecmp(value
, "always"))
497 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
498 else if (!_cups_strcasecmp(value
, "required"))
499 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
500 else if (!_cups_strcasecmp(value
, "never"))
501 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
502 else if (!_cups_strcasecmp(value
, "ifrequested"))
503 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
506 _cupsLangPrintFilter(stderr
, "ERROR",
507 _("Unknown encryption option value: \"%s\"."),
511 else if (!_cups_strcasecmp(name
, "version"))
513 if (!strcmp(value
, "1.0"))
515 else if (!strcmp(value
, "1.1"))
517 else if (!strcmp(value
, "2.0"))
519 else if (!strcmp(value
, "2.1"))
521 else if (!strcmp(value
, "2.2"))
525 _cupsLangPrintFilter(stderr
, "ERROR",
526 _("Unknown version option value: \"%s\"."),
531 else if (!_cups_strcasecmp(name
, "compression"))
533 if (!_cups_strcasecmp(value
, "true") || !_cups_strcasecmp(value
, "yes") ||
534 !_cups_strcasecmp(value
, "on") || !_cups_strcasecmp(value
, "gzip"))
535 compression
= "gzip";
537 #endif /* HAVE_LIBZ */
538 else if (!_cups_strcasecmp(name
, "contimeout"))
541 * Set the connection timeout...
545 contimeout
= atoi(value
);
553 _cupsLangPrintFilter(stderr
, "ERROR",
554 _("Unknown option \"%s\" with value \"%s\"."),
561 * If we have 7 arguments, print the file named on the command-line.
562 * Otherwise, copy stdin to a temporary file and print the temporary
570 send_options
= !_cups_strcasecmp(final_content_type
, "application/pdf") ||
571 !_cups_strcasecmp(final_content_type
, "application/vnd.cups-pdf") ||
572 !_cups_strncasecmp(final_content_type
, "image/", 6);
574 fputs("DEBUG: Sending stdin for job...\n", stderr
);
579 * Point to the files on the command-line...
582 num_files
= argc
- 6;
588 compress_files(num_files
, files
);
589 #endif /* HAVE_LIBZ */
591 fprintf(stderr
, "DEBUG: %d files to send in job...\n", num_files
);
595 * Set the authentication info, if any...
598 cupsSetPasswordCB(password_cb
);
603 * Use authenticaion information in the device URI...
606 if ((password
= strchr(username
, ':')) != NULL
)
609 cupsSetUser(username
);
614 * Try loading authentication information from the environment.
617 const char *ptr
= getenv("AUTH_USERNAME");
621 strlcpy(username
, ptr
, sizeof(username
));
625 password
= getenv("AUTH_PASSWORD");
629 * Try finding the remote server...
632 start_time
= time(NULL
);
634 sprintf(portname
, "%d", port
);
636 update_reasons(NULL
, "+connecting-to-device");
637 fprintf(stderr
, "DEBUG: Looking up \"%s\"...\n", hostname
);
639 while ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
641 _cupsLangPrintFilter(stderr
, "INFO",
642 _("Unable to locate printer \"%s\"."), hostname
);
645 if (getenv("CLASS") != NULL
)
647 update_reasons(NULL
, "-connecting-to-device");
648 return (CUPS_BACKEND_STOP
);
652 return (CUPS_BACKEND_OK
);
655 http
= _httpCreate(hostname
, port
, addrlist
, cupsEncryption(), AF_UNSPEC
);
656 httpSetTimeout(http
, 30.0, timeout_cb
, NULL
);
659 * See if the printer supports SNMP...
662 if ((snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
)) >= 0)
664 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
),
668 have_supplies
= start_count
= 0;
671 * Wait for data from the filter...
676 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), 0, backendNetworkSideCB
))
677 return (CUPS_BACKEND_OK
);
678 else if ((bytes
= read(0, buffer
, sizeof(buffer
))) <= 0)
679 return (CUPS_BACKEND_OK
);
683 * Try connecting to the remote server...
686 delay
= _cupsNextDelay(0, &prev_delay
);
690 fprintf(stderr
, "DEBUG: Connecting to %s:%d\n", hostname
, port
);
691 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
693 if (httpReconnect(http
))
695 int error
= errno
; /* Connection error */
697 if (http
->status
== HTTP_PKI_ERROR
)
698 update_reasons(NULL
, "+cups-certificate-error");
703 if (getenv("CLASS") != NULL
)
706 * If the CLASS environment variable is set, the job was submitted
707 * to a class and not to a specific queue. In this case, we want
708 * to abort immediately so that the job can be requeued on the next
709 * available printer in the class.
712 _cupsLangPrintFilter(stderr
, "INFO",
713 _("Unable to contact printer, queuing on next "
714 "printer in class."));
717 * Sleep 5 seconds to keep the job from requeuing too rapidly...
722 update_reasons(NULL
, "-connecting-to-device");
724 return (CUPS_BACKEND_FAILED
);
727 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
729 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
730 errno
== EHOSTUNREACH
)
732 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
734 _cupsLangPrintFilter(stderr
, "ERROR",
735 _("The printer is not responding."));
736 update_reasons(NULL
, "-connecting-to-device");
737 return (CUPS_BACKEND_FAILED
);
743 _cupsLangPrintFilter(stderr
, "WARNING",
744 _("The printer may not exist or "
745 "is unavailable at this time."));
749 _cupsLangPrintFilter(stderr
, "WARNING",
750 _("The printer is unreachable at this "
756 _cupsLangPrintFilter(stderr
, "WARNING",
757 _("The printer is in use."));
763 delay
= _cupsNextDelay(delay
, &prev_delay
);
767 _cupsLangPrintFilter(stderr
, "ERROR",
768 _("The printer is not responding."));
776 update_reasons(NULL
, "-cups-certificate-error");
778 while (http
->fd
< 0);
781 return (CUPS_BACKEND_OK
);
783 return (CUPS_BACKEND_FAILED
);
785 update_reasons(NULL
, "-connecting-to-device");
786 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
788 fprintf(stderr
, "DEBUG: Connected to %s:%d...\n",
789 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
790 _httpAddrPort(http
->hostaddr
));
793 * Build a URI for the printer and fill the standard IPP attributes for
794 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
795 * might contain username:password information...
798 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), scheme
, NULL
, hostname
,
802 * First validate the destination and see if the device supports multiple
809 media_col_sup
= NULL
;
811 operations_sup
= NULL
;
812 doc_handling_sup
= NULL
;
817 * Check for side-channel requests...
820 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
823 * Build the IPP request...
826 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
827 request
->request
.op
.version
[0] = version
/ 10;
828 request
->request
.op
.version
[1] = version
% 10;
830 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
833 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
834 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
841 fputs("DEBUG: Getting supported attributes...\n", stderr
);
843 if (http
->version
< HTTP_1_1
)
845 fprintf(stderr
, "DEBUG: Printer responded with HTTP version %d.%d.\n",
846 http
->version
/ 100, http
->version
% 100);
847 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
848 "cups-ipp-wrong-http-version");
851 supported
= cupsDoRequest(http
, request
, resource
);
852 ipp_status
= cupsLastError();
854 fprintf(stderr
, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
855 ippErrorString(ipp_status
), cupsLastErrorString());
857 if (ipp_status
<= IPP_OK_CONFLICT
)
861 fprintf(stderr
, "DEBUG: Get-Printer-Attributes returned %s.\n",
862 ippErrorString(ipp_status
));
864 if (ipp_status
== IPP_PRINTER_BUSY
||
865 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
867 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
869 _cupsLangPrintFilter(stderr
, "ERROR",
870 _("The printer is not responding."));
871 return (CUPS_BACKEND_FAILED
);
874 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
876 report_printer_state(supported
);
880 delay
= _cupsNextDelay(delay
, &prev_delay
);
882 else if ((ipp_status
== IPP_BAD_REQUEST
||
883 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
> 10)
886 * Switch to IPP/1.1 or IPP/1.0...
891 _cupsLangPrintFilter(stderr
, "INFO",
892 _("The printer does not support IPP/%d.%d, trying "
893 "IPP/%s."), version
/ 10, version
% 10, "1.1");
898 _cupsLangPrintFilter(stderr
, "INFO",
899 _("The printer does not support IPP/%d.%d, trying "
900 "IPP/%s."), version
/ 10, version
% 10, "1.0");
906 else if (ipp_status
== IPP_NOT_FOUND
)
908 _cupsLangPrintFilter(stderr
, "ERROR",
909 _("The printer URI is incorrect or no longer "
912 ippDelete(supported
);
914 return (CUPS_BACKEND_STOP
);
916 else if (ipp_status
== IPP_FORBIDDEN
||
917 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
919 const char *www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
920 /* WWW-Authenticate field value */
922 if (!strncmp(www_auth
, "Negotiate", 9))
923 auth_info_required
= "negotiate";
924 else if (www_auth
[0])
925 auth_info_required
= "username,password";
927 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
928 return (CUPS_BACKEND_AUTH_REQUIRED
);
930 else if (ipp_status
!= IPP_NOT_AUTHORIZED
)
932 _cupsLangPrintFilter(stderr
, "ERROR",
933 _("Unable to get printer status."));
937 ippDelete(supported
);
942 if (!getenv("CLASS"))
945 * Check printer-is-accepting-jobs = false and printer-state-reasons for the
946 * "spool-area-full" keyword...
951 if ((printer_accepting
= ippFindAttribute(supported
,
952 "printer-is-accepting-jobs",
953 IPP_TAG_BOOLEAN
)) != NULL
&&
954 !printer_accepting
->values
[0].boolean
)
956 else if (!printer_accepting
)
957 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
958 "cups-ipp-missing-printer-is-accepting-jobs");
960 if ((printer_state
= ippFindAttribute(supported
,
961 "printer-state-reasons",
962 IPP_TAG_KEYWORD
)) != NULL
&& !busy
)
964 for (i
= 0; i
< printer_state
->num_values
; i
++)
965 if (!strcmp(printer_state
->values
[0].string
.text
,
966 "spool-area-full") ||
967 !strncmp(printer_state
->values
[0].string
.text
, "spool-area-full-",
975 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
976 "cups-ipp-missing-printer-state-reasons");
980 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
982 report_printer_state(supported
);
986 delay
= _cupsNextDelay(delay
, &prev_delay
);
988 ippDelete(supported
);
995 * Check for supported attributes...
998 if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
999 IPP_TAG_RANGE
)) != NULL
)
1002 * Has the "copies-supported" attribute - does it have an upper
1006 fprintf(stderr
, "DEBUG: copies-supported=%d-%d\n",
1007 copies_sup
->values
[0].range
.lower
,
1008 copies_sup
->values
[0].range
.upper
);
1010 if (copies_sup
->values
[0].range
.upper
<= 1)
1011 copies_sup
= NULL
; /* No */
1014 cups_version
= ippFindAttribute(supported
, "cups-version", IPP_TAG_TEXT
);
1016 if ((format_sup
= ippFindAttribute(supported
, "document-format-supported",
1017 IPP_TAG_MIMETYPE
)) != NULL
)
1019 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
1020 format_sup
->num_values
);
1021 for (i
= 0; i
< format_sup
->num_values
; i
++)
1022 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
1023 format_sup
->values
[i
].string
.text
);
1026 if ((media_col_sup
= ippFindAttribute(supported
, "media-col-supported",
1027 IPP_TAG_KEYWORD
)) != NULL
)
1029 fprintf(stderr
, "DEBUG: media-col-supported (%d values)\n",
1030 media_col_sup
->num_values
);
1031 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
1032 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
1033 media_col_sup
->values
[i
].string
.text
);
1036 if ((operations_sup
= ippFindAttribute(supported
, "operations-supported",
1037 IPP_TAG_ENUM
)) != NULL
)
1039 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1040 if (operations_sup
->values
[i
].integer
== IPP_PRINT_JOB
)
1043 if (i
>= operations_sup
->num_values
)
1044 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1045 "cups-ipp-missing-print-job");
1047 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1048 if (operations_sup
->values
[i
].integer
== IPP_CANCEL_JOB
)
1051 if (i
>= operations_sup
->num_values
)
1052 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1053 "cups-ipp-missing-cancel-job");
1055 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1056 if (operations_sup
->values
[i
].integer
== IPP_GET_JOB_ATTRIBUTES
)
1059 if (i
>= operations_sup
->num_values
)
1060 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1061 "cups-ipp-missing-get-job-attributes");
1063 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1064 if (operations_sup
->values
[i
].integer
== IPP_GET_PRINTER_ATTRIBUTES
)
1067 if (i
>= operations_sup
->num_values
)
1068 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1069 "cups-ipp-missing-get-printer-attributes");
1071 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1073 if (operations_sup
->values
[i
].integer
== IPP_VALIDATE_JOB
)
1075 else if (operations_sup
->values
[i
].integer
== IPP_CREATE_JOB
)
1077 else if (operations_sup
->values
[i
].integer
== IPP_SEND_DOCUMENT
)
1079 else if (operations_sup
->values
[i
].integer
== IPP_GET_JOB_ATTRIBUTES
)
1083 if (create_job
&& !send_document
)
1085 fputs("DEBUG: Printer supports Create-Job but not Send-Document.\n",
1089 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1090 "cups-ipp-missing-send-document");
1094 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1095 "cups-ipp-missing-validate-job");
1098 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1099 "cups-ipp-missing-operations-supported");
1101 doc_handling_sup
= ippFindAttribute(supported
,
1102 "multiple-document-handling-supported",
1105 report_printer_state(supported
);
1107 while (!job_canceled
&& ipp_status
> IPP_OK_CONFLICT
);
1110 return (CUPS_BACKEND_OK
);
1113 * See if the printer is accepting jobs and is not stopped; if either
1114 * condition is true and we are printing to a class, requeue the job...
1117 if (getenv("CLASS") != NULL
)
1119 printer_state
= ippFindAttribute(supported
, "printer-state",
1121 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
1124 if (printer_state
== NULL
||
1125 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
1127 printer_accepting
== NULL
||
1128 !printer_accepting
->values
[0].boolean
)
1131 * If the CLASS environment variable is set, the job was submitted
1132 * to a class and not to a specific queue. In this case, we want
1133 * to abort immediately so that the job can be requeued on the next
1134 * available printer in the class.
1137 _cupsLangPrintFilter(stderr
, "INFO",
1138 _("Unable to contact printer, queuing on next "
1139 "printer in class."));
1141 ippDelete(supported
);
1145 * Sleep 5 seconds to keep the job from requeuing too rapidly...
1150 return (CUPS_BACKEND_FAILED
);
1155 * See if the printer supports multiple copies...
1158 copies
= atoi(argv
[4]);
1160 if (copies_sup
|| argc
< 7)
1161 copies_remaining
= 1;
1163 copies_remaining
= copies
;
1166 * Prepare remaining printing options...
1174 num_options
= cupsParseOptions(argv
[5], 0, &options
);
1176 if (!cups_version
&& media_col_sup
)
1179 * Load the PPD file and generate PWG attribute mapping information...
1182 ppd
= ppdOpenFile(getenv("PPD"));
1183 pc
= _ppdCacheCreateWithPPD(ppd
);
1191 document_format
= NULL
;
1193 if (format_sup
!= NULL
)
1195 for (i
= 0; i
< format_sup
->num_values
; i
++)
1196 if (!_cups_strcasecmp(final_content_type
,
1197 format_sup
->values
[i
].string
.text
))
1199 document_format
= final_content_type
;
1203 if (!document_format
)
1205 for (i
= 0; i
< format_sup
->num_values
; i
++)
1206 if (!_cups_strcasecmp("application/octet-stream",
1207 format_sup
->values
[i
].string
.text
))
1209 document_format
= "application/octet-stream";
1215 fprintf(stderr
, "DEBUG: final_content_type=\"%s\", document_format=\"%s\"\n",
1216 final_content_type
, document_format
? document_format
: "(null)");
1219 * If the printer does not support HTTP/1.1 (which IPP requires), copy stdin
1220 * to a temporary file so that we can do a HTTP/1.0 submission...
1222 * (I hate compatibility hacks!)
1225 if (http
->version
< HTTP_1_1
&& num_files
== 0)
1227 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
1229 perror("DEBUG: Unable to create temporary file");
1230 return (CUPS_BACKEND_FAILED
);
1233 _cupsLangPrintFilter(stderr
, "INFO", _("Copying print data."));
1235 if ((compatsize
= write(fd
, buffer
, bytes
)) < 0)
1237 perror("DEBUG: Unable to write temporary file");
1238 return (CUPS_BACKEND_FAILED
);
1241 if ((bytes
= backendRunLoop(-1, fd
, snmp_fd
, &(addrlist
->addr
), 0, 0,
1242 backendNetworkSideCB
)) < 0)
1243 return (CUPS_BACKEND_FAILED
);
1245 compatsize
+= bytes
;
1249 compatfile
= tmpfilename
;
1250 files
= &compatfile
;
1253 else if (http
->version
< HTTP_1_1
&& num_files
== 1)
1255 struct stat fileinfo
; /* File information */
1257 if (!stat(files
[0], &fileinfo
))
1258 compatsize
= fileinfo
.st_size
;
1262 * Start monitoring the printer in the background...
1266 monitor
.hostname
= hostname
;
1267 monitor
.user
= argv
[2];
1268 monitor
.resource
= resource
;
1269 monitor
.port
= port
;
1270 monitor
.version
= version
;
1272 monitor
.get_job_attrs
= get_job_attrs
;
1273 monitor
.encryption
= cupsEncryption();
1274 monitor
.job_state
= IPP_JOB_PENDING
;
1275 monitor
.printer_state
= IPP_PRINTER_IDLE
;
1279 monitor
.job_name
= argv
[3];
1283 snprintf(print_job_name
, sizeof(print_job_name
), "%s - %s", argv
[1],
1285 monitor
.job_name
= print_job_name
;
1288 _cupsThreadCreate((_cups_thread_func_t
)monitor_printer
, &monitor
);
1291 * Validate access to the printer...
1294 while (!job_canceled
&& validate_job
)
1296 request
= new_request(IPP_VALIDATE_JOB
, version
, uri
, argv
[2],
1297 monitor
.job_name
, num_options
, options
, compression
,
1298 copies_sup
? copies
: 1, document_format
, pc
,
1299 media_col_sup
, doc_handling_sup
);
1301 ippDelete(cupsDoRequest(http
, request
, resource
));
1303 ipp_status
= cupsLastError();
1305 fprintf(stderr
, "DEBUG: Validate-Job: %s (%s)\n",
1306 ippErrorString(ipp_status
), cupsLastErrorString());
1311 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
|| ipp_status
== IPP_PRINTER_BUSY
)
1313 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
1316 else if (ipp_status
== IPP_DOCUMENT_FORMAT
)
1318 else if (ipp_status
== IPP_FORBIDDEN
||
1319 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1321 const char *www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
1322 /* WWW-Authenticate field value */
1324 if (!strncmp(www_auth
, "Negotiate", 9))
1325 auth_info_required
= "negotiate";
1326 else if (www_auth
[0])
1327 auth_info_required
= "username,password";
1331 else if (ipp_status
== IPP_OPERATION_NOT_SUPPORTED
)
1334 * This is all too common...
1337 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1338 "cups-ipp-missing-validate-job");
1341 else if (ipp_status
< IPP_REDIRECTION_OTHER_SITE
||
1342 ipp_status
== IPP_BAD_REQUEST
)
1347 * Then issue the print-job request...
1352 while (!job_canceled
&& copies_remaining
> 0)
1355 * Check for side-channel requests...
1358 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1361 * Build the IPP job creation request...
1367 request
= new_request((num_files
> 1 || create_job
) ? IPP_CREATE_JOB
:
1369 version
, uri
, argv
[2], monitor
.job_name
, num_options
,
1370 options
, compression
, copies_sup
? copies
: 1,
1371 document_format
, pc
, media_col_sup
, doc_handling_sup
);
1377 if (num_files
> 1 || create_job
)
1378 response
= cupsDoRequest(http
, request
, resource
);
1381 size_t length
= 0; /* Length of request */
1385 fputs("DEBUG: Sending file using HTTP/1.0 Content-Length...\n", stderr
);
1386 length
= ippLength(request
) + (size_t)compatsize
;
1389 fputs("DEBUG: Sending file using HTTP/1.1 chunking...\n", stderr
);
1391 http_status
= cupsSendRequest(http
, request
, resource
, length
);
1392 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
)
1396 if ((fd
= open(files
[0], O_RDONLY
)) < 0)
1398 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1399 return (CUPS_BACKEND_FAILED
);
1405 http_status
= cupsWriteRequestData(http
, buffer
, bytes
);
1408 while (http_status
== HTTP_CONTINUE
&&
1409 (!job_canceled
|| compatsize
> 0))
1412 * Check for side-channel requests and more print data...
1417 FD_SET(snmp_fd
, &input
);
1419 while (select(fd
> snmp_fd
? fd
+ 1 : snmp_fd
+ 1, &input
, NULL
, NULL
,
1420 NULL
) <= 0 && !job_canceled
);
1422 if (FD_ISSET(snmp_fd
, &input
))
1423 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1425 if (FD_ISSET(fd
, &input
))
1427 if ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1429 fprintf(stderr
, "DEBUG: Read %d bytes...\n", (int)bytes
);
1431 if (cupsWriteRequestData(http
, buffer
, bytes
) != HTTP_CONTINUE
)
1434 else if (bytes
== 0 || (errno
!= EINTR
&& errno
!= EAGAIN
))
1443 response
= cupsGetResponse(http
, resource
);
1447 ipp_status
= cupsLastError();
1449 fprintf(stderr
, "DEBUG: %s: %s (%s)\n",
1450 (num_files
> 1 || create_job
) ? "Create-Job" : "Print-Job",
1451 ippErrorString(ipp_status
), cupsLastErrorString());
1453 if (ipp_status
> IPP_OK_CONFLICT
)
1460 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1461 ipp_status
== IPP_NOT_POSSIBLE
||
1462 ipp_status
== IPP_PRINTER_BUSY
)
1464 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
1470 * We can't re-submit when we have no files to print, so exit
1471 * immediately with the right status code...
1477 else if (ipp_status
== IPP_ERROR_JOB_CANCELED
)
1479 else if (ipp_status
== IPP_NOT_AUTHORIZED
)
1484 * Update auth-info-required as needed...
1487 _cupsLangPrintFilter(stderr
, "ERROR",
1488 _("Print file was not accepted."));
1490 if (ipp_status
== IPP_FORBIDDEN
||
1491 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1493 const char *www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
1494 /* WWW-Authenticate field value */
1496 if (!strncmp(www_auth
, "Negotiate", 9))
1497 auth_info_required
= "negotiate";
1498 else if (www_auth
[0])
1499 auth_info_required
= "username,password";
1501 else if (ipp_status
== IPP_REQUEST_VALUE
)
1504 * Print file is too large, abort this job...
1515 * We can't re-submit when we have no files to print, so exit
1516 * immediately with the right status code...
1523 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
1524 IPP_TAG_INTEGER
)) == NULL
)
1526 _cupsLangPrintFilter(stderr
, "INFO",
1527 _("Print file accepted - job ID unknown."));
1528 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1529 "cups-ipp-missing-job-id");
1535 monitor
.job_id
= job_id
= job_id_attr
->values
[0].integer
;
1536 _cupsLangPrintFilter(stderr
, "INFO",
1537 _("Print file accepted - job ID %d."), job_id
);
1540 fprintf(stderr
, "DEBUG: job-id=%d\n", job_id
);
1541 ippDelete(response
);
1546 if (job_id
&& (num_files
> 1 || create_job
))
1548 for (i
= 0; num_files
== 0 || i
< num_files
; i
++)
1551 * Check for side-channel requests...
1554 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1557 * Send the next file in the job...
1560 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
1561 request
->request
.op
.version
[0] = version
/ 10;
1562 request
->request
.op
.version
[1] = version
% 10;
1564 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1567 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1571 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1572 "requesting-user-name", NULL
, argv
[2]);
1574 if ((i
+ 1) >= num_files
)
1575 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
1577 if (document_format
)
1578 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1579 "document-format", NULL
, document_format
);
1581 fprintf(stderr
, "DEBUG: Sending file %d using chunking...\n", i
+ 1);
1582 http_status
= cupsSendRequest(http
, request
, resource
, 0);
1583 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
)
1588 http_status
= cupsWriteRequestData(http
, buffer
, bytes
);
1592 if ((fd
= open(files
[i
], O_RDONLY
)) < 0)
1594 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1595 return (CUPS_BACKEND_FAILED
);
1604 while (!job_canceled
&& http_status
== HTTP_CONTINUE
&&
1605 (bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1607 if ((http_status
= cupsWriteRequestData(http
, buffer
, bytes
))
1613 * Check for side-channel requests...
1616 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1624 ippDelete(cupsGetResponse(http
, resource
));
1627 fprintf(stderr
, "DEBUG: Send-Document: %s (%s)\n",
1628 ippErrorString(cupsLastError()), cupsLastErrorString());
1630 if (cupsLastError() > IPP_OK_CONFLICT
)
1632 ipp_status
= cupsLastError();
1634 _cupsLangPrintFilter(stderr
, "ERROR",
1635 _("Unable to add document to print job."));
1642 if (num_files
== 0 || fd
< 0)
1648 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
1650 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
1651 copies_remaining
--;
1653 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1654 ipp_status
== IPP_NOT_POSSIBLE
||
1655 ipp_status
== IPP_PRINTER_BUSY
)
1657 else if (ipp_status
== IPP_REQUEST_VALUE
)
1660 * Print file is too large, abort this job...
1666 copies_remaining
--;
1669 * Wait for the job to complete...
1672 if (!job_id
|| !waitjob
|| !get_job_attrs
)
1675 _cupsLangPrintFilter(stderr
, "INFO", _("Waiting for job to complete."));
1677 for (delay
= _cupsNextDelay(0, &prev_delay
); !job_canceled
;)
1680 * Check for side-channel requests...
1683 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1686 * Build an IPP_GET_JOB_ATTRIBUTES request...
1689 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1690 request
->request
.op
.version
[0] = version
/ 10;
1691 request
->request
.op
.version
[1] = version
% 10;
1693 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1696 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1700 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1701 "requesting-user-name", NULL
, argv
[2]);
1703 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1704 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
1711 httpReconnect(http
);
1712 response
= cupsDoRequest(http
, request
, resource
);
1713 ipp_status
= cupsLastError();
1715 if (ipp_status
== IPP_NOT_FOUND
|| ipp_status
== IPP_NOT_POSSIBLE
)
1718 * Job has gone away and/or the server has no job history...
1721 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1722 "cups-ipp-missing-job-history");
1723 ippDelete(response
);
1725 ipp_status
= IPP_OK
;
1729 fprintf(stderr
, "DEBUG: Get-Job-Attributes: %s (%s)\n",
1730 ippErrorString(ipp_status
), cupsLastErrorString());
1732 if (ipp_status
<= IPP_OK_CONFLICT
)
1736 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1737 ipp_status
!= IPP_PRINTER_BUSY
)
1739 ippDelete(response
);
1740 ipp_status
= IPP_OK
;
1747 if ((job_state
= ippFindAttribute(response
, "job-state",
1748 IPP_TAG_ENUM
)) != NULL
)
1751 * Reflect the remote job state in the local queue...
1755 job_state
->values
[0].integer
>= IPP_JOB_PENDING
&&
1756 job_state
->values
[0].integer
<= IPP_JOB_COMPLETED
)
1757 update_reasons(NULL
,
1758 remote_job_states
[job_state
->values
[0].integer
-
1761 if ((job_sheets
= ippFindAttribute(response
,
1762 "job-media-sheets-completed",
1763 IPP_TAG_INTEGER
)) == NULL
)
1764 job_sheets
= ippFindAttribute(response
,
1765 "job-impressions-completed",
1769 fprintf(stderr
, "PAGE: total %d\n",
1770 job_sheets
->values
[0].integer
);
1773 * Stop polling if the job is finished or pending-held...
1776 if (job_state
->values
[0].integer
> IPP_JOB_STOPPED
)
1778 ippDelete(response
);
1782 else if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1783 ipp_status
!= IPP_NOT_POSSIBLE
&&
1784 ipp_status
!= IPP_PRINTER_BUSY
)
1787 * If the printer does not return a job-state attribute, it does not
1788 * conform to the IPP specification - break out immediately and fail
1792 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1793 "cups-ipp-missing-job-state");
1794 ipp_status
= IPP_INTERNAL_ERROR
;
1799 ippDelete(response
);
1802 * Wait before polling again...
1807 delay
= _cupsNextDelay(delay
, &prev_delay
);
1812 * Cancel the job as needed...
1815 if (job_canceled
> 0 && job_id
> 0)
1816 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1819 * Check the printer state and report it if necessary...
1822 check_printer_state(http
, uri
, resource
, argv
[2], version
);
1825 * Collect the final page count as needed...
1828 if (have_supplies
&&
1829 !backendSNMPSupplies(snmp_fd
, http
->hostaddr
, &page_count
, NULL
) &&
1830 page_count
> start_count
)
1831 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
1835 * See if we used Kerberos at all...
1839 auth_info_required
= "negotiate";
1840 #endif /* HAVE_GSSAPI */
1848 cupsFreeOptions(num_options
, options
);
1849 _ppdCacheDestroy(pc
);
1853 ippDelete(supported
);
1856 * Remove the temporary file(s) if necessary...
1860 unlink(tmpfilename
);
1865 for (i
= 0; i
< num_files
; i
++)
1868 #endif /* HAVE_LIBZ */
1871 * Return the queue status...
1874 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
1875 ipp_status
== IPP_AUTHENTICATION_CANCELED
||
1876 ipp_status
<= IPP_OK_CONFLICT
)
1877 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
1879 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
1880 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1881 return (CUPS_BACKEND_AUTH_REQUIRED
);
1882 else if (ipp_status
== IPP_INTERNAL_ERROR
)
1883 return (CUPS_BACKEND_STOP
);
1884 else if (ipp_status
== IPP_CONFLICT
)
1885 return (CUPS_BACKEND_FAILED
);
1886 else if (ipp_status
== IPP_REQUEST_VALUE
||
1887 ipp_status
== IPP_DOCUMENT_FORMAT
|| job_canceled
< 0)
1889 if (ipp_status
== IPP_REQUEST_VALUE
)
1890 _cupsLangPrintFilter(stderr
, "ERROR", _("Print job too large."));
1891 else if (ipp_status
== IPP_DOCUMENT_FORMAT
)
1892 _cupsLangPrintFilter(stderr
, "ERROR",
1893 _("Printer cannot print supplied content."));
1895 _cupsLangPrintFilter(stderr
, "ERROR", _("Print job canceled at printer."));
1897 return (CUPS_BACKEND_CANCEL
);
1899 else if (ipp_status
> IPP_OK_CONFLICT
&& ipp_status
!= IPP_ERROR_JOB_CANCELED
)
1900 return (CUPS_BACKEND_RETRY_CURRENT
);
1902 return (CUPS_BACKEND_OK
);
1907 * 'cancel_job()' - Cancel a print job.
1911 cancel_job(http_t
*http
, /* I - HTTP connection */
1912 const char *uri
, /* I - printer-uri */
1913 int id
, /* I - job-id */
1914 const char *resource
, /* I - Resource path */
1915 const char *user
, /* I - requesting-user-name */
1916 int version
) /* I - IPP version */
1918 ipp_t
*request
; /* Cancel-Job request */
1921 _cupsLangPrintFilter(stderr
, "INFO", _("Canceling print job."));
1923 request
= ippNewRequest(IPP_CANCEL_JOB
);
1924 request
->request
.op
.version
[0] = version
/ 10;
1925 request
->request
.op
.version
[1] = version
% 10;
1927 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1929 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1931 if (user
&& user
[0])
1932 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1933 "requesting-user-name", NULL
, user
);
1939 ippDelete(cupsDoRequest(http
, request
, resource
));
1941 if (cupsLastError() > IPP_OK_CONFLICT
)
1942 _cupsLangPrintFilter(stderr
, "ERROR", _("Unable to cancel print job."));
1947 * 'check_printer_state()' - Check the printer state.
1950 static ipp_pstate_t
/* O - Current printer-state */
1951 check_printer_state(
1952 http_t
*http
, /* I - HTTP connection */
1953 const char *uri
, /* I - Printer URI */
1954 const char *resource
, /* I - Resource path */
1955 const char *user
, /* I - Username, if any */
1956 int version
) /* I - IPP version */
1958 ipp_t
*request
, /* IPP request */
1959 *response
; /* IPP response */
1960 ipp_attribute_t
*attr
; /* Attribute in response */
1961 ipp_pstate_t printer_state
= IPP_PRINTER_STOPPED
;
1962 /* Current printer-state */
1966 * Send a Get-Printer-Attributes request and log the results...
1969 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1970 request
->request
.op
.version
[0] = version
/ 10;
1971 request
->request
.op
.version
[1] = version
% 10;
1973 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1976 if (user
&& user
[0])
1977 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1978 "requesting-user-name", NULL
, user
);
1980 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1981 "requested-attributes",
1982 (int)(sizeof(pattrs
) / sizeof(pattrs
[0])), NULL
, pattrs
);
1984 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
1986 report_printer_state(response
);
1988 if ((attr
= ippFindAttribute(response
, "printer-state",
1989 IPP_TAG_ENUM
)) != NULL
)
1990 printer_state
= (ipp_pstate_t
)attr
->values
[0].integer
;
1992 ippDelete(response
);
1995 fprintf(stderr
, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
1996 ippErrorString(cupsLastError()), cupsLastErrorString());
1998 if (cupsLastError() <= IPP_OK_CONFLICT
)
2002 * Return the printer-state value...
2005 return (printer_state
);
2011 * 'compress_files()' - Compress print files.
2015 compress_files(int num_files
, /* I - Number of files */
2016 char **files
) /* I - Files */
2018 int i
, /* Looping var */
2019 fd
; /* Temporary file descriptor */
2020 ssize_t bytes
; /* Bytes read/written */
2021 size_t total
; /* Total bytes read */
2022 cups_file_t
*in
, /* Input file */
2023 *out
; /* Output file */
2024 struct stat outinfo
; /* Output file information */
2025 char filename
[1024], /* Temporary filename */
2026 buffer
[32768]; /* Copy buffer */
2029 fprintf(stderr
, "DEBUG: Compressing %d job files...\n", num_files
);
2030 for (i
= 0; i
< num_files
; i
++)
2032 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
2034 _cupsLangPrintError("ERROR", _("Unable to create compressed print file"));
2035 exit(CUPS_BACKEND_FAILED
);
2038 if ((out
= cupsFileOpenFd(fd
, "w9")) == NULL
)
2040 _cupsLangPrintError("ERROR", _("Unable to open compressed print file"));
2041 exit(CUPS_BACKEND_FAILED
);
2044 if ((in
= cupsFileOpen(files
[i
], "r")) == NULL
)
2046 _cupsLangPrintError("ERROR", _("Unable to open print file"));
2048 exit(CUPS_BACKEND_FAILED
);
2052 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
2053 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
2055 _cupsLangPrintError("ERROR",
2056 _("Unable to generate compressed print file"));
2059 exit(CUPS_BACKEND_FAILED
);
2067 files
[i
] = strdup(filename
);
2069 if (!stat(filename
, &outinfo
))
2071 "DEBUG: File %d compressed to %.1f%% of original size, "
2072 CUPS_LLFMT
" bytes...\n",
2073 i
+ 1, 100.0 * outinfo
.st_size
/ total
,
2074 CUPS_LLCAST outinfo
.st_size
);
2077 #endif /* HAVE_LIBZ */
2081 * 'monitor_printer()' - Monitor the printer state.
2084 static void * /* O - Thread exit code */
2086 _cups_monitor_t
*monitor
) /* I - Monitoring data */
2088 http_t
*http
; /* Connection to printer */
2089 ipp_t
*request
, /* IPP request */
2090 *response
; /* IPP response */
2091 ipp_attribute_t
*attr
; /* Attribute in response */
2092 int delay
, /* Current delay */
2093 prev_delay
; /* Previous delay */
2094 ipp_op_t job_op
; /* Operation to use */
2095 int job_id
; /* Job ID */
2096 const char *job_name
; /* Job name */
2097 ipp_jstate_t job_state
; /* Job state */
2098 const char *job_user
; /* Job originating user name */
2102 * Make a copy of the printer connection...
2105 http
= _httpCreate(monitor
->hostname
, monitor
->port
, NULL
, monitor
->encryption
,
2107 httpSetTimeout(http
, 30.0, timeout_cb
, NULL
);
2109 cupsSetUser(username
);
2110 cupsSetPasswordCB(password_cb
);
2113 * Loop until the job is canceled, aborted, or completed.
2116 delay
= _cupsNextDelay(0, &prev_delay
);
2118 while (monitor
->job_state
< IPP_JOB_CANCELED
&& !job_canceled
)
2121 * Reconnect to the printer...
2124 if (!httpReconnect(http
))
2127 * Connected, so check on the printer state...
2130 monitor
->printer_state
= check_printer_state(http
, monitor
->uri
,
2136 * Check the status of the job itself...
2139 job_op
= (monitor
->job_id
> 0 && monitor
->get_job_attrs
) ?
2140 IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
;
2141 request
= ippNewRequest(job_op
);
2142 request
->request
.op
.version
[0] = monitor
->version
/ 10;
2143 request
->request
.op
.version
[1] = monitor
->version
% 10;
2145 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2146 NULL
, monitor
->uri
);
2147 if (job_op
== IPP_GET_JOB_ATTRIBUTES
)
2148 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
2151 if (monitor
->user
&& monitor
->user
[0])
2152 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2153 "requesting-user-name", NULL
, monitor
->user
);
2155 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2156 "requested-attributes",
2157 (int)(sizeof(jattrs
) / sizeof(jattrs
[0])), NULL
, jattrs
);
2163 response
= cupsDoRequest(http
, request
, monitor
->resource
);
2165 fprintf(stderr
, "DEBUG: (monitor) %s: %s (%s)\n", ippOpString(job_op
),
2166 ippErrorString(cupsLastError()), cupsLastErrorString());
2168 if (cupsLastError() <= IPP_OK_CONFLICT
)
2171 if (job_op
== IPP_GET_JOB_ATTRIBUTES
)
2173 if ((attr
= ippFindAttribute(response
, "job-state",
2174 IPP_TAG_ENUM
)) != NULL
)
2175 monitor
->job_state
= (ipp_jstate_t
)attr
->values
[0].integer
;
2177 monitor
->job_state
= IPP_JOB_COMPLETED
;
2181 for (attr
= response
->attrs
; attr
; attr
= attr
->next
)
2185 job_state
= IPP_JOB_PENDING
;
2188 while (attr
&& attr
->group_tag
!= IPP_TAG_JOB
)
2194 while (attr
&& attr
->group_tag
== IPP_TAG_JOB
)
2196 if (!strcmp(attr
->name
, "job-id") &&
2197 attr
->value_tag
== IPP_TAG_INTEGER
)
2198 job_id
= attr
->values
[0].integer
;
2199 else if (!strcmp(attr
->name
, "job-name") &&
2200 (attr
->value_tag
== IPP_TAG_NAME
||
2201 attr
->value_tag
== IPP_TAG_NAMELANG
))
2202 job_name
= attr
->values
[0].string
.text
;
2203 else if (!strcmp(attr
->name
, "job-state") &&
2204 attr
->value_tag
== IPP_TAG_ENUM
)
2205 job_state
= attr
->values
[0].integer
;
2206 else if (!strcmp(attr
->name
, "job-originating-user-name") &&
2207 (attr
->value_tag
== IPP_TAG_NAME
||
2208 attr
->value_tag
== IPP_TAG_NAMELANG
))
2209 job_user
= attr
->values
[0].string
.text
;
2214 if (job_id
> 0 && job_name
&& !strcmp(job_name
, monitor
->job_name
) &&
2215 job_user
&& monitor
->user
&& !strcmp(job_user
, monitor
->user
))
2217 monitor
->job_id
= job_id
;
2218 monitor
->job_state
= job_state
;
2227 ippDelete(response
);
2229 fprintf(stderr
, "DEBUG: (monitor) job-state=%s\n",
2230 ippEnumString("job-state", monitor
->job_state
));
2232 if (!job_canceled
&&
2233 (monitor
->job_state
== IPP_JOB_CANCELED
||
2234 monitor
->job_state
== IPP_JOB_ABORTED
))
2238 * Disconnect from the printer - we'll reconnect on the next poll...
2241 _httpDisconnect(http
);
2245 * Sleep for N seconds...
2250 delay
= _cupsNextDelay(delay
, &prev_delay
);
2254 * Cancel the job if necessary...
2257 if (job_canceled
> 0 && monitor
->job_id
> 0)
2258 if (!httpReconnect(http
))
2259 cancel_job(http
, monitor
->uri
, monitor
->job_id
, monitor
->resource
,
2260 monitor
->user
, monitor
->version
);
2263 * Cleanup and return...
2273 * 'new_request()' - Create a new print creation or validation request.
2276 static ipp_t
* /* O - Request data */
2278 ipp_op_t op
, /* I - IPP operation code */
2279 int version
, /* I - IPP version number */
2280 const char *uri
, /* I - printer-uri value */
2281 const char *user
, /* I - requesting-user-name value */
2282 const char *title
, /* I - job-name value */
2283 int num_options
, /* I - Number of options to send */
2284 cups_option_t
*options
, /* I - Options to send */
2285 const char *compression
, /* I - compression value or NULL */
2286 int copies
, /* I - copies value or 0 */
2287 const char *format
, /* I - document-format value or NULL */
2288 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2289 ipp_attribute_t
*media_col_sup
, /* I - media-col-supported values */
2290 ipp_attribute_t
*doc_handling_sup
) /* I - multiple-document-handling-supported values */
2292 int i
; /* Looping var */
2293 ipp_t
*request
; /* Request data */
2294 const char *keyword
; /* PWG keyword */
2295 _pwg_size_t
*size
; /* PWG media size */
2296 ipp_t
*media_col
, /* media-col value */
2297 *media_size
; /* media-size value */
2298 const char *media_source
, /* media-source value */
2299 *media_type
, /* media-type value */
2300 *collate_str
; /* multiple-document-handling value */
2304 * Create the IPP request...
2307 request
= ippNewRequest(op
);
2308 request
->request
.op
.version
[0] = version
/ 10;
2309 request
->request
.op
.version
[1] = version
% 10;
2311 fprintf(stderr
, "DEBUG: %s IPP/%d.%d\n",
2312 ippOpString(request
->request
.op
.operation_id
),
2313 request
->request
.op
.version
[0],
2314 request
->request
.op
.version
[1]);
2317 * Add standard attributes...
2320 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2322 fprintf(stderr
, "DEBUG: printer-uri=\"%s\"\n", uri
);
2326 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2327 "requesting-user-name", NULL
, user
);
2328 fprintf(stderr
, "DEBUG: requesting-user-name=\"%s\"\n", user
);
2331 if (title
&& *title
)
2333 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
2335 fprintf(stderr
, "DEBUG: job-name=\"%s\"\n", title
);
2338 if (format
&& op
!= IPP_CREATE_JOB
)
2340 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
2341 "document-format", NULL
, format
);
2342 fprintf(stderr
, "DEBUG: document-format=\"%s\"\n", format
);
2346 if (compression
&& op
!= IPP_CREATE_JOB
)
2348 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2349 "compression", NULL
, compression
);
2350 fprintf(stderr
, "DEBUG: compression=\"%s\"\n", compression
);
2352 #endif /* HAVE_LIBZ */
2355 * Handle options on the command-line...
2358 if (num_options
> 0)
2362 int num_finishings
= 0, /* Number of finishing values */
2363 finishings
[10]; /* Finishing enum values */
2366 * Send standard IPP attributes...
2369 if ((keyword
= cupsGetOption("PageSize", num_options
, options
)) == NULL
)
2370 keyword
= cupsGetOption("media", num_options
, options
);
2372 if ((size
= _ppdCacheGetSize(pc
, keyword
)) != NULL
)
2375 * Add a media-col value...
2378 media_size
= ippNew();
2379 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2380 "x-dimension", size
->width
);
2381 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2382 "y-dimension", size
->length
);
2384 media_col
= ippNew();
2385 ippAddCollection(media_col
, IPP_TAG_ZERO
, "media-size", media_size
);
2387 media_source
= _ppdCacheGetSource(pc
, cupsGetOption("InputSlot",
2390 media_type
= _ppdCacheGetType(pc
, cupsGetOption("MediaType",
2394 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
2396 if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2397 "media-left-margin"))
2398 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2399 "media-left-margin", size
->left
);
2400 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2401 "media-bottom-margin"))
2402 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2403 "media-bottom-margin", size
->bottom
);
2404 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2405 "media-right-margin"))
2406 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2407 "media-right-margin", size
->right
);
2408 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2409 "media-top-margin"))
2410 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2411 "media-top-margin", size
->top
);
2412 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2413 "media-source") && media_source
)
2414 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
2415 "media-source", NULL
, media_source
);
2416 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2417 "media-type") && media_type
)
2418 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
2419 "media-type", NULL
, media_type
);
2422 ippAddCollection(request
, IPP_TAG_JOB
, "media-col", media_col
);
2425 if ((keyword
= cupsGetOption("output-bin", num_options
,
2427 keyword
= _ppdCacheGetBin(pc
, cupsGetOption("OutputBin", num_options
,
2431 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-bin",
2434 if ((keyword
= cupsGetOption("output-mode", num_options
,
2436 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
2438 else if ((keyword
= cupsGetOption("ColorModel", num_options
,
2441 if (!_cups_strcasecmp(keyword
, "Gray"))
2442 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
2443 NULL
, "monochrome");
2445 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
2449 if ((keyword
= cupsGetOption("print-quality", num_options
,
2451 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2453 else if ((keyword
= cupsGetOption("cupsPrintQuality", num_options
,
2456 if (!_cups_strcasecmp(keyword
, "draft"))
2457 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2459 else if (!_cups_strcasecmp(keyword
, "normal"))
2460 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2461 IPP_QUALITY_NORMAL
);
2462 else if (!_cups_strcasecmp(keyword
, "high"))
2463 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2467 if ((keyword
= cupsGetOption("sides", num_options
, options
)) != NULL
)
2468 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2470 else if (pc
->sides_option
&&
2471 (keyword
= cupsGetOption(pc
->sides_option
, num_options
,
2474 if (!_cups_strcasecmp(keyword
, pc
->sides_1sided
))
2475 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2477 else if (!_cups_strcasecmp(keyword
, pc
->sides_2sided_long
))
2478 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2479 NULL
, "two-sided-long-edge");
2480 if (!_cups_strcasecmp(keyword
, pc
->sides_2sided_short
))
2481 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2482 NULL
, "two-sided-short-edge");
2485 if ((keyword
= cupsGetOption("multiple-document-handling",
2486 num_options
, options
)) != NULL
)
2488 if (strstr(keyword
, "uncollated"))
2493 else if ((keyword
= cupsGetOption("collate", num_options
,
2499 if (!_cups_strcasecmp(format
, "image/gif") ||
2500 !_cups_strcasecmp(format
, "image/jp2") ||
2501 !_cups_strcasecmp(format
, "image/jpeg") ||
2502 !_cups_strcasecmp(format
, "image/png") ||
2503 !_cups_strcasecmp(format
, "image/tiff") ||
2504 !_cups_strncasecmp(format
, "image/x-", 8))
2507 * Collation makes no sense for single page image formats...
2512 else if (!_cups_strncasecmp(format
, "image/", 6) ||
2513 !_cups_strcasecmp(format
, "application/vnd.cups-raster"))
2516 * Multi-page image formats will have copies applied by the upstream
2524 if (doc_handling_sup
)
2526 if (!_cups_strcasecmp(keyword
, "true"))
2527 collate_str
= "separate-documents-collated-copies";
2529 collate_str
= "separate-documents-uncollated-copies";
2531 for (i
= 0; i
< doc_handling_sup
->num_values
; i
++)
2532 if (!strcmp(doc_handling_sup
->values
[i
].string
.text
, collate_str
))
2534 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2535 "multiple-document-handling", NULL
, collate_str
);
2539 if (i
>= doc_handling_sup
->num_values
)
2544 * Map finishing options...
2547 num_finishings
= _ppdCacheGetFinishingValues(pc
, num_options
, options
,
2548 (int)(sizeof(finishings
) /
2549 sizeof(finishings
[0])),
2551 if (num_finishings
> 0)
2552 ippAddIntegers(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "finishings",
2553 num_finishings
, finishings
);
2556 * Map FaxOut options...
2559 if ((keyword
= cupsGetOption("phone", num_options
, options
)) != NULL
)
2561 ipp_t
*destination
; /* destination collection */
2562 char tel_uri
[1024]; /* tel: URI */
2564 destination
= ippNew();
2566 httpAssembleURI(HTTP_URI_CODING_ALL
, tel_uri
, sizeof(tel_uri
), "tel",
2567 NULL
, NULL
, 0, keyword
);
2568 ippAddString(destination
, IPP_TAG_JOB
, IPP_TAG_URI
, "destination-uri",
2571 if ((keyword
= cupsGetOption("faxPrefix", num_options
,
2572 options
)) != NULL
&& *keyword
)
2573 ippAddString(destination
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
2574 "pre-dial-string", NULL
, keyword
);
2576 ippAddCollection(request
, IPP_TAG_JOB
, "destination-uris", destination
);
2577 ippDelete(destination
);
2583 * When talking to another CUPS server, send all options...
2586 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_JOB
);
2589 if (copies
> 1 && (!pc
|| copies
<= pc
->max_copies
))
2590 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies", copies
);
2598 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
2601 static const char * /* O - Password */
2602 password_cb(const char *prompt
) /* I - Prompt (not used) */
2604 fprintf(stderr
, "DEBUG: password_cb(prompt=\"%s\"), password=%p, "
2605 "password_tries=%d\n", prompt
, password
, password_tries
);
2610 * Remember that we need to authenticate...
2613 auth_info_required
= "username,password";
2615 if (password
&& *password
&& password_tries
< 3)
2624 * Give up after 3 tries or if we don't have a password to begin with...
2633 * 'report_attr()' - Report an IPP attribute value.
2637 report_attr(ipp_attribute_t
*attr
) /* I - Attribute */
2639 int i
; /* Looping var */
2640 char value
[1024], /* Value string */
2641 *valptr
, /* Pointer into value string */
2642 *attrptr
; /* Pointer into attribute value */
2643 const char *cached
; /* Cached attribute */
2647 * Convert the attribute values into quoted strings...
2650 for (i
= 0, valptr
= value
;
2651 i
< attr
->num_values
&& valptr
< (value
+ sizeof(value
) - 10);
2657 switch (attr
->value_tag
)
2659 case IPP_TAG_INTEGER
:
2661 snprintf(valptr
, sizeof(value
) - (valptr
- value
), "%d",
2662 attr
->values
[i
].integer
);
2663 valptr
+= strlen(valptr
);
2668 case IPP_TAG_KEYWORD
:
2671 for (attrptr
= attr
->values
[i
].string
.text
;
2672 *attrptr
&& valptr
< (value
+ sizeof(value
) - 10);
2675 if (*attrptr
== '\\' || *attrptr
== '\"' || *attrptr
== '\'')
2682 *valptr
++ = *attrptr
;
2690 * Unsupported value type...
2699 _cupsMutexLock(&report_mutex
);
2701 if ((cached
= cupsGetOption(attr
->name
, num_attr_cache
,
2702 attr_cache
)) == NULL
|| strcmp(cached
, value
))
2705 * Tell the scheduler about the new values...
2708 num_attr_cache
= cupsAddOption(attr
->name
, value
, num_attr_cache
,
2710 fprintf(stderr
, "ATTR: %s=%s\n", attr
->name
, value
);
2713 _cupsMutexUnlock(&report_mutex
);
2718 * 'report_printer_state()' - Report the printer state.
2722 report_printer_state(ipp_t
*ipp
) /* I - IPP response */
2724 ipp_attribute_t
*pa
, /* printer-alert */
2725 *pam
, /* printer-alert-message */
2726 *psm
, /* printer-state-message */
2727 *reasons
, /* printer-state-reasons */
2728 *marker
; /* marker-* attributes */
2729 char value
[1024], /* State/message string */
2730 *valptr
; /* Pointer into string */
2731 static int ipp_supplies
= -1;
2732 /* Report supply levels? */
2736 * Report alerts and messages...
2739 if ((pa
= ippFindAttribute(ipp
, "printer-alert", IPP_TAG_TEXT
)) != NULL
)
2742 if ((pam
= ippFindAttribute(ipp
, "printer-alert-message",
2743 IPP_TAG_TEXT
)) != NULL
)
2746 if ((psm
= ippFindAttribute(ipp
, "printer-state-message",
2747 IPP_TAG_TEXT
)) != NULL
)
2749 char *ptr
; /* Pointer into message */
2752 strlcpy(value
, "INFO: ", sizeof(value
));
2753 for (ptr
= psm
->values
[0].string
.text
, valptr
= value
+ 6;
2754 *ptr
&& valptr
< (value
+ sizeof(value
) - 6);
2757 if (*ptr
< ' ' && *ptr
> 0 && *ptr
!= '\t')
2760 * Substitute "<XX>" for the control character; sprintf is safe because
2761 * we always leave 6 chars free at the end...
2764 sprintf(valptr
, "<%02X>", *ptr
);
2774 fputs(value
, stderr
);
2778 * Now report printer-state-reasons, filtering out some of the reasons we never
2782 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
2783 IPP_TAG_KEYWORD
)) == NULL
)
2786 update_reasons(reasons
, NULL
);
2789 * Relay the current marker-* attribute values...
2792 if (ipp_supplies
< 0)
2794 ppd_file_t
*ppd
; /* PPD file */
2795 ppd_attr_t
*ppdattr
; /* Attribute in PPD file */
2797 if ((ppd
= ppdOpenFile(getenv("PPD"))) != NULL
&&
2798 (ppdattr
= ppdFindAttr(ppd
, "cupsIPPSupplies", NULL
)) != NULL
&&
2799 ppdattr
->value
&& _cups_strcasecmp(ppdattr
->value
, "true"))
2807 if (ipp_supplies
> 0)
2809 if ((marker
= ippFindAttribute(ipp
, "marker-colors", IPP_TAG_NAME
)) != NULL
)
2810 report_attr(marker
);
2811 if ((marker
= ippFindAttribute(ipp
, "marker-high-levels",
2812 IPP_TAG_INTEGER
)) != NULL
)
2813 report_attr(marker
);
2814 if ((marker
= ippFindAttribute(ipp
, "marker-levels",
2815 IPP_TAG_INTEGER
)) != NULL
)
2816 report_attr(marker
);
2817 if ((marker
= ippFindAttribute(ipp
, "marker-low-levels",
2818 IPP_TAG_INTEGER
)) != NULL
)
2819 report_attr(marker
);
2820 if ((marker
= ippFindAttribute(ipp
, "marker-message",
2821 IPP_TAG_TEXT
)) != NULL
)
2822 report_attr(marker
);
2823 if ((marker
= ippFindAttribute(ipp
, "marker-names", IPP_TAG_NAME
)) != NULL
)
2824 report_attr(marker
);
2825 if ((marker
= ippFindAttribute(ipp
, "marker-types",
2826 IPP_TAG_KEYWORD
)) != NULL
)
2827 report_attr(marker
);
2832 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
2834 * 'run_as_user()' - Run the IPP backend as the printing user.
2836 * This function uses an XPC-based user agent to run the backend as the printing
2837 * user. We need to do this in order to have access to the user's Kerberos
2841 static int /* O - Exit status */
2842 run_as_user(char *argv
[], /* I - Command-line arguments */
2843 uid_t uid
, /* I - User ID */
2844 const char *device_uri
, /* I - Device URI */
2845 int fd
) /* I - File to print */
2847 const char *auth_negotiate
;/* AUTH_NEGOTIATE env var */
2848 xpc_connection_t conn
; /* Connection to XPC service */
2849 xpc_object_t request
; /* Request message dictionary */
2850 __block xpc_object_t response
; /* Response message dictionary */
2851 dispatch_semaphore_t sem
; /* Semaphore for waiting for response */
2852 int status
= CUPS_BACKEND_FAILED
;
2853 /* Status of request */
2856 fprintf(stderr
, "DEBUG: Running IPP backend as UID %d.\n", (int)uid
);
2859 * Connect to the user agent for the specified UID...
2862 conn
= xpc_connection_create_mach_service(kPMPrintUIToolAgent
,
2863 dispatch_get_global_queue(0, 0), 0);
2866 _cupsLangPrintFilter(stderr
, "ERROR",
2867 _("Unable to start backend process."));
2868 fputs("DEBUG: Unable to create connection to agent.\n", stderr
);
2872 xpc_connection_set_event_handler(conn
,
2873 ^(xpc_object_t event
)
2875 xpc_type_t messageType
= xpc_get_type(event
);
2877 if (messageType
== XPC_TYPE_ERROR
)
2879 if (event
== XPC_ERROR_CONNECTION_INTERRUPTED
)
2880 fprintf(stderr
, "DEBUG: Interrupted connection to service %s.\n",
2881 xpc_connection_get_name(conn
));
2882 else if (event
== XPC_ERROR_CONNECTION_INVALID
)
2883 fprintf(stderr
, "DEBUG: Connection invalid for service %s.\n",
2884 xpc_connection_get_name(conn
));
2886 fprintf(stderr
, "DEBUG: Unxpected error for service %s: %s\n",
2887 xpc_connection_get_name(conn
),
2888 xpc_dictionary_get_string(event
, XPC_ERROR_KEY_DESCRIPTION
));
2891 xpc_connection_set_target_uid(conn
, uid
);
2892 xpc_connection_resume(conn
);
2895 * Try starting the backend...
2898 request
= xpc_dictionary_create(NULL
, NULL
, 0);
2899 xpc_dictionary_set_int64(request
, "command", kPMStartJob
);
2900 xpc_dictionary_set_string(request
, "device-uri", device_uri
);
2901 xpc_dictionary_set_string(request
, "job-id", argv
[1]);
2902 xpc_dictionary_set_string(request
, "user", argv
[2]);
2903 xpc_dictionary_set_string(request
, "title", argv
[3]);
2904 xpc_dictionary_set_string(request
, "copies", argv
[4]);
2905 xpc_dictionary_set_string(request
, "options", argv
[5]);
2906 xpc_dictionary_set_string(request
, "auth-info-required",
2907 getenv("AUTH_INFO_REQUIRED"));
2908 if ((auth_negotiate
= getenv("AUTH_NEGOTIATE")) != NULL
)
2909 xpc_dictionary_set_string(request
, "auth-negotiate", auth_negotiate
);
2910 xpc_dictionary_set_fd(request
, "stdin", fd
);
2911 xpc_dictionary_set_fd(request
, "stderr", 2);
2912 xpc_dictionary_set_fd(request
, "side-channel", CUPS_SC_FD
);
2914 sem
= dispatch_semaphore_create(0);
2917 xpc_connection_send_message_with_reply(conn
, request
,
2918 dispatch_get_global_queue(0,0),
2919 ^(xpc_object_t reply
)
2921 /* Save the response and wake up */
2922 if (xpc_get_type(reply
)
2923 == XPC_TYPE_DICTIONARY
)
2924 response
= xpc_retain(reply
);
2926 dispatch_semaphore_signal(sem
);
2929 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
2930 xpc_release(request
);
2931 dispatch_release(sem
);
2935 child_pid
= xpc_dictionary_get_int64(response
, "child-pid");
2937 xpc_release(response
);
2940 fprintf(stderr
, "DEBUG: Child PID=%d.\n", child_pid
);
2943 _cupsLangPrintFilter(stderr
, "ERROR",
2944 _("Unable to start backend process."));
2945 fputs("DEBUG: No child PID.\n", stderr
);
2951 _cupsLangPrintFilter(stderr
, "ERROR",
2952 _("Unable to start backend process."));
2953 fputs("DEBUG: No reply from agent.\n", stderr
);
2958 * Then wait for the backend to finish...
2961 request
= xpc_dictionary_create(NULL
, NULL
, 0);
2962 xpc_dictionary_set_int64(request
, "command", kPMWaitForJob
);
2963 xpc_dictionary_set_fd(request
, "stderr", 2);
2965 sem
= dispatch_semaphore_create(0);
2968 xpc_connection_send_message_with_reply(conn
, request
,
2969 dispatch_get_global_queue(0,0),
2970 ^(xpc_object_t reply
)
2972 /* Save the response and wake up */
2973 if (xpc_get_type(reply
)
2974 == XPC_TYPE_DICTIONARY
)
2975 response
= xpc_retain(reply
);
2977 dispatch_semaphore_signal(sem
);
2980 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
2981 xpc_release(request
);
2982 dispatch_release(sem
);
2986 status
= xpc_dictionary_get_int64(response
, "status");
2988 if (status
== SIGTERM
|| status
== SIGKILL
|| status
== SIGPIPE
)
2990 fprintf(stderr
, "DEBUG: Child terminated on signal %d.\n", status
);
2991 status
= CUPS_BACKEND_FAILED
;
2993 else if (WIFSIGNALED(status
))
2995 fprintf(stderr
, "DEBUG: Child crashed on signal %d.\n", status
);
2996 status
= CUPS_BACKEND_STOP
;
2998 else if (WIFEXITED(status
))
3000 status
= WEXITSTATUS(status
);
3001 fprintf(stderr
, "DEBUG: Child exited with status %d.\n", status
);
3004 xpc_release(response
);
3007 _cupsLangPrintFilter(stderr
, "ERROR",
3008 _("Unable to get backend exit status."));
3014 xpc_connection_suspend(conn
);
3015 xpc_connection_cancel(conn
);
3021 #endif /* HAVE_GSSAPI && HAVE_XPC */
3025 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
3029 sigterm_handler(int sig
) /* I - Signal */
3031 (void)sig
; /* remove compiler warnings... */
3033 write(2, "DEBUG: Got SIGTERM.\n", 20);
3035 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
3038 kill(child_pid
, sig
);
3041 #endif /* HAVE_GSSAPI && HAVE_XPC */
3046 * Flag that the job should be canceled...
3049 write(2, "DEBUG: job_canceled = 1.\n", 25);
3056 * The scheduler already tried to cancel us once, now just terminate
3057 * after removing our temp file!
3061 unlink(tmpfilename
);
3068 * 'timeout_cb()' - Handle HTTP timeouts.
3071 static int /* O - 1 to continue, 0 to cancel */
3072 timeout_cb(http_t
*http
, /* I - Connection to server (unused) */
3073 void *user_data
) /* I - User data (unused) */
3078 return (!job_canceled
);
3083 * 'update_reasons()' - Update the printer-state-reasons values.
3087 update_reasons(ipp_attribute_t
*attr
, /* I - printer-state-reasons or NULL */
3088 const char *s
) /* I - STATE: string or NULL */
3090 char op
; /* Add (+), remove (-), replace (\0) */
3091 cups_array_t
*new_reasons
; /* New reasons array */
3092 char *reason
, /* Current reason */
3093 add
[2048], /* Reasons added string */
3094 *addptr
, /* Pointer into add string */
3095 rem
[2048], /* Reasons removed string */
3096 *remptr
; /* Pointer into remove string */
3097 const char *addprefix
, /* Current add string prefix */
3098 *remprefix
; /* Current remove string prefix */
3101 fprintf(stderr
, "DEBUG: update_reasons(attr=%d(%s%s), s=\"%s\")\n",
3102 attr
? attr
->num_values
: 0, attr
? attr
->values
[0].string
.text
: "",
3103 attr
&& attr
->num_values
> 1 ? ",..." : "", s
? s
: "(null)");
3106 * Create an array of new reason keyword strings...
3111 int i
; /* Looping var */
3113 new_reasons
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
3116 for (i
= 0; i
< attr
->num_values
; i
++)
3118 reason
= attr
->values
[i
].string
.text
;
3120 if (strcmp(reason
, "none") &&
3121 strcmp(reason
, "none-report") &&
3122 strcmp(reason
, "paused") &&
3123 strncmp(reason
, "spool-area-full", 15) &&
3124 strcmp(reason
, "com.apple.print.recoverable-warning") &&
3125 strncmp(reason
, "cups-", 5))
3126 cupsArrayAdd(new_reasons
, reason
);
3131 if (*s
== '+' || *s
== '-')
3136 new_reasons
= _cupsArrayNewStrings(s
);
3142 * Compute the changes...
3146 addprefix
= "STATE: +";
3149 remprefix
= "STATE: -";
3152 fprintf(stderr
, "DEBUG2: op='%c', new_reasons=%d, state_reasons=%d\n",
3153 op
? op
: ' ', cupsArrayCount(new_reasons
),
3154 cupsArrayCount(state_reasons
));
3156 _cupsMutexLock(&report_mutex
);
3164 for (reason
= (char *)cupsArrayFirst(new_reasons
);
3166 reason
= (char *)cupsArrayNext(new_reasons
))
3168 if (!cupsArrayFind(state_reasons
, reason
))
3170 if (!strncmp(reason
, "cups-remote-", 12))
3173 * If we are setting cups-remote-xxx, remove all other cups-remote-xxx
3177 char *temp
; /* Current reason in state_reasons */
3179 cupsArraySave(state_reasons
);
3181 for (temp
= (char *)cupsArrayFirst(state_reasons
);
3183 temp
= (char *)cupsArrayNext(state_reasons
))
3184 if (!strncmp(temp
, "cups-remote-", 12))
3186 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
3188 remptr
+= strlen(remptr
);
3191 cupsArrayRemove(state_reasons
, temp
);
3195 cupsArrayRestore(state_reasons
);
3198 cupsArrayAdd(state_reasons
, reason
);
3200 snprintf(addptr
, sizeof(add
) - (addptr
- add
), "%s%s", addprefix
,
3202 addptr
+= strlen(addptr
);
3213 for (reason
= (char *)cupsArrayFirst(new_reasons
);
3215 reason
= (char *)cupsArrayNext(new_reasons
))
3217 if (cupsArrayFind(state_reasons
, reason
))
3219 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
3221 remptr
+= strlen(remptr
);
3224 cupsArrayRemove(state_reasons
, reason
);
3231 * Replace reasons...
3234 for (reason
= (char *)cupsArrayFirst(state_reasons
);
3236 reason
= (char *)cupsArrayNext(state_reasons
))
3238 if (strncmp(reason
, "cups-", 5) && !cupsArrayFind(new_reasons
, reason
))
3240 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
3242 remptr
+= strlen(remptr
);
3245 cupsArrayRemove(state_reasons
, reason
);
3249 for (reason
= (char *)cupsArrayFirst(new_reasons
);
3251 reason
= (char *)cupsArrayNext(new_reasons
))
3253 if (!cupsArrayFind(state_reasons
, reason
))
3255 cupsArrayAdd(state_reasons
, reason
);
3257 snprintf(addptr
, sizeof(add
) - (addptr
- add
), "%s%s", addprefix
,
3259 addptr
+= strlen(addptr
);
3265 _cupsMutexUnlock(&report_mutex
);
3268 * Report changes and return...
3271 if (add
[0] && rem
[0])
3272 fprintf(stderr
, "%s\n%s\n", add
, rem
);
3274 fprintf(stderr
, "%s\n", add
);
3276 fprintf(stderr
, "%s\n", rem
);