#!/bin/sh # $FreeBSD: stable/11/usr.sbin/ypserv/ypinit.sh 289677 2015-10-21 05:37:09Z eadler $ # # ypinit.sh - setup a master or slave server. # (Taken from OpenBSD and modified for FreeBSD.) # setglobal DOMAINNAME = '/bin/domainname' setglobal HOSTNAME = '/bin/hostname' setglobal YPWHICH = '/usr/bin/ypwhich' setglobal YPXFR = '/usr/libexec/ypxfr' setglobal YP_DIR = '/var/yp' setglobal MAKEDBM = '/usr/sbin/yp_mkdb' setglobal MAPLIST = '"master.passwd.byname master.passwd.byuid passwd.byname passwd.byuid \ group.byname group.bygid hosts.byname hosts.byaddr services.byname \ rpc.byname rpc.bynumber networks.byname networks.byaddr netgroup \ netgroup.byuser netgroup.byhost netid.byname publickey.byname \ bootparams ethers.byname ethers.byaddr eui64.byname eui64.byid \ amd.host mail.aliases ypservers protocols.byname protocols.bynumber \ netmasks.byaddr'" setglobal ERROR_EXISTS = '"NO'" umask 077 #set -xv setglobal ERROR = 'USAGE' # assume usage error if test $Argc -eq 1 { if test $1 = "-m" # ypinit -m { setglobal DOMAIN = $[$(DOMAINNAME)] setglobal SERVERTYPE = 'MASTER' setglobal ERROR = '' } if test $1 = "-u" # ypinit -u { setglobal DOMAIN = $[$(DOMAINNAME)] setglobal SERVERTYPE = 'UPDATE' setglobal ERROR = '' } } if test $Argc -eq 2 { if test $1 = "-m" # ypinit -m domainname { setglobal DOMAIN = $(2) setglobal SERVERTYPE = 'MASTER' setglobal ERROR = '' } if test $1 = "-s" # ypinit -s master_server { setglobal DOMAIN = $[$(DOMAINNAME)] setglobal SERVERTYPE = 'SLAVE' setglobal MASTER = $(2) setglobal ERROR = '' } if test $1 = "-u" # ypinit -u domainname { setglobal DOMAIN = $(2) setglobal SERVERTYPE = 'UPDATE' setglobal ERROR = '' } } if test $Argc -eq 3 { if test $1 = "-s" # ypinit -s master_server domainname { setglobal DOMAIN = $(3) setglobal SERVERTYPE = 'SLAVE' setglobal MASTER = $(2) setglobal ERROR = '' } } if test $(ERROR) = "USAGE" { cat << ''' 1>&2 usage: ypinit -m [domainname] ypinit -s master_server [domainname] ypinit -u [domainname] The `-m' flag builds a master YP server, and the `-s' flag builds a slave YP server. When building a slave YP server, `master_server' must be an existing, reachable YP server. The `-u' is for updating the ypservers map on a master server. ''' !1 > !2 usage: ypinit -m [domainname] ypinit -s master_server [domainname] ypinit -u [domainname] The `-m' flag builds a master YP server, and the `-s' flag builds a slave YP server. When building a slave YP server, `master_server' must be an existing, reachable YP server. The `-u' is for updating the ypservers map on a master server. __usage exit 1 } # Check if domainname is set, don't accept an empty domainname if test -z $(DOMAIN) { cat << ''' 1>&2 The local host's YP domain name has not been set. Please set it with the domainname(1) command or pass the domain as an argument to ypinit(8). ''' !1 > !2 The local host's YP domain name has not been set. Please set it with the domainname(1) command or pass the domain as an argument to ypinit(8). __no_domain exit 1 } # Check if hostname is set, don't accept an empty hostname setglobal HOST = $[$(HOSTNAME)] if test -z $(HOST) { cat << ''' 1>&2 The local host's hostname has not been set. Please set it with the hostname(1) command. ''' !1 > !2 The local host's hostname has not been set. Please set it with the hostname(1) command. __no_hostname exit 1 } # Check if we have contact with master. # If we can't list the maps on the master, then we fake it with a # hard-coded list of maps. The FreeBSD ypxfr command will work even # if ypbind isn't running or if we are bound to ourselves instead of # the master (the slave should be bound to itself, but since it has # no maps yet, we can't get a maplist from it). if test $(SERVERTYPE) = "SLAVE" { setglobal COUNT = $[$(YPWHICH) -d $(DOMAIN) -m !2 >/dev/null | grep -i $(MASTER) | wc -l | tr -d " ] if test $COUNT = "0" { echo "Can't enumerate maps from $(MASTER). Please check that it is running." !1 > !2 echo "Note: using hardcoded maplist for map transfers." !1 > !2 setglobal YPMAPLIST = $(MAPLIST) } else { setglobal YPMAPLIST = $[$(YPWHICH) -d $(DOMAIN) -m | cut -d' ' -f1] } echo "" !1 > !2 } # Check if user is root setglobal ID = $[id -u] if test $(ID) != "0" { echo "You have to be the superuser to run this. Please login as root." !1 > !2 exit 1 } # Check if the YP directory exists. if test ! -d $(YP_DIR) -o -f $(YP_DIR) { echo "The directory $(YP_DIR) doesn't exist. Restore it from the distribution." !1 > !2 exit 1 } echo -n "Server Type: $(SERVERTYPE) Domain: $(DOMAIN)" if test $(SERVERTYPE) = "SLAVE" { echo -n " Master: $(MASTER)" } echo "" if test $(SERVERTYPE) != "UPDATE" { cat << ''' Creating an YP server will require that you answer a few questions. Questions will all be asked at the beginning of the procedure. ''' echo -n "Do you want this procedure to quit on non-fatal errors? [y/n: n] " read DOEXIT match $(DOEXIT) { with y*|Y* setglobal ERROR_EXIT = '"YES'" with * setglobal ERROR_EXIT = '"NO'" echo "" echo "Ok, please remember to go back and redo manually whatever fails." echo "If you don't, something might not work. " } if test -d "$(YP_DIR)/$(DOMAIN)" { echo "" echo -n "Can we destroy the existing $(YP_DIR)/$(DOMAIN) and its contents? [y/n: n] " read KILL setglobal ERROR = '' match $(KILL) { with y*|Y* setglobal ERROR = '"DELETE'" with * setglobal ERROR = '' } if test $(ERROR) = "DELETE" { if ! rm -rf $(YP_DIR)/$(DOMAIN) { echo "Can't clean up old directory $(YP_DIR)/$(DOMAIN)." !1 > !2 exit 1 } } else { echo "OK, please clean it up by hand and start again. Bye" exit 0 } } if ! mkdir "$(YP_DIR)/$(DOMAIN)" { echo "Can't make new directory $(YP_DIR)/$(DOMAIN)." !1 > !2 exit 1 } } if test $(SERVERTYPE) = "MASTER" { if test ! -f $(YP_DIR)/Makefile { if test ! -f $(YP_DIR)/Makefile.dist { echo "Can't find $(YP_DIR)/Makefile.dist. " !1 > !2 exit 1 } cp $(YP_DIR)/Makefile.dist $(YP_DIR)/Makefile } } if test $(SERVERTYPE) = "SLAVE" { echo "There will be no further questions. The remainder of the procedure" echo "should take a few minutes, to copy the databases from $(MASTER)." for MAP in [$(YPMAPLIST)] { echo "Transferring $(MAP)..." if ! $(YPXFR) -p $(YP_DIR) -h $(MASTER) -c -d $(DOMAIN) $(MAP) { echo "Can't transfer map $(MAP)." !1 > !2 setglobal ERROR_EXISTS = '"YES'" if test $(ERROR_EXIT) = "YES" { exit 1 } } } echo "" if test $(ERROR_EXISTS) = "YES" { echo "$(HOST) has been setup as an YP slave server with errors. " !1 > !2 echo "Please remember fix any problem that occurred." !1 > !2 } else { echo "$(HOST) has been setup as an YP slave server without any errors. " } echo "Don't forget to update map ypservers on $(MASTER)." exit 0 } setglobal LIST_OK = '"NO'" while test $(LIST_OK) = "NO" { if test $(SERVERTYPE) = "MASTER" { setglobal HOST_LIST = $(HOST) echo "" echo "At this point, we have to construct a list of this domains YP servers." echo "$(HOST) is already known as master server." echo "Please continue to add any slave servers, one per line. When you are" echo "done with the list, type a ." echo " master server : $(HOST)" } if test $(SERVERTYPE) = "UPDATE" { setglobal HOST_LIST = $(HOST) setglobal NEW_LIST = ''"" setglobal MASTER_NAME = ''"" setglobal SHORT_HOST = $[echo $(HOST) | cut -d. -f1] if test -f $(YP_DIR)/$(DOMAIN)/ypservers { for srv in [$[$(MAKEDBM) -u $(YP_DIR)/$(DOMAIN)/ypservers | grep -v "^YP" | tr "\t" " " | cut -d' ' -f1]] { setglobal short_srv = $[echo $(srv) | cut -d. -f1] if test $(SHORT_HOST) != $(short_srv) { if test $(NEW_LIST) = "" { setglobal NEW_LIST = $(srv) } else { setglobal NEW_LIST = ""$(NEW_LIST) $(srv)"" } } }; setglobal MASTER_NAME = $[$(MAKEDBM) -u $(YP_DIR)/$(DOMAIN)/ypservers | grep "^YP_MASTER_NAME" | tr "\t" " " | cut -d' ' -f2] } echo "" echo "Update the list of hosts running YP servers in domain $(DOMAIN)." echo "Master for this domain is $(MASTER_NAME)." echo "" echo "First verify old servers, type \\\\ to remove a server." echo "Then add new servers, one per line. When done type a ." echo "" echo " master server : $(HOST)" if test $(NEW_LIST) != "" { for node in [$NEW_LIST] { echo -n " verify host : [$(node)] " read verify if test $(verify) != "\\" { setglobal HOST_LIST = ""$(HOST_LIST) $(node)"" } }; } } echo -n " next host to add: " while read h { echo -n " next host to add: " setglobal HOST_LIST = ""$(HOST_LIST) $(h)"" } echo "" echo "The current list of NIS servers looks like this:" echo "" for h in [$[echo $(HOST_LIST)]] { echo $(h) } echo "" echo -n "Is this correct? [y/n: y] " read hlist_ok match $hlist_ok { with n* echo "Let's try the whole thing again..." with N* echo "Let's try the whole thing again..." with * setglobal LIST_OK = '"YES'" } } echo "Building $(YP_DIR)/$(DOMAIN)/ypservers..." rm -f $(YP_DIR)/ypservers touch -f $(YP_DIR)/ypservers rm -f $(YP_DIR)/$(DOMAIN)/ypservers for host in [$(HOST_LIST)] { echo "$(host) $(host)" >> $(YP_DIR)/ypservers echo "$(host) $(host)" } | $(MAKEDBM) - $(YP_DIR)/$(DOMAIN)/ypservers if test $Status -ne 0 { echo "" !1 > !2 echo "Couldn't build yp data base $(YP_DIR)/$(DOMAIN)/ypservers." !1 > !2 setglobal ERROR_EXISTS = '"YES'" if test $(ERROR_EXIT) = "YES" { exit 1 } } if test $(SERVERTYPE) = "MASTER" { setglobal CUR_PWD = $[pwd] cd $(YP_DIR) echo "Running $(YP_DIR)/Makefile..." if ! make NOPUSH=True UPDATE_DOMAIN=$(DOMAIN) YP_DIR=$(YP_DIR) { echo "" !1 > !2 echo "Error running Makefile." !1 > !2 setglobal ERROR_EXISTS = '"YES'" if test $(ERROR_EXIT) = "YES" { exit 1 } } cd $(CUR_PWD) echo "" if test $(ERROR_EXISTS) = "YES" { echo "$(HOST) has been setup as an YP master server with errors. " !1 > !2 echo "Please remember fix any problem that occurred." !1 > !2 } else { echo "$(HOST) has been setup as an YP master server without any errors. " } }