1
|
<?php
|
2
|
/*
|
3
|
* auth.inc
|
4
|
*
|
5
|
* part of pfSense (https://www.pfsense.org)
|
6
|
* Copyright (c) 2003-2006 Manuel Kasper <mk@neon1.net>
|
7
|
* Copyright (c) 2005-2006 Bill Marquette <bill.marquette@gmail.com>
|
8
|
* Copyright (c) 2006 Paul Taylor <paultaylor@winn-dixie.com>
|
9
|
* Copyright (c) 2004-2013 BSD Perimeter
|
10
|
* Copyright (c) 2013-2016 Electric Sheep Fencing
|
11
|
* Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
|
12
|
* All rights reserved.
|
13
|
*
|
14
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
15
|
* you may not use this file except in compliance with the License.
|
16
|
* You may obtain a copy of the License at
|
17
|
*
|
18
|
* http://www.apache.org/licenses/LICENSE-2.0
|
19
|
*
|
20
|
* Unless required by applicable law or agreed to in writing, software
|
21
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
22
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
23
|
* See the License for the specific language governing permissions and
|
24
|
* limitations under the License.
|
25
|
*/
|
26
|
|
27
|
/*
|
28
|
* NOTE : Portions of the mschapv2 support was based on the BSD licensed CHAP.php
|
29
|
* file courtesy of Michael Retterklieber.
|
30
|
*/
|
31
|
include_once('phpsessionmanager.inc');
|
32
|
if (!$do_not_include_config_gui_inc) {
|
33
|
require_once("config.gui.inc");
|
34
|
}
|
35
|
|
36
|
// Will be changed to false if security checks fail
|
37
|
$security_passed = true;
|
38
|
|
39
|
/* If this function doesn't exist, we're being called from Captive Portal or
|
40
|
another internal subsystem which does not include authgui.inc */
|
41
|
if (function_exists("display_error_form")) {
|
42
|
/* Extra layer of lockout protection. Check if the user is in the GUI
|
43
|
* lockout table before processing a request */
|
44
|
|
45
|
/* Fetch the contents of the lockout table. */
|
46
|
$entries = array();
|
47
|
exec("/sbin/pfctl -t 'sshguard' -T show", $entries);
|
48
|
|
49
|
/* If the client is in the lockout table, print an error, kill states, and exit */
|
50
|
if (in_array($_SERVER['REMOTE_ADDR'], array_map('trim', $entries))) {
|
51
|
if (!security_checks_disabled()) {
|
52
|
/* They may never see the error since the connection will be cut off, but try to be nice anyhow. */
|
53
|
display_error_form("501", gettext("Access Denied<br/><br/>Access attempt from a temporarily locked out client address.<br /><br />Try accessing the firewall again after the lockout expires."));
|
54
|
/* If they are locked out, they shouldn't have a state. Disconnect their connections. */
|
55
|
$retval = pfSense_kill_states($_SERVER['REMOTE_ADDR']);
|
56
|
if (is_ipaddrv4($_SERVER['REMOTE_ADDR'])) {
|
57
|
$retval = pfSense_kill_states("0.0.0.0/0", $_SERVER['REMOTE_ADDR']);
|
58
|
} elseif (is_ipaddrv6($_SERVER['REMOTE_ADDR'])) {
|
59
|
$retval = pfSense_kill_states("::", $_SERVER['REMOTE_ADDR']);
|
60
|
}
|
61
|
exit;
|
62
|
}
|
63
|
$security_passed = false;
|
64
|
}
|
65
|
}
|
66
|
|
67
|
if (function_exists("display_error_form") && !isset($config['system']['webgui']['nodnsrebindcheck'])) {
|
68
|
/* DNS ReBinding attack prevention. https://redmine.pfsense.org/issues/708 */
|
69
|
$found_host = false;
|
70
|
|
71
|
/* Either a IPv6 address with or without a alternate port */
|
72
|
if (strstr($_SERVER['HTTP_HOST'], "]")) {
|
73
|
$http_host_port = explode("]", $_SERVER['HTTP_HOST']);
|
74
|
/* v6 address has more parts, drop the last part */
|
75
|
if (count($http_host_port) > 1) {
|
76
|
array_pop($http_host_port);
|
77
|
$http_host = str_replace(array("[", "]"), "", implode(":", $http_host_port));
|
78
|
} else {
|
79
|
$http_host = str_replace(array("[", "]"), "", implode(":", $http_host_port));
|
80
|
}
|
81
|
} else {
|
82
|
$http_host = explode(":", $_SERVER['HTTP_HOST']);
|
83
|
$http_host = $http_host[0];
|
84
|
}
|
85
|
if (is_ipaddr($http_host) or $_SERVER['SERVER_ADDR'] == "127.0.0.1" or
|
86
|
strcasecmp($http_host, "localhost") == 0 or $_SERVER['SERVER_ADDR'] == "::1") {
|
87
|
$found_host = true;
|
88
|
}
|
89
|
if (strcasecmp($http_host, $config['system']['hostname'] . "." . $config['system']['domain']) == 0 or
|
90
|
strcasecmp($http_host, $config['system']['hostname']) == 0) {
|
91
|
$found_host = true;
|
92
|
}
|
93
|
|
94
|
if (is_array($config['dyndnses']['dyndns']) && !$found_host) {
|
95
|
foreach ($config['dyndnses']['dyndns'] as $dyndns) {
|
96
|
if (strcasecmp($dyndns['host'], $http_host) == 0) {
|
97
|
$found_host = true;
|
98
|
break;
|
99
|
}
|
100
|
}
|
101
|
}
|
102
|
|
103
|
if (is_array($config['dnsupdates']['dnsupdate']) && !$found_host) {
|
104
|
foreach ($config['dnsupdates']['dnsupdate'] as $rfc2136) {
|
105
|
if (strcasecmp($rfc2136['host'], $http_host) == 0) {
|
106
|
$found_host = true;
|
107
|
break;
|
108
|
}
|
109
|
}
|
110
|
}
|
111
|
|
112
|
if (!empty($config['system']['webgui']['althostnames']) && !$found_host) {
|
113
|
$althosts = explode(" ", $config['system']['webgui']['althostnames']);
|
114
|
foreach ($althosts as $ah) {
|
115
|
if (strcasecmp($ah, $http_host) == 0 or strcasecmp($ah, $_SERVER['SERVER_ADDR']) == 0) {
|
116
|
$found_host = true;
|
117
|
break;
|
118
|
}
|
119
|
}
|
120
|
}
|
121
|
|
122
|
if ($found_host == false) {
|
123
|
if (!security_checks_disabled()) {
|
124
|
display_error_form("501", gettext("Potential DNS Rebind attack detected, see http://en.wikipedia.org/wiki/DNS_rebinding<br />Try accessing the router by IP address instead of by hostname."));
|
125
|
exit;
|
126
|
}
|
127
|
$security_passed = false;
|
128
|
}
|
129
|
}
|
130
|
|
131
|
// If the HTTP_REFERER is something other than ourselves then disallow.
|
132
|
if (function_exists("display_error_form") && !isset($config['system']['webgui']['nohttpreferercheck'])) {
|
133
|
if ($_SERVER['HTTP_REFERER']) {
|
134
|
if (file_exists("{$g['tmp_path']}/setupwizard_lastreferrer")) {
|
135
|
if ($_SERVER['HTTP_REFERER'] == file_get_contents("{$g['tmp_path']}/setupwizard_lastreferrer")) {
|
136
|
unlink("{$g['tmp_path']}/setupwizard_lastreferrer");
|
137
|
header("Refresh: 1; url=index.php");
|
138
|
?>
|
139
|
<!DOCTYPE html>
|
140
|
<html lang="en">
|
141
|
<head>
|
142
|
<link rel="stylesheet" href="/css/pfSense.css" />
|
143
|
<title><?=gettext("Redirecting..."); ?></title>
|
144
|
</head>
|
145
|
<body id="error" class="no-menu">
|
146
|
<div id="jumbotron">
|
147
|
<div class="container">
|
148
|
<div class="col-sm-offset-3 col-sm-6 col-xs-12">
|
149
|
<p><?=gettext("Redirecting to the dashboard...")?></p>
|
150
|
</div>
|
151
|
</div>
|
152
|
</div>
|
153
|
</body>
|
154
|
</html>
|
155
|
<?php
|
156
|
exit;
|
157
|
}
|
158
|
}
|
159
|
$found_host = false;
|
160
|
$referrer_host = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
|
161
|
$referrer_host = str_replace(array("[", "]"), "", $referrer_host);
|
162
|
if ($referrer_host) {
|
163
|
if (strcasecmp($referrer_host, $config['system']['hostname'] . "." . $config['system']['domain']) == 0 ||
|
164
|
strcasecmp($referrer_host, $config['system']['hostname']) == 0) {
|
165
|
$found_host = true;
|
166
|
}
|
167
|
|
168
|
if (!empty($config['system']['webgui']['althostnames']) && !$found_host) {
|
169
|
$althosts = explode(" ", $config['system']['webgui']['althostnames']);
|
170
|
foreach ($althosts as $ah) {
|
171
|
if (strcasecmp($referrer_host, $ah) == 0) {
|
172
|
$found_host = true;
|
173
|
break;
|
174
|
}
|
175
|
}
|
176
|
}
|
177
|
|
178
|
if (is_array($config['dyndnses']['dyndns']) && !$found_host) {
|
179
|
foreach ($config['dyndnses']['dyndns'] as $dyndns) {
|
180
|
if (strcasecmp($dyndns['host'], $referrer_host) == 0) {
|
181
|
$found_host = true;
|
182
|
break;
|
183
|
}
|
184
|
}
|
185
|
}
|
186
|
|
187
|
if (is_array($config['dnsupdates']['dnsupdate']) && !$found_host) {
|
188
|
foreach ($config['dnsupdates']['dnsupdate'] as $rfc2136) {
|
189
|
if (strcasecmp($rfc2136['host'], $referrer_host) == 0) {
|
190
|
$found_host = true;
|
191
|
break;
|
192
|
}
|
193
|
}
|
194
|
}
|
195
|
|
196
|
if (!$found_host) {
|
197
|
$interface_list_ips = get_configured_ip_addresses();
|
198
|
foreach ($interface_list_ips as $ilips) {
|
199
|
if (strcasecmp($referrer_host, $ilips) == 0) {
|
200
|
$found_host = true;
|
201
|
break;
|
202
|
}
|
203
|
}
|
204
|
$interface_list_ipv6s = get_configured_ipv6_addresses(true);
|
205
|
foreach ($interface_list_ipv6s as $ilipv6s) {
|
206
|
$ilipv6s = explode('%', $ilipv6s)[0];
|
207
|
if (strcasecmp($referrer_host, $ilipv6s) == 0) {
|
208
|
$found_host = true;
|
209
|
break;
|
210
|
}
|
211
|
}
|
212
|
if ($referrer_host == "127.0.0.1" || $referrer_host == "localhost") {
|
213
|
// allow SSH port forwarded connections and links from localhost
|
214
|
$found_host = true;
|
215
|
}
|
216
|
}
|
217
|
|
218
|
/* Fall back to probing active interface addresses rather than config.xml to allow
|
219
|
* changed addresses that have not yet been applied.
|
220
|
* See https://redmine.pfsense.org/issues/8822
|
221
|
*/
|
222
|
if (!$found_host) {
|
223
|
$refifs = get_interface_arr();
|
224
|
foreach ($refifs as $rif) {
|
225
|
if (($referrer_host == find_interface_ip($rif)) ||
|
226
|
($referrer_host == find_interface_ipv6($rif)) ||
|
227
|
($referrer_host == find_interface_ipv6_ll($rif))) {
|
228
|
$found_host = true;
|
229
|
break;
|
230
|
}
|
231
|
}
|
232
|
}
|
233
|
}
|
234
|
if ($found_host == false) {
|
235
|
if (!security_checks_disabled()) {
|
236
|
display_error_form("501", "An HTTP_REFERER was detected other than what is defined in System -> Advanced (" . htmlspecialchars($_SERVER['HTTP_REFERER']) . "). If not needed, this check can be disabled in System -> Advanced -> Admin.");
|
237
|
exit;
|
238
|
}
|
239
|
$security_passed = false;
|
240
|
}
|
241
|
} else {
|
242
|
$security_passed = false;
|
243
|
}
|
244
|
}
|
245
|
|
246
|
if (function_exists("display_error_form") && $security_passed) {
|
247
|
/* Security checks passed, so it should be OK to turn them back on */
|
248
|
restore_security_checks();
|
249
|
}
|
250
|
unset($security_passed);
|
251
|
|
252
|
$groupindex = index_groups();
|
253
|
$userindex = index_users();
|
254
|
|
255
|
function index_groups() {
|
256
|
global $g, $debug, $config, $groupindex;
|
257
|
|
258
|
$groupindex = array();
|
259
|
|
260
|
if (is_array($config['system']['group'])) {
|
261
|
$i = 0;
|
262
|
foreach ($config['system']['group'] as $groupent) {
|
263
|
$groupindex[$groupent['name']] = $i;
|
264
|
$i++;
|
265
|
}
|
266
|
}
|
267
|
|
268
|
return ($groupindex);
|
269
|
}
|
270
|
|
271
|
function index_users() {
|
272
|
global $g, $debug, $config;
|
273
|
|
274
|
if (is_array($config['system']['user'])) {
|
275
|
$i = 0;
|
276
|
foreach ($config['system']['user'] as $userent) {
|
277
|
$userindex[$userent['name']] = $i;
|
278
|
$i++;
|
279
|
}
|
280
|
}
|
281
|
|
282
|
return ($userindex);
|
283
|
}
|
284
|
|
285
|
function & getUserEntry($name) {
|
286
|
global $debug, $config, $userindex;
|
287
|
$authcfg = auth_get_authserver($config['system']['webgui']['authmode']);
|
288
|
|
289
|
if (isset($userindex[$name])) {
|
290
|
return $config['system']['user'][$userindex[$name]];
|
291
|
} elseif ($authcfg['type'] != "Local Database") {
|
292
|
$user = array();
|
293
|
$user['name'] = $name;
|
294
|
return $user;
|
295
|
}
|
296
|
}
|
297
|
|
298
|
function & getUserEntryByUID($uid) {
|
299
|
global $debug, $config;
|
300
|
|
301
|
if (is_array($config['system']['user'])) {
|
302
|
foreach ($config['system']['user'] as & $user) {
|
303
|
if ($user['uid'] == $uid) {
|
304
|
return $user;
|
305
|
}
|
306
|
}
|
307
|
}
|
308
|
|
309
|
return false;
|
310
|
}
|
311
|
|
312
|
function & getGroupEntry($name) {
|
313
|
global $debug, $config, $groupindex;
|
314
|
if (isset($groupindex[$name])) {
|
315
|
return $config['system']['group'][$groupindex[$name]];
|
316
|
}
|
317
|
}
|
318
|
|
319
|
function & getGroupEntryByGID($gid) {
|
320
|
global $debug, $config;
|
321
|
|
322
|
if (is_array($config['system']['group'])) {
|
323
|
foreach ($config['system']['group'] as & $group) {
|
324
|
if ($group['gid'] == $gid) {
|
325
|
return $group;
|
326
|
}
|
327
|
}
|
328
|
}
|
329
|
|
330
|
return false;
|
331
|
}
|
332
|
|
333
|
function get_user_privileges(& $user) {
|
334
|
global $config, $_SESSION;
|
335
|
|
336
|
$authcfg = auth_get_authserver($config['system']['webgui']['authmode']);
|
337
|
$allowed_groups = array();
|
338
|
|
339
|
$privs = $user['priv'];
|
340
|
if (!is_array($privs)) {
|
341
|
$privs = array();
|
342
|
}
|
343
|
|
344
|
// cache auth results for a short time to ease load on auth services & logs
|
345
|
if (isset($config['system']['webgui']['auth_refresh_time'])) {
|
346
|
$recheck_time = $config['system']['webgui']['auth_refresh_time'];
|
347
|
} else {
|
348
|
$recheck_time = 30;
|
349
|
}
|
350
|
|
351
|
if ($authcfg['type'] == "ldap") {
|
352
|
if (isset($_SESSION["ldap_allowed_groups"]) &&
|
353
|
(time() <= $_SESSION["auth_check_time"] + $recheck_time)) {
|
354
|
$allowed_groups = $_SESSION["ldap_allowed_groups"];
|
355
|
} else {
|
356
|
$allowed_groups = @ldap_get_groups($user['name'], $authcfg);
|
357
|
$_SESSION["ldap_allowed_groups"] = $allowed_groups;
|
358
|
$_SESSION["auth_check_time"] = time();
|
359
|
}
|
360
|
} elseif ($authcfg['type'] == "radius") {
|
361
|
if (isset($_SESSION["radius_allowed_groups"]) &&
|
362
|
(time() <= $_SESSION["auth_check_time"] + $recheck_time)) {
|
363
|
$allowed_groups = $_SESSION["radius_allowed_groups"];
|
364
|
} else {
|
365
|
$allowed_groups = @radius_get_groups($_SESSION['user_radius_attributes']);
|
366
|
$_SESSION["radius_allowed_groups"] = $allowed_groups;
|
367
|
$_SESSION["auth_check_time"] = time();
|
368
|
}
|
369
|
}
|
370
|
|
371
|
if (empty($allowed_groups)) {
|
372
|
$allowed_groups = local_user_get_groups($user, true);
|
373
|
}
|
374
|
|
375
|
if (!is_array($allowed_groups)) {
|
376
|
$allowed_groups = array('all');
|
377
|
} else {
|
378
|
$allowed_groups[] = 'all';
|
379
|
}
|
380
|
|
381
|
foreach ($allowed_groups as $name) {
|
382
|
$group = getGroupEntry($name);
|
383
|
if (is_array($group['priv'])) {
|
384
|
$privs = array_merge($privs, $group['priv']);
|
385
|
}
|
386
|
}
|
387
|
|
388
|
return $privs;
|
389
|
}
|
390
|
|
391
|
function userHasPrivilege($userent, $privid = false) {
|
392
|
global $config;
|
393
|
|
394
|
if (!$privid || !is_array($userent)) {
|
395
|
return false;
|
396
|
}
|
397
|
|
398
|
$privs = get_user_privileges($userent);
|
399
|
|
400
|
if (!is_array($privs)) {
|
401
|
return false;
|
402
|
}
|
403
|
|
404
|
if (!in_array($privid, $privs)) {
|
405
|
return false;
|
406
|
}
|
407
|
|
408
|
/* If someone is in admins group or is admin, do not honor the
|
409
|
* user-config-readonly privilege to prevent foot-shooting due to a
|
410
|
* bad privilege config.
|
411
|
* https://redmine.pfsense.org/issues/10492 */
|
412
|
$userGroups = getUserGroups($userent['name'],
|
413
|
auth_get_authserver($config['system']['webgui']['authmode']),
|
414
|
$_SESSION['user_radius_attributes']);
|
415
|
if (($privid == 'user-config-readonly') &&
|
416
|
(($userent['uid'] === "0") || (in_array('admins', $userGroups)))) {
|
417
|
return false;
|
418
|
}
|
419
|
|
420
|
return true;
|
421
|
}
|
422
|
|
423
|
function local_backed($username, $passwd) {
|
424
|
|
425
|
$user = getUserEntry($username);
|
426
|
if (!$user) {
|
427
|
return false;
|
428
|
}
|
429
|
|
430
|
if (is_account_disabled($username) || is_account_expired($username)) {
|
431
|
return false;
|
432
|
}
|
433
|
|
434
|
if ($user['bcrypt-hash']) {
|
435
|
if (password_verify($passwd, $user['bcrypt-hash'])) {
|
436
|
return true;
|
437
|
}
|
438
|
}
|
439
|
|
440
|
//for backwards compatibility
|
441
|
if ($user['password']) {
|
442
|
if (crypt($passwd, $user['password']) == $user['password']) {
|
443
|
return true;
|
444
|
}
|
445
|
}
|
446
|
|
447
|
if ($user['md5-hash']) {
|
448
|
if (md5($passwd) == $user['md5-hash']) {
|
449
|
return true;
|
450
|
}
|
451
|
}
|
452
|
|
453
|
return false;
|
454
|
}
|
455
|
|
456
|
function local_sync_accounts($u2add, $u2del, $g2add, $g2del) {
|
457
|
global $config, $debug;
|
458
|
|
459
|
if (empty($u2add) && empty($u2del) && empty($g2add) && empty($g2del)) {
|
460
|
/* Nothing to be done here */
|
461
|
return;
|
462
|
}
|
463
|
|
464
|
foreach($u2del as $user) {
|
465
|
if ($user['uid'] > 65000) {
|
466
|
continue;
|
467
|
} else if ($user['uid'] < 2000 && !in_array($user, $u2add)) {
|
468
|
continue;
|
469
|
}
|
470
|
|
471
|
/*
|
472
|
* If a crontab was created to user, pw userdel will be
|
473
|
* interactive and can cause issues. Just remove crontab
|
474
|
* before run it when necessary
|
475
|
*/
|
476
|
unlink_if_exists("/var/cron/tabs/{$user['name']}");
|
477
|
$cmd = "/usr/sbin/pw userdel -n " .
|
478
|
escapeshellarg($user['name']);
|
479
|
if ($debug) {
|
480
|
log_error(sprintf(gettext("Running: %s"), $cmd));
|
481
|
}
|
482
|
mwexec($cmd);
|
483
|
local_group_del_user($user);
|
484
|
|
485
|
$system_user = $config['system']['user'];
|
486
|
for ($i = 0; $i < count($system_user); $i++) {
|
487
|
if ($system_user[$i]['name'] == $user['name']) {
|
488
|
log_error("Removing user: {$user['name']}");
|
489
|
unset($config['system']['user'][$i]);
|
490
|
break;
|
491
|
}
|
492
|
}
|
493
|
}
|
494
|
|
495
|
foreach($g2del as $group) {
|
496
|
if ($group['gid'] < 1999 || $group['gid'] > 65000) {
|
497
|
continue;
|
498
|
}
|
499
|
|
500
|
$cmd = "/usr/sbin/pw groupdel -g " .
|
501
|
escapeshellarg($group['name']);
|
502
|
if ($debug) {
|
503
|
log_error(sprintf(gettext("Running: %s"), $cmd));
|
504
|
}
|
505
|
mwexec($cmd);
|
506
|
|
507
|
$system_group = $config['system']['group'];
|
508
|
for ($i = 0; $i < count($system_group); $i++) {
|
509
|
if ($system_group[$i]['name'] == $group['name']) {
|
510
|
log_error("Removing group: {$group['name']}");
|
511
|
unset($config['system']['group'][$i]);
|
512
|
break;
|
513
|
}
|
514
|
}
|
515
|
}
|
516
|
|
517
|
foreach ($u2add as $user) {
|
518
|
log_error("Adding user: {$user['name']}");
|
519
|
$config['system']['user'][] = $user;
|
520
|
}
|
521
|
|
522
|
foreach ($g2add as $group) {
|
523
|
log_error("Adding group: {$group['name']}");
|
524
|
$config['system']['group'][] = $group;
|
525
|
}
|
526
|
|
527
|
/* Sort it alphabetically */
|
528
|
usort($config['system']['user'], function($a, $b) {
|
529
|
return strcmp($a['name'], $b['name']);
|
530
|
});
|
531
|
usort($config['system']['group'], function($a, $b) {
|
532
|
return strcmp($a['name'], $b['name']);
|
533
|
});
|
534
|
|
535
|
write_config("Sync'd users and groups via XMLRPC");
|
536
|
|
537
|
/* make sure the all group exists */
|
538
|
$allgrp = getGroupEntryByGID(1998);
|
539
|
local_group_set($allgrp, true);
|
540
|
|
541
|
foreach ($u2add as $user) {
|
542
|
local_user_set($user);
|
543
|
}
|
544
|
|
545
|
foreach ($g2add as $group) {
|
546
|
local_group_set($group);
|
547
|
}
|
548
|
}
|
549
|
|
550
|
function local_reset_accounts() {
|
551
|
global $debug, $config;
|
552
|
|
553
|
/* remove local users to avoid uid conflicts */
|
554
|
$fd = popen("/usr/sbin/pw usershow -a", "r");
|
555
|
if ($fd) {
|
556
|
while (!feof($fd)) {
|
557
|
$line = explode(":", fgets($fd));
|
558
|
if ($line[0] != "admin") {
|
559
|
if (!strncmp($line[0], "_", 1)) {
|
560
|
continue;
|
561
|
}
|
562
|
if ($line[2] < 2000) {
|
563
|
continue;
|
564
|
}
|
565
|
if ($line[2] > 65000) {
|
566
|
continue;
|
567
|
}
|
568
|
}
|
569
|
/*
|
570
|
* If a crontab was created to user, pw userdel will be interactive and
|
571
|
* can cause issues. Just remove crontab before run it when necessary
|
572
|
*/
|
573
|
unlink_if_exists("/var/cron/tabs/{$line[0]}");
|
574
|
$cmd = "/usr/sbin/pw userdel -n " . escapeshellarg($line[0]);
|
575
|
if ($debug) {
|
576
|
log_error(sprintf(gettext("Running: %s"), $cmd));
|
577
|
}
|
578
|
mwexec($cmd);
|
579
|
}
|
580
|
pclose($fd);
|
581
|
}
|
582
|
|
583
|
/* remove local groups to avoid gid conflicts */
|
584
|
$gids = array();
|
585
|
$fd = popen("/usr/sbin/pw groupshow -a", "r");
|
586
|
if ($fd) {
|
587
|
while (!feof($fd)) {
|
588
|
$line = explode(":", fgets($fd));
|
589
|
if (!strncmp($line[0], "_", 1)) {
|
590
|
continue;
|
591
|
}
|
592
|
if ($line[2] < 2000) {
|
593
|
continue;
|
594
|
}
|
595
|
if ($line[2] > 65000) {
|
596
|
continue;
|
597
|
}
|
598
|
$cmd = "/usr/sbin/pw groupdel -g " . escapeshellarg($line[2]);
|
599
|
if ($debug) {
|
600
|
log_error(sprintf(gettext("Running: %s"), $cmd));
|
601
|
}
|
602
|
mwexec($cmd);
|
603
|
}
|
604
|
pclose($fd);
|
605
|
}
|
606
|
|
607
|
/* make sure the all group exists */
|
608
|
$allgrp = getGroupEntryByGID(1998);
|
609
|
local_group_set($allgrp, true);
|
610
|
|
611
|
/* sync all local users */
|
612
|
if (is_array($config['system']['user'])) {
|
613
|
foreach ($config['system']['user'] as $user) {
|
614
|
local_user_set($user);
|
615
|
}
|
616
|
}
|
617
|
|
618
|
/* sync all local groups */
|
619
|
if (is_array($config['system']['group'])) {
|
620
|
foreach ($config['system']['group'] as $group) {
|
621
|
local_group_set($group);
|
622
|
}
|
623
|
}
|
624
|
}
|
625
|
|
626
|
function local_user_set(& $user) {
|
627
|
global $g, $debug;
|
628
|
|
629
|
if (empty($user['password']) && empty($user['bcrypt-hash'])) {
|
630
|
log_error("There is something wrong in the config because user {$user['name']} password is missing!");
|
631
|
return;
|
632
|
}
|
633
|
|
634
|
|
635
|
$home_base = "/home/";
|
636
|
$user_uid = $user['uid'];
|
637
|
$user_name = $user['name'];
|
638
|
$user_home = "{$home_base}{$user_name}";
|
639
|
$user_shell = "/etc/rc.initial";
|
640
|
$user_group = "nobody";
|
641
|
|
642
|
// Ensure $home_base exists and is writable
|
643
|
if (!is_dir($home_base)) {
|
644
|
mkdir($home_base, 0755);
|
645
|
}
|
646
|
|
647
|
$lock_account = false;
|
648
|
/* configure shell type */
|
649
|
/* Cases here should be ordered by most privileged to least privileged. */
|
650
|
if (userHasPrivilege($user, "user-shell-access") || userHasPrivilege($user, "page-all")) {
|
651
|
$user_shell = "/bin/tcsh";
|
652
|
} elseif (userHasPrivilege($user, "user-copy-files-chroot")) {
|
653
|
$user_shell = "/usr/local/sbin/scponlyc";
|
654
|
} elseif (userHasPrivilege($user, "user-copy-files")) {
|
655
|
$user_shell = "/usr/local/bin/scponly";
|
656
|
} elseif (userHasPrivilege($user, "user-ssh-tunnel")) {
|
657
|
$user_shell = "/usr/local/sbin/ssh_tunnel_shell";
|
658
|
} elseif (userHasPrivilege($user, "user-ipsec-xauth-dialin")) {
|
659
|
$user_shell = "/sbin/nologin";
|
660
|
} else {
|
661
|
$user_shell = "/sbin/nologin";
|
662
|
$lock_account = true;
|
663
|
}
|
664
|
|
665
|
/* Lock out disabled or expired users, unless it's root/admin. */
|
666
|
if ((is_account_disabled($user_name) || is_account_expired($user_name)) && ($user_uid != 0)) {
|
667
|
$user_shell = "/sbin/nologin";
|
668
|
$lock_account = true;
|
669
|
}
|
670
|
|
671
|
/* root user special handling */
|
672
|
if ($user_uid == 0) {
|
673
|
$cmd = "/usr/sbin/pw usermod -q -n root -s /bin/sh -H 0";
|
674
|
if ($debug) {
|
675
|
log_error(sprintf(gettext("Running: %s"), $cmd));
|
676
|
}
|
677
|
$fd = popen($cmd, "w");
|
678
|
if (empty($user['bcrypt-hash'])) {
|
679
|
fwrite($fd, $user['password']);
|
680
|
} else {
|
681
|
fwrite($fd, $user['bcrypt-hash']);
|
682
|
}
|
683
|
pclose($fd);
|
684
|
$user_group = "wheel";
|
685
|
$user_home = "/root";
|
686
|
$user_shell = "/etc/rc.initial";
|
687
|
}
|
688
|
|
689
|
/* read from pw db */
|
690
|
$fd = popen("/usr/sbin/pw usershow -n {$user_name} 2>&1", "r");
|
691
|
$pwread = fgets($fd);
|
692
|
pclose($fd);
|
693
|
$userattrs = explode(":", trim($pwread));
|
694
|
|
695
|
$skel_dir = '/etc/skel';
|
696
|
|
697
|
/* determine add or mod */
|
698
|
if (($userattrs[0] != $user['name']) || (!strncmp($pwread, "pw:", 3))) {
|
699
|
$user_op = "useradd -m -k " . escapeshellarg($skel_dir) . " -o";
|
700
|
} else {
|
701
|
$user_op = "usermod";
|
702
|
}
|
703
|
|
704
|
$comment = str_replace(array(":", "!", "@"), " ", $user['descr']);
|
705
|
/* add or mod pw db */
|
706
|
$cmd = "/usr/sbin/pw {$user_op} -q " .
|
707
|
" -u " . escapeshellarg($user_uid) .
|
708
|
" -n " . escapeshellarg($user_name) .
|
709
|
" -g " . escapeshellarg($user_group) .
|
710
|
" -s " . escapeshellarg($user_shell) .
|
711
|
" -d " . escapeshellarg($user_home) .
|
712
|
" -c " . escapeshellarg($comment) .
|
713
|
" -H 0 2>&1";
|
714
|
|
715
|
if ($debug) {
|
716
|
log_error(sprintf(gettext("Running: %s"), $cmd));
|
717
|
}
|
718
|
$fd = popen($cmd, "w");
|
719
|
if (empty($user['bcrypt-hash'])) {
|
720
|
fwrite($fd, $user['password']);
|
721
|
} else {
|
722
|
fwrite($fd, $user['bcrypt-hash']);
|
723
|
}
|
724
|
pclose($fd);
|
725
|
|
726
|
/* create user directory if required */
|
727
|
if (!is_dir($user_home)) {
|
728
|
mkdir($user_home, 0700);
|
729
|
}
|
730
|
@chown($user_home, $user_name);
|
731
|
@chgrp($user_home, $user_group);
|
732
|
|
733
|
/* Make sure all users have last version of config files */
|
734
|
foreach (glob("{$skel_dir}/dot.*") as $dot_file) {
|
735
|
$target = $user_home . '/' . substr(basename($dot_file), 3);
|
736
|
@copy($dot_file, $target);
|
737
|
@chown($target, $user_name);
|
738
|
@chgrp($target, $user_group);
|
739
|
}
|
740
|
|
741
|
/* write out ssh authorized key file */
|
742
|
if ($user['authorizedkeys']) {
|
743
|
if (!is_dir("{$user_home}/.ssh")) {
|
744
|
@mkdir("{$user_home}/.ssh", 0700);
|
745
|
@chown("{$user_home}/.ssh", $user_name);
|
746
|
}
|
747
|
$keys = base64_decode($user['authorizedkeys']);
|
748
|
@file_put_contents("{$user_home}/.ssh/authorized_keys", $keys);
|
749
|
@chown("{$user_home}/.ssh/authorized_keys", $user_name);
|
750
|
} else {
|
751
|
unlink_if_exists("{$user_home}/.ssh/authorized_keys");
|
752
|
}
|
753
|
|
754
|
$un = $lock_account ? "" : "un";
|
755
|
exec("/usr/sbin/pw {$un}lock " . escapeshellarg($user_name) . " -q 2>/dev/null");
|
756
|
|
757
|
}
|
758
|
|
759
|
function local_user_del($user) {
|
760
|
global $debug;
|
761
|
|
762
|
/* remove all memberships */
|
763
|
local_user_set_groups($user);
|
764
|
|
765
|
/* Don't remove /root */
|
766
|
if ($user['uid'] != 0) {
|
767
|
$rmhome = "-r";
|
768
|
}
|
769
|
|
770
|
/* read from pw db */
|
771
|
$fd = popen("/usr/sbin/pw usershow -n {$user['name']} 2>&1", "r");
|
772
|
$pwread = fgets($fd);
|
773
|
pclose($fd);
|
774
|
$userattrs = explode(":", trim($pwread));
|
775
|
|
776
|
if ($userattrs[0] != $user['name']) {
|
777
|
log_error("Tried to remove user {$user['name']} but got user {$userattrs[0]} instead. Bailing.");
|
778
|
return;
|
779
|
}
|
780
|
|
781
|
/* delete from pw db */
|
782
|
$cmd = "/usr/sbin/pw userdel -n " . escapeshellarg($user['name']) . " " . escapeshellarg($rmhome);
|
783
|
|
784
|
if ($debug) {
|
785
|
log_error(sprintf(gettext("Running: %s"), $cmd));
|
786
|
}
|
787
|
mwexec($cmd);
|
788
|
|
789
|
/* Delete user from groups needs a call to write_config() */
|
790
|
local_group_del_user($user);
|
791
|
}
|
792
|
|
793
|
function local_user_set_password(&$user, $password) {
|
794
|
global $config;
|
795
|
|
796
|
unset($user['password']);
|
797
|
unset($user['md5-hash']);
|
798
|
$user['bcrypt-hash'] = password_hash($password, PASSWORD_BCRYPT);
|
799
|
if (($user['name'] == $config['hasync']['username']) &&
|
800
|
($config['hasync']['adminsync'] == 'on')) {
|
801
|
$config['hasync']['new_password'] = $password;
|
802
|
}
|
803
|
}
|
804
|
|
805
|
function local_user_get_groups($user, $all = false) {
|
806
|
global $debug, $config;
|
807
|
|
808
|
$groups = array();
|
809
|
if (!is_array($config['system']['group'])) {
|
810
|
return $groups;
|
811
|
}
|
812
|
|
813
|
foreach ($config['system']['group'] as $group) {
|
814
|
if ($all || (!$all && ($group['name'] != "all"))) {
|
815
|
if (is_array($group['member'])) {
|
816
|
if (in_array($user['uid'], $group['member'])) {
|
817
|
$groups[] = $group['name'];
|
818
|
}
|
819
|
}
|
820
|
}
|
821
|
}
|
822
|
|
823
|
if ($all) {
|
824
|
$groups[] = "all";
|
825
|
}
|
826
|
|
827
|
sort($groups);
|
828
|
|
829
|
return $groups;
|
830
|
|
831
|
}
|
832
|
|
833
|
function local_user_set_groups($user, $new_groups = NULL) {
|
834
|
global $debug, $config, $groupindex, $userindex;
|
835
|
|
836
|
if (!is_array($config['system']['group'])) {
|
837
|
return;
|
838
|
}
|
839
|
|
840
|
$cur_groups = local_user_get_groups($user, true);
|
841
|
$mod_groups = array();
|
842
|
|
843
|
if (!is_array($new_groups)) {
|
844
|
$new_groups = array();
|
845
|
}
|
846
|
|
847
|
if (!is_array($cur_groups)) {
|
848
|
$cur_groups = array();
|
849
|
}
|
850
|
|
851
|
/* determine which memberships to add */
|
852
|
foreach ($new_groups as $groupname) {
|
853
|
if ($groupname == '' || in_array($groupname, $cur_groups)) {
|
854
|
continue;
|
855
|
}
|
856
|
$group = &$config['system']['group'][$groupindex[$groupname]];
|
857
|
$group['member'][] = $user['uid'];
|
858
|
$mod_groups[] = $group;
|
859
|
|
860
|
/*
|
861
|
* If it's a new user, make sure it is added before try to
|
862
|
* add it as a member of a group
|
863
|
*/
|
864
|
if (!isset($userindex[$user['uid']])) {
|
865
|
local_user_set($user);
|
866
|
}
|
867
|
}
|
868
|
unset($group);
|
869
|
|
870
|
/* determine which memberships to remove */
|
871
|
foreach ($cur_groups as $groupname) {
|
872
|
if (in_array($groupname, $new_groups)) {
|
873
|
continue;
|
874
|
}
|
875
|
if (!isset($config['system']['group'][$groupindex[$groupname]])) {
|
876
|
continue;
|
877
|
}
|
878
|
$group = &$config['system']['group'][$groupindex[$groupname]];
|
879
|
if (is_array($group['member'])) {
|
880
|
$index = array_search($user['uid'], $group['member']);
|
881
|
array_splice($group['member'], $index, 1);
|
882
|
$mod_groups[] = $group;
|
883
|
}
|
884
|
}
|
885
|
unset($group);
|
886
|
|
887
|
/* sync all modified groups */
|
888
|
foreach ($mod_groups as $group) {
|
889
|
local_group_set($group);
|
890
|
}
|
891
|
}
|
892
|
|
893
|
function local_group_del_user($user) {
|
894
|
global $config;
|
895
|
|
896
|
if (!is_array($config['system']['group'])) {
|
897
|
return;
|
898
|
}
|
899
|
|
900
|
foreach ($config['system']['group'] as $group) {
|
901
|
if (is_array($group['member'])) {
|
902
|
foreach ($group['member'] as $idx => $uid) {
|
903
|
if ($user['uid'] == $uid) {
|
904
|
unset($config['system']['group']['member'][$idx]);
|
905
|
}
|
906
|
}
|
907
|
}
|
908
|
}
|
909
|
}
|
910
|
|
911
|
function local_group_set($group, $reset = false) {
|
912
|
global $debug;
|
913
|
|
914
|
$group_name = $group['name'];
|
915
|
$group_gid = $group['gid'];
|
916
|
$group_members = '';
|
917
|
|
918
|
if (!$reset && !empty($group['member']) && count($group['member']) > 0) {
|
919
|
$group_members = implode(",", $group['member']);
|
920
|
}
|
921
|
|
922
|
if (empty($group_name)) {
|
923
|
return;
|
924
|
}
|
925
|
|
926
|
// If the group is now remote, make sure there is no local group with the same name
|
927
|
if ($group['scope'] == "remote") {
|
928
|
local_group_del($group);
|
929
|
return;
|
930
|
}
|
931
|
|
932
|
/* determine add or mod */
|
933
|
if (mwexec("/usr/sbin/pw groupshow -g " . escapeshellarg($group_gid) . " 2>&1", true) == 0) {
|
934
|
$group_op = "groupmod -l";
|
935
|
} else {
|
936
|
$group_op = "groupadd -n";
|
937
|
}
|
938
|
|
939
|
/* add or mod group db */
|
940
|
$cmd = "/usr/sbin/pw {$group_op} " .
|
941
|
escapeshellarg($group_name) .
|
942
|
" -g " . escapeshellarg($group_gid) .
|
943
|
" -M " . escapeshellarg($group_members) . " 2>&1";
|
944
|
|
945
|
if ($debug) {
|
946
|
log_error(sprintf(gettext("Running: %s"), $cmd));
|
947
|
}
|
948
|
|
949
|
mwexec($cmd);
|
950
|
}
|
951
|
|
952
|
function local_group_del($group) {
|
953
|
global $debug;
|
954
|
|
955
|
/* delete from group db */
|
956
|
$cmd = "/usr/sbin/pw groupdel " . escapeshellarg($group['name']);
|
957
|
|
958
|
if ($debug) {
|
959
|
log_error(sprintf(gettext("Running: %s"), $cmd));
|
960
|
}
|
961
|
mwexec($cmd);
|
962
|
}
|
963
|
|
964
|
function ldap_test_connection($authcfg) {
|
965
|
if ($authcfg) {
|
966
|
if (strstr($authcfg['ldap_urltype'], "SSL")) {
|
967
|
$ldapproto = "ldaps";
|
968
|
} else {
|
969
|
$ldapproto = "ldap";
|
970
|
}
|
971
|
$ldapserver = "{$ldapproto}://" . ldap_format_host($authcfg['host']);
|
972
|
$ldapport = $authcfg['ldap_port'];
|
973
|
if (!empty($ldapport)) {
|
974
|
$ldapserver .= ":{$ldapport}";
|
975
|
}
|
976
|
} else {
|
977
|
return false;
|
978
|
}
|
979
|
|
980
|
/* first check if there is even an LDAP server populated */
|
981
|
if (!$ldapserver) {
|
982
|
return false;
|
983
|
}
|
984
|
|
985
|
/* Setup CA environment if needed. */
|
986
|
ldap_setup_caenv($ldap, $authcfg);
|
987
|
|
988
|
/* connect and see if server is up */
|
989
|
$error = false;
|
990
|
if (!($ldap = ldap_connect($ldapserver))) {
|
991
|
$error = true;
|
992
|
}
|
993
|
|
994
|
if ($error == true) {
|
995
|
log_error(sprintf(gettext("ERROR! Could not connect to server %s."), $authcfg['name']));
|
996
|
return false;
|
997
|
}
|
998
|
|
999
|
return true;
|
1000
|
}
|
1001
|
|
1002
|
function ldap_setup_caenv($ldap, $authcfg) {
|
1003
|
global $g;
|
1004
|
require_once("certs.inc");
|
1005
|
|
1006
|
unset($caref);
|
1007
|
if (empty($authcfg['ldap_caref']) || strstr($authcfg['ldap_urltype'], "Standard")) {
|
1008
|
putenv('LDAPTLS_REQCERT=never');
|
1009
|
return;
|
1010
|
} elseif ($authcfg['ldap_caref'] == "global") {
|
1011
|
putenv('LDAPTLS_REQCERT=hard');
|
1012
|
putenv("LDAPTLS_CACERTDIR=/etc/ssl/");
|
1013
|
putenv("LDAPTLS_CACERT=/etc/ssl/cert.pem");
|
1014
|
} else {
|
1015
|
$caref = lookup_ca($authcfg['ldap_caref']);
|
1016
|
$param = array('caref' => $authcfg['ldap_caref']);
|
1017
|
$cachain = ca_chain($param);
|
1018
|
if (!$caref) {
|
1019
|
log_error(sprintf(gettext("LDAP: Could not lookup CA by reference for host %s."), $authcfg['ldap_caref']));
|
1020
|
/* XXX: Prevent for credential leaking since we cannot setup the CA env. Better way? */
|
1021
|
putenv('LDAPTLS_REQCERT=hard');
|
1022
|
return;
|
1023
|
}
|
1024
|
|
1025
|
$cert_path = "{$g['varrun_path']}/certs";
|
1026
|
safe_mkdir($cert_path);
|
1027
|
unlink_if_exists("{$cert_path}/{$caref['refid']}.ca");
|
1028
|
file_put_contents("{$cert_path}/{$caref['refid']}.ca", $cachain);
|
1029
|
@chmod("{$cert_path}/{$caref['refid']}.ca", 0600);
|
1030
|
|
1031
|
putenv('LDAPTLS_REQCERT=hard');
|
1032
|
/* XXX: Probably even the hashed link should be created for this? */
|
1033
|
putenv("LDAPTLS_CACERTDIR={$g['varrun_path']}/certs");
|
1034
|
putenv("LDAPTLS_CACERT={$g['varrun_path']}/certs/{$caref['refid']}.ca");
|
1035
|
}
|
1036
|
}
|
1037
|
|
1038
|
function ldap_test_bind($authcfg) {
|
1039
|
global $debug, $config, $g;
|
1040
|
|
1041
|
if ($authcfg) {
|
1042
|
if (strstr($authcfg['ldap_urltype'], "SSL")) {
|
1043
|
$ldapproto = "ldaps";
|
1044
|
} else {
|
1045
|
$ldapproto = "ldap";
|
1046
|
}
|
1047
|
$ldapserver = "{$ldapproto}://" . ldap_format_host($authcfg['host']);
|
1048
|
$ldapport = $authcfg['ldap_port'];
|
1049
|
if (!empty($ldapport)) {
|
1050
|
$ldapserver .= ":{$ldapport}";
|
1051
|
}
|
1052
|
$ldapbasedn = $authcfg['ldap_basedn'];
|
1053
|
$ldapbindun = $authcfg['ldap_binddn'];
|
1054
|
$ldapbindpw = $authcfg['ldap_bindpw'];
|
1055
|
$ldapver = $authcfg['ldap_protver'];
|
1056
|
$ldaptimeout = is_numeric($authcfg['ldap_timeout']) ? $authcfg['ldap_timeout'] : 5;
|
1057
|
if (empty($ldapbindun) || empty($ldapbindpw)) {
|
1058
|
$ldapanon = true;
|
1059
|
} else {
|
1060
|
$ldapanon = false;
|
1061
|
}
|
1062
|
} else {
|
1063
|
return false;
|
1064
|
}
|
1065
|
|
1066
|
/* first check if there is even an LDAP server populated */
|
1067
|
if (!$ldapserver) {
|
1068
|
return false;
|
1069
|
}
|
1070
|
|
1071
|
/* Setup CA environment if needed. */
|
1072
|
ldap_setup_caenv($ldap, $authcfg);
|
1073
|
|
1074
|
/* connect and see if server is up */
|
1075
|
$error = false;
|
1076
|
if (!($ldap = ldap_connect($ldapserver))) {
|
1077
|
$error = true;
|
1078
|
}
|
1079
|
|
1080
|
if ($error == true) {
|
1081
|
log_error(sprintf(gettext("ERROR! Could not connect to server %s."), $ldapname));
|
1082
|
return false;
|
1083
|
}
|
1084
|
|
1085
|
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
|
1086
|
ldap_set_option($ldap, LDAP_OPT_DEREF, LDAP_DEREF_SEARCHING);
|
1087
|
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, (int)$ldapver);
|
1088
|
ldap_set_option($ldap, LDAP_OPT_TIMELIMIT, (int)$ldaptimeout);
|
1089
|
ldap_set_option($ldap, LDAP_OPT_NETWORK_TIMEOUT, (int)$ldaptimeout);
|
1090
|
|
1091
|
if (strstr($authcfg['ldap_urltype'], "STARTTLS")) {
|
1092
|
if (!(@ldap_start_tls($ldap))) {
|
1093
|
log_error(sprintf(gettext("ERROR! ldap_test_bind() could not STARTTLS to server %s."), $ldapname));
|
1094
|
@ldap_close($ldap);
|
1095
|
return false;
|
1096
|
}
|
1097
|
}
|
1098
|
|
1099
|
$ldapbindun = isset($authcfg['ldap_utf8']) ? utf8_encode($ldapbindun) : $ldapbindun;
|
1100
|
$ldapbindpw = isset($authcfg['ldap_utf8']) ? utf8_encode($ldapbindpw) : $ldapbindpw;
|
1101
|
if ($ldapanon == true) {
|
1102
|
if (!($res = @ldap_bind($ldap))) {
|
1103
|
@ldap_close($ldap);
|
1104
|
return false;
|
1105
|
}
|
1106
|
} else if (!($res = @ldap_bind($ldap, $ldapbindun, $ldapbindpw))) {
|
1107
|
@ldap_close($ldap);
|
1108
|
return false;
|
1109
|
}
|
1110
|
|
1111
|
@ldap_unbind($ldap);
|
1112
|
|
1113
|
return true;
|
1114
|
}
|
1115
|
|
1116
|
function ldap_get_user_ous($show_complete_ou=true, $authcfg) {
|
1117
|
global $debug, $config, $g;
|
1118
|
|
1119
|
if (!function_exists("ldap_connect")) {
|
1120
|
return;
|
1121
|
}
|
1122
|
|
1123
|
$ous = array();
|
1124
|
|
1125
|
if ($authcfg) {
|
1126
|
if (strstr($authcfg['ldap_urltype'], "SSL")) {
|
1127
|
$ldapproto = "ldaps";
|
1128
|
} else {
|
1129
|
$ldapproto = "ldap";
|
1130
|
}
|
1131
|
$ldapserver = "{$ldapproto}://" . ldap_format_host($authcfg['host']);
|
1132
|
$ldapport = $authcfg['ldap_port'];
|
1133
|
if (!empty($ldapport)) {
|
1134
|
$ldapserver .= ":{$ldapport}";
|
1135
|
}
|
1136
|
$ldapbasedn = $authcfg['ldap_basedn'];
|
1137
|
$ldapbindun = $authcfg['ldap_binddn'];
|
1138
|
$ldapbindpw = $authcfg['ldap_bindpw'];
|
1139
|
$ldapver = $authcfg['ldap_protver'];
|
1140
|
if (empty($ldapbindun) || empty($ldapbindpw)) {
|
1141
|
$ldapanon = true;
|
1142
|
} else {
|
1143
|
$ldapanon = false;
|
1144
|
}
|
1145
|
$ldapname = $authcfg['name'];
|
1146
|
$ldapfallback = false;
|
1147
|
$ldapscope = $authcfg['ldap_scope'];
|
1148
|
$ldaptimeout = is_numeric($authcfg['ldap_timeout']) ? $authcfg['ldap_timeout'] : 5;
|
1149
|
} else {
|
1150
|
return false;
|
1151
|
}
|
1152
|
|
1153
|
/* first check if there is even an LDAP server populated */
|
1154
|
if (!$ldapserver) {
|
1155
|
log_error(gettext("ERROR! ldap_get_user_ous() backed selected with no LDAP authentication server defined."));
|
1156
|
return $ous;
|
1157
|
}
|
1158
|
|
1159
|
/* Setup CA environment if needed. */
|
1160
|
ldap_setup_caenv($ldap, $authcfg);
|
1161
|
|
1162
|
/* connect and see if server is up */
|
1163
|
$error = false;
|
1164
|
if (!($ldap = ldap_connect($ldapserver))) {
|
1165
|
$error = true;
|
1166
|
}
|
1167
|
|
1168
|
if ($error == true) {
|
1169
|
log_error(sprintf(gettext("ERROR! Could not connect to server %s."), $ldapname));
|
1170
|
return $ous;
|
1171
|
}
|
1172
|
|
1173
|
$ldapfilter = "(|(ou=*)(cn=Users))";
|
1174
|
|
1175
|
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
|
1176
|
ldap_set_option($ldap, LDAP_OPT_DEREF, LDAP_DEREF_SEARCHING);
|
1177
|
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, (int)$ldapver);
|
1178
|
ldap_set_option($ldap, LDAP_OPT_TIMELIMIT, (int)$ldaptimeout);
|
1179
|
ldap_set_option($ldap, LDAP_OPT_NETWORK_TIMEOUT, (int)$ldaptimeout);
|
1180
|
|
1181
|
if (strstr($authcfg['ldap_urltype'], "STARTTLS")) {
|
1182
|
if (!(@ldap_start_tls($ldap))) {
|
1183
|
log_error(sprintf(gettext("ERROR! ldap_get_user_ous() could not STARTTLS to server %s."), $ldapname));
|
1184
|
@ldap_close($ldap);
|
1185
|
return false;
|
1186
|
}
|
1187
|
}
|
1188
|
|
1189
|
$ldapbindun = isset($authcfg['ldap_utf8']) ? utf8_encode($ldapbindun) : $ldapbindun;
|
1190
|
$ldapbindpw = isset($authcfg['ldap_utf8']) ? utf8_encode($ldapbindpw) : $ldapbindpw;
|
1191
|
if ($ldapanon == true) {
|
1192
|
if (!($res = @ldap_bind($ldap))) {
|
1193
|
log_error(sprintf(gettext("ERROR! ldap_get_user_ous() could not bind anonymously to server %s."), $ldapname));
|
1194
|
@ldap_close($ldap);
|
1195
|
return $ous;
|
1196
|
}
|
1197
|
} else if (!($res = @ldap_bind($ldap, $ldapbindun, $ldapbindpw))) {
|
1198
|
log_error(sprintf(gettext("ERROR! ldap_get_user_ous() could not bind to server %s."), $ldapname));
|
1199
|
@ldap_close($ldap);
|
1200
|
return $ous;
|
1201
|
}
|
1202
|
|
1203
|
if ($ldapscope == "one") {
|
1204
|
$ldapfunc = "ldap_list";
|
1205
|
} else {
|
1206
|
$ldapfunc = "ldap_search";
|
1207
|
}
|
1208
|
|
1209
|
$search = @$ldapfunc($ldap, $ldapbasedn, $ldapfilter);
|
1210
|
$info = @ldap_get_entries($ldap, $search);
|
1211
|
|
1212
|
if (is_array($info)) {
|
1213
|
foreach ($info as $inf) {
|
1214
|
if (!$show_complete_ou) {
|
1215
|
$inf_split = explode(",", $inf['dn']);
|
1216
|
$ou = $inf_split[0];
|
1217
|
$ou = str_replace("OU=", "", $ou);
|
1218
|
$ou = str_replace("CN=", "", $ou);
|
1219
|
} else {
|
1220
|
if ($inf['dn']) {
|
1221
|
$ou = $inf['dn'];
|
1222
|
}
|
1223
|
}
|
1224
|
if ($ou) {
|
1225
|
$ous[] = $ou;
|
1226
|
}
|
1227
|
}
|
1228
|
}
|
1229
|
|
1230
|
@ldap_unbind($ldap);
|
1231
|
|
1232
|
return $ous;
|
1233
|
}
|
1234
|
|
1235
|
function ldap_get_groups($username, $authcfg) {
|
1236
|
global $debug, $config;
|
1237
|
|
1238
|
if (!function_exists("ldap_connect")) {
|
1239
|
return;
|
1240
|
}
|
1241
|
|
1242
|
if (!$username) {
|
1243
|
return false;
|
1244
|
}
|
1245
|
|
1246
|
if (!isset($authcfg['ldap_nostrip_at']) && stristr($username, "@")) {
|
1247
|
$username_split = explode("@", $username);
|
1248
|
$username = $username_split[0];
|
1249
|
}
|
1250
|
|
1251
|
if (stristr($username, "\\")) {
|
1252
|
$username_split = explode("\\", $username);
|
1253
|
$username = $username_split[0];
|
1254
|
}
|
1255
|
|
1256
|
//log_error("Getting LDAP groups for {$username}.");
|
1257
|
if ($authcfg) {
|
1258
|
if (strstr($authcfg['ldap_urltype'], "SSL")) {
|
1259
|
$ldapproto = "ldaps";
|
1260
|
} else {
|
1261
|
$ldapproto = "ldap";
|
1262
|
}
|
1263
|
$ldapserver = "{$ldapproto}://" . ldap_format_host($authcfg['host']);
|
1264
|
$ldapport = $authcfg['ldap_port'];
|
1265
|
if (!empty($ldapport)) {
|
1266
|
$ldapserver .= ":{$ldapport}";
|
1267
|
}
|
1268
|
$ldapbasedn = $authcfg['ldap_basedn'];
|
1269
|
$ldapbindun = $authcfg['ldap_binddn'];
|
1270
|
$ldapbindpw = $authcfg['ldap_bindpw'];
|
1271
|
$ldapauthcont = $authcfg['ldap_authcn'];
|
1272
|
$ldapnameattribute = strtolower($authcfg['ldap_attr_user']);
|
1273
|
$ldapgroupattribute = strtolower($authcfg['ldap_attr_member']);
|
1274
|
$ldaptype = "";
|
1275
|
$ldapver = $authcfg['ldap_protver'];
|
1276
|
if (empty($ldapbindun) || empty($ldapbindpw)) {
|
1277
|
$ldapanon = true;
|
1278
|
} else {
|
1279
|
$ldapanon = false;
|
1280
|
}
|
1281
|
$ldapname = $authcfg['name'];
|
1282
|
$ldapfallback = false;
|
1283
|
$ldapscope = $authcfg['ldap_scope'];
|
1284
|
$ldaptimeout = is_numeric($authcfg['ldap_timeout']) ? $authcfg['ldap_timeout'] : 5;
|
1285
|
} else {
|
1286
|
return false;
|
1287
|
}
|
1288
|
|
1289
|
if (isset($authcfg['ldap_rfc2307'])) {
|
1290
|
$ldapdn = $ldapbasedn;
|
1291
|
} else {
|
1292
|
$ldapdn = $_SESSION['ldapdn'];
|
1293
|
}
|
1294
|
|
1295
|
/*Convert attribute to lowercase. php ldap arrays put everything in lowercase */
|
1296
|
$ldapgroupattribute = strtolower($ldapgroupattribute);
|
1297
|
$memberof = array();
|
1298
|
|
1299
|
/* Setup CA environment if needed. */
|
1300
|
ldap_setup_caenv($ldap, $authcfg);
|
1301
|
|
1302
|
/* connect and see if server is up */
|
1303
|
$error = false;
|
1304
|
if (!($ldap = ldap_connect($ldapserver))) {
|
1305
|
$error = true;
|
1306
|
}
|
1307
|
|
1308
|
if ($error == true) {
|
1309
|
log_error(sprintf(gettext("ERROR! ldap_get_groups() Could not connect to server %s."), $ldapname));
|
1310
|
return $memberof;
|
1311
|
}
|
1312
|
|
1313
|
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
|
1314
|
ldap_set_option($ldap, LDAP_OPT_DEREF, LDAP_DEREF_SEARCHING);
|
1315
|
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, (int)$ldapver);
|
1316
|
ldap_set_option($ldap, LDAP_OPT_TIMELIMIT, (int)$ldaptimeout);
|
1317
|
ldap_set_option($ldap, LDAP_OPT_NETWORK_TIMEOUT, (int)$ldaptimeout);
|
1318
|
|
1319
|
if (strstr($authcfg['ldap_urltype'], "STARTTLS")) {
|
1320
|
if (!(@ldap_start_tls($ldap))) {
|
1321
|
log_error(sprintf(gettext("ERROR! ldap_get_groups() could not STARTTLS to server %s."), $ldapname));
|
1322
|
@ldap_close($ldap);
|
1323
|
return false;
|
1324
|
}
|
1325
|
}
|
1326
|
|
1327
|
/* bind as user that has rights to read group attributes */
|
1328
|
$ldapbindun = isset($authcfg['ldap_utf8']) ? utf8_encode($ldapbindun) : $ldapbindun;
|
1329
|
$ldapbindpw = isset($authcfg['ldap_utf8']) ? utf8_encode($ldapbindpw) : $ldapbindpw;
|
1330
|
if ($ldapanon == true) {
|
1331
|
if (!($res = @ldap_bind($ldap))) {
|
1332
|
log_error(sprintf(gettext("ERROR! ldap_get_groups() could not bind anonymously to server %s."), $ldapname));
|
1333
|
@ldap_close($ldap);
|
1334
|
return false;
|
1335
|
}
|
1336
|
} else if (!($res = @ldap_bind($ldap, $ldapbindun, $ldapbindpw))) {
|
1337
|
log_error(sprintf(gettext("ERROR! ldap_get_groups() could not bind to server %s."), $ldapname));
|
1338
|
@ldap_close($ldap);
|
1339
|
return $memberof;
|
1340
|
}
|
1341
|
|
1342
|
/* get groups from DN found */
|
1343
|
/* use ldap_read instead of search so we don't have to do a bunch of extra work */
|
1344
|
/* since we know the DN is in $_SESSION['ldapdn'] */
|
1345
|
//$search = ldap_read($ldap, $ldapdn, "(objectclass=*)", array($ldapgroupattribute));
|
1346
|
if ($ldapscope == "one") {
|
1347
|
$ldapfunc = "ldap_list";
|
1348
|
} else {
|
1349
|
$ldapfunc = "ldap_search";
|
1350
|
}
|
1351
|
|
1352
|
if (isset($authcfg['ldap_rfc2307'])) {
|
1353
|
if (isset($authcfg['ldap_rfc2307_userdn'])) {
|
1354
|
$ldac_splits = explode(";", $ldapauthcont);
|
1355
|
foreach ($ldac_splits as $i => $ldac_split) {
|
1356
|
$ldac_split = isset($authcfg['ldap_utf8']) ? utf8_encode($ldac_split) : $ldac_split;
|
1357
|
$ldapsearchbasedn = isset($authcfg['ldap_utf8']) ? utf8_encode("{$ldac_split},{$ldapbasedn}") : "{$ldac_split},{$ldapbasedn}";
|
1358
|
$ldapfilter = "({$ldapnameattribute}={$username})";
|
1359
|
if (stristr($ldac_split, "DC=") || empty($ldapbasedn)) {
|
1360
|
$ldapdn = $ldac_split;
|
1361
|
} else {
|
1362
|
$ldapdn = $ldapsearchbasedn;
|
1363
|
}
|
1364
|
$usersearch = @$ldapfunc($ldap, $ldapdn, $ldapfilter);
|
1365
|
$userinfo = @ldap_get_entries($ldap, $usersearch);
|
1366
|
}
|
1367
|
$username = $userinfo[0]['dn'];
|
1368
|
}
|
1369
|
$ldapgroupfilter = "(&(objectClass={$authcfg['ldap_attr_groupobj']})({$ldapgroupattribute}={$username}))";
|
1370
|
} else {
|
1371
|
$ldapgroupfilter = "({$ldapnameattribute}={$username})";
|
1372
|
}
|
1373
|
|
1374
|
$search = @$ldapfunc($ldap, $ldapdn, $ldapgroupfilter, array($ldapgroupattribute));
|
1375
|
$info = @ldap_get_entries($ldap, $search);
|
1376
|
|
1377
|
$gresults = isset($authcfg['ldap_rfc2307']) ? $info : $info[0][$ldapgroupattribute];
|
1378
|
|
1379
|
if (is_array($gresults)) {
|
1380
|
/* Iterate through the groups and throw them into an array */
|
1381
|
foreach ($gresults as $grp) {
|
1382
|
if (((isset($authcfg['ldap_rfc2307'])) && (stristr($grp["dn"], "CN=") !== false)) ||
|
1383
|
((!isset($authcfg['ldap_rfc2307'])) && (stristr($grp, "CN=") !== false))) {
|
1384
|
$grpsplit = isset($authcfg['ldap_rfc2307']) ? explode(",", $grp["dn"]) : explode(",", $grp);
|
1385
|
$memberof[] = preg_replace("/CN=/i", "", $grpsplit[0]);
|
1386
|
}
|
1387
|
}
|
1388
|
}
|
1389
|
|
1390
|
/* Time to close LDAP connection */
|
1391
|
@ldap_unbind($ldap);
|
1392
|
|
1393
|
$groups = print_r($memberof, true);
|
1394
|
|
1395
|
//log_error("Returning groups ".$groups." for user $username");
|
1396
|
|
1397
|
return $memberof;
|
1398
|
}
|
1399
|
|
1400
|
function ldap_format_host($host) {
|
1401
|
return is_ipaddrv6($host) ? "[$host]" : $host ;
|
1402
|
}
|
1403
|
|
1404
|
function ldap_backed($username, $passwd, $authcfg, &$attributes = array()) {
|
1405
|
global $debug, $config;
|
1406
|
|
1407
|
if (!$username) {
|
1408
|
$attributes['error_message'] = gettext("Invalid Login.");
|
1409
|
return false;
|
1410
|
}
|
1411
|
|
1412
|
if (!isset($authcfg['ldap_allow_unauthenticated']) && $passwd == '') {
|
1413
|
$attributes['error_message'] = gettext("Invalid credentials.");
|
1414
|
return false;
|
1415
|
}
|
1416
|
|
1417
|
if (!function_exists("ldap_connect")) {
|
1418
|
log_error(gettext("ERROR! unable to find ldap_connect() function."));
|
1419
|
$attributes['error_message'] = gettext("Internal error during authentication.");
|
1420
|
return null;
|
1421
|
}
|
1422
|
|
1423
|
if (!isset($authcfg['ldap_nostrip_at']) && stristr($username, "@")) {
|
1424
|
$username_split = explode("@", $username);
|
1425
|
$username = $username_split[0];
|
1426
|
}
|
1427
|
if (stristr($username, "\\")) {
|
1428
|
$username_split = explode("\\", $username);
|
1429
|
$username = $username_split[0];
|
1430
|
}
|
1431
|
|
1432
|
$username = ldap_escape($username, null, LDAP_ESCAPE_FILTER);
|
1433
|
|
1434
|
if ($authcfg) {
|
1435
|
if (strstr($authcfg['ldap_urltype'], "SSL")) {
|
1436
|
$ldapproto = "ldaps";
|
1437
|
} else {
|
1438
|
$ldapproto = "ldap";
|
1439
|
}
|
1440
|
$ldapserver = "{$ldapproto}://" . ldap_format_host($authcfg['host']);
|
1441
|
$ldapport = $authcfg['ldap_port'];
|
1442
|
if (!empty($ldapport)) {
|
1443
|
$ldapserver .= ":{$ldapport}";
|
1444
|
}
|
1445
|
$ldapbasedn = $authcfg['ldap_basedn'];
|
1446
|
$ldapbindun = $authcfg['ldap_binddn'];
|
1447
|
$ldapbindpw = $authcfg['ldap_bindpw'];
|
1448
|
if (empty($ldapbindun) || empty($ldapbindpw)) {
|
1449
|
$ldapanon = true;
|
1450
|
} else {
|
1451
|
$ldapanon = false;
|
1452
|
}
|
1453
|
$ldapauthcont = $authcfg['ldap_authcn'];
|
1454
|
$ldapnameattribute = strtolower($authcfg['ldap_attr_user']);
|
1455
|
$ldapgroupattribute = $authcfg['ldap_attr_member'];
|
1456
|
$ldapextendedqueryenabled = $authcfg['ldap_extended_enabled'];
|
1457
|
$ldapextendedquery = $authcfg['ldap_extended_query'];
|
1458
|
$ldapfilter = "";
|
1459
|
if (!$ldapextendedqueryenabled) {
|
1460
|
$ldapfilter = "({$ldapnameattribute}={$username})";
|
1461
|
} else {
|
1462
|
if (isset($authcfg['ldap_rfc2307'])) {
|
1463
|
$ldapfilter = "({$ldapnameattribute}={$username})";
|
1464
|
$ldapgroupfilter = "(&({$ldapgroupattribute}={$username})({$ldapextendedquery}))";
|
1465
|
} else {
|
1466
|
$ldapfilter = "(&({$ldapnameattribute}={$username})({$ldapextendedquery}))";
|
1467
|
}
|
1468
|
}
|
1469
|
$ldaptype = "";
|
1470
|
$ldapver = $authcfg['ldap_protver'];
|
1471
|
$ldapname = $authcfg['name'];
|
1472
|
$ldapscope = $authcfg['ldap_scope'];
|
1473
|
$ldaptimeout = is_numeric($authcfg['ldap_timeout']) ? $authcfg['ldap_timeout'] : 5;
|
1474
|
} else {
|
1475
|
return null;
|
1476
|
}
|
1477
|
|
1478
|
/* first check if there is even an LDAP server populated */
|
1479
|
if (!$ldapserver) {
|
1480
|
log_error(gettext("ERROR! could not find details of the LDAP server used for authentication."));
|
1481
|
$attributes['error_message'] = gettext("Internal error during authentication.");
|
1482
|
return null;
|
1483
|
}
|
1484
|
|
1485
|
/* Setup CA environment if needed. */
|
1486
|
ldap_setup_caenv($ldap, $authcfg);
|
1487
|
|
1488
|
/* Make sure we can connect to LDAP */
|
1489
|
$error = false;
|
1490
|
if (!($ldap = ldap_connect($ldapserver))) {
|
1491
|
$error = true;
|
1492
|
}
|
1493
|
|
1494
|
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
|
1495
|
ldap_set_option($ldap, LDAP_OPT_DEREF, LDAP_DEREF_SEARCHING);
|
1496
|
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, (int)$ldapver);
|
1497
|
ldap_set_option($ldap, LDAP_OPT_TIMELIMIT, (int)$ldaptimeout);
|
1498
|
ldap_set_option($ldap, LDAP_OPT_NETWORK_TIMEOUT, (int)$ldaptimeout);
|
1499
|
|
1500
|
if (strstr($authcfg['ldap_urltype'], "STARTTLS")) {
|
1501
|
if (!(@ldap_start_tls($ldap))) {
|
1502
|
log_error(sprintf(gettext("ERROR! could not connect to LDAP server %s using STARTTLS."), $ldapname));
|
1503
|
$attributes['error_message'] = gettext("Error : could not connect to authentication server.");
|
1504
|
@ldap_close($ldap);
|
1505
|
return null;
|
1506
|
}
|
1507
|
}
|
1508
|
|
1509
|
if ($error == true) {
|
1510
|
$errormsg = sprintf(gettext("ERROR! Could not connect to server %s."), $ldapname);
|
1511
|
$attributes['error_message'] = gettext("Error : could not connect to authentication server.");
|
1512
|
return null;
|
1513
|
}
|
1514
|
|
1515
|
/* ok, its up. now, lets bind as the bind user so we can search it */
|
1516
|
$error = false;
|
1517
|
$ldapbindun = isset($authcfg['ldap_utf8']) ? utf8_encode($ldapbindun) : $ldapbindun;
|
1518
|
$ldapbindpw = isset($authcfg['ldap_utf8']) ? utf8_encode($ldapbindpw) : $ldapbindpw;
|
1519
|
if ($ldapanon == true) {
|
1520
|
if (!($res = @ldap_bind($ldap))) {
|
1521
|
$error = true;
|
1522
|
}
|
1523
|
} else if (!($res = @ldap_bind($ldap, $ldapbindun, $ldapbindpw))) {
|
1524
|
$error = true;
|
1525
|
}
|
1526
|
|
1527
|
if ($error == true) {
|
1528
|
@ldap_close($ldap);
|
1529
|
log_error(sprintf(gettext("ERROR! Could not bind to LDAP server %s. Please check the bind credentials."), $ldapname));
|
1530
|
$attributes['error_message'] = gettext("Error : could not connect to authentication server.");
|
1531
|
return null;
|
1532
|
}
|
1533
|
|
1534
|
/* Get LDAP Authcontainers and split em up. */
|
1535
|
$ldac_splits = explode(";", $ldapauthcont);
|
1536
|
|
1537
|
/* setup the usercount so we think we haven't found anyone yet */
|
1538
|
$usercount = 0;
|
1539
|
|
1540
|
/*****************************************************************/
|
1541
|
/* We first find the user based on username and filter */
|
1542
|
/* then, once we find the first occurrence of that person */
|
1543
|
/* we set session variables to point to the OU and DN of the */
|
1544
|
/* person. To later be used by ldap_get_groups. */
|
1545
|
/* that way we don't have to search twice. */
|
1546
|
/*****************************************************************/
|
1547
|
if ($debug) {
|
1548
|
log_auth(sprintf(gettext("Now Searching for %s in directory."), $username));
|
1549
|
}
|
1550
|
/* Iterate through the user containers for search */
|
1551
|
foreach ($ldac_splits as $i => $ldac_split) {
|
1552
|
$ldac_split = isset($authcfg['ldap_utf8']) ? utf8_encode($ldac_split) : $ldac_split;
|
1553
|
$ldapfilter = isset($authcfg['ldap_utf8']) ? utf8_encode($ldapfilter) : $ldapfilter;
|
1554
|
$ldapsearchbasedn = isset($authcfg['ldap_utf8']) ? utf8_encode("{$ldac_split},{$ldapbasedn}") : "{$ldac_split},{$ldapbasedn}";
|
1555
|
/* Make sure we just use the first user we find */
|
1556
|
if ($debug) {
|
1557
|
log_auth(sprintf(gettext('Now Searching in server %1$s, container %2$s with filter %3$s.'), $ldapname, utf8_decode($ldac_split), utf8_decode($ldapfilter)));
|
1558
|
}
|
1559
|
if ($ldapscope == "one") {
|
1560
|
$ldapfunc = "ldap_list";
|
1561
|
} else {
|
1562
|
$ldapfunc = "ldap_search";
|
1563
|
}
|
1564
|
/* Support legacy auth container specification. */
|
1565
|
if (stristr($ldac_split, "DC=") || empty($ldapbasedn)) {
|
1566
|
$ldapdn = $ldac_split;
|
1567
|
} else {
|
1568
|
$ldapdn = $ldapsearchbasedn;
|
1569
|
}
|
1570
|
$search = @$ldapfunc($ldap, $ldapdn, $ldapfilter);
|
1571
|
$info = ldap_get_entries($ldap, $search);
|
1572
|
if (isset($authcfg['ldap_rfc2307'])) {
|
1573
|
if (isset($authcfg['ldap_rfc2307_userdn'])) {
|
1574
|
$username = $info[0]['dn'];
|
1575
|
}
|
1576
|
$ldapgroupfilter = "(&({$ldapgroupattribute}={$username})({$ldapextendedquery}))";
|
1577
|
}
|
1578
|
|
1579
|
if (isset($ldapgroupfilter)) {
|
1580
|
$groupsearch = @$ldapfunc($ldap, $ldapdn, $ldapgroupfilter);
|
1581
|
}
|
1582
|
|
1583
|
if (isset($ldapgroupfilter) && !$groupsearch) {
|
1584
|
log_error(sprintf(gettext("Extended group search resulted in error: %s"), ldap_error($ldap)));
|
1585
|
continue;
|
1586
|
}
|
1587
|
if (!$search) {
|
1588
|
log_error(sprintf(gettext("Search resulted in error: %s"), ldap_error($ldap)));
|
1589
|
continue;
|
1590
|
}
|
1591
|
if (isset($groupsearch)) {
|
1592
|
$validgroup = ldap_count_entries($ldap, $groupsearch);
|
1593
|
if ($debug) {
|
1594
|
log_auth(sprintf(gettext("LDAP group search: %s results."), $validgroup));
|
1595
|
}
|
1596
|
}
|
1597
|
$matches = $info['count'];
|
1598
|
if ($matches == 1) {
|
1599
|
$userdn = $_SESSION['ldapdn'] = $info[0]['dn'];
|
1600
|
$_SESSION['ldapou'] = $ldac_split[$i];
|
1601
|
$_SESSION['ldapon'] = "true";
|
1602
|
$usercount = 1;
|
1603
|
break;
|
1604
|
}
|
1605
|
}
|
1606
|
|
1607
|
if ($usercount != 1) {
|
1608
|
@ldap_unbind($ldap);
|
1609
|
if ($debug) {
|
1610
|
if ($usercount === 0) {
|
1611
|
log_error(sprintf(gettext("ERROR! LDAP search failed, no user matching %s was found."), $username));
|
1612
|
} else {
|
1613
|
log_error(sprintf(gettext("ERROR! LDAP search failed, multiple users matching %s were found."), $username));
|
1614
|
}
|
1615
|
}
|
1616
|
$attributes['error_message'] = gettext("Invalid login specified.");
|
1617
|
return false;
|
1618
|
}
|
1619
|
|
1620
|
/* Now lets bind as the user we found */
|
1621
|
$passwd = isset($authcfg['ldap_utf8']) ? utf8_encode($passwd) : $passwd;
|
1622
|
if (!($res = @ldap_bind($ldap, $userdn, $passwd))) {
|
1623
|
if ($debug) {
|
1624
|
log_error(sprintf(gettext('ERROR! Could not login to server %1$s as user %2$s: %3$s'), $ldapname, $username, ldap_error($ldap)));
|
1625
|
}
|
1626
|
@ldap_unbind($ldap);
|
1627
|
return false;
|
1628
|
}
|
1629
|
|
1630
|
if ($debug) {
|
1631
|
$userdn = isset($authcfg['ldap_utf8']) ? utf8_decode($userdn) : $userdn;
|
1632
|
log_auth(sprintf(gettext('Logged in successfully as %1$s via LDAP server %2$s with DN = %3$s.'), $username, $ldapname, $userdn));
|
1633
|
}
|
1634
|
|
1635
|
if ($debug && isset($ldapgroupfilter) && $validgroup < 1) {
|
1636
|
log_auth(sprintf(gettext('Logged in successfully as %1$s but did not match any field in extended query.'), $username));
|
1637
|
}
|
1638
|
|
1639
|
/* At this point we are bound to LDAP so the user was auth'd okay. Close connection. */
|
1640
|
@ldap_unbind($ldap);
|
1641
|
|
1642
|
if (isset($ldapgroupfilter) && $validgroup < 1) {
|
1643
|
return false;
|
1644
|
}
|
1645
|
|
1646
|
return true;
|
1647
|
}
|
1648
|
|
1649
|
function radius_backed($username, $password, $authcfg, &$attributes = array()) {
|
1650
|
global $debug, $config;
|
1651
|
$ret = false;
|
1652
|
|
1653
|
require_once("Auth/RADIUS.php");
|
1654
|
require_once("Crypt/CHAP.php");
|
1655
|
|
1656
|
if ($authcfg) {
|
1657
|
$radiusservers = array();
|
1658
|
$radiusservers[0]['ipaddr'] = $authcfg['host'];
|
1659
|
$radiusservers[0]['port'] = $authcfg['radius_auth_port'];
|
1660
|
$radiusservers[0]['sharedsecret'] = $authcfg['radius_secret'];
|
1661
|
$radiusservers[0]['timeout'] = $authcfg['radius_timeout'];
|
1662
|
if(isset($authcfg['radius_protocol'])) {
|
1663
|
$radius_protocol = $authcfg['radius_protocol'];
|
1664
|
} else {
|
1665
|
$radius_protocol = 'PAP';
|
1666
|
}
|
1667
|
} else {
|
1668
|
log_error(gettext("ERROR! could not find details of the RADIUS server used for authentication."));
|
1669
|
$attributes['error_message'] = gettext("Internal error during authentication.");
|
1670
|
return null;
|
1671
|
}
|
1672
|
|
1673
|
// Create our instance
|
1674
|
$classname = 'Auth_RADIUS_' . $radius_protocol;
|
1675
|
$rauth = new $classname($username, $password);
|
1676
|
|
1677
|
/* Add new servers to our instance */
|
1678
|
foreach ($radiusservers as $radsrv) {
|
1679
|
$timeout = (is_numeric($radsrv['timeout'])) ? $radsrv['timeout'] : 5;
|
1680
|
$rauth->addServer($radsrv['ipaddr'], $radsrv['port'], $radsrv['sharedsecret'], $timeout);
|
1681
|
}
|
1682
|
|
1683
|
// Construct data package
|
1684
|
$rauth->username = $username;
|
1685
|
switch ($radius_protocol) {
|
1686
|
case 'CHAP_MD5':
|
1687
|
case 'MSCHAPv1':
|
1688
|
$classname = $radius_protocol == 'MSCHAPv1' ? 'Crypt_CHAP_MSv1' : 'Crypt_CHAP_MD5';
|
1689
|
$crpt = new $classname;
|
1690
|
$crpt->username = $username;
|
1691
|
$crpt->password = $password;
|
1692
|
$rauth->challenge = $crpt->challenge;
|
1693
|
$rauth->chapid = $crpt->chapid;
|
1694
|
$rauth->response = $crpt->challengeResponse();
|
1695
|
$rauth->flags = 1;
|
1696
|
break;
|
1697
|
|
1698
|
case 'MSCHAPv2':
|
1699
|
$crpt = new Crypt_CHAP_MSv2;
|
1700
|
$crpt->username = $username;
|
1701
|
$crpt->password = $password;
|
1702
|
$rauth->challenge = $crpt->authChallenge;
|
1703
|
$rauth->peerChallenge = $crpt->peerChallenge;
|
1704
|
$rauth->chapid = $crpt->chapid;
|
1705
|
$rauth->response = $crpt->challengeResponse();
|
1706
|
break;
|
1707
|
|
1708
|
default:
|
1709
|
$rauth->password = $password;
|
1710
|
break;
|
1711
|
}
|
1712
|
|
1713
|
if (PEAR::isError($rauth->start())) {
|
1714
|
$ret = null;
|
1715
|
log_error(sprintf(gettext("Error during RADIUS authentication : %s"), $rauth->getError()));
|
1716
|
$attributes['error_message'] = gettext("Error : could not connect to authentication server.");
|
1717
|
} else {
|
1718
|
$nasid = $attributes['nas_identifier'];
|
1719
|
$nasip = $authcfg['radius_nasip_attribute'];
|
1720
|
if (empty($nasid)) {
|
1721
|
$nasid = gethostname(); //If no RADIUS NAS-Identifier is given : we use pfsense's hostname as NAS-Identifier
|
1722
|
}
|
1723
|
if (!is_ipaddr($nasip)) {
|
1724
|
$nasip = get_interface_ip($nasip);
|
1725
|
|
1726
|
if (!is_ipaddr($nasip)) {
|
1727
|
$nasip = get_interface_ip();//We use wan interface IP as fallback for NAS-IP-Address
|
1728
|
}
|
1729
|
}
|
1730
|
$nasmac = get_interface_mac(find_ip_interface($nasip));
|
1731
|
|
1732
|
$rauth->putAttribute(RADIUS_NAS_IP_ADDRESS, $nasip, "addr");
|
1733
|
$rauth->putAttribute(RADIUS_NAS_IDENTIFIER, $nasid);
|
1734
|
|
1735
|
if(!empty($attributes['calling_station_id'])) {
|
1736
|
$rauth->putAttribute(RADIUS_CALLING_STATION_ID, $attributes['calling_station_id']);
|
1737
|
}
|
1738
|
// Carefully check that interface has a MAC address
|
1739
|
if(!empty($nasmac)) {
|
1740
|
$nasmac = mac_format($nasmac);
|
1741
|
$rauth->putAttribute(RADIUS_CALLED_STATION_ID, $nasmac.':'.gethostname());
|
1742
|
}
|
1743
|
if(!empty($attributes['nas_port_type'])) {
|
1744
|
$rauth->putAttribute(RADIUS_NAS_PORT_TYPE, $attributes['nas_port_type']);
|
1745
|
}
|
1746
|
if(!empty($attributes['nas_port'])) {
|
1747
|
$rauth->putAttribute(RADIUS_NAS_PORT, intval($attributes['nas_port']), 'integer');
|
1748
|
}
|
1749
|
if(!empty($attributes['framed_ip']) && is_ipaddr($attributes['framed_ip'])) {
|
1750
|
$rauth->putAttribute(RADIUS_FRAMED_IP_ADDRESS, $attributes['framed_ip'], "addr");
|
1751
|
}
|
1752
|
}
|
1753
|
|
1754
|
// XXX - billm - somewhere in here we need to handle securid challenge/response
|
1755
|
|
1756
|
/* Send request */
|
1757
|
$result = $rauth->send();
|
1758
|
if (PEAR::isError($result)) {
|
1759
|
log_error(sprintf(gettext("Error during RADIUS authentication : %s"), $rauth->getError()));
|
1760
|
$attributes['error_message'] = gettext("Error : could not connect to authentication server.");
|
1761
|
$ret = null;
|
1762
|
} else if ($result === true) {
|
1763
|
$ret = true;
|
1764
|
} else {
|
1765
|
$ret = false;
|
1766
|
}
|
1767
|
|
1768
|
|
1769
|
// Get attributes, even if auth failed.
|
1770
|
if ($rauth->getAttributes()) {
|
1771
|
$attributes = array_merge($attributes,$rauth->listAttributes());
|
1772
|
|
1773
|
// We convert the session_terminate_time to unixtimestamp if its set before returning the whole array to our caller
|
1774
|
if (!empty($attributes['session_terminate_time'])) {
|
1775
|
$stt = &$attributes['session_terminate_time'];
|
1776
|
$stt = strtotime(preg_replace("/\+(\d+):(\d+)$/", " +\${1}\${2}", preg_replace("/(\d+)T(\d+)/", "\${1} \${2}",$stt)));
|
1777
|
}
|
1778
|
}
|
1779
|
|
1780
|
// close OO RADIUS_AUTHENTICATION
|
1781
|
$rauth->close();
|
1782
|
|
1783
|
return $ret;
|
1784
|
}
|
1785
|
|
1786
|
/*
|
1787
|
$attributes must contain a "class" key containing the groups and local
|
1788
|
groups must exist to match.
|
1789
|
*/
|
1790
|
function radius_get_groups($attributes) {
|
1791
|
$groups = array();
|
1792
|
if (!empty($attributes) && is_array($attributes) && (!empty($attributes['class']) || !empty($attributes['class_int']))) {
|
1793
|
/* Some RADIUS servers return multiple class attributes, so check them all. */
|
1794
|
$groups = array();
|
1795
|
if (!empty($attributes['class']) && is_array($attributes['class'])) {
|
1796
|
foreach ($attributes['class'] as $class) {
|
1797
|
$groups = array_unique(array_merge($groups, explode(";", $class)));
|
1798
|
}
|
1799
|
}
|
1800
|
|
1801
|
foreach ($groups as & $grp) {
|
1802
|
$grp = trim($grp);
|
1803
|
if (strtolower(substr($grp, 0, 3)) == "ou=") {
|
1804
|
$grp = substr($grp, 3);
|
1805
|
}
|
1806
|
}
|
1807
|
}
|
1808
|
return $groups;
|
1809
|
}
|
1810
|
|
1811
|
function get_user_expiration_date($username) {
|
1812
|
$user = getUserEntry($username);
|
1813
|
if ($user['expires']) {
|
1814
|
return $user['expires'];
|
1815
|
}
|
1816
|
}
|
1817
|
|
1818
|
function is_account_expired($username) {
|
1819
|
$expirydate = get_user_expiration_date($username);
|
1820
|
if ($expirydate) {
|
1821
|
if (strtotime("-1 day") > strtotime(date("m/d/Y", strtotime($expirydate)))) {
|
1822
|
return true;
|
1823
|
}
|
1824
|
}
|
1825
|
|
1826
|
return false;
|
1827
|
}
|
1828
|
|
1829
|
function is_account_disabled($username) {
|
1830
|
$user = getUserEntry($username);
|
1831
|
if (isset($user['disabled'])) {
|
1832
|
return true;
|
1833
|
}
|
1834
|
|
1835
|
return false;
|
1836
|
}
|
1837
|
|
1838
|
function get_user_settings($username) {
|
1839
|
global $config;
|
1840
|
$settings = array();
|
1841
|
$settings['widgets'] = $config['widgets'];
|
1842
|
$settings['webgui']['dashboardcolumns'] = $config['system']['webgui']['dashboardcolumns'];
|
1843
|
$settings['webgui']['webguihostnamemenu'] = $config['system']['webgui']['webguihostnamemenu'];
|
1844
|
$settings['webgui']['webguicss'] = $config['system']['webgui']['webguicss'];
|
1845
|
$settings['webgui']['logincss'] = $config['system']['webgui']['logincss'];
|
1846
|
$settings['webgui']['interfacessort'] = isset($config['system']['webgui']['interfacessort']);
|
1847
|
$settings['webgui']['dashboardavailablewidgetspanel'] = isset($config['system']['webgui']['dashboardavailablewidgetspanel']);
|
1848
|
$settings['webgui']['webguifixedmenu'] = isset($config['system']['webgui']['webguifixedmenu']);
|
1849
|
$settings['webgui']['webguileftcolumnhyper'] = isset($config['system']['webgui']['webguileftcolumnhyper']);
|
1850
|
$settings['webgui']['disablealiaspopupdetail'] = isset($config['system']['webgui']['disablealiaspopupdetail']);
|
1851
|
$settings['webgui']['systemlogsfilterpanel'] = isset($config['system']['webgui']['systemlogsfilterpanel']);
|
1852
|
$settings['webgui']['systemlogsmanagelogpanel'] = isset($config['system']['webgui']['systemlogsmanagelogpanel']);
|
1853
|
$settings['webgui']['statusmonitoringsettingspanel'] = isset($config['system']['webgui']['statusmonitoringsettingspanel']);
|
1854
|
$settings['webgui']['pagenamefirst'] = isset($config['system']['webgui']['pagenamefirst']);
|
1855
|
$user = getUserEntry($username);
|
1856
|
if (isset($user['customsettings'])) {
|
1857
|
$settings['customsettings'] = true;
|
1858
|
if (isset($user['widgets'])) {
|
1859
|
// This includes the 'sequence', and any widgetname-config per-widget settings.
|
1860
|
$settings['widgets'] = $user['widgets'];
|
1861
|
}
|
1862
|
if (isset($user['dashboardcolumns'])) {
|
1863
|
$settings['webgui']['dashboardcolumns'] = $user['dashboardcolumns'];
|
1864
|
}
|
1865
|
if (isset($user['webguicss'])) {
|
1866
|
$settings['webgui']['webguicss'] = $user['webguicss'];
|
1867
|
}
|
1868
|
if (isset($user['webguihostnamemenu'])) {
|
1869
|
$settings['webgui']['webguihostnamemenu'] = $user['webguihostnamemenu'];
|
1870
|
}
|
1871
|
$settings['webgui']['interfacessort'] = isset($user['interfacessort']);
|
1872
|
$settings['webgui']['dashboardavailablewidgetspanel'] = isset($user['dashboardavailablewidgetspanel']);
|
1873
|
$settings['webgui']['webguifixedmenu'] = isset($user['webguifixedmenu']);
|
1874
|
$settings['webgui']['webguileftcolumnhyper'] = isset($user['webguileftcolumnhyper']);
|
1875
|
$settings['webgui']['disablealiaspopupdetail'] = isset($user['disablealiaspopupdetail']);
|
1876
|
$settings['webgui']['systemlogsfilterpanel'] = isset($user['systemlogsfilterpanel']);
|
1877
|
$settings['webgui']['systemlogsmanagelogpanel'] = isset($user['systemlogsmanagelogpanel']);
|
1878
|
$settings['webgui']['statusmonitoringsettingspanel'] = isset($user['statusmonitoringsettingspanel']);
|
1879
|
$settings['webgui']['pagenamefirst'] = isset($user['pagenamefirst']);
|
1880
|
} else {
|
1881
|
$settings['customsettings'] = false;
|
1882
|
}
|
1883
|
|
1884
|
if ($settings['webgui']['dashboardcolumns'] < 1) {
|
1885
|
$settings['webgui']['dashboardcolumns'] = 2;
|
1886
|
}
|
1887
|
|
1888
|
return $settings;
|
1889
|
}
|
1890
|
|
1891
|
function save_widget_settings($username, $settings, $message = "") {
|
1892
|
global $config, $userindex;
|
1893
|
$user = getUserEntry($username);
|
1894
|
|
1895
|
if (strlen($message) > 0) {
|
1896
|
$msgout = $message;
|
1897
|
} else {
|
1898
|
$msgout = gettext("Widget configuration has been changed.");
|
1899
|
}
|
1900
|
|
1901
|
if (isset($user['customsettings'])) {
|
1902
|
$config['system']['user'][$userindex[$username]]['widgets'] = $settings;
|
1903
|
write_config($msgout . " " . sprintf(gettext("(User %s)"), $username));
|
1904
|
} else {
|
1905
|
$config['widgets'] = $settings;
|
1906
|
write_config($msgout);
|
1907
|
}
|
1908
|
}
|
1909
|
|
1910
|
function auth_get_authserver($name) {
|
1911
|
global $config;
|
1912
|
|
1913
|
if (is_array($config['system']['authserver'])) {
|
1914
|
foreach ($config['system']['authserver'] as $authcfg) {
|
1915
|
if ($authcfg['name'] == $name) {
|
1916
|
return $authcfg;
|
1917
|
}
|
1918
|
}
|
1919
|
}
|
1920
|
if ($name == "Local Database") {
|
1921
|
return array("name" => "Local Database", "type" => "Local Auth", "host" => $config['system']['hostname']);
|
1922
|
}
|
1923
|
}
|
1924
|
|
1925
|
function auth_get_authserver_list() {
|
1926
|
global $config;
|
1927
|
|
1928
|
$list = array();
|
1929
|
|
1930
|
if (is_array($config['system']['authserver'])) {
|
1931
|
foreach ($config['system']['authserver'] as $authcfg) {
|
1932
|
/* Add support for disabled entries? */
|
1933
|
$list[$authcfg['name']] = $authcfg;
|
1934
|
}
|
1935
|
}
|
1936
|
|
1937
|
$list["Local Database"] = array("name" => "Local Database", "type" => "Local Auth", "host" => $config['system']['hostname']);
|
1938
|
return $list;
|
1939
|
}
|
1940
|
|
1941
|
function getUserGroups($username, $authcfg, &$attributes = array()) {
|
1942
|
global $config;
|
1943
|
|
1944
|
$allowed_groups = array();
|
1945
|
|
1946
|
switch ($authcfg['type']) {
|
1947
|
case 'ldap':
|
1948
|
$allowed_groups = @ldap_get_groups($username, $authcfg);
|
1949
|
break;
|
1950
|
case 'radius':
|
1951
|
$allowed_groups = @radius_get_groups($attributes);
|
1952
|
break;
|
1953
|
default:
|
1954
|
$user = getUserEntry($username);
|
1955
|
$allowed_groups = @local_user_get_groups($user, true);
|
1956
|
break;
|
1957
|
}
|
1958
|
|
1959
|
$member_groups = array();
|
1960
|
if (is_array($config['system']['group'])) {
|
1961
|
foreach ($config['system']['group'] as $group) {
|
1962
|
if (in_array($group['name'], $allowed_groups)) {
|
1963
|
$member_groups[] = $group['name'];
|
1964
|
}
|
1965
|
}
|
1966
|
}
|
1967
|
|
1968
|
return $member_groups;
|
1969
|
}
|
1970
|
|
1971
|
/*
|
1972
|
Possible return values :
|
1973
|
true : authentication worked
|
1974
|
false : authentication failed (invalid login/password, not enough permission, etc...)
|
1975
|
null : error during authentication process (unable to reach remote server, etc...)
|
1976
|
*/
|
1977
|
function authenticate_user($username, $password, $authcfg = NULL, &$attributes = array()) {
|
1978
|
|
1979
|
if (is_array($username) || is_array($password)) {
|
1980
|
return false;
|
1981
|
}
|
1982
|
|
1983
|
if (!$authcfg) {
|
1984
|
return local_backed($username, $password, $attributes);
|
1985
|
}
|
1986
|
|
1987
|
$authenticated = false;
|
1988
|
switch ($authcfg['type']) {
|
1989
|
case 'ldap':
|
1990
|
try {
|
1991
|
$authenticated = ldap_backed($username, $password, $authcfg, $attributes);
|
1992
|
} catch (Exception $e) {
|
1993
|
log_error(sprintf(gettext("LDAP authentication error: %s"), $e->getMessage()));
|
1994
|
}
|
1995
|
break;
|
1996
|
|
1997
|
break;
|
1998
|
case 'radius':
|
1999
|
try {
|
2000
|
$authenticated = radius_backed($username, $password, $authcfg, $attributes);
|
2001
|
} catch (Exception $e) {
|
2002
|
log_error(sprintf(gettext("RADIUS authentication error: %s"), $e->getMessage()));
|
2003
|
}
|
2004
|
break;
|
2005
|
default:
|
2006
|
/* lookup user object by name */
|
2007
|
try {
|
2008
|
$authenticated = local_backed($username, $password, $attributes);
|
2009
|
} catch (Exception $e) {
|
2010
|
log_error(sprintf(gettext("Local authentication error: %s"), $e->getMessage()));
|
2011
|
}
|
2012
|
break;
|
2013
|
}
|
2014
|
|
2015
|
return $authenticated;
|
2016
|
}
|
2017
|
|
2018
|
function session_auth() {
|
2019
|
global $config, $_SESSION, $page;
|
2020
|
|
2021
|
// Handle HTTPS httponly and secure flags
|
2022
|
$currentCookieParams = session_get_cookie_params();
|
2023
|
session_set_cookie_params(
|
2024
|
$currentCookieParams["lifetime"],
|
2025
|
$currentCookieParams["path"],
|
2026
|
NULL,
|
2027
|
($config['system']['webgui']['protocol'] == "https"),
|
2028
|
true
|
2029
|
);
|
2030
|
|
2031
|
phpsession_begin();
|
2032
|
|
2033
|
// Detect protocol change
|
2034
|
if (!isset($_POST['login']) && !empty($_SESSION['Logged_In']) && $_SESSION['protocol'] != $config['system']['webgui']['protocol']) {
|
2035
|
phpsession_end();
|
2036
|
return false;
|
2037
|
}
|
2038
|
|
2039
|
/* Validate incoming login request */
|
2040
|
$attributes = array('nas_identifier' => 'webConfigurator-' . gethostname());
|
2041
|
if (isset($_POST['login']) && !empty($_POST['usernamefld'])) {
|
2042
|
$authcfg = auth_get_authserver($config['system']['webgui']['authmode']);
|
2043
|
$remoteauth = authenticate_user($_POST['usernamefld'], $_POST['passwordfld'], $authcfg, $attributes);
|
2044
|
if ($remoteauth || authenticate_user($_POST['usernamefld'], $_POST['passwordfld'])) {
|
2045
|
// Generate a new id to avoid session fixation
|
2046
|
session_regenerate_id();
|
2047
|
$_SESSION['Logged_In'] = "True";
|
2048
|
$_SESSION['remoteauth'] = $remoteauth;
|
2049
|
if ($remoteauth) {
|
2050
|
if (empty($authcfg['type']) || ($authcfg['type'] == "Local Auth")) {
|
2051
|
$_SESSION['authsource'] = "Local Database";
|
2052
|
} else {
|
2053
|
$_SESSION['authsource'] = strtoupper($authcfg['type']) . "/{$authcfg['name']}";
|
2054
|
}
|
2055
|
} else {
|
2056
|
$_SESSION['authsource'] = 'Local Database Fallback';
|
2057
|
}
|
2058
|
$_SESSION['Username'] = $_POST['usernamefld'];
|
2059
|
$_SESSION['user_radius_attributes'] = $attributes;
|
2060
|
$_SESSION['last_access'] = time();
|
2061
|
$_SESSION['protocol'] = $config['system']['webgui']['protocol'];
|
2062
|
phpsession_end(true);
|
2063
|
if (!isset($config['system']['webgui']['quietlogin'])) {
|
2064
|
log_auth(sprintf(gettext("Successful login for user '%1\$s' from: %2\$s"), $_POST['usernamefld'], get_user_remote_address() . get_user_remote_authsource()));
|
2065
|
}
|
2066
|
if (isset($_POST['postafterlogin'])) {
|
2067
|
return true;
|
2068
|
} else {
|
2069
|
if (empty($page)) {
|
2070
|
$page = "/";
|
2071
|
}
|
2072
|
header("Location: {$page}");
|
2073
|
}
|
2074
|
exit;
|
2075
|
} else {
|
2076
|
/* give the user an error message */
|
2077
|
$_SESSION['Login_Error'] = gettext("Username or Password incorrect");
|
2078
|
log_auth(sprintf(gettext("webConfigurator authentication error for user '%1\$s' from: %2\$s"), $_POST['usernamefld'], get_user_remote_address() . get_user_remote_authsource()));
|
2079
|
if (isAjax()) {
|
2080
|
echo "showajaxmessage('{$_SESSION['Login_Error']}');";
|
2081
|
return;
|
2082
|
}
|
2083
|
}
|
2084
|
}
|
2085
|
|
2086
|
/* Show login page if they aren't logged in */
|
2087
|
if (empty($_SESSION['Logged_In'])) {
|
2088
|
phpsession_end(true);
|
2089
|
return false;
|
2090
|
}
|
2091
|
|
2092
|
/* If session timeout isn't set, we don't mark sessions stale */
|
2093
|
if (!isset($config['system']['webgui']['session_timeout'])) {
|
2094
|
/* Default to 4 hour timeout if one is not set */
|
2095
|
if ($_SESSION['last_access'] < (time() - 14400)) {
|
2096
|
$_POST['logout'] = true;
|
2097
|
$_SESSION['Logout'] = true;
|
2098
|
} else {
|
2099
|
$_SESSION['last_access'] = time();
|
2100
|
}
|
2101
|
} else if (intval($config['system']['webgui']['session_timeout']) == 0) {
|
2102
|
/* only update if it wasn't ajax */
|
2103
|
if (!isAjax()) {
|
2104
|
$_SESSION['last_access'] = time();
|
2105
|
}
|
2106
|
} else {
|
2107
|
/* Check for stale session */
|
2108
|
if ($_SESSION['last_access'] < (time() - ($config['system']['webgui']['session_timeout'] * 60))) {
|
2109
|
$_POST['logout'] = true;
|
2110
|
$_SESSION['Logout'] = true;
|
2111
|
} else {
|
2112
|
/* only update if it wasn't ajax */
|
2113
|
if (!isAjax()) {
|
2114
|
$_SESSION['last_access'] = time();
|
2115
|
}
|
2116
|
}
|
2117
|
}
|
2118
|
|
2119
|
/* user hit the logout button */
|
2120
|
if (isset($_POST['logout'])) {
|
2121
|
|
2122
|
if ($_SESSION['Logout']) {
|
2123
|
log_error(sprintf(gettext("Session timed out for user '%1\$s' from: %2\$s"), $_SESSION['Username'], get_user_remote_address() . get_user_remote_authsource()));
|
2124
|
} else {
|
2125
|
log_error(sprintf(gettext("User logged out for user '%1\$s' from: %2\$s"), $_SESSION['Username'], get_user_remote_address() . get_user_remote_authsource()));
|
2126
|
}
|
2127
|
|
2128
|
/* wipe out $_SESSION */
|
2129
|
$_SESSION = array();
|
2130
|
|
2131
|
if (isset($_COOKIE[session_name()])) {
|
2132
|
setcookie(session_name(), '', time()-42000, '/');
|
2133
|
}
|
2134
|
|
2135
|
/* and destroy it */
|
2136
|
phpsession_destroy();
|
2137
|
|
2138
|
$scriptName = explode("/", $_SERVER["SCRIPT_FILENAME"]);
|
2139
|
$scriptElms = count($scriptName);
|
2140
|
$scriptName = $scriptName[$scriptElms-1];
|
2141
|
|
2142
|
if (isAjax()) {
|
2143
|
return false;
|
2144
|
}
|
2145
|
|
2146
|
/* redirect to page the user is on, it'll prompt them to login again */
|
2147
|
header("Location: {$scriptName}");
|
2148
|
|
2149
|
return false;
|
2150
|
}
|
2151
|
|
2152
|
/*
|
2153
|
* this is for debugging purpose if you do not want to use Ajax
|
2154
|
* to submit a HTML form. It basically disables the observation
|
2155
|
* of the submit event and hence does not trigger Ajax.
|
2156
|
*/
|
2157
|
if ($_REQUEST['disable_ajax']) {
|
2158
|
$_SESSION['NO_AJAX'] = "True";
|
2159
|
}
|
2160
|
|
2161
|
/*
|
2162
|
* Same to re-enable Ajax.
|
2163
|
*/
|
2164
|
if ($_REQUEST['enable_ajax']) {
|
2165
|
unset($_SESSION['NO_AJAX']);
|
2166
|
}
|
2167
|
phpsession_end(true);
|
2168
|
return true;
|
2169
|
}
|
2170
|
|
2171
|
function print_credit() {
|
2172
|
global $g;
|
2173
|
|
2174
|
return '<a target="_blank" href="https://pfsense.org">' . $g["product_name"] . '</a>' .
|
2175
|
gettext(' is developed and maintained by ') .
|
2176
|
'<a target="_blank" href="https://netgate.com">Netgate. </a>' . ' © ESF ' . $g["product_copyright_years"] .
|
2177
|
'<a target="_blank" href="https://pfsense.org/license">' .
|
2178
|
gettext(' View license.') . '</a>';
|
2179
|
}
|
2180
|
function get_user_remote_address() {
|
2181
|
$remote_address = $_SERVER['REMOTE_ADDR'];
|
2182
|
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
|
2183
|
$remote_address .= "[{$_SERVER['HTTP_CLIENT_IP']}]";
|
2184
|
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
2185
|
$remote_address .= "[{$_SERVER['HTTP_X_FORWARDED_FOR']}]";
|
2186
|
}
|
2187
|
return $remote_address;
|
2188
|
}
|
2189
|
function get_user_remote_authsource() {
|
2190
|
$authsource = "";
|
2191
|
if (!empty($_SESSION['authsource'])) {
|
2192
|
$authsource .= " ({$_SESSION['authsource']})";
|
2193
|
}
|
2194
|
return $authsource;
|
2195
|
}
|
2196
|
?>
|