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.

134 lines
3.4 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 label="$1" f content
  23. for f in "${RSYNC_KEY_PATH}"/backup/"$label"/*.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 label="$1" ident="$2" delete
  35. delete="${RSYNC_KEY_PATH}/backup/$label/$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 label="$1" type="$2" key="$3" email="$4"
  45. [ "$type" == "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. content="$type $key $email"
  53. ident="${email##*@}"
  54. target="${RSYNC_KEY_PATH}/backup/$label/$ident.pub"
  55. ## is key used already ? As key give access to a specified subdir,
  56. ## we need to make sure it is unique.
  57. for key_file in "${RSYNC_KEY_PATH}/backup/"*/*.pub; do
  58. [ -e "$key_file" ] || continue
  59. key_content=$(cat "$key_file")
  60. if [ "$type $key" == "${key_content% *}" ]; then
  61. if [ "$key_file" == "$target" ]; then
  62. echo "Provided key already present for '$ident'." >&2
  63. return 0
  64. elif [[ "$key_file" == "${RSYNC_KEY_PATH}/"*"/$label/"*.pub ]]; then
  65. type=${key_file#"${RSYNC_KEY_PATH}/"}
  66. type=${type%"/$label/"*.pub}
  67. key_ident=${key_file##*/}
  68. key_ident=${key_ident%.pub}
  69. echo "Provided key already used as $type key for '$key_ident'." >&2
  70. return 1
  71. else
  72. olabel=${key_file#"${RSYNC_KEY_PATH}/"*/}
  73. olabel=${olabel%/*.pub}
  74. echo "Specified key is already used by '$olabel' account, please pick another one." >&2
  75. return 1
  76. fi
  77. fi
  78. done
  79. mkdir -p "${target%/*}"
  80. if [ -e "$target" ]; then
  81. echo "Replacing key for '$ident'." >&2
  82. elif [ -e "${RSYNC_KEY_PATH}/"*"/"*"/$ident.pub" ]; then
  83. olabel=("${RSYNC_KEY_PATH}/"*"/"*"/$ident.pub")
  84. olabel="${olabel[0]}"
  85. olabel=${olabel#"${RSYNC_KEY_PATH}/"*/}
  86. olabel=${olabel%/*.pub}
  87. echo "ident '$ident' is already reserved by '$olabel', please pick another one." >&2
  88. return 1
  89. fi
  90. echo "$content" > "$target"
  91. /usr/local/sbin/ssh-update-keys
  92. }
  93. case "$1" in
  94. "add")
  95. shift
  96. ssh-key-add "$@"
  97. ;;
  98. "rm")
  99. shift
  100. ssh-key-rm "$@"
  101. ;;
  102. "ls")
  103. shift
  104. ssh-key-ls "$@"
  105. ;;
  106. *)
  107. echo "Unknown command '$1'."
  108. ;;
  109. esac