summaryrefslogtreecommitdiffstats
path: root/sbin/init.d/sshd
blob: deb5e86f578c819c66daf991e7243b58ad167a53 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/bash

. `dirname $0`/functions

STOP_FIRST_WITH=KILL

option	bin		reserved_option	/usr/sbin/sshd
option	cmdline		reserved_option	'$bin ${opt_port:+-p $opt_port} ${opt_config:+-f $opt_config} ${opt_protocol:+-oProtocol=$opt_protocol} ${opt_listen:+-oListenAddress=$opt_listen} ${pidfile:+-oPidfile=$pidfile}'
option	port		standard_option
option	config		standard_option
option	protocol	standard_option
option	listen		standard_option
option  pidfile		reserved_option /var/run/sshd.pid

sshd_cfgfile=""

# assign default values to options and variables before parsing the cfg file
function fct_end_section {
    sshd_cfgfile=${opt_config:-/etc/ssh/sshd_config}
    valueof $sshd_cfgfile HostKey > /dev/null
    hostkey_list=$REPLY
    valueof $sshd_cfgfile PidFile > /dev/null ; pidfile=${REPLY:-$pidfile}
}

function sshd_find_keys {
    if [ -z "$hostkey_list" ]; then
	echo "  File $sshd_cfgfile references no key."
	if [ -z "$opt_protocol" -o "${opt_protocol/1//}" != "$opt_protocol" ]; then
	    echo "  Assuming /etc/ssh/ssh_host_key for Protocol v1."
	    hostkey_list="$hostkey_list /etc/ssh/ssh_host_key"
	fi
	if [ -z "$opt_protocol" -o "${opt_protocol/2//}" != "${opt_protocol}" ]
        then
	    echo "  Assuming /etc/ssh/ssh_host_rsa_key for Protocol v2."
	    hostkey_list="$hostkey_list /etc/ssh/ssh_host_rsa_key"
	    echo "  Assuming /etc/ssh/ssh_host_dsa_key for Protocol v2."
	    hostkey_list="$hostkey_list /etc/ssh/ssh_host_dsa_key"
	fi
    else
	echo "  File $sshd_cfgfile references these keys : $hostkey_list"
    fi
}

function fct_pre_start {
    local missing=0
    local key
    local must_remount_ro=0

    sshd_find_keys
    for key in $hostkey_list; do
	if [ ! -e "$key" ]; then
	    echo "  Warning! host key $key does not exist."
	    missing=$[$missing+1]
	fi
    done

    if [ "$missing" -gt "0" ]; then
	echo "  Trying to generate the keys before starting SSHD."
	remount_rw /etc && must_remount_ro=1
	do_install
	[ $must_remount_ro -eq 1 ] && remount_ro /etc
	echo "  Now starting SSHD."
    fi
}

# This ensures that we use --force to restart the service
function do_restart {
    do_stop $*
    FORCE_START_STOP=1
    do_start $*
}

function do_install {
    local type=""
    local key

    sshd_find_keys
    for key in $hostkey_list; do
	if [ ! -e "$key" ]; then
	    case "$key" in
		*/ssh_host_key*) type=rsa1 ;;
		*/ssh_host_rsa_key*) type=rsa ;;
		*/ssh_host_dsa_key*) type=dsa ;;
		*) type=""
		   echo "  Warning! host key $key does not exist and cannot"
		   echo "  be auto-generated since it does not have a standard name."
		   echo "  If SSHD doesn't start, you'll have to generate it manually this way :"
		   echo "    # ssh-keygen -t { rsa1 | rsa | dsa } -N '' -f $key"
		   echo
		   ;;
	    esac
	    if [ "$type" ]; then
		ssh-keygen -t $type -N '' -f $key
		if [ ! -e "$key" ]; then
		    echo "  ERROR : ssh-keygen could not generate $type host key $key"
		fi
	    fi
	fi
    done
}

load_config