1
|
<?php
|
2
|
|
3
|
|
4
|
|
5
|
|
6
|
|
7
|
|
8
|
|
9
|
require_once 'functions.inc';
|
10
|
|
11
|
$config = ldap_help_config();
|
12
|
|
13
|
ldap_help_display(NULL, "------------------------------------------\n");
|
14
|
ldap_help_display(NULL, LDAP_SCRIPTS_COMMAND_LINE_WARNING . "\n");
|
15
|
|
16
|
ldap_help_display(NULL, "------------------------------------------\nldap extension test\n------------------------------------------");
|
17
|
ldap_help_display("PHP Version", phpversion());
|
18
|
ldap_help_display('LDAP Extension Loaded', (extension_loaded('ldap')) ? 'yes' : 'no');
|
19
|
ldap_help_display(NULL, ldap_help_parsePHPModules());
|
20
|
|
21
|
if (!extension_loaded('ldap')) {
|
22
|
die('PHP LDAP extension not loaded. Can not run tests. Check your php.ini and make sure ldap extension is avaialable.');
|
23
|
}
|
24
|
|
25
|
foreach ($config['servers'] as $sid => $server) {
|
26
|
|
27
|
|
28
|
|
29
|
|
30
|
$results = ldap_help_connect($server['server_address'], $server['server_port'], $server['server_tls'], TRUE);
|
31
|
$test_name = "\"" . $sid . "\"";
|
32
|
|
33
|
ldap_help_display(NULL, "------------------------------------------\n$test_name connect\n------------------------------------------");
|
34
|
$tls = ($server['server_tls']) ? 'yes' : 'no';
|
35
|
ldap_help_display('tls', $tls);
|
36
|
|
37
|
$anon_bind = ($server['server_bind_method'] == LDAP_SERVERS_BIND_METHOD_ANON);
|
38
|
$anon_bind_text = ($anon_bind) ? 'yes' : 'no';
|
39
|
ldap_help_display('anonymous bind', $anon_bind_text);
|
40
|
|
41
|
ldap_help_display('connect result', $results[1]);
|
42
|
ldap_help_display('connect context', join("", ["server: ", $server['server_address'], ", port: ", $server['server_port'], ", tls= $tls"]));
|
43
|
$con = FALSE;
|
44
|
if ($results[0] == LDAP_SUCCESS) {
|
45
|
$con = $results[2];
|
46
|
}
|
47
|
elseif ($results[0] == LDAP_OTHER) {
|
48
|
$con = $results[2];
|
49
|
}
|
50
|
else {
|
51
|
ldap_help_disconnect($con);
|
52
|
continue;
|
53
|
}
|
54
|
|
55
|
|
56
|
|
57
|
|
58
|
|
59
|
ldap_help_display(NULL, "------------------------------------------\n$test_name bind\n------------------------------------------");
|
60
|
|
61
|
if ($anon_bind) {
|
62
|
if (@!ldap_bind($con)) {
|
63
|
$results = [ldap_errno($con), "LDAP anonymous bind error." . ldap_help_show_error($con)];
|
64
|
}
|
65
|
}
|
66
|
else {
|
67
|
$bind_result = @ldap_bind($con, $server['server_bind_dn'], $server['server_bind_pw']);
|
68
|
if (!$bind_result) {
|
69
|
$results = [ldap_errno($con), "LDAP bind failure for user " . $server['server_bind_dn'] . "." . ldap_help_show_error($con)];
|
70
|
}
|
71
|
else {
|
72
|
$results = [LDAP_SUCCESS, "LDAP bind success."];
|
73
|
}
|
74
|
}
|
75
|
|
76
|
ldap_help_display('bind result', $results[1]);
|
77
|
ldap_help_display('bind dn', $server['server_bind_dn']);
|
78
|
if ($results[0] != LDAP_SUCCESS) {
|
79
|
continue;
|
80
|
}
|
81
|
|
82
|
|
83
|
|
84
|
|
85
|
foreach ($server['test_queries'] as $query_id => $query) {
|
86
|
ldap_help_display(NULL, "------------------------------------------\n$test_name query \"$query_id\" \n------------------------------------------");
|
87
|
|
88
|
$filter = $query['filter'];
|
89
|
ldap_help_display('search base_dn', $server['server_base_dn']);
|
90
|
ldap_help_display('search filter', $filter);
|
91
|
ldap_help_display('server_address', $server['server_address']);
|
92
|
ldap_help_display('server_port', $server['server_port']);
|
93
|
ldap_help_display('tls', $tls);
|
94
|
|
95
|
$query_result = @ldap_search($con, $server['server_base_dn'], $filter);
|
96
|
if (!$query_result) {
|
97
|
ldap_help_display(ldap_errno($con), "LDAP search failure for user $filter." . ldap_help_show_error($con));
|
98
|
}
|
99
|
|
100
|
else {
|
101
|
$entries = ldap_get_entries($con, $query_result);
|
102
|
ldap_help_display('search result');
|
103
|
if (is_array($entries)) {
|
104
|
$entry_count = $entries['count'];
|
105
|
if ($entry_count == 0) {
|
106
|
ldap_help_display('no entries found');
|
107
|
}
|
108
|
else {
|
109
|
for ($j = 0; $j < $entry_count; $j++) {
|
110
|
$entry = $entries[$j];
|
111
|
$attr_count = $entry['count'];
|
112
|
ldap_help_display(NULL, "\nsearch results, entry[$j]:");
|
113
|
ldap_help_display(' dn[' . $j . ']', $entry['dn']);
|
114
|
for ($i = 0; $i < $attr_count; $i++) {
|
115
|
$attr_name = $entry[$i];
|
116
|
if (in_array($attr_name, $query['show_attr'])) {
|
117
|
$values_count = $entry[$attr_name]['count'];
|
118
|
for ($k = 0; $k < $values_count; $k++) {
|
119
|
ldap_help_display(' ' . $attr_name . '[' . $k . ']', $entry[$attr_name][$k]);
|
120
|
}
|
121
|
}
|
122
|
}
|
123
|
}
|
124
|
}
|
125
|
}
|
126
|
}
|
127
|
}
|
128
|
|
129
|
|
130
|
|
131
|
|
132
|
foreach ($server['test_provisions'] as $provision_id => $provision) {
|
133
|
ldap_help_display(NULL, "------------------------------------------\n$test_name provision \"$provision_id\"\n------------------------------------------");
|
134
|
|
135
|
$dn = $provision['dn'];
|
136
|
|
137
|
ldap_help_display(NULL, "\nprovision, entry[$dn]:");
|
138
|
if ($provision['delete_if_exists']) {
|
139
|
$query_result = @ldap_search($con, $server['server_base_dn'], $provision['find_filter']);
|
140
|
if ($query_result) {
|
141
|
$entries = ldap_get_entries($con, $query_result);
|
142
|
if ($entries['count'] == 1) {
|
143
|
$result = @ldap_delete($con, $dn);
|
144
|
if ($result) {
|
145
|
ldap_help_display('deleted existing entry', $dn);
|
146
|
}
|
147
|
else {
|
148
|
ldap_help_display('failed to delete existing entry in provision', $dn);
|
149
|
continue;
|
150
|
}
|
151
|
}
|
152
|
else {
|
153
|
|
154
|
}
|
155
|
}
|
156
|
}
|
157
|
|
158
|
|
159
|
$result = @ldap_add($con, $dn, $provision['attr']);
|
160
|
$show_result = $result ? 'success' : 'fail';
|
161
|
ldap_help_display('provision result', $show_result);
|
162
|
if (!$result) {
|
163
|
ldap_help_display('provision error', ldap_help_show_error($con));
|
164
|
}
|
165
|
}
|
166
|
|
167
|
}
|