]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-ldap/config.c
fix rewrite iteration
[openldap] / servers / slapd / back-ldap / config.c
index 605a16ab2f621532e3456dc1c24a9f7674ca8c5e..8f041a117e327b2db716d12c01a5315877f2898b 100644 (file)
@@ -114,9 +114,10 @@ ldap_back_db_config(
        /* dn massaging */
        } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
 #ifndef ENABLE_REWRITE
-               char *dn, *massaged_dn;
+               struct berval *bd2, *nd2;
 #endif /* ENABLE_REWRITE */
                BackendDB *tmp_be;
+               struct berval bdn, ndn;
                
                /*
                 * syntax:
@@ -137,7 +138,15 @@ ldap_back_db_config(
                        return( 1 );
                }
                
-               tmp_be = select_backend( argv[1], 0, 0 );
+               bdn.bv_val = argv[1];
+               bdn.bv_len = strlen(bdn.bv_val);
+               if ( dnNormalize2( NULL, &bdn, &ndn ) != LDAP_SUCCESS ) {
+                       fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
+                               fname, lineno, bdn.bv_val );
+                       return( 1 );
+               }
+               tmp_be = select_backend( &ndn, 0, 0 );
+               free( ndn.bv_val );
                if ( tmp_be != NULL && tmp_be != be ) {
                        fprintf( stderr, "%s: line %d: suffix already in use"
                                       " by another backend in"
@@ -147,7 +156,15 @@ ldap_back_db_config(
                        return( 1 );                                            
                }
 
-               tmp_be = select_backend( argv[2], 0, 0 );
+               bdn.bv_val = argv[2];
+               bdn.bv_len = strlen(bdn.bv_val);
+               if ( dnNormalize2( NULL, &bdn, &ndn ) != LDAP_SUCCESS ) {
+                       fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
+                               fname, lineno, bdn.bv_val );
+                       return( 1 );
+               }
+               tmp_be = select_backend( &ndn, 0, 0 );
+               free( ndn.bv_val );
                if ( tmp_be != NULL ) {
                        fprintf( stderr, "%s: line %d: massaged suffix"
                                       " already in use by another backend in" 
@@ -166,18 +183,17 @@ ldap_back_db_config(
                 */
                return suffix_massage_config( li->rwinfo, argc, argv );
 #else /* !ENABLE_REWRITE */
-               dn = ch_strdup( argv[1] );
-               charray_add( &li->suffix_massage, dn );
-               (void) dn_normalize( dn );
-               charray_add( &li->suffix_massage, dn );
-               
-               massaged_dn = ch_strdup( argv[2] );
-               charray_add( &li->suffix_massage, massaged_dn );
-               (void) dn_normalize( massaged_dn );
-               charray_add( &li->suffix_massage, massaged_dn );
+               bd2 = ber_bvstrdup( argv[1] );
+               ber_bvecadd( &li->suffix_massage, bd2 );
+               nd2 = NULL;
+               dnNormalize( NULL, bd2, &nd2 );
+               ber_bvecadd( &li->suffix_massage, nd2 );
                
-               free( dn );
-               free( massaged_dn );
+               bd2 = ber_bvstrdup( argv[2] );
+               ber_bvecadd( &li->suffix_massage, bd2 );
+               nd2 = NULL;
+               dnNormalize( NULL, bd2, &nd2 );
+               ber_bvecadd( &li->suffix_massage, nd2 );
 #endif /* !ENABLE_REWRITE */
 
 #ifdef ENABLE_REWRITE
@@ -204,8 +220,9 @@ ldap_back_db_config(
                } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
                        map = &li->at_map;
                } else {
-                       fprintf( stderr,
-       "%s: line %d: syntax is \"map {objectclass | attribute} {<source> | *} [<dest> | *]\"\n",
+                       fprintf( stderr, "%s: line %d: syntax is "
+                               "\"map {objectclass | attribute} {<source> | *} "
+                                       "[<dest> | *]\"\n",
                                fname, lineno );
                        return( 1 );
                }
@@ -241,15 +258,16 @@ ldap_back_db_config(
                                fname, lineno );
                }
 
-               mapping = (struct ldapmapping *)ch_calloc( 2, sizeof(struct ldapmapping) );
+               mapping = (struct ldapmapping *)ch_calloc( 2,
+                       sizeof(struct ldapmapping) );
                if ( mapping == NULL ) {
                        fprintf( stderr,
                                "%s: line %d: out of memory\n",
                                fname, lineno );
                        return( 1 );
                }
-               mapping->src = ch_strdup(src);
-               mapping->dst = ch_strdup(dst);
+               ber_str2bv( src, 0, 1, &mapping->src );
+               ber_str2bv( dst, 0, 1, &mapping->dst );
                if ( *dst != 0 ) {
                        mapping[1].src = mapping->dst;
                        mapping[1].dst = mapping->src;
@@ -258,8 +276,8 @@ ldap_back_db_config(
                        mapping[1].dst = mapping->dst;
                }
 
-               if ( avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL
-                       || avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
+               if ( avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL ||
+                       avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
                {
                        fprintf( stderr,
                                "%s: line %d: duplicate mapping found (ignored)\n",
@@ -274,8 +292,8 @@ ldap_back_db_config(
 
        /* anything else */
        } else {
-               fprintf( stderr,
-"%s: line %d: unknown directive \"%s\" in ldap database definition (ignored)\n",
+               fprintf( stderr, "%s: line %d: unknown directive \"%s\" "
+                       "in ldap database definition (ignored)\n",
                    fname, lineno, argv[0] );
        }
        return 0;
@@ -286,8 +304,9 @@ mapping_cmp ( const void *c1, const void *c2 )
 {
        struct ldapmapping *map1 = (struct ldapmapping *)c1;
        struct ldapmapping *map2 = (struct ldapmapping *)c2;
-
-       return ( strcasecmp(map1->src, map2->src) );
+       int rc = map1->src.bv_len - map2->src.bv_len;
+       if (rc) return rc;
+       return ( strcasecmp(map1->src.bv_val, map2->src.bv_val) );
 }
 
 int
@@ -296,11 +315,39 @@ mapping_dup ( void *c1, void *c2 )
        struct ldapmapping *map1 = (struct ldapmapping *)c1;
        struct ldapmapping *map2 = (struct ldapmapping *)c2;
 
-       return( ( strcasecmp(map1->src, map2->src) == 0 ) ? -1 : 0 );
+       return( ( strcasecmp(map1->src.bv_val, map2->src.bv_val) == 0 ) ? -1 : 0 );
 }
 
-char *
-ldap_back_map ( struct ldapmap *map, char *s, int remap )
+void
+ldap_back_map_init ( struct ldapmap *lm, struct ldapmapping **m )
+{
+       struct ldapmapping *mapping;
+
+       assert( m );
+
+       *m = NULL;
+       
+       mapping = (struct ldapmapping *)ch_calloc( 2, 
+                       sizeof( struct ldapmapping ) );
+       if ( mapping == NULL ) {
+               return;
+       }
+
+       ber_str2bv( "objectclass", sizeof("objectclass")-1, 1, &mapping->src);
+       ber_dupbv( &mapping->dst, &mapping->src );
+       mapping[1].src = mapping->src;
+       mapping[1].dst = mapping->dst;
+
+       avl_insert( &lm->map, (caddr_t)mapping, 
+                       mapping_cmp, mapping_dup );
+       avl_insert( &lm->remap, (caddr_t)&mapping[1], 
+                       mapping_cmp, mapping_dup );
+       *m = mapping;
+}
+
+void
+ldap_back_map ( struct ldapmap *map, struct berval *s, struct berval *bv,
+       int remap )
 {
        Avlnode *tree;
        struct ldapmapping *mapping, fmapping;
@@ -310,35 +357,38 @@ ldap_back_map ( struct ldapmap *map, char *s, int remap )
        else
                tree = map->map;
 
-       fmapping.src = s;
+       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 == 0 )
-                       return(NULL);
-               return(mapping->dst);
+               if ( mapping->dst.bv_val )
+                       *bv = mapping->dst;
+               return;
        }
 
-       if (map->drop_missing)
-               return(NULL);
+       if (!map->drop_missing)
+               *bv = *s;
 
-       return(s);
+       return;
 }
 
 char *
 ldap_back_map_filter(
                struct ldapmap *at_map,
                struct ldapmap *oc_map,
-               char *f,
+               struct berval *f,
                int remap
 )
 {
-       char *nf, *m, *p, *q, *s, c;
+       char *nf, *p, *q, *s, c;
        int len, extra, plen, in_quote;
+       struct berval m, tmp;
 
        if (f == NULL)
                return(NULL);
 
-       len = strlen(f);
+       len = f->bv_len;
        extra = len;
        len *= 2;
        nf = ch_malloc( len + 1 );
@@ -352,12 +402,12 @@ ldap_back_map_filter(
        s = nf;
        q = NULL;
        in_quote = 0;
-       for (p = f; (c = *p); p++) {
+       for (p = f->bv_val; (c = *p); p++) {
                if (c == '"') {
                        in_quote = !in_quote;
                        if (q != NULL) {
                                plen = p - q;
-                               memcpy(s, q, plen);
+                               AC_MEMCPY(s, q, plen);
                                s += plen;
                                q = NULL;
                        }
@@ -376,14 +426,16 @@ ldap_back_map_filter(
                } else {
                        if (q != NULL) {
                                *p = 0;
-                               m = ldap_back_map(at_map, q, remap);
-                               if (m == NULL)
-                                       m = ldap_back_map(oc_map, q, remap);
-                               if (m == NULL) {
-                                       m = q;
+                               tmp.bv_len = p - q;
+                               tmp.bv_val = q;
+                               ldap_back_map(at_map, &tmp, &m, remap);
+                               if (m.bv_val == NULL)
+                                       ldap_back_map(oc_map, &tmp, &m, remap);
+                               if (m.bv_val == NULL) {
+                                       m = tmp;
                                }
                                extra += p - q;
-                               plen = strlen(m);
+                               plen = m.bv_len;
                                extra -= plen;
                                if (extra < 0) {
                                        while (extra < 0) {
@@ -398,7 +450,7 @@ ldap_back_map_filter(
                                        }
                                        s += (long)nf;
                                }
-                               memcpy(s, m, plen);
+                               AC_MEMCPY(s, m.bv_val, plen);
                                s += plen;
                                *p = c;
                                q = NULL;
@@ -413,34 +465,30 @@ ldap_back_map_filter(
 char **
 ldap_back_map_attrs(
                struct ldapmap *at_map,
-               char **a,
+               AttributeName *an,
                int remap
 )
 {
-       int i, j, count;
-       char **na, *mapped;
+       int i;
+       char **na;
+       struct berval mapped;
 
-       if (a == NULL)
+       if (an == NULL)
                return(NULL);
 
-       for (count = 0; a[count] != NULL; count++) {
+       for (i = 0; an[i].an_name.bv_val; i++) {
                /*  */
        }
 
-       na = (char **)ch_calloc( count + 1, sizeof(char *) );
+       na = (char **)ch_calloc( i + 1, sizeof(char *) );
        if (na == NULL)
                return(NULL);
 
-       for (i = 0, j = 0; i < count; i++) {
-               mapped = ldap_back_map(at_map, a[i], remap);
-               if (mapped != NULL) {
-                       mapped = ch_strdup(mapped);
-                       if (mapped == NULL) {
-                               charray_free(na);
-                               return(NULL);
-                       }
-                       na[j] = mapped;
-                       j++;
+       for (i = 0; an[i].an_name.bv_val; i++) {
+               ldap_back_map(at_map, &an[i].an_name, &mapped, remap);
+               if (mapped.bv_val != NULL) {
+                       na[i] = mapped.bv_val;
+                       i++;
                }
        }
        return(na);
@@ -481,12 +529,12 @@ suffix_massage_patternize( const char *s, int normalize )
 {
        char *res;
 
-       res = ch_calloc( sizeof( char ), strlen( s ) + 3 );
+       res = ch_calloc( sizeof( char ), strlen( s ) + sizeof("%1") );
 
        sprintf( res, "%%1%s", s );
 
        if ( normalize ) {
-               char *out = dn_normalize( res + 2 );
+               char *out = dn_normalize( res + (sizeof("%1")-1) );
                if ( out != res + 2 ) {
                        strcpy( res + 2, out );
                        free( out );
@@ -579,4 +627,3 @@ suffix_massage_config(
        return 0;
 }
 #endif /* ENABLE_REWRITE */
-