Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use v6;
- unit class Net::LDAP;
- use NativeCall;
- enum LDAP_OPT_X_TLS <NEVER HARD DEMAND ALLOW TRY>;
- constant LDAP_SUCCESS = 0;
- constant LDAP_VERSION3 = 3;
- constant LDAP_OPT_PROTOCOL_VERSION = 0x0011;
- constant LDAP_OPT_X_TLS_REQUIRE_CERT = 0x6006;
- constant LDAP_AUTH_SIMPLE = 0x80;
- has Pointer $!ld = Pointer.new;
- has @.server = ['127.0.0.1'];
- has Int $.version = LDAP_VERSION3;
- submethod TWEAK {
- my $uri = @!server.map(-> $ip { 'ldap://' ~ $ip ~ ':389' }).join(' ');
- if ((my $rc = ldap_initialize($!ld, $uri)) != LDAP_SUCCESS) {
- fail "ldap init error: ", ldap_err2string($rc);
- }
- ldap_set_option($!ld, LDAP_OPT_PROTOCOL_VERSION, CArray[int32].new($!version));
- ldap_set_option($!ld, LDAP_OPT_X_TLS_REQUIRE_CERT, CArray[int32].new(LDAP_OPT_X_TLS::NEVER));
- }
- method bind(:$dn, :$password) {
- if ((my $rc = ldap_bind_s($!ld, $dn, $password, LDAP_AUTH_SIMPLE)) != LDAP_SUCCESS) {
- fail "ldap bind error: ", ldap_err2string($rc);
- }
- True;
- }
- method unbind() {
- ldap_unbind($!ld);
- }
- sub ldap_initialize(Pointer $ld is rw, Str $uri) returns int32 is native<ldap> { * }
- sub ldap_set_option(Pointer $ld, int32 $option, CArray[int32] $outvalue) returns int32 is native<ldap> { * };
- sub ldap_bind_s(Pointer $ld, Str $who, Str $cred, int32 $method) returns int32 is native<ldap> { * };
- sub ldap_unbind(Pointer $ld) returns int32 is native<ldap> { * };
- sub ldap_err2string(int32 $errnum) returns Str is native<ldap> { * };
Add Comment
Please, Sign In to add comment