Mercurial > dnsbl
comparison src/dnsbl.cpp @ 86:c1280cd3e248
add multiple debug syslog levels, remove duplicate dnsbl definitions
author | carl |
---|---|
date | Tue, 19 Jul 2005 22:47:15 -0700 |
parents | 091d3fe3db46 |
children | 946fc1bcfb2c |
comparison
equal
deleted
inserted
replaced
85:7e4a90e5f213 | 86:c1280cd3e248 |
---|---|
11 -p port The port through which the MTA will connect to this milter. | 11 -p port The port through which the MTA will connect to this milter. |
12 -t sec The timeout value. | 12 -t sec The timeout value. |
13 -c Check the config, and print a copy to stdout. Don't start the | 13 -c Check the config, and print a copy to stdout. Don't start the |
14 milter or do anything with the socket. | 14 milter or do anything with the socket. |
15 -s Stress test by loading and deleting the current config in a loop. | 15 -s Stress test by loading and deleting the current config in a loop. |
16 -d Add debug syslog entries | 16 -d increase debug level |
17 -e f|t Print the results of looking up from address f and to address | 17 -e f|t Print the results of looking up from address f and to address |
18 t in the current config | 18 t in the current config |
19 | 19 |
20 | 20 |
21 TODO: | 21 TODO: |
79 sfsistat mlfi_abort(SMFICTX *ctx); | 79 sfsistat mlfi_abort(SMFICTX *ctx); |
80 sfsistat mlfi_close(SMFICTX *ctx); | 80 sfsistat mlfi_close(SMFICTX *ctx); |
81 void sig_chld(int signo); | 81 void sig_chld(int signo); |
82 } | 82 } |
83 | 83 |
84 bool debug_syslog = false; | 84 int debug_syslog = 0; |
85 bool syslog_opened = false; | 85 bool syslog_opened = false; |
86 bool use_syslog = true; // false to printf | 86 bool use_syslog = true; // false to printf |
87 bool loader_run = true; // used to stop the config loader thread | 87 bool loader_run = true; // used to stop the config loader thread |
88 CONFIG *config = NULL; // protected by the config_mutex | 88 CONFIG *config = NULL; // protected by the config_mutex |
89 int generation = 0; // protected by the config_mutex | 89 int generation = 0; // protected by the config_mutex |
671 | 671 |
672 // try to only look at limit/cnt fraction of the available cnt host names in random mode | 672 // try to only look at limit/cnt fraction of the available cnt host names in random mode |
673 if ((cnt > limit) && (limit > 0) && random) { | 673 if ((cnt > limit) && (limit > 0) && random) { |
674 int r = rand() % cnt; | 674 int r = rand() % cnt; |
675 if (r >= limit) { | 675 if (r >= limit) { |
676 char buf[maxlen]; | 676 if (debug_syslog > 2) { |
677 snprintf(buf, sizeof(buf), "host %s skipped", host); | 677 char buf[maxlen]; |
678 my_syslog(&priv, buf); | 678 snprintf(buf, sizeof(buf), "host %s skipped", host); |
679 my_syslog(&priv, buf); | |
680 } | |
679 continue; | 681 continue; |
680 } | 682 } |
681 } | 683 } |
682 count++; | 684 count++; |
683 ip = dns_interface(priv, host, true, &nameservers); | 685 ip = dns_interface(priv, host, true, &nameservers); |
684 if (debug_syslog) { | 686 if (debug_syslog > 2) { |
685 char buf[maxlen]; | 687 char buf[maxlen]; |
686 if (ip) { | 688 if (ip) { |
687 char adr[sizeof "255.255.255.255"]; | 689 char adr[sizeof "255.255.255.255"]; |
688 adr[0] = '\0'; | 690 adr[0] = '\0'; |
689 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr)); | 691 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr)); |
712 return true; | 714 return true; |
713 } | 715 } |
714 host = (*i).first; // a transient reference that needs to be replaced before we return it | 716 host = (*i).first; // a transient reference that needs to be replaced before we return it |
715 ip = (*i).second; | 717 ip = (*i).second; |
716 if (!ip) ip = dns_interface(priv, host, false, NULL); | 718 if (!ip) ip = dns_interface(priv, host, false, NULL); |
717 if (debug_syslog) { | 719 if (debug_syslog > 2) { |
718 char buf[maxlen]; | 720 char buf[maxlen]; |
719 if (ip) { | 721 if (ip) { |
720 char adr[sizeof "255.255.255.255"]; | 722 char adr[sizeof "255.255.255.255"]; |
721 adr[0] = '\0'; | 723 adr[0] = '\0'; |
722 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr)); | 724 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr)); |
801 CONFIG &dc = *priv.pc; | 803 CONFIG &dc = *priv.pc; |
802 if (!priv.queueid) priv.queueid = strdup(smfi_getsymval(ctx, "i")); | 804 if (!priv.queueid) priv.queueid = strdup(smfi_getsymval(ctx, "i")); |
803 char *rcptaddr = rcpt[0]; | 805 char *rcptaddr = rcpt[0]; |
804 char *loto = to_lower_string(rcptaddr); | 806 char *loto = to_lower_string(rcptaddr); |
805 CONTEXT &con = *(dc.find_context(loto)->find_context(priv.mailaddr)); | 807 CONTEXT &con = *(dc.find_context(loto)->find_context(priv.mailaddr)); |
806 if (debug_syslog) { | 808 if (debug_syslog > 1) { |
807 char buf[maxlen]; | 809 char buf[maxlen]; |
808 char msg[maxlen]; | 810 char msg[maxlen]; |
809 snprintf(msg, sizeof(msg), "from <%s> to <%s> using context %s", priv.mailaddr, loto, con.get_full_name(buf,maxlen)); | 811 snprintf(msg, sizeof(msg), "from <%s> to <%s> using context %s", priv.mailaddr, loto, con.get_full_name(buf,maxlen)); |
810 my_syslog(&priv, msg); | 812 my_syslog(&priv, msg); |
811 } | 813 } |
965 CONFIG* new_conf() { | 967 CONFIG* new_conf() { |
966 CONFIG *newc = new CONFIG; | 968 CONFIG *newc = new CONFIG; |
967 pthread_mutex_lock(&config_mutex); | 969 pthread_mutex_lock(&config_mutex); |
968 newc->generation = generation++; | 970 newc->generation = generation++; |
969 pthread_mutex_unlock(&config_mutex); | 971 pthread_mutex_unlock(&config_mutex); |
970 char buf[maxlen]; | 972 if (debug_syslog) { |
971 snprintf(buf, sizeof(buf), "loading configuration generation %d", newc->generation); | 973 char buf[maxlen]; |
972 my_syslog(buf); | 974 snprintf(buf, sizeof(buf), "loading configuration generation %d", newc->generation); |
975 my_syslog(buf); | |
976 } | |
973 if (load_conf(*newc, "dnsbl.conf")) { | 977 if (load_conf(*newc, "dnsbl.conf")) { |
974 newc->load_time = time(NULL); | 978 newc->load_time = time(NULL); |
975 return newc; | 979 return newc; |
976 } | 980 } |
977 delete newc; | 981 delete newc; |
1012 } | 1016 } |
1013 // now look for old configs with zero ref counts | 1017 // now look for old configs with zero ref counts |
1014 for (configp_set::iterator i=old_configs.begin(); i!=old_configs.end(); ) { | 1018 for (configp_set::iterator i=old_configs.begin(); i!=old_configs.end(); ) { |
1015 CONFIG *old = *i; | 1019 CONFIG *old = *i; |
1016 if (!old->reference_count) { | 1020 if (!old->reference_count) { |
1017 char buf[maxlen]; | 1021 if (debug_syslog) { |
1018 snprintf(buf, sizeof(buf), "freeing memory for old configuration generation %d", old->generation); | 1022 char buf[maxlen]; |
1019 my_syslog(buf); | 1023 snprintf(buf, sizeof(buf), "freeing memory for old configuration generation %d", old->generation); |
1024 my_syslog(buf); | |
1025 } | |
1020 delete old; // destructor does all the work | 1026 delete old; // destructor does all the work |
1021 old_configs.erase(i++); | 1027 old_configs.erase(i++); |
1022 } | 1028 } |
1023 else i++; | 1029 else i++; |
1024 } | 1030 } |
1028 | 1034 |
1029 | 1035 |
1030 void usage(char *prog); | 1036 void usage(char *prog); |
1031 void usage(char *prog) | 1037 void usage(char *prog) |
1032 { | 1038 { |
1033 fprintf(stderr, "Usage: %s [-d] [-c] [-s] [-e from|to] -r port -p sm-sock-addr [-t timeout]\n", prog); | 1039 fprintf(stderr, "Usage: %s [-d [level]] [-c] [-s] [-e from|to] -r port -p sm-sock-addr [-t timeout]\n", prog); |
1034 fprintf(stderr, "where port is for the connection to our own dns resolver processes\n"); | 1040 fprintf(stderr, "where port is for the connection to our own dns resolver processes\n"); |
1035 fprintf(stderr, " and should be local-domain-socket-file-name\n"); | 1041 fprintf(stderr, " and should be local-domain-socket-file-name\n"); |
1036 fprintf(stderr, "where sm-sock-addr is for the connection to sendmail\n"); | 1042 fprintf(stderr, "where sm-sock-addr is for the connection to sendmail\n"); |
1037 fprintf(stderr, " and should be one of\n"); | 1043 fprintf(stderr, " and should be one of\n"); |
1038 fprintf(stderr, " inet:port@ip-address\n"); | 1044 fprintf(stderr, " inet:port@ip-address\n"); |
1039 fprintf(stderr, " local:local-domain-socket-file-name\n"); | 1045 fprintf(stderr, " local:local-domain-socket-file-name\n"); |
1040 fprintf(stderr, "-c will load and dump the config to stdout\n"); | 1046 fprintf(stderr, "-c will load and dump the config to stdout\n"); |
1041 fprintf(stderr, "-s will stress test the config loading code by repeating the load/free cycle\n"); | 1047 fprintf(stderr, "-s will stress test the config loading code by repeating the load/free cycle\n"); |
1042 fprintf(stderr, " in an infinte loop.\n"); | 1048 fprintf(stderr, " in an infinte loop.\n"); |
1043 fprintf(stderr, "-d will add some syslog debug messages\n"); | 1049 fprintf(stderr, "-d will set the syslog message level, currently 0 to 3"); |
1044 fprintf(stderr, "-e will print the results of looking up the from and to addresses in the\n"); | 1050 fprintf(stderr, "-e will print the results of looking up the from and to addresses in the\n"); |
1045 fprintf(stderr, " current config. The | character is used to separate the from and to\n"); | 1051 fprintf(stderr, " current config. The | character is used to separate the from and to\n"); |
1046 fprintf(stderr, " addresses in the argument to the -e switch\n"); | 1052 fprintf(stderr, " addresses in the argument to the -e switch\n"); |
1047 } | 1053 } |
1048 | 1054 |
1082 bool stress = false; | 1088 bool stress = false; |
1083 bool setconn = false; | 1089 bool setconn = false; |
1084 bool setreso = false; | 1090 bool setreso = false; |
1085 char *email = NULL; | 1091 char *email = NULL; |
1086 int c; | 1092 int c; |
1087 const char *args = "r:p:t:e:cdhs"; | 1093 const char *args = "r:p:t:e:d:chs"; |
1088 extern char *optarg; | 1094 extern char *optarg; |
1089 | 1095 |
1090 // Process command line options | 1096 // Process command line options |
1091 while ((c = getopt(argc, argv, args)) != -1) { | 1097 while ((c = getopt(argc, argv, args)) != -1) { |
1092 switch (c) { | 1098 switch (c) { |
1137 case 's': | 1143 case 's': |
1138 stress = true; | 1144 stress = true; |
1139 break; | 1145 break; |
1140 | 1146 |
1141 case 'd': | 1147 case 'd': |
1142 debug_syslog = true; | 1148 if (optarg == NULL || *optarg == '\0') debug_syslog = 1; |
1149 else debug_syslog = atoi(optarg); | |
1143 break; | 1150 break; |
1144 | 1151 |
1145 case 'h': | 1152 case 'h': |
1146 default: | 1153 default: |
1147 usage(argv[0]); | 1154 usage(argv[0]); |
1149 } | 1156 } |
1150 } | 1157 } |
1151 | 1158 |
1152 if (check) { | 1159 if (check) { |
1153 use_syslog = false; | 1160 use_syslog = false; |
1154 debug_syslog = true; | 1161 debug_syslog = 10; |
1155 CONFIG *conf = new_conf(); | 1162 CONFIG *conf = new_conf(); |
1156 if (conf) { | 1163 if (conf) { |
1157 conf->dump(); | 1164 conf->dump(); |
1158 delete conf; | 1165 delete conf; |
1159 return 0; | 1166 return 0; |
1311 int newchild = fork(); | 1318 int newchild = fork(); |
1312 if (newchild == 0) { | 1319 if (newchild == 0) { |
1313 // this is the worker process | 1320 // this is the worker process |
1314 // child does not need the listening socket | 1321 // child does not need the listening socket |
1315 close(resolver_socket); | 1322 close(resolver_socket); |
1316 //my_syslog("child forked a worker process"); | |
1317 process_resolver_requests(s); | 1323 process_resolver_requests(s); |
1318 //my_syslog("child terminated a worker process"); | |
1319 exit(0); | 1324 exit(0); |
1320 } | 1325 } |
1321 else { | 1326 else { |
1322 // this is the parent | 1327 // this is the parent |
1323 // parent does not need the accepted socket | 1328 // parent does not need the accepted socket |