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# 23*3478Sjacobs# 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# 28*3478Sjacobsset -o noclobber 29*3478Sjacobs 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 39*3478SjacobsCOMM=/usr/bin/comm 400Sstevel@tonic-gate 412792SjacobsHOST=$(/bin/uname -n) 420Sstevel@tonic-gateexit_code=0 430Sstevel@tonic-gate 440Sstevel@tonic-gateusage() { 450Sstevel@tonic-gate gettext "Usage:\n" 1>&2 460Sstevel@tonic-gate gettext " lpadmin -p (printer) (options)\n" 1>&2 470Sstevel@tonic-gate gettext " lpadmin -x (dest)\n" 1>&2 480Sstevel@tonic-gate gettext " lpadmin -d (dest)\n" 1>&2 490Sstevel@tonic-gate gettext " lpadmin -S print-wheel -A alert-type [ -W minutes ]\n" 1>&2 500Sstevel@tonic-gate gettext " [ -Q requests ]\n" 1>&2 510Sstevel@tonic-gate gettext " lpadmin -M -f form-name [ -a [ -o filebreak ]\n" 1>&2 520Sstevel@tonic-gate gettext " [ -t tray-number ]]\n" 1>&2 530Sstevel@tonic-gate exit 1 540Sstevel@tonic-gate} 550Sstevel@tonic-gate 562792Sjacobs# create a filter table for LP service 572792Sjacobslp_config_filters() { 582792Sjacobs if [[ ! -f /etc/lp/filter.table ]] ; then 592792Sjacobs cd /etc/lp/fd ; for filter in *.fd ; do 602792Sjacobs /usr/sbin/lpfilter \ 612792Sjacobs -f $(/usr/bin/basename $filter .fd) \ 622792Sjacobs -F $filter 632792Sjacobs done 642792Sjacobs fi 652792Sjacobs} 660Sstevel@tonic-gate 672792Sjacobs# enable/disable LP related service(s) 682792Sjacobslp_config_service() { # (enable | disable) 692792Sjacobs svcadm ${1} -s svc:/application/print/server:default 702792Sjacobs # svcadm ${1} -s svc:/application/print/rfc1179:default 712792Sjacobs # svcadm ${1} -s svc:/application/print/ipp-listener:default 722792Sjacobs} 732792Sjacobs 742792Sjacobs# synchronize printers.conf with LP configuration changes 752792Sjacobslp_config_sync_pconf() { # (pre) (post) 76*3478Sjacobs ADDED=$(${COMM} -13 ${1} ${2}) 77*3478Sjacobs REMOVED=$(${COMM} -23 ${1} ${2}) 780Sstevel@tonic-gate 79*3478Sjacobs lp_server=${server:-${HOST}} 80*3478Sjacobs for DEST in ${ADDED} ; do 81*3478Sjacobs lp_uri="ipp://${lp_server}/printers/${DEST}" 82*3478Sjacobs lp_bsdaddr="${lp_server},${DEST},Solaris" 83*3478Sjacobs ${LPSET} -n system \ 84*3478Sjacobs -a "printer-uri-supported=${lp_uri}" \ 85*3478Sjacobs -a "bsdaddr=${lp_bsdaddr}" \ 86*3478Sjacobs ${DEST} 2>/dev/null 87*3478Sjacobs done 880Sstevel@tonic-gate 89*3478Sjacobs for DEST in ${REMOVED} ; do 90*3478Sjacobs ${LPSET} -n system -x ${DEST} 2>/dev/null 91*3478Sjacobs done 922792Sjacobs} 932792Sjacobs 942792Sjacobs# Delete all destinations in printers.conf 952792Sjacobsdelete_all() { 962792Sjacobs for DEST in $(lpget -n system list | egrep -e '.+:$' | sed -e 's/://') 972792Sjacobs do 982792Sjacobs ${LPSET} -n system -x ${DEST} 992792Sjacobs status=$? 1002792Sjacobs done 1012792Sjacobs} 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate# 1042792Sjacobs# Execution begins here 1050Sstevel@tonic-gate# 1060Sstevel@tonic-gate 1072792Sjacobs# be sure that we can run lpset and lpget 1082792Sjacobsif [[ ! -x ${LPSET} || ! -x ${LPGET} ]] ; then 1092792Sjacobs gettext "lpadmin: System error; cannot set default printer\n" 1>&2 1102792Sjacobs exit 2 1110Sstevel@tonic-gatefi 1120Sstevel@tonic-gate 1132792Sjacobsif [[ $# -lt 1 ]] ; then 1140Sstevel@tonic-gate usage 1150Sstevel@tonic-gate exit 1 1160Sstevel@tonic-gatefi 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate# Deal with the -d option independently since getopts does not handle 1190Sstevel@tonic-gate# options that may or may not have arguments 1200Sstevel@tonic-gate# 1212792Sjacobsif [[ ${1} = "-d" ]] ; then 1222792Sjacobs if [[ $# -eq 1 ]] ; then # remove the "default" 1232792Sjacobs ${LPGET} -n system _default >/dev/null 2>&1 1242792Sjacobs exit_code=$? 1250Sstevel@tonic-gate 1262792Sjacobs if [[ ${exit_code} -eq 0 ]] ; then 1272792Sjacobs ${LPSET} -n system -x _default 1282792Sjacobs exit_code=$? 1292792Sjacobs else # no default, nothing to do 1302792Sjacobs exit_code=0 1312792Sjacobs fi 1322792Sjacobs elif [[ $# -eq 2 ]] ; then # add/change the "default" 1332792Sjacobs ${LPGET} -k bsdaddr ${2} >/dev/null 2>&1 1342792Sjacobs exit_code=$? 1352792Sjacobs 1362792Sjacobs if [[ $exit_code -eq 0 ]] ; then 1372792Sjacobs ${LPSET} -n system -a "use=${2}" _default 1382792Sjacobs exit_code=$? 1392792Sjacobs else # can't set default to an unconfigured printer 1402792Sjacobs gettext "${2}: undefined printer\n" 1>&1 1412792Sjacobs fi 1422792Sjacobs else # invalid usage 1430Sstevel@tonic-gate usage 1440Sstevel@tonic-gate exit 1 1450Sstevel@tonic-gate fi 1460Sstevel@tonic-gate 1470Sstevel@tonic-gate exit ${exit_code} 1480Sstevel@tonic-gatefi 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate# Strip off legal options 1510Sstevel@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 1520Sstevel@tonic-gatedo 1530Sstevel@tonic-gate case $arg in 1540Sstevel@tonic-gate D) 1550Sstevel@tonic-gate description="${OPTARG}" 1560Sstevel@tonic-gate ;; 1570Sstevel@tonic-gate p) 1582792Sjacobs if [[ -n "${delete}" ]] ; then 1590Sstevel@tonic-gate usage 1600Sstevel@tonic-gate fi 1610Sstevel@tonic-gate printer=${OPTARG} 1620Sstevel@tonic-gate ;; 1630Sstevel@tonic-gate s) 1640Sstevel@tonic-gate server=${OPTARG} 1650Sstevel@tonic-gate ;; 1660Sstevel@tonic-gate v|U) 1670Sstevel@tonic-gate device=${OPTARG} 1682792Sjacobs if [[ ! -n "${server}" ]] ; then 169831Swendyp server=${HOST} 170831Swendyp fi 1713125Sjacobs local="true" 1720Sstevel@tonic-gate ;; 1730Sstevel@tonic-gate x) 1742792Sjacobs if [[ -n "${printer}" || -n "${server}" || \ 1752792Sjacobs -n "${device}" || -n "${description}" ]] ; then 1760Sstevel@tonic-gate usage 1770Sstevel@tonic-gate fi 1780Sstevel@tonic-gate delete=${OPTARG} 1790Sstevel@tonic-gate printer=${OPTARG} 1802792Sjacobs if [[ ${printer} = "all" ]] ; then 1810Sstevel@tonic-gate local="true" 1820Sstevel@tonic-gate fi 1830Sstevel@tonic-gate ;; 1840Sstevel@tonic-gate S|M|A) 1850Sstevel@tonic-gate local="true" 1860Sstevel@tonic-gate ;; 1870Sstevel@tonic-gate c) 1880Sstevel@tonic-gate class=${OPTARG} 1890Sstevel@tonic-gate local="true" 1902792Sjacobs if [[ ! -f ${LPGET} ]] ; then 1910Sstevel@tonic-gate gettext "lpadmin: System error; cannot set class\n " 1>&2 1920Sstevel@tonic-gate exit 2 1930Sstevel@tonic-gate fi 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate ${LPGET} "${class}" > /dev/null 2>&1 1960Sstevel@tonic-gate lpget_class=$? 1972792Sjacobs if [[ ${lpget_class} -eq 0 && ! -r /etc/lp/classes/"${class}" ]] ; then 1980Sstevel@tonic-gate gettext "lpadmin: ERROR: Can't create class ${class}.\n" 1>&2 1990Sstevel@tonic-gate gettext " TO FIX: This is an existing printer name;\n" 1>&2 2000Sstevel@tonic-gate gettext " choose another name.\n" 1>&2 2010Sstevel@tonic-gate exit 1 2020Sstevel@tonic-gate fi 2030Sstevel@tonic-gate ;; 2040Sstevel@tonic-gate r) 2050Sstevel@tonic-gate local="true" 2060Sstevel@tonic-gate ;; 2070Sstevel@tonic-gate esac 2080Sstevel@tonic-gatedone 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate# 2110Sstevel@tonic-gate# We don't have anything to do; let user know and bail 2120Sstevel@tonic-gate# 2132792Sjacobsif [[ ! -n "${printer}" && ! -n "${delete}" && ! -n "${local}" ]] ; then 2140Sstevel@tonic-gate gettext "lpadmin: ERROR: Nothing to do.\n" 1>&2 2150Sstevel@tonic-gate gettext " TO FIX: You must give one of these options:\n" 1>&2 2160Sstevel@tonic-gate gettext " -p, -d, -x -S\n" 1>&2 2170Sstevel@tonic-gate exit 1 2180Sstevel@tonic-gatefi 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate# 2210Sstevel@tonic-gate# Printer does not exist 2220Sstevel@tonic-gate# To be consistent with 2.5, assume adding local printer 2230Sstevel@tonic-gate# 2242792Sjacobsif [[ ! -n "${device}" && ! -n "${server}" && ! -n "${delete}" && \ 2252792Sjacobs ! -n "${local}" ]] ; then 2260Sstevel@tonic-gate ${LPGET} "${printer}" > /dev/null 2>&1 2270Sstevel@tonic-gate lpget_stat=$? 2282792Sjacobs if [[ ${lpget_stat} -ne 0 ]] ; then 2290Sstevel@tonic-gate gettext "lpadmin: ERROR: Missing -U or -v option.\n" 1>&2 2300Sstevel@tonic-gate gettext " TO FIX: Local printers must have\n" 1>&2 2310Sstevel@tonic-gate gettext " a port defined (-v option) or\n" 1>&2 2320Sstevel@tonic-gate gettext " have dial-out instructions (-U option).\n" 1>&2 2330Sstevel@tonic-gate exit 1 2340Sstevel@tonic-gate fi 2350Sstevel@tonic-gatefi 2360Sstevel@tonic-gate 2372264Sjacobs# process the "server" value 2382264Sjacobs# It can be a hostname, UUCP form (server!queue), RCMD form(queue@server), 2392264Sjacobs# or in URI form ({scheme}://{endpoint}) 2402264Sjacobs# 2412264Sjacobscase "${server}" in 2422264Sjacobs *://*) # URI form 2432264Sjacobs uri=${server} 2442792Sjacobs rem_printer=$(expr "${server}" : ".*://.*/\([^/]*\)") 2452792Sjacobs server=$(expr "${server}" : ".*://\([^/]*\)/.*") 2462264Sjacobs ;; 2472264Sjacobs *@*) # RCMD form 2482792Sjacobs rem_printer=$(expr "${server}" : "\(.*\)@.*") 2492792Sjacobs server=$(expr "${server}" : ".*@\(.*\)") 2502264Sjacobs ;; 2512264Sjacobs *!*) # UUCP form 2522792Sjacobs rem_printer=$(expr "${server}" : ".*!\(.*\)") 2532792Sjacobs server=$(expr "${server}" : "\(.*\)!.*") 2542264Sjacobs ;; 2552264Sjacobs *) # hostname 2562264Sjacobs rem_printer=${printer} 2572264Sjacobs ;; 2582264Sjacobsesac 2592792Sjacobs 2602792Sjacobsif [[ -n "${server}" ]] ; then 2613125Sjacobs # if we need a uri, find the "best" one. 2623125Sjacobs if [[ -z "${uri}" ]] ; then 2633125Sjacobs uri="ipp://${server}/printers/${rem_printer}" 2643125Sjacobs ${LPSTAT} -p ${uri} >/dev/null 2>&1 2653125Sjacobs if [[ $? -ne 0 ]] ; then 2663125Sjacobs uri="lpd://${server}/printers/${rem_printer}#Solaris" 2673125Sjacobs fi 2683125Sjacobs fi 2693125Sjacobs # set the bsdaddr 2702264Sjacobs bsdaddr="${server},${rem_printer},Solaris" 2710Sstevel@tonic-gatefi 2720Sstevel@tonic-gate 2733125Sjacobs# if there is a "device" or LP configuration, it's local 2743125Sjacobsif [[ -n "${device}" || -f /etc/lp/printers/${printer}/configuration || \ 2753125Sjacobs -f /etc/lp/classes/${printer} ]] ; then 2763125Sjacobs local="true" 2773125Sjacobsfi 2782792Sjacobs 2792792Sjacobs# Do the LP configuration for a local printer served by lpsched 2803125Sjacobsif [[ -x ${LPADMIN} && -n "${local}" ]] ; then 2812792Sjacobs # enumerate LP configured printers before modification 282*3478Sjacobs PRE=/tmp/lpadmin-pre.$$ 283*3478Sjacobs (/bin/ls /etc/lp/printers 2>/dev/null ; /bin/ls /etc/lp/classes \ 284*3478Sjacobs 2>/dev/null) >${PRE} 2852792Sjacobs 2862792Sjacobs # if there are no printers configured, enable LP service(s) 287*3478Sjacobs [[ -s "${PRE}" ]] && lp_config_service enable 2882792Sjacobs 2892792Sjacobs # add filters to LP service 2902792Sjacobs lp_config_filters 2912792Sjacobs 2922792Sjacobs # modify LP destination(s) 2932792Sjacobs CMD=${LPADMIN} 2942792Sjacobs while [[ -n "$*" ]] ; do # to deal with multi-word arguments 2952792Sjacobs CMD="$CMD \"$1\"" 2962792Sjacobs shift 2972792Sjacobs done 2982792Sjacobs case "$CMD" in 2992792Sjacobs *\"-D\") 3002792Sjacobs CMD="$CMD \"\"" 3012792Sjacobs ;; 3022792Sjacobs esac 3030Sstevel@tonic-gate 3042792Sjacobs # execute the LP lpadmin command 3052792Sjacobs eval $CMD 3062792Sjacobs exit_code=$? 3072792Sjacobs 3082792Sjacobs # enumerate LP configured printers after modification 309*3478Sjacobs POST=/tmp/lpadmin-post.$$ 310*3478Sjacobs (/bin/ls /etc/lp/printers 2>/dev/null ; /bin/ls /etc/lp/classes \ 311*3478Sjacobs 2>/dev/null) >${POST} 3122792Sjacobs 3132792Sjacobs # if there are no destinations, disable the service(s) 314*3478Sjacobs [[ ! -s "${POST}" ]] && lp_config_service disable 3152792Sjacobs 3162792Sjacobs # sync printers.conf with LP configuration 3172792Sjacobs lp_config_sync_pconf "${PRE}" "${POST}" 318*3478Sjacobs 319*3478Sjacobs /bin/rm -f ${PRE} ${POST} 3202792Sjacobsfi 3212792Sjacobs 3222792Sjacobs# Do any printers.conf configuration that is required 3232792Sjacobsif [[ -n "${delete}" ]] ; then 3242792Sjacobs if [[ "${delete}" = "all" ]] ; then 3252792Sjacobs [[ $exit_code -eq 0 ]] && delete_all 3263125Sjacobs elif [[ -z "${local}" ]] ; then 3272792Sjacobs ${LPSET} -n system -x ${delete} 3282792Sjacobs exit_code=$? 3292792Sjacobs fi 3303125Sjacobselif [[ -z "${local}" ]] ; then 3312792Sjacobs if [[ -n "${printer}" ]] ; then 3322792Sjacobs ${LPSET} -n system \ 3332792Sjacobs -a "printer-uri-supported=${uri}" \ 3342792Sjacobs -a "bsdaddr=${bsdaddr}" ${printer} 3350Sstevel@tonic-gate exit_code=$? 3360Sstevel@tonic-gate fi 3370Sstevel@tonic-gate 3382792Sjacobs if [[ -n "${printer}" && -n "${description}" ]] ; then 3392792Sjacobs ${LPSET} -n system \ 3402792Sjacobs -a "description=${description}" ${printer} 3412792Sjacobs exit_code=$? 3420Sstevel@tonic-gate fi 3430Sstevel@tonic-gatefi 3440Sstevel@tonic-gate 3452792Sjacobs# if the "default" doesn't resolve a "bsdaddr", the printer is gone, remove it 3462792Sjacobs${LPGET} -n system -k bsdaddr _default >/dev/null 2>&1 || 3472792Sjacobs ${LPSET} -n system -x _default >/dev/null 2>&1 3480Sstevel@tonic-gate 3490Sstevel@tonic-gateexit $exit_code 350