Vous êtes sur la page 1sur 4

2/5/2015

ORACLEBASEDBMS_LDAPAccessingLDAPFromPL/SQL

LDAPServer Oracle9I

Home

Articles

Scripts

Blog

Certification

Misc

PLSQL WithOracle

About

PrinterFriendly
Oracle8i|Oracle9i|Oracle10g|Oracle11g|Oracle12c|Miscellaneous|PL/SQL|SQL|OracleRAC|
OracleApps|Linux
HomeArticles9iHere

Search

DBMS_LDAPAccessingLDAP
FromPL/SQL
TheDBMS_LDAPpackageisaPL/SQLAPItoenable
programaticsearchesandmodificationsofdatawithin
LDAPdirectories.InthisarticleI'lldemonstrateasimple
LDAPsearch.
CreateDBMS_LDAP
ConnectAndAuthenticate
SearchDirectory
LoopThroughEntries
LoopThroughAttributes
LoopThroughValues
PutItAllTogether

CreateDBMS_LDAP
IftheDBMS_LDAPpackageisnotloaded,youcaninstallitbyrunningthefollowingscriptasSYS.
SQL>@$ORACLE_HOME/rdbms/admin/catldap.sql

ConnectAndAuthenticate
ThefirststepinanyLDAPinteractionistoconnecttotheLDAPserverandauthenticateyourself.Thisis
doneusingtheinitandsimple_bind_sfunctions.
l_session:=DBMS_LDAP.init(hostname=>l_ldap_host,
portnum=>l_ldap_port);
l_retval:=DBMS_LDAP.simple_bind_s(ld=>l_session,
dn=>l_ldap_user,
passwd=>l_ldap_passwd);

SearchDirectory
Onceconnectedwecansearchthedirectory.
Translate
l_attrs(1):='*';retrieveallattributes
l_retval:=DBMS_LDAP.search_s(ld=>l_session,
base=>l_ldap_base,
scope=>DBMS_LDAP.SCOPE_SUBTREE,
filter=>'objectclass=*',
attrs=>l_attrs,
attronly=>0,
res=>l_message);

http://oraclebase.com/articles/9i/ldapfromplsql9i.php

1/4

2/5/2015

ORACLEBASEDBMS_LDAPAccessingLDAPFromPL/SQL

Thestartingpointforthesearch(base),depthofthesearch(scope),theattributessearchedfor(attrs)and
thefiltercanbemodifiedtobuildcomplexsearches.

LoopThroughEntries
Thesearchreturnsalistofentrieswhichcanbeloopedthrough.
IFDBMS_LDAP.count_entries(ld=>l_session,msg=>l_message)>0THEN
Getalltheentriesreturnedbyoursearch.
l_entry:=DBMS_LDAP.first_entry(ld=>l_session,
msg=>l_message);
<<entry_loop>>
WHILEl_entryISNOTNULLLOOP
...
...
l_entry:=DBMS_LDAP.next_entry(ld=>l_session,
msg=>l_entry);
ENDLOOPentry_loop;
ENDIF;

LoopThroughAttributes
Foreachentryweloopthroughtheassociatedattributes.
l_attr_name:=DBMS_LDAP.first_attribute(ld=>l_session,
ldapentry=>l_entry,
ber_elem=>l_ber_element);
<<attributes_loop>>
WHILEl_attr_nameISNOTNULLLOOP
...
...
l_attr_name:=DBMS_LDAP.next_attribute(ld=>l_session,
ldapentry=>l_entry,
ber_elem=>l_ber_element);
ENDLOOPattibutes_loop;

LoopThroughValues
Finally,weretrievethevaluesassociatedwiththeattribute.
<<values_loop>>
FORiINl_vals.FIRST..l_vals.LASTLOOP
DBMS_OUTPUT.PUT_LINE('ATTIBUTE_NAME:'||l_attr_name||'='||SUBSTR(l_vals(i),1,200));
ENDLOOPvalues_loop;

PutItAllTogether
Ifweputallthesestagestogetherwegetthefollowing.
SETSERVEROUTPUTONSIZE1000000
DECLARE
Adjustasnecessary.
l_ldap_hostVARCHAR2(256):='server01.tshcomputing.com';
l_ldap_portVARCHAR2(256):='389';
l_ldap_userVARCHAR2(256):='cn=orcladmin';
l_ldap_passwdVARCHAR2(256):='password';
l_ldap_baseVARCHAR2(256):='cn=Users,dc=tshcomputing,dc=com';
l_retvalPLS_INTEGER;
l_sessionDBMS_LDAP.session;
l_attrsDBMS_LDAP.string_collection;
l_messageDBMS_LDAP.message;
l_entryDBMS_LDAP.message;
l_attr_nameVARCHAR2(256);
l_ber_elementDBMS_LDAP.ber_element;
l_valsDBMS_LDAP.string_collection;

BEGIN
http://oraclebase.com/articles/9i/ldapfromplsql9i.php

2/4

2/5/2015

ORACLEBASEDBMS_LDAPAccessingLDAPFromPL/SQL

Choosetoraiseexceptions.
DBMS_LDAP.USE_EXCEPTION:=TRUE;
ConnecttotheLDAPserver.
l_session:=DBMS_LDAP.init(hostname=>l_ldap_host,
portnum=>l_ldap_port);
l_retval:=DBMS_LDAP.simple_bind_s(ld=>l_session,
dn=>l_ldap_user,
passwd=>l_ldap_passwd);
Getallattributes
l_attrs(1):='*';retrieveallattributes
l_retval:=DBMS_LDAP.search_s(ld=>l_session,
base=>l_ldap_base,
scope=>DBMS_LDAP.SCOPE_SUBTREE,
filter=>'objectclass=*',
attrs=>l_attrs,
attronly=>0,
res=>l_message);
IFDBMS_LDAP.count_entries(ld=>l_session,msg=>l_message)>0THEN
Getalltheentriesreturnedbyoursearch.
l_entry:=DBMS_LDAP.first_entry(ld=>l_session,
msg=>l_message);
<<entry_loop>>
WHILEl_entryISNOTNULLLOOP
Getalltheattributesforthisentry.
DBMS_OUTPUT.PUT_LINE('');
l_attr_name:=DBMS_LDAP.first_attribute(ld=>l_session,
ldapentry=>l_entry,
ber_elem=>l_ber_element);
<<attributes_loop>>
WHILEl_attr_nameISNOTNULLLOOP
Getallthevaluesforthisattribute.
l_vals:=DBMS_LDAP.get_values(ld=>l_session,
ldapentry=>l_entry,
attr=>l_attr_name);
<<values_loop>>
FORiINl_vals.FIRST..l_vals.LASTLOOP
DBMS_OUTPUT.PUT_LINE('ATTIBUTE_NAME:'||l_attr_name||'='||SUBSTR(l_vals(i),1,200));
ENDLOOPvalues_loop;
l_attr_name:=DBMS_LDAP.next_attribute(ld=>l_session,
ldapentry=>l_entry,
ber_elem=>l_ber_element);
ENDLOOPattibutes_loop;
l_entry:=DBMS_LDAP.next_entry(ld=>l_session,
msg=>l_entry);
ENDLOOPentry_loop;
ENDIF;
DisconnectfromtheLDAPserver.
l_retval:=DBMS_LDAP.unbind_s(ld=>l_session);
DBMS_OUTPUT.PUT_LINE('L_RETVAL:'||l_retval);
END;
/

Formoreinformationsee:
DBMS_LDAP(9i)
DBMS_LDAP(FusionMiddleware)
Hopethishelps.RegardsTim...
BacktotheTop.

http://oraclebase.com/articles/9i/ldapfromplsql9i.php

3/4

2/5/2015

ORACLEBASEDBMS_LDAPAccessingLDAPFromPL/SQL

6comments,read/addthem...

Home|Articles|Scripts|Forums|Blog|Certification|Misc|Search|About
Copyright&Disclaimer
HTMLCSS

http://oraclebase.com/articles/9i/ldapfromplsql9i.php

4/4

Vous aimerez peut-être aussi