You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.6 KiB

  1. #!/bin/bash
  2. ## Note that the shebang is not used, but it's the login shell that
  3. ## will execute this command.
  4. exname=$(basename "$0")
  5. mkdir -p /var/log/rsync
  6. LOG="/var/log/rsync/$exname.log"
  7. ssh_connection=(${SSH_CONNECTION})
  8. SSH_SOURCE_IP="${ssh_connection[0]}:${ssh_connection[1]}"
  9. log() {
  10. printf "%s [%s] %s - %s\n" \
  11. "$(date --rfc-3339=seconds)" "$$" "$SSH_SOURCE_IP" "$*" \
  12. >> "$LOG"
  13. }
  14. log "NEW ADMIN CONNECTION"
  15. reject() {
  16. log "REJECTED: $SSH_ORIGINAL_COMMAND"
  17. # echo "ORIG: $SSH_ORIGINAL_COMMAND" >&2
  18. echo "Your command has been rejected and reported to sys admin." >&2
  19. exit 1
  20. }
  21. if [[ "$SSH_ORIGINAL_COMMAND" =~ [\&\(\{\;\<\>\`\$\}] ]]; then
  22. log "BAD CHARS DETECTED"
  23. # echo "Bad chars: $SSH_ORIGINAL_COMMAND" >&2
  24. reject
  25. fi
  26. if [[ "$SSH_ORIGINAL_COMMAND" =~ ^"ssh-key add ssh-rsa "[a-zA-Z0-9/+]+" "[a-zA-Z0-9._-]+"@"[a-zA-Z0-9._-]+""$ ]]; then
  27. log "ACCEPTED: $SSH_ORIGINAL_COMMAND"
  28. ## Interpret \ to allow passing spaces (want to avoid possible issue with \n)
  29. #read -a ssh_args <<< "${SSH_ORIGINAL_COMMAND}"
  30. ssh_args=(${SSH_ORIGINAL_COMMAND})
  31. # echo "Would accept: $SSH_ORIGINAL_COMMAND" >&2
  32. exec sudo /usr/local/sbin/ssh-key "${ssh_args[@]:1}"
  33. elif [[ "$SSH_ORIGINAL_COMMAND" =~ ^"ssh-key ls"$ ]]; then
  34. log "ACCEPTED: $SSH_ORIGINAL_COMMAND"
  35. ## Interpret \ to allow passing spaces (want to avoid possible issue with \n)
  36. #read -a ssh_args <<< "${SSH_ORIGINAL_COMMAND}"
  37. ssh_args=(${SSH_ORIGINAL_COMMAND})
  38. # echo "Would accept: $SSH_ORIGINAL_COMMAND" >&2
  39. exec /usr/local/sbin/ssh-key "${ssh_args[@]:1}"
  40. elif [[ "$SSH_ORIGINAL_COMMAND" =~ ^"ssh-key rm "[a-zA-Z0-9._-]+$ ]]; then
  41. log "ACCEPTED: $SSH_ORIGINAL_COMMAND"
  42. ## Interpret \ to allow passing spaces (want to avoid possible issue with \n)
  43. #read -a ssh_args <<< "${SSH_ORIGINAL_COMMAND}"
  44. ssh_args=(${SSH_ORIGINAL_COMMAND})
  45. # echo "Would accept: $SSH_ORIGINAL_COMMAND" >&2
  46. exec sudo /usr/local/sbin/ssh-key "${ssh_args[@]:1}"
  47. else
  48. log "NOT MATCHING ANY ALLOWED COMMAND"
  49. reject
  50. fi
  51. ## For other commands, like `find` or `md5`, that could be used to
  52. ## challenge the backups and check that archive is actually
  53. ## functional, I would suggest to write a simple command that takes no
  54. ## arguments, so as to prevent allowing wildcards or suspicious
  55. ## contents. Letting `find` go through is dangerous for instance
  56. ## because of the `-exec`. And path traversal can be done also when
  57. ## allowing /my/path/* by using '..'. This is why a fixed purpose
  58. ## embedded executable will be much simpler to handle, and to be honest
  59. ## we don't need much more.