2 * "$Id: ipp.c 7948 2008-09-17 00:04:12Z mike $"
4 * IPP backend for CUPS.
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * "LICENSE" which should have been included with this file. If this
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * main() - Send a file to the printer or server.
20 * cancel_job() - Cancel a print job.
21 * check_printer_state() - Check the printer state.
22 * compress_files() - Compress print files...
23 * monitor_printer() - Monitor the printer state...
24 * new_request() - Create a new print creation or validation request.
25 * password_cb() - Disable the password prompt for
26 * cupsDoFileRequest().
27 * report_attr() - Report an IPP attribute value.
28 * report_printer_state() - Report the printer state.
29 * sigterm_handler() - Handle 'terminate' signals that stop the backend.
33 * Include necessary headers.
36 #include "backend-private.h"
37 #include <sys/types.h>
46 typedef struct _cups_monitor_s
/**** Monitoring data ****/
48 const char *uri
, /* Printer URI */
49 *hostname
, /* Hostname */
51 *resource
; /* Resource path */
52 int port
, /* Port number */
53 version
, /* IPP version */
54 job_id
; /* Job ID for submitted job */
55 http_encryption_t encryption
; /* Use encryption? */
56 ipp_jstate_t job_state
; /* Current job state */
57 ipp_pstate_t printer_state
; /* Current printer state */
65 static const char *auth_info_required
= "none";
66 /* New auth-info-required value */
67 static const char * const jattrs
[] = /* Job attributes we want */
69 "job-media-sheets-completed",
73 static int job_canceled
= 0; /* Job cancelled? */
74 static char *password
= NULL
; /* Password for device URI */
75 static int password_tries
= 0; /* Password tries */
76 static const char * const pattrs
[] = /* Printer attributes we want */
80 "document-format-supported",
88 "media-col-supported",
89 "operations-supported",
91 "printer-alert-description",
92 "printer-is-accepting-jobs",
94 "printer-state-message",
95 "printer-state-reasons",
103 static void cancel_job(http_t
*http
, const char *uri
, int id
,
104 const char *resource
, const char *user
,
106 static ipp_pstate_t
check_printer_state(http_t
*http
, const char *uri
,
107 const char *resource
,
108 const char *user
, int version
,
111 static void compress_files(int num_files
, char **files
);
112 #endif /* HAVE_LIBZ */
113 static void *monitor_printer(_cups_monitor_t
*monitor
);
114 static ipp_t
*new_request(ipp_op_t op
, int version
, const char *uri
,
115 const char *user
, const char *title
,
116 int num_options
, cups_option_t
*options
,
117 const char *compression
, int copies
,
118 const char *format
, _pwg_t
*pwg
,
119 ipp_attribute_t
*media_col_sup
);
120 static const char *password_cb(const char *);
121 static void report_attr(ipp_attribute_t
*attr
);
122 static int report_printer_state(ipp_t
*ipp
, int job_id
);
123 static void sigterm_handler(int sig
);
127 * 'main()' - Send a file to the printer or server.
131 * printer-uri job-id user title copies options [file]
134 int /* O - Exit status */
135 main(int argc
, /* I - Number of command-line args */
136 char *argv
[]) /* I - Command-line arguments */
138 int i
; /* Looping var */
139 int send_options
; /* Send job options? */
140 int num_options
; /* Number of printer options */
141 cups_option_t
*options
; /* Printer options */
142 const char *device_uri
; /* Device URI */
143 char scheme
[255], /* Scheme in URI */
144 hostname
[1024], /* Hostname */
145 username
[255], /* Username info */
146 resource
[1024], /* Resource info (printer name) */
147 addrname
[256], /* Address name */
148 *optptr
, /* Pointer to URI options */
149 *name
, /* Name of option */
150 *value
, /* Value of option */
151 sep
; /* Separator character */
152 http_addrlist_t
*addrlist
; /* Address of printer */
153 int snmp_fd
, /* SNMP socket */
154 start_count
, /* Page count via SNMP at start */
155 page_count
, /* Page count via SNMP */
156 have_supplies
; /* Printer supports supply levels? */
157 int num_files
; /* Number of files to print */
158 char **files
; /* Files to print */
159 int port
; /* Port number (not used) */
160 char portname
[255]; /* Port name */
161 char uri
[HTTP_MAX_URI
]; /* Updated URI without user/pass */
162 http_status_t http_status
; /* Status of HTTP request */
163 ipp_status_t ipp_status
; /* Status of IPP request */
164 http_t
*http
; /* HTTP connection */
165 ipp_t
*request
, /* IPP request */
166 *response
, /* IPP response */
167 *supported
; /* get-printer-attributes response */
168 time_t start_time
; /* Time of first connect */
169 int contimeout
; /* Connection timeout */
170 int delay
; /* Delay for retries... */
171 const char *compression
; /* Compression mode */
172 int waitjob
, /* Wait for job complete? */
173 waitprinter
; /* Wait for printer ready? */
174 _cups_monitor_t monitor
; /* Monitoring data */
175 ipp_attribute_t
*job_id_attr
; /* job-id attribute */
176 int job_id
; /* job-id value */
177 ipp_attribute_t
*job_sheets
; /* job-media-sheets-completed */
178 ipp_attribute_t
*job_state
; /* job-state */
179 ipp_attribute_t
*copies_sup
; /* copies-supported */
180 ipp_attribute_t
*cups_version
; /* cups-version */
181 ipp_attribute_t
*format_sup
; /* document-format-supported */
182 ipp_attribute_t
*media_col_sup
; /* media-col-supported */
183 ipp_attribute_t
*operations_sup
; /* operations-supported */
184 ipp_attribute_t
*printer_state
; /* printer-state attribute */
185 ipp_attribute_t
*printer_accepting
; /* printer-is-accepting-jobs */
186 int validate_job
; /* Does printer support Validate-Job? */
187 int copies
, /* Number of copies for job */
188 copies_remaining
; /* Number of copies remaining */
189 const char *content_type
, /* CONTENT_TYPE environment variable */
190 *final_content_type
, /* FINAL_CONTENT_TYPE environment var */
191 *document_format
; /* document-format value */
192 int fd
; /* File descriptor */
193 off_t bytes
; /* Bytes copied */
194 char buffer
[16384]; /* Copy buffer */
195 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
196 struct sigaction action
; /* Actions for POSIX signals */
197 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
198 int version
; /* IPP version */
199 ppd_file_t
*ppd
; /* PPD file */
200 _pwg_t
*pwg
; /* PWG<->PPD mapping data */
204 * Make sure status messages are not buffered...
207 setbuf(stderr
, NULL
);
210 * Ignore SIGPIPE and catch SIGTERM signals...
214 sigset(SIGPIPE
, SIG_IGN
);
215 sigset(SIGTERM
, sigterm_handler
);
216 #elif defined(HAVE_SIGACTION)
217 memset(&action
, 0, sizeof(action
));
218 action
.sa_handler
= SIG_IGN
;
219 sigaction(SIGPIPE
, &action
, NULL
);
221 sigemptyset(&action
.sa_mask
);
222 sigaddset(&action
.sa_mask
, SIGTERM
);
223 action
.sa_handler
= sigterm_handler
;
224 sigaction(SIGTERM
, &action
, NULL
);
226 signal(SIGPIPE
, SIG_IGN
);
227 signal(SIGTERM
, sigterm_handler
);
228 #endif /* HAVE_SIGSET */
231 * Check command-line...
238 if ((s
= strrchr(argv
[0], '/')) != NULL
)
243 printf("network %s \"Unknown\" \"%s (%s)\"\n",
244 s
, _cupsLangString(cupsLangDefault(),
245 _("Internet Printing Protocol")), s
);
246 return (CUPS_BACKEND_OK
);
250 _cupsLangPrintf(stderr
,
251 _("Usage: %s job-id user title copies options [file]"),
253 return (CUPS_BACKEND_STOP
);
257 * Get the (final) content type...
260 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
261 content_type
= "application/octet-stream";
263 if ((final_content_type
= getenv("FINAL_CONTENT_TYPE")) == NULL
)
265 final_content_type
= content_type
;
267 if (!strncmp(final_content_type
, "printer/", 8))
268 final_content_type
= "application/vnd.cups-raw";
272 * Extract the hostname and printer name from the URI...
275 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
277 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
280 if (getenv("CLASS") != NULL
)
281 return (CUPS_BACKEND_FAILED
);
284 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
285 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
286 resource
, sizeof(resource
));
289 port
= IPP_PORT
; /* Default to port 631 */
291 if (!strcmp(scheme
, "https"))
292 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
294 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
297 * See if there are any options...
304 contimeout
= 7 * 24 * 60 * 60;
306 if ((optptr
= strchr(resource
, '?')) != NULL
)
309 * Yup, terminate the device name string and move to the first
310 * character of the optptr...
316 * Then parse the optptr...
327 while (*optptr
&& *optptr
!= '=' && *optptr
!= '+' && *optptr
!= '&')
330 if ((sep
= *optptr
) != '\0')
341 while (*optptr
&& *optptr
!= '+' && *optptr
!= '&')
351 * Process the option...
354 if (!strcasecmp(name
, "waitjob"))
357 * Wait for job completion?
360 waitjob
= !strcasecmp(value
, "on") ||
361 !strcasecmp(value
, "yes") ||
362 !strcasecmp(value
, "true");
364 else if (!strcasecmp(name
, "waitprinter"))
367 * Wait for printer idle?
370 waitprinter
= !strcasecmp(value
, "on") ||
371 !strcasecmp(value
, "yes") ||
372 !strcasecmp(value
, "true");
374 else if (!strcasecmp(name
, "encryption"))
377 * Enable/disable encryption?
380 if (!strcasecmp(value
, "always"))
381 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
382 else if (!strcasecmp(value
, "required"))
383 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
384 else if (!strcasecmp(value
, "never"))
385 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
386 else if (!strcasecmp(value
, "ifrequested"))
387 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
390 _cupsLangPrintFilter(stderr
, "ERROR",
391 _("Unknown encryption option value: \"%s\"."),
395 else if (!strcasecmp(name
, "version"))
397 if (!strcmp(value
, "1.0"))
399 else if (!strcmp(value
, "1.1"))
401 else if (!strcmp(value
, "2.0"))
403 else if (!strcmp(value
, "2.1"))
405 else if (!strcmp(value
, "2.2"))
409 _cupsLangPrintFilter(stderr
, "ERROR",
410 _("Unknown version option value: \"%s\"."),
415 else if (!strcasecmp(name
, "compression"))
417 if (!strcasecmp(value
, "true") || !strcasecmp(value
, "yes") ||
418 !strcasecmp(value
, "on") || !strcasecmp(value
, "gzip"))
419 compression
= "gzip";
421 #endif /* HAVE_LIBZ */
422 else if (!strcasecmp(name
, "contimeout"))
425 * Set the connection timeout...
429 contimeout
= atoi(value
);
437 _cupsLangPrintFilter(stderr
, "ERROR",
438 _("Unknown option \"%s\" with value \"%s\"."),
445 * If we have 7 arguments, print the file named on the command-line.
446 * Otherwise, copy stdin to a temporary file and print the temporary
453 send_options
= !strcasecmp(final_content_type
, "application/pdf") ||
454 !strcasecmp(final_content_type
, "application/vnd.cups-pdf") ||
455 !strncasecmp(final_content_type
, "image/", 6);
457 fputs("DEBUG: Sending stdin for job...\n", stderr
);
462 * Point to the files on the command-line...
465 num_files
= argc
- 6;
471 compress_files(num_files
, files
);
472 #endif /* HAVE_LIBZ */
474 fprintf(stderr
, "DEBUG: %d files to send in job...\n", num_files
);
478 * Set the authentication info, if any...
481 cupsSetPasswordCB(password_cb
);
486 * Use authenticaion information in the device URI...
489 if ((password
= strchr(username
, ':')) != NULL
)
492 cupsSetUser(username
);
497 * Try loading authentication information from the environment.
500 const char *ptr
= getenv("AUTH_USERNAME");
505 password
= getenv("AUTH_PASSWORD");
509 * Try finding the remote server...
512 start_time
= time(NULL
);
514 sprintf(portname
, "%d", port
);
516 fputs("STATE: +connecting-to-device\n", stderr
);
517 fprintf(stderr
, "DEBUG: Looking up \"%s\"...\n", hostname
);
519 while ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
521 _cupsLangPrintFilter(stderr
, "INFO",
522 _("Unable to locate printer \"%s\"."), hostname
);
525 if (getenv("CLASS") != NULL
)
527 fputs("STATE: -connecting-to-device\n", stderr
);
528 return (CUPS_BACKEND_STOP
);
532 http
= _httpCreate(hostname
, port
, addrlist
, cupsEncryption(), AF_UNSPEC
);
535 * See if the printer supports SNMP...
538 if ((snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
)) >= 0)
540 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
),
544 have_supplies
= start_count
= 0;
547 * Wait for data from the filter...
551 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), backendNetworkSideCB
))
552 return (CUPS_BACKEND_OK
);
555 * Try connecting to the remote server...
562 fprintf(stderr
, "DEBUG: Connecting to %s:%d\n", hostname
, port
);
563 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
565 if (httpReconnect(http
))
567 int error
= errno
; /* Connection error */
569 if (http
->status
== HTTP_PKI_ERROR
)
570 fputs("STATE: +cups-certificate-error\n", stderr
);
575 if (getenv("CLASS") != NULL
)
578 * If the CLASS environment variable is set, the job was submitted
579 * to a class and not to a specific queue. In this case, we want
580 * to abort immediately so that the job can be requeued on the next
581 * available printer in the class.
584 _cupsLangPrintFilter(stderr
, "INFO",
585 _("Unable to contact printer, queuing on next "
586 "printer in class."));
589 * Sleep 5 seconds to keep the job from requeuing too rapidly...
594 fputs("STATE: -connecting-to-device\n", stderr
);
596 return (CUPS_BACKEND_FAILED
);
599 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
601 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
602 errno
== EHOSTUNREACH
)
604 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
606 _cupsLangPrintFilter(stderr
, "ERROR",
607 _("The printer is not responding."));
608 fputs("STATE: -connecting-to-device\n", stderr
);
609 return (CUPS_BACKEND_FAILED
);
615 _cupsLangPrintFilter(stderr
, "WARNING",
616 _("Network printer \"%s\" may not exist or "
617 "is unavailable at this time."),
622 _cupsLangPrintFilter(stderr
, "WARNING",
623 _("Network printer \"%s\" is unreachable at "
624 "this time."), hostname
);
629 _cupsLangPrintFilter(stderr
, "WARNING",
630 _("Network printer \"%s\" is busy."),
642 _cupsLangPrintFilter(stderr
, "ERROR",
643 _("Network printer \"%s\" is not responding."),
652 fputs("STATE: -cups-certificate-error\n", stderr
);
654 while (http
->fd
< 0);
656 if (job_canceled
|| !http
)
657 return (CUPS_BACKEND_FAILED
);
659 fputs("STATE: -connecting-to-device\n", stderr
);
660 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
663 if (http
->hostaddr
->addr
.sa_family
== AF_INET6
)
664 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
665 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
666 ntohs(http
->hostaddr
->ipv6
.sin6_port
));
668 #endif /* AF_INET6 */
669 if (http
->hostaddr
->addr
.sa_family
== AF_INET
)
670 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4)...\n",
671 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
672 ntohs(http
->hostaddr
->ipv4
.sin_port
));
675 * Build a URI for the printer and fill the standard IPP attributes for
676 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
677 * might contain username:password information...
680 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), scheme
, NULL
, hostname
,
684 * First validate the destination and see if the device supports multiple
691 media_col_sup
= NULL
;
693 operations_sup
= NULL
;
699 * Check for side-channel requests...
702 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
705 * Build the IPP request...
708 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
709 request
->request
.op
.version
[0] = version
/ 10;
710 request
->request
.op
.version
[1] = version
% 10;
712 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
715 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
716 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
723 fputs("DEBUG: Getting supported attributes...\n", stderr
);
725 if (http
->version
< HTTP_1_1
)
727 fprintf(stderr
, "DEBUG: Printer responded with HTTP version %d.%d.\n",
728 http
->version
/ 100, http
->version
% 100);
730 _cupsLangPrintFilter(stderr
, "ERROR",
731 _("Unable to print: the printer does not conform to "
732 "the IPP standard."));
733 exit(CUPS_BACKEND_STOP
);
736 supported
= cupsDoRequest(http
, request
, resource
);
737 ipp_status
= cupsLastError();
739 if (ipp_status
> IPP_OK_CONFLICT
)
741 fprintf(stderr
, "DEBUG: Get-Printer-Attributes returned %s.\n",
742 ippErrorString(ipp_status
));
744 if (ipp_status
== IPP_PRINTER_BUSY
||
745 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
747 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
749 _cupsLangPrintFilter(stderr
, "ERROR",
750 _("The printer is not responding."));
751 return (CUPS_BACKEND_FAILED
);
754 _cupsLangPrintFilter(stderr
, "WARNING",
755 _("Network host \"%s\" is busy; will retry in %d "
756 "seconds."), hostname
, delay
);
758 report_printer_state(supported
, 0);
765 else if ((ipp_status
== IPP_BAD_REQUEST
||
766 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
> 10)
769 * Switch to IPP/1.1 or IPP/1.0...
774 _cupsLangPrintFilter(stderr
, "INFO",
775 _("Printer does not support IPP/%d.%d, trying "
776 "IPP/%s."), version
/ 10, version
% 10, "1.1");
781 _cupsLangPrintFilter(stderr
, "INFO",
782 _("Printer does not support IPP/%d.%d, trying "
783 "IPP/%s."), version
/ 10, version
% 10, "1.0");
789 else if (ipp_status
== IPP_NOT_FOUND
)
791 _cupsLangPrintFilter(stderr
, "ERROR",
792 _("The printer URI is incorrect or no longer "
796 ippDelete(supported
);
798 return (CUPS_BACKEND_STOP
);
800 else if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
)
802 if (!strncmp(httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
),
804 auth_info_required
= "negotiate";
806 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
807 return (CUPS_BACKEND_AUTH_REQUIRED
);
811 _cupsLangPrintFilter(stderr
, "ERROR",
812 _("Unable to get printer status: %s"),
813 cupsLastErrorString());
818 ippDelete(supported
);
824 * Check for supported attributes...
827 if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
828 IPP_TAG_RANGE
)) != NULL
)
831 * Has the "copies-supported" attribute - does it have an upper
835 fprintf(stderr
, "DEBUG: copies-supported=%d-%d\n",
836 copies_sup
->values
[0].range
.lower
,
837 copies_sup
->values
[0].range
.upper
);
839 if (copies_sup
->values
[0].range
.upper
<= 1)
840 copies_sup
= NULL
; /* No */
843 cups_version
= ippFindAttribute(supported
, "cups-version", IPP_TAG_TEXT
);
845 if ((format_sup
= ippFindAttribute(supported
, "document-format-supported",
846 IPP_TAG_MIMETYPE
)) != NULL
)
848 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
849 format_sup
->num_values
);
850 for (i
= 0; i
< format_sup
->num_values
; i
++)
851 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
852 format_sup
->values
[i
].string
.text
);
855 if ((media_col_sup
= ippFindAttribute(supported
, "media-col-supported",
856 IPP_TAG_KEYWORD
)) != NULL
)
858 fprintf(stderr
, "DEBUG: media-col-supported (%d values)\n",
859 media_col_sup
->num_values
);
860 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
861 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
862 media_col_sup
->values
[i
].string
.text
);
865 if ((operations_sup
= ippFindAttribute(supported
, "operations-supported",
866 IPP_TAG_ENUM
)) != NULL
)
868 for (i
= 0; i
< operations_sup
->num_values
; i
++)
869 if (operations_sup
->values
[i
].integer
== IPP_VALIDATE_JOB
)
877 _cupsLangPrintFilter(stderr
, "WARNING",
878 _("This printer does not conform to the IPP "
879 "standard and may not work."));
880 fputs("DEBUG: operations-supported does not list Validate-Job.\n",
886 _cupsLangPrintFilter(stderr
, "WARNING",
887 _("This printer does not conform to the IPP "
888 "standard and may not work."));
889 fputs("DEBUG: operations-supported not returned in "
890 "Get-Printer-Attributes request.\n", stderr
);
893 report_printer_state(supported
, 0);
895 while (ipp_status
> IPP_OK_CONFLICT
);
898 * See if the printer is accepting jobs and is not stopped; if either
899 * condition is true and we are printing to a class, requeue the job...
902 if (getenv("CLASS") != NULL
)
904 printer_state
= ippFindAttribute(supported
, "printer-state",
906 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
909 if (printer_state
== NULL
||
910 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
912 printer_accepting
== NULL
||
913 !printer_accepting
->values
[0].boolean
)
916 * If the CLASS environment variable is set, the job was submitted
917 * to a class and not to a specific queue. In this case, we want
918 * to abort immediately so that the job can be requeued on the next
919 * available printer in the class.
922 _cupsLangPrintFilter(stderr
, "INFO",
923 _("Unable to contact printer, queuing on next "
924 "printer in class."));
926 ippDelete(supported
);
930 * Sleep 5 seconds to keep the job from requeuing too rapidly...
935 return (CUPS_BACKEND_FAILED
);
940 * See if the printer supports multiple copies...
943 copies
= atoi(argv
[4]);
945 if (copies_sup
|| argc
< 7)
947 copies_remaining
= 1;
953 copies_remaining
= copies
;
956 * Prepare remaining printing options...
964 num_options
= cupsParseOptions(argv
[5], 0, &options
);
966 if (!cups_version
&& media_col_sup
)
969 * Load the PPD file and generate PWG attribute mapping information...
972 ppd
= ppdOpenFile(getenv("PPD"));
973 pwg
= _pwgCreateWithPPD(ppd
);
981 document_format
= NULL
;
983 if (format_sup
!= NULL
)
985 for (i
= 0; i
< format_sup
->num_values
; i
++)
986 if (!strcasecmp(final_content_type
, format_sup
->values
[i
].string
.text
))
988 document_format
= final_content_type
;
992 if (!document_format
)
994 for (i
= 0; i
< format_sup
->num_values
; i
++)
995 if (!strcasecmp("application/octet-stream",
996 format_sup
->values
[i
].string
.text
))
998 document_format
= "application/octet-stream";
1005 * Start monitoring the printer in the background...
1009 monitor
.hostname
= hostname
;
1010 monitor
.user
= argv
[2];
1011 monitor
.resource
= resource
;
1012 monitor
.port
= port
;
1013 monitor
.version
= version
;
1015 monitor
.encryption
= cupsEncryption();
1016 monitor
.job_state
= IPP_JOB_PENDING
;
1017 monitor
.printer_state
= IPP_PRINTER_IDLE
;
1019 _cupsThreadCreate((_cups_thread_func_t
)monitor_printer
, &monitor
);
1022 * Validate access to the printer...
1025 while (!job_canceled
)
1027 request
= new_request(IPP_VALIDATE_JOB
, version
, uri
, argv
[2], argv
[3],
1028 num_options
, options
, compression
,
1029 copies_sup
? copies
: 1, document_format
, pwg
,
1032 ippDelete(cupsDoRequest(http
, request
, resource
));
1034 ipp_status
= cupsLastError();
1036 if (ipp_status
> IPP_OK_CONFLICT
&&
1037 ipp_status
!= IPP_OPERATION_NOT_SUPPORTED
)
1042 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1043 ipp_status
== IPP_PRINTER_BUSY
)
1045 _cupsLangPrintFilter(stderr
, "INFO",
1046 _("Printer busy; will retry in 10 seconds."));
1052 * Update auth-info-required as needed...
1055 _cupsLangPrintFilter(stderr
, "ERROR", "%s", cupsLastErrorString());
1057 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
)
1059 fprintf(stderr
, "DEBUG: WWW-Authenticate=\"%s\"\n",
1060 httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
));
1063 * Normal authentication goes through the password callback, which sets
1064 * auth_info_required to "username,password". Kerberos goes directly
1065 * through GSSAPI, so look for Negotiate in the WWW-Authenticate header
1066 * here and set auth_info_required as needed...
1069 if (!strncmp(httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
),
1071 auth_info_required
= "negotiate";
1082 * Then issue the print-job request...
1087 while (!job_canceled
&& copies_remaining
> 0)
1090 * Check for side-channel requests...
1093 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1096 * Build the IPP job creation request...
1102 request
= new_request(num_files
> 1 ? IPP_CREATE_JOB
: IPP_PRINT_JOB
,
1103 version
, uri
, argv
[2], argv
[3], num_options
, options
,
1104 compression
, copies_sup
? copies
: 1, document_format
,
1105 pwg
, media_col_sup
);
1112 response
= cupsDoRequest(http
, request
, resource
);
1115 fputs("DEBUG: Sending file using chunking...\n", stderr
);
1116 http_status
= cupsSendRequest(http
, request
, resource
, 0);
1117 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
)
1120 fd
= open(files
[0], O_RDONLY
);
1124 while ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1126 fprintf(stderr
, "DEBUG: Read %d bytes...\n", (int)bytes
);
1128 if (cupsWriteRequestData(http
, buffer
, bytes
) != HTTP_CONTINUE
)
1133 * Check for side-channel requests...
1136 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1144 response
= cupsGetResponse(http
, resource
);
1148 ipp_status
= cupsLastError();
1150 if (ipp_status
> IPP_OK_CONFLICT
)
1157 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1158 ipp_status
== IPP_PRINTER_BUSY
)
1160 _cupsLangPrintFilter(stderr
, "INFO",
1161 _("Printer busy; will retry in 10 seconds."));
1167 * Update auth-info-required as needed...
1170 _cupsLangPrintFilter(stderr
, "ERROR",
1171 _("Print file was not accepted: %s"),
1172 cupsLastErrorString());
1174 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
)
1176 fprintf(stderr
, "DEBUG: WWW-Authenticate=\"%s\"\n",
1177 httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
));
1180 * Normal authentication goes through the password callback, which sets
1181 * auth_info_required to "username,password". Kerberos goes directly
1182 * through GSSAPI, so look for Negotiate in the WWW-Authenticate header
1183 * here and set auth_info_required as needed...
1186 if (!strncmp(httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
),
1188 auth_info_required
= "negotiate";
1192 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
1193 IPP_TAG_INTEGER
)) == NULL
)
1195 _cupsLangPrintFilter(stderr
, "INFO",
1196 _("Print file accepted - job ID unknown."));
1201 monitor
.job_id
= job_id
= job_id_attr
->values
[0].integer
;
1202 _cupsLangPrintFilter(stderr
, "INFO",
1203 _("Print file accepted - job ID %d."), job_id
);
1206 ippDelete(response
);
1211 if (job_id
&& num_files
> 1)
1213 for (i
= 0; i
< num_files
; i
++)
1216 * Check for side-channel requests...
1219 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1222 * Send the next file in the job...
1225 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
1226 request
->request
.op
.version
[0] = version
/ 10;
1227 request
->request
.op
.version
[1] = version
% 10;
1229 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1232 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1236 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1237 "requesting-user-name", NULL
, argv
[2]);
1239 if ((i
+ 1) == num_files
)
1240 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
1242 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1243 "document-format", NULL
, content_type
);
1245 fprintf(stderr
, "DEBUG: Sending file %d using chunking...\n", i
+ 1);
1246 http_status
= cupsSendRequest(http
, request
, resource
, 0);
1247 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
&&
1248 (fd
= open(files
[i
], O_RDONLY
)) >= 0)
1250 while ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1252 if (cupsWriteRequestData(http
, buffer
, bytes
) != HTTP_CONTINUE
)
1257 * Check for side-channel requests...
1260 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1267 ippDelete(cupsGetResponse(http
, resource
));
1270 if (cupsLastError() > IPP_OK_CONFLICT
)
1272 ipp_status
= cupsLastError();
1274 _cupsLangPrintFilter(stderr
, "ERROR",
1275 _("Unable to add file to job: %s"),
1276 cupsLastErrorString());
1282 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
1284 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
1285 copies_remaining
--;
1287 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1288 ipp_status
== IPP_PRINTER_BUSY
)
1291 copies_remaining
--;
1294 * Wait for the job to complete...
1297 if (!job_id
|| !waitjob
)
1300 _cupsLangPrintFilter(stderr
, "INFO", _("Waiting for job to complete."));
1302 for (delay
= 1; !job_canceled
;)
1305 * Check for side-channel requests...
1308 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1311 * Build an IPP_GET_JOB_ATTRIBUTES request...
1314 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1315 request
->request
.op
.version
[0] = version
/ 10;
1316 request
->request
.op
.version
[1] = version
% 10;
1318 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1321 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1325 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1326 "requesting-user-name", NULL
, argv
[2]);
1328 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1329 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
1336 response
= cupsDoRequest(http
, request
, resource
);
1337 ipp_status
= cupsLastError();
1339 if (ipp_status
== IPP_NOT_FOUND
)
1342 * Job has gone away and/or the server has no job history...
1345 ippDelete(response
);
1347 ipp_status
= IPP_OK
;
1351 if (ipp_status
> IPP_OK_CONFLICT
)
1353 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1354 ipp_status
!= IPP_PRINTER_BUSY
)
1356 ippDelete(response
);
1358 _cupsLangPrintFilter(stderr
, "ERROR",
1359 _("Unable to get job attributes: %s"),
1360 cupsLastErrorString());
1367 if ((job_state
= ippFindAttribute(response
, "job-state",
1368 IPP_TAG_ENUM
)) != NULL
)
1371 * Stop polling if the job is finished or pending-held...
1374 if (job_state
->values
[0].integer
> IPP_JOB_STOPPED
)
1376 if ((job_sheets
= ippFindAttribute(response
,
1377 "job-media-sheets-completed",
1378 IPP_TAG_INTEGER
)) != NULL
)
1379 fprintf(stderr
, "PAGE: total %d\n",
1380 job_sheets
->values
[0].integer
);
1382 ippDelete(response
);
1389 * If the printer does not return a job-state attribute, it does not
1390 * conform to the IPP specification - break out immediately and fail
1394 fputs("DEBUG: No job-state available from printer - stopping queue.\n",
1396 ipp_status
= IPP_INTERNAL_ERROR
;
1401 ippDelete(response
);
1405 * Check the printer state and report it if necessary...
1408 check_printer_state(http
, uri
, resource
, argv
[2], version
, job_id
);
1412 * Wait 1-10 seconds before polling again...
1424 * Cancel the job as needed...
1427 if (job_canceled
&& job_id
)
1428 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1431 * Check the printer state and report it if necessary...
1434 check_printer_state(http
, uri
, resource
, argv
[2], version
, job_id
);
1437 * Collect the final page count as needed...
1440 if (have_supplies
&&
1441 !backendSNMPSupplies(snmp_fd
, http
->hostaddr
, &page_count
, NULL
) &&
1442 page_count
> start_count
)
1443 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
1447 * See if we used Kerberos at all...
1451 auth_info_required
= "negotiate";
1452 #endif /* HAVE_GSSAPI */
1460 cupsFreeOptions(num_options
, options
);
1465 ippDelete(supported
);
1468 * Remove the temporary file(s) if necessary...
1474 for (i
= 0; i
< num_files
; i
++)
1477 #endif /* HAVE_LIBZ */
1480 * Return the queue status...
1483 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
1485 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
)
1486 return (CUPS_BACKEND_AUTH_REQUIRED
);
1487 else if (ipp_status
== IPP_INTERNAL_ERROR
)
1488 return (CUPS_BACKEND_STOP
);
1489 else if (ipp_status
> IPP_OK_CONFLICT
)
1490 return (CUPS_BACKEND_FAILED
);
1493 _cupsLangPrintFilter(stderr
, "INFO", _("Ready to print."));
1494 return (CUPS_BACKEND_OK
);
1500 * 'cancel_job()' - Cancel a print job.
1504 cancel_job(http_t
*http
, /* I - HTTP connection */
1505 const char *uri
, /* I - printer-uri */
1506 int id
, /* I - job-id */
1507 const char *resource
, /* I - Resource path */
1508 const char *user
, /* I - requesting-user-name */
1509 int version
) /* I - IPP version */
1511 ipp_t
*request
; /* Cancel-Job request */
1514 _cupsLangPrintFilter(stderr
, "INFO", _("Canceling print job."));
1516 request
= ippNewRequest(IPP_CANCEL_JOB
);
1517 request
->request
.op
.version
[0] = version
/ 10;
1518 request
->request
.op
.version
[1] = version
% 10;
1520 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1522 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1524 if (user
&& user
[0])
1525 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1526 "requesting-user-name", NULL
, user
);
1532 ippDelete(cupsDoRequest(http
, request
, resource
));
1534 if (cupsLastError() > IPP_OK_CONFLICT
)
1535 _cupsLangPrintFilter(stderr
, "ERROR", _("Unable to cancel job: %s"),
1536 cupsLastErrorString());
1541 * 'check_printer_state()' - Check the printer state.
1544 static ipp_pstate_t
/* O - Current printer-state */
1545 check_printer_state(
1546 http_t
*http
, /* I - HTTP connection */
1547 const char *uri
, /* I - Printer URI */
1548 const char *resource
, /* I - Resource path */
1549 const char *user
, /* I - Username, if any */
1550 int version
, /* I - IPP version */
1553 ipp_t
*request
, /* IPP request */
1554 *response
; /* IPP response */
1555 ipp_attribute_t
*attr
; /* Attribute in response */
1556 ipp_pstate_t printer_state
= IPP_PRINTER_STOPPED
;
1557 /* Current printer-state */
1561 * Send a Get-Printer-Attributes request and log the results...
1564 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1565 request
->request
.op
.version
[0] = version
/ 10;
1566 request
->request
.op
.version
[1] = version
% 10;
1568 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1571 if (user
&& user
[0])
1572 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1573 "requesting-user-name", NULL
, user
);
1575 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1576 "requested-attributes",
1577 (int)(sizeof(pattrs
) / sizeof(pattrs
[0])), NULL
, pattrs
);
1579 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
1581 report_printer_state(response
, job_id
);
1583 if ((attr
= ippFindAttribute(response
, "printer-state",
1584 IPP_TAG_ENUM
)) != NULL
)
1585 printer_state
= (ipp_pstate_t
)attr
->values
[0].integer
;
1587 ippDelete(response
);
1591 * Return the printer-state value...
1594 return (printer_state
);
1600 * 'compress_files()' - Compress print files...
1604 compress_files(int num_files
, /* I - Number of files */
1605 char **files
) /* I - Files */
1607 int i
, /* Looping var */
1608 fd
; /* Temporary file descriptor */
1609 ssize_t bytes
; /* Bytes read/written */
1610 size_t total
; /* Total bytes read */
1611 cups_file_t
*in
, /* Input file */
1612 *out
; /* Output file */
1613 struct stat outinfo
; /* Output file information */
1614 char filename
[1024], /* Temporary filename */
1615 buffer
[32768]; /* Copy buffer */
1618 fprintf(stderr
, "DEBUG: Compressing %d job files...\n", num_files
);
1619 for (i
= 0; i
< num_files
; i
++)
1621 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
1623 _cupsLangPrintError("ERROR", _("Unable to create compressed print file"));
1624 exit(CUPS_BACKEND_FAILED
);
1627 if ((out
= cupsFileOpenFd(fd
, "w9")) == NULL
)
1629 _cupsLangPrintError("ERROR", _("Unable to open compressed print file"));
1630 exit(CUPS_BACKEND_FAILED
);
1633 if ((in
= cupsFileOpen(files
[i
], "r")) == NULL
)
1635 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1637 exit(CUPS_BACKEND_FAILED
);
1641 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
1642 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
1644 _cupsLangPrintError("ERROR",
1645 _("Unable to generate compressed print file"));
1648 exit(CUPS_BACKEND_FAILED
);
1656 files
[i
] = strdup(filename
);
1658 if (!stat(filename
, &outinfo
))
1660 "DEBUG: File %d compressed to %.1f%% of original size, "
1661 CUPS_LLFMT
" bytes...\n",
1662 i
+ 1, 100.0 * outinfo
.st_size
/ total
,
1663 CUPS_LLCAST outinfo
.st_size
);
1666 #endif /* HAVE_LIBZ */
1670 * 'monitor_printer()' - Monitor the printer state...
1673 static void * /* O - Thread exit code */
1675 _cups_monitor_t
*monitor
) /* I - Monitoring data */
1677 http_t
*http
; /* Connection to printer */
1678 ipp_t
*request
, /* IPP request */
1679 *response
; /* IPP response */
1680 ipp_attribute_t
*attr
; /* Attribute in response */
1681 int delay
, /* Current delay */
1682 prev_delay
, /* Previous delay */
1683 temp_delay
; /* Temporary delay value */
1687 * Make a copy of the printer connection...
1690 http
= _httpCreate(monitor
->hostname
, monitor
->port
, NULL
, monitor
->encryption
,
1692 cupsSetPasswordCB(password_cb
);
1695 * Loop until the job is canceled, aborted, or completed.
1701 while (monitor
->job_state
< IPP_JOB_CANCELED
&& !job_canceled
)
1704 * Reconnect to the printer...
1707 if (!httpReconnect(http
))
1710 * Connected, so check on the printer state...
1713 monitor
->printer_state
= check_printer_state(http
, monitor
->uri
,
1719 if (monitor
->job_id
> 0)
1722 * Check the status of the job itself...
1725 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1726 request
->request
.op
.version
[0] = monitor
->version
/ 10;
1727 request
->request
.op
.version
[1] = monitor
->version
% 10;
1729 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1730 NULL
, monitor
->uri
);
1731 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1734 if (monitor
->user
&& monitor
->user
[0])
1735 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1736 "requesting-user-name", NULL
, monitor
->user
);
1738 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1739 "requested-attributes",
1740 (int)(sizeof(jattrs
) / sizeof(jattrs
[0])), NULL
, jattrs
);
1746 response
= cupsDoRequest(http
, request
, monitor
->resource
);
1748 if ((attr
= ippFindAttribute(response
, "job-state",
1749 IPP_TAG_ENUM
)) != NULL
)
1750 monitor
->job_state
= (ipp_jstate_t
)attr
->values
[0].integer
;
1752 monitor
->job_state
= IPP_JOB_COMPLETED
;
1754 ippDelete(response
);
1758 * Disconnect from the printer - we'll reconnect on the next poll...
1761 _httpDisconnect(http
);
1765 * Sleep for N seconds, and then update the next sleep time using a
1766 * Fibonacci series (1 1 2 3 5 8)...
1772 delay
= (delay
+ prev_delay
) % 12;
1773 prev_delay
= delay
< temp_delay
? 0 : temp_delay
;
1777 * Cleanup and return...
1787 * 'new_request()' - Create a new print creation or validation request.
1790 static ipp_t
* /* O - Request data */
1792 ipp_op_t op
, /* I - IPP operation code */
1793 int version
, /* I - IPP version number */
1794 const char *uri
, /* I - printer-uri value */
1795 const char *user
, /* I - requesting-user-name value */
1796 const char *title
, /* I - job-name value */
1797 int num_options
, /* I - Number of options to send */
1798 cups_option_t
*options
, /* I - Options to send */
1799 const char *compression
, /* I - compression value or NULL */
1800 int copies
, /* I - copies value or 0 */
1801 const char *format
, /* I - documet-format value or NULL */
1802 _pwg_t
*pwg
, /* I - PWG<->PPD mapping data */
1803 ipp_attribute_t
*media_col_sup
) /* I - media-col-supported values */
1805 int i
; /* Looping var */
1806 ipp_t
*request
; /* Request data */
1807 const char *keyword
; /* PWG keyword */
1808 _pwg_size_t
*size
; /* PWG media size */
1809 ipp_t
*media_col
, /* media-col value */
1810 *media_size
; /* media-size value */
1811 const char *media_source
, /* media-source value */
1812 *media_type
; /* media-type value */
1816 * Create the IPP request...
1819 request
= ippNewRequest(op
);
1820 request
->request
.op
.version
[0] = version
/ 10;
1821 request
->request
.op
.version
[1] = version
% 10;
1823 fprintf(stderr
, "DEBUG: %s IPP/%d.%d\n",
1824 ippOpString(request
->request
.op
.operation_id
),
1825 request
->request
.op
.version
[0],
1826 request
->request
.op
.version
[1]);
1829 * Add standard attributes...
1832 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1834 fprintf(stderr
, "DEBUG: printer-uri=\"%s\"\n", uri
);
1838 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1839 "requesting-user-name", NULL
, user
);
1840 fprintf(stderr
, "DEBUG: requesting-user-name=\"%s\"\n", user
);
1843 if (title
&& *title
)
1845 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
1847 fprintf(stderr
, "DEBUG: job-name=\"%s\"\n", title
);
1852 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1853 "document-format", NULL
, format
);
1854 fprintf(stderr
, "DEBUG: document-format=\"%s\"\n", format
);
1860 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1861 "compression", NULL
, compression
);
1862 fprintf(stderr
, "DEBUG: compression=\"%s\"\n", compression
);
1864 #endif /* HAVE_LIBZ */
1867 * Handle options on the command-line...
1870 if (num_options
> 0)
1875 * Send standard IPP attributes...
1878 if ((keyword
= cupsGetOption("PageSize", num_options
, options
)) == NULL
)
1879 keyword
= cupsGetOption("media", num_options
, options
);
1881 if ((size
= _pwgGetSize(pwg
, keyword
)) != NULL
)
1884 * Add a media-col value...
1887 media_size
= ippNew();
1888 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1889 "x-dimension", size
->width
);
1890 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1891 "y-dimension", size
->length
);
1893 media_col
= ippNew();
1894 ippAddCollection(media_col
, IPP_TAG_ZERO
, "media-size", media_size
);
1896 media_source
= _pwgGetSource(pwg
, cupsGetOption("InputSlot",
1899 media_type
= _pwgGetType(pwg
, cupsGetOption("MediaType",
1900 num_options
, options
));
1902 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
1904 if (!strcmp(media_col_sup
->values
[i
].string
.text
,
1905 "media-left-margin"))
1906 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1907 "media-left-margin", size
->left
);
1908 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
1909 "media-bottom-margin"))
1910 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1911 "media-bottom-margin", size
->left
);
1912 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
1913 "media-right-margin"))
1914 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1915 "media-right-margin", size
->left
);
1916 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
1917 "media-top-margin"))
1918 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1919 "media-top-margin", size
->left
);
1920 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
1921 "media-source") && media_source
)
1922 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
1923 "media-source", NULL
, media_source
);
1924 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
1925 "media-type") && media_type
)
1926 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
1927 "media-type", NULL
, media_type
);
1930 ippAddCollection(request
, IPP_TAG_JOB
, "media-col", media_col
);
1933 if ((keyword
= cupsGetOption("output-bin", num_options
,
1935 keyword
= _pwgGetBin(pwg
, cupsGetOption("OutputBin", num_options
,
1939 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-bin",
1942 if ((keyword
= cupsGetOption("output-mode", num_options
,
1944 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
1946 else if ((keyword
= cupsGetOption("ColorModel", num_options
,
1949 if (!strcasecmp(keyword
, "Gray"))
1950 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
1951 NULL
, "monochrome");
1953 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
1957 if ((keyword
= cupsGetOption("print-quality", num_options
,
1959 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
1961 else if ((keyword
= cupsGetOption("cupsPrintQuality", num_options
,
1964 if (!strcasecmp(keyword
, "draft"))
1965 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
1967 else if (!strcasecmp(keyword
, "normal"))
1968 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
1969 IPP_QUALITY_NORMAL
);
1970 else if (!strcasecmp(keyword
, "high"))
1971 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
1975 if ((keyword
= cupsGetOption("sides", num_options
, options
)) != NULL
)
1976 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
1978 else if (pwg
->sides_option
&&
1979 (keyword
= cupsGetOption(pwg
->sides_option
, num_options
,
1982 if (!strcasecmp(keyword
, pwg
->sides_1sided
))
1983 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
1985 else if (!strcasecmp(keyword
, pwg
->sides_2sided_long
))
1986 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
1987 NULL
, "two-sided-long-edge");
1988 if (!strcasecmp(keyword
, pwg
->sides_2sided_short
))
1989 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
1990 NULL
, "two-sided-short-edge");
1996 * When talking to another CUPS server, send all options...
1999 cupsEncodeOptions(request
, num_options
, options
);
2003 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies", copies
);
2011 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
2014 static const char * /* O - Password */
2015 password_cb(const char *prompt
) /* I - Prompt (not used) */
2020 * Remember that we need to authenticate...
2023 auth_info_required
= "username,password";
2025 if (password
&& *password
&& password_tries
< 3)
2034 * Give up after 3 tries or if we don't have a password to begin with...
2043 * 'report_attr()' - Report an IPP attribute value.
2047 report_attr(ipp_attribute_t
*attr
) /* I - Attribute */
2049 int i
; /* Looping var */
2050 char value
[1024], /* Value string */
2051 *valptr
, /* Pointer into value string */
2052 *attrptr
; /* Pointer into attribute value */
2056 * Convert the attribute values into quoted strings...
2059 for (i
= 0, valptr
= value
;
2060 i
< attr
->num_values
&& valptr
< (value
+ sizeof(value
) - 10);
2066 switch (attr
->value_tag
)
2068 case IPP_TAG_INTEGER
:
2070 snprintf(valptr
, sizeof(value
) - (valptr
- value
), "%d",
2071 attr
->values
[i
].integer
);
2072 valptr
+= strlen(valptr
);
2077 case IPP_TAG_KEYWORD
:
2079 for (attrptr
= attr
->values
[i
].string
.text
;
2080 *attrptr
&& valptr
< (value
+ sizeof(value
) - 10);
2083 if (*attrptr
== '\\' || *attrptr
== '\"')
2086 *valptr
++ = *attrptr
;
2093 * Unsupported value type...
2103 * Tell the scheduler about the new values...
2106 fprintf(stderr
, "ATTR: %s=%s\n", attr
->name
, value
);
2111 * 'report_printer_state()' - Report the printer state.
2114 static int /* O - Number of reasons shown */
2115 report_printer_state(ipp_t
*ipp
, /* I - IPP response */
2116 int job_id
) /* I - Current job ID */
2118 int i
; /* Looping var */
2119 int count
; /* Count of reasons shown... */
2120 ipp_attribute_t
*pa
, /* printer-alert */
2121 *pam
, /* printer-alert-message */
2122 *psm
, /* printer-state-message */
2123 *reasons
, /* printer-state-reasons */
2124 *marker
; /* marker-* attributes */
2125 const char *reason
; /* Current reason */
2126 const char *prefix
; /* Prefix for STATE: line */
2127 char value
[1024], /* State/message string */
2128 *valptr
; /* Pointer into string */
2129 static int ipp_supplies
= -1;
2130 /* Report supply levels? */
2134 * Report alerts and messages...
2137 if ((pa
= ippFindAttribute(ipp
, "printer-alert", IPP_TAG_TEXT
)) != NULL
)
2140 if ((pam
= ippFindAttribute(ipp
, "printer-alert-message",
2141 IPP_TAG_TEXT
)) != NULL
)
2144 if ((psm
= ippFindAttribute(ipp
, "printer-state-message",
2145 IPP_TAG_TEXT
)) != NULL
)
2147 char *ptr
; /* Pointer into message */
2150 strlcpy(value
, "INFO: ", sizeof(value
));
2151 for (ptr
= psm
->values
[0].string
.text
, valptr
= value
+ 6;
2152 *ptr
&& valptr
< (value
+ sizeof(value
) - 6);
2155 if (*ptr
< ' ' && *ptr
> 0 && *ptr
!= '\t')
2158 * Substitute "<XX>" for the control character; sprintf is safe because
2159 * we always leave 6 chars free at the end...
2162 sprintf(valptr
, "<%02X>", *ptr
);
2172 fputs(value
, stderr
);
2176 * Now report printer-state-reasons, filtering out some of the reasons we never
2180 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
2181 IPP_TAG_KEYWORD
)) == NULL
)
2187 for (i
= 0, count
= 0, valptr
= value
; i
< reasons
->num_values
; i
++)
2189 reason
= reasons
->values
[i
].string
.text
;
2191 if (strcmp(reason
, "paused") &&
2192 strcmp(reason
, "com.apple.print.recoverable-warning"))
2194 strlcpy(valptr
, prefix
, sizeof(value
) - (valptr
- value
) - 1);
2195 valptr
+= strlen(valptr
);
2196 strlcpy(valptr
, reason
, sizeof(value
) - (valptr
- value
) - 1);
2197 valptr
+= strlen(valptr
);
2207 fputs(value
, stderr
);
2211 * Relay the current marker-* attribute values...
2214 if (ipp_supplies
< 0)
2216 ppd_file_t
*ppd
; /* PPD file */
2217 ppd_attr_t
*ppdattr
; /* Attribute in PPD file */
2219 if ((ppd
= ppdOpenFile(getenv("PPD"))) != NULL
&&
2220 (ppdattr
= ppdFindAttr(ppd
, "cupsIPPSupplies", NULL
)) != NULL
&&
2221 ppdattr
->value
&& strcasecmp(ppdattr
->value
, "true"))
2229 if (ipp_supplies
> 0)
2231 if ((marker
= ippFindAttribute(ipp
, "marker-colors", IPP_TAG_NAME
)) != NULL
)
2232 report_attr(marker
);
2233 if ((marker
= ippFindAttribute(ipp
, "marker-high-levels",
2234 IPP_TAG_INTEGER
)) != NULL
)
2235 report_attr(marker
);
2236 if ((marker
= ippFindAttribute(ipp
, "marker-levels",
2237 IPP_TAG_INTEGER
)) != NULL
)
2238 report_attr(marker
);
2239 if ((marker
= ippFindAttribute(ipp
, "marker-low-levels",
2240 IPP_TAG_INTEGER
)) != NULL
)
2241 report_attr(marker
);
2242 if ((marker
= ippFindAttribute(ipp
, "marker-message",
2243 IPP_TAG_TEXT
)) != NULL
)
2244 report_attr(marker
);
2245 if ((marker
= ippFindAttribute(ipp
, "marker-names", IPP_TAG_NAME
)) != NULL
)
2246 report_attr(marker
);
2247 if ((marker
= ippFindAttribute(ipp
, "marker-types",
2248 IPP_TAG_KEYWORD
)) != NULL
)
2249 report_attr(marker
);
2257 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
2261 sigterm_handler(int sig
) /* I - Signal */
2263 (void)sig
; /* remove compiler warnings... */
2268 * Flag that the job should be cancelled...
2280 * End of "$Id: ipp.c 7948 2008-09-17 00:04:12Z mike $".