| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#include "mod_dnsbl.h" |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
#undef C_ANY |
| 36 |
|
| 37 |
#include <arpa/nameser.h> |
| 38 |
#include <resolv.h> |
| 39 |
|
| 40 |
module dnsbl_module; |
| 41 |
|
| 42 |
static int dnsbl_engine = FALSE; |
| 43 |
static int dnsbl_logfd = -1; |
| 44 |
|
| 45 |
static const char *trace_channel = "dnsbl"; |
| 46 |
|
| 47 |
|
| 48 |
static int dnsbl_sess_init(void); |
| 49 |
|
| 50 |
typedef enum { |
| 51 |
DNSBL_POLICY_ALLOW_DENY, |
| 52 |
DNSBL_POLICY_DENY_ALLOW |
| 53 |
|
| 54 |
} dnsbl_policy_e; |
| 55 |
|
| 56 |
static const char *reverse_ip_addr(pool *p, const char *ip_addr) { |
| 57 |
char *addr2, *res, *tmp; |
| 58 |
size_t addrlen; |
| 59 |
|
| 60 |
if (p == NULL || |
| 61 |
ip_addr == NULL) { |
| 62 |
errno = EINVAL; |
| 63 |
return NULL; |
| 64 |
} |
| 65 |
|
| 66 |
addrlen = strlen(ip_addr) +1; |
| 67 |
|
| 68 |
res = pcalloc(p, addrlen); |
| 69 |
addr2 = pstrdup(p, ip_addr); |
| 70 |
|
| 71 |
tmp = strrchr(addr2, '.'); |
| 72 |
sstrcat(res, tmp+1, addrlen); |
| 73 |
sstrcat(res, ".", addrlen); |
| 74 |
*tmp = '\0'; |
| 75 |
|
| 76 |
tmp = strrchr(addr2, '.'); |
| 77 |
sstrcat(res, tmp+1, addrlen); |
| 78 |
sstrcat(res, ".", addrlen); |
| 79 |
*tmp = '\0'; |
| 80 |
|
| 81 |
tmp = strrchr(addr2, '.'); |
| 82 |
sstrcat(res, tmp+1, addrlen); |
| 83 |
sstrcat(res, ".", addrlen); |
| 84 |
*tmp = '\0'; |
| 85 |
|
| 86 |
sstrcat(res, addr2, addrlen); |
| 87 |
return res; |
| 88 |
} |
| 89 |
|
| 90 |
static const char *get_reversed_addr(pool *p) { |
| 91 |
const char *ipstr = NULL; |
| 92 |
|
| 93 |
if (pr_netaddr_get_family(session.c->remote_addr) == AF_INET) { |
| 94 |
ipstr = pr_netaddr_get_ipstr(session.c->remote_addr); |
| 95 |
|
| 96 |
#ifdef PR_USE_IPV6 |
| 97 |
} else { |
| 98 |
if (pr_netaddr_use_ipv6() && |
| 99 |
pr_netaddr_get_family(session.c->remote_addr) == AF_INET6 && |
| 100 |
pr_netaddr_is_v4mappedv6(session.c->remote_addr) == TRUE) { |
| 101 |
const char *ipv6str = pr_netaddr_get_ipstr(session.c->remote_addr); |
| 102 |
pr_netaddr_t *tmp = pr_netaddr_alloc(p); |
| 103 |
|
| 104 |
pr_netaddr_set_family(tmp, AF_INET); |
| 105 |
pr_netaddr_set_port(tmp, pr_netaddr_get_port(session.c->remote_addr)); |
| 106 |
memcpy(&tmp->na_addr.v4.sin_addr, |
| 107 |
(((char *) pr_netaddr_get_inaddr(session.c->remote_addr)) + 12), |
| 108 |
sizeof(struct in_addr)); |
| 109 |
|
| 110 |
ipstr = pr_netaddr_get_ipstr(tmp); |
| 111 |
|
| 112 |
(void) pr_log_writefile(dnsbl_logfd, MOD_DNSBL_VERSION, |
| 113 |
"client address '%s' is an IPv4-mapped IPv6 address, treating it as " |
| 114 |
"IPv4 address '%s'", ipv6str, ipstr); |
| 115 |
|
| 116 |
} else { |
| 117 |
return NULL; |
| 118 |
} |
| 119 |
#endif |
| 120 |
} |
| 121 |
|
| 122 |
return reverse_ip_addr(p, ipstr); |
| 123 |
} |
| 124 |
|
| 125 |
static void lookup_reason(pool *p, const char *name) { |
| 126 |
int reasonlen; |
| 127 |
unsigned char reason[NS_PACKETSZ]; |
| 128 |
|
| 129 |
reasonlen = res_query(name, ns_c_in, ns_t_txt, reason, sizeof(reason)); |
| 130 |
if (reasonlen > 0) { |
| 131 |
ns_msg handle; |
| 132 |
int rrno; |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
if (ns_initparse(reason, reasonlen, &handle) < 0) { |
| 139 |
(void) pr_log_writefile(dnsbl_logfd, MOD_DNSBL_VERSION, |
| 140 |
"error initialising nameserver response parser: %s", strerror(errno)); |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
for (rrno = 0; rrno < ns_msg_count(handle, ns_s_an); rrno++) { |
| 145 |
ns_rr rr; |
| 146 |
|
| 147 |
if (ns_parserr(&handle, ns_s_an, rrno, &rr) < 0) { |
| 148 |
(void) pr_log_writefile(dnsbl_logfd, MOD_DNSBL_VERSION, |
| 149 |
"error parsing resource record %d: %s", rrno, strerror(errno)); |
| 150 |
continue; |
| 151 |
} |
| 152 |
|
| 153 |
if (ns_rr_type(rr) == ns_t_txt) { |
| 154 |
char *reject_reason; |
| 155 |
size_t len = ns_rr_rdlen(rr); |
| 156 |
|
| 157 |
reject_reason = pcalloc(p, len+1); |
| 158 |
memcpy(reject_reason, (unsigned char *) ns_rr_rdata(rr), len); |
| 159 |
|
| 160 |
(void) pr_log_writefile(dnsbl_logfd, MOD_DNSBL_VERSION, |
| 161 |
"reason for blacklisting client address: '%s'", reject_reason); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
static int lookup_addr(pool *p, const char *addr, const char *domain) { |
| 170 |
const pr_netaddr_t *reject_addr = NULL; |
| 171 |
const char *name = pstrcat(p, addr, ".", domain, NULL); |
| 172 |
|
| 173 |
(void) pr_log_writefile(dnsbl_logfd, MOD_DNSBL_VERSION, |
| 174 |
"for DNSBLDomain '%s', resolving DNS name '%s'", domain, name); |
| 175 |
|
| 176 |
reject_addr = pr_netaddr_get_addr(p, name, NULL); |
| 177 |
if (reject_addr) { |
| 178 |
(void) pr_log_writefile(dnsbl_logfd, MOD_DNSBL_VERSION, |
| 179 |
"found record for DNS name '%s', client address has been blacklisted", |
| 180 |
name); |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
lookup_reason(p, name); |
| 186 |
return -1; |
| 187 |
} |
| 188 |
|
| 189 |
(void) pr_log_writefile(dnsbl_logfd, MOD_DNSBL_VERSION, |
| 190 |
"no record returned for DNS name '%s', client address is not blacklisted", |
| 191 |
name); |
| 192 |
return 0; |
| 193 |
} |
| 194 |
|
| 195 |
static int dnsbl_reject_conn(void) { |
| 196 |
config_rec *c; |
| 197 |
pool *tmp_pool = NULL; |
| 198 |
const char *rev_ip_addr = NULL; |
| 199 |
int reject_conn = FALSE; |
| 200 |
dnsbl_policy_e policy = DNSBL_POLICY_DENY_ALLOW; |
| 201 |
|
| 202 |
c = find_config(main_server->conf, CONF_PARAM, "DNSBLPolicy", FALSE); |
| 203 |
if (c) { |
| 204 |
policy = *((dnsbl_policy_e *) c->argv[0]); |
| 205 |
} |
| 206 |
|
| 207 |
switch (policy) { |
| 208 |
case DNSBL_POLICY_ALLOW_DENY: |
| 209 |
pr_trace_msg(trace_channel, 8, |
| 210 |
"using policy of allowing connections unless listed by DNSBLDomains"); |
| 211 |
reject_conn = FALSE; |
| 212 |
break; |
| 213 |
|
| 214 |
case DNSBL_POLICY_DENY_ALLOW: |
| 215 |
pr_trace_msg(trace_channel, 8, |
| 216 |
"using policy of rejecting connections unless listed by DNSBLDomains"); |
| 217 |
reject_conn = TRUE; |
| 218 |
break; |
| 219 |
} |
| 220 |
|
| 221 |
tmp_pool = make_sub_pool(permanent_pool); |
| 222 |
rev_ip_addr = get_reversed_addr(tmp_pool); |
| 223 |
if (rev_ip_addr == NULL) { |
| 224 |
(void) pr_log_writefile(dnsbl_logfd, MOD_DNSBL_VERSION, |
| 225 |
"client address '%s' is an IPv6 address, skipping", |
| 226 |
pr_netaddr_get_ipstr(session.c->remote_addr)); |
| 227 |
destroy_pool(tmp_pool); |
| 228 |
return -1; |
| 229 |
} |
| 230 |
|
| 231 |
switch (policy) { |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
case DNSBL_POLICY_ALLOW_DENY: { |
| 236 |
c = find_config(main_server->conf, CONF_PARAM, "DNSBLDomain", FALSE); |
| 237 |
while (c != NULL) { |
| 238 |
const char *domain; |
| 239 |
|
| 240 |
pr_signals_handle(); |
| 241 |
|
| 242 |
domain = c->argv[0]; |
| 243 |
|
| 244 |
if (lookup_addr(tmp_pool, rev_ip_addr, domain) < 0) { |
| 245 |
(void) pr_log_writefile(dnsbl_logfd, MOD_DNSBL_VERSION, |
| 246 |
"client address '%s' is listed by DNSBLDomain '%s', rejecting " |
| 247 |
"connection", pr_netaddr_get_ipstr(session.c->remote_addr), domain); |
| 248 |
reject_conn = TRUE; |
| 249 |
break; |
| 250 |
} |
| 251 |
|
| 252 |
c = find_config_next(c, c->next, CONF_PARAM, "DNSBLDomain", FALSE); |
| 253 |
} |
| 254 |
|
| 255 |
break; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
case DNSBL_POLICY_DENY_ALLOW: { |
| 262 |
c = find_config(main_server->conf, CONF_PARAM, "DNSBLDomain", FALSE); |
| 263 |
while (c) { |
| 264 |
const char *domain; |
| 265 |
|
| 266 |
pr_signals_handle(); |
| 267 |
|
| 268 |
domain = c->argv[0]; |
| 269 |
|
| 270 |
if (lookup_addr(tmp_pool, rev_ip_addr, domain) < 0) { |
| 271 |
(void) pr_log_writefile(dnsbl_logfd, MOD_DNSBL_VERSION, |
| 272 |
"client address '%s' is listed by DNSBLDomain '%s', allowing " |
| 273 |
"connection", pr_netaddr_get_ipstr(session.c->remote_addr), domain); |
| 274 |
reject_conn = FALSE; |
| 275 |
break; |
| 276 |
} |
| 277 |
|
| 278 |
c = find_config_next(c, c->next, CONF_PARAM, "DNSBLDomain", FALSE); |
| 279 |
} |
| 280 |
|
| 281 |
break; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
destroy_pool(tmp_pool); |
| 286 |
|
| 287 |
if (reject_conn) { |
| 288 |
return TRUE; |
| 289 |
} |
| 290 |
|
| 291 |
return FALSE; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
MODRET set_dnsbldomain(cmd_rec *cmd) { |
| 299 |
char *domain; |
| 300 |
config_rec *c; |
| 301 |
CHECK_ARGS(cmd, 1); |
| 302 |
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL); |
| 303 |
|
| 304 |
domain = cmd->argv[1]; |
| 305 |
|
| 306 |
|
| 307 |
if (*domain == '.') |
| 308 |
domain++; |
| 309 |
|
| 310 |
c = add_config_param_str(cmd->argv[0], 1, domain); |
| 311 |
c->flags |= CF_MERGEDOWN_MULTI; |
| 312 |
|
| 313 |
return PR_HANDLED(cmd); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
MODRET set_dnsblengine(cmd_rec *cmd) { |
| 318 |
int engine; |
| 319 |
config_rec *c; |
| 320 |
|
| 321 |
CHECK_ARGS(cmd, 1); |
| 322 |
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL); |
| 323 |
|
| 324 |
engine = get_boolean(cmd, 1); |
| 325 |
if (engine == -1) { |
| 326 |
CONF_ERROR(cmd, "expected Boolean parameter"); |
| 327 |
} |
| 328 |
|
| 329 |
c = add_config_param(cmd->argv[0], 1, NULL); |
| 330 |
c->argv[0] = pcalloc(c->pool, sizeof(unsigned int)); |
| 331 |
*((unsigned int *) c->argv[0]) = engine; |
| 332 |
|
| 333 |
return PR_HANDLED(cmd); |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
MODRET set_dnsbllog(cmd_rec *cmd) { |
| 338 |
CHECK_ARGS(cmd, 1); |
| 339 |
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL); |
| 340 |
|
| 341 |
if (pr_fs_valid_path(cmd->argv[1]) < 0) |
| 342 |
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": ", cmd->argv[1], |
| 343 |
" is not a valid path", NULL)); |
| 344 |
|
| 345 |
(void) add_config_param_str(cmd->argv[0], 1, cmd->argv[1]); |
| 346 |
return PR_HANDLED(cmd); |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
MODRET set_dnsblpolicy(cmd_rec *cmd) { |
| 351 |
dnsbl_policy_e policy = DNSBL_POLICY_ALLOW_DENY; |
| 352 |
config_rec *c; |
| 353 |
|
| 354 |
CHECK_ARGS(cmd, 1); |
| 355 |
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL); |
| 356 |
|
| 357 |
if (strcasecmp(cmd->argv[1], "allow,deny") == 0) { |
| 358 |
policy = DNSBL_POLICY_ALLOW_DENY; |
| 359 |
|
| 360 |
} else if (strcasecmp(cmd->argv[1], "deny,allow") == 0) { |
| 361 |
policy = DNSBL_POLICY_DENY_ALLOW; |
| 362 |
|
| 363 |
} else { |
| 364 |
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": '", cmd->argv[1], |
| 365 |
"' is not one of the approved DNSBLPolicy settings", NULL)); |
| 366 |
} |
| 367 |
|
| 368 |
c = add_config_param(cmd->argv[0], 1, NULL); |
| 369 |
c->argv[0] = pcalloc(c->pool, sizeof(dnsbl_policy_e)); |
| 370 |
*((dnsbl_policy_e *) c->argv[0]) = policy; |
| 371 |
|
| 372 |
return PR_HANDLED(cmd); |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
static void dnsbl_sess_reinit_ev(const void *event_data, void *user_data) { |
| 382 |
int res; |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
pr_event_unregister(&dnsbl_module, "core.session-reinit", |
| 387 |
dnsbl_sess_reinit_ev); |
| 388 |
|
| 389 |
(void) close(dnsbl_logfd); |
| 390 |
dnsbl_logfd = -1; |
| 391 |
|
| 392 |
res = dnsbl_sess_init(); |
| 393 |
if (res < 0) { |
| 394 |
pr_session_disconnect(&dnsbl_module, |
| 395 |
PR_SESS_DISCONNECT_SESSION_INIT_FAILED, NULL); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
static int dnsbl_sess_init(void) { |
| 400 |
config_rec *c; |
| 401 |
|
| 402 |
pr_event_register(&dnsbl_module, "core.session-reinit", dnsbl_sess_reinit_ev, |
| 403 |
NULL); |
| 404 |
|
| 405 |
c = find_config(main_server->conf, CONF_PARAM, "DNSBLEngine", FALSE); |
| 406 |
if (c && |
| 407 |
*((unsigned int *) c->argv[0]) == TRUE) { |
| 408 |
dnsbl_engine = TRUE; |
| 409 |
|
| 410 |
} else { |
| 411 |
return 0; |
| 412 |
} |
| 413 |
|
| 414 |
c = find_config(main_server->conf, CONF_PARAM, "DNSBLLog", FALSE); |
| 415 |
if (c && |
| 416 |
strcasecmp(c->argv[0], "none") != 0) { |
| 417 |
int res, xerrno = 0; |
| 418 |
|
| 419 |
PRIVS_ROOT |
| 420 |
res = pr_log_openfile(c->argv[0], &dnsbl_logfd, 0600); |
| 421 |
xerrno = errno; |
| 422 |
PRIVS_RELINQUISH |
| 423 |
|
| 424 |
switch (res) { |
| 425 |
case -1: |
| 426 |
pr_log_pri(PR_LOG_NOTICE, MOD_DNSBL_VERSION |
| 427 |
": notice: unable to open DNSBLLog '%s': %s", (char *) c->argv[0], |
| 428 |
strerror(xerrno)); |
| 429 |
break; |
| 430 |
|
| 431 |
case PR_LOG_WRITABLE_DIR: |
| 432 |
pr_log_pri(PR_LOG_WARNING, MOD_DNSBL_VERSION |
| 433 |
": notice: unable to use DNSBLLog '%s': parent directory is " |
| 434 |
"world-writable", (char *) c->argv[0]); |
| 435 |
break; |
| 436 |
|
| 437 |
case PR_LOG_SYMLINK: |
| 438 |
pr_log_pri(PR_LOG_WARNING, MOD_DNSBL_VERSION |
| 439 |
": notice: unable to use DNSBLLog '%s': cannot log to a symlink", |
| 440 |
(char *) c->argv[0]); |
| 441 |
break; |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
if (dnsbl_reject_conn() == TRUE) { |
| 446 |
(void) pr_log_writefile(dnsbl_logfd, MOD_DNSBL_VERSION, |
| 447 |
"client not allowed by DNSBLPolicy, rejecting connection"); |
| 448 |
errno = EACCES; |
| 449 |
return -1; |
| 450 |
} |
| 451 |
|
| 452 |
return 0; |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
static conftable dnsbl_conftab[] = { |
| 459 |
{ "DNSBLDomain", set_dnsbldomain, NULL }, |
| 460 |
{ "DNSBLEngine", set_dnsblengine, NULL }, |
| 461 |
{ "DNSBLLog", set_dnsbllog, NULL }, |
| 462 |
{ "DNSBLPolicy", set_dnsblpolicy, NULL }, |
| 463 |
{ NULL } |
| 464 |
}; |
| 465 |
|
| 466 |
module dnsbl_module = { |
| 467 |
NULL, NULL, |
| 468 |
|
| 469 |
|
| 470 |
0x20, |
| 471 |
|
| 472 |
|
| 473 |
"dnsbl", |
| 474 |
|
| 475 |
|
| 476 |
dnsbl_conftab, |
| 477 |
|
| 478 |
|
| 479 |
NULL, |
| 480 |
|
| 481 |
|
| 482 |
NULL, |
| 483 |
|
| 484 |
|
| 485 |
NULL, |
| 486 |
|
| 487 |
|
| 488 |
dnsbl_sess_init, |
| 489 |
|
| 490 |
|
| 491 |
MOD_DNSBL_VERSION |
| 492 |
}; |