xref: /PHP-7.0/ext/ldap/ldap.c (revision 49782c54)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2017 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Amitay Isaacs  <amitay@w-o-i.com>                           |
16    |          Eric Warnke    <ericw@albany.edu>                           |
17    |          Rasmus Lerdorf <rasmus@php.net>                             |
18    |          Gerrit Thomson <334647@swin.edu.au>                         |
19    |          Jani Taskinen  <sniper@iki.fi>                              |
20    |          Stig Venaas    <venaas@uninett.no>                          |
21    |          Doug Goldstein <cardoe@cardoe.com>                          |
22    | PHP 4.0 updates:  Zeev Suraski <zeev@zend.com>                       |
23    +----------------------------------------------------------------------+
24  */
25 
26 /* $Id: 1c9340c77777426d718a7cb098b59b2827d2c21e $ */
27 #define IS_EXT_MODULE
28 
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 
33 /* Additional headers for NetWare */
34 #if defined(NETWARE) && (NEW_LIBC)
35 #include <sys/select.h>
36 #include <sys/timeval.h>
37 #endif
38 
39 #include "php.h"
40 #include "php_ini.h"
41 
42 #include <stddef.h>
43 
44 #include "ext/standard/dl.h"
45 #include "php_ldap.h"
46 
47 #ifdef PHP_WIN32
48 #include <string.h>
49 #include "config.w32.h"
50 #if HAVE_NSLDAP
51 #include <winsock2.h>
52 #endif
53 #define strdup _strdup
54 #undef WINDOWS
55 #undef strcasecmp
56 #undef strncasecmp
57 #define WINSOCK 1
58 #define __STDC__ 1
59 #endif
60 
61 #include "ext/standard/php_string.h"
62 #include "ext/standard/info.h"
63 
64 #ifdef HAVE_LDAP_SASL_H
65 #include <sasl.h>
66 #elif defined(HAVE_LDAP_SASL_SASL_H)
67 #include <sasl/sasl.h>
68 #endif
69 
70 #define PHP_LDAP_ESCAPE_FILTER 0x01
71 #define PHP_LDAP_ESCAPE_DN     0x02
72 
73 #if defined(LDAP_CONTROL_PAGEDRESULTS) && !defined(HAVE_LDAP_CONTROL_FIND)
ldap_control_find(const char * oid,LDAPControl ** ctrls,LDAPControl *** nextctrlp)74 LDAPControl *ldap_control_find( const char *oid, LDAPControl **ctrls, LDAPControl ***nextctrlp)
75 {
76 	assert(nextctrlp == NULL);
77 	return ldap_find_control(oid, ctrls);
78 }
79 #endif
80 
81 #if !defined(LDAP_API_FEATURE_X_OPENLDAP)
ldap_memvfree(void ** v)82 void ldap_memvfree(void **v)
83 {
84 	ldap_value_free((char **)v);
85 }
86 #endif
87 
88 typedef struct {
89 	LDAP *link;
90 #if defined(LDAP_API_FEATURE_X_OPENLDAP) && defined(HAVE_3ARG_SETREBINDPROC)
91 	zval rebindproc;
92 #endif
93 } ldap_linkdata;
94 
95 typedef struct {
96 	LDAPMessage *data;
97 	BerElement  *ber;
98 	zval         res;
99 } ldap_resultentry;
100 
101 ZEND_DECLARE_MODULE_GLOBALS(ldap)
102 static PHP_GINIT_FUNCTION(ldap);
103 
104 static int le_link, le_result, le_result_entry;
105 
106 #ifdef COMPILE_DL_LDAP
ZEND_GET_MODULE(ldap)107 ZEND_GET_MODULE(ldap)
108 #endif
109 
110 static void _close_ldap_link(zend_resource *rsrc) /* {{{ */
111 {
112 	ldap_linkdata *ld = (ldap_linkdata *)rsrc->ptr;
113 
114 	ldap_unbind_ext(ld->link, NULL, NULL);
115 #if defined(LDAP_API_FEATURE_X_OPENLDAP) && defined(HAVE_3ARG_SETREBINDPROC)
116 	zval_ptr_dtor(&ld->rebindproc);
117 #endif
118 
119 	efree(ld);
120 	LDAPG(num_links)--;
121 }
122 /* }}} */
123 
_free_ldap_result(zend_resource * rsrc)124 static void _free_ldap_result(zend_resource *rsrc) /* {{{ */
125 {
126 	LDAPMessage *result = (LDAPMessage *)rsrc->ptr;
127 	ldap_msgfree(result);
128 }
129 /* }}} */
130 
_free_ldap_result_entry(zend_resource * rsrc)131 static void _free_ldap_result_entry(zend_resource *rsrc) /* {{{ */
132 {
133 	ldap_resultentry *entry = (ldap_resultentry *)rsrc->ptr;
134 
135 	if (entry->ber != NULL) {
136 		ber_free(entry->ber, 0);
137 		entry->ber = NULL;
138 	}
139 	zval_ptr_dtor(&entry->res);
140 	efree(entry);
141 }
142 /* }}} */
143 
144 /* {{{ PHP_INI_BEGIN
145  */
146 PHP_INI_BEGIN()
147 	STD_PHP_INI_ENTRY_EX("ldap.max_links", "-1", PHP_INI_SYSTEM, OnUpdateLong, max_links, zend_ldap_globals, ldap_globals, display_link_numbers)
PHP_INI_END()148 PHP_INI_END()
149 /* }}} */
150 
151 /* {{{ PHP_GINIT_FUNCTION
152  */
153 static PHP_GINIT_FUNCTION(ldap)
154 {
155 	ldap_globals->num_links = 0;
156 }
157 /* }}} */
158 
159 /* {{{ PHP_MINIT_FUNCTION
160  */
PHP_MINIT_FUNCTION(ldap)161 PHP_MINIT_FUNCTION(ldap)
162 {
163 	REGISTER_INI_ENTRIES();
164 
165 	/* Constants to be used with deref-parameter in php_ldap_do_search() */
166 	REGISTER_LONG_CONSTANT("LDAP_DEREF_NEVER", LDAP_DEREF_NEVER, CONST_PERSISTENT | CONST_CS);
167 	REGISTER_LONG_CONSTANT("LDAP_DEREF_SEARCHING", LDAP_DEREF_SEARCHING, CONST_PERSISTENT | CONST_CS);
168 	REGISTER_LONG_CONSTANT("LDAP_DEREF_FINDING", LDAP_DEREF_FINDING, CONST_PERSISTENT | CONST_CS);
169 	REGISTER_LONG_CONSTANT("LDAP_DEREF_ALWAYS", LDAP_DEREF_ALWAYS, CONST_PERSISTENT | CONST_CS);
170 
171 	/* Constants to be used with ldap_modify_batch() */
172 	REGISTER_LONG_CONSTANT("LDAP_MODIFY_BATCH_ADD", LDAP_MODIFY_BATCH_ADD, CONST_PERSISTENT | CONST_CS);
173 	REGISTER_LONG_CONSTANT("LDAP_MODIFY_BATCH_REMOVE", LDAP_MODIFY_BATCH_REMOVE, CONST_PERSISTENT | CONST_CS);
174 	REGISTER_LONG_CONSTANT("LDAP_MODIFY_BATCH_REMOVE_ALL", LDAP_MODIFY_BATCH_REMOVE_ALL, CONST_PERSISTENT | CONST_CS);
175 	REGISTER_LONG_CONSTANT("LDAP_MODIFY_BATCH_REPLACE", LDAP_MODIFY_BATCH_REPLACE, CONST_PERSISTENT | CONST_CS);
176 	REGISTER_STRING_CONSTANT("LDAP_MODIFY_BATCH_ATTRIB", LDAP_MODIFY_BATCH_ATTRIB, CONST_PERSISTENT | CONST_CS);
177 	REGISTER_STRING_CONSTANT("LDAP_MODIFY_BATCH_MODTYPE", LDAP_MODIFY_BATCH_MODTYPE, CONST_PERSISTENT | CONST_CS);
178 	REGISTER_STRING_CONSTANT("LDAP_MODIFY_BATCH_VALUES", LDAP_MODIFY_BATCH_VALUES, CONST_PERSISTENT | CONST_CS);
179 
180 #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP
181 	/* LDAP options */
182 	REGISTER_LONG_CONSTANT("LDAP_OPT_DEREF", LDAP_OPT_DEREF, CONST_PERSISTENT | CONST_CS);
183 	REGISTER_LONG_CONSTANT("LDAP_OPT_SIZELIMIT", LDAP_OPT_SIZELIMIT, CONST_PERSISTENT | CONST_CS);
184 	REGISTER_LONG_CONSTANT("LDAP_OPT_TIMELIMIT", LDAP_OPT_TIMELIMIT, CONST_PERSISTENT | CONST_CS);
185 #ifdef LDAP_OPT_NETWORK_TIMEOUT
186 	REGISTER_LONG_CONSTANT("LDAP_OPT_NETWORK_TIMEOUT", LDAP_OPT_NETWORK_TIMEOUT, CONST_PERSISTENT | CONST_CS);
187 #elif defined (LDAP_X_OPT_CONNECT_TIMEOUT)
188 	REGISTER_LONG_CONSTANT("LDAP_OPT_NETWORK_TIMEOUT", LDAP_X_OPT_CONNECT_TIMEOUT, CONST_PERSISTENT | CONST_CS);
189 #endif
190 #ifdef LDAP_OPT_TIMEOUT
191 	REGISTER_LONG_CONSTANT("LDAP_OPT_TIMEOUT", LDAP_OPT_TIMEOUT, CONST_PERSISTENT | CONST_CS);
192 #endif
193 	REGISTER_LONG_CONSTANT("LDAP_OPT_PROTOCOL_VERSION", LDAP_OPT_PROTOCOL_VERSION, CONST_PERSISTENT | CONST_CS);
194 	REGISTER_LONG_CONSTANT("LDAP_OPT_ERROR_NUMBER", LDAP_OPT_ERROR_NUMBER, CONST_PERSISTENT | CONST_CS);
195 	REGISTER_LONG_CONSTANT("LDAP_OPT_REFERRALS", LDAP_OPT_REFERRALS, CONST_PERSISTENT | CONST_CS);
196 #ifdef LDAP_OPT_RESTART
197 	REGISTER_LONG_CONSTANT("LDAP_OPT_RESTART", LDAP_OPT_RESTART, CONST_PERSISTENT | CONST_CS);
198 #endif
199 #ifdef LDAP_OPT_HOST_NAME
200 	REGISTER_LONG_CONSTANT("LDAP_OPT_HOST_NAME", LDAP_OPT_HOST_NAME, CONST_PERSISTENT | CONST_CS);
201 #endif
202 	REGISTER_LONG_CONSTANT("LDAP_OPT_ERROR_STRING", LDAP_OPT_ERROR_STRING, CONST_PERSISTENT | CONST_CS);
203 #ifdef LDAP_OPT_MATCHED_DN
204 	REGISTER_LONG_CONSTANT("LDAP_OPT_MATCHED_DN", LDAP_OPT_MATCHED_DN, CONST_PERSISTENT | CONST_CS);
205 #endif
206 	REGISTER_LONG_CONSTANT("LDAP_OPT_SERVER_CONTROLS", LDAP_OPT_SERVER_CONTROLS, CONST_PERSISTENT | CONST_CS);
207 	REGISTER_LONG_CONSTANT("LDAP_OPT_CLIENT_CONTROLS", LDAP_OPT_CLIENT_CONTROLS, CONST_PERSISTENT | CONST_CS);
208 #endif
209 #ifdef LDAP_OPT_DEBUG_LEVEL
210 	REGISTER_LONG_CONSTANT("LDAP_OPT_DEBUG_LEVEL", LDAP_OPT_DEBUG_LEVEL, CONST_PERSISTENT | CONST_CS);
211 #endif
212 
213 #ifdef LDAP_OPT_DIAGNOSTIC_MESSAGE
214 	REGISTER_LONG_CONSTANT("LDAP_OPT_DIAGNOSTIC_MESSAGE", LDAP_OPT_DIAGNOSTIC_MESSAGE, CONST_PERSISTENT | CONST_CS);
215 #endif
216 
217 #ifdef HAVE_LDAP_SASL
218 	REGISTER_LONG_CONSTANT("LDAP_OPT_X_SASL_MECH", LDAP_OPT_X_SASL_MECH, CONST_PERSISTENT | CONST_CS);
219 	REGISTER_LONG_CONSTANT("LDAP_OPT_X_SASL_REALM", LDAP_OPT_X_SASL_REALM, CONST_PERSISTENT | CONST_CS);
220 	REGISTER_LONG_CONSTANT("LDAP_OPT_X_SASL_AUTHCID", LDAP_OPT_X_SASL_AUTHCID, CONST_PERSISTENT | CONST_CS);
221 	REGISTER_LONG_CONSTANT("LDAP_OPT_X_SASL_AUTHZID", LDAP_OPT_X_SASL_AUTHZID, CONST_PERSISTENT | CONST_CS);
222 #endif
223 
224 #ifdef ORALDAP
225 	REGISTER_LONG_CONSTANT("GSLC_SSL_NO_AUTH", GSLC_SSL_NO_AUTH, CONST_PERSISTENT | CONST_CS);
226 	REGISTER_LONG_CONSTANT("GSLC_SSL_ONEWAY_AUTH", GSLC_SSL_ONEWAY_AUTH, CONST_PERSISTENT | CONST_CS);
227 	REGISTER_LONG_CONSTANT("GSLC_SSL_TWOWAY_AUTH", GSLC_SSL_TWOWAY_AUTH, CONST_PERSISTENT | CONST_CS);
228 #endif
229 
230 #if (LDAP_API_VERSION > 2000)
231 	REGISTER_LONG_CONSTANT("LDAP_OPT_X_TLS_REQUIRE_CERT", LDAP_OPT_X_TLS_REQUIRE_CERT, CONST_PERSISTENT | CONST_CS);
232 
233 	REGISTER_LONG_CONSTANT("LDAP_OPT_X_TLS_NEVER", LDAP_OPT_X_TLS_NEVER, CONST_PERSISTENT | CONST_CS);
234 	REGISTER_LONG_CONSTANT("LDAP_OPT_X_TLS_HARD", LDAP_OPT_X_TLS_HARD, CONST_PERSISTENT | CONST_CS);
235 	REGISTER_LONG_CONSTANT("LDAP_OPT_X_TLS_DEMAND", LDAP_OPT_X_TLS_DEMAND, CONST_PERSISTENT | CONST_CS);
236 	REGISTER_LONG_CONSTANT("LDAP_OPT_X_TLS_ALLOW", LDAP_OPT_X_TLS_ALLOW, CONST_PERSISTENT | CONST_CS);
237 	REGISTER_LONG_CONSTANT("LDAP_OPT_X_TLS_TRY", LDAP_OPT_X_TLS_TRY, CONST_PERSISTENT | CONST_CS);
238 #endif
239 
240 	REGISTER_LONG_CONSTANT("LDAP_ESCAPE_FILTER", PHP_LDAP_ESCAPE_FILTER, CONST_PERSISTENT | CONST_CS);
241 	REGISTER_LONG_CONSTANT("LDAP_ESCAPE_DN", PHP_LDAP_ESCAPE_DN, CONST_PERSISTENT | CONST_CS);
242 
243 	le_link = zend_register_list_destructors_ex(_close_ldap_link, NULL, "ldap link", module_number);
244 	le_result = zend_register_list_destructors_ex(_free_ldap_result, NULL, "ldap result", module_number);
245 	le_result_entry = zend_register_list_destructors_ex(_free_ldap_result_entry, NULL, "ldap result entry", module_number);
246 
247 	ldap_module_entry.type = type;
248 
249 	return SUCCESS;
250 }
251 /* }}} */
252 
253 /* {{{ PHP_MSHUTDOWN_FUNCTION
254  */
PHP_MSHUTDOWN_FUNCTION(ldap)255 PHP_MSHUTDOWN_FUNCTION(ldap)
256 {
257 	UNREGISTER_INI_ENTRIES();
258 	return SUCCESS;
259 }
260 /* }}} */
261 
262 /* {{{ PHP_MINFO_FUNCTION
263  */
PHP_MINFO_FUNCTION(ldap)264 PHP_MINFO_FUNCTION(ldap)
265 {
266 	char tmp[32];
267 #if HAVE_NSLDAP
268 	LDAPVersion ver;
269 	double SDKVersion;
270 #endif
271 
272 	php_info_print_table_start();
273 	php_info_print_table_row(2, "LDAP Support", "enabled");
274 	php_info_print_table_row(2, "RCS Version", "$Id: 1c9340c77777426d718a7cb098b59b2827d2c21e $");
275 
276 	if (LDAPG(max_links) == -1) {
277 		snprintf(tmp, 31, ZEND_LONG_FMT "/unlimited", LDAPG(num_links));
278 	} else {
279 		snprintf(tmp, 31, ZEND_LONG_FMT "/" ZEND_LONG_FMT, LDAPG(num_links), LDAPG(max_links));
280 	}
281 	php_info_print_table_row(2, "Total Links", tmp);
282 
283 #ifdef LDAP_API_VERSION
284 	snprintf(tmp, 31, "%d", LDAP_API_VERSION);
285 	php_info_print_table_row(2, "API Version", tmp);
286 #endif
287 
288 #ifdef LDAP_VENDOR_NAME
289 	php_info_print_table_row(2, "Vendor Name", LDAP_VENDOR_NAME);
290 #endif
291 
292 #ifdef LDAP_VENDOR_VERSION
293 	snprintf(tmp, 31, "%d", LDAP_VENDOR_VERSION);
294 	php_info_print_table_row(2, "Vendor Version", tmp);
295 #endif
296 
297 #if HAVE_NSLDAP
298 	SDKVersion = ldap_version(&ver);
299 	snprintf(tmp, 31, "%F", SDKVersion/100.0);
300 	php_info_print_table_row(2, "SDK Version", tmp);
301 
302 	snprintf(tmp, 31, "%F", ver.protocol_version/100.0);
303 	php_info_print_table_row(2, "Highest LDAP Protocol Supported", tmp);
304 
305 	snprintf(tmp, 31, "%F", ver.SSL_version/100.0);
306 	php_info_print_table_row(2, "SSL Level Supported", tmp);
307 
308 	if (ver.security_level != LDAP_SECURITY_NONE) {
309 		snprintf(tmp, 31, "%d", ver.security_level);
310 	} else {
311 		strcpy(tmp, "SSL not enabled");
312 	}
313 	php_info_print_table_row(2, "Level of Encryption", tmp);
314 #endif
315 
316 #ifdef HAVE_LDAP_SASL
317 	php_info_print_table_row(2, "SASL Support", "Enabled");
318 #endif
319 
320 	php_info_print_table_end();
321 	DISPLAY_INI_ENTRIES();
322 }
323 /* }}} */
324 
325 /* {{{ proto resource ldap_connect([string host [, int port [, string wallet [, string wallet_passwd [, int authmode]]]]])
326    Connect to an LDAP server */
PHP_FUNCTION(ldap_connect)327 PHP_FUNCTION(ldap_connect)
328 {
329 	char *host = NULL;
330 	size_t hostlen = 0;
331 	zend_long port = LDAP_PORT;
332 #ifdef HAVE_ORALDAP
333 	char *wallet = NULL, *walletpasswd = NULL;
334 	size_t walletlen = 0, walletpasswdlen = 0;
335 	zend_long authmode = GSLC_SSL_NO_AUTH;
336 	int ssl=0;
337 #endif
338 	ldap_linkdata *ld;
339 	LDAP *ldap = NULL;
340 
341 #ifdef HAVE_ORALDAP
342 	if (ZEND_NUM_ARGS() == 3 || ZEND_NUM_ARGS() == 4) {
343 		WRONG_PARAM_COUNT;
344 	}
345 
346 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|slssl", &host, &hostlen, &port, &wallet, &walletlen, &walletpasswd, &walletpasswdlen, &authmode) != SUCCESS) {
347 		RETURN_FALSE;
348 	}
349 
350 	if (ZEND_NUM_ARGS() == 5) {
351 		ssl = 1;
352 	}
353 #else
354 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sl", &host, &hostlen, &port) != SUCCESS) {
355 		RETURN_FALSE;
356 	}
357 #endif
358 
359 	if (LDAPG(max_links) != -1 && LDAPG(num_links) >= LDAPG(max_links)) {
360 		php_error_docref(NULL, E_WARNING, "Too many open links (%pd)", LDAPG(num_links));
361 		RETURN_FALSE;
362 	}
363 
364 	ld = ecalloc(1, sizeof(ldap_linkdata));
365 
366 	{
367 		int rc = LDAP_SUCCESS;
368 		char	*url = host;
369 		if (url && !ldap_is_ldap_url(url)) {
370 			int	urllen = hostlen + sizeof( "ldap://:65535" );
371 
372 			if (port <= 0 || port > 65535) {
373 				efree(ld);
374 				php_error_docref(NULL, E_WARNING, "invalid port number: %ld", port);
375 				RETURN_FALSE;
376 			}
377 
378 			url = emalloc(urllen);
379 			snprintf( url, urllen, "ldap://%s:%ld", host, port );
380 		}
381 
382 #ifdef LDAP_API_FEATURE_X_OPENLDAP
383 		/* ldap_init() is deprecated, use ldap_initialize() instead.
384 		 */
385 		rc = ldap_initialize(&ldap, url);
386 #else /* ! LDAP_API_FEATURE_X_OPENLDAP */
387 		/* ldap_init does not support URLs.
388 		 * We must try the original host and port information.
389 		 */
390 		ldap = ldap_init(host, port);
391 		if (ldap == NULL) {
392 			efree(ld);
393 			php_error_docref(NULL, E_WARNING, "Could not create session handle");
394 			RETURN_FALSE;
395 		}
396 #endif /* ! LDAP_API_FEATURE_X_OPENLDAP */
397 		if (url != host) {
398 			efree(url);
399 		}
400 		if (rc != LDAP_SUCCESS) {
401 			efree(ld);
402 			php_error_docref(NULL, E_WARNING, "Could not create session handle: %s", ldap_err2string(rc));
403 			RETURN_FALSE;
404 		}
405 	}
406 
407 	if (ldap == NULL) {
408 		efree(ld);
409 		RETURN_FALSE;
410 	} else {
411 #ifdef HAVE_ORALDAP
412 		if (ssl) {
413 			if (ldap_init_SSL(&ldap->ld_sb, wallet, walletpasswd, authmode)) {
414 				efree(ld);
415 				php_error_docref(NULL, E_WARNING, "SSL init failed");
416 				RETURN_FALSE;
417 			}
418 		}
419 #endif
420 		LDAPG(num_links)++;
421 		ld->link = ldap;
422 		RETURN_RES(zend_register_resource(ld, le_link));
423 	}
424 
425 }
426 /* }}} */
427 
428 /* {{{ _get_lderrno
429  */
_get_lderrno(LDAP * ldap)430 static int _get_lderrno(LDAP *ldap)
431 {
432 #if !HAVE_NSLDAP
433 #if LDAP_API_VERSION > 2000 || HAVE_ORALDAP
434 	int lderr;
435 
436 	/* New versions of OpenLDAP do it this way */
437 	ldap_get_option(ldap, LDAP_OPT_ERROR_NUMBER, &lderr);
438 	return lderr;
439 #else
440 	return ldap->ld_errno;
441 #endif
442 #else
443 	return ldap_get_lderrno(ldap, NULL, NULL);
444 #endif
445 }
446 /* }}} */
447 
448 /* {{{ _set_lderrno
449  */
_set_lderrno(LDAP * ldap,int lderr)450 static void _set_lderrno(LDAP *ldap, int lderr)
451 {
452 #if !HAVE_NSLDAP
453 #if LDAP_API_VERSION > 2000 || HAVE_ORALDAP
454 	/* New versions of OpenLDAP do it this way */
455 	ldap_set_option(ldap, LDAP_OPT_ERROR_NUMBER, &lderr);
456 #else
457 	ldap->ld_errno = lderr;
458 #endif
459 #else
460 	ldap_set_lderrno(ldap, lderr, NULL, NULL);
461 #endif
462 }
463 /* }}} */
464 
465 /* {{{ proto bool ldap_bind(resource link [, string dn [, string password]])
466    Bind to LDAP directory */
PHP_FUNCTION(ldap_bind)467 PHP_FUNCTION(ldap_bind)
468 {
469 	zval *link;
470 	char *ldap_bind_dn = NULL, *ldap_bind_pw = NULL;
471 	size_t ldap_bind_dnlen, ldap_bind_pwlen;
472 	ldap_linkdata *ld;
473 	int rc;
474 
475 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|ss", &link, &ldap_bind_dn, &ldap_bind_dnlen, &ldap_bind_pw, &ldap_bind_pwlen) != SUCCESS) {
476 		RETURN_FALSE;
477 	}
478 
479 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
480 		RETURN_FALSE;
481 	}
482 
483 	if (ldap_bind_dn != NULL && memchr(ldap_bind_dn, '\0', ldap_bind_dnlen) != NULL) {
484 		_set_lderrno(ld->link, LDAP_INVALID_CREDENTIALS);
485 		php_error_docref(NULL, E_WARNING, "DN contains a null byte");
486 		RETURN_FALSE;
487 	}
488 
489 	if (ldap_bind_pw != NULL && memchr(ldap_bind_pw, '\0', ldap_bind_pwlen) != NULL) {
490 		_set_lderrno(ld->link, LDAP_INVALID_CREDENTIALS);
491 		php_error_docref(NULL, E_WARNING, "Password contains a null byte");
492 		RETURN_FALSE;
493 	}
494 
495 	{
496 #ifdef LDAP_API_FEATURE_X_OPENLDAP
497 		/* ldap_simple_bind_s() is deprecated, use ldap_sasl_bind_s() instead.
498 		 */
499 		struct berval   cred;
500 
501 		cred.bv_val = ldap_bind_pw;
502 		cred.bv_len = ldap_bind_pw ? ldap_bind_pwlen : 0;
503 		rc = ldap_sasl_bind_s(ld->link, ldap_bind_dn, LDAP_SASL_SIMPLE, &cred,
504 				NULL, NULL,     /* no controls right now */
505 				NULL);	  /* we don't care about the server's credentials */
506 #else /* ! LDAP_API_FEATURE_X_OPENLDAP */
507 		rc = ldap_simple_bind_s(ld->link, ldap_bind_dn, ldap_bind_pw);
508 #endif /* ! LDAP_API_FEATURE_X_OPENLDAP */
509 	}
510 	if ( rc != LDAP_SUCCESS) {
511 		php_error_docref(NULL, E_WARNING, "Unable to bind to server: %s", ldap_err2string(rc));
512 		RETURN_FALSE;
513 	} else {
514 		RETURN_TRUE;
515 	}
516 }
517 /* }}} */
518 
519 #ifdef HAVE_LDAP_SASL
520 typedef struct {
521 	char *mech;
522 	char *realm;
523 	char *authcid;
524 	char *passwd;
525 	char *authzid;
526 } php_ldap_bictx;
527 
528 /* {{{ _php_sasl_setdefs
529  */
_php_sasl_setdefs(LDAP * ld,char * sasl_mech,char * sasl_realm,char * sasl_authc_id,char * passwd,char * sasl_authz_id)530 static php_ldap_bictx *_php_sasl_setdefs(LDAP *ld, char *sasl_mech, char *sasl_realm, char *sasl_authc_id, char *passwd, char *sasl_authz_id)
531 {
532 	php_ldap_bictx *ctx;
533 
534 	ctx = ber_memalloc(sizeof(php_ldap_bictx));
535 	ctx->mech    = (sasl_mech) ? ber_strdup(sasl_mech) : NULL;
536 	ctx->realm   = (sasl_realm) ? ber_strdup(sasl_realm) : NULL;
537 	ctx->authcid = (sasl_authc_id) ? ber_strdup(sasl_authc_id) : NULL;
538 	ctx->passwd  = (passwd) ? ber_strdup(passwd) : NULL;
539 	ctx->authzid = (sasl_authz_id) ? ber_strdup(sasl_authz_id) : NULL;
540 
541 	if (ctx->mech == NULL) {
542 		ldap_get_option(ld, LDAP_OPT_X_SASL_MECH, &ctx->mech);
543 	}
544 	if (ctx->realm == NULL) {
545 		ldap_get_option(ld, LDAP_OPT_X_SASL_REALM, &ctx->realm);
546 	}
547 	if (ctx->authcid == NULL) {
548 		ldap_get_option(ld, LDAP_OPT_X_SASL_AUTHCID, &ctx->authcid);
549 	}
550 	if (ctx->authzid == NULL) {
551 		ldap_get_option(ld, LDAP_OPT_X_SASL_AUTHZID, &ctx->authzid);
552 	}
553 
554 	return ctx;
555 }
556 /* }}} */
557 
558 /* {{{ _php_sasl_freedefs
559  */
_php_sasl_freedefs(php_ldap_bictx * ctx)560 static void _php_sasl_freedefs(php_ldap_bictx *ctx)
561 {
562 	if (ctx->mech) ber_memfree(ctx->mech);
563 	if (ctx->realm) ber_memfree(ctx->realm);
564 	if (ctx->authcid) ber_memfree(ctx->authcid);
565 	if (ctx->passwd) ber_memfree(ctx->passwd);
566 	if (ctx->authzid) ber_memfree(ctx->authzid);
567 	ber_memfree(ctx);
568 }
569 /* }}} */
570 
571 /* {{{ _php_sasl_interact
572    Internal interact function for SASL */
_php_sasl_interact(LDAP * ld,unsigned flags,void * defaults,void * in)573 static int _php_sasl_interact(LDAP *ld, unsigned flags, void *defaults, void *in)
574 {
575 	sasl_interact_t *interact = in;
576 	const char *p;
577 	php_ldap_bictx *ctx = defaults;
578 
579 	for (;interact->id != SASL_CB_LIST_END;interact++) {
580 		p = NULL;
581 		switch(interact->id) {
582 			case SASL_CB_GETREALM:
583 				p = ctx->realm;
584 				break;
585 			case SASL_CB_AUTHNAME:
586 				p = ctx->authcid;
587 				break;
588 			case SASL_CB_USER:
589 				p = ctx->authzid;
590 				break;
591 			case SASL_CB_PASS:
592 				p = ctx->passwd;
593 				break;
594 		}
595 		if (p) {
596 			interact->result = p;
597 			interact->len = strlen(interact->result);
598 		}
599 	}
600 	return LDAP_SUCCESS;
601 }
602 /* }}} */
603 
604 /* {{{ proto bool ldap_sasl_bind(resource link [, string binddn [, string password [, string sasl_mech [, string sasl_realm [, string sasl_authc_id [, string sasl_authz_id [, string props]]]]]]])
605    Bind to LDAP directory using SASL */
PHP_FUNCTION(ldap_sasl_bind)606 PHP_FUNCTION(ldap_sasl_bind)
607 {
608 	zval *link;
609 	ldap_linkdata *ld;
610 	char *binddn = NULL;
611 	char *passwd = NULL;
612 	char *sasl_mech = NULL;
613 	char *sasl_realm = NULL;
614 	char *sasl_authz_id = NULL;
615 	char *sasl_authc_id = NULL;
616 	char *props = NULL;
617 	size_t rc, dn_len, passwd_len, mech_len, realm_len, authc_id_len, authz_id_len, props_len;
618 	php_ldap_bictx *ctx;
619 
620 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|sssssss", &link, &binddn, &dn_len, &passwd, &passwd_len, &sasl_mech, &mech_len, &sasl_realm, &realm_len, &sasl_authc_id, &authc_id_len, &sasl_authz_id, &authz_id_len, &props, &props_len) != SUCCESS) {
621 		RETURN_FALSE;
622 	}
623 
624 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
625 		RETURN_FALSE;
626 	}
627 
628 	ctx = _php_sasl_setdefs(ld->link, sasl_mech, sasl_realm, sasl_authc_id, passwd, sasl_authz_id);
629 
630 	if (props) {
631 		ldap_set_option(ld->link, LDAP_OPT_X_SASL_SECPROPS, props);
632 	}
633 
634 	rc = ldap_sasl_interactive_bind_s(ld->link, binddn, ctx->mech, NULL, NULL, LDAP_SASL_QUIET, _php_sasl_interact, ctx);
635 	if (rc != LDAP_SUCCESS) {
636 		php_error_docref(NULL, E_WARNING, "Unable to bind to server: %s", ldap_err2string(rc));
637 		RETVAL_FALSE;
638 	} else {
639 		RETVAL_TRUE;
640 	}
641 	_php_sasl_freedefs(ctx);
642 }
643 /* }}} */
644 #endif /* HAVE_LDAP_SASL */
645 
646 /* {{{ proto bool ldap_unbind(resource link)
647    Unbind from LDAP directory */
PHP_FUNCTION(ldap_unbind)648 PHP_FUNCTION(ldap_unbind)
649 {
650 	zval *link;
651 	ldap_linkdata *ld;
652 
653 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &link) != SUCCESS) {
654 		RETURN_FALSE;
655 	}
656 
657 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
658 		RETURN_FALSE;
659 	}
660 
661 	zend_list_close(Z_RES_P(link));
662 	RETURN_TRUE;
663 }
664 /* }}} */
665 
666 /* {{{ php_set_opts
667  */
php_set_opts(LDAP * ldap,int sizelimit,int timelimit,int deref,int * old_sizelimit,int * old_timelimit,int * old_deref)668 static void php_set_opts(LDAP *ldap, int sizelimit, int timelimit, int deref, int *old_sizelimit, int *old_timelimit, int *old_deref)
669 {
670 	/* sizelimit */
671 	if (sizelimit > -1) {
672 #if (LDAP_API_VERSION >= 2004) || HAVE_NSLDAP || HAVE_ORALDAP
673 		ldap_get_option(ldap, LDAP_OPT_SIZELIMIT, old_sizelimit);
674 		ldap_set_option(ldap, LDAP_OPT_SIZELIMIT, &sizelimit);
675 #else
676 		*old_sizelimit = ldap->ld_sizelimit;
677 		ldap->ld_sizelimit = sizelimit;
678 #endif
679 	}
680 
681 	/* timelimit */
682 	if (timelimit > -1) {
683 #if (LDAP_API_VERSION >= 2004) || HAVE_NSLDAP || HAVE_ORALDAP
684 		ldap_get_option(ldap, LDAP_OPT_TIMELIMIT, old_timelimit);
685 		ldap_set_option(ldap, LDAP_OPT_TIMELIMIT, &timelimit);
686 #else
687 		*old_timelimit = ldap->ld_timelimit;
688 		ldap->ld_timelimit = timelimit;
689 #endif
690 	}
691 
692 	/* deref */
693 	if (deref > -1) {
694 #if (LDAP_API_VERSION >= 2004) || HAVE_NSLDAP || HAVE_ORALDAP
695 		ldap_get_option(ldap, LDAP_OPT_DEREF, old_deref);
696 		ldap_set_option(ldap, LDAP_OPT_DEREF, &deref);
697 #else
698 		*old_deref = ldap->ld_deref;
699 		ldap->ld_deref = deref;
700 #endif
701 	}
702 }
703 /* }}} */
704 
705 /* {{{ php_ldap_do_search
706  */
php_ldap_do_search(INTERNAL_FUNCTION_PARAMETERS,int scope)707 static void php_ldap_do_search(INTERNAL_FUNCTION_PARAMETERS, int scope)
708 {
709 	zval *link, *base_dn, *filter, *attrs = NULL, *attr;
710 	zend_long attrsonly, sizelimit, timelimit, deref;
711 	char *ldap_base_dn = NULL, *ldap_filter = NULL, **ldap_attrs = NULL;
712 	ldap_linkdata *ld = NULL;
713 	LDAPMessage *ldap_res;
714 	int ldap_attrsonly = 0, ldap_sizelimit = -1, ldap_timelimit = -1, ldap_deref = -1;
715 	int old_ldap_sizelimit = -1, old_ldap_timelimit = -1, old_ldap_deref = -1;
716 	int num_attribs = 0, ret = 1, i, errno, argcount = ZEND_NUM_ARGS();
717 
718 	if (zend_parse_parameters(argcount, "zzz|allll", &link, &base_dn, &filter, &attrs, &attrsonly,
719 		&sizelimit, &timelimit, &deref) == FAILURE) {
720 		return;
721 	}
722 
723 	/* Reverse -> fall through */
724 	switch (argcount) {
725 		case 8:
726 			ldap_deref = deref;
727 		case 7:
728 			ldap_timelimit = timelimit;
729 		case 6:
730 			ldap_sizelimit = sizelimit;
731 		case 5:
732 			ldap_attrsonly = attrsonly;
733 		case 4:
734 			num_attribs = zend_hash_num_elements(Z_ARRVAL_P(attrs));
735 			ldap_attrs = safe_emalloc((num_attribs+1), sizeof(char *), 0);
736 
737 			for (i = 0; i<num_attribs; i++) {
738 				if ((attr = zend_hash_index_find(Z_ARRVAL_P(attrs), i)) == NULL) {
739 					php_error_docref(NULL, E_WARNING, "Array initialization wrong");
740 					ret = 0;
741 					goto cleanup;
742 				}
743 
744 				convert_to_string_ex(attr);
745 				ldap_attrs[i] = Z_STRVAL_P(attr);
746 			}
747 			ldap_attrs[num_attribs] = NULL;
748 		default:
749 			break;
750 	}
751 
752 	/* parallel search? */
753 	if (Z_TYPE_P(link) == IS_ARRAY) {
754 		int i, nlinks, nbases, nfilters, *rcs;
755 		ldap_linkdata **lds;
756 		zval *entry, resource;
757 
758 		nlinks = zend_hash_num_elements(Z_ARRVAL_P(link));
759 		if (nlinks == 0) {
760 			php_error_docref(NULL, E_WARNING, "No links in link array");
761 			ret = 0;
762 			goto cleanup;
763 		}
764 
765 		if (Z_TYPE_P(base_dn) == IS_ARRAY) {
766 			nbases = zend_hash_num_elements(Z_ARRVAL_P(base_dn));
767 			if (nbases != nlinks) {
768 				php_error_docref(NULL, E_WARNING, "Base must either be a string, or an array with the same number of elements as the links array");
769 				ret = 0;
770 				goto cleanup;
771 			}
772 			zend_hash_internal_pointer_reset(Z_ARRVAL_P(base_dn));
773 		} else {
774 			nbases = 0; /* this means string, not array */
775 			/* If anything else than string is passed, ldap_base_dn = NULL */
776 			if (Z_TYPE_P(base_dn) == IS_STRING) {
777 				ldap_base_dn = Z_STRVAL_P(base_dn);
778 			} else {
779 				ldap_base_dn = NULL;
780 			}
781 		}
782 
783 		if (Z_TYPE_P(filter) == IS_ARRAY) {
784 			nfilters = zend_hash_num_elements(Z_ARRVAL_P(filter));
785 			if (nfilters != nlinks) {
786 				php_error_docref(NULL, E_WARNING, "Filter must either be a string, or an array with the same number of elements as the links array");
787 				ret = 0;
788 				goto cleanup;
789 			}
790 			zend_hash_internal_pointer_reset(Z_ARRVAL_P(filter));
791 		} else {
792 			nfilters = 0; /* this means string, not array */
793 			convert_to_string_ex(filter);
794 			ldap_filter = Z_STRVAL_P(filter);
795 		}
796 
797 		lds = safe_emalloc(nlinks, sizeof(ldap_linkdata), 0);
798 		rcs = safe_emalloc(nlinks, sizeof(*rcs), 0);
799 
800 		zend_hash_internal_pointer_reset(Z_ARRVAL_P(link));
801 		for (i=0; i<nlinks; i++) {
802 			entry = zend_hash_get_current_data(Z_ARRVAL_P(link));
803 
804 			ld = (ldap_linkdata *) zend_fetch_resource_ex(entry, "ldap link", le_link);
805 			if (ld == NULL) {
806 				ret = 0;
807 				goto cleanup_parallel;
808 			}
809 			if (nbases != 0) { /* base_dn an array? */
810 				entry = zend_hash_get_current_data(Z_ARRVAL_P(base_dn));
811 				zend_hash_move_forward(Z_ARRVAL_P(base_dn));
812 
813 				/* If anything else than string is passed, ldap_base_dn = NULL */
814 				if (Z_TYPE_P(entry) == IS_STRING) {
815 					ldap_base_dn = Z_STRVAL_P(entry);
816 				} else {
817 					ldap_base_dn = NULL;
818 				}
819 			}
820 			if (nfilters != 0) { /* filter an array? */
821 				entry = zend_hash_get_current_data(Z_ARRVAL_P(filter));
822 				zend_hash_move_forward(Z_ARRVAL_P(filter));
823 				convert_to_string_ex(entry);
824 				ldap_filter = Z_STRVAL_P(entry);
825 			}
826 
827 			php_set_opts(ld->link, ldap_sizelimit, ldap_timelimit, ldap_deref, &old_ldap_sizelimit, &old_ldap_timelimit, &old_ldap_deref);
828 
829 			/* Run the actual search */
830 			ldap_search_ext(ld->link, ldap_base_dn, scope, ldap_filter, ldap_attrs, ldap_attrsonly, NULL, NULL, NULL, ldap_sizelimit, &rcs[i]);
831 			lds[i] = ld;
832 			zend_hash_move_forward(Z_ARRVAL_P(link));
833 		}
834 
835 		array_init(return_value);
836 
837 		/* Collect results from the searches */
838 		for (i=0; i<nlinks; i++) {
839 			if (rcs[i] != -1) {
840 				rcs[i] = ldap_result(lds[i]->link, LDAP_RES_ANY, 1 /* LDAP_MSG_ALL */, NULL, &ldap_res);
841 			}
842 			if (rcs[i] != -1) {
843 				ZVAL_RES(&resource, zend_register_resource(ldap_res, le_result));
844 				add_next_index_zval(return_value, &resource);
845 			} else {
846 				add_next_index_bool(return_value, 0);
847 			}
848 		}
849 
850 cleanup_parallel:
851 		efree(lds);
852 		efree(rcs);
853 	} else {
854 		convert_to_string_ex(filter);
855 		ldap_filter = Z_STRVAL_P(filter);
856 
857 		/* If anything else than string is passed, ldap_base_dn = NULL */
858 		if (Z_TYPE_P(base_dn) == IS_STRING) {
859 			ldap_base_dn = Z_STRVAL_P(base_dn);
860 		}
861 
862 		ld = (ldap_linkdata *) zend_fetch_resource_ex(link, "ldap link", le_link);
863 		if (ld == NULL) {
864 			ret = 0;
865 			goto cleanup;
866 		}
867 
868 		php_set_opts(ld->link, ldap_sizelimit, ldap_timelimit, ldap_deref, &old_ldap_sizelimit, &old_ldap_timelimit, &old_ldap_deref);
869 
870 		/* Run the actual search */
871 		errno = ldap_search_ext_s(ld->link, ldap_base_dn, scope, ldap_filter, ldap_attrs, ldap_attrsonly, NULL, NULL, NULL, ldap_sizelimit, &ldap_res);
872 
873 		if (errno != LDAP_SUCCESS
874 			&& errno != LDAP_SIZELIMIT_EXCEEDED
875 #ifdef LDAP_ADMINLIMIT_EXCEEDED
876 			&& errno != LDAP_ADMINLIMIT_EXCEEDED
877 #endif
878 #ifdef LDAP_REFERRAL
879 			&& errno != LDAP_REFERRAL
880 #endif
881 		) {
882 			php_error_docref(NULL, E_WARNING, "Search: %s", ldap_err2string(errno));
883 			ret = 0;
884 		} else {
885 			if (errno == LDAP_SIZELIMIT_EXCEEDED) {
886 				php_error_docref(NULL, E_WARNING, "Partial search results returned: Sizelimit exceeded");
887 			}
888 #ifdef LDAP_ADMINLIMIT_EXCEEDED
889 			else if (errno == LDAP_ADMINLIMIT_EXCEEDED) {
890 				php_error_docref(NULL, E_WARNING, "Partial search results returned: Adminlimit exceeded");
891 			}
892 #endif
893 
894 			RETVAL_RES(zend_register_resource(ldap_res, le_result));
895 		}
896 	}
897 
898 cleanup:
899 	if (ld) {
900 		/* Restoring previous options */
901 		php_set_opts(ld->link, old_ldap_sizelimit, old_ldap_timelimit, old_ldap_deref, &ldap_sizelimit, &ldap_timelimit, &ldap_deref);
902 	}
903 	if (ldap_attrs != NULL) {
904 		efree(ldap_attrs);
905 	}
906 	if (!ret) {
907 		RETVAL_BOOL(ret);
908 	}
909 }
910 /* }}} */
911 
912 /* {{{ proto resource ldap_read(resource|array link, string base_dn, string filter [, array attrs [, int attrsonly [, int sizelimit [, int timelimit [, int deref]]]]])
913    Read an entry */
PHP_FUNCTION(ldap_read)914 PHP_FUNCTION(ldap_read)
915 {
916 	php_ldap_do_search(INTERNAL_FUNCTION_PARAM_PASSTHRU, LDAP_SCOPE_BASE);
917 }
918 /* }}} */
919 
920 /* {{{ proto resource ldap_list(resource|array link, string base_dn, string filter [, array attrs [, int attrsonly [, int sizelimit [, int timelimit [, int deref]]]]])
921    Single-level search */
PHP_FUNCTION(ldap_list)922 PHP_FUNCTION(ldap_list)
923 {
924 	php_ldap_do_search(INTERNAL_FUNCTION_PARAM_PASSTHRU, LDAP_SCOPE_ONELEVEL);
925 }
926 /* }}} */
927 
928 /* {{{ proto resource ldap_search(resource|array link, string base_dn, string filter [, array attrs [, int attrsonly [, int sizelimit [, int timelimit [, int deref]]]]])
929    Search LDAP tree under base_dn */
PHP_FUNCTION(ldap_search)930 PHP_FUNCTION(ldap_search)
931 {
932 	php_ldap_do_search(INTERNAL_FUNCTION_PARAM_PASSTHRU, LDAP_SCOPE_SUBTREE);
933 }
934 /* }}} */
935 
936 /* {{{ proto bool ldap_free_result(resource result)
937    Free result memory */
PHP_FUNCTION(ldap_free_result)938 PHP_FUNCTION(ldap_free_result)
939 {
940 	zval *result;
941 	LDAPMessage *ldap_result;
942 
943 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &result) != SUCCESS) {
944 		return;
945 	}
946 
947 	if ((ldap_result = (LDAPMessage *)zend_fetch_resource(Z_RES_P(result), "ldap result", le_result)) == NULL) {
948 		RETURN_FALSE;
949 	}
950 
951 	zend_list_close(Z_RES_P(result));  /* Delete list entry */
952 	RETVAL_TRUE;
953 }
954 /* }}} */
955 
956 /* {{{ proto int ldap_count_entries(resource link, resource result)
957    Count the number of entries in a search result */
PHP_FUNCTION(ldap_count_entries)958 PHP_FUNCTION(ldap_count_entries)
959 {
960 	zval *link, *result;
961 	ldap_linkdata *ld;
962 	LDAPMessage *ldap_result;
963 
964 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rr", &link, &result) != SUCCESS) {
965 		return;
966 	}
967 
968 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
969 		RETURN_FALSE;
970 	}
971 
972 	if ((ldap_result = (LDAPMessage *)zend_fetch_resource(Z_RES_P(result), "ldap result", le_result)) == NULL) {
973 		RETURN_FALSE;
974 	}
975 
976 	RETURN_LONG(ldap_count_entries(ld->link, ldap_result));
977 }
978 /* }}} */
979 
980 /* {{{ proto resource ldap_first_entry(resource link, resource result)
981    Return first result id */
PHP_FUNCTION(ldap_first_entry)982 PHP_FUNCTION(ldap_first_entry)
983 {
984 	zval *link, *result;
985 	ldap_linkdata *ld;
986 	ldap_resultentry *resultentry;
987 	LDAPMessage *ldap_result, *entry;
988 
989 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rr", &link, &result) != SUCCESS) {
990 		return;
991 	}
992 
993 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
994 		RETURN_FALSE;
995 	}
996 
997 	if ((ldap_result = (LDAPMessage *)zend_fetch_resource(Z_RES_P(result), "ldap result", le_result)) == NULL) {
998 		RETURN_FALSE;
999 	}
1000 
1001 	if ((entry = ldap_first_entry(ld->link, ldap_result)) == NULL) {
1002 		RETVAL_FALSE;
1003 	} else {
1004 		resultentry = emalloc(sizeof(ldap_resultentry));
1005 		RETVAL_RES(zend_register_resource(resultentry, le_result_entry));
1006 		ZVAL_COPY(&resultentry->res, result);
1007 		resultentry->data = entry;
1008 		resultentry->ber = NULL;
1009 	}
1010 }
1011 /* }}} */
1012 
1013 /* {{{ proto resource ldap_next_entry(resource link, resource result_entry)
1014    Get next result entry */
PHP_FUNCTION(ldap_next_entry)1015 PHP_FUNCTION(ldap_next_entry)
1016 {
1017 	zval *link, *result_entry;
1018 	ldap_linkdata *ld;
1019 	ldap_resultentry *resultentry, *resultentry_next;
1020 	LDAPMessage *entry_next;
1021 
1022 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rr", &link, &result_entry) != SUCCESS) {
1023 		return;
1024 	}
1025 
1026 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
1027 		RETURN_FALSE;
1028 	}
1029 	if ((resultentry = (ldap_resultentry *)zend_fetch_resource(Z_RES_P(result_entry), "ldap result entry", le_result_entry)) == NULL) {
1030 		RETURN_FALSE;
1031 	}
1032 
1033 	if ((entry_next = ldap_next_entry(ld->link, resultentry->data)) == NULL) {
1034 		RETVAL_FALSE;
1035 	} else {
1036 		resultentry_next = emalloc(sizeof(ldap_resultentry));
1037 		RETVAL_RES(zend_register_resource(resultentry_next, le_result_entry));
1038 		ZVAL_COPY(&resultentry_next->res, &resultentry->res);
1039 		resultentry_next->data = entry_next;
1040 		resultentry_next->ber = NULL;
1041 	}
1042 }
1043 /* }}} */
1044 
1045 /* {{{ proto array ldap_get_entries(resource link, resource result)
1046    Get all result entries */
PHP_FUNCTION(ldap_get_entries)1047 PHP_FUNCTION(ldap_get_entries)
1048 {
1049 	zval *link, *result;
1050 	LDAPMessage *ldap_result, *ldap_result_entry;
1051 	zval tmp1, tmp2;
1052 	ldap_linkdata *ld;
1053 	LDAP *ldap;
1054 	int num_entries, num_attrib, num_values, i;
1055 	BerElement *ber;
1056 	char *attribute;
1057 	size_t attr_len;
1058 	struct berval **ldap_value;
1059 	char *dn;
1060 
1061 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rr", &link, &result) != SUCCESS) {
1062 		return;
1063 	}
1064 
1065 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
1066 		RETURN_FALSE;
1067 	}
1068 	if ((ldap_result = (LDAPMessage *)zend_fetch_resource(Z_RES_P(result), "ldap result", le_result)) == NULL) {
1069 		RETURN_FALSE;
1070 	}
1071 
1072 	ldap = ld->link;
1073 	num_entries = ldap_count_entries(ldap, ldap_result);
1074 
1075 	array_init(return_value);
1076 	add_assoc_long(return_value, "count", num_entries);
1077 
1078 	if (num_entries == 0) {
1079 		return;
1080 	}
1081 
1082 	ldap_result_entry = ldap_first_entry(ldap, ldap_result);
1083 	if (ldap_result_entry == NULL) {
1084 		zval_dtor(return_value);
1085 		RETURN_FALSE;
1086 	}
1087 
1088 	num_entries = 0;
1089 	while (ldap_result_entry != NULL) {
1090 		array_init(&tmp1);
1091 
1092 		num_attrib = 0;
1093 		attribute = ldap_first_attribute(ldap, ldap_result_entry, &ber);
1094 
1095 		while (attribute != NULL) {
1096 			ldap_value = ldap_get_values_len(ldap, ldap_result_entry, attribute);
1097 			num_values = ldap_count_values_len(ldap_value);
1098 
1099 			array_init(&tmp2);
1100 			add_assoc_long(&tmp2, "count", num_values);
1101 			for (i = 0; i < num_values; i++) {
1102 				add_index_stringl(&tmp2, i, ldap_value[i]->bv_val, ldap_value[i]->bv_len);
1103 			}
1104 			ldap_value_free_len(ldap_value);
1105 
1106 			attr_len = strlen(attribute);
1107 			zend_hash_str_update(Z_ARRVAL(tmp1), php_strtolower(attribute, attr_len), attr_len, &tmp2);
1108 			add_index_string(&tmp1, num_attrib, attribute);
1109 
1110 			num_attrib++;
1111 #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP || WINDOWS
1112 			ldap_memfree(attribute);
1113 #endif
1114 			attribute = ldap_next_attribute(ldap, ldap_result_entry, ber);
1115 		}
1116 #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP || WINDOWS
1117 		if (ber != NULL) {
1118 			ber_free(ber, 0);
1119 		}
1120 #endif
1121 
1122 		add_assoc_long(&tmp1, "count", num_attrib);
1123 		dn = ldap_get_dn(ldap, ldap_result_entry);
1124 		if (dn) {
1125 			add_assoc_string(&tmp1, "dn", dn);
1126 		} else {
1127 			add_assoc_null(&tmp1, "dn");
1128 		}
1129 #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP || WINDOWS
1130 		ldap_memfree(dn);
1131 #else
1132 		free(dn);
1133 #endif
1134 
1135 		zend_hash_index_update(Z_ARRVAL_P(return_value), num_entries, &tmp1);
1136 
1137 		num_entries++;
1138 		ldap_result_entry = ldap_next_entry(ldap, ldap_result_entry);
1139 	}
1140 
1141 	add_assoc_long(return_value, "count", num_entries);
1142 
1143 }
1144 /* }}} */
1145 
1146 /* {{{ proto string ldap_first_attribute(resource link, resource result_entry)
1147    Return first attribute */
PHP_FUNCTION(ldap_first_attribute)1148 PHP_FUNCTION(ldap_first_attribute)
1149 {
1150 	zval *link, *result_entry;
1151 	ldap_linkdata *ld;
1152 	ldap_resultentry *resultentry;
1153 	char *attribute;
1154 	zend_long dummy_ber;
1155 
1156 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rr|l", &link, &result_entry, &dummy_ber) != SUCCESS) {
1157 		return;
1158 	}
1159 
1160 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
1161 		RETURN_FALSE;
1162 	}
1163 
1164 	if ((resultentry = (ldap_resultentry *)zend_fetch_resource(Z_RES_P(result_entry), "ldap result entry", le_result_entry)) == NULL) {
1165 		RETURN_FALSE;
1166 	}
1167 
1168 	if ((attribute = ldap_first_attribute(ld->link, resultentry->data, &resultentry->ber)) == NULL) {
1169 		RETURN_FALSE;
1170 	} else {
1171 		RETVAL_STRING(attribute);
1172 #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP || WINDOWS
1173 		ldap_memfree(attribute);
1174 #endif
1175 	}
1176 }
1177 /* }}} */
1178 
1179 /* {{{ proto string ldap_next_attribute(resource link, resource result_entry)
1180    Get the next attribute in result */
PHP_FUNCTION(ldap_next_attribute)1181 PHP_FUNCTION(ldap_next_attribute)
1182 {
1183 	zval *link, *result_entry;
1184 	ldap_linkdata *ld;
1185 	ldap_resultentry *resultentry;
1186 	char *attribute;
1187 	zend_long dummy_ber;
1188 
1189 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rr|l", &link, &result_entry, &dummy_ber) != SUCCESS) {
1190 		return;
1191 	}
1192 
1193 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
1194 		RETURN_FALSE;
1195 	}
1196 
1197 	if ((resultentry = (ldap_resultentry *)zend_fetch_resource(Z_RES_P(result_entry), "ldap result entry", le_result_entry)) == NULL) {
1198 		RETURN_FALSE;
1199 	}
1200 
1201 	if (resultentry->ber == NULL) {
1202 		php_error_docref(NULL, E_WARNING, "called before calling ldap_first_attribute() or no attributes found in result entry");
1203 		RETURN_FALSE;
1204 	}
1205 
1206 	if ((attribute = ldap_next_attribute(ld->link, resultentry->data, resultentry->ber)) == NULL) {
1207 #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP || WINDOWS
1208 		if (resultentry->ber != NULL) {
1209 			ber_free(resultentry->ber, 0);
1210 			resultentry->ber = NULL;
1211 		}
1212 #endif
1213 		RETURN_FALSE;
1214 	} else {
1215 		RETVAL_STRING(attribute);
1216 #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP || WINDOWS
1217 		ldap_memfree(attribute);
1218 #endif
1219 	}
1220 }
1221 /* }}} */
1222 
1223 /* {{{ proto array ldap_get_attributes(resource link, resource result_entry)
1224    Get attributes from a search result entry */
PHP_FUNCTION(ldap_get_attributes)1225 PHP_FUNCTION(ldap_get_attributes)
1226 {
1227 	zval *link, *result_entry;
1228 	zval tmp;
1229 	ldap_linkdata *ld;
1230 	ldap_resultentry *resultentry;
1231 	char *attribute;
1232 	struct berval **ldap_value;
1233 	int i, num_values, num_attrib;
1234 	BerElement *ber;
1235 
1236 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rr", &link, &result_entry) != SUCCESS) {
1237 		return;
1238 	}
1239 
1240 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
1241 		RETURN_FALSE;
1242 	}
1243 
1244 	if ((resultentry = (ldap_resultentry *)zend_fetch_resource(Z_RES_P(result_entry), "ldap result entry", le_result_entry)) == NULL) {
1245 		RETURN_FALSE;
1246 	}
1247 
1248 	array_init(return_value);
1249 	num_attrib = 0;
1250 
1251 	attribute = ldap_first_attribute(ld->link, resultentry->data, &ber);
1252 	while (attribute != NULL) {
1253 		ldap_value = ldap_get_values_len(ld->link, resultentry->data, attribute);
1254 		num_values = ldap_count_values_len(ldap_value);
1255 
1256 		array_init(&tmp);
1257 		add_assoc_long(&tmp, "count", num_values);
1258 		for (i = 0; i < num_values; i++) {
1259 			add_index_stringl(&tmp, i, ldap_value[i]->bv_val, ldap_value[i]->bv_len);
1260 		}
1261 		ldap_value_free_len(ldap_value);
1262 
1263 		zend_hash_str_update(Z_ARRVAL_P(return_value), attribute, strlen(attribute), &tmp);
1264 		add_index_string(return_value, num_attrib, attribute);
1265 
1266 		num_attrib++;
1267 #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP || WINDOWS
1268 		ldap_memfree(attribute);
1269 #endif
1270 		attribute = ldap_next_attribute(ld->link, resultentry->data, ber);
1271 	}
1272 #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP || WINDOWS
1273 	if (ber != NULL) {
1274 		ber_free(ber, 0);
1275 	}
1276 #endif
1277 
1278 	add_assoc_long(return_value, "count", num_attrib);
1279 }
1280 /* }}} */
1281 
1282 /* {{{ proto array ldap_get_values_len(resource link, resource result_entry, string attribute)
1283    Get all values with lengths from a result entry */
PHP_FUNCTION(ldap_get_values_len)1284 PHP_FUNCTION(ldap_get_values_len)
1285 {
1286 	zval *link, *result_entry;
1287 	ldap_linkdata *ld;
1288 	ldap_resultentry *resultentry;
1289 	char *attr;
1290 	struct berval **ldap_value_len;
1291 	int i, num_values;
1292 	size_t attr_len;
1293 
1294 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rrs", &link, &result_entry, &attr, &attr_len) != SUCCESS) {
1295 		return;
1296 	}
1297 
1298 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
1299 		RETURN_FALSE;
1300 	}
1301 
1302 	if ((resultentry = (ldap_resultentry *)zend_fetch_resource(Z_RES_P(result_entry), "ldap result entry", le_result_entry)) == NULL) {
1303 		RETURN_FALSE;
1304 	}
1305 
1306 	if ((ldap_value_len = ldap_get_values_len(ld->link, resultentry->data, attr)) == NULL) {
1307 		php_error_docref(NULL, E_WARNING, "Cannot get the value(s) of attribute %s", ldap_err2string(_get_lderrno(ld->link)));
1308 		RETURN_FALSE;
1309 	}
1310 
1311 	num_values = ldap_count_values_len(ldap_value_len);
1312 	array_init(return_value);
1313 
1314 	for (i=0; i<num_values; i++) {
1315 		add_next_index_stringl(return_value, ldap_value_len[i]->bv_val, ldap_value_len[i]->bv_len);
1316 	}
1317 
1318 	add_assoc_long(return_value, "count", num_values);
1319 	ldap_value_free_len(ldap_value_len);
1320 
1321 }
1322 /* }}} */
1323 
1324 /* {{{ proto string ldap_get_dn(resource link, resource result_entry)
1325    Get the DN of a result entry */
PHP_FUNCTION(ldap_get_dn)1326 PHP_FUNCTION(ldap_get_dn)
1327 {
1328 	zval *link, *result_entry;
1329 	ldap_linkdata *ld;
1330 	ldap_resultentry *resultentry;
1331 	char *text;
1332 
1333 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rr", &link, &result_entry) != SUCCESS) {
1334 		return;
1335 	}
1336 
1337 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
1338 		RETURN_FALSE;
1339 	}
1340 
1341 	if ((resultentry = (ldap_resultentry *)zend_fetch_resource(Z_RES_P(result_entry), "ldap result entry", le_result_entry)) == NULL) {
1342 		RETURN_FALSE;
1343 	}
1344 
1345 	text = ldap_get_dn(ld->link, resultentry->data);
1346 	if (text != NULL) {
1347 		RETVAL_STRING(text);
1348 #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP || WINDOWS
1349 		ldap_memfree(text);
1350 #else
1351 		free(text);
1352 #endif
1353 	} else {
1354 		RETURN_FALSE;
1355 	}
1356 }
1357 /* }}} */
1358 
1359 /* {{{ proto array ldap_explode_dn(string dn, int with_attrib)
1360    Splits DN into its component parts */
PHP_FUNCTION(ldap_explode_dn)1361 PHP_FUNCTION(ldap_explode_dn)
1362 {
1363 	zend_long with_attrib;
1364 	char *dn, **ldap_value;
1365 	int i, count;
1366 	size_t dn_len;
1367 
1368 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "sl", &dn, &dn_len, &with_attrib) != SUCCESS) {
1369 		return;
1370 	}
1371 
1372 	if (!(ldap_value = ldap_explode_dn(dn, with_attrib))) {
1373 		/* Invalid parameters were passed to ldap_explode_dn */
1374 		RETURN_FALSE;
1375 	}
1376 
1377 	i=0;
1378 	while (ldap_value[i] != NULL) i++;
1379 	count = i;
1380 
1381 	array_init(return_value);
1382 
1383 	add_assoc_long(return_value, "count", count);
1384 	for (i = 0; i<count; i++) {
1385 		add_index_string(return_value, i, ldap_value[i]);
1386 	}
1387 
1388 	ldap_memvfree((void **)ldap_value);
1389 }
1390 /* }}} */
1391 
1392 /* {{{ proto string ldap_dn2ufn(string dn)
1393    Convert DN to User Friendly Naming format */
PHP_FUNCTION(ldap_dn2ufn)1394 PHP_FUNCTION(ldap_dn2ufn)
1395 {
1396 	char *dn, *ufn;
1397 	size_t dn_len;
1398 
1399 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &dn, &dn_len) != SUCCESS) {
1400 		return;
1401 	}
1402 
1403 	ufn = ldap_dn2ufn(dn);
1404 
1405 	if (ufn != NULL) {
1406 		RETVAL_STRING(ufn);
1407 #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP || WINDOWS
1408 		ldap_memfree(ufn);
1409 #endif
1410 	} else {
1411 		RETURN_FALSE;
1412 	}
1413 }
1414 /* }}} */
1415 
1416 
1417 /* added to fix use of ldap_modify_add for doing an ldap_add, gerrit thomson. */
1418 #define PHP_LD_FULL_ADD 0xff
1419 /* {{{ php_ldap_do_modify
1420  */
php_ldap_do_modify(INTERNAL_FUNCTION_PARAMETERS,int oper)1421 static void php_ldap_do_modify(INTERNAL_FUNCTION_PARAMETERS, int oper)
1422 {
1423 	zval *link, *entry, *value, *ivalue;
1424 	ldap_linkdata *ld;
1425 	char *dn;
1426 	LDAPMod **ldap_mods;
1427 	int i, j, num_attribs, num_values;
1428 	size_t dn_len;
1429 	int *num_berval;
1430 	zend_string *attribute;
1431 	zend_ulong index;
1432 	int is_full_add=0; /* flag for full add operation so ldap_mod_add can be put back into oper, gerrit THomson */
1433 
1434 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rsa/", &link, &dn, &dn_len, &entry) != SUCCESS) {
1435 		return;
1436 	}
1437 
1438 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
1439 		RETURN_FALSE;
1440 	}
1441 
1442 	num_attribs = zend_hash_num_elements(Z_ARRVAL_P(entry));
1443 	ldap_mods = safe_emalloc((num_attribs+1), sizeof(LDAPMod *), 0);
1444 	num_berval = safe_emalloc(num_attribs, sizeof(int), 0);
1445 	zend_hash_internal_pointer_reset(Z_ARRVAL_P(entry));
1446 
1447 	/* added by gerrit thomson to fix ldap_add using ldap_mod_add */
1448 	if (oper == PHP_LD_FULL_ADD) {
1449 		oper = LDAP_MOD_ADD;
1450 		is_full_add = 1;
1451 	}
1452 	/* end additional , gerrit thomson */
1453 
1454 	for (i = 0; i < num_attribs; i++) {
1455 		ldap_mods[i] = emalloc(sizeof(LDAPMod));
1456 		ldap_mods[i]->mod_op = oper | LDAP_MOD_BVALUES;
1457 		ldap_mods[i]->mod_type = NULL;
1458 
1459 		if (zend_hash_get_current_key(Z_ARRVAL_P(entry), &attribute, &index) == HASH_KEY_IS_STRING) {
1460 			ldap_mods[i]->mod_type = estrndup(ZSTR_VAL(attribute), ZSTR_LEN(attribute));
1461 		} else {
1462 			php_error_docref(NULL, E_WARNING, "Unknown attribute in the data");
1463 			/* Free allocated memory */
1464 			while (i >= 0) {
1465 				if (ldap_mods[i]->mod_type) {
1466 					efree(ldap_mods[i]->mod_type);
1467 				}
1468 				efree(ldap_mods[i]);
1469 				i--;
1470 			}
1471 			efree(num_berval);
1472 			efree(ldap_mods);
1473 			RETURN_FALSE;
1474 		}
1475 
1476 		value = zend_hash_get_current_data(Z_ARRVAL_P(entry));
1477 
1478 		ZVAL_DEREF(value);
1479 		if (Z_TYPE_P(value) != IS_ARRAY) {
1480 			num_values = 1;
1481 		} else {
1482 			SEPARATE_ARRAY(value);
1483 			num_values = zend_hash_num_elements(Z_ARRVAL_P(value));
1484 		}
1485 
1486 		num_berval[i] = num_values;
1487 		ldap_mods[i]->mod_bvalues = safe_emalloc((num_values + 1), sizeof(struct berval *), 0);
1488 
1489 /* allow for arrays with one element, no allowance for arrays with none but probably not required, gerrit thomson. */
1490 		if ((num_values == 1) && (Z_TYPE_P(value) != IS_ARRAY)) {
1491 			convert_to_string_ex(value);
1492 			ldap_mods[i]->mod_bvalues[0] = (struct berval *) emalloc (sizeof(struct berval));
1493 			ldap_mods[i]->mod_bvalues[0]->bv_len = Z_STRLEN_P(value);
1494 			ldap_mods[i]->mod_bvalues[0]->bv_val = Z_STRVAL_P(value);
1495 		} else {
1496 			for (j = 0; j < num_values; j++) {
1497 				if ((ivalue = zend_hash_index_find(Z_ARRVAL_P(value), j)) == NULL) {
1498 					php_error_docref(NULL, E_WARNING, "Value array must have consecutive indices 0, 1, ...");
1499 					num_berval[i] = j;
1500 					num_attribs = i + 1;
1501 					RETVAL_FALSE;
1502 					goto errexit;
1503 				}
1504 				convert_to_string_ex(ivalue);
1505 				ldap_mods[i]->mod_bvalues[j] = (struct berval *) emalloc (sizeof(struct berval));
1506 				ldap_mods[i]->mod_bvalues[j]->bv_len = Z_STRLEN_P(ivalue);
1507 				ldap_mods[i]->mod_bvalues[j]->bv_val = Z_STRVAL_P(ivalue);
1508 			}
1509 		}
1510 		ldap_mods[i]->mod_bvalues[num_values] = NULL;
1511 		zend_hash_move_forward(Z_ARRVAL_P(entry));
1512 	}
1513 	ldap_mods[num_attribs] = NULL;
1514 
1515 /* check flag to see if do_mod was called to perform full add , gerrit thomson */
1516 	if (is_full_add == 1) {
1517 		if ((i = ldap_add_ext_s(ld->link, dn, ldap_mods, NULL, NULL)) != LDAP_SUCCESS) {
1518 			php_error_docref(NULL, E_WARNING, "Add: %s", ldap_err2string(i));
1519 			RETVAL_FALSE;
1520 		} else RETVAL_TRUE;
1521 	} else {
1522 		if ((i = ldap_modify_ext_s(ld->link, dn, ldap_mods, NULL, NULL)) != LDAP_SUCCESS) {
1523 			php_error_docref(NULL, E_WARNING, "Modify: %s", ldap_err2string(i));
1524 			RETVAL_FALSE;
1525 		} else RETVAL_TRUE;
1526 	}
1527 
1528 errexit:
1529 	for (i = 0; i < num_attribs; i++) {
1530 		efree(ldap_mods[i]->mod_type);
1531 		for (j = 0; j < num_berval[i]; j++) {
1532 			efree(ldap_mods[i]->mod_bvalues[j]);
1533 		}
1534 		efree(ldap_mods[i]->mod_bvalues);
1535 		efree(ldap_mods[i]);
1536 	}
1537 	efree(num_berval);
1538 	efree(ldap_mods);
1539 
1540 	return;
1541 }
1542 /* }}} */
1543 
1544 /* {{{ proto bool ldap_add(resource link, string dn, array entry)
1545    Add entries to LDAP directory */
PHP_FUNCTION(ldap_add)1546 PHP_FUNCTION(ldap_add)
1547 {
1548 	/* use a newly define parameter into the do_modify so ldap_mod_add can be used the way it is supposed to be used , Gerrit THomson */
1549 	php_ldap_do_modify(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_LD_FULL_ADD);
1550 }
1551 /* }}} */
1552 
1553 /* three functions for attribute base modifications, gerrit Thomson */
1554 
1555 /* {{{ proto bool ldap_mod_replace(resource link, string dn, array entry)
1556    Replace attribute values with new ones */
PHP_FUNCTION(ldap_mod_replace)1557 PHP_FUNCTION(ldap_mod_replace)
1558 {
1559 	php_ldap_do_modify(INTERNAL_FUNCTION_PARAM_PASSTHRU, LDAP_MOD_REPLACE);
1560 }
1561 /* }}} */
1562 
1563 /* {{{ proto bool ldap_mod_add(resource link, string dn, array entry)
1564    Add attribute values to current */
PHP_FUNCTION(ldap_mod_add)1565 PHP_FUNCTION(ldap_mod_add)
1566 {
1567 	php_ldap_do_modify(INTERNAL_FUNCTION_PARAM_PASSTHRU, LDAP_MOD_ADD);
1568 }
1569 /* }}} */
1570 
1571 /* {{{ proto bool ldap_mod_del(resource link, string dn, array entry)
1572    Delete attribute values */
PHP_FUNCTION(ldap_mod_del)1573 PHP_FUNCTION(ldap_mod_del)
1574 {
1575 	php_ldap_do_modify(INTERNAL_FUNCTION_PARAM_PASSTHRU, LDAP_MOD_DELETE);
1576 }
1577 /* }}} */
1578 
1579 /* {{{ proto bool ldap_delete(resource link, string dn)
1580    Delete an entry from a directory */
PHP_FUNCTION(ldap_delete)1581 PHP_FUNCTION(ldap_delete)
1582 {
1583 	zval *link;
1584 	ldap_linkdata *ld;
1585 	char *dn;
1586 	int rc;
1587 	size_t dn_len;
1588 
1589 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs", &link, &dn, &dn_len) != SUCCESS) {
1590 		return;
1591 	}
1592 
1593 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
1594 		RETURN_FALSE;
1595 	}
1596 
1597 	if ((rc = ldap_delete_ext_s(ld->link, dn, NULL, NULL)) != LDAP_SUCCESS) {
1598 		php_error_docref(NULL, E_WARNING, "Delete: %s", ldap_err2string(rc));
1599 		RETURN_FALSE;
1600 	}
1601 
1602 	RETURN_TRUE;
1603 }
1604 /* }}} */
1605 
1606 /* {{{ _ldap_str_equal_to_const
1607  */
_ldap_str_equal_to_const(const char * str,uint str_len,const char * cstr)1608 static int _ldap_str_equal_to_const(const char *str, uint str_len, const char *cstr)
1609 {
1610 	int i;
1611 
1612 	if (strlen(cstr) != str_len)
1613 		return 0;
1614 
1615 	for (i = 0; i < str_len; ++i) {
1616 		if (str[i] != cstr[i]) {
1617 			return 0;
1618 		}
1619 	}
1620 
1621 	return 1;
1622 }
1623 /* }}} */
1624 
1625 /* {{{ _ldap_strlen_max
1626  */
_ldap_strlen_max(const char * str,uint max_len)1627 static int _ldap_strlen_max(const char *str, uint max_len)
1628 {
1629 	int i;
1630 
1631 	for (i = 0; i < max_len; ++i) {
1632 		if (str[i] == '\0') {
1633 			return i;
1634 		}
1635 	}
1636 
1637 	return max_len;
1638 }
1639 /* }}} */
1640 
1641 /* {{{ _ldap_hash_fetch
1642  */
_ldap_hash_fetch(zval * hashTbl,const char * key,zval ** out)1643 static void _ldap_hash_fetch(zval *hashTbl, const char *key, zval **out)
1644 {
1645 	*out = zend_hash_str_find(Z_ARRVAL_P(hashTbl), key, strlen(key));
1646 }
1647 /* }}} */
1648 
1649 /* {{{ proto bool ldap_modify_batch(resource link, string dn, array modifs)
1650    Perform multiple modifications as part of one operation */
PHP_FUNCTION(ldap_modify_batch)1651 PHP_FUNCTION(ldap_modify_batch)
1652 {
1653 	ldap_linkdata *ld;
1654 	zval *link, *mods, *mod, *modinfo, *modval;
1655 	zval *attrib, *modtype, *vals;
1656 	zval *fetched;
1657 	char *dn;
1658 	size_t dn_len;
1659 	int i, j, k;
1660 	int num_mods, num_modprops, num_modvals;
1661 	LDAPMod **ldap_mods;
1662 	uint oper;
1663 
1664 	/*
1665 	$mods = array(
1666 		array(
1667 			"attrib" => "unicodePwd",
1668 			"modtype" => LDAP_MODIFY_BATCH_REMOVE,
1669 			"values" => array($oldpw)
1670 		),
1671 		array(
1672 			"attrib" => "unicodePwd",
1673 			"modtype" => LDAP_MODIFY_BATCH_ADD,
1674 			"values" => array($newpw)
1675 		),
1676 		array(
1677 			"attrib" => "userPrincipalName",
1678 			"modtype" => LDAP_MODIFY_BATCH_REPLACE,
1679 			"values" => array("janitor@corp.contoso.com")
1680 		),
1681 		array(
1682 			"attrib" => "userCert",
1683 			"modtype" => LDAP_MODIFY_BATCH_REMOVE_ALL
1684 		)
1685 	);
1686 	*/
1687 
1688 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rsa/", &link, &dn, &dn_len, &mods) != SUCCESS) {
1689 		return;
1690 	}
1691 
1692 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
1693 		RETURN_FALSE;
1694 	}
1695 
1696 	/* perform validation */
1697 	{
1698 		zend_string *modkey;
1699 		zend_long modtype;
1700 
1701 		/* to store the wrongly-typed keys */
1702 		zend_ulong tmpUlong;
1703 
1704 		/* make sure the DN contains no NUL bytes */
1705 		if (_ldap_strlen_max(dn, dn_len) != dn_len) {
1706 			php_error_docref(NULL, E_WARNING, "DN must not contain NUL bytes");
1707 			RETURN_FALSE;
1708 		}
1709 
1710 		/* make sure the top level is a normal array */
1711 		zend_hash_internal_pointer_reset(Z_ARRVAL_P(mods));
1712 		if (zend_hash_get_current_key_type(Z_ARRVAL_P(mods)) != HASH_KEY_IS_LONG) {
1713 			php_error_docref(NULL, E_WARNING, "Modifications array must not be string-indexed");
1714 			RETURN_FALSE;
1715 		}
1716 
1717 		num_mods = zend_hash_num_elements(Z_ARRVAL_P(mods));
1718 
1719 		for (i = 0; i < num_mods; i++) {
1720 			/* is the numbering consecutive? */
1721 			if ((fetched = zend_hash_index_find(Z_ARRVAL_P(mods), i)) == NULL) {
1722 				php_error_docref(NULL, E_WARNING, "Modifications array must have consecutive indices 0, 1, ...");
1723 				RETURN_FALSE;
1724 			}
1725 			mod = fetched;
1726 
1727 			/* is it an array? */
1728 			if (Z_TYPE_P(mod) != IS_ARRAY) {
1729 				php_error_docref(NULL, E_WARNING, "Each entry of modifications array must be an array itself");
1730 				RETURN_FALSE;
1731 			}
1732 
1733 			SEPARATE_ARRAY(mod);
1734 			/* for the modification hashtable... */
1735 			zend_hash_internal_pointer_reset(Z_ARRVAL_P(mod));
1736 			num_modprops = zend_hash_num_elements(Z_ARRVAL_P(mod));
1737 
1738 			for (j = 0; j < num_modprops; j++) {
1739 				/* are the keys strings? */
1740 				if (zend_hash_get_current_key(Z_ARRVAL_P(mod), &modkey, &tmpUlong) != HASH_KEY_IS_STRING) {
1741 					php_error_docref(NULL, E_WARNING, "Each entry of modifications array must be string-indexed");
1742 					RETURN_FALSE;
1743 				}
1744 
1745 				/* is this a valid entry? */
1746 				if (
1747 					!_ldap_str_equal_to_const(ZSTR_VAL(modkey), ZSTR_LEN(modkey), LDAP_MODIFY_BATCH_ATTRIB) &&
1748 					!_ldap_str_equal_to_const(ZSTR_VAL(modkey), ZSTR_LEN(modkey), LDAP_MODIFY_BATCH_MODTYPE) &&
1749 					!_ldap_str_equal_to_const(ZSTR_VAL(modkey), ZSTR_LEN(modkey), LDAP_MODIFY_BATCH_VALUES)
1750 				) {
1751 					php_error_docref(NULL, E_WARNING, "The only allowed keys in entries of the modifications array are '" LDAP_MODIFY_BATCH_ATTRIB "', '" LDAP_MODIFY_BATCH_MODTYPE "' and '" LDAP_MODIFY_BATCH_VALUES "'");
1752 					RETURN_FALSE;
1753 				}
1754 
1755 				fetched = zend_hash_get_current_data(Z_ARRVAL_P(mod));
1756 				modinfo = fetched;
1757 
1758 				/* does the value type match the key? */
1759 				if (_ldap_str_equal_to_const(ZSTR_VAL(modkey), ZSTR_LEN(modkey), LDAP_MODIFY_BATCH_ATTRIB)) {
1760 					if (Z_TYPE_P(modinfo) != IS_STRING) {
1761 						php_error_docref(NULL, E_WARNING, "A '" LDAP_MODIFY_BATCH_ATTRIB "' value must be a string");
1762 						RETURN_FALSE;
1763 					}
1764 
1765 					if (Z_STRLEN_P(modinfo) != _ldap_strlen_max(Z_STRVAL_P(modinfo), Z_STRLEN_P(modinfo))) {
1766 						php_error_docref(NULL, E_WARNING, "A '" LDAP_MODIFY_BATCH_ATTRIB "' value must not contain NUL bytes");
1767 						RETURN_FALSE;
1768 					}
1769 				}
1770 				else if (_ldap_str_equal_to_const(ZSTR_VAL(modkey), ZSTR_LEN(modkey), LDAP_MODIFY_BATCH_MODTYPE)) {
1771 					if (Z_TYPE_P(modinfo) != IS_LONG) {
1772 						php_error_docref(NULL, E_WARNING, "A '" LDAP_MODIFY_BATCH_MODTYPE "' value must be a long");
1773 						RETURN_FALSE;
1774 					}
1775 
1776 					/* is the value in range? */
1777 					modtype = Z_LVAL_P(modinfo);
1778 					if (
1779 						modtype != LDAP_MODIFY_BATCH_ADD &&
1780 						modtype != LDAP_MODIFY_BATCH_REMOVE &&
1781 						modtype != LDAP_MODIFY_BATCH_REPLACE &&
1782 						modtype != LDAP_MODIFY_BATCH_REMOVE_ALL
1783 					) {
1784 						php_error_docref(NULL, E_WARNING, "The '" LDAP_MODIFY_BATCH_MODTYPE "' value must match one of the LDAP_MODIFY_BATCH_* constants");
1785 						RETURN_FALSE;
1786 					}
1787 
1788 					/* if it's REMOVE_ALL, there must not be a values array; otherwise, there must */
1789 					if (modtype == LDAP_MODIFY_BATCH_REMOVE_ALL) {
1790 						if (zend_hash_str_exists(Z_ARRVAL_P(mod), LDAP_MODIFY_BATCH_VALUES, strlen(LDAP_MODIFY_BATCH_VALUES))) {
1791 							php_error_docref(NULL, E_WARNING, "If '" LDAP_MODIFY_BATCH_MODTYPE "' is LDAP_MODIFY_BATCH_REMOVE_ALL, a '" LDAP_MODIFY_BATCH_VALUES "' array must not be provided");
1792 							RETURN_FALSE;
1793 						}
1794 					}
1795 					else {
1796 						if (!zend_hash_str_exists(Z_ARRVAL_P(mod), LDAP_MODIFY_BATCH_VALUES, strlen(LDAP_MODIFY_BATCH_VALUES))) {
1797 							php_error_docref(NULL, E_WARNING, "If '" LDAP_MODIFY_BATCH_MODTYPE "' is not LDAP_MODIFY_BATCH_REMOVE_ALL, a '" LDAP_MODIFY_BATCH_VALUES "' array must be provided");
1798 							RETURN_FALSE;
1799 						}
1800 					}
1801 				}
1802 				else if (_ldap_str_equal_to_const(ZSTR_VAL(modkey), ZSTR_LEN(modkey), LDAP_MODIFY_BATCH_VALUES)) {
1803 					if (Z_TYPE_P(modinfo) != IS_ARRAY) {
1804 						php_error_docref(NULL, E_WARNING, "A '" LDAP_MODIFY_BATCH_VALUES "' value must be an array");
1805 						RETURN_FALSE;
1806 					}
1807 
1808 					SEPARATE_ARRAY(modinfo);
1809 					/* is the array not empty? */
1810 					zend_hash_internal_pointer_reset(Z_ARRVAL_P(modinfo));
1811 					num_modvals = zend_hash_num_elements(Z_ARRVAL_P(modinfo));
1812 					if (num_modvals == 0) {
1813 						php_error_docref(NULL, E_WARNING, "A '" LDAP_MODIFY_BATCH_VALUES "' array must have at least one element");
1814 						RETURN_FALSE;
1815 					}
1816 
1817 					/* are its keys integers? */
1818 					if (zend_hash_get_current_key_type(Z_ARRVAL_P(modinfo)) != HASH_KEY_IS_LONG) {
1819 						php_error_docref(NULL, E_WARNING, "A '" LDAP_MODIFY_BATCH_VALUES "' array must not be string-indexed");
1820 						RETURN_FALSE;
1821 					}
1822 
1823 					/* are the keys consecutive? */
1824 					for (k = 0; k < num_modvals; k++) {
1825 						if ((fetched = zend_hash_index_find(Z_ARRVAL_P(modinfo), k)) == NULL) {
1826 							php_error_docref(NULL, E_WARNING, "A '" LDAP_MODIFY_BATCH_VALUES "' array must have consecutive indices 0, 1, ...");
1827 							RETURN_FALSE;
1828 						}
1829 						modval = fetched;
1830 
1831 						/* is the data element a string? */
1832 						if (Z_TYPE_P(modval) != IS_STRING) {
1833 							php_error_docref(NULL, E_WARNING, "Each element of a '" LDAP_MODIFY_BATCH_VALUES "' array must be a string");
1834 							RETURN_FALSE;
1835 						}
1836 					}
1837 				}
1838 
1839 				zend_hash_move_forward(Z_ARRVAL_P(mod));
1840 			}
1841 		}
1842 	}
1843 	/* validation was successful */
1844 
1845 	/* allocate array of modifications */
1846 	ldap_mods = safe_emalloc((num_mods+1), sizeof(LDAPMod *), 0);
1847 
1848 	/* for each modification */
1849 	for (i = 0; i < num_mods; i++) {
1850 		/* allocate the modification struct */
1851 		ldap_mods[i] = safe_emalloc(1, sizeof(LDAPMod), 0);
1852 
1853 		/* fetch the relevant data */
1854 		fetched = zend_hash_index_find(Z_ARRVAL_P(mods), i);
1855 		mod = fetched;
1856 
1857 		_ldap_hash_fetch(mod, LDAP_MODIFY_BATCH_ATTRIB, &attrib);
1858 		_ldap_hash_fetch(mod, LDAP_MODIFY_BATCH_MODTYPE, &modtype);
1859 		_ldap_hash_fetch(mod, LDAP_MODIFY_BATCH_VALUES, &vals);
1860 
1861 		/* map the modification type */
1862 		switch (Z_LVAL_P(modtype)) {
1863 			case LDAP_MODIFY_BATCH_ADD:
1864 				oper = LDAP_MOD_ADD;
1865 				break;
1866 			case LDAP_MODIFY_BATCH_REMOVE:
1867 			case LDAP_MODIFY_BATCH_REMOVE_ALL:
1868 				oper = LDAP_MOD_DELETE;
1869 				break;
1870 			case LDAP_MODIFY_BATCH_REPLACE:
1871 				oper = LDAP_MOD_REPLACE;
1872 				break;
1873 			default:
1874 				php_error_docref(NULL, E_ERROR, "Unknown and uncaught modification type.");
1875 				RETURN_FALSE;
1876 		}
1877 
1878 		/* fill in the basic info */
1879 		ldap_mods[i]->mod_op = oper | LDAP_MOD_BVALUES;
1880 		ldap_mods[i]->mod_type = estrndup(Z_STRVAL_P(attrib), Z_STRLEN_P(attrib));
1881 
1882 		if (Z_LVAL_P(modtype) == LDAP_MODIFY_BATCH_REMOVE_ALL) {
1883 			/* no values */
1884 			ldap_mods[i]->mod_bvalues = NULL;
1885 		}
1886 		else {
1887 			/* allocate space for the values as part of this modification */
1888 			num_modvals = zend_hash_num_elements(Z_ARRVAL_P(vals));
1889 			ldap_mods[i]->mod_bvalues = safe_emalloc((num_modvals+1), sizeof(struct berval *), 0);
1890 
1891 			/* for each value */
1892 			for (j = 0; j < num_modvals; j++) {
1893 				/* fetch it */
1894 				fetched = zend_hash_index_find(Z_ARRVAL_P(vals), j);
1895 				modval = fetched;
1896 
1897 				/* allocate the data struct */
1898 				ldap_mods[i]->mod_bvalues[j] = safe_emalloc(1, sizeof(struct berval), 0);
1899 
1900 				/* fill it */
1901 				ldap_mods[i]->mod_bvalues[j]->bv_len = Z_STRLEN_P(modval);
1902 				ldap_mods[i]->mod_bvalues[j]->bv_val = estrndup(Z_STRVAL_P(modval), Z_STRLEN_P(modval));
1903 			}
1904 
1905 			/* NULL-terminate values */
1906 			ldap_mods[i]->mod_bvalues[num_modvals] = NULL;
1907 		}
1908 	}
1909 
1910 	/* NULL-terminate modifications */
1911 	ldap_mods[num_mods] = NULL;
1912 
1913 	/* perform (finally) */
1914 	if ((i = ldap_modify_ext_s(ld->link, dn, ldap_mods, NULL, NULL)) != LDAP_SUCCESS) {
1915 		php_error_docref(NULL, E_WARNING, "Batch Modify: %s", ldap_err2string(i));
1916 		RETVAL_FALSE;
1917 	} else RETVAL_TRUE;
1918 
1919 	/* clean up */
1920 	{
1921 		for (i = 0; i < num_mods; i++) {
1922 			/* attribute */
1923 			efree(ldap_mods[i]->mod_type);
1924 
1925 			if (ldap_mods[i]->mod_bvalues != NULL) {
1926 				/* each BER value */
1927 				for (j = 0; ldap_mods[i]->mod_bvalues[j] != NULL; j++) {
1928 					/* free the data bytes */
1929 					efree(ldap_mods[i]->mod_bvalues[j]->bv_val);
1930 
1931 					/* free the bvalue struct */
1932 					efree(ldap_mods[i]->mod_bvalues[j]);
1933 				}
1934 
1935 				/* the BER value array */
1936 				efree(ldap_mods[i]->mod_bvalues);
1937 			}
1938 
1939 			/* the modification */
1940 			efree(ldap_mods[i]);
1941 		}
1942 
1943 		/* the modifications array */
1944 		efree(ldap_mods);
1945 	}
1946 }
1947 /* }}} */
1948 
1949 /* {{{ proto int ldap_errno(resource link)
1950    Get the current ldap error number */
PHP_FUNCTION(ldap_errno)1951 PHP_FUNCTION(ldap_errno)
1952 {
1953 	zval *link;
1954 	ldap_linkdata *ld;
1955 
1956 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &link) != SUCCESS) {
1957 		return;
1958 	}
1959 
1960 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
1961 		RETURN_FALSE;
1962 	}
1963 
1964 	RETURN_LONG(_get_lderrno(ld->link));
1965 }
1966 /* }}} */
1967 
1968 /* {{{ proto string ldap_err2str(int errno)
1969    Convert error number to error string */
PHP_FUNCTION(ldap_err2str)1970 PHP_FUNCTION(ldap_err2str)
1971 {
1972 	zend_long perrno;
1973 
1974 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &perrno) != SUCCESS) {
1975 		return;
1976 	}
1977 
1978 	RETURN_STRING(ldap_err2string(perrno));
1979 }
1980 /* }}} */
1981 
1982 /* {{{ proto string ldap_error(resource link)
1983    Get the current ldap error string */
PHP_FUNCTION(ldap_error)1984 PHP_FUNCTION(ldap_error)
1985 {
1986 	zval *link;
1987 	ldap_linkdata *ld;
1988 	int ld_errno;
1989 
1990 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &link) != SUCCESS) {
1991 		return;
1992 	}
1993 
1994 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
1995 		RETURN_FALSE;
1996 	}
1997 
1998 	ld_errno = _get_lderrno(ld->link);
1999 
2000 	RETURN_STRING(ldap_err2string(ld_errno));
2001 }
2002 /* }}} */
2003 
2004 /* {{{ proto bool ldap_compare(resource link, string dn, string attr, string value)
2005    Determine if an entry has a specific value for one of its attributes */
PHP_FUNCTION(ldap_compare)2006 PHP_FUNCTION(ldap_compare)
2007 {
2008 	zval *link;
2009 	char *dn, *attr, *value;
2010 	size_t dn_len, attr_len, value_len;
2011 	ldap_linkdata *ld;
2012 	int errno;
2013 	struct berval lvalue;
2014 
2015 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rsss", &link, &dn, &dn_len, &attr, &attr_len, &value, &value_len) != SUCCESS) {
2016 		return;
2017 	}
2018 
2019 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
2020 		RETURN_FALSE;
2021 	}
2022 
2023 	lvalue.bv_val = value;
2024 	lvalue.bv_len = value_len;
2025 
2026 	errno = ldap_compare_ext_s(ld->link, dn, attr, &lvalue, NULL, NULL);
2027 
2028 	switch (errno) {
2029 		case LDAP_COMPARE_TRUE:
2030 			RETURN_TRUE;
2031 			break;
2032 
2033 		case LDAP_COMPARE_FALSE:
2034 			RETURN_FALSE;
2035 			break;
2036 	}
2037 
2038 	php_error_docref(NULL, E_WARNING, "Compare: %s", ldap_err2string(errno));
2039 	RETURN_LONG(-1);
2040 }
2041 /* }}} */
2042 
2043 /* {{{ proto bool ldap_sort(resource link, resource result, string sortfilter)
2044    Sort LDAP result entries */
PHP_FUNCTION(ldap_sort)2045 PHP_FUNCTION(ldap_sort)
2046 {
2047 	zval *link, *result;
2048 	ldap_linkdata *ld;
2049 	char *sortfilter;
2050 	size_t sflen;
2051 	zend_resource *le;
2052 
2053 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rrs", &link, &result, &sortfilter, &sflen) != SUCCESS) {
2054 		RETURN_FALSE;
2055 	}
2056 
2057 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
2058 		RETURN_FALSE;
2059 	}
2060 
2061 	le = Z_RES_P(result);
2062 	if (le->type != le_result) {
2063 		php_error_docref(NULL, E_WARNING, "Supplied resource is not a valid ldap result resource");
2064 		RETURN_FALSE;
2065 	}
2066 
2067 	if (ldap_sort_entries(ld->link, (LDAPMessage **) &le->ptr, sflen ? sortfilter : NULL, strcmp) != LDAP_SUCCESS) {
2068 		php_error_docref(NULL, E_WARNING, "%s", ldap_err2string(errno));
2069 		RETURN_FALSE;
2070 	}
2071 
2072 	RETURN_TRUE;
2073 }
2074 /* }}} */
2075 
2076 #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP
2077 /* {{{ proto bool ldap_get_option(resource link, int option, mixed retval)
2078    Get the current value of various session-wide parameters */
PHP_FUNCTION(ldap_get_option)2079 PHP_FUNCTION(ldap_get_option)
2080 {
2081 	zval *link, *retval;
2082 	ldap_linkdata *ld;
2083 	zend_long option;
2084 
2085 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rlz/", &link, &option, &retval) != SUCCESS) {
2086 		return;
2087 	}
2088 
2089 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
2090 		RETURN_FALSE;
2091 	}
2092 
2093 	switch (option) {
2094 	/* options with int value */
2095 	case LDAP_OPT_DEREF:
2096 	case LDAP_OPT_SIZELIMIT:
2097 	case LDAP_OPT_TIMELIMIT:
2098 	case LDAP_OPT_PROTOCOL_VERSION:
2099 	case LDAP_OPT_ERROR_NUMBER:
2100 	case LDAP_OPT_REFERRALS:
2101 #ifdef LDAP_OPT_RESTART
2102 	case LDAP_OPT_RESTART:
2103 #endif
2104 #ifdef LDAP_OPT_X_TLS_REQUIRE_CERT
2105 	case LDAP_OPT_X_TLS_REQUIRE_CERT:
2106 #endif
2107 		{
2108 			int val;
2109 
2110 			if (ldap_get_option(ld->link, option, &val)) {
2111 				RETURN_FALSE;
2112 			}
2113 			zval_ptr_dtor(retval);
2114 			ZVAL_LONG(retval, val);
2115 		} break;
2116 #ifdef LDAP_OPT_NETWORK_TIMEOUT
2117 	case LDAP_OPT_NETWORK_TIMEOUT:
2118 		{
2119 			struct timeval *timeout = NULL;
2120 
2121 			if (ldap_get_option(ld->link, LDAP_OPT_NETWORK_TIMEOUT, (void *) &timeout)) {
2122 				if (timeout) {
2123 					ldap_memfree(timeout);
2124 				}
2125 				RETURN_FALSE;
2126 			}
2127 			if (!timeout) {
2128 				RETURN_FALSE;
2129 			}
2130 			zval_ptr_dtor(retval);
2131 			ZVAL_LONG(retval, timeout->tv_sec);
2132 			ldap_memfree(timeout);
2133 		} break;
2134 #elif defined(LDAP_X_OPT_CONNECT_TIMEOUT)
2135 	case LDAP_X_OPT_CONNECT_TIMEOUT:
2136 		{
2137 			int timeout;
2138 
2139 			if (ldap_get_option(ld->link, LDAP_X_OPT_CONNECT_TIMEOUT, &timeout)) {
2140 				RETURN_FALSE;
2141 			}
2142 			zval_ptr_dtor(retval);
2143 			ZVAL_LONG(retval, (timeout / 1000));
2144 		} break;
2145 #endif
2146 #ifdef LDAP_OPT_TIMEOUT
2147 	case LDAP_OPT_TIMEOUT:
2148 		{
2149 			struct timeval *timeout = NULL;
2150 
2151 			if (ldap_get_option(ld->link, LDAP_OPT_TIMEOUT, (void *) &timeout)) {
2152 				if (timeout) {
2153 					ldap_memfree(timeout);
2154 				}
2155 				RETURN_FALSE;
2156 			}
2157 			if (!timeout) {
2158 				RETURN_FALSE;
2159 			}
2160 			zval_dtor(retval);
2161 			ZVAL_LONG(retval, timeout->tv_sec);
2162 			ldap_memfree(timeout);
2163 		} break;
2164 #endif
2165 	/* options with string value */
2166 	case LDAP_OPT_ERROR_STRING:
2167 #ifdef LDAP_OPT_HOST_NAME
2168 	case LDAP_OPT_HOST_NAME:
2169 #endif
2170 #ifdef HAVE_LDAP_SASL
2171 	case LDAP_OPT_X_SASL_MECH:
2172 	case LDAP_OPT_X_SASL_REALM:
2173 	case LDAP_OPT_X_SASL_AUTHCID:
2174 	case LDAP_OPT_X_SASL_AUTHZID:
2175 #endif
2176 #ifdef LDAP_OPT_MATCHED_DN
2177 	case LDAP_OPT_MATCHED_DN:
2178 #endif
2179 		{
2180 			char *val = NULL;
2181 
2182 			if (ldap_get_option(ld->link, option, &val) || val == NULL || *val == '\0') {
2183 				if (val) {
2184 					ldap_memfree(val);
2185 				}
2186 				RETURN_FALSE;
2187 			}
2188 			zval_ptr_dtor(retval);
2189 			ZVAL_STRING(retval, val);
2190 			ldap_memfree(val);
2191 		} break;
2192 /* options not implemented
2193 	case LDAP_OPT_SERVER_CONTROLS:
2194 	case LDAP_OPT_CLIENT_CONTROLS:
2195 	case LDAP_OPT_API_INFO:
2196 	case LDAP_OPT_API_FEATURE_INFO:
2197 */
2198 	default:
2199 		RETURN_FALSE;
2200 	}
2201 	RETURN_TRUE;
2202 }
2203 /* }}} */
2204 
2205 /* {{{ proto bool ldap_set_option(resource link, int option, mixed newval)
2206    Set the value of various session-wide parameters */
PHP_FUNCTION(ldap_set_option)2207 PHP_FUNCTION(ldap_set_option)
2208 {
2209 	zval *link, *newval;
2210 	ldap_linkdata *ld;
2211 	LDAP *ldap;
2212 	zend_long option;
2213 
2214 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "zlz", &link, &option, &newval) != SUCCESS) {
2215 		return;
2216 	}
2217 
2218 	if (Z_TYPE_P(link) == IS_NULL) {
2219 		ldap = NULL;
2220 	} else {
2221 		if ((ld = (ldap_linkdata *)zend_fetch_resource_ex(link, "ldap link", le_link)) == NULL) {
2222 			RETURN_FALSE;
2223 		}
2224 		ldap = ld->link;
2225 	}
2226 
2227 	switch (option) {
2228 	/* options with int value */
2229 	case LDAP_OPT_DEREF:
2230 	case LDAP_OPT_SIZELIMIT:
2231 	case LDAP_OPT_TIMELIMIT:
2232 	case LDAP_OPT_PROTOCOL_VERSION:
2233 	case LDAP_OPT_ERROR_NUMBER:
2234 #ifdef LDAP_OPT_DEBUG_LEVEL
2235 	case LDAP_OPT_DEBUG_LEVEL:
2236 #endif
2237 #ifdef LDAP_OPT_X_TLS_REQUIRE_CERT
2238 	case LDAP_OPT_X_TLS_REQUIRE_CERT:
2239 #endif
2240 		{
2241 			int val;
2242 
2243 			convert_to_long_ex(newval);
2244 			val = Z_LVAL_P(newval);
2245 			if (ldap_set_option(ldap, option, &val)) {
2246 				RETURN_FALSE;
2247 			}
2248 		} break;
2249 #ifdef LDAP_OPT_NETWORK_TIMEOUT
2250 	case LDAP_OPT_NETWORK_TIMEOUT:
2251 		{
2252 			struct timeval timeout;
2253 
2254 			convert_to_long_ex(newval);
2255 			timeout.tv_sec = Z_LVAL_P(newval);
2256 			timeout.tv_usec = 0;
2257 			if (ldap_set_option(ldap, LDAP_OPT_NETWORK_TIMEOUT, (void *) &timeout)) {
2258 				RETURN_FALSE;
2259 			}
2260 		} break;
2261 #elif defined(LDAP_X_OPT_CONNECT_TIMEOUT)
2262 	case LDAP_X_OPT_CONNECT_TIMEOUT:
2263 		{
2264 			int timeout;
2265 
2266 			convert_to_long_ex(newval);
2267 			timeout = 1000 * Z_LVAL_P(newval); /* Convert to milliseconds */
2268 			if (ldap_set_option(ldap, LDAP_X_OPT_CONNECT_TIMEOUT, &timeout)) {
2269 				RETURN_FALSE;
2270 			}
2271 		} break;
2272 #endif
2273 #ifdef LDAP_OPT_TIMEOUT
2274 	case LDAP_OPT_TIMEOUT:
2275 		{
2276 			struct timeval timeout;
2277 
2278 			convert_to_long_ex(newval);
2279 			timeout.tv_sec = Z_LVAL_P(newval);
2280 			timeout.tv_usec = 0;
2281 			if (ldap_set_option(ldap, LDAP_OPT_TIMEOUT, (void *) &timeout)) {
2282 				RETURN_FALSE;
2283 			}
2284 		} break;
2285 #endif
2286 		/* options with string value */
2287 	case LDAP_OPT_ERROR_STRING:
2288 #ifdef LDAP_OPT_HOST_NAME
2289 	case LDAP_OPT_HOST_NAME:
2290 #endif
2291 #ifdef HAVE_LDAP_SASL
2292 	case LDAP_OPT_X_SASL_MECH:
2293 	case LDAP_OPT_X_SASL_REALM:
2294 	case LDAP_OPT_X_SASL_AUTHCID:
2295 	case LDAP_OPT_X_SASL_AUTHZID:
2296 #endif
2297 #ifdef LDAP_OPT_MATCHED_DN
2298 	case LDAP_OPT_MATCHED_DN:
2299 #endif
2300 		{
2301 			char *val;
2302 			convert_to_string_ex(newval);
2303 			val = Z_STRVAL_P(newval);
2304 			if (ldap_set_option(ldap, option, val)) {
2305 				RETURN_FALSE;
2306 			}
2307 		} break;
2308 		/* options with boolean value */
2309 	case LDAP_OPT_REFERRALS:
2310 #ifdef LDAP_OPT_RESTART
2311 	case LDAP_OPT_RESTART:
2312 #endif
2313 		{
2314 			void *val;
2315 			convert_to_boolean_ex(newval);
2316 			val = Z_TYPE_P(newval) == IS_TRUE
2317 				? LDAP_OPT_ON : LDAP_OPT_OFF;
2318 			if (ldap_set_option(ldap, option, val)) {
2319 				RETURN_FALSE;
2320 			}
2321 		} break;
2322 		/* options with control list value */
2323 	case LDAP_OPT_SERVER_CONTROLS:
2324 	case LDAP_OPT_CLIENT_CONTROLS:
2325 		{
2326 			LDAPControl *ctrl, **ctrls, **ctrlp;
2327 			zval *ctrlval, *val;
2328 			int ncontrols;
2329 			char error=0;
2330 
2331 			if ((Z_TYPE_P(newval) != IS_ARRAY) || !(ncontrols = zend_hash_num_elements(Z_ARRVAL_P(newval)))) {
2332 				php_error_docref(NULL, E_WARNING, "Expected non-empty array value for this option");
2333 				RETURN_FALSE;
2334 			}
2335 			ctrls = safe_emalloc((1 + ncontrols), sizeof(*ctrls), 0);
2336 			*ctrls = NULL;
2337 			ctrlp = ctrls;
2338 			ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(newval), ctrlval) {
2339 				if (Z_TYPE_P(ctrlval) != IS_ARRAY) {
2340 					php_error_docref(NULL, E_WARNING, "The array value must contain only arrays, where each array is a control");
2341 					error = 1;
2342 					break;
2343 				}
2344 				if ((val = zend_hash_str_find(Z_ARRVAL_P(ctrlval), "oid", sizeof("oid") - 1)) == NULL) {
2345 					php_error_docref(NULL, E_WARNING, "Control must have an oid key");
2346 					error = 1;
2347 					break;
2348 				}
2349 				ctrl = *ctrlp = emalloc(sizeof(**ctrlp));
2350 				convert_to_string_ex(val);
2351 				ctrl->ldctl_oid = Z_STRVAL_P(val);
2352 				if ((val = zend_hash_str_find(Z_ARRVAL_P(ctrlval), "value", sizeof("value") - 1)) != NULL) {
2353 					convert_to_string_ex(val);
2354 					ctrl->ldctl_value.bv_val = Z_STRVAL_P(val);
2355 					ctrl->ldctl_value.bv_len = Z_STRLEN_P(val);
2356 				} else {
2357 					ctrl->ldctl_value.bv_val = NULL;
2358 					ctrl->ldctl_value.bv_len = 0;
2359 				}
2360 				if ((val = zend_hash_str_find(Z_ARRVAL_P(ctrlval), "iscritical", sizeof("iscritical") - 1)) != NULL) {
2361 					convert_to_boolean_ex(val);
2362 					ctrl->ldctl_iscritical = Z_TYPE_P(val) == IS_TRUE;
2363 				} else {
2364 					ctrl->ldctl_iscritical = 0;
2365 				}
2366 
2367 				++ctrlp;
2368 				*ctrlp = NULL;
2369 			} ZEND_HASH_FOREACH_END();
2370 			if (!error) {
2371 				error = ldap_set_option(ldap, option, ctrls);
2372 			}
2373 			ctrlp = ctrls;
2374 			while (*ctrlp) {
2375 				efree(*ctrlp);
2376 				ctrlp++;
2377 			}
2378 			efree(ctrls);
2379 			if (error) {
2380 				RETURN_FALSE;
2381 			}
2382 		} break;
2383 	default:
2384 		RETURN_FALSE;
2385 	}
2386 	RETURN_TRUE;
2387 }
2388 /* }}} */
2389 
2390 #ifdef HAVE_LDAP_PARSE_RESULT
2391 /* {{{ proto bool ldap_parse_result(resource link, resource result, int errcode, string matcheddn, string errmsg, array referrals)
2392    Extract information from result */
PHP_FUNCTION(ldap_parse_result)2393 PHP_FUNCTION(ldap_parse_result)
2394 {
2395 	zval *link, *result, *errcode, *matcheddn, *errmsg, *referrals;
2396 	ldap_linkdata *ld;
2397 	LDAPMessage *ldap_result;
2398 	char **lreferrals, **refp;
2399 	char *lmatcheddn, *lerrmsg;
2400 	int rc, lerrcode, myargcount = ZEND_NUM_ARGS();
2401 
2402 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rrz/|z/z/z/", &link, &result, &errcode, &matcheddn, &errmsg, &referrals) != SUCCESS) {
2403 		return;
2404 	}
2405 
2406 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
2407 		RETURN_FALSE;
2408 	}
2409 
2410 	if ((ldap_result = (LDAPMessage *)zend_fetch_resource(Z_RES_P(result), "ldap result", le_result)) == NULL) {
2411 		RETURN_FALSE;
2412 	}
2413 
2414 	rc = ldap_parse_result(ld->link, ldap_result, &lerrcode,
2415 				myargcount > 3 ? &lmatcheddn : NULL,
2416 				myargcount > 4 ? &lerrmsg : NULL,
2417 				myargcount > 5 ? &lreferrals : NULL,
2418 				NULL /* &serverctrls */,
2419 				0);
2420 	if (rc != LDAP_SUCCESS) {
2421 		php_error_docref(NULL, E_WARNING, "Unable to parse result: %s", ldap_err2string(rc));
2422 		RETURN_FALSE;
2423 	}
2424 
2425 	zval_ptr_dtor(errcode);
2426 	ZVAL_LONG(errcode, lerrcode);
2427 
2428 	/* Reverse -> fall through */
2429 	switch (myargcount) {
2430 		case 6:
2431 			zval_ptr_dtor(referrals);
2432 			array_init(referrals);
2433 			if (lreferrals != NULL) {
2434 				refp = lreferrals;
2435 				while (*refp) {
2436 					add_next_index_string(referrals, *refp);
2437 					refp++;
2438 				}
2439 				ldap_memvfree((void**)lreferrals);
2440 			}
2441 		case 5:
2442 			zval_ptr_dtor(errmsg);
2443 			if (lerrmsg == NULL) {
2444 				ZVAL_EMPTY_STRING(errmsg);
2445 			} else {
2446 				ZVAL_STRING(errmsg, lerrmsg);
2447 				ldap_memfree(lerrmsg);
2448 			}
2449 		case 4:
2450 			zval_ptr_dtor(matcheddn);
2451 			if (lmatcheddn == NULL) {
2452 				ZVAL_EMPTY_STRING(matcheddn);
2453 			} else {
2454 				ZVAL_STRING(matcheddn, lmatcheddn);
2455 				ldap_memfree(lmatcheddn);
2456 			}
2457 	}
2458 	RETURN_TRUE;
2459 }
2460 /* }}} */
2461 #endif
2462 
2463 /* {{{ proto resource ldap_first_reference(resource link, resource result)
2464    Return first reference */
PHP_FUNCTION(ldap_first_reference)2465 PHP_FUNCTION(ldap_first_reference)
2466 {
2467 	zval *link, *result;
2468 	ldap_linkdata *ld;
2469 	ldap_resultentry *resultentry;
2470 	LDAPMessage *ldap_result, *entry;
2471 
2472 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rr", &link, &result) != SUCCESS) {
2473 		return;
2474 	}
2475 
2476 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
2477 		RETURN_FALSE;
2478 	}
2479 
2480 	if ((ldap_result = (LDAPMessage *)zend_fetch_resource(Z_RES_P(result), "ldap result", le_result)) == NULL) {
2481 		RETURN_FALSE;
2482 	}
2483 
2484 	if ((entry = ldap_first_reference(ld->link, ldap_result)) == NULL) {
2485 		RETVAL_FALSE;
2486 	} else {
2487 		resultentry = emalloc(sizeof(ldap_resultentry));
2488 		RETVAL_RES(zend_register_resource(resultentry, le_result_entry));
2489 		ZVAL_COPY(&resultentry->res, result);
2490 		resultentry->data = entry;
2491 		resultentry->ber = NULL;
2492 	}
2493 }
2494 /* }}} */
2495 
2496 /* {{{ proto resource ldap_next_reference(resource link, resource reference_entry)
2497    Get next reference */
PHP_FUNCTION(ldap_next_reference)2498 PHP_FUNCTION(ldap_next_reference)
2499 {
2500 	zval *link, *result_entry;
2501 	ldap_linkdata *ld;
2502 	ldap_resultentry *resultentry, *resultentry_next;
2503 	LDAPMessage *entry_next;
2504 
2505 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rr", &link, &result_entry) != SUCCESS) {
2506 		return;
2507 	}
2508 
2509 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
2510 		RETURN_FALSE;
2511 	}
2512 
2513 	if ((resultentry = (ldap_resultentry *)zend_fetch_resource(Z_RES_P(result_entry), "ldap result entry", le_result_entry)) == NULL) {
2514 		RETURN_FALSE;
2515 	}
2516 
2517 	if ((entry_next = ldap_next_reference(ld->link, resultentry->data)) == NULL) {
2518 		RETVAL_FALSE;
2519 	} else {
2520 		resultentry_next = emalloc(sizeof(ldap_resultentry));
2521 		RETVAL_RES(zend_register_resource(resultentry_next, le_result_entry));
2522 		ZVAL_COPY(&resultentry_next->res, &resultentry->res);
2523 		resultentry_next->data = entry_next;
2524 		resultentry_next->ber = NULL;
2525 	}
2526 }
2527 /* }}} */
2528 
2529 #ifdef HAVE_LDAP_PARSE_REFERENCE
2530 /* {{{ proto bool ldap_parse_reference(resource link, resource reference_entry, array referrals)
2531    Extract information from reference entry */
PHP_FUNCTION(ldap_parse_reference)2532 PHP_FUNCTION(ldap_parse_reference)
2533 {
2534 	zval *link, *result_entry, *referrals;
2535 	ldap_linkdata *ld;
2536 	ldap_resultentry *resultentry;
2537 	char **lreferrals, **refp;
2538 
2539 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rrz/", &link, &result_entry, &referrals) != SUCCESS) {
2540 		return;
2541 	}
2542 
2543 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
2544 		RETURN_FALSE;
2545 	}
2546 
2547 	if ((resultentry = (ldap_resultentry *)zend_fetch_resource(Z_RES_P(result_entry), "ldap result entry", le_result_entry)) == NULL) {
2548 		RETURN_FALSE;
2549 	}
2550 
2551 	if (ldap_parse_reference(ld->link, resultentry->data, &lreferrals, NULL /* &serverctrls */, 0) != LDAP_SUCCESS) {
2552 		RETURN_FALSE;
2553 	}
2554 
2555 	zval_ptr_dtor(referrals);
2556 	array_init(referrals);
2557 	if (lreferrals != NULL) {
2558 		refp = lreferrals;
2559 		while (*refp) {
2560 			add_next_index_string(referrals, *refp);
2561 			refp++;
2562 		}
2563 		ldap_memvfree((void**)lreferrals);
2564 	}
2565 	RETURN_TRUE;
2566 }
2567 /* }}} */
2568 #endif
2569 
2570 /* {{{ proto bool ldap_rename(resource link, string dn, string newrdn, string newparent, bool deleteoldrdn)
2571    Modify the name of an entry */
PHP_FUNCTION(ldap_rename)2572 PHP_FUNCTION(ldap_rename)
2573 {
2574 	zval *link;
2575 	ldap_linkdata *ld;
2576 	int rc;
2577 	char *dn, *newrdn, *newparent;
2578 	size_t dn_len, newrdn_len, newparent_len;
2579 	zend_bool deleteoldrdn;
2580 
2581 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rsssb", &link, &dn, &dn_len, &newrdn, &newrdn_len, &newparent, &newparent_len, &deleteoldrdn) != SUCCESS) {
2582 		return;
2583 	}
2584 
2585 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
2586 		RETURN_FALSE;
2587 	}
2588 
2589 	if (newparent_len == 0) {
2590 		newparent = NULL;
2591 	}
2592 
2593 #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP
2594 	rc = ldap_rename_s(ld->link, dn, newrdn, newparent, deleteoldrdn, NULL, NULL);
2595 #else
2596 	if (newparent_len != 0) {
2597 		php_error_docref(NULL, E_WARNING, "You are using old LDAP API, newparent must be the empty string, can only modify RDN");
2598 		RETURN_FALSE;
2599 	}
2600 /* could support old APIs but need check for ldap_modrdn2()/ldap_modrdn() */
2601 	rc = ldap_modrdn2_s(ld->link, dn, newrdn, deleteoldrdn);
2602 #endif
2603 
2604 	if (rc == LDAP_SUCCESS) {
2605 		RETURN_TRUE;
2606 	}
2607 	RETURN_FALSE;
2608 }
2609 /* }}} */
2610 
2611 #ifdef HAVE_LDAP_START_TLS_S
2612 /* {{{ proto bool ldap_start_tls(resource link)
2613    Start TLS */
PHP_FUNCTION(ldap_start_tls)2614 PHP_FUNCTION(ldap_start_tls)
2615 {
2616 	zval *link;
2617 	ldap_linkdata *ld;
2618 	int rc, protocol = LDAP_VERSION3;
2619 
2620 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &link) != SUCCESS) {
2621 		return;
2622 	}
2623 
2624 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
2625 		RETURN_FALSE;
2626 	}
2627 
2628 	if (((rc = ldap_set_option(ld->link, LDAP_OPT_PROTOCOL_VERSION, &protocol)) != LDAP_SUCCESS) ||
2629 		((rc = ldap_start_tls_s(ld->link, NULL, NULL)) != LDAP_SUCCESS)
2630 	) {
2631 		php_error_docref(NULL, E_WARNING,"Unable to start TLS: %s", ldap_err2string(rc));
2632 		RETURN_FALSE;
2633 	} else {
2634 		RETURN_TRUE;
2635 	}
2636 }
2637 /* }}} */
2638 #endif
2639 #endif /* (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP */
2640 
2641 #if defined(LDAP_API_FEATURE_X_OPENLDAP) && defined(HAVE_3ARG_SETREBINDPROC)
2642 /* {{{ _ldap_rebind_proc()
2643 */
_ldap_rebind_proc(LDAP * ldap,const char * url,ber_tag_t req,ber_int_t msgid,void * params)2644 int _ldap_rebind_proc(LDAP *ldap, const char *url, ber_tag_t req, ber_int_t msgid, void *params)
2645 {
2646 	ldap_linkdata *ld;
2647 	int retval;
2648 	zval cb_args[2];
2649 	zval cb_retval;
2650 	zval *cb_link = (zval *) params;
2651 
2652 	ld = (ldap_linkdata *) zend_fetch_resource_ex(cb_link, "ldap link", le_link);
2653 
2654 	/* link exists and callback set? */
2655 	if (ld == NULL || Z_ISUNDEF(ld->rebindproc)) {
2656 		php_error_docref(NULL, E_WARNING, "Link not found or no callback set");
2657 		return LDAP_OTHER;
2658 	}
2659 
2660 	/* callback */
2661 	ZVAL_COPY_VALUE(&cb_args[0], cb_link);
2662 	ZVAL_STRING(&cb_args[1], url);
2663 	if (call_user_function_ex(EG(function_table), NULL, &ld->rebindproc, &cb_retval, 2, cb_args, 0, NULL) == SUCCESS && !Z_ISUNDEF(cb_retval)) {
2664 		convert_to_long_ex(&cb_retval);
2665 		retval = Z_LVAL(cb_retval);
2666 		zval_ptr_dtor(&cb_retval);
2667 	} else {
2668 		php_error_docref(NULL, E_WARNING, "rebind_proc PHP callback failed");
2669 		retval = LDAP_OTHER;
2670 	}
2671 	zval_ptr_dtor(&cb_args[1]);
2672 	return retval;
2673 }
2674 /* }}} */
2675 
2676 /* {{{ proto bool ldap_set_rebind_proc(resource link, string callback)
2677    Set a callback function to do re-binds on referral chasing. */
PHP_FUNCTION(ldap_set_rebind_proc)2678 PHP_FUNCTION(ldap_set_rebind_proc)
2679 {
2680 	zval *link, *callback;
2681 	ldap_linkdata *ld;
2682 	zend_string *callback_name;
2683 
2684 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &link, &callback) != SUCCESS) {
2685 		RETURN_FALSE;
2686 	}
2687 
2688 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
2689 		RETURN_FALSE;
2690 	}
2691 
2692 	if (Z_TYPE_P(callback) == IS_STRING && Z_STRLEN_P(callback) == 0) {
2693 		/* unregister rebind procedure */
2694 		if (!Z_ISUNDEF(ld->rebindproc)) {
2695 			zval_ptr_dtor(&ld->rebindproc);
2696 			ZVAL_UNDEF(&ld->rebindproc);
2697 			ldap_set_rebind_proc(ld->link, NULL, NULL);
2698 		}
2699 		RETURN_TRUE;
2700 	}
2701 
2702 	/* callable? */
2703 	if (!zend_is_callable(callback, 0, &callback_name)) {
2704 		php_error_docref(NULL, E_WARNING, "Two arguments expected for '%s' to be a valid callback", ZSTR_VAL(callback_name));
2705 		zend_string_release(callback_name);
2706 		RETURN_FALSE;
2707 	}
2708 	zend_string_release(callback_name);
2709 
2710 	/* register rebind procedure */
2711 	if (Z_ISUNDEF(ld->rebindproc)) {
2712 		ldap_set_rebind_proc(ld->link, _ldap_rebind_proc, (void *) link);
2713 	} else {
2714 		zval_ptr_dtor(&ld->rebindproc);
2715 	}
2716 
2717 	ZVAL_COPY(&ld->rebindproc, callback);
2718 	RETURN_TRUE;
2719 }
2720 /* }}} */
2721 #endif
2722 
php_ldap_do_escape(const zend_bool * map,const char * value,size_t valuelen)2723 static zend_string* php_ldap_do_escape(const zend_bool *map, const char *value, size_t valuelen)
2724 {
2725 	char hex[] = "0123456789abcdef";
2726 	int i, p = 0;
2727 	size_t len = 0;
2728 	zend_string *ret;
2729 
2730 	for (i = 0; i < valuelen; i++) {
2731 		len += (map[(unsigned char) value[i]]) ? 3 : 1;
2732 	}
2733 
2734 	ret =  zend_string_alloc(len, 0);
2735 
2736 	for (i = 0; i < valuelen; i++) {
2737 		unsigned char v = (unsigned char) value[i];
2738 
2739 		if (map[v]) {
2740 			ZSTR_VAL(ret)[p++] = '\\';
2741 			ZSTR_VAL(ret)[p++] = hex[v >> 4];
2742 			ZSTR_VAL(ret)[p++] = hex[v & 0x0f];
2743 		} else {
2744 			ZSTR_VAL(ret)[p++] = v;
2745 		}
2746 	}
2747 
2748 	ZSTR_VAL(ret)[p] = '\0';
2749 	ZSTR_LEN(ret) = p;
2750 	return ret;
2751 }
2752 
php_ldap_escape_map_set_chars(zend_bool * map,const char * chars,const int charslen,char escape)2753 static void php_ldap_escape_map_set_chars(zend_bool *map, const char *chars, const int charslen, char escape)
2754 {
2755 	int i = 0;
2756 	while (i < charslen) {
2757 		map[(unsigned char) chars[i++]] = escape;
2758 	}
2759 }
2760 
PHP_FUNCTION(ldap_escape)2761 PHP_FUNCTION(ldap_escape)
2762 {
2763 	char *value, *ignores;
2764 	size_t valuelen = 0, ignoreslen = 0;
2765 	int i;
2766 	zend_long flags = 0;
2767 	zend_bool map[256] = {0}, havecharlist = 0;
2768 
2769 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|sl", &value, &valuelen, &ignores, &ignoreslen, &flags) != SUCCESS) {
2770 		return;
2771 	}
2772 
2773 	if (!valuelen) {
2774 		RETURN_EMPTY_STRING();
2775 	}
2776 
2777 	if (flags & PHP_LDAP_ESCAPE_FILTER) {
2778 		havecharlist = 1;
2779 		php_ldap_escape_map_set_chars(map, "\\*()\0", sizeof("\\*()\0") - 1, 1);
2780 	}
2781 
2782 	if (flags & PHP_LDAP_ESCAPE_DN) {
2783 		havecharlist = 1;
2784 		php_ldap_escape_map_set_chars(map, "\\,=+<>;\"#", sizeof("\\,=+<>;\"#") - 1, 1);
2785 	}
2786 
2787 	if (!havecharlist) {
2788 		for (i = 0; i < 256; i++) {
2789 			map[i] = 1;
2790 		}
2791 	}
2792 
2793 	if (ignoreslen) {
2794 		php_ldap_escape_map_set_chars(map, ignores, ignoreslen, 0);
2795 	}
2796 
2797 	RETURN_NEW_STR(php_ldap_do_escape(map, value, valuelen));
2798 }
2799 
2800 #ifdef STR_TRANSLATION
2801 /* {{{ php_ldap_do_translate
2802  */
php_ldap_do_translate(INTERNAL_FUNCTION_PARAMETERS,int way)2803 static void php_ldap_do_translate(INTERNAL_FUNCTION_PARAMETERS, int way)
2804 {
2805 	char *value;
2806 	size_t value_len;
2807 	int result;
2808 
2809 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &value, &value_len) != SUCCESS) {
2810 		return;
2811 	}
2812 
2813 	if (value_len == 0) {
2814 		RETURN_FALSE;
2815 	}
2816 
2817 	if (way == 1) {
2818 		result = ldap_8859_to_t61(&value, &value_len, 0);
2819 	} else {
2820 		result = ldap_t61_to_8859(&value, &value_len, 0);
2821 	}
2822 
2823 	if (result == LDAP_SUCCESS) {
2824 		RETVAL_STRINGL(value, value_len);
2825 		free(value);
2826 	} else {
2827 		php_error_docref(NULL, E_WARNING, "Conversion from iso-8859-1 to t61 failed: %s", ldap_err2string(result));
2828 		RETVAL_FALSE;
2829 	}
2830 }
2831 /* }}} */
2832 
2833 /* {{{ proto string ldap_t61_to_8859(string value)
2834    Translate t61 characters to 8859 characters */
PHP_FUNCTION(ldap_t61_to_8859)2835 PHP_FUNCTION(ldap_t61_to_8859)
2836 {
2837 	php_ldap_do_translate(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
2838 }
2839 /* }}} */
2840 
2841 /* {{{ proto string ldap_8859_to_t61(string value)
2842    Translate 8859 characters to t61 characters */
PHP_FUNCTION(ldap_8859_to_t61)2843 PHP_FUNCTION(ldap_8859_to_t61)
2844 {
2845 	php_ldap_do_translate(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
2846 }
2847 /* }}} */
2848 #endif
2849 
2850 #ifdef LDAP_CONTROL_PAGEDRESULTS
2851 /* {{{ proto mixed ldap_control_paged_result(resource link, int pagesize [, bool iscritical [, string cookie]])
2852    Inject paged results control*/
PHP_FUNCTION(ldap_control_paged_result)2853 PHP_FUNCTION(ldap_control_paged_result)
2854 {
2855 	zend_long pagesize;
2856 	zend_bool iscritical;
2857 	zval *link;
2858 	char *cookie = NULL;
2859 	size_t cookie_len = 0;
2860 	struct berval lcookie = { 0, NULL };
2861 	ldap_linkdata *ld;
2862 	LDAP *ldap;
2863 	BerElement *ber = NULL;
2864 	LDAPControl	ctrl, *ctrlsp[2];
2865 	int rc, myargcount = ZEND_NUM_ARGS();
2866 
2867 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl|bs", &link, &pagesize, &iscritical, &cookie, &cookie_len) != SUCCESS) {
2868 		return;
2869 	}
2870 
2871 	if (Z_TYPE_P(link) == IS_NULL) {
2872 		ldap = NULL;
2873 	} else {
2874 		if ((ld = (ldap_linkdata *)zend_fetch_resource_ex(link, "ldap link", le_link)) == NULL) {
2875 			RETURN_FALSE;
2876 		}
2877 		ldap = ld->link;
2878 	}
2879 
2880 	ber = ber_alloc_t(LBER_USE_DER);
2881 	if (ber == NULL) {
2882 		php_error_docref(NULL, E_WARNING, "Unable to alloc BER encoding resources for paged results control");
2883 		RETURN_FALSE;
2884 	}
2885 
2886 	ctrl.ldctl_iscritical = 0;
2887 
2888 	switch (myargcount) {
2889 		case 4:
2890 			lcookie.bv_val = cookie;
2891 			lcookie.bv_len = cookie_len;
2892 			/* fallthru */
2893 		case 3:
2894 			ctrl.ldctl_iscritical = (int)iscritical;
2895 			/* fallthru */
2896 	}
2897 
2898 	if (ber_printf(ber, "{iO}", (int)pagesize, &lcookie) == LBER_ERROR) {
2899 		php_error_docref(NULL, E_WARNING, "Unable to BER printf paged results control");
2900 		RETVAL_FALSE;
2901 		goto lcpr_error_out;
2902 	}
2903 	rc = ber_flatten2(ber, &ctrl.ldctl_value, 0);
2904 	if (rc == LBER_ERROR) {
2905 		php_error_docref(NULL, E_WARNING, "Unable to BER encode paged results control");
2906 		RETVAL_FALSE;
2907 		goto lcpr_error_out;
2908 	}
2909 
2910 	ctrl.ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
2911 
2912 	if (ldap) {
2913 		/* directly set the option */
2914 		ctrlsp[0] = &ctrl;
2915 		ctrlsp[1] = NULL;
2916 
2917 		rc = ldap_set_option(ldap, LDAP_OPT_SERVER_CONTROLS, ctrlsp);
2918 		if (rc != LDAP_SUCCESS) {
2919 			php_error_docref(NULL, E_WARNING, "Unable to set paged results control: %s (%d)", ldap_err2string(rc), rc);
2920 			RETVAL_FALSE;
2921 			goto lcpr_error_out;
2922 		}
2923 		RETVAL_TRUE;
2924 	} else {
2925 		/* return a PHP control object */
2926 		array_init(return_value);
2927 
2928 		add_assoc_string(return_value, "oid", ctrl.ldctl_oid);
2929 		if (ctrl.ldctl_value.bv_len) {
2930 			add_assoc_stringl(return_value, "value", ctrl.ldctl_value.bv_val, ctrl.ldctl_value.bv_len);
2931 		}
2932 		if (ctrl.ldctl_iscritical) {
2933 			add_assoc_bool(return_value, "iscritical", ctrl.ldctl_iscritical);
2934 		}
2935 	}
2936 
2937 lcpr_error_out:
2938 	if (ber != NULL) {
2939 		ber_free(ber, 1);
2940 	}
2941 	return;
2942 }
2943 /* }}} */
2944 
2945 /* {{{ proto bool ldap_control_paged_result_response(resource link, resource result [, string &cookie [, int &estimated]])
2946    Extract paged results control response */
PHP_FUNCTION(ldap_control_paged_result_response)2947 PHP_FUNCTION(ldap_control_paged_result_response)
2948 {
2949 	zval *link, *result, *cookie, *estimated;
2950 	struct berval lcookie;
2951 	int lestimated;
2952 	ldap_linkdata *ld;
2953 	LDAPMessage *ldap_result;
2954 	LDAPControl **lserverctrls, *lctrl;
2955 	BerElement *ber;
2956 	ber_tag_t tag;
2957 	int rc, lerrcode, myargcount = ZEND_NUM_ARGS();
2958 
2959 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rr|z/z/", &link, &result, &cookie, &estimated) != SUCCESS) {
2960 		return;
2961 	}
2962 
2963 	if ((ld = (ldap_linkdata *)zend_fetch_resource(Z_RES_P(link), "ldap link", le_link)) == NULL) {
2964 		RETURN_FALSE;
2965 	}
2966 
2967 	if ((ldap_result = (LDAPMessage *)zend_fetch_resource(Z_RES_P(result), "ldap result", le_result)) == NULL) {
2968 		RETURN_FALSE;
2969 	}
2970 
2971 	rc = ldap_parse_result(ld->link,
2972 				ldap_result,
2973 				&lerrcode,
2974 				NULL,		/* matcheddn */
2975 				NULL,		/* errmsg */
2976 				NULL,		/* referrals */
2977 				&lserverctrls,
2978 				0);
2979 
2980 	if (rc != LDAP_SUCCESS) {
2981 		php_error_docref(NULL, E_WARNING, "Unable to parse result: %s (%d)", ldap_err2string(rc), rc);
2982 		RETURN_FALSE;
2983 	}
2984 
2985 	if (lerrcode != LDAP_SUCCESS) {
2986 		php_error_docref(NULL, E_WARNING, "Result is: %s (%d)", ldap_err2string(lerrcode), lerrcode);
2987 		RETURN_FALSE;
2988 	}
2989 
2990 	if (lserverctrls == NULL) {
2991 		php_error_docref(NULL, E_WARNING, "No server controls in result");
2992 		RETURN_FALSE;
2993 	}
2994 
2995 	lctrl = ldap_control_find(LDAP_CONTROL_PAGEDRESULTS, lserverctrls, NULL);
2996 	if (lctrl == NULL) {
2997 		ldap_controls_free(lserverctrls);
2998 		php_error_docref(NULL, E_WARNING, "No paged results control response in result");
2999 		RETURN_FALSE;
3000 	}
3001 
3002 	ber = ber_init(&lctrl->ldctl_value);
3003 	if (ber == NULL) {
3004 		ldap_controls_free(lserverctrls);
3005 		php_error_docref(NULL, E_WARNING, "Unable to alloc BER decoding resources for paged results control response");
3006 		RETURN_FALSE;
3007 	}
3008 
3009 	tag = ber_scanf(ber, "{io}", &lestimated, &lcookie);
3010 	(void)ber_free(ber, 1);
3011 
3012 	if (tag == LBER_ERROR) {
3013 		ldap_controls_free(lserverctrls);
3014 		php_error_docref(NULL, E_WARNING, "Unable to decode paged results control response");
3015 		RETURN_FALSE;
3016 	}
3017 
3018 	if (lestimated < 0) {
3019 		ldap_controls_free(lserverctrls);
3020 		php_error_docref(NULL, E_WARNING, "Invalid paged results control response value");
3021 		RETURN_FALSE;
3022 	}
3023 
3024 	ldap_controls_free(lserverctrls);
3025 	if (myargcount == 4) {
3026 		zval_dtor(estimated);
3027 		ZVAL_LONG(estimated, lestimated);
3028 	}
3029 
3030 	zval_ptr_dtor(cookie);
3031  	if (lcookie.bv_len == 0) {
3032 		ZVAL_EMPTY_STRING(cookie);
3033  	} else {
3034 		ZVAL_STRINGL(cookie, lcookie.bv_val, lcookie.bv_len);
3035  	}
3036  	ldap_memfree(lcookie.bv_val);
3037 
3038 	RETURN_TRUE;
3039 }
3040 /* }}} */
3041 #endif
3042 
3043 /* {{{ arginfo */
3044 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_connect, 0, 0, 0)
3045 	ZEND_ARG_INFO(0, hostname)
3046 	ZEND_ARG_INFO(0, port)
3047 #ifdef HAVE_ORALDAP
3048 	ZEND_ARG_INFO(0, wallet)
3049 	ZEND_ARG_INFO(0, wallet_passwd)
3050 	ZEND_ARG_INFO(0, authmode)
3051 #endif
3052 ZEND_END_ARG_INFO()
3053 
3054 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_resource, 0, 0, 1)
3055 	ZEND_ARG_INFO(0, link_identifier)
3056 ZEND_END_ARG_INFO()
3057 
3058 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_bind, 0, 0, 1)
3059 	ZEND_ARG_INFO(0, link_identifier)
3060 	ZEND_ARG_INFO(0, bind_rdn)
3061 	ZEND_ARG_INFO(0, bind_password)
3062 ZEND_END_ARG_INFO()
3063 
3064 #ifdef HAVE_LDAP_SASL
3065 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_sasl_bind, 0, 0, 1)
3066 	ZEND_ARG_INFO(0, link)
3067 	ZEND_ARG_INFO(0, binddn)
3068 	ZEND_ARG_INFO(0, password)
3069 	ZEND_ARG_INFO(0, sasl_mech)
3070 	ZEND_ARG_INFO(0, sasl_realm)
3071 	ZEND_ARG_INFO(0, sasl_authz_id)
3072 	ZEND_ARG_INFO(0, props)
3073 ZEND_END_ARG_INFO()
3074 #endif
3075 
3076 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_read, 0, 0, 3)
3077 	ZEND_ARG_INFO(0, link_identifier)
3078 	ZEND_ARG_INFO(0, base_dn)
3079 	ZEND_ARG_INFO(0, filter)
3080 	ZEND_ARG_INFO(0, attributes)
3081 	ZEND_ARG_INFO(0, attrsonly)
3082 	ZEND_ARG_INFO(0, sizelimit)
3083 	ZEND_ARG_INFO(0, timelimit)
3084 	ZEND_ARG_INFO(0, deref)
3085 ZEND_END_ARG_INFO()
3086 
3087 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_list, 0, 0, 3)
3088 	ZEND_ARG_INFO(0, link_identifier)
3089 	ZEND_ARG_INFO(0, base_dn)
3090 	ZEND_ARG_INFO(0, filter)
3091 	ZEND_ARG_INFO(0, attributes)
3092 	ZEND_ARG_INFO(0, attrsonly)
3093 	ZEND_ARG_INFO(0, sizelimit)
3094 	ZEND_ARG_INFO(0, timelimit)
3095 	ZEND_ARG_INFO(0, deref)
3096 ZEND_END_ARG_INFO()
3097 
3098 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_search, 0, 0, 3)
3099 	ZEND_ARG_INFO(0, link_identifier)
3100 	ZEND_ARG_INFO(0, base_dn)
3101 	ZEND_ARG_INFO(0, filter)
3102 	ZEND_ARG_INFO(0, attributes)
3103 	ZEND_ARG_INFO(0, attrsonly)
3104 	ZEND_ARG_INFO(0, sizelimit)
3105 	ZEND_ARG_INFO(0, timelimit)
3106 	ZEND_ARG_INFO(0, deref)
3107 ZEND_END_ARG_INFO()
3108 
3109 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_count_entries, 0, 0, 2)
3110 	ZEND_ARG_INFO(0, link_identifier)
3111 	ZEND_ARG_INFO(0, result_identifier)
3112 ZEND_END_ARG_INFO()
3113 
3114 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_first_entry, 0, 0, 2)
3115 	ZEND_ARG_INFO(0, link_identifier)
3116 	ZEND_ARG_INFO(0, result_identifier)
3117 ZEND_END_ARG_INFO()
3118 
3119 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_next_entry, 0, 0, 2)
3120 	ZEND_ARG_INFO(0, link_identifier)
3121 	ZEND_ARG_INFO(0, result_identifier)
3122 ZEND_END_ARG_INFO()
3123 
3124 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_get_entries, 0, 0, 2)
3125 	ZEND_ARG_INFO(0, link_identifier)
3126 	ZEND_ARG_INFO(0, result_identifier)
3127 ZEND_END_ARG_INFO()
3128 
3129 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_first_attribute, 0, 0, 2)
3130 	ZEND_ARG_INFO(0, link_identifier)
3131 	ZEND_ARG_INFO(0, result_entry_identifier)
3132 ZEND_END_ARG_INFO()
3133 
3134 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_next_attribute, 0, 0, 2)
3135 	ZEND_ARG_INFO(0, link_identifier)
3136 	ZEND_ARG_INFO(0, result_entry_identifier)
3137 ZEND_END_ARG_INFO()
3138 
3139 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_get_attributes, 0, 0, 2)
3140 	ZEND_ARG_INFO(0, link_identifier)
3141 	ZEND_ARG_INFO(0, result_entry_identifier)
3142 ZEND_END_ARG_INFO()
3143 
3144 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_get_values, 0, 0, 3)
3145 	ZEND_ARG_INFO(0, link_identifier)
3146 	ZEND_ARG_INFO(0, result_entry_identifier)
3147 	ZEND_ARG_INFO(0, attribute)
3148 ZEND_END_ARG_INFO()
3149 
3150 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_get_values_len, 0, 0, 3)
3151 	ZEND_ARG_INFO(0, link_identifier)
3152 	ZEND_ARG_INFO(0, result_entry_identifier)
3153 	ZEND_ARG_INFO(0, attribute)
3154 ZEND_END_ARG_INFO()
3155 
3156 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_get_dn, 0, 0, 2)
3157 	ZEND_ARG_INFO(0, link_identifier)
3158 	ZEND_ARG_INFO(0, result_entry_identifier)
3159 ZEND_END_ARG_INFO()
3160 
3161 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_explode_dn, 0, 0, 2)
3162 	ZEND_ARG_INFO(0, dn)
3163 	ZEND_ARG_INFO(0, with_attrib)
3164 ZEND_END_ARG_INFO()
3165 
3166 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_dn2ufn, 0, 0, 1)
3167 	ZEND_ARG_INFO(0, dn)
3168 ZEND_END_ARG_INFO()
3169 
3170 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_add, 0, 0, 3)
3171 	ZEND_ARG_INFO(0, link_identifier)
3172 	ZEND_ARG_INFO(0, dn)
3173 	ZEND_ARG_INFO(0, entry)
3174 ZEND_END_ARG_INFO()
3175 
3176 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_delete, 0, 0, 2)
3177 	ZEND_ARG_INFO(0, link_identifier)
3178 	ZEND_ARG_INFO(0, dn)
3179 ZEND_END_ARG_INFO()
3180 
3181 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_modify, 0, 0, 3)
3182 	ZEND_ARG_INFO(0, link_identifier)
3183 	ZEND_ARG_INFO(0, dn)
3184 	ZEND_ARG_INFO(0, entry)
3185 ZEND_END_ARG_INFO()
3186 
3187 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_modify_batch, 0, 0, 3)
3188 	ZEND_ARG_INFO(0, link_identifier)
3189 	ZEND_ARG_INFO(0, dn)
3190 	ZEND_ARG_ARRAY_INFO(0, modifications_info, 0)
3191 ZEND_END_ARG_INFO()
3192 
3193 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_mod_add, 0, 0, 3)
3194 	ZEND_ARG_INFO(0, link_identifier)
3195 	ZEND_ARG_INFO(0, dn)
3196 	ZEND_ARG_INFO(0, entry)
3197 ZEND_END_ARG_INFO()
3198 
3199 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_mod_replace, 0, 0, 3)
3200 	ZEND_ARG_INFO(0, link_identifier)
3201 	ZEND_ARG_INFO(0, dn)
3202 	ZEND_ARG_INFO(0, entry)
3203 ZEND_END_ARG_INFO()
3204 
3205 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_mod_del, 0, 0, 3)
3206 	ZEND_ARG_INFO(0, link_identifier)
3207 	ZEND_ARG_INFO(0, dn)
3208 	ZEND_ARG_INFO(0, entry)
3209 ZEND_END_ARG_INFO()
3210 
3211 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_err2str, 0, 0, 1)
3212 	ZEND_ARG_INFO(0, errno)
3213 ZEND_END_ARG_INFO()
3214 
3215 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_compare, 0, 0, 4)
3216 	ZEND_ARG_INFO(0, link_identifier)
3217 	ZEND_ARG_INFO(0, dn)
3218 	ZEND_ARG_INFO(0, attribute)
3219 	ZEND_ARG_INFO(0, value)
3220 ZEND_END_ARG_INFO()
3221 
3222 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_sort, 0, 0, 3)
3223 	ZEND_ARG_INFO(0, link)
3224 	ZEND_ARG_INFO(0, result)
3225 	ZEND_ARG_INFO(0, sortfilter)
3226 ZEND_END_ARG_INFO()
3227 
3228 #ifdef LDAP_CONTROL_PAGEDRESULTS
3229 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_control_paged_result, 0, 0, 2)
3230 	ZEND_ARG_INFO(0, link)
3231 	ZEND_ARG_INFO(0, pagesize)
3232 	ZEND_ARG_INFO(0, iscritical)
3233 	ZEND_ARG_INFO(0, cookie)
3234 ZEND_END_ARG_INFO();
3235 
3236 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_control_paged_result_response, 0, 0, 2)
3237 	ZEND_ARG_INFO(0, link)
3238 	ZEND_ARG_INFO(0, result)
3239 	ZEND_ARG_INFO(1, cookie)
3240 	ZEND_ARG_INFO(1, estimated)
3241 ZEND_END_ARG_INFO();
3242 #endif
3243 
3244 #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP
3245 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_rename, 0, 0, 5)
3246 	ZEND_ARG_INFO(0, link_identifier)
3247 	ZEND_ARG_INFO(0, dn)
3248 	ZEND_ARG_INFO(0, newrdn)
3249 	ZEND_ARG_INFO(0, newparent)
3250 	ZEND_ARG_INFO(0, deleteoldrdn)
3251 ZEND_END_ARG_INFO()
3252 
3253 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_get_option, 0, 0, 3)
3254 	ZEND_ARG_INFO(0, link_identifier)
3255 	ZEND_ARG_INFO(0, option)
3256 	ZEND_ARG_INFO(1, retval)
3257 ZEND_END_ARG_INFO()
3258 
3259 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_set_option, 0, 0, 3)
3260 	ZEND_ARG_INFO(0, link_identifier)
3261 	ZEND_ARG_INFO(0, option)
3262 	ZEND_ARG_INFO(0, newval)
3263 ZEND_END_ARG_INFO()
3264 
3265 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_first_reference, 0, 0, 2)
3266 	ZEND_ARG_INFO(0, link)
3267 	ZEND_ARG_INFO(0, result)
3268 ZEND_END_ARG_INFO()
3269 
3270 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_next_reference, 0, 0, 2)
3271 	ZEND_ARG_INFO(0, link)
3272 	ZEND_ARG_INFO(0, entry)
3273 ZEND_END_ARG_INFO()
3274 
3275 #ifdef HAVE_LDAP_PARSE_REFERENCE
3276 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_parse_reference, 0, 0, 3)
3277 	ZEND_ARG_INFO(0, link)
3278 	ZEND_ARG_INFO(0, entry)
3279 	ZEND_ARG_INFO(1, referrals)
3280 ZEND_END_ARG_INFO()
3281 #endif
3282 
3283 
3284 #ifdef HAVE_LDAP_PARSE_RESULT
3285 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_parse_result, 0, 0, 3)
3286 	ZEND_ARG_INFO(0, link)
3287 	ZEND_ARG_INFO(0, result)
3288 	ZEND_ARG_INFO(1, errcode)
3289 	ZEND_ARG_INFO(1, matcheddn)
3290 	ZEND_ARG_INFO(1, errmsg)
3291 	ZEND_ARG_INFO(1, referrals)
3292 ZEND_END_ARG_INFO()
3293 #endif
3294 #endif
3295 
3296 #if defined(LDAP_API_FEATURE_X_OPENLDAP) && defined(HAVE_3ARG_SETREBINDPROC)
3297 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_set_rebind_proc, 0, 0, 2)
3298 	ZEND_ARG_INFO(0, link)
3299 	ZEND_ARG_INFO(0, callback)
3300 ZEND_END_ARG_INFO()
3301 #endif
3302 
3303 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_escape, 0, 0, 1)
3304 	ZEND_ARG_INFO(0, value)
3305 	ZEND_ARG_INFO(0, ignore)
3306 	ZEND_ARG_INFO(0, flags)
3307 ZEND_END_ARG_INFO()
3308 
3309 #ifdef STR_TRANSLATION
3310 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_t61_to_8859, 0, 0, 1)
3311 	ZEND_ARG_INFO(0, value)
3312 ZEND_END_ARG_INFO()
3313 
3314 ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_8859_to_t61, 0, 0, 1)
3315 	ZEND_ARG_INFO(0, value)
3316 ZEND_END_ARG_INFO()
3317 #endif
3318 /* }}} */
3319 
3320 /*
3321 	This is just a small subset of the functionality provided by the LDAP library. All the
3322 	operations are synchronous. Referrals are not handled automatically.
3323 */
3324 /* {{{ ldap_functions[]
3325  */
3326 const zend_function_entry ldap_functions[] = {
3327 	PHP_FE(ldap_connect,								arginfo_ldap_connect)
3328 	PHP_FALIAS(ldap_close,		ldap_unbind,			arginfo_ldap_resource)
3329 	PHP_FE(ldap_bind,									arginfo_ldap_bind)
3330 #ifdef HAVE_LDAP_SASL
3331 	PHP_FE(ldap_sasl_bind,								arginfo_ldap_sasl_bind)
3332 #endif
3333 	PHP_FE(ldap_unbind,									arginfo_ldap_resource)
3334 	PHP_FE(ldap_read,									arginfo_ldap_read)
3335 	PHP_FE(ldap_list,									arginfo_ldap_list)
3336 	PHP_FE(ldap_search,									arginfo_ldap_search)
3337 	PHP_FE(ldap_free_result,							arginfo_ldap_resource)
3338 	PHP_FE(ldap_count_entries,							arginfo_ldap_count_entries)
3339 	PHP_FE(ldap_first_entry,							arginfo_ldap_first_entry)
3340 	PHP_FE(ldap_next_entry,								arginfo_ldap_next_entry)
3341 	PHP_FE(ldap_get_entries,							arginfo_ldap_get_entries)
3342 	PHP_FE(ldap_first_attribute,						arginfo_ldap_first_attribute)
3343 	PHP_FE(ldap_next_attribute,							arginfo_ldap_next_attribute)
3344 	PHP_FE(ldap_get_attributes,							arginfo_ldap_get_attributes)
3345 	PHP_FALIAS(ldap_get_values,	ldap_get_values_len,	arginfo_ldap_get_values)
3346 	PHP_FE(ldap_get_values_len,							arginfo_ldap_get_values_len)
3347 	PHP_FE(ldap_get_dn,									arginfo_ldap_get_dn)
3348 	PHP_FE(ldap_explode_dn,								arginfo_ldap_explode_dn)
3349 	PHP_FE(ldap_dn2ufn,									arginfo_ldap_dn2ufn)
3350 	PHP_FE(ldap_add,									arginfo_ldap_add)
3351 	PHP_FE(ldap_delete,									arginfo_ldap_delete)
3352 	PHP_FE(ldap_modify_batch,							arginfo_ldap_modify_batch)
3353 	PHP_FALIAS(ldap_modify,		ldap_mod_replace,		arginfo_ldap_modify)
3354 
3355 /* additional functions for attribute based modifications, Gerrit Thomson */
3356 	PHP_FE(ldap_mod_add,								arginfo_ldap_mod_add)
3357 	PHP_FE(ldap_mod_replace,							arginfo_ldap_mod_replace)
3358 	PHP_FE(ldap_mod_del,								arginfo_ldap_mod_del)
3359 /* end gjt mod */
3360 
3361 	PHP_FE(ldap_errno,									arginfo_ldap_resource)
3362 	PHP_FE(ldap_err2str,								arginfo_ldap_err2str)
3363 	PHP_FE(ldap_error,									arginfo_ldap_resource)
3364 	PHP_FE(ldap_compare,								arginfo_ldap_compare)
3365 	PHP_DEP_FE(ldap_sort,									arginfo_ldap_sort)
3366 
3367 #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP
3368 	PHP_FE(ldap_rename,									arginfo_ldap_rename)
3369 	PHP_FE(ldap_get_option,								arginfo_ldap_get_option)
3370 	PHP_FE(ldap_set_option,								arginfo_ldap_set_option)
3371 	PHP_FE(ldap_first_reference,						arginfo_ldap_first_reference)
3372 	PHP_FE(ldap_next_reference,							arginfo_ldap_next_reference)
3373 #ifdef HAVE_LDAP_PARSE_REFERENCE
3374 	PHP_FE(ldap_parse_reference,						arginfo_ldap_parse_reference)
3375 #endif
3376 #ifdef HAVE_LDAP_PARSE_RESULT
3377 	PHP_FE(ldap_parse_result,							arginfo_ldap_parse_result)
3378 #endif
3379 #ifdef HAVE_LDAP_START_TLS_S
3380 	PHP_FE(ldap_start_tls,								arginfo_ldap_resource)
3381 #endif
3382 #endif
3383 
3384 #if defined(LDAP_API_FEATURE_X_OPENLDAP) && defined(HAVE_3ARG_SETREBINDPROC)
3385 	PHP_FE(ldap_set_rebind_proc,						arginfo_ldap_set_rebind_proc)
3386 #endif
3387 
3388 	PHP_FE(ldap_escape,									arginfo_ldap_escape)
3389 
3390 #ifdef STR_TRANSLATION
3391 	PHP_FE(ldap_t61_to_8859,							arginfo_ldap_t61_to_8859)
3392 	PHP_FE(ldap_8859_to_t61,							arginfo_ldap_8859_to_t61)
3393 #endif
3394 
3395 #ifdef LDAP_CONTROL_PAGEDRESULTS
3396 	PHP_FE(ldap_control_paged_result,							arginfo_ldap_control_paged_result)
3397 	PHP_FE(ldap_control_paged_result_response,		arginfo_ldap_control_paged_result_response)
3398 #endif
3399 	PHP_FE_END
3400 };
3401 /* }}} */
3402 
3403 zend_module_entry ldap_module_entry = { /* {{{ */
3404 	STANDARD_MODULE_HEADER,
3405 	"ldap",
3406 	ldap_functions,
3407 	PHP_MINIT(ldap),
3408 	PHP_MSHUTDOWN(ldap),
3409 	NULL,
3410 	NULL,
3411 	PHP_MINFO(ldap),
3412 	PHP_LDAP_VERSION,
3413 	PHP_MODULE_GLOBALS(ldap),
3414 	PHP_GINIT(ldap),
3415 	NULL,
3416 	NULL,
3417 	STANDARD_MODULE_PROPERTIES_EX
3418 };
3419 /* }}} */
3420 
3421 /*
3422  * Local variables:
3423  * tab-width: 4
3424  * c-basic-offset: 4
3425  * End:
3426  * vim600: sw=4 ts=4 fdm=marker
3427  * vim<600: sw=4 ts=4
3428  */
3429