]> git.sur5r.net Git - openldap/blob - servers/slapd/ad.c
ACIs almost entirely factored out of slapd
[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 && ( options - name ) < bv->bv_len ) {
182                 /* don't go past the end of the berval! */
183                 desc.ad_cname.bv_len = options - name;
184         } else {
185                 options = NULL;
186         }
187         desc.ad_type = at_bvfind( &desc.ad_cname );
188         if( desc.ad_type == NULL ) {
189                 *text = "attribute type undefined";
190                 return rtn;
191         }
192
193         if( is_at_operational( desc.ad_type ) && options != NULL ) {
194                 *text = "operational attribute with options undefined";
195                 return rtn;
196         }
197
198         /*
199          * parse options in place
200          */
201         ntags = 0;
202         memset( tags, 0, sizeof( tags ));
203         tagslen = 0;
204
205         for( opt=options; opt != NULL; opt=next ) {
206                 int optlen;
207                 opt++; 
208                 next = strchrlen( opt, ';', &optlen );
209
210                 if( optlen == 0 ) {
211                         *text = "zero length option is invalid";
212                         return rtn;
213                 
214                 } else if ( optlen == sizeof("binary")-1 &&
215                         strncasecmp( opt, "binary", sizeof("binary")-1 ) == 0 )
216                 {
217                         /* binary option */
218                         if( slap_ad_is_binary( &desc ) ) {
219                                 *text = "option \"binary\" specified multiple times";
220                                 return rtn;
221                         }
222
223                         if( !slap_syntax_is_binary( desc.ad_type->sat_syntax )) {
224                                 /* not stored in binary, disallow option */
225                                 *text = "option \"binary\" not supported with type";
226                                 return rtn;
227                         }
228
229                         desc.ad_flags |= SLAP_DESC_BINARY;
230                         continue;
231
232                 } else if ( ad_find_option_definition( opt, optlen ) ) {
233                         int i;
234
235                         if( opt[optlen-1] == '-' ) {
236                                 desc.ad_flags |= SLAP_DESC_TAG_RANGE;
237                         }
238
239                         if( ntags >= MAX_TAGGING_OPTIONS ) {
240                                 *text = "too many tagging options";
241                                 return rtn;
242                         }
243
244                         /*
245                          * tags should be presented in sorted order,
246                          * so run the array in reverse.
247                          */
248                         for( i=ntags-1; i>=0; i-- ) {
249                                 int rc;
250
251                                 rc = strncasecmp( opt, tags[i].bv_val,
252                                         (unsigned) optlen < tags[i].bv_len
253                                                 ? optlen : tags[i].bv_len );
254
255                                 if( rc == 0 && (unsigned)optlen == tags[i].bv_len ) {
256                                         /* duplicate (ignore) */
257                                         goto done;
258
259                                 } else if ( rc > 0 ||
260                                         ( rc == 0 && (unsigned)optlen > tags[i].bv_len ))
261                                 {
262                                         AC_MEMCPY( &tags[i+2], &tags[i+1],
263                                                 (ntags-i-1)*sizeof(struct berval) );
264                                         tags[i+1].bv_val = opt;
265                                         tags[i+1].bv_len = optlen;
266                                         goto done;
267                                 }
268                         }
269
270                         if( ntags ) {
271                                 AC_MEMCPY( &tags[1], &tags[0],
272                                         ntags*sizeof(struct berval) );
273                         }
274                         tags[0].bv_val = opt;
275                         tags[0].bv_len = optlen;
276
277 done:;
278                         tagslen += optlen + 1;
279                         ntags++;
280
281                 } else {
282                         *text = "unrecognized option";
283                         return rtn;
284                 }
285         }
286
287         if( ntags > 0 ) {
288                 int i;
289
290                 if( tagslen > MAX_TAGS_LEN ) {
291                         *text = "tagging options too long";
292                         return rtn;
293                 }
294
295                 desc.ad_tags.bv_val = tagbuf;
296                 tagslen = 0;
297
298                 for( i=0; i<ntags; i++ ) {
299                         AC_MEMCPY( &desc.ad_tags.bv_val[tagslen],
300                                 tags[i].bv_val, tags[i].bv_len );
301
302                         tagslen += tags[i].bv_len;
303                         desc.ad_tags.bv_val[tagslen++] = ';';
304                 }
305
306                 desc.ad_tags.bv_val[--tagslen] = '\0';
307                 desc.ad_tags.bv_len = tagslen;
308         }
309
310         /* see if a matching description is already cached */
311         for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
312                 if( d2->ad_flags != desc.ad_flags ) {
313                         continue;
314                 }
315                 if( d2->ad_tags.bv_len != desc.ad_tags.bv_len ) {
316                         continue;
317                 }
318                 if( d2->ad_tags.bv_len == 0 ) {
319                         break;
320                 }
321                 if( strncasecmp( d2->ad_tags.bv_val, desc.ad_tags.bv_val,
322                         desc.ad_tags.bv_len ) == 0 )
323                 {
324                         break;
325                 }
326         }
327
328         /* Not found, add new one */
329         while (d2 == NULL) {
330                 size_t dlen = 0;
331                 ldap_pvt_thread_mutex_lock( &desc.ad_type->sat_ad_mutex );
332                 /* check again now that we've locked */
333                 for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
334                         if (d2->ad_flags != desc.ad_flags)
335                                 continue;
336                         if (d2->ad_tags.bv_len != desc.ad_tags.bv_len)
337                                 continue;
338                         if (d2->ad_tags.bv_len == 0)
339                                 break;
340                         if (strncasecmp(d2->ad_tags.bv_val, desc.ad_tags.bv_val,
341                                 desc.ad_tags.bv_len) == 0)
342                                 break;
343                 }
344                 if (d2) {
345                         ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
346                         break;
347                 }
348
349                 /* Allocate a single contiguous block. If there are no
350                  * options, we just need space for the AttrDesc structure.
351                  * Otherwise, we need to tack on the full name length +
352                  * options length, + maybe tagging options length again.
353                  */
354                 if (desc.ad_tags.bv_len || desc.ad_flags != SLAP_DESC_NONE) {
355                         dlen = desc.ad_type->sat_cname.bv_len + 1;
356                         if (desc.ad_tags.bv_len) {
357                                 dlen += 1+desc.ad_tags.bv_len;
358                         }
359                         if( slap_ad_is_binary( &desc ) ) {
360                                 dlen += sizeof(";binary")+desc.ad_tags.bv_len;
361                         }
362                 }
363
364                 d2 = ch_malloc(sizeof(AttributeDescription) + dlen);
365                 d2->ad_next = NULL;
366                 d2->ad_type = desc.ad_type;
367                 d2->ad_flags = desc.ad_flags;
368                 d2->ad_cname.bv_len = desc.ad_type->sat_cname.bv_len;
369                 d2->ad_tags.bv_len = desc.ad_tags.bv_len;
370
371                 if (dlen == 0) {
372                         d2->ad_cname.bv_val = d2->ad_type->sat_cname.bv_val;
373                         d2->ad_tags.bv_val = NULL;
374                 } else {
375                         char *cp, *op, *lp;
376                         int j;
377                         d2->ad_cname.bv_val = (char *)(d2+1);
378                         strcpy(d2->ad_cname.bv_val, d2->ad_type->sat_cname.bv_val);
379                         cp = d2->ad_cname.bv_val + d2->ad_cname.bv_len;
380                         if( slap_ad_is_binary( &desc ) ) {
381                                 op = cp;
382                                 lp = NULL;
383                                 if( desc.ad_tags.bv_len ) {
384                                         lp = desc.ad_tags.bv_val;
385                                         while( strncasecmp(lp, "binary", sizeof("binary")-1) < 0
386                                                && (lp = strchr( lp, ';' )) != NULL )
387                                                 ++lp;
388                                         if( lp != desc.ad_tags.bv_val ) {
389                                                 *cp++ = ';';
390                                                 j = (lp
391                                                      ? lp - desc.ad_tags.bv_val - 1
392                                                      : strlen( desc.ad_tags.bv_val ));
393                                                 cp = lutil_strncopy(cp, desc.ad_tags.bv_val, j);
394                                         }
395                                 }
396                                 cp = lutil_strcopy(cp, ";binary");
397                                 if( lp != NULL ) {
398                                         *cp++ = ';';
399                                         cp = lutil_strcopy(cp, lp);
400                                 }
401                                 d2->ad_cname.bv_len = cp - d2->ad_cname.bv_val;
402                                 if( desc.ad_tags.bv_len )
403                                         ldap_pvt_str2lower(op);
404                                 j = 1;
405                         } else {
406                                 j = 0;
407                         }
408                         if( desc.ad_tags.bv_len ) {
409                                 lp = d2->ad_cname.bv_val + d2->ad_cname.bv_len + j;
410                                 if ( j == 0 )
411                                         *lp++ = ';';
412                                 d2->ad_tags.bv_val = lp;
413                                 strcpy(lp, desc.ad_tags.bv_val);
414                                 ldap_pvt_str2lower(lp);
415                                 if( j == 0 )
416                                         d2->ad_cname.bv_len += 1 + desc.ad_tags.bv_len;
417                         }
418                 }
419                 /* Add new desc to list. We always want the bare Desc with
420                  * no options to stay at the head of the list, assuming
421                  * that one will be used most frequently.
422                  */
423                 if (desc.ad_type->sat_ad == NULL || dlen == 0) {
424                         d2->ad_next = desc.ad_type->sat_ad;
425                         desc.ad_type->sat_ad = d2;
426                 } else {
427                         d2->ad_next = desc.ad_type->sat_ad->ad_next;
428                         desc.ad_type->sat_ad->ad_next = d2;
429                 }
430                 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
431         }
432
433         if( *ad == NULL ) {
434                 *ad = d2;
435         } else {
436                 **ad = *d2;
437         }
438
439         return LDAP_SUCCESS;
440 }
441
442 static int is_ad_subtags(
443         struct berval *subtagsbv, 
444         struct berval *suptagsbv )
445 {
446         const char *suptags, *supp, *supdelimp;
447         const char *subtags, *subp, *subdelimp;
448         int  suplen, sublen;
449
450         subtags =subtagsbv->bv_val;
451         suptags =suptagsbv->bv_val;
452
453         for( supp=suptags ; supp; supp=supdelimp ) {
454                 supdelimp = strchrlen( supp, ';', &suplen );
455                 if( supdelimp ) supdelimp++;
456
457                 for( subp=subtags ; subp; subp=subdelimp ) {
458                         subdelimp = strchrlen( subp, ';', &sublen );
459                         if( subdelimp ) subdelimp++;
460
461                         if ( suplen > sublen
462                                  ? ( suplen-1 == sublen && supp[suplen-1] == '-'
463                                          && strncmp( supp, subp, sublen ) == 0 )
464                                  : ( ( suplen == sublen || supp[suplen-1] == '-' )
465                                          && strncmp( supp, subp, suplen ) == 0 ) )
466                         {
467                                 goto match;
468                         }
469                 }
470
471                 return 0;
472 match:;
473         }
474         return 1;
475 }
476
477 int is_ad_subtype(
478         AttributeDescription *sub,
479         AttributeDescription *super
480 )
481 {
482         AttributeType *a;
483         int lr;
484
485         for ( a = sub->ad_type; a; a=a->sat_sup ) {
486                 if ( a == super->ad_type ) break;
487         }
488         if( !a ) {
489                 return 0;
490         }
491
492         /* ensure sub does support all flags of super */
493         lr = sub->ad_tags.bv_len ? SLAP_DESC_TAG_RANGE : 0;
494         if(( super->ad_flags & ( sub->ad_flags | lr )) != super->ad_flags ) {
495                 return 0;
496         }
497
498         /* check for tagging options */
499         if ( super->ad_tags.bv_len == 0 )
500                 return 1;
501         if ( sub->ad_tags.bv_len == 0 )
502                 return 0;
503
504         return is_ad_subtags( &sub->ad_tags, &super->ad_tags );
505 }
506
507 int ad_inlist(
508         AttributeDescription *desc,
509         AttributeName *attrs )
510 {
511         if (! attrs ) return 0;
512
513         for( ; attrs->an_name.bv_val; attrs++ ) {
514                 AttributeType *a;
515                 ObjectClass *oc;
516                 
517                 if ( attrs->an_desc ) {
518                         int lr;
519
520                         if ( desc == attrs->an_desc ) {
521                                 return 1;
522                         }
523
524                         /*
525                          * EXTENSION: if requested description is preceeded by
526                          * a '-' character, do not match on subtypes.
527                          */
528                         if ( attrs->an_name.bv_val[0] == '-' ) {
529                                 continue;
530                         }
531                         
532                         /* Is this a subtype of the requested attr? */
533                         for (a = desc->ad_type; a; a=a->sat_sup) {
534                                 if ( a == attrs->an_desc->ad_type )
535                                         break;
536                         }
537                         if ( !a ) {
538                                 continue;
539                         }
540                         /* Does desc support all the requested flags? */
541                         lr = desc->ad_tags.bv_len ? SLAP_DESC_TAG_RANGE : 0;
542                         if(( attrs->an_desc->ad_flags & (desc->ad_flags | lr))
543                                 != attrs->an_desc->ad_flags ) {
544                                 continue;
545                         }
546                         /* Do the descs have compatible tags? */
547                         if ( attrs->an_desc->ad_tags.bv_len == 0 ) {
548                                 return 1;
549                         }
550                         if ( desc->ad_tags.bv_len == 0) {
551                                 continue;
552                         }
553                         if ( is_ad_subtags( &desc->ad_tags,
554                                 &attrs->an_desc->ad_tags ) ) {
555                                 return 1;
556                         }
557                         continue;
558                 }
559
560                 /*
561                  * EXTENSION: see if requested description is @objectClass
562                  * if so, return attributes which the class requires/allows
563                  * else if requested description is !objectClass, return
564                  * attributes which the class does not require/allow
565                  */
566                 oc = attrs->an_oc;
567                 if( oc == NULL && attrs->an_name.bv_val ) {
568                         switch( attrs->an_name.bv_val[0] ) {
569                         case '@': /* @objectClass */
570                         case '+': /* +objectClass (deprecated) */
571                         case '!': { /* exclude */
572                                         struct berval ocname;
573                                         ocname.bv_len = attrs->an_name.bv_len - 1;
574                                         ocname.bv_val = &attrs->an_name.bv_val[1];
575                                         oc = oc_bvfind( &ocname );
576                                         attrs->an_oc_exclude = 0;
577                                         if ( oc && attrs->an_name.bv_val[0] == '!' ) {
578                                                 attrs->an_oc_exclude = 1;
579                                         }
580                                 } break;
581
582                         default: /* old (deprecated) way */
583                                 oc = oc_bvfind( &attrs->an_name );
584                         }
585                         attrs->an_oc = oc;
586                 }
587                 if( oc != NULL ) {
588                         if ( attrs->an_oc_exclude ) {
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         i = 0;
782         if ( an != NULL ) {
783                 for ( i = 0; !BER_BVISNULL( &an[ i ].an_name ) ; i++)
784                         ;
785         }
786         
787         /* protect the input string from strtok */
788         str = ch_strdup( in );
789
790         /* Count words in string */
791         j = 1;
792         for ( s = str; *s; s++ ) {
793                 if ( strchr( brkstr, *s ) != NULL ) {
794                         j++;
795                 }
796         }
797
798         an = ch_realloc( an, ( i + j + 1 ) * sizeof( AttributeName ) );
799         BER_BVZERO( &an[i + j].an_name );
800         anew = an + i;
801         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
802                 s != NULL;
803                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
804         {
805                 anew->an_desc = NULL;
806                 anew->an_oc = NULL;
807                 anew->an_oc_exclude = 0;
808                 ber_str2bv(s, 0, 1, &anew->an_name);
809                 slap_bv2ad(&anew->an_name, &anew->an_desc, &text);
810                 if ( !anew->an_desc ) {
811                         switch( anew->an_name.bv_val[0] ) {
812                         case '-': {
813                                         struct berval adname;
814                                         adname.bv_len = anew->an_name.bv_len - 1;
815                                         adname.bv_val = &anew->an_name.bv_val[1];
816                                         slap_bv2ad(&adname, &anew->an_desc, &text);
817                                         if ( !anew->an_desc ) {
818                                                 goto reterr;
819                                         }
820                                 } break;
821
822                         case '@':
823                         case '+': /* (deprecated) */
824                         case '!': {
825                                         struct berval ocname;
826                                         ocname.bv_len = anew->an_name.bv_len - 1;
827                                         ocname.bv_val = &anew->an_name.bv_val[1];
828                                         anew->an_oc = oc_bvfind( &ocname );
829                                         if ( !anew->an_oc ) {
830                                                 goto reterr;
831                                         }
832
833                                         if ( anew->an_name.bv_val[0] == '!' ) {
834                                                 anew->an_oc_exclude = 1;
835                                         }
836                                 } break;
837
838                         default:
839                                 /* old (deprecated) way */
840                                 anew->an_oc = oc_bvfind( &anew->an_name );
841                                 if ( !anew->an_oc ) {
842                                         goto reterr;
843                                 }
844                         }
845                 }
846                 anew++;
847         }
848
849         BER_BVZERO( &anew->an_name );
850         free( str );
851         return( an );
852
853 reterr:
854         for ( i = 0; an[i].an_name.bv_val; i++ ) {
855                 free( an[i].an_name.bv_val );
856         }
857         free( an );
858         /*
859          * overwrites input string
860          * on error!
861          */
862         strcpy( in, s );
863         free( str );
864         return NULL;
865 }
866
867 char **anlist2charray_x( AttributeName *an, int dup, void *ctx )
868 {
869     char **attrs;
870     int i;
871                                                                                 
872     if ( an != NULL ) {
873         for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ )
874             ;
875                 attrs = (char **) slap_sl_malloc( (i + 1) * sizeof(char *), ctx );
876         for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
877                         if ( dup )
878                     attrs[i] = ch_strdup( an[i].an_name.bv_val );
879                         else
880                     attrs[i] = an[i].an_name.bv_val;
881         }
882         attrs[i] = NULL;
883     } else {
884         attrs = NULL;
885     }
886                                                                                 
887     return attrs;
888 }
889
890 char **anlist2charray( AttributeName *an, int dup )
891 {
892         return anlist2charray_x( an, dup, NULL );
893 }
894
895 char**
896 anlist2attrs( AttributeName * anlist )
897 {
898         int i, j, k = 0;
899         int n;
900         char **attrs;
901         ObjectClass *oc;
902
903         attrs = anlist2charray( anlist, 1 );
904                                                                                 
905         for ( i = 0; anlist[i].an_name.bv_val; i++ ) {
906                 if ( ( oc = anlist[i].an_oc ) ) {
907                         for ( j = 0; oc->soc_required && oc->soc_required[j]; j++ ) ;
908                         k += j;
909                         for ( j = 0; oc->soc_allowed && oc->soc_allowed[j]; j++ ) ;
910                         k += j;
911                 }
912         }
913
914         if ( i == 0 )
915                 return NULL;
916                                                                                 
917         n = i;
918                                                                                 
919         if ( k )
920                 attrs = (char **) ch_realloc( attrs, (i + k + 1) * sizeof( char * ));
921
922         for ( i = 0; anlist[i].an_name.bv_val; i++ ) {
923                 if ( ( oc = anlist[i].an_oc ) ) {
924                         for ( j = 0; oc->soc_required && oc->soc_required[j]; j++ ) {
925                                 attrs[n++] = ch_strdup(
926                                                                 oc->soc_required[j]->sat_cname.bv_val );
927                         }
928                         for ( j = 0; oc->soc_allowed && oc->soc_allowed[j]; j++ ) {
929                                 attrs[n++] = ch_strdup(
930                                                                 oc->soc_allowed[j]->sat_cname.bv_val );
931                         }
932                 }
933         }
934         
935         if ( attrs )
936                 attrs[n] = NULL;
937
938         i = 0;
939         while ( attrs && attrs[i] ) {
940                 if ( *attrs[i] == '@' ) {
941                         for ( j = i; attrs[j]; j++ ) {
942                                 if ( j == i )
943                                         ch_free( attrs[i] );
944                                 attrs[j] = attrs[j+1];
945                         }
946                 } else {
947                         i++;
948                 }
949         }
950
951         for ( i = 0; attrs && attrs[i]; i++ ) {
952                 j = i + 1;
953                 while ( attrs && attrs[j] ) {
954                         if ( !strcmp( attrs[i], attrs[j] )) {
955                                 for ( k = j; attrs && attrs[k]; k++ ) {
956                                         if ( k == j )
957                                                 ch_free( attrs[j] );
958                                         attrs[k] = attrs[k+1];
959                                 }
960                         } else {
961                                 j++;
962                         }
963                 }
964         }
965
966         if ( i != n )
967                 attrs = (char **) ch_realloc( attrs, (i+1) * sizeof( char * ));
968
969         return attrs;
970 }
971
972 #define LBUFSIZ 80
973 AttributeName*
974 file2anlist( AttributeName *an, const char *fname, const char *brkstr )
975 {
976         FILE    *fp;
977         char    *line = NULL;
978         char    *lcur = NULL;
979         char    *c;
980         size_t  lmax = LBUFSIZ;
981
982         fp = fopen( fname, "r" );
983         if ( fp == NULL ) {
984                 Debug( LDAP_DEBUG_ANY,
985                         "get_attrs_from_file: failed to open attribute list file "
986                         "\"%s\": %s\n", fname, strerror(errno), 0 );
987                 return NULL;
988         }
989
990         lcur = line = (char *) ch_malloc( lmax );
991         if ( !line ) {
992                 Debug( LDAP_DEBUG_ANY,
993                         "get_attrs_from_file: could not allocate memory\n",
994                         0, 0, 0 );
995                 fclose(fp);
996                 return NULL;
997         }
998
999         while ( fgets( lcur, LBUFSIZ, fp ) != NULL ) {
1000                 if ( ( c = strchr( lcur, '\n' ) ) ) {
1001                         if ( c == line ) {
1002                                 *c = '\0';
1003                         } else if ( *(c-1) == '\r' ) {
1004                                 *(c-1) = '\0';
1005                         } else {
1006                                 *c = '\0';
1007                         }
1008                 } else {
1009                         lmax += LBUFSIZ;
1010                         line = (char *) ch_realloc( line, lmax );
1011                         if ( !line ) {
1012                                 Debug( LDAP_DEBUG_ANY,
1013                                         "get_attrs_from_file: could not allocate memory\n",
1014                                         0, 0, 0 );
1015                                 fclose(fp);
1016                                 return NULL;
1017                         }
1018                         lcur = line + strlen( line );
1019                         continue;
1020                 }
1021                 an = str2anlist( an, line, brkstr );
1022                 if ( an == NULL )
1023                         return NULL;
1024                 lcur = line;
1025         }
1026         ch_free( line );
1027         fclose(fp);
1028         return an;
1029 }
1030 #undef LBUFSIZ
1031
1032 /* Define an attribute option. */
1033 int
1034 ad_define_option( const char *name, const char *fname, int lineno )
1035 {
1036         int i;
1037         unsigned int optlen;
1038
1039         if ( options == &lang_option ) {
1040                 options = NULL;
1041                 option_count = 0;
1042         }
1043         if ( name == NULL )
1044                 return 0;
1045
1046         optlen = 0;
1047         do {
1048                 if ( !DESC_CHAR( name[optlen] ) ) {
1049                         Debug( LDAP_DEBUG_ANY,
1050                                "%s: line %d: illegal option name \"%s\"\n",
1051                                     fname, lineno, name );
1052                         return 1;
1053                 }
1054         } while ( name[++optlen] );
1055
1056         options = ch_realloc( options,
1057                 (option_count+1) * sizeof(Attr_option) );
1058
1059         if ( strcasecmp( name, "binary" ) == 0
1060              || ad_find_option_definition( name, optlen ) ) {
1061                 Debug( LDAP_DEBUG_ANY,
1062                        "%s: line %d: option \"%s\" is already defined\n",
1063                        fname, lineno, name );
1064                 return 1;
1065         }
1066
1067         for ( i = option_count; i; --i ) {
1068                 if ( strcasecmp( name, options[i-1].name.bv_val ) >= 0 )
1069                         break;
1070                 options[i] = options[i-1];
1071         }
1072
1073         options[i].name.bv_val = ch_strdup( name );
1074         options[i].name.bv_len = optlen;
1075         options[i].prefix = (name[optlen-1] == '-');
1076
1077         if ( i != option_count &&
1078              options[i].prefix &&
1079              optlen < options[i+1].name.bv_len &&
1080              strncasecmp( name, options[i+1].name.bv_val, optlen ) == 0 ) {
1081                         Debug( LDAP_DEBUG_ANY,
1082                                "%s: line %d: option \"%s\" overrides previous option\n",
1083                                     fname, lineno, name );
1084                         return 1;
1085         }
1086
1087         option_count++;
1088         return 0;
1089 }
1090
1091 /* Find the definition of the option name or prefix matching the arguments */
1092 static Attr_option *
1093 ad_find_option_definition( const char *opt, int optlen )
1094 {
1095         int top = 0, bot = option_count;
1096         while ( top < bot ) {
1097                 int mid = (top + bot) / 2;
1098                 int mlen = options[mid].name.bv_len;
1099                 char *mname = options[mid].name.bv_val;
1100                 int j;
1101                 if ( optlen < mlen ) {
1102                         j = strncasecmp( opt, mname, optlen ) - 1;
1103                 } else {
1104                         j = strncasecmp( opt, mname, mlen );
1105                         if ( j==0 && (optlen==mlen || options[mid].prefix) )
1106                                 return &options[mid];
1107                 }
1108                 if ( j < 0 )
1109                         bot = mid;
1110                 else
1111                         top = mid + 1;
1112         }
1113         return NULL;
1114 }
1115
1116 MatchingRule *ad_mr(
1117         AttributeDescription *ad,
1118         unsigned usage )
1119 {
1120         switch( usage & SLAP_MR_TYPE_MASK ) {
1121         case SLAP_MR_NONE:
1122         case SLAP_MR_EQUALITY:
1123                 return ad->ad_type->sat_equality;
1124                 break;
1125         case SLAP_MR_ORDERING:
1126                 return ad->ad_type->sat_ordering;
1127                 break;
1128         case SLAP_MR_SUBSTR:
1129                 return ad->ad_type->sat_substr;
1130                 break;
1131         case SLAP_MR_EXT:
1132         default:
1133                 assert( 0 /* ad_mr: bad usage */);
1134         }
1135         return NULL;
1136 }