Guest User

Untitled

a guest
Mar 22nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. use v6;
  2. unit class Net::LDAP;
  3. use NativeCall;
  4.  
  5. enum LDAP_OPT_X_TLS <NEVER HARD DEMAND ALLOW TRY>;
  6.  
  7. constant LDAP_SUCCESS = 0;
  8. constant LDAP_VERSION3 = 3;
  9. constant LDAP_OPT_PROTOCOL_VERSION = 0x0011;
  10. constant LDAP_OPT_X_TLS_REQUIRE_CERT = 0x6006;
  11. constant LDAP_AUTH_SIMPLE = 0x80;
  12.  
  13. has Pointer $!ld = Pointer.new;
  14. has @.server = ['127.0.0.1'];
  15. has Int $.version = LDAP_VERSION3;
  16.  
  17. submethod TWEAK {
  18. my $uri = @!server.map(-> $ip { 'ldap://' ~ $ip ~ ':389' }).join(' ');
  19.  
  20. if ((my $rc = ldap_initialize($!ld, $uri)) != LDAP_SUCCESS) {
  21. fail "ldap init error: ", ldap_err2string($rc);
  22. }
  23.  
  24. ldap_set_option($!ld, LDAP_OPT_PROTOCOL_VERSION, CArray[int32].new($!version));
  25. ldap_set_option($!ld, LDAP_OPT_X_TLS_REQUIRE_CERT, CArray[int32].new(LDAP_OPT_X_TLS::NEVER));
  26. }
  27.  
  28. method bind(:$dn, :$password) {
  29. if ((my $rc = ldap_bind_s($!ld, $dn, $password, LDAP_AUTH_SIMPLE)) != LDAP_SUCCESS) {
  30. fail "ldap bind error: ", ldap_err2string($rc);
  31. }
  32.  
  33. True;
  34. }
  35.  
  36. method unbind() {
  37. ldap_unbind($!ld);
  38. }
  39.  
  40. sub ldap_initialize(Pointer $ld is rw, Str $uri) returns int32 is native<ldap> { * }
  41. sub ldap_set_option(Pointer $ld, int32 $option, CArray[int32] $outvalue) returns int32 is native<ldap> { * };
  42. sub ldap_bind_s(Pointer $ld, Str $who, Str $cred, int32 $method) returns int32 is native<ldap> { * };
  43. sub ldap_unbind(Pointer $ld) returns int32 is native<ldap> { * };
  44. sub ldap_err2string(int32 $errnum) returns Str is native<ldap> { * };
Add Comment
Please, Sign In to add comment