#! /sbin/sh

####
#
# This script combines the follwoing files together into one file:
#
#    /etc/rc.config   /etc/exports
#    /etc/inetd.conf  /etc/netgroup  ...
####

#
# Make sure a root user is executing the script
#
if [ $(id -u) -ne 0 ]
then
        echo
	echo "Sorry, you are not a super-user."
	echo
	echo "You must be a super-user to run this script."
	echo
        #exit 1
fi

#
PATH=/sbin:/usr/sbin:/usr/bin:/usr/lib/netsvc:/usr/lib/netsvc/yp
export PATH
HOSTNAME=`hostname`

TMPFILE1=/tmp/nfstmp1.$$
TMPFILE2=/tmp/nfstmp2.$$
TMPFILE3=/tmp/nfstmp3.$$

NFS_F=/tmp/nfs_f.$$
NIS_F=/tmp/nis_f.$$
AUTO_F=/tmp/auto_f.$$


###
# Ask what information the user wants?
###

INFOREQ=8   

while [ $INFOREQ -lt 1 -o $INFOREQ -gt 5 ]
do
     echo "\n\n"
     echo "1)  All NFS Information\n"
     echo "2)  NFS Information\n"
     echo "3)  NIS Information\n"
     echo "4)  Automounter Information\n"
     echo "5)  Exit\n\n"
     echo "Enter the number of your selection? \c"
     read INFOREQ1
     u=`echo "$INFOREQ1" | sed -e 's/[0-9]//g'`
     if [ -n "$u" ];then
        let INFOREQ=8
	echo "$INFOREQ1 is not a valid number!\n\n"
     else
        let INFOREQ="$INFOREQ1"
     fi
     echo "\n\n"
done



if [ $INFOREQ -eq 5 ] ; then

     echo "Ended nfsconfig script\n\n"
     exit

fi

###
# Import values from rc.config file so they can be used by the script.
###


if [ -d /etc/rc.config.d ] ; then
     . /etc/rc.config.d/nfsconf
     . /etc/rc.config.d/namesvrs

else

     echo "ERROR: /etc/rc.config defaults file MISSING"
     echo "NFS, NIS, and Automounter Config Information cannot be collected"
     exit 2
fi
     echo "Starting NFS, NIS, and Automounter Config Information Collection"


##
# Add the date, version, and hostname to the beginning of the file.
##

uname -a > $NFS_F           
date >> $NFS_F     
echo "\n" >> $NFS_F

##
# Collect NFS configuration files in /tmp/nfs_f.  These are 
# rc.config, exports, exportfs, inetd.conf, and netgroup.
##
if [ $INFOREQ -eq 1 -o $INFOREQ -eq 2 ] ; then
    echo "\n\n" >>$NFS_F
    echo "**********************************************************************" >>$NFS_F
    echo "******************* FILES in /etc/rc.config.d ************************" >>$NFS_F
    echo "**********************************************************************\n\n" >>$NFS_F

     config="/etc/rc.config.d/nfsconf /etc/rc.config.d/namesvrs"
     for i in $config
     do
     echo "\n\n" >>$NFS_F
     echo "**********************************************************************" >>$NFS_F
     echo "***************************  $i Entries " >>$NFS_F
     echo "**********************************************************************" >>$NFS_F
     echo "\n\n" >>$NFS_F
     cat $i >>$NFS_F
     done

echo "\n\n" >>$NFS_F
echo "**********************************************************************" >>$NFS_F
echo "******************* Entries in /etc/netgroup *************************" >>$NFS_F
echo "**********************************************************************\n\n" >>$NFS_F
     cat /etc/netgroup >> $NFS_F     

echo "\n\n" >>$NFS_F
echo "**********************************************************************" >>$NFS_F
echo "******************* Entries in /etc/exports **************************" >>$NFS_F
echo "**********************************************************************\n\n" >>$NFS_F
     cat /etc/exports >> $NFS_F     

echo "\n\n" >>$NFS_F
echo "**********************************************************************" >>$NFS_F
echo "******************* Exportfs output **********************************" >>$NFS_F
echo "**********************************************************************\n\n" >>$NFS_F
     /usr/sbin/exportfs >> $NFS_F     

echo "\n\n" >>$NFS_F
echo "**********************************************************************" >>$NFS_F
echo "******************* Entries in inetd.conf ****************************" >>$NFS_F
echo "**********************************************************************\n\n" >>$NFS_F
     grep -i rpc /etc/inetd.conf >> $NFS_F     

echo "\n\n" >>$NFS_F
echo "**********************************************************************" >>$NFS_F
echo "******************* Entries in /etc/fstab ****************************" >>$NFS_F
echo "**********************************************************************\n\n" >>$NFS_F
     cat /etc/fstab >> $NFS_F     

echo "\n\n" >>$NFS_F
echo "**********************************************************************" >>$NFS_F
echo "******************* Entries in /etc/rmtab ****************************" >>$NFS_F
echo "**********************************************************************\n\n" >>$NFS_F
     cat /etc/rmtab >> $NFS_F     

echo "\n\n" >>$NFS_F
echo "**********************************************************************" >>$NFS_F
echo "******************* Output from rpcinfo -p ***************************" >>$NFS_F
echo "**********************************************************************\n\n" >>$NFS_F
     /usr/bin/rpcinfo -p >> $NFS_F     

echo "\n\n" >>$NFS_F
echo "**********************************************************************" >>$NFS_F
echo "******************* Entries in /etc/rpc ******************************" >>$NFS_F
echo "**********************************************************************\n\n" >>$NFS_F
     cat /etc/rpc >> $NFS_F     

echo "\n\n" >>$NFS_F
echo "**********************************************************************" >>$NFS_F
echo "******************* Output from nfsstat <default parms> **************" >>$NFS_F
echo "**********************************************************************\n\n" >>$NFS_F
     /usr/bin/nfsstat >> $NFS_F     

echo "\n\n" >>$NFS_F
echo "**********************************************************************" >>$NFS_F
echo "******************* Output from nfsstat -m ***************************" >>$NFS_F
echo "**********************************************************************\n\n" >>$NFS_F
     /usr/bin/nfsstat -m >>$NFS_F     

echo "\n\n" >>$NFS_F
echo "**********************************************************************" >>$NFS_F
echo "******************* Entries in /etc/pcnfsd.conf **********************" >>$NFS_F
echo "**********************************************************************\n\n" >>$NFS_F
if [ -f /etc/pcnfsd.conf ];then
   cat /etc/pcnfsd.conf >> $NFS_F     
else
   echo "/etc/pcnfsd.conf is empty!" >>$NFS_F
fi

echo "\n\n" >>$NFS_F
echo "**********************************************************************" >>$NFS_F
echo "******************* Entries in /var/yp/securenets ********************" >>$NFS_F
echo "**********************************************************************\n\n" >>$NFS_F
if [ -f /var/yp/securenets ];then
   cat /var/yp/securenets >> $NFS_F     
else
   echo "/var/yp/securenets is empty!" >>$NFS_F
fi

echo "\n\n" >>$NFS_F
echo "**********************************************************************" >>$NFS_F
echo "******************* Entries in /var/yp/secureservers *****************" >>$NFS_F
echo "**********************************************************************\n\n" >>$NFS_F
if [ -f /var/yp/secureservers ];then
   cat /var/yp/secureservers >> $NFS_F     
else
   echo "/var/yp/secureservers is empty!" >>$NFS_F
fi

echo "\n\n" >>$NFS_F
echo "**********************************************************************" >>$NFS_F
echo "******** List of Current Proccesses Running Under root ***************" >>$NFS_F
echo "**********************************************************************\n\n" >>$NFS_F
     ps -ef | grep root >> $NFS_F     

##
#  Collect directory information on Status Monitor in /tmp/nfs_f.
##

echo "\n\n" >>$NFS_F
echo "**********************************************************************" >>$NFS_F
echo "******************* Status Monitor Information ***********************" >>$NFS_F
echo "**********************************************************************\n\n" >>$NFS_F

echo "\n\n" >>$NFS_F
echo "**********************************************************************" >>$NFS_F
echo "******** Systems listed in the /var/statmon/sm directory *************" >>$NFS_F
echo "**********************************************************************\n\n" >>$NFS_F
     /usr/bin/ll /var/statmon/sm >> $NFS_F       

x=`/usr/bin/ls /var/statmon/sm`
if [ -n "$x" ];then
  for i in $x
     do
          echo "\n\n" >>$NFS_F
          echo "**********************************************************************" >>$NFS_F
          echo "******************* /var/statmon/sm/$i  contents " >>$NFS_F
          echo "**********************************************************************\n\n" >>$NFS_F
          cat $i >>$NFS_F      
     done
fi

echo "\n\n" >>$NFS_F
echo "**********************************************************************" >>$NFS_F
echo "******** Systems listed in the /var/statmon/sm.bak directory *********" >>$NFS_F
echo "**********************************************************************\n\n" >>$NFS_F
     /usr/bin/ll /var/statmon/sm.bak >>$NFS_F     

x=`/usr/bin/ls /var/statmon/sm.bak`
if [ -n "$x" ];then
  for i in $x
     do
          echo "\n\n" >>$NFS_F
          echo "**********************************************************************" >>$NFS_F
          echo "******************* /var/statmon/sm.bak/$i  contents                  " >>$NFS_F
          echo "**********************************************************************\n\n" >>$NFS_F
          cat $i >>$NFS_F      
     done
fi
echo "" >>$NFS_F
fi


##
# Find NIS_CLIENT & NIS_SERVER in rc.config to determine if
# NIS is running.  If not skip to automounter.
##

if [ $INFOREQ -eq 1 -o $INFOREQ -eq 3 ] ; then

     if [ -z "$NIS_MASTER_SERVER" -o "$NIS_MASTER_SERVER" -eq 0 -a \
          -z "$NIS_SLAVE_SERVER"  -o "$NIS_SLAVE_SERVER" -eq 0 -a \
          -z "$NIS_CLIENT"        -o "$NIS_CLIENT" -eq 0 ] ; then
          echo "\n\n" >>$NIS_F
          echo "**********************************************************************" >>$NIS_F
          echo "******   Network Information Service not started." >>$NIS_F     
          echo "**********************************************************************" >>$NIS_F
          echo "\n\n" >>$NIS_F

     else

          echo "\n\n" >>$NIS_F
          echo "**********************************************************************" >>$NIS_F
          echo "*******   NETWORK INFORMATION SERVICE" > $NIS_F     
          echo "**********************************************************************" >>$NIS_F
          echo "\n\n" >>$NIS_F

          echo "\n\n" >>$NIS_F
          echo "**********************************************************************" >>$NIS_F
          echo "************ NIS current domainname:  \c" >> $NIS_F     
          echo "**********************************************************************" >>$NIS_F
          echo "\n\n" >>$NIS_F
          domainname >> $NIS_F      

          if [ $NIS_MASTER_SERVER -ne 0 -o $NIS_SLAVE_SERVER -ne 0 ]; then
   
###
# Determine how many domains this server knows about and collect 
# information for each one.
###


               ls /var/yp > $TMPFILE1
	       for DOMAINS in `cat $TMPFILE1`
     	       do

                   echo "\nDomain $DOMAINS has the maps with NIS Master:\n\n" >> $NIS_F     
                   /usr/bin/ypwhich -d $DOMAINS -m >> $NIS_F     

##
#  Determine the map names without extentions and paths.
##

                   ls /var/yp/$DOMAINS/*.dir |\
                      sed -e '/hosts./d' -e '/netid./d' -e s~/var/yp/$DOMAINS/~~ |\
                      cut -d. -f1 | uniq > $TMPFILE2      

##
#  Add the contents of each map except hosts to /tmp/nis_f.
##

	           for NISMAP in `cat $TMPFILE2`
	           do
	               echo "\n\nMap Name:  $NISMAP\n" >> $NIS_F      
	               /usr/bin/ypcat -k $NISMAP >> $NIS_F     
	           done

                   echo "\n\nhosts and netid maps are not listed since they are too long." >> $NIS_F      
                   rm -f $TMPFILE1 $TMPFILE2

	       done

          fi

##
#  Collecting information on the NIS client.
##

          if [ $NIS_CLIENT -ne 0 ] ; then

               echo "\nThis client, $HOSTNAME, is bound to NIS Server \c" >> $NIS_F
	       /usr/bin/ypwhich -m >> $NIS_F      

          fi

     fi

fi


##
#  This next section collects all the master, direct, and indirect
#  files associated with Automounter into a temp file called auto_f.
##


if [ $INFOREQ -eq 1 -o $INFOREQ -eq 4 ] ; then

     if [ "$AUTOMOUNT" -ne 0 ] ; then
          echo "\n\n" >>$AUTO_F
          echo "**********************************************************************" >>$AUTO_F
          echo "********************  AUTOMOUNTER FILES ******************************" >> $AUTO_F      
          echo "**********************************************************************" >>$AUTO_F
          echo "\n\n" >>$AUTO_F
          echo "**********************************************************************" >>$AUTO_F
          echo "********************* The Automounter Master file is $AUTO_MASTER." >> $AUTO_F
          echo "**********************************************************************" >>$AUTO_F
          echo "\n\n" >>$AUTO_F

echo "\n\n" >>$AUTO_F
echo "**********************************************************************" >>$AUTO_F
echo "******************* AUTO_MASTER contents: ****************************" >>$AUTO_F
echo "**********************************************************************\n\n" >>$AUTO_F
          cat $AUTO_MASTER >> $AUTO_F      

echo "\n\n" >>$AUTO_F
echo "**********************************************************************" >>$AUTO_F
echo "******************* AUTOMOUNT LOG contents: **************************" >>$AUTO_F
echo "**********************************************************************\n\n" >>$AUTO_F
if [ -f /var/adm/automount.log ];then
   cat /var/adm/automount.log >> $AUTO_F      
else
   echo "/var/adm/automount.log is empty!" >>$AUTO_F
fi

##
#  Edit AUTO_MASTER file to extract the direct and indirect
#  maps.  To do this, -hosts, -passwd, key, options, NIS maps,
#  and comments need to be stripped from the file.
##

          sed -e '/-hosts/d' -e '/-passwd/d' \
              -e '/*/d' -e '/#/d' -e '/+/d' $AUTO_MASTER |\
            awk '{echo $2}' > $TMPFILE3

##
#  Now that the names of the direct and indirect maps are known
#  the files can be copied into the /tmp/auto_f file.
##

          for AUTOTMP in `cat $TMPFILE3`
          do
          echo "\n\n" >>$AUTO_F
          echo "**********************************************************************" >>$AUTO_F
          echo "******************* $AUTOTMP  contents " >>$AUTO_F
          echo "**********************************************************************\n\n" >>$AUTO_F
              cat $AUTOTMP >> $AUTO_F      
          done
          rm -f $TMPFILE3

     else
          echo "\n     Automounter Not Started."

     fi

fi


##
#  Now we'll combine all the temp files into one big file of ONC/NFS
#  information called nfsfile.
##


cat -s $NFS_F $NIS_F $AUTO_F > /tmp/nfsfile

echo "\n        Collection of NFS, NIS, and Automounter files Complete"
echo "\n\nTo view this information, do a more or vi the file /tmp/nfsfile."
echo "\n\nTo send this file, compress or shar /tmp/nfsfile."        
rm -f $NFS_F $NIS_F $AUTO_F
exit

