]> jfr.im git - irc/rqf/shadowircd.git/blob - src/listener.c
sslproc.h include
[irc/rqf/shadowircd.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
22 * USA
23 *
24 * $Id: listener.c 25169 2008-03-29 21:41:31Z jilles $
25 */
26
27 #include "stdinc.h"
28 #include "ratbox_lib.h"
29 #include "listener.h"
30 #include "client.h"
31 #include "ircd.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_log.h"
40 #include "hash.h"
41 #include "sslproc.h"
42
43 static rb_dlink_list listener_list;
44 static int accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data);
45 static void accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data);
46
47
48
49 static struct Listener *
50 make_listener(struct rb_sockaddr_storage *addr)
51 {
52 struct Listener *listener = rb_malloc(sizeof(struct Listener));
53 s_assert(0 != listener);
54 listener->name = ServerInfo.name; /* me.name may not be valid yet -- jilles */
55 listener->F = NULL;
56 memcpy(&listener->addr, addr, sizeof(struct rb_sockaddr_storage));
57 return listener;
58 }
59
60 void
61 free_listener(struct Listener *listener)
62 {
63 s_assert(NULL != listener);
64 if(listener == NULL)
65 return;
66
67 rb_dlinkDelete(&listener->node, &listener_list);
68 rb_free(listener);
69 }
70
71 #define PORTNAMELEN 6 /* ":31337" */
72
73 /*
74 * get_listener_name - return displayable listener name and port
75 * returns "host.foo.org:6667" for a given listener
76 */
77 const char *
78 get_listener_name(struct Listener *listener)
79 {
80 static char buf[HOSTLEN + HOSTLEN + PORTNAMELEN + 4];
81 int port = 0;
82
83 s_assert(NULL != listener);
84 if(listener == NULL)
85 return NULL;
86
87 #ifdef IPV6
88 if(GET_SS_FAMILY(&listener->addr) == AF_INET6)
89 port = ntohs(((const struct sockaddr_in6 *)&listener->addr)->sin6_port);
90 else
91 #endif
92 port = ntohs(((const struct sockaddr_in *)&listener->addr)->sin_port);
93
94 rb_snprintf(buf, sizeof(buf), "%s[%s/%u]", me.name, listener->name, port);
95 return buf;
96 }
97
98 /*
99 * show_ports - send port listing to a client
100 * inputs - pointer to client to show ports to
101 * output - none
102 * side effects - show ports
103 */
104 void
105 show_ports(struct Client *source_p)
106 {
107 struct Listener *listener;
108 rb_dlink_node *ptr;
109
110 RB_DLINK_FOREACH(ptr, listener_list.head)
111 {
112 listener = ptr->data;
113 sendto_one_numeric(source_p, RPL_STATSPLINE,
114 form_str(RPL_STATSPLINE), 'P',
115 #ifdef IPV6
116 ntohs(GET_SS_FAMILY(&listener->addr) == AF_INET ? ((struct sockaddr_in *)&listener->addr)->sin_port :
117 ((struct sockaddr_in6 *)&listener->addr)->sin6_port),
118 #else
119 ntohs(((struct sockaddr_in *)&listener->addr)->sin_port),
120 #endif
121 IsOperAdmin(source_p) ? listener->name : me.name,
122 listener->ref_count, (listener->active) ? "active" : "disabled",
123 listener->ssl ? " ssl" : "");
124 }
125 }
126
127 /*
128 * inetport - create a listener socket in the AF_INET or AF_INET6 domain,
129 * bind it to the port given in 'port' and listen to it
130 * returns true (1) if successful false (0) on error.
131 *
132 * If the operating system has a define for SOMAXCONN, use it, otherwise
133 * use RATBOX_SOMAXCONN
134 */
135 #ifdef SOMAXCONN
136 #undef RATBOX_SOMAXCONN
137 #define RATBOX_SOMAXCONN SOMAXCONN
138 #endif
139
140 static int
141 inetport(struct Listener *listener)
142 {
143 rb_fde_t *F;
144 int ret;
145 int opt = 1;
146
147 /*
148 * At first, open a new socket
149 */
150
151 F = rb_socket(GET_SS_FAMILY(&listener->addr), SOCK_STREAM, 0, "Listener socket");
152
153 #ifdef IPV6
154 if(GET_SS_FAMILY(&listener->addr) == AF_INET6)
155 {
156 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&listener->addr;
157 if(!IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &in6addr_any))
158 {
159 rb_inet_ntop(AF_INET6, &in6->sin6_addr, listener->vhost, sizeof(listener->vhost));
160 listener->name = listener->vhost;
161 }
162 } else
163 #endif
164 {
165 struct sockaddr_in *in = (struct sockaddr_in *)&listener->addr;
166 if(in->sin_addr.s_addr != INADDR_ANY)
167 {
168 rb_inet_ntop(AF_INET, &in->sin_addr, listener->vhost, sizeof(listener->vhost));
169 listener->name = listener->vhost;
170 }
171 }
172
173
174 if(F == NULL)
175 {
176 report_error("opening listener socket %s:%s",
177 get_listener_name(listener),
178 get_listener_name(listener), errno);
179 return 0;
180 }
181 else if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus*/
182 {
183 report_error("no more connections left for listener %s:%s",
184 get_listener_name(listener),
185 get_listener_name(listener), errno);
186 rb_close(F);
187 return 0;
188 }
189 /*
190 * XXX - we don't want to do all this crap for a listener
191 * set_sock_opts(listener);
192 */
193 if(setsockopt(rb_get_fd(F), SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt)))
194 {
195 report_error("setting SO_REUSEADDR for listener %s:%s",
196 get_listener_name(listener),
197 get_listener_name(listener), errno);
198 rb_close(F);
199 return 0;
200 }
201
202 /*
203 * Bind a port to listen for new connections if port is non-null,
204 * else assume it is already open and try get something from it.
205 */
206
207 if(bind(rb_get_fd(F), (struct sockaddr *) &listener->addr, GET_SS_LEN(&listener->addr)))
208 {
209 report_error("binding listener socket %s:%s",
210 get_listener_name(listener),
211 get_listener_name(listener), errno);
212 rb_close(F);
213 return 0;
214 }
215
216 if((ret = rb_listen(F, RATBOX_SOMAXCONN)))
217 {
218 report_error("listen failed for %s:%s",
219 get_listener_name(listener),
220 get_listener_name(listener), errno);
221 rb_close(F);
222 return 0;
223 }
224
225 listener->F = F;
226
227 rb_accept_tcp(listener->F, accept_precallback, accept_callback, listener);
228 return 1;
229 }
230
231 static struct Listener *
232 find_listener(struct rb_sockaddr_storage *addr)
233 {
234 struct Listener *listener = NULL;
235 struct Listener *last_closed = NULL;
236 rb_dlink_node *ptr;
237
238 RB_DLINK_FOREACH(ptr, listener_list.head)
239 {
240 listener = ptr->data;
241 if(GET_SS_FAMILY(addr) != GET_SS_FAMILY(&listener->addr))
242 continue;
243
244 switch(GET_SS_FAMILY(addr))
245 {
246 case AF_INET:
247 {
248 struct sockaddr_in *in4 = (struct sockaddr_in *)addr;
249 struct sockaddr_in *lin4 = (struct sockaddr_in *)&listener->addr;
250 if(in4->sin_addr.s_addr == lin4->sin_addr.s_addr &&
251 in4->sin_port == lin4->sin_port )
252 {
253 if(listener->F == NULL)
254 last_closed = listener;
255 else
256 return(listener);
257 }
258 break;
259 }
260 #ifdef IPV6
261 case AF_INET6:
262 {
263 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)addr;
264 struct sockaddr_in6 *lin6 =(struct sockaddr_in6 *)&listener->addr;
265 if(IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &lin6->sin6_addr) &&
266 in6->sin6_port == lin6->sin6_port)
267 {
268 if(listener->F == NULL)
269 last_closed = listener;
270 else
271 return(listener);
272 }
273 break;
274
275 }
276 #endif
277
278 default:
279 break;
280 }
281 }
282 return last_closed;
283 }
284
285 /*
286 * add_listener- create a new listener
287 * port - the port number to listen on
288 * vhost_ip - if non-null must contain a valid IP address string in
289 * the format "255.255.255.255"
290 */
291 void
292 add_listener(int port, const char *vhost_ip, int family, int ssl)
293 {
294 struct Listener *listener;
295 struct rb_sockaddr_storage vaddr;
296
297 /*
298 * if no port in conf line, don't bother
299 */
300 if(port == 0)
301 return;
302
303 memset(&vaddr, 0, sizeof(vaddr));
304 GET_SS_FAMILY(&vaddr) = family;
305
306 if(vhost_ip != NULL)
307 {
308 if(rb_inet_pton_sock(vhost_ip, (struct sockaddr *)&vaddr) <= 0)
309 return;
310 } else
311 {
312 switch(family)
313 {
314 case AF_INET:
315 ((struct sockaddr_in *)&vaddr)->sin_addr.s_addr = INADDR_ANY;
316 break;
317 #ifdef IPV6
318 case AF_INET6:
319 memcpy(&((struct sockaddr_in6 *)&vaddr)->sin6_addr, &in6addr_any, sizeof(struct in6_addr));
320 break;
321 #endif
322 default:
323 return;
324 }
325 }
326 switch(family)
327 {
328 case AF_INET:
329 SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in));
330 ((struct sockaddr_in *)&vaddr)->sin_port = htons(port);
331 break;
332 #ifdef IPV6
333 case AF_INET6:
334 SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in6));
335 ((struct sockaddr_in6 *)&vaddr)->sin6_port = htons(port);
336 break;
337 #endif
338 default:
339 break;
340 }
341 if((listener = find_listener(&vaddr)))
342 {
343 if(listener->F != NULL)
344 return;
345 }
346 else
347 {
348 listener = make_listener(&vaddr);
349 rb_dlinkAdd(listener, &listener->node, &listener_list);
350 }
351
352 listener->F = NULL;
353 listener->ssl = ssl;
354 if(inetport(listener))
355 listener->active = 1;
356 else
357 close_listener(listener);
358 }
359
360 /*
361 * close_listener - close a single listener
362 */
363 void
364 close_listener(struct Listener *listener)
365 {
366 s_assert(listener != NULL);
367 if(listener == NULL)
368 return;
369 if(listener->F != NULL)
370 {
371 rb_close(listener->F);
372 listener->F = NULL;
373 }
374
375 listener->active = 0;
376
377 if(listener->ref_count)
378 return;
379
380 free_listener(listener);
381 }
382
383 /*
384 * close_listeners - close and free all listeners that are not being used
385 */
386 void
387 close_listeners()
388 {
389 struct Listener *listener;
390 rb_dlink_node *ptr, *next;
391
392 RB_DLINK_FOREACH_SAFE(ptr, next, listener_list.head)
393 {
394 listener = ptr->data;
395 close_listener(listener);
396 }
397 }
398
399 /*
400 * add_connection - creates a client which has just connected to us on
401 * the given fd. The sockhost field is initialized with the ip# of the host.
402 * The client is sent to the auth module for verification, and not put in
403 * any client list yet.
404 */
405 static void
406 add_connection(struct Listener *listener, rb_fde_t *F, struct sockaddr *sai, struct sockaddr *lai, void *ssl_ctl)
407 {
408 struct Client *new_client;
409 s_assert(NULL != listener);
410 /*
411 * get the client socket name from the socket
412 * the client has already been checked out in accept_connection
413 */
414 new_client = make_client(NULL);
415
416 memcpy(&new_client->localClient->ip, sai, sizeof(struct rb_sockaddr_storage));
417 new_client->localClient->lip = rb_malloc(sizeof(struct rb_sockaddr_storage));
418 memcpy(new_client->localClient->lip, lai, sizeof(struct rb_sockaddr_storage));
419
420 /*
421 * copy address to 'sockhost' as a string, copy it to host too
422 * so we have something valid to put into error messages...
423 */
424 rb_inet_ntop_sock((struct sockaddr *)&new_client->localClient->ip, new_client->sockhost,
425 sizeof(new_client->sockhost));
426
427
428 rb_strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
429
430 #ifdef IPV6
431 if(GET_SS_FAMILY(&new_client->localClient->ip) == AF_INET6 && ConfigFileEntry.dot_in_ip6_addr == 1)
432 {
433 rb_strlcat(new_client->host, ".", sizeof(new_client->host));
434 }
435 #endif
436
437 new_client->localClient->F = F;
438 add_to_cli_fd_hash(new_client);
439 new_client->localClient->listener = listener;
440 new_client->localClient->ssl_ctl = ssl_ctl;
441 if(ssl_ctl != NULL || rb_fd_ssl(F))
442 SetSSL(new_client);
443
444 ++listener->ref_count;
445
446 start_auth(new_client);
447 }
448
449 static time_t last_oper_notice = 0;
450
451 static const char *toofast = "ERROR :Reconnecting too fast, throttled.\r\n";
452
453 static int
454 accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data)
455 {
456 struct Listener *listener = (struct Listener *)data;
457 char buf[BUFSIZE];
458 struct ConfItem *aconf;
459
460 if(listener->ssl && (!ssl_ok || !get_ssld_count()))
461 {
462 fprintf(stderr, "closed socket\n");
463 rb_close(F);
464 return 0;
465 }
466
467 if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus */
468 {
469 ++ServerStats.is_ref;
470 /*
471 * slow down the whining to opers bit
472 */
473 if((last_oper_notice + 20) <= rb_current_time())
474 {
475 sendto_realops_flags(UMODE_ALL, L_ALL,
476 "All connections in use. (%s)",
477 get_listener_name(listener));
478 last_oper_notice = rb_current_time();
479 }
480
481 rb_write(F, "ERROR :All connections in use\r\n", 32);
482 rb_close(F);
483 /* Re-register a new IO request for the next accept .. */
484 return 0;
485 }
486
487 aconf = find_dline(addr);
488 if(aconf != NULL && (aconf->status & CONF_EXEMPTDLINE))
489 return 1;
490
491 /* Do an initial check we aren't connecting too fast or with too many
492 * from this IP... */
493 if(aconf != NULL)
494 {
495 ServerStats.is_ref++;
496
497 if(ConfigFileEntry.dline_with_reason)
498 {
499 if (rb_snprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", aconf->passwd) >= (int)(sizeof(buf)-1))
500 {
501 buf[sizeof(buf) - 3] = '\r';
502 buf[sizeof(buf) - 2] = '\n';
503 buf[sizeof(buf) - 1] = '\0';
504 }
505 }
506 else
507 strcpy(buf, "ERROR :You have been D-lined.\r\n");
508
509 rb_write(F, buf, strlen(buf));
510 rb_close(F);
511 return 0;
512 }
513
514 if(check_reject(F, addr))
515 return 0;
516
517 if(throttle_add(addr))
518 {
519 rb_write(F, toofast, strlen(toofast));
520 rb_close(F);
521 return 0;
522 }
523
524 return 1;
525 }
526
527 static void
528 accept_ssld(rb_fde_t *F, struct sockaddr *addr, struct sockaddr *laddr, struct Listener *listener)
529 {
530 ssl_ctl_t *ctl;
531 rb_fde_t *xF[2];
532 rb_socketpair(AF_UNIX, SOCK_STREAM, 0, &xF[0], &xF[1], "Incoming ssld Connection");
533 ctl = start_ssld_accept(F, xF[1], rb_get_fd(xF[0])); /* this will close F for us */
534 add_connection(listener, xF[0], addr, laddr, ctl);
535 }
536
537 static void
538 accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data)
539 {
540 struct Listener *listener = data;
541 struct rb_sockaddr_storage lip;
542 unsigned int locallen = sizeof(struct rb_sockaddr_storage);
543
544 ServerStats.is_ac++;
545 if(getsockname(rb_get_fd(F), (struct sockaddr *) &lip, &locallen) < 0)
546 {
547 /* this shouldn't fail so... */
548 /* XXX add logging of this */
549 rb_close(F);
550 }
551 if(listener->ssl)
552 accept_ssld(F, addr, (struct sockaddr *)&lip, listener);
553 else
554 add_connection(listener, F, addr, (struct sockaddr *)&lip, NULL);
555 }