]>
Commit | Line | Data |
---|---|---|
fb7d74ef AC |
1 | /* |
2 | * authd.c: An interface to authd. | |
3 | * (based somewhat on ircd-ratbox dns.c) | |
4 | * | |
5 | * Copyright (C) 2005 Aaron Sethman <androsyn@ratbox.org> | |
6 | * Copyright (C) 2005-2012 ircd-ratbox development team | |
3fc0499e | 7 | * Copyright (C) 2016 Ariadne Conill <ariadne@dereferenced.org> |
fb7d74ef AC |
8 | * |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 | |
22 | * USA | |
23 | */ | |
24 | ||
d3f6b808 EM |
25 | #include "stdinc.h" |
26 | #include "rb_lib.h" | |
27 | #include "client.h" | |
28 | #include "ircd_defs.h" | |
29 | #include "parse.h" | |
64fae260 | 30 | #include "authproc.h" |
d3f6b808 EM |
31 | #include "match.h" |
32 | #include "logger.h" | |
33 | #include "s_conf.h" | |
34 | #include "s_stats.h" | |
35 | #include "client.h" | |
36 | #include "packet.h" | |
37 | #include "hash.h" | |
38 | #include "send.h" | |
39 | #include "numeric.h" | |
40 | #include "msg.h" | |
41 | #include "dns.h" | |
fb7d74ef | 42 | |
ae0a0585 EM |
43 | typedef void (*authd_cb_t)(int, char **); |
44 | ||
45 | struct authd_cb | |
46 | { | |
47 | authd_cb_t fn; | |
48 | int min_parc; | |
49 | }; | |
50 | ||
fb7d74ef AC |
51 | static int start_authd(void); |
52 | static void parse_authd_reply(rb_helper * helper); | |
53 | static void restart_authd_cb(rb_helper * helper); | |
ef0b13b9 | 54 | static EVH timeout_dead_authd_clients; |
fb7d74ef | 55 | |
ae0a0585 EM |
56 | static void cmd_accept_client(int parc, char **parv); |
57 | static void cmd_reject_client(int parc, char **parv); | |
58 | static void cmd_dns_result(int parc, char **parv); | |
59 | static void cmd_notice_client(int parc, char **parv); | |
60 | static void cmd_oper_warn(int parc, char **parv); | |
61 | static void cmd_stats_results(int parc, char **parv); | |
62 | ||
fb7d74ef AC |
63 | rb_helper *authd_helper; |
64 | static char *authd_path; | |
65 | ||
50808796 | 66 | uint32_t cid; |
d3f6b808 | 67 | static rb_dictionary *cid_clients; |
ef0b13b9 | 68 | static struct ev_entry *timeout_ev; |
d3f6b808 | 69 | |
3321eef4 | 70 | rb_dictionary *dnsbl_stats; |
d3f6b808 | 71 | |
5e9a3f86 EM |
72 | rb_dlink_list opm_list; |
73 | struct OPMListener opm_listeners[LISTEN_LAST]; | |
74 | ||
ae0a0585 EM |
75 | static struct authd_cb authd_cmd_tab[256] = |
76 | { | |
77 | ['A'] = { cmd_accept_client, 4 }, | |
78 | ['E'] = { cmd_dns_result, 5 }, | |
79 | ['N'] = { cmd_notice_client, 3 }, | |
80 | ['R'] = { cmd_reject_client, 7 }, | |
81 | ['W'] = { cmd_oper_warn, 3 }, | |
82 | ['X'] = { cmd_stats_results, 3 }, | |
83 | ['Y'] = { cmd_stats_results, 3 }, | |
84 | ['Z'] = { cmd_stats_results, 3 }, | |
85 | }; | |
86 | ||
fb7d74ef AC |
87 | static int |
88 | start_authd(void) | |
89 | { | |
90 | char fullpath[PATH_MAX + 1]; | |
8f0c3422 | 91 | |
fb7d74ef AC |
92 | if(authd_path == NULL) |
93 | { | |
8f0c3422 | 94 | snprintf(fullpath, sizeof(fullpath), "%s/authd", ircd_paths[IRCD_PATH_LIBEXEC]); |
fb7d74ef AC |
95 | |
96 | if(access(fullpath, X_OK) == -1) | |
97 | { | |
8f0c3422 | 98 | snprintf(fullpath, sizeof(fullpath), "%s/bin/authd", ConfigFileEntry.dpath); |
fb7d74ef AC |
99 | if(access(fullpath, X_OK) == -1) |
100 | { | |
7ad083b0 EM |
101 | ierror("Unable to execute authd in %s or %s/bin", |
102 | ircd_paths[IRCD_PATH_LIBEXEC], ConfigFileEntry.dpath); | |
a9227555 | 103 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, |
4d8cfacd AC |
104 | "Unable to execute authd in %s or %s/bin", |
105 | ircd_paths[IRCD_PATH_LIBEXEC], ConfigFileEntry.dpath); | |
fb7d74ef AC |
106 | return 1; |
107 | } | |
108 | ||
109 | } | |
110 | ||
111 | authd_path = rb_strdup(fullpath); | |
112 | } | |
113 | ||
d3f6b808 EM |
114 | if(cid_clients == NULL) |
115 | cid_clients = rb_dictionary_create("authd cid to uid mapping", rb_uint32cmp); | |
116 | ||
ef0b13b9 EM |
117 | if(timeout_ev == NULL) |
118 | timeout_ev = rb_event_addish("timeout_dead_authd_clients", timeout_dead_authd_clients, NULL, 1); | |
119 | ||
fb7d74ef AC |
120 | authd_helper = rb_helper_start("authd", authd_path, parse_authd_reply, restart_authd_cb); |
121 | ||
122 | if(authd_helper == NULL) | |
123 | { | |
7ad083b0 | 124 | ierror("Unable to start authd helper: %s", strerror(errno)); |
a9227555 | 125 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "Unable to start authd helper: %s", strerror(errno)); |
fb7d74ef AC |
126 | return 1; |
127 | } | |
e7c4ecd5 | 128 | |
fb7d74ef | 129 | ilog(L_MAIN, "authd helper started"); |
a9227555 | 130 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "authd helper started"); |
fb7d74ef AC |
131 | rb_helper_run(authd_helper); |
132 | return 0; | |
133 | } | |
134 | ||
6a7bb6f1 EM |
135 | static inline uint32_t |
136 | str_to_cid(const char *str) | |
137 | { | |
138 | long lcid = strtol(str, NULL, 16); | |
139 | ||
140 | if(lcid > UINT32_MAX || lcid <= 0) | |
141 | { | |
142 | iwarn("authd sent us back a bad client ID: %lx", lcid); | |
143 | restart_authd(); | |
144 | return 0; | |
145 | } | |
146 | ||
147 | return (uint32_t)lcid; | |
148 | } | |
149 | ||
150 | static inline struct Client * | |
5cbd46a8 | 151 | cid_to_client(uint32_t ncid, bool del) |
6a7bb6f1 EM |
152 | { |
153 | struct Client *client_p; | |
154 | ||
5cbd46a8 AJ |
155 | if(del) |
156 | client_p = rb_dictionary_delete(cid_clients, RB_UINT_TO_POINTER(ncid)); | |
6a7bb6f1 | 157 | else |
5cbd46a8 | 158 | client_p = rb_dictionary_retrieve(cid_clients, RB_UINT_TO_POINTER(ncid)); |
6a7bb6f1 | 159 | |
61d1befa EM |
160 | /* If the client's not found, that's okay, it may have already gone away. |
161 | * --Elizafox */ | |
6a7bb6f1 EM |
162 | |
163 | return client_p; | |
164 | } | |
165 | ||
adfe7b83 | 166 | static inline struct Client * |
5cbd46a8 | 167 | str_cid_to_client(const char *str, bool del) |
adfe7b83 | 168 | { |
5cbd46a8 | 169 | uint32_t ncid = str_to_cid(str); |
adfe7b83 | 170 | |
5cbd46a8 | 171 | if(ncid == 0) |
adfe7b83 EM |
172 | return NULL; |
173 | ||
5cbd46a8 | 174 | return cid_to_client(ncid, del); |
adfe7b83 EM |
175 | } |
176 | ||
fb7d74ef | 177 | static void |
ae0a0585 | 178 | cmd_accept_client(int parc, char **parv) |
fb7d74ef | 179 | { |
d3f6b808 | 180 | struct Client *client_p; |
6d0fafec | 181 | |
ae0a0585 EM |
182 | /* cid to uid (retrieve and delete) */ |
183 | if((client_p = str_cid_to_client(parv[1], true)) == NULL) | |
184 | return; | |
394b8dde | 185 | |
ae0a0585 EM |
186 | authd_accept_client(client_p, parv[2], parv[3]); |
187 | } | |
fb7d74ef | 188 | |
ae0a0585 EM |
189 | static void |
190 | cmd_dns_result(int parc, char **parv) | |
191 | { | |
192 | dns_results_callback(parv[1], parv[2], parv[3], parv[4]); | |
193 | } | |
d3f6b808 | 194 | |
ae0a0585 EM |
195 | static void |
196 | cmd_notice_client(int parc, char **parv) | |
197 | { | |
198 | struct Client *client_p; | |
d3f6b808 | 199 | |
1a16d47e EK |
200 | if ((client_p = str_cid_to_client(parv[1], false)) == NULL) |
201 | return; | |
202 | ||
203 | if (IsAnyDead(client_p)) | |
ae0a0585 | 204 | return; |
d3f6b808 | 205 | |
ae0a0585 EM |
206 | sendto_one_notice(client_p, ":%s", parv[2]); |
207 | } | |
d3f6b808 | 208 | |
ae0a0585 EM |
209 | static void |
210 | cmd_reject_client(int parc, char **parv) | |
211 | { | |
212 | struct Client *client_p; | |
50808796 | 213 | |
ae0a0585 EM |
214 | /* cid to uid (retrieve and delete) */ |
215 | if((client_p = str_cid_to_client(parv[1], true)) == NULL) | |
216 | return; | |
cc4d3931 | 217 | |
ae0a0585 EM |
218 | authd_reject_client(client_p, parv[3], parv[4], toupper(*parv[2]), parv[5], parv[6]); |
219 | } | |
adfe7b83 | 220 | |
ae0a0585 EM |
221 | static void |
222 | cmd_oper_warn(int parc, char **parv) | |
223 | { | |
e2a8228f | 224 | switch(*parv[1]) |
ae0a0585 EM |
225 | { |
226 | case 'D': /* Debug */ | |
f085388a | 227 | sendto_realops_snomask(SNO_DEBUG, L_NETWIDE, "authd debug: %s", parv[2]); |
e2a8228f | 228 | idebug("authd: %s", parv[2]); |
ae0a0585 EM |
229 | break; |
230 | case 'I': /* Info */ | |
f085388a | 231 | sendto_realops_snomask(SNO_DEBUG, L_NETWIDE, "authd info: %s", parv[2]); |
e2a8228f | 232 | inotice("authd: %s", parv[2]); |
ae0a0585 EM |
233 | break; |
234 | case 'W': /* Warning */ | |
687f290a | 235 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "authd WARNING: %s", parv[2]); |
e2a8228f | 236 | iwarn("authd: %s", parv[2]); |
ae0a0585 EM |
237 | break; |
238 | case 'C': /* Critical (error) */ | |
687f290a | 239 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "authd CRITICAL: %s", parv[2]); |
e2a8228f | 240 | ierror("authd: %s", parv[2]); |
ae0a0585 EM |
241 | break; |
242 | default: /* idk */ | |
687f290a | 243 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "authd sent us an unknown oper notice type (%s): %s", parv[1], parv[2]); |
e2a8228f | 244 | ilog(L_MAIN, "authd unknown oper notice type (%s): %s", parv[1], parv[2]); |
ae0a0585 EM |
245 | break; |
246 | } | |
247 | } | |
0a659bf0 | 248 | |
ae0a0585 EM |
249 | static void |
250 | cmd_stats_results(int parc, char **parv) | |
251 | { | |
252 | /* Select by type */ | |
253 | switch(*parv[2]) | |
254 | { | |
255 | case 'D': | |
256 | /* parv[0] conveys status */ | |
257 | if(parc < 4) | |
258 | { | |
259 | iwarn("authd sent a result with wrong number of arguments: got %d", parc); | |
260 | restart_authd(); | |
261 | return; | |
262 | } | |
263 | dns_stats_results_callback(parv[1], parv[0], parc - 3, (const char **)&parv[3]); | |
264 | break; | |
265 | default: | |
266 | break; | |
267 | } | |
268 | } | |
269 | ||
270 | static void | |
271 | parse_authd_reply(rb_helper * helper) | |
272 | { | |
273 | ssize_t len; | |
274 | int parc; | |
6d0fafec | 275 | char buf[READBUF_SIZE]; |
33ded5fc | 276 | char *parv[MAXPARA]; |
0a659bf0 | 277 | |
6d0fafec | 278 | while((len = rb_helper_read(helper, buf, sizeof(buf))) > 0) |
ae0a0585 EM |
279 | { |
280 | struct authd_cb *cmd; | |
281 | ||
33ded5fc | 282 | parc = rb_string_to_array(buf, parv, sizeof(parv)); |
1729f46e | 283 | cmd = &authd_cmd_tab[(unsigned char)*parv[0]]; |
ae0a0585 EM |
284 | if(cmd->fn != NULL) |
285 | { | |
286 | if(cmd->min_parc > parc) | |
394b8dde | 287 | { |
ae0a0585 EM |
288 | iwarn("authd sent a result with wrong number of arguments: expected %d, got %d", |
289 | cmd->min_parc, parc); | |
394b8dde | 290 | restart_authd(); |
ae0a0585 | 291 | continue; |
394b8dde EM |
292 | } |
293 | ||
ae0a0585 | 294 | cmd->fn(parc, parv); |
fb7d74ef | 295 | } |
b0326abd EM |
296 | else |
297 | { | |
298 | iwarn("authd sent us a bad command type: %c", *parv[0]); | |
299 | restart_authd(); | |
300 | continue; | |
301 | } | |
fb7d74ef AC |
302 | } |
303 | } | |
304 | ||
305 | void | |
306 | init_authd(void) | |
307 | { | |
308 | if(start_authd()) | |
309 | { | |
7ad083b0 | 310 | ierror("Unable to start authd helper: %s", strerror(errno)); |
fb7d74ef AC |
311 | exit(0); |
312 | } | |
313 | } | |
314 | ||
d3f6b808 EM |
315 | void |
316 | configure_authd(void) | |
317 | { | |
5e9a3f86 | 318 | /* Timeouts */ |
d3f6b808 EM |
319 | set_authd_timeout("ident_timeout", GlobalSetOptions.ident_timeout); |
320 | set_authd_timeout("rdns_timeout", ConfigFileEntry.connect_timeout); | |
50808796 | 321 | set_authd_timeout("rbl_timeout", ConfigFileEntry.connect_timeout); |
5e9a3f86 | 322 | |
50808796 | 323 | ident_check_enable(!ConfigFileEntry.disable_auth); |
5e9a3f86 EM |
324 | |
325 | /* Configure OPM */ | |
326 | if(rb_dlink_list_length(&opm_list) > 0 && | |
327 | (opm_listeners[LISTEN_IPV4].ipaddr[0] != '\0' || | |
328 | opm_listeners[LISTEN_IPV6].ipaddr[0] != '\0')) | |
329 | { | |
330 | rb_dlink_node *ptr; | |
331 | ||
332 | if(opm_listeners[LISTEN_IPV4].ipaddr[0] != '\0') | |
333 | rb_helper_write(authd_helper, "O opm_listener %s %hu", | |
334 | opm_listeners[LISTEN_IPV4].ipaddr, opm_listeners[LISTEN_IPV4].port); | |
335 | ||
5e9a3f86 EM |
336 | if(opm_listeners[LISTEN_IPV6].ipaddr[0] != '\0') |
337 | rb_helper_write(authd_helper, "O opm_listener %s %hu", | |
338 | opm_listeners[LISTEN_IPV6].ipaddr, opm_listeners[LISTEN_IPV6].port); | |
5e9a3f86 EM |
339 | |
340 | RB_DLINK_FOREACH(ptr, opm_list.head) | |
341 | { | |
342 | struct OPMScanner *scanner = ptr->data; | |
343 | rb_helper_write(authd_helper, "O opm_scanner %s %hu", | |
344 | scanner->type, scanner->port); | |
345 | } | |
346 | ||
347 | opm_check_enable(true); | |
348 | } | |
349 | else | |
350 | opm_check_enable(false); | |
fbc97166 AJ |
351 | |
352 | /* Configure DNSBLs */ | |
353 | rb_dictionary_iter iter; | |
354 | struct DNSBLEntry *entry; | |
355 | RB_DICTIONARY_FOREACH(entry, &iter, dnsbl_stats) | |
356 | { | |
357 | rb_helper_write(authd_helper, "O rbl %s %hhu %s :%s", entry->host, | |
358 | entry->iptype, entry->filters, entry->reason); | |
359 | } | |
d3f6b808 EM |
360 | } |
361 | ||
e7c4ecd5 EM |
362 | static void |
363 | authd_free_client(struct Client *client_p) | |
364 | { | |
365 | if(client_p == NULL || client_p->preClient == NULL) | |
366 | return; | |
367 | ||
368 | if(client_p->preClient->auth.cid == 0) | |
369 | return; | |
370 | ||
371 | if(authd_helper != NULL) | |
372 | rb_helper_write(authd_helper, "E %x", client_p->preClient->auth.cid); | |
373 | ||
374 | client_p->preClient->auth.accepted = true; | |
375 | client_p->preClient->auth.cid = 0; | |
376 | } | |
377 | ||
378 | static void | |
379 | authd_free_client_cb(rb_dictionary_element *delem, void *unused) | |
380 | { | |
381 | struct Client *client_p = delem->data; | |
382 | authd_free_client(client_p); | |
383 | } | |
384 | ||
385 | void | |
386 | authd_abort_client(struct Client *client_p) | |
387 | { | |
388 | rb_dictionary_delete(cid_clients, RB_UINT_TO_POINTER(client_p->preClient->auth.cid)); | |
389 | authd_free_client(client_p); | |
390 | } | |
391 | ||
fb7d74ef AC |
392 | static void |
393 | restart_authd_cb(rb_helper * helper) | |
394 | { | |
f085388a U |
395 | iwarn("authd helper died - attempting to restart"); |
396 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "authdd helper died - attempting to restart"); | |
e1582810 | 397 | |
fb7d74ef AC |
398 | if(helper != NULL) |
399 | { | |
400 | rb_helper_close(helper); | |
401 | authd_helper = NULL; | |
402 | } | |
e1582810 | 403 | |
e7c4ecd5 EM |
404 | rb_dictionary_destroy(cid_clients, authd_free_client_cb, NULL); |
405 | cid_clients = NULL; | |
e1582810 | 406 | |
fb7d74ef | 407 | start_authd(); |
e2a8228f | 408 | configure_authd(); |
fb7d74ef AC |
409 | } |
410 | ||
411 | void | |
412 | restart_authd(void) | |
413 | { | |
5c5296c8 | 414 | ierror("authd restarting..."); |
fb7d74ef AC |
415 | restart_authd_cb(authd_helper); |
416 | } | |
417 | ||
418 | void | |
419 | rehash_authd(void) | |
420 | { | |
421 | rb_helper_write(authd_helper, "R"); | |
422 | } | |
423 | ||
424 | void | |
425 | check_authd(void) | |
426 | { | |
427 | if(authd_helper == NULL) | |
428 | restart_authd(); | |
429 | } | |
d3f6b808 EM |
430 | |
431 | static inline uint32_t | |
432 | generate_cid(void) | |
433 | { | |
434 | if(++cid == 0) | |
435 | cid = 1; | |
436 | ||
437 | return cid; | |
438 | } | |
439 | ||
440 | /* Basically when this is called we begin handing off the client to authd for | |
441 | * processing. authd "owns" the client until processing is finished, or we | |
442 | * timeout from authd. authd will make a decision whether or not to accept the | |
443 | * client, but it's up to other parts of the code (for now) to decide if we're | |
444 | * gonna accept the client and ignore authd's suggestion. | |
445 | * | |
446 | * --Elizafox | |
762468f8 SA |
447 | * |
448 | * If this is an SSL connection we must defer handing off the client for | |
449 | * reading until it is open and we have the certificate fingerprint, otherwise | |
450 | * it's possible for the client to immediately send data before authd completes | |
451 | * and before the status of the connection is communicated via ssld. This data | |
452 | * could then be processed too early by read_packet(). | |
d3f6b808 EM |
453 | */ |
454 | void | |
762468f8 | 455 | authd_initiate_client(struct Client *client_p, bool defer) |
d3f6b808 EM |
456 | { |
457 | char client_ipaddr[HOSTIPLEN+1]; | |
458 | char listen_ipaddr[HOSTIPLEN+1]; | |
459 | uint16_t client_port, listen_port; | |
460 | uint32_t authd_cid; | |
461 | ||
154dc91e | 462 | if(client_p->preClient == NULL || client_p->preClient->auth.cid != 0) |
d3f6b808 EM |
463 | return; |
464 | ||
154dc91e | 465 | authd_cid = client_p->preClient->auth.cid = generate_cid(); |
d3f6b808 EM |
466 | |
467 | /* Collisions are extremely unlikely, so disregard the possibility */ | |
50808796 | 468 | rb_dictionary_add(cid_clients, RB_UINT_TO_POINTER(authd_cid), client_p); |
d3f6b808 EM |
469 | |
470 | /* Retrieve listener and client IP's */ | |
471 | rb_inet_ntop_sock((struct sockaddr *)&client_p->preClient->lip, listen_ipaddr, sizeof(listen_ipaddr)); | |
472 | rb_inet_ntop_sock((struct sockaddr *)&client_p->localClient->ip, client_ipaddr, sizeof(client_ipaddr)); | |
473 | ||
474 | /* Retrieve listener and client ports */ | |
d86692fa EM |
475 | listen_port = ntohs(GET_SS_PORT(&client_p->preClient->lip)); |
476 | client_port = ntohs(GET_SS_PORT(&client_p->localClient->ip)); | |
d3f6b808 | 477 | |
762468f8 SA |
478 | if(defer) |
479 | client_p->preClient->auth.flags |= AUTHC_F_DEFERRED; | |
480 | ||
59d42a9f | 481 | /* Add a bit of a fudge factor... */ |
154dc91e | 482 | client_p->preClient->auth.timeout = rb_current_time() + ConfigFileEntry.connect_timeout + 10; |
d3f6b808 | 483 | |
c6ad9b0c SA |
484 | rb_helper_write(authd_helper, "C %x %s %hu %s %hu %x", authd_cid, listen_ipaddr, listen_port, client_ipaddr, client_port, |
485 | #ifdef HAVE_LIBSCTP | |
486 | IsSCTP(client_p) ? IPPROTO_SCTP : IPPROTO_TCP); | |
487 | #else | |
488 | IPPROTO_TCP); | |
489 | #endif | |
d3f6b808 EM |
490 | } |
491 | ||
762468f8 SA |
492 | static inline void |
493 | authd_read_client(struct Client *client_p) | |
494 | { | |
495 | /* | |
496 | * When a client has auth'ed, we want to start reading what it sends | |
497 | * us. This is what read_packet() does. | |
498 | * -- adrian | |
499 | * | |
500 | * Above comment was originally in s_auth.c, but moved here with below code. | |
501 | * --Elizafox | |
502 | */ | |
503 | rb_dlinkAddTail(client_p, &client_p->node, &global_client_list); | |
504 | read_packet(client_p->localClient->F, client_p); | |
505 | } | |
506 | ||
d3f6b808 EM |
507 | /* When this is called we have a decision on client acceptance. |
508 | * | |
762468f8 SA |
509 | * After this point authd no longer "owns" the client, but if |
510 | * it's flagged as deferred then we're still waiting for a call | |
511 | * to authd_deferred_client(). | |
d3f6b808 | 512 | */ |
b1a577f2 | 513 | static inline void |
d3f6b808 EM |
514 | authd_decide_client(struct Client *client_p, const char *ident, const char *host, bool accept, char cause, const char *data, const char *reason) |
515 | { | |
154dc91e | 516 | if(client_p->preClient == NULL || client_p->preClient->auth.cid == 0) |
d3f6b808 EM |
517 | return; |
518 | ||
519 | if(*ident != '*') | |
520 | { | |
521 | rb_strlcpy(client_p->username, ident, sizeof(client_p->username)); | |
e7c4cf63 | 522 | SetGotId(client_p); |
4f6119cd | 523 | ServerStats.is_asuc++; |
d3f6b808 EM |
524 | } |
525 | else | |
4f6119cd | 526 | ServerStats.is_abad++; /* s_auth used to do this, stay compatible */ |
d3f6b808 EM |
527 | |
528 | if(*host != '*') | |
529 | rb_strlcpy(client_p->host, host, sizeof(client_p->host)); | |
530 | ||
154dc91e | 531 | rb_dictionary_delete(cid_clients, RB_UINT_TO_POINTER(client_p->preClient->auth.cid)); |
6d5edc6f | 532 | |
154dc91e EM |
533 | client_p->preClient->auth.accepted = accept; |
534 | client_p->preClient->auth.cause = cause; | |
535 | client_p->preClient->auth.data = (data == NULL ? NULL : rb_strdup(data)); | |
536 | client_p->preClient->auth.reason = (reason == NULL ? NULL : rb_strdup(reason)); | |
537 | client_p->preClient->auth.cid = 0; | |
d3f6b808 | 538 | |
762468f8 SA |
539 | client_p->preClient->auth.flags |= AUTHC_F_COMPLETE; |
540 | if((client_p->preClient->auth.flags & AUTHC_F_DEFERRED) == 0) | |
541 | authd_read_client(client_p); | |
542 | } | |
543 | ||
544 | void | |
545 | authd_deferred_client(struct Client *client_p) | |
546 | { | |
547 | client_p->preClient->auth.flags &= ~AUTHC_F_DEFERRED; | |
548 | if(client_p->preClient->auth.flags & AUTHC_F_COMPLETE) | |
549 | authd_read_client(client_p); | |
d3f6b808 EM |
550 | } |
551 | ||
b1a577f2 EM |
552 | /* Convenience function to accept client */ |
553 | void | |
554 | authd_accept_client(struct Client *client_p, const char *ident, const char *host) | |
555 | { | |
556 | authd_decide_client(client_p, ident, host, true, '\0', NULL, NULL); | |
557 | } | |
558 | ||
559 | /* Convenience function to reject client */ | |
560 | void | |
561 | authd_reject_client(struct Client *client_p, const char *ident, const char *host, char cause, const char *data, const char *reason) | |
562 | { | |
563 | authd_decide_client(client_p, ident, host, false, cause, data, reason); | |
564 | } | |
565 | ||
ef0b13b9 EM |
566 | static void |
567 | timeout_dead_authd_clients(void *notused __unused) | |
568 | { | |
569 | rb_dictionary_iter iter; | |
0bb5d3f0 | 570 | struct Client *client_p; |
e7c4ecd5 EM |
571 | rb_dlink_list freelist = { NULL, NULL, 0 }; |
572 | rb_dlink_node *ptr, *nptr; | |
ef0b13b9 | 573 | |
0bb5d3f0 | 574 | RB_DICTIONARY_FOREACH(client_p, &iter, cid_clients) |
ef0b13b9 | 575 | { |
154dc91e | 576 | if(client_p->preClient->auth.timeout < rb_current_time()) |
e7c4ecd5 | 577 | { |
e7c4ecd5 EM |
578 | rb_dlinkAddAlloc(client_p, &freelist); |
579 | } | |
580 | } | |
581 | ||
582 | /* RB_DICTIONARY_FOREACH is not safe for deletion, so we do this crap */ | |
583 | RB_DLINK_FOREACH_SAFE(ptr, nptr, freelist.head) | |
584 | { | |
585 | client_p = ptr->data; | |
63ee1037 AJ |
586 | authd_abort_client(client_p); |
587 | rb_dlinkDestroy(ptr, &freelist); | |
ef0b13b9 EM |
588 | } |
589 | } | |
590 | ||
3321eef4 | 591 | /* Send a new DNSBL entry to authd */ |
d3f6b808 | 592 | void |
3321eef4 | 593 | add_dnsbl_entry(const char *host, const char *reason, uint8_t iptype, rb_dlink_list *filters) |
d3f6b808 EM |
594 | { |
595 | rb_dlink_node *ptr; | |
fbc97166 | 596 | struct DNSBLEntry *entry = rb_malloc(sizeof(*entry)); |
50808796 | 597 | char filterbuf[BUFSIZE] = "*"; |
d3f6b808 EM |
598 | size_t s = 0; |
599 | ||
3321eef4 AC |
600 | if(dnsbl_stats == NULL) |
601 | dnsbl_stats = rb_dictionary_create("dnsbl statistics", rb_strcasecmp); | |
e7c4ecd5 | 602 | |
d3f6b808 EM |
603 | /* Build a list of comma-separated values for authd. |
604 | * We don't check for validity - do it elsewhere. | |
605 | */ | |
606 | RB_DLINK_FOREACH(ptr, filters->head) | |
607 | { | |
608 | char *filter = ptr->data; | |
609 | size_t filterlen = strlen(filter) + 1; | |
610 | ||
611 | if(s + filterlen > sizeof(filterbuf)) | |
612 | break; | |
613 | ||
614 | snprintf(&filterbuf[s], sizeof(filterbuf) - s, "%s,", filter); | |
615 | ||
616 | s += filterlen; | |
617 | } | |
618 | ||
619 | if(s) | |
620 | filterbuf[s - 1] = '\0'; | |
621 | ||
fbc97166 AJ |
622 | entry->host = rb_strdup(host); |
623 | entry->reason = rb_strdup(reason); | |
624 | entry->filters = rb_strdup(filterbuf); | |
625 | entry->iptype = iptype; | |
626 | entry->hits = 0; | |
d3f6b808 | 627 | |
fbc97166 | 628 | rb_dictionary_add(dnsbl_stats, entry->host, entry); |
d3f6b808 EM |
629 | rb_helper_write(authd_helper, "O rbl %s %hhu %s :%s", host, iptype, filterbuf, reason); |
630 | } | |
631 | ||
3321eef4 | 632 | /* Delete a DNSBL entry. */ |
d3f6b808 | 633 | void |
3321eef4 | 634 | del_dnsbl_entry(const char *host) |
d3f6b808 | 635 | { |
fbc97166 AJ |
636 | struct DNSBLEntry *entry = rb_dictionary_retrieve(dnsbl_stats, host); |
637 | ||
638 | if(entry != NULL) | |
5e9a3f86 | 639 | { |
fbc97166 AJ |
640 | rb_dictionary_delete(dnsbl_stats, entry->host); |
641 | rb_free(entry->host); | |
642 | rb_free(entry->reason); | |
643 | rb_free(entry->filters); | |
644 | rb_free(entry); | |
5e9a3f86 | 645 | } |
d3f6b808 EM |
646 | |
647 | rb_helper_write(authd_helper, "O rbl_del %s", host); | |
648 | } | |
649 | ||
e7c4ecd5 | 650 | static void |
3321eef4 | 651 | dnsbl_delete_elem(rb_dictionary_element *delem, void *unused) |
e7c4ecd5 | 652 | { |
fbc97166 | 653 | struct DNSBLEntry *entry = delem->data; |
e7c4ecd5 | 654 | |
fbc97166 AJ |
655 | rb_free(entry->host); |
656 | rb_free(entry->reason); | |
657 | rb_free(entry->filters); | |
658 | rb_free(entry); | |
e7c4ecd5 EM |
659 | } |
660 | ||
3321eef4 | 661 | /* Delete all the DNSBL entries. */ |
d3f6b808 | 662 | void |
3321eef4 | 663 | del_dnsbl_entry_all(void) |
d3f6b808 | 664 | { |
3321eef4 AC |
665 | if(dnsbl_stats != NULL) |
666 | rb_dictionary_destroy(dnsbl_stats, dnsbl_delete_elem, NULL); | |
667 | dnsbl_stats = NULL; | |
d3f6b808 EM |
668 | |
669 | rb_helper_write(authd_helper, "O rbl_del_all"); | |
670 | } | |
671 | ||
672 | /* Adjust an authd timeout value */ | |
4f6119cd | 673 | bool |
d3f6b808 EM |
674 | set_authd_timeout(const char *key, int timeout) |
675 | { | |
4f6119cd EM |
676 | if(timeout <= 0) |
677 | return false; | |
678 | ||
d3f6b808 | 679 | rb_helper_write(authd_helper, "O %s %d", key, timeout); |
4f6119cd | 680 | return true; |
d3f6b808 | 681 | } |
ad043803 | 682 | |
4f6119cd | 683 | /* Enable identd checks */ |
ad043803 EM |
684 | void |
685 | ident_check_enable(bool enabled) | |
686 | { | |
687 | rb_helper_write(authd_helper, "O ident_enabled %d", enabled ? 1 : 0); | |
688 | } | |
4f6119cd | 689 | |
5e9a3f86 EM |
690 | /* Create an OPM listener |
691 | * XXX - This is a big nasty hack, but it avoids resending duplicate data when | |
692 | * configure_authd() is called. | |
693 | */ | |
694 | void | |
695 | conf_create_opm_listener(const char *ip, uint16_t port) | |
696 | { | |
697 | char ipbuf[HOSTIPLEN]; | |
698 | struct OPMListener *listener; | |
699 | ||
700 | rb_strlcpy(ipbuf, ip, sizeof(ipbuf)); | |
701 | if(ipbuf[0] == ':') | |
702 | { | |
703 | memmove(ipbuf + 1, ipbuf, sizeof(ipbuf) - 1); | |
704 | ipbuf[0] = '0'; | |
705 | } | |
706 | ||
707 | /* I am much too lazy to use rb_inet_pton and GET_SS_FAMILY for now --Elizafox */ | |
708 | listener = &opm_listeners[(strchr(ipbuf, ':') != NULL ? LISTEN_IPV6 : LISTEN_IPV4)]; | |
709 | rb_strlcpy(listener->ipaddr, ipbuf, sizeof(listener->ipaddr)); | |
710 | listener->port = port; | |
711 | } | |
712 | ||
b1a577f2 | 713 | void |
34f16c46 | 714 | create_opm_listener(const char *ip, uint16_t port) |
4f6119cd | 715 | { |
d9364d29 | 716 | char ipbuf[HOSTIPLEN]; |
5e9a3f86 EM |
717 | |
718 | /* XXX duplicated in conf_create_opm_listener */ | |
d9364d29 EM |
719 | rb_strlcpy(ipbuf, ip, sizeof(ipbuf)); |
720 | if(ipbuf[0] == ':') | |
721 | { | |
722 | memmove(ipbuf + 1, ipbuf, sizeof(ipbuf) - 1); | |
723 | ipbuf[0] = '0'; | |
724 | } | |
725 | ||
5e9a3f86 | 726 | conf_create_opm_listener(ip, port); |
9e5c31ea | 727 | rb_helper_write(authd_helper, "O opm_listener %s %hu", ipbuf, port); |
5e9a3f86 EM |
728 | } |
729 | ||
730 | void | |
731 | delete_opm_listener_all(void) | |
732 | { | |
733 | memset(&opm_listeners, 0, sizeof(opm_listeners)); | |
734 | rb_helper_write(authd_helper, "O opm_listener_del_all"); | |
b1a577f2 EM |
735 | } |
736 | ||
737 | /* Disable all OPM scans */ | |
738 | void | |
739 | opm_check_enable(bool enabled) | |
740 | { | |
3d2fc110 | 741 | rb_helper_write(authd_helper, "O opm_enabled %d", enabled ? 1 : 0); |
b1a577f2 EM |
742 | } |
743 | ||
5e9a3f86 EM |
744 | /* Create an OPM proxy scanner |
745 | * XXX - This is a big nasty hack, but it avoids resending duplicate data when | |
746 | * configure_authd() is called. | |
747 | */ | |
748 | void | |
749 | conf_create_opm_proxy_scanner(const char *type, uint16_t port) | |
750 | { | |
751 | struct OPMScanner *scanner = rb_malloc(sizeof(struct OPMScanner)); | |
752 | ||
753 | rb_strlcpy(scanner->type, type, sizeof(scanner->type)); | |
754 | scanner->port = port; | |
755 | rb_dlinkAdd(scanner, &scanner->node, &opm_list); | |
756 | } | |
757 | ||
b1a577f2 | 758 | void |
51fa2ab8 | 759 | create_opm_proxy_scanner(const char *type, uint16_t port) |
b1a577f2 | 760 | { |
5e9a3f86 | 761 | conf_create_opm_proxy_scanner(type, port); |
51fa2ab8 | 762 | rb_helper_write(authd_helper, "O opm_scanner %s %hu", type, port); |
4f6119cd | 763 | } |
3d2fc110 EM |
764 | |
765 | void | |
766 | delete_opm_proxy_scanner(const char *type, uint16_t port) | |
767 | { | |
5e9a3f86 EM |
768 | rb_dlink_node *ptr, *nptr; |
769 | ||
770 | RB_DLINK_FOREACH_SAFE(ptr, nptr, opm_list.head) | |
771 | { | |
772 | struct OPMScanner *scanner = ptr->data; | |
773 | ||
774 | if(rb_strncasecmp(scanner->type, type, sizeof(scanner->type)) == 0 && | |
775 | scanner->port == port) | |
776 | { | |
777 | rb_dlinkDelete(ptr, &opm_list); | |
778 | rb_free(scanner); | |
779 | break; | |
780 | } | |
781 | } | |
782 | ||
3d2fc110 EM |
783 | rb_helper_write(authd_helper, "O opm_scanner_del %s %hu", type, port); |
784 | } | |
785 | ||
786 | void | |
787 | delete_opm_proxy_scanner_all(void) | |
788 | { | |
5e9a3f86 EM |
789 | rb_dlink_node *ptr, *nptr; |
790 | ||
791 | RB_DLINK_FOREACH_SAFE(ptr, nptr, opm_list.head) | |
792 | { | |
793 | struct OPMScanner *scanner = ptr->data; | |
794 | ||
795 | rb_dlinkDelete(ptr, &opm_list); | |
796 | rb_free(scanner); | |
797 | } | |
798 | ||
3d2fc110 EM |
799 | rb_helper_write(authd_helper, "O opm_scanner_del_all"); |
800 | } |