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 ( 70079c...969347 )
by François
02:54
created

Firewall::getInputChain()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 1
nop 2
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
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\Common\Config;
21
use SURFnet\VPN\Server\PoolConfig;
22
use SURFnet\VPN\Server\FirewallConfig;
23
use SURFnet\VPN\Server\IP;
24
25
class Firewall
26
{
27
    public static function getFirewall4(array $configList, FirewallConfig $firewallConfig, $asArray = false)
28
    {
29
        return self::getFirewall($configList, $firewallConfig, 4, $asArray);
30
    }
31
32
    public static function getFirewall6(array $configList, FirewallConfig $firewallConfig, $asArray = false)
33
    {
34
        return self::getFirewall($configList, $firewallConfig, 6, $asArray);
35
    }
36
37
    private static function getFirewall(array $configList, FirewallConfig $firewallConfig, $inetFamily, $asArray)
38
    {
39
        $firewall = [];
40
41
        // NAT
42
        $firewall = array_merge(
43
            $firewall,
44
             [
45
            '*nat',
46
            ':PREROUTING ACCEPT [0:0]',
47
            ':INPUT ACCEPT [0:0]',
48
            ':OUTPUT ACCEPT [0:0]',
49
            ':POSTROUTING ACCEPT [0:0]',
50
            ]
51
        );
52
        // add all instances
53
        foreach ($configList as $config) {
54
            $firewall = array_merge($firewall, self::getNat($config, $inetFamily));
55
        }
56
        $firewall[] = 'COMMIT';
57
58
        // FILTER
59
        $firewall = array_merge(
60
            $firewall,
61
            [
62
                '*filter',
63
                ':INPUT ACCEPT [0:0]',
64
                ':FORWARD ACCEPT [0:0]',
65
                ':OUTPUT ACCEPT [0:0]',
66
            ]
67
        );
68
69
        // INPUT
70
        $firewall = array_merge($firewall, self::getInputChain($inetFamily, $firewallConfig));
71
72
        // FORWARD
73
        $firewall = array_merge(
74
            $firewall,
75
            [
76
                //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...
77
                '-A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT',
78
            ]
79
        );
80
81
        // add all instances
82
        foreach ($configList as $config) {
83
            $firewall = array_merge($firewall, self::getForwardChain($config, $inetFamily));
84
        }
85
        $firewall[] = sprintf('-A FORWARD -j REJECT --reject-with %s', 4 === $inetFamily ? 'icmp-host-prohibited' : 'icmp6-adm-prohibited');
86
        $firewall[] = 'COMMIT';
87
88
        if ($asArray) {
89
            return $firewall;
90
        }
91
92
        return implode(PHP_EOL, $firewall).PHP_EOL;
93
    }
94
95
    private static function getNat(Config $config, $inetFamily)
96
    {
97
        $nat = [];
98
99
        foreach (array_keys($config->v('vpnPools')) as $poolId) {
100
            $poolConfig = new PoolConfig($config->v('vpnPools', $poolId));
101
            if ($poolConfig->v('useNat')) {
102
                if (4 === $inetFamily) {
103
                    // get the IPv4 range
104
                    $srcNet = $poolConfig->v('range');
105
                } else {
106
                    // get the IPv6 range
107
                    $srcNet = $poolConfig->v('range6');
108
                }
109
                // -i (--in-interface) cannot be specified for POSTROUTING
110
                $nat[] = sprintf('-A POSTROUTING -s %s -o %s -j MASQUERADE', $srcNet, $poolConfig->v('extIf'));
111
            }
112
        }
113
114
        return $nat;
115
    }
116
117
    private static function getInputChain($inetFamily, FirewallConfig $firewallConfig)
118
    {
119
        $inputChain = [
120
            '-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT',
121
            sprintf('-A INPUT -p %s -j ACCEPT', 4 === $inetFamily ? 'icmp' : 'ipv6-icmp'),
122
            '-A INPUT -i lo -j ACCEPT',
123
        ];
124
125
        // NOTE: multiport is limited to 15 ports (a range counts as two)
126
        $inputChain[] = sprintf(
127
            '-A INPUT -m state --state NEW -m multiport -p udp --dports %s -j ACCEPT',
128
            implode(',', $firewallConfig->v('inputChain', 'udp'))
129
        );
130
131
        $inputChain[] = sprintf(
132
            '-A INPUT -m state --state NEW -m multiport -p tcp --dports %s -j ACCEPT',
133
            implode(',', $firewallConfig->v('inputChain', 'tcp'))
134
        );
135
136
        $inputChain[] = sprintf('-A INPUT -j REJECT --reject-with %s', 4 === $inetFamily ? 'icmp-host-prohibited' : 'icmp6-adm-prohibited');
137
138
        return $inputChain;
139
    }
140
141
    private static function getForwardChain(Config $config, $inetFamily)
142
    {
143
        $forwardChain = [];
144
145
        foreach (array_keys($config->v('vpnPools')) as $poolId) {
146
            $poolConfig = new PoolConfig($config->v('vpnPools', $poolId));
147
            $poolNumber = $poolConfig->v('poolNumber');
148
            if (6 === $inetFamily && !$poolConfig->v('forward6')) {
149
                // IPv6 forwarding was disabled
150
                continue;
151
            }
152
153
            if (4 === $inetFamily) {
154
                // get the IPv4 range
155
                $srcNet = $poolConfig->v('range');
156
            } else {
157
                // get the IPv6 range
158
                $srcNet = $poolConfig->v('range6');
159
            }
160
            $forwardChain[] = sprintf('-N vpn-%s-%s', $config->v('instanceNumber'), $poolNumber);
161
162
            $forwardChain[] = sprintf('-A FORWARD -i tun-%s-%s+ -s %s -j vpn-%s-%s', $config->v('instanceNumber'), $poolNumber, $srcNet, $config->v('instanceNumber'), $poolNumber);
163
164
            // merge outgoing forwarding firewall rules to prevent certain
165
            // traffic
166
            $forwardChain = array_merge($forwardChain, self::getForwardFirewall($config->v('instanceNumber'), $poolNumber, $poolConfig, $inetFamily));
167
168
            if ($poolConfig->v('clientToClient')) {
169
                // allow client-to-client
170
                $forwardChain[] = sprintf('-A vpn-%s-%s -o tun-%s-%s+ -d %s -j ACCEPT', $config->v('instanceNumber'), $poolNumber, $config->v('instanceNumber'), $poolNumber, $srcNet);
171
            }
172
            if ($poolConfig->v('defaultGateway')) {
173
                // allow traffic to all outgoing destinations
174
                $forwardChain[] = sprintf('-A vpn-%s-%s -o %s -j ACCEPT', $config->v('instanceNumber'), $poolNumber, $poolConfig->v('extIf'), $srcNet);
175
            } else {
176
                // only allow certain traffic to the external interface
177
                foreach ($poolConfig->v('routes') as $route) {
178
                    $routeIp = new IP($route);
179
                    if ($inetFamily === $routeIp->getFamily()) {
180
                        $forwardChain[] = sprintf('-A vpn-%s-%s -o %s -d %s -j ACCEPT', $config->v('instanceNumber'), $poolNumber, $poolConfig->v('extIf'), $route);
181
                    }
182
                }
183
            }
184
        }
185
186
        return $forwardChain;
187
    }
188
189
    private static function getForwardFirewall($instanceNumber, $poolNumber, PoolConfig $poolConfig, $inetFamily)
190
    {
191
        $forwardFirewall = [];
192
        if ($poolConfig->v('blockSmb')) {
193
            // drop SMB outgoing traffic
194
            // @see https://medium.com/@ValdikSS/deanonymizing-windows-users-and-capturing-microsoft-and-vpn-accounts-f7e53fe73834
195
            foreach (['tcp', 'udp'] as $proto) {
196
                $forwardFirewall[] = sprintf(
197
                    '-A vpn-%s-%s -o %s -m multiport -p %s --dports 137:139,445 -j REJECT --reject-with %s',
198
                    $instanceNumber,
199
                    $poolNumber,
200
                    $poolConfig->v('extIf'),
201
                    $proto,
202
                    4 === $inetFamily ? 'icmp-host-prohibited' : 'icmp6-adm-prohibited');
203
            }
204
        }
205
206
        return $forwardFirewall;
207
    }
208
}
209