]>
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 listener_t *ListenerPollList = NULL; | |
55 | static int accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data); | |
56 | static void accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data); | |
57 | ||
58 | static listener_t * | |
59 | make_listener(struct rb_sockaddr_storage *addr) | |
60 | { | |
61 | listener_t *listener = (listener_t *) rb_malloc(sizeof(listener_t)); | |
62 | s_assert(0 != listener); | |
63 | listener->name = me.name; | |
64 | listener->F = NULL; | |
65 | ||
66 | memcpy(&listener->addr, addr, sizeof(struct rb_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 | rb_fde_t *F; | |
169 | int ret; | |
170 | int opt = 1; | |
171 | ||
172 | /* | |
173 | * At first, open a new socket | |
174 | */ | |
175 | ||
176 | F = rb_socket(GET_SS_FAMILY(&listener->addr), SOCK_STREAM, 0, "Listener socket"); | |
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 | ||
198 | if(F == NULL) | |
199 | { | |
200 | ilog_error("opening listener socket"); | |
201 | return 0; | |
202 | } | |
203 | else if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus*/ | |
204 | { | |
205 | ilog_error("no more connections left for listener"); | |
206 | rb_close(F); | |
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(rb_get_fd(F), SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt))) | |
215 | { | |
216 | ilog_error("setting SO_REUSEADDR for listener"); | |
217 | rb_close(F); | |
218 | return 0; | |
219 | } | |
220 | ||
221 | /* | |
222 | * Bind a port to listen for new connections if port is non-null, | |
223 | * else assume it is already open and try get something from it. | |
224 | */ | |
225 | ||
226 | if(bind(rb_get_fd(F), (struct sockaddr *) &listener->addr, GET_SS_LEN(&listener->addr))) | |
227 | { | |
228 | ilog_error("binding listener socket"); | |
229 | rb_close(F); | |
230 | return 0; | |
231 | } | |
232 | ||
233 | if((ret = rb_listen(F, RATBOX_SOMAXCONN))) | |
234 | { | |
235 | ilog_error("listen()"); | |
236 | rb_close(F); | |
237 | return 0; | |
238 | } | |
239 | ||
240 | listener->F = F; | |
241 | ||
242 | rb_accept_tcp(listener->F, accept_precallback, accept_callback, listener); | |
243 | return 1; | |
244 | } | |
245 | ||
246 | static listener_t * | |
247 | find_listener(struct rb_sockaddr_storage *addr) | |
248 | { | |
249 | listener_t *listener = NULL; | |
250 | listener_t *last_closed = NULL; | |
251 | ||
252 | for (listener = ListenerPollList; listener; listener = listener->next) | |
253 | { | |
254 | if(addr->ss_family != listener->addr.ss_family) | |
255 | continue; | |
256 | ||
257 | switch(addr->ss_family) | |
258 | { | |
259 | case AF_INET: | |
260 | { | |
261 | struct sockaddr_in *in4 = (struct sockaddr_in *)addr; | |
262 | struct sockaddr_in *lin4 = (struct sockaddr_in *)&listener->addr; | |
263 | if(in4->sin_addr.s_addr == lin4->sin_addr.s_addr && | |
264 | in4->sin_port == lin4->sin_port ) | |
265 | { | |
266 | if(listener->F == NULL) | |
267 | last_closed = listener; | |
268 | else | |
269 | return(listener); | |
270 | } | |
271 | break; | |
272 | } | |
273 | #ifdef IPV6 | |
274 | case AF_INET6: | |
275 | { | |
276 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)addr; | |
277 | struct sockaddr_in6 *lin6 =(struct sockaddr_in6 *)&listener->addr; | |
278 | if(IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &lin6->sin6_addr) && | |
279 | in6->sin6_port == lin6->sin6_port) | |
280 | { | |
281 | if(listener->F == NULL) | |
282 | last_closed = listener; | |
283 | else | |
284 | return(listener); | |
285 | } | |
286 | break; | |
287 | ||
288 | } | |
289 | #endif | |
290 | ||
291 | default: | |
292 | break; | |
293 | } | |
294 | } | |
295 | return last_closed; | |
296 | } | |
297 | ||
298 | ||
299 | /* | |
300 | * add_listener- create a new listener | |
301 | * port - the port number to listen on | |
302 | * vhost_ip - if non-null must contain a valid IP address string in | |
303 | * the format "255.255.255.255" | |
304 | */ | |
305 | void | |
306 | add_listener(int port, const char *vhost_ip, int family) | |
307 | { | |
308 | listener_t *listener; | |
309 | struct rb_sockaddr_storage vaddr; | |
310 | ||
311 | /* | |
312 | * if no port in conf line, don't bother | |
313 | */ | |
314 | if(port == 0) | |
315 | return; | |
316 | memset(&vaddr, 0, sizeof(vaddr)); | |
317 | vaddr.ss_family = family; | |
318 | ||
319 | if(vhost_ip != NULL) | |
320 | { | |
321 | if(family == AF_INET) | |
322 | { | |
323 | if(inetpton(family, vhost_ip, &((struct sockaddr_in *)&vaddr)->sin_addr) <= 0) | |
324 | return; | |
325 | } | |
326 | #ifdef IPV6 | |
327 | else | |
328 | { | |
329 | if(inetpton(family, vhost_ip, &((struct sockaddr_in6 *)&vaddr)->sin6_addr) <= 0) | |
330 | return; | |
331 | ||
332 | } | |
333 | #endif | |
334 | } else | |
335 | { | |
336 | switch(family) | |
337 | { | |
338 | case AF_INET: | |
339 | ((struct sockaddr_in *)&vaddr)->sin_addr.s_addr = INADDR_ANY; | |
340 | break; | |
341 | #ifdef IPV6 | |
342 | case AF_INET6: | |
343 | memcpy(&((struct sockaddr_in6 *)&vaddr)->sin6_addr, &in6addr_any, sizeof(struct in6_addr)); | |
344 | break; | |
345 | default: | |
346 | return; | |
347 | #endif | |
348 | } | |
349 | } | |
350 | switch(family) | |
351 | { | |
352 | case AF_INET: | |
353 | SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in)); | |
354 | ((struct sockaddr_in *)&vaddr)->sin_port = htons(port); | |
355 | break; | |
356 | #ifdef IPV6 | |
357 | case AF_INET6: | |
358 | SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in6)); | |
359 | ((struct sockaddr_in6 *)&vaddr)->sin6_port = htons(port); | |
360 | break; | |
361 | #endif | |
362 | default: | |
363 | break; | |
364 | } | |
365 | if((listener = find_listener(&vaddr))) | |
366 | { | |
367 | if(listener->F != NULL) | |
368 | return; | |
369 | } | |
370 | else | |
371 | { | |
372 | listener = make_listener(&vaddr); | |
373 | listener->next = ListenerPollList; | |
374 | ListenerPollList = listener; | |
375 | } | |
376 | ||
377 | listener->F = NULL; | |
378 | ||
379 | if(inetport(listener)) | |
380 | listener->active = 1; | |
381 | else | |
382 | close_listener(listener); | |
383 | } | |
384 | ||
385 | /* | |
386 | * close_listener - close a single listener | |
387 | */ | |
388 | void | |
389 | close_listener(listener_t *listener) | |
390 | { | |
391 | s_assert(listener != NULL); | |
392 | if(listener == NULL) | |
393 | return; | |
394 | if(listener->F != NULL) | |
395 | { | |
396 | rb_close(listener->F); | |
397 | listener->F = NULL; | |
398 | } | |
399 | ||
400 | listener->active = 0; | |
401 | ||
402 | if(listener->ref_count) | |
403 | return; | |
404 | ||
405 | free_listener(listener); | |
406 | } | |
407 | ||
408 | /* | |
409 | * close_listeners - close and free all listeners that are not being used | |
410 | */ | |
411 | void | |
412 | close_listeners() | |
413 | { | |
414 | listener_t *listener; | |
415 | listener_t *listener_next = 0; | |
416 | /* | |
417 | * close all 'extra' listening ports we have | |
418 | */ | |
419 | for (listener = ListenerPollList; listener; listener = listener_next) | |
420 | { | |
421 | listener_next = listener->next; | |
422 | close_listener(listener); | |
423 | } | |
424 | } | |
425 | ||
426 | #define DLINE_WARNING "ERROR :You have been D-lined.\r\n" | |
427 | ||
428 | /* | |
429 | * add_connection - creates a client which has just connected to us on | |
430 | * the given fd. The sockhost field is initialized with the ip# of the host. | |
431 | * The client is sent to the auth module for verification, and not put in | |
432 | * any client list yet. | |
433 | */ | |
434 | static void | |
435 | add_connection(struct Listener *listener, rb_fde_t *F, struct sockaddr *sai, int exempt) | |
436 | { | |
437 | struct Client *new_client; | |
438 | s_assert(NULL != listener); | |
439 | ||
440 | /* | |
441 | * get the client socket name from the socket | |
442 | * the client has already been checked out in accept_connection | |
443 | */ | |
444 | new_client = make_client(NULL); | |
445 | ||
446 | memcpy(&new_client->localClient->ip, sai, sizeof(struct rb_sockaddr_storage)); | |
447 | ||
448 | /* | |
449 | * copy address to 'sockhost' as a string, copy it to host too | |
450 | * so we have something valid to put into error messages... | |
451 | */ | |
452 | inetntop_sock((struct sockaddr *)&new_client->localClient->ip, new_client->sockhost, | |
453 | sizeof(new_client->sockhost)); | |
454 | ||
455 | ||
456 | strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host)); | |
457 | ||
458 | new_client->localClient->F = F; | |
459 | ||
460 | new_client->localClient->listener = listener; | |
461 | ++listener->ref_count; | |
462 | ||
463 | if(!exempt) | |
464 | { | |
465 | if(check_reject(new_client)) | |
466 | return; | |
467 | if(add_unknown_ip(new_client)) | |
468 | return; | |
469 | } | |
470 | ||
471 | start_auth(new_client); | |
472 | } | |
473 | ||
474 | static int | |
475 | accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data) | |
476 | { | |
477 | struct Listener *listener = (struct Listener *)data; | |
478 | char buf[BUFSIZE]; | |
479 | struct ConfItem *aconf; | |
480 | static time_t last_oper_notice = 0; | |
481 | ||
482 | if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus */ | |
483 | { | |
484 | ++ServerStats->is_ref; | |
485 | /* | |
486 | * slow down the whining to opers bit | |
487 | */ | |
488 | if((last_oper_notice + 20) <= rb_current_time()) | |
489 | { | |
490 | sendto_realops_flags(SNO_GENERAL, L_ALL, | |
491 | "All connections in use. (%s)", | |
492 | get_listener_name(listener)); | |
493 | last_oper_notice = rb_current_time(); | |
494 | } | |
495 | ||
496 | rb_write(F, "ERROR :All connections in use\r\n", 32); | |
497 | rb_close(F); | |
498 | /* Re-register a new IO request for the next accept .. */ | |
499 | return 0; | |
500 | } | |
501 | ||
502 | aconf = find_dline(addr, AF_INET); | |
503 | if(aconf != NULL && (aconf->status & CONF_EXEMPTDLINE)) | |
504 | return 1; | |
505 | ||
506 | /* Do an initial check we aren't connecting too fast or with too many | |
507 | * from this IP... */ | |
508 | if(aconf != NULL) | |
509 | { | |
510 | ServerStats->is_ref++; | |
511 | ||
512 | if(ConfigFileEntry.dline_with_reason) | |
513 | { | |
514 | if (rb_snprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", aconf->passwd) >= (int)(sizeof(buf)-1)) | |
515 | { | |
516 | buf[sizeof(buf) - 3] = '\r'; | |
517 | buf[sizeof(buf) - 2] = '\n'; | |
518 | buf[sizeof(buf) - 1] = '\0'; | |
519 | } | |
520 | } | |
521 | else | |
522 | strcpy(buf, "ERROR :You have been D-lined.\r\n"); | |
523 | ||
524 | rb_write(F, buf, strlen(buf)); | |
525 | rb_close(F); | |
526 | return 0; | |
527 | } | |
528 | ||
529 | return 1; | |
530 | } | |
531 | ||
532 | static void | |
533 | accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data) | |
534 | { | |
535 | struct Listener *listener = data; | |
536 | struct rb_sockaddr_storage lip; | |
537 | unsigned int locallen = sizeof(struct rb_sockaddr_storage); | |
538 | ||
539 | ServerStats->is_ac++; | |
540 | ||
541 | if(getsockname(rb_get_fd(F), (struct sockaddr *) &lip, &locallen) < 0) | |
542 | { | |
543 | /* this shouldn't fail so... */ | |
544 | /* XXX add logging of this */ | |
545 | rb_close(F); | |
546 | } | |
547 | ||
548 | add_connection(listener, F, addr, 1); | |
549 | } |