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