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