From 6feec86e5d7e2e74b5f5e5e8ab5c0f257c5e073a Mon Sep 17 00:00:00 2001 From: Pierangelo Masarati Date: Wed, 1 May 2002 11:41:57 +0000 Subject: [PATCH] - trim check for real naming context already defined as suffix - improve automatic massaging (prettify once) - add (optimistic) automatic filter massaging - cleanup of massaging stuff --- servers/slapd/back-ldap/back-ldap.h | 4 +- servers/slapd/back-ldap/config.c | 168 ++++++++++++++++------------ servers/slapd/back-meta/config.c | 33 +++--- 3 files changed, 118 insertions(+), 87 deletions(-) diff --git a/servers/slapd/back-ldap/back-ldap.h b/servers/slapd/back-ldap/back-ldap.h index e68c3d138b..81f8b0c871 100644 --- a/servers/slapd/back-ldap/back-ldap.h +++ b/servers/slapd/back-ldap/back-ldap.h @@ -123,7 +123,9 @@ ldap_back_map_attrs( extern void mapping_free ( struct ldapmapping *mapping ); #ifdef ENABLE_REWRITE -extern int suffix_massage_config( struct rewrite_info *info, int argc, char **argv ); +extern int suffix_massage_config( struct rewrite_info *info, + struct berval *pvnc, struct berval *nvnc, + struct berval *prnc, struct berval *nrnc); extern int ldap_dnattr_rewrite( struct rewrite_info *rwinfo, BerVarray a_vals, void *cookie ); #endif /* ENABLE_REWRITE */ diff --git a/servers/slapd/back-ldap/config.c b/servers/slapd/back-ldap/config.c index f6d2f3832b..3343dc594a 100644 --- a/servers/slapd/back-ldap/config.c +++ b/servers/slapd/back-ldap/config.c @@ -123,11 +123,10 @@ ldap_back_db_config( /* dn massaging */ } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) { -#ifndef ENABLE_REWRITE - struct berval *bd2, *nd2; -#endif /* ENABLE_REWRITE */ BackendDB *tmp_be; - struct berval bdn, ndn; + struct berval bvnc, *nvnc = NULL, *pvnc = NULL, + brnc, *nrnc = NULL, *prnc = NULL; + int rc; /* * syntax: @@ -148,41 +147,52 @@ ldap_back_db_config( return( 1 ); } - bdn.bv_val = argv[1]; - bdn.bv_len = strlen(bdn.bv_val); - if ( dnNormalize2( NULL, &bdn, &ndn ) != LDAP_SUCCESS ) { + ber_str2bv( argv[1], 0, 0, &bvnc ); + pvnc = (struct berval *)ber_memalloc( sizeof( struct berval ) ); + nvnc = (struct berval *)ber_memalloc( sizeof( struct berval ) ); + 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 ); - ch_free( ndn.bv_val ); + 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 " " \"\n", fname, lineno ); - return( 1 ); + ber_bvfree( nvnc ); + ber_bvfree( pvnc ); + return( 1 ); } - bdn.bv_val = argv[2]; - bdn.bv_len = strlen(bdn.bv_val); - if ( dnNormalize2( NULL, &bdn, &ndn ) != LDAP_SUCCESS ) { + ber_str2bv( argv[2], 0, 0, &brnc ); + prnc = (struct berval *)ber_memalloc( sizeof( struct berval ) ); + nrnc = (struct berval *)ber_memalloc( sizeof( struct berval ) ); + 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 ); + ber_bvfree( nvnc ); + ber_bvfree( pvnc ); return( 1 ); } - tmp_be = select_backend( &ndn, 0, 0 ); - ch_free( ndn.bv_val ); + +#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 " " \"\n", fname, lineno ); + ber_bvfree( nvnc ); + ber_bvfree( pvnc ); + ber_bvfree( nrnc ); + ber_bvfree( prnc ); return( 1 ); } +#endif #ifdef ENABLE_REWRITE /* @@ -191,26 +201,32 @@ 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 ); + + ber_bvfree( nvnc ); + ber_bvfree( pvnc ); + ber_bvfree( nrnc ); + ber_bvfree( prnc ); + + return( rc ); + #else /* !ENABLE_REWRITE */ - bd2 = ber_bvstrdup( argv[1] ); - ber_bvecadd( &li->suffix_massage, bd2 ); - nd2 = NULL; - dnNormalize( NULL, bd2, &nd2 ); - ber_bvecadd( &li->suffix_massage, nd2 ); + ber_bvecadd( &li->suffix_massage, pvnc ); + ber_bvecadd( &li->suffix_massage, nvnc ); - bd2 = ber_bvstrdup( argv[2] ); - ber_bvecadd( &li->suffix_massage, bd2 ); - nd2 = NULL; - dnNormalize( NULL, bd2, &nd2 ); - ber_bvecadd( &li->suffix_massage, nd2 ); + ber_bvecadd( &li->suffix_massage, prnc ); + ber_bvecadd( &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 ) { @@ -341,35 +357,20 @@ suffix_massage_regexize( const char *s ) } static char * -suffix_massage_patternize( const char *s, int normalize ) +suffix_massage_patternize( const char *s ) { - struct berval dn = { 0, NULL }, odn = { 0, NULL }; - int rc; + ber_len_t len; char *res; - dn.bv_val = ( char * )s; - dn.bv_len = strlen( s ); + len = strlen( s ); - if ( normalize ) { - rc = dnNormalize2( NULL, &dn, &odn ); - } else { - rc = dnPretty2( NULL, &dn, &odn ); - } - - if ( rc != LDAP_SUCCESS ) { - return NULL; - } - - res = ch_calloc( sizeof( char ), odn.bv_len + sizeof( "%1" ) ); + res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) ); if ( res == NULL ) { return NULL; } strcpy( res, "%1" ); - strcpy( res + sizeof( "%1" ) - 1, odn.bv_val ); - - /* FIXME: what FREE should I use? */ - free( odn.bv_val ); + strcpy( res + sizeof( "%1" ) - 1, s ); return res; } @@ -377,42 +378,45 @@ suffix_massage_patternize( const char *s, int normalize ) 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, "", 1, 2, rargv ); + rewrite_parse( info, "", ++line, 2, rargv ); rargv[ 0 ] = "rewriteContext"; rargv[ 1 ] = "default"; rargv[ 2 ] = NULL; - rewrite_parse( info, "", 2, 2, rargv ); + rewrite_parse( info, "", ++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, "", 3, 4, rargv ); + rewrite_parse( info, "", ++line, 4, rargv ); ch_free( rargv[ 1 ] ); ch_free( rargv[ 2 ] ); rargv[ 0 ] = "rewriteContext"; rargv[ 1 ] = "searchResult"; rargv[ 2 ] = NULL; - rewrite_parse( info, "", 4, 2, rargv ); + rewrite_parse( info, "", ++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, "", 5, 4, rargv ); + rewrite_parse( info, "", ++line, 4, rargv ); ch_free( rargv[ 1 ] ); ch_free( rargv[ 2 ] ); @@ -429,30 +433,54 @@ suffix_massage_config( rargv[ 0 ] = "rewriteContext"; rargv[ 1 ] = "searchFilter"; rargv[ 2 ] = NULL; - rewrite_parse( info, "", 6, 2, rargv ); + rewrite_parse( info, "", ++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[LDAP_FILT_MAXSIZ], rbuf[LDAP_FILT_MAXSIZ]; + + snprintf( vbuf, sizeof( vbuf ), "(.*)%s\\)(.*)", nvnc->bv_val ); + snprintf( rbuf, sizeof( rbuf ), "%%1%s)%%2", nrnc->bv_val ); + + rargv[ 0 ] = "rewriteRule"; + rargv[ 1 ] = vbuf; + rargv[ 2 ] = rbuf; + rargv[ 3 ] = ":"; + rargv[ 4 ] = NULL; + rewrite_parse( info, "", ++line, 4, rargv ); + } +#endif /* rewrite filters */ -#if 0 /* matched is not normalized */ +#if 0 /* "matched" is not normalized */ rargv[ 0 ] = "rewriteContext"; rargv[ 1 ] = "matchedDn"; rargv[ 2 ] = "alias"; rargv[ 3 ] = "searchResult"; rargv[ 4 ] = NULL; - rewrite_parse( info, "", 7, 4, rargv ); -#else /* normalize matched */ + rewrite_parse( info, "", ++line, 4, rargv ); +#else /* normalize "matched" */ rargv[ 0 ] = "rewriteContext"; rargv[ 1 ] = "matchedDn"; rargv[ 2 ] = NULL; - rewrite_parse( info, "", 7, 2, rargv ); + rewrite_parse( info, "", ++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, "", 8, 4, rargv ); + rewrite_parse( info, "", ++line, 4, rargv ); ch_free( rargv[ 1 ] ); ch_free( rargv[ 2 ] ); -#endif /* normalize matched */ +#endif /* normalize "matched" */ return 0; } diff --git a/servers/slapd/back-meta/config.c b/servers/slapd/back-meta/config.c index 9c00cdc1a3..7b8f7cf6e5 100644 --- a/servers/slapd/back-meta/config.c +++ b/servers/slapd/back-meta/config.c @@ -75,13 +75,6 @@ #include "../back-ldap/back-ldap.h" #include "back-meta.h" -extern int -suffix_massage_config( - struct rewrite_info *info, - int argc, - char **argv -); - static struct metatarget * new_target( void ) { @@ -399,7 +392,7 @@ meta_back_db_config( } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) { BackendDB *tmp_be; int i = li->ntargets-1; - struct berval dn, ndn; + struct berval dn, nvnc, pvnc, nrnc, prnc; if ( i < 0 ) { fprintf( stderr, @@ -428,41 +421,49 @@ meta_back_db_config( dn.bv_val = argv[ 1 ]; dn.bv_len = strlen( argv[ 1 ] ); - if ( dnNormalize2( NULL, &dn, &ndn ) != LDAP_SUCCESS ) { + if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc ) != LDAP_SUCCESS ) { fprintf( stderr, "%s: line %d: " "suffix '%s' is invalid\n", fname, lineno, argv[ 1 ] ); return 1; } - tmp_be = select_backend( &ndn, 0, 0 ); - free( ndn.bv_val ); + 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 \"\n", fname, lineno ); + free( pvnc.bv_val ); + free( nvnc.bv_val ); return 1; } dn.bv_val = argv[ 2 ]; dn.bv_len = strlen( argv[ 2 ] ); - if ( dnNormalize2( NULL, &dn, &ndn ) != LDAP_SUCCESS ) { + if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc ) != LDAP_SUCCESS ) { fprintf( stderr, "%s: line %d: " "massaged suffix '%s' is invalid\n", fname, lineno, argv[ 2 ] ); + free( pvnc.bv_val ); + free( nvnc.bv_val ); return 1; } - - tmp_be = select_backend( &ndn, 0, 0 ); - free( ndn.bv_val ); + +#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 \"\n", fname, lineno ); + free( pvnc.bv_val ); + free( nvnc.bv_val ); + free( prnc.bv_val ); + free( nrnc.bv_val ); return 1; } +#endif /* * The suffix massaging is emulated by means of the @@ -471,7 +472,7 @@ meta_back_db_config( * to the database */ return suffix_massage_config( li->targets[ i ]->rwinfo, - argc, argv ); + &pvnc, &nvnc, &prnc, &nrnc ); /* rewrite stuff ... */ } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) { -- 2.39.5