2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2000-2012 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>
38 int ldap_syslog_level;
43 const char *rewriteContext,
47 struct rewrite_info *info;
48 char *string, *sep, *result = NULL;
52 info = rewrite_info_init( REWRITE_MODE_ERR );
54 if ( rewrite_read( fin, info ) != 0 ) {
58 rewrite_param_set( info, "prog", "rewrite" );
60 rewrite_session_init( info, cookie );
63 for ( sep = strchr( rewriteContext, ',' );
64 rewriteContext != NULL;
66 sep ? sep = strchr( rewriteContext, ',' ) : NULL )
74 /* rc = rewrite( info, rewriteContext, string, &result ); */
75 rc = rewrite_session( info, rewriteContext, string,
79 case REWRITE_REGEXEC_OK:
83 case REWRITE_REGEXEC_ERR:
87 case REWRITE_REGEXEC_STOP:
91 case REWRITE_REGEXEC_UNWILLING:
92 errmsg = "unwilling to perform";
96 if (rc >= REWRITE_REGEXEC_USER) {
97 errmsg = "user-defined";
104 fprintf( stdout, "%s -> %s [%d:%s]\n", string,
105 ( result ? result : "(null)" ),
107 if ( result == NULL ) {
110 if ( string != arg && string != result ) {
116 if ( result && result != arg ) {
120 rewrite_session_delete( info, cookie );
122 rewrite_info_delete( &info );
126 main( int argc, char *argv[] )
129 char *rewriteContext = REWRITE_DEFAULT_CONTEXT;
133 int opt = getopt( argc, argv, "d:f:hr:" );
141 if ( lutil_atoi( &debug, optarg ) != 0 ) {
142 fprintf( stderr, "illegal log level '%s'\n",
144 exit( EXIT_FAILURE );
149 fin = fopen( optarg, "r" );
151 fprintf( stderr, "unable to open file '%s'\n",
153 exit( EXIT_FAILURE );
159 "usage: rewrite [options] string\n"
161 "\t\t-f file\t\tconfiguration file\n"
162 "\t\t-r rule[s]\tlist of comma-separated rules\n"
165 "\t\trewriteEngine\t{on|off}\n"
166 "\t\trewriteContext\tcontextName [alias aliasedContextName]\n"
167 "\t\trewriteRule\tpattern subst [flags]\n"
170 exit( EXIT_SUCCESS );
173 rewriteContext = optarg;
179 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &debug);
180 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug);
183 if ( optind >= argc ) {
187 apply( ( fin ? fin : stdin ), rewriteContext, argv[ optind ] );