2 * "$Id: ipp.c 4906 2006-01-10 20:53:28Z 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 * check_printer_state() - Check the printer state...
30 * password_cb() - Disable the password prompt for
31 * cupsDoFileRequest().
32 * report_printer_state() - Report the printer state.
33 * run_pictwps_filter() - Convert PICT files to PostScript when printing
35 * sigterm_handler() - Handle 'terminate' signals that stop the backend.
39 * Include necessary headers.
42 #include <cups/http-private.h>
46 #include <sys/types.h>
48 #include <cups/backend.h>
49 #include <cups/cups.h>
50 #include <cups/language.h>
51 #include <cups/string.h>
60 static char *password
= NULL
; /* Password for device URI */
62 static char pstmpname
[1024] = ""; /* Temporary PostScript file name */
63 #endif /* __APPLE__ */
64 static char tmpfilename
[1024] = ""; /* Temporary spool file name */
71 void check_printer_state(http_t
*http
, cups_lang_t
*language
,
72 const char *charset
, const char *uri
, /* I - Printer URI */
73 const char *resource
, const char *user
,
75 const char *password_cb(const char *);
76 int report_printer_state(ipp_t
*ipp
);
79 int run_pictwps_filter(char **argv
, const char *filename
);
80 #endif /* __APPLE__ */
81 static void sigterm_handler(int sig
);
85 * 'main()' - Send a file to the printer or server.
89 * printer-uri job-id user title copies options [file]
92 int /* O - Exit status */
93 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
94 char *argv
[]) /* I - Command-line arguments */
96 int i
; /* Looping var */
97 int num_options
; /* Number of printer options */
98 cups_option_t
*options
; /* Printer options */
99 char method
[255], /* Method in URI */
100 hostname
[1024], /* Hostname */
101 username
[255], /* Username info */
102 resource
[1024], /* Resource info (printer name) */
103 *optptr
, /* Pointer to URI options */
104 name
[255], /* Name of option */
105 value
[255], /* Value of option */
106 *ptr
; /* Pointer into name or value */
107 char *filename
; /* File to print */
108 int port
; /* Port number (not used) */
109 char uri
[HTTP_MAX_URI
]; /* Updated URI without user/pass */
110 ipp_status_t ipp_status
; /* Status of IPP request */
111 http_t
*http
; /* HTTP connection */
112 ipp_t
*request
, /* IPP request */
113 *response
, /* IPP response */
114 *supported
; /* get-printer-attributes response */
115 int waitjob
, /* Wait for job complete? */
116 waitprinter
; /* Wait for printer ready? */
117 ipp_attribute_t
*job_id_attr
; /* job-id attribute */
118 int job_id
; /* job-id value */
119 ipp_attribute_t
*job_sheets
; /* job-media-sheets-completed attribute */
120 ipp_attribute_t
*job_state
; /* job-state attribute */
121 ipp_attribute_t
*copies_sup
; /* copies-supported attribute */
122 ipp_attribute_t
*charset_sup
; /* charset-supported attribute */
123 ipp_attribute_t
*format_sup
; /* document-format-supported attribute */
124 ipp_attribute_t
*printer_state
; /* printer-state attribute */
125 ipp_attribute_t
*printer_accepting
; /* printer-is-accepting-jobs attribute */
126 const char *charset
; /* Character set to use */
127 cups_lang_t
*language
; /* Default language */
128 int copies
; /* Number of copies remaining */
129 const char *content_type
; /* CONTENT_TYPE environment variable */
130 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
131 struct sigaction action
; /* Actions for POSIX signals */
132 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
133 int version
; /* IPP version */
134 int reasons
; /* Number of printer-state-reasons shown */
135 static const char * const pattrs
[] =
136 { /* Printer attributes we want */
139 "document-format-supported",
140 "printer-is-accepting-jobs",
142 "printer-state-reasons",
144 static const char * const jattrs
[] =
145 { /* Job attributes we want */
146 "job-media-sheets-completed",
152 * Make sure status messages are not buffered...
155 setbuf(stderr
, NULL
);
158 * Ignore SIGPIPE and catch SIGTERM signals...
162 sigset(SIGPIPE
, SIG_IGN
);
163 sigset(SIGTERM
, sigterm_handler
);
164 #elif defined(HAVE_SIGACTION)
165 memset(&action
, 0, sizeof(action
));
166 action
.sa_handler
= SIG_IGN
;
167 sigaction(SIGPIPE
, &action
, NULL
);
169 sigemptyset(&action
.sa_mask
);
170 sigaddset(&action
.sa_mask
, SIGTERM
);
171 action
.sa_handler
= sigterm_handler
;
172 sigaction(SIGTERM
, &action
, NULL
);
174 signal(SIGPIPE
, SIG_IGN
);
175 signal(SIGTERM
, sigterm_handler
);
176 #endif /* HAVE_SIGSET */
179 * Check command-line...
186 if ((s
= strrchr(argv
[0], '/')) != NULL
)
191 printf("network %s \"Unknown\" \"Internet Printing Protocol (%s)\"\n", s
, s
);
192 return (CUPS_BACKEND_OK
);
194 else if (argc
< 6 || argc
> 7)
196 fprintf(stderr
, "Usage: %s job-id user title copies options [file]\n",
198 return (CUPS_BACKEND_STOP
);
202 * Get the content type...
206 content_type
= getenv("CONTENT_TYPE");
208 content_type
= "application/vnd.cups-raw";
210 if (content_type
== NULL
)
211 content_type
= "application/octet-stream";
214 * Extract the hostname and printer name from the URI...
217 if (getenv("DEVICE_URI") != NULL
)
218 /* authentication information is only available in the env var */
219 httpSeparateURI(getenv("DEVICE_URI"), method
, sizeof(method
),
220 username
, sizeof(username
),
221 hostname
, sizeof(hostname
), &port
,
222 resource
, sizeof(resource
));
223 else if (strchr(argv
[0], ':') != NULL
)
224 httpSeparateURI(argv
[0], method
, sizeof(method
), username
, sizeof(username
),
225 hostname
, sizeof(hostname
), &port
,
226 resource
, sizeof(resource
));
229 fputs("ERROR: Missing device URI on command-line and no DEVICE_URI environment variable!\n",
231 return (CUPS_BACKEND_STOP
);
234 if (!strcmp(method
, "https"))
235 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
238 * If we have 7 arguments, print the file named on the command-line.
239 * Otherwise, copy stdin to a temporary file and print the temporary
246 * Copy stdin to a temporary file...
249 int fd
; /* Temporary file */
250 char buffer
[8192]; /* Buffer for copying */
251 int bytes
; /* Number of bytes read */
254 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
256 perror("ERROR: unable to create temporary file");
257 return (CUPS_BACKEND_FAILED
);
260 while ((bytes
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0)
261 if (write(fd
, buffer
, bytes
) < bytes
)
263 perror("ERROR: unable to write to temporary file");
266 return (CUPS_BACKEND_FAILED
);
270 filename
= tmpfilename
;
276 * See if there are any options...
283 if ((optptr
= strchr(resource
, '?')) != NULL
)
286 * Yup, terminate the device name string and move to the first
287 * character of the optptr...
293 * Then parse the optptr...
302 for (ptr
= name
; *optptr
&& *optptr
!= '=';)
303 if (ptr
< (name
+ sizeof(name
) - 1))
315 for (ptr
= value
; *optptr
&& *optptr
!= '+' && *optptr
!= '&';)
316 if (ptr
< (value
+ sizeof(value
) - 1))
327 * Process the option...
330 if (!strcasecmp(name
, "waitjob"))
333 * Wait for job completion?
336 waitjob
= !strcasecmp(value
, "on") ||
337 !strcasecmp(value
, "yes") ||
338 !strcasecmp(value
, "true");
340 else if (!strcasecmp(name
, "waitprinter"))
343 * Wait for printer idle?
346 waitprinter
= !strcasecmp(value
, "on") ||
347 !strcasecmp(value
, "yes") ||
348 !strcasecmp(value
, "true");
350 else if (!strcasecmp(name
, "encryption"))
353 * Enable/disable encryption?
356 if (!strcasecmp(value
, "always"))
357 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
358 else if (!strcasecmp(value
, "required"))
359 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
360 else if (!strcasecmp(value
, "never"))
361 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
362 else if (!strcasecmp(value
, "ifrequested"))
363 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
366 fprintf(stderr
, "ERROR: Unknown encryption option value \"%s\"!\n",
370 else if (!strcasecmp(name
, "version"))
372 if (!strcmp(value
, "1.0"))
374 else if (!strcmp(value
, "1.1"))
378 fprintf(stderr
, "ERROR: Unknown version option value \"%s\"!\n",
388 fprintf(stderr
, "ERROR: Unknown option \"%s\" with value \"%s\"!\n",
395 * Set the authentication info, if any...
398 cupsSetPasswordCB(password_cb
);
403 * Use authenticaion information in the device URI...
406 if ((password
= strchr(username
, ':')) != NULL
)
409 cupsSetUser(username
);
414 * Try loading authentication information from the a##### file.
417 const char *request_root
; /* CUPS_REQUESTROOT env var */
418 char afilename
[1024], /* a##### filename */
419 aline
[1024]; /* Line from file */
420 FILE *fp
; /* File pointer */
423 if ((request_root
= getenv("CUPS_REQUESTROOT")) != NULL
)
426 * Try opening authentication cache file...
429 snprintf(afilename
, sizeof(afilename
), "%s/a%05d", request_root
,
431 if ((fp
= fopen(afilename
, "r")) != NULL
)
437 if (fgets(aline
, sizeof(aline
), fp
))
443 i
= sizeof(username
);
444 httpDecode64_2(username
, &i
, aline
);
450 if (fgets(aline
, sizeof(aline
), fp
))
456 i
= sizeof(password
);
457 httpDecode64_2(password
, &i
, aline
);
471 * Try connecting to the remote server...
476 fprintf(stderr
, "INFO: Connecting to %s on port %d...\n", hostname
, port
);
478 if ((http
= httpConnectEncrypt(hostname
, port
, cupsEncryption())) == NULL
)
480 if (getenv("CLASS") != NULL
)
483 * If the CLASS environment variable is set, the job was submitted
484 * to a class and not to a specific queue. In this case, we want
485 * to abort immediately so that the job can be requeued on the next
486 * available printer in the class.
489 fprintf(stderr
, "INFO: Unable to connect to %s, queuing on next printer in class...\n",
492 if (argc
== 6 || strcmp(filename
, argv
[6]))
496 * Sleep 5 seconds to keep the job from requeuing too rapidly...
501 return (CUPS_BACKEND_FAILED
);
504 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
505 errno
== EHOSTUNREACH
)
507 fprintf(stderr
, "INFO: Network host \'%s\' is busy; will retry in 30 seconds...\n",
513 fprintf(stderr
, "INFO: Unable to lookup host \'%s\' - %s\n",
514 hostname
, hstrerror(h_errno
));
519 perror("ERROR: Unable to connect to IPP host");
524 while (http
== NULL
);
526 fprintf(stderr
, "INFO: Connected to %s...\n", hostname
);
529 * Build a URI for the printer and fill the standard IPP attributes for
530 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
531 * might contain username:password information...
534 snprintf(uri
, sizeof(uri
), "%s://%s:%d%s", method
, hostname
, port
, resource
);
537 * First validate the destination and see if the device supports multiple
538 * copies. We have to do this because some IPP servers (e.g. HP JetDirect)
539 * don't support the copies attribute...
542 language
= cupsLangDefault();
551 * Build the IPP request...
555 request
->request
.op
.version
[1] = version
;
556 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
557 request
->request
.op
.request_id
= 1;
559 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
560 "attributes-charset", NULL
, "utf-8");
562 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
563 "attributes-natural-language", NULL
,
564 language
!= NULL
? language
->language
: "en");
566 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
569 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
570 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
577 fputs("DEBUG: Getting supported attributes...\n", stderr
);
579 if ((supported
= cupsDoRequest(http
, request
, resource
)) == NULL
)
580 ipp_status
= cupsLastError();
582 ipp_status
= supported
->request
.status
.status_code
;
584 if (ipp_status
> IPP_OK_CONFLICT
)
586 if (ipp_status
== IPP_PRINTER_BUSY
||
587 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
589 fputs("INFO: Printer busy; will retry in 10 seconds...\n", stderr
);
590 report_printer_state(supported
);
593 else if ((ipp_status
== IPP_BAD_REQUEST
||
594 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
== 1)
597 * Switch to IPP/1.0...
600 fputs("INFO: Printer does not support IPP/1.1, trying IPP/1.0...\n", stderr
);
604 else if (ipp_status
== IPP_NOT_FOUND
)
606 fputs("ERROR: Destination printer does not exist!\n", stderr
);
609 ippDelete(supported
);
611 return (CUPS_BACKEND_STOP
);
615 fprintf(stderr
, "ERROR: Unable to get printer status (%s)!\n",
616 ippErrorString(ipp_status
));
621 ippDelete(supported
);
625 else if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
626 IPP_TAG_RANGE
)) != NULL
)
629 * Has the "copies-supported" attribute - does it have an upper
633 if (copies_sup
->values
[0].range
.upper
<= 1)
634 copies_sup
= NULL
; /* No */
637 charset_sup
= ippFindAttribute(supported
, "charset-supported",
639 format_sup
= ippFindAttribute(supported
, "document-format-supported",
644 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
645 format_sup
->num_values
);
646 for (i
= 0; i
< format_sup
->num_values
; i
++)
647 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
648 format_sup
->values
[i
].string
.text
);
651 report_printer_state(supported
);
653 while (ipp_status
> IPP_OK_CONFLICT
);
656 * See if the printer is accepting jobs and is not stopped; if either
657 * condition is true and we are printing to a class, requeue the job...
660 if (getenv("CLASS") != NULL
)
662 printer_state
= ippFindAttribute(supported
, "printer-state",
664 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
667 if (printer_state
== NULL
||
668 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&& waitprinter
) ||
669 printer_accepting
== NULL
||
670 !printer_accepting
->values
[0].boolean
)
673 * If the CLASS environment variable is set, the job was submitted
674 * to a class and not to a specific queue. In this case, we want
675 * to abort immediately so that the job can be requeued on the next
676 * available printer in the class.
679 fprintf(stderr
, "INFO: Unable to queue job on %s, queuing on next printer in class...\n",
682 ippDelete(supported
);
685 if (argc
== 6 || strcmp(filename
, argv
[6]))
689 * Sleep 5 seconds to keep the job from requeuing too rapidly...
694 return (CUPS_BACKEND_FAILED
);
699 * See if the printer supports multiple copies...
702 if (copies_sup
|| argc
< 7)
705 copies
= atoi(argv
[4]);
708 * Figure out the character set to use...
711 charset
= language
? cupsLangEncoding(language
) : "us-ascii";
716 * See if IPP server supports the requested character set...
719 for (i
= 0; i
< charset_sup
->num_values
; i
++)
720 if (strcasecmp(charset
, charset_sup
->values
[i
].string
.text
) == 0)
724 * If not, choose us-ascii or utf-8...
727 if (i
>= charset_sup
->num_values
)
730 * See if us-ascii is supported...
733 for (i
= 0; i
< charset_sup
->num_values
; i
++)
734 if (strcasecmp("us-ascii", charset_sup
->values
[i
].string
.text
) == 0)
737 if (i
< charset_sup
->num_values
)
738 charset
= "us-ascii";
745 * Then issue the print-job request...
753 * Build the IPP request...
757 request
->request
.op
.version
[1] = version
;
758 request
->request
.op
.operation_id
= IPP_PRINT_JOB
;
759 request
->request
.op
.request_id
= 1;
761 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
762 "attributes-charset", NULL
, charset
);
764 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
765 "attributes-natural-language", NULL
,
766 language
!= NULL
? language
->language
: "en");
768 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
771 fprintf(stderr
, "DEBUG: printer-uri = \"%s\"\n", uri
);
774 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
775 "requesting-user-name", NULL
, argv
[2]);
777 fprintf(stderr
, "DEBUG: requesting-user-name = \"%s\"\n", argv
[2]);
780 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
783 fprintf(stderr
, "DEBUG: job-name = \"%s\"\n", argv
[3]);
786 * Handle options on the command-line...
790 num_options
= cupsParseOptions(argv
[5], 0, &options
);
793 if (content_type
!= NULL
&& strcasecmp(content_type
, "application/pictwps") == 0)
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
) == 0)
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...
818 content_type
= "application/postscript";
821 #endif /* __APPLE__ */
823 if (content_type
!= NULL
&& format_sup
!= NULL
)
825 for (i
= 0; i
< format_sup
->num_values
; i
++)
826 if (strcasecmp(content_type
, format_sup
->values
[i
].string
.text
) == 0)
829 if (i
< format_sup
->num_values
)
830 num_options
= cupsAddOption("document-format", content_type
,
831 num_options
, &options
);
837 * Only send options if the destination printer supports the copies
838 * attribute. This is a hack for the HP JetDirect implementation of
839 * IPP, which does not accept extension attributes and incorrectly
840 * reports a client-error-bad-request error instead of the
841 * successful-ok-unsupported-attributes status. In short, at least
842 * some HP implementations of IPP are non-compliant.
845 cupsEncodeOptions(request
, num_options
, options
);
846 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies",
850 cupsFreeOptions(num_options
, options
);
853 * If copies aren't supported, then we are likely dealing with an HP
854 * JetDirect. The HP IPP implementation seems to close the connection
855 * after every request (that is, it does *not* implement HTTP Keep-
856 * Alive, which is REQUIRED by HTTP/1.1...
866 if ((response
= cupsDoFileRequest(http
, request
, resource
, filename
)) == NULL
)
867 ipp_status
= cupsLastError();
869 ipp_status
= response
->request
.status
.status_code
;
871 if (ipp_status
> IPP_OK_CONFLICT
)
875 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
876 ipp_status
== IPP_PRINTER_BUSY
)
878 fputs("INFO: Printer is busy; retrying print job...\n", stderr
);
882 fprintf(stderr
, "ERROR: Print file was not accepted (%s)!\n",
883 ippErrorString(ipp_status
));
885 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
886 IPP_TAG_INTEGER
)) == NULL
)
888 fputs("NOTICE: Print file accepted - job ID unknown.\n", stderr
);
893 job_id
= job_id_attr
->values
[0].integer
;
894 fprintf(stderr
, "NOTICE: Print file accepted - job ID %d.\n", job_id
);
900 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
902 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
905 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
906 ipp_status
== IPP_PRINTER_BUSY
)
912 * Wait for the job to complete...
915 if (!job_id
|| !waitjob
)
918 fputs("INFO: Waiting for job to complete...\n", stderr
);
923 * Build an IPP_GET_JOB_ATTRIBUTES request...
927 request
->request
.op
.version
[1] = version
;
928 request
->request
.op
.operation_id
= IPP_GET_JOB_ATTRIBUTES
;
929 request
->request
.op
.request_id
= 1;
931 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
932 "attributes-charset", NULL
, charset
);
934 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
935 "attributes-natural-language", NULL
,
936 language
!= NULL
? language
->language
: "en");
938 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
941 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
945 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
946 "requesting-user-name", NULL
, argv
[2]);
948 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
949 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
959 if ((response
= cupsDoRequest(http
, request
, resource
)) == NULL
)
960 ipp_status
= cupsLastError();
962 ipp_status
= response
->request
.status
.status_code
;
964 if (ipp_status
== IPP_NOT_FOUND
)
967 * Job has gone away and/or the server has no job history...
976 if (ipp_status
> IPP_OK_CONFLICT
)
978 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
979 ipp_status
!= IPP_PRINTER_BUSY
)
984 fprintf(stderr
, "ERROR: Unable to get job %d attributes (%s)!\n",
985 job_id
, ippErrorString(ipp_status
));
990 if (response
!= NULL
)
992 if ((job_state
= ippFindAttribute(response
, "job-state",
993 IPP_TAG_ENUM
)) != NULL
)
996 * Stop polling if the job is finished or pending-held...
999 if (job_state
->values
[0].integer
> IPP_JOB_PROCESSING
||
1000 job_state
->values
[0].integer
== IPP_JOB_HELD
)
1002 if ((job_sheets
= ippFindAttribute(response
, "job-media-sheets-completed",
1003 IPP_TAG_INTEGER
)) != NULL
)
1004 fprintf(stderr
, "PAGE: total %d\n", job_sheets
->values
[0].integer
);
1006 ippDelete(response
);
1013 ippDelete(response
);
1016 * Check the printer state and report it if necessary...
1020 httpReconnect(http);*/
1022 check_printer_state(http
, language
, charset
, uri
, resource
, argv
[2],
1026 * Wait 10 seconds before polling again...
1034 * Check the printer state and report it if necessary...
1038 httpReconnect(http);*/
1040 check_printer_state(http
, language
, charset
, uri
, resource
, argv
[2], version
);
1049 ippDelete(supported
);
1052 * Remove the temporary file(s) if necessary...
1056 unlink(tmpfilename
);
1061 #endif /* __APPLE__ */
1064 * Return the queue status...
1067 return (ipp_status
> IPP_OK_CONFLICT
? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
1072 * 'check_printer_state()' - Check the printer state...
1076 check_printer_state(http_t
*http
, /* I - HTTP connection */
1077 cups_lang_t
*language
,
1079 const char *charset
,
1081 const char *uri
, /* I - Printer URI */
1082 const char *resource
,
1083 /* I - Resource path */
1084 const char *user
, /* I - Username, if any */
1085 int version
)/* I - IPP version */
1087 ipp_t
*request
, /* IPP request */
1088 *response
; /* IPP response */
1092 * Check on the printer state...
1096 request
->request
.op
.version
[1] = version
;
1097 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
1098 request
->request
.op
.request_id
= 1;
1100 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1101 "attributes-charset", NULL
, charset
);
1103 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1104 "attributes-natural-language", NULL
,
1105 language
!= NULL
? language
->language
: "en");
1107 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1110 if (user
&& user
[0])
1111 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1112 "requesting-user-name", NULL
, user
);
1114 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1115 "requested-attributes", NULL
, "printer-state-reasons");
1121 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
1123 report_printer_state(response
);
1124 ippDelete(response
);
1130 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
1133 const char * /* O - Password */
1134 password_cb(const char *prompt
) /* I - Prompt (not used) */
1143 * If there is no password set in the device URI, return the
1144 * "authentication required" exit code...
1148 unlink(tmpfilename
);
1153 #endif /* __APPLE__ */
1155 exit(CUPS_BACKEND_AUTH_REQUIRED
);
1161 * 'report_printer_state()' - Report the printer state.
1164 int /* O - Number of reasons shown */
1165 report_printer_state(ipp_t
*ipp
) /* I - IPP response */
1167 int i
; /* Looping var */
1168 int count
; /* Count of reasons shown... */
1169 ipp_attribute_t
*reasons
; /* printer-state-reasons */
1170 const char *reason
; /* Current reason */
1171 const char *message
; /* Message to show */
1172 char unknown
[1024]; /* Unknown message string */
1173 const char *prefix
; /* Prefix for STATE: line */
1174 char state
[1024]; /* State string */
1177 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
1178 IPP_TAG_KEYWORD
)) == NULL
)
1184 for (i
= 0, count
= 0; i
< reasons
->num_values
; i
++)
1186 reason
= reasons
->values
[i
].string
.text
;
1188 strlcat(state
, prefix
, sizeof(state
));
1189 strlcat(state
, reason
, sizeof(state
));
1194 if (!strncmp(reason
, "media-needed", 12))
1195 message
= "Media tray needs to be filled.";
1196 else if (!strncmp(reason
, "media-jam", 9))
1197 message
= "Media jam!";
1198 else if (!strncmp(reason
, "moving-to-paused", 16) ||
1199 !strncmp(reason
, "paused", 6) ||
1200 !strncmp(reason
, "shutdown", 8))
1201 message
= "Printer off-line.";
1202 else if (!strncmp(reason
, "toner-low", 9))
1203 message
= "Toner low.";
1204 else if (!strncmp(reason
, "toner-empty", 11))
1205 message
= "Out of toner!";
1206 else if (!strncmp(reason
, "cover-open", 10))
1207 message
= "Cover open.";
1208 else if (!strncmp(reason
, "interlock-open", 14))
1209 message
= "Interlock open.";
1210 else if (!strncmp(reason
, "door-open", 9))
1211 message
= "Door open.";
1212 else if (!strncmp(reason
, "input-tray-missing", 18))
1213 message
= "Media tray missing!";
1214 else if (!strncmp(reason
, "media-low", 9))
1215 message
= "Media tray almost empty.";
1216 else if (!strncmp(reason
, "media-empty", 11))
1217 message
= "Media tray empty!";
1218 else if (!strncmp(reason
, "output-tray-missing", 19))
1219 message
= "Output tray missing!";
1220 else if (!strncmp(reason
, "output-area-almost-full", 23))
1221 message
= "Output bin almost full.";
1222 else if (!strncmp(reason
, "output-area-full", 16))
1223 message
= "Output bin full!";
1224 else if (!strncmp(reason
, "marker-supply-low", 17))
1225 message
= "Ink/toner almost empty.";
1226 else if (!strncmp(reason
, "marker-supply-empty", 19))
1227 message
= "Ink/toner empty!";
1228 else if (!strncmp(reason
, "marker-waste-almost-full", 24))
1229 message
= "Ink/toner waste bin almost full.";
1230 else if (!strncmp(reason
, "marker-waste-full", 17))
1231 message
= "Ink/toner waste bin full!";
1232 else if (!strncmp(reason
, "fuser-over-temp", 15))
1233 message
= "Fuser temperature high!";
1234 else if (!strncmp(reason
, "fuser-under-temp", 16))
1235 message
= "Fuser temperature low!";
1236 else if (!strncmp(reason
, "opc-near-eol", 12))
1237 message
= "OPC almost at end-of-life.";
1238 else if (!strncmp(reason
, "opc-life-over", 13))
1239 message
= "OPC at end-of-life!";
1240 else if (!strncmp(reason
, "developer-low", 13))
1241 message
= "Developer almost empty.";
1242 else if (!strncmp(reason
, "developer-empty", 15))
1243 message
= "Developer empty!";
1244 else if (strstr(reason
, "error") != NULL
)
1248 snprintf(unknown
, sizeof(unknown
), "Unknown printer error (%s)!",
1255 if (strstr(reasons
->values
[i
].string
.text
, "error"))
1256 fprintf(stderr
, "ERROR: %s\n", message
);
1257 else if (strstr(reasons
->values
[i
].string
.text
, "warning"))
1258 fprintf(stderr
, "WARNING: %s\n", message
);
1260 fprintf(stderr
, "INFO: %s\n", message
);
1264 fprintf(stderr
, "%s\n", state
);
1272 * 'run_pictwps_filter()' - Convert PICT files to PostScript when printing
1275 * This step is required because the PICT format is not documented and
1276 * subject to change, so developing a filter for other OS's is infeasible.
1277 * Also, fonts required by the PICT file need to be embedded on the
1278 * client side (which has the fonts), so we run the filter to get a
1279 * PostScript file for printing...
1282 int /* O - Exit status of filter */
1283 run_pictwps_filter(char **argv
, /* I - Command-line arguments */
1284 const char *filename
) /* I - Filename */
1286 struct stat fileinfo
; /* Print file information */
1287 const char *ppdfile
; /* PPD file for destination printer */
1288 int pid
; /* Child process ID */
1289 int fd
; /* Temporary file descriptor */
1290 int status
; /* Exit status of filter */
1291 const char *printer
; /* PRINTER env var */
1292 static char ppdenv
[1024]; /* PPD environment variable */
1296 * First get the PPD file for the printer...
1299 printer
= getenv("PRINTER");
1302 fputs("ERROR: PRINTER environment variable not defined!\n", stderr
);
1306 if ((ppdfile
= cupsGetPPD(printer
)) == NULL
)
1308 fprintf(stderr
, "ERROR: Unable to get PPD file for printer \"%s\" - %s.\n",
1309 printer
, ippErrorString(cupsLastError()));
1314 snprintf(ppdenv
, sizeof(ppdenv
), "PPD=%s", ppdfile
);
1319 * Then create a temporary file for printing...
1322 if ((fd
= cupsTempFd(pstmpname
, sizeof(pstmpname
))) < 0)
1324 fprintf(stderr
, "ERROR: Unable to create temporary file - %s.\n",
1332 * Get the owner of the spool file - it is owned by the user we want to run
1337 stat(argv
[6], &fileinfo
);
1341 * Use the OSX defaults, as an up-stream filter created the PICT
1345 fileinfo
.st_uid
= 1;
1346 fileinfo
.st_gid
= 80;
1350 chown(ppdfile
, fileinfo
.st_uid
, fileinfo
.st_gid
);
1352 fchown(fd
, fileinfo
.st_uid
, fileinfo
.st_gid
);
1355 * Finally, run the filter to convert the file...
1358 if ((pid
= fork()) == 0)
1361 * Child process for pictwpstops... Redirect output of pictwpstops to a
1372 * Change to an unpriviledged user...
1375 setgid(fileinfo
.st_gid
);
1376 setuid(fileinfo
.st_uid
);
1379 execlp("pictwpstops", printer
, argv
[1], argv
[2], argv
[3], argv
[4], argv
[5],
1381 perror("ERROR: Unable to exec pictwpstops");
1393 perror("ERROR: Unable to fork pictwpstops");
1401 * Now wait for the filter to complete...
1404 if (wait(&status
) < 0)
1406 perror("ERROR: Unable to wait for pictwpstops");
1422 fprintf(stderr
, "ERROR: pictwpstops exited with status %d!\n",
1425 fprintf(stderr
, "ERROR: pictwpstops exited on signal %d!\n",
1433 * Return with no errors..
1438 #endif /* __APPLE__ */
1442 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1446 sigterm_handler(int sig
) /* I - Signal */
1448 (void)sig
; /* remove compiler warnings... */
1451 * Remove the temporary file(s) if necessary...
1455 unlink(tmpfilename
);
1460 #endif /* __APPLE__ */
1467 * End of "$Id: ipp.c 4906 2006-01-10 20:53:28Z mike $".