2 * "$Id: ipp.c 5180 2006-02-26 01:31:45Z 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...
512 fprintf(stderr
, "INFO: Connecting to %s on port %d...\n", hostname
, port
);
514 if ((http
= httpConnectEncrypt(hostname
, port
, cupsEncryption())) == NULL
)
516 if (getenv("CLASS") != NULL
)
519 * If the CLASS environment variable is set, the job was submitted
520 * to a class and not to a specific queue. In this case, we want
521 * to abort immediately so that the job can be requeued on the next
522 * available printer in the class.
526 "INFO: Unable to connect to %s, queuing on next printer in "
530 if (argc
== 6 || strcmp(filename
, argv
[6]))
534 * Sleep 5 seconds to keep the job from requeuing too rapidly...
539 return (CUPS_BACKEND_FAILED
);
542 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
543 errno
== EHOSTUNREACH
)
546 "INFO: Network host \'%s\' is busy; will retry in 30 "
553 fprintf(stderr
, "INFO: Unable to lookup host \'%s\' - %s\n",
554 hostname
, hstrerror(h_errno
));
559 perror("ERROR: Unable to connect to IPP host");
564 while (http
== NULL
);
566 fprintf(stderr
, "INFO: Connected to %s...\n", hostname
);
569 * Build a URI for the printer and fill the standard IPP attributes for
570 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
571 * might contain username:password information...
574 snprintf(uri
, sizeof(uri
), "%s://%s:%d%s", method
, hostname
, port
, resource
);
577 * First validate the destination and see if the device supports multiple
578 * copies. We have to do this because some IPP servers (e.g. HP JetDirect)
579 * don't support the copies attribute...
589 * Build the IPP request...
592 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
593 request
->request
.op
.version
[1] = version
;
595 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
598 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
599 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
606 fputs("DEBUG: Getting supported attributes...\n", stderr
);
608 if ((supported
= cupsDoRequest(http
, request
, resource
)) == NULL
)
609 ipp_status
= cupsLastError();
611 ipp_status
= supported
->request
.status
.status_code
;
613 if (ipp_status
> IPP_OK_CONFLICT
)
615 if (ipp_status
== IPP_PRINTER_BUSY
||
616 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
618 fputs("INFO: Printer busy; will retry in 10 seconds...\n", stderr
);
619 report_printer_state(supported
);
622 else if ((ipp_status
== IPP_BAD_REQUEST
||
623 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
== 1)
626 * Switch to IPP/1.0...
629 fputs("INFO: Printer does not support IPP/1.1, trying IPP/1.0...\n",
634 else if (ipp_status
== IPP_NOT_FOUND
)
636 fputs("ERROR: Destination printer does not exist!\n", stderr
);
639 ippDelete(supported
);
641 return (CUPS_BACKEND_STOP
);
645 fprintf(stderr
, "ERROR: Unable to get printer status (%s)!\n",
646 ippErrorString(ipp_status
));
651 ippDelete(supported
);
655 else if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
656 IPP_TAG_RANGE
)) != NULL
)
659 * Has the "copies-supported" attribute - does it have an upper
663 if (copies_sup
->values
[0].range
.upper
<= 1)
664 copies_sup
= NULL
; /* No */
667 format_sup
= ippFindAttribute(supported
, "document-format-supported",
672 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
673 format_sup
->num_values
);
674 for (i
= 0; i
< format_sup
->num_values
; i
++)
675 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
676 format_sup
->values
[i
].string
.text
);
679 report_printer_state(supported
);
681 while (ipp_status
> IPP_OK_CONFLICT
);
684 * See if the printer is accepting jobs and is not stopped; if either
685 * condition is true and we are printing to a class, requeue the job...
688 if (getenv("CLASS") != NULL
)
690 printer_state
= ippFindAttribute(supported
, "printer-state",
692 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
695 if (printer_state
== NULL
||
696 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
698 printer_accepting
== NULL
||
699 !printer_accepting
->values
[0].boolean
)
702 * If the CLASS environment variable is set, the job was submitted
703 * to a class and not to a specific queue. In this case, we want
704 * to abort immediately so that the job can be requeued on the next
705 * available printer in the class.
709 "INFO: Unable to queue job on %s, queuing on next printer in "
713 ippDelete(supported
);
716 if (argc
== 6 || strcmp(filename
, argv
[6]))
720 * Sleep 5 seconds to keep the job from requeuing too rapidly...
725 return (CUPS_BACKEND_FAILED
);
730 * See if the printer supports multiple copies...
733 if (copies_sup
|| argc
< 7)
736 copies
= atoi(argv
[4]);
739 * Then issue the print-job request...
748 * Build the IPP request...
755 request
= ippNewRequest(IPP_CREATE_JOB
);
757 request
= ippNewRequest(IPP_PRINT_JOB
);
759 request
->request
.op
.version
[1] = version
;
761 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
764 fprintf(stderr
, "DEBUG: printer-uri = \"%s\"\n", uri
);
767 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
768 "requesting-user-name", NULL
, argv
[2]);
770 fprintf(stderr
, "DEBUG: requesting-user-name = \"%s\"\n", argv
[2]);
773 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
776 fprintf(stderr
, "DEBUG: job-name = \"%s\"\n", argv
[3]);
780 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
781 "compression", NULL
, "gzip");
782 #endif /* HAVE_LIBZ */
785 * Handle options on the command-line...
789 num_options
= cupsParseOptions(argv
[5], 0, &options
);
792 if (content_type
!= NULL
&&
793 !strcasecmp(content_type
, "application/pictwps") && num_files
== 1)
795 if (format_sup
!= NULL
)
797 for (i
= 0; i
< format_sup
->num_values
; i
++)
798 if (!strcasecmp(content_type
, format_sup
->values
[i
].string
.text
))
802 if (format_sup
== NULL
|| i
>= format_sup
->num_values
)
805 * Remote doesn't support "application/pictwps" (i.e. it's not MacOS X)
806 * so convert the document to PostScript...
809 if (run_pictwps_filter(argv
, filename
))
810 return (CUPS_BACKEND_FAILED
);
812 filename
= pstmpname
;
815 * Change the MIME type to application/postscript and change the
816 * number of copies to 1...
819 content_type
= "application/postscript";
823 #endif /* __APPLE__ */
825 if (content_type
!= NULL
&& format_sup
!= NULL
)
827 for (i
= 0; i
< format_sup
->num_values
; i
++)
828 if (!strcasecmp(content_type
, format_sup
->values
[i
].string
.text
))
831 if (i
< format_sup
->num_values
)
832 num_options
= cupsAddOption("document-format", content_type
,
833 num_options
, &options
);
839 * Only send options if the destination printer supports the copies
840 * attribute. This is a hack for the HP JetDirect implementation of
841 * IPP, which does not accept extension attributes and incorrectly
842 * reports a client-error-bad-request error instead of the
843 * successful-ok-unsupported-attributes status. In short, at least
844 * some HP implementations of IPP are non-compliant.
847 cupsEncodeOptions(request
, num_options
, options
);
848 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies",
852 cupsFreeOptions(num_options
, options
);
855 * If copies aren't supported, then we are likely dealing with an HP
856 * JetDirect. The HP IPP implementation seems to close the connection
857 * after every request (that is, it does *not* implement HTTP Keep-
858 * Alive, which is REQUIRED by HTTP/1.1...
869 response
= cupsDoRequest(http
, request
, resource
);
871 response
= cupsDoFileRequest(http
, request
, resource
, files
[0]);
873 ipp_status
= cupsLastError();
875 if (ipp_status
> IPP_OK_CONFLICT
)
882 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
883 ipp_status
== IPP_PRINTER_BUSY
)
885 fputs("INFO: Printer is busy; retrying print job...\n", stderr
);
889 fprintf(stderr
, "ERROR: Print file was not accepted (%s)!\n",
890 cupsLastErrorString());
892 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
893 IPP_TAG_INTEGER
)) == NULL
)
895 fputs("NOTICE: Print file accepted - job ID unknown.\n", stderr
);
900 job_id
= job_id_attr
->values
[0].integer
;
901 fprintf(stderr
, "NOTICE: Print file accepted - job ID %d.\n", job_id
);
909 if (job_id
&& num_files
> 1)
911 for (i
= 0; i
< num_files
; i
++)
913 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
915 request
->request
.op
.version
[1] = version
;
917 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
920 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
924 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
925 "requesting-user-name", NULL
, argv
[2]);
927 if ((i
+ 1) == num_files
)
928 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
930 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
931 "document-format", NULL
, content_type
);
933 ippDelete(cupsDoFileRequest(http
, request
, resource
, files
[i
]));
935 if (cupsLastError() > IPP_OK_CONFLICT
)
937 ipp_status
= cupsLastError();
939 fprintf(stderr
, "ERROR: Unable to add file %d to job: %s\n",
940 job_id
, cupsLastErrorString());
946 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
948 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
951 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
952 ipp_status
== IPP_PRINTER_BUSY
)
958 * Wait for the job to complete...
961 if (!job_id
|| !waitjob
)
964 fputs("INFO: Waiting for job to complete...\n", stderr
);
966 for (; !job_cancelled
;)
969 * Build an IPP_GET_JOB_ATTRIBUTES request...
972 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
973 request
->request
.op
.version
[1] = version
;
975 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
978 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
982 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
983 "requesting-user-name", NULL
, argv
[2]);
985 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
986 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
996 response
= cupsDoRequest(http
, request
, resource
);
997 ipp_status
= cupsLastError();
999 if (ipp_status
== IPP_NOT_FOUND
)
1002 * Job has gone away and/or the server has no job history...
1005 ippDelete(response
);
1007 ipp_status
= IPP_OK
;
1011 if (ipp_status
> IPP_OK_CONFLICT
)
1013 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1014 ipp_status
!= IPP_PRINTER_BUSY
)
1016 ippDelete(response
);
1018 fprintf(stderr
, "ERROR: Unable to get job %d attributes (%s)!\n",
1019 job_id
, ippErrorString(ipp_status
));
1026 if ((job_state
= ippFindAttribute(response
, "job-state",
1027 IPP_TAG_ENUM
)) != NULL
)
1030 * Stop polling if the job is finished or pending-held...
1033 if (job_state
->values
[0].integer
> IPP_JOB_PROCESSING
||
1034 job_state
->values
[0].integer
== IPP_JOB_HELD
)
1036 if ((job_sheets
= ippFindAttribute(response
,
1037 "job-media-sheets-completed",
1038 IPP_TAG_INTEGER
)) != NULL
)
1039 fprintf(stderr
, "PAGE: total %d\n",
1040 job_sheets
->values
[0].integer
);
1042 ippDelete(response
);
1048 ippDelete(response
);
1051 * Check the printer state and report it if necessary...
1054 check_printer_state(http
, uri
, resource
, argv
[2], version
);
1057 * Wait 10 seconds before polling again...
1065 * Cancel the job as needed...
1068 if (job_cancelled
&& job_id
)
1069 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1072 * Check the printer state and report it if necessary...
1075 check_printer_state(http
, uri
, resource
, argv
[2], version
);
1083 ippDelete(supported
);
1086 * Remove the temporary file(s) if necessary...
1090 unlink(tmpfilename
);
1095 for (i
= 0; i
< num_files
; i
++)
1098 #endif /* HAVE_LIBZ */
1103 #endif /* __APPLE__ */
1106 * Return the queue status...
1109 return (ipp_status
> IPP_OK_CONFLICT
? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
1114 * 'cancel_job()' - Cancel a print job.
1118 cancel_job(http_t
*http
, /* I - HTTP connection */
1119 const char *uri
, /* I - printer-uri */
1120 int id
, /* I - job-id */
1121 const char *resource
, /* I - Resource path */
1122 const char *user
, /* I - requesting-user-name */
1123 int version
) /* I - IPP version */
1125 ipp_t
*request
; /* Cancel-Job request */
1128 fputs("INFO: Cancelling print job...\n", stderr
);
1130 request
= ippNewRequest(IPP_CANCEL_JOB
);
1131 request
->request
.op
.version
[1] = version
;
1133 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1135 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1137 if (user
&& user
[0])
1138 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1139 "requesting-user-name", NULL
, user
);
1145 ippDelete(cupsDoRequest(http
, request
, resource
));
1147 if (cupsLastError() > IPP_OK_CONFLICT
)
1148 fprintf(stderr
, "ERROR: Unable to cancel job %d: %s\n", id
,
1149 cupsLastErrorString());
1154 * 'check_printer_state()' - Check the printer state...
1158 check_printer_state(
1159 http_t
*http
, /* I - HTTP connection */
1160 const char *uri
, /* I - Printer URI */
1161 const char *resource
, /* I - Resource path */
1162 const char *user
, /* I - Username, if any */
1163 int version
) /* I - IPP version */
1165 ipp_t
*request
, /* IPP request */
1166 *response
; /* IPP response */
1170 * Check on the printer state...
1173 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1174 request
->request
.op
.version
[1] = version
;
1176 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1179 if (user
&& user
[0])
1180 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1181 "requesting-user-name", NULL
, user
);
1183 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1184 "requested-attributes", NULL
, "printer-state-reasons");
1190 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
1192 report_printer_state(response
);
1193 ippDelete(response
);
1200 * 'compress_files()' - Compress print files...
1204 compress_files(int num_files
, /* I - Number of files */
1205 char **files
) /* I - Files */
1207 int i
, /* Looping var */
1208 fd
; /* Temporary file descriptor */
1209 ssize_t bytes
; /* Bytes read/written */
1210 size_t total
; /* Total bytes read */
1211 cups_file_t
*in
, /* Input file */
1212 *out
; /* Output file */
1213 struct stat outinfo
; /* Output file information */
1214 char filename
[1024], /* Temporary filename */
1215 buffer
[65536]; /* Copy buffer */
1218 fprintf(stderr
, "DEBUG: Compressing %d job files...\n", num_files
);
1219 for (i
= 0; i
< num_files
; i
++)
1221 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
1223 perror("ERROR: Unable to create temporary compressed print file");
1224 exit(CUPS_BACKEND_FAILED
);
1227 if ((out
= cupsFileOpenFd(fd
, "w9")) == NULL
)
1229 perror("ERROR: Unable to open temporary compressed print file");
1230 exit(CUPS_BACKEND_FAILED
);
1233 if ((in
= cupsFileOpen(files
[i
], "r")) == NULL
)
1235 fprintf(stderr
, "ERROR: Unable to open print file \"%s\": %s\n",
1236 files
[i
], strerror(errno
));
1238 exit(CUPS_BACKEND_FAILED
);
1242 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
1243 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
1245 fprintf(stderr
, "ERROR: Unable to write %d bytes to \"%s\": %s\n",
1246 bytes
, filename
, strerror(errno
));
1249 exit(CUPS_BACKEND_FAILED
);
1257 files
[i
] = strdup(filename
);
1259 if (!stat(filename
, &outinfo
))
1261 "DEBUG: File %d compressed to %.1f%% of original size, "
1262 CUPS_LLFMT
" bytes...\n",
1263 i
+ 1, 100.0 * outinfo
.st_size
/ total
, outinfo
.st_size
);
1266 #endif /* HAVE_LIBZ */
1270 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
1273 static const char * /* O - Password */
1274 password_cb(const char *prompt
) /* I - Prompt (not used) */
1283 * If there is no password set in the device URI, return the
1284 * "authentication required" exit code...
1288 unlink(tmpfilename
);
1293 #endif /* __APPLE__ */
1295 exit(CUPS_BACKEND_AUTH_REQUIRED
);
1301 * 'report_printer_state()' - Report the printer state.
1304 static int /* O - Number of reasons shown */
1305 report_printer_state(ipp_t
*ipp
) /* I - IPP response */
1307 int i
; /* Looping var */
1308 int count
; /* Count of reasons shown... */
1309 ipp_attribute_t
*reasons
; /* printer-state-reasons */
1310 const char *reason
; /* Current reason */
1311 const char *message
; /* Message to show */
1312 char unknown
[1024]; /* Unknown message string */
1313 const char *prefix
; /* Prefix for STATE: line */
1314 char state
[1024]; /* State string */
1317 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
1318 IPP_TAG_KEYWORD
)) == NULL
)
1324 for (i
= 0, count
= 0; i
< reasons
->num_values
; i
++)
1326 reason
= reasons
->values
[i
].string
.text
;
1328 strlcat(state
, prefix
, sizeof(state
));
1329 strlcat(state
, reason
, sizeof(state
));
1334 if (!strncmp(reason
, "media-needed", 12))
1335 message
= "Media tray needs to be filled.";
1336 else if (!strncmp(reason
, "media-jam", 9))
1337 message
= "Media jam!";
1338 else if (!strncmp(reason
, "moving-to-paused", 16) ||
1339 !strncmp(reason
, "paused", 6) ||
1340 !strncmp(reason
, "shutdown", 8))
1341 message
= "Printer off-line.";
1342 else if (!strncmp(reason
, "toner-low", 9))
1343 message
= "Toner low.";
1344 else if (!strncmp(reason
, "toner-empty", 11))
1345 message
= "Out of toner!";
1346 else if (!strncmp(reason
, "cover-open", 10))
1347 message
= "Cover open.";
1348 else if (!strncmp(reason
, "interlock-open", 14))
1349 message
= "Interlock open.";
1350 else if (!strncmp(reason
, "door-open", 9))
1351 message
= "Door open.";
1352 else if (!strncmp(reason
, "input-tray-missing", 18))
1353 message
= "Media tray missing!";
1354 else if (!strncmp(reason
, "media-low", 9))
1355 message
= "Media tray almost empty.";
1356 else if (!strncmp(reason
, "media-empty", 11))
1357 message
= "Media tray empty!";
1358 else if (!strncmp(reason
, "output-tray-missing", 19))
1359 message
= "Output tray missing!";
1360 else if (!strncmp(reason
, "output-area-almost-full", 23))
1361 message
= "Output bin almost full.";
1362 else if (!strncmp(reason
, "output-area-full", 16))
1363 message
= "Output bin full!";
1364 else if (!strncmp(reason
, "marker-supply-low", 17))
1365 message
= "Ink/toner almost empty.";
1366 else if (!strncmp(reason
, "marker-supply-empty", 19))
1367 message
= "Ink/toner empty!";
1368 else if (!strncmp(reason
, "marker-waste-almost-full", 24))
1369 message
= "Ink/toner waste bin almost full.";
1370 else if (!strncmp(reason
, "marker-waste-full", 17))
1371 message
= "Ink/toner waste bin full!";
1372 else if (!strncmp(reason
, "fuser-over-temp", 15))
1373 message
= "Fuser temperature high!";
1374 else if (!strncmp(reason
, "fuser-under-temp", 16))
1375 message
= "Fuser temperature low!";
1376 else if (!strncmp(reason
, "opc-near-eol", 12))
1377 message
= "OPC almost at end-of-life.";
1378 else if (!strncmp(reason
, "opc-life-over", 13))
1379 message
= "OPC at end-of-life!";
1380 else if (!strncmp(reason
, "developer-low", 13))
1381 message
= "Developer almost empty.";
1382 else if (!strncmp(reason
, "developer-empty", 15))
1383 message
= "Developer empty!";
1384 else if (strstr(reason
, "error") != NULL
)
1388 snprintf(unknown
, sizeof(unknown
), "Unknown printer error (%s)!",
1395 if (strstr(reasons
->values
[i
].string
.text
, "error"))
1396 fprintf(stderr
, "ERROR: %s\n", message
);
1397 else if (strstr(reasons
->values
[i
].string
.text
, "warning"))
1398 fprintf(stderr
, "WARNING: %s\n", message
);
1400 fprintf(stderr
, "INFO: %s\n", message
);
1404 fprintf(stderr
, "%s\n", state
);
1412 * 'run_pictwps_filter()' - Convert PICT files to PostScript when printing
1415 * This step is required because the PICT format is not documented and
1416 * subject to change, so developing a filter for other OS's is infeasible.
1417 * Also, fonts required by the PICT file need to be embedded on the
1418 * client side (which has the fonts), so we run the filter to get a
1419 * PostScript file for printing...
1422 static int /* O - Exit status of filter */
1423 run_pictwps_filter(char **argv
, /* I - Command-line arguments */
1424 const char *filename
)/* I - Filename */
1426 struct stat fileinfo
; /* Print file information */
1427 const char *ppdfile
; /* PPD file for destination printer */
1428 int pid
; /* Child process ID */
1429 int fd
; /* Temporary file descriptor */
1430 int status
; /* Exit status of filter */
1431 const char *printer
; /* PRINTER env var */
1432 static char ppdenv
[1024]; /* PPD environment variable */
1436 * First get the PPD file for the printer...
1439 printer
= getenv("PRINTER");
1442 fputs("ERROR: PRINTER environment variable not defined!\n", stderr
);
1446 if ((ppdfile
= cupsGetPPD(printer
)) == NULL
)
1448 fprintf(stderr
, "ERROR: Unable to get PPD file for printer \"%s\" - %s.\n",
1449 printer
, ippErrorString(cupsLastError()));
1454 snprintf(ppdenv
, sizeof(ppdenv
), "PPD=%s", ppdfile
);
1459 * Then create a temporary file for printing...
1462 if ((fd
= cupsTempFd(pstmpname
, sizeof(pstmpname
))) < 0)
1464 fprintf(stderr
, "ERROR: Unable to create temporary file - %s.\n",
1472 * Get the owner of the spool file - it is owned by the user we want to run
1477 stat(argv
[6], &fileinfo
);
1481 * Use the OSX defaults, as an up-stream filter created the PICT
1485 fileinfo
.st_uid
= 1;
1486 fileinfo
.st_gid
= 80;
1490 chown(ppdfile
, fileinfo
.st_uid
, fileinfo
.st_gid
);
1492 fchown(fd
, fileinfo
.st_uid
, fileinfo
.st_gid
);
1495 * Finally, run the filter to convert the file...
1498 if ((pid
= fork()) == 0)
1501 * Child process for pictwpstops... Redirect output of pictwpstops to a
1512 * Change to an unpriviledged user...
1515 setgid(fileinfo
.st_gid
);
1516 setuid(fileinfo
.st_uid
);
1519 execlp("pictwpstops", printer
, argv
[1], argv
[2], argv
[3], argv
[4], argv
[5],
1521 perror("ERROR: Unable to exec pictwpstops");
1533 perror("ERROR: Unable to fork pictwpstops");
1541 * Now wait for the filter to complete...
1544 if (wait(&status
) < 0)
1546 perror("ERROR: Unable to wait for pictwpstops");
1562 fprintf(stderr
, "ERROR: pictwpstops exited with status %d!\n",
1565 fprintf(stderr
, "ERROR: pictwpstops exited on signal %d!\n",
1573 * Return with no errors..
1578 #endif /* __APPLE__ */
1582 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1586 sigterm_handler(int sig
) /* I - Signal */
1588 (void)sig
; /* remove compiler warnings... */
1593 * Flag that the job should be cancelled...
1601 * The scheduler already tried to cancel us once, now just terminate
1602 * after removing our temp files!
1606 unlink(tmpfilename
);
1611 #endif /* __APPLE__ */
1618 * End of "$Id: ipp.c 5180 2006-02-26 01:31:45Z mike $".