title: How to get LDAP groups name using Oracle dbms_ldap package?
body: <p>I used this example and successfully connected to <code>LDAP</code> server - <a href="http://www.oracle-base.com/articles/9i/ldap-from-plsql-9i.php" rel="noreferrer">http://www.oracle-base.com/articles/9i/ldap-from-plsql-9i.php</a>.</p>
  
  <pre><code>SET SERVEROUTPUT ON SIZE 1000000
  DECLARE
    -- Adjust as necessary.
    l_ldap_host    VARCHAR2(256) := 'server01.tshcomputing.com';
    l_ldap_port    VARCHAR2(256) := '389';
    l_ldap_user    VARCHAR2(256) := 'cn=orcladmin';
    l_ldap_passwd  VARCHAR2(256) := 'password';
    l_ldap_base    VARCHAR2(256) := 'cn=Users,dc=tshcomputing,dc=com';
  
    l_retval       PLS_INTEGER; 
    l_session      DBMS_LDAP.session;
    l_attrs        DBMS_LDAP.string_collection;
    l_message      DBMS_LDAP.message;
    l_entry        DBMS_LDAP.message;
    l_attr_name    VARCHAR2(256);
    l_ber_element  DBMS_LDAP.ber_element;
    l_vals         DBMS_LDAP.string_collection;
  
  BEGIN
    -- Choose to raise exceptions.
    DBMS_LDAP.USE_EXCEPTION := TRUE;
  
    -- Connect to the LDAP server.
    l_session := DBMS_LDAP.init(hostname =&gt; l_ldap_host,
                                portnum  =&gt; l_ldap_port);
  
    l_retval := DBMS_LDAP.simple_bind_s(ld     =&gt; l_session,
                                        dn     =&gt; l_ldap_user,
                                        passwd =&gt; l_ldap_passwd);
  
    -- Get all attributes
    l_attrs(1) := '*'; -- retrieve all attributes 
    l_retval := DBMS_LDAP.search_s(ld       =&gt; l_session, 
                                   base     =&gt; l_ldap_base, 
                                   scope    =&gt; DBMS_LDAP.SCOPE_SUBTREE,
                                   filter   =&gt; 'objectclass=*',
                                   attrs    =&gt; l_attrs,
                                   attronly =&gt; 0,
                                   res      =&gt; l_message);
  
    IF DBMS_LDAP.count_entries(ld =&gt; l_session, msg =&gt; l_message) &gt; 0 THEN
      -- Get all the entries returned by our search.
      l_entry := DBMS_LDAP.first_entry(ld  =&gt; l_session,
                                       msg =&gt; l_message);
  
      &lt;&lt; entry_loop &gt;&gt;
      WHILE l_entry IS NOT NULL LOOP
        -- Get all the attributes for this entry.
        DBMS_OUTPUT.PUT_LINE('---------------------------------------');
        l_attr_name := DBMS_LDAP.first_attribute(ld        =&gt; l_session,
                                                 ldapentry =&gt; l_entry,
                                                 ber_elem  =&gt; l_ber_element);
        &lt;&lt; attributes_loop &gt;&gt;
        WHILE l_attr_name IS NOT NULL LOOP
          -- Get all the values for this attribute.
          l_vals := DBMS_LDAP.get_values (ld        =&gt; l_session,
                                          ldapentry =&gt; l_entry,
                                          attr      =&gt; l_attr_name);
          &lt;&lt; values_loop &gt;&gt;
          FOR i IN l_vals.FIRST .. l_vals.LAST LOOP
            DBMS_OUTPUT.PUT_LINE('ATTIBUTE_NAME: ' || l_attr_name || ' = ' || SUBSTR(l_vals(i),1,200));
          END LOOP values_loop;
          l_attr_name := DBMS_LDAP.next_attribute(ld        =&gt; l_session,
                                                  ldapentry =&gt; l_entry,
                                                  ber_elem  =&gt; l_ber_element);
        END LOOP attibutes_loop;
        l_entry := DBMS_LDAP.next_entry(ld  =&gt; l_session,
                                        msg =&gt; l_entry);
      END LOOP entry_loop;
    END IF;
  
    -- Disconnect from the LDAP server.
    l_retval := DBMS_LDAP.unbind_s(ld =&gt; l_session);
    DBMS_OUTPUT.PUT_LINE('L_RETVAL: ' || l_retval);
  END;
  /
  </code></pre>
  
  <p>I got this this result:</p>
  
  <p><img src="https://i.stack.imgur.com/kD4Xy.png" alt="Results">
  Actually, the user have 3 groups, but one group is <code>Primary</code> and stored in <code>primaryGroupID</code>. I tried to do queries for groups, but I couldn't find attribute like ID. How can I get groups info (like a value of <code>memberOf</code> attribute) by <code>primaryGroupID</code>?</p>