lpadmin revision 355b4669e025ff377602b6fc7caaf30dbc218371
4675N/A#!/bin/sh
4675N/A#
4675N/A# CDDL HEADER START
4675N/A#
4675N/A# The contents of this file are subject to the terms of the
4675N/A# Common Development and Distribution License (the "License").
4675N/A# You may not use this file except in compliance with the License.
4675N/A#
4675N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
4675N/A# or http://www.opensolaris.org/os/licensing.
4675N/A# See the License for the specific language governing permissions
4675N/A# and limitations under the License.
4675N/A#
4675N/A# When distributing Covered Code, include this CDDL HEADER in each
4675N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
4675N/A# If applicable, add the following below this CDDL HEADER, with the
4675N/A# fields enclosed by brackets "[]" replaced with your own identifying
4675N/A# information: Portions Copyright [yyyy] [name of copyright owner]
4675N/A#
4675N/A# CDDL HEADER END
5680N/A#
5680N/A#
5453N/A# Copyright 2006 Sun Microsystems, Inc. All rights reserved.
4675N/A# Use is subject to license terms.
5680N/A#
5680N/A# ident "%Z%%M% %I% %E% SMI"
4675N/A#
4675N/APATH=/bin:/usr/bin:/usr/sbin export PATH
5775N/A
4675N/ATEXTDOMAIN="SUNW_OST_OSCMD"
4675N/Aexport TEXTDOMAIN
5775N/A
4675N/ALPSET=/usr/bin/lpset
4675N/ALPGET=/usr/bin/lpget
5775N/A
4675N/AHOST=`/bin/uname -n`
5453N/ALHOST="localhost"
5453N/APID=$$
5453N/A
5453N/Acmd_name=lpadmin
5453N/Aexit_code=0
5453N/A
5453N/Ausage() {
5453N/A gettext "Usage:\n" 1>&2
5453N/A gettext " lpadmin -p (printer) (options)\n" 1>&2
5453N/A gettext " lpadmin -x (dest)\n" 1>&2
5453N/A gettext " lpadmin -d (dest)\n" 1>&2
5453N/A gettext " lpadmin -S print-wheel -A alert-type [ -W minutes ]\n" 1>&2
5453N/A gettext " [ -Q requests ]\n" 1>&2
5453N/A gettext " lpadmin -M -f form-name [ -a [ -o filebreak ]\n" 1>&2
5453N/A gettext " [ -t tray-number ]]\n" 1>&2
5680N/A exit 1
5680N/A}
5680N/A
5680N/A#
5680N/A# Delete entries in /etc/printers.conf for local printers/classes that no longer
5453N/A# exist in the /etc/lp database
5453N/A#
5453N/A
5453N/Adelete_local() {
5453N/A
5453N/A# Get printer names for each local printer
5453N/A# grep /etc/printers.conf for each bsdaddr for this server
5453N/A# get printer name from that line
5453N/A
5453N/Afor LINE in `/bin/grep bsdaddr /etc/printers.conf |
5453N/A /bin/egrep -e ${HOST}\|${LHOST}`
5453N/Ado
5453N/A PRINTER=`echo ${LINE} | /bin/sed -e 's/^:bsdaddr='$LHOST',//' \
5453N/A -e 's/^:bsdaddr='$HOST',//' \
5453N/A -e 's/[,:].*//'`
5453N/A
5453N/A# If there is not an entry for this printer in
4675N/A# /etc/lp/printers or /etc/lp/classes
4675N/A# Then delete the entry for this printer in /etc/printers.conf
5680N/A
4675N/A if [ ! -d /etc/lp/printers/${PRINTER} -a ! -f /etc/lp/classes/${PRINTER} ] ;
4675N/A then
4675N/A logger -p lpr.debug -t "lpadmin[${PID}]" \
5453N/A "Removing $PRINTER entry from /etc/printers.conf"
5453N/A ${LPSET} -x ${PRINTER}
5453N/A status=$?
5453N/A
5453N/A if [ ${status} -ne 0 ] ; then
5453N/A gettext "Warning: error removing ${PRINTER} " 1>&2
5453N/A gettext "entry from /etc/printers.conf\n" 1>&2
5453N/A logger -p lpr.debug -t "lpadmin[${PID}]" \
5453N/A "Call to lpset -x $PRINTER exits with ${status}"
5453N/A exit_code=1
5453N/A fi
5453N/A fi
5453N/Adone
5453N/A
5453N/A#
4675N/A# shutdown scheduler if there are no local printers
4675N/A#
4675N/ACONFIGS=/etc/lp/printers/*/configuration
4675N/A
4675N/Aprinters_configured=`echo $CONFIGS`
4675N/Aif [ "$printers_configured" = "$CONFIGS" ]; then
4675N/A svcprop -q svc:/application/print/server:default &&
4675N/A svcadm disable svc:/application/print/server:default
4675N/Afi
5453N/A}
5453N/A
5453N/Adelete_entries() {
5453N/Aif [ ! -f /etc/printers.conf ] ; then
5453N/A logger -p lpr.debug -t "lpadmin[${PID}]" \
5453N/A "System error: Cannot access /etc/printers.conf"
5453N/A gettext "lpadmin: System error; Cannot access /etc/printers.conf\n" 1>&2
5453N/A exit 1
5453N/Afi
5453N/A
5453N/A# remove _default
5453N/A
5453N/ADEFAULTP=`${LPGET} _default | /bin/grep use | /bin/sed -e 's/[ ]*use=//'`
5453N/A${LPGET} -k bsdaddr ${DEFAULTP} >/dev/null 2>&1
5453N/Astatus=$?
5453N/A
5453N/Aif [ ${status} -eq 0 ] ; then
5453N/A ${LPSET} -x _default
5453N/A status=$?
5453N/A if [ ${status} -ne 0 ] ; then
5453N/A gettext "Warning: error removing _default entry from /etc/printers.conf\n" 1>&2
5453N/A logger -p lpr.debug -t "lpadmin[${PID}]" \
5453N/A "Call to lpset -x _default exits with ${status}"
5453N/A exit_code=1
5453N/A fi
5453N/Afi
5453N/A
5453N/A# delete entries in /etc/printers.conf for printers/classes that have
5453N/A# been deleted
5453N/A
5453N/Adelete_local
5453N/A
5453N/A# Delete all the remote printers using bsdaddr
5453N/A
5453N/Afor LINE in `/bin/grep bsdaddr /etc/printers.conf | /bin/egrep -v -e ${HOST}\|${LHOST}`
5453N/Ado
5453N/A PRINTER=`echo $LINE | /bin/sed -e 's/^:bsdaddr=[^,]*,//' -e 's/[,:].*//'`
5680N/A ${LPSET} -x $PRINTER
5680N/A status=$?
4675N/A
4675N/A if [ ${status} -ne 0 ] ; then
gettext "Warning: error removing ${PRINTER} entry from /etc/printers.conf\n" 1>&2
logger -p lpr.debug -t "lpadmin[${PID}]" \
"Call to lpset -x $PRINTER exits with ${status}"
exit_code=1
fi
done
}
if [ $# -lt 1 ] ; then
usage
exit 1
fi
# Deal with the -d option independently since getopts does not handle
# options that may or may not have arguments
#
first=$1
second=$2
third=$3
if [ ${first} = "-d" ] ; then
# check that there are no extra arguments
if [ -n "${third}" ] ; then
usage
exit 1
fi
# be sure we have lpset and lpget
if [ ! -f ${LPSET} -o ! -f ${LPGET} ] ; then
gettext "lpadmin: System error; cannot set default printer\n" 1>&2
exit 2
fi
if [ ! -n "${second}" ] ; then
${LPGET} -n system _default >/dev/null 2>&1
exit_code=$?
if [ ${exit_code} -eq 0 ] ; then
# delete _default entry in /etc/printers.conf
${LPSET} -n system -x _default
exit_code=$?
if [ ${exit_code} -ne 0 ] ; then
gettext "lpadmin: System error while trying to delete default printer\n" 1>&2
fi
else
# there was no _default, the work is done
exit_code=0
fi
else
# add/change _default entry in /etc/printers.conf
${LPGET} -k bsdaddr ${second} >/dev/null 2>&1
exit_code=$?
if [ $exit_code -eq 0 ] ; then
${LPSET} -n system -a use=${second} _default
exit_code=$?
else
echo "${second}: " 1>&2
gettext "undefined printer\n" 1>&2
fi
fi
exit ${exit_code}
fi
# Strip off legal options
while 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
do
case $arg in
D)
description="${OPTARG}"
;;
p)
if [ -n "${delete}" ] ; then
usage
fi
printer=${OPTARG}
;;
s)
server=${OPTARG}
;;
v|U)
device=${OPTARG}
if [ ! -n "${server}" ] ; then
server=${HOST}
fi
;;
x)
if [ -n "${printer}" -o -n "${server}" -o \
-n "${device}" -o -n "${description}" ] ; then
usage
fi
delete=${OPTARG}
printer=${OPTARG}
if [ ${printer} = "all" ] ; then
local="true"
fi
;;
S|M|A)
local="true"
;;
c)
class=${OPTARG}
local="true"
if [ ! -f ${LPGET} ] ; then
gettext "lpadmin: System error; cannot set class\n " 1>&2
exit 2
fi
${LPGET} "${class}" > /dev/null 2>&1
lpget_class=$?
if [ ${lpget_class} -eq 0 -a ! -r /etc/lp/classes/"${class}" ] ; then
gettext "lpadmin: ERROR: Can't create class ${class}.\n" 1>&2
gettext " TO FIX: This is an existing printer name;\n" 1>&2
gettext " choose another name.\n" 1>&2
exit 1
fi
;;
r)
pconflocalclean="true"
local="true"
;;
esac
done
#
# We don't have anything to do; let user know and bail
#
if [ ! -n "${printer}" -a ! -n "${delete}" -a ! -n "${local}" ] ; then
gettext "lpadmin: ERROR: Nothing to do.\n" 1>&2
gettext " TO FIX: You must give one of these options:\n" 1>&2
gettext " -p, -d, -x -S\n" 1>&2
exit 1
fi
#
# Printer does not exist
# To be consistent with 2.5, assume adding local printer
#
if [ ! -n "${device}" -a ! -n "${server}" -a ! -n "${delete}" \
-a ! -n "${local}" ] ; then
${LPGET} "${printer}" > /dev/null 2>&1
lpget_stat=$?
if [ ${lpget_stat} -ne 0 ] ; then
gettext "lpadmin: ERROR: Missing -U or -v option.\n" 1>&2
gettext " TO FIX: Local printers must have\n" 1>&2
gettext " a port defined (-v option) or\n" 1>&2
gettext " have dial-out instructions (-U option).\n" 1>&2
exit 1
fi
fi
#
# Do the LP configuration for a local printer served by lpsched
#
if [ -f /usr/lib/lp/local/$cmd_name ] ; then
if [ -f /etc/lp/printers/${printer}/configuration -o -n "${device}" -o \
-f /etc/lp/classes/${printer} -o -n "${local}" ] ; then
# to deal with multi-word arguments
CMD="/usr/lib/lp/local/$cmd_name"
while [ -n "$*" ] ; do
CMD="$CMD \"$1\""
shift
done
case "$CMD" in
*\"-D\")
CMD="$CMD \"\""
;;
esac
# if adding a printer, make sure scheduler is running
if [ -n "${printer}" -a ! -n "${delete}" -a \
! -p /var/spool/lp/fifos/FIFO ]; then
svcadm enable -s svc:/application/print/server:default
fi
eval $CMD
exit_code=$?
# add filters to the print server
if [ ! -f /etc/lp/filter.table ] ; then
cd /etc/lp/fd ; for filter in *.fd ; do
/usr/sbin/lpfilter \
-f `/usr/bin/basename $filter .fd` \
-F $filter
done
fi
fi
fi
if [ $exit_code != 0 ] ; then
exit $exit_code
fi
# process the "server" value
# It can be a hostname, UUCP form (server!queue), RCMD form(queue@server),
# or in URI form ({scheme}://{endpoint})
#
case "${server}" in
*://*) # URI form
uri=${server}
rem_printer=`expr "${server}" : ".*://.*/\([^/]*\)"`
server=`expr "${server}" : ".*://\([^/]*\)/.*"`
;;
*@*) # RCMD form
rem_printer=`expr "${server}" : "\(.*\)@.*"`
server=`expr "${server}" : ".*@\(.*\)"`
;;
*!*) # UUCP form
rem_printer=`expr "${server}" : ".*!\(.*\)"`
server=`expr "${server}" : "\(.*\)!.*"`
;;
*) # hostname
rem_printer=${printer}
;;
esac
# default URI form is "lpd" form
if [ -n "${server}" ] ; then
uri=${uri:-"lpd://${server}/printers/${rem_printer}#Solaris"}
bsdaddr="${server},${rem_printer},Solaris"
fi
#
# Do the Solstice Print Configuration in /etc
#
if [ ! -f ${LPSET} -o ! -f ${LPGET} ] ; then
exit_code=2
else
if [ -n "${delete}" ] ; then
if [ "${delete}" = "all" ] ; then
delete_entries
else
${LPSET} -n system -x ${delete}
exit_code=$?
delete_local
fi
fi
if [ -n "${printer}" -a -n "${uri}" ] ; then
${LPSET} -n system -a "printer-uri-supported=${uri}" ${printer}
exit_code=$?
fi
if [ -n "${printer}" -a -n "${bsdaddr}" ] ; then
${LPSET} -n system -a "bsdaddr=${bsdaddr}" ${printer}
exit_code=$?
fi
if [ -n "${printer}" -a -n "${description}" ] ; then
${LPSET} -n system -a "description=${description}" ${printer}
exit_code=$?
fi
# Add class for local printers only
if [ -n "${class}" -a -n "${printer}" \
-a -f /etc/lp/printers/${printer}/configuration ] ; then
${LPGET} "${class}" > /dev/null 2>&1
lpget_class=$?
# If the class doesn't already exist in printers.conf, add it.
server=${server:-$HOST}
uri="lpd://${server}/printers/${class}#Solaris"
bsdaddr="${server},${class},Solaris"
if [ ${lpget_class} -ne 0 ] ; then
${LPSET} -n system \
-a "printer-uri-supported=${uri}" \
-a "bsdaddr=${bsdaddr}" \
${class}
exit_code=$?
fi
fi
fi
# /usr/lib/lp/local/lpadmin has changed the database. This cleans up cruft in the
# /etc/printers.conf file that refers to deleted objects.
if [ -n "${pconflocalclean}" ] ; then
delete_local
fi
exit $exit_code