0

I need a function in C or C++ (actually I need in Ada 95, but a pragma import can be used with no problem - I must not use the -gnat05 switch) to check if a user is present in a LDAP network group.

For getting the username, I have the function GetEnv in C, which I can import in Ada 95 to:

function GetUsername return String is
   function GetEnv (Variable : String) return Interfaces.C.Strings.chars_ptr;
   pragma Import (C, GetEnv, "getenv");

   Command : constant String := "USER" & ASCII.Nul;
   Answer_Ptr : constant Interfaces.C.Strings.chars_ptr := GetEnv (Command);
   Answer : constant String := Interfaces.C.Strings.Value (Answer_Ptr);
begin
   return Answer;
end GetUsername;

So I need a function Boolean Check_LDAP_Authentication (char* Username) or something like this in C or C++, (or even Check_LDAP_Authentication (Username : String) return Boolean in Ada). How can I do it?

Thanks in advance.

Update

I found a post on How to write LDAP query to test if user is member of a group?, which express quite well (using C#/VB.Net and System.DirectoryServices) what I need to do, just that I need an Ada 95 equivalent.

DirectoryEntry rootEntry = new DirectoryEntry("LDAP://dc=yourcompany,dc=com");

DirectorySearcher srch = new DirectorySearcher(rootEntry);
srch.SearchScope = SearchScope.Subtree;

srch.Filter = "(&(objectcategory=user)(sAMAccountName=yourusername)(memberof=CN=yourgroup,OU=yourOU,DC=yourcompany,DC=com))";

SearchResultCollection res = srch.FindAll();

if(res == null || res.Count <= 0)
{
    Console.WriteLine("This user is *NOT* member of that group");
}
else
{
    Console.WriteLine("This user is INDEED a member of that group");
}
Community
  • 1
  • 1
Rego
  • 1,118
  • 1
  • 18
  • 40

2 Answers2

0

For what I understood, you'll need several LDAP calls. Why don't you write only a very thin binding in Ada95 to link with OpenLDAP ? Or directly a C code inspired from this small tutorial (but with the current OpenLDAP API) and call it from Ada ?

For the first solution, I think you will need to call

It's not as straightforward as using an existing Ada library but that should do the trick.

Hope it helps

Frédéric Praca
  • 1,620
  • 15
  • 29
0

First, your Command variable should be of type chars_ptr, too, and should contain a \0 as end. If it worked for you, you just were lucky. Make sure to free the chars_ptr afterwards. See http://www.dwheeler.com/lovelace/s16s2.htm for an example.

There is a LDAP binding for Ada: http://savannah.nongnu.org/projects/adaldap/ - but it seems to be very inactive.

AWS supports LDAP, too. See here for an example: http://www.adacore.com/wp-content/files/auto_update/aws-docs/aws.html#LDAP

Rommudoh
  • 1,844
  • 10
  • 11
  • I don't believe luck has something to it, just analyse why it works. I may look around savanna and AWS code, maybe I get some hint. Include the hole library is not a good idea (plant a tree to get a seed, right?), but look around them could help. Thank you. – Rego Feb 16 '12 at 20:34
  • Running your code on Mac OS X Lion, with the appropriate environment variable (`USER` rather than `USERNAME`), results in a `Dereference_Error`. – Simon Wright Feb 17 '12 at 07:10
  • ... but `Command : constant String := "USER" & ASCII.Nul;` appears to behave better. Still not recommended, though (and, I realise, not related to your actual problem). – Simon Wright Feb 17 '12 at 12:32
  • Thanks @SimonWright. Good suggestion. It appears that it worked for me before because I already had a variable `USERNAME`, but `USER` looks to be more general. – Rego Feb 23 '12 at 13:02