1*b51e021dSjacobs#!/bin/ksh 27c478bd9Sstevel@tonic-gate# 37c478bd9Sstevel@tonic-gate# CDDL HEADER START 47c478bd9Sstevel@tonic-gate# 57c478bd9Sstevel@tonic-gate# The contents of this file are subject to the terms of the 6355b4669Sjacobs# Common Development and Distribution License (the "License"). 7355b4669Sjacobs# You may not use this file except in compliance with the License. 87c478bd9Sstevel@tonic-gate# 97c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate# and limitations under the License. 137c478bd9Sstevel@tonic-gate# 147c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate# 207c478bd9Sstevel@tonic-gate# CDDL HEADER END 217c478bd9Sstevel@tonic-gate# 227c478bd9Sstevel@tonic-gate# 23355b4669Sjacobs# Copyright 2006 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate# Use is subject to license terms. 257c478bd9Sstevel@tonic-gate# 267c478bd9Sstevel@tonic-gate# ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate# 287c478bd9Sstevel@tonic-gatePATH=/bin:/usr/bin:/usr/sbin export PATH 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gateTEXTDOMAIN="SUNW_OST_OSCMD" 317c478bd9Sstevel@tonic-gateexport TEXTDOMAIN 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gateLPSET=/usr/bin/lpset 347c478bd9Sstevel@tonic-gateLPGET=/usr/bin/lpget 35*b51e021dSjacobsLPADMIN=/usr/lib/lp/local/lpadmin 367c478bd9Sstevel@tonic-gate 37*b51e021dSjacobsHOST=$(/bin/uname -n) 387c478bd9Sstevel@tonic-gateexit_code=0 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gateusage() { 417c478bd9Sstevel@tonic-gate gettext "Usage:\n" 1>&2 427c478bd9Sstevel@tonic-gate gettext " lpadmin -p (printer) (options)\n" 1>&2 437c478bd9Sstevel@tonic-gate gettext " lpadmin -x (dest)\n" 1>&2 447c478bd9Sstevel@tonic-gate gettext " lpadmin -d (dest)\n" 1>&2 457c478bd9Sstevel@tonic-gate gettext " lpadmin -S print-wheel -A alert-type [ -W minutes ]\n" 1>&2 467c478bd9Sstevel@tonic-gate gettext " [ -Q requests ]\n" 1>&2 477c478bd9Sstevel@tonic-gate gettext " lpadmin -M -f form-name [ -a [ -o filebreak ]\n" 1>&2 487c478bd9Sstevel@tonic-gate gettext " [ -t tray-number ]]\n" 1>&2 497c478bd9Sstevel@tonic-gate exit 1 507c478bd9Sstevel@tonic-gate} 517c478bd9Sstevel@tonic-gate 52*b51e021dSjacobs# echo names in ${1} that are not in ${2} 53*b51e021dSjacobsmissing() { 54*b51e021dSjacobs for i in ${1} ; do 55*b51e021dSjacobs MATCHED=0 56*b51e021dSjacobs for j in ${2} ; do 57*b51e021dSjacobs if [[ $i = $j ]] ; then 58*b51e021dSjacobs MATCHED=1 597c478bd9Sstevel@tonic-gate fi 607c478bd9Sstevel@tonic-gate done 61*b51e021dSjacobs if [[ $MATCHED == 0 ]] ; then 62*b51e021dSjacobs echo $i 637c478bd9Sstevel@tonic-gate fi 647c478bd9Sstevel@tonic-gate done 657c478bd9Sstevel@tonic-gate} 667c478bd9Sstevel@tonic-gate 67*b51e021dSjacobs# create a filter table for LP service 68*b51e021dSjacobslp_config_filters() { 69*b51e021dSjacobs if [[ ! -f /etc/lp/filter.table ]] ; then 70*b51e021dSjacobs cd /etc/lp/fd ; for filter in *.fd ; do 71*b51e021dSjacobs /usr/sbin/lpfilter \ 72*b51e021dSjacobs -f $(/usr/bin/basename $filter .fd) \ 73*b51e021dSjacobs -F $filter 74*b51e021dSjacobs done 75*b51e021dSjacobs fi 76*b51e021dSjacobs} 77*b51e021dSjacobs 78*b51e021dSjacobs# enable/disable LP related service(s) 79*b51e021dSjacobslp_config_service() { # (enable | disable) 80*b51e021dSjacobs svcadm ${1} -s svc:/application/print/server:default 81*b51e021dSjacobs # svcadm ${1} -s svc:/application/print/rfc1179:default 82*b51e021dSjacobs # svcadm ${1} -s svc:/application/print/ipp-listener:default 83*b51e021dSjacobs} 84*b51e021dSjacobs 85*b51e021dSjacobs# synchronize printers.conf with LP configuration changes 86*b51e021dSjacobslp_config_sync_pconf() { # (pre) (post) 87*b51e021dSjacobs if [[ "${1}" != "${2}" ]] ; then 88*b51e021dSjacobs ADDED=$(missing "${2}" "${1}") 89*b51e021dSjacobs REMOVED=$(missing "${1}" "${2}") 90*b51e021dSjacobs 91*b51e021dSjacobs lp_server=${server:-${HOST}} 92*b51e021dSjacobs for DEST in ${ADDED} ; do 93*b51e021dSjacobs lp_uri="ipp://${lp_server}/printers/${DEST}" 94*b51e021dSjacobs lp_bsdaddr="${lp_server},${DEST},Solaris" 95*b51e021dSjacobs ${LPSET} -n system \ 96*b51e021dSjacobs -a "printer-uri-supported=${lp_uri}" \ 97*b51e021dSjacobs -a "bsdaddr=${lp_bsdaddr}" \ 98*b51e021dSjacobs ${DEST} 2>/dev/null 99*b51e021dSjacobs done 100*b51e021dSjacobs 101*b51e021dSjacobs for DEST in ${REMOVED} ; do 102*b51e021dSjacobs ${LPSET} -n system -x ${DEST} 2>/dev/null 103*b51e021dSjacobs done 104*b51e021dSjacobs fi 105*b51e021dSjacobs} 106*b51e021dSjacobs 107*b51e021dSjacobs# Delete all destinations in printers.conf 108*b51e021dSjacobsdelete_all() { 109*b51e021dSjacobs for DEST in $(lpget -n system list | egrep -e '.+:$' | sed -e 's/://') 110*b51e021dSjacobs do 111*b51e021dSjacobs ${LPSET} -n system -x ${DEST} 112*b51e021dSjacobs status=$? 113*b51e021dSjacobs done 114*b51e021dSjacobs} 115*b51e021dSjacobs 116*b51e021dSjacobs# 117*b51e021dSjacobs# Execution begins here 118*b51e021dSjacobs# 119*b51e021dSjacobs 120*b51e021dSjacobs# be sure that we can run lpset and lpget 121*b51e021dSjacobsif [[ ! -x ${LPSET} || ! -x ${LPGET} ]] ; then 122*b51e021dSjacobs gettext "lpadmin: System error; cannot set default printer\n" 1>&2 123*b51e021dSjacobs exit 2 124*b51e021dSjacobsfi 125*b51e021dSjacobs 126*b51e021dSjacobsif [[ $# -lt 1 ]] ; then 1277c478bd9Sstevel@tonic-gate usage 1287c478bd9Sstevel@tonic-gate exit 1 1297c478bd9Sstevel@tonic-gatefi 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate# Deal with the -d option independently since getopts does not handle 1327c478bd9Sstevel@tonic-gate# options that may or may not have arguments 1337c478bd9Sstevel@tonic-gate# 134*b51e021dSjacobsif [[ ${1} = "-d" ]] ; then 135*b51e021dSjacobs if [[ $# -eq 1 ]] ; then # remove the "default" 136*b51e021dSjacobs ${LPGET} -n system _default >/dev/null 2>&1 137*b51e021dSjacobs exit_code=$? 1387c478bd9Sstevel@tonic-gate 139*b51e021dSjacobs if [[ ${exit_code} -eq 0 ]] ; then 140*b51e021dSjacobs ${LPSET} -n system -x _default 141*b51e021dSjacobs exit_code=$? 142*b51e021dSjacobs else # no default, nothing to do 143*b51e021dSjacobs exit_code=0 144*b51e021dSjacobs fi 145*b51e021dSjacobs elif [[ $# -eq 2 ]] ; then # add/change the "default" 146*b51e021dSjacobs ${LPGET} -k bsdaddr ${2} >/dev/null 2>&1 147*b51e021dSjacobs exit_code=$? 148*b51e021dSjacobs 149*b51e021dSjacobs if [[ $exit_code -eq 0 ]] ; then 150*b51e021dSjacobs ${LPSET} -n system -a "use=${2}" _default 151*b51e021dSjacobs exit_code=$? 152*b51e021dSjacobs else # can't set default to an unconfigured printer 153*b51e021dSjacobs gettext "${2}: undefined printer\n" 1>&1 154*b51e021dSjacobs fi 155*b51e021dSjacobs else # invalid usage 1567c478bd9Sstevel@tonic-gate usage 1577c478bd9Sstevel@tonic-gate exit 1 1587c478bd9Sstevel@tonic-gate fi 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate exit ${exit_code} 1617c478bd9Sstevel@tonic-gatefi 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate# Strip off legal options 1647c478bd9Sstevel@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 1657c478bd9Sstevel@tonic-gatedo 1667c478bd9Sstevel@tonic-gate case $arg in 1677c478bd9Sstevel@tonic-gate D) 1687c478bd9Sstevel@tonic-gate description="${OPTARG}" 1697c478bd9Sstevel@tonic-gate ;; 1707c478bd9Sstevel@tonic-gate p) 171*b51e021dSjacobs if [[ -n "${delete}" ]] ; then 1727c478bd9Sstevel@tonic-gate usage 1737c478bd9Sstevel@tonic-gate fi 1747c478bd9Sstevel@tonic-gate printer=${OPTARG} 1757c478bd9Sstevel@tonic-gate ;; 1767c478bd9Sstevel@tonic-gate s) 1777c478bd9Sstevel@tonic-gate server=${OPTARG} 1787c478bd9Sstevel@tonic-gate ;; 1797c478bd9Sstevel@tonic-gate v|U) 1807c478bd9Sstevel@tonic-gate device=${OPTARG} 181*b51e021dSjacobs if [[ ! -n "${server}" ]] ; then 1825c88ba20Swendyp server=${HOST} 1835c88ba20Swendyp fi 1847c478bd9Sstevel@tonic-gate ;; 1857c478bd9Sstevel@tonic-gate x) 186*b51e021dSjacobs if [[ -n "${printer}" || -n "${server}" || \ 187*b51e021dSjacobs -n "${device}" || -n "${description}" ]] ; then 1887c478bd9Sstevel@tonic-gate usage 1897c478bd9Sstevel@tonic-gate fi 1907c478bd9Sstevel@tonic-gate delete=${OPTARG} 1917c478bd9Sstevel@tonic-gate printer=${OPTARG} 192*b51e021dSjacobs if [[ ${printer} = "all" ]] ; then 1937c478bd9Sstevel@tonic-gate local="true" 1947c478bd9Sstevel@tonic-gate fi 1957c478bd9Sstevel@tonic-gate ;; 1967c478bd9Sstevel@tonic-gate S|M|A) 1977c478bd9Sstevel@tonic-gate local="true" 1987c478bd9Sstevel@tonic-gate ;; 1997c478bd9Sstevel@tonic-gate c) 2007c478bd9Sstevel@tonic-gate class=${OPTARG} 2017c478bd9Sstevel@tonic-gate local="true" 202*b51e021dSjacobs if [[ ! -f ${LPGET} ]] ; then 2037c478bd9Sstevel@tonic-gate gettext "lpadmin: System error; cannot set class\n " 1>&2 2047c478bd9Sstevel@tonic-gate exit 2 2057c478bd9Sstevel@tonic-gate fi 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate ${LPGET} "${class}" > /dev/null 2>&1 2087c478bd9Sstevel@tonic-gate lpget_class=$? 209*b51e021dSjacobs if [[ ${lpget_class} -eq 0 && ! -r /etc/lp/classes/"${class}" ]] ; then 2107c478bd9Sstevel@tonic-gate gettext "lpadmin: ERROR: Can't create class ${class}.\n" 1>&2 2117c478bd9Sstevel@tonic-gate gettext " TO FIX: This is an existing printer name;\n" 1>&2 2127c478bd9Sstevel@tonic-gate gettext " choose another name.\n" 1>&2 2137c478bd9Sstevel@tonic-gate exit 1 2147c478bd9Sstevel@tonic-gate fi 2157c478bd9Sstevel@tonic-gate ;; 2167c478bd9Sstevel@tonic-gate r) 2177c478bd9Sstevel@tonic-gate local="true" 2187c478bd9Sstevel@tonic-gate ;; 2197c478bd9Sstevel@tonic-gate esac 2207c478bd9Sstevel@tonic-gatedone 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate# 2237c478bd9Sstevel@tonic-gate# We don't have anything to do; let user know and bail 2247c478bd9Sstevel@tonic-gate# 225*b51e021dSjacobsif [[ ! -n "${printer}" && ! -n "${delete}" && ! -n "${local}" ]] ; then 2267c478bd9Sstevel@tonic-gate gettext "lpadmin: ERROR: Nothing to do.\n" 1>&2 2277c478bd9Sstevel@tonic-gate gettext " TO FIX: You must give one of these options:\n" 1>&2 2287c478bd9Sstevel@tonic-gate gettext " -p, -d, -x -S\n" 1>&2 2297c478bd9Sstevel@tonic-gate exit 1 2307c478bd9Sstevel@tonic-gatefi 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate# 2337c478bd9Sstevel@tonic-gate# Printer does not exist 2347c478bd9Sstevel@tonic-gate# To be consistent with 2.5, assume adding local printer 2357c478bd9Sstevel@tonic-gate# 236*b51e021dSjacobsif [[ ! -n "${device}" && ! -n "${server}" && ! -n "${delete}" && \ 237*b51e021dSjacobs ! -n "${local}" ]] ; then 2387c478bd9Sstevel@tonic-gate ${LPGET} "${printer}" > /dev/null 2>&1 2397c478bd9Sstevel@tonic-gate lpget_stat=$? 240*b51e021dSjacobs if [[ ${lpget_stat} -ne 0 ]] ; then 2417c478bd9Sstevel@tonic-gate gettext "lpadmin: ERROR: Missing -U or -v option.\n" 1>&2 2427c478bd9Sstevel@tonic-gate gettext " TO FIX: Local printers must have\n" 1>&2 2437c478bd9Sstevel@tonic-gate gettext " a port defined (-v option) or\n" 1>&2 2447c478bd9Sstevel@tonic-gate gettext " have dial-out instructions (-U option).\n" 1>&2 2457c478bd9Sstevel@tonic-gate exit 1 2467c478bd9Sstevel@tonic-gate fi 2477c478bd9Sstevel@tonic-gatefi 2487c478bd9Sstevel@tonic-gate 249355b4669Sjacobs# process the "server" value 250355b4669Sjacobs# It can be a hostname, UUCP form (server!queue), RCMD form(queue@server), 251355b4669Sjacobs# or in URI form ({scheme}://{endpoint}) 252355b4669Sjacobs# 253355b4669Sjacobscase "${server}" in 254355b4669Sjacobs *://*) # URI form 255355b4669Sjacobs uri=${server} 256*b51e021dSjacobs rem_printer=$(expr "${server}" : ".*://.*/\([^/]*\)") 257*b51e021dSjacobs server=$(expr "${server}" : ".*://\([^/]*\)/.*") 258355b4669Sjacobs ;; 259355b4669Sjacobs *@*) # RCMD form 260*b51e021dSjacobs rem_printer=$(expr "${server}" : "\(.*\)@.*") 261*b51e021dSjacobs server=$(expr "${server}" : ".*@\(.*\)") 262355b4669Sjacobs ;; 263355b4669Sjacobs *!*) # UUCP form 264*b51e021dSjacobs rem_printer=$(expr "${server}" : ".*!\(.*\)") 265*b51e021dSjacobs server=$(expr "${server}" : "\(.*\)!.*") 266355b4669Sjacobs ;; 267355b4669Sjacobs *) # hostname 2687c478bd9Sstevel@tonic-gate rem_printer=${printer} 269355b4669Sjacobs ;; 270355b4669Sjacobsesac 271*b51e021dSjacobs 272355b4669Sjacobs# default URI form is "lpd" form 273*b51e021dSjacobsif [[ -n "${server}" ]] ; then 274355b4669Sjacobs uri=${uri:-"lpd://${server}/printers/${rem_printer}#Solaris"} 275355b4669Sjacobs bsdaddr="${server},${rem_printer},Solaris" 2767c478bd9Sstevel@tonic-gatefi 2777c478bd9Sstevel@tonic-gate 278*b51e021dSjacobs 279*b51e021dSjacobs# Do the LP configuration for a local printer served by lpsched 280*b51e021dSjacobsif [[ -x ${LPADMIN} && ( -n "${local}" || -n "${device}" || \ 281*b51e021dSjacobs -f /etc/lp/printers/${printer}/configuration || \ 282*b51e021dSjacobs -f /etc/lp/classes/${printer} ) ]] ; then 283*b51e021dSjacobs # enumerate LP configured printers before modification 284*b51e021dSjacobs PRE=$(/bin/ls /etc/lp/printers 2>/dev/null ; /bin/ls /etc/lp/classes \ 285*b51e021dSjacobs 2>/dev/null) 286*b51e021dSjacobs 287*b51e021dSjacobs # if there are no printers configured, enable LP service(s) 288*b51e021dSjacobs [[ -z "${PRE}" ]] && lp_config_service enable 289*b51e021dSjacobs 290*b51e021dSjacobs # add filters to LP service 291*b51e021dSjacobs lp_config_filters 292*b51e021dSjacobs 293*b51e021dSjacobs # modify LP destination(s) 294*b51e021dSjacobs CMD=${LPADMIN} 295*b51e021dSjacobs while [[ -n "$*" ]] ; do # to deal with multi-word arguments 296*b51e021dSjacobs CMD="$CMD \"$1\"" 297*b51e021dSjacobs shift 298*b51e021dSjacobs done 299*b51e021dSjacobs case "$CMD" in 300*b51e021dSjacobs *\"-D\") 301*b51e021dSjacobs CMD="$CMD \"\"" 302*b51e021dSjacobs ;; 303*b51e021dSjacobs esac 304*b51e021dSjacobs 305*b51e021dSjacobs # execute the LP lpadmin command 306*b51e021dSjacobs eval $CMD 307*b51e021dSjacobs exit_code=$? 308*b51e021dSjacobs 309*b51e021dSjacobs # enumerate LP configured printers after modification 310*b51e021dSjacobs POST=$(/bin/ls /etc/lp/printers 2>/dev/null ; /bin/ls /etc/lp/classes \ 311*b51e021dSjacobs 2>/dev/null) 312*b51e021dSjacobs 313*b51e021dSjacobs # if there are no destinations, disable the service(s) 314*b51e021dSjacobs [[ -z "${POST}" ]] && lp_config_service disable 315*b51e021dSjacobs 316*b51e021dSjacobs # sync printers.conf with LP configuration 317*b51e021dSjacobs lp_config_sync_pconf "${PRE}" "${POST}" 318*b51e021dSjacobsfi 319*b51e021dSjacobs 320*b51e021dSjacobs# Do any printers.conf configuration that is required 321*b51e021dSjacobsif [[ -n "${delete}" ]] ; then 322*b51e021dSjacobs if [[ "${delete}" = "all" ]] ; then 323*b51e021dSjacobs [[ $exit_code -eq 0 ]] && delete_all 324*b51e021dSjacobs elif [[ -n "${local}" ]] ; then 3257c478bd9Sstevel@tonic-gate ${LPSET} -n system -x ${delete} 3267c478bd9Sstevel@tonic-gate exit_code=$? 3277c478bd9Sstevel@tonic-gate fi 328*b51e021dSjacobselif [[ -n "${local}" ]] ; then 329*b51e021dSjacobs if [[ -n "${printer}" ]] ; then 3307c478bd9Sstevel@tonic-gate ${LPSET} -n system \ 331355b4669Sjacobs -a "printer-uri-supported=${uri}" \ 332*b51e021dSjacobs -a "bsdaddr=${bsdaddr}" ${printer} 333*b51e021dSjacobs exit_code=$? 334*b51e021dSjacobs fi 335*b51e021dSjacobs 336*b51e021dSjacobs if [[ -n "${printer}" && -n "${description}" ]] ; then 337*b51e021dSjacobs ${LPSET} -n system \ 338*b51e021dSjacobs -a "description=${description}" ${printer} 3397c478bd9Sstevel@tonic-gate exit_code=$? 3407c478bd9Sstevel@tonic-gate fi 3417c478bd9Sstevel@tonic-gatefi 3427c478bd9Sstevel@tonic-gate 343*b51e021dSjacobs# if the "default" doesn't resolve a "bsdaddr", the printer is gone, remove it 344*b51e021dSjacobs${LPGET} -n system -k bsdaddr _default >/dev/null 2>&1 || 345*b51e021dSjacobs ${LPSET} -n system -x _default >/dev/null 2>&1 3467c478bd9Sstevel@tonic-gate 3477c478bd9Sstevel@tonic-gateexit $exit_code 348