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 */
92 "job-impressions-completed",
93 "job-media-sheets-completed",
95 "job-originating-user-name",
99 static int job_canceled
= 0;
101 static char username
[256] = "",
102 /* Username for device URI */
104 /* Password for device URI */
105 static int password_tries
= 0;
107 static const char * const pattrs
[] = /* Printer attributes we want */
111 "document-format-supported",
113 "marker-high-levels",
119 "media-col-supported",
120 "multiple-document-handling-supported",
121 "operations-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
);
161 static void compress_files(int num_files
, char **files
);
162 #endif /* HAVE_LIBZ */
163 static void *monitor_printer(_cups_monitor_t
*monitor
);
164 static ipp_t
*new_request(ipp_op_t op
, int version
, const char *uri
,
165 const char *user
, const char *title
,
166 int num_options
, cups_option_t
*options
,
167 const char *compression
, int copies
,
168 const char *format
, _ppd_cache_t
*pc
,
169 ipp_attribute_t
*media_col_sup
,
170 ipp_attribute_t
*doc_handling_sup
);
171 static const char *password_cb(const char *prompt
, http_t
*http
,
172 const char *method
, const char *resource
,
174 static const char *quote_string(const char *s
, char *q
, size_t qsize
);
175 static void report_attr(ipp_attribute_t
*attr
);
176 static void report_printer_state(ipp_t
*ipp
);
177 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
178 static int run_as_user(char *argv
[], uid_t uid
,
179 const char *device_uri
, int fd
);
180 #endif /* HAVE_GSSAPI && HAVE_XPC */
181 static void sigterm_handler(int sig
);
182 static int timeout_cb(http_t
*http
, void *user_data
);
183 static void update_reasons(ipp_attribute_t
*attr
, const char *s
);
187 * 'main()' - Send a file to the printer or server.
191 * printer-uri job-id user title copies options [file]
194 int /* O - Exit status */
195 main(int argc
, /* I - Number of command-line args */
196 char *argv
[]) /* I - Command-line arguments */
198 int i
; /* Looping var */
199 int send_options
; /* Send job options? */
200 int num_options
; /* Number of printer options */
201 cups_option_t
*options
; /* Printer options */
202 const char *device_uri
; /* Device URI */
203 char scheme
[255], /* Scheme in URI */
204 hostname
[1024], /* Hostname */
205 resource
[1024], /* Resource info (printer name) */
206 addrname
[256], /* Address name */
207 *optptr
, /* Pointer to URI options */
208 *name
, /* Name of option */
209 *value
, /* Value of option */
210 sep
; /* Separator character */
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 waitprinter
; /* Wait for printer ready? */
238 _cups_monitor_t monitor
; /* Monitoring data */
239 ipp_attribute_t
*job_id_attr
; /* job-id attribute */
240 int job_id
; /* job-id value */
241 ipp_attribute_t
*job_sheets
; /* job-media-sheets-completed */
242 ipp_attribute_t
*job_state
; /* job-state */
243 ipp_attribute_t
*copies_sup
; /* copies-supported */
244 ipp_attribute_t
*cups_version
; /* cups-version */
245 ipp_attribute_t
*format_sup
; /* document-format-supported */
246 ipp_attribute_t
*media_col_sup
; /* media-col-supported */
247 ipp_attribute_t
*operations_sup
; /* operations-supported */
248 ipp_attribute_t
*doc_handling_sup
; /* multiple-document-handling-supported */
249 ipp_attribute_t
*printer_state
; /* printer-state attribute */
250 ipp_attribute_t
*printer_accepting
; /* printer-is-accepting-jobs */
251 int create_job
= 0, /* Does printer support Create-Job? */
252 get_job_attrs
= 0, /* Does printer support Get-Job-Attributes? */
253 send_document
= 0, /* Does printer support Send-Document? */
254 validate_job
= 0; /* Does printer support Validate-Job? */
255 int copies
, /* Number of copies for job */
256 copies_remaining
; /* Number of copies remaining */
257 const char *content_type
, /* CONTENT_TYPE environment variable */
258 *final_content_type
, /* FINAL_CONTENT_TYPE environment var */
259 *document_format
; /* document-format value */
260 int fd
; /* File descriptor */
261 off_t bytes
= 0; /* Bytes copied */
262 char buffer
[16384]; /* Copy buffer */
263 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
264 struct sigaction action
; /* Actions for POSIX signals */
265 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
266 int version
; /* IPP version */
267 ppd_file_t
*ppd
; /* PPD file */
268 _ppd_cache_t
*pc
; /* PPD cache and mapping data */
269 fd_set input
; /* Input set for select() */
273 * Make sure status messages are not buffered...
276 setbuf(stderr
, NULL
);
279 * Ignore SIGPIPE and catch SIGTERM signals...
283 sigset(SIGPIPE
, SIG_IGN
);
284 sigset(SIGTERM
, sigterm_handler
);
285 #elif defined(HAVE_SIGACTION)
286 memset(&action
, 0, sizeof(action
));
287 action
.sa_handler
= SIG_IGN
;
288 sigaction(SIGPIPE
, &action
, NULL
);
290 sigemptyset(&action
.sa_mask
);
291 sigaddset(&action
.sa_mask
, SIGTERM
);
292 action
.sa_handler
= sigterm_handler
;
293 sigaction(SIGTERM
, &action
, NULL
);
295 signal(SIGPIPE
, SIG_IGN
);
296 signal(SIGTERM
, sigterm_handler
);
297 #endif /* HAVE_SIGSET */
300 * Check command-line...
307 if ((s
= strrchr(argv
[0], '/')) != NULL
)
312 printf("network %s \"Unknown\" \"%s (%s)\"\n",
313 s
, _cupsLangString(cupsLangDefault(),
314 _("Internet Printing Protocol")), s
);
315 return (CUPS_BACKEND_OK
);
319 _cupsLangPrintf(stderr
,
320 _("Usage: %s job-id user title copies options [file]"),
322 return (CUPS_BACKEND_STOP
);
326 * Get the device URI...
329 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
331 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
334 if (getenv("CLASS") != NULL
)
335 return (CUPS_BACKEND_FAILED
);
338 if ((auth_info_required
= getenv("AUTH_INFO_REQUIRED")) == NULL
)
339 auth_info_required
= "none";
341 state_reasons
= _cupsArrayNewStrings(getenv("PRINTER_STATE_REASONS"), ',');
345 * For Kerberos, become the printing user (if we can) to get the credentials
349 if (!getuid() && (value
= getenv("AUTH_UID")) != NULL
)
351 uid_t uid
= (uid_t
)atoi(value
);
358 return (run_as_user(argv
, uid
, device_uri
, 0));
361 int status
= 0; /* Exit status */
363 for (i
= 6; i
< argc
&& !status
&& !job_canceled
; i
++)
365 if ((fd
= open(argv
[i
], O_RDONLY
)) >= 0)
367 status
= run_as_user(argv
, uid
, device_uri
, fd
);
372 _cupsLangPrintError("ERROR", _("Unable to open print file"));
373 status
= CUPS_BACKEND_FAILED
;
381 # else /* No XPC, just try to run as the user ID */
384 # endif /* HAVE_XPC */
386 #endif /* HAVE_GSSAPI */
389 * Get the (final) content type...
392 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
393 content_type
= "application/octet-stream";
395 if ((final_content_type
= getenv("FINAL_CONTENT_TYPE")) == NULL
)
397 final_content_type
= content_type
;
399 if (!strncmp(final_content_type
, "printer/", 8))
400 final_content_type
= "application/vnd.cups-raw";
404 * Extract the hostname and printer name from the URI...
407 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
408 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
409 resource
, sizeof(resource
));
412 port
= IPP_PORT
; /* Default to port 631 */
414 if (!strcmp(scheme
, "https"))
415 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
417 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
420 * See if there are any options...
427 contimeout
= 7 * 24 * 60 * 60;
429 if ((optptr
= strchr(resource
, '?')) != NULL
)
432 * Yup, terminate the device name string and move to the first
433 * character of the optptr...
439 * Then parse the optptr...
450 while (*optptr
&& *optptr
!= '=' && *optptr
!= '+' && *optptr
!= '&')
453 if ((sep
= *optptr
) != '\0')
464 while (*optptr
&& *optptr
!= '+' && *optptr
!= '&')
474 * Process the option...
477 if (!_cups_strcasecmp(name
, "waitjob"))
480 * Wait for job completion?
483 waitjob
= !_cups_strcasecmp(value
, "on") ||
484 !_cups_strcasecmp(value
, "yes") ||
485 !_cups_strcasecmp(value
, "true");
487 else if (!_cups_strcasecmp(name
, "waitprinter"))
490 * Wait for printer idle?
493 waitprinter
= !_cups_strcasecmp(value
, "on") ||
494 !_cups_strcasecmp(value
, "yes") ||
495 !_cups_strcasecmp(value
, "true");
497 else if (!_cups_strcasecmp(name
, "encryption"))
500 * Enable/disable encryption?
503 if (!_cups_strcasecmp(value
, "always"))
504 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
505 else if (!_cups_strcasecmp(value
, "required"))
506 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
507 else if (!_cups_strcasecmp(value
, "never"))
508 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
509 else if (!_cups_strcasecmp(value
, "ifrequested"))
510 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
513 _cupsLangPrintFilter(stderr
, "ERROR",
514 _("Unknown encryption option value: \"%s\"."),
518 else if (!_cups_strcasecmp(name
, "snmp"))
521 * Enable/disable SNMP stuff...
524 snmp_enabled
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
525 _cups_strcasecmp(value
, "yes") ||
526 _cups_strcasecmp(value
, "true");
528 else if (!_cups_strcasecmp(name
, "version"))
530 if (!strcmp(value
, "1.0"))
532 else if (!strcmp(value
, "1.1"))
534 else if (!strcmp(value
, "2.0"))
536 else if (!strcmp(value
, "2.1"))
538 else if (!strcmp(value
, "2.2"))
542 _cupsLangPrintFilter(stderr
, "ERROR",
543 _("Unknown version option value: \"%s\"."),
548 else if (!_cups_strcasecmp(name
, "compression"))
550 if (!_cups_strcasecmp(value
, "true") || !_cups_strcasecmp(value
, "yes") ||
551 !_cups_strcasecmp(value
, "on") || !_cups_strcasecmp(value
, "gzip"))
552 compression
= "gzip";
554 #endif /* HAVE_LIBZ */
555 else if (!_cups_strcasecmp(name
, "contimeout"))
558 * Set the connection timeout...
562 contimeout
= atoi(value
);
570 _cupsLangPrintFilter(stderr
, "ERROR",
571 _("Unknown option \"%s\" with value \"%s\"."),
578 * If we have 7 arguments, print the file named on the command-line.
579 * Otherwise, copy stdin to a temporary file and print the temporary
587 send_options
= !_cups_strcasecmp(final_content_type
, "application/pdf") ||
588 !_cups_strcasecmp(final_content_type
, "application/vnd.cups-pdf") ||
589 !_cups_strncasecmp(final_content_type
, "image/", 6);
591 fputs("DEBUG: Sending stdin for job...\n", stderr
);
596 * Point to the files on the command-line...
599 num_files
= argc
- 6;
605 compress_files(num_files
, files
);
606 #endif /* HAVE_LIBZ */
608 fprintf(stderr
, "DEBUG: %d files to send in job...\n", num_files
);
612 * Set the authentication info, if any...
615 cupsSetPasswordCB2(password_cb
, NULL
);
620 * Use authentication information in the device URI...
623 if ((password
= strchr(username
, ':')) != NULL
)
626 cupsSetUser(username
);
631 * Try loading authentication information from the environment.
634 const char *ptr
= getenv("AUTH_USERNAME");
638 strlcpy(username
, ptr
, sizeof(username
));
642 password
= getenv("AUTH_PASSWORD");
646 * Try finding the remote server...
649 start_time
= time(NULL
);
651 sprintf(portname
, "%d", port
);
653 update_reasons(NULL
, "+connecting-to-device");
654 fprintf(stderr
, "DEBUG: Looking up \"%s\"...\n", hostname
);
656 while ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
658 _cupsLangPrintFilter(stderr
, "INFO",
659 _("Unable to locate printer \"%s\"."), hostname
);
662 if (getenv("CLASS") != NULL
)
664 update_reasons(NULL
, "-connecting-to-device");
665 return (CUPS_BACKEND_STOP
);
669 return (CUPS_BACKEND_OK
);
672 http
= _httpCreate(hostname
, port
, addrlist
, cupsEncryption(), AF_UNSPEC
);
673 httpSetTimeout(http
, 30.0, timeout_cb
, NULL
);
676 * See if the printer supports SNMP...
680 snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
);
685 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
),
688 have_supplies
= start_count
= 0;
691 * Wait for data from the filter...
696 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), 0, backendNetworkSideCB
))
697 return (CUPS_BACKEND_OK
);
698 else if ((bytes
= read(0, buffer
, sizeof(buffer
))) <= 0)
699 return (CUPS_BACKEND_OK
);
703 * Try connecting to the remote server...
706 delay
= _cupsNextDelay(0, &prev_delay
);
710 fprintf(stderr
, "DEBUG: Connecting to %s:%d\n", hostname
, port
);
711 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
713 if (httpReconnect(http
))
715 int error
= errno
; /* Connection error */
717 if (http
->status
== HTTP_PKI_ERROR
)
718 update_reasons(NULL
, "+cups-certificate-error");
723 if (getenv("CLASS") != NULL
)
726 * If the CLASS environment variable is set, the job was submitted
727 * to a class and not to a specific queue. In this case, we want
728 * to abort immediately so that the job can be requeued on the next
729 * available printer in the class.
732 _cupsLangPrintFilter(stderr
, "INFO",
733 _("Unable to contact printer, queuing on next "
734 "printer in class."));
737 * Sleep 5 seconds to keep the job from requeuing too rapidly...
742 update_reasons(NULL
, "-connecting-to-device");
744 return (CUPS_BACKEND_FAILED
);
747 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
749 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
750 errno
== EHOSTUNREACH
)
752 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
754 _cupsLangPrintFilter(stderr
, "ERROR",
755 _("The printer is not responding."));
756 update_reasons(NULL
, "-connecting-to-device");
757 return (CUPS_BACKEND_FAILED
);
763 _cupsLangPrintFilter(stderr
, "WARNING",
764 _("The printer may not exist or "
765 "is unavailable at this time."));
769 _cupsLangPrintFilter(stderr
, "WARNING",
770 _("The printer is unreachable at this "
776 _cupsLangPrintFilter(stderr
, "WARNING",
777 _("The printer is in use."));
783 delay
= _cupsNextDelay(delay
, &prev_delay
);
787 _cupsLangPrintFilter(stderr
, "ERROR",
788 _("The printer is not responding."));
796 update_reasons(NULL
, "-cups-certificate-error");
798 while (http
->fd
< 0);
801 return (CUPS_BACKEND_OK
);
803 return (CUPS_BACKEND_FAILED
);
805 update_reasons(NULL
, "-connecting-to-device");
806 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
808 fprintf(stderr
, "DEBUG: Connected to %s:%d...\n",
809 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
810 _httpAddrPort(http
->hostaddr
));
813 * Build a URI for the printer and fill the standard IPP attributes for
814 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
815 * might contain username:password information...
818 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), scheme
, NULL
, hostname
,
822 * First validate the destination and see if the device supports multiple
829 media_col_sup
= NULL
;
831 operations_sup
= NULL
;
832 doc_handling_sup
= NULL
;
837 * Check for side-channel requests...
840 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
843 * Build the IPP request...
846 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
847 request
->request
.op
.version
[0] = version
/ 10;
848 request
->request
.op
.version
[1] = version
% 10;
850 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
853 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
854 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
861 fputs("DEBUG: Getting supported attributes...\n", stderr
);
863 if (http
->version
< HTTP_1_1
)
865 fprintf(stderr
, "DEBUG: Printer responded with HTTP version %d.%d.\n",
866 http
->version
/ 100, http
->version
% 100);
867 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
868 "cups-ipp-wrong-http-version");
871 supported
= cupsDoRequest(http
, request
, resource
);
872 ipp_status
= cupsLastError();
874 fprintf(stderr
, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
875 ippErrorString(ipp_status
), cupsLastErrorString());
877 if (ipp_status
<= IPP_OK_CONFLICT
)
881 fprintf(stderr
, "DEBUG: Get-Printer-Attributes returned %s.\n",
882 ippErrorString(ipp_status
));
884 if (ipp_status
== IPP_PRINTER_BUSY
||
885 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
887 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
889 _cupsLangPrintFilter(stderr
, "ERROR",
890 _("The printer is not responding."));
891 return (CUPS_BACKEND_FAILED
);
894 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
896 report_printer_state(supported
);
900 delay
= _cupsNextDelay(delay
, &prev_delay
);
902 else if ((ipp_status
== IPP_BAD_REQUEST
||
903 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
> 10)
906 * Switch to IPP/1.1 or IPP/1.0...
911 _cupsLangPrintFilter(stderr
, "INFO",
912 _("The printer does not support IPP/%d.%d, trying "
913 "IPP/%s."), version
/ 10, version
% 10, "1.1");
918 _cupsLangPrintFilter(stderr
, "INFO",
919 _("The printer does not support IPP/%d.%d, trying "
920 "IPP/%s."), version
/ 10, version
% 10, "1.0");
926 else if (ipp_status
== IPP_NOT_FOUND
)
928 _cupsLangPrintFilter(stderr
, "ERROR",
929 _("The printer URI is incorrect or no longer "
932 ippDelete(supported
);
934 return (CUPS_BACKEND_STOP
);
936 else if (ipp_status
== IPP_FORBIDDEN
||
937 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
939 const char *www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
940 /* WWW-Authenticate field value */
942 if (!strncmp(www_auth
, "Negotiate", 9))
943 auth_info_required
= "negotiate";
944 else if (www_auth
[0])
945 auth_info_required
= "username,password";
947 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
948 return (CUPS_BACKEND_AUTH_REQUIRED
);
950 else if (ipp_status
!= IPP_NOT_AUTHORIZED
)
952 _cupsLangPrintFilter(stderr
, "ERROR",
953 _("Unable to get printer status."));
959 ippDelete(supported
);
964 if (!getenv("CLASS"))
967 * Check printer-is-accepting-jobs = false and printer-state-reasons for the
968 * "spool-area-full" keyword...
973 if ((printer_accepting
= ippFindAttribute(supported
,
974 "printer-is-accepting-jobs",
975 IPP_TAG_BOOLEAN
)) != NULL
&&
976 !printer_accepting
->values
[0].boolean
)
978 else if (!printer_accepting
)
979 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
980 "cups-ipp-missing-printer-is-accepting-jobs");
982 if ((printer_state
= ippFindAttribute(supported
,
983 "printer-state-reasons",
984 IPP_TAG_KEYWORD
)) != NULL
&& !busy
)
986 for (i
= 0; i
< printer_state
->num_values
; i
++)
987 if (!strcmp(printer_state
->values
[0].string
.text
,
988 "spool-area-full") ||
989 !strncmp(printer_state
->values
[0].string
.text
, "spool-area-full-",
997 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
998 "cups-ipp-missing-printer-state-reasons");
1002 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
1004 report_printer_state(supported
);
1008 delay
= _cupsNextDelay(delay
, &prev_delay
);
1010 ippDelete(supported
);
1017 * Check for supported attributes...
1020 if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
1021 IPP_TAG_RANGE
)) != NULL
)
1024 * Has the "copies-supported" attribute - does it have an upper
1028 fprintf(stderr
, "DEBUG: copies-supported=%d-%d\n",
1029 copies_sup
->values
[0].range
.lower
,
1030 copies_sup
->values
[0].range
.upper
);
1032 if (copies_sup
->values
[0].range
.upper
<= 1)
1033 copies_sup
= NULL
; /* No */
1036 cups_version
= ippFindAttribute(supported
, "cups-version", IPP_TAG_TEXT
);
1038 if ((format_sup
= ippFindAttribute(supported
, "document-format-supported",
1039 IPP_TAG_MIMETYPE
)) != NULL
)
1041 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
1042 format_sup
->num_values
);
1043 for (i
= 0; i
< format_sup
->num_values
; i
++)
1044 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
1045 format_sup
->values
[i
].string
.text
);
1048 if ((media_col_sup
= ippFindAttribute(supported
, "media-col-supported",
1049 IPP_TAG_KEYWORD
)) != NULL
)
1051 fprintf(stderr
, "DEBUG: media-col-supported (%d values)\n",
1052 media_col_sup
->num_values
);
1053 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
1054 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
1055 media_col_sup
->values
[i
].string
.text
);
1058 if ((operations_sup
= ippFindAttribute(supported
, "operations-supported",
1059 IPP_TAG_ENUM
)) != NULL
)
1061 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1062 if (operations_sup
->values
[i
].integer
== IPP_PRINT_JOB
)
1065 if (i
>= operations_sup
->num_values
)
1066 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1067 "cups-ipp-missing-print-job");
1069 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1070 if (operations_sup
->values
[i
].integer
== IPP_CANCEL_JOB
)
1073 if (i
>= operations_sup
->num_values
)
1074 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1075 "cups-ipp-missing-cancel-job");
1077 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1078 if (operations_sup
->values
[i
].integer
== IPP_GET_JOB_ATTRIBUTES
)
1081 if (i
>= operations_sup
->num_values
)
1082 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1083 "cups-ipp-missing-get-job-attributes");
1085 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1086 if (operations_sup
->values
[i
].integer
== IPP_GET_PRINTER_ATTRIBUTES
)
1089 if (i
>= operations_sup
->num_values
)
1090 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1091 "cups-ipp-missing-get-printer-attributes");
1093 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1095 if (operations_sup
->values
[i
].integer
== IPP_VALIDATE_JOB
)
1097 else if (operations_sup
->values
[i
].integer
== IPP_CREATE_JOB
)
1099 else if (operations_sup
->values
[i
].integer
== IPP_SEND_DOCUMENT
)
1101 else if (operations_sup
->values
[i
].integer
== IPP_GET_JOB_ATTRIBUTES
)
1105 if (create_job
&& !send_document
)
1107 fputs("DEBUG: Printer supports Create-Job but not Send-Document.\n",
1111 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1112 "cups-ipp-missing-send-document");
1116 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1117 "cups-ipp-missing-validate-job");
1120 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1121 "cups-ipp-missing-operations-supported");
1123 doc_handling_sup
= ippFindAttribute(supported
,
1124 "multiple-document-handling-supported",
1127 report_printer_state(supported
);
1129 while (!job_canceled
&& ipp_status
> IPP_OK_CONFLICT
);
1132 return (CUPS_BACKEND_OK
);
1135 * See if the printer is accepting jobs and is not stopped; if either
1136 * condition is true and we are printing to a class, requeue the job...
1139 if (getenv("CLASS") != NULL
)
1141 printer_state
= ippFindAttribute(supported
, "printer-state",
1143 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
1146 if (printer_state
== NULL
||
1147 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
1149 printer_accepting
== NULL
||
1150 !printer_accepting
->values
[0].boolean
)
1153 * If the CLASS environment variable is set, the job was submitted
1154 * to a class and not to a specific queue. In this case, we want
1155 * to abort immediately so that the job can be requeued on the next
1156 * available printer in the class.
1159 _cupsLangPrintFilter(stderr
, "INFO",
1160 _("Unable to contact printer, queuing on next "
1161 "printer in class."));
1163 ippDelete(supported
);
1167 * Sleep 5 seconds to keep the job from requeuing too rapidly...
1172 return (CUPS_BACKEND_FAILED
);
1177 * See if the printer supports multiple copies...
1180 copies
= atoi(argv
[4]);
1182 if (copies_sup
|| argc
< 7)
1183 copies_remaining
= 1;
1185 copies_remaining
= copies
;
1188 * Prepare remaining printing options...
1196 num_options
= cupsParseOptions(argv
[5], 0, &options
);
1198 if (!cups_version
&& media_col_sup
)
1201 * Load the PPD file and generate PWG attribute mapping information...
1204 ppd
= ppdOpenFile(getenv("PPD"));
1205 pc
= _ppdCacheCreateWithPPD(ppd
);
1213 document_format
= NULL
;
1215 if (format_sup
!= NULL
)
1217 for (i
= 0; i
< format_sup
->num_values
; i
++)
1218 if (!_cups_strcasecmp(final_content_type
,
1219 format_sup
->values
[i
].string
.text
))
1221 document_format
= final_content_type
;
1225 if (!document_format
)
1227 for (i
= 0; i
< format_sup
->num_values
; i
++)
1228 if (!_cups_strcasecmp("application/octet-stream",
1229 format_sup
->values
[i
].string
.text
))
1231 document_format
= "application/octet-stream";
1237 fprintf(stderr
, "DEBUG: final_content_type=\"%s\", document_format=\"%s\"\n",
1238 final_content_type
, document_format
? document_format
: "(null)");
1241 * If the printer does not support HTTP/1.1 (which IPP requires), copy stdin
1242 * to a temporary file so that we can do a HTTP/1.0 submission...
1244 * (I hate compatibility hacks!)
1247 if (http
->version
< HTTP_1_1
&& num_files
== 0)
1249 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
1251 perror("DEBUG: Unable to create temporary file");
1252 return (CUPS_BACKEND_FAILED
);
1255 _cupsLangPrintFilter(stderr
, "INFO", _("Copying print data."));
1257 if ((compatsize
= write(fd
, buffer
, bytes
)) < 0)
1259 perror("DEBUG: Unable to write temporary file");
1260 return (CUPS_BACKEND_FAILED
);
1263 if ((bytes
= backendRunLoop(-1, fd
, snmp_fd
, &(addrlist
->addr
), 0, 0,
1264 backendNetworkSideCB
)) < 0)
1265 return (CUPS_BACKEND_FAILED
);
1267 compatsize
+= bytes
;
1271 compatfile
= tmpfilename
;
1272 files
= &compatfile
;
1275 else if (http
->version
< HTTP_1_1
&& num_files
== 1)
1277 struct stat fileinfo
; /* File information */
1279 if (!stat(files
[0], &fileinfo
))
1280 compatsize
= fileinfo
.st_size
;
1284 * If the printer only claims to support IPP/1.0, or if the user specifically
1285 * included version=1.0 in the URI, then do not try to use Create-Job or
1286 * Send-Document. This is another dreaded compatibility hack, but unfortunately
1287 * there are enough broken printers out there that we need this for now...
1291 create_job
= send_document
= 0;
1294 * Start monitoring the printer in the background...
1298 monitor
.hostname
= hostname
;
1299 monitor
.user
= argv
[2];
1300 monitor
.resource
= resource
;
1301 monitor
.port
= port
;
1302 monitor
.version
= version
;
1304 monitor
.get_job_attrs
= get_job_attrs
;
1305 monitor
.encryption
= cupsEncryption();
1306 monitor
.job_state
= IPP_JOB_PENDING
;
1307 monitor
.printer_state
= IPP_PRINTER_IDLE
;
1311 monitor
.job_name
= argv
[3];
1315 snprintf(print_job_name
, sizeof(print_job_name
), "%s - %s", argv
[1],
1317 monitor
.job_name
= print_job_name
;
1320 _cupsThreadCreate((_cups_thread_func_t
)monitor_printer
, &monitor
);
1323 * Validate access to the printer...
1326 while (!job_canceled
&& validate_job
)
1328 request
= new_request(IPP_VALIDATE_JOB
, version
, uri
, argv
[2],
1329 monitor
.job_name
, num_options
, options
, compression
,
1330 copies_sup
? copies
: 1, document_format
, pc
,
1331 media_col_sup
, doc_handling_sup
);
1333 ippDelete(cupsDoRequest(http
, request
, resource
));
1335 ipp_status
= cupsLastError();
1337 fprintf(stderr
, "DEBUG: Validate-Job: %s (%s)\n",
1338 ippErrorString(ipp_status
), cupsLastErrorString());
1343 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
|| ipp_status
== IPP_PRINTER_BUSY
)
1345 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
1348 else if (ipp_status
== IPP_DOCUMENT_FORMAT
)
1350 else if (ipp_status
== IPP_FORBIDDEN
||
1351 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1353 const char *www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
1354 /* WWW-Authenticate field value */
1356 if (!strncmp(www_auth
, "Negotiate", 9))
1357 auth_info_required
= "negotiate";
1358 else if (www_auth
[0])
1359 auth_info_required
= "username,password";
1363 else if (ipp_status
== IPP_OPERATION_NOT_SUPPORTED
)
1366 * This is all too common...
1369 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1370 "cups-ipp-missing-validate-job");
1373 else if (ipp_status
< IPP_REDIRECTION_OTHER_SITE
||
1374 ipp_status
== IPP_BAD_REQUEST
)
1379 * Then issue the print-job request...
1384 while (!job_canceled
&& copies_remaining
> 0)
1387 * Check for side-channel requests...
1390 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1393 * Build the IPP job creation request...
1399 request
= new_request((num_files
> 1 || create_job
) ? IPP_CREATE_JOB
:
1401 version
, uri
, argv
[2], monitor
.job_name
, num_options
,
1402 options
, compression
, copies_sup
? copies
: 1,
1403 document_format
, pc
, media_col_sup
, doc_handling_sup
);
1409 if (num_files
> 1 || create_job
)
1410 response
= cupsDoRequest(http
, request
, resource
);
1413 size_t length
= 0; /* Length of request */
1417 fputs("DEBUG: Sending file using HTTP/1.0 Content-Length...\n", stderr
);
1418 length
= ippLength(request
) + (size_t)compatsize
;
1421 fputs("DEBUG: Sending file using HTTP/1.1 chunking...\n", stderr
);
1423 http_status
= cupsSendRequest(http
, request
, resource
, length
);
1424 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
)
1428 if ((fd
= open(files
[0], O_RDONLY
)) < 0)
1430 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1431 return (CUPS_BACKEND_FAILED
);
1437 http_status
= cupsWriteRequestData(http
, buffer
, bytes
);
1440 while (http_status
== HTTP_CONTINUE
&&
1441 (!job_canceled
|| compatsize
> 0))
1444 * Check for side-channel requests and more print data...
1449 FD_SET(snmp_fd
, &input
);
1451 while (select(fd
> snmp_fd
? fd
+ 1 : snmp_fd
+ 1, &input
, NULL
, NULL
,
1452 NULL
) <= 0 && !job_canceled
);
1454 if (FD_ISSET(snmp_fd
, &input
))
1455 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1457 if (FD_ISSET(fd
, &input
))
1459 if ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1461 fprintf(stderr
, "DEBUG: Read %d bytes...\n", (int)bytes
);
1463 if (cupsWriteRequestData(http
, buffer
, bytes
) != HTTP_CONTINUE
)
1466 else if (bytes
== 0 || (errno
!= EINTR
&& errno
!= EAGAIN
))
1475 response
= cupsGetResponse(http
, resource
);
1479 ipp_status
= cupsLastError();
1481 fprintf(stderr
, "DEBUG: %s: %s (%s)\n",
1482 (num_files
> 1 || create_job
) ? "Create-Job" : "Print-Job",
1483 ippErrorString(ipp_status
), cupsLastErrorString());
1485 if (ipp_status
> IPP_OK_CONFLICT
)
1492 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1493 ipp_status
== IPP_NOT_POSSIBLE
||
1494 ipp_status
== IPP_PRINTER_BUSY
)
1496 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
1502 * We can't re-submit when we have no files to print, so exit
1503 * immediately with the right status code...
1509 else if (ipp_status
== IPP_ERROR_JOB_CANCELED
||
1510 ipp_status
== IPP_NOT_AUTHORIZED
)
1515 * Update auth-info-required as needed...
1518 _cupsLangPrintFilter(stderr
, "ERROR",
1519 _("Print file was not accepted."));
1521 if (ipp_status
== IPP_FORBIDDEN
||
1522 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1524 const char *www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
1525 /* WWW-Authenticate field value */
1527 if (!strncmp(www_auth
, "Negotiate", 9))
1528 auth_info_required
= "negotiate";
1529 else if (www_auth
[0])
1530 auth_info_required
= "username,password";
1532 else if (ipp_status
== IPP_REQUEST_VALUE
)
1535 * Print file is too large, abort this job...
1546 * We can't re-submit when we have no files to print, so exit
1547 * immediately with the right status code...
1554 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
1555 IPP_TAG_INTEGER
)) == NULL
)
1557 _cupsLangPrintFilter(stderr
, "INFO",
1558 _("Print file accepted - job ID unknown."));
1559 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1560 "cups-ipp-missing-job-id");
1566 monitor
.job_id
= job_id
= job_id_attr
->values
[0].integer
;
1567 _cupsLangPrintFilter(stderr
, "INFO",
1568 _("Print file accepted - job ID %d."), job_id
);
1571 fprintf(stderr
, "DEBUG: job-id=%d\n", job_id
);
1572 ippDelete(response
);
1577 if (job_id
&& (num_files
> 1 || create_job
))
1579 for (i
= 0; num_files
== 0 || i
< num_files
; i
++)
1582 * Check for side-channel requests...
1585 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1588 * Send the next file in the job...
1591 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
1592 request
->request
.op
.version
[0] = version
/ 10;
1593 request
->request
.op
.version
[1] = version
% 10;
1595 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1598 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1602 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1603 "requesting-user-name", NULL
, argv
[2]);
1605 if ((i
+ 1) >= num_files
)
1606 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
1608 if (document_format
)
1609 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1610 "document-format", NULL
, document_format
);
1613 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1614 "compression", NULL
, compression
);
1616 fprintf(stderr
, "DEBUG: Sending file %d using chunking...\n", i
+ 1);
1617 http_status
= cupsSendRequest(http
, request
, resource
, 0);
1618 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
)
1623 http_status
= cupsWriteRequestData(http
, buffer
, bytes
);
1627 if ((fd
= open(files
[i
], O_RDONLY
)) < 0)
1629 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1630 return (CUPS_BACKEND_FAILED
);
1639 while (!job_canceled
&& http_status
== HTTP_CONTINUE
&&
1640 (bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1642 if ((http_status
= cupsWriteRequestData(http
, buffer
, bytes
))
1648 * Check for side-channel requests...
1651 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1659 ippDelete(cupsGetResponse(http
, resource
));
1662 fprintf(stderr
, "DEBUG: Send-Document: %s (%s)\n",
1663 ippErrorString(cupsLastError()), cupsLastErrorString());
1665 if (cupsLastError() > IPP_OK_CONFLICT
)
1667 ipp_status
= cupsLastError();
1669 _cupsLangPrintFilter(stderr
, "ERROR",
1670 _("Unable to add document to print job."));
1677 if (num_files
== 0 || fd
< 0)
1683 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
1685 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
1686 copies_remaining
--;
1688 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1689 ipp_status
== IPP_NOT_POSSIBLE
||
1690 ipp_status
== IPP_PRINTER_BUSY
)
1692 else if (ipp_status
== IPP_REQUEST_VALUE
||
1693 ipp_status
== IPP_ERROR_JOB_CANCELED
||
1694 ipp_status
== IPP_NOT_AUTHORIZED
||
1695 ipp_status
== IPP_INTERNAL_ERROR
)
1698 * Print file is too large, job was canceled, we need new
1699 * authentication data, or we had some sort of error...
1704 else if (ipp_status
== IPP_NOT_FOUND
)
1707 * Printer does not actually implement support for Create-Job/
1708 * Send-Document, so log the conformance issue and stop the printer.
1711 fputs("DEBUG: This printer claims to support Create-Job and "
1712 "Send-Document, but those operations failed.\n", stderr
);
1713 fputs("DEBUG: Add '?version=1.0' to the device URI to use legacy "
1714 "compatibility mode.\n", stderr
);
1715 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1716 "cups-ipp-missing-send-document");
1718 ipp_status
= IPP_INTERNAL_ERROR
; /* Force queue to stop */
1723 copies_remaining
--;
1726 * Wait for the job to complete...
1729 if (!job_id
|| !waitjob
|| !get_job_attrs
)
1732 _cupsLangPrintFilter(stderr
, "INFO", _("Waiting for job to complete."));
1734 for (delay
= _cupsNextDelay(0, &prev_delay
); !job_canceled
;)
1737 * Check for side-channel requests...
1740 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1743 * Build an IPP_GET_JOB_ATTRIBUTES request...
1746 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1747 request
->request
.op
.version
[0] = version
/ 10;
1748 request
->request
.op
.version
[1] = version
% 10;
1750 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1753 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1757 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1758 "requesting-user-name", NULL
, argv
[2]);
1760 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1761 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
1768 httpReconnect(http
);
1769 response
= cupsDoRequest(http
, request
, resource
);
1770 ipp_status
= cupsLastError();
1772 if (ipp_status
== IPP_NOT_FOUND
|| ipp_status
== IPP_NOT_POSSIBLE
)
1775 * Job has gone away and/or the server has no job history...
1778 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1779 "cups-ipp-missing-job-history");
1780 ippDelete(response
);
1782 ipp_status
= IPP_OK
;
1786 fprintf(stderr
, "DEBUG: Get-Job-Attributes: %s (%s)\n",
1787 ippErrorString(ipp_status
), cupsLastErrorString());
1789 if (ipp_status
<= IPP_OK_CONFLICT
)
1793 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1794 ipp_status
!= IPP_PRINTER_BUSY
)
1796 ippDelete(response
);
1797 ipp_status
= IPP_OK
;
1804 if ((job_state
= ippFindAttribute(response
, "job-state",
1805 IPP_TAG_ENUM
)) != NULL
)
1808 * Reflect the remote job state in the local queue...
1812 job_state
->values
[0].integer
>= IPP_JOB_PENDING
&&
1813 job_state
->values
[0].integer
<= IPP_JOB_COMPLETED
)
1814 update_reasons(NULL
,
1815 remote_job_states
[job_state
->values
[0].integer
-
1818 if ((job_sheets
= ippFindAttribute(response
,
1819 "job-media-sheets-completed",
1820 IPP_TAG_INTEGER
)) == NULL
)
1821 job_sheets
= ippFindAttribute(response
,
1822 "job-impressions-completed",
1826 fprintf(stderr
, "PAGE: total %d\n",
1827 job_sheets
->values
[0].integer
);
1830 * Stop polling if the job is finished or pending-held...
1833 if (job_state
->values
[0].integer
> IPP_JOB_STOPPED
)
1835 ippDelete(response
);
1839 else if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1840 ipp_status
!= IPP_NOT_POSSIBLE
&&
1841 ipp_status
!= IPP_PRINTER_BUSY
)
1844 * If the printer does not return a job-state attribute, it does not
1845 * conform to the IPP specification - break out immediately and fail
1849 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1850 "cups-ipp-missing-job-state");
1851 ipp_status
= IPP_INTERNAL_ERROR
;
1856 ippDelete(response
);
1859 * Wait before polling again...
1864 delay
= _cupsNextDelay(delay
, &prev_delay
);
1869 * Cancel the job as needed...
1872 if (job_canceled
> 0 && job_id
> 0)
1873 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1876 * Check the printer state and report it if necessary...
1879 check_printer_state(http
, uri
, resource
, argv
[2], version
);
1882 * Collect the final page count as needed...
1885 if (have_supplies
&&
1886 !backendSNMPSupplies(snmp_fd
, http
->hostaddr
, &page_count
, NULL
) &&
1887 page_count
> start_count
)
1888 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
1892 * See if we used Kerberos at all...
1896 auth_info_required
= "negotiate";
1897 #endif /* HAVE_GSSAPI */
1905 cupsFreeOptions(num_options
, options
);
1906 _ppdCacheDestroy(pc
);
1910 ippDelete(supported
);
1913 * Remove the temporary file(s) if necessary...
1917 unlink(tmpfilename
);
1922 for (i
= 0; i
< num_files
; i
++)
1925 #endif /* HAVE_LIBZ */
1928 * Return the queue status...
1931 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
1932 ipp_status
== IPP_AUTHENTICATION_CANCELED
||
1933 ipp_status
<= IPP_OK_CONFLICT
)
1934 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
1936 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
1937 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1938 return (CUPS_BACKEND_AUTH_REQUIRED
);
1939 else if (ipp_status
== IPP_INTERNAL_ERROR
)
1940 return (CUPS_BACKEND_STOP
);
1941 else if (ipp_status
== IPP_CONFLICT
)
1942 return (CUPS_BACKEND_FAILED
);
1943 else if (ipp_status
== IPP_REQUEST_VALUE
||
1944 ipp_status
== IPP_DOCUMENT_FORMAT
|| job_canceled
< 0)
1946 if (ipp_status
== IPP_REQUEST_VALUE
)
1947 _cupsLangPrintFilter(stderr
, "ERROR", _("Print job too large."));
1948 else if (ipp_status
== IPP_DOCUMENT_FORMAT
)
1949 _cupsLangPrintFilter(stderr
, "ERROR",
1950 _("Printer cannot print supplied content."));
1952 _cupsLangPrintFilter(stderr
, "ERROR", _("Print job canceled at printer."));
1954 return (CUPS_BACKEND_CANCEL
);
1956 else if (ipp_status
> IPP_OK_CONFLICT
&& ipp_status
!= IPP_ERROR_JOB_CANCELED
)
1957 return (CUPS_BACKEND_RETRY_CURRENT
);
1959 return (CUPS_BACKEND_OK
);
1964 * 'cancel_job()' - Cancel a print job.
1968 cancel_job(http_t
*http
, /* I - HTTP connection */
1969 const char *uri
, /* I - printer-uri */
1970 int id
, /* I - job-id */
1971 const char *resource
, /* I - Resource path */
1972 const char *user
, /* I - requesting-user-name */
1973 int version
) /* I - IPP version */
1975 ipp_t
*request
; /* Cancel-Job request */
1978 _cupsLangPrintFilter(stderr
, "INFO", _("Canceling print job."));
1980 request
= ippNewRequest(IPP_CANCEL_JOB
);
1981 request
->request
.op
.version
[0] = version
/ 10;
1982 request
->request
.op
.version
[1] = version
% 10;
1984 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1986 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1988 if (user
&& user
[0])
1989 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1990 "requesting-user-name", NULL
, user
);
1996 ippDelete(cupsDoRequest(http
, request
, resource
));
1998 if (cupsLastError() > IPP_OK_CONFLICT
)
1999 _cupsLangPrintFilter(stderr
, "ERROR", _("Unable to cancel print job."));
2004 * 'check_printer_state()' - Check the printer state.
2007 static ipp_pstate_t
/* O - Current printer-state */
2008 check_printer_state(
2009 http_t
*http
, /* I - HTTP connection */
2010 const char *uri
, /* I - Printer URI */
2011 const char *resource
, /* I - Resource path */
2012 const char *user
, /* I - Username, if any */
2013 int version
) /* I - IPP version */
2015 ipp_t
*request
, /* IPP request */
2016 *response
; /* IPP response */
2017 ipp_attribute_t
*attr
; /* Attribute in response */
2018 ipp_pstate_t printer_state
= IPP_PRINTER_STOPPED
;
2019 /* Current printer-state */
2023 * Send a Get-Printer-Attributes request and log the results...
2026 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
2027 request
->request
.op
.version
[0] = version
/ 10;
2028 request
->request
.op
.version
[1] = version
% 10;
2030 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2033 if (user
&& user
[0])
2034 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2035 "requesting-user-name", NULL
, user
);
2037 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2038 "requested-attributes",
2039 (int)(sizeof(pattrs
) / sizeof(pattrs
[0])), NULL
, pattrs
);
2041 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
2043 report_printer_state(response
);
2045 if ((attr
= ippFindAttribute(response
, "printer-state",
2046 IPP_TAG_ENUM
)) != NULL
)
2047 printer_state
= (ipp_pstate_t
)attr
->values
[0].integer
;
2049 ippDelete(response
);
2052 fprintf(stderr
, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
2053 ippErrorString(cupsLastError()), cupsLastErrorString());
2055 if (cupsLastError() <= IPP_OK_CONFLICT
)
2059 * Return the printer-state value...
2062 return (printer_state
);
2068 * 'compress_files()' - Compress print files.
2072 compress_files(int num_files
, /* I - Number of files */
2073 char **files
) /* I - Files */
2075 int i
, /* Looping var */
2076 fd
; /* Temporary file descriptor */
2077 ssize_t bytes
; /* Bytes read/written */
2078 size_t total
; /* Total bytes read */
2079 cups_file_t
*in
, /* Input file */
2080 *out
; /* Output file */
2081 struct stat outinfo
; /* Output file information */
2082 char filename
[1024], /* Temporary filename */
2083 buffer
[32768]; /* Copy buffer */
2086 fprintf(stderr
, "DEBUG: Compressing %d job files...\n", num_files
);
2087 for (i
= 0; i
< num_files
; i
++)
2089 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
2091 _cupsLangPrintError("ERROR", _("Unable to create compressed print file"));
2092 exit(CUPS_BACKEND_FAILED
);
2095 if ((out
= cupsFileOpenFd(fd
, "w9")) == NULL
)
2097 _cupsLangPrintError("ERROR", _("Unable to open compressed print file"));
2098 exit(CUPS_BACKEND_FAILED
);
2101 if ((in
= cupsFileOpen(files
[i
], "r")) == NULL
)
2103 _cupsLangPrintError("ERROR", _("Unable to open print file"));
2105 exit(CUPS_BACKEND_FAILED
);
2109 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
2110 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
2112 _cupsLangPrintError("ERROR",
2113 _("Unable to generate compressed print file"));
2116 exit(CUPS_BACKEND_FAILED
);
2124 files
[i
] = strdup(filename
);
2126 if (!stat(filename
, &outinfo
))
2128 "DEBUG: File %d compressed to %.1f%% of original size, "
2129 CUPS_LLFMT
" bytes...\n",
2130 i
+ 1, 100.0 * outinfo
.st_size
/ total
,
2131 CUPS_LLCAST outinfo
.st_size
);
2134 #endif /* HAVE_LIBZ */
2138 * 'monitor_printer()' - Monitor the printer state.
2141 static void * /* O - Thread exit code */
2143 _cups_monitor_t
*monitor
) /* I - Monitoring data */
2145 http_t
*http
; /* Connection to printer */
2146 ipp_t
*request
, /* IPP request */
2147 *response
; /* IPP response */
2148 ipp_attribute_t
*attr
; /* Attribute in response */
2149 int delay
, /* Current delay */
2150 prev_delay
; /* Previous delay */
2151 ipp_op_t job_op
; /* Operation to use */
2152 int job_id
; /* Job ID */
2153 const char *job_name
; /* Job name */
2154 ipp_jstate_t job_state
; /* Job state */
2155 const char *job_user
; /* Job originating user name */
2159 * Make a copy of the printer connection...
2162 http
= _httpCreate(monitor
->hostname
, monitor
->port
, NULL
, monitor
->encryption
,
2164 httpSetTimeout(http
, 30.0, timeout_cb
, NULL
);
2166 cupsSetUser(username
);
2167 cupsSetPasswordCB2(password_cb
, NULL
);
2170 * Loop until the job is canceled, aborted, or completed.
2173 delay
= _cupsNextDelay(0, &prev_delay
);
2175 while (monitor
->job_state
< IPP_JOB_CANCELED
&& !job_canceled
)
2178 * Reconnect to the printer...
2181 if (!httpReconnect(http
))
2184 * Connected, so check on the printer state...
2187 monitor
->printer_state
= check_printer_state(http
, monitor
->uri
,
2193 * Check the status of the job itself...
2196 job_op
= (monitor
->job_id
> 0 && monitor
->get_job_attrs
) ?
2197 IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
;
2198 request
= ippNewRequest(job_op
);
2199 request
->request
.op
.version
[0] = monitor
->version
/ 10;
2200 request
->request
.op
.version
[1] = monitor
->version
% 10;
2202 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2203 NULL
, monitor
->uri
);
2204 if (job_op
== IPP_GET_JOB_ATTRIBUTES
)
2205 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
2208 if (monitor
->user
&& monitor
->user
[0])
2209 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2210 "requesting-user-name", NULL
, monitor
->user
);
2212 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2213 "requested-attributes",
2214 (int)(sizeof(jattrs
) / sizeof(jattrs
[0])), NULL
, jattrs
);
2220 response
= cupsDoRequest(http
, request
, monitor
->resource
);
2222 fprintf(stderr
, "DEBUG: (monitor) %s: %s (%s)\n", ippOpString(job_op
),
2223 ippErrorString(cupsLastError()), cupsLastErrorString());
2225 if (cupsLastError() <= IPP_OK_CONFLICT
)
2228 if (job_op
== IPP_GET_JOB_ATTRIBUTES
)
2230 if ((attr
= ippFindAttribute(response
, "job-state",
2231 IPP_TAG_ENUM
)) != NULL
)
2232 monitor
->job_state
= (ipp_jstate_t
)attr
->values
[0].integer
;
2234 monitor
->job_state
= IPP_JOB_COMPLETED
;
2238 for (attr
= response
->attrs
; attr
; attr
= attr
->next
)
2242 job_state
= IPP_JOB_PENDING
;
2245 while (attr
&& attr
->group_tag
!= IPP_TAG_JOB
)
2251 while (attr
&& attr
->group_tag
== IPP_TAG_JOB
)
2253 if (!strcmp(attr
->name
, "job-id") &&
2254 attr
->value_tag
== IPP_TAG_INTEGER
)
2255 job_id
= attr
->values
[0].integer
;
2256 else if (!strcmp(attr
->name
, "job-name") &&
2257 (attr
->value_tag
== IPP_TAG_NAME
||
2258 attr
->value_tag
== IPP_TAG_NAMELANG
))
2259 job_name
= attr
->values
[0].string
.text
;
2260 else if (!strcmp(attr
->name
, "job-state") &&
2261 attr
->value_tag
== IPP_TAG_ENUM
)
2262 job_state
= attr
->values
[0].integer
;
2263 else if (!strcmp(attr
->name
, "job-originating-user-name") &&
2264 (attr
->value_tag
== IPP_TAG_NAME
||
2265 attr
->value_tag
== IPP_TAG_NAMELANG
))
2266 job_user
= attr
->values
[0].string
.text
;
2271 if (job_id
> 0 && job_name
&& !strcmp(job_name
, monitor
->job_name
) &&
2272 job_user
&& monitor
->user
&& !strcmp(job_user
, monitor
->user
))
2274 monitor
->job_id
= job_id
;
2275 monitor
->job_state
= job_state
;
2284 ippDelete(response
);
2286 fprintf(stderr
, "DEBUG: (monitor) job-state=%s\n",
2287 ippEnumString("job-state", monitor
->job_state
));
2289 if (!job_canceled
&&
2290 (monitor
->job_state
== IPP_JOB_CANCELED
||
2291 monitor
->job_state
== IPP_JOB_ABORTED
))
2295 * Disconnect from the printer - we'll reconnect on the next poll...
2298 _httpDisconnect(http
);
2302 * Sleep for N seconds...
2307 delay
= _cupsNextDelay(delay
, &prev_delay
);
2311 * Cancel the job if necessary...
2314 if (job_canceled
> 0 && monitor
->job_id
> 0)
2315 if (!httpReconnect(http
))
2316 cancel_job(http
, monitor
->uri
, monitor
->job_id
, monitor
->resource
,
2317 monitor
->user
, monitor
->version
);
2320 * Cleanup and return...
2330 * 'new_request()' - Create a new print creation or validation request.
2333 static ipp_t
* /* O - Request data */
2335 ipp_op_t op
, /* I - IPP operation code */
2336 int version
, /* I - IPP version number */
2337 const char *uri
, /* I - printer-uri value */
2338 const char *user
, /* I - requesting-user-name value */
2339 const char *title
, /* I - job-name value */
2340 int num_options
, /* I - Number of options to send */
2341 cups_option_t
*options
, /* I - Options to send */
2342 const char *compression
, /* I - compression value or NULL */
2343 int copies
, /* I - copies value or 0 */
2344 const char *format
, /* I - document-format value or NULL */
2345 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2346 ipp_attribute_t
*media_col_sup
, /* I - media-col-supported values */
2347 ipp_attribute_t
*doc_handling_sup
) /* I - multiple-document-handling-supported values */
2349 int i
; /* Looping var */
2350 ipp_t
*request
; /* Request data */
2351 const char *keyword
; /* PWG keyword */
2352 _pwg_size_t
*size
; /* PWG media size */
2353 ipp_t
*media_col
, /* media-col value */
2354 *media_size
; /* media-size value */
2355 const char *media_source
, /* media-source value */
2356 *media_type
, /* media-type value */
2357 *collate_str
, /* multiple-document-handling value */
2358 *mandatory
; /* Mandatory attributes */
2362 * Create the IPP request...
2365 request
= ippNewRequest(op
);
2366 request
->request
.op
.version
[0] = version
/ 10;
2367 request
->request
.op
.version
[1] = version
% 10;
2369 fprintf(stderr
, "DEBUG: %s IPP/%d.%d\n",
2370 ippOpString(request
->request
.op
.operation_id
),
2371 request
->request
.op
.version
[0],
2372 request
->request
.op
.version
[1]);
2375 * Add standard attributes...
2378 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2380 fprintf(stderr
, "DEBUG: printer-uri=\"%s\"\n", uri
);
2384 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2385 "requesting-user-name", NULL
, user
);
2386 fprintf(stderr
, "DEBUG: requesting-user-name=\"%s\"\n", user
);
2389 if (title
&& *title
)
2391 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
2393 fprintf(stderr
, "DEBUG: job-name=\"%s\"\n", title
);
2396 if (format
&& op
!= IPP_CREATE_JOB
)
2398 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
2399 "document-format", NULL
, format
);
2400 fprintf(stderr
, "DEBUG: document-format=\"%s\"\n", format
);
2404 if (compression
&& op
!= IPP_CREATE_JOB
)
2406 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2407 "compression", NULL
, compression
);
2408 fprintf(stderr
, "DEBUG: compression=\"%s\"\n", compression
);
2410 #endif /* HAVE_LIBZ */
2413 * Handle options on the command-line...
2416 if (num_options
> 0)
2420 int num_finishings
= 0, /* Number of finishing values */
2421 finishings
[10]; /* Finishing enum values */
2424 * Send standard IPP attributes...
2428 (keyword
= cupsGetOption("job-password", num_options
,
2431 ippAddOctetString(request
, IPP_TAG_OPERATION
, "job-password",
2432 keyword
, strlen(keyword
));
2433 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2434 "job-password-encryption", NULL
, "none");
2437 if (pc
->account_id
&&
2438 (keyword
= cupsGetOption("job-account-id", num_options
,
2440 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-account-id",
2443 if (pc
->account_id
&&
2444 (keyword
= cupsGetOption("job-accounting-user-id", num_options
,
2446 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_NAME
,
2447 "job-accounting-user-id", NULL
, keyword
);
2449 for (mandatory
= (char *)cupsArrayFirst(pc
->mandatory
);
2451 mandatory
= (char *)cupsArrayNext(pc
->mandatory
))
2453 if (strcmp(mandatory
, "copies") &&
2454 strcmp(mandatory
, "destination-uris") &&
2455 strcmp(mandatory
, "finishings") &&
2456 strcmp(mandatory
, "job-account-id") &&
2457 strcmp(mandatory
, "job-accounting-user-id") &&
2458 strcmp(mandatory
, "job-password") &&
2459 strcmp(mandatory
, "media") &&
2460 strncmp(mandatory
, "media-col", 9) &&
2461 strcmp(mandatory
, "multiple-document-handling") &&
2462 strcmp(mandatory
, "output-bin") &&
2463 strcmp(mandatory
, "print-color-mode") &&
2464 strcmp(mandatory
, "print-quality") &&
2465 strcmp(mandatory
, "sides") &&
2466 (keyword
= cupsGetOption(mandatory
, num_options
, options
)) != NULL
)
2468 _ipp_option_t
*opt
= _ippFindOption(mandatory
);
2470 ipp_tag_t value_tag
= opt
? opt
->value_tag
: IPP_TAG_NAME
;
2475 case IPP_TAG_INTEGER
:
2477 ippAddInteger(request
, IPP_TAG_JOB
, value_tag
, mandatory
,
2480 case IPP_TAG_BOOLEAN
:
2481 ippAddBoolean(request
, IPP_TAG_JOB
, mandatory
,
2482 !_cups_strcasecmp(keyword
, "true"));
2484 case IPP_TAG_RANGE
:
2486 int lower
, upper
; /* Range */
2488 if (sscanf(keyword
, "%d-%d", &lower
, &upper
) != 2)
2489 lower
= upper
= atoi(keyword
);
2491 ippAddRange(request
, IPP_TAG_JOB
, mandatory
, lower
, upper
);
2494 case IPP_TAG_STRING
:
2495 ippAddOctetString(request
, IPP_TAG_JOB
, mandatory
, keyword
,
2499 ippAddString(request
, IPP_TAG_JOB
, value_tag
, mandatory
,
2506 if ((keyword
= cupsGetOption("PageSize", num_options
, options
)) == NULL
)
2507 keyword
= cupsGetOption("media", num_options
, options
);
2509 if ((size
= _ppdCacheGetSize(pc
, keyword
)) != NULL
)
2512 * Add a media-col value...
2515 media_size
= ippNew();
2516 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2517 "x-dimension", size
->width
);
2518 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2519 "y-dimension", size
->length
);
2521 media_col
= ippNew();
2522 ippAddCollection(media_col
, IPP_TAG_ZERO
, "media-size", media_size
);
2524 media_source
= _ppdCacheGetSource(pc
, cupsGetOption("InputSlot",
2527 media_type
= _ppdCacheGetType(pc
, cupsGetOption("MediaType",
2531 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
2533 if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2534 "media-left-margin"))
2535 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2536 "media-left-margin", size
->left
);
2537 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2538 "media-bottom-margin"))
2539 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2540 "media-bottom-margin", size
->bottom
);
2541 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2542 "media-right-margin"))
2543 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2544 "media-right-margin", size
->right
);
2545 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2546 "media-top-margin"))
2547 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2548 "media-top-margin", size
->top
);
2549 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2550 "media-source") && media_source
)
2551 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
2552 "media-source", NULL
, media_source
);
2553 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2554 "media-type") && media_type
)
2555 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
2556 "media-type", NULL
, media_type
);
2559 ippAddCollection(request
, IPP_TAG_JOB
, "media-col", media_col
);
2562 if ((keyword
= cupsGetOption("output-bin", num_options
,
2564 keyword
= _ppdCacheGetBin(pc
, cupsGetOption("OutputBin", num_options
,
2568 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-bin",
2571 if ((keyword
= cupsGetOption("print-color-mode", num_options
,
2573 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "print-color-mode",
2575 else if ((keyword
= cupsGetOption("ColorModel", num_options
,
2578 if (!_cups_strcasecmp(keyword
, "Gray"))
2579 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2580 "print-color-mode", NULL
, "monochrome");
2582 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2583 "print-color-mode", NULL
, "color");
2586 if ((keyword
= cupsGetOption("print-quality", num_options
,
2588 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2590 else if ((keyword
= cupsGetOption("cupsPrintQuality", num_options
,
2593 if (!_cups_strcasecmp(keyword
, "draft"))
2594 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2596 else if (!_cups_strcasecmp(keyword
, "normal"))
2597 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2598 IPP_QUALITY_NORMAL
);
2599 else if (!_cups_strcasecmp(keyword
, "high"))
2600 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2604 if ((keyword
= cupsGetOption("sides", num_options
, options
)) != NULL
)
2605 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2607 else if (pc
->sides_option
&&
2608 (keyword
= cupsGetOption(pc
->sides_option
, num_options
,
2611 if (!_cups_strcasecmp(keyword
, pc
->sides_1sided
))
2612 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2614 else if (!_cups_strcasecmp(keyword
, pc
->sides_2sided_long
))
2615 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2616 NULL
, "two-sided-long-edge");
2617 if (!_cups_strcasecmp(keyword
, pc
->sides_2sided_short
))
2618 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2619 NULL
, "two-sided-short-edge");
2622 if ((keyword
= cupsGetOption("multiple-document-handling",
2623 num_options
, options
)) != NULL
)
2625 if (strstr(keyword
, "uncollated"))
2630 else if ((keyword
= cupsGetOption("collate", num_options
,
2636 if (!_cups_strcasecmp(format
, "image/gif") ||
2637 !_cups_strcasecmp(format
, "image/jp2") ||
2638 !_cups_strcasecmp(format
, "image/jpeg") ||
2639 !_cups_strcasecmp(format
, "image/png") ||
2640 !_cups_strcasecmp(format
, "image/tiff") ||
2641 !_cups_strncasecmp(format
, "image/x-", 8))
2644 * Collation makes no sense for single page image formats...
2649 else if (!_cups_strncasecmp(format
, "image/", 6) ||
2650 !_cups_strcasecmp(format
, "application/vnd.cups-raster"))
2653 * Multi-page image formats will have copies applied by the upstream
2661 if (doc_handling_sup
)
2663 if (!_cups_strcasecmp(keyword
, "true"))
2664 collate_str
= "separate-documents-collated-copies";
2666 collate_str
= "separate-documents-uncollated-copies";
2668 for (i
= 0; i
< doc_handling_sup
->num_values
; i
++)
2669 if (!strcmp(doc_handling_sup
->values
[i
].string
.text
, collate_str
))
2671 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2672 "multiple-document-handling", NULL
, collate_str
);
2676 if (i
>= doc_handling_sup
->num_values
)
2681 * Map finishing options...
2684 num_finishings
= _ppdCacheGetFinishingValues(pc
, num_options
, options
,
2685 (int)(sizeof(finishings
) /
2686 sizeof(finishings
[0])),
2688 if (num_finishings
> 0)
2689 ippAddIntegers(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "finishings",
2690 num_finishings
, finishings
);
2693 * Map FaxOut options...
2696 if ((keyword
= cupsGetOption("phone", num_options
, options
)) != NULL
)
2698 ipp_t
*destination
; /* destination collection */
2699 char tel_uri
[1024]; /* tel: URI */
2701 destination
= ippNew();
2703 httpAssembleURI(HTTP_URI_CODING_ALL
, tel_uri
, sizeof(tel_uri
), "tel",
2704 NULL
, NULL
, 0, keyword
);
2705 ippAddString(destination
, IPP_TAG_JOB
, IPP_TAG_URI
, "destination-uri",
2708 if ((keyword
= cupsGetOption("faxPrefix", num_options
,
2709 options
)) != NULL
&& *keyword
)
2710 ippAddString(destination
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
2711 "pre-dial-string", NULL
, keyword
);
2713 ippAddCollection(request
, IPP_TAG_JOB
, "destination-uris", destination
);
2714 ippDelete(destination
);
2720 * When talking to another CUPS server, send all options...
2723 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_JOB
);
2726 if (copies
> 1 && (!pc
|| copies
<= pc
->max_copies
))
2727 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies", copies
);
2735 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
2738 static const char * /* O - Password */
2739 password_cb(const char *prompt
, /* I - Prompt (not used) */
2740 http_t
*http
, /* I - Connection */
2741 const char *method
, /* I - Request method (not used) */
2742 const char *resource
, /* I - Resource path (not used) */
2743 void *user_data
) /* I - User data (not used) */
2745 char def_username
[HTTP_MAX_VALUE
]; /* Default username */
2748 fprintf(stderr
, "DEBUG: password_cb(prompt=\"%s\"), password=%p, "
2749 "password_tries=%d\n", prompt
, password
, password_tries
);
2757 * Remember that we need to authenticate...
2760 auth_info_required
= "username,password";
2762 if (httpGetSubField(http
, HTTP_FIELD_WWW_AUTHENTICATE
, "username",
2765 char quoted
[HTTP_MAX_VALUE
* 2 + 4];
2768 fprintf(stderr
, "ATTR: auth-info-default=%s,\n",
2769 quote_string(def_username
, quoted
, sizeof(quoted
)));
2772 if (password
&& *password
&& password_tries
< 3)
2781 * Give up after 3 tries or if we don't have a password to begin with...
2790 * 'quote_string()' - Quote a string value.
2793 static const char * /* O - Quoted string */
2794 quote_string(const char *s
, /* I - String */
2795 char *q
, /* I - Quoted string buffer */
2796 size_t qsize
) /* I - Size of quoted string buffer */
2798 char *qptr
, /* Pointer into string buffer */
2799 *qend
; /* End of string buffer */
2803 qend
= q
+ qsize
- 5;
2814 while (*s
&& qptr
< qend
)
2816 if (*s
== '\\' || *s
== '\"' || *s
== '\'')
2840 * 'report_attr()' - Report an IPP attribute value.
2844 report_attr(ipp_attribute_t
*attr
) /* I - Attribute */
2846 int i
; /* Looping var */
2847 char value
[1024], /* Value string */
2848 *valptr
; /* Pointer into value string */
2849 const char *cached
; /* Cached attribute */
2853 * Convert the attribute values into quoted strings...
2856 for (i
= 0, valptr
= value
;
2857 i
< attr
->num_values
&& valptr
< (value
+ sizeof(value
) - 10);
2863 switch (attr
->value_tag
)
2865 case IPP_TAG_INTEGER
:
2867 snprintf(valptr
, sizeof(value
) - (valptr
- value
), "%d",
2868 attr
->values
[i
].integer
);
2869 valptr
+= strlen(valptr
);
2874 case IPP_TAG_KEYWORD
:
2875 quote_string(attr
->values
[i
].string
.text
, valptr
,
2876 value
+ sizeof(value
) - valptr
);
2877 valptr
+= strlen(valptr
);
2882 * Unsupported value type...
2891 _cupsMutexLock(&report_mutex
);
2893 if ((cached
= cupsGetOption(attr
->name
, num_attr_cache
,
2894 attr_cache
)) == NULL
|| strcmp(cached
, value
))
2897 * Tell the scheduler about the new values...
2900 num_attr_cache
= cupsAddOption(attr
->name
, value
, num_attr_cache
,
2902 fprintf(stderr
, "ATTR: %s=%s\n", attr
->name
, value
);
2905 _cupsMutexUnlock(&report_mutex
);
2910 * 'report_printer_state()' - Report the printer state.
2914 report_printer_state(ipp_t
*ipp
) /* I - IPP response */
2916 ipp_attribute_t
*pa
, /* printer-alert */
2917 *pam
, /* printer-alert-message */
2918 *psm
, /* printer-state-message */
2919 *reasons
, /* printer-state-reasons */
2920 *marker
; /* marker-* attributes */
2921 char value
[1024], /* State/message string */
2922 *valptr
; /* Pointer into string */
2923 static int ipp_supplies
= -1;
2924 /* Report supply levels? */
2928 * Report alerts and messages...
2931 if ((pa
= ippFindAttribute(ipp
, "printer-alert", IPP_TAG_TEXT
)) != NULL
)
2934 if ((pam
= ippFindAttribute(ipp
, "printer-alert-message",
2935 IPP_TAG_TEXT
)) != NULL
)
2938 if ((psm
= ippFindAttribute(ipp
, "printer-state-message",
2939 IPP_TAG_TEXT
)) != NULL
)
2941 char *ptr
; /* Pointer into message */
2944 strlcpy(value
, "INFO: ", sizeof(value
));
2945 for (ptr
= psm
->values
[0].string
.text
, valptr
= value
+ 6;
2946 *ptr
&& valptr
< (value
+ sizeof(value
) - 6);
2949 if (*ptr
< ' ' && *ptr
> 0 && *ptr
!= '\t')
2952 * Substitute "<XX>" for the control character; sprintf is safe because
2953 * we always leave 6 chars free at the end...
2956 sprintf(valptr
, "<%02X>", *ptr
);
2966 fputs(value
, stderr
);
2970 * Now report printer-state-reasons, filtering out some of the reasons we never
2974 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
2975 IPP_TAG_KEYWORD
)) == NULL
)
2978 update_reasons(reasons
, NULL
);
2981 * Relay the current marker-* attribute values...
2984 if (ipp_supplies
< 0)
2986 ppd_file_t
*ppd
; /* PPD file */
2987 ppd_attr_t
*ppdattr
; /* Attribute in PPD file */
2989 if ((ppd
= ppdOpenFile(getenv("PPD"))) != NULL
&&
2990 (ppdattr
= ppdFindAttr(ppd
, "cupsIPPSupplies", NULL
)) != NULL
&&
2991 ppdattr
->value
&& _cups_strcasecmp(ppdattr
->value
, "true"))
2999 if (ipp_supplies
> 0)
3001 if ((marker
= ippFindAttribute(ipp
, "marker-colors", IPP_TAG_NAME
)) != NULL
)
3002 report_attr(marker
);
3003 if ((marker
= ippFindAttribute(ipp
, "marker-high-levels",
3004 IPP_TAG_INTEGER
)) != NULL
)
3005 report_attr(marker
);
3006 if ((marker
= ippFindAttribute(ipp
, "marker-levels",
3007 IPP_TAG_INTEGER
)) != NULL
)
3008 report_attr(marker
);
3009 if ((marker
= ippFindAttribute(ipp
, "marker-low-levels",
3010 IPP_TAG_INTEGER
)) != NULL
)
3011 report_attr(marker
);
3012 if ((marker
= ippFindAttribute(ipp
, "marker-message",
3013 IPP_TAG_TEXT
)) != NULL
)
3014 report_attr(marker
);
3015 if ((marker
= ippFindAttribute(ipp
, "marker-names", IPP_TAG_NAME
)) != NULL
)
3016 report_attr(marker
);
3017 if ((marker
= ippFindAttribute(ipp
, "marker-types",
3018 IPP_TAG_KEYWORD
)) != NULL
)
3019 report_attr(marker
);
3024 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
3026 * 'run_as_user()' - Run the IPP backend as the printing user.
3028 * This function uses an XPC-based user agent to run the backend as the printing
3029 * user. We need to do this in order to have access to the user's Kerberos
3033 static int /* O - Exit status */
3034 run_as_user(char *argv
[], /* I - Command-line arguments */
3035 uid_t uid
, /* I - User ID */
3036 const char *device_uri
, /* I - Device URI */
3037 int fd
) /* I - File to print */
3039 const char *auth_negotiate
;/* AUTH_NEGOTIATE env var */
3040 xpc_connection_t conn
; /* Connection to XPC service */
3041 xpc_object_t request
; /* Request message dictionary */
3042 __block xpc_object_t response
; /* Response message dictionary */
3043 dispatch_semaphore_t sem
; /* Semaphore for waiting for response */
3044 int status
= CUPS_BACKEND_FAILED
;
3045 /* Status of request */
3048 fprintf(stderr
, "DEBUG: Running IPP backend as UID %d.\n", (int)uid
);
3051 * Connect to the user agent for the specified UID...
3054 conn
= xpc_connection_create_mach_service(kPMPrintUIToolAgent
,
3055 dispatch_get_global_queue(0, 0), 0);
3058 _cupsLangPrintFilter(stderr
, "ERROR",
3059 _("Unable to start backend process."));
3060 fputs("DEBUG: Unable to create connection to agent.\n", stderr
);
3064 xpc_connection_set_event_handler(conn
,
3065 ^(xpc_object_t event
)
3067 xpc_type_t messageType
= xpc_get_type(event
);
3069 if (messageType
== XPC_TYPE_ERROR
)
3071 if (event
== XPC_ERROR_CONNECTION_INTERRUPTED
)
3072 fprintf(stderr
, "DEBUG: Interrupted connection to service %s.\n",
3073 xpc_connection_get_name(conn
));
3074 else if (event
== XPC_ERROR_CONNECTION_INVALID
)
3075 fprintf(stderr
, "DEBUG: Connection invalid for service %s.\n",
3076 xpc_connection_get_name(conn
));
3078 fprintf(stderr
, "DEBUG: Unxpected error for service %s: %s\n",
3079 xpc_connection_get_name(conn
),
3080 xpc_dictionary_get_string(event
, XPC_ERROR_KEY_DESCRIPTION
));
3083 xpc_connection_set_target_uid(conn
, uid
);
3084 xpc_connection_resume(conn
);
3087 * Try starting the backend...
3090 request
= xpc_dictionary_create(NULL
, NULL
, 0);
3091 xpc_dictionary_set_int64(request
, "command", kPMStartJob
);
3092 xpc_dictionary_set_string(request
, "device-uri", device_uri
);
3093 xpc_dictionary_set_string(request
, "job-id", argv
[1]);
3094 xpc_dictionary_set_string(request
, "user", argv
[2]);
3095 xpc_dictionary_set_string(request
, "title", argv
[3]);
3096 xpc_dictionary_set_string(request
, "copies", argv
[4]);
3097 xpc_dictionary_set_string(request
, "options", argv
[5]);
3098 xpc_dictionary_set_string(request
, "auth-info-required",
3099 getenv("AUTH_INFO_REQUIRED"));
3100 if ((auth_negotiate
= getenv("AUTH_NEGOTIATE")) != NULL
)
3101 xpc_dictionary_set_string(request
, "auth-negotiate", auth_negotiate
);
3102 xpc_dictionary_set_fd(request
, "stdin", fd
);
3103 xpc_dictionary_set_fd(request
, "stderr", 2);
3104 xpc_dictionary_set_fd(request
, "side-channel", CUPS_SC_FD
);
3106 sem
= dispatch_semaphore_create(0);
3109 xpc_connection_send_message_with_reply(conn
, request
,
3110 dispatch_get_global_queue(0,0),
3111 ^(xpc_object_t reply
)
3113 /* Save the response and wake up */
3114 if (xpc_get_type(reply
)
3115 == XPC_TYPE_DICTIONARY
)
3116 response
= xpc_retain(reply
);
3118 dispatch_semaphore_signal(sem
);
3121 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
3122 xpc_release(request
);
3123 dispatch_release(sem
);
3127 child_pid
= xpc_dictionary_get_int64(response
, "child-pid");
3129 xpc_release(response
);
3132 fprintf(stderr
, "DEBUG: Child PID=%d.\n", child_pid
);
3135 _cupsLangPrintFilter(stderr
, "ERROR",
3136 _("Unable to start backend process."));
3137 fputs("DEBUG: No child PID.\n", stderr
);
3143 _cupsLangPrintFilter(stderr
, "ERROR",
3144 _("Unable to start backend process."));
3145 fputs("DEBUG: No reply from agent.\n", stderr
);
3150 * Then wait for the backend to finish...
3153 request
= xpc_dictionary_create(NULL
, NULL
, 0);
3154 xpc_dictionary_set_int64(request
, "command", kPMWaitForJob
);
3155 xpc_dictionary_set_fd(request
, "stderr", 2);
3157 sem
= dispatch_semaphore_create(0);
3160 xpc_connection_send_message_with_reply(conn
, request
,
3161 dispatch_get_global_queue(0,0),
3162 ^(xpc_object_t reply
)
3164 /* Save the response and wake up */
3165 if (xpc_get_type(reply
)
3166 == XPC_TYPE_DICTIONARY
)
3167 response
= xpc_retain(reply
);
3169 dispatch_semaphore_signal(sem
);
3172 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
3173 xpc_release(request
);
3174 dispatch_release(sem
);
3178 status
= xpc_dictionary_get_int64(response
, "status");
3180 if (status
== SIGTERM
|| status
== SIGKILL
|| status
== SIGPIPE
)
3182 fprintf(stderr
, "DEBUG: Child terminated on signal %d.\n", status
);
3183 status
= CUPS_BACKEND_FAILED
;
3185 else if (WIFSIGNALED(status
))
3187 fprintf(stderr
, "DEBUG: Child crashed on signal %d.\n", status
);
3188 status
= CUPS_BACKEND_STOP
;
3190 else if (WIFEXITED(status
))
3192 status
= WEXITSTATUS(status
);
3193 fprintf(stderr
, "DEBUG: Child exited with status %d.\n", status
);
3196 xpc_release(response
);
3199 _cupsLangPrintFilter(stderr
, "ERROR",
3200 _("Unable to get backend exit status."));
3206 xpc_connection_suspend(conn
);
3207 xpc_connection_cancel(conn
);
3213 #endif /* HAVE_GSSAPI && HAVE_XPC */
3217 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
3221 sigterm_handler(int sig
) /* I - Signal */
3223 (void)sig
; /* remove compiler warnings... */
3225 write(2, "DEBUG: Got SIGTERM.\n", 20);
3227 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
3230 kill(child_pid
, sig
);
3233 #endif /* HAVE_GSSAPI && HAVE_XPC */
3238 * Flag that the job should be canceled...
3241 write(2, "DEBUG: job_canceled = 1.\n", 25);
3248 * The scheduler already tried to cancel us once, now just terminate
3249 * after removing our temp file!
3253 unlink(tmpfilename
);
3260 * 'timeout_cb()' - Handle HTTP timeouts.
3263 static int /* O - 1 to continue, 0 to cancel */
3264 timeout_cb(http_t
*http
, /* I - Connection to server (unused) */
3265 void *user_data
) /* I - User data (unused) */
3270 return (!job_canceled
);
3275 * 'update_reasons()' - Update the printer-state-reasons values.
3279 update_reasons(ipp_attribute_t
*attr
, /* I - printer-state-reasons or NULL */
3280 const char *s
) /* I - STATE: string or NULL */
3282 char op
; /* Add (+), remove (-), replace (\0) */
3283 cups_array_t
*new_reasons
; /* New reasons array */
3284 char *reason
, /* Current reason */
3285 add
[2048], /* Reasons added string */
3286 *addptr
, /* Pointer into add string */
3287 rem
[2048], /* Reasons removed string */
3288 *remptr
; /* Pointer into remove string */
3289 const char *addprefix
, /* Current add string prefix */
3290 *remprefix
; /* Current remove string prefix */
3293 fprintf(stderr
, "DEBUG: update_reasons(attr=%d(%s%s), s=\"%s\")\n",
3294 attr
? attr
->num_values
: 0, attr
? attr
->values
[0].string
.text
: "",
3295 attr
&& attr
->num_values
> 1 ? ",..." : "", s
? s
: "(null)");
3298 * Create an array of new reason keyword strings...
3303 int i
; /* Looping var */
3305 new_reasons
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
3308 for (i
= 0; i
< attr
->num_values
; i
++)
3310 reason
= attr
->values
[i
].string
.text
;
3312 if (strcmp(reason
, "none") &&
3313 strcmp(reason
, "none-report") &&
3314 strcmp(reason
, "paused") &&
3315 strncmp(reason
, "spool-area-full", 15) &&
3316 strcmp(reason
, "com.apple.print.recoverable-warning") &&
3317 strncmp(reason
, "cups-", 5))
3318 cupsArrayAdd(new_reasons
, reason
);
3323 if (*s
== '+' || *s
== '-')
3328 new_reasons
= _cupsArrayNewStrings(s
, ',');
3334 * Compute the changes...
3338 addprefix
= "STATE: +";
3341 remprefix
= "STATE: -";
3344 fprintf(stderr
, "DEBUG2: op='%c', new_reasons=%d, state_reasons=%d\n",
3345 op
? op
: ' ', cupsArrayCount(new_reasons
),
3346 cupsArrayCount(state_reasons
));
3348 _cupsMutexLock(&report_mutex
);
3356 for (reason
= (char *)cupsArrayFirst(new_reasons
);
3358 reason
= (char *)cupsArrayNext(new_reasons
))
3360 if (!cupsArrayFind(state_reasons
, reason
))
3362 if (!strncmp(reason
, "cups-remote-", 12))
3365 * If we are setting cups-remote-xxx, remove all other cups-remote-xxx
3369 char *temp
; /* Current reason in state_reasons */
3371 cupsArraySave(state_reasons
);
3373 for (temp
= (char *)cupsArrayFirst(state_reasons
);
3375 temp
= (char *)cupsArrayNext(state_reasons
))
3376 if (!strncmp(temp
, "cups-remote-", 12))
3378 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
3380 remptr
+= strlen(remptr
);
3383 cupsArrayRemove(state_reasons
, temp
);
3387 cupsArrayRestore(state_reasons
);
3390 cupsArrayAdd(state_reasons
, reason
);
3392 snprintf(addptr
, sizeof(add
) - (addptr
- add
), "%s%s", addprefix
,
3394 addptr
+= strlen(addptr
);
3405 for (reason
= (char *)cupsArrayFirst(new_reasons
);
3407 reason
= (char *)cupsArrayNext(new_reasons
))
3409 if (cupsArrayFind(state_reasons
, reason
))
3411 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
3413 remptr
+= strlen(remptr
);
3416 cupsArrayRemove(state_reasons
, reason
);
3423 * Replace reasons...
3426 for (reason
= (char *)cupsArrayFirst(state_reasons
);
3428 reason
= (char *)cupsArrayNext(state_reasons
))
3430 if (strncmp(reason
, "cups-", 5) && !cupsArrayFind(new_reasons
, reason
))
3432 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
3434 remptr
+= strlen(remptr
);
3437 cupsArrayRemove(state_reasons
, reason
);
3441 for (reason
= (char *)cupsArrayFirst(new_reasons
);
3443 reason
= (char *)cupsArrayNext(new_reasons
))
3445 if (!cupsArrayFind(state_reasons
, reason
))
3447 cupsArrayAdd(state_reasons
, reason
);
3449 snprintf(addptr
, sizeof(add
) - (addptr
- add
), "%s%s", addprefix
,
3451 addptr
+= strlen(addptr
);
3457 _cupsMutexUnlock(&report_mutex
);
3460 * Report changes and return...
3463 if (add
[0] && rem
[0])
3464 fprintf(stderr
, "%s\n%s\n", add
, rem
);
3466 fprintf(stderr
, "%s\n", add
);
3468 fprintf(stderr
, "%s\n", rem
);
3472 * End of "$Id: ipp.c 9759 2011-05-11 03:24:33Z mike $".