Project

General

Profile

Download (8.07 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
#! /usr/local/bin/php -f
2
<?php
3
/*
4
	sshd - Modified to work on disk based system
5
	Copyright 2004 Scott K Ullrich
6 b2981d7a Scott Ullrich
7 5b237745 Scott Ullrich
	Original Copyright (C) 2004 Fred Mol <fredmol@xs4all.nl>.
8
	All rights reserved.
9 b2981d7a Scott Ullrich
10 5b237745 Scott Ullrich
	Redistribution and use in source and binary forms, with or without
11
	modification, are permitted provided that the following conditions are met:
12 b2981d7a Scott Ullrich
13 5b237745 Scott Ullrich
	1. Redistributions of source code must retain the above copyright notice,
14
	   this list of conditions and the following disclaimer.
15 b2981d7a Scott Ullrich
16 5b237745 Scott Ullrich
	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 b2981d7a Scott Ullrich
20 5b237745 Scott Ullrich
	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 b2981d7a Scott Ullrich
33 668b7b2e Scott Ullrich
	if(isset($config['system']['enablesshd'])) {
34
		/* do nothing, we're enabled */
35
	} else {
36
		if($g['booting'])
37
			echo "SSHD is disabled.";
38 b15e2ed4 Scott Ullrich
		exit;
39 668b7b2e Scott Ullrich
	}
40 36aaefff Scott Ullrich
	
41 746b5ee2 Scott Ullrich
	function file_size($file) {
42
	  $size = filesize($file);
43
	  return $size;
44
	}
45
46
	/*    if any of these files are 0 bytes then they are corrupted.
47
	 *    remove them
48
	 */
49 03b42caa Scott Ullrich
	$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/.authorized_keys');
50 746b5ee2 Scott Ullrich
	foreach($files_to_check as $f2c) {
51 7e6d816a Scott Ullrich
		if(file_exists("/etc/ssh/{$f2c}"))
52
			if(file_size("/etc/ssh/{$f2c}")==0) {
53 c5a9b876 Scott Ullrich
				mwexec("rm /etc/ssh/ssh_host*");
54 5858cbdb Scott Ullrich
			}
55 746b5ee2 Scott Ullrich
	}
56
57 5b237745 Scott Ullrich
	if (!is_dir("/var/empty")) {
58 efa761f6 Scott Ullrich
		/* make ssh home directory */
59 5b237745 Scott Ullrich
		mkdir("/var/empty", 0555);
60
	}
61
62
	if(!file_exists("")) {
63 efa761f6 Scott Ullrich
		/* Login related files. */
64 5b237745 Scott Ullrich
		touch("/var/log/lastlog");
65
	}
66
67 efa761f6 Scott Ullrich
	/* Make the root passwords are the same as the web admin password. */
68 823b7b0f Scott Ullrich
	conf_mount_rw();
69 5b237745 Scott Ullrich
	$fd = popen("/usr/sbin/pw usermod -n root -H 0", "w");
70
	fwrite($fd, $config['system']['password']);
71
	pclose($fd);
72 efa761f6 Scott Ullrich
73
	/* Make the admin passwords are the same as the web admin password. */
74 128f6a3e Bill Marquette
	$fd = popen("/usr/sbin/pw usermod -n admin -H 0", "w");
75
	fwrite($fd, $config['system']['password']);
76
	pclose($fd);
77 5b237745 Scott Ullrich
78
	$sshConfigDir = "/etc/ssh";
79 850b71ec Scott Ullrich
80 5b7eb87c Seth Mos
	if($config['system']['ssh']['port'] <> "") {
81
		$sshport = $config['system']['ssh']['port'];
82
	} else {
83
		$sshport = 22;
84
	}
85
86
	/* Include default configuration for pfSense */
87
	$sshconf = "# This file is automatically generated at startup\n";
88
	$sshconf .= "PermitRootLogin yes\n";
89
	$sshconf .= "Compression yes\n";
90
	$sshconf .= "ClientAliveInterval 30\n";
91
	$sshconf .= "UseDNS no\n";
92
	$sshconf .= "X11Forwarding no\n";
93
	$sshconf .= "# override default of no subsystems\n";
94
	$sshconf .= "Subsystem       sftp    /usr/libexec/sftp-server\n";
95
	/* Only allow protocol 2, because we say so */
96
	$sshconf .= "Protocol 2\n";
97
	/* Run the server on another port if we have one defined */
98
	$sshconf .= "Port $sshport\n";
99
100
	/* Write the new sshd config file */
101
	$fd = fopen("/etc/ssh/sshd_config", "w");
102
	fwrite($fd, $sshconf);
103
	pclose($fd);
104
105 0f953a29 Scott Ullrich
	/* mop up from a badly implemented ssh keys -> cf backup */
106 426f300c Scott Ullrich
	if($config['ssh']['dsa_key'] <> "") {
107
		unset($config['ssh']['dsa_key']);
108
		unset($config['ssh']['rsa_key']);
109
		unset($config['ssh']['rsa1_key']);
110
		unset($config['ssh']['dsa']);
111
		unset($config['ssh']['rsa']);
112
		unset($config['ssh']['rsa1']);
113
		unset($config['ssh']['ak']);
114
		write_config("Clearing SSH keys from config.xml");
115
	}
116 c3290534 Scott Ullrich
	
117 dcb64768 Scott Ullrich
	/* are we already running?  if so exit */
118
	if(file_exists("/tmp/keys_generating"))
119
		exit;
120
	
121 0f953a29 Scott Ullrich
	if (!file_exists("$sshConfigDir/ssh_host_key") or file_exists("/etc/keys_generating")) {
122 c2338828 Scott Ullrich
		/* remove previous keys and regen later */
123 0ae71d81 Scott Ullrich
		conf_mount_rw();
124 c2338828 Scott Ullrich
		mwexec("rm /etc/ssh/ssh_host_*");
125 0f953a29 Scott Ullrich
		touch("/etc/keys_generating");
126 dcb64768 Scott Ullrich
		touch("/tmp/keys_generating");
127 0f953a29 Scott Ullrich
		echo " Generating Keys:\n";
128 e1338375 Scott Ullrich
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa1 -N '' -f $sshConfigDir/ssh_host_key");
129
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa -N '' -f $sshConfigDir/ssh_host_rsa_key");
130
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t dsa -N '' -f $sshConfigDir/ssh_host_dsa_key");
131 0f953a29 Scott Ullrich
		unlink("/etc/keys_generating");
132 dcb64768 Scott Ullrich
		unlink("/tmp/keys_generating");
133 0f953a29 Scott Ullrich
		echo "Starting SSH... ";
134 c2338828 Scott Ullrich
	}
135 efa761f6 Scott Ullrich
136 5b7eb87c Seth Mos
	/* kill existing sshd process, server only, not the childs */
137
	$sshd_pid = exec("ps ax | egrep '/usr/sbin/[s]shd' | awk '{print $1}'");
138
	if($sshd_pid <> "") {
139
		echo "stopping ssh process $sshd_pid \n";
140
		mwexec("kill $sshd_pid");
141
	}
142
	/* Launch new server process */
143 2d195c3f Scott Ullrich
	$status = mwexec("/usr/sbin/sshd");
144 0f953a29 Scott Ullrich
	if($status <> 0) {
145 fdfc687c Scott Ullrich
		file_notice("sshd_startup", "SSHD failed to start.", "SSHD Daemon", "");
146 f6661aed Scott Ullrich
		echo "error!\n";
147 0f953a29 Scott Ullrich
	} else {
148 f6661aed Scott Ullrich
		echo "done.\n";
149 0f953a29 Scott Ullrich
	}
150
	
151 823b7b0f Scott Ullrich
	conf_mount_ro();
152 0f953a29 Scott Ullrich
	
153
	
154
	
155
	
156
	
157
	
158
	
159
	
160
	
161
	
162
	
163
	
164
	
165
	
166
	
167
	
168
	
169
	
170
	
171
	
172
	
173
	
174
	
175
	
176
	
177
	
178
	
179
	
180
	
181
	
182
	
183
	
184
	
185
	
186
	
187
	
188
	
189
	
190 21b20aae Scott Ullrich
	exit;
191 6b21d1ed Scott Ullrich
192
	/* exit early, this needs more testing. */
193
194 efa761f6 Scott Ullrich
	if (!file_exists("$sshConfigDir/ssh_host_key") and $config['ssh']['dsa'] == "") {
195
		/* generate keys */
196 e1338375 Scott Ullrich
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa1 -N '' -f $sshConfigDir/ssh_host_key");
197
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa -N '' -f $sshConfigDir/ssh_host_rsa_key");
198
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t dsa -N '' -f $sshConfigDir/ssh_host_dsa_key");
199 efa761f6 Scott Ullrich
200
		/* save keys */
201 7822d966 Colin Smith
		$dsa  = file_get_contents("{$sshConfigDir}/ssh_host_dsa_key");
202
		$rsa  = file_get_contents("{$sshConfigDir}/ssh_host_rsa_key");
203
		$rsa1 = file_get_contents("{$sshConfigDir}/ssh_host_key");
204 efa761f6 Scott Ullrich
		$config['ssh']['dsa'] = base64_encode($dsa);
205
		$config['ssh']['rsa'] = base64_encode($rsa);
206
		$config['ssh']['rsa1']= base64_encode($rsa1);
207
208
		/* save public keys */
209 7822d966 Colin Smith
		$dsapub  = file_get_contents("{$sshConfigDir}/ssh_host_dsa_key.pub");
210
		$rsapub  = file_get_contents("{$sshConfigDir}/ssh_host_rsa_key.pub");
211
		$rsa1pub = file_get_contents("{$sshConfigDir}/ssh_host_key.pub");
212 efa761f6 Scott Ullrich
		$config['ssh']['dsa_key'] = base64_encode($dsapub);
213
		$config['ssh']['rsa_key'] = base64_encode($rsapub);
214
		$config['ssh']['rsa1_key']= base64_encode($rsa1pub);
215 5fa404d4 Scott Ullrich
		write_config("Saved SSH keys.");
216 9e3fc88e Scott Ullrich
	} else {
217 efa761f6 Scott Ullrich
		/* restore keys */
218
		$rsa1 = base64_decode($config['ssh']['rsa1']);
219
		$rsa  = base64_decode($config['ssh']['rsa']);
220
		$dsa  = base64_decode($config['ssh']['dsa']);
221
		file_put_contents("{$sshConfigDir}/ssh_host_key", $rsa1);
222
		file_put_contents("{$sshConfigDir}/ssh_host_rsa_key", $rsa);
223
		file_put_contents("{$sshConfigDir}/ssh_host_dsa_key", $dsa);
224
225
		/* restore public keys */
226
		$rsa1_pub = base64_decode($config['ssh']['rsa1_key']);
227
		$rsa_pub  = base64_decode($config['ssh']['rsa_key']);
228
		$dsa_pub  = base64_decode($config['ssh']['dsa_key']);
229
		file_put_contents("{$sshConfigDir}/ssh_host_key.pub", $rsa1_pub);
230
		file_put_contents("{$sshConfigDir}/ssh_host_rsa_key.pub", $rsa_pub);
231
		file_put_contents("{$sshConfigDir}/ssh_host_dsa_key.pub", $dsa_pub);
232
233
		/* change keys owner to root */
234
		chown("{$sshConfigDir}/ssh_host_key", "root");
235
		chown("{$sshConfigDir}/ssh_host_rsa_key", "root");
236
		chown("{$sshConfigDir}/ssh_host_dsa_key", "root");
237
238
		/* change public keys owner to root */
239
		chown("{$sshConfigDir}/ssh_host_key.pub", "root");
240
		chown("{$sshConfigDir}/ssh_host_rsa_key.pub", "root");
241
		chown("{$sshConfigDir}/ssh_host_dsa_key.pub", "root");
242
243
		/* change mode on keys to u+rw */
244
		chmod("{$sshConfigDir}/ssh_host_key",0600);
245
		chmod("{$sshConfigDir}/ssh_host_rsa_key",0600);
246
		chmod("{$sshConfigDir}/ssh_host_dsa_key",0600);
247
248
		/* change mode on public keys to u+rw */
249
		chmod("{$sshConfigDir}/ssh_host_key.pub",0600);
250
		chmod("{$sshConfigDir}/ssh_host_rsa_key.pub",0600);
251
		chmod("{$sshConfigDir}/ssh_host_dsa_key.pub",0600);
252 5b237745 Scott Ullrich
	}
253
254 0a4869c5 Scott Ullrich
	if($config['ssh']['ak'] <> "") {
255
		$ak  = base64_decode($config['ssh']['ak']);
256
		file_put_contents("/root/.authorized_keys", $ak);
257
		chmod("/root/.authorized_keys",0600);
258
	}
259
260 efa761f6 Scott Ullrich
	/* start sshd */
261 abafa16b Scott Ullrich
	system("/usr/sbin/sshd");
262 f6661aed Scott Ullrich
	echo "done.\n";
263 efa761f6 Scott Ullrich
264 7822d966 Colin Smith
?>