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# 232264Sjacobs# Copyright 2006 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# 280Sstevel@tonic-gatePATH=/bin:/usr/bin:/usr/sbin export PATH 290Sstevel@tonic-gate 300Sstevel@tonic-gateTEXTDOMAIN="SUNW_OST_OSCMD" 310Sstevel@tonic-gateexport TEXTDOMAIN 320Sstevel@tonic-gate 330Sstevel@tonic-gateLPSET=/usr/bin/lpset 340Sstevel@tonic-gateLPGET=/usr/bin/lpget 35*3125SjacobsLPSTAT=/usr/bin/lpstat 362792SjacobsLPADMIN=/usr/lib/lp/local/lpadmin 370Sstevel@tonic-gate 382792SjacobsHOST=$(/bin/uname -n) 390Sstevel@tonic-gateexit_code=0 400Sstevel@tonic-gate 410Sstevel@tonic-gateusage() { 420Sstevel@tonic-gate gettext "Usage:\n" 1>&2 430Sstevel@tonic-gate gettext " lpadmin -p (printer) (options)\n" 1>&2 440Sstevel@tonic-gate gettext " lpadmin -x (dest)\n" 1>&2 450Sstevel@tonic-gate gettext " lpadmin -d (dest)\n" 1>&2 460Sstevel@tonic-gate gettext " lpadmin -S print-wheel -A alert-type [ -W minutes ]\n" 1>&2 470Sstevel@tonic-gate gettext " [ -Q requests ]\n" 1>&2 480Sstevel@tonic-gate gettext " lpadmin -M -f form-name [ -a [ -o filebreak ]\n" 1>&2 490Sstevel@tonic-gate gettext " [ -t tray-number ]]\n" 1>&2 500Sstevel@tonic-gate exit 1 510Sstevel@tonic-gate} 520Sstevel@tonic-gate 532792Sjacobs# echo names in ${1} that are not in ${2} 542792Sjacobsmissing() { 552792Sjacobs for i in ${1} ; do 562792Sjacobs MATCHED=0 572792Sjacobs for j in ${2} ; do 582792Sjacobs if [[ $i = $j ]] ; then 592792Sjacobs MATCHED=1 602792Sjacobs fi 612792Sjacobs done 622792Sjacobs if [[ $MATCHED == 0 ]] ; then 632792Sjacobs echo $i 642792Sjacobs fi 652792Sjacobs done 662792Sjacobs} 670Sstevel@tonic-gate 682792Sjacobs# create a filter table for LP service 692792Sjacobslp_config_filters() { 702792Sjacobs if [[ ! -f /etc/lp/filter.table ]] ; then 712792Sjacobs cd /etc/lp/fd ; for filter in *.fd ; do 722792Sjacobs /usr/sbin/lpfilter \ 732792Sjacobs -f $(/usr/bin/basename $filter .fd) \ 742792Sjacobs -F $filter 752792Sjacobs done 762792Sjacobs fi 772792Sjacobs} 780Sstevel@tonic-gate 792792Sjacobs# enable/disable LP related service(s) 802792Sjacobslp_config_service() { # (enable | disable) 812792Sjacobs svcadm ${1} -s svc:/application/print/server:default 822792Sjacobs # svcadm ${1} -s svc:/application/print/rfc1179:default 832792Sjacobs # svcadm ${1} -s svc:/application/print/ipp-listener:default 842792Sjacobs} 852792Sjacobs 862792Sjacobs# synchronize printers.conf with LP configuration changes 872792Sjacobslp_config_sync_pconf() { # (pre) (post) 882792Sjacobs if [[ "${1}" != "${2}" ]] ; then 892792Sjacobs ADDED=$(missing "${2}" "${1}") 902792Sjacobs REMOVED=$(missing "${1}" "${2}") 910Sstevel@tonic-gate 922792Sjacobs lp_server=${server:-${HOST}} 932792Sjacobs for DEST in ${ADDED} ; do 942792Sjacobs lp_uri="ipp://${lp_server}/printers/${DEST}" 952792Sjacobs lp_bsdaddr="${lp_server},${DEST},Solaris" 962792Sjacobs ${LPSET} -n system \ 972792Sjacobs -a "printer-uri-supported=${lp_uri}" \ 982792Sjacobs -a "bsdaddr=${lp_bsdaddr}" \ 992792Sjacobs ${DEST} 2>/dev/null 1002792Sjacobs done 1010Sstevel@tonic-gate 1022792Sjacobs for DEST in ${REMOVED} ; do 1032792Sjacobs ${LPSET} -n system -x ${DEST} 2>/dev/null 1042792Sjacobs done 1052792Sjacobs fi 1062792Sjacobs} 1072792Sjacobs 1082792Sjacobs# Delete all destinations in printers.conf 1092792Sjacobsdelete_all() { 1102792Sjacobs for DEST in $(lpget -n system list | egrep -e '.+:$' | sed -e 's/://') 1112792Sjacobs do 1122792Sjacobs ${LPSET} -n system -x ${DEST} 1132792Sjacobs status=$? 1142792Sjacobs done 1152792Sjacobs} 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate# 1182792Sjacobs# Execution begins here 1190Sstevel@tonic-gate# 1200Sstevel@tonic-gate 1212792Sjacobs# be sure that we can run lpset and lpget 1222792Sjacobsif [[ ! -x ${LPSET} || ! -x ${LPGET} ]] ; then 1232792Sjacobs gettext "lpadmin: System error; cannot set default printer\n" 1>&2 1242792Sjacobs exit 2 1250Sstevel@tonic-gatefi 1260Sstevel@tonic-gate 1272792Sjacobsif [[ $# -lt 1 ]] ; then 1280Sstevel@tonic-gate usage 1290Sstevel@tonic-gate exit 1 1300Sstevel@tonic-gatefi 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate# Deal with the -d option independently since getopts does not handle 1330Sstevel@tonic-gate# options that may or may not have arguments 1340Sstevel@tonic-gate# 1352792Sjacobsif [[ ${1} = "-d" ]] ; then 1362792Sjacobs if [[ $# -eq 1 ]] ; then # remove the "default" 1372792Sjacobs ${LPGET} -n system _default >/dev/null 2>&1 1382792Sjacobs exit_code=$? 1390Sstevel@tonic-gate 1402792Sjacobs if [[ ${exit_code} -eq 0 ]] ; then 1412792Sjacobs ${LPSET} -n system -x _default 1422792Sjacobs exit_code=$? 1432792Sjacobs else # no default, nothing to do 1442792Sjacobs exit_code=0 1452792Sjacobs fi 1462792Sjacobs elif [[ $# -eq 2 ]] ; then # add/change the "default" 1472792Sjacobs ${LPGET} -k bsdaddr ${2} >/dev/null 2>&1 1482792Sjacobs exit_code=$? 1492792Sjacobs 1502792Sjacobs if [[ $exit_code -eq 0 ]] ; then 1512792Sjacobs ${LPSET} -n system -a "use=${2}" _default 1522792Sjacobs exit_code=$? 1532792Sjacobs else # can't set default to an unconfigured printer 1542792Sjacobs gettext "${2}: undefined printer\n" 1>&1 1552792Sjacobs fi 1562792Sjacobs else # invalid usage 1570Sstevel@tonic-gate usage 1580Sstevel@tonic-gate exit 1 1590Sstevel@tonic-gate fi 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate exit ${exit_code} 1620Sstevel@tonic-gatefi 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate# Strip off legal options 1650Sstevel@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 1660Sstevel@tonic-gatedo 1670Sstevel@tonic-gate case $arg in 1680Sstevel@tonic-gate D) 1690Sstevel@tonic-gate description="${OPTARG}" 1700Sstevel@tonic-gate ;; 1710Sstevel@tonic-gate p) 1722792Sjacobs if [[ -n "${delete}" ]] ; then 1730Sstevel@tonic-gate usage 1740Sstevel@tonic-gate fi 1750Sstevel@tonic-gate printer=${OPTARG} 1760Sstevel@tonic-gate ;; 1770Sstevel@tonic-gate s) 1780Sstevel@tonic-gate server=${OPTARG} 1790Sstevel@tonic-gate ;; 1800Sstevel@tonic-gate v|U) 1810Sstevel@tonic-gate device=${OPTARG} 1822792Sjacobs if [[ ! -n "${server}" ]] ; then 183831Swendyp server=${HOST} 184831Swendyp fi 185*3125Sjacobs local="true" 1860Sstevel@tonic-gate ;; 1870Sstevel@tonic-gate x) 1882792Sjacobs if [[ -n "${printer}" || -n "${server}" || \ 1892792Sjacobs -n "${device}" || -n "${description}" ]] ; then 1900Sstevel@tonic-gate usage 1910Sstevel@tonic-gate fi 1920Sstevel@tonic-gate delete=${OPTARG} 1930Sstevel@tonic-gate printer=${OPTARG} 1942792Sjacobs if [[ ${printer} = "all" ]] ; then 1950Sstevel@tonic-gate local="true" 1960Sstevel@tonic-gate fi 1970Sstevel@tonic-gate ;; 1980Sstevel@tonic-gate S|M|A) 1990Sstevel@tonic-gate local="true" 2000Sstevel@tonic-gate ;; 2010Sstevel@tonic-gate c) 2020Sstevel@tonic-gate class=${OPTARG} 2030Sstevel@tonic-gate local="true" 2042792Sjacobs if [[ ! -f ${LPGET} ]] ; then 2050Sstevel@tonic-gate gettext "lpadmin: System error; cannot set class\n " 1>&2 2060Sstevel@tonic-gate exit 2 2070Sstevel@tonic-gate fi 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate ${LPGET} "${class}" > /dev/null 2>&1 2100Sstevel@tonic-gate lpget_class=$? 2112792Sjacobs if [[ ${lpget_class} -eq 0 && ! -r /etc/lp/classes/"${class}" ]] ; then 2120Sstevel@tonic-gate gettext "lpadmin: ERROR: Can't create class ${class}.\n" 1>&2 2130Sstevel@tonic-gate gettext " TO FIX: This is an existing printer name;\n" 1>&2 2140Sstevel@tonic-gate gettext " choose another name.\n" 1>&2 2150Sstevel@tonic-gate exit 1 2160Sstevel@tonic-gate fi 2170Sstevel@tonic-gate ;; 2180Sstevel@tonic-gate r) 2190Sstevel@tonic-gate local="true" 2200Sstevel@tonic-gate ;; 2210Sstevel@tonic-gate esac 2220Sstevel@tonic-gatedone 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate# 2250Sstevel@tonic-gate# We don't have anything to do; let user know and bail 2260Sstevel@tonic-gate# 2272792Sjacobsif [[ ! -n "${printer}" && ! -n "${delete}" && ! -n "${local}" ]] ; then 2280Sstevel@tonic-gate gettext "lpadmin: ERROR: Nothing to do.\n" 1>&2 2290Sstevel@tonic-gate gettext " TO FIX: You must give one of these options:\n" 1>&2 2300Sstevel@tonic-gate gettext " -p, -d, -x -S\n" 1>&2 2310Sstevel@tonic-gate exit 1 2320Sstevel@tonic-gatefi 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate# 2350Sstevel@tonic-gate# Printer does not exist 2360Sstevel@tonic-gate# To be consistent with 2.5, assume adding local printer 2370Sstevel@tonic-gate# 2382792Sjacobsif [[ ! -n "${device}" && ! -n "${server}" && ! -n "${delete}" && \ 2392792Sjacobs ! -n "${local}" ]] ; then 2400Sstevel@tonic-gate ${LPGET} "${printer}" > /dev/null 2>&1 2410Sstevel@tonic-gate lpget_stat=$? 2422792Sjacobs if [[ ${lpget_stat} -ne 0 ]] ; then 2430Sstevel@tonic-gate gettext "lpadmin: ERROR: Missing -U or -v option.\n" 1>&2 2440Sstevel@tonic-gate gettext " TO FIX: Local printers must have\n" 1>&2 2450Sstevel@tonic-gate gettext " a port defined (-v option) or\n" 1>&2 2460Sstevel@tonic-gate gettext " have dial-out instructions (-U option).\n" 1>&2 2470Sstevel@tonic-gate exit 1 2480Sstevel@tonic-gate fi 2490Sstevel@tonic-gatefi 2500Sstevel@tonic-gate 2512264Sjacobs# process the "server" value 2522264Sjacobs# It can be a hostname, UUCP form (server!queue), RCMD form(queue@server), 2532264Sjacobs# or in URI form ({scheme}://{endpoint}) 2542264Sjacobs# 2552264Sjacobscase "${server}" in 2562264Sjacobs *://*) # URI form 2572264Sjacobs uri=${server} 2582792Sjacobs rem_printer=$(expr "${server}" : ".*://.*/\([^/]*\)") 2592792Sjacobs server=$(expr "${server}" : ".*://\([^/]*\)/.*") 2602264Sjacobs ;; 2612264Sjacobs *@*) # RCMD form 2622792Sjacobs rem_printer=$(expr "${server}" : "\(.*\)@.*") 2632792Sjacobs server=$(expr "${server}" : ".*@\(.*\)") 2642264Sjacobs ;; 2652264Sjacobs *!*) # UUCP form 2662792Sjacobs rem_printer=$(expr "${server}" : ".*!\(.*\)") 2672792Sjacobs server=$(expr "${server}" : "\(.*\)!.*") 2682264Sjacobs ;; 2692264Sjacobs *) # hostname 2702264Sjacobs rem_printer=${printer} 2712264Sjacobs ;; 2722264Sjacobsesac 2732792Sjacobs 2742792Sjacobsif [[ -n "${server}" ]] ; then 275*3125Sjacobs # if we need a uri, find the "best" one. 276*3125Sjacobs if [[ -z "${uri}" ]] ; then 277*3125Sjacobs uri="ipp://${server}/printers/${rem_printer}" 278*3125Sjacobs ${LPSTAT} -p ${uri} >/dev/null 2>&1 279*3125Sjacobs if [[ $? -ne 0 ]] ; then 280*3125Sjacobs uri="lpd://${server}/printers/${rem_printer}#Solaris" 281*3125Sjacobs fi 282*3125Sjacobs fi 283*3125Sjacobs # set the bsdaddr 2842264Sjacobs bsdaddr="${server},${rem_printer},Solaris" 2850Sstevel@tonic-gatefi 2860Sstevel@tonic-gate 287*3125Sjacobs# if there is a "device" or LP configuration, it's local 288*3125Sjacobsif [[ -n "${device}" || -f /etc/lp/printers/${printer}/configuration || \ 289*3125Sjacobs -f /etc/lp/classes/${printer} ]] ; then 290*3125Sjacobs local="true" 291*3125Sjacobsfi 2922792Sjacobs 2932792Sjacobs# Do the LP configuration for a local printer served by lpsched 294*3125Sjacobsif [[ -x ${LPADMIN} && -n "${local}" ]] ; then 2952792Sjacobs # enumerate LP configured printers before modification 2962792Sjacobs PRE=$(/bin/ls /etc/lp/printers 2>/dev/null ; /bin/ls /etc/lp/classes \ 2972792Sjacobs 2>/dev/null) 2982792Sjacobs 2992792Sjacobs # if there are no printers configured, enable LP service(s) 3002792Sjacobs [[ -z "${PRE}" ]] && lp_config_service enable 3012792Sjacobs 3022792Sjacobs # add filters to LP service 3032792Sjacobs lp_config_filters 3042792Sjacobs 3052792Sjacobs # modify LP destination(s) 3062792Sjacobs CMD=${LPADMIN} 3072792Sjacobs while [[ -n "$*" ]] ; do # to deal with multi-word arguments 3082792Sjacobs CMD="$CMD \"$1\"" 3092792Sjacobs shift 3102792Sjacobs done 3112792Sjacobs case "$CMD" in 3122792Sjacobs *\"-D\") 3132792Sjacobs CMD="$CMD \"\"" 3142792Sjacobs ;; 3152792Sjacobs esac 3160Sstevel@tonic-gate 3172792Sjacobs # execute the LP lpadmin command 3182792Sjacobs eval $CMD 3192792Sjacobs exit_code=$? 3202792Sjacobs 3212792Sjacobs # enumerate LP configured printers after modification 3222792Sjacobs POST=$(/bin/ls /etc/lp/printers 2>/dev/null ; /bin/ls /etc/lp/classes \ 3232792Sjacobs 2>/dev/null) 3242792Sjacobs 3252792Sjacobs # if there are no destinations, disable the service(s) 3262792Sjacobs [[ -z "${POST}" ]] && lp_config_service disable 3272792Sjacobs 3282792Sjacobs # sync printers.conf with LP configuration 3292792Sjacobs lp_config_sync_pconf "${PRE}" "${POST}" 3302792Sjacobsfi 3312792Sjacobs 3322792Sjacobs# Do any printers.conf configuration that is required 3332792Sjacobsif [[ -n "${delete}" ]] ; then 3342792Sjacobs if [[ "${delete}" = "all" ]] ; then 3352792Sjacobs [[ $exit_code -eq 0 ]] && delete_all 336*3125Sjacobs elif [[ -z "${local}" ]] ; then 3372792Sjacobs ${LPSET} -n system -x ${delete} 3382792Sjacobs exit_code=$? 3392792Sjacobs fi 340*3125Sjacobselif [[ -z "${local}" ]] ; then 3412792Sjacobs if [[ -n "${printer}" ]] ; then 3422792Sjacobs ${LPSET} -n system \ 3432792Sjacobs -a "printer-uri-supported=${uri}" \ 3442792Sjacobs -a "bsdaddr=${bsdaddr}" ${printer} 3450Sstevel@tonic-gate exit_code=$? 3460Sstevel@tonic-gate fi 3470Sstevel@tonic-gate 3482792Sjacobs if [[ -n "${printer}" && -n "${description}" ]] ; then 3492792Sjacobs ${LPSET} -n system \ 3502792Sjacobs -a "description=${description}" ${printer} 3512792Sjacobs exit_code=$? 3520Sstevel@tonic-gate fi 3530Sstevel@tonic-gatefi 3540Sstevel@tonic-gate 3552792Sjacobs# if the "default" doesn't resolve a "bsdaddr", the printer is gone, remove it 3562792Sjacobs${LPGET} -n system -k bsdaddr _default >/dev/null 2>&1 || 3572792Sjacobs ${LPSET} -n system -x _default >/dev/null 2>&1 3580Sstevel@tonic-gate 3590Sstevel@tonic-gateexit $exit_code 360