#!/bin/bash

# 
#   The 3 step program to nfs mount through an ssh tunnel.
#
############################################################
function find_server_ports {
   # find mountd tcp port
   export MOUNTD_port=`rpcinfo -p "$NFS_server" \
	     |egrep 'mount' \
	     |grep tcp \
	     |awk '{print $4}' \
	     |sort -u`
   #	 
   # find nfs tcp port
   #
   export NFS_port=`rpcinfo -p "$NFS_server" \
	     |egrep 'nfs' \
	     |grep tcp \
	     |awk '{print $4}' \
	     |sort -u`
   # 
   # test for null port for nfs or mountd
   #
   if [ -n $MOUNTD_port ];then
	echo "TCP Mountd port:    $MOUNTD_port"
   else
        echo "TCP Mountd port:    Not Found"
	exit
   fi
   if [ -n $NFS_port ]; then
	echo "TCP NFS    port:    $NFS_port"
   else
        echo "TCP NFS    port:    Not Found"
	exit
   fi
}
############################################################
#  If:  ----------        ----------          ------------
#      |  client  | ---->| firewall | -----> | NFS_server |
#       ----------        ----------          ------------   
#function open_ports_firewall {
#  ssh -f -c blowfish \
#      -L 2818:"$NFS_server":"$NFS_port" \
#      -L 3045:"$NFS_server":"$MOUNTD_port" \
#      -l "$NFS_user"  \
#      "$firewall"  \
#      /bin/sleep 86400
#}

#  If:  ----------        ------------
#      |  client  | ---->| NFS_server |
#       ----------        ------------
#  Setup both tunnels, using blowfish, sleep for one day, 
function open_ports {
   ssh -c blowfish \
      -L 2818:"$NFS_server":"$NFS_port" \
      -L 3045:"$NFS_server":"$MOUNTD_port" \
      -l "$NFS_user" \
      "$NFS_server" -f /bin/sleep 86400
} 

############################################################
echo " #########################################################"
echo " These are the steps to setup SNFS ( NFS over SSH Tunnel )"
echo " #########################################################"
############################################################
export PATH=$PATH:/usr/sbin

echo " Step 1.   Find TCP ports on Server for mountd and nfs."
echo "           Services must be offered on TCP"
#export NFS_server="192.168.0.10"
echo " What is your server's name"
read  NFS_server
export NFS_server

find_server_ports

############################################################
echo " Step 2.   Setup the local forward rules for ssh"
#export NFS_user="jstile"
echo " What is the nfs users name"
read NFS_user
export NFS_user

open_ports

############################################################
echo " Step 3.  Mount nfs server server" 

mount -t nfs -o tcp,port=2818,mountport=3045 \
             localhost:/home/$NFS_user /home/$NFS_user/test
	   
############################################################
echo "Now, in a different terminal, as the NFS_user, ls the test directory."
echo "Remember to umount before killing the local port map."
############################################################
