2 * "$Id: ipp.c 181 2006-06-22 20:01:18Z jlovell $"
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 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
215 content_type
= "application/octet-stream";
218 * Extract the hostname and printer name from the URI...
221 if (httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
222 method
, sizeof(method
), username
, sizeof(username
),
223 hostname
, sizeof(hostname
), &port
,
224 resource
, sizeof(resource
)) < HTTP_URI_OK
)
226 fputs("ERROR: Missing device URI on command-line and no DEVICE_URI "
227 "environment variable!\n", stderr
);
228 return (CUPS_BACKEND_STOP
);
231 if (!strcmp(method
, "https"))
232 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
235 * See if there are any options...
243 if ((optptr
= strchr(resource
, '?')) != NULL
)
246 * Yup, terminate the device name string and move to the first
247 * character of the optptr...
253 * Then parse the optptr...
262 for (ptr
= name
; *optptr
&& *optptr
!= '=';)
263 if (ptr
< (name
+ sizeof(name
) - 1))
275 for (ptr
= value
; *optptr
&& *optptr
!= '+' && *optptr
!= '&';)
276 if (ptr
< (value
+ sizeof(value
) - 1))
280 if (*optptr
== '+' || *optptr
== '&')
287 * Process the option...
290 if (!strcasecmp(name
, "waitjob"))
293 * Wait for job completion?
296 waitjob
= !strcasecmp(value
, "on") ||
297 !strcasecmp(value
, "yes") ||
298 !strcasecmp(value
, "true");
300 else if (!strcasecmp(name
, "waitprinter"))
303 * Wait for printer idle?
306 waitprinter
= !strcasecmp(value
, "on") ||
307 !strcasecmp(value
, "yes") ||
308 !strcasecmp(value
, "true");
310 else if (!strcasecmp(name
, "encryption"))
313 * Enable/disable encryption?
316 if (!strcasecmp(value
, "always"))
317 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
318 else if (!strcasecmp(value
, "required"))
319 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
320 else if (!strcasecmp(value
, "never"))
321 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
322 else if (!strcasecmp(value
, "ifrequested"))
323 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
326 fprintf(stderr
, "ERROR: Unknown encryption option value \"%s\"!\n",
330 else if (!strcasecmp(name
, "version"))
332 if (!strcmp(value
, "1.0"))
334 else if (!strcmp(value
, "1.1"))
338 fprintf(stderr
, "ERROR: Unknown version option value \"%s\"!\n",
343 else if (!strcasecmp(name
, "compression"))
345 compression
= !strcasecmp(value
, "true") ||
346 !strcasecmp(value
, "yes") ||
347 !strcasecmp(value
, "on") ||
348 !strcasecmp(value
, "gzip");
350 #endif /* HAVE_LIBZ */
357 fprintf(stderr
, "ERROR: Unknown option \"%s\" with value \"%s\"!\n",
364 * If we have 7 arguments, print the file named on the command-line.
365 * Otherwise, copy stdin to a temporary file and print the temporary
372 * Copy stdin to a temporary file...
375 int fd
; /* File descriptor */
376 cups_file_t
*fp
; /* Temporary file */
377 char buffer
[8192]; /* Buffer for copying */
378 int bytes
; /* Number of bytes read */
381 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
383 perror("ERROR: unable to create temporary file");
384 return (CUPS_BACKEND_FAILED
);
387 if ((fp
= cupsFileOpenFd(fd
, compression
? "w9" : "w")) == NULL
)
389 perror("ERROR: unable to open temporary file");
392 return (CUPS_BACKEND_FAILED
);
395 while ((bytes
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0)
396 if (cupsFileWrite(fp
, buffer
, bytes
) < bytes
)
398 perror("ERROR: unable to write to temporary file");
401 return (CUPS_BACKEND_FAILED
);
407 * Point to the single file from stdin...
410 filename
= tmpfilename
;
417 * Point to the files on the command-line...
420 num_files
= argc
- 6;
425 compress_files(num_files
, files
);
426 #endif /* HAVE_LIBZ */
429 fprintf(stderr
, "DEBUG: %d files to send in job...\n", num_files
);
432 * Set the authentication info, if any...
435 cupsSetPasswordCB(password_cb
);
440 * Use authenticaion information in the device URI...
443 if ((password
= strchr(username
, ':')) != NULL
)
446 cupsSetUser(username
);
451 * Try loading authentication information from the a##### file.
454 const char *request_root
; /* CUPS_REQUESTROOT env var */
455 char afilename
[1024], /* a##### filename */
456 aline
[1024]; /* Line from file */
457 FILE *fp
; /* File pointer */
460 if ((request_root
= getenv("CUPS_REQUESTROOT")) != NULL
)
463 * Try opening authentication cache file...
466 snprintf(afilename
, sizeof(afilename
), "%s/a%05d", request_root
,
468 if ((fp
= fopen(afilename
, "r")) != NULL
)
474 if (fgets(aline
, sizeof(aline
), fp
))
480 i
= sizeof(username
);
481 httpDecode64_2(username
, &i
, aline
);
487 if (fgets(aline
, sizeof(aline
), fp
))
493 i
= sizeof(password
);
494 httpDecode64_2(password
, &i
, aline
);
508 * Try connecting to the remote server...
511 fputs("STATE: +connecting-to-device\n", stderr
);
515 fprintf(stderr
, "INFO: Connecting to %s on port %d...\n", hostname
, port
);
517 if ((http
= httpConnectEncrypt(hostname
, port
, cupsEncryption())) == NULL
)
519 if (getenv("CLASS") != NULL
)
522 * If the CLASS environment variable is set, the job was submitted
523 * to a class and not to a specific queue. In this case, we want
524 * to abort immediately so that the job can be requeued on the next
525 * available printer in the class.
529 "INFO: Unable to connect to %s, queuing on next printer in "
533 if (argc
== 6 || strcmp(filename
, argv
[6]))
537 * Sleep 5 seconds to keep the job from requeuing too rapidly...
542 return (CUPS_BACKEND_FAILED
);
545 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
546 errno
== EHOSTUNREACH
)
549 "INFO: Network host \'%s\' is busy; will retry in 30 "
556 fprintf(stderr
, "INFO: Unable to lookup host \'%s\' - %s\n",
557 hostname
, hstrerror(h_errno
));
562 perror("ERROR: Unable to connect to IPP host");
567 while (http
== NULL
);
569 fputs("STATE: -connecting-to-device\n", stderr
);
570 fprintf(stderr
, "INFO: Connected to %s...\n", hostname
);
573 * Build a URI for the printer and fill the standard IPP attributes for
574 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
575 * might contain username:password information...
578 snprintf(uri
, sizeof(uri
), "%s://%s:%d%s", method
, hostname
, port
, resource
);
581 * First validate the destination and see if the device supports multiple
582 * copies. We have to do this because some IPP servers (e.g. HP JetDirect)
583 * don't support the copies attribute...
593 * Build the IPP request...
596 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
597 request
->request
.op
.version
[1] = version
;
599 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
602 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
603 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
610 fputs("DEBUG: Getting supported attributes...\n", stderr
);
612 if ((supported
= cupsDoRequest(http
, request
, resource
)) == NULL
)
613 ipp_status
= cupsLastError();
615 ipp_status
= supported
->request
.status
.status_code
;
617 if (ipp_status
> IPP_OK_CONFLICT
)
619 if (ipp_status
== IPP_PRINTER_BUSY
||
620 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
622 fputs("INFO: Printer busy; will retry in 10 seconds...\n", stderr
);
623 report_printer_state(supported
);
626 else if ((ipp_status
== IPP_BAD_REQUEST
||
627 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
== 1)
630 * Switch to IPP/1.0...
633 fputs("INFO: Printer does not support IPP/1.1, trying IPP/1.0...\n",
638 else if (ipp_status
== IPP_NOT_FOUND
)
640 fputs("ERROR: Destination printer does not exist!\n", stderr
);
643 ippDelete(supported
);
645 return (CUPS_BACKEND_STOP
);
649 fprintf(stderr
, "ERROR: Unable to get printer status (%s)!\n",
650 ippErrorString(ipp_status
));
655 ippDelete(supported
);
659 else if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
660 IPP_TAG_RANGE
)) != NULL
)
663 * Has the "copies-supported" attribute - does it have an upper
667 if (copies_sup
->values
[0].range
.upper
<= 1)
668 copies_sup
= NULL
; /* No */
671 format_sup
= ippFindAttribute(supported
, "document-format-supported",
676 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
677 format_sup
->num_values
);
678 for (i
= 0; i
< format_sup
->num_values
; i
++)
679 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
680 format_sup
->values
[i
].string
.text
);
683 report_printer_state(supported
);
685 while (ipp_status
> IPP_OK_CONFLICT
);
688 * See if the printer is accepting jobs and is not stopped; if either
689 * condition is true and we are printing to a class, requeue the job...
692 if (getenv("CLASS") != NULL
)
694 printer_state
= ippFindAttribute(supported
, "printer-state",
696 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
699 if (printer_state
== NULL
||
700 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
702 printer_accepting
== NULL
||
703 !printer_accepting
->values
[0].boolean
)
706 * If the CLASS environment variable is set, the job was submitted
707 * to a class and not to a specific queue. In this case, we want
708 * to abort immediately so that the job can be requeued on the next
709 * available printer in the class.
713 "INFO: Unable to queue job on %s, queuing on next printer in "
717 ippDelete(supported
);
720 if (argc
== 6 || strcmp(filename
, argv
[6]))
724 * Sleep 5 seconds to keep the job from requeuing too rapidly...
729 return (CUPS_BACKEND_FAILED
);
734 * See if the printer supports multiple copies...
737 if (copies_sup
|| argc
< 7)
740 copies
= atoi(argv
[4]);
743 * Then issue the print-job request...
752 * Build the IPP request...
759 request
= ippNewRequest(IPP_CREATE_JOB
);
761 request
= ippNewRequest(IPP_PRINT_JOB
);
763 request
->request
.op
.version
[1] = version
;
765 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
768 fprintf(stderr
, "DEBUG: printer-uri = \"%s\"\n", uri
);
771 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
772 "requesting-user-name", NULL
, argv
[2]);
774 fprintf(stderr
, "DEBUG: requesting-user-name = \"%s\"\n", argv
[2]);
777 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
780 fprintf(stderr
, "DEBUG: job-name = \"%s\"\n", argv
[3]);
784 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
785 "compression", NULL
, "gzip");
786 #endif /* HAVE_LIBZ */
789 * Handle options on the command-line...
793 num_options
= cupsParseOptions(argv
[5], 0, &options
);
796 if (content_type
!= NULL
&&
797 !strcasecmp(content_type
, "application/pictwps") && num_files
== 1)
799 if (format_sup
!= NULL
)
801 for (i
= 0; i
< format_sup
->num_values
; i
++)
802 if (!strcasecmp(content_type
, format_sup
->values
[i
].string
.text
))
806 if (format_sup
== NULL
|| i
>= format_sup
->num_values
)
809 * Remote doesn't support "application/pictwps" (i.e. it's not MacOS X)
810 * so convert the document to PostScript...
813 if (run_pictwps_filter(argv
, filename
))
814 return (CUPS_BACKEND_FAILED
);
816 filename
= pstmpname
;
819 * Change the MIME type to application/postscript and change the
820 * number of copies to 1...
823 content_type
= "application/postscript";
827 #endif /* __APPLE__ */
829 if (content_type
!= NULL
&& format_sup
!= NULL
)
831 for (i
= 0; i
< format_sup
->num_values
; i
++)
832 if (!strcasecmp(content_type
, format_sup
->values
[i
].string
.text
))
835 if (i
< format_sup
->num_values
)
836 num_options
= cupsAddOption("document-format", content_type
,
837 num_options
, &options
);
843 * Only send options if the destination printer supports the copies
844 * attribute. This is a hack for the HP JetDirect implementation of
845 * IPP, which does not accept extension attributes and incorrectly
846 * reports a client-error-bad-request error instead of the
847 * successful-ok-unsupported-attributes status. In short, at least
848 * some HP implementations of IPP are non-compliant.
851 cupsEncodeOptions(request
, num_options
, options
);
852 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies",
856 cupsFreeOptions(num_options
, options
);
859 * If copies aren't supported, then we are likely dealing with an HP
860 * JetDirect. The HP IPP implementation seems to close the connection
861 * after every request (that is, it does *not* implement HTTP Keep-
862 * Alive, which is REQUIRED by HTTP/1.1...
873 response
= cupsDoRequest(http
, request
, resource
);
875 response
= cupsDoFileRequest(http
, request
, resource
, files
[0]);
877 ipp_status
= cupsLastError();
879 if (ipp_status
> IPP_OK_CONFLICT
)
886 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
887 ipp_status
== IPP_PRINTER_BUSY
)
889 fputs("INFO: Printer is busy; retrying print job...\n", stderr
);
893 fprintf(stderr
, "ERROR: Print file was not accepted (%s)!\n",
894 cupsLastErrorString());
896 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
897 IPP_TAG_INTEGER
)) == NULL
)
899 fputs("NOTICE: Print file accepted - job ID unknown.\n", stderr
);
904 job_id
= job_id_attr
->values
[0].integer
;
905 fprintf(stderr
, "NOTICE: Print file accepted - job ID %d.\n", job_id
);
913 if (job_id
&& num_files
> 1)
915 for (i
= 0; i
< num_files
; i
++)
917 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
919 request
->request
.op
.version
[1] = version
;
921 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
924 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
928 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
929 "requesting-user-name", NULL
, argv
[2]);
931 if ((i
+ 1) == num_files
)
932 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
934 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
935 "document-format", NULL
, content_type
);
937 ippDelete(cupsDoFileRequest(http
, request
, resource
, files
[i
]));
939 if (cupsLastError() > IPP_OK_CONFLICT
)
941 ipp_status
= cupsLastError();
943 fprintf(stderr
, "ERROR: Unable to add file %d to job: %s\n",
944 job_id
, cupsLastErrorString());
950 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
952 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
955 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
956 ipp_status
== IPP_PRINTER_BUSY
)
962 * Wait for the job to complete...
965 if (!job_id
|| !waitjob
)
968 fputs("INFO: Waiting for job to complete...\n", stderr
);
970 for (; !job_cancelled
;)
973 * Build an IPP_GET_JOB_ATTRIBUTES request...
976 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
977 request
->request
.op
.version
[1] = version
;
979 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
982 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
986 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
987 "requesting-user-name", NULL
, argv
[2]);
989 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
990 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
1000 response
= cupsDoRequest(http
, request
, resource
);
1001 ipp_status
= cupsLastError();
1003 if (ipp_status
== IPP_NOT_FOUND
)
1006 * Job has gone away and/or the server has no job history...
1009 ippDelete(response
);
1011 ipp_status
= IPP_OK
;
1015 if (ipp_status
> IPP_OK_CONFLICT
)
1017 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1018 ipp_status
!= IPP_PRINTER_BUSY
)
1020 ippDelete(response
);
1022 fprintf(stderr
, "ERROR: Unable to get job %d attributes (%s)!\n",
1023 job_id
, ippErrorString(ipp_status
));
1030 if ((job_state
= ippFindAttribute(response
, "job-state",
1031 IPP_TAG_ENUM
)) != NULL
)
1034 * Stop polling if the job is finished or pending-held...
1037 if (job_state
->values
[0].integer
> IPP_JOB_PROCESSING
||
1038 job_state
->values
[0].integer
== IPP_JOB_HELD
)
1040 if ((job_sheets
= ippFindAttribute(response
,
1041 "job-media-sheets-completed",
1042 IPP_TAG_INTEGER
)) != NULL
)
1043 fprintf(stderr
, "PAGE: total %d\n",
1044 job_sheets
->values
[0].integer
);
1046 ippDelete(response
);
1052 ippDelete(response
);
1055 * Check the printer state and report it if necessary...
1058 check_printer_state(http
, uri
, resource
, argv
[2], version
);
1061 * Wait 10 seconds before polling again...
1069 * Cancel the job as needed...
1072 if (job_cancelled
&& job_id
)
1073 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1076 * Check the printer state and report it if necessary...
1079 check_printer_state(http
, uri
, resource
, argv
[2], version
);
1087 ippDelete(supported
);
1090 * Remove the temporary file(s) if necessary...
1094 unlink(tmpfilename
);
1099 for (i
= 0; i
< num_files
; i
++)
1102 #endif /* HAVE_LIBZ */
1107 #endif /* __APPLE__ */
1110 * Return the queue status...
1113 return (ipp_status
> IPP_OK_CONFLICT
? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
1118 * 'cancel_job()' - Cancel a print job.
1122 cancel_job(http_t
*http
, /* I - HTTP connection */
1123 const char *uri
, /* I - printer-uri */
1124 int id
, /* I - job-id */
1125 const char *resource
, /* I - Resource path */
1126 const char *user
, /* I - requesting-user-name */
1127 int version
) /* I - IPP version */
1129 ipp_t
*request
; /* Cancel-Job request */
1132 fputs("INFO: Cancelling print job...\n", stderr
);
1134 request
= ippNewRequest(IPP_CANCEL_JOB
);
1135 request
->request
.op
.version
[1] = version
;
1137 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1139 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1141 if (user
&& user
[0])
1142 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1143 "requesting-user-name", NULL
, user
);
1149 ippDelete(cupsDoRequest(http
, request
, resource
));
1151 if (cupsLastError() > IPP_OK_CONFLICT
)
1152 fprintf(stderr
, "ERROR: Unable to cancel job %d: %s\n", id
,
1153 cupsLastErrorString());
1158 * 'check_printer_state()' - Check the printer state...
1162 check_printer_state(
1163 http_t
*http
, /* I - HTTP connection */
1164 const char *uri
, /* I - Printer URI */
1165 const char *resource
, /* I - Resource path */
1166 const char *user
, /* I - Username, if any */
1167 int version
) /* I - IPP version */
1169 ipp_t
*request
, /* IPP request */
1170 *response
; /* IPP response */
1174 * Check on the printer state...
1177 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1178 request
->request
.op
.version
[1] = version
;
1180 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1183 if (user
&& user
[0])
1184 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1185 "requesting-user-name", NULL
, user
);
1187 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1188 "requested-attributes", NULL
, "printer-state-reasons");
1194 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
1196 report_printer_state(response
);
1197 ippDelete(response
);
1204 * 'compress_files()' - Compress print files...
1208 compress_files(int num_files
, /* I - Number of files */
1209 char **files
) /* I - Files */
1211 int i
, /* Looping var */
1212 fd
; /* Temporary file descriptor */
1213 ssize_t bytes
; /* Bytes read/written */
1214 size_t total
; /* Total bytes read */
1215 cups_file_t
*in
, /* Input file */
1216 *out
; /* Output file */
1217 struct stat outinfo
; /* Output file information */
1218 char filename
[1024], /* Temporary filename */
1219 buffer
[32768]; /* Copy buffer */
1222 fprintf(stderr
, "DEBUG: Compressing %d job files...\n", num_files
);
1223 for (i
= 0; i
< num_files
; i
++)
1225 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
1227 perror("ERROR: Unable to create temporary compressed print file");
1228 exit(CUPS_BACKEND_FAILED
);
1231 if ((out
= cupsFileOpenFd(fd
, "w9")) == NULL
)
1233 perror("ERROR: Unable to open temporary compressed print file");
1234 exit(CUPS_BACKEND_FAILED
);
1237 if ((in
= cupsFileOpen(files
[i
], "r")) == NULL
)
1239 fprintf(stderr
, "ERROR: Unable to open print file \"%s\": %s\n",
1240 files
[i
], strerror(errno
));
1242 exit(CUPS_BACKEND_FAILED
);
1246 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
1247 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
1249 fprintf(stderr
, "ERROR: Unable to write " CUPS_LLFMT
" bytes to \"%s\": %s\n",
1250 CUPS_LLCAST bytes
, filename
, strerror(errno
));
1253 exit(CUPS_BACKEND_FAILED
);
1261 files
[i
] = strdup(filename
);
1263 if (!stat(filename
, &outinfo
))
1265 "DEBUG: File %d compressed to %.1f%% of original size, "
1266 CUPS_LLFMT
" bytes...\n",
1267 i
+ 1, 100.0 * outinfo
.st_size
/ total
,
1268 CUPS_LLCAST outinfo
.st_size
);
1271 #endif /* HAVE_LIBZ */
1275 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
1278 static const char * /* O - Password */
1279 password_cb(const char *prompt
) /* I - Prompt (not used) */
1288 * If there is no password set in the device URI, return the
1289 * "authentication required" exit code...
1293 unlink(tmpfilename
);
1298 #endif /* __APPLE__ */
1300 exit(CUPS_BACKEND_AUTH_REQUIRED
);
1306 * 'report_printer_state()' - Report the printer state.
1309 static int /* O - Number of reasons shown */
1310 report_printer_state(ipp_t
*ipp
) /* I - IPP response */
1312 int i
; /* Looping var */
1313 int count
; /* Count of reasons shown... */
1314 ipp_attribute_t
*reasons
; /* printer-state-reasons */
1315 const char *reason
; /* Current reason */
1316 const char *message
; /* Message to show */
1317 char unknown
[1024]; /* Unknown message string */
1318 const char *prefix
; /* Prefix for STATE: line */
1319 char state
[1024]; /* State string */
1322 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
1323 IPP_TAG_KEYWORD
)) == NULL
)
1329 for (i
= 0, count
= 0; i
< reasons
->num_values
; i
++)
1331 reason
= reasons
->values
[i
].string
.text
;
1333 strlcat(state
, prefix
, sizeof(state
));
1334 strlcat(state
, reason
, sizeof(state
));
1339 if (!strncmp(reason
, "media-needed", 12))
1340 message
= "Media tray needs to be filled.";
1341 else if (!strncmp(reason
, "media-jam", 9))
1342 message
= "Media jam!";
1343 else if (!strncmp(reason
, "moving-to-paused", 16) ||
1344 !strncmp(reason
, "paused", 6) ||
1345 !strncmp(reason
, "shutdown", 8))
1346 message
= "Printer off-line.";
1347 else if (!strncmp(reason
, "toner-low", 9))
1348 message
= "Toner low.";
1349 else if (!strncmp(reason
, "toner-empty", 11))
1350 message
= "Out of toner!";
1351 else if (!strncmp(reason
, "cover-open", 10))
1352 message
= "Cover open.";
1353 else if (!strncmp(reason
, "interlock-open", 14))
1354 message
= "Interlock open.";
1355 else if (!strncmp(reason
, "door-open", 9))
1356 message
= "Door open.";
1357 else if (!strncmp(reason
, "input-tray-missing", 18))
1358 message
= "Media tray missing!";
1359 else if (!strncmp(reason
, "media-low", 9))
1360 message
= "Media tray almost empty.";
1361 else if (!strncmp(reason
, "media-empty", 11))
1362 message
= "Media tray empty!";
1363 else if (!strncmp(reason
, "output-tray-missing", 19))
1364 message
= "Output tray missing!";
1365 else if (!strncmp(reason
, "output-area-almost-full", 23))
1366 message
= "Output bin almost full.";
1367 else if (!strncmp(reason
, "output-area-full", 16))
1368 message
= "Output bin full!";
1369 else if (!strncmp(reason
, "marker-supply-low", 17))
1370 message
= "Ink/toner almost empty.";
1371 else if (!strncmp(reason
, "marker-supply-empty", 19))
1372 message
= "Ink/toner empty!";
1373 else if (!strncmp(reason
, "marker-waste-almost-full", 24))
1374 message
= "Ink/toner waste bin almost full.";
1375 else if (!strncmp(reason
, "marker-waste-full", 17))
1376 message
= "Ink/toner waste bin full!";
1377 else if (!strncmp(reason
, "fuser-over-temp", 15))
1378 message
= "Fuser temperature high!";
1379 else if (!strncmp(reason
, "fuser-under-temp", 16))
1380 message
= "Fuser temperature low!";
1381 else if (!strncmp(reason
, "opc-near-eol", 12))
1382 message
= "OPC almost at end-of-life.";
1383 else if (!strncmp(reason
, "opc-life-over", 13))
1384 message
= "OPC at end-of-life!";
1385 else if (!strncmp(reason
, "developer-low", 13))
1386 message
= "Developer almost empty.";
1387 else if (!strncmp(reason
, "developer-empty", 15))
1388 message
= "Developer empty!";
1389 else if (strstr(reason
, "error") != NULL
)
1393 snprintf(unknown
, sizeof(unknown
), "Unknown printer error (%s)!",
1400 if (strstr(reasons
->values
[i
].string
.text
, "error"))
1401 fprintf(stderr
, "ERROR: %s\n", message
);
1402 else if (strstr(reasons
->values
[i
].string
.text
, "warning"))
1403 fprintf(stderr
, "WARNING: %s\n", message
);
1405 fprintf(stderr
, "INFO: %s\n", message
);
1409 fprintf(stderr
, "%s\n", state
);
1417 * 'run_pictwps_filter()' - Convert PICT files to PostScript when printing
1420 * This step is required because the PICT format is not documented and
1421 * subject to change, so developing a filter for other OS's is infeasible.
1422 * Also, fonts required by the PICT file need to be embedded on the
1423 * client side (which has the fonts), so we run the filter to get a
1424 * PostScript file for printing...
1427 static int /* O - Exit status of filter */
1428 run_pictwps_filter(char **argv
, /* I - Command-line arguments */
1429 const char *filename
)/* I - Filename */
1431 struct stat fileinfo
; /* Print file information */
1432 const char *ppdfile
; /* PPD file for destination printer */
1433 int pid
; /* Child process ID */
1434 int fd
; /* Temporary file descriptor */
1435 int status
; /* Exit status of filter */
1436 const char *printer
; /* PRINTER env var */
1437 static char ppdenv
[1024]; /* PPD environment variable */
1441 * First get the PPD file for the printer...
1444 printer
= getenv("PRINTER");
1447 fputs("ERROR: PRINTER environment variable not defined!\n", stderr
);
1451 if ((ppdfile
= cupsGetPPD(printer
)) == NULL
)
1453 fprintf(stderr
, "ERROR: Unable to get PPD file for printer \"%s\" - %s.\n",
1454 printer
, ippErrorString(cupsLastError()));
1459 snprintf(ppdenv
, sizeof(ppdenv
), "PPD=%s", ppdfile
);
1464 * Then create a temporary file for printing...
1467 if ((fd
= cupsTempFd(pstmpname
, sizeof(pstmpname
))) < 0)
1469 fprintf(stderr
, "ERROR: Unable to create temporary file - %s.\n",
1477 * Get the owner of the spool file - it is owned by the user we want to run
1482 stat(argv
[6], &fileinfo
);
1486 * Use the OSX defaults, as an up-stream filter created the PICT
1490 fileinfo
.st_uid
= 1;
1491 fileinfo
.st_gid
= 80;
1495 chown(ppdfile
, fileinfo
.st_uid
, fileinfo
.st_gid
);
1497 fchown(fd
, fileinfo
.st_uid
, fileinfo
.st_gid
);
1500 * Finally, run the filter to convert the file...
1503 if ((pid
= fork()) == 0)
1506 * Child process for pictwpstops... Redirect output of pictwpstops to a
1517 * Change to an unpriviledged user...
1520 setgid(fileinfo
.st_gid
);
1521 setuid(fileinfo
.st_uid
);
1524 execlp("pictwpstops", printer
, argv
[1], argv
[2], argv
[3], argv
[4], argv
[5],
1526 perror("ERROR: Unable to exec pictwpstops");
1538 perror("ERROR: Unable to fork pictwpstops");
1546 * Now wait for the filter to complete...
1549 if (wait(&status
) < 0)
1551 perror("ERROR: Unable to wait for pictwpstops");
1567 fprintf(stderr
, "ERROR: pictwpstops exited with status %d!\n",
1570 fprintf(stderr
, "ERROR: pictwpstops exited on signal %d!\n",
1578 * Return with no errors..
1583 #endif /* __APPLE__ */
1587 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1591 sigterm_handler(int sig
) /* I - Signal */
1593 (void)sig
; /* remove compiler warnings... */
1598 * Flag that the job should be cancelled...
1606 * The scheduler already tried to cancel us once, now just terminate
1607 * after removing our temp files!
1611 unlink(tmpfilename
);
1616 #endif /* __APPLE__ */
1623 * End of "$Id: ipp.c 181 2006-06-22 20:01:18Z jlovell $".