]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/ad.c
need this outside for back monitor ...
[openldap] / servers / slapd / ad.c
index 219b66b9b17820d73bb13ef84df09c0e19fd8e42..75193d93810576e95d09b92efd55522e1bdc9008 100644 (file)
@@ -1,6 +1,6 @@
 /* $OpenLDAP$ */
 /*
- * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  */
 /* ad.c - routines for dealing with attribute descriptions */
@@ -35,13 +35,13 @@ static int ad_keystring(
        return 0;
 }
 
-void ad_destroy( void *in )
+void ad_destroy( AttributeDescription *ad )
 {
-       AttributeDescription *ad = in, *n;
+       AttributeDescription *n;
 
-       for (;ad;ad = n) {
+       for (; ad != NULL; ad = n) {
                n = ad->ad_next;
-               ldap_memfree(ad);
+               ldap_memfree( ad );
        }
 }
 
@@ -154,7 +154,6 @@ int slap_bv2ad(
                }
        }
 
-       ldap_pvt_thread_mutex_lock( &desc.ad_type->sat_ad_mutex );
        /* see if a matching description is already cached */
        for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
                if (d2->ad_flags != desc.ad_flags)
@@ -171,6 +170,23 @@ int slap_bv2ad(
        /* Not found, add new one */
        while (d2 == NULL) {
                int dlen = 0;
+               ldap_pvt_thread_mutex_lock( &desc.ad_type->sat_ad_mutex );
+               /* check again now that we've locked */
+               for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
+                       if (d2->ad_flags != desc.ad_flags)
+                               continue;
+                       if (d2->ad_lang.bv_len != desc.ad_lang.bv_len)
+                               continue;
+                       if (d2->ad_lang.bv_len == 0)
+                               break;
+                       if (strncasecmp(d2->ad_lang.bv_val, desc.ad_lang.bv_val,
+                               desc.ad_lang.bv_len) == 0)
+                               break;
+               }
+               if (d2) {
+                       ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
+                       break;
+               }
 
                /* Allocate a single contiguous block. If there are no
                 * options, we just need space for the AttrDesc structure.
@@ -225,8 +241,8 @@ int slap_bv2ad(
                        d2->ad_next = desc.ad_type->sat_ad->ad_next;
                        desc.ad_type->sat_ad->ad_next = d2;
                }
+               ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
        }
-       ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
 
        if( *ad == NULL ) {
                *ad = d2;
@@ -266,27 +282,30 @@ int is_ad_subtype(
 
 int ad_inlist(
        AttributeDescription *desc,
-       struct berval **attrs )
+       AttributeName *attrs )
 {
-       int i;
-       for( i=0; attrs[i] != NULL; i++ ) {
+       if (! attrs ) return 0;
+
+       for( ; attrs->an_name.bv_val; attrs++ ) {
                ObjectClass *oc;
-               AttributeDescription *ad = NULL;
-               const char *text;
                int rc;
                
-               rc = slap_bv2ad( attrs[i], &ad, &text );
-               if( rc == LDAP_SUCCESS ) {
-                       rc = is_ad_subtype( desc, ad );
-                       if( rc ) return 1;
+               if ( attrs->an_desc ) {
+                       if ( is_ad_subtype( desc, attrs->an_desc ))
+                               return 1;
                        continue;
                }
 
+
                /*
                 * EXTENSION: see if requested description is an object class
                 * if so, return attributes which the class requires/allows
                 */
-               oc = oc_bvfind( attrs[i] );
+               oc = attrs->an_oc;
+               if( oc == NULL ) {
+                       oc = oc_bvfind( &attrs->an_name );
+                       attrs->an_oc = oc;
+               }
                if( oc != NULL ) {
                        if ( oc == slap_schema.si_oc_extensibleObject ) {
                                /* extensibleObject allows the return of anything */
@@ -311,6 +330,16 @@ int ad_inlist(
                                        if( rc ) return 1;
                                }
                        }
+               } else {
+                       /* short-circuit this search next time around */
+                       if (!slap_schema.si_at_undefined->sat_ad) {
+                               const char *text;
+                               slap_bv2undef_ad(&attrs->an_name,
+                                       &attrs->an_desc, &text);
+                       } else {
+                               attrs->an_desc =
+                                       slap_schema.si_at_undefined->sat_ad;
+                       }
                }
        }
 
@@ -383,3 +412,75 @@ int slap_bv2undef_ad(
 
        return LDAP_SUCCESS;
 }
+
+int
+an_find(
+    AttributeName *a,
+    struct berval *s
+)
+{
+       if( a == NULL ) return 0;
+
+       for ( ; a->an_name.bv_val; a++ ) {
+               if ( a->an_name.bv_len != s->bv_len) continue;
+               if ( strcasecmp( s->bv_val, a->an_name.bv_val ) == 0 ) {
+                       return( 1 );
+               }
+       }
+
+       return( 0 );
+}
+
+/* Convert a delimited string into a list of AttributeNames; Add on
+ * to an existing list if it was given.
+ */
+AttributeName *
+str2anlist( AttributeName *an, char *in, const char *brkstr )
+{
+       char    *str;
+       char    *s;
+       char    *lasts;
+       int     i, j;
+       const char *text;
+       AttributeName *anew;
+
+       /* find last element in list */
+       for (i = 0; an && an[i].an_name.bv_val; i++);
+       
+       /* protect the input string from strtok */
+       str = ch_strdup( in );
+
+       /* Count words in string */
+       j=1;
+       for ( s = str; *s; s++ ) {
+               if ( strchr( brkstr, *s ) != NULL ) {
+                       j++;
+               }
+       }
+
+       an = ch_realloc( an, ( i + j + 1 ) * sizeof( AttributeName ) );
+       anew = an + i;
+       for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
+               s != NULL;
+               s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
+       {
+               anew->an_desc = NULL;
+               anew->an_oc = NULL;
+               ber_str2bv(s, 0, 1, &anew->an_name);
+               slap_bv2ad(&anew->an_name, &anew->an_desc, &text);
+               if ( !anew->an_desc ) {
+                       anew->an_oc = oc_bvfind( &anew->an_name );
+                       if ( !anew->an_oc ) {
+                               free( an );
+                               /* overwrites input string on error! */
+                               strcpy( in, s );
+                               return NULL;
+                       }
+               }
+               anew++;
+       }
+       anew->an_name.bv_val = NULL;
+
+       free( str );
+       return( an );
+}