3 # ssh-user-config, Copyright 2000-2008 Red Hat Inc.
5 # This file is part of the Cygwin port of OpenSSH.
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.
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.
19 # ======================================================================
21 # ======================================================================
22 PROGNAME=$(basename -- $0)
23 _tdir=$(dirname -- $0)
24 PROGDIR=$(cd $_tdir && pwd)
26 CSIH_SCRIPT=/usr/share/csih/cygwin-service-installation-helper.sh
28 # Subdirectory where the new package is being installed
31 # Directory where the config files are stored
41 # ======================================================================
42 # Routine: create_identity
43 # optionally create identity of type argument in ~/.ssh
44 # optionally add result to ~/.ssh/authorized_keys
45 # ======================================================================
50 if [ ! -f "${pwdhome}/.ssh/${file}" ]
52 if csih_request "Shall I create a ${name} identity file for you?"
54 csih_inform "Generating ${pwdhome}/.ssh/${file}"
55 if [ "${with_passphrase}" = "yes" ]
57 ssh-keygen -t "${type}" -N "${passphrase}" -f "${pwdhome}/.ssh/${file}" > /dev/null
59 ssh-keygen -t "${type}" -f "${pwdhome}/.ssh/${file}" > /dev/null
61 if csih_request "Do you want to use this identity to login to this machine?"
63 csih_inform "Adding to ${pwdhome}/.ssh/authorized_keys"
64 cat "${pwdhome}/.ssh/${file}.pub" >> "${pwdhome}/.ssh/authorized_keys"
68 } # === End of create_ssh1_identity() === #
69 readonly -f create_identity
71 # ======================================================================
72 # Routine: check_user_homedir
73 # Perform various checks on the user's home directory
74 # SETS GLOBAL VARIABLE:
76 # ======================================================================
77 check_user_homedir() {
79 pwdhome=$(awk -F: '{ if ( $3 == '${uid}' ) print $6; }' < ${SYSCONFDIR}/passwd)
80 if [ "X${pwdhome}" = "X" ]
83 "There is no home directory set for you in ${SYSCONFDIR}/passwd." \
84 'Setting $HOME is not sufficient!'
87 if [ ! -d "${pwdhome}" ]
90 "${pwdhome} is set in ${SYSCONFDIR}/passwd as your home directory" \
91 'but it is not a valid directory. Cannot create user identity files.'
94 # If home is the root dir, set home to empty string to avoid error messages
95 # in subsequent parts of that script.
96 if [ "X${pwdhome}" = "X/" ]
98 # But first raise a warning!
99 csih_warning "Your home directory in ${SYSCONFDIR}/passwd is set to root (/). This is not recommended!"
100 if csih_request "Would you like to proceed anyway?"
104 csih_warning "Exiting. Configuration is not complete"
109 if [ -d "${pwdhome}" -a csih_is_nt -a -n "`chmod -c g-w,o-w "${pwdhome}"`" ]
112 csih_warning 'group and other have been revoked write permission to your home'
113 csih_warning "directory ${pwdhome}."
114 csih_warning 'This is required by OpenSSH to allow public key authentication using'
115 csih_warning 'the key files stored in your .ssh subdirectory.'
116 csih_warning 'Revert this change ONLY if you know what you are doing!'
119 } # === End of check_user_homedir() === #
120 readonly -f check_user_homedir
122 # ======================================================================
123 # Routine: check_user_dot_ssh_dir
124 # Perform various checks on the ~/.ssh directory
126 # pwdhome -- check_user_homedir()
127 # ======================================================================
128 check_user_dot_ssh_dir() {
129 if [ -e "${pwdhome}/.ssh" -a ! -d "${pwdhome}/.ssh" ]
131 csih_error "${pwdhome}/.ssh is existant but not a directory. Cannot create user identity files."
134 if [ ! -e "${pwdhome}/.ssh" ]
136 mkdir "${pwdhome}/.ssh"
137 if [ ! -e "${pwdhome}/.ssh" ]
139 csih_error "Creating users ${pwdhome}/.ssh directory failed"
142 } # === End of check_user_dot_ssh_dir() === #
143 readonly -f check_user_dot_ssh_dir
145 # ======================================================================
146 # Routine: fix_authorized_keys_perms
147 # Corrects the permissions of ~/.ssh/authorized_keys
149 # pwdhome -- check_user_homedir()
150 # ======================================================================
151 fix_authorized_keys_perms() {
152 if [ csih_is_nt -a -e "${pwdhome}/.ssh/authorized_keys" ]
154 if ! setfacl -m "u::rw-,g::---,o::---" "${pwdhome}/.ssh/authorized_keys"
156 csih_warning "Setting correct permissions to ${pwdhome}/.ssh/authorized_keys"
157 csih_warning "failed. Please care for the correct permissions. The minimum requirement"
158 csih_warning "is, the owner needs read permissions."
162 } # === End of fix_authorized_keys_perms() === #
163 readonly -f fix_authorized_keys_perms
166 # ======================================================================
168 # ======================================================================
170 # Check how the script has been started. If
171 # (1) it has been started by giving the full path and
172 # that path is /etc/postinstall, OR
173 # (2) Otherwise, if the environment variable
174 # SSH_USER_CONFIG_AUTO_ANSWER_NO is set
175 # then set auto_answer to "no". This allows automatic
176 # creation of the config files in /etc w/o overwriting
177 # them if they already exist. In both cases, color
178 # escape sequences are suppressed, so as to prevent
179 # cluttering setup's logfiles.
180 if [ "$PROGDIR" = "/etc/postinstall" ]
182 csih_auto_answer="no"
185 if [ -n "${SSH_USER_CONFIG_AUTO_ANSWER_NO}" ]
187 csih_auto_answer="no"
191 # ======================================================================
193 # ======================================================================
220 with_passphrase="yes"
226 echo "usage: ${PROGNAME} [OPTION]..."
228 echo "This script creates an OpenSSH user configuration."
231 echo " --debug -d Enable shell's debug output."
232 echo " --yes -y Answer all questions with \"yes\" automatically."
233 echo " --no -n Answer all questions with \"no\" automatically."
234 echo " --passphrase -p word Use \"word\" as passphrase automatically."
242 # ======================================================================
244 # ======================================================================
247 if [ ! -f ${SYSCONFDIR}/passwd ]
250 "${SYSCONFDIR}/passwd is nonexistant. Please generate an ${SYSCONFDIR}/passwd file" \
251 'first using mkpasswd. Check if it contains an entry for you and' \
252 'please care for the home directory in your entry as well.'
256 check_user_dot_ssh_dir
257 create_identity id_rsa rsa "SSH2 RSA"
258 create_identity id_dsa dsa "SSH2 DSA"
259 create_identity id_ecdsa ecdsa "SSH2 ECDSA"
260 create_identity identity rsa1 "(deprecated) SSH1 RSA"
261 fix_authorized_keys_perms
264 csih_inform "Configuration finished. Have fun!"