xref: /titanic_50/usr/src/cmd/print/scripts/lpadmin (revision fa9e4066f08beec538e775443c5be79dd423fcab)
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 2004 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`
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 `grep bsdaddr=${HOST}, /etc/printers.conf`
67do
68        PRINTER=`echo ${LINE} | sed -e 's/^:bsdaddr='$HOST',//' -e 's/[,:].*//'`
69
70# If there is not an entry for this printer in
71#       /etc/lp/printers or /etc/lp/classes
72# Then delete the entry for this printer in /etc/printers.conf
73
74        if [ ! -d /etc/lp/printers/${PRINTER} -a ! -f /etc/lp/classes/${PRINTER} ] ;
75        then
76                logger -p lpr.debug -t "lpadmin[${PID}]" \
77                         "Removing $PRINTER entry from /etc/printers.conf"
78                ${LPSET} -x ${PRINTER}
79                status=$?
80
81                if [ ${status} -ne 0 ] ; then
82                        gettext "Warning: error removing ${PRINTER} " 1>&2
83			gettext "entry from /etc/printers.conf\n" 1>&2
84                        logger -p lpr.debug -t "lpadmin[${PID}]" \
85				"Call to lpset -x $PRINTER exits with ${status}"
86			exit_code=1
87                fi
88        fi
89done
90
91#
92# shutdown scheduler if there are no local printers
93#
94CONFIGS=/etc/lp/printers/*/configuration
95
96printers_configured=`echo $CONFIGS`
97if [ "$printers_configured" = "$CONFIGS" ]; then
98	svcprop -q svc:/application/print/server:default &&
99		svcadm disable svc:/application/print/server:default
100fi
101}
102
103delete_entries() {
104if [ ! -f /etc/printers.conf ] ; then
105        logger -p lpr.debug -t "lpadmin[${PID}]" \
106		"System error: Cannot access /etc/printers.conf"
107        gettext "lpadmin: System error; Cannot access /etc/printers.conf\n" 1>&2
108        exit 1
109fi
110
111# remove _default
112
113DEFAULTP=`${LPGET} _default | grep use | sed -e 's/[    ]*use=//'`
114${LPGET} -k bsdaddr ${DEFAULTP} >/dev/null 2>&1
115status=$?
116
117if [ ${status} -eq 0 ] ; then
118	${LPSET} -x _default
119	status=$?
120	if [ ${status} -ne 0 ] ; then
121		gettext "Warning: error removing _default entry from /etc/printers.conf\n" 1>&2
122		logger -p lpr.debug -t "lpadmin[${PID}]" \
123			"Call to lpset -x _default exits with ${status}"
124		exit_code=1
125	fi
126fi
127
128# delete entries in /etc/printers.conf for printers/classes that have
129# been deleted
130
131delete_local
132
133# Delete all the remote printers using bsdaddr
134
135for LINE in `grep bsdaddr /etc/printers.conf | grep -v ${HOST}`
136do
137        PRINTER=`echo $LINE | sed -e 's/^:bsdaddr=[^,]*,//' -e 's/[,:].*//'`
138        ${LPSET} -x $PRINTER
139	status=$?
140
141	if [ ${status} -ne 0 ] ; then
142		gettext "Warning: error removing ${PRINTER} entry from /etc/printers.conf\n" 1>&2
143		logger -p lpr.debug -t "lpadmin[${PID}]" \
144			"Call to lpset -x $PRINTER exits with ${status}"
145		exit_code=1
146	fi
147done
148}
149
150if [ $# -lt 1 ] ; then
151	usage
152	exit 1
153fi
154
155# Deal with the -d option independently since getopts does not handle
156# options that may or may not have arguments
157#
158first=$1
159second=$2
160third=$3
161
162if [ ${first} = "-d" ] ; then
163	# check that there are no extra arguments
164	if [ -n "${third}" ] ; then
165		usage
166		exit 1
167	fi
168
169
170	# be sure we have lpset and lpget
171	if [ ! -f ${LPSET} -o ! -f ${LPGET} ] ; then
172		gettext "lpadmin: System error; cannot set default printer\n" 1>&2
173		exit 2
174	fi
175
176	if [ ! -n "${second}" ] ; then
177		${LPGET} -n system _default >/dev/null 2>&1
178		exit_code=$?
179		if [ ${exit_code} -eq 0 ] ; then
180			# delete _default entry in /etc/printers.conf
181			${LPSET} -n system -x _default
182			exit_code=$?
183			if [ ${exit_code} -ne 0 ] ; then
184				gettext "lpadmin: System error while trying to delete default printer\n" 1>&2
185			fi
186		else
187			# there was no _default, the work is done
188			exit_code=0
189		fi
190	else
191		# add/change  _default entry in /etc/printers.conf
192		${LPGET} -k bsdaddr ${second} >/dev/null 2>&1
193		exit_code=$?
194		if [ $exit_code -eq 0 ] ; then
195			${LPSET} -n system -a use=${second} _default
196			exit_code=$?
197		else
198			echo "${second}: " 1>&2
199			gettext "undefined printer\n" 1>&2
200		fi
201
202	fi
203	exit ${exit_code}
204fi
205
206#		Strip off legal options
207while 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
208do
209	case $arg in
210	D)
211		description="${OPTARG}"
212	;;
213	p)
214		if [ -n "${delete}" ] ; then
215			usage
216		fi
217		printer=${OPTARG}
218	;;
219	s)
220		server=${OPTARG}
221	;;
222	v|U)
223		device=${OPTARG}
224		server=`uname -n`
225	;;
226	x)
227		if [ -n "${printer}" -o -n "${server}" -o \
228		     -n "${device}" -o -n "${description}" ] ; then
229			usage
230		fi
231		delete=${OPTARG}
232		printer=${OPTARG}
233		if [ ${printer} = "all" ] ; then
234			local="true"
235		fi
236	;;
237	S|M|A)
238		local="true"
239	;;
240	c)
241		class=${OPTARG}
242		local="true"
243		if [ ! -f ${LPGET} ] ; then
244			gettext "lpadmin: System error; cannot set class\n " 1>&2
245			exit 2
246		fi
247
248		${LPGET} "${class}" > /dev/null 2>&1
249		lpget_class=$?
250		if [ ${lpget_class} -eq 0 -a ! -r /etc/lp/classes/"${class}" ] ; then
251			gettext "lpadmin: ERROR: Can't create class ${class}.\n" 1>&2
252			gettext "           TO FIX: This is an existing printer name;\n" 1>&2
253			gettext "                   choose another name.\n" 1>&2
254			exit 1
255		fi
256	;;
257	r)
258		pconflocalclean="true"
259		local="true"
260	;;
261	esac
262done
263
264#
265# We don't have anything to do; let user know and bail
266#
267if [ ! -n "${printer}" -a ! -n "${delete}" -a ! -n "${local}" ] ; then
268	gettext "lpadmin: ERROR: Nothing to do.\n" 1>&2
269	gettext "        TO FIX: You must give one of these options:\n" 1>&2
270	gettext "		      -p, -d, -x -S\n" 1>&2
271	exit 1
272fi
273
274#
275#       Printer does not exist
276#       To be consistent with 2.5, assume adding local printer
277#
278if [ ! -n "${device}" -a ! -n "${server}" -a ! -n "${delete}" \
279					 -a ! -n "${local}" ] ; then
280	${LPGET} "${printer}" > /dev/null 2>&1
281	lpget_stat=$?
282	if [ ${lpget_stat} -ne 0 ] ; then
283		gettext "lpadmin: ERROR: Missing -U or -v option.\n" 1>&2
284		gettext "           TO FIX: Local printers must have\n" 1>&2
285		gettext "                   a port defined (-v option) or\n" 1>&2
286		gettext "                   have dial-out instructions (-U option).\n" 1>&2
287		exit 1
288	fi
289fi
290
291#
292#	Do the LP configuration for a local printer served by lpsched
293#
294if [ -f /usr/lib/lp/local/$cmd_name ] ; then
295	if [ -f /etc/lp/printers/${printer}/configuration -o -n "${device}" -o \
296	     -f /etc/lp/classes/${printer} -o -n "${local}" ] ; then
297		# to deal with multi-word arguments
298		CMD="/usr/lib/lp/local/$cmd_name"
299		while [ -n "$*" ] ; do
300			CMD="$CMD \"$1\""
301			shift
302		done
303		case "$CMD" in
304			*\"-D\")
305				CMD="$CMD \"\""
306			;;
307		esac
308		# if adding a printer, make sure scheduler is running
309		if [ -n "${printer}" -a ! -n "${delete}" -a \
310		    ! -p /var/spool/lp/fifos/FIFO ]; then
311			svcadm enable svc:/application/print/server:default
312		fi
313		eval $CMD
314		exit_code=$?
315	fi
316fi
317
318if [ $exit_code != 0 ] ; then
319	exit $exit_code
320fi
321
322
323#	split the "server" into printer and server
324if [ -n "${server}" ] ; then
325	if [ `echo ${server} | grep -c !` = 1 ] ; then
326		rem_printer=`echo ${server} | cut -d! -f2`
327	fi
328	server=`echo ${server} | cut -d! -f1`
329fi
330
331if [ -z "${rem_printer}" ] ; then
332	rem_printer=${printer}
333fi
334
335
336
337#
338#	Do the Solstice Print Configuration in /etc
339#
340if [ ! -f ${LPSET} -o ! -f ${LPGET} ] ; then
341	exit_code=2
342else
343	if [ -n "${delete}" ] ; then
344		if [ "${delete}" = "all" ] ; then
345			delete_entries
346   		else
347   			${LPSET} -n system -x ${delete}
348   			exit_code=$?
349			delete_local
350   		fi
351	fi
352
353	if [ -n "${printer}" -a -n "${server}" ] ; then
354		${LPSET} -n system \
355			-a "bsdaddr=${server},${rem_printer},Solaris" \
356			${printer}
357		exit_code=$?
358	fi
359	if [ -n "${printer}" -a -n "${description}" ] ; then
360		${LPSET} -n system -a "description=${description}" ${printer}
361		exit_code=$?
362	fi
363
364#	Add class for local printers only
365
366	if [ -n "${class}" -a -n "${printer}" \
367		-a -f /etc/lp/printers/${printer}/configuration ] ; then
368
369		${LPGET} "${class}" > /dev/null 2>&1
370		lpget_class=$?
371
372#		If the class doesn't already exist in printers.conf, add it.
373
374		if [ ${lpget_class} -ne 0 ] ; then
375			this_server=`uname -n`
376			${LPSET} -n system \
377				-a "bsdaddr=${this_server},${class},Solaris" ${class}
378			exit_code=$?
379		fi
380	fi
381fi
382
383# /usr/lib/lp/local/lpadmin has changed the database. This cleans up cruft in the
384# /etc/printers.conf file that refers to deleted objects.
385
386	if [ -n "${pconflocalclean}" ] ; then
387		delete_local
388	fi
389
390exit $exit_code
391