blob: 8708b7a58954c3af6d4eb316eba7154d693f363d [file] [log] [blame]
Damien Miller1fc231c2008-07-14 12:12:52 +10001#!/bin/bash
Ben Lindstromb100ec92001-01-19 05:37:32 +00002#
Darren Tucker8fdcba52009-07-12 21:58:42 +10003# ssh-user-config, Copyright 2000-2008 Red Hat Inc.
Ben Lindstromb100ec92001-01-19 05:37:32 +00004#
5# This file is part of the Cygwin port of OpenSSH.
Darren Tucker8fdcba52009-07-12 21:58:42 +10006#
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.
Ben Lindstromb100ec92001-01-19 05:37:32 +000018
Damien Miller1fc231c2008-07-14 12:12:52 +100019# ======================================================================
20# Initialization
21# ======================================================================
22PROGNAME=$(basename -- $0)
23_tdir=$(dirname -- $0)
24PROGDIR=$(cd $_tdir && pwd)
25
26CSIH_SCRIPT=/usr/share/csih/cygwin-service-installation-helper.sh
27
28# Subdirectory where the new package is being installed
29PREFIX=/usr
30
Darren Tucker798ca842003-11-13 11:28:49 +110031# Directory where the config files are stored
32SYSCONFDIR=/etc
33
Damien Miller1fc231c2008-07-14 12:12:52 +100034source ${CSIH_SCRIPT}
35
Ben Lindstromb100ec92001-01-19 05:37:32 +000036auto_passphrase="no"
37passphrase=""
Damien Miller1fc231c2008-07-14 12:12:52 +100038pwdhome=
39with_passphrase=
Ben Lindstromb100ec92001-01-19 05:37:32 +000040
Damien Miller1fc231c2008-07-14 12:12:52 +100041# ======================================================================
Darren Tuckerea676a62011-02-06 13:31:23 +110042# Routine: create_identity
43# optionally create identity of type argument in ~/.ssh
Damien Miller1fc231c2008-07-14 12:12:52 +100044# optionally add result to ~/.ssh/authorized_keys
45# ======================================================================
Darren Tuckerea676a62011-02-06 13:31:23 +110046create_identity() {
47 local file="$1"
48 local type="$2"
49 local name="$3"
50 if [ ! -f "${pwdhome}/.ssh/${file}" ]
Ben Lindstromb100ec92001-01-19 05:37:32 +000051 then
Darren Tuckerea676a62011-02-06 13:31:23 +110052 if csih_request "Shall I create a ${name} identity file for you?"
Damien Miller1fc231c2008-07-14 12:12:52 +100053 then
Darren Tuckerea676a62011-02-06 13:31:23 +110054 csih_inform "Generating ${pwdhome}/.ssh/${file}"
Damien Miller1fc231c2008-07-14 12:12:52 +100055 if [ "${with_passphrase}" = "yes" ]
56 then
Darren Tuckerea676a62011-02-06 13:31:23 +110057 ssh-keygen -t "${type}" -N "${passphrase}" -f "${pwdhome}/.ssh/${file}" > /dev/null
Damien Miller1fc231c2008-07-14 12:12:52 +100058 else
Darren Tuckerea676a62011-02-06 13:31:23 +110059 ssh-keygen -t "${type}" -f "${pwdhome}/.ssh/${file}" > /dev/null
Damien Miller1fc231c2008-07-14 12:12:52 +100060 fi
61 if csih_request "Do you want to use this identity to login to this machine?"
62 then
63 csih_inform "Adding to ${pwdhome}/.ssh/authorized_keys"
Darren Tuckerea676a62011-02-06 13:31:23 +110064 cat "${pwdhome}/.ssh/${file}.pub" >> "${pwdhome}/.ssh/authorized_keys"
Damien Miller1fc231c2008-07-14 12:12:52 +100065 fi
66 fi
Ben Lindstromb100ec92001-01-19 05:37:32 +000067 fi
Damien Miller1fc231c2008-07-14 12:12:52 +100068} # === End of create_ssh1_identity() === #
Darren Tuckerea676a62011-02-06 13:31:23 +110069readonly -f create_identity
Damien Miller1fc231c2008-07-14 12:12:52 +100070
71# ======================================================================
72# Routine: check_user_homedir
73# Perform various checks on the user's home directory
74# SETS GLOBAL VARIABLE:
75# pwdhome
76# ======================================================================
77check_user_homedir() {
78 local uid=$(id -u)
79 pwdhome=$(awk -F: '{ if ( $3 == '${uid}' ) print $6; }' < ${SYSCONFDIR}/passwd)
80 if [ "X${pwdhome}" = "X" ]
81 then
Tim Ricecaeb1642009-07-29 07:21:13 -070082 csih_error_multi \
Damien Miller1fc231c2008-07-14 12:12:52 +100083 "There is no home directory set for you in ${SYSCONFDIR}/passwd." \
84 'Setting $HOME is not sufficient!'
85 fi
86
87 if [ ! -d "${pwdhome}" ]
88 then
Tim Ricecaeb1642009-07-29 07:21:13 -070089 csih_error_multi \
Damien Miller1fc231c2008-07-14 12:12:52 +100090 "${pwdhome} is set in ${SYSCONFDIR}/passwd as your home directory" \
91 'but it is not a valid directory. Cannot create user identity files.'
92 fi
93
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/" ]
97 then
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?"
101 then
102 pwdhome=''
103 else
104 csih_warning "Exiting. Configuration is not complete"
105 exit 1
106 fi
107 fi
108
109 if [ -d "${pwdhome}" -a csih_is_nt -a -n "`chmod -c g-w,o-w "${pwdhome}"`" ]
110 then
111 echo
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!'
117 echo
118 fi
119} # === End of check_user_homedir() === #
120readonly -f check_user_homedir
121
122# ======================================================================
123# Routine: check_user_dot_ssh_dir
124# Perform various checks on the ~/.ssh directory
125# PREREQUISITE:
126# pwdhome -- check_user_homedir()
127# ======================================================================
128check_user_dot_ssh_dir() {
129 if [ -e "${pwdhome}/.ssh" -a ! -d "${pwdhome}/.ssh" ]
130 then
131 csih_error "${pwdhome}/.ssh is existant but not a directory. Cannot create user identity files."
132 fi
133
134 if [ ! -e "${pwdhome}/.ssh" ]
135 then
136 mkdir "${pwdhome}/.ssh"
137 if [ ! -e "${pwdhome}/.ssh" ]
138 then
139 csih_error "Creating users ${pwdhome}/.ssh directory failed"
140 fi
141 fi
142} # === End of check_user_dot_ssh_dir() === #
143readonly -f check_user_dot_ssh_dir
144
145# ======================================================================
146# Routine: fix_authorized_keys_perms
147# Corrects the permissions of ~/.ssh/authorized_keys
148# PREREQUISITE:
149# pwdhome -- check_user_homedir()
150# ======================================================================
151fix_authorized_keys_perms() {
152 if [ csih_is_nt -a -e "${pwdhome}/.ssh/authorized_keys" ]
153 then
154 if ! setfacl -m "u::rw-,g::---,o::---" "${pwdhome}/.ssh/authorized_keys"
155 then
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."
159 echo
160 fi
161 fi
162} # === End of fix_authorized_keys_perms() === #
163readonly -f fix_authorized_keys_perms
164
165
166# ======================================================================
167# Main Entry Point
168# ======================================================================
169
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.
180if [ "$PROGDIR" = "/etc/postinstall" ]
Darren Tucker798ca842003-11-13 11:28:49 +1100181then
Damien Miller1fc231c2008-07-14 12:12:52 +1000182 csih_auto_answer="no"
183 csih_disable_color
184fi
185if [ -n "${SSH_USER_CONFIG_AUTO_ANSWER_NO}" ]
186then
187 csih_auto_answer="no"
188 csih_disable_color
Darren Tucker798ca842003-11-13 11:28:49 +1100189fi
190
Damien Miller1fc231c2008-07-14 12:12:52 +1000191# ======================================================================
192# Parse options
193# ======================================================================
Ben Lindstromb100ec92001-01-19 05:37:32 +0000194while :
195do
196 case $# in
197 0)
198 break
199 ;;
200 esac
201
202 option=$1
203 shift
204
205 case "$option" in
206 -d | --debug )
207 set -x
Damien Miller1fc231c2008-07-14 12:12:52 +1000208 csih_trace_on
Ben Lindstromb100ec92001-01-19 05:37:32 +0000209 ;;
210
211 -y | --yes )
Damien Miller1fc231c2008-07-14 12:12:52 +1000212 csih_auto_answer=yes
Ben Lindstromb100ec92001-01-19 05:37:32 +0000213 ;;
214
215 -n | --no )
Damien Miller1fc231c2008-07-14 12:12:52 +1000216 csih_auto_answer=no
Ben Lindstromb100ec92001-01-19 05:37:32 +0000217 ;;
218
219 -p | --passphrase )
220 with_passphrase="yes"
221 passphrase=$1
222 shift
223 ;;
224
225 *)
Damien Miller1fc231c2008-07-14 12:12:52 +1000226 echo "usage: ${PROGNAME} [OPTION]..."
Ben Lindstromb100ec92001-01-19 05:37:32 +0000227 echo
228 echo "This script creates an OpenSSH user configuration."
229 echo
230 echo "Options:"
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."
235 echo
236 exit 1
237 ;;
238
239 esac
240done
241
Damien Miller1fc231c2008-07-14 12:12:52 +1000242# ======================================================================
243# Action!
244# ======================================================================
Ben Lindstromb100ec92001-01-19 05:37:32 +0000245
Damien Miller1fc231c2008-07-14 12:12:52 +1000246# Check passwd file
Darren Tucker798ca842003-11-13 11:28:49 +1100247if [ ! -f ${SYSCONFDIR}/passwd ]
Ben Lindstromb100ec92001-01-19 05:37:32 +0000248then
Tim Ricecaeb1642009-07-29 07:21:13 -0700249 csih_error_multi \
Damien Miller1fc231c2008-07-14 12:12:52 +1000250 "${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.'
Ben Lindstromb100ec92001-01-19 05:37:32 +0000253fi
254
Damien Miller1fc231c2008-07-14 12:12:52 +1000255check_user_homedir
256check_user_dot_ssh_dir
Darren Tuckerea676a62011-02-06 13:31:23 +1100257create_identity id_rsa rsa "SSH2 RSA"
258create_identity id_dsa dsa "SSH2 DSA"
259create_identity id_ecdsa ecdsa "SSH2 ECDSA"
260create_identity identity rsa1 "(deprecated) SSH1 RSA"
Damien Miller1fc231c2008-07-14 12:12:52 +1000261fix_authorized_keys_perms
Darren Tucker798ca842003-11-13 11:28:49 +1100262
Ben Lindstromb100ec92001-01-19 05:37:32 +0000263echo
Damien Miller1fc231c2008-07-14 12:12:52 +1000264csih_inform "Configuration finished. Have fun!"
265
266