1 /* ad.c - routines for dealing with attribute descriptions */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2009 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 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( "+" );
34 static AttributeName anlist_no_attrs[] = {
35 { BER_BVC( LDAP_NO_ATTRS ), NULL, 0, NULL },
36 { BER_BVNULL, NULL, 0, NULL }
39 static AttributeName anlist_all_user_attributes[] = {
40 { BER_BVC( LDAP_ALL_USER_ATTRIBUTES ), NULL, 0, NULL },
41 { BER_BVNULL, NULL, 0, NULL }
44 static AttributeName anlist_all_operational_attributes[] = {
45 { BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES ), NULL, 0, NULL },
46 { BER_BVNULL, NULL, 0, NULL }
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 }
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;
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;
64 typedef struct Attr_option {
65 struct berval name; /* option name or prefix */
66 int prefix; /* NAME is a tag and range prefix */
69 static Attr_option lang_option = { BER_BVC("lang-"), 1 };
71 /* Options sorted by name, and number of options */
72 static Attr_option *options = &lang_option;
73 static int option_count = 1;
75 static int msad_range_hack = 0;
77 static Attr_option *ad_find_option_definition( const char *opt, int optlen );
79 static int ad_keystring(
84 if( !AD_LEADCHAR( bv->bv_val[0] ) ) {
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] == '=' )
98 void ad_destroy( AttributeDescription *ad )
100 AttributeDescription *n;
102 for (; ad != NULL; ad = n) {
108 /* Is there an AttributeDescription for this type that uses these tags? */
109 AttributeDescription * ad_find_tags(
111 struct berval *tags )
113 AttributeDescription *ad;
115 ldap_pvt_thread_mutex_lock( &type->sat_ad_mutex );
116 for (ad = type->sat_ad; ad; ad=ad->ad_next)
118 if (ad->ad_tags.bv_len == tags->bv_len &&
119 !strcasecmp(ad->ad_tags.bv_val, tags->bv_val))
122 ldap_pvt_thread_mutex_unlock( &type->sat_ad_mutex );
128 AttributeDescription **ad,
132 bv.bv_val = (char *) str;
133 bv.bv_len = strlen( str );
135 return slap_bv2ad( &bv, ad, text );
138 static char *strchrlen(
146 for( p=beg; *p && p < end; p++ ) {
159 AttributeDescription **ad,
162 int rtn = LDAP_UNDEFINED_TYPE;
163 AttributeDescription desc, *d2;
164 char *name, *options, *optn;
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];
175 assert( ad != NULL );
176 assert( *ad == NULL ); /* temporary */
178 if( bv == NULL || BER_BVISNULL( bv ) || BER_BVISEMPTY( bv ) ) {
179 *text = "empty AttributeDescription";
183 /* make sure description is IA5 */
184 if( ad_keystring( bv ) ) {
185 *text = "AttributeDescription contains inappropriate characters";
189 /* find valid base attribute type; parse in place */
192 BER_BVZERO( &desc.ad_tags );
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;
201 desc.ad_type = at_bvfind( &desc.ad_cname );
202 if( desc.ad_type == NULL ) {
203 *text = "attribute type undefined";
207 if( is_at_operational( desc.ad_type ) && options != NULL ) {
208 *text = "operational attribute with options undefined";
213 * parse options in place
217 optn = bv->bv_val + bv->bv_len;
219 for( opt=options; opt != NULL; opt=next ) {
222 next = strchrlen( opt, optn, ';', &optlen );
225 *text = "zero length option is invalid";
228 } else if ( optlen == STRLENOF("binary") &&
229 strncasecmp( opt, "binary", STRLENOF("binary") ) == 0 )
232 if( slap_ad_is_binary( &desc ) ) {
233 *text = "option \"binary\" specified multiple times";
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";
243 desc.ad_flags |= SLAP_DESC_BINARY;
246 } else if ( ad_find_option_definition( opt, optlen ) ) {
249 if( opt[optlen-1] == '-' ||
250 ( opt[optlen-1] == '=' && msad_range_hack )) {
251 desc.ad_flags |= SLAP_DESC_TAG_RANGE;
254 if( ntags >= MAX_TAGGING_OPTIONS ) {
255 *text = "too many tagging options";
260 * tags should be presented in sorted order,
261 * so run the array in reverse.
263 for( i=ntags-1; i>=0; i-- ) {
266 rc = strncasecmp( opt, tags[i].bv_val,
267 (unsigned) optlen < tags[i].bv_len
268 ? (unsigned) optlen : tags[i].bv_len );
270 if( rc == 0 && (unsigned)optlen == tags[i].bv_len ) {
271 /* duplicate (ignore) */
274 } else if ( rc > 0 ||
275 ( rc == 0 && (unsigned)optlen > tags[i].bv_len ))
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;
286 AC_MEMCPY( &tags[1], &tags[0],
287 ntags*sizeof(struct berval) );
289 tags[0].bv_val = opt;
290 tags[0].bv_len = optlen;
293 tagslen += optlen + 1;
297 *text = "unrecognized option";
305 if( tagslen > MAX_TAGS_LEN ) {
306 *text = "tagging options too long";
310 desc.ad_tags.bv_val = tagbuf;
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 );
317 tagslen += tags[i].bv_len;
318 desc.ad_tags.bv_val[tagslen++] = ';';
321 desc.ad_tags.bv_val[--tagslen] = '\0';
322 desc.ad_tags.bv_len = tagslen;
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 ) {
330 if( d2->ad_tags.bv_len != desc.ad_tags.bv_len ) {
333 if( d2->ad_tags.bv_len == 0 ) {
336 if( strncasecmp( d2->ad_tags.bv_val, desc.ad_tags.bv_val,
337 desc.ad_tags.bv_len ) == 0 )
343 /* Not found, add new one */
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)
351 if (d2->ad_tags.bv_len != desc.ad_tags.bv_len)
353 if (d2->ad_tags.bv_len == 0)
355 if (strncasecmp(d2->ad_tags.bv_val, desc.ad_tags.bv_val,
356 desc.ad_tags.bv_len) == 0)
360 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
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.
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;
374 if ( slap_ad_is_binary( &desc ) ) {
375 dlen += 1 + STRLENOF(";binary") + desc.ad_tags.bv_len;
379 d2 = ch_malloc(sizeof(AttributeDescription) + dlen);
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;
387 d2->ad_cname.bv_val = d2->ad_type->sat_cname.bv_val;
388 d2->ad_tags.bv_val = NULL;
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 ) ) {
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 )
403 if( lp != desc.ad_tags.bv_val ) {
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);
411 cp = lutil_strcopy(cp, ";binary");
414 cp = lutil_strcopy(cp, lp);
416 d2->ad_cname.bv_len = cp - d2->ad_cname.bv_val;
417 if( desc.ad_tags.bv_len )
418 ldap_pvt_str2lower(op);
423 if( desc.ad_tags.bv_len ) {
424 lp = d2->ad_cname.bv_val + d2->ad_cname.bv_len + j;
427 d2->ad_tags.bv_val = lp;
428 strcpy(lp, desc.ad_tags.bv_val);
429 ldap_pvt_str2lower(lp);
431 d2->ad_cname.bv_len += 1 + desc.ad_tags.bv_len;
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.
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;
442 d2->ad_next = desc.ad_type->sat_ad->ad_next;
443 desc.ad_type->sat_ad->ad_next = d2;
445 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
457 static int is_ad_subtags(
458 struct berval *subtagsbv,
459 struct berval *suptagsbv )
461 const char *suptags, *supp, *supdelimp, *supn;
462 const char *subtags, *subp, *subdelimp, *subn;
465 subtags =subtagsbv->bv_val;
466 suptags =suptagsbv->bv_val;
467 subn = subtags + subtagsbv->bv_len;
468 supn = suptags + suptagsbv->bv_len;
470 for( supp=suptags ; supp; supp=supdelimp ) {
471 supdelimp = strchrlen( supp, supn, ';', &suplen );
472 if( supdelimp ) supdelimp++;
474 for( subp=subtags ; subp; subp=subdelimp ) {
475 subdelimp = strchrlen( subp, subn, ';', &sublen );
476 if( subdelimp ) subdelimp++;
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 ) )
495 AttributeDescription *sub,
496 AttributeDescription *super
502 for ( a = sub->ad_type; a; a=a->sat_sup ) {
503 if ( a == super->ad_type ) break;
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 ) {
515 /* check for tagging options */
516 if ( super->ad_tags.bv_len == 0 )
518 if ( sub->ad_tags.bv_len == 0 )
521 return is_ad_subtags( &sub->ad_tags, &super->ad_tags );
525 AttributeDescription *desc,
526 AttributeName *attrs )
528 if (! attrs ) return 0;
530 for( ; attrs->an_name.bv_val; attrs++ ) {
534 if ( attrs->an_desc ) {
537 if ( desc == attrs->an_desc ) {
542 * EXTENSION: if requested description is preceeded by
543 * a '-' character, do not match on subtypes.
545 if ( attrs->an_name.bv_val[0] == '-' ) {
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 )
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 ) {
563 /* Do the descs have compatible tags? */
564 if ( attrs->an_desc->ad_tags.bv_len == 0 ) {
567 if ( desc->ad_tags.bv_len == 0) {
570 if ( is_ad_subtags( &desc->ad_tags,
571 &attrs->an_desc->ad_tags ) ) {
577 if ( ber_bvccmp( &attrs->an_name, '*' ) ) {
578 if ( !is_at_operational( desc->ad_type ) ) {
584 if ( ber_bvccmp( &attrs->an_name, '+' ) ) {
585 if ( is_at_operational( desc->ad_type ) ) {
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
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;
610 attrs->an_flags &= ~SLAP_AN_OCEXCLUDE;
614 default: /* old (deprecated) way */
615 oc = oc_bvfind( &attrs->an_name );
619 attrs->an_flags |= SLAP_AN_OCINITED;
623 if ( attrs->an_flags & SLAP_AN_OCEXCLUDE ) {
624 if ( oc == slap_schema.si_oc_extensibleObject ) {
625 /* extensibleObject allows the return of anything */
629 if( oc->soc_required ) {
630 /* allow return of required attributes */
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] ) {
642 if( oc->soc_allowed ) {
643 /* allow return of allowed attributes */
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] ) {
657 if ( oc == slap_schema.si_oc_extensibleObject ) {
658 /* extensibleObject allows the return of anything */
662 if( oc->soc_required ) {
663 /* allow return of required attributes */
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] ) {
675 if( oc->soc_allowed ) {
676 /* allow return of allowed attributes */
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] ) {
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 );
701 int slap_str2undef_ad(
703 AttributeDescription **ad,
708 bv.bv_val = (char *) str;
709 bv.bv_len = strlen( str );
711 return slap_bv2undef_ad( &bv, ad, text, flags );
714 int slap_bv2undef_ad(
716 AttributeDescription **ad,
720 AttributeDescription *desc;
723 assert( ad != NULL );
725 if( bv == NULL || bv->bv_len == 0 ) {
726 *text = "empty AttributeDescription";
727 return LDAP_UNDEFINED_TYPE;
730 /* make sure description is IA5 */
731 if( ad_keystring( bv ) ) {
732 *text = "AttributeDescription contains inappropriate characters";
733 return LDAP_UNDEFINED_TYPE;
736 /* use the appropriate type */
737 if ( flags & SLAP_AD_PROXIED ) {
738 at = slap_schema.si_at_proxied;
741 at = slap_schema.si_at_undefined;
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 ) )
753 if ( flags & SLAP_AD_NOINSERT ) {
755 return LDAP_UNDEFINED_TYPE;
758 desc = ch_malloc(sizeof(AttributeDescription) + 1 +
761 desc->ad_flags = SLAP_DESC_NONE;
762 BER_BVZERO( &desc->ad_tags );
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);
768 /* canonical to upper case */
769 ldap_pvt_str2upper( desc->ad_cname.bv_val );
771 /* shouldn't we protect this for concurrency? */
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 );
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 );
793 AttributeDescription *
798 AttributeDescription *ad =
799 slap_sl_mfuncs.bmf_malloc( sizeof(AttributeDescription) +
800 bv->bv_len + 1, memctx );
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;
817 AttributeDescription **u_ad, **n_ad;
819 /* Get to last ad on the new type */
820 for ( n_ad = &nat->sat_ad; *n_ad; n_ad = &(*n_ad)->ad_next ) ;
822 for ( u_ad = &at->sat_ad; *u_ad; ) {
825 ber_str2bv( name, 0, 0, &bv );
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 ] == ';' ) )
833 AttributeDescription *tmp = *u_ad;
835 *u_ad = (*u_ad)->ad_next;
839 /* ad_cname was contiguous, no leak here */
840 tmp->ad_cname = nat->sat_cname;
842 n_ad = &tmp->ad_next;
844 u_ad = &(*u_ad)->ad_next;
852 slap_ad_undef_promote(
858 ldap_pvt_thread_mutex_lock( &ad_undef_mutex );
860 rc = undef_promote( slap_schema.si_at_undefined, name, at );
862 rc = undef_promote( slap_schema.si_at_proxied, name, at );
865 ldap_pvt_thread_mutex_unlock( &ad_undef_mutex );
876 if( a == NULL ) return 0;
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 ) {
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.
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.
901 str2anlist( AttributeName *an, char *in, const char *brkstr )
910 /* find last element in list */
913 for ( i = 0; !BER_BVISNULL( &an[ i ].an_name ) ; i++)
917 /* protect the input string from strtok */
918 str = ch_strdup( in );
920 /* Count words in string */
922 for ( s = str; *s; s++ ) {
923 if ( strchr( brkstr, *s ) != NULL ) {
928 an = ch_realloc( an, ( i + j + 1 ) * sizeof( AttributeName ) );
930 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
932 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
934 /* put a stop mark */
935 BER_BVZERO( &anew[1].an_name );
937 anew->an_desc = NULL;
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] ) {
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 ) {
955 case '+': /* (deprecated) */
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 ) {
965 if ( anew->an_name.bv_val[0] == '!' ) {
966 anew->an_flags |= SLAP_AN_OCEXCLUDE;
971 /* old (deprecated) way */
972 anew->an_oc = oc_bvfind( &anew->an_name );
973 if ( !anew->an_oc ) {
978 anew->an_flags |= SLAP_AN_OCINITED;
982 BER_BVZERO( &anew->an_name );
987 anlist_free( an, 1, NULL );
990 * overwrites input string
999 anlist_free( AttributeName *an, int freename, void *ctx )
1008 for ( i = 0; an[i].an_name.bv_val; i++ ) {
1009 ber_memfree_x( an[i].an_name.bv_val, ctx );
1013 ber_memfree_x( an, ctx );
1016 char **anlist2charray_x( AttributeName *an, int dup, void *ctx )
1022 for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ )
1024 attrs = (char **) slap_sl_malloc( (i + 1) * sizeof(char *), ctx );
1025 for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
1027 attrs[i] = ch_strdup( an[i].an_name.bv_val );
1029 attrs[i] = an[i].an_name.bv_val;
1039 char **anlist2charray( AttributeName *an, int dup )
1041 return anlist2charray_x( an, dup, NULL );
1045 anlist2attrs( AttributeName * anlist )
1052 if ( anlist == NULL )
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++ ) ;
1059 for ( j = 0; oc->soc_allowed && oc->soc_allowed[j]; j++ ) ;
1067 attrs = anlist2charray( anlist, 1 );
1072 attrs = (char **) ch_realloc( attrs, (i + k + 1) * sizeof( char * ));
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 );
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 );
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];
1102 for ( i = 0; attrs && attrs[i]; i++ ) {
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];
1117 attrs = (char **) ch_realloc( attrs, (i+1) * sizeof( char * ));
1124 file2anlist( AttributeName *an, const char *fname, const char *brkstr )
1130 size_t lmax = LBUFSIZ;
1132 fp = fopen( fname, "r" );
1134 Debug( LDAP_DEBUG_ANY,
1135 "get_attrs_from_file: failed to open attribute list file "
1136 "\"%s\": %s\n", fname, strerror(errno), 0 );
1140 lcur = line = (char *) ch_malloc( lmax );
1142 Debug( LDAP_DEBUG_ANY,
1143 "get_attrs_from_file: could not allocate memory\n",
1149 while ( fgets( lcur, LBUFSIZ, fp ) != NULL ) {
1150 if ( ( c = strchr( lcur, '\n' ) ) ) {
1153 } else if ( *(c-1) == '\r' ) {
1160 line = (char *) ch_realloc( line, lmax );
1162 Debug( LDAP_DEBUG_ANY,
1163 "get_attrs_from_file: could not allocate memory\n",
1168 lcur = line + strlen( line );
1171 an = str2anlist( an, line, brkstr );
1182 /* Define an attribute option. */
1184 ad_define_option( const char *name, const char *fname, int lineno )
1187 unsigned int optlen;
1189 if ( options == &lang_option ) {
1198 if ( !DESC_CHAR( name[optlen] ) ) {
1199 /* allow trailing '=', same as '-' */
1200 if ( name[optlen] == '=' && !name[optlen+1] ) {
1201 msad_range_hack = 1;
1204 Debug( LDAP_DEBUG_ANY,
1205 "%s: line %d: illegal option name \"%s\"\n",
1206 fname, lineno, name );
1209 } while ( name[++optlen] );
1211 options = ch_realloc( options,
1212 (option_count+1) * sizeof(Attr_option) );
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 );
1222 for ( i = option_count; i; --i ) {
1223 if ( strcasecmp( name, options[i-1].name.bv_val ) >= 0 )
1225 options[i] = options[i-1];
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] == '=');
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 );
1248 ad_unparse_options( BerVarray *res )
1251 for ( i = 0; i < option_count; i++ ) {
1252 value_add_one( res, &options[i].name );
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 )
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;
1266 if ( optlen < mlen ) {
1267 j = strncasecmp( opt, mname, optlen ) - 1;
1269 j = strncasecmp( opt, mname, mlen );
1270 if ( j==0 && (optlen==mlen || options[mid].prefix) )
1271 return &options[mid];
1281 MatchingRule *ad_mr(
1282 AttributeDescription *ad,
1285 switch( usage & SLAP_MR_TYPE_MASK ) {
1287 case SLAP_MR_EQUALITY:
1288 return ad->ad_type->sat_equality;
1290 case SLAP_MR_ORDERING:
1291 return ad->ad_type->sat_ordering;
1293 case SLAP_MR_SUBSTR:
1294 return ad->ad_type->sat_substr;
1298 assert( 0 /* ad_mr: bad usage */);