Projet

Général

Profil

Télécharger (6,98 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / etc / sshd @ c650b2f7

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
		return;
39
	}
40

    
41
	/* are we already running?  if not, do conf_mount_rw(), otherwise it should already be rw */
42
	if (!is_subsystem_dirty('sshdkeys')) {
43
		conf_mount_rw();
44
	}
45

    
46
	$keys = array(
47
		'ssh_host_key',
48
		'ssh_host_key.pub',
49
		'ssh_host_dsa_key',
50
		'ssh_host_dsa_key.pub',
51
		'ssh_host_rsa_key',
52
		'ssh_host_rsa_key.pub',
53
		'ssh_host_ecdsa_key',
54
		'ssh_host_ecdsa_key.pub',
55
		'ssh_host_ed25519_key',
56
		'ssh_host_ed25519_key.pub'
57
	);
58

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

    
64
		/* make sure host private key permissions aren't too open so sshd won't complain */
65
		foreach($keys as $f2c) {
66
			if(file_exists("/etc/ssh/{$f2c}"))
67
				chmod("/etc/ssh/{$f2c}", 0600);
68
		}
69
	}
70

    
71
	/*    if any of these files are 0 bytes then they are corrupted.
72
	 *    remove them
73
	 */
74
	foreach($keys as $f2c) {
75
		if (file_exists("/etc/ssh/{$f2c}") && filesize("/etc/ssh/{$f2c}") == 0) {
76
			unlink_if_exists('/etc/ssh/ssh_host*');
77
			break;
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 (is_array($config['system']['ssh']) && !empty($config['system']['ssh']['port']))
94
		$sshport = $config['system']['ssh']['port'];
95
	else
96
		$sshport = 22;
97

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

    
132
	/* Write the new sshd config file */
133
	@file_put_contents("/etc/ssh/sshd_config", $sshconf);
134

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

    
149
	/* are we already running?  if so exit */
150
	if(is_subsystem_dirty('sshdkeys')) {
151
		unset($keys);
152
		return;
153
	}
154
	
155
	// Check for all needed key files. If any are missing, the keys need to be regenerated.
156
	$generate_keys = false;
157
	foreach ($keys as $f2c) {
158
		if (!file_exists("/etc/ssh/{$f2c}")) {
159
			$generate_keys = true;
160
			break;
161
		}
162
	}
163

    
164
	if ($generate_keys) {
165
		/* remove previous keys and regen later */
166
		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", "");
167
		unlink_if_exists('/etc/ssh/ssh_host_*');
168
		mark_subsystem_dirty('sshdkeys');
169
		echo " Generating Keys:\n";
170
		$_gb = exec("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa1 -N '' -f $sshConfigDir/ssh_host_key");
171
		$_gb = exec("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa -N '' -f $sshConfigDir/ssh_host_rsa_key");
172
		$_gb = exec("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t dsa -N '' -f $sshConfigDir/ssh_host_dsa_key");
173
		$_gb = exec("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t ecdsa -N '' -f $sshConfigDir/ssh_host_ecdsa_key");
174
		$_gb = exec("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t ed25519 -N '' -f $sshConfigDir/ssh_host_ed25519_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
	}
178

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

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