1 /* ad.c - routines for dealing with attribute descriptions */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
23 #include <ac/socket.h>
24 #include <ac/string.h>
30 static AttributeName anlist_no_attrs[] = {
31 { BER_BVC( LDAP_NO_ATTRS ), NULL, 0, NULL },
32 { BER_BVNULL, NULL, 0, NULL }
35 static AttributeName anlist_all_user_attributes[] = {
36 { BER_BVC( LDAP_ALL_USER_ATTRIBUTES ), NULL, 0, NULL },
37 { BER_BVNULL, NULL, 0, NULL }
40 static AttributeName anlist_all_operational_attributes[] = {
41 { BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES ), NULL, 0, NULL },
42 { BER_BVNULL, NULL, 0, NULL }
45 static AttributeName anlist_all_attributes[] = {
46 { BER_BVC( LDAP_ALL_USER_ATTRIBUTES ), NULL, 0, NULL },
47 { BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES ), NULL, 0, NULL },
48 { BER_BVNULL, NULL, 0, NULL }
51 AttributeName *slap_anlist_no_attrs = anlist_no_attrs;
52 AttributeName *slap_anlist_all_user_attributes = anlist_all_user_attributes;
53 AttributeName *slap_anlist_all_operational_attributes = anlist_all_operational_attributes;
54 AttributeName *slap_anlist_all_attributes = anlist_all_attributes;
56 typedef struct Attr_option {
57 struct berval name; /* option name or prefix */
58 int prefix; /* NAME is a tag and range prefix */
61 static Attr_option lang_option = { { sizeof("lang-")-1, "lang-" }, 1 };
63 /* Options sorted by name, and number of options */
64 static Attr_option *options = &lang_option;
65 static int option_count = 1;
67 static Attr_option *ad_find_option_definition( const char *opt, int optlen );
69 static int ad_keystring(
74 if( !AD_LEADCHAR( bv->bv_val[0] ) ) {
78 for( i=1; i<bv->bv_len; i++ ) {
79 if( !AD_CHAR( bv->bv_val[i] ) ) {
86 void ad_destroy( AttributeDescription *ad )
88 AttributeDescription *n;
90 for (; ad != NULL; ad = n) {
96 /* Is there an AttributeDescription for this type that uses these tags? */
97 AttributeDescription * ad_find_tags(
101 AttributeDescription *ad;
103 ldap_pvt_thread_mutex_lock( &type->sat_ad_mutex );
104 for (ad = type->sat_ad; ad; ad=ad->ad_next)
106 if (ad->ad_tags.bv_len == tags->bv_len &&
107 !strcasecmp(ad->ad_tags.bv_val, tags->bv_val))
110 ldap_pvt_thread_mutex_unlock( &type->sat_ad_mutex );
116 AttributeDescription **ad,
120 bv.bv_val = (char *) str;
121 bv.bv_len = strlen( str );
123 return slap_bv2ad( &bv, ad, text );
126 static char *strchrlen(
133 for( i=0; p[i]; i++ ) {
136 return (char *) &p[i];
146 AttributeDescription **ad,
149 int rtn = LDAP_UNDEFINED_TYPE;
150 AttributeDescription desc, *d2;
151 char *name, *options;
156 /* hardcoded limits for speed */
157 #define MAX_TAGGING_OPTIONS 128
158 struct berval tags[MAX_TAGGING_OPTIONS+1];
159 #define MAX_TAGS_LEN 1024
160 char tagbuf[MAX_TAGS_LEN];
162 assert( ad != NULL );
163 assert( *ad == NULL ); /* temporary */
165 if( bv == NULL || BER_BVISNULL( bv ) || BER_BVISEMPTY( bv ) ) {
166 *text = "empty AttributeDescription";
170 /* make sure description is IA5 */
171 if( ad_keystring( bv ) ) {
172 *text = "AttributeDescription contains inappropriate characters";
176 /* find valid base attribute type; parse in place */
177 memset( &desc, 0, sizeof( desc ) );
180 options = ber_bvchr( bv, ';' );
181 if ( options != NULL && (unsigned) ( options - name ) < bv->bv_len ) {
182 /* don't go past the end of the berval! */
183 desc.ad_cname.bv_len = options - name;
187 desc.ad_type = at_bvfind( &desc.ad_cname );
188 if( desc.ad_type == NULL ) {
189 *text = "attribute type undefined";
193 if( is_at_operational( desc.ad_type ) && options != NULL ) {
194 *text = "operational attribute with options undefined";
199 * parse options in place
202 memset( tags, 0, sizeof( tags ));
205 for( opt=options; opt != NULL; opt=next ) {
208 next = strchrlen( opt, ';', &optlen );
211 *text = "zero length option is invalid";
214 } else if ( optlen == sizeof("binary")-1 &&
215 strncasecmp( opt, "binary", sizeof("binary")-1 ) == 0 )
218 if( slap_ad_is_binary( &desc ) ) {
219 *text = "option \"binary\" specified multiple times";
223 if( !slap_syntax_is_binary( desc.ad_type->sat_syntax )) {
224 /* not stored in binary, disallow option */
225 *text = "option \"binary\" not supported with type";
229 desc.ad_flags |= SLAP_DESC_BINARY;
232 } else if ( ad_find_option_definition( opt, optlen ) ) {
235 if( opt[optlen-1] == '-' ) {
236 desc.ad_flags |= SLAP_DESC_TAG_RANGE;
239 if( ntags >= MAX_TAGGING_OPTIONS ) {
240 *text = "too many tagging options";
245 * tags should be presented in sorted order,
246 * so run the array in reverse.
248 for( i=ntags-1; i>=0; i-- ) {
251 rc = strncasecmp( opt, tags[i].bv_val,
252 (unsigned) optlen < tags[i].bv_len
253 ? (unsigned) optlen : tags[i].bv_len );
255 if( rc == 0 && (unsigned)optlen == tags[i].bv_len ) {
256 /* duplicate (ignore) */
259 } else if ( rc > 0 ||
260 ( rc == 0 && (unsigned)optlen > tags[i].bv_len ))
262 AC_MEMCPY( &tags[i+2], &tags[i+1],
263 (ntags-i-1)*sizeof(struct berval) );
264 tags[i+1].bv_val = opt;
265 tags[i+1].bv_len = optlen;
271 AC_MEMCPY( &tags[1], &tags[0],
272 ntags*sizeof(struct berval) );
274 tags[0].bv_val = opt;
275 tags[0].bv_len = optlen;
278 tagslen += optlen + 1;
282 *text = "unrecognized option";
290 if( tagslen > MAX_TAGS_LEN ) {
291 *text = "tagging options too long";
295 desc.ad_tags.bv_val = tagbuf;
298 for( i=0; i<ntags; i++ ) {
299 AC_MEMCPY( &desc.ad_tags.bv_val[tagslen],
300 tags[i].bv_val, tags[i].bv_len );
302 tagslen += tags[i].bv_len;
303 desc.ad_tags.bv_val[tagslen++] = ';';
306 desc.ad_tags.bv_val[--tagslen] = '\0';
307 desc.ad_tags.bv_len = tagslen;
310 /* see if a matching description is already cached */
311 for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
312 if( d2->ad_flags != desc.ad_flags ) {
315 if( d2->ad_tags.bv_len != desc.ad_tags.bv_len ) {
318 if( d2->ad_tags.bv_len == 0 ) {
321 if( strncasecmp( d2->ad_tags.bv_val, desc.ad_tags.bv_val,
322 desc.ad_tags.bv_len ) == 0 )
328 /* Not found, add new one */
331 ldap_pvt_thread_mutex_lock( &desc.ad_type->sat_ad_mutex );
332 /* check again now that we've locked */
333 for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
334 if (d2->ad_flags != desc.ad_flags)
336 if (d2->ad_tags.bv_len != desc.ad_tags.bv_len)
338 if (d2->ad_tags.bv_len == 0)
340 if (strncasecmp(d2->ad_tags.bv_val, desc.ad_tags.bv_val,
341 desc.ad_tags.bv_len) == 0)
345 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
349 /* Allocate a single contiguous block. If there are no
350 * options, we just need space for the AttrDesc structure.
351 * Otherwise, we need to tack on the full name length +
352 * options length, + maybe tagging options length again.
354 if (desc.ad_tags.bv_len || desc.ad_flags != SLAP_DESC_NONE) {
355 dlen = desc.ad_type->sat_cname.bv_len + 1;
356 if (desc.ad_tags.bv_len) {
357 dlen += 1+desc.ad_tags.bv_len;
359 if( slap_ad_is_binary( &desc ) ) {
360 dlen += sizeof(";binary")+desc.ad_tags.bv_len;
364 d2 = ch_malloc(sizeof(AttributeDescription) + dlen);
366 d2->ad_type = desc.ad_type;
367 d2->ad_flags = desc.ad_flags;
368 d2->ad_cname.bv_len = desc.ad_type->sat_cname.bv_len;
369 d2->ad_tags.bv_len = desc.ad_tags.bv_len;
372 d2->ad_cname.bv_val = d2->ad_type->sat_cname.bv_val;
373 d2->ad_tags.bv_val = NULL;
377 d2->ad_cname.bv_val = (char *)(d2+1);
378 strcpy(d2->ad_cname.bv_val, d2->ad_type->sat_cname.bv_val);
379 cp = d2->ad_cname.bv_val + d2->ad_cname.bv_len;
380 if( slap_ad_is_binary( &desc ) ) {
383 if( desc.ad_tags.bv_len ) {
384 lp = desc.ad_tags.bv_val;
385 while( strncasecmp(lp, "binary", sizeof("binary")-1) < 0
386 && (lp = strchr( lp, ';' )) != NULL )
388 if( lp != desc.ad_tags.bv_val ) {
391 ? (unsigned) (lp - desc.ad_tags.bv_val - 1)
392 : strlen( desc.ad_tags.bv_val ));
393 cp = lutil_strncopy(cp, desc.ad_tags.bv_val, j);
396 cp = lutil_strcopy(cp, ";binary");
399 cp = lutil_strcopy(cp, lp);
401 d2->ad_cname.bv_len = cp - d2->ad_cname.bv_val;
402 if( desc.ad_tags.bv_len )
403 ldap_pvt_str2lower(op);
408 if( desc.ad_tags.bv_len ) {
409 lp = d2->ad_cname.bv_val + d2->ad_cname.bv_len + j;
412 d2->ad_tags.bv_val = lp;
413 strcpy(lp, desc.ad_tags.bv_val);
414 ldap_pvt_str2lower(lp);
416 d2->ad_cname.bv_len += 1 + desc.ad_tags.bv_len;
419 /* Add new desc to list. We always want the bare Desc with
420 * no options to stay at the head of the list, assuming
421 * that one will be used most frequently.
423 if (desc.ad_type->sat_ad == NULL || dlen == 0) {
424 d2->ad_next = desc.ad_type->sat_ad;
425 desc.ad_type->sat_ad = d2;
427 d2->ad_next = desc.ad_type->sat_ad->ad_next;
428 desc.ad_type->sat_ad->ad_next = d2;
430 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
442 static int is_ad_subtags(
443 struct berval *subtagsbv,
444 struct berval *suptagsbv )
446 const char *suptags, *supp, *supdelimp;
447 const char *subtags, *subp, *subdelimp;
450 subtags =subtagsbv->bv_val;
451 suptags =suptagsbv->bv_val;
453 for( supp=suptags ; supp; supp=supdelimp ) {
454 supdelimp = strchrlen( supp, ';', &suplen );
455 if( supdelimp ) supdelimp++;
457 for( subp=subtags ; subp; subp=subdelimp ) {
458 subdelimp = strchrlen( subp, ';', &sublen );
459 if( subdelimp ) subdelimp++;
462 ? ( suplen-1 == sublen && supp[suplen-1] == '-'
463 && strncmp( supp, subp, sublen ) == 0 )
464 : ( ( suplen == sublen || supp[suplen-1] == '-' )
465 && strncmp( supp, subp, suplen ) == 0 ) )
478 AttributeDescription *sub,
479 AttributeDescription *super
485 for ( a = sub->ad_type; a; a=a->sat_sup ) {
486 if ( a == super->ad_type ) break;
492 /* ensure sub does support all flags of super */
493 lr = sub->ad_tags.bv_len ? SLAP_DESC_TAG_RANGE : 0;
494 if(( super->ad_flags & ( sub->ad_flags | lr )) != super->ad_flags ) {
498 /* check for tagging options */
499 if ( super->ad_tags.bv_len == 0 )
501 if ( sub->ad_tags.bv_len == 0 )
504 return is_ad_subtags( &sub->ad_tags, &super->ad_tags );
508 AttributeDescription *desc,
509 AttributeName *attrs )
511 if (! attrs ) return 0;
513 for( ; attrs->an_name.bv_val; attrs++ ) {
517 if ( attrs->an_desc ) {
520 if ( desc == attrs->an_desc ) {
525 * EXTENSION: if requested description is preceeded by
526 * a '-' character, do not match on subtypes.
528 if ( attrs->an_name.bv_val[0] == '-' ) {
532 /* Is this a subtype of the requested attr? */
533 for (a = desc->ad_type; a; a=a->sat_sup) {
534 if ( a == attrs->an_desc->ad_type )
540 /* Does desc support all the requested flags? */
541 lr = desc->ad_tags.bv_len ? SLAP_DESC_TAG_RANGE : 0;
542 if(( attrs->an_desc->ad_flags & (desc->ad_flags | lr))
543 != attrs->an_desc->ad_flags ) {
546 /* Do the descs have compatible tags? */
547 if ( attrs->an_desc->ad_tags.bv_len == 0 ) {
550 if ( desc->ad_tags.bv_len == 0) {
553 if ( is_ad_subtags( &desc->ad_tags,
554 &attrs->an_desc->ad_tags ) ) {
560 if ( ber_bvccmp( &attrs->an_name, '*' ) ) {
561 if ( !is_at_operational( desc->ad_type ) ) {
567 if ( ber_bvccmp( &attrs->an_name, '+' ) ) {
568 if ( is_at_operational( desc->ad_type ) ) {
575 * EXTENSION: see if requested description is @objectClass
576 * if so, return attributes which the class requires/allows
577 * else if requested description is !objectClass, return
578 * attributes which the class does not require/allow
581 if( oc == NULL && attrs->an_name.bv_val ) {
582 switch( attrs->an_name.bv_val[0] ) {
583 case '@': /* @objectClass */
584 case '+': /* +objectClass (deprecated) */
585 case '!': { /* exclude */
586 struct berval ocname;
587 ocname.bv_len = attrs->an_name.bv_len - 1;
588 ocname.bv_val = &attrs->an_name.bv_val[1];
589 oc = oc_bvfind( &ocname );
590 attrs->an_oc_exclude = 0;
591 if ( oc && attrs->an_name.bv_val[0] == '!' ) {
592 attrs->an_oc_exclude = 1;
596 default: /* old (deprecated) way */
597 oc = oc_bvfind( &attrs->an_name );
602 if ( attrs->an_oc_exclude ) {
603 if ( oc == slap_schema.si_oc_extensibleObject ) {
604 /* extensibleObject allows the return of anything */
608 if( oc->soc_required ) {
609 /* allow return of required attributes */
612 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
613 for (a = desc->ad_type; a; a=a->sat_sup) {
614 if ( a == oc->soc_required[i] ) {
621 if( oc->soc_allowed ) {
622 /* allow return of allowed attributes */
624 for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
625 for (a = desc->ad_type; a; a=a->sat_sup) {
626 if ( a == oc->soc_allowed[i] ) {
636 if ( oc == slap_schema.si_oc_extensibleObject ) {
637 /* extensibleObject allows the return of anything */
641 if( oc->soc_required ) {
642 /* allow return of required attributes */
645 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
646 for (a = desc->ad_type; a; a=a->sat_sup) {
647 if ( a == oc->soc_required[i] ) {
654 if( oc->soc_allowed ) {
655 /* allow return of allowed attributes */
657 for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
658 for (a = desc->ad_type; a; a=a->sat_sup) {
659 if ( a == oc->soc_allowed[i] ) {
669 /* give it a chance of being retrieved by a proxy... */
670 (void)slap_bv2undef_ad( &attrs->an_name,
671 &attrs->an_desc, &text,
672 SLAP_AD_PROXIED|SLAP_AD_NOINSERT );
680 int slap_str2undef_ad(
682 AttributeDescription **ad,
687 bv.bv_val = (char *) str;
688 bv.bv_len = strlen( str );
690 return slap_bv2undef_ad( &bv, ad, text, flags );
693 int slap_bv2undef_ad(
695 AttributeDescription **ad,
699 AttributeDescription *desc;
702 assert( ad != NULL );
704 if( bv == NULL || bv->bv_len == 0 ) {
705 *text = "empty AttributeDescription";
706 return LDAP_UNDEFINED_TYPE;
709 /* make sure description is IA5 */
710 if( ad_keystring( bv ) ) {
711 *text = "AttributeDescription contains inappropriate characters";
712 return LDAP_UNDEFINED_TYPE;
715 /* use the appropriate type */
716 if ( flags & SLAP_AD_PROXIED ) {
717 at = slap_schema.si_at_proxied;
720 at = slap_schema.si_at_undefined;
723 for( desc = at->sat_ad; desc; desc=desc->ad_next ) {
724 if( desc->ad_cname.bv_len == bv->bv_len &&
725 !strcasecmp( desc->ad_cname.bv_val, bv->bv_val ) )
732 if ( flags & SLAP_AD_NOINSERT ) {
734 return LDAP_UNDEFINED_TYPE;
737 desc = ch_malloc(sizeof(AttributeDescription) + 1 +
740 desc->ad_flags = SLAP_DESC_NONE;
741 BER_BVZERO( &desc->ad_tags );
743 desc->ad_cname.bv_len = bv->bv_len;
744 desc->ad_cname.bv_val = (char *)(desc+1);
745 strcpy(desc->ad_cname.bv_val, bv->bv_val);
747 /* canonical to upper case */
748 ldap_pvt_str2upper( desc->ad_cname.bv_val );
750 /* shouldn't we protect this for concurrency? */
752 ldap_pvt_thread_mutex_lock( &ad_undef_mutex );
753 desc->ad_next = desc->ad_type->sat_ad;
754 desc->ad_type->sat_ad = desc;
755 ldap_pvt_thread_mutex_unlock( &ad_undef_mutex );
757 Debug( LDAP_DEBUG_ANY,
758 "%s attributeDescription \"%s\" inserted.\n",
759 ( flags & SLAP_AD_PROXIED ) ? "PROXIED" : "UNKNOWN",
760 desc->ad_cname.bv_val, 0 );
778 AttributeDescription **u_ad, **n_ad;
780 /* Get to last ad on the new type */
781 for ( n_ad = &nat->sat_ad; *n_ad; n_ad = &(*n_ad)->ad_next ) ;
783 for ( u_ad = &at->sat_ad; *u_ad; ) {
786 ber_str2bv( name, 0, 0, &bv );
788 /* remove iff undef == name or undef == name;tag */
789 if ( (*u_ad)->ad_cname.bv_len >= bv.bv_len
790 && strncasecmp( (*u_ad)->ad_cname.bv_val, bv.bv_val, bv.bv_len ) == 0
791 && ( (*u_ad)->ad_cname.bv_val[ bv.bv_len ] == '\0'
792 || (*u_ad)->ad_cname.bv_val[ bv.bv_len ] == ';' ) )
794 AttributeDescription *tmp = *u_ad;
796 *u_ad = (*u_ad)->ad_next;
800 n_ad = &tmp->ad_next;
802 u_ad = &(*u_ad)->ad_next;
810 slap_ad_undef_promote(
816 ldap_pvt_thread_mutex_lock( &ad_undef_mutex );
818 rc = undef_promote( slap_schema.si_at_undefined, name, at );
820 rc = undef_promote( slap_schema.si_at_proxied, name, at );
823 ldap_pvt_thread_mutex_unlock( &ad_undef_mutex );
834 if( a == NULL ) return 0;
836 for ( ; a->an_name.bv_val; a++ ) {
837 if ( a->an_name.bv_len != s->bv_len) continue;
838 if ( strcasecmp( s->bv_val, a->an_name.bv_val ) == 0 ) {
847 * Convert a delimited string into a list of AttributeNames; add
848 * on to an existing list if it was given. If the string is not
849 * a valid attribute name, if a '-' is prepended it is skipped
850 * and the remaining name is tried again; if a '@' (or '+') is
851 * prepended, an objectclass name is searched instead; if a '!'
852 * is prepended, the objectclass name is negated.
854 * NOTE: currently, if a valid attribute name is not found, the
855 * same string is also checked as valid objectclass name; however,
856 * this behavior is deprecated.
859 str2anlist( AttributeName *an, char *in, const char *brkstr )
868 /* find last element in list */
871 for ( i = 0; !BER_BVISNULL( &an[ i ].an_name ) ; i++)
875 /* protect the input string from strtok */
876 str = ch_strdup( in );
878 /* Count words in string */
880 for ( s = str; *s; s++ ) {
881 if ( strchr( brkstr, *s ) != NULL ) {
886 an = ch_realloc( an, ( i + j + 1 ) * sizeof( AttributeName ) );
887 BER_BVZERO( &an[i + j].an_name );
889 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
891 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
893 anew->an_desc = NULL;
895 anew->an_oc_exclude = 0;
896 ber_str2bv(s, 0, 1, &anew->an_name);
897 slap_bv2ad(&anew->an_name, &anew->an_desc, &text);
898 if ( !anew->an_desc ) {
899 switch( anew->an_name.bv_val[0] ) {
901 struct berval adname;
902 adname.bv_len = anew->an_name.bv_len - 1;
903 adname.bv_val = &anew->an_name.bv_val[1];
904 slap_bv2ad(&adname, &anew->an_desc, &text);
905 if ( !anew->an_desc ) {
911 case '+': /* (deprecated) */
913 struct berval ocname;
914 ocname.bv_len = anew->an_name.bv_len - 1;
915 ocname.bv_val = &anew->an_name.bv_val[1];
916 anew->an_oc = oc_bvfind( &ocname );
917 if ( !anew->an_oc ) {
921 if ( anew->an_name.bv_val[0] == '!' ) {
922 anew->an_oc_exclude = 1;
927 /* old (deprecated) way */
928 anew->an_oc = oc_bvfind( &anew->an_name );
929 if ( !anew->an_oc ) {
937 BER_BVZERO( &anew->an_name );
942 for ( i = 0; an[i].an_name.bv_val; i++ ) {
943 free( an[i].an_name.bv_val );
947 * overwrites input string
955 char **anlist2charray_x( AttributeName *an, int dup, void *ctx )
961 for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ )
963 attrs = (char **) slap_sl_malloc( (i + 1) * sizeof(char *), ctx );
964 for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
966 attrs[i] = ch_strdup( an[i].an_name.bv_val );
968 attrs[i] = an[i].an_name.bv_val;
978 char **anlist2charray( AttributeName *an, int dup )
980 return anlist2charray_x( an, dup, NULL );
984 anlist2attrs( AttributeName * anlist )
991 if ( anlist == NULL )
994 for ( i = 0; anlist[i].an_name.bv_val; i++ ) {
995 if ( ( oc = anlist[i].an_oc ) ) {
996 for ( j = 0; oc->soc_required && oc->soc_required[j]; j++ ) ;
998 for ( j = 0; oc->soc_allowed && oc->soc_allowed[j]; j++ ) ;
1006 attrs = anlist2charray( anlist, 1 );
1011 attrs = (char **) ch_realloc( attrs, (i + k + 1) * sizeof( char * ));
1013 for ( i = 0; anlist[i].an_name.bv_val; i++ ) {
1014 if ( ( oc = anlist[i].an_oc ) ) {
1015 for ( j = 0; oc->soc_required && oc->soc_required[j]; j++ ) {
1016 attrs[n++] = ch_strdup(
1017 oc->soc_required[j]->sat_cname.bv_val );
1019 for ( j = 0; oc->soc_allowed && oc->soc_allowed[j]; j++ ) {
1020 attrs[n++] = ch_strdup(
1021 oc->soc_allowed[j]->sat_cname.bv_val );
1030 while ( attrs && attrs[i] ) {
1031 if ( *attrs[i] == '@' ) {
1032 ch_free( attrs[i] );
1033 for ( j = i; attrs[j]; j++ ) {
1034 attrs[j] = attrs[j+1];
1041 for ( i = 0; attrs && attrs[i]; i++ ) {
1043 while ( attrs && attrs[j] ) {
1044 if ( !strcmp( attrs[i], attrs[j] )) {
1045 ch_free( attrs[j] );
1046 for ( k = j; attrs && attrs[k]; k++ ) {
1047 attrs[k] = attrs[k+1];
1056 attrs = (char **) ch_realloc( attrs, (i+1) * sizeof( char * ));
1063 file2anlist( AttributeName *an, const char *fname, const char *brkstr )
1069 size_t lmax = LBUFSIZ;
1071 fp = fopen( fname, "r" );
1073 Debug( LDAP_DEBUG_ANY,
1074 "get_attrs_from_file: failed to open attribute list file "
1075 "\"%s\": %s\n", fname, strerror(errno), 0 );
1079 lcur = line = (char *) ch_malloc( lmax );
1081 Debug( LDAP_DEBUG_ANY,
1082 "get_attrs_from_file: could not allocate memory\n",
1088 while ( fgets( lcur, LBUFSIZ, fp ) != NULL ) {
1089 if ( ( c = strchr( lcur, '\n' ) ) ) {
1092 } else if ( *(c-1) == '\r' ) {
1099 line = (char *) ch_realloc( line, lmax );
1101 Debug( LDAP_DEBUG_ANY,
1102 "get_attrs_from_file: could not allocate memory\n",
1107 lcur = line + strlen( line );
1110 an = str2anlist( an, line, brkstr );
1121 /* Define an attribute option. */
1123 ad_define_option( const char *name, const char *fname, int lineno )
1126 unsigned int optlen;
1128 if ( options == &lang_option ) {
1137 if ( !DESC_CHAR( name[optlen] ) ) {
1138 Debug( LDAP_DEBUG_ANY,
1139 "%s: line %d: illegal option name \"%s\"\n",
1140 fname, lineno, name );
1143 } while ( name[++optlen] );
1145 options = ch_realloc( options,
1146 (option_count+1) * sizeof(Attr_option) );
1148 if ( strcasecmp( name, "binary" ) == 0
1149 || ad_find_option_definition( name, optlen ) ) {
1150 Debug( LDAP_DEBUG_ANY,
1151 "%s: line %d: option \"%s\" is already defined\n",
1152 fname, lineno, name );
1156 for ( i = option_count; i; --i ) {
1157 if ( strcasecmp( name, options[i-1].name.bv_val ) >= 0 )
1159 options[i] = options[i-1];
1162 options[i].name.bv_val = ch_strdup( name );
1163 options[i].name.bv_len = optlen;
1164 options[i].prefix = (name[optlen-1] == '-');
1166 if ( i != option_count &&
1167 options[i].prefix &&
1168 optlen < options[i+1].name.bv_len &&
1169 strncasecmp( name, options[i+1].name.bv_val, optlen ) == 0 ) {
1170 Debug( LDAP_DEBUG_ANY,
1171 "%s: line %d: option \"%s\" overrides previous option\n",
1172 fname, lineno, name );
1180 /* Find the definition of the option name or prefix matching the arguments */
1181 static Attr_option *
1182 ad_find_option_definition( const char *opt, int optlen )
1184 int top = 0, bot = option_count;
1185 while ( top < bot ) {
1186 int mid = (top + bot) / 2;
1187 int mlen = options[mid].name.bv_len;
1188 char *mname = options[mid].name.bv_val;
1190 if ( optlen < mlen ) {
1191 j = strncasecmp( opt, mname, optlen ) - 1;
1193 j = strncasecmp( opt, mname, mlen );
1194 if ( j==0 && (optlen==mlen || options[mid].prefix) )
1195 return &options[mid];
1205 MatchingRule *ad_mr(
1206 AttributeDescription *ad,
1209 switch( usage & SLAP_MR_TYPE_MASK ) {
1211 case SLAP_MR_EQUALITY:
1212 return ad->ad_type->sat_equality;
1214 case SLAP_MR_ORDERING:
1215 return ad->ad_type->sat_ordering;
1217 case SLAP_MR_SUBSTR:
1218 return ad->ad_type->sat_substr;
1222 assert( 0 /* ad_mr: bad usage */);