#!/bin/bash rm -f variables.txt echo -e "0\n0\n0\n0" > variables.txt test -e users.txt || touch users.txt test -p pipe || mkfifo pipe port=1337 function setuser() { if [[ $(grep -En "^[^\:]*$1" users.txt) ]]; then echo "User already exists" sed -i "1s/.*/0/" variables.txt #status=0 sed -i "2s/.*/0/" variables.txt #registerstatus=0 else echo -n $1 >> users.txt echo -n ":" >> users.txt sed -i "2s/.*/1/" variables.txt #registerstatus=1 fi } function setpassword() { hashedpw=$1 n=$2 echo -n $n >> users.txt echo -n ":" >> users.txt echo -n $hashedpw >> users.txt echo -n ":" >> users.txt echo "0" >> users.txt } function checkuser() { # check if user exists # send c in this function if [[ $(grep -En "^[^\:]*$1" users.txt) ]]; then LINE=$(grep -En "^[^\:]*$1" users.txt | cut -d ':' -f 1) N=$(grep -En "^[^\:]*$1" users.txt | cut -d ':' -f 3) sed -i "4s/.*/$LINE/" variables.txt sed -i "3s/.*/1/" variables.txt #loginstatus=1 T=$(grep -En "^[^\:]*$1" users.txt | cut -d ':' -f 5) T=$(expr $T + 1) sed -i -e "${LINE}s/\:[^\:]*$//" users.txt sed -i -e "${LINE}s/$/:$T/" users.txt if [[ "$T" == "$N" ]]; then echo "USERACCEPTED $T no" sed -i "3s/.*/3/" variables.txt #loginstatus=3 else echo "USERACCEPTED $T yes" fi else echo "User does not exist" sed -i "1s/.*/0/" variables.txt #status=0 sed -i "3s/.*/0/" variables.txt #loginstatus=0 fi } function checkpassword() { # checks if pw matches for given user newpw=$1 passwd=$(echo $1 | shasum | cut -d ' ' -f 1) pwline=$(sed -n 4p variables.txt) realpw=$(sed -n ${pwline}p users.txt | cut -d ':' -f 3) if [[ "$passwd" == "$realpw" ]]; then sed -i "3s/.*/2/" variables.txt #loginstatus=2 OLDT=$(sed -n ${pwline}p users.txt | cut -d ':' -f 4) sed -i -e "${LINE}s/\:[^\:]*$//" users.txt sed -i -e "${LINE}s/\:[^\:]*$//" users.txt sed -i -e "${LINE}s/$/:$newpw/" users.txt sed -i -e "${LINE}s/$/:$OLDT/" users.txt echo "PASSWORDACCEPTED yes" else sed -i "3s/.*/0/" variables.txt #loginstatus=0 sed -i "1s/.*/0/" variables.txt #status=0 echo "Wrong password!" fi } function test() { read input status=$(sed -n 1p variables.txt) registerstatus=$(sed -n 2p variables.txt) loginstatus=$(sed -n 3p variables.txt) if [[ "$status" == "0" ]]; then if [[ "$input" == "REGISTER" ]]; then sed -i "1s/.*/1/" variables.txt elif [[ "$input" == "LOGIN" ]]; then sed -i "1s/.*/2/" variables.txt else sed -i "1s/.*/0/" variables.txt fi elif [[ "$status" == "1" ]]; then if [[ "$registerstatus" == "0" ]]; then setuser $input elif [[ "$registerstatus" == "1" ]]; then setpassword $input sed -i "1s/.*/0/" variables.txt #status=0 sed -i "2s/.*/0/" variables.txt #registerstatus=0 else echo "impossible register else case" fi elif [[ "$status" == "2" ]]; then if [[ "$loginstatus" == "0" ]]; then checkuser $input elif [[ "$loginstatus" == "1" ]]; then checkpassword $input elif [[ "$loginstatus" == "2" ]]; then if [ ! -f $input ]; then echo "File not found!" else echo $(cat $input) fi sed -i "1s/.*/0/" variables.txt #status=0 sed -i "3s/.*/0/" variables.txt #loginstatus=0 elif [[ "$loginstatus" == "3" ]]; then N=$(echo $input | cut -d ' ' -f 2) NEWPW=$(echo $input | cut -d ' ' -f 1) LINE=$(sed -n 4p variables.txt) sed -i -e "${LINE}s/\:[^\:]*$//" users.txt sed -i -e "${LINE}s/\:[^\:]*$//" users.txt sed -i -e "${LINE}s/\:[^\:]*$//" users.txt sed -i -e "${LINE}s/$/:$N/" users.txt sed -i -e "${LINE}s/$/:$NEWPW/" users.txt sed -i -e "${LINE}s/$/:0/" users.txt sed -i "1s/.*/0/" variables.txt #status=0 sed -i "3s/.*/0/" variables.txt #loginstatus=0 else echo "impossible login else case" fi else echo "impossible status else case" fi } while true; do nc -l -p $port < pipe | test | cat > pipe; done