| 1 |
/* |
| 2 |
Copyright (C) 2002-2003 Erik Fears |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or |
| 5 |
modify it under the terms of the GNU General Public License |
| 6 |
as published by the Free Software Foundation; either version 2 |
| 7 |
of the License, or (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, write to the Free Software |
| 16 |
|
| 17 |
Foundation, Inc. |
| 18 |
59 Temple Place - Suite 330 |
| 19 |
Boston, MA 02111-1307, USA. |
| 20 |
|
| 21 |
*/ |
| 22 |
|
| 23 |
#include "setup.h" |
| 24 |
|
| 25 |
#include <stdio.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <string.h> |
| 28 |
#include <sys/types.h> |
| 29 |
#include <sys/socket.h> |
| 30 |
#include <netinet/in.h> |
| 31 |
#include <arpa/inet.h> |
| 32 |
#include <time.h> |
| 33 |
#include <errno.h> |
| 34 |
|
| 35 |
#include "compat.h" |
| 36 |
#include "config.h" |
| 37 |
#include "dnsbl.h" |
| 38 |
#include "list.h" |
| 39 |
#include "log.h" |
| 40 |
#include "main.h" |
| 41 |
#include "malloc.h" |
| 42 |
#include "match.h" |
| 43 |
#include "scan.h" |
| 44 |
#include "irc.h" |
| 45 |
#include "stats.h" |
| 46 |
|
| 47 |
|
| 48 |
/* |
| 49 |
* Work out the DNSBL zones and send the dns query |
| 50 |
*/ |
| 51 |
void |
| 52 |
dnsbl_add(struct scan_struct *ss) |
| 53 |
{ |
| 54 |
struct in_addr in; |
| 55 |
unsigned char a, b, c, d; |
| 56 |
char lookup[128]; |
| 57 |
node_t *p; |
| 58 |
int res; |
| 59 |
struct dnsbl_scan *ds; |
| 60 |
|
| 61 |
|
| 62 |
if (inet_pton(AF_INET, ss->ip, &in) <= 0) |
| 63 |
{ |
| 64 |
log_printf("DNSBL -> Invalid address '%s', ignoring.", ss->ip); |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
d = (unsigned char)(in.s_addr >> 24) & 0xFF; |
| 69 |
c = (unsigned char)(in.s_addr >> 16) & 0xFF; |
| 70 |
b = (unsigned char)(in.s_addr >> 8) & 0xFF; |
| 71 |
a = (unsigned char) in.s_addr & 0xFF; |
| 72 |
|
| 73 |
LIST_FOREACH(p, OpmItem->blacklists->head) |
| 74 |
{ |
| 75 |
struct BlacklistConf *bl = p->data; |
| 76 |
|
| 77 |
#ifdef WORDS_BIGENDIAN |
| 78 |
snprintf(lookup, 128, "%d.%d.%d.%d.%s", a, b, c, d, bl->name); |
| 79 |
#else |
| 80 |
snprintf(lookup, 128, "%d.%d.%d.%d.%s", d, c, b, a, bl->name); |
| 81 |
#endif |
| 82 |
|
| 83 |
ds = MyMalloc(sizeof *ds); |
| 84 |
ds->ss = ss; |
| 85 |
ds->bl = bl; |
| 86 |
|
| 87 |
if (OPT_DEBUG) |
| 88 |
log_printf("DNSBL -> Passed '%s' to resolver", lookup); |
| 89 |
|
| 90 |
res = firedns_getip(FDNS_QRY_A, lookup, (void *) ds); |
| 91 |
|
| 92 |
if (res == -1 && fdns_errno != FDNS_ERR_FDLIMIT) |
| 93 |
{ |
| 94 |
log_printf("DNSBL -> Error sending dns lookup for '%s': %s", lookup, firedns_strerror(fdns_errno)); |
| 95 |
free(ds); |
| 96 |
} |
| 97 |
else |
| 98 |
++ss->scans; /* Increase scan count - one for each blacklist */ |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
static void |
| 103 |
dnsbl_positive(struct scan_struct *ss, struct BlacklistConf *bl, unsigned char type) |
| 104 |
{ |
| 105 |
char text_type[128] = ""; |
| 106 |
node_t *p; |
| 107 |
|
| 108 |
if (bl->type == A_BITMASK) |
| 109 |
{ |
| 110 |
LIST_FOREACH(p, bl->reply->head) |
| 111 |
{ |
| 112 |
struct BlacklistReplyConf *item = p->data; |
| 113 |
|
| 114 |
if (item->number & type) |
| 115 |
{ |
| 116 |
strncat(text_type, item->type, sizeof(text_type) - strlen(text_type) - 2); |
| 117 |
text_type[sizeof(text_type) - 2] = '\0'; |
| 118 |
|
| 119 |
strncat(text_type, ", ", sizeof(text_type) - strlen(text_type) - 1); |
| 120 |
text_type[sizeof(text_type) - 1] = '\0'; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
if (text_type[0]) |
| 125 |
*(strrchr(text_type, ',')) = '\0'; |
| 126 |
} |
| 127 |
else |
| 128 |
{ |
| 129 |
LIST_FOREACH(p, bl->reply->head) |
| 130 |
{ |
| 131 |
struct BlacklistReplyConf *item = p->data; |
| 132 |
|
| 133 |
if (item->number == type) |
| 134 |
{ |
| 135 |
strlcpy(text_type, item->type, sizeof(text_type)); |
| 136 |
break; |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
if (text_type[0] == '\0' && bl->ban_unknown == 0) |
| 142 |
{ |
| 143 |
if (OPT_DEBUG) |
| 144 |
log_printf("DNSBL -> Unknown result from BL zone %s (%d)", bl->name, type); |
| 145 |
|
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
if (ss->manual_target) |
| 150 |
irc_send("PRIVMSG %s :CHECK -> DNSBL -> %s appears in BL zone %s (%s)", |
| 151 |
ss->manual_target->name, ss->ip, bl->name, text_type); |
| 152 |
else if (!ss->positive) |
| 153 |
{ |
| 154 |
/* Only report it if no other scans have found positives yet. */ |
| 155 |
scan_positive(ss, (bl->kline[0] ? bl->kline : IRCItem->kline), text_type); |
| 156 |
|
| 157 |
irc_send_channels("DNSBL -> %s!%s@%s appears in BL zone %s (%s)", |
| 158 |
ss->irc_nick, ss->irc_username, ss->irc_hostname, bl->name, |
| 159 |
text_type); |
| 160 |
log_printf("DNSBL -> %s!%s@%s appears in BL zone %s (%s)", |
| 161 |
ss->irc_nick, ss->irc_username, ss->irc_hostname, bl->name, |
| 162 |
text_type); |
| 163 |
} |
| 164 |
|
| 165 |
/* Record stat */ |
| 166 |
stats_dnsblrecv(bl); |
| 167 |
} |
| 168 |
|
| 169 |
void |
| 170 |
dnsbl_result(struct firedns_result *res) |
| 171 |
{ |
| 172 |
struct dnsbl_scan *ds = res->info; |
| 173 |
|
| 174 |
if (OPT_DEBUG) |
| 175 |
{ |
| 176 |
if (ds->ss->manual_target) |
| 177 |
log_printf("DNSBL -> Lookup result for %s (%s) %d.%d.%d.%d (error: %d)", |
| 178 |
ds->ss->ip, |
| 179 |
res->lookup, |
| 180 |
(unsigned char)res->text[0], |
| 181 |
(unsigned char)res->text[1], |
| 182 |
(unsigned char)res->text[2], |
| 183 |
(unsigned char)res->text[3], fdns_errno); |
| 184 |
else |
| 185 |
log_printf("DNSBL -> Lookup result for %s!%s@%s (%s) %d.%d.%d.%d (error: %d)", |
| 186 |
ds->ss->irc_nick, |
| 187 |
ds->ss->irc_username, |
| 188 |
ds->ss->irc_hostname, |
| 189 |
res->lookup, |
| 190 |
(unsigned char)res->text[0], |
| 191 |
(unsigned char)res->text[1], |
| 192 |
(unsigned char)res->text[2], |
| 193 |
(unsigned char)res->text[3], fdns_errno); |
| 194 |
} |
| 195 |
|
| 196 |
/* Everything is OK */ |
| 197 |
if (res->text[0] == '\0' && fdns_errno == FDNS_ERR_NXDOMAIN) |
| 198 |
{ |
| 199 |
if (ds->ss->manual_target) |
| 200 |
irc_send("PRIVMSG %s :CHECK -> DNSBL -> %s does not appear in BL zone %s", |
| 201 |
ds->ss->manual_target->name, ds->ss->ip, |
| 202 |
(strlen(ds->ss->ip) < strlen(res->lookup)) ? (res->lookup + strlen(ds->ss->ip) + 1) : res->lookup); |
| 203 |
|
| 204 |
--ds->ss->scans; /* We are done with ss here */ |
| 205 |
scan_checkfinished(ds->ss); /* This could free ss, don't use ss after this point */ |
| 206 |
MyFree(ds); /* No longer need our information */ |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
/* Either an error, or a positive lookup */ |
| 211 |
if (fdns_errno == FDNS_ERR_NONE) |
| 212 |
dnsbl_positive(ds->ss, ds->bl, (unsigned char)res->text[3]); |
| 213 |
else |
| 214 |
{ |
| 215 |
log_printf("DNSBL -> Lookup error on %s: %s", res->lookup, |
| 216 |
firedns_strerror(fdns_errno)); |
| 217 |
|
| 218 |
if (fdns_errno != FDNS_ERR_TIMEOUT) |
| 219 |
irc_send_channels("DNSBL -> Lookup error on %s: %s", res->lookup, |
| 220 |
firedns_strerror(fdns_errno)); |
| 221 |
} |
| 222 |
|
| 223 |
/* Check if ss has any remaining scans */ |
| 224 |
--ds->ss->scans; /* We are done with ss here */ |
| 225 |
scan_checkfinished(ds->ss); /* This could free ss, don't use ss after this point */ |
| 226 |
MyFree(ds); /* Finished with dnsbl_scan too */ |
| 227 |
} |
| 228 |
|
| 229 |
void |
| 230 |
dnsbl_cycle(void) |
| 231 |
{ |
| 232 |
firedns_cycle(); |
| 233 |
} |
| 234 |
|
| 235 |
/* |
| 236 |
* Send an email to report this open proxy. |
| 237 |
*/ |
| 238 |
void |
| 239 |
dnsbl_report(struct scan_struct *ss) |
| 240 |
{ |
| 241 |
char buf[4096], cmdbuf[512]; |
| 242 |
FILE *fp; |
| 243 |
|
| 244 |
if (ss->ip == NULL) |
| 245 |
return; |
| 246 |
|
| 247 |
if (EmptyString(OpmItem->dnsbl_to) || EmptyString(OpmItem->dnsbl_from) || EmptyString(OpmItem->sendmail)) |
| 248 |
return; |
| 249 |
|
| 250 |
snprintf(cmdbuf, sizeof(cmdbuf), "%s -t", OpmItem->sendmail); |
| 251 |
snprintf(buf, sizeof(buf), |
| 252 |
"From: %s <%s>\n" |
| 253 |
"To: %s\n" |
| 254 |
"Subject: BOPM Report\n" |
| 255 |
"X-BOPM-Version: %s\n\n" |
| 256 |
"%s: %s:%d\n\n" |
| 257 |
"%s\n", IRCItem->nick, OpmItem->dnsbl_from, OpmItem->dnsbl_to, |
| 258 |
VERSION, scan_gettype(ss->remote->protocol), ss->ip, |
| 259 |
ss->remote->port, ss->proof); |
| 260 |
|
| 261 |
if (OPT_DEBUG >= 3) |
| 262 |
log_printf("DNSBL -> Sending following email:\n%s\n", buf); |
| 263 |
|
| 264 |
if ((fp = popen(cmdbuf, "w")) == NULL) |
| 265 |
{ |
| 266 |
log_printf("DNSBL -> Failed to create pipe to '%s' for email report!", cmdbuf); |
| 267 |
irc_send_channels("I was trying to create a pipe to'%s' to send a DNSBL " |
| 268 |
"report, and it failed! I'll give up for now.", |
| 269 |
cmdbuf); |
| 270 |
return; |
| 271 |
} |
| 272 |
|
| 273 |
fputs(buf, fp); |
| 274 |
pclose(fp); |
| 275 |
|
| 276 |
log_printf("DNSBL -> Sent report to %s [%s]", OpmItem->dnsbl_to, ss->ip); |
| 277 |
|
| 278 |
/* Record send in stats */ |
| 279 |
stats_dnsblsend(); |
| 280 |
} |