Mercurial > dnsbl
changeset 3:7e1eb343a825
updates to use dcc conf files
author | carl |
---|---|
date | Wed, 21 Apr 2004 12:52:29 -0700 |
parents | 9bcd5ef11279 |
children | 15a7e942adec |
files | src/dnsbl.cpp |
diffstat | 1 files changed, 134 insertions(+), 42 deletions(-) [+] |
line wrap: on
line diff
--- a/src/dnsbl.cpp Tue Apr 20 22:11:14 2004 -0700 +++ b/src/dnsbl.cpp Wed Apr 21 12:52:29 2004 -0700 @@ -73,12 +73,6 @@ sfsistat mlfi_close(SMFICTX *ctx); } -#ifndef bool -# define bool int -# define TRUE 1 -# define FALSE 0 -#endif /* ! bool */ - struct ltstr { bool operator()(char* s1, char* s2) const { return strcmp(s1, s2) < 0; @@ -153,6 +147,8 @@ #define DEFAULT "default" #define WHITE "white" #define BLACK "black" +#define OK "ok" +#define MANY "many" //////////////////////////////////////////////// @@ -494,23 +490,22 @@ static void dumpit(from_map map); static void dumpit(from_map map) { - fprintf(stderr, "\n"); for (from_map::iterator i=map.begin(); i!=map.end(); i++) { - fprintf(stderr, "envfrom map %s\n", (*i).first); + char buf[2000]; + snprintf(buf, sizeof(buf), "envelope from map for %s", (*i).first); string_map *sm = (*i).second; - dumpit("envelope from", *sm); + dumpit(buf, *sm); } } -static void dumpit(); -static void dumpit() { - CONFIG &dc = *config; - fprintf(stderr, "dnsbls\n"); +static void dumpit(CONFIG &dc); +static void dumpit(CONFIG &dc) { + fprintf(stderr, "\ndnsbls\n"); for (dnsblp_map::iterator i=dc.dnsbls.begin(); i!=dc.dnsbls.end(); i++) { fprintf(stderr, "%s %s %s\n", (*i).first, (*i).second->suffix, (*i).second->message); } - fprintf(stderr, "dnsbl_lists\n"); + fprintf(stderr, "\ndnsbl_lists\n"); for (dnsbllp_map::iterator i=dc.dnsblls.begin(); i!=dc.dnsblls.end(); i++) { char *name = (*i).first; DNSBLL &dl = *((*i).second); @@ -521,22 +516,110 @@ } fprintf(stderr, "\n"); } + fprintf(stderr, "\nfiles\n"); + for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) { + char *f = *i; + fprintf(stderr, "config includes %s\n", f); + } +} + + +//////////////////////////////////////////////// +// check for redundant or recursive include files +// +static bool ok_to_include(CONFIG &dc, char *fn); +static bool ok_to_include(CONFIG &dc, char *fn) { + if (!fn) return false; + bool ok = true; + for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) { + char *f = *i; + if (strcmp(f, fn) == 0) { + my_syslog("redundant or recursive include file detected"); + ok = false; + break; + } + } + return ok; } //////////////////////////////////////////////// // load a single config file // +static void load_conf_dcc(CONFIG &dc, char *name, char *fn); +static void load_conf_dcc(CONFIG &dc, char *name, char *fn) { + dc.config_files.push_back(fn); + char *list = BLACK; + const int LINE_SIZE = 2000; + ifstream is(fn); + if (is.fail()) return; + char line[LINE_SIZE]; + char *delim = " \t"; + int curline = 0; + while (!is.eof()) { + is.getline(line, LINE_SIZE); + curline++; + int n = strlen(line); + if (!n) continue; + for (int i=0; i<n; i++) line[i] = tolower(line[i]); + if (line[0] == '#') continue; + char *head = line; + if (strspn(line, delim) == 0) { + // have a leading ok/many tag to fetch + char *cmd = strtok(line, delim); + if (strcmp(cmd, MANY) == 0) list = BLACK; + else if (strcmp(cmd, OK) == 0) list = WHITE; + head = cmd + strlen(cmd) + 1; + } + char *cmd = strtok(head, delim); + if (!cmd) continue; + if (strcmp(cmd, "env_from") == 0) { + char *from = next_token(delim); + if (from) { + string_map &fm = really_find_from_map(dc, name); + fm[from] = list; + } + } + else if (strcmp(cmd, "env_to") == 0) { + char *to = next_token(delim); + if (to) { + dc.env_to_dnsbll[to] = list; + dc.env_to_chkfrom[to] = list; + } + } + else if (strcmp(cmd, "substitute") == 0) { + char *tag = next_token(delim); + if (tag && (strcmp(tag, "mail_host") == 0)) { + char *from = next_token(delim); + if (from) { + string_map &fm = really_find_from_map(dc, name); + fm[from] = list; + } + } + } + else if (strcmp(cmd, "include") == 0) { + char *fn = next_token(delim); + if (ok_to_include(dc, fn)) { + load_conf_dcc(dc, name, fn); + } + } + + } + is.close(); +} + + static void load_conf(CONFIG &dc, char *fn); static void load_conf(CONFIG &dc, char *fn) { dc.config_files.push_back(fn); map<char*, int, ltstr> commands; - enum {dummy, dnsbl, dnsbll, envfrom, envto, include}; + enum {dummy, dnsbl, dnsbll, envfrom, envto, include, includedcc}; commands["dnsbl" ] = dnsbl; commands["dnsbl_list"] = dnsbll; commands["env_from" ] = envfrom; commands["env_to" ] = envto; commands["include" ] = include; + commands["include_dcc"] = includedcc; const int LINE_SIZE = 2000; ifstream is(fn); if (is.fail()) return; @@ -636,20 +719,19 @@ case include: { char *fn = next_token(delim); - if (fn) { - bool ok = true; - for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) { - char *f = *i; - if (strcmp(f, fn) == 0) { - my_syslog("recursive include file detected"); - ok = false; - break; - } - } - if (ok) { + if (ok_to_include(dc, fn)) { load_conf(dc, fn); processed = true; } + } break; + + case includedcc: { + char *name = next_token(delim); + if (!name) break; + char *fn = next_token(delim); + if (ok_to_include(dc, fn)) { + load_conf_dcc(dc, name, fn); + processed = true; } } break; @@ -710,10 +792,6 @@ CONFIG *old = config; config = newc; pthread_mutex_unlock(&config_mutex); - // dumpit(env_from); - // dumpit("envelope to dnsbl", env_to_dnsbl); - // dumpit("envelope to check from", env_to_chkfrom); - // dumpit(); if (old) old_configs.insert(old); } // now look for old configs with zero ref counts @@ -741,9 +819,10 @@ int main(int argc, char**argv) { - bool setconn = FALSE; + bool check = false; + bool setconn = false; int c; - const char *args = "p:s:h"; + const char *args = "p:t:hc"; extern char *optarg; // Process command line options @@ -761,7 +840,7 @@ if (strncasecmp(optarg, "unix:", 5) == 0) unlink(optarg + 5); else if (strncasecmp(optarg, "local:", 6) == 0) unlink(optarg + 6); - setconn = TRUE; + setconn = true; break; case 't': @@ -775,6 +854,10 @@ } break; + case 'c': + check = true; + break; + case 'h': default: usage(argv[0]); @@ -791,6 +874,15 @@ exit(EX_UNAVAILABLE); } + if (check) { + CONFIG &dc = *new_conf(); + dumpit(dc.env_from); + dumpit("envelope to (dnsbl list)", dc.env_to_dnsbll); + dumpit("envelope to (from map)", dc.env_to_chkfrom); + dumpit(dc); + return 0; + } + // switch to background mode if (daemon(1,0) < 0) { fprintf(stderr, "daemon() call failed\n"); @@ -821,5 +913,5 @@ fclose(f); } - int rc = smfi_main(); + return smfi_main(); }