forked from LimeSurvey/LimeSurvey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldap-functions.php
317 lines (266 loc) · 11.6 KB
/
ldap-functions.php
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
/*
* LimeSurvey
* Copyright (C) 2007 The LimeSurvey Project Team / Carsten Schmitz
* All rights reserved.
* License: GNU/GPL License v2 or later, see LICENSE.php
* LimeSurvey is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*
* $Id: ldap-functions.php 8540 2010-03-31 11:37:19Z texens $
*/
// Security Checked: POST, GET, SESSION, REQUEST, returnglobal, DB
/*************** LDAP Functions *************/
/* */
/*********************************************/
function ldap_getCnx($server_id = null) {
global $ldap_server;
if ( is_null($server_id) ) {
return False;
}
else {
if ($ldap_server[$server_id]['protoversion'] == 'ldapv3' && $ldap_server[$server_id]['encrypt'] != 'ldaps') {
$ds = ldap_connect($ldap_server[$server_id]['server'], $ldap_server[$server_id]['port']);
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
if (! $ldap_server[$server_id]['referrals']) {
ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
}
if ($ldap_server[$server_id]['encrypt'] == 'start-tls' ) {
ldap_start_tls ($ds);
}
}
elseif ($ldap_server[$server_id]['protoversion'] == 'ldapv2') {
if ($ldap_server[$server_id]['encrypt'] == 'ldaps') {
$ds = ldap_connect("ldaps://".$ldap_server[$server_id]['server'], $ldap_server[$server_id]['port']);
}
else {
$ds = ldap_connect($ldap_server[$server_id]['server'], $ldap_server[$server_id]['port']);
}
if (! $ldap_server[$server_id]['referrals']) {
ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
}
}
return $ds;
}
}
function ldap_bindCnx($ds, $server_id = null) {
global $ldap_server;
$resbind=0;
if ( !$ds || is_null($server_id) ) {
return 0;
}
if ( isset($ldap_server[$server_id]['binddn']) && isset($ldap_server[$server_id]['bindpw']) ) {
$resbind=@ldap_bind($ds,
$ldap_server[$server_id]['binddn'],
$ldap_server[$server_id]['bindpw']);
}
else {
$resbind=@ldap_bind($ds);
}
return $resbind;
}
function ldap_readattr($attr) {
if (is_array($attr)) {
return trim($attr[0]);
}
else {
return trim($attr);
}
}
function ldap_search_withScope($ds, $basedn, $filter, $attrlist, $scope) {
if ( $scope == "base" ) {
$search = ldap_read($ds, $basedn, $filter, $attrlist);
}
elseif ( $scope == "one" ) {
$search = ldap_list($ds, $basedn, $filter, $attrlist);
}
elseif ( $scope == "sub" ) {
$search = ldap_search($ds, $basedn, $filter, $attrlist);
}
return $search;
}
function ldap_doTokenSearch($ds, $ldapq, &$ResArray) {
global $ldap_queries,$surveyid;
$totalrescount=0;
$userattrs=array();
// First let's lowercase the ldap query values
prepareLdapQuery($ldapq);
// Retrieve the ldap user attribute-list to read
$userparams = array('firstname_attr','lastname_attr',
'email_attr','token_attr', 'language');
// 'attr1', 'attr2');
$aTokenAttr=GetAttributeFieldNames($surveyid);
foreach ($aTokenAttr as $thisattrfieldname)
{
$attridx=substr($thisattrfieldname,10); // the 'attribute_' prefix is 10 chars long
$userparams[] = "attr".$attridx;
}
foreach ($userparams as $id => $attr) {
if (array_key_exists($attr,$ldap_queries[$ldapq]) &&
$ldap_queries[$ldapq][$attr] != '') {
$userattrs[]=$ldap_queries[$ldapq][$attr];
}
}
// If ldap group filtering is required
if (isset($ldap_queries[$ldapq]['groupfilter']) &&
$ldap_queries[$ldapq]['groupfilter'] != '') {
$userCandidates=array(); // list of candidates
$groupscope='sub'; // subtree search unless specified
if (isset($ldap_queries[$ldapq]['groupscope']) &&
$ldap_queries[$ldapq]['groupscope'] != '') {
$groupscope=$ldap_queries[$ldapq]['groupscope'];
}
$groupmemberattr='member'; //use 'member' attribute unless specified
if (isset($ldap_queries[$ldapq]['groupmemberattr']) &&
$ldap_queries[$ldapq]['groupmemberattr'] != '') {
$groupmemberattr=$ldap_queries[$ldapq]['groupmemberattr'];
}
// Search for group candidates
$search_groups=ldap_search_withScope($ds,
$ldap_queries[$ldapq]['groupbase'],
$ldap_queries[$ldapq]['groupfilter'],
array($groupmemberattr),
$groupscope);
$rescount=@ldap_count_entries($ds,$search_groups);
if ($rescount >= 1) { // at least 1 group was selected
$group_info=ldap_get_entries($ds, $search_groups);
// For each group candidate add members's id to $userCandidates[]
for ($i=0;$i<$group_info["count"];$i++) {
for ($j=0;$j<$group_info[$i][$groupmemberattr]["count"];$j++) {
// Only add the user's id if not already listed
// (avoids duplicates if this user is in several groups)
if (! in_array($group_info[$i][$groupmemberattr][$j],
$userCandidates)) {
$userCandidates[]=$group_info[$i][$groupmemberattr][$j];
}
}
}
// For each user, apply userfilter if defined
// and get user attrs
foreach ($userCandidates as $key => $user) {
$user_is_dn=TRUE; // Suppose group members are DNs by default
if (isset($ldap_queries[$ldapq]['groupmemberisdn']) &&
$ldap_queries[$ldapq]['groupmemberisdn'] == False) {
$user_is_dn=False;
}
if ($user_is_dn) {
// If group members are DNs
// Set userfilter (no filter by default)
$userfilter='(objectclass=*)';
if (isset($ldap_queries[$ldapq]['userfilter']) &&
$ldap_queries[$ldapq]['userfilter'] != '') {
$userfilter=$ldap_queries[$ldapq]['userfilter'];
}
$userscope='sub'; // subtree search unless specified
if (isset($ldap_queries[$ldapq]['userscope']) &&
$ldap_queries[$ldapq]['userscope'] != '') {
$userscope=$ldap_queries[$ldapq]['userscope'];
}
// If a userbase is defined, then get user's RND
// and do a user search based on this RDN
// Note: User's RDN is supposed to be made
// of only ONE attribute by this function
if (isset($ldap_queries[$ldapq]['userbase']) &&
$ldap_queries[$ldapq]['userbase'] != '') {
// get user's rdn
$user_dn_tab=explode(",", $user);
$user_rdn=$user_dn_tab[0];
$userfilter_rdn="(&("
.$user_rdn.")".$userfilter.")";
$search_users=ldap_search_withScope($ds,
$ldap_queries[$ldapq]['userbase'],
$userfilter_rdn,
$userattrs,
$userscope);
$rescount=@ldap_count_entries($ds,$search_users);
if ($rescount >= 1) {
// DN match criteria
// add to result array
$user_info=@ldap_get_entries($ds, $search_users);
for ($i=0;$i<$rescount;$i++) {
if ($user_info[$i]['dn'] == $user) {
$ResArray[]=$user_info;
$totalrescount++;
}
}
}
} // End of Member is DN and a userbase is defined
else {
// There is no userbase defined
// Only apply userfilter to the user's DN
$search_users=ldap_search_withScope($ds,
$user,
$userfilter,
$userattrs,
'base');
$rescount=@ldap_count_entries($ds,$search_users);
if ($rescount >= 1) {
// DN match criteria, add result to the result Array
$userentry=ldap_get_entries($ds, $search_users);
$ResArray[]=$userentry;
$totalrescount++;
}
} // End of Member is DN and a userbase is NOT defined
} // End of the member are DNs case
else {
//$user is the user ID, not a DN
// Search given userid combined with userfilter
// Set userfilter ('open filter' by default)
$userfilter='(objectclass=*)';
if (isset($ldap_queries[$ldapq]['userfilter']) &&
$ldap_queries[$ldapq]['userfilter'] != '') {
$userfilter=$ldap_queries[$ldapq]['userfilter'];
}
// Build the user filter from the RDN
$userfilter_notdn="(&("
.$ldap_queries[$ldapq]['useridattr']."=".$user.")"
.$userfilter.")";
$search_users=ldap_search_withScope($ds,
$ldap_queries[$ldapq]['userbase'],
$userfilter_notdn,
$userattrs,
$ldap_queries[$ldapq]['userscope']);
$rescount=@ldap_count_entries($ds,$search_users);
if ($rescount >= 1) {
// user matches criteria, add result to the result Array
$user_info=ldap_get_entries($ds, $search_users);
$ResArray[]=$user_info;
$totalrescount+=$rescount;
}
} // End of the members are not DN case
} // End of foreach user member in the group
} // End of foreach group
} // End of GroupSearches
else {
// No groupfilter is defined
// Apply a simple userfilter then
$userscope='sub'; // default to subtree search
if (isset($ldap_queries[$ldapq]['userscope']) &&
$ldap_queries[$ldapq]['userscope'] != '') {
$userscope=$ldap_queries[$ldapq]['userscope'];
}
$search_result = ldap_search_withScope($ds,
$ldap_queries[$ldapq]['userbase'],
$ldap_queries[$ldapq]['userfilter'],
$userattrs,
$userscope);
$rescount=ldap_count_entries($ds,$search_result);
if ( $rescount >= 1) {
$user_info = ldap_get_entries($ds, $search_result);
$ResArray[]=$user_info;
$totalrescount+=$rescount;
}
} // End of no group filtering
return $totalrescount;
}
function prepareLdapQuery($queryId)
{
global $ldap_queries;
$QueryName=$ldap_queries[$queryId]['name'];
$ldap_queries[$queryId] = array_map('strtolower',$ldap_queries[$queryId]);
$ldap_queries[$queryId]['name']=$QueryName;
}
?>