Advertisement
kivaari

Server.sh Aufgabe 2

Dec 6th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.07 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. rm -f variables.txt
  4. echo -e "0\n0\n0\n0" > variables.txt
  5. test -e users.txt || touch users.txt
  6. test -p pipe || mkfifo pipe
  7.  
  8. port=1337
  9.  
  10. function setuser() {
  11.     if [[ $(grep -En "^[^\:]*$1" users.txt) ]]; then
  12.         echo "User already exists"
  13.                 sed -i "1s/.*/0/" variables.txt #status=0
  14.                 sed -i "2s/.*/0/" variables.txt #registerstatus=0
  15.     else
  16.         echo -n $1 >> users.txt
  17.                 echo -n ":" >> users.txt
  18.                 sed -i "2s/.*/1/" variables.txt #registerstatus=1
  19.     fi
  20. }
  21.  
  22. function setpassword() {
  23.     hashedpw=$1
  24.     n=$2
  25.     echo -n $n >> users.txt
  26.     echo -n ":" >> users.txt
  27.     echo -n $hashedpw >> users.txt
  28.     echo -n ":" >> users.txt
  29.     echo "0" >> users.txt
  30. }
  31.  
  32. function checkuser() {
  33.     # check if user exists
  34.     # send c in this function
  35.     if [[ $(grep -En "^[^\:]*$1" users.txt) ]]; then
  36.         LINE=$(grep -En "^[^\:]*$1" users.txt | cut -d ':' -f 1)
  37.         N=$(grep -En "^[^\:]*$1" users.txt | cut -d ':' -f 3)
  38.         sed -i "4s/.*/$LINE/" variables.txt
  39.         sed -i "3s/.*/1/" variables.txt #loginstatus=1
  40.         T=$(grep -En "^[^\:]*$1" users.txt | cut -d ':' -f 5)
  41.         T=$(expr $T + 1)
  42.         sed -i -e "${LINE}s/\:[^\:]*$//" users.txt
  43.         sed -i -e "${LINE}s/$/:$T/" users.txt
  44.         if [[ "$T" == "$N" ]]; then
  45.             echo "USERACCEPTED $T no"
  46.             sed -i "3s/.*/3/" variables.txt #loginstatus=3
  47.         else
  48.             echo "USERACCEPTED $T yes"
  49.         fi
  50.     else
  51.         echo "User does not exist"
  52.         sed -i "1s/.*/0/" variables.txt #status=0
  53.                 sed -i "3s/.*/0/" variables.txt #loginstatus=0
  54.     fi
  55. }
  56.  
  57. function checkpassword() {
  58.     # checks if pw matches for given user
  59.     newpw=$1
  60.     passwd=$(echo $1 | shasum | cut -d ' ' -f 1)
  61.     pwline=$(sed -n 4p variables.txt)
  62.     realpw=$(sed -n ${pwline}p users.txt | cut -d ':' -f 3)
  63.     if [[ "$passwd" == "$realpw" ]]; then
  64.         sed -i "3s/.*/2/" variables.txt #loginstatus=2
  65.         OLDT=$(sed -n ${pwline}p users.txt | cut -d ':' -f 4)
  66.                 sed -i -e "${LINE}s/\:[^\:]*$//" users.txt
  67.         sed -i -e "${LINE}s/\:[^\:]*$//" users.txt
  68.                 sed -i -e "${LINE}s/$/:$newpw/" users.txt
  69.         sed -i -e "${LINE}s/$/:$OLDT/" users.txt
  70.         echo "PASSWORDACCEPTED yes"
  71.     else
  72.          sed -i "3s/.*/0/" variables.txt #loginstatus=0
  73.          sed -i "1s/.*/0/" variables.txt #status=0
  74.         echo "Wrong password!"
  75.     fi
  76. }
  77.  
  78. function test() {
  79.     read input
  80.     status=$(sed -n 1p variables.txt)
  81.     registerstatus=$(sed -n 2p variables.txt)
  82.     loginstatus=$(sed -n 3p variables.txt)
  83.     if [[ "$status" == "0" ]]; then
  84.         if [[ "$input" == "REGISTER" ]]; then
  85.             sed -i "1s/.*/1/" variables.txt
  86.         elif [[ "$input" == "LOGIN" ]]; then
  87.             sed -i "1s/.*/2/" variables.txt
  88.         else
  89.             sed -i "1s/.*/0/" variables.txt
  90.         fi
  91.     elif [[ "$status" == "1" ]]; then
  92.         if [[ "$registerstatus" == "0" ]]; then
  93.             setuser $input
  94.         elif [[ "$registerstatus" == "1" ]]; then
  95.             setpassword $input
  96.             sed -i "1s/.*/0/" variables.txt #status=0
  97.             sed -i "2s/.*/0/" variables.txt #registerstatus=0
  98.         else
  99.             echo "impossible register else case"
  100.         fi
  101.     elif [[ "$status" == "2" ]]; then
  102.         if [[ "$loginstatus" == "0" ]]; then
  103.             checkuser $input
  104.         elif [[ "$loginstatus" == "1" ]]; then
  105.             checkpassword $input
  106.         elif [[ "$loginstatus" == "2" ]]; then
  107.             if [ ! -f $input ]; then
  108.                     echo "File not found!"
  109.             else
  110.                 echo $(cat $input)
  111.             fi
  112.             sed -i "1s/.*/0/" variables.txt #status=0
  113.             sed -i "3s/.*/0/" variables.txt #loginstatus=0
  114.         elif [[ "$loginstatus" == "3" ]]; then
  115.             N=$(echo $input | cut -d ' ' -f 2)
  116.             NEWPW=$(echo $input | cut -d ' ' -f 1)
  117.             LINE=$(sed -n 4p variables.txt)
  118.             sed -i -e "${LINE}s/\:[^\:]*$//" users.txt
  119.                     sed -i -e "${LINE}s/\:[^\:]*$//" users.txt
  120.             sed -i -e "${LINE}s/\:[^\:]*$//" users.txt
  121.             sed -i -e "${LINE}s/$/:$N/" users.txt
  122.                     sed -i -e "${LINE}s/$/:$NEWPW/" users.txt
  123.                     sed -i -e "${LINE}s/$/:0/" users.txt
  124.             sed -i "1s/.*/0/" variables.txt #status=0
  125.                         sed -i "3s/.*/0/" variables.txt #loginstatus=0
  126.         else
  127.             echo "impossible login else case"
  128.         fi
  129.     else
  130.         echo "impossible status else case"
  131.     fi
  132. }
  133.  
  134. while true;
  135. do
  136.     nc -l -p $port < pipe | test | cat > pipe;
  137. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement