Projet

Général

Profil

Télécharger (7,03 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / etc / sshd @ 2b56c7da

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
	/* are we already running?  if not, do conf_mount_rw(), otherwise it should already be rw */
46
	if(!is_subsystem_dirty('sshdkeys')) {
47
		conf_mount_rw();
48
	}
49

    
50
	function file_size($file) {
51
	  $size = filesize($file);
52
	  return $size;
53
	}
54

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

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

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

    
81
	if (!is_dir("/var/empty")) {
82
		/* make ssh home directory */
83
		mkdir("/var/empty", 0555);
84
	}
85

    
86
	if(!file_exists("/var/log/lastlog")) {
87
		/* Login related files. */
88
		touch("/var/log/lastlog");
89
	}
90

    
91
	$sshConfigDir = "/etc/ssh";
92

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

    
99
	/* Include default configuration for pfSense */
100
	$sshconf = "# This file is automatically generated at startup\n";
101
	$sshconf .= "Ciphers aes128-ctr,aes256-ctr,arcfour256,arcfour,aes128-cbc,aes256-cbc\n";
102
	$sshconf .= "PermitRootLogin yes\n";
103
	$sshconf .= "Compression yes\n";
104
	$sshconf .= "ClientAliveInterval 30\n";
105
	$sshconf .= "UseDNS no\n";
106
	$sshconf .= "X11Forwarding no\n";
107
	if(isset($config['system']['ssh']['sshdkeyonly'])) {
108
		$sshconf .= "# Login via Key only\n";
109
		$sshconf .= "PasswordAuthentication no\n";
110
		$sshconf .= "ChallengeResponseAuthentication no\n";
111
		$sshconf .= "PubkeyAuthentication yes\n";
112
	} else {
113
		$sshconf .= "# Login via Key and Password\n";
114
		$sshconf .= "PasswordAuthentication yes\n";
115
		$sshconf .= "ChallengeResponseAuthentication yes\n";
116
		$sshconf .= "PubkeyAuthentication yes\n";
117
	}
118
	$sshconf .= "# override default of no subsystems\n";
119
	$sshconf .= "Subsystem       sftp    /usr/libexec/sftp-server\n";
120
	/* Only allow protocol 2, because we say so */
121
	$sshconf .= "Protocol 2\n";
122
	/* Run the server on another port if we have one defined */
123
	$sshconf .= "Port $sshport\n";
124
	/* Hide FreeBSD version */
125
	$sshconf .= "VersionAddendum \n";
126
	
127
	/* Apply package SSHDCond settings if config file exists */
128
	if(file_exists("/etc/sshd_extra"))
129
	{
130
		$fdExtra = fopen("/etc/sshd_extra", 'r');
131
		$szExtra = fread($fdExtra, 1048576); // Read up to 1MB from extra file
132
		$sshconf .= $szExtra;
133
		fclose($fdExtra);
134
	}
135

    
136
	/* Write the new sshd config file */
137
	$fd = fopen("/etc/ssh/sshd_config", "w");
138
	fwrite($fd, $sshconf);
139
	fclose($fd);
140

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

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

    
180
	/* kill existing sshd process, server only, not the childs */
181
	$sshd_pid = exec("ps ax | egrep '/usr/sbin/[s]shd' | awk '{print $1}'");
182
	if($sshd_pid <> "") {
183
		echo "stopping ssh process $sshd_pid \n";
184
		mwexec("kill $sshd_pid");
185
	}
186
	/* Launch new server process */
187
	$status = mwexec("/usr/sbin/sshd");
188
	if($status <> 0) {
189
		file_notice("sshd_startup", "SSHD failed to start.", "SSHD Daemon", "");
190
		echo "error!\n";
191
	} else {
192
		echo "done.\n";
193
	}
194

    
195
	// NanoBSD
196
	if($g['platform'] == "nanobsd") {
197
		if(!is_dir("/conf/sshd"))
198
			exec("mkdir /conf/sshd");
199
		exec("/bin/cp -p /etc/ssh/ssh_host* /conf/sshd");
200
	}
201
	conf_mount_ro();
202

    
203
?>
(104-104/110)