2003-12-09 01:41:40 +08:00
|
|
|
/* $OpenLDAP$ */
|
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
2001-05-12 08:51:28 +08:00
|
|
|
*
|
2019-01-15 02:46:16 +08:00
|
|
|
* Copyright 2000-2019 The OpenLDAP Foundation.
|
2001-05-12 08:51:28 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2003-12-09 01:41:40 +08:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
2001-05-12 08:51:28 +08:00
|
|
|
*
|
2003-12-09 01:41:40 +08:00
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
|
|
|
*/
|
|
|
|
/* ACKNOWLEDGEMENT:
|
|
|
|
* This work was initially developed by Pierangelo Masarati for
|
|
|
|
* inclusion in OpenLDAP Software.
|
|
|
|
*/
|
2001-05-12 08:51:28 +08:00
|
|
|
|
|
|
|
#include <portable.h>
|
|
|
|
|
|
|
|
#include "rewrite-int.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Global data
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This becomes the running context for subsequent calls to
|
|
|
|
* rewrite_parse; it can be altered only by a
|
|
|
|
* rewriteContext config line or by a change in info.
|
|
|
|
*/
|
2003-11-15 02:39:18 +08:00
|
|
|
struct rewrite_context *rewrite_int_curr_context = NULL;
|
2001-05-12 08:51:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Inits the info
|
|
|
|
*/
|
|
|
|
struct rewrite_info *
|
|
|
|
rewrite_info_init(
|
|
|
|
int mode
|
|
|
|
)
|
|
|
|
{
|
|
|
|
struct rewrite_info *info;
|
|
|
|
struct rewrite_context *context;
|
|
|
|
|
|
|
|
switch ( mode ) {
|
|
|
|
case REWRITE_MODE_ERR:
|
|
|
|
case REWRITE_MODE_OK:
|
|
|
|
case REWRITE_MODE_COPY_INPUT:
|
|
|
|
case REWRITE_MODE_USE_DEFAULT:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mode = REWRITE_MODE_USE_DEFAULT;
|
|
|
|
break;
|
|
|
|
/* return NULL */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Resets the running context for parsing ...
|
|
|
|
*/
|
2003-11-15 02:39:18 +08:00
|
|
|
rewrite_int_curr_context = NULL;
|
2001-05-12 08:51:28 +08:00
|
|
|
|
|
|
|
info = calloc( sizeof( struct rewrite_info ), 1 );
|
|
|
|
if ( info == NULL ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
info->li_state = REWRITE_DEFAULT;
|
|
|
|
info->li_max_passes = REWRITE_MAX_PASSES;
|
2003-12-30 02:02:49 +08:00
|
|
|
info->li_max_passes_per_rule = REWRITE_MAX_PASSES;
|
2001-05-12 08:51:28 +08:00
|
|
|
info->li_rewrite_mode = mode;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add the default (empty) rule
|
|
|
|
*/
|
|
|
|
context = rewrite_context_create( info, REWRITE_DEFAULT_CONTEXT );
|
|
|
|
if ( context == NULL ) {
|
|
|
|
free( info );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_REWRITE_LDAP_PVT_THREADS
|
|
|
|
if ( ldap_pvt_thread_rdwr_init( &info->li_cookies_mutex ) ) {
|
2005-11-03 18:52:58 +08:00
|
|
|
avl_free( info->li_context, rewrite_context_free );
|
2001-05-12 08:51:28 +08:00
|
|
|
free( info );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ( ldap_pvt_thread_rdwr_init( &info->li_params_mutex ) ) {
|
2005-11-03 18:52:58 +08:00
|
|
|
ldap_pvt_thread_rdwr_destroy( &info->li_cookies_mutex );
|
|
|
|
avl_free( info->li_context, rewrite_context_free );
|
2001-05-12 08:51:28 +08:00
|
|
|
free( info );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif /* USE_REWRITE_LDAP_PVT_THREADS */
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cleans up the info structure
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
rewrite_info_delete(
|
2003-11-15 02:39:18 +08:00
|
|
|
struct rewrite_info **pinfo
|
2001-05-12 08:51:28 +08:00
|
|
|
)
|
|
|
|
{
|
2003-11-15 02:39:18 +08:00
|
|
|
struct rewrite_info *info;
|
|
|
|
|
|
|
|
assert( pinfo != NULL );
|
|
|
|
assert( *pinfo != NULL );
|
|
|
|
|
|
|
|
info = *pinfo;
|
2001-05-12 08:51:28 +08:00
|
|
|
|
2003-11-15 02:39:18 +08:00
|
|
|
if ( info->li_context ) {
|
|
|
|
avl_free( info->li_context, rewrite_context_free );
|
|
|
|
}
|
|
|
|
info->li_context = NULL;
|
|
|
|
|
2003-11-15 04:00:20 +08:00
|
|
|
if ( info->li_maps ) {
|
|
|
|
avl_free( info->li_maps, rewrite_builtin_map_free );
|
|
|
|
}
|
2005-11-03 18:52:58 +08:00
|
|
|
info->li_maps = NULL;
|
2003-11-15 04:00:20 +08:00
|
|
|
|
2001-05-12 08:51:28 +08:00
|
|
|
rewrite_session_destroy( info );
|
|
|
|
|
2003-11-15 02:39:18 +08:00
|
|
|
#ifdef USE_REWRITE_LDAP_PVT_THREADS
|
|
|
|
ldap_pvt_thread_rdwr_destroy( &info->li_cookies_mutex );
|
|
|
|
#endif /* USE_REWRITE_LDAP_PVT_THREADS */
|
|
|
|
|
2001-05-12 08:51:28 +08:00
|
|
|
rewrite_param_destroy( info );
|
|
|
|
|
|
|
|
#ifdef USE_REWRITE_LDAP_PVT_THREADS
|
|
|
|
ldap_pvt_thread_rdwr_destroy( &info->li_params_mutex );
|
|
|
|
#endif /* USE_REWRITE_LDAP_PVT_THREADS */
|
|
|
|
|
2003-11-15 02:39:18 +08:00
|
|
|
free( info );
|
|
|
|
*pinfo = NULL;
|
|
|
|
|
2001-05-12 08:51:28 +08:00
|
|
|
return REWRITE_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Rewrites a string according to context.
|
|
|
|
* If the engine is off, OK is returned, but the return string will be NULL.
|
|
|
|
* In case of 'unwilling to perform', UNWILLING is returned, and the
|
|
|
|
* return string will also be null. The same in case of error.
|
|
|
|
* Otherwise, OK is returned, and result will hold a newly allocated string
|
|
|
|
* with the rewriting.
|
|
|
|
*
|
|
|
|
* What to do in case of non-existing rewrite context is still an issue.
|
|
|
|
* Four possibilities:
|
|
|
|
* - error,
|
|
|
|
* - ok with NULL result,
|
|
|
|
* - ok with copy of string as result,
|
|
|
|
* - use the default rewrite context.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
rewrite(
|
|
|
|
struct rewrite_info *info,
|
|
|
|
const char *rewriteContext,
|
|
|
|
const char *string,
|
|
|
|
char **result
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return rewrite_session( info, rewriteContext,
|
|
|
|
string, NULL, result );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rewrite_session(
|
|
|
|
struct rewrite_info *info,
|
|
|
|
const char *rewriteContext,
|
|
|
|
const char *string,
|
|
|
|
const void *cookie,
|
|
|
|
char **result
|
|
|
|
)
|
|
|
|
{
|
|
|
|
struct rewrite_context *context;
|
2003-11-15 04:00:20 +08:00
|
|
|
struct rewrite_op op = { 0, 0, NULL, NULL, NULL };
|
2001-05-12 08:51:28 +08:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
assert( info != NULL );
|
|
|
|
assert( rewriteContext != NULL );
|
|
|
|
assert( string != NULL );
|
|
|
|
assert( result != NULL );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* cookie can be null; means: don't care about session stuff
|
|
|
|
*/
|
|
|
|
|
|
|
|
*result = NULL;
|
|
|
|
op.lo_cookie = cookie;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Engine not on means no failure, but explicit no rewriting
|
|
|
|
*/
|
|
|
|
if ( info->li_state != REWRITE_ON ) {
|
|
|
|
rc = REWRITE_REGEXEC_OK;
|
|
|
|
goto rc_return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Undefined context means no rewriting also
|
|
|
|
* (conservative, are we sure it's what we want?)
|
|
|
|
*/
|
|
|
|
context = rewrite_context_find( info, rewriteContext );
|
|
|
|
if ( context == NULL ) {
|
|
|
|
switch ( info->li_rewrite_mode ) {
|
|
|
|
case REWRITE_MODE_ERR:
|
|
|
|
rc = REWRITE_REGEXEC_ERR;
|
|
|
|
goto rc_return;
|
2003-11-15 18:02:44 +08:00
|
|
|
|
2001-05-12 08:51:28 +08:00
|
|
|
case REWRITE_MODE_OK:
|
|
|
|
rc = REWRITE_REGEXEC_OK;
|
|
|
|
goto rc_return;
|
2003-11-15 18:02:44 +08:00
|
|
|
|
2001-05-12 08:51:28 +08:00
|
|
|
case REWRITE_MODE_COPY_INPUT:
|
|
|
|
*result = strdup( string );
|
2008-09-10 03:58:47 +08:00
|
|
|
rc = ( *result != NULL ) ? REWRITE_REGEXEC_OK : REWRITE_REGEXEC_ERR;
|
2001-05-12 08:51:28 +08:00
|
|
|
goto rc_return;
|
2003-11-15 18:02:44 +08:00
|
|
|
|
2001-05-12 08:51:28 +08:00
|
|
|
case REWRITE_MODE_USE_DEFAULT:
|
|
|
|
context = rewrite_context_find( info,
|
|
|
|
REWRITE_DEFAULT_CONTEXT );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-11-15 04:00:20 +08:00
|
|
|
|
|
|
|
#if 0 /* FIXME: not used anywhere! (debug? then, why strdup?) */
|
2001-05-12 08:51:28 +08:00
|
|
|
op.lo_string = strdup( string );
|
|
|
|
if ( op.lo_string == NULL ) {
|
|
|
|
rc = REWRITE_REGEXEC_ERR;
|
|
|
|
goto rc_return;
|
|
|
|
}
|
2003-11-15 04:00:20 +08:00
|
|
|
#endif
|
2001-05-12 08:51:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Applies rewrite context
|
|
|
|
*/
|
2003-11-15 04:00:20 +08:00
|
|
|
rc = rewrite_context_apply( info, &op, context, string, result );
|
2001-05-12 08:51:28 +08:00
|
|
|
assert( op.lo_depth == 0 );
|
|
|
|
|
2003-11-15 04:00:20 +08:00
|
|
|
#if 0 /* FIXME: not used anywhere! (debug? then, why strdup?) */
|
2001-05-12 08:51:28 +08:00
|
|
|
free( op.lo_string );
|
2003-11-15 04:00:20 +08:00
|
|
|
#endif
|
2001-05-12 08:51:28 +08:00
|
|
|
|
|
|
|
switch ( rc ) {
|
|
|
|
/*
|
|
|
|
* Success
|
|
|
|
*/
|
|
|
|
case REWRITE_REGEXEC_OK:
|
|
|
|
case REWRITE_REGEXEC_STOP:
|
|
|
|
/*
|
|
|
|
* If rewrite succeeded return OK regardless of how
|
|
|
|
* the successful rewriting was obtained!
|
|
|
|
*/
|
|
|
|
rc = REWRITE_REGEXEC_OK;
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Internal or forced error, return = NULL; rc already OK.
|
|
|
|
*/
|
|
|
|
case REWRITE_REGEXEC_UNWILLING:
|
|
|
|
case REWRITE_REGEXEC_ERR:
|
|
|
|
if ( *result != NULL ) {
|
2005-01-02 00:10:25 +08:00
|
|
|
if ( *result != string ) {
|
|
|
|
free( *result );
|
|
|
|
}
|
2001-05-12 08:51:28 +08:00
|
|
|
*result = NULL;
|
|
|
|
}
|
2003-12-30 01:06:43 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
|
|
|
|
2001-06-03 16:52:22 +08:00
|
|
|
rc_return:;
|
2001-05-12 08:51:28 +08:00
|
|
|
if ( op.lo_vars ) {
|
|
|
|
rewrite_var_delete( op.lo_vars );
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|