Skip to content

Commit dca335b

Browse files
committed
optimize new TLS context:
- first connection - after TLS options change
1 parent 665e79c commit dca335b

File tree

2 files changed

+48
-25
lines changed

2 files changed

+48
-25
lines changed

ext/ldap/ldap.c

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,21 @@ static PHP_GINIT_FUNCTION(ldap)
835835
}
836836
/* }}} */
837837

838+
/* {{{ PHP_RINIT_FUNCTION */
839+
static PHP_RINIT_FUNCTION(ldap)
840+
{
841+
#if defined(COMPILE_DL_LDAP) && defined(ZTS)
842+
ZEND_TSRMLS_CACHE_UPDATE();
843+
#endif
844+
845+
/* needed before first connect and after TLS option changes */
846+
LDAPG(newctx) = true;
847+
848+
return SUCCESS;
849+
}
850+
/* }}} */
851+
852+
838853
/* {{{ PHP_MINIT_FUNCTION */
839854
PHP_MINIT_FUNCTION(ldap)
840855
{
@@ -988,7 +1003,7 @@ PHP_FUNCTION(ldap_connect)
9881003
}
9891004

9901005
#ifdef LDAP_OPT_X_TLS_NEWCTX
991-
if (url && !strncmp(url, "ldaps:", 6)) {
1006+
if (LDAPG(newctx) && url && !strncmp(url, "ldaps:", 6)) {
9921007
int val = 0;
9931008

9941009
/* ensure all pending TLS options are applied in a new context */
@@ -997,6 +1012,7 @@ PHP_FUNCTION(ldap_connect)
9971012
php_error_docref(NULL, E_WARNING, "Could not create new security context");
9981013
RETURN_FALSE;
9991014
}
1015+
LDAPG(newctx) = false;
10001016
}
10011017
#endif
10021018

@@ -3185,15 +3201,7 @@ PHP_FUNCTION(ldap_set_option)
31853201
}
31863202

31873203
switch (option) {
3188-
/* options with int value */
3189-
case LDAP_OPT_DEREF:
3190-
case LDAP_OPT_SIZELIMIT:
3191-
case LDAP_OPT_TIMELIMIT:
3192-
case LDAP_OPT_PROTOCOL_VERSION:
3193-
case LDAP_OPT_ERROR_NUMBER:
3194-
#ifdef LDAP_OPT_DEBUG_LEVEL
3195-
case LDAP_OPT_DEBUG_LEVEL:
3196-
#endif
3204+
/* TLS options with int value */
31973205
#ifdef LDAP_OPT_X_TLS_REQUIRE_CERT
31983206
case LDAP_OPT_X_TLS_REQUIRE_CERT:
31993207
#endif
@@ -3202,6 +3210,17 @@ PHP_FUNCTION(ldap_set_option)
32023210
#endif
32033211
#ifdef LDAP_OPT_X_TLS_PROTOCOL_MIN
32043212
case LDAP_OPT_X_TLS_PROTOCOL_MIN:
3213+
#endif
3214+
LDAPG(newctx) = true;
3215+
ZEND_FALLTHROUGH;
3216+
/* other options with int value */
3217+
case LDAP_OPT_DEREF:
3218+
case LDAP_OPT_SIZELIMIT:
3219+
case LDAP_OPT_TIMELIMIT:
3220+
case LDAP_OPT_PROTOCOL_VERSION:
3221+
case LDAP_OPT_ERROR_NUMBER:
3222+
#ifdef LDAP_OPT_DEBUG_LEVEL
3223+
case LDAP_OPT_DEBUG_LEVEL:
32053224
#endif
32063225
#ifdef LDAP_OPT_X_KEEPALIVE_IDLE
32073226
case LDAP_OPT_X_KEEPALIVE_IDLE:
@@ -3258,17 +3277,7 @@ PHP_FUNCTION(ldap_set_option)
32583277
}
32593278
} break;
32603279
#endif
3261-
/* options with string value */
3262-
case LDAP_OPT_ERROR_STRING:
3263-
#ifdef LDAP_OPT_HOST_NAME
3264-
case LDAP_OPT_HOST_NAME:
3265-
#endif
3266-
#ifdef HAVE_LDAP_SASL
3267-
case LDAP_OPT_X_SASL_MECH:
3268-
case LDAP_OPT_X_SASL_REALM:
3269-
case LDAP_OPT_X_SASL_AUTHCID:
3270-
case LDAP_OPT_X_SASL_AUTHZID:
3271-
#endif
3280+
/* TLS options with string value */
32723281
#if (LDAP_API_VERSION > 2000)
32733282
case LDAP_OPT_X_TLS_CACERTDIR:
32743283
case LDAP_OPT_X_TLS_CACERTFILE:
@@ -3282,6 +3291,19 @@ PHP_FUNCTION(ldap_set_option)
32823291
#endif
32833292
#ifdef LDAP_OPT_X_TLS_DHFILE
32843293
case LDAP_OPT_X_TLS_DHFILE:
3294+
#endif
3295+
LDAPG(newctx) = true;
3296+
ZEND_FALLTHROUGH;
3297+
/* other options with string value */
3298+
case LDAP_OPT_ERROR_STRING:
3299+
#ifdef LDAP_OPT_HOST_NAME
3300+
case LDAP_OPT_HOST_NAME:
3301+
#endif
3302+
#ifdef HAVE_LDAP_SASL
3303+
case LDAP_OPT_X_SASL_MECH:
3304+
case LDAP_OPT_X_SASL_REALM:
3305+
case LDAP_OPT_X_SASL_AUTHCID:
3306+
case LDAP_OPT_X_SASL_AUTHZID:
32853307
#endif
32863308
#ifdef LDAP_OPT_MATCHED_DN
32873309
case LDAP_OPT_MATCHED_DN:
@@ -3714,15 +3736,15 @@ PHP_FUNCTION(ldap_start_tls)
37143736

37153737
if (((rc = ldap_set_option(ld->link, LDAP_OPT_PROTOCOL_VERSION, &protocol)) != LDAP_SUCCESS) ||
37163738
#ifdef LDAP_OPT_X_TLS_NEWCTX
3717-
((rc = ldap_set_option(ld->link, LDAP_OPT_X_TLS_NEWCTX, &val)) != LDAP_OPT_SUCCESS) ||
3739+
(LDAPG(newctx) && (rc = ldap_set_option(ld->link, LDAP_OPT_X_TLS_NEWCTX, &val)) != LDAP_OPT_SUCCESS) ||
37183740
#endif
37193741
((rc = ldap_start_tls_s(ld->link, NULL, NULL)) != LDAP_SUCCESS)
37203742
) {
37213743
php_error_docref(NULL, E_WARNING,"Unable to start TLS: %s", ldap_err2string(rc));
37223744
RETURN_FALSE;
3723-
} else {
3724-
RETURN_TRUE;
37253745
}
3746+
LDAPG(newctx) = false;
3747+
RETURN_TRUE;
37263748
}
37273749
/* }}} */
37283750
#endif
@@ -4237,7 +4259,7 @@ zend_module_entry ldap_module_entry = { /* {{{ */
42374259
ext_functions,
42384260
PHP_MINIT(ldap),
42394261
PHP_MSHUTDOWN(ldap),
4240-
NULL,
4262+
PHP_RINIT(ldap),
42414263
NULL,
42424264
PHP_MINFO(ldap),
42434265
PHP_LDAP_VERSION,

ext/ldap/php_ldap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ PHP_MINFO_FUNCTION(ldap);
3939
ZEND_BEGIN_MODULE_GLOBALS(ldap)
4040
zend_long num_links;
4141
zend_long max_links;
42+
bool newctx; /* create new TLS context before connect */
4243
ZEND_END_MODULE_GLOBALS(ldap)
4344

4445
#if defined(ZTS) && defined(COMPILE_DL_LDAP)

0 commit comments

Comments
 (0)