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