value.c 4.81 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-2000 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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
int
value_add( 
    struct berval	***vals,
    struct berval	**addvals
)
{
	int	n, nn, i, j;

	for ( nn = 0; addvals != NULL && addvals[nn] != NULL; nn++ )
		;	/* NULL */

	if ( *vals == NULL ) {
		*vals = (struct berval **) ch_malloc( (nn + 1)
		    * sizeof(struct berval *) );
		n = 0;
	} else {
		for ( n = 0; (*vals)[n] != NULL; n++ )
			;	/* NULL */
		*vals = (struct berval **) ch_realloc( (char *) *vals,
		    (n + nn + 1) * sizeof(struct berval *) );
	}

	for ( i = 0, j = 0; i < nn; i++ ) {
		if ( addvals[i]->bv_len > 0 ) {
			(*vals)[n + j] = ber_bvdup( addvals[i] );
			if( (*vals)[n + j++] == NULL ) break;
		}
	}
	(*vals)[n + j] = NULL;

	return( 0 );
}

#ifdef SLAPD_SCHEMA_NOT_COMPAT
55
	/* not used */
56
#else
Kurt Zeilenga's avatar
Kurt Zeilenga committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
int
value_add_fast( 
    struct berval	***vals,
    struct berval	**addvals,
    int			nvals,
    int			naddvals,
    int			*maxvals
)
{
	int	need, i, j;

	if ( *maxvals == 0 ) {
		*maxvals = 1;
	}
	need = nvals + naddvals + 1;
	while ( *maxvals < need ) {
		*maxvals *= 2;
		*vals = (struct berval **) ch_realloc( (char *) *vals,
		    *maxvals * sizeof(struct berval *) );
	}

78
	for ( i = 0, j = 0; i < naddvals; i++ ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
79
80
		if ( addvals[i]->bv_len > 0 ) {
			(*vals)[nvals + j] = ber_bvdup( addvals[i] );
81
			if( (*vals)[nvals + j] != NULL ) j++;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
82
83
84
85
86
87
		}
	}
	(*vals)[nvals + j] = NULL;

	return( 0 );
}
88
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
89

90
#ifdef SLAPD_SCHEMA_NOT_COMPAT
Kurt Zeilenga's avatar
Kurt Zeilenga committed
91
int
92
93
94
value_normalize(
	AttributeDescription *ad,
	unsigned usage,
95
96
	struct berval *in,
	struct berval **out,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
97
	const char **text )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
98
{
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
	int rc;
	MatchingRule *mr;

	switch( usage & SLAP_MR_TYPE_MASK ) {
	case SLAP_MR_NONE:
	case SLAP_MR_EQUALITY:
		mr = ad->ad_type->sat_equality;
		break;
	case SLAP_MR_ORDERING:
		mr = ad->ad_type->sat_ordering;
		break;
	case SLAP_MR_SUBSTR:
		mr = ad->ad_type->sat_substr;
		break;
	case SLAP_MR_EXT:
	default:
		assert( 0 );
		*text = "internal error";
		return LDAP_OTHER;
	}

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

	/* we only support equality matching of binary attributes */
	if( slap_ad_is_binary( ad ) && usage != SLAP_MR_EQUALITY ) {
		*text = "inappropriate binary matching";
		return LDAP_INAPPROPRIATE_MATCHING;
	}

131
132
133
134
135
136
137
138
139
	if( mr->smr_normalize ) {
		rc = (mr->smr_normalize)( usage,
			ad->ad_type->sat_syntax,
			mr, in, out );

		if( rc != LDAP_SUCCESS ) {
			*text = "unable to normalize value";
			return LDAP_INVALID_SYNTAX;
		}
140

141
142
	} else {
		*out = ber_bvdup( in );
143
144
	}

145
	return LDAP_SUCCESS;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
146
147
}

148
#else
Kurt Zeilenga's avatar
Kurt Zeilenga committed
149
150
151
152
153
154
void
value_normalize(
    char	*s,
    int		syntax
)
{
Hallvard Furuseth's avatar
Hallvard Furuseth committed
155
	char	*d, *save;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
156

Hallvard Furuseth's avatar
Hallvard Furuseth committed
157
	if ( ! (syntax & SYNTAX_CIS) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
158
159
		return;
	}
Hallvard Furuseth's avatar
Hallvard Furuseth committed
160
161

	if ( syntax & SYNTAX_DN ) {
162
		(void) dn_normalize( s );
163
164
		return;
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
165

Hallvard Furuseth's avatar
Hallvard Furuseth committed
166
	save = s;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
167
168
169
170
	for ( d = s; *s; s++ ) {
		if ( (syntax & SYNTAX_TEL) && (*s == ' ' || *s == '-') ) {
			continue;
		}
171
		*d++ = TOUPPER( (unsigned char) *s );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
172
173
174
	}
	*d = '\0';
}
175
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
176

177
#ifdef SLAPD_SCHEMA_NOT_COMPAT
178
179
180
181
182
183
int
value_match(
	AttributeDescription *ad,
	MatchingRule *mr,
	struct berval *v1, /* (unnormalized) stored value */
	struct berval *v2, /* (normalized) asserted value */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
184
	const char ** text )
185
{
186
	/* not yet implemented */
187
188
	return 0;
}
189
#else
Kurt Zeilenga's avatar
Kurt Zeilenga committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
int
value_cmp(
    struct berval	*v1,
    struct berval	*v2,
    int			syntax,
    int			normalize	/* 1 => arg 1; 2 => arg 2; 3 => both */
)
{
	int		rc;

	if ( normalize & 1 ) {
		v1 = ber_bvdup( v1 );
		value_normalize( v1->bv_val, syntax );
	}
	if ( normalize & 2 ) {
		v2 = ber_bvdup( v2 );
		value_normalize( v2->bv_val, syntax );
	}

	switch ( syntax ) {
	case SYNTAX_CIS:
	case (SYNTAX_CIS | SYNTAX_TEL):
Hallvard Furuseth's avatar
Hallvard Furuseth committed
212
	case (SYNTAX_CIS | SYNTAX_DN):
Kurt Zeilenga's avatar
Kurt Zeilenga committed
213
214
215
216
217
218
219
		rc = strcasecmp( v1->bv_val, v2->bv_val );
		break;

	case SYNTAX_CES:
		rc = strcmp( v1->bv_val, v2->bv_val );
		break;

220
	default:        /* Unknown syntax */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
221
	case SYNTAX_BIN:
222
223
224
		rc = (v1->bv_len == v2->bv_len
		      ? memcmp( v1->bv_val, v2->bv_val, v1->bv_len )
		      : v1->bv_len > v2->bv_len ? 1 : -1);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
225
226
227
228
229
230
231
232
233
234
235
236
		break;
	}

	if ( normalize & 1 ) {
		ber_bvfree( v1 );
	}
	if ( normalize & 2 ) {
		ber_bvfree( v2 );
	}

	return( rc );
}
237
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
238

239
240
241
242
243
244
#ifdef SLAPD_SCHEMA_NOT_COMPAT
int value_find(
	AttributeDescription *ad,
	MatchingRule *mr,
	struct berval **vals,
	struct berval *val,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
245
	const char ** text )
246
#else
Kurt Zeilenga's avatar
Kurt Zeilenga committed
247
248
249
250
251
int
value_find(
    struct berval	**vals,
    struct berval	*v,
    int			syntax,
252
253
    int			normalize )
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
254
255
256
257
{
	int	i;

	for ( i = 0; vals[i] != NULL; i++ ) {
258
259
260
261
262
263
264
#ifdef SLAPD_SCHEMA_NOT_COMPAT
		if ( value_match( ad, mr, vals[i], val, text ) == 0 )
#else
		if ( value_cmp( vals[i], v, syntax, normalize ) == 0 )
#endif
		{
			return LDAP_SUCCESS;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
265
266
267
		}
	}

268
	return LDAP_NO_SUCH_ATTRIBUTE;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
269
}