]>
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 | * | |
b808adf9 | 24 | * $Id: listener.c 3460 2007-05-18 20:31:33Z jilles $ |
212380e3 AC |
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" | |
b808adf9 JT |
44 | #include "s_conf.h" |
45 | #include "hostmask.h" | |
212380e3 AC |
46 | |
47 | #ifndef INADDR_NONE | |
48 | #define INADDR_NONE ((unsigned int) 0xffffffff) | |
49 | #endif | |
50 | ||
51 | #if defined(NO_IN6ADDR_ANY) && defined(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 PF accept_connection; | |
57 | ||
58 | static listener_t *ListenerPollList = NULL; | |
59 | ||
60 | static listener_t * | |
61 | make_listener(struct irc_sockaddr_storage *addr) | |
62 | { | |
63 | listener_t *listener = (listener_t *) MyMalloc(sizeof(listener_t)); | |
64 | s_assert(0 != listener); | |
65 | ||
66 | listener->name = me.name; | |
67 | listener->fd = -1; | |
68 | memcpy(&listener->addr, addr, sizeof(struct irc_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 | MyFree(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 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 | ircsnprintf(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 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"); | |
151 | } | |
152 | } | |
153 | ||
154 | /* | |
155 | * inetport - create a listener socket in the AF_INET or AF_INET6 domain, | |
156 | * bind it to the port given in 'port' and listen to it | |
157 | * returns true (1) if successful false (0) on error. | |
158 | * | |
159 | * If the operating system has a define for SOMAXCONN, use it, otherwise | |
160 | * use RATBOX_SOMAXCONN | |
161 | */ | |
162 | #ifdef SOMAXCONN | |
163 | #undef RATBOX_SOMAXCONN | |
164 | #define RATBOX_SOMAXCONN SOMAXCONN | |
165 | #endif | |
166 | ||
167 | static int | |
168 | inetport(listener_t *listener) | |
169 | { | |
170 | int fd; | |
171 | int opt = 1; | |
172 | ||
173 | /* | |
174 | * At first, open a new socket | |
175 | */ | |
176 | ||
177 | fd = comm_socket(listener->addr.ss_family, SOCK_STREAM, 0, "Listener socket"); | |
178 | ||
179 | #ifdef IPV6 | |
180 | if(listener->addr.ss_family == AF_INET6) | |
181 | { | |
182 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&listener->addr; | |
183 | if(!IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &in6addr_any)) | |
184 | { | |
185 | inetntop(AF_INET6, &in6->sin6_addr, listener->vhost, sizeof(listener->vhost)); | |
186 | listener->name = listener->vhost; | |
187 | } | |
188 | } else | |
189 | #endif | |
190 | { | |
191 | struct sockaddr_in *in = (struct sockaddr_in *)&listener->addr; | |
192 | if(in->sin_addr.s_addr != INADDR_ANY) | |
193 | { | |
194 | inetntop(AF_INET, &in->sin_addr, listener->vhost, sizeof(listener->vhost)); | |
195 | listener->name = listener->vhost; | |
196 | } | |
197 | } | |
198 | ||
6fcb8629 AC |
199 | /* |
200 | * At one point, we enforced a strange arbitrary limit here. | |
201 | * We no longer do this, and just check if the fd is valid or not. | |
202 | * -nenolod | |
203 | */ | |
212380e3 AC |
204 | if(fd == -1) |
205 | { | |
206 | report_error("opening listener socket %s:%s", | |
207 | get_listener_name(listener), | |
208 | get_listener_name(listener), errno); | |
209 | return 0; | |
210 | } | |
6fcb8629 | 211 | |
212380e3 AC |
212 | /* |
213 | * XXX - we don't want to do all this crap for a listener | |
214 | * set_sock_opts(listener); | |
215 | */ | |
216 | if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt))) | |
217 | { | |
218 | report_error("setting SO_REUSEADDR for listener %s:%s", | |
219 | get_listener_name(listener), | |
220 | get_listener_name(listener), errno); | |
221 | comm_close(fd); | |
222 | return 0; | |
223 | } | |
224 | ||
225 | /* | |
226 | * Bind a port to listen for new connections if port is non-null, | |
227 | * else assume it is already open and try get something from it. | |
228 | */ | |
229 | ||
230 | if(bind(fd, (struct sockaddr *) &listener->addr, GET_SS_LEN(listener->addr))) | |
231 | { | |
232 | report_error("binding listener socket %s:%s", | |
233 | get_listener_name(listener), | |
234 | get_listener_name(listener), errno); | |
235 | comm_close(fd); | |
236 | return 0; | |
237 | } | |
238 | ||
239 | if(listen(fd, RATBOX_SOMAXCONN)) | |
240 | { | |
241 | report_error("listen failed for %s:%s", | |
242 | get_listener_name(listener), | |
243 | get_listener_name(listener), errno); | |
244 | comm_close(fd); | |
245 | return 0; | |
246 | } | |
247 | ||
248 | listener->fd = fd; | |
249 | ||
250 | /* Listen completion events are READ events .. */ | |
251 | ||
252 | accept_connection(fd, listener); | |
253 | return 1; | |
254 | } | |
255 | ||
256 | static listener_t * | |
257 | find_listener(struct irc_sockaddr_storage *addr) | |
258 | { | |
259 | listener_t *listener = NULL; | |
260 | listener_t *last_closed = NULL; | |
261 | ||
262 | for (listener = ListenerPollList; listener; listener = listener->next) | |
263 | { | |
264 | if(addr->ss_family != listener->addr.ss_family) | |
265 | continue; | |
266 | ||
267 | switch(addr->ss_family) | |
268 | { | |
269 | case AF_INET: | |
270 | { | |
271 | struct sockaddr_in *in4 = (struct sockaddr_in *)addr; | |
272 | struct sockaddr_in *lin4 = (struct sockaddr_in *)&listener->addr; | |
273 | if(in4->sin_addr.s_addr == lin4->sin_addr.s_addr && | |
274 | in4->sin_port == lin4->sin_port ) | |
275 | { | |
276 | if(listener->fd == -1) | |
277 | last_closed = listener; | |
278 | else | |
279 | return(listener); | |
280 | } | |
281 | break; | |
282 | } | |
283 | #ifdef IPV6 | |
284 | case AF_INET6: | |
285 | { | |
286 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)addr; | |
287 | struct sockaddr_in6 *lin6 =(struct sockaddr_in6 *)&listener->addr; | |
288 | if(IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &lin6->sin6_addr) && | |
289 | in6->sin6_port == lin6->sin6_port) | |
290 | { | |
291 | if(listener->fd == -1) | |
292 | last_closed = listener; | |
293 | else | |
294 | return(listener); | |
295 | } | |
296 | break; | |
297 | ||
298 | } | |
299 | #endif | |
300 | ||
301 | default: | |
302 | break; | |
303 | } | |
304 | } | |
305 | return last_closed; | |
306 | } | |
307 | ||
308 | ||
309 | /* | |
310 | * add_listener- create a new listener | |
311 | * port - the port number to listen on | |
312 | * vhost_ip - if non-null must contain a valid IP address string in | |
313 | * the format "255.255.255.255" | |
314 | */ | |
315 | void | |
316 | add_listener(int port, const char *vhost_ip, int family) | |
317 | { | |
318 | listener_t *listener; | |
319 | struct irc_sockaddr_storage vaddr; | |
320 | ||
321 | /* | |
322 | * if no port in conf line, don't bother | |
323 | */ | |
324 | if(port == 0) | |
325 | return; | |
326 | memset(&vaddr, 0, sizeof(vaddr)); | |
327 | vaddr.ss_family = family; | |
328 | ||
329 | if(vhost_ip != NULL) | |
330 | { | |
331 | if(family == AF_INET) | |
332 | { | |
333 | if(inetpton(family, vhost_ip, &((struct sockaddr_in *)&vaddr)->sin_addr) <= 0) | |
334 | return; | |
335 | } | |
336 | #ifdef IPV6 | |
337 | else | |
338 | { | |
339 | if(inetpton(family, vhost_ip, &((struct sockaddr_in6 *)&vaddr)->sin6_addr) <= 0) | |
340 | return; | |
341 | ||
342 | } | |
343 | #endif | |
344 | } else | |
345 | { | |
346 | switch(family) | |
347 | { | |
348 | case AF_INET: | |
349 | ((struct sockaddr_in *)&vaddr)->sin_addr.s_addr = INADDR_ANY; | |
350 | break; | |
351 | #ifdef IPV6 | |
352 | case AF_INET6: | |
353 | memcpy(&((struct sockaddr_in6 *)&vaddr)->sin6_addr, &in6addr_any, sizeof(struct in6_addr)); | |
354 | break; | |
355 | default: | |
356 | return; | |
357 | #endif | |
358 | } | |
359 | } | |
360 | switch(family) | |
361 | { | |
362 | case AF_INET: | |
363 | SET_SS_LEN(vaddr, sizeof(struct sockaddr_in)); | |
364 | ((struct sockaddr_in *)&vaddr)->sin_port = htons(port); | |
365 | break; | |
366 | #ifdef IPV6 | |
367 | case AF_INET6: | |
368 | SET_SS_LEN(vaddr, sizeof(struct sockaddr_in6)); | |
369 | ((struct sockaddr_in6 *)&vaddr)->sin6_port = htons(port); | |
370 | break; | |
371 | #endif | |
372 | default: | |
373 | break; | |
374 | } | |
375 | if((listener = find_listener(&vaddr))) | |
376 | { | |
377 | if(listener->fd > -1) | |
378 | return; | |
379 | } | |
380 | else | |
381 | { | |
382 | listener = make_listener(&vaddr); | |
383 | listener->next = ListenerPollList; | |
384 | ListenerPollList = listener; | |
385 | } | |
386 | ||
387 | listener->fd = -1; | |
388 | ||
389 | if(inetport(listener)) | |
390 | listener->active = 1; | |
391 | else | |
392 | close_listener(listener); | |
393 | } | |
394 | ||
395 | /* | |
396 | * close_listener - close a single listener | |
397 | */ | |
398 | void | |
399 | close_listener(listener_t *listener) | |
400 | { | |
401 | s_assert(listener != NULL); | |
402 | if(listener == NULL) | |
403 | return; | |
404 | if(listener->fd >= 0) | |
405 | { | |
406 | comm_close(listener->fd); | |
407 | listener->fd = -1; | |
408 | } | |
409 | ||
410 | listener->active = 0; | |
411 | ||
412 | if(listener->ref_count) | |
413 | return; | |
414 | ||
415 | free_listener(listener); | |
416 | } | |
417 | ||
418 | /* | |
419 | * close_listeners - close and free all listeners that are not being used | |
420 | */ | |
421 | void | |
422 | close_listeners() | |
423 | { | |
424 | listener_t *listener; | |
425 | listener_t *listener_next = 0; | |
426 | /* | |
427 | * close all 'extra' listening ports we have | |
428 | */ | |
429 | for (listener = ListenerPollList; listener; listener = listener_next) | |
430 | { | |
431 | listener_next = listener->next; | |
432 | close_listener(listener); | |
433 | } | |
434 | } | |
435 | ||
436 | #define DLINE_WARNING "ERROR :You have been D-lined.\r\n" | |
437 | ||
438 | /* | |
439 | * add_connection - creates a client which has just connected to us on | |
440 | * the given fd. The sockhost field is initialized with the ip# of the host. | |
441 | * The client is sent to the auth module for verification, and not put in | |
442 | * any client list yet. | |
443 | */ | |
444 | static void | |
b808adf9 | 445 | add_connection(listener_t *listener, int fd, struct sockaddr *sai, int exempt) |
212380e3 AC |
446 | { |
447 | struct Client *new_client; | |
448 | s_assert(NULL != listener); | |
449 | ||
450 | /* | |
451 | * get the client socket name from the socket | |
452 | * the client has already been checked out in accept_connection | |
453 | */ | |
454 | new_client = make_client(NULL); | |
455 | ||
456 | memcpy(&new_client->localClient->ip, sai, sizeof(struct irc_sockaddr_storage)); | |
457 | ||
458 | /* | |
459 | * copy address to 'sockhost' as a string, copy it to host too | |
460 | * so we have something valid to put into error messages... | |
461 | */ | |
462 | inetntop_sock((struct sockaddr *)&new_client->localClient->ip, new_client->sockhost, | |
463 | sizeof(new_client->sockhost)); | |
464 | ||
465 | ||
466 | strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host)); | |
467 | ||
c5c2f506 | 468 | new_client->localClient->F = comm_add_fd(fd); |
212380e3 AC |
469 | |
470 | new_client->localClient->listener = listener; | |
471 | ++listener->ref_count; | |
472 | ||
b808adf9 JT |
473 | if(!exempt) |
474 | { | |
475 | if(check_reject(new_client)) | |
476 | return; | |
477 | if(add_unknown_ip(new_client)) | |
478 | return; | |
479 | } | |
54015b5f | 480 | |
212380e3 AC |
481 | start_auth(new_client); |
482 | } | |
483 | ||
484 | ||
485 | static void | |
486 | accept_connection(int pfd, void *data) | |
487 | { | |
488 | static time_t last_oper_notice = 0; | |
212380e3 AC |
489 | struct irc_sockaddr_storage sai; |
490 | socklen_t addrlen = sizeof(sai); | |
491 | int fd; | |
492 | listener_t *listener = data; | |
6fcb8629 AC |
493 | struct ConfItem *aconf; |
494 | char buf[BUFSIZE]; | |
495 | ||
212380e3 AC |
496 | s_assert(listener != NULL); |
497 | if(listener == NULL) | |
498 | return; | |
499 | /* | |
500 | * There may be many reasons for error return, but | |
501 | * in otherwise correctly working environment the | |
502 | * probable cause is running out of file descriptors | |
503 | * (EMFILE, ENFILE or others?). The man pages for | |
504 | * accept don't seem to list these as possible, | |
505 | * although it's obvious that it may happen here. | |
506 | * Thus no specific errors are tested at this | |
507 | * point, just assume that connections cannot | |
508 | * be accepted until some old is closed first. | |
509 | */ | |
510 | ||
511 | fd = comm_accept(listener->fd, (struct sockaddr *)&sai, &addrlen); | |
512 | if(fd < 0) | |
513 | { | |
514 | /* Re-register a new IO request for the next accept .. */ | |
515 | comm_setselect(listener->fd, FDLIST_SERVICE, | |
516 | COMM_SELECT_READ, accept_connection, listener, 0); | |
517 | return; | |
518 | } | |
519 | ||
520 | /* This needs to be done here, otherwise we break dlines */ | |
521 | mangle_mapped_sockaddr((struct sockaddr *)&sai); | |
522 | ||
523 | /* | |
524 | * check for connection limit | |
6fcb8629 | 525 | * TBD: this is stupid... either we have a socket or we don't. -nenolod |
212380e3 | 526 | */ |
6fcb8629 | 527 | if((comm_get_maxconnections() - 10) < fd) |
212380e3 AC |
528 | { |
529 | ++ServerStats->is_ref; | |
530 | /* | |
531 | * slow down the whining to opers bit | |
532 | */ | |
533 | if((last_oper_notice + 20) <= CurrentTime) | |
534 | { | |
535 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
536 | "All connections in use. (%s)", | |
537 | get_listener_name(listener)); | |
538 | last_oper_notice = CurrentTime; | |
539 | } | |
540 | ||
541 | write(fd, "ERROR :All connections in use\r\n", 32); | |
542 | comm_close(fd); | |
543 | /* Re-register a new IO request for the next accept .. */ | |
544 | comm_setselect(listener->fd, FDLIST_SERVICE, | |
545 | COMM_SELECT_READ, accept_connection, listener, 0); | |
546 | return; | |
547 | } | |
548 | ||
549 | /* Do an initial check we aren't connecting too fast or with too many | |
550 | * from this IP... */ | |
b808adf9 JT |
551 | aconf = find_dline((struct sockaddr *) &sai, sai.ss_family); |
552 | /* check it wasn't an exempt */ | |
553 | if (aconf != NULL && (aconf->status & CONF_EXEMPTDLINE) == 0) | |
212380e3 AC |
554 | { |
555 | ServerStats->is_ref++; | |
556 | ||
6fcb8629 AC |
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"); | |
212380e3 | 568 | |
6fcb8629 | 569 | write(fd, buf, strlen(buf)); |
212380e3 AC |
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++; | |
b808adf9 | 579 | add_connection(listener, fd, (struct sockaddr *)&sai, aconf ? 1 : 0); |
212380e3 AC |
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 | } |