Project

General

Profile

Download (6.48 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

    
32
	require_once("globals.inc");
33
	require_once("functions.inc");
34
	require_once("config.inc");
35
	
36
	if(isset($config['system']['enablesshd'])) {
37
		/* do nothing, we're enabled */
38
	} else {
39
		if($g['booting'])
40
			echo "SSHD is disabled.";
41
		exit;
42
	}
43

    
44
	conf_mount_rw();
45

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

    
51
	/* restore ssh data for nanobsd platform */
52
	if($g['platform'] == "nanobsd" and file_exists("/conf/sshd/ssh_host_key")) {
53
		if(!file_exists("/etc/ssh/ssh_host_key.pub")) {
54
			echo "Restoring SSH from /conf/sshd/";
55
			exec("cp /conf/sshd/* /etc/ssh/");
56
		}
57
	}
58

    
59
	/*    if any of these files are 0 bytes then they are corrupted.
60
	 *    remove them
61
	 */
62
	$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');
63
	foreach($files_to_check as $f2c) {
64
		if(file_exists("/etc/ssh/{$f2c}"))
65
			if(file_size("/etc/ssh/{$f2c}")==0) {
66
				mwexec("rm /etc/ssh/ssh_host*");
67
			}
68
	}
69

    
70
	if (!is_dir("/var/empty")) {
71
		/* make ssh home directory */
72
		mkdir("/var/empty", 0555);
73
	}
74

    
75
	if(!file_exists("/var/log/lastlog")) {
76
		/* Login related files. */
77
		touch("/var/log/lastlog");
78
	}
79

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

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

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

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

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

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

    
148
	/* are we already running?  if so exit */
149
	if(is_subsystem_dirty('sshdkeys')) {
150
		conf_mount_ro();
151
		exit;
152
	}
153
	
154
	if (!file_exists("$sshConfigDir/ssh_host_key")) {
155
		/* remove previous keys and regen later */
156
		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", "");
157
		mwexec("rm /etc/ssh/ssh_host_*");
158
		mark_subsystem_dirty('sshdkeys');
159
		echo " Generating Keys:\n";
160
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa1 -N '' -f $sshConfigDir/ssh_host_key");
161
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa -N '' -f $sshConfigDir/ssh_host_rsa_key");
162
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t dsa -N '' -f $sshConfigDir/ssh_host_dsa_key");
163
		clear_subsystem_dirty('sshdkeys');
164
		file_notice("SSH", "{$g['product_name']} has completed creating your SSH keys.  SSH is now started.", "SSH Startup", "");
165
		echo "Starting SSH... ";
166
	}
167

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

    
183
	// NanoBSD
184
	if($g['platform'] == "nanobsd") {
185
		if(!is_dir("/conf/sshd"))
186
			exec("mkdir /conf/sshd");
187
		exec("cp /etc/ssh/ssh_host* /conf/sshd");
188
	}
189
	conf_mount_ro();
190

    
191
?>
(83-83/89)