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