summaryrefslogtreecommitdiff
path: root/usr.sbin/ypserv/ypinit/ypinit.sh
blob: 2e21cd6deb1971efb2710d2082051a9dc2d1221a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/bin/sh
#
#	$NetBSD: ypinit.sh,v 1.15 2021/08/17 22:00:33 andvar Exp $
#
# ypinit.sh - setup a master or slave YP server
#
# Originally written by Mats O Jansson <moj@stacken.kth.se>
# Modified by Jason R. Thorpe <thorpej@NetBSD.org>
# Reworked by Luke Mewburn <lukem@NetBSD.org>
#

PATH=/bin:/usr/sbin:/usr/bin:${PATH}
BIN_DOMAINNAME=/bin/domainname
BIN_HOSTNAME=/bin/hostname
ID=/usr/bin/id
INSTALL=/usr/bin/install
MAKEDBM=/usr/sbin/makedbm
YPWHICH=/usr/bin/ypwhich
YPXFR=/usr/sbin/ypxfr

progname=$( basename $0 )
yp_dir=/var/yp
tmpfile=$(mktemp /tmp/ypservers.XXXXXX) || exit 1
trap "rm -f ${tmpfile} ; exit 0" EXIT INT QUIT

umask 077				# protect created directories

if [ $( ${ID} -u ) != 0 ]; then
	echo 1>&2 "$progname: you must be root to run this"
	exit 1
fi

while getopts cl:ms: i
do
	case $i in
	c)
		servertype=client
		;;
	l)
		noninteractive=yes
		serverlist=${OPTARG}
		;;
	m)
		servertype=master
		;;
	s)
		servertype=slave
		master=${OPTARG}
		;;
	\?)
		echo >&2	# "Illegal option" message issued by getopts
		servertype=	# force usage message just below
		break
		;;
	esac
done
shift $((OPTIND - 1))

if [ $# -eq 1 ]; then
	domain=${1}
	shift;
else
	domain=$( ${BIN_DOMAINNAME} )
fi

if [ -z ${servertype} ]; then
	cat 1>&2 << __usage
usage: 	${progname} -c [-l server1,...,serverN] [domainname]
	${progname} -m [-l server1,...,serverN] [domainname]
	${progname} -s master_server [-l server1,...,serverN] [domainname]

The \`-c' flag sets up a YP client, the \`-m' flag builds a master YP
server, and the \`-s' flag builds a slave YP server.  When building a
slave YP server, \`master_server' must be an existing, reachable YP server.
__usage
	exit 1
fi

# Check if domainname is set, don't accept an empty domainname
if [ -z "${domain}" ]; then
	cat << __no_domain 1>&2
$progname: The local host's YP domain name has not been set.
	Please set it with the domainname(1) command or pass the domain as
	an argument to ${progname}.
__no_domain

	exit 1
fi

# Check if hostname is set, don't accept an empty hostname
host=$( ${BIN_HOSTNAME} )
if [ -z "${host}" ]; then
	cat 1>&2 << __no_hostname
$progname: The local host's hostname has not been set.
	Please set it with the hostname(1) command.
__no_hostname

	exit 1
fi
if [ "${servertype}" = slave ] && [ "${host}" = "${master}" ]; then
	echo 1>&2 \
	    "$progname: cannot setup a YP slave server off the local host."
	exit 1
fi

# Check if the YP directory exists.
if ! [ -d "${yp_dir}" ]; then
	cat 1>&2 << __no_dir
$progname: The directory ${yp_dir} does not exist.
	Restore it from the distribution.
__no_dir

	exit 1
fi

echo "Server type: ${servertype}"
echo "Domain:      ${domain}"
if [ "${servertype}" = "slave" ]; then
	echo "Master:      ${master}"
fi
echo ""

binding_dir=${yp_dir}/binding
if ! [ -d ${binding_dir} ]; then
	cat 1>&2 << __no_dir
$progname: The directory ${binding_dir} does not exist.
	Restore it from the distribution.
__no_dir
	exit 1
fi

if [ -z "${noninteractive}" ]; then
	cat << __client_setup
A YP client needs a list of YP servers to bind to.
Whilst ypbind supports -broadcast, its use is not recommended.
__client_setup

	done=
	while [ -z "${done}" ]; do
		> ${tmpfile}
		cat <<__list_of_servers

Please enter a list of YP servers, in order of preference.
When finished, press RETURN on a blank line or enter EOF.

__list_of_servers

		if [ "${servertype}" != client ]; then
			echo "${host}" >> ${tmpfile}
			echo "	next host: ${host}";
		fi
		echo -n "	next host: ";

		while read nextserver && test -n "${nextserver}"
		do
			echo ${nextserver} >> ${tmpfile}
			echo -n "	next host: ";
		done

		if [ -s ${tmpfile} ]; then
			echo ""
			echo "The current servers are:"
			echo ""
			cat ${tmpfile}
			echo ""
			echo -n "Is this correct? [y/n: n] "
			read DONE
			case ${DONE} in
			y*|Y*)
				done=yes
				;;
			esac
		else
			echo    ""
			echo    "You have not supplied any servers."
		fi
		if [ -z "${done}" ]; then
			echo -n "Do you wish to abort? [y/n: n] "
			read ABORT
			case ${ABORT} in
			y*|Y*)
				exit 0
				;;
			esac
		fi
	done
else # interactive
	if [ "${servertype}" != "client" ]; then
		echo ${host} >> ${tmpfile}
	fi
	echo "${serverlist}" | sed -e 's/,/\
/g' >> ${tmpfile}
#the above newline is required
	echo ""
	echo "The current servers are:"
	echo ""
	cat ${tmpfile}
	echo ""
fi # interactive

if [ -s ${tmpfile} ]; then
	${INSTALL} -c -m 0444 ${tmpfile} ${binding_dir}/${domain}.ypservers
fi

if [ "${servertype}" = "client" ]; then
	exit 0
fi

cat << __notice1

Installing the YP database may require that you answer a few questions.
Any configuration questions will be asked at the beginning of the procedure.

__notice1

if [ -d "${yp_dir}/${domain}" ]; then
	echo	"Can we destroy the existing ${yp_dir}/${domain}"
	echo -n	"and its contents? [y/n: n]  "
	read KILL

	case ${KILL} in
	y*|Y*)
		rm -rf ${yp_dir}/${domain}
		if [ $? != 0 ]; then
			echo 1>&2 \
		"$progname: Can't clean up old directory ${yp_dir}/${domain}"
			exit 1
		fi
		;;

	*)
		echo "OK, please clean it up by hand and start again."
		exit 0
		;;
	esac
fi

if ! mkdir "${yp_dir}/${domain}"; then
	echo 1>&2 "$progname: Can't make new directory ${yp_dir}/${domain}"
	exit 1
fi

case ${servertype} in
master)
	if ! [ -f ${yp_dir}/Makefile ];	then
		if ! [ -f ${yp_dir}/Makefile.main ]; then
			echo 1>&2 \
			    "$progname: Can't find ${yp_dir}/Makefile.main"
			exit 1
		fi
		cp ${yp_dir}/Makefile.main ${yp_dir}/Makefile
	fi

	subdir=$(grep "^SUBDIR=" ${yp_dir}/Makefile)

	if [ -z "${subdir}" ]; then
		echo 1>&2 \
    "$progname: Can't find line starting with 'SUBDIR=' in ${yp_dir}/Makefile"
		exit 1
	fi

	newsubdir="SUBDIR="
	for dir in $(echo ${subdir} | cut -c8-255); do
		if [ "${dir}" != "${domain}" ]; then
			newsubdir="${newsubdir} ${dir}"
		fi
	done
	newsubdir="${newsubdir} ${domain}"

	if [ -f ${yp_dir}/Makefile.tmp ]; then
		rm ${yp_dir}/Makefile.tmp
	fi

	mv ${yp_dir}/Makefile ${yp_dir}/Makefile.tmp
	sed -e "s/^${subdir}/${newsubdir}/" ${yp_dir}/Makefile.tmp > \
	    ${yp_dir}/Makefile
	rm ${yp_dir}/Makefile.tmp

	if ! [ -f ${yp_dir}/Makefile.yp ]; then
		echo 1>&2 "$progname: Can't find ${yp_dir}/Makefile.yp"
		exit 1
	fi

	cp ${yp_dir}/Makefile.yp ${yp_dir}/${domain}/Makefile

	# Create `ypservers' with own name, so that yppush won't
	# lose when we run "make".
	(
		cd ${yp_dir}/${domain}
		echo "$host $host" > ypservers
		${MAKEDBM} ypservers ypservers
	)

	echo "Done.  Be sure to run \`make' in ${yp_dir}."

	;;

slave)
	echo ""

	maps=$( ${YPWHICH} -d ${domain} -h ${master} -f -m 2>/dev/null |
	    awk '{ if (substr($2, 1, length("'$master'")) == "'$master'") \
		print $1; }' )

	if [ -z "${maps}" ]; then
		cat 1>&2 << __no_maps
$progname: Can't find any maps for ${domain} on ${master}
	Please check that the appropriate YP service is running.
__no_maps
		exit 1
	fi

	for map in ${maps}; do
		echo "Transferring ${map}..."
		if ! ${YPXFR} -h ${master} -c -d ${domain} ${map}; then
			echo 1>&2 "$progname: Can't transfer map ${map}"
			exit 1
		fi
	done

	cat << __dont_forget

Don't forget to update the \`ypservers' on ${master},
by adding an entry similar to:
  ${host} ${host}

__dont_forget
	exit 0

	;;

*)
	echo 1>&2 "$progname: unknown servertype \`${servertype}'"
	exit 1
esac