04 Aug 2005 11:14

Only Share Some Printers

At work we have a server that has a bunch of printers that should not be shared, and incidentally also happens to be the file/print server. The standard cups which is so easy to implement shares all available printers, even the ones we didn't want to share. Here's how to share only some.

First, we have to not share everything. To do that, remove (or comment out) the [printers] section. It seems like load printers = no would be important too, but I have not been able to discern any difference when changing that.

Second, you need to put a share for each printer. Well, with 52 printers that is tedious and prone to decay, so I wrote a script to generate the stanzas:

#!/bin/sh
# cd /etc/samba
# ./gen_printers.sh > printers.conf

printers=`lpstat -a | cut -d ' ' -f 1 | egrep -v '(label|DEVNULL|test)'`

echo "# auto-generated `date -R`"

for p in $printers; do cat <<EOF
[$p]
path=/var/spool/samba
printable = yes
public = yes
writable = no
browseable = yes

EOF
done

Now that you have printers.conf, you just need to include it from smb.conf, which you do like this:

echo "include = /etc/samba/printers.conf" >> /etc/samba/smb.conf

Warning: I tried putting the include directive in the [global] section and it really messed with samba's mind, creating AWOL processes that needed to be killed with signal 9 and other ugly stuff. I don't know if it was the indentation or the file position, but it works best if you include it at the end of the file instead.