]> git.sur5r.net Git - openldap/blob - servers/slapd/ad.c
Fix previous commit
[openldap] / servers / slapd / ad.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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( AttributeDescription *ad )
39 {
40         AttributeDescription *n;
41
42         for (; ad != NULL; 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         desc.ad_cname = *bv;
104         name = bv->bv_val;
105         options = strchr(name, ';');
106         if (options != NULL) {
107                 desc.ad_cname.bv_len = options - name;
108         }
109         desc.ad_type = at_bvfind( &desc.ad_cname );
110         if( desc.ad_type == NULL ) {
111                 *text = "attribute type undefined";
112                 return rtn;
113         }
114
115         desc.ad_flags = SLAP_DESC_NONE;
116         desc.ad_lang.bv_len = 0;
117         desc.ad_lang.bv_val = NULL;
118
119         if( is_at_operational( desc.ad_type ) && options != NULL ) {
120                 *text = "operational attribute with options undefined";
121                 return rtn;
122         }
123
124         /* parse options in place */
125         for( ; options != NULL; ) {
126                 name = options+1;
127                 options = strchr( name, ';' );
128                 if ( options != NULL )
129                         i = options - name;
130                 else
131                         i = bv->bv_len - (name - bv->bv_val);
132
133                 if( i == sizeof("binary")-1 && strncasecmp( name, "binary", i) == 0 ) {
134                         if( slap_ad_is_binary( &desc ) ) {
135                                 *text = "option \"binary\" specified multiple times";
136                                 goto done;
137                         }
138
139                         if( !slap_syntax_is_binary( desc.ad_type->sat_syntax )) {
140                                 /* not stored in binary, disallow option */
141                                 *text = "option \"binary\" with type not supported";
142                                 goto done;
143                         }
144
145                         desc.ad_flags |= SLAP_DESC_BINARY;
146
147                 } else if ( i >= sizeof("lang-") && strncasecmp( name, "lang-",
148                         sizeof("lang-")-1 ) == 0)
149                 {
150                         if( desc.ad_lang.bv_len != 0 ) {
151                                 *text = "multiple language tag options specified";
152                                 goto done;
153                         }
154
155                         desc.ad_lang.bv_val = name;
156                         desc.ad_lang.bv_len = i;
157                 } else {
158                         *text = "unrecognized option";
159                         goto done;
160                 }
161         }
162
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                 ldap_pvt_thread_mutex_lock( &desc.ad_type->sat_ad_mutex );
180                 /* check again now that we've locked */
181                 for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
182                         if (d2->ad_flags != desc.ad_flags)
183                                 continue;
184                         if (d2->ad_lang.bv_len != desc.ad_lang.bv_len)
185                                 continue;
186                         if (d2->ad_lang.bv_len == 0)
187                                 break;
188                         if (strncasecmp(d2->ad_lang.bv_val, desc.ad_lang.bv_val,
189                                 desc.ad_lang.bv_len) == 0)
190                                 break;
191                 }
192                 if (d2) {
193                         ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
194                         break;
195                 }
196
197                 /* Allocate a single contiguous block. If there are no
198                  * options, we just need space for the AttrDesc structure.
199                  * Otherwise, we need to tack on the full name length +
200                  * options length.
201                  */
202                 i = sizeof(AttributeDescription);
203                 if (desc.ad_lang.bv_len || desc.ad_flags != SLAP_DESC_NONE) {
204                         if (desc.ad_lang.bv_len)
205                                 dlen = desc.ad_lang.bv_len+1;
206                         dlen += desc.ad_type->sat_cname.bv_len+1;
207                         if( slap_ad_is_binary( &desc ) ) {
208                                 dlen += sizeof("binary");
209                         }
210                 }
211
212                 d2 = ch_malloc(i + dlen);
213                 d2->ad_type = desc.ad_type;
214                 d2->ad_flags = desc.ad_flags;
215                 d2->ad_cname.bv_len = desc.ad_cname.bv_len;
216                 d2->ad_lang.bv_len = desc.ad_lang.bv_len;
217                 if (dlen == 0) {
218                         d2->ad_cname.bv_val = d2->ad_type->sat_cname.bv_val;
219                         d2->ad_lang.bv_val = NULL;
220                 } else {
221                         d2->ad_cname.bv_val = (char *)(d2+1);
222                         strcpy(d2->ad_cname.bv_val, d2->ad_type->sat_cname.bv_val);
223                         if( slap_ad_is_binary( &desc ) ) {
224                                 strcpy(d2->ad_cname.bv_val+d2->ad_cname.bv_len,
225                                         ";binary");
226                                 d2->ad_cname.bv_len += sizeof("binary");
227                         }
228                         if( d2->ad_lang.bv_len ) {
229                                 d2->ad_cname.bv_val[d2->ad_cname.bv_len++]=';';
230                                 d2->ad_lang.bv_val = d2->ad_cname.bv_val+
231                                         d2->ad_cname.bv_len;
232                                 strncpy(d2->ad_lang.bv_val,desc.ad_lang.bv_val,
233                                         d2->ad_lang.bv_len);
234                                 d2->ad_lang.bv_val[d2->ad_lang.bv_len] = '\0';
235                                 ldap_pvt_str2lower(d2->ad_lang.bv_val);
236                                 d2->ad_cname.bv_len += d2->ad_lang.bv_len;
237                         }
238                 }
239                 /* Add new desc to list. We always want the bare Desc with
240                  * no options to stay at the head of the list, assuming
241                  * that one will be used most frequently.
242                  */
243                 if (desc.ad_type->sat_ad == NULL || dlen == 0) {
244                         d2->ad_next = desc.ad_type->sat_ad;
245                         desc.ad_type->sat_ad = d2;
246                 } else {
247                         d2->ad_next = desc.ad_type->sat_ad->ad_next;
248                         desc.ad_type->sat_ad->ad_next = d2;
249                 }
250                 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
251         }
252
253         if( *ad == NULL ) {
254                 *ad = d2;
255         } else {
256                 **ad = *d2;
257         }
258
259         rtn = LDAP_SUCCESS;
260
261 done:
262         return rtn;
263 }
264
265 int is_ad_subtype(
266         AttributeDescription *sub,
267         AttributeDescription *super
268 )
269 {
270         if( !is_at_subtype( sub->ad_type, super->ad_type ) ) {
271                 return 0;
272         }
273
274         if( super->ad_flags && ( super->ad_flags != sub->ad_flags )) {
275                 return 0;
276         }
277
278         if( super->ad_lang.bv_len && (sub->ad_lang.bv_len !=
279                 super->ad_lang.bv_len || strcmp( super->ad_lang.bv_val,
280                 sub->ad_lang.bv_val)))
281         {
282                 return 0;
283         }
284
285         return 1;
286 }
287
288
289 int ad_inlist(
290         AttributeDescription *desc,
291         AttributeName *attrs )
292 {
293         if (! attrs ) return 0;
294
295         for( ; attrs->an_name.bv_val; attrs++ ) {
296                 ObjectClass *oc;
297                 int rc;
298                 
299                 if ( attrs->an_desc ) {
300                         if ( is_ad_subtype( desc, attrs->an_desc ))
301                                 return 1;
302                         continue;
303                 }
304
305
306                 /*
307                  * EXTENSION: see if requested description is an object class
308                  * if so, return attributes which the class requires/allows
309                  */
310                 oc = attrs->an_oc;
311                 if( oc == NULL ) {
312                         oc = oc_bvfind( &attrs->an_name );
313                         attrs->an_oc = oc;
314                 }
315                 if( oc != NULL ) {
316                         if ( oc == slap_schema.si_oc_extensibleObject ) {
317                                 /* extensibleObject allows the return of anything */
318                                 return 1;
319                         }
320
321                         if( oc->soc_required ) {
322                                 /* allow return of required attributes */
323                                 int i;
324                                 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
325                                         rc = is_at_subtype( desc->ad_type,
326                                                 oc->soc_allowed[i] );
327                                         if( rc ) return 1;
328                                 }
329                         }
330                         if( oc->soc_allowed ) {
331                                 /* allow return of allowed attributes */
332                                 int i;
333                                 for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
334                                         rc = is_at_subtype( desc->ad_type,
335                                                 oc->soc_allowed[i] );
336                                         if( rc ) return 1;
337                                 }
338                         }
339                 } else {
340                         /* short-circuit this search next time around */
341                         if (!slap_schema.si_at_undefined->sat_ad) {
342                                 const char *text;
343                                 slap_bv2undef_ad(&attrs->an_name,
344                                         &attrs->an_desc, &text);
345                         } else {
346                                 attrs->an_desc =
347                                         slap_schema.si_at_undefined->sat_ad;
348                         }
349                 }
350         }
351
352         return 0;
353 }
354
355
356 int slap_str2undef_ad(
357         const char *str,
358         AttributeDescription **ad,
359         const char **text )
360 {
361         struct berval bv;
362         bv.bv_val = (char *) str;
363         bv.bv_len = strlen( str );
364
365         return slap_bv2undef_ad( &bv, ad, text );
366 }
367
368 int slap_bv2undef_ad(
369         struct berval *bv,
370         AttributeDescription **ad,
371         const char **text )
372 {
373         AttributeDescription *desc;
374
375         assert( ad != NULL );
376
377         if( bv == NULL || bv->bv_len == 0 ) {
378                 *text = "empty attribute description";
379                 return LDAP_UNDEFINED_TYPE;
380         }
381
382         /* make sure description is IA5 */
383         if( ad_keystring( bv ) ) {
384                 *text = "attribute description contains inappropriate characters";
385                 return LDAP_UNDEFINED_TYPE;
386         }
387
388         for (desc = slap_schema.si_at_undefined->sat_ad; desc;
389                 desc=desc->ad_next)
390                 if (desc->ad_cname.bv_len == bv->bv_len &&
391                     !strcasecmp(desc->ad_cname.bv_val, bv->bv_val))
392                         break;
393         
394         if (!desc) {
395                 desc = ch_malloc(sizeof(AttributeDescription) +
396                         bv->bv_len + 1);
397                 
398                 desc->ad_flags = SLAP_DESC_NONE;
399                 desc->ad_lang.bv_val = NULL;
400                 desc->ad_lang.bv_len = 0;
401
402                 desc->ad_cname.bv_len = bv->bv_len;
403                 desc->ad_cname.bv_val = (char *)(desc+1);
404                 strcpy(desc->ad_cname.bv_val, bv->bv_val);
405
406                 /* canonical to upper case */
407                 ldap_pvt_str2upper( desc->ad_cname.bv_val );
408
409                 desc->ad_type = slap_schema.si_at_undefined;
410                 desc->ad_next = desc->ad_type->sat_ad;
411                 desc->ad_type->sat_ad = desc;
412         }
413
414         if (!*ad)
415                 *ad = desc;
416         else
417                 **ad = *desc;
418
419         return LDAP_SUCCESS;
420 }
421
422 int
423 an_find(
424     AttributeName *a,
425     struct berval *s
426 )
427 {
428         if( a == NULL ) return 0;
429
430         for ( ; a->an_name.bv_val; a++ ) {
431                 if ( a->an_name.bv_len != s->bv_len) continue;
432                 if ( strcasecmp( s->bv_val, a->an_name.bv_val ) == 0 ) {
433                         return( 1 );
434                 }
435         }
436
437         return( 0 );
438 }
439
440 /* Convert a delimited string into a list of AttributeNames; Add on
441  * to an existing list if it was given.
442  */
443 AttributeName *
444 str2anlist( AttributeName *an, char *in, const char *brkstr )
445 {
446         char    *str;
447         char    *s;
448         char    *lasts;
449         int     i, j;
450         const char *text;
451         AttributeName *anew;
452
453         /* find last element in list */
454         for (i = 0; an && an[i].an_name.bv_val; i++);
455         
456         /* protect the input string from strtok */
457         str = ch_strdup( in );
458
459         /* Count words in string */
460         j=1;
461         for ( s = str; *s; s++ ) {
462                 if ( strchr( brkstr, *s ) != NULL ) {
463                         j++;
464                 }
465         }
466
467         an = ch_realloc( an, ( i + j + 1 ) * sizeof( AttributeName ) );
468         anew = an + i;
469         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
470                 s != NULL;
471                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
472         {
473                 anew->an_desc = NULL;
474                 anew->an_oc = NULL;
475                 ber_str2bv(s, 0, 1, &anew->an_name);
476                 slap_bv2ad(&anew->an_name, &anew->an_desc, &text);
477                 if ( !anew->an_desc ) {
478                         anew->an_oc = oc_bvfind( &anew->an_name );
479                         if ( !anew->an_oc ) {
480                                 free( an );
481                                 /* overwrites input string on error! */
482                                 strcpy( in, s );
483                                 return NULL;
484                         }
485                 }
486                 anew++;
487         }
488         anew->an_name.bv_val = NULL;
489
490         free( str );
491         return( an );
492 }