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