Project

General

Profile

« Previous | Next » 

Revision 16559651

Added by Scott Ullrich about 17 years ago

Sync w/ HEAD

View differences:

etc/sshd
28 28
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 29
	POSSIBILITY OF SUCH DAMAGE.
30 30
*/
31
	require_once("config.inc");
32
	require_once("notices.inc");
33 31

  
34
	if(isset($config['system']['enablesshd'])) {
35
		/* do nothing, we're enabled */
36
	} else {
32
	require("functions.inc");
33

  
34
	/* exit, if sshd is not enabled */
35
	if (! isset($config['system']['ssh']['enable'])) {
37 36
		if($g['booting'])
38 37
			echo "SSHD is disabled.";
39 38
		exit;
40 39
	}
41 40

  
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
	}
41
	/* specify the locations of sshd's configuration */
42
	$ssh_cfg_dir  = "{$g['etc_path']}/ssh";
43
	$sshd_pidfile = "{$g['varrun_path']}/sshd.pid";
44
	$sshd_config  = "{$ssh_cfg_dir}/sshd_config";
45

  
46
	/* create ssh configuration directory */
47
	if(! is_dir($ssh_cfg_dir))
48
		mkdir($ssh_cfg_dir, 0755);
49

  
50
	/* if any of these files are 0 bytes then they are corrupted -> remove them */
51
	exec("find ../../" . ($g['platform'] == "pfSense" ? "home" : "var/home") . " -name authorized_keys", $files_in_homes);
52
	$files_to_check = array_merge(
53
		array(
54
			'ssh_host_dsa_key', 'ssh_host_dsa_key.pub', 'ssh_host_key', 'ssh_host_key.pub',
55
			'ssh_host_rsa_key', 'ssh_host_rsa_key.pub', '../../root/.ssh/authorized_keys'
56
		),
57
		$files_in_homes
58
	);
59
	foreach($files_to_check as $f2c)
60
		if(file_exists("{$ssh_cfg_dir}/{$f2c}") && filesize("{$ssh_cfg_dir}/{$f2c}") == 0) {
61
			if(basename($f2c) == "authorized_keys")
62
				mwexec("rm {$ssh_cfg_dir}/{$f2c}");
63
			else
64
				mwexec("rm {$ssh_cfg_dir}/ssh_host_*");
65
		}
57 66

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

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

  
68
	/* Make the root passwords are the same as the web admin password. */
75
	/* sync passwords and create per-user authorized_keys file */
76
	system_password_configure();
69 77
	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 78

  
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";
79
	/* include default configuration for pfSense */
80
	$sshconf  = "#\n";
81
	$sshconf .= "# pfSense SSHd configuration file\n";
82
	$sshconf .= "# This file is automatically generated at startup\n";
83
	$sshconf .= "#\n";
90 84
	$sshconf .= "Compression yes\n";
91 85
	$sshconf .= "ClientAliveInterval 30\n";
86
	$sshconf .= "ClientAliveCountMax 3\n";
87
	$sshconf .= "IgnoreRhosts yes\n";
88
	$sshconf .= "LoginGraceTime 60\n";
89
	$sshconf .= "MaxStartups 5\n";
90
	$sshconf .= "PermitEmptyPasswords no\n";
91
	$sshconf .= "PermitRootLogin yes\n";
92
	$sshconf .= "PidFile {$sshd_pidfile}\n";
93
	$sshconf .= "Protocol 2\n";
92 94
	$sshconf .= "UseDNS no\n";
93
	$sshconf .= "X11Forwarding no\n";
94
	if($config['system']['ssh']['sshdkeyonly'] <> "") {
95
		$sshconf .= "# Login via Key only\n";
95
	$sshconf .= "VersionAddendum\n";
96
	$sshconf .= "X11Forwarding no\n\n";
97

  
98
	/* run the server on another port (if we have one defined) */
99
	if($config['system']['ssh']['port'] <> "")
100
		$sshconf .= "Port " . $config['system']['ssh']['port'] . "\n";
101
	else
102
		$sshconf .= "Port 22\n";
103

  
104
	/* listen on all or specified interfaces only */
105
	/* note: if no interfaces are specified or "*" is set, bind to all interfaces */
106
	if($config['system']['ssh']['interfaces'] <> "") {
107
		$sshifaces = explode(" ", $config['system']['ssh']['interfaces']);
108
		if(! in_array("*", $sshifaces))
109
			foreach($sshifaces as $iface) {
110
				$ipv4 = get_interface_address($iface, 4);
111
				if($ipv4)
112
					$sshconf .= "ListenAddress " . $ipv4 . "\n";
113
//				$ipv6 = get_interface_address($iface, 6);
114
//				if($ipv6)
115
//					$sshconf .= "ListenAddress " . $ipv6 . "\n";
116
			}
117
	}
118

  
119
	if(isset($config['system']['ssh']['sshdkeyonly'])) {
120
		$sshconf .= "\n# Login via Key only\n";
96 121
		$sshconf .= "PasswordAuthentication no\n";
97 122
		$sshconf .= "ChallengeResponseAuthentication no\n";
98 123
		$sshconf .= "PubkeyAuthentication yes\n";
99 124
	} else {
100
		$sshconf .= "# Login via Key and Password\n";
125
		$sshconf .= "\n# Login via Key and Password\n";
101 126
		$sshconf .= "PasswordAuthentication yes\n";
102 127
		$sshconf .= "ChallengeResponseAuthentication yes\n";
103 128
		$sshconf .= "PubkeyAuthentication yes\n";
104 129
	}
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 130

  
112
	/* Write the new sshd config file */
113
	$fd = fopen("/etc/ssh/sshd_config", "w");
131
// TODO: allow SCP to be disabled (via the webConfigurator or a hidden option)
132
	$sshconf .= "\n# SFTP server subsystem\n";
133
	$sshconf .= "Subsystem sftp /usr/libexec/sftp-server\n";
134

  
135
	/* write the new sshd config file */
136
	$fd = fopen($sshd_config, "w");
114 137
	fwrite($fd, $sshconf);
115 138
	fclose($fd);
116 139

  
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
	} 
129

  
140
// FIXME: this seems to be completely outdated (but harmless)
130 141
	/* mop up from a badly implemented ssh keys -> cf backup */
131 142
	if($config['ssh']['dsa_key'] <> "") {
143
		write_config("Clearing outdated SSH keys from config.xml");
132 144
		unset($config['ssh']['dsa_key']);
133 145
		unset($config['ssh']['rsa_key']);
134 146
		unset($config['ssh']['rsa1_key']);
......
136 148
		unset($config['ssh']['rsa']);
137 149
		unset($config['ssh']['rsa1']);
138 150
		unset($config['ssh']['ak']);
139
		write_config("Clearing SSH keys from config.xml");
140 151
	}
141 152

  
142
	/* are we already running?  if so exit */
153
	/* are we already running? if so exit. */
143 154
	if(file_exists("/tmp/keys_generating"))
144 155
		exit;
145 156

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

  
163
	/* kill existing sshd process, server only, not the childs */
164
	$sshd_pid = exec("ps ax | egrep '/usr/sbin/[s]shd' | awk '{print $1}'");
165
	if($sshd_pid <> "") {
166
		echo "stopping ssh process $sshd_pid \n";
167
		mwexec("kill $sshd_pid");
174
	/* kill existing sshd process: server only, not the childs */
175
	if(file_exists($sshd_pidfile)) {
176
		mwexec("echo 'stopping ssh' 1>&2");
177
		killbypid($sshd_pidfile);
168 178
	}
169
	/* Launch new server process */
170
	$status = mwexec("/usr/sbin/sshd");
179

  
180
	/* launch new server process */
181
	$status = mwexec("/usr/sbin/sshd -f {$sshd_config}");
171 182
	if($status <> 0) {
172 183
		file_notice("sshd_startup", "SSHD failed to start.", "SSHD Daemon", "");
173
		echo "error!\n";
174
	} else {
175
		echo "done.\n";
184
		mwexec("echo -n 'error!' 1>&2");
176 185
	}
186
	else
187
		mwexec("echo -n 'done.' 1>&2");
177 188

  
178 189
	conf_mount_ro();
179 190

  
180 191

  
181

  
182

  
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

  
192
/* ===== exit, since the following stuff needs more testing ===== */
217 193
	exit;
194
/* ============================================================== */
218 195

  
219
	/* exit early, this needs more testing. */
220 196

  
221
	if (!file_exists("$sshConfigDir/ssh_host_key") and $config['ssh']['dsa'] == "") {
197
// TODO: here be dragons
198
	if(!file_exists("{$ssh_cfg_dir}/ssh_host_key") and $config['ssh']['dsa'] == "") {
222 199
		/* generate keys */
223
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa1 -N '' -f $sshConfigDir/ssh_host_key");
224
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa -N '' -f $sshConfigDir/ssh_host_rsa_key");
225
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t dsa -N '' -f $sshConfigDir/ssh_host_dsa_key");
200
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa1 -N '' -f {$ssh_cfg_dir}/ssh_host_key");
201
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa -N '' -f {$ssh_cfg_dir}/ssh_host_rsa_key");
202
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t dsa -N '' -f {$ssh_cfg_dir}/ssh_host_dsa_key");
226 203

  
227 204
		/* save keys */
228
		$dsa  = file_get_contents("{$sshConfigDir}/ssh_host_dsa_key");
229
		$rsa  = file_get_contents("{$sshConfigDir}/ssh_host_rsa_key");
230
		$rsa1 = file_get_contents("{$sshConfigDir}/ssh_host_key");
205
		$dsa  = file_get_contents("{$ssh_cfg_dir}/ssh_host_dsa_key");
206
		$rsa  = file_get_contents("{$ssh_cfg_dir}/ssh_host_rsa_key");
207
		$rsa1 = file_get_contents("{$ssh_cfg_dir}/ssh_host_key");
231 208
		$config['ssh']['dsa'] = base64_encode($dsa);
232 209
		$config['ssh']['rsa'] = base64_encode($rsa);
233 210
		$config['ssh']['rsa1']= base64_encode($rsa1);
234 211

  
235 212
		/* save public keys */
236
		$dsapub  = file_get_contents("{$sshConfigDir}/ssh_host_dsa_key.pub");
237
		$rsapub  = file_get_contents("{$sshConfigDir}/ssh_host_rsa_key.pub");
238
		$rsa1pub = file_get_contents("{$sshConfigDir}/ssh_host_key.pub");
213
		$dsapub  = file_get_contents("{$ssh_cfg_dir}/ssh_host_dsa_key.pub");
214
		$rsapub  = file_get_contents("{$ssh_cfg_dir}/ssh_host_rsa_key.pub");
215
		$rsa1pub = file_get_contents("{$ssh_cfg_dir}/ssh_host_key.pub");
239 216
		$config['ssh']['dsa_key'] = base64_encode($dsapub);
240 217
		$config['ssh']['rsa_key'] = base64_encode($rsapub);
241 218
		$config['ssh']['rsa1_key']= base64_encode($rsa1pub);
......
245 222
		$rsa1 = base64_decode($config['ssh']['rsa1']);
246 223
		$rsa  = base64_decode($config['ssh']['rsa']);
247 224
		$dsa  = base64_decode($config['ssh']['dsa']);
248
		file_put_contents("{$sshConfigDir}/ssh_host_key", $rsa1);
249
		file_put_contents("{$sshConfigDir}/ssh_host_rsa_key", $rsa);
250
		file_put_contents("{$sshConfigDir}/ssh_host_dsa_key", $dsa);
225
		file_put_contents("{$ssh_cfg_dir}/ssh_host_key", $rsa1);
226
		file_put_contents("{$ssh_cfg_dir}/ssh_host_rsa_key", $rsa);
227
		file_put_contents("{$ssh_cfg_dir}/ssh_host_dsa_key", $dsa);
251 228

  
252 229
		/* restore public keys */
253 230
		$rsa1_pub = base64_decode($config['ssh']['rsa1_key']);
254 231
		$rsa_pub  = base64_decode($config['ssh']['rsa_key']);
255 232
		$dsa_pub  = base64_decode($config['ssh']['dsa_key']);
256
		file_put_contents("{$sshConfigDir}/ssh_host_key.pub", $rsa1_pub);
257
		file_put_contents("{$sshConfigDir}/ssh_host_rsa_key.pub", $rsa_pub);
258
		file_put_contents("{$sshConfigDir}/ssh_host_dsa_key.pub", $dsa_pub);
233
		file_put_contents("{$ssh_cfg_dir}/ssh_host_key.pub", $rsa1_pub);
234
		file_put_contents("{$ssh_cfg_dir}/ssh_host_rsa_key.pub", $rsa_pub);
235
		file_put_contents("{$ssh_cfg_dir}/ssh_host_dsa_key.pub", $dsa_pub);
259 236

  
260 237
		/* change keys owner to root */
261
		chown("{$sshConfigDir}/ssh_host_key", "root");
262
		chown("{$sshConfigDir}/ssh_host_rsa_key", "root");
263
		chown("{$sshConfigDir}/ssh_host_dsa_key", "root");
238
		chown("{$ssh_cfg_dir}/ssh_host_key", "root");
239
		chown("{$ssh_cfg_dir}/ssh_host_rsa_key", "root");
240
		chown("{$ssh_cfg_dir}/ssh_host_dsa_key", "root");
264 241

  
265 242
		/* change public keys owner to root */
266
		chown("{$sshConfigDir}/ssh_host_key.pub", "root");
267
		chown("{$sshConfigDir}/ssh_host_rsa_key.pub", "root");
268
		chown("{$sshConfigDir}/ssh_host_dsa_key.pub", "root");
243
		chown("{$ssh_cfg_dir}/ssh_host_key.pub", "root");
244
		chown("{$ssh_cfg_dir}/ssh_host_rsa_key.pub", "root");
245
		chown("{$ssh_cfg_dir}/ssh_host_dsa_key.pub", "root");
269 246

  
270 247
		/* change mode on keys to u+rw */
271
		chmod("{$sshConfigDir}/ssh_host_key",0600);
272
		chmod("{$sshConfigDir}/ssh_host_rsa_key",0600);
273
		chmod("{$sshConfigDir}/ssh_host_dsa_key",0600);
248
		chmod("{$ssh_cfg_dir}/ssh_host_key",0600);
249
		chmod("{$ssh_cfg_dir}/ssh_host_rsa_key",0600);
250
		chmod("{$ssh_cfg_dir}/ssh_host_dsa_key",0600);
274 251

  
275 252
		/* change mode on public keys to u+rw */
276
		chmod("{$sshConfigDir}/ssh_host_key.pub",0600);
277
		chmod("{$sshConfigDir}/ssh_host_rsa_key.pub",0600);
278
		chmod("{$sshConfigDir}/ssh_host_dsa_key.pub",0600);
253
		chmod("{$ssh_cfg_dir}/ssh_host_key.pub",0600);
254
		chmod("{$ssh_cfg_dir}/ssh_host_rsa_key.pub",0600);
255
		chmod("{$ssh_cfg_dir}/ssh_host_dsa_key.pub",0600);
279 256
	}
280 257

  
281 258
	/* start sshd */
282
	system("/usr/sbin/sshd");
259
	system("/usr/sbin/sshd -f {$sshd_config}");
283 260
	echo "done.\n";
284

  
285 261
?>

Also available in: Unified diff