Debian Codegraph @

modules/contrib/src/ns_mxcheck_async.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 #define MAX_CHILDPROCS 10
32
33 struct procdata
34 {
35 char name[COMPAT_NICKLEN + 1];
36 char email[COMPAT_EMAILLEN + 1];
37 };
38
39 static unsigned int proccount = 0;
40 static struct procdata procdata[MAX_CHILDPROCS];
41
42 static int
43 count_mx(const char *host)
44 {
45 unsigned char nsbuf[4096];
46 ns_msg amsg;
47
48 if (! host || ! *host)
49 return 0;
50
51 const int len = res_query(host, C_ANY, T_MX, nsbuf, sizeof nsbuf);
52
53 if (len < 0)
54 return 0;
55
56 (void) ns_initparse(nsbuf, len, &amsg);
57
58 return ns_msg_count(amsg, S_AN);
59 }
60
61 static void
62 childproc_cb(pid_t pid, int status, void *data)
63 {
64 struct procdata *pd = data;
65 myuser_t *mu;
66 const char *domain;
67
68 return_if_fail(proccount > 0);
69 proccount--;
70
71 if (! WIFEXITED(status))
72 return;
73
74 mu = myuser_find(pd->name);
75 if (! mu || strcmp(pd->email, mu->email))
76 return;
77
78 domain = strchr(pd->email, '@');
79 if (! domain)
80 return;
81
82 domain++;
83
84 if (WEXITSTATUS(status) == 1)
85 {
86 slog(LG_INFO, "REGISTER: mxcheck: no A/MX records for %s - REGISTER failed", domain);
87 myuser_notice(nicksvs.nick, mu, "Sorry, \2%s\2 does not exist, I can't send mail there. "
88 "Please check and try again.", domain);
89 atheme_object_unref(mu);
90 }
91 else if (WEXITSTATUS(status) == 0)
92 {
93 slog(LG_DEBUG, "REGISTER: mxcheck: valid MX records for %s", domain);
94 }
95 }
96
97 static void
98 check_registration(hook_user_register_check_t *hdata)
99 {
100 char buf[1024];
101 const char *user;
102 const char *domain;
103 int count;
104 pid_t pid;
105 struct procdata *pd;
106
107 if (hdata->approved)
108 return;
109
110 if (proccount >= MAX_CHILDPROCS)
111 {
112 command_fail(hdata->si, fault_toomany, "Sorry, too many registrations in progress. Try again later.");
113 hdata->approved = 1;
114 return;
115 }
116 switch (pid = fork())
117 {
118 case 0: /* child */
119 connection_close_all_fds();
120 mowgli_strlcpy(buf, hdata->email, sizeof buf);
121 user = strtok(buf, "@");
122 domain = strtok(NULL, "@");
123 count = count_mx(domain);
124
125 if (count <= 0)
126 {
127 /* no MX records or error */
128 struct hostent *host;
129
130 /* attempt to resolve host (fallback to A) */
131 if (! (host = gethostbyname(domain)))
132 _exit(1);
133 }
134 _exit(0);
135 break;
136 case -1: /* error */
137 slog(LG_ERROR, "fork() failed for check_registration(): %s", strerror(errno));
138 command_fail(hdata->si, fault_toomany,
139 "Sorry, too many registrations in progress. Try again later.");
140 hdata->approved = 1;
141 return;
142 default: /* parent */
143 pd = &procdata[proccount++];
144 mowgli_strlcpy(pd->name, hdata->account, sizeof pd->name);
145 mowgli_strlcpy(pd->email, hdata->email, sizeof pd->email);
146 childproc_add(pid, "ns_mxcheck_async", childproc_cb, pd);
147 return;
148 }
149 }
150
151 static void
152 mod_init(module_t *const restrict m)
153 {
154 hook_add_event("user_can_register");
155 hook_add_user_can_register(check_registration);
156 }
157
158 static void
159 mod_deinit(const module_unload_intent_t intent)
160 {
161 hook_del_user_can_register(check_registration);
162 childproc_delete_all(childproc_cb);
163 }
164
165 #else /* HAVE_RES_QUERY */
166
167 static void
168 mod_init(module_t *const restrict m)
169 {
170 (void) slog(LG_ERROR, "%s: this module requires a functioning res_query(3); refusing to load", m->name);
171
172 m->mflags |= MODFLAG_FAIL;
173 }
174
175 static void
176 mod_deinit(const module_unload_intent_t intent)
177 {
178
179 }
180
181 #endif /* !HAVE_RES_QUERY */
182
183 VENDOR_DECLARE_MODULE_V1("contrib/ns_mxcheck_async", MODULE_UNLOAD_CAPABILITY_OK, CONTRIB_VENDOR_JAMIE_PENMAN)