xref: /titanic_52/usr/src/cmd/print/scripts/lpadmin (revision ea8dc4b6d2251b437950c0056bc626b311c73c27)
1#!/bin/sh
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License, Version 1.0 only
7# (the "License").  You may not use this file except in compliance
8# with the License.
9#
10# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11# or http://www.opensolaris.org/os/licensing.
12# See the License for the specific language governing permissions
13# and limitations under the License.
14#
15# When distributing Covered Code, include this CDDL HEADER in each
16# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17# If applicable, add the following below this CDDL HEADER, with the
18# fields enclosed by brackets "[]" replaced with your own identifying
19# information: Portions Copyright [yyyy] [name of copyright owner]
20#
21# CDDL HEADER END
22#
23#
24# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27# ident	"%Z%%M%	%I%	%E% SMI"
28#
29PATH=/bin:/usr/bin:/usr/sbin export PATH
30
31TEXTDOMAIN="SUNW_OST_OSCMD"
32export TEXTDOMAIN
33
34LPSET=/usr/bin/lpset
35LPGET=/usr/bin/lpget
36
37HOST=`/bin/uname -n`
38LHOST="localhost"
39PID=$$
40
41cmd_name=lpadmin
42exit_code=0
43
44usage() {
45	gettext "Usage:\n" 1>&2
46	gettext "	lpadmin -p (printer) (options)\n" 1>&2
47	gettext "	lpadmin -x (dest)\n" 1>&2
48	gettext "	lpadmin -d (dest)\n" 1>&2
49	gettext "	lpadmin -S print-wheel -A alert-type [ -W minutes ]\n" 1>&2
50	gettext "		[ -Q requests ]\n" 1>&2
51	gettext "	lpadmin -M -f form-name [ -a [ -o filebreak ]\n" 1>&2
52	gettext "		[ -t tray-number ]]\n" 1>&2
53	exit 1
54}
55
56#
57# Delete entries in /etc/printers.conf for local printers/classes that no longer
58# exist in the /etc/lp database
59#
60
61delete_local() {
62
63# Get printer names for each local printer
64# grep /etc/printers.conf for each bsdaddr for this server
65# get printer name from that line
66
67for LINE in `/bin/grep bsdaddr /etc/printers.conf |
68	/bin/egrep -e ${HOST}\|${LHOST}`
69do
70        PRINTER=`echo ${LINE} | /bin/sed -e 's/^:bsdaddr='$LHOST',//' \
71		-e 's/^:bsdaddr='$HOST',//' \
72		-e 's/[,:].*//'`
73
74# If there is not an entry for this printer in
75#       /etc/lp/printers or /etc/lp/classes
76# Then delete the entry for this printer in /etc/printers.conf
77
78        if [ ! -d /etc/lp/printers/${PRINTER} -a ! -f /etc/lp/classes/${PRINTER} ] ;
79        then
80                logger -p lpr.debug -t "lpadmin[${PID}]" \
81                         "Removing $PRINTER entry from /etc/printers.conf"
82                ${LPSET} -x ${PRINTER}
83                status=$?
84
85                if [ ${status} -ne 0 ] ; then
86                        gettext "Warning: error removing ${PRINTER} " 1>&2
87			gettext "entry from /etc/printers.conf\n" 1>&2
88                        logger -p lpr.debug -t "lpadmin[${PID}]" \
89				"Call to lpset -x $PRINTER exits with ${status}"
90			exit_code=1
91                fi
92        fi
93done
94
95#
96# shutdown scheduler if there are no local printers
97#
98CONFIGS=/etc/lp/printers/*/configuration
99
100printers_configured=`echo $CONFIGS`
101if [ "$printers_configured" = "$CONFIGS" ]; then
102	svcprop -q svc:/application/print/server:default &&
103		svcadm disable svc:/application/print/server:default
104fi
105}
106
107delete_entries() {
108if [ ! -f /etc/printers.conf ] ; then
109        logger -p lpr.debug -t "lpadmin[${PID}]" \
110		"System error: Cannot access /etc/printers.conf"
111        gettext "lpadmin: System error; Cannot access /etc/printers.conf\n" 1>&2
112        exit 1
113fi
114
115# remove _default
116
117DEFAULTP=`${LPGET} _default | /bin/grep use | /bin/sed -e 's/[    ]*use=//'`
118${LPGET} -k bsdaddr ${DEFAULTP} >/dev/null 2>&1
119status=$?
120
121if [ ${status} -eq 0 ] ; then
122	${LPSET} -x _default
123	status=$?
124	if [ ${status} -ne 0 ] ; then
125		gettext "Warning: error removing _default entry from /etc/printers.conf\n" 1>&2
126		logger -p lpr.debug -t "lpadmin[${PID}]" \
127			"Call to lpset -x _default exits with ${status}"
128		exit_code=1
129	fi
130fi
131
132# delete entries in /etc/printers.conf for printers/classes that have
133# been deleted
134
135delete_local
136
137# Delete all the remote printers using bsdaddr
138
139for LINE in `/bin/grep bsdaddr /etc/printers.conf | /bin/egrep -v -e ${HOST}\|${LHOST}`
140do
141        PRINTER=`echo $LINE | /bin/sed -e 's/^:bsdaddr=[^,]*,//' -e 's/[,:].*//'`
142        ${LPSET} -x $PRINTER
143	status=$?
144
145	if [ ${status} -ne 0 ] ; then
146		gettext "Warning: error removing ${PRINTER} entry from /etc/printers.conf\n" 1>&2
147		logger -p lpr.debug -t "lpadmin[${PID}]" \
148			"Call to lpset -x $PRINTER exits with ${status}"
149		exit_code=1
150	fi
151done
152}
153
154if [ $# -lt 1 ] ; then
155	usage
156	exit 1
157fi
158
159# Deal with the -d option independently since getopts does not handle
160# options that may or may not have arguments
161#
162first=$1
163second=$2
164third=$3
165
166if [ ${first} = "-d" ] ; then
167	# check that there are no extra arguments
168	if [ -n "${third}" ] ; then
169		usage
170		exit 1
171	fi
172
173
174	# be sure we have lpset and lpget
175	if [ ! -f ${LPSET} -o ! -f ${LPGET} ] ; then
176		gettext "lpadmin: System error; cannot set default printer\n" 1>&2
177		exit 2
178	fi
179
180	if [ ! -n "${second}" ] ; then
181		${LPGET} -n system _default >/dev/null 2>&1
182		exit_code=$?
183		if [ ${exit_code} -eq 0 ] ; then
184			# delete _default entry in /etc/printers.conf
185			${LPSET} -n system -x _default
186			exit_code=$?
187			if [ ${exit_code} -ne 0 ] ; then
188				gettext "lpadmin: System error while trying to delete default printer\n" 1>&2
189			fi
190		else
191			# there was no _default, the work is done
192			exit_code=0
193		fi
194	else
195		# add/change  _default entry in /etc/printers.conf
196		${LPGET} -k bsdaddr ${second} >/dev/null 2>&1
197		exit_code=$?
198		if [ $exit_code -eq 0 ] ; then
199			${LPSET} -n system -a use=${second} _default
200			exit_code=$?
201		else
202			echo "${second}: " 1>&2
203			gettext "undefined printer\n" 1>&2
204		fi
205
206	fi
207	exit ${exit_code}
208fi
209
210#		Strip off legal options
211while getopts "A:ac:D:e:f:F:H:hi:I:lm:Mn:o:p:Q:r:S:s:T:u:U:v:W:x:t:P:" arg
212do
213	case $arg in
214	D)
215		description="${OPTARG}"
216	;;
217	p)
218		if [ -n "${delete}" ] ; then
219			usage
220		fi
221		printer=${OPTARG}
222	;;
223	s)
224		server=${OPTARG}
225	;;
226	v|U)
227		device=${OPTARG}
228		if [ ! -n "${server}" ] ; then
229			server=${HOST}
230		fi
231	;;
232	x)
233		if [ -n "${printer}" -o -n "${server}" -o \
234		     -n "${device}" -o -n "${description}" ] ; then
235			usage
236		fi
237		delete=${OPTARG}
238		printer=${OPTARG}
239		if [ ${printer} = "all" ] ; then
240			local="true"
241		fi
242	;;
243	S|M|A)
244		local="true"
245	;;
246	c)
247		class=${OPTARG}
248		local="true"
249		if [ ! -f ${LPGET} ] ; then
250			gettext "lpadmin: System error; cannot set class\n " 1>&2
251			exit 2
252		fi
253
254		${LPGET} "${class}" > /dev/null 2>&1
255		lpget_class=$?
256		if [ ${lpget_class} -eq 0 -a ! -r /etc/lp/classes/"${class}" ] ; then
257			gettext "lpadmin: ERROR: Can't create class ${class}.\n" 1>&2
258			gettext "           TO FIX: This is an existing printer name;\n" 1>&2
259			gettext "                   choose another name.\n" 1>&2
260			exit 1
261		fi
262	;;
263	r)
264		pconflocalclean="true"
265		local="true"
266	;;
267	esac
268done
269
270#
271# We don't have anything to do; let user know and bail
272#
273if [ ! -n "${printer}" -a ! -n "${delete}" -a ! -n "${local}" ] ; then
274	gettext "lpadmin: ERROR: Nothing to do.\n" 1>&2
275	gettext "        TO FIX: You must give one of these options:\n" 1>&2
276	gettext "		      -p, -d, -x -S\n" 1>&2
277	exit 1
278fi
279
280#
281#       Printer does not exist
282#       To be consistent with 2.5, assume adding local printer
283#
284if [ ! -n "${device}" -a ! -n "${server}" -a ! -n "${delete}" \
285					 -a ! -n "${local}" ] ; then
286	${LPGET} "${printer}" > /dev/null 2>&1
287	lpget_stat=$?
288	if [ ${lpget_stat} -ne 0 ] ; then
289		gettext "lpadmin: ERROR: Missing -U or -v option.\n" 1>&2
290		gettext "           TO FIX: Local printers must have\n" 1>&2
291		gettext "                   a port defined (-v option) or\n" 1>&2
292		gettext "                   have dial-out instructions (-U option).\n" 1>&2
293		exit 1
294	fi
295fi
296
297#
298#	Do the LP configuration for a local printer served by lpsched
299#
300if [ -f /usr/lib/lp/local/$cmd_name ] ; then
301	if [ -f /etc/lp/printers/${printer}/configuration -o -n "${device}" -o \
302	     -f /etc/lp/classes/${printer} -o -n "${local}" ] ; then
303		# to deal with multi-word arguments
304		CMD="/usr/lib/lp/local/$cmd_name"
305		while [ -n "$*" ] ; do
306			CMD="$CMD \"$1\""
307			shift
308		done
309		case "$CMD" in
310			*\"-D\")
311				CMD="$CMD \"\""
312			;;
313		esac
314		# if adding a printer, make sure scheduler is running
315		if [ -n "${printer}" -a ! -n "${delete}" -a \
316		    ! -p /var/spool/lp/fifos/FIFO ]; then
317			svcadm enable -s svc:/application/print/server:default
318		fi
319		eval $CMD
320		exit_code=$?
321	fi
322fi
323
324if [ $exit_code != 0 ] ; then
325	exit $exit_code
326fi
327
328
329#	split the "server" into printer and server
330if [ -n "${server}" ] ; then
331	if [ `echo ${server} | /bin/grep -c !` = 1 ] ; then
332		rem_printer=`echo ${server} | cut -d! -f2`
333	fi
334	server=`echo ${server} | cut -d! -f1`
335fi
336
337if [ -z "${rem_printer}" ] ; then
338	rem_printer=${printer}
339fi
340
341
342
343#
344#	Do the Solstice Print Configuration in /etc
345#
346if [ ! -f ${LPSET} -o ! -f ${LPGET} ] ; then
347	exit_code=2
348else
349	if [ -n "${delete}" ] ; then
350		if [ "${delete}" = "all" ] ; then
351			delete_entries
352   		else
353   			${LPSET} -n system -x ${delete}
354   			exit_code=$?
355			delete_local
356   		fi
357	fi
358
359	if [ -n "${printer}" -a -n "${server}" ] ; then
360		${LPSET} -n system \
361			-a "bsdaddr=${server},${rem_printer},Solaris" \
362			${printer}
363		exit_code=$?
364	fi
365	if [ -n "${printer}" -a -n "${description}" ] ; then
366		${LPSET} -n system -a "description=${description}" ${printer}
367		exit_code=$?
368	fi
369
370#	Add class for local printers only
371
372	if [ -n "${class}" -a -n "${printer}" \
373		-a -f /etc/lp/printers/${printer}/configuration ] ; then
374
375		${LPGET} "${class}" > /dev/null 2>&1
376		lpget_class=$?
377
378#		If the class doesn't already exist in printers.conf, add it.
379
380		if [ -n "${server}" ] ; then
381			CHOST=$server
382		else
383			CHOST=$HOST
384		fi
385		if [ ${lpget_class} -ne 0 ] ; then
386			${LPSET} -n system \
387				-a "bsdaddr=${CHOST},${class},Solaris" ${class}
388			exit_code=$?
389		fi
390	fi
391fi
392
393# /usr/lib/lp/local/lpadmin has changed the database. This cleans up cruft in the
394# /etc/printers.conf file that refers to deleted objects.
395
396	if [ -n "${pconflocalclean}" ] ; then
397		delete_local
398	fi
399
400exit $exit_code
401