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-2008 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 int num_files
; /* Number of files to print */
107 char **files
, /* Files to print */
108 *filename
; /* Pointer to single filename */
109 int port
; /* Port number (not used) */
110 char uri
[HTTP_MAX_URI
]; /* Updated URI without user/pass */
111 ipp_status_t ipp_status
; /* Status of IPP request */
112 http_t
*http
; /* HTTP connection */
113 ipp_t
*request
, /* IPP request */
114 *response
, /* IPP response */
115 *supported
; /* get-printer-attributes response */
116 time_t start_time
; /* Time of first connect */
117 int recoverable
; /* Recoverable error shown? */
118 int contimeout
; /* Connection timeout */
119 int delay
; /* Delay for retries... */
120 int compression
, /* Do compression of the job data? */
121 waitjob
, /* Wait for job complete? */
122 waitprinter
; /* Wait for printer ready? */
123 ipp_attribute_t
*job_id_attr
; /* job-id attribute */
124 int job_id
; /* job-id value */
125 ipp_attribute_t
*job_sheets
; /* job-media-sheets-completed */
126 ipp_attribute_t
*job_state
; /* job-state */
127 ipp_attribute_t
*copies_sup
; /* copies-supported */
128 ipp_attribute_t
*format_sup
; /* document-format-supported */
129 ipp_attribute_t
*printer_state
; /* printer-state attribute */
130 ipp_attribute_t
*printer_accepting
; /* printer-is-accepting-jobs */
131 int copies
, /* Number of copies for job */
132 copies_remaining
; /* Number of copies remaining */
133 const char *content_type
, /* CONTENT_TYPE environment variable */
134 *final_content_type
; /* FINAL_CONTENT_TYPE environment var */
135 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
136 struct sigaction action
; /* Actions for POSIX signals */
137 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
138 int version
; /* IPP version */
139 static const char * const pattrs
[] =
140 { /* Printer attributes we want */
141 "com.apple.print.recoverable-message",
143 "document-format-supported",
149 "printer-is-accepting-jobs",
151 "printer-state-message",
152 "printer-state-reasons",
154 static const char * const jattrs
[] =
155 { /* Job attributes we want */
156 "job-media-sheets-completed",
162 * Make sure status messages are not buffered...
165 setbuf(stderr
, NULL
);
168 * Ignore SIGPIPE and catch SIGTERM signals...
172 sigset(SIGPIPE
, SIG_IGN
);
173 sigset(SIGTERM
, sigterm_handler
);
174 #elif defined(HAVE_SIGACTION)
175 memset(&action
, 0, sizeof(action
));
176 action
.sa_handler
= SIG_IGN
;
177 sigaction(SIGPIPE
, &action
, NULL
);
179 sigemptyset(&action
.sa_mask
);
180 sigaddset(&action
.sa_mask
, SIGTERM
);
181 action
.sa_handler
= sigterm_handler
;
182 sigaction(SIGTERM
, &action
, NULL
);
184 signal(SIGPIPE
, SIG_IGN
);
185 signal(SIGTERM
, sigterm_handler
);
186 #endif /* HAVE_SIGSET */
189 * Check command-line...
196 if ((s
= strrchr(argv
[0], '/')) != NULL
)
201 printf("network %s \"Unknown\" \"Internet Printing Protocol (%s)\"\n",
203 return (CUPS_BACKEND_OK
);
207 _cupsLangPrintf(stderr
,
208 _("Usage: %s job-id user title copies options [file]\n"),
210 return (CUPS_BACKEND_STOP
);
214 * Get the (final) content type...
217 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
218 content_type
= "application/octet-stream";
220 if ((final_content_type
= getenv("FINAL_CONTENT_TYPE")) == NULL
)
222 final_content_type
= content_type
;
224 if (!strncmp(final_content_type
, "printer/", 8))
225 final_content_type
= "application/vnd.cups-raw";
229 * Extract the hostname and printer name from the URI...
232 if ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
233 return (CUPS_BACKEND_FAILED
);
235 if (httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
,
236 method
, sizeof(method
), username
, sizeof(username
),
237 hostname
, sizeof(hostname
), &port
,
238 resource
, sizeof(resource
)) < HTTP_URI_OK
)
240 _cupsLangPuts(stderr
,
241 _("ERROR: Missing device URI on command-line and no "
242 "DEVICE_URI environment variable!\n"));
243 return (CUPS_BACKEND_STOP
);
247 port
= IPP_PORT
; /* Default to port 631 */
249 if (!strcmp(method
, "https"))
250 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
252 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
255 * See if there are any options...
262 contimeout
= 7 * 24 * 60 * 60;
264 if ((optptr
= strchr(resource
, '?')) != NULL
)
267 * Yup, terminate the device name string and move to the first
268 * character of the optptr...
274 * Then parse the optptr...
285 while (*optptr
&& *optptr
!= '=' && *optptr
!= '+' && *optptr
!= '&')
288 if ((sep
= *optptr
) != '\0')
299 while (*optptr
&& *optptr
!= '+' && *optptr
!= '&')
309 * Process the option...
312 if (!strcasecmp(name
, "waitjob"))
315 * Wait for job completion?
318 waitjob
= !strcasecmp(value
, "on") ||
319 !strcasecmp(value
, "yes") ||
320 !strcasecmp(value
, "true");
322 else if (!strcasecmp(name
, "waitprinter"))
325 * Wait for printer idle?
328 waitprinter
= !strcasecmp(value
, "on") ||
329 !strcasecmp(value
, "yes") ||
330 !strcasecmp(value
, "true");
332 else if (!strcasecmp(name
, "encryption"))
335 * Enable/disable encryption?
338 if (!strcasecmp(value
, "always"))
339 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
340 else if (!strcasecmp(value
, "required"))
341 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
342 else if (!strcasecmp(value
, "never"))
343 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
344 else if (!strcasecmp(value
, "ifrequested"))
345 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
348 _cupsLangPrintf(stderr
,
349 _("ERROR: Unknown encryption option value \"%s\"!\n"),
353 else if (!strcasecmp(name
, "version"))
355 if (!strcmp(value
, "1.0"))
357 else if (!strcmp(value
, "1.1"))
361 _cupsLangPrintf(stderr
,
362 _("ERROR: Unknown version option value \"%s\"!\n"),
367 else if (!strcasecmp(name
, "compression"))
369 compression
= !strcasecmp(value
, "true") ||
370 !strcasecmp(value
, "yes") ||
371 !strcasecmp(value
, "on") ||
372 !strcasecmp(value
, "gzip");
374 #endif /* HAVE_LIBZ */
375 else if (!strcasecmp(name
, "contimeout"))
378 * Set the connection timeout...
382 contimeout
= atoi(value
);
390 _cupsLangPrintf(stderr
,
391 _("ERROR: Unknown option \"%s\" with value \"%s\"!\n"),
398 * If we have 7 arguments, print the file named on the command-line.
399 * Otherwise, copy stdin to a temporary file and print the temporary
406 * Copy stdin to a temporary file...
409 int fd
; /* File descriptor */
410 cups_file_t
*fp
; /* Temporary file */
411 char buffer
[8192]; /* Buffer for copying */
412 int bytes
; /* Number of bytes read */
413 off_t tbytes
; /* Total bytes copied */
416 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
418 _cupsLangPrintError(_("ERROR: Unable to create temporary file"));
419 return (CUPS_BACKEND_FAILED
);
422 if ((fp
= cupsFileOpenFd(fd
, compression
? "w9" : "w")) == NULL
)
424 _cupsLangPrintError(_("ERROR: Unable to open temporary file"));
427 return (CUPS_BACKEND_FAILED
);
432 while ((bytes
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0)
433 if (cupsFileWrite(fp
, buffer
, bytes
) < bytes
)
435 _cupsLangPrintError(_("ERROR: Unable to write to temporary file"));
438 return (CUPS_BACKEND_FAILED
);
446 * Don't try printing files less than 2 bytes...
451 _cupsLangPuts(stderr
, _("ERROR: Empty print file!\n"));
453 return (CUPS_BACKEND_FAILED
);
457 * Point to the single file from stdin...
460 filename
= tmpfilename
;
468 * Point to the files on the command-line...
471 num_files
= argc
- 6;
477 compress_files(num_files
, files
);
478 #endif /* HAVE_LIBZ */
481 fprintf(stderr
, "DEBUG: %d files to send in job...\n", num_files
);
484 * Set the authentication info, if any...
487 cupsSetPasswordCB(password_cb
);
492 * Use authenticaion information in the device URI...
495 if ((password
= strchr(username
, ':')) != NULL
)
498 cupsSetUser(username
);
503 * Try loading authentication information from the environment.
506 const char *ptr
= getenv("AUTH_USERNAME");
511 password
= getenv("AUTH_PASSWORD");
515 * Try connecting to the remote server...
520 start_time
= time(NULL
);
522 fputs("STATE: +connecting-to-device\n", stderr
);
526 _cupsLangPrintf(stderr
, _("INFO: Connecting to %s on port %d...\n"),
529 if ((http
= httpConnectEncrypt(hostname
, port
, cupsEncryption())) == NULL
)
534 if (getenv("CLASS") != NULL
)
537 * If the CLASS environment variable is set, the job was submitted
538 * to a class and not to a specific queue. In this case, we want
539 * to abort immediately so that the job can be requeued on the next
540 * available printer in the class.
543 _cupsLangPuts(stderr
,
544 _("INFO: Unable to contact printer, queuing on next "
545 "printer in class...\n"));
551 * Sleep 5 seconds to keep the job from requeuing too rapidly...
556 return (CUPS_BACKEND_FAILED
);
559 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
560 errno
== EHOSTUNREACH
)
562 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
564 _cupsLangPuts(stderr
, _("ERROR: Printer not responding!\n"));
565 return (CUPS_BACKEND_FAILED
);
570 _cupsLangPrintf(stderr
,
571 _("WARNING: recoverable: Network host \'%s\' is busy; "
572 "will retry in %d seconds...\n"),
582 _cupsLangPrintf(stderr
, _("ERROR: Unable to locate printer \'%s\'!\n"),
584 return (CUPS_BACKEND_STOP
);
590 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
591 _cupsLangPuts(stderr
,
592 _("ERROR: recoverable: Unable to connect to printer; will "
593 "retry in 30 seconds...\n"));
601 while (http
== NULL
);
608 return (CUPS_BACKEND_FAILED
);
611 fputs("STATE: -connecting-to-device\n", stderr
);
612 _cupsLangPrintf(stderr
, _("INFO: Connected to %s...\n"), hostname
);
615 if (http
->hostaddr
->addr
.sa_family
== AF_INET6
)
616 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
617 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
618 ntohs(http
->hostaddr
->ipv6
.sin6_port
));
620 #endif /* AF_INET6 */
621 if (http
->hostaddr
->addr
.sa_family
== AF_INET
)
622 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4)...\n",
623 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
624 ntohs(http
->hostaddr
->ipv4
.sin_port
));
627 * See if the printer supports SNMP...
630 if ((snmp_fd
= _cupsSNMPOpen(http
->hostaddr
->addr
.sa_family
)) >= 0)
632 if (backendSNMPSupplies(snmp_fd
, http
->hostaddr
, &start_count
, NULL
))
638 _cupsSNMPClose(snmp_fd
);
646 * Build a URI for the printer and fill the standard IPP attributes for
647 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
648 * might contain username:password information...
651 snprintf(uri
, sizeof(uri
), "%s://%s:%d%s", method
, hostname
, port
, resource
);
654 * First validate the destination and see if the device supports multiple
655 * copies. We have to do this because some IPP servers (e.g. HP JetDirect)
656 * don't support the copies attribute...
666 * Check for side-channel requests...
669 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
672 * Build the IPP request...
675 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
676 request
->request
.op
.version
[1] = version
;
678 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
681 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
682 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
689 fputs("DEBUG: Getting supported attributes...\n", stderr
);
691 if ((supported
= cupsDoRequest(http
, request
, resource
)) == NULL
)
692 ipp_status
= cupsLastError();
694 ipp_status
= supported
->request
.status
.status_code
;
696 if (ipp_status
> IPP_OK_CONFLICT
)
698 if (ipp_status
== IPP_PRINTER_BUSY
||
699 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
701 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
703 _cupsLangPuts(stderr
, _("ERROR: Printer not responding!\n"));
704 return (CUPS_BACKEND_FAILED
);
709 _cupsLangPrintf(stderr
,
710 _("WARNING: recoverable: Network host \'%s\' is busy; "
711 "will retry in %d seconds...\n"),
714 report_printer_state(supported
, 0);
721 else if ((ipp_status
== IPP_BAD_REQUEST
||
722 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
== 1)
725 * Switch to IPP/1.0...
728 _cupsLangPuts(stderr
,
729 _("INFO: Printer does not support IPP/1.1, trying "
734 else if (ipp_status
== IPP_NOT_FOUND
)
736 _cupsLangPuts(stderr
, _("ERROR: Destination printer does not exist!\n"));
739 ippDelete(supported
);
741 return (CUPS_BACKEND_STOP
);
745 _cupsLangPrintf(stderr
,
746 _("ERROR: Unable to get printer status (%s)!\n"),
747 cupsLastErrorString());
752 ippDelete(supported
);
756 else if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
757 IPP_TAG_RANGE
)) != NULL
)
760 * Has the "copies-supported" attribute - does it have an upper
764 if (copies_sup
->values
[0].range
.upper
<= 1)
765 copies_sup
= NULL
; /* No */
768 format_sup
= ippFindAttribute(supported
, "document-format-supported",
773 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
774 format_sup
->num_values
);
775 for (i
= 0; i
< format_sup
->num_values
; i
++)
776 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
777 format_sup
->values
[i
].string
.text
);
780 report_printer_state(supported
, 0);
782 while (ipp_status
> IPP_OK_CONFLICT
);
785 * See if the printer is accepting jobs and is not stopped; if either
786 * condition is true and we are printing to a class, requeue the job...
789 if (getenv("CLASS") != NULL
)
791 printer_state
= ippFindAttribute(supported
, "printer-state",
793 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
796 if (printer_state
== NULL
||
797 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
799 printer_accepting
== NULL
||
800 !printer_accepting
->values
[0].boolean
)
803 * If the CLASS environment variable is set, the job was submitted
804 * to a class and not to a specific queue. In this case, we want
805 * to abort immediately so that the job can be requeued on the next
806 * available printer in the class.
809 _cupsLangPuts(stderr
,
810 _("INFO: Unable to contact printer, queuing on next "
811 "printer in class...\n"));
813 ippDelete(supported
);
820 * Sleep 5 seconds to keep the job from requeuing too rapidly...
825 return (CUPS_BACKEND_FAILED
);
832 * If we've shown a recoverable error make sure the printer proxies
833 * have a chance to see the recovered message. Not pretty but
834 * necessary for now...
837 fputs("INFO: recovered: \n", stderr
);
842 * See if the printer supports multiple copies...
845 copies
= atoi(argv
[4]);
847 if (copies_sup
|| argc
< 7)
849 copies_remaining
= 1;
855 copies_remaining
= copies
;
858 * Then issue the print-job request...
863 while (copies_remaining
> 0)
866 * Check for side-channel requests...
869 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
872 * Build the IPP request...
879 request
= ippNewRequest(IPP_CREATE_JOB
);
881 request
= ippNewRequest(IPP_PRINT_JOB
);
883 request
->request
.op
.version
[1] = version
;
885 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
888 fprintf(stderr
, "DEBUG: printer-uri = \"%s\"\n", uri
);
891 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
892 "requesting-user-name", NULL
, argv
[2]);
894 fprintf(stderr
, "DEBUG: requesting-user-name = \"%s\"\n", argv
[2]);
897 * Only add a "job-name" attribute if the remote server supports
898 * copy generation - some IPP implementations like HP's don't seem
899 * to like UTF-8 job names (STR #1837)...
902 if (argv
[3][0] && copies_sup
)
903 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
906 fprintf(stderr
, "DEBUG: job-name = \"%s\"\n", argv
[3]);
910 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
911 "compression", NULL
, "gzip");
912 #endif /* HAVE_LIBZ */
915 * Handle options on the command-line...
919 num_options
= cupsParseOptions(argv
[5], 0, &options
);
922 if (!strcasecmp(final_content_type
, "application/pictwps") &&
925 if (format_sup
!= NULL
)
927 for (i
= 0; i
< format_sup
->num_values
; i
++)
928 if (!strcasecmp(final_content_type
, format_sup
->values
[i
].string
.text
))
932 if (format_sup
== NULL
|| i
>= format_sup
->num_values
)
935 * Remote doesn't support "application/pictwps" (i.e. it's not MacOS X)
936 * so convert the document to PostScript...
939 if (run_pictwps_filter(argv
, files
[0]))
947 return (CUPS_BACKEND_FAILED
);
950 files
[0] = pstmpname
;
953 * Change the MIME type to application/postscript and change the
954 * number of copies to 1...
957 final_content_type
= "application/postscript";
959 copies_remaining
= 1;
963 #endif /* __APPLE__ */
965 if (format_sup
!= NULL
)
967 for (i
= 0; i
< format_sup
->num_values
; i
++)
968 if (!strcasecmp(final_content_type
, format_sup
->values
[i
].string
.text
))
971 if (i
< format_sup
->num_values
)
972 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
973 "document-format", NULL
, final_content_type
);
976 if (copies_sup
&& version
> 0 && send_options
)
979 * Only send options if the destination printer supports the copies
980 * attribute and IPP/1.1. This is a hack for the HP and Lexmark
981 * implementations of IPP, which do not accept extension attributes
982 * and incorrectly report a client-error-bad-request error instead of
983 * the successful-ok-unsupported-attributes status. In short, at least
984 * some HP and Lexmark implementations of IPP are non-compliant.
987 cupsEncodeOptions(request
, num_options
, options
);
989 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies",
993 cupsFreeOptions(num_options
, options
);
996 * If copies aren't supported, then we are likely dealing with an HP
997 * JetDirect. The HP IPP implementation seems to close the connection
998 * after every request - that is, it does *not* implement HTTP Keep-
999 * Alive, which is REQUIRED by HTTP/1.1...
1003 httpReconnect(http
);
1010 response
= cupsDoRequest(http
, request
, resource
);
1012 response
= cupsDoFileRequest(http
, request
, resource
, files
[0]);
1014 ipp_status
= cupsLastError();
1016 if (ipp_status
> IPP_OK_CONFLICT
)
1023 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1024 ipp_status
== IPP_PRINTER_BUSY
)
1026 _cupsLangPuts(stderr
,
1027 _("INFO: Printer busy; will retry in 10 seconds...\n"));
1030 else if ((ipp_status
== IPP_BAD_REQUEST
||
1031 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
== 1)
1034 * Switch to IPP/1.0...
1037 _cupsLangPuts(stderr
,
1038 _("INFO: Printer does not support IPP/1.1, trying "
1041 httpReconnect(http
);
1044 _cupsLangPrintf(stderr
, _("ERROR: Print file was not accepted (%s)!\n"),
1045 cupsLastErrorString());
1047 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
1048 IPP_TAG_INTEGER
)) == NULL
)
1050 _cupsLangPuts(stderr
,
1051 _("NOTICE: Print file accepted - job ID unknown.\n"));
1056 job_id
= job_id_attr
->values
[0].integer
;
1057 _cupsLangPrintf(stderr
, _("NOTICE: Print file accepted - job ID %d.\n"),
1061 ippDelete(response
);
1066 if (job_id
&& num_files
> 1)
1068 for (i
= 0; i
< num_files
; i
++)
1071 * Check for side-channel requests...
1074 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1077 * Send the next file in the job...
1080 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
1082 request
->request
.op
.version
[1] = version
;
1084 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1087 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1091 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1092 "requesting-user-name", NULL
, argv
[2]);
1094 if ((i
+ 1) == num_files
)
1095 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
1097 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1098 "document-format", NULL
, content_type
);
1100 ippDelete(cupsDoFileRequest(http
, request
, resource
, files
[i
]));
1102 if (cupsLastError() > IPP_OK_CONFLICT
)
1104 ipp_status
= cupsLastError();
1106 _cupsLangPrintf(stderr
,
1107 _("ERROR: Unable to add file %d to job: %s\n"),
1108 job_id
, cupsLastErrorString());
1114 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
1116 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
1117 copies_remaining
--;
1119 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1120 ipp_status
== IPP_PRINTER_BUSY
)
1123 copies_remaining
--;
1126 * Wait for the job to complete...
1129 if (!job_id
|| !waitjob
)
1132 _cupsLangPuts(stderr
, _("INFO: Waiting for job to complete...\n"));
1134 for (delay
= 1; !job_cancelled
;)
1137 * Check for side-channel requests...
1140 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1143 * Build an IPP_GET_JOB_ATTRIBUTES request...
1146 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1147 request
->request
.op
.version
[1] = version
;
1149 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1152 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1156 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1157 "requesting-user-name", NULL
, argv
[2]);
1159 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1160 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
1168 httpReconnect(http
);
1170 response
= cupsDoRequest(http
, request
, resource
);
1171 ipp_status
= cupsLastError();
1173 if (ipp_status
== IPP_NOT_FOUND
)
1176 * Job has gone away and/or the server has no job history...
1179 ippDelete(response
);
1181 ipp_status
= IPP_OK
;
1185 if (ipp_status
> IPP_OK_CONFLICT
)
1187 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1188 ipp_status
!= IPP_PRINTER_BUSY
)
1190 ippDelete(response
);
1192 _cupsLangPrintf(stderr
,
1193 _("ERROR: Unable to get job %d attributes (%s)!\n"),
1194 job_id
, cupsLastErrorString());
1201 if ((job_state
= ippFindAttribute(response
, "job-state",
1202 IPP_TAG_ENUM
)) != NULL
)
1205 * Stop polling if the job is finished or pending-held...
1208 if (job_state
->values
[0].integer
> IPP_JOB_STOPPED
)
1210 if ((job_sheets
= ippFindAttribute(response
,
1211 "job-media-sheets-completed",
1212 IPP_TAG_INTEGER
)) != NULL
)
1213 fprintf(stderr
, "PAGE: total %d\n",
1214 job_sheets
->values
[0].integer
);
1216 ippDelete(response
);
1222 ippDelete(response
);
1225 * Check the printer state and report it if necessary...
1228 check_printer_state(http
, uri
, resource
, argv
[2], version
, job_id
);
1231 * Wait 1-10 seconds before polling again...
1243 * Cancel the job as needed...
1246 if (job_cancelled
&& job_id
)
1247 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1250 * Check the printer state and report it if necessary...
1253 check_printer_state(http
, uri
, resource
, argv
[2], version
, job_id
);
1256 * Collect the final page count as needed...
1260 !backendSNMPSupplies(snmp_fd
, http
->hostaddr
, &page_count
, NULL
) &&
1261 page_count
> start_count
)
1262 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
1270 ippDelete(supported
);
1273 * Remove the temporary file(s) if necessary...
1277 unlink(tmpfilename
);
1282 for (i
= 0; i
< num_files
; i
++)
1285 #endif /* HAVE_LIBZ */
1290 #endif /* __APPLE__ */
1293 * Return the queue status...
1296 if (ipp_status
== IPP_NOT_AUTHORIZED
)
1299 * Authorization failures here mean that we need Kerberos. Username +
1300 * password authentication is handled in the password_cb function.
1303 fputs("ATTR: auth-info-required=negotiate\n", stderr
);
1304 return (CUPS_BACKEND_AUTH_REQUIRED
);
1306 else if (ipp_status
> IPP_OK_CONFLICT
)
1307 return (CUPS_BACKEND_FAILED
);
1309 return (CUPS_BACKEND_OK
);
1314 * 'cancel_job()' - Cancel a print job.
1318 cancel_job(http_t
*http
, /* I - HTTP connection */
1319 const char *uri
, /* I - printer-uri */
1320 int id
, /* I - job-id */
1321 const char *resource
, /* I - Resource path */
1322 const char *user
, /* I - requesting-user-name */
1323 int version
) /* I - IPP version */
1325 ipp_t
*request
; /* Cancel-Job request */
1328 _cupsLangPuts(stderr
, _("INFO: Canceling print job...\n"));
1330 request
= ippNewRequest(IPP_CANCEL_JOB
);
1331 request
->request
.op
.version
[1] = version
;
1333 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1335 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1337 if (user
&& user
[0])
1338 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1339 "requesting-user-name", NULL
, user
);
1345 ippDelete(cupsDoRequest(http
, request
, resource
));
1347 if (cupsLastError() > IPP_OK_CONFLICT
)
1348 _cupsLangPrintf(stderr
, _("ERROR: Unable to cancel job %d: %s\n"), id
,
1349 cupsLastErrorString());
1354 * 'check_printer_state()' - Check the printer state...
1358 check_printer_state(
1359 http_t
*http
, /* I - HTTP connection */
1360 const char *uri
, /* I - Printer URI */
1361 const char *resource
, /* I - Resource path */
1362 const char *user
, /* I - Username, if any */
1363 int version
, /* I - IPP version */
1364 int job_id
) /* I - Current job ID */
1366 ipp_t
*request
, /* IPP request */
1367 *response
; /* IPP response */
1368 static const char * const attrs
[] = /* Attributes we want */
1370 "com.apple.print.recoverable-message",
1376 "printer-state-message",
1377 "printer-state-reasons"
1382 * Check on the printer state...
1385 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1386 request
->request
.op
.version
[1] = version
;
1388 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1391 if (user
&& user
[0])
1392 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1393 "requesting-user-name", NULL
, user
);
1395 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1396 "requested-attributes",
1397 (int)(sizeof(attrs
) / sizeof(attrs
[0])), NULL
, attrs
);
1403 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
1405 report_printer_state(response
, job_id
);
1406 ippDelete(response
);
1413 * 'compress_files()' - Compress print files...
1417 compress_files(int num_files
, /* I - Number of files */
1418 char **files
) /* I - Files */
1420 int i
, /* Looping var */
1421 fd
; /* Temporary file descriptor */
1422 ssize_t bytes
; /* Bytes read/written */
1423 size_t total
; /* Total bytes read */
1424 cups_file_t
*in
, /* Input file */
1425 *out
; /* Output file */
1426 struct stat outinfo
; /* Output file information */
1427 char filename
[1024], /* Temporary filename */
1428 buffer
[32768]; /* Copy buffer */
1431 fprintf(stderr
, "DEBUG: Compressing %d job files...\n", num_files
);
1432 for (i
= 0; i
< num_files
; i
++)
1434 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
1436 _cupsLangPrintf(stderr
,
1437 _("ERROR: Unable to create temporary compressed print "
1438 "file: %s\n"), strerror(errno
));
1439 exit(CUPS_BACKEND_FAILED
);
1442 if ((out
= cupsFileOpenFd(fd
, "w9")) == NULL
)
1444 _cupsLangPrintf(stderr
,
1445 _("ERROR: Unable to open temporary compressed print "
1446 "file: %s\n"), strerror(errno
));
1447 exit(CUPS_BACKEND_FAILED
);
1450 if ((in
= cupsFileOpen(files
[i
], "r")) == NULL
)
1452 _cupsLangPrintf(stderr
,
1453 _("ERROR: Unable to open print file \"%s\": %s\n"),
1454 files
[i
], strerror(errno
));
1456 exit(CUPS_BACKEND_FAILED
);
1460 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
1461 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
1463 _cupsLangPrintf(stderr
,
1464 _("ERROR: Unable to write %d bytes to \"%s\": %s\n"),
1465 (int)bytes
, filename
, strerror(errno
));
1468 exit(CUPS_BACKEND_FAILED
);
1476 files
[i
] = strdup(filename
);
1478 if (!stat(filename
, &outinfo
))
1480 "DEBUG: File %d compressed to %.1f%% of original size, "
1481 CUPS_LLFMT
" bytes...\n",
1482 i
+ 1, 100.0 * outinfo
.st_size
/ total
,
1483 CUPS_LLCAST outinfo
.st_size
);
1486 #endif /* HAVE_LIBZ */
1490 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
1493 static const char * /* O - Password */
1494 password_cb(const char *prompt
) /* I - Prompt (not used) */
1498 if (password
&& *password
&& password_tries
< 3)
1507 * If there is no password set in the device URI, return the
1508 * "authentication required" exit code...
1512 unlink(tmpfilename
);
1517 #endif /* __APPLE__ */
1519 fputs("ATTR: auth-info-required=username,password\n", stderr
);
1521 exit(CUPS_BACKEND_AUTH_REQUIRED
);
1523 return (NULL
); /* Eliminate compiler warning */
1529 * 'report_attr()' - Report an IPP attribute value.
1533 report_attr(ipp_attribute_t
*attr
) /* I - Attribute */
1535 int i
; /* Looping var */
1536 char value
[1024], /* Value string */
1537 *valptr
, /* Pointer into value string */
1538 *attrptr
; /* Pointer into attribute value */
1542 * Convert the attribute values into quoted strings...
1545 for (i
= 0, valptr
= value
;
1546 i
< attr
->num_values
&& valptr
< (value
+ sizeof(value
) - 10);
1552 switch (attr
->value_tag
)
1554 case IPP_TAG_INTEGER
:
1556 snprintf(valptr
, sizeof(value
) - (valptr
- value
), "%d",
1557 attr
->values
[i
].integer
);
1558 valptr
+= strlen(valptr
);
1563 case IPP_TAG_KEYWORD
:
1565 for (attrptr
= attr
->values
[i
].string
.text
;
1566 *attrptr
&& valptr
< (value
+ sizeof(value
) - 10);
1569 if (*attrptr
== '\\' || *attrptr
== '\"')
1572 *valptr
++ = *attrptr
;
1579 * Unsupported value type...
1589 * Tell the scheduler about the new values...
1592 fprintf(stderr
, "ATTR: %s=%s\n", attr
->name
, value
);
1597 * 'report_printer_state()' - Report the printer state.
1600 static int /* O - Number of reasons shown */
1601 report_printer_state(ipp_t
*ipp
, /* I - IPP response */
1602 int job_id
) /* I - Current job ID */
1604 int i
; /* Looping var */
1605 int count
; /* Count of reasons shown... */
1606 ipp_attribute_t
*caprm
, /* com.apple.print.recoverable-message */
1607 *psm
, /* printer-state-message */
1608 *reasons
, /* printer-state-reasons */
1609 *marker
; /* marker-* attributes */
1610 const char *reason
; /* Current reason */
1611 const char *message
; /* Message to show */
1612 char unknown
[1024]; /* Unknown message string */
1613 const char *prefix
; /* Prefix for STATE: line */
1614 char state
[1024]; /* State string */
1615 cups_lang_t
*language
; /* Current localization */
1616 int saw_caprw
; /* Saw com.apple.print.recoverable-warning state */
1619 if ((psm
= ippFindAttribute(ipp
, "printer-state-message",
1620 IPP_TAG_TEXT
)) != NULL
)
1621 fprintf(stderr
, "INFO: %s\n", psm
->values
[0].string
.text
);
1623 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
1624 IPP_TAG_KEYWORD
)) == NULL
)
1630 language
= cupsLangDefault();
1632 for (i
= 0, count
= 0; i
< reasons
->num_values
; i
++)
1634 reason
= reasons
->values
[i
].string
.text
;
1636 if (strcmp(reason
, "paused"))
1638 strlcat(state
, prefix
, sizeof(state
));
1639 strlcat(state
, reason
, sizeof(state
));
1646 if (!strncmp(reason
, "media-needed", 12))
1647 message
= _("Media tray needs to be filled.");
1648 else if (!strncmp(reason
, "media-jam", 9))
1649 message
= _("Media jam!");
1650 else if (!strncmp(reason
, "moving-to-paused", 16) ||
1651 !strncmp(reason
, "paused", 6) ||
1652 !strncmp(reason
, "shutdown", 8))
1653 message
= _("Printer offline.");
1654 else if (!strncmp(reason
, "toner-low", 9))
1655 message
= _("Toner low.");
1656 else if (!strncmp(reason
, "toner-empty", 11))
1657 message
= _("Out of toner!");
1658 else if (!strncmp(reason
, "cover-open", 10))
1659 message
= _("Cover open.");
1660 else if (!strncmp(reason
, "interlock-open", 14))
1661 message
= _("Interlock open.");
1662 else if (!strncmp(reason
, "door-open", 9))
1663 message
= _("Door open.");
1664 else if (!strncmp(reason
, "input-tray-missing", 18))
1665 message
= _("Media tray missing!");
1666 else if (!strncmp(reason
, "media-low", 9))
1667 message
= _("Media tray almost empty.");
1668 else if (!strncmp(reason
, "media-empty", 11))
1669 message
= _("Media tray empty!");
1670 else if (!strncmp(reason
, "output-tray-missing", 19))
1671 message
= _("Output tray missing!");
1672 else if (!strncmp(reason
, "output-area-almost-full", 23))
1673 message
= _("Output bin almost full.");
1674 else if (!strncmp(reason
, "output-area-full", 16))
1675 message
= _("Output bin full!");
1676 else if (!strncmp(reason
, "marker-supply-low", 17))
1677 message
= _("Ink/toner almost empty.");
1678 else if (!strncmp(reason
, "marker-supply-empty", 19))
1679 message
= _("Ink/toner empty!");
1680 else if (!strncmp(reason
, "marker-waste-almost-full", 24))
1681 message
= _("Ink/toner waste bin almost full.");
1682 else if (!strncmp(reason
, "marker-waste-full", 17))
1683 message
= _("Ink/toner waste bin full!");
1684 else if (!strncmp(reason
, "fuser-over-temp", 15))
1685 message
= _("Fuser temperature high!");
1686 else if (!strncmp(reason
, "fuser-under-temp", 16))
1687 message
= _("Fuser temperature low!");
1688 else if (!strncmp(reason
, "opc-near-eol", 12))
1689 message
= _("OPC almost at end-of-life.");
1690 else if (!strncmp(reason
, "opc-life-over", 13))
1691 message
= _("OPC at end-of-life!");
1692 else if (!strncmp(reason
, "developer-low", 13))
1693 message
= _("Developer almost empty.");
1694 else if (!strncmp(reason
, "developer-empty", 15))
1695 message
= _("Developer empty!");
1696 else if (!strcmp(reason
, "com.apple.print.recoverable-warning"))
1698 else if (strstr(reason
, "error") != NULL
)
1702 snprintf(unknown
, sizeof(unknown
), _("Unknown printer error (%s)!"),
1709 if (strstr(reasons
->values
[i
].string
.text
, "error"))
1710 fprintf(stderr
, "ERROR: %s\n", _cupsLangString(language
, message
));
1711 else if (strstr(reasons
->values
[i
].string
.text
, "warning"))
1712 fprintf(stderr
, "WARNING: %s\n", _cupsLangString(language
, message
));
1714 fprintf(stderr
, "INFO: %s\n", _cupsLangString(language
, message
));
1718 fprintf(stderr
, "%s\n", state
);
1721 * Relay com.apple.print.recoverable-message...
1724 if ((caprm
= ippFindAttribute(ipp
, "com.apple.print.recoverable-message",
1725 IPP_TAG_TEXT
)) != NULL
)
1726 fprintf(stderr
, "WARNING: %s: %s\n",
1727 saw_caprw
? "recoverable" : "recovered",
1728 caprm
->values
[0].string
.text
);
1731 * Relay the current marker-* attribute values...
1734 if ((marker
= ippFindAttribute(ipp
, "marker-colors", IPP_TAG_NAME
)) != NULL
)
1735 report_attr(marker
);
1736 if ((marker
= ippFindAttribute(ipp
, "marker-levels",
1737 IPP_TAG_INTEGER
)) != NULL
)
1738 report_attr(marker
);
1739 if ((marker
= ippFindAttribute(ipp
, "marker-message", IPP_TAG_TEXT
)) != NULL
)
1740 report_attr(marker
);
1741 if ((marker
= ippFindAttribute(ipp
, "marker-names", IPP_TAG_NAME
)) != NULL
)
1742 report_attr(marker
);
1743 if ((marker
= ippFindAttribute(ipp
, "marker-types", IPP_TAG_KEYWORD
)) != NULL
)
1744 report_attr(marker
);
1752 * 'run_pictwps_filter()' - Convert PICT files to PostScript when printing
1755 * This step is required because the PICT format is not documented and
1756 * subject to change, so developing a filter for other OS's is infeasible.
1757 * Also, fonts required by the PICT file need to be embedded on the
1758 * client side (which has the fonts), so we run the filter to get a
1759 * PostScript file for printing...
1762 static int /* O - Exit status of filter */
1763 run_pictwps_filter(char **argv
, /* I - Command-line arguments */
1764 const char *filename
)/* I - Filename */
1766 struct stat fileinfo
; /* Print file information */
1767 const char *ppdfile
; /* PPD file for destination printer */
1768 int pid
; /* Child process ID */
1769 int fd
; /* Temporary file descriptor */
1770 int status
; /* Exit status of filter */
1771 const char *printer
; /* PRINTER env var */
1772 static char ppdenv
[1024]; /* PPD environment variable */
1776 * First get the PPD file for the printer...
1779 printer
= getenv("PRINTER");
1782 _cupsLangPuts(stderr
,
1783 _("ERROR: PRINTER environment variable not defined!\n"));
1787 if ((ppdfile
= cupsGetPPD(printer
)) == NULL
)
1789 _cupsLangPrintf(stderr
,
1790 _("ERROR: Unable to get PPD file for printer \"%s\" - "
1791 "%s.\n"), printer
, cupsLastErrorString());
1795 snprintf(ppdenv
, sizeof(ppdenv
), "PPD=%s", ppdfile
);
1800 * Then create a temporary file for printing...
1803 if ((fd
= cupsTempFd(pstmpname
, sizeof(pstmpname
))) < 0)
1805 _cupsLangPrintf(stderr
, _("ERROR: Unable to create temporary file - %s.\n"),
1813 * Get the owner of the spool file - it is owned by the user we want to run
1818 stat(argv
[6], &fileinfo
);
1822 * Use the OSX defaults, as an up-stream filter created the PICT
1826 fileinfo
.st_uid
= 1;
1827 fileinfo
.st_gid
= 80;
1831 chown(ppdfile
, fileinfo
.st_uid
, fileinfo
.st_gid
);
1833 fchown(fd
, fileinfo
.st_uid
, fileinfo
.st_gid
);
1836 * Finally, run the filter to convert the file...
1839 if ((pid
= fork()) == 0)
1842 * Child process for pictwpstops... Redirect output of pictwpstops to a
1853 * Change to an unpriviledged user...
1856 setgid(fileinfo
.st_gid
);
1857 setuid(fileinfo
.st_uid
);
1860 execlp("pictwpstops", printer
, argv
[1], argv
[2], argv
[3], argv
[4], argv
[5],
1862 _cupsLangPrintf(stderr
, _("ERROR: Unable to exec pictwpstops: %s\n"),
1875 _cupsLangPrintf(stderr
, _("ERROR: Unable to fork pictwpstops: %s\n"),
1883 * Now wait for the filter to complete...
1886 if (wait(&status
) < 0)
1888 _cupsLangPrintf(stderr
, _("ERROR: Unable to wait for pictwpstops: %s\n"),
1904 _cupsLangPrintf(stderr
, _("ERROR: pictwpstops exited with status %d!\n"),
1907 _cupsLangPrintf(stderr
, _("ERROR: pictwpstops exited on signal %d!\n"),
1914 * Return with no errors..
1919 #endif /* __APPLE__ */
1923 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1927 sigterm_handler(int sig
) /* I - Signal */
1929 (void)sig
; /* remove compiler warnings... */
1934 * Flag that the job should be cancelled...
1942 * The scheduler already tried to cancel us once, now just terminate
1943 * after removing our temp files!
1947 unlink(tmpfilename
);
1952 #endif /* __APPLE__ */
1959 * End of "$Id: ipp.c 7948 2008-09-17 00:04:12Z mike $".