Project

General

Profile

Download (38.1 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	Copyright (C) 2010 Ermal Lu?i
5
	All rights reserved.
6

    
7
	Copyright (C) 2007, 2008 Scott Ullrich <sullrich@gmail.com>
8
	All rights reserved.
9

    
10
        Copyright (C) 2005-2006 Bill Marquette <bill.marquette@gmail.com>
11
        All rights reserved.
12

    
13
        Copyright (C) 2006 Paul Taylor <paultaylor@winn-dixie.com>.
14
        All rights reserved.
15

    
16
        Copyright (C) 2003-2006 Manuel Kasper <mk@neon1.net>.
17
        All rights reserved.
18

    
19
        Redistribution and use in source and binary forms, with or without
20
        modification, are permitted provided that the following conditions are met:
21

    
22
        1. Redistributions of source code must retain the above copyright notice,
23
           this list of conditions and the following disclaimer.
24

    
25
        2. Redistributions in binary form must reproduce the above copyright
26
           notice, this list of conditions and the following disclaimer in the
27
           documentation and/or other materials provided with the distribution.
28

    
29
        THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
30
        INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
31
        AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
32
        AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
33
        OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34
        SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35
        INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36
        CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37
        ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
        POSSIBILITY OF SUCH DAMAGE.
39

    
40
		DISABLE_PHP_LINT_CHECKING
41
		pfSense_BUILDER_BINARIES:	/usr/sbin/pw	/bin/cp
42
		pfSense_MODULE:	auth
43
*/
44

    
45
/*
46
 * NOTE : Portions of the mschapv2 support was based on the BSD licensed CHAP.php
47
 * file courtesy of Michael Retterklieber.
48
 */
49
if(!$do_not_include_config_gui_inc)
50
	require_once("config.gui.inc");
51

    
52
// Will be changed to false if security checks fail
53
$security_passed = true;
54

    
55
/* If this function doesn't exist, we're being called from Captive Portal or 
56
   another internal subsystem which does not include authgui.inc */
57
if (function_exists("display_error_form") && !isset($config['system']['webgui']['nodnsrebindcheck'])) {
58
	/* DNS ReBinding attack prevention.  http://redmine.pfsense.org/issues/708 */
59
	$found_host = false;
60
	if(strstr($_SERVER['HTTP_HOST'], ":")) {
61
		$http_host_port = explode(":", $_SERVER['HTTP_HOST']);
62
		$http_host = $http_host_port[0];
63
	} else {
64
		$http_host = $_SERVER['HTTP_HOST'];
65
	}
66
	if(is_ipaddr($http_host) or $_SERVER['SERVER_ADDR'] == "127.0.0.1" or
67
			strcasecmp($http_host, "localhost") == 0)
68
		$found_host = true;
69
	if(strcasecmp($http_host, $config['system']['hostname'] . "." . $config['system']['domain']) == 0 or
70
			strcasecmp($http_host, $config['system']['hostname']) == 0)
71
		$found_host = true;
72

    
73
	if(is_array($config['dyndnses']['dyndns']) && !$found_host)
74
		foreach($config['dyndnses']['dyndns'] as $dyndns)
75
			if(strcasecmp($dyndns['host'], $http_host) == 0) {
76
				$found_host = true;
77
				break;
78
			}
79

    
80
	if(!empty($config['system']['webgui']['althostnames']) && !$found_host) {
81
		$althosts = explode(" ", $config['system']['webgui']['althostnames']);
82
		foreach ($althosts as $ah)
83
			if(strcasecmp($ah, $http_host) == 0 or strcasecmp($ah, $_SERVER['SERVER_ADDR']) == 0) {
84
				$found_host = true;
85
				break;
86
			}
87
	}
88

    
89
	if($found_host == false) {
90
		if(!security_checks_disabled()) {
91
			display_error_form("501", "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.");
92
			exit;
93
		}
94
		$security_passed = false;
95
	}
96
}
97

    
98
// If the HTTP_REFERER is something other than ourselves then disallow.
99
if(function_exists("display_error_form") && !isset($config['system']['webgui']['nohttpreferercheck'])) {
100
	if($_SERVER['HTTP_REFERER']) {
101
		if(file_exists("{$g['tmp_path']}/setupwizard_lastreferrer")) {
102
			if($_SERVER['HTTP_REFERER'] == file_get_contents("{$g['tmp_path']}/setupwizard_lastreferrer")) {
103
				unlink("{$g['tmp_path']}/setupwizard_lastreferrer");
104
				header("Refresh: 1; url=index.php");
105
				echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n        \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
106
				echo "<html><head><title>" . gettext("Redirecting...") . "</title></head><body>" . gettext("Redirecting to the dashboard...") . "</body></html>";
107
				exit;
108
			}
109
		}
110
		$found_host = false;
111
		$referrer_host = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
112
		if($referrer_host) {
113
			if(strcasecmp($referrer_host, $config['system']['hostname'] . "." . $config['system']['domain']) == 0
114
					|| strcasecmp($referrer_host, $config['system']['hostname']) == 0)
115
				$found_host = true;
116
			if(!empty($config['system']['webgui']['althostnames']) && !$found_host) {
117
				$althosts = explode(" ", $config['system']['webgui']['althostnames']);
118
				foreach ($althosts as $ah) {
119
					if(strcasecmp($referrer_host, $ah) == 0) {
120
						$found_host = true;
121
						break;
122
					}
123
				}
124
			}
125
			if(!$found_host) {
126
				$interface_list_ips = get_configured_ip_addresses();
127
				foreach($interface_list_ips as $ilips) {
128
					if(strcasecmp($referrer_host, $ilips) == 0) {
129
						$found_host = true;
130
						break;
131
					}
132
				}
133
			}
134
		}
135
		if($found_host == false) {
136
			if(!security_checks_disabled()) {
137
				display_error_form("501", "An HTTP_REFERER was detected other than what is defined in System -> Advanced (" . htmlspecialchars($_SERVER['HTTP_REFERER']) . ").  You can disable this check if needed in System -> Advanced -> Admin.");
138
				exit;
139
			}
140
			$security_passed = false;
141
		}
142
	} else
143
		$security_passed = false;
144
}
145

    
146
if (function_exists("display_error_form") && $security_passed)
147
	/* Security checks passed, so it should be OK to turn them back on */
148
	restore_security_checks();
149
unset($security_passed);
150

    
151
$groupindex = index_groups();
152
$userindex = index_users();
153

    
154
function index_groups() {
155
	global $g, $debug, $config, $groupindex;
156

    
157
	$groupindex = array();
158

    
159
	if (is_array($config['system']['group'])) {
160
		$i = 0;
161
		foreach($config['system']['group'] as $groupent) {
162
			$groupindex[$groupent['name']] = $i;
163
			$i++;
164
		}
165
	}
166

    
167
	return ($groupindex);
168
}
169

    
170
function index_users() {
171
	global $g, $debug, $config;
172

    
173
	if (is_array($config['system']['user'])) {
174
		$i = 0;
175
		foreach($config['system']['user'] as $userent) {
176
			$userindex[$userent['name']] = $i;
177
			$i++;
178
		}
179
	}
180

    
181
	return ($userindex);
182
}
183

    
184
function & getUserEntry($name) {
185
	global $debug, $config, $userindex;
186
	if (isset($userindex[$name]))
187
		return $config['system']['user'][$userindex[$name]];
188
}
189

    
190
function & getUserEntryByUID($uid) {
191
	global $debug, $config;
192
	foreach ($config['system']['user'] as & $user)
193
		if ($user['uid'] == $uid)
194
			return $user;
195

    
196
	return false;
197
}
198

    
199
function & getGroupEntry($name) {
200
	global $debug, $config, $groupindex;
201
	if (isset($groupindex[$name]))
202
		return $config['system']['group'][$groupindex[$name]];
203
}
204

    
205
function & getGroupEntryByGID($gid) {
206
	global $debug, $config;
207
	foreach ($config['system']['group'] as & $group)
208
		if ($group['gid'] == $gid)
209
			return $group;
210

    
211
	return false;
212
}
213

    
214
function get_user_privileges(& $user) {
215

    
216
        $privs = $user['priv'];
217
        if (!is_array($privs))
218
                $privs = array();
219

    
220
        $names = local_user_get_groups($user, true);
221

    
222
        foreach ($names as $name) {
223
                $group = getGroupEntry($name);
224
                if (is_array($group['priv']))
225
                        $privs = array_merge( $privs, $group['priv']);
226
        }
227

    
228
        return $privs;
229
}
230

    
231
function userHasPrivilege($userent, $privid = false) {
232

    
233
        if (!$privid || !is_array($userent))
234
                return false;
235

    
236
        $privs = get_user_privileges($userent);
237

    
238
        if (!is_array($privs))
239
                return false;
240

    
241
        if (!in_array($privid, $privs))
242
                return false;
243

    
244
        return true;
245
}
246

    
247
function local_backed($username, $passwd) {
248

    
249
	$user = getUserEntry($username);
250
	if (!$user)
251
		return false;
252

    
253
	if (is_account_disabled($username) || is_account_expired($username))
254
		return false;
255

    
256
	if ($user['password'])
257
	{
258
		$passwd = crypt($passwd, $user['password']);
259
		if ($passwd == $user['password'])
260
			return true;
261
	}
262

    
263
	if ($user['md5-hash'])
264
	{
265
		$passwd = md5($passwd);
266
		if ($passwd == $user['md5-hash'])
267
			return true;
268
	}
269

    
270
	return false;
271
}
272

    
273
function local_sync_accounts() {
274
	global $debug, $config;
275
	conf_mount_rw();
276

    
277
	/* remove local users to avoid uid conflicts */
278
	$fd = popen("/usr/sbin/pw usershow -a", "r");
279
	if ($fd) {
280
		while (!feof($fd)) {
281
			$line = explode(":",fgets($fd));
282
			if (!strncmp($line[0], "_", 1))
283
				continue;
284
			if ($line[2] < 2000)
285
				continue;
286
			if ($line[2] > 65000)
287
				continue;
288
			$cmd = "/usr/sbin/pw userdel {$line[2]}";
289
			if($debug)
290
				log_error("Running: {$cmd}");
291
			mwexec($cmd);
292
		}
293
		pclose($fd);
294
	}
295

    
296
	/* remove local groups to avoid gid conflicts */
297
	$gids = array();
298
	$fd = popen("/usr/sbin/pw groupshow -a", "r");
299
	if ($fd) {
300
		while (!feof($fd)) {
301
			$line = explode(":",fgets($fd));
302
			if (!strncmp($line[0], "_", 1))
303
				continue;
304
			if ($line[2] < 2000)
305
				continue;
306
			if ($line[2] > 65000)
307
				continue;
308
			$cmd = "/usr/sbin/pw groupdel {$line[2]}";
309
			if($debug)
310
				log_error("Running: {$cmd}");
311
			mwexec($cmd);
312
		}
313
		pclose($fd);
314
	}
315

    
316
	/* make sure the all group exists */
317
	$allgrp = getGroupEntryByGID(1998);
318
	local_group_set($allgrp, true);
319

    
320
	/* sync all local users */
321
	if (is_array($config['system']['user']))
322
		foreach ($config['system']['user'] as $user)
323
			local_user_set($user);
324

    
325
	/* sync all local groups */
326
	if (is_array($config['system']['group']))
327
		foreach ($config['system']['group'] as $group)
328
			local_group_set($group);
329

    
330
	conf_mount_ro();
331

    
332
}
333

    
334
function local_user_set(& $user) {
335
	global $g, $debug;
336

    
337
	conf_mount_rw();
338

    
339
	$home_base = "/home/";	
340
	$user_uid = $user['uid'];
341
	$user_name = $user['name'];
342
	$user_home = "{$home_base}{$user_name}";
343
	$user_shell = "/etc/rc.initial";
344
	$user_group = "nobody";
345

    
346
	// Ensure $home_base exists and is writable
347
	if (!is_dir($home_base)) 
348
		mkdir($home_base, 0755);
349

    
350
	$lock_account = false;
351
	/* configure shell type */
352
	/* Cases here should be ordered by most privileged to least privileged. */
353
	if (userHasPrivilege($user, "user-shell-access") || userHasPrivilege($user, "page-all")) {
354
		$user_shell = "/bin/tcsh";
355
	} elseif (userHasPrivilege($user, "user-copy-files")) {
356
		$user_shell = "/usr/local/bin/scponly";
357
	} elseif (userHasPrivilege($user, "user-ssh-tunnel")) {
358
		$user_shell = "/usr/local/sbin/ssh_tunnel_shell";
359
	} else {
360
		$user_shell = "/sbin/nologin";
361
		$lock_account = true;
362
	}
363

    
364
	/* Lock out disabled or expired users, unless it's root/admin. */
365
	if ((is_account_disabled($user_name) || is_account_expired($user_name)) && ($user_uid != 0)) {
366
		$user_shell = "/sbin/nologin";
367
		$lock_account = true;
368
	}
369

    
370
	/* root user special handling */
371
	if ($user_uid == 0) {
372
		$cmd = "/usr/sbin/pw usermod -q -n root -s /bin/sh -H 0";
373
		if($debug)
374
			log_error("Running: {$cmd}");
375
		$fd = popen($cmd, "w");
376
		fwrite($fd, $user['password']);
377
		pclose($fd);
378
		$user_group = "wheel";
379
		$user_home = "/root";
380
		$user_shell = "/etc/rc.initial";
381
	}
382

    
383
	/* read from pw db */
384
	$fd = popen("/usr/sbin/pw usershow {$user_name} 2>&1", "r");
385
	$pwread = fgets($fd);
386
	pclose($fd);
387

    
388
	/* determine add or mod */
389
	if (!strncmp($pwread, "pw:", 3)) {
390
		$user_op = "useradd -m -k /etc/skel -o";
391
	} else {
392
		$user_op = "usermod";
393
	}
394

    
395
	/* add or mod pw db */
396
	$cmd = "/usr/sbin/pw {$user_op} -q -u {$user_uid} -n {$user_name}".
397
			" -g {$user_group} -s {$user_shell} -d {$user_home}".
398
			" -c ".escapeshellarg($user['descr'])." -H 0 2>&1";
399

    
400
	if($debug)
401
		log_error("Running: {$cmd}");
402
	$fd = popen($cmd, "w");
403
	fwrite($fd, $user['password']);
404
	pclose($fd);
405

    
406
	/* create user directory if required */
407
	if (!is_dir($user_home)) {
408
		mkdir($user_home, 0700);
409
		mwexec("cp /root/.* {$home_base}/");
410
	}
411
	chown($user_home, $user_name);
412
	chgrp($user_home, $user_group);
413

    
414
	/* write out ssh authorized key file */
415
	if($user['authorizedkeys']) {
416
		if (!is_dir("{$user_home}/.ssh")) {
417
			mkdir("{$user_home}/.ssh", 0700);
418
			chown("{$user_home}/.ssh", $user_name);
419
		}
420
		$keys = base64_decode($user['authorizedkeys']);
421
		file_put_contents("{$user_home}/.ssh/authorized_keys", $keys);
422
		chown("{$user_home}/.ssh/authorized_keys", $user_name);
423
	} else
424
		unlink_if_exists("{$user_home}/.ssh/authorized_keys");
425

    
426
	$un = $lock_account ? "" : "un";
427
	exec("/usr/sbin/pw {$un}lock {$user_name} -q");
428
	
429
	conf_mount_ro();
430
}
431

    
432
function local_user_del($user) {
433
	global $debug;
434

    
435
	/* remove all memberships */
436
	local_user_set_groups($user);
437

    
438
	/* Don't remove /root */
439
	if ($user['uid'] != 0)
440
		$rmhome = "-r";
441

    
442
	/* delete from pw db */
443
	$cmd = "/usr/sbin/pw userdel {$user['name']} {$rmhome}";
444

    
445
	if($debug)
446
		log_error("Running: {$cmd}");
447
	mwexec($cmd);
448

    
449
	/* Delete user from groups needs a call to write_config() */
450
	local_group_del_user($user);
451
}
452

    
453
function local_user_set_password(& $user, $password) {
454

    
455
	$user['password'] = crypt($password);
456
	$user['md5-hash'] = md5($password);
457

    
458
	// Converts ascii to unicode.
459
	$astr = (string) $password;
460
	$ustr = '';
461
	for ($i = 0; $i < strlen($astr); $i++) {
462
		$a = ord($astr{$i}) << 8;
463
		$ustr.= sprintf("%X", $a);
464
	}
465

    
466
	// Generate the NT-HASH from the unicode string
467
	$user['nt-hash'] = bin2hex(mhash(MHASH_MD4, $ustr));
468
}
469

    
470
function local_user_get_groups($user, $all = false) {
471
	global $debug, $config;
472

    
473
	$groups = array();
474
	if (!is_array($config['system']['group']))
475
		return $groups;
476

    
477
	foreach ($config['system']['group'] as $group)
478
		if ( $all || ( !$all && ($group['name'] != "all")))
479
			if (is_array($group['member']))
480
				if (in_array($user['uid'], $group['member']))
481
					$groups[] = $group['name'];
482

    
483
	if ( $all )
484
		$groups[] = "all";
485

    
486
	sort($groups);
487

    
488
	return $groups;
489
	
490
}
491

    
492
function local_user_set_groups($user, $new_groups = NULL ) {
493
	global $debug, $config, $groupindex;
494

    
495
	if (!is_array($config['system']['group']))
496
		return;
497

    
498
	$cur_groups = local_user_get_groups($user, true);
499
	$mod_groups = array();
500

    
501
	if (!is_array($new_groups))
502
		$new_groups = array();
503

    
504
	if (!is_array($cur_groups))
505
		$cur_groups = array();
506

    
507
	/* determine which memberships to add */
508
	foreach ($new_groups as $groupname) {
509
		if (in_array($groupname,$cur_groups))
510
			continue;
511
		$group = & $config['system']['group'][$groupindex[$groupname]];
512
		$group['member'][] = $user['uid'];
513
		$mod_groups[] = $group;
514
	}
515
	unset($group);
516

    
517
	/* determine which memberships to remove */
518
	foreach ($cur_groups as $groupname) {
519
		if (in_array($groupname,$new_groups))
520
			continue;
521
		if (!isset($config['system']['group'][$groupindex[$groupname]]))
522
			continue;
523
		$group = & $config['system']['group'][$groupindex[$groupname]];
524
		if (is_array($group['member'])) {
525
			$index = array_search($user['uid'], $group['member']);
526
			array_splice($group['member'], $index, 1);
527
			$mod_groups[] = $group;
528
		}
529
	}
530
	unset($group);
531

    
532
	/* sync all modified groups */
533
	foreach ($mod_groups as $group)
534
		local_group_set($group);
535
}
536

    
537
function local_group_del_user($user) {
538
	global $config;
539

    
540
	if (!is_array($config['system']['group']))
541
                return;
542

    
543
        foreach ($config['system']['group'] as $group) {
544
		if (is_array($group['member'])) {
545
			foreach ($group['member'] as $idx => $uid) {
546
				if ($user['uid'] == $uid)
547
					unset($config['system']['group']['member'][$idx]);
548
			}
549
		}
550
	}
551
}
552

    
553
function local_group_set($group, $reset = false) {
554
	global $debug;
555

    
556
	$group_name = $group['name'];
557
	$group_gid = $group['gid'];
558
	$group_members = "''";
559
	if (!$reset && !empty($group['member']) && count($group['member']) > 0)
560
		$group_members = implode(",",$group['member']);
561

    
562
	/* read from group db */
563
	$fd = popen("/usr/sbin/pw groupshow {$group_name} 2>&1", "r");
564
	$pwread = fgets($fd);
565
	pclose($fd);
566

    
567
	/* determine add or mod */
568
	if (!strncmp($pwread, "pw:", 3))
569
		$group_op = "groupadd";
570
	else
571
		$group_op = "groupmod";
572

    
573
	/* add or mod group db */
574
	$cmd = "/usr/sbin/pw {$group_op} {$group_name} -g {$group_gid} -M {$group_members} 2>&1";
575

    
576
	if($debug)
577
		log_error("Running: {$cmd}");
578
	mwexec($cmd);
579

    
580
}
581

    
582
function local_group_del($group) {
583
	global $debug;
584

    
585
	/* delete from group db */
586
	$cmd = "/usr/sbin/pw groupdel {$group['name']}";
587

    
588
	if($debug)
589
		log_error("Running: {$cmd}");
590
	mwexec($cmd);
591
}
592

    
593
function ldap_test_connection($authcfg) {
594
	global $debug, $config, $g;
595

    
596
	if ($authcfg) {
597
                if (strstr($authcfg['ldap_urltype'], "Standard"))
598
                        $ldapproto = "ldap";
599
                else
600
                        $ldapproto = "ldaps";
601
                $ldapserver         = "{$ldapproto}://{$authcfg['host']}";
602
                $ldapport           = $authcfg['ldap_port'];
603
                $ldapbasedn         = $authcfg['ldap_basedn'];
604
                $ldapbindun         = $authcfg['ldap_binddn'];
605
                $ldapbindpw         = $authcfg['ldap_bindpw'];
606
        } else
607
		return false;
608

    
609
        /* first check if there is even an LDAP server populated */
610
        if(!$ldapserver)
611
                return false;
612

    
613
        /* connect and see if server is up */
614
        putenv('LDAPTLS_REQCERT=never');
615
        $error = false;
616
        if (empty($ldapport)) {
617
                if (!($ldap = ldap_connect($ldapserver)))
618
                        $error = true;
619
        } else if (!($ldap = ldap_connect($ldapserver, $ldapport)))
620
                $error = true;
621

    
622
        if ($error == true) {
623
                log_error("ERROR!  Could not connect to server {$ldapname}.");
624
                return false;
625
        }
626

    
627
	return true;
628
}
629

    
630
function ldap_test_bind($authcfg) {
631
	global $debug, $config, $g;
632

    
633
	if ($authcfg) {
634
                if (strstr($authcfg['ldap_urltype'], "Standard"))
635
                        $ldapproto = "ldap";
636
                else
637
                        $ldapproto = "ldaps";
638
                $ldapserver         = "{$ldapproto}://{$authcfg['host']}";
639
                $ldapport           = $authcfg['ldap_port'];
640
                $ldapbasedn         = $authcfg['ldap_basedn'];
641
                $ldapbindun         = $authcfg['ldap_binddn'];
642
                $ldapbindpw         = $authcfg['ldap_bindpw'];
643
                $ldapver            = $authcfg['ldap_protver'];
644
		if (empty($ldapbndun) || empty($ldapbindpw))
645
                        $ldapanon = true;
646
                else
647
                        $ldapanon = false;
648
	} else
649
		return false;
650

    
651
	/* first check if there is even an LDAP server populated */
652
        if(!$ldapserver)
653
                return false;
654

    
655
        /* connect and see if server is up */
656
        putenv('LDAPTLS_REQCERT=never');
657
        $error = false;
658
        if (empty($ldapport)) {
659
                if (!($ldap = ldap_connect($ldapserver)))
660
                        $error = true;
661
        } else if (!($ldap = ldap_connect($ldapserver, $ldapport)))
662
                $error = true;
663

    
664
        if ($error == true) {
665
                log_error("ERROR!  Could not connect to server {$ldapname}.");
666
                return false;
667
        }
668

    
669
	ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
670
	ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, (int)$ldapver);
671
 
672
	if ($ldapanon == true) {
673
		if (!($res = @ldap_bind($ldap))) {
674
			@ldap_close($ldap);
675
			return false;
676
		}
677
	} else if (!($res = @ldap_bind($ldap, $ldapbindun, $ldapbindpw))) {
678
		@ldap_close($ldap);
679
		return false;
680
	}
681

    
682
	@ldap_unbind($ldap);
683

    
684
	return true;
685
}
686

    
687
function ldap_get_user_ous($show_complete_ou=true, $authcfg) {
688
	global $debug, $config, $g;
689

    
690
	if(!function_exists("ldap_connect"))
691
		return;
692

    
693
	$ous = array();
694

    
695
	if ($authcfg) {
696
                if (strstr($authcfg['ldap_urltype'], "Standard"))
697
                        $ldapproto = "ldap";
698
                else
699
                        $ldapproto = "ldaps";
700
                $ldapserver         = "{$ldapproto}://{$authcfg['host']}";
701
                $ldapport           = $authcfg['ldap_port'];
702
                $ldapbasedn         = $authcfg['ldap_basedn'];
703
                $ldapbindun         = $authcfg['ldap_binddn'];
704
                $ldapbindpw         = $authcfg['ldap_bindpw'];
705
                $ldapver            = $authcfg['ldap_protver'];
706
		if (empty($ldapbindun) || empty($ldapbindpw))
707
                        $ldapanon = true;
708
                else
709
                        $ldapanon = false;
710
                $ldapname           = $authcfg['name'];
711
                $ldapfallback       = false;
712
		$ldapscope          = $authcfg['ldap_scope'];
713
        } else
714
		return false;
715

    
716
        /* first check if there is even an LDAP server populated */
717
        if(!$ldapserver) {
718
                log_error("ERROR!  ldap_get_user_ous() backed selected with no LDAP authentication server defined.");
719
                return $ous;
720
        }
721

    
722
	/* connect and see if server is up */
723
        putenv('LDAPTLS_REQCERT=never');
724
        $error = false;
725
        if (empty($ldapport)) {
726
                if (!($ldap = ldap_connect($ldapserver)))
727
                        $error = true;
728
        } else if (!($ldap = ldap_connect($ldapserver, $ldapport)))
729
                $error = true;
730

    
731
        if ($error == true) {
732
		log_error("ERROR!  Could not connect to server {$ldapname}.");
733
                return $ous;
734
        }
735

    
736
	$ldapfilter = "(|(ou=*)(cn=Users))";
737

    
738
	ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
739
	ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, (int)$ldapver);
740

    
741
	if ($ldapanon == true) {
742
                if (!($res = @ldap_bind($ldap))) {
743
			log_error("ERROR! ldap_get_user_ous() could not bind anonymously to server {$ldapname}.");
744
			@ldap_close($ldap);
745
                        return $ous;
746
		}
747
	} else if (!($res = @ldap_bind($ldap, $ldapbindun, $ldapbindpw))) {
748
		log_error("ERROR! ldap_get_user_ous() could not bind to server {$ldapname}.");
749
		@ldap_close($ldap);
750
		return $ous;
751
	}
752

    
753
	if ($ldapscope == "one")
754
		$ldapfunc = "ldap_list";
755
	else
756
		$ldapfunc = "ldap_search";
757

    
758
	$search = @$ldapfunc($ldap, $ldapbasedn, $ldapfilter);
759
	$info = @ldap_get_entries($ldap, $search);
760

    
761
	if (is_array($info)) {
762
		foreach ($info as $inf) {
763
			if (!$show_complete_ou) {
764
				$inf_split = split(",", $inf['dn']);
765
				$ou = $inf_split[0];
766
				$ou = str_replace("OU=","", $ou);
767
				$ou = str_replace("CN=","", $ou);
768
			} else
769
				if($inf['dn'])
770
					$ou = $inf['dn'];
771
			if($ou)
772
				$ous[] = $ou;
773
		}
774
	}
775

    
776
	@ldap_unbind($ldap);
777

    
778
	return $ous;
779
}
780

    
781
function ldap_get_groups($username, $authcfg) {
782
	global $debug, $config;
783
	
784
	if(!function_exists("ldap_connect"))
785
		return;
786
	
787
	if(!$username) 
788
		return false;
789

    
790
	if(stristr($username, "@")) {
791
		$username_split=split("\@", $username);
792
		$username = $username_split[0];		
793
	}
794

    
795
	if(stristr($username, "\\")) {
796
		$username_split=split("\\", $username);
797
		$username = $username_split[0];        
798
	}    
799
	
800
	//log_error("Getting LDAP groups for {$username}.");
801
        if ($authcfg) {
802
                if (strstr($authcfg['ldap_urltype'], "Standard"))
803
                        $ldapproto = "ldap";
804
                else
805
                        $ldapproto = "ldaps";
806
                $ldapserver         = "{$ldapproto}://{$authcfg['host']}";
807
                $ldapport           = $authcfg['ldap_port'];
808
                $ldapbasedn         = $authcfg['ldap_basedn'];
809
                $ldapbindun         = $authcfg['ldap_binddn'];
810
                $ldapbindpw         = $authcfg['ldap_bindpw'];
811
                $ldapauthcont       = $authcfg['ldap_authcn'];
812
                $ldapnameattribute  = strtolower($authcfg['ldap_attr_user']);
813
                $ldapgroupattribute  = strtolower($authcfg['ldap_attr_member']);
814
                $ldapfilter         = "({$ldapnameattribute}={$username})";
815
                $ldaptype           = "";
816
                $ldapver            = $authcfg['ldap_protver'];
817
		if (empty($ldapbindun) || empty($ldapbindpw))
818
                        $ldapanon = true;
819
                else
820
                        $ldapanon = false;
821
                $ldapname           = $authcfg['name'];
822
                $ldapfallback       = false;
823
		$ldapscope          = $authcfg['ldap_scope'];
824
	} else
825
		return false;
826

    
827
	$ldapdn             = $_SESSION['ldapdn'];
828

    
829
	/*Convert attribute to lowercase.  php ldap arrays put everything in lowercase */
830
	$ldapgroupattribute = strtolower($ldapgroupattribute);
831
	$memberof = array();
832

    
833
	/* connect and see if server is up */
834
	putenv('LDAPTLS_REQCERT=never');
835
	$error = false;
836
        if (empty($ldapport)) {
837
                if (!($ldap = ldap_connect($ldapserver)))
838
                        $error = true;
839
        } else if (!($ldap = ldap_connect($ldapserver, $ldapport)))
840
                $error = true;
841

    
842
	if ($error == true) {
843
		log_error("ERROR! ldap_get_groups() Could not connect to server {$ldapname}.");
844
                return memberof;
845
        }
846
    
847
	ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
848
	ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, (int)$ldapver);
849

    
850
	/* bind as user that has rights to read group attributes */
851
	if ($ldapanon == true) {
852
                if (!($res = @ldap_bind($ldap))) {
853
			log_error("ERROR! ldap_get_groups() could not bind anonymously to server {$ldapname}.");
854
			@ldap_close($ldap);
855
                        return false;
856
		}
857
	} else if (!($res = @ldap_bind($ldap, $ldapbindun, $ldapbindpw))) {
858
		log_error("ERROR! ldap_get_groups() could not bind to server {$ldapname}.");
859
		@ldap_close($ldap);
860
		return memberof;
861
	}
862

    
863
	/* get groups from DN found */
864
	/* use ldap_read instead of search so we don't have to do a bunch of extra work */
865
	/* since we know the DN is in $_SESSION['ldapdn'] */
866
	//$search    = ldap_read($ldap, $ldapdn, "(objectclass=*)", array($ldapgroupattribute));
867
	if ($ldapscope == "one")
868
                $ldapfunc = "ldap_list";
869
        else
870
                $ldapfunc = "ldap_search";
871

    
872
	$search    = @$ldapfunc($ldap, $ldapdn, $ldapfilter, array($ldapgroupattribute));
873
	$info      = @ldap_get_entries($ldap, $search);
874

    
875
	$countem = $info["count"];	
876
	
877
	if(is_array($info[0][$ldapgroupattribute])) {
878
		/* Iterate through the groups and throw them into an array */
879
		foreach ($info[0][$ldapgroupattribute] as $member) {
880
			if (stristr($member, "CN=") !== false) {
881
				$membersplit = split(",", $member);
882
				$memberof[] = preg_replace("/CN=/i", "", $membersplit[0]);
883
			}
884
		}
885
	}
886
	
887
	/* Time to close LDAP connection */
888
	@ldap_unbind($ldap);
889
	
890
	$groups = print_r($memberof,true);
891
	
892
	//log_error("Returning groups ".$groups." for user $username");
893
	
894
	return $memberof;
895
}
896

    
897
function ldap_backed($username, $passwd, $authcfg) {
898
	global $debug, $config;
899
	
900
	if(!$username) 
901
		return;
902

    
903
	if(!function_exists("ldap_connect"))
904
		return;
905

    
906
	if(stristr($username, "@")) {
907
		$username_split=split("\@", $username);
908
		$username = $username_split[0];        
909
	}
910
	if(stristr($username, "\\")) {
911
		$username_split=split("\\", $username);
912
		$username = $username_split[0];        
913
	}
914

    
915
	if ($authcfg) {
916
		if (strstr($authcfg['ldap_urltype'], "Standard"))
917
			$ldapproto = "ldap";
918
		else
919
			$ldapproto = "ldaps";
920
		$ldapserver         = "{$ldapproto}://{$authcfg['host']}";
921
		$ldapport	    = $authcfg['ldap_port'];
922
                $ldapbasedn         = $authcfg['ldap_basedn'];
923
                $ldapbindun         = $authcfg['ldap_binddn'];
924
                $ldapbindpw         = $authcfg['ldap_bindpw'];
925
		if (empty($ldapbindun) || empty($ldapbindpw))
926
			$ldapanon = true;
927
		else
928
			$ldapanon = false;
929
                $ldapauthcont       = $authcfg['ldap_authcn'];
930
                $ldapnameattribute  = strtolower($authcfg['ldap_attr_user']);
931
                $ldapfilter         = "({$ldapnameattribute}={$username})";
932
                $ldaptype           = "";
933
                $ldapver            = $authcfg['ldap_protver'];
934
		$ldapname	    = $authcfg['name'];
935
		$ldapscope	    = $authcfg['ldap_scope'];
936
	} else
937
		return false;
938

    
939
	/* first check if there is even an LDAP server populated */ 
940
	if(!$ldapserver) {
941
		if ($ldapfallback) {
942
			log_error("ERROR! ldap_backed() called with no LDAP authentication server defined.  Defaulting to local user database. Visit System -> User Manager.");
943
			return local_backed($username, $passwd);
944
		} else
945
			log_error("ERROR! ldap_backed() called with no LDAP authentication server defined.");
946

    
947
		return false;
948
	}
949
	
950
	ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
951
	ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, (int)$ldapver);
952

    
953
	/* Make sure we can connect to LDAP */
954
	putenv('LDAPTLS_REQCERT=never');
955
	$error = false;
956
	if (empty($ldapport)) {
957
		if (!($ldap = ldap_connect($ldapserver)))
958
			$error = true;
959
	} else if (!($ldap = ldap_connect($ldapserver, $ldapport)))
960
		$error = true;
961

    
962
	if ($error == true) {
963
		log_error("ERROR!  Could not connect to server {$ldapname}.");
964
		return false;
965
	}
966

    
967
	/* ok, its up.  now, lets bind as the bind user so we can search it */
968
	$error = false;
969
	if ($ldapanon == true) {
970
                if (!($res = @ldap_bind($ldap)))
971
                        $error = true;
972
	} else if (!($res = ldap_bind($ldap, $ldapbindun, $ldapbindpw)))
973
		$error = true;
974

    
975
	if ($error == true) {
976
		@ldap_close($ldap);
977
		log_error("ERROR! Could not bind to server {$ldapname}.");
978
		return false;
979
	}
980
	
981
	/* Get LDAP Authcontainers and split em up. */
982
	$ldac_splits = split(";", $ldapauthcont);
983
	
984
	/* setup the usercount so we think we havn't found anyone yet */
985
	$usercount  = 0;
986

    
987
	/*****************************************************************/
988
	/*  We First find the user based on username and filter          */
989
	/*  Then, once we find the first occurance of that person        */
990
	/*  We set seesion variables to ponit to the OU and DN of the    */
991
	/*  Person.  To later be used by ldap_get_groups.                */
992
	/*  that way we don't have to search twice.                      */
993
	/*****************************************************************/
994
	log_error("Now Searching for {$username} in directory.");
995
	/* Iterate through the user containers for search */
996
	foreach ($ldac_splits as $i => $ldac_split) {
997
		/* Make sure we just use the first user we find */
998
		log_error("Now Searching in server {$ldapname}, container {$ldac_split} with filter {$ldapfilter}.");
999
		if ($ldapscope == "one")
1000
			$ldapfunc = "ldap_list";
1001
		else
1002
			$ldapfunc = "ldap_search";
1003
		/* Support legacy auth container specification. */
1004
		if (stristr($ldac_split, "DC=") || empty($ldapbasedn))
1005
			$search	 = @$ldapfunc($ldap,$ldac_split,$ldapfilter);
1006
		else
1007
			$search  = @$ldapfunc($ldap,"{$ldac_split},{$ldapbasedn}",$ldapfilter);
1008
		if (!$search) {
1009
			log_error("Search resulted in error: " . ldap_error($ldap));
1010
			continue;
1011
		}
1012
		$info	 = ldap_get_entries($ldap,$search);
1013
		$matches = $info['count'];
1014
		if ($matches == 1){
1015
			$userdn = $_SESSION['ldapdn'] = $info[0]['dn'];
1016
			$_SESSION['ldapou'] = $ldac_split[$i];
1017
			$_SESSION['ldapon'] = "true";
1018
			$usercount = 1;
1019
			break;
1020
		}
1021
	}
1022

    
1023
	if ($usercount != 1){
1024
		@ldap_unbind($ldap);
1025
		log_error("ERROR! Either LDAP search failed, or multiple users were found.");
1026
		return false;                         
1027
	}
1028

    
1029
	/* Now lets bind as the user we found */
1030
	if (!($res = @ldap_bind($ldap, $userdn, $passwd))) {
1031
		log_error("ERROR! Could not login to server {$ldapname} as user {$username}.");
1032
		@ldap_unbind($ldap);
1033
		return false;
1034
	}
1035

    
1036
	log_error("Logged in successfully as {$username} via LDAP server {$ldapname} with DN = {$userdn}.");
1037

    
1038
	/* At this point we are bound to LDAP so the user was auth'd okay. Close connection. */
1039
	@ldap_unbind($ldap);
1040

    
1041
	return true;
1042
}
1043

    
1044
function radius_backed($username, $passwd, $authcfg){
1045
	global $debug, $config;
1046
	$ret = false;
1047

    
1048
	require_once("radius.inc");
1049

    
1050
	$rauth = new Auth_RADIUS_PAP($username, $passwd);
1051
	if ($authcfg) {
1052
		$radiusservers = array();
1053
		$radiusservers[0]['ipaddr'] = $authcfg['host'];
1054
		$radiusservers[0]['port'] = $authcfg['radius_auth_port'];
1055
		$radiusservers[0]['sharedsecret'] = $authcfg['radius_secret'];
1056
	} else
1057
		return false;
1058

    
1059
	/* Add a new servers to our instance */
1060
	foreach ($radiusservers as $radsrv)
1061
		$rauth->addServer($radsrv['ipaddr'], $radsrv['port'], $radsrv['sharedsecret']);
1062

    
1063
	if (PEAR::isError($rauth->start())) {
1064
		$retvalue['auth_val'] = 1;
1065
		$retvalue['error'] = $rauth->getError();
1066
		if ($debug)
1067
			printf("Radius start: %s<br>\n", $retvalue['error']);
1068
	}
1069

    
1070
	// XXX - billm - somewhere in here we need to handle securid challenge/response
1071

    
1072
	/* Send request */
1073
	$result = $rauth->send();
1074
	if (PEAR::isError($result)) {
1075
		$retvalue['auth_val'] = 1;
1076
		$retvalue['error'] = $result->getMessage();
1077
		if ($debug)
1078
			printf("Radius send failed: %s<br>\n", $retvalue['error']);
1079
	} else if ($result === true) {
1080
		$retvalue['auth_val'] = 2;
1081
		if ($debug)
1082
			printf(gettext("Radius Auth succeeded")."<br>\n");
1083
		$ret = true;
1084
	} else {
1085
		$retvalue['auth_val'] = 3;
1086
		if ($debug)
1087
			printf(gettext("Radius Auth rejected")."<br>\n");
1088
	}
1089

    
1090
	// close OO RADIUS_AUTHENTICATION
1091
	$rauth->close();
1092

    
1093
	return $ret;
1094
}
1095

    
1096
function get_user_expiration_date($username) {
1097
	$user = getUserEntry($username);
1098
	if ($user['expires']) 
1099
		return $user['expires'];
1100
}
1101

    
1102
function is_account_expired($username) {
1103
	$expirydate = get_user_expiration_date($username);
1104
	if ($expirydate) {
1105
		if (strtotime("-1 day") > strtotime(date("m/d/Y",strtotime($expirydate))))
1106
			return true;
1107
	}
1108

    
1109
	return false;
1110
}
1111

    
1112
function is_account_disabled($username) {
1113
	$user = getUserEntry($username);
1114
	if (isset($user['disabled']))
1115
		return true;
1116

    
1117
	return false;
1118
}
1119

    
1120
function auth_get_authserver($name) {
1121
        global $config;
1122

    
1123
        if (is_array($config['system']['authserver'])) {
1124
                foreach ($config['system']['authserver'] as $authcfg) {
1125
                        if ($authcfg['name'] == $name)
1126
                                return $authcfg;
1127
                }
1128
        }
1129
	if ($name == "Local Database")
1130
		return array("name" => "Local Database", "type" => "Local Auth", "host" => $config['system']['hostname']);
1131
}
1132

    
1133
function auth_get_authserver_list() {
1134
        global $config;
1135

    
1136
	$list = array();
1137

    
1138
        if (is_array($config['system']['authserver'])) {
1139
                foreach ($config['system']['authserver'] as $authcfg) {
1140
			/* Add support for disabled entries? */
1141
			$list[$authcfg['name']] = $authcfg;
1142
                }
1143
        }
1144

    
1145
	$list["Local Database"] = array( "name" => "Local Database", "type" => "Local Auth", "host" => $config['system']['hostname']);
1146
	return $list;
1147
}
1148

    
1149
function getUserGroups($username, $authcfg) {
1150
	global $config;
1151

    
1152
	$allowed_groups = array();
1153

    
1154
	switch($authcfg['type']) {
1155
        case 'ldap':
1156
		$allowed_groups = @ldap_get_groups($username, $authcfg);
1157
		break;
1158
	case 'radius':
1159
		break;
1160
	default:
1161
		$user = getUserEntry($username);
1162
		$allowed_groups = @local_user_get_groups($user, true);
1163
		break;
1164
	}
1165

    
1166
	$member_groups = array();
1167
        if (is_array($config['system']['group'])) {
1168
                foreach ($config['system']['group'] as $group)
1169
                        if (in_array($group['name'], $allowed_groups))
1170
				$member_groups[] = $group['name'];
1171
	}
1172

    
1173
	return $member_groups;
1174
}
1175

    
1176
function authenticate_user($username, $password, $authcfg = NULL) {
1177

    
1178
	if (!$authcfg) {
1179
		return local_backed($username, $password);
1180
	}
1181

    
1182
	$authenticated = false;
1183
	switch($authcfg['type']) {
1184
        case 'ldap':
1185
                if (ldap_backed($username, $password, $authcfg))
1186
                        $authenticated = true;
1187
                break;
1188
        case 'radius':
1189
                if (radius_backed($username, $password, $authcfg))
1190
                        $authenticated = true;
1191
                break;
1192
        default:
1193
                /* lookup user object by name */
1194
                if (local_backed($username, $password))
1195
                        $authenticated = true;
1196
                break;
1197
        }
1198

    
1199
	return $authenticated;
1200
}
1201

    
1202
function session_auth() {
1203
	global $HTTP_SERVER_VARS, $config, $_SESSION, $page;
1204

    
1205
	session_start();
1206

    
1207
	/* Validate incoming login request */
1208
	if (isset($_POST['login'])) {
1209
		$authcfg = auth_get_authserver($config['system']['webgui']['authmode']);
1210
		if (authenticate_user($_POST['usernamefld'], $_POST['passwordfld'], $authcfg) || 
1211
		    authenticate_user($_POST['usernamefld'], $_POST['passwordfld'])) {
1212
			$_SESSION['Logged_In'] = "True";
1213
			$_SESSION['Username'] = $_POST['usernamefld'];
1214
			$_SESSION['last_access'] = time();
1215
			log_error("Successful webConfigurator login for user '{$_POST['usernamefld']}' host: {$_SERVER['REMOTE_ADDR']}");
1216
			$HTTP_SERVER_VARS['AUTH_USER'] = $_SESSION['Username'];
1217
			if (isset($_POST['postafterlogin']))
1218
				return true;
1219
			else {
1220
				if (empty($page))
1221
					$page = "/";
1222
				header("Location: {$page}");
1223
			}
1224
			exit;
1225
		} else {
1226
			/* give the user an error message */
1227
			$_SESSION['Login_Error'] = "Username or Password incorrect";
1228
			log_error("webConfigurator authentication error for '{$_POST['usernamefld']}' host: '{$_SERVER['REMOTE_ADDR']}'");
1229
			
1230
			if(isAjax()) {
1231
				echo "showajaxmessage('{$_SESSION['Login_Error']}');";
1232
				return;
1233
			}
1234
		}
1235
	}
1236

    
1237
	/* Show login page if they aren't logged in */
1238
	if (empty($_SESSION['Logged_In']))
1239
		return false;
1240

    
1241
	/* If session timeout isn't set, we don't mark sessions stale */
1242
	if (!isset($config['system']['webgui']['session_timeout'])) {
1243
		/* Default to 4 hour timeout if one is not set */
1244
		if ($_SESSION['last_access'] < (time() - 14400)) {
1245
			$_GET['logout'] = true;
1246
			$_SESSION['Logout'] = true;
1247
		} else
1248
			$_SESSION['last_access'] = time();	
1249
	} else if (intval($config['system']['webgui']['session_timeout']) == 0) {
1250
		/* only update if it wasn't ajax */
1251
		if (!isAjax())
1252
			$_SESSION['last_access'] = time();
1253
	} else {
1254
		/* Check for stale session */
1255
		if ($_SESSION['last_access'] < (time() - ($config['system']['webgui']['session_timeout'] * 60))) {
1256
			$_GET['logout'] = true;
1257
			$_SESSION['Logout'] = true;
1258
		} else {
1259
			/* only update if it wasn't ajax */
1260
			if (!isAjax())
1261
				$_SESSION['last_access'] = time();
1262
		}
1263
	}
1264

    
1265
	/* user hit the logout button */
1266
	if (isset($_GET['logout'])) {
1267

    
1268
		if ($_SESSION['Logout'])
1269
			log_error("Session timed out for user '{$_SESSION['Username']}' from: {$_SERVER['REMOTE_ADDR']}");
1270
		else
1271
			log_error("User logged out for user '{$_SESSION['Username']}' from: {$_SERVER['REMOTE_ADDR']}");
1272

    
1273
		/* wipe out $_SESSION */
1274
		$_SESSION = array();
1275

    
1276
		if (isset($_COOKIE[session_name()]))
1277
			setcookie(session_name(), '', time()-42000, '/');
1278

    
1279
		/* and destroy it */
1280
		session_destroy();
1281

    
1282
		$scriptName = split("/", $_SERVER["SCRIPT_FILENAME"]);
1283
		$scriptElms = count($scriptName);
1284
		$scriptName = $scriptName[$scriptElms-1];
1285

    
1286
		if (isAjax())
1287
			return false;
1288

    
1289
		/* redirect to page the user is on, it'll prompt them to login again */
1290
		Header("Location: {$scriptName}");
1291

    
1292
		return false;
1293
	}
1294

    
1295
	/*
1296
	 * this is for debugging purpose if you do not want to use Ajax
1297
	 * to submit a HTML form. It basically diables the observation
1298
	 * of the submit event and hence does not trigger Ajax.
1299
	 */
1300
	if ($_GET['disable_ajax'])
1301
		$_SESSION['NO_AJAX'] = "True";
1302

    
1303
	/*
1304
	 * Same to re-enable Ajax.
1305
	 */
1306
	if ($_GET['enable_ajax'])
1307
		unset($_SESSION['NO_AJAX']);
1308

    
1309
	$HTTP_SERVER_VARS['AUTH_USER'] = $_SESSION['Username'];
1310
	return true;
1311
}
1312

    
1313
?>
(4-4/54)