rc.sshd 4.7 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 33 34 35
require_once('globals.inc');
require_once('config.inc');
require_once('functions.inc');
require_once('shaper.inc');
Franco Fichtner's avatar
Franco Fichtner committed
36

37 38
killbyname('sshd');

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

$keys = array(
44 45 46 47 48 49
	/* .pub files are implied */
	'rsa1' => 'ssh_host_key',
	'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
50 51
);

52 53 54
$bin_ssh_keygen = '/usr/local/bin/ssh-keygen';
$sbin_sshd = '/usr/local/sbin/sshd';
$etc_ssh = '/usr/local/etc/ssh';
55 56 57 58 59 60 61 62 63 64 65 66 67 68

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

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
69 70 71
	}
}

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

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

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

/*
 * 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
91

92
/* Include default configuration for OPNsense */
93 94 95 96
$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
97 98 99 100
$sshconf .= "Compression yes\n";
$sshconf .= "ClientAliveInterval 30\n";
$sshconf .= "UseDNS no\n";
$sshconf .= "X11Forwarding no\n";
101 102 103 104 105
$sshconf .= "PubkeyAuthentication yes\n";
$sshconf .= "Subsystem\tsftp\tinternal-sftp\n";
if (isset($config['system']['ssh']['permitrootlogin'])) {
	$sshconf .= "PermitRootLogin yes\n";
}
106
if (isset($config['system']['ssh']['passwordauth'])) {
Franco Fichtner's avatar
Franco Fichtner committed
107
	$sshconf .= "ChallengeResponseAuthentication yes\n";
108 109 110 111
	$sshconf .= "PasswordAuthentication yes\n";
} else {
	$sshconf .= "ChallengeResponseAuthentication no\n";
	$sshconf .= "PasswordAuthentication no\n";
Franco Fichtner's avatar
Franco Fichtner committed
112 113 114
}

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

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

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

if ($generate_keys) {
133
	log_error(_('Started creating your SSH keys. SSH startup is being delayed a wee bit.'));
Franco Fichtner's avatar
Franco Fichtner committed
134
	mark_subsystem_dirty('sshdkeys');
135 136 137 138
	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
139
	clear_subsystem_dirty('sshdkeys');
140
	log_error(_('Completed creating your SSH keys. SSH will now be started.'));
Franco Fichtner's avatar
Franco Fichtner committed
141 142 143
}

/* Launch new server process */
144
echo "Reloading sshd...";
145
if (mwexec($sbin_sshd)) {
146
	echo "failed.\n";
Franco Fichtner's avatar
Franco Fichtner committed
147 148 149 150
} else {
	echo "done.\n";
}

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

Franco Fichtner's avatar
Franco Fichtner committed
155
unset($keys);