Darren Tucker | 798ca84 | 2003-11-13 11:28:49 +1100 | [diff] [blame] | 1 | #!/bin/bash |
Kevin Steves | 9be6e26 | 2000-10-29 19:18:49 +0000 | [diff] [blame] | 2 | # |
Darren Tucker | 4d4fdc0 | 2009-07-07 21:19:11 +1000 | [diff] [blame] | 3 | # ssh-host-config, Copyright 2000-2009 Red Hat Inc. |
Kevin Steves | 9be6e26 | 2000-10-29 19:18:49 +0000 | [diff] [blame] | 4 | # |
| 5 | # This file is part of the Cygwin port of OpenSSH. |
Darren Tucker | 8fdcba5 | 2009-07-12 21:58:42 +1000 | [diff] [blame] | 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. |
Kevin Steves | 9be6e26 | 2000-10-29 19:18:49 +0000 | [diff] [blame] | 18 | |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 19 | # ====================================================================== |
| 20 | # Initialization |
| 21 | # ====================================================================== |
| 22 | PROGNAME=$(basename $0) |
| 23 | _tdir=$(dirname $0) |
| 24 | PROGDIR=$(cd $_tdir && pwd) |
| 25 | |
| 26 | CSIH_SCRIPT=/usr/share/csih/cygwin-service-installation-helper.sh |
| 27 | |
Kevin Steves | 9be6e26 | 2000-10-29 19:18:49 +0000 | [diff] [blame] | 28 | # Subdirectory where the new package is being installed |
| 29 | PREFIX=/usr |
| 30 | |
| 31 | # Directory where the config files are stored |
| 32 | SYSCONFDIR=/etc |
Darren Tucker | 798ca84 | 2003-11-13 11:28:49 +1100 | [diff] [blame] | 33 | LOCALSTATEDIR=/var |
Kevin Steves | 9be6e26 | 2000-10-29 19:18:49 +0000 | [diff] [blame] | 34 | |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 35 | source ${CSIH_SCRIPT} |
Ben Lindstrom | b100ec9 | 2001-01-19 05:37:32 +0000 | [diff] [blame] | 36 | |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 37 | port_number=22 |
Ben Lindstrom | 6dbf300 | 2002-07-03 23:33:19 +0000 | [diff] [blame] | 38 | privsep_configured=no |
| 39 | privsep_used=yes |
Tim Rice | ca3692d | 2009-01-28 12:50:04 -0800 | [diff] [blame] | 40 | cygwin_value="" |
Darren Tucker | 4d4fdc0 | 2009-07-07 21:19:11 +1000 | [diff] [blame] | 41 | user_account= |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 42 | password_value= |
Darren Tucker | 4d4fdc0 | 2009-07-07 21:19:11 +1000 | [diff] [blame] | 43 | opt_force=no |
Ben Lindstrom | 6dbf300 | 2002-07-03 23:33:19 +0000 | [diff] [blame] | 44 | |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 45 | # ====================================================================== |
| 46 | # Routine: create_host_keys |
| 47 | # ====================================================================== |
| 48 | create_host_keys() { |
| 49 | if [ ! -f "${SYSCONFDIR}/ssh_host_key" ] |
Ben Lindstrom | b100ec9 | 2001-01-19 05:37:32 +0000 | [diff] [blame] | 50 | then |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 51 | csih_inform "Generating ${SYSCONFDIR}/ssh_host_key" |
| 52 | ssh-keygen -t rsa1 -f ${SYSCONFDIR}/ssh_host_key -N '' > /dev/null |
Ben Lindstrom | b100ec9 | 2001-01-19 05:37:32 +0000 | [diff] [blame] | 53 | fi |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 54 | |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 55 | if [ ! -f "${SYSCONFDIR}/ssh_host_rsa_key" ] |
Kevin Steves | 9be6e26 | 2000-10-29 19:18:49 +0000 | [diff] [blame] | 56 | then |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 57 | csih_inform "Generating ${SYSCONFDIR}/ssh_host_rsa_key" |
| 58 | ssh-keygen -t rsa -f ${SYSCONFDIR}/ssh_host_rsa_key -N '' > /dev/null |
| 59 | fi |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 60 | |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 61 | if [ ! -f "${SYSCONFDIR}/ssh_host_dsa_key" ] |
| 62 | then |
| 63 | csih_inform "Generating ${SYSCONFDIR}/ssh_host_dsa_key" |
| 64 | ssh-keygen -t dsa -f ${SYSCONFDIR}/ssh_host_dsa_key -N '' > /dev/null |
| 65 | fi |
Darren Tucker | ea676a6 | 2011-02-06 13:31:23 +1100 | [diff] [blame] | 66 | |
| 67 | if [ ! -f "${SYSCONFDIR}/ssh_host_ecdsa_key" ] |
| 68 | then |
| 69 | csih_inform "Generating ${SYSCONFDIR}/ssh_host_ecdsa_key" |
| 70 | ssh-keygen -t ecdsa -f ${SYSCONFDIR}/ssh_host_ecdsa_key -N '' > /dev/null |
| 71 | fi |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 72 | } # --- End of create_host_keys --- # |
| 73 | |
| 74 | # ====================================================================== |
| 75 | # Routine: update_services_file |
| 76 | # ====================================================================== |
| 77 | update_services_file() { |
| 78 | local _my_etcdir="/ssh-host-config.$$" |
| 79 | local _win_etcdir |
| 80 | local _services |
| 81 | local _spaces |
| 82 | local _serv_tmp |
| 83 | local _wservices |
| 84 | |
| 85 | if csih_is_nt |
| 86 | then |
| 87 | _win_etcdir="${SYSTEMROOT}\\system32\\drivers\\etc" |
| 88 | _services="${_my_etcdir}/services" |
| 89 | # On NT, 27 spaces, no space after the hash |
| 90 | _spaces=" #" |
Kevin Steves | 9be6e26 | 2000-10-29 19:18:49 +0000 | [diff] [blame] | 91 | else |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 92 | _win_etcdir="${WINDIR}" |
| 93 | _services="${_my_etcdir}/SERVICES" |
| 94 | # On 9x, 18 spaces (95 is very touchy), a space after the hash |
| 95 | _spaces=" # " |
Kevin Steves | 9be6e26 | 2000-10-29 19:18:49 +0000 | [diff] [blame] | 96 | fi |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 97 | _serv_tmp="${_my_etcdir}/srv.out.$$" |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 98 | |
Darren Tucker | 62131dc | 2010-03-24 13:03:32 +1100 | [diff] [blame] | 99 | mount -o text,posix=0,noacl -f "${_win_etcdir}" "${_my_etcdir}" |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 100 | |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 101 | # Depends on the above mount |
| 102 | _wservices=`cygpath -w "${_services}"` |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 103 | |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 104 | # Remove sshd 22/port from services |
| 105 | if [ `grep -q 'sshd[ \t][ \t]*22' "${_services}"; echo $?` -eq 0 ] |
| 106 | then |
| 107 | grep -v 'sshd[ \t][ \t]*22' "${_services}" > "${_serv_tmp}" |
| 108 | if [ -f "${_serv_tmp}" ] |
| 109 | then |
| 110 | if mv "${_serv_tmp}" "${_services}" |
| 111 | then |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 112 | csih_inform "Removing sshd from ${_wservices}" |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 113 | else |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 114 | csih_warning "Removing sshd from ${_wservices} failed!" |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 115 | fi |
| 116 | rm -f "${_serv_tmp}" |
| 117 | else |
| 118 | csih_warning "Removing sshd from ${_wservices} failed!" |
| 119 | fi |
| 120 | fi |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 121 | |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 122 | # Add ssh 22/tcp and ssh 22/udp to services |
| 123 | if [ `grep -q 'ssh[ \t][ \t]*22' "${_services}"; echo $?` -ne 0 ] |
| 124 | then |
| 125 | if awk '{ if ( $2 ~ /^23\/tcp/ ) print "ssh 22/tcp'"${_spaces}"'SSH Remote Login Protocol\nssh 22/udp'"${_spaces}"'SSH Remote Login Protocol"; print $0; }' < "${_services}" > "${_serv_tmp}" |
| 126 | then |
| 127 | if mv "${_serv_tmp}" "${_services}" |
| 128 | then |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 129 | csih_inform "Added ssh to ${_wservices}" |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 130 | else |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 131 | csih_warning "Adding ssh to ${_wservices} failed!" |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 132 | fi |
| 133 | rm -f "${_serv_tmp}" |
| 134 | else |
| 135 | csih_warning "Adding ssh to ${_wservices} failed!" |
| 136 | fi |
| 137 | fi |
| 138 | umount "${_my_etcdir}" |
| 139 | } # --- End of update_services_file --- # |
Kevin Steves | 9be6e26 | 2000-10-29 19:18:49 +0000 | [diff] [blame] | 140 | |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 141 | # ====================================================================== |
| 142 | # Routine: sshd_privsep |
| 143 | # MODIFIES: privsep_configured privsep_used |
| 144 | # ====================================================================== |
| 145 | sshd_privsep() { |
| 146 | local sshdconfig_tmp |
Ben Lindstrom | b100ec9 | 2001-01-19 05:37:32 +0000 | [diff] [blame] | 147 | |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 148 | if [ "${privsep_configured}" != "yes" ] |
| 149 | then |
| 150 | if csih_is_nt |
| 151 | then |
| 152 | csih_inform "Privilege separation is set to yes by default since OpenSSH 3.3." |
| 153 | csih_inform "However, this requires a non-privileged account called 'sshd'." |
| 154 | csih_inform "For more info on privilege separation read /usr/share/doc/openssh/README.privsep." |
| 155 | if csih_request "Should privilege separation be used?" |
| 156 | then |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 157 | privsep_used=yes |
| 158 | if ! csih_create_unprivileged_user sshd |
| 159 | then |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 160 | csih_warning "Couldn't create user 'sshd'!" |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 161 | csih_warning "Privilege separation set to 'no' again!" |
| 162 | csih_warning "Check your ${SYSCONFDIR}/sshd_config file!" |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 163 | privsep_used=no |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 164 | fi |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 165 | else |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 166 | privsep_used=no |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 167 | fi |
| 168 | else |
| 169 | # On 9x don't use privilege separation. Since security isn't |
| 170 | # available it just adds useless additional processes. |
| 171 | privsep_used=no |
| 172 | fi |
| 173 | fi |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 174 | |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 175 | # Create default sshd_config from skeleton files in /etc/defaults/etc or |
| 176 | # modify to add the missing privsep configuration option |
| 177 | if cmp "${SYSCONFDIR}/sshd_config" "${SYSCONFDIR}/defaults/${SYSCONFDIR}/sshd_config" >/dev/null 2>&1 |
| 178 | then |
| 179 | csih_inform "Updating ${SYSCONFDIR}/sshd_config file" |
| 180 | sshdconfig_tmp=${SYSCONFDIR}/sshd_config.$$ |
| 181 | sed -e "s/^#UsePrivilegeSeparation yes/UsePrivilegeSeparation ${privsep_used}/ |
| 182 | s/^#Port 22/Port ${port_number}/ |
| 183 | s/^#StrictModes yes/StrictModes no/" \ |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 184 | < ${SYSCONFDIR}/sshd_config \ |
| 185 | > "${sshdconfig_tmp}" |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 186 | mv "${sshdconfig_tmp}" ${SYSCONFDIR}/sshd_config |
| 187 | elif [ "${privsep_configured}" != "yes" ] |
| 188 | then |
| 189 | echo >> ${SYSCONFDIR}/sshd_config |
| 190 | echo "UsePrivilegeSeparation ${privsep_used}" >> ${SYSCONFDIR}/sshd_config |
| 191 | fi |
| 192 | } # --- End of sshd_privsep --- # |
| 193 | |
| 194 | # ====================================================================== |
| 195 | # Routine: update_inetd_conf |
| 196 | # ====================================================================== |
| 197 | update_inetd_conf() { |
| 198 | local _inetcnf="${SYSCONFDIR}/inetd.conf" |
| 199 | local _inetcnf_tmp="${SYSCONFDIR}/inetd.conf.$$" |
| 200 | local _inetcnf_dir="${SYSCONFDIR}/inetd.d" |
| 201 | local _sshd_inetd_conf="${_inetcnf_dir}/sshd-inetd" |
| 202 | local _sshd_inetd_conf_tmp="${_inetcnf_dir}/sshd-inetd.$$" |
| 203 | local _with_comment=1 |
| 204 | |
| 205 | if [ -d "${_inetcnf_dir}" ] |
| 206 | then |
| 207 | # we have inetutils-1.5 inetd.d support |
| 208 | if [ -f "${_inetcnf}" ] |
| 209 | then |
| 210 | grep -q '^[ \t]*ssh' "${_inetcnf}" && _with_comment=0 |
| 211 | |
| 212 | # check for sshd OR ssh in top-level inetd.conf file, and remove |
| 213 | # will be replaced by a file in inetd.d/ |
| 214 | if [ `grep -q '^[# \t]*ssh' "${_inetcnf}"; echo $?` -eq 0 ] |
| 215 | then |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 216 | grep -v '^[# \t]*ssh' "${_inetcnf}" >> "${_inetcnf_tmp}" |
| 217 | if [ -f "${_inetcnf_tmp}" ] |
| 218 | then |
| 219 | if mv "${_inetcnf_tmp}" "${_inetcnf}" |
| 220 | then |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 221 | csih_inform "Removed ssh[d] from ${_inetcnf}" |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 222 | else |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 223 | csih_warning "Removing ssh[d] from ${_inetcnf} failed!" |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 224 | fi |
| 225 | rm -f "${_inetcnf_tmp}" |
| 226 | else |
| 227 | csih_warning "Removing ssh[d] from ${_inetcnf} failed!" |
| 228 | fi |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 229 | fi |
| 230 | fi |
| 231 | |
| 232 | csih_install_config "${_sshd_inetd_conf}" "${SYSCONFDIR}/defaults" |
| 233 | if cmp "${SYSCONFDIR}/defaults${_sshd_inetd_conf}" "${_sshd_inetd_conf}" >/dev/null 2>&1 |
| 234 | then |
| 235 | if [ "${_with_comment}" -eq 0 ] |
| 236 | then |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 237 | sed -e 's/@COMMENT@[ \t]*//' < "${_sshd_inetd_conf}" > "${_sshd_inetd_conf_tmp}" |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 238 | else |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 239 | sed -e 's/@COMMENT@[ \t]*/# /' < "${_sshd_inetd_conf}" > "${_sshd_inetd_conf_tmp}" |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 240 | fi |
| 241 | mv "${_sshd_inetd_conf_tmp}" "${_sshd_inetd_conf}" |
| 242 | csih_inform "Updated ${_sshd_inetd_conf}" |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 243 | fi |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 244 | |
| 245 | elif [ -f "${_inetcnf}" ] |
| 246 | then |
| 247 | grep -q '^[ \t]*sshd' "${_inetcnf}" && _with_comment=0 |
| 248 | |
| 249 | # check for sshd in top-level inetd.conf file, and remove |
| 250 | # will be replaced by a file in inetd.d/ |
| 251 | if [ `grep -q '^[# \t]*sshd' "${_inetcnf}"; echo $?` -eq 0 ] |
| 252 | then |
| 253 | grep -v '^[# \t]*sshd' "${_inetcnf}" >> "${_inetcnf_tmp}" |
| 254 | if [ -f "${_inetcnf_tmp}" ] |
| 255 | then |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 256 | if mv "${_inetcnf_tmp}" "${_inetcnf}" |
| 257 | then |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 258 | csih_inform "Removed sshd from ${_inetcnf}" |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 259 | else |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 260 | csih_warning "Removing sshd from ${_inetcnf} failed!" |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 261 | fi |
| 262 | rm -f "${_inetcnf_tmp}" |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 263 | else |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 264 | csih_warning "Removing sshd from ${_inetcnf} failed!" |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 265 | fi |
| 266 | fi |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 267 | |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 268 | # Add ssh line to inetd.conf |
| 269 | if [ `grep -q '^[# \t]*ssh' "${_inetcnf}"; echo $?` -ne 0 ] |
| 270 | then |
| 271 | if [ "${_with_comment}" -eq 0 ] |
| 272 | then |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 273 | echo 'ssh stream tcp nowait root /usr/sbin/sshd sshd -i' >> "${_inetcnf}" |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 274 | else |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 275 | echo '# ssh stream tcp nowait root /usr/sbin/sshd sshd -i' >> "${_inetcnf}" |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 276 | fi |
| 277 | csih_inform "Added ssh to ${_inetcnf}" |
| 278 | fi |
| 279 | fi |
| 280 | } # --- End of update_inetd_conf --- # |
| 281 | |
| 282 | # ====================================================================== |
| 283 | # Routine: install_service |
| 284 | # Install sshd as a service |
| 285 | # ====================================================================== |
| 286 | install_service() { |
| 287 | local run_service_as |
| 288 | local password |
| 289 | |
| 290 | if csih_is_nt |
| 291 | then |
| 292 | if ! cygrunsrv -Q sshd >/dev/null 2>&1 |
| 293 | then |
| 294 | echo |
| 295 | echo |
| 296 | csih_warning "The following functions require administrator privileges!" |
| 297 | echo |
| 298 | echo -e "${_csih_QUERY_STR} Do you want to install sshd as a service?" |
| 299 | if csih_request "(Say \"no\" if it is already installed as a service)" |
| 300 | then |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 301 | csih_get_cygenv "${cygwin_value}" |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 302 | |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 303 | if ( csih_is_nt2003 || [ "$csih_FORCE_PRIVILEGED_USER" = "yes" ] ) |
| 304 | then |
| 305 | csih_inform "On Windows Server 2003, Windows Vista, and above, the" |
| 306 | csih_inform "SYSTEM account cannot setuid to other users -- a capability" |
| 307 | csih_inform "sshd requires. You need to have or to create a privileged" |
| 308 | csih_inform "account. This script will help you do so." |
| 309 | echo |
Darren Tucker | 4d4fdc0 | 2009-07-07 21:19:11 +1000 | [diff] [blame] | 310 | |
| 311 | [ "${opt_force}" = "yes" ] && opt_f=-f |
| 312 | [ -n "${user_account}" ] && opt_u="-u ""${user_account}""" |
| 313 | csih_select_privileged_username ${opt_f} ${opt_u} sshd |
| 314 | |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 315 | if ! csih_create_privileged_user "${password_value}" |
| 316 | then |
| 317 | csih_error_recoverable "There was a serious problem creating a privileged user." |
| 318 | csih_request "Do you want to proceed anyway?" || exit 1 |
| 319 | fi |
| 320 | fi |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 321 | |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 322 | # never returns empty if NT or above |
| 323 | run_service_as=$(csih_service_should_run_as) |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 324 | |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 325 | if [ "${run_service_as}" = "${csih_PRIVILEGED_USERNAME}" ] |
| 326 | then |
| 327 | password="${csih_PRIVILEGED_PASSWORD}" |
| 328 | if [ -z "${password}" ] |
| 329 | then |
| 330 | csih_get_value "Please enter the password for user '${run_service_as}':" "-s" |
| 331 | password="${csih_value}" |
| 332 | fi |
| 333 | fi |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 334 | |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 335 | # at this point, we either have $run_service_as = "system" and $password is empty, |
| 336 | # or $run_service_as is some privileged user and (hopefully) $password contains |
| 337 | # the correct password. So, from here out, we use '-z "${password}"' to discriminate |
| 338 | # the two cases. |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 339 | |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 340 | csih_check_user "${run_service_as}" |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 341 | |
Tim Rice | 6a32534 | 2009-01-29 12:30:01 -0800 | [diff] [blame] | 342 | if [ -n "${csih_cygenv}" ] |
| 343 | then |
Darren Tucker | 4d4fdc0 | 2009-07-07 21:19:11 +1000 | [diff] [blame] | 344 | cygwin_env=( -e "CYGWIN=${csih_cygenv}" ) |
Tim Rice | 6a32534 | 2009-01-29 12:30:01 -0800 | [diff] [blame] | 345 | fi |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 346 | if [ -z "${password}" ] |
| 347 | then |
Darren Tucker | 4d4fdc0 | 2009-07-07 21:19:11 +1000 | [diff] [blame] | 348 | if cygrunsrv -I sshd -d "CYGWIN sshd" -p /usr/sbin/sshd \ |
| 349 | -a "-D" -y tcpip "${cygwin_env[@]}" |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 350 | then |
| 351 | echo |
| 352 | csih_inform "The sshd service has been installed under the LocalSystem" |
| 353 | csih_inform "account (also known as SYSTEM). To start the service now, call" |
| 354 | csih_inform "\`net start sshd' or \`cygrunsrv -S sshd'. Otherwise, it" |
| 355 | csih_inform "will start automatically after the next reboot." |
| 356 | fi |
| 357 | else |
Darren Tucker | 4d4fdc0 | 2009-07-07 21:19:11 +1000 | [diff] [blame] | 358 | if cygrunsrv -I sshd -d "CYGWIN sshd" -p /usr/sbin/sshd \ |
| 359 | -a "-D" -y tcpip "${cygwin_env[@]}" \ |
Tim Rice | 6a32534 | 2009-01-29 12:30:01 -0800 | [diff] [blame] | 360 | -u "${run_service_as}" -w "${password}" |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 361 | then |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 362 | echo |
| 363 | csih_inform "The sshd service has been installed under the '${run_service_as}'" |
| 364 | csih_inform "account. To start the service now, call \`net start sshd' or" |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 365 | csih_inform "\`cygrunsrv -S sshd'. Otherwise, it will start automatically" |
| 366 | csih_inform "after the next reboot." |
| 367 | fi |
| 368 | fi |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 369 | |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 370 | # now, if successfully installed, set ownership of the affected files |
| 371 | if cygrunsrv -Q sshd >/dev/null 2>&1 |
| 372 | then |
| 373 | chown "${run_service_as}" ${SYSCONFDIR}/ssh* |
| 374 | chown "${run_service_as}".544 ${LOCALSTATEDIR}/empty |
| 375 | chown "${run_service_as}".544 ${LOCALSTATEDIR}/log/lastlog |
| 376 | if [ -f ${LOCALSTATEDIR}/log/sshd.log ] |
| 377 | then |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 378 | chown "${run_service_as}".544 ${LOCALSTATEDIR}/log/sshd.log |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 379 | fi |
| 380 | else |
| 381 | csih_warning "Something went wrong installing the sshd service." |
| 382 | fi |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 383 | fi # user allowed us to install as service |
| 384 | fi # service not yet installed |
| 385 | fi # csih_is_nt |
| 386 | } # --- End of install_service --- # |
| 387 | |
| 388 | # ====================================================================== |
| 389 | # Main Entry Point |
| 390 | # ====================================================================== |
| 391 | |
| 392 | # Check how the script has been started. If |
| 393 | # (1) it has been started by giving the full path and |
| 394 | # that path is /etc/postinstall, OR |
| 395 | # (2) Otherwise, if the environment variable |
| 396 | # SSH_HOST_CONFIG_AUTO_ANSWER_NO is set |
| 397 | # then set auto_answer to "no". This allows automatic |
| 398 | # creation of the config files in /etc w/o overwriting |
| 399 | # them if they already exist. In both cases, color |
| 400 | # escape sequences are suppressed, so as to prevent |
| 401 | # cluttering setup's logfiles. |
| 402 | if [ "$PROGDIR" = "/etc/postinstall" ] |
| 403 | then |
| 404 | csih_auto_answer="no" |
| 405 | csih_disable_color |
Darren Tucker | 4d4fdc0 | 2009-07-07 21:19:11 +1000 | [diff] [blame] | 406 | opt_force=yes |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 407 | fi |
| 408 | if [ -n "${SSH_HOST_CONFIG_AUTO_ANSWER_NO}" ] |
| 409 | then |
| 410 | csih_auto_answer="no" |
| 411 | csih_disable_color |
Darren Tucker | 4d4fdc0 | 2009-07-07 21:19:11 +1000 | [diff] [blame] | 412 | opt_force=yes |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 413 | fi |
| 414 | |
| 415 | # ====================================================================== |
| 416 | # Parse options |
| 417 | # ====================================================================== |
Ben Lindstrom | b100ec9 | 2001-01-19 05:37:32 +0000 | [diff] [blame] | 418 | while : |
| 419 | do |
| 420 | case $# in |
| 421 | 0) |
| 422 | break |
| 423 | ;; |
| 424 | esac |
| 425 | |
| 426 | option=$1 |
| 427 | shift |
| 428 | |
Darren Tucker | 798ca84 | 2003-11-13 11:28:49 +1100 | [diff] [blame] | 429 | case "${option}" in |
Ben Lindstrom | b100ec9 | 2001-01-19 05:37:32 +0000 | [diff] [blame] | 430 | -d | --debug ) |
| 431 | set -x |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 432 | csih_trace_on |
Ben Lindstrom | b100ec9 | 2001-01-19 05:37:32 +0000 | [diff] [blame] | 433 | ;; |
| 434 | |
| 435 | -y | --yes ) |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 436 | csih_auto_answer=yes |
Darren Tucker | 4d4fdc0 | 2009-07-07 21:19:11 +1000 | [diff] [blame] | 437 | opt_force=yes |
Ben Lindstrom | b100ec9 | 2001-01-19 05:37:32 +0000 | [diff] [blame] | 438 | ;; |
| 439 | |
| 440 | -n | --no ) |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 441 | csih_auto_answer=no |
Darren Tucker | 4d4fdc0 | 2009-07-07 21:19:11 +1000 | [diff] [blame] | 442 | opt_force=yes |
Ben Lindstrom | b100ec9 | 2001-01-19 05:37:32 +0000 | [diff] [blame] | 443 | ;; |
| 444 | |
Darren Tucker | 798ca84 | 2003-11-13 11:28:49 +1100 | [diff] [blame] | 445 | -c | --cygwin ) |
| 446 | cygwin_value="$1" |
| 447 | shift |
| 448 | ;; |
| 449 | |
Damien Miller | 8ac0a7e | 2001-03-07 21:38:19 +1100 | [diff] [blame] | 450 | -p | --port ) |
| 451 | port_number=$1 |
| 452 | shift |
| 453 | ;; |
| 454 | |
Darren Tucker | 4d4fdc0 | 2009-07-07 21:19:11 +1000 | [diff] [blame] | 455 | -u | --user ) |
| 456 | user_account="$1" |
| 457 | shift |
| 458 | ;; |
| 459 | |
Darren Tucker | 798ca84 | 2003-11-13 11:28:49 +1100 | [diff] [blame] | 460 | -w | --pwd ) |
| 461 | password_value="$1" |
| 462 | shift |
| 463 | ;; |
| 464 | |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 465 | --privileged ) |
| 466 | csih_FORCE_PRIVILEGED_USER=yes |
| 467 | ;; |
| 468 | |
Ben Lindstrom | b100ec9 | 2001-01-19 05:37:32 +0000 | [diff] [blame] | 469 | *) |
| 470 | echo "usage: ${progname} [OPTION]..." |
| 471 | echo |
| 472 | echo "This script creates an OpenSSH host configuration." |
| 473 | echo |
| 474 | echo "Options:" |
Darren Tucker | 798ca84 | 2003-11-13 11:28:49 +1100 | [diff] [blame] | 475 | echo " --debug -d Enable shell's debug output." |
| 476 | echo " --yes -y Answer all questions with \"yes\" automatically." |
| 477 | echo " --no -n Answer all questions with \"no\" automatically." |
| 478 | echo " --cygwin -c <options> Use \"options\" as value for CYGWIN environment var." |
| 479 | echo " --port -p <n> sshd listens on port n." |
Darren Tucker | 4d4fdc0 | 2009-07-07 21:19:11 +1000 | [diff] [blame] | 480 | echo " --user -u <account> privileged user for service." |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 481 | echo " --pwd -w <passwd> Use \"pwd\" as password for privileged user." |
| 482 | echo " --privileged On Windows NT/2k/XP, require privileged user" |
| 483 | echo " instead of LocalSystem for sshd service." |
Ben Lindstrom | b100ec9 | 2001-01-19 05:37:32 +0000 | [diff] [blame] | 484 | echo |
| 485 | exit 1 |
| 486 | ;; |
| 487 | |
| 488 | esac |
| 489 | done |
| 490 | |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 491 | # ====================================================================== |
| 492 | # Action! |
| 493 | # ====================================================================== |
Ben Lindstrom | 6dbf300 | 2002-07-03 23:33:19 +0000 | [diff] [blame] | 494 | |
Kevin Steves | 9be6e26 | 2000-10-29 19:18:49 +0000 | [diff] [blame] | 495 | # Check for running ssh/sshd processes first. Refuse to do anything while |
| 496 | # some ssh processes are still running |
Darren Tucker | 83795d6 | 2008-12-01 21:34:28 +1100 | [diff] [blame] | 497 | if ps -ef | grep -q '/sshd\?$' |
Kevin Steves | 9be6e26 | 2000-10-29 19:18:49 +0000 | [diff] [blame] | 498 | then |
| 499 | echo |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 500 | csih_error "There are still ssh processes running. Please shut them down first." |
Kevin Steves | 9be6e26 | 2000-10-29 19:18:49 +0000 | [diff] [blame] | 501 | fi |
| 502 | |
| 503 | # Check for ${SYSCONFDIR} directory |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 504 | csih_make_dir "${SYSCONFDIR}" "Cannot create global configuration files." |
| 505 | chmod 775 "${SYSCONFDIR}" |
| 506 | setfacl -m u:system:rwx "${SYSCONFDIR}" |
Kevin Steves | 9be6e26 | 2000-10-29 19:18:49 +0000 | [diff] [blame] | 507 | |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 508 | # Check for /var/log directory |
| 509 | csih_make_dir "${LOCALSTATEDIR}/log" "Cannot create log directory." |
| 510 | chmod 775 "${LOCALSTATEDIR}/log" |
| 511 | setfacl -m u:system:rwx "${LOCALSTATEDIR}/log" |
Kevin Steves | 9be6e26 | 2000-10-29 19:18:49 +0000 | [diff] [blame] | 512 | |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 513 | # Create /var/log/lastlog if not already exists |
Darren Tucker | 18614c2 | 2006-03-04 08:50:31 +1100 | [diff] [blame] | 514 | if [ -e ${LOCALSTATEDIR}/log/lastlog -a ! -f ${LOCALSTATEDIR}/log/lastlog ] |
| 515 | then |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 516 | echo |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 517 | csih_error_multi "${LOCALSTATEDIR}/log/lastlog exists, but is not a file." \ |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 518 | "Cannot create ssh host configuration." |
Darren Tucker | 18614c2 | 2006-03-04 08:50:31 +1100 | [diff] [blame] | 519 | fi |
| 520 | if [ ! -e ${LOCALSTATEDIR}/log/lastlog ] |
| 521 | then |
| 522 | cat /dev/null > ${LOCALSTATEDIR}/log/lastlog |
| 523 | chmod 644 ${LOCALSTATEDIR}/log/lastlog |
Ben Lindstrom | 6dbf300 | 2002-07-03 23:33:19 +0000 | [diff] [blame] | 524 | fi |
| 525 | |
| 526 | # Create /var/empty file used as chroot jail for privilege separation |
Darren Tucker | 4d4fdc0 | 2009-07-07 21:19:11 +1000 | [diff] [blame] | 527 | csih_make_dir "${LOCALSTATEDIR}/empty" "Cannot create ${LOCALSTATEDIR}/empty directory." |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 528 | chmod 755 "${LOCALSTATEDIR}/empty" |
| 529 | setfacl -m u:system:rwx "${LOCALSTATEDIR}/empty" |
| 530 | |
| 531 | # host keys |
| 532 | create_host_keys |
| 533 | |
| 534 | # use 'cmp' program to determine if a config file is identical |
| 535 | # to the default version of that config file |
| 536 | csih_check_program_or_error cmp diffutils |
| 537 | |
| 538 | |
| 539 | # handle ssh_config |
| 540 | csih_install_config "${SYSCONFDIR}/ssh_config" "${SYSCONFDIR}/defaults" |
| 541 | if cmp "${SYSCONFDIR}/ssh_config" "${SYSCONFDIR}/defaults/${SYSCONFDIR}/ssh_config" >/dev/null 2>&1 |
Ben Lindstrom | 6dbf300 | 2002-07-03 23:33:19 +0000 | [diff] [blame] | 542 | then |
Darren Tucker | 798ca84 | 2003-11-13 11:28:49 +1100 | [diff] [blame] | 543 | if [ "${port_number}" != "22" ] |
Damien Miller | 8ac0a7e | 2001-03-07 21:38:19 +1100 | [diff] [blame] | 544 | then |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 545 | csih_inform "Updating ${SYSCONFDIR}/ssh_config file with requested port" |
Damien Miller | 8ac0a7e | 2001-03-07 21:38:19 +1100 | [diff] [blame] | 546 | echo "Host localhost" >> ${SYSCONFDIR}/ssh_config |
Darren Tucker | 798ca84 | 2003-11-13 11:28:49 +1100 | [diff] [blame] | 547 | echo " Port ${port_number}" >> ${SYSCONFDIR}/ssh_config |
Damien Miller | 8ac0a7e | 2001-03-07 21:38:19 +1100 | [diff] [blame] | 548 | fi |
Kevin Steves | 9be6e26 | 2000-10-29 19:18:49 +0000 | [diff] [blame] | 549 | fi |
| 550 | |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 551 | # handle sshd_config (and privsep) |
| 552 | csih_install_config "${SYSCONFDIR}/sshd_config" "${SYSCONFDIR}/defaults" |
| 553 | if ! cmp "${SYSCONFDIR}/sshd_config" "${SYSCONFDIR}/defaults/${SYSCONFDIR}/sshd_config" >/dev/null 2>&1 |
Kevin Steves | 9be6e26 | 2000-10-29 19:18:49 +0000 | [diff] [blame] | 554 | then |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 555 | grep -q UsePrivilegeSeparation ${SYSCONFDIR}/sshd_config && privsep_configured=yes |
Kevin Steves | 9be6e26 | 2000-10-29 19:18:49 +0000 | [diff] [blame] | 556 | fi |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 557 | sshd_privsep |
Kevin Steves | 9be6e26 | 2000-10-29 19:18:49 +0000 | [diff] [blame] | 558 | |
Ben Lindstrom | 6dbf300 | 2002-07-03 23:33:19 +0000 | [diff] [blame] | 559 | |
Ben Lindstrom | 6dbf300 | 2002-07-03 23:33:19 +0000 | [diff] [blame] | 560 | |
Tim Rice | 0d8f2f3 | 2009-01-29 12:40:30 -0800 | [diff] [blame] | 561 | update_services_file |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 562 | update_inetd_conf |
| 563 | install_service |
Ben Lindstrom | a582029 | 2001-07-18 16:25:41 +0000 | [diff] [blame] | 564 | |
Kevin Steves | 9be6e26 | 2000-10-29 19:18:49 +0000 | [diff] [blame] | 565 | echo |
Damien Miller | 1fc231c | 2008-07-14 12:12:52 +1000 | [diff] [blame] | 566 | csih_inform "Host configuration finished. Have fun!" |
| 567 | |