2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2000-2005 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
16 * This work was initially developed by Pierangelo Masarati for
17 * inclusion in OpenLDAP Software.
22 #include <ac/stdlib.h>
23 #include <ac/string.h>
24 #include <ac/syslog.h>
26 #include <ac/socket.h>
27 #include <ac/unistd.h>
29 #include <ac/string.h>
37 int ldap_syslog_level;
42 const char *rewriteContext,
46 struct rewrite_info *info;
47 char *string, *sep, *result = NULL;
51 info = rewrite_info_init( REWRITE_MODE_ERR );
53 if ( rewrite_read( fin, info ) != 0 ) {
57 rewrite_param_set( info, "prog", "rewrite" );
59 rewrite_session_init( info, cookie );
62 for ( sep = strchr( rewriteContext, ',' );
63 rewriteContext != NULL;
65 sep ? sep = strchr( rewriteContext, ',' ) : NULL )
73 /* rc = rewrite( info, rewriteContext, string, &result ); */
74 rc = rewrite_session( info, rewriteContext, string,
78 case REWRITE_REGEXEC_OK:
82 case REWRITE_REGEXEC_ERR:
86 case REWRITE_REGEXEC_STOP:
90 case REWRITE_REGEXEC_UNWILLING:
91 errmsg = "unwilling to perform";
95 if (rc >= REWRITE_REGEXEC_USER) {
96 errmsg = "user-defined";
103 fprintf( stdout, "%s -> %s [%d:%s]\n", string,
104 ( result ? result : "(null)" ),
106 if ( result == NULL ) {
109 if ( string != arg && string != result ) {
115 if ( result && result != arg ) {
119 rewrite_session_delete( info, cookie );
121 rewrite_info_delete( &info );
125 main( int argc, char *argv[] )
128 char *rewriteContext = REWRITE_DEFAULT_CONTEXT;
133 int opt = getopt( argc, argv, "d:f:hr:" );
141 debug = strtol( optarg, &next, 10 );
142 if ( next == NULL || next[0] != '\0' ) {
143 fprintf( stderr, "illegal log level '%s'\n",
145 exit( EXIT_FAILURE );
150 fin = fopen( optarg, "r" );
152 fprintf( stderr, "unable to open file '%s'\n",
154 exit( EXIT_FAILURE );
160 "usage: rewrite [options] string\n"
162 "\t\t-f file\t\tconfiguration file\n"
163 "\t\t-r rule[s]\tlist of comma-separated rules\n"
166 "\t\trewriteEngine\t{on|off}\n"
167 "\t\trewriteContext\tcontextName [alias aliasedContextName]\n"
168 "\t\trewriteRule\tpattern subst [flags]\n"
171 exit( EXIT_SUCCESS );
174 rewriteContext = optarg;
180 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &debug);
181 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug);
184 if ( optind >= argc ) {
188 apply( ( fin ? fin : stdin ), rewriteContext, argv[ optind ] );