]> jfr.im git - solanum.git/blob - ircd/listener.c
opm: add configuration interface for configuring scan ports
[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 "authd.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 return ntohs(GET_SS_PORT(&listener->addr));
109 }
110
111 /*
112 * get_listener_name - return displayable listener name and port
113 * returns "host.foo.org:6667" for a given listener
114 */
115 const char *
116 get_listener_name(const struct Listener *listener)
117 {
118 static char buf[HOSTLEN + HOSTLEN + PORTNAMELEN + 4];
119
120 s_assert(NULL != listener);
121 if(listener == NULL)
122 return NULL;
123
124 snprintf(buf, sizeof(buf), "%s[%s/%u]",
125 me.name, listener->name, get_listener_port(listener));
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 */
135 void
136 show_ports(struct Client *source_p)
137 {
138 struct Listener *listener = 0;
139
140 for (listener = ListenerPollList; listener; listener = listener->next)
141 {
142 sendto_one_numeric(source_p, RPL_STATSPLINE,
143 form_str(RPL_STATSPLINE), 'P',
144 get_listener_port(listener),
145 IsOperAdmin(source_p) ? listener->name : me.name,
146 listener->ref_count, (listener->active) ? "active" : "disabled",
147 listener->ssl ? " ssl" : "");
148 }
149 }
150
151 /*
152 * inetport - create a listener socket in the AF_INET or AF_INET6 domain,
153 * bind it to the port given in 'port' and listen to it
154 * returns true (1) if successful false (0) on error.
155 *
156 * If the operating system has a define for SOMAXCONN, use it, otherwise
157 * use CHARYBDIS_SOMAXCONN
158 */
159 #ifdef SOMAXCONN
160 #undef CHARYBDIS_SOMAXCONN
161 #define CHARYBDIS_SOMAXCONN SOMAXCONN
162 #endif
163
164 static int
165 inetport(struct Listener *listener)
166 {
167 rb_fde_t *F;
168 int opt = 1;
169 const char *errstr;
170
171 /*
172 * At first, open a new socket
173 */
174
175 F = rb_socket(GET_SS_FAMILY(&listener->addr), SOCK_STREAM, 0, "Listener socket");
176
177 #ifdef RB_IPV6
178 if(GET_SS_FAMILY(&listener->addr) == AF_INET6)
179 {
180 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&listener->addr;
181 if(!IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &in6addr_any))
182 {
183 rb_inet_ntop(AF_INET6, &in6->sin6_addr, listener->vhost, sizeof(listener->vhost));
184 listener->name = listener->vhost;
185 }
186 } else
187 #endif
188 {
189 struct sockaddr_in *in = (struct sockaddr_in *)&listener->addr;
190 if(in->sin_addr.s_addr != INADDR_ANY)
191 {
192 rb_inet_ntop(AF_INET, &in->sin_addr, listener->vhost, sizeof(listener->vhost));
193 listener->name = listener->vhost;
194 }
195 }
196
197 if(F == NULL)
198 {
199 sendto_realops_snomask(SNO_GENERAL, L_ALL,
200 "Cannot open socket for listener on port %d",
201 get_listener_port(listener));
202 ilog(L_MAIN, "Cannot open socket for listener %s",
203 get_listener_name(listener));
204 return 0;
205 }
206 else if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus*/
207 {
208 ilog_error("no more connections left for listener");
209 sendto_realops_snomask(SNO_GENERAL, L_ALL,
210 "No more connections left for listener on port %d",
211 get_listener_port(listener));
212 ilog(L_MAIN, "No more connections left for listener %s",
213 get_listener_name(listener));
214 rb_close(F);
215 return 0;
216 }
217
218 /*
219 * XXX - we don't want to do all this crap for a listener
220 * set_sock_opts(listener);
221 *
222 * FIXME - doesn't this belong in librb? --Elizafox
223 */
224 if(setsockopt(rb_get_fd(F), SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt)))
225 {
226 errstr = strerror(rb_get_sockerr(F));
227 sendto_realops_snomask(SNO_GENERAL, L_ALL,
228 "Cannot set SO_REUSEADDR for listener on port %d: %s",
229 get_listener_port(listener), errstr);
230 ilog(L_MAIN, "Cannot set SO_REUSEADDR for listener %s: %s",
231 get_listener_name(listener), errstr);
232 rb_close(F);
233 return 0;
234 }
235
236 /* FIXME - doesn't this belong in librb? --Elizafox */
237 if(bind(rb_get_fd(F), (struct sockaddr *) &listener->addr, GET_SS_LEN(&listener->addr)))
238 {
239 errstr = strerror(rb_get_sockerr(F));
240 sendto_realops_snomask(SNO_GENERAL, L_ALL,
241 "Cannot bind for listener on port %d: %s",
242 get_listener_port(listener), errstr);
243 ilog(L_MAIN, "Cannot bind for listener %s: %s",
244 get_listener_name(listener), errstr);
245 rb_close(F);
246 return 0;
247 }
248
249 if(rb_listen(F, CHARYBDIS_SOMAXCONN, listener->defer_accept))
250 {
251 errstr = strerror(rb_get_sockerr(F));
252 sendto_realops_snomask(SNO_GENERAL, L_ALL,
253 "Cannot listen() for listener on port %d: %s",
254 get_listener_port(listener), errstr);
255 ilog(L_MAIN, "Cannot listen() for listener %s: %s",
256 get_listener_name(listener), errstr);
257 rb_close(F);
258 return 0;
259 }
260
261 listener->F = F;
262
263 rb_accept_tcp(listener->F, accept_precallback, accept_callback, listener);
264 return 1;
265 }
266
267 static struct Listener *
268 find_listener(struct rb_sockaddr_storage *addr)
269 {
270 struct Listener *listener = NULL;
271 struct Listener *last_closed = NULL;
272
273 for (listener = ListenerPollList; listener; listener = listener->next)
274 {
275 if(GET_SS_FAMILY(addr) != GET_SS_FAMILY(&listener->addr))
276 continue;
277
278 switch(GET_SS_FAMILY(addr))
279 {
280 case AF_INET:
281 {
282 struct sockaddr_in *in4 = (struct sockaddr_in *)addr;
283 struct sockaddr_in *lin4 = (struct sockaddr_in *)&listener->addr;
284 if(in4->sin_addr.s_addr == lin4->sin_addr.s_addr &&
285 in4->sin_port == lin4->sin_port )
286 {
287 if(listener->F == NULL)
288 last_closed = listener;
289 else
290 return(listener);
291 }
292 break;
293 }
294 #ifdef RB_IPV6
295 case AF_INET6:
296 {
297 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)addr;
298 struct sockaddr_in6 *lin6 =(struct sockaddr_in6 *)&listener->addr;
299 if(IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &lin6->sin6_addr) &&
300 in6->sin6_port == lin6->sin6_port)
301 {
302 if(listener->F == NULL)
303 last_closed = listener;
304 else
305 return(listener);
306 }
307 break;
308
309 }
310 #endif
311
312 default:
313 break;
314 }
315 }
316 return last_closed;
317 }
318
319
320 /*
321 * add_listener- create a new listener
322 * port - the port number to listen on
323 * vhost_ip - if non-null must contain a valid IP address string in
324 * the format "255.255.255.255"
325 */
326 void
327 add_listener(int port, const char *vhost_ip, int family, int ssl, int defer_accept)
328 {
329 struct Listener *listener;
330 struct rb_sockaddr_storage vaddr;
331
332 /*
333 * if no port in conf line, don't bother
334 */
335 if(port == 0)
336 return;
337 memset(&vaddr, 0, sizeof(vaddr));
338 SET_SS_FAMILY(&vaddr, family);
339
340 if(vhost_ip != NULL)
341 {
342 if(family == AF_INET)
343 {
344 if(rb_inet_pton(family, vhost_ip, &((struct sockaddr_in *)&vaddr)->sin_addr) <= 0)
345 return;
346 }
347 #ifdef RB_IPV6
348 else
349 {
350 if(rb_inet_pton(family, vhost_ip, &((struct sockaddr_in6 *)&vaddr)->sin6_addr) <= 0)
351 return;
352
353 }
354 #endif
355 } else
356 {
357 switch(family)
358 {
359 case AF_INET:
360 ((struct sockaddr_in *)&vaddr)->sin_addr.s_addr = INADDR_ANY;
361 break;
362 #ifdef RB_IPV6
363 case AF_INET6:
364 memcpy(&((struct sockaddr_in6 *)&vaddr)->sin6_addr, &in6addr_any, sizeof(struct in6_addr));
365 break;
366 default:
367 return;
368 #endif
369 }
370 }
371 switch(family)
372 {
373 case AF_INET:
374 SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in));
375 SET_SS_FAMILY(&vaddr, AF_INET);
376 SET_SS_PORT(&vaddr, htons(port));
377 break;
378 #ifdef RB_IPV6
379 case AF_INET6:
380 SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in6));
381 SET_SS_FAMILY(&vaddr, AF_INET6);
382 SET_SS_PORT(&vaddr, htons(port));
383 break;
384 #endif
385 default:
386 break;
387 }
388 if((listener = find_listener(&vaddr)))
389 {
390 if(listener->F != NULL)
391 return;
392 }
393 else
394 {
395 listener = make_listener(&vaddr);
396 listener->next = ListenerPollList;
397 ListenerPollList = listener;
398 }
399
400 listener->F = NULL;
401 listener->ssl = ssl;
402 listener->defer_accept = defer_accept;
403
404 if(inetport(listener))
405 listener->active = 1;
406 else
407 close_listener(listener);
408 }
409
410 /*
411 * close_listener - close a single listener
412 */
413 void
414 close_listener(struct Listener *listener)
415 {
416 s_assert(listener != NULL);
417 if(listener == NULL)
418 return;
419 if(listener->F != NULL)
420 {
421 rb_close(listener->F);
422 listener->F = NULL;
423 }
424
425 listener->active = 0;
426
427 if(listener->ref_count)
428 return;
429
430 free_listener(listener);
431 }
432
433 /*
434 * close_listeners - close and free all listeners that are not being used
435 */
436 void
437 close_listeners()
438 {
439 struct Listener *listener;
440 struct Listener *listener_next = 0;
441 /*
442 * close all 'extra' listening ports we have
443 */
444 for (listener = ListenerPollList; listener; listener = listener_next)
445 {
446 listener_next = listener->next;
447 close_listener(listener);
448 }
449 }
450
451 #define DLINE_WARNING "ERROR :You have been D-lined.\r\n"
452
453 /*
454 * add_connection - creates a client which has just connected to us on
455 * the given fd. The sockhost field is initialized with the ip# of the host.
456 * The client is sent to the auth module for verification, and not put in
457 * any client list yet.
458 */
459 static void
460 add_connection(struct Listener *listener, rb_fde_t *F, struct sockaddr *sai, struct sockaddr *lai)
461 {
462 struct Client *new_client;
463 s_assert(NULL != listener);
464
465 /*
466 * get the client socket name from the socket
467 * the client has already been checked out in accept_connection
468 */
469 new_client = make_client(NULL);
470
471 if (listener->ssl)
472 {
473 rb_fde_t *xF[2];
474 if(rb_socketpair(AF_UNIX, SOCK_STREAM, 0, &xF[0], &xF[1], "Incoming ssld Connection") == -1)
475 {
476 free_client(new_client);
477 return;
478 }
479 new_client->localClient->ssl_ctl = start_ssld_accept(F, xF[1], connid_get(new_client)); /* this will close F for us */
480 if(new_client->localClient->ssl_ctl == NULL)
481 {
482 free_client(new_client);
483 return;
484 }
485 F = xF[0];
486 SetSSL(new_client);
487 }
488
489 memcpy(&new_client->localClient->ip, sai, sizeof(struct rb_sockaddr_storage));
490 memcpy(&new_client->preClient->lip, lai, sizeof(struct rb_sockaddr_storage));
491
492 /*
493 * copy address to 'sockhost' as a string, copy it to host too
494 * so we have something valid to put into error messages...
495 */
496 rb_inet_ntop_sock((struct sockaddr *)&new_client->localClient->ip, new_client->sockhost,
497 sizeof(new_client->sockhost));
498
499
500 rb_strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
501
502 new_client->localClient->F = F;
503 new_client->localClient->listener = listener;
504
505 ++listener->ref_count;
506
507 authd_initiate_client(new_client);
508 }
509
510 static const char *toofast = "ERROR :Reconnecting too fast, throttled.\r\n";
511
512 static int
513 accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data)
514 {
515 struct Listener *listener = (struct Listener *)data;
516 char buf[BUFSIZE];
517 struct ConfItem *aconf;
518 static time_t last_oper_notice = 0;
519 int len;
520
521 if(listener->ssl && (!ircd_ssl_ok || !get_ssld_count()))
522 {
523 rb_close(F);
524 return 0;
525 }
526
527 if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus */
528 {
529 ++ServerStats.is_ref;
530 /*
531 * slow down the whining to opers bit
532 */
533 if((last_oper_notice + 20) <= rb_current_time())
534 {
535 sendto_realops_snomask(SNO_GENERAL, L_ALL,
536 "All connections in use. (%s)",
537 get_listener_name(listener));
538 last_oper_notice = rb_current_time();
539 }
540
541 rb_write(F, "ERROR :All connections in use\r\n", 31);
542 rb_close(F);
543 return 0;
544 }
545
546 aconf = find_dline(addr, addr->sa_family);
547 if(aconf != NULL && (aconf->status & CONF_EXEMPTDLINE))
548 return 1;
549
550 /* Do an initial check we aren't connecting too fast or with too many
551 * from this IP... */
552 if(aconf != NULL)
553 {
554 ServerStats.is_ref++;
555
556 if(ConfigFileEntry.dline_with_reason)
557 {
558 len = snprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", get_user_ban_reason(aconf));
559 if (len >= (int)(sizeof(buf)-1))
560 {
561 buf[sizeof(buf) - 3] = '\r';
562 buf[sizeof(buf) - 2] = '\n';
563 buf[sizeof(buf) - 1] = '\0';
564 }
565 }
566 else
567 strcpy(buf, "ERROR :You have been D-lined.\r\n");
568
569 rb_write(F, buf, strlen(buf));
570 rb_close(F);
571 return 0;
572 }
573
574 if(check_reject(F, addr))
575 return 0;
576
577 if(throttle_add(addr))
578 {
579 rb_write(F, toofast, strlen(toofast));
580 rb_close(F);
581 return 0;
582 }
583
584 return 1;
585 }
586
587 static void
588 accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data)
589 {
590 struct Listener *listener = data;
591 struct rb_sockaddr_storage lip;
592 unsigned int locallen = sizeof(struct rb_sockaddr_storage);
593
594 ServerStats.is_ac++;
595
596 if(getsockname(rb_get_fd(F), (struct sockaddr *) &lip, &locallen) < 0)
597 {
598 /* this can fail if the connection disappeared in the meantime */
599 rb_close(F);
600 return;
601 }
602
603 add_connection(listener, F, addr, (struct sockaddr *)&lip);
604 }