]> git.sur5r.net Git - openldap/blob - servers/slapd/value.c
753c7240051d37092c4998876421998cfffdead4
[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
190         if( !mr->smr_match ) {
191                 return LDAP_INAPPROPRIATE_MATCHING;
192         }
193
194         rc = (mr->smr_match)( match, usage,
195                 ad->ad_type->sat_syntax,
196                 mr, v1, v2 );
197         
198         return rc;
199 }
200
201 #else
202 int
203 value_cmp(
204     struct berval       *v1,
205     struct berval       *v2,
206     int                 syntax,
207     int                 normalize       /* 1 => arg 1; 2 => arg 2; 3 => both */
208 )
209 {
210         int             rc;
211
212         if ( normalize & 1 ) {
213                 v1 = ber_bvdup( v1 );
214                 value_normalize( v1->bv_val, syntax );
215         }
216         if ( normalize & 2 ) {
217                 v2 = ber_bvdup( v2 );
218                 value_normalize( v2->bv_val, syntax );
219         }
220
221         switch ( syntax ) {
222         case SYNTAX_CIS:
223         case (SYNTAX_CIS | SYNTAX_TEL):
224         case (SYNTAX_CIS | SYNTAX_DN):
225                 rc = strcasecmp( v1->bv_val, v2->bv_val );
226                 break;
227
228         case SYNTAX_CES:
229                 rc = strcmp( v1->bv_val, v2->bv_val );
230                 break;
231
232         default:        /* Unknown syntax */
233         case SYNTAX_BIN:
234                 rc = (v1->bv_len == v2->bv_len
235                       ? memcmp( v1->bv_val, v2->bv_val, v1->bv_len )
236                       : v1->bv_len > v2->bv_len ? 1 : -1);
237                 break;
238         }
239
240         if ( normalize & 1 ) {
241                 ber_bvfree( v1 );
242         }
243         if ( normalize & 2 ) {
244                 ber_bvfree( v2 );
245         }
246
247         return( rc );
248 }
249 #endif
250
251 #ifdef SLAPD_SCHEMA_NOT_COMPAT
252 int value_find(
253         AttributeDescription *ad,
254         MatchingRule *mr,
255         struct berval **vals,
256         struct berval *val,
257         const char ** text )
258 #else
259 int
260 value_find(
261     struct berval       **vals,
262     struct berval       *v,
263     int                 syntax,
264     int                 normalize )
265 #endif
266 {
267         int     i;
268
269         for ( i = 0; vals[i] != NULL; i++ ) {
270 #ifdef SLAPD_SCHEMA_NOT_COMPAT
271                 int rc;
272                 int match;
273                 rc = value_match( &match, ad, mr, vals[i], val, text );
274
275                 if( rc == LDAP_SUCCESS && match == 0 )
276 #else
277                 if ( value_cmp( vals[i], v, syntax, normalize ) == 0 )
278 #endif
279                 {
280                         return LDAP_SUCCESS;
281                 }
282         }
283
284         return LDAP_NO_SUCH_ATTRIBUTE;
285 }