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