Back to index

openldap  2.4.31
Defines | Functions
sortctrl.c File Reference
#include "portable.h"
#include <stdio.h>
#include <ac/stdlib.h>
#include <ac/string.h>
#include <ac/time.h>
#include "ldap-int.h"

Go to the source code of this file.

Defines

#define LDAP_MATCHRULE_IDENTIFIER   0x80L
#define LDAP_REVERSEORDER_IDENTIFIER   0x81L
#define LDAP_ATTRTYPES_IDENTIFIER   0x80L

Functions

static int countKeys (char *keyString)
static int readNextKey (char **pNextKey, LDAPSortKey **key)
int ldap_create_sort_keylist (LDAPSortKey ***sortKeyList, char *keyString)
void ldap_free_sort_keylist (LDAPSortKey **keyList)
int ldap_create_sort_control_value (LDAP *ld, LDAPSortKey **keyList, struct berval *value)
int ldap_create_sort_control (LDAP *ld, LDAPSortKey **keyList, int isCritical, LDAPControl **ctrlp)
int ldap_parse_sortresponse_control (LDAP *ld, LDAPControl *ctrl, ber_int_t *returnCode, char **attribute)

Define Documentation

#define LDAP_ATTRTYPES_IDENTIFIER   0x80L

Definition at line 42 of file sortctrl.c.

#define LDAP_MATCHRULE_IDENTIFIER   0x80L

Definition at line 40 of file sortctrl.c.

#define LDAP_REVERSEORDER_IDENTIFIER   0x81L

Definition at line 41 of file sortctrl.c.


Function Documentation

static int countKeys ( char *  keyString) [static]

Definition at line 54 of file sortctrl.c.

{
       char *p = keyString;
       int count = 0;

       for (;;)
       {
              while (LDAP_SPACE(*p))              /* Skip leading whitespace */
                     p++;

              if (*p == '\0')                    /* End of string? */
                     return count;

              count++;                           /* Found start of a key */

              while (!LDAP_SPACE(*p))     /* Skip till next space or end of string. */
                     if (*p++ == '\0')
                            return count;
       }
}

Here is the caller graph for this function:

int ldap_create_sort_control ( LDAP *  ld,
LDAPSortKey **  keyList,
int  isCritical,
LDAPControl **  ctrlp 
)

Definition at line 405 of file sortctrl.c.

{
       struct berval value;

       assert( ld != NULL );
       assert( LDAP_VALID( ld ) );

       if ( ld == NULL ) {
              return LDAP_PARAM_ERROR;
       }

       if ( ctrlp == NULL ) {
              ld->ld_errno = LDAP_PARAM_ERROR;
              return ld->ld_errno;
       }

       ld->ld_errno = ldap_create_sort_control_value( ld, keyList, &value );
       if ( ld->ld_errno == LDAP_SUCCESS ) {
              ld->ld_errno = ldap_control_create( LDAP_CONTROL_SORTREQUEST,
                     isCritical, &value, 0, ctrlp );
              if ( ld->ld_errno != LDAP_SUCCESS ) {
                     LDAP_FREE( value.bv_val );
              }
       }

       return ld->ld_errno;
}

Here is the call graph for this function:

int ldap_create_sort_control_value ( LDAP *  ld,
LDAPSortKey **  keyList,
struct berval value 
)

Definition at line 287 of file sortctrl.c.

{
       int           i;
       BerElement    *ber = NULL;
       ber_tag_t     tag;

       assert( ld != NULL );
       assert( LDAP_VALID( ld ) );

       if ( ld == NULL ) return LDAP_PARAM_ERROR;
       if ( keyList == NULL || value == NULL ) {
              ld->ld_errno = LDAP_PARAM_ERROR;
              return LDAP_PARAM_ERROR;
       }

       value->bv_val = NULL;
       value->bv_len = 0;
       ld->ld_errno = LDAP_SUCCESS;

       ber = ldap_alloc_ber_with_options( ld );
       if ( ber == NULL) {
              ld->ld_errno = LDAP_NO_MEMORY;
              return ld->ld_errno;
       }

       tag = ber_printf( ber, "{" /*}*/ );
       if ( tag == LBER_ERROR ) {
              goto error_return;
       }

       for ( i = 0; keyList[i] != NULL; i++ ) {
              tag = ber_printf( ber, "{s" /*}*/, keyList[i]->attributeType );
              if ( tag == LBER_ERROR ) {
                     goto error_return;
              }

              if ( keyList[i]->orderingRule != NULL ) {
                     tag = ber_printf( ber, "ts",
                            LDAP_MATCHRULE_IDENTIFIER,
                            keyList[i]->orderingRule );

                     if ( tag == LBER_ERROR ) {
                            goto error_return;
                     }
              }

              if ( keyList[i]->reverseOrder ) {
                     tag = ber_printf( ber, "tb",
                            LDAP_REVERSEORDER_IDENTIFIER,
                            keyList[i]->reverseOrder );

                     if ( tag == LBER_ERROR ) {
                            goto error_return;
                     }
              }

              tag = ber_printf( ber, /*{*/ "N}" );
              if ( tag == LBER_ERROR ) {
                     goto error_return;
              }
       }

       tag = ber_printf( ber, /*{*/ "N}" );
       if ( tag == LBER_ERROR ) {
              goto error_return;
       }

       if ( ber_flatten2( ber, value, 1 ) == -1 ) {
              ld->ld_errno = LDAP_NO_MEMORY;
       }

       if ( 0 ) {
error_return:;
              ld->ld_errno =  LDAP_ENCODING_ERROR;
       }

       if ( ber != NULL ) {
              ber_free( ber, 1 );
       }

       return ld->ld_errno;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int ldap_create_sort_keylist ( LDAPSortKey ***  sortKeyList,
char *  keyString 
)

Definition at line 185 of file sortctrl.c.

{
       int         numKeys, rc, i;
       char        *nextKey;
       LDAPSortKey **keyList = NULL;

       assert( sortKeyList != NULL );
       assert( keyString != NULL );

       *sortKeyList = NULL;

       /* Determine the number of sort keys so we can allocate memory. */
       if (( numKeys = countKeys(keyString)) == 0) {
              return LDAP_PARAM_ERROR;
       }

       /* Allocate the array of pointers.  Initialize to NULL. */
       keyList=(LDAPSortKey**)LBER_CALLOC(numKeys+1, sizeof(LDAPSortKey*));
       if ( keyList == NULL) return LDAP_NO_MEMORY;

       /* For each sort key in the string, create an LDAPSortKey structure
          and add it to the list.
       */
       nextKey = keyString;          /* Points to the next key in the string */
       for (i=0; i < numKeys; i++) {
              rc = readNextKey(&nextKey, &keyList[i]);

              if (rc != LDAP_SUCCESS) {
                     ldap_free_sort_keylist(keyList);
                     return rc;
              }
       }

       *sortKeyList = keyList;
       return LDAP_SUCCESS;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 235 of file sortctrl.c.

{
       int i;
       LDAPSortKey *nextKeyp;

       if (keyList == NULL) return;

       i=0;
       while ( 0 != (nextKeyp = keyList[i++]) ) {
              if (nextKeyp->attributeType) {
                     LBER_FREE(nextKeyp->attributeType);
              }

              if (nextKeyp->orderingRule != NULL) {
                     LBER_FREE(nextKeyp->orderingRule);
              }

              LBER_FREE(nextKeyp);
       }

       LBER_FREE(keyList);
}

Here is the caller graph for this function:

int ldap_parse_sortresponse_control ( LDAP *  ld,
LDAPControl ctrl,
ber_int_t *  returnCode,
char **  attribute 
)

Definition at line 486 of file sortctrl.c.

{
       BerElement *ber;
       ber_tag_t tag, berTag;
       ber_len_t berLen;

       assert( ld != NULL );
       assert( LDAP_VALID( ld ) );

       if (ld == NULL) {
              return LDAP_PARAM_ERROR;
       }

       if (ctrl == NULL) {
              ld->ld_errno =  LDAP_PARAM_ERROR;
              return(ld->ld_errno);
       }

       if (attribute) {
              *attribute = NULL;
       }

       if ( strcmp(LDAP_CONTROL_SORTRESPONSE, ctrl->ldctl_oid) != 0 ) {
              /* Not sort result control */
              ld->ld_errno = LDAP_CONTROL_NOT_FOUND;
              return(ld->ld_errno);
       }

       /* Create a BerElement from the berval returned in the control. */
       ber = ber_init(&ctrl->ldctl_value);

       if (ber == NULL) {
              ld->ld_errno = LDAP_NO_MEMORY;
              return(ld->ld_errno);
       }

       /* Extract the result code from the control. */
       tag = ber_scanf(ber, "{e" /*}*/, returnCode);

       if( tag == LBER_ERROR ) {
              ber_free(ber, 1);
              ld->ld_errno = LDAP_DECODING_ERROR;
              return(ld->ld_errno);
       }

       /* If caller wants the attribute name, and if it's present in the control,
          extract the attribute name which caused the error. */
       if (attribute && (LDAP_ATTRTYPES_IDENTIFIER == ber_peek_tag(ber, &berLen)))
       {
              tag = ber_scanf(ber, "ta", &berTag, attribute);

              if (tag == LBER_ERROR ) {
                     ber_free(ber, 1);
                     ld->ld_errno = LDAP_DECODING_ERROR;
                     return(ld->ld_errno);
              }
       }

       ber_free(ber,1);

       ld->ld_errno = LDAP_SUCCESS;
       return(ld->ld_errno);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int readNextKey ( char **  pNextKey,
LDAPSortKey **  key 
) [static]

Definition at line 95 of file sortctrl.c.

{
       char *p = *pNextKey;
       int rev = 0;
       char *attrStart;
       int attrLen;
       char *oidStart = NULL;
       int oidLen = 0;

       /* Skip leading white space. */
       while (LDAP_SPACE(*p))
              p++;

       if (*p == '-')               /* Check if the reverse flag is present. */
       {
              rev=1;
              p++;
       }

       /* We're now positioned at the start of the attribute. */
       attrStart = p;

       /* Get the length of the attribute until the next whitespace or ":". */
       attrLen = strcspn(p, " \t:");
       p += attrLen;

       if (attrLen == 0)     /* If no attribute name was present, quit. */
              return LDAP_PARAM_ERROR;

       if (*p == ':')
       {
              oidStart = ++p;                            /* Start of the OID, after the colon */
              oidLen = strcspn(p, " \t");  /* Get length of OID till next whitespace */
              p += oidLen;
       }

       *pNextKey = p;               /* Update argument to point to next key */

       /* Allocate an LDAPSortKey structure */
       *key = LDAP_MALLOC(sizeof(LDAPSortKey));
       if (*key == NULL) return LDAP_NO_MEMORY;

       /* Allocate memory for the attribute and copy to it. */
       (*key)->attributeType = LDAP_MALLOC(attrLen+1);
       if ((*key)->attributeType == NULL) {
              LDAP_FREE(*key);
              return LDAP_NO_MEMORY;
       }

       strncpy((*key)->attributeType, attrStart, attrLen);
       (*key)->attributeType[attrLen] = 0;

       /* If present, allocate memory for the OID and copy to it. */
       if (oidLen) {
              (*key)->orderingRule = LDAP_MALLOC(oidLen+1);
              if ((*key)->orderingRule == NULL) {
                     LDAP_FREE((*key)->attributeType);
                     LDAP_FREE(*key);
                     return LDAP_NO_MEMORY;
              }
              strncpy((*key)->orderingRule, oidStart, oidLen);
              (*key)->orderingRule[oidLen] = 0;

       } else {
              (*key)->orderingRule = NULL;
       }

       (*key)->reverseOrder = rev;

       return LDAP_SUCCESS;
}

Here is the caller graph for this function: