rc.sshd 4.86 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');

Franco Fichtner's avatar
Franco Fichtner committed
39 40 41 42 43 44 45 46 47 48
if (!isset($config['system']['enablesshd'])) {
	return;
}

/* are we already running?  if not, do conf_mount_rw(), otherwise it should already be rw */
if (!is_subsystem_dirty('sshdkeys')) {
	conf_mount_rw();
}

$keys = array(
49 50 51 52 53 54
	/* .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
55 56
);

57 58 59
$bin_ssh_keygen = '/usr/local/bin/ssh-keygen';
$sbin_sshd = '/usr/local/sbin/sshd';
$etc_ssh = '/usr/local/etc/ssh';
60 61 62 63 64 65 66 67 68 69 70 71 72 73

/* 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
74 75 76
	}
}

77 78
/* make ssh home directory */
@mkdir("/var/empty", 0555, true);
Franco Fichtner's avatar
Franco Fichtner committed
79

80 81
/* Login related files. */
touch("/var/log/lastlog");
Franco Fichtner's avatar
Franco Fichtner committed
82

83
if (isset($config['system']['ssh']['port'])) {
Franco Fichtner's avatar
Franco Fichtner committed
84
	$sshport = $config['system']['ssh']['port'];
85
} else {
Franco Fichtner's avatar
Franco Fichtner committed
86
	$sshport = 22;
87 88 89 90 91 92 93 94 95
}

/*
 * 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
96 97

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

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

/* are we already running?  if so exit */
123
if (is_subsystem_dirty('sshdkeys')) {
Franco Fichtner's avatar
Franco Fichtner committed
124 125 126 127 128
	return;
}

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

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

/* Launch new server process */
149
echo "Reloading sshd...";
150
if (mwexec($sbin_sshd)) {
151
	echo "failed.\n";
Franco Fichtner's avatar
Franco Fichtner committed
152 153 154 155
} else {
	echo "done.\n";
}

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

Franco Fichtner's avatar
Franco Fichtner committed
160 161
conf_mount_ro();
unset($keys);