|
1
|
|
|
#!/usr/bin/php |
|
2
|
|
|
<?php |
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (C) 2016 SURFnet. |
|
5
|
|
|
* |
|
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
8
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
9
|
|
|
* License, or (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* This program is distributed in the hope that it will be useful, |
|
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
* GNU Affero General Public License for more details. |
|
15
|
|
|
* |
|
16
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
17
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
18
|
|
|
*/ |
|
19
|
|
|
require_once sprintf('%s/vendor/autoload.php', dirname(__DIR__)); |
|
20
|
|
|
|
|
21
|
|
|
use SURFnet\VPN\Server\Config\Firewall; |
|
22
|
|
|
use SURFnet\VPN\Server\InstanceConfig; |
|
23
|
|
|
use SURFnet\VPN\Common\FileIO; |
|
24
|
|
|
use SURFnet\VPN\Server\CliParser; |
|
25
|
|
|
|
|
26
|
|
|
try { |
|
27
|
|
|
$p = new CliParser( |
|
28
|
|
|
'Generate firewall rules for all instances', |
|
29
|
|
|
[ |
|
30
|
|
|
'install' => ['install the firewall', false, false], |
|
31
|
|
|
] |
|
32
|
|
|
); |
|
33
|
|
|
|
|
34
|
|
|
$opt = $p->parse($argv); |
|
35
|
|
|
if ($opt->e('help')) { |
|
36
|
|
|
echo $p->help(); |
|
37
|
|
|
exit(0); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
// detect all instances |
|
41
|
|
|
$configList = []; |
|
42
|
|
|
$configDir = sprintf('%s/config', dirname(__DIR__)); |
|
43
|
|
|
foreach (glob(sprintf('%s/*', $configDir), GLOB_ONLYDIR | GLOB_ERR) as $instanceDir) { |
|
44
|
|
|
$instanceId = basename($instanceDir); |
|
45
|
|
|
$configList[$instanceId] = InstanceConfig::fromFile(sprintf('%s/%s/config.yaml', $configDir, $instanceId)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$firewall = Firewall::getFirewall4($configList); |
|
49
|
|
|
$firewall6 = Firewall::getFirewall6($configList); |
|
50
|
|
|
|
|
51
|
|
|
if ($opt->e('install')) { |
|
52
|
|
|
FileIO::writeFile('/etc/sysconfig/iptables', $firewall); |
|
53
|
|
|
FileIO::writeFile('/etc/sysconfig/ip6tables', $firewall6); |
|
54
|
|
|
} else { |
|
55
|
|
|
echo '##########################################'.PHP_EOL; |
|
56
|
|
|
echo '# IPv4'.PHP_EOL; |
|
57
|
|
|
echo '##########################################'.PHP_EOL; |
|
58
|
|
|
echo $firewall; |
|
59
|
|
|
|
|
60
|
|
|
echo '##########################################'.PHP_EOL; |
|
61
|
|
|
echo '# IPv6'.PHP_EOL; |
|
62
|
|
|
echo '##########################################'.PHP_EOL; |
|
63
|
|
|
echo $firewall6; |
|
64
|
|
|
} |
|
65
|
|
|
} catch (Exception $e) { |
|
66
|
|
|
echo sprintf('ERROR: %s', $e->getMessage()).PHP_EOL; |
|
67
|
|
|
exit(1); |
|
68
|
|
|
} |
|
69
|
|
|
|