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