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.

68 lines
1.9 KiB

  1. #!/bin/bash
  2. ## Keep in mind possible race conditions as this script will be called
  3. ## from different place to update the access tokens.
  4. ##
  5. ## Code
  6. ##
  7. RSYNC_KEY_PATH=/etc/rsync/keys
  8. RSYNC_HOME=/var/lib/rsync
  9. BACKUP_KEY_PATH=${RSYNC_KEY_PATH}/backup
  10. RECOVER_KEY_PATH=${RSYNC_KEY_PATH}/recover
  11. mkdir -p "$RSYNC_HOME/.ssh" "$RECOVER_KEY_PATH"
  12. ## delete old recovery keys
  13. find "${RECOVER_KEY_PATH}" \
  14. -maxdepth 1 -not -newermt "-15 minutes" \
  15. -type f -delete
  16. ##
  17. ## New
  18. ##
  19. pid=$$
  20. new="$RSYNC_HOME"/.ssh/authorized_keys.tmp."$pid"
  21. touch "$new"
  22. for f in "$BACKUP_KEY_PATH"/*/*.pub "$RECOVER_KEY_PATH"/*.pub; do
  23. [ -e "$f" ] || continue
  24. content=$(cat "$f")
  25. if [[ "$content" == *" "*" "*@* ]]; then
  26. ident="${content##*@}"
  27. else
  28. ident="${f##*/}"
  29. ident="${ident%.pub}"
  30. fi
  31. if ! [[ "$ident" =~ ^[a-zA-Z0-9._-]+$ ]]; then
  32. echo "bad: '$ident'" >&2
  33. continue
  34. fi
  35. if [[ "$f" == "${RECOVER_KEY_PATH}"/*.pub ]]; then
  36. basename=${f##*/}
  37. basename=${basename%.pub}
  38. cmd="/usr/local/sbin/ssh-recover-cmd-validate $basename"
  39. else
  40. cmd=/usr/local/sbin/ssh-cmd-validate
  41. fi
  42. echo "command=\"$cmd \\\"$ident\\\"\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty $content"
  43. done >> "$new"
  44. [ -e "$RSYNC_HOME"/.ssh/authorized_keys ] &&
  45. mv "$RSYNC_HOME"/.ssh/authorized_keys{,.old}
  46. ## XXXvlab: Atomic operation. It's the last call to this instruction
  47. ## that will prevail. There are some very special cases where some
  48. ## added key would not be added as expected: for instance an older
  49. ## call to ``ssh-update-key``, if made before a specific public key
  50. ## file was added to directory, could take a longer time to reach this
  51. ## next instruction than a more recent call (that would be after
  52. ## the specific public key was added).
  53. mv "$new" "$RSYNC_HOME"/.ssh/authorized_keys
  54. chown rsync:rsync "$RSYNC_HOME"/.ssh -R