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