/home/coltondrg/.ssh/authorized_keys https://git.coltondrg.com/ssh-keystore/
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.
 
 

181 lines
4.9 KiB

  1. #!/usr/bin/env bash
  2. # coltondrg?'s ssh keystore
  3. # Script to generate authorized_keys file
  4. # This script is under the MIT License. You can do whatever you want with it!
  5. {
  6. function do_cd() {
  7. cd "$(dirname "$0")"
  8. }
  9. function build_vars() {
  10. SCRIPT_REV="v14"
  11. if [ "$INSTALL_FILE" = "" ]; then
  12. INSTALL_FILE="$HOME/.ssh/authorized_keys"
  13. fi
  14. PROJECT_URL="https://git.coltondrg.com/ssh-keystore"
  15. SCRIPT_DOCUMENTATION="
  16. coltondrg?'s ssh keystore $(git rev-parse --short HEAD) ($PROJECT_URL)
  17. authorized_keys generation (install) script $SCRIPT_REV
  18. Usage: ./install [options]
  19. Available Options:
  20. -help Print this help message and exit
  21. -auto Don't confirm before overwriting authorized_keys
  22. -pull Update to the latest commit before updating
  23. -cron Same as -pull and -auto together. For use in cron jobs
  24. -file <file> Specify the location of the authorized_keys file.
  25. Default is $HOME/.ssh/authorized_keys
  26. -perm CHMOD option, resets install file's permissions to 600
  27. Fixes some distros (like Fedora), also sets dir to 700
  28. Use \`git pull\` to update the keys & scripts
  29. "
  30. }
  31. function get_options() {
  32. case $1 in
  33. -help|--help|help|-h|"-?"|-version|--version|version|-v)
  34. do_cd
  35. build_vars
  36. echo "$SCRIPT_DOCUMENTATION"
  37. exit 0
  38. ;;
  39. -file|--file|file|-f)
  40. shift
  41. INSTALL_FILE="$(readlink -m "$1")"
  42. shift
  43. get_options "$@"
  44. ;;
  45. -auto|--auto|auto|-a)
  46. OPT_NOCONFIRM="true"
  47. shift
  48. get_options "$@"
  49. ;;
  50. -pull|--pull|pull|-p)
  51. OPT_DO_PULL="true"
  52. shift
  53. get_options "$@"
  54. ;;
  55. -cron|--cron|cron|-c)
  56. OPT_NOCONFIRM="true"
  57. OPT_DO_PULL="true"
  58. shift
  59. get_options "$@"
  60. ;;
  61. -perm|--perm|perm|-m)
  62. OPT_CHMOD="true"
  63. shift
  64. get_options "$@"
  65. ;;
  66. "")
  67. do_cd
  68. build_vars
  69. do_the_thing
  70. ;;
  71. *)
  72. echo "Invalid option: $1"
  73. echo "Use \`./install -help\` to learn how to use the script or get version information."
  74. exit 1
  75. ;;
  76. esac
  77. }
  78. function get_md5() {
  79. if [ $(command -v md5sum) ];then
  80. md5sum "$@"
  81. elif [ $(command -v openssl) ];then
  82. openssl md5 "$@"
  83. elif [ $(command -v md5) ];then
  84. md5 "$@"
  85. else
  86. echo "no_md5_err"
  87. fi
  88. }
  89. function do_the_thing() {
  90. if [ "$OPT_DO_PULL" = "true" ]; then
  91. SCRIPT_MD5=$(get_md5 install)
  92. git pull
  93. if [ "$SCRIPT_MD5" = "no_md5_err" ];then
  94. echo "Unable to verify the integrity of this script."
  95. if [ ! "$SCRIPT_RERUN" ];then
  96. export SCRIPT_RERUN=1
  97. "$0" "${ORIGINAL_ARGS[@]}"
  98. SCRIPT_EXIT=$?
  99. export SCRIPT_RERUN=""
  100. exit $SCRIPT_EXIT
  101. fi
  102. elif [ "$SCRIPT_MD5" != "$(get_md5 install)" ]; then
  103. echo "Script updated, re-running."
  104. "$0" "${ORIGINAL_ARGS[@]}"
  105. exit $?
  106. else
  107. echo "Script OK!"
  108. fi
  109. fi
  110. echo "coltondrg's ssh keystore authorized_keys generation script $SCRIPT_REV"
  111. echo "The keys from commit $(git rev-parse --short HEAD) will be used to generate an authorized_keys file"
  112. echo "The authorized_keys file generated will be placed at $INSTALL_FILE"
  113. echo "Note that the existing contents of $INSTALL_FILE will be destroyed."
  114. if [ "$OPT_CHMOD" = "true" ]; then
  115. echo "CHMOD option activated (with -perm or -m)"
  116. echo "Permissions for $(dirname "$INSTALL_FILE") will be reset to 700"
  117. echo "Permissions for $INSTALL_FILE will be reset to 600"
  118. echo "On some distros these exact modes are required for security purposes"
  119. fi
  120. echo "For help on using this script, do \`./install -help\`"
  121. if [ "$OPT_NOCONFIRM" != "true" ]; then
  122. echo "If these are correct, press [Enter]. Press [Ctrl+C] to cancel."
  123. read _
  124. fi
  125. INSTALL_DIR=$(dirname "$INSTALL_FILE")
  126. if [ ! -d "$INSTALL_DIR" ];then
  127. echo "$INSTALL_DIR does not exist, creating it."
  128. mkdir -p "$INSTALL_DIR" || { echo "Failed to create $INSTALL_DIR."; exit 4; }
  129. fi
  130. touch "$INSTALL_FILE" || { echo "Unable to write to $INSTALL_FILE."; exit 2; }
  131. if [ ! -w "$INSTALL_FILE" ];then
  132. echo "Unable to write to $INSTALL_FILE."
  133. exit 2
  134. fi
  135. echo "# authorized_keys generated by coltondrg?'s ssh keystore install script $SCRIPT_REV ($PROJECT_URL)" > "$INSTALL_FILE"
  136. echo "# Generated from $(git config --get remote.origin.url) commit $(git rev-parse --short HEAD) on $(date -u +%Y/%m/%d\ %H:%M)" >> "$INSTALL_FILE"
  137. echo >> "$INSTALL_FILE"
  138. cat *.pub >> "$INSTALL_FILE"
  139. if [ -x "postinstall" ]; then
  140. echo "Executing postinstall script."
  141. export -f add_extra_key
  142. export -f add_mobile_key
  143. export -f add_comment
  144. export INSTALL_FILE="$INSTALL_FILE"
  145. "./postinstall" "${ORIGINAL_ARGS[@]}"
  146. fi
  147. if [ "$OPT_CHMOD" = "true" ]; then
  148. chmod 600 "$INSTALL_FILE" || { echo "Failed to set mode 600 on $INSTALL_FILE."; exit 7; }
  149. chmod 700 "$INSTALL_DIR" || { echo "Failed to set mode 700 on $INSTALL_DIR."; exit 7; }
  150. fi
  151. echo "Process complete"
  152. exit 0
  153. }
  154. function add_extra_key() {
  155. if [ -f "extras/$1.pub" ];then
  156. cat "extras/$1.pub" >> "$INSTALL_FILE"
  157. fi
  158. }
  159. function add_mobile_key() {
  160. if [ -f "mobile/$1.pub" ];then
  161. cat "mobile/$1.pub" >> "$INSTALL_FILE"
  162. fi
  163. }
  164. function add_comment() {
  165. echo "$1" >> "$INSTALL_FILE"
  166. }
  167. ORIGINAL_ARGS=("$@")
  168. get_options "$@"
  169. }