]> git.sur5r.net Git - openldap/blob - servers/slapd/ad.c
fix previous commit
[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         i = 0;
779         if ( an != NULL ) {
780                 for ( i = 0; !BER_BVISNULL( &an[ i ].an_name ) ; i++)
781                         ;
782         }
783         
784         /* protect the input string from strtok */
785         str = ch_strdup( in );
786
787         /* Count words in string */
788         j = 1;
789         for ( s = str; *s; s++ ) {
790                 if ( strchr( brkstr, *s ) != NULL ) {
791                         j++;
792                 }
793         }
794
795         an = ch_realloc( an, ( i + j + 1 ) * sizeof( AttributeName ) );
796         BER_BVZERO( &an[i + j].an_name );
797         anew = an + i;
798         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
799                 s != NULL;
800                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
801         {
802                 anew->an_desc = NULL;
803                 anew->an_oc = NULL;
804                 anew->an_oc_exclude = 0;
805                 ber_str2bv(s, 0, 1, &anew->an_name);
806                 slap_bv2ad(&anew->an_name, &anew->an_desc, &text);
807                 if ( !anew->an_desc ) {
808                         switch( anew->an_name.bv_val[0] ) {
809                         case '-': {
810                                         struct berval adname;
811                                         adname.bv_len = anew->an_name.bv_len - 1;
812                                         adname.bv_val = &anew->an_name.bv_val[1];
813                                         slap_bv2ad(&adname, &anew->an_desc, &text);
814                                         if ( !anew->an_desc ) {
815                                                 goto reterr;
816                                         }
817                                 } break;
818
819                         case '@':
820                         case '+': /* (deprecated) */
821                         case '!': {
822                                         struct berval ocname;
823                                         ocname.bv_len = anew->an_name.bv_len - 1;
824                                         ocname.bv_val = &anew->an_name.bv_val[1];
825                                         anew->an_oc = oc_bvfind( &ocname );
826                                         if ( !anew->an_oc ) {
827                                                 goto reterr;
828                                         }
829
830                                         if ( anew->an_name.bv_val[0] == '!' ) {
831                                                 anew->an_oc_exclude = 1;
832                                         }
833                                 } break;
834
835                         default:
836                                 /* old (deprecated) way */
837                                 anew->an_oc = oc_bvfind( &anew->an_name );
838                                 if ( !anew->an_oc ) {
839                                         goto reterr;
840                                 }
841                         }
842                 }
843                 anew++;
844         }
845
846         BER_BVZERO( &anew->an_name );
847         free( str );
848         return( an );
849
850 reterr:
851         for ( i = 0; an[i].an_name.bv_val; i++ ) {
852                 free( an[i].an_name.bv_val );
853         }
854         free( an );
855         /*
856          * overwrites input string
857          * on error!
858          */
859         strcpy( in, s );
860         free( str );
861         return NULL;
862 }
863
864 char **anlist2charray_x( AttributeName *an, int dup, void *ctx )
865 {
866     char **attrs;
867     int i;
868                                                                                 
869     if ( an != NULL ) {
870         for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ )
871             ;
872                 attrs = (char **) slap_sl_malloc( (i + 1) * sizeof(char *), ctx );
873         for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
874                         if ( dup )
875                     attrs[i] = ch_strdup( an[i].an_name.bv_val );
876                         else
877                     attrs[i] = an[i].an_name.bv_val;
878         }
879         attrs[i] = NULL;
880     } else {
881         attrs = NULL;
882     }
883                                                                                 
884     return attrs;
885 }
886
887 char **anlist2charray( AttributeName *an, int dup )
888 {
889         return anlist2charray_x( an, dup, NULL );
890 }
891
892 char**
893 anlist2attrs( AttributeName * anlist )
894 {
895         int i, j, k = 0;
896         int n;
897         char **attrs;
898         ObjectClass *oc;
899
900         attrs = anlist2charray( anlist, 1 );
901                                                                                 
902         for ( i = 0; anlist[i].an_name.bv_val; i++ ) {
903                 if ( ( oc = anlist[i].an_oc ) ) {
904                         for ( j = 0; oc->soc_required && oc->soc_required[j]; j++ ) ;
905                         k += j;
906                         for ( j = 0; oc->soc_allowed && oc->soc_allowed[j]; j++ ) ;
907                         k += j;
908                 }
909         }
910
911         if ( i == 0 )
912                 return NULL;
913                                                                                 
914         n = i;
915                                                                                 
916         if ( k )
917                 attrs = (char **) ch_realloc( attrs, (i + k + 1) * sizeof( char * ));
918
919         for ( i = 0; anlist[i].an_name.bv_val; i++ ) {
920                 if ( ( oc = anlist[i].an_oc ) ) {
921                         for ( j = 0; oc->soc_required && oc->soc_required[j]; j++ ) {
922                                 attrs[n++] = ch_strdup(
923                                                                 oc->soc_required[j]->sat_cname.bv_val );
924                         }
925                         for ( j = 0; oc->soc_allowed && oc->soc_allowed[j]; j++ ) {
926                                 attrs[n++] = ch_strdup(
927                                                                 oc->soc_allowed[j]->sat_cname.bv_val );
928                         }
929                 }
930         }
931         
932         if ( attrs )
933                 attrs[n] = NULL;
934
935         i = 0;
936         while ( attrs && attrs[i] ) {
937                 if ( *attrs[i] == '@' ) {
938                         for ( j = i; attrs[j]; j++ ) {
939                                 if ( j == i )
940                                         ch_free( attrs[i] );
941                                 attrs[j] = attrs[j+1];
942                         }
943                 } else {
944                         i++;
945                 }
946         }
947
948         for ( i = 0; attrs && attrs[i]; i++ ) {
949                 j = i + 1;
950                 while ( attrs && attrs[j] ) {
951                         if ( !strcmp( attrs[i], attrs[j] )) {
952                                 for ( k = j; attrs && attrs[k]; k++ ) {
953                                         if ( k == j )
954                                                 ch_free( attrs[j] );
955                                         attrs[k] = attrs[k+1];
956                                 }
957                         } else {
958                                 j++;
959                         }
960                 }
961         }
962
963         if ( i != n )
964                 attrs = (char **) ch_realloc( attrs, (i+1) * sizeof( char * ));
965
966         return attrs;
967 }
968
969 #define LBUFSIZ 80
970 AttributeName*
971 file2anlist( AttributeName *an, const char *fname, const char *brkstr )
972 {
973         FILE    *fp;
974         char    *line = NULL;
975         char    *lcur = NULL;
976         char    *c;
977         size_t  lmax = LBUFSIZ;
978
979         fp = fopen( fname, "r" );
980         if ( fp == NULL ) {
981                 Debug( LDAP_DEBUG_ANY,
982                         "get_attrs_from_file: failed to open attribute list file "
983                         "\"%s\": %s\n", fname, strerror(errno), 0 );
984                 return NULL;
985         }
986
987         lcur = line = (char *) ch_malloc( lmax );
988         if ( !line ) {
989                 Debug( LDAP_DEBUG_ANY,
990                         "get_attrs_from_file: could not allocate memory\n",
991                         0, 0, 0 );
992                 fclose(fp);
993                 return NULL;
994         }
995
996         while ( fgets( lcur, LBUFSIZ, fp ) != NULL ) {
997                 if ( ( c = strchr( lcur, '\n' ) ) ) {
998                         if ( c == line ) {
999                                 *c = '\0';
1000                         } else if ( *(c-1) == '\r' ) {
1001                                 *(c-1) = '\0';
1002                         } else {
1003                                 *c = '\0';
1004                         }
1005                 } else {
1006                         lmax += LBUFSIZ;
1007                         line = (char *) ch_realloc( line, lmax );
1008                         if ( !line ) {
1009                                 Debug( LDAP_DEBUG_ANY,
1010                                         "get_attrs_from_file: could not allocate memory\n",
1011                                         0, 0, 0 );
1012                                 fclose(fp);
1013                                 return NULL;
1014                         }
1015                         lcur = line + strlen( line );
1016                         continue;
1017                 }
1018                 an = str2anlist( an, line, brkstr );
1019                 if ( an == NULL )
1020                         return NULL;
1021                 lcur = line;
1022         }
1023         ch_free( line );
1024         fclose(fp);
1025         return an;
1026 }
1027 #undef LBUFSIZ
1028
1029 /* Define an attribute option. */
1030 int
1031 ad_define_option( const char *name, const char *fname, int lineno )
1032 {
1033         int i;
1034         unsigned int optlen;
1035
1036         if ( options == &lang_option ) {
1037                 options = NULL;
1038                 option_count = 0;
1039         }
1040         if ( name == NULL )
1041                 return 0;
1042
1043         optlen = 0;
1044         do {
1045                 if ( !DESC_CHAR( name[optlen] ) ) {
1046                         Debug( LDAP_DEBUG_ANY,
1047                                "%s: line %d: illegal option name \"%s\"\n",
1048                                     fname, lineno, name );
1049                         return 1;
1050                 }
1051         } while ( name[++optlen] );
1052
1053         options = ch_realloc( options,
1054                 (option_count+1) * sizeof(Attr_option) );
1055
1056         if ( strcasecmp( name, "binary" ) == 0
1057              || ad_find_option_definition( name, optlen ) ) {
1058                 Debug( LDAP_DEBUG_ANY,
1059                        "%s: line %d: option \"%s\" is already defined\n",
1060                        fname, lineno, name );
1061                 return 1;
1062         }
1063
1064         for ( i = option_count; i; --i ) {
1065                 if ( strcasecmp( name, options[i-1].name.bv_val ) >= 0 )
1066                         break;
1067                 options[i] = options[i-1];
1068         }
1069
1070         options[i].name.bv_val = ch_strdup( name );
1071         options[i].name.bv_len = optlen;
1072         options[i].prefix = (name[optlen-1] == '-');
1073
1074         if ( i != option_count &&
1075              options[i].prefix &&
1076              optlen < options[i+1].name.bv_len &&
1077              strncasecmp( name, options[i+1].name.bv_val, optlen ) == 0 ) {
1078                         Debug( LDAP_DEBUG_ANY,
1079                                "%s: line %d: option \"%s\" overrides previous option\n",
1080                                     fname, lineno, name );
1081                         return 1;
1082         }
1083
1084         option_count++;
1085         return 0;
1086 }
1087
1088 /* Find the definition of the option name or prefix matching the arguments */
1089 static Attr_option *
1090 ad_find_option_definition( const char *opt, int optlen )
1091 {
1092         int top = 0, bot = option_count;
1093         while ( top < bot ) {
1094                 int mid = (top + bot) / 2;
1095                 int mlen = options[mid].name.bv_len;
1096                 char *mname = options[mid].name.bv_val;
1097                 int j;
1098                 if ( optlen < mlen ) {
1099                         j = strncasecmp( opt, mname, optlen ) - 1;
1100                 } else {
1101                         j = strncasecmp( opt, mname, mlen );
1102                         if ( j==0 && (optlen==mlen || options[mid].prefix) )
1103                                 return &options[mid];
1104                 }
1105                 if ( j < 0 )
1106                         bot = mid;
1107                 else
1108                         top = mid + 1;
1109         }
1110         return NULL;
1111 }
1112
1113 MatchingRule *ad_mr(
1114         AttributeDescription *ad,
1115         unsigned usage )
1116 {
1117         switch( usage & SLAP_MR_TYPE_MASK ) {
1118         case SLAP_MR_NONE:
1119         case SLAP_MR_EQUALITY:
1120                 return ad->ad_type->sat_equality;
1121                 break;
1122         case SLAP_MR_ORDERING:
1123                 return ad->ad_type->sat_ordering;
1124                 break;
1125         case SLAP_MR_SUBSTR:
1126                 return ad->ad_type->sat_substr;
1127                 break;
1128         case SLAP_MR_EXT:
1129         default:
1130                 assert( 0 /* ad_mr: bad usage */);
1131         }
1132         return NULL;
1133 }