Oracle stored procedure for ldap authentication

create or replace FUNCTION                  AUTH_LDAP_IPA 
                                         (org_username in varchar, 
                                          org_password in varchar) 
return varchar is
    vSession DBMS_LDAP.session;
    vResult  PLS_INTEGER;
BEGIN
    DBMS_LDAP.use_exception := TRUE;
    begin
        vSession := DBMS_LDAP.init
                  ( hostname => 'host-name or ipaddress'
                  , portnum  => 389
                  );
    exception
      when others then
        dbms_output.put_line(' Error code : ' || TO_CHAR(SQLCODE));
        dbms_output.put_line(' Error Message : ' || SQLERRM);
        dbms_output.put_line(' Exception encountered .. exiting');
        return 'E';      
    END;     

    vResult  := DBMS_LDAP.simple_bind_s
                  ( ld     => vSession
                  , dn     => 'uid=' || org_username || ',cn=users,cn=accounts,dc=mydomain,dc=com'
                  , passwd => org_password
                  );
   
    vResult  := DBMS_LDAP.unbind_s(vSession);
    return 'T';

exception
  when others then
    dbms_output.put_line(' Error code : ' || TO_CHAR(SQLCODE));
    dbms_output.put_line(' Error Message : ' || SQLERRM);
    dbms_output.put_line(' Exception encountered .. exiting');
    return 'F';
    

END AUTH_LDAP_IPA;

Leave a Reply