Example 2: Example: Searching an LDAP Directory

The following example uses LDAP call routines to search an LDAP directory and process the search results.
data _null_;

   length entryname $200 attrName $100 value $100 filter $100;

   rc =0; handle =0;
   server="alpair01.unx.com";
   port=8010;
   base="sasComponent=sasPublishSubscribe,cn=SAS,o=Alphalite Airways,c=US";
   bindDN=""; Pw="";

   /* open connection to LDAP server */
   call ldaps_open(handle, server, port, base, bindDn, Pw, rc);
   if rc ne 0 then do;
      msg = sysmsg();
      put msg;
   end;
   else
      put "LDAPS_OPEN call successful.";

   shandle=0;
   num=0;
   filter="(&(saschannelcn=DeleteTest)(objectclass=*))";
   attrs="description";

   /* search the LDAP directory */
   call ldaps_search(handle,shandle,filter, attrs, num, rc);
   if rc ne 0 then do;
      msg = sysmsg();
      put msg;
   end;
   else do;
      put " ";
      put "LDAPS_SEARCH call successful.";
      put "Num entries returned is " num;
      put " ";
   end;

   do eIndex = 1 to num;
      numAttrs=0;
      entryname='';

      /* retrieve each entry name and number of attributes */
      call ldaps_entry(shandle, eIndex, entryname, numAttrs, rc);
      if rc ne 0 then do;
         msg = sysmsg();
         put msg;
      end;
      else do;
         put " ";
         put "LDAPS_ENTRY call successful.";
         put "Num attributes returned is " numAttrs;
      end;

      /* for each attribute, retrieve name and values */
      do aIndex = 1 to numAttrs;
         attrName='';
         numValues=0;
         call ldaps_attrName(shandle, eIndex, aIndex, attrName, numValues, rc);
         if rc ne 0 then do;
            msg = sysmsg();
            put msg;
         end;
         else do;
            put " ";
            put "Attribute name is " attrName;
            put "Num values returned is " numValues;
         end;

         do vIndex = 1 to numValues;
            call ldaps_attrValue(shandle, eIndex, aIndex, vIndex, value, rc);
            if rc ne 0 then do;
               msg = sysmsg();
               put msg;
            end;
            else do;
               put "Value : " value;
            end;
         end;
      end;
   end;

   /* free search resources */
   call ldaps_free(shandle,rc);
   if rc ne 0 then do;
      msg = sysmsg();
      put msg;
   end;
   else
      put "LDAPS_FREE call successful.";

   /* close connection to LDAP server */
   call ldaps_close(handle,rc);
   if rc ne 0 then do;
      msg = sysmsg();
      put msg;
   end;
   else
      put "LDAPS_CLOSE call successful.";
   run;
quit;