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