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 ( 437568...05b6dd )
by François
02:22
created

CcdHandler::getDisabledCommonNames()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 22
rs 8.6737
cc 5
eloc 12
nc 12
nop 1
1
<?php
2
/**
3
 * Copyright 2015 François Kooman <[email protected]>.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
namespace fkooman\VPN\Server;
18
19
use RuntimeException;
20
21
/**
22
 * Manage the Client Configuration Directory (CCD) used by the OpenVPN 
23
 * instances running on this machine. It can be used to set client specific
24
 * configurations based on the CN.
25
 */
26
class CcdHandler
27
{
28
    /** @var string */
29
    private $ccdPath;
30
31
    public function __construct($ccdPath)
32
    {
33
        $this->ccdPath = $ccdPath;
34
    }
35
36
    private function parseCcd($commonName)
37
    {
38
        $commonNamePath = sprintf('%s/%s', $this->ccdPath, $commonName);
39
        $clientConfig = array();
40
41
        $handle = @fopen($commonNamePath, 'r');
42
        if ($handle) {
43
            while (false !== $line = fgets($handle, 128)) {
44
                $clientConfig[] = trim($line);
45
            }
46
            fclose($handle);
47
        }
48
49
        return $clientConfig;
50
    }
51
52 View Code Duplication
    public function disableCommonName($commonName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        Utils::validateCommonName($commonName);
55
56
        $clientConfig = $this->parseCcd($commonName);
57
        foreach ($clientConfig as $k => $v) {
58
            if ('disable' === $v) {
59
                // already disabled
60
                return false;
61
            }
62
        }
63
64
        // not yet disabled
65
        $clientConfig[] = 'disable';
66
67
        $this->writeFile($commonName, $clientConfig);
68
69
        return true;
70
    }
71
72 View Code Duplication
    public function enableCommonName($commonName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74
        Utils::validateCommonName($commonName);
75
76
        $clientConfig = $this->parseCcd($commonName);
77
        foreach ($clientConfig as $k => $v) {
78
            if ('disable' === $v) {
79
                // it is disabled
80
                unset($clientConfig[$k]);
81
                $this->writeFile($commonName, $clientConfig);
82
83
                return true;
84
            }
85
        }
86
87
        // not disabled
88
        return false;
89
    }
90
91
    public function isDisabled($commonName)
92
    {
93
        Utils::validateCommonName($commonName);
94
95
        $clientConfig = $this->parseCcd($commonName);
96
        foreach ($clientConfig as $k => $v) {
97
            if ('disable' === $v) {
98
                // disabled
99
                return true;
100
            }
101
        }
102
103
        // not disabled
104
        return false;
105
    }
106
107
    public function getDisabledCommonNames($userId = null)
108
    {
109
        if (!is_null($userId)) {
110
            Utils::validateUserId($userId);
111
        }
112
113
        $disabledCommonNames = array();
114
        $pathFilter = sprintf('%s/*', $this->ccdPath);
115
        if (!is_null($userId)) {
116
            $pathFilter = sprintf('%s/%s_*', $this->ccdPath, $userId);
117
        }
118
119
        foreach (glob($pathFilter) as $commonNamePath) {
0 ignored issues
show
Security File Exposure introduced by
$pathFilter can contain request data and is used in file inclusion context(s) leading to a potential security vulnerability.

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
120
            $commonName = basename($commonNamePath);
121
122
            if ($this->isDisabled($commonName)) {
123
                $disabledCommonNames[] = $commonName;
124
            }
125
        }
126
127
        return $disabledCommonNames;
128
    }
129
130
    public function getStaticIpAddress($commonName)
0 ignored issues
show
Unused Code introduced by
The parameter $commonName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
    {
132
    }
133
134
    public function setStaticIpAddress($commonName, $v4, $v6)
0 ignored issues
show
Unused Code introduced by
The parameter $commonName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
135
    {
136
        if (!is_null($v4)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
137
        }
138
139
        if (!is_null($v6)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
140
        }
141
    }
142
143
    private function writeFile($commonName, array $clientConfig)
144
    {
145
        $commonNamePath = sprintf('%s/%s', $this->ccdPath, $commonName);
146
147
        if (false === @file_put_contents($commonNamePath, implode(PHP_EOL, $clientConfig).PHP_EOL)) {
0 ignored issues
show
Security File Manipulation introduced by
$commonNamePath can contain request data and is used in file manipulation context(s) leading to a potential security vulnerability.

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
148
            throw new RuntimeException('unable to write to CCD file');
149
        }
150
    }
151
}
152