~fkooman/vpn-user-portal

ref: cc452cbd571f1e45ac50ffc38662dbd3fc6c3e8a vpn-user-portal/src/Http/NodeApiModule.php -rw-r--r-- 6.9 KiB
cc452cbdFrançois Kooman update dependencies 2 months ago
                                                                                
1
2
3
4
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php

declare(strict_types=1);

/*
 * eduVPN - End-user friendly VPN.
 *
 * Copyright: 2016-2021, The Commons Conservancy eduVPN Programme
 * SPDX-License-Identifier: AGPL-3.0+
 */

namespace LC\Portal\Http;

use DateTimeImmutable;
use LC\Portal\Config;
use LC\Portal\Dt;
use LC\Portal\Exception\NodeApiException;
use LC\Portal\LoggerInterface;
use LC\Portal\ServerConfig;
use LC\Portal\Storage;

class NodeApiModule implements ServiceModuleInterface
{
    private Config $config;

    private Storage $storage;

    private ServerConfig $serverConfig;

    private LoggerInterface $logger;

    private DateTimeImmutable $dateTime;

    public function __construct(Config $config, Storage $storage, ServerConfig $serverConfig, LoggerInterface $logger)
    {
        $this->config = $config;
        $this->storage = $storage;
        $this->serverConfig = $serverConfig;
        $this->logger = $logger;
        $this->dateTime = Dt::get();
    }

    public function init(Service $service): void
    {
        $service->post(
            '/server_config',
            function (UserInfo $userInfo, Request $request): Response {
                // XXX we may want to restrict the profiles for particular nodes!
                $serverConfigList = $this->serverConfig->get($this->config->profileConfigList());
                $bodyLines = [];
                foreach ($serverConfigList as $configName => $configFile) {
                    $bodyLines[] = $configName.':'.sodium_bin2base64($configFile, \SODIUM_BASE64_VARIANT_ORIGINAL);
                }

                /// XXX content type?
                return new Response(implode("\r\n", $bodyLines));
            }
        );

        $service->post(
            '/connect',
            function (UserInfo $userInfo, Request $request): Response {
                try {
                    $this->connect($request);

                    return new Response('OK');
                } catch (NodeApiException $e) {
                    if (null !== $userId = $e->getUserId()) {
                        $this->storage->addUserLog($userId, LoggerInterface::ERROR, 'unable to connect: '.$e->getMessage(), $this->dateTime);
                    }

                    return new Response('ERR');
                }
            }
        );

        $service->post(
            '/disconnect',
            function (UserInfo $userInfo, Request $request): Response {
                try {
                    $this->disconnect($request);

                    return new Response('OK');
                } catch (NodeApiException $e) {
                    if (null !== $userId = $e->getUserId()) {
                        $this->storage->addUserLog($userId, LoggerInterface::ERROR, 'unable to disconnect: '.$e->getMessage(), $this->dateTime);
                    }

                    return new Response('ERR');
                }
            }
        );
    }

    public function connect(Request $request): void
    {
        $profileId = InputValidation::profileId($request->requirePostParameter('profile_id'));
        $commonName = InputValidation::commonName($request->requirePostParameter('common_name'));
        $originatingIp = InputValidation::ipAddress($request->requirePostParameter('originating_ip'));
        $ipFour = InputValidation::ipFour($request->requirePostParameter('ip_four'));
        $ipSix = InputValidation::ipSix($request->requirePostParameter('ip_six'));
        $connectedAt = InputValidation::connectedAt($request->requirePostParameter('connected_at'));
        $userId = $this->verifyConnection($profileId, $commonName);
        $this->storage->clientConnect($userId, $profileId, $ipFour, $ipSix, Dt::get(sprintf('@%d', $connectedAt)));
        $this->logger->info(
            sprintf('CONNECT %s (%s) [%s => %s,%s]', $userId, $profileId, $originatingIp, $ipFour, $ipSix)
        );
    }

    public function disconnect(Request $request): void
    {
        $profileId = InputValidation::profileId($request->requirePostParameter('profile_id'));
        $commonName = InputValidation::commonName($request->requirePostParameter('common_name'));
        $originatingIp = InputValidation::ipAddress($request->requirePostParameter('originating_ip'));
        $ipFour = InputValidation::ipFour($request->requirePostParameter('ip_four'));
        $ipSix = InputValidation::ipSix($request->requirePostParameter('ip_six'));
        $disconnectedAt = InputValidation::disconnectedAt($request->requirePostParameter('disconnected_at'));
//        $bytesTransferred = InputValidation::bytesTransferred($request->requirePostParameter('bytes_transferred'));
        // XXX add bytesTransferred to some global table

        if (null === $certInfo = $this->storage->getUserCertificateInfo($commonName)) {
            // CN does not exist (anymore)
            // XXX do we need to do something special?
            return;
        }
        $userId = $certInfo['user_id'];
        $this->storage->clientDisconnect($userId, $profileId, $ipFour, $ipSix, Dt::get(sprintf('@%d', $disconnectedAt)));
        $this->logger->info(
            sprintf('DISCONNECT %s (%s) [%s => %s,%s]', $userId, $profileId, $originatingIp, $ipFour, $ipSix)
        );
    }

    private function verifyConnection(string $profileId, string $commonName): string
    {
        // verify status of certificate/user
        if (null === $userCertInfo = $this->storage->getUserCertificateInfo($commonName)) {
            // we do not (yet) know the user as only an existing *//* certificate can be linked back to a user...
            throw new NodeApiException(null, sprintf('user or certificate does not exist [profile_id: %s, common_name: %s]', $profileId, $commonName));
        }

        $userId = $userCertInfo['user_id'];
        if ($userCertInfo['user_is_disabled']) {
            throw new NodeApiException($userId, 'unable to connect, account is disabled');
        }

        $this->verifyAcl($profileId, $userId);

        return $userId;
    }

    private function verifyAcl(string $profileId, string $userId): void
    {
        $profileConfig = $this->config->profileConfig($profileId);
        if ($profileConfig->enableAcl()) {
            // ACL is enabled for this profile
            $userPermissionList = $this->storage->getPermissionList($userId);
            $profilePermissionList = $profileConfig->aclPermissionList();
            if (false === self::hasPermission($userPermissionList, $profilePermissionList)) {
                throw new NodeApiException($userId, sprintf('unable to connect, user permissions are [%s], but requires any of [%s]', implode(',', $userPermissionList), implode(',', $profilePermissionList)));
            }
        }
    }

    private static function hasPermission(array $userPermissionList, array $aclPermissionList): bool
    {
        // one of the permissions must be listed in the profile ACL list
        foreach ($userPermissionList as $userPermission) {
            if (\in_array($userPermission, $aclPermissionList, true)) {
                return true;
            }
        }

        return false;
    }
}