rc.sshd 4.73 KB
Newer Older
1
#!/usr/local/bin/php
Franco Fichtner's avatar
Franco Fichtner committed
2 3 4
<?php

/*
5 6
	Copyright (C) 2004 Scott K Ullrich
	Copyright (C) 2004 Fred Mol <fredmol@xs4all.nl>.
7
	Copyright (C) 2015 Franco Fichtner <franco@opnsense.org>
Franco Fichtner's avatar
Franco Fichtner committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	1. Redistributions of source code must retain the above copyright notice,
	   this list of conditions and the following disclaimer.

	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.

	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.
*/

32
require_once('config.inc');
33
require_once("util.inc");
Franco Fichtner's avatar
Franco Fichtner committed
34

35 36 37 38 39 40
$bin_ssh_keygen = '/usr/local/bin/ssh-keygen';
$sbin_sshd = '/usr/local/sbin/sshd';
$etc_ssh = '/usr/local/etc/ssh';

/* if run from a shell session, `-af' and the full path is needed */
mwexecf('/bin/pkill -af %s', $sbin_sshd);
41

42
if (!isset($config['system']['ssh']['enabled'])) {
Franco Fichtner's avatar
Franco Fichtner committed
43 44 45
	return;
}

46
/* reinstall the backup if it is available */
47
if (file_exists('/conf/sshd/ssh_host_rsa_key') && !file_exists("{$etc_ssh}/ssh_host_rsa_key")) {
48 49 50
	mwexec("/bin/cp -p /conf/sshd/* {$etc_ssh}/");
}

Franco Fichtner's avatar
Franco Fichtner committed
51
$keys = array(
52 53 54 55 56
	/* .pub files are implied */
	'rsa' => 'ssh_host_rsa_key',
	'dsa' => 'ssh_host_dsa_key',
	'ecdsa' => 'ssh_host_ecdsa_key',
	'ed25519' => 'ssh_host_ed25519_key',
Franco Fichtner's avatar
Franco Fichtner committed
57 58
);

59 60 61 62 63 64 65 66
foreach($keys as $name) {
	$file = "{$etc_ssh}/etc/ssh/{$name}";
	if (file_exists($file) && filesize($file) == 0) {
		unlink($file);
	}
	$file = "{$file}.pub";
	if (file_exists($file) && filesize($file) == 0) {
		unlink($file);
Franco Fichtner's avatar
Franco Fichtner committed
67 68 69
	}
}

70 71
/* make ssh home directory */
@mkdir("/var/empty", 0555, true);
Franco Fichtner's avatar
Franco Fichtner committed
72

73 74
/* Login related files. */
touch("/var/log/lastlog");
Franco Fichtner's avatar
Franco Fichtner committed
75

76
if (isset($config['system']['ssh']['port'])) {
Franco Fichtner's avatar
Franco Fichtner committed
77
	$sshport = $config['system']['ssh']['port'];
78
} else {
Franco Fichtner's avatar
Franco Fichtner committed
79
	$sshport = 22;
80 81 82 83 84 85 86 87 88
}

/*
 * XXX ZOMG sshd_config BOOTSTRAPPING ACTION
 *
 * We can't just ditch the system file by default and roll
 * our own.  Instead, use the current file to find the actual
 * settings that need toggling, then toggle them!
 */
Franco Fichtner's avatar
Franco Fichtner committed
89

90
/* Include default configuration for OPNsense */
91 92 93 94
$sshconf = "# This file was automatically generated by /usr/local/etc/rc.sshd\n";
$sshconf .= "Port {$sshport}\n";
$sshconf .= "Protocol 2\n";
/* XXX a couple of those need moar cleanups: */
Franco Fichtner's avatar
Franco Fichtner committed
95 96 97 98
$sshconf .= "Compression yes\n";
$sshconf .= "ClientAliveInterval 30\n";
$sshconf .= "UseDNS no\n";
$sshconf .= "X11Forwarding no\n";
99 100 101 102 103
$sshconf .= "PubkeyAuthentication yes\n";
$sshconf .= "Subsystem\tsftp\tinternal-sftp\n";
if (isset($config['system']['ssh']['permitrootlogin'])) {
	$sshconf .= "PermitRootLogin yes\n";
}
104
if (isset($config['system']['ssh']['passwordauth'])) {
Franco Fichtner's avatar
Franco Fichtner committed
105
	$sshconf .= "ChallengeResponseAuthentication yes\n";
106 107 108 109
	$sshconf .= "PasswordAuthentication yes\n";
} else {
	$sshconf .= "ChallengeResponseAuthentication no\n";
	$sshconf .= "PasswordAuthentication no\n";
Franco Fichtner's avatar
Franco Fichtner committed
110 111 112
}

/* Write the new sshd config file */
113
file_put_contents("{$etc_ssh}/sshd_config", $sshconf);
Franco Fichtner's avatar
Franco Fichtner committed
114 115

/* are we already running?  if so exit */
116
if (is_subsystem_dirty('sshdkeys')) {
Franco Fichtner's avatar
Franco Fichtner committed
117 118 119 120 121
	return;
}

// Check for all needed key files. If any are missing, the keys need to be regenerated.
$generate_keys = false;
122 123 124
foreach ($keys as $name) {
	$file = "{$etc_ssh}/{$name}";
	if (!file_exists($file) || !file_exists("{$file}.pub")) {
Franco Fichtner's avatar
Franco Fichtner committed
125 126 127 128 129 130
		$generate_keys = true;
		break;
	}
}

if ($generate_keys) {
131
	log_error(gettext('Started creating your SSH keys. SSH startup is being delayed a wee bit.'));
Franco Fichtner's avatar
Franco Fichtner committed
132
	mark_subsystem_dirty('sshdkeys');
133 134 135 136
	mwexec("/bin/rm -f {$etc_ssh}/ssh_host_*");
	foreach ($keys as $type => $name) {
		mwexec(sprintf('%s -t %s -N "" -f %s/%s', $bin_ssh_keygen, $type, $etc_ssh, $name));
	}
Franco Fichtner's avatar
Franco Fichtner committed
137
	clear_subsystem_dirty('sshdkeys');
138
	log_error(gettext('Completed creating your SSH keys. SSH will now be started.'));
Franco Fichtner's avatar
Franco Fichtner committed
139 140 141
}

/* Launch new server process */
142
echo "Reloading sshd...";
143
if (mwexecf('/usr/bin/protect -i %s', $sbin_sshd)) {
144
	echo "failed.\n";
Franco Fichtner's avatar
Franco Fichtner committed
145 146 147 148
} else {
	echo "done.\n";
}

149
/* back up files in case they are useful ;) */
150
@mkdir('/conf/sshd', 0777, true);
151
mwexec("/bin/cp -p ${etc_ssh}/ssh_host_* /conf/sshd/");