3 # ssh-host-config, Copyright 2000, 2001, 2002, 2003 Red Hat Inc.
5 # This file is part of the Cygwin port of OpenSSH.
7 # ======================================================================
9 # ======================================================================
10 PROGNAME=$(basename $0)
12 PROGDIR=$(cd $_tdir && pwd)
14 CSIH_SCRIPT=/usr/share/csih/cygwin-service-installation-helper.sh
16 # Subdirectory where the new package is being installed
19 # Directory where the config files are stored
31 # ======================================================================
32 # Routine: create_host_keys
33 # ======================================================================
35 if [ ! -f "${SYSCONFDIR}/ssh_host_key" ]
37 csih_inform "Generating ${SYSCONFDIR}/ssh_host_key"
38 ssh-keygen -t rsa1 -f ${SYSCONFDIR}/ssh_host_key -N '' > /dev/null
41 if [ ! -f "${SYSCONFDIR}/ssh_host_rsa_key" ]
43 csih_inform "Generating ${SYSCONFDIR}/ssh_host_rsa_key"
44 ssh-keygen -t rsa -f ${SYSCONFDIR}/ssh_host_rsa_key -N '' > /dev/null
47 if [ ! -f "${SYSCONFDIR}/ssh_host_dsa_key" ]
49 csih_inform "Generating ${SYSCONFDIR}/ssh_host_dsa_key"
50 ssh-keygen -t dsa -f ${SYSCONFDIR}/ssh_host_dsa_key -N '' > /dev/null
52 } # --- End of create_host_keys --- #
54 # ======================================================================
55 # Routine: update_services_file
56 # ======================================================================
57 update_services_file() {
58 local _my_etcdir="/ssh-host-config.$$"
67 _win_etcdir="${SYSTEMROOT}\\system32\\drivers\\etc"
68 _services="${_my_etcdir}/services"
69 # On NT, 27 spaces, no space after the hash
72 _win_etcdir="${WINDIR}"
73 _services="${_my_etcdir}/SERVICES"
74 # On 9x, 18 spaces (95 is very touchy), a space after the hash
77 _serv_tmp="${_my_etcdir}/srv.out.$$"
79 mount -o text -f "${_win_etcdir}" "${_my_etcdir}"
81 # Depends on the above mount
82 _wservices=`cygpath -w "${_services}"`
84 # Remove sshd 22/port from services
85 if [ `grep -q 'sshd[ \t][ \t]*22' "${_services}"; echo $?` -eq 0 ]
87 grep -v 'sshd[ \t][ \t]*22' "${_services}" > "${_serv_tmp}"
88 if [ -f "${_serv_tmp}" ]
90 if mv "${_serv_tmp}" "${_services}"
92 csih_inform "Removing sshd from ${_wservices}"
94 csih_warning "Removing sshd from ${_wservices} failed!"
98 csih_warning "Removing sshd from ${_wservices} failed!"
102 # Add ssh 22/tcp and ssh 22/udp to services
103 if [ `grep -q 'ssh[ \t][ \t]*22' "${_services}"; echo $?` -ne 0 ]
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}"
107 if mv "${_serv_tmp}" "${_services}"
109 csih_inform "Added ssh to ${_wservices}"
111 csih_warning "Adding ssh to ${_wservices} failed!"
115 csih_warning "Adding ssh to ${_wservices} failed!"
118 umount "${_my_etcdir}"
119 } # --- End of update_services_file --- #
121 # ======================================================================
122 # Routine: sshd_privsep
123 # MODIFIES: privsep_configured privsep_used
124 # ======================================================================
128 if [ "${privsep_configured}" != "yes" ]
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?"
138 if ! csih_create_unprivileged_user sshd
140 csih_warning "Couldn't create user 'sshd'!"
141 csih_warning "Privilege separation set to 'no' again!"
142 csih_warning "Check your ${SYSCONFDIR}/sshd_config file!"
149 # On 9x don't use privilege separation. Since security isn't
150 # available it just adds useless additional processes.
155 # 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
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/" \
164 < ${SYSCONFDIR}/sshd_config \
165 > "${sshdconfig_tmp}"
166 mv "${sshdconfig_tmp}" ${SYSCONFDIR}/sshd_config
167 elif [ "${privsep_configured}" != "yes" ]
169 echo >> ${SYSCONFDIR}/sshd_config
170 echo "UsePrivilegeSeparation ${privsep_used}" >> ${SYSCONFDIR}/sshd_config
172 } # --- End of sshd_privsep --- #
174 # ======================================================================
175 # Routine: update_inetd_conf
176 # ======================================================================
177 update_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
185 if [ -d "${_inetcnf_dir}" ]
187 # we have inetutils-1.5 inetd.d support
188 if [ -f "${_inetcnf}" ]
190 grep -q '^[ \t]*ssh' "${_inetcnf}" && _with_comment=0
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 ]
196 grep -v '^[# \t]*ssh' "${_inetcnf}" >> "${_inetcnf_tmp}"
197 if [ -f "${_inetcnf_tmp}" ]
199 if mv "${_inetcnf_tmp}" "${_inetcnf}"
201 csih_inform "Removed ssh[d] from ${_inetcnf}"
203 csih_warning "Removing ssh[d] from ${_inetcnf} failed!"
205 rm -f "${_inetcnf_tmp}"
207 csih_warning "Removing ssh[d] from ${_inetcnf} failed!"
212 csih_install_config "${_sshd_inetd_conf}" "${SYSCONFDIR}/defaults"
213 if cmp "${SYSCONFDIR}/defaults${_sshd_inetd_conf}" "${_sshd_inetd_conf}" >/dev/null 2>&1
215 if [ "${_with_comment}" -eq 0 ]
217 sed -e 's/@COMMENT@[ \t]*//' < "${_sshd_inetd_conf}" > "${_sshd_inetd_conf_tmp}"
219 sed -e 's/@COMMENT@[ \t]*/# /' < "${_sshd_inetd_conf}" > "${_sshd_inetd_conf_tmp}"
221 mv "${_sshd_inetd_conf_tmp}" "${_sshd_inetd_conf}"
222 csih_inform "Updated ${_sshd_inetd_conf}"
225 elif [ -f "${_inetcnf}" ]
227 grep -q '^[ \t]*sshd' "${_inetcnf}" && _with_comment=0
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 ]
233 grep -v '^[# \t]*sshd' "${_inetcnf}" >> "${_inetcnf_tmp}"
234 if [ -f "${_inetcnf_tmp}" ]
236 if mv "${_inetcnf_tmp}" "${_inetcnf}"
238 csih_inform "Removed sshd from ${_inetcnf}"
240 csih_warning "Removing sshd from ${_inetcnf} failed!"
242 rm -f "${_inetcnf_tmp}"
244 csih_warning "Removing sshd from ${_inetcnf} failed!"
248 # Add ssh line to inetd.conf
249 if [ `grep -q '^[# \t]*ssh' "${_inetcnf}"; echo $?` -ne 0 ]
251 if [ "${_with_comment}" -eq 0 ]
253 echo 'ssh stream tcp nowait root /usr/sbin/sshd sshd -i' >> "${_inetcnf}"
255 echo '# ssh stream tcp nowait root /usr/sbin/sshd sshd -i' >> "${_inetcnf}"
257 csih_inform "Added ssh to ${_inetcnf}"
260 } # --- End of update_inetd_conf --- #
262 # ======================================================================
263 # Routine: install_service
264 # Install sshd as a service
265 # ======================================================================
272 if ! cygrunsrv -Q sshd >/dev/null 2>&1
276 csih_warning "The following functions require administrator privileges!"
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)"
281 csih_get_cygenv "${cygwin_value}"
283 if ( csih_is_nt2003 || [ "$csih_FORCE_PRIVILEGED_USER" = "yes" ] )
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."
290 if ! csih_create_privileged_user "${password_value}"
292 csih_error_recoverable "There was a serious problem creating a privileged user."
293 csih_request "Do you want to proceed anyway?" || exit 1
297 # never returns empty if NT or above
298 run_service_as=$(csih_service_should_run_as)
300 if [ "${run_service_as}" = "${csih_PRIVILEGED_USERNAME}" ]
302 password="${csih_PRIVILEGED_PASSWORD}"
303 if [ -z "${password}" ]
305 csih_get_value "Please enter the password for user '${run_service_as}':" "-s"
306 password="${csih_value}"
310 # 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
315 csih_check_user "${run_service_as}"
317 if [ -n "${csih_cygenv}" ]
319 cygwin_env="-e CYGWIN=\"${csih_cygenv}\""
321 if [ -z "${password}" ]
323 if eval cygrunsrv -I sshd -d \"CYGWIN sshd\" -p /usr/sbin/sshd \
324 -a "-D" -y tcpip ${cygwin_env}
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."
333 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}"
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"
340 csih_inform "\`cygrunsrv -S sshd'. Otherwise, it will start automatically"
341 csih_inform "after the next reboot."
345 # now, if successfully installed, set ownership of the affected files
346 if cygrunsrv -Q sshd >/dev/null 2>&1
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 ]
353 chown "${run_service_as}".544 ${LOCALSTATEDIR}/log/sshd.log
356 csih_warning "Something went wrong installing the sshd service."
358 fi # user allowed us to install as service
359 fi # service not yet installed
361 } # --- End of install_service --- #
363 # ======================================================================
365 # ======================================================================
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.
377 if [ "$PROGDIR" = "/etc/postinstall" ]
379 csih_auto_answer="no"
382 if [ -n "${SSH_HOST_CONFIG_AUTO_ANSWER_NO}" ]
384 csih_auto_answer="no"
388 # ======================================================================
390 # ======================================================================
432 csih_FORCE_PRIVILEGED_USER=yes
436 echo "usage: ${progname} [OPTION]..."
438 echo "This script creates an OpenSSH host configuration."
441 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."
446 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."
456 # ======================================================================
458 # ======================================================================
460 # Check for running ssh/sshd processes first. Refuse to do anything while
461 # some ssh processes are still running
462 if ps -ef | grep -q '/sshd\?$'
465 csih_error "There are still ssh processes running. Please shut them down first."
468 # Check for ${SYSCONFDIR} directory
469 csih_make_dir "${SYSCONFDIR}" "Cannot create global configuration files."
470 chmod 775 "${SYSCONFDIR}"
471 setfacl -m u:system:rwx "${SYSCONFDIR}"
473 # Check for /var/log directory
474 csih_make_dir "${LOCALSTATEDIR}/log" "Cannot create log directory."
475 chmod 775 "${LOCALSTATEDIR}/log"
476 setfacl -m u:system:rwx "${LOCALSTATEDIR}/log"
478 # Create /var/log/lastlog if not already exists
479 if [ -e ${LOCALSTATEDIR}/log/lastlog -a ! -f ${LOCALSTATEDIR}/log/lastlog ]
482 csih_error_multi "${LOCALSTATEDIR}/log/lastlog exists, but is not a file." \
483 "Cannot create ssh host configuration."
485 if [ ! -e ${LOCALSTATEDIR}/log/lastlog ]
487 cat /dev/null > ${LOCALSTATEDIR}/log/lastlog
488 chmod 644 ${LOCALSTATEDIR}/log/lastlog
491 # Create /var/empty file used as chroot jail for privilege separation
492 csih_make_dir "${LOCALSTATEDIR}/empty" "Cannot create log directory."
493 chmod 755 "${LOCALSTATEDIR}/empty"
494 setfacl -m u:system:rwx "${LOCALSTATEDIR}/empty"
499 # use 'cmp' program to determine if a config file is identical
500 # to the default version of that config file
501 csih_check_program_or_error cmp diffutils
505 csih_install_config "${SYSCONFDIR}/ssh_config" "${SYSCONFDIR}/defaults"
506 if cmp "${SYSCONFDIR}/ssh_config" "${SYSCONFDIR}/defaults/${SYSCONFDIR}/ssh_config" >/dev/null 2>&1
508 if [ "${port_number}" != "22" ]
510 csih_inform "Updating ${SYSCONFDIR}/ssh_config file with requested port"
511 echo "Host localhost" >> ${SYSCONFDIR}/ssh_config
512 echo " Port ${port_number}" >> ${SYSCONFDIR}/ssh_config
516 # handle sshd_config (and privsep)
517 csih_install_config "${SYSCONFDIR}/sshd_config" "${SYSCONFDIR}/defaults"
518 if ! cmp "${SYSCONFDIR}/sshd_config" "${SYSCONFDIR}/defaults/${SYSCONFDIR}/sshd_config" >/dev/null 2>&1
520 grep -q UsePrivilegeSeparation ${SYSCONFDIR}/sshd_config && privsep_configured=yes
531 csih_inform "Host configuration finished. Have fun!"