ldap_create_sort_keylist()--Create a Structure with Sort Key Values used when Sorting Search Results


  Syntax
 #include <ldap.h>
 
 int ldap_create_sort_keylist(
              LDAPsortkey ***sortKeyList,
              const char  *sortString)

  Library Name/Service Program: QSYS/QGLDCLNT

  Default Public Authority: *USE

  Threadsafe: Yes

The ldap_create_sort_keylist() function is used to create a structure with sort key values used when sorting search results.

See Sorted LDAP Search Results for usage information about the functions used to perform sorting of entries returned from the server following an LDAP search operation.


Authorities and Locks

No IBM® i authority is required. All authority checking is done by the LDAP server.


Parameters

sortKeyList
(Output) On return will point to an array of LDAPsortkey structures, which represent attributes that the server uses to sort returned entries. Input when used for ldap_create_sort_control() and ldap_free_sort_keylist(). This list must be freed by the caller by calling ldap_free_sort_keylist.

sortString
(Input) Specifies the string with one or more attributes to be used to sort entries returned by the server. Multiple sort keys are separated with a space character. (e.g. "sn -givenname")


Return Value

LDAP_SUCCESS
if the request was successful.
another LDAP error code
if the keylist could not be created.

Example

The following example uses ldap_create_sort_keylist(), ldap_create_sort_control(), ldap_free_sort_keylist(), and ldap_parse_sort_control() to perform an LDAP sorted search.

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.

/***********************************************************************/
/* File Name:   LDAPSortS.c                                            */
/*                                                                     */
/* Description: An example of how to perform an LDAP Sorted Search     */
/* using the following APIs                                            */
/*    - ldap_create_sort_keylist()                                     */
/*    - ldap_create_sort_control()                                     */
/*    - ldap_free_sort_keylist()                                       */
/*    - ldap_parse_sort_control()                                      */
/*                                                                     */
/* How to compile:                                                     */
/*  Assuming the C source for LDAPSortS.c has been downloaded into     */
/*  a source physical file MyLDAPLib/QCSRC (with record length of at   */
/*  least 132 characters) as member LDAPSortS on a System i® platform, */
/*  program LDAPSortS (*PGM) can be built via the following commands:  */
/*                                                                     */
/*  CRTCMOD MODULE(MyLDAPLib/LDAPSORTS)                                */
/*          SRCFILE(MyLDAPLib/QCSRC)                                   */
/*                                                                     */
/*  CRTPGM PGM(MyLDAPLib/LDAPSORTS)                                    */
/*         MODULE(MyLDAPLib/LDAPSORTS)                                 */
/*         BNDSRVPGM(QSYS/QGLDCLNT)                                    */
/*                                                                     */
/* Usage:                                                              */
/* The input parameters are as follows:                                */
/*   argv[1] = Search base                                             */
/*   argv[2] = filter                                                  */
/*   argv[3] = search scope (0=base, 1=onelevel, OR 2=subtree)         */
/*   argv[4] = sort attribute for sort criteria. To sort in reverse    */
/*             order,  use a minus sign (-) as a prefix.               */
/*             Ex: for ascending order on the sn attribute; use "sn"   */
/*                 for descending order on the sn attribute; use "-sn" */
/*                                                                     */
/***********************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <ldap.h>

static char ibmid[] = "Copyright IBM Corporation 2003 LICENSED MATERIAL - PROGRAM PROPERTY OF IBM";
#define         BIND_DN "cn=administrator"
#define         BIND_PW "adminpwd"

char             *server, *base, *filter, *scopes[] = { "BASE", "ONELEVEL", "SUBTREE" };
int              scope;
LDAP             *ld;

int main (int argc, char *argv[])
{
   int           l_rc, l_entries, l_port, l_entry_count=0, l_search_count=0, l_errcode=0; 
   char          *sortString, sortCriticality = 'T', *l_dn, *attrInError = NULL;
   unsigned long sortRC=0;
   LDAPMessage   *l_result, *l_entry;
   LDAPsortkey   **sortKeyList = NULL;
   LDAPControl   *sortControl = NULL, *M_controls[2] = { NULL, NULL }, **returnedControls = NULL;

   /******************************************************************/
   /* Check input parameters                                         */
   /*                                                                */
   if (argc < 5)
   {
      printf("The input parameters are as follows:\n");
      printf("     1. Search base\n");
      printf("     2. Filter\n");
      printf("     3. Search Scope (0=base, 1=onelevel, OR 2=subtree)\n");
      printf("     4. Sorting Attribute  Ex: for  ascending order on the sn attribute; use  \"sn\"\n");
      printf("                               for descending order on the sn attribute; use \"-sn\"\n");
   return 0;
   }
   /*                                                                */
   /******************************************************************/

   /******************************************************************/
   /* Set default values: Server and Port. And then parse            */
   /* input parameters into program variables                        */
   /*                                                                */
   server   = NULL;
   l_port     = LDAP_PORT;

   base     = argv[1];
   filter   = argv[2];
   scope    = atoi(argv[3]);
   sortString = argv[4];
   /*                                                                */
   /******************************************************************/

   /******************************************************************/
   /* Initialize an LDAP session                                     */
   /*                                                                */     
   ld = ldap_init(server, l_port);
   /* Check if connection is OK                                      */
   if (ld == NULL)
   {
      printf("==Error==");
      printf("  Init of server %s at port %d failed.\n", server, l_port);
      return 0;
   }
   /*                                                                */
   /******************************************************************/

   /******************************************************************/
   /* Bind as the ldap administrator                                 */
   /*                                                                */
   l_rc = ldap_simple_bind_s (ld, BIND_DN, BIND_PW);
   if ( l_rc != LDAP_SUCCESS)
   {
      printf("==Error== %s");
      printf("  Unable to Bind to the LDAP server.  Return code is %d.\n", l_rc);
      return 0;
   }
   /*                                                                */
   /******************************************************************/

   /******************************************************************/
   /* Create the sortKeyList structure with sort key values used     */
   /* when sorting search result. The structure is needed for the    */
   /* ldap_create_sort_control API.                                  */
   /*                                                                */
   /*                                                                */
   l_rc = ldap_create_sort_keylist(&sortKeyList, sortString);

   if (l_rc != LDAP_SUCCESS)
   {
      printf("==Error==");
      printf("  Failure during a create_sort_keylist.  Return code is %d.\n",l_rc);
      ldap_unbind(ld);
      return 0;
   }
   /*                                                                */
   /******************************************************************/

   /******************************************************************/
   /* Create the control for sorting to be used by the search using: */
   /* the ld pointer created by ldap_init API,                       */
   /* sortKeyList created by the ldap_create_sort_keylist API,       */
   /* sortCriticality defined to be True.                            */
   /* The return is a sortControl that is used in the                */
   /* ldap_search_ext_s API                                          */
   l_rc = ldap_create_sort_control(ld, sortKeyList, sortCriticality, &sortControl);
   /*                                                                */
   /******************************************************************/

   /******************************************************************/
   /* Free all Memory used by the Sort Key List                      */
   /* This API must be called after the ldap_create_sort_control()   */
   /* function has completed                                         */
   ldap_free_sort_keylist(sortKeyList);
   if (l_rc != LDAP_SUCCESS)
   {
      printf("==Error==");
      printf("  Failure during a create_sort_control.  Return code is %d.\n",l_rc);
      ldap_unbind(ld);
      return 0;
   }
   /*                                                                */
   /******************************************************************/

   /******************************************************************/
   /* Search for entries in the directory using the parmeters        */
   /*                                                                */
   M_controls[0] = sortControl;
   l_rc = ldap_search_ext_s(ld, base, scope, filter, NULL, 0, M_controls, NULL, NULL, 0, &l_result);
   if ((l_rc != LDAP_SUCCESS) & (l_rc != LDAP_PARTIAL_RESULTS))
   {
      printf("==Error==");
      printf("  Failure during a search.  Return code is %d.\n",l_rc);
      printf("  The search parms were:\n");
      printf("       base: %s\n",base);
      printf("      scope: %s\n",scopes[scope]);
      printf("     filter: %s\n",filter);
      ldap_unbind(ld);
      return 0;
   }
   /*                                                                */
   /******************************************************************/

   /******************************************************************/
   /* Parse the result to get the returned controls                  */
   /*                                                                */
   l_rc = ldap_parse_result(ld, l_result, &l_errcode, NULL, NULL, NULL, &returnedControls, LDAP_FALSE);

   if ((l_rc != LDAP_SUCCESS) | (l_errcode != LDAP_SUCCESS))
   {
      printf("==Error==");
      printf("  Failure during parse result.  Return code is %d.\n",l_rc);
      printf("                                Error code is %d.\n",l_errcode);
      ldap_unbind(ld);
      return 0;
   }
   /*                                                                */
   /******************************************************************/

   /******************************************************************/
   /* Parse the sort control returned, looking for any errors        */
   /*                                                                */
   if (returnedControls != NULL)
   {
      l_rc = ldap_parse_sort_control(ld, returnedControls, &sortRC, &attrInError);

      if ((l_rc != LDAP_SUCCESS) | (sortRC != LDAP_SUCCESS))
      {
         printf("==Error==");
         printf("  Failure during parse sort control.  Return code is %d.\n", l_rc);
         printf("                                      Sort return code is %d.\n", sortRC);
         printf("                ld: %p\n", ld);
         printf("  returnedControls: %p\n", returnedControls);
         printf("       attrInError: %p\n", attrInError);
         ldap_unbind(ld);
      return 0;
      }
   /*                                                                */
   /******************************************************************/

   /******************************************************************/
   /* Do the required cleanup                                        */
   /*                                                                */
      ldap_controls_free(returnedControls);
      returnedControls = NULL;

      if (attrInError != NULL)
      {
         free(attrInError);
         attrInError = NULL;
      }
   }

   M_controls[0] = NULL;
   ldap_control_free(sortControl);
   sortControl = NULL;
   /*                                                                */
   /******************************************************************/

   /******************************************************************/
   /* Disply the returned result                                     */
   /*                                                                */
   /* Determine how many entries have been found.                    */
   l_entries = ldap_count_entries(ld, l_result);
    
   if (l_entries > 0)
   {
      l_entry_count = l_entry_count + l_entries;
   }

   printf("  The search parms were:\n");
   printf("        base: %s\n",base);
   printf("       scope: %s\n",scopes[scope]);
   printf("      filter: %s\n",filter);
   printf("    sortAttr: %s\n",sortString);
   printf("  The sorted entries returned were:\n");

   for ( l_entry = ldap_first_entry(ld, l_result);
         l_entry != NULL;
         l_entry = ldap_next_entry(ld, l_entry) )
   {
      l_dn = ldap_get_dn(ld, l_entry);
      printf("    %s\n",l_dn);
   }

   /* Free the search results.                                       */
   ldap_msgfree(l_result);
        
   printf("\n  %d entries found during the search",l_entry_count);
   /*                                                                */
   /******************************************************************/

   /******************************************************************/
   /* Close the LDAP session                                         */
   /*                                                                */
   ldap_unbind(ld);
   /*                                                                */
   /******************************************************************/

   return 0;
}

Related Information



API introduced: V5R3

[ Back to top | LDAP APIs | APIs by category ]