value.c 5.38 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
/* value.c - routines for dealing with values */
2
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
3
/*
Kurt Zeilenga's avatar
Kurt Zeilenga committed
4
 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
6
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
7

Kurt Zeilenga's avatar
Kurt Zeilenga committed
8
9
#include "portable.h"

Kurt Zeilenga's avatar
Kurt Zeilenga committed
10
#include <stdio.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
11
12
13
14
15
16

#include <ac/ctype.h>
#include <ac/socket.h>
#include <ac/string.h>
#include <ac/time.h>

Kurt Zeilenga's avatar
Kurt Zeilenga committed
17
#include <sys/stat.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
18

Kurt Zeilenga's avatar
Kurt Zeilenga committed
19
20
#include "slap.h"

21
22
int
value_add( 
23
24
    BerVarray *vals,
    BerVarray addvals
25
26
)
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
27
	int	n, nn;
28
	BerVarray v2;
29

30
	for ( nn = 0; addvals != NULL && addvals[nn].bv_val != NULL; nn++ )
31
32
33
		;	/* NULL */

	if ( *vals == NULL ) {
Julius Enarusai's avatar
   
Julius Enarusai committed
34
		*vals = (BerVarray) SLAP_MALLOC( (nn + 1)
35
		    * sizeof(struct berval) );
Julius Enarusai's avatar
   
Julius Enarusai committed
36
37
38
39
40
41
42
43
44
45
		if( *vals == NULL ) {
#ifdef NEW_LOGGING
			 LDAP_LOG( OPERATION, ERR,
		      "value_add: SLAP_MALLOC failed.\n", 0, 0, 0 );
#else
			Debug(LDAP_DEBUG_TRACE,
		      "value_add: SLAP_MALLOC failed.\n", 0, 0, 0 );
#endif
			return LBER_ERROR_MEMORY;
		}
46
47
		n = 0;
	} else {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
48
49
50
		for ( n = 0; (*vals)[n].bv_val != NULL; n++ ) {
			;	/* Empty */
		}
Julius Enarusai's avatar
   
Julius Enarusai committed
51
		*vals = (BerVarray) SLAP_REALLOC( (char *) *vals,
52
		    (n + nn + 1) * sizeof(struct berval) );
Julius Enarusai's avatar
   
Julius Enarusai committed
53
54
55
56
57
58
59
60
61
62
		if( *vals == NULL ) {
#ifdef NEW_LOGGING
			 LDAP_LOG( OPERATION, ERR,
		      "value_add: SLAP_MALLOC failed.\n", 0, 0, 0 );
#else
			Debug(LDAP_DEBUG_TRACE,
		      "value_add: SLAP_MALLOC failed.\n", 0, 0, 0 );
#endif
			return LBER_ERROR_MEMORY;
		}
63
64
	}

65
66
67
68
	v2 = *vals + n;
	for ( ; addvals->bv_val; v2++, addvals++ ) {
		ber_dupbv(v2, addvals);
		if (v2->bv_val == NULL) break;
69
	}
70
71
	v2->bv_val = NULL;
	v2->bv_len = 0;
72

73
74
75
76
77
78
79
80
81
82
83
84
85
	return LDAP_SUCCESS;
}

int
value_add_one( 
    BerVarray *vals,
    struct berval *addval
)
{
	int	n;
	BerVarray v2;

	if ( *vals == NULL ) {
Julius Enarusai's avatar
   
Julius Enarusai committed
86
87
88
89
90
91
92
93
94
95
96
		*vals = (BerVarray) SLAP_MALLOC( 2 * sizeof(struct berval) );
		if( *vals == NULL ) {
#ifdef NEW_LOGGING
			 LDAP_LOG( OPERATION, ERR,
		      "value_add_one: SLAP_MALLOC failed.\n", 0, 0, 0 );
#else
			Debug(LDAP_DEBUG_TRACE,
		      "value_add_one: SLAP_MALLOC failed.\n", 0, 0, 0 );
#endif
			return LBER_ERROR_MEMORY;
		}
97
98
99
100
101
		n = 0;
	} else {
		for ( n = 0; (*vals)[n].bv_val != NULL; n++ ) {
			;	/* Empty */
		}
Julius Enarusai's avatar
   
Julius Enarusai committed
102
		*vals = (BerVarray) SLAP_REALLOC( (char *) *vals,
103
		    (n + 2) * sizeof(struct berval) );
Julius Enarusai's avatar
   
Julius Enarusai committed
104
105
106
107
108
109
110
111
112
113
		if( *vals == NULL ) {
#ifdef NEW_LOGGING
			 LDAP_LOG( OPERATION, ERR,
		      "value_add_one: SLAP_MALLOC failed.\n", 0, 0, 0 );
#else
			Debug(LDAP_DEBUG_TRACE,
		      "value_add_one: SLAP_MALLOC failed.\n", 0, 0, 0 );
#endif
			return LBER_ERROR_MEMORY;
		}
114
115
116
117
118
119
120
121
122
	}

	v2 = *vals + n;
	ber_dupbv(v2, addval);

	v2++;
	v2->bv_val = NULL;
	v2->bv_len = 0;

123
	return LDAP_SUCCESS;
124
125
}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
126
127
128
129
130
131
int asserted_value_validate_normalize( 
	AttributeDescription *ad,
	MatchingRule *mr,
	unsigned usage,
	struct berval *in,
	struct berval *out,
Howard Chu's avatar
Howard Chu committed
132
133
	const char ** text,
	void *ctx )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
134
135
{
	int rc;
136
137
	struct berval pval;
	pval.bv_val = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151

	/* we expect the value to be in the assertion syntax */
	assert( !SLAP_MR_IS_VALUE_OF_ATTRIBUTE_SYNTAX(usage) );

	if( mr == NULL ) {
		*text = "inappropriate matching request";
		return LDAP_INAPPROPRIATE_MATCHING;
	}

	if( !mr->smr_match ) {
		*text = "requested matching rule not supported";
		return LDAP_INAPPROPRIATE_MATCHING;
	}

152
153
154
155
156
157
158
	if( mr->smr_syntax->ssyn_pretty ) {
		rc = (mr->smr_syntax->ssyn_pretty)( mr->smr_syntax, in, &pval, ctx );
		in = &pval;

	} else {
		rc = (mr->smr_syntax->ssyn_validate)( mr->smr_syntax, in );
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
159
160
161
162
163
164
165

	if( rc != LDAP_SUCCESS ) {
		*text = "value does not conform to assertion syntax";
		return LDAP_INVALID_SYNTAX;
	}

	if( mr->smr_normalize ) {
166
167
		rc = (mr->smr_normalize)(
			usage|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
168
			ad ? ad->ad_type->sat_syntax : NULL,
Howard Chu's avatar
Howard Chu committed
169
			mr, in, out, ctx );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
170

171
172
		if( pval.bv_val ) ber_memfree_x( pval.bv_val, ctx );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
173
174
175
176
177
		if( rc != LDAP_SUCCESS ) {
			*text = "unable to normalize value for matching";
			return LDAP_INVALID_SYNTAX;
		}

178
179
180
	} else if ( pval.bv_val != NULL ) {
		*out = pval;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
181
	} else {
Howard Chu's avatar
Howard Chu committed
182
		ber_dupbv_x( out, in, ctx );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
183
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
184
185

	return LDAP_SUCCESS;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
186
187
}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
188

189
190
int
value_match(
191
	int *match,
192
193
	AttributeDescription *ad,
	MatchingRule *mr,
194
	unsigned flags,
195
196
	struct berval *v1, /* stored value */
	void *v2, /* assertion */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
197
	const char ** text )
198
{
199
	int rc;
Howard Chu's avatar
Howard Chu committed
200
201
	struct berval nv1 = { 0, NULL };
	struct berval nv2 = { 0, NULL };
202

203
204
205
	assert( mr != NULL );

	if( !mr->smr_match ) {
206
207
208
		return LDAP_INAPPROPRIATE_MATCHING;
	}

209
	rc = (mr->smr_match)( match, flags,
210
		ad->ad_type->sat_syntax,
211
		mr,
212
213
		nv1.bv_val != NULL ? &nv1 : v1,
		nv2.bv_val != NULL ? &nv2 : v2 );
214
	
Howard Chu's avatar
Howard Chu committed
215
216
	if (nv1.bv_val ) free( nv1.bv_val );
	if (nv2.bv_val ) free( nv2.bv_val );
217
	return rc;
218
}
219

220
int value_find_ex(
221
	AttributeDescription *ad,
222
	unsigned flags,
223
	BerVarray vals,
Howard Chu's avatar
Howard Chu committed
224
225
	struct berval *val,
	void *ctx )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
226
227
{
	int	i;
228
	int rc;
Howard Chu's avatar
Howard Chu committed
229
	struct berval nval = { 0, NULL };
230
231
	MatchingRule *mr = ad->ad_type->sat_equality;

232
	if( mr == NULL || !mr->smr_match ) {
233
234
		return LDAP_INAPPROPRIATE_MATCHING;
	}
235

Kurt Zeilenga's avatar
Kurt Zeilenga committed
236
237
238
239
240
241
	assert(SLAP_IS_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH( flags ));

	if( !SLAP_IS_MR_ASSERTED_VALUE_NORMALIZED_MATCH( flags ) &&
		mr->smr_normalize )
	{
		rc = (mr->smr_normalize)(
242
			flags & (SLAP_MR_TYPE_MASK|SLAP_MR_SUBTYPE_MASK|SLAP_MR_VALUE_OF_SYNTAX),
Kurt Zeilenga's avatar
Kurt Zeilenga committed
243
			ad ? ad->ad_type->sat_syntax : NULL,
Howard Chu's avatar
Howard Chu committed
244
			mr, val, &nval, ctx );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
245
246
247
248
249

		if( rc != LDAP_SUCCESS ) {
			return LDAP_INVALID_SYNTAX;
		}
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
250

251
	for ( i = 0; vals[i].bv_val != NULL; i++ ) {
252
		int match;
253
		const char *text;
254

255
		rc = value_match( &match, ad, mr, flags,
256
			&vals[i], nval.bv_val == NULL ? val : &nval, &text );
257

258
		if( rc == LDAP_SUCCESS && match == 0 ) {
Howard Chu's avatar
Howard Chu committed
259
260
			sl_free( nval.bv_val, ctx );
			return rc;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
261
262
263
		}
	}

264
	sl_free( nval.bv_val, ctx );
Howard Chu's avatar
Howard Chu committed
265
	return LDAP_NO_SUCH_ATTRIBUTE;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
266
}