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-supported",
103 "marker-high-levels",
109 "media-col-supported",
110 "multiple-document-handling-supported",
111 "operations-supported",
113 "printer-alert-description",
114 "printer-is-accepting-jobs",
116 "printer-state-message",
117 "printer-state-reasons"
119 static const char * const remote_job_states
[] =
120 { /* Remote job state keywords */
121 "+cups-remote-pending",
122 "+cups-remote-pending-held",
123 "+cups-remote-processing",
124 "+cups-remote-stopped",
125 "+cups-remote-canceled",
126 "+cups-remote-aborted",
127 "+cups-remote-completed"
129 static _cups_mutex_t report_mutex
= _CUPS_MUTEX_INITIALIZER
;
130 /* Mutex to control access */
131 static int num_attr_cache
= 0;
132 /* Number of cached attributes */
133 static cups_option_t
*attr_cache
= NULL
;
134 /* Cached attributes */
135 static cups_array_t
*state_reasons
; /* Array of printe-state-reasons keywords */
136 static char tmpfilename
[1024] = "";
137 /* Temporary spool file name */
144 static void cancel_job(http_t
*http
, const char *uri
, int id
,
145 const char *resource
, const char *user
,
147 static ipp_pstate_t
check_printer_state(http_t
*http
, const char *uri
,
148 const char *resource
,
149 const char *user
, int version
);
151 static void compress_files(int num_files
, char **files
);
152 #endif /* HAVE_LIBZ */
153 static void *monitor_printer(_cups_monitor_t
*monitor
);
154 static ipp_t
*new_request(ipp_op_t op
, int version
, const char *uri
,
155 const char *user
, const char *title
,
156 int num_options
, cups_option_t
*options
,
157 const char *compression
, int copies
,
158 const char *format
, _ppd_cache_t
*pc
,
159 ipp_attribute_t
*media_col_sup
,
160 ipp_attribute_t
*doc_handling_sup
);
161 static const char *password_cb(const char *);
162 static void report_attr(ipp_attribute_t
*attr
);
163 static void report_printer_state(ipp_t
*ipp
);
164 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
165 static int run_as_user(int argc
, char *argv
[], uid_t uid
,
166 const char *device_uri
, int fd
);
167 #endif /* HAVE_GSSAPI && HAVE_XPC */
168 static void sigterm_handler(int sig
);
169 static int timeout_cb(http_t
*http
, void *user_data
);
170 static void update_reasons(ipp_attribute_t
*attr
, const char *s
);
174 * 'main()' - Send a file to the printer or server.
178 * printer-uri job-id user title copies options [file]
181 int /* O - Exit status */
182 main(int argc
, /* I - Number of command-line args */
183 char *argv
[]) /* I - Command-line arguments */
185 int i
; /* Looping var */
186 int send_options
; /* Send job options? */
187 int num_options
; /* Number of printer options */
188 cups_option_t
*options
; /* Printer options */
189 const char *device_uri
; /* Device URI */
190 char scheme
[255], /* Scheme in URI */
191 hostname
[1024], /* Hostname */
192 username
[255], /* Username info */
193 resource
[1024], /* Resource info (printer name) */
194 addrname
[256], /* Address name */
195 *optptr
, /* Pointer to URI options */
196 *name
, /* Name of option */
197 *value
, /* Value of option */
198 sep
; /* Separator character */
199 http_addrlist_t
*addrlist
; /* Address of printer */
200 int snmp_fd
, /* SNMP socket */
201 start_count
, /* Page count via SNMP at start */
202 page_count
, /* Page count via SNMP */
203 have_supplies
; /* Printer supports supply levels? */
204 int num_files
; /* Number of files to print */
205 char **files
, /* Files to print */
206 *compatfile
= NULL
; /* Compatibility filename */
207 off_t compatsize
= 0; /* Size of compatibility file */
208 int port
; /* Port number (not used) */
209 char portname
[255]; /* Port name */
210 char uri
[HTTP_MAX_URI
]; /* Updated URI without user/pass */
211 char print_job_name
[1024]; /* Update job-name for Print-Job */
212 http_status_t http_status
; /* Status of HTTP request */
213 ipp_status_t ipp_status
; /* Status of IPP request */
214 http_t
*http
; /* HTTP connection */
215 ipp_t
*request
, /* IPP request */
216 *response
, /* IPP response */
217 *supported
; /* get-printer-attributes response */
218 time_t start_time
; /* Time of first connect */
219 int contimeout
; /* Connection timeout */
220 int delay
, /* Delay for retries */
221 prev_delay
; /* Previous delay */
222 const char *compression
; /* Compression mode */
223 int waitjob
, /* Wait for job complete? */
224 waitprinter
; /* Wait for printer ready? */
225 _cups_monitor_t monitor
; /* Monitoring data */
226 ipp_attribute_t
*job_id_attr
; /* job-id attribute */
227 int job_id
; /* job-id value */
228 ipp_attribute_t
*job_sheets
; /* job-media-sheets-completed */
229 ipp_attribute_t
*job_state
; /* job-state */
230 ipp_attribute_t
*copies_sup
; /* copies-supported */
231 ipp_attribute_t
*cups_version
; /* cups-version */
232 ipp_attribute_t
*format_sup
; /* document-format-supported */
233 ipp_attribute_t
*media_col_sup
; /* media-col-supported */
234 ipp_attribute_t
*operations_sup
; /* operations-supported */
235 ipp_attribute_t
*doc_handling_sup
; /* multiple-document-handling-supported */
236 ipp_attribute_t
*printer_state
; /* printer-state attribute */
237 ipp_attribute_t
*printer_accepting
; /* printer-is-accepting-jobs */
238 int create_job
= 0, /* Does printer support Create-Job? */
239 send_document
= 0, /* Does printer support Send-Document? */
240 validate_job
= 0; /* Does printer support Validate-Job? */
241 int copies
, /* Number of copies for job */
242 copies_remaining
; /* Number of copies remaining */
243 const char *content_type
, /* CONTENT_TYPE environment variable */
244 *final_content_type
, /* FINAL_CONTENT_TYPE environment var */
245 *document_format
; /* document-format value */
246 int fd
; /* File descriptor */
247 off_t bytes
= 0; /* Bytes copied */
248 char buffer
[16384]; /* Copy buffer */
249 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
250 struct sigaction action
; /* Actions for POSIX signals */
251 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
252 int version
; /* IPP version */
253 ppd_file_t
*ppd
; /* PPD file */
254 _ppd_cache_t
*pc
; /* PPD cache and mapping data */
255 fd_set input
; /* Input set for select() */
259 * Make sure status messages are not buffered...
262 setbuf(stderr
, NULL
);
265 * Ignore SIGPIPE and catch SIGTERM signals...
269 sigset(SIGPIPE
, SIG_IGN
);
270 sigset(SIGTERM
, sigterm_handler
);
271 #elif defined(HAVE_SIGACTION)
272 memset(&action
, 0, sizeof(action
));
273 action
.sa_handler
= SIG_IGN
;
274 sigaction(SIGPIPE
, &action
, NULL
);
276 sigemptyset(&action
.sa_mask
);
277 sigaddset(&action
.sa_mask
, SIGTERM
);
278 action
.sa_handler
= sigterm_handler
;
279 sigaction(SIGTERM
, &action
, NULL
);
281 signal(SIGPIPE
, SIG_IGN
);
282 signal(SIGTERM
, sigterm_handler
);
283 #endif /* HAVE_SIGSET */
286 * Check command-line...
293 if ((s
= strrchr(argv
[0], '/')) != NULL
)
298 printf("network %s \"Unknown\" \"%s (%s)\"\n",
299 s
, _cupsLangString(cupsLangDefault(),
300 _("Internet Printing Protocol")), s
);
301 return (CUPS_BACKEND_OK
);
305 _cupsLangPrintf(stderr
,
306 _("Usage: %s job-id user title copies options [file]"),
308 return (CUPS_BACKEND_STOP
);
312 * Get the device URI...
315 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
317 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
320 if (getenv("CLASS") != NULL
)
321 return (CUPS_BACKEND_FAILED
);
324 if ((auth_info_required
= getenv("AUTH_INFO_REQUIRED")) == NULL
)
325 auth_info_required
= "none";
327 state_reasons
= _cupsArrayNewStrings(getenv("PRINTER_STATE_REASONS"));
331 * For Kerberos, become the printing user (if we can) to get the credentials
335 if (!getuid() && (value
= getenv("AUTH_UID")) != NULL
)
337 uid_t uid
= (uid_t
)atoi(value
);
344 return (run_as_user(argc
, argv
, uid
, device_uri
, 0));
347 int status
= 0; /* Exit status */
349 for (i
= 6; i
< argc
&& !status
&& !job_canceled
; i
++)
351 if ((fd
= open(argv
[i
], O_RDONLY
)) >= 0)
353 status
= run_as_user(argc
, argv
, uid
, device_uri
, fd
);
358 _cupsLangPrintError("ERROR", _("Unable to open print file"));
359 status
= CUPS_BACKEND_FAILED
;
367 # else /* No XPC, just try to run as the user ID */
370 # endif /* HAVE_XPC */
372 #endif /* HAVE_GSSAPI */
375 * Get the (final) content type...
378 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
379 content_type
= "application/octet-stream";
381 if ((final_content_type
= getenv("FINAL_CONTENT_TYPE")) == NULL
)
383 final_content_type
= content_type
;
385 if (!strncmp(final_content_type
, "printer/", 8))
386 final_content_type
= "application/vnd.cups-raw";
390 * Extract the hostname and printer name from the URI...
393 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
394 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
395 resource
, sizeof(resource
));
398 port
= IPP_PORT
; /* Default to port 631 */
400 if (!strcmp(scheme
, "https"))
401 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
403 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
406 * See if there are any options...
413 contimeout
= 7 * 24 * 60 * 60;
415 if ((optptr
= strchr(resource
, '?')) != NULL
)
418 * Yup, terminate the device name string and move to the first
419 * character of the optptr...
425 * Then parse the optptr...
436 while (*optptr
&& *optptr
!= '=' && *optptr
!= '+' && *optptr
!= '&')
439 if ((sep
= *optptr
) != '\0')
450 while (*optptr
&& *optptr
!= '+' && *optptr
!= '&')
460 * Process the option...
463 if (!_cups_strcasecmp(name
, "waitjob"))
466 * Wait for job completion?
469 waitjob
= !_cups_strcasecmp(value
, "on") ||
470 !_cups_strcasecmp(value
, "yes") ||
471 !_cups_strcasecmp(value
, "true");
473 else if (!_cups_strcasecmp(name
, "waitprinter"))
476 * Wait for printer idle?
479 waitprinter
= !_cups_strcasecmp(value
, "on") ||
480 !_cups_strcasecmp(value
, "yes") ||
481 !_cups_strcasecmp(value
, "true");
483 else if (!_cups_strcasecmp(name
, "encryption"))
486 * Enable/disable encryption?
489 if (!_cups_strcasecmp(value
, "always"))
490 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
491 else if (!_cups_strcasecmp(value
, "required"))
492 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
493 else if (!_cups_strcasecmp(value
, "never"))
494 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
495 else if (!_cups_strcasecmp(value
, "ifrequested"))
496 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
499 _cupsLangPrintFilter(stderr
, "ERROR",
500 _("Unknown encryption option value: \"%s\"."),
504 else if (!_cups_strcasecmp(name
, "version"))
506 if (!strcmp(value
, "1.0"))
508 else if (!strcmp(value
, "1.1"))
510 else if (!strcmp(value
, "2.0"))
512 else if (!strcmp(value
, "2.1"))
514 else if (!strcmp(value
, "2.2"))
518 _cupsLangPrintFilter(stderr
, "ERROR",
519 _("Unknown version option value: \"%s\"."),
524 else if (!_cups_strcasecmp(name
, "compression"))
526 if (!_cups_strcasecmp(value
, "true") || !_cups_strcasecmp(value
, "yes") ||
527 !_cups_strcasecmp(value
, "on") || !_cups_strcasecmp(value
, "gzip"))
528 compression
= "gzip";
530 #endif /* HAVE_LIBZ */
531 else if (!_cups_strcasecmp(name
, "contimeout"))
534 * Set the connection timeout...
538 contimeout
= atoi(value
);
546 _cupsLangPrintFilter(stderr
, "ERROR",
547 _("Unknown option \"%s\" with value \"%s\"."),
554 * If we have 7 arguments, print the file named on the command-line.
555 * Otherwise, copy stdin to a temporary file and print the temporary
563 send_options
= !_cups_strcasecmp(final_content_type
, "application/pdf") ||
564 !_cups_strcasecmp(final_content_type
, "application/vnd.cups-pdf") ||
565 !_cups_strncasecmp(final_content_type
, "image/", 6);
567 fputs("DEBUG: Sending stdin for job...\n", stderr
);
572 * Point to the files on the command-line...
575 num_files
= argc
- 6;
581 compress_files(num_files
, files
);
582 #endif /* HAVE_LIBZ */
584 fprintf(stderr
, "DEBUG: %d files to send in job...\n", num_files
);
588 * Set the authentication info, if any...
591 cupsSetPasswordCB(password_cb
);
596 * Use authenticaion information in the device URI...
599 if ((password
= strchr(username
, ':')) != NULL
)
602 cupsSetUser(username
);
607 * Try loading authentication information from the environment.
610 const char *ptr
= getenv("AUTH_USERNAME");
615 password
= getenv("AUTH_PASSWORD");
619 * Try finding the remote server...
622 start_time
= time(NULL
);
624 sprintf(portname
, "%d", port
);
626 update_reasons(NULL
, "+connecting-to-device");
627 fprintf(stderr
, "DEBUG: Looking up \"%s\"...\n", hostname
);
629 while ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
631 _cupsLangPrintFilter(stderr
, "INFO",
632 _("Unable to locate printer \"%s\"."), hostname
);
635 if (getenv("CLASS") != NULL
)
637 update_reasons(NULL
, "-connecting-to-device");
638 return (CUPS_BACKEND_STOP
);
642 http
= _httpCreate(hostname
, port
, addrlist
, cupsEncryption(), AF_UNSPEC
);
643 httpSetTimeout(http
, 30.0, timeout_cb
, NULL
);
646 * See if the printer supports SNMP...
649 if ((snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
)) >= 0)
651 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
),
655 have_supplies
= start_count
= 0;
658 * Wait for data from the filter...
663 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), 0, backendNetworkSideCB
))
664 return (CUPS_BACKEND_OK
);
665 else if ((bytes
= read(0, buffer
, sizeof(buffer
))) <= 0)
666 return (CUPS_BACKEND_OK
);
670 * Try connecting to the remote server...
673 delay
= _cupsNextDelay(0, &prev_delay
);
677 fprintf(stderr
, "DEBUG: Connecting to %s:%d\n", hostname
, port
);
678 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
680 if (httpReconnect(http
))
682 int error
= errno
; /* Connection error */
684 if (http
->status
== HTTP_PKI_ERROR
)
685 update_reasons(NULL
, "+cups-certificate-error");
690 if (getenv("CLASS") != NULL
)
693 * If the CLASS environment variable is set, the job was submitted
694 * to a class and not to a specific queue. In this case, we want
695 * to abort immediately so that the job can be requeued on the next
696 * available printer in the class.
699 _cupsLangPrintFilter(stderr
, "INFO",
700 _("Unable to contact printer, queuing on next "
701 "printer in class."));
704 * Sleep 5 seconds to keep the job from requeuing too rapidly...
709 update_reasons(NULL
, "-connecting-to-device");
711 return (CUPS_BACKEND_FAILED
);
714 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
716 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
717 errno
== EHOSTUNREACH
)
719 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
721 _cupsLangPrintFilter(stderr
, "ERROR",
722 _("The printer is not responding."));
723 update_reasons(NULL
, "-connecting-to-device");
724 return (CUPS_BACKEND_FAILED
);
730 _cupsLangPrintFilter(stderr
, "WARNING",
731 _("The printer may not exist or "
732 "is unavailable at this time."));
736 _cupsLangPrintFilter(stderr
, "WARNING",
737 _("The printer is unreachable at this "
743 _cupsLangPrintFilter(stderr
, "WARNING",
744 _("The printer is busy."));
750 delay
= _cupsNextDelay(delay
, &prev_delay
);
754 _cupsLangPrintFilter(stderr
, "ERROR",
755 _("The printer is not responding."));
763 update_reasons(NULL
, "-cups-certificate-error");
765 while (http
->fd
< 0);
767 if (job_canceled
|| !http
)
768 return (CUPS_BACKEND_FAILED
);
770 update_reasons(NULL
, "-connecting-to-device");
771 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
773 fprintf(stderr
, "DEBUG: Connected to %s:%d...\n",
774 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
775 _httpAddrPort(http
->hostaddr
));
778 * Build a URI for the printer and fill the standard IPP attributes for
779 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
780 * might contain username:password information...
783 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), scheme
, NULL
, hostname
,
787 * First validate the destination and see if the device supports multiple
794 media_col_sup
= NULL
;
796 operations_sup
= NULL
;
797 doc_handling_sup
= NULL
;
802 * Check for side-channel requests...
805 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
808 * Build the IPP request...
811 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
812 request
->request
.op
.version
[0] = version
/ 10;
813 request
->request
.op
.version
[1] = version
% 10;
815 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
818 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
819 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
826 fputs("DEBUG: Getting supported attributes...\n", stderr
);
828 if (http
->version
< HTTP_1_1
)
830 fprintf(stderr
, "DEBUG: Printer responded with HTTP version %d.%d.\n",
831 http
->version
/ 100, http
->version
% 100);
832 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
833 "cups-ipp-wrong-http-version");
836 supported
= cupsDoRequest(http
, request
, resource
);
837 ipp_status
= cupsLastError();
839 fprintf(stderr
, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
840 ippErrorString(ipp_status
), cupsLastErrorString());
842 if (ipp_status
> IPP_OK_CONFLICT
)
844 fprintf(stderr
, "DEBUG: Get-Printer-Attributes returned %s.\n",
845 ippErrorString(ipp_status
));
847 if (ipp_status
== IPP_PRINTER_BUSY
||
848 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
850 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
852 _cupsLangPrintFilter(stderr
, "ERROR",
853 _("The printer is not responding."));
854 return (CUPS_BACKEND_FAILED
);
857 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is busy."));
859 report_printer_state(supported
);
863 delay
= _cupsNextDelay(delay
, &prev_delay
);
865 else if ((ipp_status
== IPP_BAD_REQUEST
||
866 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
> 10)
869 * Switch to IPP/1.1 or IPP/1.0...
874 _cupsLangPrintFilter(stderr
, "INFO",
875 _("Printer does not support IPP/%d.%d, trying "
876 "IPP/%s."), version
/ 10, version
% 10, "1.1");
881 _cupsLangPrintFilter(stderr
, "INFO",
882 _("Printer does not support IPP/%d.%d, trying "
883 "IPP/%s."), version
/ 10, version
% 10, "1.0");
889 else if (ipp_status
== IPP_NOT_FOUND
)
891 _cupsLangPrintFilter(stderr
, "ERROR",
892 _("The printer URI is incorrect or no longer "
895 ippDelete(supported
);
897 return (CUPS_BACKEND_STOP
);
899 else if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
)
901 if (!strncmp(httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
),
903 auth_info_required
= "negotiate";
905 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
906 return (CUPS_BACKEND_AUTH_REQUIRED
);
910 _cupsLangPrintFilter(stderr
, "ERROR",
911 _("Unable to get printer status."));
915 ippDelete(supported
);
920 if (!getenv("CLASS"))
923 * Check printer-is-accepting-jobs = false and printer-state-reasons for the
924 * "spool-area-full" keyword...
929 if ((printer_accepting
= ippFindAttribute(supported
,
930 "printer-is-accepting-jobs",
931 IPP_TAG_BOOLEAN
)) != NULL
&&
932 !printer_accepting
->values
[0].boolean
)
934 else if (!printer_accepting
)
935 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
936 "cups-ipp-missing-printer-is-accepting-jobs");
938 if ((printer_state
= ippFindAttribute(supported
,
939 "printer-state-reasons",
940 IPP_TAG_KEYWORD
)) != NULL
&& !busy
)
942 for (i
= 0; i
< printer_state
->num_values
; i
++)
943 if (!strcmp(printer_state
->values
[0].string
.text
,
944 "spool-area-full") ||
945 !strncmp(printer_state
->values
[0].string
.text
, "spool-area-full-",
953 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
954 "cups-ipp-missing-printer-state-reasons");
958 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is busy."));
960 report_printer_state(supported
);
964 delay
= _cupsNextDelay(delay
, &prev_delay
);
966 ippDelete(supported
);
973 * Check for supported attributes...
976 if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
977 IPP_TAG_RANGE
)) != NULL
)
980 * Has the "copies-supported" attribute - does it have an upper
984 fprintf(stderr
, "DEBUG: copies-supported=%d-%d\n",
985 copies_sup
->values
[0].range
.lower
,
986 copies_sup
->values
[0].range
.upper
);
988 if (copies_sup
->values
[0].range
.upper
<= 1)
989 copies_sup
= NULL
; /* No */
992 cups_version
= ippFindAttribute(supported
, "cups-version", IPP_TAG_TEXT
);
994 if ((format_sup
= ippFindAttribute(supported
, "document-format-supported",
995 IPP_TAG_MIMETYPE
)) != NULL
)
997 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
998 format_sup
->num_values
);
999 for (i
= 0; i
< format_sup
->num_values
; i
++)
1000 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
1001 format_sup
->values
[i
].string
.text
);
1004 if ((media_col_sup
= ippFindAttribute(supported
, "media-col-supported",
1005 IPP_TAG_KEYWORD
)) != NULL
)
1007 fprintf(stderr
, "DEBUG: media-col-supported (%d values)\n",
1008 media_col_sup
->num_values
);
1009 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
1010 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
1011 media_col_sup
->values
[i
].string
.text
);
1014 if ((operations_sup
= ippFindAttribute(supported
, "operations-supported",
1015 IPP_TAG_ENUM
)) != NULL
)
1017 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1018 if (operations_sup
->values
[i
].integer
== IPP_PRINT_JOB
)
1021 if (i
>= operations_sup
->num_values
)
1022 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1023 "cups-ipp-missing-print-job");
1025 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1026 if (operations_sup
->values
[i
].integer
== IPP_CANCEL_JOB
)
1029 if (i
>= operations_sup
->num_values
)
1030 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1031 "cups-ipp-missing-cancel-job");
1033 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1034 if (operations_sup
->values
[i
].integer
== IPP_GET_JOB_ATTRIBUTES
)
1037 if (i
>= operations_sup
->num_values
)
1038 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1039 "cups-ipp-missing-get-job-attributes");
1041 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1042 if (operations_sup
->values
[i
].integer
== IPP_GET_PRINTER_ATTRIBUTES
)
1045 if (i
>= operations_sup
->num_values
)
1046 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1047 "cups-ipp-missing-get-printer-attributes");
1049 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1051 if (operations_sup
->values
[i
].integer
== IPP_VALIDATE_JOB
)
1053 else if (operations_sup
->values
[i
].integer
== IPP_CREATE_JOB
)
1055 else if (operations_sup
->values
[i
].integer
== IPP_SEND_DOCUMENT
)
1061 fputs("DEBUG: Printer supports Create-Job but not Send-Document.\n",
1067 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1068 "cups-ipp-missing-validate-job");
1071 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1072 "cups-ipp-missing-operations-supported");
1074 doc_handling_sup
= ippFindAttribute(supported
,
1075 "multiple-document-handling-supported",
1078 report_printer_state(supported
);
1080 while (ipp_status
> IPP_OK_CONFLICT
);
1083 * See if the printer is accepting jobs and is not stopped; if either
1084 * condition is true and we are printing to a class, requeue the job...
1087 if (getenv("CLASS") != NULL
)
1089 printer_state
= ippFindAttribute(supported
, "printer-state",
1091 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
1094 if (printer_state
== NULL
||
1095 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
1097 printer_accepting
== NULL
||
1098 !printer_accepting
->values
[0].boolean
)
1101 * If the CLASS environment variable is set, the job was submitted
1102 * to a class and not to a specific queue. In this case, we want
1103 * to abort immediately so that the job can be requeued on the next
1104 * available printer in the class.
1107 _cupsLangPrintFilter(stderr
, "INFO",
1108 _("Unable to contact printer, queuing on next "
1109 "printer in class."));
1111 ippDelete(supported
);
1115 * Sleep 5 seconds to keep the job from requeuing too rapidly...
1120 return (CUPS_BACKEND_FAILED
);
1125 * See if the printer supports multiple copies...
1128 copies
= atoi(argv
[4]);
1130 if (copies_sup
|| argc
< 7)
1132 copies_remaining
= 1;
1134 if (argc
< 7 && !_cups_strncasecmp(final_content_type
, "image/", 6))
1138 copies_remaining
= copies
;
1141 * Prepare remaining printing options...
1149 num_options
= cupsParseOptions(argv
[5], 0, &options
);
1151 if (!cups_version
&& media_col_sup
)
1154 * Load the PPD file and generate PWG attribute mapping information...
1157 ppd
= ppdOpenFile(getenv("PPD"));
1158 pc
= _ppdCacheCreateWithPPD(ppd
);
1166 document_format
= NULL
;
1168 if (format_sup
!= NULL
)
1170 for (i
= 0; i
< format_sup
->num_values
; i
++)
1171 if (!_cups_strcasecmp(final_content_type
, format_sup
->values
[i
].string
.text
))
1173 document_format
= final_content_type
;
1177 if (!document_format
)
1179 for (i
= 0; i
< format_sup
->num_values
; i
++)
1180 if (!_cups_strcasecmp("application/octet-stream",
1181 format_sup
->values
[i
].string
.text
))
1183 document_format
= "application/octet-stream";
1190 * If the printer does not support HTTP/1.1 (which IPP requires), copy stdin
1191 * to a temporary file so that we can do a HTTP/1.0 submission...
1193 * (I hate compatibility hacks!)
1196 if (http
->version
< HTTP_1_1
&& num_files
== 0)
1198 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
1200 perror("DEBUG: Unable to create temporary file");
1201 return (CUPS_BACKEND_FAILED
);
1204 _cupsLangPrintFilter(stderr
, "INFO", _("Copying print data."));
1206 compatsize
= backendRunLoop(-1, fd
, snmp_fd
, &(addrlist
->addr
), 0, 0,
1207 backendNetworkSideCB
);
1211 compatfile
= tmpfilename
;
1212 files
= &compatfile
;
1215 else if (http
->version
< HTTP_1_1
&& num_files
== 1)
1217 struct stat fileinfo
; /* File information */
1219 if (!stat(files
[0], &fileinfo
))
1220 compatsize
= fileinfo
.st_size
;
1224 * Start monitoring the printer in the background...
1228 monitor
.hostname
= hostname
;
1229 monitor
.user
= argv
[2];
1230 monitor
.resource
= resource
;
1231 monitor
.port
= port
;
1232 monitor
.version
= version
;
1234 monitor
.encryption
= cupsEncryption();
1235 monitor
.job_state
= IPP_JOB_PENDING
;
1236 monitor
.printer_state
= IPP_PRINTER_IDLE
;
1240 monitor
.job_name
= argv
[3];
1244 snprintf(print_job_name
, sizeof(print_job_name
), "%s - %s", argv
[1],
1246 monitor
.job_name
= print_job_name
;
1249 _cupsThreadCreate((_cups_thread_func_t
)monitor_printer
, &monitor
);
1252 * Validate access to the printer...
1255 while (!job_canceled
&& validate_job
)
1257 request
= new_request(IPP_VALIDATE_JOB
, version
, uri
, argv
[2],
1258 monitor
.job_name
, num_options
, options
, compression
,
1259 copies_sup
? copies
: 1, document_format
, pc
,
1260 media_col_sup
, doc_handling_sup
);
1262 ippDelete(cupsDoRequest(http
, request
, resource
));
1264 ipp_status
= cupsLastError();
1266 fprintf(stderr
, "DEBUG: Validate-Job: %s (%s)\n",
1267 ippErrorString(ipp_status
), cupsLastErrorString());
1272 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
|| ipp_status
== IPP_PRINTER_BUSY
)
1274 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is busy."));
1277 else if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
1278 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1281 * Update auth-info-required as needed...
1284 fprintf(stderr
, "DEBUG: WWW-Authenticate=\"%s\"\n",
1285 httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
));
1288 * Normal authentication goes through the password callback, which sets
1289 * auth_info_required to "username,password". Kerberos goes directly
1290 * through GSSAPI, so look for Negotiate in the WWW-Authenticate header
1291 * here and set auth_info_required as needed...
1294 if (!strncmp(httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
),
1296 auth_info_required
= "negotiate";
1300 else if (ipp_status
== IPP_OPERATION_NOT_SUPPORTED
)
1303 * This is all too common...
1306 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1307 "cups-ipp-missing-validate-job");
1310 else if (ipp_status
< IPP_REDIRECTION_OTHER_SITE
)
1315 * Then issue the print-job request...
1320 while (!job_canceled
&& copies_remaining
> 0)
1323 * Check for side-channel requests...
1326 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1329 * Build the IPP job creation request...
1335 request
= new_request((num_files
> 1 || create_job
) ? IPP_CREATE_JOB
:
1337 version
, uri
, argv
[2], monitor
.job_name
, num_options
,
1338 options
, compression
, copies_sup
? copies
: 1,
1339 document_format
, pc
, media_col_sup
, doc_handling_sup
);
1345 if (num_files
> 1 || create_job
)
1346 response
= cupsDoRequest(http
, request
, resource
);
1349 size_t length
= 0; /* Length of request */
1353 fputs("DEBUG: Sending file using HTTP/1.0 Content-Length...\n", stderr
);
1354 length
= ippLength(request
) + (size_t)compatsize
;
1357 fputs("DEBUG: Sending file using HTTP/1.1 chunking...\n", stderr
);
1359 http_status
= cupsSendRequest(http
, request
, resource
, length
);
1360 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
)
1364 if ((fd
= open(files
[0], O_RDONLY
)) < 0)
1366 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1367 return (CUPS_BACKEND_FAILED
);
1373 http_status
= cupsWriteRequestData(http
, buffer
, bytes
);
1376 while (http_status
== HTTP_CONTINUE
&&
1377 (!job_canceled
|| compatsize
> 0))
1380 * Check for side-channel requests and more print data...
1385 FD_SET(snmp_fd
, &input
);
1387 while (select(fd
> snmp_fd
? fd
+ 1 : snmp_fd
+ 1, &input
, NULL
, NULL
,
1388 NULL
) <= 0 && !job_canceled
);
1390 if (FD_ISSET(snmp_fd
, &input
))
1391 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1393 if (FD_ISSET(fd
, &input
))
1395 if ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1397 fprintf(stderr
, "DEBUG: Read %d bytes...\n", (int)bytes
);
1399 if (cupsWriteRequestData(http
, buffer
, bytes
) != HTTP_CONTINUE
)
1402 else if (bytes
== 0 || (errno
!= EINTR
&& errno
!= EAGAIN
))
1411 response
= cupsGetResponse(http
, resource
);
1415 ipp_status
= cupsLastError();
1417 fprintf(stderr
, "DEBUG: %s: %s (%s)\n",
1418 (num_files
> 1 || create_job
) ? "Create-Job" : "Print-Job",
1419 ippErrorString(ipp_status
), cupsLastErrorString());
1421 if (ipp_status
> IPP_OK_CONFLICT
)
1428 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1429 ipp_status
== IPP_NOT_POSSIBLE
||
1430 ipp_status
== IPP_PRINTER_BUSY
)
1432 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is busy."));
1438 * We can't re-submit when we have no files to print, so exit
1439 * immediately with the right status code...
1445 else if (ipp_status
== IPP_ERROR_JOB_CANCELED
)
1450 * Update auth-info-required as needed...
1453 _cupsLangPrintFilter(stderr
, "ERROR",
1454 _("Print file was not accepted."));
1456 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
)
1458 fprintf(stderr
, "DEBUG: WWW-Authenticate=\"%s\"\n",
1459 httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
));
1462 * Normal authentication goes through the password callback, which sets
1463 * auth_info_required to "username,password". Kerberos goes directly
1464 * through GSSAPI, so look for Negotiate in the WWW-Authenticate header
1465 * here and set auth_info_required as needed...
1468 if (!strncmp(httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
),
1470 auth_info_required
= "negotiate";
1478 * We can't re-submit when we have no files to print, so exit
1479 * immediately with the right status code...
1486 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
1487 IPP_TAG_INTEGER
)) == NULL
)
1489 _cupsLangPrintFilter(stderr
, "INFO",
1490 _("Print file accepted - job ID unknown."));
1491 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1492 "cups-ipp-missing-job-id");
1497 monitor
.job_id
= job_id
= job_id_attr
->values
[0].integer
;
1498 _cupsLangPrintFilter(stderr
, "INFO",
1499 _("Print file accepted - job ID %d."), job_id
);
1502 fprintf(stderr
, "DEBUG: job-id=%d\n", job_id
);
1503 ippDelete(response
);
1508 if (job_id
&& (num_files
> 1 || create_job
))
1510 for (i
= 0; num_files
== 0 || i
< num_files
; i
++)
1513 * Check for side-channel requests...
1516 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1519 * Send the next file in the job...
1522 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
1523 request
->request
.op
.version
[0] = version
/ 10;
1524 request
->request
.op
.version
[1] = version
% 10;
1526 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1529 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1533 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1534 "requesting-user-name", NULL
, argv
[2]);
1536 if ((i
+ 1) >= num_files
)
1537 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
1539 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1540 "document-format", NULL
, document_format
);
1542 fprintf(stderr
, "DEBUG: Sending file %d using chunking...\n", i
+ 1);
1543 http_status
= cupsSendRequest(http
, request
, resource
, 0);
1544 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
)
1549 http_status
= cupsWriteRequestData(http
, buffer
, bytes
);
1553 if ((fd
= open(files
[i
], O_RDONLY
)) < 0)
1555 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1556 return (CUPS_BACKEND_FAILED
);
1565 while (!job_canceled
&&
1566 (bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1568 if (cupsWriteRequestData(http
, buffer
, bytes
) != HTTP_CONTINUE
)
1573 * Check for side-channel requests...
1576 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1584 ippDelete(cupsGetResponse(http
, resource
));
1587 fprintf(stderr
, "DEBUG: Send-Document: %s (%s)\n",
1588 ippErrorString(cupsLastError()), cupsLastErrorString());
1590 if (cupsLastError() > IPP_OK_CONFLICT
)
1592 ipp_status
= cupsLastError();
1594 _cupsLangPrintFilter(stderr
, "ERROR",
1595 _("Unable to add document to print job."));
1598 else if (num_files
== 0 || fd
< 0)
1603 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
1605 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
1606 copies_remaining
--;
1608 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1609 ipp_status
== IPP_NOT_POSSIBLE
||
1610 ipp_status
== IPP_PRINTER_BUSY
)
1613 copies_remaining
--;
1616 * Wait for the job to complete...
1619 if (!job_id
|| !waitjob
)
1622 _cupsLangPrintFilter(stderr
, "INFO", _("Waiting for job to complete."));
1624 for (delay
= _cupsNextDelay(0, &prev_delay
); !job_canceled
;)
1627 * Check for side-channel requests...
1630 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1633 * Build an IPP_GET_JOB_ATTRIBUTES request...
1636 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1637 request
->request
.op
.version
[0] = version
/ 10;
1638 request
->request
.op
.version
[1] = version
% 10;
1640 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1643 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1647 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1648 "requesting-user-name", NULL
, argv
[2]);
1650 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1651 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
1658 httpReconnect(http
);
1659 response
= cupsDoRequest(http
, request
, resource
);
1660 ipp_status
= cupsLastError();
1662 if (ipp_status
== IPP_NOT_FOUND
)
1665 * Job has gone away and/or the server has no job history...
1668 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1669 "cups-ipp-missing-job-history");
1670 ippDelete(response
);
1672 ipp_status
= IPP_OK
;
1676 fprintf(stderr
, "DEBUG: Get-Job-Attributes: %s (%s)\n",
1677 ippErrorString(ipp_status
), cupsLastErrorString());
1679 if (ipp_status
> IPP_OK_CONFLICT
)
1681 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1682 ipp_status
!= IPP_NOT_POSSIBLE
&&
1683 ipp_status
!= IPP_PRINTER_BUSY
)
1685 ippDelete(response
);
1687 _cupsLangPrintFilter(stderr
, "ERROR",
1688 _("Unable to get print job status."));
1695 if ((job_state
= ippFindAttribute(response
, "job-state",
1696 IPP_TAG_ENUM
)) != NULL
)
1699 * Reflect the remote job state in the local queue...
1703 job_state
->values
[0].integer
>= IPP_JOB_PENDING
&&
1704 job_state
->values
[0].integer
<= IPP_JOB_COMPLETED
)
1705 update_reasons(NULL
,
1706 remote_job_states
[job_state
->values
[0].integer
-
1709 if ((job_sheets
= ippFindAttribute(response
,
1710 "job-media-sheets-completed",
1711 IPP_TAG_INTEGER
)) == NULL
)
1712 job_sheets
= ippFindAttribute(response
,
1713 "job-impressions-completed",
1717 fprintf(stderr
, "PAGE: total %d\n",
1718 job_sheets
->values
[0].integer
);
1721 * Stop polling if the job is finished or pending-held...
1724 if (job_state
->values
[0].integer
> IPP_JOB_STOPPED
)
1726 ippDelete(response
);
1730 else if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1731 ipp_status
!= IPP_NOT_POSSIBLE
&&
1732 ipp_status
!= IPP_PRINTER_BUSY
)
1735 * If the printer does not return a job-state attribute, it does not
1736 * conform to the IPP specification - break out immediately and fail
1740 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1741 "cups-ipp-missing-job-state");
1742 ipp_status
= IPP_INTERNAL_ERROR
;
1747 ippDelete(response
);
1750 * Wait before polling again...
1755 delay
= _cupsNextDelay(delay
, &prev_delay
);
1760 * Cancel the job as needed...
1763 if (job_canceled
&& job_id
)
1764 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1767 * Check the printer state and report it if necessary...
1770 check_printer_state(http
, uri
, resource
, argv
[2], version
);
1773 * Collect the final page count as needed...
1776 if (have_supplies
&&
1777 !backendSNMPSupplies(snmp_fd
, http
->hostaddr
, &page_count
, NULL
) &&
1778 page_count
> start_count
)
1779 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
1783 * See if we used Kerberos at all...
1787 auth_info_required
= "negotiate";
1788 #endif /* HAVE_GSSAPI */
1796 cupsFreeOptions(num_options
, options
);
1797 _ppdCacheDestroy(pc
);
1801 ippDelete(supported
);
1804 * Remove the temporary file(s) if necessary...
1808 unlink(tmpfilename
);
1813 for (i
= 0; i
< num_files
; i
++)
1816 #endif /* HAVE_LIBZ */
1819 * Return the queue status...
1822 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
1823 ipp_status
== IPP_AUTHENTICATION_CANCELED
||
1824 ipp_status
<= IPP_OK_CONFLICT
)
1825 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
1827 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
1828 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1829 return (CUPS_BACKEND_AUTH_REQUIRED
);
1830 else if (ipp_status
== IPP_INTERNAL_ERROR
)
1831 return (CUPS_BACKEND_STOP
);
1832 else if (ipp_status
== IPP_DOCUMENT_FORMAT
||
1833 ipp_status
== IPP_CONFLICT
)
1834 return (CUPS_BACKEND_FAILED
);
1835 else if (ipp_status
> IPP_OK_CONFLICT
&& ipp_status
!= IPP_ERROR_JOB_CANCELED
)
1836 return (CUPS_BACKEND_RETRY_CURRENT
);
1839 _cupsLangPrintFilter(stderr
, "INFO", _("Ready to print."));
1840 return (CUPS_BACKEND_OK
);
1846 * 'cancel_job()' - Cancel a print job.
1850 cancel_job(http_t
*http
, /* I - HTTP connection */
1851 const char *uri
, /* I - printer-uri */
1852 int id
, /* I - job-id */
1853 const char *resource
, /* I - Resource path */
1854 const char *user
, /* I - requesting-user-name */
1855 int version
) /* I - IPP version */
1857 ipp_t
*request
; /* Cancel-Job request */
1860 _cupsLangPrintFilter(stderr
, "INFO", _("Canceling print job."));
1862 request
= ippNewRequest(IPP_CANCEL_JOB
);
1863 request
->request
.op
.version
[0] = version
/ 10;
1864 request
->request
.op
.version
[1] = version
% 10;
1866 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1868 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1870 if (user
&& user
[0])
1871 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1872 "requesting-user-name", NULL
, user
);
1878 ippDelete(cupsDoRequest(http
, request
, resource
));
1880 if (cupsLastError() > IPP_OK_CONFLICT
)
1881 _cupsLangPrintFilter(stderr
, "ERROR", _("Unable to cancel print job."));
1886 * 'check_printer_state()' - Check the printer state.
1889 static ipp_pstate_t
/* O - Current printer-state */
1890 check_printer_state(
1891 http_t
*http
, /* I - HTTP connection */
1892 const char *uri
, /* I - Printer URI */
1893 const char *resource
, /* I - Resource path */
1894 const char *user
, /* I - Username, if any */
1895 int version
) /* I - IPP version */
1897 ipp_t
*request
, /* IPP request */
1898 *response
; /* IPP response */
1899 ipp_attribute_t
*attr
; /* Attribute in response */
1900 ipp_pstate_t printer_state
= IPP_PRINTER_STOPPED
;
1901 /* Current printer-state */
1905 * Send a Get-Printer-Attributes request and log the results...
1908 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1909 request
->request
.op
.version
[0] = version
/ 10;
1910 request
->request
.op
.version
[1] = version
% 10;
1912 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1915 if (user
&& user
[0])
1916 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1917 "requesting-user-name", NULL
, user
);
1919 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1920 "requested-attributes",
1921 (int)(sizeof(pattrs
) / sizeof(pattrs
[0])), NULL
, pattrs
);
1923 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
1925 report_printer_state(response
);
1927 if ((attr
= ippFindAttribute(response
, "printer-state",
1928 IPP_TAG_ENUM
)) != NULL
)
1929 printer_state
= (ipp_pstate_t
)attr
->values
[0].integer
;
1931 ippDelete(response
);
1934 fprintf(stderr
, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
1935 ippErrorString(cupsLastError()), cupsLastErrorString());
1938 * Return the printer-state value...
1941 return (printer_state
);
1947 * 'compress_files()' - Compress print files.
1951 compress_files(int num_files
, /* I - Number of files */
1952 char **files
) /* I - Files */
1954 int i
, /* Looping var */
1955 fd
; /* Temporary file descriptor */
1956 ssize_t bytes
; /* Bytes read/written */
1957 size_t total
; /* Total bytes read */
1958 cups_file_t
*in
, /* Input file */
1959 *out
; /* Output file */
1960 struct stat outinfo
; /* Output file information */
1961 char filename
[1024], /* Temporary filename */
1962 buffer
[32768]; /* Copy buffer */
1965 fprintf(stderr
, "DEBUG: Compressing %d job files...\n", num_files
);
1966 for (i
= 0; i
< num_files
; i
++)
1968 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
1970 _cupsLangPrintError("ERROR", _("Unable to create compressed print file"));
1971 exit(CUPS_BACKEND_FAILED
);
1974 if ((out
= cupsFileOpenFd(fd
, "w9")) == NULL
)
1976 _cupsLangPrintError("ERROR", _("Unable to open compressed print file"));
1977 exit(CUPS_BACKEND_FAILED
);
1980 if ((in
= cupsFileOpen(files
[i
], "r")) == NULL
)
1982 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1984 exit(CUPS_BACKEND_FAILED
);
1988 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
1989 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
1991 _cupsLangPrintError("ERROR",
1992 _("Unable to generate compressed print file"));
1995 exit(CUPS_BACKEND_FAILED
);
2003 files
[i
] = strdup(filename
);
2005 if (!stat(filename
, &outinfo
))
2007 "DEBUG: File %d compressed to %.1f%% of original size, "
2008 CUPS_LLFMT
" bytes...\n",
2009 i
+ 1, 100.0 * outinfo
.st_size
/ total
,
2010 CUPS_LLCAST outinfo
.st_size
);
2013 #endif /* HAVE_LIBZ */
2017 * 'monitor_printer()' - Monitor the printer state.
2020 static void * /* O - Thread exit code */
2022 _cups_monitor_t
*monitor
) /* I - Monitoring data */
2024 http_t
*http
; /* Connection to printer */
2025 ipp_t
*request
, /* IPP request */
2026 *response
; /* IPP response */
2027 ipp_attribute_t
*attr
; /* Attribute in response */
2028 int delay
, /* Current delay */
2029 prev_delay
; /* Previous delay */
2030 ipp_op_t job_op
; /* Operation to use */
2031 int job_id
; /* Job ID */
2032 const char *job_name
; /* Job name */
2033 ipp_jstate_t job_state
; /* Job state */
2034 const char *job_user
; /* Job originating user name */
2038 * Make a copy of the printer connection...
2041 http
= _httpCreate(monitor
->hostname
, monitor
->port
, NULL
, monitor
->encryption
,
2043 httpSetTimeout(http
, 30.0, timeout_cb
, NULL
);
2044 cupsSetPasswordCB(password_cb
);
2047 * Loop until the job is canceled, aborted, or completed.
2050 delay
= _cupsNextDelay(0, &prev_delay
);
2052 while (monitor
->job_state
< IPP_JOB_CANCELED
&& !job_canceled
)
2055 * Reconnect to the printer...
2058 if (!httpReconnect(http
))
2061 * Connected, so check on the printer state...
2064 monitor
->printer_state
= check_printer_state(http
, monitor
->uri
,
2070 * Check the status of the job itself...
2073 job_op
= monitor
->job_id
> 0 ? IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
;
2074 request
= ippNewRequest(job_op
);
2075 request
->request
.op
.version
[0] = monitor
->version
/ 10;
2076 request
->request
.op
.version
[1] = monitor
->version
% 10;
2078 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2079 NULL
, monitor
->uri
);
2080 if (job_op
== IPP_GET_JOB_ATTRIBUTES
)
2081 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
2084 if (monitor
->user
&& monitor
->user
[0])
2085 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2086 "requesting-user-name", NULL
, monitor
->user
);
2088 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2089 "requested-attributes",
2090 (int)(sizeof(jattrs
) / sizeof(jattrs
[0])), NULL
, jattrs
);
2096 response
= cupsDoRequest(http
, request
, monitor
->resource
);
2098 fprintf(stderr
, "DEBUG: %s: %s (%s)\n", ippOpString(job_op
),
2099 ippErrorString(cupsLastError()), cupsLastErrorString());
2101 if (job_op
== IPP_GET_JOB_ATTRIBUTES
)
2103 if ((attr
= ippFindAttribute(response
, "job-state",
2104 IPP_TAG_ENUM
)) != NULL
)
2105 monitor
->job_state
= (ipp_jstate_t
)attr
->values
[0].integer
;
2107 monitor
->job_state
= IPP_JOB_COMPLETED
;
2111 for (attr
= response
->attrs
; attr
; attr
= attr
->next
)
2115 job_state
= IPP_JOB_PENDING
;
2118 while (attr
&& attr
->group_tag
!= IPP_TAG_JOB
)
2124 while (attr
&& attr
->group_tag
== IPP_TAG_JOB
)
2126 if (!strcmp(attr
->name
, "job-id") &&
2127 attr
->value_tag
== IPP_TAG_INTEGER
)
2128 job_id
= attr
->values
[0].integer
;
2129 else if (!strcmp(attr
->name
, "job-name") &&
2130 (attr
->value_tag
== IPP_TAG_NAME
||
2131 attr
->value_tag
== IPP_TAG_NAMELANG
))
2132 job_name
= attr
->values
[0].string
.text
;
2133 else if (!strcmp(attr
->name
, "job-state") &&
2134 attr
->value_tag
== IPP_TAG_ENUM
)
2135 job_state
= attr
->values
[0].integer
;
2136 else if (!strcmp(attr
->name
, "job-originating-user-name") &&
2137 (attr
->value_tag
== IPP_TAG_NAME
||
2138 attr
->value_tag
== IPP_TAG_NAMELANG
))
2139 job_user
= attr
->values
[0].string
.text
;
2144 if (job_id
> 0 && job_name
&& !strcmp(job_name
, monitor
->job_name
) &&
2145 job_user
&& monitor
->user
&& !strcmp(job_user
, monitor
->user
))
2147 monitor
->job_id
= job_id
;
2148 monitor
->job_state
= job_state
;
2157 ippDelete(response
);
2160 * Disconnect from the printer - we'll reconnect on the next poll...
2163 _httpDisconnect(http
);
2167 * Sleep for N seconds...
2172 delay
= _cupsNextDelay(delay
, &prev_delay
);
2176 * Cancel the job if necessary...
2179 if (job_canceled
&& monitor
->job_id
> 0)
2180 if (!httpReconnect(http
))
2181 cancel_job(http
, monitor
->uri
, monitor
->job_id
, monitor
->resource
,
2182 monitor
->user
, monitor
->version
);
2185 * Cleanup and return...
2195 * 'new_request()' - Create a new print creation or validation request.
2198 static ipp_t
* /* O - Request data */
2200 ipp_op_t op
, /* I - IPP operation code */
2201 int version
, /* I - IPP version number */
2202 const char *uri
, /* I - printer-uri value */
2203 const char *user
, /* I - requesting-user-name value */
2204 const char *title
, /* I - job-name value */
2205 int num_options
, /* I - Number of options to send */
2206 cups_option_t
*options
, /* I - Options to send */
2207 const char *compression
, /* I - compression value or NULL */
2208 int copies
, /* I - copies value or 0 */
2209 const char *format
, /* I - document-format value or NULL */
2210 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2211 ipp_attribute_t
*media_col_sup
, /* I - media-col-supported values */
2212 ipp_attribute_t
*doc_handling_sup
) /* I - multiple-document-handling-supported values */
2214 int i
; /* Looping var */
2215 ipp_t
*request
; /* Request data */
2216 const char *keyword
; /* PWG keyword */
2217 _pwg_size_t
*size
; /* PWG media size */
2218 ipp_t
*media_col
, /* media-col value */
2219 *media_size
; /* media-size value */
2220 const char *media_source
, /* media-source value */
2221 *media_type
, /* media-type value */
2222 *collate_str
; /* multiple-document-handling value */
2226 * Create the IPP request...
2229 request
= ippNewRequest(op
);
2230 request
->request
.op
.version
[0] = version
/ 10;
2231 request
->request
.op
.version
[1] = version
% 10;
2233 fprintf(stderr
, "DEBUG: %s IPP/%d.%d\n",
2234 ippOpString(request
->request
.op
.operation_id
),
2235 request
->request
.op
.version
[0],
2236 request
->request
.op
.version
[1]);
2239 * Add standard attributes...
2242 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2244 fprintf(stderr
, "DEBUG: printer-uri=\"%s\"\n", uri
);
2248 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2249 "requesting-user-name", NULL
, user
);
2250 fprintf(stderr
, "DEBUG: requesting-user-name=\"%s\"\n", user
);
2253 if (title
&& *title
)
2255 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
2257 fprintf(stderr
, "DEBUG: job-name=\"%s\"\n", title
);
2262 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
2263 "document-format", NULL
, format
);
2264 fprintf(stderr
, "DEBUG: document-format=\"%s\"\n", format
);
2270 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2271 "compression", NULL
, compression
);
2272 fprintf(stderr
, "DEBUG: compression=\"%s\"\n", compression
);
2274 #endif /* HAVE_LIBZ */
2277 * Handle options on the command-line...
2280 if (num_options
> 0)
2284 int num_finishings
= 0, /* Number of finishing values */
2285 finishings
[10]; /* Finishing enum values */
2288 * Send standard IPP attributes...
2291 if ((keyword
= cupsGetOption("PageSize", num_options
, options
)) == NULL
)
2292 keyword
= cupsGetOption("media", num_options
, options
);
2294 if ((size
= _ppdCacheGetSize(pc
, keyword
)) != NULL
)
2297 * Add a media-col value...
2300 media_size
= ippNew();
2301 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2302 "x-dimension", size
->width
);
2303 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2304 "y-dimension", size
->length
);
2306 media_col
= ippNew();
2307 ippAddCollection(media_col
, IPP_TAG_ZERO
, "media-size", media_size
);
2309 media_source
= _ppdCacheGetSource(pc
, cupsGetOption("InputSlot",
2312 media_type
= _ppdCacheGetType(pc
, cupsGetOption("MediaType",
2316 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
2318 if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2319 "media-left-margin"))
2320 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2321 "media-left-margin", size
->left
);
2322 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2323 "media-bottom-margin"))
2324 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2325 "media-bottom-margin", size
->bottom
);
2326 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2327 "media-right-margin"))
2328 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2329 "media-right-margin", size
->right
);
2330 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2331 "media-top-margin"))
2332 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2333 "media-top-margin", size
->top
);
2334 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2335 "media-source") && media_source
)
2336 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
2337 "media-source", NULL
, media_source
);
2338 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2339 "media-type") && media_type
)
2340 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
2341 "media-type", NULL
, media_type
);
2344 ippAddCollection(request
, IPP_TAG_JOB
, "media-col", media_col
);
2347 if ((keyword
= cupsGetOption("output-bin", num_options
,
2349 keyword
= _ppdCacheGetBin(pc
, cupsGetOption("OutputBin", num_options
,
2353 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-bin",
2356 if ((keyword
= cupsGetOption("output-mode", num_options
,
2358 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
2360 else if ((keyword
= cupsGetOption("ColorModel", num_options
,
2363 if (!_cups_strcasecmp(keyword
, "Gray"))
2364 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
2365 NULL
, "monochrome");
2367 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
2371 if ((keyword
= cupsGetOption("print-quality", num_options
,
2373 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2375 else if ((keyword
= cupsGetOption("cupsPrintQuality", num_options
,
2378 if (!_cups_strcasecmp(keyword
, "draft"))
2379 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2381 else if (!_cups_strcasecmp(keyword
, "normal"))
2382 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2383 IPP_QUALITY_NORMAL
);
2384 else if (!_cups_strcasecmp(keyword
, "high"))
2385 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2389 if ((keyword
= cupsGetOption("sides", num_options
, options
)) != NULL
)
2390 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2392 else if (pc
->sides_option
&&
2393 (keyword
= cupsGetOption(pc
->sides_option
, num_options
,
2396 if (!_cups_strcasecmp(keyword
, pc
->sides_1sided
))
2397 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2399 else if (!_cups_strcasecmp(keyword
, pc
->sides_2sided_long
))
2400 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2401 NULL
, "two-sided-long-edge");
2402 if (!_cups_strcasecmp(keyword
, pc
->sides_2sided_short
))
2403 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2404 NULL
, "two-sided-short-edge");
2407 if (doc_handling_sup
&&
2408 (!format
|| _cups_strncasecmp(format
, "image/", 6)) &&
2409 (keyword
= cupsGetOption("collate", num_options
, options
)) != NULL
)
2411 if (!_cups_strcasecmp(keyword
, "true"))
2412 collate_str
= "separate-documents-collated-copies";
2414 collate_str
= "separate-documents-uncollated-copies";
2416 for (i
= 0; i
< doc_handling_sup
->num_values
; i
++)
2417 if (!strcmp(doc_handling_sup
->values
[i
].string
.text
, collate_str
))
2419 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2420 "multiple-document-handling", NULL
, collate_str
);
2426 * Map finishing options...
2429 num_finishings
= _ppdCacheGetFinishingValues(pc
, num_options
, options
,
2430 (int)(sizeof(finishings
) /
2431 sizeof(finishings
[0])),
2433 if (num_finishings
> 0)
2434 ippAddIntegers(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "finishings",
2435 num_finishings
, finishings
);
2438 * Map FaxOut options...
2441 if ((keyword
= cupsGetOption("phone", num_options
, options
)) != NULL
)
2443 ipp_t
*destination
; /* destination collection */
2444 char tel_uri
[1024]; /* tel: URI */
2446 destination
= ippNew();
2448 httpAssembleURI(HTTP_URI_CODING_ALL
, tel_uri
, sizeof(tel_uri
), "tel",
2449 NULL
, NULL
, 0, keyword
);
2450 ippAddString(destination
, IPP_TAG_JOB
, IPP_TAG_URI
, "destination-uri",
2453 if ((keyword
= cupsGetOption("faxPrefix", num_options
,
2454 options
)) != NULL
&& *keyword
)
2455 ippAddString(destination
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
2456 "pre-dial-string", NULL
, keyword
);
2458 ippAddCollection(request
, IPP_TAG_JOB
, "destination-uris", destination
);
2459 ippDelete(destination
);
2465 * When talking to another CUPS server, send all options...
2468 cupsEncodeOptions(request
, num_options
, options
);
2472 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies", copies
);
2480 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
2483 static const char * /* O - Password */
2484 password_cb(const char *prompt
) /* I - Prompt (not used) */
2489 * Remember that we need to authenticate...
2492 auth_info_required
= "username,password";
2494 if (password
&& *password
&& password_tries
< 3)
2503 * Give up after 3 tries or if we don't have a password to begin with...
2512 * 'report_attr()' - Report an IPP attribute value.
2516 report_attr(ipp_attribute_t
*attr
) /* I - Attribute */
2518 int i
; /* Looping var */
2519 char value
[1024], /* Value string */
2520 *valptr
, /* Pointer into value string */
2521 *attrptr
; /* Pointer into attribute value */
2522 const char *cached
; /* Cached attribute */
2526 * Convert the attribute values into quoted strings...
2529 for (i
= 0, valptr
= value
;
2530 i
< attr
->num_values
&& valptr
< (value
+ sizeof(value
) - 10);
2536 switch (attr
->value_tag
)
2538 case IPP_TAG_INTEGER
:
2540 snprintf(valptr
, sizeof(value
) - (valptr
- value
), "%d",
2541 attr
->values
[i
].integer
);
2542 valptr
+= strlen(valptr
);
2547 case IPP_TAG_KEYWORD
:
2549 for (attrptr
= attr
->values
[i
].string
.text
;
2550 *attrptr
&& valptr
< (value
+ sizeof(value
) - 10);
2553 if (*attrptr
== '\\' || *attrptr
== '\"')
2556 *valptr
++ = *attrptr
;
2563 * Unsupported value type...
2572 _cupsMutexLock(&report_mutex
);
2574 if ((cached
= cupsGetOption(attr
->name
, num_attr_cache
,
2575 attr_cache
)) == NULL
|| strcmp(cached
, value
))
2578 * Tell the scheduler about the new values...
2581 num_attr_cache
= cupsAddOption(attr
->name
, value
, num_attr_cache
,
2583 fprintf(stderr
, "ATTR: %s=%s\n", attr
->name
, value
);
2586 _cupsMutexUnlock(&report_mutex
);
2591 * 'report_printer_state()' - Report the printer state.
2595 report_printer_state(ipp_t
*ipp
) /* I - IPP response */
2597 ipp_attribute_t
*pa
, /* printer-alert */
2598 *pam
, /* printer-alert-message */
2599 *psm
, /* printer-state-message */
2600 *reasons
, /* printer-state-reasons */
2601 *marker
; /* marker-* attributes */
2602 char value
[1024], /* State/message string */
2603 *valptr
; /* Pointer into string */
2604 static int ipp_supplies
= -1;
2605 /* Report supply levels? */
2609 * Report alerts and messages...
2612 if ((pa
= ippFindAttribute(ipp
, "printer-alert", IPP_TAG_TEXT
)) != NULL
)
2615 if ((pam
= ippFindAttribute(ipp
, "printer-alert-message",
2616 IPP_TAG_TEXT
)) != NULL
)
2619 if ((psm
= ippFindAttribute(ipp
, "printer-state-message",
2620 IPP_TAG_TEXT
)) != NULL
)
2622 char *ptr
; /* Pointer into message */
2625 strlcpy(value
, "INFO: ", sizeof(value
));
2626 for (ptr
= psm
->values
[0].string
.text
, valptr
= value
+ 6;
2627 *ptr
&& valptr
< (value
+ sizeof(value
) - 6);
2630 if (*ptr
< ' ' && *ptr
> 0 && *ptr
!= '\t')
2633 * Substitute "<XX>" for the control character; sprintf is safe because
2634 * we always leave 6 chars free at the end...
2637 sprintf(valptr
, "<%02X>", *ptr
);
2647 fputs(value
, stderr
);
2651 * Now report printer-state-reasons, filtering out some of the reasons we never
2655 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
2656 IPP_TAG_KEYWORD
)) == NULL
)
2659 update_reasons(reasons
, NULL
);
2662 * Relay the current marker-* attribute values...
2665 if (ipp_supplies
< 0)
2667 ppd_file_t
*ppd
; /* PPD file */
2668 ppd_attr_t
*ppdattr
; /* Attribute in PPD file */
2670 if ((ppd
= ppdOpenFile(getenv("PPD"))) != NULL
&&
2671 (ppdattr
= ppdFindAttr(ppd
, "cupsIPPSupplies", NULL
)) != NULL
&&
2672 ppdattr
->value
&& _cups_strcasecmp(ppdattr
->value
, "true"))
2680 if (ipp_supplies
> 0)
2682 if ((marker
= ippFindAttribute(ipp
, "marker-colors", IPP_TAG_NAME
)) != NULL
)
2683 report_attr(marker
);
2684 if ((marker
= ippFindAttribute(ipp
, "marker-high-levels",
2685 IPP_TAG_INTEGER
)) != NULL
)
2686 report_attr(marker
);
2687 if ((marker
= ippFindAttribute(ipp
, "marker-levels",
2688 IPP_TAG_INTEGER
)) != NULL
)
2689 report_attr(marker
);
2690 if ((marker
= ippFindAttribute(ipp
, "marker-low-levels",
2691 IPP_TAG_INTEGER
)) != NULL
)
2692 report_attr(marker
);
2693 if ((marker
= ippFindAttribute(ipp
, "marker-message",
2694 IPP_TAG_TEXT
)) != NULL
)
2695 report_attr(marker
);
2696 if ((marker
= ippFindAttribute(ipp
, "marker-names", IPP_TAG_NAME
)) != NULL
)
2697 report_attr(marker
);
2698 if ((marker
= ippFindAttribute(ipp
, "marker-types",
2699 IPP_TAG_KEYWORD
)) != NULL
)
2700 report_attr(marker
);
2705 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
2707 * 'run_as_user()' - Run the IPP backend as the printing user.
2709 * This function uses an XPC-based user agent to run the backend as the printing
2710 * user. We need to do this in order to have access to the user's Kerberos
2714 static int /* O - Exit status */
2715 run_as_user(int argc
, /* I - Number of command-line args */
2716 char *argv
[], /* I - Command-line arguments */
2717 uid_t uid
, /* I - User ID */
2718 const char *device_uri
, /* I - Device URI */
2719 int fd
) /* I - File to print */
2721 const char *auth_negotiate
;/* AUTH_NEGOTIATE env var */
2722 xpc_connection_t conn
; /* Connection to XPC service */
2723 xpc_object_t request
; /* Request message dictionary */
2724 __block xpc_object_t response
; /* Response message dictionary */
2725 dispatch_semaphore_t sem
; /* Semaphore for waiting for response */
2726 int status
= CUPS_BACKEND_FAILED
;
2727 /* Status of request */
2730 fprintf(stderr
, "DEBUG: Running IPP backend as UID %d.\n", (int)uid
);
2733 * Connect to the user agent for the specified UID...
2736 conn
= xpc_connection_create_mach_service(kPMPrintUIToolAgent
,
2737 dispatch_get_global_queue(0, 0), 0);
2740 _cupsLangPrintFilter(stderr
, "ERROR",
2741 _("Unable to start backend process."));
2742 fputs("DEBUG: Unable to create connection to agent.\n", stderr
);
2746 xpc_connection_set_event_handler(conn
,
2747 ^(xpc_object_t event
)
2749 xpc_type_t messageType
= xpc_get_type(event
);
2751 if (messageType
== XPC_TYPE_ERROR
)
2753 if (event
== XPC_ERROR_CONNECTION_INTERRUPTED
)
2754 fprintf(stderr
, "DEBUG: Interrupted connection to service %s.\n",
2755 xpc_connection_get_name(conn
));
2756 else if (event
== XPC_ERROR_CONNECTION_INVALID
)
2757 fprintf(stderr
, "DEBUG: Connection invalid for service %s.\n",
2758 xpc_connection_get_name(conn
));
2760 fprintf(stderr
, "DEBUG: Unxpected error for service %s: %s\n",
2761 xpc_connection_get_name(conn
),
2762 xpc_dictionary_get_string(event
, XPC_ERROR_KEY_DESCRIPTION
));
2765 xpc_connection_set_target_uid(conn
, uid
);
2766 xpc_connection_resume(conn
);
2769 * Try starting the backend...
2772 request
= xpc_dictionary_create(NULL
, NULL
, 0);
2773 xpc_dictionary_set_int64(request
, "command", kPMStartJob
);
2774 xpc_dictionary_set_string(request
, "device-uri", device_uri
);
2775 xpc_dictionary_set_string(request
, "job-id", argv
[1]);
2776 xpc_dictionary_set_string(request
, "user", argv
[2]);
2777 xpc_dictionary_set_string(request
, "title", argv
[3]);
2778 xpc_dictionary_set_string(request
, "copies", argv
[4]);
2779 xpc_dictionary_set_string(request
, "options", argv
[5]);
2780 xpc_dictionary_set_string(request
, "auth-info-required",
2781 getenv("AUTH_INFO_REQUIRED"));
2782 if ((auth_negotiate
= getenv("AUTH_NEGOTIATE")) != NULL
)
2783 xpc_dictionary_set_string(request
, "auth-negotiate", auth_negotiate
);
2784 xpc_dictionary_set_fd(request
, "stdin", fd
);
2785 xpc_dictionary_set_fd(request
, "stderr", 2);
2786 xpc_dictionary_set_fd(request
, "side-channel", CUPS_SC_FD
);
2788 sem
= dispatch_semaphore_create(0);
2791 xpc_connection_send_message_with_reply(conn
, request
,
2792 dispatch_get_global_queue(0,0),
2793 ^(xpc_object_t reply
)
2795 /* Save the response and wake up */
2796 if (xpc_get_type(reply
)
2797 == XPC_TYPE_DICTIONARY
)
2798 response
= xpc_retain(reply
);
2800 dispatch_semaphore_signal(sem
);
2803 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
2804 xpc_release(request
);
2805 dispatch_release(sem
);
2809 child_pid
= xpc_dictionary_get_int64(response
, "child-pid");
2811 xpc_release(response
);
2814 fprintf(stderr
, "DEBUG: Child PID=%d.\n", child_pid
);
2817 _cupsLangPrintFilter(stderr
, "ERROR",
2818 _("Unable to start backend process."));
2819 fputs("DEBUG: No child PID.\n", stderr
);
2825 _cupsLangPrintFilter(stderr
, "ERROR",
2826 _("Unable to start backend process."));
2827 fputs("DEBUG: No reply from agent.\n", stderr
);
2832 * Then wait for the backend to finish...
2835 request
= xpc_dictionary_create(NULL
, NULL
, 0);
2836 xpc_dictionary_set_int64(request
, "command", kPMWaitForJob
);
2837 xpc_dictionary_set_fd(request
, "stderr", 2);
2839 sem
= dispatch_semaphore_create(0);
2842 xpc_connection_send_message_with_reply(conn
, request
,
2843 dispatch_get_global_queue(0,0),
2844 ^(xpc_object_t reply
)
2846 /* Save the response and wake up */
2847 if (xpc_get_type(reply
)
2848 == XPC_TYPE_DICTIONARY
)
2849 response
= xpc_retain(reply
);
2851 dispatch_semaphore_signal(sem
);
2854 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
2855 xpc_release(request
);
2856 dispatch_release(sem
);
2860 status
= xpc_dictionary_get_int64(response
, "status");
2862 if (status
== SIGTERM
|| status
== SIGKILL
|| status
== SIGPIPE
)
2864 fprintf(stderr
, "DEBUG: Child terminated on signal %d.\n", status
);
2865 status
= CUPS_BACKEND_FAILED
;
2867 else if (WIFSIGNALED(status
))
2869 fprintf(stderr
, "DEBUG: Child crashed on signal %d.\n", status
);
2870 status
= CUPS_BACKEND_STOP
;
2872 else if (WIFEXITED(status
))
2874 status
= WEXITSTATUS(status
);
2875 fprintf(stderr
, "DEBUG: Child exited with status %d.\n", status
);
2878 xpc_release(response
);
2881 _cupsLangPrintFilter(stderr
, "ERROR",
2882 _("Unable to get backend exit status."));
2888 xpc_connection_suspend(conn
);
2889 xpc_connection_cancel(conn
);
2895 #endif /* HAVE_GSSAPI && HAVE_XPC */
2899 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
2903 sigterm_handler(int sig
) /* I - Signal */
2905 (void)sig
; /* remove compiler warnings... */
2907 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
2910 kill(child_pid
, sig
);
2913 #endif /* HAVE_GSSAPI && HAVE_XPC */
2918 * Flag that the job should be canceled...
2926 * The scheduler already tried to cancel us once, now just terminate
2927 * after removing our temp file!
2931 unlink(tmpfilename
);
2938 * 'timeout_cb()' - Handle HTTP timeouts.
2941 static int /* O - 1 to continue, 0 to cancel */
2942 timeout_cb(http_t
*http
, /* I - Connection to server (unused) */
2943 void *user_data
) /* I - User data (unused) */
2948 return (!job_canceled
);
2953 * 'update_reasons()' - Update the printer-state-reasons values.
2957 update_reasons(ipp_attribute_t
*attr
, /* I - printer-state-reasons or NULL */
2958 const char *s
) /* I - STATE: string or NULL */
2960 char op
; /* Add (+), remove (-), replace (\0) */
2961 cups_array_t
*new_reasons
; /* New reasons array */
2962 char *reason
, /* Current reason */
2963 add
[2048], /* Reasons added string */
2964 *addptr
, /* Pointer into add string */
2965 rem
[2048], /* Reasons removed string */
2966 *remptr
; /* Pointer into remove string */
2967 const char *addprefix
, /* Current add string prefix */
2968 *remprefix
; /* Current remove string prefix */
2971 fprintf(stderr
, "DEBUG: update_reasons(attr=%d(%s%s), s=\"%s\")\n",
2972 attr
? attr
->num_values
: 0, attr
? attr
->values
[0].string
.text
: "",
2973 attr
&& attr
->num_values
> 1 ? ",..." : "", s
? s
: "(null)");
2976 * Create an array of new reason keyword strings...
2981 int i
; /* Looping var */
2983 new_reasons
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
2986 for (i
= 0; i
< attr
->num_values
; i
++)
2988 reason
= attr
->values
[i
].string
.text
;
2990 if (strcmp(reason
, "none") &&
2991 strcmp(reason
, "none-report") &&
2992 strcmp(reason
, "paused") &&
2993 strncmp(reason
, "spool-area-full", 15) &&
2994 strcmp(reason
, "com.apple.print.recoverable-warning") &&
2995 strncmp(reason
, "cups-", 5))
2996 cupsArrayAdd(new_reasons
, reason
);
3001 if (*s
== '+' || *s
== '-')
3006 new_reasons
= _cupsArrayNewStrings(s
);
3012 * Compute the changes...
3016 addprefix
= "STATE: +";
3019 remprefix
= "STATE: -";
3022 fprintf(stderr
, "DEBUG2: op='%c', new_reasons=%d, state_reasons=%d\n",
3023 op
? op
: ' ', cupsArrayCount(new_reasons
),
3024 cupsArrayCount(state_reasons
));
3026 _cupsMutexLock(&report_mutex
);
3034 for (reason
= (char *)cupsArrayFirst(new_reasons
);
3036 reason
= (char *)cupsArrayNext(new_reasons
))
3038 if (!cupsArrayFind(state_reasons
, reason
))
3040 if (!strncmp(reason
, "cups-remote-", 12))
3043 * If we are setting cups-remote-xxx, remove all other cups-remote-xxx
3047 char *temp
; /* Current reason in state_reasons */
3049 cupsArraySave(state_reasons
);
3051 for (temp
= (char *)cupsArrayFirst(state_reasons
);
3053 temp
= (char *)cupsArrayNext(state_reasons
))
3054 if (!strncmp(temp
, "cups-remote-", 12))
3056 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
3058 remptr
+= strlen(remptr
);
3061 cupsArrayRemove(state_reasons
, temp
);
3065 cupsArrayRestore(state_reasons
);
3068 cupsArrayAdd(state_reasons
, reason
);
3070 snprintf(addptr
, sizeof(add
) - (addptr
- add
), "%s%s", addprefix
,
3072 addptr
+= strlen(addptr
);
3083 for (reason
= (char *)cupsArrayFirst(new_reasons
);
3085 reason
= (char *)cupsArrayNext(new_reasons
))
3087 if (cupsArrayFind(state_reasons
, reason
))
3089 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
3091 remptr
+= strlen(remptr
);
3094 cupsArrayRemove(state_reasons
, reason
);
3101 * Replace reasons...
3104 for (reason
= (char *)cupsArrayFirst(state_reasons
);
3106 reason
= (char *)cupsArrayNext(state_reasons
))
3108 if (strncmp(reason
, "cups-", 5) && !cupsArrayFind(new_reasons
, reason
))
3110 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
3112 remptr
+= strlen(remptr
);
3115 cupsArrayRemove(state_reasons
, reason
);
3119 for (reason
= (char *)cupsArrayFirst(new_reasons
);
3121 reason
= (char *)cupsArrayNext(new_reasons
))
3123 if (!cupsArrayFind(state_reasons
, reason
))
3125 cupsArrayAdd(state_reasons
, reason
);
3127 snprintf(addptr
, sizeof(add
) - (addptr
- add
), "%s%s", addprefix
,
3129 addptr
+= strlen(addptr
);
3135 _cupsMutexUnlock(&report_mutex
);
3138 * Report changes and return...
3141 if (add
[0] && rem
[0])
3142 fprintf(stderr
, "%s\n%s\n", add
, rem
);
3144 fprintf(stderr
, "%s\n", add
);
3146 fprintf(stderr
, "%s\n", rem
);