]> git.sur5r.net Git - openldap/blob - libraries/librewrite/rewrite.c
1964deba52004c567056f58ab35efb09da72163d
[openldap] / libraries / librewrite / rewrite.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 2000-2004 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 <ldap.h>
34
35 int ldap_debug;
36 int ldap_syslog;
37 int ldap_syslog_level;
38
39 char *
40 apply( 
41                 FILE *fin, 
42                 const char *rewriteContext,
43                 const char *arg
44 )
45 {
46         struct rewrite_info *info;
47         char *string, *sep, *result = NULL;
48         int rc;
49         void *cookie = &info;
50
51         info = rewrite_info_init( REWRITE_MODE_ERR );
52
53         if ( rewrite_read( fin, info ) != 0 ) {
54                 exit( EXIT_FAILURE );
55         }
56
57         rewrite_param_set( info, "prog", "rewrite" );
58
59         rewrite_session_init( info, cookie );
60
61         string = strdup( arg );
62         for ( sep = strchr( rewriteContext, ',' );
63                         rewriteContext != NULL;
64                         rewriteContext = sep,
65                         sep ? sep = strchr( rewriteContext, ',' ) : NULL ) {
66                 char    *errmsg = "";
67
68                 if ( sep != NULL ) {
69                         sep[ 0 ] = '\0';
70                         sep++;
71                 }
72                 /* rc = rewrite( info, rewriteContext, string, &result ); */
73                 rc = rewrite_session( info, rewriteContext, string,
74                                 cookie, &result );
75                 
76                 switch ( rc ) {
77                 case REWRITE_REGEXEC_OK:
78                         errmsg = "ok";
79                         break;
80
81                 case REWRITE_REGEXEC_ERR:
82                         errmsg = "error";
83                         break;
84
85                 case REWRITE_REGEXEC_STOP:
86                         errmsg = "stop";
87                         break;
88
89                 case REWRITE_REGEXEC_UNWILLING:
90                         errmsg = "unwilling to perform";
91                         break;
92
93                 default:
94                         if (rc >= REWRITE_REGEXEC_USER) {
95                                 errmsg = "user-defined";
96                         } else {
97                                 errmsg = "unknown";
98                         }
99                         break;
100                 }
101                 
102                 fprintf( stdout, "%s -> %s [%d:%s]\n", string, 
103                                 ( result ? result : "(null)" ),
104                                 rc, errmsg );
105                 if ( result == NULL ) {
106                         break;
107                 }
108                 free( string );
109                 string = result;
110         }
111
112         free( string );
113
114         rewrite_session_delete( info, cookie );
115
116         rewrite_info_delete( &info );
117
118         return result;
119 }
120
121 int
122 main( int argc, char *argv[] )
123 {
124         FILE    *fin = NULL;
125         char    *rewriteContext = REWRITE_DEFAULT_CONTEXT;
126         int     debug = 0;
127         char    *next;
128
129         while ( 1 ) {
130                 int opt = getopt( argc, argv, "d:f:hr:" );
131
132                 if ( opt == EOF ) {
133                         break;
134                 }
135
136                 switch ( opt ) {
137                 case 'd':
138                         debug = strtol( optarg, &next, 10 );
139                         if ( next == NULL || next[0] != '\0' ) {
140                                 fprintf( stderr, "illegal log level '%s'\n",
141                                                 optarg );
142                                 exit( EXIT_FAILURE );
143                         }
144                         break;
145
146                 case 'f':
147                         fin = fopen( optarg, "r" );
148                         if ( fin == NULL ) {
149                                 fprintf( stderr, "unable to open file '%s'\n",
150                                                 optarg );
151                                 exit( EXIT_FAILURE );
152                         }
153                         break;
154                         
155                 case 'h':
156                         fprintf( stderr, 
157         "usage: rewrite [options] string\n"
158         "\n"
159         "\t\t-f file\t\tconfiguration file\n"
160         "\t\t-r rule[s]\tlist of comma-separated rules\n"
161         "\n"
162         "\tsyntax:\n"
163         "\t\trewriteEngine\t{on|off}\n"
164         "\t\trewriteContext\tcontextName [alias aliasedContextName]\n"
165         "\t\trewriteRule\tpattern subst [flags]\n"
166         "\n" 
167                                 );
168                         exit( EXIT_SUCCESS );
169                         
170                 case 'r':
171                         rewriteContext = optarg;
172                         break;
173                 }
174         }
175         
176         if ( debug != 0 ) {
177                 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &debug);
178                 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug);
179         }
180         
181         if ( optind >= argc ) {
182                 return -1;
183         }
184
185         apply( ( fin ? fin : stdin ), rewriteContext, argv[ optind ] );
186
187         if ( fin ) {
188                 fclose( fin );
189         }
190
191         return 0;
192 }
193