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