]> jfr.im git - solanum.git/blame - ircd/listener.c
ircd: integrate ircd side of wsockd support
[solanum.git] / ircd / listener.c
CommitLineData
54ac8b60
VY
1/*
2 * ircd-ratbox: A slightly useful ircd.
3 * listener.c: Listens on a port.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
54ac8b60
VY
23 */
24
25#include "stdinc.h"
26#include "setup.h"
27#include "listener.h"
28#include "client.h"
4562c604 29#include "match.h"
54ac8b60
VY
30#include "ircd.h"
31#include "ircd_defs.h"
32#include "numeric.h"
33#include "s_conf.h"
34#include "s_newconf.h"
35#include "s_stats.h"
36#include "send.h"
d3f6b808 37#include "authd.h"
54ac8b60
VY
38#include "reject.h"
39#include "s_conf.h"
40#include "hostmask.h"
c6d72037 41#include "sslproc.h"
c53ca1e0 42#include "wsproc.h"
c6d72037 43#include "hash.h"
77d3d2db
KB
44#include "s_assert.h"
45#include "logger.h"
54ac8b60
VY
46
47#ifndef INADDR_NONE
48#define INADDR_NONE ((unsigned int) 0xffffffff)
49#endif
50
9ea3ea10 51#if defined(NO_IN6ADDR_ANY) && defined(RB_IPV6)
54ac8b60
VY
52static const struct in6_addr in6addr_any =
53{ { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } };
55abcbb2 54#endif
54ac8b60 55
8f103562 56static struct Listener *ListenerPollList = NULL;
1087485c 57static int accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data);
8e09c4a2 58static void accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data);
54ac8b60 59
8f103562 60static struct Listener *
e7046ee5 61make_listener(struct rb_sockaddr_storage *addr)
54ac8b60 62{
8f103562 63 struct Listener *listener = (struct Listener *) rb_malloc(sizeof(struct Listener));
54ac8b60 64 s_assert(0 != listener);
54ac8b60 65 listener->name = me.name;
f691939a
VY
66 listener->F = NULL;
67
e7046ee5 68 memcpy(&listener->addr, addr, sizeof(struct rb_sockaddr_storage));
54ac8b60
VY
69 listener->next = NULL;
70 return listener;
71}
72
73void
8f103562 74free_listener(struct Listener *listener)
54ac8b60
VY
75{
76 s_assert(NULL != listener);
77 if(listener == NULL)
78 return;
79 /*
80 * remove from listener list
81 */
82 if(listener == ListenerPollList)
83 ListenerPollList = listener->next;
84 else
85 {
8f103562 86 struct Listener *prev = ListenerPollList;
54ac8b60
VY
87 for (; prev; prev = prev->next)
88 {
89 if(listener == prev->next)
90 {
91 prev->next = listener->next;
92 break;
93 }
94 }
95 }
96
97 /* free */
98 rb_free(listener);
99}
100
101#define PORTNAMELEN 6 /* ":31337" */
102
40173bcb
JT
103/*
104 * get_listener_port - return displayable listener port
105 */
106static uint16_t
107get_listener_port(const struct Listener *listener)
108{
d86692fa 109 return ntohs(GET_SS_PORT(&listener->addr));
40173bcb
JT
110}
111
54ac8b60
VY
112/*
113 * get_listener_name - return displayable listener name and port
114 * returns "host.foo.org:6667" for a given listener
115 */
116const char *
8f103562 117get_listener_name(const struct Listener *listener)
54ac8b60
VY
118{
119 static char buf[HOSTLEN + HOSTLEN + PORTNAMELEN + 4];
54ac8b60
VY
120
121 s_assert(NULL != listener);
122 if(listener == NULL)
123 return NULL;
124
5203cba5 125 snprintf(buf, sizeof(buf), "%s[%s/%u]",
40173bcb 126 me.name, listener->name, get_listener_port(listener));
54ac8b60
VY
127 return buf;
128}
129
130/*
131 * show_ports - send port listing to a client
132 * inputs - pointer to client to show ports to
133 * output - none
134 * side effects - show ports
135 */
136void
137show_ports(struct Client *source_p)
138{
8f103562 139 struct Listener *listener = 0;
54ac8b60
VY
140
141 for (listener = ListenerPollList; listener; listener = listener->next)
142 {
55abcbb2 143 sendto_one_numeric(source_p, RPL_STATSPLINE,
40173bcb
JT
144 form_str(RPL_STATSPLINE), 'P',
145 get_listener_port(listener),
54ac8b60 146 IsOperAdmin(source_p) ? listener->name : me.name,
8bd5767b 147 listener->ref_count, (listener->active) ? "active" : "disabled",
c6d72037 148 listener->ssl ? " ssl" : "");
54ac8b60
VY
149 }
150}
151
152/*
153 * inetport - create a listener socket in the AF_INET or AF_INET6 domain,
154 * bind it to the port given in 'port' and listen to it
155 * returns true (1) if successful false (0) on error.
156 *
157 * If the operating system has a define for SOMAXCONN, use it, otherwise
638d2862 158 * use CHARYBDIS_SOMAXCONN
54ac8b60
VY
159 */
160#ifdef SOMAXCONN
638d2862
EM
161#undef CHARYBDIS_SOMAXCONN
162#define CHARYBDIS_SOMAXCONN SOMAXCONN
54ac8b60
VY
163#endif
164
165static int
8f103562 166inetport(struct Listener *listener)
54ac8b60 167{
f691939a 168 rb_fde_t *F;
54ac8b60 169 int opt = 1;
40173bcb 170 const char *errstr;
54ac8b60
VY
171
172 /*
173 * At first, open a new socket
174 */
55abcbb2 175
f691939a 176 F = rb_socket(GET_SS_FAMILY(&listener->addr), SOCK_STREAM, 0, "Listener socket");
54ac8b60 177
9ea3ea10 178#ifdef RB_IPV6
e867208d 179 if(GET_SS_FAMILY(&listener->addr) == AF_INET6)
54ac8b60
VY
180 {
181 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&listener->addr;
182 if(!IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &in6addr_any))
183 {
caa4d9d2 184 rb_inet_ntop(AF_INET6, &in6->sin6_addr, listener->vhost, sizeof(listener->vhost));
54ac8b60
VY
185 listener->name = listener->vhost;
186 }
187 } else
188#endif
189 {
190 struct sockaddr_in *in = (struct sockaddr_in *)&listener->addr;
191 if(in->sin_addr.s_addr != INADDR_ANY)
192 {
caa4d9d2 193 rb_inet_ntop(AF_INET, &in->sin_addr, listener->vhost, sizeof(listener->vhost));
54ac8b60 194 listener->name = listener->vhost;
55abcbb2 195 }
54ac8b60
VY
196 }
197
1087485c
JT
198 if(F == NULL)
199 {
40173bcb
JT
200 sendto_realops_snomask(SNO_GENERAL, L_ALL,
201 "Cannot open socket for listener on port %d",
202 get_listener_port(listener));
203 ilog(L_MAIN, "Cannot open socket for listener %s",
204 get_listener_name(listener));
1087485c
JT
205 return 0;
206 }
207 else if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus*/
208 {
825ddf13 209 ilog_error("no more connections left for listener");
40173bcb
JT
210 sendto_realops_snomask(SNO_GENERAL, L_ALL,
211 "No more connections left for listener on port %d",
212 get_listener_port(listener));
213 ilog(L_MAIN, "No more connections left for listener %s",
214 get_listener_name(listener));
1087485c
JT
215 rb_close(F);
216 return 0;
54ac8b60
VY
217 }
218
1087485c
JT
219 /*
220 * XXX - we don't want to do all this crap for a listener
221 * set_sock_opts(listener);
bf3ecca2
EM
222 *
223 * FIXME - doesn't this belong in librb? --Elizafox
1087485c
JT
224 */
225 if(setsockopt(rb_get_fd(F), SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt)))
226 {
40173bcb
JT
227 errstr = strerror(rb_get_sockerr(F));
228 sendto_realops_snomask(SNO_GENERAL, L_ALL,
229 "Cannot set SO_REUSEADDR for listener on port %d: %s",
230 get_listener_port(listener), errstr);
231 ilog(L_MAIN, "Cannot set SO_REUSEADDR for listener %s: %s",
232 get_listener_name(listener), errstr);
1087485c
JT
233 rb_close(F);
234 return 0;
54ac8b60
VY
235 }
236
bf3ecca2 237 /* FIXME - doesn't this belong in librb? --Elizafox */
1087485c
JT
238 if(bind(rb_get_fd(F), (struct sockaddr *) &listener->addr, GET_SS_LEN(&listener->addr)))
239 {
40173bcb
JT
240 errstr = strerror(rb_get_sockerr(F));
241 sendto_realops_snomask(SNO_GENERAL, L_ALL,
242 "Cannot bind for listener on port %d: %s",
243 get_listener_port(listener), errstr);
244 ilog(L_MAIN, "Cannot bind for listener %s: %s",
245 get_listener_name(listener), errstr);
1087485c
JT
246 rb_close(F);
247 return 0;
54ac8b60
VY
248 }
249
638d2862 250 if(rb_listen(F, CHARYBDIS_SOMAXCONN, listener->defer_accept))
1087485c 251 {
40173bcb
JT
252 errstr = strerror(rb_get_sockerr(F));
253 sendto_realops_snomask(SNO_GENERAL, L_ALL,
254 "Cannot listen() for listener on port %d: %s",
255 get_listener_port(listener), errstr);
256 ilog(L_MAIN, "Cannot listen() for listener %s: %s",
257 get_listener_name(listener), errstr);
1087485c
JT
258 rb_close(F);
259 return 0;
54ac8b60
VY
260 }
261
1087485c
JT
262 listener->F = F;
263
f691939a 264 rb_accept_tcp(listener->F, accept_precallback, accept_callback, listener);
54ac8b60
VY
265 return 1;
266}
267
8f103562 268static struct Listener *
e7046ee5 269find_listener(struct rb_sockaddr_storage *addr)
54ac8b60 270{
8f103562
JT
271 struct Listener *listener = NULL;
272 struct Listener *last_closed = NULL;
54ac8b60
VY
273
274 for (listener = ListenerPollList; listener; listener = listener->next)
275 {
e867208d 276 if(GET_SS_FAMILY(addr) != GET_SS_FAMILY(&listener->addr))
54ac8b60 277 continue;
55abcbb2 278
a7fb2693 279 switch(GET_SS_FAMILY(addr))
54ac8b60
VY
280 {
281 case AF_INET:
282 {
283 struct sockaddr_in *in4 = (struct sockaddr_in *)addr;
284 struct sockaddr_in *lin4 = (struct sockaddr_in *)&listener->addr;
55abcbb2 285 if(in4->sin_addr.s_addr == lin4->sin_addr.s_addr &&
54ac8b60
VY
286 in4->sin_port == lin4->sin_port )
287 {
1087485c
JT
288 if(listener->F == NULL)
289 last_closed = listener;
290 else
54ac8b60
VY
291 return(listener);
292 }
293 break;
294 }
9ea3ea10 295#ifdef RB_IPV6
54ac8b60
VY
296 case AF_INET6:
297 {
298 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)addr;
299 struct sockaddr_in6 *lin6 =(struct sockaddr_in6 *)&listener->addr;
300 if(IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &lin6->sin6_addr) &&
301 in6->sin6_port == lin6->sin6_port)
302 {
1087485c
JT
303 if(listener->F == NULL)
304 last_closed = listener;
305 else
54ac8b60
VY
306 return(listener);
307 }
308 break;
55abcbb2 309
54ac8b60
VY
310 }
311#endif
312
313 default:
314 break;
315 }
316 }
317 return last_closed;
318}
319
320
321/*
322 * add_listener- create a new listener
323 * port - the port number to listen on
324 * vhost_ip - if non-null must contain a valid IP address string in
325 * the format "255.255.255.255"
326 */
327void
c53ca1e0 328add_listener(int port, const char *vhost_ip, int family, int ssl, int defer_accept, int wsock)
54ac8b60 329{
8f103562 330 struct Listener *listener;
e7046ee5 331 struct rb_sockaddr_storage vaddr;
54ac8b60
VY
332
333 /*
334 * if no port in conf line, don't bother
335 */
336 if(port == 0)
337 return;
338 memset(&vaddr, 0, sizeof(vaddr));
e867208d 339 SET_SS_FAMILY(&vaddr, family);
54ac8b60
VY
340
341 if(vhost_ip != NULL)
342 {
343 if(family == AF_INET)
344 {
caa4d9d2 345 if(rb_inet_pton(family, vhost_ip, &((struct sockaddr_in *)&vaddr)->sin_addr) <= 0)
54ac8b60 346 return;
55abcbb2 347 }
9ea3ea10 348#ifdef RB_IPV6
54ac8b60
VY
349 else
350 {
caa4d9d2 351 if(rb_inet_pton(family, vhost_ip, &((struct sockaddr_in6 *)&vaddr)->sin6_addr) <= 0)
54ac8b60 352 return;
55abcbb2 353
54ac8b60
VY
354 }
355#endif
356 } else
357 {
358 switch(family)
359 {
360 case AF_INET:
361 ((struct sockaddr_in *)&vaddr)->sin_addr.s_addr = INADDR_ANY;
362 break;
9ea3ea10 363#ifdef RB_IPV6
54ac8b60
VY
364 case AF_INET6:
365 memcpy(&((struct sockaddr_in6 *)&vaddr)->sin6_addr, &in6addr_any, sizeof(struct in6_addr));
366 break;
367 default:
368 return;
369#endif
55abcbb2 370 }
54ac8b60
VY
371 }
372 switch(family)
373 {
374 case AF_INET:
99c4835f 375 SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in));
d86692fa
EM
376 SET_SS_FAMILY(&vaddr, AF_INET);
377 SET_SS_PORT(&vaddr, htons(port));
54ac8b60 378 break;
9ea3ea10 379#ifdef RB_IPV6
54ac8b60 380 case AF_INET6:
99c4835f 381 SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in6));
d86692fa
EM
382 SET_SS_FAMILY(&vaddr, AF_INET6);
383 SET_SS_PORT(&vaddr, htons(port));
54ac8b60
VY
384 break;
385#endif
386 default:
387 break;
388 }
389 if((listener = find_listener(&vaddr)))
390 {
1087485c 391 if(listener->F != NULL)
54ac8b60
VY
392 return;
393 }
394 else
395 {
396 listener = make_listener(&vaddr);
397 listener->next = ListenerPollList;
398 ListenerPollList = listener;
399 }
400
f691939a 401 listener->F = NULL;
c6d72037 402 listener->ssl = ssl;
02270e96 403 listener->defer_accept = defer_accept;
c53ca1e0 404 listener->wsock = wsock;
54ac8b60
VY
405
406 if(inetport(listener))
407 listener->active = 1;
408 else
409 close_listener(listener);
410}
411
412/*
413 * close_listener - close a single listener
414 */
415void
8f103562 416close_listener(struct Listener *listener)
54ac8b60
VY
417{
418 s_assert(listener != NULL);
419 if(listener == NULL)
420 return;
1087485c
JT
421 if(listener->F != NULL)
422 {
423 rb_close(listener->F);
424 listener->F = NULL;
54ac8b60
VY
425 }
426
427 listener->active = 0;
428
429 if(listener->ref_count)
430 return;
431
432 free_listener(listener);
433}
434
435/*
436 * close_listeners - close and free all listeners that are not being used
437 */
438void
439close_listeners()
440{
8f103562
JT
441 struct Listener *listener;
442 struct Listener *listener_next = 0;
54ac8b60
VY
443 /*
444 * close all 'extra' listening ports we have
445 */
446 for (listener = ListenerPollList; listener; listener = listener_next)
447 {
448 listener_next = listener->next;
449 close_listener(listener);
450 }
451}
452
453#define DLINE_WARNING "ERROR :You have been D-lined.\r\n"
454
455/*
55abcbb2 456 * add_connection - creates a client which has just connected to us on
54ac8b60
VY
457 * the given fd. The sockhost field is initialized with the ip# of the host.
458 * The client is sent to the auth module for verification, and not put in
459 * any client list yet.
460 */
1087485c 461static void
b5b4a0e7 462add_connection(struct Listener *listener, rb_fde_t *F, struct sockaddr *sai, struct sockaddr *lai)
54ac8b60
VY
463{
464 struct Client *new_client;
465 s_assert(NULL != listener);
466
55abcbb2 467 /*
54ac8b60
VY
468 * get the client socket name from the socket
469 * the client has already been checked out in accept_connection
470 */
471 new_client = make_client(NULL);
472
b5b4a0e7
AC
473 if (listener->ssl)
474 {
475 rb_fde_t *xF[2];
476 if(rb_socketpair(AF_UNIX, SOCK_STREAM, 0, &xF[0], &xF[1], "Incoming ssld Connection") == -1)
477 {
478 free_client(new_client);
479 return;
480 }
de7cf7e0 481 new_client->localClient->ssl_ctl = start_ssld_accept(F, xF[1], connid_get(new_client)); /* this will close F for us */
b5b4a0e7
AC
482 if(new_client->localClient->ssl_ctl == NULL)
483 {
484 free_client(new_client);
485 return;
486 }
487 F = xF[0];
488 SetSSL(new_client);
489 }
490
c53ca1e0
AC
491 if (listener->wsock)
492 {
493 rb_fde_t *xF[2];
494 if(rb_socketpair(AF_UNIX, SOCK_STREAM, 0, &xF[0], &xF[1], "Incoming wsockd Connection") == -1)
495 {
496 free_client(new_client);
497 return;
498 }
499 new_client->localClient->ws_ctl = start_wsockd_accept(F, xF[1], connid_get(new_client)); /* this will close F for us */
500 if(new_client->localClient->ws_ctl == NULL)
501 {
502 free_client(new_client);
503 return;
504 }
505 F = xF[0];
506 }
507
e7046ee5 508 memcpy(&new_client->localClient->ip, sai, sizeof(struct rb_sockaddr_storage));
3540120a 509 memcpy(&new_client->preClient->lip, lai, sizeof(struct rb_sockaddr_storage));
54ac8b60 510
55abcbb2 511 /*
54ac8b60
VY
512 * copy address to 'sockhost' as a string, copy it to host too
513 * so we have something valid to put into error messages...
514 */
55abcbb2 515 rb_inet_ntop_sock((struct sockaddr *)&new_client->localClient->ip, new_client->sockhost,
54ac8b60
VY
516 sizeof(new_client->sockhost));
517
518
f427c8b0 519 rb_strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
54ac8b60 520
99c4835f 521 new_client->localClient->F = F;
54ac8b60 522 new_client->localClient->listener = listener;
c6d72037 523
54ac8b60
VY
524 ++listener->ref_count;
525
d3f6b808 526 authd_initiate_client(new_client);
54ac8b60 527}
0d89d5cd 528
43946961
JT
529static const char *toofast = "ERROR :Reconnecting too fast, throttled.\r\n";
530
1087485c
JT
531static int
532accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data)
533{
534 struct Listener *listener = (struct Listener *)data;
535 char buf[BUFSIZE];
536 struct ConfItem *aconf;
537 static time_t last_oper_notice = 0;
b52c2949 538 int len;
1087485c 539
bfc44622 540 if(listener->ssl && (!ircd_ssl_ok || !get_ssld_count()))
8bd5767b
JT
541 {
542 rb_close(F);
543 return 0;
c6d72037
VY
544 }
545
1087485c
JT
546 if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus */
547 {
47adde3d 548 ++ServerStats.is_ref;
1087485c
JT
549 /*
550 * slow down the whining to opers bit
551 */
552 if((last_oper_notice + 20) <= rb_current_time())
553 {
26716d6d 554 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1087485c
JT
555 "All connections in use. (%s)",
556 get_listener_name(listener));
557 last_oper_notice = rb_current_time();
558 }
55abcbb2 559
76d82c19 560 rb_write(F, "ERROR :All connections in use\r\n", 31);
1087485c 561 rb_close(F);
1087485c
JT
562 return 0;
563 }
564
41d7fefa 565 aconf = find_dline(addr, addr->sa_family);
1087485c
JT
566 if(aconf != NULL && (aconf->status & CONF_EXEMPTDLINE))
567 return 1;
55abcbb2 568
1087485c
JT
569 /* Do an initial check we aren't connecting too fast or with too many
570 * from this IP... */
571 if(aconf != NULL)
572 {
47adde3d 573 ServerStats.is_ref++;
55abcbb2 574
1087485c
JT
575 if(ConfigFileEntry.dline_with_reason)
576 {
5203cba5 577 len = snprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", get_user_ban_reason(aconf));
b52c2949 578 if (len >= (int)(sizeof(buf)-1))
1087485c
JT
579 {
580 buf[sizeof(buf) - 3] = '\r';
581 buf[sizeof(buf) - 2] = '\n';
582 buf[sizeof(buf) - 1] = '\0';
583 }
584 }
585 else
586 strcpy(buf, "ERROR :You have been D-lined.\r\n");
55abcbb2 587
1087485c
JT
588 rb_write(F, buf, strlen(buf));
589 rb_close(F);
590 return 0;
591 }
592
43946961
JT
593 if(check_reject(F, addr))
594 return 0;
55abcbb2 595
43946961
JT
596 if(throttle_add(addr))
597 {
598 rb_write(F, toofast, strlen(toofast));
599 rb_close(F);
600 return 0;
601 }
602
1087485c
JT
603 return 1;
604}
605
606static void
607accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data)
608{
609 struct Listener *listener = data;
610 struct rb_sockaddr_storage lip;
611 unsigned int locallen = sizeof(struct rb_sockaddr_storage);
55abcbb2 612
47adde3d 613 ServerStats.is_ac++;
1087485c
JT
614
615 if(getsockname(rb_get_fd(F), (struct sockaddr *) &lip, &locallen) < 0)
616 {
d60a42a2 617 /* this can fail if the connection disappeared in the meantime */
1087485c 618 rb_close(F);
9692f954 619 return;
1087485c 620 }
55abcbb2 621
b5b4a0e7 622 add_connection(listener, F, addr, (struct sockaddr *)&lip);
0d89d5cd 623}