#!/bin/ksh # # CDDL HEADER START # # The contents of this file are subject to the terms of the # Common Development and Distribution License (the "License"). # You may not use this file except in compliance with the License. # # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE # or http://www.opensolaris.org/os/licensing. # See the License for the specific language governing permissions # and limitations under the License. # # When distributing Covered Code, include this CDDL HEADER in each # file and include the License file at usr/src/OPENSOLARIS.LICENSE. # If applicable, add the following below this CDDL HEADER, with the # fields enclosed by brackets "[]" replaced with your own identifying # information: Portions Copyright [yyyy] [name of copyright owner] # # CDDL HEADER END # # # Copyright 2006 Sun Microsystems, Inc. All rights reserved. # Use is subject to license terms. # # ident "%Z%%M% %I% %E% SMI" # PATH=/bin:/usr/bin:/usr/sbin export PATH TEXTDOMAIN="SUNW_OST_OSCMD" export TEXTDOMAIN LPSET=/usr/bin/lpset LPGET=/usr/bin/lpget LPADMIN=/usr/lib/lp/local/lpadmin HOST=$(/bin/uname -n) exit_code=0 usage() { gettext "Usage:\n" 1>&2 gettext " lpadmin -p (printer) (options)\n" 1>&2 gettext " lpadmin -x (dest)\n" 1>&2 gettext " lpadmin -d (dest)\n" 1>&2 gettext " lpadmin -S print-wheel -A alert-type [ -W minutes ]\n" 1>&2 gettext " [ -Q requests ]\n" 1>&2 gettext " lpadmin -M -f form-name [ -a [ -o filebreak ]\n" 1>&2 gettext " [ -t tray-number ]]\n" 1>&2 exit 1 } # echo names in ${1} that are not in ${2} missing() { for i in ${1} ; do MATCHED=0 for j in ${2} ; do if [[ $i = $j ]] ; then MATCHED=1 fi done if [[ $MATCHED == 0 ]] ; then echo $i fi done } # create a filter table for LP service lp_config_filters() { 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 } # enable/disable LP related service(s) lp_config_service() { # (enable | disable) svcadm ${1} -s svc:/application/print/server:default # svcadm ${1} -s svc:/application/print/rfc1179:default # svcadm ${1} -s svc:/application/print/ipp-listener:default } # synchronize printers.conf with LP configuration changes lp_config_sync_pconf() { # (pre) (post) if [[ "${1}" != "${2}" ]] ; then ADDED=$(missing "${2}" "${1}") REMOVED=$(missing "${1}" "${2}") lp_server=${server:-${HOST}} for DEST in ${ADDED} ; do lp_uri="ipp://${lp_server}/printers/${DEST}" lp_bsdaddr="${lp_server},${DEST},Solaris" ${LPSET} -n system \ -a "printer-uri-supported=${lp_uri}" \ -a "bsdaddr=${lp_bsdaddr}" \ ${DEST} 2>/dev/null done for DEST in ${REMOVED} ; do ${LPSET} -n system -x ${DEST} 2>/dev/null done fi } # Delete all destinations in printers.conf delete_all() { for DEST in $(lpget -n system list | egrep -e '.+:$' | sed -e 's/://') do ${LPSET} -n system -x ${DEST} status=$? done } # # Execution begins here # # be sure that we can run lpset and lpget if [[ ! -x ${LPSET} || ! -x ${LPGET} ]] ; then gettext "lpadmin: System error; cannot set default printer\n" 1>&2 exit 2 fi 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 # if [[ ${1} = "-d" ]] ; then if [[ $# -eq 1 ]] ; then # remove the "default" ${LPGET} -n system _default >/dev/null 2>&1 exit_code=$? if [[ ${exit_code} -eq 0 ]] ; then ${LPSET} -n system -x _default exit_code=$? else # no default, nothing to do exit_code=0 fi elif [[ $# -eq 2 ]] ; then # add/change the "default" ${LPGET} -k bsdaddr ${2} >/dev/null 2>&1 exit_code=$? if [[ $exit_code -eq 0 ]] ; then ${LPSET} -n system -a "use=${2}" _default exit_code=$? else # can't set default to an unconfigured printer gettext "${2}: undefined printer\n" 1>&1 fi else # invalid usage usage exit 1 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}" || -n "${server}" || \ -n "${device}" || -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 && ! -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) local="true" ;; esac done # # We don't have anything to do; let user know and bail # if [[ ! -n "${printer}" && ! -n "${delete}" && ! -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}" && ! -n "${server}" && ! -n "${delete}" && \ ! -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 # 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 LP configuration for a local printer served by lpsched if [[ -x ${LPADMIN} && ( -n "${local}" || -n "${device}" || \ -f /etc/lp/printers/${printer}/configuration || \ -f /etc/lp/classes/${printer} ) ]] ; then # enumerate LP configured printers before modification PRE=$(/bin/ls /etc/lp/printers 2>/dev/null ; /bin/ls /etc/lp/classes \ 2>/dev/null) # if there are no printers configured, enable LP service(s) [[ -z "${PRE}" ]] && lp_config_service enable # add filters to LP service lp_config_filters # modify LP destination(s) CMD=${LPADMIN} while [[ -n "$*" ]] ; do # to deal with multi-word arguments CMD="$CMD \"$1\"" shift done case "$CMD" in *\"-D\") CMD="$CMD \"\"" ;; esac # execute the LP lpadmin command eval $CMD exit_code=$? # enumerate LP configured printers after modification POST=$(/bin/ls /etc/lp/printers 2>/dev/null ; /bin/ls /etc/lp/classes \ 2>/dev/null) # if there are no destinations, disable the service(s) [[ -z "${POST}" ]] && lp_config_service disable # sync printers.conf with LP configuration lp_config_sync_pconf "${PRE}" "${POST}" fi # Do any printers.conf configuration that is required if [[ -n "${delete}" ]] ; then if [[ "${delete}" = "all" ]] ; then [[ $exit_code -eq 0 ]] && delete_all elif [[ -n "${local}" ]] ; then ${LPSET} -n system -x ${delete} exit_code=$? fi elif [[ -n "${local}" ]] ; then if [[ -n "${printer}" ]] ; then ${LPSET} -n system \ -a "printer-uri-supported=${uri}" \ -a "bsdaddr=${bsdaddr}" ${printer} exit_code=$? fi if [[ -n "${printer}" && -n "${description}" ]] ; then ${LPSET} -n system \ -a "description=${description}" ${printer} exit_code=$? fi fi # if the "default" doesn't resolve a "bsdaddr", the printer is gone, remove it ${LPGET} -n system -k bsdaddr _default >/dev/null 2>&1 || ${LPSET} -n system -x _default >/dev/null 2>&1 exit $exit_code