Sunday, January 18, 2009

Powerbuilder authenticate Active Directory/LDAP

Looks like there isn't much out there on this. Most use a third party dll/ole, some even go as far to say it can't be done.. This here is pure PB that makes a single API call.


type guid from structure
long data1
integer data2
integer data3
character data4[4]
end type

type prototypes
function long ADsOpenObject(string path, string userName, string password, long flags, ref GUID iid, ref long ppObject) library "activeds.dll"
end prototypes

public function long of_is_authenticated (string as_domain, string as_user_name, string as_password)
/*
Authenticates against Active directory
as_domain = Domain to connect to. exp. "yourplace.com"
as_user_name = User name to authenticate.
as_password = Password for user.

Returns Values:
0 = Authenticated
-1 = Unknown username or bad password
-2 = Password expired
-3 = Account disabled
-4 = Domain not found
-5 = Logon ID already in use
-6 = Unknown Error
*/

GUID ls_iid
//oleobject la_pp //sometimes this causes an issue..
long la_pp
long ll_ret


// IUnknown interface = 00000000-0000-0000-C000-000000000046
ls_iid.data4[1] = char(192)
ls_iid.data4[4] = char(17920) //LittleEndian

ll_ret = ADsOpenObject("LDAP://" + as_domain, as_user_name, as_password, 1, ref ls_iid, ref la_pp)

//la_pp.disconnectobject( ) //How do we clean up?
//destroy la_pp

Choose Case ll_ret
case 0
//Success
case -2147023570
//Unknown username or bad password
ll_ret = -1
case -2147943730
//Password expired
ll_ret = -2
case -2147943731
//Account disabled
ll_ret = -3
case -2147943755
//Domain not found
ll_ret = -4
case -2147943763
//Logon ID already in use
ll_ret = -5
case else
//Unknown Error
ll_ret = -6
End Choose

return ll_ret
end function


Tada..

This is compatiable with PB 10. In the newer versions of PB it is easier to use the byte datatype with the guid instead of the backwards chars..

There are a few more error codes i only took the common ones. Take a look here on how to find the numerous others: http://msdn.microsoft.com/en-us/library/aa746386(VS.85).aspx

[UPDATE:]
I've run into an issue with powerbuilder 11.5 using this code..it doesn't seem to like the oleobject..any ideas are welcome..