Project

General

Profile

Download (6.21 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 116852b8 Scott Ullrich
	require_once("config.inc");
32
	require_once("notices.inc");
33 36fcc77f Scott Ullrich
	
34 116852b8 Scott Ullrich
	if(isset($config['system']['enablesshd'])) {
35
		/* do nothing, we're enabled */
36
	} else {
37 668b7b2e Scott Ullrich
		if($g['booting'])
38
			echo "SSHD is disabled.";
39 b15e2ed4 Scott Ullrich
		exit;
40 668b7b2e Scott Ullrich
	}
41 579946e2 Scott Ullrich
42 36fcc77f Scott Ullrich
	conf_mount_rw();
43
44 116852b8 Scott Ullrich
	function file_size($file) {
45
	  $size = filesize($file);
46
	  return $size;
47
	}
48 746b5ee2 Scott Ullrich
49 116852b8 Scott Ullrich
	/*    if any of these files are 0 bytes then they are corrupted.
50
	 *    remove them
51
	 */
52
	$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');
53
	foreach($files_to_check as $f2c) {
54
		if(file_exists("/etc/ssh/{$f2c}"))
55
			if(file_size("/etc/ssh/{$f2c}")==0) {
56
				mwexec("rm /etc/ssh/ssh_host*");
57
			}
58
	}
59
60
	if (!is_dir("/var/empty")) {
61
		/* make ssh home directory */
62 5b237745 Scott Ullrich
		mkdir("/var/empty", 0555);
63 116852b8 Scott Ullrich
	}
64 5b237745 Scott Ullrich
65 116852b8 Scott Ullrich
	if(!file_exists("")) {
66
		/* Login related files. */
67 5b237745 Scott Ullrich
		touch("/var/log/lastlog");
68 116852b8 Scott Ullrich
	}
69 5b237745 Scott Ullrich
70 116852b8 Scott Ullrich
	$sshConfigDir = "/etc/ssh";
71 850b71ec Scott Ullrich
72 116852b8 Scott Ullrich
	if($config['system']['ssh']['port'] <> "") {
73
		$sshport = $config['system']['ssh']['port'];
74
	} else {
75
		$sshport = 22;
76
	}
77
78
	/* Include default configuration for pfSense */
79
	$sshconf = "# This file is automatically generated at startup\n";
80 868a5b99 Scott Ullrich
	$sshconf .= "Ciphers aes128-ctr,aes256-ctr,arcfour256,arcfour,aes128-cbc,aes256-cbc\n";
81 116852b8 Scott Ullrich
	$sshconf .= "PermitRootLogin yes\n";
82 5b7eb87c Seth Mos
	$sshconf .= "Compression yes\n";
83
	$sshconf .= "ClientAliveInterval 30\n";
84
	$sshconf .= "UseDNS no\n";
85 116852b8 Scott Ullrich
	$sshconf .= "X11Forwarding no\n";
86
	if($config['system']['ssh']['sshdkeyonly'] <> "") {
87
		$sshconf .= "# Login via Key only\n";
88 ed4b63b0 Timo Boettcher
		$sshconf .= "PasswordAuthentication no\n";
89
		$sshconf .= "ChallengeResponseAuthentication no\n";
90
		$sshconf .= "PubkeyAuthentication yes\n";
91
	} else {
92 116852b8 Scott Ullrich
		$sshconf .= "# Login via Key and Password\n";
93 ed4b63b0 Timo Boettcher
		$sshconf .= "PasswordAuthentication yes\n";
94
		$sshconf .= "ChallengeResponseAuthentication yes\n";
95
		$sshconf .= "PubkeyAuthentication yes\n";
96
	}
97 116852b8 Scott Ullrich
	$sshconf .= "# override default of no subsystems\n";
98
	$sshconf .= "Subsystem       sftp    /usr/libexec/sftp-server\n";
99
	/* Only allow protocol 2, because we say so */
100
	$sshconf .= "Protocol 2\n";
101
	/* Run the server on another port if we have one defined */
102
	$sshconf .= "Port $sshport\n";
103 5b7eb87c Seth Mos
104 116852b8 Scott Ullrich
	/* Write the new sshd config file */
105
	$fd = fopen("/etc/ssh/sshd_config", "w");
106 5b7eb87c Seth Mos
	fwrite($fd, $sshconf);
107 579946e2 Scott Ullrich
	fclose($fd);
108 5b7eb87c Seth Mos
109 116852b8 Scott Ullrich
	if($config['system']['ssh']['authorizedkeys'] <> "") {
110
		echo "writing /root/.ssh/authorized_keys\n";
111
		if (!is_dir("/root/.ssh")) {
112
			mkdir('/root/.ssh', 0700);
113
		}
114
		$authorizedkeys  = "# This file is automatically generated at startup\n";
115
		$authorizedkeys .= base64_decode($config['system']['ssh']['authorizedkeys']);
116
		$fd = fopen("/root/.ssh/authorized_keys", "w");
117
		fwrite($fd, $authorizedkeys);
118
		pclose($fd);
119
		chmod("/root/.ssh/authorized_keys",0644);
120 90ebd0b3 Bill Marquette
	} else {
121
		if(file_exists("/root/.ssh/authorized_keys")) {
122
			unlink("/root/.ssh/authorized_keys");
123
		}
124 116852b8 Scott Ullrich
	} 
125
126 0f953a29 Scott Ullrich
	/* mop up from a badly implemented ssh keys -> cf backup */
127 426f300c Scott Ullrich
	if($config['ssh']['dsa_key'] <> "") {
128
		unset($config['ssh']['dsa_key']);
129
		unset($config['ssh']['rsa_key']);
130
		unset($config['ssh']['rsa1_key']);
131
		unset($config['ssh']['dsa']);
132
		unset($config['ssh']['rsa']);
133
		unset($config['ssh']['rsa1']);
134
		unset($config['ssh']['ak']);
135 116852b8 Scott Ullrich
		write_config("Clearing SSH keys from config.xml");
136 426f300c Scott Ullrich
	}
137 579946e2 Scott Ullrich
138 116852b8 Scott Ullrich
	/* are we already running?  if so exit */
139 36fcc77f Scott Ullrich
	if(file_exists("/tmp/keys_generating")) {
140
		conf_mount_ro();
141 dcb64768 Scott Ullrich
		exit;
142 36fcc77f Scott Ullrich
	}
143
	
144 116852b8 Scott Ullrich
	if (!file_exists("$sshConfigDir/ssh_host_key") or file_exists("/etc/keys_generating")) {
145
		/* remove previous keys and regen later */
146 5b6eac01 Scott Ullrich
		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", "");
147 116852b8 Scott Ullrich
		mwexec("rm /etc/ssh/ssh_host_*");
148 0f953a29 Scott Ullrich
		touch("/etc/keys_generating");
149 dcb64768 Scott Ullrich
		touch("/tmp/keys_generating");
150 116852b8 Scott Ullrich
		echo " Generating Keys:\n";
151
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa1 -N '' -f $sshConfigDir/ssh_host_key");
152
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa -N '' -f $sshConfigDir/ssh_host_rsa_key");
153
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t dsa -N '' -f $sshConfigDir/ssh_host_dsa_key");
154 0f953a29 Scott Ullrich
		unlink("/etc/keys_generating");
155 dcb64768 Scott Ullrich
		unlink("/tmp/keys_generating");
156 5b6eac01 Scott Ullrich
		file_notice("SSH", "{$g['product_name']} has completed creating your SSH keys.  SSH is now started.", "SSH Startup", "");
157 116852b8 Scott Ullrich
		echo "Starting SSH... ";
158 c2338828 Scott Ullrich
	}
159 efa761f6 Scott Ullrich
160 116852b8 Scott Ullrich
	/* kill existing sshd process, server only, not the childs */
161
	$sshd_pid = exec("ps ax | egrep '/usr/sbin/[s]shd' | awk '{print $1}'");
162
	if($sshd_pid <> "") {
163
		echo "stopping ssh process $sshd_pid \n";
164
		mwexec("kill $sshd_pid");
165 5b7eb87c Seth Mos
	}
166 116852b8 Scott Ullrich
	/* Launch new server process */
167
	$status = mwexec("/usr/sbin/sshd");
168 0f953a29 Scott Ullrich
	if($status <> 0) {
169 fdfc687c Scott Ullrich
		file_notice("sshd_startup", "SSHD failed to start.", "SSHD Daemon", "");
170 116852b8 Scott Ullrich
		echo "error!\n";
171
	} else {
172
		echo "done.\n";
173 0f953a29 Scott Ullrich
	}
174 579946e2 Scott Ullrich
175 43640486 Scott Ullrich
	// NanoBSD
176
	if(file_exists("/root/save_sshkeys"))
177
		exec("/root/save_sshkeys");
178
179 823b7b0f Scott Ullrich
	conf_mount_ro();
180 579946e2 Scott Ullrich
181 36fcc77f Scott Ullrich
?>