2 * "$Id: ipp.c 5023 2006-01-29 14:39:44Z mike $"
4 * IPP backend for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * main() - Send a file to the printer or server.
29 * 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
, const char *uri
,
72 const char *resource
, const char *user
,
74 const char *password_cb(const char *);
75 int report_printer_state(ipp_t
*ipp
);
78 int run_pictwps_filter(char **argv
, const char *filename
);
79 #endif /* __APPLE__ */
80 static void sigterm_handler(int sig
);
84 * 'main()' - Send a file to the printer or server.
88 * printer-uri job-id user title copies options [file]
91 int /* O - Exit status */
92 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
93 char *argv
[]) /* I - Command-line arguments */
95 int i
; /* Looping var */
96 int num_options
; /* Number of printer options */
97 cups_option_t
*options
; /* Printer options */
98 char method
[255], /* Method in URI */
99 hostname
[1024], /* Hostname */
100 username
[255], /* Username info */
101 resource
[1024], /* Resource info (printer name) */
102 *optptr
, /* Pointer to URI options */
103 name
[255], /* Name of option */
104 value
[255], /* Value of option */
105 *ptr
; /* Pointer into name or value */
106 char *filename
; /* File to print */
107 int port
; /* Port number (not used) */
108 char uri
[HTTP_MAX_URI
]; /* Updated URI without user/pass */
109 ipp_status_t ipp_status
; /* Status of IPP request */
110 http_t
*http
; /* HTTP connection */
111 ipp_t
*request
, /* IPP request */
112 *response
, /* IPP response */
113 *supported
; /* get-printer-attributes response */
114 int waitjob
, /* Wait for job complete? */
115 waitprinter
; /* Wait for printer ready? */
116 ipp_attribute_t
*job_id_attr
; /* job-id attribute */
117 int job_id
; /* job-id value */
118 ipp_attribute_t
*job_sheets
; /* job-media-sheets-completed attribute */
119 ipp_attribute_t
*job_state
; /* job-state attribute */
120 ipp_attribute_t
*copies_sup
; /* copies-supported attribute */
121 ipp_attribute_t
*format_sup
; /* document-format-supported attribute */
122 ipp_attribute_t
*printer_state
; /* printer-state attribute */
123 ipp_attribute_t
*printer_accepting
; /* printer-is-accepting-jobs attribute */
124 int copies
; /* Number of copies remaining */
125 const char *content_type
; /* CONTENT_TYPE environment variable */
126 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
127 struct sigaction action
; /* Actions for POSIX signals */
128 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
129 int version
; /* IPP version */
130 int reasons
; /* Number of printer-state-reasons shown */
131 static const char * const pattrs
[] =
132 { /* Printer attributes we want */
134 "document-format-supported",
135 "printer-is-accepting-jobs",
137 "printer-state-reasons",
139 static const char * const jattrs
[] =
140 { /* Job attributes we want */
141 "job-media-sheets-completed",
147 * Make sure status messages are not buffered...
150 setbuf(stderr
, NULL
);
153 * Ignore SIGPIPE and catch SIGTERM signals...
157 sigset(SIGPIPE
, SIG_IGN
);
158 sigset(SIGTERM
, sigterm_handler
);
159 #elif defined(HAVE_SIGACTION)
160 memset(&action
, 0, sizeof(action
));
161 action
.sa_handler
= SIG_IGN
;
162 sigaction(SIGPIPE
, &action
, NULL
);
164 sigemptyset(&action
.sa_mask
);
165 sigaddset(&action
.sa_mask
, SIGTERM
);
166 action
.sa_handler
= sigterm_handler
;
167 sigaction(SIGTERM
, &action
, NULL
);
169 signal(SIGPIPE
, SIG_IGN
);
170 signal(SIGTERM
, sigterm_handler
);
171 #endif /* HAVE_SIGSET */
174 * Check command-line...
181 if ((s
= strrchr(argv
[0], '/')) != NULL
)
186 printf("network %s \"Unknown\" \"Internet Printing Protocol (%s)\"\n", s
, s
);
187 return (CUPS_BACKEND_OK
);
189 else if (argc
< 6 || argc
> 7)
191 fprintf(stderr
, "Usage: %s job-id user title copies options [file]\n",
193 return (CUPS_BACKEND_STOP
);
197 * Get the content type...
201 content_type
= getenv("CONTENT_TYPE");
203 content_type
= "application/vnd.cups-raw";
205 if (content_type
== NULL
)
206 content_type
= "application/octet-stream";
209 * Extract the hostname and printer name from the URI...
212 if (httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
213 method
, sizeof(method
), username
, sizeof(username
),
214 hostname
, sizeof(hostname
), &port
,
215 resource
, sizeof(resource
)) < HTTP_URI_OK
)
217 fputs("ERROR: Missing device URI on command-line and no DEVICE_URI "
218 "environment variable!\n", stderr
);
219 return (CUPS_BACKEND_STOP
);
222 if (!strcmp(method
, "https"))
223 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
226 * If we have 7 arguments, print the file named on the command-line.
227 * Otherwise, copy stdin to a temporary file and print the temporary
234 * Copy stdin to a temporary file...
237 int fd
; /* Temporary file */
238 char buffer
[8192]; /* Buffer for copying */
239 int bytes
; /* Number of bytes read */
242 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
244 perror("ERROR: unable to create temporary file");
245 return (CUPS_BACKEND_FAILED
);
248 while ((bytes
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0)
249 if (write(fd
, buffer
, bytes
) < bytes
)
251 perror("ERROR: unable to write to temporary file");
254 return (CUPS_BACKEND_FAILED
);
258 filename
= tmpfilename
;
264 * See if there are any options...
271 if ((optptr
= strchr(resource
, '?')) != NULL
)
274 * Yup, terminate the device name string and move to the first
275 * character of the optptr...
281 * Then parse the optptr...
290 for (ptr
= name
; *optptr
&& *optptr
!= '=';)
291 if (ptr
< (name
+ sizeof(name
) - 1))
303 for (ptr
= value
; *optptr
&& *optptr
!= '+' && *optptr
!= '&';)
304 if (ptr
< (value
+ sizeof(value
) - 1))
315 * Process the option...
318 if (!strcasecmp(name
, "waitjob"))
321 * Wait for job completion?
324 waitjob
= !strcasecmp(value
, "on") ||
325 !strcasecmp(value
, "yes") ||
326 !strcasecmp(value
, "true");
328 else if (!strcasecmp(name
, "waitprinter"))
331 * Wait for printer idle?
334 waitprinter
= !strcasecmp(value
, "on") ||
335 !strcasecmp(value
, "yes") ||
336 !strcasecmp(value
, "true");
338 else if (!strcasecmp(name
, "encryption"))
341 * Enable/disable encryption?
344 if (!strcasecmp(value
, "always"))
345 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
346 else if (!strcasecmp(value
, "required"))
347 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
348 else if (!strcasecmp(value
, "never"))
349 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
350 else if (!strcasecmp(value
, "ifrequested"))
351 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
354 fprintf(stderr
, "ERROR: Unknown encryption option value \"%s\"!\n",
358 else if (!strcasecmp(name
, "version"))
360 if (!strcmp(value
, "1.0"))
362 else if (!strcmp(value
, "1.1"))
366 fprintf(stderr
, "ERROR: Unknown version option value \"%s\"!\n",
376 fprintf(stderr
, "ERROR: Unknown option \"%s\" with value \"%s\"!\n",
383 * Set the authentication info, if any...
386 cupsSetPasswordCB(password_cb
);
391 * Use authenticaion information in the device URI...
394 if ((password
= strchr(username
, ':')) != NULL
)
397 cupsSetUser(username
);
402 * Try loading authentication information from the a##### file.
405 const char *request_root
; /* CUPS_REQUESTROOT env var */
406 char afilename
[1024], /* a##### filename */
407 aline
[1024]; /* Line from file */
408 FILE *fp
; /* File pointer */
411 if ((request_root
= getenv("CUPS_REQUESTROOT")) != NULL
)
414 * Try opening authentication cache file...
417 snprintf(afilename
, sizeof(afilename
), "%s/a%05d", request_root
,
419 if ((fp
= fopen(afilename
, "r")) != NULL
)
425 if (fgets(aline
, sizeof(aline
), fp
))
431 i
= sizeof(username
);
432 httpDecode64_2(username
, &i
, aline
);
438 if (fgets(aline
, sizeof(aline
), fp
))
444 i
= sizeof(password
);
445 httpDecode64_2(password
, &i
, aline
);
459 * Try connecting to the remote server...
464 fprintf(stderr
, "INFO: Connecting to %s on port %d...\n", hostname
, port
);
466 if ((http
= httpConnectEncrypt(hostname
, port
, cupsEncryption())) == NULL
)
468 if (getenv("CLASS") != NULL
)
471 * If the CLASS environment variable is set, the job was submitted
472 * to a class and not to a specific queue. In this case, we want
473 * to abort immediately so that the job can be requeued on the next
474 * available printer in the class.
477 fprintf(stderr
, "INFO: Unable to connect to %s, queuing on next printer in class...\n",
480 if (argc
== 6 || strcmp(filename
, argv
[6]))
484 * Sleep 5 seconds to keep the job from requeuing too rapidly...
489 return (CUPS_BACKEND_FAILED
);
492 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
493 errno
== EHOSTUNREACH
)
495 fprintf(stderr
, "INFO: Network host \'%s\' is busy; will retry in 30 seconds...\n",
501 fprintf(stderr
, "INFO: Unable to lookup host \'%s\' - %s\n",
502 hostname
, hstrerror(h_errno
));
507 perror("ERROR: Unable to connect to IPP host");
512 while (http
== NULL
);
514 fprintf(stderr
, "INFO: Connected to %s...\n", hostname
);
517 * Build a URI for the printer and fill the standard IPP attributes for
518 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
519 * might contain username:password information...
522 snprintf(uri
, sizeof(uri
), "%s://%s:%d%s", method
, hostname
, port
, resource
);
525 * First validate the destination and see if the device supports multiple
526 * copies. We have to do this because some IPP servers (e.g. HP JetDirect)
527 * don't support the copies attribute...
537 * Build the IPP request...
540 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
541 request
->request
.op
.version
[1] = version
;
543 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
546 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
547 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
554 fputs("DEBUG: Getting supported attributes...\n", stderr
);
556 if ((supported
= cupsDoRequest(http
, request
, resource
)) == NULL
)
557 ipp_status
= cupsLastError();
559 ipp_status
= supported
->request
.status
.status_code
;
561 if (ipp_status
> IPP_OK_CONFLICT
)
563 if (ipp_status
== IPP_PRINTER_BUSY
||
564 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
566 fputs("INFO: Printer busy; will retry in 10 seconds...\n", stderr
);
567 report_printer_state(supported
);
570 else if ((ipp_status
== IPP_BAD_REQUEST
||
571 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
== 1)
574 * Switch to IPP/1.0...
577 fputs("INFO: Printer does not support IPP/1.1, trying IPP/1.0...\n", stderr
);
581 else if (ipp_status
== IPP_NOT_FOUND
)
583 fputs("ERROR: Destination printer does not exist!\n", stderr
);
586 ippDelete(supported
);
588 return (CUPS_BACKEND_STOP
);
592 fprintf(stderr
, "ERROR: Unable to get printer status (%s)!\n",
593 ippErrorString(ipp_status
));
598 ippDelete(supported
);
602 else if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
603 IPP_TAG_RANGE
)) != NULL
)
606 * Has the "copies-supported" attribute - does it have an upper
610 if (copies_sup
->values
[0].range
.upper
<= 1)
611 copies_sup
= NULL
; /* No */
614 format_sup
= ippFindAttribute(supported
, "document-format-supported",
619 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
620 format_sup
->num_values
);
621 for (i
= 0; i
< format_sup
->num_values
; i
++)
622 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
623 format_sup
->values
[i
].string
.text
);
626 report_printer_state(supported
);
628 while (ipp_status
> IPP_OK_CONFLICT
);
631 * See if the printer is accepting jobs and is not stopped; if either
632 * condition is true and we are printing to a class, requeue the job...
635 if (getenv("CLASS") != NULL
)
637 printer_state
= ippFindAttribute(supported
, "printer-state",
639 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
642 if (printer_state
== NULL
||
643 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&& waitprinter
) ||
644 printer_accepting
== NULL
||
645 !printer_accepting
->values
[0].boolean
)
648 * If the CLASS environment variable is set, the job was submitted
649 * to a class and not to a specific queue. In this case, we want
650 * to abort immediately so that the job can be requeued on the next
651 * available printer in the class.
654 fprintf(stderr
, "INFO: Unable to queue job on %s, queuing on next printer in class...\n",
657 ippDelete(supported
);
660 if (argc
== 6 || strcmp(filename
, argv
[6]))
664 * Sleep 5 seconds to keep the job from requeuing too rapidly...
669 return (CUPS_BACKEND_FAILED
);
674 * See if the printer supports multiple copies...
677 if (copies_sup
|| argc
< 7)
680 copies
= atoi(argv
[4]);
683 * Then issue the print-job request...
691 * Build the IPP request...
694 request
= ippNewRequest(IPP_PRINT_JOB
);
695 request
->request
.op
.version
[1] = version
;
697 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
700 fprintf(stderr
, "DEBUG: printer-uri = \"%s\"\n", uri
);
703 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
704 "requesting-user-name", NULL
, argv
[2]);
706 fprintf(stderr
, "DEBUG: requesting-user-name = \"%s\"\n", argv
[2]);
709 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
712 fprintf(stderr
, "DEBUG: job-name = \"%s\"\n", argv
[3]);
715 * Handle options on the command-line...
719 num_options
= cupsParseOptions(argv
[5], 0, &options
);
722 if (content_type
!= NULL
&& strcasecmp(content_type
, "application/pictwps") == 0)
724 if (format_sup
!= NULL
)
726 for (i
= 0; i
< format_sup
->num_values
; i
++)
727 if (strcasecmp(content_type
, format_sup
->values
[i
].string
.text
) == 0)
731 if (format_sup
== NULL
|| i
>= format_sup
->num_values
)
734 * Remote doesn't support "application/pictwps" (i.e. it's not MacOS X)
735 * so convert the document to PostScript...
738 if (run_pictwps_filter(argv
, filename
))
739 return (CUPS_BACKEND_FAILED
);
741 filename
= pstmpname
;
744 * Change the MIME type to application/postscript...
747 content_type
= "application/postscript";
750 #endif /* __APPLE__ */
752 if (content_type
!= NULL
&& format_sup
!= NULL
)
754 for (i
= 0; i
< format_sup
->num_values
; i
++)
755 if (strcasecmp(content_type
, format_sup
->values
[i
].string
.text
) == 0)
758 if (i
< format_sup
->num_values
)
759 num_options
= cupsAddOption("document-format", content_type
,
760 num_options
, &options
);
766 * Only send options if the destination printer supports the copies
767 * attribute. This is a hack for the HP JetDirect implementation of
768 * IPP, which does not accept extension attributes and incorrectly
769 * reports a client-error-bad-request error instead of the
770 * successful-ok-unsupported-attributes status. In short, at least
771 * some HP implementations of IPP are non-compliant.
774 cupsEncodeOptions(request
, num_options
, options
);
775 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies",
779 cupsFreeOptions(num_options
, options
);
782 * If copies aren't supported, then we are likely dealing with an HP
783 * JetDirect. The HP IPP implementation seems to close the connection
784 * after every request (that is, it does *not* implement HTTP Keep-
785 * Alive, which is REQUIRED by HTTP/1.1...
795 if ((response
= cupsDoFileRequest(http
, request
, resource
, filename
)) == NULL
)
796 ipp_status
= cupsLastError();
798 ipp_status
= response
->request
.status
.status_code
;
800 if (ipp_status
> IPP_OK_CONFLICT
)
804 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
805 ipp_status
== IPP_PRINTER_BUSY
)
807 fputs("INFO: Printer is busy; retrying print job...\n", stderr
);
811 fprintf(stderr
, "ERROR: Print file was not accepted (%s)!\n",
812 ippErrorString(ipp_status
));
814 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
815 IPP_TAG_INTEGER
)) == NULL
)
817 fputs("NOTICE: Print file accepted - job ID unknown.\n", stderr
);
822 job_id
= job_id_attr
->values
[0].integer
;
823 fprintf(stderr
, "NOTICE: Print file accepted - job ID %d.\n", job_id
);
829 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
831 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
834 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
835 ipp_status
== IPP_PRINTER_BUSY
)
841 * Wait for the job to complete...
844 if (!job_id
|| !waitjob
)
847 fputs("INFO: Waiting for job to complete...\n", stderr
);
852 * Build an IPP_GET_JOB_ATTRIBUTES request...
855 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
856 request
->request
.op
.version
[1] = version
;
858 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
861 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
865 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
866 "requesting-user-name", NULL
, argv
[2]);
868 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
869 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
879 if ((response
= cupsDoRequest(http
, request
, resource
)) == NULL
)
880 ipp_status
= cupsLastError();
882 ipp_status
= response
->request
.status
.status_code
;
884 if (ipp_status
== IPP_NOT_FOUND
)
887 * Job has gone away and/or the server has no job history...
896 if (ipp_status
> IPP_OK_CONFLICT
)
898 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
899 ipp_status
!= IPP_PRINTER_BUSY
)
904 fprintf(stderr
, "ERROR: Unable to get job %d attributes (%s)!\n",
905 job_id
, ippErrorString(ipp_status
));
910 if (response
!= NULL
)
912 if ((job_state
= ippFindAttribute(response
, "job-state",
913 IPP_TAG_ENUM
)) != NULL
)
916 * Stop polling if the job is finished or pending-held...
919 if (job_state
->values
[0].integer
> IPP_JOB_PROCESSING
||
920 job_state
->values
[0].integer
== IPP_JOB_HELD
)
922 if ((job_sheets
= ippFindAttribute(response
,
923 "job-media-sheets-completed",
924 IPP_TAG_INTEGER
)) != NULL
)
925 fprintf(stderr
, "PAGE: total %d\n", job_sheets
->values
[0].integer
);
937 * Check the printer state and report it if necessary...
940 check_printer_state(http
, uri
, resource
, argv
[2], version
);
943 * Wait 10 seconds before polling again...
951 * Check the printer state and report it if necessary...
955 httpReconnect(http);*/
957 check_printer_state(http
, uri
, resource
, argv
[2], version
);
966 ippDelete(supported
);
969 * Remove the temporary file(s) if necessary...
978 #endif /* __APPLE__ */
981 * Return the queue status...
984 return (ipp_status
> IPP_OK_CONFLICT
? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
989 * 'check_printer_state()' - Check the printer state...
994 http_t
*http
, /* I - HTTP connection */
995 const char *uri
, /* I - Printer URI */
996 const char *resource
, /* I - Resource path */
997 const char *user
, /* I - Username, if any */
998 int version
) /* I - IPP version */
1000 ipp_t
*request
, /* IPP request */
1001 *response
; /* IPP response */
1005 * Check on the printer state...
1008 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1009 request
->request
.op
.version
[1] = version
;
1011 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1014 if (user
&& user
[0])
1015 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1016 "requesting-user-name", NULL
, user
);
1018 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1019 "requested-attributes", NULL
, "printer-state-reasons");
1025 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
1027 report_printer_state(response
);
1028 ippDelete(response
);
1034 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
1037 const char * /* O - Password */
1038 password_cb(const char *prompt
) /* I - Prompt (not used) */
1047 * If there is no password set in the device URI, return the
1048 * "authentication required" exit code...
1052 unlink(tmpfilename
);
1057 #endif /* __APPLE__ */
1059 exit(CUPS_BACKEND_AUTH_REQUIRED
);
1065 * 'report_printer_state()' - Report the printer state.
1068 int /* O - Number of reasons shown */
1069 report_printer_state(ipp_t
*ipp
) /* I - IPP response */
1071 int i
; /* Looping var */
1072 int count
; /* Count of reasons shown... */
1073 ipp_attribute_t
*reasons
; /* printer-state-reasons */
1074 const char *reason
; /* Current reason */
1075 const char *message
; /* Message to show */
1076 char unknown
[1024]; /* Unknown message string */
1077 const char *prefix
; /* Prefix for STATE: line */
1078 char state
[1024]; /* State string */
1081 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
1082 IPP_TAG_KEYWORD
)) == NULL
)
1088 for (i
= 0, count
= 0; i
< reasons
->num_values
; i
++)
1090 reason
= reasons
->values
[i
].string
.text
;
1092 strlcat(state
, prefix
, sizeof(state
));
1093 strlcat(state
, reason
, sizeof(state
));
1098 if (!strncmp(reason
, "media-needed", 12))
1099 message
= "Media tray needs to be filled.";
1100 else if (!strncmp(reason
, "media-jam", 9))
1101 message
= "Media jam!";
1102 else if (!strncmp(reason
, "moving-to-paused", 16) ||
1103 !strncmp(reason
, "paused", 6) ||
1104 !strncmp(reason
, "shutdown", 8))
1105 message
= "Printer off-line.";
1106 else if (!strncmp(reason
, "toner-low", 9))
1107 message
= "Toner low.";
1108 else if (!strncmp(reason
, "toner-empty", 11))
1109 message
= "Out of toner!";
1110 else if (!strncmp(reason
, "cover-open", 10))
1111 message
= "Cover open.";
1112 else if (!strncmp(reason
, "interlock-open", 14))
1113 message
= "Interlock open.";
1114 else if (!strncmp(reason
, "door-open", 9))
1115 message
= "Door open.";
1116 else if (!strncmp(reason
, "input-tray-missing", 18))
1117 message
= "Media tray missing!";
1118 else if (!strncmp(reason
, "media-low", 9))
1119 message
= "Media tray almost empty.";
1120 else if (!strncmp(reason
, "media-empty", 11))
1121 message
= "Media tray empty!";
1122 else if (!strncmp(reason
, "output-tray-missing", 19))
1123 message
= "Output tray missing!";
1124 else if (!strncmp(reason
, "output-area-almost-full", 23))
1125 message
= "Output bin almost full.";
1126 else if (!strncmp(reason
, "output-area-full", 16))
1127 message
= "Output bin full!";
1128 else if (!strncmp(reason
, "marker-supply-low", 17))
1129 message
= "Ink/toner almost empty.";
1130 else if (!strncmp(reason
, "marker-supply-empty", 19))
1131 message
= "Ink/toner empty!";
1132 else if (!strncmp(reason
, "marker-waste-almost-full", 24))
1133 message
= "Ink/toner waste bin almost full.";
1134 else if (!strncmp(reason
, "marker-waste-full", 17))
1135 message
= "Ink/toner waste bin full!";
1136 else if (!strncmp(reason
, "fuser-over-temp", 15))
1137 message
= "Fuser temperature high!";
1138 else if (!strncmp(reason
, "fuser-under-temp", 16))
1139 message
= "Fuser temperature low!";
1140 else if (!strncmp(reason
, "opc-near-eol", 12))
1141 message
= "OPC almost at end-of-life.";
1142 else if (!strncmp(reason
, "opc-life-over", 13))
1143 message
= "OPC at end-of-life!";
1144 else if (!strncmp(reason
, "developer-low", 13))
1145 message
= "Developer almost empty.";
1146 else if (!strncmp(reason
, "developer-empty", 15))
1147 message
= "Developer empty!";
1148 else if (strstr(reason
, "error") != NULL
)
1152 snprintf(unknown
, sizeof(unknown
), "Unknown printer error (%s)!",
1159 if (strstr(reasons
->values
[i
].string
.text
, "error"))
1160 fprintf(stderr
, "ERROR: %s\n", message
);
1161 else if (strstr(reasons
->values
[i
].string
.text
, "warning"))
1162 fprintf(stderr
, "WARNING: %s\n", message
);
1164 fprintf(stderr
, "INFO: %s\n", message
);
1168 fprintf(stderr
, "%s\n", state
);
1176 * 'run_pictwps_filter()' - Convert PICT files to PostScript when printing
1179 * This step is required because the PICT format is not documented and
1180 * subject to change, so developing a filter for other OS's is infeasible.
1181 * Also, fonts required by the PICT file need to be embedded on the
1182 * client side (which has the fonts), so we run the filter to get a
1183 * PostScript file for printing...
1186 int /* O - Exit status of filter */
1187 run_pictwps_filter(char **argv
, /* I - Command-line arguments */
1188 const char *filename
)/* I - Filename */
1190 struct stat fileinfo
; /* Print file information */
1191 const char *ppdfile
; /* PPD file for destination printer */
1192 int pid
; /* Child process ID */
1193 int fd
; /* Temporary file descriptor */
1194 int status
; /* Exit status of filter */
1195 const char *printer
; /* PRINTER env var */
1196 static char ppdenv
[1024]; /* PPD environment variable */
1200 * First get the PPD file for the printer...
1203 printer
= getenv("PRINTER");
1206 fputs("ERROR: PRINTER environment variable not defined!\n", stderr
);
1210 if ((ppdfile
= cupsGetPPD(printer
)) == NULL
)
1212 fprintf(stderr
, "ERROR: Unable to get PPD file for printer \"%s\" - %s.\n",
1213 printer
, ippErrorString(cupsLastError()));
1218 snprintf(ppdenv
, sizeof(ppdenv
), "PPD=%s", ppdfile
);
1223 * Then create a temporary file for printing...
1226 if ((fd
= cupsTempFd(pstmpname
, sizeof(pstmpname
))) < 0)
1228 fprintf(stderr
, "ERROR: Unable to create temporary file - %s.\n",
1236 * Get the owner of the spool file - it is owned by the user we want to run
1241 stat(argv
[6], &fileinfo
);
1245 * Use the OSX defaults, as an up-stream filter created the PICT
1249 fileinfo
.st_uid
= 1;
1250 fileinfo
.st_gid
= 80;
1254 chown(ppdfile
, fileinfo
.st_uid
, fileinfo
.st_gid
);
1256 fchown(fd
, fileinfo
.st_uid
, fileinfo
.st_gid
);
1259 * Finally, run the filter to convert the file...
1262 if ((pid
= fork()) == 0)
1265 * Child process for pictwpstops... Redirect output of pictwpstops to a
1276 * Change to an unpriviledged user...
1279 setgid(fileinfo
.st_gid
);
1280 setuid(fileinfo
.st_uid
);
1283 execlp("pictwpstops", printer
, argv
[1], argv
[2], argv
[3], argv
[4], argv
[5],
1285 perror("ERROR: Unable to exec pictwpstops");
1297 perror("ERROR: Unable to fork pictwpstops");
1305 * Now wait for the filter to complete...
1308 if (wait(&status
) < 0)
1310 perror("ERROR: Unable to wait for pictwpstops");
1326 fprintf(stderr
, "ERROR: pictwpstops exited with status %d!\n",
1329 fprintf(stderr
, "ERROR: pictwpstops exited on signal %d!\n",
1337 * Return with no errors..
1342 #endif /* __APPLE__ */
1346 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1350 sigterm_handler(int sig
) /* I - Signal */
1352 (void)sig
; /* remove compiler warnings... */
1355 * Remove the temporary file(s) if necessary...
1359 unlink(tmpfilename
);
1364 #endif /* __APPLE__ */
1371 * End of "$Id: ipp.c 5023 2006-01-29 14:39:44Z mike $".