Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
p6-Net-LDAP
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> { * };
perl6 -Ilib -MNet::LDAP -e 'Net::LDAP.new(:server(<1.2.3.4 1.2.3.5>)).bind(:dn<foo@bar.baz>, :password<flubber>)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment