2 * "$Id: ipp.c 7948 2008-09-17 00:04:12Z mike $"
4 * IPP backend for CUPS.
6 * Copyright 2007-2010 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 * password_cb() - Disable the password prompt for
24 * cupsDoFileRequest().
25 * report_attr() - Report an IPP attribute value.
26 * report_printer_state() - Report the printer state.
27 * sigterm_handler() - Handle 'terminate' signals that stop the backend.
31 * Include necessary headers.
34 #include "backend-private.h"
35 #include <sys/types.h>
43 static char *password
= NULL
; /* Password for device URI */
44 static int password_tries
= 0; /* Password tries */
45 static const char *auth_info_required
= "none";
46 /* New auth-info-required value */
47 static int job_canceled
= 0; /* Job cancelled? */
54 static void cancel_job(http_t
*http
, const char *uri
, int id
,
55 const char *resource
, const char *user
, int version
);
56 static void check_printer_state(http_t
*http
, const char *uri
,
57 const char *resource
, const char *user
,
58 int version
, int job_id
);
60 static void compress_files(int num_files
, char **files
);
61 #endif /* HAVE_LIBZ */
62 static const char *password_cb(const char *);
63 static void report_attr(ipp_attribute_t
*attr
);
64 static int report_printer_state(ipp_t
*ipp
, int job_id
);
65 static void sigterm_handler(int sig
);
69 * 'main()' - Send a file to the printer or server.
73 * printer-uri job-id user title copies options [file]
76 int /* O - Exit status */
77 main(int argc
, /* I - Number of command-line args */
78 char *argv
[]) /* I - Command-line arguments */
80 int i
; /* Looping var */
81 int send_options
; /* Send job options? */
82 int num_options
; /* Number of printer options */
83 cups_option_t
*options
; /* Printer options */
84 const char *device_uri
; /* Device URI */
85 char scheme
[255], /* Scheme in URI */
86 hostname
[1024], /* Hostname */
87 username
[255], /* Username info */
88 resource
[1024], /* Resource info (printer name) */
89 addrname
[256], /* Address name */
90 *optptr
, /* Pointer to URI options */
91 *name
, /* Name of option */
92 *value
, /* Value of option */
93 sep
; /* Separator character */
94 int snmp_fd
, /* SNMP socket */
95 start_count
, /* Page count via SNMP at start */
96 page_count
, /* Page count via SNMP */
97 have_supplies
; /* Printer supports supply levels? */
98 int num_files
; /* Number of files to print */
99 char **files
; /* Files to print */
100 int port
; /* Port number (not used) */
101 char uri
[HTTP_MAX_URI
]; /* Updated URI without user/pass */
102 http_status_t http_status
; /* Status of HTTP request */
103 ipp_status_t ipp_status
; /* Status of IPP request */
104 http_t
*http
; /* HTTP connection */
105 ipp_t
*request
, /* IPP request */
106 *response
, /* IPP response */
107 *supported
; /* get-printer-attributes response */
108 time_t start_time
; /* Time of first connect */
109 int contimeout
; /* Connection timeout */
110 int delay
; /* Delay for retries... */
111 int compression
, /* Do compression of the job data? */
112 waitjob
, /* Wait for job complete? */
113 waitprinter
; /* Wait for printer ready? */
114 ipp_attribute_t
*job_id_attr
; /* job-id attribute */
115 int job_id
; /* job-id value */
116 ipp_attribute_t
*job_sheets
; /* job-media-sheets-completed */
117 ipp_attribute_t
*job_state
; /* job-state */
118 ipp_attribute_t
*copies_sup
; /* copies-supported */
119 ipp_attribute_t
*cups_version
; /* cups-version */
120 ipp_attribute_t
*format_sup
; /* document-format-supported */
121 ipp_attribute_t
*media_col_sup
; /* media-col-supported */
122 ipp_attribute_t
*printer_state
; /* printer-state attribute */
123 ipp_attribute_t
*printer_accepting
; /* printer-is-accepting-jobs */
124 int copies
, /* Number of copies for job */
125 copies_remaining
; /* Number of copies remaining */
126 const char *content_type
, /* CONTENT_TYPE environment variable */
127 *final_content_type
; /* FINAL_CONTENT_TYPE environment var */
128 int fd
; /* File descriptor */
129 off_t bytes
; /* Bytes copied */
130 char buffer
[16384]; /* Copy buffer */
131 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
132 struct sigaction action
; /* Actions for POSIX signals */
133 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
134 int version
; /* IPP version */
135 static const char * const pattrs
[] =
136 { /* Printer attributes we want */
138 "document-format-supported",
140 "marker-high-levels",
146 "media-col-supported",
147 "printer-is-accepting-jobs",
149 "printer-state-message",
150 "printer-state-reasons",
152 static const char * const jattrs
[] =
153 { /* Job attributes we want */
154 "job-media-sheets-completed",
160 * Make sure status messages are not buffered...
163 setbuf(stderr
, NULL
);
166 * Ignore SIGPIPE and catch SIGTERM signals...
170 sigset(SIGPIPE
, SIG_IGN
);
171 sigset(SIGTERM
, sigterm_handler
);
172 #elif defined(HAVE_SIGACTION)
173 memset(&action
, 0, sizeof(action
));
174 action
.sa_handler
= SIG_IGN
;
175 sigaction(SIGPIPE
, &action
, NULL
);
177 sigemptyset(&action
.sa_mask
);
178 sigaddset(&action
.sa_mask
, SIGTERM
);
179 action
.sa_handler
= sigterm_handler
;
180 sigaction(SIGTERM
, &action
, NULL
);
182 signal(SIGPIPE
, SIG_IGN
);
183 signal(SIGTERM
, sigterm_handler
);
184 #endif /* HAVE_SIGSET */
187 * Check command-line...
194 if ((s
= strrchr(argv
[0], '/')) != NULL
)
199 printf("network %s \"Unknown\" \"%s (%s)\"\n",
200 s
, _cupsLangString(cupsLangDefault(),
201 _("Internet Printing Protocol")), s
);
202 return (CUPS_BACKEND_OK
);
206 _cupsLangPrintf(stderr
,
207 _("Usage: %s job-id user title copies options [file]\n"),
209 return (CUPS_BACKEND_STOP
);
213 * Get the (final) content type...
216 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
217 content_type
= "application/octet-stream";
219 if ((final_content_type
= getenv("FINAL_CONTENT_TYPE")) == NULL
)
221 final_content_type
= content_type
;
223 if (!strncmp(final_content_type
, "printer/", 8))
224 final_content_type
= "application/vnd.cups-raw";
228 * Extract the hostname and printer name from the URI...
231 if ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
232 return (CUPS_BACKEND_FAILED
);
234 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
235 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
236 resource
, sizeof(resource
));
239 port
= IPP_PORT
; /* Default to port 631 */
241 if (!strcmp(scheme
, "https"))
242 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
244 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
247 * See if there are any options...
254 contimeout
= 7 * 24 * 60 * 60;
256 if ((optptr
= strchr(resource
, '?')) != NULL
)
259 * Yup, terminate the device name string and move to the first
260 * character of the optptr...
266 * Then parse the optptr...
277 while (*optptr
&& *optptr
!= '=' && *optptr
!= '+' && *optptr
!= '&')
280 if ((sep
= *optptr
) != '\0')
291 while (*optptr
&& *optptr
!= '+' && *optptr
!= '&')
301 * Process the option...
304 if (!strcasecmp(name
, "waitjob"))
307 * Wait for job completion?
310 waitjob
= !strcasecmp(value
, "on") ||
311 !strcasecmp(value
, "yes") ||
312 !strcasecmp(value
, "true");
314 else if (!strcasecmp(name
, "waitprinter"))
317 * Wait for printer idle?
320 waitprinter
= !strcasecmp(value
, "on") ||
321 !strcasecmp(value
, "yes") ||
322 !strcasecmp(value
, "true");
324 else if (!strcasecmp(name
, "encryption"))
327 * Enable/disable encryption?
330 if (!strcasecmp(value
, "always"))
331 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
332 else if (!strcasecmp(value
, "required"))
333 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
334 else if (!strcasecmp(value
, "never"))
335 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
336 else if (!strcasecmp(value
, "ifrequested"))
337 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
340 _cupsLangPrintf(stderr
,
341 _("ERROR: Unknown encryption option value \"%s\"\n"),
345 else if (!strcasecmp(name
, "version"))
347 if (!strcmp(value
, "1.0"))
349 else if (!strcmp(value
, "1.1"))
351 else if (!strcmp(value
, "2.0"))
353 else if (!strcmp(value
, "2.1"))
357 _cupsLangPrintf(stderr
,
358 _("ERROR: Unknown version option value \"%s\"\n"),
363 else if (!strcasecmp(name
, "compression"))
365 compression
= !strcasecmp(value
, "true") ||
366 !strcasecmp(value
, "yes") ||
367 !strcasecmp(value
, "on") ||
368 !strcasecmp(value
, "gzip");
370 #endif /* HAVE_LIBZ */
371 else if (!strcasecmp(name
, "contimeout"))
374 * Set the connection timeout...
378 contimeout
= atoi(value
);
386 _cupsLangPrintf(stderr
,
387 _("ERROR: Unknown option \"%s\" with value \"%s\"\n"),
394 * If we have 7 arguments, print the file named on the command-line.
395 * Otherwise, copy stdin to a temporary file and print the temporary
402 send_options
= !strcasecmp(final_content_type
, "application/pdf") ||
403 !strcasecmp(final_content_type
, "application/vnd.cups-pdf") ||
404 !strcasecmp(final_content_type
, "image/jpeg") ||
405 !strcasecmp(final_content_type
, "image/png") ||
406 !strcasecmp(final_content_type
, "image/urf");
408 fputs("DEBUG: Sending stdin for job...\n", stderr
);
413 * Point to the files on the command-line...
416 num_files
= argc
- 6;
422 compress_files(num_files
, files
);
423 #endif /* HAVE_LIBZ */
425 fprintf(stderr
, "DEBUG: %d files to send in job...\n", num_files
);
429 * Set the authentication info, if any...
432 cupsSetPasswordCB(password_cb
);
437 * Use authenticaion information in the device URI...
440 if ((password
= strchr(username
, ':')) != NULL
)
443 cupsSetUser(username
);
448 * Try loading authentication information from the environment.
451 const char *ptr
= getenv("AUTH_USERNAME");
456 password
= getenv("AUTH_PASSWORD");
460 * Try connecting to the remote server...
464 start_time
= time(NULL
);
466 fputs("STATE: +connecting-to-device\n", stderr
);
470 fprintf(stderr
, "DEBUG: Connecting to %s:%d\n", hostname
, port
);
471 _cupsLangPuts(stderr
, _("INFO: Connecting to printer...\n"));
473 if ((http
= httpConnectEncrypt(hostname
, port
, cupsEncryption())) == NULL
)
478 if (getenv("CLASS") != NULL
)
481 * If the CLASS environment variable is set, the job was submitted
482 * to a class and not to a specific queue. In this case, we want
483 * to abort immediately so that the job can be requeued on the next
484 * available printer in the class.
487 _cupsLangPuts(stderr
,
488 _("INFO: Unable to contact printer, queuing on next "
489 "printer in class...\n"));
492 * Sleep 5 seconds to keep the job from requeuing too rapidly...
497 return (CUPS_BACKEND_FAILED
);
500 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
501 errno
== EHOSTUNREACH
)
503 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
505 _cupsLangPuts(stderr
, _("ERROR: Printer not responding\n"));
506 return (CUPS_BACKEND_FAILED
);
509 _cupsLangPrintf(stderr
,
510 _("WARNING: Network host \'%s\' is busy; "
511 "will retry in %d seconds...\n"),
521 _cupsLangPrintf(stderr
, _("ERROR: Unable to locate printer \'%s\'\n"),
523 return (CUPS_BACKEND_STOP
);
527 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
528 _cupsLangPuts(stderr
,
529 _("ERROR: Unable to connect to printer; will retry in 30 "
538 while (http
== NULL
);
540 if (job_canceled
|| !http
)
541 return (CUPS_BACKEND_FAILED
);
543 fputs("STATE: -connecting-to-device\n", stderr
);
544 _cupsLangPuts(stderr
, _("INFO: Connected to printer...\n"));
547 if (http
->hostaddr
->addr
.sa_family
== AF_INET6
)
548 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
549 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
550 ntohs(http
->hostaddr
->ipv6
.sin6_port
));
552 #endif /* AF_INET6 */
553 if (http
->hostaddr
->addr
.sa_family
== AF_INET
)
554 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4)...\n",
555 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
556 ntohs(http
->hostaddr
->ipv4
.sin_port
));
559 * See if the printer supports SNMP...
562 if ((snmp_fd
= _cupsSNMPOpen(http
->hostaddr
->addr
.sa_family
)) >= 0)
563 have_supplies
= !backendSNMPSupplies(snmp_fd
, http
->hostaddr
, &start_count
,
566 have_supplies
= start_count
= 0;
569 * Build a URI for the printer and fill the standard IPP attributes for
570 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
571 * might contain username:password information...
574 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), scheme
, NULL
, hostname
,
578 * First validate the destination and see if the device supports multiple
585 media_col_sup
= NULL
;
591 * Check for side-channel requests...
594 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
597 * Build the IPP request...
600 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
601 request
->request
.op
.version
[0] = version
/ 10;
602 request
->request
.op
.version
[1] = version
% 10;
604 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
607 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
608 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
615 fputs("DEBUG: Getting supported attributes...\n", stderr
);
617 if (http
->version
< HTTP_1_1
)
619 _cupsLangPuts(stderr
,
620 _("ERROR: Unable to print - printer does not conform to "
621 "the IPP standard.\n"));
622 exit(CUPS_BACKEND_STOP
);
625 if ((supported
= cupsDoRequest(http
, request
, resource
)) == NULL
)
626 ipp_status
= cupsLastError();
628 ipp_status
= supported
->request
.status
.status_code
;
630 if (ipp_status
> IPP_OK_CONFLICT
)
632 if (ipp_status
== IPP_PRINTER_BUSY
||
633 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
635 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
637 _cupsLangPuts(stderr
, _("ERROR: Printer not responding\n"));
638 return (CUPS_BACKEND_FAILED
);
641 _cupsLangPrintf(stderr
,
642 _("WARNING: Network host \'%s\' is busy; will retry in "
643 "%d seconds...\n"), hostname
, delay
);
645 report_printer_state(supported
, 0);
652 else if ((ipp_status
== IPP_BAD_REQUEST
||
653 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
> 10)
656 * Switch to IPP/1.1 or IPP/1.0...
661 _cupsLangPrintf(stderr
,
662 _("INFO: Printer does not support IPP/%d.%d, trying "
663 "IPP/%s...\n"), version
/ 10, version
% 10, "1.1");
668 _cupsLangPrintf(stderr
,
669 _("INFO: Printer does not support IPP/%d.%d, trying "
670 "IPP/%s...\n"), version
/ 10, version
% 10, "1.0");
676 else if (ipp_status
== IPP_NOT_FOUND
)
678 _cupsLangPuts(stderr
, _("ERROR: Destination printer does not exist\n"));
681 ippDelete(supported
);
683 return (CUPS_BACKEND_STOP
);
685 else if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
)
687 if (!strncmp(httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
),
689 auth_info_required
= "negotiate";
691 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
692 return (CUPS_BACKEND_AUTH_REQUIRED
);
696 _cupsLangPrintf(stderr
,
697 _("ERROR: Unable to get printer status (%s)\n"),
698 cupsLastErrorString());
703 ippDelete(supported
);
709 * Check for supported attributes...
712 if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
713 IPP_TAG_RANGE
)) != NULL
)
716 * Has the "copies-supported" attribute - does it have an upper
720 fprintf(stderr
, "DEBUG: copies-supported=%d-%d\n",
721 copies_sup
->values
[0].range
.lower
,
722 copies_sup
->values
[0].range
.upper
);
724 if (copies_sup
->values
[0].range
.upper
<= 1)
725 copies_sup
= NULL
; /* No */
728 cups_version
= ippFindAttribute(supported
, "cups-version", IPP_TAG_TEXT
);
730 if ((format_sup
= ippFindAttribute(supported
, "document-format-supported",
731 IPP_TAG_MIMETYPE
)) != NULL
)
733 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
734 format_sup
->num_values
);
735 for (i
= 0; i
< format_sup
->num_values
; i
++)
736 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
737 format_sup
->values
[i
].string
.text
);
740 if ((media_col_sup
= ippFindAttribute(supported
, "media-col-supported",
741 IPP_TAG_KEYWORD
)) != NULL
)
743 fprintf(stderr
, "DEBUG: media-col-supported (%d values)\n",
744 media_col_sup
->num_values
);
745 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
746 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
747 media_col_sup
->values
[i
].string
.text
);
750 report_printer_state(supported
, 0);
752 while (ipp_status
> IPP_OK_CONFLICT
);
755 * See if the printer is accepting jobs and is not stopped; if either
756 * condition is true and we are printing to a class, requeue the job...
759 if (getenv("CLASS") != NULL
)
761 printer_state
= ippFindAttribute(supported
, "printer-state",
763 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
766 if (printer_state
== NULL
||
767 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
769 printer_accepting
== NULL
||
770 !printer_accepting
->values
[0].boolean
)
773 * If the CLASS environment variable is set, the job was submitted
774 * to a class and not to a specific queue. In this case, we want
775 * to abort immediately so that the job can be requeued on the next
776 * available printer in the class.
779 _cupsLangPuts(stderr
,
780 _("INFO: Unable to contact printer, queuing on next "
781 "printer in class...\n"));
783 ippDelete(supported
);
787 * Sleep 5 seconds to keep the job from requeuing too rapidly...
792 return (CUPS_BACKEND_FAILED
);
797 * See if the printer supports multiple copies...
800 copies
= atoi(argv
[4]);
802 if (copies_sup
|| argc
< 7)
804 copies_remaining
= 1;
810 copies_remaining
= copies
;
813 * Then issue the print-job request...
818 while (copies_remaining
> 0)
821 * Check for side-channel requests...
824 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
827 * Build the IPP request...
834 request
= ippNewRequest(IPP_CREATE_JOB
);
836 request
= ippNewRequest(IPP_PRINT_JOB
);
838 request
->request
.op
.version
[0] = version
/ 10;
839 request
->request
.op
.version
[1] = version
% 10;
841 fprintf(stderr
, "DEBUG: %s IPP/%d.%d\n",
842 ippOpString(request
->request
.op
.operation_id
),
843 request
->request
.op
.version
[0],
844 request
->request
.op
.version
[1]);
846 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
848 fprintf(stderr
, "DEBUG: printer-uri=\"%s\"\n", uri
);
852 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
853 "requesting-user-name", NULL
, argv
[2]);
854 fprintf(stderr
, "DEBUG: requesting-user-name=\"%s\"\n", argv
[2]);
859 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
861 fprintf(stderr
, "DEBUG: job-name=\"%s\"\n", argv
[3]);
866 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
867 "compression", NULL
, "gzip");
868 #endif /* HAVE_LIBZ */
871 * Handle options on the command-line...
875 num_options
= cupsParseOptions(argv
[5], 0, &options
);
877 if (format_sup
!= NULL
)
879 for (i
= 0; i
< format_sup
->num_values
; i
++)
880 if (!strcasecmp(final_content_type
, format_sup
->values
[i
].string
.text
))
883 if (i
< format_sup
->num_values
)
884 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
885 "document-format", NULL
, final_content_type
);
890 if (cups_version
|| !media_col_sup
)
893 * When talking to another CUPS server, send all options...
896 cupsEncodeOptions(request
, num_options
, options
);
901 * Otherwise send standard IPP attributes...
904 char cachefile
[1024];/* Printer PWG cache file */
905 const char *cups_cachedir
; /* Location of cache file */
906 _pwg_t
*pwg
; /* PWG mapping data */
907 const char *keyword
; /* PWG keyword */
908 _pwg_size_t
*size
; /* PWG media size */
911 if ((cups_cachedir
= getenv("CUPS_CACHEDIR")) == NULL
)
912 cups_cachedir
= CUPS_CACHEDIR
;
914 snprintf(cachefile
, sizeof(cachefile
), "%s/%s.pwg", cups_cachedir
,
917 if ((keyword
= cupsGetOption("PageSize", num_options
, options
)) == NULL
)
918 keyword
= cupsGetOption("media", num_options
, options
);
920 pwg
= _pwgCreateWithFile(cachefile
);
921 size
= _pwgGetSize(pwg
, keyword
);
923 if (media_col_sup
&& size
)
925 ipp_t
*media_col
, /* media-col value */
926 *media_size
; /* media-size value */
927 const char *media_source
, /* media-source value */
928 *media_type
; /* media-type value */
930 media_size
= ippNew();
931 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
932 "x-dimension", size
->width
);
933 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
934 "y-dimension", size
->length
);
936 media_col
= ippNew();
937 ippAddCollection(media_col
, IPP_TAG_ZERO
, "media-size", media_size
);
939 media_source
= _pwgGetSource(pwg
, cupsGetOption("InputSlot",
942 media_type
= _pwgGetType(pwg
, cupsGetOption("MediaType",
943 num_options
, options
));
945 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
947 if (!strcmp(media_col_sup
->values
[i
].string
.text
,
948 "media-left-margin"))
949 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
950 "media-left-margin", size
->left
);
951 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
952 "media-bottom-margin"))
953 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
954 "media-bottom-margin", size
->left
);
955 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
956 "media-right-margin"))
957 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
958 "media-right-margin", size
->left
);
959 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
961 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
962 "media-top-margin", size
->left
);
963 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
964 "media-source") && media_source
)
965 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
966 "media-source", NULL
, media_source
);
967 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
968 "media-type") && media_type
)
969 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
970 "media-type", NULL
, media_type
);
973 ippAddCollection(request
, IPP_TAG_JOB
, "media-col", media_col
);
976 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "media",
977 NULL
, size
->map
.pwg
);
980 _pwg_media_t
*found
; /* PWG media */
983 if ((found
= _pwgMediaForPPD(keyword
)) == NULL
)
984 keyword
= found
->pwg
;
985 else if ((found
= _pwgMediaForLegacy(keyword
)) == NULL
)
986 keyword
= found
->pwg
;
988 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "media",
992 if ((keyword
= cupsGetOption("output-bin", num_options
,
994 keyword
= _pwgGetBin(pwg
, cupsGetOption("OutputBin", num_options
,
998 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-bin",
1001 if ((keyword
= cupsGetOption("output-mode", num_options
,
1003 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
1006 if ((keyword
= cupsGetOption("sides", num_options
, options
)) != NULL
)
1007 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
1013 if (copies_sup
&& copies
> 1)
1014 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies", copies
);
1017 cupsFreeOptions(num_options
, options
);
1024 response
= cupsDoRequest(http
, request
, resource
);
1027 fputs("DEBUG: Sending file using chunking...\n", stderr
);
1028 http_status
= cupsSendRequest(http
, request
, resource
, 0);
1029 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
)
1032 fd
= open(files
[0], O_RDONLY
);
1036 while ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1038 fprintf(stderr
, "DEBUG: Read %d bytes...\n", (int)bytes
);
1040 if ((http_status
= cupsWriteRequestData(http
, buffer
, bytes
))
1046 * Check for side-channel requests...
1049 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1057 response
= cupsGetResponse(http
, resource
);
1061 ipp_status
= cupsLastError();
1063 if (ipp_status
> IPP_OK_CONFLICT
)
1070 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1071 ipp_status
== IPP_PRINTER_BUSY
)
1073 _cupsLangPuts(stderr
,
1074 _("INFO: Printer busy; will retry in 10 seconds...\n"));
1080 * Update auth-info-required as needed...
1083 _cupsLangPrintf(stderr
, _("ERROR: Print file was not accepted (%s)\n"),
1084 cupsLastErrorString());
1086 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
)
1088 fprintf(stderr
, "DEBUG: WWW-Authenticate=\"%s\"\n",
1089 httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
));
1092 * Normal authentication goes through the password callback, which sets
1093 * auth_info_required to "username,password". Kerberos goes directly
1094 * through GSSAPI, so look for Negotiate in the WWW-Authenticate header
1095 * here and set auth_info_required as needed...
1098 if (!strncmp(httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
),
1100 auth_info_required
= "negotiate";
1104 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
1105 IPP_TAG_INTEGER
)) == NULL
)
1107 _cupsLangPuts(stderr
,
1108 _("NOTICE: Print file accepted - job ID unknown.\n"));
1113 job_id
= job_id_attr
->values
[0].integer
;
1114 _cupsLangPrintf(stderr
, _("NOTICE: Print file accepted - job ID %d.\n"),
1118 ippDelete(response
);
1123 if (job_id
&& num_files
> 1)
1125 for (i
= 0; i
< num_files
; i
++)
1128 * Check for side-channel requests...
1131 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1134 * Send the next file in the job...
1137 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
1138 request
->request
.op
.version
[0] = version
/ 10;
1139 request
->request
.op
.version
[1] = version
% 10;
1141 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1144 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1148 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1149 "requesting-user-name", NULL
, argv
[2]);
1151 if ((i
+ 1) == num_files
)
1152 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
1154 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1155 "document-format", NULL
, content_type
);
1157 fprintf(stderr
, "DEBUG: Sending file %d using chunking...\n", i
+ 1);
1158 http_status
= cupsSendRequest(http
, request
, resource
, 0);
1159 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
&&
1160 (fd
= open(files
[i
], O_RDONLY
)) >= 0)
1162 while ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1164 if ((http_status
= cupsWriteRequestData(http
, buffer
, bytes
))
1170 * Check for side-channel requests...
1173 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1180 ippDelete(cupsGetResponse(http
, resource
));
1183 if (cupsLastError() > IPP_OK_CONFLICT
)
1185 ipp_status
= cupsLastError();
1187 _cupsLangPrintf(stderr
,
1188 _("ERROR: Unable to add file %d to job: %s\n"),
1189 job_id
, cupsLastErrorString());
1195 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
1197 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
1198 copies_remaining
--;
1200 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1201 ipp_status
== IPP_PRINTER_BUSY
)
1204 copies_remaining
--;
1207 * Wait for the job to complete...
1210 if (!job_id
|| !waitjob
)
1213 _cupsLangPuts(stderr
, _("INFO: Waiting for job to complete...\n"));
1215 for (delay
= 1; !job_canceled
;)
1218 * Check for side-channel requests...
1221 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1224 * Build an IPP_GET_JOB_ATTRIBUTES request...
1227 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1228 request
->request
.op
.version
[0] = version
/ 10;
1229 request
->request
.op
.version
[1] = version
% 10;
1231 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1234 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1238 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1239 "requesting-user-name", NULL
, argv
[2]);
1241 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1242 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
1249 response
= cupsDoRequest(http
, request
, resource
);
1250 ipp_status
= cupsLastError();
1252 if (ipp_status
== IPP_NOT_FOUND
)
1255 * Job has gone away and/or the server has no job history...
1258 ippDelete(response
);
1260 ipp_status
= IPP_OK
;
1264 if (ipp_status
> IPP_OK_CONFLICT
)
1266 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1267 ipp_status
!= IPP_PRINTER_BUSY
)
1269 ippDelete(response
);
1271 _cupsLangPrintf(stderr
,
1272 _("ERROR: Unable to get job %d attributes (%s)\n"),
1273 job_id
, cupsLastErrorString());
1280 if ((job_state
= ippFindAttribute(response
, "job-state",
1281 IPP_TAG_ENUM
)) != NULL
)
1284 * Stop polling if the job is finished or pending-held...
1287 if (job_state
->values
[0].integer
> IPP_JOB_STOPPED
)
1289 if ((job_sheets
= ippFindAttribute(response
,
1290 "job-media-sheets-completed",
1291 IPP_TAG_INTEGER
)) != NULL
)
1292 fprintf(stderr
, "PAGE: total %d\n",
1293 job_sheets
->values
[0].integer
);
1295 ippDelete(response
);
1302 * If the printer does not return a job-state attribute, it does not
1303 * conform to the IPP specification - break out immediately and fail
1307 fputs("DEBUG: No job-state available from printer - stopping queue.\n",
1309 ipp_status
= IPP_INTERNAL_ERROR
;
1314 ippDelete(response
);
1317 * Check the printer state and report it if necessary...
1320 check_printer_state(http
, uri
, resource
, argv
[2], version
, job_id
);
1323 * Wait 1-10 seconds before polling again...
1335 * Cancel the job as needed...
1338 if (job_canceled
&& job_id
)
1339 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1342 * Check the printer state and report it if necessary...
1345 check_printer_state(http
, uri
, resource
, argv
[2], version
, job_id
);
1348 * Collect the final page count as needed...
1351 if (have_supplies
&&
1352 !backendSNMPSupplies(snmp_fd
, http
->hostaddr
, &page_count
, NULL
) &&
1353 page_count
> start_count
)
1354 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
1358 * See if we used Kerberos at all...
1362 auth_info_required
= "negotiate";
1363 #endif /* HAVE_GSSAPI */
1371 ippDelete(supported
);
1374 * Remove the temporary file(s) if necessary...
1380 for (i
= 0; i
< num_files
; i
++)
1383 #endif /* HAVE_LIBZ */
1386 * Return the queue status...
1389 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
1391 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
)
1392 return (CUPS_BACKEND_AUTH_REQUIRED
);
1393 else if (ipp_status
== IPP_INTERNAL_ERROR
)
1394 return (CUPS_BACKEND_STOP
);
1395 else if (ipp_status
> IPP_OK_CONFLICT
)
1396 return (CUPS_BACKEND_FAILED
);
1399 _cupsLangPuts(stderr
, _("INFO: Ready to print.\n"));
1400 return (CUPS_BACKEND_OK
);
1406 * 'cancel_job()' - Cancel a print job.
1410 cancel_job(http_t
*http
, /* I - HTTP connection */
1411 const char *uri
, /* I - printer-uri */
1412 int id
, /* I - job-id */
1413 const char *resource
, /* I - Resource path */
1414 const char *user
, /* I - requesting-user-name */
1415 int version
) /* I - IPP version */
1417 ipp_t
*request
; /* Cancel-Job request */
1420 _cupsLangPuts(stderr
, _("INFO: Canceling print job...\n"));
1422 request
= ippNewRequest(IPP_CANCEL_JOB
);
1423 request
->request
.op
.version
[0] = version
/ 10;
1424 request
->request
.op
.version
[1] = version
% 10;
1426 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1428 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1430 if (user
&& user
[0])
1431 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1432 "requesting-user-name", NULL
, user
);
1438 ippDelete(cupsDoRequest(http
, request
, resource
));
1440 if (cupsLastError() > IPP_OK_CONFLICT
)
1441 _cupsLangPrintf(stderr
, _("ERROR: Unable to cancel job %d: %s\n"), id
,
1442 cupsLastErrorString());
1447 * 'check_printer_state()' - Check the printer state...
1451 check_printer_state(
1452 http_t
*http
, /* I - HTTP connection */
1453 const char *uri
, /* I - Printer URI */
1454 const char *resource
, /* I - Resource path */
1455 const char *user
, /* I - Username, if any */
1456 int version
, /* I - IPP version */
1457 int job_id
) /* I - Current job ID */
1459 ipp_t
*request
, /* IPP request */
1460 *response
; /* IPP response */
1461 static const char * const attrs
[] = /* Attributes we want */
1468 "printer-state-message",
1469 "printer-state-reasons"
1474 * Check on the printer state...
1477 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1478 request
->request
.op
.version
[0] = version
/ 10;
1479 request
->request
.op
.version
[1] = version
% 10;
1481 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1484 if (user
&& user
[0])
1485 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1486 "requesting-user-name", NULL
, user
);
1488 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1489 "requested-attributes",
1490 (int)(sizeof(attrs
) / sizeof(attrs
[0])), NULL
, attrs
);
1496 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
1498 report_printer_state(response
, job_id
);
1499 ippDelete(response
);
1506 * 'compress_files()' - Compress print files...
1510 compress_files(int num_files
, /* I - Number of files */
1511 char **files
) /* I - Files */
1513 int i
, /* Looping var */
1514 fd
; /* Temporary file descriptor */
1515 ssize_t bytes
; /* Bytes read/written */
1516 size_t total
; /* Total bytes read */
1517 cups_file_t
*in
, /* Input file */
1518 *out
; /* Output file */
1519 struct stat outinfo
; /* Output file information */
1520 char filename
[1024], /* Temporary filename */
1521 buffer
[32768]; /* Copy buffer */
1524 fprintf(stderr
, "DEBUG: Compressing %d job files...\n", num_files
);
1525 for (i
= 0; i
< num_files
; i
++)
1527 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
1529 _cupsLangPrintf(stderr
,
1530 _("ERROR: Unable to create temporary compressed print "
1531 "file: %s\n"), strerror(errno
));
1532 exit(CUPS_BACKEND_FAILED
);
1535 if ((out
= cupsFileOpenFd(fd
, "w9")) == NULL
)
1537 _cupsLangPrintf(stderr
,
1538 _("ERROR: Unable to open temporary compressed print "
1539 "file: %s\n"), strerror(errno
));
1540 exit(CUPS_BACKEND_FAILED
);
1543 if ((in
= cupsFileOpen(files
[i
], "r")) == NULL
)
1545 _cupsLangPrintf(stderr
,
1546 _("ERROR: Unable to open print file \"%s\": %s\n"),
1547 files
[i
], strerror(errno
));
1549 exit(CUPS_BACKEND_FAILED
);
1553 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
1554 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
1556 _cupsLangPrintf(stderr
,
1557 _("ERROR: Unable to write %d bytes to \"%s\": %s\n"),
1558 (int)bytes
, filename
, strerror(errno
));
1561 exit(CUPS_BACKEND_FAILED
);
1569 files
[i
] = strdup(filename
);
1571 if (!stat(filename
, &outinfo
))
1573 "DEBUG: File %d compressed to %.1f%% of original size, "
1574 CUPS_LLFMT
" bytes...\n",
1575 i
+ 1, 100.0 * outinfo
.st_size
/ total
,
1576 CUPS_LLCAST outinfo
.st_size
);
1579 #endif /* HAVE_LIBZ */
1583 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
1586 static const char * /* O - Password */
1587 password_cb(const char *prompt
) /* I - Prompt (not used) */
1592 * Remember that we need to authenticate...
1595 auth_info_required
= "username,password";
1597 if (password
&& *password
&& password_tries
< 3)
1606 * Give up after 3 tries or if we don't have a password to begin with...
1615 * 'report_attr()' - Report an IPP attribute value.
1619 report_attr(ipp_attribute_t
*attr
) /* I - Attribute */
1621 int i
; /* Looping var */
1622 char value
[1024], /* Value string */
1623 *valptr
, /* Pointer into value string */
1624 *attrptr
; /* Pointer into attribute value */
1628 * Convert the attribute values into quoted strings...
1631 for (i
= 0, valptr
= value
;
1632 i
< attr
->num_values
&& valptr
< (value
+ sizeof(value
) - 10);
1638 switch (attr
->value_tag
)
1640 case IPP_TAG_INTEGER
:
1642 snprintf(valptr
, sizeof(value
) - (valptr
- value
), "%d",
1643 attr
->values
[i
].integer
);
1644 valptr
+= strlen(valptr
);
1649 case IPP_TAG_KEYWORD
:
1651 for (attrptr
= attr
->values
[i
].string
.text
;
1652 *attrptr
&& valptr
< (value
+ sizeof(value
) - 10);
1655 if (*attrptr
== '\\' || *attrptr
== '\"')
1658 *valptr
++ = *attrptr
;
1665 * Unsupported value type...
1675 * Tell the scheduler about the new values...
1678 fprintf(stderr
, "ATTR: %s=%s\n", attr
->name
, value
);
1683 * 'report_printer_state()' - Report the printer state.
1686 static int /* O - Number of reasons shown */
1687 report_printer_state(ipp_t
*ipp
, /* I - IPP response */
1688 int job_id
) /* I - Current job ID */
1690 int i
; /* Looping var */
1691 int count
; /* Count of reasons shown... */
1692 ipp_attribute_t
*psm
, /* printer-state-message */
1693 *reasons
, /* printer-state-reasons */
1694 *marker
; /* marker-* attributes */
1695 const char *reason
; /* Current reason */
1696 const char *prefix
; /* Prefix for STATE: line */
1697 char state
[1024]; /* State string */
1700 if ((psm
= ippFindAttribute(ipp
, "printer-state-message",
1701 IPP_TAG_TEXT
)) != NULL
)
1702 fprintf(stderr
, "INFO: %s\n", psm
->values
[0].string
.text
);
1704 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
1705 IPP_TAG_KEYWORD
)) == NULL
)
1711 for (i
= 0, count
= 0; i
< reasons
->num_values
; i
++)
1713 reason
= reasons
->values
[i
].string
.text
;
1715 if (strcmp(reason
, "paused") &&
1716 strcmp(reason
, "com.apple.print.recoverable-warning"))
1718 strlcat(state
, prefix
, sizeof(state
));
1719 strlcat(state
, reason
, sizeof(state
));
1726 fprintf(stderr
, "%s\n", state
);
1729 * Relay the current marker-* attribute values...
1732 if ((marker
= ippFindAttribute(ipp
, "marker-colors", IPP_TAG_NAME
)) != NULL
)
1733 report_attr(marker
);
1734 if ((marker
= ippFindAttribute(ipp
, "marker-high-levels",
1735 IPP_TAG_INTEGER
)) != NULL
)
1736 report_attr(marker
);
1737 if ((marker
= ippFindAttribute(ipp
, "marker-levels",
1738 IPP_TAG_INTEGER
)) != NULL
)
1739 report_attr(marker
);
1740 if ((marker
= ippFindAttribute(ipp
, "marker-low-levels",
1741 IPP_TAG_INTEGER
)) != NULL
)
1742 report_attr(marker
);
1743 if ((marker
= ippFindAttribute(ipp
, "marker-message", IPP_TAG_TEXT
)) != NULL
)
1744 report_attr(marker
);
1745 if ((marker
= ippFindAttribute(ipp
, "marker-names", IPP_TAG_NAME
)) != NULL
)
1746 report_attr(marker
);
1747 if ((marker
= ippFindAttribute(ipp
, "marker-types", IPP_TAG_KEYWORD
)) != NULL
)
1748 report_attr(marker
);
1755 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1759 sigterm_handler(int sig
) /* I - Signal */
1761 (void)sig
; /* remove compiler warnings... */
1766 * Flag that the job should be cancelled...
1778 * End of "$Id: ipp.c 7948 2008-09-17 00:04:12Z mike $".