]> jfr.im git - irc/rqf/shadowircd.git/blame - src/listener.c
Fix some part of IPv6 dline checking.
[irc/rqf/shadowircd.git] / src / listener.c
CommitLineData
21c9d815
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
23 *
24 * $Id: listener.c 3460 2007-05-18 20:31:33Z jilles $
25 */
26
27#include "stdinc.h"
28#include "setup.h"
29#include "listener.h"
30#include "client.h"
13ae2f4b 31#include "match.h"
21c9d815
VY
32#include "ircd.h"
33#include "ircd_defs.h"
34#include "numeric.h"
35#include "s_conf.h"
36#include "s_newconf.h"
37#include "s_stats.h"
38#include "send.h"
39#include "s_auth.h"
40#include "reject.h"
41#include "s_conf.h"
42#include "hostmask.h"
8db00894
VY
43#include "sslproc.h"
44#include "hash.h"
21c9d815
VY
45
46#ifndef INADDR_NONE
47#define INADDR_NONE ((unsigned int) 0xffffffff)
48#endif
49
2f5fa921 50#if defined(NO_IN6ADDR_ANY) && defined(RB_IPV6)
21c9d815
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 } } };
53#endif
54
21c9d815 55static listener_t *ListenerPollList = NULL;
ae4091d2 56static int accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data);
734bfb87 57static void accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data);
21c9d815
VY
58
59static listener_t *
3ea5fee7 60make_listener(struct rb_sockaddr_storage *addr)
21c9d815
VY
61{
62 listener_t *listener = (listener_t *) rb_malloc(sizeof(listener_t));
63 s_assert(0 != listener);
21c9d815 64 listener->name = me.name;
fb968f05
VY
65 listener->F = NULL;
66
3ea5fee7 67 memcpy(&listener->addr, addr, sizeof(struct rb_sockaddr_storage));
21c9d815
VY
68 listener->next = NULL;
69 return listener;
70}
71
72void
73free_listener(listener_t *listener)
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 {
85 listener_t *prev = ListenerPollList;
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
102/*
103 * get_listener_name - return displayable listener name and port
104 * returns "host.foo.org:6667" for a given listener
105 */
106const char *
107get_listener_name(const listener_t *listener)
108{
109 static char buf[HOSTLEN + HOSTLEN + PORTNAMELEN + 4];
110 int port = 0;
111
112 s_assert(NULL != listener);
113 if(listener == NULL)
114 return NULL;
115
2f5fa921 116#ifdef RB_IPV6
21c9d815
VY
117 if(listener->addr.ss_family == AF_INET6)
118 port = ntohs(((const struct sockaddr_in6 *)&listener->addr)->sin6_port);
119 else
120#endif
121 port = ntohs(((const struct sockaddr_in *)&listener->addr)->sin_port);
122
123 rb_snprintf(buf, sizeof(buf), "%s[%s/%u]", me.name, listener->name, port);
124 return buf;
125}
126
127/*
128 * show_ports - send port listing to a client
129 * inputs - pointer to client to show ports to
130 * output - none
131 * side effects - show ports
132 */
133void
134show_ports(struct Client *source_p)
135{
136 listener_t *listener = 0;
137
138 for (listener = ListenerPollList; listener; listener = listener->next)
139 {
140 sendto_one_numeric(source_p, RPL_STATSPLINE,
141 form_str(RPL_STATSPLINE), 'P',
2f5fa921 142#ifdef RB_IPV6
21c9d815
VY
143 ntohs(listener->addr.ss_family == AF_INET ? ((struct sockaddr_in *)&listener->addr)->sin_port :
144 ((struct sockaddr_in6 *)&listener->addr)->sin6_port),
145#else
146 ntohs(((struct sockaddr_in *)&listener->addr)->sin_port),
147#endif
148 IsOperAdmin(source_p) ? listener->name : me.name,
b717a466 149 listener->ref_count, (listener->active) ? "active" : "disabled",
8db00894 150 listener->ssl ? " ssl" : "");
21c9d815
VY
151 }
152}
153
154/*
155 * inetport - create a listener socket in the AF_INET or AF_INET6 domain,
156 * bind it to the port given in 'port' and listen to it
157 * returns true (1) if successful false (0) on error.
158 *
159 * If the operating system has a define for SOMAXCONN, use it, otherwise
160 * use RATBOX_SOMAXCONN
161 */
162#ifdef SOMAXCONN
163#undef RATBOX_SOMAXCONN
164#define RATBOX_SOMAXCONN SOMAXCONN
165#endif
166
167static int
168inetport(listener_t *listener)
169{
fb968f05 170 rb_fde_t *F;
2cbef5bc 171 int ret;
21c9d815
VY
172 int opt = 1;
173
174 /*
175 * At first, open a new socket
176 */
177
fb968f05 178 F = rb_socket(GET_SS_FAMILY(&listener->addr), SOCK_STREAM, 0, "Listener socket");
21c9d815 179
2f5fa921 180#ifdef RB_IPV6
21c9d815
VY
181 if(listener->addr.ss_family == AF_INET6)
182 {
183 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&listener->addr;
184 if(!IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &in6addr_any))
185 {
9879cd59 186 rb_inet_ntop(AF_INET6, &in6->sin6_addr, listener->vhost, sizeof(listener->vhost));
21c9d815
VY
187 listener->name = listener->vhost;
188 }
189 } else
190#endif
191 {
192 struct sockaddr_in *in = (struct sockaddr_in *)&listener->addr;
193 if(in->sin_addr.s_addr != INADDR_ANY)
194 {
9879cd59 195 rb_inet_ntop(AF_INET, &in->sin_addr, listener->vhost, sizeof(listener->vhost));
21c9d815
VY
196 listener->name = listener->vhost;
197 }
198 }
199
ae4091d2
JT
200 if(F == NULL)
201 {
d7b7d8bb 202 ilog_error("opening listener socket");
ae4091d2
JT
203 return 0;
204 }
205 else if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus*/
206 {
d7b7d8bb 207 ilog_error("no more connections left for listener");
ae4091d2
JT
208 rb_close(F);
209 return 0;
21c9d815
VY
210 }
211
ae4091d2
JT
212 /*
213 * XXX - we don't want to do all this crap for a listener
214 * set_sock_opts(listener);
215 */
216 if(setsockopt(rb_get_fd(F), SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt)))
217 {
d7b7d8bb 218 ilog_error("setting SO_REUSEADDR for listener");
ae4091d2
JT
219 rb_close(F);
220 return 0;
21c9d815
VY
221 }
222
ae4091d2
JT
223 /*
224 * Bind a port to listen for new connections if port is non-null,
225 * else assume it is already open and try get something from it.
226 */
227
228 if(bind(rb_get_fd(F), (struct sockaddr *) &listener->addr, GET_SS_LEN(&listener->addr)))
229 {
d7b7d8bb 230 ilog_error("binding listener socket");
ae4091d2
JT
231 rb_close(F);
232 return 0;
21c9d815
VY
233 }
234
ae4091d2
JT
235 if((ret = rb_listen(F, RATBOX_SOMAXCONN)))
236 {
d7b7d8bb 237 ilog_error("listen()");
ae4091d2
JT
238 rb_close(F);
239 return 0;
21c9d815
VY
240 }
241
ae4091d2
JT
242 listener->F = F;
243
fb968f05 244 rb_accept_tcp(listener->F, accept_precallback, accept_callback, listener);
21c9d815
VY
245 return 1;
246}
247
248static listener_t *
3ea5fee7 249find_listener(struct rb_sockaddr_storage *addr)
21c9d815
VY
250{
251 listener_t *listener = NULL;
252 listener_t *last_closed = NULL;
253
254 for (listener = ListenerPollList; listener; listener = listener->next)
255 {
256 if(addr->ss_family != listener->addr.ss_family)
257 continue;
258
259 switch(addr->ss_family)
260 {
261 case AF_INET:
262 {
263 struct sockaddr_in *in4 = (struct sockaddr_in *)addr;
264 struct sockaddr_in *lin4 = (struct sockaddr_in *)&listener->addr;
265 if(in4->sin_addr.s_addr == lin4->sin_addr.s_addr &&
266 in4->sin_port == lin4->sin_port )
267 {
ae4091d2
JT
268 if(listener->F == NULL)
269 last_closed = listener;
270 else
21c9d815
VY
271 return(listener);
272 }
273 break;
274 }
2f5fa921 275#ifdef RB_IPV6
21c9d815
VY
276 case AF_INET6:
277 {
278 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)addr;
279 struct sockaddr_in6 *lin6 =(struct sockaddr_in6 *)&listener->addr;
280 if(IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &lin6->sin6_addr) &&
281 in6->sin6_port == lin6->sin6_port)
282 {
ae4091d2
JT
283 if(listener->F == NULL)
284 last_closed = listener;
285 else
21c9d815
VY
286 return(listener);
287 }
288 break;
289
290 }
291#endif
292
293 default:
294 break;
295 }
296 }
297 return last_closed;
298}
299
300
301/*
302 * add_listener- create a new listener
303 * port - the port number to listen on
304 * vhost_ip - if non-null must contain a valid IP address string in
305 * the format "255.255.255.255"
306 */
307void
8db00894 308add_listener(int port, const char *vhost_ip, int family, int ssl)
21c9d815
VY
309{
310 listener_t *listener;
3ea5fee7 311 struct rb_sockaddr_storage vaddr;
21c9d815
VY
312
313 /*
314 * if no port in conf line, don't bother
315 */
316 if(port == 0)
317 return;
318 memset(&vaddr, 0, sizeof(vaddr));
319 vaddr.ss_family = family;
320
321 if(vhost_ip != NULL)
322 {
323 if(family == AF_INET)
324 {
9879cd59 325 if(rb_inet_pton(family, vhost_ip, &((struct sockaddr_in *)&vaddr)->sin_addr) <= 0)
21c9d815
VY
326 return;
327 }
2f5fa921 328#ifdef RB_IPV6
21c9d815
VY
329 else
330 {
9879cd59 331 if(rb_inet_pton(family, vhost_ip, &((struct sockaddr_in6 *)&vaddr)->sin6_addr) <= 0)
21c9d815
VY
332 return;
333
334 }
335#endif
336 } else
337 {
338 switch(family)
339 {
340 case AF_INET:
341 ((struct sockaddr_in *)&vaddr)->sin_addr.s_addr = INADDR_ANY;
342 break;
2f5fa921 343#ifdef RB_IPV6
21c9d815
VY
344 case AF_INET6:
345 memcpy(&((struct sockaddr_in6 *)&vaddr)->sin6_addr, &in6addr_any, sizeof(struct in6_addr));
346 break;
347 default:
348 return;
349#endif
350 }
351 }
352 switch(family)
353 {
354 case AF_INET:
9674dc79 355 SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in));
21c9d815
VY
356 ((struct sockaddr_in *)&vaddr)->sin_port = htons(port);
357 break;
2f5fa921 358#ifdef RB_IPV6
21c9d815 359 case AF_INET6:
9674dc79 360 SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in6));
21c9d815
VY
361 ((struct sockaddr_in6 *)&vaddr)->sin6_port = htons(port);
362 break;
363#endif
364 default:
365 break;
366 }
367 if((listener = find_listener(&vaddr)))
368 {
ae4091d2 369 if(listener->F != NULL)
21c9d815
VY
370 return;
371 }
372 else
373 {
374 listener = make_listener(&vaddr);
375 listener->next = ListenerPollList;
376 ListenerPollList = listener;
377 }
378
fb968f05 379 listener->F = NULL;
8db00894 380 listener->ssl = ssl;
21c9d815
VY
381
382 if(inetport(listener))
383 listener->active = 1;
384 else
385 close_listener(listener);
386}
387
388/*
389 * close_listener - close a single listener
390 */
391void
392close_listener(listener_t *listener)
393{
394 s_assert(listener != NULL);
395 if(listener == NULL)
396 return;
ae4091d2
JT
397 if(listener->F != NULL)
398 {
399 rb_close(listener->F);
400 listener->F = NULL;
21c9d815
VY
401 }
402
403 listener->active = 0;
404
405 if(listener->ref_count)
406 return;
407
408 free_listener(listener);
409}
410
411/*
412 * close_listeners - close and free all listeners that are not being used
413 */
414void
415close_listeners()
416{
417 listener_t *listener;
418 listener_t *listener_next = 0;
419 /*
420 * close all 'extra' listening ports we have
421 */
422 for (listener = ListenerPollList; listener; listener = listener_next)
423 {
424 listener_next = listener->next;
425 close_listener(listener);
426 }
427}
428
429#define DLINE_WARNING "ERROR :You have been D-lined.\r\n"
430
431/*
432 * add_connection - creates a client which has just connected to us on
433 * the given fd. The sockhost field is initialized with the ip# of the host.
434 * The client is sent to the auth module for verification, and not put in
435 * any client list yet.
436 */
ae4091d2 437static void
8db00894 438add_connection(struct Listener *listener, rb_fde_t *F, struct sockaddr *sai, void *ssl_ctl, int exempt)
21c9d815
VY
439{
440 struct Client *new_client;
441 s_assert(NULL != listener);
442
443 /*
444 * get the client socket name from the socket
445 * the client has already been checked out in accept_connection
446 */
447 new_client = make_client(NULL);
448
3ea5fee7 449 memcpy(&new_client->localClient->ip, sai, sizeof(struct rb_sockaddr_storage));
21c9d815
VY
450
451 /*
452 * copy address to 'sockhost' as a string, copy it to host too
453 * so we have something valid to put into error messages...
454 */
9879cd59 455 rb_inet_ntop_sock((struct sockaddr *)&new_client->localClient->ip, new_client->sockhost,
21c9d815
VY
456 sizeof(new_client->sockhost));
457
458
907468c4 459 rb_strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
21c9d815 460
9674dc79 461 new_client->localClient->F = F;
8db00894 462 add_to_cli_fd_hash(new_client);
21c9d815 463 new_client->localClient->listener = listener;
b717a466
JT
464 new_client->localClient->ssl_ctl = ssl_ctl;
465 if(ssl_ctl != NULL || rb_fd_ssl(F))
8db00894
VY
466 SetSSL(new_client);
467
21c9d815
VY
468 ++listener->ref_count;
469
470 if(!exempt)
471 {
472 if(check_reject(new_client))
473 return;
474 if(add_unknown_ip(new_client))
475 return;
476 }
477
478 start_auth(new_client);
479}
2cbef5bc 480
ae4091d2
JT
481static int
482accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data)
483{
484 struct Listener *listener = (struct Listener *)data;
485 char buf[BUFSIZE];
486 struct ConfItem *aconf;
487 static time_t last_oper_notice = 0;
488
b717a466
JT
489 if(listener->ssl && (!ssl_ok || !get_ssld_count()))
490 {
491 rb_close(F);
492 return 0;
8db00894
VY
493 }
494
ae4091d2
JT
495 if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus */
496 {
83251205 497 ++ServerStats.is_ref;
ae4091d2
JT
498 /*
499 * slow down the whining to opers bit
500 */
501 if((last_oper_notice + 20) <= rb_current_time())
502 {
03d18a46 503 sendto_realops_snomask(SNO_GENERAL, L_ALL,
ae4091d2
JT
504 "All connections in use. (%s)",
505 get_listener_name(listener));
506 last_oper_notice = rb_current_time();
507 }
508
509 rb_write(F, "ERROR :All connections in use\r\n", 32);
510 rb_close(F);
511 /* Re-register a new IO request for the next accept .. */
512 return 0;
513 }
514
bcd8cc02 515 aconf = find_dline(addr, addr->sa_family);
ae4091d2
JT
516 if(aconf != NULL && (aconf->status & CONF_EXEMPTDLINE))
517 return 1;
518
519 /* Do an initial check we aren't connecting too fast or with too many
520 * from this IP... */
521 if(aconf != NULL)
522 {
83251205 523 ServerStats.is_ref++;
ae4091d2
JT
524
525 if(ConfigFileEntry.dline_with_reason)
526 {
527 if (rb_snprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", aconf->passwd) >= (int)(sizeof(buf)-1))
528 {
529 buf[sizeof(buf) - 3] = '\r';
530 buf[sizeof(buf) - 2] = '\n';
531 buf[sizeof(buf) - 1] = '\0';
532 }
533 }
534 else
535 strcpy(buf, "ERROR :You have been D-lined.\r\n");
536
537 rb_write(F, buf, strlen(buf));
538 rb_close(F);
539 return 0;
540 }
541
542 return 1;
543}
544
b717a466
JT
545static void
546accept_ssld(rb_fde_t *F, struct sockaddr *addr, struct sockaddr *laddr, struct Listener *listener)
547{
548 ssl_ctl_t *ctl;
549 rb_fde_t *xF[2];
550 rb_socketpair(AF_UNIX, SOCK_STREAM, 0, &xF[0], &xF[1], "Incoming ssld Connection");
551 ctl = start_ssld_accept(F, xF[1], rb_get_fd(xF[0])); /* this will close F for us */
552 add_connection(listener, xF[0], addr, ctl, 1);
8db00894
VY
553}
554
ae4091d2
JT
555static void
556accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data)
557{
558 struct Listener *listener = data;
559 struct rb_sockaddr_storage lip;
560 unsigned int locallen = sizeof(struct rb_sockaddr_storage);
561
83251205 562 ServerStats.is_ac++;
ae4091d2
JT
563
564 if(getsockname(rb_get_fd(F), (struct sockaddr *) &lip, &locallen) < 0)
565 {
566 /* this shouldn't fail so... */
567 /* XXX add logging of this */
568 rb_close(F);
569 }
570
b717a466
JT
571 if(listener->ssl)
572 accept_ssld(F, addr, (struct sockaddr *)&lip, listener);
573 else
8db00894 574 add_connection(listener, F, addr, NULL, 1);
2cbef5bc 575}