]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
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 1675 2006-06-15 22:32:23Z 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 "commio.h" | |
37 | #include "s_conf.h" | |
38 | #include "s_newconf.h" | |
39 | #include "s_stats.h" | |
40 | #include "send.h" | |
41 | #include "memory.h" | |
42 | #include "s_auth.h" | |
43 | #include "reject.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 *) MyMalloc(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 | MyFree(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 | ircsnprintf(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 = comm_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 | if(fd == -1) | |
199 | { | |
200 | report_error("opening listener socket %s:%s", | |
201 | get_listener_name(listener), | |
202 | get_listener_name(listener), errno); | |
203 | return 0; | |
204 | } | |
205 | else if((HARD_FDLIMIT - 10) < fd) | |
206 | { | |
207 | report_error("no more connections left for listener %s:%s", | |
208 | get_listener_name(listener), | |
209 | get_listener_name(listener), errno); | |
210 | comm_close(fd); | |
211 | return 0; | |
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(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt))) | |
218 | { | |
219 | report_error("setting SO_REUSEADDR for listener %s:%s", | |
220 | get_listener_name(listener), | |
221 | get_listener_name(listener), errno); | |
222 | comm_close(fd); | |
223 | return 0; | |
224 | } | |
225 | ||
226 | /* | |
227 | * Bind a port to listen for new connections if port is non-null, | |
228 | * else assume it is already open and try get something from it. | |
229 | */ | |
230 | ||
231 | if(bind(fd, (struct sockaddr *) &listener->addr, GET_SS_LEN(listener->addr))) | |
232 | { | |
233 | report_error("binding listener socket %s:%s", | |
234 | get_listener_name(listener), | |
235 | get_listener_name(listener), errno); | |
236 | comm_close(fd); | |
237 | return 0; | |
238 | } | |
239 | ||
240 | if(listen(fd, RATBOX_SOMAXCONN)) | |
241 | { | |
242 | report_error("listen failed for %s:%s", | |
243 | get_listener_name(listener), | |
244 | get_listener_name(listener), errno); | |
245 | comm_close(fd); | |
246 | return 0; | |
247 | } | |
248 | ||
249 | listener->fd = fd; | |
250 | ||
251 | /* Listen completion events are READ events .. */ | |
252 | ||
253 | accept_connection(fd, listener); | |
254 | return 1; | |
255 | } | |
256 | ||
257 | static listener_t * | |
258 | find_listener(struct irc_sockaddr_storage *addr) | |
259 | { | |
260 | listener_t *listener = NULL; | |
261 | listener_t *last_closed = NULL; | |
262 | ||
263 | for (listener = ListenerPollList; listener; listener = listener->next) | |
264 | { | |
265 | if(addr->ss_family != listener->addr.ss_family) | |
266 | continue; | |
267 | ||
268 | switch(addr->ss_family) | |
269 | { | |
270 | case AF_INET: | |
271 | { | |
272 | struct sockaddr_in *in4 = (struct sockaddr_in *)addr; | |
273 | struct sockaddr_in *lin4 = (struct sockaddr_in *)&listener->addr; | |
274 | if(in4->sin_addr.s_addr == lin4->sin_addr.s_addr && | |
275 | in4->sin_port == lin4->sin_port ) | |
276 | { | |
277 | if(listener->fd == -1) | |
278 | last_closed = listener; | |
279 | else | |
280 | return(listener); | |
281 | } | |
282 | break; | |
283 | } | |
284 | #ifdef IPV6 | |
285 | case AF_INET6: | |
286 | { | |
287 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)addr; | |
288 | struct sockaddr_in6 *lin6 =(struct sockaddr_in6 *)&listener->addr; | |
289 | if(IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &lin6->sin6_addr) && | |
290 | in6->sin6_port == lin6->sin6_port) | |
291 | { | |
292 | if(listener->fd == -1) | |
293 | last_closed = listener; | |
294 | else | |
295 | return(listener); | |
296 | } | |
297 | break; | |
298 | ||
299 | } | |
300 | #endif | |
301 | ||
302 | default: | |
303 | break; | |
304 | } | |
305 | } | |
306 | return last_closed; | |
307 | } | |
308 | ||
309 | ||
310 | /* | |
311 | * add_listener- create a new listener | |
312 | * port - the port number to listen on | |
313 | * vhost_ip - if non-null must contain a valid IP address string in | |
314 | * the format "255.255.255.255" | |
315 | */ | |
316 | void | |
317 | add_listener(int port, const char *vhost_ip, int family) | |
318 | { | |
319 | listener_t *listener; | |
320 | struct irc_sockaddr_storage vaddr; | |
321 | ||
322 | /* | |
323 | * if no port in conf line, don't bother | |
324 | */ | |
325 | if(port == 0) | |
326 | return; | |
327 | memset(&vaddr, 0, sizeof(vaddr)); | |
328 | vaddr.ss_family = family; | |
329 | ||
330 | if(vhost_ip != NULL) | |
331 | { | |
332 | if(family == AF_INET) | |
333 | { | |
334 | if(inetpton(family, vhost_ip, &((struct sockaddr_in *)&vaddr)->sin_addr) <= 0) | |
335 | return; | |
336 | } | |
337 | #ifdef IPV6 | |
338 | else | |
339 | { | |
340 | if(inetpton(family, vhost_ip, &((struct sockaddr_in6 *)&vaddr)->sin6_addr) <= 0) | |
341 | return; | |
342 | ||
343 | } | |
344 | #endif | |
345 | } else | |
346 | { | |
347 | switch(family) | |
348 | { | |
349 | case AF_INET: | |
350 | ((struct sockaddr_in *)&vaddr)->sin_addr.s_addr = INADDR_ANY; | |
351 | break; | |
352 | #ifdef IPV6 | |
353 | case AF_INET6: | |
354 | memcpy(&((struct sockaddr_in6 *)&vaddr)->sin6_addr, &in6addr_any, sizeof(struct in6_addr)); | |
355 | break; | |
356 | default: | |
357 | return; | |
358 | #endif | |
359 | } | |
360 | } | |
361 | switch(family) | |
362 | { | |
363 | case AF_INET: | |
364 | SET_SS_LEN(vaddr, sizeof(struct sockaddr_in)); | |
365 | ((struct sockaddr_in *)&vaddr)->sin_port = htons(port); | |
366 | break; | |
367 | #ifdef IPV6 | |
368 | case AF_INET6: | |
369 | SET_SS_LEN(vaddr, sizeof(struct sockaddr_in6)); | |
370 | ((struct sockaddr_in6 *)&vaddr)->sin6_port = htons(port); | |
371 | break; | |
372 | #endif | |
373 | default: | |
374 | break; | |
375 | } | |
376 | if((listener = find_listener(&vaddr))) | |
377 | { | |
378 | if(listener->fd > -1) | |
379 | return; | |
380 | } | |
381 | else | |
382 | { | |
383 | listener = make_listener(&vaddr); | |
384 | listener->next = ListenerPollList; | |
385 | ListenerPollList = listener; | |
386 | } | |
387 | ||
388 | listener->fd = -1; | |
389 | ||
390 | if(inetport(listener)) | |
391 | listener->active = 1; | |
392 | else | |
393 | close_listener(listener); | |
394 | } | |
395 | ||
396 | /* | |
397 | * close_listener - close a single listener | |
398 | */ | |
399 | void | |
400 | close_listener(listener_t *listener) | |
401 | { | |
402 | s_assert(listener != NULL); | |
403 | if(listener == NULL) | |
404 | return; | |
405 | if(listener->fd >= 0) | |
406 | { | |
407 | comm_close(listener->fd); | |
408 | listener->fd = -1; | |
409 | } | |
410 | ||
411 | listener->active = 0; | |
412 | ||
413 | if(listener->ref_count) | |
414 | return; | |
415 | ||
416 | free_listener(listener); | |
417 | } | |
418 | ||
419 | /* | |
420 | * close_listeners - close and free all listeners that are not being used | |
421 | */ | |
422 | void | |
423 | close_listeners() | |
424 | { | |
425 | listener_t *listener; | |
426 | listener_t *listener_next = 0; | |
427 | /* | |
428 | * close all 'extra' listening ports we have | |
429 | */ | |
430 | for (listener = ListenerPollList; listener; listener = listener_next) | |
431 | { | |
432 | listener_next = listener->next; | |
433 | close_listener(listener); | |
434 | } | |
435 | } | |
436 | ||
437 | #define DLINE_WARNING "ERROR :You have been D-lined.\r\n" | |
438 | ||
439 | /* | |
440 | * add_connection - creates a client which has just connected to us on | |
441 | * the given fd. The sockhost field is initialized with the ip# of the host. | |
442 | * The client is sent to the auth module for verification, and not put in | |
443 | * any client list yet. | |
444 | */ | |
445 | static void | |
446 | add_connection(listener_t *listener, int fd, struct sockaddr *sai) | |
447 | { | |
448 | struct Client *new_client; | |
449 | s_assert(NULL != listener); | |
450 | ||
451 | /* | |
452 | * get the client socket name from the socket | |
453 | * the client has already been checked out in accept_connection | |
454 | */ | |
455 | new_client = make_client(NULL); | |
456 | ||
457 | memcpy(&new_client->localClient->ip, sai, sizeof(struct irc_sockaddr_storage)); | |
458 | ||
459 | /* | |
460 | * copy address to 'sockhost' as a string, copy it to host too | |
461 | * so we have something valid to put into error messages... | |
462 | */ | |
463 | inetntop_sock((struct sockaddr *)&new_client->localClient->ip, new_client->sockhost, | |
464 | sizeof(new_client->sockhost)); | |
465 | ||
466 | ||
467 | strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host)); | |
468 | ||
469 | #ifdef IPV6 | |
470 | if(new_client->localClient->ip.ss_family == AF_INET6 && ConfigFileEntry.dot_in_ip6_addr == 1) | |
471 | { | |
472 | strlcat(new_client->host, ".", sizeof(new_client->host)); | |
473 | } | |
474 | #endif | |
475 | ||
476 | new_client->localClient->fd = fd; | |
477 | ||
478 | new_client->localClient->listener = listener; | |
479 | ++listener->ref_count; | |
480 | ||
481 | if(check_reject(new_client)) | |
482 | return; | |
483 | start_auth(new_client); | |
484 | } | |
485 | ||
486 | ||
487 | static void | |
488 | accept_connection(int pfd, void *data) | |
489 | { | |
490 | static time_t last_oper_notice = 0; | |
491 | ||
492 | struct irc_sockaddr_storage sai; | |
493 | socklen_t addrlen = sizeof(sai); | |
494 | int fd; | |
495 | listener_t *listener = data; | |
496 | struct ConfItem *aconf; | |
497 | char buf[BUFSIZE]; | |
498 | ||
499 | s_assert(listener != NULL); | |
500 | if(listener == NULL) | |
501 | return; | |
502 | /* | |
503 | * There may be many reasons for error return, but | |
504 | * in otherwise correctly working environment the | |
505 | * probable cause is running out of file descriptors | |
506 | * (EMFILE, ENFILE or others?). The man pages for | |
507 | * accept don't seem to list these as possible, | |
508 | * although it's obvious that it may happen here. | |
509 | * Thus no specific errors are tested at this | |
510 | * point, just assume that connections cannot | |
511 | * be accepted until some old is closed first. | |
512 | */ | |
513 | ||
514 | fd = comm_accept(listener->fd, (struct sockaddr *)&sai, &addrlen); | |
515 | if(fd < 0) | |
516 | { | |
517 | /* Re-register a new IO request for the next accept .. */ | |
518 | comm_setselect(listener->fd, FDLIST_SERVICE, | |
519 | COMM_SELECT_READ, accept_connection, listener, 0); | |
520 | return; | |
521 | } | |
522 | ||
523 | /* This needs to be done here, otherwise we break dlines */ | |
524 | mangle_mapped_sockaddr((struct sockaddr *)&sai); | |
525 | ||
526 | /* | |
527 | * check for connection limit | |
528 | */ | |
529 | if((MAXCONNECTIONS - 10) < fd) | |
530 | { | |
531 | ++ServerStats->is_ref; | |
532 | /* | |
533 | * slow down the whining to opers bit | |
534 | */ | |
535 | if((last_oper_notice + 20) <= CurrentTime) | |
536 | { | |
537 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
538 | "All connections in use. (%s)", | |
539 | get_listener_name(listener)); | |
540 | last_oper_notice = CurrentTime; | |
541 | } | |
542 | ||
543 | write(fd, "ERROR :All connections in use\r\n", 32); | |
544 | comm_close(fd); | |
545 | /* Re-register a new IO request for the next accept .. */ | |
546 | comm_setselect(listener->fd, FDLIST_SERVICE, | |
547 | COMM_SELECT_READ, accept_connection, listener, 0); | |
548 | return; | |
549 | } | |
550 | ||
551 | /* Do an initial check we aren't connecting too fast or with too many | |
552 | * from this IP... */ | |
553 | if((aconf = conf_connect_allowed((struct sockaddr *)&sai, sai.ss_family)) != NULL) | |
554 | { | |
555 | ServerStats->is_ref++; | |
556 | ||
557 | if(ConfigFileEntry.dline_with_reason) | |
558 | { | |
559 | if (ircsnprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", aconf->passwd) >= (sizeof(buf)-1)) | |
560 | { | |
561 | buf[sizeof(buf) - 3] = '\r'; | |
562 | buf[sizeof(buf) - 2] = '\n'; | |
563 | buf[sizeof(buf) - 1] = '\0'; | |
564 | } | |
565 | } | |
566 | else | |
567 | ircsprintf(buf, "ERROR :You have been D-lined.\r\n"); | |
568 | ||
569 | write(fd, buf, strlen(buf)); | |
570 | comm_close(fd); | |
571 | ||
572 | /* Re-register a new IO request for the next accept .. */ | |
573 | comm_setselect(listener->fd, FDLIST_SERVICE, | |
574 | COMM_SELECT_READ, accept_connection, listener, 0); | |
575 | return; | |
576 | } | |
577 | ||
578 | ServerStats->is_ac++; | |
579 | add_connection(listener, fd, (struct sockaddr *)&sai); | |
580 | ||
581 | /* Re-register a new IO request for the next accept .. */ | |
582 | comm_setselect(listener->fd, FDLIST_SERVICE, COMM_SELECT_READ, | |
583 | accept_connection, listener, 0); | |
584 | } |