2005-08-23 16:47:38 +08:00
|
|
|
/* valsort.c - sort attribute values */
|
|
|
|
/* $OpenLDAP$ */
|
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2019-01-15 02:46:16 +08:00
|
|
|
* Copyright 2005-2019 The OpenLDAP Foundation.
|
2005-08-23 16:47:38 +08:00
|
|
|
* Portions copyright 2005 Symas Corporation.
|
|
|
|
* 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
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
|
|
|
*/
|
|
|
|
/* ACKNOWLEDGEMENTS:
|
|
|
|
* This work was initially developed by Howard Chu for inclusion in
|
2005-08-23 18:44:36 +08:00
|
|
|
* OpenLDAP Software. This work was sponsored by Stanford University.
|
2005-08-23 16:47:38 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This overlay sorts the values of multi-valued attributes when returning
|
|
|
|
* them in a search response.
|
|
|
|
*/
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#ifdef SLAPD_OVER_VALSORT
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/ctype.h>
|
|
|
|
|
|
|
|
#include "slap.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "lutil.h"
|
|
|
|
|
|
|
|
#define VALSORT_ASCEND 0
|
|
|
|
#define VALSORT_DESCEND 1
|
|
|
|
|
|
|
|
#define VALSORT_ALPHA 2
|
|
|
|
#define VALSORT_NUMERIC 4
|
|
|
|
|
|
|
|
#define VALSORT_WEIGHTED 8
|
|
|
|
|
|
|
|
typedef struct valsort_info {
|
|
|
|
struct valsort_info *vi_next;
|
|
|
|
struct berval vi_dn;
|
|
|
|
AttributeDescription *vi_ad;
|
|
|
|
slap_mask_t vi_sort;
|
|
|
|
} valsort_info;
|
|
|
|
|
2005-08-24 10:07:43 +08:00
|
|
|
static int valsort_cid;
|
|
|
|
|
2005-08-23 16:47:38 +08:00
|
|
|
static ConfigDriver valsort_cf_func;
|
|
|
|
|
|
|
|
static ConfigTable valsort_cfats[] = {
|
|
|
|
{ "valsort-attr", "attribute> <dn> <sort-type", 4, 5, 0, ARG_MAGIC,
|
|
|
|
valsort_cf_func, "( OLcfgOvAt:5.1 NAME 'olcValSortAttr' "
|
|
|
|
"DESC 'Sorting rule for attribute under given DN' "
|
|
|
|
"EQUALITY caseIgnoreMatch "
|
|
|
|
"SYNTAX OMsDirectoryString )", NULL, NULL },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static ConfigOCs valsort_cfocs[] = {
|
|
|
|
{ "( OLcfgOvOc:5.1 "
|
|
|
|
"NAME 'olcValSortConfig' "
|
|
|
|
"DESC 'Value Sorting configuration' "
|
|
|
|
"SUP olcOverlayConfig "
|
|
|
|
"MUST olcValSortAttr )",
|
|
|
|
Cft_Overlay, valsort_cfats },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static slap_verbmasks sorts[] = {
|
|
|
|
{ BER_BVC("alpha-ascend"), VALSORT_ASCEND|VALSORT_ALPHA },
|
|
|
|
{ BER_BVC("alpha-descend"), VALSORT_DESCEND|VALSORT_ALPHA },
|
|
|
|
{ BER_BVC("numeric-ascend"), VALSORT_ASCEND|VALSORT_NUMERIC },
|
2005-08-24 10:26:55 +08:00
|
|
|
{ BER_BVC("numeric-descend"), VALSORT_DESCEND|VALSORT_NUMERIC },
|
2005-08-23 16:47:38 +08:00
|
|
|
{ BER_BVC("weighted"), VALSORT_WEIGHTED },
|
|
|
|
{ BER_BVNULL, 0 }
|
|
|
|
};
|
|
|
|
|
2005-08-24 09:52:05 +08:00
|
|
|
static Syntax *syn_numericString;
|
|
|
|
|
2005-08-23 16:47:38 +08:00
|
|
|
static int
|
|
|
|
valsort_cf_func(ConfigArgs *c) {
|
|
|
|
slap_overinst *on = (slap_overinst *)c->bi;
|
|
|
|
valsort_info vitmp, *vi;
|
|
|
|
const char *text = NULL;
|
2005-08-24 09:52:05 +08:00
|
|
|
int i, is_numeric;
|
2005-08-24 09:34:49 +08:00
|
|
|
struct berval bv = BER_BVNULL;
|
2005-08-23 16:47:38 +08:00
|
|
|
|
|
|
|
if ( c->op == SLAP_CONFIG_EMIT ) {
|
|
|
|
for ( vi = on->on_bi.bi_private; vi; vi = vi->vi_next ) {
|
|
|
|
struct berval bv2 = BER_BVNULL, bvret;
|
|
|
|
char *ptr;
|
|
|
|
int len;
|
|
|
|
|
2005-08-24 16:32:03 +08:00
|
|
|
len = vi->vi_ad->ad_cname.bv_len + 1 + vi->vi_dn.bv_len + 2;
|
2005-08-23 16:47:38 +08:00
|
|
|
i = vi->vi_sort;
|
|
|
|
if ( i & VALSORT_WEIGHTED ) {
|
|
|
|
enum_to_verb( sorts, VALSORT_WEIGHTED, &bv2 );
|
|
|
|
len += bv2.bv_len + 1;
|
|
|
|
i ^= VALSORT_WEIGHTED;
|
|
|
|
}
|
2005-08-24 16:32:03 +08:00
|
|
|
if ( i ) {
|
|
|
|
enum_to_verb( sorts, i, &bv );
|
|
|
|
len += bv.bv_len + 1;
|
|
|
|
}
|
2005-08-23 16:47:38 +08:00
|
|
|
bvret.bv_val = ch_malloc( len+1 );
|
|
|
|
bvret.bv_len = len;
|
|
|
|
|
|
|
|
ptr = lutil_strcopy( bvret.bv_val, vi->vi_ad->ad_cname.bv_val );
|
|
|
|
*ptr++ = ' ';
|
|
|
|
*ptr++ = '"';
|
|
|
|
ptr = lutil_strcopy( ptr, vi->vi_dn.bv_val );
|
|
|
|
*ptr++ = '"';
|
|
|
|
if ( vi->vi_sort & VALSORT_WEIGHTED ) {
|
2005-08-24 09:34:49 +08:00
|
|
|
*ptr++ = ' ';
|
2005-08-23 16:47:38 +08:00
|
|
|
ptr = lutil_strcopy( ptr, bv2.bv_val );
|
2005-08-24 09:34:49 +08:00
|
|
|
}
|
2005-08-24 16:32:03 +08:00
|
|
|
if ( i ) {
|
2005-08-23 16:47:38 +08:00
|
|
|
*ptr++ = ' ';
|
2005-08-24 09:34:49 +08:00
|
|
|
strcpy( ptr, bv.bv_val );
|
2005-08-23 16:47:38 +08:00
|
|
|
}
|
|
|
|
ber_bvarray_add( &c->rvalue_vals, &bvret );
|
|
|
|
}
|
|
|
|
i = ( c->rvalue_vals != NULL ) ? 0 : 1;
|
|
|
|
return i;
|
|
|
|
} else if ( c->op == LDAP_MOD_DELETE ) {
|
|
|
|
if ( c->valx < 0 ) {
|
2005-08-24 16:32:03 +08:00
|
|
|
for ( vi = on->on_bi.bi_private; vi; vi = on->on_bi.bi_private ) {
|
2005-08-23 16:47:38 +08:00
|
|
|
on->on_bi.bi_private = vi->vi_next;
|
|
|
|
ch_free( vi->vi_dn.bv_val );
|
|
|
|
ch_free( vi );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
valsort_info **prev;
|
|
|
|
|
|
|
|
for (i=0, prev = (valsort_info **)&on->on_bi.bi_private,
|
|
|
|
vi = *prev; vi && i<c->valx;
|
|
|
|
prev = &vi->vi_next, vi = vi->vi_next, i++ );
|
|
|
|
(*prev)->vi_next = vi->vi_next;
|
|
|
|
ch_free( vi->vi_dn.bv_val );
|
|
|
|
ch_free( vi );
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
vitmp.vi_ad = NULL;
|
|
|
|
i = slap_str2ad( c->argv[1], &vitmp.vi_ad, &text );
|
|
|
|
if ( i ) {
|
2007-09-30 01:55:33 +08:00
|
|
|
snprintf( c->cr_msg, sizeof( c->cr_msg), "<%s> %s", c->argv[0], text );
|
2005-08-23 16:47:38 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
|
2007-08-03 20:02:07 +08:00
|
|
|
c->log, c->cr_msg, c->argv[1] );
|
2005-08-23 16:47:38 +08:00
|
|
|
return(1);
|
|
|
|
}
|
2005-08-24 09:34:49 +08:00
|
|
|
if ( is_at_single_value( vitmp.vi_ad->ad_type )) {
|
2007-09-30 01:55:33 +08:00
|
|
|
snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> %s is single-valued, ignoring", c->argv[0],
|
2005-08-24 09:34:49 +08:00
|
|
|
vitmp.vi_ad->ad_cname.bv_val );
|
|
|
|
Debug( LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
|
2007-08-03 20:02:07 +08:00
|
|
|
c->log, c->cr_msg, c->argv[1] );
|
2005-08-24 09:34:49 +08:00
|
|
|
return(0);
|
|
|
|
}
|
2005-08-24 09:52:05 +08:00
|
|
|
is_numeric = ( vitmp.vi_ad->ad_type->sat_syntax == syn_numericString ||
|
|
|
|
vitmp.vi_ad->ad_type->sat_syntax == slap_schema.si_syn_integer ) ? 1
|
|
|
|
: 0;
|
2005-08-23 16:47:38 +08:00
|
|
|
ber_str2bv( c->argv[2], 0, 0, &bv );
|
|
|
|
i = dnNormalize( 0, NULL, NULL, &bv, &vitmp.vi_dn, NULL );
|
|
|
|
if ( i ) {
|
2007-09-30 01:55:33 +08:00
|
|
|
snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to normalize DN", c->argv[0] );
|
2005-08-23 16:47:38 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
|
2007-08-03 20:02:07 +08:00
|
|
|
c->log, c->cr_msg, c->argv[2] );
|
2005-08-23 16:47:38 +08:00
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
i = verb_to_mask( c->argv[3], sorts );
|
|
|
|
if ( BER_BVISNULL( &sorts[i].word )) {
|
2007-09-30 01:55:33 +08:00
|
|
|
snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unrecognized sort type", c->argv[0] );
|
2005-08-23 16:47:38 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
|
2007-08-03 20:02:07 +08:00
|
|
|
c->log, c->cr_msg, c->argv[3] );
|
2005-08-23 16:47:38 +08:00
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
vitmp.vi_sort = sorts[i].mask;
|
|
|
|
if ( sorts[i].mask == VALSORT_WEIGHTED && c->argc == 5 ) {
|
|
|
|
i = verb_to_mask( c->argv[4], sorts );
|
|
|
|
if ( BER_BVISNULL( &sorts[i].word )) {
|
2007-09-30 01:55:33 +08:00
|
|
|
snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unrecognized sort type", c->argv[0] );
|
2005-08-23 16:47:38 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
|
2007-08-03 20:02:07 +08:00
|
|
|
c->log, c->cr_msg, c->argv[4] );
|
2005-08-23 16:47:38 +08:00
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
vitmp.vi_sort |= sorts[i].mask;
|
|
|
|
}
|
2005-08-24 09:52:05 +08:00
|
|
|
if (( vitmp.vi_sort & VALSORT_NUMERIC ) && !is_numeric ) {
|
2007-09-30 01:55:33 +08:00
|
|
|
snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> numeric sort specified for non-numeric syntax",
|
2005-08-24 09:52:05 +08:00
|
|
|
c->argv[0] );
|
|
|
|
Debug( LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
|
2007-08-03 20:02:07 +08:00
|
|
|
c->log, c->cr_msg, c->argv[1] );
|
2005-08-24 09:52:05 +08:00
|
|
|
return(1);
|
|
|
|
}
|
2005-08-23 16:47:38 +08:00
|
|
|
vi = ch_malloc( sizeof(valsort_info) );
|
|
|
|
*vi = vitmp;
|
|
|
|
vi->vi_next = on->on_bi.bi_private;
|
|
|
|
on->on_bi.bi_private = vi;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Use Insertion Sort algorithm on selected values */
|
|
|
|
static void
|
|
|
|
do_sort( Operation *op, Attribute *a, int beg, int num, slap_mask_t sort )
|
|
|
|
{
|
|
|
|
int i, j, gotnvals;
|
2005-11-07 07:27:09 +08:00
|
|
|
struct berval tmp, ntmp, *vals = NULL, *nvals;
|
2005-08-23 16:47:38 +08:00
|
|
|
|
|
|
|
gotnvals = (a->a_vals != a->a_nvals );
|
|
|
|
|
|
|
|
nvals = a->a_nvals + beg;
|
|
|
|
if ( gotnvals )
|
|
|
|
vals = a->a_vals + beg;
|
|
|
|
|
|
|
|
if ( sort & VALSORT_NUMERIC ) {
|
|
|
|
long *numbers = op->o_tmpalloc( num * sizeof(long), op->o_tmpmemctx ),
|
|
|
|
idx;
|
|
|
|
for (i=0; i<num; i++)
|
|
|
|
numbers[i] = strtol( nvals[i].bv_val, NULL, 0 );
|
|
|
|
|
|
|
|
for (i=1; i<num; i++) {
|
|
|
|
idx = numbers[i];
|
|
|
|
ntmp = nvals[i];
|
|
|
|
if ( gotnvals ) tmp = vals[i];
|
|
|
|
j = i;
|
|
|
|
while ( j>0 ) {
|
|
|
|
int cmp = (sort & VALSORT_DESCEND) ? numbers[j-1] < idx :
|
|
|
|
numbers[j-1] > idx;
|
|
|
|
if ( !cmp ) break;
|
|
|
|
numbers[j] = numbers[j-1];
|
|
|
|
nvals[j] = nvals[j-1];
|
|
|
|
if ( gotnvals ) vals[j] = vals[j-1];
|
|
|
|
j--;
|
|
|
|
}
|
|
|
|
numbers[j] = idx;
|
|
|
|
nvals[j] = ntmp;
|
|
|
|
if ( gotnvals ) vals[j] = tmp;
|
|
|
|
}
|
|
|
|
op->o_tmpfree( numbers, op->o_tmpmemctx );
|
|
|
|
} else {
|
|
|
|
for (i=1; i<num; i++) {
|
|
|
|
ntmp = nvals[i];
|
|
|
|
if ( gotnvals ) tmp = vals[i];
|
|
|
|
j = i;
|
|
|
|
while ( j>0 ) {
|
|
|
|
int cmp = strcmp( nvals[j-1].bv_val, ntmp.bv_val );
|
|
|
|
cmp = (sort & VALSORT_DESCEND) ? (cmp < 0) : (cmp > 0);
|
|
|
|
if ( !cmp ) break;
|
|
|
|
|
|
|
|
nvals[j] = nvals[j-1];
|
|
|
|
if ( gotnvals ) vals[j] = vals[j-1];
|
|
|
|
j--;
|
|
|
|
}
|
|
|
|
nvals[j] = ntmp;
|
|
|
|
if ( gotnvals ) vals[j] = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
valsort_response( Operation *op, SlapReply *rs )
|
|
|
|
{
|
|
|
|
slap_overinst *on;
|
|
|
|
valsort_info *vi;
|
|
|
|
Attribute *a;
|
|
|
|
|
2005-08-27 09:21:51 +08:00
|
|
|
/* If this is not a search response, or it is a syncrepl response,
|
|
|
|
* or the valsort control wants raw results, pass thru unmodified.
|
|
|
|
*/
|
|
|
|
if ( rs->sr_type != REP_SEARCH ||
|
|
|
|
( _SCM(op->o_sync) > SLAP_CONTROL_IGNORED ) ||
|
|
|
|
( op->o_ctrlflag[valsort_cid] & SLAP_CONTROL_DATA0))
|
|
|
|
return SLAP_CB_CONTINUE;
|
2005-08-25 14:56:09 +08:00
|
|
|
|
2005-08-23 16:47:38 +08:00
|
|
|
on = (slap_overinst *) op->o_bd->bd_info;
|
|
|
|
vi = on->on_bi.bi_private;
|
|
|
|
|
|
|
|
/* And we must have something configured */
|
|
|
|
if ( !vi ) return SLAP_CB_CONTINUE;
|
|
|
|
|
|
|
|
/* Find a rule whose baseDN matches this entry */
|
|
|
|
for (; vi; vi = vi->vi_next ) {
|
|
|
|
int i, n;
|
|
|
|
|
|
|
|
if ( !dnIsSuffix( &rs->sr_entry->e_nname, &vi->vi_dn ))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Find attr that this rule affects */
|
|
|
|
a = attr_find( rs->sr_entry->e_attrs, vi->vi_ad );
|
|
|
|
if ( !a ) continue;
|
|
|
|
|
2011-01-29 00:33:55 +08:00
|
|
|
if ( rs_entry2modifiable( op, rs, on )) {
|
2005-08-23 16:47:38 +08:00
|
|
|
a = attr_find( rs->sr_entry->e_attrs, vi->vi_ad );
|
|
|
|
}
|
|
|
|
|
2007-09-23 12:40:16 +08:00
|
|
|
n = a->a_numvals;
|
2005-08-23 16:47:38 +08:00
|
|
|
if ( vi->vi_sort & VALSORT_WEIGHTED ) {
|
|
|
|
int j, gotnvals;
|
|
|
|
long *index = op->o_tmpalloc( n * sizeof(long), op->o_tmpmemctx );
|
|
|
|
|
|
|
|
gotnvals = (a->a_vals != a->a_nvals );
|
|
|
|
|
|
|
|
for (i=0; i<n; i++) {
|
2005-11-14 06:26:53 +08:00
|
|
|
char *ptr = ber_bvchr( &a->a_nvals[i], '{' );
|
2005-08-23 16:47:38 +08:00
|
|
|
char *end = NULL;
|
|
|
|
if ( !ptr ) {
|
|
|
|
Debug(LDAP_DEBUG_TRACE, "weights missing from attr %s "
|
|
|
|
"in entry %s\n", vi->vi_ad->ad_cname.bv_val,
|
2019-02-16 00:49:52 +08:00
|
|
|
rs->sr_entry->e_name.bv_val );
|
2005-08-23 16:47:38 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
index[i] = strtol( ptr+1, &end, 0 );
|
|
|
|
if ( *end != '}' ) {
|
|
|
|
Debug(LDAP_DEBUG_TRACE, "weights misformatted "
|
|
|
|
"in entry %s\n",
|
2019-02-16 00:49:52 +08:00
|
|
|
rs->sr_entry->e_name.bv_val );
|
2005-08-23 16:47:38 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Strip out weights */
|
|
|
|
ptr = a->a_nvals[i].bv_val;
|
|
|
|
end++;
|
|
|
|
for (;*end;)
|
|
|
|
*ptr++ = *end++;
|
|
|
|
*ptr = '\0';
|
|
|
|
a->a_nvals[i].bv_len = ptr - a->a_nvals[i].bv_val;
|
|
|
|
|
|
|
|
if ( a->a_vals != a->a_nvals ) {
|
|
|
|
ptr = a->a_vals[i].bv_val;
|
2005-11-14 23:02:14 +08:00
|
|
|
end = ber_bvchr( &a->a_vals[i], '}' );
|
2005-11-14 06:26:53 +08:00
|
|
|
assert( end != NULL );
|
2005-11-14 23:02:14 +08:00
|
|
|
end++;
|
2005-08-23 16:47:38 +08:00
|
|
|
for (;*end;)
|
|
|
|
*ptr++ = *end++;
|
|
|
|
*ptr = '\0';
|
|
|
|
a->a_vals[i].bv_len = ptr - a->a_vals[i].bv_val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* An attr was missing weights here, ignore it */
|
|
|
|
if ( i<n ) {
|
|
|
|
op->o_tmpfree( index, op->o_tmpmemctx );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Insertion sort */
|
|
|
|
for ( i=1; i<n; i++) {
|
|
|
|
long idx = index[i];
|
|
|
|
struct berval tmp = a->a_vals[i], ntmp;
|
|
|
|
if ( gotnvals ) ntmp = a->a_nvals[i];
|
|
|
|
j = i;
|
|
|
|
while (( j>0 ) && (index[j-1] > idx )) {
|
|
|
|
index[j] = index[j-1];
|
|
|
|
a->a_vals[j] = a->a_vals[j-1];
|
|
|
|
if ( gotnvals ) a->a_nvals[j] = a->a_nvals[j-1];
|
|
|
|
j--;
|
|
|
|
}
|
|
|
|
index[j] = idx;
|
|
|
|
a->a_vals[j] = tmp;
|
|
|
|
if ( gotnvals ) a->a_nvals[j] = ntmp;
|
|
|
|
}
|
|
|
|
/* Check for secondary sort */
|
|
|
|
if ( vi->vi_sort ^ VALSORT_WEIGHTED ) {
|
|
|
|
for ( i=0; i<n;) {
|
|
|
|
for (j=i+1; j<n; j++) {
|
|
|
|
if (index[i] != index[j])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if( j-i > 1 )
|
|
|
|
do_sort( op, a, i, j-i, vi->vi_sort );
|
|
|
|
i = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
op->o_tmpfree( index, op->o_tmpmemctx );
|
|
|
|
} else {
|
|
|
|
do_sort( op, a, 0, n, vi->vi_sort );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SLAP_CB_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
valsort_add( Operation *op, SlapReply *rs )
|
|
|
|
{
|
|
|
|
slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
|
|
|
|
valsort_info *vi = on->on_bi.bi_private;
|
|
|
|
|
|
|
|
Attribute *a;
|
|
|
|
int i;
|
|
|
|
char *ptr, *end;
|
|
|
|
|
|
|
|
/* See if any weighted sorting applies to this entry */
|
|
|
|
for ( ;vi;vi=vi->vi_next ) {
|
|
|
|
if ( !dnIsSuffix( &op->o_req_ndn, &vi->vi_dn ))
|
|
|
|
continue;
|
|
|
|
if ( !(vi->vi_sort & VALSORT_WEIGHTED ))
|
|
|
|
continue;
|
|
|
|
a = attr_find( op->ora_e->e_attrs, vi->vi_ad );
|
|
|
|
if ( !a )
|
|
|
|
continue;
|
|
|
|
for (i=0; !BER_BVISNULL( &a->a_vals[i] ); i++) {
|
2005-11-14 06:26:53 +08:00
|
|
|
ptr = ber_bvchr(&a->a_vals[i], '{' );
|
2005-08-23 16:47:38 +08:00
|
|
|
if ( !ptr ) {
|
|
|
|
Debug(LDAP_DEBUG_TRACE, "weight missing from attribute %s\n",
|
2019-02-16 00:49:52 +08:00
|
|
|
vi->vi_ad->ad_cname.bv_val );
|
2005-08-23 16:47:38 +08:00
|
|
|
send_ldap_error( op, rs, LDAP_CONSTRAINT_VIOLATION,
|
|
|
|
"weight missing from attribute" );
|
|
|
|
return rs->sr_err;
|
|
|
|
}
|
|
|
|
strtol( ptr+1, &end, 0 );
|
|
|
|
if ( *end != '}' ) {
|
|
|
|
Debug(LDAP_DEBUG_TRACE, "weight is misformatted in %s\n",
|
2019-02-16 00:49:52 +08:00
|
|
|
vi->vi_ad->ad_cname.bv_val );
|
2005-08-23 16:47:38 +08:00
|
|
|
send_ldap_error( op, rs, LDAP_CONSTRAINT_VIOLATION,
|
|
|
|
"weight is misformatted" );
|
|
|
|
return rs->sr_err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SLAP_CB_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
valsort_modify( Operation *op, SlapReply *rs )
|
|
|
|
{
|
|
|
|
slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
|
|
|
|
valsort_info *vi = on->on_bi.bi_private;
|
|
|
|
|
|
|
|
Modifications *ml;
|
|
|
|
int i;
|
|
|
|
char *ptr, *end;
|
|
|
|
|
|
|
|
/* See if any weighted sorting applies to this entry */
|
|
|
|
for ( ;vi;vi=vi->vi_next ) {
|
|
|
|
if ( !dnIsSuffix( &op->o_req_ndn, &vi->vi_dn ))
|
|
|
|
continue;
|
|
|
|
if ( !(vi->vi_sort & VALSORT_WEIGHTED ))
|
|
|
|
continue;
|
|
|
|
for (ml = op->orm_modlist; ml; ml=ml->sml_next ) {
|
2007-05-25 08:58:59 +08:00
|
|
|
/* Must be a Delete Attr op, so no values to consider */
|
|
|
|
if ( !ml->sml_values )
|
|
|
|
continue;
|
2005-08-23 16:47:38 +08:00
|
|
|
if ( ml->sml_desc == vi->vi_ad )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( !ml )
|
|
|
|
continue;
|
|
|
|
for (i=0; !BER_BVISNULL( &ml->sml_values[i] ); i++) {
|
2005-11-14 06:26:53 +08:00
|
|
|
ptr = ber_bvchr(&ml->sml_values[i], '{' );
|
2005-08-23 16:47:38 +08:00
|
|
|
if ( !ptr ) {
|
|
|
|
Debug(LDAP_DEBUG_TRACE, "weight missing from attribute %s\n",
|
2019-02-16 00:49:52 +08:00
|
|
|
vi->vi_ad->ad_cname.bv_val );
|
2005-08-23 16:47:38 +08:00
|
|
|
send_ldap_error( op, rs, LDAP_CONSTRAINT_VIOLATION,
|
|
|
|
"weight missing from attribute" );
|
|
|
|
return rs->sr_err;
|
|
|
|
}
|
|
|
|
strtol( ptr+1, &end, 0 );
|
|
|
|
if ( *end != '}' ) {
|
|
|
|
Debug(LDAP_DEBUG_TRACE, "weight is misformatted in %s\n",
|
2019-02-16 00:49:52 +08:00
|
|
|
vi->vi_ad->ad_cname.bv_val );
|
2005-08-23 16:47:38 +08:00
|
|
|
send_ldap_error( op, rs, LDAP_CONSTRAINT_VIOLATION,
|
|
|
|
"weight is misformatted" );
|
|
|
|
return rs->sr_err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SLAP_CB_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2005-08-27 09:21:51 +08:00
|
|
|
static int
|
|
|
|
valsort_db_open(
|
2007-07-26 01:21:05 +08:00
|
|
|
BackendDB *be,
|
2007-08-03 20:02:07 +08:00
|
|
|
ConfigReply *cr
|
2005-08-27 09:21:51 +08:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return overlay_register_control( be, LDAP_CONTROL_VALSORT );
|
|
|
|
}
|
|
|
|
|
2005-08-23 16:47:38 +08:00
|
|
|
static int
|
|
|
|
valsort_destroy(
|
2007-07-26 01:21:05 +08:00
|
|
|
BackendDB *be,
|
2007-08-03 20:02:07 +08:00
|
|
|
ConfigReply *cr
|
2005-08-23 16:47:38 +08:00
|
|
|
)
|
|
|
|
{
|
|
|
|
slap_overinst *on = (slap_overinst *)be->bd_info;
|
|
|
|
valsort_info *vi = on->on_bi.bi_private, *next;
|
|
|
|
|
2010-07-01 19:51:43 +08:00
|
|
|
#ifdef SLAP_CONFIG_DELETE
|
|
|
|
overlay_unregister_control( be, LDAP_CONTROL_VALSORT );
|
|
|
|
#endif /* SLAP_CONFIG_DELETE */
|
|
|
|
|
2005-08-23 16:47:38 +08:00
|
|
|
for (; vi; vi = next) {
|
|
|
|
next = vi->vi_next;
|
|
|
|
ch_free( vi->vi_dn.bv_val );
|
|
|
|
ch_free( vi );
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-08-24 10:07:43 +08:00
|
|
|
static int
|
|
|
|
valsort_parseCtrl(
|
|
|
|
Operation *op,
|
|
|
|
SlapReply *rs,
|
|
|
|
LDAPControl *ctrl )
|
|
|
|
{
|
2005-08-27 09:21:51 +08:00
|
|
|
ber_tag_t tag;
|
|
|
|
BerElementBuffer berbuf;
|
|
|
|
BerElement *ber = (BerElement *)&berbuf;
|
|
|
|
ber_int_t flag = 0;
|
|
|
|
|
2007-04-21 06:32:58 +08:00
|
|
|
if ( BER_BVISNULL( &ctrl->ldctl_value )) {
|
|
|
|
rs->sr_text = "valSort control value is absent";
|
|
|
|
return LDAP_PROTOCOL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
|
|
|
|
rs->sr_text = "valSort control value is empty";
|
2005-08-27 09:21:51 +08:00
|
|
|
return LDAP_PROTOCOL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ber_init2( ber, &ctrl->ldctl_value, 0 );
|
|
|
|
if (( tag = ber_scanf( ber, "{b}", &flag )) == LBER_ERROR ) {
|
|
|
|
rs->sr_text = "valSort control: flag decoding error";
|
2005-08-24 10:07:43 +08:00
|
|
|
return LDAP_PROTOCOL_ERROR;
|
|
|
|
}
|
2005-08-27 09:21:51 +08:00
|
|
|
|
2005-08-24 10:07:43 +08:00
|
|
|
op->o_ctrlflag[valsort_cid] = ctrl->ldctl_iscritical ?
|
|
|
|
SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
|
2005-08-27 09:21:51 +08:00
|
|
|
if ( flag )
|
|
|
|
op->o_ctrlflag[valsort_cid] |= SLAP_CONTROL_DATA0;
|
2005-08-24 10:07:43 +08:00
|
|
|
|
|
|
|
return LDAP_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2005-08-23 16:47:38 +08:00
|
|
|
static slap_overinst valsort;
|
|
|
|
|
2005-11-25 20:43:43 +08:00
|
|
|
int valsort_initialize( void )
|
2005-08-23 16:47:38 +08:00
|
|
|
{
|
2005-08-27 05:42:48 +08:00
|
|
|
int rc;
|
2005-08-23 16:47:38 +08:00
|
|
|
|
|
|
|
valsort.on_bi.bi_type = "valsort";
|
|
|
|
valsort.on_bi.bi_db_destroy = valsort_destroy;
|
2005-08-27 09:21:51 +08:00
|
|
|
valsort.on_bi.bi_db_open = valsort_db_open;
|
2005-08-23 16:47:38 +08:00
|
|
|
|
|
|
|
valsort.on_bi.bi_op_add = valsort_add;
|
|
|
|
valsort.on_bi.bi_op_modify = valsort_modify;
|
|
|
|
|
|
|
|
valsort.on_response = valsort_response;
|
|
|
|
|
|
|
|
valsort.on_bi.bi_cf_ocs = valsort_cfocs;
|
|
|
|
|
2005-08-24 10:07:43 +08:00
|
|
|
rc = register_supported_control( LDAP_CONTROL_VALSORT,
|
|
|
|
SLAP_CTRL_SEARCH | SLAP_CTRL_HIDE, NULL, valsort_parseCtrl,
|
|
|
|
&valsort_cid );
|
|
|
|
if ( rc != LDAP_SUCCESS ) {
|
2019-02-16 00:49:52 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "Failed to register control %d\n", rc );
|
2005-08-24 10:07:43 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2005-08-24 09:52:05 +08:00
|
|
|
syn_numericString = syn_find( "1.3.6.1.4.1.1466.115.121.1.36" );
|
|
|
|
|
2005-08-23 16:47:38 +08:00
|
|
|
rc = config_register_schema( valsort_cfats, valsort_cfocs );
|
|
|
|
if ( rc ) return rc;
|
|
|
|
|
|
|
|
return overlay_register(&valsort);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if SLAPD_OVER_VALSORT == SLAPD_MOD_DYNAMIC
|
|
|
|
int init_module( int argc, char *argv[]) {
|
2005-11-25 20:43:43 +08:00
|
|
|
return valsort_initialize();
|
2005-08-23 16:47:38 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* SLAPD_OVER_VALSORT */
|