/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
- * Copyright 1998-2006 The OpenLDAP Foundation.
+ * Copyright 1998-2009 The OpenLDAP Foundation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* can be found in the file "build/LICENSE-2.0.1" in this distribution
* of OpenLDAP Software.
*/
-/* Portions Copyright (C) The Internet Society (1997)
- * ASN.1 fragments are from RFC 2251; see RFC for full legal notices.
- */
#include "portable.h"
/* ---------------------------------------------------------------------------
- ldap_create_sort_control
+ ldap_create_sort_control_value
- Create and encode the server-side sort control.
+ Create and encode the value of the server-side sort control.
ld (IN) An LDAP session handle, as obtained from a call to
ldap_init().
consists of an attribute name, ascending/descending flag,
and an optional matching rule (OID) to use.
- isCritical (IN) 0 - Indicates the control is not critical to the operation.
- non-zero - The control is critical to the operation.
-
- ctrlp (OUT) Returns a pointer to the LDAPControl created. This control
- SHOULD be freed by calling ldap_control_free() when done.
+ value (OUT) Contains the control value; the bv_val member of the berval structure
+ SHOULD be freed by calling ldap_memfree() when done.
Ber encoding
---------------------------------------------------------------------------*/
int
-ldap_create_sort_control (
+ldap_create_sort_control_value(
LDAP *ld,
LDAPSortKey **keyList,
- int isCritical,
- LDAPControl **ctrlp )
+ struct berval *value )
{
- int i;
- BerElement *ber;
- ber_tag_t tag;
+ int i;
+ BerElement *ber = NULL;
+ ber_tag_t tag;
+ assert( ld != NULL );
+ assert( LDAP_VALID( ld ) );
- if ( (ld == NULL) || (keyList == NULL) || (ctrlp == NULL) ) {
+ if ( ld == NULL ) return LDAP_PARAM_ERROR;
+ if ( keyList == NULL || value == NULL ) {
ld->ld_errno = LDAP_PARAM_ERROR;
- return(ld->ld_errno);
+ return LDAP_PARAM_ERROR;
}
- if ((ber = ldap_alloc_ber_with_options(ld)) == NULL) {
+ 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 );
+ return ld->ld_errno;
}
- tag = ber_printf(ber, "{" /*}*/);
- if (tag == LBER_ERROR) goto exit;
+ 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 exit;
+ 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) {
+ if ( keyList[i]->orderingRule != NULL ) {
tag = ber_printf( ber, "ts",
LDAP_MATCHRULE_IDENTIFIER,
- (keyList[i])->orderingRule );
+ keyList[i]->orderingRule );
- if( tag == LBER_ERROR ) goto exit;
+ if ( tag == LBER_ERROR ) {
+ goto error_return;
+ }
}
- if ((keyList[i])->reverseOrder) {
- tag = ber_printf(ber, "tb",
+ if ( keyList[i]->reverseOrder ) {
+ tag = ber_printf( ber, "tb",
LDAP_REVERSEORDER_IDENTIFIER,
- (keyList[i])->reverseOrder );
+ keyList[i]->reverseOrder );
+
+ if ( tag == LBER_ERROR ) {
+ goto error_return;
+ }
+ }
- if( tag == LBER_ERROR ) goto exit;
+ tag = ber_printf( ber, /*{*/ "N}" );
+ 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 exit;
+ if ( ber_flatten2( ber, value, 1 ) == -1 ) {
+ ld->ld_errno = LDAP_NO_MEMORY;
}
- tag = ber_printf(ber, /*{*/ "N}");
- if( tag == LBER_ERROR ) goto exit;
+ if ( 0 ) {
+error_return:;
+ ld->ld_errno = LDAP_ENCODING_ERROR;
+ }
- ld->ld_errno = ldap_create_control( LDAP_CONTROL_SORTREQUEST,
- ber, isCritical, ctrlp);
+ if ( ber != NULL ) {
+ ber_free( ber, 1 );
+ }
- ber_free(ber, 1);
+ return ld->ld_errno;
+}
- return(ld->ld_errno);
-exit:
- ber_free(ber, 1);
- ld->ld_errno = LDAP_ENCODING_ERROR;
- return(ld->ld_errno);
+/* ---------------------------------------------------------------------------
+ ldap_create_sort_control
+
+ Create and encode the server-side sort control.
+
+ ld (IN) An LDAP session handle, as obtained from a call to
+ ldap_init().
+
+ keyList (IN) Points to a null-terminated array of pointers to
+ LDAPSortKey structures, containing a description of
+ each of the sort keys to be used. The description
+ consists of an attribute name, ascending/descending flag,
+ and an optional matching rule (OID) to use.
+
+ isCritical (IN) 0 - Indicates the control is not critical to the operation.
+ non-zero - The control is critical to the operation.
+
+ ctrlp (OUT) Returns a pointer to the LDAPControl created. This control
+ SHOULD be freed by calling ldap_control_free() when done.
+
+
+ Ber encoding
+
+ SortKeyList ::= SEQUENCE OF SEQUENCE {
+ attributeType AttributeDescription,
+ orderingRule [0] MatchingRuleId OPTIONAL,
+ reverseOrder [1] BOOLEAN DEFAULT FALSE }
+
+ ---------------------------------------------------------------------------*/
+
+int
+ldap_create_sort_control(
+ LDAP *ld,
+ LDAPSortKey **keyList,
+ int isCritical,
+ LDAPControl **ctrlp )
+{
+ 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;
}
---------------------------------------------------------------------------*/
int
-ldap_parse_sortedresult_control(
- LDAP *ld,
- LDAPControl *ctrl,
- unsigned long *returnCode,
- char **attribute )
+ldap_parse_sortresponse_control(
+ LDAP *ld,
+ LDAPControl *ctrl,
+ ber_int_t *returnCode,
+ char **attribute )
{
BerElement *ber;
- LDAPControl *pControl;
- int i;
ber_tag_t tag, berTag;
ber_len_t berLen;
+ assert( ld != NULL );
+ assert( LDAP_VALID( ld ) );
+
if (ld == NULL) {
- ld->ld_errno = LDAP_PARAM_ERROR;
- return(ld->ld_errno);
+ return LDAP_PARAM_ERROR;
}
if (ctrl == NULL) {