2 * "$Id: ipp.c 7948 2008-09-17 00:04:12Z mike $"
4 * IPP backend for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2009 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 * run_pictwps_filter() - Convert PICT files to PostScript when printing
29 * sigterm_handler() - Handle 'terminate' signals that stop the backend.
33 * Include necessary headers.
36 #include <cups/http-private.h>
37 #include "backend-private.h"
38 #include <sys/types.h>
46 static char *password
= NULL
; /* Password for device URI */
47 static int password_tries
= 0; /* Password tries */
49 static char pstmpname
[1024] = ""; /* Temporary PostScript file name */
50 #endif /* __APPLE__ */
51 static char tmpfilename
[1024] = ""; /* Temporary spool file name */
52 static int job_cancelled
= 0; /* Job cancelled? */
59 static void cancel_job(http_t
*http
, const char *uri
, int id
,
60 const char *resource
, const char *user
, int version
);
61 static void check_printer_state(http_t
*http
, const char *uri
,
62 const char *resource
, const char *user
,
63 int version
, int job_id
);
65 static void compress_files(int num_files
, char **files
);
66 #endif /* HAVE_LIBZ */
67 static const char *password_cb(const char *);
68 static void report_attr(ipp_attribute_t
*attr
);
69 static int report_printer_state(ipp_t
*ipp
, int job_id
);
72 static int run_pictwps_filter(char **argv
, const char *filename
);
73 #endif /* __APPLE__ */
74 static void sigterm_handler(int sig
);
78 * 'main()' - Send a file to the printer or server.
82 * printer-uri job-id user title copies options [file]
85 int /* O - Exit status */
86 main(int argc
, /* I - Number of command-line args */
87 char *argv
[]) /* I - Command-line arguments */
89 int i
; /* Looping var */
90 int send_options
; /* Send job options? */
91 int num_options
; /* Number of printer options */
92 cups_option_t
*options
; /* Printer options */
93 const char *device_uri
; /* Device URI */
94 char method
[255], /* Method in URI */
95 hostname
[1024], /* Hostname */
96 username
[255], /* Username info */
97 resource
[1024], /* Resource info (printer name) */
98 addrname
[256], /* Address name */
99 *optptr
, /* Pointer to URI options */
100 *name
, /* Name of option */
101 *value
, /* Value of option */
102 sep
; /* Separator character */
103 int snmp_fd
, /* SNMP socket */
104 start_count
, /* Page count via SNMP at start */
105 page_count
, /* Page count via SNMP */
106 have_supplies
; /* Printer supports supply levels? */
107 int num_files
; /* Number of files to print */
108 char **files
, /* Files to print */
109 *filename
; /* Pointer to single filename */
110 int port
; /* Port number (not used) */
111 char uri
[HTTP_MAX_URI
]; /* Updated URI without user/pass */
112 ipp_status_t ipp_status
; /* Status of IPP request */
113 http_t
*http
; /* HTTP connection */
114 ipp_t
*request
, /* IPP request */
115 *response
, /* IPP response */
116 *supported
; /* get-printer-attributes response */
117 time_t start_time
; /* Time of first connect */
118 int recoverable
; /* Recoverable error shown? */
119 int contimeout
; /* Connection timeout */
120 int delay
; /* Delay for retries... */
121 int compression
, /* Do compression of the job data? */
122 waitjob
, /* Wait for job complete? */
123 waitprinter
; /* Wait for printer ready? */
124 ipp_attribute_t
*job_id_attr
; /* job-id attribute */
125 int job_id
; /* job-id value */
126 ipp_attribute_t
*job_sheets
; /* job-media-sheets-completed */
127 ipp_attribute_t
*job_state
; /* job-state */
128 ipp_attribute_t
*copies_sup
; /* copies-supported */
129 ipp_attribute_t
*format_sup
; /* document-format-supported */
130 ipp_attribute_t
*printer_state
; /* printer-state attribute */
131 ipp_attribute_t
*printer_accepting
; /* printer-is-accepting-jobs */
132 int copies
, /* Number of copies for job */
133 copies_remaining
; /* Number of copies remaining */
134 const char *content_type
, /* CONTENT_TYPE environment variable */
135 *final_content_type
; /* FINAL_CONTENT_TYPE environment var */
136 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
137 struct sigaction action
; /* Actions for POSIX signals */
138 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
139 int version
; /* IPP version */
140 static const char * const pattrs
[] =
141 { /* Printer attributes we want */
142 "com.apple.print.recoverable-message",
144 "document-format-supported",
146 "marker-high-levels",
152 "printer-is-accepting-jobs",
154 "printer-state-message",
155 "printer-state-reasons",
157 static const char * const jattrs
[] =
158 { /* Job attributes we want */
159 "job-media-sheets-completed",
165 * Make sure status messages are not buffered...
168 setbuf(stderr
, NULL
);
171 * Ignore SIGPIPE and catch SIGTERM signals...
175 sigset(SIGPIPE
, SIG_IGN
);
176 sigset(SIGTERM
, sigterm_handler
);
177 #elif defined(HAVE_SIGACTION)
178 memset(&action
, 0, sizeof(action
));
179 action
.sa_handler
= SIG_IGN
;
180 sigaction(SIGPIPE
, &action
, NULL
);
182 sigemptyset(&action
.sa_mask
);
183 sigaddset(&action
.sa_mask
, SIGTERM
);
184 action
.sa_handler
= sigterm_handler
;
185 sigaction(SIGTERM
, &action
, NULL
);
187 signal(SIGPIPE
, SIG_IGN
);
188 signal(SIGTERM
, sigterm_handler
);
189 #endif /* HAVE_SIGSET */
192 * Check command-line...
199 if ((s
= strrchr(argv
[0], '/')) != NULL
)
204 printf("network %s \"Unknown\" \"%s (%s)\"\n",
205 s
, _cupsLangString(cupsLangDefault(),
206 _("Internet Printing Protocol")), s
);
207 return (CUPS_BACKEND_OK
);
211 _cupsLangPrintf(stderr
,
212 _("Usage: %s job-id user title copies options [file]\n"),
214 return (CUPS_BACKEND_STOP
);
218 * Get the (final) content type...
221 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
222 content_type
= "application/octet-stream";
224 if ((final_content_type
= getenv("FINAL_CONTENT_TYPE")) == NULL
)
226 final_content_type
= content_type
;
228 if (!strncmp(final_content_type
, "printer/", 8))
229 final_content_type
= "application/vnd.cups-raw";
233 * Extract the hostname and printer name from the URI...
236 if ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
237 return (CUPS_BACKEND_FAILED
);
239 if (httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
,
240 method
, sizeof(method
), username
, sizeof(username
),
241 hostname
, sizeof(hostname
), &port
,
242 resource
, sizeof(resource
)) < HTTP_URI_OK
)
244 _cupsLangPuts(stderr
,
245 _("ERROR: Missing device URI on command-line and no "
246 "DEVICE_URI environment variable!\n"));
247 return (CUPS_BACKEND_STOP
);
251 port
= IPP_PORT
; /* Default to port 631 */
253 if (!strcmp(method
, "https"))
254 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
256 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
259 * See if there are any options...
266 contimeout
= 7 * 24 * 60 * 60;
268 if ((optptr
= strchr(resource
, '?')) != NULL
)
271 * Yup, terminate the device name string and move to the first
272 * character of the optptr...
278 * Then parse the optptr...
289 while (*optptr
&& *optptr
!= '=' && *optptr
!= '+' && *optptr
!= '&')
292 if ((sep
= *optptr
) != '\0')
303 while (*optptr
&& *optptr
!= '+' && *optptr
!= '&')
313 * Process the option...
316 if (!strcasecmp(name
, "waitjob"))
319 * Wait for job completion?
322 waitjob
= !strcasecmp(value
, "on") ||
323 !strcasecmp(value
, "yes") ||
324 !strcasecmp(value
, "true");
326 else if (!strcasecmp(name
, "waitprinter"))
329 * Wait for printer idle?
332 waitprinter
= !strcasecmp(value
, "on") ||
333 !strcasecmp(value
, "yes") ||
334 !strcasecmp(value
, "true");
336 else if (!strcasecmp(name
, "encryption"))
339 * Enable/disable encryption?
342 if (!strcasecmp(value
, "always"))
343 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
344 else if (!strcasecmp(value
, "required"))
345 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
346 else if (!strcasecmp(value
, "never"))
347 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
348 else if (!strcasecmp(value
, "ifrequested"))
349 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
352 _cupsLangPrintf(stderr
,
353 _("ERROR: Unknown encryption option value \"%s\"!\n"),
357 else if (!strcasecmp(name
, "version"))
359 if (!strcmp(value
, "1.0"))
361 else if (!strcmp(value
, "1.1"))
363 else if (!strcmp(value
, "2.0"))
365 else if (!strcmp(value
, "2.1"))
369 _cupsLangPrintf(stderr
,
370 _("ERROR: Unknown version option value \"%s\"!\n"),
375 else if (!strcasecmp(name
, "compression"))
377 compression
= !strcasecmp(value
, "true") ||
378 !strcasecmp(value
, "yes") ||
379 !strcasecmp(value
, "on") ||
380 !strcasecmp(value
, "gzip");
382 #endif /* HAVE_LIBZ */
383 else if (!strcasecmp(name
, "contimeout"))
386 * Set the connection timeout...
390 contimeout
= atoi(value
);
398 _cupsLangPrintf(stderr
,
399 _("ERROR: Unknown option \"%s\" with value \"%s\"!\n"),
406 * If we have 7 arguments, print the file named on the command-line.
407 * Otherwise, copy stdin to a temporary file and print the temporary
414 * Copy stdin to a temporary file...
417 int fd
; /* File descriptor */
418 http_addrlist_t
*addrlist
; /* Address list */
419 char buffer
[8192]; /* Buffer for copying */
420 int bytes
; /* Number of bytes read */
421 off_t tbytes
; /* Total bytes copied */
424 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, "1")) == NULL
)
426 _cupsLangPrintf(stderr
, _("ERROR: Unable to locate printer \'%s\'!\n"),
428 return (CUPS_BACKEND_STOP
);
431 snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
);
433 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
435 _cupsLangPrintError(_("ERROR: Unable to create temporary file"));
436 return (CUPS_BACKEND_FAILED
);
439 _cupsLangPuts(stderr
, _("INFO: Copying print data...\n"));
443 while ((bytes
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0)
445 if (write(fd
, buffer
, bytes
) < bytes
)
447 _cupsLangPrintError(_("ERROR: Unable to write to temporary file"));
450 return (CUPS_BACKEND_FAILED
);
456 backendCheckSideChannel(snmp_fd
, &(addrlist
->addr
));
460 _cupsSNMPClose(snmp_fd
);
462 httpAddrFreeList(addrlist
);
467 * Don't try printing files less than 2 bytes...
472 _cupsLangPuts(stderr
, _("ERROR: Empty print file!\n"));
474 return (CUPS_BACKEND_FAILED
);
478 * Point to the single file from stdin...
481 filename
= tmpfilename
;
489 * Point to the files on the command-line...
492 num_files
= argc
- 6;
498 compress_files(num_files
, files
);
499 #endif /* HAVE_LIBZ */
502 fprintf(stderr
, "DEBUG: %d files to send in job...\n", num_files
);
505 * Set the authentication info, if any...
508 cupsSetPasswordCB(password_cb
);
513 * Use authenticaion information in the device URI...
516 if ((password
= strchr(username
, ':')) != NULL
)
519 cupsSetUser(username
);
524 * Try loading authentication information from the environment.
527 const char *ptr
= getenv("AUTH_USERNAME");
532 password
= getenv("AUTH_PASSWORD");
536 * Try connecting to the remote server...
541 start_time
= time(NULL
);
543 fputs("STATE: +connecting-to-device\n", stderr
);
547 fprintf(stderr
, "DEBUG: Connecting to %s:%d\n",
549 _cupsLangPuts(stderr
, _("INFO: Connecting to printer...\n"));
551 if ((http
= httpConnectEncrypt(hostname
, port
, cupsEncryption())) == NULL
)
556 if (getenv("CLASS") != NULL
)
559 * If the CLASS environment variable is set, the job was submitted
560 * to a class and not to a specific queue. In this case, we want
561 * to abort immediately so that the job can be requeued on the next
562 * available printer in the class.
565 _cupsLangPuts(stderr
,
566 _("INFO: Unable to contact printer, queuing on next "
567 "printer in class...\n"));
573 * Sleep 5 seconds to keep the job from requeuing too rapidly...
578 return (CUPS_BACKEND_FAILED
);
581 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
582 errno
== EHOSTUNREACH
)
584 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
586 _cupsLangPuts(stderr
, _("ERROR: Printer not responding!\n"));
587 return (CUPS_BACKEND_FAILED
);
592 _cupsLangPrintf(stderr
,
593 _("WARNING: recoverable: Network host \'%s\' is busy; "
594 "will retry in %d seconds...\n"),
604 _cupsLangPrintf(stderr
, _("ERROR: Unable to locate printer \'%s\'!\n"),
606 return (CUPS_BACKEND_STOP
);
612 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
613 _cupsLangPuts(stderr
,
614 _("ERROR: recoverable: Unable to connect to printer; will "
615 "retry in 30 seconds...\n"));
623 while (http
== NULL
);
630 return (CUPS_BACKEND_FAILED
);
633 fputs("STATE: -connecting-to-device\n", stderr
);
634 _cupsLangPuts(stderr
, _("INFO: Connected to printer...\n"));
637 if (http
->hostaddr
->addr
.sa_family
== AF_INET6
)
638 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
639 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
640 ntohs(http
->hostaddr
->ipv6
.sin6_port
));
642 #endif /* AF_INET6 */
643 if (http
->hostaddr
->addr
.sa_family
== AF_INET
)
644 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4)...\n",
645 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
646 ntohs(http
->hostaddr
->ipv4
.sin_port
));
649 * See if the printer supports SNMP...
652 if ((snmp_fd
= _cupsSNMPOpen(http
->hostaddr
->addr
.sa_family
)) >= 0)
653 have_supplies
= !backendSNMPSupplies(snmp_fd
, http
->hostaddr
, &start_count
,
656 have_supplies
= start_count
= 0;
659 * Build a URI for the printer and fill the standard IPP attributes for
660 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
661 * might contain username:password information...
664 snprintf(uri
, sizeof(uri
), "%s://%s:%d%s", method
, hostname
, port
, resource
);
667 * First validate the destination and see if the device supports multiple
668 * copies. We have to do this because some IPP servers (e.g. HP JetDirect)
669 * don't support the copies attribute...
679 * Check for side-channel requests...
682 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
685 * Build the IPP request...
688 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
689 request
->request
.op
.version
[0] = version
/ 10;
690 request
->request
.op
.version
[1] = version
% 10;
692 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
695 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
696 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
703 fputs("DEBUG: Getting supported attributes...\n", stderr
);
705 if ((supported
= cupsDoRequest(http
, request
, resource
)) == NULL
)
706 ipp_status
= cupsLastError();
708 ipp_status
= supported
->request
.status
.status_code
;
710 if (ipp_status
> IPP_OK_CONFLICT
)
712 if (ipp_status
== IPP_PRINTER_BUSY
||
713 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
715 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
717 _cupsLangPuts(stderr
, _("ERROR: Printer not responding!\n"));
718 return (CUPS_BACKEND_FAILED
);
723 _cupsLangPrintf(stderr
,
724 _("WARNING: recoverable: Network host \'%s\' is busy; "
725 "will retry in %d seconds...\n"),
728 report_printer_state(supported
, 0);
735 else if ((ipp_status
== IPP_BAD_REQUEST
||
736 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
> 10)
739 * Switch to IPP/1.0...
742 _cupsLangPrintf(stderr
,
743 _("INFO: Printer does not support IPP/%d.%d, trying "
744 "IPP/1.0...\n"), version
/ 10, version
% 10);
748 else if (ipp_status
== IPP_NOT_FOUND
)
750 _cupsLangPuts(stderr
, _("ERROR: Destination printer does not exist!\n"));
753 ippDelete(supported
);
755 return (CUPS_BACKEND_STOP
);
759 _cupsLangPrintf(stderr
,
760 _("ERROR: Unable to get printer status (%s)!\n"),
761 cupsLastErrorString());
766 ippDelete(supported
);
770 else if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
771 IPP_TAG_RANGE
)) != NULL
)
774 * Has the "copies-supported" attribute - does it have an upper
778 if (copies_sup
->values
[0].range
.upper
<= 1)
779 copies_sup
= NULL
; /* No */
782 format_sup
= ippFindAttribute(supported
, "document-format-supported",
787 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
788 format_sup
->num_values
);
789 for (i
= 0; i
< format_sup
->num_values
; i
++)
790 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
791 format_sup
->values
[i
].string
.text
);
794 report_printer_state(supported
, 0);
796 while (ipp_status
> IPP_OK_CONFLICT
);
799 * See if the printer is accepting jobs and is not stopped; if either
800 * condition is true and we are printing to a class, requeue the job...
803 if (getenv("CLASS") != NULL
)
805 printer_state
= ippFindAttribute(supported
, "printer-state",
807 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
810 if (printer_state
== NULL
||
811 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
813 printer_accepting
== NULL
||
814 !printer_accepting
->values
[0].boolean
)
817 * If the CLASS environment variable is set, the job was submitted
818 * to a class and not to a specific queue. In this case, we want
819 * to abort immediately so that the job can be requeued on the next
820 * available printer in the class.
823 _cupsLangPuts(stderr
,
824 _("INFO: Unable to contact printer, queuing on next "
825 "printer in class...\n"));
827 ippDelete(supported
);
834 * Sleep 5 seconds to keep the job from requeuing too rapidly...
839 return (CUPS_BACKEND_FAILED
);
846 * If we've shown a recoverable error make sure the printer proxies
847 * have a chance to see the recovered message. Not pretty but
848 * necessary for now...
851 fputs("INFO: recovered: \n", stderr
);
856 * See if the printer supports multiple copies...
859 copies
= atoi(argv
[4]);
861 if (copies_sup
|| argc
< 7)
863 copies_remaining
= 1;
869 copies_remaining
= copies
;
872 * Then issue the print-job request...
877 while (copies_remaining
> 0)
880 * Check for side-channel requests...
883 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
886 * Build the IPP request...
893 request
= ippNewRequest(IPP_CREATE_JOB
);
895 request
= ippNewRequest(IPP_PRINT_JOB
);
897 request
->request
.op
.version
[0] = version
/ 10;
898 request
->request
.op
.version
[1] = version
% 10;
900 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
903 fprintf(stderr
, "DEBUG: printer-uri = \"%s\"\n", uri
);
906 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
907 "requesting-user-name", NULL
, argv
[2]);
909 fprintf(stderr
, "DEBUG: requesting-user-name = \"%s\"\n", argv
[2]);
912 * Only add a "job-name" attribute if the remote server supports
913 * copy generation - some IPP implementations like HP's don't seem
914 * to like UTF-8 job names (STR #1837)...
917 if (argv
[3][0] && copies_sup
)
918 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
921 fprintf(stderr
, "DEBUG: job-name = \"%s\"\n", argv
[3]);
925 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
926 "compression", NULL
, "gzip");
927 #endif /* HAVE_LIBZ */
930 * Handle options on the command-line...
934 num_options
= cupsParseOptions(argv
[5], 0, &options
);
937 if (!strcasecmp(final_content_type
, "application/pictwps") &&
940 if (format_sup
!= NULL
)
942 for (i
= 0; i
< format_sup
->num_values
; i
++)
943 if (!strcasecmp(final_content_type
, format_sup
->values
[i
].string
.text
))
947 if (format_sup
== NULL
|| i
>= format_sup
->num_values
)
950 * Remote doesn't support "application/pictwps" (i.e. it's not MacOS X)
951 * so convert the document to PostScript...
954 if (run_pictwps_filter(argv
, files
[0]))
962 return (CUPS_BACKEND_FAILED
);
965 files
[0] = pstmpname
;
968 * Change the MIME type to application/postscript and change the
969 * number of copies to 1...
972 final_content_type
= "application/postscript";
974 copies_remaining
= 1;
978 #endif /* __APPLE__ */
980 if (format_sup
!= NULL
)
982 for (i
= 0; i
< format_sup
->num_values
; i
++)
983 if (!strcasecmp(final_content_type
, format_sup
->values
[i
].string
.text
))
986 if (i
< format_sup
->num_values
)
987 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
988 "document-format", NULL
, final_content_type
);
991 if (copies_sup
&& version
> 10 && send_options
)
994 * Only send options if the destination printer supports the copies
995 * attribute and IPP/1.1. This is a hack for the HP and Lexmark
996 * implementations of IPP, which do not accept extension attributes
997 * and incorrectly report a client-error-bad-request error instead of
998 * the successful-ok-unsupported-attributes status. In short, at least
999 * some HP and Lexmark implementations of IPP are non-compliant.
1002 cupsEncodeOptions(request
, num_options
, options
);
1004 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies",
1008 cupsFreeOptions(num_options
, options
);
1011 * If copies aren't supported, then we are likely dealing with an HP
1012 * JetDirect. The HP IPP implementation seems to close the connection
1013 * after every request - that is, it does *not* implement HTTP Keep-
1014 * Alive, which is REQUIRED by HTTP/1.1...
1018 httpReconnect(http
);
1025 response
= cupsDoRequest(http
, request
, resource
);
1027 response
= cupsDoFileRequest(http
, request
, resource
, files
[0]);
1029 ipp_status
= cupsLastError();
1031 if (ipp_status
> IPP_OK_CONFLICT
)
1038 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1039 ipp_status
== IPP_PRINTER_BUSY
)
1041 _cupsLangPuts(stderr
,
1042 _("INFO: Printer busy; will retry in 10 seconds...\n"));
1045 else if ((ipp_status
== IPP_BAD_REQUEST
||
1046 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
> 10)
1049 * Switch to IPP/1.0...
1052 _cupsLangPrintf(stderr
,
1053 _("INFO: Printer does not support IPP/%d.%d, trying "
1054 "IPP/1.0...\n"), version
/ 10, version
% 10);
1056 httpReconnect(http
);
1059 _cupsLangPrintf(stderr
, _("ERROR: Print file was not accepted (%s)!\n"),
1060 cupsLastErrorString());
1062 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
1063 IPP_TAG_INTEGER
)) == NULL
)
1065 _cupsLangPuts(stderr
,
1066 _("NOTICE: Print file accepted - job ID unknown.\n"));
1071 job_id
= job_id_attr
->values
[0].integer
;
1072 _cupsLangPrintf(stderr
, _("NOTICE: Print file accepted - job ID %d.\n"),
1076 ippDelete(response
);
1081 if (job_id
&& num_files
> 1)
1083 for (i
= 0; i
< num_files
; i
++)
1086 * Check for side-channel requests...
1089 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1092 * Send the next file in the job...
1095 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
1096 request
->request
.op
.version
[0] = version
/ 10;
1097 request
->request
.op
.version
[1] = version
% 10;
1099 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1102 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1106 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1107 "requesting-user-name", NULL
, argv
[2]);
1109 if ((i
+ 1) == num_files
)
1110 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
1112 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1113 "document-format", NULL
, content_type
);
1115 ippDelete(cupsDoFileRequest(http
, request
, resource
, files
[i
]));
1117 if (cupsLastError() > IPP_OK_CONFLICT
)
1119 ipp_status
= cupsLastError();
1121 _cupsLangPrintf(stderr
,
1122 _("ERROR: Unable to add file %d to job: %s\n"),
1123 job_id
, cupsLastErrorString());
1129 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
1131 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
1132 copies_remaining
--;
1134 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1135 ipp_status
== IPP_PRINTER_BUSY
)
1138 copies_remaining
--;
1141 * Wait for the job to complete...
1144 if (!job_id
|| !waitjob
)
1147 _cupsLangPuts(stderr
, _("INFO: Waiting for job to complete...\n"));
1149 for (delay
= 1; !job_cancelled
;)
1152 * Check for side-channel requests...
1155 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1158 * Build an IPP_GET_JOB_ATTRIBUTES request...
1161 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1162 request
->request
.op
.version
[0] = version
/ 10;
1163 request
->request
.op
.version
[1] = version
% 10;
1165 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1168 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1172 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1173 "requesting-user-name", NULL
, argv
[2]);
1175 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1176 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
1184 httpReconnect(http
);
1186 response
= cupsDoRequest(http
, request
, resource
);
1187 ipp_status
= cupsLastError();
1189 if (ipp_status
== IPP_NOT_FOUND
)
1192 * Job has gone away and/or the server has no job history...
1195 ippDelete(response
);
1197 ipp_status
= IPP_OK
;
1201 if (ipp_status
> IPP_OK_CONFLICT
)
1203 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1204 ipp_status
!= IPP_PRINTER_BUSY
)
1206 ippDelete(response
);
1208 _cupsLangPrintf(stderr
,
1209 _("ERROR: Unable to get job %d attributes (%s)!\n"),
1210 job_id
, cupsLastErrorString());
1217 if ((job_state
= ippFindAttribute(response
, "job-state",
1218 IPP_TAG_ENUM
)) != NULL
)
1221 * Stop polling if the job is finished or pending-held...
1224 if (job_state
->values
[0].integer
> IPP_JOB_STOPPED
)
1226 if ((job_sheets
= ippFindAttribute(response
,
1227 "job-media-sheets-completed",
1228 IPP_TAG_INTEGER
)) != NULL
)
1229 fprintf(stderr
, "PAGE: total %d\n",
1230 job_sheets
->values
[0].integer
);
1232 ippDelete(response
);
1238 ippDelete(response
);
1241 * Check the printer state and report it if necessary...
1244 check_printer_state(http
, uri
, resource
, argv
[2], version
, job_id
);
1247 * Wait 1-10 seconds before polling again...
1259 * Cancel the job as needed...
1262 if (job_cancelled
&& job_id
)
1263 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1266 * Check the printer state and report it if necessary...
1269 check_printer_state(http
, uri
, resource
, argv
[2], version
, job_id
);
1272 * Collect the final page count as needed...
1275 if (have_supplies
&&
1276 !backendSNMPSupplies(snmp_fd
, http
->hostaddr
, &page_count
, NULL
) &&
1277 page_count
> start_count
)
1278 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
1281 * Update auth-info-required as needed...
1284 if (ipp_status
== IPP_NOT_AUTHORIZED
)
1286 if (!strncmp(httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
),
1288 fputs("ATTR: auth-info-required=negotiate\n", stderr
);
1290 fputs("ATTR: auth-info-required=username,password\n", stderr
);
1299 ippDelete(supported
);
1302 * Remove the temporary file(s) if necessary...
1306 unlink(tmpfilename
);
1311 for (i
= 0; i
< num_files
; i
++)
1314 #endif /* HAVE_LIBZ */
1319 #endif /* __APPLE__ */
1322 * Return the queue status...
1325 if (ipp_status
== IPP_NOT_AUTHORIZED
)
1326 return (CUPS_BACKEND_AUTH_REQUIRED
);
1327 else if (ipp_status
> IPP_OK_CONFLICT
)
1328 return (CUPS_BACKEND_FAILED
);
1330 return (CUPS_BACKEND_OK
);
1335 * 'cancel_job()' - Cancel a print job.
1339 cancel_job(http_t
*http
, /* I - HTTP connection */
1340 const char *uri
, /* I - printer-uri */
1341 int id
, /* I - job-id */
1342 const char *resource
, /* I - Resource path */
1343 const char *user
, /* I - requesting-user-name */
1344 int version
) /* I - IPP version */
1346 ipp_t
*request
; /* Cancel-Job request */
1349 _cupsLangPuts(stderr
, _("INFO: Canceling print job...\n"));
1351 request
= ippNewRequest(IPP_CANCEL_JOB
);
1352 request
->request
.op
.version
[0] = version
/ 10;
1353 request
->request
.op
.version
[1] = version
% 10;
1355 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1357 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1359 if (user
&& user
[0])
1360 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1361 "requesting-user-name", NULL
, user
);
1367 ippDelete(cupsDoRequest(http
, request
, resource
));
1369 if (cupsLastError() > IPP_OK_CONFLICT
)
1370 _cupsLangPrintf(stderr
, _("ERROR: Unable to cancel job %d: %s\n"), id
,
1371 cupsLastErrorString());
1376 * 'check_printer_state()' - Check the printer state...
1380 check_printer_state(
1381 http_t
*http
, /* I - HTTP connection */
1382 const char *uri
, /* I - Printer URI */
1383 const char *resource
, /* I - Resource path */
1384 const char *user
, /* I - Username, if any */
1385 int version
, /* I - IPP version */
1386 int job_id
) /* I - Current job ID */
1388 ipp_t
*request
, /* IPP request */
1389 *response
; /* IPP response */
1390 static const char * const attrs
[] = /* Attributes we want */
1392 "com.apple.print.recoverable-message",
1398 "printer-state-message",
1399 "printer-state-reasons"
1404 * Check on the printer state...
1407 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1408 request
->request
.op
.version
[0] = version
/ 10;
1409 request
->request
.op
.version
[1] = version
% 10;
1411 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1414 if (user
&& user
[0])
1415 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1416 "requesting-user-name", NULL
, user
);
1418 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1419 "requested-attributes",
1420 (int)(sizeof(attrs
) / sizeof(attrs
[0])), NULL
, attrs
);
1426 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
1428 report_printer_state(response
, job_id
);
1429 ippDelete(response
);
1436 * 'compress_files()' - Compress print files...
1440 compress_files(int num_files
, /* I - Number of files */
1441 char **files
) /* I - Files */
1443 int i
, /* Looping var */
1444 fd
; /* Temporary file descriptor */
1445 ssize_t bytes
; /* Bytes read/written */
1446 size_t total
; /* Total bytes read */
1447 cups_file_t
*in
, /* Input file */
1448 *out
; /* Output file */
1449 struct stat outinfo
; /* Output file information */
1450 char filename
[1024], /* Temporary filename */
1451 buffer
[32768]; /* Copy buffer */
1454 fprintf(stderr
, "DEBUG: Compressing %d job files...\n", num_files
);
1455 for (i
= 0; i
< num_files
; i
++)
1457 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
1459 _cupsLangPrintf(stderr
,
1460 _("ERROR: Unable to create temporary compressed print "
1461 "file: %s\n"), strerror(errno
));
1462 exit(CUPS_BACKEND_FAILED
);
1465 if ((out
= cupsFileOpenFd(fd
, "w9")) == NULL
)
1467 _cupsLangPrintf(stderr
,
1468 _("ERROR: Unable to open temporary compressed print "
1469 "file: %s\n"), strerror(errno
));
1470 exit(CUPS_BACKEND_FAILED
);
1473 if ((in
= cupsFileOpen(files
[i
], "r")) == NULL
)
1475 _cupsLangPrintf(stderr
,
1476 _("ERROR: Unable to open print file \"%s\": %s\n"),
1477 files
[i
], strerror(errno
));
1479 exit(CUPS_BACKEND_FAILED
);
1483 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
1484 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
1486 _cupsLangPrintf(stderr
,
1487 _("ERROR: Unable to write %d bytes to \"%s\": %s\n"),
1488 (int)bytes
, filename
, strerror(errno
));
1491 exit(CUPS_BACKEND_FAILED
);
1499 files
[i
] = strdup(filename
);
1501 if (!stat(filename
, &outinfo
))
1503 "DEBUG: File %d compressed to %.1f%% of original size, "
1504 CUPS_LLFMT
" bytes...\n",
1505 i
+ 1, 100.0 * outinfo
.st_size
/ total
,
1506 CUPS_LLCAST outinfo
.st_size
);
1509 #endif /* HAVE_LIBZ */
1513 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
1516 static const char * /* O - Password */
1517 password_cb(const char *prompt
) /* I - Prompt (not used) */
1521 if (password
&& *password
&& password_tries
< 3)
1530 * If there is no password set in the device URI, return the
1531 * "authentication required" exit code...
1535 unlink(tmpfilename
);
1540 #endif /* __APPLE__ */
1542 fputs("ATTR: auth-info-required=username,password\n", stderr
);
1544 exit(CUPS_BACKEND_AUTH_REQUIRED
);
1546 return (NULL
); /* Eliminate compiler warning */
1552 * 'report_attr()' - Report an IPP attribute value.
1556 report_attr(ipp_attribute_t
*attr
) /* I - Attribute */
1558 int i
; /* Looping var */
1559 char value
[1024], /* Value string */
1560 *valptr
, /* Pointer into value string */
1561 *attrptr
; /* Pointer into attribute value */
1565 * Convert the attribute values into quoted strings...
1568 for (i
= 0, valptr
= value
;
1569 i
< attr
->num_values
&& valptr
< (value
+ sizeof(value
) - 10);
1575 switch (attr
->value_tag
)
1577 case IPP_TAG_INTEGER
:
1579 snprintf(valptr
, sizeof(value
) - (valptr
- value
), "%d",
1580 attr
->values
[i
].integer
);
1581 valptr
+= strlen(valptr
);
1586 case IPP_TAG_KEYWORD
:
1588 for (attrptr
= attr
->values
[i
].string
.text
;
1589 *attrptr
&& valptr
< (value
+ sizeof(value
) - 10);
1592 if (*attrptr
== '\\' || *attrptr
== '\"')
1595 *valptr
++ = *attrptr
;
1602 * Unsupported value type...
1612 * Tell the scheduler about the new values...
1615 fprintf(stderr
, "ATTR: %s=%s\n", attr
->name
, value
);
1620 * 'report_printer_state()' - Report the printer state.
1623 static int /* O - Number of reasons shown */
1624 report_printer_state(ipp_t
*ipp
, /* I - IPP response */
1625 int job_id
) /* I - Current job ID */
1627 int i
; /* Looping var */
1628 int count
; /* Count of reasons shown... */
1629 ipp_attribute_t
*caprm
, /* com.apple.print.recoverable-message */
1630 *psm
, /* printer-state-message */
1631 *reasons
, /* printer-state-reasons */
1632 *marker
; /* marker-* attributes */
1633 const char *reason
; /* Current reason */
1634 const char *prefix
; /* Prefix for STATE: line */
1635 char state
[1024]; /* State string */
1636 int saw_caprw
; /* Saw com.apple.print.recoverable-warning state */
1639 if ((psm
= ippFindAttribute(ipp
, "printer-state-message",
1640 IPP_TAG_TEXT
)) != NULL
)
1641 fprintf(stderr
, "INFO: %s\n", psm
->values
[0].string
.text
);
1643 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
1644 IPP_TAG_KEYWORD
)) == NULL
)
1651 for (i
= 0, count
= 0; i
< reasons
->num_values
; i
++)
1653 reason
= reasons
->values
[i
].string
.text
;
1655 if (!strcmp(reason
, "com.apple.print.recoverable-warning"))
1657 else if (strcmp(reason
, "paused"))
1659 strlcat(state
, prefix
, sizeof(state
));
1660 strlcat(state
, reason
, sizeof(state
));
1667 fprintf(stderr
, "%s\n", state
);
1670 * Relay com.apple.print.recoverable-message...
1673 if ((caprm
= ippFindAttribute(ipp
, "com.apple.print.recoverable-message",
1674 IPP_TAG_TEXT
)) != NULL
)
1675 fprintf(stderr
, "WARNING: %s: %s\n",
1676 saw_caprw
? "recoverable" : "recovered",
1677 caprm
->values
[0].string
.text
);
1680 * Relay the current marker-* attribute values...
1683 if ((marker
= ippFindAttribute(ipp
, "marker-colors", IPP_TAG_NAME
)) != NULL
)
1684 report_attr(marker
);
1685 if ((marker
= ippFindAttribute(ipp
, "marker-high-levels",
1686 IPP_TAG_INTEGER
)) != NULL
)
1687 report_attr(marker
);
1688 if ((marker
= ippFindAttribute(ipp
, "marker-levels",
1689 IPP_TAG_INTEGER
)) != NULL
)
1690 report_attr(marker
);
1691 if ((marker
= ippFindAttribute(ipp
, "marker-low-levels",
1692 IPP_TAG_INTEGER
)) != NULL
)
1693 report_attr(marker
);
1694 if ((marker
= ippFindAttribute(ipp
, "marker-message", IPP_TAG_TEXT
)) != NULL
)
1695 report_attr(marker
);
1696 if ((marker
= ippFindAttribute(ipp
, "marker-names", IPP_TAG_NAME
)) != NULL
)
1697 report_attr(marker
);
1698 if ((marker
= ippFindAttribute(ipp
, "marker-types", IPP_TAG_KEYWORD
)) != NULL
)
1699 report_attr(marker
);
1707 * 'run_pictwps_filter()' - Convert PICT files to PostScript when printing
1710 * This step is required because the PICT format is not documented and
1711 * subject to change, so developing a filter for other OS's is infeasible.
1712 * Also, fonts required by the PICT file need to be embedded on the
1713 * client side (which has the fonts), so we run the filter to get a
1714 * PostScript file for printing...
1717 static int /* O - Exit status of filter */
1718 run_pictwps_filter(char **argv
, /* I - Command-line arguments */
1719 const char *filename
)/* I - Filename */
1721 struct stat fileinfo
; /* Print file information */
1722 const char *ppdfile
; /* PPD file for destination printer */
1723 int pid
; /* Child process ID */
1724 int fd
; /* Temporary file descriptor */
1725 int status
; /* Exit status of filter */
1726 const char *printer
; /* PRINTER env var */
1727 static char ppdenv
[1024]; /* PPD environment variable */
1731 * First get the PPD file for the printer...
1734 printer
= getenv("PRINTER");
1737 _cupsLangPuts(stderr
,
1738 _("ERROR: PRINTER environment variable not defined!\n"));
1742 if ((ppdfile
= cupsGetPPD(printer
)) == NULL
)
1744 _cupsLangPrintf(stderr
,
1745 _("ERROR: Unable to get PPD file for printer \"%s\" - "
1746 "%s.\n"), printer
, cupsLastErrorString());
1750 snprintf(ppdenv
, sizeof(ppdenv
), "PPD=%s", ppdfile
);
1755 * Then create a temporary file for printing...
1758 if ((fd
= cupsTempFd(pstmpname
, sizeof(pstmpname
))) < 0)
1760 _cupsLangPrintError(_("ERROR: Unable to create temporary file"));
1767 * Get the owner of the spool file - it is owned by the user we want to run
1772 stat(argv
[6], &fileinfo
);
1776 * Use the OSX defaults, as an up-stream filter created the PICT
1780 fileinfo
.st_uid
= 1;
1781 fileinfo
.st_gid
= 80;
1785 chown(ppdfile
, fileinfo
.st_uid
, fileinfo
.st_gid
);
1787 fchown(fd
, fileinfo
.st_uid
, fileinfo
.st_gid
);
1790 * Finally, run the filter to convert the file...
1793 if ((pid
= fork()) == 0)
1796 * Child process for pictwpstops... Redirect output of pictwpstops to a
1807 * Change to an unpriviledged user...
1810 setgid(fileinfo
.st_gid
);
1811 setuid(fileinfo
.st_uid
);
1814 execlp("pictwpstops", printer
, argv
[1], argv
[2], argv
[3], argv
[4], argv
[5],
1816 _cupsLangPrintf(stderr
, _("ERROR: Unable to exec pictwpstops: %s\n"),
1829 _cupsLangPrintf(stderr
, _("ERROR: Unable to fork pictwpstops: %s\n"),
1837 * Now wait for the filter to complete...
1840 if (wait(&status
) < 0)
1842 _cupsLangPrintf(stderr
, _("ERROR: Unable to wait for pictwpstops: %s\n"),
1858 _cupsLangPrintf(stderr
, _("ERROR: pictwpstops exited with status %d!\n"),
1861 _cupsLangPrintf(stderr
, _("ERROR: pictwpstops exited on signal %d!\n"),
1868 * Return with no errors..
1873 #endif /* __APPLE__ */
1877 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1881 sigterm_handler(int sig
) /* I - Signal */
1883 (void)sig
; /* remove compiler warnings... */
1888 * Flag that the job should be cancelled...
1896 * The scheduler already tried to cancel us once, now just terminate
1897 * after removing our temp files!
1901 unlink(tmpfilename
);
1906 #endif /* __APPLE__ */
1913 * End of "$Id: ipp.c 7948 2008-09-17 00:04:12Z mike $".