forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldap.c
2133 lines (1813 loc) · 59 KB
/
ldap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| PHP Version 4 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2003 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 2.02 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.php.net/license/2_02.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Amitay Isaacs <[email protected]> |
| Eric Warnke <[email protected]> |
| Rasmus Lerdorf <[email protected]> |
| Gerrit Thomson <[email protected]> |
| Jani Taskinen <[email protected]> |
| Stig Venaas <[email protected]> |
| PHP 4.0 updates: Zeev Suraski <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#define IS_EXT_MODULE
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
/* Additional headers for NetWare */
#if defined(NETWARE) && (NEW_LIBC)
#include <sys/select.h>
#include <sys/timeval.h>
#endif
#include "php.h"
#include "php_ini.h"
#include <stddef.h>
#include "ext/standard/dl.h"
#include "php_ldap.h"
#ifdef PHP_WIN32
#include <string.h>
#if HAVE_NSLDAP
#include <winsock.h>
#endif
#define strdup _strdup
#undef WINDOWS
#undef strcasecmp
#undef strncasecmp
#define WINSOCK 1
#define __STDC__ 1
#endif
#include "ext/standard/php_string.h"
#include "ext/standard/info.h"
typedef struct {
LDAP *link;
#if defined(LDAP_API_FEATURE_X_OPENLDAP) && defined(HAVE_3ARG_SETREBINDPROC)
zval *rebindproc;
#endif
} ldap_linkdata;
typedef struct {
LDAPMessage *data;
int id;
} ldap_resultentry;
ZEND_DECLARE_MODULE_GLOBALS(ldap)
static unsigned char third_argument_force_ref[] = { 3, BYREF_NONE, BYREF_NONE, BYREF_FORCE };
static unsigned char arg3to6of6_force_ref[] = { 6, BYREF_NONE, BYREF_NONE, BYREF_FORCE, BYREF_FORCE, BYREF_FORCE, BYREF_FORCE };
static int le_link, le_result, le_result_entry, le_ber_entry;
/*
This is just a small subset of the functionality provided by the LDAP library. All the
operations are synchronous. Referrals are not handled automatically.
*/
/* {{{ ldap_functions[]
*/
function_entry ldap_functions[] = {
PHP_FE(ldap_connect, NULL)
PHP_FALIAS(ldap_close, ldap_unbind, NULL)
PHP_FE(ldap_bind, NULL)
PHP_FE(ldap_unbind, NULL)
PHP_FE(ldap_read, NULL)
PHP_FE(ldap_list, NULL)
PHP_FE(ldap_search, NULL)
PHP_FE(ldap_free_result, NULL)
PHP_FE(ldap_count_entries, NULL)
PHP_FE(ldap_first_entry, NULL)
PHP_FE(ldap_next_entry, NULL)
PHP_FE(ldap_get_entries, NULL)
PHP_FE(ldap_first_attribute, third_argument_force_ref)
PHP_FE(ldap_next_attribute, third_argument_force_ref)
PHP_FE(ldap_get_attributes, NULL)
PHP_FE(ldap_get_values, NULL)
PHP_FE(ldap_get_values_len, NULL)
PHP_FE(ldap_get_dn, NULL)
PHP_FE(ldap_explode_dn, NULL)
PHP_FE(ldap_dn2ufn, NULL)
PHP_FE(ldap_add, NULL)
PHP_FE(ldap_delete, NULL)
PHP_FALIAS(ldap_modify, ldap_mod_replace, NULL)
/* additional functions for attribute based modifications, Gerrit Thomson */
PHP_FE(ldap_mod_add, NULL)
PHP_FE(ldap_mod_replace, NULL)
PHP_FE(ldap_mod_del, NULL)
/* end gjt mod */
PHP_FE(ldap_errno, NULL)
PHP_FE(ldap_err2str, NULL)
PHP_FE(ldap_error, NULL)
PHP_FE(ldap_compare, NULL)
PHP_FE(ldap_sort, NULL)
#if ( LDAP_API_VERSION > 2000 ) || HAVE_NSLDAP
PHP_FE(ldap_get_option, third_argument_force_ref)
PHP_FE(ldap_set_option, NULL)
PHP_FE(ldap_parse_result, arg3to6of6_force_ref)
PHP_FE(ldap_first_reference, NULL)
PHP_FE(ldap_next_reference, NULL)
#ifdef HAVE_LDAP_PARSE_REFERENCE
PHP_FE(ldap_parse_reference, third_argument_force_ref)
#endif
PHP_FE(ldap_rename, NULL)
#ifdef HAVE_LDAP_START_TLS_S
PHP_FE(ldap_start_tls, NULL)
#endif
#endif
#if defined(LDAP_API_FEATURE_X_OPENLDAP) && defined(HAVE_3ARG_SETREBINDPROC)
PHP_FE(ldap_set_rebind_proc, NULL)
#endif
#ifdef STR_TRANSLATION
PHP_FE(ldap_t61_to_8859, NULL)
PHP_FE(ldap_8859_to_t61, NULL)
#endif
{NULL, NULL, NULL}
};
/* }}} */
zend_module_entry ldap_module_entry = {
STANDARD_MODULE_HEADER,
"ldap",
ldap_functions,
PHP_MINIT(ldap),
PHP_MSHUTDOWN(ldap),
NULL,
NULL,
PHP_MINFO(ldap),
NO_VERSION_YET,
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_LDAP
ZEND_GET_MODULE(ldap)
#endif
static void _close_ldap_link(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
ldap_linkdata *ld = (ldap_linkdata *)rsrc->ptr;
ldap_unbind_s(ld->link);
#if defined(LDAP_API_FEATURE_X_OPENLDAP) && defined(HAVE_3ARG_SETREBINDPROC)
if (ld->rebindproc != NULL) {
zval_dtor(ld->rebindproc);
FREE_ZVAL(ld->rebindproc);
}
#endif
efree(ld);
LDAPG(num_links)--;
}
static void _free_ldap_result(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
LDAPMessage *result = (LDAPMessage *)rsrc->ptr;
ldap_msgfree(result);
}
static void _free_ldap_result_entry(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
ldap_resultentry *entry = (ldap_resultentry *)rsrc->ptr;
zend_list_delete(entry->id);
efree(entry);
}
/* {{{ PHP_INI_BEGIN
*/
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY_EX("ldap.max_links", "-1", PHP_INI_SYSTEM, OnUpdateInt, max_links, zend_ldap_globals, ldap_globals, display_link_numbers)
PHP_INI_END()
/* }}} */
/* {{{ php_ldap_init_globals
*/
static void php_ldap_init_globals(zend_ldap_globals *ldap_globals)
{
ldap_globals->num_links = 0;
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(ldap)
{
ZEND_INIT_MODULE_GLOBALS(ldap, php_ldap_init_globals, NULL);
REGISTER_INI_ENTRIES();
/* Constants to be used with deref-parameter in php_ldap_do_search() */
REGISTER_LONG_CONSTANT("LDAP_DEREF_NEVER", LDAP_DEREF_NEVER, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("LDAP_DEREF_SEARCHING", LDAP_DEREF_SEARCHING, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("LDAP_DEREF_FINDING", LDAP_DEREF_FINDING, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("LDAP_DEREF_ALWAYS", LDAP_DEREF_ALWAYS, CONST_PERSISTENT | CONST_CS);
#if ( LDAP_API_VERSION > 2000 ) || HAVE_NSLDAP
/* LDAP options */
REGISTER_LONG_CONSTANT("LDAP_OPT_DEREF", LDAP_OPT_DEREF, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("LDAP_OPT_SIZELIMIT", LDAP_OPT_SIZELIMIT, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("LDAP_OPT_TIMELIMIT", LDAP_OPT_TIMELIMIT, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("LDAP_OPT_PROTOCOL_VERSION", LDAP_OPT_PROTOCOL_VERSION, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("LDAP_OPT_ERROR_NUMBER", LDAP_OPT_ERROR_NUMBER, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("LDAP_OPT_REFERRALS", LDAP_OPT_REFERRALS, CONST_PERSISTENT | CONST_CS);
#ifdef LDAP_OPT_RESTART
REGISTER_LONG_CONSTANT("LDAP_OPT_RESTART", LDAP_OPT_RESTART, CONST_PERSISTENT | CONST_CS);
#endif
REGISTER_LONG_CONSTANT("LDAP_OPT_HOST_NAME", LDAP_OPT_HOST_NAME, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("LDAP_OPT_ERROR_STRING", LDAP_OPT_ERROR_STRING, CONST_PERSISTENT | CONST_CS);
#ifdef LDAP_OPT_MATCHED_DN
REGISTER_LONG_CONSTANT("LDAP_OPT_MATCHED_DN", LDAP_OPT_MATCHED_DN, CONST_PERSISTENT | CONST_CS);
#endif
REGISTER_LONG_CONSTANT("LDAP_OPT_SERVER_CONTROLS", LDAP_OPT_SERVER_CONTROLS, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("LDAP_OPT_CLIENT_CONTROLS", LDAP_OPT_CLIENT_CONTROLS, CONST_PERSISTENT | CONST_CS);
#endif
#ifdef LDAP_OPT_DEBUG_LEVEL
REGISTER_LONG_CONSTANT("LDAP_OPT_DEBUG_LEVEL", LDAP_OPT_DEBUG_LEVEL, CONST_PERSISTENT | CONST_CS);
#endif
#ifdef ORALDAP
REGISTER_LONG_CONSTANT("GSLC_SSL_NO_AUTH", GSLC_SSL_NO_AUTH, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("GSLC_SSL_ONEWAY_AUTH", GSLC_SSL_ONEWAY_AUTH, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("GSLC_SSL_TWOWAY_AUTH", GSLC_SSL_TWOWAY_AUTH, CONST_PERSISTENT | CONST_CS);
#endif
le_result = zend_register_list_destructors_ex(_free_ldap_result, NULL, "ldap result", module_number);
le_link = zend_register_list_destructors_ex(_close_ldap_link, NULL, "ldap link", module_number);
le_result_entry = zend_register_list_destructors_ex(_free_ldap_result_entry, NULL, "ldap result entry", module_number);
le_ber_entry = zend_register_list_destructors_ex(NULL, NULL, "ldap ber entry", module_number);
Z_TYPE(ldap_module_entry) = type;
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(ldap)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(ldap)
{
char tmp[32];
#if HAVE_NSLDAP
LDAPVersion ver;
double SDKVersion;
#endif
php_info_print_table_start();
php_info_print_table_row(2, "LDAP Support", "enabled" );
php_info_print_table_row(2, "RCS Version", "$Id$" );
if (LDAPG(max_links) == -1) {
snprintf(tmp, 31, "%ld/unlimited", LDAPG(num_links));
} else {
snprintf(tmp, 31, "%ld/%ld", LDAPG(num_links), LDAPG(max_links));
}
tmp[31] = '\0';
php_info_print_table_row(2, "Total Links", tmp);
#ifdef LDAP_API_VERSION
snprintf(tmp, 31, "%d", LDAP_API_VERSION);
tmp[31] = '\0';
php_info_print_table_row(2, "API Version", tmp);
#endif
#ifdef LDAP_VENDOR_NAME
php_info_print_table_row(2, "Vendor Name", LDAP_VENDOR_NAME);
#endif
#ifdef LDAP_VENDOR_VERSION
snprintf(tmp, 31, "%d", LDAP_VENDOR_VERSION);
tmp[31] = '\0';
php_info_print_table_row(2, "Vendor Version", tmp);
#endif
#if HAVE_NSLDAP
SDKVersion = ldap_version( &ver );
snprintf(tmp, 31, "%f", SDKVersion/100.0 );
tmp[31] = '\0';
php_info_print_table_row(2, "SDK Version", tmp );
snprintf(tmp, 31, "%f", ver.protocol_version/100.0 );
tmp[31] = '\0';
php_info_print_table_row(2, "Highest LDAP Protocol Supported", tmp );
snprintf(tmp, 31, "%f", ver.SSL_version/100.0 );
tmp[31] = '\0';
php_info_print_table_row(2, "SSL Level Supported", tmp );
if ( ver.security_level != LDAP_SECURITY_NONE ) {
snprintf(tmp, 31, "%d", ver.security_level );
tmp[31] = '\0';
} else {
strcpy(tmp, "SSL not enabled" );
}
php_info_print_table_row(2, "Level of Encryption", tmp );
#endif
php_info_print_table_end();
}
/* }}} */
/* {{{ proto resource ldap_connect([string host [, int port]])
Connect to an LDAP server */
PHP_FUNCTION(ldap_connect)
{
char *host = NULL;
int hostlen;
int port = 389; /* Default port */
#ifdef HAVE_ORALDAP
char *wallet, *walletpasswd;
int walletlen, walletpasswdlen;
int authmode;
int ssl=0;
#endif
ldap_linkdata *ld;
LDAP *ldap;
#ifdef HAVE_ORALDAP
if (ZEND_NUM_ARGS() == 3 || ZEND_NUM_ARGS() == 4) {
WRONG_PARAM_COUNT;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|slssl", &host, &hostlen, &port, &wallet, &walletlen, &walletpasswd, &walletpasswdlen, &authmode) == FAILURE) {
RETURN_FALSE;
}
if (ZEND_NUM_ARGS() == 5 ) {
ssl = 1;
}
#else
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl", &host, &hostlen, &port) == FAILURE) {
RETURN_FALSE;
}
#endif
if (LDAPG(max_links) != -1 && LDAPG(num_links) >= LDAPG(max_links)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Too many open links (%d)", LDAPG(num_links));
RETURN_FALSE;
}
ld = ecalloc(1, sizeof(ldap_linkdata));
if (ld == NULL) {
RETURN_FALSE;
}
#ifdef LDAP_API_FEATURE_X_OPENLDAP
if (host != NULL && strchr(host, '/')) {
int rc;
rc = ldap_initialize(&ldap, host);
if (rc != LDAP_SUCCESS) {
efree(ld);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not create session handle: %s", ldap_err2string(rc));
RETURN_FALSE;
}
} else {
ldap = ldap_init(host, port);
}
#else
ldap = ldap_open(host, port);
#endif
if ( ldap == NULL ) {
efree(ld);
RETURN_FALSE;
} else {
#ifdef HAVE_ORALDAP
if (ssl) {
if (ldap_init_SSL(&ldap->ld_sb, wallet, walletpasswd, authmode)) {
efree(ld);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "SSL init failed");
RETURN_FALSE;
}
}
#endif
LDAPG(num_links)++;
ld->link = ldap;
ZEND_REGISTER_RESOURCE(return_value, ld, le_link);
}
}
/* }}} */
/* {{{ _get_lderrno
*/
static int _get_lderrno(LDAP *ldap)
{
#if !HAVE_NSLDAP
#if LDAP_API_VERSION > 2000
int lderr;
/* New versions of OpenLDAP do it this way */
ldap_get_option(ldap, LDAP_OPT_ERROR_NUMBER, &lderr);
return lderr;
#else
return ldap->ld_errno;
#endif
#else
return ldap_get_lderrno(ldap, NULL, NULL);
#endif
}
/* }}} */
/* {{{ proto bool ldap_bind(resource link [, string dn, string password])
Bind to LDAP directory */
PHP_FUNCTION(ldap_bind)
{
zval *link;
char *ldap_bind_dn = NULL, *ldap_bind_pw = NULL;
int ldap_bind_dnlen, ldap_bind_pwlen;
ldap_linkdata *ld;
int rc;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|ss", &link, &ldap_bind_dn, &ldap_bind_dnlen, &ldap_bind_pw, &ldap_bind_pwlen) == FAILURE) {
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
if ((rc = ldap_bind_s(ld->link, ldap_bind_dn, ldap_bind_pw, LDAP_AUTH_SIMPLE)) != LDAP_SUCCESS) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to bind to server: %s", ldap_err2string(rc));
RETURN_FALSE;
} else {
RETURN_TRUE;
}
}
/* }}} */
/* {{{ proto bool ldap_unbind(resource link)
Unbind from LDAP directory */
PHP_FUNCTION(ldap_unbind)
{
zval *link;
ldap_linkdata *ld;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &link) == FAILURE) {
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
zend_list_delete(Z_LVAL_P(link));
RETURN_TRUE;
}
/* }}} */
/* {{{ php_set_opts
*/
static void php_set_opts(LDAP *ldap, int sizelimit, int timelimit, int deref)
{
/* sizelimit */
if (sizelimit > -1) {
#if ( LDAP_API_VERSION >= 2004 ) || HAVE_NSLDAP
ldap_set_option(ldap, LDAP_OPT_SIZELIMIT, &sizelimit);
#else
ldap->ld_sizelimit = sizelimit;
#endif
}
/* timelimit */
if (timelimit > -1) {
#if ( LDAP_API_VERSION >= 2004 ) || HAVE_NSLDAP
ldap_set_option(ldap, LDAP_OPT_TIMELIMIT, &timelimit);
#else
ldap->ld_timelimit = timelimit;
#endif
}
/* deref */
if (deref > -1) {
#if ( LDAP_API_VERSION >= 2004 ) || HAVE_NSLDAP
ldap_set_option(ldap, LDAP_OPT_DEREF, &deref);
#else
ldap->ld_deref = deref;
#endif
}
}
/* }}} */
/* {{{ php_ldap_do_search
*/
static void php_ldap_do_search(INTERNAL_FUNCTION_PARAMETERS, int scope)
{
pval **link, **base_dn, **filter, **attrs, **attr, **attrsonly, **sizelimit, **timelimit, **deref;
char *ldap_base_dn = NULL;
char *ldap_filter = NULL;
char **ldap_attrs = NULL;
ldap_linkdata *ld;
LDAPMessage *ldap_res;
int ldap_attrsonly = 0;
int ldap_sizelimit = -1;
int ldap_timelimit = -1;
int ldap_deref = -1;
int num_attribs = 0;
int i, errno;
int myargcount = ZEND_NUM_ARGS();
if (myargcount < 3 || myargcount > 8 || zend_get_parameters_ex(myargcount, &link, &base_dn, &filter, &attrs, &attrsonly, &sizelimit, &timelimit, &deref) == FAILURE) {
WRONG_PARAM_COUNT;
}
/* Reverse -> fall through */
switch(myargcount) {
case 8 :
convert_to_long_ex(deref);
ldap_deref = Z_LVAL_PP(deref);
case 7 :
convert_to_long_ex(timelimit);
ldap_timelimit = Z_LVAL_PP(timelimit);
case 6 :
convert_to_long_ex(sizelimit);
ldap_sizelimit = Z_LVAL_PP(sizelimit);
case 5 :
convert_to_long_ex(attrsonly);
ldap_attrsonly = Z_LVAL_PP(attrsonly);
case 4 :
if (Z_TYPE_PP(attrs) != IS_ARRAY) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expected Array as last element");
RETURN_FALSE;
}
num_attribs = zend_hash_num_elements(Z_ARRVAL_PP(attrs));
if ((ldap_attrs = emalloc((num_attribs+1) * sizeof(char *))) == NULL) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not allocate memory");
RETURN_FALSE;
}
for(i=0; i<num_attribs; i++) {
if(zend_hash_index_find(Z_ARRVAL_PP(attrs), i, (void **) &attr) == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Array initialization wrong");
efree(ldap_attrs);
RETURN_FALSE;
}
SEPARATE_ZVAL(attr);
convert_to_string_ex(attr);
ldap_attrs[i] = Z_STRVAL_PP(attr);
}
ldap_attrs[num_attribs] = NULL;
case 3 :
/* parallel search? */
if (Z_TYPE_PP(link) != IS_ARRAY) {
convert_to_string_ex(filter);
ldap_filter = Z_STRVAL_PP(filter);
convert_to_string_ex(base_dn);
ldap_base_dn = Z_STRVAL_PP(base_dn);
}
break;
default:
WRONG_PARAM_COUNT;
break;
}
/* parallel search? */
if (Z_TYPE_PP(link) == IS_ARRAY) {
int i, nlinks, nbases, nfilters, *rcs;
ldap_linkdata **lds;
zval **entry, *resource;
nlinks = zend_hash_num_elements(Z_ARRVAL_PP(link));
if (nlinks == 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "No links in link array");
if (ldap_attrs != NULL) {
efree(ldap_attrs);
}
RETURN_FALSE;
}
if (Z_TYPE_PP(base_dn) == IS_ARRAY) {
nbases = zend_hash_num_elements(Z_ARRVAL_PP(base_dn));
if (nbases != nlinks) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Base must either be a string, or an array with the same number of elements as the links array");
if (ldap_attrs != NULL) {
efree(ldap_attrs);
}
RETURN_FALSE;
}
zend_hash_internal_pointer_reset(Z_ARRVAL_PP(base_dn));
} else {
nbases = 0; /* this means string, not array */
convert_to_string_ex(base_dn);
ldap_base_dn = Z_STRLEN_PP(base_dn) < 1 ? NULL : Z_STRVAL_PP(base_dn);
}
if (Z_TYPE_PP(filter) == IS_ARRAY) {
nfilters = zend_hash_num_elements(Z_ARRVAL_PP(filter));
if (nfilters != nlinks) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Filter must either be a string, or an array with the same number of elements as the links array");
if (ldap_attrs != NULL) {
efree(ldap_attrs);
}
RETURN_FALSE;
}
zend_hash_internal_pointer_reset(Z_ARRVAL_PP(filter));
} else {
nfilters = 0; /* this means string, not array */
convert_to_string_ex(filter);
ldap_filter = Z_STRVAL_PP(filter);
}
lds = emalloc(nlinks * sizeof(ldap_linkdata));
rcs = emalloc(nlinks * sizeof(*rcs));
zend_hash_internal_pointer_reset(Z_ARRVAL_PP(link));
for (i=0; i<nlinks; i++) {
zend_hash_get_current_data(Z_ARRVAL_PP(link), (void **)&entry);
ld = (ldap_linkdata *) zend_fetch_resource(entry TSRMLS_CC, -1, "ldap link", NULL, 1, le_link);
if (ld == NULL) {
efree(lds);
efree(rcs);
if (ldap_attrs != NULL) {
efree(ldap_attrs);
}
RETURN_FALSE;
}
if (nbases != 0) { /* base_dn an array? */
zend_hash_get_current_data(Z_ARRVAL_PP(base_dn), (void **)&entry);
zend_hash_move_forward(Z_ARRVAL_PP(base_dn));
convert_to_string_ex(entry);
ldap_base_dn = Z_STRLEN_PP(entry) < 1 ? NULL : Z_STRVAL_PP(entry);
}
if (nfilters != 0) { /* filter an array? */
zend_hash_get_current_data(Z_ARRVAL_PP(filter), (void **)&entry);
zend_hash_move_forward(Z_ARRVAL_PP(filter));
convert_to_string_ex(entry);
ldap_filter = Z_STRVAL_PP(entry);
}
php_set_opts(ld->link, ldap_sizelimit, ldap_timelimit, ldap_deref);
/* Run the actual search */
rcs[i] = ldap_search(ld->link, ldap_base_dn, scope, ldap_filter, ldap_attrs, ldap_attrsonly);
lds[i] = ld;
zend_hash_move_forward(Z_ARRVAL_PP(link));
}
if (ldap_attrs != NULL) {
efree(ldap_attrs);
}
array_init(return_value);
/* Collect results from the searches */
for (i=0; i<nlinks; i++) {
MAKE_STD_ZVAL(resource);
if (rcs[i] != -1) {
rcs[i] = ldap_result(lds[i]->link, LDAP_RES_ANY, 1 /* LDAP_MSG_ALL */, NULL, &ldap_res);
}
if (rcs[i] != -1) {
ZEND_REGISTER_RESOURCE(resource, ldap_res, le_result);
add_next_index_zval(return_value, resource);
} else {
add_next_index_bool(return_value, 0);
}
}
efree(lds);
efree(rcs);
return;
}
/* fix to make null base_dn's work */
if ( strlen(ldap_base_dn) < 1 ) {
ldap_base_dn = NULL;
}
ld = (ldap_linkdata *) zend_fetch_resource(link TSRMLS_CC, -1, "ldap link", NULL, 1, le_link);
if (ld == NULL) {
if (ldap_attrs != NULL) {
efree(ldap_attrs);
}
RETURN_FALSE;
}
php_set_opts(ld->link, ldap_sizelimit, ldap_timelimit, ldap_deref);
/* Run the actual search */
errno = ldap_search_s(ld->link, ldap_base_dn, scope, ldap_filter, ldap_attrs, ldap_attrsonly, &ldap_res);
if (ldap_attrs != NULL) {
efree(ldap_attrs);
}
if (errno != LDAP_SUCCESS
&& errno != LDAP_SIZELIMIT_EXCEEDED
#ifdef LDAP_ADMINLIMIT_EXCEEDED
&& errno != LDAP_ADMINLIMIT_EXCEEDED
#endif
#ifdef LDAP_REFERRAL
&& errno != LDAP_REFERRAL
#endif
) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Search: %s", ldap_err2string(errno));
RETVAL_FALSE;
} else {
if (errno == LDAP_SIZELIMIT_EXCEEDED) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Partial search results returned: Sizelimit exceeded.");
}
#ifdef LDAP_ADMINLIMIT_EXCEEDED
else if (errno == LDAP_ADMINLIMIT_EXCEEDED) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Partial search results returned: Adminlimit exceeded.");
}
#endif
ZEND_REGISTER_RESOURCE(return_value, ldap_res, le_result);
}
}
/* }}} */
/* {{{ proto resource ldap_read(resource link, string base_dn, string filter [, array attrs [, int attrsonly [, int sizelimit [, int timelimit [, int deref]]]]])
Read an entry */
PHP_FUNCTION(ldap_read)
{
php_ldap_do_search(INTERNAL_FUNCTION_PARAM_PASSTHRU, LDAP_SCOPE_BASE);
}
/* }}} */
/* {{{ proto resource ldap_list(resource link, string base_dn, string filter [, array attrs [, int attrsonly [, int sizelimit [, int timelimit [, int deref]]]]])
Single-level search */
PHP_FUNCTION(ldap_list)
{
php_ldap_do_search(INTERNAL_FUNCTION_PARAM_PASSTHRU, LDAP_SCOPE_ONELEVEL);
}
/* }}} */
/* {{{ proto resource ldap_search(resource link, string base_dn, string filter [, array attrs [, int attrsonly [, int sizelimit [, int timelimit [, int deref]]]]])
Search LDAP tree under base_dn */
PHP_FUNCTION(ldap_search)
{
php_ldap_do_search(INTERNAL_FUNCTION_PARAM_PASSTHRU, LDAP_SCOPE_SUBTREE);
}
/* }}} */
/* {{{ proto bool ldap_free_result(resource result)
Free result memory */
PHP_FUNCTION(ldap_free_result)
{
pval **result;
LDAPMessage *ldap_result;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &result) == FAILURE) {
WRONG_PARAM_COUNT;
}
ZEND_FETCH_RESOURCE(ldap_result, LDAPMessage *, result, -1, "ldap result", le_result);
zend_list_delete(Z_LVAL_PP(result)); /* Delete list entry and call registered destructor function */
RETVAL_TRUE;
}
/* }}} */
/* {{{ proto int ldap_count_entries(resource link, resource result)
Count the number of entries in a search result */
PHP_FUNCTION(ldap_count_entries)
{
pval **link, **result;
ldap_linkdata *ld;
LDAPMessage *ldap_result;
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &link, &result) == FAILURE) {
WRONG_PARAM_COUNT;
}
ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, link, -1, "ldap link", le_link);
ZEND_FETCH_RESOURCE(ldap_result, LDAPMessage *, result, -1, "ldap result", le_result);
RETURN_LONG(ldap_count_entries(ld->link, ldap_result));
}
/* }}} */
/* {{{ proto resource ldap_first_entry(resource link, resource result)
Return first result id */
PHP_FUNCTION(ldap_first_entry)
{
pval **link, **result;
ldap_linkdata *ld;
ldap_resultentry *resultentry;
LDAPMessage *ldap_result, *entry;
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &link, &result) == FAILURE) {
WRONG_PARAM_COUNT;
}
ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, link, -1, "ldap link", le_link);
ZEND_FETCH_RESOURCE(ldap_result, LDAPMessage *, result, -1, "ldap result", le_result);
if ((entry = ldap_first_entry(ld->link, ldap_result)) == NULL) {
RETVAL_FALSE;
} else {
resultentry = emalloc(sizeof(ldap_resultentry));
ZEND_REGISTER_RESOURCE(return_value, resultentry, le_result_entry);
resultentry->id = Z_LVAL_PP(result);
zend_list_addref(resultentry->id);
resultentry->data = entry;
}
}
/* }}} */
/* {{{ proto resource ldap_next_entry(resource link, resource result_entry)
Get next result entry */
PHP_FUNCTION(ldap_next_entry)
{
pval **link, **result_entry;
ldap_linkdata *ld;
ldap_resultentry *resultentry, *resultentry_next;
LDAPMessage *entry_next;
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &link, &result_entry) == FAILURE) {
WRONG_PARAM_COUNT;
}
ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, link, -1, "ldap link", le_link);
ZEND_FETCH_RESOURCE(resultentry, ldap_resultentry *, result_entry, -1, "ldap result entry", le_result_entry);
if ((entry_next = ldap_next_entry(ld->link, resultentry->data)) == NULL) {
RETVAL_FALSE;
} else {
resultentry_next = emalloc(sizeof(ldap_resultentry));
ZEND_REGISTER_RESOURCE(return_value, resultentry_next, le_result_entry);
resultentry_next->id = resultentry->id;
zend_list_addref(resultentry->id);
resultentry_next->data = entry_next;
}
}
/* }}} */
/* {{{ proto array ldap_get_entries(resource link, resource result)
Get all result entries */
PHP_FUNCTION(ldap_get_entries)
{
pval **link, **result;
LDAPMessage *ldap_result, *ldap_result_entry;
pval *tmp1, *tmp2;
ldap_linkdata *ld;
LDAP *ldap;
int num_entries, num_attrib, num_values, i;
BerElement *ber;
char *attribute;
size_t attr_len;
char **ldap_value;
char *dn;
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &link, &result) == FAILURE) {
WRONG_PARAM_COUNT;
}
ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, link, -1, "ldap link", le_link);
ZEND_FETCH_RESOURCE(ldap_result, LDAPMessage *, result, -1, "ldap result", le_result);
ldap = ld->link;
num_entries = ldap_count_entries(ldap, ldap_result);
array_init(return_value);
add_assoc_long(return_value, "count", num_entries);
if (num_entries == 0) return;
num_entries = 0;
ldap_result_entry = ldap_first_entry(ldap, ldap_result);
if (ldap_result_entry == NULL) RETURN_FALSE;
while(ldap_result_entry != NULL) {
MAKE_STD_ZVAL(tmp1);
array_init(tmp1);
num_attrib = 0;
attribute = ldap_first_attribute(ldap, ldap_result_entry, &ber);
while (attribute != NULL) {
ldap_value = ldap_get_values(ldap, ldap_result_entry, attribute);
num_values = ldap_count_values(ldap_value);
MAKE_STD_ZVAL(tmp2);
array_init(tmp2);
add_assoc_long(tmp2, "count", num_values);
for(i=0; i < num_values; i++) {
add_index_string(tmp2, i, ldap_value[i], 1);
}
ldap_value_free(ldap_value);
attr_len = strlen(attribute);
zend_hash_update(Z_ARRVAL_P(tmp1), php_strtolower(attribute, attr_len), attr_len+1, (void *) &tmp2, sizeof(pval *), NULL);
add_index_string(tmp1, num_attrib, attribute, 1);
num_attrib++;
#if ( LDAP_API_VERSION > 2000 ) || HAVE_NSLDAP || WINDOWS
ldap_memfree(attribute);
#endif
attribute = ldap_next_attribute(ldap, ldap_result_entry, ber);
}
#if ( LDAP_API_VERSION > 2000 ) || HAVE_NSLDAP || WINDOWS
if (ber != NULL)
ber_free(ber, 0);
#endif
add_assoc_long(tmp1, "count", num_attrib);
dn = ldap_get_dn(ldap, ldap_result_entry);
add_assoc_string(tmp1, "dn", dn, 1);
#if ( LDAP_API_VERSION > 2000 ) || HAVE_NSLDAP || WINDOWS
ldap_memfree(dn);
#else
free(dn);
#endif
zend_hash_index_update(Z_ARRVAL_P(return_value), num_entries, (void *) &tmp1, sizeof(pval *), NULL);
num_entries++;
ldap_result_entry = ldap_next_entry(ldap, ldap_result_entry);
}
add_assoc_long(return_value, "count", num_entries);
}
/* }}} */
/* {{{ proto string ldap_first_attribute(resource link, resource result_entry, int ber)
Return first attribute */
PHP_FUNCTION(ldap_first_attribute)
{
pval **link, **result_entry, **berp;
ldap_linkdata *ld;
ldap_resultentry *resultentry;
BerElement *ber;
char *attribute;
if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &link, &result_entry, &berp) == FAILURE) {
WRONG_PARAM_COUNT;
}
ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, link, -1, "ldap link", le_link);
ZEND_FETCH_RESOURCE(resultentry, ldap_resultentry *, result_entry, -1, "ldap result entry", le_result_entry);
if ((attribute = ldap_first_attribute(ld->link, resultentry->data, &ber)) == NULL) {
RETURN_FALSE;
} else {
ZEND_REGISTER_RESOURCE(*berp, ber, le_ber_entry);
RETVAL_STRING(attribute, 1);
#if ( LDAP_API_VERSION > 2000 ) || HAVE_NSLDAP || WINDOWS
ldap_memfree(attribute);
#endif
}
}
/* }}} */
/* {{{ proto string ldap_next_attribute(resource link, resource result_entry, resource ber)
Get the next attribute in result */
PHP_FUNCTION(ldap_next_attribute)
{
pval **link, **result_entry, **berp;
ldap_linkdata *ld;
ldap_resultentry *resultentry;
BerElement *ber;