Pull up CVS idents from FreeBSD to match our current version.
[dragonfly.git] / usr.sbin / ypserv / ypinit.sh
bloba2a2d86720de7a651e3549552dd72e0f879b6b00
1 #!/bin/sh
2 # $FreeBSD: src/usr.sbin/ypserv/ypinit.sh,v 1.3 1999/08/28 01:21:15 peter Exp $
3 # $DragonFly: src/usr.sbin/ypserv/ypinit.sh,v 1.3 2007/05/13 22:25:42 swildner Exp $
5 # ypinit.sh - setup an master or slave server.
6 # (Taken from OpenBSD and modified for FreeBSD.)
8 DOMAINNAME=/bin/domainname
9 HOSTNAME=/bin/hostname
10 YPWHICH=/usr/bin/ypwhich
11 YPXFR=/usr/libexec/ypxfr
12 YP_DIR=/var/yp
13 MAKEDBM=/usr/sbin/yp_mkdb
14 MAPLIST="master.passwd.byname master.passwd.byuid passwd.byname passwd.byuid \
15 group.byname group.bygid hosts.byname hosts.byaddr services.byname \
16 rpc.byname rpc.bynumber networks.byname networks.byaddr netgroup \
17 netgroup.byuser netgroup.byhost netid.byname publickey.byname \
18 bootparams ethers.byname ethers.byaddr amd.host mail.aliases \
19 ypservers protocols.byname protocols.bynumber netmasks.byaddr"
21 ERROR_EXISTS="NO"
22 umask 077
24 #set -xv
26 ERROR=USAGE # assume usage error
28 if [ $# -eq 1 ]
29 then
30 if [ $1 = "-m" ] # ypinit -m
31 then
32 DOMAIN=`${DOMAINNAME}`
33 SERVERTYPE=MASTER
34 ERROR=
37 if [ $1 = "-u" ] # ypinit -u
38 then
39 DOMAIN=`${DOMAINNAME}`
40 SERVERTYPE=UPDATE
41 ERROR=
45 if [ $# -eq 2 ]
46 then
47 if [ $1 = "-m" ] # ypinit -m domainname
48 then
49 DOMAIN=${2}
50 SERVERTYPE=MASTER
51 ERROR=
54 if [ $1 = "-s" ] # ypinit -s master_server
55 then
56 DOMAIN=`${DOMAINNAME}`
57 SERVERTYPE=SLAVE
58 MASTER=${2}
59 ERROR=
62 if [ $1 = "-u" ] # ypinit -u domainname
63 then
64 DOMAIN=${2}
65 SERVERTYPE=UPDATE
66 ERROR=
70 if [ $# -eq 3 ]
71 then
72 if [ $1 = "-s" ] # ypinit -s master_server domainname
73 then
74 DOMAIN=${3}
75 SERVERTYPE=SLAVE
76 MASTER=${2}
77 ERROR=
81 if [ "${ERROR}" = "USAGE" ]; then
82 cat << \__usage 1>&2
83 usage: ypinit -m [domainname]
84 ypinit -s master_server [domainname]
85 ypinit -u [domainname]
87 The `-m' flag builds a master YP server, and the `-s' flag builds
88 a slave YP server. When building a slave YP server, `master_server'
89 must be an existing, reachable YP server.
90 The `-u' is for updating the ypservers map on a master server.
91 __usage
93 exit 1
96 # Check if domainname is set, don't accept an empty domainname
97 if [ -z "${DOMAIN}" ]; then
98 cat << \__no_domain 1>&2
99 The local host's YP domain name has not been set. Please set it with
100 the domainname(1) command or pass the domain as an argument to ypinit(8).
101 __no_domain
103 exit 1
106 # Check if hostname is set, don't accept an empty hostname
107 HOST=`${HOSTNAME}`
108 if [ -z "${HOST}" ]; then
109 cat << \__no_hostname 1>&2
110 The local host's hostname has not been set. Please set it with the
111 hostname(1) command.
112 __no_hostname
114 exit 1
117 # Check if we have contact with master.
118 # If we can't list the maps on the master, then we fake it with a
119 # hard-coded list of maps. The FreeBSD ypxfr command will work even
120 # if ypbind isn't running or if we are bound to ourselves instead of
121 # the master (the slave should be bound to itself, but since it has
122 # no maps yet, we can't get a maplist from it).
123 if [ "${SERVERTYPE}" = "SLAVE" ];
124 then
125 COUNT=`${YPWHICH} -d ${DOMAIN} -m 2>/dev/null | grep -i ${MASTER} | wc -l | tr -d " "`
126 if [ "$COUNT" = "0" ]
127 then
128 echo "Can't enumerate maps from ${MASTER}. Please check that it is running." 1>&2
129 echo "Note: using hardcoded maplist for map transfers." 1>&2
130 YPMAPLIST=${MAPLIST}
131 else
132 YPMAPLIST=`${YPWHICH} -d ${DOMAIN} -m | cut -d\ -f1`
134 echo "" 1>&2
137 # Check if user is root
138 ID=`id -u`
139 if [ "${ID}" != "0" ]; then
140 echo "You have to be the superuser to run this. Please login as root." 1>&2
141 exit 1
144 # Check if the YP directory exists.
146 if [ ! -d ${YP_DIR} -o -f ${YP_DIR} ]
147 then
148 echo "The directory ${YP_DIR} doesn't exist. Restore it from the distribution." 1>&2
149 exit 1
153 echo -n "Server Type: ${SERVERTYPE} Domain: ${DOMAIN}"
154 if [ "${SERVERTYPE}" = "SLAVE" ]; then
155 echo -n " Master: ${MASTER}"
157 echo ""
159 if [ "${SERVERTYPE}" != "UPDATE" ];
160 then
161 cat << \__notice1
163 Creating an YP server will require that you answer a few questions.
164 Questions will all be asked at the beginning of the procedure.
166 __notice1
168 echo -n "Do you want this procedure to quit on non-fatal errors? [y/n: n] "
169 read DOEXIT
171 case ${DOEXIT} in
172 y*|Y*)
173 ERROR_EXIT="YES"
176 *) ERROR_EXIT="NO"
177 echo ""
178 echo "Ok, please remember to go back and redo manually whatever fails."
179 echo "If you don't, something might not work. "
181 esac
183 if [ -d "${YP_DIR}/${DOMAIN}" ]; then
184 echo ""
185 echo -n "Can we destroy the existing ${YP_DIR}/${DOMAIN} and its contents? [y/n: n] "
186 read KILL
188 ERROR=
189 case ${KILL} in
190 y*|Y*)
191 ERROR="DELETE"
194 *) ERROR=
196 esac
198 if [ "${ERROR}" = "DELETE" ]; then
199 if ! rm -rf ${YP_DIR}/${DOMAIN}; then
200 echo "Can't clean up old directory ${YP_DIR}/${DOMAIN}." 1>&2
201 exit 1
203 else
204 echo "OK, please clean it up by hand and start again. Bye"
205 exit 0
210 if ! mkdir "${YP_DIR}/${DOMAIN}"; then
211 echo "Can't make new directory ${YP_DIR}/${DOMAIN}." 1>&2
212 exit 1
216 if [ "${SERVERTYPE}" = "MASTER" ];
217 then
219 if [ ! -f ${YP_DIR}/Makefile ]
220 then
221 if [ ! -f ${YP_DIR}/Makefile.dist ]
222 then
223 echo "Can't find ${YP_DIR}/Makefile.dist. " 1>&2
224 exit 1
226 cp ${YP_DIR}/Makefile.dist ${YP_DIR}/Makefile
231 if [ "${SERVERTYPE}" = "SLAVE" ];
232 then
234 echo "There will be no further questions. The remainder of the procedure"
235 echo "should take a few minutes, to copy the databases from ${MASTER}."
237 for MAP in ${YPMAPLIST}
239 echo "Transferring ${MAP}..."
240 if ! ${YPXFR} -p ${YP_DIR} -h ${MASTER} -c -d ${DOMAIN} ${MAP}; then
241 echo "Can't transfer map ${MAP}." 1>&2
242 ERROR_EXISTS="YES"
243 if [ "${ERROR_EXIT}" = "YES" ]; then
244 exit 1
247 done
249 echo ""
250 if [ "${ERROR_EXISTS}" = "YES" ]; then
251 echo "${HOST} has been setup as an YP slave server with errors. " 1>&2
252 echo "Please remember fix any problem that occurred." 1>&2
253 else
254 echo "${HOST} has been setup as an YP slave server without any errors. "
257 echo "Don't forget to update map ypservers on ${MASTER}."
258 exit 0
261 LIST_OK="NO"
263 while [ "${LIST_OK}" = "NO" ];
266 if [ "${SERVERTYPE}" = "MASTER" ];
267 then
268 HOST_LIST="${HOST}"
269 echo ""
270 echo "At this point, we have to construct a list of this domains YP servers."
271 echo "${HOST} is already known as master server."
272 echo "Please continue to add any slave servers, one per line. When you are"
273 echo "done with the list, type a <control D>."
274 echo " master server : ${HOST}"
277 if [ "${SERVERTYPE}" = "UPDATE" ];
278 then
279 HOST_LIST="${HOST}"
280 NEW_LIST=""
281 MASTER_NAME=""
282 SHORT_HOST=`echo ${HOST} | cut -d. -f1`
283 if [ -f ${YP_DIR}/${DOMAIN}/ypservers ];
284 then
285 for srv in `${MAKEDBM} -u ${YP_DIR}/${DOMAIN}/ypservers | grep -v "^YP" | tr "\t" " " | cut -d\ -f1`;
287 short_srv=`echo ${srv} | cut -d. -f1`
288 if [ "${SHORT_HOST}" != "${short_srv}" ]
289 then
290 if [ "${NEW_LIST}" = "" ];
291 then
292 NEW_LIST="${srv}"
293 else
294 NEW_LIST="${NEW_LIST} ${srv}"
297 done;
298 MASTER_NAME=`${MAKEDBM} -u ${YP_DIR}/${DOMAIN}/ypservers | grep "^YP_MASTER_NAME" | tr "\t" " " | cut -d\ -f2`
300 echo ""
301 echo "Update the list of hosts running YP servers in domain ${DOMAIN}."
302 echo "Master for this domain is ${MASTER_NAME}."
303 echo ""
304 echo "First verify old servers, type \\ to remove a server."
305 echo "Then add new servers, one per line. When done type a <control D>."
306 echo ""
307 echo " master server : ${HOST}"
308 if [ "${NEW_LIST}" != "" ]; then
309 for node in $NEW_LIST; do
310 echo -n " verify host : [${node}] "
311 read verify
312 if [ "${verify}" != "\\" ]; then
313 HOST_LIST="${HOST_LIST} ${node}"
315 done;
319 echo -n " next host to add: "
321 while read h
323 echo -n " next host to add: "
324 HOST_LIST="${HOST_LIST} ${h}"
325 done
327 echo ""
328 echo "The current list of NIS servers looks like this:"
329 echo ""
331 for h in `echo ${HOST_LIST}`;
333 echo ${h}
334 done
336 echo ""
337 echo -n "Is this correct? [y/n: y] "
338 read hlist_ok
340 case $hlist_ok in
341 n*) echo "Let's try the whole thing again...";;
342 N*) echo "Let's try the whole thing again...";;
343 *) LIST_OK="YES";;
344 esac
346 done
348 echo "Building ${YP_DIR}/${DOMAIN}/ypservers..."
349 rm -f ${YP_DIR}/ypservers
350 touch -f ${YP_DIR}/ypservers
351 rm -f ${YP_DIR}/${DOMAIN}/ypservers
352 for host in ${HOST_LIST};
354 echo "${host} ${host}" >> ${YP_DIR}/ypservers
355 echo "${host} ${host}"
356 done | ${MAKEDBM} - ${YP_DIR}/${DOMAIN}/ypservers
358 if [ $? -ne 0 ]; then
359 echo "" 1>&2
360 echo "Couldn't build yp data base ${YP_DIR}/${DOMAIN}/ypservers." 1>&2
361 ERROR_EXISTS="YES"
362 if [ "${ERROR_EXIT}" = "YES" ]; then
363 exit 1
367 if [ "${SERVERTYPE}" = "MASTER" ]; then
369 CUR_PWD=`pwd`
370 cd ${YP_DIR}
371 echo "Running ${YP_DIR}/Makefile..."
372 if ! make NOPUSH=True UPDATE_DOMAIN=${DOMAIN} YP_DIR=${YP_DIR}; then
373 echo "" 1>&2
374 echo "Error running Makefile." 1>&2
375 ERROR_EXISTS="YES"
376 if [ "${ERROR_EXIT}" = "YES" ]; then
377 exit 1
381 cd ${CUR_PWD}
383 echo ""
384 if [ "${ERROR_EXISTS}" = "YES" ]; then
385 echo "${HOST} has been setup as an YP master server with errors. " 1>&2
386 echo "Please remember fix any problem that occurred." 1>&2
387 else
388 echo "${HOST} has been setup as an YP master server without any errors. "