static int slap_authz_regexp( struct berval *in, struct berval *out,
		int flags, void *ctx )
{
#ifdef SLAP_AUTH_REWRITE
	const char	*context = AUTHID_CONTEXT;

	if ( sasl_rwinfo == NULL || BER_BVISNULL( in ) ) {
		return 0;
	}

	/* FIXME: if aware of authc/authz mapping, 
	 * we could use different contexts ... */
	switch ( rewrite_session( sasl_rwinfo, context, in->bv_val, NULL, 
				&out->bv_val ) )
	{
	case REWRITE_REGEXEC_OK:
		if ( !BER_BVISNULL( out ) ) {
			char *val = out->bv_val;
			ber_str2bv_x( val, 0, 1, out, ctx );
			if ( val != in->bv_val ) {
				free( val );
			}
		} else {
			ber_dupbv_x( out, in, ctx );
		}
		Debug( LDAP_DEBUG_ARGS,
			"[rw] %s: \"%s\" -> \"%s\"\n",
			context, in->bv_val, out->bv_val );		
		return 1;
 		
 	case REWRITE_REGEXEC_UNWILLING:
	case REWRITE_REGEXEC_ERR:
	default:
		return 0;
	}

#else /* ! SLAP_AUTH_REWRITE */
	char *saslname = in->bv_val;
	SaslRegexp_t *reg;
  	regmatch_t sr_strings[SASLREGEX_REPLACE];	/* strings matching $1,$2 ... */
	int i;

	memset( out, 0, sizeof( *out ) );

	Debug( LDAP_DEBUG_TRACE, "slap_authz_regexp: converting SASL name %s\n",
	   saslname, 0, 0 );

	if (( saslname == NULL ) || ( nSaslRegexp == 0 )) {
		return( 0 );
	}

	/* Match the normalized SASL name to the saslregexp patterns */
	for( reg = SaslRegexp,i=0;  i<nSaslRegexp;  i++,reg++ ) {
		if ( regexec( &reg->sr_workspace, saslname, SASLREGEX_REPLACE,
		  sr_strings, 0)  == 0 )
			break;
	}

	if( i >= nSaslRegexp ) return( 0 );

	/*
	 * The match pattern may have been of the form "a(b.*)c(d.*)e" and the
	 * replace pattern of the form "x$1y$2z". The returned string needs
	 * to replace the $1,$2 with the strings that matched (b.*) and (d.*)
	 */
	slap_sasl_rx_exp( reg->sr_replace, reg->sr_offset,
		sr_strings, saslname, out, ctx );

	Debug( LDAP_DEBUG_TRACE,
		"slap_authz_regexp: converted SASL name to %s\n",
		BER_BVISEMPTY( out ) ? "" : out->bv_val, 0, 0 );

	return( 1 );
#endif /* ! SLAP_AUTH_REWRITE */
}
static int slap_authz_regexp( struct berval *in, struct berval *out,
		int flags, void *ctx )
{
#ifdef SLAP_AUTH_REWRITE
	const char	*context = AUTHID_CONTEXT;

	if ( sasl_rwinfo == NULL || BER_BVISNULL( in ) ) {
		return 0;
	}

	 
	switch ( rewrite_session( sasl_rwinfo, context, in->bv_val, NULL, 
				&out->bv_val ) )
	{
	case REWRITE_REGEXEC_OK:
		if ( !BER_BVISNULL( out ) ) {
			char *val = out->bv_val;
			ber_str2bv_x( val, 0, 1, out, ctx );
			if ( val != in->bv_val ) {
				free( val );
			}
		} else {
			ber_dupbv_x( out, in, ctx );
		}
		Debug( LDAP_DEBUG_ARGS,
			"[rw] %s: \"%s\" -> \"%s\"\n",
			context, in->bv_val, out->bv_val );		
		return 1;
 		
 	case REWRITE_REGEXEC_UNWILLING:
	case REWRITE_REGEXEC_ERR:
	default:
		return 0;
	}

#else  
	char *saslname = in->bv_val;
	SaslRegexp_t *reg;
  	regmatch_t sr_strings[SASLREGEX_REPLACE];	 
	int i;

	memset( out, 0, sizeof( *out ) );

	Debug( LDAP_DEBUG_TRACE, "slap_authz_regexp: converting SASL name %s\n",
	   saslname, 0, 0 );

	if (( saslname == NULL ) || ( nSaslRegexp == 0 )) {
		return( 0 );
	}

	 
	for( reg = SaslRegexp,i=0;  i<nSaslRegexp;  i++,reg++ ) {
		if ( regexec( &reg->sr_workspace, saslname, SASLREGEX_REPLACE,
		  sr_strings, 0)  == 0 )
			break;
	}

	if( i >= nSaslRegexp ) return( 0 );

	 
	slap_sasl_rx_exp( reg->sr_replace, reg->sr_offset,
		sr_strings, saslname, out, ctx );

	Debug( LDAP_DEBUG_TRACE,
		"slap_authz_regexp: converted SASL name to %s\n",
		BER_BVISEMPTY( out ) ? "" : out->bv_val, 0, 0 );

	return( 1 );
#endif  
}