Home | History | Annotate | Line # | Download | only in ypinit
ypinit.sh revision 1.7
      1  1.1  thorpej #!/bin/sh
      2  1.1  thorpej #
      3  1.7    lukem #	$NetBSD: ypinit.sh,v 1.7 1997/11/18 00:44:19 lukem 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.1  thorpej # Modified by Jason R. Thorpe <thorpej (at] NetBSD.ORG>
      9  1.7    lukem # 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.7    lukem MAKEDBM=/usr/sbin/makedbm
     17  1.6    lukem YPWHICH=/usr/bin/ypwhich
     18  1.6    lukem YPXFR=/usr/sbin/ypxfr
     19  1.1  thorpej 
     20  1.7    lukem progname=`basename $0`
     21  1.7    lukem yp_dir=/var/yp
     22  1.7    lukem error=usage				# assume usage error
     23  1.1  thorpej 
     24  1.4    lukem umask 077				# protect created directories
     25  1.4    lukem 
     26  1.7    lukem if [ `${ID} -u` != 0 ]; then
     27  1.7    lukem 	echo 1>&2 "$progname: you must be root to run this"
     28  1.7    lukem 	exit 1
     29  1.7    lukem fi
     30  1.7    lukem 
     31  1.7    lukem case $# in
     32  1.7    lukem 1)
     33  1.2  thorpej 	if [ $1 = "-m" ]; then		# ypinit -m
     34  1.7    lukem 		domain=`${DOMAINNAME}`
     35  1.7    lukem 		servertype=master
     36  1.7    lukem 		error=
     37  1.1  thorpej 	fi
     38  1.7    lukem 	;;
     39  1.1  thorpej 
     40  1.7    lukem 2)
     41  1.2  thorpej 	if [ $1 = "-m" ]; then		# ypinit -m domainname
     42  1.7    lukem 		domain=${2}
     43  1.7    lukem 		servertype=master
     44  1.7    lukem 		error=
     45  1.1  thorpej 	fi
     46  1.2  thorpej 	if [ $1 = "-s" ]; then		# ypinit -s master_server
     47  1.7    lukem 		domain=`${DOMAINNAME}`
     48  1.7    lukem 		servertype=slave
     49  1.7    lukem 		master=${2}
     50  1.7    lukem 		error=
     51  1.1  thorpej 	fi
     52  1.7    lukem 	;;
     53  1.1  thorpej 
     54  1.7    lukem 3)
     55  1.2  thorpej 	if [ $1 = "-s" ]; then		# ypinit -s master_server domainname
     56  1.7    lukem 		domain=${3}
     57  1.7    lukem 		servertype=slave
     58  1.7    lukem 		master=${2}
     59  1.7    lukem 		error=
     60  1.7    lukem 	fi
     61  1.7    lukem 	;;
     62  1.7    lukem esac
     63  1.7    lukem 
     64  1.7    lukem if [ "${error}" = "usage" ]; then
     65  1.7    lukem 	cat 1>&2 << __usage 
     66  1.7    lukem usage: ${progname} -m [domainname]
     67  1.7    lukem        ${progname} -s master_server [domainname]
     68  1.1  thorpej 
     69  1.7    lukem The \`-m' flag builds a master YP server, and the \`-s' flag builds
     70  1.7    lukem a slave YP server.  When building a slave YP server, \`master_server'
     71  1.1  thorpej must be an existing, reachable YP server.
     72  1.1  thorpej __usage
     73  1.1  thorpej 
     74  1.1  thorpej 	exit 1
     75  1.1  thorpej fi
     76  1.1  thorpej 
     77  1.1  thorpej # Check if domainname is set, don't accept an empty domainname
     78  1.7    lukem if [ -z "${domain}" ]; then
     79  1.7    lukem 	cat << __no_domain 1>&2
     80  1.7    lukem $progname: The local host's YP domain name has not been set.
     81  1.7    lukem 	Please set it with the domainname(1) command or pass the domain as
     82  1.7    lukem 	an argument to ${progname}.
     83  1.1  thorpej __no_domain
     84  1.1  thorpej 
     85  1.1  thorpej 	exit 1
     86  1.1  thorpej fi
     87  1.1  thorpej 
     88  1.1  thorpej # Check if hostname is set, don't accept an empty hostname
     89  1.7    lukem host=`${HOSTNAME}`
     90  1.7    lukem if [ -z "${host}" ]; then
     91  1.7    lukem 	cat 1>&2 << __no_hostname
     92  1.7    lukem $progname: The local host's hostname has not been set.
     93  1.7    lukem 	Please set it with the hostname(1) command.
     94  1.1  thorpej __no_hostname
     95  1.1  thorpej 
     96  1.1  thorpej 	exit 1
     97  1.1  thorpej fi
     98  1.1  thorpej 
     99  1.1  thorpej # Check if the YP directory exists.
    100  1.7    lukem if [ ! -d ${yp_dir} -o -f ${yp_dir} ]; then
    101  1.7    lukem 	cat 1>&2 << __no_dir
    102  1.7    lukem $progname: The directory ${yp_dir} does not exist.
    103  1.7    lukem 	Restore it from the distribution.
    104  1.7    lukem __no_dir
    105  1.7    lukem 
    106  1.1  thorpej 	exit 1
    107  1.1  thorpej fi
    108  1.1  thorpej 
    109  1.7    lukem echo "Server type: ${servertype}"
    110  1.7    lukem echo "Domain:      ${domain}"
    111  1.7    lukem if [ "${servertype}" != "master" ]; then
    112  1.7    lukem 	echo "Master:      ${master}"
    113  1.2  thorpej fi
    114  1.2  thorpej echo ""
    115  1.7    lukem cat << __notice1
    116  1.1  thorpej 
    117  1.7    lukem Installing the YP database may require that you answer a few questions.
    118  1.6    lukem Any configuration questions will be asked at the beginning of the procedure.
    119  1.1  thorpej 
    120  1.1  thorpej __notice1
    121  1.1  thorpej 
    122  1.7    lukem if [ -d "${yp_dir}/${domain}" ]; then
    123  1.7    lukem 	echo	"Can we destroy the existing ${yp_dir}/${domain}"
    124  1.7    lukem 	echo -n	"and its contents? [y/n: n]  "
    125  1.1  thorpej 	read KILL
    126  1.1  thorpej 
    127  1.7    lukem 	error=
    128  1.1  thorpej 	case ${KILL} in
    129  1.2  thorpej 	y*|Y*)
    130  1.7    lukem 		error="delete"
    131  1.1  thorpej 		;;
    132  1.1  thorpej 
    133  1.1  thorpej 	*)
    134  1.7    lukem 		error=
    135  1.1  thorpej 		;;
    136  1.1  thorpej 	esac
    137  1.1  thorpej 
    138  1.7    lukem 	if [ "${error}" = "delete" ]; then
    139  1.7    lukem 		rm -rf ${yp_dir}/${domain}
    140  1.7    lukem 		if [ $? != 0 ]; then
    141  1.7    lukem 			echo 1>&2 \
    142  1.7    lukem 		"$progname: Can't clean up old directory ${yp_dir}/${domain}"
    143  1.1  thorpej 			exit 1
    144  1.1  thorpej 		fi
    145  1.2  thorpej 	else
    146  1.2  thorpej 		echo "OK, please clean it up by hand and start again."
    147  1.2  thorpej 		exit 0
    148  1.1  thorpej 	fi
    149  1.1  thorpej fi
    150  1.1  thorpej 
    151  1.7    lukem if ! mkdir "${yp_dir}/${domain}"; then
    152  1.7    lukem 	echo 1>&2 "$progname: Can't make new directory ${yp_dir}/${domain}"
    153  1.1  thorpej 	exit 1
    154  1.1  thorpej fi
    155  1.1  thorpej 
    156  1.7    lukem case ${servertype} in
    157  1.7    lukem master)
    158  1.7    lukem 	if [ ! -f ${yp_dir}/Makefile ];	then
    159  1.7    lukem 		if [ ! -f ${yp_dir}/Makefile.main ]; then
    160  1.7    lukem 			echo 1>&2 \
    161  1.7    lukem 			    "$progname: Can't find ${yp_dir}/Makefile.main"
    162  1.1  thorpej 			exit 1
    163  1.1  thorpej 		fi
    164  1.7    lukem 		cp ${yp_dir}/Makefile.main ${yp_dir}/Makefile
    165  1.1  thorpej 	fi
    166  1.1  thorpej 
    167  1.7    lukem 	subdir=`grep "^SUBDIR=" ${yp_dir}/Makefile`
    168  1.1  thorpej 
    169  1.7    lukem 	if [ -z "${subdir}" ]; then
    170  1.7    lukem 		echo 1>&2 \
    171  1.7    lukem     "$progname: Can't find line starting with 'SUBDIR=' in ${yp_dir}/Makefile"
    172  1.1  thorpej 		exit 1
    173  1.1  thorpej 	fi
    174  1.1  thorpej 
    175  1.7    lukem 	newsubdir="SUBDIR="
    176  1.7    lukem 	for dir in `echo ${subdir} | cut -c8-255`; do
    177  1.7    lukem 		if [ "${dir}" != "${domain}" ]; then
    178  1.7    lukem 			newsubdir="${newsubdir} ${dir}"
    179  1.1  thorpej 		fi
    180  1.1  thorpej 	done
    181  1.7    lukem 	newsubdir="${newsubdir} ${domain}"
    182  1.1  thorpej 
    183  1.7    lukem 	if [ -f ${yp_dir}/Makefile.tmp ]; then 
    184  1.7    lukem 		rm ${yp_dir}/Makefile.tmp
    185  1.1  thorpej 	fi
    186  1.1  thorpej 
    187  1.7    lukem 	mv ${yp_dir}/Makefile ${yp_dir}/Makefile.tmp
    188  1.7    lukem 	sed -e "s/^${subdir}/${newsubdir}/" ${yp_dir}/Makefile.tmp > \
    189  1.7    lukem 	    ${yp_dir}/Makefile
    190  1.7    lukem 	rm ${yp_dir}/Makefile.tmp
    191  1.1  thorpej 
    192  1.7    lukem 	if [ ! -f ${yp_dir}/Makefile.yp ]; then
    193  1.7    lukem 		echo 1>&2 "$progname: Can't find ${yp_dir}/Makefile.yp"
    194  1.1  thorpej 		exit 1
    195  1.1  thorpej 	fi
    196  1.1  thorpej 
    197  1.7    lukem 	cp ${yp_dir}/Makefile.yp ${yp_dir}/${domain}/Makefile
    198  1.1  thorpej 
    199  1.7    lukem 	# Create `ypservers' with own name, so that yppush won't
    200  1.1  thorpej 	# lose when we run "make".
    201  1.1  thorpej 	(
    202  1.7    lukem 		cd ${yp_dir}/${domain}
    203  1.7    lukem 		echo "$host $host" > ypservers
    204  1.6    lukem 		${MAKEDBM} ypservers ypservers
    205  1.1  thorpej 	)
    206  1.2  thorpej 
    207  1.7    lukem 	echo "Done.  Be sure to run \`make' in ${yp_dir}."
    208  1.7    lukem 
    209  1.7    lukem 	;;
    210  1.1  thorpej 
    211  1.7    lukem slave)
    212  1.1  thorpej 	echo ""
    213  1.1  thorpej 
    214  1.7    lukem 	maps=`${YPWHICH} -d ${domain} -h ${master} -f -m 2>/dev/null | \
    215  1.7    lukem 	    awk '{ if (substr($2, 1, length("'$master'")) == "'$master'") \
    216  1.7    lukem 		print $1; }'`
    217  1.7    lukem 
    218  1.7    lukem 	if [ -z "${maps}" ]; then
    219  1.7    lukem 		cat 1>&2 << __no_maps
    220  1.7    lukem $progname: Can't find any maps for ${domain} on ${master}
    221  1.7    lukem 	Please check that the appropriate YP service is running.
    222  1.7    lukem __no_maps
    223  1.7    lukem 		exit 1
    224  1.7    lukem 	fi
    225  1.7    lukem 
    226  1.7    lukem 	for map in ${maps}; do
    227  1.7    lukem 		echo "Transferring ${map}..."
    228  1.7    lukem 		if ! ${YPXFR} -h ${master} -c -d ${domain} ${map}; then
    229  1.7    lukem 			echo 1>&2 "$progname: Can't transfer map ${map}"
    230  1.1  thorpej 			exit 1
    231  1.1  thorpej 		fi
    232  1.1  thorpej 	done
    233  1.1  thorpej 
    234  1.7    lukem 	cat << __dont_forget
    235  1.7    lukem 
    236  1.7    lukem Don't forget to update the \`ypservers' on ${master},
    237  1.7    lukem by adding an entry similar to:
    238  1.7    lukem   ${host} ${host}
    239  1.7    lukem 
    240  1.7    lukem __dont_forget
    241  1.1  thorpej 	exit 0
    242  1.7    lukem 
    243  1.7    lukem 	;;
    244  1.7    lukem 
    245  1.7    lukem *)
    246  1.7    lukem 	echo 1>&2 "$progname: unknown servertype \`${servertype}'"
    247  1.7    lukem 	exit 1
    248  1.7    lukem esac
    249