-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRadius.php
More file actions
266 lines (220 loc) · 8.08 KB
/
Radius.php
File metadata and controls
266 lines (220 loc) · 8.08 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
declare(strict_types=1);
namespace SimpleSAML\Module\radius\Auth\Source;
use Dapphp\Radius\Radius as RadiusClient;
use Exception;
use SimpleSAML\Configuration;
use SimpleSAML\Error;
use SimpleSAML\Logger;
use SimpleSAML\Module\core\Auth\UserPassBase;
use SimpleSAML\Utils;
use function array_merge;
use function sprintf;
use function strtok;
use function var_export;
/**
* RADIUS authentication source.
*
* This class is based on www/auth/login-radius.php.
*
* @package SimpleSAMLphp
*/
class Radius extends UserPassBase
{
public const int RADIUS_USERNAME = 1;
public const int RADIUS_VENDOR_SPECIFIC = 26;
public const int RADIUS_NAS_IDENTIFIER = 32;
/**
* @var array<mixed> The list of radius servers to use.
*/
private array $servers;
/**
* @var string The hostname of the radius server.
*/
private string $hostname;
/**
* @var int The port of the radius server.
*/
private int $port;
/**
* @var string The secret used when communicating with the radius server.
*/
private string $secret;
/**
* @var int The timeout for contacting the radius server.
*/
private int $timeout;
/**
* @var string|null The realm to be added to the entered username.
*/
private ?string $realm;
/**
* @var string|null The attribute name where the username should be stored.
*/
private ?string $usernameAttribute = null;
/**
* @var int|null The vendor for the RADIUS attributes we are interrested in.
*/
private ?int $vendor = null;
/**
* @var int The vendor-specific attribute for the RADIUS attributes we are
* interrested in.
*/
private int $vendorType;
/**
* @var string|null The NAS-Identifier that should be set in Access-Request packets.
*/
private ?string $nasIdentifier = null;
/**
* @var bool Debug modus
*/
private bool $debug;
/**
* Constructor for this authentication source.
*
* @param array<mixed> $info Information about this authentication source.
* @param array<mixed> $config Configuration.
*/
public function __construct(array $info, array $config)
{
// Call the parent constructor first, as required by the interface
parent::__construct($info, $config);
// Parse configuration.
$cfg = Configuration::loadFromArray(
$config,
'Authentication source ' . var_export($this->authId, true),
);
$this->servers = $cfg->getArray('servers');
// For backwards compatibility
if (empty($this->servers)) {
$this->hostname = $cfg->getString('hostname');
$this->port = $cfg->getOptionalIntegerRange('port', 1, 65535, 1812);
$this->secret = $cfg->getString('secret');
$this->servers[] = [
'hostname' => $this->hostname,
'port' => $this->port,
'secret' => $this->secret,
];
}
$this->debug = $cfg->getOptionalBoolean('debug', false);
$this->timeout = $cfg->getOptionalInteger('timeout', 5);
$this->realm = $cfg->getOptionalString('realm', null);
$this->usernameAttribute = $cfg->getOptionalString('username_attribute', null);
$this->nasIdentifier = $cfg->getOptionalString('nas_identifier', null);
$this->vendor = $cfg->getOptionalInteger('attribute_vendor', null);
if ($this->vendor !== null) {
$this->vendorType = $cfg->getInteger('attribute_vendor_type');
}
}
/**
* Attempt to log in using the given username and password.
*
* @param string $username The username the user wrote.
* @param string $password The password the user wrote.
* @return array<mixed> Associative array with the user's attributes.
*/
protected function login(
string $username,
#[\SensitiveParameter]
string $password,
): array {
$radius = new RadiusClient();
$response = false;
// Try to add all radius servers, trigger a failure if no one works
foreach ($this->servers as $server) {
$radius->setServer($server['hostname']);
$radius->setAuthenticationPort($server['port']);
$radius->setSecret($server['secret']);
$radius->setDebug($this->debug);
$radius->setTimeout($this->timeout);
$radius->setIncludeMessageAuthenticator();
$httpUtils = new Utils\HTTP();
$radius->setAttribute((string)self::RADIUS_NAS_IDENTIFIER, $this->nasIdentifier ?: $httpUtils->getSelfHost());
if ($this->realm !== null) {
$radius->setRadiusSuffix('@' . $this->realm);
}
$response = $radius->accessRequest($username, $password);
if ($response !== false) {
break;
}
}
if ($response === false) {
$errorCode = $radius->getErrorCode();
switch ($errorCode) {
case $radius::TYPE_ACCESS_REJECT:
Logger::warning('ldapRadius: Radius authentication failed.');
throw new Error\Error('WRONGUSERPASS');
case $radius::TYPE_ACCESS_CHALLENGE:
throw new Exception('Radius authentication error: Challenge requested, but not supported.');
default:
throw new Exception(sprintf(
'Error during radius authentication; %s (%d)',
$radius->getErrorMessage(),
$errorCode,
));
}
}
Logger::info('ldapRadius: Radius authentication succeeded.');
// If we get this far, we have a valid login
$attributes = [];
if ($this->usernameAttribute !== null) {
$attributes[$this->usernameAttribute] = [$username];
}
if ($this->vendor === null) {
/*
* We aren't interested in any vendor-specific attributes. We are
* therefore done now.
*/
return $attributes;
} else {
foreach ($radius->getReceivedAttributes() as $content) {
if ($content[0] == 26) { // is a Vendor-Specific attribute
$vsa = $radius->decodeVendorSpecificContent($content[1]);
// matches configured Vendor and Type
if ($vsa[0][0] === $this->vendor && $vsa[0][1] === $this->vendorType) {
// SAML attributes expected in a URN=value, so split at first =
$decomposed = explode("=", $vsa[0][2], 2);
$attributes[$decomposed[0]][] = $decomposed[1];
}
}
}
}
return array_merge($attributes, $this->getAttributes($radius));
}
/**
* @param \Dapphp\Radius\Radius $radius
* @return array<mixed>
*/
private function getAttributes(RadiusClient $radius): array
{
// get AAI attribute sets.
$resa = $radius->getReceivedAttributes();
$attributes = [];
// Use the received user name
if ($resa['attr'] === self::RADIUS_USERNAME && $this->usernameAttribute !== null) {
$attributes[$this->usernameAttribute] = [$resa['data']];
return $attributes;
}
if ($resa['attr'] !== self::RADIUS_VENDOR_SPECIFIC) {
return $attributes;
}
$resv = $resa['data'];
if ($resv === false) {
throw new Exception(sprintf(
'Error getting vendor specific attribute: %s (%d)',
$radius->getErrorMessage(),
$radius->getErrorCode(),
));
}
$vendor = $resv['vendor'];
$attrv = $resv['attr'];
$datav = $resv['data'];
if ($vendor !== $this->vendor || $attrv !== $this->vendorType) {
return $attributes;
}
$attrib_name = strtok($datav, '=');
$attrib_value = strtok('=');
$attributes[$attrib_name] = [$attrib_value];
return $attributes;
}
}