2 * "$Id: ipp.c 5241 2006-03-07 22:07:44Z 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 *optptr
, /* Pointer to URI options */
111 name
[255], /* Name of option */
112 value
[255], /* Value of option */
113 *ptr
; /* Pointer into name or value */
114 int num_files
; /* Number of files to print */
115 char **files
, /* Files to print */
116 *filename
; /* Pointer to single filename */
117 int port
; /* Port number (not used) */
118 char uri
[HTTP_MAX_URI
]; /* Updated URI without user/pass */
119 ipp_status_t ipp_status
; /* Status of IPP request */
120 http_t
*http
; /* HTTP connection */
121 ipp_t
*request
, /* IPP request */
122 *response
, /* IPP response */
123 *supported
; /* get-printer-attributes response */
124 int compression
, /* Do compression of the job data? */
125 waitjob
, /* Wait for job complete? */
126 waitprinter
; /* Wait for printer ready? */
127 ipp_attribute_t
*job_id_attr
; /* job-id attribute */
128 int job_id
; /* job-id value */
129 ipp_attribute_t
*job_sheets
; /* job-media-sheets-completed */
130 ipp_attribute_t
*job_state
; /* job-state */
131 ipp_attribute_t
*copies_sup
; /* copies-supported */
132 ipp_attribute_t
*format_sup
; /* document-format-supported */
133 ipp_attribute_t
*printer_state
; /* printer-state attribute */
134 ipp_attribute_t
*printer_accepting
; /* printer-is-accepting-jobs */
135 int copies
; /* Number of copies remaining */
136 const char *content_type
; /* CONTENT_TYPE environment variable */
137 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
138 struct sigaction action
; /* Actions for POSIX signals */
139 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
140 int version
; /* IPP version */
141 int reasons
; /* Number of printer-state-reasons */
142 static const char * const pattrs
[] =
143 { /* Printer attributes we want */
145 "document-format-supported",
146 "printer-is-accepting-jobs",
148 "printer-state-reasons",
150 static const char * const jattrs
[] =
151 { /* Job attributes we want */
152 "job-media-sheets-completed",
158 * Make sure status messages are not buffered...
161 setbuf(stderr
, NULL
);
164 * Ignore SIGPIPE and catch SIGTERM signals...
168 sigset(SIGPIPE
, SIG_IGN
);
169 sigset(SIGTERM
, sigterm_handler
);
170 #elif defined(HAVE_SIGACTION)
171 memset(&action
, 0, sizeof(action
));
172 action
.sa_handler
= SIG_IGN
;
173 sigaction(SIGPIPE
, &action
, NULL
);
175 sigemptyset(&action
.sa_mask
);
176 sigaddset(&action
.sa_mask
, SIGTERM
);
177 action
.sa_handler
= sigterm_handler
;
178 sigaction(SIGTERM
, &action
, NULL
);
180 signal(SIGPIPE
, SIG_IGN
);
181 signal(SIGTERM
, sigterm_handler
);
182 #endif /* HAVE_SIGSET */
185 * Check command-line...
192 if ((s
= strrchr(argv
[0], '/')) != NULL
)
197 printf("network %s \"Unknown\" \"Internet Printing Protocol (%s)\"\n",
199 return (CUPS_BACKEND_OK
);
204 "Usage: %s job-id user title copies options [file ... fileN]\n",
206 return (CUPS_BACKEND_STOP
);
210 * Get the (final) content type...
213 if ((content_type
= getenv("FINAL_CONTENT_TYPE")) == NULL
)
214 content_type
= "application/octet-stream";
217 * Extract the hostname and printer name from the URI...
220 if (httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
221 method
, sizeof(method
), username
, sizeof(username
),
222 hostname
, sizeof(hostname
), &port
,
223 resource
, sizeof(resource
)) < HTTP_URI_OK
)
225 fputs("ERROR: Missing device URI on command-line and no DEVICE_URI "
226 "environment variable!\n", stderr
);
227 return (CUPS_BACKEND_STOP
);
230 if (!strcmp(method
, "https"))
231 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
234 * See if there are any options...
242 if ((optptr
= strchr(resource
, '?')) != NULL
)
245 * Yup, terminate the device name string and move to the first
246 * character of the optptr...
252 * Then parse the optptr...
261 for (ptr
= name
; *optptr
&& *optptr
!= '=';)
262 if (ptr
< (name
+ sizeof(name
) - 1))
274 for (ptr
= value
; *optptr
&& *optptr
!= '+' && *optptr
!= '&';)
275 if (ptr
< (value
+ sizeof(value
) - 1))
286 * Process the option...
289 if (!strcasecmp(name
, "waitjob"))
292 * Wait for job completion?
295 waitjob
= !strcasecmp(value
, "on") ||
296 !strcasecmp(value
, "yes") ||
297 !strcasecmp(value
, "true");
299 else if (!strcasecmp(name
, "waitprinter"))
302 * Wait for printer idle?
305 waitprinter
= !strcasecmp(value
, "on") ||
306 !strcasecmp(value
, "yes") ||
307 !strcasecmp(value
, "true");
309 else if (!strcasecmp(name
, "encryption"))
312 * Enable/disable encryption?
315 if (!strcasecmp(value
, "always"))
316 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
317 else if (!strcasecmp(value
, "required"))
318 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
319 else if (!strcasecmp(value
, "never"))
320 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
321 else if (!strcasecmp(value
, "ifrequested"))
322 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
325 fprintf(stderr
, "ERROR: Unknown encryption option value \"%s\"!\n",
329 else if (!strcasecmp(name
, "version"))
331 if (!strcmp(value
, "1.0"))
333 else if (!strcmp(value
, "1.1"))
337 fprintf(stderr
, "ERROR: Unknown version option value \"%s\"!\n",
342 else if (!strcasecmp(name
, "compression"))
344 compression
= !strcasecmp(value
, "true") ||
345 !strcasecmp(value
, "yes") ||
346 !strcasecmp(value
, "on") ||
347 !strcasecmp(value
, "gzip");
349 #endif /* HAVE_LIBZ */
356 fprintf(stderr
, "ERROR: Unknown option \"%s\" with value \"%s\"!\n",
363 * If we have 7 arguments, print the file named on the command-line.
364 * Otherwise, copy stdin to a temporary file and print the temporary
371 * Copy stdin to a temporary file...
374 int fd
; /* File descriptor */
375 cups_file_t
*fp
; /* Temporary file */
376 char buffer
[8192]; /* Buffer for copying */
377 int bytes
; /* Number of bytes read */
380 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
382 perror("ERROR: unable to create temporary file");
383 return (CUPS_BACKEND_FAILED
);
386 if ((fp
= cupsFileOpenFd(fd
, compression
? "w9" : "w")) == NULL
)
388 perror("ERROR: unable to open temporary file");
391 return (CUPS_BACKEND_FAILED
);
394 while ((bytes
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0)
395 if (cupsFileWrite(fp
, buffer
, bytes
) < bytes
)
397 perror("ERROR: unable to write to temporary file");
400 return (CUPS_BACKEND_FAILED
);
406 * Point to the single file from stdin...
409 filename
= tmpfilename
;
416 * Point to the files on the command-line...
419 num_files
= argc
- 6;
424 compress_files(num_files
, files
);
425 #endif /* HAVE_LIBZ */
428 fprintf(stderr
, "DEBUG: %d files to send in job...\n", num_files
);
431 * Set the authentication info, if any...
434 cupsSetPasswordCB(password_cb
);
439 * Use authenticaion information in the device URI...
442 if ((password
= strchr(username
, ':')) != NULL
)
445 cupsSetUser(username
);
450 * Try loading authentication information from the a##### file.
453 const char *request_root
; /* CUPS_REQUESTROOT env var */
454 char afilename
[1024], /* a##### filename */
455 aline
[1024]; /* Line from file */
456 FILE *fp
; /* File pointer */
459 if ((request_root
= getenv("CUPS_REQUESTROOT")) != NULL
)
462 * Try opening authentication cache file...
465 snprintf(afilename
, sizeof(afilename
), "%s/a%05d", request_root
,
467 if ((fp
= fopen(afilename
, "r")) != NULL
)
473 if (fgets(aline
, sizeof(aline
), fp
))
479 i
= sizeof(username
);
480 httpDecode64_2(username
, &i
, aline
);
486 if (fgets(aline
, sizeof(aline
), fp
))
492 i
= sizeof(password
);
493 httpDecode64_2(password
, &i
, aline
);
507 * Try connecting to the remote server...
510 fputs("STATE: +connecting-to-device\n", stderr
);
514 fprintf(stderr
, "INFO: Connecting to %s on port %d...\n", hostname
, port
);
516 if ((http
= httpConnectEncrypt(hostname
, port
, cupsEncryption())) == NULL
)
518 if (getenv("CLASS") != NULL
)
521 * If the CLASS environment variable is set, the job was submitted
522 * to a class and not to a specific queue. In this case, we want
523 * to abort immediately so that the job can be requeued on the next
524 * available printer in the class.
528 "INFO: Unable to connect to %s, queuing on next printer in "
532 if (argc
== 6 || strcmp(filename
, argv
[6]))
536 * Sleep 5 seconds to keep the job from requeuing too rapidly...
541 return (CUPS_BACKEND_FAILED
);
544 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
545 errno
== EHOSTUNREACH
)
548 "INFO: Network host \'%s\' is busy; will retry in 30 "
555 fprintf(stderr
, "INFO: Unable to lookup host \'%s\' - %s\n",
556 hostname
, hstrerror(h_errno
));
561 perror("ERROR: Unable to connect to IPP host");
566 while (http
== NULL
);
568 fputs("STATE: -connecting-to-device\n", stderr
);
569 fprintf(stderr
, "INFO: Connected to %s...\n", hostname
);
572 * Build a URI for the printer and fill the standard IPP attributes for
573 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
574 * might contain username:password information...
577 snprintf(uri
, sizeof(uri
), "%s://%s:%d%s", method
, hostname
, port
, resource
);
580 * First validate the destination and see if the device supports multiple
581 * copies. We have to do this because some IPP servers (e.g. HP JetDirect)
582 * don't support the copies attribute...
592 * Build the IPP request...
595 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
596 request
->request
.op
.version
[1] = version
;
598 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
601 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
602 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
609 fputs("DEBUG: Getting supported attributes...\n", stderr
);
611 if ((supported
= cupsDoRequest(http
, request
, resource
)) == NULL
)
612 ipp_status
= cupsLastError();
614 ipp_status
= supported
->request
.status
.status_code
;
616 if (ipp_status
> IPP_OK_CONFLICT
)
618 if (ipp_status
== IPP_PRINTER_BUSY
||
619 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
621 fputs("INFO: Printer busy; will retry in 10 seconds...\n", stderr
);
622 report_printer_state(supported
);
625 else if ((ipp_status
== IPP_BAD_REQUEST
||
626 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
== 1)
629 * Switch to IPP/1.0...
632 fputs("INFO: Printer does not support IPP/1.1, trying IPP/1.0...\n",
637 else if (ipp_status
== IPP_NOT_FOUND
)
639 fputs("ERROR: Destination printer does not exist!\n", stderr
);
642 ippDelete(supported
);
644 return (CUPS_BACKEND_STOP
);
648 fprintf(stderr
, "ERROR: Unable to get printer status (%s)!\n",
649 ippErrorString(ipp_status
));
654 ippDelete(supported
);
658 else if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
659 IPP_TAG_RANGE
)) != NULL
)
662 * Has the "copies-supported" attribute - does it have an upper
666 if (copies_sup
->values
[0].range
.upper
<= 1)
667 copies_sup
= NULL
; /* No */
670 format_sup
= ippFindAttribute(supported
, "document-format-supported",
675 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
676 format_sup
->num_values
);
677 for (i
= 0; i
< format_sup
->num_values
; i
++)
678 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
679 format_sup
->values
[i
].string
.text
);
682 report_printer_state(supported
);
684 while (ipp_status
> IPP_OK_CONFLICT
);
687 * See if the printer is accepting jobs and is not stopped; if either
688 * condition is true and we are printing to a class, requeue the job...
691 if (getenv("CLASS") != NULL
)
693 printer_state
= ippFindAttribute(supported
, "printer-state",
695 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
698 if (printer_state
== NULL
||
699 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
701 printer_accepting
== NULL
||
702 !printer_accepting
->values
[0].boolean
)
705 * If the CLASS environment variable is set, the job was submitted
706 * to a class and not to a specific queue. In this case, we want
707 * to abort immediately so that the job can be requeued on the next
708 * available printer in the class.
712 "INFO: Unable to queue job on %s, queuing on next printer in "
716 ippDelete(supported
);
719 if (argc
== 6 || strcmp(filename
, argv
[6]))
723 * Sleep 5 seconds to keep the job from requeuing too rapidly...
728 return (CUPS_BACKEND_FAILED
);
733 * See if the printer supports multiple copies...
736 if (copies_sup
|| argc
< 7)
739 copies
= atoi(argv
[4]);
742 * Then issue the print-job request...
751 * Build the IPP request...
758 request
= ippNewRequest(IPP_CREATE_JOB
);
760 request
= ippNewRequest(IPP_PRINT_JOB
);
762 request
->request
.op
.version
[1] = version
;
764 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
767 fprintf(stderr
, "DEBUG: printer-uri = \"%s\"\n", uri
);
770 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
771 "requesting-user-name", NULL
, argv
[2]);
773 fprintf(stderr
, "DEBUG: requesting-user-name = \"%s\"\n", argv
[2]);
776 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
779 fprintf(stderr
, "DEBUG: job-name = \"%s\"\n", argv
[3]);
783 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
784 "compression", NULL
, "gzip");
785 #endif /* HAVE_LIBZ */
788 * Handle options on the command-line...
792 num_options
= cupsParseOptions(argv
[5], 0, &options
);
795 if (content_type
!= NULL
&&
796 !strcasecmp(content_type
, "application/pictwps") && num_files
== 1)
798 if (format_sup
!= NULL
)
800 for (i
= 0; i
< format_sup
->num_values
; i
++)
801 if (!strcasecmp(content_type
, format_sup
->values
[i
].string
.text
))
805 if (format_sup
== NULL
|| i
>= format_sup
->num_values
)
808 * Remote doesn't support "application/pictwps" (i.e. it's not MacOS X)
809 * so convert the document to PostScript...
812 if (run_pictwps_filter(argv
, filename
))
813 return (CUPS_BACKEND_FAILED
);
815 filename
= pstmpname
;
818 * Change the MIME type to application/postscript and change the
819 * number of copies to 1...
822 content_type
= "application/postscript";
826 #endif /* __APPLE__ */
828 if (content_type
!= NULL
&& format_sup
!= NULL
)
830 for (i
= 0; i
< format_sup
->num_values
; i
++)
831 if (!strcasecmp(content_type
, format_sup
->values
[i
].string
.text
))
834 if (i
< format_sup
->num_values
)
835 num_options
= cupsAddOption("document-format", content_type
,
836 num_options
, &options
);
842 * Only send options if the destination printer supports the copies
843 * attribute. This is a hack for the HP JetDirect implementation of
844 * IPP, which does not accept extension attributes and incorrectly
845 * reports a client-error-bad-request error instead of the
846 * successful-ok-unsupported-attributes status. In short, at least
847 * some HP implementations of IPP are non-compliant.
850 cupsEncodeOptions(request
, num_options
, options
);
851 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies",
855 cupsFreeOptions(num_options
, options
);
858 * If copies aren't supported, then we are likely dealing with an HP
859 * JetDirect. The HP IPP implementation seems to close the connection
860 * after every request (that is, it does *not* implement HTTP Keep-
861 * Alive, which is REQUIRED by HTTP/1.1...
872 response
= cupsDoRequest(http
, request
, resource
);
874 response
= cupsDoFileRequest(http
, request
, resource
, files
[0]);
876 ipp_status
= cupsLastError();
878 if (ipp_status
> IPP_OK_CONFLICT
)
885 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
886 ipp_status
== IPP_PRINTER_BUSY
)
888 fputs("INFO: Printer is busy; retrying print job...\n", stderr
);
892 fprintf(stderr
, "ERROR: Print file was not accepted (%s)!\n",
893 cupsLastErrorString());
895 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
896 IPP_TAG_INTEGER
)) == NULL
)
898 fputs("NOTICE: Print file accepted - job ID unknown.\n", stderr
);
903 job_id
= job_id_attr
->values
[0].integer
;
904 fprintf(stderr
, "NOTICE: Print file accepted - job ID %d.\n", job_id
);
912 if (job_id
&& num_files
> 1)
914 for (i
= 0; i
< num_files
; i
++)
916 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
918 request
->request
.op
.version
[1] = version
;
920 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
923 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
927 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
928 "requesting-user-name", NULL
, argv
[2]);
930 if ((i
+ 1) == num_files
)
931 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
933 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
934 "document-format", NULL
, content_type
);
936 ippDelete(cupsDoFileRequest(http
, request
, resource
, files
[i
]));
938 if (cupsLastError() > IPP_OK_CONFLICT
)
940 ipp_status
= cupsLastError();
942 fprintf(stderr
, "ERROR: Unable to add file %d to job: %s\n",
943 job_id
, cupsLastErrorString());
949 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
951 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
954 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
955 ipp_status
== IPP_PRINTER_BUSY
)
961 * Wait for the job to complete...
964 if (!job_id
|| !waitjob
)
967 fputs("INFO: Waiting for job to complete...\n", stderr
);
969 for (; !job_cancelled
;)
972 * Build an IPP_GET_JOB_ATTRIBUTES request...
975 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
976 request
->request
.op
.version
[1] = version
;
978 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
981 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
985 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
986 "requesting-user-name", NULL
, argv
[2]);
988 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
989 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
999 response
= cupsDoRequest(http
, request
, resource
);
1000 ipp_status
= cupsLastError();
1002 if (ipp_status
== IPP_NOT_FOUND
)
1005 * Job has gone away and/or the server has no job history...
1008 ippDelete(response
);
1010 ipp_status
= IPP_OK
;
1014 if (ipp_status
> IPP_OK_CONFLICT
)
1016 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1017 ipp_status
!= IPP_PRINTER_BUSY
)
1019 ippDelete(response
);
1021 fprintf(stderr
, "ERROR: Unable to get job %d attributes (%s)!\n",
1022 job_id
, ippErrorString(ipp_status
));
1029 if ((job_state
= ippFindAttribute(response
, "job-state",
1030 IPP_TAG_ENUM
)) != NULL
)
1033 * Stop polling if the job is finished or pending-held...
1036 if (job_state
->values
[0].integer
> IPP_JOB_PROCESSING
||
1037 job_state
->values
[0].integer
== IPP_JOB_HELD
)
1039 if ((job_sheets
= ippFindAttribute(response
,
1040 "job-media-sheets-completed",
1041 IPP_TAG_INTEGER
)) != NULL
)
1042 fprintf(stderr
, "PAGE: total %d\n",
1043 job_sheets
->values
[0].integer
);
1045 ippDelete(response
);
1051 ippDelete(response
);
1054 * Check the printer state and report it if necessary...
1057 check_printer_state(http
, uri
, resource
, argv
[2], version
);
1060 * Wait 10 seconds before polling again...
1068 * Cancel the job as needed...
1071 if (job_cancelled
&& job_id
)
1072 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1075 * Check the printer state and report it if necessary...
1078 check_printer_state(http
, uri
, resource
, argv
[2], version
);
1086 ippDelete(supported
);
1089 * Remove the temporary file(s) if necessary...
1093 unlink(tmpfilename
);
1098 for (i
= 0; i
< num_files
; i
++)
1101 #endif /* HAVE_LIBZ */
1106 #endif /* __APPLE__ */
1109 * Return the queue status...
1112 return (ipp_status
> IPP_OK_CONFLICT
? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
1117 * 'cancel_job()' - Cancel a print job.
1121 cancel_job(http_t
*http
, /* I - HTTP connection */
1122 const char *uri
, /* I - printer-uri */
1123 int id
, /* I - job-id */
1124 const char *resource
, /* I - Resource path */
1125 const char *user
, /* I - requesting-user-name */
1126 int version
) /* I - IPP version */
1128 ipp_t
*request
; /* Cancel-Job request */
1131 fputs("INFO: Cancelling print job...\n", stderr
);
1133 request
= ippNewRequest(IPP_CANCEL_JOB
);
1134 request
->request
.op
.version
[1] = version
;
1136 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1138 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1140 if (user
&& user
[0])
1141 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1142 "requesting-user-name", NULL
, user
);
1148 ippDelete(cupsDoRequest(http
, request
, resource
));
1150 if (cupsLastError() > IPP_OK_CONFLICT
)
1151 fprintf(stderr
, "ERROR: Unable to cancel job %d: %s\n", id
,
1152 cupsLastErrorString());
1157 * 'check_printer_state()' - Check the printer state...
1161 check_printer_state(
1162 http_t
*http
, /* I - HTTP connection */
1163 const char *uri
, /* I - Printer URI */
1164 const char *resource
, /* I - Resource path */
1165 const char *user
, /* I - Username, if any */
1166 int version
) /* I - IPP version */
1168 ipp_t
*request
, /* IPP request */
1169 *response
; /* IPP response */
1173 * Check on the printer state...
1176 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1177 request
->request
.op
.version
[1] = version
;
1179 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1182 if (user
&& user
[0])
1183 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1184 "requesting-user-name", NULL
, user
);
1186 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1187 "requested-attributes", NULL
, "printer-state-reasons");
1193 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
1195 report_printer_state(response
);
1196 ippDelete(response
);
1203 * 'compress_files()' - Compress print files...
1207 compress_files(int num_files
, /* I - Number of files */
1208 char **files
) /* I - Files */
1210 int i
, /* Looping var */
1211 fd
; /* Temporary file descriptor */
1212 ssize_t bytes
; /* Bytes read/written */
1213 size_t total
; /* Total bytes read */
1214 cups_file_t
*in
, /* Input file */
1215 *out
; /* Output file */
1216 struct stat outinfo
; /* Output file information */
1217 char filename
[1024], /* Temporary filename */
1218 buffer
[65536]; /* Copy buffer */
1221 fprintf(stderr
, "DEBUG: Compressing %d job files...\n", num_files
);
1222 for (i
= 0; i
< num_files
; i
++)
1224 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
1226 perror("ERROR: Unable to create temporary compressed print file");
1227 exit(CUPS_BACKEND_FAILED
);
1230 if ((out
= cupsFileOpenFd(fd
, "w9")) == NULL
)
1232 perror("ERROR: Unable to open temporary compressed print file");
1233 exit(CUPS_BACKEND_FAILED
);
1236 if ((in
= cupsFileOpen(files
[i
], "r")) == NULL
)
1238 fprintf(stderr
, "ERROR: Unable to open print file \"%s\": %s\n",
1239 files
[i
], strerror(errno
));
1241 exit(CUPS_BACKEND_FAILED
);
1245 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
1246 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
1248 fprintf(stderr
, "ERROR: Unable to write " CUPS_LLFMT
" bytes to \"%s\": %s\n",
1249 CUPS_LLCAST bytes
, filename
, strerror(errno
));
1252 exit(CUPS_BACKEND_FAILED
);
1260 files
[i
] = strdup(filename
);
1262 if (!stat(filename
, &outinfo
))
1264 "DEBUG: File %d compressed to %.1f%% of original size, "
1265 CUPS_LLFMT
" bytes...\n",
1266 i
+ 1, 100.0 * outinfo
.st_size
/ total
, outinfo
.st_size
);
1269 #endif /* HAVE_LIBZ */
1273 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
1276 static const char * /* O - Password */
1277 password_cb(const char *prompt
) /* I - Prompt (not used) */
1286 * If there is no password set in the device URI, return the
1287 * "authentication required" exit code...
1291 unlink(tmpfilename
);
1296 #endif /* __APPLE__ */
1298 exit(CUPS_BACKEND_AUTH_REQUIRED
);
1304 * 'report_printer_state()' - Report the printer state.
1307 static int /* O - Number of reasons shown */
1308 report_printer_state(ipp_t
*ipp
) /* I - IPP response */
1310 int i
; /* Looping var */
1311 int count
; /* Count of reasons shown... */
1312 ipp_attribute_t
*reasons
; /* printer-state-reasons */
1313 const char *reason
; /* Current reason */
1314 const char *message
; /* Message to show */
1315 char unknown
[1024]; /* Unknown message string */
1316 const char *prefix
; /* Prefix for STATE: line */
1317 char state
[1024]; /* State string */
1320 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
1321 IPP_TAG_KEYWORD
)) == NULL
)
1327 for (i
= 0, count
= 0; i
< reasons
->num_values
; i
++)
1329 reason
= reasons
->values
[i
].string
.text
;
1331 strlcat(state
, prefix
, sizeof(state
));
1332 strlcat(state
, reason
, sizeof(state
));
1337 if (!strncmp(reason
, "media-needed", 12))
1338 message
= "Media tray needs to be filled.";
1339 else if (!strncmp(reason
, "media-jam", 9))
1340 message
= "Media jam!";
1341 else if (!strncmp(reason
, "moving-to-paused", 16) ||
1342 !strncmp(reason
, "paused", 6) ||
1343 !strncmp(reason
, "shutdown", 8))
1344 message
= "Printer off-line.";
1345 else if (!strncmp(reason
, "toner-low", 9))
1346 message
= "Toner low.";
1347 else if (!strncmp(reason
, "toner-empty", 11))
1348 message
= "Out of toner!";
1349 else if (!strncmp(reason
, "cover-open", 10))
1350 message
= "Cover open.";
1351 else if (!strncmp(reason
, "interlock-open", 14))
1352 message
= "Interlock open.";
1353 else if (!strncmp(reason
, "door-open", 9))
1354 message
= "Door open.";
1355 else if (!strncmp(reason
, "input-tray-missing", 18))
1356 message
= "Media tray missing!";
1357 else if (!strncmp(reason
, "media-low", 9))
1358 message
= "Media tray almost empty.";
1359 else if (!strncmp(reason
, "media-empty", 11))
1360 message
= "Media tray empty!";
1361 else if (!strncmp(reason
, "output-tray-missing", 19))
1362 message
= "Output tray missing!";
1363 else if (!strncmp(reason
, "output-area-almost-full", 23))
1364 message
= "Output bin almost full.";
1365 else if (!strncmp(reason
, "output-area-full", 16))
1366 message
= "Output bin full!";
1367 else if (!strncmp(reason
, "marker-supply-low", 17))
1368 message
= "Ink/toner almost empty.";
1369 else if (!strncmp(reason
, "marker-supply-empty", 19))
1370 message
= "Ink/toner empty!";
1371 else if (!strncmp(reason
, "marker-waste-almost-full", 24))
1372 message
= "Ink/toner waste bin almost full.";
1373 else if (!strncmp(reason
, "marker-waste-full", 17))
1374 message
= "Ink/toner waste bin full!";
1375 else if (!strncmp(reason
, "fuser-over-temp", 15))
1376 message
= "Fuser temperature high!";
1377 else if (!strncmp(reason
, "fuser-under-temp", 16))
1378 message
= "Fuser temperature low!";
1379 else if (!strncmp(reason
, "opc-near-eol", 12))
1380 message
= "OPC almost at end-of-life.";
1381 else if (!strncmp(reason
, "opc-life-over", 13))
1382 message
= "OPC at end-of-life!";
1383 else if (!strncmp(reason
, "developer-low", 13))
1384 message
= "Developer almost empty.";
1385 else if (!strncmp(reason
, "developer-empty", 15))
1386 message
= "Developer empty!";
1387 else if (strstr(reason
, "error") != NULL
)
1391 snprintf(unknown
, sizeof(unknown
), "Unknown printer error (%s)!",
1398 if (strstr(reasons
->values
[i
].string
.text
, "error"))
1399 fprintf(stderr
, "ERROR: %s\n", message
);
1400 else if (strstr(reasons
->values
[i
].string
.text
, "warning"))
1401 fprintf(stderr
, "WARNING: %s\n", message
);
1403 fprintf(stderr
, "INFO: %s\n", message
);
1407 fprintf(stderr
, "%s\n", state
);
1415 * 'run_pictwps_filter()' - Convert PICT files to PostScript when printing
1418 * This step is required because the PICT format is not documented and
1419 * subject to change, so developing a filter for other OS's is infeasible.
1420 * Also, fonts required by the PICT file need to be embedded on the
1421 * client side (which has the fonts), so we run the filter to get a
1422 * PostScript file for printing...
1425 static int /* O - Exit status of filter */
1426 run_pictwps_filter(char **argv
, /* I - Command-line arguments */
1427 const char *filename
)/* I - Filename */
1429 struct stat fileinfo
; /* Print file information */
1430 const char *ppdfile
; /* PPD file for destination printer */
1431 int pid
; /* Child process ID */
1432 int fd
; /* Temporary file descriptor */
1433 int status
; /* Exit status of filter */
1434 const char *printer
; /* PRINTER env var */
1435 static char ppdenv
[1024]; /* PPD environment variable */
1439 * First get the PPD file for the printer...
1442 printer
= getenv("PRINTER");
1445 fputs("ERROR: PRINTER environment variable not defined!\n", stderr
);
1449 if ((ppdfile
= cupsGetPPD(printer
)) == NULL
)
1451 fprintf(stderr
, "ERROR: Unable to get PPD file for printer \"%s\" - %s.\n",
1452 printer
, ippErrorString(cupsLastError()));
1457 snprintf(ppdenv
, sizeof(ppdenv
), "PPD=%s", ppdfile
);
1462 * Then create a temporary file for printing...
1465 if ((fd
= cupsTempFd(pstmpname
, sizeof(pstmpname
))) < 0)
1467 fprintf(stderr
, "ERROR: Unable to create temporary file - %s.\n",
1475 * Get the owner of the spool file - it is owned by the user we want to run
1480 stat(argv
[6], &fileinfo
);
1484 * Use the OSX defaults, as an up-stream filter created the PICT
1488 fileinfo
.st_uid
= 1;
1489 fileinfo
.st_gid
= 80;
1493 chown(ppdfile
, fileinfo
.st_uid
, fileinfo
.st_gid
);
1495 fchown(fd
, fileinfo
.st_uid
, fileinfo
.st_gid
);
1498 * Finally, run the filter to convert the file...
1501 if ((pid
= fork()) == 0)
1504 * Child process for pictwpstops... Redirect output of pictwpstops to a
1515 * Change to an unpriviledged user...
1518 setgid(fileinfo
.st_gid
);
1519 setuid(fileinfo
.st_uid
);
1522 execlp("pictwpstops", printer
, argv
[1], argv
[2], argv
[3], argv
[4], argv
[5],
1524 perror("ERROR: Unable to exec pictwpstops");
1536 perror("ERROR: Unable to fork pictwpstops");
1544 * Now wait for the filter to complete...
1547 if (wait(&status
) < 0)
1549 perror("ERROR: Unable to wait for pictwpstops");
1565 fprintf(stderr
, "ERROR: pictwpstops exited with status %d!\n",
1568 fprintf(stderr
, "ERROR: pictwpstops exited on signal %d!\n",
1576 * Return with no errors..
1581 #endif /* __APPLE__ */
1585 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1589 sigterm_handler(int sig
) /* I - Signal */
1591 (void)sig
; /* remove compiler warnings... */
1596 * Flag that the job should be cancelled...
1604 * The scheduler already tried to cancel us once, now just terminate
1605 * after removing our temp files!
1609 unlink(tmpfilename
);
1614 #endif /* __APPLE__ */
1621 * End of "$Id: ipp.c 5241 2006-03-07 22:07:44Z mike $".