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