]> jfr.im git - solanum.git/blame - ircd/authd.c
authd: It Works, Bitches™
[solanum.git] / ircd / authd.c
CommitLineData
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
7 * Copyright (C) 2016 William Pitcock <nenolod@dereferenced.org>
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"
30#include "authd.h"
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
AC
42
43static int start_authd(void);
44static void parse_authd_reply(rb_helper * helper);
45static void restart_authd_cb(rb_helper * helper);
ef0b13b9 46static EVH timeout_dead_authd_clients;
fb7d74ef
AC
47
48rb_helper *authd_helper;
49static char *authd_path;
50
50808796 51uint32_t cid;
d3f6b808 52static rb_dictionary *cid_clients;
ef0b13b9 53static struct ev_entry *timeout_ev;
d3f6b808
EM
54
55rb_dictionary *bl_stats;
56
fb7d74ef
AC
57static int
58start_authd(void)
59{
60 char fullpath[PATH_MAX + 1];
61#ifdef _WIN32
62 const char *suffix = ".exe";
63#else
64 const char *suffix = "";
65#endif
66 if(authd_path == NULL)
67 {
4d8cfacd 68 snprintf(fullpath, sizeof(fullpath), "%s%cauthd%s", ircd_paths[IRCD_PATH_LIBEXEC], RB_PATH_SEPARATOR, suffix);
fb7d74ef
AC
69
70 if(access(fullpath, X_OK) == -1)
71 {
4d8cfacd
AC
72 snprintf(fullpath, sizeof(fullpath), "%s%cbin%cauthd%s",
73 ConfigFileEntry.dpath, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR, suffix);
fb7d74ef
AC
74 if(access(fullpath, X_OK) == -1)
75 {
76 ilog(L_MAIN,
4d8cfacd
AC
77 "Unable to execute authd in %s or %s/bin",
78 ircd_paths[IRCD_PATH_LIBEXEC], ConfigFileEntry.dpath);
fb7d74ef 79 sendto_realops_snomask(SNO_GENERAL, L_ALL,
4d8cfacd
AC
80 "Unable to execute authd in %s or %s/bin",
81 ircd_paths[IRCD_PATH_LIBEXEC], ConfigFileEntry.dpath);
fb7d74ef
AC
82 return 1;
83 }
84
85 }
86
87 authd_path = rb_strdup(fullpath);
88 }
89
d3f6b808
EM
90 if(cid_clients == NULL)
91 cid_clients = rb_dictionary_create("authd cid to uid mapping", rb_uint32cmp);
92
93 if(bl_stats == NULL)
94 bl_stats = rb_dictionary_create("blacklist statistics", strcasecmp);
95
ef0b13b9
EM
96 if(timeout_ev == NULL)
97 timeout_ev = rb_event_addish("timeout_dead_authd_clients", timeout_dead_authd_clients, NULL, 1);
98
fb7d74ef
AC
99 authd_helper = rb_helper_start("authd", authd_path, parse_authd_reply, restart_authd_cb);
100
101 if(authd_helper == NULL)
102 {
103 ilog(L_MAIN, "Unable to start authd helper: %s", strerror(errno));
104 sendto_realops_snomask(SNO_GENERAL, L_ALL, "Unable to start authd helper: %s", strerror(errno));
105 return 1;
106 }
107 ilog(L_MAIN, "authd helper started");
108 sendto_realops_snomask(SNO_GENERAL, L_ALL, "authd helper started");
109 rb_helper_run(authd_helper);
110 return 0;
111}
112
113static void
114parse_authd_reply(rb_helper * helper)
115{
116 ssize_t len;
117 int parc;
50808796 118 char authdBuf[READBUF_SIZE];
fb7d74ef 119 char *parv[MAXPARA + 1];
d3f6b808 120 long lcid;
50808796 121 uint32_t cid;
d3f6b808 122 struct Client *client_p;
394b8dde 123
50808796 124 while((len = rb_helper_read(helper, authdBuf, sizeof(authdBuf))) > 0)
fb7d74ef 125 {
50808796 126 parc = rb_string_to_array(authdBuf, parv, MAXPARA+1);
fb7d74ef 127
3949459b 128 switch (*parv[0])
fb7d74ef 129 {
d3f6b808
EM
130 case 'A': /* Accepted a client */
131 if(parc != 4)
132 {
133 iwarn("authd sent a result with wrong number of arguments: got %d", parc);
134 restart_authd();
135 return;
136 }
137
50808796 138 if((lcid = strtol(parv[1], NULL, 16)) > UINT32_MAX || lcid < 0)
d3f6b808 139 {
50808796 140 iwarn("authd sent us back a bad client ID: %ld", lcid);
d3f6b808
EM
141 restart_authd();
142 return;
143 }
144
50808796
EM
145 cid = (uint32_t)lcid;
146
d3f6b808 147 /* cid to uid (retrieve and delete) */
50808796 148 if((client_p = rb_dictionary_delete(cid_clients, RB_UINT_TO_POINTER(cid))) == NULL)
d3f6b808 149 {
50808796 150 iwarn("authd sent us back an unknown client ID %x", cid);
d3f6b808
EM
151 restart_authd();
152 return;
153 }
154
d3f6b808
EM
155 authd_decide_client(client_p, parv[2], parv[3], true, '\0', NULL, NULL);
156 break;
157 case 'R': /* Reject client */
158 if(parc != 7)
159 {
160 iwarn("authd sent us a result with wrong number of arguments: got %d", parc);
161 restart_authd();
162 return;
163 }
164
50808796 165 if((lcid = strtol(parv[1], NULL, 16)) > UINT32_MAX || lcid < 0)
d3f6b808 166 {
50808796 167 iwarn("authd sent us back a bad client ID %ld", lcid);
d3f6b808
EM
168 restart_authd();
169 return;
170 }
171
50808796
EM
172 cid = (uint32_t)lcid;
173
d3f6b808 174 /* cid to uid (retrieve and delete) */
50808796 175 if((client_p = rb_dictionary_delete(cid_clients, RB_UINT_TO_POINTER(cid))) == NULL)
d3f6b808 176 {
50808796 177 iwarn("authd sent us back an unknown client ID %x", cid);
d3f6b808
EM
178 restart_authd();
179 return;
180 }
181
d3f6b808 182 authd_decide_client(client_p, parv[3], parv[4], false, toupper(*parv[2]), parv[5], parv[6]);
cc4d3931
EM
183 break;
184 case 'N': /* Notice to client */
ef0b13b9 185 if(parc != 3)
cc4d3931
EM
186 {
187 iwarn("authd sent us a result with wrong number of arguments: got %d", parc);
188 restart_authd();
189 return;
190 }
50808796
EM
191
192 if((lcid = strtol(parv[1], NULL, 16)) > UINT32_MAX || lcid < 0)
cc4d3931 193 {
50808796 194 iwarn("authd sent us back a bad client ID %ld", lcid);
cc4d3931
EM
195 restart_authd();
196 return;
197 }
198
50808796
EM
199 cid = (uint32_t)lcid;
200
cc4d3931 201 /* cid to uid */
50808796 202 if((client_p = rb_dictionary_retrieve(cid_clients, RB_UINT_TO_POINTER(cid))) == NULL)
cc4d3931 203 {
50808796 204 iwarn("authd sent us back an unknown client ID %x", cid);
cc4d3931
EM
205 restart_authd();
206 return;
207 }
208
cc4d3931
EM
209 sendto_one_notice(client_p, ":%s", parv[2]);
210 break;
d3f6b808 211 case 'E': /* DNS Result */
fb7d74ef
AC
212 if(parc != 5)
213 {
214 ilog(L_MAIN, "authd sent a result with wrong number of arguments: got %d", parc);
215 restart_authd();
216 return;
217 }
218 dns_results_callback(parv[1], parv[2], parv[3], parv[4]);
3949459b 219 break;
d3f6b808 220 case 'W': /* Oper warning */
0a659bf0
EM
221 if(parc != 3)
222 {
223 ilog(L_MAIN, "authd sent a result with wrong number of arguments: got %d", parc);
224 restart_authd();
225 return;
226 }
227
228 switch(*parv[2])
229 {
d3f6b808 230 case 'D': /* Debug */
0a659bf0
EM
231 sendto_realops_snomask(SNO_DEBUG, L_ALL, "authd debug: %s", parv[3]);
232 break;
938f93f4 233 case 'I': /* Info */
0a659bf0
EM
234 sendto_realops_snomask(SNO_GENERAL, L_ALL, "authd info: %s", parv[3]);
235 inotice("authd info: %s", parv[3]);
236 break;
938f93f4 237 case 'W': /* Warning */
0a659bf0
EM
238 sendto_realops_snomask(SNO_GENERAL, L_ALL, "authd WARNING: %s", parv[3]);
239 iwarn("authd warning: %s", parv[3]);
240 break;
938f93f4 241 case 'C': /* Critical (error) */
0a659bf0
EM
242 sendto_realops_snomask(SNO_GENERAL, L_ALL, "authd CRITICAL: %s", parv[3]);
243 ierror("authd critical: %s", parv[3]);
244 break;
938f93f4 245 default: /* idk */
0a659bf0
EM
246 sendto_realops_snomask(SNO_GENERAL, L_ALL, "authd sent us an unknown oper notice type (%s): %s", parv[2], parv[3]);
247 ilog(L_MAIN, "authd unknown oper notice type (%s): %s", parv[2], parv[3]);
248 break;
249 }
250
251 /* NOTREACHED */
252 break;
d3f6b808
EM
253 case 'X': /* Stats error */
254 case 'Y': /* Stats reply */
255 case 'Z': /* End of stats reply */
394b8dde
EM
256 if(parc < 3)
257 {
258 ilog(L_MAIN, "authd sent a result with wrong number of arguments: got %d", parc);
259 restart_authd();
260 return;
261 }
262
263 /* Select by type */
264 switch(*parv[2])
265 {
266 case 'D':
267 /* parv[0] conveys status */
268 if(parc < 4)
269 {
270 ilog(L_MAIN, "authd sent a result with wrong number of arguments: got %d", parc);
271 restart_authd();
272 return;
273 }
274 dns_stats_results_callback(parv[1], parv[0], parc - 3, (const char **)&parv[3]);
275 break;
276 default:
277 break;
278 }
279 break;
3949459b
AC
280 default:
281 break;
fb7d74ef 282 }
fb7d74ef
AC
283 }
284}
285
286void
287init_authd(void)
288{
289 if(start_authd())
290 {
291 ilog(L_MAIN, "Unable to start authd helper: %s", strerror(errno));
292 exit(0);
293 }
294}
295
d3f6b808
EM
296void
297configure_authd(void)
298{
299 /* These will do for now */
300 set_authd_timeout("ident_timeout", GlobalSetOptions.ident_timeout);
301 set_authd_timeout("rdns_timeout", ConfigFileEntry.connect_timeout);
50808796
EM
302 set_authd_timeout("rbl_timeout", ConfigFileEntry.connect_timeout);
303 ident_check_enable(!ConfigFileEntry.disable_auth);
d3f6b808
EM
304}
305
fb7d74ef
AC
306static void
307restart_authd_cb(rb_helper * helper)
308{
309 ilog(L_MAIN, "authd: restart_authd_cb called, authd died?");
3949459b 310 sendto_realops_snomask(SNO_GENERAL, L_ALL, "authd: restart_authd_cb called, authd died?");
fb7d74ef
AC
311 if(helper != NULL)
312 {
313 rb_helper_close(helper);
314 authd_helper = NULL;
315 }
316 start_authd();
317}
318
319void
320restart_authd(void)
321{
322 restart_authd_cb(authd_helper);
323}
324
325void
326rehash_authd(void)
327{
328 rb_helper_write(authd_helper, "R");
d3f6b808 329 configure_authd();
fb7d74ef
AC
330}
331
332void
333check_authd(void)
334{
335 if(authd_helper == NULL)
336 restart_authd();
337}
d3f6b808
EM
338
339static inline uint32_t
340generate_cid(void)
341{
342 if(++cid == 0)
343 cid = 1;
344
345 return cid;
346}
347
348/* Basically when this is called we begin handing off the client to authd for
349 * processing. authd "owns" the client until processing is finished, or we
350 * timeout from authd. authd will make a decision whether or not to accept the
351 * client, but it's up to other parts of the code (for now) to decide if we're
352 * gonna accept the client and ignore authd's suggestion.
353 *
354 * --Elizafox
355 */
356void
357authd_initiate_client(struct Client *client_p)
358{
359 char client_ipaddr[HOSTIPLEN+1];
360 char listen_ipaddr[HOSTIPLEN+1];
361 uint16_t client_port, listen_port;
362 uint32_t authd_cid;
363
50808796 364 if(client_p->preClient == NULL || client_p->preClient->authd_cid != 0)
d3f6b808
EM
365 return;
366
367 authd_cid = client_p->preClient->authd_cid = generate_cid();
368
369 /* Collisions are extremely unlikely, so disregard the possibility */
50808796 370 rb_dictionary_add(cid_clients, RB_UINT_TO_POINTER(authd_cid), client_p);
d3f6b808
EM
371
372 /* Retrieve listener and client IP's */
373 rb_inet_ntop_sock((struct sockaddr *)&client_p->preClient->lip, listen_ipaddr, sizeof(listen_ipaddr));
374 rb_inet_ntop_sock((struct sockaddr *)&client_p->localClient->ip, client_ipaddr, sizeof(client_ipaddr));
375
376 /* Retrieve listener and client ports */
377#ifdef RB_IPV6
378 if(GET_SS_FAMILY(&client_p->preClient->lip) == AF_INET6)
379 listen_port = ntohs(((struct sockaddr_in6 *)&client_p->preClient->lip)->sin6_port);
380 else
381#endif
382 listen_port = ntohs(((struct sockaddr_in *)&client_p->preClient->lip)->sin_port);
383
384#ifdef RB_IPV6
385 if(GET_SS_FAMILY(&client_p->localClient->ip) == AF_INET6)
386 client_port = ntohs(((struct sockaddr_in6 *)&client_p->localClient->ip)->sin6_port);
387 else
388#endif
389 client_port = ntohs(((struct sockaddr_in *)&client_p->localClient->ip)->sin_port);
390
59d42a9f
EM
391 /* Add a bit of a fudge factor... */
392 client_p->preClient->authd_timeout = rb_current_time() + ConfigFileEntry.connect_timeout + 5;
d3f6b808
EM
393
394 rb_helper_write(authd_helper, "C %x %s %hu %s %hu", authd_cid, listen_ipaddr, listen_port, client_ipaddr, client_port);
395}
396
397/* When this is called we have a decision on client acceptance.
398 *
399 * After this point authd no longer "owns" the client.
400 */
401void
402authd_decide_client(struct Client *client_p, const char *ident, const char *host, bool accept, char cause, const char *data, const char *reason)
403{
404 if(client_p->preClient == NULL || client_p->preClient->authd_cid == 0)
405 return;
406
407 if(*ident != '*')
408 {
409 rb_strlcpy(client_p->username, ident, sizeof(client_p->username));
410 ServerStats.is_abad++; /* s_auth used to do this, stay compatible */
411 }
412 else
413 ServerStats.is_asuc++;
414
415 if(*host != '*')
416 rb_strlcpy(client_p->host, host, sizeof(client_p->host));
417
418 client_p->preClient->authd_accepted = accept;
419 client_p->preClient->authd_cause = cause;
420 client_p->preClient->authd_data = (data == NULL ? NULL : rb_strdup(data));
421 client_p->preClient->authd_reason = (reason == NULL ? NULL : rb_strdup(reason));
422
423 rb_dictionary_delete(cid_clients, RB_UINT_TO_POINTER(client_p->preClient->authd_cid));
424
425 client_p->preClient->authd_cid = 0;
426
427 /*
428 * When a client has auth'ed, we want to start reading what it sends
429 * us. This is what read_packet() does.
430 * -- adrian
431 *
432 * Above comment was originally in s_auth.c, but moved here with below code.
433 * --Elizafox
434 */
435 rb_dlinkAddTail(client_p, &client_p->node, &global_client_list);
436 read_packet(client_p->localClient->F, client_p);
437}
438
439void
440authd_abort_client(struct Client *client_p)
441{
442 if(client_p->preClient == NULL)
443 return;
444
445 if(client_p->preClient->authd_cid == 0)
446 return;
447
448 rb_dictionary_delete(cid_clients, RB_UINT_TO_POINTER(client_p->preClient->authd_cid));
449
450 rb_helper_write(authd_helper, "E %x", client_p->preClient->authd_cid);
451 client_p->preClient->authd_cid = 0;
452}
453
ef0b13b9
EM
454static void
455timeout_dead_authd_clients(void *notused __unused)
456{
457 rb_dictionary_iter iter;
458 char *id;
459
460 RB_DICTIONARY_FOREACH(id, &iter, cid_clients)
461 {
462 struct Client *client_p;
ef0b13b9
EM
463
464 if(client_p->preClient->authd_timeout < rb_current_time())
465 {
466 rb_helper_write(authd_helper, "E %x", client_p->preClient->authd_cid);
467 rb_free(id);
468 rb_dictionary_delete(cid_clients, RB_UINT_TO_POINTER(client_p->preClient->authd_cid));
469 }
470 }
471}
472
d3f6b808
EM
473/* Turn a cause char (who rejected us) into the name of the provider */
474const char *
475get_provider_string(char cause)
476{
477 switch(cause)
478 {
479 case 'B':
480 return "Blacklist";
481 case 'D':
482 return "rDNS";
483 case 'I':
484 return "Ident";
485 default:
486 return "Unknown";
487 }
488}
489
490/* Send a new blacklist to authd */
491void
492add_blacklist(const char *host, const char *reason, uint8_t iptype, rb_dlink_list *filters)
493{
494 rb_dlink_node *ptr;
495 struct blacklist_stats *stats = rb_malloc(sizeof(struct blacklist_stats));
50808796 496 char filterbuf[BUFSIZE] = "*";
d3f6b808
EM
497 size_t s = 0;
498
499 /* Build a list of comma-separated values for authd.
500 * We don't check for validity - do it elsewhere.
501 */
502 RB_DLINK_FOREACH(ptr, filters->head)
503 {
504 char *filter = ptr->data;
505 size_t filterlen = strlen(filter) + 1;
506
507 if(s + filterlen > sizeof(filterbuf))
508 break;
509
510 snprintf(&filterbuf[s], sizeof(filterbuf) - s, "%s,", filter);
511
512 s += filterlen;
513 }
514
515 if(s)
516 filterbuf[s - 1] = '\0';
517
518 stats->iptype = iptype;
519 stats->hits = 0;
520 rb_dictionary_add(bl_stats, host, stats);
521
522 rb_helper_write(authd_helper, "O rbl %s %hhu %s :%s", host, iptype, filterbuf, reason);
523}
524
525/* Delete a blacklist */
526void
527del_blacklist(const char *host)
528{
529 rb_dictionary_delete(bl_stats, host);
530
531 rb_helper_write(authd_helper, "O rbl_del %s", host);
532}
533
534/* Delete all the blacklists */
535void
536del_blacklist_all(void)
537{
538 struct blacklist_stats *stats;
539 rb_dictionary_iter iter;
540
541 RB_DICTIONARY_FOREACH(stats, &iter, bl_stats)
542 {
543 rb_free(stats);
544 rb_dictionary_delete(bl_stats, iter.cur->key);
545 }
546
547 rb_helper_write(authd_helper, "O rbl_del_all");
548}
549
550/* Adjust an authd timeout value */
551void
552set_authd_timeout(const char *key, int timeout)
553{
554 rb_helper_write(authd_helper, "O %s %d", key, timeout);
555}
ad043803
EM
556
557void
558ident_check_enable(bool enabled)
559{
560 rb_helper_write(authd_helper, "O ident_enabled %d", enabled ? 1 : 0);
561}