Commit 526779bb authored by Franco Fichtner's avatar Franco Fichtner

rc: merge ssh changes from master

PR: https://github.com/opnsense/core/issues/1200
parent 3fe64a5f
...@@ -2,42 +2,38 @@ ...@@ -2,42 +2,38 @@
<?php <?php
/* /*
Copyright (C) 2004 Scott K Ullrich * Copyright (C) 2004 Scott K Ullrich
Copyright (C) 2004 Fred Mol <fredmol@xs4all.nl>. * Copyright (C) 2004 Fred Mol <fredmol@xs4all.nl>.
Copyright (C) 2015-2016 Franco Fichtner <franco@opnsense.org> * Copyright (C) 2015-2016 Franco Fichtner <franco@opnsense.org>
All rights reserved. * All rights reserved.
*
Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
*
1. Redistributions of source code must retain the above copyright notice, * 1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer. * this list of conditions and the following disclaimer.
*
2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
*
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * 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 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
require_once('config.inc'); require_once('config.inc');
require_once("util.inc"); require_once("util.inc");
$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 */ /* if run from a shell session, `-af' and the full path is needed */
mwexecf('/bin/pkill -af %s', $sbin_sshd); mwexecf('/bin/pkill -af %s', '/usr/local/sbin/sshd', true);
$sshcfg = null; $sshcfg = null;
...@@ -47,17 +43,21 @@ if (isset($config['system']['ssh'])) { ...@@ -47,17 +43,21 @@ if (isset($config['system']['ssh'])) {
} }
} elseif (count($argv) > 1 && $argv[1] == 'installer') { } elseif (count($argv) > 1 && $argv[1] == 'installer') {
/* only revert to installer config when ssh is not set at all */ /* only revert to installer config when ssh is not set at all */
$sshcfg = array( 'permitrootlogin' => 1, 'passwordauth' => 1); $sshcfg = array('permitrootlogin' => 1, 'passwordauth' => 1);
} }
if ($sshcfg === null) { if ($sshcfg === null) {
return; return;
} }
/* reinstall the backup if it is available */ /* make sshd key store */
if (file_exists('/conf/sshd/ssh_host_rsa_key') && !file_exists("{$etc_ssh}/ssh_host_rsa_key")) { @mkdir('/conf/sshd', 0777, true);
mwexec("/bin/cp -p /conf/sshd/* {$etc_ssh}/");
} /* make ssh home directory */
@mkdir('/var/empty', 0555, true);
/* Login related files. */
touch('/var/log/lastlog');
$keys = array( $keys = array(
/* .pub files are implied */ /* .pub files are implied */
...@@ -66,29 +66,44 @@ $keys = array( ...@@ -66,29 +66,44 @@ $keys = array(
'ed25519' => 'ssh_host_ed25519_key', 'ed25519' => 'ssh_host_ed25519_key',
); );
foreach($keys as $name) { $keys_dep = array(
$file = "{$etc_ssh}/etc/ssh/{$name}"; /* .pub files are implied */
if (file_exists($file) && filesize($file) == 0) { 'dsa' => 'ssh_host_dsa_key',
unlink($file); );
}
$file = "{$file}.pub"; $keys_all = array_merge($keys, $keys_dep);
if (file_exists($file) && filesize($file) == 0) {
unlink($file); /* Check for all needed key files. If any are missing, the keys need to be regenerated. */
$generate_keys = false;
foreach ($keys as $name) {
$file = "/conf/sshd/{$name}";
if (!file_exists($file) || !file_exists("{$file}.pub")) {
$generate_keys = true;
break;
} }
} }
/* make ssh home directory */ if ($generate_keys) {
@mkdir("/var/empty", 0555, true); if (is_subsystem_dirty('sshdkeys')) {
return;
/* Login related files. */ }
touch("/var/log/lastlog"); log_error('Started creating your SSH keys. SSH startup is being delayed a wee bit.');
mark_subsystem_dirty('sshdkeys');
foreach ($keys as $type => $name) {
$file = "/conf/sshd/{$name}";
@unlink("{$file}.pub");
@unlink($file);
mwexecf('/usr/local/bin/ssh-keygen -t %s -N "" -f %s', array($type, $file));
}
clear_subsystem_dirty('sshdkeys');
log_error('Completed creating your SSH keys. SSH will now be started.');
}
$sshport = isset($sshcfg['port']) ? $sshcfg['port'] : 22; $sshport = isset($sshcfg['port']) ? $sshcfg['port'] : 22;
$sshconf = "# This file was automatically generated by /usr/local/etc/rc.sshd\n"; $sshconf = "# This file was automatically generated by /usr/local/etc/rc.sshd\n";
$sshconf .= "Port {$sshport}\n"; $sshconf .= "Port {$sshport}\n";
$sshconf .= "Protocol 2\n"; $sshconf .= "Protocol 2\n";
/* XXX a couple of those need moar cleanups: */
$sshconf .= "Compression yes\n"; $sshconf .= "Compression yes\n";
$sshconf .= "ClientAliveInterval 30\n"; $sshconf .= "ClientAliveInterval 30\n";
$sshconf .= "UseDNS no\n"; $sshconf .= "UseDNS no\n";
...@@ -105,44 +120,21 @@ if (isset($sshcfg['passwordauth'])) { ...@@ -105,44 +120,21 @@ if (isset($sshcfg['passwordauth'])) {
$sshconf .= "ChallengeResponseAuthentication no\n"; $sshconf .= "ChallengeResponseAuthentication no\n";
$sshconf .= "PasswordAuthentication no\n"; $sshconf .= "PasswordAuthentication no\n";
} }
foreach ($keys_all as $name) {
/* Write the new sshd config file */ $file = "/conf/sshd/{$name}";
file_put_contents("{$etc_ssh}/sshd_config", $sshconf); if (!file_exists($file)) {
continue;
/* are we already running? if so exit */
if (is_subsystem_dirty('sshdkeys')) {
return;
}
// Check for all needed key files. If any are missing, the keys need to be regenerated.
$generate_keys = false;
foreach ($keys as $name) {
$file = "{$etc_ssh}/{$name}";
if (!file_exists($file) || !file_exists("{$file}.pub")) {
$generate_keys = true;
break;
} }
$sshconf .= "HostKey {$file}\n";
} }
if ($generate_keys) { /* Write the new sshd config file */
log_error('Started creating your SSH keys. SSH startup is being delayed a wee bit.'); file_put_contents("/usr/local/etc/ssh/sshd_config", $sshconf);
mark_subsystem_dirty('sshdkeys');
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));
}
clear_subsystem_dirty('sshdkeys');
log_error('Completed creating your SSH keys. SSH will now be started.');
}
/* Launch new server process */ /* Launch new server process */
echo "Reloading sshd..."; echo "Reloading sshd...";
if (mwexecf('/usr/bin/protect -i %s', $sbin_sshd)) { if (mwexecf('/usr/bin/protect -i /usr/local/sbin/sshd')) {
echo "failed.\n"; echo "failed.\n";
} else { } else {
echo "done.\n"; echo "done.\n";
} }
/* back up files in case they are useful ;) */
@mkdir('/conf/sshd', 0777, true);
mwexec("/bin/cp -p ${etc_ssh}/ssh_host_* /conf/sshd/");
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment