SAS 9.1.3 Integration Technologies » Developer's Guide


LDAP CALL Routine Interface
LDAPS_ADD
LDAPS_ATTRNAME
LDAPS_ATTRVALUE
LDAPS_CLOSE
LDAPS_DELETE
LDAPS_ENTRY
LDAPS_FREE
LDAPS_MODIFY
LDAPS_OPEN
LDAPS_SETOPTIONS
LDAPS_SEARCH
Coding Examples
Adding a Directory Entry to an LDAP Server
Searching an LDAP Directory
Directory Services

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;