Back to index

openldap  2.4.31
rewrite.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 2000-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in the file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 /* ACKNOWLEDGEMENT:
00016  * This work was initially developed by Pierangelo Masarati for
00017  * inclusion in OpenLDAP Software.
00018  */
00019 
00020 #include <portable.h>
00021 
00022 #include <ac/stdlib.h>
00023 #include <ac/string.h>
00024 #include <ac/syslog.h>
00025 #include <ac/regex.h>
00026 #include <ac/socket.h>
00027 #include <ac/unistd.h>
00028 #include <ac/ctype.h>
00029 #include <ac/string.h>
00030 #include <stdio.h>
00031 
00032 #include <rewrite.h>
00033 #include <lutil.h>
00034 #include <ldap.h>
00035 
00036 int ldap_debug;
00037 int ldap_syslog;
00038 int ldap_syslog_level;
00039 
00040 static void
00041 apply( 
00042               FILE *fin, 
00043               const char *rewriteContext,
00044               const char *arg
00045 )
00046 {
00047        struct rewrite_info *info;
00048        char *string, *sep, *result = NULL;
00049        int rc;
00050        void *cookie = &info;
00051 
00052        info = rewrite_info_init( REWRITE_MODE_ERR );
00053 
00054        if ( rewrite_read( fin, info ) != 0 ) {
00055               exit( EXIT_FAILURE );
00056        }
00057 
00058        rewrite_param_set( info, "prog", "rewrite" );
00059 
00060        rewrite_session_init( info, cookie );
00061 
00062        string = (char *)arg;
00063        for ( sep = strchr( rewriteContext, ',' );
00064                      rewriteContext != NULL;
00065                      rewriteContext = sep,
00066                      sep ? sep = strchr( rewriteContext, ',' ) : NULL )
00067        {
00068               char   *errmsg = "";
00069 
00070               if ( sep != NULL ) {
00071                      sep[ 0 ] = '\0';
00072                      sep++;
00073               }
00074               /* rc = rewrite( info, rewriteContext, string, &result ); */
00075               rc = rewrite_session( info, rewriteContext, string,
00076                             cookie, &result );
00077               
00078               switch ( rc ) {
00079               case REWRITE_REGEXEC_OK:
00080                      errmsg = "ok";
00081                      break;
00082 
00083               case REWRITE_REGEXEC_ERR:
00084                      errmsg = "error";
00085                      break;
00086 
00087               case REWRITE_REGEXEC_STOP:
00088                      errmsg = "stop";
00089                      break;
00090 
00091               case REWRITE_REGEXEC_UNWILLING:
00092                      errmsg = "unwilling to perform";
00093                      break;
00094 
00095               default:
00096                      if (rc >= REWRITE_REGEXEC_USER) {
00097                             errmsg = "user-defined";
00098                      } else {
00099                             errmsg = "unknown";
00100                      }
00101                      break;
00102               }
00103               
00104               fprintf( stdout, "%s -> %s [%d:%s]\n", string, 
00105                             ( result ? result : "(null)" ),
00106                             rc, errmsg );
00107               if ( result == NULL ) {
00108                      break;
00109               }
00110               if ( string != arg && string != result ) {
00111                      free( string );
00112               }
00113               string = result;
00114        }
00115 
00116        if ( result && result != arg ) {
00117               free( result );
00118        }
00119 
00120        rewrite_session_delete( info, cookie );
00121 
00122        rewrite_info_delete( &info );
00123 }
00124 
00125 int
00126 main( int argc, char *argv[] )
00127 {
00128        FILE   *fin = NULL;
00129        char   *rewriteContext = REWRITE_DEFAULT_CONTEXT;
00130        int    debug = 0;
00131 
00132        while ( 1 ) {
00133               int opt = getopt( argc, argv, "d:f:hr:" );
00134 
00135               if ( opt == EOF ) {
00136                      break;
00137               }
00138 
00139               switch ( opt ) {
00140               case 'd':
00141                      if ( lutil_atoi( &debug, optarg ) != 0 ) {
00142                             fprintf( stderr, "illegal log level '%s'\n",
00143                                           optarg );
00144                             exit( EXIT_FAILURE );
00145                      }
00146                      break;
00147 
00148               case 'f':
00149                      fin = fopen( optarg, "r" );
00150                      if ( fin == NULL ) {
00151                             fprintf( stderr, "unable to open file '%s'\n",
00152                                           optarg );
00153                             exit( EXIT_FAILURE );
00154                      }
00155                      break;
00156                      
00157               case 'h':
00158                      fprintf( stderr, 
00159        "usage: rewrite [options] string\n"
00160        "\n"
00161        "\t\t-f file\t\tconfiguration file\n"
00162        "\t\t-r rule[s]\tlist of comma-separated rules\n"
00163        "\n"
00164        "\tsyntax:\n"
00165        "\t\trewriteEngine\t{on|off}\n"
00166        "\t\trewriteContext\tcontextName [alias aliasedContextName]\n"
00167        "\t\trewriteRule\tpattern subst [flags]\n"
00168        "\n" 
00169                             );
00170                      exit( EXIT_SUCCESS );
00171                      
00172               case 'r':
00173                      rewriteContext = optarg;
00174                      break;
00175               }
00176        }
00177        
00178        if ( debug != 0 ) {
00179               ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &debug);
00180               ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug);
00181        }
00182        
00183        if ( optind >= argc ) {
00184               return -1;
00185        }
00186 
00187        apply( ( fin ? fin : stdin ), rewriteContext, argv[ optind ] );
00188 
00189        if ( fin ) {
00190               fclose( fin );
00191        }
00192 
00193        return 0;
00194 }
00195