#!/bin/sh  

if [ -f "/usr/local/sbin/twadmin" ] ; then
       
	if [ $#	-gt 0 ] ; then  
 		case $1 in 
			-h | --help | *  ) 	echo "usage: $0 [-h | --help]"
						exit 				  
                	       			;;
		esac		 	
	
	fi
	
	wentry=0
  	while [ $wentry -eq 0 ]
	do
 		echo	
		echo -n "Are you changing Passphrase first time after tripwire installation? [yes or no]: "
		read yno
		case $yno in

 		       [yY] | [yY][Ee][Ss] )
                		previous_passphrase="FreeBSD"
				wentry=1	
				;;

 		       	[nN] | [nN][Oo] )
                	 	passreq="y"
			 	wentry=1		
                		;;
        		*)	 echo "Please type yes or no only "
            			;;
		esac
	done

	if [ "$passreq" = "y" ] ; then  
		
		 echo
		 echo -n  " Please enter your previous Passphrase : " ; read pans 
		 previous_passphrase=$pans
	fi 

	
	echo 
       	echo " Note : When selecting a passphrase, keep in mind that good passphrases typically "
       	echo " have upper and lower case letters, digits and punctuation marks, and are   "
       	echo " at least 8 characters in length. "
       	echo
	echo
       	echo -n  "Please enter your new Passphrase : " ;  read nans
	new_passphrase=$nans

	reppass=0
	count=0

        while [ $reppass -eq 0 ]
        do
		
		if [ $count -eq 3 ] ; then 
			echo
                        echo "Number of chance exceeds for Passphrase re-type.Please try latter"
        		echo
	                exit
		else 
			echo -n  "Please verify your new Passphrase : " ; read vans 
			vnew_passphrase=$vans
			if [ "$new_passphrase" = "$vnew_passphrase" ] ; then 
				 new_passphrase=$new_passphrase
				 reppass=1
			else 
			 	 count=$((count+1))	
			 	 echo 
				 echo "Passphrase doesnot match. Try again " ;
				 echo 
			fi
		fi
	done 
 	
	cfg_location="/usr/local/etc/tripwire/tw.cfg"
	pol_location="/usr/local/etc/tripwire/tw.pol"
	db_location="/var/db/tripwire/$(hostname).twd"
	bak_cfg_location="/var/tmp/tw.cfg" 
	bak_pol_location="/var/tmp/tw.pol"
	bak_db_location="/var/tmp/$(hostname).twd"
	site_key_file="/usr/local/etc/tripwire/site.key"
	local_key_file="/usr/local/etc/tripwire/$(hostname)-local.key"
	bak_local_key_file="/var/tmp/$(hostname)-local.key"
        bak_site_key_file="/var/tmp/site.key"			


	file_backup(){

		cp $cfg_location $bak_cfg_location 
		cp $pol_location $bak_pol_location 
		cp $db_location $bak_db_location
		cp $site_key_file $bak_site_key_file
                cp $local_key_file $bak_local_key_file 
	}
	
	remove_backup(){

                rm -rf $bak_cfg_location
                rm -rf $bak_pol_location
                rm -rf $bak_db_location
                rm -rf $bak_site_key_file
                rm -rf $bak_local_key_file
		rm -rf /usr/local/etc/tripwire/$(hostname)-local.key.bak
                rm -rf /usr/local/etc/tripwire/site.key.bak

        }	
	
	
	generate_db(){

                /usr/local/sbin/tripwire --init -P $previous_passphrase
        }

	do_site_key_encryption(){

                targetfile="$1"

                /usr/local/sbin/twadmin --encrypt --site-keyfile $site_key_file -Q $new_passphrase $targetfile
        }

        do_local_key_encryption(){

                targetfile="$1"

                /usr/local/sbin/twadmin --encrypt --local-keyfile  $local_key_file -P $new_passphrase $targetfile
        }
	
	remove_file_encryption(){
       		
		targetfile="$1"	
		/usr/local/sbin/twadmin --remove-encryption -Q $previous_passphrase  -P $previous_passphrase $targetfile

	 }
	
	generate_sitekey(){
		
		/usr/local/sbin/twadmin --generate-keys --site-keyfile $site_key_file --site-passphrase $new_passphrase
	}
 
	generate_localkey(){
		
                /usr/local/sbin/twadmin --generate-keys --local-keyfile $local_key_file --local-passphrase $new_passphrase
        }

	restore_file(){
		
		cp $bak_cfg_location $cfg_location
		cp $bak_pol_location $pol_location
		cp $bak_db_location $db_location
		cp $bak_local_key_file $local_key_file 
		cp $bak_site_key_file $site_key_file
	}	


##------------------------------------------------------------------
## Generate baseline database if not exits with previous passphrase
##------------------------------------------------------------------

        if [ !  -f "/var/db/tripwire/$(hostname).twd" ] ; then
		echo 
		echo "We donot find Baseline database on this system which need to have before changing password"
		echo 
                generate_db
                 if [ $? -ne 0 ] ; then
                        echo "Password change failed. Reason: Baseline database creation failed "
                        exit
                 fi
        fi

##------------------------------------------------------------------
## Backup all files before any operation
##------------------------------------------------------------------
	echo 
	echo "Taking backup of important files .... "
        file_backup

##------------------------------------------------------------------
## Remove encryption to remove the signature of Previous Passphrase 
##------------------------------------------------------------------ 
 	echo 
	echo "Removing encryption .... "
	frestore=0
	for f in $cfg_location $pol_location $db_location 
	do
		if  [ -f "$f" ] ; then
			remove_file_encryption "$f"
			if [ $? -ne 0 ] ; then 
				frestore=1 
			fi
		else 
			echo "$f not exits"
		fi
	done 

	if [ $frestore -eq 1 ]; then
		echo "Password change failed. Reason : Removing file encryption failed "
		echo "Restoring files ..."
		restore_file  
		remove_backup   
		exit
	fi

##----------------------------------------------------------------------
## Generate site key file with new Passphrase
##----------------------------------------------------------------------
	
	generate_sitekey
	if [ $? -ne 0 ] ; then
		echo " Password change failed. Reason : Site key generation failed "
		echo "Restoring files ..."
                restore_file
		remove_backup
		rm -rf /usr/local/etc/tripwire/site.key.bak   	
		exit
	else 
		echo "Site key generation sucessful " 
	fi

##------------------------------------------------------------------------------
## Generate local key file with new Passphrase
##-----------------------------------------------------------------------------

	generate_localkey
	 if [ $? -ne 0 ] ; then
                echo " Password change failed. Reason : Local key generation failed "
                echo "Restoring files ..."
                restore_file
		remove_backup
                rm -rf /usr/local/etc/tripwire/$(hostname)-local.key.bak
		rm -rf /usr/local/etc/tripwire/site.key.bak
		exit
        else
                echo "Local key generation sucessful "
        fi

##------------------------------------------------------------------------------------
## Encrypt configuration and policy to sign the files with new site key and 
## database file to sign the files with new local key.
##------------------------------------------------------------------------------------
	echo 
	echo "Doing encryption ... "
	for f in $cfg_location $pol_location $db_location
        do
                if  [ -f "$f" ] ; then
			
			if [ $f != $db_location ] ; then
				do_site_key_encryption  "$f" 
			else 
				do_local_key_encryption  "$f"
			fi

                        if [ $? -ne 0 ] ; then
                                frestore=1
                        fi

                else
                        echo "$f not exits"
                fi
        done

        if [ $frestore -eq 1 ]; then
                echo "Password change failed. Reason : File encryption failed "
                echo "Restoring files ..."
                restore_file
		remove_backup
		rm -rf /usr/local/etc/tripwire/$(hostname)-local.key.bak
                rm -rf /usr/local/etc/tripwire/site.key.bak
		exit 
        fi

##-------------------------------------------------------------------------------------------
## Clean up backup files at the end 
##-------------------------------------------------------------------------------------------
	echo 
	echo "Cleaning up ...."
	remove_backup
			
else
        echo " Either Tripwire is not installed at your system or it is not properly installed "
fi

