xref: /freebsd/usr.sbin/ypserv/ypinit.sh (revision d37eb51047221dc3322b34db1038ff3aa533883f)
1#!/bin/sh
2#
3# ypinit.sh - setup a master or slave server.
4# (Taken from OpenBSD and modified for FreeBSD.)
5#
6DOMAINNAME=/bin/domainname
7HOSTNAME=/bin/hostname
8YPWHICH=/usr/bin/ypwhich
9YPXFR=/usr/libexec/ypxfr
10YP_DIR=/var/yp
11MAKEDBM=/usr/sbin/yp_mkdb
12MAPLIST="master.passwd.byname master.passwd.byuid passwd.byname passwd.byuid \
13	 group.byname group.bygid hosts.byname hosts.byaddr services.byname \
14	 rpc.byname rpc.bynumber networks.byname networks.byaddr netgroup \
15	 netgroup.byuser netgroup.byhost netid.byname publickey.byname \
16	 bootparams ethers.byname ethers.byaddr eui64.byname eui64.byid \
17	 amd.host mail.aliases ypservers protocols.byname protocols.bynumber \
18	 netmasks.byaddr"
19
20ERROR_EXISTS="NO"
21umask 077
22
23#set -xv
24
25ERROR=USAGE				# assume usage error
26
27if [ $# -eq 1 ]
28then
29	if [ $1 = "-m" ]		# ypinit -m
30	then
31		DOMAIN=`${DOMAINNAME}`
32		SERVERTYPE=MASTER
33		ERROR=
34	fi
35
36	if [ $1 = "-u" ]		# ypinit -u
37	then
38		DOMAIN=`${DOMAINNAME}`
39		SERVERTYPE=UPDATE
40		ERROR=
41	fi
42fi
43
44if [ $# -eq 2 ]
45then
46	if [ $1 = "-m" ]		# ypinit -m domainname
47	then
48		DOMAIN=${2}
49		SERVERTYPE=MASTER
50		ERROR=
51	fi
52
53	if [ $1 = "-s" ]		# ypinit -s master_server
54	then
55		DOMAIN=`${DOMAINNAME}`
56		SERVERTYPE=SLAVE
57		MASTER=${2}
58		ERROR=
59	fi
60
61	if [ $1 = "-u" ]		# ypinit -u domainname
62	then
63		DOMAIN=${2}
64		SERVERTYPE=UPDATE
65		ERROR=
66	fi
67fi
68
69if [ $# -eq 3 ]
70then
71	if [ $1 = "-s" ]		# ypinit -s master_server domainname
72	then
73		DOMAIN=${3}
74		SERVERTYPE=SLAVE
75		MASTER=${2}
76		ERROR=
77	fi
78fi
79
80if [ "${ERROR}" = "USAGE" ]; then
81	cat << \__usage 1>&2
82usage: ypinit -m [domainname]
83       ypinit -s master_server [domainname]
84       ypinit -u [domainname]
85
86The `-m' flag builds a master YP server, and the `-s' flag builds
87a slave YP server.  When building a slave YP server, `master_server'
88must be an existing, reachable YP server.
89The `-u' is for updating the ypservers map on a master server.
90__usage
91
92	exit 1
93fi
94
95# Check if domainname is set, don't accept an empty domainname
96if [ -z "${DOMAIN}" ]; then
97	cat << \__no_domain 1>&2
98The local host's YP domain name has not been set.  Please set it with
99the domainname(1) command or pass the domain as an argument to ypinit(8).
100__no_domain
101
102	exit 1
103fi
104
105# Check if hostname is set, don't accept an empty hostname
106HOST=`${HOSTNAME}`
107if [ -z "${HOST}" ]; then
108	cat << \__no_hostname 1>&2
109The local host's hostname has not been set.  Please set it with the
110hostname(1) command.
111__no_hostname
112
113	exit 1
114fi
115
116# Check if we have contact with master.
117# If we can't list the maps on the master, then we fake it with a
118# hard-coded list of maps. The FreeBSD ypxfr command will work even
119# if ypbind isn't running or if we are bound to ourselves instead of
120# the master (the slave should be bound to itself, but since it has
121# no maps yet, we can't get a maplist from it).
122if [ "${SERVERTYPE}" = "SLAVE" ];
123then
124	COUNT=`${YPWHICH} -d ${DOMAIN} -m 2>/dev/null | grep -i ${MASTER} | wc -l | tr -d " "`
125	if [ "$COUNT" = "0" ]
126	then
127		echo "Can't enumerate maps from ${MASTER}. Please check that it is running." 1>&2
128		echo "Note: using hardcoded maplist for map transfers." 1>&2
129		YPMAPLIST=${MAPLIST}
130	else
131		YPMAPLIST=`${YPWHICH} -d ${DOMAIN} -m | cut -d\  -f1`
132	fi
133	echo "" 1>&2
134fi
135
136# Check if user is root
137ID=`id -u`
138if [ "${ID}" != "0" ]; then
139	echo "You have to be the superuser to run this.  Please login as root." 1>&2
140	exit 1
141fi
142
143# Check if the YP directory exists.
144
145if [ ! -d ${YP_DIR} -o -f ${YP_DIR} ]
146then
147	echo "The directory ${YP_DIR} doesn't exist.  Restore it from the distribution." 1>&2
148	exit 1
149
150fi
151
152echo -n "Server Type: ${SERVERTYPE} Domain: ${DOMAIN}"
153if [ "${SERVERTYPE}" = "SLAVE" ]; then
154	echo -n " Master: ${MASTER}"
155fi
156echo ""
157
158if [ "${SERVERTYPE}" != "UPDATE" ];
159then
160	cat << \__notice1
161
162Creating an YP server will require that you answer a few questions.
163Questions will all be asked at the beginning of the procedure.
164
165__notice1
166
167	echo -n "Do you want this procedure to quit on non-fatal errors? [y/n: n]  "
168	read DOEXIT
169
170	case ${DOEXIT} in
171	y*|Y*)
172		ERROR_EXIT="YES"
173		;;
174
175	*)	ERROR_EXIT="NO"
176		echo ""
177		echo "Ok, please remember to go back and redo manually whatever fails."
178		echo "If you don't, something might not work. "
179		;;
180	esac
181
182	if [ -d "${YP_DIR}/${DOMAIN}" ]; then
183		echo ""
184		echo -n "Can we destroy the existing ${YP_DIR}/${DOMAIN} and its contents? [y/n: n]  "
185		read KILL
186
187		ERROR=
188		case ${KILL} in
189		y*|Y*)
190			ERROR="DELETE"
191			;;
192
193		*)	ERROR=
194			;;
195		esac
196
197		if [ "${ERROR}" = "DELETE" ]; then
198			if ! rm -rf ${YP_DIR}/${DOMAIN}; then
199				echo "Can't clean up old directory ${YP_DIR}/${DOMAIN}." 1>&2
200				exit 1
201			fi
202		else
203			echo "OK, please clean it up by hand and start again.  Bye"
204			exit 0
205		fi
206	fi
207
208	if ! mkdir "${YP_DIR}/${DOMAIN}"; then
209		echo "Can't make new directory ${YP_DIR}/${DOMAIN}." 1>&2
210		exit 1
211	fi
212fi
213
214if [ "${SERVERTYPE}" = "MASTER" ];
215then
216
217	if [ ! -f ${YP_DIR}/Makefile ]
218	then
219		if [ ! -f ${YP_DIR}/Makefile.dist ]
220		then
221			echo "Can't find ${YP_DIR}/Makefile.dist. " 1>&2
222			exit 1
223		fi
224		cp ${YP_DIR}/Makefile.dist ${YP_DIR}/Makefile
225	fi
226
227fi
228
229if [ "${SERVERTYPE}" = "SLAVE" ];
230then
231
232	echo "There will be no further questions. The remainder of the procedure"
233	echo "should take a few minutes, to copy the databases from ${MASTER}."
234
235	for MAP in ${YPMAPLIST}
236	do
237		echo "Transferring ${MAP}..."
238		if ! ${YPXFR} -p ${YP_DIR} -h ${MASTER} -c -d ${DOMAIN} ${MAP}; then
239			echo "Can't transfer map ${MAP}." 1>&2
240			ERROR_EXISTS="YES"
241			if [ "${ERROR_EXIT}" = "YES" ]; then
242				exit 1
243			fi
244		fi
245	done
246
247	echo ""
248	if [ "${ERROR_EXISTS}" = "YES"  ]; then
249		echo "${HOST} has been setup as an YP slave server with errors. " 1>&2
250		echo "Please remember fix any problem that occurred." 1>&2
251	else
252		echo "${HOST} has been setup as an YP slave server without any errors. "
253	fi
254
255	echo "Don't forget to update map ypservers on ${MASTER}."
256	exit 0
257fi
258
259LIST_OK="NO"
260
261while [ "${LIST_OK}" = "NO" ];
262do
263	if [ "${SERVERTYPE}" = "MASTER" ];
264	then
265		HOST_LIST="${HOST}"
266		echo ""
267		echo "At this point, we have to construct a list of this domains YP servers."
268		echo "${HOST} is already known as master server."
269		echo "Please continue to add any slave servers, one per line. When you are"
270		echo "done with the list, type a <control D>."
271		echo "	master server   :  ${HOST}"
272	fi
273
274	if [ "${SERVERTYPE}" = "UPDATE" ];
275	then
276		HOST_LIST="${HOST}"
277		NEW_LIST=""
278		MASTER_NAME=""
279		SHORT_HOST=`echo ${HOST} | cut -d. -f1`
280		if [ -f ${YP_DIR}/${DOMAIN}/ypservers ];
281		then
282			for srv in `${MAKEDBM} -u ${YP_DIR}/${DOMAIN}/ypservers | grep -v "^YP" | tr "\t" " " | cut -d\  -f1`;
283			do
284				short_srv=`echo ${srv} | cut -d. -f1`
285				if [ "${SHORT_HOST}" != "${short_srv}" ]
286				then
287					if [ "${NEW_LIST}" = "" ];
288					then
289						NEW_LIST="${srv}"
290					else
291						NEW_LIST="${NEW_LIST} ${srv}"
292					fi
293				fi
294			done;
295			MASTER_NAME=`${MAKEDBM} -u ${YP_DIR}/${DOMAIN}/ypservers | grep "^YP_MASTER_NAME" | tr "\t" " " | cut -d\  -f2`
296		fi
297		echo ""
298		echo "Update the list of hosts running YP servers in domain ${DOMAIN}."
299		echo "Master for this domain is ${MASTER_NAME}."
300		echo ""
301		echo "First verify old servers, type \\\\ to remove a server."
302		echo "Then add new servers, one per line. When done type a <control D>."
303		echo ""
304		echo "	master server   :  ${HOST}"
305		if [ "${NEW_LIST}" != "" ]; then
306			for node in $NEW_LIST; do
307				echo -n "	verify host     : [${node}] "
308				read verify
309				if [ "${verify}" != "\\" ]; then
310					HOST_LIST="${HOST_LIST} ${node}"
311				fi
312			done;
313		fi
314	fi
315
316	echo -n "	next host to add:  "
317
318	while read h
319	do
320		echo -n "	next host to add:  "
321		HOST_LIST="${HOST_LIST} ${h}"
322	done
323
324	echo ""
325	echo "The current list of NIS servers looks like this:"
326	echo ""
327
328	for h in `echo ${HOST_LIST}`;
329	do
330		echo ${h}
331	done
332
333	echo ""
334	echo -n "Is this correct?  [y/n: y]  "
335	read hlist_ok
336
337	case $hlist_ok in
338	n*)	echo "Let's try the whole thing again...";;
339	N*)	echo "Let's try the whole thing again...";;
340	*)	LIST_OK="YES";;
341	esac
342
343done
344
345echo "Building ${YP_DIR}/${DOMAIN}/ypservers..."
346rm -f ${YP_DIR}/ypservers
347touch -f ${YP_DIR}/ypservers
348rm -f ${YP_DIR}/${DOMAIN}/ypservers
349for host in ${HOST_LIST};
350do
351	echo "${host} ${host}" >> ${YP_DIR}/ypservers
352	echo "${host} ${host}"
353done | ${MAKEDBM} - ${YP_DIR}/${DOMAIN}/ypservers
354
355if [ $? -ne 0 ]; then
356	echo "" 1>&2
357	echo "Couldn't build yp data base ${YP_DIR}/${DOMAIN}/ypservers." 1>&2
358	ERROR_EXISTS="YES"
359	if [ "${ERROR_EXIT}" = "YES" ]; then
360		exit 1
361	fi
362fi
363
364if [ "${SERVERTYPE}" = "MASTER" ]; then
365	CUR_PWD=`pwd`
366	cd ${YP_DIR}
367	echo "Running ${YP_DIR}/Makefile..."
368	if ! make NOPUSH=True UPDATE_DOMAIN=${DOMAIN} YP_DIR=${YP_DIR}; then
369		echo "" 1>&2
370		echo "Error running Makefile." 1>&2
371		ERROR_EXISTS="YES"
372		if [ "${ERROR_EXIT}" = "YES" ]; then
373			exit 1
374		fi
375	fi
376
377	cd ${CUR_PWD}
378
379	echo ""
380	if [ "${ERROR_EXISTS}" = "YES" ]; then
381		echo "${HOST} has been setup as an YP master server with errors. " 1>&2
382		echo "Please remember fix any problem that occurred." 1>&2
383	else
384		echo "${HOST} has been setup as an YP master server without any errors. "
385	fi
386fi
387