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 );
182 *text = "inappropriate matching request";
183 return LDAP_INAPPROPRIATE_MATCHING;
186 if( mr->smr_syntax == NULL ) {
187 *text = "no assertion syntax";
188 return LDAP_INVALID_SYNTAX;
191 if( ! mr->smr_syntax->ssyn_validate ) {
192 *text = "no syntax validator";
193 return LDAP_INVALID_SYNTAX;
196 rc = (mr->smr_syntax->ssyn_validate)( mr->smr_syntax, in );
198 if( rc != LDAP_SUCCESS ) {
199 *text = "value is invalid";
200 return LDAP_INVALID_SYNTAX;
208 AttributeDescription *ad,
215 MatchingRule *mr = ad_mr( ad, usage );
218 *text = "inappropriate matching request";
219 return LDAP_INAPPROPRIATE_MATCHING;
222 /* we only support equality matching of binary attributes */
223 /* This is suspect, flexible certificate matching will hit this */
224 if( slap_ad_is_binary( ad ) && usage != SLAP_MR_EQUALITY ) {
225 *text = "inappropriate binary matching";
226 return LDAP_INAPPROPRIATE_MATCHING;
229 if( mr->smr_normalize ) {
230 rc = (mr->smr_normalize)( usage,
231 ad->ad_type->sat_syntax,
234 if( rc != LDAP_SUCCESS ) {
235 *text = "unable to normalize value";
236 return LDAP_INVALID_SYNTAX;
239 } else if ( mr->smr_syntax->ssyn_normalize ) {
240 rc = (mr->smr_syntax->ssyn_normalize)(
241 ad->ad_type->sat_syntax,
244 if( rc != LDAP_SUCCESS ) {
245 *text = "unable to normalize value";
246 return LDAP_INVALID_SYNTAX;
250 ber_dupbv( out, in );
257 value_validate_normalize(
258 AttributeDescription *ad,
265 MatchingRule *mr = ad_mr( ad, usage );
268 *text = "inappropriate matching request";
269 return LDAP_INAPPROPRIATE_MATCHING;
272 if( mr->smr_syntax == NULL ) {
273 *text = "no assertion syntax";
274 return LDAP_INVALID_SYNTAX;
277 if( ! mr->smr_syntax->ssyn_validate ) {
278 *text = "no syntax validator";
279 return LDAP_INVALID_SYNTAX;
282 rc = (mr->smr_syntax->ssyn_validate)( mr->smr_syntax, in );
284 if( rc != LDAP_SUCCESS ) {
285 *text = "value is invalid";
286 return LDAP_INVALID_SYNTAX;
289 /* we only support equality matching of binary attributes */
290 /* This is suspect, flexible certificate matching will hit this */
291 if( slap_ad_is_binary( ad ) && usage != SLAP_MR_EQUALITY ) {
292 *text = "inappropriate binary matching";
293 return LDAP_INAPPROPRIATE_MATCHING;
296 if( mr->smr_normalize ) {
297 rc = (mr->smr_normalize)( usage,
298 ad->ad_type->sat_syntax,
301 if( rc != LDAP_SUCCESS ) {
302 *text = "unable to normalize value";
303 return LDAP_INVALID_SYNTAX;
306 } else if ( mr->smr_syntax->ssyn_normalize ) {
307 rc = (mr->smr_syntax->ssyn_normalize)(
308 ad->ad_type->sat_syntax,
311 if( rc != LDAP_SUCCESS ) {
312 *text = "unable to normalize value";
313 return LDAP_INVALID_SYNTAX;
317 ber_dupbv( out, in );
327 AttributeDescription *ad,
330 struct berval *v1, /* stored value */
331 void *v2, /* assertion */
335 struct berval nv1 = { 0, NULL };
336 struct berval nv2 = { 0, NULL };
338 assert( mr != NULL );
340 if( !mr->smr_match ) {
341 return LDAP_INAPPROPRIATE_MATCHING;
345 if( ad->ad_type->sat_syntax->ssyn_normalize ) {
346 rc = ad->ad_type->sat_syntax->ssyn_normalize(
347 ad->ad_type->sat_syntax, v1, &nv1 );
349 if( rc != LDAP_SUCCESS ) {
350 return LDAP_INAPPROPRIATE_MATCHING;
354 if ( SLAP_IS_MR_ATTRIBUTE_SYNTAX_NONCONVERTED_MATCH( flags ) &&
357 rc = (mr->smr_convert)( v2, &nv2 );
358 if ( rc != LDAP_SUCCESS ) {
359 return LDAP_INVALID_SYNTAX;
362 /* let smr_match know we've converted the value */
363 flags |= SLAP_MR_ATTRIBUTE_SYNTAX_CONVERTED_MATCH;
367 rc = (mr->smr_match)( match, flags,
368 ad->ad_type->sat_syntax,
370 nv1.bv_val != NULL ? &nv1 : v1,
371 nv2.bv_val != NULL ? &nv2 : v2 );
373 if (nv1.bv_val ) free( nv1.bv_val );
374 if (nv2.bv_val ) free( nv2.bv_val );
379 AttributeDescription *ad,
386 struct berval nval = { 0, NULL };
387 MatchingRule *mr = ad->ad_type->sat_equality;
389 if( mr == NULL || !mr->smr_match ) {
390 return LDAP_INAPPROPRIATE_MATCHING;
394 assert(SLAP_IS_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH( flags ));
396 if( !SLAP_IS_MR_ASSERTED_VALUE_NORMALIZED_MATCH( flags ) &&
399 rc = (mr->smr_normalize)(
400 flags & SLAP_MR_TYPE_MASK|SLAP_MR_SUBTYPE_MASK,
401 ad ? ad->ad_type->sat_syntax : NULL,
404 if( rc != LDAP_SUCCESS ) {
405 return LDAP_INVALID_SYNTAX;
410 /* Take care of this here or ssyn_normalize later will hurt */
411 if ( SLAP_IS_MR_ATTRIBUTE_SYNTAX_NONCONVERTED_MATCH( flags )
414 rc = (mr->smr_convert)( val, &nval );
415 if ( rc != LDAP_SUCCESS ) {
416 return LDAP_INVALID_SYNTAX;
419 /* let value_match know we've done the version */
420 flags |= SLAP_MR_ATTRIBUTE_SYNTAX_CONVERTED_MATCH;
423 if( !(flags & SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH) &&
424 mr->smr_syntax->ssyn_normalize )
426 struct berval nval_tmp = { 0, NULL };
428 rc = mr->smr_syntax->ssyn_normalize(
430 nval.bv_val == NULL ? val : &nval, &nval_tmp );
434 if( rc != LDAP_SUCCESS ) {
436 return LDAP_INAPPROPRIATE_MATCHING;
441 for ( i = 0; vals[i].bv_val != NULL; i++ ) {
445 rc = value_match( &match, ad, mr, flags,
446 &vals[i], nval.bv_val == NULL ? val : &nval, &text );
448 if( rc == LDAP_SUCCESS && match == 0 ) {
455 return LDAP_NO_SUCH_ATTRIBUTE;