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