Tips and wisdom from 15 years of DBA experience

Thursday, September 17, 2009

How to conect to Active Directory though Oracle

Connecting to an AD server through Oracle can be accomplished using DBMS_LDAP. This particular example was tested in Oracle 9i, but will probably work in more recent versions of Oracle. Note that the l_ldap_user variable MUST contain the ldap domain.

I highly recommend parameterizing the password, and would make certain that I am using all security precautions recommended by Oracle before implementing in production.


-- 9/17/09 - Craig Glendenning - a test procedure based on Oracle sample code to check ldap connectivity
create or replace
PROCEDURE LDAP_CONNECT AS
l_ldap_host VARCHAR2(256) := 'YOUR_LDAP_DOMAIN.com';
l_ldap_port VARCHAR2(256) := '389';
l_ldap_user VARCHAR2(256) := 'ad_user@YOUR_LDAP_DOMAIN.com';
l_ldap_passwd VARCHAR2(256) := 'your_pw';
l_retval PLS_INTEGER;
l_session DBMS_LDAP.session;
BEGIN
-- Choose to raise exceptions.
--DBMS_LDAP.USE_EXCEPTION := TRUE;
DBMS_OUTPUT.PUT_LINE('Connecting');
-- Connect to the LDAP server.
l_session := DBMS_LDAP.init(l_ldap_host,l_ldap_port);
DBMS_OUTPUT.PUT_LINE('Init done ..Session is ' || l_session);
l_retval := DBMS_LDAP.simple_bind_s( ld => l_session,
dn => l_ldap_user,
passwd => l_ldap_passwd);
DBMS_OUTPUT.PUT_LINE('Connected');
-- Disconnect from the LDAP server.
l_retval := DBMS_LDAP.unbind_s(l_session);
DBMS_OUTPUT.PUT_LINE('L_RETVAL: ' || l_retval);
dbms_output.put_line('All Done!!');
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20001,'Error - '||SQLCODE||' '||SQLERRM);
END LDAP_CONNECT;
/

3 comments:

Siddhartha said...

Dear Craig,

We want to get the User's AD Login ID using user's emp code from Active Directory. I have users emp Code with me in Oracle Database but I need to get his AD id also. Is it possible to do that and if yes can you provide me the code for the same. My mail id is siddhartharathi at gmail dot com


Thanks in advance,
Siddharta

Anonymous said...

In this case is it necessary another LDAP server like openLDAP?? I have a Oracle Database on linux. Tks

Anonymous said...

Could you please give details explanation for this?

Followers