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
26 * password_cb() - Disable the password prompt for
27 * cupsDoFileRequest().
28 * quote_string() - Quote a string value.
29 * report_attr() - Report an IPP attribute value.
30 * report_printer_state() - Report the printer state.
31 * run_as_user() - Run the IPP backend as the printing user.
32 * sigterm_handler() - Handle 'terminate' signals that stop the backend.
33 * timeout_cb() - Handle HTTP timeouts.
34 * update_reasons() - Update the printer-state-reasons values.
38 * Include necessary headers.
41 #include "backend-private.h"
42 #include <cups/array-private.h>
43 #include <sys/types.h>
46 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
48 # define kPMPrintUIToolAgent "com.apple.printuitool.agent"
49 # define kPMStartJob 100
50 # define kPMWaitForJob 101
51 # ifdef HAVE_XPC_PRIVATE_H
52 # include <xpc/private.h>
54 extern void xpc_connection_set_target_uid(xpc_connection_t connection
,
56 # endif /* HAVE_XPC_PRIVATE_H */
57 #endif /* HAVE_GSSAPI && HAVE_XPC */
61 * Bits for job-state-reasons we care about...
64 #define _CUPS_JSR_ACCOUNT_AUTHORIZATION_FAILED 0x01
65 #define _CUPS_JSR_ACCOUNT_CLOSED 0x02
66 #define _CUPS_JSR_ACCOUNT_INFO_NEEDED 0x04
67 #define _CUPS_JSR_ACCOUNT_LIMIT_REACHED 0x08
68 #define _CUPS_JSR_JOB_PASSWORD_WAIT 0x10
69 #define _CUPS_JSR_JOB_RELEASE_WAIT 0x20
76 typedef struct _cups_monitor_s
/**** Monitoring data ****/
78 const char *uri
, /* Printer URI */
79 *hostname
, /* Hostname */
81 *resource
; /* Resource path */
82 int port
, /* Port number */
83 version
, /* IPP version */
84 job_id
, /* Job ID for submitted job */
85 job_reasons
, /* Job state reasons bits */
86 get_job_attrs
; /* Support Get-Job-Attributes? */
87 const char *job_name
; /* Job name for submitted job */
88 http_encryption_t encryption
; /* Use encryption? */
89 ipp_jstate_t job_state
; /* Current job state */
90 ipp_pstate_t printer_state
; /* Current printer state */
98 static const char *auth_info_required
;
99 /* New auth-info-required value */
100 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
101 static int child_pid
= 0; /* Child process ID */
102 #endif /* HAVE_GSSAPI && HAVE_XPC */
103 static const char * const jattrs
[] = /* Job attributes we want */
106 "job-impressions-completed",
107 "job-media-sheets-completed",
109 "job-originating-user-name",
113 static int job_canceled
= 0;
115 static char username
[256] = "",
116 /* Username for device URI */
118 /* Password for device URI */
119 static int password_tries
= 0;
121 static const char * const pattrs
[] = /* Printer attributes we want */
125 "document-format-supported",
127 "marker-high-levels",
133 "media-col-supported",
134 "multiple-document-handling-supported",
135 "operations-supported",
137 "printer-alert-description",
138 "printer-is-accepting-jobs",
140 "printer-state-message",
141 "printer-state-reasons"
143 static const char * const remote_job_states
[] =
144 { /* Remote job state keywords */
145 "+cups-remote-pending",
146 "+cups-remote-pending-held",
147 "+cups-remote-processing",
148 "+cups-remote-stopped",
149 "+cups-remote-canceled",
150 "+cups-remote-aborted",
151 "+cups-remote-completed"
153 static _cups_mutex_t report_mutex
= _CUPS_MUTEX_INITIALIZER
;
154 /* Mutex to control access */
155 static int num_attr_cache
= 0;
156 /* Number of cached attributes */
157 static cups_option_t
*attr_cache
= NULL
;
158 /* Cached attributes */
159 static cups_array_t
*state_reasons
; /* Array of printe-state-reasons keywords */
160 static char tmpfilename
[1024] = "";
161 /* Temporary spool file name */
168 static void cancel_job(http_t
*http
, const char *uri
, int id
,
169 const char *resource
, const char *user
,
171 static ipp_pstate_t
check_printer_state(http_t
*http
, const char *uri
,
172 const char *resource
,
173 const char *user
, int version
);
175 static void compress_files(int num_files
, char **files
);
176 #endif /* HAVE_LIBZ */
177 static void *monitor_printer(_cups_monitor_t
*monitor
);
178 static ipp_t
*new_request(ipp_op_t op
, int version
, const char *uri
,
179 const char *user
, const char *title
,
180 int num_options
, cups_option_t
*options
,
181 const char *compression
, int copies
,
182 const char *format
, _ppd_cache_t
*pc
,
184 ipp_attribute_t
*media_col_sup
,
185 ipp_attribute_t
*doc_handling_sup
,
186 int print_color_mode
);
187 static const char *password_cb(const char *prompt
, http_t
*http
,
188 const char *method
, const char *resource
,
190 static const char *quote_string(const char *s
, char *q
, size_t qsize
);
191 static void report_attr(ipp_attribute_t
*attr
);
192 static void report_printer_state(ipp_t
*ipp
);
193 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
194 static int run_as_user(char *argv
[], uid_t uid
,
195 const char *device_uri
, int fd
);
196 #endif /* HAVE_GSSAPI && HAVE_XPC */
197 static void sigterm_handler(int sig
);
198 static int timeout_cb(http_t
*http
, void *user_data
);
199 static void update_reasons(ipp_attribute_t
*attr
, const char *s
);
203 * 'main()' - Send a file to the printer or server.
207 * printer-uri job-id user title copies options [file]
210 int /* O - Exit status */
211 main(int argc
, /* I - Number of command-line args */
212 char *argv
[]) /* I - Command-line arguments */
214 int i
; /* Looping var */
215 int send_options
; /* Send job options? */
216 int num_options
; /* Number of printer options */
217 cups_option_t
*options
; /* Printer options */
218 const char *device_uri
; /* Device URI */
219 char scheme
[255], /* Scheme in URI */
220 hostname
[1024], /* Hostname */
221 resource
[1024], /* Resource info (printer name) */
222 addrname
[256], /* Address name */
223 *optptr
, /* Pointer to URI options */
224 *name
, /* Name of option */
225 *value
, /* Value of option */
226 sep
; /* Separator character */
227 http_addrlist_t
*addrlist
; /* Address of printer */
228 int snmp_enabled
= 1; /* Is SNMP enabled? */
229 int snmp_fd
, /* SNMP socket */
230 start_count
, /* Page count via SNMP at start */
231 page_count
, /* Page count via SNMP */
232 have_supplies
; /* Printer supports supply levels? */
233 int num_files
; /* Number of files to print */
234 char **files
, /* Files to print */
235 *compatfile
= NULL
; /* Compatibility filename */
236 off_t compatsize
= 0; /* Size of compatibility file */
237 int port
; /* Port number (not used) */
238 char portname
[255]; /* Port name */
239 char uri
[HTTP_MAX_URI
]; /* Updated URI without user/pass */
240 char print_job_name
[1024]; /* Update job-name for Print-Job */
241 http_status_t http_status
; /* Status of HTTP request */
242 ipp_status_t ipp_status
; /* Status of IPP request */
243 http_t
*http
; /* HTTP connection */
244 ipp_t
*request
, /* IPP request */
245 *response
, /* IPP response */
246 *supported
; /* get-printer-attributes response */
247 time_t start_time
; /* Time of first connect */
248 int contimeout
; /* Connection timeout */
249 int delay
, /* Delay for retries */
250 prev_delay
; /* Previous delay */
251 const char *compression
; /* Compression mode */
252 int waitjob
, /* Wait for job complete? */
253 waitjob_tries
= 0, /* Number of times we've waited */
254 waitprinter
; /* Wait for printer ready? */
255 _cups_monitor_t monitor
; /* Monitoring data */
256 ipp_attribute_t
*job_id_attr
; /* job-id attribute */
257 int job_id
; /* job-id value */
258 ipp_attribute_t
*job_sheets
; /* job-media-sheets-completed */
259 ipp_attribute_t
*job_state
; /* job-state */
260 ipp_attribute_t
*copies_sup
; /* copies-supported */
261 ipp_attribute_t
*cups_version
; /* cups-version */
262 ipp_attribute_t
*format_sup
; /* document-format-supported */
263 ipp_attribute_t
*media_col_sup
; /* media-col-supported */
264 ipp_attribute_t
*operations_sup
; /* operations-supported */
265 ipp_attribute_t
*doc_handling_sup
; /* multiple-document-handling-supported */
266 ipp_attribute_t
*printer_state
; /* printer-state attribute */
267 ipp_attribute_t
*printer_accepting
; /* printer-is-accepting-jobs */
268 int create_job
= 0, /* Does printer support Create-Job? */
269 get_job_attrs
= 0, /* Does printer support Get-Job-Attributes? */
270 send_document
= 0, /* Does printer support Send-Document? */
271 validate_job
= 0, /* Does printer support Validate-Job? */
272 print_color_mode
= 0; /* Does printer support print-color-mode? */
273 int copies
, /* Number of copies for job */
274 copies_remaining
; /* Number of copies remaining */
275 const char *content_type
, /* CONTENT_TYPE environment variable */
276 *final_content_type
, /* FINAL_CONTENT_TYPE environment var */
277 *document_format
; /* document-format value */
278 int fd
; /* File descriptor */
279 off_t bytes
= 0; /* Bytes copied */
280 char buffer
[16384]; /* Copy buffer */
281 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
282 struct sigaction action
; /* Actions for POSIX signals */
283 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
284 int version
; /* IPP version */
285 ppd_file_t
*ppd
= NULL
; /* PPD file */
286 _ppd_cache_t
*pc
= NULL
; /* PPD cache and mapping data */
287 fd_set input
; /* Input set for select() */
291 * Make sure status messages are not buffered...
294 setbuf(stderr
, NULL
);
297 * Ignore SIGPIPE and catch SIGTERM signals...
301 sigset(SIGPIPE
, SIG_IGN
);
302 sigset(SIGTERM
, sigterm_handler
);
303 #elif defined(HAVE_SIGACTION)
304 memset(&action
, 0, sizeof(action
));
305 action
.sa_handler
= SIG_IGN
;
306 sigaction(SIGPIPE
, &action
, NULL
);
308 sigemptyset(&action
.sa_mask
);
309 sigaddset(&action
.sa_mask
, SIGTERM
);
310 action
.sa_handler
= sigterm_handler
;
311 sigaction(SIGTERM
, &action
, NULL
);
313 signal(SIGPIPE
, SIG_IGN
);
314 signal(SIGTERM
, sigterm_handler
);
315 #endif /* HAVE_SIGSET */
318 * Check command-line...
325 if ((s
= strrchr(argv
[0], '/')) != NULL
)
330 printf("network %s \"Unknown\" \"%s (%s)\"\n",
331 s
, _cupsLangString(cupsLangDefault(),
332 _("Internet Printing Protocol")), s
);
333 return (CUPS_BACKEND_OK
);
337 _cupsLangPrintf(stderr
,
338 _("Usage: %s job-id user title copies options [file]"),
340 return (CUPS_BACKEND_STOP
);
344 * Get the device URI...
347 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
349 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
352 if (getenv("CLASS") != NULL
)
353 return (CUPS_BACKEND_FAILED
);
356 if ((auth_info_required
= getenv("AUTH_INFO_REQUIRED")) == NULL
)
357 auth_info_required
= "none";
359 state_reasons
= _cupsArrayNewStrings(getenv("PRINTER_STATE_REASONS"), ',');
363 * For Kerberos, become the printing user (if we can) to get the credentials
367 if (!getuid() && (value
= getenv("AUTH_UID")) != NULL
)
369 uid_t uid
= (uid_t
)atoi(value
);
376 return (run_as_user(argv
, uid
, device_uri
, 0));
379 int status
= 0; /* Exit status */
381 for (i
= 6; i
< argc
&& !status
&& !job_canceled
; i
++)
383 if ((fd
= open(argv
[i
], O_RDONLY
)) >= 0)
385 status
= run_as_user(argv
, uid
, device_uri
, fd
);
390 _cupsLangPrintError("ERROR", _("Unable to open print file"));
391 status
= CUPS_BACKEND_FAILED
;
399 # else /* No XPC, just try to run as the user ID */
402 # endif /* HAVE_XPC */
404 #endif /* HAVE_GSSAPI */
407 * Get the (final) content type...
410 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
411 content_type
= "application/octet-stream";
413 if ((final_content_type
= getenv("FINAL_CONTENT_TYPE")) == NULL
)
415 final_content_type
= content_type
;
417 if (!strncmp(final_content_type
, "printer/", 8))
418 final_content_type
= "application/vnd.cups-raw";
422 * Extract the hostname and printer name from the URI...
425 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
426 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
427 resource
, sizeof(resource
));
430 port
= IPP_PORT
; /* Default to port 631 */
432 if (!strcmp(scheme
, "https"))
433 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
435 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
438 * See if there are any options...
445 contimeout
= 7 * 24 * 60 * 60;
447 if ((optptr
= strchr(resource
, '?')) != NULL
)
450 * Yup, terminate the device name string and move to the first
451 * character of the optptr...
457 * Then parse the optptr...
468 while (*optptr
&& *optptr
!= '=' && *optptr
!= '+' && *optptr
!= '&')
471 if ((sep
= *optptr
) != '\0')
482 while (*optptr
&& *optptr
!= '+' && *optptr
!= '&')
492 * Process the option...
495 if (!_cups_strcasecmp(name
, "waitjob"))
498 * Wait for job completion?
501 waitjob
= !_cups_strcasecmp(value
, "on") ||
502 !_cups_strcasecmp(value
, "yes") ||
503 !_cups_strcasecmp(value
, "true");
505 else if (!_cups_strcasecmp(name
, "waitprinter"))
508 * Wait for printer idle?
511 waitprinter
= !_cups_strcasecmp(value
, "on") ||
512 !_cups_strcasecmp(value
, "yes") ||
513 !_cups_strcasecmp(value
, "true");
515 else if (!_cups_strcasecmp(name
, "encryption"))
518 * Enable/disable encryption?
521 if (!_cups_strcasecmp(value
, "always"))
522 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
523 else if (!_cups_strcasecmp(value
, "required"))
524 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
525 else if (!_cups_strcasecmp(value
, "never"))
526 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
527 else if (!_cups_strcasecmp(value
, "ifrequested"))
528 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
531 _cupsLangPrintFilter(stderr
, "ERROR",
532 _("Unknown encryption option value: \"%s\"."),
536 else if (!_cups_strcasecmp(name
, "snmp"))
539 * Enable/disable SNMP stuff...
542 snmp_enabled
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
543 _cups_strcasecmp(value
, "yes") ||
544 _cups_strcasecmp(value
, "true");
546 else if (!_cups_strcasecmp(name
, "version"))
548 if (!strcmp(value
, "1.0"))
550 else if (!strcmp(value
, "1.1"))
552 else if (!strcmp(value
, "2.0"))
554 else if (!strcmp(value
, "2.1"))
556 else if (!strcmp(value
, "2.2"))
560 _cupsLangPrintFilter(stderr
, "ERROR",
561 _("Unknown version option value: \"%s\"."),
566 else if (!_cups_strcasecmp(name
, "compression"))
568 if (!_cups_strcasecmp(value
, "true") || !_cups_strcasecmp(value
, "yes") ||
569 !_cups_strcasecmp(value
, "on") || !_cups_strcasecmp(value
, "gzip"))
570 compression
= "gzip";
572 #endif /* HAVE_LIBZ */
573 else if (!_cups_strcasecmp(name
, "contimeout"))
576 * Set the connection timeout...
580 contimeout
= atoi(value
);
588 _cupsLangPrintFilter(stderr
, "ERROR",
589 _("Unknown option \"%s\" with value \"%s\"."),
596 * If we have 7 arguments, print the file named on the command-line.
597 * Otherwise, copy stdin to a temporary file and print the temporary
605 send_options
= !_cups_strcasecmp(final_content_type
, "application/pdf") ||
606 !_cups_strcasecmp(final_content_type
, "application/vnd.cups-pdf") ||
607 !_cups_strncasecmp(final_content_type
, "image/", 6);
609 fputs("DEBUG: Sending stdin for job...\n", stderr
);
614 * Point to the files on the command-line...
617 num_files
= argc
- 6;
623 compress_files(num_files
, files
);
624 #endif /* HAVE_LIBZ */
626 fprintf(stderr
, "DEBUG: %d files to send in job...\n", num_files
);
630 * Set the authentication info, if any...
633 cupsSetPasswordCB2(password_cb
, NULL
);
638 * Use authentication information in the device URI...
641 if ((password
= strchr(username
, ':')) != NULL
)
644 cupsSetUser(username
);
649 * Try loading authentication information from the environment.
652 const char *ptr
= getenv("AUTH_USERNAME");
656 strlcpy(username
, ptr
, sizeof(username
));
660 password
= getenv("AUTH_PASSWORD");
664 * Try finding the remote server...
667 start_time
= time(NULL
);
669 sprintf(portname
, "%d", port
);
671 update_reasons(NULL
, "+connecting-to-device");
672 fprintf(stderr
, "DEBUG: Looking up \"%s\"...\n", hostname
);
674 while ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
676 _cupsLangPrintFilter(stderr
, "INFO",
677 _("Unable to locate printer \"%s\"."), hostname
);
680 if (getenv("CLASS") != NULL
)
682 update_reasons(NULL
, "-connecting-to-device");
683 return (CUPS_BACKEND_STOP
);
687 return (CUPS_BACKEND_OK
);
690 http
= _httpCreate(hostname
, port
, addrlist
, AF_UNSPEC
, cupsEncryption(), 1,
692 httpSetTimeout(http
, 30.0, timeout_cb
, NULL
);
695 * See if the printer supports SNMP...
699 snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
);
704 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
),
707 have_supplies
= start_count
= 0;
710 * Wait for data from the filter...
715 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), 0, backendNetworkSideCB
))
716 return (CUPS_BACKEND_OK
);
717 else if ((bytes
= read(0, buffer
, sizeof(buffer
))) <= 0)
718 return (CUPS_BACKEND_OK
);
722 * Try connecting to the remote server...
725 delay
= _cupsNextDelay(0, &prev_delay
);
729 fprintf(stderr
, "DEBUG: Connecting to %s:%d\n", hostname
, port
);
730 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
732 if (httpReconnect(http
))
734 int error
= errno
; /* Connection error */
736 if (http
->status
== HTTP_PKI_ERROR
)
737 update_reasons(NULL
, "+cups-certificate-error");
742 if (getenv("CLASS") != NULL
)
745 * If the CLASS environment variable is set, the job was submitted
746 * to a class and not to a specific queue. In this case, we want
747 * to abort immediately so that the job can be requeued on the next
748 * available printer in the class.
751 _cupsLangPrintFilter(stderr
, "INFO",
752 _("Unable to contact printer, queuing on next "
753 "printer in class."));
756 * Sleep 5 seconds to keep the job from requeuing too rapidly...
761 update_reasons(NULL
, "-connecting-to-device");
763 return (CUPS_BACKEND_FAILED
);
766 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
768 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
769 errno
== EHOSTUNREACH
)
771 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
773 _cupsLangPrintFilter(stderr
, "ERROR",
774 _("The printer is not responding."));
775 update_reasons(NULL
, "-connecting-to-device");
776 return (CUPS_BACKEND_FAILED
);
782 _cupsLangPrintFilter(stderr
, "WARNING",
783 _("The printer may not exist or "
784 "is unavailable at this time."));
788 _cupsLangPrintFilter(stderr
, "WARNING",
789 _("The printer is unreachable at this "
795 _cupsLangPrintFilter(stderr
, "WARNING",
796 _("The printer is in use."));
802 delay
= _cupsNextDelay(delay
, &prev_delay
);
806 _cupsLangPrintFilter(stderr
, "ERROR",
807 _("The printer is not responding."));
815 update_reasons(NULL
, "-cups-certificate-error");
817 while (http
->fd
< 0);
820 return (CUPS_BACKEND_OK
);
822 return (CUPS_BACKEND_FAILED
);
824 update_reasons(NULL
, "-connecting-to-device");
825 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
827 fprintf(stderr
, "DEBUG: Connected to %s:%d...\n",
828 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
829 httpAddrPort(http
->hostaddr
));
832 * Build a URI for the printer and fill the standard IPP attributes for
833 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
834 * might contain username:password information...
837 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), scheme
, NULL
, hostname
,
841 * First validate the destination and see if the device supports multiple
848 media_col_sup
= NULL
;
850 operations_sup
= NULL
;
851 doc_handling_sup
= NULL
;
856 * Check for side-channel requests...
859 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
862 * Build the IPP request...
865 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
866 request
->request
.op
.version
[0] = version
/ 10;
867 request
->request
.op
.version
[1] = version
% 10;
869 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
872 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
873 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
880 fputs("DEBUG: Getting supported attributes...\n", stderr
);
882 if (http
->version
< HTTP_1_1
)
884 fprintf(stderr
, "DEBUG: Printer responded with HTTP version %d.%d.\n",
885 http
->version
/ 100, http
->version
% 100);
886 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
887 "cups-ipp-wrong-http-version");
890 supported
= cupsDoRequest(http
, request
, resource
);
891 ipp_status
= cupsLastError();
893 fprintf(stderr
, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
894 ippErrorString(ipp_status
), cupsLastErrorString());
896 if (ipp_status
<= IPP_OK_CONFLICT
)
900 fprintf(stderr
, "DEBUG: Get-Printer-Attributes returned %s.\n",
901 ippErrorString(ipp_status
));
903 if (ipp_status
== IPP_PRINTER_BUSY
||
904 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
906 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
908 _cupsLangPrintFilter(stderr
, "ERROR",
909 _("The printer is not responding."));
910 return (CUPS_BACKEND_FAILED
);
913 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
915 report_printer_state(supported
);
919 delay
= _cupsNextDelay(delay
, &prev_delay
);
921 else if ((ipp_status
== IPP_BAD_REQUEST
||
922 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
> 10)
925 * Switch to IPP/1.1 or IPP/1.0...
930 _cupsLangPrintFilter(stderr
, "INFO", _("Preparing to print."));
932 "DEBUG: The printer does not support IPP/%d.%d, trying "
933 "IPP/1.1.\n", version
/ 10, version
% 10);
938 _cupsLangPrintFilter(stderr
, "INFO", _("Preparing to print."));
940 "DEBUG: The printer does not support IPP/%d.%d, trying "
941 "IPP/1.0.\n", version
/ 10, version
% 10);
947 else if (ipp_status
== IPP_NOT_FOUND
)
949 _cupsLangPrintFilter(stderr
, "ERROR",
950 _("The printer configuration is incorrect or the "
951 "printer no longer exists."));
953 ippDelete(supported
);
955 return (CUPS_BACKEND_STOP
);
957 else if (ipp_status
== IPP_FORBIDDEN
||
958 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
960 const char *www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
961 /* WWW-Authenticate field value */
963 if (!strncmp(www_auth
, "Negotiate", 9))
964 auth_info_required
= "negotiate";
965 else if (www_auth
[0])
966 auth_info_required
= "username,password";
968 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
969 return (CUPS_BACKEND_AUTH_REQUIRED
);
971 else if (ipp_status
!= IPP_NOT_AUTHORIZED
)
973 _cupsLangPrintFilter(stderr
, "ERROR",
974 _("Unable to get printer status."));
980 ippDelete(supported
);
985 if (!getenv("CLASS"))
988 * Check printer-is-accepting-jobs = false and printer-state-reasons for the
989 * "spool-area-full" keyword...
994 if ((printer_accepting
= ippFindAttribute(supported
,
995 "printer-is-accepting-jobs",
996 IPP_TAG_BOOLEAN
)) != NULL
&&
997 !printer_accepting
->values
[0].boolean
)
999 else if (!printer_accepting
)
1000 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1001 "cups-ipp-missing-printer-is-accepting-jobs");
1003 if ((printer_state
= ippFindAttribute(supported
,
1004 "printer-state-reasons",
1005 IPP_TAG_KEYWORD
)) == NULL
)
1007 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1008 "cups-ipp-missing-printer-state-reasons");
1012 for (i
= 0; i
< printer_state
->num_values
; i
++)
1014 if (!strcmp(printer_state
->values
[0].string
.text
,
1015 "spool-area-full") ||
1016 !strncmp(printer_state
->values
[0].string
.text
, "spool-area-full-",
1027 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
1029 report_printer_state(supported
);
1033 delay
= _cupsNextDelay(delay
, &prev_delay
);
1035 ippDelete(supported
);
1042 * Check for supported attributes...
1045 if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
1046 IPP_TAG_RANGE
)) != NULL
)
1049 * Has the "copies-supported" attribute - does it have an upper
1053 fprintf(stderr
, "DEBUG: copies-supported=%d-%d\n",
1054 copies_sup
->values
[0].range
.lower
,
1055 copies_sup
->values
[0].range
.upper
);
1057 if (copies_sup
->values
[0].range
.upper
<= 1)
1058 copies_sup
= NULL
; /* No */
1061 cups_version
= ippFindAttribute(supported
, "cups-version", IPP_TAG_TEXT
);
1063 if ((format_sup
= ippFindAttribute(supported
, "document-format-supported",
1064 IPP_TAG_MIMETYPE
)) != NULL
)
1066 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
1067 format_sup
->num_values
);
1068 for (i
= 0; i
< format_sup
->num_values
; i
++)
1069 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
1070 format_sup
->values
[i
].string
.text
);
1073 if ((media_col_sup
= ippFindAttribute(supported
, "media-col-supported",
1074 IPP_TAG_KEYWORD
)) != NULL
)
1076 fprintf(stderr
, "DEBUG: media-col-supported (%d values)\n",
1077 media_col_sup
->num_values
);
1078 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
1079 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
1080 media_col_sup
->values
[i
].string
.text
);
1083 print_color_mode
= ippFindAttribute(supported
,
1084 "print-color-mode-supported",
1085 IPP_TAG_KEYWORD
) != NULL
;
1087 if ((operations_sup
= ippFindAttribute(supported
, "operations-supported",
1088 IPP_TAG_ENUM
)) != NULL
)
1090 fprintf(stderr
, "DEBUG: operations-supported (%d values)\n",
1091 operations_sup
->num_values
);
1092 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1093 fprintf(stderr
, "DEBUG: [%d] = %s\n", i
,
1094 ippOpString(operations_sup
->values
[i
].integer
));
1096 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1097 if (operations_sup
->values
[i
].integer
== IPP_PRINT_JOB
)
1100 if (i
>= operations_sup
->num_values
)
1101 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1102 "cups-ipp-missing-print-job");
1104 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1105 if (operations_sup
->values
[i
].integer
== IPP_CANCEL_JOB
)
1108 if (i
>= operations_sup
->num_values
)
1109 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1110 "cups-ipp-missing-cancel-job");
1112 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1113 if (operations_sup
->values
[i
].integer
== IPP_GET_JOB_ATTRIBUTES
)
1116 if (i
>= operations_sup
->num_values
)
1117 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1118 "cups-ipp-missing-get-job-attributes");
1120 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1121 if (operations_sup
->values
[i
].integer
== IPP_GET_PRINTER_ATTRIBUTES
)
1124 if (i
>= operations_sup
->num_values
)
1125 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1126 "cups-ipp-missing-get-printer-attributes");
1128 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1130 if (operations_sup
->values
[i
].integer
== IPP_VALIDATE_JOB
)
1132 else if (operations_sup
->values
[i
].integer
== IPP_CREATE_JOB
)
1134 else if (operations_sup
->values
[i
].integer
== IPP_SEND_DOCUMENT
)
1136 else if (operations_sup
->values
[i
].integer
== IPP_GET_JOB_ATTRIBUTES
)
1140 if (create_job
&& !send_document
)
1142 fputs("DEBUG: Printer supports Create-Job but not Send-Document.\n",
1146 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1147 "cups-ipp-missing-send-document");
1151 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1152 "cups-ipp-missing-validate-job");
1155 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1156 "cups-ipp-missing-operations-supported");
1158 doc_handling_sup
= ippFindAttribute(supported
,
1159 "multiple-document-handling-supported",
1162 report_printer_state(supported
);
1164 while (!job_canceled
&& ipp_status
> IPP_OK_CONFLICT
);
1167 return (CUPS_BACKEND_OK
);
1170 * See if the printer is accepting jobs and is not stopped; if either
1171 * condition is true and we are printing to a class, requeue the job...
1174 if (getenv("CLASS") != NULL
)
1176 printer_state
= ippFindAttribute(supported
, "printer-state",
1178 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
1181 if (printer_state
== NULL
||
1182 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
1184 printer_accepting
== NULL
||
1185 !printer_accepting
->values
[0].boolean
)
1188 * If the CLASS environment variable is set, the job was submitted
1189 * to a class and not to a specific queue. In this case, we want
1190 * to abort immediately so that the job can be requeued on the next
1191 * available printer in the class.
1194 _cupsLangPrintFilter(stderr
, "INFO",
1195 _("Unable to contact printer, queuing on next "
1196 "printer in class."));
1198 ippDelete(supported
);
1202 * Sleep 5 seconds to keep the job from requeuing too rapidly...
1207 return (CUPS_BACKEND_FAILED
);
1212 * See if the printer supports multiple copies...
1215 copies
= atoi(argv
[4]);
1217 if (copies_sup
|| argc
< 7)
1218 copies_remaining
= 1;
1220 copies_remaining
= copies
;
1223 * Prepare remaining printing options...
1230 num_options
= cupsParseOptions(argv
[5], 0, &options
);
1232 if (!cups_version
&& media_col_sup
)
1235 * Load the PPD file and generate PWG attribute mapping information...
1238 ppd
= ppdOpenFile(getenv("PPD"));
1239 pc
= _ppdCacheCreateWithPPD(ppd
);
1241 ppdMarkDefaults(ppd
);
1242 cupsMarkOptions(ppd
, num_options
, options
);
1248 document_format
= NULL
;
1250 if (format_sup
!= NULL
)
1252 for (i
= 0; i
< format_sup
->num_values
; i
++)
1253 if (!_cups_strcasecmp(final_content_type
,
1254 format_sup
->values
[i
].string
.text
))
1256 document_format
= final_content_type
;
1260 if (!document_format
)
1262 for (i
= 0; i
< format_sup
->num_values
; i
++)
1263 if (!_cups_strcasecmp("application/octet-stream",
1264 format_sup
->values
[i
].string
.text
))
1266 document_format
= "application/octet-stream";
1272 fprintf(stderr
, "DEBUG: final_content_type=\"%s\", document_format=\"%s\"\n",
1273 final_content_type
, document_format
? document_format
: "(null)");
1276 * If the printer does not support HTTP/1.1 (which IPP requires), copy stdin
1277 * to a temporary file so that we can do a HTTP/1.0 submission...
1279 * (I hate compatibility hacks!)
1282 if (http
->version
< HTTP_1_1
&& num_files
== 0)
1284 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
1286 perror("DEBUG: Unable to create temporary file");
1287 return (CUPS_BACKEND_FAILED
);
1290 _cupsLangPrintFilter(stderr
, "INFO", _("Copying print data."));
1292 if ((compatsize
= write(fd
, buffer
, bytes
)) < 0)
1294 perror("DEBUG: Unable to write temporary file");
1295 return (CUPS_BACKEND_FAILED
);
1298 if ((bytes
= backendRunLoop(-1, fd
, snmp_fd
, &(addrlist
->addr
), 0, 0,
1299 backendNetworkSideCB
)) < 0)
1300 return (CUPS_BACKEND_FAILED
);
1302 compatsize
+= bytes
;
1306 compatfile
= tmpfilename
;
1307 files
= &compatfile
;
1310 else if (http
->version
< HTTP_1_1
&& num_files
== 1)
1312 struct stat fileinfo
; /* File information */
1314 if (!stat(files
[0], &fileinfo
))
1315 compatsize
= fileinfo
.st_size
;
1319 * If the printer only claims to support IPP/1.0, or if the user specifically
1320 * included version=1.0 in the URI, then do not try to use Create-Job or
1321 * Send-Document. This is another dreaded compatibility hack, but unfortunately
1322 * there are enough broken printers out there that we need this for now...
1326 create_job
= send_document
= 0;
1329 * Start monitoring the printer in the background...
1333 monitor
.hostname
= hostname
;
1334 monitor
.user
= argv
[2];
1335 monitor
.resource
= resource
;
1336 monitor
.port
= port
;
1337 monitor
.version
= version
;
1339 monitor
.get_job_attrs
= get_job_attrs
;
1340 monitor
.encryption
= cupsEncryption();
1341 monitor
.job_state
= IPP_JOB_PENDING
;
1342 monitor
.printer_state
= IPP_PRINTER_IDLE
;
1346 monitor
.job_name
= argv
[3];
1350 snprintf(print_job_name
, sizeof(print_job_name
), "%s - %s", argv
[1],
1352 monitor
.job_name
= print_job_name
;
1355 _cupsThreadCreate((_cups_thread_func_t
)monitor_printer
, &monitor
);
1358 * Validate access to the printer...
1361 while (!job_canceled
&& validate_job
)
1363 request
= new_request(IPP_VALIDATE_JOB
, version
, uri
, argv
[2],
1364 monitor
.job_name
, num_options
, options
, compression
,
1365 copies_sup
? copies
: 1, document_format
, pc
, ppd
,
1366 media_col_sup
, doc_handling_sup
, print_color_mode
);
1368 ippDelete(cupsDoRequest(http
, request
, resource
));
1370 ipp_status
= cupsLastError();
1372 fprintf(stderr
, "DEBUG: Validate-Job: %s (%s)\n",
1373 ippErrorString(ipp_status
), cupsLastErrorString());
1378 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
|| ipp_status
== IPP_PRINTER_BUSY
)
1380 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
1383 else if (ipp_status
== IPP_DOCUMENT_FORMAT
||
1384 ipp_status
== CUPS_ACCOUNT_INFO_NEEDED
||
1385 ipp_status
== CUPS_ACCOUNT_CLOSED
||
1386 ipp_status
== CUPS_ACCOUNT_LIMIT_REACHED
||
1387 ipp_status
== CUPS_ACCOUNT_AUTHORIZATION_FAILED
)
1389 else if (ipp_status
== IPP_FORBIDDEN
||
1390 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1392 const char *www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
1393 /* WWW-Authenticate field value */
1395 if (!strncmp(www_auth
, "Negotiate", 9))
1396 auth_info_required
= "negotiate";
1397 else if (www_auth
[0])
1398 auth_info_required
= "username,password";
1402 else if (ipp_status
== IPP_OPERATION_NOT_SUPPORTED
)
1405 * This is all too common...
1408 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1409 "cups-ipp-missing-validate-job");
1412 else if (ipp_status
< IPP_REDIRECTION_OTHER_SITE
||
1413 ipp_status
== IPP_BAD_REQUEST
)
1418 * Then issue the print-job request...
1423 while (!job_canceled
&& copies_remaining
> 0)
1426 * Check for side-channel requests...
1429 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1432 * Build the IPP job creation request...
1438 request
= new_request((num_files
> 1 || create_job
) ? IPP_CREATE_JOB
:
1440 version
, uri
, argv
[2], monitor
.job_name
, num_options
,
1441 options
, compression
, copies_sup
? copies
: 1,
1442 document_format
, pc
, ppd
, media_col_sup
,
1443 doc_handling_sup
, print_color_mode
);
1449 if (num_files
> 1 || create_job
)
1450 response
= cupsDoRequest(http
, request
, resource
);
1453 size_t length
= 0; /* Length of request */
1457 fputs("DEBUG: Sending file using HTTP/1.0 Content-Length...\n", stderr
);
1458 length
= ippLength(request
) + (size_t)compatsize
;
1461 fputs("DEBUG: Sending file using HTTP/1.1 chunking...\n", stderr
);
1463 http_status
= cupsSendRequest(http
, request
, resource
, length
);
1464 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
)
1468 if ((fd
= open(files
[0], O_RDONLY
)) < 0)
1470 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1471 return (CUPS_BACKEND_FAILED
);
1477 http_status
= cupsWriteRequestData(http
, buffer
, bytes
);
1480 while (http_status
== HTTP_CONTINUE
&&
1481 (!job_canceled
|| compatsize
> 0))
1484 * Check for side-channel requests and more print data...
1489 FD_SET(snmp_fd
, &input
);
1491 while (select(fd
> snmp_fd
? fd
+ 1 : snmp_fd
+ 1, &input
, NULL
, NULL
,
1492 NULL
) <= 0 && !job_canceled
);
1494 if (FD_ISSET(snmp_fd
, &input
))
1495 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1497 if (FD_ISSET(fd
, &input
))
1499 if ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1501 fprintf(stderr
, "DEBUG: Read %d bytes...\n", (int)bytes
);
1503 if (cupsWriteRequestData(http
, buffer
, bytes
) != HTTP_CONTINUE
)
1506 else if (bytes
== 0 || (errno
!= EINTR
&& errno
!= EAGAIN
))
1515 response
= cupsGetResponse(http
, resource
);
1519 ipp_status
= cupsLastError();
1521 fprintf(stderr
, "DEBUG: %s: %s (%s)\n",
1522 (num_files
> 1 || create_job
) ? "Create-Job" : "Print-Job",
1523 ippErrorString(ipp_status
), cupsLastErrorString());
1525 if (ipp_status
> IPP_OK_CONFLICT
)
1532 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1533 ipp_status
== IPP_NOT_POSSIBLE
||
1534 ipp_status
== IPP_PRINTER_BUSY
)
1536 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
1542 * We can't re-submit when we have no files to print, so exit
1543 * immediately with the right status code...
1549 else if (ipp_status
== IPP_ERROR_JOB_CANCELED
||
1550 ipp_status
== IPP_NOT_AUTHORIZED
||
1551 ipp_status
== CUPS_ACCOUNT_INFO_NEEDED
||
1552 ipp_status
== CUPS_ACCOUNT_CLOSED
||
1553 ipp_status
== CUPS_ACCOUNT_LIMIT_REACHED
||
1554 ipp_status
== CUPS_ACCOUNT_AUTHORIZATION_FAILED
)
1559 * Update auth-info-required as needed...
1562 _cupsLangPrintFilter(stderr
, "ERROR",
1563 _("Print job was not accepted."));
1565 if (ipp_status
== IPP_FORBIDDEN
||
1566 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1568 const char *www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
1569 /* WWW-Authenticate field value */
1571 if (!strncmp(www_auth
, "Negotiate", 9))
1572 auth_info_required
= "negotiate";
1573 else if (www_auth
[0])
1574 auth_info_required
= "username,password";
1576 else if (ipp_status
== IPP_REQUEST_VALUE
)
1579 * Print file is too large, abort this job...
1590 * We can't re-submit when we have no files to print, so exit
1591 * immediately with the right status code...
1598 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
1599 IPP_TAG_INTEGER
)) == NULL
)
1601 fputs("DEBUG: Print job accepted - job ID unknown.\n", stderr
);
1602 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1603 "cups-ipp-missing-job-id");
1609 monitor
.job_id
= job_id
= job_id_attr
->values
[0].integer
;
1610 fprintf(stderr
, "DEBUG: Print job accepted - job ID %d.\n", job_id
);
1613 ippDelete(response
);
1618 if (job_id
&& (num_files
> 1 || create_job
))
1620 for (i
= 0; num_files
== 0 || i
< num_files
; i
++)
1623 * Check for side-channel requests...
1626 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1629 * Send the next file in the job...
1632 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
1633 request
->request
.op
.version
[0] = version
/ 10;
1634 request
->request
.op
.version
[1] = version
% 10;
1636 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1639 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1643 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1644 "requesting-user-name", NULL
, argv
[2]);
1646 if ((i
+ 1) >= num_files
)
1647 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
1649 if (document_format
)
1650 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1651 "document-format", NULL
, document_format
);
1654 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1655 "compression", NULL
, compression
);
1657 fprintf(stderr
, "DEBUG: Sending file %d using chunking...\n", i
+ 1);
1658 http_status
= cupsSendRequest(http
, request
, resource
, 0);
1659 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
)
1664 http_status
= cupsWriteRequestData(http
, buffer
, bytes
);
1668 if ((fd
= open(files
[i
], O_RDONLY
)) < 0)
1670 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1671 return (CUPS_BACKEND_FAILED
);
1680 while (!job_canceled
&& http_status
== HTTP_CONTINUE
&&
1681 (bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1683 if ((http_status
= cupsWriteRequestData(http
, buffer
, bytes
))
1689 * Check for side-channel requests...
1692 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1700 ippDelete(cupsGetResponse(http
, resource
));
1703 fprintf(stderr
, "DEBUG: Send-Document: %s (%s)\n",
1704 ippErrorString(cupsLastError()), cupsLastErrorString());
1706 if (cupsLastError() > IPP_OK_CONFLICT
)
1708 ipp_status
= cupsLastError();
1710 _cupsLangPrintFilter(stderr
, "ERROR",
1711 _("Unable to add document to print job."));
1718 if (num_files
== 0 || fd
< 0)
1724 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
1726 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
1727 copies_remaining
--;
1729 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1730 ipp_status
== IPP_NOT_POSSIBLE
||
1731 ipp_status
== IPP_PRINTER_BUSY
)
1736 * Need to reprocess the entire job; if we have a job ID, cancel the
1741 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1747 else if (ipp_status
== IPP_REQUEST_VALUE
||
1748 ipp_status
== IPP_ERROR_JOB_CANCELED
||
1749 ipp_status
== IPP_NOT_AUTHORIZED
||
1750 ipp_status
== CUPS_ACCOUNT_INFO_NEEDED
||
1751 ipp_status
== CUPS_ACCOUNT_CLOSED
||
1752 ipp_status
== CUPS_ACCOUNT_LIMIT_REACHED
||
1753 ipp_status
== CUPS_ACCOUNT_AUTHORIZATION_FAILED
||
1754 ipp_status
== IPP_INTERNAL_ERROR
)
1757 * Print file is too large, job was canceled, we need new
1758 * authentication data, or we had some sort of error...
1763 else if (ipp_status
== IPP_NOT_FOUND
)
1766 * Printer does not actually implement support for Create-Job/
1767 * Send-Document, so log the conformance issue and stop the printer.
1770 fputs("DEBUG: This printer claims to support Create-Job and "
1771 "Send-Document, but those operations failed.\n", stderr
);
1772 fputs("DEBUG: Add '?version=1.0' to the device URI to use legacy "
1773 "compatibility mode.\n", stderr
);
1774 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1775 "cups-ipp-missing-send-document");
1777 ipp_status
= IPP_INTERNAL_ERROR
; /* Force queue to stop */
1782 copies_remaining
--;
1785 * Wait for the job to complete...
1788 if (!job_id
|| !waitjob
|| !get_job_attrs
)
1791 _cupsLangPrintFilter(stderr
, "INFO", _("Waiting for job to complete."));
1793 for (delay
= _cupsNextDelay(0, &prev_delay
); !job_canceled
;)
1796 * Check for side-channel requests...
1799 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1802 * Build an IPP_GET_JOB_ATTRIBUTES request...
1805 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1806 request
->request
.op
.version
[0] = version
/ 10;
1807 request
->request
.op
.version
[1] = version
% 10;
1809 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1812 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1816 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1817 "requesting-user-name", NULL
, argv
[2]);
1819 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1820 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
1827 httpReconnect(http
);
1828 response
= cupsDoRequest(http
, request
, resource
);
1829 ipp_status
= cupsLastError();
1831 if (ipp_status
== IPP_NOT_FOUND
|| ipp_status
== IPP_NOT_POSSIBLE
)
1834 * Job has gone away and/or the server has no job history...
1837 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1838 "cups-ipp-missing-job-history");
1839 ippDelete(response
);
1841 ipp_status
= IPP_OK
;
1845 fprintf(stderr
, "DEBUG: Get-Job-Attributes: %s (%s)\n",
1846 ippErrorString(ipp_status
), cupsLastErrorString());
1848 if (ipp_status
<= IPP_OK_CONFLICT
)
1852 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1853 ipp_status
!= IPP_PRINTER_BUSY
)
1855 ippDelete(response
);
1856 ipp_status
= IPP_OK
;
1859 else if (ipp_status
== IPP_INTERNAL_ERROR
)
1863 if (waitjob_tries
> 4)
1865 ippDelete(response
);
1866 ipp_status
= IPP_OK
;
1874 if ((job_state
= ippFindAttribute(response
, "job-state",
1875 IPP_TAG_ENUM
)) != NULL
)
1878 * Reflect the remote job state in the local queue...
1882 job_state
->values
[0].integer
>= IPP_JOB_PENDING
&&
1883 job_state
->values
[0].integer
<= IPP_JOB_COMPLETED
)
1884 update_reasons(NULL
,
1885 remote_job_states
[job_state
->values
[0].integer
-
1888 if ((job_sheets
= ippFindAttribute(response
,
1889 "job-media-sheets-completed",
1890 IPP_TAG_INTEGER
)) == NULL
)
1891 job_sheets
= ippFindAttribute(response
,
1892 "job-impressions-completed",
1896 fprintf(stderr
, "PAGE: total %d\n",
1897 job_sheets
->values
[0].integer
);
1900 * Stop polling if the job is finished or pending-held...
1903 if (job_state
->values
[0].integer
> IPP_JOB_STOPPED
)
1905 ippDelete(response
);
1909 else if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1910 ipp_status
!= IPP_NOT_POSSIBLE
&&
1911 ipp_status
!= IPP_PRINTER_BUSY
)
1914 * If the printer does not return a job-state attribute, it does not
1915 * conform to the IPP specification - break out immediately and fail
1919 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1920 "cups-ipp-missing-job-state");
1921 ipp_status
= IPP_INTERNAL_ERROR
;
1926 ippDelete(response
);
1929 * Wait before polling again...
1934 delay
= _cupsNextDelay(delay
, &prev_delay
);
1939 * Cancel the job as needed...
1942 if (job_canceled
> 0 && job_id
> 0)
1943 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1946 * Check the printer state and report it if necessary...
1949 check_printer_state(http
, uri
, resource
, argv
[2], version
);
1952 * Collect the final page count as needed...
1955 if (have_supplies
&&
1956 !backendSNMPSupplies(snmp_fd
, http
->hostaddr
, &page_count
, NULL
) &&
1957 page_count
> start_count
)
1958 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
1962 * See if we used Kerberos at all...
1966 auth_info_required
= "negotiate";
1967 #endif /* HAVE_GSSAPI */
1975 cupsFreeOptions(num_options
, options
);
1976 _ppdCacheDestroy(pc
);
1981 ippDelete(supported
);
1984 * Remove the temporary file(s) if necessary...
1988 unlink(tmpfilename
);
1993 for (i
= 0; i
< num_files
; i
++)
1996 #endif /* HAVE_LIBZ */
1999 * Return the queue status...
2002 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
2003 ipp_status
== IPP_AUTHENTICATION_CANCELED
||
2004 ipp_status
<= IPP_OK_CONFLICT
)
2005 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
2007 if (ipp_status
== CUPS_ACCOUNT_INFO_NEEDED
)
2008 fputs("JOBSTATE: account-info-needed\n", stderr
);
2009 else if (ipp_status
== CUPS_ACCOUNT_CLOSED
)
2010 fputs("JOBSTATE: account-closed\n", stderr
);
2011 else if (ipp_status
== CUPS_ACCOUNT_LIMIT_REACHED
)
2012 fputs("JOBSTATE: account-limit-reached\n", stderr
);
2013 else if (ipp_status
== CUPS_ACCOUNT_AUTHORIZATION_FAILED
)
2014 fputs("JOBSTATE: account-authorization-failed\n", stderr
);
2016 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
2017 ipp_status
== IPP_AUTHENTICATION_CANCELED
||
2018 ipp_status
== CUPS_ACCOUNT_INFO_NEEDED
||
2019 ipp_status
== CUPS_ACCOUNT_CLOSED
||
2020 ipp_status
== CUPS_ACCOUNT_LIMIT_REACHED
||
2021 ipp_status
== CUPS_ACCOUNT_AUTHORIZATION_FAILED
)
2022 return (CUPS_BACKEND_AUTH_REQUIRED
);
2023 else if (ipp_status
== IPP_INTERNAL_ERROR
)
2024 return (CUPS_BACKEND_STOP
);
2025 else if (ipp_status
== IPP_CONFLICT
)
2026 return (CUPS_BACKEND_FAILED
);
2027 else if (ipp_status
== IPP_REQUEST_VALUE
||
2028 ipp_status
== IPP_DOCUMENT_FORMAT
|| job_canceled
< 0)
2030 if (ipp_status
== IPP_REQUEST_VALUE
)
2031 _cupsLangPrintFilter(stderr
, "ERROR", _("Print job too large."));
2032 else if (ipp_status
== IPP_DOCUMENT_FORMAT
)
2033 _cupsLangPrintFilter(stderr
, "ERROR",
2034 _("Printer cannot print supplied content."));
2036 _cupsLangPrintFilter(stderr
, "ERROR", _("Print job canceled at printer."));
2038 return (CUPS_BACKEND_CANCEL
);
2040 else if (ipp_status
> IPP_OK_CONFLICT
&& ipp_status
!= IPP_ERROR_JOB_CANCELED
)
2041 return (CUPS_BACKEND_RETRY_CURRENT
);
2043 return (CUPS_BACKEND_OK
);
2048 * 'cancel_job()' - Cancel a print job.
2052 cancel_job(http_t
*http
, /* I - HTTP connection */
2053 const char *uri
, /* I - printer-uri */
2054 int id
, /* I - job-id */
2055 const char *resource
, /* I - Resource path */
2056 const char *user
, /* I - requesting-user-name */
2057 int version
) /* I - IPP version */
2059 ipp_t
*request
; /* Cancel-Job request */
2062 _cupsLangPrintFilter(stderr
, "INFO", _("Canceling print job."));
2064 request
= ippNewRequest(IPP_CANCEL_JOB
);
2065 request
->request
.op
.version
[0] = version
/ 10;
2066 request
->request
.op
.version
[1] = version
% 10;
2068 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2070 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
2072 if (user
&& user
[0])
2073 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2074 "requesting-user-name", NULL
, user
);
2080 ippDelete(cupsDoRequest(http
, request
, resource
));
2082 if (cupsLastError() > IPP_OK_CONFLICT
)
2083 _cupsLangPrintFilter(stderr
, "ERROR", _("Unable to cancel print job."));
2088 * 'check_printer_state()' - Check the printer state.
2091 static ipp_pstate_t
/* O - Current printer-state */
2092 check_printer_state(
2093 http_t
*http
, /* I - HTTP connection */
2094 const char *uri
, /* I - Printer URI */
2095 const char *resource
, /* I - Resource path */
2096 const char *user
, /* I - Username, if any */
2097 int version
) /* I - IPP version */
2099 ipp_t
*request
, /* IPP request */
2100 *response
; /* IPP response */
2101 ipp_attribute_t
*attr
; /* Attribute in response */
2102 ipp_pstate_t printer_state
= IPP_PRINTER_STOPPED
;
2103 /* Current printer-state */
2107 * Send a Get-Printer-Attributes request and log the results...
2110 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
2111 request
->request
.op
.version
[0] = version
/ 10;
2112 request
->request
.op
.version
[1] = version
% 10;
2114 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2117 if (user
&& user
[0])
2118 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2119 "requesting-user-name", NULL
, user
);
2121 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2122 "requested-attributes",
2123 (int)(sizeof(pattrs
) / sizeof(pattrs
[0])), NULL
, pattrs
);
2125 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
2127 report_printer_state(response
);
2129 if ((attr
= ippFindAttribute(response
, "printer-state",
2130 IPP_TAG_ENUM
)) != NULL
)
2131 printer_state
= (ipp_pstate_t
)attr
->values
[0].integer
;
2133 ippDelete(response
);
2136 fprintf(stderr
, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
2137 ippErrorString(cupsLastError()), cupsLastErrorString());
2139 if (cupsLastError() <= IPP_OK_CONFLICT
)
2143 * Return the printer-state value...
2146 return (printer_state
);
2152 * 'compress_files()' - Compress print files.
2156 compress_files(int num_files
, /* I - Number of files */
2157 char **files
) /* I - Files */
2159 int i
, /* Looping var */
2160 fd
; /* Temporary file descriptor */
2161 ssize_t bytes
; /* Bytes read/written */
2162 size_t total
; /* Total bytes read */
2163 cups_file_t
*in
, /* Input file */
2164 *out
; /* Output file */
2165 struct stat outinfo
; /* Output file information */
2166 char filename
[1024], /* Temporary filename */
2167 buffer
[32768]; /* Copy buffer */
2170 fprintf(stderr
, "DEBUG: Compressing %d job files...\n", num_files
);
2171 for (i
= 0; i
< num_files
; i
++)
2173 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
2175 _cupsLangPrintError("ERROR", _("Unable to create compressed print file"));
2176 exit(CUPS_BACKEND_FAILED
);
2179 if ((out
= cupsFileOpenFd(fd
, "w9")) == NULL
)
2181 _cupsLangPrintError("ERROR", _("Unable to open compressed print file"));
2182 exit(CUPS_BACKEND_FAILED
);
2185 if ((in
= cupsFileOpen(files
[i
], "r")) == NULL
)
2187 _cupsLangPrintError("ERROR", _("Unable to open print file"));
2189 exit(CUPS_BACKEND_FAILED
);
2193 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
2194 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
2196 _cupsLangPrintError("ERROR",
2197 _("Unable to generate compressed print file"));
2200 exit(CUPS_BACKEND_FAILED
);
2208 files
[i
] = strdup(filename
);
2210 if (!stat(filename
, &outinfo
))
2212 "DEBUG: File %d compressed to %.1f%% of original size, "
2213 CUPS_LLFMT
" bytes...\n",
2214 i
+ 1, 100.0 * outinfo
.st_size
/ total
,
2215 CUPS_LLCAST outinfo
.st_size
);
2218 #endif /* HAVE_LIBZ */
2222 * 'monitor_printer()' - Monitor the printer state.
2225 static void * /* O - Thread exit code */
2227 _cups_monitor_t
*monitor
) /* I - Monitoring data */
2229 http_t
*http
; /* Connection to printer */
2230 ipp_t
*request
, /* IPP request */
2231 *response
; /* IPP response */
2232 ipp_attribute_t
*attr
; /* Attribute in response */
2233 int delay
, /* Current delay */
2234 prev_delay
; /* Previous delay */
2235 ipp_op_t job_op
; /* Operation to use */
2236 int job_id
; /* Job ID */
2237 const char *job_name
; /* Job name */
2238 ipp_jstate_t job_state
; /* Job state */
2239 const char *job_user
; /* Job originating user name */
2243 * Make a copy of the printer connection...
2246 http
= _httpCreate(monitor
->hostname
, monitor
->port
, NULL
, AF_UNSPEC
,
2247 monitor
->encryption
, 1, _HTTP_MODE_CLIENT
);
2248 httpSetTimeout(http
, 30.0, timeout_cb
, NULL
);
2250 cupsSetUser(username
);
2251 cupsSetPasswordCB2(password_cb
, NULL
);
2254 * Loop until the job is canceled, aborted, or completed.
2257 delay
= _cupsNextDelay(0, &prev_delay
);
2259 monitor
->job_reasons
= 0;
2261 while (monitor
->job_state
< IPP_JOB_CANCELED
&& !job_canceled
)
2264 * Reconnect to the printer...
2267 if (!httpReconnect(http
))
2270 * Connected, so check on the printer state...
2273 monitor
->printer_state
= check_printer_state(http
, monitor
->uri
,
2279 * Check the status of the job itself...
2282 job_op
= (monitor
->job_id
> 0 && monitor
->get_job_attrs
) ?
2283 IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
;
2284 request
= ippNewRequest(job_op
);
2285 request
->request
.op
.version
[0] = monitor
->version
/ 10;
2286 request
->request
.op
.version
[1] = monitor
->version
% 10;
2288 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2289 NULL
, monitor
->uri
);
2290 if (job_op
== IPP_GET_JOB_ATTRIBUTES
)
2291 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
2294 if (monitor
->user
&& monitor
->user
[0])
2295 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2296 "requesting-user-name", NULL
, monitor
->user
);
2298 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2299 "requested-attributes",
2300 (int)(sizeof(jattrs
) / sizeof(jattrs
[0])), NULL
, jattrs
);
2306 response
= cupsDoRequest(http
, request
, monitor
->resource
);
2308 fprintf(stderr
, "DEBUG: (monitor) %s: %s (%s)\n", ippOpString(job_op
),
2309 ippErrorString(cupsLastError()), cupsLastErrorString());
2311 if (cupsLastError() <= IPP_OK_CONFLICT
)
2314 if (job_op
== IPP_GET_JOB_ATTRIBUTES
)
2316 if ((attr
= ippFindAttribute(response
, "job-state",
2317 IPP_TAG_ENUM
)) != NULL
)
2318 monitor
->job_state
= (ipp_jstate_t
)attr
->values
[0].integer
;
2320 monitor
->job_state
= IPP_JOB_COMPLETED
;
2324 for (attr
= response
->attrs
; attr
; attr
= attr
->next
)
2328 job_state
= IPP_JOB_PENDING
;
2331 while (attr
&& attr
->group_tag
!= IPP_TAG_JOB
)
2337 while (attr
&& attr
->group_tag
== IPP_TAG_JOB
)
2339 if (!strcmp(attr
->name
, "job-id") &&
2340 attr
->value_tag
== IPP_TAG_INTEGER
)
2341 job_id
= attr
->values
[0].integer
;
2342 else if (!strcmp(attr
->name
, "job-name") &&
2343 (attr
->value_tag
== IPP_TAG_NAME
||
2344 attr
->value_tag
== IPP_TAG_NAMELANG
))
2345 job_name
= attr
->values
[0].string
.text
;
2346 else if (!strcmp(attr
->name
, "job-state") &&
2347 attr
->value_tag
== IPP_TAG_ENUM
)
2348 job_state
= attr
->values
[0].integer
;
2349 else if (!strcmp(attr
->name
, "job-originating-user-name") &&
2350 (attr
->value_tag
== IPP_TAG_NAME
||
2351 attr
->value_tag
== IPP_TAG_NAMELANG
))
2352 job_user
= attr
->values
[0].string
.text
;
2357 if (job_id
> 0 && job_name
&& !strcmp(job_name
, monitor
->job_name
) &&
2358 job_user
&& monitor
->user
&& !strcmp(job_user
, monitor
->user
))
2360 monitor
->job_id
= job_id
;
2361 monitor
->job_state
= job_state
;
2370 if ((attr
= ippFindAttribute(response
, "job-state-reasons",
2371 IPP_TAG_KEYWORD
)) != NULL
)
2373 int i
, new_reasons
= 0; /* Looping var, new reasons */
2375 for (i
= 0; i
< attr
->num_values
; i
++)
2377 if (!strcmp(attr
->values
[i
].string
.text
,
2378 "account-authorization-failed"))
2379 new_reasons
|= _CUPS_JSR_ACCOUNT_AUTHORIZATION_FAILED
;
2380 else if (!strcmp(attr
->values
[i
].string
.text
, "account-closed"))
2381 new_reasons
|= _CUPS_JSR_ACCOUNT_CLOSED
;
2382 else if (!strcmp(attr
->values
[i
].string
.text
, "account-info-needed"))
2383 new_reasons
|= _CUPS_JSR_ACCOUNT_INFO_NEEDED
;
2384 else if (!strcmp(attr
->values
[i
].string
.text
,
2385 "account-limit-reached"))
2386 new_reasons
|= _CUPS_JSR_ACCOUNT_LIMIT_REACHED
;
2387 else if (!strcmp(attr
->values
[i
].string
.text
, "job-password-wait"))
2388 new_reasons
|= _CUPS_JSR_JOB_PASSWORD_WAIT
;
2389 else if (!strcmp(attr
->values
[i
].string
.text
, "job-release-wait"))
2390 new_reasons
|= _CUPS_JSR_JOB_RELEASE_WAIT
;
2393 if (new_reasons
!= monitor
->job_reasons
)
2395 if (new_reasons
& _CUPS_JSR_ACCOUNT_AUTHORIZATION_FAILED
)
2396 fputs("JOBSTATE: account-authorization-failed\n", stderr
);
2397 else if (new_reasons
& _CUPS_JSR_ACCOUNT_CLOSED
)
2398 fputs("JOBSTATE: account-closed\n", stderr
);
2399 else if (new_reasons
& _CUPS_JSR_ACCOUNT_INFO_NEEDED
)
2400 fputs("JOBSTATE: account-info-needed\n", stderr
);
2401 else if (new_reasons
& _CUPS_JSR_ACCOUNT_LIMIT_REACHED
)
2402 fputs("JOBSTATE: account-limit-reached\n", stderr
);
2403 else if (new_reasons
& _CUPS_JSR_JOB_PASSWORD_WAIT
)
2404 fputs("JOBSTATE: job-password-wait\n", stderr
);
2405 else if (new_reasons
& _CUPS_JSR_JOB_RELEASE_WAIT
)
2406 fputs("JOBSTATE: job-release-wait\n", stderr
);
2408 fputs("JOBSTATE: job-printing\n", stderr
);
2410 monitor
->job_reasons
= new_reasons
;
2414 ippDelete(response
);
2416 fprintf(stderr
, "DEBUG: (monitor) job-state=%s\n",
2417 ippEnumString("job-state", monitor
->job_state
));
2419 if (!job_canceled
&&
2420 (monitor
->job_state
== IPP_JOB_CANCELED
||
2421 monitor
->job_state
== IPP_JOB_ABORTED
))
2425 * Disconnect from the printer - we'll reconnect on the next poll...
2428 _httpDisconnect(http
);
2432 * Sleep for N seconds...
2437 delay
= _cupsNextDelay(delay
, &prev_delay
);
2441 * Cancel the job if necessary...
2444 if (job_canceled
> 0 && monitor
->job_id
> 0)
2445 if (!httpReconnect(http
))
2446 cancel_job(http
, monitor
->uri
, monitor
->job_id
, monitor
->resource
,
2447 monitor
->user
, monitor
->version
);
2450 * Cleanup and return...
2460 * 'new_request()' - Create a new print creation or validation request.
2463 static ipp_t
* /* O - Request data */
2465 ipp_op_t op
, /* I - IPP operation code */
2466 int version
, /* I - IPP version number */
2467 const char *uri
, /* I - printer-uri value */
2468 const char *user
, /* I - requesting-user-name value */
2469 const char *title
, /* I - job-name value */
2470 int num_options
, /* I - Number of options to send */
2471 cups_option_t
*options
, /* I - Options to send */
2472 const char *compression
, /* I - compression value or NULL */
2473 int copies
, /* I - copies value or 0 */
2474 const char *format
, /* I - document-format value or NULL */
2475 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2476 ppd_file_t
*ppd
, /* I - PPD file data */
2477 ipp_attribute_t
*media_col_sup
, /* I - media-col-supported values */
2478 ipp_attribute_t
*doc_handling_sup
, /* I - multiple-document-handling-supported values */
2479 int print_color_mode
) /* I - Printer supports print-color-mode */
2481 int i
; /* Looping var */
2482 ipp_t
*request
; /* Request data */
2483 const char *keyword
; /* PWG keyword */
2484 _pwg_size_t
*size
; /* PWG media size */
2485 ipp_t
*media_col
, /* media-col value */
2486 *media_size
; /* media-size value */
2487 const char *media_source
, /* media-source value */
2488 *media_type
, /* media-type value */
2489 *collate_str
, /* multiple-document-handling value */
2490 *mandatory
; /* Mandatory attributes */
2491 ipp_tag_t group
; /* Current group */
2492 ipp_attribute_t
*attr
; /* Current attribute */
2493 const char *color_attr_name
; /* Supported color attribute */
2494 char buffer
[1024]; /* Value buffer */
2498 * Create the IPP request...
2501 request
= ippNewRequest(op
);
2502 request
->request
.op
.version
[0] = version
/ 10;
2503 request
->request
.op
.version
[1] = version
% 10;
2505 fprintf(stderr
, "DEBUG: %s IPP/%d.%d\n",
2506 ippOpString(request
->request
.op
.operation_id
),
2507 request
->request
.op
.version
[0],
2508 request
->request
.op
.version
[1]);
2511 * Add standard attributes...
2514 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2516 fprintf(stderr
, "DEBUG: printer-uri=\"%s\"\n", uri
);
2520 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2521 "requesting-user-name", NULL
, user
);
2522 fprintf(stderr
, "DEBUG: requesting-user-name=\"%s\"\n", user
);
2525 if (title
&& *title
)
2527 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
2529 fprintf(stderr
, "DEBUG: job-name=\"%s\"\n", title
);
2532 if (format
&& op
!= IPP_CREATE_JOB
)
2534 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
2535 "document-format", NULL
, format
);
2536 fprintf(stderr
, "DEBUG: document-format=\"%s\"\n", format
);
2540 if (compression
&& op
!= IPP_CREATE_JOB
)
2542 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2543 "compression", NULL
, compression
);
2544 fprintf(stderr
, "DEBUG: compression=\"%s\"\n", compression
);
2546 #endif /* HAVE_LIBZ */
2549 * Handle options on the command-line...
2552 if (num_options
> 0)
2556 int num_finishings
= 0, /* Number of finishing values */
2557 finishings
[10]; /* Finishing enum values */
2558 ppd_choice_t
*choice
; /* Marked choice */
2561 * Send standard IPP attributes...
2564 fputs("DEBUG: Adding standard IPP operation/job attributes.\n", stderr
);
2567 (keyword
= cupsGetOption("job-password", num_options
,
2570 ippAddOctetString(request
, IPP_TAG_OPERATION
, "job-password",
2571 keyword
, strlen(keyword
));
2573 if ((keyword
= cupsGetOption("job-password-encryption", num_options
,
2577 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2578 "job-password-encryption", NULL
, keyword
);
2581 if (pc
->account_id
&&
2582 (keyword
= cupsGetOption("job-account-id", num_options
,
2584 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-account-id",
2587 if (pc
->account_id
&&
2588 (keyword
= cupsGetOption("job-accounting-user-id", num_options
,
2590 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_NAME
,
2591 "job-accounting-user-id", NULL
, keyword
);
2593 for (mandatory
= (char *)cupsArrayFirst(pc
->mandatory
);
2595 mandatory
= (char *)cupsArrayNext(pc
->mandatory
))
2597 if (strcmp(mandatory
, "copies") &&
2598 strcmp(mandatory
, "destination-uris") &&
2599 strcmp(mandatory
, "finishings") &&
2600 strcmp(mandatory
, "job-account-id") &&
2601 strcmp(mandatory
, "job-accounting-user-id") &&
2602 strcmp(mandatory
, "job-password") &&
2603 strcmp(mandatory
, "job-password-encryption") &&
2604 strcmp(mandatory
, "media") &&
2605 strncmp(mandatory
, "media-col", 9) &&
2606 strcmp(mandatory
, "multiple-document-handling") &&
2607 strcmp(mandatory
, "output-bin") &&
2608 strcmp(mandatory
, "print-color-mode") &&
2609 strcmp(mandatory
, "print-quality") &&
2610 strcmp(mandatory
, "sides") &&
2611 (keyword
= cupsGetOption(mandatory
, num_options
, options
)) != NULL
)
2613 _ipp_option_t
*opt
= _ippFindOption(mandatory
);
2615 ipp_tag_t value_tag
= opt
? opt
->value_tag
: IPP_TAG_NAME
;
2620 case IPP_TAG_INTEGER
:
2622 ippAddInteger(request
, IPP_TAG_JOB
, value_tag
, mandatory
,
2625 case IPP_TAG_BOOLEAN
:
2626 ippAddBoolean(request
, IPP_TAG_JOB
, mandatory
,
2627 !_cups_strcasecmp(keyword
, "true"));
2629 case IPP_TAG_RANGE
:
2631 int lower
, upper
; /* Range */
2633 if (sscanf(keyword
, "%d-%d", &lower
, &upper
) != 2)
2634 lower
= upper
= atoi(keyword
);
2636 ippAddRange(request
, IPP_TAG_JOB
, mandatory
, lower
, upper
);
2639 case IPP_TAG_STRING
:
2640 ippAddOctetString(request
, IPP_TAG_JOB
, mandatory
, keyword
,
2644 ippAddString(request
, IPP_TAG_JOB
, value_tag
, mandatory
,
2651 if ((keyword
= cupsGetOption("PageSize", num_options
, options
)) == NULL
)
2652 keyword
= cupsGetOption("media", num_options
, options
);
2654 if ((size
= _ppdCacheGetSize(pc
, keyword
)) != NULL
)
2657 * Add a media-col value...
2660 media_size
= ippNew();
2661 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2662 "x-dimension", size
->width
);
2663 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2664 "y-dimension", size
->length
);
2666 media_col
= ippNew();
2667 ippAddCollection(media_col
, IPP_TAG_ZERO
, "media-size", media_size
);
2669 media_source
= _ppdCacheGetSource(pc
, cupsGetOption("InputSlot",
2672 media_type
= _ppdCacheGetType(pc
, cupsGetOption("MediaType",
2676 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
2678 if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2679 "media-left-margin"))
2680 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2681 "media-left-margin", size
->left
);
2682 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2683 "media-bottom-margin"))
2684 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2685 "media-bottom-margin", size
->bottom
);
2686 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2687 "media-right-margin"))
2688 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2689 "media-right-margin", size
->right
);
2690 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2691 "media-top-margin"))
2692 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2693 "media-top-margin", size
->top
);
2694 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2695 "media-source") && media_source
)
2696 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
2697 "media-source", NULL
, media_source
);
2698 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2699 "media-type") && media_type
)
2700 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
2701 "media-type", NULL
, media_type
);
2704 ippAddCollection(request
, IPP_TAG_JOB
, "media-col", media_col
);
2707 if ((keyword
= cupsGetOption("output-bin", num_options
,
2710 if ((choice
= ppdFindMarkedChoice(ppd
, "OutputBin")) != NULL
)
2711 keyword
= _ppdCacheGetBin(pc
, choice
->choice
);
2715 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-bin",
2718 color_attr_name
= print_color_mode
? "print-color-mode" : "output-mode";
2720 if ((keyword
= cupsGetOption("print-color-mode", num_options
,
2722 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, color_attr_name
,
2724 else if ((choice
= ppdFindMarkedChoice(ppd
, "ColorModel")) != NULL
)
2726 if (!_cups_strcasecmp(choice
->choice
, "Gray"))
2727 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2728 color_attr_name
, NULL
, "monochrome");
2730 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2731 color_attr_name
, NULL
, "color");
2734 if ((keyword
= cupsGetOption("print-quality", num_options
,
2736 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2738 else if ((choice
= ppdFindMarkedChoice(ppd
, "cupsPrintQuality")) != NULL
)
2740 if (!_cups_strcasecmp(choice
->choice
, "draft"))
2741 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2743 else if (!_cups_strcasecmp(choice
->choice
, "normal"))
2744 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2745 IPP_QUALITY_NORMAL
);
2746 else if (!_cups_strcasecmp(choice
->choice
, "high"))
2747 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2751 if ((keyword
= cupsGetOption("sides", num_options
, options
)) != NULL
)
2752 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2754 else if (pc
->sides_option
&&
2755 (choice
= ppdFindMarkedChoice(ppd
, pc
->sides_option
)) != NULL
)
2757 if (!_cups_strcasecmp(choice
->choice
, pc
->sides_1sided
))
2758 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2760 else if (!_cups_strcasecmp(choice
->choice
, pc
->sides_2sided_long
))
2761 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2762 NULL
, "two-sided-long-edge");
2763 if (!_cups_strcasecmp(choice
->choice
, pc
->sides_2sided_short
))
2764 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2765 NULL
, "two-sided-short-edge");
2768 if ((keyword
= cupsGetOption("multiple-document-handling",
2769 num_options
, options
)) != NULL
)
2771 if (strstr(keyword
, "uncollated"))
2776 else if ((keyword
= cupsGetOption("collate", num_options
,
2782 if (!_cups_strcasecmp(format
, "image/gif") ||
2783 !_cups_strcasecmp(format
, "image/jp2") ||
2784 !_cups_strcasecmp(format
, "image/jpeg") ||
2785 !_cups_strcasecmp(format
, "image/png") ||
2786 !_cups_strcasecmp(format
, "image/tiff") ||
2787 !_cups_strncasecmp(format
, "image/x-", 8))
2790 * Collation makes no sense for single page image formats...
2795 else if (!_cups_strncasecmp(format
, "image/", 6) ||
2796 !_cups_strcasecmp(format
, "application/vnd.cups-raster"))
2799 * Multi-page image formats will have copies applied by the upstream
2807 if (doc_handling_sup
)
2809 if (!_cups_strcasecmp(keyword
, "true"))
2810 collate_str
= "separate-documents-collated-copies";
2812 collate_str
= "separate-documents-uncollated-copies";
2814 for (i
= 0; i
< doc_handling_sup
->num_values
; i
++)
2815 if (!strcmp(doc_handling_sup
->values
[i
].string
.text
, collate_str
))
2817 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2818 "multiple-document-handling", NULL
, collate_str
);
2822 if (i
>= doc_handling_sup
->num_values
)
2827 * Map finishing options...
2830 num_finishings
= _ppdCacheGetFinishingValues(pc
, num_options
, options
,
2831 (int)(sizeof(finishings
) /
2832 sizeof(finishings
[0])),
2834 if (num_finishings
> 0)
2835 ippAddIntegers(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "finishings",
2836 num_finishings
, finishings
);
2839 * Map FaxOut options...
2842 if ((keyword
= cupsGetOption("phone", num_options
, options
)) != NULL
)
2844 ipp_t
*destination
; /* destination collection */
2845 char tel_uri
[1024]; /* tel: URI */
2847 destination
= ippNew();
2849 httpAssembleURI(HTTP_URI_CODING_ALL
, tel_uri
, sizeof(tel_uri
), "tel",
2850 NULL
, NULL
, 0, keyword
);
2851 ippAddString(destination
, IPP_TAG_JOB
, IPP_TAG_URI
, "destination-uri",
2854 if ((keyword
= cupsGetOption("faxPrefix", num_options
,
2855 options
)) != NULL
&& *keyword
)
2856 ippAddString(destination
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
2857 "pre-dial-string", NULL
, keyword
);
2859 ippAddCollection(request
, IPP_TAG_JOB
, "destination-uris", destination
);
2860 ippDelete(destination
);
2866 * When talking to another CUPS server, send all options...
2869 fputs("DEBUG: Adding all operation/job attributes.\n", stderr
);
2870 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_OPERATION
);
2871 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_JOB
);
2874 if (copies
> 1 && (!pc
|| copies
<= pc
->max_copies
))
2875 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies", copies
);
2878 fprintf(stderr
, "DEBUG: IPP/%d.%d %s #%d\n", version
/ 10, version
% 10,
2879 ippOpString(ippGetOperation(request
)), ippGetRequestId(request
));
2880 for (group
= IPP_TAG_ZERO
, attr
= ippFirstAttribute(request
);
2882 attr
= ippNextAttribute(request
))
2884 const char *name
= ippGetName(attr
);
2888 group
= IPP_TAG_ZERO
;
2892 if (group
!= ippGetGroupTag(attr
))
2894 group
= ippGetGroupTag(attr
);
2895 fprintf(stderr
, "DEBUG: ---- %s ----\n", ippTagString(group
));
2898 ippAttributeString(attr
, buffer
, sizeof(buffer
));
2899 fprintf(stderr
, "DEBUG: %s %s%s %s\n", name
,
2900 ippGetCount(attr
) > 1 ? "1setOf " : "",
2901 ippTagString(ippGetValueTag(attr
)), buffer
);
2904 fprintf(stderr
, "DEBUG: ---- %s ----\n", ippTagString(IPP_TAG_END
));
2911 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
2914 static const char * /* O - Password */
2915 password_cb(const char *prompt
, /* I - Prompt (not used) */
2916 http_t
*http
, /* I - Connection */
2917 const char *method
, /* I - Request method (not used) */
2918 const char *resource
, /* I - Resource path (not used) */
2919 void *user_data
) /* I - User data (not used) */
2921 char def_username
[HTTP_MAX_VALUE
]; /* Default username */
2924 fprintf(stderr
, "DEBUG: password_cb(prompt=\"%s\"), password=%p, "
2925 "password_tries=%d\n", prompt
, password
, password_tries
);
2933 * Remember that we need to authenticate...
2936 auth_info_required
= "username,password";
2938 if (httpGetSubField(http
, HTTP_FIELD_WWW_AUTHENTICATE
, "username",
2941 char quoted
[HTTP_MAX_VALUE
* 2 + 4];
2944 fprintf(stderr
, "ATTR: auth-info-default=%s,\n",
2945 quote_string(def_username
, quoted
, sizeof(quoted
)));
2948 if (password
&& *password
&& password_tries
< 3)
2957 * Give up after 3 tries or if we don't have a password to begin with...
2966 * 'quote_string()' - Quote a string value.
2969 static const char * /* O - Quoted string */
2970 quote_string(const char *s
, /* I - String */
2971 char *q
, /* I - Quoted string buffer */
2972 size_t qsize
) /* I - Size of quoted string buffer */
2974 char *qptr
, /* Pointer into string buffer */
2975 *qend
; /* End of string buffer */
2979 qend
= q
+ qsize
- 5;
2990 while (*s
&& qptr
< qend
)
2992 if (*s
== '\\' || *s
== '\"' || *s
== '\'')
3016 * 'report_attr()' - Report an IPP attribute value.
3020 report_attr(ipp_attribute_t
*attr
) /* I - Attribute */
3022 int i
; /* Looping var */
3023 char value
[1024], /* Value string */
3024 *valptr
; /* Pointer into value string */
3025 const char *cached
; /* Cached attribute */
3029 * Convert the attribute values into quoted strings...
3032 for (i
= 0, valptr
= value
;
3033 i
< attr
->num_values
&& valptr
< (value
+ sizeof(value
) - 10);
3039 switch (attr
->value_tag
)
3041 case IPP_TAG_INTEGER
:
3043 snprintf(valptr
, sizeof(value
) - (valptr
- value
), "%d",
3044 attr
->values
[i
].integer
);
3045 valptr
+= strlen(valptr
);
3050 case IPP_TAG_KEYWORD
:
3051 quote_string(attr
->values
[i
].string
.text
, valptr
,
3052 value
+ sizeof(value
) - valptr
);
3053 valptr
+= strlen(valptr
);
3058 * Unsupported value type...
3067 _cupsMutexLock(&report_mutex
);
3069 if ((cached
= cupsGetOption(attr
->name
, num_attr_cache
,
3070 attr_cache
)) == NULL
|| strcmp(cached
, value
))
3073 * Tell the scheduler about the new values...
3076 num_attr_cache
= cupsAddOption(attr
->name
, value
, num_attr_cache
,
3078 fprintf(stderr
, "ATTR: %s=%s\n", attr
->name
, value
);
3081 _cupsMutexUnlock(&report_mutex
);
3086 * 'report_printer_state()' - Report the printer state.
3090 report_printer_state(ipp_t
*ipp
) /* I - IPP response */
3092 ipp_attribute_t
*pa
, /* printer-alert */
3093 *pam
, /* printer-alert-message */
3094 *psm
, /* printer-state-message */
3095 *reasons
, /* printer-state-reasons */
3096 *marker
; /* marker-* attributes */
3097 char value
[1024], /* State/message string */
3098 *valptr
; /* Pointer into string */
3099 static int ipp_supplies
= -1;
3100 /* Report supply levels? */
3104 * Report alerts and messages...
3107 if ((pa
= ippFindAttribute(ipp
, "printer-alert", IPP_TAG_TEXT
)) != NULL
)
3110 if ((pam
= ippFindAttribute(ipp
, "printer-alert-message",
3111 IPP_TAG_TEXT
)) != NULL
)
3114 if ((psm
= ippFindAttribute(ipp
, "printer-state-message",
3115 IPP_TAG_TEXT
)) != NULL
)
3117 char *ptr
; /* Pointer into message */
3120 strlcpy(value
, "INFO: ", sizeof(value
));
3121 for (ptr
= psm
->values
[0].string
.text
, valptr
= value
+ 6;
3122 *ptr
&& valptr
< (value
+ sizeof(value
) - 6);
3125 if (*ptr
< ' ' && *ptr
> 0 && *ptr
!= '\t')
3128 * Substitute "<XX>" for the control character; sprintf is safe because
3129 * we always leave 6 chars free at the end...
3132 sprintf(valptr
, "<%02X>", *ptr
);
3142 fputs(value
, stderr
);
3146 * Now report printer-state-reasons, filtering out some of the reasons we never
3150 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
3151 IPP_TAG_KEYWORD
)) == NULL
)
3154 update_reasons(reasons
, NULL
);
3157 * Relay the current marker-* attribute values...
3160 if (ipp_supplies
< 0)
3162 ppd_file_t
*ppd
; /* PPD file */
3163 ppd_attr_t
*ppdattr
; /* Attribute in PPD file */
3165 if ((ppd
= ppdOpenFile(getenv("PPD"))) != NULL
&&
3166 (ppdattr
= ppdFindAttr(ppd
, "cupsIPPSupplies", NULL
)) != NULL
&&
3167 ppdattr
->value
&& _cups_strcasecmp(ppdattr
->value
, "true"))
3175 if (ipp_supplies
> 0)
3177 if ((marker
= ippFindAttribute(ipp
, "marker-colors", IPP_TAG_NAME
)) != NULL
)
3178 report_attr(marker
);
3179 if ((marker
= ippFindAttribute(ipp
, "marker-high-levels",
3180 IPP_TAG_INTEGER
)) != NULL
)
3181 report_attr(marker
);
3182 if ((marker
= ippFindAttribute(ipp
, "marker-levels",
3183 IPP_TAG_INTEGER
)) != NULL
)
3184 report_attr(marker
);
3185 if ((marker
= ippFindAttribute(ipp
, "marker-low-levels",
3186 IPP_TAG_INTEGER
)) != NULL
)
3187 report_attr(marker
);
3188 if ((marker
= ippFindAttribute(ipp
, "marker-message",
3189 IPP_TAG_TEXT
)) != NULL
)
3190 report_attr(marker
);
3191 if ((marker
= ippFindAttribute(ipp
, "marker-names", IPP_TAG_NAME
)) != NULL
)
3192 report_attr(marker
);
3193 if ((marker
= ippFindAttribute(ipp
, "marker-types",
3194 IPP_TAG_KEYWORD
)) != NULL
)
3195 report_attr(marker
);
3200 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
3202 * 'run_as_user()' - Run the IPP backend as the printing user.
3204 * This function uses an XPC-based user agent to run the backend as the printing
3205 * user. We need to do this in order to have access to the user's Kerberos
3209 static int /* O - Exit status */
3210 run_as_user(char *argv
[], /* I - Command-line arguments */
3211 uid_t uid
, /* I - User ID */
3212 const char *device_uri
, /* I - Device URI */
3213 int fd
) /* I - File to print */
3215 const char *auth_negotiate
;/* AUTH_NEGOTIATE env var */
3216 xpc_connection_t conn
; /* Connection to XPC service */
3217 xpc_object_t request
; /* Request message dictionary */
3218 __block xpc_object_t response
; /* Response message dictionary */
3219 dispatch_semaphore_t sem
; /* Semaphore for waiting for response */
3220 int status
= CUPS_BACKEND_FAILED
;
3221 /* Status of request */
3224 fprintf(stderr
, "DEBUG: Running IPP backend as UID %d.\n", (int)uid
);
3227 * Connect to the user agent for the specified UID...
3230 conn
= xpc_connection_create_mach_service(kPMPrintUIToolAgent
,
3231 dispatch_get_global_queue(0, 0), 0);
3234 _cupsLangPrintFilter(stderr
, "ERROR",
3235 _("Unable to start backend process."));
3236 fputs("DEBUG: Unable to create connection to agent.\n", stderr
);
3240 xpc_connection_set_event_handler(conn
,
3241 ^(xpc_object_t event
)
3243 xpc_type_t messageType
= xpc_get_type(event
);
3245 if (messageType
== XPC_TYPE_ERROR
)
3247 if (event
== XPC_ERROR_CONNECTION_INTERRUPTED
)
3248 fprintf(stderr
, "DEBUG: Interrupted connection to service %s.\n",
3249 xpc_connection_get_name(conn
));
3250 else if (event
== XPC_ERROR_CONNECTION_INVALID
)
3251 fprintf(stderr
, "DEBUG: Connection invalid for service %s.\n",
3252 xpc_connection_get_name(conn
));
3254 fprintf(stderr
, "DEBUG: Unxpected error for service %s: %s\n",
3255 xpc_connection_get_name(conn
),
3256 xpc_dictionary_get_string(event
, XPC_ERROR_KEY_DESCRIPTION
));
3259 xpc_connection_set_target_uid(conn
, uid
);
3260 xpc_connection_resume(conn
);
3263 * Try starting the backend...
3266 request
= xpc_dictionary_create(NULL
, NULL
, 0);
3267 xpc_dictionary_set_int64(request
, "command", kPMStartJob
);
3268 xpc_dictionary_set_string(request
, "device-uri", device_uri
);
3269 xpc_dictionary_set_string(request
, "job-id", argv
[1]);
3270 xpc_dictionary_set_string(request
, "user", argv
[2]);
3271 xpc_dictionary_set_string(request
, "title", argv
[3]);
3272 xpc_dictionary_set_string(request
, "copies", argv
[4]);
3273 xpc_dictionary_set_string(request
, "options", argv
[5]);
3274 xpc_dictionary_set_string(request
, "auth-info-required",
3275 getenv("AUTH_INFO_REQUIRED"));
3276 if ((auth_negotiate
= getenv("AUTH_NEGOTIATE")) != NULL
)
3277 xpc_dictionary_set_string(request
, "auth-negotiate", auth_negotiate
);
3278 xpc_dictionary_set_fd(request
, "stdin", fd
);
3279 xpc_dictionary_set_fd(request
, "stderr", 2);
3280 xpc_dictionary_set_fd(request
, "side-channel", CUPS_SC_FD
);
3282 sem
= dispatch_semaphore_create(0);
3285 xpc_connection_send_message_with_reply(conn
, request
,
3286 dispatch_get_global_queue(0,0),
3287 ^(xpc_object_t reply
)
3289 /* Save the response and wake up */
3290 if (xpc_get_type(reply
)
3291 == XPC_TYPE_DICTIONARY
)
3292 response
= xpc_retain(reply
);
3294 dispatch_semaphore_signal(sem
);
3297 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
3298 xpc_release(request
);
3299 dispatch_release(sem
);
3303 child_pid
= xpc_dictionary_get_int64(response
, "child-pid");
3305 xpc_release(response
);
3308 fprintf(stderr
, "DEBUG: Child PID=%d.\n", child_pid
);
3311 _cupsLangPrintFilter(stderr
, "ERROR",
3312 _("Unable to start backend process."));
3313 fputs("DEBUG: No child PID.\n", stderr
);
3319 _cupsLangPrintFilter(stderr
, "ERROR",
3320 _("Unable to start backend process."));
3321 fputs("DEBUG: No reply from agent.\n", stderr
);
3326 * Then wait for the backend to finish...
3329 request
= xpc_dictionary_create(NULL
, NULL
, 0);
3330 xpc_dictionary_set_int64(request
, "command", kPMWaitForJob
);
3331 xpc_dictionary_set_fd(request
, "stderr", 2);
3333 sem
= dispatch_semaphore_create(0);
3336 xpc_connection_send_message_with_reply(conn
, request
,
3337 dispatch_get_global_queue(0,0),
3338 ^(xpc_object_t reply
)
3340 /* Save the response and wake up */
3341 if (xpc_get_type(reply
)
3342 == XPC_TYPE_DICTIONARY
)
3343 response
= xpc_retain(reply
);
3345 dispatch_semaphore_signal(sem
);
3348 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
3349 xpc_release(request
);
3350 dispatch_release(sem
);
3354 status
= xpc_dictionary_get_int64(response
, "status");
3356 if (status
== SIGTERM
|| status
== SIGKILL
|| status
== SIGPIPE
)
3358 fprintf(stderr
, "DEBUG: Child terminated on signal %d.\n", status
);
3359 status
= CUPS_BACKEND_FAILED
;
3361 else if (WIFSIGNALED(status
))
3363 fprintf(stderr
, "DEBUG: Child crashed on signal %d.\n", status
);
3364 status
= CUPS_BACKEND_STOP
;
3366 else if (WIFEXITED(status
))
3368 status
= WEXITSTATUS(status
);
3369 fprintf(stderr
, "DEBUG: Child exited with status %d.\n", status
);
3372 xpc_release(response
);
3375 _cupsLangPrintFilter(stderr
, "ERROR",
3376 _("Unable to get backend exit status."));
3382 xpc_connection_suspend(conn
);
3383 xpc_connection_cancel(conn
);
3389 #endif /* HAVE_GSSAPI && HAVE_XPC */
3393 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
3397 sigterm_handler(int sig
) /* I - Signal */
3399 (void)sig
; /* remove compiler warnings... */
3401 write(2, "DEBUG: Got SIGTERM.\n", 20);
3403 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
3406 kill(child_pid
, sig
);
3409 #endif /* HAVE_GSSAPI && HAVE_XPC */
3414 * Flag that the job should be canceled...
3417 write(2, "DEBUG: job_canceled = 1.\n", 25);
3424 * The scheduler already tried to cancel us once, now just terminate
3425 * after removing our temp file!
3429 unlink(tmpfilename
);
3436 * 'timeout_cb()' - Handle HTTP timeouts.
3439 static int /* O - 1 to continue, 0 to cancel */
3440 timeout_cb(http_t
*http
, /* I - Connection to server (unused) */
3441 void *user_data
) /* I - User data (unused) */
3446 return (!job_canceled
);
3451 * 'update_reasons()' - Update the printer-state-reasons values.
3455 update_reasons(ipp_attribute_t
*attr
, /* I - printer-state-reasons or NULL */
3456 const char *s
) /* I - STATE: string or NULL */
3458 char op
; /* Add (+), remove (-), replace (\0) */
3459 cups_array_t
*new_reasons
; /* New reasons array */
3460 char *reason
, /* Current reason */
3461 add
[2048], /* Reasons added string */
3462 *addptr
, /* Pointer into add string */
3463 rem
[2048], /* Reasons removed string */
3464 *remptr
; /* Pointer into remove string */
3465 const char *addprefix
, /* Current add string prefix */
3466 *remprefix
; /* Current remove string prefix */
3469 fprintf(stderr
, "DEBUG: update_reasons(attr=%d(%s%s), s=\"%s\")\n",
3470 attr
? attr
->num_values
: 0, attr
? attr
->values
[0].string
.text
: "",
3471 attr
&& attr
->num_values
> 1 ? ",..." : "", s
? s
: "(null)");
3474 * Create an array of new reason keyword strings...
3479 int i
; /* Looping var */
3481 new_reasons
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
3484 for (i
= 0; i
< attr
->num_values
; i
++)
3486 reason
= attr
->values
[i
].string
.text
;
3488 if (strcmp(reason
, "none") &&
3489 strcmp(reason
, "none-report") &&
3490 strcmp(reason
, "paused") &&
3491 strncmp(reason
, "spool-area-full", 15) &&
3492 strcmp(reason
, "com.apple.print.recoverable-warning") &&
3493 strncmp(reason
, "cups-", 5))
3494 cupsArrayAdd(new_reasons
, reason
);
3499 if (*s
== '+' || *s
== '-')
3504 new_reasons
= _cupsArrayNewStrings(s
, ',');
3510 * Compute the changes...
3514 addprefix
= "STATE: +";
3517 remprefix
= "STATE: -";
3520 fprintf(stderr
, "DEBUG2: op='%c', new_reasons=%d, state_reasons=%d\n",
3521 op
? op
: ' ', cupsArrayCount(new_reasons
),
3522 cupsArrayCount(state_reasons
));
3524 _cupsMutexLock(&report_mutex
);
3532 for (reason
= (char *)cupsArrayFirst(new_reasons
);
3534 reason
= (char *)cupsArrayNext(new_reasons
))
3536 if (!cupsArrayFind(state_reasons
, reason
))
3538 if (!strncmp(reason
, "cups-remote-", 12))
3541 * If we are setting cups-remote-xxx, remove all other cups-remote-xxx
3545 char *temp
; /* Current reason in state_reasons */
3547 cupsArraySave(state_reasons
);
3549 for (temp
= (char *)cupsArrayFirst(state_reasons
);
3551 temp
= (char *)cupsArrayNext(state_reasons
))
3552 if (!strncmp(temp
, "cups-remote-", 12))
3554 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
3556 remptr
+= strlen(remptr
);
3559 cupsArrayRemove(state_reasons
, temp
);
3563 cupsArrayRestore(state_reasons
);
3566 cupsArrayAdd(state_reasons
, reason
);
3568 snprintf(addptr
, sizeof(add
) - (addptr
- add
), "%s%s", addprefix
,
3570 addptr
+= strlen(addptr
);
3581 for (reason
= (char *)cupsArrayFirst(new_reasons
);
3583 reason
= (char *)cupsArrayNext(new_reasons
))
3585 if (cupsArrayFind(state_reasons
, reason
))
3587 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
3589 remptr
+= strlen(remptr
);
3592 cupsArrayRemove(state_reasons
, reason
);
3599 * Replace reasons...
3602 for (reason
= (char *)cupsArrayFirst(state_reasons
);
3604 reason
= (char *)cupsArrayNext(state_reasons
))
3606 if (strncmp(reason
, "cups-", 5) && !cupsArrayFind(new_reasons
, reason
))
3608 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
3610 remptr
+= strlen(remptr
);
3613 cupsArrayRemove(state_reasons
, reason
);
3617 for (reason
= (char *)cupsArrayFirst(new_reasons
);
3619 reason
= (char *)cupsArrayNext(new_reasons
))
3621 if (!cupsArrayFind(state_reasons
, reason
))
3623 cupsArrayAdd(state_reasons
, reason
);
3625 snprintf(addptr
, sizeof(add
) - (addptr
- add
), "%s%s", addprefix
,
3627 addptr
+= strlen(addptr
);
3633 _cupsMutexUnlock(&report_mutex
);
3636 * Report changes and return...
3639 if (add
[0] && rem
[0])
3640 fprintf(stderr
, "%s\n%s\n", add
, rem
);
3642 fprintf(stderr
, "%s\n", add
);
3644 fprintf(stderr
, "%s\n", rem
);