Home | History | Annotate | Line # | Download | only in ypinit
ypinit.sh revision 1.10
      1 #!/bin/sh
      2 #
      3 #	$NetBSD: ypinit.sh,v 1.10 2001/08/01 07:01:03 garbled Exp $
      4 #
      5 # ypinit.sh - setup a master or slave YP server
      6 #
      7 # Originally written by Mats O Jansson <moj (at] stacken.kth.se>
      8 # Modified by Jason R. Thorpe <thorpej (at] NetBSD.ORG>
      9 # Reworked by Luke Mewburn <lukem (at] netbsd.org>
     10 #
     11 
     12 PATH=/bin:/usr/sbin:/usr/bin:${PATH}
     13 DOMAINNAME=/bin/domainname
     14 HOSTNAME=/bin/hostname
     15 ID=/usr/bin/id
     16 INSTALL=/usr/bin/install
     17 MAKEDBM=/usr/sbin/makedbm
     18 YPWHICH=/usr/bin/ypwhich
     19 YPXFR=/usr/sbin/ypxfr
     20 
     21 progname=`basename $0`
     22 yp_dir=/var/yp
     23 tmpfile=`mktemp /tmp/ypservers.XXXXXX` || exit 1
     24 trap "rm -f ${tmpfile} ; exit 0" EXIT INT QUIT
     25 
     26 umask 077				# protect created directories
     27 
     28 if [ `${ID} -u` != 0 ]; then
     29 	echo 1>&2 "$progname: you must be root to run this"
     30 	exit 1
     31 fi
     32 
     33 args=`getopt cl:ms: $*`
     34 if [ $? -eq 0 ]; then
     35 	set -- $args
     36 	for i; do
     37 		case $i in
     38 		"-c")
     39 			servertype=client
     40 			shift
     41 			;;
     42 		"-m")
     43 			servertype=master
     44 			shift
     45 			;;
     46 		"-s")
     47 			servertype=slave
     48 			master=${2}
     49 			shift
     50 			shift
     51 			;;
     52 		"-l")
     53 			noninteractive=yes
     54 			serverlist=${2}
     55 			shift
     56 			shift
     57 			;;
     58 		"--")
     59 			shift
     60 			break
     61 			;;
     62 		esac
     63 	done
     64 
     65 	if [ $# -eq 1 ]; then
     66 		domain=${1}
     67 		shift;
     68 	else
     69 		domain=`${DOMAINNAME}`
     70 	fi
     71 fi
     72 
     73 if [ -z ${servertype} ]; then
     74 	cat 1>&2 << __usage 
     75 usage: 	${progname} -c [domainname] [-l server1,...,serverN]
     76 	${progname} -m [domainname] [-l server1,...,serverN]
     77 	${progname} -s master_server [domainname] [-l server1,...,serverN]
     78 
     79 The \`-c' flag sets up a YP client, the \`-m' flag builds a master YP
     80 server, and the \`-s' flag builds a slave YP server.  When building a
     81 slave YP server, \`master_server' must be an existing, reachable YP server.
     82 __usage
     83 	exit 1
     84 fi
     85 
     86 # Check if domainname is set, don't accept an empty domainname
     87 if [ -z "${domain}" ]; then
     88 	cat << __no_domain 1>&2
     89 $progname: The local host's YP domain name has not been set.
     90 	Please set it with the domainname(1) command or pass the domain as
     91 	an argument to ${progname}.
     92 __no_domain
     93 
     94 	exit 1
     95 fi
     96 
     97 # Check if hostname is set, don't accept an empty hostname
     98 host=`${HOSTNAME}`
     99 if [ -z "${host}" ]; then
    100 	cat 1>&2 << __no_hostname
    101 $progname: The local host's hostname has not been set.
    102 	Please set it with the hostname(1) command.
    103 __no_hostname
    104 
    105 	exit 1
    106 fi
    107 if [ "${servertype}" = "slave" -a "${host}" = "${master}" ]; then
    108 	echo 1>&2 \
    109 	    "$progname: cannot setup a YP slave server off the local host."
    110 	exit 1
    111 fi
    112 
    113 # Check if the YP directory exists.
    114 if [ ! -d ${yp_dir} -o -f ${yp_dir} ]; then
    115 	cat 1>&2 << __no_dir
    116 $progname: The directory ${yp_dir} does not exist.
    117 	Restore it from the distribution.
    118 __no_dir
    119 
    120 	exit 1
    121 fi
    122 
    123 echo "Server type: ${servertype}"
    124 echo "Domain:      ${domain}"
    125 if [ "${servertype}" = "slave" ]; then
    126 	echo "Master:      ${master}"
    127 fi
    128 echo ""
    129 
    130 binding_dir=${yp_dir}/binding
    131 if [ ! -d ${binding_dir} ]; then
    132 $progname: The directory ${binding_dir} does not exist.
    133 	Restore it from the distribution.
    134 __no_dir
    135 	exit 1
    136 fi
    137 
    138 if [ -z "${noninteractive}" ]; then
    139 	cat << __client_setup
    140 A YP client needs a list of YP servers to bind to.
    141 Whilst ypbind supports -broadcast, its use is not recommended.
    142 __client_setup
    143 
    144 	done=
    145 	while [ -z "${done}" ]; do
    146 		> ${tmpfile}
    147 		cat <<__list_of_servers
    148 
    149 Please enter a list of YP servers, in order of preference.
    150 When finished, press RETURN on a blank line or enter EOF.
    151 
    152 __list_of_servers
    153 
    154 		if [ "${servertype}" != "client" ]; then
    155 			echo ${host} >> ${tmpfile}
    156 			echo "	next host: ${host}";
    157 		fi
    158 		echo -n "	next host: ";
    159 
    160 		while read nextserver ; test -n "${nextserver}"
    161 		do
    162 			echo ${nextserver} >> ${tmpfile}
    163 			echo -n "	next host: ";
    164 		done
    165 
    166 		if [ -s ${tmpfile} ]; then
    167 			echo ""
    168 			echo "The current servers are:"
    169 			echo ""
    170 			cat ${tmpfile}
    171 			echo ""
    172 			echo -n "Is this correct? [y/n: n] "
    173 			read DONE
    174 			case ${DONE} in
    175 			y*|Y*)
    176 				done=yes
    177 				;;
    178 			esac
    179 		else
    180 			echo    ""
    181 			echo    "You have not supplied any servers."
    182 		fi
    183 		if [ -z "${done}" ]; then
    184 			echo -n "Do you wish to abort? [y/n: n] "
    185 			read ABORT
    186 			case ${ABORT} in
    187 			y*|Y*)
    188 				exit 0
    189 				;;
    190 			esac
    191 		fi
    192 	done
    193 else # interacive
    194 	if [ "${servertype}" != "client" ]; then
    195 		echo ${host} >> ${tmpfile}
    196 	fi
    197 	echo "${serverlist}" | sed -e 's/,/\
    198 /g' >> ${tmpfile}
    199 #the above newline is required
    200 	echo ""
    201 	echo "The current servers are:"
    202 	echo ""
    203 	cat ${tmpfile}
    204 	echo ""
    205 fi # interactive
    206 
    207 if [ -s ${tmpfile} ]; then
    208 	${INSTALL} -c -m 0444 ${tmpfile} ${binding_dir}/${domain}.ypservers
    209 fi
    210 
    211 if [ "${servertype}" = "client" ]; then
    212 	exit 0
    213 fi
    214 
    215 cat << __notice1
    216 
    217 Installing the YP database may require that you answer a few questions.
    218 Any configuration questions will be asked at the beginning of the procedure.
    219 
    220 __notice1
    221 
    222 if [ -d "${yp_dir}/${domain}" ]; then
    223 	echo	"Can we destroy the existing ${yp_dir}/${domain}"
    224 	echo -n	"and its contents? [y/n: n]  "
    225 	read KILL
    226 
    227 	case ${KILL} in
    228 	y*|Y*)
    229 		rm -rf ${yp_dir}/${domain}
    230 		if [ $? != 0 ]; then
    231 			echo 1>&2 \
    232 		"$progname: Can't clean up old directory ${yp_dir}/${domain}"
    233 			exit 1
    234 		fi
    235 		;;
    236 
    237 	*)
    238 		echo "OK, please clean it up by hand and start again."
    239 		exit 0
    240 		;;
    241 	esac
    242 fi
    243 
    244 if ! mkdir "${yp_dir}/${domain}"; then
    245 	echo 1>&2 "$progname: Can't make new directory ${yp_dir}/${domain}"
    246 	exit 1
    247 fi
    248 
    249 case ${servertype} in
    250 master)
    251 	if [ ! -f ${yp_dir}/Makefile ];	then
    252 		if [ ! -f ${yp_dir}/Makefile.main ]; then
    253 			echo 1>&2 \
    254 			    "$progname: Can't find ${yp_dir}/Makefile.main"
    255 			exit 1
    256 		fi
    257 		cp ${yp_dir}/Makefile.main ${yp_dir}/Makefile
    258 	fi
    259 
    260 	subdir=`grep "^SUBDIR=" ${yp_dir}/Makefile`
    261 
    262 	if [ -z "${subdir}" ]; then
    263 		echo 1>&2 \
    264     "$progname: Can't find line starting with 'SUBDIR=' in ${yp_dir}/Makefile"
    265 		exit 1
    266 	fi
    267 
    268 	newsubdir="SUBDIR="
    269 	for dir in `echo ${subdir} | cut -c8-255`; do
    270 		if [ "${dir}" != "${domain}" ]; then
    271 			newsubdir="${newsubdir} ${dir}"
    272 		fi
    273 	done
    274 	newsubdir="${newsubdir} ${domain}"
    275 
    276 	if [ -f ${yp_dir}/Makefile.tmp ]; then 
    277 		rm ${yp_dir}/Makefile.tmp
    278 	fi
    279 
    280 	mv ${yp_dir}/Makefile ${yp_dir}/Makefile.tmp
    281 	sed -e "s/^${subdir}/${newsubdir}/" ${yp_dir}/Makefile.tmp > \
    282 	    ${yp_dir}/Makefile
    283 	rm ${yp_dir}/Makefile.tmp
    284 
    285 	if [ ! -f ${yp_dir}/Makefile.yp ]; then
    286 		echo 1>&2 "$progname: Can't find ${yp_dir}/Makefile.yp"
    287 		exit 1
    288 	fi
    289 
    290 	cp ${yp_dir}/Makefile.yp ${yp_dir}/${domain}/Makefile
    291 
    292 	# Create `ypservers' with own name, so that yppush won't
    293 	# lose when we run "make".
    294 	(
    295 		cd ${yp_dir}/${domain}
    296 		echo "$host $host" > ypservers
    297 		${MAKEDBM} ypservers ypservers
    298 	)
    299 
    300 	echo "Done.  Be sure to run \`make' in ${yp_dir}."
    301 
    302 	;;
    303 
    304 slave)
    305 	echo ""
    306 
    307 	maps=`${YPWHICH} -d ${domain} -h ${master} -f -m 2>/dev/null | \
    308 	    awk '{ if (substr($2, 1, length("'$master'")) == "'$master'") \
    309 		print $1; }'`
    310 
    311 	if [ -z "${maps}" ]; then
    312 		cat 1>&2 << __no_maps
    313 $progname: Can't find any maps for ${domain} on ${master}
    314 	Please check that the appropriate YP service is running.
    315 __no_maps
    316 		exit 1
    317 	fi
    318 
    319 	for map in ${maps}; do
    320 		echo "Transferring ${map}..."
    321 		if ! ${YPXFR} -h ${master} -c -d ${domain} ${map}; then
    322 			echo 1>&2 "$progname: Can't transfer map ${map}"
    323 			exit 1
    324 		fi
    325 	done
    326 
    327 	cat << __dont_forget
    328 
    329 Don't forget to update the \`ypservers' on ${master},
    330 by adding an entry similar to:
    331   ${host} ${host}
    332 
    333 __dont_forget
    334 	exit 0
    335 
    336 	;;
    337 
    338 *)
    339 	echo 1>&2 "$progname: unknown servertype \`${servertype}'"
    340 	exit 1
    341 esac
    342