Project

General

Profile

Download (9.69 KB) Statistics
| Branch: | Tag: | Revision:
1
#! /usr/local/bin/php -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
	require_once("config.inc");
32
	require_once("notices.inc");
33

    
34
	if(isset($config['system']['enablesshd'])) {
35
		/* do nothing, we're enabled */
36
	} else {
37
		if($g['booting'])
38
			echo "SSHD is disabled.";
39
		exit;
40
	}
41

    
42
	function file_size($file) {
43
	  $size = filesize($file);
44
	  return $size;
45
	}
46

    
47
	/* restore ssh data for nanobsd platform */
48
	if($g['platform'] == "nanobsd" and file_exists("/config/ssh/ssh_host_key")) {
49
		if(!file_exists("/etc/ssh/sshd_host_key.pub")) {
50
			cp("/config/ssh/* /etc/ssh/");
51
		}
52
	}
53

    
54
	/*    if any of these files are 0 bytes then they are corrupted.
55
	 *    remove them
56
	 */
57
	$files_to_check = array('ssh_host_dsa_key','ssh_host_dsa_key.pub','ssh_host_key','ssh_host_key.pub','ssh_host_rsa_key','ssh_host_rsa_key.pub','/root/.ssh/authorized_keys');
58
	foreach($files_to_check as $f2c) {
59
		if(file_exists("/etc/ssh/{$f2c}"))
60
			if(file_size("/etc/ssh/{$f2c}")==0) {
61
				mwexec("rm /etc/ssh/ssh_host*");
62
			}
63
	}
64

    
65
	if (!is_dir("/var/empty")) {
66
		/* make ssh home directory */
67
		mkdir("/var/empty", 0555);
68
	}
69

    
70
	if(!file_exists("")) {
71
		/* Login related files. */
72
		touch("/var/log/lastlog");
73
	}
74

    
75
	/* Make the root passwords are the same as the web admin password. */
76
	conf_mount_rw();
77
	$fd = popen("/usr/sbin/pw usermod -n root -H 0", "w");
78
	fwrite($fd, $config['system']['password']);
79
	fclose($fd);
80

    
81
	/* Make the admin passwords are the same as the web admin password. */
82
	$fd = popen("/usr/sbin/pw usermod -n admin -H 0", "w");
83
	fwrite($fd, $config['system']['password']);
84
	fclose($fd);
85

    
86
	$sshConfigDir = "/etc/ssh";
87

    
88
	if($config['system']['ssh']['port'] <> "") {
89
		$sshport = $config['system']['ssh']['port'];
90
	} else {
91
		$sshport = 22;
92
	}
93

    
94
	/* Include default configuration for pfSense */
95
	$sshconf = "# This file is automatically generated at startup\n";
96
	$sshconf = "Ciphers aes128-ctr,aes256-ctr,arcfour256,arcfour,aes128-cbc,aes256-cbc\n";
97
	$sshconf .= "PermitRootLogin yes\n";
98
	$sshconf .= "Compression yes\n";
99
	$sshconf .= "ClientAliveInterval 30\n";
100
	$sshconf .= "UseDNS no\n";
101
	$sshconf .= "X11Forwarding no\n";
102
	if($config['system']['ssh']['sshdkeyonly'] <> "") {
103
		$sshconf .= "# Login via Key only\n";
104
		$sshconf .= "PasswordAuthentication no\n";
105
		$sshconf .= "ChallengeResponseAuthentication no\n";
106
		$sshconf .= "PubkeyAuthentication yes\n";
107
	} else {
108
		$sshconf .= "# Login via Key and Password\n";
109
		$sshconf .= "PasswordAuthentication yes\n";
110
		$sshconf .= "ChallengeResponseAuthentication yes\n";
111
		$sshconf .= "PubkeyAuthentication yes\n";
112
	}
113
	$sshconf .= "# override default of no subsystems\n";
114
	$sshconf .= "Subsystem       sftp    /usr/libexec/sftp-server\n";
115
	/* Only allow protocol 2, because we say so */
116
	$sshconf .= "Protocol 2\n";
117
	/* Run the server on another port if we have one defined */
118
	$sshconf .= "Port $sshport\n";
119

    
120
	/* Write the new sshd config file */
121
	$fd = fopen("/etc/ssh/sshd_config", "w");
122
	fwrite($fd, $sshconf);
123
	fclose($fd);
124

    
125
	if($config['system']['ssh']['authorizedkeys'] <> "") {
126
		echo "writing /root/.ssh/authorized_keys\n";
127
		if (!is_dir("/root/.ssh")) {
128
			mkdir('/root/.ssh', 0700);
129
		}
130
		$authorizedkeys  = "# This file is automatically generated at startup\n";
131
		$authorizedkeys .= base64_decode($config['system']['ssh']['authorizedkeys']);
132
		$fd = fopen("/root/.ssh/authorized_keys", "w");
133
		fwrite($fd, $authorizedkeys);
134
		pclose($fd);
135
		chmod("/root/.ssh/authorized_keys",0644);
136
	} else {
137
		if(file_exists("/root/.ssh/authorized_keys")) {
138
			unlink("/root/.ssh/authorized_keys");
139
		}
140
	} 
141

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

    
154
	/* are we already running?  if so exit */
155
	if(file_exists("/tmp/keys_generating"))
156
		exit;
157

    
158
	if (!file_exists("$sshConfigDir/ssh_host_key") or file_exists("/etc/keys_generating")) {
159
		/* remove previous keys and regen later */
160
		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", "");
161
		conf_mount_rw();
162
		mwexec("rm /etc/ssh/ssh_host_*");
163
		touch("/etc/keys_generating");
164
		touch("/tmp/keys_generating");
165
		echo " Generating Keys:\n";
166
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa1 -N '' -f $sshConfigDir/ssh_host_key");
167
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa -N '' -f $sshConfigDir/ssh_host_rsa_key");
168
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t dsa -N '' -f $sshConfigDir/ssh_host_dsa_key");
169
		unlink("/etc/keys_generating");
170
		unlink("/tmp/keys_generating");
171
		file_notice("SSH", "{$g['product_name']} has completed creating your SSH keys.  SSH is now started.", "SSH Startup", "");
172
		echo "Starting SSH... ";
173
	}
174

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

    
190
	// NanoBSD
191
	if($g['platform'] == "nanobsd") {
192
		if(!is_dir("/config/sshd"))
193
			exec("mkdir /config/sshd");
194
		exec("cp /etc/ssh/ssh_host* /config/sshd");
195
	}
196

    
197
	conf_mount_ro();
198

    
199

    
200

    
201

    
202

    
203

    
204

    
205

    
206

    
207

    
208

    
209

    
210

    
211

    
212

    
213

    
214

    
215

    
216

    
217

    
218

    
219

    
220

    
221

    
222

    
223

    
224

    
225

    
226

    
227

    
228

    
229

    
230

    
231

    
232

    
233

    
234

    
235

    
236
	exit;
237

    
238
	/* exit early, this needs more testing. */
239

    
240
	if (!file_exists("$sshConfigDir/ssh_host_key") and $config['ssh']['dsa'] == "") {
241
		/* generate keys */
242
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa1 -N '' -f $sshConfigDir/ssh_host_key");
243
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa -N '' -f $sshConfigDir/ssh_host_rsa_key");
244
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t dsa -N '' -f $sshConfigDir/ssh_host_dsa_key");
245

    
246
		/* save keys */
247
		$dsa  = file_get_contents("{$sshConfigDir}/ssh_host_dsa_key");
248
		$rsa  = file_get_contents("{$sshConfigDir}/ssh_host_rsa_key");
249
		$rsa1 = file_get_contents("{$sshConfigDir}/ssh_host_key");
250
		$config['ssh']['dsa'] = base64_encode($dsa);
251
		$config['ssh']['rsa'] = base64_encode($rsa);
252
		$config['ssh']['rsa1']= base64_encode($rsa1);
253

    
254
		/* save public keys */
255
		$dsapub  = file_get_contents("{$sshConfigDir}/ssh_host_dsa_key.pub");
256
		$rsapub  = file_get_contents("{$sshConfigDir}/ssh_host_rsa_key.pub");
257
		$rsa1pub = file_get_contents("{$sshConfigDir}/ssh_host_key.pub");
258
		$config['ssh']['dsa_key'] = base64_encode($dsapub);
259
		$config['ssh']['rsa_key'] = base64_encode($rsapub);
260
		$config['ssh']['rsa1_key']= base64_encode($rsa1pub);
261
		write_config("Saved SSH keys.");
262
	} else {
263
		/* restore keys */
264
		$rsa1 = base64_decode($config['ssh']['rsa1']);
265
		$rsa  = base64_decode($config['ssh']['rsa']);
266
		$dsa  = base64_decode($config['ssh']['dsa']);
267
		file_put_contents("{$sshConfigDir}/ssh_host_key", $rsa1);
268
		file_put_contents("{$sshConfigDir}/ssh_host_rsa_key", $rsa);
269
		file_put_contents("{$sshConfigDir}/ssh_host_dsa_key", $dsa);
270

    
271
		/* restore public keys */
272
		$rsa1_pub = base64_decode($config['ssh']['rsa1_key']);
273
		$rsa_pub  = base64_decode($config['ssh']['rsa_key']);
274
		$dsa_pub  = base64_decode($config['ssh']['dsa_key']);
275
		file_put_contents("{$sshConfigDir}/ssh_host_key.pub", $rsa1_pub);
276
		file_put_contents("{$sshConfigDir}/ssh_host_rsa_key.pub", $rsa_pub);
277
		file_put_contents("{$sshConfigDir}/ssh_host_dsa_key.pub", $dsa_pub);
278

    
279
		/* change keys owner to root */
280
		chown("{$sshConfigDir}/ssh_host_key", "root");
281
		chown("{$sshConfigDir}/ssh_host_rsa_key", "root");
282
		chown("{$sshConfigDir}/ssh_host_dsa_key", "root");
283

    
284
		/* change public keys owner to root */
285
		chown("{$sshConfigDir}/ssh_host_key.pub", "root");
286
		chown("{$sshConfigDir}/ssh_host_rsa_key.pub", "root");
287
		chown("{$sshConfigDir}/ssh_host_dsa_key.pub", "root");
288

    
289
		/* change mode on keys to u+rw */
290
		chmod("{$sshConfigDir}/ssh_host_key",0600);
291
		chmod("{$sshConfigDir}/ssh_host_rsa_key",0600);
292
		chmod("{$sshConfigDir}/ssh_host_dsa_key",0600);
293

    
294
		/* change mode on public keys to u+rw */
295
		chmod("{$sshConfigDir}/ssh_host_key.pub",0600);
296
		chmod("{$sshConfigDir}/ssh_host_rsa_key.pub",0600);
297
		chmod("{$sshConfigDir}/ssh_host_dsa_key.pub",0600);
298
	}
299

    
300
	/* start sshd */
301
	system("/usr/sbin/sshd");
302
	echo "done.\n";
303

    
304
?>
(74-74/81)