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