]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-ldap/config.c
Cleanup up LDAP_CLIENT_UPDATE code... including some bug fixing.
[openldap] / servers / slapd / back-ldap / config.c
index 5fb1f3a6c1ea63396e48dc3f79b9eeab91e510bb..42e44659bfd049f8a02f4155f16d5227871b39f9 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-2002 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,49 +112,119 @@ 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 ) {
-               char *dn, *massaged_dn;
                BackendDB *tmp_be;
+               struct berval bvnc, nvnc, pvnc, brnc, nrnc, prnc;
+#ifdef ENABLE_REWRITE
+               int rc;
+#endif /* ENABLE_REWRITE */
                
+               /*
+                * syntax:
+                * 
+                *      suffixmassage <suffix> <massaged suffix>
+                *
+                * the <suffix> field must be defined as a valid suffix
+                * (or suffixAlias?) for the current database;
+                * the <massaged suffix> shouldn't have already been
+                * defined as a valid suffix or suffixAlias for the 
+                * current server
+                */
                if ( argc != 3 ) {
-                       fprintf( stderr,
-       "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
+                       fprintf( stderr, "%s: line %d: syntax is"
+                                      " \"suffixMassage <suffix>"
+                                      " <massaged suffix>\"\n",
                                fname, lineno );
                        return( 1 );
                }
                
-               tmp_be = select_backend( argv[1], 0 );
+               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, bvnc.bv_val );
+                       return( 1 );
+               }
+               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",
+                       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 );
                }
 
-               tmp_be = select_backend( argv[2], 0 );
+               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, brnc.bv_val );
+                       free( nvnc.bv_val );
+                       free( pvnc.bv_val );
+                       return( 1 );
+               }
+
+#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",
+                       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
+               /*
+                * The suffix massaging is emulated by means of the
+                * rewrite capabilities
+                * FIXME: no extra rewrite capabilities should be added
+                * to the database
+                */
+               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 */
+               ber_bvarray_add( &li->suffix_massage, &pvnc );
+               ber_bvarray_add( &li->suffix_massage, &nvnc );
                
-               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 );
+               ber_bvarray_add( &li->suffix_massage, &prnc );
+               ber_bvarray_add( &li->suffix_massage, &nrnc );
+#endif /* !ENABLE_REWRITE */
+
+       /* rewrite stuff ... */
+       } else if ( strncasecmp( argv[0], "rewrite", 7 ) == 0 ) {
+#ifdef ENABLE_REWRITE
+               return rewrite_parse( li->rwinfo, fname, lineno, argc, argv );
+
+#else /* !ENABLE_REWRITE */
+               fprintf( stderr, "%s: line %d: rewrite capabilities "
+                               "are not enabled\n", fname, lineno );
+#endif /* !ENABLE_REWRITE */
                
-               free( dn );
-               free( massaged_dn );
-
        /* objectclass/attribute mapping */
        } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
                struct ldapmap *map;
@@ -162,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 );
                }
@@ -172,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;
+               if ( strcmp( argv[2], "*" ) == 0 ) {
+                       if ( argc < 4 || strcmp( argv[3], "*" ) == 0 ) {
+                               map->drop_missing = ( argc < 4 );
                                return 0;
                        }
-                       if ( strcasecmp( argv[3], "*" ) == 0 ) {
-                               map->drop_missing = 0;
-                               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 )
@@ -209,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",
@@ -235,173 +296,216 @@ 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 )
+#ifdef ENABLE_REWRITE
+static char *
+suffix_massage_regexize( const char *s )
 {
-       struct ldapmapping *map1 = (struct ldapmapping *)c1;
-       struct ldapmapping *map2 = (struct ldapmapping *)c2;
+       char *res, *ptr;
+       const char *p, *r;
+       int i;
+
+       for ( i = 0, p = s; 
+                       ( r = strchr( p, ',' ) ) != NULL; 
+                       p = r + 1, i++ )
+               ;
+
+       res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
+
+       ptr = lutil_strcopy( res, "(.*)" );
+       for ( i = 0, p = s;
+                       ( r = strchr( p, ',' ) ) != NULL;
+                       p = r + 1 , i++ ) {
+               ptr = lutil_strncopy( ptr, p, r - p + 1 );
+               ptr = lutil_strcopy( ptr, "[ ]?" );
+
+               if ( r[ 1 ] == ' ' ) {
+                       r++;
+               }
+       }
+       lutil_strcopy( ptr, p );
 
-       return ( strcasecmp(map1->src, map2->src) );
+       return res;
 }
 
-int
-mapping_dup ( void *c1, void *c2 )
+static char *
+suffix_massage_patternize( const char *s )
 {
-       struct ldapmapping *map1 = (struct ldapmapping *)c1;
-       struct ldapmapping *map2 = (struct ldapmapping *)c2;
+       ber_len_t       len;
+       char            *res;
 
-       return( ( strcasecmp(map1->src, map2->src) == 0 ) ? -1 : 0 );
-}
+       len = strlen( s );
 
-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);
+       res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) );
+       if ( res == NULL ) {
+               return NULL;
        }
 
-       if (map->drop_missing)
-               return(NULL);
+       strcpy( res, "%1" );
+       strcpy( res + sizeof( "%1" ) - 1, s );
 
-       return(s);
+       return res;
 }
 
-char *
-ldap_back_map_filter ( struct ldapinfo *li, char *f, int remap )
+int
+suffix_massage_config( 
+               struct rewrite_info *info,
+               struct berval *pvnc,
+               struct berval *nvnc,
+               struct berval *prnc,
+               struct berval *nrnc
+)
 {
-       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 ')'.
+       char *rargv[ 5 ];
+       int line = 0;
+
+       rargv[ 0 ] = "rewriteEngine";
+       rargv[ 1 ] = "on";
+       rargv[ 2 ] = NULL;
+       rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
+
+       rargv[ 0 ] = "rewriteContext";
+       rargv[ 1 ] = "default";
+       rargv[ 2 ] = NULL;
+       rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
+
+       rargv[ 0 ] = "rewriteRule";
+       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>", ++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>", ++line, 2, rargv );
+       
+       rargv[ 0 ] = "rewriteRule";
+       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>", ++line, 4, rargv );
+       ch_free( rargv[ 1 ] );
+       ch_free( rargv[ 2 ] );
+
+       /*
+        * the filter should be rewritten as
+        * 
+        * rewriteRule
+        *      "(.*)member=([^)]+),o=Foo Bar,[ ]?c=US(.*)"
+        *      "%1member=%2,dc=example,dc=com%3"
+        *
+        * where "o=Foo Bar, c=US" is the virtual naming context,
+        * and "dc=example, dc=com" is the real naming context
         */
+       rargv[ 0 ] = "rewriteContext";
+       rargv[ 1 ] = "searchFilter";
+       rargv[ 2 ] = NULL;
+       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;
 
-       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?
+               } else if ( len >= sizeof( vbuf_ ) ) {
+                       /* 
+                        * C99: snprintf returns the required size 
                         */
-                       *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(&li->at_map, q, remap);
-                               if (m == NULL)
-                                       m = ldap_back_map(&li->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;
+                       vbuf = ch_malloc( len + 1 );
+                       len = snprintf( vbuf, len,
+                                       "(.*)%s\\)(.*)", nvnc->bv_val );
+                       assert( len > 0 );
                }
-       }
-       *s = 0;
-       return(nf);
-}
 
-char **
-ldap_back_map_attrs ( struct ldapinfo *li, char **a, int remap )
-{
-       int i, j, count;
-       char **na, *mapped;
-
-       if (a == NULL)
-               return(NULL);
+               len = snprintf( rbuf, sizeof( rbuf_ ), "%%1%s)%%2", 
+                               nrnc->bv_val );
+               if ( len == -1 ) {
+                       return -1;
 
-       for (count = 0; a[count] != NULL; count++) {
-               /*  */
-       }
+               } 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 );
+               }
 
-       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(&li->at_map, a[i], remap);
-               if (mapped != NULL) {
-                       mapped = ch_strdup(mapped);
-                       if (mapped == NULL) {
-                               charray_free(na);
-                               return(NULL);
-                       }
-                       na[j] = mapped;
-                       j++;
+               if ( rbuf != rbuf_ ) {
+                       ch_free( rbuf );
                }
        }
-       return(na);
-}
+#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>", ++line, 4, rargv );
+#else /* normalize "matched" */
+       rargv[ 0 ] = "rewriteContext";
+       rargv[ 1 ] = "matchedDn";
+       rargv[ 2 ] = NULL;
+       rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
+
+       rargv[ 0 ] = "rewriteRule";
+       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>", ++line, 4, rargv );
+       ch_free( rargv[ 1 ] );
+       ch_free( rargv[ 2 ] );
+#endif /* normalize "matched" */
 
+       return 0;
+}
+#endif /* ENABLE_REWRITE */