blob: 32cb6ba23b7a5586373ddf6e6def538fd775fb11 [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.
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=""
Darren Tucker4d4fdc02009-07-07 21:19:11 +100029user_account=
Damien Miller1fc231c2008-07-14 12:12:52 +100030password_value=
Darren Tucker4d4fdc02009-07-07 21:19:11 +100031opt_force=no
Ben Lindstrom6dbf3002002-07-03 23:33:19 +000032
Damien Miller1fc231c2008-07-14 12:12:52 +100033# ======================================================================
34# Routine: create_host_keys
35# ======================================================================
36create_host_keys() {
37 if [ ! -f "${SYSCONFDIR}/ssh_host_key" ]
Ben Lindstromb100ec92001-01-19 05:37:32 +000038 then
Damien Miller1fc231c2008-07-14 12:12:52 +100039 csih_inform "Generating ${SYSCONFDIR}/ssh_host_key"
40 ssh-keygen -t rsa1 -f ${SYSCONFDIR}/ssh_host_key -N '' > /dev/null
Ben Lindstromb100ec92001-01-19 05:37:32 +000041 fi
Tim Rice0d8f2f32009-01-29 12:40:30 -080042
Damien Miller1fc231c2008-07-14 12:12:52 +100043 if [ ! -f "${SYSCONFDIR}/ssh_host_rsa_key" ]
Kevin Steves9be6e262000-10-29 19:18:49 +000044 then
Damien Miller1fc231c2008-07-14 12:12:52 +100045 csih_inform "Generating ${SYSCONFDIR}/ssh_host_rsa_key"
46 ssh-keygen -t rsa -f ${SYSCONFDIR}/ssh_host_rsa_key -N '' > /dev/null
47 fi
Tim Rice0d8f2f32009-01-29 12:40:30 -080048
Damien Miller1fc231c2008-07-14 12:12:52 +100049 if [ ! -f "${SYSCONFDIR}/ssh_host_dsa_key" ]
50 then
51 csih_inform "Generating ${SYSCONFDIR}/ssh_host_dsa_key"
52 ssh-keygen -t dsa -f ${SYSCONFDIR}/ssh_host_dsa_key -N '' > /dev/null
53 fi
54} # --- End of create_host_keys --- #
55
56# ======================================================================
57# Routine: update_services_file
58# ======================================================================
59update_services_file() {
60 local _my_etcdir="/ssh-host-config.$$"
61 local _win_etcdir
62 local _services
63 local _spaces
64 local _serv_tmp
65 local _wservices
66
67 if csih_is_nt
68 then
69 _win_etcdir="${SYSTEMROOT}\\system32\\drivers\\etc"
70 _services="${_my_etcdir}/services"
71 # On NT, 27 spaces, no space after the hash
72 _spaces=" #"
Kevin Steves9be6e262000-10-29 19:18:49 +000073 else
Damien Miller1fc231c2008-07-14 12:12:52 +100074 _win_etcdir="${WINDIR}"
75 _services="${_my_etcdir}/SERVICES"
76 # On 9x, 18 spaces (95 is very touchy), a space after the hash
77 _spaces=" # "
Kevin Steves9be6e262000-10-29 19:18:49 +000078 fi
Damien Miller1fc231c2008-07-14 12:12:52 +100079 _serv_tmp="${_my_etcdir}/srv.out.$$"
Tim Rice0d8f2f32009-01-29 12:40:30 -080080
Tim Riceca3692d2009-01-28 12:50:04 -080081 mount -o text -f "${_win_etcdir}" "${_my_etcdir}"
Tim Rice0d8f2f32009-01-29 12:40:30 -080082
Damien Miller1fc231c2008-07-14 12:12:52 +100083 # Depends on the above mount
84 _wservices=`cygpath -w "${_services}"`
Tim Rice0d8f2f32009-01-29 12:40:30 -080085
Damien Miller1fc231c2008-07-14 12:12:52 +100086 # Remove sshd 22/port from services
87 if [ `grep -q 'sshd[ \t][ \t]*22' "${_services}"; echo $?` -eq 0 ]
88 then
89 grep -v 'sshd[ \t][ \t]*22' "${_services}" > "${_serv_tmp}"
90 if [ -f "${_serv_tmp}" ]
91 then
92 if mv "${_serv_tmp}" "${_services}"
93 then
Tim Rice0d8f2f32009-01-29 12:40:30 -080094 csih_inform "Removing sshd from ${_wservices}"
Damien Miller1fc231c2008-07-14 12:12:52 +100095 else
Tim Rice0d8f2f32009-01-29 12:40:30 -080096 csih_warning "Removing sshd from ${_wservices} failed!"
Damien Miller1fc231c2008-07-14 12:12:52 +100097 fi
98 rm -f "${_serv_tmp}"
99 else
100 csih_warning "Removing sshd from ${_wservices} failed!"
101 fi
102 fi
Tim Rice0d8f2f32009-01-29 12:40:30 -0800103
Damien Miller1fc231c2008-07-14 12:12:52 +1000104 # Add ssh 22/tcp and ssh 22/udp to services
105 if [ `grep -q 'ssh[ \t][ \t]*22' "${_services}"; echo $?` -ne 0 ]
106 then
107 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}"
108 then
109 if mv "${_serv_tmp}" "${_services}"
110 then
Tim Rice0d8f2f32009-01-29 12:40:30 -0800111 csih_inform "Added ssh to ${_wservices}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000112 else
Tim Rice0d8f2f32009-01-29 12:40:30 -0800113 csih_warning "Adding ssh to ${_wservices} failed!"
Damien Miller1fc231c2008-07-14 12:12:52 +1000114 fi
115 rm -f "${_serv_tmp}"
116 else
117 csih_warning "Adding ssh to ${_wservices} failed!"
118 fi
119 fi
120 umount "${_my_etcdir}"
121} # --- End of update_services_file --- #
Kevin Steves9be6e262000-10-29 19:18:49 +0000122
Damien Miller1fc231c2008-07-14 12:12:52 +1000123# ======================================================================
124# Routine: sshd_privsep
125# MODIFIES: privsep_configured privsep_used
126# ======================================================================
127sshd_privsep() {
128 local sshdconfig_tmp
Ben Lindstromb100ec92001-01-19 05:37:32 +0000129
Damien Miller1fc231c2008-07-14 12:12:52 +1000130 if [ "${privsep_configured}" != "yes" ]
131 then
132 if csih_is_nt
133 then
134 csih_inform "Privilege separation is set to yes by default since OpenSSH 3.3."
135 csih_inform "However, this requires a non-privileged account called 'sshd'."
136 csih_inform "For more info on privilege separation read /usr/share/doc/openssh/README.privsep."
137 if csih_request "Should privilege separation be used?"
138 then
Tim Rice0d8f2f32009-01-29 12:40:30 -0800139 privsep_used=yes
140 if ! csih_create_unprivileged_user sshd
141 then
Damien Miller1fc231c2008-07-14 12:12:52 +1000142 csih_warning "Couldn't create user 'sshd'!"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800143 csih_warning "Privilege separation set to 'no' again!"
144 csih_warning "Check your ${SYSCONFDIR}/sshd_config file!"
Damien Miller1fc231c2008-07-14 12:12:52 +1000145 privsep_used=no
Tim Rice0d8f2f32009-01-29 12:40:30 -0800146 fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000147 else
Tim Rice0d8f2f32009-01-29 12:40:30 -0800148 privsep_used=no
Damien Miller1fc231c2008-07-14 12:12:52 +1000149 fi
150 else
151 # On 9x don't use privilege separation. Since security isn't
152 # available it just adds useless additional processes.
153 privsep_used=no
154 fi
155 fi
Tim Rice0d8f2f32009-01-29 12:40:30 -0800156
Damien Miller1fc231c2008-07-14 12:12:52 +1000157 # Create default sshd_config from skeleton files in /etc/defaults/etc or
158 # modify to add the missing privsep configuration option
159 if cmp "${SYSCONFDIR}/sshd_config" "${SYSCONFDIR}/defaults/${SYSCONFDIR}/sshd_config" >/dev/null 2>&1
160 then
161 csih_inform "Updating ${SYSCONFDIR}/sshd_config file"
162 sshdconfig_tmp=${SYSCONFDIR}/sshd_config.$$
163 sed -e "s/^#UsePrivilegeSeparation yes/UsePrivilegeSeparation ${privsep_used}/
164 s/^#Port 22/Port ${port_number}/
165 s/^#StrictModes yes/StrictModes no/" \
Tim Rice0d8f2f32009-01-29 12:40:30 -0800166 < ${SYSCONFDIR}/sshd_config \
167 > "${sshdconfig_tmp}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000168 mv "${sshdconfig_tmp}" ${SYSCONFDIR}/sshd_config
169 elif [ "${privsep_configured}" != "yes" ]
170 then
171 echo >> ${SYSCONFDIR}/sshd_config
172 echo "UsePrivilegeSeparation ${privsep_used}" >> ${SYSCONFDIR}/sshd_config
173 fi
174} # --- End of sshd_privsep --- #
175
176# ======================================================================
177# Routine: update_inetd_conf
178# ======================================================================
179update_inetd_conf() {
180 local _inetcnf="${SYSCONFDIR}/inetd.conf"
181 local _inetcnf_tmp="${SYSCONFDIR}/inetd.conf.$$"
182 local _inetcnf_dir="${SYSCONFDIR}/inetd.d"
183 local _sshd_inetd_conf="${_inetcnf_dir}/sshd-inetd"
184 local _sshd_inetd_conf_tmp="${_inetcnf_dir}/sshd-inetd.$$"
185 local _with_comment=1
186
187 if [ -d "${_inetcnf_dir}" ]
188 then
189 # we have inetutils-1.5 inetd.d support
190 if [ -f "${_inetcnf}" ]
191 then
192 grep -q '^[ \t]*ssh' "${_inetcnf}" && _with_comment=0
193
194 # check for sshd OR ssh in top-level inetd.conf file, and remove
195 # will be replaced by a file in inetd.d/
196 if [ `grep -q '^[# \t]*ssh' "${_inetcnf}"; echo $?` -eq 0 ]
197 then
Tim Rice0d8f2f32009-01-29 12:40:30 -0800198 grep -v '^[# \t]*ssh' "${_inetcnf}" >> "${_inetcnf_tmp}"
199 if [ -f "${_inetcnf_tmp}" ]
200 then
201 if mv "${_inetcnf_tmp}" "${_inetcnf}"
202 then
Damien Miller1fc231c2008-07-14 12:12:52 +1000203 csih_inform "Removed ssh[d] from ${_inetcnf}"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800204 else
Damien Miller1fc231c2008-07-14 12:12:52 +1000205 csih_warning "Removing ssh[d] from ${_inetcnf} failed!"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800206 fi
207 rm -f "${_inetcnf_tmp}"
208 else
209 csih_warning "Removing ssh[d] from ${_inetcnf} failed!"
210 fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000211 fi
212 fi
213
214 csih_install_config "${_sshd_inetd_conf}" "${SYSCONFDIR}/defaults"
215 if cmp "${SYSCONFDIR}/defaults${_sshd_inetd_conf}" "${_sshd_inetd_conf}" >/dev/null 2>&1
216 then
217 if [ "${_with_comment}" -eq 0 ]
218 then
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 else
Tim Rice0d8f2f32009-01-29 12:40:30 -0800221 sed -e 's/@COMMENT@[ \t]*/# /' < "${_sshd_inetd_conf}" > "${_sshd_inetd_conf_tmp}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000222 fi
223 mv "${_sshd_inetd_conf_tmp}" "${_sshd_inetd_conf}"
224 csih_inform "Updated ${_sshd_inetd_conf}"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800225 fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000226
227 elif [ -f "${_inetcnf}" ]
228 then
229 grep -q '^[ \t]*sshd' "${_inetcnf}" && _with_comment=0
230
231 # check for sshd in top-level inetd.conf file, and remove
232 # will be replaced by a file in inetd.d/
233 if [ `grep -q '^[# \t]*sshd' "${_inetcnf}"; echo $?` -eq 0 ]
234 then
235 grep -v '^[# \t]*sshd' "${_inetcnf}" >> "${_inetcnf_tmp}"
236 if [ -f "${_inetcnf_tmp}" ]
237 then
Tim Rice0d8f2f32009-01-29 12:40:30 -0800238 if mv "${_inetcnf_tmp}" "${_inetcnf}"
239 then
Damien Miller1fc231c2008-07-14 12:12:52 +1000240 csih_inform "Removed sshd from ${_inetcnf}"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800241 else
Damien Miller1fc231c2008-07-14 12:12:52 +1000242 csih_warning "Removing sshd from ${_inetcnf} failed!"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800243 fi
244 rm -f "${_inetcnf_tmp}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000245 else
Tim Rice0d8f2f32009-01-29 12:40:30 -0800246 csih_warning "Removing sshd from ${_inetcnf} failed!"
Damien Miller1fc231c2008-07-14 12:12:52 +1000247 fi
248 fi
Tim Rice0d8f2f32009-01-29 12:40:30 -0800249
Damien Miller1fc231c2008-07-14 12:12:52 +1000250 # Add ssh line to inetd.conf
251 if [ `grep -q '^[# \t]*ssh' "${_inetcnf}"; echo $?` -ne 0 ]
252 then
253 if [ "${_with_comment}" -eq 0 ]
254 then
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 else
Tim Rice0d8f2f32009-01-29 12:40:30 -0800257 echo '# ssh stream tcp nowait root /usr/sbin/sshd sshd -i' >> "${_inetcnf}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000258 fi
259 csih_inform "Added ssh to ${_inetcnf}"
260 fi
261 fi
262} # --- End of update_inetd_conf --- #
263
264# ======================================================================
265# Routine: install_service
266# Install sshd as a service
267# ======================================================================
268install_service() {
269 local run_service_as
270 local password
271
272 if csih_is_nt
273 then
274 if ! cygrunsrv -Q sshd >/dev/null 2>&1
275 then
276 echo
277 echo
278 csih_warning "The following functions require administrator privileges!"
279 echo
280 echo -e "${_csih_QUERY_STR} Do you want to install sshd as a service?"
281 if csih_request "(Say \"no\" if it is already installed as a service)"
282 then
Tim Rice0d8f2f32009-01-29 12:40:30 -0800283 csih_get_cygenv "${cygwin_value}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000284
Tim Rice0d8f2f32009-01-29 12:40:30 -0800285 if ( csih_is_nt2003 || [ "$csih_FORCE_PRIVILEGED_USER" = "yes" ] )
286 then
287 csih_inform "On Windows Server 2003, Windows Vista, and above, the"
288 csih_inform "SYSTEM account cannot setuid to other users -- a capability"
289 csih_inform "sshd requires. You need to have or to create a privileged"
290 csih_inform "account. This script will help you do so."
291 echo
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000292
293 [ "${opt_force}" = "yes" ] && opt_f=-f
294 [ -n "${user_account}" ] && opt_u="-u ""${user_account}"""
295 csih_select_privileged_username ${opt_f} ${opt_u} sshd
296
Tim Rice0d8f2f32009-01-29 12:40:30 -0800297 if ! csih_create_privileged_user "${password_value}"
298 then
299 csih_error_recoverable "There was a serious problem creating a privileged user."
300 csih_request "Do you want to proceed anyway?" || exit 1
301 fi
302 fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000303
Tim Rice0d8f2f32009-01-29 12:40:30 -0800304 # never returns empty if NT or above
305 run_service_as=$(csih_service_should_run_as)
Damien Miller1fc231c2008-07-14 12:12:52 +1000306
Tim Rice0d8f2f32009-01-29 12:40:30 -0800307 if [ "${run_service_as}" = "${csih_PRIVILEGED_USERNAME}" ]
308 then
309 password="${csih_PRIVILEGED_PASSWORD}"
310 if [ -z "${password}" ]
311 then
312 csih_get_value "Please enter the password for user '${run_service_as}':" "-s"
313 password="${csih_value}"
314 fi
315 fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000316
Tim Rice0d8f2f32009-01-29 12:40:30 -0800317 # at this point, we either have $run_service_as = "system" and $password is empty,
318 # or $run_service_as is some privileged user and (hopefully) $password contains
319 # the correct password. So, from here out, we use '-z "${password}"' to discriminate
320 # the two cases.
Damien Miller1fc231c2008-07-14 12:12:52 +1000321
Tim Rice0d8f2f32009-01-29 12:40:30 -0800322 csih_check_user "${run_service_as}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000323
Tim Rice6a325342009-01-29 12:30:01 -0800324 if [ -n "${csih_cygenv}" ]
325 then
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000326 cygwin_env=( -e "CYGWIN=${csih_cygenv}" )
Tim Rice6a325342009-01-29 12:30:01 -0800327 fi
Tim Rice0d8f2f32009-01-29 12:40:30 -0800328 if [ -z "${password}" ]
329 then
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000330 if cygrunsrv -I sshd -d "CYGWIN sshd" -p /usr/sbin/sshd \
331 -a "-D" -y tcpip "${cygwin_env[@]}"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800332 then
333 echo
334 csih_inform "The sshd service has been installed under the LocalSystem"
335 csih_inform "account (also known as SYSTEM). To start the service now, call"
336 csih_inform "\`net start sshd' or \`cygrunsrv -S sshd'. Otherwise, it"
337 csih_inform "will start automatically after the next reboot."
338 fi
339 else
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000340 if cygrunsrv -I sshd -d "CYGWIN sshd" -p /usr/sbin/sshd \
341 -a "-D" -y tcpip "${cygwin_env[@]}" \
Tim Rice6a325342009-01-29 12:30:01 -0800342 -u "${run_service_as}" -w "${password}"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800343 then
Damien Miller1fc231c2008-07-14 12:12:52 +1000344 echo
345 csih_inform "The sshd service has been installed under the '${run_service_as}'"
346 csih_inform "account. To start the service now, call \`net start sshd' or"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800347 csih_inform "\`cygrunsrv -S sshd'. Otherwise, it will start automatically"
348 csih_inform "after the next reboot."
349 fi
350 fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000351
Tim Rice0d8f2f32009-01-29 12:40:30 -0800352 # now, if successfully installed, set ownership of the affected files
353 if cygrunsrv -Q sshd >/dev/null 2>&1
354 then
355 chown "${run_service_as}" ${SYSCONFDIR}/ssh*
356 chown "${run_service_as}".544 ${LOCALSTATEDIR}/empty
357 chown "${run_service_as}".544 ${LOCALSTATEDIR}/log/lastlog
358 if [ -f ${LOCALSTATEDIR}/log/sshd.log ]
359 then
Damien Miller1fc231c2008-07-14 12:12:52 +1000360 chown "${run_service_as}".544 ${LOCALSTATEDIR}/log/sshd.log
Tim Rice0d8f2f32009-01-29 12:40:30 -0800361 fi
362 else
363 csih_warning "Something went wrong installing the sshd service."
364 fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000365 fi # user allowed us to install as service
366 fi # service not yet installed
367 fi # csih_is_nt
368} # --- End of install_service --- #
369
370# ======================================================================
371# Main Entry Point
372# ======================================================================
373
374# Check how the script has been started. If
375# (1) it has been started by giving the full path and
376# that path is /etc/postinstall, OR
377# (2) Otherwise, if the environment variable
378# SSH_HOST_CONFIG_AUTO_ANSWER_NO is set
379# then set auto_answer to "no". This allows automatic
380# creation of the config files in /etc w/o overwriting
381# them if they already exist. In both cases, color
382# escape sequences are suppressed, so as to prevent
383# cluttering setup's logfiles.
384if [ "$PROGDIR" = "/etc/postinstall" ]
385then
386 csih_auto_answer="no"
387 csih_disable_color
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000388 opt_force=yes
Damien Miller1fc231c2008-07-14 12:12:52 +1000389fi
390if [ -n "${SSH_HOST_CONFIG_AUTO_ANSWER_NO}" ]
391then
392 csih_auto_answer="no"
393 csih_disable_color
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000394 opt_force=yes
Damien Miller1fc231c2008-07-14 12:12:52 +1000395fi
396
397# ======================================================================
398# Parse options
399# ======================================================================
Ben Lindstromb100ec92001-01-19 05:37:32 +0000400while :
401do
402 case $# in
403 0)
404 break
405 ;;
406 esac
407
408 option=$1
409 shift
410
Darren Tucker798ca842003-11-13 11:28:49 +1100411 case "${option}" in
Ben Lindstromb100ec92001-01-19 05:37:32 +0000412 -d | --debug )
413 set -x
Damien Miller1fc231c2008-07-14 12:12:52 +1000414 csih_trace_on
Ben Lindstromb100ec92001-01-19 05:37:32 +0000415 ;;
416
417 -y | --yes )
Damien Miller1fc231c2008-07-14 12:12:52 +1000418 csih_auto_answer=yes
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000419 opt_force=yes
Ben Lindstromb100ec92001-01-19 05:37:32 +0000420 ;;
421
422 -n | --no )
Damien Miller1fc231c2008-07-14 12:12:52 +1000423 csih_auto_answer=no
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000424 opt_force=yes
Ben Lindstromb100ec92001-01-19 05:37:32 +0000425 ;;
426
Darren Tucker798ca842003-11-13 11:28:49 +1100427 -c | --cygwin )
428 cygwin_value="$1"
429 shift
430 ;;
431
Damien Miller8ac0a7e2001-03-07 21:38:19 +1100432 -p | --port )
433 port_number=$1
434 shift
435 ;;
436
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000437 -u | --user )
438 user_account="$1"
439 shift
440 ;;
441
Darren Tucker798ca842003-11-13 11:28:49 +1100442 -w | --pwd )
443 password_value="$1"
444 shift
445 ;;
446
Damien Miller1fc231c2008-07-14 12:12:52 +1000447 --privileged )
448 csih_FORCE_PRIVILEGED_USER=yes
449 ;;
450
Ben Lindstromb100ec92001-01-19 05:37:32 +0000451 *)
452 echo "usage: ${progname} [OPTION]..."
453 echo
454 echo "This script creates an OpenSSH host configuration."
455 echo
456 echo "Options:"
Darren Tucker798ca842003-11-13 11:28:49 +1100457 echo " --debug -d Enable shell's debug output."
458 echo " --yes -y Answer all questions with \"yes\" automatically."
459 echo " --no -n Answer all questions with \"no\" automatically."
460 echo " --cygwin -c <options> Use \"options\" as value for CYGWIN environment var."
461 echo " --port -p <n> sshd listens on port n."
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000462 echo " --user -u <account> privileged user for service."
Damien Miller1fc231c2008-07-14 12:12:52 +1000463 echo " --pwd -w <passwd> Use \"pwd\" as password for privileged user."
464 echo " --privileged On Windows NT/2k/XP, require privileged user"
465 echo " instead of LocalSystem for sshd service."
Ben Lindstromb100ec92001-01-19 05:37:32 +0000466 echo
467 exit 1
468 ;;
469
470 esac
471done
472
Damien Miller1fc231c2008-07-14 12:12:52 +1000473# ======================================================================
474# Action!
475# ======================================================================
Ben Lindstrom6dbf3002002-07-03 23:33:19 +0000476
Kevin Steves9be6e262000-10-29 19:18:49 +0000477# Check for running ssh/sshd processes first. Refuse to do anything while
478# some ssh processes are still running
Darren Tucker83795d62008-12-01 21:34:28 +1100479if ps -ef | grep -q '/sshd\?$'
Kevin Steves9be6e262000-10-29 19:18:49 +0000480then
481 echo
Damien Miller1fc231c2008-07-14 12:12:52 +1000482 csih_error "There are still ssh processes running. Please shut them down first."
Kevin Steves9be6e262000-10-29 19:18:49 +0000483fi
484
485# Check for ${SYSCONFDIR} directory
Damien Miller1fc231c2008-07-14 12:12:52 +1000486csih_make_dir "${SYSCONFDIR}" "Cannot create global configuration files."
487chmod 775 "${SYSCONFDIR}"
488setfacl -m u:system:rwx "${SYSCONFDIR}"
Kevin Steves9be6e262000-10-29 19:18:49 +0000489
Damien Miller1fc231c2008-07-14 12:12:52 +1000490# Check for /var/log directory
491csih_make_dir "${LOCALSTATEDIR}/log" "Cannot create log directory."
492chmod 775 "${LOCALSTATEDIR}/log"
493setfacl -m u:system:rwx "${LOCALSTATEDIR}/log"
Kevin Steves9be6e262000-10-29 19:18:49 +0000494
Damien Miller1fc231c2008-07-14 12:12:52 +1000495# Create /var/log/lastlog if not already exists
Darren Tucker18614c22006-03-04 08:50:31 +1100496if [ -e ${LOCALSTATEDIR}/log/lastlog -a ! -f ${LOCALSTATEDIR}/log/lastlog ]
497then
Tim Rice0d8f2f32009-01-29 12:40:30 -0800498 echo
Damien Miller1fc231c2008-07-14 12:12:52 +1000499 csih_error_multi "${LOCALSTATEDIR}/log/lastlog exists, but is not a file." \
Tim Rice0d8f2f32009-01-29 12:40:30 -0800500 "Cannot create ssh host configuration."
Darren Tucker18614c22006-03-04 08:50:31 +1100501fi
502if [ ! -e ${LOCALSTATEDIR}/log/lastlog ]
503then
504 cat /dev/null > ${LOCALSTATEDIR}/log/lastlog
505 chmod 644 ${LOCALSTATEDIR}/log/lastlog
Ben Lindstrom6dbf3002002-07-03 23:33:19 +0000506fi
507
508# Create /var/empty file used as chroot jail for privilege separation
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000509csih_make_dir "${LOCALSTATEDIR}/empty" "Cannot create ${LOCALSTATEDIR}/empty directory."
Damien Miller1fc231c2008-07-14 12:12:52 +1000510chmod 755 "${LOCALSTATEDIR}/empty"
511setfacl -m u:system:rwx "${LOCALSTATEDIR}/empty"
512
513# host keys
514create_host_keys
515
516# use 'cmp' program to determine if a config file is identical
517# to the default version of that config file
518csih_check_program_or_error cmp diffutils
519
520
521# handle ssh_config
522csih_install_config "${SYSCONFDIR}/ssh_config" "${SYSCONFDIR}/defaults"
523if cmp "${SYSCONFDIR}/ssh_config" "${SYSCONFDIR}/defaults/${SYSCONFDIR}/ssh_config" >/dev/null 2>&1
Ben Lindstrom6dbf3002002-07-03 23:33:19 +0000524then
Darren Tucker798ca842003-11-13 11:28:49 +1100525 if [ "${port_number}" != "22" ]
Damien Miller8ac0a7e2001-03-07 21:38:19 +1100526 then
Damien Miller1fc231c2008-07-14 12:12:52 +1000527 csih_inform "Updating ${SYSCONFDIR}/ssh_config file with requested port"
Damien Miller8ac0a7e2001-03-07 21:38:19 +1100528 echo "Host localhost" >> ${SYSCONFDIR}/ssh_config
Darren Tucker798ca842003-11-13 11:28:49 +1100529 echo " Port ${port_number}" >> ${SYSCONFDIR}/ssh_config
Damien Miller8ac0a7e2001-03-07 21:38:19 +1100530 fi
Kevin Steves9be6e262000-10-29 19:18:49 +0000531fi
532
Damien Miller1fc231c2008-07-14 12:12:52 +1000533# handle sshd_config (and privsep)
534csih_install_config "${SYSCONFDIR}/sshd_config" "${SYSCONFDIR}/defaults"
535if ! cmp "${SYSCONFDIR}/sshd_config" "${SYSCONFDIR}/defaults/${SYSCONFDIR}/sshd_config" >/dev/null 2>&1
Kevin Steves9be6e262000-10-29 19:18:49 +0000536then
Damien Miller1fc231c2008-07-14 12:12:52 +1000537 grep -q UsePrivilegeSeparation ${SYSCONFDIR}/sshd_config && privsep_configured=yes
Kevin Steves9be6e262000-10-29 19:18:49 +0000538fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000539sshd_privsep
Kevin Steves9be6e262000-10-29 19:18:49 +0000540
Ben Lindstrom6dbf3002002-07-03 23:33:19 +0000541
Ben Lindstrom6dbf3002002-07-03 23:33:19 +0000542
Tim Rice0d8f2f32009-01-29 12:40:30 -0800543update_services_file
Damien Miller1fc231c2008-07-14 12:12:52 +1000544update_inetd_conf
545install_service
Ben Lindstroma5820292001-07-18 16:25:41 +0000546
Kevin Steves9be6e262000-10-29 19:18:49 +0000547echo
Damien Miller1fc231c2008-07-14 12:12:52 +1000548csih_inform "Host configuration finished. Have fun!"
549