Project

General

Profile

Download (9.21 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
	/*    if any of these files are 0 bytes then they are corrupted.
48
	 *    remove them
49
	 */
50
	$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');
51
	foreach($files_to_check as $f2c) {
52
		if(file_exists("/etc/ssh/{$f2c}"))
53
			if(file_size("/etc/ssh/{$f2c}")==0) {
54
				mwexec("rm /etc/ssh/ssh_host*");
55
			}
56
	}
57

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

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

    
68
	/* Make the root passwords are the same as the web admin password. */
69
	conf_mount_rw();
70
	$fd = popen("/usr/sbin/pw usermod -n root -H 0", "w");
71
	fwrite($fd, $config['system']['password']);
72
	fclose($fd);
73

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

    
79
	$sshConfigDir = "/etc/ssh";
80

    
81
	if($config['system']['ssh']['port'] <> "") {
82
		$sshport = $config['system']['ssh']['port'];
83
	} else {
84
		$sshport = 22;
85
	}
86

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

    
112
	/* Write the new sshd config file */
113
	$fd = fopen("/etc/ssh/sshd_config", "w");
114
	fwrite($fd, $sshconf);
115
	fclose($fd);
116

    
117
	if($config['system']['ssh']['authorizedkeys'] <> "") {
118
		echo "writing /root/.ssh/authorized_keys\n";
119
		if (!is_dir("/root/.ssh")) {
120
			mkdir('/root/.ssh', 0700);
121
		}
122
		$authorizedkeys  = "# This file is automatically generated at startup\n";
123
		$authorizedkeys .= base64_decode($config['system']['ssh']['authorizedkeys']);
124
		$fd = fopen("/root/.ssh/authorized_keys", "w");
125
		fwrite($fd, $authorizedkeys);
126
		pclose($fd);
127
		chmod("/root/.ssh/authorized_keys",0644);
128
	} else {
129
		if(file_exists("/root/.ssh/authorized_keys")) {
130
			unlink("/root/.ssh/authorized_keys");
131
		}
132
	} 
133

    
134
	/* mop up from a badly implemented ssh keys -> cf backup */
135
	if($config['ssh']['dsa_key'] <> "") {
136
		unset($config['ssh']['dsa_key']);
137
		unset($config['ssh']['rsa_key']);
138
		unset($config['ssh']['rsa1_key']);
139
		unset($config['ssh']['dsa']);
140
		unset($config['ssh']['rsa']);
141
		unset($config['ssh']['rsa1']);
142
		unset($config['ssh']['ak']);
143
		write_config("Clearing SSH keys from config.xml");
144
	}
145

    
146
	/* are we already running?  if so exit */
147
	if(file_exists("/tmp/keys_generating"))
148
		exit;
149

    
150
	if (!file_exists("$sshConfigDir/ssh_host_key") or file_exists("/etc/keys_generating")) {
151
		/* remove previous keys and regen later */
152
		file_notice("SSH", "pfSense 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", "");
153
		conf_mount_rw();
154
		mwexec("rm /etc/ssh/ssh_host_*");
155
		touch("/etc/keys_generating");
156
		touch("/tmp/keys_generating");
157
		echo " Generating Keys:\n";
158
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa1 -N '' -f $sshConfigDir/ssh_host_key");
159
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa -N '' -f $sshConfigDir/ssh_host_rsa_key");
160
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t dsa -N '' -f $sshConfigDir/ssh_host_dsa_key");
161
		unlink("/etc/keys_generating");
162
		unlink("/tmp/keys_generating");
163
		file_notice("SSH", "pfSense has completed creating your SSH keys.  SSH is now started.", "SSH Startup", "");
164
		echo "Starting SSH... ";
165
	}
166

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

    
182
	conf_mount_ro();
183

    
184

    
185

    
186

    
187

    
188

    
189

    
190

    
191

    
192

    
193

    
194

    
195

    
196

    
197

    
198

    
199

    
200

    
201

    
202

    
203

    
204

    
205

    
206

    
207

    
208

    
209

    
210

    
211

    
212

    
213

    
214

    
215

    
216

    
217

    
218

    
219

    
220

    
221
	exit;
222

    
223
	/* exit early, this needs more testing. */
224

    
225
	if (!file_exists("$sshConfigDir/ssh_host_key") and $config['ssh']['dsa'] == "") {
226
		/* generate keys */
227
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa1 -N '' -f $sshConfigDir/ssh_host_key");
228
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa -N '' -f $sshConfigDir/ssh_host_rsa_key");
229
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t dsa -N '' -f $sshConfigDir/ssh_host_dsa_key");
230

    
231
		/* save keys */
232
		$dsa  = file_get_contents("{$sshConfigDir}/ssh_host_dsa_key");
233
		$rsa  = file_get_contents("{$sshConfigDir}/ssh_host_rsa_key");
234
		$rsa1 = file_get_contents("{$sshConfigDir}/ssh_host_key");
235
		$config['ssh']['dsa'] = base64_encode($dsa);
236
		$config['ssh']['rsa'] = base64_encode($rsa);
237
		$config['ssh']['rsa1']= base64_encode($rsa1);
238

    
239
		/* save public keys */
240
		$dsapub  = file_get_contents("{$sshConfigDir}/ssh_host_dsa_key.pub");
241
		$rsapub  = file_get_contents("{$sshConfigDir}/ssh_host_rsa_key.pub");
242
		$rsa1pub = file_get_contents("{$sshConfigDir}/ssh_host_key.pub");
243
		$config['ssh']['dsa_key'] = base64_encode($dsapub);
244
		$config['ssh']['rsa_key'] = base64_encode($rsapub);
245
		$config['ssh']['rsa1_key']= base64_encode($rsa1pub);
246
		write_config("Saved SSH keys.");
247
	} else {
248
		/* restore keys */
249
		$rsa1 = base64_decode($config['ssh']['rsa1']);
250
		$rsa  = base64_decode($config['ssh']['rsa']);
251
		$dsa  = base64_decode($config['ssh']['dsa']);
252
		file_put_contents("{$sshConfigDir}/ssh_host_key", $rsa1);
253
		file_put_contents("{$sshConfigDir}/ssh_host_rsa_key", $rsa);
254
		file_put_contents("{$sshConfigDir}/ssh_host_dsa_key", $dsa);
255

    
256
		/* restore public keys */
257
		$rsa1_pub = base64_decode($config['ssh']['rsa1_key']);
258
		$rsa_pub  = base64_decode($config['ssh']['rsa_key']);
259
		$dsa_pub  = base64_decode($config['ssh']['dsa_key']);
260
		file_put_contents("{$sshConfigDir}/ssh_host_key.pub", $rsa1_pub);
261
		file_put_contents("{$sshConfigDir}/ssh_host_rsa_key.pub", $rsa_pub);
262
		file_put_contents("{$sshConfigDir}/ssh_host_dsa_key.pub", $dsa_pub);
263

    
264
		/* change keys owner to root */
265
		chown("{$sshConfigDir}/ssh_host_key", "root");
266
		chown("{$sshConfigDir}/ssh_host_rsa_key", "root");
267
		chown("{$sshConfigDir}/ssh_host_dsa_key", "root");
268

    
269
		/* change public keys owner to root */
270
		chown("{$sshConfigDir}/ssh_host_key.pub", "root");
271
		chown("{$sshConfigDir}/ssh_host_rsa_key.pub", "root");
272
		chown("{$sshConfigDir}/ssh_host_dsa_key.pub", "root");
273

    
274
		/* change mode on keys to u+rw */
275
		chmod("{$sshConfigDir}/ssh_host_key",0600);
276
		chmod("{$sshConfigDir}/ssh_host_rsa_key",0600);
277
		chmod("{$sshConfigDir}/ssh_host_dsa_key",0600);
278

    
279
		/* change mode on public keys to u+rw */
280
		chmod("{$sshConfigDir}/ssh_host_key.pub",0600);
281
		chmod("{$sshConfigDir}/ssh_host_rsa_key.pub",0600);
282
		chmod("{$sshConfigDir}/ssh_host_dsa_key.pub",0600);
283
	}
284

    
285
	/* start sshd */
286
	system("/usr/sbin/sshd");
287
	echo "done.\n";
288

    
289
?>
(71-71/78)