firewall_plugin.inc.php 9.74 KB
Newer Older
tbrehm's avatar
tbrehm committed
1
2
3
<?php

/*
redray's avatar
redray committed
4
Copyright (c) 2007, Till Brehm, projektfarm Gmbh
tbrehm's avatar
tbrehm committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name of ISPConfig nor the names of its contributors
      may be used to endorse or promote products derived from this software without
      specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

class firewall_plugin {
32

33
34
	private $plugin_name = 'firewall_plugin';
	private $class_name  = 'firewall_plugin';
35

36
37
	//* This function is called during ispconfig installation to determine
	//  if a symlink shall be created for this plugin.
38
	public function onInstall() {
39
		global $conf;
40

41
		if(($conf['bastille']['installed'] == true || $conf['ufw']['installed'] == true || $conf['firewall']['installed'] == true) && $conf['services']['firewall'] == true) {
42
43
44
45
			return true;
		} else {
			return false;
		}
46

47
	}
48
49


tbrehm's avatar
tbrehm committed
50
51
52
	/*
	 	This function is called when the plugin is loaded
	*/
53

54
	public function onLoad() {
tbrehm's avatar
tbrehm committed
55
		global $app;
56

tbrehm's avatar
tbrehm committed
57
58
59
		/*
		Register for the events
		*/
60

tbrehm's avatar
tbrehm committed
61
		//* Mailboxes
62
63
64
		$app->plugins->registerEvent('firewall_insert', $this->plugin_name, 'insert');
		$app->plugins->registerEvent('firewall_update', $this->plugin_name, 'update');
		$app->plugins->registerEvent('firewall_delete', $this->plugin_name, 'delete');
65
	}
66
67
68


	public function insert($event_name, $data) {
69
		global $app, $conf;
70
71
72

		$this->update($event_name, $data);

tbrehm's avatar
tbrehm committed
73
	}
74
75

	public function update($event_name, $data) {
76
		global $app, $conf;
77

78
		//* load the server configuration options
79
80
81
82
83
84
85
86
		if(!$data['mirrored']) {
			$app->uses('getconf');
			$server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
			if($server_config['firewall'] == 'ufw') {
				$this->ufw_update($event_name, $data);
			} else {
				$this->bastille_update($event_name, $data);
			}
87
88
		}
	}
89
90

	public function delete($event_name, $data) {
tbrehm's avatar
tbrehm committed
91
		global $app, $conf;
92

93
		//* load the server configuration options
94
95
96
97
98
99
100
101
102
		if(!$data['mirrored']) {
			$app->uses('getconf');
			$server_config = $app->getconf->get_server_config($conf['server_id'], 'server');

			if($server_config['firewall'] == 'ufw') {
				$this->ufw_delete($event_name, $data);
			} else {
				$this->bastille_delete($event_name, $data);
			}
103
		}
tbrehm's avatar
tbrehm committed
104
	}
105
106

	private function ufw_update($event_name, $data) {
tbrehm's avatar
tbrehm committed
107
		global $app, $conf;
108

109
		$app->uses('system');
110

111
		if(!$app->system->is_installed('ufw')) {
112
			$app->log('UFW Firewall is not installed', LOGLEVEL_WARN);
113
114
			return false;
		}
115
116
117

		exec('ufw --version', $out);
		$parts = explode(' ', $out[0]);
118
119
120
121
		$ufwversion = $parts[1];
		unset($parts);
		unset($out);

122
123
		if(version_compare( $ufwversion , '0.30') < 0) {
			$app->log('The installed UFW Firewall version is too old. Minimum required version 0.30', LOGLEVEL_WARN);
124
125
			return false;
		}
126

127
128
129
130
131
132
133
		//* Basic firewall setup when the firewall is added the first time
		if($event_name == 'firewall_insert') {
			exec('ufw --force disable');
			exec('ufw --force reset');
			exec('ufw default deny incoming');
			exec('ufw default allow outgoing');
		}
134
135
136
137
138
139
140
141
142
143
144

		$tcp_ports_new = $this->clean_ports($data['new']['tcp_port'], ',');
		$tcp_ports_old = $this->clean_ports($data['old']['tcp_port'], ',');
		$udp_ports_new = $this->clean_ports($data['new']['udp_port'], ',');
		$udp_ports_old = $this->clean_ports($data['old']['udp_port'], ',');

		$tcp_ports_new_array = explode(',', $tcp_ports_new);
		$tcp_ports_old_array = explode(',', $tcp_ports_old);
		$udp_ports_new_array = explode(',', $udp_ports_new);
		$udp_ports_old_array = explode(',', $udp_ports_old);

145
146
		//* add tcp ports
		foreach($tcp_ports_new_array as $port) {
147
			if(!in_array($port, $tcp_ports_old_array) && $port > 0) {
148
				exec('ufw allow '.$port.'/tcp');
149
				$app->log('ufw allow '.$port.'/tcp', LOGLEVEL_DEBUG);
150
				sleep(1);
tbrehm's avatar
tbrehm committed
151
152
			}
		}
153

154
155
		//* remove tcp ports
		foreach($tcp_ports_old_array as $port) {
156
			if(!in_array($port, $tcp_ports_new_array) && $port > 0) {
157
				exec('ufw delete allow '.$port.'/tcp');
158
				$app->log('ufw delete allow '.$port.'/tcp', LOGLEVEL_DEBUG);
159
160
161
				sleep(1);
			}
		}
162

163
164
		//* add udp ports
		foreach($udp_ports_new_array as $port) {
165
			if(!in_array($port, $udp_ports_old_array) && $port > 0) {
166
				exec('ufw allow '.$port.'/udp');
167
				$app->log('ufw allow '.$port.'/udp', LOGLEVEL_DEBUG);
168
169
170
				sleep(1);
			}
		}
171

172
173
		//* remove udp ports
		foreach($udp_ports_old_array as $port) {
174
			if(!in_array($port, $udp_ports_new_array) && $port > 0) {
175
				exec('ufw delete allow '.$port.'/udp');
176
				$app->log('ufw delete allow '.$port.'/udp', LOGLEVEL_DEBUG);
177
178
179
				sleep(1);
			}
		}
180

181
182
183
184
185
186
187
188
189
		/*
		if($tcp_ports_new != $tcp_ports_old) {
			exec('ufw allow to any proto tcp port '.$tcp_ports_new);
			$app->log('ufw allow to any proto tcp port '.$tcp_ports_new,LOGLEVEL_DEBUG);
			if($event_name == 'firewall_update') {
				exec('ufw delete allow to any proto tcp port '.$tcp_ports_old);
				$app->log('ufw delete allow to any proto tcp port '.$tcp_ports_old,LOGLEVEL_DEBUG);
			}
		}
190

191
192
193
194
195
196
197
198
199
		if($udp_ports_new != $udp_ports_old) {
			exec('ufw allow to any proto udp port '.$udp_ports_new);
			$app->log('ufw allow to any proto udp port '.$udp_ports_new,LOGLEVEL_DEBUG);
			if($event_name == 'firewall_update') {
				exec('ufw delete allow to any proto udp port '.$udp_ports_old);
				$app->log('ufw delete allow to any proto udp port '.$udp_ports_old,LOGLEVEL_DEBUG);
			}
		}
		*/
200

201
202
203
		if($data['new']['active'] == 'y') {
			if($data['new']['active'] == $data['old']['active']) {
				exec('ufw reload');
204
				$app->log('Reloading the firewall', LOGLEVEL_DEBUG);
205
206
			} else {
				//* Ensure that bastille firewall is stopped
207
				exec($conf['init_scripts'] . '/' . 'bastille-firewall stop 2>/dev/null');
208
				if(@is_file('/etc/debian_version')) exec('update-rc.d -f bastille-firewall remove');
209

210
211
				//* Start ufw firewall
				exec('ufw --force enable');
212
				$app->log('Starting the firewall', LOGLEVEL_DEBUG);
tbrehm's avatar
tbrehm committed
213
			}
214
215
		} else {
			exec('ufw disable');
216
			$app->log('Stopping the firewall', LOGLEVEL_DEBUG);
217
218
		}
	}
219
220

	private function ufw_delete($event_name, $data) {
221
		global $app, $conf;
222

223
		$app->uses('system');
224

225
		if(!$app->system->is_installed('ufw')) {
226
			$app->log('UFW Firewall is not installed', LOGLEVEL_DEBUG);
227
			return false;
tbrehm's avatar
tbrehm committed
228
		}
229

230
231
		exec('ufw --force reset');
		exec('ufw disable');
232
233
		$app->log('Stopping the firewall', LOGLEVEL_DEBUG);

234
	}
235
236

	private function bastille_update($event_name, $data) {
237
		global $app, $conf;
238

239
		$app->uses('system');
240
241
242
243

		$tcp_ports = $this->clean_ports($data['new']['tcp_port'], ' ');
		$udp_ports = $this->clean_ports($data['new']['udp_port'], ' ');

tbrehm's avatar
tbrehm committed
244
		$app->load('tpl');
tbrehm's avatar
tbrehm committed
245
		$tpl = new tpl();
246
		$tpl->newTemplate('bastille-firewall.cfg.master');
247
248
249
250
251
252

		$tpl->setVar('TCP_PUBLIC_SERVICES', $tcp_ports);
		$tpl->setVar('UDP_PUBLIC_SERVICES', $udp_ports);

		file_put_contents('/etc/Bastille/bastille-firewall.cfg', $tpl->grab());
		$app->log('Writing firewall configuration /etc/Bastille/bastille-firewall.cfg', LOGLEVEL_DEBUG);
tbrehm's avatar
tbrehm committed
253
		unset($tpl);
254

255
		if($data['new']['active'] == 'y') {
256
257
258
259
			//* ensure that ufw firewall is disabled in case both firewalls are installed
			if($app->system->is_installed('ufw')) {
				exec('ufw disable');
			}
260
			exec($conf['init_scripts'] . '/' . 'bastille-firewall restart 2>/dev/null');
tbrehm's avatar
tbrehm committed
261
			if(@is_file('/etc/debian_version')) exec('update-rc.d bastille-firewall defaults');
262
			if(@is_file('/sbin/insserv')) exec('insserv -d bastille-firewall');
263
			$app->log('Restarting the firewall', LOGLEVEL_DEBUG);
tbrehm's avatar
tbrehm committed
264
		} else {
265
			exec($conf['init_scripts'] . '/' . 'bastille-firewall stop 2>/dev/null');
266
			if(@is_file('/etc/debian_version')) exec('update-rc.d -f bastille-firewall remove');
267
			if(@is_file('/sbin/insserv')) exec('insserv -r -f bastille-firewall');
268
			$app->log('Stopping the firewall', LOGLEVEL_DEBUG);
tbrehm's avatar
tbrehm committed
269
		}
270
271


tbrehm's avatar
tbrehm committed
272
	}
273
274

	private function bastille_delete($event_name, $data) {
tbrehm's avatar
tbrehm committed
275
		global $app, $conf;
276

277
		exec($conf['init_scripts'] . '/' . 'bastille-firewall stop 2>/dev/null');
278
		if(@is_file('/etc/debian_version')) exec('update-rc.d -f bastille-firewall remove');
279
		if(@is_file('/sbin/insserv')) exec('insserv -r -f bastille-firewall');
280
281
		$app->log('Stopping the firewall', LOGLEVEL_DEBUG);

tbrehm's avatar
tbrehm committed
282
	}
283
284
285
286
287


	private function clean_ports($portlist, $spacer) {

		$ports = explode(',', $portlist);
288
		$ports_out = '';
289

290
291
292
		if(is_array($ports)) {
			foreach($ports as $p) {
				$p_clean = '';
293
294
				if(strstr($p, ':')) {
					$p_parts = explode(':', $p);
295
296
297
298
299
300
301
302
303
304
305
306
					$tmp_lower = intval($p_parts[0]);
					$tmp_higher = intval($p_parts[1]);
					if($tmp_lower > 0 && $tmp_lower <= 65535 && $tmp_higher > 0 && $tmp_higher <= 65535 && $tmp_lower < $tmp_higher) {
						$p_clean = $tmp_lower.':'.$tmp_higher;
					}
				} else {
					$tmp = intval($p);
					if($tmp > 0 && $tmp <= 65535) {
						$p_clean = $tmp;
					}
				}
				if($p_clean != '') $ports_out .= $p_clean . $spacer;
307

308
309
			}
		}
310
		return substr($ports_out, 0, strlen($spacer)*-1);
311
	}
312
313


tbrehm's avatar
tbrehm committed
314
315
316

} // end class

317
?>