Project

General

Profile

Download (6.42 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
#! /usr/local/bin/php -f
2
<?php
3
/*
4
	sshd - Modified to work on disk based system
5
	Copyright 2004 Scott K Ullrich
6 b2981d7a Scott Ullrich
7 5b237745 Scott Ullrich
	Original Copyright (C) 2004 Fred Mol <fredmol@xs4all.nl>.
8
	All rights reserved.
9 b2981d7a Scott Ullrich
10 5b237745 Scott Ullrich
	Redistribution and use in source and binary forms, with or without
11
	modification, are permitted provided that the following conditions are met:
12 b2981d7a Scott Ullrich
13 5b237745 Scott Ullrich
	1. Redistributions of source code must retain the above copyright notice,
14
	   this list of conditions and the following disclaimer.
15 b2981d7a Scott Ullrich
16 5b237745 Scott Ullrich
	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 b2981d7a Scott Ullrich
20 5b237745 Scott Ullrich
	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 b2981d7a Scott Ullrich
33 5b237745 Scott Ullrich
	$stderr = fopen("php://stderr", "w");
34
35 668b7b2e Scott Ullrich
	if(isset($config['system']['enablesshd'])) {
36
		/* do nothing, we're enabled */
37
	} else {
38
		if($g['booting'])
39
			echo "SSHD is disabled.";
40
	}
41 36aaefff Scott Ullrich
	
42 746b5ee2 Scott Ullrich
	function file_size($file) {
43
	  $size = filesize($file);
44
	  return $size;
45
	}
46
47
	/*    if any of these files are 0 bytes then they are corrupted.
48
	 *    remove them
49
	 */
50 03b42caa Scott Ullrich
	$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');
51 746b5ee2 Scott Ullrich
	foreach($files_to_check as $f2c) {
52 7e6d816a Scott Ullrich
		if(file_exists("/etc/ssh/{$f2c}"))
53
			if(file_size("/etc/ssh/{$f2c}")==0) {
54 c5a9b876 Scott Ullrich
				mwexec("rm /etc/ssh/ssh_host*");
55 5858cbdb Scott Ullrich
			}
56 746b5ee2 Scott Ullrich
	}
57
58 5b237745 Scott Ullrich
	if (!is_dir("/var/empty")) {
59 efa761f6 Scott Ullrich
		/* make ssh home directory */
60 5b237745 Scott Ullrich
		mkdir("/var/empty", 0555);
61
	}
62
63
	if(!file_exists("")) {
64 efa761f6 Scott Ullrich
		/* Login related files. */
65 5b237745 Scott Ullrich
		touch("/var/log/lastlog");
66
	}
67
68 efa761f6 Scott Ullrich
	/* Make the root passwords are the same as the web admin password. */
69 5b237745 Scott Ullrich
	$fd = popen("/usr/sbin/pw usermod -n root -H 0", "w");
70
	fwrite($fd, $config['system']['password']);
71
	pclose($fd);
72 efa761f6 Scott Ullrich
73
	/* Make the admin passwords are the same as the web admin password. */
74 128f6a3e Bill Marquette
	$fd = popen("/usr/sbin/pw usermod -n admin -H 0", "w");
75
	fwrite($fd, $config['system']['password']);
76
	pclose($fd);
77 5b237745 Scott Ullrich
78
	$sshConfigDir = "/etc/ssh";
79 850b71ec Scott Ullrich
80 426f300c Scott Ullrich
	if($config['ssh']['dsa_key'] <> "") {
81
		unset($config['ssh']['dsa_key']);
82
		unset($config['ssh']['rsa_key']);
83
		unset($config['ssh']['rsa1_key']);
84
		unset($config['ssh']['dsa']);
85
		unset($config['ssh']['rsa']);
86
		unset($config['ssh']['rsa1']);
87
		unset($config['ssh']['ak']);
88
		write_config("Clearing SSH keys from config.xml");
89
	}
90 c3290534 Scott Ullrich
	
91
	//if (!file_exists("$sshConfigDir/ssh_host_key") and $config['ssh']['dsa'] == "") {
92
	if (!file_exists("$sshConfigDir/ssh_host_key")) {
93 c2338828 Scott Ullrich
		/* remove previous keys and regen later */
94 0ae71d81 Scott Ullrich
		conf_mount_rw();
95 c2338828 Scott Ullrich
		mwexec("rm /etc/ssh/ssh_host_*");
96 09982081 Scott Ullrich
		echo "\n";
97 abafa16b Scott Ullrich
		system("/usr/bin/ssh-keygen -t rsa1 -N '' -f $sshConfigDir/ssh_host_key");
98
		system("/usr/bin/ssh-keygen -t rsa -N '' -f $sshConfigDir/ssh_host_rsa_key");
99
		system("/usr/bin/ssh-keygen -t dsa -N '' -f $sshConfigDir/ssh_host_dsa_key");
100 6b21d1ed Scott Ullrich
		
101 c2338828 Scott Ullrich
	}
102 efa761f6 Scott Ullrich
103 6b21d1ed Scott Ullrich
	system("/usr/sbin/sshd");
104
	fwrite($stderr, "Done.\n");
105 21b20aae Scott Ullrich
	exit;
106 6b21d1ed Scott Ullrich
107
	/* exit early, this needs more testing. */
108
109 efa761f6 Scott Ullrich
	if (!file_exists("$sshConfigDir/ssh_host_key") and $config['ssh']['dsa'] == "") {
110
		/* generate keys */
111 5b237745 Scott Ullrich
		system("/usr/bin/ssh-keygen -t rsa1 -N '' -f $sshConfigDir/ssh_host_key");
112
		system("/usr/bin/ssh-keygen -t rsa -N '' -f $sshConfigDir/ssh_host_rsa_key");
113
		system("/usr/bin/ssh-keygen -t dsa -N '' -f $sshConfigDir/ssh_host_dsa_key");
114 efa761f6 Scott Ullrich
115
		/* save keys */
116 7822d966 Colin Smith
		$dsa  = file_get_contents("{$sshConfigDir}/ssh_host_dsa_key");
117
		$rsa  = file_get_contents("{$sshConfigDir}/ssh_host_rsa_key");
118
		$rsa1 = file_get_contents("{$sshConfigDir}/ssh_host_key");
119 efa761f6 Scott Ullrich
		$config['ssh']['dsa'] = base64_encode($dsa);
120
		$config['ssh']['rsa'] = base64_encode($rsa);
121
		$config['ssh']['rsa1']= base64_encode($rsa1);
122
123
		/* save public keys */
124 7822d966 Colin Smith
		$dsapub  = file_get_contents("{$sshConfigDir}/ssh_host_dsa_key.pub");
125
		$rsapub  = file_get_contents("{$sshConfigDir}/ssh_host_rsa_key.pub");
126
		$rsa1pub = file_get_contents("{$sshConfigDir}/ssh_host_key.pub");
127 efa761f6 Scott Ullrich
		$config['ssh']['dsa_key'] = base64_encode($dsapub);
128
		$config['ssh']['rsa_key'] = base64_encode($rsapub);
129
		$config['ssh']['rsa1_key']= base64_encode($rsa1pub);
130 5fa404d4 Scott Ullrich
		write_config("Saved SSH keys.");
131 9e3fc88e Scott Ullrich
	} else {
132 efa761f6 Scott Ullrich
		/* restore keys */
133
		$rsa1 = base64_decode($config['ssh']['rsa1']);
134
		$rsa  = base64_decode($config['ssh']['rsa']);
135
		$dsa  = base64_decode($config['ssh']['dsa']);
136
		file_put_contents("{$sshConfigDir}/ssh_host_key", $rsa1);
137
		file_put_contents("{$sshConfigDir}/ssh_host_rsa_key", $rsa);
138
		file_put_contents("{$sshConfigDir}/ssh_host_dsa_key", $dsa);
139
140
		/* restore public keys */
141
		$rsa1_pub = base64_decode($config['ssh']['rsa1_key']);
142
		$rsa_pub  = base64_decode($config['ssh']['rsa_key']);
143
		$dsa_pub  = base64_decode($config['ssh']['dsa_key']);
144
		file_put_contents("{$sshConfigDir}/ssh_host_key.pub", $rsa1_pub);
145
		file_put_contents("{$sshConfigDir}/ssh_host_rsa_key.pub", $rsa_pub);
146
		file_put_contents("{$sshConfigDir}/ssh_host_dsa_key.pub", $dsa_pub);
147
148
		/* change keys owner to root */
149
		chown("{$sshConfigDir}/ssh_host_key", "root");
150
		chown("{$sshConfigDir}/ssh_host_rsa_key", "root");
151
		chown("{$sshConfigDir}/ssh_host_dsa_key", "root");
152
153
		/* change public keys owner to root */
154
		chown("{$sshConfigDir}/ssh_host_key.pub", "root");
155
		chown("{$sshConfigDir}/ssh_host_rsa_key.pub", "root");
156
		chown("{$sshConfigDir}/ssh_host_dsa_key.pub", "root");
157
158
		/* change mode on keys to u+rw */
159
		chmod("{$sshConfigDir}/ssh_host_key",0600);
160
		chmod("{$sshConfigDir}/ssh_host_rsa_key",0600);
161
		chmod("{$sshConfigDir}/ssh_host_dsa_key",0600);
162
163
		/* change mode on public keys to u+rw */
164
		chmod("{$sshConfigDir}/ssh_host_key.pub",0600);
165
		chmod("{$sshConfigDir}/ssh_host_rsa_key.pub",0600);
166
		chmod("{$sshConfigDir}/ssh_host_dsa_key.pub",0600);
167 5b237745 Scott Ullrich
	}
168
169 0a4869c5 Scott Ullrich
	if($config['ssh']['ak'] <> "") {
170
		$ak  = base64_decode($config['ssh']['ak']);
171
		file_put_contents("/root/.authorized_keys", $ak);
172
		chmod("/root/.authorized_keys",0600);
173
	}
174
175 efa761f6 Scott Ullrich
	/* start sshd */
176 abafa16b Scott Ullrich
	system("/usr/sbin/sshd");
177 5b237745 Scott Ullrich
	fwrite($stderr, "Done.\n");
178 efa761f6 Scott Ullrich
179 7822d966 Colin Smith
?>