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