X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;ds=sidebyside;f=libraries%2Flibldap%2Fsortctrl.c;h=c523b631a258a5ad7fe46aed330f6ea00c4ec74d;hb=3a2e98e91c3a8f93e5b37cb7e5a76708194cff77;hp=8079b0549fb709661d86bde8383d76a8348d4216;hpb=6939c531700652491f4be4688c6a1f35a1ab8a18;p=openldap
diff --git a/libraries/libldap/sortctrl.c b/libraries/libldap/sortctrl.c
index 8079b0549f..c523b631a2 100644
--- a/libraries/libldap/sortctrl.c
+++ b/libraries/libldap/sortctrl.c
@@ -1,10 +1,18 @@
-/*
- * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+/* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software .
+ *
+ * Copyright 1998-2009 The OpenLDAP Foundation.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * .
*/
-/* Adapted for inclusion into OpenLDAP by Kurt D. Zeilenga */
-/*---
- * Copyright (C) 1999, 2000 Novell, Inc. All Rights Reserved.
+/* Portions Copyright (C) 1999, 2000 Novell, Inc. All Rights Reserved.
*
* THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND
* TREATIES. USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT
@@ -14,8 +22,7 @@
* OF THIS WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP
* PUBLIC LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM NOVELL, COULD SUBJECT
* THE PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
- *
- *---*/
+ */
/* Note: A verbatim copy of version 2.0.1 of the OpenLDAP Public License
* can be found in the file "build/LICENSE-2.0.1" in this distribution
* of OpenLDAP Software.
@@ -250,9 +257,9 @@ ldap_free_sort_keylist ( LDAPSortKey **keyList )
/* ---------------------------------------------------------------------------
- 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().
@@ -263,11 +270,8 @@ ldap_free_sort_keylist ( LDAPSortKey **keyList )
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
@@ -280,82 +284,166 @@ ldap_free_sort_keylist ( LDAPSortKey **keyList )
---------------------------------------------------------------------------*/
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 exit;
+ if ( tag == LBER_ERROR ) {
+ goto error_return;
+ }
}
- tag = ber_printf(ber, /*{*/ "N}");
- 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 exit;
+ tag = ber_printf( ber, /*{*/ "N}" );
+ if ( tag == LBER_ERROR ) {
+ goto error_return;
+ }
- ld->ld_errno = ldap_create_control( LDAP_CONTROL_SORTREQUEST,
- ber, isCritical, ctrlp);
+ if ( ber_flatten2( ber, value, 1 ) == -1 ) {
+ ld->ld_errno = LDAP_NO_MEMORY;
+ }
- ber_free(ber, 1);
+ if ( 0 ) {
+error_return:;
+ ld->ld_errno = LDAP_ENCODING_ERROR;
+ }
- return(ld->ld_errno);
+ if ( ber != NULL ) {
+ ber_free( ber, 1 );
+ }
-exit:
- ber_free(ber, 1);
- ld->ld_errno = LDAP_ENCODING_ERROR;
- return(ld->ld_errno);
+ return ld->ld_errno;
}
/* ---------------------------------------------------------------------------
- ldap_parse_sort_control
+ 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;
+}
+
+
+/* ---------------------------------------------------------------------------
+ ldap_parse_sortedresult_control
Decode the server-side sort control return information.
ld (IN) An LDAP session handle, as obtained from a call to
ldap_init().
- ctrls (IN) The address of a NULL-terminated array of LDAPControl
- structures, typically obtained by a call to
- ldap_parse_result().
+ ctrl (IN) The address of the LDAP Control Structure.
returnCode (OUT) This result parameter is filled in with the sort control
result code. This parameter MUST not be NULL.
@@ -395,25 +483,25 @@ exit:
---------------------------------------------------------------------------*/
int
-ldap_parse_sort_control(
- LDAP *ld,
- LDAPControl **ctrls,
- 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 (ctrls == NULL) {
- ld->ld_errno = LDAP_CONTROL_NOT_FOUND;
+ if (ctrl == NULL) {
+ ld->ld_errno = LDAP_PARAM_ERROR;
return(ld->ld_errno);
}
@@ -421,20 +509,14 @@ ldap_parse_sort_control(
*attribute = NULL;
}
- /* Search the list of control responses for a sort control. */
- for (i=0; ctrls[i]; i++) {
- pControl = ctrls[i];
- if (!strcmp(LDAP_CONTROL_SORTRESPONSE, pControl->ldctl_oid))
- goto foundSortControl;
+ if ( strcmp(LDAP_CONTROL_SORTRESPONSE, ctrl->ldctl_oid) != 0 ) {
+ /* Not sort result control */
+ ld->ld_errno = LDAP_CONTROL_NOT_FOUND;
+ return(ld->ld_errno);
}
- /* No sort control was found. */
- ld->ld_errno = LDAP_CONTROL_NOT_FOUND;
- return(ld->ld_errno);
-
-foundSortControl:
/* Create a BerElement from the berval returned in the control. */
- ber = ber_init(&pControl->ldctl_value);
+ ber = ber_init(&ctrl->ldctl_value);
if (ber == NULL) {
ld->ld_errno = LDAP_NO_MEMORY;