]> git.sur5r.net Git - openldap/blob - servers/slapd/value.c
21ec4e067e39df46b91ddbb7fd13ac49ec8e1029
[openldap] / servers / slapd / value.c
1 /* value.c - routines for dealing with values */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/ctype.h>
13 #include <ac/socket.h>
14 #include <ac/string.h>
15 #include <ac/time.h>
16
17 #include <sys/stat.h>
18
19 #include "slap.h"
20
21 int
22 value_add( 
23     struct berval       ***vals,
24     struct berval       **addvals
25 )
26 {
27         int     n, nn, i, j;
28
29         for ( nn = 0; addvals != NULL && addvals[nn] != NULL; nn++ )
30                 ;       /* NULL */
31
32         if ( *vals == NULL ) {
33                 *vals = (struct berval **) ch_malloc( (nn + 1)
34                     * sizeof(struct berval *) );
35                 n = 0;
36         } else {
37                 for ( n = 0; (*vals)[n] != NULL; n++ )
38                         ;       /* NULL */
39                 *vals = (struct berval **) ch_realloc( (char *) *vals,
40                     (n + nn + 1) * sizeof(struct berval *) );
41         }
42
43         for ( i = 0, j = 0; i < nn; i++ ) {
44                 if ( addvals[i]->bv_len > 0 ) {
45                         (*vals)[n + j] = ber_bvdup( addvals[i] );
46                         if( (*vals)[n + j++] == NULL ) break;
47                 }
48         }
49         (*vals)[n + j] = NULL;
50
51         return( 0 );
52 }
53
54 #ifdef SLAPD_SCHEMA_NOT_COMPAT
55         /* not used */
56 #else
57 int
58 value_add_fast( 
59     struct berval       ***vals,
60     struct berval       **addvals,
61     int                 nvals,
62     int                 naddvals,
63     int                 *maxvals
64 )
65 {
66         int     need, i, j;
67
68         if ( *maxvals == 0 ) {
69                 *maxvals = 1;
70         }
71         need = nvals + naddvals + 1;
72         while ( *maxvals < need ) {
73                 *maxvals *= 2;
74                 *vals = (struct berval **) ch_realloc( (char *) *vals,
75                     *maxvals * sizeof(struct berval *) );
76         }
77
78         for ( i = 0, j = 0; i < naddvals; i++ ) {
79                 if ( addvals[i]->bv_len > 0 ) {
80                         (*vals)[nvals + j] = ber_bvdup( addvals[i] );
81                         if( (*vals)[nvals + j] != NULL ) j++;
82                 }
83         }
84         (*vals)[nvals + j] = NULL;
85
86         return( 0 );
87 }
88 #endif
89
90 #ifdef SLAPD_SCHEMA_NOT_COMPAT
91 int
92 value_normalize(
93         AttributeDescription *ad,
94         unsigned usage,
95         struct berval *in,
96         struct berval **out,
97         const char **text )
98 {
99         int rc;
100         MatchingRule *mr;
101
102         switch( usage & SLAP_MR_TYPE_MASK ) {
103         case SLAP_MR_NONE:
104         case SLAP_MR_EQUALITY:
105                 mr = ad->ad_type->sat_equality;
106                 break;
107         case SLAP_MR_ORDERING:
108                 mr = ad->ad_type->sat_ordering;
109                 break;
110         case SLAP_MR_SUBSTR:
111                 mr = ad->ad_type->sat_substr;
112                 break;
113         case SLAP_MR_EXT:
114         default:
115                 assert( 0 );
116                 *text = "internal error";
117                 return LDAP_OTHER;
118         }
119
120         if( mr == NULL ) {
121                 *text = "inappropriate matching request";
122                 return LDAP_INAPPROPRIATE_MATCHING;
123         }
124
125         /* we only support equality matching of binary attributes */
126         if( slap_ad_is_binary( ad ) && usage != SLAP_MR_EQUALITY ) {
127                 *text = "inappropriate binary matching";
128                 return LDAP_INAPPROPRIATE_MATCHING;
129         }
130
131         if( mr->smr_normalize ) {
132                 rc = (mr->smr_normalize)( usage,
133                         ad->ad_type->sat_syntax,
134                         mr, in, out );
135
136                 if( rc != LDAP_SUCCESS ) {
137                         *text = "unable to normalize value";
138                         return LDAP_INVALID_SYNTAX;
139                 }
140
141         } else {
142                 *out = ber_bvdup( in );
143         }
144
145         return LDAP_SUCCESS;
146 }
147
148 #else
149 void
150 value_normalize(
151     char        *s,
152     int         syntax
153 )
154 {
155         char    *d, *save;
156
157         if ( ! (syntax & SYNTAX_CIS) ) {
158                 return;
159         }
160
161         if ( syntax & SYNTAX_DN ) {
162                 (void) dn_normalize( s );
163                 return;
164         }
165
166         save = s;
167         for ( d = s; *s; s++ ) {
168                 if ( (syntax & SYNTAX_TEL) && (*s == ' ' || *s == '-') ) {
169                         continue;
170                 }
171                 *d++ = TOUPPER( (unsigned char) *s );
172         }
173         *d = '\0';
174 }
175 #endif
176
177 #ifdef SLAPD_SCHEMA_NOT_COMPAT
178 int
179 value_match(
180         int *match,
181         AttributeDescription *ad,
182         MatchingRule *mr,
183         struct berval *v1, /* (unnormalized) stored value */
184         struct berval *v2, /* (normalized) asserted value */
185         const char ** text )
186 {
187         int rc;
188         int usage = 0;
189         struct berval *nv1 = NULL;
190
191         if( ad->ad_type->sat_syntax->ssyn_normalize ) {
192                 rc = ad->ad_type->sat_syntax->ssyn_normalize(
193                         ad->ad_type->sat_syntax, v1, &nv1 );
194
195                 if( rc != LDAP_SUCCESS ) {
196                         return LDAP_INAPPROPRIATE_MATCHING;
197                 }
198         }
199
200         if( !mr->smr_match ) {
201                 return LDAP_INAPPROPRIATE_MATCHING;
202         }
203
204         rc = (mr->smr_match)( match, usage,
205                 ad->ad_type->sat_syntax,
206                 mr,
207                 nv1 != NULL ? nv1 : v1,
208                 v2 );
209         
210         ber_bvfree( nv1 );
211         return rc;
212 }
213
214 #else
215 int
216 value_cmp(
217     struct berval       *v1,
218     struct berval       *v2,
219     int                 syntax,
220     int                 normalize       /* 1 => arg 1; 2 => arg 2; 3 => both */
221 )
222 {
223         int             rc;
224
225         if ( normalize & 1 ) {
226                 v1 = ber_bvdup( v1 );
227                 value_normalize( v1->bv_val, syntax );
228         }
229         if ( normalize & 2 ) {
230                 v2 = ber_bvdup( v2 );
231                 value_normalize( v2->bv_val, syntax );
232         }
233
234         switch ( syntax ) {
235         case SYNTAX_CIS:
236         case (SYNTAX_CIS | SYNTAX_TEL):
237         case (SYNTAX_CIS | SYNTAX_DN):
238                 rc = strcasecmp( v1->bv_val, v2->bv_val );
239                 break;
240
241         case SYNTAX_CES:
242                 rc = strcmp( v1->bv_val, v2->bv_val );
243                 break;
244
245         default:        /* Unknown syntax */
246         case SYNTAX_BIN:
247                 rc = (v1->bv_len == v2->bv_len
248                       ? memcmp( v1->bv_val, v2->bv_val, v1->bv_len )
249                       : v1->bv_len > v2->bv_len ? 1 : -1);
250                 break;
251         }
252
253         if ( normalize & 1 ) {
254                 ber_bvfree( v1 );
255         }
256         if ( normalize & 2 ) {
257                 ber_bvfree( v2 );
258         }
259
260         return( rc );
261 }
262 #endif
263
264 #ifdef SLAPD_SCHEMA_NOT_COMPAT
265 int value_find(
266         AttributeDescription *ad,
267         MatchingRule *mr,
268         struct berval **vals,
269         struct berval *val,
270         const char ** text )
271 #else
272 int
273 value_find(
274     struct berval       **vals,
275     struct berval       *v,
276     int                 syntax,
277     int                 normalize )
278 #endif
279 {
280         int     i;
281
282         for ( i = 0; vals[i] != NULL; i++ ) {
283 #ifdef SLAPD_SCHEMA_NOT_COMPAT
284                 int rc;
285                 int match;
286                 rc = value_match( &match, ad, mr, vals[i], val, text );
287
288                 if( rc == LDAP_SUCCESS && match == 0 )
289 #else
290                 if ( value_cmp( vals[i], v, syntax, normalize ) == 0 )
291 #endif
292                 {
293                         return LDAP_SUCCESS;
294                 }
295         }
296
297         return LDAP_NO_SUCH_ATTRIBUTE;
298 }