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