1 /* value.c - routines for dealing with values */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/socket.h>
14 #include <ac/string.h>
30 for ( nn = 0; addvals != NULL && addvals[nn].bv_val != NULL; nn++ )
33 if ( *vals == NULL ) {
34 *vals = (BerVarray) SLAP_MALLOC( (nn + 1)
35 * sizeof(struct berval) );
38 LDAP_LOG( OPERATION, ERR,
39 "value_add: SLAP_MALLOC failed.\n", 0, 0, 0 );
41 Debug(LDAP_DEBUG_TRACE,
42 "value_add: SLAP_MALLOC failed.\n", 0, 0, 0 );
44 return LBER_ERROR_MEMORY;
48 for ( n = 0; (*vals)[n].bv_val != NULL; n++ ) {
51 *vals = (BerVarray) SLAP_REALLOC( (char *) *vals,
52 (n + nn + 1) * sizeof(struct berval) );
55 LDAP_LOG( OPERATION, ERR,
56 "value_add: SLAP_MALLOC failed.\n", 0, 0, 0 );
58 Debug(LDAP_DEBUG_TRACE,
59 "value_add: SLAP_MALLOC failed.\n", 0, 0, 0 );
61 return LBER_ERROR_MEMORY;
66 for ( ; addvals->bv_val; v2++, addvals++ ) {
67 ber_dupbv(v2, addvals);
68 if (v2->bv_val == NULL) break;
85 if ( *vals == NULL ) {
86 *vals = (BerVarray) SLAP_MALLOC( 2 * sizeof(struct berval) );
89 LDAP_LOG( OPERATION, ERR,
90 "value_add_one: SLAP_MALLOC failed.\n", 0, 0, 0 );
92 Debug(LDAP_DEBUG_TRACE,
93 "value_add_one: SLAP_MALLOC failed.\n", 0, 0, 0 );
95 return LBER_ERROR_MEMORY;
99 for ( n = 0; (*vals)[n].bv_val != NULL; n++ ) {
102 *vals = (BerVarray) SLAP_REALLOC( (char *) *vals,
103 (n + 2) * sizeof(struct berval) );
104 if( *vals == NULL ) {
106 LDAP_LOG( OPERATION, ERR,
107 "value_add_one: SLAP_MALLOC failed.\n", 0, 0, 0 );
109 Debug(LDAP_DEBUG_TRACE,
110 "value_add_one: SLAP_MALLOC failed.\n", 0, 0, 0 );
112 return LBER_ERROR_MEMORY;
117 ber_dupbv(v2, addval);
127 int asserted_value_validate_normalize(
128 AttributeDescription *ad,
137 /* we expect the value to be in the assertion syntax */
138 assert( !SLAP_MR_IS_VALUE_OF_ATTRIBUTE_SYNTAX(usage) );
141 *text = "inappropriate matching request";
142 return LDAP_INAPPROPRIATE_MATCHING;
145 if( !mr->smr_match ) {
146 *text = "requested matching rule not supported";
147 return LDAP_INAPPROPRIATE_MATCHING;
150 rc = (mr->smr_syntax->ssyn_validate)( mr->smr_syntax, in );
152 if( rc != LDAP_SUCCESS ) {
153 *text = "value does not conform to assertion syntax";
154 return LDAP_INVALID_SYNTAX;
157 if( mr->smr_normalize ) {
158 rc = (mr->smr_normalize)( usage,
159 ad ? ad->ad_type->sat_syntax : NULL,
162 if( rc != LDAP_SUCCESS ) {
163 *text = "unable to normalize value for matching";
164 return LDAP_INVALID_SYNTAX;
168 ber_dupbv( out, in );
184 *text = "inappropriate matching request";
185 return LDAP_INAPPROPRIATE_MATCHING;
188 if( mr->smr_syntax == NULL ) {
189 *text = "no assertion syntax";
190 return LDAP_INVALID_SYNTAX;
193 if( ! mr->smr_syntax->ssyn_validate ) {
194 *text = "no syntax validator";
195 return LDAP_INVALID_SYNTAX;
198 rc = (mr->smr_syntax->ssyn_validate)( mr->smr_syntax, in );
200 if( rc != LDAP_SUCCESS ) {
201 *text = "value is invalid";
202 return LDAP_INVALID_SYNTAX;
210 AttributeDescription *ad,
217 MatchingRule *mr = ad_mr( ad, usage );
220 *text = "inappropriate matching request";
221 return LDAP_INAPPROPRIATE_MATCHING;
224 /* we only support equality matching of binary attributes */
225 /* This is suspect, flexible certificate matching will hit this */
226 if( slap_ad_is_binary( ad ) && usage != SLAP_MR_EQUALITY ) {
227 *text = "inappropriate binary matching";
228 return LDAP_INAPPROPRIATE_MATCHING;
231 if( mr->smr_normalize ) {
232 rc = (mr->smr_normalize)( usage,
233 ad->ad_type->sat_syntax,
236 if( rc != LDAP_SUCCESS ) {
237 *text = "unable to normalize value";
238 return LDAP_INVALID_SYNTAX;
241 } else if ( mr->smr_syntax->ssyn_normalize ) {
242 rc = (mr->smr_syntax->ssyn_normalize)(
243 ad->ad_type->sat_syntax,
246 if( rc != LDAP_SUCCESS ) {
247 *text = "unable to normalize value";
248 return LDAP_INVALID_SYNTAX;
252 ber_dupbv( out, in );
259 value_validate_normalize(
260 AttributeDescription *ad,
267 MatchingRule *mr = ad_mr( ad, usage );
270 *text = "inappropriate matching request";
271 return LDAP_INAPPROPRIATE_MATCHING;
274 if( mr->smr_syntax == NULL ) {
275 *text = "no assertion syntax";
276 return LDAP_INVALID_SYNTAX;
279 if( ! mr->smr_syntax->ssyn_validate ) {
280 *text = "no syntax validator";
281 return LDAP_INVALID_SYNTAX;
284 rc = (mr->smr_syntax->ssyn_validate)( mr->smr_syntax, in );
286 if( rc != LDAP_SUCCESS ) {
287 *text = "value is invalid";
288 return LDAP_INVALID_SYNTAX;
291 /* we only support equality matching of binary attributes */
292 /* This is suspect, flexible certificate matching will hit this */
293 if( slap_ad_is_binary( ad ) && usage != SLAP_MR_EQUALITY ) {
294 *text = "inappropriate binary matching";
295 return LDAP_INAPPROPRIATE_MATCHING;
298 if( mr->smr_normalize ) {
299 rc = (mr->smr_normalize)( usage,
300 ad->ad_type->sat_syntax,
303 if( rc != LDAP_SUCCESS ) {
304 *text = "unable to normalize value";
305 return LDAP_INVALID_SYNTAX;
308 } else if ( mr->smr_syntax->ssyn_normalize ) {
309 rc = (mr->smr_syntax->ssyn_normalize)(
310 ad->ad_type->sat_syntax,
313 if( rc != LDAP_SUCCESS ) {
314 *text = "unable to normalize value";
315 return LDAP_INVALID_SYNTAX;
319 ber_dupbv( out, in );
329 AttributeDescription *ad,
332 struct berval *v1, /* stored value */
333 void *v2, /* assertion */
337 struct berval nv1 = { 0, NULL };
338 struct berval nv2 = { 0, NULL };
340 assert( mr != NULL );
342 if( !mr->smr_match ) {
343 return LDAP_INAPPROPRIATE_MATCHING;
347 if( ad->ad_type->sat_syntax->ssyn_normalize ) {
348 rc = ad->ad_type->sat_syntax->ssyn_normalize(
349 ad->ad_type->sat_syntax, v1, &nv1 );
351 if( rc != LDAP_SUCCESS ) {
352 return LDAP_INAPPROPRIATE_MATCHING;
356 if ( SLAP_IS_MR_ATTRIBUTE_SYNTAX_NONCONVERTED_MATCH( flags ) &&
359 rc = (mr->smr_convert)( v2, &nv2 );
360 if ( rc != LDAP_SUCCESS ) {
361 return LDAP_INVALID_SYNTAX;
364 /* let smr_match know we've converted the value */
365 flags |= SLAP_MR_ATTRIBUTE_SYNTAX_CONVERTED_MATCH;
369 rc = (mr->smr_match)( match, flags,
370 ad->ad_type->sat_syntax,
372 nv1.bv_val != NULL ? &nv1 : v1,
373 nv2.bv_val != NULL ? &nv2 : v2 );
375 if (nv1.bv_val ) free( nv1.bv_val );
376 if (nv2.bv_val ) free( nv2.bv_val );
381 AttributeDescription *ad,
388 struct berval nval = { 0, NULL };
389 MatchingRule *mr = ad->ad_type->sat_equality;
391 if( mr == NULL || !mr->smr_match ) {
392 return LDAP_INAPPROPRIATE_MATCHING;
396 assert(SLAP_IS_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH( flags ));
398 if( !SLAP_IS_MR_ASSERTED_VALUE_NORMALIZED_MATCH( flags ) &&
401 rc = (mr->smr_normalize)(
402 flags & SLAP_MR_TYPE_MASK|SLAP_MR_SUBTYPE_MASK,
403 ad ? ad->ad_type->sat_syntax : NULL,
406 if( rc != LDAP_SUCCESS ) {
407 return LDAP_INVALID_SYNTAX;
412 /* Take care of this here or ssyn_normalize later will hurt */
413 if ( SLAP_IS_MR_ATTRIBUTE_SYNTAX_NONCONVERTED_MATCH( flags )
416 rc = (mr->smr_convert)( val, &nval );
417 if ( rc != LDAP_SUCCESS ) {
418 return LDAP_INVALID_SYNTAX;
421 /* let value_match know we've done the version */
422 flags |= SLAP_MR_ATTRIBUTE_SYNTAX_CONVERTED_MATCH;
425 if( !(flags & SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH) &&
426 mr->smr_syntax->ssyn_normalize )
428 struct berval nval_tmp = { 0, NULL };
430 rc = mr->smr_syntax->ssyn_normalize(
432 nval.bv_val == NULL ? val : &nval, &nval_tmp );
436 if( rc != LDAP_SUCCESS ) {
438 return LDAP_INAPPROPRIATE_MATCHING;
443 for ( i = 0; vals[i].bv_val != NULL; i++ ) {
447 rc = value_match( &match, ad, mr, flags,
448 &vals[i], nval.bv_val == NULL ? val : &nval, &text );
450 if( rc == LDAP_SUCCESS && match == 0 ) {
457 return LDAP_NO_SUCH_ATTRIBUTE;