📄 ssh-host-config
字号:
#!/bin/bash## ssh-host-config, Copyright 2000, 2001, 2002, 2003 Red Hat Inc.## This file is part of the Cygwin port of OpenSSH.# Subdirectory where the new package is being installedPREFIX=/usr# Directory where the config files are storedSYSCONFDIR=/etcLOCALSTATEDIR=/varprogname=$0auto_answer=""port_number=22privsep_configured=noprivsep_used=yessshd_in_passwd=nosshd_in_sam=norequest(){ if [ "${auto_answer}" = "yes" ] then echo "$1 (yes/no) yes" return 0 elif [ "${auto_answer}" = "no" ] then echo "$1 (yes/no) no" return 1 fi answer="" while [ "X${answer}" != "Xyes" -a "X${answer}" != "Xno" ] do echo -n "$1 (yes/no) " read -e answer done if [ "X${answer}" = "Xyes" ] then return 0 else return 1 fi}# Check optionswhile :do case $# in 0) break ;; esac option=$1 shift case "${option}" in -d | --debug ) set -x ;; -y | --yes ) auto_answer=yes ;; -n | --no ) auto_answer=no ;; -c | --cygwin ) cygwin_value="$1" shift ;; -p | --port ) port_number=$1 shift ;; -w | --pwd ) password_value="$1" shift ;; *) echo "usage: ${progname} [OPTION]..." echo echo "This script creates an OpenSSH host 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 " --cygwin -c <options> Use \"options\" as value for CYGWIN environment var." echo " --port -p <n> sshd listens on port n." echo " --pwd -w <passwd> Use \"pwd\" as password for user 'sshd_server'." echo exit 1 ;; esacdone# Check if running on NT_sys="`uname`"_nt=`expr "${_sys}" : "CYGWIN_NT"`# If running on NT, check if running under 2003 Server or laterif [ ${_nt} -gt 0 ]then _nt2003=`uname | awk -F- '{print ( $2 >= 5.2 ) ? 1 : 0;}'`fi# Check for running ssh/sshd processes first. Refuse to do anything while# some ssh processes are still runningif ps -ef | grep -v grep | grep -q sshthen echo echo "There are still ssh processes running. Please shut them down first." echo exit 1fi# Check for ${SYSCONFDIR} directoryif [ -e "${SYSCONFDIR}" -a ! -d "${SYSCONFDIR}" ]then echo echo "${SYSCONFDIR} is existant but not a directory." echo "Cannot create global configuration files." echo exit 1fi# Create it if necessaryif [ ! -e "${SYSCONFDIR}" ]then mkdir "${SYSCONFDIR}" if [ ! -e "${SYSCONFDIR}" ] then echo echo "Creating ${SYSCONFDIR} directory failed" echo exit 1 fifi# Create /var/log and /var/log/lastlog if not already existingif [ -e ${LOCALSTATEDIR}/log -a ! -d ${LOCALSTATEDIR}/log ]then echo echo "${LOCALSTATEDIR}/log is existant but not a directory." echo "Cannot create ssh host configuration." echo exit 1fiif [ ! -e ${LOCALSTATEDIR}/log ]then mkdir -p ${LOCALSTATEDIR}/logfiif [ -e ${LOCALSTATEDIR}/log/lastlog -a ! -f ${LOCALSTATEDIR}/log/lastlog ]then echo echo "${LOCALSTATEDIR}/log/lastlog exists, but is not a file." echo "Cannot create ssh host configuration." echo exit 1fiif [ ! -e ${LOCALSTATEDIR}/log/lastlog ]then cat /dev/null > ${LOCALSTATEDIR}/log/lastlog chmod 644 ${LOCALSTATEDIR}/log/lastlogfi# Create /var/empty file used as chroot jail for privilege separationif [ -f ${LOCALSTATEDIR}/empty ]then echo "Creating ${LOCALSTATEDIR}/empty failed!"else mkdir -p ${LOCALSTATEDIR}/empty if [ ${_nt} -gt 0 ] then chmod 755 ${LOCALSTATEDIR}/empty fifi# First generate host keys if not already existingif [ ! -f "${SYSCONFDIR}/ssh_host_key" ]then echo "Generating ${SYSCONFDIR}/ssh_host_key" ssh-keygen -t rsa1 -f ${SYSCONFDIR}/ssh_host_key -N '' > /dev/nullfiif [ ! -f "${SYSCONFDIR}/ssh_host_rsa_key" ]then echo "Generating ${SYSCONFDIR}/ssh_host_rsa_key" ssh-keygen -t rsa -f ${SYSCONFDIR}/ssh_host_rsa_key -N '' > /dev/nullfiif [ ! -f "${SYSCONFDIR}/ssh_host_dsa_key" ]then echo "Generating ${SYSCONFDIR}/ssh_host_dsa_key" ssh-keygen -t dsa -f ${SYSCONFDIR}/ssh_host_dsa_key -N '' > /dev/nullfi# Check if ssh_config exists. If yes, ask for overwritingif [ -f "${SYSCONFDIR}/ssh_config" ]then if request "Overwrite existing ${SYSCONFDIR}/ssh_config file?" then rm -f "${SYSCONFDIR}/ssh_config" if [ -f "${SYSCONFDIR}/ssh_config" ] then echo "Can't overwrite. ${SYSCONFDIR}/ssh_config is write protected." fi fifi# Create default ssh_config from skeleton file in /etc/defaults/etcif [ ! -f "${SYSCONFDIR}/ssh_config" ]then echo "Generating ${SYSCONFDIR}/ssh_config file" cp ${SYSCONFDIR}/defaults/etc/ssh_config ${SYSCONFDIR}/ssh_config if [ "${port_number}" != "22" ] then echo "Host localhost" >> ${SYSCONFDIR}/ssh_config echo " Port ${port_number}" >> ${SYSCONFDIR}/ssh_config fifi# Check if sshd_config exists. If yes, ask for overwritingif [ -f "${SYSCONFDIR}/sshd_config" ]then if request "Overwrite existing ${SYSCONFDIR}/sshd_config file?" then rm -f "${SYSCONFDIR}/sshd_config" if [ -f "${SYSCONFDIR}/sshd_config" ] then echo "Can't overwrite. ${SYSCONFDIR}/sshd_config is write protected." fi else grep -q UsePrivilegeSeparation ${SYSCONFDIR}/sshd_config && privsep_configured=yes fifi# Prior to creating or modifying sshd_config, care for privilege separationif [ "${privsep_configured}" != "yes" ]then if [ ${_nt} -gt 0 ] then echo "Privilege separation is set to yes by default since OpenSSH 3.3." echo "However, this requires a non-privileged account called 'sshd'." echo "For more info on privilege separation read /usr/share/doc/openssh/README.privsep." echo if request "Should privilege separation be used?" then privsep_used=yes grep -q '^sshd:' ${SYSCONFDIR}/passwd && sshd_in_passwd=yes net user sshd >/dev/null 2>&1 && sshd_in_sam=yes if [ "${sshd_in_passwd}" != "yes" ] then if [ "${sshd_in_sam}" != "yes" ] then echo "Warning: The following function requires administrator privileges!" if request "Should this script create a local user 'sshd' on this machine?" then dos_var_empty=`cygpath -w ${LOCALSTATEDIR}/empty` net user sshd /add /fullname:"sshd privsep" "/homedir:${dos_var_empty}" /active:no > /dev/null 2>&1 && sshd_in_sam=yes if [ "${sshd_in_sam}" != "yes" ] then echo "Warning: Creating the user 'sshd' failed!" fi fi fi if [ "${sshd_in_sam}" != "yes" ] then echo "Warning: Can't create user 'sshd' in ${SYSCONFDIR}/passwd!" echo " Privilege separation set to 'no' again!" echo " Check your ${SYSCONFDIR}/sshd_config file!" privsep_used=no else mkpasswd -l -u sshd | sed -e 's/bash$/false/' >> ${SYSCONFDIR}/passwd fi fi else privsep_used=no fi else # On 9x don't use privilege separation. Since security isn't # available it just adds useless additional processes. privsep_used=no fifi
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -