1 |
/** |
2 |
* ns_mxcheck.C: Check MX records for registering email. |
3 |
* |
4 |
* Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team |
5 |
* Rights to this code are as documented in COPYING. |
6 |
* |
7 |
* |
8 |
* Portions of this file were derived from sources bearing the following license: |
9 |
* Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team |
10 |
* Copyright © 2007 Atheme Development Group |
11 |
* Rights to this code are as documented in doc/pod/license.pod. |
12 |
* |
13 |
* $Id: ns_mxcheck.C,v 1.3 2007-08-30 19:56:22 pippijn Exp $ |
14 |
*/ |
15 |
|
16 |
#include <netinet/in.h> |
17 |
#include <arpa/nameser.h> |
18 |
#include <resolv.h> |
19 |
#include <netdb.h> |
20 |
|
21 |
#include "atheme.h" |
22 |
#include <ermyth/module.h> |
23 |
|
24 |
static char const rcsid[] = "$Id: ns_mxcheck.C,v 1.3 2007-08-30 19:56:22 pippijn Exp $"; |
25 |
|
26 |
REGISTER_MODULE ("nickserv/mxcheck", false, "Jamie L. Penman-Smithson <jamie@slacked.org>"); |
27 |
|
28 |
static int |
29 |
count_mx (char const * const host) |
30 |
{ |
31 |
u_char nsbuf[4096]; |
32 |
ns_msg amsg; |
33 |
int l; |
34 |
|
35 |
l = res_query (host, ns_c_any, ns_t_mx, nsbuf, sizeof (nsbuf)); |
36 |
|
37 |
if (l < 0) |
38 |
return 0; |
39 |
|
40 |
ns_initparse (nsbuf, l, &amsg); |
41 |
l = ns_msg_count (amsg, ns_s_an); |
42 |
|
43 |
return l; |
44 |
} |
45 |
|
46 |
static bool |
47 |
check_registration (sourceinfo_t *si, char const * const account, char const * const email) |
48 |
{ |
49 |
char buf[1024]; |
50 |
int count; |
51 |
|
52 |
strlcpy (buf, email, sizeof buf); |
53 |
/* char const * const user = */strtok (buf, "@"); |
54 |
char const * const domain = strtok (NULL, "@"); |
55 |
count = count_mx (domain); |
56 |
|
57 |
if (count > 0) |
58 |
/* there are MX records for this domain */ |
59 |
snoop ("REGISTER: mxcheck: %d MX records for %s", count, domain); |
60 |
else |
61 |
{ |
62 |
/* no MX records or error */ |
63 |
struct hostent *host; |
64 |
|
65 |
/* attempt to resolve host (fallback to A) */ |
66 |
if ((host = gethostbyname (domain)) == NULL) |
67 |
{ |
68 |
snoop ("REGISTER: mxcheck: no A/MX records for %s - " |
69 |
"REGISTER failed", domain); |
70 |
command_fail (si, fault::noprivs, "Sorry, \2%s\2 does not exist, " |
71 |
"I can't send mail there. Please check and try again.", domain); |
72 |
return false; |
73 |
} |
74 |
} |
75 |
|
76 |
return true; |
77 |
} |
78 |
|
79 |
bool |
80 |
_modinit (module *m) |
81 |
{ |
82 |
user_t::callback.can_register.attach (check_registration); |
83 |
|
84 |
return true; |
85 |
} |
86 |
|
87 |
void |
88 |
_moddeinit (void) |
89 |
{ |
90 |
user_t::callback.can_register.detach (check_registration); |
91 |
} |