Debian Codegraph @

servers/slapd/overlays/valsort.c

raw
1 /* valsort.c - sort attribute values */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 2005-2024 The OpenLDAP Foundation.
6 * Portions copyright 2005 Symas Corporation.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
11 * Public License.
12 *
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
16 */
17 /* ACKNOWLEDGEMENTS:
18 * This work was initially developed by Howard Chu for inclusion in
19 * OpenLDAP Software. This work was sponsored by Stanford University.
20 */
21
22 /*
23 * This overlay sorts the values of multi-valued attributes when returning
24 * them in a search response.
25 */
26 #include "portable.h"
27
28 #ifdef SLAPD_OVER_VALSORT
29
30 #include <stdio.h>
31
32 #include <ac/string.h>
33 #include <ac/ctype.h>
34
35 #include "slap.h"
36 #include "slap-config.h"
37 #include "lutil.h"
38
39 #define VALSORT_ASCEND 0
40 #define VALSORT_DESCEND 1
41
42 #define VALSORT_ALPHA 2
43 #define VALSORT_NUMERIC 4
44
45 #define VALSORT_WEIGHTED 8
46
47 typedef struct valsort_info {
48 struct valsort_info *vi_next;
49 struct berval vi_dn;
50 AttributeDescription *vi_ad;
51 slap_mask_t vi_sort;
52 } valsort_info;
53
54 static int valsort_cid;
55
56 static ConfigDriver valsort_cf_func;
57
58 static ConfigTable valsort_cfats[] = {
59 { "valsort-attr", "attribute> <dn> <sort-type", 4, 5, 0, ARG_MAGIC,
60 valsort_cf_func, "( OLcfgOvAt:5.1 NAME 'olcValSortAttr' "
61 "DESC 'Sorting rule for attribute under given DN' "
62 "EQUALITY caseIgnoreMatch "
63 "SYNTAX OMsDirectoryString )", NULL, NULL },
64 { NULL }
65 };
66
67 static ConfigOCs valsort_cfocs[] = {
68 { "( OLcfgOvOc:5.1 "
69 "NAME 'olcValSortConfig' "
70 "DESC 'Value Sorting configuration' "
71 "SUP olcOverlayConfig "
72 "MUST olcValSortAttr )",
73 Cft_Overlay, valsort_cfats },
74 { NULL }
75 };
76
77 static slap_verbmasks sorts[] = {
78 { BER_BVC("alpha-ascend"), VALSORT_ASCEND|VALSORT_ALPHA },
79 { BER_BVC("alpha-descend"), VALSORT_DESCEND|VALSORT_ALPHA },
80 { BER_BVC("numeric-ascend"), VALSORT_ASCEND|VALSORT_NUMERIC },
81 { BER_BVC("numeric-descend"), VALSORT_DESCEND|VALSORT_NUMERIC },
82 { BER_BVC("weighted"), VALSORT_WEIGHTED },
83 { BER_BVNULL, 0 }
84 };
85
86 static Syntax *syn_numericString;
87
88 static int
89 valsort_cf_func(ConfigArgs *c) {
90 slap_overinst *on = (slap_overinst *)c->bi;
91 valsort_info vitmp, *vi, **vip;
92 const char *text = NULL;
93 int i, is_numeric;
94 struct berval bv = BER_BVNULL;
95
96 if ( c->op == SLAP_CONFIG_EMIT ) {
97 for ( vi = on->on_bi.bi_private; vi; vi = vi->vi_next ) {
98 struct berval bv2 = BER_BVNULL, bvret;
99 char *ptr;
100 int len;
101
102 len = vi->vi_ad->ad_cname.bv_len + 1 + vi->vi_dn.bv_len + 2;
103 i = vi->vi_sort;
104 if ( i & VALSORT_WEIGHTED ) {
105 enum_to_verb( sorts, VALSORT_WEIGHTED, &bv2 );
106 len += bv2.bv_len + 1;
107 i ^= VALSORT_WEIGHTED;
108 }
109 if ( i ) {
110 enum_to_verb( sorts, i, &bv );
111 len += bv.bv_len + 1;
112 }
113 bvret.bv_val = ch_malloc( len+1 );
114 bvret.bv_len = len;
115
116 ptr = lutil_strcopy( bvret.bv_val, vi->vi_ad->ad_cname.bv_val );
117 *ptr++ = ' ';
118 *ptr++ = '"';
119 ptr = lutil_strcopy( ptr, vi->vi_dn.bv_val );
120 *ptr++ = '"';
121 if ( vi->vi_sort & VALSORT_WEIGHTED ) {
122 *ptr++ = ' ';
123 ptr = lutil_strcopy( ptr, bv2.bv_val );
124 }
125 if ( i ) {
126 *ptr++ = ' ';
127 strcpy( ptr, bv.bv_val );
128 }
129 ber_bvarray_add( &c->rvalue_vals, &bvret );
130 }
131 i = ( c->rvalue_vals != NULL ) ? 0 : 1;
132 return i;
133 } else if ( c->op == LDAP_MOD_DELETE ) {
134 if ( c->valx < 0 ) {
135 for ( vi = on->on_bi.bi_private; vi; vi = on->on_bi.bi_private ) {
136 on->on_bi.bi_private = vi->vi_next;
137 ch_free( vi->vi_dn.bv_val );
138 ch_free( vi );
139 }
140 } else {
141 valsort_info **prev;
142
143 for (i=0, prev = (valsort_info **)&on->on_bi.bi_private,
144 vi = *prev; vi && i<c->valx;
145 prev = &vi->vi_next, vi = vi->vi_next, i++ );
146 (*prev)->vi_next = vi->vi_next;
147 ch_free( vi->vi_dn.bv_val );
148 ch_free( vi );
149 }
150 return 0;
151 }
152 vitmp.vi_ad = NULL;
153 i = slap_str2ad( c->argv[1], &vitmp.vi_ad, &text );
154 if ( i ) {
155 snprintf( c->cr_msg, sizeof( c->cr_msg), "<%s> %s", c->argv[0], text );
156 Debug( LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
157 c->log, c->cr_msg, c->argv[1] );
158 return(1);
159 }
160 if ( is_at_single_value( vitmp.vi_ad->ad_type )) {
161 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> %s is single-valued, ignoring", c->argv[0],
162 vitmp.vi_ad->ad_cname.bv_val );
163 Debug( LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
164 c->log, c->cr_msg, c->argv[1] );
165 return(0);
166 }
167 is_numeric = ( vitmp.vi_ad->ad_type->sat_syntax == syn_numericString ||
168 vitmp.vi_ad->ad_type->sat_syntax == slap_schema.si_syn_integer ) ? 1
169 : 0;
170 ber_str2bv( c->argv[2], 0, 0, &bv );
171 i = dnNormalize( 0, NULL, NULL, &bv, &vitmp.vi_dn, NULL );
172 if ( i ) {
173 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to normalize DN", c->argv[0] );
174 Debug( LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
175 c->log, c->cr_msg, c->argv[2] );
176 return(1);
177 }
178 i = verb_to_mask( c->argv[3], sorts );
179 if ( BER_BVISNULL( &sorts[i].word )) {
180 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unrecognized sort type", c->argv[0] );
181 Debug( LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
182 c->log, c->cr_msg, c->argv[3] );
183 return(1);
184 }
185 vitmp.vi_sort = sorts[i].mask;
186 if ( sorts[i].mask == VALSORT_WEIGHTED && c->argc == 5 ) {
187 i = verb_to_mask( c->argv[4], sorts );
188 if ( BER_BVISNULL( &sorts[i].word )) {
189 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unrecognized sort type", c->argv[0] );
190 Debug( LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
191 c->log, c->cr_msg, c->argv[4] );
192 return(1);
193 }
194 vitmp.vi_sort |= sorts[i].mask;
195 }
196 if (( vitmp.vi_sort & VALSORT_NUMERIC ) && !is_numeric ) {
197 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> numeric sort specified for non-numeric syntax",
198 c->argv[0] );
199 Debug( LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
200 c->log, c->cr_msg, c->argv[1] );
201 return(1);
202 }
203
204 for ( vip = (valsort_info **)&on->on_bi.bi_private; *vip; vip = &(*vip)->vi_next )
205 /* Get to the end */ ;
206
207 vi = ch_malloc( sizeof(valsort_info) );
208 *vi = vitmp;
209 vi->vi_next = *vip;
210 *vip = vi;
211 return 0;
212 }
213
214 /* Use Insertion Sort algorithm on selected values */
215 static void
216 do_sort( Operation *op, Attribute *a, int beg, int num, slap_mask_t sort )
217 {
218 int i, j, gotnvals;
219 struct berval tmp, ntmp, *vals = NULL, *nvals;
220
221 gotnvals = (a->a_vals != a->a_nvals );
222
223 nvals = a->a_nvals + beg;
224 if ( gotnvals )
225 vals = a->a_vals + beg;
226
227 if ( sort & VALSORT_NUMERIC ) {
228 long *numbers = op->o_tmpalloc( num * sizeof(long), op->o_tmpmemctx ),
229 idx;
230 for (i=0; i<num; i++)
231 numbers[i] = strtol( nvals[i].bv_val, NULL, 0 );
232
233 for (i=1; i<num; i++) {
234 idx = numbers[i];
235 ntmp = nvals[i];
236 if ( gotnvals ) tmp = vals[i];
237 j = i;
238 while ( j>0 ) {
239 int cmp = (sort & VALSORT_DESCEND) ? numbers[j-1] < idx :
240 numbers[j-1] > idx;
241 if ( !cmp ) break;
242 numbers[j] = numbers[j-1];
243 nvals[j] = nvals[j-1];
244 if ( gotnvals ) vals[j] = vals[j-1];
245 j--;
246 }
247 numbers[j] = idx;
248 nvals[j] = ntmp;
249 if ( gotnvals ) vals[j] = tmp;
250 }
251 op->o_tmpfree( numbers, op->o_tmpmemctx );
252 } else {
253 for (i=1; i<num; i++) {
254 ntmp = nvals[i];
255 if ( gotnvals ) tmp = vals[i];
256 j = i;
257 while ( j>0 ) {
258 int cmp = strcmp( nvals[j-1].bv_val, ntmp.bv_val );
259 cmp = (sort & VALSORT_DESCEND) ? (cmp < 0) : (cmp > 0);
260 if ( !cmp ) break;
261
262 nvals[j] = nvals[j-1];
263 if ( gotnvals ) vals[j] = vals[j-1];
264 j--;
265 }
266 nvals[j] = ntmp;
267 if ( gotnvals ) vals[j] = tmp;
268 }
269 }
270 }
271
272 static int
273 valsort_response( Operation *op, SlapReply *rs )
274 {
275 slap_overinst *on;
276 valsort_info *vi;
277 Attribute *a;
278
279 /* If this is not a search response, or it is a syncrepl response,
280 * or the valsort control wants raw results, pass thru unmodified.
281 */
282 if ( rs->sr_type != REP_SEARCH ||
283 ( _SCM(op->o_sync) > SLAP_CONTROL_IGNORED ) ||
284 ( op->o_ctrlflag[valsort_cid] & SLAP_CONTROL_DATA0))
285 return SLAP_CB_CONTINUE;
286
287 on = (slap_overinst *) op->o_bd->bd_info;
288 vi = on->on_bi.bi_private;
289
290 /* And we must have something configured */
291 if ( !vi ) return SLAP_CB_CONTINUE;
292
293 /* Find a rule whose baseDN matches this entry */
294 for (; vi; vi = vi->vi_next ) {
295 int i, n;
296
297 if ( !dnIsSuffix( &rs->sr_entry->e_nname, &vi->vi_dn ))
298 continue;
299
300 /* Find attr that this rule affects */
301 a = attr_find( rs->sr_entry->e_attrs, vi->vi_ad );
302 if ( !a ) continue;
303
304 if ( rs_entry2modifiable( op, rs, on )) {
305 a = attr_find( rs->sr_entry->e_attrs, vi->vi_ad );
306 }
307
308 n = a->a_numvals;
309 if ( vi->vi_sort & VALSORT_WEIGHTED ) {
310 int j, gotnvals;
311 long *index = op->o_tmpalloc( n * sizeof(long), op->o_tmpmemctx );
312
313 gotnvals = (a->a_vals != a->a_nvals );
314
315 for (i=0; i<n; i++) {
316 char *ptr = ber_bvchr( &a->a_nvals[i], '{' );
317 char *end = NULL;
318 if ( !ptr ) {
319 Debug(LDAP_DEBUG_TRACE, "weights missing from attr %s "
320 "in entry %s\n", vi->vi_ad->ad_cname.bv_val,
321 rs->sr_entry->e_name.bv_val );
322 break;
323 }
324 index[i] = strtol( ptr+1, &end, 0 );
325 if ( *end != '}' ) {
326 Debug(LDAP_DEBUG_TRACE, "weights misformatted "
327 "in entry %s\n",
328 rs->sr_entry->e_name.bv_val );
329 break;
330 }
331 /* Strip out weights */
332 ptr = a->a_nvals[i].bv_val;
333 end++;
334 for (;*end;)
335 *ptr++ = *end++;
336 *ptr = '\0';
337 a->a_nvals[i].bv_len = ptr - a->a_nvals[i].bv_val;
338
339 if ( a->a_vals != a->a_nvals ) {
340 ptr = a->a_vals[i].bv_val;
341 end = ber_bvchr( &a->a_vals[i], '}' );
342 assert( end != NULL );
343 end++;
344 for (;*end;)
345 *ptr++ = *end++;
346 *ptr = '\0';
347 a->a_vals[i].bv_len = ptr - a->a_vals[i].bv_val;
348 }
349 }
350 /* An attr was missing weights here, ignore it */
351 if ( i<n ) {
352 op->o_tmpfree( index, op->o_tmpmemctx );
353 continue;
354 }
355 /* Insertion sort */
356 for ( i=1; i<n; i++) {
357 long idx = index[i];
358 struct berval tmp = a->a_vals[i], ntmp;
359 if ( gotnvals ) ntmp = a->a_nvals[i];
360 j = i;
361 while (( j>0 ) && (index[j-1] > idx )) {
362 index[j] = index[j-1];
363 a->a_vals[j] = a->a_vals[j-1];
364 if ( gotnvals ) a->a_nvals[j] = a->a_nvals[j-1];
365 j--;
366 }
367 index[j] = idx;
368 a->a_vals[j] = tmp;
369 if ( gotnvals ) a->a_nvals[j] = ntmp;
370 }
371 /* Check for secondary sort */
372 if ( vi->vi_sort ^ VALSORT_WEIGHTED ) {
373 for ( i=0; i<n;) {
374 for (j=i+1; j<n; j++) {
375 if (index[i] != index[j])
376 break;
377 }
378 if( j-i > 1 )
379 do_sort( op, a, i, j-i, vi->vi_sort );
380 i = j;
381 }
382 }
383 op->o_tmpfree( index, op->o_tmpmemctx );
384 } else {
385 do_sort( op, a, 0, n, vi->vi_sort );
386 }
387 }
388 return SLAP_CB_CONTINUE;
389 }
390
391 static int
392 valsort_add( Operation *op, SlapReply *rs )
393 {
394 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
395 valsort_info *vi = on->on_bi.bi_private;
396
397 Attribute *a;
398 int i;
399 char *ptr, *end;
400
401 /* See if any weighted sorting applies to this entry */
402 for ( ;vi;vi=vi->vi_next ) {
403 if ( !dnIsSuffix( &op->o_req_ndn, &vi->vi_dn ))
404 continue;
405 if ( !(vi->vi_sort & VALSORT_WEIGHTED ))
406 continue;
407 a = attr_find( op->ora_e->e_attrs, vi->vi_ad );
408 if ( !a )
409 continue;
410 for (i=0; !BER_BVISNULL( &a->a_vals[i] ); i++) {
411 ptr = ber_bvchr(&a->a_vals[i], '{' );
412 if ( !ptr ) {
413 Debug(LDAP_DEBUG_TRACE, "weight missing from attribute %s\n",
414 vi->vi_ad->ad_cname.bv_val );
415 send_ldap_error( op, rs, LDAP_CONSTRAINT_VIOLATION,
416 "weight missing from attribute" );
417 return rs->sr_err;
418 }
419 strtol( ptr+1, &end, 0 );
420 if ( *end != '}' ) {
421 Debug(LDAP_DEBUG_TRACE, "weight is misformatted in %s\n",
422 vi->vi_ad->ad_cname.bv_val );
423 send_ldap_error( op, rs, LDAP_CONSTRAINT_VIOLATION,
424 "weight is misformatted" );
425 return rs->sr_err;
426 }
427 }
428 }
429 return SLAP_CB_CONTINUE;
430 }
431
432 static int
433 valsort_modify( Operation *op, SlapReply *rs )
434 {
435 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
436 valsort_info *vi = on->on_bi.bi_private;
437
438 Modifications *ml;
439 int i;
440 char *ptr, *end;
441
442 /* See if any weighted sorting applies to this entry */
443 for ( ;vi;vi=vi->vi_next ) {
444 if ( !dnIsSuffix( &op->o_req_ndn, &vi->vi_dn ))
445 continue;
446 if ( !(vi->vi_sort & VALSORT_WEIGHTED ))
447 continue;
448 for (ml = op->orm_modlist; ml; ml=ml->sml_next ) {
449 /* Must be a Delete Attr op, so no values to consider */
450 if ( !ml->sml_values )
451 continue;
452 if ( ml->sml_desc == vi->vi_ad )
453 break;
454 }
455 if ( !ml )
456 continue;
457 for (i=0; !BER_BVISNULL( &ml->sml_values[i] ); i++) {
458 ptr = ber_bvchr(&ml->sml_values[i], '{' );
459 if ( !ptr ) {
460 Debug(LDAP_DEBUG_TRACE, "weight missing from attribute %s\n",
461 vi->vi_ad->ad_cname.bv_val );
462 send_ldap_error( op, rs, LDAP_CONSTRAINT_VIOLATION,
463 "weight missing from attribute" );
464 return rs->sr_err;
465 }
466 strtol( ptr+1, &end, 0 );
467 if ( *end != '}' ) {
468 Debug(LDAP_DEBUG_TRACE, "weight is misformatted in %s\n",
469 vi->vi_ad->ad_cname.bv_val );
470 send_ldap_error( op, rs, LDAP_CONSTRAINT_VIOLATION,
471 "weight is misformatted" );
472 return rs->sr_err;
473 }
474 }
475 }
476 return SLAP_CB_CONTINUE;
477 }
478
479 static int
480 valsort_db_open(
481 BackendDB *be,
482 ConfigReply *cr
483 )
484 {
485 return overlay_register_control( be, LDAP_CONTROL_VALSORT );
486 }
487
488 static int
489 valsort_destroy(
490 BackendDB *be,
491 ConfigReply *cr
492 )
493 {
494 slap_overinst *on = (slap_overinst *)be->bd_info;
495 valsort_info *vi = on->on_bi.bi_private, *next;
496
497 #ifdef SLAP_CONFIG_DELETE
498 overlay_unregister_control( be, LDAP_CONTROL_VALSORT );
499 #endif /* SLAP_CONFIG_DELETE */
500
501 for (; vi; vi = next) {
502 next = vi->vi_next;
503 ch_free( vi->vi_dn.bv_val );
504 ch_free( vi );
505 }
506
507 return 0;
508 }
509
510 static int
511 valsort_parseCtrl(
512 Operation *op,
513 SlapReply *rs,
514 LDAPControl *ctrl )
515 {
516 ber_tag_t tag;
517 BerElementBuffer berbuf;
518 BerElement *ber = (BerElement *)&berbuf;
519 ber_int_t flag = 0;
520
521 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
522 rs->sr_text = "valSort control value is absent";
523 return LDAP_PROTOCOL_ERROR;
524 }
525
526 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
527 rs->sr_text = "valSort control value is empty";
528 return LDAP_PROTOCOL_ERROR;
529 }
530
531 ber_init2( ber, &ctrl->ldctl_value, 0 );
532 if (( tag = ber_scanf( ber, "{b}", &flag )) == LBER_ERROR ) {
533 rs->sr_text = "valSort control: flag decoding error";
534 return LDAP_PROTOCOL_ERROR;
535 }
536
537 op->o_ctrlflag[valsort_cid] = ctrl->ldctl_iscritical ?
538 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
539 if ( flag )
540 op->o_ctrlflag[valsort_cid] |= SLAP_CONTROL_DATA0;
541
542 return LDAP_SUCCESS;
543 }
544
545 static slap_overinst valsort;
546
547 int valsort_initialize( void )
548 {
549 int rc;
550
551 valsort.on_bi.bi_type = "valsort";
552 valsort.on_bi.bi_flags = SLAPO_BFLAG_SINGLE;
553 valsort.on_bi.bi_db_destroy = valsort_destroy;
554 valsort.on_bi.bi_db_open = valsort_db_open;
555
556 valsort.on_bi.bi_op_add = valsort_add;
557 valsort.on_bi.bi_op_modify = valsort_modify;
558
559 valsort.on_response = valsort_response;
560
561 valsort.on_bi.bi_cf_ocs = valsort_cfocs;
562
563 rc = register_supported_control( LDAP_CONTROL_VALSORT,
564 SLAP_CTRL_SEARCH | SLAP_CTRL_HIDE, NULL, valsort_parseCtrl,
565 &valsort_cid );
566 if ( rc != LDAP_SUCCESS ) {
567 Debug( LDAP_DEBUG_ANY, "Failed to register control %d\n", rc );
568 return rc;
569 }
570
571 syn_numericString = syn_find( "1.3.6.1.4.1.1466.115.121.1.36" );
572
573 rc = config_register_schema( valsort_cfats, valsort_cfocs );
574 if ( rc ) return rc;
575
576 return overlay_register(&valsort);
577 }
578
579 #if SLAPD_OVER_VALSORT == SLAPD_MOD_DYNAMIC
580 int init_module( int argc, char *argv[]) {
581 return valsort_initialize();
582 }
583 #endif
584
585 #endif /* SLAPD_OVER_VALSORT */