2 * "$Id: ipp.c 5956 2006-09-13 18:22:34Z 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 for job */
136 copies_remaining
; /* Number of copies remaining */
137 const char *content_type
; /* CONTENT_TYPE environment variable */
138 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
139 struct sigaction action
; /* Actions for POSIX signals */
140 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
141 int version
; /* IPP version */
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";
217 if (!strncmp(content_type
, "printer/", 8))
218 content_type
= "application/vnd.cups-raw";
221 * Extract the hostname and printer name from the URI...
224 if (httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
225 method
, sizeof(method
), username
, sizeof(username
),
226 hostname
, sizeof(hostname
), &port
,
227 resource
, sizeof(resource
)) < HTTP_URI_OK
)
229 fputs("ERROR: Missing device URI on command-line and no DEVICE_URI "
230 "environment variable!\n", stderr
);
231 return (CUPS_BACKEND_STOP
);
234 if (!strcmp(method
, "https"))
235 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
237 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
240 * See if there are any options...
248 if ((optptr
= strchr(resource
, '?')) != NULL
)
251 * Yup, terminate the device name string and move to the first
252 * character of the optptr...
258 * Then parse the optptr...
267 for (ptr
= name
; *optptr
&& *optptr
!= '=';)
268 if (ptr
< (name
+ sizeof(name
) - 1))
280 for (ptr
= value
; *optptr
&& *optptr
!= '+' && *optptr
!= '&';)
281 if (ptr
< (value
+ sizeof(value
) - 1))
285 if (*optptr
== '+' || *optptr
== '&')
292 * Process the option...
295 if (!strcasecmp(name
, "waitjob"))
298 * Wait for job completion?
301 waitjob
= !strcasecmp(value
, "on") ||
302 !strcasecmp(value
, "yes") ||
303 !strcasecmp(value
, "true");
305 else if (!strcasecmp(name
, "waitprinter"))
308 * Wait for printer idle?
311 waitprinter
= !strcasecmp(value
, "on") ||
312 !strcasecmp(value
, "yes") ||
313 !strcasecmp(value
, "true");
315 else if (!strcasecmp(name
, "encryption"))
318 * Enable/disable encryption?
321 if (!strcasecmp(value
, "always"))
322 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
323 else if (!strcasecmp(value
, "required"))
324 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
325 else if (!strcasecmp(value
, "never"))
326 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
327 else if (!strcasecmp(value
, "ifrequested"))
328 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
331 fprintf(stderr
, "ERROR: Unknown encryption option value \"%s\"!\n",
335 else if (!strcasecmp(name
, "version"))
337 if (!strcmp(value
, "1.0"))
339 else if (!strcmp(value
, "1.1"))
343 fprintf(stderr
, "ERROR: Unknown version option value \"%s\"!\n",
348 else if (!strcasecmp(name
, "compression"))
350 compression
= !strcasecmp(value
, "true") ||
351 !strcasecmp(value
, "yes") ||
352 !strcasecmp(value
, "on") ||
353 !strcasecmp(value
, "gzip");
355 #endif /* HAVE_LIBZ */
362 fprintf(stderr
, "ERROR: Unknown option \"%s\" with value \"%s\"!\n",
369 * If we have 7 arguments, print the file named on the command-line.
370 * Otherwise, copy stdin to a temporary file and print the temporary
377 * Copy stdin to a temporary file...
380 int fd
; /* File descriptor */
381 cups_file_t
*fp
; /* Temporary file */
382 char buffer
[8192]; /* Buffer for copying */
383 int bytes
; /* Number of bytes read */
386 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
388 perror("ERROR: unable to create temporary file");
389 return (CUPS_BACKEND_FAILED
);
392 if ((fp
= cupsFileOpenFd(fd
, compression
? "w9" : "w")) == NULL
)
394 perror("ERROR: unable to open temporary file");
397 return (CUPS_BACKEND_FAILED
);
400 while ((bytes
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0)
401 if (cupsFileWrite(fp
, buffer
, bytes
) < bytes
)
403 perror("ERROR: unable to write to temporary file");
406 return (CUPS_BACKEND_FAILED
);
412 * Point to the single file from stdin...
415 filename
= tmpfilename
;
422 * Point to the files on the command-line...
425 num_files
= argc
- 6;
430 compress_files(num_files
, files
);
431 #endif /* HAVE_LIBZ */
434 fprintf(stderr
, "DEBUG: %d files to send in job...\n", num_files
);
437 * Set the authentication info, if any...
440 cupsSetPasswordCB(password_cb
);
445 * Use authenticaion information in the device URI...
448 if ((password
= strchr(username
, ':')) != NULL
)
451 cupsSetUser(username
);
456 * Try loading authentication information from the a##### file.
459 const char *request_root
; /* CUPS_REQUESTROOT env var */
460 char afilename
[1024], /* a##### filename */
461 aline
[1024]; /* Line from file */
462 FILE *fp
; /* File pointer */
465 if ((request_root
= getenv("CUPS_REQUESTROOT")) != NULL
)
468 * Try opening authentication cache file...
471 snprintf(afilename
, sizeof(afilename
), "%s/a%05d", request_root
,
473 if ((fp
= fopen(afilename
, "r")) != NULL
)
479 if (fgets(aline
, sizeof(aline
), fp
))
485 i
= sizeof(username
);
486 httpDecode64_2(username
, &i
, aline
);
492 if (fgets(aline
, sizeof(aline
), fp
))
498 i
= sizeof(password
);
499 httpDecode64_2(password
, &i
, aline
);
513 * Try connecting to the remote server...
516 fputs("STATE: +connecting-to-device\n", stderr
);
520 fprintf(stderr
, "INFO: Connecting to %s on port %d...\n", hostname
, port
);
522 if ((http
= httpConnectEncrypt(hostname
, port
, cupsEncryption())) == NULL
)
524 if (getenv("CLASS") != NULL
)
527 * If the CLASS environment variable is set, the job was submitted
528 * to a class and not to a specific queue. In this case, we want
529 * to abort immediately so that the job can be requeued on the next
530 * available printer in the class.
534 "INFO: Unable to connect to %s, queuing on next printer in "
538 if (argc
== 6 || strcmp(filename
, argv
[6]))
542 * Sleep 5 seconds to keep the job from requeuing too rapidly...
547 return (CUPS_BACKEND_FAILED
);
550 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
551 errno
== EHOSTUNREACH
)
554 "INFO: Network host \'%s\' is busy; will retry in 30 "
561 fprintf(stderr
, "INFO: Unable to lookup host \'%s\' - %s\n",
562 hostname
, hstrerror(h_errno
));
567 perror("ERROR: Unable to connect to IPP host");
572 while (http
== NULL
);
574 fputs("STATE: -connecting-to-device\n", stderr
);
575 fprintf(stderr
, "INFO: Connected to %s...\n", hostname
);
578 * Build a URI for the printer and fill the standard IPP attributes for
579 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
580 * might contain username:password information...
583 snprintf(uri
, sizeof(uri
), "%s://%s:%d%s", method
, hostname
, port
, resource
);
586 * First validate the destination and see if the device supports multiple
587 * copies. We have to do this because some IPP servers (e.g. HP JetDirect)
588 * don't support the copies attribute...
598 * Build the IPP request...
601 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
602 request
->request
.op
.version
[1] = version
;
604 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
607 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
608 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
615 fputs("DEBUG: Getting supported attributes...\n", stderr
);
617 if ((supported
= cupsDoRequest(http
, request
, resource
)) == NULL
)
618 ipp_status
= cupsLastError();
620 ipp_status
= supported
->request
.status
.status_code
;
622 if (ipp_status
> IPP_OK_CONFLICT
)
624 if (ipp_status
== IPP_PRINTER_BUSY
||
625 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
627 fputs("INFO: Printer busy; will retry in 10 seconds...\n", stderr
);
628 report_printer_state(supported
);
631 else if ((ipp_status
== IPP_BAD_REQUEST
||
632 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
== 1)
635 * Switch to IPP/1.0...
638 fputs("INFO: Printer does not support IPP/1.1, trying IPP/1.0...\n",
643 else if (ipp_status
== IPP_NOT_FOUND
)
645 fputs("ERROR: Destination printer does not exist!\n", stderr
);
648 ippDelete(supported
);
650 return (CUPS_BACKEND_STOP
);
654 fprintf(stderr
, "ERROR: Unable to get printer status (%s)!\n",
655 ippErrorString(ipp_status
));
660 ippDelete(supported
);
664 else if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
665 IPP_TAG_RANGE
)) != NULL
)
668 * Has the "copies-supported" attribute - does it have an upper
672 if (copies_sup
->values
[0].range
.upper
<= 1)
673 copies_sup
= NULL
; /* No */
676 format_sup
= ippFindAttribute(supported
, "document-format-supported",
681 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
682 format_sup
->num_values
);
683 for (i
= 0; i
< format_sup
->num_values
; i
++)
684 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
685 format_sup
->values
[i
].string
.text
);
688 report_printer_state(supported
);
690 while (ipp_status
> IPP_OK_CONFLICT
);
693 * See if the printer is accepting jobs and is not stopped; if either
694 * condition is true and we are printing to a class, requeue the job...
697 if (getenv("CLASS") != NULL
)
699 printer_state
= ippFindAttribute(supported
, "printer-state",
701 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
704 if (printer_state
== NULL
||
705 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
707 printer_accepting
== NULL
||
708 !printer_accepting
->values
[0].boolean
)
711 * If the CLASS environment variable is set, the job was submitted
712 * to a class and not to a specific queue. In this case, we want
713 * to abort immediately so that the job can be requeued on the next
714 * available printer in the class.
718 "INFO: Unable to queue job on %s, queuing on next printer in "
722 ippDelete(supported
);
725 if (argc
== 6 || strcmp(filename
, argv
[6]))
729 * Sleep 5 seconds to keep the job from requeuing too rapidly...
734 return (CUPS_BACKEND_FAILED
);
739 * See if the printer supports multiple copies...
742 copies
= atoi(argv
[4]);
744 if (copies_sup
|| argc
< 7)
746 copies_remaining
= 1;
752 copies_remaining
= copies
;
755 * Then issue the print-job request...
760 while (copies_remaining
> 0)
763 * Build the IPP request...
770 request
= ippNewRequest(IPP_CREATE_JOB
);
772 request
= ippNewRequest(IPP_PRINT_JOB
);
774 request
->request
.op
.version
[1] = version
;
776 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
779 fprintf(stderr
, "DEBUG: printer-uri = \"%s\"\n", uri
);
782 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
783 "requesting-user-name", NULL
, argv
[2]);
785 fprintf(stderr
, "DEBUG: requesting-user-name = \"%s\"\n", argv
[2]);
788 * Only add a "job-name" attribute if the remote server supports
789 * copy generation - some IPP implementations like HP's don't seem
790 * to like UTF-8 job names (STR #1837)...
793 if (argv
[3][0] && copies_sup
)
794 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
797 fprintf(stderr
, "DEBUG: job-name = \"%s\"\n", argv
[3]);
801 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
802 "compression", NULL
, "gzip");
803 #endif /* HAVE_LIBZ */
806 * Handle options on the command-line...
810 num_options
= cupsParseOptions(argv
[5], 0, &options
);
813 if (content_type
!= NULL
&&
814 !strcasecmp(content_type
, "application/pictwps") && num_files
== 1)
816 if (format_sup
!= NULL
)
818 for (i
= 0; i
< format_sup
->num_values
; i
++)
819 if (!strcasecmp(content_type
, format_sup
->values
[i
].string
.text
))
823 if (format_sup
== NULL
|| i
>= format_sup
->num_values
)
826 * Remote doesn't support "application/pictwps" (i.e. it's not MacOS X)
827 * so convert the document to PostScript...
830 if (run_pictwps_filter(argv
, filename
))
831 return (CUPS_BACKEND_FAILED
);
833 filename
= pstmpname
;
836 * Change the MIME type to application/postscript and change the
837 * number of copies to 1...
840 content_type
= "application/postscript";
842 copies_remaining
= 1;
845 #endif /* __APPLE__ */
847 if (content_type
!= NULL
&& format_sup
!= NULL
)
849 for (i
= 0; i
< format_sup
->num_values
; i
++)
850 if (!strcasecmp(content_type
, format_sup
->values
[i
].string
.text
))
853 if (i
< format_sup
->num_values
)
854 num_options
= cupsAddOption("document-format", content_type
,
855 num_options
, &options
);
858 if (copies_sup
&& version
> 0)
861 * Only send options if the destination printer supports the copies
862 * attribute and IPP/1.1. This is a hack for the HP and Lexmark
863 * implementations of IPP, which do not accept extension attributes
864 * and incorrectly report a client-error-bad-request error instead of
865 * the successful-ok-unsupported-attributes status. In short, at least
866 * some HP and Lexmark implementations of IPP are non-compliant.
869 cupsEncodeOptions(request
, num_options
, options
);
871 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies",
875 cupsFreeOptions(num_options
, options
);
878 * If copies aren't supported, then we are likely dealing with an HP
879 * JetDirect. The HP IPP implementation seems to close the connection
880 * after every request - that is, it does *not* implement HTTP Keep-
881 * Alive, which is REQUIRED by HTTP/1.1...
892 response
= cupsDoRequest(http
, request
, resource
);
894 response
= cupsDoFileRequest(http
, request
, resource
, files
[0]);
896 ipp_status
= cupsLastError();
898 if (ipp_status
> IPP_OK_CONFLICT
)
905 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
906 ipp_status
== IPP_PRINTER_BUSY
)
908 fputs("INFO: Printer is busy; retrying print job...\n", stderr
);
911 else if ((ipp_status
== IPP_BAD_REQUEST
||
912 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
== 1)
915 * Switch to IPP/1.0...
918 fputs("INFO: Printer does not support IPP/1.1, trying IPP/1.0...\n",
924 fprintf(stderr
, "ERROR: Print file was not accepted (%s)!\n",
925 cupsLastErrorString());
927 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
928 IPP_TAG_INTEGER
)) == NULL
)
930 fputs("NOTICE: Print file accepted - job ID unknown.\n", stderr
);
935 job_id
= job_id_attr
->values
[0].integer
;
936 fprintf(stderr
, "NOTICE: Print file accepted - job ID %d.\n", job_id
);
944 if (job_id
&& num_files
> 1)
946 for (i
= 0; i
< num_files
; i
++)
948 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
950 request
->request
.op
.version
[1] = version
;
952 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
955 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
959 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
960 "requesting-user-name", NULL
, argv
[2]);
962 if ((i
+ 1) == num_files
)
963 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
965 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
966 "document-format", NULL
, content_type
);
968 ippDelete(cupsDoFileRequest(http
, request
, resource
, files
[i
]));
970 if (cupsLastError() > IPP_OK_CONFLICT
)
972 ipp_status
= cupsLastError();
974 fprintf(stderr
, "ERROR: Unable to add file %d to job: %s\n",
975 job_id
, cupsLastErrorString());
981 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
983 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
986 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
987 ipp_status
== IPP_PRINTER_BUSY
)
993 * Wait for the job to complete...
996 if (!job_id
|| !waitjob
)
999 fputs("INFO: Waiting for job to complete...\n", stderr
);
1001 for (; !job_cancelled
;)
1004 * Build an IPP_GET_JOB_ATTRIBUTES request...
1007 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1008 request
->request
.op
.version
[1] = version
;
1010 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1013 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1017 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1018 "requesting-user-name", NULL
, argv
[2]);
1020 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1021 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
1029 httpReconnect(http
);
1031 response
= cupsDoRequest(http
, request
, resource
);
1032 ipp_status
= cupsLastError();
1034 if (ipp_status
== IPP_NOT_FOUND
)
1037 * Job has gone away and/or the server has no job history...
1040 ippDelete(response
);
1042 ipp_status
= IPP_OK
;
1046 if (ipp_status
> IPP_OK_CONFLICT
)
1048 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1049 ipp_status
!= IPP_PRINTER_BUSY
)
1051 ippDelete(response
);
1053 fprintf(stderr
, "ERROR: Unable to get job %d attributes (%s)!\n",
1054 job_id
, ippErrorString(ipp_status
));
1061 if ((job_state
= ippFindAttribute(response
, "job-state",
1062 IPP_TAG_ENUM
)) != NULL
)
1065 * Stop polling if the job is finished or pending-held...
1068 if (job_state
->values
[0].integer
> IPP_JOB_PROCESSING
||
1069 job_state
->values
[0].integer
== IPP_JOB_HELD
)
1071 if ((job_sheets
= ippFindAttribute(response
,
1072 "job-media-sheets-completed",
1073 IPP_TAG_INTEGER
)) != NULL
)
1074 fprintf(stderr
, "PAGE: total %d\n",
1075 job_sheets
->values
[0].integer
);
1077 ippDelete(response
);
1083 ippDelete(response
);
1086 * Check the printer state and report it if necessary...
1089 check_printer_state(http
, uri
, resource
, argv
[2], version
);
1092 * Wait 10 seconds before polling again...
1100 * Cancel the job as needed...
1103 if (job_cancelled
&& job_id
)
1104 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1107 * Check the printer state and report it if necessary...
1110 check_printer_state(http
, uri
, resource
, argv
[2], version
);
1118 ippDelete(supported
);
1121 * Remove the temporary file(s) if necessary...
1125 unlink(tmpfilename
);
1130 for (i
= 0; i
< num_files
; i
++)
1133 #endif /* HAVE_LIBZ */
1138 #endif /* __APPLE__ */
1141 * Return the queue status...
1144 return (ipp_status
> IPP_OK_CONFLICT
? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
1149 * 'cancel_job()' - Cancel a print job.
1153 cancel_job(http_t
*http
, /* I - HTTP connection */
1154 const char *uri
, /* I - printer-uri */
1155 int id
, /* I - job-id */
1156 const char *resource
, /* I - Resource path */
1157 const char *user
, /* I - requesting-user-name */
1158 int version
) /* I - IPP version */
1160 ipp_t
*request
; /* Cancel-Job request */
1163 fputs("INFO: Cancelling print job...\n", stderr
);
1165 request
= ippNewRequest(IPP_CANCEL_JOB
);
1166 request
->request
.op
.version
[1] = version
;
1168 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1170 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1172 if (user
&& user
[0])
1173 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1174 "requesting-user-name", NULL
, user
);
1180 ippDelete(cupsDoRequest(http
, request
, resource
));
1182 if (cupsLastError() > IPP_OK_CONFLICT
)
1183 fprintf(stderr
, "ERROR: Unable to cancel job %d: %s\n", id
,
1184 cupsLastErrorString());
1189 * 'check_printer_state()' - Check the printer state...
1193 check_printer_state(
1194 http_t
*http
, /* I - HTTP connection */
1195 const char *uri
, /* I - Printer URI */
1196 const char *resource
, /* I - Resource path */
1197 const char *user
, /* I - Username, if any */
1198 int version
) /* I - IPP version */
1200 ipp_t
*request
, /* IPP request */
1201 *response
; /* IPP response */
1205 * Check on the printer state...
1208 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1209 request
->request
.op
.version
[1] = version
;
1211 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1214 if (user
&& user
[0])
1215 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1216 "requesting-user-name", NULL
, user
);
1218 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1219 "requested-attributes", NULL
, "printer-state-reasons");
1225 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
1227 report_printer_state(response
);
1228 ippDelete(response
);
1235 * 'compress_files()' - Compress print files...
1239 compress_files(int num_files
, /* I - Number of files */
1240 char **files
) /* I - Files */
1242 int i
, /* Looping var */
1243 fd
; /* Temporary file descriptor */
1244 ssize_t bytes
; /* Bytes read/written */
1245 size_t total
; /* Total bytes read */
1246 cups_file_t
*in
, /* Input file */
1247 *out
; /* Output file */
1248 struct stat outinfo
; /* Output file information */
1249 char filename
[1024], /* Temporary filename */
1250 buffer
[32768]; /* Copy buffer */
1253 fprintf(stderr
, "DEBUG: Compressing %d job files...\n", num_files
);
1254 for (i
= 0; i
< num_files
; i
++)
1256 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
1258 perror("ERROR: Unable to create temporary compressed print file");
1259 exit(CUPS_BACKEND_FAILED
);
1262 if ((out
= cupsFileOpenFd(fd
, "w9")) == NULL
)
1264 perror("ERROR: Unable to open temporary compressed print file");
1265 exit(CUPS_BACKEND_FAILED
);
1268 if ((in
= cupsFileOpen(files
[i
], "r")) == NULL
)
1270 fprintf(stderr
, "ERROR: Unable to open print file \"%s\": %s\n",
1271 files
[i
], strerror(errno
));
1273 exit(CUPS_BACKEND_FAILED
);
1277 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
1278 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
1280 fprintf(stderr
, "ERROR: Unable to write " CUPS_LLFMT
" bytes to \"%s\": %s\n",
1281 CUPS_LLCAST bytes
, filename
, strerror(errno
));
1284 exit(CUPS_BACKEND_FAILED
);
1292 files
[i
] = strdup(filename
);
1294 if (!stat(filename
, &outinfo
))
1296 "DEBUG: File %d compressed to %.1f%% of original size, "
1297 CUPS_LLFMT
" bytes...\n",
1298 i
+ 1, 100.0 * outinfo
.st_size
/ total
,
1299 CUPS_LLCAST outinfo
.st_size
);
1302 #endif /* HAVE_LIBZ */
1306 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
1309 static const char * /* O - Password */
1310 password_cb(const char *prompt
) /* I - Prompt (not used) */
1319 * If there is no password set in the device URI, return the
1320 * "authentication required" exit code...
1324 unlink(tmpfilename
);
1329 #endif /* __APPLE__ */
1331 exit(CUPS_BACKEND_AUTH_REQUIRED
);
1333 return (NULL
); /* Eliminate compiler warning */
1339 * 'report_printer_state()' - Report the printer state.
1342 static int /* O - Number of reasons shown */
1343 report_printer_state(ipp_t
*ipp
) /* I - IPP response */
1345 int i
; /* Looping var */
1346 int count
; /* Count of reasons shown... */
1347 ipp_attribute_t
*reasons
; /* printer-state-reasons */
1348 const char *reason
; /* Current reason */
1349 const char *message
; /* Message to show */
1350 char unknown
[1024]; /* Unknown message string */
1351 const char *prefix
; /* Prefix for STATE: line */
1352 char state
[1024]; /* State string */
1355 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
1356 IPP_TAG_KEYWORD
)) == NULL
)
1362 for (i
= 0, count
= 0; i
< reasons
->num_values
; i
++)
1364 reason
= reasons
->values
[i
].string
.text
;
1366 strlcat(state
, prefix
, sizeof(state
));
1367 strlcat(state
, reason
, sizeof(state
));
1372 if (!strncmp(reason
, "media-needed", 12))
1373 message
= "Media tray needs to be filled.";
1374 else if (!strncmp(reason
, "media-jam", 9))
1375 message
= "Media jam!";
1376 else if (!strncmp(reason
, "moving-to-paused", 16) ||
1377 !strncmp(reason
, "paused", 6) ||
1378 !strncmp(reason
, "shutdown", 8))
1379 message
= "Printer off-line.";
1380 else if (!strncmp(reason
, "toner-low", 9))
1381 message
= "Toner low.";
1382 else if (!strncmp(reason
, "toner-empty", 11))
1383 message
= "Out of toner!";
1384 else if (!strncmp(reason
, "cover-open", 10))
1385 message
= "Cover open.";
1386 else if (!strncmp(reason
, "interlock-open", 14))
1387 message
= "Interlock open.";
1388 else if (!strncmp(reason
, "door-open", 9))
1389 message
= "Door open.";
1390 else if (!strncmp(reason
, "input-tray-missing", 18))
1391 message
= "Media tray missing!";
1392 else if (!strncmp(reason
, "media-low", 9))
1393 message
= "Media tray almost empty.";
1394 else if (!strncmp(reason
, "media-empty", 11))
1395 message
= "Media tray empty!";
1396 else if (!strncmp(reason
, "output-tray-missing", 19))
1397 message
= "Output tray missing!";
1398 else if (!strncmp(reason
, "output-area-almost-full", 23))
1399 message
= "Output bin almost full.";
1400 else if (!strncmp(reason
, "output-area-full", 16))
1401 message
= "Output bin full!";
1402 else if (!strncmp(reason
, "marker-supply-low", 17))
1403 message
= "Ink/toner almost empty.";
1404 else if (!strncmp(reason
, "marker-supply-empty", 19))
1405 message
= "Ink/toner empty!";
1406 else if (!strncmp(reason
, "marker-waste-almost-full", 24))
1407 message
= "Ink/toner waste bin almost full.";
1408 else if (!strncmp(reason
, "marker-waste-full", 17))
1409 message
= "Ink/toner waste bin full!";
1410 else if (!strncmp(reason
, "fuser-over-temp", 15))
1411 message
= "Fuser temperature high!";
1412 else if (!strncmp(reason
, "fuser-under-temp", 16))
1413 message
= "Fuser temperature low!";
1414 else if (!strncmp(reason
, "opc-near-eol", 12))
1415 message
= "OPC almost at end-of-life.";
1416 else if (!strncmp(reason
, "opc-life-over", 13))
1417 message
= "OPC at end-of-life!";
1418 else if (!strncmp(reason
, "developer-low", 13))
1419 message
= "Developer almost empty.";
1420 else if (!strncmp(reason
, "developer-empty", 15))
1421 message
= "Developer empty!";
1422 else if (strstr(reason
, "error") != NULL
)
1426 snprintf(unknown
, sizeof(unknown
), "Unknown printer error (%s)!",
1433 if (strstr(reasons
->values
[i
].string
.text
, "error"))
1434 fprintf(stderr
, "ERROR: %s\n", message
);
1435 else if (strstr(reasons
->values
[i
].string
.text
, "warning"))
1436 fprintf(stderr
, "WARNING: %s\n", message
);
1438 fprintf(stderr
, "INFO: %s\n", message
);
1442 fprintf(stderr
, "%s\n", state
);
1450 * 'run_pictwps_filter()' - Convert PICT files to PostScript when printing
1453 * This step is required because the PICT format is not documented and
1454 * subject to change, so developing a filter for other OS's is infeasible.
1455 * Also, fonts required by the PICT file need to be embedded on the
1456 * client side (which has the fonts), so we run the filter to get a
1457 * PostScript file for printing...
1460 static int /* O - Exit status of filter */
1461 run_pictwps_filter(char **argv
, /* I - Command-line arguments */
1462 const char *filename
)/* I - Filename */
1464 struct stat fileinfo
; /* Print file information */
1465 const char *ppdfile
; /* PPD file for destination printer */
1466 int pid
; /* Child process ID */
1467 int fd
; /* Temporary file descriptor */
1468 int status
; /* Exit status of filter */
1469 const char *printer
; /* PRINTER env var */
1470 static char ppdenv
[1024]; /* PPD environment variable */
1474 * First get the PPD file for the printer...
1477 printer
= getenv("PRINTER");
1480 fputs("ERROR: PRINTER environment variable not defined!\n", stderr
);
1484 if ((ppdfile
= cupsGetPPD(printer
)) == NULL
)
1486 fprintf(stderr
, "ERROR: Unable to get PPD file for printer \"%s\" - %s.\n",
1487 printer
, ippErrorString(cupsLastError()));
1492 snprintf(ppdenv
, sizeof(ppdenv
), "PPD=%s", ppdfile
);
1497 * Then create a temporary file for printing...
1500 if ((fd
= cupsTempFd(pstmpname
, sizeof(pstmpname
))) < 0)
1502 fprintf(stderr
, "ERROR: Unable to create temporary file - %s.\n",
1510 * Get the owner of the spool file - it is owned by the user we want to run
1515 stat(argv
[6], &fileinfo
);
1519 * Use the OSX defaults, as an up-stream filter created the PICT
1523 fileinfo
.st_uid
= 1;
1524 fileinfo
.st_gid
= 80;
1528 chown(ppdfile
, fileinfo
.st_uid
, fileinfo
.st_gid
);
1530 fchown(fd
, fileinfo
.st_uid
, fileinfo
.st_gid
);
1533 * Finally, run the filter to convert the file...
1536 if ((pid
= fork()) == 0)
1539 * Child process for pictwpstops... Redirect output of pictwpstops to a
1550 * Change to an unpriviledged user...
1553 setgid(fileinfo
.st_gid
);
1554 setuid(fileinfo
.st_uid
);
1557 execlp("pictwpstops", printer
, argv
[1], argv
[2], argv
[3], argv
[4], argv
[5],
1559 perror("ERROR: Unable to exec pictwpstops");
1571 perror("ERROR: Unable to fork pictwpstops");
1579 * Now wait for the filter to complete...
1582 if (wait(&status
) < 0)
1584 perror("ERROR: Unable to wait for pictwpstops");
1600 fprintf(stderr
, "ERROR: pictwpstops exited with status %d!\n",
1603 fprintf(stderr
, "ERROR: pictwpstops exited on signal %d!\n",
1611 * Return with no errors..
1616 #endif /* __APPLE__ */
1620 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1624 sigterm_handler(int sig
) /* I - Signal */
1626 (void)sig
; /* remove compiler warnings... */
1631 * Flag that the job should be cancelled...
1639 * The scheduler already tried to cancel us once, now just terminate
1640 * after removing our temp files!
1644 unlink(tmpfilename
);
1649 #endif /* __APPLE__ */
1656 * End of "$Id: ipp.c 5956 2006-09-13 18:22:34Z mike $".