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