blob: bfeee7fca8b55fcde2b3be1875f72b59be13026a [file] [log] [blame]
Kevin Steves9be6e262000-10-29 19:18:49 +00001#!/bin/sh
2#
Ben Lindstromb100ec92001-01-19 05:37:32 +00003# ssh-host-config, Copyright 2000, Red Hat Inc.
Kevin Steves9be6e262000-10-29 19:18:49 +00004#
5# This file is part of the Cygwin port of OpenSSH.
6
Kevin Steves9be6e262000-10-29 19:18:49 +00007# Subdirectory where the new package is being installed
8PREFIX=/usr
9
10# Directory where the config files are stored
11SYSCONFDIR=/etc
12
13# Subdirectory where an old package might be installed
14OLDPREFIX=/usr/local
15OLDSYSCONFDIR=${OLDPREFIX}/etc
16
Ben Lindstromb100ec92001-01-19 05:37:32 +000017progname=$0
18auto_answer=""
Damien Miller8ac0a7e2001-03-07 21:38:19 +110019port_number=22
Ben Lindstromb100ec92001-01-19 05:37:32 +000020
Kevin Steves9be6e262000-10-29 19:18:49 +000021request()
22{
Ben Lindstromb100ec92001-01-19 05:37:32 +000023 if [ "${auto_answer}" = "yes" ]
24 then
25 return 0
26 elif [ "${auto_answer}" = "no" ]
27 then
28 return 1
29 fi
30
Kevin Steves9be6e262000-10-29 19:18:49 +000031 answer=""
32 while [ "X${answer}" != "Xyes" -a "X${answer}" != "Xno" ]
33 do
34 echo -n "$1 (yes/no) "
35 read answer
36 done
37 if [ "X${answer}" = "Xyes" ]
38 then
39 return 0
40 else
41 return 1
42 fi
43}
44
Ben Lindstromb100ec92001-01-19 05:37:32 +000045# Check options
46
47while :
48do
49 case $# in
50 0)
51 break
52 ;;
53 esac
54
55 option=$1
56 shift
57
58 case "$option" in
59 -d | --debug )
60 set -x
61 ;;
62
63 -y | --yes )
64 auto_answer=yes
65 ;;
66
67 -n | --no )
68 auto_answer=no
69 ;;
70
Damien Miller8ac0a7e2001-03-07 21:38:19 +110071 -p | --port )
72 port_number=$1
73 shift
74 ;;
75
Ben Lindstromb100ec92001-01-19 05:37:32 +000076 *)
77 echo "usage: ${progname} [OPTION]..."
78 echo
79 echo "This script creates an OpenSSH host configuration."
80 echo
81 echo "Options:"
82 echo " --debug -d Enable shell's debug output."
83 echo " --yes -y Answer all questions with \"yes\" automatically."
84 echo " --no -n Answer all questions with \"no\" automatically."
Damien Miller8ac0a7e2001-03-07 21:38:19 +110085 echo " --port -p <n> sshd listens on port n."
Ben Lindstromb100ec92001-01-19 05:37:32 +000086 echo
87 exit 1
88 ;;
89
90 esac
91done
92
Kevin Steves9be6e262000-10-29 19:18:49 +000093# Check for running ssh/sshd processes first. Refuse to do anything while
94# some ssh processes are still running
95
96if ps -ef | grep -v grep | grep -q ssh
97then
98 echo
99 echo "There are still ssh processes running. Please shut them down first."
100 echo
Damien Milleraba690c2001-11-12 10:36:21 +1100101 #exit 1
Kevin Steves9be6e262000-10-29 19:18:49 +0000102fi
103
104# Check for ${SYSCONFDIR} directory
105
106if [ -e "${SYSCONFDIR}" -a ! -d "${SYSCONFDIR}" ]
107then
108 echo
109 echo "${SYSCONFDIR} is existant but not a directory."
110 echo "Cannot create global configuration files."
111 echo
112 exit 1
113fi
114
115# Create it if necessary
116
117if [ ! -e "${SYSCONFDIR}" ]
118then
119 mkdir "${SYSCONFDIR}"
120 if [ ! -e "${SYSCONFDIR}" ]
121 then
122 echo
123 echo "Creating ${SYSCONFDIR} directory failed"
124 echo
125 exit 1
126 fi
127fi
128
129# Check for an old installation in ${OLDPREFIX} unless ${OLDPREFIX} isn't
130# the same as ${PREFIX}
131
Ben Lindstromb100ec92001-01-19 05:37:32 +0000132old_install=0
Kevin Steves9be6e262000-10-29 19:18:49 +0000133if [ "${OLDPREFIX}" != "${PREFIX}" ]
134then
135 if [ -f "${OLDPREFIX}/sbin/sshd" ]
136 then
137 echo
138 echo "You seem to have an older installation in ${OLDPREFIX}."
139 echo
140 # Check if old global configuration files exist
141 if [ -f "${OLDSYSCONFDIR}/ssh_host_key" ]
142 then
143 if request "Do you want to copy your config files to your new installation?"
144 then
145 cp -f ${OLDSYSCONFDIR}/ssh_host_key ${SYSCONFDIR}
146 cp -f ${OLDSYSCONFDIR}/ssh_host_key.pub ${SYSCONFDIR}
147 cp -f ${OLDSYSCONFDIR}/ssh_host_dsa_key ${SYSCONFDIR}
148 cp -f ${OLDSYSCONFDIR}/ssh_host_dsa_key.pub ${SYSCONFDIR}
149 cp -f ${OLDSYSCONFDIR}/ssh_config ${SYSCONFDIR}
150 cp -f ${OLDSYSCONFDIR}/sshd_config ${SYSCONFDIR}
151 fi
152 fi
153 if request "Do you want to erase your old installation?"
154 then
155 rm -f ${OLDPREFIX}/bin/ssh.exe
156 rm -f ${OLDPREFIX}/bin/ssh-config
157 rm -f ${OLDPREFIX}/bin/scp.exe
158 rm -f ${OLDPREFIX}/bin/ssh-add.exe
159 rm -f ${OLDPREFIX}/bin/ssh-agent.exe
160 rm -f ${OLDPREFIX}/bin/ssh-keygen.exe
161 rm -f ${OLDPREFIX}/bin/slogin
162 rm -f ${OLDSYSCONFDIR}/ssh_host_key
163 rm -f ${OLDSYSCONFDIR}/ssh_host_key.pub
164 rm -f ${OLDSYSCONFDIR}/ssh_host_dsa_key
165 rm -f ${OLDSYSCONFDIR}/ssh_host_dsa_key.pub
166 rm -f ${OLDSYSCONFDIR}/ssh_config
167 rm -f ${OLDSYSCONFDIR}/sshd_config
168 rm -f ${OLDPREFIX}/man/man1/ssh.1
169 rm -f ${OLDPREFIX}/man/man1/scp.1
170 rm -f ${OLDPREFIX}/man/man1/ssh-add.1
171 rm -f ${OLDPREFIX}/man/man1/ssh-agent.1
172 rm -f ${OLDPREFIX}/man/man1/ssh-keygen.1
173 rm -f ${OLDPREFIX}/man/man1/slogin.1
174 rm -f ${OLDPREFIX}/man/man8/sshd.8
175 rm -f ${OLDPREFIX}/sbin/sshd.exe
176 rm -f ${OLDPREFIX}/sbin/sftp-server.exe
177 fi
Ben Lindstromb100ec92001-01-19 05:37:32 +0000178 old_install=1
Kevin Steves9be6e262000-10-29 19:18:49 +0000179 fi
180fi
181
182# First generate host keys if not already existing
183
184if [ ! -f "${SYSCONFDIR}/ssh_host_key" ]
185then
186 echo "Generating ${SYSCONFDIR}/ssh_host_key"
Ben Lindstromb100ec92001-01-19 05:37:32 +0000187 ssh-keygen -t rsa1 -f ${SYSCONFDIR}/ssh_host_key -N '' > /dev/null
188fi
189
190if [ ! -f "${SYSCONFDIR}/ssh_host_rsa_key" ]
191then
192 echo "Generating ${SYSCONFDIR}/ssh_host_rsa_key"
193 ssh-keygen -t rsa -f ${SYSCONFDIR}/ssh_host_rsa_key -N '' > /dev/null
Kevin Steves9be6e262000-10-29 19:18:49 +0000194fi
195
196if [ ! -f "${SYSCONFDIR}/ssh_host_dsa_key" ]
197then
198 echo "Generating ${SYSCONFDIR}/ssh_host_dsa_key"
Ben Lindstromb100ec92001-01-19 05:37:32 +0000199 ssh-keygen -t dsa -f ${SYSCONFDIR}/ssh_host_dsa_key -N '' > /dev/null
Kevin Steves9be6e262000-10-29 19:18:49 +0000200fi
201
202# Check if ssh_config exists. If yes, ask for overwriting
203
204if [ -f "${SYSCONFDIR}/ssh_config" ]
205then
206 if request "Overwrite existing ${SYSCONFDIR}/ssh_config file?"
207 then
208 rm -f "${SYSCONFDIR}/ssh_config"
209 if [ -f "${SYSCONFDIR}/ssh_config" ]
210 then
211 echo "Can't overwrite. ${SYSCONFDIR}/ssh_config is write protected."
212 fi
213 fi
214fi
215
216# Create default ssh_config from here script
217
218if [ ! -f "${SYSCONFDIR}/ssh_config" ]
219then
Ben Lindstromb100ec92001-01-19 05:37:32 +0000220 echo "Generating ${SYSCONFDIR}/ssh_config file"
Kevin Steves9be6e262000-10-29 19:18:49 +0000221 cat > ${SYSCONFDIR}/ssh_config << EOF
222# This is ssh client systemwide configuration file. This file provides
223# defaults for users, and the values can be changed in per-user configuration
224# files or on the command line.
225
226# Configuration data is parsed as follows:
227# 1. command line options
228# 2. user-specific file
229# 3. system-wide file
230# Any configuration value is only changed the first time it is set.
231# Thus, host-specific definitions should be at the beginning of the
232# configuration file, and defaults at the end.
233
234# Site-wide defaults for various options
235
236# Host *
Damien Milleraba690c2001-11-12 10:36:21 +1100237# ForwardAgent no
238# ForwardX11 no
239# RhostsAuthentication no
Kevin Steves9be6e262000-10-29 19:18:49 +0000240# RhostsRSAAuthentication yes
241# RSAAuthentication yes
242# PasswordAuthentication yes
243# FallBackToRsh no
244# UseRsh no
245# BatchMode no
246# CheckHostIP yes
Damien Milleraba690c2001-11-12 10:36:21 +1100247# StrictHostKeyChecking yes
248# IdentityFile ~/.ssh/identity
249# IdentityFile ~/.ssh/id_dsa
250# IdentityFile ~/.ssh/id_rsa
Kevin Steves9be6e262000-10-29 19:18:49 +0000251# Port 22
252# Protocol 2,1
Damien Milleraba690c2001-11-12 10:36:21 +1100253# Cipher blowfish
Kevin Steves9be6e262000-10-29 19:18:49 +0000254# EscapeChar ~
Kevin Steves9be6e262000-10-29 19:18:49 +0000255EOF
Damien Miller8ac0a7e2001-03-07 21:38:19 +1100256 if [ "$port_number" != "22" ]
257 then
258 echo "Host localhost" >> ${SYSCONFDIR}/ssh_config
259 echo " Port $port_number" >> ${SYSCONFDIR}/ssh_config
260 fi
Kevin Steves9be6e262000-10-29 19:18:49 +0000261fi
262
263# Check if sshd_config exists. If yes, ask for overwriting
264
265if [ -f "${SYSCONFDIR}/sshd_config" ]
266then
267 if request "Overwrite existing ${SYSCONFDIR}/sshd_config file?"
268 then
269 rm -f "${SYSCONFDIR}/sshd_config"
270 if [ -f "${SYSCONFDIR}/sshd_config" ]
271 then
272 echo "Can't overwrite. ${SYSCONFDIR}/sshd_config is write protected."
273 fi
274 fi
275fi
276
277# Create default sshd_config from here script
278
279if [ ! -f "${SYSCONFDIR}/sshd_config" ]
280then
Ben Lindstromb100ec92001-01-19 05:37:32 +0000281 echo "Generating ${SYSCONFDIR}/sshd_config file"
Kevin Steves9be6e262000-10-29 19:18:49 +0000282 cat > ${SYSCONFDIR}/sshd_config << EOF
Damien Milleraba690c2001-11-12 10:36:21 +1100283# This is the sshd server system-wide configuration file. See sshd(8)
284# for more information.
Kevin Steves9be6e262000-10-29 19:18:49 +0000285
Damien Miller8ac0a7e2001-03-07 21:38:19 +1100286Port $port_number
Damien Milleraba690c2001-11-12 10:36:21 +1100287#Protocol 2,1
288#ListenAddress 0.0.0.0
Kevin Steves9be6e262000-10-29 19:18:49 +0000289#ListenAddress ::
Damien Milleraba690c2001-11-12 10:36:21 +1100290
291# HostKey for protocol version 1
Ben Lindstromb100ec92001-01-19 05:37:32 +0000292HostKey /etc/ssh_host_key
Damien Milleraba690c2001-11-12 10:36:21 +1100293# HostKeys for protocol version 2
Ben Lindstromb100ec92001-01-19 05:37:32 +0000294HostKey /etc/ssh_host_rsa_key
295HostKey /etc/ssh_host_dsa_key
Damien Milleraba690c2001-11-12 10:36:21 +1100296
297# Lifetime and size of ephemeral version 1 server ke
Kevin Steves9be6e262000-10-29 19:18:49 +0000298KeyRegenerationInterval 3600
Damien Milleraba690c2001-11-12 10:36:21 +1100299ServerKeyBits 768
Kevin Steves9be6e262000-10-29 19:18:49 +0000300
301# Logging
302SyslogFacility AUTH
303LogLevel INFO
304#obsoletes QuietMode and FascistLogging
305
Damien Milleraba690c2001-11-12 10:36:21 +1100306# Authentication:
307
308LoginGraceTime 600
309PermitRootLogin yes
310# The following setting overrides permission checks on host key files
311# and directories. For security reasons set this to "yes" when running
312# NT/W2K, NTFS and CYGWIN=ntsec.
313StrictModes no
Kevin Steves9be6e262000-10-29 19:18:49 +0000314
Kevin Steves9be6e262000-10-29 19:18:49 +0000315RSAAuthentication yes
Damien Milleraba690c2001-11-12 10:36:21 +1100316PubkeyAuthentication yes
317#AuthorizedKeysFile %h/.ssh/authorized_keys
Kevin Steves9be6e262000-10-29 19:18:49 +0000318
Damien Milleraba690c2001-11-12 10:36:21 +1100319# rhosts authentication should not be used
320RhostsAuthentication no
321# Don't read ~/.rhosts and ~/.shosts files
322IgnoreRhosts yes
323# For this to work you will also need host keys in /etc/ssh_known_hosts
324RhostsRSAAuthentication no
325# similar for protocol version 2
326HostbasedAuthentication no
327# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
328#IgnoreUserKnownHosts yes
329
330# To disable tunneled clear text passwords, change to no here!
Ben Lindstroma5820292001-07-18 16:25:41 +0000331PasswordAuthentication yes
Kevin Steves9be6e262000-10-29 19:18:49 +0000332PermitEmptyPasswords no
333
Damien Milleraba690c2001-11-12 10:36:21 +1100334X11Forwarding no
335X11DisplayOffset 10
336PrintMotd yes
337#PrintLastLog no
338KeepAlive yes
339#UseLogin no
Kevin Steves9be6e262000-10-29 19:18:49 +0000340
Kevin Steves9be6e262000-10-29 19:18:49 +0000341#MaxStartups 10:30:60
Damien Milleraba690c2001-11-12 10:36:21 +1100342#Banner /etc/issue.net
343#ReverseMappingCheck yes
344
345Subsystem sftp /usr/sbin/sftp-server
Kevin Steves9be6e262000-10-29 19:18:49 +0000346EOF
347fi
348
Damien Miller8ac0a7e2001-03-07 21:38:19 +1100349# Care for services file
Ben Lindstromb100ec92001-01-19 05:37:32 +0000350_sys="`uname -a`"
351_nt=`expr "$_sys" : "CYGWIN_NT"`
352if [ $_nt -gt 0 ]
Kevin Steves9be6e262000-10-29 19:18:49 +0000353then
Ben Lindstromb100ec92001-01-19 05:37:32 +0000354 _wservices="${SYSTEMROOT}\\system32\\drivers\\etc\\services"
355 _wserv_tmp="${SYSTEMROOT}\\system32\\drivers\\etc\\srv.out.$$"
356else
357 _wservices="${WINDIR}\\SERVICES"
358 _wserv_tmp="${WINDIR}\\SERV.$$"
Kevin Steves9be6e262000-10-29 19:18:49 +0000359fi
Ben Lindstromb100ec92001-01-19 05:37:32 +0000360_services=`cygpath -u "${_wservices}"`
361_serv_tmp=`cygpath -u "${_wserv_tmp}"`
Kevin Steves9be6e262000-10-29 19:18:49 +0000362
Damien Miller8ac0a7e2001-03-07 21:38:19 +1100363mount -t -f "${_wservices}" "${_services}"
364mount -t -f "${_wserv_tmp}" "${_serv_tmp}"
Kevin Steves9be6e262000-10-29 19:18:49 +0000365
Damien Miller8ac0a7e2001-03-07 21:38:19 +1100366# Remove sshd 22/port from services
367if [ `grep -q 'sshd[ \t][ \t]*22' "${_services}"; echo $?` -eq 0 ]
Kevin Steves9be6e262000-10-29 19:18:49 +0000368then
Damien Miller8ac0a7e2001-03-07 21:38:19 +1100369 grep -v 'sshd[ \t][ \t]*22' "${_services}" > "${_serv_tmp}"
370 if [ -f "${_serv_tmp}" ]
371 then
372 if mv "${_serv_tmp}" "${_services}"
373 then
374 echo "Removing sshd from ${_services}"
375 else
376 echo "Removing sshd from ${_services} failed\!"
377 fi
378 rm -f "${_serv_tmp}"
379 else
380 echo "Removing sshd from ${_services} failed\!"
381 fi
382fi
383
384# Add ssh 22/tcp and ssh 22/udp to services
385if [ `grep -q 'ssh[ \t][ \t]*22' "${_services}"; echo $?` -ne 0 ]
386then
387 awk '{ if ( $2 ~ /^23\/tcp/ ) print "ssh 22/tcp #SSH Remote Login Protocol\nssh 22/udp #SSH Remote Login Protocol"; print $0; }' < "${_services}" > "${_serv_tmp}"
Ben Lindstromb100ec92001-01-19 05:37:32 +0000388 if [ -f "${_serv_tmp}" ]
Kevin Steves9be6e262000-10-29 19:18:49 +0000389 then
Ben Lindstromb100ec92001-01-19 05:37:32 +0000390 if mv "${_serv_tmp}" "${_services}"
391 then
Damien Miller8ac0a7e2001-03-07 21:38:19 +1100392 echo "Added ssh to ${_services}"
Ben Lindstromb100ec92001-01-19 05:37:32 +0000393 else
Damien Miller8ac0a7e2001-03-07 21:38:19 +1100394 echo "Adding ssh to ${_services} failed\!"
Ben Lindstromb100ec92001-01-19 05:37:32 +0000395 fi
396 rm -f "${_serv_tmp}"
397 else
Damien Miller8ac0a7e2001-03-07 21:38:19 +1100398 echo "Adding ssh to ${_services} failed\!"
Kevin Steves9be6e262000-10-29 19:18:49 +0000399 fi
400fi
401
Ben Lindstromb100ec92001-01-19 05:37:32 +0000402umount "${_services}"
403umount "${_serv_tmp}"
404
Damien Miller8ac0a7e2001-03-07 21:38:19 +1100405# Care for inetd.conf file
406_inetcnf="/etc/inetd.conf"
407_inetcnf_tmp="/etc/inetd.conf.$$"
408
409if [ -f "${_inetcnf}" ]
Kevin Steves9be6e262000-10-29 19:18:49 +0000410then
Damien Miller8ac0a7e2001-03-07 21:38:19 +1100411 # Check if ssh service is already in use as sshd
412 with_comment=1
413 grep -q '^[ \t]*sshd' "${_inetcnf}" && with_comment=0
414 # Remove sshd line from inetd.conf
415 if [ `grep -q '^[# \t]*sshd' "${_inetcnf}"; echo $?` -eq 0 ]
416 then
417 grep -v '^[# \t]*sshd' "${_inetcnf}" >> "${_inetcnf_tmp}"
418 if [ -f "${_inetcnf_tmp}" ]
419 then
420 if mv "${_inetcnf_tmp}" "${_inetcnf}"
421 then
422 echo "Removed sshd from ${_inetcnf}"
423 else
424 echo "Removing sshd from ${_inetcnf} failed\!"
425 fi
426 rm -f "${_inetcnf_tmp}"
427 else
428 echo "Removing sshd from ${_inetcnf} failed\!"
429 fi
430 fi
431
432 # Add ssh line to inetd.conf
433 if [ `grep -q '^[# \t]*ssh' "${_inetcnf}"; echo $?` -ne 0 ]
434 then
435 if [ "${with_comment}" -eq 0 ]
436 then
437 echo 'ssh stream tcp nowait root /usr/sbin/sshd -i' >> "${_inetcnf}"
438 else
439 echo '# ssh stream tcp nowait root /usr/sbin/sshd -i' >> "${_inetcnf}"
440 fi
441 echo "Added ssh to ${_inetcnf}"
442 fi
Kevin Steves9be6e262000-10-29 19:18:49 +0000443fi
444
Ben Lindstroma5820292001-07-18 16:25:41 +0000445# Create /var/log and /var/log/lastlog if not already existing
446
447if [ -f /var/log ]
448then
449 echo "Creating /var/log failed\!"
450else
451 if [ ! -d /var/log ]
452 then
453 mkdir /var/log
454 fi
455 if [ -d /var/log/lastlog ]
456 then
457 echo "Creating /var/log/lastlog failed\!"
458 elif [ ! -f /var/log/lastlog ]
459 then
460 cat /dev/null > /var/log/lastlog
461 fi
462fi
463
464# On NT ask if sshd should be installed as service
465if [ $_nt -gt 0 ]
466then
467 echo
468 echo "Do you want to install sshd as service?"
469 if request "(Say \"no\" if it's already installed as service)"
470 then
471 echo
472 echo "Which value should the environment variable CYGWIN have when"
473 echo "sshd starts? It's recommended to set at least \"ntsec\" to be"
474 echo "able to change user context without password."
475 echo -n "Default is \"binmode ntsec tty\". CYGWIN="
476 read _cygwin
477 [ -z "${_cygwin}" ] && _cygwin="binmode ntsec tty"
478 if cygrunsrv -I sshd -d "CYGWIN sshd" -p /usr/sbin/sshd -a -D -e "CYGWIN=${_cygwin}"
479 then
480 chown system /etc/ssh*
481 echo
482 echo "The service has been installed under LocalSystem account."
483 fi
484 fi
485fi
486
Ben Lindstromb100ec92001-01-19 05:37:32 +0000487if [ "${old_install}" = "1" ]
Kevin Steves9be6e262000-10-29 19:18:49 +0000488then
Ben Lindstromb100ec92001-01-19 05:37:32 +0000489 echo
490 echo "Note: If you have used sshd as service or from inetd, don't forget to"
491 echo " change the path to sshd.exe in the service entry or in inetd.conf."
Kevin Steves9be6e262000-10-29 19:18:49 +0000492fi
493
494echo
Ben Lindstromb100ec92001-01-19 05:37:32 +0000495echo "Host configuration finished. Have fun!"