]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-ldap/config.c
Happy new year
[openldap] / servers / slapd / back-ldap / config.c
index 425ef251da78487c9e34124396592ca43d27f491..b61821914ca8f3ea1b2d4e9056618b757ede5e91 100644 (file)
@@ -1,7 +1,7 @@
 /* config.c - ldap backend configuration file routine */
 /* $OpenLDAP$ */
 /*
- * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  */
 /* This is an altered version */
@@ -44,6 +44,7 @@
 
 #include "slap.h"
 #include "back-ldap.h"
+#include "lutil.h"
 
 int
 ldap_back_db_config(
@@ -111,13 +112,23 @@ ldap_back_db_config(
                }
                li->bindpw = ch_strdup(argv[1]);
        
+       /* save bind creds for referral rebinds? */
+       } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
+               if (argc != 1) {
+                       fprintf( stderr,
+       "%s: line %d: rebind-as-user takes no arguments\n",
+                           fname, lineno );
+                       return( 1 );
+               }
+               li->savecred = 1;
+       
        /* dn massaging */
        } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
-#ifndef ENABLE_REWRITE
-               char *dn, *massaged_dn;
-#endif /* ENABLE_REWRITE */
                BackendDB *tmp_be;
-               struct berval bdn, *ndn = NULL;
+               struct berval bvnc, nvnc, pvnc, brnc, nrnc, prnc;
+#ifdef ENABLE_REWRITE
+               int rc;
+#endif /* ENABLE_REWRITE */
                
                /*
                 * syntax:
@@ -138,42 +149,48 @@ ldap_back_db_config(
                        return( 1 );
                }
                
-               bdn.bv_val = argv[1];
-               bdn.bv_len = strlen(bdn.bv_val);
-               if ( dnNormalize( NULL, &bdn, &ndn ) != LDAP_SUCCESS ) {
+               ber_str2bv( argv[1], 0, 0, &bvnc );
+               if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc ) != LDAP_SUCCESS ) {
                        fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
-                               fname, lineno, bdn.bv_val );
+                               fname, lineno, bvnc.bv_val );
                        return( 1 );
                }
-               tmp_be = select_backend( ndn, 0, 0 );
-               ber_bvfree( ndn );
-               ndn = NULL;
+               tmp_be = select_backend( &nvnc, 0, 0 );
                if ( tmp_be != NULL && tmp_be != be ) {
                        fprintf( stderr, "%s: line %d: suffix already in use"
                                       " by another backend in"
                                       " \"suffixMassage <suffix>"
                                       " <massaged suffix>\"\n",
                                fname, lineno );
-                       return( 1 );                                            
+                       free( nvnc.bv_val );
+                       free( pvnc.bv_val );
+                       return( 1 );
                }
 
-               bdn.bv_val = argv[2];
-               bdn.bv_len = strlen(bdn.bv_val);
-               if ( dnNormalize( NULL, &bdn, &ndn ) != LDAP_SUCCESS ) {
+               ber_str2bv( argv[2], 0, 0, &brnc );
+               if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc ) != LDAP_SUCCESS ) {
                        fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
-                               fname, lineno, bdn.bv_val );
+                               fname, lineno, brnc.bv_val );
+                       free( nvnc.bv_val );
+                       free( pvnc.bv_val );
                        return( 1 );
                }
-               tmp_be = select_backend( ndn, 0, 0 );
-               ber_bvfree( ndn );
+
+#if 0
+               tmp_be = select_backend( &nrnc, 0, 0 );
                if ( tmp_be != NULL ) {
                        fprintf( stderr, "%s: line %d: massaged suffix"
                                       " already in use by another backend in" 
                                       " \"suffixMassage <suffix>"
                                       " <massaged suffix>\"\n",
                                 fname, lineno );
+                       free( nvnc.bv_val );
+                       free( pvnc.bv_val );
+                       free( nrnc.bv_val );
+                       free( prnc.bv_val );
                         return( 1 );
                }
+#endif
 
 #ifdef ENABLE_REWRITE
                /*
@@ -182,27 +199,31 @@ ldap_back_db_config(
                 * FIXME: no extra rewrite capabilities should be added
                 * to the database
                 */
-               return suffix_massage_config( li->rwinfo, argc, argv );
+               rc = suffix_massage_config( li->rwinfo, &pvnc, &nvnc, &prnc, &nrnc );
+               free( nvnc.bv_val );
+               free( pvnc.bv_val );
+               free( nrnc.bv_val );
+               free( prnc.bv_val );
+
+               return( rc );
+
 #else /* !ENABLE_REWRITE */
-               dn = ch_strdup( argv[1] );
-               charray_add( &li->suffix_massage, dn );
-               (void) dn_normalize( dn );
-               charray_add( &li->suffix_massage, dn );
+               ber_bvarray_add( &li->suffix_massage, &pvnc );
+               ber_bvarray_add( &li->suffix_massage, &nvnc );
                
-               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 );
-               
-               free( dn );
-               free( massaged_dn );
+               ber_bvarray_add( &li->suffix_massage, &prnc );
+               ber_bvarray_add( &li->suffix_massage, &nrnc );
 #endif /* !ENABLE_REWRITE */
 
-#ifdef ENABLE_REWRITE
        /* rewrite stuff ... */
        } else if ( strncasecmp( argv[0], "rewrite", 7 ) == 0 ) {
+#ifdef ENABLE_REWRITE
                return rewrite_parse( li->rwinfo, fname, lineno, argc, argv );
-#endif /* ENABLE_REWRITE */
+
+#else /* !ENABLE_REWRITE */
+               fprintf( stderr, "%s: line %d: rewrite capabilities "
+                               "are not enabled\n", fname, lineno );
+#endif /* !ENABLE_REWRITE */
                
        /* objectclass/attribute mapping */
        } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
@@ -212,7 +233,7 @@ ldap_back_db_config(
 
                if ( argc < 3 || argc > 4 ) {
                        fprintf( stderr,
-       "%s: line %d: syntax is \"map {objectclass | attribute} {<source> | *} [<dest> | *]\"\n",
+       "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
                                fname, lineno );
                        return( 1 );
                }
@@ -222,32 +243,25 @@ 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} [<local> | *] "
+                               "{<foreign> | *}\"\n",
                                fname, lineno );
                        return( 1 );
                }
 
-               if ( strcasecmp( argv[2], "*" ) != 0 ) {
-                       src = argv[2];
-                       if ( argc < 4 )
-                               dst = "";
-                       else if ( strcasecmp( argv[3], "*" ) == 0 )
-                               dst = src;
-                       else
-                               dst = argv[3];
-               } else {
-                       if ( argc < 4 ) {
-                               map->drop_missing = 1;
-                               return 0;
-                       }
-                       if ( strcasecmp( argv[3], "*" ) == 0 ) {
-                               map->drop_missing = 0;
+               if ( strcmp( argv[2], "*" ) == 0 ) {
+                       if ( argc < 4 || strcmp( argv[3], "*" ) == 0 ) {
+                               map->drop_missing = ( argc < 4 );
                                return 0;
                        }
-
-                       src = argv[3];
-                       dst = src;
+                       src = dst = argv[3];
+               } else if ( argc < 4 ) {
+                       src = "";
+                       dst = argv[2];
+               } else {
+                       src = argv[2];
+                       dst = ( strcmp( argv[3], "*" ) == 0 ? src : argv[3] );
                }
 
                if ( ( map == &li->at_map )
@@ -259,25 +273,22 @@ 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);
-               if ( *dst != 0 ) {
-                       mapping[1].src = mapping->dst;
-                       mapping[1].dst = mapping->src;
-               } else {
-                       mapping[1].src = mapping->src;
-                       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)
+               ber_str2bv( src, 0, 1, &mapping->src );
+               ber_str2bv( dst, 0, 1, &mapping->dst );
+               mapping[1].src = mapping->dst;
+               mapping[1].dst = mapping->src;
+
+               if ( (*src != '\0' &&
+                         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",
@@ -285,269 +296,113 @@ ldap_back_db_config(
                        return 0;
                }
 
-               avl_insert( &map->map, (caddr_t)mapping,
-                                       mapping_cmp, mapping_dup );
+               if ( *src != '\0' )
+                       avl_insert( &map->map, (caddr_t)mapping,
+                                               mapping_cmp, mapping_dup );
                avl_insert( &map->remap, (caddr_t)&mapping[1],
                                        mapping_cmp, mapping_dup );
 
        /* 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;
 }
 
-int
-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
-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 );
-}
-
-char *
-ldap_back_map ( struct ldapmap *map, char *s, int remap )
-{
-       Avlnode *tree;
-       struct ldapmapping *mapping, fmapping;
-
-       if (remap)
-               tree = map->remap;
-       else
-               tree = map->map;
-
-       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 (map->drop_missing)
-               return(NULL);
-
-       return(s);
-}
-
-char *
-ldap_back_map_filter(
-               struct ldapmap *at_map,
-               struct ldapmap *oc_map,
-               char *f,
-               int remap
-)
-{
-       char *nf, *m, *p, *q, *s, c;
-       int len, extra, plen, in_quote;
-
-       if (f == NULL)
-               return(NULL);
-
-       len = strlen(f);
-       extra = len;
-       len *= 2;
-       nf = ch_malloc( len + 1 );
-       if (nf == NULL)
-               return(NULL);
-
-       /* this loop assumes the filter ends with one
-        * of the delimiter chars -- probably ')'.
-        */
-
-       s = nf;
-       q = NULL;
-       in_quote = 0;
-       for (p = f; (c = *p); p++) {
-               if (c == '"') {
-                       in_quote = !in_quote;
-                       if (q != NULL) {
-                               plen = p - q;
-                               memcpy(s, q, plen);
-                               s += plen;
-                               q = NULL;
-                       }
-                       *s++ = c;
-               } else if (in_quote) {
-                       /* ignore everything in quotes --
-                        * what about attrs in DNs?
-                        */
-                       *s++ = c;
-               } else if (c != '(' && c != ')'
-                       && c != '=' && c != '>' && c != '<'
-                       && c != '|' && c != '&')
-               {
-                       if (q == NULL)
-                               q = p;
-               } 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;
-                               }
-                               extra += p - q;
-                               plen = strlen(m);
-                               extra -= plen;
-                               if (extra < 0) {
-                                       while (extra < 0) {
-                                               extra += len;
-                                               len *= 2;
-                                       }
-                                       s -= (long)nf;
-                                       nf = ch_realloc(nf, len + 1);
-                                       if (nf == NULL) {
-                                               free(nf);
-                                               return(NULL);
-                                       }
-                                       s += (long)nf;
-                               }
-                               memcpy(s, m, plen);
-                               s += plen;
-                               *p = c;
-                               q = NULL;
-                       }
-                       *s++ = c;
-               }
-       }
-       *s = 0;
-       return(nf);
-}
-
-char **
-ldap_back_map_attrs(
-               struct ldapmap *at_map,
-               struct berval **a,
-               int remap
-)
-{
-       int i, j, count;
-       char *mapped, **na;
-
-       if (a == NULL)
-               return(NULL);
-
-       for (count = 0; a[count] != NULL; count++) {
-               /*  */
-       }
-
-       na = (char **)ch_calloc( count + 1, sizeof(char *) );
-       if (na == NULL)
-               return(NULL);
-
-       for (i = 0, j = 0; i < count; i++) {
-               mapped = ldap_back_map(at_map, a[i]->bv_val, remap);
-               if (mapped != NULL) {
-                       na[j] = mapped;
-                       j++;
-               }
-       }
-       return(na);
-}
-
 #ifdef ENABLE_REWRITE
 static char *
 suffix_massage_regexize( const char *s )
 {
-       char *res, *p, *r;
+       char *res, *ptr;
+       const char *p, *r;
        int i;
 
-       for ( i = 0, p = ( char * )s; 
+       for ( i = 0, p = s; 
                        ( r = strchr( p, ',' ) ) != NULL; 
                        p = r + 1, i++ )
                ;
 
        res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
 
-       strcpy( res, "(.*)" );
-       for ( i = 0, p = ( char * )s;
+       ptr = lutil_strcopy( res, "(.*)" );
+       for ( i = 0, p = s;
                        ( r = strchr( p, ',' ) ) != NULL;
                        p = r + 1 , i++ ) {
-               strncat( res, p, r - p + 1 );
-               strcat( res, "[ ]?" );
+               ptr = lutil_strncopy( ptr, p, r - p + 1 );
+               ptr = lutil_strcopy( ptr, "[ ]?" );
 
                if ( r[ 1 ] == ' ' ) {
                        r++;
                }
        }
-       strcat( res, p );
+       lutil_strcopy( ptr, p );
 
        return res;
 }
 
 static char *
-suffix_massage_patternize( const char *s, int normalize )
+suffix_massage_patternize( const char *s )
 {
-       char *res;
-
-       res = ch_calloc( sizeof( char ), strlen( s ) + 3 );
+       ber_len_t       len;
+       char            *res;
 
-       sprintf( res, "%%1%s", s );
+       len = strlen( s );
 
-       if ( normalize ) {
-               char *out = dn_normalize( res + 2 );
-               if ( out != res + 2 ) {
-                       strcpy( res + 2, out );
-                       free( out );
-               }
+       res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) );
+       if ( res == NULL ) {
+               return NULL;
        }
 
+       strcpy( res, "%1" );
+       strcpy( res + sizeof( "%1" ) - 1, s );
+
        return res;
 }
 
 int
 suffix_massage_config( 
                struct rewrite_info *info,
-               int argc,
-               char **argv
+               struct berval *pvnc,
+               struct berval *nvnc,
+               struct berval *prnc,
+               struct berval *nrnc
 )
 {
        char *rargv[ 5 ];
+       int line = 0;
 
        rargv[ 0 ] = "rewriteEngine";
        rargv[ 1 ] = "on";
        rargv[ 2 ] = NULL;
-       rewrite_parse( info, "<suffix massage>", 1, 2, rargv );
+       rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
 
        rargv[ 0 ] = "rewriteContext";
        rargv[ 1 ] = "default";
        rargv[ 2 ] = NULL;
-       rewrite_parse( info, "<suffix massage>", 2, 2, rargv );
+       rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
 
        rargv[ 0 ] = "rewriteRule";
-       rargv[ 1 ] = suffix_massage_regexize( argv[ 1 ] );
-       rargv[ 2 ] = suffix_massage_patternize( argv[ 2 ], 0 );
+       rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
+       rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val );
        rargv[ 3 ] = ":";
        rargv[ 4 ] = NULL;
-       rewrite_parse( info, "<suffix massage>", 3, 4, rargv );
+       rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
        ch_free( rargv[ 1 ] );
        ch_free( rargv[ 2 ] );
        
        rargv[ 0 ] = "rewriteContext";
        rargv[ 1 ] = "searchResult";
        rargv[ 2 ] = NULL;
-       rewrite_parse( info, "<suffix massage>", 4, 2, rargv );
+       rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
        
        rargv[ 0 ] = "rewriteRule";
-       rargv[ 1 ] = suffix_massage_regexize( argv[ 2 ] );
-       rargv[ 2 ] = suffix_massage_patternize( argv[ 1 ], 0 );
+       rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
+       rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val );
        rargv[ 3 ] = ":";
        rargv[ 4 ] = NULL;
-       rewrite_parse( info, "<suffix massage>", 5, 4, rargv );
+       rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
        ch_free( rargv[ 1 ] );
        ch_free( rargv[ 2 ] );
 
@@ -564,32 +419,93 @@ suffix_massage_config(
        rargv[ 0 ] = "rewriteContext";
        rargv[ 1 ] = "searchFilter";
        rargv[ 2 ] = NULL;
-       rewrite_parse( info, "<suffix massage>", 6, 2, rargv );
+       rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
+
+#if 1 /* rewrite filters */
+       {
+               /*
+                * Note: this is far more optimistic than desirable:
+                * for any AVA value ending with the virtual naming
+                * context the terminal part will be replaced by the
+                * real naming context; a better solution would be to
+                * walk the filter looking for DN-valued attributes,
+                * and only rewrite those that require rewriting
+                */
+               char    vbuf_[BUFSIZ], *vbuf = vbuf_,
+                       rbuf_[BUFSIZ], *rbuf = rbuf_;
+               int     len;
+
+               len = snprintf( vbuf, sizeof( vbuf_ ), 
+                               "(.*)%s\\)(.*)", nvnc->bv_val );
+               if ( len == -1 ) {
+                       /* 
+                        * traditional behavior: snprintf returns -1 
+                        * if buffer is insufficient
+                        */
+                       return -1;
 
-#if 0 /*  matched is not normalized */
+               } else if ( len >= sizeof( vbuf_ ) ) {
+                       /* 
+                        * C99: snprintf returns the required size 
+                        */
+                       vbuf = ch_malloc( len + 1 );
+                       len = snprintf( vbuf, len,
+                                       "(.*)%s\\)(.*)", nvnc->bv_val );
+                       assert( len > 0 );
+               }
+
+               len = snprintf( rbuf, sizeof( rbuf_ ), "%%1%s)%%2", 
+                               nrnc->bv_val );
+               if ( len == -1 ) {
+                       return -1;
+
+               } else if ( len >= sizeof( rbuf_ ) ) {
+                       rbuf = ch_malloc( len + 1 );
+                       len = snprintf( rbuf, sizeof( rbuf_ ), "%%1%s)%%2", 
+                                       nrnc->bv_val );
+                       assert( len > 0 );
+               }
+               
+               rargv[ 0 ] = "rewriteRule";
+               rargv[ 1 ] = vbuf;
+               rargv[ 2 ] = rbuf;
+               rargv[ 3 ] = ":";
+               rargv[ 4 ] = NULL;
+               rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
+
+               if ( vbuf != vbuf_ ) {
+                       ch_free( vbuf );
+               }
+
+               if ( rbuf != rbuf_ ) {
+                       ch_free( rbuf );
+               }
+       }
+#endif /* rewrite filters */
+
+#if 0 /*  "matched" is not normalized */
        rargv[ 0 ] = "rewriteContext";
        rargv[ 1 ] = "matchedDn";
        rargv[ 2 ] = "alias";
        rargv[ 3 ] = "searchResult";
        rargv[ 4 ] = NULL;
-       rewrite_parse( info, "<suffix massage>", 7, 4, rargv );
-#else /* normalize matched */
+       rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
+#else /* normalize "matched" */
        rargv[ 0 ] = "rewriteContext";
        rargv[ 1 ] = "matchedDn";
        rargv[ 2 ] = NULL;
-       rewrite_parse( info, "<suffix massage>", 7, 2, rargv );
+       rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
 
        rargv[ 0 ] = "rewriteRule";
-       rargv[ 1 ] = suffix_massage_regexize( argv[ 2 ] );
-       rargv[ 2 ] = suffix_massage_patternize( argv[ 1 ], 1 );
+       rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
+       rargv[ 2 ] = suffix_massage_patternize( nvnc->bv_val );
        rargv[ 3 ] = ":";
        rargv[ 4 ] = NULL;
-       rewrite_parse( info, "<suffix massage>", 8, 4, rargv );
+       rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
        ch_free( rargv[ 1 ] );
        ch_free( rargv[ 2 ] );
-#endif /* normalize matched */
+#endif /* normalize "matched" */
 
        return 0;
 }
 #endif /* ENABLE_REWRITE */
-