#!/bin/tcsh -f
# JLdL 05Sep11.
#
# Copyright (C) 2008-2011 by Jorge L. deLyra <delyra@fma.if.usp.br>.
# This program may be copied and/or distributed freely. See the
# _ terms and conditions in /usr/share/doc/<package>/copyright.
#
# This script is to be executed from /etc/init.d/dynahostnis;
# _ it disables all the clients registered in the dynahostnis
# _ service, by clobbering their IP numbers within the hosts
# _ database of the master NIS server, and then updating the
# _ binary NIS maps; the latter is done in the background and
# _ after a delay, since not all the NIS slave servers may be
# _ responding at boot time of the master server.
#
# Get the list of remote hosts from the command line.
set rhosts = ( $* )
#
# Define the IP address to be used for disabling the entries.
set raddr = "000.000.000.000"
#
# Record the name this script was called with.
set name = `basename $0`
#
# Get the domain in which to act, from the configuration file;
# _ this is needed in case the host has two network interfaces
# _ to different domains, typically the internet and a private
# _ network, and we need to choose in which domain this remote
# _ authorization system will be active.
set dhost = `cat /etc/dynahost/defaultdomain`
#
# Define a variable with the TAB character.
set tab = "`echo '\t'`"
#
# Define a grep target to match a generic numerical IP address.
set iptrg = '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'
#
# Define the hosts file.
set hfile = /etc/hosts
#
# Define the temporary sed-rules file.
set tfile = /tmp/$name.sed.rules
#
# Initialize the temporary file.
cat /dev/null >! $tfile
#
# Loop over the remote hosts in order to build a single
# _ sed editing rules file for all the remote hosts.
foreach rhost ( $rhosts )
    #
    # Define the fully qualified remote domain name.
    set rfqdn = "$rhost.$dhost"
    #
    # Add the rule for this remote host to the rules file.
    echo "s|^$iptrg$tab$rfqdn|$raddr$tab$rfqdn|g" >> $tfile
    #
end
#
# Update the hosts file.
cat $hfile | sed -f $tfile >! $hfile.DYNAHOSTNIS
cp -f $hfile.DYNAHOSTNIS $hfile
#
# Remove the temporary file.
rm -f $tfile
#
# Update the binary NIS maps, after a 10-minute delay,
# _ in the background, and disregarding any errors.
echo "${name}: updating the NIS maps in the background"
( cd /var/yp ; sleep 600 ; make ) >& /dev/null < /dev/null &
