Project

General

Profile

Download (6.59 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("config.inc");
34
	require_once("functions.inc");
35
	require_once("shaper.inc");
36

    
37
	if(isset($config['system']['enablesshd'])) {
38
		/* do nothing, we're enabled */
39
	} else {
40
		if($g['booting'])
41
			echo "SSHD is disabled.";
42
		exit;
43
	}
44

    
45
	conf_mount_rw();
46

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

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

    
58
			/* make sure host private key permissions aren't too open so sshd won't complain */
59
			$files_to_check = array('ssh_host_dsa_key','ssh_host_key','ssh_host_rsa_key');
60
			foreach($files_to_check as $f2c) {
61
				if(file_exists("/etc/ssh/{$f2c}"))
62
					chmod("/etc/ssh/{$f2c}", 0600);
63
			}
64
		}
65
	}
66

    
67
	/*    if any of these files are 0 bytes then they are corrupted.
68
	 *    remove them
69
	 */
70
	$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');
71
	foreach($files_to_check as $f2c) {
72
		if(file_exists("/etc/ssh/{$f2c}"))
73
			if(file_size("/etc/ssh/{$f2c}")==0) {
74
				mwexec("rm /etc/ssh/ssh_host*", true);
75
			}
76
	}
77

    
78
	if (!is_dir("/var/empty")) {
79
		/* make ssh home directory */
80
		mkdir("/var/empty", 0555);
81
	}
82

    
83
	if(!file_exists("/var/log/lastlog")) {
84
		/* Login related files. */
85
		touch("/var/log/lastlog");
86
	}
87

    
88
	$sshConfigDir = "/etc/ssh";
89

    
90
	if($config['system']['ssh']['port'] <> "") {
91
		$sshport = $config['system']['ssh']['port'];
92
	} else {
93
		$sshport = 22;
94
	}
95

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

    
122
	/* Write the new sshd config file */
123
	$fd = fopen("/etc/ssh/sshd_config", "w");
124
	fwrite($fd, $sshconf);
125
	fclose($fd);
126

    
127
	/* mop up from a badly implemented ssh keys -> cf backup */
128
	if($config['ssh']['dsa_key'] <> "") {
129
		unset($config['ssh']['dsa_key']);
130
		unset($config['ssh']['rsa_key']);
131
		unset($config['ssh']['rsa1_key']);
132
		unset($config['ssh']['dsa']);
133
		unset($config['ssh']['rsa']);
134
		unset($config['ssh']['rsa1']);
135
		unset($config['ssh']['ak']);
136
		write_config("Clearing SSH keys from config.xml");
137
	}
138

    
139
	/* are we already running?  if so exit */
140
	if(is_subsystem_dirty('sshdkeys')) {
141
		conf_mount_ro();
142
		exit;
143
	}
144
	
145
	// Check for all needed key files. If any are missing, the keys need to be regenerated.
146
	$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');
147
	$generate_keys = false;
148
	foreach ($files_to_check as $f2c) {
149
		if (!file_exists("/etc/ssh/{$f2c}")) {
150
			$generate_keys = true;
151
		}
152
	}
153
	if ($generate_keys) {
154
		/* remove previous keys and regen later */
155
		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", "");
156
		mwexec("rm /etc/ssh/ssh_host_*", true);
157
		mark_subsystem_dirty('sshdkeys');
158
		echo " Generating Keys:\n";
159
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa1 -N '' -f $sshConfigDir/ssh_host_key");
160
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa -N '' -f $sshConfigDir/ssh_host_rsa_key");
161
		system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t dsa -N '' -f $sshConfigDir/ssh_host_dsa_key");
162
		clear_subsystem_dirty('sshdkeys');
163
		file_notice("SSH", "{$g['product_name']} has completed creating your SSH keys.  SSH is now started.", "SSH Startup", "");
164
		echo "Starting SSH... ";
165
	}
166

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

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

    
190
?>
(93-93/99)