]> git.sur5r.net Git - openldap/blob - servers/slapd/ad.c
f570c0b6d69fc0b3e33ce86e74b3346632eb715f
[openldap] / servers / slapd / ad.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /* ad.c - routines for dealing with attribute descriptions */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/ctype.h>
13 #include <ac/errno.h>
14 #include <ac/socket.h>
15 #include <ac/string.h>
16 #include <ac/time.h>
17
18 #include "ldap_pvt.h"
19 #include "slap.h"
20
21 static int ad_keystring(
22         struct berval *bv )
23 {
24         ber_len_t i;
25
26         if( !AD_CHAR( bv->bv_val[0] ) ) {
27                 return 1;
28         }
29
30         for( i=1; i<bv->bv_len; i++ ) {
31                 if( !AD_CHAR( bv->bv_val[i] ) ) {
32                         return 1;
33                 }
34         }
35         return 0;
36 }
37
38 void ad_destroy( void *in )
39 {
40         AttributeDescription *ad = in, *n;
41
42         for (;ad;ad = n) {
43                 n = ad->ad_next;
44                 ldap_memfree(ad);
45         }
46 }
47
48 /* Is there an AttributeDescription for this type that uses this language? */
49 AttributeDescription * ad_find_lang(
50         AttributeType *type,
51         struct berval *lang )
52 {
53         AttributeDescription *ad;
54
55         ldap_pvt_thread_mutex_lock( &type->sat_ad_mutex );
56         for (ad = type->sat_ad; ad; ad=ad->ad_next)
57         {
58                 if (ad->ad_lang.bv_len == lang->bv_len &&
59                         !strcasecmp(ad->ad_lang.bv_val, lang->bv_val))
60                         break;
61         }
62         ldap_pvt_thread_mutex_unlock( &type->sat_ad_mutex );
63         return ad;
64 }
65
66 int slap_str2ad(
67         const char *str,
68         AttributeDescription **ad,
69         const char **text )
70 {
71         struct berval bv;
72         bv.bv_val = (char *) str;
73         bv.bv_len = strlen( str );
74
75         return slap_bv2ad( &bv, ad, text );
76 }
77
78 int slap_bv2ad(
79         struct berval *bv,
80         AttributeDescription **ad,
81         const char **text )
82 {
83         int rtn = LDAP_UNDEFINED_TYPE;
84         int i;
85         AttributeDescription desc, *d2;
86         char *name, *options;
87
88         assert( ad != NULL );
89         assert( *ad == NULL ); /* temporary */
90
91         if( bv == NULL || bv->bv_len == 0 ) {
92                 *text = "empty attribute description";
93                 return rtn;
94         }
95
96         /* make sure description is IA5 */
97         if( ad_keystring( bv ) ) {
98                 *text = "attribute description contains inappropriate characters";
99                 return rtn;
100         }
101
102         /* find valid base attribute type; parse in place */
103         name = bv->bv_val;
104         options = strchr(name, ';');
105         if (options != NULL) *options = '\0';
106         desc.ad_type = at_find( name );
107         if (options != NULL) *options = ';';
108         if( desc.ad_type == NULL ) {
109                 *text = "attribute type undefined";
110                 return rtn;
111         }
112
113         if (options != NULL) {
114                 desc.ad_cname.bv_len = options - name;
115         } else {
116                 desc.ad_cname.bv_len = bv->bv_len;
117         }
118
119         desc.ad_flags = SLAP_DESC_NONE;
120         desc.ad_lang.bv_len = 0;
121         desc.ad_lang.bv_val = NULL;
122
123         /* parse options in place */
124         for( ; options != NULL; ) {
125                 name = options+1;
126                 options = strchr( name, ';' );
127                 if ( options != NULL )
128                         i = options - name;
129                 else
130                         i = strlen(name);
131
132                 if( i == sizeof("binary")-1 && strncasecmp( name, "binary", i) == 0 ) {
133                         if( slap_ad_is_binary( &desc ) ) {
134                                 *text = "option \"binary\" specified multiple times";
135                                 goto done;
136                         }
137
138                         if( !slap_syntax_is_binary( desc.ad_type->sat_syntax )) {
139                                 /* not stored in binary, disallow option */
140                                 *text = "option \"binary\" with type not supported";
141                                 goto done;
142                         }
143
144                         desc.ad_flags |= SLAP_DESC_BINARY;
145
146                 } else if ( i >= sizeof("lang-") && strncasecmp( name, "lang-",
147                         sizeof("lang-")-1 ) == 0)
148                 {
149                         if( desc.ad_lang.bv_len != 0 ) {
150                                 *text = "multiple language tag options specified";
151                                 goto done;
152                         }
153
154                         desc.ad_lang.bv_val = name;
155                         desc.ad_lang.bv_len = i;
156                 } else {
157                         *text = "unrecognized option";
158                         goto done;
159                 }
160         }
161
162         ldap_pvt_thread_mutex_lock( &desc.ad_type->sat_ad_mutex );
163         /* see if a matching description is already cached */
164         for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
165                 if (d2->ad_flags != desc.ad_flags)
166                         continue;
167                 if (d2->ad_lang.bv_len != desc.ad_lang.bv_len)
168                         continue;
169                 if (d2->ad_lang.bv_len == 0)
170                         break;
171                 if (strncasecmp(d2->ad_lang.bv_val, desc.ad_lang.bv_val,
172                         desc.ad_lang.bv_len) == 0)
173                         break;
174         }
175
176         /* Not found, add new one */
177         while (d2 == NULL) {
178                 int dlen = 0;
179
180                 /* Allocate a single contiguous block. If there are no
181                  * options, we just need space for the AttrDesc structure.
182                  * Otherwise, we need to tack on the full name length +
183                  * options length.
184                  */
185                 i = sizeof(AttributeDescription);
186                 if (desc.ad_lang.bv_len || desc.ad_flags != SLAP_DESC_NONE) {
187                         if (desc.ad_lang.bv_len)
188                                 dlen = desc.ad_lang.bv_len+1;
189                         dlen += desc.ad_type->sat_cname.bv_len+1;
190                         if( slap_ad_is_binary( &desc ) ) {
191                                 dlen += sizeof("binary");
192                         }
193                 }
194
195                 d2 = ch_malloc(i + dlen);
196                 d2->ad_type = desc.ad_type;
197                 d2->ad_flags = desc.ad_flags;
198                 d2->ad_cname.bv_len = desc.ad_cname.bv_len;
199                 d2->ad_lang.bv_len = desc.ad_lang.bv_len;
200                 if (dlen == 0) {
201                         d2->ad_cname.bv_val = d2->ad_type->sat_cname.bv_val;
202                         d2->ad_lang.bv_val = NULL;
203                 } else {
204                         d2->ad_cname.bv_val = (char *)(d2+1);
205                         strcpy(d2->ad_cname.bv_val, d2->ad_type->sat_cname.bv_val);
206                         if( slap_ad_is_binary( &desc ) ) {
207                                 strcpy(d2->ad_cname.bv_val+d2->ad_cname.bv_len,
208                                         ";binary");
209                                 d2->ad_cname.bv_len += sizeof("binary");
210                         }
211                         if( d2->ad_lang.bv_len ) {
212                                 d2->ad_cname.bv_val[d2->ad_cname.bv_len++]=';';
213                                 d2->ad_lang.bv_val = d2->ad_cname.bv_val+
214                                         d2->ad_cname.bv_len;
215                                 strncpy(d2->ad_lang.bv_val,desc.ad_lang.bv_val,
216                                         d2->ad_lang.bv_len);
217                                 d2->ad_lang.bv_val[d2->ad_lang.bv_len] = '\0';
218                                 ldap_pvt_str2lower(d2->ad_lang.bv_val);
219                                 d2->ad_cname.bv_len += d2->ad_lang.bv_len;
220                         }
221                 }
222                 /* Add new desc to list. We always want the bare Desc with
223                  * no options to stay at the head of the list, assuming
224                  * that one will be used most frequently.
225                  */
226                 if (desc.ad_type->sat_ad == NULL || dlen == 0) {
227                         d2->ad_next = desc.ad_type->sat_ad;
228                         desc.ad_type->sat_ad = d2;
229                 } else {
230                         d2->ad_next = desc.ad_type->sat_ad->ad_next;
231                         desc.ad_type->sat_ad->ad_next = d2;
232                 }
233         }
234         ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
235
236         if( *ad == NULL ) {
237                 *ad = d2;
238         } else {
239                 **ad = *d2;
240         }
241
242         rtn = LDAP_SUCCESS;
243
244 done:
245         return rtn;
246 }
247
248 int is_ad_subtype(
249         AttributeDescription *sub,
250         AttributeDescription *super
251 )
252 {
253         if( !is_at_subtype( sub->ad_type, super->ad_type ) ) {
254                 return 0;
255         }
256
257         if( super->ad_flags && ( super->ad_flags != sub->ad_flags )) {
258                 return 0;
259         }
260
261         if( super->ad_lang.bv_len && (sub->ad_lang.bv_len !=
262                 super->ad_lang.bv_len || strcmp( super->ad_lang.bv_val,
263                 sub->ad_lang.bv_val)))
264         {
265                 return 0;
266         }
267
268         return 1;
269 }
270
271
272 int ad_inlist(
273         AttributeDescription *desc,
274         char **attrs )
275 {
276         int i;
277         for( i=0; attrs[i] != NULL; i++ ) {
278                 AttributeDescription *ad = NULL;
279                 const char *text;
280                 int rc;
281                 
282                 rc = slap_str2ad( attrs[i], &ad, &text );
283
284                 if( rc != LDAP_SUCCESS ) continue;
285
286                 rc = is_ad_subtype( desc, ad );
287
288                 if( rc ) return 1;
289         }
290
291         return 0;
292 }
293
294
295 int slap_str2undef_ad(
296         const char *str,
297         AttributeDescription **ad,
298         const char **text )
299 {
300         struct berval bv;
301         bv.bv_val = (char *) str;
302         bv.bv_len = strlen( str );
303
304         return slap_bv2undef_ad( &bv, ad, text );
305 }
306
307 int slap_bv2undef_ad(
308         struct berval *bv,
309         AttributeDescription **ad,
310         const char **text )
311 {
312         AttributeDescription *desc;
313
314         assert( ad != NULL );
315
316         if( bv == NULL || bv->bv_len == 0 ) {
317                 *text = "empty attribute description";
318                 return LDAP_UNDEFINED_TYPE;
319         }
320
321         /* make sure description is IA5 */
322         if( ad_keystring( bv ) ) {
323                 *text = "attribute description contains inappropriate characters";
324                 return LDAP_UNDEFINED_TYPE;
325         }
326
327         for (desc = slap_schema.si_at_undefined->sat_ad; desc;
328                 desc=desc->ad_next)
329                 if (desc->ad_cname.bv_len == bv->bv_len &&
330                     !strcasecmp(desc->ad_cname.bv_val, bv->bv_val))
331                         break;
332         
333         if (!desc) {
334                 desc = ch_malloc(sizeof(AttributeDescription) +
335                         bv->bv_len + 1);
336                 
337                 desc->ad_flags = SLAP_DESC_NONE;
338                 desc->ad_lang.bv_val = NULL;
339                 desc->ad_lang.bv_len = 0;
340
341                 desc->ad_cname.bv_len = bv->bv_len;
342                 desc->ad_cname.bv_val = (char *)(desc+1);
343                 strcpy(desc->ad_cname.bv_val, bv->bv_val);
344
345                 /* canonical to upper case */
346                 ldap_pvt_str2upper( desc->ad_cname.bv_val );
347
348                 desc->ad_type = slap_schema.si_at_undefined;
349                 desc->ad_next = desc->ad_type->sat_ad;
350                 desc->ad_type->sat_ad = desc;
351         }
352
353         if (!*ad)
354                 *ad = desc;
355         else
356                 **ad = *desc;
357
358         return LDAP_SUCCESS;
359 }