]> git.sur5r.net Git - openldap/blob - servers/slapd/ad.c
remove cruft
[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                 
514                 if ( attrs->an_desc ) {
515                         int lr;
516
517                         if ( desc == attrs->an_desc ) {
518                                 return 1;
519                         }
520
521                         /*
522                          * EXTENSION: if requested description is preceeded by
523                          * a '-' character, do not match on subtypes.
524                          */
525                         if ( attrs->an_name.bv_val[0] == '-' ) {
526                                 continue;
527                         }
528                         
529                         /* Is this a subtype of the requested attr? */
530                         for (a = desc->ad_type; a; a=a->sat_sup) {
531                                 if ( a == attrs->an_desc->ad_type )
532                                         break;
533                         }
534                         if ( !a ) {
535                                 continue;
536                         }
537                         /* Does desc support all the requested flags? */
538                         lr = desc->ad_tags.bv_len ? SLAP_DESC_TAG_RANGE : 0;
539                         if(( attrs->an_desc->ad_flags & (desc->ad_flags | lr))
540                                 != attrs->an_desc->ad_flags ) {
541                                 continue;
542                         }
543                         /* Do the descs have compatible tags? */
544                         if ( attrs->an_desc->ad_tags.bv_len == 0 ) {
545                                 return 1;
546                         }
547                         if ( desc->ad_tags.bv_len == 0) {
548                                 continue;
549                         }
550                         if ( is_ad_subtags( &desc->ad_tags,
551                                 &attrs->an_desc->ad_tags ) ) {
552                                 return 1;
553                         }
554                         continue;
555                 }
556
557                 /*
558                  * EXTENSION: see if requested description is @objectClass
559                  * if so, return attributes which the class requires/allows
560                  * else if requested description is !objectClass, return
561                  * attributes which the class does not require/allow
562                  */
563                 oc = attrs->an_oc;
564                 if( oc == NULL && attrs->an_name.bv_val ) {
565                         switch( attrs->an_name.bv_val[0] ) {
566                         case '@': /* @objectClass */
567                         case '+': /* +objectClass (deprecated) */
568                         case '!': { /* exclude */
569                                         struct berval ocname;
570                                         ocname.bv_len = attrs->an_name.bv_len - 1;
571                                         ocname.bv_val = &attrs->an_name.bv_val[1];
572                                         oc = oc_bvfind( &ocname );
573                                         attrs->an_oc_exclude = 0;
574                                         if ( oc && attrs->an_name.bv_val[0] == '!' ) {
575                                                 attrs->an_oc_exclude = 1;
576                                         }
577                                 } break;
578
579                         default: /* old (deprecated) way */
580                                 oc = oc_bvfind( &attrs->an_name );
581                         }
582                         attrs->an_oc = oc;
583                 }
584                 if( oc != NULL ) {
585                         if ( attrs->an_oc_exclude ) {
586                                 if ( oc == slap_schema.si_oc_extensibleObject ) {
587                                         /* extensibleObject allows the return of anything */
588                                         return 0;
589                                 }
590
591                                 if( oc->soc_required ) {
592                                         /* allow return of required attributes */
593                                         int i;
594                                 
595                                         for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
596                                                 for (a = desc->ad_type; a; a=a->sat_sup) {
597                                                         if ( a == oc->soc_required[i] ) {
598                                                                 return 0;
599                                                         }
600                                                 }
601                                         }
602                                 }
603
604                                 if( oc->soc_allowed ) {
605                                         /* allow return of allowed attributes */
606                                         int i;
607                                         for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
608                                                 for (a = desc->ad_type; a; a=a->sat_sup) {
609                                                         if ( a == oc->soc_allowed[i] ) {
610                                                                 return 0;
611                                                         }
612                                                 }
613                                         }
614                                 }
615
616                                 return 1;
617                         }
618                         
619                         if ( oc == slap_schema.si_oc_extensibleObject ) {
620                                 /* extensibleObject allows the return of anything */
621                                 return 1;
622                         }
623
624                         if( oc->soc_required ) {
625                                 /* allow return of required attributes */
626                                 int i;
627                                 
628                                 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
629                                         for (a = desc->ad_type; a; a=a->sat_sup) {
630                                                 if ( a == oc->soc_required[i] ) {
631                                                         return 1;
632                                                 }
633                                         }
634                                 }
635                         }
636
637                         if( oc->soc_allowed ) {
638                                 /* allow return of allowed attributes */
639                                 int i;
640                                 for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
641                                         for (a = desc->ad_type; a; a=a->sat_sup) {
642                                                 if ( a == oc->soc_allowed[i] ) {
643                                                         return 1;
644                                                 }
645                                         }
646                                 }
647                         }
648
649                 } else {
650                         /* short-circuit this search next time around */
651                         if (!slap_schema.si_at_undefined->sat_ad) {
652                                 const char *text;
653                                 slap_bv2undef_ad(&attrs->an_name,
654                                         &attrs->an_desc, &text);
655                         } else {
656                                 attrs->an_desc =
657                                         slap_schema.si_at_undefined->sat_ad;
658                         }
659                 }
660         }
661
662         return 0;
663 }
664
665
666 int slap_str2undef_ad(
667         const char *str,
668         AttributeDescription **ad,
669         const char **text )
670 {
671         struct berval bv;
672         bv.bv_val = (char *) str;
673         bv.bv_len = strlen( str );
674
675         return slap_bv2undef_ad( &bv, ad, text );
676 }
677
678 int slap_bv2undef_ad(
679         struct berval *bv,
680         AttributeDescription **ad,
681         const char **text )
682 {
683         AttributeDescription *desc;
684
685         assert( ad != NULL );
686
687         if( bv == NULL || bv->bv_len == 0 ) {
688                 *text = "empty AttributeDescription";
689                 return LDAP_UNDEFINED_TYPE;
690         }
691
692         /* make sure description is IA5 */
693         if( ad_keystring( bv ) ) {
694                 *text = "AttributeDescription contains inappropriate characters";
695                 return LDAP_UNDEFINED_TYPE;
696         }
697
698         for( desc = slap_schema.si_at_undefined->sat_ad; desc;
699                 desc=desc->ad_next ) 
700         {
701                 if( desc->ad_cname.bv_len == bv->bv_len &&
702                     !strcasecmp( desc->ad_cname.bv_val, bv->bv_val ))
703                 {
704                         break;
705                 }
706         }
707         
708         if( !desc ) {
709                 desc = ch_malloc(sizeof(AttributeDescription) + 1 +
710                         bv->bv_len);
711                 
712                 desc->ad_flags = SLAP_DESC_NONE;
713                 desc->ad_tags.bv_val = NULL;
714                 desc->ad_tags.bv_len = 0;
715
716                 desc->ad_cname.bv_len = bv->bv_len;
717                 desc->ad_cname.bv_val = (char *)(desc+1);
718                 strcpy(desc->ad_cname.bv_val, bv->bv_val);
719
720                 /* canonical to upper case */
721                 ldap_pvt_str2upper( desc->ad_cname.bv_val );
722
723                 desc->ad_type = slap_schema.si_at_undefined;
724                 desc->ad_next = desc->ad_type->sat_ad;
725                 desc->ad_type->sat_ad = desc;
726         }
727
728         if( !*ad ) {
729                 *ad = desc;
730         } else {
731                 **ad = *desc;
732         }
733
734         return LDAP_SUCCESS;
735 }
736
737 int
738 an_find(
739     AttributeName *a,
740     struct berval *s
741 )
742 {
743         if( a == NULL ) return 0;
744
745         for ( ; a->an_name.bv_val; a++ ) {
746                 if ( a->an_name.bv_len != s->bv_len) continue;
747                 if ( strcasecmp( s->bv_val, a->an_name.bv_val ) == 0 ) {
748                         return( 1 );
749                 }
750         }
751
752         return( 0 );
753 }
754
755 /*
756  * Convert a delimited string into a list of AttributeNames; add
757  * on to an existing list if it was given.  If the string is not
758  * a valid attribute name, if a '-' is prepended it is skipped
759  * and the remaining name is tried again; if a '@' (or '+') is
760  * prepended, an objectclass name is searched instead; if a '!'
761  * is prepended, the objectclass name is negated.
762  * 
763  * NOTE: currently, if a valid attribute name is not found, the
764  * same string is also checked as valid objectclass name; however,
765  * this behavior is deprecated.
766  */
767 AttributeName *
768 str2anlist( AttributeName *an, char *in, const char *brkstr )
769 {
770         char    *str;
771         char    *s;
772         char    *lasts;
773         int     i, j;
774         const char *text;
775         AttributeName *anew;
776
777         /* find last element in list */
778         for (i = 0; an && an[i].an_name.bv_val; i++);
779         
780         /* protect the input string from strtok */
781         str = ch_strdup( in );
782
783         /* Count words in string */
784         j=1;
785         for ( s = str; *s; s++ ) {
786                 if ( strchr( brkstr, *s ) != NULL ) {
787                         j++;
788                 }
789         }
790
791         an = ch_realloc( an, ( i + j + 1 ) * sizeof( AttributeName ) );
792         BER_BVZERO( &an[i + j].an_name );
793         anew = an + i;
794         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
795                 s != NULL;
796                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
797         {
798                 anew->an_desc = NULL;
799                 anew->an_oc = NULL;
800                 anew->an_oc_exclude = 0;
801                 ber_str2bv(s, 0, 1, &anew->an_name);
802                 slap_bv2ad(&anew->an_name, &anew->an_desc, &text);
803                 if ( !anew->an_desc ) {
804                         switch( anew->an_name.bv_val[0] ) {
805                         case '-': {
806                                         struct berval adname;
807                                         adname.bv_len = anew->an_name.bv_len - 1;
808                                         adname.bv_val = &anew->an_name.bv_val[1];
809                                         slap_bv2ad(&adname, &anew->an_desc, &text);
810                                         if ( !anew->an_desc ) {
811                                                 goto reterr;
812                                         }
813                                 } break;
814
815                         case '@':
816                         case '+': /* (deprecated) */
817                         case '!': {
818                                         struct berval ocname;
819                                         ocname.bv_len = anew->an_name.bv_len - 1;
820                                         ocname.bv_val = &anew->an_name.bv_val[1];
821                                         anew->an_oc = oc_bvfind( &ocname );
822                                         if ( !anew->an_oc ) {
823                                                 goto reterr;
824                                         }
825
826                                         if ( anew->an_name.bv_val[0] == '!' ) {
827                                                 anew->an_oc_exclude = 1;
828                                         }
829                                 } break;
830
831                         default:
832                                 /* old (deprecated) way */
833                                 anew->an_oc = oc_bvfind( &anew->an_name );
834                                 if ( !anew->an_oc ) {
835                                         goto reterr;
836                                 }
837                         }
838                 }
839                 anew++;
840         }
841
842         anew->an_name.bv_val = NULL;
843         free( str );
844         return( an );
845
846 reterr:
847         for ( i = 0; an[i].an_name.bv_val; i++ ) {
848                 free( an[i].an_name.bv_val );
849         }
850         free( an );
851         /*
852          * overwrites input string
853          * on error!
854          */
855         strcpy( in, s );
856         free( str );
857         return NULL;
858 }
859
860 char **anlist2charray_x( AttributeName *an, int dup, void *ctx )
861 {
862     char **attrs;
863     int i;
864                                                                                 
865     if ( an != NULL ) {
866         for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ )
867             ;
868                 attrs = (char **) slap_sl_malloc( (i + 1) * sizeof(char *), ctx );
869         for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
870                         if ( dup )
871                     attrs[i] = ch_strdup( an[i].an_name.bv_val );
872                         else
873                     attrs[i] = an[i].an_name.bv_val;
874         }
875         attrs[i] = NULL;
876     } else {
877         attrs = NULL;
878     }
879                                                                                 
880     return attrs;
881 }
882
883 char **anlist2charray( AttributeName *an, int dup )
884 {
885         return anlist2charray_x( an, dup, NULL );
886 }
887
888 char**
889 anlist2attrs( AttributeName * anlist )
890 {
891         int i, j, k = 0;
892         int n;
893         char **attrs;
894         ObjectClass *oc;
895
896         attrs = anlist2charray( anlist, 1 );
897                                                                                 
898         for ( i = 0; anlist[i].an_name.bv_val; i++ ) {
899                 if ( ( oc = anlist[i].an_oc ) ) {
900                         for ( j = 0; oc->soc_required && oc->soc_required[j]; j++ ) ;
901                         k += j;
902                         for ( j = 0; oc->soc_allowed && oc->soc_allowed[j]; j++ ) ;
903                         k += j;
904                 }
905         }
906
907         if ( i == 0 )
908                 return NULL;
909                                                                                 
910         n = i;
911                                                                                 
912         if ( k )
913                 attrs = (char **) ch_realloc( attrs, (i + k + 1) * sizeof( char * ));
914
915         for ( i = 0; anlist[i].an_name.bv_val; i++ ) {
916                 if ( ( oc = anlist[i].an_oc ) ) {
917                         for ( j = 0; oc->soc_required && oc->soc_required[j]; j++ ) {
918                                 attrs[n++] = ch_strdup(
919                                                                 oc->soc_required[j]->sat_cname.bv_val );
920                         }
921                         for ( j = 0; oc->soc_allowed && oc->soc_allowed[j]; j++ ) {
922                                 attrs[n++] = ch_strdup(
923                                                                 oc->soc_allowed[j]->sat_cname.bv_val );
924                         }
925                 }
926         }
927         
928         if ( attrs )
929                 attrs[n] = NULL;
930
931         i = 0;
932         while ( attrs && attrs[i] ) {
933                 if ( *attrs[i] == '@' ) {
934                         for ( j = i; attrs[j]; j++ ) {
935                                 if ( j == i )
936                                         ch_free( attrs[i] );
937                                 attrs[j] = attrs[j+1];
938                         }
939                 } else {
940                         i++;
941                 }
942         }
943
944         for ( i = 0; attrs && attrs[i]; i++ ) {
945                 j = i + 1;
946                 while ( attrs && attrs[j] ) {
947                         if ( !strcmp( attrs[i], attrs[j] )) {
948                                 for ( k = j; attrs && attrs[k]; k++ ) {
949                                         if ( k == j )
950                                                 ch_free( attrs[j] );
951                                         attrs[k] = attrs[k+1];
952                                 }
953                         } else {
954                                 j++;
955                         }
956                 }
957         }
958
959         if ( i != n )
960                 attrs = (char **) ch_realloc( attrs, (i+1) * sizeof( char * ));
961
962         return attrs;
963 }
964
965 #define LBUFSIZ 80
966 AttributeName*
967 file2anlist( AttributeName *an, const char *fname, const char *brkstr )
968 {
969         FILE    *fp;
970         char    *line = NULL;
971         char    *lcur = NULL;
972         char    *c;
973         size_t  lmax = LBUFSIZ;
974
975         fp = fopen( fname, "r" );
976         if ( fp == NULL ) {
977                 Debug( LDAP_DEBUG_ANY,
978                         "get_attrs_from_file: failed to open attribute list file "
979                         "\"%s\": %s\n", fname, strerror(errno), 0 );
980                 return NULL;
981         }
982
983         lcur = line = (char *) ch_malloc( lmax );
984         if ( !line ) {
985                 Debug( LDAP_DEBUG_ANY,
986                         "get_attrs_from_file: could not allocate memory\n",
987                         0, 0, 0 );
988                 fclose(fp);
989                 return NULL;
990         }
991
992         while ( fgets( lcur, LBUFSIZ, fp ) != NULL ) {
993                 if ( ( c = strchr( lcur, '\n' ) ) ) {
994                         if ( c == line ) {
995                                 *c = '\0';
996                         } else if ( *(c-1) == '\r' ) {
997                                 *(c-1) = '\0';
998                         } else {
999                                 *c = '\0';
1000                         }
1001                 } else {
1002                         lmax += LBUFSIZ;
1003                         line = (char *) ch_realloc( line, lmax );
1004                         if ( !line ) {
1005                                 Debug( LDAP_DEBUG_ANY,
1006                                         "get_attrs_from_file: could not allocate memory\n",
1007                                         0, 0, 0 );
1008                                 fclose(fp);
1009                                 return NULL;
1010                         }
1011                         lcur = line + strlen( line );
1012                         continue;
1013                 }
1014                 an = str2anlist( an, line, brkstr );
1015                 if ( an == NULL )
1016                         return NULL;
1017                 lcur = line;
1018         }
1019         ch_free( line );
1020         fclose(fp);
1021         return an;
1022 }
1023 #undef LBUFSIZ
1024
1025 /* Define an attribute option. */
1026 int
1027 ad_define_option( const char *name, const char *fname, int lineno )
1028 {
1029         int i;
1030         unsigned int optlen;
1031
1032         if ( options == &lang_option ) {
1033                 options = NULL;
1034                 option_count = 0;
1035         }
1036         if ( name == NULL )
1037                 return 0;
1038
1039         optlen = 0;
1040         do {
1041                 if ( !DESC_CHAR( name[optlen] ) ) {
1042                         Debug( LDAP_DEBUG_ANY,
1043                                "%s: line %d: illegal option name \"%s\"\n",
1044                                     fname, lineno, name );
1045                         return 1;
1046                 }
1047         } while ( name[++optlen] );
1048
1049         options = ch_realloc( options,
1050                 (option_count+1) * sizeof(Attr_option) );
1051
1052         if ( strcasecmp( name, "binary" ) == 0
1053              || ad_find_option_definition( name, optlen ) ) {
1054                 Debug( LDAP_DEBUG_ANY,
1055                        "%s: line %d: option \"%s\" is already defined\n",
1056                        fname, lineno, name );
1057                 return 1;
1058         }
1059
1060         for ( i = option_count; i; --i ) {
1061                 if ( strcasecmp( name, options[i-1].name.bv_val ) >= 0 )
1062                         break;
1063                 options[i] = options[i-1];
1064         }
1065
1066         options[i].name.bv_val = ch_strdup( name );
1067         options[i].name.bv_len = optlen;
1068         options[i].prefix = (name[optlen-1] == '-');
1069
1070         if ( i != option_count &&
1071              options[i].prefix &&
1072              optlen < options[i+1].name.bv_len &&
1073              strncasecmp( name, options[i+1].name.bv_val, optlen ) == 0 ) {
1074                         Debug( LDAP_DEBUG_ANY,
1075                                "%s: line %d: option \"%s\" overrides previous option\n",
1076                                     fname, lineno, name );
1077                         return 1;
1078         }
1079
1080         option_count++;
1081         return 0;
1082 }
1083
1084 /* Find the definition of the option name or prefix matching the arguments */
1085 static Attr_option *
1086 ad_find_option_definition( const char *opt, int optlen )
1087 {
1088         int top = 0, bot = option_count;
1089         while ( top < bot ) {
1090                 int mid = (top + bot) / 2;
1091                 int mlen = options[mid].name.bv_len;
1092                 char *mname = options[mid].name.bv_val;
1093                 int j;
1094                 if ( optlen < mlen ) {
1095                         j = strncasecmp( opt, mname, optlen ) - 1;
1096                 } else {
1097                         j = strncasecmp( opt, mname, mlen );
1098                         if ( j==0 && (optlen==mlen || options[mid].prefix) )
1099                                 return &options[mid];
1100                 }
1101                 if ( j < 0 )
1102                         bot = mid;
1103                 else
1104                         top = mid + 1;
1105         }
1106         return NULL;
1107 }
1108
1109 MatchingRule *ad_mr(
1110         AttributeDescription *ad,
1111         unsigned usage )
1112 {
1113         switch( usage & SLAP_MR_TYPE_MASK ) {
1114         case SLAP_MR_NONE:
1115         case SLAP_MR_EQUALITY:
1116                 return ad->ad_type->sat_equality;
1117                 break;
1118         case SLAP_MR_ORDERING:
1119                 return ad->ad_type->sat_ordering;
1120                 break;
1121         case SLAP_MR_SUBSTR:
1122                 return ad->ad_type->sat_substr;
1123                 break;
1124         case SLAP_MR_EXT:
1125         default:
1126                 assert( 0 /* ad_mr: bad usage */);
1127         }
1128         return NULL;
1129 }