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