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 assert( mr->smr_syntax );
151 assert( mr->smr_syntax->ssyn_validate );
153 rc = (mr->smr_syntax->ssyn_validate)( mr->smr_syntax, in );
155 if( rc != LDAP_SUCCESS ) {
156 *text = "value does not conform to assertion syntax";
157 return LDAP_INVALID_SYNTAX;
160 if( mr->smr_normalize ) {
161 rc = (mr->smr_normalize)( usage,
162 ad ? ad->ad_type->sat_syntax : NULL,
165 if( rc != LDAP_SUCCESS ) {
166 *text = "unable to normalize value for matching";
167 return LDAP_INVALID_SYNTAX;
171 ber_dupbv( out, in );
185 *text = "inappropriate matching request";
186 return LDAP_INAPPROPRIATE_MATCHING;
189 if( mr->smr_syntax == NULL ) {
190 *text = "no assertion syntax";
191 return LDAP_INVALID_SYNTAX;
194 if( ! mr->smr_syntax->ssyn_validate ) {
195 *text = "no syntax validator";
196 return LDAP_INVALID_SYNTAX;
199 rc = (mr->smr_syntax->ssyn_validate)( mr->smr_syntax, in );
201 if( rc != LDAP_SUCCESS ) {
202 *text = "value is invalid";
203 return LDAP_INVALID_SYNTAX;
211 AttributeDescription *ad,
218 MatchingRule *mr = ad_mr( ad, usage );
221 *text = "inappropriate matching request";
222 return LDAP_INAPPROPRIATE_MATCHING;
225 /* we only support equality matching of binary attributes */
226 /* This is suspect, flexible certificate matching will hit this */
227 if( slap_ad_is_binary( ad ) && usage != SLAP_MR_EQUALITY ) {
228 *text = "inappropriate binary matching";
229 return LDAP_INAPPROPRIATE_MATCHING;
232 if( mr->smr_normalize ) {
233 rc = (mr->smr_normalize)( usage,
234 ad->ad_type->sat_syntax,
237 if( rc != LDAP_SUCCESS ) {
238 *text = "unable to normalize value";
239 return LDAP_INVALID_SYNTAX;
242 } else if ( mr->smr_syntax->ssyn_normalize ) {
243 rc = (mr->smr_syntax->ssyn_normalize)(
244 ad->ad_type->sat_syntax,
247 if( rc != LDAP_SUCCESS ) {
248 *text = "unable to normalize value";
249 return LDAP_INVALID_SYNTAX;
253 ber_dupbv( out, in );
260 value_validate_normalize(
261 AttributeDescription *ad,
268 MatchingRule *mr = ad_mr( ad, usage );
271 *text = "inappropriate matching request";
272 return LDAP_INAPPROPRIATE_MATCHING;
275 if( mr->smr_syntax == NULL ) {
276 *text = "no assertion syntax";
277 return LDAP_INVALID_SYNTAX;
280 if( ! mr->smr_syntax->ssyn_validate ) {
281 *text = "no syntax validator";
282 return LDAP_INVALID_SYNTAX;
285 rc = (mr->smr_syntax->ssyn_validate)( mr->smr_syntax, in );
287 if( rc != LDAP_SUCCESS ) {
288 *text = "value is invalid";
289 return LDAP_INVALID_SYNTAX;
292 /* we only support equality matching of binary attributes */
293 /* This is suspect, flexible certificate matching will hit this */
294 if( slap_ad_is_binary( ad ) && usage != SLAP_MR_EQUALITY ) {
295 *text = "inappropriate binary matching";
296 return LDAP_INAPPROPRIATE_MATCHING;
299 if( mr->smr_normalize ) {
300 rc = (mr->smr_normalize)( usage,
301 ad->ad_type->sat_syntax,
304 if( rc != LDAP_SUCCESS ) {
305 *text = "unable to normalize value";
306 return LDAP_INVALID_SYNTAX;
309 } else if ( mr->smr_syntax->ssyn_normalize ) {
310 rc = (mr->smr_syntax->ssyn_normalize)(
311 ad->ad_type->sat_syntax,
314 if( rc != LDAP_SUCCESS ) {
315 *text = "unable to normalize value";
316 return LDAP_INVALID_SYNTAX;
320 ber_dupbv( out, in );
330 AttributeDescription *ad,
333 struct berval *v1, /* stored value */
334 void *v2, /* assertion */
338 struct berval nv1 = { 0, NULL };
339 struct berval nv2 = { 0, NULL };
341 assert( mr != NULL );
343 if( !mr->smr_match ) {
344 return LDAP_INAPPROPRIATE_MATCHING;
348 if( ad->ad_type->sat_syntax->ssyn_normalize ) {
349 rc = ad->ad_type->sat_syntax->ssyn_normalize(
350 ad->ad_type->sat_syntax, v1, &nv1 );
352 if( rc != LDAP_SUCCESS ) {
353 return LDAP_INAPPROPRIATE_MATCHING;
357 if ( SLAP_IS_MR_ATTRIBUTE_SYNTAX_NONCONVERTED_MATCH( flags ) &&
360 rc = (mr->smr_convert)( v2, &nv2 );
361 if ( rc != LDAP_SUCCESS ) {
362 return LDAP_INVALID_SYNTAX;
365 /* let smr_match know we've converted the value */
366 flags |= SLAP_MR_ATTRIBUTE_SYNTAX_CONVERTED_MATCH;
370 rc = (mr->smr_match)( match, flags,
371 ad->ad_type->sat_syntax,
373 nv1.bv_val != NULL ? &nv1 : v1,
374 nv2.bv_val != NULL ? &nv2 : v2 );
376 if (nv1.bv_val ) free( nv1.bv_val );
377 if (nv2.bv_val ) free( nv2.bv_val );
382 AttributeDescription *ad,
389 struct berval nval = { 0, NULL };
390 MatchingRule *mr = ad->ad_type->sat_equality;
392 if( mr == NULL || !mr->smr_match ) {
393 return LDAP_INAPPROPRIATE_MATCHING;
397 assert(SLAP_IS_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH( flags ));
399 if( !SLAP_IS_MR_ASSERTED_VALUE_NORMALIZED_MATCH( flags ) &&
402 rc = (mr->smr_normalize)(
403 flags & SLAP_MR_TYPE_MASK|SLAP_MR_SUBTYPE_MASK,
404 ad ? ad->ad_type->sat_syntax : NULL,
407 if( rc != LDAP_SUCCESS ) {
408 return LDAP_INVALID_SYNTAX;
413 /* Take care of this here or ssyn_normalize later will hurt */
414 if ( SLAP_IS_MR_ATTRIBUTE_SYNTAX_NONCONVERTED_MATCH( flags )
417 rc = (mr->smr_convert)( val, &nval );
418 if ( rc != LDAP_SUCCESS ) {
419 return LDAP_INVALID_SYNTAX;
422 /* let value_match know we've done the version */
423 flags |= SLAP_MR_ATTRIBUTE_SYNTAX_CONVERTED_MATCH;
426 if( !(flags & SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH) &&
427 mr->smr_syntax->ssyn_normalize )
429 struct berval nval_tmp = { 0, NULL };
431 rc = mr->smr_syntax->ssyn_normalize(
433 nval.bv_val == NULL ? val : &nval, &nval_tmp );
437 if( rc != LDAP_SUCCESS ) {
439 return LDAP_INAPPROPRIATE_MATCHING;
444 for ( i = 0; vals[i].bv_val != NULL; i++ ) {
448 rc = value_match( &match, ad, mr, flags,
449 &vals[i], nval.bv_val == NULL ? val : &nval, &text );
451 if( rc == LDAP_SUCCESS && match == 0 ) {
458 return LDAP_NO_SUCH_ATTRIBUTE;