2 * "$Id: ipp.c 9759 2011-05-11 03:24:33Z mike $"
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 extern void xpc_connection_set_target_uid(xpc_connection_t connection
,
50 #endif /* HAVE_GSSAPI && HAVE_XPC */
57 typedef struct _cups_monitor_s
/**** Monitoring data ****/
59 const char *uri
, /* Printer URI */
60 *hostname
, /* Hostname */
62 *resource
; /* Resource path */
63 int port
, /* Port number */
64 version
, /* IPP version */
65 job_id
; /* Job ID for submitted job */
66 const char *job_name
; /* Job name for submitted job */
67 http_encryption_t encryption
; /* Use encryption? */
68 ipp_jstate_t job_state
; /* Current job state */
69 ipp_pstate_t printer_state
; /* Current printer state */
77 static const char *auth_info_required
;
78 /* New auth-info-required value */
79 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
80 static int child_pid
= 0; /* Child process ID */
81 #endif /* HAVE_GSSAPI && HAVE_XPC */
82 static const char * const jattrs
[] = /* Job attributes we want */
84 "job-impressions-completed",
85 "job-media-sheets-completed",
87 "job-originating-user-name",
91 static int job_canceled
= 0;
93 static char *password
= NULL
;
94 /* Password for device URI */
95 static int password_tries
= 0;
97 static const char * const pattrs
[] = /* Printer attributes we want */
101 "document-format-default",
102 "document-format-supported",
104 "marker-high-levels",
110 "media-col-supported",
111 "multiple-document-handling-supported",
112 "operations-supported",
114 "printer-alert-description",
115 "printer-is-accepting-jobs",
117 "printer-state-message",
118 "printer-state-reasons"
120 static const char * const remote_job_states
[] =
121 { /* Remote job state keywords */
122 "+cups-remote-pending",
123 "+cups-remote-pending-held",
124 "+cups-remote-processing",
125 "+cups-remote-stopped",
126 "+cups-remote-canceled",
127 "+cups-remote-aborted",
128 "+cups-remote-completed"
130 static _cups_mutex_t report_mutex
= _CUPS_MUTEX_INITIALIZER
;
131 /* Mutex to control access */
132 static int num_attr_cache
= 0;
133 /* Number of cached attributes */
134 static cups_option_t
*attr_cache
= NULL
;
135 /* Cached attributes */
136 static cups_array_t
*state_reasons
; /* Array of printe-state-reasons keywords */
137 static char tmpfilename
[1024] = "";
138 /* Temporary spool file name */
145 static void cancel_job(http_t
*http
, const char *uri
, int id
,
146 const char *resource
, const char *user
,
148 static ipp_pstate_t
check_printer_state(http_t
*http
, const char *uri
,
149 const char *resource
,
150 const char *user
, int version
);
152 static void compress_files(int num_files
, char **files
);
153 #endif /* HAVE_LIBZ */
154 static void *monitor_printer(_cups_monitor_t
*monitor
);
155 static ipp_t
*new_request(ipp_op_t op
, int version
, const char *uri
,
156 const char *user
, const char *title
,
157 int num_options
, cups_option_t
*options
,
158 const char *compression
, int copies
,
159 const char *format
, _ppd_cache_t
*pc
,
160 ipp_attribute_t
*media_col_sup
,
161 ipp_attribute_t
*doc_handling_sup
);
162 static const char *password_cb(const char *);
163 static void report_attr(ipp_attribute_t
*attr
);
164 static void report_printer_state(ipp_t
*ipp
);
165 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
166 static int run_as_user(int argc
, char *argv
[], uid_t uid
,
167 const char *device_uri
, int fd
);
168 #endif /* HAVE_GSSAPI && HAVE_XPC */
169 static void sigterm_handler(int sig
);
170 static int timeout_cb(http_t
*http
, void *user_data
);
171 static void update_reasons(ipp_attribute_t
*attr
, const char *s
);
175 * 'main()' - Send a file to the printer or server.
179 * printer-uri job-id user title copies options [file]
182 int /* O - Exit status */
183 main(int argc
, /* I - Number of command-line args */
184 char *argv
[]) /* I - Command-line arguments */
186 int i
; /* Looping var */
187 int send_options
; /* Send job options? */
188 int num_options
; /* Number of printer options */
189 cups_option_t
*options
; /* Printer options */
190 const char *device_uri
; /* Device URI */
191 char scheme
[255], /* Scheme in URI */
192 hostname
[1024], /* Hostname */
193 username
[255], /* Username info */
194 resource
[1024], /* Resource info (printer name) */
195 addrname
[256], /* Address name */
196 *optptr
, /* Pointer to URI options */
197 *name
, /* Name of option */
198 *value
, /* Value of option */
199 sep
; /* Separator character */
200 http_addrlist_t
*addrlist
; /* Address of printer */
201 int snmp_fd
, /* SNMP socket */
202 start_count
, /* Page count via SNMP at start */
203 page_count
, /* Page count via SNMP */
204 have_supplies
; /* Printer supports supply levels? */
205 int num_files
; /* Number of files to print */
206 char **files
, /* Files to print */
207 *compatfile
= NULL
; /* Compatibility filename */
208 off_t compatsize
= 0; /* Size of compatibility file */
209 int port
; /* Port number (not used) */
210 char portname
[255]; /* Port name */
211 char uri
[HTTP_MAX_URI
]; /* Updated URI without user/pass */
212 char print_job_name
[1024]; /* Update job-name for Print-Job */
213 http_status_t http_status
; /* Status of HTTP request */
214 ipp_status_t ipp_status
; /* Status of IPP request */
215 http_t
*http
; /* HTTP connection */
216 ipp_t
*request
, /* IPP request */
217 *response
, /* IPP response */
218 *supported
; /* get-printer-attributes response */
219 time_t start_time
; /* Time of first connect */
220 int contimeout
; /* Connection timeout */
221 int delay
, /* Delay for retries */
222 prev_delay
; /* Previous delay */
223 const char *compression
; /* Compression mode */
224 int waitjob
, /* Wait for job complete? */
225 waitprinter
; /* Wait for printer ready? */
226 _cups_monitor_t monitor
; /* Monitoring data */
227 ipp_attribute_t
*job_id_attr
; /* job-id attribute */
228 int job_id
; /* job-id value */
229 ipp_attribute_t
*job_sheets
; /* job-media-sheets-completed */
230 ipp_attribute_t
*job_state
; /* job-state */
231 ipp_attribute_t
*copies_sup
; /* copies-supported */
232 ipp_attribute_t
*cups_version
; /* cups-version */
233 ipp_attribute_t
*format_dflt
; /* document-format-default */
234 ipp_attribute_t
*format_sup
; /* document-format-supported */
235 ipp_attribute_t
*media_col_sup
; /* media-col-supported */
236 ipp_attribute_t
*operations_sup
; /* operations-supported */
237 ipp_attribute_t
*doc_handling_sup
; /* multiple-document-handling-supported */
238 ipp_attribute_t
*printer_state
; /* printer-state attribute */
239 ipp_attribute_t
*printer_accepting
; /* printer-is-accepting-jobs */
240 int create_job
= 0, /* Does printer support Create-Job? */
241 send_document
= 0, /* Does printer support Send-Document? */
242 validate_job
= 0; /* Does printer support Validate-Job? */
243 int copies
, /* Number of copies for job */
244 copies_remaining
; /* Number of copies remaining */
245 const char *content_type
, /* CONTENT_TYPE environment variable */
246 *final_content_type
, /* FINAL_CONTENT_TYPE environment var */
247 *document_format
; /* document-format value */
248 int fd
; /* File descriptor */
249 off_t bytes
= 0; /* Bytes copied */
250 char buffer
[16384]; /* Copy buffer */
251 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
252 struct sigaction action
; /* Actions for POSIX signals */
253 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
254 int version
; /* IPP version */
255 ppd_file_t
*ppd
; /* PPD file */
256 _ppd_cache_t
*pc
; /* PPD cache and mapping data */
257 fd_set input
; /* Input set for select() */
261 * Make sure status messages are not buffered...
264 setbuf(stderr
, NULL
);
267 * Ignore SIGPIPE and catch SIGTERM signals...
271 sigset(SIGPIPE
, SIG_IGN
);
272 sigset(SIGTERM
, sigterm_handler
);
273 #elif defined(HAVE_SIGACTION)
274 memset(&action
, 0, sizeof(action
));
275 action
.sa_handler
= SIG_IGN
;
276 sigaction(SIGPIPE
, &action
, NULL
);
278 sigemptyset(&action
.sa_mask
);
279 sigaddset(&action
.sa_mask
, SIGTERM
);
280 action
.sa_handler
= sigterm_handler
;
281 sigaction(SIGTERM
, &action
, NULL
);
283 signal(SIGPIPE
, SIG_IGN
);
284 signal(SIGTERM
, sigterm_handler
);
285 #endif /* HAVE_SIGSET */
288 * Check command-line...
295 if ((s
= strrchr(argv
[0], '/')) != NULL
)
300 printf("network %s \"Unknown\" \"%s (%s)\"\n",
301 s
, _cupsLangString(cupsLangDefault(),
302 _("Internet Printing Protocol")), s
);
303 return (CUPS_BACKEND_OK
);
307 _cupsLangPrintf(stderr
,
308 _("Usage: %s job-id user title copies options [file]"),
310 return (CUPS_BACKEND_STOP
);
314 * Get the device URI...
317 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
319 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
322 if (getenv("CLASS") != NULL
)
323 return (CUPS_BACKEND_FAILED
);
326 if ((auth_info_required
= getenv("AUTH_INFO_REQUIRED")) == NULL
)
327 auth_info_required
= "none";
329 state_reasons
= _cupsArrayNewStrings(getenv("PRINTER_STATE_REASONS"));
333 * For Kerberos, become the printing user (if we can) to get the credentials
337 if (!getuid() && (value
= getenv("AUTH_UID")) != NULL
)
339 uid_t uid
= (uid_t
)atoi(value
);
346 return (run_as_user(argc
, argv
, uid
, device_uri
, 0));
349 int status
= 0; /* Exit status */
351 for (i
= 6; i
< argc
&& !status
&& !job_canceled
; i
++)
353 if ((fd
= open(argv
[i
], O_RDONLY
)) >= 0)
355 status
= run_as_user(argc
, argv
, uid
, device_uri
, fd
);
360 _cupsLangPrintError("ERROR", _("Unable to open print file"));
361 status
= CUPS_BACKEND_FAILED
;
369 # else /* No XPC, just try to run as the user ID */
372 # endif /* HAVE_XPC */
374 #endif /* HAVE_GSSAPI */
377 * Get the (final) content type...
380 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
381 content_type
= "application/octet-stream";
383 if ((final_content_type
= getenv("FINAL_CONTENT_TYPE")) == NULL
)
385 final_content_type
= content_type
;
387 if (!strncmp(final_content_type
, "printer/", 8))
388 final_content_type
= "application/vnd.cups-raw";
392 * Extract the hostname and printer name from the URI...
395 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
396 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
397 resource
, sizeof(resource
));
400 port
= IPP_PORT
; /* Default to port 631 */
402 if (!strcmp(scheme
, "https"))
403 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
405 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
408 * See if there are any options...
415 contimeout
= 7 * 24 * 60 * 60;
417 if ((optptr
= strchr(resource
, '?')) != NULL
)
420 * Yup, terminate the device name string and move to the first
421 * character of the optptr...
427 * Then parse the optptr...
438 while (*optptr
&& *optptr
!= '=' && *optptr
!= '+' && *optptr
!= '&')
441 if ((sep
= *optptr
) != '\0')
452 while (*optptr
&& *optptr
!= '+' && *optptr
!= '&')
462 * Process the option...
465 if (!_cups_strcasecmp(name
, "waitjob"))
468 * Wait for job completion?
471 waitjob
= !_cups_strcasecmp(value
, "on") ||
472 !_cups_strcasecmp(value
, "yes") ||
473 !_cups_strcasecmp(value
, "true");
475 else if (!_cups_strcasecmp(name
, "waitprinter"))
478 * Wait for printer idle?
481 waitprinter
= !_cups_strcasecmp(value
, "on") ||
482 !_cups_strcasecmp(value
, "yes") ||
483 !_cups_strcasecmp(value
, "true");
485 else if (!_cups_strcasecmp(name
, "encryption"))
488 * Enable/disable encryption?
491 if (!_cups_strcasecmp(value
, "always"))
492 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
493 else if (!_cups_strcasecmp(value
, "required"))
494 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
495 else if (!_cups_strcasecmp(value
, "never"))
496 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
497 else if (!_cups_strcasecmp(value
, "ifrequested"))
498 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
501 _cupsLangPrintFilter(stderr
, "ERROR",
502 _("Unknown encryption option value: \"%s\"."),
506 else if (!_cups_strcasecmp(name
, "version"))
508 if (!strcmp(value
, "1.0"))
510 else if (!strcmp(value
, "1.1"))
512 else if (!strcmp(value
, "2.0"))
514 else if (!strcmp(value
, "2.1"))
516 else if (!strcmp(value
, "2.2"))
520 _cupsLangPrintFilter(stderr
, "ERROR",
521 _("Unknown version option value: \"%s\"."),
526 else if (!_cups_strcasecmp(name
, "compression"))
528 if (!_cups_strcasecmp(value
, "true") || !_cups_strcasecmp(value
, "yes") ||
529 !_cups_strcasecmp(value
, "on") || !_cups_strcasecmp(value
, "gzip"))
530 compression
= "gzip";
532 #endif /* HAVE_LIBZ */
533 else if (!_cups_strcasecmp(name
, "contimeout"))
536 * Set the connection timeout...
540 contimeout
= atoi(value
);
548 _cupsLangPrintFilter(stderr
, "ERROR",
549 _("Unknown option \"%s\" with value \"%s\"."),
556 * If we have 7 arguments, print the file named on the command-line.
557 * Otherwise, copy stdin to a temporary file and print the temporary
565 send_options
= !_cups_strcasecmp(final_content_type
, "application/pdf") ||
566 !_cups_strcasecmp(final_content_type
, "application/vnd.cups-pdf") ||
567 !_cups_strncasecmp(final_content_type
, "image/", 6);
569 fputs("DEBUG: Sending stdin for job...\n", stderr
);
574 * Point to the files on the command-line...
577 num_files
= argc
- 6;
583 compress_files(num_files
, files
);
584 #endif /* HAVE_LIBZ */
586 fprintf(stderr
, "DEBUG: %d files to send in job...\n", num_files
);
590 * Set the authentication info, if any...
593 cupsSetPasswordCB(password_cb
);
598 * Use authenticaion information in the device URI...
601 if ((password
= strchr(username
, ':')) != NULL
)
604 cupsSetUser(username
);
609 * Try loading authentication information from the environment.
612 const char *ptr
= getenv("AUTH_USERNAME");
617 password
= getenv("AUTH_PASSWORD");
621 * Try finding the remote server...
624 start_time
= time(NULL
);
626 sprintf(portname
, "%d", port
);
628 update_reasons(NULL
, "+connecting-to-device");
629 fprintf(stderr
, "DEBUG: Looking up \"%s\"...\n", hostname
);
631 while ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
633 _cupsLangPrintFilter(stderr
, "INFO",
634 _("Unable to locate printer \"%s\"."), hostname
);
637 if (getenv("CLASS") != NULL
)
639 update_reasons(NULL
, "-connecting-to-device");
640 return (CUPS_BACKEND_STOP
);
644 http
= _httpCreate(hostname
, port
, addrlist
, cupsEncryption(), AF_UNSPEC
);
645 httpSetTimeout(http
, 30.0, timeout_cb
, NULL
);
648 * See if the printer supports SNMP...
651 if ((snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
)) >= 0)
653 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
),
657 have_supplies
= start_count
= 0;
660 * Wait for data from the filter...
665 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), 0, backendNetworkSideCB
))
666 return (CUPS_BACKEND_OK
);
667 else if ((bytes
= read(0, buffer
, sizeof(buffer
))) <= 0)
668 return (CUPS_BACKEND_OK
);
672 * Try connecting to the remote server...
675 delay
= _cupsNextDelay(0, &prev_delay
);
679 fprintf(stderr
, "DEBUG: Connecting to %s:%d\n", hostname
, port
);
680 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
682 if (httpReconnect(http
))
684 int error
= errno
; /* Connection error */
686 if (http
->status
== HTTP_PKI_ERROR
)
687 update_reasons(NULL
, "+cups-certificate-error");
692 if (getenv("CLASS") != NULL
)
695 * If the CLASS environment variable is set, the job was submitted
696 * to a class and not to a specific queue. In this case, we want
697 * to abort immediately so that the job can be requeued on the next
698 * available printer in the class.
701 _cupsLangPrintFilter(stderr
, "INFO",
702 _("Unable to contact printer, queuing on next "
703 "printer in class."));
706 * Sleep 5 seconds to keep the job from requeuing too rapidly...
711 update_reasons(NULL
, "-connecting-to-device");
713 return (CUPS_BACKEND_FAILED
);
716 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
718 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
719 errno
== EHOSTUNREACH
)
721 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
723 _cupsLangPrintFilter(stderr
, "ERROR",
724 _("The printer is not responding."));
725 update_reasons(NULL
, "-connecting-to-device");
726 return (CUPS_BACKEND_FAILED
);
732 _cupsLangPrintFilter(stderr
, "WARNING",
733 _("The printer may not exist or "
734 "is unavailable at this time."));
738 _cupsLangPrintFilter(stderr
, "WARNING",
739 _("The printer is unreachable at this "
745 _cupsLangPrintFilter(stderr
, "WARNING",
746 _("The printer is busy."));
752 delay
= _cupsNextDelay(delay
, &prev_delay
);
756 _cupsLangPrintFilter(stderr
, "ERROR",
757 _("The printer is not responding."));
765 update_reasons(NULL
, "-cups-certificate-error");
767 while (http
->fd
< 0);
769 if (job_canceled
|| !http
)
770 return (CUPS_BACKEND_FAILED
);
772 update_reasons(NULL
, "-connecting-to-device");
773 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
775 fprintf(stderr
, "DEBUG: Connected to %s:%d...\n",
776 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
777 _httpAddrPort(http
->hostaddr
));
780 * Build a URI for the printer and fill the standard IPP attributes for
781 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
782 * might contain username:password information...
785 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), scheme
, NULL
, hostname
,
789 * First validate the destination and see if the device supports multiple
796 media_col_sup
= NULL
;
798 operations_sup
= NULL
;
799 doc_handling_sup
= NULL
;
804 * Check for side-channel requests...
807 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
810 * Build the IPP request...
813 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
814 request
->request
.op
.version
[0] = version
/ 10;
815 request
->request
.op
.version
[1] = version
% 10;
817 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
820 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
821 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
828 fputs("DEBUG: Getting supported attributes...\n", stderr
);
830 if (http
->version
< HTTP_1_1
)
832 fprintf(stderr
, "DEBUG: Printer responded with HTTP version %d.%d.\n",
833 http
->version
/ 100, http
->version
% 100);
834 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
835 "cups-ipp-wrong-http-version");
838 supported
= cupsDoRequest(http
, request
, resource
);
839 ipp_status
= cupsLastError();
841 fprintf(stderr
, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
842 ippErrorString(ipp_status
), cupsLastErrorString());
844 if (ipp_status
> IPP_OK_CONFLICT
)
846 fprintf(stderr
, "DEBUG: Get-Printer-Attributes returned %s.\n",
847 ippErrorString(ipp_status
));
849 if (ipp_status
== IPP_PRINTER_BUSY
||
850 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
852 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
854 _cupsLangPrintFilter(stderr
, "ERROR",
855 _("The printer is not responding."));
856 return (CUPS_BACKEND_FAILED
);
859 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is busy."));
861 report_printer_state(supported
);
865 delay
= _cupsNextDelay(delay
, &prev_delay
);
867 else if ((ipp_status
== IPP_BAD_REQUEST
||
868 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
> 10)
871 * Switch to IPP/1.1 or IPP/1.0...
876 _cupsLangPrintFilter(stderr
, "INFO",
877 _("Printer does not support IPP/%d.%d, trying "
878 "IPP/%s."), version
/ 10, version
% 10, "1.1");
883 _cupsLangPrintFilter(stderr
, "INFO",
884 _("Printer does not support IPP/%d.%d, trying "
885 "IPP/%s."), version
/ 10, version
% 10, "1.0");
891 else if (ipp_status
== IPP_NOT_FOUND
)
893 _cupsLangPrintFilter(stderr
, "ERROR",
894 _("The printer URI is incorrect or no longer "
897 ippDelete(supported
);
899 return (CUPS_BACKEND_STOP
);
901 else if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
)
903 const char *www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
904 /* WWW-Authenticate field value */
906 if (!strncmp(www_auth
, "Negotiate", 9))
907 auth_info_required
= "negotiate";
908 else if (www_auth
[0])
909 auth_info_required
= "username,password";
911 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
912 return (CUPS_BACKEND_AUTH_REQUIRED
);
916 _cupsLangPrintFilter(stderr
, "ERROR",
917 _("Unable to get printer status."));
921 ippDelete(supported
);
926 if (!getenv("CLASS"))
929 * Check printer-is-accepting-jobs = false and printer-state-reasons for the
930 * "spool-area-full" keyword...
935 if ((printer_accepting
= ippFindAttribute(supported
,
936 "printer-is-accepting-jobs",
937 IPP_TAG_BOOLEAN
)) != NULL
&&
938 !printer_accepting
->values
[0].boolean
)
940 else if (!printer_accepting
)
941 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
942 "cups-ipp-missing-printer-is-accepting-jobs");
944 if ((printer_state
= ippFindAttribute(supported
,
945 "printer-state-reasons",
946 IPP_TAG_KEYWORD
)) != NULL
&& !busy
)
948 for (i
= 0; i
< printer_state
->num_values
; i
++)
949 if (!strcmp(printer_state
->values
[0].string
.text
,
950 "spool-area-full") ||
951 !strncmp(printer_state
->values
[0].string
.text
, "spool-area-full-",
959 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
960 "cups-ipp-missing-printer-state-reasons");
964 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is busy."));
966 report_printer_state(supported
);
970 delay
= _cupsNextDelay(delay
, &prev_delay
);
972 ippDelete(supported
);
979 * Check for supported attributes...
982 if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
983 IPP_TAG_RANGE
)) != NULL
)
986 * Has the "copies-supported" attribute - does it have an upper
990 fprintf(stderr
, "DEBUG: copies-supported=%d-%d\n",
991 copies_sup
->values
[0].range
.lower
,
992 copies_sup
->values
[0].range
.upper
);
994 if (copies_sup
->values
[0].range
.upper
<= 1)
995 copies_sup
= NULL
; /* No */
998 cups_version
= ippFindAttribute(supported
, "cups-version", IPP_TAG_TEXT
);
1000 if ((format_dflt
= ippFindAttribute(supported
, "document-format-default",
1001 IPP_TAG_MIMETYPE
)) != NULL
)
1003 fprintf(stderr
, "DEBUG: document-format-default (%d values)\n",
1004 format_dflt
->num_values
);
1005 for (i
= 0; i
< format_dflt
->num_values
; i
++)
1006 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
1007 format_dflt
->values
[i
].string
.text
);
1010 if ((format_sup
= ippFindAttribute(supported
, "document-format-supported",
1011 IPP_TAG_MIMETYPE
)) != NULL
)
1013 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
1014 format_sup
->num_values
);
1015 for (i
= 0; i
< format_sup
->num_values
; i
++)
1016 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
1017 format_sup
->values
[i
].string
.text
);
1020 if ((media_col_sup
= ippFindAttribute(supported
, "media-col-supported",
1021 IPP_TAG_KEYWORD
)) != NULL
)
1023 fprintf(stderr
, "DEBUG: media-col-supported (%d values)\n",
1024 media_col_sup
->num_values
);
1025 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
1026 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
1027 media_col_sup
->values
[i
].string
.text
);
1030 if ((operations_sup
= ippFindAttribute(supported
, "operations-supported",
1031 IPP_TAG_ENUM
)) != NULL
)
1033 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1034 if (operations_sup
->values
[i
].integer
== IPP_PRINT_JOB
)
1037 if (i
>= operations_sup
->num_values
)
1038 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1039 "cups-ipp-missing-print-job");
1041 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1042 if (operations_sup
->values
[i
].integer
== IPP_CANCEL_JOB
)
1045 if (i
>= operations_sup
->num_values
)
1046 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1047 "cups-ipp-missing-cancel-job");
1049 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1050 if (operations_sup
->values
[i
].integer
== IPP_GET_JOB_ATTRIBUTES
)
1053 if (i
>= operations_sup
->num_values
)
1054 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1055 "cups-ipp-missing-get-job-attributes");
1057 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1058 if (operations_sup
->values
[i
].integer
== IPP_GET_PRINTER_ATTRIBUTES
)
1061 if (i
>= operations_sup
->num_values
)
1062 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1063 "cups-ipp-missing-get-printer-attributes");
1065 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1067 if (operations_sup
->values
[i
].integer
== IPP_VALIDATE_JOB
)
1069 else if (operations_sup
->values
[i
].integer
== IPP_CREATE_JOB
)
1071 else if (operations_sup
->values
[i
].integer
== IPP_SEND_DOCUMENT
)
1077 fputs("DEBUG: Printer supports Create-Job but not Send-Document.\n",
1083 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1084 "cups-ipp-missing-validate-job");
1087 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1088 "cups-ipp-missing-operations-supported");
1090 doc_handling_sup
= ippFindAttribute(supported
,
1091 "multiple-document-handling-supported",
1094 report_printer_state(supported
);
1096 while (ipp_status
> IPP_OK_CONFLICT
);
1099 * See if the printer is accepting jobs and is not stopped; if either
1100 * condition is true and we are printing to a class, requeue the job...
1103 if (getenv("CLASS") != NULL
)
1105 printer_state
= ippFindAttribute(supported
, "printer-state",
1107 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
1110 if (printer_state
== NULL
||
1111 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
1113 printer_accepting
== NULL
||
1114 !printer_accepting
->values
[0].boolean
)
1117 * If the CLASS environment variable is set, the job was submitted
1118 * to a class and not to a specific queue. In this case, we want
1119 * to abort immediately so that the job can be requeued on the next
1120 * available printer in the class.
1123 _cupsLangPrintFilter(stderr
, "INFO",
1124 _("Unable to contact printer, queuing on next "
1125 "printer in class."));
1127 ippDelete(supported
);
1131 * Sleep 5 seconds to keep the job from requeuing too rapidly...
1136 return (CUPS_BACKEND_FAILED
);
1141 * See if the printer supports multiple copies...
1144 copies
= atoi(argv
[4]);
1146 if (copies_sup
|| argc
< 7)
1148 copies_remaining
= 1;
1150 if (argc
< 7 && !_cups_strncasecmp(final_content_type
, "image/", 6))
1154 copies_remaining
= copies
;
1157 * Prepare remaining printing options...
1165 num_options
= cupsParseOptions(argv
[5], 0, &options
);
1167 if (!cups_version
&& media_col_sup
)
1170 * Load the PPD file and generate PWG attribute mapping information...
1173 ppd
= ppdOpenFile(getenv("PPD"));
1174 pc
= _ppdCacheCreateWithPPD(ppd
);
1182 document_format
= NULL
;
1184 if (format_sup
!= NULL
)
1186 for (i
= 0; i
< format_sup
->num_values
; i
++)
1187 if (!_cups_strcasecmp(final_content_type
,
1188 format_sup
->values
[i
].string
.text
))
1190 document_format
= final_content_type
;
1194 if (!document_format
&&
1196 _cups_strcasecmp(format_dflt
->values
[0].string
.text
,
1197 "application/octet-stream")))
1199 for (i
= 0; i
< format_sup
->num_values
; i
++)
1200 if (!_cups_strcasecmp("application/octet-stream",
1201 format_sup
->values
[i
].string
.text
))
1203 document_format
= "application/octet-stream";
1210 * If the printer does not support HTTP/1.1 (which IPP requires), copy stdin
1211 * to a temporary file so that we can do a HTTP/1.0 submission...
1213 * (I hate compatibility hacks!)
1216 if (http
->version
< HTTP_1_1
&& num_files
== 0)
1218 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
1220 perror("DEBUG: Unable to create temporary file");
1221 return (CUPS_BACKEND_FAILED
);
1224 _cupsLangPrintFilter(stderr
, "INFO", _("Copying print data."));
1226 if ((compatsize
= write(fd
, buffer
, bytes
)) < 0)
1228 perror("DEBUG: Unable to write temporary file");
1229 return (CUPS_BACKEND_FAILED
);
1232 if ((bytes
= backendRunLoop(-1, fd
, snmp_fd
, &(addrlist
->addr
), 0, 0,
1233 backendNetworkSideCB
)) < 0)
1234 return (CUPS_BACKEND_FAILED
);
1236 compatsize
+= bytes
;
1240 compatfile
= tmpfilename
;
1241 files
= &compatfile
;
1244 else if (http
->version
< HTTP_1_1
&& num_files
== 1)
1246 struct stat fileinfo
; /* File information */
1248 if (!stat(files
[0], &fileinfo
))
1249 compatsize
= fileinfo
.st_size
;
1253 * Start monitoring the printer in the background...
1257 monitor
.hostname
= hostname
;
1258 monitor
.user
= argv
[2];
1259 monitor
.resource
= resource
;
1260 monitor
.port
= port
;
1261 monitor
.version
= version
;
1263 monitor
.encryption
= cupsEncryption();
1264 monitor
.job_state
= IPP_JOB_PENDING
;
1265 monitor
.printer_state
= IPP_PRINTER_IDLE
;
1269 monitor
.job_name
= argv
[3];
1273 snprintf(print_job_name
, sizeof(print_job_name
), "%s - %s", argv
[1],
1275 monitor
.job_name
= print_job_name
;
1278 _cupsThreadCreate((_cups_thread_func_t
)monitor_printer
, &monitor
);
1281 * Validate access to the printer...
1284 while (!job_canceled
&& validate_job
)
1286 request
= new_request(IPP_VALIDATE_JOB
, version
, uri
, argv
[2],
1287 monitor
.job_name
, num_options
, options
, compression
,
1288 copies_sup
? copies
: 1, document_format
, pc
,
1289 media_col_sup
, doc_handling_sup
);
1291 ippDelete(cupsDoRequest(http
, request
, resource
));
1293 ipp_status
= cupsLastError();
1295 fprintf(stderr
, "DEBUG: Validate-Job: %s (%s)\n",
1296 ippErrorString(ipp_status
), cupsLastErrorString());
1301 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
|| ipp_status
== IPP_PRINTER_BUSY
)
1303 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is busy."));
1306 else if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
1307 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1309 const char *www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
1310 /* WWW-Authenticate field value */
1312 if (!strncmp(www_auth
, "Negotiate", 9))
1313 auth_info_required
= "negotiate";
1314 else if (www_auth
[0])
1315 auth_info_required
= "username,password";
1319 else if (ipp_status
== IPP_OPERATION_NOT_SUPPORTED
)
1322 * This is all too common...
1325 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1326 "cups-ipp-missing-validate-job");
1329 else if (ipp_status
< IPP_REDIRECTION_OTHER_SITE
)
1334 * Then issue the print-job request...
1339 while (!job_canceled
&& copies_remaining
> 0)
1342 * Check for side-channel requests...
1345 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1348 * Build the IPP job creation request...
1354 request
= new_request((num_files
> 1 || create_job
) ? IPP_CREATE_JOB
:
1356 version
, uri
, argv
[2], monitor
.job_name
, num_options
,
1357 options
, compression
, copies_sup
? copies
: 1,
1358 document_format
, pc
, media_col_sup
, doc_handling_sup
);
1364 if (num_files
> 1 || create_job
)
1365 response
= cupsDoRequest(http
, request
, resource
);
1368 size_t length
= 0; /* Length of request */
1372 fputs("DEBUG: Sending file using HTTP/1.0 Content-Length...\n", stderr
);
1373 length
= ippLength(request
) + (size_t)compatsize
;
1376 fputs("DEBUG: Sending file using HTTP/1.1 chunking...\n", stderr
);
1378 http_status
= cupsSendRequest(http
, request
, resource
, length
);
1379 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
)
1383 if ((fd
= open(files
[0], O_RDONLY
)) < 0)
1385 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1386 return (CUPS_BACKEND_FAILED
);
1392 http_status
= cupsWriteRequestData(http
, buffer
, bytes
);
1395 while (http_status
== HTTP_CONTINUE
&&
1396 (!job_canceled
|| compatsize
> 0))
1399 * Check for side-channel requests and more print data...
1404 FD_SET(snmp_fd
, &input
);
1406 while (select(fd
> snmp_fd
? fd
+ 1 : snmp_fd
+ 1, &input
, NULL
, NULL
,
1407 NULL
) <= 0 && !job_canceled
);
1409 if (FD_ISSET(snmp_fd
, &input
))
1410 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1412 if (FD_ISSET(fd
, &input
))
1414 if ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1416 fprintf(stderr
, "DEBUG: Read %d bytes...\n", (int)bytes
);
1418 if (cupsWriteRequestData(http
, buffer
, bytes
) != HTTP_CONTINUE
)
1421 else if (bytes
== 0 || (errno
!= EINTR
&& errno
!= EAGAIN
))
1430 response
= cupsGetResponse(http
, resource
);
1434 ipp_status
= cupsLastError();
1436 fprintf(stderr
, "DEBUG: %s: %s (%s)\n",
1437 (num_files
> 1 || create_job
) ? "Create-Job" : "Print-Job",
1438 ippErrorString(ipp_status
), cupsLastErrorString());
1440 if (ipp_status
> IPP_OK_CONFLICT
)
1447 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1448 ipp_status
== IPP_NOT_POSSIBLE
||
1449 ipp_status
== IPP_PRINTER_BUSY
)
1451 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is busy."));
1457 * We can't re-submit when we have no files to print, so exit
1458 * immediately with the right status code...
1464 else if (ipp_status
== IPP_ERROR_JOB_CANCELED
)
1469 * Update auth-info-required as needed...
1472 _cupsLangPrintFilter(stderr
, "ERROR",
1473 _("Print file was not accepted."));
1475 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
)
1477 const char *www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
1478 /* WWW-Authenticate field value */
1480 if (!strncmp(www_auth
, "Negotiate", 9))
1481 auth_info_required
= "negotiate";
1482 else if (www_auth
[0])
1483 auth_info_required
= "username,password";
1485 else if (ipp_status
== IPP_REQUEST_VALUE
)
1488 * Print file is too large, abort this job...
1499 * We can't re-submit when we have no files to print, so exit
1500 * immediately with the right status code...
1507 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
1508 IPP_TAG_INTEGER
)) == NULL
)
1510 _cupsLangPrintFilter(stderr
, "INFO",
1511 _("Print file accepted - job ID unknown."));
1512 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1513 "cups-ipp-missing-job-id");
1518 monitor
.job_id
= job_id
= job_id_attr
->values
[0].integer
;
1519 _cupsLangPrintFilter(stderr
, "INFO",
1520 _("Print file accepted - job ID %d."), job_id
);
1523 fprintf(stderr
, "DEBUG: job-id=%d\n", job_id
);
1524 ippDelete(response
);
1529 if (job_id
&& (num_files
> 1 || create_job
))
1531 for (i
= 0; num_files
== 0 || i
< num_files
; i
++)
1534 * Check for side-channel requests...
1537 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1540 * Send the next file in the job...
1543 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
1544 request
->request
.op
.version
[0] = version
/ 10;
1545 request
->request
.op
.version
[1] = version
% 10;
1547 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1550 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1554 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1555 "requesting-user-name", NULL
, argv
[2]);
1557 if ((i
+ 1) >= num_files
)
1558 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
1560 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1561 "document-format", NULL
, document_format
);
1563 fprintf(stderr
, "DEBUG: Sending file %d using chunking...\n", i
+ 1);
1564 http_status
= cupsSendRequest(http
, request
, resource
, 0);
1565 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
)
1570 http_status
= cupsWriteRequestData(http
, buffer
, bytes
);
1574 if ((fd
= open(files
[i
], O_RDONLY
)) < 0)
1576 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1577 return (CUPS_BACKEND_FAILED
);
1586 while (!job_canceled
&&
1587 (bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1589 if (cupsWriteRequestData(http
, buffer
, bytes
) != HTTP_CONTINUE
)
1594 * Check for side-channel requests...
1597 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1605 ippDelete(cupsGetResponse(http
, resource
));
1608 fprintf(stderr
, "DEBUG: Send-Document: %s (%s)\n",
1609 ippErrorString(cupsLastError()), cupsLastErrorString());
1611 if (cupsLastError() > IPP_OK_CONFLICT
)
1613 ipp_status
= cupsLastError();
1615 _cupsLangPrintFilter(stderr
, "ERROR",
1616 _("Unable to add document to print job."));
1619 else if (num_files
== 0 || fd
< 0)
1624 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
1626 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
1627 copies_remaining
--;
1629 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1630 ipp_status
== IPP_NOT_POSSIBLE
||
1631 ipp_status
== IPP_PRINTER_BUSY
)
1633 else if (ipp_status
== IPP_REQUEST_VALUE
)
1636 * Print file is too large, abort this job...
1642 copies_remaining
--;
1645 * Wait for the job to complete...
1648 if (!job_id
|| !waitjob
)
1651 _cupsLangPrintFilter(stderr
, "INFO", _("Waiting for job to complete."));
1653 for (delay
= _cupsNextDelay(0, &prev_delay
); !job_canceled
;)
1656 * Check for side-channel requests...
1659 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1662 * Build an IPP_GET_JOB_ATTRIBUTES request...
1665 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1666 request
->request
.op
.version
[0] = version
/ 10;
1667 request
->request
.op
.version
[1] = version
% 10;
1669 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1672 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1676 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1677 "requesting-user-name", NULL
, argv
[2]);
1679 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1680 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
1687 httpReconnect(http
);
1688 response
= cupsDoRequest(http
, request
, resource
);
1689 ipp_status
= cupsLastError();
1691 if (ipp_status
== IPP_NOT_FOUND
)
1694 * Job has gone away and/or the server has no job history...
1697 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1698 "cups-ipp-missing-job-history");
1699 ippDelete(response
);
1701 ipp_status
= IPP_OK
;
1705 fprintf(stderr
, "DEBUG: Get-Job-Attributes: %s (%s)\n",
1706 ippErrorString(ipp_status
), cupsLastErrorString());
1708 if (ipp_status
> IPP_OK_CONFLICT
)
1710 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1711 ipp_status
!= IPP_NOT_POSSIBLE
&&
1712 ipp_status
!= IPP_PRINTER_BUSY
)
1714 ippDelete(response
);
1715 ipp_status
= IPP_OK
;
1722 if ((job_state
= ippFindAttribute(response
, "job-state",
1723 IPP_TAG_ENUM
)) != NULL
)
1726 * Reflect the remote job state in the local queue...
1730 job_state
->values
[0].integer
>= IPP_JOB_PENDING
&&
1731 job_state
->values
[0].integer
<= IPP_JOB_COMPLETED
)
1732 update_reasons(NULL
,
1733 remote_job_states
[job_state
->values
[0].integer
-
1736 if ((job_sheets
= ippFindAttribute(response
,
1737 "job-media-sheets-completed",
1738 IPP_TAG_INTEGER
)) == NULL
)
1739 job_sheets
= ippFindAttribute(response
,
1740 "job-impressions-completed",
1744 fprintf(stderr
, "PAGE: total %d\n",
1745 job_sheets
->values
[0].integer
);
1748 * Stop polling if the job is finished or pending-held...
1751 if (job_state
->values
[0].integer
> IPP_JOB_STOPPED
)
1753 ippDelete(response
);
1757 else if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1758 ipp_status
!= IPP_NOT_POSSIBLE
&&
1759 ipp_status
!= IPP_PRINTER_BUSY
)
1762 * If the printer does not return a job-state attribute, it does not
1763 * conform to the IPP specification - break out immediately and fail
1767 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1768 "cups-ipp-missing-job-state");
1769 ipp_status
= IPP_INTERNAL_ERROR
;
1774 ippDelete(response
);
1777 * Wait before polling again...
1782 delay
= _cupsNextDelay(delay
, &prev_delay
);
1787 * Cancel the job as needed...
1790 if (job_canceled
&& job_id
)
1791 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1794 * Check the printer state and report it if necessary...
1797 check_printer_state(http
, uri
, resource
, argv
[2], version
);
1800 * Collect the final page count as needed...
1803 if (have_supplies
&&
1804 !backendSNMPSupplies(snmp_fd
, http
->hostaddr
, &page_count
, NULL
) &&
1805 page_count
> start_count
)
1806 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
1810 * See if we used Kerberos at all...
1814 auth_info_required
= "negotiate";
1815 #endif /* HAVE_GSSAPI */
1823 cupsFreeOptions(num_options
, options
);
1824 _ppdCacheDestroy(pc
);
1828 ippDelete(supported
);
1831 * Remove the temporary file(s) if necessary...
1835 unlink(tmpfilename
);
1840 for (i
= 0; i
< num_files
; i
++)
1843 #endif /* HAVE_LIBZ */
1846 * Return the queue status...
1849 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
1850 ipp_status
== IPP_AUTHENTICATION_CANCELED
||
1851 ipp_status
<= IPP_OK_CONFLICT
)
1852 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
1854 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
1855 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1856 return (CUPS_BACKEND_AUTH_REQUIRED
);
1857 else if (ipp_status
== IPP_INTERNAL_ERROR
)
1858 return (CUPS_BACKEND_STOP
);
1859 else if (ipp_status
== IPP_DOCUMENT_FORMAT
||
1860 ipp_status
== IPP_CONFLICT
)
1861 return (CUPS_BACKEND_FAILED
);
1862 else if (ipp_status
== IPP_REQUEST_VALUE
)
1864 _cupsLangPrintFilter(stderr
, "ERROR", _("Print job too large."));
1865 return (CUPS_BACKEND_CANCEL
);
1867 else if (ipp_status
> IPP_OK_CONFLICT
&& ipp_status
!= IPP_ERROR_JOB_CANCELED
)
1868 return (CUPS_BACKEND_RETRY_CURRENT
);
1871 _cupsLangPrintFilter(stderr
, "INFO", _("Ready to print."));
1872 return (CUPS_BACKEND_OK
);
1878 * 'cancel_job()' - Cancel a print job.
1882 cancel_job(http_t
*http
, /* I - HTTP connection */
1883 const char *uri
, /* I - printer-uri */
1884 int id
, /* I - job-id */
1885 const char *resource
, /* I - Resource path */
1886 const char *user
, /* I - requesting-user-name */
1887 int version
) /* I - IPP version */
1889 ipp_t
*request
; /* Cancel-Job request */
1892 _cupsLangPrintFilter(stderr
, "INFO", _("Canceling print job."));
1894 request
= ippNewRequest(IPP_CANCEL_JOB
);
1895 request
->request
.op
.version
[0] = version
/ 10;
1896 request
->request
.op
.version
[1] = version
% 10;
1898 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1900 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1902 if (user
&& user
[0])
1903 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1904 "requesting-user-name", NULL
, user
);
1910 ippDelete(cupsDoRequest(http
, request
, resource
));
1912 if (cupsLastError() > IPP_OK_CONFLICT
)
1913 _cupsLangPrintFilter(stderr
, "ERROR", _("Unable to cancel print job."));
1918 * 'check_printer_state()' - Check the printer state.
1921 static ipp_pstate_t
/* O - Current printer-state */
1922 check_printer_state(
1923 http_t
*http
, /* I - HTTP connection */
1924 const char *uri
, /* I - Printer URI */
1925 const char *resource
, /* I - Resource path */
1926 const char *user
, /* I - Username, if any */
1927 int version
) /* I - IPP version */
1929 ipp_t
*request
, /* IPP request */
1930 *response
; /* IPP response */
1931 ipp_attribute_t
*attr
; /* Attribute in response */
1932 ipp_pstate_t printer_state
= IPP_PRINTER_STOPPED
;
1933 /* Current printer-state */
1937 * Send a Get-Printer-Attributes request and log the results...
1940 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1941 request
->request
.op
.version
[0] = version
/ 10;
1942 request
->request
.op
.version
[1] = version
% 10;
1944 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1947 if (user
&& user
[0])
1948 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1949 "requesting-user-name", NULL
, user
);
1951 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1952 "requested-attributes",
1953 (int)(sizeof(pattrs
) / sizeof(pattrs
[0])), NULL
, pattrs
);
1955 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
1957 report_printer_state(response
);
1959 if ((attr
= ippFindAttribute(response
, "printer-state",
1960 IPP_TAG_ENUM
)) != NULL
)
1961 printer_state
= (ipp_pstate_t
)attr
->values
[0].integer
;
1963 ippDelete(response
);
1966 fprintf(stderr
, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
1967 ippErrorString(cupsLastError()), cupsLastErrorString());
1970 * Return the printer-state value...
1973 return (printer_state
);
1979 * 'compress_files()' - Compress print files.
1983 compress_files(int num_files
, /* I - Number of files */
1984 char **files
) /* I - Files */
1986 int i
, /* Looping var */
1987 fd
; /* Temporary file descriptor */
1988 ssize_t bytes
; /* Bytes read/written */
1989 size_t total
; /* Total bytes read */
1990 cups_file_t
*in
, /* Input file */
1991 *out
; /* Output file */
1992 struct stat outinfo
; /* Output file information */
1993 char filename
[1024], /* Temporary filename */
1994 buffer
[32768]; /* Copy buffer */
1997 fprintf(stderr
, "DEBUG: Compressing %d job files...\n", num_files
);
1998 for (i
= 0; i
< num_files
; i
++)
2000 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
2002 _cupsLangPrintError("ERROR", _("Unable to create compressed print file"));
2003 exit(CUPS_BACKEND_FAILED
);
2006 if ((out
= cupsFileOpenFd(fd
, "w9")) == NULL
)
2008 _cupsLangPrintError("ERROR", _("Unable to open compressed print file"));
2009 exit(CUPS_BACKEND_FAILED
);
2012 if ((in
= cupsFileOpen(files
[i
], "r")) == NULL
)
2014 _cupsLangPrintError("ERROR", _("Unable to open print file"));
2016 exit(CUPS_BACKEND_FAILED
);
2020 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
2021 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
2023 _cupsLangPrintError("ERROR",
2024 _("Unable to generate compressed print file"));
2027 exit(CUPS_BACKEND_FAILED
);
2035 files
[i
] = strdup(filename
);
2037 if (!stat(filename
, &outinfo
))
2039 "DEBUG: File %d compressed to %.1f%% of original size, "
2040 CUPS_LLFMT
" bytes...\n",
2041 i
+ 1, 100.0 * outinfo
.st_size
/ total
,
2042 CUPS_LLCAST outinfo
.st_size
);
2045 #endif /* HAVE_LIBZ */
2049 * 'monitor_printer()' - Monitor the printer state.
2052 static void * /* O - Thread exit code */
2054 _cups_monitor_t
*monitor
) /* I - Monitoring data */
2056 http_t
*http
; /* Connection to printer */
2057 ipp_t
*request
, /* IPP request */
2058 *response
; /* IPP response */
2059 ipp_attribute_t
*attr
; /* Attribute in response */
2060 int delay
, /* Current delay */
2061 prev_delay
; /* Previous delay */
2062 ipp_op_t job_op
; /* Operation to use */
2063 int job_id
; /* Job ID */
2064 const char *job_name
; /* Job name */
2065 ipp_jstate_t job_state
; /* Job state */
2066 const char *job_user
; /* Job originating user name */
2070 * Make a copy of the printer connection...
2073 http
= _httpCreate(monitor
->hostname
, monitor
->port
, NULL
, monitor
->encryption
,
2075 httpSetTimeout(http
, 30.0, timeout_cb
, NULL
);
2076 cupsSetPasswordCB(password_cb
);
2079 * Loop until the job is canceled, aborted, or completed.
2082 delay
= _cupsNextDelay(0, &prev_delay
);
2084 while (monitor
->job_state
< IPP_JOB_CANCELED
&& !job_canceled
)
2087 * Reconnect to the printer...
2090 if (!httpReconnect(http
))
2093 * Connected, so check on the printer state...
2096 monitor
->printer_state
= check_printer_state(http
, monitor
->uri
,
2102 * Check the status of the job itself...
2105 job_op
= monitor
->job_id
> 0 ? IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
;
2106 request
= ippNewRequest(job_op
);
2107 request
->request
.op
.version
[0] = monitor
->version
/ 10;
2108 request
->request
.op
.version
[1] = monitor
->version
% 10;
2110 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2111 NULL
, monitor
->uri
);
2112 if (job_op
== IPP_GET_JOB_ATTRIBUTES
)
2113 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
2116 if (monitor
->user
&& monitor
->user
[0])
2117 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2118 "requesting-user-name", NULL
, monitor
->user
);
2120 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2121 "requested-attributes",
2122 (int)(sizeof(jattrs
) / sizeof(jattrs
[0])), NULL
, jattrs
);
2128 response
= cupsDoRequest(http
, request
, monitor
->resource
);
2130 fprintf(stderr
, "DEBUG: %s: %s (%s)\n", ippOpString(job_op
),
2131 ippErrorString(cupsLastError()), cupsLastErrorString());
2133 if (job_op
== IPP_GET_JOB_ATTRIBUTES
)
2135 if ((attr
= ippFindAttribute(response
, "job-state",
2136 IPP_TAG_ENUM
)) != NULL
)
2137 monitor
->job_state
= (ipp_jstate_t
)attr
->values
[0].integer
;
2139 monitor
->job_state
= IPP_JOB_COMPLETED
;
2143 for (attr
= response
->attrs
; attr
; attr
= attr
->next
)
2147 job_state
= IPP_JOB_PENDING
;
2150 while (attr
&& attr
->group_tag
!= IPP_TAG_JOB
)
2156 while (attr
&& attr
->group_tag
== IPP_TAG_JOB
)
2158 if (!strcmp(attr
->name
, "job-id") &&
2159 attr
->value_tag
== IPP_TAG_INTEGER
)
2160 job_id
= attr
->values
[0].integer
;
2161 else if (!strcmp(attr
->name
, "job-name") &&
2162 (attr
->value_tag
== IPP_TAG_NAME
||
2163 attr
->value_tag
== IPP_TAG_NAMELANG
))
2164 job_name
= attr
->values
[0].string
.text
;
2165 else if (!strcmp(attr
->name
, "job-state") &&
2166 attr
->value_tag
== IPP_TAG_ENUM
)
2167 job_state
= attr
->values
[0].integer
;
2168 else if (!strcmp(attr
->name
, "job-originating-user-name") &&
2169 (attr
->value_tag
== IPP_TAG_NAME
||
2170 attr
->value_tag
== IPP_TAG_NAMELANG
))
2171 job_user
= attr
->values
[0].string
.text
;
2176 if (job_id
> 0 && job_name
&& !strcmp(job_name
, monitor
->job_name
) &&
2177 job_user
&& monitor
->user
&& !strcmp(job_user
, monitor
->user
))
2179 monitor
->job_id
= job_id
;
2180 monitor
->job_state
= job_state
;
2189 ippDelete(response
);
2192 * Disconnect from the printer - we'll reconnect on the next poll...
2195 _httpDisconnect(http
);
2199 * Sleep for N seconds...
2204 delay
= _cupsNextDelay(delay
, &prev_delay
);
2208 * Cancel the job if necessary...
2211 if (job_canceled
&& monitor
->job_id
> 0)
2212 if (!httpReconnect(http
))
2213 cancel_job(http
, monitor
->uri
, monitor
->job_id
, monitor
->resource
,
2214 monitor
->user
, monitor
->version
);
2217 * Cleanup and return...
2227 * 'new_request()' - Create a new print creation or validation request.
2230 static ipp_t
* /* O - Request data */
2232 ipp_op_t op
, /* I - IPP operation code */
2233 int version
, /* I - IPP version number */
2234 const char *uri
, /* I - printer-uri value */
2235 const char *user
, /* I - requesting-user-name value */
2236 const char *title
, /* I - job-name value */
2237 int num_options
, /* I - Number of options to send */
2238 cups_option_t
*options
, /* I - Options to send */
2239 const char *compression
, /* I - compression value or NULL */
2240 int copies
, /* I - copies value or 0 */
2241 const char *format
, /* I - document-format value or NULL */
2242 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2243 ipp_attribute_t
*media_col_sup
, /* I - media-col-supported values */
2244 ipp_attribute_t
*doc_handling_sup
) /* I - multiple-document-handling-supported values */
2246 int i
; /* Looping var */
2247 ipp_t
*request
; /* Request data */
2248 const char *keyword
; /* PWG keyword */
2249 _pwg_size_t
*size
; /* PWG media size */
2250 ipp_t
*media_col
, /* media-col value */
2251 *media_size
; /* media-size value */
2252 const char *media_source
, /* media-source value */
2253 *media_type
, /* media-type value */
2254 *collate_str
; /* multiple-document-handling value */
2258 * Create the IPP request...
2261 request
= ippNewRequest(op
);
2262 request
->request
.op
.version
[0] = version
/ 10;
2263 request
->request
.op
.version
[1] = version
% 10;
2265 fprintf(stderr
, "DEBUG: %s IPP/%d.%d\n",
2266 ippOpString(request
->request
.op
.operation_id
),
2267 request
->request
.op
.version
[0],
2268 request
->request
.op
.version
[1]);
2271 * Add standard attributes...
2274 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2276 fprintf(stderr
, "DEBUG: printer-uri=\"%s\"\n", uri
);
2280 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2281 "requesting-user-name", NULL
, user
);
2282 fprintf(stderr
, "DEBUG: requesting-user-name=\"%s\"\n", user
);
2285 if (title
&& *title
)
2287 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
2289 fprintf(stderr
, "DEBUG: job-name=\"%s\"\n", title
);
2294 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
2295 "document-format", NULL
, format
);
2296 fprintf(stderr
, "DEBUG: document-format=\"%s\"\n", format
);
2302 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2303 "compression", NULL
, compression
);
2304 fprintf(stderr
, "DEBUG: compression=\"%s\"\n", compression
);
2306 #endif /* HAVE_LIBZ */
2309 * Handle options on the command-line...
2312 if (num_options
> 0)
2316 int num_finishings
= 0, /* Number of finishing values */
2317 finishings
[10]; /* Finishing enum values */
2320 * Send standard IPP attributes...
2323 if ((keyword
= cupsGetOption("PageSize", num_options
, options
)) == NULL
)
2324 keyword
= cupsGetOption("media", num_options
, options
);
2326 if ((size
= _ppdCacheGetSize(pc
, keyword
)) != NULL
)
2329 * Add a media-col value...
2332 media_size
= ippNew();
2333 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2334 "x-dimension", size
->width
);
2335 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2336 "y-dimension", size
->length
);
2338 media_col
= ippNew();
2339 ippAddCollection(media_col
, IPP_TAG_ZERO
, "media-size", media_size
);
2341 media_source
= _ppdCacheGetSource(pc
, cupsGetOption("InputSlot",
2344 media_type
= _ppdCacheGetType(pc
, cupsGetOption("MediaType",
2348 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
2350 if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2351 "media-left-margin"))
2352 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2353 "media-left-margin", size
->left
);
2354 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2355 "media-bottom-margin"))
2356 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2357 "media-bottom-margin", size
->bottom
);
2358 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2359 "media-right-margin"))
2360 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2361 "media-right-margin", size
->right
);
2362 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2363 "media-top-margin"))
2364 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2365 "media-top-margin", size
->top
);
2366 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2367 "media-source") && media_source
)
2368 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
2369 "media-source", NULL
, media_source
);
2370 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2371 "media-type") && media_type
)
2372 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
2373 "media-type", NULL
, media_type
);
2376 ippAddCollection(request
, IPP_TAG_JOB
, "media-col", media_col
);
2379 if ((keyword
= cupsGetOption("output-bin", num_options
,
2381 keyword
= _ppdCacheGetBin(pc
, cupsGetOption("OutputBin", num_options
,
2385 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-bin",
2388 if ((keyword
= cupsGetOption("output-mode", num_options
,
2390 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
2392 else if ((keyword
= cupsGetOption("ColorModel", num_options
,
2395 if (!_cups_strcasecmp(keyword
, "Gray"))
2396 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
2397 NULL
, "monochrome");
2399 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
2403 if ((keyword
= cupsGetOption("print-quality", num_options
,
2405 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2407 else if ((keyword
= cupsGetOption("cupsPrintQuality", num_options
,
2410 if (!_cups_strcasecmp(keyword
, "draft"))
2411 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2413 else if (!_cups_strcasecmp(keyword
, "normal"))
2414 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2415 IPP_QUALITY_NORMAL
);
2416 else if (!_cups_strcasecmp(keyword
, "high"))
2417 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2421 if ((keyword
= cupsGetOption("sides", num_options
, options
)) != NULL
)
2422 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2424 else if (pc
->sides_option
&&
2425 (keyword
= cupsGetOption(pc
->sides_option
, num_options
,
2428 if (!_cups_strcasecmp(keyword
, pc
->sides_1sided
))
2429 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2431 else if (!_cups_strcasecmp(keyword
, pc
->sides_2sided_long
))
2432 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2433 NULL
, "two-sided-long-edge");
2434 if (!_cups_strcasecmp(keyword
, pc
->sides_2sided_short
))
2435 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2436 NULL
, "two-sided-short-edge");
2439 if (doc_handling_sup
&&
2440 (!format
|| _cups_strncasecmp(format
, "image/", 6)) &&
2441 (keyword
= cupsGetOption("collate", num_options
, options
)) != NULL
)
2443 if (!_cups_strcasecmp(keyword
, "true"))
2444 collate_str
= "separate-documents-collated-copies";
2446 collate_str
= "separate-documents-uncollated-copies";
2448 for (i
= 0; i
< doc_handling_sup
->num_values
; i
++)
2449 if (!strcmp(doc_handling_sup
->values
[i
].string
.text
, collate_str
))
2451 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2452 "multiple-document-handling", NULL
, collate_str
);
2458 * Map finishing options...
2461 num_finishings
= _ppdCacheGetFinishingValues(pc
, num_options
, options
,
2462 (int)(sizeof(finishings
) /
2463 sizeof(finishings
[0])),
2465 if (num_finishings
> 0)
2466 ippAddIntegers(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "finishings",
2467 num_finishings
, finishings
);
2470 * Map FaxOut options...
2473 if ((keyword
= cupsGetOption("phone", num_options
, options
)) != NULL
)
2475 ipp_t
*destination
; /* destination collection */
2476 char tel_uri
[1024]; /* tel: URI */
2478 destination
= ippNew();
2480 httpAssembleURI(HTTP_URI_CODING_ALL
, tel_uri
, sizeof(tel_uri
), "tel",
2481 NULL
, NULL
, 0, keyword
);
2482 ippAddString(destination
, IPP_TAG_JOB
, IPP_TAG_URI
, "destination-uri",
2485 if ((keyword
= cupsGetOption("faxPrefix", num_options
,
2486 options
)) != NULL
&& *keyword
)
2487 ippAddString(destination
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
2488 "pre-dial-string", NULL
, keyword
);
2490 ippAddCollection(request
, IPP_TAG_JOB
, "destination-uris", destination
);
2491 ippDelete(destination
);
2497 * When talking to another CUPS server, send all options...
2500 cupsEncodeOptions(request
, num_options
, options
);
2504 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies", copies
);
2512 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
2515 static const char * /* O - Password */
2516 password_cb(const char *prompt
) /* I - Prompt (not used) */
2521 * Remember that we need to authenticate...
2524 auth_info_required
= "username,password";
2526 if (password
&& *password
&& password_tries
< 3)
2535 * Give up after 3 tries or if we don't have a password to begin with...
2544 * 'report_attr()' - Report an IPP attribute value.
2548 report_attr(ipp_attribute_t
*attr
) /* I - Attribute */
2550 int i
; /* Looping var */
2551 char value
[1024], /* Value string */
2552 *valptr
, /* Pointer into value string */
2553 *attrptr
; /* Pointer into attribute value */
2554 const char *cached
; /* Cached attribute */
2558 * Convert the attribute values into quoted strings...
2561 for (i
= 0, valptr
= value
;
2562 i
< attr
->num_values
&& valptr
< (value
+ sizeof(value
) - 10);
2568 switch (attr
->value_tag
)
2570 case IPP_TAG_INTEGER
:
2572 snprintf(valptr
, sizeof(value
) - (valptr
- value
), "%d",
2573 attr
->values
[i
].integer
);
2574 valptr
+= strlen(valptr
);
2579 case IPP_TAG_KEYWORD
:
2582 for (attrptr
= attr
->values
[i
].string
.text
;
2583 *attrptr
&& valptr
< (value
+ sizeof(value
) - 10);
2586 if (*attrptr
== '\\' || *attrptr
== '\"' || *attrptr
== '\'')
2593 *valptr
++ = *attrptr
;
2601 * Unsupported value type...
2610 _cupsMutexLock(&report_mutex
);
2612 if ((cached
= cupsGetOption(attr
->name
, num_attr_cache
,
2613 attr_cache
)) == NULL
|| strcmp(cached
, value
))
2616 * Tell the scheduler about the new values...
2619 num_attr_cache
= cupsAddOption(attr
->name
, value
, num_attr_cache
,
2621 fprintf(stderr
, "ATTR: %s=%s\n", attr
->name
, value
);
2624 _cupsMutexUnlock(&report_mutex
);
2629 * 'report_printer_state()' - Report the printer state.
2633 report_printer_state(ipp_t
*ipp
) /* I - IPP response */
2635 ipp_attribute_t
*pa
, /* printer-alert */
2636 *pam
, /* printer-alert-message */
2637 *psm
, /* printer-state-message */
2638 *reasons
, /* printer-state-reasons */
2639 *marker
; /* marker-* attributes */
2640 char value
[1024], /* State/message string */
2641 *valptr
; /* Pointer into string */
2642 static int ipp_supplies
= -1;
2643 /* Report supply levels? */
2647 * Report alerts and messages...
2650 if ((pa
= ippFindAttribute(ipp
, "printer-alert", IPP_TAG_TEXT
)) != NULL
)
2653 if ((pam
= ippFindAttribute(ipp
, "printer-alert-message",
2654 IPP_TAG_TEXT
)) != NULL
)
2657 if ((psm
= ippFindAttribute(ipp
, "printer-state-message",
2658 IPP_TAG_TEXT
)) != NULL
)
2660 char *ptr
; /* Pointer into message */
2663 strlcpy(value
, "INFO: ", sizeof(value
));
2664 for (ptr
= psm
->values
[0].string
.text
, valptr
= value
+ 6;
2665 *ptr
&& valptr
< (value
+ sizeof(value
) - 6);
2668 if (*ptr
< ' ' && *ptr
> 0 && *ptr
!= '\t')
2671 * Substitute "<XX>" for the control character; sprintf is safe because
2672 * we always leave 6 chars free at the end...
2675 sprintf(valptr
, "<%02X>", *ptr
);
2685 fputs(value
, stderr
);
2689 * Now report printer-state-reasons, filtering out some of the reasons we never
2693 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
2694 IPP_TAG_KEYWORD
)) == NULL
)
2697 update_reasons(reasons
, NULL
);
2700 * Relay the current marker-* attribute values...
2703 if (ipp_supplies
< 0)
2705 ppd_file_t
*ppd
; /* PPD file */
2706 ppd_attr_t
*ppdattr
; /* Attribute in PPD file */
2708 if ((ppd
= ppdOpenFile(getenv("PPD"))) != NULL
&&
2709 (ppdattr
= ppdFindAttr(ppd
, "cupsIPPSupplies", NULL
)) != NULL
&&
2710 ppdattr
->value
&& _cups_strcasecmp(ppdattr
->value
, "true"))
2718 if (ipp_supplies
> 0)
2720 if ((marker
= ippFindAttribute(ipp
, "marker-colors", IPP_TAG_NAME
)) != NULL
)
2721 report_attr(marker
);
2722 if ((marker
= ippFindAttribute(ipp
, "marker-high-levels",
2723 IPP_TAG_INTEGER
)) != NULL
)
2724 report_attr(marker
);
2725 if ((marker
= ippFindAttribute(ipp
, "marker-levels",
2726 IPP_TAG_INTEGER
)) != NULL
)
2727 report_attr(marker
);
2728 if ((marker
= ippFindAttribute(ipp
, "marker-low-levels",
2729 IPP_TAG_INTEGER
)) != NULL
)
2730 report_attr(marker
);
2731 if ((marker
= ippFindAttribute(ipp
, "marker-message",
2732 IPP_TAG_TEXT
)) != NULL
)
2733 report_attr(marker
);
2734 if ((marker
= ippFindAttribute(ipp
, "marker-names", IPP_TAG_NAME
)) != NULL
)
2735 report_attr(marker
);
2736 if ((marker
= ippFindAttribute(ipp
, "marker-types",
2737 IPP_TAG_KEYWORD
)) != NULL
)
2738 report_attr(marker
);
2743 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
2745 * 'run_as_user()' - Run the IPP backend as the printing user.
2747 * This function uses an XPC-based user agent to run the backend as the printing
2748 * user. We need to do this in order to have access to the user's Kerberos
2752 static int /* O - Exit status */
2753 run_as_user(int argc
, /* I - Number of command-line args */
2754 char *argv
[], /* I - Command-line arguments */
2755 uid_t uid
, /* I - User ID */
2756 const char *device_uri
, /* I - Device URI */
2757 int fd
) /* I - File to print */
2759 const char *auth_negotiate
;/* AUTH_NEGOTIATE env var */
2760 xpc_connection_t conn
; /* Connection to XPC service */
2761 xpc_object_t request
; /* Request message dictionary */
2762 __block xpc_object_t response
; /* Response message dictionary */
2763 dispatch_semaphore_t sem
; /* Semaphore for waiting for response */
2764 int status
= CUPS_BACKEND_FAILED
;
2765 /* Status of request */
2768 fprintf(stderr
, "DEBUG: Running IPP backend as UID %d.\n", (int)uid
);
2771 * Connect to the user agent for the specified UID...
2774 conn
= xpc_connection_create_mach_service(kPMPrintUIToolAgent
,
2775 dispatch_get_global_queue(0, 0), 0);
2778 _cupsLangPrintFilter(stderr
, "ERROR",
2779 _("Unable to start backend process."));
2780 fputs("DEBUG: Unable to create connection to agent.\n", stderr
);
2784 xpc_connection_set_event_handler(conn
,
2785 ^(xpc_object_t event
)
2787 xpc_type_t messageType
= xpc_get_type(event
);
2789 if (messageType
== XPC_TYPE_ERROR
)
2791 if (event
== XPC_ERROR_CONNECTION_INTERRUPTED
)
2792 fprintf(stderr
, "DEBUG: Interrupted connection to service %s.\n",
2793 xpc_connection_get_name(conn
));
2794 else if (event
== XPC_ERROR_CONNECTION_INVALID
)
2795 fprintf(stderr
, "DEBUG: Connection invalid for service %s.\n",
2796 xpc_connection_get_name(conn
));
2798 fprintf(stderr
, "DEBUG: Unxpected error for service %s: %s\n",
2799 xpc_connection_get_name(conn
),
2800 xpc_dictionary_get_string(event
, XPC_ERROR_KEY_DESCRIPTION
));
2803 xpc_connection_set_target_uid(conn
, uid
);
2804 xpc_connection_resume(conn
);
2807 * Try starting the backend...
2810 request
= xpc_dictionary_create(NULL
, NULL
, 0);
2811 xpc_dictionary_set_int64(request
, "command", kPMStartJob
);
2812 xpc_dictionary_set_string(request
, "device-uri", device_uri
);
2813 xpc_dictionary_set_string(request
, "job-id", argv
[1]);
2814 xpc_dictionary_set_string(request
, "user", argv
[2]);
2815 xpc_dictionary_set_string(request
, "title", argv
[3]);
2816 xpc_dictionary_set_string(request
, "copies", argv
[4]);
2817 xpc_dictionary_set_string(request
, "options", argv
[5]);
2818 xpc_dictionary_set_string(request
, "auth-info-required",
2819 getenv("AUTH_INFO_REQUIRED"));
2820 if ((auth_negotiate
= getenv("AUTH_NEGOTIATE")) != NULL
)
2821 xpc_dictionary_set_string(request
, "auth-negotiate", auth_negotiate
);
2822 xpc_dictionary_set_fd(request
, "stdin", fd
);
2823 xpc_dictionary_set_fd(request
, "stderr", 2);
2824 xpc_dictionary_set_fd(request
, "side-channel", CUPS_SC_FD
);
2826 sem
= dispatch_semaphore_create(0);
2829 xpc_connection_send_message_with_reply(conn
, request
,
2830 dispatch_get_global_queue(0,0),
2831 ^(xpc_object_t reply
)
2833 /* Save the response and wake up */
2834 if (xpc_get_type(reply
)
2835 == XPC_TYPE_DICTIONARY
)
2836 response
= xpc_retain(reply
);
2838 dispatch_semaphore_signal(sem
);
2841 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
2842 xpc_release(request
);
2843 dispatch_release(sem
);
2847 child_pid
= xpc_dictionary_get_int64(response
, "child-pid");
2849 xpc_release(response
);
2852 fprintf(stderr
, "DEBUG: Child PID=%d.\n", child_pid
);
2855 _cupsLangPrintFilter(stderr
, "ERROR",
2856 _("Unable to start backend process."));
2857 fputs("DEBUG: No child PID.\n", stderr
);
2863 _cupsLangPrintFilter(stderr
, "ERROR",
2864 _("Unable to start backend process."));
2865 fputs("DEBUG: No reply from agent.\n", stderr
);
2870 * Then wait for the backend to finish...
2873 request
= xpc_dictionary_create(NULL
, NULL
, 0);
2874 xpc_dictionary_set_int64(request
, "command", kPMWaitForJob
);
2875 xpc_dictionary_set_fd(request
, "stderr", 2);
2877 sem
= dispatch_semaphore_create(0);
2880 xpc_connection_send_message_with_reply(conn
, request
,
2881 dispatch_get_global_queue(0,0),
2882 ^(xpc_object_t reply
)
2884 /* Save the response and wake up */
2885 if (xpc_get_type(reply
)
2886 == XPC_TYPE_DICTIONARY
)
2887 response
= xpc_retain(reply
);
2889 dispatch_semaphore_signal(sem
);
2892 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
2893 xpc_release(request
);
2894 dispatch_release(sem
);
2898 status
= xpc_dictionary_get_int64(response
, "status");
2900 if (status
== SIGTERM
|| status
== SIGKILL
|| status
== SIGPIPE
)
2902 fprintf(stderr
, "DEBUG: Child terminated on signal %d.\n", status
);
2903 status
= CUPS_BACKEND_FAILED
;
2905 else if (WIFSIGNALED(status
))
2907 fprintf(stderr
, "DEBUG: Child crashed on signal %d.\n", status
);
2908 status
= CUPS_BACKEND_STOP
;
2910 else if (WIFEXITED(status
))
2912 status
= WEXITSTATUS(status
);
2913 fprintf(stderr
, "DEBUG: Child exited with status %d.\n", status
);
2916 xpc_release(response
);
2919 _cupsLangPrintFilter(stderr
, "ERROR",
2920 _("Unable to get backend exit status."));
2926 xpc_connection_suspend(conn
);
2927 xpc_connection_cancel(conn
);
2933 #endif /* HAVE_GSSAPI && HAVE_XPC */
2937 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
2941 sigterm_handler(int sig
) /* I - Signal */
2943 (void)sig
; /* remove compiler warnings... */
2945 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
2948 kill(child_pid
, sig
);
2951 #endif /* HAVE_GSSAPI && HAVE_XPC */
2956 * Flag that the job should be canceled...
2964 * The scheduler already tried to cancel us once, now just terminate
2965 * after removing our temp file!
2969 unlink(tmpfilename
);
2976 * 'timeout_cb()' - Handle HTTP timeouts.
2979 static int /* O - 1 to continue, 0 to cancel */
2980 timeout_cb(http_t
*http
, /* I - Connection to server (unused) */
2981 void *user_data
) /* I - User data (unused) */
2986 return (!job_canceled
);
2991 * 'update_reasons()' - Update the printer-state-reasons values.
2995 update_reasons(ipp_attribute_t
*attr
, /* I - printer-state-reasons or NULL */
2996 const char *s
) /* I - STATE: string or NULL */
2998 char op
; /* Add (+), remove (-), replace (\0) */
2999 cups_array_t
*new_reasons
; /* New reasons array */
3000 char *reason
, /* Current reason */
3001 add
[2048], /* Reasons added string */
3002 *addptr
, /* Pointer into add string */
3003 rem
[2048], /* Reasons removed string */
3004 *remptr
; /* Pointer into remove string */
3005 const char *addprefix
, /* Current add string prefix */
3006 *remprefix
; /* Current remove string prefix */
3009 fprintf(stderr
, "DEBUG: update_reasons(attr=%d(%s%s), s=\"%s\")\n",
3010 attr
? attr
->num_values
: 0, attr
? attr
->values
[0].string
.text
: "",
3011 attr
&& attr
->num_values
> 1 ? ",..." : "", s
? s
: "(null)");
3014 * Create an array of new reason keyword strings...
3019 int i
; /* Looping var */
3021 new_reasons
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
3024 for (i
= 0; i
< attr
->num_values
; i
++)
3026 reason
= attr
->values
[i
].string
.text
;
3028 if (strcmp(reason
, "none") &&
3029 strcmp(reason
, "none-report") &&
3030 strcmp(reason
, "paused") &&
3031 strncmp(reason
, "spool-area-full", 15) &&
3032 strcmp(reason
, "com.apple.print.recoverable-warning") &&
3033 strncmp(reason
, "cups-", 5))
3034 cupsArrayAdd(new_reasons
, reason
);
3039 if (*s
== '+' || *s
== '-')
3044 new_reasons
= _cupsArrayNewStrings(s
);
3050 * Compute the changes...
3054 addprefix
= "STATE: +";
3057 remprefix
= "STATE: -";
3060 fprintf(stderr
, "DEBUG2: op='%c', new_reasons=%d, state_reasons=%d\n",
3061 op
? op
: ' ', cupsArrayCount(new_reasons
),
3062 cupsArrayCount(state_reasons
));
3064 _cupsMutexLock(&report_mutex
);
3072 for (reason
= (char *)cupsArrayFirst(new_reasons
);
3074 reason
= (char *)cupsArrayNext(new_reasons
))
3076 if (!cupsArrayFind(state_reasons
, reason
))
3078 if (!strncmp(reason
, "cups-remote-", 12))
3081 * If we are setting cups-remote-xxx, remove all other cups-remote-xxx
3085 char *temp
; /* Current reason in state_reasons */
3087 cupsArraySave(state_reasons
);
3089 for (temp
= (char *)cupsArrayFirst(state_reasons
);
3091 temp
= (char *)cupsArrayNext(state_reasons
))
3092 if (!strncmp(temp
, "cups-remote-", 12))
3094 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
3096 remptr
+= strlen(remptr
);
3099 cupsArrayRemove(state_reasons
, temp
);
3103 cupsArrayRestore(state_reasons
);
3106 cupsArrayAdd(state_reasons
, reason
);
3108 snprintf(addptr
, sizeof(add
) - (addptr
- add
), "%s%s", addprefix
,
3110 addptr
+= strlen(addptr
);
3121 for (reason
= (char *)cupsArrayFirst(new_reasons
);
3123 reason
= (char *)cupsArrayNext(new_reasons
))
3125 if (cupsArrayFind(state_reasons
, reason
))
3127 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
3129 remptr
+= strlen(remptr
);
3132 cupsArrayRemove(state_reasons
, reason
);
3139 * Replace reasons...
3142 for (reason
= (char *)cupsArrayFirst(state_reasons
);
3144 reason
= (char *)cupsArrayNext(state_reasons
))
3146 if (strncmp(reason
, "cups-", 5) && !cupsArrayFind(new_reasons
, reason
))
3148 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
3150 remptr
+= strlen(remptr
);
3153 cupsArrayRemove(state_reasons
, reason
);
3157 for (reason
= (char *)cupsArrayFirst(new_reasons
);
3159 reason
= (char *)cupsArrayNext(new_reasons
))
3161 if (!cupsArrayFind(state_reasons
, reason
))
3163 cupsArrayAdd(state_reasons
, reason
);
3165 snprintf(addptr
, sizeof(add
) - (addptr
- add
), "%s%s", addprefix
,
3167 addptr
+= strlen(addptr
);
3173 _cupsMutexUnlock(&report_mutex
);
3176 * Report changes and return...
3179 if (add
[0] && rem
[0])
3180 fprintf(stderr
, "%s\n%s\n", add
, rem
);
3182 fprintf(stderr
, "%s\n", add
);
3184 fprintf(stderr
, "%s\n", rem
);
3188 * End of "$Id: ipp.c 9759 2011-05-11 03:24:33Z mike $".