cert_func.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. is_cert_free() {
  2. # Defining path
  3. user_cert="$V_USERS/$user/cert/$cert"
  4. # Checking file existance
  5. if [ -e "$user_cert.crt" ] || [ -e "$user_cert.key" ]; then
  6. echo "Error: certificate exist"
  7. log_event 'debug' "$E_CERT_EXIST $V_EVENT"
  8. exit $E_CERT_EXIST
  9. fi
  10. }
  11. is_cert_valid() {
  12. path="$1"
  13. # Checking file existance
  14. if [ ! -e "$path/$cert.crt" ] || [ ! -e "$path/$cert.key" ]; then
  15. echo "Error: certificate not exist"
  16. log_event 'debug' "$E_CERT_NOTEXIST $V_EVENT"
  17. exit $E_CERT_NOTEXIST
  18. fi
  19. # Checking crt file
  20. crt=$(openssl verify "$path/$cert.crt" 2>/dev/null|tail -n 1|grep -w 'OK')
  21. if [ -z "$crt" ]; then
  22. echo "Error: certificate invalid"
  23. log_event 'debug' "$E_CERT_INVALID $V_EVENT"
  24. exit $E_CERT_INVALID
  25. fi
  26. # Checking key file
  27. key=$(openssl rsa -in "$path/$cert.key" -check 2>/dev/null|\
  28. head -n1|grep -w 'ok')
  29. if [ -z "$key" ]; then
  30. echo "Error: key invalid"
  31. log_event 'debug' "$E_KEY_INVALID $V_EVENT"
  32. exit $E_KEY_INVALID
  33. fi
  34. # FIXME we should run server on free port
  35. # Checking server
  36. cmd="openssl s_server -quiet -cert $path/$cert.crt -key $path/$cert.key"
  37. $cmd &
  38. # Defining pid
  39. pid=$!
  40. # Sleep 1 second
  41. sleep 1
  42. # Disown background process
  43. disown > /dev/null 2>&1
  44. # Killing ssl server
  45. kill $pid > /dev/null 2>&1
  46. # Checking result
  47. result=$?
  48. if [ "$result" -ne '0' ]; then
  49. echo "Error: certificate key pair invalid"
  50. log_event 'debug' "$E_CERTKEY_INVALID $V_EVENT"
  51. exit $E_CERTKEY_INVALID
  52. fi
  53. }
  54. is_cert_used() {
  55. # Parsing config
  56. check_cert=$(grep "SSL_CERT='$cert'" $V_USERS/$user/web.conf)
  57. # Checking result
  58. if [ ! -z "$check_cert" ]; then
  59. echo "Error: certificate used"
  60. log_event 'debug' "$E_CERT_USED $V_EVENT"
  61. exit $E_CERT_USED
  62. fi
  63. }
  64. cert_json_list() {
  65. # Definigng variables
  66. i='1' # iterator
  67. j='1' # iterator
  68. end=$(($limit + $offset)) # last string
  69. # Print top bracket
  70. echo '['
  71. # Checking certificates number
  72. last=$(ls $V_USERS/$user/cert/|grep '.crt' | wc -l)
  73. # Listing files by mask
  74. for cert in $(ls $V_USERS/$user/cert/|grep '.crt'); do
  75. # Checking offset and limit
  76. if [ "$i" -ge "$offset" ] && [ "$i" -lt "$end" ] && [ "$offset" -gt 0 ]
  77. then
  78. if [ "$i" -ne "$last" ] && [ "$j" -ne "$limit" ]; then
  79. echo -e "\t\"${cert//.crt/}\","
  80. else
  81. echo -e "\t\"${cert//.crt/}\""
  82. fi
  83. j=$(($j + 1))
  84. fi
  85. i=$(($i + 1))
  86. done
  87. # Printing bottom bracket
  88. echo -e "]"
  89. }
  90. cert_shell_list() {
  91. i='1' # iterator
  92. end=$(($limit + $offset)) # last string
  93. # Print brief info
  94. echo "Certificate"
  95. echo "----------"
  96. # Listing files by mask
  97. for cert in $(ls $V_USERS/$user/cert/|grep '.crt'); do
  98. # Checking offset and limit
  99. if [ "$i" -ge "$offset" ] && [ "$i" -lt "$end" ] && [ "$offset" -gt 0 ]
  100. then
  101. # Print result
  102. echo "${cert//.crt/}"
  103. fi
  104. i=$(($i + 1))
  105. done
  106. }