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
|
|
|
*
|
2020-01-10 00:50:21 +08:00
|
|
|
* Copyright 2000-2020 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 <ac/stdlib.h>
|
2001-06-24 00:06:34 +08:00
|
|
|
#include <ac/string.h>
|
2001-05-12 08:51:28 +08:00
|
|
|
#include <ac/syslog.h>
|
|
|
|
#include <ac/regex.h>
|
|
|
|
#include <ac/socket.h>
|
|
|
|
#include <ac/unistd.h>
|
|
|
|
#include <ac/ctype.h>
|
2001-06-23 09:20:32 +08:00
|
|
|
#include <ac/string.h>
|
2002-07-19 22:24:53 +08:00
|
|
|
#include <stdio.h>
|
2001-05-12 08:51:28 +08:00
|
|
|
|
|
|
|
#include <rewrite.h>
|
2005-11-24 09:10:05 +08:00
|
|
|
#include <lutil.h>
|
2004-06-21 06:40:32 +08:00
|
|
|
#include <ldap.h>
|
2001-05-12 08:51:28 +08:00
|
|
|
|
|
|
|
int ldap_debug;
|
|
|
|
int ldap_syslog;
|
|
|
|
int ldap_syslog_level;
|
|
|
|
|
2005-01-02 00:10:25 +08:00
|
|
|
static void
|
2001-05-12 08:51:28 +08:00
|
|
|
apply(
|
|
|
|
FILE *fin,
|
|
|
|
const char *rewriteContext,
|
|
|
|
const char *arg
|
|
|
|
)
|
|
|
|
{
|
|
|
|
struct rewrite_info *info;
|
|
|
|
char *string, *sep, *result = NULL;
|
|
|
|
int rc;
|
|
|
|
void *cookie = &info;
|
|
|
|
|
2003-11-15 02:39:18 +08:00
|
|
|
info = rewrite_info_init( REWRITE_MODE_ERR );
|
2001-05-12 08:51:28 +08:00
|
|
|
|
2002-01-03 07:31:44 +08:00
|
|
|
if ( rewrite_read( fin, info ) != 0 ) {
|
2001-05-12 08:51:28 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
rewrite_param_set( info, "prog", "rewrite" );
|
|
|
|
|
|
|
|
rewrite_session_init( info, cookie );
|
|
|
|
|
2005-01-02 00:10:25 +08:00
|
|
|
string = (char *)arg;
|
2001-05-12 08:51:28 +08:00
|
|
|
for ( sep = strchr( rewriteContext, ',' );
|
|
|
|
rewriteContext != NULL;
|
|
|
|
rewriteContext = sep,
|
2005-01-02 00:10:25 +08:00
|
|
|
sep ? sep = strchr( rewriteContext, ',' ) : NULL )
|
|
|
|
{
|
2003-12-30 00:14:47 +08:00
|
|
|
char *errmsg = "";
|
|
|
|
|
2001-05-12 08:51:28 +08:00
|
|
|
if ( sep != NULL ) {
|
|
|
|
sep[ 0 ] = '\0';
|
|
|
|
sep++;
|
|
|
|
}
|
2001-10-31 17:28:03 +08:00
|
|
|
/* rc = rewrite( info, rewriteContext, string, &result ); */
|
2001-05-12 08:51:28 +08:00
|
|
|
rc = rewrite_session( info, rewriteContext, string,
|
|
|
|
cookie, &result );
|
|
|
|
|
2003-12-30 00:14:47 +08:00
|
|
|
switch ( rc ) {
|
|
|
|
case REWRITE_REGEXEC_OK:
|
|
|
|
errmsg = "ok";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REWRITE_REGEXEC_ERR:
|
|
|
|
errmsg = "error";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REWRITE_REGEXEC_STOP:
|
|
|
|
errmsg = "stop";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REWRITE_REGEXEC_UNWILLING:
|
|
|
|
errmsg = "unwilling to perform";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (rc >= REWRITE_REGEXEC_USER) {
|
|
|
|
errmsg = "user-defined";
|
|
|
|
} else {
|
|
|
|
errmsg = "unknown";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf( stdout, "%s -> %s [%d:%s]\n", string,
|
|
|
|
( result ? result : "(null)" ),
|
|
|
|
rc, errmsg );
|
2001-05-12 08:51:28 +08:00
|
|
|
if ( result == NULL ) {
|
|
|
|
break;
|
|
|
|
}
|
2005-01-02 00:10:25 +08:00
|
|
|
if ( string != arg && string != result ) {
|
|
|
|
free( string );
|
|
|
|
}
|
2001-05-12 08:51:28 +08:00
|
|
|
string = result;
|
|
|
|
}
|
|
|
|
|
2005-01-02 00:10:25 +08:00
|
|
|
if ( result && result != arg ) {
|
|
|
|
free( result );
|
|
|
|
}
|
2003-11-15 02:39:18 +08:00
|
|
|
|
2001-05-12 08:51:28 +08:00
|
|
|
rewrite_session_delete( info, cookie );
|
|
|
|
|
2003-11-15 02:39:18 +08:00
|
|
|
rewrite_info_delete( &info );
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main( int argc, char *argv[] )
|
|
|
|
{
|
2004-06-21 06:40:32 +08:00
|
|
|
FILE *fin = NULL;
|
|
|
|
char *rewriteContext = REWRITE_DEFAULT_CONTEXT;
|
|
|
|
int debug = 0;
|
2001-05-12 08:51:28 +08:00
|
|
|
|
|
|
|
while ( 1 ) {
|
2004-06-21 06:40:32 +08:00
|
|
|
int opt = getopt( argc, argv, "d:f:hr:" );
|
2001-05-12 08:51:28 +08:00
|
|
|
|
|
|
|
if ( opt == EOF ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( opt ) {
|
2004-06-21 06:40:32 +08:00
|
|
|
case 'd':
|
2005-11-24 09:10:05 +08:00
|
|
|
if ( lutil_atoi( &debug, optarg ) != 0 ) {
|
2004-06-21 06:40:32 +08:00
|
|
|
fprintf( stderr, "illegal log level '%s'\n",
|
|
|
|
optarg );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2001-05-12 08:51:28 +08:00
|
|
|
case 'f':
|
|
|
|
fin = fopen( optarg, "r" );
|
|
|
|
if ( fin == NULL ) {
|
|
|
|
fprintf( stderr, "unable to open file '%s'\n",
|
|
|
|
optarg );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h':
|
|
|
|
fprintf( stderr,
|
|
|
|
"usage: rewrite [options] string\n"
|
|
|
|
"\n"
|
|
|
|
"\t\t-f file\t\tconfiguration file\n"
|
|
|
|
"\t\t-r rule[s]\tlist of comma-separated rules\n"
|
|
|
|
"\n"
|
|
|
|
"\tsyntax:\n"
|
|
|
|
"\t\trewriteEngine\t{on|off}\n"
|
|
|
|
"\t\trewriteContext\tcontextName [alias aliasedContextName]\n"
|
|
|
|
"\t\trewriteRule\tpattern subst [flags]\n"
|
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
exit( EXIT_SUCCESS );
|
|
|
|
|
|
|
|
case 'r':
|
2003-11-15 18:02:44 +08:00
|
|
|
rewriteContext = optarg;
|
2001-05-12 08:51:28 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-21 06:40:32 +08:00
|
|
|
if ( debug != 0 ) {
|
|
|
|
ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &debug);
|
|
|
|
ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug);
|
|
|
|
}
|
|
|
|
|
2001-05-12 08:51:28 +08:00
|
|
|
if ( optind >= argc ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
apply( ( fin ? fin : stdin ), rewriteContext, argv[ optind ] );
|
|
|
|
|
2003-11-15 02:39:18 +08:00
|
|
|
if ( fin ) {
|
|
|
|
fclose( fin );
|
|
|
|
}
|
|
|
|
|
2001-05-12 08:51:28 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|