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
|
|
33
|
if(isset($config['system']['enablesshd'])) {
|
34
|
/* do nothing, we're enabled */
|
35
|
} else {
|
36
|
if($g['booting'])
|
37
|
echo "SSHD is disabled.";
|
38
|
exit;
|
39
|
}
|
40
|
|
41
|
function file_size($file) {
|
42
|
$size = filesize($file);
|
43
|
return $size;
|
44
|
}
|
45
|
|
46
|
/* if any of these files are 0 bytes then they are corrupted.
|
47
|
* remove them
|
48
|
*/
|
49
|
$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/.authorized_keys');
|
50
|
foreach($files_to_check as $f2c) {
|
51
|
if(file_exists("/etc/ssh/{$f2c}"))
|
52
|
if(file_size("/etc/ssh/{$f2c}")==0) {
|
53
|
mwexec("rm /etc/ssh/ssh_host*");
|
54
|
}
|
55
|
}
|
56
|
|
57
|
if (!is_dir("/var/empty")) {
|
58
|
/* make ssh home directory */
|
59
|
mkdir("/var/empty", 0555);
|
60
|
}
|
61
|
|
62
|
if(!file_exists("")) {
|
63
|
/* Login related files. */
|
64
|
touch("/var/log/lastlog");
|
65
|
}
|
66
|
|
67
|
/* Make the root passwords are the same as the web admin password. */
|
68
|
conf_mount_rw();
|
69
|
$fd = popen("/usr/sbin/pw usermod -n root -H 0", "w");
|
70
|
fwrite($fd, $config['system']['password']);
|
71
|
pclose($fd);
|
72
|
|
73
|
/* Make the admin passwords are the same as the web admin password. */
|
74
|
$fd = popen("/usr/sbin/pw usermod -n admin -H 0", "w");
|
75
|
fwrite($fd, $config['system']['password']);
|
76
|
pclose($fd);
|
77
|
|
78
|
$sshConfigDir = "/etc/ssh";
|
79
|
|
80
|
if($config['system']['ssh']['port'] <> "") {
|
81
|
$sshport = $config['system']['ssh']['port'];
|
82
|
} else {
|
83
|
$sshport = 22;
|
84
|
}
|
85
|
|
86
|
/* Include default configuration for pfSense */
|
87
|
$sshconf = "# This file is automatically generated at startup\n";
|
88
|
$sshconf .= "PermitRootLogin yes\n";
|
89
|
$sshconf .= "Compression yes\n";
|
90
|
$sshconf .= "ClientAliveInterval 30\n";
|
91
|
$sshconf .= "UseDNS no\n";
|
92
|
$sshconf .= "X11Forwarding no\n";
|
93
|
$sshconf .= "# override default of no subsystems\n";
|
94
|
$sshconf .= "Subsystem sftp /usr/libexec/sftp-server\n";
|
95
|
/* Only allow protocol 2, because we say so */
|
96
|
$sshconf .= "Protocol 2\n";
|
97
|
/* Run the server on another port if we have one defined */
|
98
|
$sshconf .= "Port $sshport\n";
|
99
|
|
100
|
/* Write the new sshd config file */
|
101
|
$fd = fopen("/etc/ssh/sshd_config", "w");
|
102
|
fwrite($fd, $sshconf);
|
103
|
pclose($fd);
|
104
|
|
105
|
/* mop up from a badly implemented ssh keys -> cf backup */
|
106
|
if($config['ssh']['dsa_key'] <> "") {
|
107
|
unset($config['ssh']['dsa_key']);
|
108
|
unset($config['ssh']['rsa_key']);
|
109
|
unset($config['ssh']['rsa1_key']);
|
110
|
unset($config['ssh']['dsa']);
|
111
|
unset($config['ssh']['rsa']);
|
112
|
unset($config['ssh']['rsa1']);
|
113
|
unset($config['ssh']['ak']);
|
114
|
write_config("Clearing SSH keys from config.xml");
|
115
|
}
|
116
|
|
117
|
/* are we already running? if so exit */
|
118
|
if(file_exists("/tmp/keys_generating"))
|
119
|
exit;
|
120
|
|
121
|
if (!file_exists("$sshConfigDir/ssh_host_key") or file_exists("/etc/keys_generating")) {
|
122
|
/* remove previous keys and regen later */
|
123
|
conf_mount_rw();
|
124
|
mwexec("rm /etc/ssh/ssh_host_*");
|
125
|
touch("/etc/keys_generating");
|
126
|
touch("/tmp/keys_generating");
|
127
|
echo " Generating Keys:\n";
|
128
|
system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa1 -N '' -f $sshConfigDir/ssh_host_key");
|
129
|
system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa -N '' -f $sshConfigDir/ssh_host_rsa_key");
|
130
|
system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t dsa -N '' -f $sshConfigDir/ssh_host_dsa_key");
|
131
|
unlink("/etc/keys_generating");
|
132
|
unlink("/tmp/keys_generating");
|
133
|
echo "Starting SSH... ";
|
134
|
}
|
135
|
|
136
|
/* kill existing sshd process, server only, not the childs */
|
137
|
$sshd_pid = exec("ps ax | egrep '/usr/sbin/[s]shd' | awk '{print $1}'");
|
138
|
if($sshd_pid <> "") {
|
139
|
echo "stopping ssh process $sshd_pid \n";
|
140
|
mwexec("kill $sshd_pid");
|
141
|
}
|
142
|
/* Launch new server process */
|
143
|
$status = mwexec("/usr/sbin/sshd");
|
144
|
if($status <> 0) {
|
145
|
file_notice("sshd_startup", "SSHD failed to start.", "SSHD Daemon", "");
|
146
|
echo "error!\n";
|
147
|
} else {
|
148
|
echo "done.\n";
|
149
|
}
|
150
|
|
151
|
conf_mount_ro();
|
152
|
|
153
|
|
154
|
|
155
|
|
156
|
|
157
|
|
158
|
|
159
|
|
160
|
|
161
|
|
162
|
|
163
|
|
164
|
|
165
|
|
166
|
|
167
|
|
168
|
|
169
|
|
170
|
|
171
|
|
172
|
|
173
|
|
174
|
|
175
|
|
176
|
|
177
|
|
178
|
|
179
|
|
180
|
|
181
|
|
182
|
|
183
|
|
184
|
|
185
|
|
186
|
|
187
|
|
188
|
|
189
|
|
190
|
exit;
|
191
|
|
192
|
/* exit early, this needs more testing. */
|
193
|
|
194
|
if (!file_exists("$sshConfigDir/ssh_host_key") and $config['ssh']['dsa'] == "") {
|
195
|
/* generate keys */
|
196
|
system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa1 -N '' -f $sshConfigDir/ssh_host_key");
|
197
|
system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t rsa -N '' -f $sshConfigDir/ssh_host_rsa_key");
|
198
|
system("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t dsa -N '' -f $sshConfigDir/ssh_host_dsa_key");
|
199
|
|
200
|
/* save keys */
|
201
|
$dsa = file_get_contents("{$sshConfigDir}/ssh_host_dsa_key");
|
202
|
$rsa = file_get_contents("{$sshConfigDir}/ssh_host_rsa_key");
|
203
|
$rsa1 = file_get_contents("{$sshConfigDir}/ssh_host_key");
|
204
|
$config['ssh']['dsa'] = base64_encode($dsa);
|
205
|
$config['ssh']['rsa'] = base64_encode($rsa);
|
206
|
$config['ssh']['rsa1']= base64_encode($rsa1);
|
207
|
|
208
|
/* save public keys */
|
209
|
$dsapub = file_get_contents("{$sshConfigDir}/ssh_host_dsa_key.pub");
|
210
|
$rsapub = file_get_contents("{$sshConfigDir}/ssh_host_rsa_key.pub");
|
211
|
$rsa1pub = file_get_contents("{$sshConfigDir}/ssh_host_key.pub");
|
212
|
$config['ssh']['dsa_key'] = base64_encode($dsapub);
|
213
|
$config['ssh']['rsa_key'] = base64_encode($rsapub);
|
214
|
$config['ssh']['rsa1_key']= base64_encode($rsa1pub);
|
215
|
write_config("Saved SSH keys.");
|
216
|
} else {
|
217
|
/* restore keys */
|
218
|
$rsa1 = base64_decode($config['ssh']['rsa1']);
|
219
|
$rsa = base64_decode($config['ssh']['rsa']);
|
220
|
$dsa = base64_decode($config['ssh']['dsa']);
|
221
|
file_put_contents("{$sshConfigDir}/ssh_host_key", $rsa1);
|
222
|
file_put_contents("{$sshConfigDir}/ssh_host_rsa_key", $rsa);
|
223
|
file_put_contents("{$sshConfigDir}/ssh_host_dsa_key", $dsa);
|
224
|
|
225
|
/* restore public keys */
|
226
|
$rsa1_pub = base64_decode($config['ssh']['rsa1_key']);
|
227
|
$rsa_pub = base64_decode($config['ssh']['rsa_key']);
|
228
|
$dsa_pub = base64_decode($config['ssh']['dsa_key']);
|
229
|
file_put_contents("{$sshConfigDir}/ssh_host_key.pub", $rsa1_pub);
|
230
|
file_put_contents("{$sshConfigDir}/ssh_host_rsa_key.pub", $rsa_pub);
|
231
|
file_put_contents("{$sshConfigDir}/ssh_host_dsa_key.pub", $dsa_pub);
|
232
|
|
233
|
/* change keys owner to root */
|
234
|
chown("{$sshConfigDir}/ssh_host_key", "root");
|
235
|
chown("{$sshConfigDir}/ssh_host_rsa_key", "root");
|
236
|
chown("{$sshConfigDir}/ssh_host_dsa_key", "root");
|
237
|
|
238
|
/* change public keys owner to root */
|
239
|
chown("{$sshConfigDir}/ssh_host_key.pub", "root");
|
240
|
chown("{$sshConfigDir}/ssh_host_rsa_key.pub", "root");
|
241
|
chown("{$sshConfigDir}/ssh_host_dsa_key.pub", "root");
|
242
|
|
243
|
/* change mode on keys to u+rw */
|
244
|
chmod("{$sshConfigDir}/ssh_host_key",0600);
|
245
|
chmod("{$sshConfigDir}/ssh_host_rsa_key",0600);
|
246
|
chmod("{$sshConfigDir}/ssh_host_dsa_key",0600);
|
247
|
|
248
|
/* change mode on public keys to u+rw */
|
249
|
chmod("{$sshConfigDir}/ssh_host_key.pub",0600);
|
250
|
chmod("{$sshConfigDir}/ssh_host_rsa_key.pub",0600);
|
251
|
chmod("{$sshConfigDir}/ssh_host_dsa_key.pub",0600);
|
252
|
}
|
253
|
|
254
|
if($config['ssh']['ak'] <> "") {
|
255
|
$ak = base64_decode($config['ssh']['ak']);
|
256
|
file_put_contents("/root/.authorized_keys", $ak);
|
257
|
chmod("/root/.authorized_keys",0600);
|
258
|
}
|
259
|
|
260
|
/* start sshd */
|
261
|
system("/usr/sbin/sshd");
|
262
|
echo "done.\n";
|
263
|
|
264
|
?>
|