1/* $OpenLDAP$ */
2/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 *
4 * Copyright 2000-2011 The OpenLDAP Foundation.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
9 * Public License.
10 *
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>.
14 */
15/* ACKNOWLEDGEMENT:
16 * This work was initially developed by Pierangelo Masarati for
17 * inclusion in OpenLDAP Software.
18 */
19
20#include <portable.h>
21
22#include <ac/stdlib.h>
23#include <ac/string.h>
24#include <ac/syslog.h>
25#include <ac/regex.h>
26#include <ac/socket.h>
27#include <ac/unistd.h>
28#include <ac/ctype.h>
29#include <ac/string.h>
30#include <stdio.h>
31
32#include <rewrite.h>
33#include <lutil.h>
34#include <ldap.h>
35
36int ldap_debug;
37int ldap_syslog;
38int ldap_syslog_level;
39
40static void
41apply(
42		FILE *fin,
43		const char *rewriteContext,
44		const char *arg
45)
46{
47	struct rewrite_info *info;
48	char *string, *sep, *result = NULL;
49	int rc;
50	void *cookie = &info;
51
52	info = rewrite_info_init( REWRITE_MODE_ERR );
53
54	if ( rewrite_read( fin, info ) != 0 ) {
55		exit( EXIT_FAILURE );
56	}
57
58	rewrite_param_set( info, "prog", "rewrite" );
59
60	rewrite_session_init( info, cookie );
61
62	string = (char *)arg;
63	for ( sep = strchr( rewriteContext, ',' );
64			rewriteContext != NULL;
65			rewriteContext = sep,
66			sep ? sep = strchr( rewriteContext, ',' ) : NULL )
67	{
68		char	*errmsg = "";
69
70		if ( sep != NULL ) {
71			sep[ 0 ] = '\0';
72			sep++;
73		}
74		/* rc = rewrite( info, rewriteContext, string, &result ); */
75		rc = rewrite_session( info, rewriteContext, string,
76				cookie, &result );
77
78		switch ( rc ) {
79		case REWRITE_REGEXEC_OK:
80			errmsg = "ok";
81			break;
82
83		case REWRITE_REGEXEC_ERR:
84			errmsg = "error";
85			break;
86
87		case REWRITE_REGEXEC_STOP:
88			errmsg = "stop";
89			break;
90
91		case REWRITE_REGEXEC_UNWILLING:
92			errmsg = "unwilling to perform";
93			break;
94
95		default:
96			if (rc >= REWRITE_REGEXEC_USER) {
97				errmsg = "user-defined";
98			} else {
99				errmsg = "unknown";
100			}
101			break;
102		}
103
104		fprintf( stdout, "%s -> %s [%d:%s]\n", string,
105				( result ? result : "(null)" ),
106				rc, errmsg );
107		if ( result == NULL ) {
108			break;
109		}
110		if ( string != arg && string != result ) {
111			free( string );
112		}
113		string = result;
114	}
115
116	if ( result && result != arg ) {
117		free( result );
118	}
119
120	rewrite_session_delete( info, cookie );
121
122	rewrite_info_delete( &info );
123}
124
125int
126main( int argc, char *argv[] )
127{
128	FILE	*fin = NULL;
129	char	*rewriteContext = REWRITE_DEFAULT_CONTEXT;
130	int	debug = 0;
131
132	while ( 1 ) {
133		int opt = getopt( argc, argv, "d:f:hr:" );
134
135		if ( opt == EOF ) {
136			break;
137		}
138
139		switch ( opt ) {
140		case 'd':
141			if ( lutil_atoi( &debug, optarg ) != 0 ) {
142				fprintf( stderr, "illegal log level '%s'\n",
143						optarg );
144				exit( EXIT_FAILURE );
145			}
146			break;
147
148		case 'f':
149			fin = fopen( optarg, "r" );
150			if ( fin == NULL ) {
151				fprintf( stderr, "unable to open file '%s'\n",
152						optarg );
153				exit( EXIT_FAILURE );
154			}
155			break;
156
157		case 'h':
158			fprintf( stderr,
159	"usage: rewrite [options] string\n"
160	"\n"
161	"\t\t-f file\t\tconfiguration file\n"
162	"\t\t-r rule[s]\tlist of comma-separated rules\n"
163	"\n"
164	"\tsyntax:\n"
165	"\t\trewriteEngine\t{on|off}\n"
166	"\t\trewriteContext\tcontextName [alias aliasedContextName]\n"
167	"\t\trewriteRule\tpattern subst [flags]\n"
168	"\n"
169				);
170			exit( EXIT_SUCCESS );
171
172		case 'r':
173			rewriteContext = optarg;
174			break;
175		}
176	}
177
178	if ( debug != 0 ) {
179		ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &debug);
180		ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug);
181	}
182
183	if ( optind >= argc ) {
184		return -1;
185	}
186
187	apply( ( fin ? fin : stdin ), rewriteContext, argv[ optind ] );
188
189	if ( fin ) {
190		fclose( fin );
191	}
192
193	return 0;
194}
195
196