]> jfr.im git - solanum.git/blame - ircd/listener.c
ircd: do not shadow internal openssl symbol "ssl_ok" (yeah, i know)
[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"
37#include "s_auth.h"
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
109 if(listener->addr.ss_family == AF_INET6)
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
54ac8b60
VY
183 if(listener->addr.ss_family == AF_INET6)
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);
226 */
227 if(setsockopt(rb_get_fd(F), SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt)))
228 {
40173bcb
JT
229 errstr = strerror(rb_get_sockerr(F));
230 sendto_realops_snomask(SNO_GENERAL, L_ALL,
231 "Cannot set SO_REUSEADDR for listener on port %d: %s",
232 get_listener_port(listener), errstr);
233 ilog(L_MAIN, "Cannot set SO_REUSEADDR for listener %s: %s",
234 get_listener_name(listener), errstr);
1087485c
JT
235 rb_close(F);
236 return 0;
54ac8b60
VY
237 }
238
1087485c
JT
239 /*
240 * Bind a port to listen for new connections if port is non-null,
241 * else assume it is already open and try get something from it.
242 */
243
244 if(bind(rb_get_fd(F), (struct sockaddr *) &listener->addr, GET_SS_LEN(&listener->addr)))
245 {
40173bcb
JT
246 errstr = strerror(rb_get_sockerr(F));
247 sendto_realops_snomask(SNO_GENERAL, L_ALL,
248 "Cannot bind for listener on port %d: %s",
249 get_listener_port(listener), errstr);
250 ilog(L_MAIN, "Cannot bind for listener %s: %s",
251 get_listener_name(listener), errstr);
1087485c
JT
252 rb_close(F);
253 return 0;
54ac8b60
VY
254 }
255
638d2862 256 if(rb_listen(F, CHARYBDIS_SOMAXCONN, listener->defer_accept))
1087485c 257 {
40173bcb
JT
258 errstr = strerror(rb_get_sockerr(F));
259 sendto_realops_snomask(SNO_GENERAL, L_ALL,
260 "Cannot listen() for listener on port %d: %s",
261 get_listener_port(listener), errstr);
262 ilog(L_MAIN, "Cannot listen() for listener %s: %s",
263 get_listener_name(listener), errstr);
1087485c
JT
264 rb_close(F);
265 return 0;
54ac8b60
VY
266 }
267
1087485c
JT
268 listener->F = F;
269
f691939a 270 rb_accept_tcp(listener->F, accept_precallback, accept_callback, listener);
54ac8b60
VY
271 return 1;
272}
273
8f103562 274static struct Listener *
e7046ee5 275find_listener(struct rb_sockaddr_storage *addr)
54ac8b60 276{
8f103562
JT
277 struct Listener *listener = NULL;
278 struct Listener *last_closed = NULL;
54ac8b60
VY
279
280 for (listener = ListenerPollList; listener; listener = listener->next)
281 {
282 if(addr->ss_family != listener->addr.ss_family)
283 continue;
55abcbb2 284
54ac8b60
VY
285 switch(addr->ss_family)
286 {
287 case AF_INET:
288 {
289 struct sockaddr_in *in4 = (struct sockaddr_in *)addr;
290 struct sockaddr_in *lin4 = (struct sockaddr_in *)&listener->addr;
55abcbb2 291 if(in4->sin_addr.s_addr == lin4->sin_addr.s_addr &&
54ac8b60
VY
292 in4->sin_port == lin4->sin_port )
293 {
1087485c
JT
294 if(listener->F == NULL)
295 last_closed = listener;
296 else
54ac8b60
VY
297 return(listener);
298 }
299 break;
300 }
9ea3ea10 301#ifdef RB_IPV6
54ac8b60
VY
302 case AF_INET6:
303 {
304 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)addr;
305 struct sockaddr_in6 *lin6 =(struct sockaddr_in6 *)&listener->addr;
306 if(IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &lin6->sin6_addr) &&
307 in6->sin6_port == lin6->sin6_port)
308 {
1087485c
JT
309 if(listener->F == NULL)
310 last_closed = listener;
311 else
54ac8b60
VY
312 return(listener);
313 }
314 break;
55abcbb2 315
54ac8b60
VY
316 }
317#endif
318
319 default:
320 break;
321 }
322 }
323 return last_closed;
324}
325
326
327/*
328 * add_listener- create a new listener
329 * port - the port number to listen on
330 * vhost_ip - if non-null must contain a valid IP address string in
331 * the format "255.255.255.255"
332 */
333void
02270e96 334add_listener(int port, const char *vhost_ip, int family, int ssl, int defer_accept)
54ac8b60 335{
8f103562 336 struct Listener *listener;
e7046ee5 337 struct rb_sockaddr_storage vaddr;
54ac8b60
VY
338
339 /*
340 * if no port in conf line, don't bother
341 */
342 if(port == 0)
343 return;
344 memset(&vaddr, 0, sizeof(vaddr));
345 vaddr.ss_family = family;
346
347 if(vhost_ip != NULL)
348 {
349 if(family == AF_INET)
350 {
caa4d9d2 351 if(rb_inet_pton(family, vhost_ip, &((struct sockaddr_in *)&vaddr)->sin_addr) <= 0)
54ac8b60 352 return;
55abcbb2 353 }
9ea3ea10 354#ifdef RB_IPV6
54ac8b60
VY
355 else
356 {
caa4d9d2 357 if(rb_inet_pton(family, vhost_ip, &((struct sockaddr_in6 *)&vaddr)->sin6_addr) <= 0)
54ac8b60 358 return;
55abcbb2 359
54ac8b60
VY
360 }
361#endif
362 } else
363 {
364 switch(family)
365 {
366 case AF_INET:
367 ((struct sockaddr_in *)&vaddr)->sin_addr.s_addr = INADDR_ANY;
368 break;
9ea3ea10 369#ifdef RB_IPV6
54ac8b60
VY
370 case AF_INET6:
371 memcpy(&((struct sockaddr_in6 *)&vaddr)->sin6_addr, &in6addr_any, sizeof(struct in6_addr));
372 break;
373 default:
374 return;
375#endif
55abcbb2 376 }
54ac8b60
VY
377 }
378 switch(family)
379 {
380 case AF_INET:
99c4835f 381 SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in));
54ac8b60
VY
382 ((struct sockaddr_in *)&vaddr)->sin_port = htons(port);
383 break;
9ea3ea10 384#ifdef RB_IPV6
54ac8b60 385 case AF_INET6:
99c4835f 386 SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in6));
54ac8b60
VY
387 ((struct sockaddr_in6 *)&vaddr)->sin6_port = htons(port);
388 break;
389#endif
390 default:
391 break;
392 }
393 if((listener = find_listener(&vaddr)))
394 {
1087485c 395 if(listener->F != NULL)
54ac8b60
VY
396 return;
397 }
398 else
399 {
400 listener = make_listener(&vaddr);
401 listener->next = ListenerPollList;
402 ListenerPollList = listener;
403 }
404
f691939a 405 listener->F = NULL;
c6d72037 406 listener->ssl = ssl;
02270e96 407 listener->defer_accept = defer_accept;
54ac8b60
VY
408
409 if(inetport(listener))
410 listener->active = 1;
411 else
412 close_listener(listener);
413}
414
415/*
416 * close_listener - close a single listener
417 */
418void
8f103562 419close_listener(struct Listener *listener)
54ac8b60
VY
420{
421 s_assert(listener != NULL);
422 if(listener == NULL)
423 return;
1087485c
JT
424 if(listener->F != NULL)
425 {
426 rb_close(listener->F);
427 listener->F = NULL;
54ac8b60
VY
428 }
429
430 listener->active = 0;
431
432 if(listener->ref_count)
433 return;
434
435 free_listener(listener);
436}
437
438/*
439 * close_listeners - close and free all listeners that are not being used
440 */
441void
442close_listeners()
443{
8f103562
JT
444 struct Listener *listener;
445 struct Listener *listener_next = 0;
54ac8b60
VY
446 /*
447 * close all 'extra' listening ports we have
448 */
449 for (listener = ListenerPollList; listener; listener = listener_next)
450 {
451 listener_next = listener->next;
452 close_listener(listener);
453 }
454}
455
456#define DLINE_WARNING "ERROR :You have been D-lined.\r\n"
457
458/*
55abcbb2 459 * add_connection - creates a client which has just connected to us on
54ac8b60
VY
460 * the given fd. The sockhost field is initialized with the ip# of the host.
461 * The client is sent to the auth module for verification, and not put in
462 * any client list yet.
463 */
1087485c 464static void
b5b4a0e7 465add_connection(struct Listener *listener, rb_fde_t *F, struct sockaddr *sai, struct sockaddr *lai)
54ac8b60
VY
466{
467 struct Client *new_client;
468 s_assert(NULL != listener);
469
55abcbb2 470 /*
54ac8b60
VY
471 * get the client socket name from the socket
472 * the client has already been checked out in accept_connection
473 */
474 new_client = make_client(NULL);
475
b5b4a0e7
AC
476 if (listener->ssl)
477 {
478 rb_fde_t *xF[2];
479 if(rb_socketpair(AF_UNIX, SOCK_STREAM, 0, &xF[0], &xF[1], "Incoming ssld Connection") == -1)
480 {
481 free_client(new_client);
482 return;
483 }
484 new_client->localClient->ssl_ctl = start_ssld_accept(F, xF[1], new_client->localClient->connid); /* this will close F for us */
485 if(new_client->localClient->ssl_ctl == NULL)
486 {
487 free_client(new_client);
488 return;
489 }
490 F = xF[0];
491 SetSSL(new_client);
492 }
493
e7046ee5 494 memcpy(&new_client->localClient->ip, sai, sizeof(struct rb_sockaddr_storage));
3540120a 495 memcpy(&new_client->preClient->lip, lai, sizeof(struct rb_sockaddr_storage));
54ac8b60 496
55abcbb2 497 /*
54ac8b60
VY
498 * copy address to 'sockhost' as a string, copy it to host too
499 * so we have something valid to put into error messages...
500 */
55abcbb2 501 rb_inet_ntop_sock((struct sockaddr *)&new_client->localClient->ip, new_client->sockhost,
54ac8b60
VY
502 sizeof(new_client->sockhost));
503
504
f427c8b0 505 rb_strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
54ac8b60 506
99c4835f 507 new_client->localClient->F = F;
54ac8b60 508 new_client->localClient->listener = listener;
c6d72037 509
54ac8b60
VY
510 ++listener->ref_count;
511
54ac8b60
VY
512 start_auth(new_client);
513}
0d89d5cd 514
43946961
JT
515static const char *toofast = "ERROR :Reconnecting too fast, throttled.\r\n";
516
1087485c
JT
517static int
518accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data)
519{
520 struct Listener *listener = (struct Listener *)data;
521 char buf[BUFSIZE];
522 struct ConfItem *aconf;
523 static time_t last_oper_notice = 0;
b52c2949 524 int len;
1087485c 525
bfc44622 526 if(listener->ssl && (!ircd_ssl_ok || !get_ssld_count()))
8bd5767b
JT
527 {
528 rb_close(F);
529 return 0;
c6d72037
VY
530 }
531
1087485c
JT
532 if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus */
533 {
47adde3d 534 ++ServerStats.is_ref;
1087485c
JT
535 /*
536 * slow down the whining to opers bit
537 */
538 if((last_oper_notice + 20) <= rb_current_time())
539 {
26716d6d 540 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1087485c
JT
541 "All connections in use. (%s)",
542 get_listener_name(listener));
543 last_oper_notice = rb_current_time();
544 }
55abcbb2 545
76d82c19 546 rb_write(F, "ERROR :All connections in use\r\n", 31);
1087485c 547 rb_close(F);
1087485c
JT
548 return 0;
549 }
550
41d7fefa 551 aconf = find_dline(addr, addr->sa_family);
1087485c
JT
552 if(aconf != NULL && (aconf->status & CONF_EXEMPTDLINE))
553 return 1;
55abcbb2 554
1087485c
JT
555 /* Do an initial check we aren't connecting too fast or with too many
556 * from this IP... */
557 if(aconf != NULL)
558 {
47adde3d 559 ServerStats.is_ref++;
55abcbb2 560
1087485c
JT
561 if(ConfigFileEntry.dline_with_reason)
562 {
5203cba5 563 len = snprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", get_user_ban_reason(aconf));
b52c2949 564 if (len >= (int)(sizeof(buf)-1))
1087485c
JT
565 {
566 buf[sizeof(buf) - 3] = '\r';
567 buf[sizeof(buf) - 2] = '\n';
568 buf[sizeof(buf) - 1] = '\0';
569 }
570 }
571 else
572 strcpy(buf, "ERROR :You have been D-lined.\r\n");
55abcbb2 573
1087485c
JT
574 rb_write(F, buf, strlen(buf));
575 rb_close(F);
576 return 0;
577 }
578
43946961
JT
579 if(check_reject(F, addr))
580 return 0;
55abcbb2 581
43946961
JT
582 if(throttle_add(addr))
583 {
584 rb_write(F, toofast, strlen(toofast));
585 rb_close(F);
586 return 0;
587 }
588
1087485c
JT
589 return 1;
590}
591
592static void
593accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data)
594{
595 struct Listener *listener = data;
596 struct rb_sockaddr_storage lip;
597 unsigned int locallen = sizeof(struct rb_sockaddr_storage);
55abcbb2 598
47adde3d 599 ServerStats.is_ac++;
1087485c
JT
600
601 if(getsockname(rb_get_fd(F), (struct sockaddr *) &lip, &locallen) < 0)
602 {
d60a42a2 603 /* this can fail if the connection disappeared in the meantime */
1087485c 604 rb_close(F);
9692f954 605 return;
1087485c 606 }
55abcbb2 607
b5b4a0e7 608 add_connection(listener, F, addr, (struct sockaddr *)&lip);
0d89d5cd 609}