]> git.sur5r.net Git - openldap/blob - servers/slapd/value.c
SLAPD_SCHEMA_NOT_COMPAT:
[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 if ( mr->smr_syntax->ssyn_normalize ) {
142                 rc = (mr->smr_syntax->ssyn_normalize)(
143                         ad->ad_type->sat_syntax,
144                         in, out );
145
146                 if( rc != LDAP_SUCCESS ) {
147                         *text = "unable to normalize value";
148                         return LDAP_INVALID_SYNTAX;
149                 }
150
151         } else {
152                 *out = ber_bvdup( in );
153         }
154
155         return LDAP_SUCCESS;
156 }
157
158 #else
159 void
160 value_normalize(
161     char        *s,
162     int         syntax
163 )
164 {
165         char    *d, *save;
166
167         if ( ! (syntax & SYNTAX_CIS) ) {
168                 return;
169         }
170
171         if ( syntax & SYNTAX_DN ) {
172                 (void) dn_normalize( s );
173                 return;
174         }
175
176         save = s;
177         for ( d = s; *s; s++ ) {
178                 if ( (syntax & SYNTAX_TEL) && (*s == ' ' || *s == '-') ) {
179                         continue;
180                 }
181                 *d++ = TOUPPER( (unsigned char) *s );
182         }
183         *d = '\0';
184 }
185 #endif
186
187 #ifdef SLAPD_SCHEMA_NOT_COMPAT
188 int
189 value_match(
190         int *match,
191         AttributeDescription *ad,
192         MatchingRule *mr,
193         struct berval *v1, /* (unnormalized) stored value */
194         struct berval *v2, /* (normalized) asserted value */
195         const char ** text )
196 {
197         int rc;
198         int usage = 0;
199         struct berval *nv1 = NULL;
200
201         if( ad->ad_type->sat_syntax->ssyn_normalize ) {
202                 rc = ad->ad_type->sat_syntax->ssyn_normalize(
203                         ad->ad_type->sat_syntax, v1, &nv1 );
204
205                 if( rc != LDAP_SUCCESS ) {
206                         return LDAP_INAPPROPRIATE_MATCHING;
207                 }
208         }
209
210         if( !mr->smr_match ) {
211                 return LDAP_INAPPROPRIATE_MATCHING;
212         }
213
214         rc = (mr->smr_match)( match, usage,
215                 ad->ad_type->sat_syntax,
216                 mr,
217                 nv1 != NULL ? nv1 : v1,
218                 v2 );
219         
220         ber_bvfree( nv1 );
221         return rc;
222 }
223
224 #else
225 int
226 value_cmp(
227     struct berval       *v1,
228     struct berval       *v2,
229     int                 syntax,
230     int                 normalize       /* 1 => arg 1; 2 => arg 2; 3 => both */
231 )
232 {
233         int             rc;
234
235         if ( normalize & 1 ) {
236                 v1 = ber_bvdup( v1 );
237                 value_normalize( v1->bv_val, syntax );
238         }
239         if ( normalize & 2 ) {
240                 v2 = ber_bvdup( v2 );
241                 value_normalize( v2->bv_val, syntax );
242         }
243
244         switch ( syntax ) {
245         case SYNTAX_CIS:
246         case (SYNTAX_CIS | SYNTAX_TEL):
247         case (SYNTAX_CIS | SYNTAX_DN):
248                 rc = strcasecmp( v1->bv_val, v2->bv_val );
249                 break;
250
251         case SYNTAX_CES:
252                 rc = strcmp( v1->bv_val, v2->bv_val );
253                 break;
254
255         default:        /* Unknown syntax */
256         case SYNTAX_BIN:
257                 rc = (v1->bv_len == v2->bv_len
258                       ? memcmp( v1->bv_val, v2->bv_val, v1->bv_len )
259                       : v1->bv_len > v2->bv_len ? 1 : -1);
260                 break;
261         }
262
263         if ( normalize & 1 ) {
264                 ber_bvfree( v1 );
265         }
266         if ( normalize & 2 ) {
267                 ber_bvfree( v2 );
268         }
269
270         return( rc );
271 }
272 #endif
273
274 #ifdef SLAPD_SCHEMA_NOT_COMPAT
275 int value_find(
276         AttributeDescription *ad,
277         MatchingRule *mr,
278         struct berval **vals,
279         struct berval *val,
280         const char ** text )
281 #else
282 int
283 value_find(
284     struct berval       **vals,
285     struct berval       *v,
286     int                 syntax,
287     int                 normalize )
288 #endif
289 {
290         int     i;
291
292         for ( i = 0; vals[i] != NULL; i++ ) {
293 #ifdef SLAPD_SCHEMA_NOT_COMPAT
294                 int rc;
295                 int match;
296                 rc = value_match( &match, ad, mr, vals[i], val, text );
297
298                 if( rc == LDAP_SUCCESS && match == 0 )
299 #else
300                 if ( value_cmp( vals[i], v, syntax, normalize ) == 0 )
301 #endif
302                 {
303                         return LDAP_SUCCESS;
304                 }
305         }
306
307         return LDAP_NO_SUCH_ATTRIBUTE;
308 }