summaryrefslogtreecommitdiff
path: root/security/openssh-portable/files/openssh.in
blob: a8c0043e607a68244780c9c8f8b330ba7035afb7 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/sh

# PROVIDE: openssh
# REQUIRE: DAEMON
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf to enable openssh:
#
# openssh_enable (bool):	Set it to "YES" to enable openssh.
#				Default is "NO".
# openssh_flags (flags):	Set extra flags to openssh.
#				Default is "". see sshd(1).
# openssh_pidfile (file):	Set full path to pid file.

. /etc/rc.subr

name="openssh"
rcvar=openssh_enable

load_rc_config ${name}

: ${openssh_enable:="NO"}
: ${openssh_skipportscheck="NO"}

command=%%PREFIX%%/sbin/sshd
extra_commands="configtest reload keygen"
start_precmd="${name}_checks"
reload_precmd="${name}_checks"
restart_precmd="${name}_checks"
configtest_cmd="${name}_configtest"
keygen_cmd="${name}_keygen"
pidfile=${openssh_pidfile:="/var/run/sshd.pid"}

openssh_keygen()
{
	if [ -f %%ETCDIR%%/ssh_host_dsa_key -a \
	    -f %%ETCDIR%%/ssh_host_rsa_key -a \
	    -f %%ETCDIR%%/ssh_host_ecdsa_key -a \
	    -f %%ETCDIR%%/ssh_host_ed25519_key ]; then
		return 0
	fi

	umask 022

	# Can't do anything if ssh is not installed
	[ -x %%PREFIX%%/bin/ssh-keygen ] ||
		err 1 "%%PREFIX%%/bin/ssh-keygen does not exist."

	if [ -f %%ETCDIR%%/ssh_host_dsa_key ]; then
		echo "You already have a DSA host key" \
			"in %%ETCDIR%%/ssh_host_dsa_key"
		echo "Skipping protocol version 2 DSA Key Generation"
	else
		%%PREFIX%%/bin/ssh-keygen -t dsa \
			-f %%ETCDIR%%/ssh_host_dsa_key -N ''
	fi

	if [ -f %%ETCDIR%%/ssh_host_rsa_key ]; then
		echo "You already have a RSA host key" \
			"in %%ETCDIR%%/ssh_host_rsa_key"
		echo "Skipping protocol version 2 RSA Key Generation"
	else
		%%PREFIX%%/bin/ssh-keygen -t rsa \
			-f %%ETCDIR%%/ssh_host_rsa_key -N ''
	fi

	if [ -f %%ETCDIR%%/ssh_host_ecdsa_key ]; then
		echo "You already have a Elliptic Curve DSA host key" \
			"in %%ETCDIR%%/ssh_host_ecdsa_key"
		echo "Skipping protocol version 2 Elliptic Curve DSA Key Generation"
	else
		%%PREFIX%%/bin/ssh-keygen -t ecdsa \
			-f %%ETCDIR%%/ssh_host_ecdsa_key -N ''
	fi

	if [ -f %%ETCDIR%%/ssh_host_ed25519_key ]; then
		echo "You already have a Elliptic Curve ED25519 host key" \
			"in %%ETCDIR%%/ssh_host_ed25519_key"
		echo "Skipping protocol version 2 Elliptic Curve ED25519 Key Generation"
	else
		%%PREFIX%%/bin/ssh-keygen -t ed25519 \
			-f %%ETCDIR%%/ssh_host_ed25519_key -N ''
	fi
}

openssh_check_same_ports(){
    # check if opensshd don't use base system sshd's port
    #
    # openssh binds ports in priority (lowest first):
    # Port from sshd_config
    # -p option from command line
    # ListenAddress addr:port from sshd_config


    #check if opensshd-portable installed in replacement of base sshd
    if [ "%%ETCDIR%%" = "/etc/ssh" ]; then
        return 1
    fi

    self_port=$(awk '$1~/^ListenAddress/ \
        {mlen=match($0,":[0-9]*$"); print \
        substr($0,mlen+1,length($0)-mlen)}' %%ETCDIR%%/sshd_config)
    if [ -z "$self_port" ]; then
        self_port=$(echo $openssh_flags | awk \
            '{for (i = 1; i <= NF; i++) if ($i == "-p") \
            {i++; printf "%s", $i; break; }; }')
        if [ -z "$self_port" ]; then
            self_port=$(awk '$1~/^Port/ {print $2}' \
                %%ETCDIR%%/sshd_config)
        fi
    fi
    # assume default 22 port
    if [ -z "$self_port" ]; then
        self_port=22
    fi

    load_rc_config "sshd"

    base_sshd_port=$(awk '$1~/^ListenAddress/ \
        {mlen=match($0,":[0-9]*$"); print \
        substr($0,mlen+1,length($0)-mlen)}' /etc/ssh/sshd_config)
    if [ -z "$base_sshd_port" ]; then
        base_sshd_port=$(echo $sshd_flags | awk \
            '{for (i = 1; i <= NF; i++) if ($i == "-p") \
            {i++; printf "%s", $i; break; }; }')
        if [ -z "$base_sshd_port" ]; then
            base_sshd_port=$(awk '$1~/^Port/ {print $2}' \
                /etc/ssh/sshd_config)
        fi
    fi
    if [ -z "$base_sshd_port" ]; then
        base_sshd_port=22
    fi

    # self_port and base_sshd_port may have multiple values. Compare them all
    for sport in ${self_port}; do
	    for bport in ${base_sshd_port}; do
		    [ ${sport} -eq ${bport} ] && return 0
	    done
    done

    return 1
}

openssh_configtest()
{
	echo "Performing sanity check on ${name} configuration."
	eval ${command} ${openssh_flags} -t
}

openssh_checks()
{
	if checkyesno sshd_enable ; then
      if openssh_check_same_ports && ! checkyesno openssh_skipportscheck; then
          err 1 "sshd_enable is set, but $name and /usr/sbin/sshd use the same port"
      fi
	fi

	run_rc_command keygen
	openssh_configtest
}

run_rc_command "$1"