Project

General

Profile

Download (6.36 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php-cgi -f
2
<?php
3
/*
4
 * sshd
5
 *
6
 * part of pfSense (https://www.pfsense.org)
7
 * Copyright (c) 2004 Fred Mol <fredmol@xs4all.nl>.
8
 * Copyright (c) 2004-2018 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * Licensed under the Apache License, Version 2.0 (the "License");
12
 * you may not use this file except in compliance with the License.
13
 * You may obtain a copy of the License at
14
 *
15
 * http://www.apache.org/licenses/LICENSE-2.0
16
 *
17
 * Unless required by applicable law or agreed to in writing, software
18
 * distributed under the License is distributed on an "AS IS" BASIS,
19
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
 * See the License for the specific language governing permissions and
21
 * limitations under the License.
22
 */
23

    
24
require_once("globals.inc");
25
require_once("config.inc");
26
require_once("functions.inc");
27
require_once("shaper.inc");
28

    
29
if (!isset($config['system']['enablesshd'])) {
30
	return;
31
}
32

    
33
$sshConfigDir = "/etc/ssh";
34

    
35
$keys = array(
36
	array('type' => 'rsa',     'suffix' => 'rsa_'),
37
	array('type' => 'ed25519', 'suffix' => 'ed25519_')
38
);
39

    
40
$keyfiles = array();
41
foreach ($keys as $key) {
42
	$keyfiles[] = "ssh_host_{$key['suffix']}key";
43
	$keyfiles[] = "ssh_host_{$key['suffix']}key.pub";
44
}
45

    
46
/*    if any of these files are 0 bytes then they are corrupted.
47
 *    remove them
48
 */
49
foreach ($keyfiles as $f2c) {
50
	if (!file_exists("{$sshConfigDir}/{$f2c}") || filesize("{$sshConfigDir}/{$f2c}") == 0) {
51
		/* Make sure we remove both files */
52
		unlink_if_exists($sshConfigDir . '/' . basename($f2c, ".pub"));
53
		unlink_if_exists($sshConfigDir . '/' . $f2c);
54
	}
55
}
56

    
57
if (!is_dir("/var/empty")) {
58
	/* make ssh home directory */
59
	mkdir("/var/empty", 0555);
60
}
61

    
62
if (!file_exists("/var/log/lastlog")) {
63
	/* Login related files. */
64
	@touch("/var/log/lastlog");
65
}
66

    
67
if (is_array($config['system']['ssh']) && !empty($config['system']['ssh']['port'])) {
68
	$sshport = $config['system']['ssh']['port'];
69
} else {
70
	$sshport = 22;
71
}
72

    
73
/* Include default configuration for pfSense */
74
/* Taken from https://stribika.github.io/2015/01/04/secure-secure-shell.html */
75
$sshconf = "# This file is automatically generated at startup\n";
76
$sshconf .= "KexAlgorithms curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256\n";
77
/* Run the server on another port if we have one defined */
78
$sshconf .= "Port $sshport\n";
79
/* Only allow protocol 2, because we say so */
80
$sshconf .= "Protocol 2\n";
81
foreach ($keys as $key) {
82
	$sshconf .= "HostKey {$sshConfigDir}/ssh_host_{$key['suffix']}key\n";
83
}
84
$sshconf .= "Compression delayed\n";
85
$sshconf .= "ClientAliveInterval 30\n";
86
$sshconf .= "PermitRootLogin yes\n";
87
if ($config['system']['ssh']['sshdkeyonly'] == "both") {
88
	$sshconf .= "# Login via both Key and Password only\n";
89
	$sshconf .= "AuthenticationMethods publickey,password\n";
90
	$sshconf .= "ChallengeResponseAuthentication yes\n";
91
	$sshconf .= "PasswordAuthentication yes\n";
92
	$sshconf .= "PubkeyAuthentication yes\n";
93
	$sshconf .= "UsePAM yes\n";
94
} else if (isset($config['system']['ssh']['sshdkeyonly'])) {
95
	$sshconf .= "# Login via Key only\n";
96
	$sshconf .= "ChallengeResponseAuthentication no\n";
97
	$sshconf .= "PasswordAuthentication no\n";
98
	$sshconf .= "PubkeyAuthentication yes\n";
99
	$sshconf .= "UsePAM no\n";
100
} else {
101
	$sshconf .= "# Login via Key or Password\n";
102
	$sshconf .= "ChallengeResponseAuthentication yes\n";
103
	$sshconf .= "PasswordAuthentication yes\n";
104
	$sshconf .= "PubkeyAuthentication yes\n";
105
}
106
$sshconf .= "UseDNS no\n";
107
$sshconf .= "LoginGraceTime 30s\n";
108
/* Hide FreeBSD version */
109
$sshconf .= "VersionAddendum none\n";
110
$sshconf .= "X11Forwarding no\n";
111
$sshconf .= "Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr\n";
112
$sshconf .= "MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-ripemd160-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512,hmac-sha2-256,hmac-ripemd160,umac-128@openssh.com\n";
113
$sshconf .= "# override default of no subsystems\n";
114
$sshconf .= "Subsystem\tsftp\t/usr/libexec/sftp-server\n";
115

    
116
/* Apply package SSHDCond settings if config file exists */
117
if (file_exists("/etc/sshd_extra")) {
118
	$fdExtra = fopen("/etc/sshd_extra", 'r');
119
	$szExtra = fread($fdExtra, 1048576); // Read up to 1MB from extra file
120
	$sshconf .= $szExtra;
121
	fclose($fdExtra);
122
}
123

    
124
/* Write the new sshd config file */
125
@file_put_contents("{$sshConfigDir}/sshd_config", $sshconf);
126

    
127
/* mop up from a badly implemented ssh keys -> cf backup */
128
if ($config['ssh']['dsa_key'] <> "") {
129
	unset($config['ssh']['dsa_key']);
130
	unset($config['ssh']['ecdsa_key']);
131
	unset($config['ssh']['ed25519_key']);
132
	unset($config['ssh']['rsa_key']);
133
	unset($config['ssh']['rsa1_key']);
134
	unset($config['ssh']['dsa']);
135
	unset($config['ssh']['rsa']);
136
	unset($config['ssh']['rsa1']);
137
	unset($config['ssh']['ak']);
138
	write_config("Clearing SSH keys from config.xml");
139
}
140

    
141
/* are we already running?  if so exit */
142
if (is_subsystem_dirty('sshdkeys')) {
143
	unset($keys, $keyfiles);
144
	return;
145
}
146

    
147
// Check for all needed key files. If any are missing, the keys need to be regenerated.
148
$generate_keys = array();
149
foreach ($keys as $key) {
150
	if (!file_exists("{$sshConfigDir}/ssh_host_{$key['suffix']}key") ||
151
	    !file_exists("{$sshConfigDir}/ssh_host_{$key['suffix']}key.pub")) {
152
		$generate_keys[] = $key;
153
	}
154
}
155

    
156
if (!empty($generate_keys)) {
157
	/* remove previous keys and regen later */
158
	file_notice("SSH", "{$g['product_name']} has started creating missing 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", "");
159
	mark_subsystem_dirty('sshdkeys');
160
	echo " Generating Keys:\n";
161
	foreach ($generate_keys as $key) {
162
		$_gb = exec("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t {$key['type']} -b 4096 -N '' -f {$sshConfigDir}/ssh_host_{$key['suffix']}key");
163
	}
164
	clear_subsystem_dirty('sshdkeys');
165
	file_notice("SSH", "{$g['product_name']} has completed creating your SSH keys.  SSH is now started.", "SSH Startup", "");
166
}
167

    
168
/* kill existing sshd process, server only, not the childs */
169
$sshd_pid = exec("ps ax | egrep '/usr/sbin/[s]shd' | awk '{print $1}'");
170
if ($sshd_pid <> "") {
171
	echo "stopping ssh process $sshd_pid \n";
172
	@posix_kill($sshd_pid, SIGTERM);
173
}
174
/* Launch new server process */
175
$status = mwexec("/usr/sbin/sshd");
176
if ($status <> 0) {
177
	file_notice("sshd_startup", "SSHD failed to start.", "SSHD Daemon", "");
178
	echo "error!\n";
179
} else {
180
	echo "done.\n";
181
}
182

    
183
unset($keys, $keyfiles);
184
?>
(78-78/79)