1#!/bin/bash
2
3# This script is an input filter for printcap printing on a unix machine. It
4# uses the smbclient program to print the file to the specified smb-based 
5# server and service.
6# For example you could have a printcap entry like this
7#
8# smb:lp=/dev/null:sd=/usr/spool/smb:sh:if=/usr/local/samba/smbprint
9#
10# which would create a unix printer called "smb" that will print via this 
11# script. You will need to create the spool directory /usr/spool/smb with
12# appropriate permissions and ownerships for your system.
13
14# Set these to the server and service you wish to print to 
15# In this example I have a WfWg PC called "lapland" that has a printer 
16# exported called "printer" with no password.
17
18#
19# Script further altered by hamiltom@ecnz.co.nz (Michael Hamilton)
20# so that the server, service, and password can be read from 
21# a /usr/var/spool/lpd/PRINTNAME/.config file.
22#
23# Script further modified by Richard Sharpe to fix some things.
24# Get rid of the -x on the first line, and add parameters
25#
26#    -t  now causes translate to be used when sending files
27#
28# Further modifications by Alfred Perlstein to fix some problems and
29# improve the quality of the code (3-Dec-2001).
30#
31# More hacking by Richard Sharpe to improve portability. 9-Dec-2001.
32#
33# In order for this to work the /etc/printcap entry must include an 
34# accounting file (af=...):
35#
36#   cdcolour:\
37#	:cm=CD IBM Colorjet on 6th:\
38#	:sd=/var/spool/lpd/cdcolour:\
39#	:af=/var/spool/lpd/cdcolour/acct:\
40#	:if=/usr/local/etc/smbprint:\
41#	:mx=0:\
42#	:lp=/dev/null:
43#
44# The /usr/var/spool/lpd/PRINTNAME/.config file should contain:
45#   server=PC_SERVER
46#   service=PR_SHARENAME
47#   password="password"
48#
49# E.g.
50#   server=PAULS_PC
51#   service=CJET_371
52#   password=""
53
54#smbclient=/usr/pkg/bin/smbclient
55# Assume that smbclient will be in the same place as smbprint
56
57smbclient="`dirname $0`/smbclient"
58
59#
60# The last parameter to the filter is the accounting file name.
61#   Extract the directory name from the file name.
62#   Concat this with /.config to get the config file.
63#
64TRANS=0
65eval acct_file=\${$#}
66spool_dir=`dirname $acct_file` 
67config_file=$spool_dir/.config
68
69# Should read the following variables set in the config file:
70#   server
71#   service
72#   password
73#   username (optional)
74#   IP (optional)
75#   debug (optional)
76#   debugsmb (optional)
77#   debugfile (optional)
78. $config_file
79
80if [ "x$password" = "x" ] ; then
81	password="-N"
82fi
83
84if [ "x$username" == "x" ] ; then
85	username="$server";
86fi
87
88while test $# -gt 0; do
89	case "$1" in
90	-t)
91		TRANS=1
92		;;
93
94	 *)   # Bad Parameters, ignore them ...
95		;;
96	 esac
97	 shift
98done
99
100command="print - ;"
101if [ $TRANS -eq 1 ]; then
102	command="translate;$command";
103fi
104
105##
106## Some security checks on the logfile if we are using it
107##
108## make the directory containing the logfile is necessary 
109## and set the permissions to be rwx for owner only
110##
111
112debugfile="/tmp/smb-print/logfile"
113logdir=`dirname $debugfile`
114if [ ! -d $logdir ]; then 
115	mkdir -m 0700 $logdir
116fi
117
118##
119## check ownership.  If I don't own it refuse to 
120## create the logfile
121##
122if [ ! -O $logdir ]; then
123	echo "user running script does not own $logdir. Ignoring any debug options."
124	debug=""
125fi
126
127touch $debugfile
128if [ -h $debugfile ]; then
129	echo "$debugful is a symlink.  Turning off debugging!"
130	debug=""
131fi
132
133##
134## We should be safe at this point to create the log file
135## without fear of a symlink attack -- move on to more script work.
136##
137
138if [ "x$debug" = "x" ] ; then 
139		debugfile=/dev/null debugargs=
140else
141        if [ $debug -eq 0 ] ; then
142		debugfile=/dev/null debugargs=
143	else
144		set -x; exec >>$debugfile 2>&1
145		debugargs="$debugfile."
146		#[ "x$debugsmb" == "x" ] || debugargs="$debugargs -d $debugsmb"
147	fi
148fi
149
150if [ "x$smbconf" != "x" ]; then
151
152	smbconf="-s $smbconf"
153
154fi
155
156if [ "x$IP" != "x" ]; then
157
158	IP="-I $IP"
159
160fi
161
162if [ "x$debugargs" != "x" ]; then
163
164	debugargs="-l $debugargs"
165
166fi
167
168$smbclient \
169	"\\\\$server\\$service" \
170	$password \
171	$smbconf \
172	$IP \
173	$debugargs \
174	-U $username \
175	-c "$command"
176#
177