blob: d968d4619d54afb542e4fd62a2924a5579773222 [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
66} # --- End of create_host_keys --- #
67
68# ======================================================================
69# Routine: update_services_file
70# ======================================================================
71update_services_file() {
72 local _my_etcdir="/ssh-host-config.$$"
73 local _win_etcdir
74 local _services
75 local _spaces
76 local _serv_tmp
77 local _wservices
78
79 if csih_is_nt
80 then
81 _win_etcdir="${SYSTEMROOT}\\system32\\drivers\\etc"
82 _services="${_my_etcdir}/services"
83 # On NT, 27 spaces, no space after the hash
84 _spaces=" #"
Kevin Steves9be6e262000-10-29 19:18:49 +000085 else
Damien Miller1fc231c2008-07-14 12:12:52 +100086 _win_etcdir="${WINDIR}"
87 _services="${_my_etcdir}/SERVICES"
88 # On 9x, 18 spaces (95 is very touchy), a space after the hash
89 _spaces=" # "
Kevin Steves9be6e262000-10-29 19:18:49 +000090 fi
Damien Miller1fc231c2008-07-14 12:12:52 +100091 _serv_tmp="${_my_etcdir}/srv.out.$$"
Tim Rice0d8f2f32009-01-29 12:40:30 -080092
Darren Tucker62131dc2010-03-24 13:03:32 +110093 mount -o text,posix=0,noacl -f "${_win_etcdir}" "${_my_etcdir}"
Tim Rice0d8f2f32009-01-29 12:40:30 -080094
Damien Miller1fc231c2008-07-14 12:12:52 +100095 # Depends on the above mount
96 _wservices=`cygpath -w "${_services}"`
Tim Rice0d8f2f32009-01-29 12:40:30 -080097
Damien Miller1fc231c2008-07-14 12:12:52 +100098 # Remove sshd 22/port from services
99 if [ `grep -q 'sshd[ \t][ \t]*22' "${_services}"; echo $?` -eq 0 ]
100 then
101 grep -v 'sshd[ \t][ \t]*22' "${_services}" > "${_serv_tmp}"
102 if [ -f "${_serv_tmp}" ]
103 then
104 if mv "${_serv_tmp}" "${_services}"
105 then
Tim Rice0d8f2f32009-01-29 12:40:30 -0800106 csih_inform "Removing sshd from ${_wservices}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000107 else
Tim Rice0d8f2f32009-01-29 12:40:30 -0800108 csih_warning "Removing sshd from ${_wservices} failed!"
Damien Miller1fc231c2008-07-14 12:12:52 +1000109 fi
110 rm -f "${_serv_tmp}"
111 else
112 csih_warning "Removing sshd from ${_wservices} failed!"
113 fi
114 fi
Tim Rice0d8f2f32009-01-29 12:40:30 -0800115
Damien Miller1fc231c2008-07-14 12:12:52 +1000116 # Add ssh 22/tcp and ssh 22/udp to services
117 if [ `grep -q 'ssh[ \t][ \t]*22' "${_services}"; echo $?` -ne 0 ]
118 then
119 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}"
120 then
121 if mv "${_serv_tmp}" "${_services}"
122 then
Tim Rice0d8f2f32009-01-29 12:40:30 -0800123 csih_inform "Added ssh to ${_wservices}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000124 else
Tim Rice0d8f2f32009-01-29 12:40:30 -0800125 csih_warning "Adding ssh to ${_wservices} failed!"
Damien Miller1fc231c2008-07-14 12:12:52 +1000126 fi
127 rm -f "${_serv_tmp}"
128 else
129 csih_warning "Adding ssh to ${_wservices} failed!"
130 fi
131 fi
132 umount "${_my_etcdir}"
133} # --- End of update_services_file --- #
Kevin Steves9be6e262000-10-29 19:18:49 +0000134
Damien Miller1fc231c2008-07-14 12:12:52 +1000135# ======================================================================
136# Routine: sshd_privsep
137# MODIFIES: privsep_configured privsep_used
138# ======================================================================
139sshd_privsep() {
140 local sshdconfig_tmp
Ben Lindstromb100ec92001-01-19 05:37:32 +0000141
Damien Miller1fc231c2008-07-14 12:12:52 +1000142 if [ "${privsep_configured}" != "yes" ]
143 then
144 if csih_is_nt
145 then
146 csih_inform "Privilege separation is set to yes by default since OpenSSH 3.3."
147 csih_inform "However, this requires a non-privileged account called 'sshd'."
148 csih_inform "For more info on privilege separation read /usr/share/doc/openssh/README.privsep."
149 if csih_request "Should privilege separation be used?"
150 then
Tim Rice0d8f2f32009-01-29 12:40:30 -0800151 privsep_used=yes
152 if ! csih_create_unprivileged_user sshd
153 then
Damien Miller1fc231c2008-07-14 12:12:52 +1000154 csih_warning "Couldn't create user 'sshd'!"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800155 csih_warning "Privilege separation set to 'no' again!"
156 csih_warning "Check your ${SYSCONFDIR}/sshd_config file!"
Damien Miller1fc231c2008-07-14 12:12:52 +1000157 privsep_used=no
Tim Rice0d8f2f32009-01-29 12:40:30 -0800158 fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000159 else
Tim Rice0d8f2f32009-01-29 12:40:30 -0800160 privsep_used=no
Damien Miller1fc231c2008-07-14 12:12:52 +1000161 fi
162 else
163 # On 9x don't use privilege separation. Since security isn't
164 # available it just adds useless additional processes.
165 privsep_used=no
166 fi
167 fi
Tim Rice0d8f2f32009-01-29 12:40:30 -0800168
Damien Miller1fc231c2008-07-14 12:12:52 +1000169 # Create default sshd_config from skeleton files in /etc/defaults/etc or
170 # modify to add the missing privsep configuration option
171 if cmp "${SYSCONFDIR}/sshd_config" "${SYSCONFDIR}/defaults/${SYSCONFDIR}/sshd_config" >/dev/null 2>&1
172 then
173 csih_inform "Updating ${SYSCONFDIR}/sshd_config file"
174 sshdconfig_tmp=${SYSCONFDIR}/sshd_config.$$
175 sed -e "s/^#UsePrivilegeSeparation yes/UsePrivilegeSeparation ${privsep_used}/
176 s/^#Port 22/Port ${port_number}/
177 s/^#StrictModes yes/StrictModes no/" \
Tim Rice0d8f2f32009-01-29 12:40:30 -0800178 < ${SYSCONFDIR}/sshd_config \
179 > "${sshdconfig_tmp}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000180 mv "${sshdconfig_tmp}" ${SYSCONFDIR}/sshd_config
181 elif [ "${privsep_configured}" != "yes" ]
182 then
183 echo >> ${SYSCONFDIR}/sshd_config
184 echo "UsePrivilegeSeparation ${privsep_used}" >> ${SYSCONFDIR}/sshd_config
185 fi
186} # --- End of sshd_privsep --- #
187
188# ======================================================================
189# Routine: update_inetd_conf
190# ======================================================================
191update_inetd_conf() {
192 local _inetcnf="${SYSCONFDIR}/inetd.conf"
193 local _inetcnf_tmp="${SYSCONFDIR}/inetd.conf.$$"
194 local _inetcnf_dir="${SYSCONFDIR}/inetd.d"
195 local _sshd_inetd_conf="${_inetcnf_dir}/sshd-inetd"
196 local _sshd_inetd_conf_tmp="${_inetcnf_dir}/sshd-inetd.$$"
197 local _with_comment=1
198
199 if [ -d "${_inetcnf_dir}" ]
200 then
201 # we have inetutils-1.5 inetd.d support
202 if [ -f "${_inetcnf}" ]
203 then
204 grep -q '^[ \t]*ssh' "${_inetcnf}" && _with_comment=0
205
206 # check for sshd OR ssh in top-level inetd.conf file, and remove
207 # will be replaced by a file in inetd.d/
208 if [ `grep -q '^[# \t]*ssh' "${_inetcnf}"; echo $?` -eq 0 ]
209 then
Tim Rice0d8f2f32009-01-29 12:40:30 -0800210 grep -v '^[# \t]*ssh' "${_inetcnf}" >> "${_inetcnf_tmp}"
211 if [ -f "${_inetcnf_tmp}" ]
212 then
213 if mv "${_inetcnf_tmp}" "${_inetcnf}"
214 then
Damien Miller1fc231c2008-07-14 12:12:52 +1000215 csih_inform "Removed ssh[d] from ${_inetcnf}"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800216 else
Damien Miller1fc231c2008-07-14 12:12:52 +1000217 csih_warning "Removing ssh[d] from ${_inetcnf} failed!"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800218 fi
219 rm -f "${_inetcnf_tmp}"
220 else
221 csih_warning "Removing ssh[d] from ${_inetcnf} failed!"
222 fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000223 fi
224 fi
225
226 csih_install_config "${_sshd_inetd_conf}" "${SYSCONFDIR}/defaults"
227 if cmp "${SYSCONFDIR}/defaults${_sshd_inetd_conf}" "${_sshd_inetd_conf}" >/dev/null 2>&1
228 then
229 if [ "${_with_comment}" -eq 0 ]
230 then
Tim Rice0d8f2f32009-01-29 12:40:30 -0800231 sed -e 's/@COMMENT@[ \t]*//' < "${_sshd_inetd_conf}" > "${_sshd_inetd_conf_tmp}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000232 else
Tim Rice0d8f2f32009-01-29 12:40:30 -0800233 sed -e 's/@COMMENT@[ \t]*/# /' < "${_sshd_inetd_conf}" > "${_sshd_inetd_conf_tmp}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000234 fi
235 mv "${_sshd_inetd_conf_tmp}" "${_sshd_inetd_conf}"
236 csih_inform "Updated ${_sshd_inetd_conf}"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800237 fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000238
239 elif [ -f "${_inetcnf}" ]
240 then
241 grep -q '^[ \t]*sshd' "${_inetcnf}" && _with_comment=0
242
243 # check for sshd in top-level inetd.conf file, and remove
244 # will be replaced by a file in inetd.d/
245 if [ `grep -q '^[# \t]*sshd' "${_inetcnf}"; echo $?` -eq 0 ]
246 then
247 grep -v '^[# \t]*sshd' "${_inetcnf}" >> "${_inetcnf_tmp}"
248 if [ -f "${_inetcnf_tmp}" ]
249 then
Tim Rice0d8f2f32009-01-29 12:40:30 -0800250 if mv "${_inetcnf_tmp}" "${_inetcnf}"
251 then
Damien Miller1fc231c2008-07-14 12:12:52 +1000252 csih_inform "Removed sshd from ${_inetcnf}"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800253 else
Damien Miller1fc231c2008-07-14 12:12:52 +1000254 csih_warning "Removing sshd from ${_inetcnf} failed!"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800255 fi
256 rm -f "${_inetcnf_tmp}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000257 else
Tim Rice0d8f2f32009-01-29 12:40:30 -0800258 csih_warning "Removing sshd from ${_inetcnf} failed!"
Damien Miller1fc231c2008-07-14 12:12:52 +1000259 fi
260 fi
Tim Rice0d8f2f32009-01-29 12:40:30 -0800261
Damien Miller1fc231c2008-07-14 12:12:52 +1000262 # Add ssh line to inetd.conf
263 if [ `grep -q '^[# \t]*ssh' "${_inetcnf}"; echo $?` -ne 0 ]
264 then
265 if [ "${_with_comment}" -eq 0 ]
266 then
Tim Rice0d8f2f32009-01-29 12:40:30 -0800267 echo 'ssh stream tcp nowait root /usr/sbin/sshd sshd -i' >> "${_inetcnf}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000268 else
Tim Rice0d8f2f32009-01-29 12:40:30 -0800269 echo '# ssh stream tcp nowait root /usr/sbin/sshd sshd -i' >> "${_inetcnf}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000270 fi
271 csih_inform "Added ssh to ${_inetcnf}"
272 fi
273 fi
274} # --- End of update_inetd_conf --- #
275
276# ======================================================================
277# Routine: install_service
278# Install sshd as a service
279# ======================================================================
280install_service() {
281 local run_service_as
282 local password
283
284 if csih_is_nt
285 then
286 if ! cygrunsrv -Q sshd >/dev/null 2>&1
287 then
288 echo
289 echo
290 csih_warning "The following functions require administrator privileges!"
291 echo
292 echo -e "${_csih_QUERY_STR} Do you want to install sshd as a service?"
293 if csih_request "(Say \"no\" if it is already installed as a service)"
294 then
Tim Rice0d8f2f32009-01-29 12:40:30 -0800295 csih_get_cygenv "${cygwin_value}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000296
Tim Rice0d8f2f32009-01-29 12:40:30 -0800297 if ( csih_is_nt2003 || [ "$csih_FORCE_PRIVILEGED_USER" = "yes" ] )
298 then
299 csih_inform "On Windows Server 2003, Windows Vista, and above, the"
300 csih_inform "SYSTEM account cannot setuid to other users -- a capability"
301 csih_inform "sshd requires. You need to have or to create a privileged"
302 csih_inform "account. This script will help you do so."
303 echo
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000304
305 [ "${opt_force}" = "yes" ] && opt_f=-f
306 [ -n "${user_account}" ] && opt_u="-u ""${user_account}"""
307 csih_select_privileged_username ${opt_f} ${opt_u} sshd
308
Tim Rice0d8f2f32009-01-29 12:40:30 -0800309 if ! csih_create_privileged_user "${password_value}"
310 then
311 csih_error_recoverable "There was a serious problem creating a privileged user."
312 csih_request "Do you want to proceed anyway?" || exit 1
313 fi
314 fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000315
Tim Rice0d8f2f32009-01-29 12:40:30 -0800316 # never returns empty if NT or above
317 run_service_as=$(csih_service_should_run_as)
Damien Miller1fc231c2008-07-14 12:12:52 +1000318
Tim Rice0d8f2f32009-01-29 12:40:30 -0800319 if [ "${run_service_as}" = "${csih_PRIVILEGED_USERNAME}" ]
320 then
321 password="${csih_PRIVILEGED_PASSWORD}"
322 if [ -z "${password}" ]
323 then
324 csih_get_value "Please enter the password for user '${run_service_as}':" "-s"
325 password="${csih_value}"
326 fi
327 fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000328
Tim Rice0d8f2f32009-01-29 12:40:30 -0800329 # at this point, we either have $run_service_as = "system" and $password is empty,
330 # or $run_service_as is some privileged user and (hopefully) $password contains
331 # the correct password. So, from here out, we use '-z "${password}"' to discriminate
332 # the two cases.
Damien Miller1fc231c2008-07-14 12:12:52 +1000333
Tim Rice0d8f2f32009-01-29 12:40:30 -0800334 csih_check_user "${run_service_as}"
Damien Miller1fc231c2008-07-14 12:12:52 +1000335
Tim Rice6a325342009-01-29 12:30:01 -0800336 if [ -n "${csih_cygenv}" ]
337 then
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000338 cygwin_env=( -e "CYGWIN=${csih_cygenv}" )
Tim Rice6a325342009-01-29 12:30:01 -0800339 fi
Tim Rice0d8f2f32009-01-29 12:40:30 -0800340 if [ -z "${password}" ]
341 then
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000342 if cygrunsrv -I sshd -d "CYGWIN sshd" -p /usr/sbin/sshd \
343 -a "-D" -y tcpip "${cygwin_env[@]}"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800344 then
345 echo
346 csih_inform "The sshd service has been installed under the LocalSystem"
347 csih_inform "account (also known as SYSTEM). To start the service now, call"
348 csih_inform "\`net start sshd' or \`cygrunsrv -S sshd'. Otherwise, it"
349 csih_inform "will start automatically after the next reboot."
350 fi
351 else
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000352 if cygrunsrv -I sshd -d "CYGWIN sshd" -p /usr/sbin/sshd \
353 -a "-D" -y tcpip "${cygwin_env[@]}" \
Tim Rice6a325342009-01-29 12:30:01 -0800354 -u "${run_service_as}" -w "${password}"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800355 then
Damien Miller1fc231c2008-07-14 12:12:52 +1000356 echo
357 csih_inform "The sshd service has been installed under the '${run_service_as}'"
358 csih_inform "account. To start the service now, call \`net start sshd' or"
Tim Rice0d8f2f32009-01-29 12:40:30 -0800359 csih_inform "\`cygrunsrv -S sshd'. Otherwise, it will start automatically"
360 csih_inform "after the next reboot."
361 fi
362 fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000363
Tim Rice0d8f2f32009-01-29 12:40:30 -0800364 # now, if successfully installed, set ownership of the affected files
365 if cygrunsrv -Q sshd >/dev/null 2>&1
366 then
367 chown "${run_service_as}" ${SYSCONFDIR}/ssh*
368 chown "${run_service_as}".544 ${LOCALSTATEDIR}/empty
369 chown "${run_service_as}".544 ${LOCALSTATEDIR}/log/lastlog
370 if [ -f ${LOCALSTATEDIR}/log/sshd.log ]
371 then
Damien Miller1fc231c2008-07-14 12:12:52 +1000372 chown "${run_service_as}".544 ${LOCALSTATEDIR}/log/sshd.log
Tim Rice0d8f2f32009-01-29 12:40:30 -0800373 fi
374 else
375 csih_warning "Something went wrong installing the sshd service."
376 fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000377 fi # user allowed us to install as service
378 fi # service not yet installed
379 fi # csih_is_nt
380} # --- End of install_service --- #
381
382# ======================================================================
383# Main Entry Point
384# ======================================================================
385
386# Check how the script has been started. If
387# (1) it has been started by giving the full path and
388# that path is /etc/postinstall, OR
389# (2) Otherwise, if the environment variable
390# SSH_HOST_CONFIG_AUTO_ANSWER_NO is set
391# then set auto_answer to "no". This allows automatic
392# creation of the config files in /etc w/o overwriting
393# them if they already exist. In both cases, color
394# escape sequences are suppressed, so as to prevent
395# cluttering setup's logfiles.
396if [ "$PROGDIR" = "/etc/postinstall" ]
397then
398 csih_auto_answer="no"
399 csih_disable_color
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000400 opt_force=yes
Damien Miller1fc231c2008-07-14 12:12:52 +1000401fi
402if [ -n "${SSH_HOST_CONFIG_AUTO_ANSWER_NO}" ]
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
408
409# ======================================================================
410# Parse options
411# ======================================================================
Ben Lindstromb100ec92001-01-19 05:37:32 +0000412while :
413do
414 case $# in
415 0)
416 break
417 ;;
418 esac
419
420 option=$1
421 shift
422
Darren Tucker798ca842003-11-13 11:28:49 +1100423 case "${option}" in
Ben Lindstromb100ec92001-01-19 05:37:32 +0000424 -d | --debug )
425 set -x
Damien Miller1fc231c2008-07-14 12:12:52 +1000426 csih_trace_on
Ben Lindstromb100ec92001-01-19 05:37:32 +0000427 ;;
428
429 -y | --yes )
Damien Miller1fc231c2008-07-14 12:12:52 +1000430 csih_auto_answer=yes
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000431 opt_force=yes
Ben Lindstromb100ec92001-01-19 05:37:32 +0000432 ;;
433
434 -n | --no )
Damien Miller1fc231c2008-07-14 12:12:52 +1000435 csih_auto_answer=no
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000436 opt_force=yes
Ben Lindstromb100ec92001-01-19 05:37:32 +0000437 ;;
438
Darren Tucker798ca842003-11-13 11:28:49 +1100439 -c | --cygwin )
440 cygwin_value="$1"
441 shift
442 ;;
443
Damien Miller8ac0a7e2001-03-07 21:38:19 +1100444 -p | --port )
445 port_number=$1
446 shift
447 ;;
448
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000449 -u | --user )
450 user_account="$1"
451 shift
452 ;;
453
Darren Tucker798ca842003-11-13 11:28:49 +1100454 -w | --pwd )
455 password_value="$1"
456 shift
457 ;;
458
Damien Miller1fc231c2008-07-14 12:12:52 +1000459 --privileged )
460 csih_FORCE_PRIVILEGED_USER=yes
461 ;;
462
Ben Lindstromb100ec92001-01-19 05:37:32 +0000463 *)
464 echo "usage: ${progname} [OPTION]..."
465 echo
466 echo "This script creates an OpenSSH host configuration."
467 echo
468 echo "Options:"
Darren Tucker798ca842003-11-13 11:28:49 +1100469 echo " --debug -d Enable shell's debug output."
470 echo " --yes -y Answer all questions with \"yes\" automatically."
471 echo " --no -n Answer all questions with \"no\" automatically."
472 echo " --cygwin -c <options> Use \"options\" as value for CYGWIN environment var."
473 echo " --port -p <n> sshd listens on port n."
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000474 echo " --user -u <account> privileged user for service."
Damien Miller1fc231c2008-07-14 12:12:52 +1000475 echo " --pwd -w <passwd> Use \"pwd\" as password for privileged user."
476 echo " --privileged On Windows NT/2k/XP, require privileged user"
477 echo " instead of LocalSystem for sshd service."
Ben Lindstromb100ec92001-01-19 05:37:32 +0000478 echo
479 exit 1
480 ;;
481
482 esac
483done
484
Damien Miller1fc231c2008-07-14 12:12:52 +1000485# ======================================================================
486# Action!
487# ======================================================================
Ben Lindstrom6dbf3002002-07-03 23:33:19 +0000488
Kevin Steves9be6e262000-10-29 19:18:49 +0000489# Check for running ssh/sshd processes first. Refuse to do anything while
490# some ssh processes are still running
Darren Tucker83795d62008-12-01 21:34:28 +1100491if ps -ef | grep -q '/sshd\?$'
Kevin Steves9be6e262000-10-29 19:18:49 +0000492then
493 echo
Damien Miller1fc231c2008-07-14 12:12:52 +1000494 csih_error "There are still ssh processes running. Please shut them down first."
Kevin Steves9be6e262000-10-29 19:18:49 +0000495fi
496
497# Check for ${SYSCONFDIR} directory
Damien Miller1fc231c2008-07-14 12:12:52 +1000498csih_make_dir "${SYSCONFDIR}" "Cannot create global configuration files."
499chmod 775 "${SYSCONFDIR}"
500setfacl -m u:system:rwx "${SYSCONFDIR}"
Kevin Steves9be6e262000-10-29 19:18:49 +0000501
Damien Miller1fc231c2008-07-14 12:12:52 +1000502# Check for /var/log directory
503csih_make_dir "${LOCALSTATEDIR}/log" "Cannot create log directory."
504chmod 775 "${LOCALSTATEDIR}/log"
505setfacl -m u:system:rwx "${LOCALSTATEDIR}/log"
Kevin Steves9be6e262000-10-29 19:18:49 +0000506
Damien Miller1fc231c2008-07-14 12:12:52 +1000507# Create /var/log/lastlog if not already exists
Darren Tucker18614c22006-03-04 08:50:31 +1100508if [ -e ${LOCALSTATEDIR}/log/lastlog -a ! -f ${LOCALSTATEDIR}/log/lastlog ]
509then
Tim Rice0d8f2f32009-01-29 12:40:30 -0800510 echo
Damien Miller1fc231c2008-07-14 12:12:52 +1000511 csih_error_multi "${LOCALSTATEDIR}/log/lastlog exists, but is not a file." \
Tim Rice0d8f2f32009-01-29 12:40:30 -0800512 "Cannot create ssh host configuration."
Darren Tucker18614c22006-03-04 08:50:31 +1100513fi
514if [ ! -e ${LOCALSTATEDIR}/log/lastlog ]
515then
516 cat /dev/null > ${LOCALSTATEDIR}/log/lastlog
517 chmod 644 ${LOCALSTATEDIR}/log/lastlog
Ben Lindstrom6dbf3002002-07-03 23:33:19 +0000518fi
519
520# Create /var/empty file used as chroot jail for privilege separation
Darren Tucker4d4fdc02009-07-07 21:19:11 +1000521csih_make_dir "${LOCALSTATEDIR}/empty" "Cannot create ${LOCALSTATEDIR}/empty directory."
Damien Miller1fc231c2008-07-14 12:12:52 +1000522chmod 755 "${LOCALSTATEDIR}/empty"
523setfacl -m u:system:rwx "${LOCALSTATEDIR}/empty"
524
525# host keys
526create_host_keys
527
528# use 'cmp' program to determine if a config file is identical
529# to the default version of that config file
530csih_check_program_or_error cmp diffutils
531
532
533# handle ssh_config
534csih_install_config "${SYSCONFDIR}/ssh_config" "${SYSCONFDIR}/defaults"
535if cmp "${SYSCONFDIR}/ssh_config" "${SYSCONFDIR}/defaults/${SYSCONFDIR}/ssh_config" >/dev/null 2>&1
Ben Lindstrom6dbf3002002-07-03 23:33:19 +0000536then
Darren Tucker798ca842003-11-13 11:28:49 +1100537 if [ "${port_number}" != "22" ]
Damien Miller8ac0a7e2001-03-07 21:38:19 +1100538 then
Damien Miller1fc231c2008-07-14 12:12:52 +1000539 csih_inform "Updating ${SYSCONFDIR}/ssh_config file with requested port"
Damien Miller8ac0a7e2001-03-07 21:38:19 +1100540 echo "Host localhost" >> ${SYSCONFDIR}/ssh_config
Darren Tucker798ca842003-11-13 11:28:49 +1100541 echo " Port ${port_number}" >> ${SYSCONFDIR}/ssh_config
Damien Miller8ac0a7e2001-03-07 21:38:19 +1100542 fi
Kevin Steves9be6e262000-10-29 19:18:49 +0000543fi
544
Damien Miller1fc231c2008-07-14 12:12:52 +1000545# handle sshd_config (and privsep)
546csih_install_config "${SYSCONFDIR}/sshd_config" "${SYSCONFDIR}/defaults"
547if ! cmp "${SYSCONFDIR}/sshd_config" "${SYSCONFDIR}/defaults/${SYSCONFDIR}/sshd_config" >/dev/null 2>&1
Kevin Steves9be6e262000-10-29 19:18:49 +0000548then
Damien Miller1fc231c2008-07-14 12:12:52 +1000549 grep -q UsePrivilegeSeparation ${SYSCONFDIR}/sshd_config && privsep_configured=yes
Kevin Steves9be6e262000-10-29 19:18:49 +0000550fi
Damien Miller1fc231c2008-07-14 12:12:52 +1000551sshd_privsep
Kevin Steves9be6e262000-10-29 19:18:49 +0000552
Ben Lindstrom6dbf3002002-07-03 23:33:19 +0000553
Ben Lindstrom6dbf3002002-07-03 23:33:19 +0000554
Tim Rice0d8f2f32009-01-29 12:40:30 -0800555update_services_file
Damien Miller1fc231c2008-07-14 12:12:52 +1000556update_inetd_conf
557install_service
Ben Lindstroma5820292001-07-18 16:25:41 +0000558
Kevin Steves9be6e262000-10-29 19:18:49 +0000559echo
Damien Miller1fc231c2008-07-14 12:12:52 +1000560csih_inform "Host configuration finished. Have fun!"
561