]> jfr.im git - solanum.git/blame - ircd/authd.c
authd: add API for setting OPM listeners
[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 {
7ad083b0
EM
76 ierror("Unable to execute authd in %s or %s/bin",
77 ircd_paths[IRCD_PATH_LIBEXEC], ConfigFileEntry.dpath);
fb7d74ef 78 sendto_realops_snomask(SNO_GENERAL, L_ALL,
4d8cfacd
AC
79 "Unable to execute authd in %s or %s/bin",
80 ircd_paths[IRCD_PATH_LIBEXEC], ConfigFileEntry.dpath);
fb7d74ef
AC
81 return 1;
82 }
83
84 }
85
86 authd_path = rb_strdup(fullpath);
87 }
88
d3f6b808
EM
89 if(cid_clients == NULL)
90 cid_clients = rb_dictionary_create("authd cid to uid mapping", rb_uint32cmp);
91
92 if(bl_stats == NULL)
93 bl_stats = rb_dictionary_create("blacklist statistics", strcasecmp);
94
ef0b13b9
EM
95 if(timeout_ev == NULL)
96 timeout_ev = rb_event_addish("timeout_dead_authd_clients", timeout_dead_authd_clients, NULL, 1);
97
fb7d74ef
AC
98 authd_helper = rb_helper_start("authd", authd_path, parse_authd_reply, restart_authd_cb);
99
100 if(authd_helper == NULL)
101 {
7ad083b0 102 ierror("Unable to start authd helper: %s", strerror(errno));
fb7d74ef
AC
103 sendto_realops_snomask(SNO_GENERAL, L_ALL, "Unable to start authd helper: %s", strerror(errno));
104 return 1;
105 }
106 ilog(L_MAIN, "authd helper started");
107 sendto_realops_snomask(SNO_GENERAL, L_ALL, "authd helper started");
108 rb_helper_run(authd_helper);
109 return 0;
110}
111
112static void
113parse_authd_reply(rb_helper * helper)
114{
115 ssize_t len;
116 int parc;
50808796 117 char authdBuf[READBUF_SIZE];
fb7d74ef 118 char *parv[MAXPARA + 1];
d3f6b808 119 long lcid;
50808796 120 uint32_t cid;
d3f6b808 121 struct Client *client_p;
394b8dde 122
50808796 123 while((len = rb_helper_read(helper, authdBuf, sizeof(authdBuf))) > 0)
fb7d74ef 124 {
50808796 125 parc = rb_string_to_array(authdBuf, parv, MAXPARA+1);
fb7d74ef 126
3949459b 127 switch (*parv[0])
fb7d74ef 128 {
d3f6b808
EM
129 case 'A': /* Accepted a client */
130 if(parc != 4)
131 {
132 iwarn("authd sent a result with wrong number of arguments: got %d", parc);
133 restart_authd();
134 return;
135 }
136
50808796 137 if((lcid = strtol(parv[1], NULL, 16)) > UINT32_MAX || lcid < 0)
d3f6b808 138 {
50808796 139 iwarn("authd sent us back a bad client ID: %ld", lcid);
d3f6b808
EM
140 restart_authd();
141 return;
142 }
143
50808796
EM
144 cid = (uint32_t)lcid;
145
d3f6b808 146 /* cid to uid (retrieve and delete) */
50808796 147 if((client_p = rb_dictionary_delete(cid_clients, RB_UINT_TO_POINTER(cid))) == NULL)
d3f6b808 148 {
50808796 149 iwarn("authd sent us back an unknown client ID %x", cid);
d3f6b808
EM
150 restart_authd();
151 return;
152 }
153
d3f6b808
EM
154 authd_decide_client(client_p, parv[2], parv[3], true, '\0', NULL, NULL);
155 break;
156 case 'R': /* Reject client */
157 if(parc != 7)
158 {
159 iwarn("authd sent us a result with wrong number of arguments: got %d", parc);
160 restart_authd();
161 return;
162 }
163
50808796 164 if((lcid = strtol(parv[1], NULL, 16)) > UINT32_MAX || lcid < 0)
d3f6b808 165 {
50808796 166 iwarn("authd sent us back a bad client ID %ld", lcid);
d3f6b808
EM
167 restart_authd();
168 return;
169 }
170
50808796
EM
171 cid = (uint32_t)lcid;
172
d3f6b808 173 /* cid to uid (retrieve and delete) */
50808796 174 if((client_p = rb_dictionary_delete(cid_clients, RB_UINT_TO_POINTER(cid))) == NULL)
d3f6b808 175 {
50808796 176 iwarn("authd sent us back an unknown client ID %x", cid);
d3f6b808
EM
177 restart_authd();
178 return;
179 }
180
d3f6b808 181 authd_decide_client(client_p, parv[3], parv[4], false, toupper(*parv[2]), parv[5], parv[6]);
cc4d3931
EM
182 break;
183 case 'N': /* Notice to client */
ef0b13b9 184 if(parc != 3)
cc4d3931
EM
185 {
186 iwarn("authd sent us a result with wrong number of arguments: got %d", parc);
187 restart_authd();
188 return;
189 }
50808796
EM
190
191 if((lcid = strtol(parv[1], NULL, 16)) > UINT32_MAX || lcid < 0)
cc4d3931 192 {
50808796 193 iwarn("authd sent us back a bad client ID %ld", lcid);
cc4d3931
EM
194 restart_authd();
195 return;
196 }
197
50808796
EM
198 cid = (uint32_t)lcid;
199
cc4d3931 200 /* cid to uid */
50808796 201 if((client_p = rb_dictionary_retrieve(cid_clients, RB_UINT_TO_POINTER(cid))) == NULL)
cc4d3931 202 {
50808796 203 iwarn("authd sent us back an unknown client ID %x", cid);
cc4d3931
EM
204 restart_authd();
205 return;
206 }
207
cc4d3931
EM
208 sendto_one_notice(client_p, ":%s", parv[2]);
209 break;
d3f6b808 210 case 'E': /* DNS Result */
fb7d74ef
AC
211 if(parc != 5)
212 {
7ad083b0 213 iwarn("authd sent a result with wrong number of arguments: got %d", parc);
fb7d74ef
AC
214 restart_authd();
215 return;
216 }
217 dns_results_callback(parv[1], parv[2], parv[3], parv[4]);
3949459b 218 break;
d3f6b808 219 case 'W': /* Oper warning */
0a659bf0
EM
220 if(parc != 3)
221 {
7ad083b0 222 iwarn("authd sent a result with wrong number of arguments: got %d", parc);
0a659bf0
EM
223 restart_authd();
224 return;
225 }
226
227 switch(*parv[2])
228 {
d3f6b808 229 case 'D': /* Debug */
0a659bf0 230 sendto_realops_snomask(SNO_DEBUG, L_ALL, "authd debug: %s", parv[3]);
7ad083b0 231 idebug("authd: %s", parv[3]);
0a659bf0 232 break;
938f93f4 233 case 'I': /* Info */
0a659bf0 234 sendto_realops_snomask(SNO_GENERAL, L_ALL, "authd info: %s", parv[3]);
7ad083b0 235 inotice("authd: %s", parv[3]);
0a659bf0 236 break;
938f93f4 237 case 'W': /* Warning */
0a659bf0 238 sendto_realops_snomask(SNO_GENERAL, L_ALL, "authd WARNING: %s", parv[3]);
7ad083b0 239 iwarn("authd: %s", parv[3]);
0a659bf0 240 break;
938f93f4 241 case 'C': /* Critical (error) */
0a659bf0 242 sendto_realops_snomask(SNO_GENERAL, L_ALL, "authd CRITICAL: %s", parv[3]);
7ad083b0 243 ierror("authd: %s", parv[3]);
0a659bf0 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 {
7ad083b0 258 iwarn("authd sent a result with wrong number of arguments: got %d", parc);
394b8dde
EM
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 {
7ad083b0 270 iwarn("authd sent a result with wrong number of arguments: got %d", parc);
394b8dde
EM
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 {
7ad083b0 291 ierror("Unable to start authd helper: %s", strerror(errno));
fb7d74ef
AC
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{
e1582810
EM
309 rb_dictionary_iter iter;
310 struct Client *client_p;
311
7ad083b0 312 iwarn("authd: restart_authd_cb called, authd died?");
3949459b 313 sendto_realops_snomask(SNO_GENERAL, L_ALL, "authd: restart_authd_cb called, authd died?");
e1582810 314
fb7d74ef
AC
315 if(helper != NULL)
316 {
317 rb_helper_close(helper);
318 authd_helper = NULL;
319 }
e1582810
EM
320
321 RB_DICTIONARY_FOREACH(client_p, &iter, cid_clients)
322 {
323 /* Abort any existing clients */
324 authd_abort_client(client_p);
325 }
326
fb7d74ef
AC
327 start_authd();
328}
329
330void
331restart_authd(void)
332{
333 restart_authd_cb(authd_helper);
334}
335
336void
337rehash_authd(void)
338{
339 rb_helper_write(authd_helper, "R");
d3f6b808 340 configure_authd();
fb7d74ef
AC
341}
342
343void
344check_authd(void)
345{
346 if(authd_helper == NULL)
347 restart_authd();
348}
d3f6b808
EM
349
350static inline uint32_t
351generate_cid(void)
352{
353 if(++cid == 0)
354 cid = 1;
355
356 return cid;
357}
358
359/* Basically when this is called we begin handing off the client to authd for
360 * processing. authd "owns" the client until processing is finished, or we
361 * timeout from authd. authd will make a decision whether or not to accept the
362 * client, but it's up to other parts of the code (for now) to decide if we're
363 * gonna accept the client and ignore authd's suggestion.
364 *
365 * --Elizafox
366 */
367void
368authd_initiate_client(struct Client *client_p)
369{
370 char client_ipaddr[HOSTIPLEN+1];
371 char listen_ipaddr[HOSTIPLEN+1];
372 uint16_t client_port, listen_port;
373 uint32_t authd_cid;
374
50808796 375 if(client_p->preClient == NULL || client_p->preClient->authd_cid != 0)
d3f6b808
EM
376 return;
377
378 authd_cid = client_p->preClient->authd_cid = generate_cid();
379
380 /* Collisions are extremely unlikely, so disregard the possibility */
50808796 381 rb_dictionary_add(cid_clients, RB_UINT_TO_POINTER(authd_cid), client_p);
d3f6b808
EM
382
383 /* Retrieve listener and client IP's */
384 rb_inet_ntop_sock((struct sockaddr *)&client_p->preClient->lip, listen_ipaddr, sizeof(listen_ipaddr));
385 rb_inet_ntop_sock((struct sockaddr *)&client_p->localClient->ip, client_ipaddr, sizeof(client_ipaddr));
386
387 /* Retrieve listener and client ports */
d86692fa
EM
388 listen_port = ntohs(GET_SS_PORT(&client_p->preClient->lip));
389 client_port = ntohs(GET_SS_PORT(&client_p->localClient->ip));
d3f6b808 390
59d42a9f 391 /* Add a bit of a fudge factor... */
d86692fa 392 client_p->preClient->authd_timeout = rb_current_time() + ConfigFileEntry.connect_timeout + 10;
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));
4f6119cd 410 ServerStats.is_asuc++;
d3f6b808
EM
411 }
412 else
4f6119cd 413 ServerStats.is_abad++; /* s_auth used to do this, stay compatible */
d3f6b808
EM
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));
d3f6b808
EM
424 client_p->preClient->authd_cid = 0;
425
426 /*
427 * When a client has auth'ed, we want to start reading what it sends
428 * us. This is what read_packet() does.
429 * -- adrian
430 *
431 * Above comment was originally in s_auth.c, but moved here with below code.
432 * --Elizafox
433 */
434 rb_dlinkAddTail(client_p, &client_p->node, &global_client_list);
435 read_packet(client_p->localClient->F, client_p);
436}
437
438void
439authd_abort_client(struct Client *client_p)
440{
e1582810 441 if(client_p == NULL || client_p->preClient == NULL)
d3f6b808
EM
442 return;
443
444 if(client_p->preClient->authd_cid == 0)
445 return;
446
447 rb_dictionary_delete(cid_clients, RB_UINT_TO_POINTER(client_p->preClient->authd_cid));
448
e1582810
EM
449 if(authd_helper != NULL)
450 rb_helper_write(authd_helper, "E %x", client_p->preClient->authd_cid);
451
452 /* XXX should we blindly allow like this? */
7372553d 453 authd_decide_client(client_p, "*", "*", true, '\0', NULL, NULL);
e1582810 454
d3f6b808
EM
455 client_p->preClient->authd_cid = 0;
456}
457
ef0b13b9
EM
458static void
459timeout_dead_authd_clients(void *notused __unused)
460{
461 rb_dictionary_iter iter;
0bb5d3f0 462 struct Client *client_p;
ef0b13b9 463
0bb5d3f0 464 RB_DICTIONARY_FOREACH(client_p, &iter, cid_clients)
ef0b13b9 465 {
ef0b13b9 466 if(client_p->preClient->authd_timeout < rb_current_time())
e1582810 467 authd_abort_client(client_p);
ef0b13b9
EM
468 }
469}
470
d3f6b808
EM
471/* Turn a cause char (who rejected us) into the name of the provider */
472const char *
473get_provider_string(char cause)
474{
475 switch(cause)
476 {
477 case 'B':
478 return "Blacklist";
479 case 'D':
480 return "rDNS";
481 case 'I':
482 return "Ident";
483 default:
484 return "Unknown";
485 }
486}
487
488/* Send a new blacklist to authd */
489void
490add_blacklist(const char *host, const char *reason, uint8_t iptype, rb_dlink_list *filters)
491{
492 rb_dlink_node *ptr;
493 struct blacklist_stats *stats = rb_malloc(sizeof(struct blacklist_stats));
50808796 494 char filterbuf[BUFSIZE] = "*";
d3f6b808
EM
495 size_t s = 0;
496
497 /* Build a list of comma-separated values for authd.
498 * We don't check for validity - do it elsewhere.
499 */
500 RB_DLINK_FOREACH(ptr, filters->head)
501 {
502 char *filter = ptr->data;
503 size_t filterlen = strlen(filter) + 1;
504
505 if(s + filterlen > sizeof(filterbuf))
506 break;
507
508 snprintf(&filterbuf[s], sizeof(filterbuf) - s, "%s,", filter);
509
510 s += filterlen;
511 }
512
513 if(s)
514 filterbuf[s - 1] = '\0';
515
516 stats->iptype = iptype;
517 stats->hits = 0;
518 rb_dictionary_add(bl_stats, host, stats);
519
520 rb_helper_write(authd_helper, "O rbl %s %hhu %s :%s", host, iptype, filterbuf, reason);
521}
522
523/* Delete a blacklist */
524void
525del_blacklist(const char *host)
526{
527 rb_dictionary_delete(bl_stats, host);
528
529 rb_helper_write(authd_helper, "O rbl_del %s", host);
530}
531
532/* Delete all the blacklists */
533void
534del_blacklist_all(void)
535{
536 struct blacklist_stats *stats;
537 rb_dictionary_iter iter;
538
539 RB_DICTIONARY_FOREACH(stats, &iter, bl_stats)
540 {
541 rb_free(stats);
542 rb_dictionary_delete(bl_stats, iter.cur->key);
543 }
544
545 rb_helper_write(authd_helper, "O rbl_del_all");
546}
547
548/* Adjust an authd timeout value */
4f6119cd 549bool
d3f6b808
EM
550set_authd_timeout(const char *key, int timeout)
551{
4f6119cd
EM
552 if(timeout <= 0)
553 return false;
554
d3f6b808 555 rb_helper_write(authd_helper, "O %s %d", key, timeout);
4f6119cd 556 return true;
d3f6b808 557}
ad043803 558
4f6119cd 559/* Enable identd checks */
ad043803
EM
560void
561ident_check_enable(bool enabled)
562{
563 rb_helper_write(authd_helper, "O ident_enabled %d", enabled ? 1 : 0);
564}
4f6119cd
EM
565
566/* Create an OPM listener */
567bool
568create_opm_listener(struct rb_sockaddr_storage *addr)
569{
570 char addrbuf[HOSTIPLEN];
571
572 if(!rb_inet_ntop_sock((struct sockaddr *)addr, addrbuf, sizeof(addrbuf)))
573 return false;
574
575 if(addrbuf[0] == ':')
576 {
577 /* Reformat for authd sending */
578 memmove(addrbuf + 1, addrbuf, sizeof(addrbuf) - 1);
579 addrbuf[0] = '0';
580 }
581
582 rb_helper_write(authd_helper, "O opm_listener %s %hu", addrbuf, ntohs(GET_SS_PORT(addr)));
583 return true;
584}