Project

General

Profile

Download (7.57 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php-cgi -f
2
<?php
3
/*
4
	sshd - Modified to work on disk based system
5
	Copyright 2004 Scott K Ullrich
6

    
7
	Original Copyright (C) 2004 Fred Mol <fredmol@xs4all.nl>.
8
	All rights reserved.
9

    
10
	Redistribution and use in source and binary forms, with or without
11
	modification, are permitted provided that the following conditions are met:
12

    
13
	1. Redistributions of source code must retain the above copyright notice,
14
	   this list of conditions and the following disclaimer.
15

    
16
	2. Redistributions in binary form must reproduce the above copyright
17
	   notice, this list of conditions and the following disclaimer in the
18
	   documentation and/or other materials provided with the distribution.
19

    
20
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
	POSSIBILITY OF SUCH DAMAGE.
30
*/
31

    
32
	require_once("globals.inc");
33
	require_once("config.inc");
34
	require_once("functions.inc");
35
	require_once("shaper.inc");
36

    
37
	if (!isset($config['system']['enablesshd'])) {
38
		return;
39
	}
40

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

    
46
	$sshConfigDir = "/etc/ssh";
47

    
48
	$keys = array(
49
		array('type' => 'rsa',     'suffix' => 'rsa_'),
50
		array('type' => 'ed25519', 'suffix' => 'ed25519_')
51
	);
52

    
53
	$keyfiles = array();
54
	foreach ($keys as $key) {
55
		$keyfiles[] = "ssh_host_{$key['suffix']}key";
56
		$keyfiles[] = "ssh_host_{$key['suffix']}key.pub";
57
	}
58

    
59
	/* restore ssh data for nanobsd platform */
60
	if ($g['platform'] == "nanobsd" and file_exists("/conf/sshd/ssh_host_key") and !file_exists("{$sshConfigDir}/ssh_host_key.pub")) {
61
		echo "Restoring SSH from /conf/sshd/";
62
		exec("/bin/cp -p /conf/sshd/* {$sshConfigDir}/");
63

    
64
		/* make sure host private key permissions aren't too open so sshd won't complain */
65
		foreach ($keyfiles as $f2c) {
66
			if (file_exists("{$sshConfigDir}/{$f2c}")) {
67
				chmod("{$sshConfigDir}/{$f2c}", 0600);
68
			}
69
		}
70
	}
71

    
72
	/*    if any of these files are 0 bytes then they are corrupted.
73
	 *    remove them
74
	 */
75
	foreach ($keyfiles as $f2c) {
76
		if (!file_exists("{$sshConfigDir}/{$f2c}") || filesize("{$sshConfigDir}/{$f2c}") == 0) {
77
			/* Make sure we remove both files */
78
			unlink_if_exists($sshConfigDir . '/' . basename($f2c, ".pub"));
79
			unlink_if_exists($sshConfigDir . '/' . $f2c);
80
		}
81
	}
82

    
83
	if (!is_dir("/var/empty")) {
84
		/* make ssh home directory */
85
		mkdir("/var/empty", 0555);
86
	}
87

    
88
	if (!file_exists("/var/log/lastlog")) {
89
		/* Login related files. */
90
		@touch("/var/log/lastlog");
91
	}
92

    
93
	if (is_array($config['system']['ssh']) && !empty($config['system']['ssh']['port'])) {
94
		$sshport = $config['system']['ssh']['port'];
95
	} else {
96
		$sshport = 22;
97
	}
98

    
99
	/* Include default configuration for pfSense */
100
	/* Taken from https://stribika.github.io/2015/01/04/secure-secure-shell.html */
101
	$sshconf = "# This file is automatically generated at startup\n";
102
	$sshconf .= "KexAlgorithms curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256\n";
103
	/* Run the server on another port if we have one defined */
104
	$sshconf .= "Port $sshport\n";
105
	/* Only allow protocol 2, because we say so */
106
	$sshconf .= "Protocol 2\n";
107
	foreach ($keys as $key) {
108
		$sshconf .= "HostKey {$sshConfigDir}/ssh_host_{$key['suffix']}key\n";
109
	}
110
	$sshconf .= "Compression yes\n";
111
	$sshconf .= "ClientAliveInterval 30\n";
112
	$sshconf .= "PermitRootLogin yes\n";
113
	if (isset($config['system']['ssh']['sshdkeyonly'])) {
114
		$sshconf .= "# Login via Key only\n";
115
		$sshconf .= "ChallengeResponseAuthentication no\n";
116
		$sshconf .= "PasswordAuthentication no\n";
117
		$sshconf .= "PubkeyAuthentication yes\n";
118
	} else {
119
		$sshconf .= "# Login via Key and Password\n";
120
		$sshconf .= "ChallengeResponseAuthentication yes\n";
121
		$sshconf .= "PasswordAuthentication yes\n";
122
		$sshconf .= "PubkeyAuthentication yes\n";
123
	}
124
	$sshconf .= "UseDNS no\n";
125
	$sshconf .= "UsePAM no\n";
126
	$sshconf .= "LoginGraceTime 30s\n";
127
	/* Hide FreeBSD version */
128
	$sshconf .= "VersionAddendum none\n";
129
	$sshconf .= "X11Forwarding no\n";
130
	$sshconf .= "Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr\n";
131
	$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";
132
	$sshconf .= "# override default of no subsystems\n";
133
	$sshconf .= "Subsystem\tsftp\t/usr/libexec/sftp-server\n";
134

    
135
	/* Apply package SSHDCond settings if config file exists */
136
	if (file_exists("/etc/sshd_extra")) {
137
		$fdExtra = fopen("/etc/sshd_extra", 'r');
138
		$szExtra = fread($fdExtra, 1048576); // Read up to 1MB from extra file
139
		$sshconf .= $szExtra;
140
		fclose($fdExtra);
141
	}
142

    
143
	/* Write the new sshd config file */
144
	@file_put_contents("{$sshConfigDir}/sshd_config", $sshconf);
145

    
146
	/* mop up from a badly implemented ssh keys -> cf backup */
147
	if ($config['ssh']['dsa_key'] <> "") {
148
		unset($config['ssh']['dsa_key']);
149
		unset($config['ssh']['ecdsa_key']);
150
		unset($config['ssh']['ed25519_key']);
151
		unset($config['ssh']['rsa_key']);
152
		unset($config['ssh']['rsa1_key']);
153
		unset($config['ssh']['dsa']);
154
		unset($config['ssh']['rsa']);
155
		unset($config['ssh']['rsa1']);
156
		unset($config['ssh']['ak']);
157
		write_config("Clearing SSH keys from config.xml");
158
	}
159

    
160
	/* are we already running?  if so exit */
161
	if (is_subsystem_dirty('sshdkeys')) {
162
		unset($keys, $keyfiles);
163
		return;
164
	}
165

    
166
	// Check for all needed key files. If any are missing, the keys need to be regenerated.
167
	$generate_keys = array();
168
	foreach ($keys as $key) {
169
		if (!file_exists("{$sshConfigDir}/ssh_host_{$key['suffix']}key") ||
170
		    !file_exists("{$sshConfigDir}/ssh_host_{$key['suffix']}key.pub")) {
171
			$generate_keys[] = $key;
172
		}
173
	}
174

    
175
	if (!empty($generate_keys)) {
176
		/* remove previous keys and regen later */
177
		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", "");
178
		mark_subsystem_dirty('sshdkeys');
179
		echo " Generating Keys:\n";
180
		foreach ($generate_keys as $key) {
181
			$_gb = exec("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t {$key['type']} -b 4096 -N '' -f {$sshConfigDir}/ssh_host_{$key['suffix']}key");
182
		}
183
		clear_subsystem_dirty('sshdkeys');
184
		file_notice("SSH", "{$g['product_name']} has completed creating your SSH keys.  SSH is now started.", "SSH Startup", "");
185
	}
186

    
187
	/* kill existing sshd process, server only, not the childs */
188
	$sshd_pid = exec("ps ax | egrep '/usr/sbin/[s]shd' | awk '{print $1}'");
189
	if ($sshd_pid <> "") {
190
		echo "stopping ssh process $sshd_pid \n";
191
		@posix_kill($sshd_pid, SIGTERM);
192
	}
193
	/* Launch new server process */
194
	$status = mwexec("/usr/sbin/sshd");
195
	if ($status <> 0) {
196
		file_notice("sshd_startup", "SSHD failed to start.", "SSHD Daemon", "");
197
		echo "error!\n";
198
	} else {
199
		echo "done.\n";
200
	}
201

    
202
	// NanoBSD
203
	if ($g['platform'] == "nanobsd") {
204
		if (!is_dir("/conf/sshd")) {
205
			mkdir("/conf/sshd", 0750);
206
		}
207
		$_gb = exec("/bin/cp -p {$sshConfigDir}/ssh_host* /conf/sshd");
208
	}
209
	conf_mount_ro();
210
	unset($keys, $keyfiles);
211
?>
(91-91/94)