2 # $FreeBSD: src/usr.sbin/ypserv/ypinit.sh,v 1.4 2002/12/30 21:18:15 schweikh Exp $
4 # ypinit.sh - setup a master or slave server.
5 # (Taken from OpenBSD and modified for FreeBSD.)
7 DOMAINNAME
=/bin
/domainname
9 YPWHICH
=/usr
/bin
/ypwhich
10 YPXFR
=/usr
/libexec
/ypxfr
12 MAKEDBM
=/usr
/sbin
/yp_mkdb
13 MAPLIST
="master.passwd.byname master.passwd.byuid passwd.byname passwd.byuid \
14 group.byname group.bygid hosts.byname hosts.byaddr services.byname \
15 rpc.byname rpc.bynumber networks.byname networks.byaddr netgroup \
16 netgroup.byuser netgroup.byhost netid.byname publickey.byname \
17 bootparams ethers.byname ethers.byaddr mail.aliases \
18 ypservers protocols.byname protocols.bynumber netmasks.byaddr"
25 ERROR
=USAGE
# assume usage error
29 if [ $1 = "-m" ] # ypinit -m
31 DOMAIN
=`${DOMAINNAME}`
36 if [ $1 = "-u" ] # ypinit -u
38 DOMAIN
=`${DOMAINNAME}`
46 if [ $1 = "-m" ] # ypinit -m domainname
53 if [ $1 = "-s" ] # ypinit -s master_server
55 DOMAIN
=`${DOMAINNAME}`
61 if [ $1 = "-u" ] # ypinit -u domainname
71 if [ $1 = "-s" ] # ypinit -s master_server domainname
80 if [ "${ERROR}" = "USAGE" ]; then
82 usage
: ypinit
-m [domainname
]
83 ypinit
-s master_server
[domainname
]
84 ypinit
-u [domainname
]
86 The
`-m' flag builds a master YP server, and the `-s' flag builds
87 a slave YP server. When building a slave YP server, `master_server'
88 must be an existing
, reachable YP server.
89 The
`-u' is for updating the ypservers map on a master server.
95 # Check if domainname is set, don't accept an empty domainname
96 if [ -z "${DOMAIN}" ]; then
97 cat << \__no_domain 1>&2
98 The local host's YP domain name has not been set. Please set it with
99 the domainname(1) command or pass the domain as an argument to ypinit(8).
105 # Check if hostname is set, don't accept an empty hostname
107 if [ -z "${HOST}" ]; then
108 cat << \__no_hostname 1>&2
109 The local host's hostname has not been set. Please set it with the
116 # Check if we have contact with master.
117 # If we can't list the maps on the master, then we fake it with a
118 # hard-coded list of maps. The FreeBSD ypxfr command will work even
119 # if ypbind isn't running or if we are bound to ourselves instead of
120 # the master (the slave should be bound to itself, but since it has
121 # no maps yet, we can't get a maplist from it).
122 if [ "${SERVERTYPE}" = "SLAVE" ];
124 COUNT=`${YPWHICH} -d ${DOMAIN} -m 2>/dev/null | grep -i ${MASTER} |
wc -l |
tr -d " "`
125 if [ "$COUNT" = "0" ]
127 echo "Can't enumerate maps from ${MASTER}. Please check that it is running." 1>&2
128 echo "Note: using hardcoded maplist for map transfers." 1>&2
131 YPMAPLIST=`${YPWHICH} -d ${DOMAIN} -m | cut
-d\
-f1`
136 # Check if user is root
138 if [ "${ID}" != "0" ]; then
139 echo "You have to be the superuser to run this. Please login as root." 1>&2
143 # Check if the YP directory exists.
145 if [ ! -d ${YP_DIR} -o -f ${YP_DIR} ]
147 echo "The directory ${YP_DIR} doesn't exist. Restore it from the distribution." 1>&2
152 echo -n "Server Type: ${SERVERTYPE} Domain: ${DOMAIN}"
153 if [ "${SERVERTYPE}" = "SLAVE" ]; then
154 echo -n " Master: ${MASTER}"
158 if [ "${SERVERTYPE}" != "UPDATE" ];
162 Creating an YP server will require that you answer a few questions.
163 Questions will all be asked at the beginning of the procedure.
167 echo -n "Do you want this procedure to quit on non-fatal errors? [y/n: n] "
177 echo "Ok, please remember to go back and redo manually whatever fails."
178 echo "If you don't, something might not work. "
182 if [ -d "${YP_DIR}/${DOMAIN}" ]; then
184 echo -n "Can we destroy the existing ${YP_DIR}/${DOMAIN} and its contents? [y/n: n] "
197 if [ "${ERROR}" = "DELETE" ]; then
198 if ! rm -rf ${YP_DIR}/${DOMAIN}; then
199 echo "Can't clean up old directory ${YP_DIR}/${DOMAIN}." 1>&2
203 echo "OK, please clean it up by hand and start again. Bye"
208 if ! mkdir "${YP_DIR}/${DOMAIN}"; then
209 echo "Can't make new directory ${YP_DIR}/${DOMAIN}." 1>&2
214 if [ "${SERVERTYPE}" = "MASTER" ];
217 if [ ! -f ${YP_DIR}/Makefile ]
219 if [ ! -f ${YP_DIR}/Makefile.dist ]
221 echo "Can't find ${YP_DIR}/Makefile.dist. " 1>&2
224 cp ${YP_DIR}/Makefile.dist ${YP_DIR}/Makefile
229 if [ "${SERVERTYPE}" = "SLAVE" ];
232 echo "There will be no further questions. The remainder of the procedure"
233 echo "should take a few minutes, to copy the databases from ${MASTER}."
235 for MAP in ${YPMAPLIST}
237 echo "Transferring ${MAP}..."
238 if ! ${YPXFR} -p ${YP_DIR} -h ${MASTER} -c -d ${DOMAIN} ${MAP}; then
239 echo "Can't transfer map ${MAP}." 1>&2
241 if [ "${ERROR_EXIT}" = "YES" ]; then
248 if [ "${ERROR_EXISTS}" = "YES" ]; then
249 echo "${HOST} has been setup as an YP slave server with errors. " 1>&2
250 echo "Please remember fix any problem that occurred." 1>&2
252 echo "${HOST} has been setup as an YP slave server without any errors. "
255 echo "Don't forget to update map ypservers on ${MASTER}."
261 while [ "${LIST_OK}" = "NO" ];
263 if [ "${SERVERTYPE}" = "MASTER" ];
267 echo "At this point, we have to construct a list of this domains YP servers."
268 echo "${HOST} is already known as master server."
269 echo "Please continue to add any slave servers, one per line. When you are"
270 echo "done with the list, type a <control D>."
271 echo " master server : ${HOST}"
274 if [ "${SERVERTYPE}" = "UPDATE" ];
279 SHORT_HOST=`echo ${HOST} | cut
-d.
-f1`
280 if [ -f ${YP_DIR}/${DOMAIN}/ypservers ];
282 for srv in `${MAKEDBM} -u ${YP_DIR}/${DOMAIN}/ypservers |
grep -v "^YP" |
tr "\t" " " | cut
-d\
-f1`;
284 short_srv=`echo ${srv} | cut
-d.
-f1`
285 if [ "${SHORT_HOST}" != "${short_srv}" ]
287 if [ "${NEW_LIST}" = "" ];
291 NEW_LIST="${NEW_LIST} ${srv}"
295 MASTER_NAME=`${MAKEDBM} -u ${YP_DIR}/${DOMAIN}/ypservers |
grep "^YP_MASTER_NAME" |
tr "\t" " " | cut
-d\
-f2`
298 echo "Update the list of hosts running YP servers in domain ${DOMAIN}."
299 echo "Master for this domain is ${MASTER_NAME}."
301 echo "First verify old servers, type \\ to remove a server."
302 echo "Then add new servers, one per line. When done type a <control D>."
304 echo " master server : ${HOST}"
305 if [ "${NEW_LIST}" != "" ]; then
306 for node in $NEW_LIST; do
307 echo -n " verify host : [${node}] "
309 if [ "${verify}" != "\\" ]; then
310 HOST_LIST="${HOST_LIST} ${node}"
316 echo -n " next host to add: "
320 echo -n " next host to add: "
321 HOST_LIST="${HOST_LIST} ${h}"
325 echo "The current list of NIS servers looks like this:"
328 for h in `echo ${HOST_LIST}`;
334 echo -n "Is this correct? [y/n: y] "
338 n*) echo "Let's try the whole thing again...";;
339 N*) echo "Let's try the whole thing again...";;
345 echo "Building ${YP_DIR}/${DOMAIN}/ypservers..."
346 rm -f ${YP_DIR}/ypservers
347 touch -f ${YP_DIR}/ypservers
348 rm -f ${YP_DIR}/${DOMAIN}/ypservers
349 for host in ${HOST_LIST};
351 echo "${host} ${host}" >> ${YP_DIR}/ypservers
352 echo "${host} ${host}"
353 done | ${MAKEDBM} - ${YP_DIR}/${DOMAIN}/ypservers
355 if [ $? -ne 0 ]; then
357 echo "Couldn't build yp data base ${YP_DIR}/${DOMAIN}/ypservers." 1>&2
359 if [ "${ERROR_EXIT}" = "YES" ]; then
364 if [ "${SERVERTYPE}" = "MASTER" ]; then
367 echo "Running ${YP_DIR}/Makefile..."
368 if ! make NOPUSH=True UPDATE_DOMAIN=${DOMAIN} YP_DIR=${YP_DIR}; then
370 echo "Error running Makefile." 1>&2
372 if [ "${ERROR_EXIT}" = "YES" ]; then
380 if [ "${ERROR_EXISTS}" = "YES" ]; then
381 echo "${HOST} has been setup as an YP master server with errors. " 1>&2
382 echo "Please remember fix any problem that occurred." 1>&2
384 echo "${HOST} has been setup as an YP master server without any errors. "