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

Generated by: LCOV version 1.10

Generated at Tue, 26 Jul 2016 17:07:37 +0000 (27 hours ago)

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