1 /* value.c - routines for dealing with values */
4 * Copyright 1998-2002 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);
135 *text = "inappropriate matching request";
136 return LDAP_INAPPROPRIATE_MATCHING;
139 if( mr->smr_syntax == NULL ) {
140 *text = "no assertion syntax";
141 return LDAP_INVALID_SYNTAX;
144 if( ! mr->smr_syntax->ssyn_validate ) {
145 *text = "no syntax validator";
146 return LDAP_INVALID_SYNTAX;
149 rc = (mr->smr_syntax->ssyn_validate)( mr->smr_syntax, in );
151 if( rc != LDAP_SUCCESS ) {
152 *text = "value is invalid";
153 return LDAP_INVALID_SYNTAX;
161 AttributeDescription *ad,
170 switch( usage & SLAP_MR_TYPE_MASK ) {
172 case SLAP_MR_EQUALITY:
173 mr = ad->ad_type->sat_equality;
175 case SLAP_MR_ORDERING:
176 mr = ad->ad_type->sat_ordering;
179 mr = ad->ad_type->sat_substr;
184 *text = "internal error";
189 *text = "inappropriate matching request";
190 return LDAP_INAPPROPRIATE_MATCHING;
193 /* we only support equality matching of binary attributes */
194 /* This is suspect, flexible certificate matching will hit this */
195 if( slap_ad_is_binary( ad ) && usage != SLAP_MR_EQUALITY ) {
196 *text = "inappropriate binary matching";
197 return LDAP_INAPPROPRIATE_MATCHING;
200 if( mr->smr_normalize ) {
201 rc = (mr->smr_normalize)( usage,
202 ad->ad_type->sat_syntax,
205 if( rc != LDAP_SUCCESS ) {
206 *text = "unable to normalize value";
207 return LDAP_INVALID_SYNTAX;
210 } else if ( mr->smr_syntax->ssyn_normalize ) {
211 rc = (mr->smr_syntax->ssyn_normalize)(
212 ad->ad_type->sat_syntax,
215 if( rc != LDAP_SUCCESS ) {
216 *text = "unable to normalize value";
217 return LDAP_INVALID_SYNTAX;
221 ber_dupbv( out, in );
228 value_validate_normalize(
229 AttributeDescription *ad,
238 switch( usage & SLAP_MR_TYPE_MASK ) {
240 case SLAP_MR_EQUALITY:
241 mr = ad->ad_type->sat_equality;
243 case SLAP_MR_ORDERING:
244 mr = ad->ad_type->sat_ordering;
247 mr = ad->ad_type->sat_substr;
252 *text = "internal error";
257 *text = "inappropriate matching request";
258 return LDAP_INAPPROPRIATE_MATCHING;
261 if( mr->smr_syntax == NULL ) {
262 *text = "no assertion syntax";
263 return LDAP_INVALID_SYNTAX;
266 if( ! mr->smr_syntax->ssyn_validate ) {
267 *text = "no syntax validator";
268 return LDAP_INVALID_SYNTAX;
271 rc = (mr->smr_syntax->ssyn_validate)( mr->smr_syntax, in );
273 if( rc != LDAP_SUCCESS ) {
274 *text = "value is invalid";
275 return LDAP_INVALID_SYNTAX;
278 /* we only support equality matching of binary attributes */
279 /* This is suspect, flexible certificate matching will hit this */
280 if( slap_ad_is_binary( ad ) && usage != SLAP_MR_EQUALITY ) {
281 *text = "inappropriate binary matching";
282 return LDAP_INAPPROPRIATE_MATCHING;
285 if( mr->smr_normalize ) {
286 rc = (mr->smr_normalize)( usage,
287 ad->ad_type->sat_syntax,
290 if( rc != LDAP_SUCCESS ) {
291 *text = "unable to normalize value";
292 return LDAP_INVALID_SYNTAX;
295 } else if ( mr->smr_syntax->ssyn_normalize ) {
296 rc = (mr->smr_syntax->ssyn_normalize)(
297 ad->ad_type->sat_syntax,
300 if( rc != LDAP_SUCCESS ) {
301 *text = "unable to normalize value";
302 return LDAP_INVALID_SYNTAX;
306 ber_dupbv( out, in );
316 AttributeDescription *ad,
319 struct berval *v1, /* stored value */
320 void *v2, /* assertion */
324 struct berval nv1 = { 0, NULL };
325 struct berval nv2 = { 0, NULL };
327 assert( mr != NULL );
329 if( !mr->smr_match ) {
330 return LDAP_INAPPROPRIATE_MATCHING;
333 if( ad->ad_type->sat_syntax->ssyn_normalize ) {
334 rc = ad->ad_type->sat_syntax->ssyn_normalize(
335 ad->ad_type->sat_syntax, v1, &nv1 );
337 if( rc != LDAP_SUCCESS ) {
338 return LDAP_INAPPROPRIATE_MATCHING;
342 if ( SLAP_IS_MR_VALUE_SYNTAX_NONCONVERTED_MATCH( flags ) &&
345 rc = (mr->smr_convert)( v2, &nv2 );
346 if ( rc != LDAP_SUCCESS ) {
347 return LDAP_INVALID_SYNTAX;
350 /* let smr_match know we've converted the value */
351 flags |= SLAP_MR_VALUE_SYNTAX_CONVERTED_MATCH;
354 rc = (mr->smr_match)( match, flags,
355 ad->ad_type->sat_syntax,
357 nv1.bv_val != NULL ? &nv1 : v1,
358 nv2.bv_val != NULL ? &nv2 : v2 );
360 if (nv1.bv_val ) free( nv1.bv_val );
361 if (nv2.bv_val ) free( nv2.bv_val );
367 AttributeDescription *ad,
374 struct berval nval = { 0, NULL };
375 MatchingRule *mr = ad->ad_type->sat_equality;
377 if( mr == NULL || !mr->smr_match ) {
378 return LDAP_INAPPROPRIATE_MATCHING;
381 /* Take care of this here or ssyn_normalize later will hurt */
382 if ( SLAP_IS_MR_VALUE_SYNTAX_NONCONVERTED_MATCH( flags )
385 rc = (mr->smr_convert)( val, &nval );
386 if ( rc != LDAP_SUCCESS ) {
387 return LDAP_INVALID_SYNTAX;
390 /* let value_match know we've done the version */
391 flags |= SLAP_MR_VALUE_SYNTAX_CONVERTED_MATCH;
394 if( !(flags & SLAP_MR_VALUE_NORMALIZED_MATCH) &&
395 mr->smr_syntax->ssyn_normalize ) {
396 struct berval nval_tmp = { 0, NULL };
398 rc = mr->smr_syntax->ssyn_normalize(
400 nval.bv_val == NULL ? val : &nval, &nval_tmp );
404 if( rc != LDAP_SUCCESS ) {
406 return LDAP_INAPPROPRIATE_MATCHING;
410 for ( i = 0; vals[i].bv_val != NULL; i++ ) {
414 rc = value_match( &match, ad, mr, flags,
415 &vals[i], nval.bv_val == NULL ? val : &nval, &text );
417 if( rc == LDAP_SUCCESS && match == 0 ) {
424 return LDAP_NO_SUCH_ATTRIBUTE;