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.

108 lines
2.1 KiB

  1. #!/bin/bash
  2. RSYNC_KEY_PATH=/etc/rsync/keys
  3. ANSI_ESC=$'\e['
  4. NORMAL="${ANSI_ESC}0m"
  5. GRAY="${ANSI_ESC}1;30m"
  6. RED="${ANSI_ESC}1;31m"
  7. GREEN="${ANSI_ESC}1;32m"
  8. YELLOW="${ANSI_ESC}1;33m"
  9. BLUE="${ANSI_ESC}1;34m"
  10. PINK="${ANSI_ESC}1;35m"
  11. CYAN="${ANSI_ESC}1;36m"
  12. WHITE="${ANSI_ESC}1;37m"
  13. DARKGRAY="${ANSI_ESC}0;30m"
  14. DARKRED="${ANSI_ESC}0;31m"
  15. DARKGREEN="${ANSI_ESC}0;32m"
  16. DARKYELLOW="${ANSI_ESC}0;33m"
  17. DARKBLUE="${ANSI_ESC}0;34m"
  18. DARKPINK="${ANSI_ESC}0;35m"
  19. DARKCYAN="${ANSI_ESC}0;36m"
  20. DARKWHITE="${ANSI_ESC}0;37m"
  21. ssh-key-ls() {
  22. local f content
  23. for f in "${RSYNC_KEY_PATH}"/backup/*.pub; do
  24. [ -e "$f" ] || continue
  25. ident=${f##*/}
  26. ident=${ident%.pub}
  27. content=$(cat "$f")
  28. key=${content#* }
  29. key=${key% *}
  30. printf "${DARKGRAY}..${NORMAL}%24s ${DARKCYAN}%s${NORMAL}\n" "${key: -24}" "$ident"
  31. done
  32. }
  33. ssh-key-rm() {
  34. local ident="$1" delete
  35. delete="${RSYNC_KEY_PATH}/backup/$ident.pub"
  36. if ! [ -e "$delete" ]; then
  37. echo "Error: key '$ident' not found." >&2
  38. return 1
  39. fi
  40. rm "$delete"
  41. /usr/local/sbin/ssh-update-keys
  42. }
  43. ssh-key-add() {
  44. local type="$1" key="$2" email="$3"
  45. [ "$1" == "ssh-rsa" ] || {
  46. echo "Error: expecting ssh-rsa key type" >&2
  47. return 1
  48. }
  49. ## ident are unique by construction (they are struct keys)
  50. ## but keys need to be also unique
  51. declare -A keys
  52. mkdir -p "${RSYNC_KEY_PATH}/backup"
  53. content="$type $key $email"
  54. ident="${email##*@}"
  55. target="${RSYNC_KEY_PATH}/backup/$ident.pub"
  56. if [ -e "$target" ]; then
  57. old_content=$(cat "$target")
  58. if [ "$content" == "$old_content" ]; then
  59. echo "Provided key already present for '$ident'." >&2
  60. return 0
  61. fi
  62. echo "Replacing key for '$ident'." >&2
  63. fi
  64. echo "$content" > "$target"
  65. /usr/local/sbin/ssh-update-keys
  66. }
  67. case "$1" in
  68. "add")
  69. shift
  70. ssh-key-add "$@"
  71. ;;
  72. "rm")
  73. shift
  74. ssh-key-rm "$@"
  75. ;;
  76. "ls")
  77. shift
  78. ssh-key-ls "$@"
  79. ;;
  80. *)
  81. echo "Unknown command '$1'."
  82. ;;
  83. esac