1#!/bin/sh 2# $Id: ypinit.sh,v 1.2 2002/03/26 14:38:48 epeyton Exp $ 3# 4# ypinit.sh - setup an master or slave server. 5# 6DOMAINNAME=/bin/domainname 7HOSTNAME=/bin/hostname 8YPWHICH=/usr/bin/ypwhich 9YPXFR=/usr/sbin/ypxfr 10YP_DIR=/var/yp 11MAKEDBM=/usr/sbin/makedbm 12ERROR_EXISTS="NO" 13MAKE=bsdmake 14umask 077 15 16#set -xv 17 18ERROR=USAGE # assume usage error 19 20if [ $# -eq 1 ] 21then 22 if [ $1 = "-m" ] # ypinit -m 23 then 24 DOMAIN=`${DOMAINNAME}` 25 SERVERTYPE=MASTER 26 ERROR= 27 fi 28 29 if [ $1 = "-u" ] # ypinit -u 30 then 31 DOMAIN=`${DOMAINNAME}` 32 SERVERTYPE=UPDATE 33 ERROR= 34 fi 35fi 36 37if [ $# -eq 2 ] 38then 39 if [ $1 = "-m" ] # ypinit -m domainname 40 then 41 DOMAIN=${2} 42 SERVERTYPE=MASTER 43 ERROR= 44 fi 45 46 if [ $1 = "-s" ] # ypinit -s master_server 47 then 48 DOMAIN=`${DOMAINNAME}` 49 SERVERTYPE=SLAVE 50 MASTER=${2} 51 ERROR= 52 fi 53 54 if [ $1 = "-u" ] # ypinit -u domainname 55 then 56 DOMAIN=${2} 57 SERVERTYPE=UPDATE 58 ERROR= 59 fi 60fi 61 62if [ $# -eq 3 ] 63then 64 if [ $1 = "-s" ] # ypinit -s master_server domainname 65 then 66 DOMAIN=${3} 67 SERVERTYPE=SLAVE 68 MASTER=${2} 69 ERROR= 70 fi 71fi 72 73if [ "${ERROR}" = "USAGE" ]; then 74 cat << \__usage 1>&2 75usage: ypinit -m [domainname] 76 ypinit -s master_server [domainname] 77 ypinit -u [domainname] 78 79The `-m' flag builds a master YP server, and the `-s' flag builds 80a slave YP server. When building a slave YP server, `master_server' 81must be an existing, reachable YP server. 82The `-u' is for updating the ypservers map on a master server. 83__usage 84 85 exit 1 86fi 87 88# Check if domainname is set, don't accept an empty domainname 89if [ -z "${DOMAIN}" ]; then 90 cat << \__no_domain 1>&2 91The local host's YP domain name has not been set. Please set it with 92the domainname(8) command or pass the domain as an argument to ypinit(8). 93__no_domain 94 95 exit 1 96fi 97 98# Check if hostname is set, don't accept an empty hostname 99HOST=`${HOSTNAME}` 100if [ -z "${HOST}" ]; then 101 cat << \__no_hostname 1>&2 102The local host's hostname has not been set. Please set it with the 103hostname(8) command. 104__no_hostname 105 106 exit 1 107fi 108 109# Check if we have contact with master. 110if [ "${SERVERTYPE}" = "SLAVE" ]; 111then 112 COUNT=`${YPWHICH} -d ${DOMAIN} -m 2>/dev/null | grep -i ${MASTER} | wc -l | tr -d " "` 113 if [ "$COUNT" = "0" ] 114 then 115 echo "Can't enumerate maps from ${MASTER}. Please check that it is running." 1>&2 116 exit 1 117 fi 118fi 119 120# Check if user is root 121ID=`id -u` 122if [ "${ID}" != "0" ]; then 123 echo "You have to be the superuser to run this. Please login as root." 1>&2 124 exit 1 125fi 126 127# Check if the YP directory exists. 128 129if [ ! -d ${YP_DIR} -o -f ${YP_DIR} ] 130then 131 echo "The directory ${YP_DIR} doesn't exist. Restore it from the distribution." 1>&2 132 exit 1 133 134fi 135 136echo -n "Server Type: ${SERVERTYPE} Domain: ${DOMAIN}" 137if [ "${SERVERTYPE}" = "SLAVE" ]; then 138 echo -n " Master: ${MASTER}" 139fi 140echo "" 141 142if [ "${SERVERTYPE}" != "UPDATE" ]; 143then 144 cat << \__notice1 145 146Creating an YP server will require that you answer a few questions. 147Questions will all be asked at the beginning of the procedure. 148 149__notice1 150 151 echo -n "Do you want this procedure to quit on non-fatal errors? [y/n: n] " 152 read DOEXIT 153 154 case ${DOEXIT} in 155 y*|Y*) 156 ERROR_EXIT="YES" 157 ;; 158 159 *) ERROR_EXIT="NO" 160 echo "" 161 echo "Ok, please remember to go back and redo manually whatever fails." 162 echo "If you don't, something might not work. " 163 ;; 164 esac 165 166 if [ -d "${YP_DIR}/${DOMAIN}" ]; then 167 echo "" 168 echo -n "Can we destroy the existing ${YP_DIR}/${DOMAIN} and its contents? [y/n: n] " 169 read KILL 170 171 ERROR= 172 case ${KILL} in 173 y*|Y*) 174 ERROR="DELETE" 175 ;; 176 177 *) ERROR= 178 ;; 179 esac 180 181 if [ "${ERROR}" = "DELETE" ]; then 182 if ! rm -rf ${YP_DIR}/${DOMAIN}; then 183 echo "Can't clean up old directory ${YP_DIR}/${DOMAIN}." 1>&2 184 exit 1 185 fi 186 else 187 echo "OK, please clean it up by hand and start again. Bye" 188 exit 0 189 fi 190 191 fi 192 193 if ! mkdir "${YP_DIR}/${DOMAIN}"; then 194 echo "Can't make new directory ${YP_DIR}/${DOMAIN}." 1>&2 195 exit 1 196 fi 197fi 198 199if [ "${SERVERTYPE}" = "MASTER" ]; 200then 201 202 if [ ! -f ${YP_DIR}/Makefile ] 203 then 204 if [ ! -f ${YP_DIR}/Makefile.main ] 205 then 206 echo "Can't find ${YP_DIR}/Makefile.main. " 1>&2 207 exit 1 208 fi 209 cp ${YP_DIR}/Makefile.main ${YP_DIR}/Makefile 210 fi 211 212 SUBDIR=`grep "^SUBDIR=" ${YP_DIR}/Makefile` 213 214 if [ -z "${SUBDIR}" ] 215 then 216 echo "Can't find line starting with 'SUBDIR=' in ${YP_DIR}/Makefile. " 1>&2 217 exit 1 218 fi 219 220 NEWSUBDIR="SUBDIR=" 221 for DIR in `echo ${SUBDIR} | cut -c8-255`; do 222 if [ ${DIR} != ${DOMAIN} ]; then 223 NEWSUBDIR="${NEWSUBDIR} ${DIR}" 224 fi 225 done 226 NEWSUBDIR="${NEWSUBDIR} ${DOMAIN}" 227 228 if [ -f ${YP_DIR}/Makefile.tmp ]; then 229 rm ${YP_DIR}/Makefile.tmp 230 fi 231 232 mv ${YP_DIR}/Makefile ${YP_DIR}/Makefile.tmp 233 sed -e "s/^${SUBDIR}/${NEWSUBDIR}/" ${YP_DIR}/Makefile.tmp > \ 234 ${YP_DIR}/Makefile 235 rm ${YP_DIR}/Makefile.tmp 236 237 if [ ! -f ${YP_DIR}/Makefile.yp ]; then 238 echo "Can't find ${YP_DIR}/Makefile.yp. " 1>&2 239 exit 1 240 fi 241 242 cp ${YP_DIR}/Makefile.yp ${YP_DIR}/${DOMAIN}/Makefile 243 244fi 245 246if [ "${SERVERTYPE}" = "SLAVE" ]; 247then 248 249 echo "There will be no further questions. The remainder of the procedure" 250 echo "should take a few minutes, to copy the databases from ${MASTER}." 251 252 for MAP in `${YPWHICH} -d ${DOMAIN} -m | cut -d\ -f1` 253 do 254 echo "Transfering ${MAP}..." 255 if ! ${YPXFR} -h ${MASTER} -c -d ${DOMAIN} ${MAP}; then 256 echo "Can't transfer map ${MAP}." 1>&2 257 ERROR_EXISTS="YES" 258 if [ "${ERROR_EXIT}" = "YES" ]; then 259 exit 1 260 fi 261 fi 262 done 263 264 echo "" 265 if [ "${ERROR_EXISTS}" = "YES" ]; then 266 echo "${HOST} has been setup as an YP slave server with errors. " 1>&2 267 echo "Please remember fix any problem that occurred." 1>&2 268 else 269 echo "${HOST} has been setup as an YP slave server without any errors. " 270 fi 271 272 echo "Don't forget to update map ypservers on ${MASTER}." 273 exit 0 274fi 275 276LIST_OK="NO" 277 278while [ "${LIST_OK}" = "NO" ]; 279do 280 281 if [ "${SERVERTYPE}" = "MASTER" ]; 282 then 283 HOST_LIST="${HOST}" 284 echo "" 285 echo "At this point, we have to construct a list of this domains YP servers." 286 echo "${HOST} is already known as master server." 287 echo "Please continue to add any slave servers, one per line. When you are" 288 echo "done with the list, type a <control D>." 289 echo " master server : ${HOST}" 290 fi 291 292 if [ "${SERVERTYPE}" = "UPDATE" ]; 293 then 294 HOST_LIST="${HOST}" 295 NEW_LIST="" 296 MASTER_NAME="" 297 SHORT_HOST=`echo ${HOST} | cut -d. -f1` 298 if [ -f ${YP_DIR}/${DOMAIN}/ypservers.db ]; 299 then 300 for srv in `${MAKEDBM} -u ${YP_DIR}/${DOMAIN}/ypservers | grep -v "^YP" | tr "\t" " " | cut -d\ -f1`; 301 do 302 short_srv=`echo ${srv} | cut -d. -f1` 303 if [ "${SHORT_HOST}" != "${short_srv}" ] 304 then 305 if [ "${NEW_LIST}" = "" ]; 306 then 307 NEW_LIST="${srv}" 308 else 309 NEW_LIST="${NEW_LIST} ${srv}" 310 fi 311 fi 312 done; 313 MASTER_NAME=`${MAKEDBM} -u ${YP_DIR}/${DOMAIN}/ypservers | grep "^YP_MASTER_NAME" | tr "\t" " " | cut -d\ -f2` 314 fi 315 echo "" 316 echo "Update the list of hosts running YP servers in domain ${DOMAIN}." 317 echo "Master for this domain is ${MASTER_NAME}." 318 echo "" 319 echo "First verify old servers, type \\ to remove a server." 320 echo "Then add new servers, one per line. When done type a <control D>." 321 echo "" 322 echo " master server : ${HOST}" 323 if [ "${NEW_LIST}" != "" ]; then 324 for node in $NEW_LIST; do 325 echo -n " verify host : [${node}] " 326 read verify 327 if [ "${verify}" != "\\" ]; then 328 HOST_LIST="${HOST_LIST} ${node}" 329 fi 330 done; 331 fi 332 fi 333 334 echo -n " next host to add: " 335 336 while read h 337 do 338 echo -n " next host to add: " 339 HOST_LIST="${HOST_LIST} ${h}" 340 done 341 342 echo "" 343 echo "The current list of NIS servers looks like this:" 344 echo "" 345 346 for h in `echo ${HOST_LIST}`; 347 do 348 echo ${h} 349 done 350 351 echo "" 352 echo -n "Is this correct? [y/n: y] " 353 read hlist_ok 354 355 case $hlist_ok in 356 n*) echo "Let's try the whole thing again...";; 357 N*) echo "Let's try the whole thing again...";; 358 *) LIST_OK="YES";; 359 esac 360 361done 362 363echo "Building ${YP_DIR}/${DOMAIN}/ypservers..." 364for host in ${HOST_LIST}; 365do 366 echo "${host} ${host}" 367done | ${MAKEDBM} - ${YP_DIR}/${DOMAIN}/ypservers 368 369if [ $? -ne 0 ]; then 370 echo "" 1>&2 371 echo "Couldn't build yp data base ${YP_DIR}/${DOMAIN}/ypservers." 1>&2 372 ERROR_EXISTS="YES" 373 if [ "${ERROR_EXIT}" = "YES" ]; then 374 exit 1 375 fi 376fi 377 378if [ "${SERVERTYPE}" = "MASTER" ]; then 379 380 CUR_PWD=`pwd` 381 cd ${YP_DIR}/${DOMAIN} 382 echo "Running ${YP_DIR}/${DOMAIN}/Makefile..." 383 if ! ${MAKE} NOPUSH=1; then 384 echo "" 1>&2 385 echo "Error running Makefile." 1>&2 386 ERROR_EXISTS="YES" 387 if [ "${ERROR_EXIT}" = "YES" ]; then 388 exit 1 389 fi 390 fi 391 392 cd ${CUR_PWD} 393 394 echo "" 395 if [ "${ERROR_EXISTS}" = "YES" ]; then 396 echo "${HOST} has been setup as an YP master server with errors. " 1>&2 397 echo "Please remember fix any problem that occurred." 1>&2 398 else 399 echo "${HOST} has been setup as an YP master server without any errors. " 400 fi 401 402fi 403