12792Sjacobs#!/bin/ksh
20Sstevel@tonic-gate#
30Sstevel@tonic-gate# CDDL HEADER START
40Sstevel@tonic-gate#
50Sstevel@tonic-gate# The contents of this file are subject to the terms of the
62264Sjacobs# Common Development and Distribution License (the "License").
72264Sjacobs# You may not use this file except in compliance with the License.
80Sstevel@tonic-gate#
90Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate# See the License for the specific language governing permissions
120Sstevel@tonic-gate# and limitations under the License.
130Sstevel@tonic-gate#
140Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate#
200Sstevel@tonic-gate# CDDL HEADER END
210Sstevel@tonic-gate#
220Sstevel@tonic-gate#
23*7661SJonathan.Ca@Sun.COM# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate# Use is subject to license terms.
250Sstevel@tonic-gate#
260Sstevel@tonic-gate#
273478Sjacobsset -o noclobber
283478Sjacobs
290Sstevel@tonic-gatePATH=/bin:/usr/bin:/usr/sbin export PATH
300Sstevel@tonic-gate
310Sstevel@tonic-gateTEXTDOMAIN="SUNW_OST_OSCMD"
320Sstevel@tonic-gateexport TEXTDOMAIN
330Sstevel@tonic-gate
343999SjacobsPFEXEC=/usr/bin/pfexec
350Sstevel@tonic-gateLPSET=/usr/bin/lpset
360Sstevel@tonic-gateLPGET=/usr/bin/lpget
373125SjacobsLPSTAT=/usr/bin/lpstat
382792SjacobsLPADMIN=/usr/lib/lp/local/lpadmin
393999SjacobsLPFILTER=/usr/sbin/lpfilter
403478SjacobsCOMM=/usr/bin/comm
413781SceasthaPPDMGR=/usr/sbin/ppdmgr
420Sstevel@tonic-gate
432792SjacobsHOST=$(/bin/uname -n)
440Sstevel@tonic-gateexit_code=0
450Sstevel@tonic-gate
460Sstevel@tonic-gateusage() {
470Sstevel@tonic-gate	gettext "Usage:\n" 1>&2
480Sstevel@tonic-gate	gettext "	lpadmin -p (printer) (options)\n" 1>&2
490Sstevel@tonic-gate	gettext "	lpadmin -x (dest)\n" 1>&2
500Sstevel@tonic-gate	gettext "	lpadmin -d (dest)\n" 1>&2
510Sstevel@tonic-gate	gettext "	lpadmin -S print-wheel -A alert-type [ -W minutes ]\n" 1>&2
520Sstevel@tonic-gate	gettext "		[ -Q requests ]\n" 1>&2
530Sstevel@tonic-gate	gettext "	lpadmin -M -f form-name [ -a [ -o filebreak ]\n" 1>&2
540Sstevel@tonic-gate	gettext "		[ -t tray-number ]]\n" 1>&2
550Sstevel@tonic-gate	exit 1
560Sstevel@tonic-gate}
570Sstevel@tonic-gate
582792Sjacobs# create a filter table for LP service
592792Sjacobslp_config_filters() {
602792Sjacobs	if [[ ! -f /etc/lp/filter.table ]] ; then
612792Sjacobs		cd /etc/lp/fd ; for filter in *.fd ; do
623999Sjacobs			${PFEXEC} ${LPFILTER} \
632792Sjacobs				-f $(/usr/bin/basename $filter .fd) \
642792Sjacobs				-F $filter
652792Sjacobs		done
662792Sjacobs	fi
672792Sjacobs}
680Sstevel@tonic-gate
692792Sjacobs# enable/disable LP related service(s)
702792Sjacobslp_config_service() {	# (enable | disable)
712792Sjacobs	svcadm ${1} -s svc:/application/print/server:default
722792Sjacobs	# svcadm ${1} -s svc:/application/print/rfc1179:default
732792Sjacobs	# svcadm ${1} -s svc:/application/print/ipp-listener:default
742792Sjacobs}
752792Sjacobs
762792Sjacobs# synchronize printers.conf with LP configuration changes
772792Sjacobslp_config_sync_pconf() {	# (pre) (post)
783478Sjacobs	ADDED=$(${COMM} -13 ${1} ${2})
793478Sjacobs	REMOVED=$(${COMM} -23 ${1} ${2})
800Sstevel@tonic-gate
813478Sjacobs	lp_server=${server:-${HOST}}
823478Sjacobs	for DEST in ${ADDED} ; do
833478Sjacobs		lp_uri="ipp://${lp_server}/printers/${DEST}"
843478Sjacobs		lp_bsdaddr="${lp_server},${DEST},Solaris"
853478Sjacobs		${LPSET} -n system \
863478Sjacobs			-a "printer-uri-supported=${lp_uri}" \
873478Sjacobs			-a "bsdaddr=${lp_bsdaddr}" \
883478Sjacobs		 	${DEST} 2>/dev/null
893478Sjacobs	done
900Sstevel@tonic-gate
913478Sjacobs	for DEST in ${REMOVED} ; do
923478Sjacobs		${LPSET} -n system -x ${DEST} 2>/dev/null
933478Sjacobs	done
942792Sjacobs}
952792Sjacobs
962792Sjacobs# Delete all destinations in printers.conf
972792Sjacobsdelete_all() {
982792Sjacobs	for DEST in $(lpget -n system list | egrep -e '.+:$' | sed -e 's/://')
992792Sjacobs	do
1002792Sjacobs		${LPSET} -n system -x ${DEST}
1012792Sjacobs		status=$?
1022792Sjacobs	done
1032792Sjacobs}
1040Sstevel@tonic-gate
1053781Sceastha# Call the ppdmgr utility to add a new PPD file to the system.
1063781Sceastha#
1073781Sceastha# $1  - path to PPD file
1083781Sceastha# $2  - label name (optional)
1093781Sceasthaadd_new_ppd_file() {
1103781Sceastha	# Add new ppd file and echo full path it was actually saved to
1113999Sjacobs	ppdmgrcmd="${PFEXEC} ${PPDMGR} -a ${1} -w"
1123781Sceastha
1133781Sceastha	ppderrfile=/tmp/lpadminerror.$$
1143781Sceastha	ppd_file=$(${ppdmgrcmd} 2>${ppderrfile})
1153781Sceastha	ppdmgrrc=$?
1163781Sceastha	if [[ -s "${ppderrfile}" ]] ; then
1173781Sceastha		print -n "lpadmin: " 1>&2
1183781Sceastha		cat ${ppderrfile} 1>&2
1193781Sceastha		rm -f ${ppderrfile} >/dev/null 2>&1
1203781Sceastha		if [[ ${ppdmgrrc} -ne 0 ]] ; then
1213781Sceastha			exit 1
1223781Sceastha		fi
1233781Sceastha	fi
1243781Sceastha	rm -f ${ppderrfile} >/dev/null 2>&1
1253781Sceastha}
1263781Sceastha
1270Sstevel@tonic-gate#
1282792Sjacobs# Execution begins here
1290Sstevel@tonic-gate#
1300Sstevel@tonic-gate
1312792Sjacobs# be sure that we can run lpset and lpget
1322792Sjacobsif [[ ! -x ${LPSET} || ! -x ${LPGET} ]] ; then
1332792Sjacobs	gettext "lpadmin: System error; cannot set default printer\n" 1>&2
1342792Sjacobs	exit 2
1350Sstevel@tonic-gatefi
1360Sstevel@tonic-gate
1372792Sjacobsif [[ $# -lt 1 ]] ; then
1380Sstevel@tonic-gate	usage
1390Sstevel@tonic-gate	exit 1
1400Sstevel@tonic-gatefi
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate# Deal with the -d option independently since getopts does not handle
1430Sstevel@tonic-gate# options that may or may not have arguments
1440Sstevel@tonic-gate#
1452792Sjacobsif [[ ${1} = "-d" ]] ; then
1462792Sjacobs	if [[ $# -eq 1 ]] ; then	# remove the "default"
1472792Sjacobs		${LPGET} -n system _default >/dev/null 2>&1
1482792Sjacobs		exit_code=$?
1490Sstevel@tonic-gate
1502792Sjacobs		if [[ ${exit_code} -eq 0 ]] ; then
1512792Sjacobs			${LPSET} -n system -x _default
1522792Sjacobs			exit_code=$?
1532792Sjacobs		else	# no default, nothing to do
1542792Sjacobs			exit_code=0
1552792Sjacobs		fi
1562792Sjacobs	elif [[ $# -eq 2 ]] ; then	# add/change the "default"
1572792Sjacobs		${LPGET} -k bsdaddr ${2} >/dev/null 2>&1
1582792Sjacobs		exit_code=$?
1592792Sjacobs
1602792Sjacobs		if [[ $exit_code -eq 0 ]] ; then
1612792Sjacobs			${LPSET} -n system -a "use=${2}" _default
1622792Sjacobs			exit_code=$?
1632792Sjacobs		else	# can't set default to an unconfigured printer
1642792Sjacobs			gettext "${2}: undefined printer\n" 1>&1
1652792Sjacobs		fi
1662792Sjacobs	else				# invalid usage
1670Sstevel@tonic-gate		usage
1680Sstevel@tonic-gate		exit 1
1690Sstevel@tonic-gate	fi
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate	exit ${exit_code}
1720Sstevel@tonic-gatefi
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate#		Strip off legal options
1750Sstevel@tonic-gatewhile 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
1760Sstevel@tonic-gatedo
1770Sstevel@tonic-gate	case $arg in
1780Sstevel@tonic-gate	D)
1790Sstevel@tonic-gate		description="${OPTARG}"
1800Sstevel@tonic-gate	;;
1813781Sceastha	n)
1823781Sceastha		ppd_file="${OPTARG}"
1833781Sceastha	;;
1840Sstevel@tonic-gate	p)
1852792Sjacobs		if [[ -n "${delete}" ]] ; then
1860Sstevel@tonic-gate			usage
1870Sstevel@tonic-gate		fi
1880Sstevel@tonic-gate		printer=${OPTARG}
1890Sstevel@tonic-gate	;;
1900Sstevel@tonic-gate	s)
1910Sstevel@tonic-gate		server=${OPTARG}
1920Sstevel@tonic-gate	;;
1930Sstevel@tonic-gate	v|U)
1940Sstevel@tonic-gate		device=${OPTARG}
1952792Sjacobs		if [[ ! -n "${server}" ]] ; then
196831Swendyp			server=${HOST}
197831Swendyp		fi
1983125Sjacobs		local="true"
1990Sstevel@tonic-gate	;;
2000Sstevel@tonic-gate	x)
2012792Sjacobs		if [[ -n "${printer}" || -n "${server}" || \
2022792Sjacobs		     -n "${device}" || -n "${description}" ]] ; then
2030Sstevel@tonic-gate			usage
2040Sstevel@tonic-gate		fi
2050Sstevel@tonic-gate		delete=${OPTARG}
2060Sstevel@tonic-gate		printer=${OPTARG}
2072792Sjacobs		if [[ ${printer} = "all" ]] ; then
2080Sstevel@tonic-gate			local="true"
2090Sstevel@tonic-gate		fi
2100Sstevel@tonic-gate	;;
2110Sstevel@tonic-gate	S|M|A)
2120Sstevel@tonic-gate		local="true"
2130Sstevel@tonic-gate	;;
2140Sstevel@tonic-gate	c)
2150Sstevel@tonic-gate		class=${OPTARG}
2160Sstevel@tonic-gate		local="true"
2172792Sjacobs		if [[ ! -f ${LPGET} ]] ; then
2180Sstevel@tonic-gate			gettext "lpadmin: System error; cannot set class\n " 1>&2
2190Sstevel@tonic-gate			exit 2
2200Sstevel@tonic-gate		fi
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate		${LPGET} "${class}" > /dev/null 2>&1
2230Sstevel@tonic-gate		lpget_class=$?
2242792Sjacobs		if [[ ${lpget_class} -eq 0 && ! -r /etc/lp/classes/"${class}" ]] ; then
2250Sstevel@tonic-gate			gettext "lpadmin: ERROR: Can't create class ${class}.\n" 1>&2
2260Sstevel@tonic-gate			gettext "           TO FIX: This is an existing printer name;\n" 1>&2
2270Sstevel@tonic-gate			gettext "                   choose another name.\n" 1>&2
2280Sstevel@tonic-gate			exit 1
2290Sstevel@tonic-gate		fi
2300Sstevel@tonic-gate	;;
2310Sstevel@tonic-gate	r)
2320Sstevel@tonic-gate		local="true"
2330Sstevel@tonic-gate	;;
2340Sstevel@tonic-gate	esac
2350Sstevel@tonic-gatedone
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate#
2380Sstevel@tonic-gate# We don't have anything to do; let user know and bail
2390Sstevel@tonic-gate#
2402792Sjacobsif [[ ! -n "${printer}" && ! -n "${delete}" && ! -n "${local}" ]] ; then
2410Sstevel@tonic-gate	gettext "lpadmin: ERROR: Nothing to do.\n" 1>&2
2420Sstevel@tonic-gate	gettext "        TO FIX: You must give one of these options:\n" 1>&2
2430Sstevel@tonic-gate	gettext "		      -p, -d, -x -S\n" 1>&2
2440Sstevel@tonic-gate	exit 1
2450Sstevel@tonic-gatefi
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate#
2480Sstevel@tonic-gate#       Printer does not exist
2490Sstevel@tonic-gate#       To be consistent with 2.5, assume adding local printer
2500Sstevel@tonic-gate#
2512792Sjacobsif [[ ! -n "${device}" && ! -n "${server}" && ! -n "${delete}" && \
2522792Sjacobs	  ! -n "${local}" ]] ; then
2530Sstevel@tonic-gate	${LPGET} "${printer}" > /dev/null 2>&1
2540Sstevel@tonic-gate	lpget_stat=$?
2552792Sjacobs	if [[ ${lpget_stat} -ne 0 ]] ; then
2560Sstevel@tonic-gate		gettext "lpadmin: ERROR: Missing -U or -v option.\n" 1>&2
2570Sstevel@tonic-gate		gettext "           TO FIX: Local printers must have\n" 1>&2
2580Sstevel@tonic-gate		gettext "                   a port defined (-v option) or\n" 1>&2
2590Sstevel@tonic-gate		gettext "                   have dial-out instructions (-U option).\n" 1>&2
2600Sstevel@tonic-gate		exit 1
2610Sstevel@tonic-gate	fi
2620Sstevel@tonic-gatefi
2630Sstevel@tonic-gate
2642264Sjacobs#	process the "server" value
2652264Sjacobs#	It can be a hostname, UUCP form (server!queue), RCMD form(queue@server),
2662264Sjacobs#	or in URI form ({scheme}://{endpoint})
2672264Sjacobs#
2682264Sjacobscase "${server}" in
2692264Sjacobs	*://*)	# URI form
2702264Sjacobs		uri=${server}
2712792Sjacobs		rem_printer=$(expr "${server}" : ".*://.*/\([^/]*\)")
2722792Sjacobs		server=$(expr "${server}" : ".*://\([^/]*\)/.*")
2732264Sjacobs		;;
2742264Sjacobs	*@*)	# RCMD form
2752792Sjacobs		rem_printer=$(expr "${server}" : "\(.*\)@.*")
2762792Sjacobs		server=$(expr "${server}" : ".*@\(.*\)")
2772264Sjacobs		;;
2782264Sjacobs	*!*)	# UUCP form
2792792Sjacobs		rem_printer=$(expr "${server}" : ".*!\(.*\)")
2802792Sjacobs		server=$(expr "${server}" : "\(.*\)!.*")
2812264Sjacobs		;;
2822264Sjacobs	*)	# hostname
2832264Sjacobs		rem_printer=${printer}
2842264Sjacobs		;;
2852264Sjacobsesac
2862792Sjacobs
2873125Sjacobs# if there is a "device" or LP configuration, it's local
2883125Sjacobsif [[ -n "${device}" || -f /etc/lp/printers/${printer}/configuration || \
2893125Sjacobs      -f /etc/lp/classes/${printer} ]] ; then
2903125Sjacobs	local="true"
2913125Sjacobsfi
2922792Sjacobs
2932792Sjacobs# Do the LP configuration for a local printer served by lpsched
2943125Sjacobsif [[ -x ${LPADMIN} && -n "${local}" ]] ; then
2952792Sjacobs	# enumerate LP configured printers before modification
2963478Sjacobs	PRE=/tmp/lpadmin-pre.$$
2973478Sjacobs	(/bin/ls /etc/lp/printers 2>/dev/null ; /bin/ls /etc/lp/classes \
2983478Sjacobs		2>/dev/null) >${PRE}
2992792Sjacobs
3002792Sjacobs	# if there are no printers configured, enable LP service(s)
3013781Sceastha	[[ ! -s "${PRE}" ]] && lp_config_service enable
3022792Sjacobs
3032792Sjacobs	# add filters to LP service
3042792Sjacobs	lp_config_filters
3052792Sjacobs
3063781Sceastha	# add new ppd file to PPD file repositories
3073781Sceastha	if [[ -n "${ppd_file}" && -x ${PPDMGR} ]] ; then
3083781Sceastha		add_new_ppd_file "${ppd_file}"
3093781Sceastha	fi
3103781Sceastha
3112792Sjacobs	# modify LP destination(s)
3123999Sjacobs	CMD="${PFEXEC} ${LPADMIN}"
3132792Sjacobs	while [[ -n "$*" ]] ; do	# to deal with multi-word arguments
3142792Sjacobs		CMD="$CMD \"$1\""
3153781Sceastha		# replace the ppd_file originally specified with the -n option
3163781Sceastha		# with the one returned from call to ppdmgr
3173781Sceastha		if [[ "${1}" = "-n" ]] ; then
3183781Sceastha			CMD="$CMD \"${ppd_file}\""
3193781Sceastha			shift
3203781Sceastha		fi
3212792Sjacobs		shift
3222792Sjacobs	done
3232792Sjacobs	case "$CMD" in
3242792Sjacobs		*\"-D\")
3252792Sjacobs			CMD="$CMD \"\""
3262792Sjacobs		;;
3272792Sjacobs	esac
3280Sstevel@tonic-gate
3292792Sjacobs	# execute the LP lpadmin command
3302792Sjacobs	eval $CMD
3312792Sjacobs	exit_code=$?
3322792Sjacobs
3332792Sjacobs	# enumerate LP configured printers after modification
3343478Sjacobs	POST=/tmp/lpadmin-post.$$
3353478Sjacobs	(/bin/ls /etc/lp/printers 2>/dev/null ; /bin/ls /etc/lp/classes \
3363478Sjacobs		2>/dev/null) >${POST}
3372792Sjacobs
3382792Sjacobs	# if there are no destinations, disable the service(s)
3393478Sjacobs	[[ ! -s "${POST}" ]] && lp_config_service disable
3402792Sjacobs
3412792Sjacobs	# sync printers.conf with LP configuration
3422792Sjacobs	lp_config_sync_pconf "${PRE}" "${POST}"
3433478Sjacobs
3443478Sjacobs	/bin/rm -f ${PRE} ${POST}
3452792Sjacobsfi
3462792Sjacobs
3472792Sjacobs# Do any printers.conf configuration that is required
3482792Sjacobsif [[ -n "${delete}" ]] ; then
3492792Sjacobs	if [[ "${delete}" = "all" ]] ; then
3502792Sjacobs		[[ $exit_code -eq 0 ]] && delete_all
3513125Sjacobs   	elif [[ -z "${local}" ]] ; then
3522792Sjacobs   		${LPSET} -n system -x ${delete}
3532792Sjacobs   		exit_code=$?
3542792Sjacobs   	fi
355*7661SJonathan.Ca@Sun.COMelse
356*7661SJonathan.Ca@Sun.COM	if [[ -z "${local}" ]] ; then
357*7661SJonathan.Ca@Sun.COM		# if we need a uri, find the "best" one.
358*7661SJonathan.Ca@Sun.COM		if [[ -z "${uri}" ]] ; then
359*7661SJonathan.Ca@Sun.COM			uri="ipp://${server}/printers/${rem_printer}"
360*7661SJonathan.Ca@Sun.COM			${LPSTAT} -p ${uri} >/dev/null 2>&1
361*7661SJonathan.Ca@Sun.COM			if [[ $? -ne 0 ]] ; then
362*7661SJonathan.Ca@Sun.COM				uri="lpd://${server}/printers/${rem_printer}#Solaris"
363*7661SJonathan.Ca@Sun.COM			fi
3643710Sjacobs		fi
365*7661SJonathan.Ca@Sun.COM		# set the bsdaddr
366*7661SJonathan.Ca@Sun.COM		bsdaddr="${server},${rem_printer},Solaris"
3673710Sjacobs
368*7661SJonathan.Ca@Sun.COM		if [[ -n "${printer}" ]] ; then
369*7661SJonathan.Ca@Sun.COM			${LPSET} -n system \
370*7661SJonathan.Ca@Sun.COM				-a "printer-uri-supported=${uri}" \
371*7661SJonathan.Ca@Sun.COM				-a "bsdaddr=${bsdaddr}" ${printer}
372*7661SJonathan.Ca@Sun.COM			exit_code=$?
373*7661SJonathan.Ca@Sun.COM		fi
374*7661SJonathan.Ca@Sun.COM
3750Sstevel@tonic-gate	fi
3760Sstevel@tonic-gate
3772792Sjacobs	if [[ -n "${printer}" && -n "${description}" ]] ; then
3782792Sjacobs		${LPSET} -n system \
3792792Sjacobs			-a "description=${description}" ${printer}
3802792Sjacobs		exit_code=$?
3810Sstevel@tonic-gate	fi
3820Sstevel@tonic-gatefi
3830Sstevel@tonic-gate
3842792Sjacobs# if the "default" doesn't resolve a "bsdaddr", the printer is gone, remove it
3852792Sjacobs${LPGET} -n system -k bsdaddr _default >/dev/null 2>&1 ||
3862792Sjacobs	${LPSET} -n system -x _default >/dev/null 2>&1
3870Sstevel@tonic-gate
3880Sstevel@tonic-gateexit $exit_code
389