]> git.sur5r.net Git - openldap/blob - servers/slapd/value.c
allow backwards compatibility for 'T' option (single char)
[openldap] / servers / slapd / value.c
1 /* value.c - routines for dealing with values */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2004 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /*
17  * Copyright (c) 1995 Regents of the University of Michigan.
18  * All rights reserved.
19  *
20  * Redistribution and use in source and binary forms are permitted
21  * provided that this notice is preserved and that due credit is given
22  * to the University of Michigan at Ann Arbor. The name of the University
23  * may not be used to endorse or promote products derived from this
24  * software without specific prior written permission. This software
25  * is provided ``as is'' without express or implied warranty.
26  */
27
28 #include "portable.h"
29
30 #include <stdio.h>
31
32 #include <ac/ctype.h>
33 #include <ac/socket.h>
34 #include <ac/string.h>
35 #include <ac/time.h>
36
37 #include <sys/stat.h>
38
39 #include "slap.h"
40
41 int
42 value_add( 
43     BerVarray *vals,
44     BerVarray addvals )
45 {
46         int     n, nn;
47         BerVarray v2;
48
49         for ( nn = 0; addvals != NULL && addvals[nn].bv_val != NULL; nn++ )
50                 ;       /* NULL */
51
52         if ( *vals == NULL ) {
53                 *vals = (BerVarray) SLAP_MALLOC( (nn + 1)
54                     * sizeof(struct berval) );
55                 if( *vals == NULL ) {
56 #ifdef NEW_LOGGING
57                          LDAP_LOG( OPERATION, ERR,
58                       "value_add: SLAP_MALLOC failed.\n", 0, 0, 0 );
59 #else
60                         Debug(LDAP_DEBUG_TRACE,
61                       "value_add: SLAP_MALLOC failed.\n", 0, 0, 0 );
62 #endif
63                         return LBER_ERROR_MEMORY;
64                 }
65                 n = 0;
66         } else {
67                 for ( n = 0; (*vals)[n].bv_val != NULL; n++ ) {
68                         ;       /* Empty */
69                 }
70                 *vals = (BerVarray) SLAP_REALLOC( (char *) *vals,
71                     (n + nn + 1) * sizeof(struct berval) );
72                 if( *vals == NULL ) {
73 #ifdef NEW_LOGGING
74                          LDAP_LOG( OPERATION, ERR,
75                       "value_add: SLAP_MALLOC failed.\n", 0, 0, 0 );
76 #else
77                         Debug(LDAP_DEBUG_TRACE,
78                       "value_add: SLAP_MALLOC failed.\n", 0, 0, 0 );
79 #endif
80                         return LBER_ERROR_MEMORY;
81                 }
82         }
83
84         v2 = *vals + n;
85         for ( ; addvals->bv_val; v2++, addvals++ ) {
86                 ber_dupbv(v2, addvals);
87                 if (v2->bv_val == NULL) break;
88         }
89         v2->bv_val = NULL;
90         v2->bv_len = 0;
91
92         return LDAP_SUCCESS;
93 }
94
95 int
96 value_add_one( 
97     BerVarray *vals,
98     struct berval *addval )
99 {
100         int     n;
101         BerVarray v2;
102
103         if ( *vals == NULL ) {
104                 *vals = (BerVarray) SLAP_MALLOC( 2 * sizeof(struct berval) );
105                 if( *vals == NULL ) {
106 #ifdef NEW_LOGGING
107                          LDAP_LOG( OPERATION, ERR,
108                       "value_add_one: SLAP_MALLOC failed.\n", 0, 0, 0 );
109 #else
110                         Debug(LDAP_DEBUG_TRACE,
111                       "value_add_one: SLAP_MALLOC failed.\n", 0, 0, 0 );
112 #endif
113                         return LBER_ERROR_MEMORY;
114                 }
115                 n = 0;
116         } else {
117                 for ( n = 0; (*vals)[n].bv_val != NULL; n++ ) {
118                         ;       /* Empty */
119                 }
120                 *vals = (BerVarray) SLAP_REALLOC( (char *) *vals,
121                     (n + 2) * sizeof(struct berval) );
122                 if( *vals == NULL ) {
123 #ifdef NEW_LOGGING
124                          LDAP_LOG( OPERATION, ERR,
125                       "value_add_one: SLAP_MALLOC failed.\n", 0, 0, 0 );
126 #else
127                         Debug(LDAP_DEBUG_TRACE,
128                       "value_add_one: SLAP_MALLOC failed.\n", 0, 0, 0 );
129 #endif
130                         return LBER_ERROR_MEMORY;
131                 }
132         }
133
134         v2 = *vals + n;
135         ber_dupbv(v2, addval);
136
137         v2++;
138         v2->bv_val = NULL;
139         v2->bv_len = 0;
140
141         return LDAP_SUCCESS;
142 }
143
144 int asserted_value_validate_normalize( 
145         AttributeDescription *ad,
146         MatchingRule *mr,
147         unsigned usage,
148         struct berval *in,
149         struct berval *out,
150         const char ** text,
151         void *ctx )
152 {
153         int rc;
154         struct berval pval;
155         pval.bv_val = NULL;
156
157         /* we expect the value to be in the assertion syntax */
158         assert( !SLAP_MR_IS_VALUE_OF_ATTRIBUTE_SYNTAX(usage) );
159
160         if( mr == NULL ) {
161                 *text = "inappropriate matching request";
162                 return LDAP_INAPPROPRIATE_MATCHING;
163         }
164
165         if( !mr->smr_match ) {
166                 *text = "requested matching rule not supported";
167                 return LDAP_INAPPROPRIATE_MATCHING;
168         }
169
170         if( mr->smr_syntax->ssyn_pretty ) {
171                 rc = (mr->smr_syntax->ssyn_pretty)( mr->smr_syntax, in, &pval, ctx );
172                 in = &pval;
173
174         } else {
175                 rc = (mr->smr_syntax->ssyn_validate)( mr->smr_syntax, in );
176         }
177
178         if( rc != LDAP_SUCCESS ) {
179                 *text = "value does not conform to assertion syntax";
180                 return LDAP_INVALID_SYNTAX;
181         }
182
183         if( mr->smr_normalize ) {
184                 rc = (mr->smr_normalize)(
185                         usage|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
186                         ad ? ad->ad_type->sat_syntax : NULL,
187                         mr, in, out, ctx );
188
189                 if( pval.bv_val ) ber_memfree_x( pval.bv_val, ctx );
190
191                 if( rc != LDAP_SUCCESS ) {
192                         *text = "unable to normalize value for matching";
193                         return LDAP_INVALID_SYNTAX;
194                 }
195
196         } else if ( pval.bv_val != NULL ) {
197                 *out = pval;
198
199         } else {
200                 ber_dupbv_x( out, in, ctx );
201         }
202
203         return LDAP_SUCCESS;
204 }
205
206
207 int
208 value_match(
209         int *match,
210         AttributeDescription *ad,
211         MatchingRule *mr,
212         unsigned flags,
213         struct berval *v1, /* stored value */
214         void *v2, /* assertion */
215         const char ** text )
216 {
217         int rc;
218         struct berval nv1 = BER_BVNULL;
219         struct berval nv2 = BER_BVNULL;
220
221         assert( mr != NULL );
222
223         if( !mr->smr_match ) {
224                 return LDAP_INAPPROPRIATE_MATCHING;
225         }
226
227         rc = (mr->smr_match)( match, flags,
228                 ad->ad_type->sat_syntax,
229                 mr,
230                 nv1.bv_val != NULL ? &nv1 : v1,
231                 nv2.bv_val != NULL ? &nv2 : v2 );
232         
233         if (nv1.bv_val ) free( nv1.bv_val );
234         if (nv2.bv_val ) free( nv2.bv_val );
235         return rc;
236 }
237
238 int value_find_ex(
239         AttributeDescription *ad,
240         unsigned flags,
241         BerVarray vals,
242         struct berval *val,
243         void *ctx )
244 {
245         int     i;
246         int rc;
247         struct berval nval = BER_BVNULL;
248         MatchingRule *mr = ad->ad_type->sat_equality;
249
250         if( mr == NULL || !mr->smr_match ) {
251                 return LDAP_INAPPROPRIATE_MATCHING;
252         }
253
254         assert(SLAP_IS_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH( flags ));
255
256         if( !SLAP_IS_MR_ASSERTED_VALUE_NORMALIZED_MATCH( flags ) &&
257                 mr->smr_normalize )
258         {
259                 rc = (mr->smr_normalize)(
260                         flags & (SLAP_MR_TYPE_MASK|SLAP_MR_SUBTYPE_MASK|SLAP_MR_VALUE_OF_SYNTAX),
261                         ad ? ad->ad_type->sat_syntax : NULL,
262                         mr, val, &nval, ctx );
263
264                 if( rc != LDAP_SUCCESS ) {
265                         return LDAP_INVALID_SYNTAX;
266                 }
267         }
268
269         for ( i = 0; vals[i].bv_val != NULL; i++ ) {
270                 int match;
271                 const char *text;
272
273                 rc = value_match( &match, ad, mr, flags,
274                         &vals[i], nval.bv_val == NULL ? val : &nval, &text );
275
276                 if( rc == LDAP_SUCCESS && match == 0 ) {
277                         sl_free( nval.bv_val, ctx );
278                         return rc;
279                 }
280         }
281
282         sl_free( nval.bv_val, ctx );
283         return LDAP_NO_SUCH_ATTRIBUTE;
284 }