1180740Sdes#!/bin/bash
2180740Sdes#
3294332Sdes# ssh-host-config, Copyright 2000-2014 Red Hat Inc.
4180740Sdes#
5180740Sdes# This file is part of the Cygwin port of OpenSSH.
6197670Sdes#
7197670Sdes# Permission to use, copy, modify, and distribute this software for any
8197670Sdes# purpose with or without fee is hereby granted, provided that the above
9197670Sdes# copyright notice and this permission notice appear in all copies.
10197670Sdes#
11197670Sdes# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  
12197670Sdes# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               
13197670Sdes# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   
14197670Sdes# IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   
15197670Sdes# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    
16197670Sdes# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    
17197670Sdes# THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               
18180740Sdes
19180750Sdes# ======================================================================
20180750Sdes# Initialization
21180750Sdes# ======================================================================
22180750Sdes
23180750SdesCSIH_SCRIPT=/usr/share/csih/cygwin-service-installation-helper.sh
24180750Sdes
25221377Sdes# List of apps used.  This is checkad for existance in csih_sanity_check
26221377Sdes# Don't use *any* transient commands before sourcing the csih helper script,
27221377Sdes# otherwise the sanity checks are short-circuited.
28221377Sdesdeclare -a csih_required_commands=(
29221377Sdes  /usr/bin/basename coreutils
30221377Sdes  /usr/bin/cat coreutils
31221377Sdes  /usr/bin/chmod coreutils
32221377Sdes  /usr/bin/dirname coreutils
33221377Sdes  /usr/bin/id coreutils
34221377Sdes  /usr/bin/mv coreutils
35221377Sdes  /usr/bin/rm coreutils
36221377Sdes  /usr/bin/cygpath cygwin
37294328Sdes  /usr/bin/mkpasswd cygwin
38221377Sdes  /usr/bin/mount cygwin
39221377Sdes  /usr/bin/ps cygwin
40221377Sdes  /usr/bin/umount cygwin
41221377Sdes  /usr/bin/cmp diffutils
42221377Sdes  /usr/bin/grep grep
43221377Sdes  /usr/bin/awk gawk
44221377Sdes  /usr/bin/ssh-keygen openssh
45221377Sdes  /usr/sbin/sshd openssh
46221377Sdes  /usr/bin/sed sed
47221377Sdes)
48221377Sdescsih_sanity_check_server=yes
49221377Sdessource ${CSIH_SCRIPT}
50221377Sdes
51221377SdesPROGNAME=$(/usr/bin/basename $0)
52221377Sdes_tdir=$(/usr/bin/dirname $0)
53221377SdesPROGDIR=$(cd $_tdir && pwd)
54221377Sdes
55180740Sdes# Subdirectory where the new package is being installed
56180740SdesPREFIX=/usr
57180740Sdes
58180740Sdes# Directory where the config files are stored
59180740SdesSYSCONFDIR=/etc
60180740SdesLOCALSTATEDIR=/var
61180740Sdes
62294328Sdessshd_config_configured=no
63180740Sdesport_number=22
64294332Sdesservice_name=sshd
65294328Sdesstrictmodes=yes
66180740Sdesprivsep_used=yes
67189006Sdescygwin_value=""
68197670Sdesuser_account=
69180750Sdespassword_value=
70197670Sdesopt_force=no
71180740Sdes
72180750Sdes# ======================================================================
73180750Sdes# Routine: update_services_file
74180750Sdes# ======================================================================
75180750Sdesupdate_services_file() {
76180750Sdes  local _my_etcdir="/ssh-host-config.$$"
77180750Sdes  local _win_etcdir
78180750Sdes  local _services
79180750Sdes  local _spaces
80180750Sdes  local _serv_tmp
81180750Sdes  local _wservices
82221377Sdes  local ret=0
83180750Sdes
84221377Sdes  _win_etcdir="${SYSTEMROOT}\\system32\\drivers\\etc"
85221377Sdes  _services="${_my_etcdir}/services"
86221377Sdes  _spaces="                           #"
87180750Sdes  _serv_tmp="${_my_etcdir}/srv.out.$$"
88189006Sdes
89221377Sdes  /usr/bin/mount -o text,posix=0,noacl -f "${_win_etcdir}" "${_my_etcdir}"
90189006Sdes
91180750Sdes  # Depends on the above mount
92180750Sdes  _wservices=`cygpath -w "${_services}"`
93189006Sdes
94180750Sdes  # Add ssh 22/tcp  and ssh 22/udp to services
95294328Sdes  if [ `/usr/bin/grep -q 'ssh[[:space:]][[:space:]]*22' "${_services}"; echo $?` -ne 0 ]
96180750Sdes  then
97221377Sdes    if /usr/bin/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}"
98180750Sdes    then
99221377Sdes      if /usr/bin/mv "${_serv_tmp}" "${_services}"
100180750Sdes      then
101189006Sdes	csih_inform "Added ssh to ${_wservices}"
102180750Sdes      else
103189006Sdes	csih_warning "Adding ssh to ${_wservices} failed!"
104221377Sdes	let ++ret
105180750Sdes      fi
106221377Sdes      /usr/bin/rm -f "${_serv_tmp}"
107180750Sdes    else
108180750Sdes      csih_warning "Adding ssh to ${_wservices} failed!"
109221377Sdes      let ++ret
110180750Sdes    fi
111180750Sdes  fi
112221377Sdes  /usr/bin/umount "${_my_etcdir}"
113221377Sdes  return $ret
114180750Sdes} # --- End of update_services_file --- #
115180740Sdes
116180750Sdes# ======================================================================
117294328Sdes# Routine: sshd_strictmodes
118294328Sdes#  MODIFIES: strictmodes
119294328Sdes# ======================================================================
120294328Sdessshd_strictmodes() {
121294328Sdes  if [ "${sshd_config_configured}" != "yes" ]
122294328Sdes  then
123294328Sdes    echo
124294328Sdes    csih_inform "StrictModes is set to 'yes' by default."
125294328Sdes    csih_inform "This is the recommended setting, but it requires that the POSIX"
126294328Sdes    csih_inform "permissions of the user's home directory, the user's .ssh"
127294328Sdes    csih_inform "directory, and the user's ssh key files are tight so that"
128294328Sdes    csih_inform "only the user has write permissions."
129294328Sdes    csih_inform "On the other hand, StrictModes don't work well with default"
130294328Sdes    csih_inform "Windows permissions of a home directory mounted with the"
131294328Sdes    csih_inform "'noacl' option, and they don't work at all if the home"
132294328Sdes    csih_inform "directory is on a FAT or FAT32 partition."
133294328Sdes    if ! csih_request "Should StrictModes be used?"
134294328Sdes    then
135294328Sdes      strictmodes=no
136294328Sdes    fi
137294328Sdes  fi
138294328Sdes  return 0
139294328Sdes}
140294328Sdes
141294328Sdes# ======================================================================
142180750Sdes# Routine: sshd_privsep
143294328Sdes#  MODIFIES: privsep_used
144180750Sdes# ======================================================================
145180750Sdessshd_privsep() {
146221377Sdes  local ret=0
147180740Sdes
148294328Sdes  if [ "${sshd_config_configured}" != "yes" ]
149180750Sdes  then
150294328Sdes    echo
151294328Sdes    csih_inform "Privilege separation is set to 'sandbox' by default since"
152294328Sdes    csih_inform "OpenSSH 6.1.  This is unsupported by Cygwin and has to be set"
153294328Sdes    csih_inform "to 'yes' or 'no'."
154294328Sdes    csih_inform "However, using privilege separation requires a non-privileged account"
155294328Sdes    csih_inform "called 'sshd'."
156221377Sdes    csih_inform "For more info on privilege separation read /usr/share/doc/openssh/README.privsep."
157221377Sdes    if csih_request "Should privilege separation be used?"
158180750Sdes    then
159221377Sdes      privsep_used=yes
160221377Sdes      if ! csih_create_unprivileged_user sshd
161180750Sdes      then
162221377Sdes	csih_error_recoverable "Couldn't create user 'sshd'!"
163221377Sdes	csih_error_recoverable "Privilege separation set to 'no' again!"
164221377Sdes	csih_error_recoverable "Check your ${SYSCONFDIR}/sshd_config file!"
165221377Sdes	let ++ret
166189006Sdes	privsep_used=no
167180750Sdes      fi
168180750Sdes    else
169180750Sdes      privsep_used=no
170180750Sdes    fi
171180750Sdes  fi
172294328Sdes  return $ret
173294328Sdes} # --- End of sshd_privsep --- #
174189006Sdes
175294328Sdes# ======================================================================
176294328Sdes# Routine: sshd_config_tweak
177294328Sdes# ======================================================================
178294328Sdessshd_config_tweak() {
179294328Sdes  local ret=0
180294328Sdes
181294328Sdes  # Modify sshd_config
182294328Sdes  csih_inform "Updating ${SYSCONFDIR}/sshd_config file"
183294328Sdes  if [ "${port_number}" -ne 22 ]
184180750Sdes  then
185294328Sdes    /usr/bin/sed -i -e "s/^#\?[[:space:]]*Port[[:space:]].*/Port ${port_number}/" \
186294328Sdes      ${SYSCONFDIR}/sshd_config
187294328Sdes    if [ $? -ne 0 ]
188221377Sdes    then
189294328Sdes      csih_warning "Setting listening port to ${port_number} failed!"
190294328Sdes      csih_warning "Check your ${SYSCONFDIR}/sshd_config file!"
191294328Sdes      let ++ret
192221377Sdes    fi
193294328Sdes  fi
194294328Sdes  if [ "${strictmodes}" = "no" ]
195180750Sdes  then
196294328Sdes    /usr/bin/sed -i -e "s/^#\?[[:space:]]*StrictModes[[:space:]].*/StrictModes no/" \
197294328Sdes      ${SYSCONFDIR}/sshd_config
198294328Sdes    if [ $? -ne 0 ]
199221377Sdes    then
200294328Sdes      csih_warning "Setting StrictModes to 'no' failed!"
201294328Sdes      csih_warning "Check your ${SYSCONFDIR}/sshd_config file!"
202294328Sdes      let ++ret
203221377Sdes    fi
204180750Sdes  fi
205294328Sdes  if [ "${sshd_config_configured}" != "yes" ]
206294328Sdes  then
207294328Sdes    /usr/bin/sed -i -e "
208294328Sdes      s/^#\?UsePrivilegeSeparation .*/UsePrivilegeSeparation ${privsep_used}/" \
209294328Sdes      ${SYSCONFDIR}/sshd_config
210294328Sdes    if [ $? -ne 0 ]
211294328Sdes    then
212294328Sdes      csih_warning "Setting privilege separation failed!"
213294328Sdes      csih_warning "Check your ${SYSCONFDIR}/sshd_config file!"
214294328Sdes      let ++ret
215294328Sdes    fi
216294328Sdes  fi
217221377Sdes  return $ret
218294328Sdes} # --- End of sshd_config_tweak --- #
219180750Sdes
220180750Sdes# ======================================================================
221180750Sdes# Routine: update_inetd_conf
222180750Sdes# ======================================================================
223180750Sdesupdate_inetd_conf() {
224180750Sdes  local _inetcnf="${SYSCONFDIR}/inetd.conf"
225180750Sdes  local _inetcnf_tmp="${SYSCONFDIR}/inetd.conf.$$"
226180750Sdes  local _inetcnf_dir="${SYSCONFDIR}/inetd.d"
227180750Sdes  local _sshd_inetd_conf="${_inetcnf_dir}/sshd-inetd"
228180750Sdes  local _sshd_inetd_conf_tmp="${_inetcnf_dir}/sshd-inetd.$$"
229180750Sdes  local _with_comment=1
230221377Sdes  local ret=0
231180750Sdes
232180750Sdes  if [ -d "${_inetcnf_dir}" ]
233180750Sdes  then
234180750Sdes    # we have inetutils-1.5 inetd.d support
235180750Sdes    if [ -f "${_inetcnf}" ]
236180750Sdes    then
237294328Sdes      /usr/bin/grep -q '^[[:space:]]*ssh' "${_inetcnf}" && _with_comment=0
238180750Sdes
239180750Sdes      # check for sshd OR ssh in top-level inetd.conf file, and remove
240180750Sdes      # will be replaced by a file in inetd.d/
241294328Sdes      if [ $(/usr/bin/grep -q '^[# \t]*ssh' "${_inetcnf}"; echo $?) -eq 0 ]
242180750Sdes      then
243221377Sdes	/usr/bin/grep -v '^[# \t]*ssh' "${_inetcnf}" >> "${_inetcnf_tmp}"
244189006Sdes	if [ -f "${_inetcnf_tmp}" ]
245189006Sdes	then
246221377Sdes	  if /usr/bin/mv "${_inetcnf_tmp}" "${_inetcnf}"
247189006Sdes	  then
248180750Sdes  	    csih_inform "Removed ssh[d] from ${_inetcnf}"
249189006Sdes	  else
250180750Sdes  	    csih_warning "Removing ssh[d] from ${_inetcnf} failed!"
251221377Sdes	    let ++ret
252189006Sdes	  fi
253221377Sdes	  /usr/bin/rm -f "${_inetcnf_tmp}"
254189006Sdes	else
255189006Sdes	  csih_warning "Removing ssh[d] from ${_inetcnf} failed!"
256221377Sdes	  let ++ret
257189006Sdes	fi
258180750Sdes      fi
259180750Sdes    fi
260180750Sdes
261180750Sdes    csih_install_config "${_sshd_inetd_conf}"   "${SYSCONFDIR}/defaults"
262221377Sdes    if /usr/bin/cmp "${SYSCONFDIR}/defaults${_sshd_inetd_conf}" "${_sshd_inetd_conf}" >/dev/null 2>&1
263180750Sdes    then
264180750Sdes      if [ "${_with_comment}" -eq 0 ]
265180750Sdes      then
266294328Sdes	/usr/bin/sed -e 's/@COMMENT@[[:space:]]*//' < "${_sshd_inetd_conf}" > "${_sshd_inetd_conf_tmp}"
267180750Sdes      else
268294328Sdes	/usr/bin/sed -e 's/@COMMENT@[[:space:]]*/# /' < "${_sshd_inetd_conf}" > "${_sshd_inetd_conf_tmp}"
269180750Sdes      fi
270221377Sdes      if /usr/bin/mv "${_sshd_inetd_conf_tmp}" "${_sshd_inetd_conf}"
271221377Sdes      then
272221377Sdes	csih_inform "Updated ${_sshd_inetd_conf}"
273221377Sdes      else
274221377Sdes	csih_warning "Updating ${_sshd_inetd_conf} failed!"
275221377Sdes	let ++ret
276221377Sdes      fi
277189006Sdes    fi
278180750Sdes
279180750Sdes  elif [ -f "${_inetcnf}" ]
280180750Sdes  then
281294328Sdes    /usr/bin/grep -q '^[[:space:]]*sshd' "${_inetcnf}" && _with_comment=0
282180750Sdes
283180750Sdes    # check for sshd in top-level inetd.conf file, and remove
284180750Sdes    # will be replaced by a file in inetd.d/
285294328Sdes    if [ `/usr/bin/grep -q '^#\?[[:space:]]*sshd' "${_inetcnf}"; echo $?` -eq 0 ]
286180750Sdes    then
287294328Sdes      /usr/bin/grep -v '^#\?[[:space:]]*sshd' "${_inetcnf}" >> "${_inetcnf_tmp}"
288180750Sdes      if [ -f "${_inetcnf_tmp}" ]
289180750Sdes      then
290221377Sdes	if /usr/bin/mv "${_inetcnf_tmp}" "${_inetcnf}"
291189006Sdes	then
292180750Sdes	    csih_inform "Removed sshd from ${_inetcnf}"
293189006Sdes	else
294180750Sdes	    csih_warning "Removing sshd from ${_inetcnf} failed!"
295221377Sdes	    let ++ret
296189006Sdes	fi
297221377Sdes	/usr/bin/rm -f "${_inetcnf_tmp}"
298180750Sdes      else
299189006Sdes	csih_warning "Removing sshd from ${_inetcnf} failed!"
300221377Sdes	let ++ret
301180750Sdes      fi
302180750Sdes    fi
303189006Sdes
304180750Sdes    # Add ssh line to inetd.conf
305221377Sdes    if [ `/usr/bin/grep -q '^[# \t]*ssh' "${_inetcnf}"; echo $?` -ne 0 ]
306180750Sdes    then
307180750Sdes      if [ "${_with_comment}" -eq 0 ]
308180750Sdes      then
309189006Sdes	echo 'ssh  stream  tcp     nowait  root    /usr/sbin/sshd sshd -i' >> "${_inetcnf}"
310180750Sdes      else
311189006Sdes	echo '# ssh  stream  tcp     nowait  root    /usr/sbin/sshd sshd -i' >> "${_inetcnf}"
312180750Sdes      fi
313221377Sdes      if [ $? -eq 0 ]
314221377Sdes      then
315221377Sdes	csih_inform "Added ssh to ${_inetcnf}"
316221377Sdes      else
317221377Sdes	csih_warning "Adding ssh to ${_inetcnf} failed!"
318221377Sdes	let ++ret
319221377Sdes      fi
320180750Sdes    fi
321180750Sdes  fi
322221377Sdes  return $ret
323180750Sdes} # --- End of update_inetd_conf --- #
324180750Sdes
325180750Sdes# ======================================================================
326221377Sdes# Routine: check_service_files_ownership
327221377Sdes#   Checks that the files in /etc and /var belong to the right owner
328221377Sdes# ======================================================================
329221377Sdescheck_service_files_ownership() {
330221377Sdes  local run_service_as=$1
331221377Sdes  local ret=0
332221377Sdes
333221377Sdes  if [ -z "${run_service_as}" ]
334221377Sdes  then
335294328Sdes    accnt_name=$(/usr/bin/cygrunsrv -VQ sshd |
336294328Sdes    		 /usr/bin/sed -ne 's/^Account *: *//gp')
337221377Sdes    if [ "${accnt_name}" = "LocalSystem" ]
338221377Sdes    then
339221377Sdes      # Convert "LocalSystem" to "SYSTEM" as is the correct account name
340294328Sdes      run_service_as="SYSTEM"
341294328Sdes    else
342294328Sdes      dom="${accnt_name%%\\*}"
343294328Sdes      accnt_name="${accnt_name#*\\}"
344294328Sdes      if [ "${dom}" = '.' ]
345294328Sdes      then
346294328Sdes	# Check local account
347294328Sdes	run_service_as=$(/usr/bin/mkpasswd -l -u "${accnt_name}" |
348294328Sdes			 /usr/bin/awk -F: '{print $1;}')
349294328Sdes      else
350294328Sdes      	# Check domain
351294328Sdes	run_service_as=$(/usr/bin/mkpasswd -d "${dom}" -u "${accnt_name}" |
352294328Sdes			 /usr/bin/awk -F: '{print $1;}')
353294328Sdes      fi
354221377Sdes    fi
355221377Sdes    if [ -z "${run_service_as}" ]
356221377Sdes    then
357294332Sdes      csih_warning "Couldn't determine name of user running sshd service from account database!"
358221377Sdes      csih_warning "As a result, this script cannot make sure that the files used"
359221377Sdes      csih_warning "by the sshd service belong to the user running the service."
360221377Sdes      return 1
361221377Sdes    fi
362221377Sdes  fi
363221377Sdes  for i in "${SYSCONFDIR}"/ssh_config "${SYSCONFDIR}"/sshd_config "${SYSCONFDIR}"/ssh_host_*key "${SYSCONFDIR}"/ssh_host_*key.pub
364221377Sdes  do
365221377Sdes    if [ -f "$i" ]
366221377Sdes    then
367221377Sdes      if ! chown "${run_service_as}".544 "$i" >/dev/null 2>&1
368221377Sdes      then
369221377Sdes	csih_warning "Couldn't change owner of $i!"
370221377Sdes	let ++ret
371221377Sdes      fi
372221377Sdes    fi
373221377Sdes  done
374221377Sdes  if ! chown "${run_service_as}".544 ${LOCALSTATEDIR}/empty >/dev/null 2>&1
375221377Sdes  then
376221377Sdes    csih_warning "Couldn't change owner of ${LOCALSTATEDIR}/empty!"
377221377Sdes    let ++ret
378221377Sdes  fi
379221377Sdes  if ! chown "${run_service_as}".544 ${LOCALSTATEDIR}/log/lastlog >/dev/null 2>&1
380221377Sdes  then
381221377Sdes    csih_warning "Couldn't change owner of ${LOCALSTATEDIR}/log/lastlog!"
382221377Sdes    let ++ret
383221377Sdes  fi
384221377Sdes  if [ -f ${LOCALSTATEDIR}/log/sshd.log ]
385221377Sdes  then
386221377Sdes    if ! chown "${run_service_as}".544 ${LOCALSTATEDIR}/log/sshd.log >/dev/null 2>&1
387221377Sdes    then
388221377Sdes      csih_warning "Couldn't change owner of ${LOCALSTATEDIR}/log/sshd.log!"
389221377Sdes      let ++ret
390221377Sdes    fi
391221377Sdes  fi
392221377Sdes  if [ $ret -ne 0 ]
393221377Sdes  then
394221377Sdes    csih_warning "Couldn't change owner of important files to ${run_service_as}!"
395221377Sdes    csih_warning "This may cause the sshd service to fail!  Please make sure that"
396221377Sdes    csih_warning "you have suufficient permissions to change the ownership of files"
397221377Sdes    csih_warning "and try to run the ssh-host-config script again."
398221377Sdes  fi
399221377Sdes  return $ret
400221377Sdes} # --- End of check_service_files_ownership --- #
401221377Sdes
402221377Sdes# ======================================================================
403180750Sdes# Routine: install_service
404180750Sdes#   Install sshd as a service
405180750Sdes# ======================================================================
406180750Sdesinstall_service() {
407180750Sdes  local run_service_as
408180750Sdes  local password
409221377Sdes  local ret=0
410180750Sdes
411221377Sdes  echo
412294332Sdes  if /usr/bin/cygrunsrv -Q ${service_name} >/dev/null 2>&1
413180750Sdes  then
414221377Sdes    csih_inform "Sshd service is already installed."
415221377Sdes    check_service_files_ownership "" || let ret+=$?
416221377Sdes  else
417221377Sdes    echo -e "${_csih_QUERY_STR} Do you want to install sshd as a service?"
418221377Sdes    if csih_request "(Say \"no\" if it is already installed as a service)"
419180750Sdes    then
420221377Sdes      csih_get_cygenv "${cygwin_value}"
421221377Sdes
422221377Sdes      if ( csih_is_nt2003 || [ "$csih_FORCE_PRIVILEGED_USER" = "yes" ] )
423180750Sdes      then
424221377Sdes	csih_inform "On Windows Server 2003, Windows Vista, and above, the"
425221377Sdes	csih_inform "SYSTEM account cannot setuid to other users -- a capability"
426221377Sdes	csih_inform "sshd requires.  You need to have or to create a privileged"
427221377Sdes	csih_inform "account.  This script will help you do so."
428221377Sdes	echo
429180750Sdes
430221377Sdes	[ "${opt_force}" = "yes" ] && opt_f=-f
431221377Sdes	[ -n "${user_account}" ] && opt_u="-u ""${user_account}"""
432221377Sdes	csih_select_privileged_username ${opt_f} ${opt_u} sshd
433221377Sdes
434221377Sdes	if ! csih_create_privileged_user "${password_value}"
435189006Sdes	then
436221377Sdes	  csih_error_recoverable "There was a serious problem creating a privileged user."
437221377Sdes	  csih_request "Do you want to proceed anyway?" || exit 1
438221377Sdes	  let ++ret
439189006Sdes	fi
440221377Sdes      fi
441180750Sdes
442221377Sdes      # Never returns empty if NT or above
443221377Sdes      run_service_as=$(csih_service_should_run_as)
444180750Sdes
445221377Sdes      if [ "${run_service_as}" = "${csih_PRIVILEGED_USERNAME}" ]
446221377Sdes      then
447221377Sdes	password="${csih_PRIVILEGED_PASSWORD}"
448221377Sdes	if [ -z "${password}" ]
449189006Sdes	then
450221377Sdes	  csih_get_value "Please enter the password for user '${run_service_as}':" "-s"
451221377Sdes	  password="${csih_value}"
452189006Sdes	fi
453221377Sdes      fi
454180750Sdes
455221377Sdes      # At this point, we either have $run_service_as = "system" and
456221377Sdes      # $password is empty, or $run_service_as is some privileged user and
457221377Sdes      # (hopefully) $password contains the correct password.  So, from here
458221377Sdes      # out, we use '-z "${password}"' to discriminate the two cases.
459180750Sdes
460221377Sdes      csih_check_user "${run_service_as}"
461180750Sdes
462221377Sdes      if [ -n "${csih_cygenv}" ]
463221377Sdes      then
464221377Sdes	cygwin_env=( -e "CYGWIN=${csih_cygenv}" )
465221377Sdes      fi
466221377Sdes      if [ -z "${password}" ]
467221377Sdes      then
468294332Sdes	if /usr/bin/cygrunsrv -I ${service_name} -d "CYGWIN ${service_name}" -p /usr/sbin/sshd \
469221377Sdes			      -a "-D" -y tcpip "${cygwin_env[@]}"
470189006Sdes	then
471221377Sdes	  echo
472221377Sdes	  csih_inform "The sshd service has been installed under the LocalSystem"
473221377Sdes	  csih_inform "account (also known as SYSTEM). To start the service now, call"
474221377Sdes	  csih_inform "\`net start sshd' or \`cygrunsrv -S sshd'.  Otherwise, it"
475221377Sdes	  csih_inform "will start automatically after the next reboot."
476189006Sdes	fi
477221377Sdes      else
478294332Sdes	if /usr/bin/cygrunsrv -I ${service_name} -d "CYGWIN ${service_name}" -p /usr/sbin/sshd \
479221377Sdes			      -a "-D" -y tcpip "${cygwin_env[@]}" \
480221377Sdes			      -u "${run_service_as}" -w "${password}"
481189006Sdes	then
482239849Sdes	  /usr/bin/editrights -u "${run_service_as}" -a SeServiceLogonRight
483221377Sdes	  echo
484221377Sdes	  csih_inform "The sshd service has been installed under the '${run_service_as}'"
485294332Sdes	  csih_inform "account.  To start the service now, call \`net start ${service_name}' or"
486294332Sdes	  csih_inform "\`cygrunsrv -S ${service_name}'.  Otherwise, it will start automatically"
487221377Sdes	  csih_inform "after the next reboot."
488189006Sdes	fi
489221377Sdes      fi
490180750Sdes
491294332Sdes      if /usr/bin/cygrunsrv -Q ${service_name} >/dev/null 2>&1
492221377Sdes      then
493221377Sdes	check_service_files_ownership "${run_service_as}" || let ret+=$?
494221377Sdes      else
495221377Sdes	csih_error_recoverable "Installing sshd as a service failed!"
496221377Sdes	let ++ret
497221377Sdes      fi
498221377Sdes    fi # user allowed us to install as service
499221377Sdes  fi # service not yet installed
500221377Sdes  return $ret
501180750Sdes} # --- End of install_service --- #
502180750Sdes
503180750Sdes# ======================================================================
504180750Sdes# Main Entry Point
505180750Sdes# ======================================================================
506180750Sdes
507180750Sdes# Check how the script has been started.  If
508180750Sdes#   (1) it has been started by giving the full path and
509180750Sdes#       that path is /etc/postinstall, OR
510180750Sdes#   (2) Otherwise, if the environment variable
511180750Sdes#       SSH_HOST_CONFIG_AUTO_ANSWER_NO is set
512180750Sdes# then set auto_answer to "no".  This allows automatic
513180750Sdes# creation of the config files in /etc w/o overwriting
514180750Sdes# them if they already exist.  In both cases, color
515180750Sdes# escape sequences are suppressed, so as to prevent
516180750Sdes# cluttering setup's logfiles.
517180750Sdesif [ "$PROGDIR" = "/etc/postinstall" ]
518180750Sdesthen
519180750Sdes  csih_auto_answer="no"
520180750Sdes  csih_disable_color
521197670Sdes  opt_force=yes
522180750Sdesfi
523180750Sdesif [ -n "${SSH_HOST_CONFIG_AUTO_ANSWER_NO}" ]
524180750Sdesthen
525180750Sdes  csih_auto_answer="no"
526180750Sdes  csih_disable_color
527197670Sdes  opt_force=yes
528180750Sdesfi
529180750Sdes
530180750Sdes# ======================================================================
531180750Sdes# Parse options
532180750Sdes# ======================================================================
533180740Sdeswhile :
534180740Sdesdo
535180740Sdes  case $# in
536180740Sdes  0)
537180740Sdes    break
538180740Sdes    ;;
539180740Sdes  esac
540180740Sdes
541180740Sdes  option=$1
542180740Sdes  shift
543180740Sdes
544180740Sdes  case "${option}" in
545180740Sdes  -d | --debug )
546180740Sdes    set -x
547180750Sdes    csih_trace_on
548180740Sdes    ;;
549180740Sdes
550180740Sdes  -y | --yes )
551180750Sdes    csih_auto_answer=yes
552197670Sdes    opt_force=yes
553180740Sdes    ;;
554180740Sdes
555180740Sdes  -n | --no )
556180750Sdes    csih_auto_answer=no
557197670Sdes    opt_force=yes
558180740Sdes    ;;
559180740Sdes
560180740Sdes  -c | --cygwin )
561180740Sdes    cygwin_value="$1"
562180740Sdes    shift
563180740Sdes    ;;
564180740Sdes
565294332Sdes  -N | --name )
566294332Sdes    service_name=$1
567294332Sdes    shift
568294332Sdes    ;;
569294332Sdes
570180740Sdes  -p | --port )
571180740Sdes    port_number=$1
572180740Sdes    shift
573180740Sdes    ;;
574180740Sdes
575197670Sdes  -u | --user )
576197670Sdes    user_account="$1"
577197670Sdes    shift
578197670Sdes    ;;
579197670Sdes    
580180740Sdes  -w | --pwd )
581180740Sdes    password_value="$1"
582180740Sdes    shift
583180740Sdes    ;;
584180740Sdes
585180750Sdes  --privileged )
586180750Sdes    csih_FORCE_PRIVILEGED_USER=yes
587180750Sdes    ;;
588180750Sdes
589180740Sdes  *)
590180740Sdes    echo "usage: ${progname} [OPTION]..."
591180740Sdes    echo
592180740Sdes    echo "This script creates an OpenSSH host configuration."
593180740Sdes    echo
594180740Sdes    echo "Options:"
595180740Sdes    echo "  --debug  -d            Enable shell's debug output."
596180740Sdes    echo "  --yes    -y            Answer all questions with \"yes\" automatically."
597180740Sdes    echo "  --no     -n            Answer all questions with \"no\" automatically."
598180740Sdes    echo "  --cygwin -c <options>  Use \"options\" as value for CYGWIN environment var."
599294332Sdes    echo "  --name   -N <name>     sshd windows service name."
600180740Sdes    echo "  --port   -p <n>        sshd listens on port n."
601255670Sdes    echo "  --user   -u <account>  privileged user for service, default 'cyg_server'."
602180750Sdes    echo "  --pwd    -w <passwd>   Use \"pwd\" as password for privileged user."
603255670Sdes    echo "  --privileged           On Windows XP, require privileged user"
604180750Sdes    echo "                         instead of LocalSystem for sshd service."
605180740Sdes    echo
606180740Sdes    exit 1
607180740Sdes    ;;
608180740Sdes
609180740Sdes  esac
610180740Sdesdone
611180740Sdes
612180750Sdes# ======================================================================
613180750Sdes# Action!
614180750Sdes# ======================================================================
615180740Sdes
616180740Sdes# Check for running ssh/sshd processes first. Refuse to do anything while
617180740Sdes# some ssh processes are still running
618221377Sdesif /usr/bin/ps -ef | /usr/bin/grep -q '/sshd\?$'
619180740Sdesthen
620180740Sdes  echo
621180750Sdes  csih_error "There are still ssh processes running. Please shut them down first."
622180740Sdesfi
623180740Sdes
624221377Sdes# Make sure the user is running in an administrative context
625221377Sdesadmin=$(/usr/bin/id -G | /usr/bin/grep -Eq '\<544\>' && echo yes || echo no)
626221377Sdesif [ "${admin}" != "yes" ]
627221377Sdesthen
628221377Sdes  echo
629221377Sdes  csih_warning "Running this script typically requires administrator privileges!"
630221377Sdes  csih_warning "However, it seems your account does not have these privileges."
631221377Sdes  csih_warning "Here's the list of groups in your user token:"
632221377Sdes  echo
633294332Sdes  /usr/bin/id -Gnz | xargs -0n1 echo "   "
634221377Sdes  echo
635221377Sdes  csih_warning "This usually means you're running this script from a non-admin"
636221377Sdes  csih_warning "desktop session, or in a non-elevated shell under UAC control."
637221377Sdes  echo
638221377Sdes  csih_warning "Make sure you have the appropriate privileges right now,"
639221377Sdes  csih_warning "otherwise parts of this script will probably fail!"
640221377Sdes  echo
641221377Sdes  echo -e "${_csih_QUERY_STR} Are you sure you want to continue?  (Say \"no\" if you're not sure"
642221377Sdes  if ! csih_request "you have the required privileges)"
643221377Sdes  then
644221377Sdes    echo
645221377Sdes    csih_inform "Ok.  Exiting.  Make sure to switch to an administrative account"
646221377Sdes    csih_inform "or to start this script from an elevated shell."
647221377Sdes    exit 1
648221377Sdes  fi
649221377Sdesfi
650221377Sdes
651221377Sdesecho
652221377Sdes
653221377Sdeswarning_cnt=0
654221377Sdes
655180750Sdes# Create /var/log/lastlog if not already exists
656180740Sdesif [ -e ${LOCALSTATEDIR}/log/lastlog -a ! -f ${LOCALSTATEDIR}/log/lastlog ]
657180740Sdesthen
658189006Sdes  echo
659180750Sdes  csih_error_multi "${LOCALSTATEDIR}/log/lastlog exists, but is not a file." \
660189006Sdes		   "Cannot create ssh host configuration."
661180740Sdesfi
662180740Sdesif [ ! -e ${LOCALSTATEDIR}/log/lastlog ]
663180740Sdesthen
664221377Sdes  /usr/bin/cat /dev/null > ${LOCALSTATEDIR}/log/lastlog
665221377Sdes  if ! /usr/bin/chmod 644 ${LOCALSTATEDIR}/log/lastlog >/dev/null 2>&1
666221377Sdes  then
667221377Sdes    csih_warning "Can't set permissions on ${LOCALSTATEDIR}/log/lastlog!"
668221377Sdes    let ++warning_cnt
669221377Sdes  fi
670180740Sdesfi
671180740Sdes
672180740Sdes# Create /var/empty file used as chroot jail for privilege separation
673197670Sdescsih_make_dir "${LOCALSTATEDIR}/empty" "Cannot create ${LOCALSTATEDIR}/empty directory."
674221377Sdesif ! /usr/bin/chmod 755 "${LOCALSTATEDIR}/empty" >/dev/null 2>&1
675221377Sdesthen
676221377Sdes  csih_warning "Can't set permissions on ${LOCALSTATEDIR}/empty!"
677221377Sdes  let ++warning_cnt
678221377Sdesfi
679180740Sdes
680261320Sdes# generate missing host keys
681294328Sdescsih_inform "Generating missing SSH host keys"
682261320Sdes/usr/bin/ssh-keygen -A || let warning_cnt+=$?
683180740Sdes
684180750Sdes# handle ssh_config
685221377Sdescsih_install_config "${SYSCONFDIR}/ssh_config" "${SYSCONFDIR}/defaults" || let ++warning_cnt
686221377Sdesif /usr/bin/cmp "${SYSCONFDIR}/ssh_config" "${SYSCONFDIR}/defaults/${SYSCONFDIR}/ssh_config" >/dev/null 2>&1
687180740Sdesthen
688180740Sdes  if [ "${port_number}" != "22" ]
689180740Sdes  then
690180750Sdes    csih_inform "Updating ${SYSCONFDIR}/ssh_config file with requested port"
691180740Sdes    echo "Host localhost" >> ${SYSCONFDIR}/ssh_config
692180740Sdes    echo "    Port ${port_number}" >> ${SYSCONFDIR}/ssh_config
693180740Sdes  fi
694180740Sdesfi
695180740Sdes
696180750Sdes# handle sshd_config (and privsep)
697221377Sdescsih_install_config "${SYSCONFDIR}/sshd_config" "${SYSCONFDIR}/defaults" || let ++warning_cnt
698221377Sdesif ! /usr/bin/cmp "${SYSCONFDIR}/sshd_config" "${SYSCONFDIR}/defaults/${SYSCONFDIR}/sshd_config" >/dev/null 2>&1
699180740Sdesthen
700294328Sdes  sshd_config_configured=yes
701180740Sdesfi
702294328Sdessshd_strictmodes || let warning_cnt+=$?
703221377Sdessshd_privsep || let warning_cnt+=$?
704294328Sdessshd_config_tweak || let warning_cnt+=$?
705221377Sdesupdate_services_file || let warning_cnt+=$?
706221377Sdesupdate_inetd_conf || let warning_cnt+=$?
707221377Sdesinstall_service || let warning_cnt+=$?
708180740Sdes
709180750Sdesecho
710221377Sdesif [ $warning_cnt -eq 0 ]
711221377Sdesthen
712221377Sdes  csih_inform "Host configuration finished. Have fun!"
713221377Sdeselse
714221377Sdes  csih_warning "Host configuration exited with ${warning_cnt} errors or warnings!"
715221377Sdes  csih_warning "Make sure that all problems reported are fixed,"
716221377Sdes  csih_warning "then re-run ssh-host-config."
717221377Sdesfi
718221377Sdesexit $warning_cnt
719