]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/at.c
Merge remote branch 'origin/mdb.master'
[openldap] / servers / slapd / at.c
index 7ab6eed84fd0cf7761791aaa84a301ffd44446e1..e4d95541e23965c159c671f411107ebf13c38144 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1998-2006 The OpenLDAP Foundation.
+ * Copyright 1998-2012 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
 #include "slap.h"
 
 
-int is_at_syntax(
-       AttributeType *at,
-       const char *oid )
+const char *
+at_syntax(
+       AttributeType   *at )
 {
-       for( ; at != NULL; at = at->sat_sup ) {
-               if( at->sat_syntax_oid ) {
-                       return ( strcmp( at->sat_syntax_oid, oid ) == 0 );
+       for ( ; at != NULL; at = at->sat_sup ) {
+               if ( at->sat_syntax_oid ) {
+                       return at->sat_syntax_oid;
                }
        }
 
+       assert( 0 );
+
+       return NULL;
+}
+
+int
+is_at_syntax(
+       AttributeType   *at,
+       const char      *oid )
+{
+       const char *syn_oid = at_syntax( at );
+
+       if ( syn_oid ) {
+               return strcmp( syn_oid, oid ) == 0;
+       }
+
        return 0;
 }
 
@@ -58,9 +74,12 @@ struct aindexrec {
 
 static Avlnode *attr_index = NULL;
 static Avlnode *attr_cache = NULL;
-static LDAP_STAILQ_HEAD(ATList, slap_attribute_type) attr_list
+static LDAP_STAILQ_HEAD(ATList, AttributeType) attr_list
        = LDAP_STAILQ_HEAD_INITIALIZER(attr_list);
 
+/* Last hardcoded attribute registered */
+AttributeType *at_sys_tail;
+
 int at_oc_cache;
 
 static int
@@ -234,7 +253,7 @@ at_delete( AttributeType *at )
 {
        at->sat_flags |= SLAP_AT_DELETED;
 
-       LDAP_STAILQ_REMOVE(&attr_list,at,slap_attribute_type,sat_next);
+       LDAP_STAILQ_REMOVE(&attr_list, at, AttributeType, sat_next);
 
        at_delete_names( at );
 }
@@ -265,8 +284,18 @@ at_clean( AttributeType *a )
                }
        }
 
-       if ( a->sat_oidmacro ) ldap_memfree( a->sat_oidmacro );
-       if ( a->sat_subtypes ) ldap_memfree( a->sat_subtypes );
+       if ( a->sat_oidmacro ) {
+               ldap_memfree( a->sat_oidmacro );
+               a->sat_oidmacro = NULL;
+       }
+       if ( a->sat_soidmacro ) {
+               ldap_memfree( a->sat_soidmacro );
+               a->sat_soidmacro = NULL;
+       }
+       if ( a->sat_subtypes ) {
+               ldap_memfree( a->sat_subtypes );
+               a->sat_subtypes = NULL;
+       }
 }
 
 static void
@@ -320,7 +349,7 @@ at_next( AttributeType **at )
 {
        assert( at != NULL );
 
-#if 1  /* pedantic check */
+#if 0  /* pedantic check: don't use this */
        {
                AttributeType *tmp = NULL;
 
@@ -430,9 +459,11 @@ at_insert(
                        /* replacing a deleted definition? */
                        if ( old_sat->sat_flags & SLAP_AT_DELETED ) {
                                AttributeType tmp;
+                               AttributeDescription *ad;
                                
                                /* Keep old oid, free new oid;
                                 * Keep old ads, free new ads;
+                                * Keep old ad_mutex, free new ad_mutex;
                                 * Keep new everything else, free old
                                 */
                                tmp = *old_sat;
@@ -441,8 +472,18 @@ at_insert(
                                tmp.sat_oid = sat->sat_oid;
                                old_sat->sat_ad = tmp.sat_ad;
                                tmp.sat_ad = sat->sat_ad;
+                               old_sat->sat_ad_mutex = tmp.sat_ad_mutex;
+                               tmp.sat_ad_mutex = sat->sat_ad_mutex;
                                *sat = tmp;
 
+                               /* Check for basic ad pointing at old cname */
+                               for ( ad = old_sat->sat_ad; ad; ad=ad->ad_next ) {
+                                       if ( ad->ad_cname.bv_val == sat->sat_cname.bv_val ) {
+                                               ad->ad_cname = old_sat->sat_cname;
+                                               break;
+                                       }
+                               }
+
                                at_clean( sat );
                                at_destroy_one( air );
 
@@ -525,6 +566,10 @@ at_insert(
                }
        }
 
+       if ( sat->sat_flags & SLAP_AT_HARDCODE ) {
+               prev = at_sys_tail;
+               at_sys_tail = sat;
+       }
        if ( prev ) {
                LDAP_STAILQ_INSERT_AFTER( &attr_list, prev, sat, sat_next );
        } else {
@@ -549,6 +594,7 @@ at_add(
        int             code = LDAP_SUCCESS;
        char            *cname = NULL;
        char            *oidm = NULL;
+       char            *soidm = NULL;
 
        if ( !at->at_oid ) {
                *err = "";
@@ -581,7 +627,7 @@ at_add(
                        goto error_return;
                }
                if ( oid != at->at_syntax_oid ) {
-                       ldap_memfree( at->at_syntax_oid );
+                       soidm = at->at_syntax_oid;
                        at->at_syntax_oid = oid;
                }
        }
@@ -632,6 +678,7 @@ at_add(
        sat->sat_cname.bv_val = cname;
        sat->sat_cname.bv_len = strlen( cname );
        sat->sat_oidmacro = oidm;
+       sat->sat_soidmacro = soidm;
        ldap_pvt_thread_mutex_init(&sat->sat_ad_mutex);
 
        if ( at->at_sup_oid ) {
@@ -675,8 +722,22 @@ at_add(
         * its own superiorss
         */
        if ( sat->sat_sup ) {
-               sat->sat_syntax = sat->sat_sup->sat_syntax;
-               sat->sat_equality = sat->sat_sup->sat_equality;
+               Syntax *syn = syn_find(sat->sat_sup->sat_syntax->ssyn_oid);
+               if ( syn != sat->sat_sup->sat_syntax ) {
+                       sat->sat_syntax = ch_malloc( sizeof( Syntax ));
+                       *sat->sat_syntax = *sat->sat_sup->sat_syntax;
+               } else {
+                       sat->sat_syntax = sat->sat_sup->sat_syntax;
+               }
+               if ( sat->sat_sup->sat_equality ) {
+                       MatchingRule *mr = mr_find( sat->sat_sup->sat_equality->smr_oid );
+                       if ( mr != sat->sat_sup->sat_equality ) {
+                               sat->sat_equality = ch_malloc( sizeof( MatchingRule ));
+                               *sat->sat_equality = *sat->sat_sup->sat_equality;
+                       } else {
+                               sat->sat_equality = sat->sat_sup->sat_equality;
+                       }
+               }
                sat->sat_approx = sat->sat_sup->sat_approx;
                sat->sat_ordering = sat->sat_sup->sat_ordering;
                sat->sat_substr = sat->sat_sup->sat_substr;
@@ -713,9 +774,12 @@ at_add(
                        goto error_return;
                }
 
-               if( sat->sat_syntax != NULL && sat->sat_syntax != syn ) {
-                       code = SLAP_SCHERR_ATTR_BAD_SUP;
-                       goto error_return;
+               if ( sat->sat_syntax != NULL && sat->sat_syntax != syn ) {
+                       /* BEWARE: no loop detection! */
+                       if ( syn_is_sup( sat->sat_syntax, syn ) ) {
+                               code = SLAP_SCHERR_ATTR_BAD_SUP;
+                               goto error_return;
+                       }
                }
 
                sat->sat_syntax = syn;
@@ -875,6 +939,11 @@ error_return:;
                        at->at_oid = oidm;
                }
 
+               if ( soidm ) {
+                       SLAP_FREE( at->at_syntax_oid );
+                       at->at_syntax_oid = soidm;
+               }
+
        } else if ( rsat ) {
                *rsat = sat;
        }
@@ -917,7 +986,7 @@ at_unparse( BerVarray *res, AttributeType *start, AttributeType *end, int sys )
        /* count the result size */
        i = 0;
        for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
-               if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) continue;
+               if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) break;
                i++;
                if ( at == end ) break;
        }
@@ -934,10 +1003,13 @@ at_unparse( BerVarray *res, AttributeType *start, AttributeType *end, int sys )
        i = 0;
        for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
                LDAPAttributeType lat, *latp;
-               if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) continue;
-               if ( at->sat_oidmacro ) {
+               if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) break;
+               if ( at->sat_oidmacro || at->sat_soidmacro ) {
                        lat = at->sat_atype;
-                       lat.at_oid = at->sat_oidmacro;
+                       if ( at->sat_oidmacro )
+                               lat.at_oid = at->sat_oidmacro;
+                       if ( at->sat_soidmacro )
+                               lat.at_syntax_oid = at->sat_soidmacro;
                        latp = &lat;
                } else {
                        latp = &at->sat_atype;
@@ -987,7 +1059,7 @@ at_schema_info( Entry *e )
 }
 
 int
-register_at( char *def, AttributeDescription **rad, int dupok )
+register_at( const char *def, AttributeDescription **rad, int dupok )
 {
        LDAPAttributeType *at;
        int code, freeit = 0;
@@ -1008,10 +1080,10 @@ register_at( char *def, AttributeDescription **rad, int dupok )
                        freeit = 1;
 
                } else {
-                       ldap_attributetype_free( at );
                        Debug( LDAP_DEBUG_ANY,
                                "register_at: AttributeType \"%s\": %s, %s\n",
                                def, scherr2str(code), err );
+                       ldap_attributetype_free( at );
                        return code;
                }
        }