1 #!/usr/bin/ksh
   2 #
   3 # CDDL HEADER START
   4 #
   5 # The contents of this file are subject to the terms of the
   6 # Common Development and Distribution License, Version 1.0 only
   7 # (the "License").  You may not use this file except in compliance
   8 # with the License.
   9 #
  10 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  11 # or http://www.opensolaris.org/os/licensing.
  12 # See the License for the specific language governing permissions
  13 # and limitations under the License.
  14 #
  15 # When distributing Covered Code, include this CDDL HEADER in each
  16 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  17 # If applicable, add the following below this CDDL HEADER, with the
  18 # fields enclosed by brackets "[]" replaced with your own identifying
  19 # information: Portions Copyright [yyyy] [name of copyright owner]
  20 #
  21 # CDDL HEADER END
  22 #
  23 #
  24 #ident  "%Z%%M% %I%     %E% SMI"
  25 #
  26 # Copyright (c) 2001 by Sun Microsystems, Inc.
  27 # All rights reserved.
  28 #
  29 
  30 #
  31 # Process arguments.
  32 #
  33 process_args()
  34 {
  35     DEBUG=
  36     DHCPRSRC=
  37     DHCPPATH=
  38     DHCPRSRC_NEW=
  39     DHCPPATH_NEW=
  40     ERR=
  41     USAGE="Usage: %s: [-d]"
  42     while getopts ${VALIDOPTS} name
  43     do
  44         case $name in
  45         d)    DEBUG="-d";;
  46         r)    DHCPRSRC="${OPTARG}";;
  47         p)    DHCPPATH="${OPTARG}";;
  48         R)    DHCPRSRC_NEW="${OPTARG}";;
  49         P)    DHCPPATH_NEW="${OPTARG}";;
  50         ?)    ERR=1;;
  51         esac
  52     done
  53 
  54     #
  55     # If the resource is a valid option, then it is a required one
  56     #
  57     echo ${VALIDOPTS} | grep r: >/dev/null
  58     if [ $? == 0 ]
  59     then
  60         USAGE="${USAGE} -r resource"
  61         if [ -z "${DHCPRSRC}" ]
  62         then
  63             ERR=1
  64         fi
  65     fi
  66 
  67     #
  68     # If the path is a valid option, then it is a required one
  69     #
  70     echo ${VALIDOPTS} | grep p: >/dev/null
  71     if [ $? == 0 ]
  72     then
  73         USAGE="${USAGE} -p path"
  74         if [ -z "${DHCPPATH}" ]
  75         then
  76             ERR=1
  77         fi
  78     fi
  79 
  80     #
  81     # If the conversion resource is a valid option, then it is a required one
  82     #
  83     echo ${VALIDOPTS} | grep R: >/dev/null
  84     if [ $? == 0 ]
  85     then
  86         USAGE="${USAGE} -R resource"
  87         if [ -z "${DHCPRSRC_NEW}" ]
  88         then
  89             ERR=1
  90         fi
  91     fi
  92 
  93     #
  94     # If the conversion path is a valid option, then it is a required one
  95     #
  96     echo ${VALIDOPTS} | grep P: >/dev/null
  97     if [ $? == 0 ]
  98     then
  99         USAGE="${USAGE} -P path"
 100         if [ -z "${DHCPPATH_NEW}" ]
 101         then
 102             ERR=1
 103         fi
 104     fi
 105 
 106     if [ ! -z "${ERR}" ]
 107     then
 108         printf "$USAGE\n" $0
 109         exit -1
 110     fi
 111 }
 112 
 113 #
 114 # Return the primary interface's IP address.
 115 #
 116 get_server_ip()
 117 {
 118     awk '$1 ~ /^[0-9]/ && $2 == "'${SRVNAME}'" { printf "%s", $1; exit }' /etc/inet/hosts
 119 }
 120 
 121 #
 122 # Based on the network specification, determine whether or not network is 
 123 # subnetted or supernetted.
 124 # Given a dotted IP network number, convert it to the default class
 125 # network.(used to detect subnetting). Requires one argument, the
 126 # network number. (e.g. 10.0.0.0) Echos the default network and default
 127 # mask for success, null if error.
 128 #
 129 get_default_class()
 130 {
 131     NN01=${1%%.*}
 132     tmp=${1#*.}
 133     NN02=${tmp%%.*}
 134     tmp=${tmp#*.}
 135     NN03=${tmp%%.*}
 136     tmp=${tmp#*.}
 137     NN04=${tmp%%.*}
 138     RETNET=""
 139     RETMASK=""
 140 
 141     typeset -i16 ONE=10#${1%%.*}
 142     typeset -i10 X=$((${ONE}&16#f0))
 143     if [ ${X} -eq 224 ]
 144     then
 145         # Multicast
 146         typeset -i10 TMP=$((${ONE}&16#f0))
 147         RETNET="${TMP}.0.0.0"
 148         RETMASK="240.0.0.0"
 149     fi
 150     typeset -i10 X=$((${ONE}&16#80))
 151     if [ -z "${RETNET}" -a ${X} -eq 0 ]
 152     then
 153         # Class A
 154         RETNET="${NN01}.0.0.0"
 155         RETMASK="255.0.0.0"
 156     fi
 157     typeset -i10 X=$((${ONE}&16#c0))
 158     if [ -z "${RETNET}" -a ${X} -eq 128 ]
 159     then
 160         # Class B
 161         RETNET="${NN01}.${NN02}.0.0"
 162         RETMASK="255.255.0.0"
 163     fi
 164     typeset -i10 X=$((${ONE}&16#e0))
 165     if [ -z "${RETNET}" -a ${X} -eq 192 ]
 166     then
 167         # Class C
 168         RETNET="${NN01}.${NN02}.${NN03}.0"
 169         RETMASK="255.255.255.0"
 170     fi
 171     print - ${RETNET} ${RETMASK}
 172     unset NNO1 NNO2 NNO3 NNO4 RETNET RETMASK X ONE
 173 }
 174 
 175 #
 176 # Based on the nsswitch setting, query the netmasks table for a netmask.
 177 # Accepts one argument, a dotted IP address.
 178 #
 179 get_netmask()
 180 {
 181     MTMP=`getent netmasks ${1} | awk '{ print $2 }'`
 182     if [ ! -z "${MTMP}" ]
 183     then
 184         print - ${MTMP}
 185     fi
 186 }
 187 
 188 # Given a network number and subnetmask, return the broadcast address.
 189 get_bcast_addr()
 190 {
 191     typeset -i16 NNO1=10#${1%%.*}
 192     tmp=${1#*.}
 193     typeset -i16 NNO2=10#${tmp%%.*}
 194     tmp=${tmp#*.}
 195     typeset -i16 NNO3=10#${tmp%%.*}
 196     tmp=${tmp#*.}
 197     typeset -i16 NNO4=10#${tmp%%.*}
 198 
 199     typeset -i16 NMO1=10#${2%%.*}
 200     tmp=${2#*.}
 201     typeset -i16 NMO2=10#${tmp%%.*}
 202     tmp=${tmp#*.}
 203     typeset -i16 NMO3=10#${tmp%%.*}
 204     tmp=${tmp#*.}
 205     typeset -i16 NMO4=10#${tmp%%.*}
 206 
 207     typeset -i16 ONE
 208     typeset -i16 TWO
 209     typeset -i16 THREE
 210     typeset -i16 FOUR
 211     let ONE=\~${NMO1}\|${NNO1}
 212     let ONE=${ONE}\&16#ff
 213     let TWO=\~${NMO2}\|${NNO2}
 214     let TWO=${TWO}\&16#ff
 215     let THREE=\~${NMO3}\|${NNO3}
 216     let THREE=${THREE}\&16#ff
 217     let FOUR=\~${NMO4}\|${NNO4}
 218     let FOUR=${FOUR}\&16#ff
 219     typeset -i10 ONE
 220     typeset -i10 TWO
 221     typeset -i10 THREE
 222     typeset -i10 FOUR
 223     print - "${ONE}.${TWO}.${THREE}.${FOUR}"
 224 }
 225 
 226 # Given a network number and subnetmask, return the subnet address.
 227 get_subnet_addr()
 228 {
 229     typeset -i16 NNO1=10#${1%%.*}
 230     tmp=${1#*.}
 231     typeset -i16 NNO2=10#${tmp%%.*}
 232     tmp=${tmp#*.}
 233     typeset -i16 NNO3=10#${tmp%%.*}
 234     tmp=${tmp#*.}
 235     typeset -i16 NNO4=10#${tmp%%.*}
 236 
 237     typeset -i16 NMO1=10#${2%%.*}
 238     tmp=${2#*.}
 239     typeset -i16 NMO2=10#${tmp%%.*}
 240     tmp=${tmp#*.}
 241     typeset -i16 NMO3=10#${tmp%%.*}
 242     tmp=${tmp#*.}
 243     typeset -i16 NMO4=10#${tmp%%.*}
 244 
 245     typeset -i16 ONE
 246     typeset -i16 TWO
 247     typeset -i16 THREE
 248     typeset -i16 FOUR
 249     let ONE=${NMO1}\&${NNO1}
 250     let TWO=${NMO2}\&${NNO2}
 251     let THREE=${NMO3}\&${NNO3}
 252     let FOUR=${NMO4}\&${NNO4}
 253     typeset -i10 ONE
 254     typeset -i10 TWO
 255     typeset -i10 THREE
 256     typeset -i10 FOUR
 257     print - "${ONE}.${TWO}.${THREE}.${FOUR}"
 258 }
 259 
 260 #
 261 # Given a macro definition defined in MACRO_DEFINITION and a symbol/value string
 262 # defined in SRCH, search the macro definition for the string. If not found, abort.
 263 # If found, remove the string from the definition.
 264 #
 265 macro_find_and_replace()
 266 {
 267     VAL=`expr "${MACRO_DEFINITION}" : .*"${SRCH}".*`
 268     if [ "${VAL}" = "0" ]
 269     then
 270         echo "${SRCH} not defined as part of the macro definition"
 271         echo "${TESTNAME} - Test failed!"
 272         exit 1
 273     fi
 274 
 275     SRCH=`echo "${SRCH}" | cut -c2-`
 276     MACRO_DEFINITION=`echo "${MACRO_DEFINITION}" | sed s/"${SRCH}"//`
 277 }
 278 
 279 #
 280 # Given a macro or option definition, returns its value.
 281 #
 282 function get_value
 283 {
 284         echo $* | cut -d ' ' -f 3-
 285 }