GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 1ec020...a4523c )
by François
06:27 queued 03:56
created

Firewall::getFirewall4()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
/**
3
 *  Copyright (C) 2016 SURFnet.
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU Affero General Public License as
7
 *  published by the Free Software Foundation, either version 3 of the
8
 *  License, or (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU Affero General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU Affero General Public License
16
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
namespace SURFnet\VPN\Server\Config;
19
20
use SURFnet\VPN\Server\InstanceConfig;
21
use SURFnet\VPN\Server\PoolConfig;
22
use SURFnet\VPN\Server\IP;
23
24
class Firewall
25
{
26
    public static function getFirewall4(array $instanceConfigList, $asArray = false)
27
    {
28
        return self::getFirewall($instanceConfigList, 4, $asArray);
29
    }
30
31
    public static function getFirewall6(array $instanceConfigList, $asArray = false)
32
    {
33
        return self::getFirewall($instanceConfigList, 6, $asArray);
34
    }
35
36
    private static function getFirewall(array $instanceConfigList, $inetFamily, $asArray)
37
    {
38
        $firewall = [];
39
40
        // NAT
41
        $firewall = array_merge(
42
            $firewall,
43
             [
44
            '*nat',
45
            ':PREROUTING ACCEPT [0:0]',
46
            ':INPUT ACCEPT [0:0]',
47
            ':OUTPUT ACCEPT [0:0]',
48
            ':POSTROUTING ACCEPT [0:0]',
49
            ]
50
        );
51
        // add all instances
52
        foreach ($instanceConfigList as $instanceConfig) {
53
            $firewall = array_merge($firewall, self::getNat($instanceConfig, $inetFamily));
54
        }
55
        $firewall[] = 'COMMIT';
56
57
        // FILTER
58
        $firewall = array_merge(
59
            $firewall,
60
            [
61
                '*filter',
62
                ':INPUT ACCEPT [0:0]',
63
                ':FORWARD ACCEPT [0:0]',
64
                ':OUTPUT ACCEPT [0:0]',
65
            ]
66
        );
67
68
        // INPUT
69
        $firewall = array_merge($firewall, self::getInputChain($inetFamily));
70
71
        // FORWARD
72
        $firewall = array_merge(
73
            $firewall,
74
            [
75
                //sprintf('-A FORWARD -p %s -j ACCEPT', 4 === $inetFamily ? 'icmp' : 'ipv6-icmp'),
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
76
                '-A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT',
77
            ]
78
        );
79
80
        // add all instances
81
        foreach ($instanceConfigList as $instanceConfig) {
82
            $firewall = array_merge($firewall, self::getForwardChain($instanceConfig, $inetFamily));
83
        }
84
        $firewall[] = sprintf('-A FORWARD -j REJECT --reject-with %s', 4 === $inetFamily ? 'icmp-host-prohibited' : 'icmp6-adm-prohibited');
85
        $firewall[] = 'COMMIT';
86
87
        if ($asArray) {
88
            return $firewall;
89
        }
90
91
        return implode(PHP_EOL, $firewall).PHP_EOL;
92
    }
93
94
    private static function getNat(InstanceConfig $instanceConfig, $inetFamily)
95
    {
96
        $nat = [];
97
98
        foreach (array_keys($instanceConfig->v('vpnPools')) as $poolNumber => $poolId) {
99
            $poolConfig = new PoolConfig($instanceConfig->v('vpnPools', $poolId));
100
            if ($poolConfig->v('useNat')) {
101
                if (4 === $inetFamily) {
102
                    // get the IPv4 range
103
                    $srcNet = $poolConfig->v('range');
104
                } else {
105
                    // get the IPv6 range
106
                    $srcNet = $poolConfig->v('range6');
107
                }
108
                // -i (--in-interface) cannot be specified for POSTROUTING
109
                $nat[] = sprintf('-A POSTROUTING -s %s -o %s -j MASQUERADE', $srcNet, $poolConfig->v('extIf'));
110
            }
111
        }
112
113
        return $nat;
114
    }
115
116
    private static function getInputChain($inetFamily)
117
    {
118
        $inputChain = [
119
            '-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT',
120
            sprintf('-A INPUT -p %s -j ACCEPT', 4 === $inetFamily ? 'icmp' : 'ipv6-icmp'),
121
            '-A INPUT -i lo -j ACCEPT',
122
            '-A INPUT -m state --state NEW -m multiport -p tcp --dports 22,80,443 -j ACCEPT',
123
            '-A INPUT -m state --state NEW -m udp -p udp --dport 1194:1201 -j ACCEPT',
124
            sprintf('-A INPUT -j REJECT --reject-with %s', 4 === $inetFamily ? 'icmp-host-prohibited' : 'icmp6-adm-prohibited'),
125
        ];
126
127
        return $inputChain;
128
    }
129
130
    private static function getForwardChain(InstanceConfig $instanceConfig, $inetFamily)
131
    {
132
        $forwardChain = [];
133
134
        foreach (array_keys($instanceConfig->v('vpnPools')) as $poolNumber => $poolId) {
135
            $poolConfig = new PoolConfig($instanceConfig->v('vpnPools', $poolId));
136
            if (6 === $inetFamily && !$poolConfig->v('forward6')) {
137
                // IPv6 forwarding was disabled
138
                continue;
139
            }
140
141
            if (4 === $inetFamily) {
142
                // get the IPv4 range
143
                $srcNet = $poolConfig->v('range');
144
            } else {
145
                // get the IPv6 range
146
                $srcNet = $poolConfig->v('range6');
147
            }
148
            $forwardChain[] = sprintf('-N vpn-%s-%s', $instanceConfig->v('instanceNumber'), $poolNumber);
149
150
            $forwardChain[] = sprintf('-A FORWARD -i tun-%s-%s+ -s %s -j vpn-%s-%s', $instanceConfig->v('instanceNumber'), $poolNumber, $srcNet, $instanceConfig->v('instanceNumber'), $poolNumber);
151
152
            // merge outgoing forwarding firewall rules to prevent certain
153
            // traffic
154
            $forwardChain = array_merge($forwardChain, self::getForwardFirewall($instanceConfig->v('instanceNumber'), $poolNumber, $poolConfig, $inetFamily));
155
156
            if ($poolConfig->v('clientToClient')) {
157
                // allow client-to-client
158
                $forwardChain[] = sprintf('-A vpn-%s-%s -o tun-%s-%s+ -d %s -j ACCEPT', $instanceConfig->v('instanceNumber'), $poolNumber, $instanceConfig->v('instanceNumber'), $poolNumber, $srcNet);
159
            }
160
            if ($poolConfig->v('defaultGateway')) {
161
                // allow traffic to all outgoing destinations
162
                $forwardChain[] = sprintf('-A vpn-%s-%s -o %s -j ACCEPT', $instanceConfig->v('instanceNumber'), $poolNumber, $poolConfig->v('extIf'), $srcNet);
163
            } else {
164
                // only allow certain traffic to the external interface
165
                foreach ($poolConfig->v('routes') as $route) {
166
                    $routeIp = new IP($route);
167
                    if ($inetFamily === $routeIp->getFamily()) {
168
                        $forwardChain[] = sprintf('-A vpn-%s-%s -o %s -d %s -j ACCEPT', $instanceConfig->v('instanceNumber'), $poolNumber, $poolConfig->v('extIf'), $route);
169
                    }
170
                }
171
            }
172
        }
173
174
        return $forwardChain;
175
    }
176
177
    private static function getForwardFirewall($instanceNumber, $poolNumber, PoolConfig $poolConfig, $inetFamily)
178
    {
179
        $forwardFirewall = [];
180
        if ($poolConfig->v('blockSmb')) {
181
            // drop SMB outgoing traffic
182
            // @see https://medium.com/@ValdikSS/deanonymizing-windows-users-and-capturing-microsoft-and-vpn-accounts-f7e53fe73834
183
            foreach (['tcp', 'udp'] as $proto) {
184
                $forwardFirewall[] = sprintf(
185
                    '-A vpn-%s-%s -o %s -m multiport -p %s --dports 137:139,445 -j REJECT --reject-with %s',
186
                    $instanceNumber,
187
                    $poolNumber,
188
                    $poolConfig->v('extIf'),
189
                    $proto,
190
                    4 === $inetFamily ? 'icmp-host-prohibited' : 'icmp6-adm-prohibited');
191
            }
192
        }
193
194
        return $forwardFirewall;
195
    }
196
}
197