xref: /netbsd/usr.sbin/ypserv/ypinit/ypinit.sh (revision 6550d01e)
1#!/bin/sh
2#
3#	$NetBSD: ypinit.sh,v 1.12 2004/10/05 11:35:35 tron 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=`mktemp /tmp/ypservers.XXXXXX` || exit 1
24trap "rm -f ${tmpfile} ; exit 0" EXIT INT QUIT
25
26umask 077				# protect created directories
27
28if [ `${ID} -u` != 0 ]; then
29	echo 1>&2 "$progname: you must be root to run this"
30	exit 1
31fi
32
33args=`getopt cl:ms: $*`
34if [ $? -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
71fi
72
73if [ -z ${servertype} ]; then
74	cat 1>&2 << __usage 
75usage: 	${progname} -c [domainname] [-l server1,...,serverN]
76	${progname} -m [domainname] [-l server1,...,serverN]
77	${progname} -s master_server [domainname] [-l server1,...,serverN]
78
79The \`-c' flag sets up a YP client, the \`-m' flag builds a master YP
80server, and the \`-s' flag builds a slave YP server.  When building a
81slave YP server, \`master_server' must be an existing, reachable YP server.
82__usage
83	exit 1
84fi
85
86# Check if domainname is set, don't accept an empty domainname
87if [ -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
95fi
96
97# Check if hostname is set, don't accept an empty hostname
98host=`${HOSTNAME}`
99if [ -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
106fi
107if [ "${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
111fi
112
113# Check if the YP directory exists.
114if [ ! -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
121fi
122
123echo "Server type: ${servertype}"
124echo "Domain:      ${domain}"
125if [ "${servertype}" = "slave" ]; then
126	echo "Master:      ${master}"
127fi
128echo ""
129
130binding_dir=${yp_dir}/binding
131if [ ! -d ${binding_dir} ]; then
132	cat 1>&2 << __no_dir
133$progname: The directory ${binding_dir} does not exist.
134	Restore it from the distribution.
135__no_dir
136	exit 1
137fi
138
139if [ -z "${noninteractive}" ]; then
140	cat << __client_setup
141A YP client needs a list of YP servers to bind to.
142Whilst ypbind supports -broadcast, its use is not recommended.
143__client_setup
144
145	done=
146	while [ -z "${done}" ]; do
147		> ${tmpfile}
148		cat <<__list_of_servers
149
150Please enter a list of YP servers, in order of preference.
151When finished, press RETURN on a blank line or enter EOF.
152
153__list_of_servers
154
155		if [ "${servertype}" != "client" ]; then
156			echo ${host} >> ${tmpfile}
157			echo "	next host: ${host}";
158		fi
159		echo -n "	next host: ";
160
161		while read nextserver ; test -n "${nextserver}"
162		do
163			echo ${nextserver} >> ${tmpfile}
164			echo -n "	next host: ";
165		done
166
167		if [ -s ${tmpfile} ]; then
168			echo ""
169			echo "The current servers are:"
170			echo ""
171			cat ${tmpfile}
172			echo ""
173			echo -n "Is this correct? [y/n: n] "
174			read DONE
175			case ${DONE} in
176			y*|Y*)
177				done=yes
178				;;
179			esac
180		else
181			echo    ""
182			echo    "You have not supplied any servers."
183		fi
184		if [ -z "${done}" ]; then
185			echo -n "Do you wish to abort? [y/n: n] "
186			read ABORT
187			case ${ABORT} in
188			y*|Y*)
189				exit 0
190				;;
191			esac
192		fi
193	done
194else # interacive
195	if [ "${servertype}" != "client" ]; then
196		echo ${host} >> ${tmpfile}
197	fi
198	echo "${serverlist}" | sed -e 's/,/\
199/g' >> ${tmpfile}
200#the above newline is required
201	echo ""
202	echo "The current servers are:"
203	echo ""
204	cat ${tmpfile}
205	echo ""
206fi # interactive
207
208if [ -s ${tmpfile} ]; then
209	${INSTALL} -c -m 0444 ${tmpfile} ${binding_dir}/${domain}.ypservers
210fi
211
212if [ "${servertype}" = "client" ]; then
213	exit 0
214fi
215
216cat << __notice1
217
218Installing the YP database may require that you answer a few questions.
219Any configuration questions will be asked at the beginning of the procedure.
220
221__notice1
222
223if [ -d "${yp_dir}/${domain}" ]; then
224	echo	"Can we destroy the existing ${yp_dir}/${domain}"
225	echo -n	"and its contents? [y/n: n]  "
226	read KILL
227
228	case ${KILL} in
229	y*|Y*)
230		rm -rf ${yp_dir}/${domain}
231		if [ $? != 0 ]; then
232			echo 1>&2 \
233		"$progname: Can't clean up old directory ${yp_dir}/${domain}"
234			exit 1
235		fi
236		;;
237
238	*)
239		echo "OK, please clean it up by hand and start again."
240		exit 0
241		;;
242	esac
243fi
244
245if ! mkdir "${yp_dir}/${domain}"; then
246	echo 1>&2 "$progname: Can't make new directory ${yp_dir}/${domain}"
247	exit 1
248fi
249
250case ${servertype} in
251master)
252	if [ ! -f ${yp_dir}/Makefile ];	then
253		if [ ! -f ${yp_dir}/Makefile.main ]; then
254			echo 1>&2 \
255			    "$progname: Can't find ${yp_dir}/Makefile.main"
256			exit 1
257		fi
258		cp ${yp_dir}/Makefile.main ${yp_dir}/Makefile
259	fi
260
261	subdir=`grep "^SUBDIR=" ${yp_dir}/Makefile`
262
263	if [ -z "${subdir}" ]; then
264		echo 1>&2 \
265    "$progname: Can't find line starting with 'SUBDIR=' in ${yp_dir}/Makefile"
266		exit 1
267	fi
268
269	newsubdir="SUBDIR="
270	for dir in `echo ${subdir} | cut -c8-255`; do
271		if [ "${dir}" != "${domain}" ]; then
272			newsubdir="${newsubdir} ${dir}"
273		fi
274	done
275	newsubdir="${newsubdir} ${domain}"
276
277	if [ -f ${yp_dir}/Makefile.tmp ]; then
278		rm ${yp_dir}/Makefile.tmp
279	fi
280
281	mv ${yp_dir}/Makefile ${yp_dir}/Makefile.tmp
282	sed -e "s/^${subdir}/${newsubdir}/" ${yp_dir}/Makefile.tmp > \
283	    ${yp_dir}/Makefile
284	rm ${yp_dir}/Makefile.tmp
285
286	if [ ! -f ${yp_dir}/Makefile.yp ]; then
287		echo 1>&2 "$progname: Can't find ${yp_dir}/Makefile.yp"
288		exit 1
289	fi
290
291	cp ${yp_dir}/Makefile.yp ${yp_dir}/${domain}/Makefile
292
293	# Create `ypservers' with own name, so that yppush won't
294	# lose when we run "make".
295	(
296		cd ${yp_dir}/${domain}
297		echo "$host $host" > ypservers
298		${MAKEDBM} ypservers ypservers
299	)
300
301	echo "Done.  Be sure to run \`make' in ${yp_dir}."
302
303	;;
304
305slave)
306	echo ""
307
308	maps=`${YPWHICH} -d ${domain} -h ${master} -f -m 2>/dev/null | \
309	    awk '{ if (substr($2, 1, length("'$master'")) == "'$master'") \
310		print $1; }'`
311
312	if [ -z "${maps}" ]; then
313		cat 1>&2 << __no_maps
314$progname: Can't find any maps for ${domain} on ${master}
315	Please check that the appropriate YP service is running.
316__no_maps
317		exit 1
318	fi
319
320	for map in ${maps}; do
321		echo "Transferring ${map}..."
322		if ! ${YPXFR} -h ${master} -c -d ${domain} ${map}; then
323			echo 1>&2 "$progname: Can't transfer map ${map}"
324			exit 1
325		fi
326	done
327
328	cat << __dont_forget
329
330Don't forget to update the \`ypservers' on ${master},
331by adding an entry similar to:
332  ${host} ${host}
333
334__dont_forget
335	exit 0
336
337	;;
338
339*)
340	echo 1>&2 "$progname: unknown servertype \`${servertype}'"
341	exit 1
342esac
343