]> jfr.im git - solanum.git/blob - ircd/listener.c
Change some RATBOX_FOO defines to CHARYBDIS_FOO.
[solanum.git] / ircd / listener.c
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
25 #include "stdinc.h"
26 #include "setup.h"
27 #include "listener.h"
28 #include "client.h"
29 #include "match.h"
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"
41 #include "sslproc.h"
42 #include "hash.h"
43 #include "s_assert.h"
44 #include "logger.h"
45
46 #ifndef INADDR_NONE
47 #define INADDR_NONE ((unsigned int) 0xffffffff)
48 #endif
49
50 #if defined(NO_IN6ADDR_ANY) && defined(RB_IPV6)
51 static 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
55 static struct Listener *ListenerPollList = NULL;
56 static int accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data);
57 static void accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data);
58
59 static struct Listener *
60 make_listener(struct rb_sockaddr_storage *addr)
61 {
62 struct Listener *listener = (struct Listener *) rb_malloc(sizeof(struct Listener));
63 s_assert(0 != listener);
64 listener->name = me.name;
65 listener->F = NULL;
66
67 memcpy(&listener->addr, addr, sizeof(struct rb_sockaddr_storage));
68 listener->next = NULL;
69 return listener;
70 }
71
72 void
73 free_listener(struct Listener *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 struct Listener *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_port - return displayable listener port
104 */
105 static uint16_t
106 get_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
116 /*
117 * get_listener_name - return displayable listener name and port
118 * returns "host.foo.org:6667" for a given listener
119 */
120 const char *
121 get_listener_name(const struct Listener *listener)
122 {
123 static char buf[HOSTLEN + HOSTLEN + PORTNAMELEN + 4];
124
125 s_assert(NULL != listener);
126 if(listener == NULL)
127 return NULL;
128
129 snprintf(buf, sizeof(buf), "%s[%s/%u]",
130 me.name, listener->name, get_listener_port(listener));
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 */
140 void
141 show_ports(struct Client *source_p)
142 {
143 struct Listener *listener = 0;
144
145 for (listener = ListenerPollList; listener; listener = listener->next)
146 {
147 sendto_one_numeric(source_p, RPL_STATSPLINE,
148 form_str(RPL_STATSPLINE), 'P',
149 get_listener_port(listener),
150 IsOperAdmin(source_p) ? listener->name : me.name,
151 listener->ref_count, (listener->active) ? "active" : "disabled",
152 listener->ssl ? " ssl" : "");
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 CHARYBDIS_SOMAXCONN
163 */
164 #ifdef SOMAXCONN
165 #undef CHARYBDIS_SOMAXCONN
166 #define CHARYBDIS_SOMAXCONN SOMAXCONN
167 #endif
168
169 static int
170 inetport(struct Listener *listener)
171 {
172 rb_fde_t *F;
173 int opt = 1;
174 const char *errstr;
175
176 /*
177 * At first, open a new socket
178 */
179
180 F = rb_socket(GET_SS_FAMILY(&listener->addr), SOCK_STREAM, 0, "Listener socket");
181
182 #ifdef RB_IPV6
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 {
188 rb_inet_ntop(AF_INET6, &in6->sin6_addr, listener->vhost, sizeof(listener->vhost));
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 {
197 rb_inet_ntop(AF_INET, &in->sin_addr, listener->vhost, sizeof(listener->vhost));
198 listener->name = listener->vhost;
199 }
200 }
201
202 if(F == NULL)
203 {
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));
209 return 0;
210 }
211 else if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus*/
212 {
213 ilog_error("no more connections left for listener");
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));
219 rb_close(F);
220 return 0;
221 }
222
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 {
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);
235 rb_close(F);
236 return 0;
237 }
238
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 {
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);
252 rb_close(F);
253 return 0;
254 }
255
256 if(rb_listen(F, CHARYBDIS_SOMAXCONN, listener->defer_accept))
257 {
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);
264 rb_close(F);
265 return 0;
266 }
267
268 listener->F = F;
269
270 rb_accept_tcp(listener->F, accept_precallback, accept_callback, listener);
271 return 1;
272 }
273
274 static struct Listener *
275 find_listener(struct rb_sockaddr_storage *addr)
276 {
277 struct Listener *listener = NULL;
278 struct Listener *last_closed = NULL;
279
280 for (listener = ListenerPollList; listener; listener = listener->next)
281 {
282 if(addr->ss_family != listener->addr.ss_family)
283 continue;
284
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;
291 if(in4->sin_addr.s_addr == lin4->sin_addr.s_addr &&
292 in4->sin_port == lin4->sin_port )
293 {
294 if(listener->F == NULL)
295 last_closed = listener;
296 else
297 return(listener);
298 }
299 break;
300 }
301 #ifdef RB_IPV6
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 {
309 if(listener->F == NULL)
310 last_closed = listener;
311 else
312 return(listener);
313 }
314 break;
315
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 */
333 void
334 add_listener(int port, const char *vhost_ip, int family, int ssl, int defer_accept)
335 {
336 struct Listener *listener;
337 struct rb_sockaddr_storage vaddr;
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 {
351 if(rb_inet_pton(family, vhost_ip, &((struct sockaddr_in *)&vaddr)->sin_addr) <= 0)
352 return;
353 }
354 #ifdef RB_IPV6
355 else
356 {
357 if(rb_inet_pton(family, vhost_ip, &((struct sockaddr_in6 *)&vaddr)->sin6_addr) <= 0)
358 return;
359
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;
369 #ifdef RB_IPV6
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
376 }
377 }
378 switch(family)
379 {
380 case AF_INET:
381 SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in));
382 ((struct sockaddr_in *)&vaddr)->sin_port = htons(port);
383 break;
384 #ifdef RB_IPV6
385 case AF_INET6:
386 SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in6));
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 {
395 if(listener->F != NULL)
396 return;
397 }
398 else
399 {
400 listener = make_listener(&vaddr);
401 listener->next = ListenerPollList;
402 ListenerPollList = listener;
403 }
404
405 listener->F = NULL;
406 listener->ssl = ssl;
407 listener->defer_accept = defer_accept;
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 */
418 void
419 close_listener(struct Listener *listener)
420 {
421 s_assert(listener != NULL);
422 if(listener == NULL)
423 return;
424 if(listener->F != NULL)
425 {
426 rb_close(listener->F);
427 listener->F = NULL;
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 */
441 void
442 close_listeners()
443 {
444 struct Listener *listener;
445 struct Listener *listener_next = 0;
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 /*
459 * add_connection - creates a client which has just connected to us on
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 */
464 static void
465 add_connection(struct Listener *listener, rb_fde_t *F, struct sockaddr *sai, struct sockaddr *lai)
466 {
467 struct Client *new_client;
468 s_assert(NULL != listener);
469
470 /*
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
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
494 memcpy(&new_client->localClient->ip, sai, sizeof(struct rb_sockaddr_storage));
495 memcpy(&new_client->preClient->lip, lai, sizeof(struct rb_sockaddr_storage));
496
497 /*
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 */
501 rb_inet_ntop_sock((struct sockaddr *)&new_client->localClient->ip, new_client->sockhost,
502 sizeof(new_client->sockhost));
503
504
505 rb_strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
506
507 new_client->localClient->F = F;
508 new_client->localClient->listener = listener;
509
510 ++listener->ref_count;
511
512 start_auth(new_client);
513 }
514
515 static const char *toofast = "ERROR :Reconnecting too fast, throttled.\r\n";
516
517 static int
518 accept_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;
524 int len;
525
526 if(listener->ssl && (!ssl_ok || !get_ssld_count()))
527 {
528 rb_close(F);
529 return 0;
530 }
531
532 if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus */
533 {
534 ++ServerStats.is_ref;
535 /*
536 * slow down the whining to opers bit
537 */
538 if((last_oper_notice + 20) <= rb_current_time())
539 {
540 sendto_realops_snomask(SNO_GENERAL, L_ALL,
541 "All connections in use. (%s)",
542 get_listener_name(listener));
543 last_oper_notice = rb_current_time();
544 }
545
546 rb_write(F, "ERROR :All connections in use\r\n", 31);
547 rb_close(F);
548 return 0;
549 }
550
551 aconf = find_dline(addr, addr->sa_family);
552 if(aconf != NULL && (aconf->status & CONF_EXEMPTDLINE))
553 return 1;
554
555 /* Do an initial check we aren't connecting too fast or with too many
556 * from this IP... */
557 if(aconf != NULL)
558 {
559 ServerStats.is_ref++;
560
561 if(ConfigFileEntry.dline_with_reason)
562 {
563 len = snprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", get_user_ban_reason(aconf));
564 if (len >= (int)(sizeof(buf)-1))
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");
573
574 rb_write(F, buf, strlen(buf));
575 rb_close(F);
576 return 0;
577 }
578
579 if(check_reject(F, addr))
580 return 0;
581
582 if(throttle_add(addr))
583 {
584 rb_write(F, toofast, strlen(toofast));
585 rb_close(F);
586 return 0;
587 }
588
589 return 1;
590 }
591
592 static void
593 accept_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);
598
599 ServerStats.is_ac++;
600
601 if(getsockname(rb_get_fd(F), (struct sockaddr *) &lip, &locallen) < 0)
602 {
603 /* this can fail if the connection disappeared in the meantime */
604 rb_close(F);
605 return;
606 }
607
608 add_connection(listener, F, addr, (struct sockaddr *)&lip);
609 }