]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-meta/map.c
Read config tree from back-ldif
[openldap] / servers / slapd / back-meta / map.c
index 57660178a77cf49b515a6f1a85e3bcd298cdd0ec..5021fc5c59f3da827b5b38e9e980811d4d4695d3 100644 (file)
@@ -70,7 +70,7 @@ mapping_cmp ( const void *c1, const void *c2 )
        struct ldapmapping *map2 = (struct ldapmapping *)c2;
        int rc = map1->src.bv_len - map2->src.bv_len;
        if (rc) return rc;
-       return ( strcasecmp(map1->src.bv_val, map2->src.bv_val) );
+       return ( strcasecmp( map1->src.bv_val, map2->src.bv_val ) );
 }
 
 int
@@ -79,7 +79,7 @@ mapping_dup ( void *c1, void *c2 )
        struct ldapmapping *map1 = (struct ldapmapping *)c1;
        struct ldapmapping *map2 = (struct ldapmapping *)c2;
 
-       return( ( strcasecmp(map1->src.bv_val, map2->src.bv_val) == 0 ) ? -1 : 0 );
+       return ( ( strcasecmp( map1->src.bv_val, map2->src.bv_val ) == 0 ) ? -1 : 0 );
 }
 
 void
@@ -109,32 +109,48 @@ ldap_back_map_init ( struct ldapmap *lm, struct ldapmapping **m )
        *m = mapping;
 }
 
-void
-ldap_back_map ( struct ldapmap *map, struct berval *s, struct berval *bv,
+int
+ldap_back_mapping ( struct ldapmap *map, struct berval *s, struct ldapmapping **m,
        int remap )
 {
        Avlnode *tree;
-       struct ldapmapping *mapping, fmapping;
+       struct ldapmapping fmapping;
+
+       assert( m );
 
-       if (remap == BACKLDAP_REMAP)
+       if ( remap == BACKLDAP_REMAP ) {
                tree = map->remap;
-       else
+       } else {
                tree = map->map;
+       }
 
-       bv->bv_len = 0;
-       bv->bv_val = NULL;
        fmapping.src = *s;
-       mapping = (struct ldapmapping *)avl_find( tree, (caddr_t)&fmapping, mapping_cmp );
-       if (mapping != NULL) {
-               if ( mapping->dst.bv_val )
+       *m = (struct ldapmapping *)avl_find( tree, (caddr_t)&fmapping, mapping_cmp );
+       if ( *m == NULL ) {
+               return map->drop_missing;
+       }
+
+       return 0;
+}
+
+void
+ldap_back_map ( struct ldapmap *map, struct berval *s, struct berval *bv,
+       int remap )
+{
+       struct ldapmapping *mapping;
+
+       BER_BVZERO( bv );
+       ( void )ldap_back_mapping( map, s, &mapping, remap );
+       if ( mapping != NULL ) {
+               if ( !BER_BVISNULL( &mapping->dst ) ) {
                        *bv = mapping->dst;
+               }
                return;
        }
 
-       if (!map->drop_missing)
+       if ( !map->drop_missing ) {
                *bv = *s;
-
-       return;
+       }
 }
 
 int
@@ -149,28 +165,29 @@ ldap_back_map_attrs(
        char **na;
        struct berval mapped;
 
-       if (an == NULL) {
+       if ( an == NULL ) {
                *mapped_attrs = NULL;
                return LDAP_SUCCESS;
        }
 
-       for (i = 0; an[i].an_name.bv_val; i++) {
-               /*  */
-       }
+       for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ )
+               /*  */ ;
 
        na = (char **)ch_calloc( i + 1, sizeof(char *) );
-       if (na == NULL) {
+       if ( na == NULL ) {
                *mapped_attrs = NULL;
                return LDAP_NO_MEMORY;
        }
 
-       for (i = j = 0; an[i].an_name.bv_val; i++) {
-               ldap_back_map(at_map, &an[i].an_name, &mapped, remap);
-               if (mapped.bv_val != NULL && mapped.bv_val != '\0')
+       for ( i = j = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
+               ldap_back_map( at_map, &an[i].an_name, &mapped, remap );
+               if ( !BER_BVISNULL( &mapped ) && !BER_BVISEMPTY( &mapped ) ) {
                        na[j++] = mapped.bv_val;
+               }
        }
-       if (j == 0 && i != 0)
+       if ( j == 0 && i != 0 ) {
                na[j++] = LDAP_NO_ATTRS;
+       }
        na[j] = NULL;
 
        *mapped_attrs = na;
@@ -190,12 +207,12 @@ map_attr_value(
        int                     freeval = 0;
 
        ldap_back_map( &dc->rwmap->rwm_at, &ad->ad_cname, mapped_attr, remap );
-       if ( mapped_attr->bv_val == NULL || mapped_attr->bv_val[0] == '\0') {
+       if ( BER_BVISNULL( mapped_attr ) || BER_BVISEMPTY( mapped_attr ) ) {
                /*
                 * FIXME: are we sure we need to search oc_map if at_map fails?
                 */
                ldap_back_map( &dc->rwmap->rwm_oc, &ad->ad_cname, mapped_attr, remap );
-               if ( mapped_attr->bv_val == NULL || mapped_attr->bv_val[0] == '\0' ) {
+               if ( BER_BVISNULL( mapped_attr ) || BER_BVISEMPTY( mapped_attr ) ) {
                        *mapped_attr = ad->ad_cname;
                }
        }
@@ -228,7 +245,7 @@ map_attr_value(
 
        } else if ( ad == slap_schema.si_ad_objectClass || ad == slap_schema.si_ad_structuralObjectClass ) {
                ldap_back_map( &dc->rwmap->rwm_oc, value, &vtmp, remap );
-               if ( vtmp.bv_val == NULL || vtmp.bv_val[0] == '\0' ) {
+               if ( BER_BVISNULL( &vtmp ) || BER_BVISEMPTY( &vtmp ) ) {
                        vtmp = *value;
                }
                
@@ -341,13 +358,13 @@ ldap_back_int_filter_map_rewrite(
 
                /* cannot be a DN ... */
 
-               fstr->bv_len = atmp.bv_len + ( sizeof("(=*)") - 1 );
-               fstr->bv_val = malloc( fstr->bv_len + 128 );
+               fstr->bv_len = atmp.bv_len + ( STRLENOF( "(=*)" ) );
+               fstr->bv_val = malloc( fstr->bv_len + 128 ); /* FIXME: why 128 ? */
 
                snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
                        atmp.bv_val );
 
-               if ( f->f_sub_initial.bv_val != NULL ) {
+               if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
                        len = fstr->bv_len;
 
                        filter_escape_value( &f->f_sub_initial, &vtmp );
@@ -363,7 +380,7 @@ ldap_back_int_filter_map_rewrite(
                }
 
                if ( f->f_sub_any != NULL ) {
-                       for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
+                       for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
                                len = fstr->bv_len;
                                filter_escape_value( &f->f_sub_any[i], &vtmp );
 
@@ -377,7 +394,7 @@ ldap_back_int_filter_map_rewrite(
                        }
                }
 
-               if ( f->f_sub_final.bv_val != NULL ) {
+               if ( !BER_BVISNULL( &f->f_sub_final ) ) {
                        len = fstr->bv_len;
 
                        filter_escape_value( &f->f_sub_final, &vtmp );
@@ -401,7 +418,7 @@ ldap_back_int_filter_map_rewrite(
                        return -1;
                }
 
-               fstr->bv_len = atmp.bv_len + ( sizeof("(=*)") - 1 );
+               fstr->bv_len = atmp.bv_len + ( STRLENOF( "(=*)" ) );
                fstr->bv_val = malloc( fstr->bv_len + 1 );
 
                snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
@@ -411,8 +428,8 @@ ldap_back_int_filter_map_rewrite(
        case LDAP_FILTER_AND:
        case LDAP_FILTER_OR:
        case LDAP_FILTER_NOT:
-               fstr->bv_len = sizeof("(%)") - 1;
-               fstr->bv_val = malloc( fstr->bv_len + 128 );
+               fstr->bv_len = STRLENOF( "(%)" );
+               fstr->bv_val = malloc( fstr->bv_len + 128 );    /* FIXME: why 128? */
 
                snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
                        f->f_choice == LDAP_FILTER_AND ? '&' :
@@ -446,24 +463,22 @@ ldap_back_int_filter_map_rewrite(
                        }
 
                } else {
-                       atmp.bv_len = 0;
-                       atmp.bv_val = "";
-                       
+                       BER_BVSTR( &atmp, "" );
                        filter_escape_value( &f->f_mr_value, &vtmp );
                }
-                       
 
+               /* FIXME: cleanup (less ?: operators...) */
                fstr->bv_len = atmp.bv_len +
-                       ( f->f_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
-                       ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) +
-                       vtmp.bv_len + ( sizeof("(:=)") - 1 );
+                       ( f->f_mr_dnattrs ? STRLENOF( ":dn" ) : 0 ) +
+                       ( !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_len + 1 : 0 ) +
+                       vtmp.bv_len + ( STRLENOF( "(:=)" ) );
                fstr->bv_val = malloc( fstr->bv_len + 1 );
 
                snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
                        atmp.bv_val,
                        f->f_mr_dnattrs ? ":dn" : "",
-                       f->f_mr_rule_text.bv_len ? ":" : "",
-                       f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "",
+                       !BER_BVISEMPTY( &f->f_mr_rule_text ) ? ":" : "",
+                       !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_val : "",
                        vtmp.bv_val );
                ber_memfree( vtmp.bv_val );
                } break;
@@ -652,10 +667,13 @@ ldap_dnattr_rewrite(
        struct berval   bv;
        int             i, last;
 
-       for ( last = 0; a_vals[last].bv_val != NULL; last++ );
+       assert( a_vals != NULL );
+
+       for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
+               ;
        last--;
 
-       for ( i = 0; a_vals[i].bv_val != NULL; i++ ) {
+       for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
                switch ( ldap_back_dn_massage( dc, &a_vals[i], &bv ) ) {
                case LDAP_UNWILLING_TO_PERFORM:
                        /*
@@ -664,17 +682,16 @@ ldap_dnattr_rewrite(
                         * it should be when searching (e.g. ACLs).
                         */
                        ch_free( a_vals[i].bv_val );
-                       if (last > i ) {
+                       if ( last > i ) {
                                a_vals[i] = a_vals[last];
                        }
-                       a_vals[last].bv_len = 0;
-                       a_vals[last].bv_val = NULL;
+                       BER_BVZERO( &a_vals[last] );
                        last--;
                        break;
 
                default:
                        /* leave attr untouched if massage failed */
-                       if ( bv.bv_val && bv.bv_val != a_vals[i].bv_val ) {
+                       if ( !BER_BVISNULL( &bv ) && bv.bv_val != a_vals[i].bv_val ) {
                                ch_free( a_vals[i].bv_val );
                                a_vals[i] = bv;
                        }
@@ -694,10 +711,13 @@ ldap_dnattr_result_rewrite(
        struct berval   bv;
        int             i, last;
 
-       for ( last = 0; a_vals[last].bv_val; last++ );
+       assert( a_vals != NULL );
+
+       for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
+               ;
        last--;
 
-       for ( i = 0; a_vals[i].bv_val; i++ ) {
+       for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
                switch ( ldap_back_dn_massage( dc, &a_vals[i], &bv ) ) {
                case LDAP_UNWILLING_TO_PERFORM:
                        /*
@@ -715,7 +735,7 @@ ldap_dnattr_result_rewrite(
 
                default:
                        /* leave attr untouched if massage failed */
-                       if ( bv.bv_val && a_vals[i].bv_val != bv.bv_val ) {
+                       if ( !BER_BVISNULL( &bv ) && a_vals[i].bv_val != bv.bv_val ) {
                                LBER_FREE( a_vals[i].bv_val );
                                a_vals[i] = bv;
                        }