v-check-api-key 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/bin/bash
  2. # info: check api key
  3. # options: KEY [IP]
  4. # labels:
  5. #
  6. # example: v-check-api-key random_key 127.0.0.1
  7. #
  8. # The function checks a key file in $HESTIA/data/keys/
  9. #----------------------------------------------------------#
  10. # Variable&Function #
  11. #----------------------------------------------------------#
  12. [[ -z $HESTIA ]] && HESTIA="/usr/local/hestia"
  13. source $HESTIA/func/main.sh
  14. new_timestamp
  15. abort_missmatch() {
  16. echo "Error: key missmatch"
  17. echo "$date $time api $ip failed to login" >> $HESTIA/log/auth.log
  18. exit $E_PASSWORD
  19. }
  20. ip=${2-127.0.0.1}
  21. time_n_date=$(date +'%T %F')
  22. time=$(echo "$time_n_date" |cut -f 1 -d \ )
  23. date=$(echo "$time_n_date" |cut -f 2 -d \ )
  24. #----------------------------------------------------------#
  25. # Action #
  26. #----------------------------------------------------------#
  27. key="$(basename "$1")"
  28. # Exit if Key is unset or to short
  29. [[ -z $key || ${#key} -lt 16 ]] && abort_missmatch
  30. # Key file must exist
  31. maybe_key_path="$(readlink -e "${HESTIA}/data/keys/${key}")"
  32. [[ -z $maybe_key_path ]] && abort_missmatch
  33. # Key file cannot be the key store
  34. [[ $maybe_key_path == "${HESTIA}/data/keys" ]] && abort_missmatch
  35. # Key file must be in the key store
  36. [[ $maybe_key_path == "${HESTIA}/data/keys/"* ]] || abort_missmatch
  37. #----------------------------------------------------------#
  38. # Hestia #
  39. #----------------------------------------------------------#
  40. echo "$date $time api $ip successfully launched" >> $HESTIA/log/auth.log
  41. exit