]> jfr.im git - solanum.git/blame - src/listener.c
server: Use rb_strlcpy() instead of strcpy().
[solanum.git] / src / 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
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"
4562c604 31#include "match.h"
54ac8b60
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"
c6d72037
VY
43#include "sslproc.h"
44#include "hash.h"
77d3d2db
KB
45#include "s_assert.h"
46#include "logger.h"
54ac8b60
VY
47
48#ifndef INADDR_NONE
49#define INADDR_NONE ((unsigned int) 0xffffffff)
50#endif
51
9ea3ea10 52#if defined(NO_IN6ADDR_ANY) && defined(RB_IPV6)
54ac8b60
VY
53static const struct in6_addr in6addr_any =
54{ { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } };
55#endif
56
8f103562 57static struct Listener *ListenerPollList = NULL;
1087485c 58static int accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data);
8e09c4a2 59static void accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data);
54ac8b60 60
8f103562 61static struct Listener *
e7046ee5 62make_listener(struct rb_sockaddr_storage *addr)
54ac8b60 63{
8f103562 64 struct Listener *listener = (struct Listener *) rb_malloc(sizeof(struct Listener));
54ac8b60 65 s_assert(0 != listener);
54ac8b60 66 listener->name = me.name;
f691939a
VY
67 listener->F = NULL;
68
e7046ee5 69 memcpy(&listener->addr, addr, sizeof(struct rb_sockaddr_storage));
54ac8b60
VY
70 listener->next = NULL;
71 return listener;
72}
73
74void
8f103562 75free_listener(struct Listener *listener)
54ac8b60
VY
76{
77 s_assert(NULL != listener);
78 if(listener == NULL)
79 return;
80 /*
81 * remove from listener list
82 */
83 if(listener == ListenerPollList)
84 ListenerPollList = listener->next;
85 else
86 {
8f103562 87 struct Listener *prev = ListenerPollList;
54ac8b60
VY
88 for (; prev; prev = prev->next)
89 {
90 if(listener == prev->next)
91 {
92 prev->next = listener->next;
93 break;
94 }
95 }
96 }
97
98 /* free */
99 rb_free(listener);
100}
101
102#define PORTNAMELEN 6 /* ":31337" */
103
104/*
105 * get_listener_name - return displayable listener name and port
106 * returns "host.foo.org:6667" for a given listener
107 */
108const char *
8f103562 109get_listener_name(const struct Listener *listener)
54ac8b60
VY
110{
111 static char buf[HOSTLEN + HOSTLEN + PORTNAMELEN + 4];
112 int port = 0;
113
114 s_assert(NULL != listener);
115 if(listener == NULL)
116 return NULL;
117
9ea3ea10 118#ifdef RB_IPV6
54ac8b60
VY
119 if(listener->addr.ss_family == AF_INET6)
120 port = ntohs(((const struct sockaddr_in6 *)&listener->addr)->sin6_port);
121 else
122#endif
123 port = ntohs(((const struct sockaddr_in *)&listener->addr)->sin_port);
124
125 rb_snprintf(buf, sizeof(buf), "%s[%s/%u]", me.name, listener->name, port);
126 return buf;
127}
128
129/*
130 * show_ports - send port listing to a client
131 * inputs - pointer to client to show ports to
132 * output - none
133 * side effects - show ports
134 */
135void
136show_ports(struct Client *source_p)
137{
8f103562 138 struct Listener *listener = 0;
54ac8b60
VY
139
140 for (listener = ListenerPollList; listener; listener = listener->next)
141 {
142 sendto_one_numeric(source_p, RPL_STATSPLINE,
143 form_str(RPL_STATSPLINE), 'P',
9ea3ea10 144#ifdef RB_IPV6
54ac8b60
VY
145 ntohs(listener->addr.ss_family == AF_INET ? ((struct sockaddr_in *)&listener->addr)->sin_port :
146 ((struct sockaddr_in6 *)&listener->addr)->sin6_port),
147#else
148 ntohs(((struct sockaddr_in *)&listener->addr)->sin_port),
149#endif
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
162 * use RATBOX_SOMAXCONN
163 */
164#ifdef SOMAXCONN
165#undef RATBOX_SOMAXCONN
166#define RATBOX_SOMAXCONN SOMAXCONN
167#endif
168
169static int
8f103562 170inetport(struct Listener *listener)
54ac8b60 171{
f691939a 172 rb_fde_t *F;
54ac8b60
VY
173 int opt = 1;
174
175 /*
176 * At first, open a new socket
177 */
178
f691939a 179 F = rb_socket(GET_SS_FAMILY(&listener->addr), SOCK_STREAM, 0, "Listener socket");
54ac8b60 180
9ea3ea10 181#ifdef RB_IPV6
54ac8b60
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 {
caa4d9d2 187 rb_inet_ntop(AF_INET6, &in6->sin6_addr, listener->vhost, sizeof(listener->vhost));
54ac8b60
VY
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 {
caa4d9d2 196 rb_inet_ntop(AF_INET, &in->sin_addr, listener->vhost, sizeof(listener->vhost));
54ac8b60
VY
197 listener->name = listener->vhost;
198 }
199 }
200
1087485c
JT
201 if(F == NULL)
202 {
825ddf13 203 ilog_error("opening listener socket");
1087485c
JT
204 return 0;
205 }
206 else if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus*/
207 {
825ddf13 208 ilog_error("no more connections left for listener");
1087485c
JT
209 rb_close(F);
210 return 0;
54ac8b60
VY
211 }
212
1087485c
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 {
825ddf13 219 ilog_error("setting SO_REUSEADDR for listener");
1087485c
JT
220 rb_close(F);
221 return 0;
54ac8b60
VY
222 }
223
1087485c
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 {
825ddf13 231 ilog_error("binding listener socket");
1087485c
JT
232 rb_close(F);
233 return 0;
54ac8b60
VY
234 }
235
02270e96 236 if(rb_listen(F, RATBOX_SOMAXCONN, listener->defer_accept))
1087485c 237 {
825ddf13 238 ilog_error("listen()");
1087485c
JT
239 rb_close(F);
240 return 0;
54ac8b60
VY
241 }
242
1087485c
JT
243 listener->F = F;
244
f691939a 245 rb_accept_tcp(listener->F, accept_precallback, accept_callback, listener);
54ac8b60
VY
246 return 1;
247}
248
8f103562 249static struct Listener *
e7046ee5 250find_listener(struct rb_sockaddr_storage *addr)
54ac8b60 251{
8f103562
JT
252 struct Listener *listener = NULL;
253 struct Listener *last_closed = NULL;
54ac8b60
VY
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 {
1087485c
JT
269 if(listener->F == NULL)
270 last_closed = listener;
271 else
54ac8b60
VY
272 return(listener);
273 }
274 break;
275 }
9ea3ea10 276#ifdef RB_IPV6
54ac8b60
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 {
1087485c
JT
284 if(listener->F == NULL)
285 last_closed = listener;
286 else
54ac8b60
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
02270e96 309add_listener(int port, const char *vhost_ip, int family, int ssl, int defer_accept)
54ac8b60 310{
8f103562 311 struct Listener *listener;
e7046ee5 312 struct rb_sockaddr_storage vaddr;
54ac8b60
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 {
caa4d9d2 326 if(rb_inet_pton(family, vhost_ip, &((struct sockaddr_in *)&vaddr)->sin_addr) <= 0)
54ac8b60
VY
327 return;
328 }
9ea3ea10 329#ifdef RB_IPV6
54ac8b60
VY
330 else
331 {
caa4d9d2 332 if(rb_inet_pton(family, vhost_ip, &((struct sockaddr_in6 *)&vaddr)->sin6_addr) <= 0)
54ac8b60
VY
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;
9ea3ea10 344#ifdef RB_IPV6
54ac8b60
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:
99c4835f 356 SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in));
54ac8b60
VY
357 ((struct sockaddr_in *)&vaddr)->sin_port = htons(port);
358 break;
9ea3ea10 359#ifdef RB_IPV6
54ac8b60 360 case AF_INET6:
99c4835f 361 SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in6));
54ac8b60
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 {
1087485c 370 if(listener->F != NULL)
54ac8b60
VY
371 return;
372 }
373 else
374 {
375 listener = make_listener(&vaddr);
376 listener->next = ListenerPollList;
377 ListenerPollList = listener;
378 }
379
f691939a 380 listener->F = NULL;
c6d72037 381 listener->ssl = ssl;
02270e96 382 listener->defer_accept = defer_accept;
54ac8b60
VY
383
384 if(inetport(listener))
385 listener->active = 1;
386 else
387 close_listener(listener);
388}
389
390/*
391 * close_listener - close a single listener
392 */
393void
8f103562 394close_listener(struct Listener *listener)
54ac8b60
VY
395{
396 s_assert(listener != NULL);
397 if(listener == NULL)
398 return;
1087485c
JT
399 if(listener->F != NULL)
400 {
401 rb_close(listener->F);
402 listener->F = NULL;
54ac8b60
VY
403 }
404
405 listener->active = 0;
406
407 if(listener->ref_count)
408 return;
409
410 free_listener(listener);
411}
412
413/*
414 * close_listeners - close and free all listeners that are not being used
415 */
416void
417close_listeners()
418{
8f103562
JT
419 struct Listener *listener;
420 struct Listener *listener_next = 0;
54ac8b60
VY
421 /*
422 * close all 'extra' listening ports we have
423 */
424 for (listener = ListenerPollList; listener; listener = listener_next)
425 {
426 listener_next = listener->next;
427 close_listener(listener);
428 }
429}
430
431#define DLINE_WARNING "ERROR :You have been D-lined.\r\n"
432
433/*
434 * add_connection - creates a client which has just connected to us on
435 * the given fd. The sockhost field is initialized with the ip# of the host.
436 * The client is sent to the auth module for verification, and not put in
437 * any client list yet.
438 */
1087485c 439static void
3540120a 440add_connection(struct Listener *listener, rb_fde_t *F, struct sockaddr *sai, struct sockaddr *lai, void *ssl_ctl)
54ac8b60
VY
441{
442 struct Client *new_client;
443 s_assert(NULL != listener);
444
445 /*
446 * get the client socket name from the socket
447 * the client has already been checked out in accept_connection
448 */
449 new_client = make_client(NULL);
450
e7046ee5 451 memcpy(&new_client->localClient->ip, sai, sizeof(struct rb_sockaddr_storage));
3540120a 452 memcpy(&new_client->preClient->lip, lai, sizeof(struct rb_sockaddr_storage));
54ac8b60
VY
453
454 /*
455 * copy address to 'sockhost' as a string, copy it to host too
456 * so we have something valid to put into error messages...
457 */
caa4d9d2 458 rb_inet_ntop_sock((struct sockaddr *)&new_client->localClient->ip, new_client->sockhost,
54ac8b60
VY
459 sizeof(new_client->sockhost));
460
461
f427c8b0 462 rb_strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
54ac8b60 463
99c4835f 464 new_client->localClient->F = F;
c6d72037 465 add_to_cli_fd_hash(new_client);
54ac8b60 466 new_client->localClient->listener = listener;
8bd5767b
JT
467 new_client->localClient->ssl_ctl = ssl_ctl;
468 if(ssl_ctl != NULL || rb_fd_ssl(F))
c6d72037
VY
469 SetSSL(new_client);
470
54ac8b60
VY
471 ++listener->ref_count;
472
54ac8b60
VY
473 start_auth(new_client);
474}
0d89d5cd 475
43946961
JT
476static const char *toofast = "ERROR :Reconnecting too fast, throttled.\r\n";
477
1087485c
JT
478static int
479accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data)
480{
481 struct Listener *listener = (struct Listener *)data;
482 char buf[BUFSIZE];
483 struct ConfItem *aconf;
484 static time_t last_oper_notice = 0;
b52c2949 485 int len;
1087485c 486
8bd5767b
JT
487 if(listener->ssl && (!ssl_ok || !get_ssld_count()))
488 {
489 rb_close(F);
490 return 0;
c6d72037
VY
491 }
492
1087485c
JT
493 if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus */
494 {
47adde3d 495 ++ServerStats.is_ref;
1087485c
JT
496 /*
497 * slow down the whining to opers bit
498 */
499 if((last_oper_notice + 20) <= rb_current_time())
500 {
26716d6d 501 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1087485c
JT
502 "All connections in use. (%s)",
503 get_listener_name(listener));
504 last_oper_notice = rb_current_time();
505 }
506
507 rb_write(F, "ERROR :All connections in use\r\n", 32);
508 rb_close(F);
1087485c
JT
509 return 0;
510 }
511
41d7fefa 512 aconf = find_dline(addr, addr->sa_family);
1087485c
JT
513 if(aconf != NULL && (aconf->status & CONF_EXEMPTDLINE))
514 return 1;
515
516 /* Do an initial check we aren't connecting too fast or with too many
517 * from this IP... */
518 if(aconf != NULL)
519 {
47adde3d 520 ServerStats.is_ref++;
1087485c
JT
521
522 if(ConfigFileEntry.dline_with_reason)
523 {
a12ad044 524 len = rb_snprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", get_user_ban_reason(aconf));
b52c2949 525 if (len >= (int)(sizeof(buf)-1))
1087485c
JT
526 {
527 buf[sizeof(buf) - 3] = '\r';
528 buf[sizeof(buf) - 2] = '\n';
529 buf[sizeof(buf) - 1] = '\0';
530 }
531 }
532 else
533 strcpy(buf, "ERROR :You have been D-lined.\r\n");
534
535 rb_write(F, buf, strlen(buf));
536 rb_close(F);
537 return 0;
538 }
539
43946961
JT
540 if(check_reject(F, addr))
541 return 0;
542
543 if(throttle_add(addr))
544 {
545 rb_write(F, toofast, strlen(toofast));
546 rb_close(F);
547 return 0;
548 }
549
1087485c
JT
550 return 1;
551}
552
8bd5767b
JT
553static void
554accept_ssld(rb_fde_t *F, struct sockaddr *addr, struct sockaddr *laddr, struct Listener *listener)
555{
556 ssl_ctl_t *ctl;
557 rb_fde_t *xF[2];
6388eda6
JT
558 if(rb_socketpair(AF_UNIX, SOCK_STREAM, 0, &xF[0], &xF[1], "Incoming ssld Connection") == -1)
559 {
560 ilog_error("creating SSL/TLS socket pairs");
561 rb_close(F);
562 return;
563 }
8bd5767b 564 ctl = start_ssld_accept(F, xF[1], rb_get_fd(xF[0])); /* this will close F for us */
3540120a 565 add_connection(listener, xF[0], addr, laddr, ctl);
c6d72037
VY
566}
567
1087485c
JT
568static void
569accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data)
570{
571 struct Listener *listener = data;
572 struct rb_sockaddr_storage lip;
573 unsigned int locallen = sizeof(struct rb_sockaddr_storage);
574
47adde3d 575 ServerStats.is_ac++;
1087485c
JT
576
577 if(getsockname(rb_get_fd(F), (struct sockaddr *) &lip, &locallen) < 0)
578 {
d60a42a2 579 /* this can fail if the connection disappeared in the meantime */
1087485c 580 rb_close(F);
9692f954 581 return;
1087485c
JT
582 }
583
8bd5767b
JT
584 if(listener->ssl)
585 accept_ssld(F, addr, (struct sockaddr *)&lip, listener);
586 else
3540120a 587 add_connection(listener, F, addr, (struct sockaddr *)&lip, NULL);
0d89d5cd 588}