2 "$Id: api-filter.shtml 7677 2008-06-19 23:22:19Z mike $"
4 Filter and backend programming introduction for the Common UNIX Printing
7 Copyright 2007-2009 by Apple Inc.
8 Copyright 1997-2006 by Easy Software Products, all rights reserved.
10 These coded instructions, statements, and computer programs are the
11 property of Apple Inc. and are protected by Federal copyright
12 law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 which should have been included with this file. If this file is
14 file is missing or damaged, see the license at "http://www.cups.org/".
17 <h2 class='title'><a name="OVERVIEW">Overview</a></h2>
19 <p>Filters (which include printer drivers and port monitors) and backends
20 are used to convert job files to a printable format and send that data to the
21 printer itself. All of these programs use a common interface for processing
22 print jobs and communicating status information to the scheduler. Each is run
23 with a standard set of command-line arguments:<p>
31 <dd>The user printing the job</dd>
34 <dd>The job name/title</dd>
37 <dd>The number of copies to print</dd>
40 <dd>The options that were provided when the job was submitted</dd>
43 <dd>The file to print (first program only)</dd>
46 <p>The scheduler runs one or more of these programs to print any given job. The
47 first filter reads from the print file and writes to the standard output, while
48 the remaining filters read from the standard input and write to the standard
49 output. The backend is the last filter in the chain and writes to the
52 <p>Filters are always run as a non-privileged user, typically "lp", with no
53 connection to the user's desktop. Backends are run either as a non-privileged
54 user or as root if the file permissions do not allow user or group execution.
55 The <a href="#PERMISSIONS">file permissions</a> section talks about this in
58 <h3><a name="SECURITY">Security Considerations</a></h3>
60 <p>It is always important to use security programming practices. Filters and
61 most backends are run as a non-priviledged user, so the major security
62 consideration is resource utilization - filters should not depend on unlimited
63 amounts of CPU, memory, or disk space, and should protect against conditions
64 that could lead to excess usage of any resource like infinite loops and
65 unbounded recursion. In addition, filters must <em>never</em> allow the user to
66 specify an arbitrary file path to a separator page, template, or other file
67 used by the filter since that can lead to an unauthorized disclosure of
68 information. <em>Always</em> treat input as suspect and validate it!</p>
70 <p>If you are developing a backend that runs as root, make sure to check for
71 potential buffer overflows, integer under/overflow conditions, and file
72 accesses since these can lead to privilege escalations. When writing files,
73 always validate the file path and <em>never</em> allow a user to determine
74 where to store a file.</p>
76 <blockquote><b>Note:</b>
78 <p><em>Never</em> write files to a user's home directory. Aside from the
79 security implications, CUPS is a network print service and as such the network
80 user may not be the same as the local user and/or there may not be a local home
81 directory to write to.</p>
83 <p>In addition, some operating systems provide additional security mechanisms
84 that further limit file system access, even for backends running as root. On
85 Mac OS X, for example, no backend may write to a user's home directory.</p>
88 <h3><a name="PERMISSIONS">File Permissions</a></h3>
90 <p>For security reasons, CUPS will only run filters and backends that are owned
91 by root and do not have world or group write permissions. The recommended
92 permissions for filters and backends are 0555 - read and execute but no write.
93 Backends that must run as root should use permissions of 0500 - read and execute
94 by root, no access for other users. Write permissions can be enabled for the
97 <p>To avoid a warning message, the directory containing your filter(s) must also
98 be owned by root and have world and group write disabled - permissions of 0755
99 or 0555 are strongly encouraged.</p>
101 <h3><a name="TEMPFILES">Temporary Files</a></h3>
103 <p>Temporary files should be created in the directory specified by the
104 "TMPDIR" environment variable. The
105 <a href="#cupsTempFile2"><code>cupsTempFile2</code></a> function can be
106 used to safely create temporary files in this directory.</p>
108 <h3><a name="COPIES">Copy Generation</a></h3>
110 <p>The <code>argv[4]</code> argument specifies the number of copies to produce
111 of the input file. In general, you should only generate copies if the
112 <em>filename</em> argument is supplied. The only exception to this are
113 filters that produce device-independent PostScript output, since the PostScript
114 filter <var>pstops</var> is responsible for generating copies of PostScript
117 <h3><a name="EXITCODES">Exit Codes</a></h3>
119 <p>Filters must exit with status 0 when they successfully generate print data
120 or 1 when they encounter an error. Backends can return any of the
121 <a href="#cups_backend_t"><code>cups_backend_t</code></a> constants.</p>
123 <h3><a name="ENVIRONMENT">Environment Variables</a></h3>
125 <p>The following environment variables are defined by the printing system
126 when running print filters and backends:</p>
130 <dt>APPLE_LANGUAGE</dt>
131 <dd>The Apple language identifier associated with the job
132 (Mac OS X only).</dd>
135 <dd>The job character set, typically "utf-8".</dd>
138 <dd>When a job is submitted to a printer class, contains the name of
139 the destination printer class. Otherwise this environment
140 variable will not be set.</dd>
142 <dt>CONTENT_TYPE</dt>
143 <dd>The MIME type associated with the file (e.g.
144 application/postscript).</dd>
146 <dt>CUPS_CACHEDIR</dt>
147 <dd>The directory where cache files can be stored. Cache files can be
148 used to retain information between jobs or files in a job.</dd>
150 <dt>CUPS_DATADIR</dt>
151 <dd>The directory where (read-only) CUPS data files can be found.</dd>
153 <dt>CUPS_FILETYPE</dt>
154 <dd>The type of file being printed: "job-sheet" for a banner page and
155 "document" for a regular print file.</dd>
157 <dt>CUPS_SERVERROOT</dt>
158 <dd>The root directory of the server.</dd>
161 <dd>The device-uri associated with the printer.</dd>
163 <dt>FINAL_CONTENT_TYPE</dt>
164 <dd>The MIME type associated with the printer (e.g.
165 application/vnd.cups-postscript).</dd>
168 <dd>The language locale associated with the job.</dd>
171 <dd>The full pathname of the PostScript Printer Description (PPD)
172 file for this printer.</dd>
175 <dd>The queue name of the class or printer.</dd>
178 <dd>The recommended amount of memory to use for Raster Image
179 Processors (RIPs).</dd>
182 <dd>The directory where temporary files should be created.</dd>
186 <h3><a name="MESSAGES">Communicating with the Scheduler</a></h3>
188 <p>Filters and backends communicate with the scheduler by writing messages
189 to the standard error file. The scheduler reads messages from all filters in
190 a job and processes the message based on its prefix. For example, the following
191 code sets the current printer state message to "Printing page 5":</p>
193 <pre class="example">
196 fprintf(stderr, "INFO: Printing page %d\n", page);
199 <p>Each message is a single line of text starting with one of the following
204 <dt>ALERT: message</dt>
205 <dd>Sets the printer-state-message attribute and adds the specified
206 message to the current error log file using the "alert" log level.</dd>
208 <dt>ATTR: attribute=value [attribute=value]</dt>
209 <dd>Sets the named printer or job attribute(s). Typically this is used
210 to set the <code>marker-colors</code>, <code>marker-levels</code>,
211 <code>marker-message</code>, <code>marker-names</code>,
212 <code>marker-types</code>, <code>printer-alert</code>, and
213 <code>printer-alert-description</code> printer attributes. Standard
214 <code>marker-types</code> values are listed in <a href='#TABLE1'>Table
217 <dt>CRIT: message</dt>
218 <dd>Sets the printer-state-message attribute and adds the specified
219 message to the current error log file using the "critical" log
222 <dt>DEBUG: message</dt>
223 <dd>Sets the printer-state-message attribute and adds the specified
224 message to the current error log file using the "debug" log level.</dd>
226 <dt>DEBUG2: message</dt>
227 <dd>Sets the printer-state-message attribute and adds the specified
228 message to the current error log file using the "debug2" log level.</dd>
230 <dt>EMERG: message</dt>
231 <dd>Sets the printer-state-message attribute and adds the specified
232 message to the current error log file using the "emergency" log
235 <dt>ERROR: message</dt>
236 <dd>Sets the printer-state-message attribute and adds the specified
237 message to the current error log file using the "error" log level.
238 Use "ERROR:" messages for non-persistent processing errors.</dd>
240 <dt>INFO: message</dt>
241 <dd>Sets the printer-state-message attribute. If the current log level
242 is set to "debug2", also adds the specified message to the current error
243 log file using the "info" log level.</dd>
245 <dt>NOTICE: message</dt>
246 <dd>Sets the printer-state-message attribute and adds the specified
247 message to the current error log file using the "notice" log level.</dd>
249 <dt>PAGE: page-number #-copies</dt>
250 <dt>PAGE: total #-pages</dt>
251 <dd>Adds an entry to the current page log file. The first form adds
252 #-copies to the job-media-sheets-completed attribute. The second
253 form sets the job-media-sheets-completed attribute to #-pages.</dd>
255 <dt>PPD: keyword=value [keyword=value ...]</dt>
256 <dd>Changes or adds keywords to the printer's PPD file. Typically
257 this is used to update installable options or default media settings
258 based on the printer configuration.</dd>
260 <dt>STATE: printer-state-reason [printer-state-reason ...]</dt>
261 <dt>STATE: + printer-state-reason [printer-state-reason ...]</dt>
262 <dt>STATE: - printer-state-reason [printer-state-reason ...]</dt>
263 <dd>Sets, adds, or removes printer-state-reason keywords to the
264 current queue. Typically this is used to indicate persistent media,
265 ink, toner, and configuration conditions or errors on a printer.
266 <a href='#TABLE2'>Table 2</a> lists the standard state keywords -
267 use vendor-prefixed ("com.acme.foo") keywords for custom states.
269 <blockquote><b>Note:</b>
271 <p>"STATE:" messages often provide visible alerts to the user. For example, on
272 Mac OS X setting a printer-state-reason value with an "-error" or "-warning"
273 suffix will cause the printer's dock item to bounce if the corresponding reason
274 is localized with a cupsIPPReason keyword in the printer's PPD file.</p>
278 <dt>WARNING: message</dt>
279 <dd>Sets the printer-state-message attribute and adds the specified
280 message to the current error log file using the "warning" log
285 <p>Messages without one of these prefixes are treated as if they began with
286 the "DEBUG:" prefix string.</p>
288 <div class='table'><table width='80%' summary='Table 1: Standard marker-types Values'>
289 <caption>Table 1: <a name='TABLE1'>Standard marker-types Values</a></caption>
299 <td>Developer unit</td>
306 <td>fuserCleaningPad</td>
307 <td>Fuser cleaning pad</td>
319 <td>Photo conductor</td>
327 <td>Staple supply</td>
331 <td>Toner supply</td>
334 <td>transferUnit</td>
335 <td>Transfer unit</td>
339 <td>Waste ink tank</td>
343 <td>Waste toner tank</td>
347 <td>Waste wax tank</td>
354 <div class='table'><table width='80%' summary='Table 2: Standard State Keywords'>
355 <caption>Table 2: <a name='TABLE2'>Standard State Keywords</a></caption>
364 <td>connecting-to-device</td>
365 <td>Connecting to printer but not printing yet</td>
369 <td>A cover is open on the printer</td>
372 <td>input-tray-missing</td>
373 <td>An input tray is missing from the printer</td>
376 <td>marker-supply-empty</td>
380 <td>marker-supply-low</td>
384 <td>marker-waste-almost-full</td>
385 <td>Waste tank almost full</td>
388 <td>marker-waste-full</td>
389 <td>Waste tank full</td>
393 <td>Out of media</td>
397 <td>Media is jammed in the printer</td>
401 <td>Low on media</td>
405 <td>Stop the printer</td>
409 <td>Unable to connect to printer</td>
413 <td>Out of toner</td>
417 <td>Low on toner</td>
422 <h3><a name="COMMUNICATING_BACKEND">Communicating with the Backend</a></h3>
424 <p>Filters can communicate with the backend via the
425 <a href="#cupsBackChannelRead"><code>cupsBackChannelRead</code></a> and
426 <a href="#cupsSideChannelDoRequest"><code>cupsSideChannelDoRequest</code></a>
428 <a href="#cupsBackChannelRead"><code>cupsBackChannelRead</code></a> function
429 reads data that has been sent back from the device and is typically used to
430 obtain status and configuration information. For example, the following code
431 polls the backend for back-channel data:</p>
433 <pre class="example">
434 #include <cups/cups.h>
439 /* Use a timeout of 0.0 seconds to poll for back-channel data */
440 bytes = cupsBackChannelRead(buffer, sizeof(buffer), 0.0);
443 <p>Filters can also use <code>select()</code> or <code>poll()</code> on the
444 back-channel file descriptor (3 or <code>CUPS_BC_FD</code>) to read data only
445 when it is available.</p>
448 <a href="#cupsSideChannelDoRequest"><code>cupsSideChannelDoRequest</code></a>
449 function allows you to get out-of-band status information and do synchronization
450 with the device. For example, the following code gets the current IEEE-1284
451 device ID string from the backend:</p>
453 <pre class="example">
454 #include <cups/sidechannel.h>
458 <a href="#cups_sc_status_t">cups_sc_status_t</a> status;
460 /* Tell cupsSideChannelDoRequest() how big our buffer is, less 1 byte for
461 nul-termination... */
462 datalen = sizeof(data) - 1;
464 /* Get the IEEE-1284 device ID, waiting for up to 1 second */
465 status = <a href="#cupsSideChannelDoRequest">cupsSideChannelDoRequest</a>(CUPS_SC_CMD_GET_DEVICE_ID, data, &datalen, 1.0);
467 /* Use the returned value if OK was returned and the length is non-zero */
468 if (status == CUPS_SC_STATUS_OK && datalen > 0)
469 data[datalen] = '\0';
474 <h3><a name="COMMUNICATING_FILTER">Communicating with Filters</a></h3>
476 <p>Backends communicate with filters using the reciprocal functions
477 <a href="#cupsBackChannelWrite"><code>cupsBackChannelWrite</code></a>,
478 <a href="#cupsSideChannelRead"><code>cupsSideChannelRead</code></a>, and
479 <a href="#cupsSideChannelWrite"><code>cupsSideChannelWrite</code></a>. We
480 recommend writing back-channel data using a timeout of 1.0 seconds:</p>
482 <pre class="example">
483 #include <cups/cups.h>
488 /* Obtain data from printer/device */
491 /* Use a timeout of 1.0 seconds to give filters a chance to read */
492 cupsBackChannelWrite(buffer, bytes, 1.0);
495 <p>The <a href="#cupsSideChannelRead"><code>cupsSideChannelRead</code></a>
496 function reads a side-channel command from a filter, driver, or port monitor.
497 Backends can either poll for commands using a <code>timeout</code> of 0.0, wait
498 indefinitely for commands using a <code>timeout</code> of -1.0 (probably in a
499 separate thread for that purpose), or use <code>select</code> or
500 <code>poll</code> on the <code>CUPS_SC_FD</code> file descriptor (4) to handle
501 input and output on several file descriptors at the same time.</p>
503 <p>Once a command is processed, the backend uses the
504 <a href="#cupsSideChannelWrite"><code>cupsSideChannelWrite</code></a> function
505 to send its response. For example, the following code shows how to poll for a
506 side-channel command and respond to it:</p>
508 <pre class="example">
509 #include <cups/sidechannel.h>
511 <a href="#cups_sc_command_t">cups_sc_command_t</a> command;
512 <a href="#cups_sc_status_t">cups_sc_status_t</a> status;
514 int datalen = sizeof(data);
516 /* Poll for a command... */
517 if (!<a href="#cupsSideChannelRead">cupsSideChannelRead</a>(&command, &status, data, &datalen, 0.0))
521 /* handle supported commands, fill data/datalen/status with values as needed */
524 status = CUPS_SC_STATUS_NOT_IMPLEMENTED;
529 /* Send a response... */
530 <a href="#cupsSideChannelWrite">cupsSideChannelWrite</a>(command, status, data, datalen, 1.0);
534 <h3><a name="SNMP">Doing SNMP Queries with Network Printers</a></h3>
536 <p>The Simple Network Management Protocol (SNMP) allows you to get the current
537 status, page counter, and supply levels from most network printers. Every
538 piece of information is associated with an Object Identifier (OID), and
539 every printer has a <em>community</em> name associated with it. OIDs can be
540 queried directly or by "walking" over a range of OIDs with a common prefix.</p>
542 <p>The two CUPS SNMP functions provide a simple API for querying network
543 printers through the side-channel interface. Each accepts a string containing
544 an OID like ".1.3.6.1.2.1.43.10.2.1.4.1.1" (the standard page counter OID)
545 along with a timeout for the query.</p>
547 <p>The <a href="#cupsSideChannelSNMPGet"><code>cupsSideChannelSNMPGet</code></a>
548 function queries a single OID and returns the value as a string in a buffer
551 <pre class="example">
552 #include <cups/sidechannel.h>
555 int datalen = sizeof(data);
557 if (<a href="#cupsSideChannelSNMPGet">cupsSideChannelSNMPGet</a>(".1.3.6.1.2.1.43.10.2.1.4.1.1", data, &datalen, 5.0)
558 == CUPS_SC_STATUS_OK)
560 /* Do something with the value */
561 printf("Page counter is: %s\n", data);
566 <a href="#cupsSideChannelSNMPWalk"><code>cupsSideChannelSNMPWalk</code></a>
567 function allows you to query a whole group of OIDs, calling a function of your
568 choice for each OID that is found:</p>
570 <pre class="example">
571 #include <cups/sidechannel.h>
574 my_callback(const char *oid, const char *data, int datalen, void *context)
576 /* Do something with the value */
577 printf("%s=%s\n", oid, data);
584 <a href="#cupsSideChannelSNMPWalk">cupsSNMPSideChannelWalk</a>(".1.3.6.1.2.1.43", 5.0, my_callback, my_data);