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#
233478Sjacobs# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate# Use is subject to license terms.
250Sstevel@tonic-gate#
260Sstevel@tonic-gate# ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate#
283478Sjacobsset -o noclobber
293478Sjacobs
300Sstevel@tonic-gatePATH=/bin:/usr/bin:/usr/sbin export PATH
310Sstevel@tonic-gate
320Sstevel@tonic-gateTEXTDOMAIN="SUNW_OST_OSCMD"
330Sstevel@tonic-gateexport TEXTDOMAIN
340Sstevel@tonic-gate
35*3999SjacobsPFEXEC=/usr/bin/pfexec
360Sstevel@tonic-gateLPSET=/usr/bin/lpset
370Sstevel@tonic-gateLPGET=/usr/bin/lpget
383125SjacobsLPSTAT=/usr/bin/lpstat
392792SjacobsLPADMIN=/usr/lib/lp/local/lpadmin
40*3999SjacobsLPFILTER=/usr/sbin/lpfilter
413478SjacobsCOMM=/usr/bin/comm
423781SceasthaPPDMGR=/usr/sbin/ppdmgr
430Sstevel@tonic-gate
442792SjacobsHOST=$(/bin/uname -n)
450Sstevel@tonic-gateexit_code=0
460Sstevel@tonic-gate
470Sstevel@tonic-gateusage() {
480Sstevel@tonic-gate	gettext "Usage:\n" 1>&2
490Sstevel@tonic-gate	gettext "	lpadmin -p (printer) (options)\n" 1>&2
500Sstevel@tonic-gate	gettext "	lpadmin -x (dest)\n" 1>&2
510Sstevel@tonic-gate	gettext "	lpadmin -d (dest)\n" 1>&2
520Sstevel@tonic-gate	gettext "	lpadmin -S print-wheel -A alert-type [ -W minutes ]\n" 1>&2
530Sstevel@tonic-gate	gettext "		[ -Q requests ]\n" 1>&2
540Sstevel@tonic-gate	gettext "	lpadmin -M -f form-name [ -a [ -o filebreak ]\n" 1>&2
550Sstevel@tonic-gate	gettext "		[ -t tray-number ]]\n" 1>&2
560Sstevel@tonic-gate	exit 1
570Sstevel@tonic-gate}
580Sstevel@tonic-gate
592792Sjacobs# create a filter table for LP service
602792Sjacobslp_config_filters() {
612792Sjacobs	if [[ ! -f /etc/lp/filter.table ]] ; then
622792Sjacobs		cd /etc/lp/fd ; for filter in *.fd ; do
63*3999Sjacobs			${PFEXEC} ${LPFILTER} \
642792Sjacobs				-f $(/usr/bin/basename $filter .fd) \
652792Sjacobs				-F $filter
662792Sjacobs		done
672792Sjacobs	fi
682792Sjacobs}
690Sstevel@tonic-gate
702792Sjacobs# enable/disable LP related service(s)
712792Sjacobslp_config_service() {	# (enable | disable)
722792Sjacobs	svcadm ${1} -s svc:/application/print/server:default
732792Sjacobs	# svcadm ${1} -s svc:/application/print/rfc1179:default
742792Sjacobs	# svcadm ${1} -s svc:/application/print/ipp-listener:default
752792Sjacobs}
762792Sjacobs
772792Sjacobs# synchronize printers.conf with LP configuration changes
782792Sjacobslp_config_sync_pconf() {	# (pre) (post)
793478Sjacobs	ADDED=$(${COMM} -13 ${1} ${2})
803478Sjacobs	REMOVED=$(${COMM} -23 ${1} ${2})
810Sstevel@tonic-gate
823478Sjacobs	lp_server=${server:-${HOST}}
833478Sjacobs	for DEST in ${ADDED} ; do
843478Sjacobs		lp_uri="ipp://${lp_server}/printers/${DEST}"
853478Sjacobs		lp_bsdaddr="${lp_server},${DEST},Solaris"
863478Sjacobs		${LPSET} -n system \
873478Sjacobs			-a "printer-uri-supported=${lp_uri}" \
883478Sjacobs			-a "bsdaddr=${lp_bsdaddr}" \
893478Sjacobs		 	${DEST} 2>/dev/null
903478Sjacobs	done
910Sstevel@tonic-gate
923478Sjacobs	for DEST in ${REMOVED} ; do
933478Sjacobs		${LPSET} -n system -x ${DEST} 2>/dev/null
943478Sjacobs	done
952792Sjacobs}
962792Sjacobs
972792Sjacobs# Delete all destinations in printers.conf
982792Sjacobsdelete_all() {
992792Sjacobs	for DEST in $(lpget -n system list | egrep -e '.+:$' | sed -e 's/://')
1002792Sjacobs	do
1012792Sjacobs		${LPSET} -n system -x ${DEST}
1022792Sjacobs		status=$?
1032792Sjacobs	done
1042792Sjacobs}
1050Sstevel@tonic-gate
1063781Sceastha# Call the ppdmgr utility to add a new PPD file to the system.
1073781Sceastha#
1083781Sceastha# $1  - path to PPD file
1093781Sceastha# $2  - label name (optional)
1103781Sceasthaadd_new_ppd_file() {
1113781Sceastha	# Add new ppd file and echo full path it was actually saved to
112*3999Sjacobs	ppdmgrcmd="${PFEXEC} ${PPDMGR} -a ${1} -w"
1133781Sceastha
1143781Sceastha	ppderrfile=/tmp/lpadminerror.$$
1153781Sceastha	ppd_file=$(${ppdmgrcmd} 2>${ppderrfile})
1163781Sceastha	ppdmgrrc=$?
1173781Sceastha	if [[ -s "${ppderrfile}" ]] ; then
1183781Sceastha		print -n "lpadmin: " 1>&2
1193781Sceastha		cat ${ppderrfile} 1>&2
1203781Sceastha		rm -f ${ppderrfile} >/dev/null 2>&1
1213781Sceastha		if [[ ${ppdmgrrc} -ne 0 ]] ; then
1223781Sceastha			exit 1
1233781Sceastha		fi
1243781Sceastha	fi
1253781Sceastha	rm -f ${ppderrfile} >/dev/null 2>&1
1263781Sceastha}
1273781Sceastha
1280Sstevel@tonic-gate#
1292792Sjacobs# Execution begins here
1300Sstevel@tonic-gate#
1310Sstevel@tonic-gate
1322792Sjacobs# be sure that we can run lpset and lpget
1332792Sjacobsif [[ ! -x ${LPSET} || ! -x ${LPGET} ]] ; then
1342792Sjacobs	gettext "lpadmin: System error; cannot set default printer\n" 1>&2
1352792Sjacobs	exit 2
1360Sstevel@tonic-gatefi
1370Sstevel@tonic-gate
1382792Sjacobsif [[ $# -lt 1 ]] ; then
1390Sstevel@tonic-gate	usage
1400Sstevel@tonic-gate	exit 1
1410Sstevel@tonic-gatefi
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate# Deal with the -d option independently since getopts does not handle
1440Sstevel@tonic-gate# options that may or may not have arguments
1450Sstevel@tonic-gate#
1462792Sjacobsif [[ ${1} = "-d" ]] ; then
1472792Sjacobs	if [[ $# -eq 1 ]] ; then	# remove the "default"
1482792Sjacobs		${LPGET} -n system _default >/dev/null 2>&1
1492792Sjacobs		exit_code=$?
1500Sstevel@tonic-gate
1512792Sjacobs		if [[ ${exit_code} -eq 0 ]] ; then
1522792Sjacobs			${LPSET} -n system -x _default
1532792Sjacobs			exit_code=$?
1542792Sjacobs		else	# no default, nothing to do
1552792Sjacobs			exit_code=0
1562792Sjacobs		fi
1572792Sjacobs	elif [[ $# -eq 2 ]] ; then	# add/change the "default"
1582792Sjacobs		${LPGET} -k bsdaddr ${2} >/dev/null 2>&1
1592792Sjacobs		exit_code=$?
1602792Sjacobs
1612792Sjacobs		if [[ $exit_code -eq 0 ]] ; then
1622792Sjacobs			${LPSET} -n system -a "use=${2}" _default
1632792Sjacobs			exit_code=$?
1642792Sjacobs		else	# can't set default to an unconfigured printer
1652792Sjacobs			gettext "${2}: undefined printer\n" 1>&1
1662792Sjacobs		fi
1672792Sjacobs	else				# invalid usage
1680Sstevel@tonic-gate		usage
1690Sstevel@tonic-gate		exit 1
1700Sstevel@tonic-gate	fi
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate	exit ${exit_code}
1730Sstevel@tonic-gatefi
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate#		Strip off legal options
1760Sstevel@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
1770Sstevel@tonic-gatedo
1780Sstevel@tonic-gate	case $arg in
1790Sstevel@tonic-gate	D)
1800Sstevel@tonic-gate		description="${OPTARG}"
1810Sstevel@tonic-gate	;;
1823781Sceastha	n)
1833781Sceastha		ppd_file="${OPTARG}"
1843781Sceastha	;;
1850Sstevel@tonic-gate	p)
1862792Sjacobs		if [[ -n "${delete}" ]] ; then
1870Sstevel@tonic-gate			usage
1880Sstevel@tonic-gate		fi
1890Sstevel@tonic-gate		printer=${OPTARG}
1900Sstevel@tonic-gate	;;
1910Sstevel@tonic-gate	s)
1920Sstevel@tonic-gate		server=${OPTARG}
1930Sstevel@tonic-gate	;;
1940Sstevel@tonic-gate	v|U)
1950Sstevel@tonic-gate		device=${OPTARG}
1962792Sjacobs		if [[ ! -n "${server}" ]] ; then
197831Swendyp			server=${HOST}
198831Swendyp		fi
1993125Sjacobs		local="true"
2000Sstevel@tonic-gate	;;
2010Sstevel@tonic-gate	x)
2022792Sjacobs		if [[ -n "${printer}" || -n "${server}" || \
2032792Sjacobs		     -n "${device}" || -n "${description}" ]] ; then
2040Sstevel@tonic-gate			usage
2050Sstevel@tonic-gate		fi
2060Sstevel@tonic-gate		delete=${OPTARG}
2070Sstevel@tonic-gate		printer=${OPTARG}
2082792Sjacobs		if [[ ${printer} = "all" ]] ; then
2090Sstevel@tonic-gate			local="true"
2100Sstevel@tonic-gate		fi
2110Sstevel@tonic-gate	;;
2120Sstevel@tonic-gate	S|M|A)
2130Sstevel@tonic-gate		local="true"
2140Sstevel@tonic-gate	;;
2150Sstevel@tonic-gate	c)
2160Sstevel@tonic-gate		class=${OPTARG}
2170Sstevel@tonic-gate		local="true"
2182792Sjacobs		if [[ ! -f ${LPGET} ]] ; then
2190Sstevel@tonic-gate			gettext "lpadmin: System error; cannot set class\n " 1>&2
2200Sstevel@tonic-gate			exit 2
2210Sstevel@tonic-gate		fi
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate		${LPGET} "${class}" > /dev/null 2>&1
2240Sstevel@tonic-gate		lpget_class=$?
2252792Sjacobs		if [[ ${lpget_class} -eq 0 && ! -r /etc/lp/classes/"${class}" ]] ; then
2260Sstevel@tonic-gate			gettext "lpadmin: ERROR: Can't create class ${class}.\n" 1>&2
2270Sstevel@tonic-gate			gettext "           TO FIX: This is an existing printer name;\n" 1>&2
2280Sstevel@tonic-gate			gettext "                   choose another name.\n" 1>&2
2290Sstevel@tonic-gate			exit 1
2300Sstevel@tonic-gate		fi
2310Sstevel@tonic-gate	;;
2320Sstevel@tonic-gate	r)
2330Sstevel@tonic-gate		local="true"
2340Sstevel@tonic-gate	;;
2350Sstevel@tonic-gate	esac
2360Sstevel@tonic-gatedone
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate#
2390Sstevel@tonic-gate# We don't have anything to do; let user know and bail
2400Sstevel@tonic-gate#
2412792Sjacobsif [[ ! -n "${printer}" && ! -n "${delete}" && ! -n "${local}" ]] ; then
2420Sstevel@tonic-gate	gettext "lpadmin: ERROR: Nothing to do.\n" 1>&2
2430Sstevel@tonic-gate	gettext "        TO FIX: You must give one of these options:\n" 1>&2
2440Sstevel@tonic-gate	gettext "		      -p, -d, -x -S\n" 1>&2
2450Sstevel@tonic-gate	exit 1
2460Sstevel@tonic-gatefi
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate#
2490Sstevel@tonic-gate#       Printer does not exist
2500Sstevel@tonic-gate#       To be consistent with 2.5, assume adding local printer
2510Sstevel@tonic-gate#
2522792Sjacobsif [[ ! -n "${device}" && ! -n "${server}" && ! -n "${delete}" && \
2532792Sjacobs	  ! -n "${local}" ]] ; then
2540Sstevel@tonic-gate	${LPGET} "${printer}" > /dev/null 2>&1
2550Sstevel@tonic-gate	lpget_stat=$?
2562792Sjacobs	if [[ ${lpget_stat} -ne 0 ]] ; then
2570Sstevel@tonic-gate		gettext "lpadmin: ERROR: Missing -U or -v option.\n" 1>&2
2580Sstevel@tonic-gate		gettext "           TO FIX: Local printers must have\n" 1>&2
2590Sstevel@tonic-gate		gettext "                   a port defined (-v option) or\n" 1>&2
2600Sstevel@tonic-gate		gettext "                   have dial-out instructions (-U option).\n" 1>&2
2610Sstevel@tonic-gate		exit 1
2620Sstevel@tonic-gate	fi
2630Sstevel@tonic-gatefi
2640Sstevel@tonic-gate
2652264Sjacobs#	process the "server" value
2662264Sjacobs#	It can be a hostname, UUCP form (server!queue), RCMD form(queue@server),
2672264Sjacobs#	or in URI form ({scheme}://{endpoint})
2682264Sjacobs#
2692264Sjacobscase "${server}" in
2702264Sjacobs	*://*)	# URI form
2712264Sjacobs		uri=${server}
2722792Sjacobs		rem_printer=$(expr "${server}" : ".*://.*/\([^/]*\)")
2732792Sjacobs		server=$(expr "${server}" : ".*://\([^/]*\)/.*")
2742264Sjacobs		;;
2752264Sjacobs	*@*)	# RCMD form
2762792Sjacobs		rem_printer=$(expr "${server}" : "\(.*\)@.*")
2772792Sjacobs		server=$(expr "${server}" : ".*@\(.*\)")
2782264Sjacobs		;;
2792264Sjacobs	*!*)	# UUCP form
2802792Sjacobs		rem_printer=$(expr "${server}" : ".*!\(.*\)")
2812792Sjacobs		server=$(expr "${server}" : "\(.*\)!.*")
2822264Sjacobs		;;
2832264Sjacobs	*)	# hostname
2842264Sjacobs		rem_printer=${printer}
2852264Sjacobs		;;
2862264Sjacobsesac
2872792Sjacobs
2883125Sjacobs# if there is a "device" or LP configuration, it's local
2893125Sjacobsif [[ -n "${device}" || -f /etc/lp/printers/${printer}/configuration || \
2903125Sjacobs      -f /etc/lp/classes/${printer} ]] ; then
2913125Sjacobs	local="true"
2923125Sjacobsfi
2932792Sjacobs
2942792Sjacobs# Do the LP configuration for a local printer served by lpsched
2953125Sjacobsif [[ -x ${LPADMIN} && -n "${local}" ]] ; then
2962792Sjacobs	# enumerate LP configured printers before modification
2973478Sjacobs	PRE=/tmp/lpadmin-pre.$$
2983478Sjacobs	(/bin/ls /etc/lp/printers 2>/dev/null ; /bin/ls /etc/lp/classes \
2993478Sjacobs		2>/dev/null) >${PRE}
3002792Sjacobs
3012792Sjacobs	# if there are no printers configured, enable LP service(s)
3023781Sceastha	[[ ! -s "${PRE}" ]] && lp_config_service enable
3032792Sjacobs
3042792Sjacobs	# add filters to LP service
3052792Sjacobs	lp_config_filters
3062792Sjacobs
3073781Sceastha	# add new ppd file to PPD file repositories
3083781Sceastha	if [[ -n "${ppd_file}" && -x ${PPDMGR} ]] ; then
3093781Sceastha		add_new_ppd_file "${ppd_file}"
3103781Sceastha	fi
3113781Sceastha
3122792Sjacobs	# modify LP destination(s)
313*3999Sjacobs	CMD="${PFEXEC} ${LPADMIN}"
3142792Sjacobs	while [[ -n "$*" ]] ; do	# to deal with multi-word arguments
3152792Sjacobs		CMD="$CMD \"$1\""
3163781Sceastha		# replace the ppd_file originally specified with the -n option
3173781Sceastha		# with the one returned from call to ppdmgr
3183781Sceastha		if [[ "${1}" = "-n" ]] ; then
3193781Sceastha			CMD="$CMD \"${ppd_file}\""
3203781Sceastha			shift
3213781Sceastha		fi
3222792Sjacobs		shift
3232792Sjacobs	done
3242792Sjacobs	case "$CMD" in
3252792Sjacobs		*\"-D\")
3262792Sjacobs			CMD="$CMD \"\""
3272792Sjacobs		;;
3282792Sjacobs	esac
3290Sstevel@tonic-gate
3302792Sjacobs	# execute the LP lpadmin command
3312792Sjacobs	eval $CMD
3322792Sjacobs	exit_code=$?
3332792Sjacobs
3342792Sjacobs	# enumerate LP configured printers after modification
3353478Sjacobs	POST=/tmp/lpadmin-post.$$
3363478Sjacobs	(/bin/ls /etc/lp/printers 2>/dev/null ; /bin/ls /etc/lp/classes \
3373478Sjacobs		2>/dev/null) >${POST}
3382792Sjacobs
3392792Sjacobs	# if there are no destinations, disable the service(s)
3403478Sjacobs	[[ ! -s "${POST}" ]] && lp_config_service disable
3412792Sjacobs
3422792Sjacobs	# sync printers.conf with LP configuration
3432792Sjacobs	lp_config_sync_pconf "${PRE}" "${POST}"
3443478Sjacobs
3453478Sjacobs	/bin/rm -f ${PRE} ${POST}
3462792Sjacobsfi
3472792Sjacobs
3482792Sjacobs# Do any printers.conf configuration that is required
3492792Sjacobsif [[ -n "${delete}" ]] ; then
3502792Sjacobs	if [[ "${delete}" = "all" ]] ; then
3512792Sjacobs		[[ $exit_code -eq 0 ]] && delete_all
3523125Sjacobs   	elif [[ -z "${local}" ]] ; then
3532792Sjacobs   		${LPSET} -n system -x ${delete}
3542792Sjacobs   		exit_code=$?
3552792Sjacobs   	fi
3563125Sjacobselif [[ -z "${local}" ]] ; then
3573710Sjacobs	# if we need a uri, find the "best" one.
3583710Sjacobs	if [[ -z "${uri}" ]] ; then
3593710Sjacobs		uri="ipp://${server}/printers/${rem_printer}"
3603710Sjacobs		${LPSTAT} -p ${uri} >/dev/null 2>&1
3613710Sjacobs		if [[ $? -ne 0 ]] ; then
3623710Sjacobs			uri="lpd://${server}/printers/${rem_printer}#Solaris"
3633710Sjacobs		fi
3643710Sjacobs	fi
3653710Sjacobs	# set the bsdaddr
3663710Sjacobs	bsdaddr="${server},${rem_printer},Solaris"
3673710Sjacobs
3682792Sjacobs	if [[ -n "${printer}" ]] ; then
3692792Sjacobs		${LPSET} -n system \
3702792Sjacobs			-a "printer-uri-supported=${uri}" \
3712792Sjacobs			-a "bsdaddr=${bsdaddr}" ${printer}
3720Sstevel@tonic-gate		exit_code=$?
3730Sstevel@tonic-gate	fi
3740Sstevel@tonic-gate
3752792Sjacobs	if [[ -n "${printer}" && -n "${description}" ]] ; then
3762792Sjacobs		${LPSET} -n system \
3772792Sjacobs			-a "description=${description}" ${printer}
3782792Sjacobs		exit_code=$?
3790Sstevel@tonic-gate	fi
3800Sstevel@tonic-gatefi
3810Sstevel@tonic-gate
3822792Sjacobs# if the "default" doesn't resolve a "bsdaddr", the printer is gone, remove it
3832792Sjacobs${LPGET} -n system -k bsdaddr _default >/dev/null 2>&1 ||
3842792Sjacobs	${LPSET} -n system -x _default >/dev/null 2>&1
3850Sstevel@tonic-gate
3860Sstevel@tonic-gateexit $exit_code
387