Project

General

Profile

Download (6.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
	conf_mount_rw();
43

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

    
49
	/*    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
		mkdir("/var/empty", 0555);
63
	}
64

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

    
70
	$sshConfigDir = "/etc/ssh";
71

    
72
	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
	$sshconf .= "Ciphers aes128-ctr,aes256-ctr,arcfour256,arcfour,aes128-cbc,aes256-cbc\n";
81
	$sshconf .= "PermitRootLogin yes\n";
82
	$sshconf .= "Compression yes\n";
83
	$sshconf .= "ClientAliveInterval 30\n";
84
	$sshconf .= "UseDNS no\n";
85
	$sshconf .= "X11Forwarding no\n";
86
	if($config['system']['ssh']['sshdkeyonly'] <> "") {
87
		$sshconf .= "# Login via Key only\n";
88
		$sshconf .= "PasswordAuthentication no\n";
89
		$sshconf .= "ChallengeResponseAuthentication no\n";
90
		$sshconf .= "PubkeyAuthentication yes\n";
91
	} else {
92
		$sshconf .= "# Login via Key and Password\n";
93
		$sshconf .= "PasswordAuthentication yes\n";
94
		$sshconf .= "ChallengeResponseAuthentication yes\n";
95
		$sshconf .= "PubkeyAuthentication yes\n";
96
	}
97
	$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

    
104
	/* Write the new sshd config file */
105
	$fd = fopen("/etc/ssh/sshd_config", "w");
106
	fwrite($fd, $sshconf);
107
	fclose($fd);
108

    
109
	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
	} else {
121
		if(file_exists("/root/.ssh/authorized_keys")) {
122
			unlink("/root/.ssh/authorized_keys");
123
		}
124
	} 
125

    
126
	/* mop up from a badly implemented ssh keys -> cf backup */
127
	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
		write_config("Clearing SSH keys from config.xml");
136
	}
137

    
138
	/* are we already running?  if so exit */
139
	if(file_exists("/tmp/keys_generating")) {
140
		conf_mount_ro();
141
		exit;
142
	}
143
	
144
	if (!file_exists("$sshConfigDir/ssh_host_key") or file_exists("/etc/keys_generating")) {
145
		/* remove previous keys and regen later */
146
		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
		mwexec("rm /etc/ssh/ssh_host_*");
148
		touch("/etc/keys_generating");
149
		touch("/tmp/keys_generating");
150
		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
		unlink("/etc/keys_generating");
155
		unlink("/tmp/keys_generating");
156
		file_notice("SSH", "{$g['product_name']} has completed creating your SSH keys.  SSH is now started.", "SSH Startup", "");
157
		echo "Starting SSH... ";
158
	}
159

    
160
	/* 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
	}
166
	/* Launch new server process */
167
	$status = mwexec("/usr/sbin/sshd");
168
	if($status <> 0) {
169
		file_notice("sshd_startup", "SSHD failed to start.", "SSHD Daemon", "");
170
		echo "error!\n";
171
	} else {
172
		echo "done.\n";
173
	}
174

    
175
	// NanoBSD
176
	if(file_exists("/root/save_sshkeys"))
177
		exec("/root/save_sshkeys");
178

    
179
	conf_mount_ro();
180

    
181
?>
(81-81/87)