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