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*8532SSumanth.Naropanth@Sun.COM# Copyright 2009 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate# Use is subject to license terms. 250Sstevel@tonic-gate# 260Sstevel@tonic-gate# 273478Sjacobs 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 333999SjacobsPFEXEC=/usr/bin/pfexec 340Sstevel@tonic-gateLPSET=/usr/bin/lpset 350Sstevel@tonic-gateLPGET=/usr/bin/lpget 363125SjacobsLPSTAT=/usr/bin/lpstat 372792SjacobsLPADMIN=/usr/lib/lp/local/lpadmin 383999SjacobsLPFILTER=/usr/sbin/lpfilter 393478SjacobsCOMM=/usr/bin/comm 403781SceasthaPPDMGR=/usr/sbin/ppdmgr 41*8532SSumanth.Naropanth@Sun.COMMKTEMP="/usr/bin/mktemp -t" 420Sstevel@tonic-gate 432792SjacobsHOST=$(/bin/uname -n) 440Sstevel@tonic-gateexit_code=0 450Sstevel@tonic-gate 460Sstevel@tonic-gateusage() { 470Sstevel@tonic-gate gettext "Usage:\n" 1>&2 480Sstevel@tonic-gate gettext " lpadmin -p (printer) (options)\n" 1>&2 490Sstevel@tonic-gate gettext " lpadmin -x (dest)\n" 1>&2 500Sstevel@tonic-gate gettext " lpadmin -d (dest)\n" 1>&2 510Sstevel@tonic-gate gettext " lpadmin -S print-wheel -A alert-type [ -W minutes ]\n" 1>&2 520Sstevel@tonic-gate gettext " [ -Q requests ]\n" 1>&2 530Sstevel@tonic-gate gettext " lpadmin -M -f form-name [ -a [ -o filebreak ]\n" 1>&2 540Sstevel@tonic-gate gettext " [ -t tray-number ]]\n" 1>&2 550Sstevel@tonic-gate exit 1 560Sstevel@tonic-gate} 570Sstevel@tonic-gate 582792Sjacobs# create a filter table for LP service 592792Sjacobslp_config_filters() { 602792Sjacobs if [[ ! -f /etc/lp/filter.table ]] ; then 612792Sjacobs cd /etc/lp/fd ; for filter in *.fd ; do 623999Sjacobs ${PFEXEC} ${LPFILTER} \ 632792Sjacobs -f $(/usr/bin/basename $filter .fd) \ 642792Sjacobs -F $filter 652792Sjacobs done 662792Sjacobs fi 672792Sjacobs} 680Sstevel@tonic-gate 692792Sjacobs# enable/disable LP related service(s) 702792Sjacobslp_config_service() { # (enable | disable) 712792Sjacobs svcadm ${1} -s svc:/application/print/server:default 722792Sjacobs # svcadm ${1} -s svc:/application/print/rfc1179:default 732792Sjacobs # svcadm ${1} -s svc:/application/print/ipp-listener:default 742792Sjacobs} 752792Sjacobs 762792Sjacobs# synchronize printers.conf with LP configuration changes 772792Sjacobslp_config_sync_pconf() { # (pre) (post) 783478Sjacobs ADDED=$(${COMM} -13 ${1} ${2}) 793478Sjacobs REMOVED=$(${COMM} -23 ${1} ${2}) 800Sstevel@tonic-gate 813478Sjacobs lp_server=${server:-${HOST}} 823478Sjacobs for DEST in ${ADDED} ; do 833478Sjacobs lp_uri="ipp://${lp_server}/printers/${DEST}" 843478Sjacobs lp_bsdaddr="${lp_server},${DEST},Solaris" 853478Sjacobs ${LPSET} -n system \ 863478Sjacobs -a "printer-uri-supported=${lp_uri}" \ 873478Sjacobs -a "bsdaddr=${lp_bsdaddr}" \ 883478Sjacobs ${DEST} 2>/dev/null 893478Sjacobs done 900Sstevel@tonic-gate 913478Sjacobs for DEST in ${REMOVED} ; do 923478Sjacobs ${LPSET} -n system -x ${DEST} 2>/dev/null 933478Sjacobs done 942792Sjacobs} 952792Sjacobs 962792Sjacobs# Delete all destinations in printers.conf 972792Sjacobsdelete_all() { 982792Sjacobs for DEST in $(lpget -n system list | egrep -e '.+:$' | sed -e 's/://') 992792Sjacobs do 1002792Sjacobs ${LPSET} -n system -x ${DEST} 1012792Sjacobs status=$? 1022792Sjacobs done 1032792Sjacobs} 1040Sstevel@tonic-gate 1053781Sceastha# Call the ppdmgr utility to add a new PPD file to the system. 1063781Sceastha# 1073781Sceastha# $1 - path to PPD file 1083781Sceastha# $2 - label name (optional) 1093781Sceasthaadd_new_ppd_file() { 1103781Sceastha # Add new ppd file and echo full path it was actually saved to 1113999Sjacobs ppdmgrcmd="${PFEXEC} ${PPDMGR} -a ${1} -w" 1123781Sceastha 113*8532SSumanth.Naropanth@Sun.COM ppderrfile=$(${MKTEMP} lpadminerror.XXXXXX) 114*8532SSumanth.Naropanth@Sun.COM if [[ -z "${ppderrfile}" ]] ; then 115*8532SSumanth.Naropanth@Sun.COM gettext "lpadmin: System error; cannot create temporary file\n" 1>&2 116*8532SSumanth.Naropanth@Sun.COM exit 2 117*8532SSumanth.Naropanth@Sun.COM fi 1183781Sceastha ppd_file=$(${ppdmgrcmd} 2>${ppderrfile}) 1193781Sceastha ppdmgrrc=$? 1203781Sceastha if [[ -s "${ppderrfile}" ]] ; then 1213781Sceastha print -n "lpadmin: " 1>&2 1223781Sceastha cat ${ppderrfile} 1>&2 1233781Sceastha rm -f ${ppderrfile} >/dev/null 2>&1 1243781Sceastha if [[ ${ppdmgrrc} -ne 0 ]] ; then 1253781Sceastha exit 1 1263781Sceastha fi 1273781Sceastha fi 1283781Sceastha rm -f ${ppderrfile} >/dev/null 2>&1 1293781Sceastha} 1303781Sceastha 1310Sstevel@tonic-gate# 1322792Sjacobs# Execution begins here 1330Sstevel@tonic-gate# 1340Sstevel@tonic-gate 1352792Sjacobs# be sure that we can run lpset and lpget 1362792Sjacobsif [[ ! -x ${LPSET} || ! -x ${LPGET} ]] ; then 1372792Sjacobs gettext "lpadmin: System error; cannot set default printer\n" 1>&2 1382792Sjacobs exit 2 1390Sstevel@tonic-gatefi 1400Sstevel@tonic-gate 1412792Sjacobsif [[ $# -lt 1 ]] ; then 1420Sstevel@tonic-gate usage 1430Sstevel@tonic-gate exit 1 1440Sstevel@tonic-gatefi 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate# Deal with the -d option independently since getopts does not handle 1470Sstevel@tonic-gate# options that may or may not have arguments 1480Sstevel@tonic-gate# 1492792Sjacobsif [[ ${1} = "-d" ]] ; then 1502792Sjacobs if [[ $# -eq 1 ]] ; then # remove the "default" 1512792Sjacobs ${LPGET} -n system _default >/dev/null 2>&1 1522792Sjacobs exit_code=$? 1530Sstevel@tonic-gate 1542792Sjacobs if [[ ${exit_code} -eq 0 ]] ; then 1552792Sjacobs ${LPSET} -n system -x _default 1562792Sjacobs exit_code=$? 1572792Sjacobs else # no default, nothing to do 1582792Sjacobs exit_code=0 1592792Sjacobs fi 1602792Sjacobs elif [[ $# -eq 2 ]] ; then # add/change the "default" 1612792Sjacobs ${LPGET} -k bsdaddr ${2} >/dev/null 2>&1 1622792Sjacobs exit_code=$? 1632792Sjacobs 1642792Sjacobs if [[ $exit_code -eq 0 ]] ; then 1652792Sjacobs ${LPSET} -n system -a "use=${2}" _default 1662792Sjacobs exit_code=$? 1672792Sjacobs else # can't set default to an unconfigured printer 1682792Sjacobs gettext "${2}: undefined printer\n" 1>&1 1692792Sjacobs fi 1702792Sjacobs else # invalid usage 1710Sstevel@tonic-gate usage 1720Sstevel@tonic-gate exit 1 1730Sstevel@tonic-gate fi 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate exit ${exit_code} 1760Sstevel@tonic-gatefi 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate# Strip off legal options 1790Sstevel@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 1800Sstevel@tonic-gatedo 1810Sstevel@tonic-gate case $arg in 1820Sstevel@tonic-gate D) 1830Sstevel@tonic-gate description="${OPTARG}" 1840Sstevel@tonic-gate ;; 1853781Sceastha n) 1863781Sceastha ppd_file="${OPTARG}" 1873781Sceastha ;; 1880Sstevel@tonic-gate p) 1892792Sjacobs if [[ -n "${delete}" ]] ; then 1900Sstevel@tonic-gate usage 1910Sstevel@tonic-gate fi 1920Sstevel@tonic-gate printer=${OPTARG} 1930Sstevel@tonic-gate ;; 1940Sstevel@tonic-gate s) 1950Sstevel@tonic-gate server=${OPTARG} 1960Sstevel@tonic-gate ;; 1970Sstevel@tonic-gate v|U) 1980Sstevel@tonic-gate device=${OPTARG} 1992792Sjacobs if [[ ! -n "${server}" ]] ; then 200831Swendyp server=${HOST} 201831Swendyp fi 2023125Sjacobs local="true" 2030Sstevel@tonic-gate ;; 2040Sstevel@tonic-gate x) 2052792Sjacobs if [[ -n "${printer}" || -n "${server}" || \ 2062792Sjacobs -n "${device}" || -n "${description}" ]] ; then 2070Sstevel@tonic-gate usage 2080Sstevel@tonic-gate fi 2090Sstevel@tonic-gate delete=${OPTARG} 2100Sstevel@tonic-gate printer=${OPTARG} 2112792Sjacobs if [[ ${printer} = "all" ]] ; then 2120Sstevel@tonic-gate local="true" 2130Sstevel@tonic-gate fi 2140Sstevel@tonic-gate ;; 2150Sstevel@tonic-gate S|M|A) 2160Sstevel@tonic-gate local="true" 2170Sstevel@tonic-gate ;; 2180Sstevel@tonic-gate c) 2190Sstevel@tonic-gate class=${OPTARG} 2200Sstevel@tonic-gate local="true" 2212792Sjacobs if [[ ! -f ${LPGET} ]] ; then 2220Sstevel@tonic-gate gettext "lpadmin: System error; cannot set class\n " 1>&2 2230Sstevel@tonic-gate exit 2 2240Sstevel@tonic-gate fi 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate ${LPGET} "${class}" > /dev/null 2>&1 2270Sstevel@tonic-gate lpget_class=$? 2282792Sjacobs if [[ ${lpget_class} -eq 0 && ! -r /etc/lp/classes/"${class}" ]] ; then 2290Sstevel@tonic-gate gettext "lpadmin: ERROR: Can't create class ${class}.\n" 1>&2 2300Sstevel@tonic-gate gettext " TO FIX: This is an existing printer name;\n" 1>&2 2310Sstevel@tonic-gate gettext " choose another name.\n" 1>&2 2320Sstevel@tonic-gate exit 1 2330Sstevel@tonic-gate fi 2340Sstevel@tonic-gate ;; 2350Sstevel@tonic-gate r) 2360Sstevel@tonic-gate local="true" 2370Sstevel@tonic-gate ;; 2380Sstevel@tonic-gate esac 2390Sstevel@tonic-gatedone 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate# 2420Sstevel@tonic-gate# We don't have anything to do; let user know and bail 2430Sstevel@tonic-gate# 2442792Sjacobsif [[ ! -n "${printer}" && ! -n "${delete}" && ! -n "${local}" ]] ; then 2450Sstevel@tonic-gate gettext "lpadmin: ERROR: Nothing to do.\n" 1>&2 2460Sstevel@tonic-gate gettext " TO FIX: You must give one of these options:\n" 1>&2 2470Sstevel@tonic-gate gettext " -p, -d, -x -S\n" 1>&2 2480Sstevel@tonic-gate exit 1 2490Sstevel@tonic-gatefi 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate# 2520Sstevel@tonic-gate# Printer does not exist 2530Sstevel@tonic-gate# To be consistent with 2.5, assume adding local printer 2540Sstevel@tonic-gate# 2552792Sjacobsif [[ ! -n "${device}" && ! -n "${server}" && ! -n "${delete}" && \ 2562792Sjacobs ! -n "${local}" ]] ; then 2570Sstevel@tonic-gate ${LPGET} "${printer}" > /dev/null 2>&1 2580Sstevel@tonic-gate lpget_stat=$? 2592792Sjacobs if [[ ${lpget_stat} -ne 0 ]] ; then 2600Sstevel@tonic-gate gettext "lpadmin: ERROR: Missing -U or -v option.\n" 1>&2 2610Sstevel@tonic-gate gettext " TO FIX: Local printers must have\n" 1>&2 2620Sstevel@tonic-gate gettext " a port defined (-v option) or\n" 1>&2 2630Sstevel@tonic-gate gettext " have dial-out instructions (-U option).\n" 1>&2 2640Sstevel@tonic-gate exit 1 2650Sstevel@tonic-gate fi 2660Sstevel@tonic-gatefi 2670Sstevel@tonic-gate 2682264Sjacobs# process the "server" value 2692264Sjacobs# It can be a hostname, UUCP form (server!queue), RCMD form(queue@server), 2702264Sjacobs# or in URI form ({scheme}://{endpoint}) 2712264Sjacobs# 2722264Sjacobscase "${server}" in 2732264Sjacobs *://*) # URI form 2742264Sjacobs uri=${server} 2752792Sjacobs rem_printer=$(expr "${server}" : ".*://.*/\([^/]*\)") 2762792Sjacobs server=$(expr "${server}" : ".*://\([^/]*\)/.*") 2772264Sjacobs ;; 2782264Sjacobs *@*) # RCMD form 2792792Sjacobs rem_printer=$(expr "${server}" : "\(.*\)@.*") 2802792Sjacobs server=$(expr "${server}" : ".*@\(.*\)") 2812264Sjacobs ;; 2822264Sjacobs *!*) # UUCP form 2832792Sjacobs rem_printer=$(expr "${server}" : ".*!\(.*\)") 2842792Sjacobs server=$(expr "${server}" : "\(.*\)!.*") 2852264Sjacobs ;; 2862264Sjacobs *) # hostname 2872264Sjacobs rem_printer=${printer} 2882264Sjacobs ;; 2892264Sjacobsesac 2902792Sjacobs 2913125Sjacobs# if there is a "device" or LP configuration, it's local 2923125Sjacobsif [[ -n "${device}" || -f /etc/lp/printers/${printer}/configuration || \ 2933125Sjacobs -f /etc/lp/classes/${printer} ]] ; then 2943125Sjacobs local="true" 2953125Sjacobsfi 2962792Sjacobs 2972792Sjacobs# Do the LP configuration for a local printer served by lpsched 2983125Sjacobsif [[ -x ${LPADMIN} && -n "${local}" ]] ; then 2992792Sjacobs # enumerate LP configured printers before modification 300*8532SSumanth.Naropanth@Sun.COM PRE=$(${MKTEMP} lpadmin-pre.XXXXXX) 301*8532SSumanth.Naropanth@Sun.COM if [[ -z "${PRE}" ]] ; then 302*8532SSumanth.Naropanth@Sun.COM gettext "lpadmin: System error; cannot create temporary file\n" 1>&2 303*8532SSumanth.Naropanth@Sun.COM exit 2 304*8532SSumanth.Naropanth@Sun.COM fi 305*8532SSumanth.Naropanth@Sun.COM 3063478Sjacobs (/bin/ls /etc/lp/printers 2>/dev/null ; /bin/ls /etc/lp/classes \ 3073478Sjacobs 2>/dev/null) >${PRE} 3082792Sjacobs 3092792Sjacobs # if there are no printers configured, enable LP service(s) 3103781Sceastha [[ ! -s "${PRE}" ]] && lp_config_service enable 3112792Sjacobs 3122792Sjacobs # add filters to LP service 3132792Sjacobs lp_config_filters 3142792Sjacobs 3153781Sceastha # add new ppd file to PPD file repositories 3163781Sceastha if [[ -n "${ppd_file}" && -x ${PPDMGR} ]] ; then 3173781Sceastha add_new_ppd_file "${ppd_file}" 3183781Sceastha fi 3193781Sceastha 3202792Sjacobs # modify LP destination(s) 3213999Sjacobs CMD="${PFEXEC} ${LPADMIN}" 3222792Sjacobs while [[ -n "$*" ]] ; do # to deal with multi-word arguments 3232792Sjacobs CMD="$CMD \"$1\"" 3243781Sceastha # replace the ppd_file originally specified with the -n option 3253781Sceastha # with the one returned from call to ppdmgr 3263781Sceastha if [[ "${1}" = "-n" ]] ; then 3273781Sceastha CMD="$CMD \"${ppd_file}\"" 3283781Sceastha shift 3293781Sceastha fi 3302792Sjacobs shift 3312792Sjacobs done 3322792Sjacobs case "$CMD" in 3332792Sjacobs *\"-D\") 3342792Sjacobs CMD="$CMD \"\"" 3352792Sjacobs ;; 3362792Sjacobs esac 3370Sstevel@tonic-gate 3382792Sjacobs # execute the LP lpadmin command 3392792Sjacobs eval $CMD 3402792Sjacobs exit_code=$? 3412792Sjacobs 3422792Sjacobs # enumerate LP configured printers after modification 343*8532SSumanth.Naropanth@Sun.COM POST=$(${MKTEMP} lpadmin-post.XXXXXX) 344*8532SSumanth.Naropanth@Sun.COM if [[ -z "${POST}" ]] ; then 345*8532SSumanth.Naropanth@Sun.COM gettext "lpadmin: System error; cannot create temporary file\n" 1>&2 346*8532SSumanth.Naropanth@Sun.COM /bin/rm -f ${PRE} >/dev/null 2>&1 347*8532SSumanth.Naropanth@Sun.COM exit 2 348*8532SSumanth.Naropanth@Sun.COM fi 349*8532SSumanth.Naropanth@Sun.COM 3503478Sjacobs (/bin/ls /etc/lp/printers 2>/dev/null ; /bin/ls /etc/lp/classes \ 3513478Sjacobs 2>/dev/null) >${POST} 3522792Sjacobs 3532792Sjacobs # if there are no destinations, disable the service(s) 3543478Sjacobs [[ ! -s "${POST}" ]] && lp_config_service disable 3552792Sjacobs 3562792Sjacobs # sync printers.conf with LP configuration 3572792Sjacobs lp_config_sync_pconf "${PRE}" "${POST}" 3583478Sjacobs 3593478Sjacobs /bin/rm -f ${PRE} ${POST} 3602792Sjacobsfi 3612792Sjacobs 3622792Sjacobs# Do any printers.conf configuration that is required 3632792Sjacobsif [[ -n "${delete}" ]] ; then 3642792Sjacobs if [[ "${delete}" = "all" ]] ; then 3652792Sjacobs [[ $exit_code -eq 0 ]] && delete_all 3663125Sjacobs elif [[ -z "${local}" ]] ; then 3672792Sjacobs ${LPSET} -n system -x ${delete} 3682792Sjacobs exit_code=$? 3692792Sjacobs fi 3707661SJonathan.Ca@Sun.COMelse 3717661SJonathan.Ca@Sun.COM if [[ -z "${local}" ]] ; then 3727661SJonathan.Ca@Sun.COM # if we need a uri, find the "best" one. 3737661SJonathan.Ca@Sun.COM if [[ -z "${uri}" ]] ; then 3747661SJonathan.Ca@Sun.COM uri="ipp://${server}/printers/${rem_printer}" 3757661SJonathan.Ca@Sun.COM ${LPSTAT} -p ${uri} >/dev/null 2>&1 3767661SJonathan.Ca@Sun.COM if [[ $? -ne 0 ]] ; then 3777661SJonathan.Ca@Sun.COM uri="lpd://${server}/printers/${rem_printer}#Solaris" 3787661SJonathan.Ca@Sun.COM fi 3793710Sjacobs fi 3807661SJonathan.Ca@Sun.COM # set the bsdaddr 3817661SJonathan.Ca@Sun.COM bsdaddr="${server},${rem_printer},Solaris" 3823710Sjacobs 3838204SJonathan.Ca@Sun.COM if [[ -n "${printer}" && -n "${server}" ]] ; then 3847661SJonathan.Ca@Sun.COM ${LPSET} -n system \ 3857661SJonathan.Ca@Sun.COM -a "printer-uri-supported=${uri}" \ 3867661SJonathan.Ca@Sun.COM -a "bsdaddr=${bsdaddr}" ${printer} 3877661SJonathan.Ca@Sun.COM exit_code=$? 3887661SJonathan.Ca@Sun.COM fi 3897661SJonathan.Ca@Sun.COM 3900Sstevel@tonic-gate fi 3910Sstevel@tonic-gate 3922792Sjacobs if [[ -n "${printer}" && -n "${description}" ]] ; then 3932792Sjacobs ${LPSET} -n system \ 3942792Sjacobs -a "description=${description}" ${printer} 3952792Sjacobs exit_code=$? 3960Sstevel@tonic-gate fi 3970Sstevel@tonic-gatefi 3980Sstevel@tonic-gate 3992792Sjacobs# if the "default" doesn't resolve a "bsdaddr", the printer is gone, remove it 4002792Sjacobs${LPGET} -n system -k bsdaddr _default >/dev/null 2>&1 || 4012792Sjacobs ${LPSET} -n system -x _default >/dev/null 2>&1 4020Sstevel@tonic-gate 4030Sstevel@tonic-gateexit $exit_code 404