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 350Sstevel@tonic-gateLPSET=/usr/bin/lpset 360Sstevel@tonic-gateLPGET=/usr/bin/lpget 373125SjacobsLPSTAT=/usr/bin/lpstat 382792SjacobsLPADMIN=/usr/lib/lp/local/lpadmin 393478SjacobsCOMM=/usr/bin/comm 40*3781SceasthaPPDMGR=/usr/sbin/ppdmgr 410Sstevel@tonic-gate 422792SjacobsHOST=$(/bin/uname -n) 430Sstevel@tonic-gateexit_code=0 440Sstevel@tonic-gate 450Sstevel@tonic-gateusage() { 460Sstevel@tonic-gate gettext "Usage:\n" 1>&2 470Sstevel@tonic-gate gettext " lpadmin -p (printer) (options)\n" 1>&2 480Sstevel@tonic-gate gettext " lpadmin -x (dest)\n" 1>&2 490Sstevel@tonic-gate gettext " lpadmin -d (dest)\n" 1>&2 500Sstevel@tonic-gate gettext " lpadmin -S print-wheel -A alert-type [ -W minutes ]\n" 1>&2 510Sstevel@tonic-gate gettext " [ -Q requests ]\n" 1>&2 520Sstevel@tonic-gate gettext " lpadmin -M -f form-name [ -a [ -o filebreak ]\n" 1>&2 530Sstevel@tonic-gate gettext " [ -t tray-number ]]\n" 1>&2 540Sstevel@tonic-gate exit 1 550Sstevel@tonic-gate} 560Sstevel@tonic-gate 572792Sjacobs# create a filter table for LP service 582792Sjacobslp_config_filters() { 592792Sjacobs if [[ ! -f /etc/lp/filter.table ]] ; then 602792Sjacobs cd /etc/lp/fd ; for filter in *.fd ; do 612792Sjacobs /usr/sbin/lpfilter \ 622792Sjacobs -f $(/usr/bin/basename $filter .fd) \ 632792Sjacobs -F $filter 642792Sjacobs done 652792Sjacobs fi 662792Sjacobs} 670Sstevel@tonic-gate 682792Sjacobs# enable/disable LP related service(s) 692792Sjacobslp_config_service() { # (enable | disable) 702792Sjacobs svcadm ${1} -s svc:/application/print/server:default 712792Sjacobs # svcadm ${1} -s svc:/application/print/rfc1179:default 722792Sjacobs # svcadm ${1} -s svc:/application/print/ipp-listener:default 732792Sjacobs} 742792Sjacobs 752792Sjacobs# synchronize printers.conf with LP configuration changes 762792Sjacobslp_config_sync_pconf() { # (pre) (post) 773478Sjacobs ADDED=$(${COMM} -13 ${1} ${2}) 783478Sjacobs REMOVED=$(${COMM} -23 ${1} ${2}) 790Sstevel@tonic-gate 803478Sjacobs lp_server=${server:-${HOST}} 813478Sjacobs for DEST in ${ADDED} ; do 823478Sjacobs lp_uri="ipp://${lp_server}/printers/${DEST}" 833478Sjacobs lp_bsdaddr="${lp_server},${DEST},Solaris" 843478Sjacobs ${LPSET} -n system \ 853478Sjacobs -a "printer-uri-supported=${lp_uri}" \ 863478Sjacobs -a "bsdaddr=${lp_bsdaddr}" \ 873478Sjacobs ${DEST} 2>/dev/null 883478Sjacobs done 890Sstevel@tonic-gate 903478Sjacobs for DEST in ${REMOVED} ; do 913478Sjacobs ${LPSET} -n system -x ${DEST} 2>/dev/null 923478Sjacobs done 932792Sjacobs} 942792Sjacobs 952792Sjacobs# Delete all destinations in printers.conf 962792Sjacobsdelete_all() { 972792Sjacobs for DEST in $(lpget -n system list | egrep -e '.+:$' | sed -e 's/://') 982792Sjacobs do 992792Sjacobs ${LPSET} -n system -x ${DEST} 1002792Sjacobs status=$? 1012792Sjacobs done 1022792Sjacobs} 1030Sstevel@tonic-gate 104*3781Sceastha# Call the ppdmgr utility to add a new PPD file to the system. 105*3781Sceastha# 106*3781Sceastha# $1 - path to PPD file 107*3781Sceastha# $2 - label name (optional) 108*3781Sceasthaadd_new_ppd_file() { 109*3781Sceastha # Add new ppd file and echo full path it was actually saved to 110*3781Sceastha ppdmgrcmd="${PPDMGR} -a ${1} -w" 111*3781Sceastha 112*3781Sceastha ppderrfile=/tmp/lpadminerror.$$ 113*3781Sceastha ppd_file=$(${ppdmgrcmd} 2>${ppderrfile}) 114*3781Sceastha ppdmgrrc=$? 115*3781Sceastha if [[ -s "${ppderrfile}" ]] ; then 116*3781Sceastha print -n "lpadmin: " 1>&2 117*3781Sceastha cat ${ppderrfile} 1>&2 118*3781Sceastha rm -f ${ppderrfile} >/dev/null 2>&1 119*3781Sceastha if [[ ${ppdmgrrc} -ne 0 ]] ; then 120*3781Sceastha exit 1 121*3781Sceastha fi 122*3781Sceastha fi 123*3781Sceastha rm -f ${ppderrfile} >/dev/null 2>&1 124*3781Sceastha} 125*3781Sceastha 1260Sstevel@tonic-gate# 1272792Sjacobs# Execution begins here 1280Sstevel@tonic-gate# 1290Sstevel@tonic-gate 1302792Sjacobs# be sure that we can run lpset and lpget 1312792Sjacobsif [[ ! -x ${LPSET} || ! -x ${LPGET} ]] ; then 1322792Sjacobs gettext "lpadmin: System error; cannot set default printer\n" 1>&2 1332792Sjacobs exit 2 1340Sstevel@tonic-gatefi 1350Sstevel@tonic-gate 1362792Sjacobsif [[ $# -lt 1 ]] ; then 1370Sstevel@tonic-gate usage 1380Sstevel@tonic-gate exit 1 1390Sstevel@tonic-gatefi 1400Sstevel@tonic-gate 1410Sstevel@tonic-gate# Deal with the -d option independently since getopts does not handle 1420Sstevel@tonic-gate# options that may or may not have arguments 1430Sstevel@tonic-gate# 1442792Sjacobsif [[ ${1} = "-d" ]] ; then 1452792Sjacobs if [[ $# -eq 1 ]] ; then # remove the "default" 1462792Sjacobs ${LPGET} -n system _default >/dev/null 2>&1 1472792Sjacobs exit_code=$? 1480Sstevel@tonic-gate 1492792Sjacobs if [[ ${exit_code} -eq 0 ]] ; then 1502792Sjacobs ${LPSET} -n system -x _default 1512792Sjacobs exit_code=$? 1522792Sjacobs else # no default, nothing to do 1532792Sjacobs exit_code=0 1542792Sjacobs fi 1552792Sjacobs elif [[ $# -eq 2 ]] ; then # add/change the "default" 1562792Sjacobs ${LPGET} -k bsdaddr ${2} >/dev/null 2>&1 1572792Sjacobs exit_code=$? 1582792Sjacobs 1592792Sjacobs if [[ $exit_code -eq 0 ]] ; then 1602792Sjacobs ${LPSET} -n system -a "use=${2}" _default 1612792Sjacobs exit_code=$? 1622792Sjacobs else # can't set default to an unconfigured printer 1632792Sjacobs gettext "${2}: undefined printer\n" 1>&1 1642792Sjacobs fi 1652792Sjacobs else # invalid usage 1660Sstevel@tonic-gate usage 1670Sstevel@tonic-gate exit 1 1680Sstevel@tonic-gate fi 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate exit ${exit_code} 1710Sstevel@tonic-gatefi 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate# Strip off legal options 1740Sstevel@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 1750Sstevel@tonic-gatedo 1760Sstevel@tonic-gate case $arg in 1770Sstevel@tonic-gate D) 1780Sstevel@tonic-gate description="${OPTARG}" 1790Sstevel@tonic-gate ;; 180*3781Sceastha n) 181*3781Sceastha ppd_file="${OPTARG}" 182*3781Sceastha ;; 1830Sstevel@tonic-gate p) 1842792Sjacobs if [[ -n "${delete}" ]] ; then 1850Sstevel@tonic-gate usage 1860Sstevel@tonic-gate fi 1870Sstevel@tonic-gate printer=${OPTARG} 1880Sstevel@tonic-gate ;; 1890Sstevel@tonic-gate s) 1900Sstevel@tonic-gate server=${OPTARG} 1910Sstevel@tonic-gate ;; 1920Sstevel@tonic-gate v|U) 1930Sstevel@tonic-gate device=${OPTARG} 1942792Sjacobs if [[ ! -n "${server}" ]] ; then 195831Swendyp server=${HOST} 196831Swendyp fi 1973125Sjacobs local="true" 1980Sstevel@tonic-gate ;; 1990Sstevel@tonic-gate x) 2002792Sjacobs if [[ -n "${printer}" || -n "${server}" || \ 2012792Sjacobs -n "${device}" || -n "${description}" ]] ; then 2020Sstevel@tonic-gate usage 2030Sstevel@tonic-gate fi 2040Sstevel@tonic-gate delete=${OPTARG} 2050Sstevel@tonic-gate printer=${OPTARG} 2062792Sjacobs if [[ ${printer} = "all" ]] ; then 2070Sstevel@tonic-gate local="true" 2080Sstevel@tonic-gate fi 2090Sstevel@tonic-gate ;; 2100Sstevel@tonic-gate S|M|A) 2110Sstevel@tonic-gate local="true" 2120Sstevel@tonic-gate ;; 2130Sstevel@tonic-gate c) 2140Sstevel@tonic-gate class=${OPTARG} 2150Sstevel@tonic-gate local="true" 2162792Sjacobs if [[ ! -f ${LPGET} ]] ; then 2170Sstevel@tonic-gate gettext "lpadmin: System error; cannot set class\n " 1>&2 2180Sstevel@tonic-gate exit 2 2190Sstevel@tonic-gate fi 2200Sstevel@tonic-gate 2210Sstevel@tonic-gate ${LPGET} "${class}" > /dev/null 2>&1 2220Sstevel@tonic-gate lpget_class=$? 2232792Sjacobs if [[ ${lpget_class} -eq 0 && ! -r /etc/lp/classes/"${class}" ]] ; then 2240Sstevel@tonic-gate gettext "lpadmin: ERROR: Can't create class ${class}.\n" 1>&2 2250Sstevel@tonic-gate gettext " TO FIX: This is an existing printer name;\n" 1>&2 2260Sstevel@tonic-gate gettext " choose another name.\n" 1>&2 2270Sstevel@tonic-gate exit 1 2280Sstevel@tonic-gate fi 2290Sstevel@tonic-gate ;; 2300Sstevel@tonic-gate r) 2310Sstevel@tonic-gate local="true" 2320Sstevel@tonic-gate ;; 2330Sstevel@tonic-gate esac 2340Sstevel@tonic-gatedone 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate# 2370Sstevel@tonic-gate# We don't have anything to do; let user know and bail 2380Sstevel@tonic-gate# 2392792Sjacobsif [[ ! -n "${printer}" && ! -n "${delete}" && ! -n "${local}" ]] ; then 2400Sstevel@tonic-gate gettext "lpadmin: ERROR: Nothing to do.\n" 1>&2 2410Sstevel@tonic-gate gettext " TO FIX: You must give one of these options:\n" 1>&2 2420Sstevel@tonic-gate gettext " -p, -d, -x -S\n" 1>&2 2430Sstevel@tonic-gate exit 1 2440Sstevel@tonic-gatefi 2450Sstevel@tonic-gate 2460Sstevel@tonic-gate# 2470Sstevel@tonic-gate# Printer does not exist 2480Sstevel@tonic-gate# To be consistent with 2.5, assume adding local printer 2490Sstevel@tonic-gate# 2502792Sjacobsif [[ ! -n "${device}" && ! -n "${server}" && ! -n "${delete}" && \ 2512792Sjacobs ! -n "${local}" ]] ; then 2520Sstevel@tonic-gate ${LPGET} "${printer}" > /dev/null 2>&1 2530Sstevel@tonic-gate lpget_stat=$? 2542792Sjacobs if [[ ${lpget_stat} -ne 0 ]] ; then 2550Sstevel@tonic-gate gettext "lpadmin: ERROR: Missing -U or -v option.\n" 1>&2 2560Sstevel@tonic-gate gettext " TO FIX: Local printers must have\n" 1>&2 2570Sstevel@tonic-gate gettext " a port defined (-v option) or\n" 1>&2 2580Sstevel@tonic-gate gettext " have dial-out instructions (-U option).\n" 1>&2 2590Sstevel@tonic-gate exit 1 2600Sstevel@tonic-gate fi 2610Sstevel@tonic-gatefi 2620Sstevel@tonic-gate 2632264Sjacobs# process the "server" value 2642264Sjacobs# It can be a hostname, UUCP form (server!queue), RCMD form(queue@server), 2652264Sjacobs# or in URI form ({scheme}://{endpoint}) 2662264Sjacobs# 2672264Sjacobscase "${server}" in 2682264Sjacobs *://*) # URI form 2692264Sjacobs uri=${server} 2702792Sjacobs rem_printer=$(expr "${server}" : ".*://.*/\([^/]*\)") 2712792Sjacobs server=$(expr "${server}" : ".*://\([^/]*\)/.*") 2722264Sjacobs ;; 2732264Sjacobs *@*) # RCMD form 2742792Sjacobs rem_printer=$(expr "${server}" : "\(.*\)@.*") 2752792Sjacobs server=$(expr "${server}" : ".*@\(.*\)") 2762264Sjacobs ;; 2772264Sjacobs *!*) # UUCP form 2782792Sjacobs rem_printer=$(expr "${server}" : ".*!\(.*\)") 2792792Sjacobs server=$(expr "${server}" : "\(.*\)!.*") 2802264Sjacobs ;; 2812264Sjacobs *) # hostname 2822264Sjacobs rem_printer=${printer} 2832264Sjacobs ;; 2842264Sjacobsesac 2852792Sjacobs 2863125Sjacobs# if there is a "device" or LP configuration, it's local 2873125Sjacobsif [[ -n "${device}" || -f /etc/lp/printers/${printer}/configuration || \ 2883125Sjacobs -f /etc/lp/classes/${printer} ]] ; then 2893125Sjacobs local="true" 2903125Sjacobsfi 2912792Sjacobs 2922792Sjacobs# Do the LP configuration for a local printer served by lpsched 2933125Sjacobsif [[ -x ${LPADMIN} && -n "${local}" ]] ; then 2942792Sjacobs # enumerate LP configured printers before modification 2953478Sjacobs PRE=/tmp/lpadmin-pre.$$ 2963478Sjacobs (/bin/ls /etc/lp/printers 2>/dev/null ; /bin/ls /etc/lp/classes \ 2973478Sjacobs 2>/dev/null) >${PRE} 2982792Sjacobs 2992792Sjacobs # if there are no printers configured, enable LP service(s) 300*3781Sceastha [[ ! -s "${PRE}" ]] && lp_config_service enable 3012792Sjacobs 3022792Sjacobs # add filters to LP service 3032792Sjacobs lp_config_filters 3042792Sjacobs 305*3781Sceastha # add new ppd file to PPD file repositories 306*3781Sceastha if [[ -n "${ppd_file}" && -x ${PPDMGR} ]] ; then 307*3781Sceastha add_new_ppd_file "${ppd_file}" 308*3781Sceastha fi 309*3781Sceastha 3102792Sjacobs # modify LP destination(s) 3112792Sjacobs CMD=${LPADMIN} 3122792Sjacobs while [[ -n "$*" ]] ; do # to deal with multi-word arguments 3132792Sjacobs CMD="$CMD \"$1\"" 314*3781Sceastha # replace the ppd_file originally specified with the -n option 315*3781Sceastha # with the one returned from call to ppdmgr 316*3781Sceastha if [[ "${1}" = "-n" ]] ; then 317*3781Sceastha CMD="$CMD \"${ppd_file}\"" 318*3781Sceastha shift 319*3781Sceastha fi 3202792Sjacobs shift 3212792Sjacobs done 3222792Sjacobs case "$CMD" in 3232792Sjacobs *\"-D\") 3242792Sjacobs CMD="$CMD \"\"" 3252792Sjacobs ;; 3262792Sjacobs esac 3270Sstevel@tonic-gate 3282792Sjacobs # execute the LP lpadmin command 3292792Sjacobs eval $CMD 3302792Sjacobs exit_code=$? 3312792Sjacobs 3322792Sjacobs # enumerate LP configured printers after modification 3333478Sjacobs POST=/tmp/lpadmin-post.$$ 3343478Sjacobs (/bin/ls /etc/lp/printers 2>/dev/null ; /bin/ls /etc/lp/classes \ 3353478Sjacobs 2>/dev/null) >${POST} 3362792Sjacobs 3372792Sjacobs # if there are no destinations, disable the service(s) 3383478Sjacobs [[ ! -s "${POST}" ]] && lp_config_service disable 3392792Sjacobs 3402792Sjacobs # sync printers.conf with LP configuration 3412792Sjacobs lp_config_sync_pconf "${PRE}" "${POST}" 3423478Sjacobs 3433478Sjacobs /bin/rm -f ${PRE} ${POST} 3442792Sjacobsfi 3452792Sjacobs 3462792Sjacobs# Do any printers.conf configuration that is required 3472792Sjacobsif [[ -n "${delete}" ]] ; then 3482792Sjacobs if [[ "${delete}" = "all" ]] ; then 3492792Sjacobs [[ $exit_code -eq 0 ]] && delete_all 3503125Sjacobs elif [[ -z "${local}" ]] ; then 3512792Sjacobs ${LPSET} -n system -x ${delete} 3522792Sjacobs exit_code=$? 3532792Sjacobs fi 3543125Sjacobselif [[ -z "${local}" ]] ; then 3553710Sjacobs # if we need a uri, find the "best" one. 3563710Sjacobs if [[ -z "${uri}" ]] ; then 3573710Sjacobs uri="ipp://${server}/printers/${rem_printer}" 3583710Sjacobs ${LPSTAT} -p ${uri} >/dev/null 2>&1 3593710Sjacobs if [[ $? -ne 0 ]] ; then 3603710Sjacobs uri="lpd://${server}/printers/${rem_printer}#Solaris" 3613710Sjacobs fi 3623710Sjacobs fi 3633710Sjacobs # set the bsdaddr 3643710Sjacobs bsdaddr="${server},${rem_printer},Solaris" 3653710Sjacobs 3662792Sjacobs if [[ -n "${printer}" ]] ; then 3672792Sjacobs ${LPSET} -n system \ 3682792Sjacobs -a "printer-uri-supported=${uri}" \ 3692792Sjacobs -a "bsdaddr=${bsdaddr}" ${printer} 3700Sstevel@tonic-gate exit_code=$? 3710Sstevel@tonic-gate fi 3720Sstevel@tonic-gate 3732792Sjacobs if [[ -n "${printer}" && -n "${description}" ]] ; then 3742792Sjacobs ${LPSET} -n system \ 3752792Sjacobs -a "description=${description}" ${printer} 3762792Sjacobs exit_code=$? 3770Sstevel@tonic-gate fi 3780Sstevel@tonic-gatefi 3790Sstevel@tonic-gate 3802792Sjacobs# if the "default" doesn't resolve a "bsdaddr", the printer is gone, remove it 3812792Sjacobs${LPGET} -n system -k bsdaddr _default >/dev/null 2>&1 || 3822792Sjacobs ${LPSET} -n system -x _default >/dev/null 2>&1 3830Sstevel@tonic-gate 3840Sstevel@tonic-gateexit $exit_code 385