sshd revision 252062
1150835Syar#!/bin/sh
2150835Syar#
3150835Syar# $FreeBSD: head/etc/rc.d/sshd 252062 2013-06-21 17:23:19Z delphij $
4150835Syar#
5150835Syar
6150835Syar# PROVIDE: sshd
7195026Sdougb# REQUIRE: LOGIN FILESYSTEMS
8150835Syar# KEYWORD: shutdown
9150835Syar
10150835Syar. /etc/rc.subr
11150835Syar
12150835Syarname="sshd"
13230099Sdougbrcvar="sshd_enable"
14150835Syarcommand="/usr/sbin/${name}"
15150835Syarkeygen_cmd="sshd_keygen"
16150835Syarstart_precmd="sshd_precmd"
17165683Syarreload_precmd="sshd_precmd"
18150835Syarrestart_precmd="sshd_precmd"
19150835Syarconfigtest_cmd="sshd_configtest"
20150835Syarpidfile="/var/run/${name}.pid"
21150835Syarextra_commands="configtest keygen reload"
22150835Syar
23150835Syartimeout=300
24150835Syar
25150835Syaruser_reseed()
26150835Syar{
27150835Syar	(
28150835Syar	seeded=`sysctl -n kern.random.sys.seeded 2>/dev/null`
29150835Syar	if [ "x${seeded}" != "x" ] && [ ${seeded} -eq 0 ] ; then
30150835Syar		warn "Setting entropy source to blocking mode."
31150835Syar		echo "===================================================="
32168593Skeramida		echo "Type a full screenful of random junk to unblock"
33168593Skeramida		echo "it and remember to finish with <enter>. This will"
34150835Syar		echo "timeout in ${timeout} seconds, but waiting for"
35168593Skeramida		echo "the timeout without typing junk may make the"
36168593Skeramida		echo "entropy source deliver predictable output."
37168593Skeramida		echo ""
38246358Sdes		echo "Just hit <enter> for fast+insecure startup."
39168593Skeramida		echo "===================================================="
40150835Syar		sysctl kern.random.sys.seeded=0 2>/dev/null
41150835Syar		read -t ${timeout} junk
42150835Syar		echo "${junk}" `sysctl -a` `date` > /dev/random
43150835Syar	fi
44150835Syar	)
45150835Syar}
46150835Syar
47150835Syarsshd_keygen()
48150835Syar{
49150835Syar	(
50	umask 022
51
52	# Can't do anything if ssh is not installed
53	[ -x /usr/bin/ssh-keygen ] || {
54		warn "/usr/bin/ssh-keygen does not exist."
55		return 1
56	}
57
58	if [ -f /etc/ssh/ssh_host_key ]; then
59		echo "You already have an RSA host key" \
60		    "in /etc/ssh/ssh_host_key"
61		echo "Skipping protocol version 1 RSA Key Generation"
62	else
63		/usr/bin/ssh-keygen -t rsa1 -b 1024 \
64		    -f /etc/ssh/ssh_host_key -N ''
65	fi
66
67	if [ -f /etc/ssh/ssh_host_dsa_key ]; then
68		echo "You already have a DSA host key" \
69		    "in /etc/ssh/ssh_host_dsa_key"
70		echo "Skipping protocol version 2 DSA Key Generation"
71	else
72		/usr/bin/ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key -N ''
73	fi
74
75	if [ -f /etc/ssh/ssh_host_rsa_key ]; then
76		echo "You already have an RSA host key" \
77		    "in /etc/ssh/ssh_host_rsa_key"
78		echo "Skipping protocol version 2 RSA Key Generation"
79	else
80		/usr/bin/ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
81	fi
82
83	if [ -f /etc/ssh/ssh_host_ecdsa_key ]; then
84		echo "You already have an ECDSA host key" \
85		    "in /etc/ssh/ssh_host_ecdsa_key"
86		echo "Skipping protocol version 2 ECDSA Key Generation"
87	else
88		/usr/bin/ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
89	fi
90	)
91}
92
93sshd_configtest()
94{
95	echo "Performing sanity check on ${name} configuration."
96	eval ${command} ${sshd_flags} -t
97}
98
99sshd_precmd()
100{
101	if [ ! -f /etc/ssh/ssh_host_key -o \
102	    ! -f /etc/ssh/ssh_host_dsa_key -o \
103	    ! -f /etc/ssh/ssh_host_ecdsa_key -o \
104	    ! -f /etc/ssh/ssh_host_rsa_key ]; then
105		user_reseed
106		run_rc_command keygen
107	fi
108	sshd_configtest
109}
110
111load_rc_config $name
112run_rc_command "$1"
113