]> jfr.im git - solanum.git/blob - ircd/authd.c
aa657d5cec3dd71bf54245745dcbca52ed2c9c91
[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 void
113 parse_authd_reply(rb_helper * helper)
114 {
115 ssize_t len;
116 int parc;
117 char authdBuf[READBUF_SIZE];
118 char *parv[MAXPARA + 1];
119 long lcid;
120 uint32_t cid;
121 struct Client *client_p;
122
123 while((len = rb_helper_read(helper, authdBuf, sizeof(authdBuf))) > 0)
124 {
125 parc = rb_string_to_array(authdBuf, parv, MAXPARA+1);
126
127 switch (*parv[0])
128 {
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
137 if((lcid = strtol(parv[1], NULL, 16)) > UINT32_MAX || lcid < 0)
138 {
139 iwarn("authd sent us back a bad client ID: %ld", lcid);
140 restart_authd();
141 return;
142 }
143
144 cid = (uint32_t)lcid;
145
146 /* cid to uid (retrieve and delete) */
147 if((client_p = rb_dictionary_delete(cid_clients, RB_UINT_TO_POINTER(cid))) == NULL)
148 {
149 iwarn("authd sent us back an unknown client ID %x", cid);
150 restart_authd();
151 return;
152 }
153
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
164 if((lcid = strtol(parv[1], NULL, 16)) > UINT32_MAX || lcid < 0)
165 {
166 iwarn("authd sent us back a bad client ID %ld", lcid);
167 restart_authd();
168 return;
169 }
170
171 cid = (uint32_t)lcid;
172
173 /* cid to uid (retrieve and delete) */
174 if((client_p = rb_dictionary_delete(cid_clients, RB_UINT_TO_POINTER(cid))) == NULL)
175 {
176 iwarn("authd sent us back an unknown client ID %x", cid);
177 restart_authd();
178 return;
179 }
180
181 authd_decide_client(client_p, parv[3], parv[4], false, toupper(*parv[2]), parv[5], parv[6]);
182 break;
183 case 'N': /* Notice to client */
184 if(parc != 3)
185 {
186 iwarn("authd sent us a result with wrong number of arguments: got %d", parc);
187 restart_authd();
188 return;
189 }
190
191 if((lcid = strtol(parv[1], NULL, 16)) > UINT32_MAX || lcid < 0)
192 {
193 iwarn("authd sent us back a bad client ID %ld", lcid);
194 restart_authd();
195 return;
196 }
197
198 cid = (uint32_t)lcid;
199
200 /* cid to uid */
201 if((client_p = rb_dictionary_retrieve(cid_clients, RB_UINT_TO_POINTER(cid))) == NULL)
202 {
203 iwarn("authd sent us back an unknown client ID %x", cid);
204 restart_authd();
205 return;
206 }
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 dns_results_callback(parv[1], parv[2], parv[3], parv[4]);
218 break;
219 case 'W': /* Oper warning */
220 if(parc != 3)
221 {
222 iwarn("authd sent a result with wrong number of arguments: got %d", parc);
223 restart_authd();
224 return;
225 }
226
227 switch(*parv[2])
228 {
229 case 'D': /* Debug */
230 sendto_realops_snomask(SNO_DEBUG, L_ALL, "authd debug: %s", parv[3]);
231 idebug("authd: %s", parv[3]);
232 break;
233 case 'I': /* Info */
234 sendto_realops_snomask(SNO_GENERAL, L_ALL, "authd info: %s", parv[3]);
235 inotice("authd: %s", parv[3]);
236 break;
237 case 'W': /* Warning */
238 sendto_realops_snomask(SNO_GENERAL, L_ALL, "authd WARNING: %s", parv[3]);
239 iwarn("authd: %s", parv[3]);
240 break;
241 case 'C': /* Critical (error) */
242 sendto_realops_snomask(SNO_GENERAL, L_ALL, "authd CRITICAL: %s", parv[3]);
243 ierror("authd: %s", parv[3]);
244 break;
245 default: /* idk */
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;
253 case 'X': /* Stats error */
254 case 'Y': /* Stats reply */
255 case 'Z': /* End of stats reply */
256 if(parc < 3)
257 {
258 iwarn("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 iwarn("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;
280 default:
281 break;
282 }
283 }
284 }
285
286 void
287 init_authd(void)
288 {
289 if(start_authd())
290 {
291 ierror("Unable to start authd helper: %s", strerror(errno));
292 exit(0);
293 }
294 }
295
296 void
297 configure_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);
302 set_authd_timeout("rbl_timeout", ConfigFileEntry.connect_timeout);
303 ident_check_enable(!ConfigFileEntry.disable_auth);
304 }
305
306 static void
307 restart_authd_cb(rb_helper * helper)
308 {
309 iwarn("authd: restart_authd_cb called, authd died?");
310 sendto_realops_snomask(SNO_GENERAL, L_ALL, "authd: restart_authd_cb called, authd died?");
311 if(helper != NULL)
312 {
313 rb_helper_close(helper);
314 authd_helper = NULL;
315 }
316 start_authd();
317 }
318
319 void
320 restart_authd(void)
321 {
322 restart_authd_cb(authd_helper);
323 }
324
325 void
326 rehash_authd(void)
327 {
328 rb_helper_write(authd_helper, "R");
329 configure_authd();
330 }
331
332 void
333 check_authd(void)
334 {
335 if(authd_helper == NULL)
336 restart_authd();
337 }
338
339 static inline uint32_t
340 generate_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 */
356 void
357 authd_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
364 if(client_p->preClient == NULL || client_p->preClient->authd_cid != 0)
365 return;
366
367 authd_cid = client_p->preClient->authd_cid = generate_cid();
368
369 /* Collisions are extremely unlikely, so disregard the possibility */
370 rb_dictionary_add(cid_clients, RB_UINT_TO_POINTER(authd_cid), client_p);
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 listen_port = ntohs(GET_SS_PORT(&client_p->preClient->lip));
378 client_port = ntohs(GET_SS_PORT(&client_p->localClient->ip));
379
380 /* Add a bit of a fudge factor... */
381 client_p->preClient->authd_timeout = rb_current_time() + ConfigFileEntry.connect_timeout + 10;
382
383 rb_helper_write(authd_helper, "C %x %s %hu %s %hu", authd_cid, listen_ipaddr, listen_port, client_ipaddr, client_port);
384 }
385
386 /* When this is called we have a decision on client acceptance.
387 *
388 * After this point authd no longer "owns" the client.
389 */
390 void
391 authd_decide_client(struct Client *client_p, const char *ident, const char *host, bool accept, char cause, const char *data, const char *reason)
392 {
393 if(client_p->preClient == NULL || client_p->preClient->authd_cid == 0)
394 return;
395
396 if(*ident != '*')
397 {
398 rb_strlcpy(client_p->username, ident, sizeof(client_p->username));
399 ServerStats.is_abad++; /* s_auth used to do this, stay compatible */
400 }
401 else
402 ServerStats.is_asuc++;
403
404 if(*host != '*')
405 rb_strlcpy(client_p->host, host, sizeof(client_p->host));
406
407 client_p->preClient->authd_accepted = accept;
408 client_p->preClient->authd_cause = cause;
409 client_p->preClient->authd_data = (data == NULL ? NULL : rb_strdup(data));
410 client_p->preClient->authd_reason = (reason == NULL ? NULL : rb_strdup(reason));
411
412 rb_dictionary_delete(cid_clients, RB_UINT_TO_POINTER(client_p->preClient->authd_cid));
413
414 client_p->preClient->authd_cid = 0;
415
416 /*
417 * When a client has auth'ed, we want to start reading what it sends
418 * us. This is what read_packet() does.
419 * -- adrian
420 *
421 * Above comment was originally in s_auth.c, but moved here with below code.
422 * --Elizafox
423 */
424 rb_dlinkAddTail(client_p, &client_p->node, &global_client_list);
425 read_packet(client_p->localClient->F, client_p);
426 }
427
428 void
429 authd_abort_client(struct Client *client_p)
430 {
431 if(client_p->preClient == NULL)
432 return;
433
434 if(client_p->preClient->authd_cid == 0)
435 return;
436
437 rb_dictionary_delete(cid_clients, RB_UINT_TO_POINTER(client_p->preClient->authd_cid));
438
439 rb_helper_write(authd_helper, "E %x", client_p->preClient->authd_cid);
440 client_p->preClient->authd_cid = 0;
441 }
442
443 static void
444 timeout_dead_authd_clients(void *notused __unused)
445 {
446 rb_dictionary_iter iter;
447 struct Client *client_p;
448
449 RB_DICTIONARY_FOREACH(client_p, &iter, cid_clients)
450 {
451 if(client_p->preClient->authd_timeout < rb_current_time())
452 {
453 rb_helper_write(authd_helper, "E %x", client_p->preClient->authd_cid);
454 rb_dictionary_delete(cid_clients, RB_UINT_TO_POINTER(client_p->preClient->authd_cid));
455 }
456 }
457 }
458
459 /* Turn a cause char (who rejected us) into the name of the provider */
460 const char *
461 get_provider_string(char cause)
462 {
463 switch(cause)
464 {
465 case 'B':
466 return "Blacklist";
467 case 'D':
468 return "rDNS";
469 case 'I':
470 return "Ident";
471 default:
472 return "Unknown";
473 }
474 }
475
476 /* Send a new blacklist to authd */
477 void
478 add_blacklist(const char *host, const char *reason, uint8_t iptype, rb_dlink_list *filters)
479 {
480 rb_dlink_node *ptr;
481 struct blacklist_stats *stats = rb_malloc(sizeof(struct blacklist_stats));
482 char filterbuf[BUFSIZE] = "*";
483 size_t s = 0;
484
485 /* Build a list of comma-separated values for authd.
486 * We don't check for validity - do it elsewhere.
487 */
488 RB_DLINK_FOREACH(ptr, filters->head)
489 {
490 char *filter = ptr->data;
491 size_t filterlen = strlen(filter) + 1;
492
493 if(s + filterlen > sizeof(filterbuf))
494 break;
495
496 snprintf(&filterbuf[s], sizeof(filterbuf) - s, "%s,", filter);
497
498 s += filterlen;
499 }
500
501 if(s)
502 filterbuf[s - 1] = '\0';
503
504 stats->iptype = iptype;
505 stats->hits = 0;
506 rb_dictionary_add(bl_stats, host, stats);
507
508 rb_helper_write(authd_helper, "O rbl %s %hhu %s :%s", host, iptype, filterbuf, reason);
509 }
510
511 /* Delete a blacklist */
512 void
513 del_blacklist(const char *host)
514 {
515 rb_dictionary_delete(bl_stats, host);
516
517 rb_helper_write(authd_helper, "O rbl_del %s", host);
518 }
519
520 /* Delete all the blacklists */
521 void
522 del_blacklist_all(void)
523 {
524 struct blacklist_stats *stats;
525 rb_dictionary_iter iter;
526
527 RB_DICTIONARY_FOREACH(stats, &iter, bl_stats)
528 {
529 rb_free(stats);
530 rb_dictionary_delete(bl_stats, iter.cur->key);
531 }
532
533 rb_helper_write(authd_helper, "O rbl_del_all");
534 }
535
536 /* Adjust an authd timeout value */
537 void
538 set_authd_timeout(const char *key, int timeout)
539 {
540 rb_helper_write(authd_helper, "O %s %d", key, timeout);
541 }
542
543 void
544 ident_check_enable(bool enabled)
545 {
546 rb_helper_write(authd_helper, "O ident_enabled %d", enabled ? 1 : 0);
547 }