xref: /titanic_44/usr/src/cmd/print/scripts/lpadmin (revision 104d3bde5b4ac46904f144d3676110fc57a69603)
1#!/bin/ksh
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 2009 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26#
27
28PATH=/bin:/usr/bin:/usr/sbin export PATH
29
30TEXTDOMAIN="SUNW_OST_OSCMD"
31export TEXTDOMAIN
32
33PFEXEC=/usr/bin/pfexec
34LPSET=/usr/bin/lpset
35LPGET=/usr/bin/lpget
36LPSTAT=/usr/bin/lpstat
37LPADMIN=/usr/lib/lp/local/lpadmin
38LPFILTER=/usr/sbin/lpfilter
39COMM=/usr/bin/comm
40PPDMGR=/usr/sbin/ppdmgr
41MKTEMP="/usr/bin/mktemp -t"
42
43HOST=$(/bin/uname -n)
44exit_code=0
45
46usage() {
47	gettext "Usage:\n" 1>&2
48	gettext "	lpadmin -p (printer) (options)\n" 1>&2
49	gettext "	lpadmin -x (dest)\n" 1>&2
50	gettext "	lpadmin -d (dest)\n" 1>&2
51	gettext "	lpadmin -S print-wheel -A alert-type [ -W minutes ]\n" 1>&2
52	gettext "		[ -Q requests ]\n" 1>&2
53	gettext "	lpadmin -M -f form-name [ -a [ -o filebreak ]\n" 1>&2
54	gettext "		[ -t tray-number ]]\n" 1>&2
55	exit 1
56}
57
58# create a filter table for LP service
59lp_config_filters() {
60	if [[ ! -f /etc/lp/filter.table ]] ; then
61		cd /etc/lp/fd ; for filter in *.fd ; do
62			${PFEXEC} ${LPFILTER} \
63				-f $(/usr/bin/basename $filter .fd) \
64				-F $filter
65		done
66	fi
67}
68
69# enable/disable LP related service(s)
70lp_config_service() {	# (enable | disable)
71	svcadm ${1} -s svc:/application/print/server:default
72	# svcadm ${1} -s svc:/application/print/rfc1179:default
73	# svcadm ${1} -s svc:/application/print/ipp-listener:default
74}
75
76# synchronize printers.conf with LP configuration changes
77lp_config_sync_pconf() {	# (pre) (post)
78	ADDED=$(${COMM} -13 ${1} ${2})
79	REMOVED=$(${COMM} -23 ${1} ${2})
80
81	lp_server=${server:-${HOST}}
82	for DEST in ${ADDED} ; do
83		lp_uri="ipp://${lp_server}/printers/${DEST}"
84		lp_bsdaddr="${lp_server},${DEST},Solaris"
85		${LPSET} -n system \
86			-a "printer-uri-supported=${lp_uri}" \
87			-a "bsdaddr=${lp_bsdaddr}" \
88		 	${DEST} 2>/dev/null
89	done
90
91	for DEST in ${REMOVED} ; do
92		${LPSET} -n system -x ${DEST} 2>/dev/null
93	done
94}
95
96# Delete all destinations in printers.conf
97delete_all() {
98	for DEST in $(lpget -n system list | egrep -e '.+:$' | sed -e 's/://')
99	do
100		${LPSET} -n system -x ${DEST}
101		status=$?
102	done
103}
104
105# Call the ppdmgr utility to add a new PPD file to the system.
106#
107# $1  - path to PPD file
108# $2  - label name (optional)
109add_new_ppd_file() {
110	# Add new ppd file and echo full path it was actually saved to
111	ppdmgrcmd="${PFEXEC} ${PPDMGR} -a ${1} -w"
112
113	ppderrfile=$(${MKTEMP} lpadminerror.XXXXXX)
114	if [[ -z "${ppderrfile}" ]] ; then
115		gettext "lpadmin: System error; cannot create temporary file\n" 1>&2
116		exit 2
117	fi
118	ppd_file=$(${ppdmgrcmd} 2>${ppderrfile})
119	ppdmgrrc=$?
120	if [[ -s "${ppderrfile}" ]] ; then
121		print -n "lpadmin: " 1>&2
122		cat ${ppderrfile} 1>&2
123		rm -f ${ppderrfile} >/dev/null 2>&1
124		if [[ ${ppdmgrrc} -ne 0 ]] ; then
125			exit 1
126		fi
127	fi
128	rm -f ${ppderrfile} >/dev/null 2>&1
129}
130
131#
132# Execution begins here
133#
134
135# be sure that we can run lpset and lpget
136if [[ ! -x ${LPSET} || ! -x ${LPGET} ]] ; then
137	gettext "lpadmin: System error; cannot set default printer\n" 1>&2
138	exit 2
139fi
140
141if [[ $# -lt 1 ]] ; then
142	usage
143	exit 1
144fi
145
146# Deal with the -d option independently since getopts does not handle
147# options that may or may not have arguments
148#
149if [[ ${1} = "-d" ]] ; then
150	if [[ $# -eq 1 ]] ; then	# remove the "default"
151		${LPGET} -n system _default >/dev/null 2>&1
152		exit_code=$?
153
154		if [[ ${exit_code} -eq 0 ]] ; then
155			${LPSET} -n system -x _default
156			exit_code=$?
157		else	# no default, nothing to do
158			exit_code=0
159		fi
160	elif [[ $# -eq 2 ]] ; then	# add/change the "default"
161		${LPGET} -k bsdaddr ${2} >/dev/null 2>&1
162		exit_code=$?
163
164		if [[ $exit_code -eq 0 ]] ; then
165			${LPSET} -n system -a "use=${2}" _default
166			exit_code=$?
167		else	# can't set default to an unconfigured printer
168			gettext "${2}: undefined printer\n" 1>&1
169		fi
170	else				# invalid usage
171		usage
172		exit 1
173	fi
174
175	exit ${exit_code}
176fi
177
178#		Strip off legal options
179while 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
180do
181	case $arg in
182	D)
183		description="${OPTARG}"
184	;;
185	n)
186		ppd_file="${OPTARG}"
187	;;
188	p)
189		if [[ -n "${delete}" ]] ; then
190			usage
191		fi
192		printer=${OPTARG}
193	;;
194	s)
195		server=${OPTARG}
196	;;
197	v|U)
198		device=${OPTARG}
199		if [[ ! -n "${server}" ]] ; then
200			server=${HOST}
201		fi
202		local="true"
203	;;
204	x)
205		if [[ -n "${printer}" || -n "${server}" || \
206		     -n "${device}" || -n "${description}" ]] ; then
207			usage
208		fi
209		delete=${OPTARG}
210		printer=${OPTARG}
211		if [[ ${printer} = "all" ]] ; then
212			local="true"
213		fi
214	;;
215	S|M|A)
216		local="true"
217	;;
218	c)
219		class=${OPTARG}
220		local="true"
221		if [[ ! -f ${LPGET} ]] ; then
222			gettext "lpadmin: System error; cannot set class\n " 1>&2
223			exit 2
224		fi
225
226		${LPGET} "${class}" > /dev/null 2>&1
227		lpget_class=$?
228		if [[ ${lpget_class} -eq 0 && ! -r /etc/lp/classes/"${class}" ]] ; then
229			gettext "lpadmin: ERROR: Can't create class ${class}.\n" 1>&2
230			gettext "           TO FIX: This is an existing printer name;\n" 1>&2
231			gettext "                   choose another name.\n" 1>&2
232			exit 1
233		fi
234	;;
235	r)
236		local="true"
237	;;
238	esac
239done
240
241#
242# We don't have anything to do; let user know and bail
243#
244if [[ ! -n "${printer}" && ! -n "${delete}" && ! -n "${local}" ]] ; then
245	gettext "lpadmin: ERROR: Nothing to do.\n" 1>&2
246	gettext "        TO FIX: You must give one of these options:\n" 1>&2
247	gettext "		      -p, -d, -x -S\n" 1>&2
248	exit 1
249fi
250
251#
252#       Printer does not exist
253#       To be consistent with 2.5, assume adding local printer
254#
255if [[ ! -n "${device}" && ! -n "${server}" && ! -n "${delete}" && \
256	  ! -n "${local}" ]] ; then
257	${LPGET} "${printer}" > /dev/null 2>&1
258	lpget_stat=$?
259	if [[ ${lpget_stat} -ne 0 ]] ; then
260		gettext "lpadmin: ERROR: Missing -U or -v option.\n" 1>&2
261		gettext "           TO FIX: Local printers must have\n" 1>&2
262		gettext "                   a port defined (-v option) or\n" 1>&2
263		gettext "                   have dial-out instructions (-U option).\n" 1>&2
264		exit 1
265	fi
266fi
267
268#	process the "server" value
269#	It can be a hostname, UUCP form (server!queue), RCMD form(queue@server),
270#	or in URI form ({scheme}://{endpoint})
271#
272case "${server}" in
273	*://*)	# URI form
274		uri=${server}
275		rem_printer=$(expr "${server}" : ".*://.*/\([^/]*\)")
276		server=$(expr "${server}" : ".*://\([^/]*\)/.*")
277		;;
278	*@*)	# RCMD form
279		rem_printer=$(expr "${server}" : "\(.*\)@.*")
280		server=$(expr "${server}" : ".*@\(.*\)")
281		;;
282	*!*)	# UUCP form
283		rem_printer=$(expr "${server}" : ".*!\(.*\)")
284		server=$(expr "${server}" : "\(.*\)!.*")
285		;;
286	*)	# hostname
287		rem_printer=${printer}
288		;;
289esac
290
291# if there is a "device" or LP configuration, it's local
292if [[ -n "${device}" || -f /etc/lp/printers/${printer}/configuration || \
293      -f /etc/lp/classes/${printer} ]] ; then
294	local="true"
295fi
296
297# Do the LP configuration for a local printer served by lpsched
298if [[ -x ${LPADMIN} && -n "${local}" ]] ; then
299	# enumerate LP configured printers before modification
300	PRE=$(${MKTEMP} lpadmin-pre.XXXXXX)
301	if [[ -z "${PRE}" ]] ; then
302		gettext "lpadmin: System error; cannot create temporary file\n" 1>&2
303		exit 2
304	fi
305
306	(/bin/ls /etc/lp/printers 2>/dev/null ; /bin/ls /etc/lp/classes \
307		2>/dev/null) >${PRE}
308
309	# if there are no printers configured, enable LP service(s)
310	[[ ! -s "${PRE}" ]] && lp_config_service enable
311
312	# add filters to LP service
313	lp_config_filters
314
315	# add new ppd file to PPD file repositories
316	if [[ -n "${ppd_file}" && -x ${PPDMGR} ]] ; then
317		add_new_ppd_file "${ppd_file}"
318	fi
319
320	# modify LP destination(s)
321	CMD="${PFEXEC} ${LPADMIN}"
322	while [[ -n "$*" ]] ; do	# to deal with multi-word arguments
323		CMD="$CMD \"$1\""
324		# replace the ppd_file originally specified with the -n option
325		# with the one returned from call to ppdmgr
326		if [[ "${1}" = "-n" ]] ; then
327			CMD="$CMD \"${ppd_file}\""
328			shift
329		fi
330		shift
331	done
332	case "$CMD" in
333		*\"-D\")
334			CMD="$CMD \"\""
335		;;
336	esac
337
338	# execute the LP lpadmin command
339	eval $CMD
340	exit_code=$?
341
342	# enumerate LP configured printers after modification
343	POST=$(${MKTEMP} lpadmin-post.XXXXXX)
344	if [[ -z "${POST}" ]] ; then
345		gettext "lpadmin: System error; cannot create temporary file\n" 1>&2
346		/bin/rm -f ${PRE} >/dev/null 2>&1
347		exit 2
348	fi
349
350	(/bin/ls /etc/lp/printers 2>/dev/null ; /bin/ls /etc/lp/classes \
351		2>/dev/null) >${POST}
352
353	# if there are no destinations, disable the service(s)
354	[[ ! -s "${POST}" ]] && lp_config_service disable
355
356	# sync printers.conf with LP configuration
357	lp_config_sync_pconf "${PRE}" "${POST}"
358
359	/bin/rm -f ${PRE} ${POST}
360fi
361
362# Do any printers.conf configuration that is required
363if [[ -n "${delete}" ]] ; then
364	if [[ "${delete}" = "all" ]] ; then
365		[[ $exit_code -eq 0 ]] && delete_all
366   	elif [[ -z "${local}" ]] ; then
367   		${LPSET} -n system -x ${delete}
368   		exit_code=$?
369   	fi
370else
371	if [[ -z "${local}" ]] ; then
372		# if we need a uri, find the "best" one.
373		if [[ -z "${uri}" ]] ; then
374			uri="ipp://${server}/printers/${rem_printer}"
375			${LPSTAT} -p ${uri} >/dev/null 2>&1
376			if [[ $? -ne 0 ]] ; then
377				uri="lpd://${server}/printers/${rem_printer}#Solaris"
378			fi
379		fi
380		# set the bsdaddr
381		bsdaddr="${server},${rem_printer},Solaris"
382
383		if [[ -n "${printer}" && -n "${server}" ]] ; then
384			${LPSET} -n system \
385				-a "printer-uri-supported=${uri}" \
386				-a "bsdaddr=${bsdaddr}" ${printer}
387			exit_code=$?
388		fi
389
390	fi
391
392	if [[ -n "${printer}" && -n "${description}" ]] ; then
393		${LPSET} -n system \
394			-a "description=${description}" ${printer}
395		exit_code=$?
396	fi
397fi
398
399# if the "default" doesn't resolve a "bsdaddr", the printer is gone, remove it
400${LPGET} -n system -k bsdaddr _default >/dev/null 2>&1 ||
401	${LPSET} -n system -x _default >/dev/null 2>&1
402
403exit $exit_code
404