2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2000-2004 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 );
61 string = strdup( arg );
62 for ( sep = strchr( rewriteContext, ',' );
63 rewriteContext != NULL;
65 sep ? sep = strchr( rewriteContext, ',' ) : NULL ) {
72 /* rc = rewrite( info, rewriteContext, string, &result ); */
73 rc = rewrite_session( info, rewriteContext, string,
77 case REWRITE_REGEXEC_OK:
81 case REWRITE_REGEXEC_ERR:
85 case REWRITE_REGEXEC_STOP:
89 case REWRITE_REGEXEC_UNWILLING:
90 errmsg = "unwilling to perform";
94 if (rc >= REWRITE_REGEXEC_USER) {
95 errmsg = "user-defined";
102 fprintf( stdout, "%s -> %s [%d:%s]\n", string,
103 ( result ? result : "(null)" ),
105 if ( result == NULL ) {
114 rewrite_session_delete( info, cookie );
116 rewrite_info_delete( &info );
122 main( int argc, char *argv[] )
125 char *rewriteContext = REWRITE_DEFAULT_CONTEXT;
130 int opt = getopt( argc, argv, "d:f:hr:" );
138 debug = strtol( optarg, &next, 10 );
139 if ( next == NULL || next[0] != '\0' ) {
140 fprintf( stderr, "illegal log level '%s'\n",
142 exit( EXIT_FAILURE );
147 fin = fopen( optarg, "r" );
149 fprintf( stderr, "unable to open file '%s'\n",
151 exit( EXIT_FAILURE );
157 "usage: rewrite [options] string\n"
159 "\t\t-f file\t\tconfiguration file\n"
160 "\t\t-r rule[s]\tlist of comma-separated rules\n"
163 "\t\trewriteEngine\t{on|off}\n"
164 "\t\trewriteContext\tcontextName [alias aliasedContextName]\n"
165 "\t\trewriteRule\tpattern subst [flags]\n"
168 exit( EXIT_SUCCESS );
171 rewriteContext = optarg;
177 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &debug);
178 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug);
181 if ( optind >= argc ) {
185 apply( ( fin ? fin : stdin ), rewriteContext, argv[ optind ] );