Link to home
Start Free TrialLog in
Avatar of toooki
toooki

asked on

Query LDAP via Oracle Pl/sql

I want to query LDAP server via Pl/Sql code. I am able to connect to the LDAP server but having difficulty to search/retrieve LDAP attributes.

Attached is the code.

If I call the function with a valid input value for LDAP attribute "uid" it returns 1. Otherwise 0. When it returns 1 I want to search LDAP to get the value of the LDAP attribute "cn" corresponding to the input "uid" attribute.

How can I write the code?
CREATE OR REPLACE PACKAGE BODY TEST IS
 FUNCTION test (in_uid IN VARCHAR2) RETURN NUMBER AS
 retval           INTEGER;
 my_attr      VARCHAR2(256);
 my_str       VARCHAR2(256);
 my_session   DBMS_LDAP.session;
 my_attrs     DBMS_LDAP.string_collection;
 my_message   DBMS_LDAP.message;
 entry_index      PLS_INTEGER;
 ldap_host        VARCHAR2(256);
 ldap_port        VARCHAR2(256);
 ldap_user        VARCHAR2(256);
 ldap_passwd      VARCHAR2(256);
 ldap_base        VARCHAR2(256);

 BEGIN
 retval := -1;
 ldap_host  := 'server.myco.com';
 ldap_port  := '389';
 ldap_user  := '';
 ldap_passwd:= '';
 ldap_base  := 'ou=People, ou=Intranet, dc=XXX, dc=com';
 my_session := DBMS_LDAP.init(ldap_host, ldap_port);
 retval := DBMS_LDAP.simple_bind_s(my_session, '', '');
 my_attrs(1) := '*';
 my_attr := 'UID';
 my_str  := my_attr || '=' || TRIM(in_uid);
 retval := DBMS_LDAP.my_s(my_session, ldap_base, DBMS_LDAP.SCOPE_SUBTREE, my_str, my_attrs, 0, my_message);
 retval := DBMS_LDAP.count_entries(my_session, my_message);
 RETURN(retval);
 END;
END;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ajexpert
ajexpert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
retval := DBMS_LDAP.my_s(my_session, ldap_base, DBMS_LDAP.SCOPE_SUBTREE, my_str, my_attrs, 0, my_message);

-- the above value will be overwritten with --
retval := DBMS_LDAP.count_entries(my_session, my_message);

Open in new window

Avatar of toooki
toooki

ASKER

retval := DBMS_LDAP.count_entries(my_session, my_message);

the above is optional.
Avatar of toooki

ASKER

It works somehow now.
Thank you.