-
Notifications
You must be signed in to change notification settings - Fork 6
/
LDAPapi.pm
3188 lines (2258 loc) · 86.1 KB
/
LDAPapi.pm
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
package Net::LDAPapi;
use strict;
use Carp;
use Convert::ASN1;
use vars qw($VERSION @ISA @EXPORT $AUTOLOAD);
no warnings "uninitialized";
require Exporter;
require DynaLoader;
require AutoLoader;
@ISA = qw(Exporter DynaLoader);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
@EXPORT =
qw(
ldap_create ldap_set_option ldap_get_option ldap_unbind_ext
ldap_unbind_ext_s ldap_version ldap_abandon_ext ldap_add_ext ldap_add_ext_s
ldap_set_rebind_proc
ldap_rename ldap_rename_s
ldap_compare_ext ldap_compare_ext_s ldap_delete_ext
ldap_delete_ext_s ldap_search_ext ldap_search_ext_s ldap_result
ldap_extended_operation ldap_extended_operation_s ldap_parse_extended_result
ldap_parse_whoami ldap_whoami ldap_whoami_s
ldap_msgfree ldap_msg_free ldap_msgid ldap_msgtype
ldap_get_lderrno ldap_set_lderrno ldap_parse_result ldap_err2string
ldap_count_entries ldap_first_entry ldap_next_entry ldap_get_dn
ldap_err2string ldap_dn2ufn ldap_str2dn ldap_str2rdn ldap_explode_rdn
ldap_explode_dns ldap_first_attribute ldap_next_attribute
ldap_get_values ldap_get_values_len ldap_sasl_bind ldap_sasl_bind_s
ldapssl_client_init ldapssl_init ldapssl_install_routines
ldap_get_all_entries ldap_multisort_entries
ldap_is_ldap_url ldap_url_parse ldap_url_search ldap_url_search_s
ldap_url_search_st ber_free ldap_init ldap_initialize ldap_start_tls_s
ldap_sasl_interactive_bind_s
ldap_create_control ldap_control_berval
LDAP_RES_BIND
LDAP_RES_SEARCH_ENTRY
LDAP_RES_SEARCH_REFERENCE
LDAP_RES_SEARCH_RESULT
LDAP_RES_MODIFY
LDAP_RES_ADD
LDAP_RES_DELETE
LDAP_RES_MODDN
LDAP_RES_COMPARE
LDAP_RES_EXTENDED
LDAP_RES_INTERMEDIATE
LDAP_RES_ANY
LDAP_RES_UNSOLICITED
LDAPS_PORT
LDAP_ADMIN_LIMIT_EXCEEDED
LDAP_AFFECTS_MULTIPLE_DSAS
LDAP_ALIAS_DEREF_PROBLEM
LDAP_ALIAS_PROBLEM
LDAP_ALREADY_EXISTS
LDAP_AUTH_KRBV4
LDAP_AUTH_KRBV41
LDAP_AUTH_KRBV41_30
LDAP_AUTH_KRBV42
LDAP_AUTH_KRBV42_30
LDAP_AUTH_NONE
LDAP_AUTH_SASL
LDAP_AUTH_SIMPLE
LDAP_AUTH_UNKNOWN
LDAP_BUSY
LDAP_CACHE_CHECK
LDAP_CACHE_LOCALDB
LDAP_CACHE_POPULATE
LDAP_CALLBACK
LDAP_COMPARE_FALSE
LDAP_COMPARE_TRUE
LDAP_CONNECT_ERROR
LDAP_CONSTRAINT_VIOLATION
LDAP_CONTROL_ASSERT
LDAP_CONTROL_DUPENT
LDAP_CONTROL_DUPENT_ENTRY
LDAP_CONTROL_DUPENT_REQUEST
LDAP_CONTROL_DUPENT_RESPONSE
LDAP_CONTROL_GROUPING
LDAP_CONTROL_MANAGEDIT
LDAP_CONTROL_MANAGEDSAIT
LDAP_CONTROL_NOOP
LDAP_CONTROL_NO_SUBORDINATES
LDAP_CONTROL_PAGEDRESULTS
LDAP_CONTROL_PASSWORDPOLICYREQUEST
LDAP_CONTROL_PASSWORDPOLICYRESPONSE
LDAP_CONTROL_PERSIST_ENTRY_CHANGE_NOTICE
LDAP_CONTROL_PERSIST_REQUEST
LDAP_CONTROL_POST_READ
LDAP_CONTROL_PRE_READ
LDAP_CONTROL_PROXY_AUTHZ
LDAP_CONTROL_SLURP
LDAP_CONTROL_SORTREQUEST
LDAP_CONTROL_SORTRESPONSE
LDAP_CONTROL_SUBENTRIES
LDAP_CONTROL_SYNC
LDAP_CONTROL_SYNC_DONE
LDAP_CONTROL_SYNC_STATE
LDAP_CONTROL_VALSORT
LDAP_CONTROL_VALUESRETURNFILTER
LDAP_CONTROL_VLVREQUEST
LDAP_CONTROL_VLVRESPONSE
LDAP_CONTROL_X_CHAINING_BEHAVIOR
LDAP_CONTROL_X_DOMAIN_SCOPE
LDAP_CONTROL_X_EXTENDED_DN
LDAP_CONTROL_X_INCREMENTAL_VALUES
LDAP_CONTROL_X_PERMISSIVE_MODIFY
LDAP_CONTROL_X_SEARCH_OPTIONS
LDAP_CONTROL_X_TREE_DELETE
LDAP_CONTROL_X_VALUESRETURNFILTER
LDAP_CUP_INVALID_DATA
LDAP_DECODING_ERROR
LDAP_DEREF_ALWAYS
LDAP_DEREF_FINDING
LDAP_DEREF_NEVER
LDAP_DEREF_SEARCHING
LDAP_ENCODING_ERROR
LDAP_FILTER_ERROR
LDAP_FILT_MAXSIZ
LDAP_INAPPROPRIATE_AUTH
LDAP_INAPPROPRIATE_MATCHING
LDAP_INSUFFICIENT_ACCESS
LDAP_INVALID_CREDENTIALS
LDAP_INVALID_DN_SYNTAX
LDAP_INVALID_SYNTAX
LDAP_IS_LEAF
LDAP_LOCAL_ERROR
LDAP_LOOP_DETECT
LDAP_MOD_ADD
LDAP_MOD_BVALUES
LDAP_MOD_DELETE
LDAP_MOD_REPLACE
LDAP_NAMING_VIOLATION
LDAP_NOT_ALLOWED_ON_NONLEAF
LDAP_NOT_ALLOWED_ON_RDN
LDAP_NO_LIMIT
LDAP_NO_MEMORY
LDAP_NO_OBJECT_CLASS_MODS
LDAP_NO_SUCH_ATTRIBUTE
LDAP_NO_SUCH_OBJECT
LDAP_OBJECT_CLASS_VIOLATION
LDAP_OPERATIONS_ERROR
LDAP_OPT_CACHE_ENABLE
LDAP_OPT_CACHE_FN_PTRS
LDAP_OPT_CACHE_STRATEGY
LDAP_OPT_DEBUG_LEVEL
LDAP_OPT_DEREF
LDAP_OPT_DESC
LDAP_OPT_DNS
LDAP_OPT_IO_FN_PTRS
LDAP_OPT_OFF
LDAP_OPT_ON
LDAP_OPT_PROTOCOL_VERSION
LDAP_OPT_REBIND_ARG
LDAP_OPT_REBIND_FN
LDAP_OPT_REFERRALS
LDAP_OPT_REFERRAL_HOP_LIMIT
LDAP_OPT_RESTART
LDAP_OPT_SIZELIMIT
LDAP_OPT_SSL
LDAP_OPT_THREAD_FN_PTRS
LDAP_OPT_TIMELIMIT
LDAP_OPT_TIMEOUT
LDAP_OPT_NETWORK_TIMEOUT
LDAP_OTHER
LDAP_PARAM_ERROR
LDAP_PARTIAL_RESULTS
LDAP_PORT
LDAP_PORT_MAX
LDAP_PROTOCOL_ERROR
LDAP_REFERRAL
LDAP_RESULTS_TOO_LARGE
LDAP_SASL_AUTOMATIC
LDAP_SASL_INTERACTIVE
LDAP_SASL_NULL
LDAP_SASL_QUIET
LDAP_SASL_SIMPLE
LDAP_SCOPE_BASE
LDAP_SCOPE_ONELEVEL
LDAP_SCOPE_SUBTREE
LDAP_SECURITY_NONE
LDAP_SERVER_DOWN
LDAP_SIZELIMIT_EXCEEDED
LDAP_STRONG_AUTH_NOT_SUPPORTED
LDAP_STRONG_AUTH_REQUIRED
LDAP_SUCCESS
LDAP_SYNC_INFO
LDAP_TIMELIMIT_EXCEEDED
LDAP_TIMEOUT
LDAP_TYPE_OR_VALUE_EXISTS
LDAP_UNAVAILABLE
LDAP_UNAVAILABLE_CRITICAL_EXTN
LDAP_UNDEFINED_TYPE
LDAP_UNWILLING_TO_PERFORM
LDAP_URL_ERR_BADSCOPE
LDAP_URL_ERR_MEM
LDAP_URL_ERR_NODN
LDAP_URL_ERR_NOTLDAP
LDAP_URL_ERR_PARAM
LDAP_URL_OPT_SECURE
LDAP_USER_CANCELLED
LDAP_VERSION
LDAP_VERSION1
LDAP_VERSION2
LDAP_VERSION3
LDAP_TAG_SYNC_NEW_COOKIE
LDAP_TAG_SYNC_REFRESH_DELETE
LDAP_TAG_SYNC_REFRESH_PRESENT
LDAP_TAG_SYNC_ID_SET
LDAP_TAG_SYNC_COOKIE
LDAP_TAG_REFRESHDELETES
LDAP_TAG_REFRESHDONE
LDAP_TAG_RELOAD_HINT
LDAP_TAG_EXOP_MODIFY_PASSWD_ID
LDAP_TAG_EXOP_MODIFY_PASSWD_OLD
LDAP_TAG_EXOP_MODIFY_PASSWD_NEW
LDAP_TAG_EXOP_MODIFY_PASSWD_GEN
LDAP_TAG_MESSAGE
LDAP_TAG_MSGID
LDAP_TAG_LDAPDN
LDAP_TAG_LDAPCRED
LDAP_TAG_CONTROLS
LDAP_TAG_REFERRAL
LDAP_TAG_NEWSUPERIOR
LDAP_TAG_EXOP_REQ_OID
LDAP_TAG_EXOP_REQ_VALUE
LDAP_TAG_EXOP_RES_OID
LDAP_TAG_EXOP_RES_VALUE
LDAP_TAG_IM_RES_OID
LDAP_TAG_IM_RES_VALUE
LDAP_TAG_SASL_RES_CREDS
);
$VERSION = '3.0.x';
sub AUTOLOAD {
# This AUTOLOAD is used to 'autoload' constants from the constant()
# XS function. If a constant is not found then control is passed
# to the AUTOLOAD in AutoLoader.
my $constname;
($constname = $AUTOLOAD) =~ s/.*:://;
my $val = constant($constname, @_ ? $_[0] : 0);
if ($! != 0) {
if ($! =~ /Invalid/) {
# try constants_h
$val = '"'.constant_s($constname).'"';
goto SUBDEF if ($! == 0);
$AutoLoader::AUTOLOAD = $AUTOLOAD;
goto &AutoLoader::AUTOLOAD;
} else {
croak "Your vendor has not defined LDAP macro $constname";
}
}
SUBDEF:
eval "sub $AUTOLOAD { $val }";
goto &$AUTOLOAD;
}
bootstrap Net::LDAPapi $VERSION;
# creats blessed ldap object.
# accepts following arguments '-host', '-port', '-url', '-debug'
# if '-url' is given then then '-host' and '-port' are not used
sub new
{
my ($this, @args) = @_;
my $class = ref($this) || $this;
my $self = {};
my $ld;
bless $self, $class;
my ($host, $port, $url, $debug) =
$self->rearrange(['HOST','PORT','URL', 'DEBUG'],@args);
if ( defined($url) ) {
return -1 unless (ldap_initialize($ld, $url) == $self->LDAP_SUCCESS );
} else {
$host = "localhost" unless $host;
$port = $self->LDAP_PORT unless $port;
return -1 unless ( ldap_initialize($ld, "ldap://$host:$port") == $self-> LDAP_SUCCESS);
}
# Following ASN.1 contains definitions for synrepl API
my $asn = Convert::ASN1->new;
$asn->prepare(<<ASN) or die "prepare: ", $asn->error;
syncUUID ::= OCTET STRING
syncCookie ::= OCTET STRING
syncRequestValue ::= SEQUENCE {
mode ENUMERATED {
refreshOnly (1),
refreshAndPersist (3)
},
cookie syncCookie OPTIONAL,
reloadHint BOOLEAN
}
syncStateValue ::= SEQUENCE {
state ENUMERATED,
entryUUID syncUUID,
cookie syncCookie OPTIONAL
}
refresh_Delete ::= SEQUENCE {
cookie syncCookie OPTIONAL,
refreshDone BOOLEAN OPTIONAL
}
refresh_Present ::= SEQUENCE {
cookie syncCookie OPTIONAL,
refreshDone BOOLEAN OPTIONAL
}
syncId_Set ::= SEQUENCE {
cookie syncCookie OPTIONAL,
refreshDeletes BOOLEAN OPTIONAL,
syncUUIDs SET OF syncUUID
}
syncInfoValue ::= CHOICE {
newcookie [0] syncCookie,
refreshDelete [1] refresh_Delete,
refreshPresent [2] refresh_Present,
syncIdSet [3] syncId_Set
}
ASN
$self->{"asn"} = $asn;
$self->{"ld"} = $ld;
$self->{"errno"} = 0;
$self->{"errstring"} = undef;
$self->{"debug"} = $debug;
ldap_set_option($ld, $self->LDAP_OPT_PROTOCOL_VERSION, $self->LDAP_VERSION3);
return $self;
} # end of new
sub DESTROY {};
sub abandon
{
my ($self, @args) = @_;
my ($status, $sctrls, $cctrls);
my ($msgid, $serverctrls, $clientctrls) =
$self->rearrange(['MSGID', 'SCTRLS', 'CCTRLS'], @args);
croak("Invalid MSGID") if ($msgid < 0);
$sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
$cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;
$status = ldap_abandon_ext($self->{"ld"}, $msgid, $sctrls, $cctrls);
$self->errorize($status);
ldap_controls_array_free($sctrls) if $sctrls;
ldap_controls_array_free($cctrls) if $cctrls;
return $status;
} # end of abandon
# synonim for abandon(...)
sub abandon_ext {
my ($self, @args) = @_;
return $self->abandon(@args);
} # end of abandon_ext
sub add
{
my ($self,@args) = @_;
my ($msgid, $sctrls, $cctrls, $status);
my ($dn, $mod, $serverctrls, $clientctrls) =
$self->rearrange(['DN', 'MOD', 'SCTRLS', 'CCTRLS'], @args);
croak("No DN Specified") if ($dn eq "");
croak("LDAPMod structure is not a hash reference.") if( ref($mod) ne "HASH" );
$sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
$cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;
$status = ldap_add_ext($self->{"ld"}, $dn, $mod, $sctrls, $cctrls, $msgid);
ldap_controls_array_free($sctrls) if $sctrls;
ldap_controls_array_free($cctrls) if $cctrls;
$self->errorize($status);
if( $status != $self->LDAP_SUCCESS ) {
return undef;
}
return $msgid;
} # end of add
# synonym for add
sub add_ext
{
my ($self, @args) = @_;
return $self->add(@args);
} # end of add_ext
sub add_s
{
my ($self,@args) = @_;
my ($sctrls, $cctrls, $status);
my ($dn, $mod, $serverctrls, $clientctrls) =
$self->rearrange(['DN', 'MOD', 'SCTRLS', 'CCTRLS'], @args);
croak("No DN Specified") if ($dn eq "");
croak("LDAP Modify Structure Not a HASH Reference") if (ref($mod) ne "HASH");
$sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
$cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;
$status = ldap_add_ext_s($self->{"ld"}, $dn, $mod, $sctrls, $cctrls);
ldap_controls_array_free($sctrls) if $sctrls;
ldap_controls_array_free($cctrls) if $cctrls;
$self->errorize($status);
return $status;
} # end of add_s
# synonym for add_s
sub add_ext_s
{
my ($self, @args) = @_;
return $self->add_s(@args);
} # end of add_ext_s
sub bind
{
my ($self,@args) = @_;
my ($msgid, $sctrls, $cctrls, $status);
my ($dn, $pass, $authtype, $serverctrls, $clientctrls) =
$self->rearrange(['DN', 'PASSWORD', 'TYPE', 'SCTRLS', 'CCTRLS'],@args);
$dn = "" unless $dn;
$pass = "" unless $pass;
$authtype = $authtype || $self->LDAP_AUTH_SIMPLE;
croak("bind supports only LDAP_AUTH_SIMPLE auth type")
unless $authtype == $self->LDAP_AUTH_SIMPLE;
$sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
$cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;
$status = ldap_sasl_bind($self->{"ld"}, $dn, $pass,
$sctrls, $cctrls, $msgid);
ldap_controls_array_free($sctrls) if $sctrls;
ldap_controls_array_free($cctrls) if $cctrls;
$self->errorize($status);
if( $status != $self->LDAP_SUCCESS ) {
return undef;
}
return $msgid;
} # end of bind
sub bind_s
{
my ($self, @args) = @_;
my ($status, $servercredp, $sctrls, $cctrls);
my ($dn, $pass, $authtype, $serverctrls, $clientctrls) =
$self->rearrange(['DN', 'PASSWORD', 'TYPE', 'SCTRLS', 'CCTRLS'], @args);
$dn = "" unless $dn;
$pass = "" unless $pass;
$sctrls = 0 unless $sctrls;
$cctrls = 0 unless $cctrls;
$authtype = $authtype || $self->LDAP_AUTH_SIMPLE;
$sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
$cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;
if ($authtype == $self->LDAP_AUTH_SASL) {
$status =
ldap_sasl_interactive_bind_s($self->{"ld"}, $dn, $pass,
$sctrls, $cctrls, $self->{"saslmech"},
$self->{"saslrealm"},
$self->{"saslauthzid"},
$self->{"saslsecprops"},
$self->{"saslflags"});
} else {
# not sure here what to do with $servercredp
$status = ldap_sasl_bind_s($self->{"ld"}, $dn, $pass,
$sctrls, $cctrls, \$servercredp);
}
ldap_controls_array_free($sctrls) if $sctrls;
ldap_controls_array_free($cctrls) if $cctrls;
$self->errorize($status);
return $status;
} # end of bind_s
sub sasl_parms
{
my ($self,@args) = @_;
my ($mech, $realm, $authzid, $secprops, $flags) =
$self->rearrange(['MECH', 'REALM', 'AUTHZID', 'SECPROPS', 'FLAGS'],
@args);
$mech = "" unless $mech;
$realm = "" unless $realm;
$authzid = "" unless $authzid;
$secprops = "" unless $secprops;
$flags = $self->LDAP_SASL_QUIET unless defined($flags);
$self->{"saslmech"} = $mech;
$self->{"saslrealm"} = $realm;
$self->{"saslauthzid"} = $authzid;
$self->{"saslsecprops"} = $secprops;
$self->{"saslflags"} = $flags;
} # end of sasl_parms
sub compare
{
my ($self, @args) = @_;
my ($status, $msgid, $sctrls, $cctrls);
my ($dn, $attr, $value, $serverctrls, $clientctrls) =
$self->rearrange(['DN','ATTR', ['VALUE', 'VALUES'], 'SCTRLS', 'CCTRLS'], @args);
croak("No DN Specified") if ($dn eq "");
$value = "" unless $value;
$sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
$cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;
$status =
ldap_compare_ext($self->{"ld"}, $dn, $attr, $value, $sctrls, $cctrls, $msgid);
ldap_controls_array_free($sctrls) if $sctrls;
ldap_controls_array_free($cctrls) if $cctrls;
$self->errorize($status);
if( $status != $self->LDAP_SUCCESS ) {
return undef;
}
return $msgid;
} # end of compare
# synonym for compare
sub compare_ext {
my ($self, @args) = @_;
return $self->compare(@args);
} # end of compare_ext
# needs to use ldap_compare_ext_s
sub compare_s
{
my ($self, @args) = @_;
my ($status, $sctrls, $cctrls);
my ($dn, $attr, $value, $serverctrls, $clientctrls) =
$self->rearrange(['DN', 'ATTR' , ['VALUE', 'VALUES'], 'SCTRLS', 'CCTRLS'], @args);
croak("No DN Specified") if ($dn eq "");
$value = "" unless $value;
$sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
$cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;
$status = ldap_compare_ext_s($self->{"ld"}, $dn, $attr, $value, $sctrls, $cctrls);
ldap_controls_array_free($sctrls) if $sctrls;
ldap_controls_array_free($cctrls) if $cctrls;
$self->errorize($status);
return $status;
} # end of compare_s
# synonym for compare
sub compare_ext_s {
my ($self, @args) = @_;
return $self->compare_s(@args);
} # end of compare_ext
# needs DOC in POD bellow. XXX
sub start_tls
{
my ($self, @args) = @_;
my ($msgid, $status, $sctrls, $cctrls);
my ($serverctrls, $clientctrls) =
$self->rearrange(['SCTRLS', 'CCTRLS'], @args);
$sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
$cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;
$status = ldap_start_tls($self->{"ld"}, $sctrls, $cctrls, $msgid);
ldap_controls_array_free($sctrls) if $sctrls;
ldap_controls_array_free($cctrls) if $cctrls;
$self->errorize($status);
if( $status != $self->LDAP_SUCCESS ) {
return undef;
}
return $msgid;
} # end of start_tls
# needs DOC in POD bellow. XXX
sub start_tls_s
{
my ($self, @args) = @_;
my ($status, $sctrls, $cctrls);
$sctrls=0;
$cctrls=0;
my ($serverctrls, $clientctrls) = $self->rearrange(['SCTRLS', 'CCTRLS'], @args);
$sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
$cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;
$status = ldap_start_tls_s($self->{"ld"}, $sctrls, $cctrls);
ldap_controls_array_free($sctrls) if $sctrls;
ldap_controls_array_free($cctrls) if $cctrls;
$self->errorize($status);
return $status;
} # end of start_tls_s
sub count_entries
{
my ($self, @args) = @_;
my ($result) = $self->rearrange(['RESULT'], @args);
$result = $self->{"result"} unless $result;
croak("No result is given") unless $result;
return ldap_count_entries($self->{"ld"}, $result);
} # end of count_entries
sub delete
{
my ($self,@args) = @_;
my ($msgid, $status, $sctrls, $cctrls);
my ($dn, $serverctrls, $clientctrls) =
$self->rearrange(['DN', 'SCTRLS', 'CCTRLS'], @args);
croak("No DN Specified") if ($dn eq "");
$sctrls = 0;
$cctrls = 0;
$sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
$cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;
$status = ldap_delete_ext($self->{"ld"}, $dn, $sctrls, $cctrls, $msgid);
ldap_controls_array_free($sctrls) if $sctrls;
ldap_controls_array_free($cctrls) if $cctrls;
$self->errorize($status);
if( $status != $self->LDAP_SUCCESS ) {
return undef;
}
return $msgid;
} # end of delete
sub delete_s
{
my ($self,@args) = @_;
my ($status, $sctrls, $cctrls);
my ($dn, $serverctrls, $clientctrls) =
$self->rearrange(['DN', 'SCTRLS', 'CCTRLS'], @args);
croak("No DN Specified") if ($dn eq "");
$sctrls = $self->create_controls_array(@$serverctrls) if $serverctrls;
$cctrls = $self->create_controls_array(@$clientctrls) if $clientctrls;
$status = ldap_delete_ext_s($self->{"ld"}, $dn, $sctrls, $cctrls);
ldap_controls_array_free($sctrls) if $sctrls;
ldap_controls_array_free($cctrls) if $cctrls;
$self->errorize($status);
return $status;
} # end of delete_s
sub dn2ufn
{
my ($self, @args) = @_;
my ($dn) = $self->rearrange(['DN'], @args);
return ldap_dn2ufn($dn);
} # end of dn2ufn
sub explode_dn
{
my ($self, @args) = @_;
my ($dn, $notypes) = $self->rearrange(['DN', 'NOTYPES'],@args);
return ldap_explode_dn($dn, $notypes);
} # end of explode_dn
sub explode_rdn
{
my ($self, @args) = @_;
my (@components);
my ($rdn, $notypes) = $self->rearrange(['RDN', 'NOTYPES'], @args);
return ldap_explode_rdn($rdn, $notypes);
} # end of explode_rdn
sub first_message
{
my ($self, @args) = @_;
my ($result) = $self->rearrange(['RESULT'], @args);
$result = $self->{"result"} unless $result;
croak("No Current Result") unless $result;
$self->{"msg"} = ldap_first_message($self->{"ld"}, $self->{"result"});
return $self->{"msg"};
} # end of first_message
sub next_message
{
my ($self, @args) = @_;
my ($msg) = $self->rearrange(['MSG'], @args);
$msg = $self->{"msg"} unless $msg;
croak("No Current Message") unless $msg;
$self->{"msg"} = ldap_next_message($self->{"ld"}, $msg);
return $self->{"msg"};
} # end of next_message
# using this function you don't have to call fist_message and next_message
# here is an example:
#
# print "message = $message\n" while( $msg = $ld->result_message );
#
sub result_message
{
my ($self, @args) = @_;
my ($result) = $self->rearrange(['RESULT'], @args);
$result = $self->{"result"} unless $result;
croak("No Current Result") unless $result;
if( $self->{"msg"} == 0 ) {
$self->{"msg"} = ldap_first_message($self->{"ld"}, $self->{"result"});
} else {
$self->{"msg"} = ldap_next_message($self->{"ld"}, $self->{"msg"});
}
return $self->{"msg"};
} # end of result_message
sub next_changed_entries {
my ($self, @args) = @_;
my ($msgid, $allnone, $timeout) =
$self->rearrange(['MSGID', 'ALL', 'TIMEOUT'], @args);
my ($rc, $msg, $msgtype, $asn, $syncInfoValue,
$syncInfoValues, $refreshPresent, $ctrl, $oid, %parsed,
$retdatap, $retoidp, @entries, $syncStateValue, $syncStateValues,
$state, $berval, $cookie);
$rc = $self->result($msgid, $allnone, $timeout);
@entries = ();
if ($self->{'status'} == 0) { # ldap_result return 0 = timeout
return @entries;
}
$asn = $self->{"asn"};
while( $msg = $self->result_message ) {
$msgtype = $self->msgtype($msg);
if( $msgtype eq $self->LDAP_RES_SEARCH_ENTRY ) {
my %entr = ('entry' => $msg);
push(@entries, \%entr);
$self->{"entry"} = $msg;
# extract controls if any
my @sctrls = $self->get_entry_controls($msg);
foreach $ctrl (@sctrls) {
$oid = $self->get_control_oid($ctrl);
if( $oid eq $self->LDAP_CONTROL_SYNC_STATE ) {
$berval = $self->get_control_berval($ctrl);
$syncStateValue = $asn->find('syncStateValue');
$syncStateValues = $syncStateValue->decode($berval);
$state = $syncStateValues->{'state'};
if( $state == 0 ) {
$entr{'state'} = "present";
} elsif( $state == 1 ) {
$entr{'state'} = "add";
} elsif( $state == 2 ) {
$entr{'state'} = "modify";
} elsif( $state == 3 ) {
$entr{'state'} = "delete";
} else {
$entr{'state'} = "unknown";
}
}
$cookie = $syncStateValues->{'cookie'};
if( $cookie ) {
# save the cookie
save_cookie($cookie, $self->{"cookie"});
}
ldap_control_free($ctrl);
}
} elsif( $msgtype eq $self->LDAP_RES_INTERMEDIATE ) {
%parsed = $self->parse_intermediate($msg);
$retdatap = $parsed{'retdatap'};
$retoidp = $parsed{'retoidp'};
if( $retoidp eq $self->LDAP_SYNC_INFO ) {
my $cookie;
$asn->configure(encoding => "DER");
$syncInfoValue = $asn->find('syncInfoValue');
$syncInfoValues = $syncInfoValue->decode($retdatap);
# trying to get the cookie from one of the foolowing choices.
$cookie = $syncInfoValues->{'newcookie'};
my $refreshPresent = $syncInfoValues->{'refreshPresent'};
$cookie = $refreshPresent->{'cookie'} if( $refreshPresent );
my $refreshDelete = $syncInfoValues->{'refreshDelete'};
$cookie = $refreshDelete->{'cookie'} if( $refreshDelete );
my $syncIdSet = $syncInfoValues->{'syncIdSet'};
$cookie = $syncIdSet->{'cookie'} if( $syncIdSet );
$asn->configure(encoding => "BER");
# see if we got any and save it.
if( $cookie ) {
save_cookie($cookie, $self->{"cookie"});
}
}
}
}
return @entries;
} # next_changed_entries
sub save_cookie
{
my ($self,@args) = @_;
my $cookiestr = $_[0];
my $cookie = $_[1];
# Skip all if there's no csn value
if ($cookiestr =~ m/csn=/) {
# Get new CSN array and a copy
chomp(my @newcsns = split(';',$cookiestr =~ s/(rid=\d{3},)|(sid=\d{3},)|(csn=)//rg));
# These will be the CSNs to write to the cookie file
# All CSNs from the new cookie must be used
# my @outcsns = @newcsns;
my @outcsns = @newcsns;
# Get the old cookie for comparison/persisting
if (-w $cookie) {
open(COOKIE_FILE, "<", $cookie) || die("Cannot open file '".$cookie."' for reading.");
chomp(my @oldcsns = <COOKIE_FILE>);
close(COOKIE_FILE);
# Look for old CSNs with SIDs that don't match any of the new
# CSNs. If there are no matches, push the old CSN to the list
# of CSNs to be written to the cookie file.
foreach my $oldcsn (@oldcsns) {
my $match = 0;
my $p_sid = ($oldcsn =~ /(#\d{3}#)/i)[0];
foreach my $newcsn (@newcsns) {
if ($newcsn =~ m/\Q$p_sid/) {
$match = 1;
last;
}
}
if (!$match) { push @outcsns,$oldcsn; }
}
}
# Write the cookie
open(COOKIE_FILE, ">", $cookie) || die("Cannot open file '".$cookie."' for writing.");
print COOKIE_FILE "$_\n" for @outcsns;
close(COOKIE_FILE);
}
} # end save_cookie
sub first_entry
{
my ($self) = @_;
croak("No Current Result") if ($self->{"result"} == 0);
$self->{"entry"} = ldap_first_entry($self->{"ld"}, $self->{"result"});
return $self->{"entry"};
} # end of first_entry
sub next_entry
{
my ($self) = @_;
croak("No Current Entry") if ($self->{"entry"} == 0);
$self->{"entry"} = ldap_next_entry($self->{"ld"}, $self->{"entry"});
return $self->{"entry"};
} # end of next_entry
# using this function you don't have to call fist_entry and next_entry
# here is an example: