1 1.1 thorpej #!/bin/sh 2 1.1 thorpej # 3 1.15 andvar # $NetBSD: ypinit.sh,v 1.15 2021/08/17 22:00:33 andvar Exp $ 4 1.1 thorpej # 5 1.1 thorpej # ypinit.sh - setup a master or slave YP server 6 1.1 thorpej # 7 1.1 thorpej # Originally written by Mats O Jansson <moj (at] stacken.kth.se> 8 1.11 grant # Modified by Jason R. Thorpe <thorpej (at] NetBSD.org> 9 1.11 grant # Reworked by Luke Mewburn <lukem (at] NetBSD.org> 10 1.1 thorpej # 11 1.1 thorpej 12 1.7 lukem PATH=/bin:/usr/sbin:/usr/bin:${PATH} 13 1.13 kre BIN_DOMAINNAME=/bin/domainname 14 1.13 kre BIN_HOSTNAME=/bin/hostname 15 1.7 lukem ID=/usr/bin/id 16 1.8 lukem INSTALL=/usr/bin/install 17 1.7 lukem MAKEDBM=/usr/sbin/makedbm 18 1.6 lukem YPWHICH=/usr/bin/ypwhich 19 1.6 lukem YPXFR=/usr/sbin/ypxfr 20 1.1 thorpej 21 1.13 kre progname=$( basename $0 ) 22 1.7 lukem yp_dir=/var/yp 23 1.13 kre tmpfile=$(mktemp /tmp/ypservers.XXXXXX) || exit 1 24 1.9 lukem trap "rm -f ${tmpfile} ; exit 0" EXIT INT QUIT 25 1.1 thorpej 26 1.4 lukem umask 077 # protect created directories 27 1.4 lukem 28 1.13 kre if [ $( ${ID} -u ) != 0 ]; then 29 1.7 lukem echo 1>&2 "$progname: you must be root to run this" 30 1.7 lukem exit 1 31 1.7 lukem fi 32 1.7 lukem 33 1.14 kre while getopts cl:ms: i 34 1.14 kre do 35 1.14 kre case $i in 36 1.14 kre c) 37 1.14 kre servertype=client 38 1.14 kre ;; 39 1.14 kre l) 40 1.14 kre noninteractive=yes 41 1.14 kre serverlist=${OPTARG} 42 1.14 kre ;; 43 1.14 kre m) 44 1.14 kre servertype=master 45 1.14 kre ;; 46 1.14 kre s) 47 1.14 kre servertype=slave 48 1.14 kre master=${OPTARG} 49 1.14 kre ;; 50 1.14 kre \?) 51 1.14 kre echo >&2 # "Illegal option" message issued by getopts 52 1.14 kre servertype= # force usage message just below 53 1.14 kre break 54 1.14 kre ;; 55 1.14 kre esac 56 1.14 kre done 57 1.14 kre shift $((OPTIND - 1)) 58 1.8 lukem 59 1.14 kre if [ $# -eq 1 ]; then 60 1.14 kre domain=${1} 61 1.14 kre shift; 62 1.14 kre else 63 1.14 kre domain=$( ${BIN_DOMAINNAME} ) 64 1.8 lukem fi 65 1.1 thorpej 66 1.8 lukem if [ -z ${servertype} ]; then 67 1.13 kre cat 1>&2 << __usage 68 1.14 kre usage: ${progname} -c [-l server1,...,serverN] [domainname] 69 1.14 kre ${progname} -m [-l server1,...,serverN] [domainname] 70 1.14 kre ${progname} -s master_server [-l server1,...,serverN] [domainname] 71 1.8 lukem 72 1.8 lukem The \`-c' flag sets up a YP client, the \`-m' flag builds a master YP 73 1.8 lukem server, and the \`-s' flag builds a slave YP server. When building a 74 1.8 lukem slave YP server, \`master_server' must be an existing, reachable YP server. 75 1.1 thorpej __usage 76 1.1 thorpej exit 1 77 1.1 thorpej fi 78 1.1 thorpej 79 1.1 thorpej # Check if domainname is set, don't accept an empty domainname 80 1.7 lukem if [ -z "${domain}" ]; then 81 1.7 lukem cat << __no_domain 1>&2 82 1.7 lukem $progname: The local host's YP domain name has not been set. 83 1.7 lukem Please set it with the domainname(1) command or pass the domain as 84 1.7 lukem an argument to ${progname}. 85 1.1 thorpej __no_domain 86 1.1 thorpej 87 1.1 thorpej exit 1 88 1.1 thorpej fi 89 1.1 thorpej 90 1.1 thorpej # Check if hostname is set, don't accept an empty hostname 91 1.13 kre host=$( ${BIN_HOSTNAME} ) 92 1.7 lukem if [ -z "${host}" ]; then 93 1.7 lukem cat 1>&2 << __no_hostname 94 1.7 lukem $progname: The local host's hostname has not been set. 95 1.7 lukem Please set it with the hostname(1) command. 96 1.1 thorpej __no_hostname 97 1.1 thorpej 98 1.1 thorpej exit 1 99 1.1 thorpej fi 100 1.14 kre if [ "${servertype}" = slave ] && [ "${host}" = "${master}" ]; then 101 1.8 lukem echo 1>&2 \ 102 1.8 lukem "$progname: cannot setup a YP slave server off the local host." 103 1.8 lukem exit 1 104 1.8 lukem fi 105 1.1 thorpej 106 1.1 thorpej # Check if the YP directory exists. 107 1.14 kre if ! [ -d "${yp_dir}" ]; then 108 1.7 lukem cat 1>&2 << __no_dir 109 1.7 lukem $progname: The directory ${yp_dir} does not exist. 110 1.7 lukem Restore it from the distribution. 111 1.7 lukem __no_dir 112 1.7 lukem 113 1.1 thorpej exit 1 114 1.1 thorpej fi 115 1.1 thorpej 116 1.7 lukem echo "Server type: ${servertype}" 117 1.7 lukem echo "Domain: ${domain}" 118 1.8 lukem if [ "${servertype}" = "slave" ]; then 119 1.7 lukem echo "Master: ${master}" 120 1.2 thorpej fi 121 1.2 thorpej echo "" 122 1.8 lukem 123 1.8 lukem binding_dir=${yp_dir}/binding 124 1.14 kre if ! [ -d ${binding_dir} ]; then 125 1.12 tron cat 1>&2 << __no_dir 126 1.8 lukem $progname: The directory ${binding_dir} does not exist. 127 1.8 lukem Restore it from the distribution. 128 1.8 lukem __no_dir 129 1.8 lukem exit 1 130 1.8 lukem fi 131 1.8 lukem 132 1.10 garbled if [ -z "${noninteractive}" ]; then 133 1.10 garbled cat << __client_setup 134 1.8 lukem A YP client needs a list of YP servers to bind to. 135 1.8 lukem Whilst ypbind supports -broadcast, its use is not recommended. 136 1.8 lukem __client_setup 137 1.8 lukem 138 1.10 garbled done= 139 1.10 garbled while [ -z "${done}" ]; do 140 1.10 garbled > ${tmpfile} 141 1.10 garbled cat <<__list_of_servers 142 1.8 lukem 143 1.8 lukem Please enter a list of YP servers, in order of preference. 144 1.8 lukem When finished, press RETURN on a blank line or enter EOF. 145 1.8 lukem 146 1.8 lukem __list_of_servers 147 1.8 lukem 148 1.14 kre if [ "${servertype}" != client ]; then 149 1.14 kre echo "${host}" >> ${tmpfile} 150 1.10 garbled echo " next host: ${host}"; 151 1.10 garbled fi 152 1.10 garbled echo -n " next host: "; 153 1.10 garbled 154 1.14 kre while read nextserver && test -n "${nextserver}" 155 1.10 garbled do 156 1.10 garbled echo ${nextserver} >> ${tmpfile} 157 1.10 garbled echo -n " next host: "; 158 1.10 garbled done 159 1.10 garbled 160 1.10 garbled if [ -s ${tmpfile} ]; then 161 1.10 garbled echo "" 162 1.10 garbled echo "The current servers are:" 163 1.10 garbled echo "" 164 1.10 garbled cat ${tmpfile} 165 1.10 garbled echo "" 166 1.10 garbled echo -n "Is this correct? [y/n: n] " 167 1.10 garbled read DONE 168 1.10 garbled case ${DONE} in 169 1.10 garbled y*|Y*) 170 1.10 garbled done=yes 171 1.10 garbled ;; 172 1.10 garbled esac 173 1.10 garbled else 174 1.10 garbled echo "" 175 1.10 garbled echo "You have not supplied any servers." 176 1.10 garbled fi 177 1.10 garbled if [ -z "${done}" ]; then 178 1.10 garbled echo -n "Do you wish to abort? [y/n: n] " 179 1.10 garbled read ABORT 180 1.10 garbled case ${ABORT} in 181 1.10 garbled y*|Y*) 182 1.10 garbled exit 0 183 1.10 garbled ;; 184 1.10 garbled esac 185 1.10 garbled fi 186 1.10 garbled done 187 1.15 andvar else # interactive 188 1.8 lukem if [ "${servertype}" != "client" ]; then 189 1.8 lukem echo ${host} >> ${tmpfile} 190 1.8 lukem fi 191 1.10 garbled echo "${serverlist}" | sed -e 's/,/\ 192 1.10 garbled /g' >> ${tmpfile} 193 1.10 garbled #the above newline is required 194 1.10 garbled echo "" 195 1.10 garbled echo "The current servers are:" 196 1.10 garbled echo "" 197 1.10 garbled cat ${tmpfile} 198 1.10 garbled echo "" 199 1.10 garbled fi # interactive 200 1.8 lukem 201 1.8 lukem if [ -s ${tmpfile} ]; then 202 1.8 lukem ${INSTALL} -c -m 0444 ${tmpfile} ${binding_dir}/${domain}.ypservers 203 1.8 lukem fi 204 1.8 lukem 205 1.8 lukem if [ "${servertype}" = "client" ]; then 206 1.8 lukem exit 0 207 1.8 lukem fi 208 1.8 lukem 209 1.7 lukem cat << __notice1 210 1.1 thorpej 211 1.7 lukem Installing the YP database may require that you answer a few questions. 212 1.6 lukem Any configuration questions will be asked at the beginning of the procedure. 213 1.1 thorpej 214 1.1 thorpej __notice1 215 1.1 thorpej 216 1.7 lukem if [ -d "${yp_dir}/${domain}" ]; then 217 1.7 lukem echo "Can we destroy the existing ${yp_dir}/${domain}" 218 1.7 lukem echo -n "and its contents? [y/n: n] " 219 1.1 thorpej read KILL 220 1.1 thorpej 221 1.1 thorpej case ${KILL} in 222 1.2 thorpej y*|Y*) 223 1.7 lukem rm -rf ${yp_dir}/${domain} 224 1.7 lukem if [ $? != 0 ]; then 225 1.7 lukem echo 1>&2 \ 226 1.7 lukem "$progname: Can't clean up old directory ${yp_dir}/${domain}" 227 1.1 thorpej exit 1 228 1.1 thorpej fi 229 1.8 lukem ;; 230 1.8 lukem 231 1.8 lukem *) 232 1.2 thorpej echo "OK, please clean it up by hand and start again." 233 1.2 thorpej exit 0 234 1.8 lukem ;; 235 1.8 lukem esac 236 1.1 thorpej fi 237 1.1 thorpej 238 1.7 lukem if ! mkdir "${yp_dir}/${domain}"; then 239 1.7 lukem echo 1>&2 "$progname: Can't make new directory ${yp_dir}/${domain}" 240 1.1 thorpej exit 1 241 1.1 thorpej fi 242 1.1 thorpej 243 1.7 lukem case ${servertype} in 244 1.7 lukem master) 245 1.14 kre if ! [ -f ${yp_dir}/Makefile ]; then 246 1.14 kre if ! [ -f ${yp_dir}/Makefile.main ]; then 247 1.7 lukem echo 1>&2 \ 248 1.7 lukem "$progname: Can't find ${yp_dir}/Makefile.main" 249 1.1 thorpej exit 1 250 1.1 thorpej fi 251 1.7 lukem cp ${yp_dir}/Makefile.main ${yp_dir}/Makefile 252 1.1 thorpej fi 253 1.1 thorpej 254 1.13 kre subdir=$(grep "^SUBDIR=" ${yp_dir}/Makefile) 255 1.1 thorpej 256 1.7 lukem if [ -z "${subdir}" ]; then 257 1.7 lukem echo 1>&2 \ 258 1.7 lukem "$progname: Can't find line starting with 'SUBDIR=' in ${yp_dir}/Makefile" 259 1.1 thorpej exit 1 260 1.1 thorpej fi 261 1.1 thorpej 262 1.7 lukem newsubdir="SUBDIR=" 263 1.13 kre for dir in $(echo ${subdir} | cut -c8-255); do 264 1.7 lukem if [ "${dir}" != "${domain}" ]; then 265 1.7 lukem newsubdir="${newsubdir} ${dir}" 266 1.1 thorpej fi 267 1.1 thorpej done 268 1.7 lukem newsubdir="${newsubdir} ${domain}" 269 1.1 thorpej 270 1.13 kre if [ -f ${yp_dir}/Makefile.tmp ]; then 271 1.7 lukem rm ${yp_dir}/Makefile.tmp 272 1.1 thorpej fi 273 1.1 thorpej 274 1.7 lukem mv ${yp_dir}/Makefile ${yp_dir}/Makefile.tmp 275 1.7 lukem sed -e "s/^${subdir}/${newsubdir}/" ${yp_dir}/Makefile.tmp > \ 276 1.7 lukem ${yp_dir}/Makefile 277 1.7 lukem rm ${yp_dir}/Makefile.tmp 278 1.1 thorpej 279 1.14 kre if ! [ -f ${yp_dir}/Makefile.yp ]; then 280 1.7 lukem echo 1>&2 "$progname: Can't find ${yp_dir}/Makefile.yp" 281 1.1 thorpej exit 1 282 1.1 thorpej fi 283 1.1 thorpej 284 1.7 lukem cp ${yp_dir}/Makefile.yp ${yp_dir}/${domain}/Makefile 285 1.1 thorpej 286 1.7 lukem # Create `ypservers' with own name, so that yppush won't 287 1.1 thorpej # lose when we run "make". 288 1.1 thorpej ( 289 1.7 lukem cd ${yp_dir}/${domain} 290 1.7 lukem echo "$host $host" > ypservers 291 1.6 lukem ${MAKEDBM} ypservers ypservers 292 1.1 thorpej ) 293 1.2 thorpej 294 1.7 lukem echo "Done. Be sure to run \`make' in ${yp_dir}." 295 1.7 lukem 296 1.7 lukem ;; 297 1.1 thorpej 298 1.7 lukem slave) 299 1.1 thorpej echo "" 300 1.1 thorpej 301 1.13 kre maps=$( ${YPWHICH} -d ${domain} -h ${master} -f -m 2>/dev/null | 302 1.7 lukem awk '{ if (substr($2, 1, length("'$master'")) == "'$master'") \ 303 1.13 kre print $1; }' ) 304 1.7 lukem 305 1.7 lukem if [ -z "${maps}" ]; then 306 1.7 lukem cat 1>&2 << __no_maps 307 1.7 lukem $progname: Can't find any maps for ${domain} on ${master} 308 1.7 lukem Please check that the appropriate YP service is running. 309 1.7 lukem __no_maps 310 1.7 lukem exit 1 311 1.7 lukem fi 312 1.7 lukem 313 1.7 lukem for map in ${maps}; do 314 1.7 lukem echo "Transferring ${map}..." 315 1.7 lukem if ! ${YPXFR} -h ${master} -c -d ${domain} ${map}; then 316 1.7 lukem echo 1>&2 "$progname: Can't transfer map ${map}" 317 1.1 thorpej exit 1 318 1.1 thorpej fi 319 1.1 thorpej done 320 1.1 thorpej 321 1.7 lukem cat << __dont_forget 322 1.7 lukem 323 1.7 lukem Don't forget to update the \`ypservers' on ${master}, 324 1.7 lukem by adding an entry similar to: 325 1.7 lukem ${host} ${host} 326 1.7 lukem 327 1.7 lukem __dont_forget 328 1.1 thorpej exit 0 329 1.7 lukem 330 1.7 lukem ;; 331 1.7 lukem 332 1.7 lukem *) 333 1.7 lukem echo 1>&2 "$progname: unknown servertype \`${servertype}'" 334 1.7 lukem exit 1 335 1.7 lukem esac 336