2 * "$Id: ipp.c 6214 2007-01-23 17:01:48Z mike $"
4 * IPP backend for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 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/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 int compression
, /* Do compression of the job data? */
126 waitjob
, /* Wait for job complete? */
127 waitprinter
; /* Wait for printer ready? */
128 ipp_attribute_t
*job_id_attr
; /* job-id attribute */
129 int job_id
; /* job-id value */
130 ipp_attribute_t
*job_sheets
; /* job-media-sheets-completed */
131 ipp_attribute_t
*job_state
; /* job-state */
132 ipp_attribute_t
*copies_sup
; /* copies-supported */
133 ipp_attribute_t
*format_sup
; /* document-format-supported */
134 ipp_attribute_t
*printer_state
; /* printer-state attribute */
135 ipp_attribute_t
*printer_accepting
; /* printer-is-accepting-jobs */
136 int copies
, /* Number of copies for job */
137 copies_remaining
; /* Number of copies remaining */
138 const char *content_type
; /* CONTENT_TYPE environment variable */
139 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
140 struct sigaction action
; /* Actions for POSIX signals */
141 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
142 int version
; /* IPP version */
143 static const char * const pattrs
[] =
144 { /* Printer attributes we want */
146 "document-format-supported",
147 "printer-is-accepting-jobs",
149 "printer-state-reasons",
151 static const char * const jattrs
[] =
152 { /* Job attributes we want */
153 "job-media-sheets-completed",
159 * Make sure status messages are not buffered...
162 setbuf(stderr
, NULL
);
165 * Ignore SIGPIPE and catch SIGTERM signals...
169 sigset(SIGPIPE
, SIG_IGN
);
170 sigset(SIGTERM
, sigterm_handler
);
171 #elif defined(HAVE_SIGACTION)
172 memset(&action
, 0, sizeof(action
));
173 action
.sa_handler
= SIG_IGN
;
174 sigaction(SIGPIPE
, &action
, NULL
);
176 sigemptyset(&action
.sa_mask
);
177 sigaddset(&action
.sa_mask
, SIGTERM
);
178 action
.sa_handler
= sigterm_handler
;
179 sigaction(SIGTERM
, &action
, NULL
);
181 signal(SIGPIPE
, SIG_IGN
);
182 signal(SIGTERM
, sigterm_handler
);
183 #endif /* HAVE_SIGSET */
186 * Check command-line...
193 if ((s
= strrchr(argv
[0], '/')) != NULL
)
198 printf("network %s \"Unknown\" \"Internet Printing Protocol (%s)\"\n",
200 return (CUPS_BACKEND_OK
);
205 "Usage: %s job-id user title copies options [file ... fileN]\n",
207 return (CUPS_BACKEND_STOP
);
211 * Get the (final) content type...
214 if ((content_type
= getenv("FINAL_CONTENT_TYPE")) == NULL
)
215 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
216 content_type
= "application/octet-stream";
218 if (!strncmp(content_type
, "printer/", 8))
219 content_type
= "application/vnd.cups-raw";
222 * Extract the hostname and printer name from the URI...
225 if (httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
226 method
, sizeof(method
), username
, sizeof(username
),
227 hostname
, sizeof(hostname
), &port
,
228 resource
, sizeof(resource
)) < HTTP_URI_OK
)
230 fputs("ERROR: Missing device URI on command-line and no DEVICE_URI "
231 "environment variable!\n", stderr
);
232 return (CUPS_BACKEND_STOP
);
235 if (!strcmp(method
, "https"))
236 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
238 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
241 * See if there are any options...
249 if ((optptr
= strchr(resource
, '?')) != NULL
)
252 * Yup, terminate the device name string and move to the first
253 * character of the optptr...
259 * Then parse the optptr...
268 for (ptr
= name
; *optptr
&& *optptr
!= '=';)
269 if (ptr
< (name
+ sizeof(name
) - 1))
281 for (ptr
= value
; *optptr
&& *optptr
!= '+' && *optptr
!= '&';)
282 if (ptr
< (value
+ sizeof(value
) - 1))
286 if (*optptr
== '+' || *optptr
== '&')
293 * Process the option...
296 if (!strcasecmp(name
, "waitjob"))
299 * Wait for job completion?
302 waitjob
= !strcasecmp(value
, "on") ||
303 !strcasecmp(value
, "yes") ||
304 !strcasecmp(value
, "true");
306 else if (!strcasecmp(name
, "waitprinter"))
309 * Wait for printer idle?
312 waitprinter
= !strcasecmp(value
, "on") ||
313 !strcasecmp(value
, "yes") ||
314 !strcasecmp(value
, "true");
316 else if (!strcasecmp(name
, "encryption"))
319 * Enable/disable encryption?
322 if (!strcasecmp(value
, "always"))
323 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
324 else if (!strcasecmp(value
, "required"))
325 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
326 else if (!strcasecmp(value
, "never"))
327 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
328 else if (!strcasecmp(value
, "ifrequested"))
329 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
332 fprintf(stderr
, "ERROR: Unknown encryption option value \"%s\"!\n",
336 else if (!strcasecmp(name
, "version"))
338 if (!strcmp(value
, "1.0"))
340 else if (!strcmp(value
, "1.1"))
344 fprintf(stderr
, "ERROR: Unknown version option value \"%s\"!\n",
349 else if (!strcasecmp(name
, "compression"))
351 compression
= !strcasecmp(value
, "true") ||
352 !strcasecmp(value
, "yes") ||
353 !strcasecmp(value
, "on") ||
354 !strcasecmp(value
, "gzip");
356 #endif /* HAVE_LIBZ */
363 fprintf(stderr
, "ERROR: Unknown option \"%s\" with value \"%s\"!\n",
370 * If we have 7 arguments, print the file named on the command-line.
371 * Otherwise, copy stdin to a temporary file and print the temporary
378 * Copy stdin to a temporary file...
381 int fd
; /* File descriptor */
382 cups_file_t
*fp
; /* Temporary file */
383 char buffer
[8192]; /* Buffer for copying */
384 int bytes
; /* Number of bytes read */
387 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
389 perror("ERROR: unable to create temporary file");
390 return (CUPS_BACKEND_FAILED
);
393 if ((fp
= cupsFileOpenFd(fd
, compression
? "w9" : "w")) == NULL
)
395 perror("ERROR: unable to open temporary file");
398 return (CUPS_BACKEND_FAILED
);
401 while ((bytes
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0)
402 if (cupsFileWrite(fp
, buffer
, bytes
) < bytes
)
404 perror("ERROR: unable to write to temporary file");
407 return (CUPS_BACKEND_FAILED
);
413 * Point to the single file from stdin...
416 filename
= tmpfilename
;
423 * Point to the files on the command-line...
426 num_files
= argc
- 6;
431 compress_files(num_files
, files
);
432 #endif /* HAVE_LIBZ */
435 fprintf(stderr
, "DEBUG: %d files to send in job...\n", num_files
);
438 * Set the authentication info, if any...
441 cupsSetPasswordCB(password_cb
);
446 * Use authenticaion information in the device URI...
449 if ((password
= strchr(username
, ':')) != NULL
)
452 cupsSetUser(username
);
457 * Try loading authentication information from the a##### file.
460 const char *request_root
; /* CUPS_REQUESTROOT env var */
461 char afilename
[1024], /* a##### filename */
462 aline
[1024]; /* Line from file */
463 FILE *fp
; /* File pointer */
466 if ((request_root
= getenv("CUPS_REQUESTROOT")) != NULL
)
469 * Try opening authentication cache file...
472 snprintf(afilename
, sizeof(afilename
), "%s/a%05d", request_root
,
474 if ((fp
= fopen(afilename
, "r")) != NULL
)
480 if (fgets(aline
, sizeof(aline
), fp
))
486 i
= sizeof(username
);
487 httpDecode64_2(username
, &i
, aline
);
493 if (fgets(aline
, sizeof(aline
), fp
))
499 i
= sizeof(password
);
500 httpDecode64_2(password
, &i
, aline
);
514 * Try connecting to the remote server...
517 fputs("STATE: +connecting-to-device\n", stderr
);
521 fprintf(stderr
, "INFO: Connecting to %s on port %d...\n", hostname
, port
);
523 if ((http
= httpConnectEncrypt(hostname
, port
, cupsEncryption())) == NULL
)
525 if (getenv("CLASS") != NULL
)
528 * If the CLASS environment variable is set, the job was submitted
529 * to a class and not to a specific queue. In this case, we want
530 * to abort immediately so that the job can be requeued on the next
531 * available printer in the class.
535 "INFO: Unable to connect to %s, queuing on next printer in "
539 if (argc
== 6 || strcmp(filename
, argv
[6]))
543 * Sleep 5 seconds to keep the job from requeuing too rapidly...
548 return (CUPS_BACKEND_FAILED
);
551 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
552 errno
== EHOSTUNREACH
)
555 "INFO: Network host \'%s\' is busy; will retry in 30 "
562 fprintf(stderr
, "INFO: Unable to lookup host \'%s\' - %s\n",
563 hostname
, hstrerror(h_errno
));
568 perror("ERROR: Unable to connect to IPP host");
573 while (http
== NULL
);
575 fputs("STATE: -connecting-to-device\n", stderr
);
576 fprintf(stderr
, "INFO: Connected to %s...\n", hostname
);
579 if (http
->hostaddr
->addr
.sa_family
== AF_INET6
)
580 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
581 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
582 ntohs(http
->hostaddr
->ipv6
.sin6_port
));
584 #endif /* AF_INET6 */
585 if (http
->hostaddr
->addr
.sa_family
== AF_INET
)
586 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4)...\n",
587 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
588 ntohs(http
->hostaddr
->ipv4
.sin_port
));
591 * Build a URI for the printer and fill the standard IPP attributes for
592 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
593 * might contain username:password information...
596 snprintf(uri
, sizeof(uri
), "%s://%s:%d%s", method
, hostname
, port
, resource
);
599 * First validate the destination and see if the device supports multiple
600 * copies. We have to do this because some IPP servers (e.g. HP JetDirect)
601 * don't support the copies attribute...
611 * Build the IPP request...
614 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
615 request
->request
.op
.version
[1] = version
;
617 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
620 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
621 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
628 fputs("DEBUG: Getting supported attributes...\n", stderr
);
630 if ((supported
= cupsDoRequest(http
, request
, resource
)) == NULL
)
631 ipp_status
= cupsLastError();
633 ipp_status
= supported
->request
.status
.status_code
;
635 if (ipp_status
> IPP_OK_CONFLICT
)
637 if (ipp_status
== IPP_PRINTER_BUSY
||
638 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
640 fputs("INFO: Printer busy; will retry in 10 seconds...\n", stderr
);
641 report_printer_state(supported
);
644 else if ((ipp_status
== IPP_BAD_REQUEST
||
645 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
== 1)
648 * Switch to IPP/1.0...
651 fputs("INFO: Printer does not support IPP/1.1, trying IPP/1.0...\n",
656 else if (ipp_status
== IPP_NOT_FOUND
)
658 fputs("ERROR: Destination printer does not exist!\n", stderr
);
661 ippDelete(supported
);
663 return (CUPS_BACKEND_STOP
);
667 fprintf(stderr
, "ERROR: Unable to get printer status (%s)!\n",
668 ippErrorString(ipp_status
));
673 ippDelete(supported
);
677 else if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
678 IPP_TAG_RANGE
)) != NULL
)
681 * Has the "copies-supported" attribute - does it have an upper
685 if (copies_sup
->values
[0].range
.upper
<= 1)
686 copies_sup
= NULL
; /* No */
689 format_sup
= ippFindAttribute(supported
, "document-format-supported",
694 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
695 format_sup
->num_values
);
696 for (i
= 0; i
< format_sup
->num_values
; i
++)
697 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
698 format_sup
->values
[i
].string
.text
);
701 report_printer_state(supported
);
703 while (ipp_status
> IPP_OK_CONFLICT
);
706 * See if the printer is accepting jobs and is not stopped; if either
707 * condition is true and we are printing to a class, requeue the job...
710 if (getenv("CLASS") != NULL
)
712 printer_state
= ippFindAttribute(supported
, "printer-state",
714 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
717 if (printer_state
== NULL
||
718 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
720 printer_accepting
== NULL
||
721 !printer_accepting
->values
[0].boolean
)
724 * If the CLASS environment variable is set, the job was submitted
725 * to a class and not to a specific queue. In this case, we want
726 * to abort immediately so that the job can be requeued on the next
727 * available printer in the class.
731 "INFO: Unable to queue job on %s, queuing on next printer in "
735 ippDelete(supported
);
738 if (argc
== 6 || strcmp(filename
, argv
[6]))
742 * Sleep 5 seconds to keep the job from requeuing too rapidly...
747 return (CUPS_BACKEND_FAILED
);
752 * See if the printer supports multiple copies...
755 copies
= atoi(argv
[4]);
757 if (copies_sup
|| argc
< 7)
759 copies_remaining
= 1;
765 copies_remaining
= copies
;
768 * Then issue the print-job request...
773 while (copies_remaining
> 0)
776 * Build the IPP request...
783 request
= ippNewRequest(IPP_CREATE_JOB
);
785 request
= ippNewRequest(IPP_PRINT_JOB
);
787 request
->request
.op
.version
[1] = version
;
789 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
792 fprintf(stderr
, "DEBUG: printer-uri = \"%s\"\n", uri
);
795 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
796 "requesting-user-name", NULL
, argv
[2]);
798 fprintf(stderr
, "DEBUG: requesting-user-name = \"%s\"\n", argv
[2]);
801 * Only add a "job-name" attribute if the remote server supports
802 * copy generation - some IPP implementations like HP's don't seem
803 * to like UTF-8 job names (STR #1837)...
806 if (argv
[3][0] && copies_sup
)
807 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
810 fprintf(stderr
, "DEBUG: job-name = \"%s\"\n", argv
[3]);
814 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
815 "compression", NULL
, "gzip");
816 #endif /* HAVE_LIBZ */
819 * Handle options on the command-line...
823 num_options
= cupsParseOptions(argv
[5], 0, &options
);
826 if (content_type
!= NULL
&&
827 !strcasecmp(content_type
, "application/pictwps") && num_files
== 1)
829 if (format_sup
!= NULL
)
831 for (i
= 0; i
< format_sup
->num_values
; i
++)
832 if (!strcasecmp(content_type
, format_sup
->values
[i
].string
.text
))
836 if (format_sup
== NULL
|| i
>= format_sup
->num_values
)
839 * Remote doesn't support "application/pictwps" (i.e. it's not MacOS X)
840 * so convert the document to PostScript...
843 if (run_pictwps_filter(argv
, filename
))
844 return (CUPS_BACKEND_FAILED
);
846 filename
= pstmpname
;
849 * Change the MIME type to application/postscript and change the
850 * number of copies to 1...
853 content_type
= "application/postscript";
855 copies_remaining
= 1;
858 #endif /* __APPLE__ */
860 if (content_type
!= NULL
&& format_sup
!= NULL
)
862 for (i
= 0; i
< format_sup
->num_values
; i
++)
863 if (!strcasecmp(content_type
, format_sup
->values
[i
].string
.text
))
866 if (i
< format_sup
->num_values
)
867 num_options
= cupsAddOption("document-format", content_type
,
868 num_options
, &options
);
871 if (copies_sup
&& version
> 0)
874 * Only send options if the destination printer supports the copies
875 * attribute and IPP/1.1. This is a hack for the HP and Lexmark
876 * implementations of IPP, which do not accept extension attributes
877 * and incorrectly report a client-error-bad-request error instead of
878 * the successful-ok-unsupported-attributes status. In short, at least
879 * some HP and Lexmark implementations of IPP are non-compliant.
882 cupsEncodeOptions(request
, num_options
, options
);
884 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies",
888 cupsFreeOptions(num_options
, options
);
891 * If copies aren't supported, then we are likely dealing with an HP
892 * JetDirect. The HP IPP implementation seems to close the connection
893 * after every request - that is, it does *not* implement HTTP Keep-
894 * Alive, which is REQUIRED by HTTP/1.1...
905 response
= cupsDoRequest(http
, request
, resource
);
907 response
= cupsDoFileRequest(http
, request
, resource
, files
[0]);
909 ipp_status
= cupsLastError();
911 if (ipp_status
> IPP_OK_CONFLICT
)
918 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
919 ipp_status
== IPP_PRINTER_BUSY
)
921 fputs("INFO: Printer is busy; retrying print job...\n", stderr
);
924 else if ((ipp_status
== IPP_BAD_REQUEST
||
925 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
== 1)
928 * Switch to IPP/1.0...
931 fputs("INFO: Printer does not support IPP/1.1, trying IPP/1.0...\n",
937 fprintf(stderr
, "ERROR: Print file was not accepted (%s)!\n",
938 cupsLastErrorString());
940 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
941 IPP_TAG_INTEGER
)) == NULL
)
943 fputs("NOTICE: Print file accepted - job ID unknown.\n", stderr
);
948 job_id
= job_id_attr
->values
[0].integer
;
949 fprintf(stderr
, "NOTICE: Print file accepted - job ID %d.\n", job_id
);
957 if (job_id
&& num_files
> 1)
959 for (i
= 0; i
< num_files
; i
++)
961 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
963 request
->request
.op
.version
[1] = version
;
965 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
968 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
972 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
973 "requesting-user-name", NULL
, argv
[2]);
975 if ((i
+ 1) == num_files
)
976 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
978 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
979 "document-format", NULL
, content_type
);
981 ippDelete(cupsDoFileRequest(http
, request
, resource
, files
[i
]));
983 if (cupsLastError() > IPP_OK_CONFLICT
)
985 ipp_status
= cupsLastError();
987 fprintf(stderr
, "ERROR: Unable to add file %d to job: %s\n",
988 job_id
, cupsLastErrorString());
994 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
996 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
999 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1000 ipp_status
== IPP_PRINTER_BUSY
)
1003 copies_remaining
--;
1006 * Wait for the job to complete...
1009 if (!job_id
|| !waitjob
)
1012 fputs("INFO: Waiting for job to complete...\n", stderr
);
1014 for (; !job_cancelled
;)
1017 * Build an IPP_GET_JOB_ATTRIBUTES request...
1020 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1021 request
->request
.op
.version
[1] = version
;
1023 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1026 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1030 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1031 "requesting-user-name", NULL
, argv
[2]);
1033 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1034 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
1042 httpReconnect(http
);
1044 response
= cupsDoRequest(http
, request
, resource
);
1045 ipp_status
= cupsLastError();
1047 if (ipp_status
== IPP_NOT_FOUND
)
1050 * Job has gone away and/or the server has no job history...
1053 ippDelete(response
);
1055 ipp_status
= IPP_OK
;
1059 if (ipp_status
> IPP_OK_CONFLICT
)
1061 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1062 ipp_status
!= IPP_PRINTER_BUSY
)
1064 ippDelete(response
);
1066 fprintf(stderr
, "ERROR: Unable to get job %d attributes (%s)!\n",
1067 job_id
, ippErrorString(ipp_status
));
1074 if ((job_state
= ippFindAttribute(response
, "job-state",
1075 IPP_TAG_ENUM
)) != NULL
)
1078 * Stop polling if the job is finished or pending-held...
1081 if (job_state
->values
[0].integer
> IPP_JOB_PROCESSING
||
1082 job_state
->values
[0].integer
== IPP_JOB_HELD
)
1084 if ((job_sheets
= ippFindAttribute(response
,
1085 "job-media-sheets-completed",
1086 IPP_TAG_INTEGER
)) != NULL
)
1087 fprintf(stderr
, "PAGE: total %d\n",
1088 job_sheets
->values
[0].integer
);
1090 ippDelete(response
);
1096 ippDelete(response
);
1099 * Check the printer state and report it if necessary...
1102 check_printer_state(http
, uri
, resource
, argv
[2], version
);
1105 * Wait 10 seconds before polling again...
1113 * Cancel the job as needed...
1116 if (job_cancelled
&& job_id
)
1117 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1120 * Check the printer state and report it if necessary...
1123 check_printer_state(http
, uri
, resource
, argv
[2], version
);
1131 ippDelete(supported
);
1134 * Remove the temporary file(s) if necessary...
1138 unlink(tmpfilename
);
1143 for (i
= 0; i
< num_files
; i
++)
1146 #endif /* HAVE_LIBZ */
1151 #endif /* __APPLE__ */
1154 * Return the queue status...
1157 return (ipp_status
> IPP_OK_CONFLICT
? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
1162 * 'cancel_job()' - Cancel a print job.
1166 cancel_job(http_t
*http
, /* I - HTTP connection */
1167 const char *uri
, /* I - printer-uri */
1168 int id
, /* I - job-id */
1169 const char *resource
, /* I - Resource path */
1170 const char *user
, /* I - requesting-user-name */
1171 int version
) /* I - IPP version */
1173 ipp_t
*request
; /* Cancel-Job request */
1176 fputs("INFO: Cancelling print job...\n", stderr
);
1178 request
= ippNewRequest(IPP_CANCEL_JOB
);
1179 request
->request
.op
.version
[1] = version
;
1181 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1183 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1185 if (user
&& user
[0])
1186 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1187 "requesting-user-name", NULL
, user
);
1193 ippDelete(cupsDoRequest(http
, request
, resource
));
1195 if (cupsLastError() > IPP_OK_CONFLICT
)
1196 fprintf(stderr
, "ERROR: Unable to cancel job %d: %s\n", id
,
1197 cupsLastErrorString());
1202 * 'check_printer_state()' - Check the printer state...
1206 check_printer_state(
1207 http_t
*http
, /* I - HTTP connection */
1208 const char *uri
, /* I - Printer URI */
1209 const char *resource
, /* I - Resource path */
1210 const char *user
, /* I - Username, if any */
1211 int version
) /* I - IPP version */
1213 ipp_t
*request
, /* IPP request */
1214 *response
; /* IPP response */
1218 * Check on the printer state...
1221 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1222 request
->request
.op
.version
[1] = version
;
1224 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1227 if (user
&& user
[0])
1228 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1229 "requesting-user-name", NULL
, user
);
1231 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1232 "requested-attributes", NULL
, "printer-state-reasons");
1238 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
1240 report_printer_state(response
);
1241 ippDelete(response
);
1248 * 'compress_files()' - Compress print files...
1252 compress_files(int num_files
, /* I - Number of files */
1253 char **files
) /* I - Files */
1255 int i
, /* Looping var */
1256 fd
; /* Temporary file descriptor */
1257 ssize_t bytes
; /* Bytes read/written */
1258 size_t total
; /* Total bytes read */
1259 cups_file_t
*in
, /* Input file */
1260 *out
; /* Output file */
1261 struct stat outinfo
; /* Output file information */
1262 char filename
[1024], /* Temporary filename */
1263 buffer
[32768]; /* Copy buffer */
1266 fprintf(stderr
, "DEBUG: Compressing %d job files...\n", num_files
);
1267 for (i
= 0; i
< num_files
; i
++)
1269 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
1271 perror("ERROR: Unable to create temporary compressed print file");
1272 exit(CUPS_BACKEND_FAILED
);
1275 if ((out
= cupsFileOpenFd(fd
, "w9")) == NULL
)
1277 perror("ERROR: Unable to open temporary compressed print file");
1278 exit(CUPS_BACKEND_FAILED
);
1281 if ((in
= cupsFileOpen(files
[i
], "r")) == NULL
)
1283 fprintf(stderr
, "ERROR: Unable to open print file \"%s\": %s\n",
1284 files
[i
], strerror(errno
));
1286 exit(CUPS_BACKEND_FAILED
);
1290 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
1291 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
1293 fprintf(stderr
, "ERROR: Unable to write " CUPS_LLFMT
" bytes to \"%s\": %s\n",
1294 CUPS_LLCAST bytes
, filename
, strerror(errno
));
1297 exit(CUPS_BACKEND_FAILED
);
1305 files
[i
] = strdup(filename
);
1307 if (!stat(filename
, &outinfo
))
1309 "DEBUG: File %d compressed to %.1f%% of original size, "
1310 CUPS_LLFMT
" bytes...\n",
1311 i
+ 1, 100.0 * outinfo
.st_size
/ total
,
1312 CUPS_LLCAST outinfo
.st_size
);
1315 #endif /* HAVE_LIBZ */
1319 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
1322 static const char * /* O - Password */
1323 password_cb(const char *prompt
) /* I - Prompt (not used) */
1332 * If there is no password set in the device URI, return the
1333 * "authentication required" exit code...
1337 unlink(tmpfilename
);
1342 #endif /* __APPLE__ */
1344 exit(CUPS_BACKEND_AUTH_REQUIRED
);
1346 return (NULL
); /* Eliminate compiler warning */
1352 * 'report_printer_state()' - Report the printer state.
1355 static int /* O - Number of reasons shown */
1356 report_printer_state(ipp_t
*ipp
) /* I - IPP response */
1358 int i
; /* Looping var */
1359 int count
; /* Count of reasons shown... */
1360 ipp_attribute_t
*reasons
; /* printer-state-reasons */
1361 const char *reason
; /* Current reason */
1362 const char *message
; /* Message to show */
1363 char unknown
[1024]; /* Unknown message string */
1364 const char *prefix
; /* Prefix for STATE: line */
1365 char state
[1024]; /* State string */
1368 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
1369 IPP_TAG_KEYWORD
)) == NULL
)
1375 for (i
= 0, count
= 0; i
< reasons
->num_values
; i
++)
1377 reason
= reasons
->values
[i
].string
.text
;
1379 strlcat(state
, prefix
, sizeof(state
));
1380 strlcat(state
, reason
, sizeof(state
));
1385 if (!strncmp(reason
, "media-needed", 12))
1386 message
= "Media tray needs to be filled.";
1387 else if (!strncmp(reason
, "media-jam", 9))
1388 message
= "Media jam!";
1389 else if (!strncmp(reason
, "moving-to-paused", 16) ||
1390 !strncmp(reason
, "paused", 6) ||
1391 !strncmp(reason
, "shutdown", 8))
1392 message
= "Printer off-line.";
1393 else if (!strncmp(reason
, "toner-low", 9))
1394 message
= "Toner low.";
1395 else if (!strncmp(reason
, "toner-empty", 11))
1396 message
= "Out of toner!";
1397 else if (!strncmp(reason
, "cover-open", 10))
1398 message
= "Cover open.";
1399 else if (!strncmp(reason
, "interlock-open", 14))
1400 message
= "Interlock open.";
1401 else if (!strncmp(reason
, "door-open", 9))
1402 message
= "Door open.";
1403 else if (!strncmp(reason
, "input-tray-missing", 18))
1404 message
= "Media tray missing!";
1405 else if (!strncmp(reason
, "media-low", 9))
1406 message
= "Media tray almost empty.";
1407 else if (!strncmp(reason
, "media-empty", 11))
1408 message
= "Media tray empty!";
1409 else if (!strncmp(reason
, "output-tray-missing", 19))
1410 message
= "Output tray missing!";
1411 else if (!strncmp(reason
, "output-area-almost-full", 23))
1412 message
= "Output bin almost full.";
1413 else if (!strncmp(reason
, "output-area-full", 16))
1414 message
= "Output bin full!";
1415 else if (!strncmp(reason
, "marker-supply-low", 17))
1416 message
= "Ink/toner almost empty.";
1417 else if (!strncmp(reason
, "marker-supply-empty", 19))
1418 message
= "Ink/toner empty!";
1419 else if (!strncmp(reason
, "marker-waste-almost-full", 24))
1420 message
= "Ink/toner waste bin almost full.";
1421 else if (!strncmp(reason
, "marker-waste-full", 17))
1422 message
= "Ink/toner waste bin full!";
1423 else if (!strncmp(reason
, "fuser-over-temp", 15))
1424 message
= "Fuser temperature high!";
1425 else if (!strncmp(reason
, "fuser-under-temp", 16))
1426 message
= "Fuser temperature low!";
1427 else if (!strncmp(reason
, "opc-near-eol", 12))
1428 message
= "OPC almost at end-of-life.";
1429 else if (!strncmp(reason
, "opc-life-over", 13))
1430 message
= "OPC at end-of-life!";
1431 else if (!strncmp(reason
, "developer-low", 13))
1432 message
= "Developer almost empty.";
1433 else if (!strncmp(reason
, "developer-empty", 15))
1434 message
= "Developer empty!";
1435 else if (strstr(reason
, "error") != NULL
)
1439 snprintf(unknown
, sizeof(unknown
), "Unknown printer error (%s)!",
1446 if (strstr(reasons
->values
[i
].string
.text
, "error"))
1447 fprintf(stderr
, "ERROR: %s\n", message
);
1448 else if (strstr(reasons
->values
[i
].string
.text
, "warning"))
1449 fprintf(stderr
, "WARNING: %s\n", message
);
1451 fprintf(stderr
, "INFO: %s\n", message
);
1455 fprintf(stderr
, "%s\n", state
);
1463 * 'run_pictwps_filter()' - Convert PICT files to PostScript when printing
1466 * This step is required because the PICT format is not documented and
1467 * subject to change, so developing a filter for other OS's is infeasible.
1468 * Also, fonts required by the PICT file need to be embedded on the
1469 * client side (which has the fonts), so we run the filter to get a
1470 * PostScript file for printing...
1473 static int /* O - Exit status of filter */
1474 run_pictwps_filter(char **argv
, /* I - Command-line arguments */
1475 const char *filename
)/* I - Filename */
1477 struct stat fileinfo
; /* Print file information */
1478 const char *ppdfile
; /* PPD file for destination printer */
1479 int pid
; /* Child process ID */
1480 int fd
; /* Temporary file descriptor */
1481 int status
; /* Exit status of filter */
1482 const char *printer
; /* PRINTER env var */
1483 static char ppdenv
[1024]; /* PPD environment variable */
1487 * First get the PPD file for the printer...
1490 printer
= getenv("PRINTER");
1493 fputs("ERROR: PRINTER environment variable not defined!\n", stderr
);
1497 if ((ppdfile
= cupsGetPPD(printer
)) == NULL
)
1499 fprintf(stderr
, "ERROR: Unable to get PPD file for printer \"%s\" - %s.\n",
1500 printer
, ippErrorString(cupsLastError()));
1505 snprintf(ppdenv
, sizeof(ppdenv
), "PPD=%s", ppdfile
);
1510 * Then create a temporary file for printing...
1513 if ((fd
= cupsTempFd(pstmpname
, sizeof(pstmpname
))) < 0)
1515 fprintf(stderr
, "ERROR: Unable to create temporary file - %s.\n",
1523 * Get the owner of the spool file - it is owned by the user we want to run
1528 stat(argv
[6], &fileinfo
);
1532 * Use the OSX defaults, as an up-stream filter created the PICT
1536 fileinfo
.st_uid
= 1;
1537 fileinfo
.st_gid
= 80;
1541 chown(ppdfile
, fileinfo
.st_uid
, fileinfo
.st_gid
);
1543 fchown(fd
, fileinfo
.st_uid
, fileinfo
.st_gid
);
1546 * Finally, run the filter to convert the file...
1549 if ((pid
= fork()) == 0)
1552 * Child process for pictwpstops... Redirect output of pictwpstops to a
1563 * Change to an unpriviledged user...
1566 setgid(fileinfo
.st_gid
);
1567 setuid(fileinfo
.st_uid
);
1570 execlp("pictwpstops", printer
, argv
[1], argv
[2], argv
[3], argv
[4], argv
[5],
1572 perror("ERROR: Unable to exec pictwpstops");
1584 perror("ERROR: Unable to fork pictwpstops");
1592 * Now wait for the filter to complete...
1595 if (wait(&status
) < 0)
1597 perror("ERROR: Unable to wait for pictwpstops");
1613 fprintf(stderr
, "ERROR: pictwpstops exited with status %d!\n",
1616 fprintf(stderr
, "ERROR: pictwpstops exited on signal %d!\n",
1624 * Return with no errors..
1629 #endif /* __APPLE__ */
1633 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1637 sigterm_handler(int sig
) /* I - Signal */
1639 (void)sig
; /* remove compiler warnings... */
1644 * Flag that the job should be cancelled...
1652 * The scheduler already tried to cancel us once, now just terminate
1653 * after removing our temp files!
1657 unlink(tmpfilename
);
1662 #endif /* __APPLE__ */
1669 * End of "$Id: ipp.c 6214 2007-01-23 17:01:48Z mike $".