blob: 57e728fbc6dd3e5551faed8f3f0e3fcf931477c6 [file] [log] [blame]
Darren Tucker798ca842003-11-13 11:28:49 +11001#!/bin/bash
Kevin Steves9be6e262000-10-29 19:18:49 +00002#
Darren Tucker798ca842003-11-13 11:28:49 +11003# ssh-host-config, Copyright 2000, 2001, 2002, 2003 Red Hat Inc.
Kevin Steves9be6e262000-10-29 19:18:49 +00004#
5# This file is part of the Cygwin port of OpenSSH.
6
Damien Miller1fc231c2008-07-14 12:12:52 +10007# ======================================================================
8# Initialization
9# ======================================================================
10PROGNAME=$(basename $0)
11_tdir=$(dirname $0)
12PROGDIR=$(cd $_tdir && pwd)
13
14CSIH_SCRIPT=/usr/share/csih/cygwin-service-installation-helper.sh
15
Kevin Steves9be6e262000-10-29 19:18:49 +000016# Subdirectory where the new package is being installed
17PREFIX=/usr
18
19# Directory where the config files are stored
20SYSCONFDIR=/etc
Darren Tucker798ca842003-11-13 11:28:49 +110021LOCALSTATEDIR=/var
Kevin Steves9be6e262000-10-29 19:18:49 +000022
Damien Miller1fc231c2008-07-14 12:12:52 +100023source ${CSIH_SCRIPT}
Ben Lindstromb100ec92001-01-19 05:37:32 +000024
Damien Miller1fc231c2008-07-14 12:12:52 +100025port_number=22
Ben Lindstrom6dbf3002002-07-03 23:33:19 +000026privsep_configured=no
27privsep_used=yes
Tim Riceca3692d2009-01-28 12:50:04 -080028cygwin_value=""
Damien Miller1fc231c2008-07-14 12:12:52 +100029password_value=
Ben Lindstrom6dbf3002002-07-03 23:33:19 +000030
Damien Miller1fc231c2008-07-14 12:12:52 +100031# ======================================================================
32# Routine: create_host_keys
33# ======================================================================
34create_host_keys() {
35 if [ ! -f "${SYSCONFDIR}/ssh_host_key" ]
Ben Lindstromb100ec92001-01-19 05:37:32 +000036 then
Damien Miller1fc231c2008-07-14 12:12:52 +100037 csih_inform "Generating ${SYSCONFDIR}/ssh_host_key"
38 ssh-keygen -t rsa1 -f ${SYSCONFDIR}/ssh_host_key -N '' > /dev/null
Ben Lindstromb100ec92001-01-19 05:37:32 +000039 fi
Tim Rice0d8f2f32009-01-29 12:40:30 -080040
Damien Miller1fc231c2008-07-14 12:12:52 +100041 if [ ! -f "${SYSCONFDIR}/ssh_host_rsa_key" ]
Kevin Steves9be6e262000-10-29 19:18:49 +000042 then
Damien Miller1fc231c2008-07-14 12:12:52 +100043 csih_inform "Generating ${SYSCONFDIR}/ssh_host_rsa_key"
44 ssh-keygen -t rsa -f ${SYSCONFDIR}/ssh_host_rsa_key -N '' > /dev/null
45 fi
Tim Rice0d8f2f32009-01-29 12:40:30 -080046
Damien Miller1fc231c2008-07-14 12:12:52 +100047 if [ ! -f "${SYSCONFDIR}/ssh_host_dsa_key" ]
48 then
49 csih_inform "Generating ${SYSCONFDIR}/ssh_host_dsa_key"
50 ssh-keygen -t dsa -f ${SYSCONFDIR}/ssh_host_dsa_key -N '' > /dev/null
51 fi
52} # --- End of create_host_keys --- #
53
54# ======================================================================
55# Routine: update_services_file
56# ======================================================================
57update_services_file() {
58 local _my_etcdir="/ssh-host-config.$$"
59 local _win_etcdir
60 local _services
61 local _spaces
62 local _serv_tmp
63 local _wservices
64
65 if csih_is_nt
66 then
67 _win_etcdir="${SYSTEMROOT}\\system32\\drivers\\etc"
68 _services="${_my_etcdir}/services"
69 # On NT, 27 spaces, no space after the hash
70 _spaces=" #"
Kevin Steves9be6e262000-10-29 19:18:49 +000071 else
Damien Miller1fc231c2008-07-14 12:12:52 +100072 _win_etcdir="${WINDIR}"
73 _services="${_my_etcdir}/SERVICES"
74 # On 9x, 18 spaces (95 is very touchy), a space after the hash
75 _spaces=" # "
Kevin Steves9be6e262000-10-29 19:18:49 +000076 fi
Damien Miller1fc231c2008-07-14 12:12:52 +100077 _serv_tmp="${_my_etcdir}/srv.out.$$"
Tim Rice0d8f2f32009-01-29 12:40:30 -080078
Tim Riceca3692d2009-01-28 12:50:04 -080079 mount -o text -f "${_win_etcdir}" "${_my_etcdir}"
Tim Rice0d8f2f32009-01-29 12:40:30 -080080
Damien Miller1fc231c2008-07-14 12:12:52 +100081 # Depends on the above mount
82 _wservices=`cygpath -w "${_services}"`
Tim Rice0d8f2f32009-01-29 12:40:30 -080083
Damien Miller1fc231c2008-07-14 12:12:52 +100084 # Remove sshd 22/port from services
85 if [ `grep -q 'sshd[ \t][ \t]*22' "${_services}"; echo $?` -eq 0 ]
86 then
87 grep -v 'sshd[ \t][ \t]*22' "${_services}" > "${_serv_tmp}"
88 if [ -f "${_serv_tmp}" ]
89 then
90 if mv "${_serv_tmp}" "${_services}"
91 then
Tim Rice0d8f2f32009-01-29 12:40:30 -080092 csih_inform "Removing sshd from ${_wservices}"
Damien Miller1fc231c2008-07-14 12:12:52 +100093 else
Tim Rice0d8f2f32009-01-29 12:40:30 -080094 csih_warning "Removing sshd from ${_wservices} failed!"
Damien Miller1fc231c2008-07-14 12:12:52 +100095 fi
96 rm -f "${_serv_tmp}"
97 else
98 csih_warning "Removing sshd from ${_wservices} failed!"
99 fi
100 fi
Tim Rice0d8f2f32009-01-29 12:40:30 -0800101
Damien Miller1fc231c2008-07-14 12:12:52 +1000102 # Add ssh 22/tcp and ssh 22/udp to services
103 if [ `grep -q 'ssh[ \t][ \t]*22' "${_services}"; echo $?` -ne 0 ]
104 then
105 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}"
106 then
107 if mv "${_serv_tmp}" "${_services}"
108 then
Tim Rice0d8f2f32009-01-29 12:40:30 -0800109 csih_inform "Added ssh to ${_wservices}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000110 else
Tim Rice0d8f2f32009-01-29 12:40:30 -0800111 csih_warning "Adding ssh to ${_wservices} failed!"
Damien Miller1fc231c2008-07-14 12:12:52 +1000112 fi
113 rm -f "${_serv_tmp}"
114 else
115 csih_warning "Adding ssh to ${_wservices} failed!"
116 fi
117 fi
118 umount "${_my_etcdir}"
119} # --- End of update_services_file --- #
Kevin Steves9be6e262000-10-29 19:18:49 +0000120
Damien Miller1fc231c2008-07-14 12:12:52 +1000121# ======================================================================
122# Routine: sshd_privsep
123# MODIFIES: privsep_configured privsep_used
124# ======================================================================
125sshd_privsep() {
126 local sshdconfig_tmp
Ben Lindstromb100ec92001-01-19 05:37:32 +0000127
Damien Miller1fc231c2008-07-14 12:12:52 +1000128 if [ "${privsep_configured}" != "yes" ]
129 then
130 if csih_is_nt
131 then
132 csih_inform "Privilege separation is set to yes by default since OpenSSH 3.3."
133 csih_inform "However, this requires a non-privileged account called 'sshd'."
134 csih_inform "For more info on privilege separation read /usr/share/doc/openssh/README.privsep."
135 if csih_request "Should privilege separation be used?"
136 then
Tim Rice0d8f2f32009-01-29 12:40:30 -0800137 privsep_used=yes
138 if ! csih_create_unprivileged_user sshd
139 then
Damien Miller1fc231c2008-07-14 12:12:52 +1000140 csih_warning "Couldn't create user 'sshd'!"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800141 csih_warning "Privilege separation set to 'no' again!"
142 csih_warning "Check your ${SYSCONFDIR}/sshd_config file!"
Damien Miller1fc231c2008-07-14 12:12:52 +1000143 privsep_used=no
Tim Rice0d8f2f32009-01-29 12:40:30 -0800144 fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000145 else
Tim Rice0d8f2f32009-01-29 12:40:30 -0800146 privsep_used=no
Damien Miller1fc231c2008-07-14 12:12:52 +1000147 fi
148 else
149 # On 9x don't use privilege separation. Since security isn't
150 # available it just adds useless additional processes.
151 privsep_used=no
152 fi
153 fi
Tim Rice0d8f2f32009-01-29 12:40:30 -0800154
Damien Miller1fc231c2008-07-14 12:12:52 +1000155 # Create default sshd_config from skeleton files in /etc/defaults/etc or
156 # modify to add the missing privsep configuration option
157 if cmp "${SYSCONFDIR}/sshd_config" "${SYSCONFDIR}/defaults/${SYSCONFDIR}/sshd_config" >/dev/null 2>&1
158 then
159 csih_inform "Updating ${SYSCONFDIR}/sshd_config file"
160 sshdconfig_tmp=${SYSCONFDIR}/sshd_config.$$
161 sed -e "s/^#UsePrivilegeSeparation yes/UsePrivilegeSeparation ${privsep_used}/
162 s/^#Port 22/Port ${port_number}/
163 s/^#StrictModes yes/StrictModes no/" \
Tim Rice0d8f2f32009-01-29 12:40:30 -0800164 < ${SYSCONFDIR}/sshd_config \
165 > "${sshdconfig_tmp}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000166 mv "${sshdconfig_tmp}" ${SYSCONFDIR}/sshd_config
167 elif [ "${privsep_configured}" != "yes" ]
168 then
169 echo >> ${SYSCONFDIR}/sshd_config
170 echo "UsePrivilegeSeparation ${privsep_used}" >> ${SYSCONFDIR}/sshd_config
171 fi
172} # --- End of sshd_privsep --- #
173
174# ======================================================================
175# Routine: update_inetd_conf
176# ======================================================================
177update_inetd_conf() {
178 local _inetcnf="${SYSCONFDIR}/inetd.conf"
179 local _inetcnf_tmp="${SYSCONFDIR}/inetd.conf.$$"
180 local _inetcnf_dir="${SYSCONFDIR}/inetd.d"
181 local _sshd_inetd_conf="${_inetcnf_dir}/sshd-inetd"
182 local _sshd_inetd_conf_tmp="${_inetcnf_dir}/sshd-inetd.$$"
183 local _with_comment=1
184
185 if [ -d "${_inetcnf_dir}" ]
186 then
187 # we have inetutils-1.5 inetd.d support
188 if [ -f "${_inetcnf}" ]
189 then
190 grep -q '^[ \t]*ssh' "${_inetcnf}" && _with_comment=0
191
192 # check for sshd OR ssh in top-level inetd.conf file, and remove
193 # will be replaced by a file in inetd.d/
194 if [ `grep -q '^[# \t]*ssh' "${_inetcnf}"; echo $?` -eq 0 ]
195 then
Tim Rice0d8f2f32009-01-29 12:40:30 -0800196 grep -v '^[# \t]*ssh' "${_inetcnf}" >> "${_inetcnf_tmp}"
197 if [ -f "${_inetcnf_tmp}" ]
198 then
199 if mv "${_inetcnf_tmp}" "${_inetcnf}"
200 then
Damien Miller1fc231c2008-07-14 12:12:52 +1000201 csih_inform "Removed ssh[d] from ${_inetcnf}"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800202 else
Damien Miller1fc231c2008-07-14 12:12:52 +1000203 csih_warning "Removing ssh[d] from ${_inetcnf} failed!"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800204 fi
205 rm -f "${_inetcnf_tmp}"
206 else
207 csih_warning "Removing ssh[d] from ${_inetcnf} failed!"
208 fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000209 fi
210 fi
211
212 csih_install_config "${_sshd_inetd_conf}" "${SYSCONFDIR}/defaults"
213 if cmp "${SYSCONFDIR}/defaults${_sshd_inetd_conf}" "${_sshd_inetd_conf}" >/dev/null 2>&1
214 then
215 if [ "${_with_comment}" -eq 0 ]
216 then
Tim Rice0d8f2f32009-01-29 12:40:30 -0800217 sed -e 's/@COMMENT@[ \t]*//' < "${_sshd_inetd_conf}" > "${_sshd_inetd_conf_tmp}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000218 else
Tim Rice0d8f2f32009-01-29 12:40:30 -0800219 sed -e 's/@COMMENT@[ \t]*/# /' < "${_sshd_inetd_conf}" > "${_sshd_inetd_conf_tmp}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000220 fi
221 mv "${_sshd_inetd_conf_tmp}" "${_sshd_inetd_conf}"
222 csih_inform "Updated ${_sshd_inetd_conf}"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800223 fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000224
225 elif [ -f "${_inetcnf}" ]
226 then
227 grep -q '^[ \t]*sshd' "${_inetcnf}" && _with_comment=0
228
229 # check for sshd in top-level inetd.conf file, and remove
230 # will be replaced by a file in inetd.d/
231 if [ `grep -q '^[# \t]*sshd' "${_inetcnf}"; echo $?` -eq 0 ]
232 then
233 grep -v '^[# \t]*sshd' "${_inetcnf}" >> "${_inetcnf_tmp}"
234 if [ -f "${_inetcnf_tmp}" ]
235 then
Tim Rice0d8f2f32009-01-29 12:40:30 -0800236 if mv "${_inetcnf_tmp}" "${_inetcnf}"
237 then
Damien Miller1fc231c2008-07-14 12:12:52 +1000238 csih_inform "Removed sshd from ${_inetcnf}"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800239 else
Damien Miller1fc231c2008-07-14 12:12:52 +1000240 csih_warning "Removing sshd from ${_inetcnf} failed!"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800241 fi
242 rm -f "${_inetcnf_tmp}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000243 else
Tim Rice0d8f2f32009-01-29 12:40:30 -0800244 csih_warning "Removing sshd from ${_inetcnf} failed!"
Damien Miller1fc231c2008-07-14 12:12:52 +1000245 fi
246 fi
Tim Rice0d8f2f32009-01-29 12:40:30 -0800247
Damien Miller1fc231c2008-07-14 12:12:52 +1000248 # Add ssh line to inetd.conf
249 if [ `grep -q '^[# \t]*ssh' "${_inetcnf}"; echo $?` -ne 0 ]
250 then
251 if [ "${_with_comment}" -eq 0 ]
252 then
Tim Rice0d8f2f32009-01-29 12:40:30 -0800253 echo 'ssh stream tcp nowait root /usr/sbin/sshd sshd -i' >> "${_inetcnf}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000254 else
Tim Rice0d8f2f32009-01-29 12:40:30 -0800255 echo '# ssh stream tcp nowait root /usr/sbin/sshd sshd -i' >> "${_inetcnf}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000256 fi
257 csih_inform "Added ssh to ${_inetcnf}"
258 fi
259 fi
260} # --- End of update_inetd_conf --- #
261
262# ======================================================================
263# Routine: install_service
264# Install sshd as a service
265# ======================================================================
266install_service() {
267 local run_service_as
268 local password
269
270 if csih_is_nt
271 then
272 if ! cygrunsrv -Q sshd >/dev/null 2>&1
273 then
274 echo
275 echo
276 csih_warning "The following functions require administrator privileges!"
277 echo
278 echo -e "${_csih_QUERY_STR} Do you want to install sshd as a service?"
279 if csih_request "(Say \"no\" if it is already installed as a service)"
280 then
Tim Rice0d8f2f32009-01-29 12:40:30 -0800281 csih_get_cygenv "${cygwin_value}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000282
Tim Rice0d8f2f32009-01-29 12:40:30 -0800283 if ( csih_is_nt2003 || [ "$csih_FORCE_PRIVILEGED_USER" = "yes" ] )
284 then
285 csih_inform "On Windows Server 2003, Windows Vista, and above, the"
286 csih_inform "SYSTEM account cannot setuid to other users -- a capability"
287 csih_inform "sshd requires. You need to have or to create a privileged"
288 csih_inform "account. This script will help you do so."
289 echo
290 if ! csih_create_privileged_user "${password_value}"
291 then
292 csih_error_recoverable "There was a serious problem creating a privileged user."
293 csih_request "Do you want to proceed anyway?" || exit 1
294 fi
295 fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000296
Tim Rice0d8f2f32009-01-29 12:40:30 -0800297 # never returns empty if NT or above
298 run_service_as=$(csih_service_should_run_as)
Damien Miller1fc231c2008-07-14 12:12:52 +1000299
Tim Rice0d8f2f32009-01-29 12:40:30 -0800300 if [ "${run_service_as}" = "${csih_PRIVILEGED_USERNAME}" ]
301 then
302 password="${csih_PRIVILEGED_PASSWORD}"
303 if [ -z "${password}" ]
304 then
305 csih_get_value "Please enter the password for user '${run_service_as}':" "-s"
306 password="${csih_value}"
307 fi
308 fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000309
Tim Rice0d8f2f32009-01-29 12:40:30 -0800310 # at this point, we either have $run_service_as = "system" and $password is empty,
311 # or $run_service_as is some privileged user and (hopefully) $password contains
312 # the correct password. So, from here out, we use '-z "${password}"' to discriminate
313 # the two cases.
Damien Miller1fc231c2008-07-14 12:12:52 +1000314
Tim Rice0d8f2f32009-01-29 12:40:30 -0800315 csih_check_user "${run_service_as}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000316
Tim Rice6a325342009-01-29 12:30:01 -0800317 if [ -n "${csih_cygenv}" ]
318 then
319 cygwin_env="-e CYGWIN=\"${csih_cygenv}\""
320 fi
Tim Rice0d8f2f32009-01-29 12:40:30 -0800321 if [ -z "${password}" ]
322 then
Tim Rice6a325342009-01-29 12:30:01 -0800323 if eval cygrunsrv -I sshd -d \"CYGWIN sshd\" -p /usr/sbin/sshd \
324 -a "-D" -y tcpip ${cygwin_env}
Tim Rice0d8f2f32009-01-29 12:40:30 -0800325 then
326 echo
327 csih_inform "The sshd service has been installed under the LocalSystem"
328 csih_inform "account (also known as SYSTEM). To start the service now, call"
329 csih_inform "\`net start sshd' or \`cygrunsrv -S sshd'. Otherwise, it"
330 csih_inform "will start automatically after the next reboot."
331 fi
332 else
Tim Rice6a325342009-01-29 12:30:01 -0800333 if eval cygrunsrv -I sshd -d \"CYGWIN sshd\" -p /usr/sbin/sshd \
334 -a "-D" -y tcpip ${cygwin_env} \
335 -u "${run_service_as}" -w "${password}"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800336 then
Damien Miller1fc231c2008-07-14 12:12:52 +1000337 echo
338 csih_inform "The sshd service has been installed under the '${run_service_as}'"
339 csih_inform "account. To start the service now, call \`net start sshd' or"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800340 csih_inform "\`cygrunsrv -S sshd'. Otherwise, it will start automatically"
341 csih_inform "after the next reboot."
342 fi
343 fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000344
Tim Rice0d8f2f32009-01-29 12:40:30 -0800345 # now, if successfully installed, set ownership of the affected files
346 if cygrunsrv -Q sshd >/dev/null 2>&1
347 then
348 chown "${run_service_as}" ${SYSCONFDIR}/ssh*
349 chown "${run_service_as}".544 ${LOCALSTATEDIR}/empty
350 chown "${run_service_as}".544 ${LOCALSTATEDIR}/log/lastlog
351 if [ -f ${LOCALSTATEDIR}/log/sshd.log ]
352 then
Damien Miller1fc231c2008-07-14 12:12:52 +1000353 chown "${run_service_as}".544 ${LOCALSTATEDIR}/log/sshd.log
Tim Rice0d8f2f32009-01-29 12:40:30 -0800354 fi
355 else
356 csih_warning "Something went wrong installing the sshd service."
357 fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000358 fi # user allowed us to install as service
359 fi # service not yet installed
360 fi # csih_is_nt
361} # --- End of install_service --- #
362
363# ======================================================================
364# Main Entry Point
365# ======================================================================
366
367# Check how the script has been started. If
368# (1) it has been started by giving the full path and
369# that path is /etc/postinstall, OR
370# (2) Otherwise, if the environment variable
371# SSH_HOST_CONFIG_AUTO_ANSWER_NO is set
372# then set auto_answer to "no". This allows automatic
373# creation of the config files in /etc w/o overwriting
374# them if they already exist. In both cases, color
375# escape sequences are suppressed, so as to prevent
376# cluttering setup's logfiles.
377if [ "$PROGDIR" = "/etc/postinstall" ]
378then
379 csih_auto_answer="no"
380 csih_disable_color
381fi
382if [ -n "${SSH_HOST_CONFIG_AUTO_ANSWER_NO}" ]
383then
384 csih_auto_answer="no"
385 csih_disable_color
386fi
387
388# ======================================================================
389# Parse options
390# ======================================================================
Ben Lindstromb100ec92001-01-19 05:37:32 +0000391while :
392do
393 case $# in
394 0)
395 break
396 ;;
397 esac
398
399 option=$1
400 shift
401
Darren Tucker798ca842003-11-13 11:28:49 +1100402 case "${option}" in
Ben Lindstromb100ec92001-01-19 05:37:32 +0000403 -d | --debug )
404 set -x
Damien Miller1fc231c2008-07-14 12:12:52 +1000405 csih_trace_on
Ben Lindstromb100ec92001-01-19 05:37:32 +0000406 ;;
407
408 -y | --yes )
Damien Miller1fc231c2008-07-14 12:12:52 +1000409 csih_auto_answer=yes
Ben Lindstromb100ec92001-01-19 05:37:32 +0000410 ;;
411
412 -n | --no )
Damien Miller1fc231c2008-07-14 12:12:52 +1000413 csih_auto_answer=no
Ben Lindstromb100ec92001-01-19 05:37:32 +0000414 ;;
415
Darren Tucker798ca842003-11-13 11:28:49 +1100416 -c | --cygwin )
417 cygwin_value="$1"
418 shift
419 ;;
420
Damien Miller8ac0a7e2001-03-07 21:38:19 +1100421 -p | --port )
422 port_number=$1
423 shift
424 ;;
425
Darren Tucker798ca842003-11-13 11:28:49 +1100426 -w | --pwd )
427 password_value="$1"
428 shift
429 ;;
430
Damien Miller1fc231c2008-07-14 12:12:52 +1000431 --privileged )
432 csih_FORCE_PRIVILEGED_USER=yes
433 ;;
434
Ben Lindstromb100ec92001-01-19 05:37:32 +0000435 *)
436 echo "usage: ${progname} [OPTION]..."
437 echo
438 echo "This script creates an OpenSSH host configuration."
439 echo
440 echo "Options:"
Darren Tucker798ca842003-11-13 11:28:49 +1100441 echo " --debug -d Enable shell's debug output."
442 echo " --yes -y Answer all questions with \"yes\" automatically."
443 echo " --no -n Answer all questions with \"no\" automatically."
444 echo " --cygwin -c <options> Use \"options\" as value for CYGWIN environment var."
445 echo " --port -p <n> sshd listens on port n."
Damien Miller1fc231c2008-07-14 12:12:52 +1000446 echo " --pwd -w <passwd> Use \"pwd\" as password for privileged user."
447 echo " --privileged On Windows NT/2k/XP, require privileged user"
448 echo " instead of LocalSystem for sshd service."
Ben Lindstromb100ec92001-01-19 05:37:32 +0000449 echo
450 exit 1
451 ;;
452
453 esac
454done
455
Damien Miller1fc231c2008-07-14 12:12:52 +1000456# ======================================================================
457# Action!
458# ======================================================================
Ben Lindstrom6dbf3002002-07-03 23:33:19 +0000459
Kevin Steves9be6e262000-10-29 19:18:49 +0000460# Check for running ssh/sshd processes first. Refuse to do anything while
461# some ssh processes are still running
Darren Tucker83795d62008-12-01 21:34:28 +1100462if ps -ef | grep -q '/sshd\?$'
Kevin Steves9be6e262000-10-29 19:18:49 +0000463then
464 echo
Damien Miller1fc231c2008-07-14 12:12:52 +1000465 csih_error "There are still ssh processes running. Please shut them down first."
Kevin Steves9be6e262000-10-29 19:18:49 +0000466fi
467
468# Check for ${SYSCONFDIR} directory
Damien Miller1fc231c2008-07-14 12:12:52 +1000469csih_make_dir "${SYSCONFDIR}" "Cannot create global configuration files."
470chmod 775 "${SYSCONFDIR}"
471setfacl -m u:system:rwx "${SYSCONFDIR}"
Kevin Steves9be6e262000-10-29 19:18:49 +0000472
Damien Miller1fc231c2008-07-14 12:12:52 +1000473# Check for /var/log directory
474csih_make_dir "${LOCALSTATEDIR}/log" "Cannot create log directory."
475chmod 775 "${LOCALSTATEDIR}/log"
476setfacl -m u:system:rwx "${LOCALSTATEDIR}/log"
Kevin Steves9be6e262000-10-29 19:18:49 +0000477
Damien Miller1fc231c2008-07-14 12:12:52 +1000478# Create /var/log/lastlog if not already exists
Darren Tucker18614c22006-03-04 08:50:31 +1100479if [ -e ${LOCALSTATEDIR}/log/lastlog -a ! -f ${LOCALSTATEDIR}/log/lastlog ]
480then
Tim Rice0d8f2f32009-01-29 12:40:30 -0800481 echo
Damien Miller1fc231c2008-07-14 12:12:52 +1000482 csih_error_multi "${LOCALSTATEDIR}/log/lastlog exists, but is not a file." \
Tim Rice0d8f2f32009-01-29 12:40:30 -0800483 "Cannot create ssh host configuration."
Darren Tucker18614c22006-03-04 08:50:31 +1100484fi
485if [ ! -e ${LOCALSTATEDIR}/log/lastlog ]
486then
487 cat /dev/null > ${LOCALSTATEDIR}/log/lastlog
488 chmod 644 ${LOCALSTATEDIR}/log/lastlog
Ben Lindstrom6dbf3002002-07-03 23:33:19 +0000489fi
490
491# Create /var/empty file used as chroot jail for privilege separation
Damien Miller1fc231c2008-07-14 12:12:52 +1000492csih_make_dir "${LOCALSTATEDIR}/empty" "Cannot create log directory."
493chmod 755 "${LOCALSTATEDIR}/empty"
494setfacl -m u:system:rwx "${LOCALSTATEDIR}/empty"
495
496# host keys
497create_host_keys
498
499# use 'cmp' program to determine if a config file is identical
500# to the default version of that config file
501csih_check_program_or_error cmp diffutils
502
503
504# handle ssh_config
505csih_install_config "${SYSCONFDIR}/ssh_config" "${SYSCONFDIR}/defaults"
506if cmp "${SYSCONFDIR}/ssh_config" "${SYSCONFDIR}/defaults/${SYSCONFDIR}/ssh_config" >/dev/null 2>&1
Ben Lindstrom6dbf3002002-07-03 23:33:19 +0000507then
Darren Tucker798ca842003-11-13 11:28:49 +1100508 if [ "${port_number}" != "22" ]
Damien Miller8ac0a7e2001-03-07 21:38:19 +1100509 then
Damien Miller1fc231c2008-07-14 12:12:52 +1000510 csih_inform "Updating ${SYSCONFDIR}/ssh_config file with requested port"
Damien Miller8ac0a7e2001-03-07 21:38:19 +1100511 echo "Host localhost" >> ${SYSCONFDIR}/ssh_config
Darren Tucker798ca842003-11-13 11:28:49 +1100512 echo " Port ${port_number}" >> ${SYSCONFDIR}/ssh_config
Damien Miller8ac0a7e2001-03-07 21:38:19 +1100513 fi
Kevin Steves9be6e262000-10-29 19:18:49 +0000514fi
515
Damien Miller1fc231c2008-07-14 12:12:52 +1000516# handle sshd_config (and privsep)
517csih_install_config "${SYSCONFDIR}/sshd_config" "${SYSCONFDIR}/defaults"
518if ! cmp "${SYSCONFDIR}/sshd_config" "${SYSCONFDIR}/defaults/${SYSCONFDIR}/sshd_config" >/dev/null 2>&1
Kevin Steves9be6e262000-10-29 19:18:49 +0000519then
Damien Miller1fc231c2008-07-14 12:12:52 +1000520 grep -q UsePrivilegeSeparation ${SYSCONFDIR}/sshd_config && privsep_configured=yes
Kevin Steves9be6e262000-10-29 19:18:49 +0000521fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000522sshd_privsep
Kevin Steves9be6e262000-10-29 19:18:49 +0000523
Ben Lindstrom6dbf3002002-07-03 23:33:19 +0000524
Ben Lindstrom6dbf3002002-07-03 23:33:19 +0000525
Tim Rice0d8f2f32009-01-29 12:40:30 -0800526update_services_file
Damien Miller1fc231c2008-07-14 12:12:52 +1000527update_inetd_conf
528install_service
Ben Lindstroma5820292001-07-18 16:25:41 +0000529
Kevin Steves9be6e262000-10-29 19:18:49 +0000530echo
Damien Miller1fc231c2008-07-14 12:12:52 +1000531csih_inform "Host configuration finished. Have fun!"
532