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