Ldap Query

Just starting out? Need help? Post your questions and find answers here.
User avatar
Ajm
Enthusiast
Enthusiast
Posts: 242
Joined: Fri Apr 25, 2003 9:27 pm
Location: Kent, UK

Ldap Query

Post by Ajm »

Hi,

I have searched this forum with regards ldap and found 1 post.

While the code contained within the post works for counting the number of entries at the required level I am having trouble retrieving values.

I'm not sure I fully understand what i'm doing :oops:


Code: Select all

lpres.l 
dn$ = "cn=Manager, dc=xxxxxx, dc=xxxx" 
base$ = "DC=xx, DC=xxxx" 
cred$ = "secret" 
;user$ = dn$ 
;password$ = cred$ 
user$ = "xxxxxxxx"
password$ = "xxxxxx"
filter$ = "(objectClass=*)" 

#LDAP_SUCCESS = 0 
#LDAP_OPT_VERSION = $11 
#LDAP_VERSIONV3 = 3
#LDAP_SCOPE_BASE = $00
#LDAP_SCOPE_ONELEVEL = $01
#LDAP_SCOPE_SUBTREE = $02
#LDAP_MSG_ONE = $00 
#LDAP_PORT = 389 
msgid = 0 
timeout.timeval 
timeout\tv_sec = 0 
timeout\tv_usec = 0 
Structure LDAP 
  ld_deref.l 
  ld_timelimit.l 
  ld_sizelimit.l 
  ld_errno.l 
  ld_matched.s 
  ld_error.s 
EndStructure 

Procedure.s LDAPError(rt) 
  If rt=-1 
    errcode = CallCFunction(0, "LdapGetLastError") 
  Else 
    errcode = rt 
  EndIf 
  ProcedureReturn PeekS(CallCFunction(0, "ldap_err2string", errcode)) 
EndProcedure 

; 
; Initialize Libraries 
; 
OpenLibrary(0, "wldap32.dll") 
OpenConsole() 
; 
; LDAP Connect... 
; 
*ld.LDAP= CallCFunction(0, "ldap_init", "10.1.0.40", #LDAP_PORT) 
PrintN("ldap_init: $"+Hex(*ld)) 

If *ld=0:PrintN("Error1: "+LDAPError(*ld)):Goto Fin:EndIf 

;rt = CallCFunction(0, "ldap_set_option", *ld, #LDAP_OPT_VERSION, #LDAP_VERSIONV3) 
;PrintN("ldap_set_option: $"+Hex(rt)) 
;If rt<>#LDAP_SUCCESS:PrintN("Error: "+LDAPError(rt)):Goto Fin:EndIf 

rt = CallCFunction(0, "ldap_connect", *ld, 0);@timeout) 
PrintN("ldap_connect: $"+Hex(rt)) 

If rt<>#LDAP_SUCCESS:PrintN("Error2: "+LDAPError(rt)):Goto Fin:EndIf 

rt = CallCFunction(0, "ldap_simple_bindA", *ld, user$, password$) 
PrintN("ldap_simple_bind: $"+Hex(rt)) 

If rt=-1:PrintN("Error3: "+LDAPError(rt)):Goto Fin:EndIf 

rt = CallCFunction(0, "ldap_search_sA", *ld, base$, #LDAP_SCOPE_ONELEVEL, filter$, #Null, 0, @lpres) 
PrintN("ldap_search_s: $"+ Hex(rt)) 

If rt<>#LDAP_SUCCESS:PrintN("Error4: "+LDAPError(rt)):Goto Fin:EndIf 
PrintN("ldap_search_s Res: "+ Str(lpres)) 

rt = CallFunction(0,"ldap_first_entryA",*ld, lpres)
PrintN("ldap_first_entry: "+ Str(rt))

If rt<>0:PrintN("Error5 "+LDAPError(rt)):Goto Fin:EndIf 

rt = CallFunction(0,"ldap_get_valuesA",*ld, rt, "distinguishedName")
PrintN("ldap_get_values: "+Str(rt)) 

If rt='':PrintN("Error6"+LDAPError(rt)):Goto Fin:EndIf 

rt = CallCFunction(0, "ldap_count_entries", *ld, lpres) 
PrintN("ldap_count_entries: "+Str(rt)) 

If rt=-1:PrintN("Error7"+LDAPError(rt)):Goto Fin:EndIf 
; 
; Close All and End. 
; 
Fin: 
PrintN( "- End. ---ENTER to quit." ) 
key$=Input()
If lpres <> '':rt = CallCFunction(0, "ldap_msgfree", lpres):EndIf 
rt = CallCFunction(0, "ldap_unbind", *ld) 
CloseLibrary(0) 
CloseConsole()
I am unsure how to use the CallCFunction when it comes to returning results other than a numeric.

The code fails at Error 6.

Could anyone help me please.
Regards

Andy

Image
Registered PB & PureVision User
Fred
Administrator
Administrator
Posts: 18541
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

You have to use PeekS(rt) if rt isn't NULL and it should get the string back.
User avatar
Ajm
Enthusiast
Enthusiast
Posts: 242
Joined: Fri Apr 25, 2003 9:27 pm
Location: Kent, UK

Post by Ajm »

Fred,

Is it possible you could give me a working sample, I am still unable to get this to work.


Code: Select all

lpres.l
;rt.l 
dn$ = "cn=Manager, dc=xxxxxx, dc=xxxx" 
base$ = "DC=ad, DC=moat" 
cred$ = "secret" 
;user$ = dn$ 
;password$ = cred$ 
user$ = "xxxx"
password$ = "xxxx"
filter$ = "(objectClass=*)" 
;filter$ = "(sAMAccountName=MarshA)" 

#LDAP_SUCCESS = 0 
#LDAP_OPT_VERSION = $11 
#LDAP_VERSIONV3 = 3
#LDAP_SCOPE_BASE = $00
#LDAP_SCOPE_ONELEVEL = $01
#LDAP_SCOPE_SUBTREE = $02
#LDAP_MSG_ONE = $00 
#LDAP_PORT = 389 
msgid = 0 
timeout.timeval 
timeout\tv_sec = 0 
timeout\tv_usec = 0 
Structure LDAP 
  ld_deref.l 
  ld_timelimit.l 
  ld_sizelimit.l 
  ld_errno.l 
  ld_matched.s 
  ld_error.s 
EndStructure 

Procedure.s LDAPError(rt) 
  If rt=-1 
    errcode = CallCFunction(0, "LdapGetLastError") 
  Else 
    errcode = rt 
  EndIf 
  ProcedureReturn PeekS(CallCFunction(0, "ldap_err2string", errcode)) 
EndProcedure 

; 
; Initialize Libraries 
; 
OpenLibrary(0, "wldap32.dll") 
OpenConsole() 
; 
; LDAP Connect... 
; 
*ld.LDAP= CallCFunction(0, "ldap_init", "10.1.0.40", #LDAP_PORT) 
PrintN("ldap_init: $"+Hex(*ld)) 

If *ld=0:PrintN("Error1: "+LDAPError(*ld)):Goto Fin:EndIf 

;rt = CallCFunction(0, "ldap_set_option", *ld, #LDAP_OPT_VERSION, #LDAP_VERSIONV3) 
;PrintN("ldap_set_option: $"+Hex(rt)) 
;If rt<>#LDAP_SUCCESS:PrintN("Error: "+LDAPError(rt)):Goto Fin:EndIf 

rt = CallCFunction(0, "ldap_connect", *ld, 0);@timeout) 
PrintN("ldap_connect: $"+Hex(rt)) 

If rt<>#LDAP_SUCCESS:PrintN("Error2: "+LDAPError(rt)):Goto Fin:EndIf 

rt = CallCFunction(0, "ldap_simple_bindA", *ld, user$, password$) 
PrintN("ldap_simple_bind: $"+Hex(rt)) 

If rt=-1:PrintN("Error3: "+LDAPError(rt)):Goto Fin:EndIf 

rt = CallCFunction(0, "ldap_search_sA", *ld, base$, #LDAP_SCOPE_BASE, filter$, #Null, 0, @lpres) 
PrintN("ldap_search_s: $"+ Hex(rt)) 

PrintN("ldap_search_s Res: "+ Str(lpres)) 
PrintN("ldap_search_s: "+ Str(rt)) 

If rt <> #LDAP_SUCCESS:PrintN("Error4: "+LDAPError(rt)):Goto Fin:EndIf 

*fe = CallFunction(0,"ldap_first_entryA",*ld, lpres)
PrintN("ldap_first_entry: "+Str(*fe)) 
If *fe <> #Null
   PrintN("ldap_first_entry: "+Str(*fe)) 
Else
   PrintN("Error5 fe is Null"):Goto Fin
EndIf

rt = CallFunction(0,"ldap_get_valuesA",*ld, *fe, "distinguishedName")
If rt <> #Null
   PrintN("ldap_get_values: "+PeekS(rt)) 
Else
   PrintN("Error6 rt is Null"):Goto Fin
EndIf

rt = CallCFunction(0, "ldap_count_entries", *ld, lpres) 
PrintN("ldap_count_entries: "+Str(rt)) 

If rt=-1:PrintN("Error7"+LDAPError(rt)):Goto Fin:EndIf 
; 
; Close All and End. 
; 
Fin: 
PrintN( "- End. ---ENTER to quit." ) 
key$=Input()
If lpres <> '':rt = CallCFunction(0, "ldap_msgfree", lpres):EndIf 
rt = CallCFunction(0, "ldap_unbind", *ld) 
CloseLibrary(0) 
CloseConsole()
Regards

Andy

Image
Registered PB & PureVision User
TeddyLM
Enthusiast
Enthusiast
Posts: 133
Joined: Wed Apr 30, 2003 2:04 pm
Location: Germany (French expat)

Post by TeddyLM »

Hi Ajm

Thank you for sharing.
I have the same problem... I can't get this to work.
'ldap_count_entries' returns values other than -1 but 'ldap_first_entryA' still returns 0 (pointer).
Any ideas... anybody?

Ajm wrote:
I'm not sure I fully understand what i'm doing
same problem here :D

-where did you find the LDAP structure? MSDN says it's an opaque data type (same as LDAPMESSAGE structure)-

Code: Select all

#LDAP_PORT = 389
#LDAP_MSG_ONE = $00 ;retrieves one message at a time
#LDAP_MSG_ALL = $01 ;requests that all results of a search be received before returning all results in a single chain
#LDAP_MSG_RECEIVED = $02 ;indicates that all results retrieved so far should be returned in the result chain 
#LDAP_SCOPE_BASE = 0 ;Searches the base entry only. 
#LDAP_SCOPE_ONELEVEL = 1 ;Searches all entries in the first level below the base entry, excluding the base entry. 
#LDAP_SCOPE_SUBTREE = 2 ;Searches the base entry And all entries in the tree below the base.
#LDAP_SUCCESS = $00 ;The call completed successfully. 

Structure LDAP 
  ld_deref.l 
  ld_timelimit.l 
  ld_sizelimit.l 
  ld_errno.l 
  ld_matched.s 
  ld_error.s 
EndStructure 
    
lpres.l = 0
Domainname$ = "xxxxx"
base$ = "dc=xxxxx,dc=de" 
user$ = "xxxxx" 
password$ = "xxxxx" 
filter$ = "(objectClass=*)"  

If OpenLibrary(0, "wldap32.dll") 
    Debug "wldap32.dll Library found"
    *ld.LDAP = CallCFunction(0, "ldap_init", Domainname$, #LDAP_PORT) 
    If *ld 
        Debug "ldap_init successfully called"
        rt = CallCFunction(0, "ldap_connect", *ld, 0) 
        If rt = #LDAP_SUCCESS
            Debug "ldap_connect successfully called" 
            rt = CallCFunction(0, "ldap_simple_bindA", *ld, user$, password$)
            If rt <> -1
                Debug "ldap_simple_bind successfully called"                                
                rt = CallCFunction(0, "ldap_search_sA", *ld, base$, #LDAP_SCOPE_BASE, filter$, #Null, 0, @lpres)
                If rt = #LDAP_SUCCESS
                    Debug "ldap_search_s successfully called"                                     
                    rt = CallCFunction(0, "ldap_count_entries", *ld, lpres)  ;returns -1 (not successfull) or the number of entries
                    Debug "ldap_count_entries: "+Str(rt)
                    If rt <> -1                        
                        *fe = CallCFunction(0,"ldap_first_entryA",*ld, *rt) 
                        ;***
                        Debug *fe     ;always 0 ??
                        ;***                       
                    EndIf
                Else
                    Debug "Unable to search. $" + Hex(CallCFunction(0, "LdapGetLastError"))
                EndIf                                 
            Else
                Debug "Unable to bind. $" + Hex(CallCFunction(0, "LdapGetLastError"))
            EndIf
        Else
            Debug "Unable to connect. $" + Hex(CallCFunction(0, "LdapGetLastError"))
        EndIf
        CallCFunction(0, "ldap_unbind", *ld)
    Else
        Debug "Unable to call ldap_init. $" + Hex(CallCFunction(0, "LdapGetLastError"))  
    EndIf   
    CloseLibrary(0)
Else
    Debug "Unable to open the library. $" + Hex(CallCFunction(0, "LdapGetLastError"))
EndIf
End
User avatar
Shardik
Addict
Addict
Posts: 2079
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Ldap Query

Post by Shardik »

In TeddyLM's example code the following lines

Code: Select all

...
            rt = CallCFunction(0, "ldap_simple_bindA", *ld, user$, password$)
...
                rt = CallCFunction(0, "ldap_search_sA", *ld, base$, #LDAP_SCOPE_BASE, filter$, #Null, 0, @lpres)
...
                        *fe = CallCFunction(0,"ldap_first_entryA",*ld, *rt)
...
have to be modified in order for the example to work (Strings as parameters have to be prefixed with @, the A in ldap_first_entryA has to be removed and *rt has to be replaced by lpres):

Code: Select all

...
            rt = CallCFunction(0, "ldap_simple_bindA", *ld, @user$, @password$)
...
                rt = CallCFunction(0, "ldap_search_sA", *ld, @base$, #LDAP_SCOPE_BASE, @filter$, #Null, 0, @lpres)
...
                        *fe = CallCFunction(0,"ldap_first_entry",*ld, lpres)
...
A further hint is to use ImportC "Wldap32.Lib" to import the required functions from Wldap32.dll instead of using CallCFunction(). Wldap32.dll is normally contained in the Windows\System32 directory on Windows XP SP3 and Windows 7 x86 SP1, but to obtain Wldap32.Lib I had to extract this file from the Windows Platform SDK February 2003.
Post Reply