Commit a1a316b7 authored by Franco Fichtner's avatar Franco Fichtner

rc: rework sshd start/stop script

parent c076d1c7
...@@ -2,9 +2,8 @@ ...@@ -2,9 +2,8 @@
<?php <?php
/* /*
Copyright 2004 Scott K Ullrich Copyright (C) 2004 Scott K Ullrich
Copyright (C) 2004 Fred Mol <fredmol@xs4all.nl>.
Original Copyright (C) 2004 Fred Mol <fredmol@xs4all.nl>.
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
...@@ -34,6 +33,8 @@ require_once("config.inc"); ...@@ -34,6 +33,8 @@ require_once("config.inc");
require_once("functions.inc"); require_once("functions.inc");
require_once("shaper.inc"); require_once("shaper.inc");
killbyname('sshd');
if (!isset($config['system']['enablesshd'])) { if (!isset($config['system']['enablesshd'])) {
return; return;
} }
...@@ -43,6 +44,10 @@ if (!is_subsystem_dirty('sshdkeys')) { ...@@ -43,6 +44,10 @@ if (!is_subsystem_dirty('sshdkeys')) {
conf_mount_rw(); conf_mount_rw();
} }
if (file_exists('/conf/sshd/ssh_host_key') && !file_exists('/etc/ssh/ssh_host_key')) {
mwexec('/bin/cp -p /conf/sshd/* /etc/ssh/');
}
$keys = array( $keys = array(
'ssh_host_key', 'ssh_host_key',
'ssh_host_key.pub', 'ssh_host_key.pub',
...@@ -56,93 +61,61 @@ $keys = array( ...@@ -56,93 +61,61 @@ $keys = array(
'ssh_host_ed25519_key.pub' 'ssh_host_ed25519_key.pub'
); );
/* restore ssh data for nanobsd platform */
if($g['platform'] == "nanobsd" and file_exists("/conf/sshd/ssh_host_key") and !file_exists("/etc/ssh/ssh_host_key.pub")) {
echo "Restoring SSH from /conf/sshd/";
exec("/bin/cp -p /conf/sshd/* /etc/ssh/");
/* make sure host private key permissions aren't too open so sshd won't complain */
foreach($keys as $f2c) {
if(file_exists("/etc/ssh/{$f2c}"))
chmod("/etc/ssh/{$f2c}", 0600);
}
}
/* if any of these files are 0 bytes then they are corrupted.
* remove them
*/
foreach($keys as $f2c) { foreach($keys as $f2c) {
if (file_exists("/etc/ssh/{$f2c}") && filesize("/etc/ssh/{$f2c}") == 0) { if (file_exists("/etc/ssh/{$f2c}") && filesize("/etc/ssh/{$f2c}") == 0) {
unlink_if_exists('/etc/ssh/ssh_host*'); unlink("/etc/ssh/{$f2c}");
break;
} }
} }
if (!is_dir("/var/empty")) { /* make ssh home directory */
/* make ssh home directory */ @mkdir("/var/empty", 0555, true);
mkdir("/var/empty", 0555);
}
if(!file_exists("/var/log/lastlog")) { /* Login related files. */
/* Login related files. */ touch("/var/log/lastlog");
@touch("/var/log/lastlog");
}
$sshConfigDir = "/etc/ssh"; $sshConfigDir = "/etc/ssh";
if (is_array($config['system']['ssh']) && !empty($config['system']['ssh']['port'])) if (isset($config['system']['ssh']['port'])) {
$sshport = $config['system']['ssh']['port']; $sshport = $config['system']['ssh']['port'];
else } else {
$sshport = 22; $sshport = 22;
}
/*
* 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!
*/
/* Include default configuration for pfSense */ /* Include default configuration for pfSense */
$sshconf = "# This file is automatically generated at startup\n"; $sshconf = "# This file was automatically generated by /usr/local/etc/rc.sshd\n";
$sshconf .= "Ciphers aes128-ctr,aes256-ctr,arcfour256,arcfour,aes128-cbc,aes256-cbc\n"; $sshconf .= "Port {$sshport}\n";
$sshconf .= "PermitRootLogin yes\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";
$sshconf .= "X11Forwarding no\n"; $sshconf .= "X11Forwarding no\n";
$sshconf .= "PubkeyAuthentication yes\n";
$sshconf .= "Subsystem\tsftp\tinternal-sftp\n";
if (isset($config['system']['ssh']['permitrootlogin'])) {
$sshconf .= "PermitRootLogin yes\n";
}
if (isset($config['system']['ssh']['sshdkeyonly'])) { if (isset($config['system']['ssh']['sshdkeyonly'])) {
$sshconf .= "# Login via Key only\n";
$sshconf .= "PasswordAuthentication no\n"; $sshconf .= "PasswordAuthentication no\n";
$sshconf .= "ChallengeResponseAuthentication no\n"; $sshconf .= "ChallengeResponseAuthentication no\n";
$sshconf .= "PubkeyAuthentication yes\n";
} else { } else {
$sshconf .= "# Login via Key and Password\n";
$sshconf .= "PasswordAuthentication yes\n"; $sshconf .= "PasswordAuthentication yes\n";
$sshconf .= "ChallengeResponseAuthentication yes\n"; $sshconf .= "ChallengeResponseAuthentication yes\n";
$sshconf .= "PubkeyAuthentication yes\n";
} }
$sshconf .= "# override default of no subsystems\n";
$sshconf .= "Subsystem sftp /usr/libexec/sftp-server\n";
/* Only allow protocol 2, because we say so */
$sshconf .= "Protocol 2\n";
/* Run the server on another port if we have one defined */
$sshconf .= "Port $sshport\n";
/* Hide FreeBSD version */
$sshconf .= "VersionAddendum \n";
/* Write the new sshd config file */ /* Write the new sshd config file */
@file_put_contents("/etc/ssh/sshd_config", $sshconf); file_put_contents('/etc/ssh/sshd_config', $sshconf);
/* mop up from a badly implemented ssh keys -> cf backup */
if($config['ssh']['dsa_key'] <> "") {
unset($config['ssh']['dsa_key']);
unset($config['ssh']['ecdsa_key']);
unset($config['ssh']['ed25519_key']);
unset($config['ssh']['rsa_key']);
unset($config['ssh']['rsa1_key']);
unset($config['ssh']['dsa']);
unset($config['ssh']['rsa']);
unset($config['ssh']['rsa1']);
unset($config['ssh']['ak']);
write_config("Clearing SSH keys from config.xml");
}
/* are we already running? if so exit */ /* are we already running? if so exit */
if(is_subsystem_dirty('sshdkeys')) { if (is_subsystem_dirty('sshdkeys')) {
unset($keys);
return; return;
} }
...@@ -156,40 +129,29 @@ foreach ($keys as $f2c) { ...@@ -156,40 +129,29 @@ foreach ($keys as $f2c) {
} }
if ($generate_keys) { if ($generate_keys) {
/* remove previous keys and regen later */ log_error(_('Started creating your SSH keys. SSH startup is being delayed a wee bit.'));
file_notice("SSH", "{$g['product_name']} has started creating your SSH keys. SSH Startup will be delayed. Please note that reloading the filter rules and changes will be delayed until this operation is completed.", "SSH KeyGen", "");
unlink_if_exists('/etc/ssh/ssh_host_*');
mark_subsystem_dirty('sshdkeys'); mark_subsystem_dirty('sshdkeys');
echo " Generating Keys:\n"; mwexec('/bin/rm /etc/ssh/ssh_host_*', true);
$_gb = exec("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa1 -N '' -f $sshConfigDir/ssh_host_key"); mwexec("/usr/bin/ssh-keygen -t rsa1 -N '' -f {$sshConfigDir}/ssh_host_key");
$_gb = exec("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa -N '' -f $sshConfigDir/ssh_host_rsa_key"); mwexec("/usr/bin/ssh-keygen -t rsa -N '' -f {$sshConfigDir}/ssh_host_rsa_key");
$_gb = exec("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t dsa -N '' -f $sshConfigDir/ssh_host_dsa_key"); mwexec("/usr/bin/ssh-keygen -t dsa -N '' -f {$sshConfigDir}/ssh_host_dsa_key");
$_gb = exec("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t ecdsa -N '' -f $sshConfigDir/ssh_host_ecdsa_key"); mwexec("/usr/bin/ssh-keygen -t ecdsa -N '' -f {$sshConfigDir}/ssh_host_ecdsa_key");
$_gb = exec("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t ed25519 -N '' -f $sshConfigDir/ssh_host_ed25519_key"); mwexec("/usr/bin/ssh-keygen -t ed25519 -N '' -f {$sshConfigDir}/ssh_host_ed25519_key");
clear_subsystem_dirty('sshdkeys'); clear_subsystem_dirty('sshdkeys');
file_notice("SSH", "{$g['product_name']} has completed creating your SSH keys. SSH is now started.", "SSH Startup", ""); log_error(_('Completed creating your SSH keys. SSH will now be started.'));
} }
/* kill existing sshd process, server only, not the childs */
$sshd_pid = exec("ps ax | egrep '/usr/sbin/[s]shd' | awk '{print $1}'");
if($sshd_pid <> "") {
echo "stopping ssh process $sshd_pid \n";
@posix_kill($sshd_pid, SIGTERM);
}
/* Launch new server process */ /* Launch new server process */
$status = mwexec("/usr/sbin/sshd"); echo "Reloading sshd...";
if($status <> 0) { if (mwexec('/usr/sbin/sshd')) {
file_notice("sshd_startup", "SSHD failed to start.", "SSHD Daemon", ""); echo "failed.\n";
echo "error!\n";
} else { } else {
echo "done.\n"; echo "done.\n";
} }
// NanoBSD /* back up files in case they are useful */
if($g['platform'] == "nanobsd") { @mkdir('/conf/sshd', 0777, true);
if(!is_dir("/conf/sshd")) mwexec('/bin/cp -p /etc/ssh/ssh_host* /conf/sshd');
mkdir("/conf/sshd", 0750);
$_gb = exec("/bin/cp -p /etc/ssh/ssh_host* /conf/sshd");
}
conf_mount_ro(); conf_mount_ro();
unset($keys); unset($keys);
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