1
|
<?php
|
2
|
|
3
|
|
4
|
/**
|
5
|
* @file
|
6
|
* test script functions for testing ldap functionality outside of Drupal
|
7
|
*
|
8
|
*/
|
9
|
|
10
|
error_reporting(E_ALL | E_STRICT);
|
11
|
set_time_limit (0);
|
12
|
ini_set("display_errors", 1);
|
13
|
ini_set("max_execution_time", 0);
|
14
|
|
15
|
define('LDAP_SERVERS_BIND_METHOD_SERVICE_ACCT', 1);
|
16
|
define('LDAP_SERVERS_BIND_METHOD_ANON', 3);
|
17
|
|
18
|
define('LDAP_FAIL', -1);
|
19
|
|
20
|
define('LDAP_SUCCESS', 0x00);
|
21
|
define('LDAP_OPERATIONS_ERROR', 0x01);
|
22
|
define('LDAP_PROTOCOL_ERROR', 0x02);
|
23
|
define('LDAP_TIMELIMIT_EXCEEDED', 0x03);
|
24
|
define('LDAP_SIZELIMIT_EXCEEDED', 0x04);
|
25
|
define('LDAP_COMPARE_FALSE', 0x05);
|
26
|
define('LDAP_COMPARE_TRUE', 0x06);
|
27
|
define('LDAP_AUTH_METHOD_NOT_SUPPORTED', 0x07);
|
28
|
define('LDAP_STRONG_AUTH_REQUIRED', 0x08);
|
29
|
//NotusedinLDAPv3);
|
30
|
define('LDAP_PARTIAL_RESULTS', 0x09);
|
31
|
|
32
|
//Next5newinLDAPv3);
|
33
|
define('LDAP_REFERRAL', 0x0a);
|
34
|
define('LDAP_ADMINLIMIT_EXCEEDED', 0x0b);
|
35
|
define('LDAP_UNAVAILABLE_CRITICAL_EXTENSION', 0x0c);
|
36
|
define('LDAP_CONFIDENTIALITY_REQUIRED', 0x0d);
|
37
|
define('LDAP_SASL_BIND_INPROGRESS', 0x0e);
|
38
|
|
39
|
define('LDAP_NO_SUCH_ATTRIBUTE', 0x10);
|
40
|
define('LDAP_UNDEFINED_TYPE', 0x11);
|
41
|
define('LDAP_INAPPROPRIATE_MATCHING', 0x12);
|
42
|
define('LDAP_CONSTRAINT_VIOLATION', 0x13);
|
43
|
define('LDAP_TYPE_OR_VALUE_EXISTS', 0x14);
|
44
|
define('LDAP_INVALID_SYNTAX', 0x15);
|
45
|
|
46
|
define('LDAP_NO_SUCH_OBJECT', 0x20);
|
47
|
define('LDAP_ALIAS_PROBLEM', 0x21);
|
48
|
define('LDAP_INVALID_DN_SYNTAX', 0x22);
|
49
|
|
50
|
define('LDAP_IS_LEAF', 0x23);
|
51
|
define('LDAP_ALIAS_DEREF_PROBLEM', 0x24);
|
52
|
|
53
|
define('LDAP_INAPPROPRIATE_AUTH', 0x30);
|
54
|
define('LDAP_INVALID_CREDENTIALS', 0x31);
|
55
|
define('LDAP_INSUFFICIENT_ACCESS', 0x32);
|
56
|
define('LDAP_BUSY', 0x33);
|
57
|
define('LDAP_UNAVAILABLE', 0x34);
|
58
|
define('LDAP_UNWILLING_TO_PERFORM', 0x35);
|
59
|
define('LDAP_LOOP_DETECT', 0x36);
|
60
|
|
61
|
define('LDAP_SORT_CONTROL_MISSING', 0x3C);
|
62
|
define('LDAP_INDEX_RANGE_ERROR', 0x3D);
|
63
|
|
64
|
define('LDAP_NAMING_VIOLATION', 0x40);
|
65
|
define('LDAP_OBJECT_CLASS_VIOLATION', 0x41);
|
66
|
define('LDAP_NOT_ALLOWED_ON_NONLEAF', 0x42);
|
67
|
define('LDAP_NOT_ALLOWED_ON_RDN', 0x43);
|
68
|
define('LDAP_ALREADY_EXISTS', 0x44);
|
69
|
define('LDAP_NO_OBJECT_CLASS_MODS', 0x45);
|
70
|
define('LDAP_RESULTS_TOO_LARGE', 0x46);
|
71
|
//NexttwoforLDAPv3);
|
72
|
define('LDAP_AFFECTS_MULTIPLE_DSAS', 0x47);
|
73
|
define('LDAP_OTHER', 0x50);
|
74
|
|
75
|
//UsedbysomeAPIs);
|
76
|
define('LDAP_SERVER_DOWN', 0x51);
|
77
|
define('LDAP_LOCAL_ERROR', 0x52);
|
78
|
define('LDAP_ENCODING_ERROR', 0x53);
|
79
|
define('LDAP_DECODING_ERROR', 0x54);
|
80
|
define('LDAP_TIMEOUT', 0x55);
|
81
|
define('LDAP_AUTH_UNKNOWN', 0x56);
|
82
|
define('LDAP_FILTER_ERROR', 0x57);
|
83
|
define('LDAP_USER_CANCELLED', 0x58);
|
84
|
define('LDAP_PARAM_ERROR', 0x59);
|
85
|
define('LDAP_NO_MEMORY', 0x5a);
|
86
|
|
87
|
//PreliminaryLDAPv3codes);
|
88
|
define('LDAP_CONNECT_ERROR', 0x5b);
|
89
|
define('LDAP_NOT_SUPPORTED', 0x5c);
|
90
|
define('LDAP_CONTROL_NOT_FOUND', 0x5d);
|
91
|
define('LDAP_NO_RESULTS_RETURNED', 0x5e);
|
92
|
define('LDAP_MORE_RESULTS_TO_RETURN', 0x5f);
|
93
|
define('LDAP_CLIENT_LOOP', 0x60);
|
94
|
define('LDAP_REFERRAL_LIMIT_EXCEEDED', 0x61);
|
95
|
|
96
|
|
97
|
//error_reporting(E_ALL & ~E_NOTICE);
|
98
|
|
99
|
define('LDAP_SCRIPTS_COMMAND_LINE_WARNING', "Warning: PHP from the command line may have different PHP versions, php.ini files, and security context than running in a webserver context. This may produce false test results since Drupal LDAP Modules are run in the web server context.");
|
100
|
|
101
|
require_once('config.inc');
|
102
|
|
103
|
function ldap_help_connect($address, $port, $tls, $test = FALSE) {
|
104
|
|
105
|
if ($test) {
|
106
|
$false_con = ldap_connect("fakehostname.sdfserewerdfsdf.com", 389); // test for ldap extensions that don't actually connect until bind
|
107
|
if (ldap_errno($false_con) == LDAP_SUCCESS) {
|
108
|
$con = ldap_connect($address, $port);
|
109
|
return array(LDAP_OTHER, "ldap_connect does not actually connect until bind with installed extension, so connect is not a valid test.", $con);
|
110
|
}
|
111
|
}
|
112
|
|
113
|
$con = ldap_connect($address, $port);
|
114
|
|
115
|
if (!$con || ldap_errno($con) != LDAP_SUCCESS) {
|
116
|
$err = ldap_errno($con) . ":" . ldap_error($con) . ":" . ldap_err2str(ldap_errno($con)) . "!";
|
117
|
return array(LDAP_CONNECT_ERROR, "LDAP Connect failure to $address : $port. $err");
|
118
|
}
|
119
|
|
120
|
// Use TLS if we are configured and able to.
|
121
|
if ($tls) {
|
122
|
ldap_get_option($con, LDAP_OPT_PROTOCOL_VERSION, $vers);
|
123
|
if ($vers == -1) {
|
124
|
return array(LDAP_PROTOCOL_ERROR, "Could not get LDAP protocol version.");
|
125
|
}
|
126
|
if ($vers != 3) {
|
127
|
return array(LDAP_CONNECT_ERROR, 'Could not start TLS, only supported by LDAP v3.');
|
128
|
}
|
129
|
elseif (!function_exists('ldap_start_tls')) {
|
130
|
return array(LDAP_CONNECT_ERROR, 'Could not start TLS. It does not seem to be supported by this PHP setup.');
|
131
|
}
|
132
|
elseif (!ldap_start_tls($con)) {
|
133
|
return array(LDAP_CONNECT_ERROR, "Could not start TLS. (Error " . ldap_errno($con) . ":" . ldap_error($con) . ").");
|
134
|
}
|
135
|
}
|
136
|
|
137
|
return array(LDAP_SUCCESS, "Successful Connection!", $con);
|
138
|
}
|
139
|
|
140
|
function ldap_help_show_error($con) {
|
141
|
return "\nLDAP Error Number: " . ldap_errno($con) . "\nLDAP Error Description: " . ldap_error($con);
|
142
|
}
|
143
|
|
144
|
function ldap_help_display($title, $value = NULL) {
|
145
|
if (is_array($value)) {
|
146
|
echo "\n" . $title;
|
147
|
foreach ($title as $subtitle => $subvalue) {
|
148
|
ldap_help_display($subtitle, $subvalue);
|
149
|
}
|
150
|
}
|
151
|
if (!$title && $value) {
|
152
|
echo "\n" . $value;
|
153
|
}
|
154
|
elseif ((int)$title === $title) {
|
155
|
echo "\n" . $value;
|
156
|
}
|
157
|
else {
|
158
|
echo "\n" . "$title: $value";
|
159
|
}
|
160
|
|
161
|
}
|
162
|
/**
|
163
|
* Disconnect (unbind) from an active LDAP server.
|
164
|
*/
|
165
|
function ldap_help_disconnect(&$con) {
|
166
|
if (!$con) {
|
167
|
// never bound or not currently bound, so no need to disconnect
|
168
|
}
|
169
|
else {
|
170
|
ldap_help_disconnect($con);
|
171
|
$con = NULL;
|
172
|
}
|
173
|
}
|
174
|
|
175
|
/** parse php modules from phpinfo */
|
176
|
function ldap_help_parsePHPModules() {
|
177
|
ob_start();
|
178
|
phpinfo(INFO_MODULES);
|
179
|
$s = ob_get_contents();
|
180
|
ob_end_clean();
|
181
|
$matches = array();
|
182
|
preg_match_all("/(\nLDAP Support.*Vendor Version[^\n]*?).*$/iDsU", $s, $matches);
|
183
|
return isset($matches[1][0]) ? "\nphpinfo() LDAP Info:" . $matches[1][0] : '';
|
184
|
}
|
185
|
|
186
|
function ldap_help_encodePassword($password) {
|
187
|
$password = "\"" . $password . "\"";
|
188
|
$encoded = "";
|
189
|
for ($i = 0; $i < strlen($password); $i++) {
|
190
|
$encoded .= "{$password{$i}}\000";
|
191
|
}
|
192
|
return $encoded;
|
193
|
}
|