]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/ad.c
Move experimental Back-BDB2 to Attic
[openldap] / servers / slapd / ad.c
index db715584e3cd2cd900c0cb5c3a6babfdb882a18f..f73f0f7ce98077cf62b3c94331913959c0de7d1e 100644 (file)
@@ -1,6 +1,6 @@
 /* $OpenLDAP$ */
 /*
- * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  */
 /* ad.c - routines for dealing with attribute descriptions */
 #include "slap.h"
 
 #ifdef SLAPD_SCHEMA_NOT_COMPAT
+AttributeDescription *ad_dup(
+       AttributeDescription *desc )
+{
+       AttributeDescription *ad;
+
+       if( desc == NULL ) {
+               return NULL;
+       }
+
+       ad = (AttributeDescription *) ch_malloc( sizeof(AttributeDescription) );
+
+       *ad = *desc;
+
+       if( ad->ad_cname != NULL ) {
+               ad->ad_cname = ber_bvdup( ad->ad_cname );
+       }
+
+       if( ad->ad_lang != NULL ) {
+               ad->ad_lang = ch_strdup( ad->ad_lang );
+       }
+
+       return ad;
+}
+
+void
+ad_free( AttributeDescription *ad, int freeit )
+{
+       if( ad == NULL ) return;
+
+       if( ad->ad_cname != NULL ) {
+               ber_bvfree( ad->ad_cname );
+       }
+
+       free( ad->ad_lang );
+
+       if( freeit ) free( ad );
+}
+
+static int ad_keystring(
+       struct berval *bv )
+{
+       ber_len_t i;
+
+       if( !AD_CHAR( bv->bv_val[0] ) ) {
+               return 1;
+       }
+
+       for( i=1; i<bv->bv_len; i++ ) {
+               if( !AD_CHAR( bv->bv_val[i] ) ) {
+                       return 1;
+               }
+       }
+       return 0;
+}
+
+int slap_str2ad(
+       const char *str,
+       AttributeDescription **ad,
+       const char **text )
+{
+       struct berval bv;
+       bv.bv_val = (char *) str;
+       bv.bv_len = strlen( str );
+
+       return slap_bv2ad( &bv, ad, text );
+}
 
 int slap_bv2ad(
        struct berval *bv,
        AttributeDescription **ad,
-       char **text )
+       const char **text )
 {
        int rtn = LDAP_UNDEFINED_TYPE;
        int i;
        AttributeDescription desc;
        char **tokens;
 
-       assert( *ad != NULL );
-       assert( *text != NULL );
+       assert( ad != NULL );
+       assert( *ad == NULL ); /* temporary */
 
        if( bv == NULL || bv->bv_len == 0 ) {
                *text = "empty attribute description";
-               return LDAP_UNDEFINED_TYPE;
+               return rtn;
        }
 
        /* make sure description is IA5 */
-       if( IA5StringValidate( NULL, bv ) != 0 ) {
-               *text = "attribute description contains non-IA5 characters";
-               return LDAP_UNDEFINED_TYPE;
+       if( ad_keystring( bv ) ) {
+               *text = "attribute description contains inappropriate characters";
+               return rtn;
        }
 
        tokens = str2charray( bv->bv_val, ";");
@@ -63,14 +129,12 @@ int slap_bv2ad(
 
        for( i=1; tokens[i] != NULL; i++ ) {
                if( strcasecmp( tokens[i], "binary" ) == 0 ) {
-                       if( desc.ad_flags & SLAP_DESC_BINARY ) {
+                       if( slap_ad_is_binary( &desc ) ) {
                                *text = "option \"binary\" specified multiple times";
                                goto done;
                        }
 
-                       if(!( desc.ad_type->sat_syntax->ssyn_flags
-                               & SLAP_SYNTAX_BINARY ))
-                       {
+                       if( !slap_syntax_is_binary( desc.ad_type->sat_syntax )) {
                                /* not stored in binary, disallow option */
                                *text = "option \"binary\" with type not supported";
                                goto done;
@@ -85,6 +149,7 @@ int slap_bv2ad(
                                *text = "multiple language tag options specified";
                                goto done;
                        }
+
                        desc.ad_lang = tokens[i];
 
                        /* normalize to all lower case, it's easy */
@@ -99,18 +164,17 @@ int slap_bv2ad(
        desc.ad_cname = ch_malloc( sizeof( struct berval ) );
 
        desc.ad_cname->bv_len = strlen( desc.ad_type->sat_cname );
-       if( desc.ad_flags & SLAP_DESC_BINARY ) {
+       if( slap_ad_is_binary( &desc ) ) {
                desc.ad_cname->bv_len += sizeof("binary");
        }
        if( desc.ad_lang != NULL ) {
-               desc.ad_cname->bv_len += strlen( desc.ad_lang );
+               desc.ad_cname->bv_len += 1 + strlen( desc.ad_lang );
        }
 
-       desc.ad_cname = ch_malloc( desc.ad_cname->bv_len + 1 );
+       desc.ad_cname->bv_val = ch_malloc( desc.ad_cname->bv_len + 1 );
 
        strcpy( desc.ad_cname->bv_val, desc.ad_type->sat_cname );
-       strcat( desc.ad_cname->bv_val, ";binary" );
-       if( desc.ad_flags & SLAP_DESC_BINARY ) {
+       if( slap_ad_is_binary( &desc ) ) {
                strcat( desc.ad_cname->bv_val, ";binary" );
        }
 
@@ -119,7 +183,10 @@ int slap_bv2ad(
                strcat( desc.ad_cname->bv_val, desc.ad_lang );
        }
 
-       *ad = ch_malloc( sizeof( AttributeDescription ) );
+       if( *ad == NULL ) {
+               *ad = ch_malloc( sizeof( AttributeDescription ) );
+       }
+
        **ad = desc;
 
        rtn = LDAP_SUCCESS;
@@ -129,15 +196,51 @@ done:
        return rtn;
 }
 
-void
-ad_free( AttributeDescription *ad, int freeit )
+int is_ad_subtype(
+       AttributeDescription *sub,
+       AttributeDescription *super
+)
 {
-       if( ad == NULL ) return;
+       if( !is_at_subtype( sub->ad_type, super->ad_type ) ) {
+               return 0;
+       }
 
-       ber_bvfree( ad->ad_cname );
-       free( ad->ad_lang );
+       if( super->ad_flags && ( super->ad_flags == sub->ad_flags )) {
+               return 0;
+       }
 
-       if( freeit ) free( ad );
+       if( super->ad_lang != NULL && ( sub->ad_lang == NULL
+               || strcasecmp( super->ad_lang, sub->ad_lang )))
+       {
+               return 0;
+       }
+
+       return 1;
+}
+
+
+int ad_inlist(
+       AttributeDescription *desc,
+       char **attrs )
+{
+       int i;
+       for( i=0; attrs[i] != NULL; i++ ) {
+               AttributeDescription *ad = NULL;
+               const char *text;
+               int rc;
+               
+               rc = slap_str2ad( attrs[i], &ad, &text );
+
+               if( rc != LDAP_SUCCESS ) continue;
+
+               rc = is_ad_subtype( desc, ad );
+
+               ad_free( ad, 1 );
+
+               if( rc ) return 1;
+       }
+
+       return 0;
 }
 
 #endif