1
|
<?php
|
2
|
/*
|
3
|
auth.inc
|
4
|
*/
|
5
|
/* ====================================================================
|
6
|
* Copyright (c) 2004-2015 Electric Sheep Fencing, LLC. All rights reserved.
|
7
|
* Copyright (c) 2005-2006 Bill Marquette <bill.marquette@gmail.com>
|
8
|
* Copyright (c) 2006 Paul Taylor <paultaylor@winn-dixie.com>
|
9
|
* Copyright (c) 2003-2006 Manuel Kasper <mk@neon1.net>
|
10
|
*
|
11
|
* Redistribution and use in source and binary forms, with or without modification,
|
12
|
* are permitted provided that the following conditions are met:
|
13
|
*
|
14
|
* 1. Redistributions of source code must retain the above copyright notice,
|
15
|
* this list of conditions and the following disclaimer.
|
16
|
*
|
17
|
* 2. Redistributions in binary form must reproduce the above copyright
|
18
|
* notice, this list of conditions and the following disclaimer in
|
19
|
* the documentation and/or other materials provided with the
|
20
|
* distribution.
|
21
|
*
|
22
|
* 3. All advertising materials mentioning features or use of this software
|
23
|
* must display the following acknowledgment:
|
24
|
* "This product includes software developed by the pfSense Project
|
25
|
* for use in the pfSense software distribution. (http://www.pfsense.org/).
|
26
|
*
|
27
|
* 4. The names "pfSense" and "pfSense Project" must not be used to
|
28
|
* endorse or promote products derived from this software without
|
29
|
* prior written permission. For written permission, please contact
|
30
|
* coreteam@pfsense.org.
|
31
|
*
|
32
|
* 5. Products derived from this software may not be called "pfSense"
|
33
|
* nor may "pfSense" appear in their names without prior written
|
34
|
* permission of the Electric Sheep Fencing, LLC.
|
35
|
*
|
36
|
* 6. Redistributions of any form whatsoever must retain the following
|
37
|
* acknowledgment:
|
38
|
*
|
39
|
* "This product includes software developed by the pfSense Project
|
40
|
* for use in the pfSense software distribution (http://www.pfsense.org/).
|
41
|
*
|
42
|
* THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
|
43
|
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
44
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
45
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
|
46
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
47
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
48
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
49
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
50
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
51
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
52
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
53
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
54
|
*
|
55
|
* ====================================================================
|
56
|
*
|
57
|
*/
|
58
|
/*
|
59
|
* NOTE : Portions of the mschapv2 support was based on the BSD licensed CHAP.php
|
60
|
* file courtesy of Michael Retterklieber.
|
61
|
*/
|
62
|
if (!$do_not_include_config_gui_inc) {
|
63
|
require_once("config.gui.inc");
|
64
|
}
|
65
|
|
66
|
// Will be changed to false if security checks fail
|
67
|
$security_passed = true;
|
68
|
|
69
|
/* If this function doesn't exist, we're being called from Captive Portal or
|
70
|
another internal subsystem which does not include authgui.inc */
|
71
|
if (function_exists("display_error_form") && !isset($config['system']['webgui']['nodnsrebindcheck'])) {
|
72
|
/* DNS ReBinding attack prevention. https://redmine.pfsense.org/issues/708 */
|
73
|
$found_host = false;
|
74
|
|
75
|
/* Either a IPv6 address with or without a alternate port */
|
76
|
if (strstr($_SERVER['HTTP_HOST'], "]")) {
|
77
|
$http_host_port = explode("]", $_SERVER['HTTP_HOST']);
|
78
|
/* v6 address has more parts, drop the last part */
|
79
|
if (count($http_host_port) > 1) {
|
80
|
array_pop($http_host_port);
|
81
|
$http_host = str_replace(array("[", "]"), "", implode(":", $http_host_port));
|
82
|
} else {
|
83
|
$http_host = str_replace(array("[", "]"), "", implode(":", $http_host_port));
|
84
|
}
|
85
|
} else {
|
86
|
$http_host = explode(":", $_SERVER['HTTP_HOST']);
|
87
|
$http_host = $http_host[0];
|
88
|
}
|
89
|
if (is_ipaddr($http_host) or $_SERVER['SERVER_ADDR'] == "127.0.0.1" or
|
90
|
strcasecmp($http_host, "localhost") == 0 or $_SERVER['SERVER_ADDR'] == "::1") {
|
91
|
$found_host = true;
|
92
|
}
|
93
|
if (strcasecmp($http_host, $config['system']['hostname'] . "." . $config['system']['domain']) == 0 or
|
94
|
strcasecmp($http_host, $config['system']['hostname']) == 0) {
|
95
|
$found_host = true;
|
96
|
}
|
97
|
|
98
|
if (is_array($config['dyndnses']['dyndns']) && !$found_host) {
|
99
|
foreach ($config['dyndnses']['dyndns'] as $dyndns) {
|
100
|
if (strcasecmp($dyndns['host'], $http_host) == 0) {
|
101
|
$found_host = true;
|
102
|
break;
|
103
|
}
|
104
|
}
|
105
|
}
|
106
|
|
107
|
if (is_array($config['dnsupdates']['dnsupdate']) && !$found_host) {
|
108
|
foreach ($config['dnsupdates']['dnsupdate'] as $rfc2136) {
|
109
|
if (strcasecmp($rfc2136['host'], $http_host) == 0) {
|
110
|
$found_host = true;
|
111
|
break;
|
112
|
}
|
113
|
}
|
114
|
}
|
115
|
|
116
|
if (!empty($config['system']['webgui']['althostnames']) && !$found_host) {
|
117
|
$althosts = explode(" ", $config['system']['webgui']['althostnames']);
|
118
|
foreach ($althosts as $ah) {
|
119
|
if (strcasecmp($ah, $http_host) == 0 or strcasecmp($ah, $_SERVER['SERVER_ADDR']) == 0) {
|
120
|
$found_host = true;
|
121
|
break;
|
122
|
}
|
123
|
}
|
124
|
}
|
125
|
|
126
|
if ($found_host == false) {
|
127
|
if (!security_checks_disabled()) {
|
128
|
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."));
|
129
|
exit;
|
130
|
}
|
131
|
$security_passed = false;
|
132
|
}
|
133
|
}
|
134
|
|
135
|
// If the HTTP_REFERER is something other than ourselves then disallow.
|
136
|
if (function_exists("display_error_form") && !isset($config['system']['webgui']['nohttpreferercheck'])) {
|
137
|
if ($_SERVER['HTTP_REFERER']) {
|
138
|
if (file_exists("{$g['tmp_path']}/setupwizard_lastreferrer")) {
|
139
|
if ($_SERVER['HTTP_REFERER'] == file_get_contents("{$g['tmp_path']}/setupwizard_lastreferrer")) {
|
140
|
unlink("{$g['tmp_path']}/setupwizard_lastreferrer");
|
141
|
header("Refresh: 1; url=index.php");
|
142
|
?>
|
143
|
<!DOCTYPE html>
|
144
|
<html lang="en">
|
145
|
<head>
|
146
|
<link rel="stylesheet" href="/css/pfSense.css" />
|
147
|
<title><?=gettext("Redirecting..."); ?></title>
|
148
|
</head>
|
149
|
<body id="error" class="no-menu">
|
150
|
<div id="jumbotron">
|
151
|
<div class="container">
|
152
|
<div class="col-sm-offset-3 col-sm-6 col-xs-12">
|
153
|
<p><?=gettext("Redirecting to the dashboard...")?></p>
|
154
|
</div>
|
155
|
</div>
|
156
|
</div>
|
157
|
</body>
|
158
|
</html>
|
159
|
<?php
|
160
|
exit;
|
161
|
}
|
162
|
}
|
163
|
$found_host = false;
|
164
|
$referrer_host = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
|
165
|
$referrer_host = str_replace(array("[", "]"), "", $referrer_host);
|
166
|
if ($referrer_host) {
|
167
|
if (strcasecmp($referrer_host, $config['system']['hostname'] . "." . $config['system']['domain']) == 0 ||
|
168
|
strcasecmp($referrer_host, $config['system']['hostname']) == 0) {
|
169
|
$found_host = true;
|
170
|
}
|
171
|
|
172
|
if (!empty($config['system']['webgui']['althostnames']) && !$found_host) {
|
173
|
$althosts = explode(" ", $config['system']['webgui']['althostnames']);
|
174
|
foreach ($althosts as $ah) {
|
175
|
if (strcasecmp($referrer_host, $ah) == 0) {
|
176
|
$found_host = true;
|
177
|
break;
|
178
|
}
|
179
|
}
|
180
|
}
|
181
|
|
182
|
if (is_array($config['dyndnses']['dyndns']) && !$found_host) {
|
183
|
foreach ($config['dyndnses']['dyndns'] as $dyndns) {
|
184
|
if (strcasecmp($dyndns['host'], $referrer_host) == 0) {
|
185
|
$found_host = true;
|
186
|
break;
|
187
|
}
|
188
|
}
|
189
|
}
|
190
|
|
191
|
if (is_array($config['dnsupdates']['dnsupdate']) && !$found_host) {
|
192
|
foreach ($config['dnsupdates']['dnsupdate'] as $rfc2136) {
|
193
|
if (strcasecmp($rfc2136['host'], $referrer_host) == 0) {
|
194
|
$found_host = true;
|
195
|
break;
|
196
|
}
|
197
|
}
|
198
|
}
|
199
|
|
200
|
if (!$found_host) {
|
201
|
$interface_list_ips = get_configured_ip_addresses();
|
202
|
foreach ($interface_list_ips as $ilips) {
|
203
|
if (strcasecmp($referrer_host, $ilips) == 0) {
|
204
|
$found_host = true;
|
205
|
break;
|
206
|
}
|
207
|
}
|
208
|
$interface_list_ipv6s = get_configured_ipv6_addresses();
|
209
|
foreach ($interface_list_ipv6s as $ilipv6s) {
|
210
|
if (strcasecmp($referrer_host, $ilipv6s) == 0) {
|
211
|
$found_host = true;
|
212
|
break;
|
213
|
}
|
214
|
}
|
215
|
if ($referrer_host == "127.0.0.1" || $referrer_host == "localhost") {
|
216
|
// allow SSH port forwarded connections and links from localhost
|
217
|
$found_host = true;
|
218
|
}
|
219
|
}
|
220
|
}
|
221
|
if ($found_host == false) {
|
222
|
if (!security_checks_disabled()) {
|
223
|
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.");
|
224
|
exit;
|
225
|
}
|
226
|
$security_passed = false;
|
227
|
}
|
228
|
} else {
|
229
|
$security_passed = false;
|
230
|
}
|
231
|
}
|
232
|
|
233
|
if (function_exists("display_error_form") && $security_passed) {
|
234
|
/* Security checks passed, so it should be OK to turn them back on */
|
235
|
restore_security_checks();
|
236
|
}
|
237
|
unset($security_passed);
|
238
|
|
239
|
$groupindex = index_groups();
|
240
|
$userindex = index_users();
|
241
|
|
242
|
function index_groups() {
|
243
|
global $g, $debug, $config, $groupindex;
|
244
|
|
245
|
$groupindex = array();
|
246
|
|
247
|
if (is_array($config['system']['group'])) {
|
248
|
$i = 0;
|
249
|
foreach ($config['system']['group'] as $groupent) {
|
250
|
$groupindex[$groupent['name']] = $i;
|
251
|
$i++;
|
252
|
}
|
253
|
}
|
254
|
|
255
|
return ($groupindex);
|
256
|
}
|
257
|
|
258
|
function index_users() {
|
259
|
global $g, $debug, $config;
|
260
|
|
261
|
if (is_array($config['system']['user'])) {
|
262
|
$i = 0;
|
263
|
foreach ($config['system']['user'] as $userent) {
|
264
|
$userindex[$userent['name']] = $i;
|
265
|
$i++;
|
266
|
}
|
267
|
}
|
268
|
|
269
|
return ($userindex);
|
270
|
}
|
271
|
|
272
|
function & getUserEntry($name) {
|
273
|
global $debug, $config, $userindex;
|
274
|
$authcfg = auth_get_authserver($config['system']['webgui']['authmode']);
|
275
|
|
276
|
if (isset($userindex[$name])) {
|
277
|
return $config['system']['user'][$userindex[$name]];
|
278
|
} elseif ($authcfg['type'] != "Local Database") {
|
279
|
$user = array();
|
280
|
$user['name'] = $name;
|
281
|
return $user;
|
282
|
}
|
283
|
}
|
284
|
|
285
|
function & getUserEntryByUID($uid) {
|
286
|
global $debug, $config;
|
287
|
|
288
|
if (is_array($config['system']['user'])) {
|
289
|
foreach ($config['system']['user'] as & $user) {
|
290
|
if ($user['uid'] == $uid) {
|
291
|
return $user;
|
292
|
}
|
293
|
}
|
294
|
}
|
295
|
|
296
|
return false;
|
297
|
}
|
298
|
|
299
|
function & getGroupEntry($name) {
|
300
|
global $debug, $config, $groupindex;
|
301
|
if (isset($groupindex[$name])) {
|
302
|
return $config['system']['group'][$groupindex[$name]];
|
303
|
}
|
304
|
}
|
305
|
|
306
|
function & getGroupEntryByGID($gid) {
|
307
|
global $debug, $config;
|
308
|
|
309
|
if (is_array($config['system']['group'])) {
|
310
|
foreach ($config['system']['group'] as & $group) {
|
311
|
if ($group['gid'] == $gid) {
|
312
|
return $group;
|
313
|
}
|
314
|
}
|
315
|
}
|
316
|
|
317
|
return false;
|
318
|
}
|
319
|
|
320
|
function get_user_privileges(& $user) {
|
321
|
global $config;
|
322
|
|
323
|
$authcfg = auth_get_authserver($config['system']['webgui']['authmode']);
|
324
|
$names = array();
|
325
|
|
326
|
$privs = $user['priv'];
|
327
|
if (!is_array($privs)) {
|
328
|
$privs = array();
|
329
|
}
|
330
|
|
331
|
if ($authcfg['type'] == "ldap") {
|
332
|
$names = @ldap_get_groups($user['name'], $authcfg);
|
333
|
} elseif ($authcfg['type'] == "radius") {
|
334
|
$names = @radius_get_groups($_SESSION['user_radius_attributes']);
|
335
|
}
|
336
|
|
337
|
if (empty($names)) {
|
338
|
$names = local_user_get_groups($user, true);
|
339
|
}
|
340
|
|
341
|
foreach ($names as $name) {
|
342
|
$group = getGroupEntry($name);
|
343
|
if (is_array($group['priv'])) {
|
344
|
$privs = array_merge($privs, $group['priv']);
|
345
|
}
|
346
|
}
|
347
|
|
348
|
return $privs;
|
349
|
}
|
350
|
|
351
|
function userHasPrivilege($userent, $privid = false) {
|
352
|
|
353
|
if (!$privid || !is_array($userent)) {
|
354
|
return false;
|
355
|
}
|
356
|
|
357
|
$privs = get_user_privileges($userent);
|
358
|
|
359
|
if (!is_array($privs)) {
|
360
|
return false;
|
361
|
}
|
362
|
|
363
|
if (!in_array($privid, $privs)) {
|
364
|
return false;
|
365
|
}
|
366
|
|
367
|
return true;
|
368
|
}
|
369
|
|
370
|
function local_backed($username, $passwd) {
|
371
|
|
372
|
$user = getUserEntry($username);
|
373
|
if (!$user) {
|
374
|
return false;
|
375
|
}
|
376
|
|
377
|
if (is_account_disabled($username) || is_account_expired($username)) {
|
378
|
return false;
|
379
|
}
|
380
|
|
381
|
if ($user['bcrypt-hash']) {
|
382
|
if (password_verify($passwd, $user['bcrypt-hash'])) {
|
383
|
return true;
|
384
|
}
|
385
|
}
|
386
|
|
387
|
//for backwards compatibility
|
388
|
if ($user['password']) {
|
389
|
if (crypt($passwd, $user['password']) == $user['password']) {
|
390
|
return true;
|
391
|
}
|
392
|
}
|
393
|
|
394
|
if ($user['md5-hash']) {
|
395
|
if (md5($passwd) == $user['md5-hash']) {
|
396
|
return true;
|
397
|
}
|
398
|
}
|
399
|
|
400
|
return false;
|
401
|
}
|
402
|
|
403
|
function local_sync_accounts() {
|
404
|
global $debug, $config;
|
405
|
conf_mount_rw();
|
406
|
|
407
|
/* remove local users to avoid uid conflicts */
|
408
|
$fd = popen("/usr/sbin/pw usershow -a", "r");
|
409
|
if ($fd) {
|
410
|
while (!feof($fd)) {
|
411
|
$line = explode(":", fgets($fd));
|
412
|
if ($line[0] != "admin") {
|
413
|
if (!strncmp($line[0], "_", 1)) {
|
414
|
continue;
|
415
|
}
|
416
|
if ($line[2] < 2000) {
|
417
|
continue;
|
418
|
}
|
419
|
if ($line[2] > 65000) {
|
420
|
continue;
|
421
|
}
|
422
|
}
|
423
|
/*
|
424
|
* If a crontab was created to user, pw userdel will be interactive and
|
425
|
* can cause issues. Just remove crontab before run it when necessary
|
426
|
*/
|
427
|
unlink_if_exists("/var/cron/tabs/{$line[0]}");
|
428
|
$cmd = "/usr/sbin/pw userdel -n " . escapeshellarg($line[0]);
|
429
|
if ($debug) {
|
430
|
log_error(sprintf(gettext("Running: %s"), $cmd));
|
431
|
}
|
432
|
mwexec($cmd);
|
433
|
}
|
434
|
pclose($fd);
|
435
|
}
|
436
|
|
437
|
/* remove local groups to avoid gid conflicts */
|
438
|
$gids = array();
|
439
|
$fd = popen("/usr/sbin/pw groupshow -a", "r");
|
440
|
if ($fd) {
|
441
|
while (!feof($fd)) {
|
442
|
$line = explode(":", fgets($fd));
|
443
|
if (!strncmp($line[0], "_", 1)) {
|
444
|
continue;
|
445
|
}
|
446
|
if ($line[2] < 2000) {
|
447
|
continue;
|
448
|
}
|
449
|
if ($line[2] > 65000) {
|
450
|
continue;
|
451
|
}
|
452
|
$cmd = "/usr/sbin/pw groupdel -g " . escapeshellarg($line[2]);
|
453
|
if ($debug) {
|
454
|
log_error(sprintf(gettext("Running: %s"), $cmd));
|
455
|
}
|
456
|
mwexec($cmd);
|
457
|
}
|
458
|
pclose($fd);
|
459
|
}
|
460
|
|
461
|
/* make sure the all group exists */
|
462
|
$allgrp = getGroupEntryByGID(1998);
|
463
|
local_group_set($allgrp, true);
|
464
|
|
465
|
/* sync all local users */
|
466
|
if (is_array($config['system']['user'])) {
|
467
|
foreach ($config['system']['user'] as $user) {
|
468
|
local_user_set($user);
|
469
|
}
|
470
|
}
|
471
|
|
472
|
/* sync all local groups */
|
473
|
if (is_array($config['system']['group'])) {
|
474
|
foreach ($config['system']['group'] as $group) {
|
475
|
local_group_set($group);
|
476
|
}
|
477
|
}
|
478
|
|
479
|
conf_mount_ro();
|
480
|
|
481
|
}
|
482
|
|
483
|
function local_user_set(& $user) {
|
484
|
global $g, $debug;
|
485
|
|
486
|
if (empty($user['password']) && empty($user['bcrypt-hash'])) {
|
487
|
log_error("There is something wrong in the config because user {$user['name']} password is missing!");
|
488
|
return;
|
489
|
}
|
490
|
|
491
|
conf_mount_rw();
|
492
|
|
493
|
$home_base = "/home/";
|
494
|
$user_uid = $user['uid'];
|
495
|
$user_name = $user['name'];
|
496
|
$user_home = "{$home_base}{$user_name}";
|
497
|
$user_shell = "/etc/rc.initial";
|
498
|
$user_group = "nobody";
|
499
|
|
500
|
// Ensure $home_base exists and is writable
|
501
|
if (!is_dir($home_base)) {
|
502
|
mkdir($home_base, 0755);
|
503
|
}
|
504
|
|
505
|
$lock_account = false;
|
506
|
/* configure shell type */
|
507
|
/* Cases here should be ordered by most privileged to least privileged. */
|
508
|
if (userHasPrivilege($user, "user-shell-access") || userHasPrivilege($user, "page-all")) {
|
509
|
$user_shell = "/bin/tcsh";
|
510
|
} elseif (userHasPrivilege($user, "user-copy-files")) {
|
511
|
$user_shell = "/usr/local/bin/scponly";
|
512
|
} elseif (userHasPrivilege($user, "user-ssh-tunnel")) {
|
513
|
$user_shell = "/usr/local/sbin/ssh_tunnel_shell";
|
514
|
} elseif (userHasPrivilege($user, "user-ipsec-xauth-dialin")) {
|
515
|
$user_shell = "/sbin/nologin";
|
516
|
} else {
|
517
|
$user_shell = "/sbin/nologin";
|
518
|
$lock_account = true;
|
519
|
}
|
520
|
|
521
|
/* Lock out disabled or expired users, unless it's root/admin. */
|
522
|
if ((is_account_disabled($user_name) || is_account_expired($user_name)) && ($user_uid != 0)) {
|
523
|
$user_shell = "/sbin/nologin";
|
524
|
$lock_account = true;
|
525
|
}
|
526
|
|
527
|
/* root user special handling */
|
528
|
if ($user_uid == 0) {
|
529
|
$cmd = "/usr/sbin/pw usermod -q -n root -s /bin/sh -H 0";
|
530
|
if ($debug) {
|
531
|
log_error(sprintf(gettext("Running: %s"), $cmd));
|
532
|
}
|
533
|
$fd = popen($cmd, "w");
|
534
|
if (empty($user['bcrypt-hash'])) {
|
535
|
fwrite($fd, $user['password']);
|
536
|
} else {
|
537
|
fwrite($fd, $user['bcrypt-hash']);
|
538
|
}
|
539
|
pclose($fd);
|
540
|
$user_group = "wheel";
|
541
|
$user_home = "/root";
|
542
|
$user_shell = "/etc/rc.initial";
|
543
|
}
|
544
|
|
545
|
/* read from pw db */
|
546
|
$fd = popen("/usr/sbin/pw usershow -n {$user_name} 2>&1", "r");
|
547
|
$pwread = fgets($fd);
|
548
|
pclose($fd);
|
549
|
$userattrs = explode(":", trim($pwread));
|
550
|
|
551
|
$skel_dir = '/etc/skel';
|
552
|
|
553
|
/* determine add or mod */
|
554
|
if (($userattrs[0] != $user['name']) || (!strncmp($pwread, "pw:", 3))) {
|
555
|
$user_op = "useradd -m -k " . escapeshellarg($skel_dir) . " -o";
|
556
|
} else {
|
557
|
$user_op = "usermod";
|
558
|
}
|
559
|
|
560
|
$comment = str_replace(array(":", "!", "@"), " ", $user['descr']);
|
561
|
/* add or mod pw db */
|
562
|
$cmd = "/usr/sbin/pw {$user_op} -q " .
|
563
|
" -u " . escapeshellarg($user_uid) .
|
564
|
" -n " . escapeshellarg($user_name) .
|
565
|
" -g " . escapeshellarg($user_group) .
|
566
|
" -s " . escapeshellarg($user_shell) .
|
567
|
" -d " . escapeshellarg($user_home) .
|
568
|
" -c " . escapeshellarg($comment) .
|
569
|
" -H 0 2>&1";
|
570
|
|
571
|
if ($debug) {
|
572
|
log_error(sprintf(gettext("Running: %s"), $cmd));
|
573
|
}
|
574
|
$fd = popen($cmd, "w");
|
575
|
if (empty($user['bcrypt-hash'])) {
|
576
|
fwrite($fd, $user['password']);
|
577
|
} else {
|
578
|
fwrite($fd, $user['bcrypt-hash']);
|
579
|
}
|
580
|
pclose($fd);
|
581
|
|
582
|
/* create user directory if required */
|
583
|
if (!is_dir($user_home)) {
|
584
|
mkdir($user_home, 0700);
|
585
|
}
|
586
|
@chown($user_home, $user_name);
|
587
|
@chgrp($user_home, $user_group);
|
588
|
|
589
|
/* Make sure all users have last version of config files */
|
590
|
foreach (glob("{$skel_dir}/dot.*") as $dot_file) {
|
591
|
$target = $user_home . '/' . substr(basename($dot_file), 3);
|
592
|
@copy($dot_file, $target);
|
593
|
@chown($target, $user_name);
|
594
|
@chgrp($target, $user_group);
|
595
|
}
|
596
|
|
597
|
/* write out ssh authorized key file */
|
598
|
if ($user['authorizedkeys']) {
|
599
|
if (!is_dir("{$user_home}/.ssh")) {
|
600
|
@mkdir("{$user_home}/.ssh", 0700);
|
601
|
@chown("{$user_home}/.ssh", $user_name);
|
602
|
}
|
603
|
$keys = base64_decode($user['authorizedkeys']);
|
604
|
@file_put_contents("{$user_home}/.ssh/authorized_keys", $keys);
|
605
|
@chown("{$user_home}/.ssh/authorized_keys", $user_name);
|
606
|
} else {
|
607
|
unlink_if_exists("{$user_home}/.ssh/authorized_keys");
|
608
|
}
|
609
|
|
610
|
$un = $lock_account ? "" : "un";
|
611
|
exec("/usr/sbin/pw {$un}lock " . escapeshellarg($user_name) . " -q 2>/dev/null");
|
612
|
|
613
|
conf_mount_ro();
|
614
|
}
|
615
|
|
616
|
function local_user_del($user) {
|
617
|
global $debug;
|
618
|
|
619
|
/* remove all memberships */
|
620
|
local_user_set_groups($user);
|
621
|
|
622
|
/* Don't remove /root */
|
623
|
if ($user['uid'] != 0) {
|
624
|
$rmhome = "-r";
|
625
|
}
|
626
|
|
627
|
/* read from pw db */
|
628
|
$fd = popen("/usr/sbin/pw usershow -n {$user['name']} 2>&1", "r");
|
629
|
$pwread = fgets($fd);
|
630
|
pclose($fd);
|
631
|
$userattrs = explode(":", trim($pwread));
|
632
|
|
633
|
if ($userattrs[0] != $user['name']) {
|
634
|
log_error("Tried to remove user {$user['name']} but got user {$userattrs[0]} instead. Bailing.");
|
635
|
return;
|
636
|
}
|
637
|
|
638
|
/* delete from pw db */
|
639
|
$cmd = "/usr/sbin/pw userdel -n " . escapeshellarg($user['name']) . " " . escapeshellarg($rmhome);
|
640
|
|
641
|
if ($debug) {
|
642
|
log_error(sprintf(gettext("Running: %s"), $cmd));
|
643
|
}
|
644
|
mwexec($cmd);
|
645
|
|
646
|
/* Delete user from groups needs a call to write_config() */
|
647
|
local_group_del_user($user);
|
648
|
}
|
649
|
|
650
|
function local_user_set_password(&$user, $password) {
|
651
|
|
652
|
unset($user['password']);
|
653
|
unset($user['md5-hash']);
|
654
|
$user['bcrypt-hash'] = password_hash($password, PASSWORD_BCRYPT);
|
655
|
|
656
|
/* Maintain compatibility with FreeBSD - change $2y$ prefix to $2b$
|
657
|
* https://reviews.freebsd.org/D2742
|
658
|
* XXX: Can be removed as soon as r284483 is MFC'd.
|
659
|
*/
|
660
|
if ($user['bcrypt-hash'][2] == "y") {
|
661
|
$user['bcrypt-hash'][2] = "b";
|
662
|
}
|
663
|
|
664
|
// Converts ascii to unicode.
|
665
|
$astr = (string) $password;
|
666
|
$ustr = '';
|
667
|
for ($i = 0; $i < strlen($astr); $i++) {
|
668
|
$a = ord($astr{$i}) << 8;
|
669
|
$ustr .= sprintf("%X", $a);
|
670
|
}
|
671
|
|
672
|
}
|
673
|
|
674
|
function local_user_get_groups($user, $all = false) {
|
675
|
global $debug, $config;
|
676
|
|
677
|
$groups = array();
|
678
|
if (!is_array($config['system']['group'])) {
|
679
|
return $groups;
|
680
|
}
|
681
|
|
682
|
foreach ($config['system']['group'] as $group) {
|
683
|
if ($all || (!$all && ($group['name'] != "all"))) {
|
684
|
if (is_array($group['member'])) {
|
685
|
if (in_array($user['uid'], $group['member'])) {
|
686
|
$groups[] = $group['name'];
|
687
|
}
|
688
|
}
|
689
|
}
|
690
|
}
|
691
|
|
692
|
if ($all) {
|
693
|
$groups[] = "all";
|
694
|
}
|
695
|
|
696
|
sort($groups);
|
697
|
|
698
|
return $groups;
|
699
|
|
700
|
}
|
701
|
|
702
|
function local_user_set_groups($user, $new_groups = NULL) {
|
703
|
global $debug, $config, $groupindex;
|
704
|
|
705
|
if (!is_array($config['system']['group'])) {
|
706
|
return;
|
707
|
}
|
708
|
|
709
|
$cur_groups = local_user_get_groups($user, true);
|
710
|
$mod_groups = array();
|
711
|
|
712
|
if (!is_array($new_groups)) {
|
713
|
$new_groups = array();
|
714
|
}
|
715
|
|
716
|
if (!is_array($cur_groups)) {
|
717
|
$cur_groups = array();
|
718
|
}
|
719
|
|
720
|
/* determine which memberships to add */
|
721
|
foreach ($new_groups as $groupname) {
|
722
|
if ($groupname == '' || in_array($groupname, $cur_groups)) {
|
723
|
continue;
|
724
|
}
|
725
|
$group = & $config['system']['group'][$groupindex[$groupname]];
|
726
|
$group['member'][] = $user['uid'];
|
727
|
$mod_groups[] = $group;
|
728
|
}
|
729
|
unset($group);
|
730
|
|
731
|
/* determine which memberships to remove */
|
732
|
foreach ($cur_groups as $groupname) {
|
733
|
if (in_array($groupname, $new_groups)) {
|
734
|
continue;
|
735
|
}
|
736
|
if (!isset($config['system']['group'][$groupindex[$groupname]])) {
|
737
|
continue;
|
738
|
}
|
739
|
$group = & $config['system']['group'][$groupindex[$groupname]];
|
740
|
if (is_array($group['member'])) {
|
741
|
$index = array_search($user['uid'], $group['member']);
|
742
|
array_splice($group['member'], $index, 1);
|
743
|
$mod_groups[] = $group;
|
744
|
}
|
745
|
}
|
746
|
unset($group);
|
747
|
|
748
|
/* sync all modified groups */
|
749
|
foreach ($mod_groups as $group) {
|
750
|
local_group_set($group);
|
751
|
}
|
752
|
}
|
753
|
|
754
|
function local_group_del_user($user) {
|
755
|
global $config;
|
756
|
|
757
|
if (!is_array($config['system']['group'])) {
|
758
|
return;
|
759
|
}
|
760
|
|
761
|
foreach ($config['system']['group'] as $group) {
|
762
|
if (is_array($group['member'])) {
|
763
|
foreach ($group['member'] as $idx => $uid) {
|
764
|
if ($user['uid'] == $uid) {
|
765
|
unset($config['system']['group']['member'][$idx]);
|
766
|
}
|
767
|
}
|
768
|
}
|
769
|
}
|
770
|
}
|
771
|
|
772
|
function local_group_set($group, $reset = false) {
|
773
|
global $debug;
|
774
|
|
775
|
$group_name = $group['name'];
|
776
|
$group_gid = $group['gid'];
|
777
|
$group_members = '';
|
778
|
if (!$reset && !empty($group['member']) && count($group['member']) > 0) {
|
779
|
$group_members = implode(",", $group['member']);
|
780
|
}
|
781
|
|
782
|
if (empty($group_name) || $group['scope'] == "remote") {
|
783
|
return;
|
784
|
}
|
785
|
|
786
|
/* determine add or mod */
|
787
|
if (mwexec("/usr/sbin/pw groupshow -g " . escapeshellarg($group_gid) . " 2>&1", true) == 0) {
|
788
|
$group_op = "groupmod -l";
|
789
|
} else {
|
790
|
$group_op = "groupadd -n";
|
791
|
}
|
792
|
|
793
|
/* add or mod group db */
|
794
|
$cmd = "/usr/sbin/pw {$group_op} " .
|
795
|
escapeshellarg($group_name) .
|
796
|
" -g " . escapeshellarg($group_gid) .
|
797
|
" -M " . escapeshellarg($group_members) . " 2>&1";
|
798
|
|
799
|
if ($debug) {
|
800
|
log_error(sprintf(gettext("Running: %s"), $cmd));
|
801
|
}
|
802
|
mwexec($cmd);
|
803
|
|
804
|
}
|
805
|
|
806
|
function local_group_del($group) {
|
807
|
global $debug;
|
808
|
|
809
|
/* delete from group db */
|
810
|
$cmd = "/usr/sbin/pw groupdel " . escapeshellarg($group['name']);
|
811
|
|
812
|
if ($debug) {
|
813
|
log_error(sprintf(gettext("Running: %s"), $cmd));
|
814
|
}
|
815
|
mwexec($cmd);
|
816
|
}
|
817
|
|
818
|
function ldap_test_connection($authcfg) {
|
819
|
global $debug, $config, $g;
|
820
|
|
821
|
if ($authcfg) {
|
822
|
if (strstr($authcfg['ldap_urltype'], "Standard")) {
|
823
|
$ldapproto = "ldap";
|
824
|
} else {
|
825
|
$ldapproto = "ldaps";
|
826
|
}
|
827
|
$ldapserver = "{$ldapproto}://" . ldap_format_host($authcfg['host']);
|
828
|
$ldapport = $authcfg['ldap_port'];
|
829
|
if (!empty($ldapport)) {
|
830
|
$ldapserver .= ":{$ldapport}";
|
831
|
}
|
832
|
$ldapbasedn = $authcfg['ldap_basedn'];
|
833
|
$ldapbindun = $authcfg['ldap_binddn'];
|
834
|
$ldapbindpw = $authcfg['ldap_bindpw'];
|
835
|
} else {
|
836
|
return false;
|
837
|
}
|
838
|
|
839
|
/* first check if there is even an LDAP server populated */
|
840
|
if (!$ldapserver) {
|
841
|
return false;
|
842
|
}
|
843
|
|
844
|
/* Setup CA environment if needed. */
|
845
|
ldap_setup_caenv($authcfg);
|
846
|
|
847
|
/* connect and see if server is up */
|
848
|
$error = false;
|
849
|
if (!($ldap = ldap_connect($ldapserver))) {
|
850
|
$error = true;
|
851
|
}
|
852
|
|
853
|
if ($error == true) {
|
854
|
log_error(sprintf(gettext("ERROR! Could not connect to server %s."), $ldapname));
|
855
|
return false;
|
856
|
}
|
857
|
|
858
|
return true;
|
859
|
}
|
860
|
|
861
|
function ldap_setup_caenv($authcfg) {
|
862
|
global $g;
|
863
|
require_once("certs.inc");
|
864
|
|
865
|
unset($caref);
|
866
|
if (empty($authcfg['ldap_caref']) || !strstr($authcfg['ldap_urltype'], "SSL")) {
|
867
|
putenv('LDAPTLS_REQCERT=never');
|
868
|
return;
|
869
|
} else {
|
870
|
$caref = lookup_ca($authcfg['ldap_caref']);
|
871
|
if (!$caref) {
|
872
|
log_error(sprintf(gettext("LDAP: Could not lookup CA by reference for host %s."), $authcfg['ldap_caref']));
|
873
|
/* XXX: Prevent for credential leaking since we cannot setup the CA env. Better way? */
|
874
|
putenv('LDAPTLS_REQCERT=hard');
|
875
|
return;
|
876
|
}
|
877
|
if (!is_dir("{$g['varrun_path']}/certs")) {
|
878
|
@mkdir("{$g['varrun_path']}/certs");
|
879
|
}
|
880
|
if (file_exists("{$g['varrun_path']}/certs/{$caref['refid']}.ca")) {
|
881
|
@unlink("{$g['varrun_path']}/certs/{$caref['refid']}.ca");
|
882
|
}
|
883
|
file_put_contents("{$g['varrun_path']}/certs/{$caref['refid']}.ca", base64_decode($caref['crt']));
|
884
|
@chmod("{$g['varrun_path']}/certs/{$caref['refid']}.ca", 0600);
|
885
|
putenv('LDAPTLS_REQCERT=hard');
|
886
|
/* XXX: Probably even the hashed link should be created for this? */
|
887
|
putenv("LDAPTLS_CACERTDIR={$g['varrun_path']}/certs");
|
888
|
putenv("LDAPTLS_CACERT={$g['varrun_path']}/certs/{$caref['refid']}.ca");
|
889
|
}
|
890
|
}
|
891
|
|
892
|
function ldap_test_bind($authcfg) {
|
893
|
global $debug, $config, $g;
|
894
|
|
895
|
if ($authcfg) {
|
896
|
if (strstr($authcfg['ldap_urltype'], "Standard")) {
|
897
|
$ldapproto = "ldap";
|
898
|
} else {
|
899
|
$ldapproto = "ldaps";
|
900
|
}
|
901
|
$ldapserver = "{$ldapproto}://" . ldap_format_host($authcfg['host']);
|
902
|
$ldapport = $authcfg['ldap_port'];
|
903
|
if (!empty($ldapport)) {
|
904
|
$ldapserver .= ":{$ldapport}";
|
905
|
}
|
906
|
$ldapbasedn = $authcfg['ldap_basedn'];
|
907
|
$ldapbindun = $authcfg['ldap_binddn'];
|
908
|
$ldapbindpw = $authcfg['ldap_bindpw'];
|
909
|
$ldapver = $authcfg['ldap_protver'];
|
910
|
$ldaptimeout = is_numeric($authcfg['ldap_timeout']) ? $authcfg['ldap_timeout'] : 5;
|
911
|
if (empty($ldapbndun) || empty($ldapbindpw)) {
|
912
|
$ldapanon = true;
|
913
|
} else {
|
914
|
$ldapanon = false;
|
915
|
}
|
916
|
} else {
|
917
|
return false;
|
918
|
}
|
919
|
|
920
|
/* first check if there is even an LDAP server populated */
|
921
|
if (!$ldapserver) {
|
922
|
return false;
|
923
|
}
|
924
|
|
925
|
/* Setup CA environment if needed. */
|
926
|
ldap_setup_caenv($authcfg);
|
927
|
|
928
|
/* connect and see if server is up */
|
929
|
$error = false;
|
930
|
if (!($ldap = ldap_connect($ldapserver))) {
|
931
|
$error = true;
|
932
|
}
|
933
|
|
934
|
if ($error == true) {
|
935
|
log_error(sprintf(gettext("ERROR! Could not connect to server %s."), $ldapname));
|
936
|
return false;
|
937
|
}
|
938
|
|
939
|
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
|
940
|
ldap_set_option($ldap, LDAP_OPT_DEREF, LDAP_DEREF_SEARCHING);
|
941
|
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, (int)$ldapver);
|
942
|
ldap_set_option($ldap, LDAP_OPT_TIMELIMIT, (int)$ldaptimeout);
|
943
|
ldap_set_option($ldap, LDAP_OPT_NETWORK_TIMEOUT, (int)$ldaptimeout);
|
944
|
|
945
|
$ldapbindun = isset($authcfg['ldap_utf8']) ? utf8_encode($ldapbindun) : $ldapbindun;
|
946
|
$ldapbindpw = isset($authcfg['ldap_utf8']) ? utf8_encode($ldapbindpw) : $ldapbindpw;
|
947
|
if ($ldapanon == true) {
|
948
|
if (!($res = @ldap_bind($ldap))) {
|
949
|
@ldap_close($ldap);
|
950
|
return false;
|
951
|
}
|
952
|
} else if (!($res = @ldap_bind($ldap, $ldapbindun, $ldapbindpw))) {
|
953
|
@ldap_close($ldap);
|
954
|
return false;
|
955
|
}
|
956
|
|
957
|
@ldap_unbind($ldap);
|
958
|
|
959
|
return true;
|
960
|
}
|
961
|
|
962
|
function ldap_get_user_ous($show_complete_ou=true, $authcfg) {
|
963
|
global $debug, $config, $g;
|
964
|
|
965
|
if (!function_exists("ldap_connect")) {
|
966
|
return;
|
967
|
}
|
968
|
|
969
|
$ous = array();
|
970
|
|
971
|
if ($authcfg) {
|
972
|
if (strstr($authcfg['ldap_urltype'], "Standard")) {
|
973
|
$ldapproto = "ldap";
|
974
|
} else {
|
975
|
$ldapproto = "ldaps";
|
976
|
}
|
977
|
$ldapserver = "{$ldapproto}://" . ldap_format_host($authcfg['host']);
|
978
|
$ldapport = $authcfg['ldap_port'];
|
979
|
if (!empty($ldapport)) {
|
980
|
$ldapserver .= ":{$ldapport}";
|
981
|
}
|
982
|
$ldapbasedn = $authcfg['ldap_basedn'];
|
983
|
$ldapbindun = $authcfg['ldap_binddn'];
|
984
|
$ldapbindpw = $authcfg['ldap_bindpw'];
|
985
|
$ldapver = $authcfg['ldap_protver'];
|
986
|
if (empty($ldapbindun) || empty($ldapbindpw)) {
|
987
|
$ldapanon = true;
|
988
|
} else {
|
989
|
$ldapanon = false;
|
990
|
}
|
991
|
$ldapname = $authcfg['name'];
|
992
|
$ldapfallback = false;
|
993
|
$ldapscope = $authcfg['ldap_scope'];
|
994
|
$ldaptimeout = is_numeric($authcfg['ldap_timeout']) ? $authcfg['ldap_timeout'] : 5;
|
995
|
} else {
|
996
|
return false;
|
997
|
}
|
998
|
|
999
|
/* first check if there is even an LDAP server populated */
|
1000
|
if (!$ldapserver) {
|
1001
|
log_error(gettext("ERROR! ldap_get_user_ous() backed selected with no LDAP authentication server defined."));
|
1002
|
return $ous;
|
1003
|
}
|
1004
|
|
1005
|
/* Setup CA environment if needed. */
|
1006
|
ldap_setup_caenv($authcfg);
|
1007
|
|
1008
|
/* connect and see if server is up */
|
1009
|
$error = false;
|
1010
|
if (!($ldap = ldap_connect($ldapserver))) {
|
1011
|
$error = true;
|
1012
|
}
|
1013
|
|
1014
|
if ($error == true) {
|
1015
|
log_error(sprintf(gettext("ERROR! Could not connect to server %s."), $ldapname));
|
1016
|
return $ous;
|
1017
|
}
|
1018
|
|
1019
|
$ldapfilter = "(|(ou=*)(cn=Users))";
|
1020
|
|
1021
|
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
|
1022
|
ldap_set_option($ldap, LDAP_OPT_DEREF, LDAP_DEREF_SEARCHING);
|
1023
|
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, (int)$ldapver);
|
1024
|
ldap_set_option($ldap, LDAP_OPT_TIMELIMIT, (int)$ldaptimeout);
|
1025
|
ldap_set_option($ldap, LDAP_OPT_NETWORK_TIMEOUT, (int)$ldaptimeout);
|
1026
|
|
1027
|
$ldapbindun = isset($authcfg['ldap_utf8']) ? utf8_encode($ldapbindun) : $ldapbindun;
|
1028
|
$ldapbindpw = isset($authcfg['ldap_utf8']) ? utf8_encode($ldapbindpw) : $ldapbindpw;
|
1029
|
if ($ldapanon == true) {
|
1030
|
if (!($res = @ldap_bind($ldap))) {
|
1031
|
log_error(sprintf(gettext("ERROR! ldap_get_user_ous() could not bind anonymously to server %s."), $ldapname));
|
1032
|
@ldap_close($ldap);
|
1033
|
return $ous;
|
1034
|
}
|
1035
|
} else if (!($res = @ldap_bind($ldap, $ldapbindun, $ldapbindpw))) {
|
1036
|
log_error(sprintf(gettext("ERROR! ldap_get_user_ous() could not bind to server %s."), $ldapname));
|
1037
|
@ldap_close($ldap);
|
1038
|
return $ous;
|
1039
|
}
|
1040
|
|
1041
|
if ($ldapscope == "one") {
|
1042
|
$ldapfunc = "ldap_list";
|
1043
|
} else {
|
1044
|
$ldapfunc = "ldap_search";
|
1045
|
}
|
1046
|
|
1047
|
$search = @$ldapfunc($ldap, $ldapbasedn, $ldapfilter);
|
1048
|
$info = @ldap_get_entries($ldap, $search);
|
1049
|
|
1050
|
if (is_array($info)) {
|
1051
|
foreach ($info as $inf) {
|
1052
|
if (!$show_complete_ou) {
|
1053
|
$inf_split = explode(",", $inf['dn']);
|
1054
|
$ou = $inf_split[0];
|
1055
|
$ou = str_replace("OU=", "", $ou);
|
1056
|
$ou = str_replace("CN=", "", $ou);
|
1057
|
} else {
|
1058
|
if ($inf['dn']) {
|
1059
|
$ou = $inf['dn'];
|
1060
|
}
|
1061
|
}
|
1062
|
if ($ou) {
|
1063
|
$ous[] = $ou;
|
1064
|
}
|
1065
|
}
|
1066
|
}
|
1067
|
|
1068
|
@ldap_unbind($ldap);
|
1069
|
|
1070
|
return $ous;
|
1071
|
}
|
1072
|
|
1073
|
function ldap_get_groups($username, $authcfg) {
|
1074
|
global $debug, $config;
|
1075
|
|
1076
|
if (!function_exists("ldap_connect")) {
|
1077
|
return;
|
1078
|
}
|
1079
|
|
1080
|
if (!$username) {
|
1081
|
return false;
|
1082
|
}
|
1083
|
|
1084
|
if (!isset($authcfg['ldap_nostrip_at']) && stristr($username, "@")) {
|
1085
|
$username_split = explode("@", $username);
|
1086
|
$username = $username_split[0];
|
1087
|
}
|
1088
|
|
1089
|
if (stristr($username, "\\")) {
|
1090
|
$username_split = explode("\\", $username);
|
1091
|
$username = $username_split[0];
|
1092
|
}
|
1093
|
|
1094
|
//log_error("Getting LDAP groups for {$username}.");
|
1095
|
if ($authcfg) {
|
1096
|
if (strstr($authcfg['ldap_urltype'], "Standard")) {
|
1097
|
$ldapproto = "ldap";
|
1098
|
} else {
|
1099
|
$ldapproto = "ldaps";
|
1100
|
}
|
1101
|
$ldapserver = "{$ldapproto}://" . ldap_format_host($authcfg['host']);
|
1102
|
$ldapport = $authcfg['ldap_port'];
|
1103
|
if (!empty($ldapport)) {
|
1104
|
$ldapserver .= ":{$ldapport}";
|
1105
|
}
|
1106
|
$ldapbasedn = $authcfg['ldap_basedn'];
|
1107
|
$ldapbindun = $authcfg['ldap_binddn'];
|
1108
|
$ldapbindpw = $authcfg['ldap_bindpw'];
|
1109
|
$ldapauthcont = $authcfg['ldap_authcn'];
|
1110
|
$ldapnameattribute = strtolower($authcfg['ldap_attr_user']);
|
1111
|
$ldapgroupattribute = strtolower($authcfg['ldap_attr_member']);
|
1112
|
if (isset($authcfg['ldap_rfc2307'])) {
|
1113
|
$ldapfilter = "(&(objectClass={$authcfg['ldap_attr_groupobj']})({$ldapgroupattribute}={$username}))";
|
1114
|
} else {
|
1115
|
$ldapfilter = "({$ldapnameattribute}={$username})";
|
1116
|
}
|
1117
|
$ldaptype = "";
|
1118
|
$ldapver = $authcfg['ldap_protver'];
|
1119
|
if (empty($ldapbindun) || empty($ldapbindpw)) {
|
1120
|
$ldapanon = true;
|
1121
|
} else {
|
1122
|
$ldapanon = false;
|
1123
|
}
|
1124
|
$ldapname = $authcfg['name'];
|
1125
|
$ldapfallback = false;
|
1126
|
$ldapscope = $authcfg['ldap_scope'];
|
1127
|
$ldaptimeout = is_numeric($authcfg['ldap_timeout']) ? $authcfg['ldap_timeout'] : 5;
|
1128
|
} else {
|
1129
|
return false;
|
1130
|
}
|
1131
|
|
1132
|
if (isset($authcfg['ldap_rfc2307'])) {
|
1133
|
$ldapdn = $ldapbasedn;
|
1134
|
} else {
|
1135
|
$ldapdn = $_SESSION['ldapdn'];
|
1136
|
}
|
1137
|
|
1138
|
/*Convert attribute to lowercase. php ldap arrays put everything in lowercase */
|
1139
|
$ldapgroupattribute = strtolower($ldapgroupattribute);
|
1140
|
$memberof = array();
|
1141
|
|
1142
|
/* Setup CA environment if needed. */
|
1143
|
ldap_setup_caenv($authcfg);
|
1144
|
|
1145
|
/* connect and see if server is up */
|
1146
|
$error = false;
|
1147
|
if (!($ldap = ldap_connect($ldapserver))) {
|
1148
|
$error = true;
|
1149
|
}
|
1150
|
|
1151
|
if ($error == true) {
|
1152
|
log_error(sprintf(gettext("ERROR! ldap_get_groups() Could not connect to server %s."), $ldapname));
|
1153
|
return memberof;
|
1154
|
}
|
1155
|
|
1156
|
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
|
1157
|
ldap_set_option($ldap, LDAP_OPT_DEREF, LDAP_DEREF_SEARCHING);
|
1158
|
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, (int)$ldapver);
|
1159
|
ldap_set_option($ldap, LDAP_OPT_TIMELIMIT, (int)$ldaptimeout);
|
1160
|
ldap_set_option($ldap, LDAP_OPT_NETWORK_TIMEOUT, (int)$ldaptimeout);
|
1161
|
|
1162
|
/* bind as user that has rights to read group attributes */
|
1163
|
$ldapbindun = isset($authcfg['ldap_utf8']) ? utf8_encode($ldapbindun) : $ldapbindun;
|
1164
|
$ldapbindpw = isset($authcfg['ldap_utf8']) ? utf8_encode($ldapbindpw) : $ldapbindpw;
|
1165
|
if ($ldapanon == true) {
|
1166
|
if (!($res = @ldap_bind($ldap))) {
|
1167
|
log_error(sprintf(gettext("ERROR! ldap_get_groups() could not bind anonymously to server %s."), $ldapname));
|
1168
|
@ldap_close($ldap);
|
1169
|
return false;
|
1170
|
}
|
1171
|
} else if (!($res = @ldap_bind($ldap, $ldapbindun, $ldapbindpw))) {
|
1172
|
log_error(sprintf(gettext("ERROR! ldap_get_groups() could not bind to server %s."), $ldapname));
|
1173
|
@ldap_close($ldap);
|
1174
|
return memberof;
|
1175
|
}
|
1176
|
|
1177
|
/* get groups from DN found */
|
1178
|
/* use ldap_read instead of search so we don't have to do a bunch of extra work */
|
1179
|
/* since we know the DN is in $_SESSION['ldapdn'] */
|
1180
|
//$search = ldap_read($ldap, $ldapdn, "(objectclass=*)", array($ldapgroupattribute));
|
1181
|
if ($ldapscope == "one") {
|
1182
|
$ldapfunc = "ldap_list";
|
1183
|
} else {
|
1184
|
$ldapfunc = "ldap_search";
|
1185
|
}
|
1186
|
|
1187
|
$search = @$ldapfunc($ldap, $ldapdn, $ldapfilter, array($ldapgroupattribute));
|
1188
|
$info = @ldap_get_entries($ldap, $search);
|
1189
|
|
1190
|
$gresults = isset($authcfg['ldap_rfc2307']) ? $info : $info[0][$ldapgroupattribute];
|
1191
|
|
1192
|
if (is_array($gresults)) {
|
1193
|
/* Iterate through the groups and throw them into an array */
|
1194
|
foreach ($gresults as $grp) {
|
1195
|
if (((isset($authcfg['ldap_rfc2307'])) && (stristr($grp["dn"], "CN=") !== false)) ||
|
1196
|
((!isset($authcfg['ldap_rfc2307'])) && (stristr($grp, "CN=") !== false))) {
|
1197
|
$grpsplit = isset($authcfg['ldap_rfc2307']) ? explode(",", $grp["dn"]) : explode(",", $grp);
|
1198
|
$memberof[] = preg_replace("/CN=/i", "", $grpsplit[0]);
|
1199
|
}
|
1200
|
}
|
1201
|
}
|
1202
|
|
1203
|
/* Time to close LDAP connection */
|
1204
|
@ldap_unbind($ldap);
|
1205
|
|
1206
|
$groups = print_r($memberof, true);
|
1207
|
|
1208
|
//log_error("Returning groups ".$groups." for user $username");
|
1209
|
|
1210
|
return $memberof;
|
1211
|
}
|
1212
|
|
1213
|
function ldap_format_host($host) {
|
1214
|
return is_ipaddrv6($host) ? "[$host]" : $host ;
|
1215
|
}
|
1216
|
|
1217
|
function ldap_backed($username, $passwd, $authcfg) {
|
1218
|
global $debug, $config;
|
1219
|
|
1220
|
if (!$username) {
|
1221
|
return;
|
1222
|
}
|
1223
|
|
1224
|
if (!function_exists("ldap_connect")) {
|
1225
|
return;
|
1226
|
}
|
1227
|
|
1228
|
if (!isset($authcfg['ldap_nostrip_at']) && stristr($username, "@")) {
|
1229
|
$username_split = explode("@", $username);
|
1230
|
$username = $username_split[0];
|
1231
|
}
|
1232
|
if (stristr($username, "\\")) {
|
1233
|
$username_split = explode("\\", $username);
|
1234
|
$username = $username_split[0];
|
1235
|
}
|
1236
|
|
1237
|
if ($authcfg) {
|
1238
|
if (strstr($authcfg['ldap_urltype'], "Standard")) {
|
1239
|
$ldapproto = "ldap";
|
1240
|
} else {
|
1241
|
$ldapproto = "ldaps";
|
1242
|
}
|
1243
|
$ldapserver = "{$ldapproto}://" . ldap_format_host($authcfg['host']);
|
1244
|
$ldapport = $authcfg['ldap_port'];
|
1245
|
if (!empty($ldapport)) {
|
1246
|
$ldapserver .= ":{$ldapport}";
|
1247
|
}
|
1248
|
$ldapbasedn = $authcfg['ldap_basedn'];
|
1249
|
$ldapbindun = $authcfg['ldap_binddn'];
|
1250
|
$ldapbindpw = $authcfg['ldap_bindpw'];
|
1251
|
if (empty($ldapbindun) || empty($ldapbindpw)) {
|
1252
|
$ldapanon = true;
|
1253
|
} else {
|
1254
|
$ldapanon = false;
|
1255
|
}
|
1256
|
$ldapauthcont = $authcfg['ldap_authcn'];
|
1257
|
$ldapnameattribute = strtolower($authcfg['ldap_attr_user']);
|
1258
|
$ldapextendedqueryenabled = $authcfg['ldap_extended_enabled'];
|
1259
|
$ldapextendedquery = $authcfg['ldap_extended_query'];
|
1260
|
$ldapfilter = "";
|
1261
|
if (!$ldapextendedqueryenabled) {
|
1262
|
$ldapfilter = "({$ldapnameattribute}={$username})";
|
1263
|
} else {
|
1264
|
$ldapfilter = "(&({$ldapnameattribute}={$username})({$ldapextendedquery}))";
|
1265
|
}
|
1266
|
$ldaptype = "";
|
1267
|
$ldapver = $authcfg['ldap_protver'];
|
1268
|
$ldapname = $authcfg['name'];
|
1269
|
$ldapscope = $authcfg['ldap_scope'];
|
1270
|
$ldaptimeout = is_numeric($authcfg['ldap_timeout']) ? $authcfg['ldap_timeout'] : 5;
|
1271
|
} else {
|
1272
|
return false;
|
1273
|
}
|
1274
|
|
1275
|
/* first check if there is even an LDAP server populated */
|
1276
|
if (!$ldapserver) {
|
1277
|
if ($ldapfallback) {
|
1278
|
log_error(gettext("ERROR! ldap_backed() called with no LDAP authentication server defined. Defaulting to local user database. Visit System -> User Manager."));
|
1279
|
return local_backed($username, $passwd);
|
1280
|
} else {
|
1281
|
log_error(gettext("ERROR! ldap_backed() called with no LDAP authentication server defined."));
|
1282
|
}
|
1283
|
|
1284
|
return false;
|
1285
|
}
|
1286
|
|
1287
|
/* Setup CA environment if needed. */
|
1288
|
ldap_setup_caenv($authcfg);
|
1289
|
|
1290
|
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
|
1291
|
ldap_set_option($ldap, LDAP_OPT_DEREF, LDAP_DEREF_SEARCHING);
|
1292
|
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, (int)$ldapver);
|
1293
|
ldap_set_option($ldap, LDAP_OPT_TIMELIMIT, (int)$ldaptimeout);
|
1294
|
ldap_set_option($ldap, LDAP_OPT_NETWORK_TIMEOUT, (int)$ldaptimeout);
|
1295
|
|
1296
|
/* Make sure we can connect to LDAP */
|
1297
|
$error = false;
|
1298
|
if (!($ldap = ldap_connect($ldapserver))) {
|
1299
|
$error = true;
|
1300
|
}
|
1301
|
|
1302
|
if ($error == true) {
|
1303
|
log_error(sprintf(gettext("ERROR! Could not connect to server %s."), $ldapname));
|
1304
|
return false;
|
1305
|
}
|
1306
|
|
1307
|
/* ok, its up. now, lets bind as the bind user so we can search it */
|
1308
|
$error = false;
|
1309
|
$ldapbindun = isset($authcfg['ldap_utf8']) ? utf8_encode($ldapbindun) : $ldapbindun;
|
1310
|
$ldapbindpw = isset($authcfg['ldap_utf8']) ? utf8_encode($ldapbindpw) : $ldapbindpw;
|
1311
|
if ($ldapanon == true) {
|
1312
|
if (!($res = @ldap_bind($ldap))) {
|
1313
|
$error = true;
|
1314
|
}
|
1315
|
} else if (!($res = @ldap_bind($ldap, $ldapbindun, $ldapbindpw))) {
|
1316
|
$error = true;
|
1317
|
}
|
1318
|
|
1319
|
if ($error == true) {
|
1320
|
@ldap_close($ldap);
|
1321
|
log_error(sprintf(gettext("ERROR! Could not bind to server %s."), $ldapname));
|
1322
|
return false;
|
1323
|
}
|
1324
|
|
1325
|
/* Get LDAP Authcontainers and split em up. */
|
1326
|
$ldac_splits = explode(";", $ldapauthcont);
|
1327
|
|
1328
|
/* setup the usercount so we think we haven't found anyone yet */
|
1329
|
$usercount = 0;
|
1330
|
|
1331
|
/*****************************************************************/
|
1332
|
/* We first find the user based on username and filter */
|
1333
|
/* then, once we find the first occurrence of that person */
|
1334
|
/* we set session variables to point to the OU and DN of the */
|
1335
|
/* person. To later be used by ldap_get_groups. */
|
1336
|
/* that way we don't have to search twice. */
|
1337
|
/*****************************************************************/
|
1338
|
if ($debug) {
|
1339
|
log_auth(sprintf(gettext("Now Searching for %s in directory."), $username));
|
1340
|
}
|
1341
|
/* Iterate through the user containers for search */
|
1342
|
foreach ($ldac_splits as $i => $ldac_split) {
|
1343
|
$ldac_split = isset($authcfg['ldap_utf8']) ? utf8_encode($ldac_split) : $ldac_split;
|
1344
|
$ldapfilter = isset($authcfg['ldap_utf8']) ? utf8_encode($ldapfilter) : $ldapfilter;
|
1345
|
$ldapsearchbasedn = isset($authcfg['ldap_utf8']) ? utf8_encode("{$ldac_split},{$ldapbasedn}") : "{$ldac_split},{$ldapbasedn}";
|
1346
|
/* Make sure we just use the first user we find */
|
1347
|
if ($debug) {
|
1348
|
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)));
|
1349
|
}
|
1350
|
if ($ldapscope == "one") {
|
1351
|
$ldapfunc = "ldap_list";
|
1352
|
} else {
|
1353
|
$ldapfunc = "ldap_search";
|
1354
|
}
|
1355
|
/* Support legacy auth container specification. */
|
1356
|
if (stristr($ldac_split, "DC=") || empty($ldapbasedn)) {
|
1357
|
$search = @$ldapfunc($ldap, $ldac_split, $ldapfilter);
|
1358
|
} else {
|
1359
|
$search = @$ldapfunc($ldap, $ldapsearchbasedn, $ldapfilter);
|
1360
|
}
|
1361
|
if (!$search) {
|
1362
|
log_error(sprintf(gettext("Search resulted in error: %s"), ldap_error($ldap)));
|
1363
|
continue;
|
1364
|
}
|
1365
|
$info = ldap_get_entries($ldap, $search);
|
1366
|
$matches = $info['count'];
|
1367
|
if ($matches == 1) {
|
1368
|
$userdn = $_SESSION['ldapdn'] = $info[0]['dn'];
|
1369
|
$_SESSION['ldapou'] = $ldac_split[$i];
|
1370
|
$_SESSION['ldapon'] = "true";
|
1371
|
$usercount = 1;
|
1372
|
break;
|
1373
|
}
|
1374
|
}
|
1375
|
|
1376
|
if ($usercount != 1) {
|
1377
|
@ldap_unbind($ldap);
|
1378
|
log_error(gettext("ERROR! Either LDAP search failed, or multiple users were found."));
|
1379
|
return false;
|
1380
|
}
|
1381
|
|
1382
|
/* Now lets bind as the user we found */
|
1383
|
$passwd = isset($authcfg['ldap_utf8']) ? utf8_encode($passwd) : $passwd;
|
1384
|
if (!($res = @ldap_bind($ldap, $userdn, $passwd))) {
|
1385
|
log_error(sprintf(gettext('ERROR! Could not login to server %1$s as user %2$s: %3$s'), $ldapname, $username, ldap_error($ldap)));
|
1386
|
@ldap_unbind($ldap);
|
1387
|
return false;
|
1388
|
}
|
1389
|
|
1390
|
if ($debug) {
|
1391
|
$userdn = isset($authcfg['ldap_utf8']) ? utf8_decode($userdn) : $userdn;
|
1392
|
log_auth(sprintf(gettext('Logged in successfully as %1$s via LDAP server %2$s with DN = %3$s.'), $username, $ldapname, $userdn));
|
1393
|
}
|
1394
|
|
1395
|
/* At this point we are bound to LDAP so the user was auth'd okay. Close connection. */
|
1396
|
@ldap_unbind($ldap);
|
1397
|
|
1398
|
return true;
|
1399
|
}
|
1400
|
|
1401
|
function radius_backed($username, $passwd, $authcfg, &$attributes = array()) {
|
1402
|
global $debug, $config;
|
1403
|
$ret = false;
|
1404
|
|
1405
|
require_once("radius.inc");
|
1406
|
|
1407
|
$rauth = new Auth_RADIUS_PAP($username, $passwd);
|
1408
|
if ($authcfg) {
|
1409
|
$radiusservers = array();
|
1410
|
$radiusservers[0]['ipaddr'] = $authcfg['host'];
|
1411
|
$radiusservers[0]['port'] = $authcfg['radius_auth_port'];
|
1412
|
$radiusservers[0]['sharedsecret'] = $authcfg['radius_secret'];
|
1413
|
$radiusservers[0]['timeout'] = $authcfg['radius_timeout'];
|
1414
|
} else {
|
1415
|
return false;
|
1416
|
}
|
1417
|
|
1418
|
/* Add new servers to our instance */
|
1419
|
foreach ($radiusservers as $radsrv) {
|
1420
|
$timeout = (is_numeric($radsrv['timeout'])) ? $radsrv['timeout'] : 5;
|
1421
|
$rauth->addServer($radsrv['ipaddr'], $radsrv['port'], $radsrv['sharedsecret'], $timeout);
|
1422
|
}
|
1423
|
|
1424
|
if (PEAR::isError($rauth->start())) {
|
1425
|
$retvalue['auth_val'] = 1;
|
1426
|
$retvalue['error'] = $rauth->getError();
|
1427
|
if ($debug) {
|
1428
|
printf(gettext("RADIUS start: %s<br />\n"), $retvalue['error']);
|
1429
|
}
|
1430
|
}
|
1431
|
|
1432
|
// XXX - billm - somewhere in here we need to handle securid challenge/response
|
1433
|
|
1434
|
/* Send request */
|
1435
|
$result = $rauth->send();
|
1436
|
if (PEAR::isError($result)) {
|
1437
|
$retvalue['auth_val'] = 1;
|
1438
|
$retvalue['error'] = $result->getMessage();
|
1439
|
if ($debug) {
|
1440
|
printf(gettext("RADIUS send failed: %s<br />\n"), $retvalue['error']);
|
1441
|
}
|
1442
|
} else if ($result === true) {
|
1443
|
if ($rauth->getAttributes()) {
|
1444
|
$attributes = $rauth->listAttributes();
|
1445
|
}
|
1446
|
$retvalue['auth_val'] = 2;
|
1447
|
if ($debug) {
|
1448
|
printf(gettext("RADIUS Auth succeeded")."<br />\n");
|
1449
|
}
|
1450
|
$ret = true;
|
1451
|
} else {
|
1452
|
$retvalue['auth_val'] = 3;
|
1453
|
if ($debug) {
|
1454
|
printf(gettext("RADIUS Auth rejected")."<br />\n");
|
1455
|
}
|
1456
|
}
|
1457
|
|
1458
|
// close OO RADIUS_AUTHENTICATION
|
1459
|
$rauth->close();
|
1460
|
|
1461
|
return $ret;
|
1462
|
}
|
1463
|
|
1464
|
/*
|
1465
|
$attributes must contain a "class" key containing the groups and local
|
1466
|
groups must exist to match.
|
1467
|
*/
|
1468
|
function radius_get_groups($attributes) {
|
1469
|
$groups = array();
|
1470
|
if (!empty($attributes) && is_array($attributes) && (!empty($attributes['class']) || !empty($attributes['class_int']))) {
|
1471
|
/* Some RADIUS servers return multiple class attributes, so check them all. */
|
1472
|
$groups = array();
|
1473
|
if (!empty($attributes['class']) && is_array($attributes['class'])) {
|
1474
|
foreach ($attributes['class'] as $class) {
|
1475
|
$groups = array_unique(array_merge($groups, explode(";", $class)));
|
1476
|
}
|
1477
|
}
|
1478
|
|
1479
|
foreach ($groups as & $grp) {
|
1480
|
$grp = trim($grp);
|
1481
|
if (strtolower(substr($grp, 0, 3)) == "ou=") {
|
1482
|
$grp = substr($grp, 3);
|
1483
|
}
|
1484
|
}
|
1485
|
}
|
1486
|
return $groups;
|
1487
|
}
|
1488
|
|
1489
|
function get_user_expiration_date($username) {
|
1490
|
$user = getUserEntry($username);
|
1491
|
if ($user['expires']) {
|
1492
|
return $user['expires'];
|
1493
|
}
|
1494
|
}
|
1495
|
|
1496
|
function is_account_expired($username) {
|
1497
|
$expirydate = get_user_expiration_date($username);
|
1498
|
if ($expirydate) {
|
1499
|
if (strtotime("-1 day") > strtotime(date("m/d/Y", strtotime($expirydate)))) {
|
1500
|
return true;
|
1501
|
}
|
1502
|
}
|
1503
|
|
1504
|
return false;
|
1505
|
}
|
1506
|
|
1507
|
function is_account_disabled($username) {
|
1508
|
$user = getUserEntry($username);
|
1509
|
if (isset($user['disabled'])) {
|
1510
|
return true;
|
1511
|
}
|
1512
|
|
1513
|
return false;
|
1514
|
}
|
1515
|
|
1516
|
function auth_get_authserver($name) {
|
1517
|
global $config;
|
1518
|
|
1519
|
if (is_array($config['system']['authserver'])) {
|
1520
|
foreach ($config['system']['authserver'] as $authcfg) {
|
1521
|
if ($authcfg['name'] == $name) {
|
1522
|
return $authcfg;
|
1523
|
}
|
1524
|
}
|
1525
|
}
|
1526
|
if ($name == "Local Database") {
|
1527
|
return array("name" => gettext("Local Database"), "type" => "Local Auth", "host" => $config['system']['hostname']);
|
1528
|
}
|
1529
|
}
|
1530
|
|
1531
|
function auth_get_authserver_list() {
|
1532
|
global $config;
|
1533
|
|
1534
|
$list = array();
|
1535
|
|
1536
|
if (is_array($config['system']['authserver'])) {
|
1537
|
foreach ($config['system']['authserver'] as $authcfg) {
|
1538
|
/* Add support for disabled entries? */
|
1539
|
$list[$authcfg['name']] = $authcfg;
|
1540
|
}
|
1541
|
}
|
1542
|
|
1543
|
$list["Local Database"] = array("name" => gettext("Local Database"), "type" => "Local Auth", "host" => $config['system']['hostname']);
|
1544
|
return $list;
|
1545
|
}
|
1546
|
|
1547
|
function getUserGroups($username, $authcfg, &$attributes = array()) {
|
1548
|
global $config;
|
1549
|
|
1550
|
$allowed_groups = array();
|
1551
|
|
1552
|
switch ($authcfg['type']) {
|
1553
|
case 'ldap':
|
1554
|
$allowed_groups = @ldap_get_groups($username, $authcfg);
|
1555
|
break;
|
1556
|
case 'radius':
|
1557
|
$allowed_groups = @radius_get_groups($attributes);
|
1558
|
break;
|
1559
|
default:
|
1560
|
$user = getUserEntry($username);
|
1561
|
$allowed_groups = @local_user_get_groups($user, true);
|
1562
|
break;
|
1563
|
}
|
1564
|
|
1565
|
$member_groups = array();
|
1566
|
if (is_array($config['system']['group'])) {
|
1567
|
foreach ($config['system']['group'] as $group) {
|
1568
|
if (in_array($group['name'], $allowed_groups)) {
|
1569
|
$member_groups[] = $group['name'];
|
1570
|
}
|
1571
|
}
|
1572
|
}
|
1573
|
|
1574
|
return $member_groups;
|
1575
|
}
|
1576
|
|
1577
|
function authenticate_user($username, $password, $authcfg = NULL, &$attributes = array()) {
|
1578
|
|
1579
|
if (is_array($username) || is_array($password)) {
|
1580
|
return false;
|
1581
|
}
|
1582
|
|
1583
|
if (!$authcfg) {
|
1584
|
return local_backed($username, $password);
|
1585
|
}
|
1586
|
|
1587
|
$authenticated = false;
|
1588
|
switch ($authcfg['type']) {
|
1589
|
case 'ldap':
|
1590
|
if (ldap_backed($username, $password, $authcfg)) {
|
1591
|
$authenticated = true;
|
1592
|
}
|
1593
|
break;
|
1594
|
case 'radius':
|
1595
|
if (radius_backed($username, $password, $authcfg, $attributes)) {
|
1596
|
$authenticated = true;
|
1597
|
}
|
1598
|
break;
|
1599
|
default:
|
1600
|
/* lookup user object by name */
|
1601
|
if (local_backed($username, $password)) {
|
1602
|
$authenticated = true;
|
1603
|
}
|
1604
|
break;
|
1605
|
}
|
1606
|
|
1607
|
return $authenticated;
|
1608
|
}
|
1609
|
|
1610
|
function session_auth() {
|
1611
|
global $config, $_SESSION, $page;
|
1612
|
|
1613
|
// Handle HTTPS httponly and secure flags
|
1614
|
$currentCookieParams = session_get_cookie_params();
|
1615
|
session_set_cookie_params(
|
1616
|
$currentCookieParams["lifetime"],
|
1617
|
$currentCookieParams["path"],
|
1618
|
NULL,
|
1619
|
($config['system']['webgui']['protocol'] == "https"),
|
1620
|
true
|
1621
|
);
|
1622
|
|
1623
|
if (!session_id()) {
|
1624
|
session_start();
|
1625
|
}
|
1626
|
|
1627
|
// Detect protocol change
|
1628
|
if (!isset($_POST['login']) && !empty($_SESSION['Logged_In']) && $_SESSION['protocol'] != $config['system']['webgui']['protocol']) {
|
1629
|
return false;
|
1630
|
}
|
1631
|
|
1632
|
/* Validate incoming login request */
|
1633
|
$attributes = array();
|
1634
|
if (isset($_POST['login']) && !empty($_POST['usernamefld']) && !empty($_POST['passwordfld'])) {
|
1635
|
$authcfg = auth_get_authserver($config['system']['webgui']['authmode']);
|
1636
|
if (authenticate_user($_POST['usernamefld'], $_POST['passwordfld'], $authcfg, $attributes) ||
|
1637
|
authenticate_user($_POST['usernamefld'], $_POST['passwordfld'])) {
|
1638
|
// Generate a new id to avoid session fixation
|
1639
|
session_regenerate_id();
|
1640
|
$_SESSION['Logged_In'] = "True";
|
1641
|
$_SESSION['Username'] = $_POST['usernamefld'];
|
1642
|
$_SESSION['user_radius_attributes'] = $attributes;
|
1643
|
$_SESSION['last_access'] = time();
|
1644
|
$_SESSION['protocol'] = $config['system']['webgui']['protocol'];
|
1645
|
if (!isset($config['system']['webgui']['quietlogin'])) {
|
1646
|
log_auth(sprintf(gettext("Successful login for user '%1\$s' from: %2\$s"), $_POST['usernamefld'], $_SERVER['REMOTE_ADDR']));
|
1647
|
}
|
1648
|
if (isset($_POST['postafterlogin'])) {
|
1649
|
return true;
|
1650
|
} else {
|
1651
|
if (empty($page)) {
|
1652
|
$page = "/";
|
1653
|
}
|
1654
|
header("Location: {$page}");
|
1655
|
}
|
1656
|
exit;
|
1657
|
} else {
|
1658
|
/* give the user an error message */
|
1659
|
$_SESSION['Login_Error'] = "Username or Password incorrect";
|
1660
|
log_auth("webConfigurator authentication error for '{$_POST['usernamefld']}' from {$_SERVER['REMOTE_ADDR']}");
|
1661
|
if (isAjax()) {
|
1662
|
echo "showajaxmessage('{$_SESSION['Login_Error']}');";
|
1663
|
return;
|
1664
|
}
|
1665
|
}
|
1666
|
}
|
1667
|
|
1668
|
/* Show login page if they aren't logged in */
|
1669
|
if (empty($_SESSION['Logged_In'])) {
|
1670
|
return false;
|
1671
|
}
|
1672
|
|
1673
|
/* If session timeout isn't set, we don't mark sessions stale */
|
1674
|
if (!isset($config['system']['webgui']['session_timeout'])) {
|
1675
|
/* Default to 4 hour timeout if one is not set */
|
1676
|
if ($_SESSION['last_access'] < (time() - 14400)) {
|
1677
|
$_GET['logout'] = true;
|
1678
|
$_SESSION['Logout'] = true;
|
1679
|
} else {
|
1680
|
$_SESSION['last_access'] = time();
|
1681
|
}
|
1682
|
} else if (intval($config['system']['webgui']['session_timeout']) == 0) {
|
1683
|
/* only update if it wasn't ajax */
|
1684
|
if (!isAjax()) {
|
1685
|
$_SESSION['last_access'] = time();
|
1686
|
}
|
1687
|
} else {
|
1688
|
/* Check for stale session */
|
1689
|
if ($_SESSION['last_access'] < (time() - ($config['system']['webgui']['session_timeout'] * 60))) {
|
1690
|
$_GET['logout'] = true;
|
1691
|
$_SESSION['Logout'] = true;
|
1692
|
} else {
|
1693
|
/* only update if it wasn't ajax */
|
1694
|
if (!isAjax()) {
|
1695
|
$_SESSION['last_access'] = time();
|
1696
|
}
|
1697
|
}
|
1698
|
}
|
1699
|
|
1700
|
/* user hit the logout button */
|
1701
|
if (isset($_GET['logout'])) {
|
1702
|
|
1703
|
if ($_SESSION['Logout']) {
|
1704
|
log_error(sprintf(gettext("Session timed out for user '%1\$s' from: %2\$s"), $_SESSION['Username'], $_SERVER['REMOTE_ADDR']));
|
1705
|
} else {
|
1706
|
log_error(sprintf(gettext("User logged out for user '%1\$s' from: %2\$s"), $_SESSION['Username'], $_SERVER['REMOTE_ADDR']));
|
1707
|
}
|
1708
|
|
1709
|
/* wipe out $_SESSION */
|
1710
|
$_SESSION = array();
|
1711
|
|
1712
|
if (isset($_COOKIE[session_name()])) {
|
1713
|
setcookie(session_name(), '', time()-42000, '/');
|
1714
|
}
|
1715
|
|
1716
|
/* and destroy it */
|
1717
|
session_destroy();
|
1718
|
|
1719
|
$scriptName = explode("/", $_SERVER["SCRIPT_FILENAME"]);
|
1720
|
$scriptElms = count($scriptName);
|
1721
|
$scriptName = $scriptName[$scriptElms-1];
|
1722
|
|
1723
|
if (isAjax()) {
|
1724
|
return false;
|
1725
|
}
|
1726
|
|
1727
|
/* redirect to page the user is on, it'll prompt them to login again */
|
1728
|
header("Location: {$scriptName}");
|
1729
|
|
1730
|
return false;
|
1731
|
}
|
1732
|
|
1733
|
/*
|
1734
|
* this is for debugging purpose if you do not want to use Ajax
|
1735
|
* to submit a HTML form. It basically disables the observation
|
1736
|
* of the submit event and hence does not trigger Ajax.
|
1737
|
*/
|
1738
|
if ($_GET['disable_ajax']) {
|
1739
|
$_SESSION['NO_AJAX'] = "True";
|
1740
|
}
|
1741
|
|
1742
|
/*
|
1743
|
* Same to re-enable Ajax.
|
1744
|
*/
|
1745
|
if ($_GET['enable_ajax']) {
|
1746
|
unset($_SESSION['NO_AJAX']);
|
1747
|
}
|
1748
|
|
1749
|
return true;
|
1750
|
}
|
1751
|
|
1752
|
?>
|