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