ich habe die Aufgabe gehabt, einen Abgleich mit LDAP zu realisieren. Ich dachte, hier kann evtl. jemand mit dem funktionsfähigen Code was anfangen.
Voraussetzungen:
- LDAPSDK.DLL von Novell (zB hier: http://developer.novell.com/wiki/index.php/Cldap)
- erreichbarer LDAP-Server (zB OpenLDAP oder OpenDS)
- zum testen ein LDAP Browser (zB http://jxplorer.org/)
Tipp:
Bei der Installation des LDAP SDK von Novell kann es zum Fehler 3 kommen. Dann das Setup mit dem LAX_VM Parameter starten und den kompletten Pfad zu deiner java.exe angeben:
novell-cldap-devel-2009.10.06-1netware_windows.exe LAX_VM "complete path for java.exe on your system"
Hier nun der Code. Beispielhaft liest dieser alle Personen aus und gibt im Debug-Fenster deren Eigenschaften und Attribute aus.
Code: Alles auswählen
; LDAP Test
;
; PureBasic V4.50
;
; Volker Schmid / Sept. 2010
#LDAP_SCOPE_BASE = $00
#LDAP_SCOPE_ONELEVEL = $01
#LDAP_SCOPE_SUBTREE = $02
#LDAP_NO_LIMIT=0
#LDAP_OPT_PROTOCOL_VERSION = $11
#LDAP_VERSION3 = $03
#Null=0
#LDAP_Library = 32; adapt for your needs and avoid conflicts with other dll's
LDAP_Host.s = "xpldap"
LDAP_Port.l = 389
LDAP_Username.s = "cn=Directory Manager"
LDAP_Password.s = "1234"
LDAP_Filter.s = "(&(objectClass=person))" ; get all persons
LDAP_DN.s = "dc=example,dc=com"
; ------------- open library ------------
If OpenLibrary(#LDAP_Library,"LDAPSDK.DLL") = 0
Debug "can not open ldapsdk.dll"
End
EndIf
; ------------- init library ------------
*ld.l = CallFunction(#LDAP_Library, "ldap_init", @LDAP_Host.s, LDAP_Port.l)
If *ld.l < 1
Debug "error while init host"
End
EndIf
; ------------- set connection protocol version ------------
Value.l = #LDAP_VERSION3
Ret.l = CallFunction(#LDAP_Library, "ldap_set_option",*ld.l, #LDAP_OPT_PROTOCOL_VERSION, @Value.l)
Debug "ldap_set_option: $"+Hex(Ret.l)
If Ret.l <> 0
Debug "error while setting option"
End
EndIf
; ------------- connect ------------
Ret.l = CallFunction(#LDAP_Library, "ldap_simple_bind_s",*ld.l, @LDAP_Username.s, @LDAP_Password.s)
Debug "ldap_simple_bind: $"+Hex(Ret.l)
If Ret.l <> 0
Debug "error while init host"
End
EndIf
; ------------- query ------------
Result.l = 0
Ret.l = 0
Ret.l = CallFunction(#LDAP_Library, "ldap_search_s", *ld.l, @LDAP_DN.s, #LDAP_SCOPE_SUBTREE, @LDAP_Filter.s,#Null, #Null, @Result.l)
Debug "ldap_search_s: $"+Hex(Ret.l)
Debug "Result.l (pointer): " + Str(Result.l)
Entries.l = CallFunction(#LDAP_Library, "ldap_count_entries", *ld.l, Result.l)
Debug "Entries: " + Str(Entries.l)
If Entries.l < 1
Debug "no result (nothing found)"
End
EndIf
; ------------- get results ------------
; get first entry
EntryPointer.l = CallFunction(#LDAP_Library,"ldap_first_entry",*ld.l, Result.l)
Debug "ldap_first_entry (pointer): $"+Hex(EntryPointer.l)
While EntryPointer.l <> 0
; loop all entries
Ret.l = CallFunction(#LDAP_Library, "ldap_get_dn", *ld.l, EntryPointer.l)
Debug "Found entry. dn: " +PeekS(Ret.l)
; get BER and first attribute
Ptr.l = 0
Attr.l = CallFunction(#LDAP_Library,"ldap_first_attribute", *ld.l, EntryPointer.l, @Ptr.l)
While Attr.l <> 0
; loop all attributes
AttrName.s = PeekS(Attr.l)
Debug " Attribute: " + AttrName.s
Values.l = CallFunction(#LDAP_Library, "ldap_get_values", *ld.l, EntryPointer.l, @AttrName.s)
Adder.l = 0
Repeat
; loop all values
Pointer.l = PeekL(Values.l + Adder.l)
If Pointer.l <> 0: Debug " Wert: " + PeekS(Pointer.l): EndIf
Adder.l = Adder.l + 4
Until Pointer.l = 0
Attr.l = CallFunction(#LDAP_Library,"ldap_next_attribute", *ld.l, EntryPointer.l, Ptr.l)
Wend
; free BER
Ret.l = CallFunction(#LDAP_Library, "ber_free", Ptr.l, 0)
EntryPointer.l = CallFunction(#LDAP_Library,"ldap_next_entry",*ld.l, EntryPointer.l)
Wend
; ------------- close library ------------
Ret.l = CallFunction(#LDAP_Library, "ldap_unbind", *ld.l)
CloseLibrary(#LDAP_Library)
- Die Rückgaben erfolgen möglicherweise UTF-8 kodiert. Tipp: PeekS() kann das zurückkonvertieren.
- Ohne Debug-Modus klappt hier nix, weil alle Ausgaben dort erscheinen. Das muss man selbst anpassen.
- Ich hab keinen Plan, ob man für x64 oder Unicode was anpassen muss.
Grüße,
Volker