4.11. Printcap Basics

In the previous sections we have used simple printcap entries to define how to set up filters and pass parameters to them. We will now examine the printcap database in more detail.

The LPRng server and client software gets their configuration information from:



Each print queue or printer has a name which is used to look up the printcap information for the printer. The /etc/printcap file is the default location for the printcap information, although it can also be obtained from database servers, or generated by programs. See the Using Programs To Get Printcap Information section for details.

We will use a more complex printcap file to explore how LPRng gets the printcap information. Put the following lines in the /etc/printcap file:

    # client entry
    lp:tc=.client
    lp2:tc=.client
    .client:
      :lp=%P@localhost
      :force_localhost
    
    lp:server
      :cm=The Main Print Queue
      :lp=/tmp/lp
      :tc=.common
    
    lp2:server
      :cm=The Second Print Queue
      :lp=/tmp/lp2
      :tc=.common
    
    .common:
      :sd=/var/spool/lpd/%P
      :mx=0
The lpc client command is very useful to see how LPRng uses this printcap information:
    h4: {214} % lpc client
    Config
    h4: {215} % lpc client all
    Config
     :lpd_port=2000
     :printcap_path=/var/tmp/LPD/printcap
    
    Names
     :.client=.client
     :.common=.common
     :lp=lp
     :lp2=lp2
     :main=lp
    
    All
     :lp
     :lp2
    
    Printcap Information
    lp|main
     :force_localhost
     :lp=lp@localhost
    lp2
     :force_localhost
     :lp=lp2@localhost


The lpc client all command shows all of the configuration and printcap information, and is the handiest one for system debugging and diagnostics. The Name information is the names of the printcap entries that have been found in the database and is listed in sorted order. The All are entries that correspond to actual queues or printers and are listed in the order that they appear in the printcap or according to an order specified by the system administrator. (See the all Printcap Entry for details.)

4.11.1. Printcap Processing Format

Queue or printer names must start with an alphanumeric character, and contain only alphanumerics, hyphens (-) and underscores (_). To avoid known and nasty problems with sending and receiving print jobs from case sensitive and case insensitive systems, LPRng brutally lowercases all printcap entry names and printer names.

The printcap file is processed by reading it line by line and composing the individual printcap entries. Each entry has an name and one or more aliases. The entries in the printcap assign values to options. These can have the format:

    option=string value \n with escapes
    flag         # equivalent to flag=1
    flag@        # equivalent to flag=0
    option#value # equivalent to option=value


An option will have the last value that occurs in the printcap entry.

Our example shows the use of the tc (termcap include) facility. The :tc value is a list of printcap entries that should be prefixed to the start of the printcap entry in which it appears. This allows options to be set in the printcap entry which will override the values in the :tc included entry. For convenience, the options are displayed in sorted order.

The LPRng clients and lpd server may require a different set of options for the same spool queue. The clients require options whose values tell the clients how the contact the lpd server and transfer a print job or query to it. The lpd server needs options that tell it how to either print a job or forward it to another lpd server. The :client or :server option marks a printcap entry as for client or lpd server use only; unmarked entries are used by both server and client. The lpc client command shows the printcap information that the LPRng clients would use. For example, here is what the lpd server would use:

    h4: {216} % lpc server all
    Config
     :lpd_port=2000
     :printcap_path=/var/tmp/LPD/printcap
    
    Names
     :.client=.client
     :.common=.common
     :lp=lp
     :lp2=lp2
     :main=lp
    
    All
     :lp
     :lp2
    
    Printcap Information
    lp|main
     :cm=The Main Print Queue
     :force_localhost
     :lp=/tmp/lp
     :mx=0
     :sd=/var/spool/lpd/%P
     :server
    lp2
     :cm=The Second Print Queue
     :force_localhost
     :lp=/tmp/lp2
     :mx=0
     :sd=/var/spool/lpd/%P
     :server


When we select the server printcap information, we see that the :sd option has been added, and the :lp replaced with new values.

We can use the :oh (on this host) option to mark printcap entries for use by a selected set of hosts. For example:

    lp:oh=10.0.0.0/255.255.255.0,*.private,!10.0.0.10
      :lp=%P@10.0.0.10


The :oh option takes a list of IP addresses and masks or glob patterns, and applies these to the IP addresses or list of Fully Qualified Domain Names for the current host. If there is a for at least one IP address or pattern in the list match then the entry is used. An exclamation mark (!) inverts the sense of the match, and the entry is used if the match fails.

Finally, we can use the wildcard facility to cause a default printcap entry to be used:

    lp|*:cm=Wildcard Alias - %P=lp, %Q=wanted
      :lp=%P@10.0.0.10
    *:cm=Wildcard Name- %P=wanted, %Q=wanted
      :lp=%P@10.0.0.10


The LPRng software first searches the printcap information for an exact match. If none is found then it searches for the first wildcard entry that matches the printer name. If the wildcard is used as an alias, then the printcap entry is simply selected for use, with the printer name and queue name selected as shown above. We can also use partial matching as well:

    lp|lp_*
      :lp=%P@10.0.0.10
    qt|qt_*
      :lp=%P@10.0.0.12


In the example above the first entry matches lp and all printer names starting with lp_, while the second entry matches qt and all printer names starting with qt_. This can be useful when setting up a family of spool queues as discussed in later sections.

4.11.2. Printcap Information From Programs and Databases

There many administrators store system information on a database server and having programs or utilities get their configuration information from this server. The use of the database allows easier system administration and also centralizes the administration. Rather than build in the various types of database access, the LPRng software allows the use of programs to obtain printcap information. This not only allows any type of database to be used, but also removes any legal or license restrictions on the redistribution of the actual software.

We will use very simple example to show how you can use a program to get printcap information. First, you must configure the LPRng software to use a program to get the filter information. This is done by setting a value in the lpc.conf file (usually /etc/lpd.conf or /usr/local/etc/lpd.conf). Copy your lpd.conf file to lpd.conf.bak and then add the following line to the end of the file:

    printcap_path=|/tmp/getpc
    
    h4: {217} % cd /etc
    h4: {218} % cp lpd.conf lpd.conf.bak
    h4: {219} % echo 'printcap_path=|/tmp/getpc' >>lpd.conf


Next, edit the /tmp/getpc file and set its values as shown below.

    set /tmp/getpc:
    
      #!/bin/sh
      # /tmp/getpc
      echo PROG $0 "$@" >>/tmp/trace
      cat >>/tmp/trace
      cat <<EOF
      lp:lp=test@host
      EOF
      exit 0
    
    h4: {220} % chmod 755 /tmp/getpc
    h4: {221} % echo testing | /tmp/getpc -aoption
    lp:lp=test@host
    h4: {222} % cat /tmp/trace
    PROG /tmp/getpc -aoption
    testing


After you have tested the getpc script, use the lpc client all command:

    h4: {223} % lpc client all
    Config
     :lockfile=/var/tmp/LPD/lpd
     :lpd_port=2000
     :printcap_path=|/tmp/getpc
    
    Names
     :lp=lp
    
    All
     :lp
    
    Printcap Information
    lp
     :lp=test@host
    h4: {224} % cat /tmp/trace
    h4: {225} % cat /tmp/trace
    PROG /tmp/getpc -Pall -aacct -l66 -sstatus \
      -t2000-05-05-08:40:51.000 -w80 -x0 -y0 acct
    all
    PROG /tmp/getpc -Pall -aacct -l66 -sstatus \
      -t2000-05-05-08:40:51.000 -w80 -x0 -y0 acct
    *


As seen from the /tmp/trace file, the getpc program is invoked with the standard filter parameters. The -P command line literal is set to the name of the printcap entry and the name of the entry is written to the filter's STDIN. If the entry is not found, then the wildcard printcap entry will be requested. The -P literal is not set to *, as this has the possibility of opening a security loophole when a shell script parses the filter's command line options.

You restore the original lpd.conf file to restore the system to normal operation.

    h4: {226} % cd /etc
    h4: {227} % cp lpd.conf.bak lpd.conf


When using the program method to return information, special consideration should be given to the all request. If there is not an explicit all entry, then the program should take appropriate steps to enumerate the values in the database, or report that there is a missing all entry to the appropriate administrative authority.

4.11.3. User Printcap Information

In addition to the system printcap, each user can define a private printcap file that will be read after the system printcap. Users can define LPRng client entries and can augment the system printcap information.

By default, ${HOME}/.printcap is the the user printcap file. Here is a simple example of a user printcap file.

    # remote printer - default
    lp:lp=raw@localhost
      :ifhp=model=laserjet4
      :filter=/usr/local/libexec/filters/ifhp
    # direct connection to printer over TCP/IP connection
    lp:lp=10.0.0.5%9100
      :direct
      :ifhp=model=phaser
      :filter=/usr/local/libexec/filters/ifhp

The two examples show how a simple printer definition can be created. The first example shows how to create a simple way to send a file directly to a remote print queue after passing it through a filter. This is usually called client side filtering.

The second example is more interesting. Here we do the same thing, but we open a connection to the remote port on a host and send the print job. We do not spool the print job but send it directly. This is called lightweight lpr printing.

While the user printcap file is read after the system printcap file, the order of printcap entries is modified so that any entries that appeared in the user printcap file will appear before entries in the system printcap file. This allows users to modify the order in which printer entries are displayed.