]> git.sur5r.net Git - openldap/blob - servers/slapd/ad.c
Sync with HEAD
[openldap] / servers / slapd / ad.c
1 /* ad.c - routines for dealing with attribute descriptions */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 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 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/ctype.h>
22 #include <ac/errno.h>
23 #include <ac/socket.h>
24 #include <ac/string.h>
25 #include <ac/time.h>
26
27 #include "slap.h"
28 #include "lutil.h"
29
30 static AttributeName anlist_no_attrs[] = {
31         { BER_BVC( LDAP_NO_ATTRS ), NULL, 0, NULL },
32         { BER_BVNULL, NULL, 0, NULL }
33 };
34
35 static AttributeName anlist_all_user_attributes[] = {
36         { BER_BVC( LDAP_ALL_USER_ATTRIBUTES ), NULL, 0, NULL },
37         { BER_BVNULL, NULL, 0, NULL }
38 };
39
40 static AttributeName anlist_all_operational_attributes[] = {
41         { BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES ), NULL, 0, NULL },
42         { BER_BVNULL, NULL, 0, NULL }
43 };
44
45 static AttributeName anlist_all_attributes[] = {
46         { BER_BVC( LDAP_ALL_USER_ATTRIBUTES ), NULL, 0, NULL },
47         { BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES ), NULL, 0, NULL },
48         { BER_BVNULL, NULL, 0, NULL }
49 };
50
51 AttributeName *slap_anlist_no_attrs = anlist_no_attrs;
52 AttributeName *slap_anlist_all_user_attributes = anlist_all_user_attributes;
53 AttributeName *slap_anlist_all_operational_attributes = anlist_all_operational_attributes;
54 AttributeName *slap_anlist_all_attributes = anlist_all_attributes;
55
56 typedef struct Attr_option {
57         struct berval name;     /* option name or prefix */
58         int           prefix;   /* NAME is a tag and range prefix */
59 } Attr_option;
60
61 static Attr_option lang_option = { { sizeof("lang-")-1, "lang-" }, 1 };
62
63 /* Options sorted by name, and number of options */
64 static Attr_option *options = &lang_option;
65 static int option_count = 1;
66
67 static Attr_option *ad_find_option_definition( const char *opt, int optlen );
68
69 static int ad_keystring(
70         struct berval *bv )
71 {
72         ber_len_t i;
73
74         if( !AD_LEADCHAR( bv->bv_val[0] ) ) {
75                 return 1;
76         }
77
78         for( i=1; i<bv->bv_len; i++ ) {
79                 if( !AD_CHAR( bv->bv_val[i] ) ) {
80                         return 1;
81                 }
82         }
83         return 0;
84 }
85
86 void ad_destroy( AttributeDescription *ad )
87 {
88         AttributeDescription *n;
89
90         for (; ad != NULL; ad = n) {
91                 n = ad->ad_next;
92                 ldap_memfree( ad );
93         }
94 }
95
96 /* Is there an AttributeDescription for this type that uses these tags? */
97 AttributeDescription * ad_find_tags(
98         AttributeType *type,
99         struct berval *tags )
100 {
101         AttributeDescription *ad;
102
103         ldap_pvt_thread_mutex_lock( &type->sat_ad_mutex );
104         for (ad = type->sat_ad; ad; ad=ad->ad_next)
105         {
106                 if (ad->ad_tags.bv_len == tags->bv_len &&
107                         !strcasecmp(ad->ad_tags.bv_val, tags->bv_val))
108                         break;
109         }
110         ldap_pvt_thread_mutex_unlock( &type->sat_ad_mutex );
111         return ad;
112 }
113
114 int slap_str2ad(
115         const char *str,
116         AttributeDescription **ad,
117         const char **text )
118 {
119         struct berval bv;
120         bv.bv_val = (char *) str;
121         bv.bv_len = strlen( str );
122
123         return slap_bv2ad( &bv, ad, text );
124 }
125
126 static char *strchrlen(
127         const char *p, 
128         const char ch, 
129         int *len )
130 {
131         int i;
132
133         for( i=0; p[i]; i++ ) {
134                 if( p[i] == ch ) {
135                         *len = i;
136                         return (char *) &p[i];
137                 }
138         }
139
140         *len = i;
141         return NULL;
142 }
143
144 int slap_bv2ad(
145         struct berval *bv,
146         AttributeDescription **ad,
147         const char **text )
148 {
149         int rtn = LDAP_UNDEFINED_TYPE;
150         AttributeDescription desc, *d2;
151         char *name, *options;
152         char *opt, *next;
153         int ntags;
154         int tagslen;
155
156         /* hardcoded limits for speed */
157 #define MAX_TAGGING_OPTIONS 128
158         struct berval tags[MAX_TAGGING_OPTIONS+1];
159 #define MAX_TAGS_LEN 1024
160         char tagbuf[MAX_TAGS_LEN];
161
162         assert( ad != NULL );
163         assert( *ad == NULL ); /* temporary */
164
165         if( bv == NULL || BER_BVISNULL( bv ) || BER_BVISEMPTY( bv ) ) {
166                 *text = "empty AttributeDescription";
167                 return rtn;
168         }
169
170         /* make sure description is IA5 */
171         if( ad_keystring( bv ) ) {
172                 *text = "AttributeDescription contains inappropriate characters";
173                 return rtn;
174         }
175
176         /* find valid base attribute type; parse in place */
177         memset( &desc, 0, sizeof( desc ));
178         desc.ad_cname = *bv;
179         name = bv->bv_val;
180         options = strchr(name, ';');
181         if( options != NULL ) {
182                 desc.ad_cname.bv_len = options - name;
183         }
184         desc.ad_type = at_bvfind( &desc.ad_cname );
185         if( desc.ad_type == NULL ) {
186                 *text = "attribute type undefined";
187                 return rtn;
188         }
189
190         if( is_at_operational( desc.ad_type ) && options != NULL ) {
191                 *text = "operational attribute with options undefined";
192                 return rtn;
193         }
194
195         /*
196          * parse options in place
197          */
198         ntags = 0;
199         memset( tags, 0, sizeof( tags ));
200         tagslen = 0;
201
202         for( opt=options; opt != NULL; opt=next ) {
203                 int optlen;
204                 opt++; 
205                 next = strchrlen( opt, ';', &optlen );
206
207                 if( optlen == 0 ) {
208                         *text = "zero length option is invalid";
209                         return rtn;
210                 
211                 } else if ( optlen == sizeof("binary")-1 &&
212                         strncasecmp( opt, "binary", sizeof("binary")-1 ) == 0 )
213                 {
214                         /* binary option */
215                         if( slap_ad_is_binary( &desc ) ) {
216                                 *text = "option \"binary\" specified multiple times";
217                                 return rtn;
218                         }
219
220                         if( !slap_syntax_is_binary( desc.ad_type->sat_syntax )) {
221                                 /* not stored in binary, disallow option */
222                                 *text = "option \"binary\" not supported with type";
223                                 return rtn;
224                         }
225
226                         desc.ad_flags |= SLAP_DESC_BINARY;
227                         continue;
228
229                 } else if ( ad_find_option_definition( opt, optlen ) ) {
230                         int i;
231
232                         if( opt[optlen-1] == '-' ) {
233                                 desc.ad_flags |= SLAP_DESC_TAG_RANGE;
234                         }
235
236                         if( ntags >= MAX_TAGGING_OPTIONS ) {
237                                 *text = "too many tagging options";
238                                 return rtn;
239                         }
240
241                         /*
242                          * tags should be presented in sorted order,
243                          * so run the array in reverse.
244                          */
245                         for( i=ntags-1; i>=0; i-- ) {
246                                 int rc;
247
248                                 rc = strncasecmp( opt, tags[i].bv_val,
249                                         (unsigned) optlen < tags[i].bv_len
250                                                 ? optlen : tags[i].bv_len );
251
252                                 if( rc == 0 && (unsigned)optlen == tags[i].bv_len ) {
253                                         /* duplicate (ignore) */
254                                         goto done;
255
256                                 } else if ( rc > 0 ||
257                                         ( rc == 0 && (unsigned)optlen > tags[i].bv_len ))
258                                 {
259                                         AC_MEMCPY( &tags[i+2], &tags[i+1],
260                                                 (ntags-i-1)*sizeof(struct berval) );
261                                         tags[i+1].bv_val = opt;
262                                         tags[i+1].bv_len = optlen;
263                                         goto done;
264                                 }
265                         }
266
267                         if( ntags ) {
268                                 AC_MEMCPY( &tags[1], &tags[0],
269                                         ntags*sizeof(struct berval) );
270                         }
271                         tags[0].bv_val = opt;
272                         tags[0].bv_len = optlen;
273
274 done:;
275                         tagslen += optlen + 1;
276                         ntags++;
277
278                 } else {
279                         *text = "unrecognized option";
280                         return rtn;
281                 }
282         }
283
284         if( ntags > 0 ) {
285                 int i;
286
287                 if( tagslen > MAX_TAGS_LEN ) {
288                         *text = "tagging options too long";
289                         return rtn;
290                 }
291
292                 desc.ad_tags.bv_val = tagbuf;
293                 tagslen = 0;
294
295                 for( i=0; i<ntags; i++ ) {
296                         AC_MEMCPY( &desc.ad_tags.bv_val[tagslen],
297                                 tags[i].bv_val, tags[i].bv_len );
298
299                         tagslen += tags[i].bv_len;
300                         desc.ad_tags.bv_val[tagslen++] = ';';
301                 }
302
303                 desc.ad_tags.bv_val[--tagslen] = '\0';
304                 desc.ad_tags.bv_len = tagslen;
305         }
306
307         /* see if a matching description is already cached */
308         for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
309                 if( d2->ad_flags != desc.ad_flags ) {
310                         continue;
311                 }
312                 if( d2->ad_tags.bv_len != desc.ad_tags.bv_len ) {
313                         continue;
314                 }
315                 if( d2->ad_tags.bv_len == 0 ) {
316                         break;
317                 }
318                 if( strncasecmp( d2->ad_tags.bv_val, desc.ad_tags.bv_val,
319                         desc.ad_tags.bv_len ) == 0 )
320                 {
321                         break;
322                 }
323         }
324
325         /* Not found, add new one */
326         while (d2 == NULL) {
327                 size_t dlen = 0;
328                 ldap_pvt_thread_mutex_lock( &desc.ad_type->sat_ad_mutex );
329                 /* check again now that we've locked */
330                 for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
331                         if (d2->ad_flags != desc.ad_flags)
332                                 continue;
333                         if (d2->ad_tags.bv_len != desc.ad_tags.bv_len)
334                                 continue;
335                         if (d2->ad_tags.bv_len == 0)
336                                 break;
337                         if (strncasecmp(d2->ad_tags.bv_val, desc.ad_tags.bv_val,
338                                 desc.ad_tags.bv_len) == 0)
339                                 break;
340                 }
341                 if (d2) {
342                         ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
343                         break;
344                 }
345
346                 /* Allocate a single contiguous block. If there are no
347                  * options, we just need space for the AttrDesc structure.
348                  * Otherwise, we need to tack on the full name length +
349                  * options length, + maybe tagging options length again.
350                  */
351                 if (desc.ad_tags.bv_len || desc.ad_flags != SLAP_DESC_NONE) {
352                         dlen = desc.ad_type->sat_cname.bv_len + 1;
353                         if (desc.ad_tags.bv_len) {
354                                 dlen += 1+desc.ad_tags.bv_len;
355                         }
356                         if( slap_ad_is_binary( &desc ) ) {
357                                 dlen += sizeof(";binary")+desc.ad_tags.bv_len;
358                         }
359                 }
360
361                 d2 = ch_malloc(sizeof(AttributeDescription) + dlen);
362                 d2->ad_next = NULL;
363                 d2->ad_type = desc.ad_type;
364                 d2->ad_flags = desc.ad_flags;
365                 d2->ad_cname.bv_len = desc.ad_type->sat_cname.bv_len;
366                 d2->ad_tags.bv_len = desc.ad_tags.bv_len;
367
368                 if (dlen == 0) {
369                         d2->ad_cname.bv_val = d2->ad_type->sat_cname.bv_val;
370                         d2->ad_tags.bv_val = NULL;
371                 } else {
372                         char *cp, *op, *lp;
373                         int j;
374                         d2->ad_cname.bv_val = (char *)(d2+1);
375                         strcpy(d2->ad_cname.bv_val, d2->ad_type->sat_cname.bv_val);
376                         cp = d2->ad_cname.bv_val + d2->ad_cname.bv_len;
377                         if( slap_ad_is_binary( &desc ) ) {
378                                 op = cp;
379                                 lp = NULL;
380                                 if( desc.ad_tags.bv_len ) {
381                                         lp = desc.ad_tags.bv_val;
382                                         while( strncasecmp(lp, "binary", sizeof("binary")-1) < 0
383                                                && (lp = strchr( lp, ';' )) != NULL )
384                                                 ++lp;
385                                         if( lp != desc.ad_tags.bv_val ) {
386                                                 *cp++ = ';';
387                                                 j = (lp
388                                                      ? lp - desc.ad_tags.bv_val - 1
389                                                      : strlen( desc.ad_tags.bv_val ));
390                                                 cp = lutil_strncopy(cp, desc.ad_tags.bv_val, j);
391                                         }
392                                 }
393                                 cp = lutil_strcopy(cp, ";binary");
394                                 if( lp != NULL ) {
395                                         *cp++ = ';';
396                                         cp = lutil_strcopy(cp, lp);
397                                 }
398                                 d2->ad_cname.bv_len = cp - d2->ad_cname.bv_val;
399                                 if( desc.ad_tags.bv_len )
400                                         ldap_pvt_str2lower(op);
401                                 j = 1;
402                         } else {
403                                 j = 0;
404                         }
405                         if( desc.ad_tags.bv_len ) {
406                                 lp = d2->ad_cname.bv_val + d2->ad_cname.bv_len + j;
407                                 if ( j == 0 )
408                                         *lp++ = ';';
409                                 d2->ad_tags.bv_val = lp;
410                                 strcpy(lp, desc.ad_tags.bv_val);
411                                 ldap_pvt_str2lower(lp);
412                                 if( j == 0 )
413                                         d2->ad_cname.bv_len += 1 + desc.ad_tags.bv_len;
414                         }
415                 }
416                 /* Add new desc to list. We always want the bare Desc with
417                  * no options to stay at the head of the list, assuming
418                  * that one will be used most frequently.
419                  */
420                 if (desc.ad_type->sat_ad == NULL || dlen == 0) {
421                         d2->ad_next = desc.ad_type->sat_ad;
422                         desc.ad_type->sat_ad = d2;
423                 } else {
424                         d2->ad_next = desc.ad_type->sat_ad->ad_next;
425                         desc.ad_type->sat_ad->ad_next = d2;
426                 }
427                 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
428         }
429
430         if( *ad == NULL ) {
431                 *ad = d2;
432         } else {
433                 **ad = *d2;
434         }
435
436         return LDAP_SUCCESS;
437 }
438
439 static int is_ad_subtags(
440         struct berval *subtagsbv, 
441         struct berval *suptagsbv )
442 {
443         const char *suptags, *supp, *supdelimp;
444         const char *subtags, *subp, *subdelimp;
445         int  suplen, sublen;
446
447         subtags =subtagsbv->bv_val;
448         suptags =suptagsbv->bv_val;
449
450         for( supp=suptags ; supp; supp=supdelimp ) {
451                 supdelimp = strchrlen( supp, ';', &suplen );
452                 if( supdelimp ) supdelimp++;
453
454                 for( subp=subtags ; subp; subp=subdelimp ) {
455                         subdelimp = strchrlen( subp, ';', &sublen );
456                         if( subdelimp ) subdelimp++;
457
458                         if ( suplen > sublen
459                                  ? ( suplen-1 == sublen && supp[suplen-1] == '-'
460                                          && strncmp( supp, subp, sublen ) == 0 )
461                                  : ( ( suplen == sublen || supp[suplen-1] == '-' )
462                                          && strncmp( supp, subp, suplen ) == 0 ) )
463                         {
464                                 goto match;
465                         }
466                 }
467
468                 return 0;
469 match:;
470         }
471         return 1;
472 }
473
474 int is_ad_subtype(
475         AttributeDescription *sub,
476         AttributeDescription *super
477 )
478 {
479         AttributeType *a;
480         int lr;
481
482         for ( a = sub->ad_type; a; a=a->sat_sup ) {
483                 if ( a == super->ad_type ) break;
484         }
485         if( !a ) {
486                 return 0;
487         }
488
489         /* ensure sub does support all flags of super */
490         lr = sub->ad_tags.bv_len ? SLAP_DESC_TAG_RANGE : 0;
491         if(( super->ad_flags & ( sub->ad_flags | lr )) != super->ad_flags ) {
492                 return 0;
493         }
494
495         /* check for tagging options */
496         if ( super->ad_tags.bv_len == 0 )
497                 return 1;
498         if ( sub->ad_tags.bv_len == 0 )
499                 return 0;
500
501         return is_ad_subtags( &sub->ad_tags, &super->ad_tags );
502 }
503
504 int ad_inlist(
505         AttributeDescription *desc,
506         AttributeName *attrs )
507 {
508         if (! attrs ) return 0;
509
510         for( ; attrs->an_name.bv_val; attrs++ ) {
511                 AttributeType *a;
512                 ObjectClass *oc;
513                 int rc;
514                 
515                 if ( attrs->an_desc ) {
516                         int lr;
517
518                         if ( desc == attrs->an_desc ) {
519                                 return 1;
520                         }
521
522                         /*
523                          * EXTENSION: if requested description is preceeded by
524                          * a '-' character, do not match on subtypes.
525                          */
526                         if ( attrs->an_name.bv_val[0] == '-' ) {
527                                 continue;
528                         }
529                         
530                         /* Is this a subtype of the requested attr? */
531                         for (a = desc->ad_type; a; a=a->sat_sup) {
532                                 if ( a == attrs->an_desc->ad_type )
533                                         break;
534                         }
535                         if ( !a ) {
536                                 continue;
537                         }
538                         /* Does desc support all the requested flags? */
539                         lr = desc->ad_tags.bv_len ? SLAP_DESC_TAG_RANGE : 0;
540                         if(( attrs->an_desc->ad_flags & (desc->ad_flags | lr))
541                                 != attrs->an_desc->ad_flags ) {
542                                 continue;
543                         }
544                         /* Do the descs have compatible tags? */
545                         if ( attrs->an_desc->ad_tags.bv_len == 0 ) {
546                                 return 1;
547                         }
548                         if ( desc->ad_tags.bv_len == 0) {
549                                 continue;
550                         }
551                         if ( is_ad_subtags( &desc->ad_tags,
552                                 &attrs->an_desc->ad_tags ) ) {
553                                 return 1;
554                         }
555                         continue;
556                 }
557
558                 /*
559                  * EXTENSION: see if requested description is @objectClass
560                  * if so, return attributes which the class requires/allows
561                  * else if requested description is !objectClass, return
562                  * attributes which the class does not require/allow
563                  */
564                 oc = attrs->an_oc;
565                 if( oc == NULL && attrs->an_name.bv_val ) {
566                         switch( attrs->an_name.bv_val[0] ) {
567                         case '@': /* @objectClass */
568                         case '+': /* +objectClass (deprecated) */
569                         case '!': { /* exclude */
570                                         struct berval ocname;
571                                         ocname.bv_len = attrs->an_name.bv_len - 1;
572                                         ocname.bv_val = &attrs->an_name.bv_val[1];
573                                         oc = oc_bvfind( &ocname );
574                                         attrs->an_oc_exclude = 0;
575                                         if ( oc && attrs->an_name.bv_val[0] == '!' ) {
576                                                 attrs->an_oc_exclude = 1;
577                                         }
578                                 } break;
579
580                         default: /* old (deprecated) way */
581                                 oc = oc_bvfind( &attrs->an_name );
582                         }
583                         attrs->an_oc = oc;
584                 }
585                 if( oc != NULL ) {
586                         if ( attrs->an_oc_exclude ) {
587                                 int gotit = 0;
588
589                                 if ( oc == slap_schema.si_oc_extensibleObject ) {
590                                         /* extensibleObject allows the return of anything */
591                                         return 0;
592                                 }
593
594                                 if( oc->soc_required ) {
595                                         /* allow return of required attributes */
596                                         int i;
597                                 
598                                         for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
599                                                 for (a = desc->ad_type; a; a=a->sat_sup) {
600                                                         if ( a == oc->soc_required[i] ) {
601                                                                 return 0;
602                                                         }
603                                                 }
604                                         }
605                                 }
606
607                                 if( oc->soc_allowed ) {
608                                         /* allow return of allowed attributes */
609                                         int i;
610                                         for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
611                                                 for (a = desc->ad_type; a; a=a->sat_sup) {
612                                                         if ( a == oc->soc_allowed[i] ) {
613                                                                 return 0;
614                                                         }
615                                                 }
616                                         }
617                                 }
618
619                                 return 1;
620                         }
621                         
622                         if ( oc == slap_schema.si_oc_extensibleObject ) {
623                                 /* extensibleObject allows the return of anything */
624                                 return 1;
625                         }
626
627                         if( oc->soc_required ) {
628                                 /* allow return of required attributes */
629                                 int i;
630                                 
631                                 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
632                                         for (a = desc->ad_type; a; a=a->sat_sup) {
633                                                 if ( a == oc->soc_required[i] ) {
634                                                         return 1;
635                                                 }
636                                         }
637                                 }
638                         }
639
640                         if( oc->soc_allowed ) {
641                                 /* allow return of allowed attributes */
642                                 int i;
643                                 for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
644                                         for (a = desc->ad_type; a; a=a->sat_sup) {
645                                                 if ( a == oc->soc_allowed[i] ) {
646                                                         return 1;
647                                                 }
648                                         }
649                                 }
650                         }
651
652                 } else {
653                         /* short-circuit this search next time around */
654                         if (!slap_schema.si_at_undefined->sat_ad) {
655                                 const char *text;
656                                 slap_bv2undef_ad(&attrs->an_name,
657                                         &attrs->an_desc, &text);
658                         } else {
659                                 attrs->an_desc =
660                                         slap_schema.si_at_undefined->sat_ad;
661                         }
662                 }
663         }
664
665         return 0;
666 }
667
668
669 int slap_str2undef_ad(
670         const char *str,
671         AttributeDescription **ad,
672         const char **text )
673 {
674         struct berval bv;
675         bv.bv_val = (char *) str;
676         bv.bv_len = strlen( str );
677
678         return slap_bv2undef_ad( &bv, ad, text );
679 }
680
681 int slap_bv2undef_ad(
682         struct berval *bv,
683         AttributeDescription **ad,
684         const char **text )
685 {
686         AttributeDescription *desc;
687
688         assert( ad != NULL );
689
690         if( bv == NULL || bv->bv_len == 0 ) {
691                 *text = "empty AttributeDescription";
692                 return LDAP_UNDEFINED_TYPE;
693         }
694
695         /* make sure description is IA5 */
696         if( ad_keystring( bv ) ) {
697                 *text = "AttributeDescription contains inappropriate characters";
698                 return LDAP_UNDEFINED_TYPE;
699         }
700
701         for( desc = slap_schema.si_at_undefined->sat_ad; desc;
702                 desc=desc->ad_next ) 
703         {
704                 if( desc->ad_cname.bv_len == bv->bv_len &&
705                     !strcasecmp( desc->ad_cname.bv_val, bv->bv_val ))
706                 {
707                         break;
708                 }
709         }
710         
711         if( !desc ) {
712                 desc = ch_malloc(sizeof(AttributeDescription) + 1 +
713                         bv->bv_len);
714                 
715                 desc->ad_flags = SLAP_DESC_NONE;
716                 desc->ad_tags.bv_val = NULL;
717                 desc->ad_tags.bv_len = 0;
718
719                 desc->ad_cname.bv_len = bv->bv_len;
720                 desc->ad_cname.bv_val = (char *)(desc+1);
721                 strcpy(desc->ad_cname.bv_val, bv->bv_val);
722
723                 /* canonical to upper case */
724                 ldap_pvt_str2upper( desc->ad_cname.bv_val );
725
726                 desc->ad_type = slap_schema.si_at_undefined;
727                 desc->ad_next = desc->ad_type->sat_ad;
728                 desc->ad_type->sat_ad = desc;
729         }
730
731         if( !*ad ) {
732                 *ad = desc;
733         } else {
734                 **ad = *desc;
735         }
736
737         return LDAP_SUCCESS;
738 }
739
740 int
741 an_find(
742     AttributeName *a,
743     struct berval *s
744 )
745 {
746         if( a == NULL ) return 0;
747
748         for ( ; a->an_name.bv_val; a++ ) {
749                 if ( a->an_name.bv_len != s->bv_len) continue;
750                 if ( strcasecmp( s->bv_val, a->an_name.bv_val ) == 0 ) {
751                         return( 1 );
752                 }
753         }
754
755         return( 0 );
756 }
757
758 /*
759  * Convert a delimited string into a list of AttributeNames; add
760  * on to an existing list if it was given.  If the string is not
761  * a valid attribute name, if a '-' is prepended it is skipped
762  * and the remaining name is tried again; if a '@' (or '+') is
763  * prepended, an objectclass name is searched instead; if a '!'
764  * is prepended, the objectclass name is negated.
765  * 
766  * NOTE: currently, if a valid attribute name is not found, the
767  * same string is also checked as valid objectclass name; however,
768  * this behavior is deprecated.
769  */
770 AttributeName *
771 str2anlist( AttributeName *an, char *in, const char *brkstr )
772 {
773         char    *str;
774         char    *s;
775         char    *lasts;
776         int     i, j;
777         const char *text;
778         AttributeName *anew;
779
780         /* find last element in list */
781         for (i = 0; an && an[i].an_name.bv_val; i++);
782         
783         /* protect the input string from strtok */
784         str = ch_strdup( in );
785
786         /* Count words in string */
787         j=1;
788         for ( s = str; *s; s++ ) {
789                 if ( strchr( brkstr, *s ) != NULL ) {
790                         j++;
791                 }
792         }
793
794         an = ch_realloc( an, ( i + j + 1 ) * sizeof( AttributeName ) );
795         BER_BVZERO( &an[i + j].an_name );
796         anew = an + i;
797         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
798                 s != NULL;
799                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
800         {
801                 anew->an_desc = NULL;
802                 anew->an_oc = NULL;
803                 anew->an_oc_exclude = 0;
804                 ber_str2bv(s, 0, 1, &anew->an_name);
805                 slap_bv2ad(&anew->an_name, &anew->an_desc, &text);
806                 if ( !anew->an_desc ) {
807                         switch( anew->an_name.bv_val[0] ) {
808                         case '-': {
809                                         struct berval adname;
810                                         adname.bv_len = anew->an_name.bv_len - 1;
811                                         adname.bv_val = &anew->an_name.bv_val[1];
812                                         slap_bv2ad(&adname, &anew->an_desc, &text);
813                                         if ( !anew->an_desc ) {
814                                                 goto reterr;
815                                         }
816                                 } break;
817
818                         case '@':
819                         case '+': /* (deprecated) */
820                         case '!': {
821                                         struct berval ocname;
822                                         ocname.bv_len = anew->an_name.bv_len - 1;
823                                         ocname.bv_val = &anew->an_name.bv_val[1];
824                                         anew->an_oc = oc_bvfind( &ocname );
825                                         if ( !anew->an_oc ) {
826                                                 goto reterr;
827                                         }
828
829                                         if ( anew->an_name.bv_val[0] == '!' ) {
830                                                 anew->an_oc_exclude = 1;
831                                         }
832                                 } break;
833
834                         default:
835                                 /* old (deprecated) way */
836                                 anew->an_oc = oc_bvfind( &anew->an_name );
837                                 if ( !anew->an_oc ) {
838                                         goto reterr;
839                                 }
840                         }
841                 }
842                 anew++;
843         }
844
845         anew->an_name.bv_val = NULL;
846         free( str );
847         return( an );
848
849 reterr:
850         for ( i = 0; an[i].an_name.bv_val; i++ ) {
851                 free( an[i].an_name.bv_val );
852         }
853         free( an );
854         /*
855          * overwrites input string
856          * on error!
857          */
858         strcpy( in, s );
859         free( str );
860         return NULL;
861 }
862
863 char **anlist2charray_x( AttributeName *an, int dup, void *ctx )
864 {
865     char **attrs;
866     int i;
867                                                                                 
868     if ( an != NULL ) {
869         for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ )
870             ;
871                 attrs = (char **) slap_sl_malloc( (i + 1) * sizeof(char *), ctx );
872         for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
873                         if ( dup )
874                     attrs[i] = ch_strdup( an[i].an_name.bv_val );
875                         else
876                     attrs[i] = an[i].an_name.bv_val;
877         }
878         attrs[i] = NULL;
879     } else {
880         attrs = NULL;
881     }
882                                                                                 
883     return attrs;
884 }
885
886 char **anlist2charray( AttributeName *an, int dup )
887 {
888         return anlist2charray_x( an, dup, NULL );
889 }
890
891 char**
892 anlist2attrs( AttributeName * anlist )
893 {
894         int i, j, k = 0;
895         int n;
896         char **attrs;
897         ObjectClass *oc;
898
899         attrs = anlist2charray( anlist, 1 );
900                                                                                 
901         for ( i = 0; anlist[i].an_name.bv_val; i++ ) {
902                 if ( ( oc = anlist[i].an_oc ) ) {
903                         for ( j = 0; oc->soc_required && oc->soc_required[j]; j++ ) ;
904                         k += j;
905                         for ( j = 0; oc->soc_allowed && oc->soc_allowed[j]; j++ ) ;
906                         k += j;
907                 }
908         }
909
910         if ( i == 0 )
911                 return NULL;
912                                                                                 
913         n = i;
914                                                                                 
915         if ( k )
916                 attrs = (char **) ch_realloc( attrs, (i + k + 1) * sizeof( char * ));
917
918         for ( i = 0; anlist[i].an_name.bv_val; i++ ) {
919                 if ( ( oc = anlist[i].an_oc ) ) {
920                         for ( j = 0; oc->soc_required && oc->soc_required[j]; j++ ) {
921                                 attrs[n++] = ch_strdup(
922                                                                 oc->soc_required[j]->sat_cname.bv_val );
923                         }
924                         for ( j = 0; oc->soc_allowed && oc->soc_allowed[j]; j++ ) {
925                                 attrs[n++] = ch_strdup(
926                                                                 oc->soc_allowed[j]->sat_cname.bv_val );
927                         }
928                 }
929         }
930         
931         if ( attrs )
932                 attrs[n] = NULL;
933
934         i = 0;
935         while ( attrs && attrs[i] ) {
936                 if ( *attrs[i] == '@' ) {
937                         for ( j = i; attrs[j]; j++ ) {
938                                 if ( j == i )
939                                         ch_free( attrs[i] );
940                                 attrs[j] = attrs[j+1];
941                         }
942                 } else {
943                         i++;
944                 }
945         }
946
947         for ( i = 0; attrs && attrs[i]; i++ ) {
948                 j = i + 1;
949                 while ( attrs && attrs[j] ) {
950                         if ( !strcmp( attrs[i], attrs[j] )) {
951                                 for ( k = j; attrs && attrs[k]; k++ ) {
952                                         if ( k == j )
953                                                 ch_free( attrs[j] );
954                                         attrs[k] = attrs[k+1];
955                                 }
956                         } else {
957                                 j++;
958                         }
959                 }
960         }
961
962         if ( i != n )
963                 attrs = (char **) ch_realloc( attrs, (i+1) * sizeof( char * ));
964
965         return attrs;
966 }
967
968 #define LBUFSIZ 80
969 AttributeName*
970 file2anlist( AttributeName *an, const char *fname, const char *brkstr )
971 {
972         FILE    *fp;
973         char    *line = NULL;
974         char    *lcur = NULL;
975         char    *c;
976         size_t  lmax = LBUFSIZ;
977
978         fp = fopen( fname, "r" );
979         if ( fp == NULL ) {
980                 Debug( LDAP_DEBUG_ANY,
981                         "get_attrs_from_file: failed to open attribute list file "
982                         "\"%s\": %s\n", fname, strerror(errno), 0 );
983                 return NULL;
984         }
985
986         lcur = line = (char *) ch_malloc( lmax );
987         if ( !line ) {
988                 Debug( LDAP_DEBUG_ANY,
989                         "get_attrs_from_file: could not allocate memory\n",
990                         0, 0, 0 );
991                 fclose(fp);
992                 return NULL;
993         }
994
995         while ( fgets( lcur, LBUFSIZ, fp ) != NULL ) {
996                 char *str, *s, *next;
997                 const char *delimstr = brkstr;
998                 if ( ( c = strchr( lcur, '\n' ) ) ) {
999                         if ( c == line ) {
1000                                 *c = '\0';
1001                         } else if ( *(c-1) == '\r' ) {
1002                                 *(c-1) = '\0';
1003                         } else {
1004                                 *c = '\0';
1005                         }
1006                 } else {
1007                         lmax += LBUFSIZ;
1008                         line = (char *) ch_realloc( line, lmax );
1009                         if ( !line ) {
1010                                 Debug( LDAP_DEBUG_ANY,
1011                                         "get_attrs_from_file: could not allocate memory\n",
1012                                         0, 0, 0 );
1013                                 fclose(fp);
1014                                 return NULL;
1015                         }
1016                         lcur = line + strlen( line );
1017                         continue;
1018                 }
1019                 an = str2anlist( an, line, brkstr );
1020                 if ( an == NULL )
1021                         return NULL;
1022                 lcur = line;
1023         }
1024         ch_free( line );
1025         fclose(fp);
1026         return an;
1027 }
1028 #undef LBUFSIZ
1029
1030 /* Define an attribute option. */
1031 int
1032 ad_define_option( const char *name, const char *fname, int lineno )
1033 {
1034         int i;
1035         unsigned int optlen;
1036
1037         if ( options == &lang_option ) {
1038                 options = NULL;
1039                 option_count = 0;
1040         }
1041         if ( name == NULL )
1042                 return 0;
1043
1044         optlen = 0;
1045         do {
1046                 if ( !DESC_CHAR( name[optlen] ) ) {
1047                         Debug( LDAP_DEBUG_ANY,
1048                                "%s: line %d: illegal option name \"%s\"\n",
1049                                     fname, lineno, name );
1050                         return 1;
1051                 }
1052         } while ( name[++optlen] );
1053
1054         options = ch_realloc( options,
1055                 (option_count+1) * sizeof(Attr_option) );
1056
1057         if ( strcasecmp( name, "binary" ) == 0
1058              || ad_find_option_definition( name, optlen ) ) {
1059                 Debug( LDAP_DEBUG_ANY,
1060                        "%s: line %d: option \"%s\" is already defined\n",
1061                        fname, lineno, name );
1062                 return 1;
1063         }
1064
1065         for ( i = option_count; i; --i ) {
1066                 if ( strcasecmp( name, options[i-1].name.bv_val ) >= 0 )
1067                         break;
1068                 options[i] = options[i-1];
1069         }
1070
1071         options[i].name.bv_val = ch_strdup( name );
1072         options[i].name.bv_len = optlen;
1073         options[i].prefix = (name[optlen-1] == '-');
1074
1075         if ( i != option_count &&
1076              options[i].prefix &&
1077              optlen < options[i+1].name.bv_len &&
1078              strncasecmp( name, options[i+1].name.bv_val, optlen ) == 0 ) {
1079                         Debug( LDAP_DEBUG_ANY,
1080                                "%s: line %d: option \"%s\" overrides previous option\n",
1081                                     fname, lineno, name );
1082                         return 1;
1083         }
1084
1085         option_count++;
1086         return 0;
1087 }
1088
1089 /* Find the definition of the option name or prefix matching the arguments */
1090 static Attr_option *
1091 ad_find_option_definition( const char *opt, int optlen )
1092 {
1093         int top = 0, bot = option_count;
1094         while ( top < bot ) {
1095                 int mid = (top + bot) / 2;
1096                 int mlen = options[mid].name.bv_len;
1097                 char *mname = options[mid].name.bv_val;
1098                 int j;
1099                 if ( optlen < mlen ) {
1100                         j = strncasecmp( opt, mname, optlen ) - 1;
1101                 } else {
1102                         j = strncasecmp( opt, mname, mlen );
1103                         if ( j==0 && (optlen==mlen || options[mid].prefix) )
1104                                 return &options[mid];
1105                 }
1106                 if ( j < 0 )
1107                         bot = mid;
1108                 else
1109                         top = mid + 1;
1110         }
1111         return NULL;
1112 }
1113
1114 MatchingRule *ad_mr(
1115         AttributeDescription *ad,
1116         unsigned usage )
1117 {
1118         switch( usage & SLAP_MR_TYPE_MASK ) {
1119         case SLAP_MR_NONE:
1120         case SLAP_MR_EQUALITY:
1121                 return ad->ad_type->sat_equality;
1122                 break;
1123         case SLAP_MR_ORDERING:
1124                 return ad->ad_type->sat_ordering;
1125                 break;
1126         case SLAP_MR_SUBSTR:
1127                 return ad->ad_type->sat_substr;
1128                 break;
1129         case SLAP_MR_EXT:
1130         default:
1131                 assert( 0 /* ad_mr: bad usage */);
1132         }
1133         return NULL;
1134 }