]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/ad.c
need this outside for back monitor ...
[openldap] / servers / slapd / ad.c
index 29a16f2292047fc21237baaa97017714a0f131e4..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 */
@@ -18,8 +18,6 @@
 #include "ldap_pvt.h"
 #include "slap.h"
 
-extern ldap_pvt_thread_mutex_t ad_mutex;       /* init.c */
-
 static int ad_keystring(
        struct berval *bv )
 {
@@ -37,16 +35,34 @@ 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 );
        }
 }
 
+/* Is there an AttributeDescription for this type that uses this language? */
+AttributeDescription * ad_find_lang(
+       AttributeType *type,
+       struct berval *lang )
+{
+       AttributeDescription *ad;
+
+       ldap_pvt_thread_mutex_lock( &type->sat_ad_mutex );
+       for (ad = type->sat_ad; ad; ad=ad->ad_next)
+       {
+               if (ad->ad_lang.bv_len == lang->bv_len &&
+                       !strcasecmp(ad->ad_lang.bv_val, lang->bv_val))
+                       break;
+       }
+       ldap_pvt_thread_mutex_unlock( &type->sat_ad_mutex );
+       return ad;
+}
+
 int slap_str2ad(
        const char *str,
        AttributeDescription **ad,
@@ -84,22 +100,17 @@ int slap_bv2ad(
        }
 
        /* find valid base attribute type; parse in place */
+       desc.ad_cname = *bv;
        name = bv->bv_val;
        options = strchr(name, ';');
-       if (options != NULL) *options = '\0';
-       desc.ad_type = at_find( name );
-       if (options != NULL) *options = ';';
+       if (options != NULL)
+               desc.ad_cname.bv_len = options - name;
+       desc.ad_type = at_bvfind( &desc.ad_cname );
        if( desc.ad_type == NULL ) {
                *text = "attribute type undefined";
-
                return rtn;
        }
 
-       if (options != NULL)
-               desc.ad_cname.bv_len = options - name;
-       else
-               desc.ad_cname.bv_len = bv->bv_len;
-
        desc.ad_flags = SLAP_DESC_NONE;
        desc.ad_lang.bv_len = 0;
        desc.ad_lang.bv_val = NULL;
@@ -111,7 +122,7 @@ int slap_bv2ad(
                if ( options != NULL )
                        i = options - name;
                else
-                       i = strlen(name);
+                       i = bv->bv_len - (name - bv->bv_val);
 
                if( i == sizeof("binary")-1 && strncasecmp( name, "binary", i) == 0 ) {
                        if( slap_ad_is_binary( &desc ) ) {
@@ -159,12 +170,8 @@ int slap_bv2ad(
        /* Not found, add new one */
        while (d2 == NULL) {
                int dlen = 0;
-               /* uses a single mutex instead of one per attributetype.
-                * I don't believe this is a significant bottleneck. If
-                * necessary, could change to a per-AttrType rwlock.
-                */
-               ldap_pvt_thread_mutex_lock( &ad_mutex );
-               /* Check again just in case another thread added it */
+               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;
@@ -172,13 +179,12 @@ int slap_bv2ad(
                                continue;
                        if (d2->ad_lang.bv_len == 0)
                                break;
-                       if (strncasecmp(d2->ad_lang.bv_val,desc.ad_lang.bv_val,
+                       if (strncasecmp(d2->ad_lang.bv_val, desc.ad_lang.bv_val,
                                desc.ad_lang.bv_len) == 0)
                                break;
                }
-               /* Some other thread added it before we got the lock. */
-               if (d2 != NULL) {
-                       ldap_pvt_thread_mutex_unlock( &ad_mutex );
+               if (d2) {
+                       ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
                        break;
                }
 
@@ -191,7 +197,7 @@ int slap_bv2ad(
                if (desc.ad_lang.bv_len || desc.ad_flags != SLAP_DESC_NONE) {
                        if (desc.ad_lang.bv_len)
                                dlen = desc.ad_lang.bv_len+1;
-                       dlen += strlen(desc.ad_type->sat_cname)+1;
+                       dlen += desc.ad_type->sat_cname.bv_len+1;
                        if( slap_ad_is_binary( &desc ) ) {
                                dlen += sizeof("binary");
                        }
@@ -203,11 +209,11 @@ int slap_bv2ad(
                d2->ad_cname.bv_len = desc.ad_cname.bv_len;
                d2->ad_lang.bv_len = desc.ad_lang.bv_len;
                if (dlen == 0) {
-                       d2->ad_cname.bv_val = d2->ad_type->sat_cname;
+                       d2->ad_cname.bv_val = d2->ad_type->sat_cname.bv_val;
                        d2->ad_lang.bv_val = NULL;
                } else {
                        d2->ad_cname.bv_val = (char *)(d2+1);
-                       strcpy(d2->ad_cname.bv_val, d2->ad_type->sat_cname);
+                       strcpy(d2->ad_cname.bv_val, d2->ad_type->sat_cname.bv_val);
                        if( slap_ad_is_binary( &desc ) ) {
                                strcpy(d2->ad_cname.bv_val+d2->ad_cname.bv_len,
                                        ";binary");
@@ -235,7 +241,7 @@ 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( &ad_mutex );
+               ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
        }
 
        if( *ad == NULL ) {
@@ -276,21 +282,65 @@ int is_ad_subtype(
 
 int ad_inlist(
        AttributeDescription *desc,
-       char **attrs )
+       AttributeName *attrs )
 {
-       int i;
-       for( i=0; attrs[i] != NULL; i++ ) {
-               AttributeDescription *ad = NULL;
-               const char *text;
+       if (! attrs ) return 0;
+
+       for( ; attrs->an_name.bv_val; attrs++ ) {
+               ObjectClass *oc;
                int rc;
                
-               rc = slap_str2ad( attrs[i], &ad, &text );
+               if ( attrs->an_desc ) {
+                       if ( is_ad_subtype( desc, attrs->an_desc ))
+                               return 1;
+                       continue;
+               }
 
-               if( rc != LDAP_SUCCESS ) continue;
 
-               rc = is_ad_subtype( desc, ad );
+               /*
+                * EXTENSION: see if requested description is an object class
+                * if so, return attributes which the class requires/allows
+                */
+               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 */
+                               return 1;
+                       }
 
-               if( rc ) return 1;
+                       if( oc->soc_required ) {
+                               /* allow return of required attributes */
+                               int i;
+                               for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
+                                       rc = is_at_subtype( desc->ad_type,
+                                               oc->soc_allowed[i] );
+                                       if( rc ) return 1;
+                               }
+                       }
+                       if( oc->soc_allowed ) {
+                               /* allow return of allowed attributes */
+                               int i;
+                               for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
+                                       rc = is_at_subtype( desc->ad_type,
+                                               oc->soc_allowed[i] );
+                                       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;
+                       }
+               }
        }
 
        return 0;
@@ -362,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 );
+}