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 username
[256] = "",
94 /* Username for device URI */
96 /* Password for device URI */
97 static int password_tries
= 0;
99 static const char * const pattrs
[] = /* Printer attributes we want */
103 "document-format-supported",
105 "marker-high-levels",
111 "media-col-supported",
112 "multiple-document-handling-supported",
113 "operations-supported",
115 "printer-alert-description",
116 "printer-is-accepting-jobs",
118 "printer-state-message",
119 "printer-state-reasons"
121 static const char * const remote_job_states
[] =
122 { /* Remote job state keywords */
123 "+cups-remote-pending",
124 "+cups-remote-pending-held",
125 "+cups-remote-processing",
126 "+cups-remote-stopped",
127 "+cups-remote-canceled",
128 "+cups-remote-aborted",
129 "+cups-remote-completed"
131 static _cups_mutex_t report_mutex
= _CUPS_MUTEX_INITIALIZER
;
132 /* Mutex to control access */
133 static int num_attr_cache
= 0;
134 /* Number of cached attributes */
135 static cups_option_t
*attr_cache
= NULL
;
136 /* Cached attributes */
137 static cups_array_t
*state_reasons
; /* Array of printe-state-reasons keywords */
138 static char tmpfilename
[1024] = "";
139 /* Temporary spool file name */
146 static void cancel_job(http_t
*http
, const char *uri
, int id
,
147 const char *resource
, const char *user
,
149 static ipp_pstate_t
check_printer_state(http_t
*http
, const char *uri
,
150 const char *resource
,
151 const char *user
, int version
);
153 static void compress_files(int num_files
, char **files
);
154 #endif /* HAVE_LIBZ */
155 static void *monitor_printer(_cups_monitor_t
*monitor
);
156 static ipp_t
*new_request(ipp_op_t op
, int version
, const char *uri
,
157 const char *user
, const char *title
,
158 int num_options
, cups_option_t
*options
,
159 const char *compression
, int copies
,
160 const char *format
, _ppd_cache_t
*pc
,
161 ipp_attribute_t
*media_col_sup
,
162 ipp_attribute_t
*doc_handling_sup
);
163 static const char *password_cb(const char *);
164 static void report_attr(ipp_attribute_t
*attr
);
165 static void report_printer_state(ipp_t
*ipp
);
166 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
167 static int run_as_user(int argc
, char *argv
[], uid_t uid
,
168 const char *device_uri
, int fd
);
169 #endif /* HAVE_GSSAPI && HAVE_XPC */
170 static void sigterm_handler(int sig
);
171 static int timeout_cb(http_t
*http
, void *user_data
);
172 static void update_reasons(ipp_attribute_t
*attr
, const char *s
);
176 * 'main()' - Send a file to the printer or server.
180 * printer-uri job-id user title copies options [file]
183 int /* O - Exit status */
184 main(int argc
, /* I - Number of command-line args */
185 char *argv
[]) /* I - Command-line arguments */
187 int i
; /* Looping var */
188 int send_options
; /* Send job options? */
189 int num_options
; /* Number of printer options */
190 cups_option_t
*options
; /* Printer options */
191 const char *device_uri
; /* Device URI */
192 char scheme
[255], /* Scheme in URI */
193 hostname
[1024], /* Hostname */
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_sup
; /* document-format-supported */
234 ipp_attribute_t
*media_col_sup
; /* media-col-supported */
235 ipp_attribute_t
*operations_sup
; /* operations-supported */
236 ipp_attribute_t
*doc_handling_sup
; /* multiple-document-handling-supported */
237 ipp_attribute_t
*printer_state
; /* printer-state attribute */
238 ipp_attribute_t
*printer_accepting
; /* printer-is-accepting-jobs */
239 int create_job
= 0, /* Does printer support Create-Job? */
240 send_document
= 0, /* Does printer support Send-Document? */
241 validate_job
= 0; /* Does printer support Validate-Job? */
242 int copies
, /* Number of copies for job */
243 copies_remaining
; /* Number of copies remaining */
244 const char *content_type
, /* CONTENT_TYPE environment variable */
245 *final_content_type
, /* FINAL_CONTENT_TYPE environment var */
246 *document_format
; /* document-format value */
247 int fd
; /* File descriptor */
248 off_t bytes
= 0; /* Bytes copied */
249 char buffer
[16384]; /* Copy buffer */
250 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
251 struct sigaction action
; /* Actions for POSIX signals */
252 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
253 int version
; /* IPP version */
254 ppd_file_t
*ppd
; /* PPD file */
255 _ppd_cache_t
*pc
; /* PPD cache and mapping data */
256 fd_set input
; /* Input set for select() */
260 * Make sure status messages are not buffered...
263 setbuf(stderr
, NULL
);
266 * Ignore SIGPIPE and catch SIGTERM signals...
270 sigset(SIGPIPE
, SIG_IGN
);
271 sigset(SIGTERM
, sigterm_handler
);
272 #elif defined(HAVE_SIGACTION)
273 memset(&action
, 0, sizeof(action
));
274 action
.sa_handler
= SIG_IGN
;
275 sigaction(SIGPIPE
, &action
, NULL
);
277 sigemptyset(&action
.sa_mask
);
278 sigaddset(&action
.sa_mask
, SIGTERM
);
279 action
.sa_handler
= sigterm_handler
;
280 sigaction(SIGTERM
, &action
, NULL
);
282 signal(SIGPIPE
, SIG_IGN
);
283 signal(SIGTERM
, sigterm_handler
);
284 #endif /* HAVE_SIGSET */
287 * Check command-line...
294 if ((s
= strrchr(argv
[0], '/')) != NULL
)
299 printf("network %s \"Unknown\" \"%s (%s)\"\n",
300 s
, _cupsLangString(cupsLangDefault(),
301 _("Internet Printing Protocol")), s
);
302 return (CUPS_BACKEND_OK
);
306 _cupsLangPrintf(stderr
,
307 _("Usage: %s job-id user title copies options [file]"),
309 return (CUPS_BACKEND_STOP
);
313 * Get the device URI...
316 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
318 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
321 if (getenv("CLASS") != NULL
)
322 return (CUPS_BACKEND_FAILED
);
325 if ((auth_info_required
= getenv("AUTH_INFO_REQUIRED")) == NULL
)
326 auth_info_required
= "none";
328 state_reasons
= _cupsArrayNewStrings(getenv("PRINTER_STATE_REASONS"));
332 * For Kerberos, become the printing user (if we can) to get the credentials
336 if (!getuid() && (value
= getenv("AUTH_UID")) != NULL
)
338 uid_t uid
= (uid_t
)atoi(value
);
345 return (run_as_user(argc
, argv
, uid
, device_uri
, 0));
348 int status
= 0; /* Exit status */
350 for (i
= 6; i
< argc
&& !status
&& !job_canceled
; i
++)
352 if ((fd
= open(argv
[i
], O_RDONLY
)) >= 0)
354 status
= run_as_user(argc
, argv
, uid
, device_uri
, fd
);
359 _cupsLangPrintError("ERROR", _("Unable to open print file"));
360 status
= CUPS_BACKEND_FAILED
;
368 # else /* No XPC, just try to run as the user ID */
371 # endif /* HAVE_XPC */
373 #endif /* HAVE_GSSAPI */
376 * Get the (final) content type...
379 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
380 content_type
= "application/octet-stream";
382 if ((final_content_type
= getenv("FINAL_CONTENT_TYPE")) == NULL
)
384 final_content_type
= content_type
;
386 if (!strncmp(final_content_type
, "printer/", 8))
387 final_content_type
= "application/vnd.cups-raw";
391 * Extract the hostname and printer name from the URI...
394 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
395 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
396 resource
, sizeof(resource
));
399 port
= IPP_PORT
; /* Default to port 631 */
401 if (!strcmp(scheme
, "https"))
402 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
404 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
407 * See if there are any options...
414 contimeout
= 7 * 24 * 60 * 60;
416 if ((optptr
= strchr(resource
, '?')) != NULL
)
419 * Yup, terminate the device name string and move to the first
420 * character of the optptr...
426 * Then parse the optptr...
437 while (*optptr
&& *optptr
!= '=' && *optptr
!= '+' && *optptr
!= '&')
440 if ((sep
= *optptr
) != '\0')
451 while (*optptr
&& *optptr
!= '+' && *optptr
!= '&')
461 * Process the option...
464 if (!_cups_strcasecmp(name
, "waitjob"))
467 * Wait for job completion?
470 waitjob
= !_cups_strcasecmp(value
, "on") ||
471 !_cups_strcasecmp(value
, "yes") ||
472 !_cups_strcasecmp(value
, "true");
474 else if (!_cups_strcasecmp(name
, "waitprinter"))
477 * Wait for printer idle?
480 waitprinter
= !_cups_strcasecmp(value
, "on") ||
481 !_cups_strcasecmp(value
, "yes") ||
482 !_cups_strcasecmp(value
, "true");
484 else if (!_cups_strcasecmp(name
, "encryption"))
487 * Enable/disable encryption?
490 if (!_cups_strcasecmp(value
, "always"))
491 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
492 else if (!_cups_strcasecmp(value
, "required"))
493 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
494 else if (!_cups_strcasecmp(value
, "never"))
495 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
496 else if (!_cups_strcasecmp(value
, "ifrequested"))
497 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
500 _cupsLangPrintFilter(stderr
, "ERROR",
501 _("Unknown encryption option value: \"%s\"."),
505 else if (!_cups_strcasecmp(name
, "version"))
507 if (!strcmp(value
, "1.0"))
509 else if (!strcmp(value
, "1.1"))
511 else if (!strcmp(value
, "2.0"))
513 else if (!strcmp(value
, "2.1"))
515 else if (!strcmp(value
, "2.2"))
519 _cupsLangPrintFilter(stderr
, "ERROR",
520 _("Unknown version option value: \"%s\"."),
525 else if (!_cups_strcasecmp(name
, "compression"))
527 if (!_cups_strcasecmp(value
, "true") || !_cups_strcasecmp(value
, "yes") ||
528 !_cups_strcasecmp(value
, "on") || !_cups_strcasecmp(value
, "gzip"))
529 compression
= "gzip";
531 #endif /* HAVE_LIBZ */
532 else if (!_cups_strcasecmp(name
, "contimeout"))
535 * Set the connection timeout...
539 contimeout
= atoi(value
);
547 _cupsLangPrintFilter(stderr
, "ERROR",
548 _("Unknown option \"%s\" with value \"%s\"."),
555 * If we have 7 arguments, print the file named on the command-line.
556 * Otherwise, copy stdin to a temporary file and print the temporary
564 send_options
= !_cups_strcasecmp(final_content_type
, "application/pdf") ||
565 !_cups_strcasecmp(final_content_type
, "application/vnd.cups-pdf") ||
566 !_cups_strncasecmp(final_content_type
, "image/", 6);
568 fputs("DEBUG: Sending stdin for job...\n", stderr
);
573 * Point to the files on the command-line...
576 num_files
= argc
- 6;
582 compress_files(num_files
, files
);
583 #endif /* HAVE_LIBZ */
585 fprintf(stderr
, "DEBUG: %d files to send in job...\n", num_files
);
589 * Set the authentication info, if any...
592 cupsSetPasswordCB(password_cb
);
597 * Use authenticaion information in the device URI...
600 if ((password
= strchr(username
, ':')) != NULL
)
603 cupsSetUser(username
);
608 * Try loading authentication information from the environment.
611 const char *ptr
= getenv("AUTH_USERNAME");
615 strlcpy(username
, ptr
, sizeof(username
));
619 password
= getenv("AUTH_PASSWORD");
623 * Try finding the remote server...
626 start_time
= time(NULL
);
628 sprintf(portname
, "%d", port
);
630 update_reasons(NULL
, "+connecting-to-device");
631 fprintf(stderr
, "DEBUG: Looking up \"%s\"...\n", hostname
);
633 while ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
635 _cupsLangPrintFilter(stderr
, "INFO",
636 _("Unable to locate printer \"%s\"."), hostname
);
639 if (getenv("CLASS") != NULL
)
641 update_reasons(NULL
, "-connecting-to-device");
642 return (CUPS_BACKEND_STOP
);
646 http
= _httpCreate(hostname
, port
, addrlist
, cupsEncryption(), AF_UNSPEC
);
647 httpSetTimeout(http
, 30.0, timeout_cb
, NULL
);
650 * See if the printer supports SNMP...
653 if ((snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
)) >= 0)
655 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
),
659 have_supplies
= start_count
= 0;
662 * Wait for data from the filter...
667 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), 0, backendNetworkSideCB
))
668 return (CUPS_BACKEND_OK
);
669 else if ((bytes
= read(0, buffer
, sizeof(buffer
))) <= 0)
670 return (CUPS_BACKEND_OK
);
674 * Try connecting to the remote server...
677 delay
= _cupsNextDelay(0, &prev_delay
);
681 fprintf(stderr
, "DEBUG: Connecting to %s:%d\n", hostname
, port
);
682 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
684 if (httpReconnect(http
))
686 int error
= errno
; /* Connection error */
688 if (http
->status
== HTTP_PKI_ERROR
)
689 update_reasons(NULL
, "+cups-certificate-error");
694 if (getenv("CLASS") != NULL
)
697 * If the CLASS environment variable is set, the job was submitted
698 * to a class and not to a specific queue. In this case, we want
699 * to abort immediately so that the job can be requeued on the next
700 * available printer in the class.
703 _cupsLangPrintFilter(stderr
, "INFO",
704 _("Unable to contact printer, queuing on next "
705 "printer in class."));
708 * Sleep 5 seconds to keep the job from requeuing too rapidly...
713 update_reasons(NULL
, "-connecting-to-device");
715 return (CUPS_BACKEND_FAILED
);
718 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
720 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
721 errno
== EHOSTUNREACH
)
723 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
725 _cupsLangPrintFilter(stderr
, "ERROR",
726 _("The printer is not responding."));
727 update_reasons(NULL
, "-connecting-to-device");
728 return (CUPS_BACKEND_FAILED
);
734 _cupsLangPrintFilter(stderr
, "WARNING",
735 _("The printer may not exist or "
736 "is unavailable at this time."));
740 _cupsLangPrintFilter(stderr
, "WARNING",
741 _("The printer is unreachable at this "
747 _cupsLangPrintFilter(stderr
, "WARNING",
748 _("The printer is busy."));
754 delay
= _cupsNextDelay(delay
, &prev_delay
);
758 _cupsLangPrintFilter(stderr
, "ERROR",
759 _("The printer is not responding."));
767 update_reasons(NULL
, "-cups-certificate-error");
769 while (http
->fd
< 0);
771 if (job_canceled
|| !http
)
772 return (CUPS_BACKEND_FAILED
);
774 update_reasons(NULL
, "-connecting-to-device");
775 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
777 fprintf(stderr
, "DEBUG: Connected to %s:%d...\n",
778 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
779 _httpAddrPort(http
->hostaddr
));
782 * Build a URI for the printer and fill the standard IPP attributes for
783 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
784 * might contain username:password information...
787 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), scheme
, NULL
, hostname
,
791 * First validate the destination and see if the device supports multiple
798 media_col_sup
= NULL
;
800 operations_sup
= NULL
;
801 doc_handling_sup
= NULL
;
806 * Check for side-channel requests...
809 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
812 * Build the IPP request...
815 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
816 request
->request
.op
.version
[0] = version
/ 10;
817 request
->request
.op
.version
[1] = version
% 10;
819 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
822 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
823 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
830 fputs("DEBUG: Getting supported attributes...\n", stderr
);
832 if (http
->version
< HTTP_1_1
)
834 fprintf(stderr
, "DEBUG: Printer responded with HTTP version %d.%d.\n",
835 http
->version
/ 100, http
->version
% 100);
836 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
837 "cups-ipp-wrong-http-version");
840 supported
= cupsDoRequest(http
, request
, resource
);
841 ipp_status
= cupsLastError();
843 fprintf(stderr
, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
844 ippErrorString(ipp_status
), cupsLastErrorString());
846 if (ipp_status
<= IPP_OK_CONFLICT
)
850 fprintf(stderr
, "DEBUG: Get-Printer-Attributes returned %s.\n",
851 ippErrorString(ipp_status
));
853 if (ipp_status
== IPP_PRINTER_BUSY
||
854 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
856 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
858 _cupsLangPrintFilter(stderr
, "ERROR",
859 _("The printer is not responding."));
860 return (CUPS_BACKEND_FAILED
);
863 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is busy."));
865 report_printer_state(supported
);
869 delay
= _cupsNextDelay(delay
, &prev_delay
);
871 else if ((ipp_status
== IPP_BAD_REQUEST
||
872 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
> 10)
875 * Switch to IPP/1.1 or IPP/1.0...
880 _cupsLangPrintFilter(stderr
, "INFO",
881 _("Printer does not support IPP/%d.%d, trying "
882 "IPP/%s."), version
/ 10, version
% 10, "1.1");
887 _cupsLangPrintFilter(stderr
, "INFO",
888 _("Printer does not support IPP/%d.%d, trying "
889 "IPP/%s."), version
/ 10, version
% 10, "1.0");
895 else if (ipp_status
== IPP_NOT_FOUND
)
897 _cupsLangPrintFilter(stderr
, "ERROR",
898 _("The printer URI is incorrect or no longer "
901 ippDelete(supported
);
903 return (CUPS_BACKEND_STOP
);
905 else if (ipp_status
== IPP_FORBIDDEN
||
906 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
908 const char *www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
909 /* WWW-Authenticate field value */
911 if (!strncmp(www_auth
, "Negotiate", 9))
912 auth_info_required
= "negotiate";
913 else if (www_auth
[0])
914 auth_info_required
= "username,password";
916 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
917 return (CUPS_BACKEND_AUTH_REQUIRED
);
919 else if (ipp_status
!= IPP_NOT_AUTHORIZED
)
921 _cupsLangPrintFilter(stderr
, "ERROR",
922 _("Unable to get printer status."));
926 ippDelete(supported
);
931 if (!getenv("CLASS"))
934 * Check printer-is-accepting-jobs = false and printer-state-reasons for the
935 * "spool-area-full" keyword...
940 if ((printer_accepting
= ippFindAttribute(supported
,
941 "printer-is-accepting-jobs",
942 IPP_TAG_BOOLEAN
)) != NULL
&&
943 !printer_accepting
->values
[0].boolean
)
945 else if (!printer_accepting
)
946 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
947 "cups-ipp-missing-printer-is-accepting-jobs");
949 if ((printer_state
= ippFindAttribute(supported
,
950 "printer-state-reasons",
951 IPP_TAG_KEYWORD
)) != NULL
&& !busy
)
953 for (i
= 0; i
< printer_state
->num_values
; i
++)
954 if (!strcmp(printer_state
->values
[0].string
.text
,
955 "spool-area-full") ||
956 !strncmp(printer_state
->values
[0].string
.text
, "spool-area-full-",
964 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
965 "cups-ipp-missing-printer-state-reasons");
969 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is busy."));
971 report_printer_state(supported
);
975 delay
= _cupsNextDelay(delay
, &prev_delay
);
977 ippDelete(supported
);
984 * Check for supported attributes...
987 if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
988 IPP_TAG_RANGE
)) != NULL
)
991 * Has the "copies-supported" attribute - does it have an upper
995 fprintf(stderr
, "DEBUG: copies-supported=%d-%d\n",
996 copies_sup
->values
[0].range
.lower
,
997 copies_sup
->values
[0].range
.upper
);
999 if (copies_sup
->values
[0].range
.upper
<= 1)
1000 copies_sup
= NULL
; /* No */
1003 cups_version
= ippFindAttribute(supported
, "cups-version", IPP_TAG_TEXT
);
1005 if ((format_sup
= ippFindAttribute(supported
, "document-format-supported",
1006 IPP_TAG_MIMETYPE
)) != NULL
)
1008 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
1009 format_sup
->num_values
);
1010 for (i
= 0; i
< format_sup
->num_values
; i
++)
1011 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
1012 format_sup
->values
[i
].string
.text
);
1015 if ((media_col_sup
= ippFindAttribute(supported
, "media-col-supported",
1016 IPP_TAG_KEYWORD
)) != NULL
)
1018 fprintf(stderr
, "DEBUG: media-col-supported (%d values)\n",
1019 media_col_sup
->num_values
);
1020 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
1021 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
1022 media_col_sup
->values
[i
].string
.text
);
1025 if ((operations_sup
= ippFindAttribute(supported
, "operations-supported",
1026 IPP_TAG_ENUM
)) != NULL
)
1028 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1029 if (operations_sup
->values
[i
].integer
== IPP_PRINT_JOB
)
1032 if (i
>= operations_sup
->num_values
)
1033 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1034 "cups-ipp-missing-print-job");
1036 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1037 if (operations_sup
->values
[i
].integer
== IPP_CANCEL_JOB
)
1040 if (i
>= operations_sup
->num_values
)
1041 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1042 "cups-ipp-missing-cancel-job");
1044 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1045 if (operations_sup
->values
[i
].integer
== IPP_GET_JOB_ATTRIBUTES
)
1048 if (i
>= operations_sup
->num_values
)
1049 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1050 "cups-ipp-missing-get-job-attributes");
1052 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1053 if (operations_sup
->values
[i
].integer
== IPP_GET_PRINTER_ATTRIBUTES
)
1056 if (i
>= operations_sup
->num_values
)
1057 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1058 "cups-ipp-missing-get-printer-attributes");
1060 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1062 if (operations_sup
->values
[i
].integer
== IPP_VALIDATE_JOB
)
1064 else if (operations_sup
->values
[i
].integer
== IPP_CREATE_JOB
)
1066 else if (operations_sup
->values
[i
].integer
== IPP_SEND_DOCUMENT
)
1072 fputs("DEBUG: Printer supports Create-Job but not Send-Document.\n",
1078 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1079 "cups-ipp-missing-validate-job");
1082 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1083 "cups-ipp-missing-operations-supported");
1085 doc_handling_sup
= ippFindAttribute(supported
,
1086 "multiple-document-handling-supported",
1089 report_printer_state(supported
);
1091 while (ipp_status
> IPP_OK_CONFLICT
);
1094 * See if the printer is accepting jobs and is not stopped; if either
1095 * condition is true and we are printing to a class, requeue the job...
1098 if (getenv("CLASS") != NULL
)
1100 printer_state
= ippFindAttribute(supported
, "printer-state",
1102 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
1105 if (printer_state
== NULL
||
1106 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
1108 printer_accepting
== NULL
||
1109 !printer_accepting
->values
[0].boolean
)
1112 * If the CLASS environment variable is set, the job was submitted
1113 * to a class and not to a specific queue. In this case, we want
1114 * to abort immediately so that the job can be requeued on the next
1115 * available printer in the class.
1118 _cupsLangPrintFilter(stderr
, "INFO",
1119 _("Unable to contact printer, queuing on next "
1120 "printer in class."));
1122 ippDelete(supported
);
1126 * Sleep 5 seconds to keep the job from requeuing too rapidly...
1131 return (CUPS_BACKEND_FAILED
);
1136 * See if the printer supports multiple copies...
1139 copies
= atoi(argv
[4]);
1141 if (copies_sup
|| argc
< 7)
1143 copies_remaining
= 1;
1145 if (argc
< 7 && !_cups_strncasecmp(final_content_type
, "image/", 6))
1149 copies_remaining
= copies
;
1152 * Prepare remaining printing options...
1160 num_options
= cupsParseOptions(argv
[5], 0, &options
);
1162 if (!cups_version
&& media_col_sup
)
1165 * Load the PPD file and generate PWG attribute mapping information...
1168 ppd
= ppdOpenFile(getenv("PPD"));
1169 pc
= _ppdCacheCreateWithPPD(ppd
);
1177 document_format
= NULL
;
1179 if (format_sup
!= NULL
)
1181 for (i
= 0; i
< format_sup
->num_values
; i
++)
1182 if (!_cups_strcasecmp(final_content_type
, format_sup
->values
[i
].string
.text
))
1184 document_format
= final_content_type
;
1188 if (!document_format
)
1190 for (i
= 0; i
< format_sup
->num_values
; i
++)
1191 if (!_cups_strcasecmp("application/octet-stream",
1192 format_sup
->values
[i
].string
.text
))
1194 document_format
= "application/octet-stream";
1201 * If the printer does not support HTTP/1.1 (which IPP requires), copy stdin
1202 * to a temporary file so that we can do a HTTP/1.0 submission...
1204 * (I hate compatibility hacks!)
1207 if (http
->version
< HTTP_1_1
&& num_files
== 0)
1209 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
1211 perror("DEBUG: Unable to create temporary file");
1212 return (CUPS_BACKEND_FAILED
);
1215 _cupsLangPrintFilter(stderr
, "INFO", _("Copying print data."));
1217 if ((compatsize
= write(fd
, buffer
, bytes
)) < 0)
1219 perror("DEBUG: Unable to write temporary file");
1220 return (CUPS_BACKEND_FAILED
);
1223 if ((bytes
= backendRunLoop(-1, fd
, snmp_fd
, &(addrlist
->addr
), 0, 0,
1224 backendNetworkSideCB
)) < 0)
1225 return (CUPS_BACKEND_FAILED
);
1227 compatsize
+= bytes
;
1231 compatfile
= tmpfilename
;
1232 files
= &compatfile
;
1235 else if (http
->version
< HTTP_1_1
&& num_files
== 1)
1237 struct stat fileinfo
; /* File information */
1239 if (!stat(files
[0], &fileinfo
))
1240 compatsize
= fileinfo
.st_size
;
1244 * Start monitoring the printer in the background...
1248 monitor
.hostname
= hostname
;
1249 monitor
.user
= argv
[2];
1250 monitor
.resource
= resource
;
1251 monitor
.port
= port
;
1252 monitor
.version
= version
;
1254 monitor
.encryption
= cupsEncryption();
1255 monitor
.job_state
= IPP_JOB_PENDING
;
1256 monitor
.printer_state
= IPP_PRINTER_IDLE
;
1260 monitor
.job_name
= argv
[3];
1264 snprintf(print_job_name
, sizeof(print_job_name
), "%s - %s", argv
[1],
1266 monitor
.job_name
= print_job_name
;
1269 _cupsThreadCreate((_cups_thread_func_t
)monitor_printer
, &monitor
);
1272 * Validate access to the printer...
1275 while (!job_canceled
&& validate_job
)
1277 request
= new_request(IPP_VALIDATE_JOB
, version
, uri
, argv
[2],
1278 monitor
.job_name
, num_options
, options
, compression
,
1279 copies_sup
? copies
: 1, document_format
, pc
,
1280 media_col_sup
, doc_handling_sup
);
1282 ippDelete(cupsDoRequest(http
, request
, resource
));
1284 ipp_status
= cupsLastError();
1286 fprintf(stderr
, "DEBUG: Validate-Job: %s (%s)\n",
1287 ippErrorString(ipp_status
), cupsLastErrorString());
1292 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
|| ipp_status
== IPP_PRINTER_BUSY
)
1294 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is busy."));
1297 else if (ipp_status
== IPP_FORBIDDEN
||
1298 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1300 const char *www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
1301 /* WWW-Authenticate field value */
1303 if (!strncmp(www_auth
, "Negotiate", 9))
1304 auth_info_required
= "negotiate";
1305 else if (www_auth
[0])
1306 auth_info_required
= "username,password";
1310 else if (ipp_status
== IPP_OPERATION_NOT_SUPPORTED
)
1313 * This is all too common...
1316 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1317 "cups-ipp-missing-validate-job");
1320 else if (ipp_status
< IPP_REDIRECTION_OTHER_SITE
)
1325 * Then issue the print-job request...
1330 while (!job_canceled
&& copies_remaining
> 0)
1333 * Check for side-channel requests...
1336 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1339 * Build the IPP job creation request...
1345 request
= new_request((num_files
> 1 || create_job
) ? IPP_CREATE_JOB
:
1347 version
, uri
, argv
[2], monitor
.job_name
, num_options
,
1348 options
, compression
, copies_sup
? copies
: 1,
1349 document_format
, pc
, media_col_sup
, doc_handling_sup
);
1355 if (num_files
> 1 || create_job
)
1356 response
= cupsDoRequest(http
, request
, resource
);
1359 size_t length
= 0; /* Length of request */
1363 fputs("DEBUG: Sending file using HTTP/1.0 Content-Length...\n", stderr
);
1364 length
= ippLength(request
) + (size_t)compatsize
;
1367 fputs("DEBUG: Sending file using HTTP/1.1 chunking...\n", stderr
);
1369 http_status
= cupsSendRequest(http
, request
, resource
, length
);
1370 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
)
1374 if ((fd
= open(files
[0], O_RDONLY
)) < 0)
1376 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1377 return (CUPS_BACKEND_FAILED
);
1383 http_status
= cupsWriteRequestData(http
, buffer
, bytes
);
1386 while (http_status
== HTTP_CONTINUE
&&
1387 (!job_canceled
|| compatsize
> 0))
1390 * Check for side-channel requests and more print data...
1395 FD_SET(snmp_fd
, &input
);
1397 while (select(fd
> snmp_fd
? fd
+ 1 : snmp_fd
+ 1, &input
, NULL
, NULL
,
1398 NULL
) <= 0 && !job_canceled
);
1400 if (FD_ISSET(snmp_fd
, &input
))
1401 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1403 if (FD_ISSET(fd
, &input
))
1405 if ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1407 fprintf(stderr
, "DEBUG: Read %d bytes...\n", (int)bytes
);
1409 if (cupsWriteRequestData(http
, buffer
, bytes
) != HTTP_CONTINUE
)
1412 else if (bytes
== 0 || (errno
!= EINTR
&& errno
!= EAGAIN
))
1421 response
= cupsGetResponse(http
, resource
);
1425 ipp_status
= cupsLastError();
1427 fprintf(stderr
, "DEBUG: %s: %s (%s)\n",
1428 (num_files
> 1 || create_job
) ? "Create-Job" : "Print-Job",
1429 ippErrorString(ipp_status
), cupsLastErrorString());
1431 if (ipp_status
> IPP_OK_CONFLICT
)
1438 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1439 ipp_status
== IPP_NOT_POSSIBLE
||
1440 ipp_status
== IPP_PRINTER_BUSY
)
1442 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is busy."));
1448 * We can't re-submit when we have no files to print, so exit
1449 * immediately with the right status code...
1455 else if (ipp_status
== IPP_ERROR_JOB_CANCELED
)
1457 else if (ipp_status
== IPP_NOT_AUTHORIZED
)
1462 * Update auth-info-required as needed...
1465 _cupsLangPrintFilter(stderr
, "ERROR",
1466 _("Print file was not accepted."));
1468 if (ipp_status
== IPP_FORBIDDEN
||
1469 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1471 const char *www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
1472 /* WWW-Authenticate field value */
1474 if (!strncmp(www_auth
, "Negotiate", 9))
1475 auth_info_required
= "negotiate";
1476 else if (www_auth
[0])
1477 auth_info_required
= "username,password";
1479 else if (ipp_status
== IPP_REQUEST_VALUE
)
1482 * Print file is too large, abort this job...
1493 * We can't re-submit when we have no files to print, so exit
1494 * immediately with the right status code...
1501 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
1502 IPP_TAG_INTEGER
)) == NULL
)
1504 _cupsLangPrintFilter(stderr
, "INFO",
1505 _("Print file accepted - job ID unknown."));
1506 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1507 "cups-ipp-missing-job-id");
1513 monitor
.job_id
= job_id
= job_id_attr
->values
[0].integer
;
1514 _cupsLangPrintFilter(stderr
, "INFO",
1515 _("Print file accepted - job ID %d."), job_id
);
1518 fprintf(stderr
, "DEBUG: job-id=%d\n", job_id
);
1519 ippDelete(response
);
1524 if (job_id
&& (num_files
> 1 || create_job
))
1526 for (i
= 0; num_files
== 0 || i
< num_files
; i
++)
1529 * Check for side-channel requests...
1532 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1535 * Send the next file in the job...
1538 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
1539 request
->request
.op
.version
[0] = version
/ 10;
1540 request
->request
.op
.version
[1] = version
% 10;
1542 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1545 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1549 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1550 "requesting-user-name", NULL
, argv
[2]);
1552 if ((i
+ 1) >= num_files
)
1553 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
1555 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1556 "document-format", NULL
, document_format
);
1558 fprintf(stderr
, "DEBUG: Sending file %d using chunking...\n", i
+ 1);
1559 http_status
= cupsSendRequest(http
, request
, resource
, 0);
1560 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
)
1565 http_status
= cupsWriteRequestData(http
, buffer
, bytes
);
1569 if ((fd
= open(files
[i
], O_RDONLY
)) < 0)
1571 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1572 return (CUPS_BACKEND_FAILED
);
1581 while (!job_canceled
&&
1582 (bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1584 if (cupsWriteRequestData(http
, buffer
, bytes
) != HTTP_CONTINUE
)
1589 * Check for side-channel requests...
1592 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1600 ippDelete(cupsGetResponse(http
, resource
));
1603 fprintf(stderr
, "DEBUG: Send-Document: %s (%s)\n",
1604 ippErrorString(cupsLastError()), cupsLastErrorString());
1606 if (cupsLastError() > IPP_OK_CONFLICT
)
1608 ipp_status
= cupsLastError();
1610 _cupsLangPrintFilter(stderr
, "ERROR",
1611 _("Unable to add document to print job."));
1618 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
)
1712 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1713 ipp_status
!= IPP_NOT_POSSIBLE
&&
1714 ipp_status
!= IPP_PRINTER_BUSY
)
1716 ippDelete(response
);
1717 ipp_status
= IPP_OK
;
1724 if ((job_state
= ippFindAttribute(response
, "job-state",
1725 IPP_TAG_ENUM
)) != NULL
)
1728 * Reflect the remote job state in the local queue...
1732 job_state
->values
[0].integer
>= IPP_JOB_PENDING
&&
1733 job_state
->values
[0].integer
<= IPP_JOB_COMPLETED
)
1734 update_reasons(NULL
,
1735 remote_job_states
[job_state
->values
[0].integer
-
1738 if ((job_sheets
= ippFindAttribute(response
,
1739 "job-media-sheets-completed",
1740 IPP_TAG_INTEGER
)) == NULL
)
1741 job_sheets
= ippFindAttribute(response
,
1742 "job-impressions-completed",
1746 fprintf(stderr
, "PAGE: total %d\n",
1747 job_sheets
->values
[0].integer
);
1750 * Stop polling if the job is finished or pending-held...
1753 if (job_state
->values
[0].integer
> IPP_JOB_STOPPED
)
1755 ippDelete(response
);
1759 else if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1760 ipp_status
!= IPP_NOT_POSSIBLE
&&
1761 ipp_status
!= IPP_PRINTER_BUSY
)
1764 * If the printer does not return a job-state attribute, it does not
1765 * conform to the IPP specification - break out immediately and fail
1769 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1770 "cups-ipp-missing-job-state");
1771 ipp_status
= IPP_INTERNAL_ERROR
;
1776 ippDelete(response
);
1779 * Wait before polling again...
1784 delay
= _cupsNextDelay(delay
, &prev_delay
);
1789 * Cancel the job as needed...
1792 if (job_canceled
&& job_id
)
1793 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1796 * Check the printer state and report it if necessary...
1799 check_printer_state(http
, uri
, resource
, argv
[2], version
);
1802 * Collect the final page count as needed...
1805 if (have_supplies
&&
1806 !backendSNMPSupplies(snmp_fd
, http
->hostaddr
, &page_count
, NULL
) &&
1807 page_count
> start_count
)
1808 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
1812 * See if we used Kerberos at all...
1816 auth_info_required
= "negotiate";
1817 #endif /* HAVE_GSSAPI */
1825 cupsFreeOptions(num_options
, options
);
1826 _ppdCacheDestroy(pc
);
1830 ippDelete(supported
);
1833 * Remove the temporary file(s) if necessary...
1837 unlink(tmpfilename
);
1842 for (i
= 0; i
< num_files
; i
++)
1845 #endif /* HAVE_LIBZ */
1848 * Return the queue status...
1851 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
1852 ipp_status
== IPP_AUTHENTICATION_CANCELED
||
1853 ipp_status
<= IPP_OK_CONFLICT
)
1854 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
1856 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
1857 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1858 return (CUPS_BACKEND_AUTH_REQUIRED
);
1859 else if (ipp_status
== IPP_INTERNAL_ERROR
)
1860 return (CUPS_BACKEND_STOP
);
1861 else if (ipp_status
== IPP_DOCUMENT_FORMAT
||
1862 ipp_status
== IPP_CONFLICT
)
1863 return (CUPS_BACKEND_FAILED
);
1864 else if (ipp_status
== IPP_REQUEST_VALUE
)
1866 _cupsLangPrintFilter(stderr
, "ERROR", _("Print job too large."));
1867 return (CUPS_BACKEND_CANCEL
);
1869 else if (ipp_status
> IPP_OK_CONFLICT
&& ipp_status
!= IPP_ERROR_JOB_CANCELED
)
1870 return (CUPS_BACKEND_RETRY_CURRENT
);
1873 _cupsLangPrintFilter(stderr
, "INFO", _("Ready to print."));
1874 return (CUPS_BACKEND_OK
);
1880 * 'cancel_job()' - Cancel a print job.
1884 cancel_job(http_t
*http
, /* I - HTTP connection */
1885 const char *uri
, /* I - printer-uri */
1886 int id
, /* I - job-id */
1887 const char *resource
, /* I - Resource path */
1888 const char *user
, /* I - requesting-user-name */
1889 int version
) /* I - IPP version */
1891 ipp_t
*request
; /* Cancel-Job request */
1894 _cupsLangPrintFilter(stderr
, "INFO", _("Canceling print job."));
1896 request
= ippNewRequest(IPP_CANCEL_JOB
);
1897 request
->request
.op
.version
[0] = version
/ 10;
1898 request
->request
.op
.version
[1] = version
% 10;
1900 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1902 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1904 if (user
&& user
[0])
1905 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1906 "requesting-user-name", NULL
, user
);
1912 ippDelete(cupsDoRequest(http
, request
, resource
));
1914 if (cupsLastError() > IPP_OK_CONFLICT
)
1915 _cupsLangPrintFilter(stderr
, "ERROR", _("Unable to cancel print job."));
1920 * 'check_printer_state()' - Check the printer state.
1923 static ipp_pstate_t
/* O - Current printer-state */
1924 check_printer_state(
1925 http_t
*http
, /* I - HTTP connection */
1926 const char *uri
, /* I - Printer URI */
1927 const char *resource
, /* I - Resource path */
1928 const char *user
, /* I - Username, if any */
1929 int version
) /* I - IPP version */
1931 ipp_t
*request
, /* IPP request */
1932 *response
; /* IPP response */
1933 ipp_attribute_t
*attr
; /* Attribute in response */
1934 ipp_pstate_t printer_state
= IPP_PRINTER_STOPPED
;
1935 /* Current printer-state */
1939 * Send a Get-Printer-Attributes request and log the results...
1942 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1943 request
->request
.op
.version
[0] = version
/ 10;
1944 request
->request
.op
.version
[1] = version
% 10;
1946 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1949 if (user
&& user
[0])
1950 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1951 "requesting-user-name", NULL
, user
);
1953 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1954 "requested-attributes",
1955 (int)(sizeof(pattrs
) / sizeof(pattrs
[0])), NULL
, pattrs
);
1957 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
1959 report_printer_state(response
);
1961 if ((attr
= ippFindAttribute(response
, "printer-state",
1962 IPP_TAG_ENUM
)) != NULL
)
1963 printer_state
= (ipp_pstate_t
)attr
->values
[0].integer
;
1965 ippDelete(response
);
1968 fprintf(stderr
, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
1969 ippErrorString(cupsLastError()), cupsLastErrorString());
1971 if (cupsLastError() <= IPP_OK_CONFLICT
)
1975 * Return the printer-state value...
1978 return (printer_state
);
1984 * 'compress_files()' - Compress print files.
1988 compress_files(int num_files
, /* I - Number of files */
1989 char **files
) /* I - Files */
1991 int i
, /* Looping var */
1992 fd
; /* Temporary file descriptor */
1993 ssize_t bytes
; /* Bytes read/written */
1994 size_t total
; /* Total bytes read */
1995 cups_file_t
*in
, /* Input file */
1996 *out
; /* Output file */
1997 struct stat outinfo
; /* Output file information */
1998 char filename
[1024], /* Temporary filename */
1999 buffer
[32768]; /* Copy buffer */
2002 fprintf(stderr
, "DEBUG: Compressing %d job files...\n", num_files
);
2003 for (i
= 0; i
< num_files
; i
++)
2005 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
2007 _cupsLangPrintError("ERROR", _("Unable to create compressed print file"));
2008 exit(CUPS_BACKEND_FAILED
);
2011 if ((out
= cupsFileOpenFd(fd
, "w9")) == NULL
)
2013 _cupsLangPrintError("ERROR", _("Unable to open compressed print file"));
2014 exit(CUPS_BACKEND_FAILED
);
2017 if ((in
= cupsFileOpen(files
[i
], "r")) == NULL
)
2019 _cupsLangPrintError("ERROR", _("Unable to open print file"));
2021 exit(CUPS_BACKEND_FAILED
);
2025 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
2026 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
2028 _cupsLangPrintError("ERROR",
2029 _("Unable to generate compressed print file"));
2032 exit(CUPS_BACKEND_FAILED
);
2040 files
[i
] = strdup(filename
);
2042 if (!stat(filename
, &outinfo
))
2044 "DEBUG: File %d compressed to %.1f%% of original size, "
2045 CUPS_LLFMT
" bytes...\n",
2046 i
+ 1, 100.0 * outinfo
.st_size
/ total
,
2047 CUPS_LLCAST outinfo
.st_size
);
2050 #endif /* HAVE_LIBZ */
2054 * 'monitor_printer()' - Monitor the printer state.
2057 static void * /* O - Thread exit code */
2059 _cups_monitor_t
*monitor
) /* I - Monitoring data */
2061 http_t
*http
; /* Connection to printer */
2062 ipp_t
*request
, /* IPP request */
2063 *response
; /* IPP response */
2064 ipp_attribute_t
*attr
; /* Attribute in response */
2065 int delay
, /* Current delay */
2066 prev_delay
; /* Previous delay */
2067 ipp_op_t job_op
; /* Operation to use */
2068 int job_id
; /* Job ID */
2069 const char *job_name
; /* Job name */
2070 ipp_jstate_t job_state
; /* Job state */
2071 const char *job_user
; /* Job originating user name */
2075 * Make a copy of the printer connection...
2078 http
= _httpCreate(monitor
->hostname
, monitor
->port
, NULL
, monitor
->encryption
,
2080 httpSetTimeout(http
, 30.0, timeout_cb
, NULL
);
2082 cupsSetUser(username
);
2083 cupsSetPasswordCB(password_cb
);
2086 * Loop until the job is canceled, aborted, or completed.
2089 delay
= _cupsNextDelay(0, &prev_delay
);
2091 while (monitor
->job_state
< IPP_JOB_CANCELED
&& !job_canceled
)
2094 * Reconnect to the printer...
2097 if (!httpReconnect(http
))
2100 * Connected, so check on the printer state...
2103 monitor
->printer_state
= check_printer_state(http
, monitor
->uri
,
2109 * Check the status of the job itself...
2112 job_op
= monitor
->job_id
> 0 ? IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
;
2113 request
= ippNewRequest(job_op
);
2114 request
->request
.op
.version
[0] = monitor
->version
/ 10;
2115 request
->request
.op
.version
[1] = monitor
->version
% 10;
2117 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2118 NULL
, monitor
->uri
);
2119 if (job_op
== IPP_GET_JOB_ATTRIBUTES
)
2120 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
2123 if (monitor
->user
&& monitor
->user
[0])
2124 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2125 "requesting-user-name", NULL
, monitor
->user
);
2127 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2128 "requested-attributes",
2129 (int)(sizeof(jattrs
) / sizeof(jattrs
[0])), NULL
, jattrs
);
2135 response
= cupsDoRequest(http
, request
, monitor
->resource
);
2137 fprintf(stderr
, "DEBUG: %s: %s (%s)\n", ippOpString(job_op
),
2138 ippErrorString(cupsLastError()), cupsLastErrorString());
2140 if (cupsLastError() <= IPP_OK_CONFLICT
)
2143 if (job_op
== IPP_GET_JOB_ATTRIBUTES
)
2145 if ((attr
= ippFindAttribute(response
, "job-state",
2146 IPP_TAG_ENUM
)) != NULL
)
2147 monitor
->job_state
= (ipp_jstate_t
)attr
->values
[0].integer
;
2149 monitor
->job_state
= IPP_JOB_COMPLETED
;
2153 for (attr
= response
->attrs
; attr
; attr
= attr
->next
)
2157 job_state
= IPP_JOB_PENDING
;
2160 while (attr
&& attr
->group_tag
!= IPP_TAG_JOB
)
2166 while (attr
&& attr
->group_tag
== IPP_TAG_JOB
)
2168 if (!strcmp(attr
->name
, "job-id") &&
2169 attr
->value_tag
== IPP_TAG_INTEGER
)
2170 job_id
= attr
->values
[0].integer
;
2171 else if (!strcmp(attr
->name
, "job-name") &&
2172 (attr
->value_tag
== IPP_TAG_NAME
||
2173 attr
->value_tag
== IPP_TAG_NAMELANG
))
2174 job_name
= attr
->values
[0].string
.text
;
2175 else if (!strcmp(attr
->name
, "job-state") &&
2176 attr
->value_tag
== IPP_TAG_ENUM
)
2177 job_state
= attr
->values
[0].integer
;
2178 else if (!strcmp(attr
->name
, "job-originating-user-name") &&
2179 (attr
->value_tag
== IPP_TAG_NAME
||
2180 attr
->value_tag
== IPP_TAG_NAMELANG
))
2181 job_user
= attr
->values
[0].string
.text
;
2186 if (job_id
> 0 && job_name
&& !strcmp(job_name
, monitor
->job_name
) &&
2187 job_user
&& monitor
->user
&& !strcmp(job_user
, monitor
->user
))
2189 monitor
->job_id
= job_id
;
2190 monitor
->job_state
= job_state
;
2199 ippDelete(response
);
2202 * Disconnect from the printer - we'll reconnect on the next poll...
2205 _httpDisconnect(http
);
2209 * Sleep for N seconds...
2214 delay
= _cupsNextDelay(delay
, &prev_delay
);
2218 * Cancel the job if necessary...
2221 if (job_canceled
&& monitor
->job_id
> 0)
2222 if (!httpReconnect(http
))
2223 cancel_job(http
, monitor
->uri
, monitor
->job_id
, monitor
->resource
,
2224 monitor
->user
, monitor
->version
);
2227 * Cleanup and return...
2237 * 'new_request()' - Create a new print creation or validation request.
2240 static ipp_t
* /* O - Request data */
2242 ipp_op_t op
, /* I - IPP operation code */
2243 int version
, /* I - IPP version number */
2244 const char *uri
, /* I - printer-uri value */
2245 const char *user
, /* I - requesting-user-name value */
2246 const char *title
, /* I - job-name value */
2247 int num_options
, /* I - Number of options to send */
2248 cups_option_t
*options
, /* I - Options to send */
2249 const char *compression
, /* I - compression value or NULL */
2250 int copies
, /* I - copies value or 0 */
2251 const char *format
, /* I - document-format value or NULL */
2252 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2253 ipp_attribute_t
*media_col_sup
, /* I - media-col-supported values */
2254 ipp_attribute_t
*doc_handling_sup
) /* I - multiple-document-handling-supported values */
2256 int i
; /* Looping var */
2257 ipp_t
*request
; /* Request data */
2258 const char *keyword
; /* PWG keyword */
2259 _pwg_size_t
*size
; /* PWG media size */
2260 ipp_t
*media_col
, /* media-col value */
2261 *media_size
; /* media-size value */
2262 const char *media_source
, /* media-source value */
2263 *media_type
, /* media-type value */
2264 *collate_str
; /* multiple-document-handling value */
2268 * Create the IPP request...
2271 request
= ippNewRequest(op
);
2272 request
->request
.op
.version
[0] = version
/ 10;
2273 request
->request
.op
.version
[1] = version
% 10;
2275 fprintf(stderr
, "DEBUG: %s IPP/%d.%d\n",
2276 ippOpString(request
->request
.op
.operation_id
),
2277 request
->request
.op
.version
[0],
2278 request
->request
.op
.version
[1]);
2281 * Add standard attributes...
2284 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2286 fprintf(stderr
, "DEBUG: printer-uri=\"%s\"\n", uri
);
2290 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2291 "requesting-user-name", NULL
, user
);
2292 fprintf(stderr
, "DEBUG: requesting-user-name=\"%s\"\n", user
);
2295 if (title
&& *title
)
2297 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
2299 fprintf(stderr
, "DEBUG: job-name=\"%s\"\n", title
);
2304 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
2305 "document-format", NULL
, format
);
2306 fprintf(stderr
, "DEBUG: document-format=\"%s\"\n", format
);
2312 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2313 "compression", NULL
, compression
);
2314 fprintf(stderr
, "DEBUG: compression=\"%s\"\n", compression
);
2316 #endif /* HAVE_LIBZ */
2319 * Handle options on the command-line...
2322 if (num_options
> 0)
2326 int num_finishings
= 0, /* Number of finishing values */
2327 finishings
[10]; /* Finishing enum values */
2330 * Send standard IPP attributes...
2333 if ((keyword
= cupsGetOption("PageSize", num_options
, options
)) == NULL
)
2334 keyword
= cupsGetOption("media", num_options
, options
);
2336 if ((size
= _ppdCacheGetSize(pc
, keyword
)) != NULL
)
2339 * Add a media-col value...
2342 media_size
= ippNew();
2343 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2344 "x-dimension", size
->width
);
2345 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2346 "y-dimension", size
->length
);
2348 media_col
= ippNew();
2349 ippAddCollection(media_col
, IPP_TAG_ZERO
, "media-size", media_size
);
2351 media_source
= _ppdCacheGetSource(pc
, cupsGetOption("InputSlot",
2354 media_type
= _ppdCacheGetType(pc
, cupsGetOption("MediaType",
2358 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
2360 if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2361 "media-left-margin"))
2362 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2363 "media-left-margin", size
->left
);
2364 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2365 "media-bottom-margin"))
2366 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2367 "media-bottom-margin", size
->bottom
);
2368 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2369 "media-right-margin"))
2370 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2371 "media-right-margin", size
->right
);
2372 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2373 "media-top-margin"))
2374 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2375 "media-top-margin", size
->top
);
2376 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2377 "media-source") && media_source
)
2378 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
2379 "media-source", NULL
, media_source
);
2380 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2381 "media-type") && media_type
)
2382 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
2383 "media-type", NULL
, media_type
);
2386 ippAddCollection(request
, IPP_TAG_JOB
, "media-col", media_col
);
2389 if ((keyword
= cupsGetOption("output-bin", num_options
,
2391 keyword
= _ppdCacheGetBin(pc
, cupsGetOption("OutputBin", num_options
,
2395 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-bin",
2398 if ((keyword
= cupsGetOption("output-mode", num_options
,
2400 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
2402 else if ((keyword
= cupsGetOption("ColorModel", num_options
,
2405 if (!_cups_strcasecmp(keyword
, "Gray"))
2406 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
2407 NULL
, "monochrome");
2409 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
2413 if ((keyword
= cupsGetOption("print-quality", num_options
,
2415 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2417 else if ((keyword
= cupsGetOption("cupsPrintQuality", num_options
,
2420 if (!_cups_strcasecmp(keyword
, "draft"))
2421 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2423 else if (!_cups_strcasecmp(keyword
, "normal"))
2424 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2425 IPP_QUALITY_NORMAL
);
2426 else if (!_cups_strcasecmp(keyword
, "high"))
2427 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2431 if ((keyword
= cupsGetOption("sides", num_options
, options
)) != NULL
)
2432 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2434 else if (pc
->sides_option
&&
2435 (keyword
= cupsGetOption(pc
->sides_option
, num_options
,
2438 if (!_cups_strcasecmp(keyword
, pc
->sides_1sided
))
2439 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2441 else if (!_cups_strcasecmp(keyword
, pc
->sides_2sided_long
))
2442 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2443 NULL
, "two-sided-long-edge");
2444 if (!_cups_strcasecmp(keyword
, pc
->sides_2sided_short
))
2445 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2446 NULL
, "two-sided-short-edge");
2449 if (doc_handling_sup
&&
2450 (!format
|| _cups_strncasecmp(format
, "image/", 6)) &&
2451 (keyword
= cupsGetOption("collate", num_options
, options
)) != NULL
)
2453 if (!_cups_strcasecmp(keyword
, "true"))
2454 collate_str
= "separate-documents-collated-copies";
2456 collate_str
= "separate-documents-uncollated-copies";
2458 for (i
= 0; i
< doc_handling_sup
->num_values
; i
++)
2459 if (!strcmp(doc_handling_sup
->values
[i
].string
.text
, collate_str
))
2461 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2462 "multiple-document-handling", NULL
, collate_str
);
2466 if (i
>= doc_handling_sup
->num_values
)
2471 * Map finishing options...
2474 num_finishings
= _ppdCacheGetFinishingValues(pc
, num_options
, options
,
2475 (int)(sizeof(finishings
) /
2476 sizeof(finishings
[0])),
2478 if (num_finishings
> 0)
2479 ippAddIntegers(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "finishings",
2480 num_finishings
, finishings
);
2483 * Map FaxOut options...
2486 if ((keyword
= cupsGetOption("phone", num_options
, options
)) != NULL
)
2488 ipp_t
*destination
; /* destination collection */
2489 char tel_uri
[1024]; /* tel: URI */
2491 destination
= ippNew();
2493 httpAssembleURI(HTTP_URI_CODING_ALL
, tel_uri
, sizeof(tel_uri
), "tel",
2494 NULL
, NULL
, 0, keyword
);
2495 ippAddString(destination
, IPP_TAG_JOB
, IPP_TAG_URI
, "destination-uri",
2498 if ((keyword
= cupsGetOption("faxPrefix", num_options
,
2499 options
)) != NULL
&& *keyword
)
2500 ippAddString(destination
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
2501 "pre-dial-string", NULL
, keyword
);
2503 ippAddCollection(request
, IPP_TAG_JOB
, "destination-uris", destination
);
2504 ippDelete(destination
);
2510 * When talking to another CUPS server, send all options...
2513 cupsEncodeOptions(request
, num_options
, options
);
2516 if (copies
> 1 && copies
<= pc
->max_copies
)
2517 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies", copies
);
2525 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
2528 static const char * /* O - Password */
2529 password_cb(const char *prompt
) /* I - Prompt (not used) */
2531 fprintf(stderr
, "DEBUG: password_cb(prompt=\"%s\"), password=%p, "
2532 "password_tries=%d\n", prompt
, password
, password_tries
);
2537 * Remember that we need to authenticate...
2540 auth_info_required
= "username,password";
2542 if (password
&& *password
&& password_tries
< 3)
2551 * Give up after 3 tries or if we don't have a password to begin with...
2560 * 'report_attr()' - Report an IPP attribute value.
2564 report_attr(ipp_attribute_t
*attr
) /* I - Attribute */
2566 int i
; /* Looping var */
2567 char value
[1024], /* Value string */
2568 *valptr
, /* Pointer into value string */
2569 *attrptr
; /* Pointer into attribute value */
2570 const char *cached
; /* Cached attribute */
2574 * Convert the attribute values into quoted strings...
2577 for (i
= 0, valptr
= value
;
2578 i
< attr
->num_values
&& valptr
< (value
+ sizeof(value
) - 10);
2584 switch (attr
->value_tag
)
2586 case IPP_TAG_INTEGER
:
2588 snprintf(valptr
, sizeof(value
) - (valptr
- value
), "%d",
2589 attr
->values
[i
].integer
);
2590 valptr
+= strlen(valptr
);
2595 case IPP_TAG_KEYWORD
:
2598 for (attrptr
= attr
->values
[i
].string
.text
;
2599 *attrptr
&& valptr
< (value
+ sizeof(value
) - 10);
2602 if (*attrptr
== '\\' || *attrptr
== '\"' || *attrptr
== '\'')
2609 *valptr
++ = *attrptr
;
2617 * Unsupported value type...
2626 _cupsMutexLock(&report_mutex
);
2628 if ((cached
= cupsGetOption(attr
->name
, num_attr_cache
,
2629 attr_cache
)) == NULL
|| strcmp(cached
, value
))
2632 * Tell the scheduler about the new values...
2635 num_attr_cache
= cupsAddOption(attr
->name
, value
, num_attr_cache
,
2637 fprintf(stderr
, "ATTR: %s=%s\n", attr
->name
, value
);
2640 _cupsMutexUnlock(&report_mutex
);
2645 * 'report_printer_state()' - Report the printer state.
2649 report_printer_state(ipp_t
*ipp
) /* I - IPP response */
2651 ipp_attribute_t
*pa
, /* printer-alert */
2652 *pam
, /* printer-alert-message */
2653 *psm
, /* printer-state-message */
2654 *reasons
, /* printer-state-reasons */
2655 *marker
; /* marker-* attributes */
2656 char value
[1024], /* State/message string */
2657 *valptr
; /* Pointer into string */
2658 static int ipp_supplies
= -1;
2659 /* Report supply levels? */
2663 * Report alerts and messages...
2666 if ((pa
= ippFindAttribute(ipp
, "printer-alert", IPP_TAG_TEXT
)) != NULL
)
2669 if ((pam
= ippFindAttribute(ipp
, "printer-alert-message",
2670 IPP_TAG_TEXT
)) != NULL
)
2673 if ((psm
= ippFindAttribute(ipp
, "printer-state-message",
2674 IPP_TAG_TEXT
)) != NULL
)
2676 char *ptr
; /* Pointer into message */
2679 strlcpy(value
, "INFO: ", sizeof(value
));
2680 for (ptr
= psm
->values
[0].string
.text
, valptr
= value
+ 6;
2681 *ptr
&& valptr
< (value
+ sizeof(value
) - 6);
2684 if (*ptr
< ' ' && *ptr
> 0 && *ptr
!= '\t')
2687 * Substitute "<XX>" for the control character; sprintf is safe because
2688 * we always leave 6 chars free at the end...
2691 sprintf(valptr
, "<%02X>", *ptr
);
2701 fputs(value
, stderr
);
2705 * Now report printer-state-reasons, filtering out some of the reasons we never
2709 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
2710 IPP_TAG_KEYWORD
)) == NULL
)
2713 update_reasons(reasons
, NULL
);
2716 * Relay the current marker-* attribute values...
2719 if (ipp_supplies
< 0)
2721 ppd_file_t
*ppd
; /* PPD file */
2722 ppd_attr_t
*ppdattr
; /* Attribute in PPD file */
2724 if ((ppd
= ppdOpenFile(getenv("PPD"))) != NULL
&&
2725 (ppdattr
= ppdFindAttr(ppd
, "cupsIPPSupplies", NULL
)) != NULL
&&
2726 ppdattr
->value
&& _cups_strcasecmp(ppdattr
->value
, "true"))
2734 if (ipp_supplies
> 0)
2736 if ((marker
= ippFindAttribute(ipp
, "marker-colors", IPP_TAG_NAME
)) != NULL
)
2737 report_attr(marker
);
2738 if ((marker
= ippFindAttribute(ipp
, "marker-high-levels",
2739 IPP_TAG_INTEGER
)) != NULL
)
2740 report_attr(marker
);
2741 if ((marker
= ippFindAttribute(ipp
, "marker-levels",
2742 IPP_TAG_INTEGER
)) != NULL
)
2743 report_attr(marker
);
2744 if ((marker
= ippFindAttribute(ipp
, "marker-low-levels",
2745 IPP_TAG_INTEGER
)) != NULL
)
2746 report_attr(marker
);
2747 if ((marker
= ippFindAttribute(ipp
, "marker-message",
2748 IPP_TAG_TEXT
)) != NULL
)
2749 report_attr(marker
);
2750 if ((marker
= ippFindAttribute(ipp
, "marker-names", IPP_TAG_NAME
)) != NULL
)
2751 report_attr(marker
);
2752 if ((marker
= ippFindAttribute(ipp
, "marker-types",
2753 IPP_TAG_KEYWORD
)) != NULL
)
2754 report_attr(marker
);
2759 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
2761 * 'run_as_user()' - Run the IPP backend as the printing user.
2763 * This function uses an XPC-based user agent to run the backend as the printing
2764 * user. We need to do this in order to have access to the user's Kerberos
2768 static int /* O - Exit status */
2769 run_as_user(int argc
, /* I - Number of command-line args */
2770 char *argv
[], /* I - Command-line arguments */
2771 uid_t uid
, /* I - User ID */
2772 const char *device_uri
, /* I - Device URI */
2773 int fd
) /* I - File to print */
2775 const char *auth_negotiate
;/* AUTH_NEGOTIATE env var */
2776 xpc_connection_t conn
; /* Connection to XPC service */
2777 xpc_object_t request
; /* Request message dictionary */
2778 __block xpc_object_t response
; /* Response message dictionary */
2779 dispatch_semaphore_t sem
; /* Semaphore for waiting for response */
2780 int status
= CUPS_BACKEND_FAILED
;
2781 /* Status of request */
2784 fprintf(stderr
, "DEBUG: Running IPP backend as UID %d.\n", (int)uid
);
2787 * Connect to the user agent for the specified UID...
2790 conn
= xpc_connection_create_mach_service(kPMPrintUIToolAgent
,
2791 dispatch_get_global_queue(0, 0), 0);
2794 _cupsLangPrintFilter(stderr
, "ERROR",
2795 _("Unable to start backend process."));
2796 fputs("DEBUG: Unable to create connection to agent.\n", stderr
);
2800 xpc_connection_set_event_handler(conn
,
2801 ^(xpc_object_t event
)
2803 xpc_type_t messageType
= xpc_get_type(event
);
2805 if (messageType
== XPC_TYPE_ERROR
)
2807 if (event
== XPC_ERROR_CONNECTION_INTERRUPTED
)
2808 fprintf(stderr
, "DEBUG: Interrupted connection to service %s.\n",
2809 xpc_connection_get_name(conn
));
2810 else if (event
== XPC_ERROR_CONNECTION_INVALID
)
2811 fprintf(stderr
, "DEBUG: Connection invalid for service %s.\n",
2812 xpc_connection_get_name(conn
));
2814 fprintf(stderr
, "DEBUG: Unxpected error for service %s: %s\n",
2815 xpc_connection_get_name(conn
),
2816 xpc_dictionary_get_string(event
, XPC_ERROR_KEY_DESCRIPTION
));
2819 xpc_connection_set_target_uid(conn
, uid
);
2820 xpc_connection_resume(conn
);
2823 * Try starting the backend...
2826 request
= xpc_dictionary_create(NULL
, NULL
, 0);
2827 xpc_dictionary_set_int64(request
, "command", kPMStartJob
);
2828 xpc_dictionary_set_string(request
, "device-uri", device_uri
);
2829 xpc_dictionary_set_string(request
, "job-id", argv
[1]);
2830 xpc_dictionary_set_string(request
, "user", argv
[2]);
2831 xpc_dictionary_set_string(request
, "title", argv
[3]);
2832 xpc_dictionary_set_string(request
, "copies", argv
[4]);
2833 xpc_dictionary_set_string(request
, "options", argv
[5]);
2834 xpc_dictionary_set_string(request
, "auth-info-required",
2835 getenv("AUTH_INFO_REQUIRED"));
2836 if ((auth_negotiate
= getenv("AUTH_NEGOTIATE")) != NULL
)
2837 xpc_dictionary_set_string(request
, "auth-negotiate", auth_negotiate
);
2838 xpc_dictionary_set_fd(request
, "stdin", fd
);
2839 xpc_dictionary_set_fd(request
, "stderr", 2);
2840 xpc_dictionary_set_fd(request
, "side-channel", CUPS_SC_FD
);
2842 sem
= dispatch_semaphore_create(0);
2845 xpc_connection_send_message_with_reply(conn
, request
,
2846 dispatch_get_global_queue(0,0),
2847 ^(xpc_object_t reply
)
2849 /* Save the response and wake up */
2850 if (xpc_get_type(reply
)
2851 == XPC_TYPE_DICTIONARY
)
2852 response
= xpc_retain(reply
);
2854 dispatch_semaphore_signal(sem
);
2857 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
2858 xpc_release(request
);
2859 dispatch_release(sem
);
2863 child_pid
= xpc_dictionary_get_int64(response
, "child-pid");
2865 xpc_release(response
);
2868 fprintf(stderr
, "DEBUG: Child PID=%d.\n", child_pid
);
2871 _cupsLangPrintFilter(stderr
, "ERROR",
2872 _("Unable to start backend process."));
2873 fputs("DEBUG: No child PID.\n", stderr
);
2879 _cupsLangPrintFilter(stderr
, "ERROR",
2880 _("Unable to start backend process."));
2881 fputs("DEBUG: No reply from agent.\n", stderr
);
2886 * Then wait for the backend to finish...
2889 request
= xpc_dictionary_create(NULL
, NULL
, 0);
2890 xpc_dictionary_set_int64(request
, "command", kPMWaitForJob
);
2891 xpc_dictionary_set_fd(request
, "stderr", 2);
2893 sem
= dispatch_semaphore_create(0);
2896 xpc_connection_send_message_with_reply(conn
, request
,
2897 dispatch_get_global_queue(0,0),
2898 ^(xpc_object_t reply
)
2900 /* Save the response and wake up */
2901 if (xpc_get_type(reply
)
2902 == XPC_TYPE_DICTIONARY
)
2903 response
= xpc_retain(reply
);
2905 dispatch_semaphore_signal(sem
);
2908 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
2909 xpc_release(request
);
2910 dispatch_release(sem
);
2914 status
= xpc_dictionary_get_int64(response
, "status");
2916 if (status
== SIGTERM
|| status
== SIGKILL
|| status
== SIGPIPE
)
2918 fprintf(stderr
, "DEBUG: Child terminated on signal %d.\n", status
);
2919 status
= CUPS_BACKEND_FAILED
;
2921 else if (WIFSIGNALED(status
))
2923 fprintf(stderr
, "DEBUG: Child crashed on signal %d.\n", status
);
2924 status
= CUPS_BACKEND_STOP
;
2926 else if (WIFEXITED(status
))
2928 status
= WEXITSTATUS(status
);
2929 fprintf(stderr
, "DEBUG: Child exited with status %d.\n", status
);
2932 xpc_release(response
);
2935 _cupsLangPrintFilter(stderr
, "ERROR",
2936 _("Unable to get backend exit status."));
2942 xpc_connection_suspend(conn
);
2943 xpc_connection_cancel(conn
);
2949 #endif /* HAVE_GSSAPI && HAVE_XPC */
2953 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
2957 sigterm_handler(int sig
) /* I - Signal */
2959 (void)sig
; /* remove compiler warnings... */
2961 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
2964 kill(child_pid
, sig
);
2967 #endif /* HAVE_GSSAPI && HAVE_XPC */
2972 * Flag that the job should be canceled...
2980 * The scheduler already tried to cancel us once, now just terminate
2981 * after removing our temp file!
2985 unlink(tmpfilename
);
2992 * 'timeout_cb()' - Handle HTTP timeouts.
2995 static int /* O - 1 to continue, 0 to cancel */
2996 timeout_cb(http_t
*http
, /* I - Connection to server (unused) */
2997 void *user_data
) /* I - User data (unused) */
3002 return (!job_canceled
);
3007 * 'update_reasons()' - Update the printer-state-reasons values.
3011 update_reasons(ipp_attribute_t
*attr
, /* I - printer-state-reasons or NULL */
3012 const char *s
) /* I - STATE: string or NULL */
3014 char op
; /* Add (+), remove (-), replace (\0) */
3015 cups_array_t
*new_reasons
; /* New reasons array */
3016 char *reason
, /* Current reason */
3017 add
[2048], /* Reasons added string */
3018 *addptr
, /* Pointer into add string */
3019 rem
[2048], /* Reasons removed string */
3020 *remptr
; /* Pointer into remove string */
3021 const char *addprefix
, /* Current add string prefix */
3022 *remprefix
; /* Current remove string prefix */
3025 fprintf(stderr
, "DEBUG: update_reasons(attr=%d(%s%s), s=\"%s\")\n",
3026 attr
? attr
->num_values
: 0, attr
? attr
->values
[0].string
.text
: "",
3027 attr
&& attr
->num_values
> 1 ? ",..." : "", s
? s
: "(null)");
3030 * Create an array of new reason keyword strings...
3035 int i
; /* Looping var */
3037 new_reasons
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
3040 for (i
= 0; i
< attr
->num_values
; i
++)
3042 reason
= attr
->values
[i
].string
.text
;
3044 if (strcmp(reason
, "none") &&
3045 strcmp(reason
, "none-report") &&
3046 strcmp(reason
, "paused") &&
3047 strncmp(reason
, "spool-area-full", 15) &&
3048 strcmp(reason
, "com.apple.print.recoverable-warning") &&
3049 strncmp(reason
, "cups-", 5))
3050 cupsArrayAdd(new_reasons
, reason
);
3055 if (*s
== '+' || *s
== '-')
3060 new_reasons
= _cupsArrayNewStrings(s
);
3066 * Compute the changes...
3070 addprefix
= "STATE: +";
3073 remprefix
= "STATE: -";
3076 fprintf(stderr
, "DEBUG2: op='%c', new_reasons=%d, state_reasons=%d\n",
3077 op
? op
: ' ', cupsArrayCount(new_reasons
),
3078 cupsArrayCount(state_reasons
));
3080 _cupsMutexLock(&report_mutex
);
3088 for (reason
= (char *)cupsArrayFirst(new_reasons
);
3090 reason
= (char *)cupsArrayNext(new_reasons
))
3092 if (!cupsArrayFind(state_reasons
, reason
))
3094 if (!strncmp(reason
, "cups-remote-", 12))
3097 * If we are setting cups-remote-xxx, remove all other cups-remote-xxx
3101 char *temp
; /* Current reason in state_reasons */
3103 cupsArraySave(state_reasons
);
3105 for (temp
= (char *)cupsArrayFirst(state_reasons
);
3107 temp
= (char *)cupsArrayNext(state_reasons
))
3108 if (!strncmp(temp
, "cups-remote-", 12))
3110 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
3112 remptr
+= strlen(remptr
);
3115 cupsArrayRemove(state_reasons
, temp
);
3119 cupsArrayRestore(state_reasons
);
3122 cupsArrayAdd(state_reasons
, reason
);
3124 snprintf(addptr
, sizeof(add
) - (addptr
- add
), "%s%s", addprefix
,
3126 addptr
+= strlen(addptr
);
3137 for (reason
= (char *)cupsArrayFirst(new_reasons
);
3139 reason
= (char *)cupsArrayNext(new_reasons
))
3141 if (cupsArrayFind(state_reasons
, reason
))
3143 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
3145 remptr
+= strlen(remptr
);
3148 cupsArrayRemove(state_reasons
, reason
);
3155 * Replace reasons...
3158 for (reason
= (char *)cupsArrayFirst(state_reasons
);
3160 reason
= (char *)cupsArrayNext(state_reasons
))
3162 if (strncmp(reason
, "cups-", 5) && !cupsArrayFind(new_reasons
, reason
))
3164 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
3166 remptr
+= strlen(remptr
);
3169 cupsArrayRemove(state_reasons
, reason
);
3173 for (reason
= (char *)cupsArrayFirst(new_reasons
);
3175 reason
= (char *)cupsArrayNext(new_reasons
))
3177 if (!cupsArrayFind(state_reasons
, reason
))
3179 cupsArrayAdd(state_reasons
, reason
);
3181 snprintf(addptr
, sizeof(add
) - (addptr
- add
), "%s%s", addprefix
,
3183 addptr
+= strlen(addptr
);
3189 _cupsMutexUnlock(&report_mutex
);
3192 * Report changes and return...
3195 if (add
[0] && rem
[0])
3196 fprintf(stderr
, "%s\n%s\n", add
, rem
);
3198 fprintf(stderr
, "%s\n", add
);
3200 fprintf(stderr
, "%s\n", rem
);
3204 * End of "$Id: ipp.c 9759 2011-05-11 03:24:33Z mike $".