diff src/dnsbl.cpp @ 235:e6c66640f6f9

Add SRS decoding to envelope addresses
author Carl Byington <carl@five-ten-sg.com>
date Tue, 09 Jun 2009 08:36:34 -0700
parents 5c3e9bf45bb5
children c0d2e99c0a1d
line wrap: on
line diff
--- a/src/dnsbl.cpp	Mon May 25 17:48:40 2009 -0700
+++ b/src/dnsbl.cpp	Tue Jun 09 08:36:34 2009 -0700
@@ -1,6 +1,6 @@
 /*
 
-Copyright (c) 2007 Carl Byington - 510 Software Group, released under
+Copyright (c) 2009 Carl Byington - 510 Software Group, released under
 the GPL version 3 or any later version at your choice available at
 http://www.gnu.org/licenses/gpl-3.0.txt
 
@@ -96,6 +96,7 @@
 CONFIG   *config = NULL;    // protected by the config_mutex
 int   generation = 0;       // protected by the config_mutex
 const int maxlen = 1000;    // used for snprintf buffers
+regex_t srs_pattern;        // used to detect srs coding in mail addresses
 
 pthread_mutex_t  config_mutex;
 pthread_mutex_t  syslog_mutex;
@@ -969,7 +970,7 @@
 // that.  So the <> wrapper is now optional.  It may have mixed case, just
 // as the mail client sent it.  We dup the string and convert the duplicate
 // to lower case. Some clients enclose the entire address in single quotes,
-// so we strip those as well.
+// so we strip those as well. We also remove the SRS coding.
 //
 const char *to_lower_string(const char *email);
 const char *to_lower_string(const char *email) {
@@ -987,6 +988,27 @@
     char *key = strdup(email);
     key[n] = '\0';
     for (int i=0; i<n; i++) key[i] = tolower(key[i]);
+    if ((n > 12) && (strncmp(key, "srs", 3) == 0)) {
+        // might have srs coding to be removed
+        const int nmatch = 6;
+        regmatch_t match[nmatch];
+        if (0 == regexec(&srs_pattern, key, nmatch, match, 0)) {
+            int s4 = match[4].rm_so;    // domain
+            int e4 = match[4].rm_eo;
+            int s5 = match[5].rm_so;    // user
+            int e5 = match[5].rm_eo;
+            if ((s4 != -1) && (s5 != -1)) {
+                char *newkey = strdup(key);   // large enough
+                key[e4] = '\0';
+                key[e5] = '\0';
+                strcpy(newkey, key+s5); // user
+                strcat(newkey, "@");    // @
+                strcat(newkey, key+s4); // domain
+                free(key);
+                key = newkey;
+            }
+        }
+    }
     return key;
 }
 
@@ -1491,6 +1513,12 @@
     const char *args = "b:r:p:t:e:d:chs";
     extern char *optarg;
 
+    // setup srs coding detection
+    if (regcomp(&srs_pattern, "^srs(0|1)=([^=]*)=([^=]*)=([^=]*)=([^@]*)@", REG_ICASE | REG_EXTENDED)) {
+        printf("cannot compile regex pattern to find srs coding in mail addresses\n");
+        exit(3);
+    }
+
     // Process command line options
     while ((c = getopt(argc, argv, args)) != -1) {
         switch (c) {