]>
Commit | Line | Data |
---|---|---|
54ac8b60 VY |
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 | ||
54ac8b60 | 54 | static listener_t *ListenerPollList = NULL; |
8e09c4a2 VY |
55 | static int accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data);\r |
56 | static void accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data); | |
54ac8b60 VY |
57 | |
58 | static listener_t * | |
e7046ee5 | 59 | make_listener(struct rb_sockaddr_storage *addr) |
54ac8b60 VY |
60 | { |
61 | listener_t *listener = (listener_t *) rb_malloc(sizeof(listener_t)); | |
62 | s_assert(0 != listener); | |
54ac8b60 | 63 | listener->name = me.name; |
f691939a VY |
64 | listener->F = NULL; |
65 | ||
e7046ee5 | 66 | memcpy(&listener->addr, addr, sizeof(struct rb_sockaddr_storage)); |
54ac8b60 VY |
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 | { | |
f691939a | 168 | rb_fde_t *F; |
0d89d5cd | 169 | int ret; |
54ac8b60 VY |
170 | int opt = 1; |
171 | ||
172 | /* | |
173 | * At first, open a new socket | |
174 | */ | |
175 | ||
f691939a | 176 | F = rb_socket(GET_SS_FAMILY(&listener->addr), SOCK_STREAM, 0, "Listener socket"); |
54ac8b60 VY |
177 | |
178 | #ifdef IPV6 | |
179 | if(listener->addr.ss_family == AF_INET6) | |
180 | { | |
181 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&listener->addr; | |
182 | if(!IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &in6addr_any)) | |
183 | { | |
184 | inetntop(AF_INET6, &in6->sin6_addr, listener->vhost, sizeof(listener->vhost)); | |
185 | listener->name = listener->vhost; | |
186 | } | |
187 | } else | |
188 | #endif | |
189 | { | |
190 | struct sockaddr_in *in = (struct sockaddr_in *)&listener->addr; | |
191 | if(in->sin_addr.s_addr != INADDR_ANY) | |
192 | { | |
193 | inetntop(AF_INET, &in->sin_addr, listener->vhost, sizeof(listener->vhost)); | |
194 | listener->name = listener->vhost; | |
195 | } | |
196 | } | |
197 | ||
f691939a VY |
198 | if(F == NULL)\r |
199 | {\r | |
200 | report_error("opening listener socket %s:%s",\r | |
201 | get_listener_name(listener), \r | |
202 | get_listener_name(listener), errno);\r | |
203 | return 0;\r | |
204 | }\r | |
205 | else if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus*/\r | |
206 | {\r | |
207 | report_error("no more connections left for listener %s:%s",\r | |
208 | get_listener_name(listener), \r | |
209 | get_listener_name(listener), errno);\r | |
210 | rb_close(F);\r | |
211 | return 0;\r | |
54ac8b60 VY |
212 | } |
213 | ||
f691939a VY |
214 | /*\r |
215 | * XXX - we don't want to do all this crap for a listener\r | |
216 | * set_sock_opts(listener);\r | |
217 | */\r | |
218 | if(setsockopt(rb_get_fd(F), SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt)))\r | |
219 | {\r | |
220 | report_error("setting SO_REUSEADDR for listener %s:%s",\r | |
221 | get_listener_name(listener), \r | |
222 | get_listener_name(listener), errno);\r | |
223 | rb_close(F);\r | |
224 | return 0;\r | |
54ac8b60 VY |
225 | } |
226 | ||
f691939a VY |
227 | /*\r |
228 | * Bind a port to listen for new connections if port is non-null,\r | |
229 | * else assume it is already open and try get something from it.\r | |
230 | */\r | |
231 | \r | |
232 | if(bind(rb_get_fd(F), (struct sockaddr *) &listener->addr, GET_SS_LEN(&listener->addr)))\r | |
233 | {\r | |
234 | report_error("binding listener socket %s:%s",\r | |
235 | get_listener_name(listener), \r | |
236 | get_listener_name(listener), errno);\r | |
237 | rb_close(F);\r | |
238 | return 0;\r | |
54ac8b60 VY |
239 | } |
240 | ||
f691939a VY |
241 | if((ret = rb_listen(F, RATBOX_SOMAXCONN)))\r |
242 | {\r | |
243 | report_error("listen failed for %s:%s", \r | |
244 | get_listener_name(listener), \r | |
245 | get_listener_name(listener), errno);\r | |
246 | rb_close(F);\r | |
247 | return 0;\r | |
54ac8b60 VY |
248 | } |
249 | ||
f691939a VY |
250 | listener->F = F;\r |
251 | \r | |
252 | rb_accept_tcp(listener->F, accept_precallback, accept_callback, listener); | |
54ac8b60 VY |
253 | return 1; |
254 | } | |
255 | ||
256 | static listener_t * | |
e7046ee5 | 257 | find_listener(struct rb_sockaddr_storage *addr) |
54ac8b60 VY |
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 | { | |
f691939a VY |
276 | if(listener->F == NULL)\r |
277 | last_closed = listener;\r | |
278 | else\r | |
54ac8b60 VY |
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 | { | |
f691939a VY |
291 | if(listener->F == NULL)\r |
292 | last_closed = listener;\r | |
293 | else\r | |
54ac8b60 VY |
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; | |
e7046ee5 | 319 | struct rb_sockaddr_storage vaddr; |
54ac8b60 VY |
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: | |
99c4835f | 363 | SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in)); |
54ac8b60 VY |
364 | ((struct sockaddr_in *)&vaddr)->sin_port = htons(port); |
365 | break; | |
366 | #ifdef IPV6 | |
367 | case AF_INET6: | |
99c4835f | 368 | SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in6)); |
54ac8b60 VY |
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 | { | |
f691939a | 377 | if(listener->F != NULL)\r |
54ac8b60 VY |
378 | return; |
379 | } | |
380 | else | |
381 | { | |
382 | listener = make_listener(&vaddr); | |
383 | listener->next = ListenerPollList; | |
384 | ListenerPollList = listener; | |
385 | } | |
386 | ||
f691939a | 387 | listener->F = NULL; |
54ac8b60 VY |
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; | |
f691939a VY |
404 | if(listener->F != NULL)\r |
405 | {\r | |
406 | rb_close(listener->F);\r | |
407 | listener->F = NULL;\r | |
54ac8b60 VY |
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 | */ | |
99c4835f VY |
444 | static void\r |
445 | add_connection(struct Listener *listener, rb_fde_t *F, struct sockaddr *sai, int exempt) | |
54ac8b60 VY |
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 | ||
e7046ee5 | 456 | memcpy(&new_client->localClient->ip, sai, sizeof(struct rb_sockaddr_storage)); |
54ac8b60 VY |
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 | ||
99c4835f | 468 | new_client->localClient->F = F; |
54ac8b60 VY |
469 | |
470 | new_client->localClient->listener = listener; | |
471 | ++listener->ref_count; | |
472 | ||
473 | if(!exempt) | |
474 | { | |
475 | if(check_reject(new_client)) | |
476 | return; | |
477 | if(add_unknown_ip(new_client)) | |
478 | return; | |
479 | } | |
480 | ||
481 | start_auth(new_client); | |
482 | } | |
0d89d5cd VY |
483 | |
484 | static int\r | |
485 | accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data)\r | |
486 | {\r | |
487 | struct Listener *listener = (struct Listener *)data;\r | |
488 | char buf[BUFSIZE];\r | |
489 | struct ConfItem *aconf;\r | |
490 | \r | |
491 | if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus */\r | |
492 | {\r | |
493 | ++ServerStats->is_ref;\r | |
494 | /*\r | |
495 | * slow down the whining to opers bit\r | |
496 | */\r | |
497 | if((last_oper_notice + 20) <= rb_current_time())\r | |
498 | {\r | |
499 | sendto_realops_flags(SNO_GENERAL, L_ALL,\r | |
500 | "All connections in use. (%s)",\r | |
501 | get_listener_name(listener));\r | |
502 | last_oper_notice = rb_current_time();\r | |
503 | }\r | |
504 | \r | |
505 | rb_write(F, "ERROR :All connections in use\r\n", 32);\r | |
506 | rb_close(F);\r | |
507 | /* Re-register a new IO request for the next accept .. */\r | |
508 | return 0;\r | |
509 | }\r | |
510 | \r | |
511 | aconf = find_dline(addr);\r | |
512 | if(aconf != NULL && (aconf->status & CONF_EXEMPTDLINE))\r | |
513 | return 1;\r | |
514 | \r | |
515 | /* Do an initial check we aren't connecting too fast or with too many\r | |
516 | * from this IP... */\r | |
517 | if(aconf != NULL)\r | |
518 | {\r | |
519 | ServerStats->is_ref++;\r | |
520 | \r | |
521 | if(ConfigFileEntry.dline_with_reason)\r | |
522 | {\r | |
523 | if (rb_snprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", aconf->passwd) >= (int)(sizeof(buf)-1))\r | |
524 | {\r | |
525 | buf[sizeof(buf) - 3] = '\r';\r | |
526 | buf[sizeof(buf) - 2] = '\n';\r | |
527 | buf[sizeof(buf) - 1] = '\0';\r | |
528 | }\r | |
529 | }\r | |
530 | else\r | |
531 | strcpy(buf, "ERROR :You have been D-lined.\r\n");\r | |
532 | \r | |
533 | rb_write(F, buf, strlen(buf));\r | |
534 | rb_close(F);\r | |
535 | return 0;\r | |
536 | }\r | |
537 | \r | |
538 | return 1;\r | |
539 | }\r | |
540 | \r | |
541 | static void\r | |
542 | accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data)\r | |
543 | {\r | |
544 | struct Listener *listener = data;\r | |
545 | struct rb_sockaddr_storage lip;\r | |
546 | unsigned int locallen = sizeof(struct rb_sockaddr_storage);\r | |
547 | \r | |
548 | ServerStats->is_ac++;\r | |
549 | \r | |
550 | if(getsockname(rb_get_fd(F), (struct sockaddr *) &lip, &locallen) < 0)\r | |
551 | {\r | |
552 | /* this shouldn't fail so... */\r | |
553 | /* XXX add logging of this */\r | |
554 | rb_close(F);\r | |
555 | }\r | |
556 | \r | |
557 | add_connection(listener, F, addr, (struct sockaddr *)&lip, NULL);\r | |
558 | } |