Debian Codegraph @

modules/contrib/src/ns_mxcheck.c

raw
1 #include "atheme-compat.h"
2
3 #ifdef HAVE_RES_QUERY
4
5 #ifdef HAVE_SYS_TYPES_H
6 # include <sys/types.h>
7 #endif
8 #ifdef HAVE_NETINET_IN_H
9 # include <netinet/in.h>
10 #endif
11 #ifdef HAVE_ARPA_NAMESER_H
12 # include <arpa/nameser.h>
13 #endif
14 #ifdef HAVE_NETDB_H
15 # include <netdb.h>
16 #endif
17 #ifdef HAVE_RESOLV_H
18 # include <resolv.h>
19 #endif
20
21 #ifndef C_ANY
22 # define C_ANY ns_c_any
23 #endif
24 #ifndef T_MX
25 # define T_MX ns_t_mx
26 #endif
27 #ifndef S_AN
28 # define S_AN ns_s_an
29 #endif
30
31 static int
32 count_mx(const char *host)
33 {
34 unsigned char nsbuf[4096];
35 ns_msg amsg;
36
37 if (! host || ! *host)
38 return 0;
39
40 const int len = res_query(host, C_ANY, T_MX, nsbuf, sizeof nsbuf);
41
42 if (len < 0)
43 return 0;
44
45 (void) ns_initparse(nsbuf, len, &amsg);
46
47 return ns_msg_count(amsg, S_AN);
48 }
49
50 static void
51 check_registration(hook_user_register_check_t *hdata)
52 {
53 char buf[1024];
54 const char *user;
55 const char *domain;
56 int count;
57
58 if (hdata->approved)
59 return;
60
61 mowgli_strlcpy(buf, hdata->email, sizeof buf);
62 user = strtok(buf, "@");
63 domain = strtok(NULL, "@");
64 count = count_mx(domain);
65
66 if (count > 0)
67 {
68 /* there are MX records for this domain */
69 slog(LG_INFO, "REGISTER: mxcheck: %d MX records for %s", count, domain);
70 }
71 else
72 {
73 /* no MX records or error */
74 struct hostent *host;
75
76 /* attempt to resolve host (fallback to A) */
77 if ((host = gethostbyname(domain)) == NULL)
78 {
79 slog(LG_INFO, "REGISTER: mxcheck: no A/MX records for %s - REGISTER failed", domain);
80 command_fail(hdata->si, fault_noprivs, "Sorry, \2%s\2 does not exist, I can't send mail "
81 "there. Please check and try again.", domain);
82 hdata->approved = 1;
83 return;
84 }
85 }
86 }
87
88 static void
89 mod_init(module_t *const restrict m)
90 {
91 hook_add_event("user_can_register");
92 hook_add_user_can_register(check_registration);
93 }
94
95 static void
96 mod_deinit(const module_unload_intent_t intent)
97 {
98 hook_del_user_can_register(check_registration);
99 }
100
101 #else /* HAVE_RES_QUERY */
102
103 static void
104 mod_init(module_t *const restrict m)
105 {
106 (void) slog(LG_ERROR, "%s: this module requires a functioning res_query(3); refusing to load", m->name);
107
108 m->mflags |= MODFLAG_FAIL;
109 }
110
111 static void
112 mod_deinit(const module_unload_intent_t intent)
113 {
114
115 }
116
117 #endif /* !HAVE_RES_QUERY */
118
119 VENDOR_DECLARE_MODULE_V1("contrib/ns_mxcheck", MODULE_UNLOAD_CAPABILITY_OK, CONTRIB_VENDOR_JAMIE_PENMAN)