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