2 * "$Id: ipp.c 9759 2011-05-11 03:24:33Z mike $"
4 * IPP backend for CUPS.
6 * Copyright 2007-2011 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 http_encryption_t encryption
; /* Use encryption? */
67 ipp_jstate_t job_state
; /* Current job state */
68 ipp_pstate_t printer_state
; /* Current printer state */
76 static const char *auth_info_required
;
77 /* New auth-info-required value */
78 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
79 static int child_pid
= 0; /* Child process ID */
80 #endif /* HAVE_GSSAPI && HAVE_XPC */
81 static const char * const jattrs
[] = /* Job attributes we want */
83 "job-impressions-completed",
84 "job-media-sheets-completed",
88 static int job_canceled
= 0;
90 static char *password
= NULL
;
91 /* Password for device URI */
92 static int password_tries
= 0;
94 static const char * const pattrs
[] = /* Printer attributes we want */
98 "document-format-supported",
100 "marker-high-levels",
106 "media-col-supported",
107 "multiple-document-handling-supported",
108 "operations-supported",
110 "printer-alert-description",
111 "printer-is-accepting-jobs",
113 "printer-state-message",
114 "printer-state-reasons"
116 static const char * const remote_job_states
[] =
117 { /* Remote job state keywords */
118 "+cups-remote-pending",
119 "+cups-remote-pending-held",
120 "+cups-remote-processing",
121 "+cups-remote-stopped",
122 "+cups-remote-canceled",
123 "+cups-remote-aborted",
124 "+cups-remote-completed"
126 static _cups_mutex_t report_mutex
= _CUPS_MUTEX_INITIALIZER
;
127 /* Mutex to control access */
128 static int num_attr_cache
= 0;
129 /* Number of cached attributes */
130 static cups_option_t
*attr_cache
= NULL
;
131 /* Cached attributes */
132 static cups_array_t
*state_reasons
; /* Array of printe-state-reasons keywords */
133 static char tmpfilename
[1024] = "";
134 /* Temporary spool file name */
141 static void cancel_job(http_t
*http
, const char *uri
, int id
,
142 const char *resource
, const char *user
,
144 static ipp_pstate_t
check_printer_state(http_t
*http
, const char *uri
,
145 const char *resource
,
146 const char *user
, int version
);
148 static void compress_files(int num_files
, char **files
);
149 #endif /* HAVE_LIBZ */
150 static void *monitor_printer(_cups_monitor_t
*monitor
);
151 static ipp_t
*new_request(ipp_op_t op
, int version
, const char *uri
,
152 const char *user
, const char *title
,
153 int num_options
, cups_option_t
*options
,
154 const char *compression
, int copies
,
155 const char *format
, _ppd_cache_t
*pc
,
156 ipp_attribute_t
*media_col_sup
,
157 ipp_attribute_t
*doc_handling_sup
);
158 static const char *password_cb(const char *);
159 static void report_attr(ipp_attribute_t
*attr
);
160 static void report_printer_state(ipp_t
*ipp
);
161 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
162 static int run_as_user(int argc
, char *argv
[], uid_t uid
,
163 const char *device_uri
, int fd
);
164 #endif /* HAVE_GSSAPI && HAVE_XPC */
165 static void sigterm_handler(int sig
);
166 static int timeout_cb(http_t
*http
, void *user_data
);
167 static void update_reasons(ipp_attribute_t
*attr
, const char *s
);
171 * 'main()' - Send a file to the printer or server.
175 * printer-uri job-id user title copies options [file]
178 int /* O - Exit status */
179 main(int argc
, /* I - Number of command-line args */
180 char *argv
[]) /* I - Command-line arguments */
182 int i
; /* Looping var */
183 int send_options
; /* Send job options? */
184 int num_options
; /* Number of printer options */
185 cups_option_t
*options
; /* Printer options */
186 const char *device_uri
; /* Device URI */
187 char scheme
[255], /* Scheme in URI */
188 hostname
[1024], /* Hostname */
189 username
[255], /* Username info */
190 resource
[1024], /* Resource info (printer name) */
191 addrname
[256], /* Address name */
192 *optptr
, /* Pointer to URI options */
193 *name
, /* Name of option */
194 *value
, /* Value of option */
195 sep
; /* Separator character */
196 http_addrlist_t
*addrlist
; /* Address of printer */
197 int snmp_fd
, /* SNMP socket */
198 start_count
, /* Page count via SNMP at start */
199 page_count
, /* Page count via SNMP */
200 have_supplies
; /* Printer supports supply levels? */
201 int num_files
; /* Number of files to print */
202 char **files
, /* Files to print */
203 *compatfile
= NULL
; /* Compatibility filename */
204 off_t compatsize
= 0; /* Size of compatibility file */
205 int port
; /* Port number (not used) */
206 char portname
[255]; /* Port name */
207 char uri
[HTTP_MAX_URI
]; /* Updated URI without user/pass */
208 http_status_t http_status
; /* Status of HTTP request */
209 ipp_status_t ipp_status
; /* Status of IPP request */
210 http_t
*http
; /* HTTP connection */
211 ipp_t
*request
, /* IPP request */
212 *response
, /* IPP response */
213 *supported
; /* get-printer-attributes response */
214 time_t start_time
; /* Time of first connect */
215 int contimeout
; /* Connection timeout */
216 int delay
, /* Delay for retries */
217 prev_delay
; /* Previous delay */
218 const char *compression
; /* Compression mode */
219 int waitjob
, /* Wait for job complete? */
220 waitprinter
; /* Wait for printer ready? */
221 _cups_monitor_t monitor
; /* Monitoring data */
222 ipp_attribute_t
*job_id_attr
; /* job-id attribute */
223 int job_id
; /* job-id value */
224 ipp_attribute_t
*job_sheets
; /* job-media-sheets-completed */
225 ipp_attribute_t
*job_state
; /* job-state */
226 ipp_attribute_t
*copies_sup
; /* copies-supported */
227 ipp_attribute_t
*cups_version
; /* cups-version */
228 ipp_attribute_t
*format_sup
; /* document-format-supported */
229 ipp_attribute_t
*media_col_sup
; /* media-col-supported */
230 ipp_attribute_t
*operations_sup
; /* operations-supported */
231 ipp_attribute_t
*doc_handling_sup
; /* multiple-document-handling-supported */
232 ipp_attribute_t
*printer_state
; /* printer-state attribute */
233 ipp_attribute_t
*printer_accepting
; /* printer-is-accepting-jobs */
234 int validate_job
; /* Does printer support Validate-Job? */
235 int copies
, /* Number of copies for job */
236 copies_remaining
; /* Number of copies remaining */
237 const char *content_type
, /* CONTENT_TYPE environment variable */
238 *final_content_type
, /* FINAL_CONTENT_TYPE environment var */
239 *document_format
; /* document-format value */
240 int fd
; /* File descriptor */
241 off_t bytes
= 0; /* Bytes copied */
242 char buffer
[16384]; /* Copy buffer */
243 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
244 struct sigaction action
; /* Actions for POSIX signals */
245 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
246 int version
; /* IPP version */
247 ppd_file_t
*ppd
; /* PPD file */
248 _ppd_cache_t
*pc
; /* PPD cache and mapping data */
249 fd_set input
; /* Input set for select() */
253 * Make sure status messages are not buffered...
256 setbuf(stderr
, NULL
);
259 * Ignore SIGPIPE and catch SIGTERM signals...
263 sigset(SIGPIPE
, SIG_IGN
);
264 sigset(SIGTERM
, sigterm_handler
);
265 #elif defined(HAVE_SIGACTION)
266 memset(&action
, 0, sizeof(action
));
267 action
.sa_handler
= SIG_IGN
;
268 sigaction(SIGPIPE
, &action
, NULL
);
270 sigemptyset(&action
.sa_mask
);
271 sigaddset(&action
.sa_mask
, SIGTERM
);
272 action
.sa_handler
= sigterm_handler
;
273 sigaction(SIGTERM
, &action
, NULL
);
275 signal(SIGPIPE
, SIG_IGN
);
276 signal(SIGTERM
, sigterm_handler
);
277 #endif /* HAVE_SIGSET */
280 * Check command-line...
287 if ((s
= strrchr(argv
[0], '/')) != NULL
)
292 printf("network %s \"Unknown\" \"%s (%s)\"\n",
293 s
, _cupsLangString(cupsLangDefault(),
294 _("Internet Printing Protocol")), s
);
295 return (CUPS_BACKEND_OK
);
299 _cupsLangPrintf(stderr
,
300 _("Usage: %s job-id user title copies options [file]"),
302 return (CUPS_BACKEND_STOP
);
306 * Get the device URI...
309 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
311 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
314 if (getenv("CLASS") != NULL
)
315 return (CUPS_BACKEND_FAILED
);
318 if ((auth_info_required
= getenv("AUTH_INFO_REQUIRED")) == NULL
)
319 auth_info_required
= "none";
321 state_reasons
= _cupsArrayNewStrings(getenv("PRINTER_STATE_REASONS"));
325 * For Kerberos, become the printing user (if we can) to get the credentials
329 if (!getuid() && (value
= getenv("AUTH_UID")) != NULL
)
331 uid_t uid
= (uid_t
)atoi(value
);
338 return (run_as_user(argc
, argv
, uid
, device_uri
, 0));
341 int status
= 0; /* Exit status */
343 for (i
= 6; i
< argc
&& !status
&& !job_canceled
; i
++)
345 if ((fd
= open(argv
[i
], O_RDONLY
)) >= 0)
347 status
= run_as_user(argc
, argv
, uid
, device_uri
, fd
);
352 _cupsLangPrintError("ERROR", _("Unable to open print file"));
353 status
= CUPS_BACKEND_FAILED
;
361 # else /* No XPC, just try to run as the user ID */
364 # endif /* HAVE_XPC */
366 #endif /* HAVE_GSSAPI */
369 * Get the (final) content type...
372 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
373 content_type
= "application/octet-stream";
375 if ((final_content_type
= getenv("FINAL_CONTENT_TYPE")) == NULL
)
377 final_content_type
= content_type
;
379 if (!strncmp(final_content_type
, "printer/", 8))
380 final_content_type
= "application/vnd.cups-raw";
384 * Extract the hostname and printer name from the URI...
387 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
388 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
389 resource
, sizeof(resource
));
392 port
= IPP_PORT
; /* Default to port 631 */
394 if (!strcmp(scheme
, "https"))
395 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
397 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
400 * See if there are any options...
407 contimeout
= 7 * 24 * 60 * 60;
409 if ((optptr
= strchr(resource
, '?')) != NULL
)
412 * Yup, terminate the device name string and move to the first
413 * character of the optptr...
419 * Then parse the optptr...
430 while (*optptr
&& *optptr
!= '=' && *optptr
!= '+' && *optptr
!= '&')
433 if ((sep
= *optptr
) != '\0')
444 while (*optptr
&& *optptr
!= '+' && *optptr
!= '&')
454 * Process the option...
457 if (!_cups_strcasecmp(name
, "waitjob"))
460 * Wait for job completion?
463 waitjob
= !_cups_strcasecmp(value
, "on") ||
464 !_cups_strcasecmp(value
, "yes") ||
465 !_cups_strcasecmp(value
, "true");
467 else if (!_cups_strcasecmp(name
, "waitprinter"))
470 * Wait for printer idle?
473 waitprinter
= !_cups_strcasecmp(value
, "on") ||
474 !_cups_strcasecmp(value
, "yes") ||
475 !_cups_strcasecmp(value
, "true");
477 else if (!_cups_strcasecmp(name
, "encryption"))
480 * Enable/disable encryption?
483 if (!_cups_strcasecmp(value
, "always"))
484 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
485 else if (!_cups_strcasecmp(value
, "required"))
486 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
487 else if (!_cups_strcasecmp(value
, "never"))
488 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
489 else if (!_cups_strcasecmp(value
, "ifrequested"))
490 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
493 _cupsLangPrintFilter(stderr
, "ERROR",
494 _("Unknown encryption option value: \"%s\"."),
498 else if (!_cups_strcasecmp(name
, "version"))
500 if (!strcmp(value
, "1.0"))
502 else if (!strcmp(value
, "1.1"))
504 else if (!strcmp(value
, "2.0"))
506 else if (!strcmp(value
, "2.1"))
508 else if (!strcmp(value
, "2.2"))
512 _cupsLangPrintFilter(stderr
, "ERROR",
513 _("Unknown version option value: \"%s\"."),
518 else if (!_cups_strcasecmp(name
, "compression"))
520 if (!_cups_strcasecmp(value
, "true") || !_cups_strcasecmp(value
, "yes") ||
521 !_cups_strcasecmp(value
, "on") || !_cups_strcasecmp(value
, "gzip"))
522 compression
= "gzip";
524 #endif /* HAVE_LIBZ */
525 else if (!_cups_strcasecmp(name
, "contimeout"))
528 * Set the connection timeout...
532 contimeout
= atoi(value
);
540 _cupsLangPrintFilter(stderr
, "ERROR",
541 _("Unknown option \"%s\" with value \"%s\"."),
548 * If we have 7 arguments, print the file named on the command-line.
549 * Otherwise, copy stdin to a temporary file and print the temporary
557 send_options
= !_cups_strcasecmp(final_content_type
, "application/pdf") ||
558 !_cups_strcasecmp(final_content_type
, "application/vnd.cups-pdf") ||
559 !_cups_strncasecmp(final_content_type
, "image/", 6);
561 fputs("DEBUG: Sending stdin for job...\n", stderr
);
566 * Point to the files on the command-line...
569 num_files
= argc
- 6;
575 compress_files(num_files
, files
);
576 #endif /* HAVE_LIBZ */
578 fprintf(stderr
, "DEBUG: %d files to send in job...\n", num_files
);
582 * Set the authentication info, if any...
585 cupsSetPasswordCB(password_cb
);
590 * Use authenticaion information in the device URI...
593 if ((password
= strchr(username
, ':')) != NULL
)
596 cupsSetUser(username
);
601 * Try loading authentication information from the environment.
604 const char *ptr
= getenv("AUTH_USERNAME");
609 password
= getenv("AUTH_PASSWORD");
613 * Try finding the remote server...
616 start_time
= time(NULL
);
618 sprintf(portname
, "%d", port
);
620 update_reasons(NULL
, "+connecting-to-device");
621 fprintf(stderr
, "DEBUG: Looking up \"%s\"...\n", hostname
);
623 while ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
625 _cupsLangPrintFilter(stderr
, "INFO",
626 _("Unable to locate printer \"%s\"."), hostname
);
629 if (getenv("CLASS") != NULL
)
631 update_reasons(NULL
, "-connecting-to-device");
632 return (CUPS_BACKEND_STOP
);
636 http
= _httpCreate(hostname
, port
, addrlist
, cupsEncryption(), AF_UNSPEC
);
637 httpSetTimeout(http
, 30.0, timeout_cb
, NULL
);
640 * See if the printer supports SNMP...
643 if ((snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
)) >= 0)
645 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
),
649 have_supplies
= start_count
= 0;
652 * Wait for data from the filter...
657 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), 0, backendNetworkSideCB
))
658 return (CUPS_BACKEND_OK
);
659 else if ((bytes
= read(0, buffer
, sizeof(buffer
))) <= 0)
660 return (CUPS_BACKEND_OK
);
664 * Try connecting to the remote server...
667 delay
= _cupsNextDelay(0, &prev_delay
);
671 fprintf(stderr
, "DEBUG: Connecting to %s:%d\n", hostname
, port
);
672 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
674 if (httpReconnect(http
))
676 int error
= errno
; /* Connection error */
678 if (http
->status
== HTTP_PKI_ERROR
)
679 update_reasons(NULL
, "+cups-certificate-error");
684 if (getenv("CLASS") != NULL
)
687 * If the CLASS environment variable is set, the job was submitted
688 * to a class and not to a specific queue. In this case, we want
689 * to abort immediately so that the job can be requeued on the next
690 * available printer in the class.
693 _cupsLangPrintFilter(stderr
, "INFO",
694 _("Unable to contact printer, queuing on next "
695 "printer in class."));
698 * Sleep 5 seconds to keep the job from requeuing too rapidly...
703 update_reasons(NULL
, "-connecting-to-device");
705 return (CUPS_BACKEND_FAILED
);
708 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
710 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
711 errno
== EHOSTUNREACH
)
713 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
715 _cupsLangPrintFilter(stderr
, "ERROR",
716 _("The printer is not responding."));
717 update_reasons(NULL
, "-connecting-to-device");
718 return (CUPS_BACKEND_FAILED
);
724 _cupsLangPrintFilter(stderr
, "WARNING",
725 _("The printer may not exist or "
726 "is unavailable at this time."));
730 _cupsLangPrintFilter(stderr
, "WARNING",
731 _("The printer is unreachable at this "
737 _cupsLangPrintFilter(stderr
, "WARNING",
738 _("The printer is busy."));
744 delay
= _cupsNextDelay(delay
, &prev_delay
);
748 _cupsLangPrintFilter(stderr
, "ERROR",
749 _("The printer is not responding."));
757 update_reasons(NULL
, "-cups-certificate-error");
759 while (http
->fd
< 0);
761 if (job_canceled
|| !http
)
762 return (CUPS_BACKEND_FAILED
);
764 update_reasons(NULL
, "-connecting-to-device");
765 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
767 fprintf(stderr
, "DEBUG: Connected to %s:%d...\n",
768 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
769 _httpAddrPort(http
->hostaddr
));
772 * Build a URI for the printer and fill the standard IPP attributes for
773 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
774 * might contain username:password information...
777 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), scheme
, NULL
, hostname
,
781 * First validate the destination and see if the device supports multiple
788 media_col_sup
= NULL
;
790 operations_sup
= NULL
;
791 doc_handling_sup
= NULL
;
797 * Check for side-channel requests...
800 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
803 * Build the IPP request...
806 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
807 request
->request
.op
.version
[0] = version
/ 10;
808 request
->request
.op
.version
[1] = version
% 10;
810 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
813 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
814 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
821 fputs("DEBUG: Getting supported attributes...\n", stderr
);
823 if (http
->version
< HTTP_1_1
)
825 fprintf(stderr
, "DEBUG: Printer responded with HTTP version %d.%d.\n",
826 http
->version
/ 100, http
->version
% 100);
827 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
828 "cups-ipp-wrong-http-version");
831 supported
= cupsDoRequest(http
, request
, resource
);
832 ipp_status
= cupsLastError();
834 fprintf(stderr
, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
835 ippErrorString(ipp_status
), cupsLastErrorString());
837 if (ipp_status
> IPP_OK_CONFLICT
)
839 fprintf(stderr
, "DEBUG: Get-Printer-Attributes returned %s.\n",
840 ippErrorString(ipp_status
));
842 if (ipp_status
== IPP_PRINTER_BUSY
||
843 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
845 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
847 _cupsLangPrintFilter(stderr
, "ERROR",
848 _("The printer is not responding."));
849 return (CUPS_BACKEND_FAILED
);
852 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is busy."));
854 report_printer_state(supported
);
858 delay
= _cupsNextDelay(delay
, &prev_delay
);
860 else if ((ipp_status
== IPP_BAD_REQUEST
||
861 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
> 10)
864 * Switch to IPP/1.1 or IPP/1.0...
869 _cupsLangPrintFilter(stderr
, "INFO",
870 _("Printer does not support IPP/%d.%d, trying "
871 "IPP/%s."), version
/ 10, version
% 10, "1.1");
876 _cupsLangPrintFilter(stderr
, "INFO",
877 _("Printer does not support IPP/%d.%d, trying "
878 "IPP/%s."), version
/ 10, version
% 10, "1.0");
884 else if (ipp_status
== IPP_NOT_FOUND
)
886 _cupsLangPrintFilter(stderr
, "ERROR",
887 _("The printer URI is incorrect or no longer "
890 ippDelete(supported
);
892 return (CUPS_BACKEND_STOP
);
894 else if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
)
896 if (!strncmp(httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
),
898 auth_info_required
= "negotiate";
900 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
901 return (CUPS_BACKEND_AUTH_REQUIRED
);
905 _cupsLangPrintFilter(stderr
, "ERROR",
906 _("Unable to get printer status."));
910 ippDelete(supported
);
915 if (!getenv("CLASS"))
918 * Check printer-is-accepting-jobs = false and printer-state-reasons for the
919 * "spool-area-full" keyword...
924 if ((printer_accepting
= ippFindAttribute(supported
,
925 "printer-is-accepting-jobs",
926 IPP_TAG_BOOLEAN
)) != NULL
&&
927 !printer_accepting
->values
[0].boolean
)
929 else if (!printer_accepting
)
930 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
931 "cups-ipp-missing-printer-is-accepting-jobs");
933 if ((printer_state
= ippFindAttribute(supported
,
934 "printer-state-reasons",
935 IPP_TAG_KEYWORD
)) != NULL
&& !busy
)
937 for (i
= 0; i
< printer_state
->num_values
; i
++)
938 if (!strcmp(printer_state
->values
[0].string
.text
,
939 "spool-area-full") ||
940 !strncmp(printer_state
->values
[0].string
.text
, "spool-area-full-",
948 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
949 "cups-ipp-missing-printer-state-reasons");
953 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is busy."));
955 report_printer_state(supported
);
959 delay
= _cupsNextDelay(delay
, &prev_delay
);
961 ippDelete(supported
);
968 * Check for supported attributes...
971 if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
972 IPP_TAG_RANGE
)) != NULL
)
975 * Has the "copies-supported" attribute - does it have an upper
979 fprintf(stderr
, "DEBUG: copies-supported=%d-%d\n",
980 copies_sup
->values
[0].range
.lower
,
981 copies_sup
->values
[0].range
.upper
);
983 if (copies_sup
->values
[0].range
.upper
<= 1)
984 copies_sup
= NULL
; /* No */
987 cups_version
= ippFindAttribute(supported
, "cups-version", IPP_TAG_TEXT
);
989 if ((format_sup
= ippFindAttribute(supported
, "document-format-supported",
990 IPP_TAG_MIMETYPE
)) != NULL
)
992 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
993 format_sup
->num_values
);
994 for (i
= 0; i
< format_sup
->num_values
; i
++)
995 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
996 format_sup
->values
[i
].string
.text
);
999 if ((media_col_sup
= ippFindAttribute(supported
, "media-col-supported",
1000 IPP_TAG_KEYWORD
)) != NULL
)
1002 fprintf(stderr
, "DEBUG: media-col-supported (%d values)\n",
1003 media_col_sup
->num_values
);
1004 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
1005 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
1006 media_col_sup
->values
[i
].string
.text
);
1009 if ((operations_sup
= ippFindAttribute(supported
, "operations-supported",
1010 IPP_TAG_ENUM
)) != NULL
)
1012 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1013 if (operations_sup
->values
[i
].integer
== IPP_PRINT_JOB
)
1016 if (i
>= operations_sup
->num_values
)
1017 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1018 "cups-ipp-missing-print-job");
1020 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1021 if (operations_sup
->values
[i
].integer
== IPP_CANCEL_JOB
)
1024 if (i
>= operations_sup
->num_values
)
1025 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1026 "cups-ipp-missing-cancel-job");
1028 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1029 if (operations_sup
->values
[i
].integer
== IPP_GET_JOB_ATTRIBUTES
)
1032 if (i
>= operations_sup
->num_values
)
1033 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1034 "cups-ipp-missing-get-job-attributes");
1036 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1037 if (operations_sup
->values
[i
].integer
== IPP_GET_PRINTER_ATTRIBUTES
)
1040 if (i
>= operations_sup
->num_values
)
1041 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1042 "cups-ipp-missing-get-printer-attributes");
1044 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1045 if (operations_sup
->values
[i
].integer
== IPP_VALIDATE_JOB
)
1052 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1053 "cups-ipp-missing-validate-job");
1056 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1057 "cups-ipp-missing-operations-supported");
1059 doc_handling_sup
= ippFindAttribute(supported
,
1060 "multiple-document-handling-supported",
1063 report_printer_state(supported
);
1065 while (ipp_status
> IPP_OK_CONFLICT
);
1068 * See if the printer is accepting jobs and is not stopped; if either
1069 * condition is true and we are printing to a class, requeue the job...
1072 if (getenv("CLASS") != NULL
)
1074 printer_state
= ippFindAttribute(supported
, "printer-state",
1076 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
1079 if (printer_state
== NULL
||
1080 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
1082 printer_accepting
== NULL
||
1083 !printer_accepting
->values
[0].boolean
)
1086 * If the CLASS environment variable is set, the job was submitted
1087 * to a class and not to a specific queue. In this case, we want
1088 * to abort immediately so that the job can be requeued on the next
1089 * available printer in the class.
1092 _cupsLangPrintFilter(stderr
, "INFO",
1093 _("Unable to contact printer, queuing on next "
1094 "printer in class."));
1096 ippDelete(supported
);
1100 * Sleep 5 seconds to keep the job from requeuing too rapidly...
1105 return (CUPS_BACKEND_FAILED
);
1110 * See if the printer supports multiple copies...
1113 copies
= atoi(argv
[4]);
1115 if (copies_sup
|| argc
< 7)
1117 copies_remaining
= 1;
1119 if (argc
< 7 && !send_options
)
1123 copies_remaining
= copies
;
1126 * Prepare remaining printing options...
1134 num_options
= cupsParseOptions(argv
[5], 0, &options
);
1136 if (!cups_version
&& media_col_sup
)
1139 * Load the PPD file and generate PWG attribute mapping information...
1142 ppd
= ppdOpenFile(getenv("PPD"));
1143 pc
= _ppdCacheCreateWithPPD(ppd
);
1151 document_format
= NULL
;
1153 if (format_sup
!= NULL
)
1155 for (i
= 0; i
< format_sup
->num_values
; i
++)
1156 if (!_cups_strcasecmp(final_content_type
, format_sup
->values
[i
].string
.text
))
1158 document_format
= final_content_type
;
1162 if (!document_format
)
1164 for (i
= 0; i
< format_sup
->num_values
; i
++)
1165 if (!_cups_strcasecmp("application/octet-stream",
1166 format_sup
->values
[i
].string
.text
))
1168 document_format
= "application/octet-stream";
1175 * If the printer does not support HTTP/1.1 (which IPP requires), copy stdin
1176 * to a temporary file so that we can do a HTTP/1.0 submission...
1178 * (I hate compatibility hacks!)
1181 if (http
->version
< HTTP_1_1
&& num_files
== 0)
1183 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
1185 perror("DEBUG: Unable to create temporary file");
1186 return (CUPS_BACKEND_FAILED
);
1189 _cupsLangPrintFilter(stderr
, "INFO", _("Copying print data."));
1191 compatsize
= backendRunLoop(-1, fd
, snmp_fd
, &(addrlist
->addr
), 0, 0,
1192 backendNetworkSideCB
);
1196 compatfile
= tmpfilename
;
1197 files
= &compatfile
;
1200 else if (http
->version
< HTTP_1_1
&& num_files
== 1)
1202 struct stat fileinfo
; /* File information */
1204 if (!stat(files
[0], &fileinfo
))
1205 compatsize
= fileinfo
.st_size
;
1209 * Start monitoring the printer in the background...
1213 monitor
.hostname
= hostname
;
1214 monitor
.user
= argv
[2];
1215 monitor
.resource
= resource
;
1216 monitor
.port
= port
;
1217 monitor
.version
= version
;
1219 monitor
.encryption
= cupsEncryption();
1220 monitor
.job_state
= IPP_JOB_PENDING
;
1221 monitor
.printer_state
= IPP_PRINTER_IDLE
;
1223 _cupsThreadCreate((_cups_thread_func_t
)monitor_printer
, &monitor
);
1226 * Validate access to the printer...
1229 while (!job_canceled
&& validate_job
)
1231 request
= new_request(IPP_VALIDATE_JOB
, version
, uri
, argv
[2], argv
[3],
1232 num_options
, options
, compression
,
1233 copies_sup
? copies
: 1, document_format
, pc
,
1234 media_col_sup
, doc_handling_sup
);
1236 ippDelete(cupsDoRequest(http
, request
, resource
));
1238 ipp_status
= cupsLastError();
1240 fprintf(stderr
, "DEBUG: Validate-Job: %s (%s)\n",
1241 ippErrorString(ipp_status
), cupsLastErrorString());
1246 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
|| ipp_status
== IPP_PRINTER_BUSY
)
1248 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is busy."));
1251 else if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
1252 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1255 * Update auth-info-required as needed...
1258 fprintf(stderr
, "DEBUG: WWW-Authenticate=\"%s\"\n",
1259 httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
));
1262 * Normal authentication goes through the password callback, which sets
1263 * auth_info_required to "username,password". Kerberos goes directly
1264 * through GSSAPI, so look for Negotiate in the WWW-Authenticate header
1265 * here and set auth_info_required as needed...
1268 if (!strncmp(httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
),
1270 auth_info_required
= "negotiate";
1274 else if (ipp_status
== IPP_OPERATION_NOT_SUPPORTED
)
1277 * This is all too common...
1280 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1281 "cups-ipp-missing-validate-job");
1284 else if (ipp_status
< IPP_REDIRECTION_OTHER_SITE
)
1289 * Then issue the print-job request...
1294 while (!job_canceled
&& copies_remaining
> 0)
1297 * Check for side-channel requests...
1300 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1303 * Build the IPP job creation request...
1309 request
= new_request(num_files
> 1 ? IPP_CREATE_JOB
: IPP_PRINT_JOB
,
1310 version
, uri
, argv
[2], argv
[3], num_options
, options
,
1311 compression
, copies_sup
? copies
: 1, document_format
,
1312 pc
, media_col_sup
, doc_handling_sup
);
1319 response
= cupsDoRequest(http
, request
, resource
);
1322 size_t length
= 0; /* Length of request */
1326 fputs("DEBUG: Sending file using HTTP/1.0 Content-Length...\n", stderr
);
1327 length
= ippLength(request
) + (size_t)compatsize
;
1330 fputs("DEBUG: Sending file using HTTP/1.1 chunking...\n", stderr
);
1332 http_status
= cupsSendRequest(http
, request
, resource
, length
);
1333 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
)
1336 fd
= open(files
[0], O_RDONLY
);
1340 http_status
= cupsWriteRequestData(http
, buffer
, bytes
);
1343 while (http_status
== HTTP_CONTINUE
&&
1344 (!job_canceled
|| compatsize
> 0))
1347 * Check for side-channel requests and more print data...
1352 FD_SET(snmp_fd
, &input
);
1354 while (select(fd
> snmp_fd
? fd
+ 1 : snmp_fd
+ 1, &input
, NULL
, NULL
,
1355 NULL
) <= 0 && !job_canceled
);
1357 if (FD_ISSET(snmp_fd
, &input
))
1358 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1360 if (FD_ISSET(fd
, &input
))
1362 if ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1364 fprintf(stderr
, "DEBUG: Read %d bytes...\n", (int)bytes
);
1366 if (cupsWriteRequestData(http
, buffer
, bytes
) != HTTP_CONTINUE
)
1369 else if (bytes
== 0 || (errno
!= EINTR
&& errno
!= EAGAIN
))
1378 response
= cupsGetResponse(http
, resource
);
1382 ipp_status
= cupsLastError();
1384 fprintf(stderr
, "DEBUG: %s: %s (%s)\n",
1385 num_files
> 1 ? "Create-Job" : "Print-Job",
1386 ippErrorString(ipp_status
), cupsLastErrorString());
1388 if (ipp_status
> IPP_OK_CONFLICT
)
1395 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1396 ipp_status
== IPP_PRINTER_BUSY
)
1398 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is busy."));
1404 * We can't re-submit when we have no files to print, so exit
1405 * immediately with the right status code...
1411 else if (ipp_status
== IPP_ERROR_JOB_CANCELED
)
1416 * Update auth-info-required as needed...
1419 _cupsLangPrintFilter(stderr
, "ERROR",
1420 _("Print file was not accepted."));
1422 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
)
1424 fprintf(stderr
, "DEBUG: WWW-Authenticate=\"%s\"\n",
1425 httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
));
1428 * Normal authentication goes through the password callback, which sets
1429 * auth_info_required to "username,password". Kerberos goes directly
1430 * through GSSAPI, so look for Negotiate in the WWW-Authenticate header
1431 * here and set auth_info_required as needed...
1434 if (!strncmp(httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
),
1436 auth_info_required
= "negotiate";
1444 * We can't re-submit when we have no files to print, so exit
1445 * immediately with the right status code...
1452 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
1453 IPP_TAG_INTEGER
)) == NULL
)
1455 _cupsLangPrintFilter(stderr
, "INFO",
1456 _("Print file accepted - job ID unknown."));
1457 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1458 "cups-ipp-missing-job-id");
1463 monitor
.job_id
= job_id
= job_id_attr
->values
[0].integer
;
1464 _cupsLangPrintFilter(stderr
, "INFO",
1465 _("Print file accepted - job ID %d."), job_id
);
1468 ippDelete(response
);
1473 if (job_id
&& num_files
> 1)
1475 for (i
= 0; i
< num_files
; i
++)
1478 * Check for side-channel requests...
1481 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1484 * Send the next file in the job...
1487 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
1488 request
->request
.op
.version
[0] = version
/ 10;
1489 request
->request
.op
.version
[1] = version
% 10;
1491 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1494 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1498 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1499 "requesting-user-name", NULL
, argv
[2]);
1501 if ((i
+ 1) == num_files
)
1502 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
1504 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1505 "document-format", NULL
, content_type
);
1507 fprintf(stderr
, "DEBUG: Sending file %d using chunking...\n", i
+ 1);
1508 http_status
= cupsSendRequest(http
, request
, resource
, 0);
1509 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
&&
1510 (fd
= open(files
[i
], O_RDONLY
)) >= 0)
1512 while (!job_canceled
&&
1513 (bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1515 if (cupsWriteRequestData(http
, buffer
, bytes
) != HTTP_CONTINUE
)
1520 * Check for side-channel requests...
1523 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1530 ippDelete(cupsGetResponse(http
, resource
));
1533 fprintf(stderr
, "DEBUG: Send-Document: %s (%s)\n",
1534 ippErrorString(cupsLastError()), cupsLastErrorString());
1536 if (cupsLastError() > IPP_OK_CONFLICT
)
1538 ipp_status
= cupsLastError();
1540 _cupsLangPrintFilter(stderr
, "ERROR",
1541 _("Unable to add document to print job."));
1547 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
1549 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
1550 copies_remaining
--;
1552 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1553 ipp_status
== IPP_PRINTER_BUSY
)
1556 copies_remaining
--;
1559 * Wait for the job to complete...
1562 if (!job_id
|| !waitjob
)
1565 _cupsLangPrintFilter(stderr
, "INFO", _("Waiting for job to complete."));
1567 for (delay
= _cupsNextDelay(0, &prev_delay
); !job_canceled
;)
1570 * Check for side-channel requests...
1573 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1576 * Build an IPP_GET_JOB_ATTRIBUTES request...
1579 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1580 request
->request
.op
.version
[0] = version
/ 10;
1581 request
->request
.op
.version
[1] = version
% 10;
1583 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1586 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1590 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1591 "requesting-user-name", NULL
, argv
[2]);
1593 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1594 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
1601 httpReconnect(http
);
1602 response
= cupsDoRequest(http
, request
, resource
);
1603 ipp_status
= cupsLastError();
1605 if (ipp_status
== IPP_NOT_FOUND
)
1608 * Job has gone away and/or the server has no job history...
1611 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1612 "cups-ipp-missing-job-history");
1613 ippDelete(response
);
1615 ipp_status
= IPP_OK
;
1619 fprintf(stderr
, "DEBUG: Get-Job-Attributes: %s (%s)\n",
1620 ippErrorString(ipp_status
), cupsLastErrorString());
1622 if (ipp_status
> IPP_OK_CONFLICT
)
1624 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1625 ipp_status
!= IPP_PRINTER_BUSY
)
1627 ippDelete(response
);
1629 _cupsLangPrintFilter(stderr
, "ERROR",
1630 _("Unable to get print job status."));
1637 if ((job_state
= ippFindAttribute(response
, "job-state",
1638 IPP_TAG_ENUM
)) != NULL
)
1641 * Reflect the remote job state in the local queue...
1645 job_state
->values
[0].integer
>= IPP_JOB_PENDING
&&
1646 job_state
->values
[0].integer
<= IPP_JOB_COMPLETED
)
1647 update_reasons(NULL
,
1648 remote_job_states
[job_state
->values
[0].integer
-
1651 if ((job_sheets
= ippFindAttribute(response
,
1652 "job-media-sheets-completed",
1653 IPP_TAG_INTEGER
)) == NULL
)
1654 job_sheets
= ippFindAttribute(response
,
1655 "job-impressions-completed",
1659 fprintf(stderr
, "PAGE: total %d\n",
1660 job_sheets
->values
[0].integer
);
1663 * Stop polling if the job is finished or pending-held...
1666 if (job_state
->values
[0].integer
> IPP_JOB_STOPPED
)
1668 ippDelete(response
);
1675 * If the printer does not return a job-state attribute, it does not
1676 * conform to the IPP specification - break out immediately and fail
1680 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1681 "cups-ipp-missing-job-state");
1682 ipp_status
= IPP_INTERNAL_ERROR
;
1687 ippDelete(response
);
1690 * Wait before polling again...
1695 delay
= _cupsNextDelay(delay
, &prev_delay
);
1700 * Cancel the job as needed...
1703 if (job_canceled
&& job_id
)
1704 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1707 * Check the printer state and report it if necessary...
1710 check_printer_state(http
, uri
, resource
, argv
[2], version
);
1713 * Collect the final page count as needed...
1716 if (have_supplies
&&
1717 !backendSNMPSupplies(snmp_fd
, http
->hostaddr
, &page_count
, NULL
) &&
1718 page_count
> start_count
)
1719 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
1723 * See if we used Kerberos at all...
1727 auth_info_required
= "negotiate";
1728 #endif /* HAVE_GSSAPI */
1736 cupsFreeOptions(num_options
, options
);
1737 _ppdCacheDestroy(pc
);
1741 ippDelete(supported
);
1744 * Remove the temporary file(s) if necessary...
1748 unlink(tmpfilename
);
1753 for (i
= 0; i
< num_files
; i
++)
1756 #endif /* HAVE_LIBZ */
1759 * Return the queue status...
1762 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
1763 ipp_status
== IPP_AUTHENTICATION_CANCELED
||
1764 ipp_status
<= IPP_OK_CONFLICT
)
1765 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
1767 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
1768 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1769 return (CUPS_BACKEND_AUTH_REQUIRED
);
1770 else if (ipp_status
== IPP_INTERNAL_ERROR
)
1771 return (CUPS_BACKEND_STOP
);
1772 else if (ipp_status
== IPP_DOCUMENT_FORMAT
||
1773 ipp_status
== IPP_CONFLICT
)
1774 return (CUPS_BACKEND_FAILED
);
1775 else if (ipp_status
> IPP_OK_CONFLICT
&& ipp_status
!= IPP_ERROR_JOB_CANCELED
)
1776 return (CUPS_BACKEND_RETRY_CURRENT
);
1779 _cupsLangPrintFilter(stderr
, "INFO", _("Ready to print."));
1780 return (CUPS_BACKEND_OK
);
1786 * 'cancel_job()' - Cancel a print job.
1790 cancel_job(http_t
*http
, /* I - HTTP connection */
1791 const char *uri
, /* I - printer-uri */
1792 int id
, /* I - job-id */
1793 const char *resource
, /* I - Resource path */
1794 const char *user
, /* I - requesting-user-name */
1795 int version
) /* I - IPP version */
1797 ipp_t
*request
; /* Cancel-Job request */
1800 _cupsLangPrintFilter(stderr
, "INFO", _("Canceling print job."));
1802 request
= ippNewRequest(IPP_CANCEL_JOB
);
1803 request
->request
.op
.version
[0] = version
/ 10;
1804 request
->request
.op
.version
[1] = version
% 10;
1806 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1808 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1810 if (user
&& user
[0])
1811 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1812 "requesting-user-name", NULL
, user
);
1818 ippDelete(cupsDoRequest(http
, request
, resource
));
1820 if (cupsLastError() > IPP_OK_CONFLICT
)
1821 _cupsLangPrintFilter(stderr
, "ERROR", _("Unable to cancel print job."));
1826 * 'check_printer_state()' - Check the printer state.
1829 static ipp_pstate_t
/* O - Current printer-state */
1830 check_printer_state(
1831 http_t
*http
, /* I - HTTP connection */
1832 const char *uri
, /* I - Printer URI */
1833 const char *resource
, /* I - Resource path */
1834 const char *user
, /* I - Username, if any */
1835 int version
) /* I - IPP version */
1837 ipp_t
*request
, /* IPP request */
1838 *response
; /* IPP response */
1839 ipp_attribute_t
*attr
; /* Attribute in response */
1840 ipp_pstate_t printer_state
= IPP_PRINTER_STOPPED
;
1841 /* Current printer-state */
1845 * Send a Get-Printer-Attributes request and log the results...
1848 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1849 request
->request
.op
.version
[0] = version
/ 10;
1850 request
->request
.op
.version
[1] = version
% 10;
1852 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1855 if (user
&& user
[0])
1856 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1857 "requesting-user-name", NULL
, user
);
1859 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1860 "requested-attributes",
1861 (int)(sizeof(pattrs
) / sizeof(pattrs
[0])), NULL
, pattrs
);
1863 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
1865 report_printer_state(response
);
1867 if ((attr
= ippFindAttribute(response
, "printer-state",
1868 IPP_TAG_ENUM
)) != NULL
)
1869 printer_state
= (ipp_pstate_t
)attr
->values
[0].integer
;
1871 ippDelete(response
);
1874 fprintf(stderr
, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
1875 ippErrorString(cupsLastError()), cupsLastErrorString());
1878 * Return the printer-state value...
1881 return (printer_state
);
1887 * 'compress_files()' - Compress print files.
1891 compress_files(int num_files
, /* I - Number of files */
1892 char **files
) /* I - Files */
1894 int i
, /* Looping var */
1895 fd
; /* Temporary file descriptor */
1896 ssize_t bytes
; /* Bytes read/written */
1897 size_t total
; /* Total bytes read */
1898 cups_file_t
*in
, /* Input file */
1899 *out
; /* Output file */
1900 struct stat outinfo
; /* Output file information */
1901 char filename
[1024], /* Temporary filename */
1902 buffer
[32768]; /* Copy buffer */
1905 fprintf(stderr
, "DEBUG: Compressing %d job files...\n", num_files
);
1906 for (i
= 0; i
< num_files
; i
++)
1908 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
1910 _cupsLangPrintError("ERROR", _("Unable to create compressed print file"));
1911 exit(CUPS_BACKEND_FAILED
);
1914 if ((out
= cupsFileOpenFd(fd
, "w9")) == NULL
)
1916 _cupsLangPrintError("ERROR", _("Unable to open compressed print file"));
1917 exit(CUPS_BACKEND_FAILED
);
1920 if ((in
= cupsFileOpen(files
[i
], "r")) == NULL
)
1922 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1924 exit(CUPS_BACKEND_FAILED
);
1928 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
1929 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
1931 _cupsLangPrintError("ERROR",
1932 _("Unable to generate compressed print file"));
1935 exit(CUPS_BACKEND_FAILED
);
1943 files
[i
] = strdup(filename
);
1945 if (!stat(filename
, &outinfo
))
1947 "DEBUG: File %d compressed to %.1f%% of original size, "
1948 CUPS_LLFMT
" bytes...\n",
1949 i
+ 1, 100.0 * outinfo
.st_size
/ total
,
1950 CUPS_LLCAST outinfo
.st_size
);
1953 #endif /* HAVE_LIBZ */
1957 * 'monitor_printer()' - Monitor the printer state.
1960 static void * /* O - Thread exit code */
1962 _cups_monitor_t
*monitor
) /* I - Monitoring data */
1964 http_t
*http
; /* Connection to printer */
1965 ipp_t
*request
, /* IPP request */
1966 *response
; /* IPP response */
1967 ipp_attribute_t
*attr
; /* Attribute in response */
1968 int delay
, /* Current delay */
1969 prev_delay
; /* Previous delay */
1973 * Make a copy of the printer connection...
1976 http
= _httpCreate(monitor
->hostname
, monitor
->port
, NULL
, monitor
->encryption
,
1978 httpSetTimeout(http
, 30.0, timeout_cb
, NULL
);
1979 cupsSetPasswordCB(password_cb
);
1982 * Loop until the job is canceled, aborted, or completed.
1985 delay
= _cupsNextDelay(0, &prev_delay
);
1987 while (monitor
->job_state
< IPP_JOB_CANCELED
&& !job_canceled
)
1990 * Reconnect to the printer...
1993 if (!httpReconnect(http
))
1996 * Connected, so check on the printer state...
1999 monitor
->printer_state
= check_printer_state(http
, monitor
->uri
,
2004 if (monitor
->job_id
> 0)
2007 * Check the status of the job itself...
2010 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
2011 request
->request
.op
.version
[0] = monitor
->version
/ 10;
2012 request
->request
.op
.version
[1] = monitor
->version
% 10;
2014 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2015 NULL
, monitor
->uri
);
2016 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
2019 if (monitor
->user
&& monitor
->user
[0])
2020 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2021 "requesting-user-name", NULL
, monitor
->user
);
2023 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2024 "requested-attributes",
2025 (int)(sizeof(jattrs
) / sizeof(jattrs
[0])), NULL
, jattrs
);
2031 response
= cupsDoRequest(http
, request
, monitor
->resource
);
2033 fprintf(stderr
, "DEBUG: Get-Job-Attributes: %s (%s)\n",
2034 ippErrorString(cupsLastError()), cupsLastErrorString());
2036 if ((attr
= ippFindAttribute(response
, "job-state",
2037 IPP_TAG_ENUM
)) != NULL
)
2038 monitor
->job_state
= (ipp_jstate_t
)attr
->values
[0].integer
;
2040 monitor
->job_state
= IPP_JOB_COMPLETED
;
2042 ippDelete(response
);
2046 * Disconnect from the printer - we'll reconnect on the next poll...
2049 _httpDisconnect(http
);
2053 * Sleep for N seconds...
2058 delay
= _cupsNextDelay(delay
, &prev_delay
);
2062 * Cleanup and return...
2072 * 'new_request()' - Create a new print creation or validation request.
2075 static ipp_t
* /* O - Request data */
2077 ipp_op_t op
, /* I - IPP operation code */
2078 int version
, /* I - IPP version number */
2079 const char *uri
, /* I - printer-uri value */
2080 const char *user
, /* I - requesting-user-name value */
2081 const char *title
, /* I - job-name value */
2082 int num_options
, /* I - Number of options to send */
2083 cups_option_t
*options
, /* I - Options to send */
2084 const char *compression
, /* I - compression value or NULL */
2085 int copies
, /* I - copies value or 0 */
2086 const char *format
, /* I - documet-format value or NULL */
2087 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2088 ipp_attribute_t
*media_col_sup
, /* I - media-col-supported values */
2089 ipp_attribute_t
*doc_handling_sup
) /* I - multiple-document-handling-supported values */
2091 int i
; /* Looping var */
2092 ipp_t
*request
; /* Request data */
2093 const char *keyword
; /* PWG keyword */
2094 _pwg_size_t
*size
; /* PWG media size */
2095 ipp_t
*media_col
, /* media-col value */
2096 *media_size
; /* media-size value */
2097 const char *media_source
, /* media-source value */
2098 *media_type
, /* media-type value */
2099 *collate_str
; /* multiple-document-handling value */
2103 * Create the IPP request...
2106 request
= ippNewRequest(op
);
2107 request
->request
.op
.version
[0] = version
/ 10;
2108 request
->request
.op
.version
[1] = version
% 10;
2110 fprintf(stderr
, "DEBUG: %s IPP/%d.%d\n",
2111 ippOpString(request
->request
.op
.operation_id
),
2112 request
->request
.op
.version
[0],
2113 request
->request
.op
.version
[1]);
2116 * Add standard attributes...
2119 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2121 fprintf(stderr
, "DEBUG: printer-uri=\"%s\"\n", uri
);
2125 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2126 "requesting-user-name", NULL
, user
);
2127 fprintf(stderr
, "DEBUG: requesting-user-name=\"%s\"\n", user
);
2130 if (title
&& *title
)
2132 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
2134 fprintf(stderr
, "DEBUG: job-name=\"%s\"\n", title
);
2139 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
2140 "document-format", NULL
, format
);
2141 fprintf(stderr
, "DEBUG: document-format=\"%s\"\n", format
);
2147 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2148 "compression", NULL
, compression
);
2149 fprintf(stderr
, "DEBUG: compression=\"%s\"\n", compression
);
2151 #endif /* HAVE_LIBZ */
2154 * Handle options on the command-line...
2157 if (num_options
> 0)
2162 * Send standard IPP attributes...
2165 if ((keyword
= cupsGetOption("PageSize", num_options
, options
)) == NULL
)
2166 keyword
= cupsGetOption("media", num_options
, options
);
2168 if ((size
= _ppdCacheGetSize(pc
, keyword
)) != NULL
)
2171 * Add a media-col value...
2174 media_size
= ippNew();
2175 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2176 "x-dimension", size
->width
);
2177 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2178 "y-dimension", size
->length
);
2180 media_col
= ippNew();
2181 ippAddCollection(media_col
, IPP_TAG_ZERO
, "media-size", media_size
);
2183 media_source
= _ppdCacheGetSource(pc
, cupsGetOption("InputSlot",
2186 media_type
= _ppdCacheGetType(pc
, cupsGetOption("MediaType",
2190 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
2192 if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2193 "media-left-margin"))
2194 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2195 "media-left-margin", size
->left
);
2196 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2197 "media-bottom-margin"))
2198 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2199 "media-bottom-margin", size
->bottom
);
2200 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2201 "media-right-margin"))
2202 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2203 "media-right-margin", size
->right
);
2204 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2205 "media-top-margin"))
2206 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2207 "media-top-margin", size
->top
);
2208 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2209 "media-source") && media_source
)
2210 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
2211 "media-source", NULL
, media_source
);
2212 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2213 "media-type") && media_type
)
2214 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
2215 "media-type", NULL
, media_type
);
2218 ippAddCollection(request
, IPP_TAG_JOB
, "media-col", media_col
);
2221 if ((keyword
= cupsGetOption("output-bin", num_options
,
2223 keyword
= _ppdCacheGetBin(pc
, cupsGetOption("OutputBin", num_options
,
2227 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-bin",
2230 if ((keyword
= cupsGetOption("output-mode", num_options
,
2232 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
2234 else if ((keyword
= cupsGetOption("ColorModel", num_options
,
2237 if (!_cups_strcasecmp(keyword
, "Gray"))
2238 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
2239 NULL
, "monochrome");
2241 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
2245 if ((keyword
= cupsGetOption("print-quality", num_options
,
2247 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2249 else if ((keyword
= cupsGetOption("cupsPrintQuality", num_options
,
2252 if (!_cups_strcasecmp(keyword
, "draft"))
2253 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2255 else if (!_cups_strcasecmp(keyword
, "normal"))
2256 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2257 IPP_QUALITY_NORMAL
);
2258 else if (!_cups_strcasecmp(keyword
, "high"))
2259 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2263 if ((keyword
= cupsGetOption("sides", num_options
, options
)) != NULL
)
2264 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2266 else if (pc
->sides_option
&&
2267 (keyword
= cupsGetOption(pc
->sides_option
, num_options
,
2270 if (!_cups_strcasecmp(keyword
, pc
->sides_1sided
))
2271 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2273 else if (!_cups_strcasecmp(keyword
, pc
->sides_2sided_long
))
2274 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2275 NULL
, "two-sided-long-edge");
2276 if (!_cups_strcasecmp(keyword
, pc
->sides_2sided_short
))
2277 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2278 NULL
, "two-sided-short-edge");
2281 if (doc_handling_sup
&&
2282 (keyword
= cupsGetOption("collate", num_options
, options
)) != NULL
)
2284 if (!_cups_strcasecmp(keyword
, "true"))
2285 collate_str
= "separate-documents-collated-copies";
2287 collate_str
= "separate-documents-uncollated-copies";
2289 for (i
= 0; i
< doc_handling_sup
->num_values
; i
++)
2290 if (!strcmp(doc_handling_sup
->values
[i
].string
.text
, collate_str
))
2292 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2293 "multiple-document-handling", NULL
, collate_str
);
2301 * When talking to another CUPS server, send all options...
2304 cupsEncodeOptions(request
, num_options
, options
);
2308 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies", copies
);
2316 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
2319 static const char * /* O - Password */
2320 password_cb(const char *prompt
) /* I - Prompt (not used) */
2325 * Remember that we need to authenticate...
2328 auth_info_required
= "username,password";
2330 if (password
&& *password
&& password_tries
< 3)
2339 * Give up after 3 tries or if we don't have a password to begin with...
2348 * 'report_attr()' - Report an IPP attribute value.
2352 report_attr(ipp_attribute_t
*attr
) /* I - Attribute */
2354 int i
; /* Looping var */
2355 char value
[1024], /* Value string */
2356 *valptr
, /* Pointer into value string */
2357 *attrptr
; /* Pointer into attribute value */
2358 const char *cached
; /* Cached attribute */
2362 * Convert the attribute values into quoted strings...
2365 for (i
= 0, valptr
= value
;
2366 i
< attr
->num_values
&& valptr
< (value
+ sizeof(value
) - 10);
2372 switch (attr
->value_tag
)
2374 case IPP_TAG_INTEGER
:
2376 snprintf(valptr
, sizeof(value
) - (valptr
- value
), "%d",
2377 attr
->values
[i
].integer
);
2378 valptr
+= strlen(valptr
);
2383 case IPP_TAG_KEYWORD
:
2385 for (attrptr
= attr
->values
[i
].string
.text
;
2386 *attrptr
&& valptr
< (value
+ sizeof(value
) - 10);
2389 if (*attrptr
== '\\' || *attrptr
== '\"')
2392 *valptr
++ = *attrptr
;
2399 * Unsupported value type...
2408 _cupsMutexLock(&report_mutex
);
2410 if ((cached
= cupsGetOption(attr
->name
, num_attr_cache
,
2411 attr_cache
)) == NULL
|| strcmp(cached
, value
))
2414 * Tell the scheduler about the new values...
2417 num_attr_cache
= cupsAddOption(attr
->name
, value
, num_attr_cache
,
2419 fprintf(stderr
, "ATTR: %s=%s\n", attr
->name
, value
);
2422 _cupsMutexUnlock(&report_mutex
);
2427 * 'report_printer_state()' - Report the printer state.
2431 report_printer_state(ipp_t
*ipp
) /* I - IPP response */
2433 ipp_attribute_t
*pa
, /* printer-alert */
2434 *pam
, /* printer-alert-message */
2435 *psm
, /* printer-state-message */
2436 *reasons
, /* printer-state-reasons */
2437 *marker
; /* marker-* attributes */
2438 char value
[1024], /* State/message string */
2439 *valptr
; /* Pointer into string */
2440 static int ipp_supplies
= -1;
2441 /* Report supply levels? */
2445 * Report alerts and messages...
2448 if ((pa
= ippFindAttribute(ipp
, "printer-alert", IPP_TAG_TEXT
)) != NULL
)
2451 if ((pam
= ippFindAttribute(ipp
, "printer-alert-message",
2452 IPP_TAG_TEXT
)) != NULL
)
2455 if ((psm
= ippFindAttribute(ipp
, "printer-state-message",
2456 IPP_TAG_TEXT
)) != NULL
)
2458 char *ptr
; /* Pointer into message */
2461 strlcpy(value
, "INFO: ", sizeof(value
));
2462 for (ptr
= psm
->values
[0].string
.text
, valptr
= value
+ 6;
2463 *ptr
&& valptr
< (value
+ sizeof(value
) - 6);
2466 if (*ptr
< ' ' && *ptr
> 0 && *ptr
!= '\t')
2469 * Substitute "<XX>" for the control character; sprintf is safe because
2470 * we always leave 6 chars free at the end...
2473 sprintf(valptr
, "<%02X>", *ptr
);
2483 fputs(value
, stderr
);
2487 * Now report printer-state-reasons, filtering out some of the reasons we never
2491 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
2492 IPP_TAG_KEYWORD
)) == NULL
)
2495 update_reasons(reasons
, NULL
);
2498 * Relay the current marker-* attribute values...
2501 if (ipp_supplies
< 0)
2503 ppd_file_t
*ppd
; /* PPD file */
2504 ppd_attr_t
*ppdattr
; /* Attribute in PPD file */
2506 if ((ppd
= ppdOpenFile(getenv("PPD"))) != NULL
&&
2507 (ppdattr
= ppdFindAttr(ppd
, "cupsIPPSupplies", NULL
)) != NULL
&&
2508 ppdattr
->value
&& _cups_strcasecmp(ppdattr
->value
, "true"))
2516 if (ipp_supplies
> 0)
2518 if ((marker
= ippFindAttribute(ipp
, "marker-colors", IPP_TAG_NAME
)) != NULL
)
2519 report_attr(marker
);
2520 if ((marker
= ippFindAttribute(ipp
, "marker-high-levels",
2521 IPP_TAG_INTEGER
)) != NULL
)
2522 report_attr(marker
);
2523 if ((marker
= ippFindAttribute(ipp
, "marker-levels",
2524 IPP_TAG_INTEGER
)) != NULL
)
2525 report_attr(marker
);
2526 if ((marker
= ippFindAttribute(ipp
, "marker-low-levels",
2527 IPP_TAG_INTEGER
)) != NULL
)
2528 report_attr(marker
);
2529 if ((marker
= ippFindAttribute(ipp
, "marker-message",
2530 IPP_TAG_TEXT
)) != NULL
)
2531 report_attr(marker
);
2532 if ((marker
= ippFindAttribute(ipp
, "marker-names", IPP_TAG_NAME
)) != NULL
)
2533 report_attr(marker
);
2534 if ((marker
= ippFindAttribute(ipp
, "marker-types",
2535 IPP_TAG_KEYWORD
)) != NULL
)
2536 report_attr(marker
);
2541 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
2543 * 'run_as_user()' - Run the IPP backend as the printing user.
2545 * This function uses an XPC-based user agent to run the backend as the printing
2546 * user. We need to do this in order to have access to the user's Kerberos
2550 static int /* O - Exit status */
2551 run_as_user(int argc
, /* I - Number of command-line args */
2552 char *argv
[], /* I - Command-line arguments */
2553 uid_t uid
, /* I - User ID */
2554 const char *device_uri
, /* I - Device URI */
2555 int fd
) /* I - File to print */
2557 const char *auth_negotiate
;/* AUTH_NEGOTIATE env var */
2558 xpc_connection_t conn
; /* Connection to XPC service */
2559 xpc_object_t request
; /* Request message dictionary */
2560 __block xpc_object_t response
; /* Response message dictionary */
2561 dispatch_semaphore_t sem
; /* Semaphore for waiting for response */
2562 int status
= CUPS_BACKEND_FAILED
;
2563 /* Status of request */
2566 fprintf(stderr
, "DEBUG: Running IPP backend as UID %d.\n", (int)uid
);
2569 * Connect to the user agent for the specified UID...
2572 conn
= xpc_connection_create_mach_service(kPMPrintUIToolAgent
,
2573 dispatch_get_global_queue(0, 0), 0);
2576 _cupsLangPrintFilter(stderr
, "ERROR",
2577 _("Unable to start backend process."));
2578 fputs("DEBUG: Unable to create connection to agent.\n", stderr
);
2582 xpc_connection_set_event_handler(conn
,
2583 ^(xpc_object_t event
)
2585 xpc_type_t messageType
= xpc_get_type(event
);
2587 if (messageType
== XPC_TYPE_ERROR
)
2589 if (event
== XPC_ERROR_CONNECTION_INTERRUPTED
)
2590 fprintf(stderr
, "DEBUG: Interrupted connection to service %s.\n",
2591 xpc_connection_get_name(conn
));
2592 else if (event
== XPC_ERROR_CONNECTION_INVALID
)
2593 fprintf(stderr
, "DEBUG: Connection invalid for service %s.\n",
2594 xpc_connection_get_name(conn
));
2596 fprintf(stderr
, "DEBUG: Unxpected error for service %s: %s\n",
2597 xpc_connection_get_name(conn
),
2598 xpc_dictionary_get_string(event
, XPC_ERROR_KEY_DESCRIPTION
));
2601 xpc_connection_set_target_uid(conn
, uid
);
2602 xpc_connection_resume(conn
);
2605 * Try starting the backend...
2608 request
= xpc_dictionary_create(NULL
, NULL
, 0);
2609 xpc_dictionary_set_int64(request
, "command", kPMStartJob
);
2610 xpc_dictionary_set_string(request
, "device-uri", device_uri
);
2611 xpc_dictionary_set_string(request
, "job-id", argv
[1]);
2612 xpc_dictionary_set_string(request
, "user", argv
[2]);
2613 xpc_dictionary_set_string(request
, "title", argv
[3]);
2614 xpc_dictionary_set_string(request
, "copies", argv
[4]);
2615 xpc_dictionary_set_string(request
, "options", argv
[5]);
2616 xpc_dictionary_set_string(request
, "auth-info-required",
2617 getenv("AUTH_INFO_REQUIRED"));
2618 if ((auth_negotiate
= getenv("AUTH_NEGOTIATE")) != NULL
)
2619 xpc_dictionary_set_string(request
, "auth-negotiate", auth_negotiate
);
2620 xpc_dictionary_set_fd(request
, "stdin", fd
);
2621 xpc_dictionary_set_fd(request
, "stderr", 2);
2622 xpc_dictionary_set_fd(request
, "side-channel", CUPS_SC_FD
);
2624 sem
= dispatch_semaphore_create(0);
2627 xpc_connection_send_message_with_reply(conn
, request
,
2628 dispatch_get_global_queue(0,0),
2629 ^(xpc_object_t reply
)
2631 /* Save the response and wake up */
2632 if (xpc_get_type(reply
)
2633 == XPC_TYPE_DICTIONARY
)
2634 response
= xpc_retain(reply
);
2636 dispatch_semaphore_signal(sem
);
2639 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
2640 xpc_release(request
);
2641 dispatch_release(sem
);
2645 child_pid
= xpc_dictionary_get_int64(response
, "child-pid");
2647 xpc_release(response
);
2650 fprintf(stderr
, "DEBUG: Child PID=%d.\n", child_pid
);
2653 _cupsLangPrintFilter(stderr
, "ERROR",
2654 _("Unable to start backend process."));
2655 fputs("DEBUG: No child PID.\n", stderr
);
2661 _cupsLangPrintFilter(stderr
, "ERROR",
2662 _("Unable to start backend process."));
2663 fputs("DEBUG: No reply from agent.\n", stderr
);
2668 * Then wait for the backend to finish...
2671 request
= xpc_dictionary_create(NULL
, NULL
, 0);
2672 xpc_dictionary_set_int64(request
, "command", kPMWaitForJob
);
2673 xpc_dictionary_set_fd(request
, "stderr", 2);
2675 sem
= dispatch_semaphore_create(0);
2678 xpc_connection_send_message_with_reply(conn
, request
,
2679 dispatch_get_global_queue(0,0),
2680 ^(xpc_object_t reply
)
2682 /* Save the response and wake up */
2683 if (xpc_get_type(reply
)
2684 == XPC_TYPE_DICTIONARY
)
2685 response
= xpc_retain(reply
);
2687 dispatch_semaphore_signal(sem
);
2690 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
2691 xpc_release(request
);
2692 dispatch_release(sem
);
2696 status
= xpc_dictionary_get_int64(response
, "status");
2698 if (status
== SIGTERM
|| status
== SIGKILL
|| status
== SIGPIPE
)
2700 fprintf(stderr
, "DEBUG: Child terminated on signal %d.\n", status
);
2701 status
= CUPS_BACKEND_FAILED
;
2703 else if (WIFSIGNALED(status
))
2705 fprintf(stderr
, "DEBUG: Child crashed on signal %d.\n", status
);
2706 status
= CUPS_BACKEND_STOP
;
2708 else if (WIFEXITED(status
))
2710 status
= WEXITSTATUS(status
);
2711 fprintf(stderr
, "DEBUG: Child exited with status %d.\n", status
);
2714 xpc_release(response
);
2717 _cupsLangPrintFilter(stderr
, "ERROR",
2718 _("Unable to get backend exit status."));
2724 xpc_connection_suspend(conn
);
2725 xpc_connection_cancel(conn
);
2731 #endif /* HAVE_GSSAPI && HAVE_XPC */
2735 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
2739 sigterm_handler(int sig
) /* I - Signal */
2741 (void)sig
; /* remove compiler warnings... */
2743 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
2746 kill(child_pid
, sig
);
2749 #endif /* HAVE_GSSAPI && HAVE_XPC */
2754 * Flag that the job should be canceled...
2762 * The scheduler already tried to cancel us once, now just terminate
2763 * after removing our temp file!
2767 unlink(tmpfilename
);
2774 * 'timeout_cb()' - Handle HTTP timeouts.
2777 static int /* O - 1 to continue, 0 to cancel */
2778 timeout_cb(http_t
*http
, /* I - Connection to server (unused) */
2779 void *user_data
) /* I - User data (unused) */
2784 return (!job_canceled
);
2789 * 'update_reasons()' - Update the printer-state-reasons values.
2793 update_reasons(ipp_attribute_t
*attr
, /* I - printer-state-reasons or NULL */
2794 const char *s
) /* I - STATE: string or NULL */
2796 char op
; /* Add (+), remove (-), replace (\0) */
2797 cups_array_t
*new_reasons
; /* New reasons array */
2798 char *reason
, /* Current reason */
2799 add
[2048], /* Reasons added string */
2800 *addptr
, /* Pointer into add string */
2801 rem
[2048], /* Reasons removed string */
2802 *remptr
; /* Pointer into remove string */
2803 const char *addprefix
, /* Current add string prefix */
2804 *remprefix
; /* Current remove string prefix */
2807 fprintf(stderr
, "DEBUG: update_reasons(attr=%d(%s%s), s=\"%s\")\n",
2808 attr
? attr
->num_values
: 0, attr
? attr
->values
[0].string
.text
: "",
2809 attr
&& attr
->num_values
> 1 ? ",..." : "", s
? s
: "(null)");
2812 * Create an array of new reason keyword strings...
2817 int i
; /* Looping var */
2819 new_reasons
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
2822 for (i
= 0; i
< attr
->num_values
; i
++)
2824 reason
= attr
->values
[i
].string
.text
;
2826 if (strcmp(reason
, "none") &&
2827 strcmp(reason
, "none-report") &&
2828 strcmp(reason
, "paused") &&
2829 strncmp(reason
, "spool-area-full", 15) &&
2830 strcmp(reason
, "com.apple.print.recoverable-warning") &&
2831 strncmp(reason
, "cups-", 5))
2832 cupsArrayAdd(new_reasons
, reason
);
2837 if (*s
== '+' || *s
== '-')
2842 new_reasons
= _cupsArrayNewStrings(s
);
2848 * Compute the changes...
2852 addprefix
= "STATE: +";
2855 remprefix
= "STATE: -";
2858 fprintf(stderr
, "DEBUG2: op='%c', new_reasons=%d, state_reasons=%d\n",
2859 op
? op
: ' ', cupsArrayCount(new_reasons
),
2860 cupsArrayCount(state_reasons
));
2862 _cupsMutexLock(&report_mutex
);
2870 for (reason
= (char *)cupsArrayFirst(new_reasons
);
2872 reason
= (char *)cupsArrayNext(new_reasons
))
2874 if (!cupsArrayFind(state_reasons
, reason
))
2876 if (!strncmp(reason
, "cups-remote-", 12))
2879 * If we are setting cups-remote-xxx, remove all other cups-remote-xxx
2883 char *temp
; /* Current reason in state_reasons */
2885 cupsArraySave(state_reasons
);
2887 for (temp
= (char *)cupsArrayFirst(state_reasons
);
2889 temp
= (char *)cupsArrayNext(state_reasons
))
2890 if (!strncmp(temp
, "cups-remote-", 12))
2892 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
2894 remptr
+= strlen(remptr
);
2897 cupsArrayRemove(state_reasons
, temp
);
2901 cupsArrayRestore(state_reasons
);
2904 cupsArrayAdd(state_reasons
, reason
);
2906 snprintf(addptr
, sizeof(add
) - (addptr
- add
), "%s%s", addprefix
,
2908 addptr
+= strlen(addptr
);
2919 for (reason
= (char *)cupsArrayFirst(new_reasons
);
2921 reason
= (char *)cupsArrayNext(new_reasons
))
2923 if (cupsArrayFind(state_reasons
, reason
))
2925 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
2927 remptr
+= strlen(remptr
);
2930 cupsArrayRemove(state_reasons
, reason
);
2937 * Replace reasons...
2940 for (reason
= (char *)cupsArrayFirst(state_reasons
);
2942 reason
= (char *)cupsArrayNext(state_reasons
))
2944 if (strncmp(reason
, "cups-", 5) && !cupsArrayFind(new_reasons
, reason
))
2946 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
2948 remptr
+= strlen(remptr
);
2951 cupsArrayRemove(state_reasons
, reason
);
2955 for (reason
= (char *)cupsArrayFirst(new_reasons
);
2957 reason
= (char *)cupsArrayNext(new_reasons
))
2959 if (!cupsArrayFind(state_reasons
, reason
))
2961 cupsArrayAdd(state_reasons
, reason
);
2963 snprintf(addptr
, sizeof(add
) - (addptr
- add
), "%s%s", addprefix
,
2965 addptr
+= strlen(addptr
);
2971 _cupsMutexUnlock(&report_mutex
);
2974 * Report changes and return...
2977 if (add
[0] && rem
[0])
2978 fprintf(stderr
, "%s\n%s\n", add
, rem
);
2980 fprintf(stderr
, "%s\n", add
);
2982 fprintf(stderr
, "%s\n", rem
);
2986 * End of "$Id: ipp.c 9759 2011-05-11 03:24:33Z mike $".