blob: 0af6907dcc8d7b850779df102febc7f379990dc9 [file] [log] [blame]
Darren Tucker798ca842003-11-13 11:28:49 +11001#!/bin/bash
Kevin Steves9be6e262000-10-29 19:18:49 +00002#
Darren Tucker4d4fdc02009-07-07 21:19:11 +10003# ssh-host-config, Copyright 2000-2009 Red Hat Inc.
Kevin Steves9be6e262000-10-29 19:18:49 +00004#
5# This file is part of the Cygwin port of OpenSSH.
Darren Tucker8fdcba52009-07-12 21:58:42 +10006#
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 Steves9be6e262000-10-29 19:18:49 +000018
Damien Miller1fc231c2008-07-14 12:12:52 +100019# ======================================================================
20# Initialization
21# ======================================================================
22PROGNAME=$(basename $0)
23_tdir=$(dirname $0)
24PROGDIR=$(cd $_tdir && pwd)
25
26CSIH_SCRIPT=/usr/share/csih/cygwin-service-installation-helper.sh
27
Kevin Steves9be6e262000-10-29 19:18:49 +000028# Subdirectory where the new package is being installed
29PREFIX=/usr
30
31# Directory where the config files are stored
32SYSCONFDIR=/etc
Darren Tucker798ca842003-11-13 11:28:49 +110033LOCALSTATEDIR=/var
Kevin Steves9be6e262000-10-29 19:18:49 +000034
Damien Miller1fc231c2008-07-14 12:12:52 +100035source ${CSIH_SCRIPT}
Ben Lindstromb100ec92001-01-19 05:37:32 +000036
Damien Miller1fc231c2008-07-14 12:12:52 +100037port_number=22
Ben Lindstrom6dbf3002002-07-03 23:33:19 +000038privsep_configured=no
39privsep_used=yes
Tim Riceca3692d2009-01-28 12:50:04 -080040cygwin_value=""
Darren Tucker4d4fdc02009-07-07 21:19:11 +100041user_account=
Damien Miller1fc231c2008-07-14 12:12:52 +100042password_value=
Darren Tucker4d4fdc02009-07-07 21:19:11 +100043opt_force=no
Ben Lindstrom6dbf3002002-07-03 23:33:19 +000044
Damien Miller1fc231c2008-07-14 12:12:52 +100045# ======================================================================
46# Routine: create_host_keys
47# ======================================================================
48create_host_keys() {
49 if [ ! -f "${SYSCONFDIR}/ssh_host_key" ]
Ben Lindstromb100ec92001-01-19 05:37:32 +000050 then
Damien Miller1fc231c2008-07-14 12:12:52 +100051 csih_inform "Generating ${SYSCONFDIR}/ssh_host_key"
52 ssh-keygen -t rsa1 -f ${SYSCONFDIR}/ssh_host_key -N '' > /dev/null
Ben Lindstromb100ec92001-01-19 05:37:32 +000053 fi
Tim Rice0d8f2f32009-01-29 12:40:30 -080054
Damien Miller1fc231c2008-07-14 12:12:52 +100055 if [ ! -f "${SYSCONFDIR}/ssh_host_rsa_key" ]
Kevin Steves9be6e262000-10-29 19:18:49 +000056 then
Damien Miller1fc231c2008-07-14 12:12:52 +100057 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 Rice0d8f2f32009-01-29 12:40:30 -080060
Damien Miller1fc231c2008-07-14 12:12:52 +100061 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 Tuckerea676a62011-02-06 13:31:23 +110066
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 Miller1fc231c2008-07-14 12:12:52 +100072} # --- End of create_host_keys --- #
73
74# ======================================================================
75# Routine: update_services_file
76# ======================================================================
77update_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 Steves9be6e262000-10-29 19:18:49 +000091 else
Damien Miller1fc231c2008-07-14 12:12:52 +100092 _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 Steves9be6e262000-10-29 19:18:49 +000096 fi
Damien Miller1fc231c2008-07-14 12:12:52 +100097 _serv_tmp="${_my_etcdir}/srv.out.$$"
Tim Rice0d8f2f32009-01-29 12:40:30 -080098
Darren Tucker62131dc2010-03-24 13:03:32 +110099 mount -o text,posix=0,noacl -f "${_win_etcdir}" "${_my_etcdir}"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800100
Damien Miller1fc231c2008-07-14 12:12:52 +1000101 # Depends on the above mount
102 _wservices=`cygpath -w "${_services}"`
Tim Rice0d8f2f32009-01-29 12:40:30 -0800103
Damien Miller1fc231c2008-07-14 12:12:52 +1000104 # 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 Rice0d8f2f32009-01-29 12:40:30 -0800112 csih_inform "Removing sshd from ${_wservices}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000113 else
Tim Rice0d8f2f32009-01-29 12:40:30 -0800114 csih_warning "Removing sshd from ${_wservices} failed!"
Damien Miller1fc231c2008-07-14 12:12:52 +1000115 fi
116 rm -f "${_serv_tmp}"
117 else
118 csih_warning "Removing sshd from ${_wservices} failed!"
119 fi
120 fi
Tim Rice0d8f2f32009-01-29 12:40:30 -0800121
Damien Miller1fc231c2008-07-14 12:12:52 +1000122 # 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 Rice0d8f2f32009-01-29 12:40:30 -0800129 csih_inform "Added ssh to ${_wservices}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000130 else
Tim Rice0d8f2f32009-01-29 12:40:30 -0800131 csih_warning "Adding ssh to ${_wservices} failed!"
Damien Miller1fc231c2008-07-14 12:12:52 +1000132 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 Steves9be6e262000-10-29 19:18:49 +0000140
Damien Miller1fc231c2008-07-14 12:12:52 +1000141# ======================================================================
142# Routine: sshd_privsep
143# MODIFIES: privsep_configured privsep_used
144# ======================================================================
145sshd_privsep() {
146 local sshdconfig_tmp
Ben Lindstromb100ec92001-01-19 05:37:32 +0000147
Damien Miller1fc231c2008-07-14 12:12:52 +1000148 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 Rice0d8f2f32009-01-29 12:40:30 -0800157 privsep_used=yes
158 if ! csih_create_unprivileged_user sshd
159 then
Damien Miller1fc231c2008-07-14 12:12:52 +1000160 csih_warning "Couldn't create user 'sshd'!"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800161 csih_warning "Privilege separation set to 'no' again!"
162 csih_warning "Check your ${SYSCONFDIR}/sshd_config file!"
Damien Miller1fc231c2008-07-14 12:12:52 +1000163 privsep_used=no
Tim Rice0d8f2f32009-01-29 12:40:30 -0800164 fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000165 else
Tim Rice0d8f2f32009-01-29 12:40:30 -0800166 privsep_used=no
Damien Miller1fc231c2008-07-14 12:12:52 +1000167 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 Rice0d8f2f32009-01-29 12:40:30 -0800174
Damien Miller1fc231c2008-07-14 12:12:52 +1000175 # 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 Rice0d8f2f32009-01-29 12:40:30 -0800184 < ${SYSCONFDIR}/sshd_config \
185 > "${sshdconfig_tmp}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000186 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# ======================================================================
197update_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 Rice0d8f2f32009-01-29 12:40:30 -0800216 grep -v '^[# \t]*ssh' "${_inetcnf}" >> "${_inetcnf_tmp}"
217 if [ -f "${_inetcnf_tmp}" ]
218 then
219 if mv "${_inetcnf_tmp}" "${_inetcnf}"
220 then
Damien Miller1fc231c2008-07-14 12:12:52 +1000221 csih_inform "Removed ssh[d] from ${_inetcnf}"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800222 else
Damien Miller1fc231c2008-07-14 12:12:52 +1000223 csih_warning "Removing ssh[d] from ${_inetcnf} failed!"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800224 fi
225 rm -f "${_inetcnf_tmp}"
226 else
227 csih_warning "Removing ssh[d] from ${_inetcnf} failed!"
228 fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000229 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 Rice0d8f2f32009-01-29 12:40:30 -0800237 sed -e 's/@COMMENT@[ \t]*//' < "${_sshd_inetd_conf}" > "${_sshd_inetd_conf_tmp}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000238 else
Tim Rice0d8f2f32009-01-29 12:40:30 -0800239 sed -e 's/@COMMENT@[ \t]*/# /' < "${_sshd_inetd_conf}" > "${_sshd_inetd_conf_tmp}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000240 fi
241 mv "${_sshd_inetd_conf_tmp}" "${_sshd_inetd_conf}"
242 csih_inform "Updated ${_sshd_inetd_conf}"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800243 fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000244
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 Rice0d8f2f32009-01-29 12:40:30 -0800256 if mv "${_inetcnf_tmp}" "${_inetcnf}"
257 then
Damien Miller1fc231c2008-07-14 12:12:52 +1000258 csih_inform "Removed sshd from ${_inetcnf}"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800259 else
Damien Miller1fc231c2008-07-14 12:12:52 +1000260 csih_warning "Removing sshd from ${_inetcnf} failed!"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800261 fi
262 rm -f "${_inetcnf_tmp}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000263 else
Tim Rice0d8f2f32009-01-29 12:40:30 -0800264 csih_warning "Removing sshd from ${_inetcnf} failed!"
Damien Miller1fc231c2008-07-14 12:12:52 +1000265 fi
266 fi
Tim Rice0d8f2f32009-01-29 12:40:30 -0800267
Damien Miller1fc231c2008-07-14 12:12:52 +1000268 # 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 Rice0d8f2f32009-01-29 12:40:30 -0800273 echo 'ssh stream tcp nowait root /usr/sbin/sshd sshd -i' >> "${_inetcnf}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000274 else
Tim Rice0d8f2f32009-01-29 12:40:30 -0800275 echo '# ssh stream tcp nowait root /usr/sbin/sshd sshd -i' >> "${_inetcnf}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000276 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# ======================================================================
286install_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 Rice0d8f2f32009-01-29 12:40:30 -0800301 csih_get_cygenv "${cygwin_value}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000302
Tim Rice0d8f2f32009-01-29 12:40:30 -0800303 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 Tucker4d4fdc02009-07-07 21:19:11 +1000310
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 Rice0d8f2f32009-01-29 12:40:30 -0800315 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 Miller1fc231c2008-07-14 12:12:52 +1000321
Tim Rice0d8f2f32009-01-29 12:40:30 -0800322 # never returns empty if NT or above
323 run_service_as=$(csih_service_should_run_as)
Damien Miller1fc231c2008-07-14 12:12:52 +1000324
Tim Rice0d8f2f32009-01-29 12:40:30 -0800325 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 Miller1fc231c2008-07-14 12:12:52 +1000334
Tim Rice0d8f2f32009-01-29 12:40:30 -0800335 # 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 Miller1fc231c2008-07-14 12:12:52 +1000339
Tim Rice0d8f2f32009-01-29 12:40:30 -0800340 csih_check_user "${run_service_as}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000341
Tim Rice6a325342009-01-29 12:30:01 -0800342 if [ -n "${csih_cygenv}" ]
343 then
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000344 cygwin_env=( -e "CYGWIN=${csih_cygenv}" )
Tim Rice6a325342009-01-29 12:30:01 -0800345 fi
Tim Rice0d8f2f32009-01-29 12:40:30 -0800346 if [ -z "${password}" ]
347 then
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000348 if cygrunsrv -I sshd -d "CYGWIN sshd" -p /usr/sbin/sshd \
349 -a "-D" -y tcpip "${cygwin_env[@]}"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800350 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 Tucker4d4fdc02009-07-07 21:19:11 +1000358 if cygrunsrv -I sshd -d "CYGWIN sshd" -p /usr/sbin/sshd \
359 -a "-D" -y tcpip "${cygwin_env[@]}" \
Tim Rice6a325342009-01-29 12:30:01 -0800360 -u "${run_service_as}" -w "${password}"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800361 then
Damien Miller1fc231c2008-07-14 12:12:52 +1000362 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 Rice0d8f2f32009-01-29 12:40:30 -0800365 csih_inform "\`cygrunsrv -S sshd'. Otherwise, it will start automatically"
366 csih_inform "after the next reboot."
367 fi
368 fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000369
Tim Rice0d8f2f32009-01-29 12:40:30 -0800370 # 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 Miller1fc231c2008-07-14 12:12:52 +1000378 chown "${run_service_as}".544 ${LOCALSTATEDIR}/log/sshd.log
Tim Rice0d8f2f32009-01-29 12:40:30 -0800379 fi
380 else
381 csih_warning "Something went wrong installing the sshd service."
382 fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000383 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.
402if [ "$PROGDIR" = "/etc/postinstall" ]
403then
404 csih_auto_answer="no"
405 csih_disable_color
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000406 opt_force=yes
Damien Miller1fc231c2008-07-14 12:12:52 +1000407fi
408if [ -n "${SSH_HOST_CONFIG_AUTO_ANSWER_NO}" ]
409then
410 csih_auto_answer="no"
411 csih_disable_color
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000412 opt_force=yes
Damien Miller1fc231c2008-07-14 12:12:52 +1000413fi
414
415# ======================================================================
416# Parse options
417# ======================================================================
Ben Lindstromb100ec92001-01-19 05:37:32 +0000418while :
419do
420 case $# in
421 0)
422 break
423 ;;
424 esac
425
426 option=$1
427 shift
428
Darren Tucker798ca842003-11-13 11:28:49 +1100429 case "${option}" in
Ben Lindstromb100ec92001-01-19 05:37:32 +0000430 -d | --debug )
431 set -x
Damien Miller1fc231c2008-07-14 12:12:52 +1000432 csih_trace_on
Ben Lindstromb100ec92001-01-19 05:37:32 +0000433 ;;
434
435 -y | --yes )
Damien Miller1fc231c2008-07-14 12:12:52 +1000436 csih_auto_answer=yes
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000437 opt_force=yes
Ben Lindstromb100ec92001-01-19 05:37:32 +0000438 ;;
439
440 -n | --no )
Damien Miller1fc231c2008-07-14 12:12:52 +1000441 csih_auto_answer=no
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000442 opt_force=yes
Ben Lindstromb100ec92001-01-19 05:37:32 +0000443 ;;
444
Darren Tucker798ca842003-11-13 11:28:49 +1100445 -c | --cygwin )
446 cygwin_value="$1"
447 shift
448 ;;
449
Damien Miller8ac0a7e2001-03-07 21:38:19 +1100450 -p | --port )
451 port_number=$1
452 shift
453 ;;
454
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000455 -u | --user )
456 user_account="$1"
457 shift
458 ;;
459
Darren Tucker798ca842003-11-13 11:28:49 +1100460 -w | --pwd )
461 password_value="$1"
462 shift
463 ;;
464
Damien Miller1fc231c2008-07-14 12:12:52 +1000465 --privileged )
466 csih_FORCE_PRIVILEGED_USER=yes
467 ;;
468
Ben Lindstromb100ec92001-01-19 05:37:32 +0000469 *)
470 echo "usage: ${progname} [OPTION]..."
471 echo
472 echo "This script creates an OpenSSH host configuration."
473 echo
474 echo "Options:"
Darren Tucker798ca842003-11-13 11:28:49 +1100475 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 Tucker4d4fdc02009-07-07 21:19:11 +1000480 echo " --user -u <account> privileged user for service."
Damien Miller1fc231c2008-07-14 12:12:52 +1000481 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 Lindstromb100ec92001-01-19 05:37:32 +0000484 echo
485 exit 1
486 ;;
487
488 esac
489done
490
Damien Miller1fc231c2008-07-14 12:12:52 +1000491# ======================================================================
492# Action!
493# ======================================================================
Ben Lindstrom6dbf3002002-07-03 23:33:19 +0000494
Kevin Steves9be6e262000-10-29 19:18:49 +0000495# Check for running ssh/sshd processes first. Refuse to do anything while
496# some ssh processes are still running
Darren Tucker83795d62008-12-01 21:34:28 +1100497if ps -ef | grep -q '/sshd\?$'
Kevin Steves9be6e262000-10-29 19:18:49 +0000498then
499 echo
Damien Miller1fc231c2008-07-14 12:12:52 +1000500 csih_error "There are still ssh processes running. Please shut them down first."
Kevin Steves9be6e262000-10-29 19:18:49 +0000501fi
502
503# Check for ${SYSCONFDIR} directory
Damien Miller1fc231c2008-07-14 12:12:52 +1000504csih_make_dir "${SYSCONFDIR}" "Cannot create global configuration files."
505chmod 775 "${SYSCONFDIR}"
506setfacl -m u:system:rwx "${SYSCONFDIR}"
Kevin Steves9be6e262000-10-29 19:18:49 +0000507
Damien Miller1fc231c2008-07-14 12:12:52 +1000508# Check for /var/log directory
509csih_make_dir "${LOCALSTATEDIR}/log" "Cannot create log directory."
510chmod 775 "${LOCALSTATEDIR}/log"
511setfacl -m u:system:rwx "${LOCALSTATEDIR}/log"
Kevin Steves9be6e262000-10-29 19:18:49 +0000512
Damien Miller1fc231c2008-07-14 12:12:52 +1000513# Create /var/log/lastlog if not already exists
Darren Tucker18614c22006-03-04 08:50:31 +1100514if [ -e ${LOCALSTATEDIR}/log/lastlog -a ! -f ${LOCALSTATEDIR}/log/lastlog ]
515then
Tim Rice0d8f2f32009-01-29 12:40:30 -0800516 echo
Damien Miller1fc231c2008-07-14 12:12:52 +1000517 csih_error_multi "${LOCALSTATEDIR}/log/lastlog exists, but is not a file." \
Tim Rice0d8f2f32009-01-29 12:40:30 -0800518 "Cannot create ssh host configuration."
Darren Tucker18614c22006-03-04 08:50:31 +1100519fi
520if [ ! -e ${LOCALSTATEDIR}/log/lastlog ]
521then
522 cat /dev/null > ${LOCALSTATEDIR}/log/lastlog
523 chmod 644 ${LOCALSTATEDIR}/log/lastlog
Ben Lindstrom6dbf3002002-07-03 23:33:19 +0000524fi
525
526# Create /var/empty file used as chroot jail for privilege separation
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000527csih_make_dir "${LOCALSTATEDIR}/empty" "Cannot create ${LOCALSTATEDIR}/empty directory."
Damien Miller1fc231c2008-07-14 12:12:52 +1000528chmod 755 "${LOCALSTATEDIR}/empty"
529setfacl -m u:system:rwx "${LOCALSTATEDIR}/empty"
530
531# host keys
532create_host_keys
533
534# use 'cmp' program to determine if a config file is identical
535# to the default version of that config file
536csih_check_program_or_error cmp diffutils
537
538
539# handle ssh_config
540csih_install_config "${SYSCONFDIR}/ssh_config" "${SYSCONFDIR}/defaults"
541if cmp "${SYSCONFDIR}/ssh_config" "${SYSCONFDIR}/defaults/${SYSCONFDIR}/ssh_config" >/dev/null 2>&1
Ben Lindstrom6dbf3002002-07-03 23:33:19 +0000542then
Darren Tucker798ca842003-11-13 11:28:49 +1100543 if [ "${port_number}" != "22" ]
Damien Miller8ac0a7e2001-03-07 21:38:19 +1100544 then
Damien Miller1fc231c2008-07-14 12:12:52 +1000545 csih_inform "Updating ${SYSCONFDIR}/ssh_config file with requested port"
Damien Miller8ac0a7e2001-03-07 21:38:19 +1100546 echo "Host localhost" >> ${SYSCONFDIR}/ssh_config
Darren Tucker798ca842003-11-13 11:28:49 +1100547 echo " Port ${port_number}" >> ${SYSCONFDIR}/ssh_config
Damien Miller8ac0a7e2001-03-07 21:38:19 +1100548 fi
Kevin Steves9be6e262000-10-29 19:18:49 +0000549fi
550
Damien Miller1fc231c2008-07-14 12:12:52 +1000551# handle sshd_config (and privsep)
552csih_install_config "${SYSCONFDIR}/sshd_config" "${SYSCONFDIR}/defaults"
553if ! cmp "${SYSCONFDIR}/sshd_config" "${SYSCONFDIR}/defaults/${SYSCONFDIR}/sshd_config" >/dev/null 2>&1
Kevin Steves9be6e262000-10-29 19:18:49 +0000554then
Damien Miller1fc231c2008-07-14 12:12:52 +1000555 grep -q UsePrivilegeSeparation ${SYSCONFDIR}/sshd_config && privsep_configured=yes
Kevin Steves9be6e262000-10-29 19:18:49 +0000556fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000557sshd_privsep
Kevin Steves9be6e262000-10-29 19:18:49 +0000558
Ben Lindstrom6dbf3002002-07-03 23:33:19 +0000559
Ben Lindstrom6dbf3002002-07-03 23:33:19 +0000560
Tim Rice0d8f2f32009-01-29 12:40:30 -0800561update_services_file
Damien Miller1fc231c2008-07-14 12:12:52 +1000562update_inetd_conf
563install_service
Ben Lindstroma5820292001-07-18 16:25:41 +0000564
Kevin Steves9be6e262000-10-29 19:18:49 +0000565echo
Damien Miller1fc231c2008-07-14 12:12:52 +1000566csih_inform "Host configuration finished. Have fun!"
567