3 # $NetBSD: ypinit.sh,v 1.11 2003/11/12 13:31:08 grant Exp $
5 # ypinit.sh - setup a master or slave YP server
7 # Originally written by Mats O Jansson <moj@stacken.kth.se>
8 # Modified by Jason R. Thorpe <thorpej@NetBSD.org>
9 # Reworked by Luke Mewburn <lukem@NetBSD.org>
12 PATH
=/bin
:/usr
/sbin
:/usr
/bin
:${PATH}
13 DOMAINNAME
=/bin
/domainname
14 HOSTNAME
=/bin
/hostname
16 INSTALL
=/usr
/bin
/install
17 MAKEDBM
=/usr
/sbin
/makedbm
18 YPWHICH
=/usr
/bin
/ypwhich
21 progname
=`basename $0`
23 tmpfile
=`mktemp /tmp/ypservers.XXXXXX` ||
exit 1
24 trap "rm -f ${tmpfile} ; exit 0" EXIT INT QUIT
26 umask 077 # protect created directories
28 if [ `${ID} -u` != 0 ]; then
29 echo 1>&2 "$progname: you must be root to run this"
33 args
=`getopt cl:ms: $*`
69 domain
=`${DOMAINNAME}`
73 if [ -z ${servertype} ]; then
75 usage: ${progname} -c [domainname] [-l server1,...,serverN]
76 ${progname} -m [domainname] [-l server1,...,serverN]
77 ${progname} -s master_server [domainname] [-l server1,...,serverN]
79 The \`-c' flag sets up a YP client, the \`-m' flag builds a master YP
80 server, and the \`-s' flag builds a slave YP server. When building a
81 slave YP server, \`master_server' must be an existing, reachable YP server.
86 # Check if domainname is set, don't accept an empty domainname
87 if [ -z "${domain}" ]; then
88 cat << __no_domain 1>&2
89 $progname: The local host's YP domain name has not been set.
90 Please set it with the domainname(1) command or pass the domain as
91 an argument to ${progname}.
97 # Check if hostname is set, don't accept an empty hostname
99 if [ -z "${host}" ]; then
100 cat 1>&2 << __no_hostname
101 $progname: The local host's hostname has not been set.
102 Please set it with the hostname(1) command.
107 if [ "${servertype}" = "slave" -a "${host}" = "${master}" ]; then
109 "$progname: cannot setup a YP slave server off the local host."
113 # Check if the YP directory exists.
114 if [ ! -d ${yp_dir} -o -f ${yp_dir} ]; then
116 $progname: The directory ${yp_dir} does not exist.
117 Restore it from the distribution.
123 echo "Server type: ${servertype}"
124 echo "Domain: ${domain}"
125 if [ "${servertype}" = "slave" ]; then
126 echo "Master: ${master}"
130 binding_dir
=${yp_dir}/binding
131 if [ ! -d ${binding_dir} ]; then
133 $progname: The directory ${binding_dir} does not exist.
134 Restore it from the distribution.
139 if [ -z "${noninteractive}" ]; then
140 cat << __client_setup
141 A YP client needs a list of YP servers to bind to.
142 Whilst ypbind supports -broadcast, its use is not recommended.
146 while [ -z "${done}" ]; do
148 cat <<__list_of_servers
150 Please enter a list of YP servers, in order of preference.
151 When finished, press RETURN on a blank line or enter EOF.
155 if [ "${servertype}" != "client" ]; then
156 echo ${host} >> ${tmpfile}
157 echo " next host: ${host}";
159 echo -n " next host: ";
161 while read nextserver
; test -n "${nextserver}"
163 echo ${nextserver} >> ${tmpfile}
164 echo -n " next host: ";
167 if [ -s ${tmpfile} ]; then
169 echo "The current servers are:"
173 echo -n "Is this correct? [y/n: n] "
182 echo "You have not supplied any servers."
184 if [ -z "${done}" ]; then
185 echo -n "Do you wish to abort? [y/n: n] "
195 if [ "${servertype}" != "client" ]; then
196 echo ${host} >> ${tmpfile}
198 echo "${serverlist}" |
sed -e 's/,/\
200 #the above newline is required
202 echo "The current servers are:"
208 if [ -s ${tmpfile} ]; then
209 ${INSTALL} -c -m 0444 ${tmpfile} ${binding_dir}/${domain}.ypservers
212 if [ "${servertype}" = "client" ]; then
218 Installing the YP database may require that you answer a few questions.
219 Any configuration questions will be asked at the beginning of the procedure.
223 if [ -d "${yp_dir}/${domain}" ]; then
224 echo "Can we destroy the existing ${yp_dir}/${domain}"
225 echo -n "and its contents? [y/n: n] "
230 rm -rf ${yp_dir}/${domain}
233 "$progname: Can't clean up old directory ${yp_dir}/${domain}"
239 echo "OK, please clean it up by hand and start again."
245 if ! mkdir
"${yp_dir}/${domain}"; then
246 echo 1>&2 "$progname: Can't make new directory ${yp_dir}/${domain}"
250 case ${servertype} in
252 if [ ! -f ${yp_dir}/Makefile
]; then
253 if [ ! -f ${yp_dir}/Makefile.main
]; then
255 "$progname: Can't find ${yp_dir}/Makefile.main"
258 cp ${yp_dir}/Makefile.main
${yp_dir}/Makefile
261 subdir
=`grep "^SUBDIR=" ${yp_dir}/Makefile`
263 if [ -z "${subdir}" ]; then
265 "$progname: Can't find line starting with 'SUBDIR=' in ${yp_dir}/Makefile"
270 for dir
in `echo ${subdir} | cut -c8-255`; do
271 if [ "${dir}" != "${domain}" ]; then
272 newsubdir
="${newsubdir} ${dir}"
275 newsubdir
="${newsubdir} ${domain}"
277 if [ -f ${yp_dir}/Makefile.tmp
]; then
278 rm ${yp_dir}/Makefile.tmp
281 mv ${yp_dir}/Makefile
${yp_dir}/Makefile.tmp
282 sed -e "s/^${subdir}/${newsubdir}/" ${yp_dir}/Makefile.tmp > \
284 rm ${yp_dir}/Makefile.tmp
286 if [ ! -f ${yp_dir}/Makefile.yp ]; then
287 echo 1>&2 "$progname: Can
't find ${yp_dir}/Makefile.yp"
291 cp ${yp_dir}/Makefile.yp ${yp_dir}/${domain}/Makefile
293 # Create `ypservers' with own name
, so that yppush won
't
294 # lose when we run "make".
296 cd ${yp_dir}/${domain}
297 echo "$host $host" > ypservers
298 ${MAKEDBM} ypservers ypservers
301 echo "Done. Be sure to run \`make' in ${yp_dir}.
"
308 maps=`${YPWHICH} -d ${domain} -h ${master} -f -m 2>/dev/null | \
309 awk '{ if (substr($2, 1, length("'$master'")) == "'$master'") \
312 if [ -z "${maps}" ]; then
313 cat 1>&2 << __no_maps
314 $progname: Can't find any maps for ${domain} on ${master}
315 Please check that the appropriate YP service is running.
320 for map in ${maps}; do
321 echo "Transferring
${map}...
"
322 if ! ${YPXFR} -h ${master} -c -d ${domain} ${map}; then
323 echo 1>&2 "$progname: Can
't transfer map ${map}"
330 Don't forget to update the \
`ypservers' on ${master},
331 by adding an entry similar to:
340 echo 1>&2 "$progname: unknown servertype \`${servertype}'"