~ubuntu-branches/debian/sid/phpldapadmin/sid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
// $Header: /cvsroot/phpldapadmin/phpldapadmin/htdocs/login.php,v 1.56.2.5 2008/12/12 12:20:22 wurley Exp $

/**
 * For servers whose auth_type is set to 'cookie' or 'session'. Pass me the
 * login info and I'll write two cookies, pla_login_dn_X and pla_pass_X where X
 * is the server_id. The cookie_time comes from config.php
 *
 * @package phpLDAPadmin
 */
/**
 */

require './common.php';

$login['val'] = get_request($ldapserver->getLoginAttr());
$login['pass'] = get_request('login_pass');

if ($ldapserver->isAnonBindAllowed())
	$anon_bind = get_request('anonymous_bind') == 'on' ? true : false;
else
	$anon_bind = false;

if (! $anon_bind && ! strlen($login['pass']))
	system_message(array(
		'title'=>_('Authenticate to server'),
		'body'=>_('You left the password blank.'),
		'type'=>'warn'),
		sprintf('cmd.php?cmd=login_form&server_id=%s',$ldapserver->server_id));

$save_auth_type = $ldapserver->auth_type;

if ($anon_bind) {
	if (DEBUG_ENABLED)
		debug_log('Anonymous Login was posted [%s].',64,__FILE__,__LINE__,__METHOD__,$anon_bind);

	$login['dn'] = null;
	$login['pass'] = null;

/* Checks if the login_attr option is enabled for this host,
   which allows users to login with a simple username like 'jdoe' rather
   than the fully qualified DN, 'uid=jdoe,ou=people,,dc=example,dc=com'. */
} elseif ($ldapserver->isLoginAttrEnabled()) {

	# Is this a login string (printf-style)
	if ($ldapserver->isLoginStringEnabled()) {
		$login['dn'] = str_replace('<username>',$login['val'],$ldapserver->getLoginString());

		if (DEBUG_ENABLED)
			debug_log('LoginStringDN: [%s]',64,__FILE__,__LINE__,__METHOD__,$login['dn']);

	} else {
		# This is a standard login_attr

		/* Fake the auth_type of config to do searching. This way, the admin can specify
		   the DN to use when searching for the login_attr user. */
		$ldapserver->auth_type = 'config';

		if ($ldapserver->login_dn)
			$ldapserver->connect();
		else
			$ldapserver->connect(true,'anonymous');

		if (! empty($ldapserver->login_class))
			$filter = sprintf('(&(objectClass=%s)(%s=%s))',$ldapserver->login_class,$ldapserver->getLoginAttr(),$login['val']);
		else
			$filter = sprintf('%s=%s',$ldapserver->getLoginAttr(),$login['val']);

		# Got through each of the BASE DNs and test the login.
		foreach ($ldapserver->getBaseDN() as $base_dn) {
			if (DEBUG_ENABLED)
				debug_log('Searching LDAP with base [%s]',64,__FILE__,__LINE__,__METHOD__,$base_dn);

			$result = $ldapserver->search(null,$base_dn,$filter,array('dn'));
			$result = array_pop($result);
			$login['dn'] = $result['dn'];

			if ($login['dn']) {
				if (DEBUG_ENABLED)
					debug_log('Got DN [%s] for user ID [%s]',64,__FILE__,__LINE__,__METHOD__,$login['dn'],$login['val']);
				break;
			}
		}

		# If we got here then we werent able to find a DN for the login filter.
		if (! $login['dn'])
			if ($ldapserver->login_fallback_dn)
				$login['dn'] = $login['val'];
			else
				system_message(array(
					'title'=>_('Authenticate to server'),
					'body'=>_('Bad username or password. Please try again.'),
					'type'=>'error'),
					sprintf('cmd.php?cmd=login_form&server_id=%s',$ldapserver->server_id));

		# Restore the original auth_type
		$ldapserver->auth_type = $save_auth_type;
	}

} else {
	$login['dn'] = $login['val'];
}

# We fake a 'config' server auth_type to omit duplicated code
if (DEBUG_ENABLED)
	debug_log('Setting login type to CONFIG with DN [%s]',64,__FILE__,__LINE__,__METHOD__,$login['dn']);

$save_auth_type = $ldapserver->auth_type;
$ldapserver->auth_type = 'config';
$ldapserver->login_dn = $login['dn'];
$ldapserver->login_pass = $login['pass'];

# Verify that dn is allowed to login
if (! $ldapserver->userIsAllowedLogin($login['dn']))
	system_message(array(
		'title'=>_('Authenticate to server'),
		'body'=>_('Sorry, you are not allowed to use phpLDAPadmin with this LDAP server.'),
		'type'=>'error'),
		sprintf('cmd.php?cmd=login_form&server_id=%s',$ldapserver->server_id));

if (DEBUG_ENABLED)
	debug_log('User is not prohibited from logging in - now bind with DN [%s]',64,__FILE__,__LINE__,__METHOD__,$login['dn']);

# Verify that the login is good
if (is_null($login['dn']) && is_null($login['pass']))
	$ds = $ldapserver->connect(false,'anonymous',true);
else
	$ds = $ldapserver->connect(false,'user',true);

if (DEBUG_ENABLED)
	debug_log('Connection returned [%s]',64,__FILE__,__LINE__,__METHOD__,$ds);

if (! is_resource($ds)) {
	if ($anon_bind)
		system_message(array(
			'title'=>_('Authenticate to server'),
			'body'=>_('Could not bind anonymously to server.'),
			'type'=>'error'),
			sprintf('cmd.php?cmd=login_form&server_id=%s',$ldapserver->server_id));

	else
		system_message(array(
			'title'=>_('Authenticate to server'),
			'body'=>_('Bad username or password. Please try again.'),
			'type'=>'error'),
			sprintf('cmd.php?cmd=login_form&server_id=%s',$ldapserver->server_id));

	syslog_notice(sprintf('Authentification FAILED for %s',$login['dn']));
}

$ldapserver->auth_type = $save_auth_type;
$ldapserver->setLoginDN($login['dn'],$login['pass'],$anon_bind) or error(_('Could not set cookie.'),'error','index.php');
set_lastactivity($ldapserver);

if (! $anon_bind) {
	syslog_notice(sprintf('Authentification successful for %s',$login['dn']));
}

# Since we were successful, clear the cache so that it will be refreshed with the new creditentials.
del_cached_item($ldapserver->server_id,'tree','null');

system_message(array(
	'title'=>_('Authenticate to server'),
	'body'=>_('Successfully logged into server.').($anon_bind ? sprintf(' (%s)',_('Anonymous Bind')) : ''),
	'type'=>'info'),
	sprintf('index.php?server_id=%s',$ldapserver->server_id));
?>