#!/bin/sh 

action=$1
version=$2

# Source debconf library.
. /usr/share/debconf/confmodule
db_version 2.0


get_config_option() {
	option="$1"

	[ -f /etc/ssh/sshd_config ] || return

	# TODO: actually only one '=' allowed after option
	perl -ne 'print if s/^[[:space:]]*'"$option"'[[:space:]=]+//i' \
	   /etc/ssh/sshd_config 2>/dev/null
}


if [ -e /etc/init.d/ssh ] && ! grep -q pidfile /etc/init.d/ssh
then
  db_fset ssh/use_old_init_script seen false
  db_input medium ssh/use_old_init_script || true
  db_go

  db_get ssh/use_old_init_script
  [ "$RET" = "false" ] && exit 0
else
  db_set ssh/use_old_init_script true
  db_fset ssh/use_old_init_script seen true
fi

if [ -e /etc/ssh/sshd_config ]
then
    # An empty version means we're upgrading from before the package split,
    # so check.
    if dpkg --compare-versions "$version" lt 1:3.8.1p1-11
    then
	passwordauth="$(get_config_option PasswordAuthentication)"
	crauth="$(get_config_option ChallengeResponseAuthentication)"
	if [ "$passwordauth" = no ] && \
	   ([ -z "$crauth" ] || [ "$crauth" = yes ])
	then
	    db_input critical ssh/disable_cr_auth || true
	fi
    fi
fi 

key=/etc/ssh/ssh_host_key
export key
if [ -n "$version" ] && [ -f $key ] && [ ! -x /usr/bin/ssh-keygen ] &&
     dpkg --compare-versions "$version" lt 1.2.28
then
  # make sure that keys get updated to get rid of IDEA; preinst
  # actually does the work, but if the old ssh-keygen is not found,
  # it can't do that -- thus, we tell the user that he must create
  # a new host key.
  printf '\0\0' | 3<&0 sh -c \
      'dd if=$key bs=1 skip=32 count=2 2>/dev/null | cmp -s - /dev/fd/3' || {
    # this means that bytes 32&33 of the key were not both zero, in which
    # case the key is encrypted, which we need to fix
    db_input high ssh/encrypted_host_key_but_no_keygen || true
  }
fi


db_go

exit 0
