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