2 * "$Id: ipp.c 9759 2011-05-11 03:24:33Z mike $"
4 * IPP backend for CUPS.
6 * Copyright 2007-2012 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * "LICENSE" which should have been included with this file. If this
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * main() - Send a file to the printer or server.
20 * cancel_job() - Cancel a print job.
21 * check_printer_state() - Check the printer state.
22 * compress_files() - Compress print files.
23 * monitor_printer() - Monitor the printer state.
24 * new_request() - Create a new print creation or validation
26 * password_cb() - Disable the password prompt for
27 * cupsDoFileRequest().
28 * quote_string() - Quote a string value.
29 * report_attr() - Report an IPP attribute value.
30 * report_printer_state() - Report the printer state.
31 * run_as_user() - Run the IPP backend as the printing user.
32 * sigterm_handler() - Handle 'terminate' signals that stop the backend.
33 * timeout_cb() - Handle HTTP timeouts.
34 * update_reasons() - Update the printer-state-reasons values.
38 * Include necessary headers.
41 #include "backend-private.h"
42 #include <cups/array-private.h>
43 #include <sys/types.h>
46 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
48 # define kPMPrintUIToolAgent "com.apple.printuitool.agent"
49 # define kPMStartJob 100
50 # define kPMWaitForJob 101
51 # ifdef HAVE_XPC_PRIVATE_H
52 # include <xpc/private.h>
54 extern void xpc_connection_set_target_uid(xpc_connection_t connection
,
56 # endif /* HAVE_XPC_PRIVATE_H */
57 #endif /* HAVE_GSSAPI && HAVE_XPC */
64 typedef struct _cups_monitor_s
/**** Monitoring data ****/
66 const char *uri
, /* Printer URI */
67 *hostname
, /* Hostname */
69 *resource
; /* Resource path */
70 int port
, /* Port number */
71 version
, /* IPP version */
72 job_id
, /* Job ID for submitted job */
73 get_job_attrs
; /* Support Get-Job-Attributes? */
74 const char *job_name
; /* Job name for submitted job */
75 http_encryption_t encryption
; /* Use encryption? */
76 ipp_jstate_t job_state
; /* Current job state */
77 ipp_pstate_t printer_state
; /* Current printer state */
85 static const char *auth_info_required
;
86 /* New auth-info-required value */
87 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
88 static int child_pid
= 0; /* Child process ID */
89 #endif /* HAVE_GSSAPI && HAVE_XPC */
90 static const char * const jattrs
[] = /* Job attributes we want */
93 "job-impressions-completed",
94 "job-media-sheets-completed",
96 "job-originating-user-name",
100 static int job_canceled
= 0;
102 static char username
[256] = "",
103 /* Username for device URI */
105 /* Password for device URI */
106 static int password_tries
= 0;
108 static const char * const pattrs
[] = /* Printer attributes we want */
112 "document-format-supported",
114 "marker-high-levels",
120 "media-col-supported",
121 "multiple-document-handling-supported",
122 "operations-supported",
124 "printer-alert-description",
125 "printer-is-accepting-jobs",
127 "printer-state-message",
128 "printer-state-reasons"
130 static const char * const remote_job_states
[] =
131 { /* Remote job state keywords */
132 "+cups-remote-pending",
133 "+cups-remote-pending-held",
134 "+cups-remote-processing",
135 "+cups-remote-stopped",
136 "+cups-remote-canceled",
137 "+cups-remote-aborted",
138 "+cups-remote-completed"
140 static _cups_mutex_t report_mutex
= _CUPS_MUTEX_INITIALIZER
;
141 /* Mutex to control access */
142 static int num_attr_cache
= 0;
143 /* Number of cached attributes */
144 static cups_option_t
*attr_cache
= NULL
;
145 /* Cached attributes */
146 static cups_array_t
*state_reasons
; /* Array of printe-state-reasons keywords */
147 static char tmpfilename
[1024] = "";
148 /* Temporary spool file name */
155 static void cancel_job(http_t
*http
, const char *uri
, int id
,
156 const char *resource
, const char *user
,
158 static ipp_pstate_t
check_printer_state(http_t
*http
, const char *uri
,
159 const char *resource
,
160 const char *user
, int version
);
162 static void compress_files(int num_files
, char **files
);
163 #endif /* HAVE_LIBZ */
164 static void *monitor_printer(_cups_monitor_t
*monitor
);
165 static ipp_t
*new_request(ipp_op_t op
, int version
, const char *uri
,
166 const char *user
, const char *title
,
167 int num_options
, cups_option_t
*options
,
168 const char *compression
, int copies
,
169 const char *format
, _ppd_cache_t
*pc
,
170 ipp_attribute_t
*media_col_sup
,
171 ipp_attribute_t
*doc_handling_sup
);
172 static const char *password_cb(const char *prompt
, http_t
*http
,
173 const char *method
, const char *resource
,
175 static const char *quote_string(const char *s
, char *q
, size_t qsize
);
176 static void report_attr(ipp_attribute_t
*attr
);
177 static void report_printer_state(ipp_t
*ipp
);
178 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
179 static int run_as_user(char *argv
[], uid_t uid
,
180 const char *device_uri
, int fd
);
181 #endif /* HAVE_GSSAPI && HAVE_XPC */
182 static void sigterm_handler(int sig
);
183 static int timeout_cb(http_t
*http
, void *user_data
);
184 static void update_reasons(ipp_attribute_t
*attr
, const char *s
);
188 * 'main()' - Send a file to the printer or server.
192 * printer-uri job-id user title copies options [file]
195 int /* O - Exit status */
196 main(int argc
, /* I - Number of command-line args */
197 char *argv
[]) /* I - Command-line arguments */
199 int i
; /* Looping var */
200 int send_options
; /* Send job options? */
201 int num_options
; /* Number of printer options */
202 cups_option_t
*options
; /* Printer options */
203 const char *device_uri
; /* Device URI */
204 char scheme
[255], /* Scheme in URI */
205 hostname
[1024], /* Hostname */
206 resource
[1024], /* Resource info (printer name) */
207 addrname
[256], /* Address name */
208 *optptr
, /* Pointer to URI options */
209 *name
, /* Name of option */
210 *value
, /* Value of option */
211 sep
; /* Separator character */
212 http_addrlist_t
*addrlist
; /* Address of printer */
213 int snmp_enabled
= 1; /* Is SNMP enabled? */
214 int snmp_fd
, /* SNMP socket */
215 start_count
, /* Page count via SNMP at start */
216 page_count
, /* Page count via SNMP */
217 have_supplies
; /* Printer supports supply levels? */
218 int num_files
; /* Number of files to print */
219 char **files
, /* Files to print */
220 *compatfile
= NULL
; /* Compatibility filename */
221 off_t compatsize
= 0; /* Size of compatibility file */
222 int port
; /* Port number (not used) */
223 char portname
[255]; /* Port name */
224 char uri
[HTTP_MAX_URI
]; /* Updated URI without user/pass */
225 char print_job_name
[1024]; /* Update job-name for Print-Job */
226 http_status_t http_status
; /* Status of HTTP request */
227 ipp_status_t ipp_status
; /* Status of IPP request */
228 http_t
*http
; /* HTTP connection */
229 ipp_t
*request
, /* IPP request */
230 *response
, /* IPP response */
231 *supported
; /* get-printer-attributes response */
232 time_t start_time
; /* Time of first connect */
233 int contimeout
; /* Connection timeout */
234 int delay
, /* Delay for retries */
235 prev_delay
; /* Previous delay */
236 const char *compression
; /* Compression mode */
237 int waitjob
, /* Wait for job complete? */
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 */
244 ipp_attribute_t
*copies_sup
; /* copies-supported */
245 ipp_attribute_t
*cups_version
; /* cups-version */
246 ipp_attribute_t
*format_sup
; /* document-format-supported */
247 ipp_attribute_t
*media_col_sup
; /* media-col-supported */
248 ipp_attribute_t
*operations_sup
; /* operations-supported */
249 ipp_attribute_t
*doc_handling_sup
; /* multiple-document-handling-supported */
250 ipp_attribute_t
*printer_state
; /* printer-state attribute */
251 ipp_attribute_t
*printer_accepting
; /* printer-is-accepting-jobs */
252 int create_job
= 0, /* Does printer support Create-Job? */
253 get_job_attrs
= 0, /* Does printer support Get-Job-Attributes? */
254 send_document
= 0, /* Does printer support Send-Document? */
255 validate_job
= 0; /* Does printer support Validate-Job? */
256 int copies
, /* Number of copies for job */
257 copies_remaining
; /* Number of copies remaining */
258 const char *content_type
, /* CONTENT_TYPE environment variable */
259 *final_content_type
, /* FINAL_CONTENT_TYPE environment var */
260 *document_format
; /* document-format value */
261 int fd
; /* File descriptor */
262 off_t bytes
= 0; /* Bytes copied */
263 char buffer
[16384]; /* Copy buffer */
264 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
265 struct sigaction action
; /* Actions for POSIX signals */
266 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
267 int version
; /* IPP version */
268 ppd_file_t
*ppd
; /* PPD file */
269 _ppd_cache_t
*pc
; /* PPD cache and mapping data */
270 fd_set input
; /* Input set for select() */
274 * Make sure status messages are not buffered...
277 setbuf(stderr
, NULL
);
280 * Ignore SIGPIPE and catch SIGTERM signals...
284 sigset(SIGPIPE
, SIG_IGN
);
285 sigset(SIGTERM
, sigterm_handler
);
286 #elif defined(HAVE_SIGACTION)
287 memset(&action
, 0, sizeof(action
));
288 action
.sa_handler
= SIG_IGN
;
289 sigaction(SIGPIPE
, &action
, NULL
);
291 sigemptyset(&action
.sa_mask
);
292 sigaddset(&action
.sa_mask
, SIGTERM
);
293 action
.sa_handler
= sigterm_handler
;
294 sigaction(SIGTERM
, &action
, NULL
);
296 signal(SIGPIPE
, SIG_IGN
);
297 signal(SIGTERM
, sigterm_handler
);
298 #endif /* HAVE_SIGSET */
301 * Check command-line...
308 if ((s
= strrchr(argv
[0], '/')) != NULL
)
313 printf("network %s \"Unknown\" \"%s (%s)\"\n",
314 s
, _cupsLangString(cupsLangDefault(),
315 _("Internet Printing Protocol")), s
);
316 return (CUPS_BACKEND_OK
);
320 _cupsLangPrintf(stderr
,
321 _("Usage: %s job-id user title copies options [file]"),
323 return (CUPS_BACKEND_STOP
);
327 * Get the device URI...
330 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
332 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
335 if (getenv("CLASS") != NULL
)
336 return (CUPS_BACKEND_FAILED
);
339 if ((auth_info_required
= getenv("AUTH_INFO_REQUIRED")) == NULL
)
340 auth_info_required
= "none";
342 state_reasons
= _cupsArrayNewStrings(getenv("PRINTER_STATE_REASONS"), ',');
346 * For Kerberos, become the printing user (if we can) to get the credentials
350 if (!getuid() && (value
= getenv("AUTH_UID")) != NULL
)
352 uid_t uid
= (uid_t
)atoi(value
);
359 return (run_as_user(argv
, uid
, device_uri
, 0));
362 int status
= 0; /* Exit status */
364 for (i
= 6; i
< argc
&& !status
&& !job_canceled
; i
++)
366 if ((fd
= open(argv
[i
], O_RDONLY
)) >= 0)
368 status
= run_as_user(argv
, uid
, device_uri
, fd
);
373 _cupsLangPrintError("ERROR", _("Unable to open print file"));
374 status
= CUPS_BACKEND_FAILED
;
382 # else /* No XPC, just try to run as the user ID */
385 # endif /* HAVE_XPC */
387 #endif /* HAVE_GSSAPI */
390 * Get the (final) content type...
393 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
394 content_type
= "application/octet-stream";
396 if ((final_content_type
= getenv("FINAL_CONTENT_TYPE")) == NULL
)
398 final_content_type
= content_type
;
400 if (!strncmp(final_content_type
, "printer/", 8))
401 final_content_type
= "application/vnd.cups-raw";
405 * Extract the hostname and printer name from the URI...
408 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
409 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
410 resource
, sizeof(resource
));
413 port
= IPP_PORT
; /* Default to port 631 */
415 if (!strcmp(scheme
, "https"))
416 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
418 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
421 * See if there are any options...
428 contimeout
= 7 * 24 * 60 * 60;
430 if ((optptr
= strchr(resource
, '?')) != NULL
)
433 * Yup, terminate the device name string and move to the first
434 * character of the optptr...
440 * Then parse the optptr...
451 while (*optptr
&& *optptr
!= '=' && *optptr
!= '+' && *optptr
!= '&')
454 if ((sep
= *optptr
) != '\0')
465 while (*optptr
&& *optptr
!= '+' && *optptr
!= '&')
475 * Process the option...
478 if (!_cups_strcasecmp(name
, "waitjob"))
481 * Wait for job completion?
484 waitjob
= !_cups_strcasecmp(value
, "on") ||
485 !_cups_strcasecmp(value
, "yes") ||
486 !_cups_strcasecmp(value
, "true");
488 else if (!_cups_strcasecmp(name
, "waitprinter"))
491 * Wait for printer idle?
494 waitprinter
= !_cups_strcasecmp(value
, "on") ||
495 !_cups_strcasecmp(value
, "yes") ||
496 !_cups_strcasecmp(value
, "true");
498 else if (!_cups_strcasecmp(name
, "encryption"))
501 * Enable/disable encryption?
504 if (!_cups_strcasecmp(value
, "always"))
505 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
506 else if (!_cups_strcasecmp(value
, "required"))
507 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
508 else if (!_cups_strcasecmp(value
, "never"))
509 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
510 else if (!_cups_strcasecmp(value
, "ifrequested"))
511 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
514 _cupsLangPrintFilter(stderr
, "ERROR",
515 _("Unknown encryption option value: \"%s\"."),
519 else if (!_cups_strcasecmp(name
, "snmp"))
522 * Enable/disable SNMP stuff...
525 snmp_enabled
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
526 _cups_strcasecmp(value
, "yes") ||
527 _cups_strcasecmp(value
, "true");
529 else if (!_cups_strcasecmp(name
, "version"))
531 if (!strcmp(value
, "1.0"))
533 else if (!strcmp(value
, "1.1"))
535 else if (!strcmp(value
, "2.0"))
537 else if (!strcmp(value
, "2.1"))
539 else if (!strcmp(value
, "2.2"))
543 _cupsLangPrintFilter(stderr
, "ERROR",
544 _("Unknown version option value: \"%s\"."),
549 else if (!_cups_strcasecmp(name
, "compression"))
551 if (!_cups_strcasecmp(value
, "true") || !_cups_strcasecmp(value
, "yes") ||
552 !_cups_strcasecmp(value
, "on") || !_cups_strcasecmp(value
, "gzip"))
553 compression
= "gzip";
555 #endif /* HAVE_LIBZ */
556 else if (!_cups_strcasecmp(name
, "contimeout"))
559 * Set the connection timeout...
563 contimeout
= atoi(value
);
571 _cupsLangPrintFilter(stderr
, "ERROR",
572 _("Unknown option \"%s\" with value \"%s\"."),
579 * If we have 7 arguments, print the file named on the command-line.
580 * Otherwise, copy stdin to a temporary file and print the temporary
588 send_options
= !_cups_strcasecmp(final_content_type
, "application/pdf") ||
589 !_cups_strcasecmp(final_content_type
, "application/vnd.cups-pdf") ||
590 !_cups_strncasecmp(final_content_type
, "image/", 6);
592 fputs("DEBUG: Sending stdin for job...\n", stderr
);
597 * Point to the files on the command-line...
600 num_files
= argc
- 6;
606 compress_files(num_files
, files
);
607 #endif /* HAVE_LIBZ */
609 fprintf(stderr
, "DEBUG: %d files to send in job...\n", num_files
);
613 * Set the authentication info, if any...
616 cupsSetPasswordCB2(password_cb
, NULL
);
621 * Use authentication information in the device URI...
624 if ((password
= strchr(username
, ':')) != NULL
)
627 cupsSetUser(username
);
632 * Try loading authentication information from the environment.
635 const char *ptr
= getenv("AUTH_USERNAME");
639 strlcpy(username
, ptr
, sizeof(username
));
643 password
= getenv("AUTH_PASSWORD");
647 * Try finding the remote server...
650 start_time
= time(NULL
);
652 sprintf(portname
, "%d", port
);
654 update_reasons(NULL
, "+connecting-to-device");
655 fprintf(stderr
, "DEBUG: Looking up \"%s\"...\n", hostname
);
657 while ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
659 _cupsLangPrintFilter(stderr
, "INFO",
660 _("Unable to locate printer \"%s\"."), hostname
);
663 if (getenv("CLASS") != NULL
)
665 update_reasons(NULL
, "-connecting-to-device");
666 return (CUPS_BACKEND_STOP
);
670 return (CUPS_BACKEND_OK
);
673 http
= _httpCreate(hostname
, port
, addrlist
, cupsEncryption(), AF_UNSPEC
);
674 httpSetTimeout(http
, 30.0, timeout_cb
, NULL
);
677 * See if the printer supports SNMP...
681 snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
);
686 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
),
689 have_supplies
= start_count
= 0;
692 * Wait for data from the filter...
697 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), 0, backendNetworkSideCB
))
698 return (CUPS_BACKEND_OK
);
699 else if ((bytes
= read(0, buffer
, sizeof(buffer
))) <= 0)
700 return (CUPS_BACKEND_OK
);
704 * Try connecting to the remote server...
707 delay
= _cupsNextDelay(0, &prev_delay
);
711 fprintf(stderr
, "DEBUG: Connecting to %s:%d\n", hostname
, port
);
712 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
714 if (httpReconnect(http
))
716 int error
= errno
; /* Connection error */
718 if (http
->status
== HTTP_PKI_ERROR
)
719 update_reasons(NULL
, "+cups-certificate-error");
724 if (getenv("CLASS") != NULL
)
727 * If the CLASS environment variable is set, the job was submitted
728 * to a class and not to a specific queue. In this case, we want
729 * to abort immediately so that the job can be requeued on the next
730 * available printer in the class.
733 _cupsLangPrintFilter(stderr
, "INFO",
734 _("Unable to contact printer, queuing on next "
735 "printer in class."));
738 * Sleep 5 seconds to keep the job from requeuing too rapidly...
743 update_reasons(NULL
, "-connecting-to-device");
745 return (CUPS_BACKEND_FAILED
);
748 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
750 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
751 errno
== EHOSTUNREACH
)
753 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
755 _cupsLangPrintFilter(stderr
, "ERROR",
756 _("The printer is not responding."));
757 update_reasons(NULL
, "-connecting-to-device");
758 return (CUPS_BACKEND_FAILED
);
764 _cupsLangPrintFilter(stderr
, "WARNING",
765 _("The printer may not exist or "
766 "is unavailable at this time."));
770 _cupsLangPrintFilter(stderr
, "WARNING",
771 _("The printer is unreachable at this "
777 _cupsLangPrintFilter(stderr
, "WARNING",
778 _("The printer is in use."));
784 delay
= _cupsNextDelay(delay
, &prev_delay
);
788 _cupsLangPrintFilter(stderr
, "ERROR",
789 _("The printer is not responding."));
797 update_reasons(NULL
, "-cups-certificate-error");
799 while (http
->fd
< 0);
802 return (CUPS_BACKEND_OK
);
804 return (CUPS_BACKEND_FAILED
);
806 update_reasons(NULL
, "-connecting-to-device");
807 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
809 fprintf(stderr
, "DEBUG: Connected to %s:%d...\n",
810 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
811 _httpAddrPort(http
->hostaddr
));
814 * Build a URI for the printer and fill the standard IPP attributes for
815 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
816 * might contain username:password information...
819 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), scheme
, NULL
, hostname
,
823 * First validate the destination and see if the device supports multiple
830 media_col_sup
= NULL
;
832 operations_sup
= NULL
;
833 doc_handling_sup
= NULL
;
838 * Check for side-channel requests...
841 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
844 * Build the IPP request...
847 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
848 request
->request
.op
.version
[0] = version
/ 10;
849 request
->request
.op
.version
[1] = version
% 10;
851 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
854 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
855 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
862 fputs("DEBUG: Getting supported attributes...\n", stderr
);
864 if (http
->version
< HTTP_1_1
)
866 fprintf(stderr
, "DEBUG: Printer responded with HTTP version %d.%d.\n",
867 http
->version
/ 100, http
->version
% 100);
868 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
869 "cups-ipp-wrong-http-version");
872 supported
= cupsDoRequest(http
, request
, resource
);
873 ipp_status
= cupsLastError();
875 fprintf(stderr
, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
876 ippErrorString(ipp_status
), cupsLastErrorString());
878 if (ipp_status
<= IPP_OK_CONFLICT
)
882 fprintf(stderr
, "DEBUG: Get-Printer-Attributes returned %s.\n",
883 ippErrorString(ipp_status
));
885 if (ipp_status
== IPP_PRINTER_BUSY
||
886 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
888 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
890 _cupsLangPrintFilter(stderr
, "ERROR",
891 _("The printer is not responding."));
892 return (CUPS_BACKEND_FAILED
);
895 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
897 report_printer_state(supported
);
901 delay
= _cupsNextDelay(delay
, &prev_delay
);
903 else if ((ipp_status
== IPP_BAD_REQUEST
||
904 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
> 10)
907 * Switch to IPP/1.1 or IPP/1.0...
912 _cupsLangPrintFilter(stderr
, "INFO",
913 _("The printer does not support IPP/%d.%d, trying "
914 "IPP/%s."), version
/ 10, version
% 10, "1.1");
919 _cupsLangPrintFilter(stderr
, "INFO",
920 _("The printer does not support IPP/%d.%d, trying "
921 "IPP/%s."), version
/ 10, version
% 10, "1.0");
927 else if (ipp_status
== IPP_NOT_FOUND
)
929 _cupsLangPrintFilter(stderr
, "ERROR",
930 _("The printer URI is incorrect or no longer "
933 ippDelete(supported
);
935 return (CUPS_BACKEND_STOP
);
937 else if (ipp_status
== IPP_FORBIDDEN
||
938 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
940 const char *www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
941 /* WWW-Authenticate field value */
943 if (!strncmp(www_auth
, "Negotiate", 9))
944 auth_info_required
= "negotiate";
945 else if (www_auth
[0])
946 auth_info_required
= "username,password";
948 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
949 return (CUPS_BACKEND_AUTH_REQUIRED
);
951 else if (ipp_status
!= IPP_NOT_AUTHORIZED
)
953 _cupsLangPrintFilter(stderr
, "ERROR",
954 _("Unable to get printer status."));
960 ippDelete(supported
);
965 if (!getenv("CLASS"))
968 * Check printer-is-accepting-jobs = false and printer-state-reasons for the
969 * "spool-area-full" keyword...
974 if ((printer_accepting
= ippFindAttribute(supported
,
975 "printer-is-accepting-jobs",
976 IPP_TAG_BOOLEAN
)) != NULL
&&
977 !printer_accepting
->values
[0].boolean
)
979 else if (!printer_accepting
)
980 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
981 "cups-ipp-missing-printer-is-accepting-jobs");
983 if ((printer_state
= ippFindAttribute(supported
,
984 "printer-state-reasons",
985 IPP_TAG_KEYWORD
)) != NULL
&& !busy
)
987 for (i
= 0; i
< printer_state
->num_values
; i
++)
988 if (!strcmp(printer_state
->values
[0].string
.text
,
989 "spool-area-full") ||
990 !strncmp(printer_state
->values
[0].string
.text
, "spool-area-full-",
998 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
999 "cups-ipp-missing-printer-state-reasons");
1003 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
1005 report_printer_state(supported
);
1009 delay
= _cupsNextDelay(delay
, &prev_delay
);
1011 ippDelete(supported
);
1018 * Check for supported attributes...
1021 if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
1022 IPP_TAG_RANGE
)) != NULL
)
1025 * Has the "copies-supported" attribute - does it have an upper
1029 fprintf(stderr
, "DEBUG: copies-supported=%d-%d\n",
1030 copies_sup
->values
[0].range
.lower
,
1031 copies_sup
->values
[0].range
.upper
);
1033 if (copies_sup
->values
[0].range
.upper
<= 1)
1034 copies_sup
= NULL
; /* No */
1037 cups_version
= ippFindAttribute(supported
, "cups-version", IPP_TAG_TEXT
);
1039 if ((format_sup
= ippFindAttribute(supported
, "document-format-supported",
1040 IPP_TAG_MIMETYPE
)) != NULL
)
1042 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
1043 format_sup
->num_values
);
1044 for (i
= 0; i
< format_sup
->num_values
; i
++)
1045 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
1046 format_sup
->values
[i
].string
.text
);
1049 if ((media_col_sup
= ippFindAttribute(supported
, "media-col-supported",
1050 IPP_TAG_KEYWORD
)) != NULL
)
1052 fprintf(stderr
, "DEBUG: media-col-supported (%d values)\n",
1053 media_col_sup
->num_values
);
1054 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
1055 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
1056 media_col_sup
->values
[i
].string
.text
);
1059 if ((operations_sup
= ippFindAttribute(supported
, "operations-supported",
1060 IPP_TAG_ENUM
)) != NULL
)
1062 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1063 if (operations_sup
->values
[i
].integer
== IPP_PRINT_JOB
)
1066 if (i
>= operations_sup
->num_values
)
1067 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1068 "cups-ipp-missing-print-job");
1070 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1071 if (operations_sup
->values
[i
].integer
== IPP_CANCEL_JOB
)
1074 if (i
>= operations_sup
->num_values
)
1075 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1076 "cups-ipp-missing-cancel-job");
1078 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1079 if (operations_sup
->values
[i
].integer
== IPP_GET_JOB_ATTRIBUTES
)
1082 if (i
>= operations_sup
->num_values
)
1083 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1084 "cups-ipp-missing-get-job-attributes");
1086 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1087 if (operations_sup
->values
[i
].integer
== IPP_GET_PRINTER_ATTRIBUTES
)
1090 if (i
>= operations_sup
->num_values
)
1091 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1092 "cups-ipp-missing-get-printer-attributes");
1094 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1096 if (operations_sup
->values
[i
].integer
== IPP_VALIDATE_JOB
)
1098 else if (operations_sup
->values
[i
].integer
== IPP_CREATE_JOB
)
1100 else if (operations_sup
->values
[i
].integer
== IPP_SEND_DOCUMENT
)
1102 else if (operations_sup
->values
[i
].integer
== IPP_GET_JOB_ATTRIBUTES
)
1106 if (create_job
&& !send_document
)
1108 fputs("DEBUG: Printer supports Create-Job but not Send-Document.\n",
1112 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1113 "cups-ipp-missing-send-document");
1117 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1118 "cups-ipp-missing-validate-job");
1121 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1122 "cups-ipp-missing-operations-supported");
1124 doc_handling_sup
= ippFindAttribute(supported
,
1125 "multiple-document-handling-supported",
1128 report_printer_state(supported
);
1130 while (!job_canceled
&& ipp_status
> IPP_OK_CONFLICT
);
1133 return (CUPS_BACKEND_OK
);
1136 * See if the printer is accepting jobs and is not stopped; if either
1137 * condition is true and we are printing to a class, requeue the job...
1140 if (getenv("CLASS") != NULL
)
1142 printer_state
= ippFindAttribute(supported
, "printer-state",
1144 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
1147 if (printer_state
== NULL
||
1148 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
1150 printer_accepting
== NULL
||
1151 !printer_accepting
->values
[0].boolean
)
1154 * If the CLASS environment variable is set, the job was submitted
1155 * to a class and not to a specific queue. In this case, we want
1156 * to abort immediately so that the job can be requeued on the next
1157 * available printer in the class.
1160 _cupsLangPrintFilter(stderr
, "INFO",
1161 _("Unable to contact printer, queuing on next "
1162 "printer in class."));
1164 ippDelete(supported
);
1168 * Sleep 5 seconds to keep the job from requeuing too rapidly...
1173 return (CUPS_BACKEND_FAILED
);
1178 * See if the printer supports multiple copies...
1181 copies
= atoi(argv
[4]);
1183 if (copies_sup
|| argc
< 7)
1184 copies_remaining
= 1;
1186 copies_remaining
= copies
;
1189 * Prepare remaining printing options...
1197 num_options
= cupsParseOptions(argv
[5], 0, &options
);
1199 if (!cups_version
&& media_col_sup
)
1202 * Load the PPD file and generate PWG attribute mapping information...
1205 ppd
= ppdOpenFile(getenv("PPD"));
1206 pc
= _ppdCacheCreateWithPPD(ppd
);
1214 document_format
= NULL
;
1216 if (format_sup
!= NULL
)
1218 for (i
= 0; i
< format_sup
->num_values
; i
++)
1219 if (!_cups_strcasecmp(final_content_type
,
1220 format_sup
->values
[i
].string
.text
))
1222 document_format
= final_content_type
;
1226 if (!document_format
)
1228 for (i
= 0; i
< format_sup
->num_values
; i
++)
1229 if (!_cups_strcasecmp("application/octet-stream",
1230 format_sup
->values
[i
].string
.text
))
1232 document_format
= "application/octet-stream";
1238 fprintf(stderr
, "DEBUG: final_content_type=\"%s\", document_format=\"%s\"\n",
1239 final_content_type
, document_format
? document_format
: "(null)");
1242 * If the printer does not support HTTP/1.1 (which IPP requires), copy stdin
1243 * to a temporary file so that we can do a HTTP/1.0 submission...
1245 * (I hate compatibility hacks!)
1248 if (http
->version
< HTTP_1_1
&& num_files
== 0)
1250 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
1252 perror("DEBUG: Unable to create temporary file");
1253 return (CUPS_BACKEND_FAILED
);
1256 _cupsLangPrintFilter(stderr
, "INFO", _("Copying print data."));
1258 if ((compatsize
= write(fd
, buffer
, bytes
)) < 0)
1260 perror("DEBUG: Unable to write temporary file");
1261 return (CUPS_BACKEND_FAILED
);
1264 if ((bytes
= backendRunLoop(-1, fd
, snmp_fd
, &(addrlist
->addr
), 0, 0,
1265 backendNetworkSideCB
)) < 0)
1266 return (CUPS_BACKEND_FAILED
);
1268 compatsize
+= bytes
;
1272 compatfile
= tmpfilename
;
1273 files
= &compatfile
;
1276 else if (http
->version
< HTTP_1_1
&& num_files
== 1)
1278 struct stat fileinfo
; /* File information */
1280 if (!stat(files
[0], &fileinfo
))
1281 compatsize
= fileinfo
.st_size
;
1285 * If the printer only claims to support IPP/1.0, or if the user specifically
1286 * included version=1.0 in the URI, then do not try to use Create-Job or
1287 * Send-Document. This is another dreaded compatibility hack, but unfortunately
1288 * there are enough broken printers out there that we need this for now...
1292 create_job
= send_document
= 0;
1295 * Start monitoring the printer in the background...
1299 monitor
.hostname
= hostname
;
1300 monitor
.user
= argv
[2];
1301 monitor
.resource
= resource
;
1302 monitor
.port
= port
;
1303 monitor
.version
= version
;
1305 monitor
.get_job_attrs
= get_job_attrs
;
1306 monitor
.encryption
= cupsEncryption();
1307 monitor
.job_state
= IPP_JOB_PENDING
;
1308 monitor
.printer_state
= IPP_PRINTER_IDLE
;
1312 monitor
.job_name
= argv
[3];
1316 snprintf(print_job_name
, sizeof(print_job_name
), "%s - %s", argv
[1],
1318 monitor
.job_name
= print_job_name
;
1321 _cupsThreadCreate((_cups_thread_func_t
)monitor_printer
, &monitor
);
1324 * Validate access to the printer...
1327 while (!job_canceled
&& validate_job
)
1329 request
= new_request(IPP_VALIDATE_JOB
, version
, uri
, argv
[2],
1330 monitor
.job_name
, num_options
, options
, compression
,
1331 copies_sup
? copies
: 1, document_format
, pc
,
1332 media_col_sup
, doc_handling_sup
);
1334 ippDelete(cupsDoRequest(http
, request
, resource
));
1336 ipp_status
= cupsLastError();
1338 fprintf(stderr
, "DEBUG: Validate-Job: %s (%s)\n",
1339 ippErrorString(ipp_status
), cupsLastErrorString());
1344 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
|| ipp_status
== IPP_PRINTER_BUSY
)
1346 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
1349 else if (ipp_status
== IPP_DOCUMENT_FORMAT
)
1351 else if (ipp_status
== IPP_FORBIDDEN
||
1352 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1354 const char *www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
1355 /* WWW-Authenticate field value */
1357 if (!strncmp(www_auth
, "Negotiate", 9))
1358 auth_info_required
= "negotiate";
1359 else if (www_auth
[0])
1360 auth_info_required
= "username,password";
1364 else if (ipp_status
== IPP_OPERATION_NOT_SUPPORTED
)
1367 * This is all too common...
1370 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1371 "cups-ipp-missing-validate-job");
1374 else if (ipp_status
< IPP_REDIRECTION_OTHER_SITE
||
1375 ipp_status
== IPP_BAD_REQUEST
)
1380 * Then issue the print-job request...
1385 while (!job_canceled
&& copies_remaining
> 0)
1388 * Check for side-channel requests...
1391 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1394 * Build the IPP job creation request...
1400 request
= new_request((num_files
> 1 || create_job
) ? IPP_CREATE_JOB
:
1402 version
, uri
, argv
[2], monitor
.job_name
, num_options
,
1403 options
, compression
, copies_sup
? copies
: 1,
1404 document_format
, pc
, media_col_sup
, doc_handling_sup
);
1410 if (num_files
> 1 || create_job
)
1411 response
= cupsDoRequest(http
, request
, resource
);
1414 size_t length
= 0; /* Length of request */
1418 fputs("DEBUG: Sending file using HTTP/1.0 Content-Length...\n", stderr
);
1419 length
= ippLength(request
) + (size_t)compatsize
;
1422 fputs("DEBUG: Sending file using HTTP/1.1 chunking...\n", stderr
);
1424 http_status
= cupsSendRequest(http
, request
, resource
, length
);
1425 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
)
1429 if ((fd
= open(files
[0], O_RDONLY
)) < 0)
1431 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1432 return (CUPS_BACKEND_FAILED
);
1438 http_status
= cupsWriteRequestData(http
, buffer
, bytes
);
1441 while (http_status
== HTTP_CONTINUE
&&
1442 (!job_canceled
|| compatsize
> 0))
1445 * Check for side-channel requests and more print data...
1450 FD_SET(snmp_fd
, &input
);
1452 while (select(fd
> snmp_fd
? fd
+ 1 : snmp_fd
+ 1, &input
, NULL
, NULL
,
1453 NULL
) <= 0 && !job_canceled
);
1455 if (FD_ISSET(snmp_fd
, &input
))
1456 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1458 if (FD_ISSET(fd
, &input
))
1460 if ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1462 fprintf(stderr
, "DEBUG: Read %d bytes...\n", (int)bytes
);
1464 if (cupsWriteRequestData(http
, buffer
, bytes
) != HTTP_CONTINUE
)
1467 else if (bytes
== 0 || (errno
!= EINTR
&& errno
!= EAGAIN
))
1476 response
= cupsGetResponse(http
, resource
);
1480 ipp_status
= cupsLastError();
1482 fprintf(stderr
, "DEBUG: %s: %s (%s)\n",
1483 (num_files
> 1 || create_job
) ? "Create-Job" : "Print-Job",
1484 ippErrorString(ipp_status
), cupsLastErrorString());
1486 if (ipp_status
> IPP_OK_CONFLICT
)
1493 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1494 ipp_status
== IPP_NOT_POSSIBLE
||
1495 ipp_status
== IPP_PRINTER_BUSY
)
1497 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
1503 * We can't re-submit when we have no files to print, so exit
1504 * immediately with the right status code...
1510 else if (ipp_status
== IPP_ERROR_JOB_CANCELED
||
1511 ipp_status
== IPP_NOT_AUTHORIZED
)
1516 * Update auth-info-required as needed...
1519 _cupsLangPrintFilter(stderr
, "ERROR",
1520 _("Print file was not accepted."));
1522 if (ipp_status
== IPP_FORBIDDEN
||
1523 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1525 const char *www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
1526 /* WWW-Authenticate field value */
1528 if (!strncmp(www_auth
, "Negotiate", 9))
1529 auth_info_required
= "negotiate";
1530 else if (www_auth
[0])
1531 auth_info_required
= "username,password";
1533 else if (ipp_status
== IPP_REQUEST_VALUE
)
1536 * Print file is too large, abort this job...
1547 * We can't re-submit when we have no files to print, so exit
1548 * immediately with the right status code...
1555 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
1556 IPP_TAG_INTEGER
)) == NULL
)
1558 _cupsLangPrintFilter(stderr
, "INFO",
1559 _("Print file accepted - job ID unknown."));
1560 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1561 "cups-ipp-missing-job-id");
1567 monitor
.job_id
= job_id
= job_id_attr
->values
[0].integer
;
1568 _cupsLangPrintFilter(stderr
, "INFO",
1569 _("Print file accepted - job ID %d."), job_id
);
1572 fprintf(stderr
, "DEBUG: job-id=%d\n", job_id
);
1573 ippDelete(response
);
1578 if (job_id
&& (num_files
> 1 || create_job
))
1580 for (i
= 0; num_files
== 0 || i
< num_files
; i
++)
1583 * Check for side-channel requests...
1586 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1589 * Send the next file in the job...
1592 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
1593 request
->request
.op
.version
[0] = version
/ 10;
1594 request
->request
.op
.version
[1] = version
% 10;
1596 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1599 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1603 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1604 "requesting-user-name", NULL
, argv
[2]);
1606 if ((i
+ 1) >= num_files
)
1607 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
1609 if (document_format
)
1610 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1611 "document-format", NULL
, document_format
);
1614 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1615 "compression", NULL
, compression
);
1617 fprintf(stderr
, "DEBUG: Sending file %d using chunking...\n", i
+ 1);
1618 http_status
= cupsSendRequest(http
, request
, resource
, 0);
1619 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
)
1624 http_status
= cupsWriteRequestData(http
, buffer
, bytes
);
1628 if ((fd
= open(files
[i
], O_RDONLY
)) < 0)
1630 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1631 return (CUPS_BACKEND_FAILED
);
1640 while (!job_canceled
&& http_status
== HTTP_CONTINUE
&&
1641 (bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1643 if ((http_status
= cupsWriteRequestData(http
, buffer
, bytes
))
1649 * Check for side-channel requests...
1652 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1660 ippDelete(cupsGetResponse(http
, resource
));
1663 fprintf(stderr
, "DEBUG: Send-Document: %s (%s)\n",
1664 ippErrorString(cupsLastError()), cupsLastErrorString());
1666 if (cupsLastError() > IPP_OK_CONFLICT
)
1668 ipp_status
= cupsLastError();
1670 _cupsLangPrintFilter(stderr
, "ERROR",
1671 _("Unable to add document to print job."));
1678 if (num_files
== 0 || fd
< 0)
1684 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
1686 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
1687 copies_remaining
--;
1689 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1690 ipp_status
== IPP_NOT_POSSIBLE
||
1691 ipp_status
== IPP_PRINTER_BUSY
)
1693 else if (ipp_status
== IPP_REQUEST_VALUE
||
1694 ipp_status
== IPP_ERROR_JOB_CANCELED
||
1695 ipp_status
== IPP_NOT_AUTHORIZED
||
1696 ipp_status
== IPP_INTERNAL_ERROR
)
1699 * Print file is too large, job was canceled, we need new
1700 * authentication data, or we had some sort of error...
1705 else if (ipp_status
== IPP_NOT_FOUND
)
1708 * Printer does not actually implement support for Create-Job/
1709 * Send-Document, so log the conformance issue and stop the printer.
1712 fputs("DEBUG: This printer claims to support Create-Job and "
1713 "Send-Document, but those operations failed.\n", stderr
);
1714 fputs("DEBUG: Add '?version=1.0' to the device URI to use legacy "
1715 "compatibility mode.\n", stderr
);
1716 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1717 "cups-ipp-missing-send-document");
1719 ipp_status
= IPP_INTERNAL_ERROR
; /* Force queue to stop */
1724 copies_remaining
--;
1727 * Wait for the job to complete...
1730 if (!job_id
|| !waitjob
|| !get_job_attrs
)
1733 _cupsLangPrintFilter(stderr
, "INFO", _("Waiting for job to complete."));
1735 for (delay
= _cupsNextDelay(0, &prev_delay
); !job_canceled
;)
1738 * Check for side-channel requests...
1741 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1744 * Build an IPP_GET_JOB_ATTRIBUTES request...
1747 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1748 request
->request
.op
.version
[0] = version
/ 10;
1749 request
->request
.op
.version
[1] = version
% 10;
1751 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1754 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1758 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1759 "requesting-user-name", NULL
, argv
[2]);
1761 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1762 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
1769 httpReconnect(http
);
1770 response
= cupsDoRequest(http
, request
, resource
);
1771 ipp_status
= cupsLastError();
1773 if (ipp_status
== IPP_NOT_FOUND
|| ipp_status
== IPP_NOT_POSSIBLE
)
1776 * Job has gone away and/or the server has no job history...
1779 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1780 "cups-ipp-missing-job-history");
1781 ippDelete(response
);
1783 ipp_status
= IPP_OK
;
1787 fprintf(stderr
, "DEBUG: Get-Job-Attributes: %s (%s)\n",
1788 ippErrorString(ipp_status
), cupsLastErrorString());
1790 if (ipp_status
<= IPP_OK_CONFLICT
)
1794 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1795 ipp_status
!= IPP_PRINTER_BUSY
)
1797 ippDelete(response
);
1798 ipp_status
= IPP_OK
;
1805 if ((job_state
= ippFindAttribute(response
, "job-state",
1806 IPP_TAG_ENUM
)) != NULL
)
1809 * Reflect the remote job state in the local queue...
1813 job_state
->values
[0].integer
>= IPP_JOB_PENDING
&&
1814 job_state
->values
[0].integer
<= IPP_JOB_COMPLETED
)
1815 update_reasons(NULL
,
1816 remote_job_states
[job_state
->values
[0].integer
-
1819 if ((job_sheets
= ippFindAttribute(response
,
1820 "job-media-sheets-completed",
1821 IPP_TAG_INTEGER
)) == NULL
)
1822 job_sheets
= ippFindAttribute(response
,
1823 "job-impressions-completed",
1827 fprintf(stderr
, "PAGE: total %d\n",
1828 job_sheets
->values
[0].integer
);
1831 * Stop polling if the job is finished or pending-held...
1834 if (job_state
->values
[0].integer
> IPP_JOB_STOPPED
)
1836 ippDelete(response
);
1840 else if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1841 ipp_status
!= IPP_NOT_POSSIBLE
&&
1842 ipp_status
!= IPP_PRINTER_BUSY
)
1845 * If the printer does not return a job-state attribute, it does not
1846 * conform to the IPP specification - break out immediately and fail
1850 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1851 "cups-ipp-missing-job-state");
1852 ipp_status
= IPP_INTERNAL_ERROR
;
1857 ippDelete(response
);
1860 * Wait before polling again...
1865 delay
= _cupsNextDelay(delay
, &prev_delay
);
1870 * Cancel the job as needed...
1873 if (job_canceled
> 0 && job_id
> 0)
1874 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1877 * Check the printer state and report it if necessary...
1880 check_printer_state(http
, uri
, resource
, argv
[2], version
);
1883 * Collect the final page count as needed...
1886 if (have_supplies
&&
1887 !backendSNMPSupplies(snmp_fd
, http
->hostaddr
, &page_count
, NULL
) &&
1888 page_count
> start_count
)
1889 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
1893 * See if we used Kerberos at all...
1897 auth_info_required
= "negotiate";
1898 #endif /* HAVE_GSSAPI */
1906 cupsFreeOptions(num_options
, options
);
1907 _ppdCacheDestroy(pc
);
1911 ippDelete(supported
);
1914 * Remove the temporary file(s) if necessary...
1918 unlink(tmpfilename
);
1923 for (i
= 0; i
< num_files
; i
++)
1926 #endif /* HAVE_LIBZ */
1929 * Return the queue status...
1932 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
1933 ipp_status
== IPP_AUTHENTICATION_CANCELED
||
1934 ipp_status
<= IPP_OK_CONFLICT
)
1935 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
1937 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
1938 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1939 return (CUPS_BACKEND_AUTH_REQUIRED
);
1940 else if (ipp_status
== IPP_INTERNAL_ERROR
)
1941 return (CUPS_BACKEND_STOP
);
1942 else if (ipp_status
== IPP_CONFLICT
)
1943 return (CUPS_BACKEND_FAILED
);
1944 else if (ipp_status
== IPP_REQUEST_VALUE
||
1945 ipp_status
== IPP_DOCUMENT_FORMAT
|| job_canceled
< 0)
1947 if (ipp_status
== IPP_REQUEST_VALUE
)
1948 _cupsLangPrintFilter(stderr
, "ERROR", _("Print job too large."));
1949 else if (ipp_status
== IPP_DOCUMENT_FORMAT
)
1950 _cupsLangPrintFilter(stderr
, "ERROR",
1951 _("Printer cannot print supplied content."));
1953 _cupsLangPrintFilter(stderr
, "ERROR", _("Print job canceled at printer."));
1955 return (CUPS_BACKEND_CANCEL
);
1957 else if (ipp_status
> IPP_OK_CONFLICT
&& ipp_status
!= IPP_ERROR_JOB_CANCELED
)
1958 return (CUPS_BACKEND_RETRY_CURRENT
);
1960 return (CUPS_BACKEND_OK
);
1965 * 'cancel_job()' - Cancel a print job.
1969 cancel_job(http_t
*http
, /* I - HTTP connection */
1970 const char *uri
, /* I - printer-uri */
1971 int id
, /* I - job-id */
1972 const char *resource
, /* I - Resource path */
1973 const char *user
, /* I - requesting-user-name */
1974 int version
) /* I - IPP version */
1976 ipp_t
*request
; /* Cancel-Job request */
1979 _cupsLangPrintFilter(stderr
, "INFO", _("Canceling print job."));
1981 request
= ippNewRequest(IPP_CANCEL_JOB
);
1982 request
->request
.op
.version
[0] = version
/ 10;
1983 request
->request
.op
.version
[1] = version
% 10;
1985 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1987 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1989 if (user
&& user
[0])
1990 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1991 "requesting-user-name", NULL
, user
);
1997 ippDelete(cupsDoRequest(http
, request
, resource
));
1999 if (cupsLastError() > IPP_OK_CONFLICT
)
2000 _cupsLangPrintFilter(stderr
, "ERROR", _("Unable to cancel print job."));
2005 * 'check_printer_state()' - Check the printer state.
2008 static ipp_pstate_t
/* O - Current printer-state */
2009 check_printer_state(
2010 http_t
*http
, /* I - HTTP connection */
2011 const char *uri
, /* I - Printer URI */
2012 const char *resource
, /* I - Resource path */
2013 const char *user
, /* I - Username, if any */
2014 int version
) /* I - IPP version */
2016 ipp_t
*request
, /* IPP request */
2017 *response
; /* IPP response */
2018 ipp_attribute_t
*attr
; /* Attribute in response */
2019 ipp_pstate_t printer_state
= IPP_PRINTER_STOPPED
;
2020 /* Current printer-state */
2024 * Send a Get-Printer-Attributes request and log the results...
2027 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
2028 request
->request
.op
.version
[0] = version
/ 10;
2029 request
->request
.op
.version
[1] = version
% 10;
2031 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2034 if (user
&& user
[0])
2035 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2036 "requesting-user-name", NULL
, user
);
2038 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2039 "requested-attributes",
2040 (int)(sizeof(pattrs
) / sizeof(pattrs
[0])), NULL
, pattrs
);
2042 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
2044 report_printer_state(response
);
2046 if ((attr
= ippFindAttribute(response
, "printer-state",
2047 IPP_TAG_ENUM
)) != NULL
)
2048 printer_state
= (ipp_pstate_t
)attr
->values
[0].integer
;
2050 ippDelete(response
);
2053 fprintf(stderr
, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
2054 ippErrorString(cupsLastError()), cupsLastErrorString());
2056 if (cupsLastError() <= IPP_OK_CONFLICT
)
2060 * Return the printer-state value...
2063 return (printer_state
);
2069 * 'compress_files()' - Compress print files.
2073 compress_files(int num_files
, /* I - Number of files */
2074 char **files
) /* I - Files */
2076 int i
, /* Looping var */
2077 fd
; /* Temporary file descriptor */
2078 ssize_t bytes
; /* Bytes read/written */
2079 size_t total
; /* Total bytes read */
2080 cups_file_t
*in
, /* Input file */
2081 *out
; /* Output file */
2082 struct stat outinfo
; /* Output file information */
2083 char filename
[1024], /* Temporary filename */
2084 buffer
[32768]; /* Copy buffer */
2087 fprintf(stderr
, "DEBUG: Compressing %d job files...\n", num_files
);
2088 for (i
= 0; i
< num_files
; i
++)
2090 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
2092 _cupsLangPrintError("ERROR", _("Unable to create compressed print file"));
2093 exit(CUPS_BACKEND_FAILED
);
2096 if ((out
= cupsFileOpenFd(fd
, "w9")) == NULL
)
2098 _cupsLangPrintError("ERROR", _("Unable to open compressed print file"));
2099 exit(CUPS_BACKEND_FAILED
);
2102 if ((in
= cupsFileOpen(files
[i
], "r")) == NULL
)
2104 _cupsLangPrintError("ERROR", _("Unable to open print file"));
2106 exit(CUPS_BACKEND_FAILED
);
2110 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
2111 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
2113 _cupsLangPrintError("ERROR",
2114 _("Unable to generate compressed print file"));
2117 exit(CUPS_BACKEND_FAILED
);
2125 files
[i
] = strdup(filename
);
2127 if (!stat(filename
, &outinfo
))
2129 "DEBUG: File %d compressed to %.1f%% of original size, "
2130 CUPS_LLFMT
" bytes...\n",
2131 i
+ 1, 100.0 * outinfo
.st_size
/ total
,
2132 CUPS_LLCAST outinfo
.st_size
);
2135 #endif /* HAVE_LIBZ */
2139 * 'monitor_printer()' - Monitor the printer state.
2142 static void * /* O - Thread exit code */
2144 _cups_monitor_t
*monitor
) /* I - Monitoring data */
2146 http_t
*http
; /* Connection to printer */
2147 ipp_t
*request
, /* IPP request */
2148 *response
; /* IPP response */
2149 ipp_attribute_t
*attr
; /* Attribute in response */
2150 int delay
, /* Current delay */
2151 prev_delay
; /* Previous delay */
2152 ipp_op_t job_op
; /* Operation to use */
2153 int job_id
; /* Job ID */
2154 const char *job_name
; /* Job name */
2155 ipp_jstate_t job_state
; /* Job state */
2156 const char *job_user
; /* Job originating user name */
2160 * Make a copy of the printer connection...
2163 http
= _httpCreate(monitor
->hostname
, monitor
->port
, NULL
, monitor
->encryption
,
2165 httpSetTimeout(http
, 30.0, timeout_cb
, NULL
);
2167 cupsSetUser(username
);
2168 cupsSetPasswordCB2(password_cb
, NULL
);
2171 * Loop until the job is canceled, aborted, or completed.
2174 delay
= _cupsNextDelay(0, &prev_delay
);
2176 while (monitor
->job_state
< IPP_JOB_CANCELED
&& !job_canceled
)
2179 * Reconnect to the printer...
2182 if (!httpReconnect(http
))
2185 * Connected, so check on the printer state...
2188 monitor
->printer_state
= check_printer_state(http
, monitor
->uri
,
2194 * Check the status of the job itself...
2197 job_op
= (monitor
->job_id
> 0 && monitor
->get_job_attrs
) ?
2198 IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
;
2199 request
= ippNewRequest(job_op
);
2200 request
->request
.op
.version
[0] = monitor
->version
/ 10;
2201 request
->request
.op
.version
[1] = monitor
->version
% 10;
2203 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2204 NULL
, monitor
->uri
);
2205 if (job_op
== IPP_GET_JOB_ATTRIBUTES
)
2206 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
2209 if (monitor
->user
&& monitor
->user
[0])
2210 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2211 "requesting-user-name", NULL
, monitor
->user
);
2213 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2214 "requested-attributes",
2215 (int)(sizeof(jattrs
) / sizeof(jattrs
[0])), NULL
, jattrs
);
2221 response
= cupsDoRequest(http
, request
, monitor
->resource
);
2223 fprintf(stderr
, "DEBUG: (monitor) %s: %s (%s)\n", ippOpString(job_op
),
2224 ippErrorString(cupsLastError()), cupsLastErrorString());
2226 if (cupsLastError() <= IPP_OK_CONFLICT
)
2229 if (job_op
== IPP_GET_JOB_ATTRIBUTES
)
2231 if ((attr
= ippFindAttribute(response
, "job-state",
2232 IPP_TAG_ENUM
)) != NULL
)
2233 monitor
->job_state
= (ipp_jstate_t
)attr
->values
[0].integer
;
2235 monitor
->job_state
= IPP_JOB_COMPLETED
;
2239 for (attr
= response
->attrs
; attr
; attr
= attr
->next
)
2243 job_state
= IPP_JOB_PENDING
;
2246 while (attr
&& attr
->group_tag
!= IPP_TAG_JOB
)
2252 while (attr
&& attr
->group_tag
== IPP_TAG_JOB
)
2254 if (!strcmp(attr
->name
, "job-id") &&
2255 attr
->value_tag
== IPP_TAG_INTEGER
)
2256 job_id
= attr
->values
[0].integer
;
2257 else if (!strcmp(attr
->name
, "job-name") &&
2258 (attr
->value_tag
== IPP_TAG_NAME
||
2259 attr
->value_tag
== IPP_TAG_NAMELANG
))
2260 job_name
= attr
->values
[0].string
.text
;
2261 else if (!strcmp(attr
->name
, "job-state") &&
2262 attr
->value_tag
== IPP_TAG_ENUM
)
2263 job_state
= attr
->values
[0].integer
;
2264 else if (!strcmp(attr
->name
, "job-originating-user-name") &&
2265 (attr
->value_tag
== IPP_TAG_NAME
||
2266 attr
->value_tag
== IPP_TAG_NAMELANG
))
2267 job_user
= attr
->values
[0].string
.text
;
2272 if (job_id
> 0 && job_name
&& !strcmp(job_name
, monitor
->job_name
) &&
2273 job_user
&& monitor
->user
&& !strcmp(job_user
, monitor
->user
))
2275 monitor
->job_id
= job_id
;
2276 monitor
->job_state
= job_state
;
2285 ippDelete(response
);
2287 fprintf(stderr
, "DEBUG: (monitor) job-state=%s\n",
2288 ippEnumString("job-state", monitor
->job_state
));
2290 if (!job_canceled
&&
2291 (monitor
->job_state
== IPP_JOB_CANCELED
||
2292 monitor
->job_state
== IPP_JOB_ABORTED
))
2296 * Disconnect from the printer - we'll reconnect on the next poll...
2299 _httpDisconnect(http
);
2303 * Sleep for N seconds...
2308 delay
= _cupsNextDelay(delay
, &prev_delay
);
2312 * Cancel the job if necessary...
2315 if (job_canceled
> 0 && monitor
->job_id
> 0)
2316 if (!httpReconnect(http
))
2317 cancel_job(http
, monitor
->uri
, monitor
->job_id
, monitor
->resource
,
2318 monitor
->user
, monitor
->version
);
2321 * Cleanup and return...
2331 * 'new_request()' - Create a new print creation or validation request.
2334 static ipp_t
* /* O - Request data */
2336 ipp_op_t op
, /* I - IPP operation code */
2337 int version
, /* I - IPP version number */
2338 const char *uri
, /* I - printer-uri value */
2339 const char *user
, /* I - requesting-user-name value */
2340 const char *title
, /* I - job-name value */
2341 int num_options
, /* I - Number of options to send */
2342 cups_option_t
*options
, /* I - Options to send */
2343 const char *compression
, /* I - compression value or NULL */
2344 int copies
, /* I - copies value or 0 */
2345 const char *format
, /* I - document-format value or NULL */
2346 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2347 ipp_attribute_t
*media_col_sup
, /* I - media-col-supported values */
2348 ipp_attribute_t
*doc_handling_sup
) /* I - multiple-document-handling-supported values */
2350 int i
; /* Looping var */
2351 ipp_t
*request
; /* Request data */
2352 const char *keyword
; /* PWG keyword */
2353 _pwg_size_t
*size
; /* PWG media size */
2354 ipp_t
*media_col
, /* media-col value */
2355 *media_size
; /* media-size value */
2356 const char *media_source
, /* media-source value */
2357 *media_type
, /* media-type value */
2358 *collate_str
, /* multiple-document-handling value */
2359 *mandatory
; /* Mandatory attributes */
2363 * Create the IPP request...
2366 request
= ippNewRequest(op
);
2367 request
->request
.op
.version
[0] = version
/ 10;
2368 request
->request
.op
.version
[1] = version
% 10;
2370 fprintf(stderr
, "DEBUG: %s IPP/%d.%d\n",
2371 ippOpString(request
->request
.op
.operation_id
),
2372 request
->request
.op
.version
[0],
2373 request
->request
.op
.version
[1]);
2376 * Add standard attributes...
2379 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2381 fprintf(stderr
, "DEBUG: printer-uri=\"%s\"\n", uri
);
2385 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2386 "requesting-user-name", NULL
, user
);
2387 fprintf(stderr
, "DEBUG: requesting-user-name=\"%s\"\n", user
);
2390 if (title
&& *title
)
2392 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
2394 fprintf(stderr
, "DEBUG: job-name=\"%s\"\n", title
);
2397 if (format
&& op
!= IPP_CREATE_JOB
)
2399 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
2400 "document-format", NULL
, format
);
2401 fprintf(stderr
, "DEBUG: document-format=\"%s\"\n", format
);
2405 if (compression
&& op
!= IPP_CREATE_JOB
)
2407 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2408 "compression", NULL
, compression
);
2409 fprintf(stderr
, "DEBUG: compression=\"%s\"\n", compression
);
2411 #endif /* HAVE_LIBZ */
2414 * Handle options on the command-line...
2417 if (num_options
> 0)
2421 int num_finishings
= 0, /* Number of finishing values */
2422 finishings
[10]; /* Finishing enum values */
2425 * Send standard IPP attributes...
2429 (keyword
= cupsGetOption("job-password", num_options
,
2432 ippAddOctetString(request
, IPP_TAG_OPERATION
, "job-password",
2433 keyword
, strlen(keyword
));
2434 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2435 "job-password-encryption", NULL
, "none");
2438 if (pc
->account_id
&&
2439 (keyword
= cupsGetOption("job-account-id", num_options
,
2441 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-account-id",
2444 if (pc
->account_id
&&
2445 (keyword
= cupsGetOption("job-accounting-user-id", num_options
,
2447 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_NAME
,
2448 "job-accounting-user-id", NULL
, keyword
);
2450 for (mandatory
= (char *)cupsArrayFirst(pc
->mandatory
);
2452 mandatory
= (char *)cupsArrayNext(pc
->mandatory
))
2454 if (strcmp(mandatory
, "copies") &&
2455 strcmp(mandatory
, "destination-uris") &&
2456 strcmp(mandatory
, "finishings") &&
2457 strcmp(mandatory
, "job-account-id") &&
2458 strcmp(mandatory
, "job-accounting-user-id") &&
2459 strcmp(mandatory
, "job-password") &&
2460 strcmp(mandatory
, "media") &&
2461 strncmp(mandatory
, "media-col", 9) &&
2462 strcmp(mandatory
, "multiple-document-handling") &&
2463 strcmp(mandatory
, "output-bin") &&
2464 strcmp(mandatory
, "print-color-mode") &&
2465 strcmp(mandatory
, "print-quality") &&
2466 strcmp(mandatory
, "sides") &&
2467 (keyword
= cupsGetOption(mandatory
, num_options
, options
)) != NULL
)
2469 _ipp_option_t
*opt
= _ippFindOption(mandatory
);
2471 ipp_tag_t value_tag
= opt
? opt
->value_tag
: IPP_TAG_NAME
;
2476 case IPP_TAG_INTEGER
:
2478 ippAddInteger(request
, IPP_TAG_JOB
, value_tag
, mandatory
,
2481 case IPP_TAG_BOOLEAN
:
2482 ippAddBoolean(request
, IPP_TAG_JOB
, mandatory
,
2483 !_cups_strcasecmp(keyword
, "true"));
2485 case IPP_TAG_RANGE
:
2487 int lower
, upper
; /* Range */
2489 if (sscanf(keyword
, "%d-%d", &lower
, &upper
) != 2)
2490 lower
= upper
= atoi(keyword
);
2492 ippAddRange(request
, IPP_TAG_JOB
, mandatory
, lower
, upper
);
2495 case IPP_TAG_STRING
:
2496 ippAddOctetString(request
, IPP_TAG_JOB
, mandatory
, keyword
,
2500 ippAddString(request
, IPP_TAG_JOB
, value_tag
, mandatory
,
2507 if ((keyword
= cupsGetOption("PageSize", num_options
, options
)) == NULL
)
2508 keyword
= cupsGetOption("media", num_options
, options
);
2510 if ((size
= _ppdCacheGetSize(pc
, keyword
)) != NULL
)
2513 * Add a media-col value...
2516 media_size
= ippNew();
2517 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2518 "x-dimension", size
->width
);
2519 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2520 "y-dimension", size
->length
);
2522 media_col
= ippNew();
2523 ippAddCollection(media_col
, IPP_TAG_ZERO
, "media-size", media_size
);
2525 media_source
= _ppdCacheGetSource(pc
, cupsGetOption("InputSlot",
2528 media_type
= _ppdCacheGetType(pc
, cupsGetOption("MediaType",
2532 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
2534 if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2535 "media-left-margin"))
2536 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2537 "media-left-margin", size
->left
);
2538 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2539 "media-bottom-margin"))
2540 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2541 "media-bottom-margin", size
->bottom
);
2542 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2543 "media-right-margin"))
2544 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2545 "media-right-margin", size
->right
);
2546 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2547 "media-top-margin"))
2548 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2549 "media-top-margin", size
->top
);
2550 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2551 "media-source") && media_source
)
2552 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
2553 "media-source", NULL
, media_source
);
2554 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2555 "media-type") && media_type
)
2556 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
2557 "media-type", NULL
, media_type
);
2560 ippAddCollection(request
, IPP_TAG_JOB
, "media-col", media_col
);
2563 if ((keyword
= cupsGetOption("output-bin", num_options
,
2565 keyword
= _ppdCacheGetBin(pc
, cupsGetOption("OutputBin", num_options
,
2569 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-bin",
2572 if ((keyword
= cupsGetOption("print-color-mode", num_options
,
2574 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "print-color-mode",
2576 else if ((keyword
= cupsGetOption("ColorModel", num_options
,
2579 if (!_cups_strcasecmp(keyword
, "Gray"))
2580 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2581 "print-color-mode", NULL
, "monochrome");
2583 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2584 "print-color-mode", NULL
, "color");
2587 if ((keyword
= cupsGetOption("print-quality", num_options
,
2589 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2591 else if ((keyword
= cupsGetOption("cupsPrintQuality", num_options
,
2594 if (!_cups_strcasecmp(keyword
, "draft"))
2595 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2597 else if (!_cups_strcasecmp(keyword
, "normal"))
2598 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2599 IPP_QUALITY_NORMAL
);
2600 else if (!_cups_strcasecmp(keyword
, "high"))
2601 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2605 if ((keyword
= cupsGetOption("sides", num_options
, options
)) != NULL
)
2606 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2608 else if (pc
->sides_option
&&
2609 (keyword
= cupsGetOption(pc
->sides_option
, num_options
,
2612 if (!_cups_strcasecmp(keyword
, pc
->sides_1sided
))
2613 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2615 else if (!_cups_strcasecmp(keyword
, pc
->sides_2sided_long
))
2616 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2617 NULL
, "two-sided-long-edge");
2618 if (!_cups_strcasecmp(keyword
, pc
->sides_2sided_short
))
2619 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2620 NULL
, "two-sided-short-edge");
2623 if ((keyword
= cupsGetOption("multiple-document-handling",
2624 num_options
, options
)) != NULL
)
2626 if (strstr(keyword
, "uncollated"))
2631 else if ((keyword
= cupsGetOption("collate", num_options
,
2637 if (!_cups_strcasecmp(format
, "image/gif") ||
2638 !_cups_strcasecmp(format
, "image/jp2") ||
2639 !_cups_strcasecmp(format
, "image/jpeg") ||
2640 !_cups_strcasecmp(format
, "image/png") ||
2641 !_cups_strcasecmp(format
, "image/tiff") ||
2642 !_cups_strncasecmp(format
, "image/x-", 8))
2645 * Collation makes no sense for single page image formats...
2650 else if (!_cups_strncasecmp(format
, "image/", 6) ||
2651 !_cups_strcasecmp(format
, "application/vnd.cups-raster"))
2654 * Multi-page image formats will have copies applied by the upstream
2662 if (doc_handling_sup
)
2664 if (!_cups_strcasecmp(keyword
, "true"))
2665 collate_str
= "separate-documents-collated-copies";
2667 collate_str
= "separate-documents-uncollated-copies";
2669 for (i
= 0; i
< doc_handling_sup
->num_values
; i
++)
2670 if (!strcmp(doc_handling_sup
->values
[i
].string
.text
, collate_str
))
2672 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2673 "multiple-document-handling", NULL
, collate_str
);
2677 if (i
>= doc_handling_sup
->num_values
)
2682 * Map finishing options...
2685 num_finishings
= _ppdCacheGetFinishingValues(pc
, num_options
, options
,
2686 (int)(sizeof(finishings
) /
2687 sizeof(finishings
[0])),
2689 if (num_finishings
> 0)
2690 ippAddIntegers(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "finishings",
2691 num_finishings
, finishings
);
2694 * Map FaxOut options...
2697 if ((keyword
= cupsGetOption("phone", num_options
, options
)) != NULL
)
2699 ipp_t
*destination
; /* destination collection */
2700 char tel_uri
[1024]; /* tel: URI */
2702 destination
= ippNew();
2704 httpAssembleURI(HTTP_URI_CODING_ALL
, tel_uri
, sizeof(tel_uri
), "tel",
2705 NULL
, NULL
, 0, keyword
);
2706 ippAddString(destination
, IPP_TAG_JOB
, IPP_TAG_URI
, "destination-uri",
2709 if ((keyword
= cupsGetOption("faxPrefix", num_options
,
2710 options
)) != NULL
&& *keyword
)
2711 ippAddString(destination
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
2712 "pre-dial-string", NULL
, keyword
);
2714 ippAddCollection(request
, IPP_TAG_JOB
, "destination-uris", destination
);
2715 ippDelete(destination
);
2721 * When talking to another CUPS server, send all options...
2724 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_JOB
);
2727 if (copies
> 1 && (!pc
|| copies
<= pc
->max_copies
))
2728 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies", copies
);
2736 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
2739 static const char * /* O - Password */
2740 password_cb(const char *prompt
, /* I - Prompt (not used) */
2741 http_t
*http
, /* I - Connection */
2742 const char *method
, /* I - Request method (not used) */
2743 const char *resource
, /* I - Resource path (not used) */
2744 void *user_data
) /* I - User data (not used) */
2746 char def_username
[HTTP_MAX_VALUE
]; /* Default username */
2749 fprintf(stderr
, "DEBUG: password_cb(prompt=\"%s\"), password=%p, "
2750 "password_tries=%d\n", prompt
, password
, password_tries
);
2758 * Remember that we need to authenticate...
2761 auth_info_required
= "username,password";
2763 if (httpGetSubField(http
, HTTP_FIELD_WWW_AUTHENTICATE
, "username",
2766 char quoted
[HTTP_MAX_VALUE
* 2 + 4];
2769 fprintf(stderr
, "ATTR: auth-info-default=%s,\n",
2770 quote_string(def_username
, quoted
, sizeof(quoted
)));
2773 if (password
&& *password
&& password_tries
< 3)
2782 * Give up after 3 tries or if we don't have a password to begin with...
2791 * 'quote_string()' - Quote a string value.
2794 static const char * /* O - Quoted string */
2795 quote_string(const char *s
, /* I - String */
2796 char *q
, /* I - Quoted string buffer */
2797 size_t qsize
) /* I - Size of quoted string buffer */
2799 char *qptr
, /* Pointer into string buffer */
2800 *qend
; /* End of string buffer */
2804 qend
= q
+ qsize
- 5;
2815 while (*s
&& qptr
< qend
)
2817 if (*s
== '\\' || *s
== '\"' || *s
== '\'')
2841 * 'report_attr()' - Report an IPP attribute value.
2845 report_attr(ipp_attribute_t
*attr
) /* I - Attribute */
2847 int i
; /* Looping var */
2848 char value
[1024], /* Value string */
2849 *valptr
; /* Pointer into value string */
2850 const char *cached
; /* Cached attribute */
2854 * Convert the attribute values into quoted strings...
2857 for (i
= 0, valptr
= value
;
2858 i
< attr
->num_values
&& valptr
< (value
+ sizeof(value
) - 10);
2864 switch (attr
->value_tag
)
2866 case IPP_TAG_INTEGER
:
2868 snprintf(valptr
, sizeof(value
) - (valptr
- value
), "%d",
2869 attr
->values
[i
].integer
);
2870 valptr
+= strlen(valptr
);
2875 case IPP_TAG_KEYWORD
:
2876 quote_string(attr
->values
[i
].string
.text
, valptr
,
2877 value
+ sizeof(value
) - valptr
);
2878 valptr
+= strlen(valptr
);
2883 * Unsupported value type...
2892 _cupsMutexLock(&report_mutex
);
2894 if ((cached
= cupsGetOption(attr
->name
, num_attr_cache
,
2895 attr_cache
)) == NULL
|| strcmp(cached
, value
))
2898 * Tell the scheduler about the new values...
2901 num_attr_cache
= cupsAddOption(attr
->name
, value
, num_attr_cache
,
2903 fprintf(stderr
, "ATTR: %s=%s\n", attr
->name
, value
);
2906 _cupsMutexUnlock(&report_mutex
);
2911 * 'report_printer_state()' - Report the printer state.
2915 report_printer_state(ipp_t
*ipp
) /* I - IPP response */
2917 ipp_attribute_t
*pa
, /* printer-alert */
2918 *pam
, /* printer-alert-message */
2919 *psm
, /* printer-state-message */
2920 *reasons
, /* printer-state-reasons */
2921 *marker
; /* marker-* attributes */
2922 char value
[1024], /* State/message string */
2923 *valptr
; /* Pointer into string */
2924 static int ipp_supplies
= -1;
2925 /* Report supply levels? */
2929 * Report alerts and messages...
2932 if ((pa
= ippFindAttribute(ipp
, "printer-alert", IPP_TAG_TEXT
)) != NULL
)
2935 if ((pam
= ippFindAttribute(ipp
, "printer-alert-message",
2936 IPP_TAG_TEXT
)) != NULL
)
2939 if ((psm
= ippFindAttribute(ipp
, "printer-state-message",
2940 IPP_TAG_TEXT
)) != NULL
)
2942 char *ptr
; /* Pointer into message */
2945 strlcpy(value
, "INFO: ", sizeof(value
));
2946 for (ptr
= psm
->values
[0].string
.text
, valptr
= value
+ 6;
2947 *ptr
&& valptr
< (value
+ sizeof(value
) - 6);
2950 if (*ptr
< ' ' && *ptr
> 0 && *ptr
!= '\t')
2953 * Substitute "<XX>" for the control character; sprintf is safe because
2954 * we always leave 6 chars free at the end...
2957 sprintf(valptr
, "<%02X>", *ptr
);
2967 fputs(value
, stderr
);
2971 * Now report printer-state-reasons, filtering out some of the reasons we never
2975 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
2976 IPP_TAG_KEYWORD
)) == NULL
)
2979 update_reasons(reasons
, NULL
);
2982 * Relay the current marker-* attribute values...
2985 if (ipp_supplies
< 0)
2987 ppd_file_t
*ppd
; /* PPD file */
2988 ppd_attr_t
*ppdattr
; /* Attribute in PPD file */
2990 if ((ppd
= ppdOpenFile(getenv("PPD"))) != NULL
&&
2991 (ppdattr
= ppdFindAttr(ppd
, "cupsIPPSupplies", NULL
)) != NULL
&&
2992 ppdattr
->value
&& _cups_strcasecmp(ppdattr
->value
, "true"))
3000 if (ipp_supplies
> 0)
3002 if ((marker
= ippFindAttribute(ipp
, "marker-colors", IPP_TAG_NAME
)) != NULL
)
3003 report_attr(marker
);
3004 if ((marker
= ippFindAttribute(ipp
, "marker-high-levels",
3005 IPP_TAG_INTEGER
)) != NULL
)
3006 report_attr(marker
);
3007 if ((marker
= ippFindAttribute(ipp
, "marker-levels",
3008 IPP_TAG_INTEGER
)) != NULL
)
3009 report_attr(marker
);
3010 if ((marker
= ippFindAttribute(ipp
, "marker-low-levels",
3011 IPP_TAG_INTEGER
)) != NULL
)
3012 report_attr(marker
);
3013 if ((marker
= ippFindAttribute(ipp
, "marker-message",
3014 IPP_TAG_TEXT
)) != NULL
)
3015 report_attr(marker
);
3016 if ((marker
= ippFindAttribute(ipp
, "marker-names", IPP_TAG_NAME
)) != NULL
)
3017 report_attr(marker
);
3018 if ((marker
= ippFindAttribute(ipp
, "marker-types",
3019 IPP_TAG_KEYWORD
)) != NULL
)
3020 report_attr(marker
);
3025 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
3027 * 'run_as_user()' - Run the IPP backend as the printing user.
3029 * This function uses an XPC-based user agent to run the backend as the printing
3030 * user. We need to do this in order to have access to the user's Kerberos
3034 static int /* O - Exit status */
3035 run_as_user(char *argv
[], /* I - Command-line arguments */
3036 uid_t uid
, /* I - User ID */
3037 const char *device_uri
, /* I - Device URI */
3038 int fd
) /* I - File to print */
3040 const char *auth_negotiate
;/* AUTH_NEGOTIATE env var */
3041 xpc_connection_t conn
; /* Connection to XPC service */
3042 xpc_object_t request
; /* Request message dictionary */
3043 __block xpc_object_t response
; /* Response message dictionary */
3044 dispatch_semaphore_t sem
; /* Semaphore for waiting for response */
3045 int status
= CUPS_BACKEND_FAILED
;
3046 /* Status of request */
3049 fprintf(stderr
, "DEBUG: Running IPP backend as UID %d.\n", (int)uid
);
3052 * Connect to the user agent for the specified UID...
3055 conn
= xpc_connection_create_mach_service(kPMPrintUIToolAgent
,
3056 dispatch_get_global_queue(0, 0), 0);
3059 _cupsLangPrintFilter(stderr
, "ERROR",
3060 _("Unable to start backend process."));
3061 fputs("DEBUG: Unable to create connection to agent.\n", stderr
);
3065 xpc_connection_set_event_handler(conn
,
3066 ^(xpc_object_t event
)
3068 xpc_type_t messageType
= xpc_get_type(event
);
3070 if (messageType
== XPC_TYPE_ERROR
)
3072 if (event
== XPC_ERROR_CONNECTION_INTERRUPTED
)
3073 fprintf(stderr
, "DEBUG: Interrupted connection to service %s.\n",
3074 xpc_connection_get_name(conn
));
3075 else if (event
== XPC_ERROR_CONNECTION_INVALID
)
3076 fprintf(stderr
, "DEBUG: Connection invalid for service %s.\n",
3077 xpc_connection_get_name(conn
));
3079 fprintf(stderr
, "DEBUG: Unxpected error for service %s: %s\n",
3080 xpc_connection_get_name(conn
),
3081 xpc_dictionary_get_string(event
, XPC_ERROR_KEY_DESCRIPTION
));
3084 xpc_connection_set_target_uid(conn
, uid
);
3085 xpc_connection_resume(conn
);
3088 * Try starting the backend...
3091 request
= xpc_dictionary_create(NULL
, NULL
, 0);
3092 xpc_dictionary_set_int64(request
, "command", kPMStartJob
);
3093 xpc_dictionary_set_string(request
, "device-uri", device_uri
);
3094 xpc_dictionary_set_string(request
, "job-id", argv
[1]);
3095 xpc_dictionary_set_string(request
, "user", argv
[2]);
3096 xpc_dictionary_set_string(request
, "title", argv
[3]);
3097 xpc_dictionary_set_string(request
, "copies", argv
[4]);
3098 xpc_dictionary_set_string(request
, "options", argv
[5]);
3099 xpc_dictionary_set_string(request
, "auth-info-required",
3100 getenv("AUTH_INFO_REQUIRED"));
3101 if ((auth_negotiate
= getenv("AUTH_NEGOTIATE")) != NULL
)
3102 xpc_dictionary_set_string(request
, "auth-negotiate", auth_negotiate
);
3103 xpc_dictionary_set_fd(request
, "stdin", fd
);
3104 xpc_dictionary_set_fd(request
, "stderr", 2);
3105 xpc_dictionary_set_fd(request
, "side-channel", CUPS_SC_FD
);
3107 sem
= dispatch_semaphore_create(0);
3110 xpc_connection_send_message_with_reply(conn
, request
,
3111 dispatch_get_global_queue(0,0),
3112 ^(xpc_object_t reply
)
3114 /* Save the response and wake up */
3115 if (xpc_get_type(reply
)
3116 == XPC_TYPE_DICTIONARY
)
3117 response
= xpc_retain(reply
);
3119 dispatch_semaphore_signal(sem
);
3122 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
3123 xpc_release(request
);
3124 dispatch_release(sem
);
3128 child_pid
= xpc_dictionary_get_int64(response
, "child-pid");
3130 xpc_release(response
);
3133 fprintf(stderr
, "DEBUG: Child PID=%d.\n", child_pid
);
3136 _cupsLangPrintFilter(stderr
, "ERROR",
3137 _("Unable to start backend process."));
3138 fputs("DEBUG: No child PID.\n", stderr
);
3144 _cupsLangPrintFilter(stderr
, "ERROR",
3145 _("Unable to start backend process."));
3146 fputs("DEBUG: No reply from agent.\n", stderr
);
3151 * Then wait for the backend to finish...
3154 request
= xpc_dictionary_create(NULL
, NULL
, 0);
3155 xpc_dictionary_set_int64(request
, "command", kPMWaitForJob
);
3156 xpc_dictionary_set_fd(request
, "stderr", 2);
3158 sem
= dispatch_semaphore_create(0);
3161 xpc_connection_send_message_with_reply(conn
, request
,
3162 dispatch_get_global_queue(0,0),
3163 ^(xpc_object_t reply
)
3165 /* Save the response and wake up */
3166 if (xpc_get_type(reply
)
3167 == XPC_TYPE_DICTIONARY
)
3168 response
= xpc_retain(reply
);
3170 dispatch_semaphore_signal(sem
);
3173 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
3174 xpc_release(request
);
3175 dispatch_release(sem
);
3179 status
= xpc_dictionary_get_int64(response
, "status");
3181 if (status
== SIGTERM
|| status
== SIGKILL
|| status
== SIGPIPE
)
3183 fprintf(stderr
, "DEBUG: Child terminated on signal %d.\n", status
);
3184 status
= CUPS_BACKEND_FAILED
;
3186 else if (WIFSIGNALED(status
))
3188 fprintf(stderr
, "DEBUG: Child crashed on signal %d.\n", status
);
3189 status
= CUPS_BACKEND_STOP
;
3191 else if (WIFEXITED(status
))
3193 status
= WEXITSTATUS(status
);
3194 fprintf(stderr
, "DEBUG: Child exited with status %d.\n", status
);
3197 xpc_release(response
);
3200 _cupsLangPrintFilter(stderr
, "ERROR",
3201 _("Unable to get backend exit status."));
3207 xpc_connection_suspend(conn
);
3208 xpc_connection_cancel(conn
);
3214 #endif /* HAVE_GSSAPI && HAVE_XPC */
3218 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
3222 sigterm_handler(int sig
) /* I - Signal */
3224 (void)sig
; /* remove compiler warnings... */
3226 write(2, "DEBUG: Got SIGTERM.\n", 20);
3228 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
3231 kill(child_pid
, sig
);
3234 #endif /* HAVE_GSSAPI && HAVE_XPC */
3239 * Flag that the job should be canceled...
3242 write(2, "DEBUG: job_canceled = 1.\n", 25);
3249 * The scheduler already tried to cancel us once, now just terminate
3250 * after removing our temp file!
3254 unlink(tmpfilename
);
3261 * 'timeout_cb()' - Handle HTTP timeouts.
3264 static int /* O - 1 to continue, 0 to cancel */
3265 timeout_cb(http_t
*http
, /* I - Connection to server (unused) */
3266 void *user_data
) /* I - User data (unused) */
3271 return (!job_canceled
);
3276 * 'update_reasons()' - Update the printer-state-reasons values.
3280 update_reasons(ipp_attribute_t
*attr
, /* I - printer-state-reasons or NULL */
3281 const char *s
) /* I - STATE: string or NULL */
3283 char op
; /* Add (+), remove (-), replace (\0) */
3284 cups_array_t
*new_reasons
; /* New reasons array */
3285 char *reason
, /* Current reason */
3286 add
[2048], /* Reasons added string */
3287 *addptr
, /* Pointer into add string */
3288 rem
[2048], /* Reasons removed string */
3289 *remptr
; /* Pointer into remove string */
3290 const char *addprefix
, /* Current add string prefix */
3291 *remprefix
; /* Current remove string prefix */
3294 fprintf(stderr
, "DEBUG: update_reasons(attr=%d(%s%s), s=\"%s\")\n",
3295 attr
? attr
->num_values
: 0, attr
? attr
->values
[0].string
.text
: "",
3296 attr
&& attr
->num_values
> 1 ? ",..." : "", s
? s
: "(null)");
3299 * Create an array of new reason keyword strings...
3304 int i
; /* Looping var */
3306 new_reasons
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
3309 for (i
= 0; i
< attr
->num_values
; i
++)
3311 reason
= attr
->values
[i
].string
.text
;
3313 if (strcmp(reason
, "none") &&
3314 strcmp(reason
, "none-report") &&
3315 strcmp(reason
, "paused") &&
3316 strncmp(reason
, "spool-area-full", 15) &&
3317 strcmp(reason
, "com.apple.print.recoverable-warning") &&
3318 strncmp(reason
, "cups-", 5))
3319 cupsArrayAdd(new_reasons
, reason
);
3324 if (*s
== '+' || *s
== '-')
3329 new_reasons
= _cupsArrayNewStrings(s
, ',');
3335 * Compute the changes...
3339 addprefix
= "STATE: +";
3342 remprefix
= "STATE: -";
3345 fprintf(stderr
, "DEBUG2: op='%c', new_reasons=%d, state_reasons=%d\n",
3346 op
? op
: ' ', cupsArrayCount(new_reasons
),
3347 cupsArrayCount(state_reasons
));
3349 _cupsMutexLock(&report_mutex
);
3357 for (reason
= (char *)cupsArrayFirst(new_reasons
);
3359 reason
= (char *)cupsArrayNext(new_reasons
))
3361 if (!cupsArrayFind(state_reasons
, reason
))
3363 if (!strncmp(reason
, "cups-remote-", 12))
3366 * If we are setting cups-remote-xxx, remove all other cups-remote-xxx
3370 char *temp
; /* Current reason in state_reasons */
3372 cupsArraySave(state_reasons
);
3374 for (temp
= (char *)cupsArrayFirst(state_reasons
);
3376 temp
= (char *)cupsArrayNext(state_reasons
))
3377 if (!strncmp(temp
, "cups-remote-", 12))
3379 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
3381 remptr
+= strlen(remptr
);
3384 cupsArrayRemove(state_reasons
, temp
);
3388 cupsArrayRestore(state_reasons
);
3391 cupsArrayAdd(state_reasons
, reason
);
3393 snprintf(addptr
, sizeof(add
) - (addptr
- add
), "%s%s", addprefix
,
3395 addptr
+= strlen(addptr
);
3406 for (reason
= (char *)cupsArrayFirst(new_reasons
);
3408 reason
= (char *)cupsArrayNext(new_reasons
))
3410 if (cupsArrayFind(state_reasons
, reason
))
3412 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
3414 remptr
+= strlen(remptr
);
3417 cupsArrayRemove(state_reasons
, reason
);
3424 * Replace reasons...
3427 for (reason
= (char *)cupsArrayFirst(state_reasons
);
3429 reason
= (char *)cupsArrayNext(state_reasons
))
3431 if (strncmp(reason
, "cups-", 5) && !cupsArrayFind(new_reasons
, reason
))
3433 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
3435 remptr
+= strlen(remptr
);
3438 cupsArrayRemove(state_reasons
, reason
);
3442 for (reason
= (char *)cupsArrayFirst(new_reasons
);
3444 reason
= (char *)cupsArrayNext(new_reasons
))
3446 if (!cupsArrayFind(state_reasons
, reason
))
3448 cupsArrayAdd(state_reasons
, reason
);
3450 snprintf(addptr
, sizeof(add
) - (addptr
- add
), "%s%s", addprefix
,
3452 addptr
+= strlen(addptr
);
3458 _cupsMutexUnlock(&report_mutex
);
3461 * Report changes and return...
3464 if (add
[0] && rem
[0])
3465 fprintf(stderr
, "%s\n%s\n", add
, rem
);
3467 fprintf(stderr
, "%s\n", add
);
3469 fprintf(stderr
, "%s\n", rem
);
3473 * End of "$Id: ipp.c 9759 2011-05-11 03:24:33Z mike $".