2008-07-14 02:12:52 +00:00
|
|
|
#!/bin/bash
|
2001-01-19 05:37:32 +00:00
|
|
|
#
|
2014-11-05 00:01:31 +00:00
|
|
|
# ssh-user-config, Copyright 2000-2014 Red Hat Inc.
|
2001-01-19 05:37:32 +00:00
|
|
|
#
|
|
|
|
# This file is part of the Cygwin port of OpenSSH.
|
2009-07-12 11:58:42 +00:00
|
|
|
#
|
|
|
|
# Permission to use, copy, modify, and distribute this software for any
|
|
|
|
# purpose with or without fee is hereby granted, provided that the above
|
|
|
|
# copyright notice and this permission notice appear in all copies.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
# IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
|
|
|
|
# THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2001-01-19 05:37:32 +00:00
|
|
|
|
2008-07-14 02:12:52 +00:00
|
|
|
# ======================================================================
|
|
|
|
# Initialization
|
|
|
|
# ======================================================================
|
|
|
|
PROGNAME=$(basename -- $0)
|
|
|
|
_tdir=$(dirname -- $0)
|
|
|
|
PROGDIR=$(cd $_tdir && pwd)
|
|
|
|
|
|
|
|
CSIH_SCRIPT=/usr/share/csih/cygwin-service-installation-helper.sh
|
|
|
|
|
|
|
|
# Subdirectory where the new package is being installed
|
|
|
|
PREFIX=/usr
|
|
|
|
|
2003-11-13 00:28:49 +00:00
|
|
|
# Directory where the config files are stored
|
|
|
|
SYSCONFDIR=/etc
|
|
|
|
|
2008-07-14 02:12:52 +00:00
|
|
|
source ${CSIH_SCRIPT}
|
|
|
|
|
2001-01-19 05:37:32 +00:00
|
|
|
auto_passphrase="no"
|
|
|
|
passphrase=""
|
2008-07-14 02:12:52 +00:00
|
|
|
pwdhome=
|
|
|
|
with_passphrase=
|
|
|
|
|
|
|
|
# ======================================================================
|
2011-02-06 02:31:23 +00:00
|
|
|
# Routine: create_identity
|
|
|
|
# optionally create identity of type argument in ~/.ssh
|
2008-07-14 02:12:52 +00:00
|
|
|
# optionally add result to ~/.ssh/authorized_keys
|
|
|
|
# ======================================================================
|
2011-02-06 02:31:23 +00:00
|
|
|
create_identity() {
|
|
|
|
local file="$1"
|
|
|
|
local type="$2"
|
|
|
|
local name="$3"
|
|
|
|
if [ ! -f "${pwdhome}/.ssh/${file}" ]
|
2008-07-14 02:12:52 +00:00
|
|
|
then
|
2011-02-06 02:31:23 +00:00
|
|
|
if csih_request "Shall I create a ${name} identity file for you?"
|
2008-07-14 02:12:52 +00:00
|
|
|
then
|
2011-02-06 02:31:23 +00:00
|
|
|
csih_inform "Generating ${pwdhome}/.ssh/${file}"
|
2008-07-14 02:12:52 +00:00
|
|
|
if [ "${with_passphrase}" = "yes" ]
|
|
|
|
then
|
2011-02-06 02:31:23 +00:00
|
|
|
ssh-keygen -t "${type}" -N "${passphrase}" -f "${pwdhome}/.ssh/${file}" > /dev/null
|
2008-07-14 02:12:52 +00:00
|
|
|
else
|
2011-02-06 02:31:23 +00:00
|
|
|
ssh-keygen -t "${type}" -f "${pwdhome}/.ssh/${file}" > /dev/null
|
2008-07-14 02:12:52 +00:00
|
|
|
fi
|
|
|
|
if csih_request "Do you want to use this identity to login to this machine?"
|
|
|
|
then
|
|
|
|
csih_inform "Adding to ${pwdhome}/.ssh/authorized_keys"
|
2011-02-06 02:31:23 +00:00
|
|
|
cat "${pwdhome}/.ssh/${file}.pub" >> "${pwdhome}/.ssh/authorized_keys"
|
2008-07-14 02:12:52 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
} # === End of create_ssh1_identity() === #
|
2011-02-06 02:31:23 +00:00
|
|
|
readonly -f create_identity
|
2008-07-14 02:12:52 +00:00
|
|
|
|
|
|
|
# ======================================================================
|
|
|
|
# Routine: check_user_homedir
|
|
|
|
# Perform various checks on the user's home directory
|
|
|
|
# SETS GLOBAL VARIABLE:
|
|
|
|
# pwdhome
|
|
|
|
# ======================================================================
|
|
|
|
check_user_homedir() {
|
2014-11-05 00:01:31 +00:00
|
|
|
pwdhome=$(getent passwd $UID | awk -F: '{ print $6; }')
|
2008-07-14 02:12:52 +00:00
|
|
|
if [ "X${pwdhome}" = "X" ]
|
|
|
|
then
|
2009-07-29 14:21:13 +00:00
|
|
|
csih_error_multi \
|
2014-11-05 00:01:31 +00:00
|
|
|
"There is no home directory set for you in the account database." \
|
2008-07-14 02:12:52 +00:00
|
|
|
'Setting $HOME is not sufficient!'
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -d "${pwdhome}" ]
|
|
|
|
then
|
2009-07-29 14:21:13 +00:00
|
|
|
csih_error_multi \
|
2014-11-05 00:01:31 +00:00
|
|
|
"${pwdhome} is set in the account database as your home directory" \
|
2008-07-14 02:12:52 +00:00
|
|
|
'but it is not a valid directory. Cannot create user identity files.'
|
|
|
|
fi
|
|
|
|
|
|
|
|
# If home is the root dir, set home to empty string to avoid error messages
|
|
|
|
# in subsequent parts of that script.
|
|
|
|
if [ "X${pwdhome}" = "X/" ]
|
|
|
|
then
|
|
|
|
# But first raise a warning!
|
2014-11-05 00:01:31 +00:00
|
|
|
csih_warning "Your home directory in the account database is set to root (/). This is not recommended!"
|
2008-07-14 02:12:52 +00:00
|
|
|
if csih_request "Would you like to proceed anyway?"
|
|
|
|
then
|
|
|
|
pwdhome=''
|
|
|
|
else
|
|
|
|
csih_warning "Exiting. Configuration is not complete"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2014-11-05 00:01:31 +00:00
|
|
|
if [ -d "${pwdhome}" -a -n "`chmod -c g-w,o-w "${pwdhome}"`" ]
|
2008-07-14 02:12:52 +00:00
|
|
|
then
|
|
|
|
echo
|
|
|
|
csih_warning 'group and other have been revoked write permission to your home'
|
|
|
|
csih_warning "directory ${pwdhome}."
|
|
|
|
csih_warning 'This is required by OpenSSH to allow public key authentication using'
|
|
|
|
csih_warning 'the key files stored in your .ssh subdirectory.'
|
|
|
|
csih_warning 'Revert this change ONLY if you know what you are doing!'
|
|
|
|
echo
|
|
|
|
fi
|
|
|
|
} # === End of check_user_homedir() === #
|
|
|
|
readonly -f check_user_homedir
|
2001-01-19 05:37:32 +00:00
|
|
|
|
2008-07-14 02:12:52 +00:00
|
|
|
# ======================================================================
|
|
|
|
# Routine: check_user_dot_ssh_dir
|
|
|
|
# Perform various checks on the ~/.ssh directory
|
|
|
|
# PREREQUISITE:
|
|
|
|
# pwdhome -- check_user_homedir()
|
|
|
|
# ======================================================================
|
|
|
|
check_user_dot_ssh_dir() {
|
|
|
|
if [ -e "${pwdhome}/.ssh" -a ! -d "${pwdhome}/.ssh" ]
|
2001-01-19 05:37:32 +00:00
|
|
|
then
|
2018-04-10 00:19:02 +00:00
|
|
|
csih_error "${pwdhome}/.ssh is existent but not a directory. Cannot create user identity files."
|
2008-07-14 02:12:52 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -e "${pwdhome}/.ssh" ]
|
2001-01-19 05:37:32 +00:00
|
|
|
then
|
2008-07-14 02:12:52 +00:00
|
|
|
mkdir "${pwdhome}/.ssh"
|
|
|
|
if [ ! -e "${pwdhome}/.ssh" ]
|
|
|
|
then
|
|
|
|
csih_error "Creating users ${pwdhome}/.ssh directory failed"
|
|
|
|
fi
|
2001-01-19 05:37:32 +00:00
|
|
|
fi
|
2008-07-14 02:12:52 +00:00
|
|
|
} # === End of check_user_dot_ssh_dir() === #
|
|
|
|
readonly -f check_user_dot_ssh_dir
|
2001-01-19 05:37:32 +00:00
|
|
|
|
2008-07-14 02:12:52 +00:00
|
|
|
# ======================================================================
|
|
|
|
# Routine: fix_authorized_keys_perms
|
|
|
|
# Corrects the permissions of ~/.ssh/authorized_keys
|
|
|
|
# PREREQUISITE:
|
|
|
|
# pwdhome -- check_user_homedir()
|
|
|
|
# ======================================================================
|
|
|
|
fix_authorized_keys_perms() {
|
2014-11-05 00:01:31 +00:00
|
|
|
if [ -e "${pwdhome}/.ssh/authorized_keys" ]
|
2001-01-19 05:37:32 +00:00
|
|
|
then
|
2014-11-05 00:01:31 +00:00
|
|
|
setfacl -b "${pwdhome}/.ssh/authorized_keys" 2>/dev/null || echo -n
|
|
|
|
if ! chmod u-x,g-wx,o-wx "${pwdhome}/.ssh/authorized_keys"
|
2008-07-14 02:12:52 +00:00
|
|
|
then
|
|
|
|
csih_warning "Setting correct permissions to ${pwdhome}/.ssh/authorized_keys"
|
|
|
|
csih_warning "failed. Please care for the correct permissions. The minimum requirement"
|
|
|
|
csih_warning "is, the owner needs read permissions."
|
|
|
|
echo
|
|
|
|
fi
|
2001-01-19 05:37:32 +00:00
|
|
|
fi
|
2008-07-14 02:12:52 +00:00
|
|
|
} # === End of fix_authorized_keys_perms() === #
|
|
|
|
readonly -f fix_authorized_keys_perms
|
|
|
|
|
|
|
|
|
|
|
|
# ======================================================================
|
|
|
|
# Main Entry Point
|
|
|
|
# ======================================================================
|
2001-01-19 05:37:32 +00:00
|
|
|
|
2008-07-14 02:12:52 +00:00
|
|
|
# Check how the script has been started. If
|
|
|
|
# (1) it has been started by giving the full path and
|
|
|
|
# that path is /etc/postinstall, OR
|
|
|
|
# (2) Otherwise, if the environment variable
|
|
|
|
# SSH_USER_CONFIG_AUTO_ANSWER_NO is set
|
|
|
|
# then set auto_answer to "no". This allows automatic
|
|
|
|
# creation of the config files in /etc w/o overwriting
|
|
|
|
# them if they already exist. In both cases, color
|
|
|
|
# escape sequences are suppressed, so as to prevent
|
|
|
|
# cluttering setup's logfiles.
|
|
|
|
if [ "$PROGDIR" = "/etc/postinstall" ]
|
2003-11-13 00:28:49 +00:00
|
|
|
then
|
2008-07-14 02:12:52 +00:00
|
|
|
csih_auto_answer="no"
|
|
|
|
csih_disable_color
|
|
|
|
fi
|
|
|
|
if [ -n "${SSH_USER_CONFIG_AUTO_ANSWER_NO}" ]
|
|
|
|
then
|
|
|
|
csih_auto_answer="no"
|
|
|
|
csih_disable_color
|
2003-11-13 00:28:49 +00:00
|
|
|
fi
|
|
|
|
|
2008-07-14 02:12:52 +00:00
|
|
|
# ======================================================================
|
|
|
|
# Parse options
|
|
|
|
# ======================================================================
|
2001-01-19 05:37:32 +00:00
|
|
|
while :
|
|
|
|
do
|
|
|
|
case $# in
|
|
|
|
0)
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
option=$1
|
|
|
|
shift
|
|
|
|
|
|
|
|
case "$option" in
|
|
|
|
-d | --debug )
|
|
|
|
set -x
|
2008-07-14 02:12:52 +00:00
|
|
|
csih_trace_on
|
2001-01-19 05:37:32 +00:00
|
|
|
;;
|
|
|
|
|
|
|
|
-y | --yes )
|
2008-07-14 02:12:52 +00:00
|
|
|
csih_auto_answer=yes
|
2001-01-19 05:37:32 +00:00
|
|
|
;;
|
|
|
|
|
|
|
|
-n | --no )
|
2008-07-14 02:12:52 +00:00
|
|
|
csih_auto_answer=no
|
2001-01-19 05:37:32 +00:00
|
|
|
;;
|
|
|
|
|
|
|
|
-p | --passphrase )
|
|
|
|
with_passphrase="yes"
|
|
|
|
passphrase=$1
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
|
|
|
|
*)
|
2008-07-14 02:12:52 +00:00
|
|
|
echo "usage: ${PROGNAME} [OPTION]..."
|
2001-01-19 05:37:32 +00:00
|
|
|
echo
|
|
|
|
echo "This script creates an OpenSSH user configuration."
|
|
|
|
echo
|
|
|
|
echo "Options:"
|
|
|
|
echo " --debug -d Enable shell's debug output."
|
|
|
|
echo " --yes -y Answer all questions with \"yes\" automatically."
|
|
|
|
echo " --no -n Answer all questions with \"no\" automatically."
|
|
|
|
echo " --passphrase -p word Use \"word\" as passphrase automatically."
|
|
|
|
echo
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2008-07-14 02:12:52 +00:00
|
|
|
# ======================================================================
|
|
|
|
# Action!
|
|
|
|
# ======================================================================
|
2001-01-19 05:37:32 +00:00
|
|
|
|
2008-07-14 02:12:52 +00:00
|
|
|
check_user_homedir
|
|
|
|
check_user_dot_ssh_dir
|
2011-02-06 02:31:23 +00:00
|
|
|
create_identity id_rsa rsa "SSH2 RSA"
|
|
|
|
create_identity id_dsa dsa "SSH2 DSA"
|
|
|
|
create_identity id_ecdsa ecdsa "SSH2 ECDSA"
|
|
|
|
create_identity identity rsa1 "(deprecated) SSH1 RSA"
|
2008-07-14 02:12:52 +00:00
|
|
|
fix_authorized_keys_perms
|
2001-01-19 05:37:32 +00:00
|
|
|
|
2008-07-14 02:12:52 +00:00
|
|
|
echo
|
|
|
|
csih_inform "Configuration finished. Have fun!"
|
2001-01-19 05:37:32 +00:00
|
|
|
|
2003-11-13 00:28:49 +00:00
|
|
|
|