2 * "$Id: ipp.c 6422 2007-03-30 20:49:37Z mike $"
4 * IPP backend for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * main() - Send a file to the printer or server.
29 * cancel_job() - Cancel a print job.
30 * check_printer_state() - Check the printer state...
31 * compress_files() - Compress print files...
32 * password_cb() - Disable the password prompt for
33 * cupsDoFileRequest().
34 * report_printer_state() - Report the printer state.
35 * run_pictwps_filter() - Convert PICT files to PostScript when printing
37 * sigterm_handler() - Handle 'terminate' signals that stop the backend.
41 * Include necessary headers.
44 #include <cups/http-private.h>
48 #include <sys/types.h>
50 #include <cups/backend.h>
51 #include <cups/cups.h>
52 #include <cups/language.h>
53 #include <cups/i18n.h>
54 #include <cups/string.h>
62 static char *password
= NULL
; /* Password for device URI */
64 static char pstmpname
[1024] = ""; /* Temporary PostScript file name */
65 #endif /* __APPLE__ */
66 static char tmpfilename
[1024] = ""; /* Temporary spool file name */
67 static int job_cancelled
= 0; /* Job cancelled? */
74 static void cancel_job(http_t
*http
, const char *uri
, int id
,
75 const char *resource
, const char *user
, int version
);
76 static void check_printer_state(http_t
*http
, const char *uri
,
77 const char *resource
, const char *user
,
80 static void compress_files(int num_files
, char **files
);
81 #endif /* HAVE_LIBZ */
82 static const char *password_cb(const char *);
83 static int report_printer_state(ipp_t
*ipp
);
86 static int run_pictwps_filter(char **argv
, const char *filename
);
87 #endif /* __APPLE__ */
88 static void sigterm_handler(int sig
);
92 * 'main()' - Send a file to the printer or server.
96 * printer-uri job-id user title copies options [file]
99 int /* O - Exit status */
100 main(int argc
, /* I - Number of command-line args */
101 char *argv
[]) /* I - Command-line arguments */
103 int i
; /* Looping var */
104 int num_options
; /* Number of printer options */
105 cups_option_t
*options
; /* Printer options */
106 char method
[255], /* Method in URI */
107 hostname
[1024], /* Hostname */
108 username
[255], /* Username info */
109 resource
[1024], /* Resource info (printer name) */
110 addrname
[256], /* Address name */
111 *optptr
, /* Pointer to URI options */
112 name
[255], /* Name of option */
113 value
[255], /* Value of option */
114 *ptr
; /* Pointer into name or value */
115 int num_files
; /* Number of files to print */
116 char **files
, /* Files to print */
117 *filename
; /* Pointer to single filename */
118 int port
; /* Port number (not used) */
119 char uri
[HTTP_MAX_URI
]; /* Updated URI without user/pass */
120 ipp_status_t ipp_status
; /* Status of IPP request */
121 http_t
*http
; /* HTTP connection */
122 ipp_t
*request
, /* IPP request */
123 *response
, /* IPP response */
124 *supported
; /* get-printer-attributes response */
125 time_t start_time
; /* Time of first connect */
126 int recoverable
; /* Recoverable error shown? */
127 int contimeout
; /* Connection timeout */
128 int delay
; /* Delay for retries... */
129 int compression
, /* Do compression of the job data? */
130 waitjob
, /* Wait for job complete? */
131 waitprinter
; /* Wait for printer ready? */
132 ipp_attribute_t
*job_id_attr
; /* job-id attribute */
133 int job_id
; /* job-id value */
134 ipp_attribute_t
*job_sheets
; /* job-media-sheets-completed */
135 ipp_attribute_t
*job_state
; /* job-state */
136 ipp_attribute_t
*copies_sup
; /* copies-supported */
137 ipp_attribute_t
*format_sup
; /* document-format-supported */
138 ipp_attribute_t
*printer_state
; /* printer-state attribute */
139 ipp_attribute_t
*printer_accepting
; /* printer-is-accepting-jobs */
140 int copies
, /* Number of copies for job */
141 copies_remaining
; /* Number of copies remaining */
142 const char *content_type
; /* CONTENT_TYPE environment variable */
143 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
144 struct sigaction action
; /* Actions for POSIX signals */
145 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
146 int version
; /* IPP version */
147 static const char * const pattrs
[] =
148 { /* Printer attributes we want */
150 "document-format-supported",
151 "printer-is-accepting-jobs",
153 "printer-state-reasons",
155 static const char * const jattrs
[] =
156 { /* Job attributes we want */
157 "job-media-sheets-completed",
163 * Make sure status messages are not buffered...
166 setbuf(stderr
, NULL
);
169 * Ignore SIGPIPE and catch SIGTERM signals...
173 sigset(SIGPIPE
, SIG_IGN
);
174 sigset(SIGTERM
, sigterm_handler
);
175 #elif defined(HAVE_SIGACTION)
176 memset(&action
, 0, sizeof(action
));
177 action
.sa_handler
= SIG_IGN
;
178 sigaction(SIGPIPE
, &action
, NULL
);
180 sigemptyset(&action
.sa_mask
);
181 sigaddset(&action
.sa_mask
, SIGTERM
);
182 action
.sa_handler
= sigterm_handler
;
183 sigaction(SIGTERM
, &action
, NULL
);
185 signal(SIGPIPE
, SIG_IGN
);
186 signal(SIGTERM
, sigterm_handler
);
187 #endif /* HAVE_SIGSET */
190 * Check command-line...
197 if ((s
= strrchr(argv
[0], '/')) != NULL
)
202 printf("network %s \"Unknown\" \"Internet Printing Protocol (%s)\"\n",
204 return (CUPS_BACKEND_OK
);
208 fprintf(stderr
, _("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("FINAL_CONTENT_TYPE")) == NULL
)
218 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
219 content_type
= "application/octet-stream";
221 if (!strncmp(content_type
, "printer/", 8))
222 content_type
= "application/vnd.cups-raw";
225 * Extract the hostname and printer name from the URI...
228 if (httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
229 method
, sizeof(method
), username
, sizeof(username
),
230 hostname
, sizeof(hostname
), &port
,
231 resource
, sizeof(resource
)) < HTTP_URI_OK
)
233 fputs(_("ERROR: Missing device URI on command-line and no "
234 "DEVICE_URI environment variable!\n"), stderr
);
235 return (CUPS_BACKEND_STOP
);
238 if (!strcmp(method
, "https"))
239 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
241 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
244 * See if there are any options...
251 contimeout
= 7 * 24 * 60 * 60;
253 if ((optptr
= strchr(resource
, '?')) != NULL
)
256 * Yup, terminate the device name string and move to the first
257 * character of the optptr...
263 * Then parse the optptr...
272 for (ptr
= name
; *optptr
&& *optptr
!= '=';)
273 if (ptr
< (name
+ sizeof(name
) - 1))
285 for (ptr
= value
; *optptr
&& *optptr
!= '+' && *optptr
!= '&';)
286 if (ptr
< (value
+ sizeof(value
) - 1))
290 if (*optptr
== '+' || *optptr
== '&')
297 * Process the option...
300 if (!strcasecmp(name
, "waitjob"))
303 * Wait for job completion?
306 waitjob
= !strcasecmp(value
, "on") ||
307 !strcasecmp(value
, "yes") ||
308 !strcasecmp(value
, "true");
310 else if (!strcasecmp(name
, "waitprinter"))
313 * Wait for printer idle?
316 waitprinter
= !strcasecmp(value
, "on") ||
317 !strcasecmp(value
, "yes") ||
318 !strcasecmp(value
, "true");
320 else if (!strcasecmp(name
, "encryption"))
323 * Enable/disable encryption?
326 if (!strcasecmp(value
, "always"))
327 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
328 else if (!strcasecmp(value
, "required"))
329 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
330 else if (!strcasecmp(value
, "never"))
331 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
332 else if (!strcasecmp(value
, "ifrequested"))
333 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
337 _("ERROR: Unknown encryption option value \"%s\"!\n"),
341 else if (!strcasecmp(name
, "version"))
343 if (!strcmp(value
, "1.0"))
345 else if (!strcmp(value
, "1.1"))
350 _("ERROR: Unknown version option value \"%s\"!\n"),
355 else if (!strcasecmp(name
, "compression"))
357 compression
= !strcasecmp(value
, "true") ||
358 !strcasecmp(value
, "yes") ||
359 !strcasecmp(value
, "on") ||
360 !strcasecmp(value
, "gzip");
362 #endif /* HAVE_LIBZ */
363 else if (!strcasecmp(name
, "contimeout"))
366 * Set the connection timeout...
370 contimeout
= atoi(value
);
378 fprintf(stderr
, _("ERROR: Unknown option \"%s\" with value \"%s\"!\n"),
385 * If we have 7 arguments, print the file named on the command-line.
386 * Otherwise, copy stdin to a temporary file and print the temporary
393 * Copy stdin to a temporary file...
396 int fd
; /* File descriptor */
397 cups_file_t
*fp
; /* Temporary file */
398 char buffer
[8192]; /* Buffer for copying */
399 int bytes
; /* Number of bytes read */
402 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
404 perror("ERROR: unable to create temporary file");
405 return (CUPS_BACKEND_FAILED
);
408 if ((fp
= cupsFileOpenFd(fd
, compression
? "w9" : "w")) == NULL
)
410 perror("ERROR: unable to open temporary file");
413 return (CUPS_BACKEND_FAILED
);
416 while ((bytes
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0)
417 if (cupsFileWrite(fp
, buffer
, bytes
) < bytes
)
419 perror("ERROR: unable to write to temporary file");
422 return (CUPS_BACKEND_FAILED
);
428 * Point to the single file from stdin...
431 filename
= tmpfilename
;
438 * Point to the files on the command-line...
441 num_files
= argc
- 6;
446 compress_files(num_files
, files
);
447 #endif /* HAVE_LIBZ */
450 fprintf(stderr
, "DEBUG: %d files to send in job...\n", num_files
);
453 * Set the authentication info, if any...
456 cupsSetPasswordCB(password_cb
);
461 * Use authenticaion information in the device URI...
464 if ((password
= strchr(username
, ':')) != NULL
)
467 cupsSetUser(username
);
472 * Try loading authentication information from the a##### file.
475 const char *request_root
; /* CUPS_REQUESTROOT env var */
476 char afilename
[1024], /* a##### filename */
477 aline
[2048]; /* Line from file */
478 FILE *fp
; /* File pointer */
481 if ((request_root
= getenv("CUPS_REQUESTROOT")) != NULL
)
484 * Try opening authentication cache file...
487 snprintf(afilename
, sizeof(afilename
), "%s/a%05d", request_root
,
489 if ((fp
= fopen(afilename
, "r")) != NULL
)
495 if (fgets(aline
, sizeof(aline
), fp
))
501 i
= sizeof(username
);
502 httpDecode64_2(username
, &i
, aline
);
508 if (fgets(aline
, sizeof(aline
), fp
))
514 i
= sizeof(password
);
515 httpDecode64_2(password
, &i
, aline
);
529 * Try connecting to the remote server...
534 start_time
= time(NULL
);
536 fputs("STATE: +connecting-to-device\n", stderr
);
540 fprintf(stderr
, _("INFO: Connecting to %s on port %d...\n"),
543 if ((http
= httpConnectEncrypt(hostname
, port
, cupsEncryption())) == NULL
)
548 if (getenv("CLASS") != NULL
)
551 * If the CLASS environment variable is set, the job was submitted
552 * to a class and not to a specific queue. In this case, we want
553 * to abort immediately so that the job can be requeued on the next
554 * available printer in the class.
557 fputs(_("INFO: Unable to contact printer, queuing on next "
558 "printer in class...\n"), stderr
);
560 if (argc
== 6 || strcmp(filename
, argv
[6]))
564 * Sleep 5 seconds to keep the job from requeuing too rapidly...
569 return (CUPS_BACKEND_FAILED
);
572 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
573 errno
== EHOSTUNREACH
)
575 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
577 fputs(_("ERROR: Printer not responding!\n"), stderr
);
578 return (CUPS_BACKEND_FAILED
);
584 _("WARNING: recoverable: Network host \'%s\' is busy; will "
585 "retry in %d seconds...\n"),
595 fprintf(stderr
, _("ERROR: Unable to locate printer \'%s\'!\n"),
597 return (CUPS_BACKEND_STOP
);
603 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
604 fputs(_("ERROR: recoverable: Unable to connect to printer; will "
605 "retry in 30 seconds...\n"), stderr
);
613 while (http
== NULL
);
617 if (argc
== 6 || strcmp(filename
, argv
[6]))
620 return (CUPS_BACKEND_FAILED
);
623 fputs("STATE: -connecting-to-device\n", stderr
);
624 fprintf(stderr
, _("INFO: Connected to %s...\n"), hostname
);
627 if (http
->hostaddr
->addr
.sa_family
== AF_INET6
)
628 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
629 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
630 ntohs(http
->hostaddr
->ipv6
.sin6_port
));
632 #endif /* AF_INET6 */
633 if (http
->hostaddr
->addr
.sa_family
== AF_INET
)
634 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4)...\n",
635 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
636 ntohs(http
->hostaddr
->ipv4
.sin_port
));
639 * Build a URI for the printer and fill the standard IPP attributes for
640 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
641 * might contain username:password information...
644 snprintf(uri
, sizeof(uri
), "%s://%s:%d%s", method
, hostname
, port
, resource
);
647 * First validate the destination and see if the device supports multiple
648 * copies. We have to do this because some IPP servers (e.g. HP JetDirect)
649 * don't support the copies attribute...
659 * Build the IPP request...
662 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
663 request
->request
.op
.version
[1] = version
;
665 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
668 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
669 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
676 fputs("DEBUG: Getting supported attributes...\n", stderr
);
678 if ((supported
= cupsDoRequest(http
, request
, resource
)) == NULL
)
679 ipp_status
= cupsLastError();
681 ipp_status
= supported
->request
.status
.status_code
;
683 if (ipp_status
> IPP_OK_CONFLICT
)
685 if (ipp_status
== IPP_PRINTER_BUSY
||
686 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
688 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
690 fputs(_("ERROR: Printer not responding!\n"), stderr
);
691 return (CUPS_BACKEND_FAILED
);
697 _("WARNING: recoverable: Network host \'%s\' is busy; will "
698 "retry in %d seconds...\n"),
701 report_printer_state(supported
);
708 else if ((ipp_status
== IPP_BAD_REQUEST
||
709 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
== 1)
712 * Switch to IPP/1.0...
715 fputs(_("INFO: Printer does not support IPP/1.1, trying IPP/1.0...\n"),
720 else if (ipp_status
== IPP_NOT_FOUND
)
722 fputs(_("ERROR: Destination printer does not exist!\n"), stderr
);
725 ippDelete(supported
);
727 return (CUPS_BACKEND_STOP
);
731 fprintf(stderr
, _("ERROR: Unable to get printer status (%s)!\n"),
732 cupsLastErrorString());
737 ippDelete(supported
);
741 else if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
742 IPP_TAG_RANGE
)) != NULL
)
745 * Has the "copies-supported" attribute - does it have an upper
749 if (copies_sup
->values
[0].range
.upper
<= 1)
750 copies_sup
= NULL
; /* No */
753 format_sup
= ippFindAttribute(supported
, "document-format-supported",
758 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
759 format_sup
->num_values
);
760 for (i
= 0; i
< format_sup
->num_values
; i
++)
761 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
762 format_sup
->values
[i
].string
.text
);
765 report_printer_state(supported
);
767 while (ipp_status
> IPP_OK_CONFLICT
);
770 * See if the printer is accepting jobs and is not stopped; if either
771 * condition is true and we are printing to a class, requeue the job...
774 if (getenv("CLASS") != NULL
)
776 printer_state
= ippFindAttribute(supported
, "printer-state",
778 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
781 if (printer_state
== NULL
||
782 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
784 printer_accepting
== NULL
||
785 !printer_accepting
->values
[0].boolean
)
788 * If the CLASS environment variable is set, the job was submitted
789 * to a class and not to a specific queue. In this case, we want
790 * to abort immediately so that the job can be requeued on the next
791 * available printer in the class.
794 fputs(_("INFO: Unable to contact printer, queuing on next "
795 "printer in class...\n"), stderr
);
797 ippDelete(supported
);
800 if (argc
== 6 || strcmp(filename
, argv
[6]))
804 * Sleep 5 seconds to keep the job from requeuing too rapidly...
809 return (CUPS_BACKEND_FAILED
);
816 * If we've shown a recoverable error make sure the printer proxies
817 * have a chance to see the recovered message. Not pretty but
818 * necessary for now...
821 fputs("INFO: recovered: \n", stderr
);
826 * See if the printer supports multiple copies...
829 copies
= atoi(argv
[4]);
831 if (copies_sup
|| argc
< 7)
833 copies_remaining
= 1;
839 copies_remaining
= copies
;
842 * Then issue the print-job request...
847 while (copies_remaining
> 0)
850 * Build the IPP request...
857 request
= ippNewRequest(IPP_CREATE_JOB
);
859 request
= ippNewRequest(IPP_PRINT_JOB
);
861 request
->request
.op
.version
[1] = version
;
863 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
866 fprintf(stderr
, "DEBUG: printer-uri = \"%s\"\n", uri
);
869 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
870 "requesting-user-name", NULL
, argv
[2]);
872 fprintf(stderr
, "DEBUG: requesting-user-name = \"%s\"\n", argv
[2]);
875 * Only add a "job-name" attribute if the remote server supports
876 * copy generation - some IPP implementations like HP's don't seem
877 * to like UTF-8 job names (STR #1837)...
880 if (argv
[3][0] && copies_sup
)
881 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
884 fprintf(stderr
, "DEBUG: job-name = \"%s\"\n", argv
[3]);
888 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
889 "compression", NULL
, "gzip");
890 #endif /* HAVE_LIBZ */
893 * Handle options on the command-line...
897 num_options
= cupsParseOptions(argv
[5], 0, &options
);
900 if (content_type
!= NULL
&&
901 !strcasecmp(content_type
, "application/pictwps") && num_files
== 1)
903 if (format_sup
!= NULL
)
905 for (i
= 0; i
< format_sup
->num_values
; i
++)
906 if (!strcasecmp(content_type
, format_sup
->values
[i
].string
.text
))
910 if (format_sup
== NULL
|| i
>= format_sup
->num_values
)
913 * Remote doesn't support "application/pictwps" (i.e. it's not MacOS X)
914 * so convert the document to PostScript...
917 if (run_pictwps_filter(argv
, filename
))
918 return (CUPS_BACKEND_FAILED
);
920 filename
= pstmpname
;
923 * Change the MIME type to application/postscript and change the
924 * number of copies to 1...
927 content_type
= "application/postscript";
929 copies_remaining
= 1;
932 #endif /* __APPLE__ */
934 if (content_type
!= NULL
&& format_sup
!= NULL
)
936 for (i
= 0; i
< format_sup
->num_values
; i
++)
937 if (!strcasecmp(content_type
, format_sup
->values
[i
].string
.text
))
940 if (i
< format_sup
->num_values
)
941 num_options
= cupsAddOption("document-format", content_type
,
942 num_options
, &options
);
945 if (copies_sup
&& version
> 0)
948 * Only send options if the destination printer supports the copies
949 * attribute and IPP/1.1. This is a hack for the HP and Lexmark
950 * implementations of IPP, which do not accept extension attributes
951 * and incorrectly report a client-error-bad-request error instead of
952 * the successful-ok-unsupported-attributes status. In short, at least
953 * some HP and Lexmark implementations of IPP are non-compliant.
956 cupsEncodeOptions(request
, num_options
, options
);
958 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies",
962 cupsFreeOptions(num_options
, options
);
965 * If copies aren't supported, then we are likely dealing with an HP
966 * JetDirect. The HP IPP implementation seems to close the connection
967 * after every request - that is, it does *not* implement HTTP Keep-
968 * Alive, which is REQUIRED by HTTP/1.1...
979 response
= cupsDoRequest(http
, request
, resource
);
981 response
= cupsDoFileRequest(http
, request
, resource
, files
[0]);
983 ipp_status
= cupsLastError();
985 if (ipp_status
> IPP_OK_CONFLICT
)
992 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
993 ipp_status
== IPP_PRINTER_BUSY
)
995 fputs(_("INFO: Printer busy; will retry in 10 seconds...\n"), stderr
);
998 else if ((ipp_status
== IPP_BAD_REQUEST
||
999 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
== 1)
1002 * Switch to IPP/1.0...
1005 fputs(_("INFO: Printer does not support IPP/1.1, trying IPP/1.0...\n"),
1008 httpReconnect(http
);
1011 fprintf(stderr
, _("ERROR: Print file was not accepted (%s)!\n"),
1012 cupsLastErrorString());
1014 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
1015 IPP_TAG_INTEGER
)) == NULL
)
1017 fputs(_("NOTICE: Print file accepted - job ID unknown.\n"), stderr
);
1022 job_id
= job_id_attr
->values
[0].integer
;
1023 fprintf(stderr
, _("NOTICE: Print file accepted - job ID %d.\n"), job_id
);
1026 ippDelete(response
);
1031 if (job_id
&& num_files
> 1)
1033 for (i
= 0; i
< num_files
; i
++)
1035 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
1037 request
->request
.op
.version
[1] = version
;
1039 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1042 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1046 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1047 "requesting-user-name", NULL
, argv
[2]);
1049 if ((i
+ 1) == num_files
)
1050 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
1052 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1053 "document-format", NULL
, content_type
);
1055 ippDelete(cupsDoFileRequest(http
, request
, resource
, files
[i
]));
1057 if (cupsLastError() > IPP_OK_CONFLICT
)
1059 ipp_status
= cupsLastError();
1061 fprintf(stderr
, _("ERROR: Unable to add file %d to job: %s\n"),
1062 job_id
, cupsLastErrorString());
1068 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
1070 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
1071 copies_remaining
--;
1073 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1074 ipp_status
== IPP_PRINTER_BUSY
)
1077 copies_remaining
--;
1080 * Wait for the job to complete...
1083 if (!job_id
|| !waitjob
)
1086 fputs(_("INFO: Waiting for job to complete...\n"), stderr
);
1088 for (; !job_cancelled
;)
1091 * Build an IPP_GET_JOB_ATTRIBUTES request...
1094 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1095 request
->request
.op
.version
[1] = version
;
1097 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1100 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1104 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1105 "requesting-user-name", NULL
, argv
[2]);
1107 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1108 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
1116 httpReconnect(http
);
1118 response
= cupsDoRequest(http
, request
, resource
);
1119 ipp_status
= cupsLastError();
1121 if (ipp_status
== IPP_NOT_FOUND
)
1124 * Job has gone away and/or the server has no job history...
1127 ippDelete(response
);
1129 ipp_status
= IPP_OK
;
1133 if (ipp_status
> IPP_OK_CONFLICT
)
1135 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1136 ipp_status
!= IPP_PRINTER_BUSY
)
1138 ippDelete(response
);
1140 fprintf(stderr
, _("ERROR: Unable to get job %d attributes (%s)!\n"),
1141 job_id
, cupsLastErrorString());
1148 if ((job_state
= ippFindAttribute(response
, "job-state",
1149 IPP_TAG_ENUM
)) != NULL
)
1152 * Stop polling if the job is finished or pending-held...
1155 if (job_state
->values
[0].integer
> IPP_JOB_PROCESSING
||
1156 job_state
->values
[0].integer
== IPP_JOB_HELD
)
1158 if ((job_sheets
= ippFindAttribute(response
,
1159 "job-media-sheets-completed",
1160 IPP_TAG_INTEGER
)) != NULL
)
1161 fprintf(stderr
, "PAGE: total %d\n",
1162 job_sheets
->values
[0].integer
);
1164 ippDelete(response
);
1170 ippDelete(response
);
1173 * Check the printer state and report it if necessary...
1176 check_printer_state(http
, uri
, resource
, argv
[2], version
);
1179 * Wait 10 seconds before polling again...
1187 * Cancel the job as needed...
1190 if (job_cancelled
&& job_id
)
1191 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1194 * Check the printer state and report it if necessary...
1197 check_printer_state(http
, uri
, resource
, argv
[2], version
);
1205 ippDelete(supported
);
1208 * Remove the temporary file(s) if necessary...
1212 unlink(tmpfilename
);
1217 for (i
= 0; i
< num_files
; i
++)
1220 #endif /* HAVE_LIBZ */
1225 #endif /* __APPLE__ */
1228 * Return the queue status...
1231 return (ipp_status
> IPP_OK_CONFLICT
? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
1236 * 'cancel_job()' - Cancel a print job.
1240 cancel_job(http_t
*http
, /* I - HTTP connection */
1241 const char *uri
, /* I - printer-uri */
1242 int id
, /* I - job-id */
1243 const char *resource
, /* I - Resource path */
1244 const char *user
, /* I - requesting-user-name */
1245 int version
) /* I - IPP version */
1247 ipp_t
*request
; /* Cancel-Job request */
1250 fputs(_("INFO: Canceling print job...\n"), stderr
);
1252 request
= ippNewRequest(IPP_CANCEL_JOB
);
1253 request
->request
.op
.version
[1] = version
;
1255 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1257 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1259 if (user
&& user
[0])
1260 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1261 "requesting-user-name", NULL
, user
);
1267 ippDelete(cupsDoRequest(http
, request
, resource
));
1269 if (cupsLastError() > IPP_OK_CONFLICT
)
1270 fprintf(stderr
, _("ERROR: Unable to cancel job %d: %s\n"), id
,
1271 cupsLastErrorString());
1276 * 'check_printer_state()' - Check the printer state...
1280 check_printer_state(
1281 http_t
*http
, /* I - HTTP connection */
1282 const char *uri
, /* I - Printer URI */
1283 const char *resource
, /* I - Resource path */
1284 const char *user
, /* I - Username, if any */
1285 int version
) /* I - IPP version */
1287 ipp_t
*request
, /* IPP request */
1288 *response
; /* IPP response */
1292 * Check on the printer state...
1295 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1296 request
->request
.op
.version
[1] = version
;
1298 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1301 if (user
&& user
[0])
1302 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1303 "requesting-user-name", NULL
, user
);
1305 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1306 "requested-attributes", NULL
, "printer-state-reasons");
1312 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
1314 report_printer_state(response
);
1315 ippDelete(response
);
1322 * 'compress_files()' - Compress print files...
1326 compress_files(int num_files
, /* I - Number of files */
1327 char **files
) /* I - Files */
1329 int i
, /* Looping var */
1330 fd
; /* Temporary file descriptor */
1331 ssize_t bytes
; /* Bytes read/written */
1332 size_t total
; /* Total bytes read */
1333 cups_file_t
*in
, /* Input file */
1334 *out
; /* Output file */
1335 struct stat outinfo
; /* Output file information */
1336 char filename
[1024], /* Temporary filename */
1337 buffer
[32768]; /* Copy buffer */
1340 fprintf(stderr
, "DEBUG: Compressing %d job files...\n", num_files
);
1341 for (i
= 0; i
< num_files
; i
++)
1343 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
1346 _("ERROR: Unable to create temporary compressed print file: %s\n"),
1348 exit(CUPS_BACKEND_FAILED
);
1351 if ((out
= cupsFileOpenFd(fd
, "w9")) == NULL
)
1354 _("ERROR: Unable to open temporary compressed print file: %s\n"),
1356 exit(CUPS_BACKEND_FAILED
);
1359 if ((in
= cupsFileOpen(files
[i
], "r")) == NULL
)
1361 fprintf(stderr
, _("ERROR: Unable to open print file \"%s\": %s\n"),
1362 files
[i
], strerror(errno
));
1364 exit(CUPS_BACKEND_FAILED
);
1368 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
1369 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
1371 fprintf(stderr
, _("ERROR: Unable to write %d bytes to \"%s\": %s\n"),
1372 (int)bytes
, filename
, strerror(errno
));
1375 exit(CUPS_BACKEND_FAILED
);
1383 files
[i
] = strdup(filename
);
1385 if (!stat(filename
, &outinfo
))
1387 "DEBUG: File %d compressed to %.1f%% of original size, "
1388 CUPS_LLFMT
" bytes...\n",
1389 i
+ 1, 100.0 * outinfo
.st_size
/ total
,
1390 CUPS_LLCAST outinfo
.st_size
);
1393 #endif /* HAVE_LIBZ */
1397 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
1400 static const char * /* O - Password */
1401 password_cb(const char *prompt
) /* I - Prompt (not used) */
1410 * If there is no password set in the device URI, return the
1411 * "authentication required" exit code...
1415 unlink(tmpfilename
);
1420 #endif /* __APPLE__ */
1422 fputs("ATTR: auth-info-required=username,password\n", stderr
);
1424 exit(CUPS_BACKEND_AUTH_REQUIRED
);
1426 return (NULL
); /* Eliminate compiler warning */
1432 * 'report_printer_state()' - Report the printer state.
1435 static int /* O - Number of reasons shown */
1436 report_printer_state(ipp_t
*ipp
) /* I - IPP response */
1438 int i
; /* Looping var */
1439 int count
; /* Count of reasons shown... */
1440 ipp_attribute_t
*reasons
; /* printer-state-reasons */
1441 const char *reason
; /* Current reason */
1442 const char *message
; /* Message to show */
1443 char unknown
[1024]; /* Unknown message string */
1444 const char *prefix
; /* Prefix for STATE: line */
1445 char state
[1024]; /* State string */
1448 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
1449 IPP_TAG_KEYWORD
)) == NULL
)
1455 for (i
= 0, count
= 0; i
< reasons
->num_values
; i
++)
1457 reason
= reasons
->values
[i
].string
.text
;
1459 strlcat(state
, prefix
, sizeof(state
));
1460 strlcat(state
, reason
, sizeof(state
));
1465 if (!strncmp(reason
, "media-needed", 12))
1466 message
= _("Media tray needs to be filled.");
1467 else if (!strncmp(reason
, "media-jam", 9))
1468 message
= _("Media jam!");
1469 else if (!strncmp(reason
, "moving-to-paused", 16) ||
1470 !strncmp(reason
, "paused", 6) ||
1471 !strncmp(reason
, "shutdown", 8))
1472 message
= _("Printer off-line.");
1473 else if (!strncmp(reason
, "toner-low", 9))
1474 message
= _("Toner low.");
1475 else if (!strncmp(reason
, "toner-empty", 11))
1476 message
= _("Out of toner!");
1477 else if (!strncmp(reason
, "cover-open", 10))
1478 message
= _("Cover open.");
1479 else if (!strncmp(reason
, "interlock-open", 14))
1480 message
= _("Interlock open.");
1481 else if (!strncmp(reason
, "door-open", 9))
1482 message
= _("Door open.");
1483 else if (!strncmp(reason
, "input-tray-missing", 18))
1484 message
= _("Media tray missing!");
1485 else if (!strncmp(reason
, "media-low", 9))
1486 message
= _("Media tray almost empty.");
1487 else if (!strncmp(reason
, "media-empty", 11))
1488 message
= _("Media tray empty!");
1489 else if (!strncmp(reason
, "output-tray-missing", 19))
1490 message
= _("Output tray missing!");
1491 else if (!strncmp(reason
, "output-area-almost-full", 23))
1492 message
= _("Output bin almost full.");
1493 else if (!strncmp(reason
, "output-area-full", 16))
1494 message
= _("Output bin full!");
1495 else if (!strncmp(reason
, "marker-supply-low", 17))
1496 message
= _("Ink/toner almost empty.");
1497 else if (!strncmp(reason
, "marker-supply-empty", 19))
1498 message
= _("Ink/toner empty!");
1499 else if (!strncmp(reason
, "marker-waste-almost-full", 24))
1500 message
= _("Ink/toner waste bin almost full.");
1501 else if (!strncmp(reason
, "marker-waste-full", 17))
1502 message
= _("Ink/toner waste bin full!");
1503 else if (!strncmp(reason
, "fuser-over-temp", 15))
1504 message
= _("Fuser temperature high!");
1505 else if (!strncmp(reason
, "fuser-under-temp", 16))
1506 message
= _("Fuser temperature low!");
1507 else if (!strncmp(reason
, "opc-near-eol", 12))
1508 message
= _("OPC almost at end-of-life.");
1509 else if (!strncmp(reason
, "opc-life-over", 13))
1510 message
= _("OPC at end-of-life!");
1511 else if (!strncmp(reason
, "developer-low", 13))
1512 message
= _("Developer almost empty.");
1513 else if (!strncmp(reason
, "developer-empty", 15))
1514 message
= _("Developer empty!");
1515 else if (strstr(reason
, "error") != NULL
)
1519 snprintf(unknown
, sizeof(unknown
), _("Unknown printer error (%s)!"),
1526 if (strstr(reasons
->values
[i
].string
.text
, "error"))
1527 fprintf(stderr
, "ERROR: %s\n", message
);
1528 else if (strstr(reasons
->values
[i
].string
.text
, "warning"))
1529 fprintf(stderr
, "WARNING: %s\n", message
);
1531 fprintf(stderr
, "INFO: %s\n", message
);
1535 fprintf(stderr
, "%s\n", state
);
1543 * 'run_pictwps_filter()' - Convert PICT files to PostScript when printing
1546 * This step is required because the PICT format is not documented and
1547 * subject to change, so developing a filter for other OS's is infeasible.
1548 * Also, fonts required by the PICT file need to be embedded on the
1549 * client side (which has the fonts), so we run the filter to get a
1550 * PostScript file for printing...
1553 static int /* O - Exit status of filter */
1554 run_pictwps_filter(char **argv
, /* I - Command-line arguments */
1555 const char *filename
)/* I - Filename */
1557 struct stat fileinfo
; /* Print file information */
1558 const char *ppdfile
; /* PPD file for destination printer */
1559 int pid
; /* Child process ID */
1560 int fd
; /* Temporary file descriptor */
1561 int status
; /* Exit status of filter */
1562 const char *printer
; /* PRINTER env var */
1563 static char ppdenv
[1024]; /* PPD environment variable */
1567 * First get the PPD file for the printer...
1570 printer
= getenv("PRINTER");
1573 fputs(_("ERROR: PRINTER environment variable not defined!\n"), stderr
);
1577 if ((ppdfile
= cupsGetPPD(printer
)) == NULL
)
1580 _("ERROR: Unable to get PPD file for printer \"%s\" - %s.\n"),
1581 printer
, cupsLastErrorString());
1585 snprintf(ppdenv
, sizeof(ppdenv
), "PPD=%s", ppdfile
);
1590 * Then create a temporary file for printing...
1593 if ((fd
= cupsTempFd(pstmpname
, sizeof(pstmpname
))) < 0)
1595 fprintf(stderr
, _("ERROR: Unable to create temporary file - %s.\n"),
1603 * Get the owner of the spool file - it is owned by the user we want to run
1608 stat(argv
[6], &fileinfo
);
1612 * Use the OSX defaults, as an up-stream filter created the PICT
1616 fileinfo
.st_uid
= 1;
1617 fileinfo
.st_gid
= 80;
1621 chown(ppdfile
, fileinfo
.st_uid
, fileinfo
.st_gid
);
1623 fchown(fd
, fileinfo
.st_uid
, fileinfo
.st_gid
);
1626 * Finally, run the filter to convert the file...
1629 if ((pid
= fork()) == 0)
1632 * Child process for pictwpstops... Redirect output of pictwpstops to a
1643 * Change to an unpriviledged user...
1646 setgid(fileinfo
.st_gid
);
1647 setuid(fileinfo
.st_uid
);
1650 execlp("pictwpstops", printer
, argv
[1], argv
[2], argv
[3], argv
[4], argv
[5],
1652 fprintf(stderr
, _("ERROR: Unable to exec pictwpstops: %s\n"),
1665 fprintf(stderr
, _("ERROR: Unable to fork pictwpstops: %s\n"),
1674 * Now wait for the filter to complete...
1677 if (wait(&status
) < 0)
1679 fprintf(stderr
, _("ERROR: Unable to wait for pictwpstops: %s\n"),
1696 fprintf(stderr
, _("ERROR: pictwpstops exited with status %d!\n"),
1699 fprintf(stderr
, _("ERROR: pictwpstops exited on signal %d!\n"),
1707 * Return with no errors..
1712 #endif /* __APPLE__ */
1716 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1720 sigterm_handler(int sig
) /* I - Signal */
1722 (void)sig
; /* remove compiler warnings... */
1727 * Flag that the job should be cancelled...
1735 * The scheduler already tried to cancel us once, now just terminate
1736 * after removing our temp files!
1740 unlink(tmpfilename
);
1745 #endif /* __APPLE__ */
1752 * End of "$Id: ipp.c 6422 2007-03-30 20:49:37Z mike $".