PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/ldap - ldap.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 807 1190 67.8 %
Date: 2015-07-26 Functions: 59 63 93.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Mon, 27 Jul 2015 02:32:20 +0000 (2 days ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.