|  | @@ -0,0 +1,99 @@
 | 
		
	
		
			
			|  | 1 | +/* PostSRSd - Sender Rewriting Scheme daemon for Postfix
 | 
		
	
		
			
			|  | 2 | + * Copyright (c) 2012 Timo Röhling <timo.roehling@gmx.de>
 | 
		
	
		
			
			|  | 3 | + *
 | 
		
	
		
			
			|  | 4 | + * This program is free software: you can redistribute it and/or modify
 | 
		
	
		
			
			|  | 5 | + * it under the terms of the GNU General Public License as published by
 | 
		
	
		
			
			|  | 6 | + * the Free Software Foundation, either version 2 of the License, or
 | 
		
	
		
			
			|  | 7 | + * (at your option) any later version.
 | 
		
	
		
			
			|  | 8 | + *
 | 
		
	
		
			
			|  | 9 | + * This program is distributed in the hope that it will be useful,
 | 
		
	
		
			
			|  | 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
		
	
		
			
			|  | 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
		
	
		
			
			|  | 12 | + * GNU General Public License for more details.
 | 
		
	
		
			
			|  | 13 | + *
 | 
		
	
		
			
			|  | 14 | + * You should have received a copy of the GNU General Public License
 | 
		
	
		
			
			|  | 15 | + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
		
	
		
			
			|  | 16 | + */
 | 
		
	
		
			
			|  | 17 | +
 | 
		
	
		
			
			|  | 18 | +/* This program uses the libsrs2 library. The relevant source
 | 
		
	
		
			
			|  | 19 | + * files have been added to this distribution. */
 | 
		
	
		
			
			|  | 20 | +#include "srs2.h"
 | 
		
	
		
			
			|  | 21 | +#include <string.h>
 | 
		
	
		
			
			|  | 22 | +
 | 
		
	
		
			
			|  | 23 | +static int run_srs(srs_t* srs, const char* address, const char* domain)
 | 
		
	
		
			
			|  | 24 | +{
 | 
		
	
		
			
			|  | 25 | +  int result, i;
 | 
		
	
		
			
			|  | 26 | +  char buf1[1024];
 | 
		
	
		
			
			|  | 27 | +  char buf2[1024];
 | 
		
	
		
			
			|  | 28 | +
 | 
		
	
		
			
			|  | 29 | +  printf("srs_forward(\"%s\", \"%s\") = ", address, domain);
 | 
		
	
		
			
			|  | 30 | +  result = srs_forward(srs, buf1, sizeof(buf1), address, domain);
 | 
		
	
		
			
			|  | 31 | +  printf ("%d\n", result);
 | 
		
	
		
			
			|  | 32 | +  if (result != SRS_SUCCESS) return 0;
 | 
		
	
		
			
			|  | 33 | +  printf("srs_reverse(\"%s\") = ", buf1);
 | 
		
	
		
			
			|  | 34 | +  result = srs_reverse(srs, buf2, sizeof(buf2), buf1);
 | 
		
	
		
			
			|  | 35 | +  printf("%d\n", result);
 | 
		
	
		
			
			|  | 36 | +  if (result != SRS_SUCCESS) return 0;
 | 
		
	
		
			
			|  | 37 | +  if (strcasecmp(address, buf2))
 | 
		
	
		
			
			|  | 38 | +  {
 | 
		
	
		
			
			|  | 39 | +    printf("SRS not idempotent: \"%s\" != \"%s\"\n", address, buf2);
 | 
		
	
		
			
			|  | 40 | +    return 0;
 | 
		
	
		
			
			|  | 41 | +  }
 | 
		
	
		
			
			|  | 42 | +
 | 
		
	
		
			
			|  | 43 | +  i = strchr(buf1, '@') - buf1;
 | 
		
	
		
			
			|  | 44 | +  while (i > 0)
 | 
		
	
		
			
			|  | 45 | +  {
 | 
		
	
		
			
			|  | 46 | +    --i;
 | 
		
	
		
			
			|  | 47 | +    if (buf1[i] == '=' || buf1[i] == '-' || buf1[i] == '+') continue;
 | 
		
	
		
			
			|  | 48 | +    buf1[i]++;
 | 
		
	
		
			
			|  | 49 | +    printf("srs_reverse(\"%s\") = ", buf1);
 | 
		
	
		
			
			|  | 50 | +    result = srs_reverse(srs, buf2, sizeof(buf2), buf1);
 | 
		
	
		
			
			|  | 51 | +    printf("%d\n", result);
 | 
		
	
		
			
			|  | 52 | +    if (result == SRS_SUCCESS) return 0;
 | 
		
	
		
			
			|  | 53 | +    buf1[i]--;
 | 
		
	
		
			
			|  | 54 | +  }
 | 
		
	
		
			
			|  | 55 | +  return 1;
 | 
		
	
		
			
			|  | 56 | +}
 | 
		
	
		
			
			|  | 57 | +
 | 
		
	
		
			
			|  | 58 | +static void generate_random_address(char* buf, size_t len1, size_t len2)
 | 
		
	
		
			
			|  | 59 | +{
 | 
		
	
		
			
			|  | 60 | +  static const char chars[] = "abcdefghijklmnopqrstuvwxyz0123456789-+=";
 | 
		
	
		
			
			|  | 61 | +  size_t i = 0, l1 = len1, l2 = len2;
 | 
		
	
		
			
			|  | 62 | +  while (l1 > 0)
 | 
		
	
		
			
			|  | 63 | +  {
 | 
		
	
		
			
			|  | 64 | +    buf[i++] = chars[random() % 39];
 | 
		
	
		
			
			|  | 65 | +    if (l1 < len1 && l1 > 1 && buf[i - 1] != '.' && (random() % 16 == 0)) buf[i - 1] = '.';
 | 
		
	
		
			
			|  | 66 | +    --l1;
 | 
		
	
		
			
			|  | 67 | +  }
 | 
		
	
		
			
			|  | 68 | +  buf[i++] = '@';
 | 
		
	
		
			
			|  | 69 | +  while (l2 > 0)
 | 
		
	
		
			
			|  | 70 | +  {
 | 
		
	
		
			
			|  | 71 | +    buf[i++] = 'a' + random() % 26;
 | 
		
	
		
			
			|  | 72 | +    if (l2 < len2 && l2 > 1 && buf[i - 1] != '.' && (random() % 16 == 0)) buf[i - 1] = '.';
 | 
		
	
		
			
			|  | 73 | +    --l2;
 | 
		
	
		
			
			|  | 74 | +  }
 | 
		
	
		
			
			|  | 75 | +  buf[i++] = 0;
 | 
		
	
		
			
			|  | 76 | +}
 | 
		
	
		
			
			|  | 77 | +
 | 
		
	
		
			
			|  | 78 | +#define ASSERT_SRS_OK(...) if (!run_srs(srs, __VA_ARGS__)) return EXIT_FAILURE
 | 
		
	
		
			
			|  | 79 | +
 | 
		
	
		
			
			|  | 80 | +int main (int argc, char** argv)
 | 
		
	
		
			
			|  | 81 | +{
 | 
		
	
		
			
			|  | 82 | +  srs_t* srs;
 | 
		
	
		
			
			|  | 83 | +  size_t l1, l2;
 | 
		
	
		
			
			|  | 84 | +  char addr[128];
 | 
		
	
		
			
			|  | 85 | +
 | 
		
	
		
			
			|  | 86 | +  srs = srs_new();
 | 
		
	
		
			
			|  | 87 | +  srs_add_secret (srs, "t0ps3cr3t");
 | 
		
	
		
			
			|  | 88 | +
 | 
		
	
		
			
			|  | 89 | +  for (l1 = 1; l1 <= 63; ++l1)
 | 
		
	
		
			
			|  | 90 | +  {
 | 
		
	
		
			
			|  | 91 | +    for (l2 = 1; l2 <= 63; ++l2)
 | 
		
	
		
			
			|  | 92 | +    {
 | 
		
	
		
			
			|  | 93 | +      generate_random_address(addr, l1, l2);
 | 
		
	
		
			
			|  | 94 | +      ASSERT_SRS_OK(addr, "example.com");
 | 
		
	
		
			
			|  | 95 | +    }
 | 
		
	
		
			
			|  | 96 | +  }
 | 
		
	
		
			
			|  | 97 | +  return EXIT_SUCCESS;
 | 
		
	
		
			
			|  | 98 | +}
 | 
		
	
		
			
			|  | 99 | +
 |