@@ -164,15 +164,17 @@ static void _php_ldap_control_to_array(LDAP *ld, LDAPControl* ctrl, zval* array,
164
164
}
165
165
} else if (strcmp (ctrl -> ldctl_oid , LDAP_CONTROL_PAGEDRESULTS ) == 0 ) {
166
166
int lestimated , rc ;
167
- struct berval lcookie ;
167
+ struct berval lcookie = { 0L , NULL } ;
168
168
zval value ;
169
169
170
170
if (ctrl -> ldctl_value .bv_len ) {
171
+ /* ldap_parse_pageresponse_control() allocates lcookie.bv_val */
171
172
rc = ldap_parse_pageresponse_control (ld , ctrl , & lestimated , & lcookie );
172
173
} else {
173
174
/* ldap_parse_pageresponse_control will crash if value is empty */
174
175
rc = -1 ;
175
176
}
177
+
176
178
if ( rc == LDAP_SUCCESS ) {
177
179
array_init (& value );
178
180
add_assoc_long (& value , "size" , lestimated );
@@ -181,6 +183,10 @@ static void _php_ldap_control_to_array(LDAP *ld, LDAPControl* ctrl, zval* array,
181
183
} else {
182
184
add_assoc_null (array , "value" );
183
185
}
186
+
187
+ if (lcookie .bv_val ) {
188
+ ldap_memfree (lcookie .bv_val );
189
+ }
184
190
} else if ((strcmp (ctrl -> ldctl_oid , LDAP_CONTROL_PRE_READ ) == 0 ) || (strcmp (ctrl -> ldctl_oid , LDAP_CONTROL_POST_READ ) == 0 )) {
185
191
BerElement * ber ;
186
192
struct berval bv ;
@@ -299,27 +305,23 @@ static int _php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, zval* arra
299
305
control_iscritical = 0 ;
300
306
}
301
307
302
- struct berval * control_value = NULL ;
308
+ BerElement * ber = NULL ;
309
+ struct berval control_value = { 0L , NULL };
310
+ int control_value_alloc = 0 ;
303
311
304
312
if ((val = zend_hash_str_find (Z_ARRVAL_P (array ), "value" , sizeof ("value" ) - 1 )) != NULL ) {
305
313
if (Z_TYPE_P (val ) != IS_ARRAY ) {
306
- control_value = ber_memalloc ( sizeof * control_value );
307
- if (control_value == NULL ) {
314
+ tmpstring = zval_get_string ( val );
315
+ if (EG ( exception ) ) {
308
316
rc = -1 ;
309
- php_error_docref (NULL , E_WARNING , "Failed to allocate control value" );
310
- } else {
311
- tmpstring = zval_get_string (val );
312
- if (EG (exception )) {
313
- rc = -1 ;
314
- goto failure ;
315
- }
316
- control_value -> bv_val = ZSTR_VAL (tmpstring );
317
- control_value -> bv_len = ZSTR_LEN (tmpstring );
317
+ goto failure ;
318
318
}
319
+ control_value .bv_val = ZSTR_VAL (tmpstring );
320
+ control_value .bv_len = ZSTR_LEN (tmpstring );
319
321
} else if (strcmp (ZSTR_VAL (control_oid ), LDAP_CONTROL_PAGEDRESULTS ) == 0 ) {
320
322
zval * tmp ;
321
323
int pagesize = 1 ;
322
- struct berval cookie = { 0 , NULL };
324
+ struct berval cookie = { 0L , NULL };
323
325
if ((tmp = zend_hash_str_find (Z_ARRVAL_P (val ), "size" , sizeof ("size" ) - 1 )) != NULL ) {
324
326
pagesize = zval_get_long (tmp );
325
327
}
@@ -332,15 +334,11 @@ static int _php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, zval* arra
332
334
cookie .bv_val = ZSTR_VAL (tmpstring );
333
335
cookie .bv_len = ZSTR_LEN (tmpstring );
334
336
}
335
- control_value = ber_memalloc (sizeof * control_value );
336
- if (control_value == NULL ) {
337
- rc = -1 ;
338
- php_error_docref (NULL , E_WARNING , "Failed to allocate control value" );
339
- } else {
340
- rc = ldap_create_page_control_value (ld , pagesize , & cookie , control_value );
341
- if (rc != LDAP_SUCCESS ) {
342
- php_error_docref (NULL , E_WARNING , "Failed to create paged result control value: %s (%d)" , ldap_err2string (rc ), rc );
343
- }
337
+ /* ldap_create_page_control_value() allocates memory for control_value.bv_val */
338
+ control_value_alloc = 1 ;
339
+ rc = ldap_create_page_control_value (ld , pagesize , & cookie , & control_value );
340
+ if (rc != LDAP_SUCCESS ) {
341
+ php_error_docref (NULL , E_WARNING , "Failed to create paged result control value: %s (%d)" , ldap_err2string (rc ), rc );
344
342
}
345
343
} else if (strcmp (ZSTR_VAL (control_oid ), LDAP_CONTROL_ASSERT ) == 0 ) {
346
344
zval * tmp ;
@@ -354,19 +352,15 @@ static int _php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, zval* arra
354
352
rc = -1 ;
355
353
goto failure ;
356
354
}
357
- control_value = ber_memalloc (sizeof * control_value );
358
- if (control_value == NULL ) {
359
- rc = -1 ;
360
- php_error_docref (NULL , E_WARNING , "Failed to allocate control value" );
361
- } else {
362
- /* ldap_create_assertion_control_value does not reset ld_errno, we need to do it ourselves
363
- See http://www.openldap.org/its/index.cgi/Incoming?id=8674 */
364
- int success = LDAP_SUCCESS ;
365
- ldap_set_option (ld , LDAP_OPT_RESULT_CODE , & success );
366
- rc = ldap_create_assertion_control_value (ld , ZSTR_VAL (assert ), control_value );
367
- if (rc != LDAP_SUCCESS ) {
368
- php_error_docref (NULL , E_WARNING , "Failed to create assert control value: %s (%d)" , ldap_err2string (rc ), rc );
369
- }
355
+ /* ldap_create_assertion_control_value does not reset ld_errno, we need to do it ourselves
356
+ See http://www.openldap.org/its/index.cgi/Incoming?id=8674 */
357
+ int success = LDAP_SUCCESS ;
358
+ ldap_set_option (ld , LDAP_OPT_RESULT_CODE , & success );
359
+ /* ldap_create_assertion_control_value() allocates memory for control_value.bv_val */
360
+ control_value_alloc = 1 ;
361
+ rc = ldap_create_assertion_control_value (ld , ZSTR_VAL (assert ), & control_value );
362
+ if (rc != LDAP_SUCCESS ) {
363
+ php_error_docref (NULL , E_WARNING , "Failed to create assert control value: %s (%d)" , ldap_err2string (rc ), rc );
370
364
}
371
365
zend_string_release (assert );
372
366
}
@@ -376,9 +370,8 @@ static int _php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, zval* arra
376
370
rc = -1 ;
377
371
php_error_docref (NULL , E_WARNING , "Filter missing from control value array" );
378
372
} else {
379
- BerElement * vrber = ber_alloc_t (LBER_USE_DER );
380
- control_value = ber_memalloc (sizeof * control_value );
381
- if ((control_value == NULL ) || (vrber == NULL )) {
373
+ ber = ber_alloc_t (LBER_USE_DER );
374
+ if (ber == NULL ) {
382
375
rc = -1 ;
383
376
php_error_docref (NULL , E_WARNING , "Failed to allocate control value" );
384
377
} else {
@@ -387,14 +380,11 @@ static int _php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, zval* arra
387
380
rc = -1 ;
388
381
goto failure ;
389
382
}
390
- if (ldap_put_vrFilter (vrber , ZSTR_VAL (tmpstring )) == -1 ) {
391
- ber_free (vrber , 1 );
383
+ if (ldap_put_vrFilter (ber , ZSTR_VAL (tmpstring )) == -1 ) {
392
384
rc = -1 ;
393
385
php_error_docref (NULL , E_WARNING , "Failed to create control value: Bad ValuesReturnFilter: %s" , ZSTR_VAL (tmpstring ));
394
- } else {
395
- if (ber_flatten2 (vrber , control_value , 0 ) == -1 ) {
396
- rc = -1 ;
397
- }
386
+ } else if (ber_flatten2 (ber , & control_value , control_value_alloc ) == -1 ) {
387
+ rc = -1 ;
398
388
}
399
389
}
400
390
}
@@ -404,10 +394,9 @@ static int _php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, zval* arra
404
394
rc = -1 ;
405
395
php_error_docref (NULL , E_WARNING , "Attributes list missing from control value array" );
406
396
} else {
407
- BerElement * ber = ber_alloc_t (LBER_USE_DER );
397
+ ber = ber_alloc_t (LBER_USE_DER );
408
398
409
- control_value = ber_memalloc (sizeof * control_value );
410
- if ((control_value == NULL ) || (ber == NULL )) {
399
+ if (ber == NULL ) {
411
400
rc = -1 ;
412
401
php_error_docref (NULL , E_WARNING , "Failed to allocate control value" );
413
402
} else {
@@ -443,7 +432,7 @@ static int _php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, zval* arra
443
432
php_error_docref (NULL , E_WARNING , "Failed to encode attribute list" );
444
433
} else {
445
434
int err ;
446
- err = ber_flatten2 (ber , control_value , 0 );
435
+ err = ber_flatten2 (ber , & control_value , control_value_alloc );
447
436
if (err < 0 ) {
448
437
rc = -1 ;
449
438
php_error_docref (NULL , E_WARNING , "Failed to encode control value (%d)" , err );
@@ -502,15 +491,11 @@ static int _php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, zval* arra
502
491
}
503
492
}
504
493
sort_keys [num_keys ] = NULL ;
505
- control_value = ber_memalloc (sizeof * control_value );
506
- if (control_value == NULL ) {
507
- rc = -1 ;
508
- php_error_docref (NULL , E_WARNING , "Failed to allocate control value" );
509
- } else {
510
- rc = ldap_create_sort_control_value (ld , sort_keys , control_value );
511
- if (rc != LDAP_SUCCESS ) {
512
- php_error_docref (NULL , E_WARNING , "Failed to create sort control value: %s (%d)" , ldap_err2string (rc ), rc );
513
- }
494
+ /* ldap_create_sort_control_value() allocates memory for control_value.bv_val */
495
+ control_value_alloc = 1 ;
496
+ rc = ldap_create_sort_control_value (ld , sort_keys , & control_value );
497
+ if (rc != LDAP_SUCCESS ) {
498
+ php_error_docref (NULL , E_WARNING , "Failed to create sort control value: %s (%d)" , ldap_err2string (rc ), rc );
514
499
}
515
500
} else if (strcmp (ZSTR_VAL (control_oid ), LDAP_CONTROL_VLVREQUEST ) == 0 ) {
516
501
zval * tmp ;
@@ -572,15 +557,11 @@ static int _php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, zval* arra
572
557
vlvInfo .ldvlv_context = NULL ;
573
558
}
574
559
575
- control_value = ber_memalloc (sizeof * control_value );
576
- if (control_value == NULL ) {
577
- rc = -1 ;
578
- php_error_docref (NULL , E_WARNING , "Failed to allocate control value" );
579
- } else {
580
- rc = ldap_create_vlv_control_value (ld , & vlvInfo , control_value );
581
- if (rc != LDAP_SUCCESS ) {
582
- php_error_docref (NULL , E_WARNING , "Failed to create VLV control value: %s (%d)" , ldap_err2string (rc ), rc );
583
- }
560
+ /* ldap_create_vlv_control_value() allocates memory for control_value.bv_val */
561
+ control_value_alloc = 1 ;
562
+ rc = ldap_create_vlv_control_value (ld , & vlvInfo , & control_value );
563
+ if (rc != LDAP_SUCCESS ) {
564
+ php_error_docref (NULL , E_WARNING , "Failed to create VLV control value: %s (%d)" , ldap_err2string (rc ), rc );
584
565
}
585
566
} else {
586
567
php_error_docref (NULL , E_WARNING , "Control OID %s does not expect an array as value" , ZSTR_VAL (control_oid ));
@@ -589,7 +570,7 @@ static int _php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, zval* arra
589
570
}
590
571
591
572
if (rc == LDAP_SUCCESS ) {
592
- rc = ldap_control_create (ZSTR_VAL (control_oid ), control_iscritical , control_value , 1 , ctrl );
573
+ rc = ldap_control_create (ZSTR_VAL (control_oid ), control_iscritical , & control_value , 1 , ctrl );
593
574
}
594
575
595
576
failure :
@@ -611,9 +592,11 @@ static int _php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, zval* arra
611
592
}
612
593
efree (tmpstrings2 );
613
594
}
614
- if (control_value != NULL ) {
615
- ber_memfree (control_value );
616
- control_value = NULL ;
595
+ if (control_value .bv_val != NULL && control_value_alloc != 0 ) {
596
+ ber_memfree (control_value .bv_val );
597
+ }
598
+ if (ber != NULL ) {
599
+ ber_free (ber , 1 );
617
600
}
618
601
if (ldap_attrs != NULL ) {
619
602
efree (ldap_attrs );
@@ -1445,7 +1428,7 @@ static void php_ldap_do_search(INTERNAL_FUNCTION_PARAMETERS, int scope)
1445
1428
zend_string * ldap_filter = NULL , * ldap_base_dn = NULL ;
1446
1429
char * * ldap_attrs = NULL ;
1447
1430
ldap_linkdata * ld = NULL ;
1448
- LDAPMessage * ldap_res ;
1431
+ LDAPMessage * ldap_res = NULL ;
1449
1432
LDAPControl * * lserverctrls = NULL ;
1450
1433
int ldap_attrsonly = 0 , ldap_sizelimit = -1 , ldap_timelimit = -1 , ldap_deref = -1 ;
1451
1434
int old_ldap_sizelimit = -1 , old_ldap_timelimit = -1 , old_ldap_deref = -1 ;
@@ -1645,6 +1628,11 @@ static void php_ldap_do_search(INTERNAL_FUNCTION_PARAMETERS, int scope)
1645
1628
&& errno != LDAP_REFERRAL
1646
1629
#endif
1647
1630
) {
1631
+ /* ldap_res should be freed regardless of return value of ldap_search_ext_s()
1632
+ * see: https://linux.die.net/man/3/ldap_search_ext_s */
1633
+ if (ldap_res != NULL ) {
1634
+ ldap_msgfree (ldap_res );
1635
+ }
1648
1636
php_error_docref (NULL , E_WARNING , "Search: %s" , ldap_err2string (errno ));
1649
1637
ret = 0 ;
1650
1638
} else {
@@ -3902,7 +3890,7 @@ PHP_FUNCTION(ldap_control_paged_result)
3902
3890
zval * link ;
3903
3891
char * cookie = NULL ;
3904
3892
size_t cookie_len = 0 ;
3905
- struct berval lcookie = { 0 , NULL };
3893
+ struct berval lcookie = { 0L , NULL };
3906
3894
ldap_linkdata * ld ;
3907
3895
LDAP * ldap ;
3908
3896
BerElement * ber = NULL ;
@@ -4190,17 +4178,15 @@ PHP_FUNCTION(ldap_exop)
4190
4178
PHP_FUNCTION (ldap_exop_passwd )
4191
4179
{
4192
4180
zval * link , * serverctrls ;
4193
- struct berval luser , loldpw , lnewpw , lgenpasswd ;
4194
- LDAPControl * * lserverctrls = NULL , * * requestctrls = NULL ;
4195
- LDAPControl * ctrl , * * ctrlp ;
4196
- LDAPMessage * ldap_res ;
4181
+ struct berval luser = { 0L , NULL };
4182
+ struct berval loldpw = { 0L , NULL };
4183
+ struct berval lnewpw = { 0L , NULL };
4184
+ struct berval lgenpasswd = { 0L , NULL };
4185
+ LDAPControl * ctrl , * * lserverctrls = NULL , * requestctrls [2 ] = { NULL , NULL };
4186
+ LDAPMessage * ldap_res = NULL ;
4197
4187
ldap_linkdata * ld ;
4198
4188
int rc , myargcount = ZEND_NUM_ARGS (), msgid , err ;
4199
- char * errmsg ;
4200
-
4201
- luser .bv_len = 0 ;
4202
- loldpw .bv_len = 0 ;
4203
- lnewpw .bv_len = 0 ;
4189
+ char * errmsg = NULL ;
4204
4190
4205
4191
if (zend_parse_parameters (myargcount , "r|sssz/" , & link , & luser .bv_val , & luser .bv_len , & loldpw .bv_val , & loldpw .bv_len , & lnewpw .bv_val , & lnewpw .bv_len , & serverctrls ) == FAILURE ) {
4206
4192
RETURN_THROWS ();
@@ -4212,16 +4198,10 @@ PHP_FUNCTION(ldap_exop_passwd)
4212
4198
4213
4199
switch (myargcount ) {
4214
4200
case 5 :
4215
- requestctrls = safe_emalloc (2 , sizeof (* requestctrls ), 0 );
4216
- * requestctrls = NULL ;
4217
- ctrlp = requestctrls ;
4218
-
4201
+ /* ldap_create_passwordpolicy_control() allocates ctrl */
4219
4202
if (ldap_create_passwordpolicy_control (ld -> link , & ctrl ) == LDAP_SUCCESS ) {
4220
- * ctrlp = ctrl ;
4221
- ++ ctrlp ;
4203
+ requestctrls [0 ] = ctrl ;
4222
4204
}
4223
-
4224
- * ctrlp = NULL ;
4225
4205
}
4226
4206
4227
4207
/* asynchronous call to get result and controls */
@@ -4231,35 +4211,43 @@ PHP_FUNCTION(ldap_exop_passwd)
4231
4211
requestctrls ,
4232
4212
NULL , & msgid );
4233
4213
4234
- if (requestctrls != NULL ) {
4235
- efree (requestctrls );
4214
+ if (requestctrls [ 0 ] != NULL ) {
4215
+ ldap_control_free (requestctrls [ 0 ] );
4236
4216
}
4237
4217
4238
4218
if (rc != LDAP_SUCCESS ) {
4239
4219
php_error_docref (NULL , E_WARNING , "Passwd modify extended operation failed: %s (%d)" , ldap_err2string (rc ), rc );
4240
- RETURN_FALSE ;
4220
+ RETVAL_FALSE ;
4221
+ goto cleanup ;
4241
4222
}
4242
4223
4243
4224
rc = ldap_result (ld -> link , msgid , 1 /* LDAP_MSG_ALL */ , NULL , & ldap_res );
4244
4225
if ((rc < 0 ) || !ldap_res ) {
4245
4226
rc = _get_lderrno (ld -> link );
4246
4227
php_error_docref (NULL , E_WARNING , "Passwd modify extended operation failed: %s (%d)" , ldap_err2string (rc ), rc );
4247
- RETURN_FALSE ;
4228
+ RETVAL_FALSE ;
4229
+ goto cleanup ;
4248
4230
}
4249
4231
4250
4232
rc = ldap_parse_passwd (ld -> link , ldap_res , & lgenpasswd );
4251
4233
if ( rc != LDAP_SUCCESS ) {
4252
4234
php_error_docref (NULL , E_WARNING , "Passwd modify extended operation failed: %s (%d)" , ldap_err2string (rc ), rc );
4253
- ldap_msgfree ( ldap_res ) ;
4254
- RETURN_FALSE ;
4235
+ RETVAL_FALSE ;
4236
+ goto cleanup ;
4255
4237
}
4256
4238
4257
- rc = ldap_parse_result (ld -> link , ldap_res , & err , NULL , & errmsg , NULL , (myargcount > 4 ? & lserverctrls : NULL ), 1 );
4239
+ rc = ldap_parse_result (ld -> link , ldap_res , & err , NULL , & errmsg , NULL , (myargcount > 4 ? & lserverctrls : NULL ), 0 );
4258
4240
if ( rc != LDAP_SUCCESS ) {
4259
4241
php_error_docref (NULL , E_WARNING , "Passwd modify extended operation failed: %s (%d)" , ldap_err2string (rc ), rc );
4260
- RETURN_FALSE ;
4242
+ RETVAL_FALSE ;
4243
+ goto cleanup ;
4244
+ }
4245
+
4246
+ if (myargcount > 4 ) {
4247
+ _php_ldap_controls_to_array (ld -> link , lserverctrls , serverctrls , 0 );
4261
4248
}
4262
4249
4250
+ /* return */
4263
4251
if (lnewpw .bv_len == 0 ) {
4264
4252
if (lgenpasswd .bv_len == 0 ) {
4265
4253
RETVAL_EMPTY_STRING ();
@@ -4273,11 +4261,16 @@ PHP_FUNCTION(ldap_exop_passwd)
4273
4261
RETVAL_FALSE ;
4274
4262
}
4275
4263
4276
- if (myargcount > 4 ) {
4277
- _php_ldap_controls_to_array (ld -> link , lserverctrls , serverctrls , 0 );
4264
+ cleanup :
4265
+ if (lgenpasswd .bv_val != NULL ) {
4266
+ ldap_memfree (lgenpasswd .bv_val );
4267
+ }
4268
+ if (ldap_res != NULL ) {
4269
+ ldap_msgfree (ldap_res );
4270
+ }
4271
+ if (errmsg != NULL ) {
4272
+ ldap_memfree (errmsg );
4278
4273
}
4279
-
4280
- ldap_memfree (lgenpasswd .bv_val );
4281
4274
}
4282
4275
/* }}} */
4283
4276
#endif
0 commit comments