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