comparison src/dnsbl.cpp @ 246:8b0f16abee53 stable-6-0-28

Add prvs decoding to envelope addresses
author Carl Byington <carl@five-ten-sg.com>
date Fri, 30 Sep 2011 15:29:07 -0700
parents ef97c7cd4a6e
children b0738685bf51
comparison
equal deleted inserted replaced
245:c7ccb14f91b4 246:8b0f16abee53
95 bool loader_run = true; // used to stop the config loader thread 95 bool loader_run = true; // used to stop the config loader thread
96 CONFIG *config = NULL; // protected by the config_mutex 96 CONFIG *config = NULL; // protected by the config_mutex
97 int generation = 0; // protected by the config_mutex 97 int generation = 0; // protected by the config_mutex
98 const int maxlen = 1000; // used for snprintf buffers 98 const int maxlen = 1000; // used for snprintf buffers
99 regex_t srs_pattern; // used to detect srs coding in mail addresses 99 regex_t srs_pattern; // used to detect srs coding in mail addresses
100 regex_t prvs_pattern; // used to detect prvs coding in mail addresses
100 101
101 pthread_mutex_t config_mutex; 102 pthread_mutex_t config_mutex;
102 pthread_mutex_t syslog_mutex; 103 pthread_mutex_t syslog_mutex;
103 pthread_mutex_t resolve_mutex; 104 pthread_mutex_t resolve_mutex;
104 pthread_mutex_t fd_pool_mutex; 105 pthread_mutex_t fd_pool_mutex;
989 // enclosed in <>. I think older versions of sendmail supplied the <> 990 // enclosed in <>. I think older versions of sendmail supplied the <>
990 // wrapper if the mail client did not, but the current version does not do 991 // wrapper if the mail client did not, but the current version does not do
991 // that. So the <> wrapper is now optional. It may have mixed case, just 992 // that. So the <> wrapper is now optional. It may have mixed case, just
992 // as the mail client sent it. We dup the string and convert the duplicate 993 // as the mail client sent it. We dup the string and convert the duplicate
993 // to lower case. Some clients enclose the entire address in single quotes, 994 // to lower case. Some clients enclose the entire address in single quotes,
994 // so we strip those as well. We also remove the SRS coding. 995 // so we strip those as well. We also remove the SRS and prvs coding.
995 // 996 //
996 const char *to_lower_string(const char *email); 997 const char *to_lower_string(const char *email);
997 const char *to_lower_string(const char *email) { 998 const char *to_lower_string(const char *email) {
998 int n = strlen(email); 999 int n = strlen(email);
999 if (email[0] == '<') { 1000 if (email[0] == '<') {
1007 email++; 1008 email++;
1008 } 1009 }
1009 char *key = strdup(email); 1010 char *key = strdup(email);
1010 key[n] = '\0'; 1011 key[n] = '\0';
1011 for (int i=0; i<n; i++) key[i] = tolower(key[i]); 1012 for (int i=0; i<n; i++) key[i] = tolower(key[i]);
1012 if ((n > 12) && (strncmp(key, "srs", 3) == 0)) { 1013 if ((n > 14) && (strncmp(key, "srs", 3) == 0)) {
1013 // might have srs coding to be removed 1014 // might have srs coding to be removed
1014 const int nmatch = 6; 1015 const int nmatch = 6;
1015 regmatch_t match[nmatch]; 1016 regmatch_t match[nmatch];
1016 if (0 == regexec(&srs_pattern, key, nmatch, match, 0)) { 1017 if (0 == regexec(&srs_pattern, key, nmatch, match, 0)) {
1017 int s4 = match[4].rm_so; // domain 1018 int s4 = match[4].rm_so; // domain
1023 key[e4] = '\0'; 1024 key[e4] = '\0';
1024 key[e5] = '\0'; 1025 key[e5] = '\0';
1025 strcpy(newkey, key+s5); // user 1026 strcpy(newkey, key+s5); // user
1026 strcat(newkey, "@"); // @ 1027 strcat(newkey, "@"); // @
1027 strcat(newkey, key+s4); // domain 1028 strcat(newkey, key+s4); // domain
1029 free(key);
1030 key = newkey;
1031 }
1032 }
1033 }
1034 if ((n > 7) && (strncmp(key, "prvs", 4) == 0)) {
1035 // might have prvs coding to be removed
1036 const int nmatch = 3;
1037 regmatch_t match[nmatch];
1038 if (0 == regexec(&prvs_pattern, key, nmatch, match, 0)) {
1039 int s2 = match[2].rm_so; // user@domain
1040 if (s2 != -1) {
1041 char *newkey = strdup(key+s2); // user@domain
1028 free(key); 1042 free(key);
1029 key = newkey; 1043 key = newkey;
1030 } 1044 }
1031 } 1045 }
1032 } 1046 }
1557 int c; 1571 int c;
1558 const char *args = "b:r:p:t:e:d:chs"; 1572 const char *args = "b:r:p:t:e:d:chs";
1559 extern char *optarg; 1573 extern char *optarg;
1560 1574
1561 // setup srs coding detection 1575 // setup srs coding detection
1562 if (regcomp(&srs_pattern, "^srs(0|1)=([^=]*)=([^=]*)=([^=]*)=([^@]*)@", REG_ICASE | REG_EXTENDED)) { 1576 if (regcomp(&srs_pattern, "^srs(0|1)=([^=]+)=([^=]+)=([^=]+)=([^@]+)@", REG_ICASE | REG_EXTENDED)) {
1563 printf("cannot compile regex pattern to find srs coding in mail addresses\n"); 1577 printf("cannot compile regex pattern to find srs coding in mail addresses\n");
1578 exit(3);
1579 }
1580
1581 // setup prvs coding detection
1582 if (regcomp(&prvs_pattern, "^prvs=([^=]+)=(.+)$", REG_ICASE | REG_EXTENDED)) {
1583 printf("cannot compile regex pattern to find prvs coding in mail addresses\n");
1564 exit(3); 1584 exit(3);
1565 } 1585 }
1566 1586
1567 // Process command line options 1587 // Process command line options
1568 while ((c = getopt(argc, argv, args)) != -1) { 1588 while ((c = getopt(argc, argv, args)) != -1) {