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