]>
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 | ||
45 | #ifndef INADDR_NONE | |
46 | #define INADDR_NONE ((unsigned int) 0xffffffff) | |
47 | #endif | |
48 | ||
49 | #if defined(NO_IN6ADDR_ANY) && defined(IPV6) | |
50 | static const struct in6_addr in6addr_any = | |
51 | { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }; | |
52 | #endif | |
53 | ||
54 | static PF accept_connection; | |
55 | ||
56 | static listener_t *ListenerPollList = NULL; | |
57 | ||
58 | static listener_t * | |
59 | make_listener(struct irc_sockaddr_storage *addr) | |
60 | { | |
61 | listener_t *listener = (listener_t *) rb_malloc(sizeof(listener_t)); | |
62 | s_assert(0 != listener); | |
63 | ||
64 | listener->name = me.name; | |
65 | listener->fd = -1; | |
66 | memcpy(&listener->addr, addr, sizeof(struct irc_sockaddr_storage)); | |
67 | listener->next = NULL; | |
68 | return listener; | |
69 | } | |
70 | ||
71 | void | |
72 | free_listener(listener_t *listener) | |
73 | { | |
74 | s_assert(NULL != listener); | |
75 | if(listener == NULL) | |
76 | return; | |
77 | /* | |
78 | * remove from listener list | |
79 | */ | |
80 | if(listener == ListenerPollList) | |
81 | ListenerPollList = listener->next; | |
82 | else | |
83 | { | |
84 | listener_t *prev = ListenerPollList; | |
85 | for (; prev; prev = prev->next) | |
86 | { | |
87 | if(listener == prev->next) | |
88 | { | |
89 | prev->next = listener->next; | |
90 | break; | |
91 | } | |
92 | } | |
93 | } | |
94 | ||
95 | /* free */ | |
96 | rb_free(listener); | |
97 | } | |
98 | ||
99 | #define PORTNAMELEN 6 /* ":31337" */ | |
100 | ||
101 | /* | |
102 | * get_listener_name - return displayable listener name and port | |
103 | * returns "host.foo.org:6667" for a given listener | |
104 | */ | |
105 | const char * | |
106 | get_listener_name(const listener_t *listener) | |
107 | { | |
108 | static char buf[HOSTLEN + HOSTLEN + PORTNAMELEN + 4]; | |
109 | int port = 0; | |
110 | ||
111 | s_assert(NULL != listener); | |
112 | if(listener == NULL) | |
113 | return NULL; | |
114 | ||
115 | #ifdef IPV6 | |
116 | if(listener->addr.ss_family == AF_INET6) | |
117 | port = ntohs(((const struct sockaddr_in6 *)&listener->addr)->sin6_port); | |
118 | else | |
119 | #endif | |
120 | port = ntohs(((const struct sockaddr_in *)&listener->addr)->sin_port); | |
121 | ||
122 | rb_snprintf(buf, sizeof(buf), "%s[%s/%u]", me.name, listener->name, port); | |
123 | return buf; | |
124 | } | |
125 | ||
126 | /* | |
127 | * show_ports - send port listing to a client | |
128 | * inputs - pointer to client to show ports to | |
129 | * output - none | |
130 | * side effects - show ports | |
131 | */ | |
132 | void | |
133 | show_ports(struct Client *source_p) | |
134 | { | |
135 | listener_t *listener = 0; | |
136 | ||
137 | for (listener = ListenerPollList; listener; listener = listener->next) | |
138 | { | |
139 | sendto_one_numeric(source_p, RPL_STATSPLINE, | |
140 | form_str(RPL_STATSPLINE), 'P', | |
141 | #ifdef IPV6 | |
142 | ntohs(listener->addr.ss_family == AF_INET ? ((struct sockaddr_in *)&listener->addr)->sin_port : | |
143 | ((struct sockaddr_in6 *)&listener->addr)->sin6_port), | |
144 | #else | |
145 | ntohs(((struct sockaddr_in *)&listener->addr)->sin_port), | |
146 | #endif | |
147 | IsOperAdmin(source_p) ? listener->name : me.name, | |
148 | listener->ref_count, (listener->active) ? "active" : "disabled"); | |
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 | * If the operating system has a define for SOMAXCONN, use it, otherwise | |
158 | * use RATBOX_SOMAXCONN | |
159 | */ | |
160 | #ifdef SOMAXCONN | |
161 | #undef RATBOX_SOMAXCONN | |
162 | #define RATBOX_SOMAXCONN SOMAXCONN | |
163 | #endif | |
164 | ||
165 | static int | |
166 | inetport(listener_t *listener) | |
167 | { | |
168 | int fd; | |
169 | int opt = 1; | |
170 | ||
171 | /* | |
172 | * At first, open a new socket | |
173 | */ | |
174 | ||
175 | fd = rb_socket(listener->addr.ss_family, SOCK_STREAM, 0, "Listener socket"); | |
176 | ||
177 | #ifdef IPV6 | |
178 | if(listener->addr.ss_family == AF_INET6) | |
179 | { | |
180 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&listener->addr; | |
181 | if(!IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &in6addr_any)) | |
182 | { | |
183 | inetntop(AF_INET6, &in6->sin6_addr, listener->vhost, sizeof(listener->vhost)); | |
184 | listener->name = listener->vhost; | |
185 | } | |
186 | } else | |
187 | #endif | |
188 | { | |
189 | struct sockaddr_in *in = (struct sockaddr_in *)&listener->addr; | |
190 | if(in->sin_addr.s_addr != INADDR_ANY) | |
191 | { | |
192 | inetntop(AF_INET, &in->sin_addr, listener->vhost, sizeof(listener->vhost)); | |
193 | listener->name = listener->vhost; | |
194 | } | |
195 | } | |
196 | ||
197 | /* | |
198 | * At one point, we enforced a strange arbitrary limit here. | |
199 | * We no longer do this, and just check if the fd is valid or not. | |
200 | * -nenolod | |
201 | */ | |
202 | if(fd == -1) | |
203 | { | |
204 | report_error("opening listener socket %s:%s", | |
205 | get_listener_name(listener), | |
206 | get_listener_name(listener), errno); | |
207 | return 0; | |
208 | } | |
209 | ||
210 | /* | |
211 | * XXX - we don't want to do all this crap for a listener | |
212 | * set_sock_opts(listener); | |
213 | */ | |
214 | if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt))) | |
215 | { | |
216 | report_error("setting SO_REUSEADDR for listener %s:%s", | |
217 | get_listener_name(listener), | |
218 | get_listener_name(listener), errno); | |
219 | rb_close(fd); | |
220 | return 0; | |
221 | } | |
222 | ||
223 | /* | |
224 | * Bind a port to listen for new connections if port is non-null, | |
225 | * else assume it is already open and try get something from it. | |
226 | */ | |
227 | ||
228 | if(bind(fd, (struct sockaddr *) &listener->addr, GET_SS_LEN(listener->addr))) | |
229 | { | |
230 | report_error("binding listener socket %s:%s", | |
231 | get_listener_name(listener), | |
232 | get_listener_name(listener), errno); | |
233 | rb_close(fd); | |
234 | return 0; | |
235 | } | |
236 | ||
237 | if(listen(fd, RATBOX_SOMAXCONN)) | |
238 | { | |
239 | report_error("listen failed for %s:%s", | |
240 | get_listener_name(listener), | |
241 | get_listener_name(listener), errno); | |
242 | rb_close(fd); | |
243 | return 0; | |
244 | } | |
245 | ||
246 | listener->fd = fd; | |
247 | ||
248 | /* Listen completion events are READ events .. */ | |
249 | ||
250 | accept_connection(fd, listener); | |
251 | return 1; | |
252 | } | |
253 | ||
254 | static listener_t * | |
255 | find_listener(struct irc_sockaddr_storage *addr) | |
256 | { | |
257 | listener_t *listener = NULL; | |
258 | listener_t *last_closed = NULL; | |
259 | ||
260 | for (listener = ListenerPollList; listener; listener = listener->next) | |
261 | { | |
262 | if(addr->ss_family != listener->addr.ss_family) | |
263 | continue; | |
264 | ||
265 | switch(addr->ss_family) | |
266 | { | |
267 | case AF_INET: | |
268 | { | |
269 | struct sockaddr_in *in4 = (struct sockaddr_in *)addr; | |
270 | struct sockaddr_in *lin4 = (struct sockaddr_in *)&listener->addr; | |
271 | if(in4->sin_addr.s_addr == lin4->sin_addr.s_addr && | |
272 | in4->sin_port == lin4->sin_port ) | |
273 | { | |
274 | if(listener->fd == -1) | |
275 | last_closed = listener; | |
276 | else | |
277 | return(listener); | |
278 | } | |
279 | break; | |
280 | } | |
281 | #ifdef IPV6 | |
282 | case AF_INET6: | |
283 | { | |
284 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)addr; | |
285 | struct sockaddr_in6 *lin6 =(struct sockaddr_in6 *)&listener->addr; | |
286 | if(IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &lin6->sin6_addr) && | |
287 | in6->sin6_port == lin6->sin6_port) | |
288 | { | |
289 | if(listener->fd == -1) | |
290 | last_closed = listener; | |
291 | else | |
292 | return(listener); | |
293 | } | |
294 | break; | |
295 | ||
296 | } | |
297 | #endif | |
298 | ||
299 | default: | |
300 | break; | |
301 | } | |
302 | } | |
303 | return last_closed; | |
304 | } | |
305 | ||
306 | ||
307 | /* | |
308 | * add_listener- create a new listener | |
309 | * port - the port number to listen on | |
310 | * vhost_ip - if non-null must contain a valid IP address string in | |
311 | * the format "255.255.255.255" | |
312 | */ | |
313 | void | |
314 | add_listener(int port, const char *vhost_ip, int family) | |
315 | { | |
316 | listener_t *listener; | |
317 | struct irc_sockaddr_storage vaddr; | |
318 | ||
319 | /* | |
320 | * if no port in conf line, don't bother | |
321 | */ | |
322 | if(port == 0) | |
323 | return; | |
324 | memset(&vaddr, 0, sizeof(vaddr)); | |
325 | vaddr.ss_family = family; | |
326 | ||
327 | if(vhost_ip != NULL) | |
328 | { | |
329 | if(family == AF_INET) | |
330 | { | |
331 | if(inetpton(family, vhost_ip, &((struct sockaddr_in *)&vaddr)->sin_addr) <= 0) | |
332 | return; | |
333 | } | |
334 | #ifdef IPV6 | |
335 | else | |
336 | { | |
337 | if(inetpton(family, vhost_ip, &((struct sockaddr_in6 *)&vaddr)->sin6_addr) <= 0) | |
338 | return; | |
339 | ||
340 | } | |
341 | #endif | |
342 | } else | |
343 | { | |
344 | switch(family) | |
345 | { | |
346 | case AF_INET: | |
347 | ((struct sockaddr_in *)&vaddr)->sin_addr.s_addr = INADDR_ANY; | |
348 | break; | |
349 | #ifdef IPV6 | |
350 | case AF_INET6: | |
351 | memcpy(&((struct sockaddr_in6 *)&vaddr)->sin6_addr, &in6addr_any, sizeof(struct in6_addr)); | |
352 | break; | |
353 | default: | |
354 | return; | |
355 | #endif | |
356 | } | |
357 | } | |
358 | switch(family) | |
359 | { | |
360 | case AF_INET: | |
361 | SET_SS_LEN(vaddr, sizeof(struct sockaddr_in)); | |
362 | ((struct sockaddr_in *)&vaddr)->sin_port = htons(port); | |
363 | break; | |
364 | #ifdef IPV6 | |
365 | case AF_INET6: | |
366 | SET_SS_LEN(vaddr, sizeof(struct sockaddr_in6)); | |
367 | ((struct sockaddr_in6 *)&vaddr)->sin6_port = htons(port); | |
368 | break; | |
369 | #endif | |
370 | default: | |
371 | break; | |
372 | } | |
373 | if((listener = find_listener(&vaddr))) | |
374 | { | |
375 | if(listener->fd > -1) | |
376 | return; | |
377 | } | |
378 | else | |
379 | { | |
380 | listener = make_listener(&vaddr); | |
381 | listener->next = ListenerPollList; | |
382 | ListenerPollList = listener; | |
383 | } | |
384 | ||
385 | listener->fd = -1; | |
386 | ||
387 | if(inetport(listener)) | |
388 | listener->active = 1; | |
389 | else | |
390 | close_listener(listener); | |
391 | } | |
392 | ||
393 | /* | |
394 | * close_listener - close a single listener | |
395 | */ | |
396 | void | |
397 | close_listener(listener_t *listener) | |
398 | { | |
399 | s_assert(listener != NULL); | |
400 | if(listener == NULL) | |
401 | return; | |
402 | if(listener->fd >= 0) | |
403 | { | |
404 | rb_close(listener->fd); | |
405 | listener->fd = -1; | |
406 | } | |
407 | ||
408 | listener->active = 0; | |
409 | ||
410 | if(listener->ref_count) | |
411 | return; | |
412 | ||
413 | free_listener(listener); | |
414 | } | |
415 | ||
416 | /* | |
417 | * close_listeners - close and free all listeners that are not being used | |
418 | */ | |
419 | void | |
420 | close_listeners() | |
421 | { | |
422 | listener_t *listener; | |
423 | listener_t *listener_next = 0; | |
424 | /* | |
425 | * close all 'extra' listening ports we have | |
426 | */ | |
427 | for (listener = ListenerPollList; listener; listener = listener_next) | |
428 | { | |
429 | listener_next = listener->next; | |
430 | close_listener(listener); | |
431 | } | |
432 | } | |
433 | ||
434 | #define DLINE_WARNING "ERROR :You have been D-lined.\r\n" | |
435 | ||
436 | /* | |
437 | * add_connection - creates a client which has just connected to us on | |
438 | * the given fd. The sockhost field is initialized with the ip# of the host. | |
439 | * The client is sent to the auth module for verification, and not put in | |
440 | * any client list yet. | |
441 | */ | |
442 | static void | |
443 | add_connection(listener_t *listener, int fd, struct sockaddr *sai, int exempt) | |
444 | { | |
445 | struct Client *new_client; | |
446 | s_assert(NULL != listener); | |
447 | ||
448 | /* | |
449 | * get the client socket name from the socket | |
450 | * the client has already been checked out in accept_connection | |
451 | */ | |
452 | new_client = make_client(NULL); | |
453 | ||
454 | memcpy(&new_client->localClient->ip, sai, sizeof(struct irc_sockaddr_storage)); | |
455 | ||
456 | /* | |
457 | * copy address to 'sockhost' as a string, copy it to host too | |
458 | * so we have something valid to put into error messages... | |
459 | */ | |
460 | inetntop_sock((struct sockaddr *)&new_client->localClient->ip, new_client->sockhost, | |
461 | sizeof(new_client->sockhost)); | |
462 | ||
463 | ||
464 | strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host)); | |
465 | ||
466 | new_client->localClient->F = rb_add_fd(fd); | |
467 | ||
468 | new_client->localClient->listener = listener; | |
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 | ||
483 | static void | |
484 | accept_connection(int pfd, void *data) | |
485 | { | |
486 | static time_t last_oper_notice = 0; | |
487 | struct irc_sockaddr_storage sai; | |
488 | socklen_t addrlen = sizeof(sai); | |
489 | int fd; | |
490 | listener_t *listener = data; | |
491 | struct ConfItem *aconf; | |
492 | char buf[BUFSIZE]; | |
493 | ||
494 | s_assert(listener != NULL); | |
495 | if(listener == NULL) | |
496 | return; | |
497 | /* | |
498 | * There may be many reasons for error return, but | |
499 | * in otherwise correctly working environment the | |
500 | * probable cause is running out of file descriptors | |
501 | * (EMFILE, ENFILE or others?). The man pages for | |
502 | * accept don't seem to list these as possible, | |
503 | * although it's obvious that it may happen here. | |
504 | * Thus no specific errors are tested at this | |
505 | * point, just assume that connections cannot | |
506 | * be accepted until some old is closed first. | |
507 | */ | |
508 | ||
509 | fd = rb_accept(listener->fd, (struct sockaddr *)&sai, &addrlen); | |
510 | if(fd < 0) | |
511 | { | |
512 | /* Re-register a new IO request for the next accept .. */ | |
513 | rb_setselect(listener->fd, FDLIST_SERVICE, | |
514 | COMM_SELECT_READ, accept_connection, listener, 0); | |
515 | return; | |
516 | } | |
517 | ||
518 | /* This needs to be done here, otherwise we break dlines */ | |
519 | mangle_mapped_sockaddr((struct sockaddr *)&sai); | |
520 | ||
521 | /* | |
522 | * check for connection limit | |
523 | * TBD: this is stupid... either we have a socket or we don't. -nenolod | |
524 | */ | |
525 | if((rb_get_maxconnections() - 10) < fd) | |
526 | { | |
527 | ++ServerStats->is_ref; | |
528 | /* | |
529 | * slow down the whining to opers bit | |
530 | */ | |
531 | if((last_oper_notice + 20) <= CurrentTime) | |
532 | { | |
533 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
534 | "All connections in use. (%s)", | |
535 | get_listener_name(listener)); | |
536 | last_oper_notice = CurrentTime; | |
537 | } | |
538 | ||
539 | write(fd, "ERROR :All connections in use\r\n", 32); | |
540 | rb_close(fd); | |
541 | /* Re-register a new IO request for the next accept .. */ | |
542 | rb_setselect(listener->fd, FDLIST_SERVICE, | |
543 | COMM_SELECT_READ, accept_connection, listener, 0); | |
544 | return; | |
545 | } | |
546 | ||
547 | /* Do an initial check we aren't connecting too fast or with too many | |
548 | * from this IP... */ | |
549 | aconf = find_dline((struct sockaddr *) &sai, sai.ss_family); | |
550 | /* check it wasn't an exempt */ | |
551 | if (aconf != NULL && (aconf->status & CONF_EXEMPTDLINE) == 0) | |
552 | { | |
553 | ServerStats->is_ref++; | |
554 | ||
555 | if(ConfigFileEntry.dline_with_reason) | |
556 | { | |
557 | if (rb_snprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", aconf->passwd) >= (sizeof(buf)-1)) | |
558 | { | |
559 | buf[sizeof(buf) - 3] = '\r'; | |
560 | buf[sizeof(buf) - 2] = '\n'; | |
561 | buf[sizeof(buf) - 1] = '\0'; | |
562 | } | |
563 | } | |
564 | else | |
565 | rb_sprintf(buf, "ERROR :You have been D-lined.\r\n"); | |
566 | ||
567 | write(fd, buf, strlen(buf)); | |
568 | rb_close(fd); | |
569 | ||
570 | /* Re-register a new IO request for the next accept .. */ | |
571 | rb_setselect(listener->fd, FDLIST_SERVICE, | |
572 | COMM_SELECT_READ, accept_connection, listener, 0); | |
573 | return; | |
574 | } | |
575 | ||
576 | ServerStats->is_ac++; | |
577 | add_connection(listener, fd, (struct sockaddr *)&sai, aconf ? 1 : 0); | |
578 | ||
579 | /* Re-register a new IO request for the next accept .. */ | |
580 | rb_setselect(listener->fd, FDLIST_SERVICE, COMM_SELECT_READ, | |
581 | accept_connection, listener, 0); | |
582 | } |