Sign in to follow this  
Followers 0
Anders Westerberg

generic ldap function but a bit slow

1 post in this topic

#1 ·  Posted (edited)

Hi. I'v been using AutoIt for a couple of years now but this is my first post here so please inform me of any errors I make.

I have written a small ldap function that I use in a couple of scripts but it is very slow when querying large number of objects/attributes. An example, doing a query that results in 500 objects and 15 attributes on each object will take over a minute. The actual ldap query in this case only takes about 3 seconds.

I do not claim to be a programmer in any way so if anyone could help me speed this up I would be very grateful!

Oh, and about the function. It works.. almost.. I have some issues with microsofts implementation of ldapsearch. I can not get values from some attributes, "cn" does not work, "groupmembership" does not work.. don't know why but don't really need them anyway.

If I want to check if a user is a member of a specific group I just do a search like this (when searching a novell ldap tree, would be different group attribute in openldap):

ldapsearch("(&(objectclass=inetorgperson)(uid=username)(groupmembership=cn=groupname,ou=groupcontainer,o=base)

)","fullname",ldapserver,basecontext) if it returns "" then the user is not a member of that group. Otherwise it will return an array.

usage, ldapsearch(ldapfiler,attributestoreturn,ldapserver,basecontext) if the query matches any objects it will return an array with the number of objects that matched in array[0] and then the DN of the first match and the attributes.. then the second DN that matches and the attributes.. and so on..

As I'm editing this i realize just how bad I am at explaining how this works.. :whistle:

edit.. cant get the code below to show tabs.. seems a bit garbled..

CODE

func ldapsearch($ldapfilter,$attributes,$ldapserver,$basecontext)

Local $objConnection,$objRecordSet,$i,$j,$temp,$dn

Local $attrarray,$tempstring,$attributecount

$attributecount=stringsplit($attributes,",")

$objConnection = ObjCreate("ADODB.Connection")

$objConnection.Provider = "ADSDSOObject"

$objConnection.open = "ADs Provider"

$objRecordSet = $objConnection.Execute("<LDAP://" & $ldapserver & "/" & $basecontext & ">;" & $ldapfilter & ";ADsPath," & $attributes & ";subtree")

if $objRecordSet.EOF then return ""

$attrarray=_ArrayCreate(0)

while not ($objRecordSet.EOF)

$dn=$objRecordSet.fields(0).value

$dn=StringTrimLeft($dn,stringinstr($dn,"/",0,-1))

_ArrayAdd($attrarray,$dn)

for $i=1 to $attributecount[0]

$tempstring=""

$temp=$objRecordSet.fields($i).value

if IsArray($temp)=1 then ;if the attribute is a multistringattribute then microsofts ldapsearch will return an array

if UBound($temp)>1 Then

for $j=0 to (UBound($temp)-1)

$tempstring&=$temp[$j] & ", "

Next

$tempstring=stringtrimright($tempstring,2)

Else

$tempstring&=$temp[0]

EndIf

_ArrayAdd($attrarray,$tempstring)

Else

_ArrayAdd($attrarray,$temp)

EndIf

Next

$objRecordSet.MoveNext

WEnd

$attrarray[0]=(UBound($attrarray)-1) / ($attributecount[0]+1)

return $attrarray

EndFunc

Edited by Anders Westerberg

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0