ssh-user-config 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #!/bin/bash
  2. #
  3. # ssh-user-config, Copyright 2000-2014 Red Hat Inc.
  4. #
  5. # This file is part of the Cygwin port of OpenSSH.
  6. #
  7. # Permission to use, copy, modify, and distribute this software for any
  8. # purpose with or without fee is hereby granted, provided that the above
  9. # copyright notice and this permission notice appear in all copies.
  10. #
  11. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  12. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  13. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  14. # IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  15. # DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. # OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
  17. # THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. # ======================================================================
  19. # Initialization
  20. # ======================================================================
  21. PROGNAME=$(basename -- $0)
  22. _tdir=$(dirname -- $0)
  23. PROGDIR=$(cd $_tdir && pwd)
  24. CSIH_SCRIPT=/usr/share/csih/cygwin-service-installation-helper.sh
  25. # Subdirectory where the new package is being installed
  26. PREFIX=/usr
  27. # Directory where the config files are stored
  28. SYSCONFDIR=/etc
  29. source ${CSIH_SCRIPT}
  30. auto_passphrase="no"
  31. passphrase=""
  32. pwdhome=
  33. with_passphrase=
  34. # ======================================================================
  35. # Routine: create_identity
  36. # optionally create identity of type argument in ~/.ssh
  37. # optionally add result to ~/.ssh/authorized_keys
  38. # ======================================================================
  39. create_identity()
  40. {
  41. local file="$1"
  42. local type="$2"
  43. local name="$3"
  44. if [ ! -f "${pwdhome}/.ssh/${file}" ]; then
  45. if csih_request "Shall I create a ${name} identity file for you?"; then
  46. csih_inform "Generating ${pwdhome}/.ssh/${file}"
  47. if [ "${with_passphrase}" = "yes" ]; then
  48. ssh-keygen -t "${type}" -N "${passphrase}" -f "${pwdhome}/.ssh/${file}" > /dev/null
  49. else
  50. ssh-keygen -t "${type}" -f "${pwdhome}/.ssh/${file}" > /dev/null
  51. fi
  52. if csih_request "Do you want to use this identity to login to this machine?"; then
  53. csih_inform "Adding to ${pwdhome}/.ssh/authorized_keys"
  54. cat "${pwdhome}/.ssh/${file}.pub" >> "${pwdhome}/.ssh/authorized_keys"
  55. fi
  56. fi
  57. fi
  58. } # === End of create_ssh1_identity() === #
  59. readonly -f create_identity
  60. # ======================================================================
  61. # Routine: check_user_homedir
  62. # Perform various checks on the user's home directory
  63. # SETS GLOBAL VARIABLE:
  64. # pwdhome
  65. # ======================================================================
  66. check_user_homedir()
  67. {
  68. pwdhome=$(getent passwd $UID | awk -F: '{ print $6; }')
  69. if [ "X${pwdhome}" = "X" ]; then
  70. csih_error_multi \
  71. "There is no home directory set for you in the account database." \
  72. 'Setting $HOME is not sufficient!'
  73. fi
  74. if [ ! -d "${pwdhome}" ]; then
  75. csih_error_multi \
  76. "${pwdhome} is set in the account database as your home directory" \
  77. 'but it is not a valid directory. Cannot create user identity files.'
  78. fi
  79. # If home is the root dir, set home to empty string to avoid error messages
  80. # in subsequent parts of that script.
  81. if [ "X${pwdhome}" = "X/" ]; then
  82. # But first raise a warning!
  83. csih_warning "Your home directory in the account database is set to root (/). This is not recommended!"
  84. if csih_request "Would you like to proceed anyway?"; then
  85. pwdhome=''
  86. else
  87. csih_warning "Exiting. Configuration is not complete"
  88. exit 1
  89. fi
  90. fi
  91. if [ -d "${pwdhome}" -a -n "$(chmod -c g-w,o-w "${pwdhome}")" ]; then
  92. echo
  93. csih_warning 'group and other have been revoked write permission to your home'
  94. csih_warning "directory ${pwdhome}."
  95. csih_warning 'This is required by OpenSSH to allow public key authentication using'
  96. csih_warning 'the key files stored in your .ssh subdirectory.'
  97. csih_warning 'Revert this change ONLY if you know what you are doing!'
  98. echo
  99. fi
  100. } # === End of check_user_homedir() === #
  101. readonly -f check_user_homedir
  102. # ======================================================================
  103. # Routine: check_user_dot_ssh_dir
  104. # Perform various checks on the ~/.ssh directory
  105. # PREREQUISITE:
  106. # pwdhome -- check_user_homedir()
  107. # ======================================================================
  108. check_user_dot_ssh_dir()
  109. {
  110. if [ -e "${pwdhome}/.ssh" -a ! -d "${pwdhome}/.ssh" ]; then
  111. csih_error "${pwdhome}/.ssh is existent but not a directory. Cannot create user identity files."
  112. fi
  113. if [ ! -e "${pwdhome}/.ssh" ]; then
  114. mkdir "${pwdhome}/.ssh"
  115. if [ ! -e "${pwdhome}/.ssh" ]; then
  116. csih_error "Creating users ${pwdhome}/.ssh directory failed"
  117. fi
  118. fi
  119. } # === End of check_user_dot_ssh_dir() === #
  120. readonly -f check_user_dot_ssh_dir
  121. # ======================================================================
  122. # Routine: fix_authorized_keys_perms
  123. # Corrects the permissions of ~/.ssh/authorized_keys
  124. # PREREQUISITE:
  125. # pwdhome -- check_user_homedir()
  126. # ======================================================================
  127. fix_authorized_keys_perms()
  128. {
  129. if [ -e "${pwdhome}/.ssh/authorized_keys" ]; then
  130. setfacl -b "${pwdhome}/.ssh/authorized_keys" 2> /dev/null || echo -n
  131. if ! chmod u-x,g-wx,o-wx "${pwdhome}/.ssh/authorized_keys"; then
  132. csih_warning "Setting correct permissions to ${pwdhome}/.ssh/authorized_keys"
  133. csih_warning "failed. Please care for the correct permissions. The minimum requirement"
  134. csih_warning "is, the owner needs read permissions."
  135. echo
  136. fi
  137. fi
  138. } # === End of fix_authorized_keys_perms() === #
  139. readonly -f fix_authorized_keys_perms
  140. # ======================================================================
  141. # Main Entry Point
  142. # ======================================================================
  143. # Check how the script has been started. If
  144. # (1) it has been started by giving the full path and
  145. # that path is /etc/postinstall, OR
  146. # (2) Otherwise, if the environment variable
  147. # SSH_USER_CONFIG_AUTO_ANSWER_NO is set
  148. # then set auto_answer to "no". This allows automatic
  149. # creation of the config files in /etc w/o overwriting
  150. # them if they already exist. In both cases, color
  151. # escape sequences are suppressed, so as to prevent
  152. # cluttering setup's logfiles.
  153. if [ "$PROGDIR" = "/etc/postinstall" ]; then
  154. csih_auto_answer="no"
  155. csih_disable_color
  156. fi
  157. if [ -n "${SSH_USER_CONFIG_AUTO_ANSWER_NO}" ]; then
  158. csih_auto_answer="no"
  159. csih_disable_color
  160. fi
  161. # ======================================================================
  162. # Parse options
  163. # ======================================================================
  164. while :; do
  165. case $# in
  166. 0)
  167. break
  168. ;;
  169. esac
  170. option=$1
  171. shift
  172. case "$option" in
  173. -d | --debug)
  174. set -x
  175. csih_trace_on
  176. ;;
  177. -y | --yes)
  178. csih_auto_answer=yes
  179. ;;
  180. -n | --no)
  181. csih_auto_answer=no
  182. ;;
  183. -p | --passphrase)
  184. with_passphrase="yes"
  185. passphrase=$1
  186. shift
  187. ;;
  188. *)
  189. echo "usage: ${PROGNAME} [OPTION]..."
  190. echo
  191. echo "This script creates an OpenSSH user configuration."
  192. echo
  193. echo "Options:"
  194. echo " --debug -d Enable shell's debug output."
  195. echo ' --yes -y Answer all questions with "yes" automatically.'
  196. echo ' --no -n Answer all questions with "no" automatically.'
  197. echo ' --passphrase -p word Use "word" as passphrase automatically.'
  198. echo
  199. exit 1
  200. ;;
  201. esac
  202. done
  203. # ======================================================================
  204. # Action!
  205. # ======================================================================
  206. check_user_homedir
  207. check_user_dot_ssh_dir
  208. create_identity id_rsa rsa "SSH2 RSA"
  209. create_identity id_dsa dsa "SSH2 DSA"
  210. create_identity id_ecdsa ecdsa "SSH2 ECDSA"
  211. create_identity identity rsa1 "(deprecated) SSH1 RSA"
  212. fix_authorized_keys_perms
  213. echo
  214. csih_inform "Configuration finished. Have fun!"