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