Skip to content

Instantly share code, notes, and snippets.

@FROGGS
Created March 13, 2018 16:08
Show Gist options
  • Save FROGGS/bafc8454a14f80a6fddd170048dabf47 to your computer and use it in GitHub Desktop.
Save FROGGS/bafc8454a14f80a6fddd170048dabf47 to your computer and use it in GitHub Desktop.
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