]> git.sur5r.net Git - openldap/blob - servers/slapd/value.c
21538c23e18a281a7d46e671b3360e737b000b96
[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         rc = (mr->smr_normalize)( usage,
132                 ad->ad_type->sat_syntax,
133                 mr, in, out );
134
135         if( rc != LDAP_SUCCESS ) {
136                 *text = "unable to normalize value";
137                 return LDAP_INVALID_SYNTAX;
138         }
139
140         return LDAP_SUCCESS;
141 }
142
143 #else
144 void
145 value_normalize(
146     char        *s,
147     int         syntax
148 )
149 {
150         char    *d, *save;
151
152         if ( ! (syntax & SYNTAX_CIS) ) {
153                 return;
154         }
155
156         if ( syntax & SYNTAX_DN ) {
157                 (void) dn_normalize( s );
158                 return;
159         }
160
161         save = s;
162         for ( d = s; *s; s++ ) {
163                 if ( (syntax & SYNTAX_TEL) && (*s == ' ' || *s == '-') ) {
164                         continue;
165                 }
166                 *d++ = TOUPPER( (unsigned char) *s );
167         }
168         *d = '\0';
169 }
170 #endif
171
172 #ifdef SLAPD_SCHEMA_NOT_COMPAT
173 int
174 value_match(
175         AttributeDescription *ad,
176         MatchingRule *mr,
177         struct berval *v1, /* (unnormalized) stored value */
178         struct berval *v2, /* (normalized) asserted value */
179         const char ** text )
180 {
181         /* not yet implemented */
182         return 0;
183 }
184 #else
185 int
186 value_cmp(
187     struct berval       *v1,
188     struct berval       *v2,
189     int                 syntax,
190     int                 normalize       /* 1 => arg 1; 2 => arg 2; 3 => both */
191 )
192 {
193         int             rc;
194
195         if ( normalize & 1 ) {
196                 v1 = ber_bvdup( v1 );
197                 value_normalize( v1->bv_val, syntax );
198         }
199         if ( normalize & 2 ) {
200                 v2 = ber_bvdup( v2 );
201                 value_normalize( v2->bv_val, syntax );
202         }
203
204         switch ( syntax ) {
205         case SYNTAX_CIS:
206         case (SYNTAX_CIS | SYNTAX_TEL):
207         case (SYNTAX_CIS | SYNTAX_DN):
208                 rc = strcasecmp( v1->bv_val, v2->bv_val );
209                 break;
210
211         case SYNTAX_CES:
212                 rc = strcmp( v1->bv_val, v2->bv_val );
213                 break;
214
215         default:        /* Unknown syntax */
216         case SYNTAX_BIN:
217                 rc = (v1->bv_len == v2->bv_len
218                       ? memcmp( v1->bv_val, v2->bv_val, v1->bv_len )
219                       : v1->bv_len > v2->bv_len ? 1 : -1);
220                 break;
221         }
222
223         if ( normalize & 1 ) {
224                 ber_bvfree( v1 );
225         }
226         if ( normalize & 2 ) {
227                 ber_bvfree( v2 );
228         }
229
230         return( rc );
231 }
232 #endif
233
234 #ifdef SLAPD_SCHEMA_NOT_COMPAT
235 int value_find(
236         AttributeDescription *ad,
237         MatchingRule *mr,
238         struct berval **vals,
239         struct berval *val,
240         const char ** text )
241 #else
242 int
243 value_find(
244     struct berval       **vals,
245     struct berval       *v,
246     int                 syntax,
247     int                 normalize )
248 #endif
249 {
250         int     i;
251
252         for ( i = 0; vals[i] != NULL; i++ ) {
253 #ifdef SLAPD_SCHEMA_NOT_COMPAT
254                 if ( value_match( ad, mr, vals[i], val, text ) == 0 )
255 #else
256                 if ( value_cmp( vals[i], v, syntax, normalize ) == 0 )
257 #endif
258                 {
259                         return LDAP_SUCCESS;
260                 }
261         }
262
263         return LDAP_NO_SUCH_ATTRIBUTE;
264 }