2 * "$Id: ipp.c 12624 2015-05-06 23:50:20Z msweet $"
4 * IPP backend for CUPS.
6 * Copyright 2007-2014 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 * Include necessary headers.
22 #include "backend-private.h"
23 #include <cups/array-private.h>
24 #include <sys/types.h>
27 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
29 # define kPMPrintUIToolAgent "com.apple.printuitool.agent"
30 # define kPMStartJob 100
31 # define kPMWaitForJob 101
32 # ifdef HAVE_XPC_PRIVATE_H
33 # include <xpc/private.h>
35 extern void xpc_connection_set_target_uid(xpc_connection_t connection
,
37 # endif /* HAVE_XPC_PRIVATE_H */
38 #endif /* HAVE_GSSAPI && HAVE_XPC */
42 * Bits for job-state-reasons we care about...
45 #define _CUPS_JSR_ACCOUNT_AUTHORIZATION_FAILED 0x01
46 #define _CUPS_JSR_ACCOUNT_CLOSED 0x02
47 #define _CUPS_JSR_ACCOUNT_INFO_NEEDED 0x04
48 #define _CUPS_JSR_ACCOUNT_LIMIT_REACHED 0x08
49 #define _CUPS_JSR_JOB_PASSWORD_WAIT 0x10
50 #define _CUPS_JSR_JOB_RELEASE_WAIT 0x20
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 job_reasons
, /* Job state reasons bits */
67 create_job
, /* Support Create-Job? */
68 get_job_attrs
; /* Support Get-Job-Attributes? */
69 const char *job_name
; /* Job name for submitted job */
70 http_encryption_t encryption
; /* Use encryption? */
71 ipp_jstate_t job_state
; /* Current job state */
72 ipp_pstate_t printer_state
; /* Current printer state */
80 static const char *auth_info_required
;
81 /* New auth-info-required value */
82 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
83 static pid_t child_pid
= 0; /* Child process ID */
84 #endif /* HAVE_GSSAPI && HAVE_XPC */
85 static const char * const jattrs
[] = /* Job attributes we want */
88 "job-impressions-completed",
89 "job-media-sheets-completed",
91 "job-originating-user-name",
95 static int job_canceled
= 0,
98 /* Credentials supplied in URI? */
99 static char username
[256] = "",
100 /* Username for device URI */
102 /* Password for device URI */
103 static const char * const pattrs
[] = /* Printer attributes we want */
106 "compression-supported",
107 #endif /* HAVE_LIBZ */
110 "document-format-supported",
112 "marker-high-levels",
118 "media-col-supported",
119 "multiple-document-handling-supported",
120 "operations-supported",
121 "print-color-mode-supported",
123 "printer-alert-description",
124 "printer-is-accepting-jobs",
126 "printer-state-message",
127 "printer-state-reasons"
129 static const char * const remote_job_states
[] =
130 { /* Remote job state keywords */
131 "+cups-remote-pending",
132 "+cups-remote-pending-held",
133 "+cups-remote-processing",
134 "+cups-remote-stopped",
135 "+cups-remote-canceled",
136 "+cups-remote-aborted",
137 "+cups-remote-completed"
139 static _cups_mutex_t report_mutex
= _CUPS_MUTEX_INITIALIZER
;
140 /* Mutex to control access */
141 static int num_attr_cache
= 0;
142 /* Number of cached attributes */
143 static cups_option_t
*attr_cache
= NULL
;
144 /* Cached attributes */
145 static cups_array_t
*state_reasons
; /* Array of printe-state-reasons keywords */
146 static char tmpfilename
[1024] = "";
147 /* Temporary spool file name */
154 static void cancel_job(http_t
*http
, const char *uri
, int id
,
155 const char *resource
, const char *user
,
157 static ipp_pstate_t
check_printer_state(http_t
*http
, const char *uri
,
158 const char *resource
,
159 const char *user
, int version
);
160 static void *monitor_printer(_cups_monitor_t
*monitor
);
161 static ipp_t
*new_request(ipp_op_t op
, int version
, const char *uri
,
162 const char *user
, const char *title
,
163 int num_options
, cups_option_t
*options
,
164 const char *compression
, int copies
,
165 const char *format
, _ppd_cache_t
*pc
,
167 ipp_attribute_t
*media_col_sup
,
168 ipp_attribute_t
*doc_handling_sup
,
169 ipp_attribute_t
*print_color_mode_sup
);
170 static const char *password_cb(const char *prompt
, http_t
*http
,
171 const char *method
, const char *resource
,
173 static const char *quote_string(const char *s
, char *q
, size_t qsize
);
174 static void report_attr(ipp_attribute_t
*attr
);
175 static void report_printer_state(ipp_t
*ipp
);
176 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
177 static int run_as_user(char *argv
[], uid_t uid
,
178 const char *device_uri
, int fd
);
179 #endif /* HAVE_GSSAPI && HAVE_XPC */
180 static void sigterm_handler(int sig
);
181 static int timeout_cb(http_t
*http
, void *user_data
);
182 static void update_reasons(ipp_attribute_t
*attr
, const char *s
);
186 * 'main()' - Send a file to the printer or server.
190 * printer-uri job-id user title copies options [file]
193 int /* O - Exit status */
194 main(int argc
, /* I - Number of command-line args */
195 char *argv
[]) /* I - Command-line arguments */
197 int i
; /* Looping var */
198 int send_options
; /* Send job options? */
199 int num_options
; /* Number of printer options */
200 cups_option_t
*options
; /* Printer options */
201 const char *device_uri
; /* Device URI */
202 char scheme
[255], /* Scheme in URI */
203 hostname
[1024], /* Hostname */
204 resource
[1024], /* Resource info (printer name) */
205 addrname
[256], /* Address name */
206 *optptr
, /* Pointer to URI options */
207 *name
, /* Name of option */
208 *value
, /* Value of option */
209 sep
; /* Separator character */
210 int password_tries
= 0; /* Password tries */
211 http_addrlist_t
*addrlist
; /* Address of printer */
212 int snmp_enabled
= 1; /* Is SNMP enabled? */
213 int snmp_fd
, /* SNMP socket */
214 start_count
, /* Page count via SNMP at start */
215 page_count
, /* Page count via SNMP */
216 have_supplies
; /* Printer supports supply levels? */
217 int num_files
; /* Number of files to print */
218 char **files
, /* Files to print */
219 *compatfile
= NULL
; /* Compatibility filename */
220 off_t compatsize
= 0; /* Size of compatibility file */
221 int port
; /* Port number (not used) */
222 char portname
[255]; /* Port name */
223 char uri
[HTTP_MAX_URI
]; /* Updated URI without user/pass */
224 char print_job_name
[1024]; /* Update job-name for Print-Job */
225 http_status_t http_status
; /* Status of HTTP request */
226 ipp_status_t ipp_status
; /* Status of IPP request */
227 http_t
*http
; /* HTTP connection */
228 ipp_t
*request
, /* IPP request */
229 *response
, /* IPP response */
230 *supported
; /* get-printer-attributes response */
231 time_t start_time
; /* Time of first connect */
232 int contimeout
; /* Connection timeout */
233 int delay
, /* Delay for retries */
234 prev_delay
; /* Previous delay */
235 const char *compression
; /* Compression mode */
236 int waitjob
, /* Wait for job complete? */
237 waitjob_tries
= 0, /* Number of times we've waited */
238 waitprinter
; /* Wait for printer ready? */
239 _cups_monitor_t monitor
; /* Monitoring data */
240 ipp_attribute_t
*job_id_attr
; /* job-id attribute */
241 int job_id
; /* job-id value */
242 ipp_attribute_t
*job_sheets
; /* job-media-sheets-completed */
243 ipp_attribute_t
*job_state
; /* job-state */
245 ipp_attribute_t
*compression_sup
; /* compression-supported */
246 #endif /* HAVE_LIBZ */
247 ipp_attribute_t
*copies_sup
; /* copies-supported */
248 ipp_attribute_t
*cups_version
; /* cups-version */
249 ipp_attribute_t
*format_sup
; /* document-format-supported */
250 ipp_attribute_t
*job_auth
; /* job-authorization-uri */
251 ipp_attribute_t
*media_col_sup
; /* media-col-supported */
252 ipp_attribute_t
*operations_sup
; /* operations-supported */
253 ipp_attribute_t
*doc_handling_sup
; /* multiple-document-handling-supported */
254 ipp_attribute_t
*printer_state
; /* printer-state attribute */
255 ipp_attribute_t
*printer_accepting
; /* printer-is-accepting-jobs */
256 ipp_attribute_t
*print_color_mode_sup
;/* Does printer support print-color-mode? */
257 int create_job
= 0, /* Does printer support Create-Job? */
258 get_job_attrs
= 0, /* Does printer support Get-Job-Attributes? */
259 send_document
= 0, /* Does printer support Send-Document? */
260 validate_job
= 0, /* Does printer support Validate-Job? */
261 copies
, /* Number of copies for job */
262 copies_remaining
; /* Number of copies remaining */
263 const char *content_type
, /* CONTENT_TYPE environment variable */
264 *final_content_type
, /* FINAL_CONTENT_TYPE environment var */
265 *document_format
; /* document-format value */
266 int fd
; /* File descriptor */
267 off_t bytes
= 0; /* Bytes copied */
268 char buffer
[16384]; /* Copy buffer */
269 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
270 struct sigaction action
; /* Actions for POSIX signals */
271 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
272 int version
; /* IPP version */
273 ppd_file_t
*ppd
= NULL
; /* PPD file */
274 _ppd_cache_t
*pc
= NULL
; /* PPD cache and mapping data */
275 fd_set input
; /* Input set for select() */
279 * Make sure status messages are not buffered...
282 setbuf(stderr
, NULL
);
285 * Ignore SIGPIPE and catch SIGTERM signals...
289 sigset(SIGPIPE
, SIG_IGN
);
290 sigset(SIGTERM
, sigterm_handler
);
291 #elif defined(HAVE_SIGACTION)
292 memset(&action
, 0, sizeof(action
));
293 action
.sa_handler
= SIG_IGN
;
294 sigaction(SIGPIPE
, &action
, NULL
);
296 sigemptyset(&action
.sa_mask
);
297 sigaddset(&action
.sa_mask
, SIGTERM
);
298 action
.sa_handler
= sigterm_handler
;
299 sigaction(SIGTERM
, &action
, NULL
);
301 signal(SIGPIPE
, SIG_IGN
);
302 signal(SIGTERM
, sigterm_handler
);
303 #endif /* HAVE_SIGSET */
306 * Check command-line...
313 if ((s
= strrchr(argv
[0], '/')) != NULL
)
318 printf("network %s \"Unknown\" \"%s (%s)\"\n",
319 s
, _cupsLangString(cupsLangDefault(),
320 _("Internet Printing Protocol")), s
);
321 return (CUPS_BACKEND_OK
);
325 _cupsLangPrintf(stderr
,
326 _("Usage: %s job-id user title copies options [file]"),
328 return (CUPS_BACKEND_STOP
);
332 * Get the device URI...
335 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
337 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
340 if (getenv("CLASS") != NULL
)
341 return (CUPS_BACKEND_FAILED
);
344 if ((auth_info_required
= getenv("AUTH_INFO_REQUIRED")) == NULL
)
345 auth_info_required
= "none";
347 state_reasons
= _cupsArrayNewStrings(getenv("PRINTER_STATE_REASONS"), ',');
351 * For Kerberos, become the printing user (if we can) to get the credentials
355 if (!getuid() && (value
= getenv("AUTH_UID")) != NULL
&&
356 !getenv("AUTH_PASSWORD"))
358 uid_t uid
= (uid_t
)atoi(value
);
365 return (run_as_user(argv
, uid
, device_uri
, 0));
368 int status
= 0; /* Exit status */
370 for (i
= 6; i
< argc
&& !status
&& !job_canceled
; i
++)
372 if ((fd
= open(argv
[i
], O_RDONLY
)) >= 0)
374 status
= run_as_user(argv
, uid
, device_uri
, fd
);
379 _cupsLangPrintError("ERROR", _("Unable to open print file"));
380 status
= CUPS_BACKEND_FAILED
;
388 # else /* No XPC, just try to run as the user ID */
391 # endif /* HAVE_XPC */
393 #endif /* HAVE_GSSAPI */
396 * Get the (final) content type...
399 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
400 content_type
= "application/octet-stream";
402 if ((final_content_type
= getenv("FINAL_CONTENT_TYPE")) == NULL
)
404 final_content_type
= content_type
;
406 if (!strncmp(final_content_type
, "printer/", 8))
407 final_content_type
= "application/vnd.cups-raw";
411 * Extract the hostname and printer name from the URI...
414 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
415 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
416 resource
, sizeof(resource
));
419 port
= IPP_PORT
; /* Default to port 631 */
421 if (!strcmp(scheme
, "https") || !strcmp(scheme
, "ipps"))
422 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
424 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
427 * See if there are any options...
434 contimeout
= 7 * 24 * 60 * 60;
436 if ((optptr
= strchr(resource
, '?')) != NULL
)
439 * Yup, terminate the device name string and move to the first
440 * character of the optptr...
446 * Then parse the optptr...
457 while (*optptr
&& *optptr
!= '=' && *optptr
!= '+' && *optptr
!= '&')
460 if ((sep
= *optptr
) != '\0')
471 while (*optptr
&& *optptr
!= '+' && *optptr
!= '&')
481 * Process the option...
484 if (!_cups_strcasecmp(name
, "waitjob"))
487 * Wait for job completion?
490 waitjob
= !_cups_strcasecmp(value
, "on") ||
491 !_cups_strcasecmp(value
, "yes") ||
492 !_cups_strcasecmp(value
, "true");
494 else if (!_cups_strcasecmp(name
, "waitprinter"))
497 * Wait for printer idle?
500 waitprinter
= !_cups_strcasecmp(value
, "on") ||
501 !_cups_strcasecmp(value
, "yes") ||
502 !_cups_strcasecmp(value
, "true");
504 else if (!_cups_strcasecmp(name
, "encryption"))
507 * Enable/disable encryption?
510 if (!_cups_strcasecmp(value
, "always"))
511 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
512 else if (!_cups_strcasecmp(value
, "required"))
513 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
514 else if (!_cups_strcasecmp(value
, "never"))
515 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
516 else if (!_cups_strcasecmp(value
, "ifrequested"))
517 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
520 _cupsLangPrintFilter(stderr
, "ERROR",
521 _("Unknown encryption option value: \"%s\"."),
525 else if (!_cups_strcasecmp(name
, "snmp"))
528 * Enable/disable SNMP stuff...
531 snmp_enabled
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
532 !_cups_strcasecmp(value
, "yes") ||
533 !_cups_strcasecmp(value
, "true");
535 else if (!_cups_strcasecmp(name
, "version"))
537 if (!strcmp(value
, "1.0"))
539 else if (!strcmp(value
, "1.1"))
541 else if (!strcmp(value
, "2.0"))
543 else if (!strcmp(value
, "2.1"))
545 else if (!strcmp(value
, "2.2"))
549 _cupsLangPrintFilter(stderr
, "ERROR",
550 _("Unknown version option value: \"%s\"."),
555 else if (!_cups_strcasecmp(name
, "compression"))
557 if (!_cups_strcasecmp(value
, "true") || !_cups_strcasecmp(value
, "yes") ||
558 !_cups_strcasecmp(value
, "on") || !_cups_strcasecmp(value
, "gzip"))
559 compression
= "gzip";
560 else if (!_cups_strcasecmp(value
, "deflate"))
561 compression
= "deflate";
562 else if (!_cups_strcasecmp(value
, "false") ||
563 !_cups_strcasecmp(value
, "no") ||
564 !_cups_strcasecmp(value
, "off") ||
565 !_cups_strcasecmp(value
, "none"))
566 compression
= "none";
568 #endif /* HAVE_LIBZ */
569 else if (!_cups_strcasecmp(name
, "contimeout"))
572 * Set the connection timeout...
576 contimeout
= atoi(value
);
584 _cupsLangPrintFilter(stderr
, "ERROR",
585 _("Unknown option \"%s\" with value \"%s\"."),
592 * If we have 7 arguments, print the file named on the command-line.
593 * Otherwise, copy stdin to a temporary file and print the temporary
601 send_options
= !_cups_strcasecmp(final_content_type
, "application/pdf") ||
602 !_cups_strcasecmp(final_content_type
, "application/vnd.cups-pdf") ||
603 !_cups_strncasecmp(final_content_type
, "image/", 6);
605 fputs("DEBUG: Sending stdin for job...\n", stderr
);
610 * Point to the files on the command-line...
613 num_files
= argc
- 6;
617 fprintf(stderr
, "DEBUG: %d files to send in job...\n", num_files
);
621 * Set the authentication info, if any...
624 cupsSetPasswordCB2((cups_password_cb2_t
)password_cb
, &password_tries
);
629 * Use authentication information in the device URI...
632 if ((password
= strchr(username
, ':')) != NULL
)
635 cupsSetUser(username
);
641 * Try loading authentication information from the environment.
644 const char *ptr
= getenv("AUTH_USERNAME");
648 strlcpy(username
, ptr
, sizeof(username
));
652 password
= getenv("AUTH_PASSWORD");
656 * Try finding the remote server...
659 start_time
= time(NULL
);
661 sprintf(portname
, "%d", port
);
663 update_reasons(NULL
, "+connecting-to-device");
664 fprintf(stderr
, "DEBUG: Looking up \"%s\"...\n", hostname
);
666 while ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
668 _cupsLangPrintFilter(stderr
, "INFO",
669 _("Unable to locate printer \"%s\"."), hostname
);
672 if (getenv("CLASS") != NULL
)
674 update_reasons(NULL
, "-connecting-to-device");
675 return (CUPS_BACKEND_STOP
);
679 return (CUPS_BACKEND_OK
);
682 http
= httpConnect2(hostname
, port
, addrlist
, AF_UNSPEC
, cupsEncryption(), 1,
684 httpSetTimeout(http
, 30.0, timeout_cb
, NULL
);
686 if (httpIsEncrypted(http
))
689 * Validate TLS credentials...
692 cups_array_t
*creds
; /* TLS credentials */
693 cups_array_t
*lcreds
= NULL
; /* Loaded credentials */
694 http_trust_t trust
; /* Trust level */
695 static const char *trusts
[] = { NULL
, "+cups-pki-invalid", "+cups-pki-changed", "+cups-pki-expired", NULL
, "+cups-pki-unknown" };
698 if (!httpCopyCredentials(http
, &creds
))
700 trust
= httpCredentialsGetTrust(creds
, hostname
);
702 update_reasons(NULL
, "-cups-pki-invalid,cups-pki-changed,cups-pki-expired,cups-pki-unknown");
705 update_reasons(NULL
, trusts
[trust
]);
706 return (CUPS_BACKEND_STOP
);
709 if (httpLoadCredentials(NULL
, &lcreds
, hostname
))
712 * Could not load the credentials, let's save the ones we have so we
713 * can detect changes...
716 httpSaveCredentials(NULL
, creds
, hostname
);
719 httpFreeCredentials(lcreds
);
720 httpFreeCredentials(creds
);
725 * See if the printer supports SNMP...
729 snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
);
734 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
),
737 have_supplies
= start_count
= 0;
740 * Wait for data from the filter...
745 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), 0, backendNetworkSideCB
))
746 return (CUPS_BACKEND_OK
);
747 else if ((bytes
= read(0, buffer
, sizeof(buffer
))) <= 0)
748 return (CUPS_BACKEND_OK
);
752 * Try connecting to the remote server...
755 delay
= _cupsNextDelay(0, &prev_delay
);
759 fprintf(stderr
, "DEBUG: Connecting to %s:%d\n", hostname
, port
);
760 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
762 if (httpReconnect(http
))
764 int error
= errno
; /* Connection error */
766 if (http
->status
== HTTP_PKI_ERROR
)
767 update_reasons(NULL
, "+cups-certificate-error");
772 if (getenv("CLASS") != NULL
)
775 * If the CLASS environment variable is set, the job was submitted
776 * to a class and not to a specific queue. In this case, we want
777 * to abort immediately so that the job can be requeued on the next
778 * available printer in the class.
781 _cupsLangPrintFilter(stderr
, "INFO",
782 _("Unable to contact printer, queuing on next "
783 "printer in class."));
786 * Sleep 5 seconds to keep the job from requeuing too rapidly...
791 update_reasons(NULL
, "-connecting-to-device");
793 return (CUPS_BACKEND_FAILED
);
796 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
798 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
799 errno
== EHOSTUNREACH
)
801 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
803 _cupsLangPrintFilter(stderr
, "ERROR",
804 _("The printer is not responding."));
805 update_reasons(NULL
, "-connecting-to-device");
806 return (CUPS_BACKEND_FAILED
);
812 _cupsLangPrintFilter(stderr
, "WARNING",
813 _("The printer may not exist or "
814 "is unavailable at this time."));
818 _cupsLangPrintFilter(stderr
, "WARNING",
819 _("The printer is unreachable at this "
825 _cupsLangPrintFilter(stderr
, "WARNING",
826 _("The printer is in use."));
830 sleep((unsigned)delay
);
832 delay
= _cupsNextDelay(delay
, &prev_delay
);
836 _cupsLangPrintFilter(stderr
, "ERROR",
837 _("The printer is not responding."));
845 update_reasons(NULL
, "-cups-certificate-error");
847 while (http
->fd
< 0);
850 return (CUPS_BACKEND_OK
);
852 return (CUPS_BACKEND_FAILED
);
854 update_reasons(NULL
, "-connecting-to-device");
855 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
857 fprintf(stderr
, "DEBUG: Connected to %s:%d...\n",
858 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
859 httpAddrPort(http
->hostaddr
));
862 * Build a URI for the printer and fill the standard IPP attributes for
863 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
864 * might contain username:password information...
867 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), scheme
, NULL
, hostname
,
871 * First validate the destination and see if the device supports multiple
876 compression_sup
= NULL
;
877 #endif /* HAVE_LIBZ */
881 media_col_sup
= NULL
;
883 operations_sup
= NULL
;
884 doc_handling_sup
= NULL
;
885 print_color_mode_sup
= NULL
;
890 * Check for side-channel requests...
893 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
896 * Build the IPP request...
899 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
900 ippSetVersion(request
, version
/ 10, version
% 10);
901 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
904 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
905 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
912 fputs("DEBUG: Getting supported attributes...\n", stderr
);
914 if (http
->version
< HTTP_1_1
)
916 fprintf(stderr
, "DEBUG: Printer responded with HTTP version %d.%d.\n",
917 http
->version
/ 100, http
->version
% 100);
918 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
919 "cups-ipp-wrong-http-version");
922 supported
= cupsDoRequest(http
, request
, resource
);
923 ipp_status
= cupsLastError();
925 fprintf(stderr
, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
926 ippErrorString(ipp_status
), cupsLastErrorString());
928 if (ipp_status
<= IPP_OK_CONFLICT
)
932 fprintf(stderr
, "DEBUG: Get-Printer-Attributes returned %s.\n",
933 ippErrorString(ipp_status
));
935 if (ipp_status
== IPP_PRINTER_BUSY
||
936 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
938 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
940 _cupsLangPrintFilter(stderr
, "ERROR",
941 _("The printer is not responding."));
942 return (CUPS_BACKEND_FAILED
);
945 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
947 report_printer_state(supported
);
949 sleep((unsigned)delay
);
951 delay
= _cupsNextDelay(delay
, &prev_delay
);
953 else if ((ipp_status
== IPP_BAD_REQUEST
||
954 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
> 10)
957 * Switch to IPP/1.1 or IPP/1.0...
962 _cupsLangPrintFilter(stderr
, "INFO", _("Preparing to print."));
964 "DEBUG: The printer does not support IPP/%d.%d, trying "
965 "IPP/1.1.\n", version
/ 10, version
% 10);
970 _cupsLangPrintFilter(stderr
, "INFO", _("Preparing to print."));
972 "DEBUG: The printer does not support IPP/%d.%d, trying "
973 "IPP/1.0.\n", version
/ 10, version
% 10);
979 else if (ipp_status
== IPP_NOT_FOUND
)
981 _cupsLangPrintFilter(stderr
, "ERROR",
982 _("The printer configuration is incorrect or the "
983 "printer no longer exists."));
985 ippDelete(supported
);
987 return (CUPS_BACKEND_STOP
);
989 else if (ipp_status
== IPP_FORBIDDEN
||
990 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
992 const char *www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
993 /* WWW-Authenticate field value */
995 if (!strncmp(www_auth
, "Negotiate", 9))
996 auth_info_required
= "negotiate";
997 else if (www_auth
[0])
998 auth_info_required
= "username,password";
1000 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
1001 return (CUPS_BACKEND_AUTH_REQUIRED
);
1003 else if (ipp_status
!= IPP_NOT_AUTHORIZED
)
1005 _cupsLangPrintFilter(stderr
, "ERROR",
1006 _("Unable to get printer status."));
1009 httpReconnect(http
);
1012 ippDelete(supported
);
1017 if (!getenv("CLASS"))
1020 * Check printer-is-accepting-jobs = false and printer-state-reasons for the
1021 * "spool-area-full" keyword...
1026 if ((printer_accepting
= ippFindAttribute(supported
,
1027 "printer-is-accepting-jobs",
1028 IPP_TAG_BOOLEAN
)) != NULL
&&
1029 !printer_accepting
->values
[0].boolean
)
1031 else if (!printer_accepting
)
1032 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1033 "cups-ipp-missing-printer-is-accepting-jobs");
1035 if ((printer_state
= ippFindAttribute(supported
,
1036 "printer-state-reasons",
1037 IPP_TAG_KEYWORD
)) == NULL
)
1039 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1040 "cups-ipp-missing-printer-state-reasons");
1044 for (i
= 0; i
< printer_state
->num_values
; i
++)
1046 if (!strcmp(printer_state
->values
[0].string
.text
,
1047 "spool-area-full") ||
1048 !strncmp(printer_state
->values
[0].string
.text
, "spool-area-full-",
1059 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
1061 report_printer_state(supported
);
1063 sleep((unsigned)delay
);
1065 delay
= _cupsNextDelay(delay
, &prev_delay
);
1067 ippDelete(supported
);
1069 ipp_status
= IPP_STATUS_ERROR_BUSY
;
1075 * Check for supported attributes...
1079 if ((compression_sup
= ippFindAttribute(supported
, "compression-supported",
1080 IPP_TAG_KEYWORD
)) != NULL
)
1083 * Check whether the requested compression is supported and/or default to
1084 * compression if supported...
1087 if (compression
&& !ippContainsString(compression_sup
, compression
))
1089 fprintf(stderr
, "DEBUG: Printer does not support the requested "
1090 "compression value \"%s\".\n", compression
);
1093 else if (!compression
)
1095 if (ippContainsString(compression_sup
, "gzip"))
1096 compression
= "gzip";
1097 else if (ippContainsString(compression_sup
, "deflate"))
1098 compression
= "deflate";
1101 fprintf(stderr
, "DEBUG: Automatically using \"%s\" compression.\n",
1105 #endif /* HAVE_LIBZ */
1107 if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
1108 IPP_TAG_RANGE
)) != NULL
)
1111 * Has the "copies-supported" attribute - does it have an upper
1115 fprintf(stderr
, "DEBUG: copies-supported=%d-%d\n",
1116 copies_sup
->values
[0].range
.lower
,
1117 copies_sup
->values
[0].range
.upper
);
1119 if (copies_sup
->values
[0].range
.upper
<= 1)
1120 copies_sup
= NULL
; /* No */
1123 cups_version
= ippFindAttribute(supported
, "cups-version", IPP_TAG_TEXT
);
1125 if ((format_sup
= ippFindAttribute(supported
, "document-format-supported",
1126 IPP_TAG_MIMETYPE
)) != NULL
)
1128 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
1129 format_sup
->num_values
);
1130 for (i
= 0; i
< format_sup
->num_values
; i
++)
1131 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
1132 format_sup
->values
[i
].string
.text
);
1135 if ((media_col_sup
= ippFindAttribute(supported
, "media-col-supported",
1136 IPP_TAG_KEYWORD
)) != NULL
)
1138 fprintf(stderr
, "DEBUG: media-col-supported (%d values)\n",
1139 media_col_sup
->num_values
);
1140 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
1141 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
1142 media_col_sup
->values
[i
].string
.text
);
1145 print_color_mode_sup
= ippFindAttribute(supported
, "print-color-mode-supported", IPP_TAG_KEYWORD
);
1147 if ((operations_sup
= ippFindAttribute(supported
, "operations-supported",
1148 IPP_TAG_ENUM
)) != NULL
)
1150 fprintf(stderr
, "DEBUG: operations-supported (%d values)\n",
1151 operations_sup
->num_values
);
1152 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1153 fprintf(stderr
, "DEBUG: [%d] = %s\n", i
,
1154 ippOpString(operations_sup
->values
[i
].integer
));
1156 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1157 if (operations_sup
->values
[i
].integer
== IPP_PRINT_JOB
)
1160 if (i
>= operations_sup
->num_values
)
1161 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1162 "cups-ipp-missing-print-job");
1164 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1165 if (operations_sup
->values
[i
].integer
== IPP_CANCEL_JOB
)
1168 if (i
>= operations_sup
->num_values
)
1169 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1170 "cups-ipp-missing-cancel-job");
1172 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1173 if (operations_sup
->values
[i
].integer
== IPP_GET_JOB_ATTRIBUTES
)
1176 if (i
>= operations_sup
->num_values
)
1177 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1178 "cups-ipp-missing-get-job-attributes");
1180 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1181 if (operations_sup
->values
[i
].integer
== IPP_GET_PRINTER_ATTRIBUTES
)
1184 if (i
>= operations_sup
->num_values
)
1185 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1186 "cups-ipp-missing-get-printer-attributes");
1188 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1190 if (operations_sup
->values
[i
].integer
== IPP_VALIDATE_JOB
)
1192 else if (operations_sup
->values
[i
].integer
== IPP_CREATE_JOB
)
1194 else if (operations_sup
->values
[i
].integer
== IPP_SEND_DOCUMENT
)
1196 else if (operations_sup
->values
[i
].integer
== IPP_GET_JOB_ATTRIBUTES
)
1200 if (create_job
&& !send_document
)
1202 fputs("DEBUG: Printer supports Create-Job but not Send-Document.\n",
1206 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1207 "cups-ipp-missing-send-document");
1211 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1212 "cups-ipp-missing-validate-job");
1215 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1216 "cups-ipp-missing-operations-supported");
1218 doc_handling_sup
= ippFindAttribute(supported
,
1219 "multiple-document-handling-supported",
1222 report_printer_state(supported
);
1224 while (!job_canceled
&& ipp_status
> IPP_OK_CONFLICT
);
1227 return (CUPS_BACKEND_OK
);
1230 * See if the printer is accepting jobs and is not stopped; if either
1231 * condition is true and we are printing to a class, requeue the job...
1234 if (getenv("CLASS") != NULL
)
1236 printer_state
= ippFindAttribute(supported
, "printer-state",
1238 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
1241 if (printer_state
== NULL
||
1242 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
1244 printer_accepting
== NULL
||
1245 !printer_accepting
->values
[0].boolean
)
1248 * If the CLASS environment variable is set, the job was submitted
1249 * to a class and not to a specific queue. In this case, we want
1250 * to abort immediately so that the job can be requeued on the next
1251 * available printer in the class.
1254 _cupsLangPrintFilter(stderr
, "INFO",
1255 _("Unable to contact printer, queuing on next "
1256 "printer in class."));
1258 ippDelete(supported
);
1262 * Sleep 5 seconds to keep the job from requeuing too rapidly...
1267 return (CUPS_BACKEND_FAILED
);
1272 * See if the printer supports multiple copies...
1275 copies
= atoi(argv
[4]);
1277 if (copies_sup
|| argc
< 7)
1278 copies_remaining
= 1;
1280 copies_remaining
= copies
;
1283 * Prepare remaining printing options...
1290 num_options
= cupsParseOptions(argv
[5], 0, &options
);
1292 if (!cups_version
&& media_col_sup
)
1295 * Load the PPD file and generate PWG attribute mapping information...
1298 ppd
= ppdOpenFile(getenv("PPD"));
1299 pc
= _ppdCacheCreateWithPPD(ppd
);
1301 ppdMarkDefaults(ppd
);
1302 cupsMarkOptions(ppd
, num_options
, options
);
1308 document_format
= NULL
;
1310 if (format_sup
!= NULL
)
1312 for (i
= 0; i
< format_sup
->num_values
; i
++)
1313 if (!_cups_strcasecmp(final_content_type
,
1314 format_sup
->values
[i
].string
.text
))
1316 document_format
= final_content_type
;
1320 if (!document_format
)
1322 for (i
= 0; i
< format_sup
->num_values
; i
++)
1323 if (!_cups_strcasecmp("application/octet-stream",
1324 format_sup
->values
[i
].string
.text
))
1326 document_format
= "application/octet-stream";
1332 fprintf(stderr
, "DEBUG: final_content_type=\"%s\", document_format=\"%s\"\n",
1333 final_content_type
, document_format
? document_format
: "(null)");
1336 * If the printer does not support HTTP/1.1 (which IPP requires), copy stdin
1337 * to a temporary file so that we can do a HTTP/1.0 submission...
1339 * (I hate compatibility hacks!)
1342 if (http
->version
< HTTP_1_1
&& num_files
== 0)
1344 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
1346 perror("DEBUG: Unable to create temporary file");
1347 return (CUPS_BACKEND_FAILED
);
1350 _cupsLangPrintFilter(stderr
, "INFO", _("Copying print data."));
1352 if ((compatsize
= write(fd
, buffer
, (size_t)bytes
)) < 0)
1354 perror("DEBUG: Unable to write temporary file");
1355 return (CUPS_BACKEND_FAILED
);
1358 if ((bytes
= backendRunLoop(-1, fd
, snmp_fd
, &(addrlist
->addr
), 0, 0,
1359 backendNetworkSideCB
)) < 0)
1360 return (CUPS_BACKEND_FAILED
);
1362 compatsize
+= bytes
;
1366 compatfile
= tmpfilename
;
1367 files
= &compatfile
;
1370 else if (http
->version
< HTTP_1_1
&& num_files
== 1)
1372 struct stat fileinfo
; /* File information */
1374 if (!stat(files
[0], &fileinfo
))
1375 compatsize
= fileinfo
.st_size
;
1379 * If the printer only claims to support IPP/1.0, or if the user specifically
1380 * included version=1.0 in the URI, then do not try to use Create-Job or
1381 * Send-Document. This is another dreaded compatibility hack, but
1382 * unfortunately there are enough broken printers out there that we need
1387 create_job
= send_document
= 0;
1390 * Start monitoring the printer in the background...
1394 monitor
.hostname
= hostname
;
1395 monitor
.user
= argv
[2];
1396 monitor
.resource
= resource
;
1397 monitor
.port
= port
;
1398 monitor
.version
= version
;
1400 monitor
.create_job
= create_job
;
1401 monitor
.get_job_attrs
= get_job_attrs
;
1402 monitor
.encryption
= cupsEncryption();
1403 monitor
.job_state
= IPP_JOB_PENDING
;
1404 monitor
.printer_state
= IPP_PRINTER_IDLE
;
1408 monitor
.job_name
= argv
[3];
1412 snprintf(print_job_name
, sizeof(print_job_name
), "%s - %s", argv
[1],
1414 monitor
.job_name
= print_job_name
;
1417 _cupsThreadCreate((_cups_thread_func_t
)monitor_printer
, &monitor
);
1420 * Validate access to the printer...
1423 while (!job_canceled
&& validate_job
)
1425 request
= new_request(IPP_VALIDATE_JOB
, version
, uri
, argv
[2],
1426 monitor
.job_name
, num_options
, options
, compression
,
1427 copies_sup
? copies
: 1, document_format
, pc
, ppd
,
1428 media_col_sup
, doc_handling_sup
, print_color_mode_sup
);
1430 response
= cupsDoRequest(http
, request
, resource
);
1432 ipp_status
= cupsLastError();
1434 fprintf(stderr
, "DEBUG: Validate-Job: %s (%s)\n",
1435 ippErrorString(ipp_status
), cupsLastErrorString());
1437 if ((job_auth
= ippFindAttribute(response
, "job-authorization-uri",
1438 IPP_TAG_URI
)) != NULL
)
1439 num_options
= cupsAddOption("job-authorization-uri",
1440 ippGetString(job_auth
, 0, NULL
), num_options
,
1443 ippDelete(response
);
1448 if (ipp_status
== IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
||
1449 ipp_status
== IPP_STATUS_ERROR_BUSY
)
1451 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
1454 else if (ipp_status
== IPP_STATUS_ERROR_DOCUMENT_FORMAT_NOT_SUPPORTED
||
1455 ipp_status
== IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES
||
1456 ipp_status
== IPP_STATUS_ERROR_CUPS_ACCOUNT_INFO_NEEDED
||
1457 ipp_status
== IPP_STATUS_ERROR_CUPS_ACCOUNT_CLOSED
||
1458 ipp_status
== IPP_STATUS_ERROR_CUPS_ACCOUNT_LIMIT_REACHED
||
1459 ipp_status
== IPP_STATUS_ERROR_CUPS_ACCOUNT_AUTHORIZATION_FAILED
)
1461 else if (ipp_status
== IPP_STATUS_ERROR_FORBIDDEN
||
1462 ipp_status
== IPP_STATUS_ERROR_CUPS_AUTHENTICATION_CANCELED
)
1464 const char *www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
1465 /* WWW-Authenticate field value */
1467 if (!strncmp(www_auth
, "Negotiate", 9))
1468 auth_info_required
= "negotiate";
1469 else if (www_auth
[0])
1470 auth_info_required
= "username,password";
1474 else if (ipp_status
== IPP_STATUS_ERROR_OPERATION_NOT_SUPPORTED
)
1477 * This is all too common...
1480 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1481 "cups-ipp-missing-validate-job");
1484 else if (ipp_status
< IPP_REDIRECTION_OTHER_SITE
||
1485 ipp_status
== IPP_BAD_REQUEST
)
1490 * Then issue the print-job request...
1495 while (!job_canceled
&& copies_remaining
> 0)
1498 * Check for side-channel requests...
1501 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1504 * Build the IPP job creation request...
1510 request
= new_request((num_files
> 1 || create_job
) ? IPP_CREATE_JOB
:
1512 version
, uri
, argv
[2], monitor
.job_name
, num_options
,
1513 options
, compression
, copies_sup
? copies
: 1,
1514 document_format
, pc
, ppd
, media_col_sup
,
1515 doc_handling_sup
, print_color_mode_sup
);
1521 if (num_files
> 1 || create_job
)
1522 response
= cupsDoRequest(http
, request
, resource
);
1525 size_t length
= 0; /* Length of request */
1529 fputs("DEBUG: Sending file using HTTP/1.0 Content-Length...\n", stderr
);
1530 length
= ippLength(request
) + (size_t)compatsize
;
1533 fputs("DEBUG: Sending file using HTTP/1.1 chunking...\n", stderr
);
1535 http_status
= cupsSendRequest(http
, request
, resource
, length
);
1536 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
)
1538 if (compression
&& strcmp(compression
, "none"))
1539 httpSetField(http
, HTTP_FIELD_CONTENT_ENCODING
, compression
);
1543 if ((fd
= open(files
[0], O_RDONLY
)) < 0)
1545 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1546 return (CUPS_BACKEND_FAILED
);
1552 http_status
= cupsWriteRequestData(http
, buffer
, (size_t)bytes
);
1555 while (http_status
== HTTP_CONTINUE
&&
1556 (!job_canceled
|| compatsize
> 0))
1559 * Check for side-channel requests and more print data...
1564 FD_SET(snmp_fd
, &input
);
1566 while (select(fd
> snmp_fd
? fd
+ 1 : snmp_fd
+ 1, &input
, NULL
, NULL
,
1567 NULL
) <= 0 && !job_canceled
);
1569 if (FD_ISSET(snmp_fd
, &input
))
1570 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1572 if (FD_ISSET(fd
, &input
))
1574 if ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1576 fprintf(stderr
, "DEBUG: Read %d bytes...\n", (int)bytes
);
1578 if ((http_status
= cupsWriteRequestData(http
, buffer
, (size_t)bytes
))
1582 else if (bytes
== 0 || (errno
!= EINTR
&& errno
!= EAGAIN
))
1587 if (http_status
== HTTP_ERROR
)
1588 fprintf(stderr
, "DEBUG: Error writing document data for "
1589 "Print-Job: %s\n", strerror(httpError(http
)));
1595 response
= cupsGetResponse(http
, resource
);
1599 ipp_status
= cupsLastError();
1601 fprintf(stderr
, "DEBUG: %s: %s (%s)\n",
1602 (num_files
> 1 || create_job
) ? "Create-Job" : "Print-Job",
1603 ippErrorString(ipp_status
), cupsLastErrorString());
1605 if (ipp_status
> IPP_OK_CONFLICT
)
1612 if (ipp_status
== IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
||
1613 ipp_status
== IPP_STATUS_ERROR_NOT_POSSIBLE
||
1614 ipp_status
== IPP_STATUS_ERROR_BUSY
)
1616 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
1622 * We can't re-submit when we have no files to print, so exit
1623 * immediately with the right status code...
1629 else if (ipp_status
== IPP_STATUS_ERROR_JOB_CANCELED
||
1630 ipp_status
== IPP_STATUS_ERROR_NOT_AUTHORIZED
||
1631 ipp_status
== IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES
||
1632 ipp_status
== IPP_STATUS_ERROR_CUPS_ACCOUNT_INFO_NEEDED
||
1633 ipp_status
== IPP_STATUS_ERROR_CUPS_ACCOUNT_CLOSED
||
1634 ipp_status
== IPP_STATUS_ERROR_CUPS_ACCOUNT_LIMIT_REACHED
||
1635 ipp_status
== IPP_STATUS_ERROR_CUPS_ACCOUNT_AUTHORIZATION_FAILED
)
1640 * Update auth-info-required as needed...
1643 _cupsLangPrintFilter(stderr
, "ERROR",
1644 _("Print job was not accepted."));
1646 if (ipp_status
== IPP_STATUS_ERROR_FORBIDDEN
||
1647 ipp_status
== IPP_STATUS_ERROR_CUPS_AUTHENTICATION_CANCELED
)
1649 const char *www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
1650 /* WWW-Authenticate field value */
1652 if (!strncmp(www_auth
, "Negotiate", 9))
1653 auth_info_required
= "negotiate";
1654 else if (www_auth
[0])
1655 auth_info_required
= "username,password";
1657 else if (ipp_status
== IPP_REQUEST_VALUE
)
1660 * Print file is too large, abort this job...
1671 * We can't re-submit when we have no files to print, so exit
1672 * immediately with the right status code...
1679 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
1680 IPP_TAG_INTEGER
)) == NULL
)
1682 fputs("DEBUG: Print job accepted - job ID unknown.\n", stderr
);
1683 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1684 "cups-ipp-missing-job-id");
1690 monitor
.job_id
= job_id
= job_id_attr
->values
[0].integer
;
1691 fprintf(stderr
, "DEBUG: Print job accepted - job ID %d.\n", job_id
);
1694 ippDelete(response
);
1699 if (job_id
&& (num_files
> 1 || create_job
))
1701 for (i
= 0; num_files
== 0 || i
< num_files
; i
++)
1704 * Check for side-channel requests...
1707 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1710 * Send the next file in the job...
1713 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
1714 ippSetVersion(request
, version
/ 10, version
% 10);
1716 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1719 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1723 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1724 "requesting-user-name", NULL
, argv
[2]);
1726 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document",
1727 (i
+ 1) >= num_files
);
1729 if (document_format
)
1730 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1731 "document-format", NULL
, document_format
);
1734 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1735 "compression", NULL
, compression
);
1737 fprintf(stderr
, "DEBUG: Sending file %d using chunking...\n", i
+ 1);
1738 http_status
= cupsSendRequest(http
, request
, resource
, 0);
1739 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
)
1741 if (compression
&& strcmp(compression
, "none"))
1742 httpSetField(http
, HTTP_FIELD_CONTENT_ENCODING
, compression
);
1747 http_status
= cupsWriteRequestData(http
, buffer
, (size_t)bytes
);
1751 if ((fd
= open(files
[i
], O_RDONLY
)) < 0)
1753 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1754 return (CUPS_BACKEND_FAILED
);
1763 while (!job_canceled
&& http_status
== HTTP_CONTINUE
&&
1764 (bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1766 if ((http_status
= cupsWriteRequestData(http
, buffer
, (size_t)bytes
))
1772 * Check for side-channel requests...
1775 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1783 if (http_status
== HTTP_ERROR
)
1784 fprintf(stderr
, "DEBUG: Error writing document data for "
1785 "Send-Document: %s\n", strerror(httpError(http
)));
1787 ippDelete(cupsGetResponse(http
, resource
));
1790 fprintf(stderr
, "DEBUG: Send-Document: %s (%s)\n",
1791 ippErrorString(cupsLastError()), cupsLastErrorString());
1793 if (cupsLastError() > IPP_OK_CONFLICT
)
1795 ipp_status
= cupsLastError();
1797 _cupsLangPrintFilter(stderr
, "ERROR",
1798 _("Unable to add document to print job."));
1805 if (num_files
== 0 || fd
< 0)
1811 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
1813 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
1814 copies_remaining
--;
1816 else if ((ipp_status
== IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR
|| ipp_status
== IPP_STATUS_ERROR_DOCUMENT_UNPRINTABLE
) &&
1818 document_format
&& strcmp(document_format
, "image/pwg-raster") && strcmp(document_format
, "image/urf"))
1821 * Need to reprocess the job as raster...
1824 fputs("JOBSTATE: cups-retry-as-raster\n", stderr
);
1826 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1830 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1831 ipp_status
== IPP_NOT_POSSIBLE
||
1832 ipp_status
== IPP_PRINTER_BUSY
)
1837 * Need to reprocess the entire job; if we have a job ID, cancel the
1842 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1848 else if (ipp_status
== IPP_REQUEST_VALUE
||
1849 ipp_status
== IPP_ERROR_JOB_CANCELED
||
1850 ipp_status
== IPP_NOT_AUTHORIZED
||
1851 ipp_status
== IPP_STATUS_ERROR_CUPS_ACCOUNT_INFO_NEEDED
||
1852 ipp_status
== IPP_STATUS_ERROR_CUPS_ACCOUNT_CLOSED
||
1853 ipp_status
== IPP_STATUS_ERROR_CUPS_ACCOUNT_LIMIT_REACHED
||
1854 ipp_status
== IPP_STATUS_ERROR_CUPS_ACCOUNT_AUTHORIZATION_FAILED
||
1855 ipp_status
== IPP_INTERNAL_ERROR
)
1858 * Print file is too large, job was canceled, we need new
1859 * authentication data, or we had some sort of error...
1864 else if (ipp_status
== IPP_STATUS_ERROR_CUPS_UPGRADE_REQUIRED
)
1867 * Server is configured incorrectly; the policy for Create-Job and
1868 * Send-Document has to be the same (auth or no auth, encryption or
1869 * no encryption). Force the queue to stop since printing will never
1873 fputs("DEBUG: The server or printer is configured incorrectly.\n",
1875 fputs("DEBUG: The policy for Create-Job and Send-Document must have the "
1876 "same authentication and encryption requirements.\n", stderr
);
1878 ipp_status
= IPP_STATUS_ERROR_INTERNAL
;
1881 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1885 else if (ipp_status
== IPP_NOT_FOUND
)
1888 * Printer does not actually implement support for Create-Job/
1889 * Send-Document, so log the conformance issue and stop the printer.
1892 fputs("DEBUG: This printer claims to support Create-Job and "
1893 "Send-Document, but those operations failed.\n", stderr
);
1894 fputs("DEBUG: Add '?version=1.0' to the device URI to use legacy "
1895 "compatibility mode.\n", stderr
);
1896 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1897 "cups-ipp-missing-send-document");
1899 ipp_status
= IPP_INTERNAL_ERROR
; /* Force queue to stop */
1904 copies_remaining
--;
1907 * Wait for the job to complete...
1910 if (!job_id
|| !waitjob
|| !get_job_attrs
)
1913 fputs("STATE: +cups-waiting-for-job-completed\n", stderr
);
1915 _cupsLangPrintFilter(stderr
, "INFO", _("Waiting for job to complete."));
1917 for (delay
= _cupsNextDelay(0, &prev_delay
); !job_canceled
;)
1920 * Check for side-channel requests...
1923 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1926 * Check printer state...
1929 check_printer_state(http
, uri
, resource
, argv
[2], version
);
1931 if (cupsLastError() <= IPP_OK_CONFLICT
)
1935 * Build an IPP_GET_JOB_ATTRIBUTES request...
1938 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1939 ippSetVersion(request
, version
/ 10, version
% 10);
1941 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1944 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1948 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1949 "requesting-user-name", NULL
, argv
[2]);
1951 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1952 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
1959 httpReconnect(http
);
1960 response
= cupsDoRequest(http
, request
, resource
);
1961 ipp_status
= cupsLastError();
1963 if (ipp_status
== IPP_NOT_FOUND
|| ipp_status
== IPP_NOT_POSSIBLE
)
1966 * Job has gone away and/or the server has no job history...
1969 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1970 "cups-ipp-missing-job-history");
1971 ippDelete(response
);
1973 ipp_status
= IPP_OK
;
1977 fprintf(stderr
, "DEBUG: Get-Job-Attributes: %s (%s)\n",
1978 ippErrorString(ipp_status
), cupsLastErrorString());
1980 if (ipp_status
<= IPP_OK_CONFLICT
)
1984 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1985 ipp_status
!= IPP_PRINTER_BUSY
)
1987 ippDelete(response
);
1988 ipp_status
= IPP_OK
;
1991 else if (ipp_status
== IPP_INTERNAL_ERROR
)
1995 if (waitjob_tries
> 4)
1997 ippDelete(response
);
1998 ipp_status
= IPP_OK
;
2006 if ((job_state
= ippFindAttribute(response
, "job-state",
2007 IPP_TAG_ENUM
)) != NULL
)
2010 * Reflect the remote job state in the local queue...
2014 job_state
->values
[0].integer
>= IPP_JOB_PENDING
&&
2015 job_state
->values
[0].integer
<= IPP_JOB_COMPLETED
)
2016 update_reasons(NULL
,
2017 remote_job_states
[job_state
->values
[0].integer
-
2020 if ((job_sheets
= ippFindAttribute(response
,
2021 "job-media-sheets-completed",
2022 IPP_TAG_INTEGER
)) == NULL
)
2023 job_sheets
= ippFindAttribute(response
,
2024 "job-impressions-completed",
2028 fprintf(stderr
, "PAGE: total %d\n",
2029 job_sheets
->values
[0].integer
);
2032 * Stop polling if the job is finished or pending-held...
2035 if (job_state
->values
[0].integer
> IPP_JOB_STOPPED
)
2037 ippDelete(response
);
2041 else if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
2042 ipp_status
!= IPP_NOT_POSSIBLE
&&
2043 ipp_status
!= IPP_PRINTER_BUSY
)
2046 * If the printer does not return a job-state attribute, it does not
2047 * conform to the IPP specification - break out immediately and fail
2051 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
2052 "cups-ipp-missing-job-state");
2053 ipp_status
= IPP_INTERNAL_ERROR
;
2058 ippDelete(response
);
2061 * Wait before polling again...
2064 sleep((unsigned)delay
);
2066 delay
= _cupsNextDelay(delay
, &prev_delay
);
2071 * Cancel the job as needed...
2074 if (job_canceled
> 0 && job_id
> 0)
2076 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
2078 if (cupsLastError() > IPP_OK_CONFLICT
)
2079 _cupsLangPrintFilter(stderr
, "ERROR", _("Unable to cancel print job."));
2083 * Check the printer state and report it if necessary...
2086 check_printer_state(http
, uri
, resource
, argv
[2], version
);
2088 if (cupsLastError() <= IPP_OK_CONFLICT
)
2092 * Collect the final page count as needed...
2095 if (have_supplies
&&
2096 !backendSNMPSupplies(snmp_fd
, &(http
->addrlist
->addr
), &page_count
,
2098 page_count
> start_count
)
2099 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
2103 * See if we used Kerberos at all...
2107 auth_info_required
= "negotiate";
2108 #endif /* HAVE_GSSAPI */
2116 cupsFreeOptions(num_options
, options
);
2117 _ppdCacheDestroy(pc
);
2122 ippDelete(supported
);
2125 * Remove the temporary file(s) if necessary...
2129 unlink(tmpfilename
);
2132 * Return the queue status...
2135 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
2136 ipp_status
== IPP_AUTHENTICATION_CANCELED
||
2137 ipp_status
<= IPP_OK_CONFLICT
)
2138 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
2140 if (ipp_status
== IPP_STATUS_ERROR_CUPS_ACCOUNT_INFO_NEEDED
)
2141 fputs("JOBSTATE: account-info-needed\n", stderr
);
2142 else if (ipp_status
== IPP_STATUS_ERROR_CUPS_ACCOUNT_CLOSED
)
2143 fputs("JOBSTATE: account-closed\n", stderr
);
2144 else if (ipp_status
== IPP_STATUS_ERROR_CUPS_ACCOUNT_LIMIT_REACHED
)
2145 fputs("JOBSTATE: account-limit-reached\n", stderr
);
2146 else if (ipp_status
== IPP_STATUS_ERROR_CUPS_ACCOUNT_AUTHORIZATION_FAILED
)
2147 fputs("JOBSTATE: account-authorization-failed\n", stderr
);
2149 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
2150 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
2151 return (CUPS_BACKEND_AUTH_REQUIRED
);
2152 else if (ipp_status
== IPP_STATUS_ERROR_CUPS_ACCOUNT_LIMIT_REACHED
||
2153 ipp_status
== IPP_STATUS_ERROR_CUPS_ACCOUNT_INFO_NEEDED
||
2154 ipp_status
== IPP_STATUS_ERROR_CUPS_ACCOUNT_CLOSED
||
2155 ipp_status
== IPP_STATUS_ERROR_CUPS_ACCOUNT_AUTHORIZATION_FAILED
)
2156 return (CUPS_BACKEND_HOLD
);
2157 else if (ipp_status
== IPP_INTERNAL_ERROR
)
2158 return (CUPS_BACKEND_STOP
);
2159 else if (ipp_status
== IPP_CONFLICT
)
2160 return (CUPS_BACKEND_FAILED
);
2161 else if (ipp_status
== IPP_REQUEST_VALUE
||
2162 ipp_status
== IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES
||
2163 ipp_status
== IPP_DOCUMENT_FORMAT
|| job_canceled
< 0)
2165 if (ipp_status
== IPP_REQUEST_VALUE
)
2166 _cupsLangPrintFilter(stderr
, "ERROR", _("Print job too large."));
2167 else if (ipp_status
== IPP_DOCUMENT_FORMAT
)
2168 _cupsLangPrintFilter(stderr
, "ERROR",
2169 _("Printer cannot print supplied content."));
2170 else if (ipp_status
== IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES
)
2171 _cupsLangPrintFilter(stderr
, "ERROR",
2172 _("Printer cannot print with supplied options."));
2174 _cupsLangPrintFilter(stderr
, "ERROR", _("Print job canceled at printer."));
2176 return (CUPS_BACKEND_CANCEL
);
2178 else if (ipp_status
> IPP_OK_CONFLICT
&& ipp_status
!= IPP_ERROR_JOB_CANCELED
)
2179 return (CUPS_BACKEND_RETRY_CURRENT
);
2181 return (CUPS_BACKEND_OK
);
2186 * 'cancel_job()' - Cancel a print job.
2190 cancel_job(http_t
*http
, /* I - HTTP connection */
2191 const char *uri
, /* I - printer-uri */
2192 int id
, /* I - job-id */
2193 const char *resource
, /* I - Resource path */
2194 const char *user
, /* I - requesting-user-name */
2195 int version
) /* I - IPP version */
2197 ipp_t
*request
; /* Cancel-Job request */
2200 _cupsLangPrintFilter(stderr
, "INFO", _("Canceling print job."));
2202 request
= ippNewRequest(IPP_CANCEL_JOB
);
2203 ippSetVersion(request
, version
/ 10, version
% 10);
2205 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2207 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
2209 if (user
&& user
[0])
2210 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2211 "requesting-user-name", NULL
, user
);
2217 ippDelete(cupsDoRequest(http
, request
, resource
));
2222 * 'check_printer_state()' - Check the printer state.
2225 static ipp_pstate_t
/* O - Current printer-state */
2226 check_printer_state(
2227 http_t
*http
, /* I - HTTP connection */
2228 const char *uri
, /* I - Printer URI */
2229 const char *resource
, /* I - Resource path */
2230 const char *user
, /* I - Username, if any */
2231 int version
) /* I - IPP version */
2233 ipp_t
*request
, /* IPP request */
2234 *response
; /* IPP response */
2235 ipp_attribute_t
*attr
; /* Attribute in response */
2236 ipp_pstate_t printer_state
= IPP_PRINTER_STOPPED
;
2237 /* Current printer-state */
2241 * Send a Get-Printer-Attributes request and log the results...
2244 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
2245 ippSetVersion(request
, version
/ 10, version
% 10);
2247 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2250 if (user
&& user
[0])
2251 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2252 "requesting-user-name", NULL
, user
);
2254 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2255 "requested-attributes",
2256 (int)(sizeof(pattrs
) / sizeof(pattrs
[0])), NULL
, pattrs
);
2258 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
2260 report_printer_state(response
);
2262 if ((attr
= ippFindAttribute(response
, "printer-state",
2263 IPP_TAG_ENUM
)) != NULL
)
2264 printer_state
= (ipp_pstate_t
)attr
->values
[0].integer
;
2266 ippDelete(response
);
2269 fprintf(stderr
, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
2270 ippErrorString(cupsLastError()), cupsLastErrorString());
2273 * Return the printer-state value...
2276 return (printer_state
);
2281 * 'monitor_printer()' - Monitor the printer state.
2284 static void * /* O - Thread exit code */
2286 _cups_monitor_t
*monitor
) /* I - Monitoring data */
2288 http_t
*http
; /* Connection to printer */
2289 ipp_t
*request
, /* IPP request */
2290 *response
; /* IPP response */
2291 ipp_attribute_t
*attr
; /* Attribute in response */
2292 int delay
, /* Current delay */
2293 prev_delay
; /* Previous delay */
2294 ipp_op_t job_op
; /* Operation to use */
2295 int job_id
; /* Job ID */
2296 const char *job_name
; /* Job name */
2297 ipp_jstate_t job_state
; /* Job state */
2298 const char *job_user
; /* Job originating user name */
2299 int password_tries
= 0; /* Password tries */
2303 * Make a copy of the printer connection...
2306 http
= httpConnect2(monitor
->hostname
, monitor
->port
, NULL
, AF_UNSPEC
,
2307 monitor
->encryption
, 1, 0, NULL
);
2308 httpSetTimeout(http
, 30.0, timeout_cb
, NULL
);
2310 cupsSetUser(username
);
2312 cupsSetPasswordCB2((cups_password_cb2_t
)password_cb
, &password_tries
);
2315 * Loop until the job is canceled, aborted, or completed.
2318 delay
= _cupsNextDelay(0, &prev_delay
);
2320 monitor
->job_reasons
= 0;
2322 while (monitor
->job_state
< IPP_JOB_CANCELED
&& !job_canceled
)
2325 * Reconnect to the printer...
2328 if (!httpReconnect(http
))
2331 * Connected, so check on the printer state...
2334 monitor
->printer_state
= check_printer_state(http
, monitor
->uri
,
2338 if (cupsLastError() <= IPP_OK_CONFLICT
)
2341 if (monitor
->job_id
== 0 && monitor
->create_job
)
2344 * No job-id yet, so continue...
2347 goto monitor_disconnect
;
2351 * Check the status of the job itself...
2354 job_op
= (monitor
->job_id
> 0 && monitor
->get_job_attrs
) ?
2355 IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
;
2356 request
= ippNewRequest(job_op
);
2357 ippSetVersion(request
, monitor
->version
/ 10, monitor
->version
% 10);
2359 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2360 NULL
, monitor
->uri
);
2361 if (job_op
== IPP_GET_JOB_ATTRIBUTES
)
2362 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
2365 if (monitor
->user
&& monitor
->user
[0])
2366 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2367 "requesting-user-name", NULL
, monitor
->user
);
2369 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2370 "requested-attributes",
2371 (int)(sizeof(jattrs
) / sizeof(jattrs
[0])), NULL
, jattrs
);
2377 response
= cupsDoRequest(http
, request
, monitor
->resource
);
2379 fprintf(stderr
, "DEBUG: (monitor) %s: %s (%s)\n", ippOpString(job_op
),
2380 ippErrorString(cupsLastError()), cupsLastErrorString());
2382 if (cupsLastError() <= IPP_OK_CONFLICT
)
2385 if (job_op
== IPP_GET_JOB_ATTRIBUTES
)
2387 if ((attr
= ippFindAttribute(response
, "job-state",
2388 IPP_TAG_ENUM
)) != NULL
)
2389 monitor
->job_state
= (ipp_jstate_t
)attr
->values
[0].integer
;
2391 monitor
->job_state
= IPP_JOB_COMPLETED
;
2395 for (attr
= response
->attrs
; attr
; attr
= attr
->next
)
2399 job_state
= IPP_JOB_PENDING
;
2402 while (attr
&& attr
->group_tag
!= IPP_TAG_JOB
)
2408 while (attr
&& attr
->group_tag
== IPP_TAG_JOB
)
2410 if (!strcmp(attr
->name
, "job-id") &&
2411 attr
->value_tag
== IPP_TAG_INTEGER
)
2412 job_id
= attr
->values
[0].integer
;
2413 else if (!strcmp(attr
->name
, "job-name") &&
2414 (attr
->value_tag
== IPP_TAG_NAME
||
2415 attr
->value_tag
== IPP_TAG_NAMELANG
))
2416 job_name
= attr
->values
[0].string
.text
;
2417 else if (!strcmp(attr
->name
, "job-state") &&
2418 attr
->value_tag
== IPP_TAG_ENUM
)
2419 job_state
= (ipp_jstate_t
)attr
->values
[0].integer
;
2420 else if (!strcmp(attr
->name
, "job-originating-user-name") &&
2421 (attr
->value_tag
== IPP_TAG_NAME
||
2422 attr
->value_tag
== IPP_TAG_NAMELANG
))
2423 job_user
= attr
->values
[0].string
.text
;
2428 if (job_id
> 0 && job_name
&& !strcmp(job_name
, monitor
->job_name
) &&
2429 job_user
&& monitor
->user
&& !strcmp(job_user
, monitor
->user
))
2431 monitor
->job_id
= job_id
;
2432 monitor
->job_state
= job_state
;
2441 if ((attr
= ippFindAttribute(response
, "job-state-reasons",
2442 IPP_TAG_KEYWORD
)) != NULL
)
2444 int i
, new_reasons
= 0; /* Looping var, new reasons */
2446 for (i
= 0; i
< attr
->num_values
; i
++)
2448 if (!strcmp(attr
->values
[i
].string
.text
,
2449 "account-authorization-failed"))
2450 new_reasons
|= _CUPS_JSR_ACCOUNT_AUTHORIZATION_FAILED
;
2451 else if (!strcmp(attr
->values
[i
].string
.text
, "account-closed"))
2452 new_reasons
|= _CUPS_JSR_ACCOUNT_CLOSED
;
2453 else if (!strcmp(attr
->values
[i
].string
.text
, "account-info-needed"))
2454 new_reasons
|= _CUPS_JSR_ACCOUNT_INFO_NEEDED
;
2455 else if (!strcmp(attr
->values
[i
].string
.text
,
2456 "account-limit-reached"))
2457 new_reasons
|= _CUPS_JSR_ACCOUNT_LIMIT_REACHED
;
2458 else if (!strcmp(attr
->values
[i
].string
.text
, "job-password-wait"))
2459 new_reasons
|= _CUPS_JSR_JOB_PASSWORD_WAIT
;
2460 else if (!strcmp(attr
->values
[i
].string
.text
, "job-release-wait"))
2461 new_reasons
|= _CUPS_JSR_JOB_RELEASE_WAIT
;
2464 if (new_reasons
!= monitor
->job_reasons
)
2466 if (new_reasons
& _CUPS_JSR_ACCOUNT_AUTHORIZATION_FAILED
)
2467 fputs("JOBSTATE: account-authorization-failed\n", stderr
);
2468 else if (new_reasons
& _CUPS_JSR_ACCOUNT_CLOSED
)
2469 fputs("JOBSTATE: account-closed\n", stderr
);
2470 else if (new_reasons
& _CUPS_JSR_ACCOUNT_INFO_NEEDED
)
2471 fputs("JOBSTATE: account-info-needed\n", stderr
);
2472 else if (new_reasons
& _CUPS_JSR_ACCOUNT_LIMIT_REACHED
)
2473 fputs("JOBSTATE: account-limit-reached\n", stderr
);
2474 else if (new_reasons
& _CUPS_JSR_JOB_PASSWORD_WAIT
)
2475 fputs("JOBSTATE: job-password-wait\n", stderr
);
2476 else if (new_reasons
& _CUPS_JSR_JOB_RELEASE_WAIT
)
2477 fputs("JOBSTATE: job-release-wait\n", stderr
);
2479 fputs("JOBSTATE: job-printing\n", stderr
);
2481 monitor
->job_reasons
= new_reasons
;
2485 ippDelete(response
);
2487 fprintf(stderr
, "DEBUG: (monitor) job-state=%s\n",
2488 ippEnumString("job-state", monitor
->job_state
));
2490 if (!job_canceled
&&
2491 (monitor
->job_state
== IPP_JOB_CANCELED
||
2492 monitor
->job_state
== IPP_JOB_ABORTED
))
2496 * Disconnect from the printer - we'll reconnect on the next poll...
2501 _httpDisconnect(http
);
2505 * Sleep for N seconds...
2508 sleep((unsigned)delay
);
2510 delay
= _cupsNextDelay(delay
, &prev_delay
);
2514 * Cancel the job if necessary...
2517 if (job_canceled
> 0 && monitor
->job_id
> 0)
2519 if (!httpReconnect(http
))
2521 cancel_job(http
, monitor
->uri
, monitor
->job_id
, monitor
->resource
,
2522 monitor
->user
, monitor
->version
);
2524 if (cupsLastError() > IPP_OK_CONFLICT
)
2525 _cupsLangPrintFilter(stderr
, "ERROR", _("Unable to cancel print job."));
2530 * Cleanup and return...
2540 * 'new_request()' - Create a new print creation or validation request.
2543 static ipp_t
* /* O - Request data */
2545 ipp_op_t op
, /* I - IPP operation code */
2546 int version
, /* I - IPP version number */
2547 const char *uri
, /* I - printer-uri value */
2548 const char *user
, /* I - requesting-user-name value */
2549 const char *title
, /* I - job-name value */
2550 int num_options
, /* I - Number of options to send */
2551 cups_option_t
*options
, /* I - Options to send */
2552 const char *compression
, /* I - compression value or NULL */
2553 int copies
, /* I - copies value or 0 */
2554 const char *format
, /* I - document-format value or NULL */
2555 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2556 ppd_file_t
*ppd
, /* I - PPD file data */
2557 ipp_attribute_t
*media_col_sup
, /* I - media-col-supported values */
2558 ipp_attribute_t
*doc_handling_sup
, /* I - multiple-document-handling-supported values */
2559 ipp_attribute_t
*print_color_mode_sup
)
2560 /* I - Printer supports print-color-mode */
2562 int i
; /* Looping var */
2563 ipp_t
*request
; /* Request data */
2564 const char *keyword
; /* PWG keyword */
2565 _pwg_size_t
*size
; /* PWG media size */
2566 ipp_t
*media_col
, /* media-col value */
2567 *media_size
; /* media-size value */
2568 const char *media_source
, /* media-source value */
2569 *media_type
, /* media-type value */
2570 *collate_str
, /* multiple-document-handling value */
2571 *mandatory
; /* Mandatory attributes */
2572 ipp_tag_t group
; /* Current group */
2573 ipp_attribute_t
*attr
; /* Current attribute */
2574 const char *color_attr_name
; /* Supported color attribute */
2575 char buffer
[1024]; /* Value buffer */
2579 * Create the IPP request...
2582 request
= ippNewRequest(op
);
2583 ippSetVersion(request
, version
/ 10, version
% 10);
2585 fprintf(stderr
, "DEBUG: %s IPP/%d.%d\n",
2586 ippOpString(request
->request
.op
.operation_id
),
2587 request
->request
.op
.version
[0],
2588 request
->request
.op
.version
[1]);
2591 * Add standard attributes...
2594 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2596 fprintf(stderr
, "DEBUG: printer-uri=\"%s\"\n", uri
);
2600 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2601 "requesting-user-name", NULL
, user
);
2602 fprintf(stderr
, "DEBUG: requesting-user-name=\"%s\"\n", user
);
2605 if (title
&& *title
)
2607 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
2609 fprintf(stderr
, "DEBUG: job-name=\"%s\"\n", title
);
2612 if (format
&& op
!= IPP_CREATE_JOB
)
2614 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
2615 "document-format", NULL
, format
);
2616 fprintf(stderr
, "DEBUG: document-format=\"%s\"\n", format
);
2620 if (compression
&& op
!= IPP_OP_CREATE_JOB
&& op
!= IPP_OP_VALIDATE_JOB
)
2622 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2623 "compression", NULL
, compression
);
2624 fprintf(stderr
, "DEBUG: compression=\"%s\"\n", compression
);
2626 #endif /* HAVE_LIBZ */
2629 * Handle options on the command-line...
2632 if (num_options
> 0)
2636 int num_finishings
= 0, /* Number of finishing values */
2637 finishings
[10]; /* Finishing enum values */
2638 ppd_choice_t
*choice
; /* Marked choice */
2641 * Send standard IPP attributes...
2644 fputs("DEBUG: Adding standard IPP operation/job attributes.\n", stderr
);
2647 (keyword
= cupsGetOption("job-password", num_options
,
2650 ippAddOctetString(request
, IPP_TAG_OPERATION
, "job-password", keyword
, (int)strlen(keyword
));
2652 if ((keyword
= cupsGetOption("job-password-encryption", num_options
,
2656 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2657 "job-password-encryption", NULL
, keyword
);
2662 if ((keyword
= cupsGetOption("job-account-id", num_options
,
2664 keyword
= cupsGetOption("job-billing", num_options
, options
);
2667 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-account-id",
2671 if (pc
->accounting_user_id
)
2673 if ((keyword
= cupsGetOption("job-accounting-user-id", num_options
,
2678 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_NAME
,
2679 "job-accounting-user-id", NULL
, keyword
);
2682 for (mandatory
= (char *)cupsArrayFirst(pc
->mandatory
);
2684 mandatory
= (char *)cupsArrayNext(pc
->mandatory
))
2686 if (strcmp(mandatory
, "copies") &&
2687 strcmp(mandatory
, "destination-uris") &&
2688 strcmp(mandatory
, "finishings") &&
2689 strcmp(mandatory
, "job-account-id") &&
2690 strcmp(mandatory
, "job-accounting-user-id") &&
2691 strcmp(mandatory
, "job-password") &&
2692 strcmp(mandatory
, "job-password-encryption") &&
2693 strcmp(mandatory
, "media") &&
2694 strncmp(mandatory
, "media-col", 9) &&
2695 strcmp(mandatory
, "multiple-document-handling") &&
2696 strcmp(mandatory
, "output-bin") &&
2697 strcmp(mandatory
, "print-color-mode") &&
2698 strcmp(mandatory
, "print-quality") &&
2699 strcmp(mandatory
, "sides") &&
2700 (keyword
= cupsGetOption(mandatory
, num_options
, options
)) != NULL
)
2702 _ipp_option_t
*opt
= _ippFindOption(mandatory
);
2704 ipp_tag_t value_tag
= opt
? opt
->value_tag
: IPP_TAG_NAME
;
2709 case IPP_TAG_INTEGER
:
2711 ippAddInteger(request
, IPP_TAG_JOB
, value_tag
, mandatory
,
2714 case IPP_TAG_BOOLEAN
:
2715 ippAddBoolean(request
, IPP_TAG_JOB
, mandatory
,
2716 !_cups_strcasecmp(keyword
, "true"));
2718 case IPP_TAG_RANGE
:
2720 int lower
, upper
; /* Range */
2722 if (sscanf(keyword
, "%d-%d", &lower
, &upper
) != 2)
2723 lower
= upper
= atoi(keyword
);
2725 ippAddRange(request
, IPP_TAG_JOB
, mandatory
, lower
, upper
);
2728 case IPP_TAG_STRING
:
2729 ippAddOctetString(request
, IPP_TAG_JOB
, mandatory
, keyword
, (int)strlen(keyword
));
2732 if (!strcmp(mandatory
, "print-color-mode") && !strcmp(keyword
, "monochrome"))
2734 if (ippContainsString(print_color_mode_sup
, "auto-monochrome"))
2735 keyword
= "auto-monochrome";
2736 else if (ippContainsString(print_color_mode_sup
, "process-monochrome") && !ippContainsString(print_color_mode_sup
, "monochrome"))
2737 keyword
= "process-monochrome";
2740 ippAddString(request
, IPP_TAG_JOB
, value_tag
, mandatory
,
2747 if ((keyword
= cupsGetOption("PageSize", num_options
, options
)) == NULL
)
2748 keyword
= cupsGetOption("media", num_options
, options
);
2750 if ((size
= _ppdCacheGetSize(pc
, keyword
)) != NULL
)
2753 * Add a media-col value...
2756 media_size
= ippNew();
2757 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2758 "x-dimension", size
->width
);
2759 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2760 "y-dimension", size
->length
);
2762 media_col
= ippNew();
2763 ippAddCollection(media_col
, IPP_TAG_ZERO
, "media-size", media_size
);
2765 media_source
= _ppdCacheGetSource(pc
, cupsGetOption("InputSlot",
2768 media_type
= _ppdCacheGetType(pc
, cupsGetOption("MediaType",
2772 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
2774 if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2775 "media-left-margin"))
2776 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2777 "media-left-margin", size
->left
);
2778 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2779 "media-bottom-margin"))
2780 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2781 "media-bottom-margin", size
->bottom
);
2782 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2783 "media-right-margin"))
2784 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2785 "media-right-margin", size
->right
);
2786 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2787 "media-top-margin"))
2788 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2789 "media-top-margin", size
->top
);
2790 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2791 "media-source") && media_source
)
2792 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
2793 "media-source", NULL
, media_source
);
2794 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2795 "media-type") && media_type
)
2796 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
2797 "media-type", NULL
, media_type
);
2800 ippAddCollection(request
, IPP_TAG_JOB
, "media-col", media_col
);
2803 if ((keyword
= cupsGetOption("output-bin", num_options
,
2806 if ((choice
= ppdFindMarkedChoice(ppd
, "OutputBin")) != NULL
)
2807 keyword
= _ppdCacheGetBin(pc
, choice
->choice
);
2811 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-bin",
2814 color_attr_name
= print_color_mode_sup
? "print-color-mode" : "output-mode";
2816 if ((keyword
= cupsGetOption("print-color-mode", num_options
,
2819 if ((choice
= ppdFindMarkedChoice(ppd
, "ColorModel")) != NULL
)
2821 if (!_cups_strcasecmp(choice
->choice
, "Gray"))
2822 keyword
= "monochrome";
2828 if (keyword
&& !strcmp(keyword
, "monochrome"))
2830 if (ippContainsString(print_color_mode_sup
, "auto-monochrome"))
2831 keyword
= "auto-monochrome";
2832 else if (ippContainsString(print_color_mode_sup
, "process-monochrome") && !ippContainsString(print_color_mode_sup
, "monochrome"))
2833 keyword
= "process-monochrome";
2837 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, color_attr_name
,
2840 if ((keyword
= cupsGetOption("print-quality", num_options
,
2842 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2844 else if ((choice
= ppdFindMarkedChoice(ppd
, "cupsPrintQuality")) != NULL
)
2846 if (!_cups_strcasecmp(choice
->choice
, "draft"))
2847 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2849 else if (!_cups_strcasecmp(choice
->choice
, "normal"))
2850 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2851 IPP_QUALITY_NORMAL
);
2852 else if (!_cups_strcasecmp(choice
->choice
, "high"))
2853 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2857 if ((keyword
= cupsGetOption("sides", num_options
, options
)) != NULL
)
2858 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2860 else if (pc
->sides_option
&&
2861 (choice
= ppdFindMarkedChoice(ppd
, pc
->sides_option
)) != NULL
)
2863 if (!_cups_strcasecmp(choice
->choice
, pc
->sides_1sided
))
2864 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2866 else if (!_cups_strcasecmp(choice
->choice
, pc
->sides_2sided_long
))
2867 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2868 NULL
, "two-sided-long-edge");
2869 if (!_cups_strcasecmp(choice
->choice
, pc
->sides_2sided_short
))
2870 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2871 NULL
, "two-sided-short-edge");
2874 if ((keyword
= cupsGetOption("multiple-document-handling",
2875 num_options
, options
)) != NULL
)
2877 if (strstr(keyword
, "uncollated"))
2882 else if ((keyword
= cupsGetOption("collate", num_options
,
2888 if (!_cups_strcasecmp(format
, "image/gif") ||
2889 !_cups_strcasecmp(format
, "image/jp2") ||
2890 !_cups_strcasecmp(format
, "image/jpeg") ||
2891 !_cups_strcasecmp(format
, "image/png") ||
2892 !_cups_strcasecmp(format
, "image/tiff") ||
2893 !_cups_strncasecmp(format
, "image/x-", 8))
2896 * Collation makes no sense for single page image formats...
2901 else if (!_cups_strncasecmp(format
, "image/", 6) ||
2902 !_cups_strcasecmp(format
, "application/vnd.cups-raster"))
2905 * Multi-page image formats will have copies applied by the upstream
2913 if (doc_handling_sup
)
2915 if (!_cups_strcasecmp(keyword
, "true"))
2916 collate_str
= "separate-documents-collated-copies";
2918 collate_str
= "separate-documents-uncollated-copies";
2920 for (i
= 0; i
< doc_handling_sup
->num_values
; i
++)
2921 if (!strcmp(doc_handling_sup
->values
[i
].string
.text
, collate_str
))
2923 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2924 "multiple-document-handling", NULL
, collate_str
);
2928 if (i
>= doc_handling_sup
->num_values
)
2933 * Map finishing options...
2936 num_finishings
= _ppdCacheGetFinishingValues(pc
, num_options
, options
,
2937 (int)(sizeof(finishings
) /
2938 sizeof(finishings
[0])),
2940 if (num_finishings
> 0)
2941 ippAddIntegers(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "finishings",
2942 num_finishings
, finishings
);
2945 * Map FaxOut options...
2948 if ((keyword
= cupsGetOption("phone", num_options
, options
)) != NULL
)
2950 ipp_t
*destination
; /* destination collection */
2951 char phone
[1024], /* Phone number string */
2952 *ptr
, /* Pointer into string */
2953 tel_uri
[1024]; /* tel: URI */
2954 static const char * const allowed
= "0123456789#*-+.()";
2955 /* Allowed characters */
2957 destination
= ippNew();
2960 * Unescape and filter out spaces and other characters that are not
2961 * allowed in a tel: URI.
2964 _httpDecodeURI(phone
, keyword
, sizeof(phone
));
2965 for (ptr
= phone
; *ptr
;)
2967 if (!strchr(allowed
, *ptr
))
2968 _cups_strcpy(ptr
, ptr
+ 1);
2973 httpAssembleURI(HTTP_URI_CODING_ALL
, tel_uri
, sizeof(tel_uri
), "tel", NULL
, NULL
, 0, phone
);
2974 ippAddString(destination
, IPP_TAG_JOB
, IPP_TAG_URI
, "destination-uri", NULL
, tel_uri
);
2976 if ((keyword
= cupsGetOption("faxPrefix", num_options
,
2977 options
)) != NULL
&& *keyword
)
2979 char predial
[1024]; /* Pre-dial string */
2981 _httpDecodeURI(predial
, keyword
, sizeof(predial
));
2982 ippAddString(destination
, IPP_TAG_JOB
, IPP_TAG_TEXT
, "pre-dial-string", NULL
, predial
);
2985 ippAddCollection(request
, IPP_TAG_JOB
, "destination-uris", destination
);
2986 ippDelete(destination
);
2992 * When talking to another CUPS server, send all options...
2995 fputs("DEBUG: Adding all operation/job attributes.\n", stderr
);
2996 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_OPERATION
);
2997 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_JOB
);
3000 if (copies
> 1 && (!pc
|| copies
<= pc
->max_copies
))
3001 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies", copies
);
3004 fprintf(stderr
, "DEBUG: IPP/%d.%d %s #%d\n", version
/ 10, version
% 10,
3005 ippOpString(ippGetOperation(request
)), ippGetRequestId(request
));
3006 for (group
= IPP_TAG_ZERO
, attr
= ippFirstAttribute(request
);
3008 attr
= ippNextAttribute(request
))
3010 const char *name
= ippGetName(attr
);
3014 group
= IPP_TAG_ZERO
;
3018 if (group
!= ippGetGroupTag(attr
))
3020 group
= ippGetGroupTag(attr
);
3021 fprintf(stderr
, "DEBUG: ---- %s ----\n", ippTagString(group
));
3024 ippAttributeString(attr
, buffer
, sizeof(buffer
));
3025 fprintf(stderr
, "DEBUG: %s %s%s %s\n", name
,
3026 ippGetCount(attr
) > 1 ? "1setOf " : "",
3027 ippTagString(ippGetValueTag(attr
)), buffer
);
3030 fprintf(stderr
, "DEBUG: ---- %s ----\n", ippTagString(IPP_TAG_END
));
3037 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
3040 static const char * /* O - Password */
3041 password_cb(const char *prompt
, /* I - Prompt (not used) */
3042 http_t
*http
, /* I - Connection */
3043 const char *method
, /* I - Request method (not used) */
3044 const char *resource
, /* I - Resource path (not used) */
3045 int *password_tries
) /* I - Password tries */
3047 char def_username
[HTTP_MAX_VALUE
]; /* Default username */
3050 fprintf(stderr
, "DEBUG: password_cb(prompt=\"%s\", http=%p, method=\"%s\", "
3051 "resource=\"%s\", password_tries=%p(%d)), password=%p\n",
3052 prompt
, http
, method
, resource
, password_tries
, *password_tries
,
3059 if (!uri_credentials
)
3062 * Remember that we need to authenticate...
3065 auth_info_required
= "username,password";
3067 if (httpGetSubField(http
, HTTP_FIELD_WWW_AUTHENTICATE
, "username",
3070 char quoted
[HTTP_MAX_VALUE
* 2 + 4];
3073 fprintf(stderr
, "ATTR: auth-info-default=%s,\n",
3074 quote_string(def_username
, quoted
, sizeof(quoted
)));
3078 if (password
&& *password
&& *password_tries
< 3)
3080 (*password_tries
) ++;
3082 cupsSetUser(username
);
3089 * Give up after 3 tries or if we don't have a password to begin with...
3098 * 'quote_string()' - Quote a string value.
3101 static const char * /* O - Quoted string */
3102 quote_string(const char *s
, /* I - String */
3103 char *q
, /* I - Quoted string buffer */
3104 size_t qsize
) /* I - Size of quoted string buffer */
3106 char *qptr
, /* Pointer into string buffer */
3107 *qend
; /* End of string buffer */
3111 qend
= q
+ qsize
- 5;
3122 while (*s
&& qptr
< qend
)
3124 if (*s
== '\\' || *s
== '\"' || *s
== '\'')
3126 if (qptr
< (qend
- 4))
3148 * 'report_attr()' - Report an IPP attribute value.
3152 report_attr(ipp_attribute_t
*attr
) /* I - Attribute */
3154 int i
; /* Looping var */
3155 char value
[1024], /* Value string */
3156 *valptr
; /* Pointer into value string */
3157 const char *cached
; /* Cached attribute */
3161 * Convert the attribute values into quoted strings...
3164 for (i
= 0, valptr
= value
;
3165 i
< attr
->num_values
&& valptr
< (value
+ sizeof(value
) - 10);
3171 switch (attr
->value_tag
)
3173 case IPP_TAG_INTEGER
:
3175 snprintf(valptr
, sizeof(value
) - (size_t)(valptr
- value
), "%d", attr
->values
[i
].integer
);
3176 valptr
+= strlen(valptr
);
3181 case IPP_TAG_KEYWORD
:
3182 quote_string(attr
->values
[i
].string
.text
, valptr
, (size_t)(value
+ sizeof(value
) - valptr
));
3183 valptr
+= strlen(valptr
);
3188 * Unsupported value type...
3197 _cupsMutexLock(&report_mutex
);
3199 if ((cached
= cupsGetOption(attr
->name
, num_attr_cache
,
3200 attr_cache
)) == NULL
|| strcmp(cached
, value
))
3203 * Tell the scheduler about the new values...
3206 num_attr_cache
= cupsAddOption(attr
->name
, value
, num_attr_cache
,
3208 fprintf(stderr
, "ATTR: %s=%s\n", attr
->name
, value
);
3211 _cupsMutexUnlock(&report_mutex
);
3216 * 'report_printer_state()' - Report the printer state.
3220 report_printer_state(ipp_t
*ipp
) /* I - IPP response */
3222 ipp_attribute_t
*pa
, /* printer-alert */
3223 *pam
, /* printer-alert-message */
3224 *psm
, /* printer-state-message */
3225 *reasons
, /* printer-state-reasons */
3226 *marker
; /* marker-* attributes */
3227 char value
[1024], /* State/message string */
3228 *valptr
; /* Pointer into string */
3229 static int ipp_supplies
= -1;
3230 /* Report supply levels? */
3234 * Report alerts and messages...
3237 if ((pa
= ippFindAttribute(ipp
, "printer-alert", IPP_TAG_TEXT
)) != NULL
)
3240 if ((pam
= ippFindAttribute(ipp
, "printer-alert-message",
3241 IPP_TAG_TEXT
)) != NULL
)
3244 if ((psm
= ippFindAttribute(ipp
, "printer-state-message",
3245 IPP_TAG_TEXT
)) != NULL
)
3247 char *ptr
; /* Pointer into message */
3250 strlcpy(value
, "INFO: ", sizeof(value
));
3251 for (ptr
= psm
->values
[0].string
.text
, valptr
= value
+ 6;
3252 *ptr
&& valptr
< (value
+ sizeof(value
) - 6);
3255 if (*ptr
< ' ' && *ptr
> 0 && *ptr
!= '\t')
3258 * Substitute "<XX>" for the control character; sprintf is safe because
3259 * we always leave 6 chars free at the end...
3262 sprintf(valptr
, "<%02X>", *ptr
);
3272 fputs(value
, stderr
);
3276 * Now report printer-state-reasons, filtering out some of the reasons we never
3280 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
3281 IPP_TAG_KEYWORD
)) == NULL
)
3284 update_reasons(reasons
, NULL
);
3287 * Relay the current marker-* attribute values...
3290 if (ipp_supplies
< 0)
3292 ppd_file_t
*ppd
; /* PPD file */
3293 ppd_attr_t
*ppdattr
; /* Attribute in PPD file */
3295 if ((ppd
= ppdOpenFile(getenv("PPD"))) != NULL
&&
3296 (ppdattr
= ppdFindAttr(ppd
, "cupsIPPSupplies", NULL
)) != NULL
&&
3297 ppdattr
->value
&& _cups_strcasecmp(ppdattr
->value
, "true"))
3305 if (ipp_supplies
> 0)
3307 if ((marker
= ippFindAttribute(ipp
, "marker-colors", IPP_TAG_NAME
)) != NULL
)
3308 report_attr(marker
);
3309 if ((marker
= ippFindAttribute(ipp
, "marker-high-levels",
3310 IPP_TAG_INTEGER
)) != NULL
)
3311 report_attr(marker
);
3312 if ((marker
= ippFindAttribute(ipp
, "marker-levels",
3313 IPP_TAG_INTEGER
)) != NULL
)
3314 report_attr(marker
);
3315 if ((marker
= ippFindAttribute(ipp
, "marker-low-levels",
3316 IPP_TAG_INTEGER
)) != NULL
)
3317 report_attr(marker
);
3318 if ((marker
= ippFindAttribute(ipp
, "marker-message",
3319 IPP_TAG_TEXT
)) != NULL
)
3320 report_attr(marker
);
3321 if ((marker
= ippFindAttribute(ipp
, "marker-names", IPP_TAG_NAME
)) != NULL
)
3322 report_attr(marker
);
3323 if ((marker
= ippFindAttribute(ipp
, "marker-types",
3324 IPP_TAG_KEYWORD
)) != NULL
)
3325 report_attr(marker
);
3330 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
3332 * 'run_as_user()' - Run the IPP backend as the printing user.
3334 * This function uses an XPC-based user agent to run the backend as the printing
3335 * user. We need to do this in order to have access to the user's Kerberos
3339 static int /* O - Exit status */
3340 run_as_user(char *argv
[], /* I - Command-line arguments */
3341 uid_t uid
, /* I - User ID */
3342 const char *device_uri
, /* I - Device URI */
3343 int fd
) /* I - File to print */
3345 const char *auth_negotiate
;/* AUTH_NEGOTIATE env var */
3346 xpc_connection_t conn
; /* Connection to XPC service */
3347 xpc_object_t request
; /* Request message dictionary */
3348 __block xpc_object_t response
; /* Response message dictionary */
3349 dispatch_semaphore_t sem
; /* Semaphore for waiting for response */
3350 int status
= CUPS_BACKEND_FAILED
;
3351 /* Status of request */
3354 fprintf(stderr
, "DEBUG: Running IPP backend as UID %d.\n", (int)uid
);
3357 * Connect to the user agent for the specified UID...
3360 conn
= xpc_connection_create_mach_service(kPMPrintUIToolAgent
,
3361 dispatch_get_global_queue(0, 0), 0);
3364 _cupsLangPrintFilter(stderr
, "ERROR",
3365 _("Unable to start backend process."));
3366 fputs("DEBUG: Unable to create connection to agent.\n", stderr
);
3370 xpc_connection_set_event_handler(conn
,
3371 ^(xpc_object_t event
)
3373 xpc_type_t messageType
= xpc_get_type(event
);
3375 if (messageType
== XPC_TYPE_ERROR
)
3377 if (event
== XPC_ERROR_CONNECTION_INTERRUPTED
)
3378 fprintf(stderr
, "DEBUG: Interrupted connection to service %s.\n",
3379 xpc_connection_get_name(conn
));
3380 else if (event
== XPC_ERROR_CONNECTION_INVALID
)
3381 fprintf(stderr
, "DEBUG: Connection invalid for service %s.\n",
3382 xpc_connection_get_name(conn
));
3384 fprintf(stderr
, "DEBUG: Unxpected error for service %s: %s\n",
3385 xpc_connection_get_name(conn
),
3386 xpc_dictionary_get_string(event
, XPC_ERROR_KEY_DESCRIPTION
));
3389 xpc_connection_set_target_uid(conn
, uid
);
3390 xpc_connection_resume(conn
);
3393 * Try starting the backend...
3396 request
= xpc_dictionary_create(NULL
, NULL
, 0);
3397 xpc_dictionary_set_int64(request
, "command", kPMStartJob
);
3398 xpc_dictionary_set_string(request
, "device-uri", device_uri
);
3399 xpc_dictionary_set_string(request
, "job-id", argv
[1]);
3400 xpc_dictionary_set_string(request
, "user", argv
[2]);
3401 xpc_dictionary_set_string(request
, "title", argv
[3]);
3402 xpc_dictionary_set_string(request
, "copies", argv
[4]);
3403 xpc_dictionary_set_string(request
, "options", argv
[5]);
3404 xpc_dictionary_set_string(request
, "auth-info-required",
3405 getenv("AUTH_INFO_REQUIRED"));
3406 if ((auth_negotiate
= getenv("AUTH_NEGOTIATE")) != NULL
)
3407 xpc_dictionary_set_string(request
, "auth-negotiate", auth_negotiate
);
3408 xpc_dictionary_set_fd(request
, "stdin", fd
);
3409 xpc_dictionary_set_fd(request
, "stderr", 2);
3410 xpc_dictionary_set_fd(request
, "side-channel", CUPS_SC_FD
);
3412 sem
= dispatch_semaphore_create(0);
3415 xpc_connection_send_message_with_reply(conn
, request
,
3416 dispatch_get_global_queue(0,0),
3417 ^(xpc_object_t reply
)
3419 /* Save the response and wake up */
3420 if (xpc_get_type(reply
)
3421 == XPC_TYPE_DICTIONARY
)
3422 response
= xpc_retain(reply
);
3424 dispatch_semaphore_signal(sem
);
3427 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
3428 xpc_release(request
);
3429 dispatch_release(sem
);
3433 child_pid
= (pid_t
)xpc_dictionary_get_int64(response
, "child-pid");
3435 xpc_release(response
);
3438 fprintf(stderr
, "DEBUG: Child PID=%d.\n", (int)child_pid
);
3441 _cupsLangPrintFilter(stderr
, "ERROR",
3442 _("Unable to start backend process."));
3443 fputs("DEBUG: No child PID.\n", stderr
);
3449 _cupsLangPrintFilter(stderr
, "ERROR",
3450 _("Unable to start backend process."));
3451 fputs("DEBUG: No reply from agent.\n", stderr
);
3456 * Then wait for the backend to finish...
3459 request
= xpc_dictionary_create(NULL
, NULL
, 0);
3460 xpc_dictionary_set_int64(request
, "command", kPMWaitForJob
);
3461 xpc_dictionary_set_fd(request
, "stderr", 2);
3463 sem
= dispatch_semaphore_create(0);
3466 xpc_connection_send_message_with_reply(conn
, request
,
3467 dispatch_get_global_queue(0,0),
3468 ^(xpc_object_t reply
)
3470 /* Save the response and wake up */
3471 if (xpc_get_type(reply
)
3472 == XPC_TYPE_DICTIONARY
)
3473 response
= xpc_retain(reply
);
3475 dispatch_semaphore_signal(sem
);
3478 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
3479 xpc_release(request
);
3480 dispatch_release(sem
);
3484 status
= (int)xpc_dictionary_get_int64(response
, "status");
3486 if (status
== SIGTERM
|| status
== SIGKILL
|| status
== SIGPIPE
)
3488 fprintf(stderr
, "DEBUG: Child terminated on signal %d.\n", status
);
3489 status
= CUPS_BACKEND_FAILED
;
3491 else if (WIFSIGNALED(status
))
3493 fprintf(stderr
, "DEBUG: Child crashed on signal %d.\n", status
);
3494 status
= CUPS_BACKEND_STOP
;
3496 else if (WIFEXITED(status
))
3498 status
= WEXITSTATUS(status
);
3499 fprintf(stderr
, "DEBUG: Child exited with status %d.\n", status
);
3502 xpc_release(response
);
3505 _cupsLangPrintFilter(stderr
, "ERROR",
3506 _("Unable to get backend exit status."));
3512 xpc_connection_cancel(conn
);
3518 #endif /* HAVE_GSSAPI && HAVE_XPC */
3522 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
3526 sigterm_handler(int sig
) /* I - Signal */
3528 (void)sig
; /* remove compiler warnings... */
3530 write(2, "DEBUG: Got SIGTERM.\n", 20);
3532 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
3535 kill(child_pid
, sig
);
3538 #endif /* HAVE_GSSAPI && HAVE_XPC */
3543 * Flag that the job should be canceled...
3546 write(2, "DEBUG: job_canceled = 1.\n", 25);
3553 * The scheduler already tried to cancel us once, now just terminate
3554 * after removing our temp file!
3558 unlink(tmpfilename
);
3565 * 'timeout_cb()' - Handle HTTP timeouts.
3568 static int /* O - 1 to continue, 0 to cancel */
3569 timeout_cb(http_t
*http
, /* I - Connection to server (unused) */
3570 void *user_data
) /* I - User data (unused) */
3575 return (!job_canceled
);
3580 * 'update_reasons()' - Update the printer-state-reasons values.
3584 update_reasons(ipp_attribute_t
*attr
, /* I - printer-state-reasons or NULL */
3585 const char *s
) /* I - STATE: string or NULL */
3587 char op
; /* Add (+), remove (-), replace (\0) */
3588 cups_array_t
*new_reasons
; /* New reasons array */
3589 char *reason
, /* Current reason */
3590 add
[2048], /* Reasons added string */
3591 *addptr
, /* Pointer into add string */
3592 rem
[2048], /* Reasons removed string */
3593 *remptr
; /* Pointer into remove string */
3594 const char *addprefix
, /* Current add string prefix */
3595 *remprefix
; /* Current remove string prefix */
3598 fprintf(stderr
, "DEBUG: update_reasons(attr=%d(%s%s), s=\"%s\")\n",
3599 attr
? attr
->num_values
: 0, attr
? attr
->values
[0].string
.text
: "",
3600 attr
&& attr
->num_values
> 1 ? ",..." : "", s
? s
: "(null)");
3603 * Create an array of new reason keyword strings...
3608 int i
; /* Looping var */
3610 new_reasons
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
3613 for (i
= 0; i
< attr
->num_values
; i
++)
3615 reason
= attr
->values
[i
].string
.text
;
3617 if (strcmp(reason
, "none") &&
3618 strcmp(reason
, "none-report") &&
3619 strcmp(reason
, "paused") &&
3620 strncmp(reason
, "spool-area-full", 15) &&
3621 strcmp(reason
, "com.apple.print.recoverable-warning") &&
3622 strncmp(reason
, "cups-", 5))
3623 cupsArrayAdd(new_reasons
, reason
);
3628 if (*s
== '+' || *s
== '-')
3633 new_reasons
= _cupsArrayNewStrings(s
, ',');
3639 * Compute the changes...
3643 addprefix
= "STATE: +";
3646 remprefix
= "STATE: -";
3649 fprintf(stderr
, "DEBUG2: op='%c', new_reasons=%d, state_reasons=%d\n",
3650 op
? op
: ' ', cupsArrayCount(new_reasons
),
3651 cupsArrayCount(state_reasons
));
3653 _cupsMutexLock(&report_mutex
);
3661 for (reason
= (char *)cupsArrayFirst(new_reasons
);
3663 reason
= (char *)cupsArrayNext(new_reasons
))
3665 if (!cupsArrayFind(state_reasons
, reason
))
3667 if (!strncmp(reason
, "cups-remote-", 12))
3670 * If we are setting cups-remote-xxx, remove all other cups-remote-xxx
3674 char *temp
; /* Current reason in state_reasons */
3676 cupsArraySave(state_reasons
);
3678 for (temp
= (char *)cupsArrayFirst(state_reasons
);
3680 temp
= (char *)cupsArrayNext(state_reasons
))
3681 if (!strncmp(temp
, "cups-remote-", 12))
3683 snprintf(remptr
, sizeof(rem
) - (size_t)(remptr
- rem
), "%s%s", remprefix
, temp
);
3684 remptr
+= strlen(remptr
);
3687 cupsArrayRemove(state_reasons
, temp
);
3691 cupsArrayRestore(state_reasons
);
3694 cupsArrayAdd(state_reasons
, reason
);
3696 snprintf(addptr
, sizeof(add
) - (size_t)(addptr
- add
), "%s%s", addprefix
, reason
);
3697 addptr
+= strlen(addptr
);
3708 for (reason
= (char *)cupsArrayFirst(new_reasons
);
3710 reason
= (char *)cupsArrayNext(new_reasons
))
3712 if (cupsArrayFind(state_reasons
, reason
))
3714 snprintf(remptr
, sizeof(rem
) - (size_t)(remptr
- rem
), "%s%s", remprefix
, reason
);
3715 remptr
+= strlen(remptr
);
3718 cupsArrayRemove(state_reasons
, reason
);
3725 * Replace reasons...
3728 for (reason
= (char *)cupsArrayFirst(state_reasons
);
3730 reason
= (char *)cupsArrayNext(state_reasons
))
3732 if (strncmp(reason
, "cups-", 5) && !cupsArrayFind(new_reasons
, reason
))
3734 snprintf(remptr
, sizeof(rem
) - (size_t)(remptr
- rem
), "%s%s", remprefix
, reason
);
3735 remptr
+= strlen(remptr
);
3738 cupsArrayRemove(state_reasons
, reason
);
3742 for (reason
= (char *)cupsArrayFirst(new_reasons
);
3744 reason
= (char *)cupsArrayNext(new_reasons
))
3746 if (!cupsArrayFind(state_reasons
, reason
))
3748 cupsArrayAdd(state_reasons
, reason
);
3750 snprintf(addptr
, sizeof(add
) - (size_t)(addptr
- add
), "%s%s", addprefix
, reason
);
3751 addptr
+= strlen(addptr
);
3757 _cupsMutexUnlock(&report_mutex
);
3760 * Report changes and return...
3763 if (add
[0] && rem
[0])
3764 fprintf(stderr
, "%s\n%s\n", add
, rem
);
3766 fprintf(stderr
, "%s\n", add
);
3768 fprintf(stderr
, "%s\n", rem
);
3772 * End of "$Id: ipp.c 12624 2015-05-06 23:50:20Z msweet $".