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: 1036 1247 83.1 %
Date: 2016-09-27 Functions: 60 63 95.2 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Tue, 27 Sep 2016 10:25:59 +0000 (3 days ago)

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