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