]>
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 | ||
25 | #include "stdinc.h" | |
26 | #include "setup.h" | |
27 | #include "listener.h" | |
28 | #include "client.h" | |
29 | #include "match.h" | |
30 | #include "ircd.h" | |
31 | #include "ircd_defs.h" | |
32 | #include "numeric.h" | |
33 | #include "s_conf.h" | |
34 | #include "s_newconf.h" | |
35 | #include "s_stats.h" | |
36 | #include "send.h" | |
37 | #include "authproc.h" | |
38 | #include "reject.h" | |
39 | #include "hostmask.h" | |
40 | #include "sslproc.h" | |
41 | #include "wsproc.h" | |
42 | #include "hash.h" | |
43 | #include "s_assert.h" | |
44 | #include "logger.h" | |
45 | ||
46 | static struct Listener *ListenerPollList = NULL; | |
47 | static int accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data); | |
48 | static void accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data); | |
49 | static SSL_OPEN_CB accept_sslcallback; | |
50 | ||
51 | static struct Listener * | |
52 | make_listener(struct rb_sockaddr_storage *addr) | |
53 | { | |
54 | struct Listener *listener = (struct Listener *) rb_malloc(sizeof(struct Listener)); | |
55 | s_assert(0 != listener); | |
56 | listener->name = me.name; | |
57 | listener->F = NULL; | |
58 | ||
59 | memcpy(&listener->addr, addr, sizeof(listener->addr)); | |
60 | listener->next = NULL; | |
61 | return listener; | |
62 | } | |
63 | ||
64 | void | |
65 | free_listener(struct Listener *listener) | |
66 | { | |
67 | s_assert(NULL != listener); | |
68 | if(listener == NULL) | |
69 | return; | |
70 | /* | |
71 | * remove from listener list | |
72 | */ | |
73 | if(listener == ListenerPollList) | |
74 | ListenerPollList = listener->next; | |
75 | else | |
76 | { | |
77 | struct Listener *prev = ListenerPollList; | |
78 | for (; prev; prev = prev->next) | |
79 | { | |
80 | if(listener == prev->next) | |
81 | { | |
82 | prev->next = listener->next; | |
83 | break; | |
84 | } | |
85 | } | |
86 | } | |
87 | ||
88 | /* free */ | |
89 | rb_free(listener); | |
90 | } | |
91 | ||
92 | #define PORTNAMELEN 6 /* ":31337" */ | |
93 | ||
94 | /* | |
95 | * get_listener_port - return displayable listener port | |
96 | */ | |
97 | static uint16_t | |
98 | get_listener_port(const struct Listener *listener) | |
99 | { | |
100 | return ntohs(GET_SS_PORT(&listener->addr[0])); | |
101 | } | |
102 | ||
103 | /* | |
104 | * get_listener_name - return displayable listener name and port | |
105 | */ | |
106 | const char * | |
107 | get_listener_name(const struct Listener *listener) | |
108 | { | |
109 | static char buf[BUFSIZE]; | |
110 | ||
111 | snprintf(buf, sizeof(buf), "%s[%s/%u]", | |
112 | me.name, listener->name, get_listener_port(listener)); | |
113 | return buf; | |
114 | } | |
115 | ||
116 | /* | |
117 | * show_ports - send port listing to a client | |
118 | * inputs - pointer to client to show ports to | |
119 | * output - none | |
120 | * side effects - show ports | |
121 | */ | |
122 | void | |
123 | show_ports(struct Client *source_p) | |
124 | { | |
125 | struct Listener *listener = 0; | |
126 | ||
127 | for (listener = ListenerPollList; listener; listener = listener->next) | |
128 | { | |
129 | sendto_one_numeric(source_p, RPL_STATSPLINE, | |
130 | form_str(RPL_STATSPLINE), 'P', | |
131 | get_listener_port(listener), | |
132 | IsOperAdmin(source_p) ? listener->name : me.name, | |
133 | listener->ref_count, (listener->active) ? "active" : "disabled", | |
134 | listener->sctp ? " sctp" : " tcp", | |
135 | listener->ssl ? " ssl" : ""); | |
136 | } | |
137 | } | |
138 | ||
139 | /* | |
140 | * inetport - create a listener socket in the AF_INET or AF_INET6 domain, | |
141 | * bind it to the port given in 'port' and listen to it | |
142 | * returns true (1) if successful false (0) on error. | |
143 | */ | |
144 | ||
145 | static int | |
146 | inetport(struct Listener *listener) | |
147 | { | |
148 | rb_fde_t *F; | |
149 | const char *errstr; | |
150 | int ret; | |
151 | ||
152 | if (listener->sctp) { | |
153 | #ifdef HAVE_LIBSCTP | |
154 | /* only AF_INET6 sockets can have both AF_INET and AF_INET6 addresses */ | |
155 | F = rb_socket(AF_INET6, SOCK_STREAM, IPPROTO_SCTP, "Listener socket"); | |
156 | #else | |
157 | F = NULL; | |
158 | #endif | |
159 | } else { | |
160 | F = rb_socket(GET_SS_FAMILY(&listener->addr[0]), SOCK_STREAM, IPPROTO_TCP, "Listener socket"); | |
161 | } | |
162 | ||
163 | memset(listener->vhost, 0, sizeof(listener->vhost)); | |
164 | ||
165 | if (GET_SS_FAMILY(&listener->addr[0]) == AF_INET6) { | |
166 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&listener->addr[0]; | |
167 | rb_inet_ntop(AF_INET6, &in6->sin6_addr, listener->vhost, sizeof(listener->vhost)); | |
168 | } else if (GET_SS_FAMILY(&listener->addr[0]) == AF_INET) { | |
169 | struct sockaddr_in *in = (struct sockaddr_in *)&listener->addr[0]; | |
170 | rb_inet_ntop(AF_INET, &in->sin_addr, listener->vhost, sizeof(listener->vhost)); | |
171 | } | |
172 | ||
173 | if (GET_SS_FAMILY(&listener->addr[1]) == AF_INET6) { | |
174 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&listener->addr[1]; | |
175 | strncat(listener->vhost, "&", sizeof(listener->vhost)); | |
176 | rb_inet_ntop(AF_INET6, &in6->sin6_addr, &listener->vhost[strlen(listener->vhost)], sizeof(listener->vhost) - strlen(listener->vhost)); | |
177 | } else if (GET_SS_FAMILY(&listener->addr[1]) == AF_INET) { | |
178 | struct sockaddr_in *in = (struct sockaddr_in *)&listener->addr[1]; | |
179 | strncat(listener->vhost, "&", sizeof(listener->vhost)); | |
180 | rb_inet_ntop(AF_INET, &in->sin_addr, &listener->vhost[strlen(listener->vhost)], sizeof(listener->vhost) - strlen(listener->vhost)); | |
181 | } | |
182 | ||
183 | if (listener->vhost[0] != '\0') { | |
184 | listener->name = listener->vhost; | |
185 | } | |
186 | ||
187 | if (F == NULL) { | |
188 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
189 | "Cannot open socket for listener on %s port %d", | |
190 | listener->sctp ? "SCTP" : "TCP", | |
191 | get_listener_port(listener)); | |
192 | ilog(L_MAIN, "Cannot open socket for %s listener %s", | |
193 | listener->sctp ? "SCTP" : "TCP", | |
194 | get_listener_name(listener)); | |
195 | return 0; | |
196 | } | |
197 | ||
198 | if (listener->sctp) { | |
199 | ret = rb_sctp_bindx(F, listener->addr, ARRAY_SIZE(listener->addr)); | |
200 | } else { | |
201 | ret = rb_bind(F, (struct sockaddr *)&listener->addr[0]); | |
202 | } | |
203 | ||
204 | if (ret) { | |
205 | errstr = strerror(rb_get_sockerr(F)); | |
206 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
207 | "Cannot bind for listener on %s port %d: %s", | |
208 | listener->sctp ? "SCTP" : "TCP", | |
209 | get_listener_port(listener), errstr); | |
210 | ilog(L_MAIN, "Cannot bind for %s listener %s: %s", | |
211 | listener->sctp ? "SCTP" : "TCP", | |
212 | get_listener_name(listener), errstr); | |
213 | rb_close(F); | |
214 | return 0; | |
215 | } | |
216 | ||
217 | if(rb_listen(F, SOMAXCONN, listener->defer_accept)) | |
218 | { | |
219 | errstr = strerror(rb_get_sockerr(F)); | |
220 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
221 | "Cannot listen() for listener on %s port %d: %s", | |
222 | listener->sctp ? "SCTP" : "TCP", | |
223 | get_listener_port(listener), errstr); | |
224 | ilog(L_MAIN, "Cannot listen() for %s listener %s: %s", | |
225 | listener->sctp ? "SCTP" : "TCP", | |
226 | get_listener_name(listener), errstr); | |
227 | rb_close(F); | |
228 | return 0; | |
229 | } | |
230 | ||
231 | listener->F = F; | |
232 | ||
233 | rb_accept_tcp(listener->F, accept_precallback, accept_callback, listener); | |
234 | return 1; | |
235 | } | |
236 | ||
237 | static struct Listener * | |
238 | find_listener(struct rb_sockaddr_storage *addr, int sctp) | |
239 | { | |
240 | struct Listener *listener = NULL; | |
241 | struct Listener *last_closed = NULL; | |
242 | ||
243 | for (listener = ListenerPollList; listener; listener = listener->next) { | |
244 | if (listener->sctp != sctp) | |
245 | continue; | |
246 | ||
247 | for (int i = 0; i < ARRAY_SIZE(listener->addr); i++) { | |
248 | if (GET_SS_FAMILY(&addr[i]) != GET_SS_FAMILY(&listener->addr[i])) | |
249 | goto next; | |
250 | ||
251 | switch (GET_SS_FAMILY(&addr[i])) { | |
252 | case AF_INET: | |
253 | { | |
254 | struct sockaddr_in *in4 = (struct sockaddr_in *)&addr[i]; | |
255 | struct sockaddr_in *lin4 = (struct sockaddr_in *)&listener->addr[i]; | |
256 | if(in4->sin_addr.s_addr != lin4->sin_addr.s_addr || | |
257 | in4->sin_port != lin4->sin_port) | |
258 | { | |
259 | goto next; | |
260 | } | |
261 | break; | |
262 | } | |
263 | ||
264 | case AF_INET6: | |
265 | { | |
266 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&addr[i]; | |
267 | struct sockaddr_in6 *lin6 =(struct sockaddr_in6 *)&listener->addr[i]; | |
268 | if (!IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &lin6->sin6_addr) || | |
269 | in6->sin6_port != lin6->sin6_port) | |
270 | { | |
271 | goto next; | |
272 | } | |
273 | break; | |
274 | } | |
275 | ||
276 | default: | |
277 | break; | |
278 | } | |
279 | } | |
280 | ||
281 | if (listener->F == NULL) { | |
282 | last_closed = listener; | |
283 | } else { | |
284 | return listener; | |
285 | } | |
286 | ||
287 | next: | |
288 | continue; | |
289 | } | |
290 | return last_closed; | |
291 | } | |
292 | ||
293 | ||
294 | /* | |
295 | * add_tcp_listener- create a new listener | |
296 | * port - the port number to listen on | |
297 | * vhost_ip - if non-null must contain a valid IP address string in | |
298 | * the format "255.255.255.255" | |
299 | */ | |
300 | void | |
301 | add_tcp_listener(int port, const char *vhost_ip, int family, int ssl, int defer_accept, int wsock) | |
302 | { | |
303 | struct Listener *listener; | |
304 | struct rb_sockaddr_storage vaddr[ARRAY_SIZE(listener->addr)]; | |
305 | ||
306 | /* | |
307 | * if no port in conf line, don't bother | |
308 | */ | |
309 | if (port == 0) | |
310 | return; | |
311 | memset(&vaddr, 0, sizeof(vaddr)); | |
312 | SET_SS_FAMILY(&vaddr[0], AF_UNSPEC); | |
313 | SET_SS_LEN(&vaddr[0], sizeof(struct sockaddr_storage)); | |
314 | SET_SS_FAMILY(&vaddr[1], AF_UNSPEC); | |
315 | SET_SS_LEN(&vaddr[1], sizeof(struct sockaddr_storage)); | |
316 | ||
317 | if (vhost_ip != NULL) { | |
318 | if (family == AF_INET) { | |
319 | if (rb_inet_pton(family, vhost_ip, &((struct sockaddr_in *)&vaddr[0])->sin_addr) <= 0) | |
320 | return; | |
321 | } else { | |
322 | if (rb_inet_pton(family, vhost_ip, &((struct sockaddr_in6 *)&vaddr[0])->sin6_addr) <= 0) | |
323 | return; | |
324 | } | |
325 | } else { | |
326 | switch(family) { | |
327 | case AF_INET: | |
328 | ((struct sockaddr_in *)&vaddr[0])->sin_addr.s_addr = INADDR_ANY; | |
329 | break; | |
330 | case AF_INET6: | |
331 | memcpy(&((struct sockaddr_in6 *)&vaddr[0])->sin6_addr, &in6addr_any, sizeof(struct in6_addr)); | |
332 | break; | |
333 | default: | |
334 | return; | |
335 | } | |
336 | } | |
337 | switch(family) { | |
338 | case AF_INET: | |
339 | SET_SS_LEN(&vaddr[0], sizeof(struct sockaddr_in)); | |
340 | SET_SS_FAMILY(&vaddr[0], AF_INET); | |
341 | SET_SS_PORT(&vaddr[0], htons(port)); | |
342 | break; | |
343 | case AF_INET6: | |
344 | SET_SS_LEN(&vaddr[0], sizeof(struct sockaddr_in6)); | |
345 | SET_SS_FAMILY(&vaddr[0], AF_INET6); | |
346 | SET_SS_PORT(&vaddr[0], htons(port)); | |
347 | break; | |
348 | default: | |
349 | break; | |
350 | } | |
351 | if ((listener = find_listener(vaddr, 0))) { | |
352 | if (listener->F != NULL) | |
353 | return; | |
354 | } else { | |
355 | listener = make_listener(vaddr); | |
356 | listener->next = ListenerPollList; | |
357 | ListenerPollList = listener; | |
358 | } | |
359 | ||
360 | listener->F = NULL; | |
361 | listener->ssl = ssl; | |
362 | listener->defer_accept = defer_accept; | |
363 | listener->sctp = 0; | |
364 | listener->wsock = wsock; | |
365 | ||
366 | if (inetport(listener)) { | |
367 | listener->active = 1; | |
368 | } else { | |
369 | close_listener(listener); | |
370 | } | |
371 | } | |
372 | ||
373 | /* | |
374 | * add_sctp_listener- create a new listener | |
375 | * port - the port number to listen on | |
376 | * vhost_ip1/2 - if non-null must contain a valid IP address string | |
377 | */ | |
378 | void | |
379 | add_sctp_listener(int port, const char *vhost_ip1, const char *vhost_ip2, int ssl, int wsock) | |
380 | { | |
381 | struct Listener *listener; | |
382 | struct rb_sockaddr_storage vaddr[ARRAY_SIZE(listener->addr)]; | |
383 | ||
384 | /* | |
385 | * if no port in conf line, don't bother | |
386 | */ | |
387 | if (port == 0) | |
388 | return; | |
389 | memset(&vaddr, 0, sizeof(vaddr)); | |
390 | ||
391 | if (vhost_ip1 != NULL) { | |
392 | if (rb_inet_pton_sock(vhost_ip1, &vaddr[0]) <= 0) | |
393 | return; | |
394 | ||
395 | if (vhost_ip2 != NULL) { | |
396 | if (rb_inet_pton_sock(vhost_ip2, &vaddr[1]) <= 0) | |
397 | return; | |
398 | } else { | |
399 | SET_SS_FAMILY(&vaddr[1], AF_UNSPEC); | |
400 | SET_SS_LEN(&vaddr[1], sizeof(struct sockaddr_storage)); | |
401 | } | |
402 | ||
403 | if (GET_SS_FAMILY(&vaddr[0]) == AF_INET && GET_SS_FAMILY(&vaddr[1]) == AF_INET6) { | |
404 | /* always put INET6 first */ | |
405 | struct rb_sockaddr_storage tmp; | |
406 | tmp = vaddr[0]; | |
407 | vaddr[0] = vaddr[1]; | |
408 | vaddr[1] = tmp; | |
409 | } | |
410 | } else { | |
411 | memcpy(&((struct sockaddr_in6 *)&vaddr[0])->sin6_addr, &in6addr_any, sizeof(struct in6_addr)); | |
412 | SET_SS_FAMILY(&vaddr[0], AF_INET6); | |
413 | SET_SS_LEN(&vaddr[0], sizeof(struct sockaddr_in6)); | |
414 | ||
415 | SET_SS_FAMILY(&vaddr[1], AF_UNSPEC); | |
416 | SET_SS_LEN(&vaddr[1], sizeof(struct sockaddr_storage)); | |
417 | } | |
418 | ||
419 | SET_SS_PORT(&vaddr[0], htons(port)); | |
420 | SET_SS_PORT(&vaddr[1], htons(port)); | |
421 | ||
422 | if ((listener = find_listener(vaddr, 1))) { | |
423 | if(listener->F != NULL) | |
424 | return; | |
425 | } else { | |
426 | listener = make_listener(vaddr); | |
427 | listener->next = ListenerPollList; | |
428 | ListenerPollList = listener; | |
429 | } | |
430 | ||
431 | listener->F = NULL; | |
432 | listener->ssl = ssl; | |
433 | listener->defer_accept = 0; | |
434 | listener->sctp = 1; | |
435 | listener->wsock = wsock; | |
436 | ||
437 | if (inetport(listener)) { | |
438 | listener->active = 1; | |
439 | } else { | |
440 | close_listener(listener); | |
441 | } | |
442 | } | |
443 | ||
444 | /* | |
445 | * close_listener - close a single listener | |
446 | */ | |
447 | void | |
448 | close_listener(struct Listener *listener) | |
449 | { | |
450 | s_assert(listener != NULL); | |
451 | if(listener == NULL) | |
452 | return; | |
453 | if(listener->F != NULL) | |
454 | { | |
455 | rb_close(listener->F); | |
456 | listener->F = NULL; | |
457 | } | |
458 | ||
459 | listener->active = 0; | |
460 | ||
461 | if(listener->ref_count) | |
462 | return; | |
463 | ||
464 | free_listener(listener); | |
465 | } | |
466 | ||
467 | /* | |
468 | * close_listeners - close and free all listeners that are not being used | |
469 | */ | |
470 | void | |
471 | close_listeners() | |
472 | { | |
473 | struct Listener *listener; | |
474 | struct Listener *listener_next = 0; | |
475 | /* | |
476 | * close all 'extra' listening ports we have | |
477 | */ | |
478 | for (listener = ListenerPollList; listener; listener = listener_next) | |
479 | { | |
480 | listener_next = listener->next; | |
481 | close_listener(listener); | |
482 | } | |
483 | } | |
484 | ||
485 | /* | |
486 | * add_connection - creates a client which has just connected to us on | |
487 | * the given fd. The sockhost field is initialized with the ip# of the host. | |
488 | * The client is sent to the auth module for verification, and not put in | |
489 | * any client list yet. | |
490 | */ | |
491 | static void | |
492 | add_connection(struct Listener *listener, rb_fde_t *F, struct sockaddr *sai, struct sockaddr *lai) | |
493 | { | |
494 | struct Client *new_client; | |
495 | bool defer = false; | |
496 | s_assert(NULL != listener); | |
497 | ||
498 | /* | |
499 | * get the client socket name from the socket | |
500 | * the client has already been checked out in accept_connection | |
501 | */ | |
502 | new_client = make_client(NULL); | |
503 | new_client->localClient->F = F; | |
504 | ||
505 | memcpy(&new_client->localClient->ip, sai, sizeof(struct rb_sockaddr_storage)); | |
506 | memcpy(&new_client->preClient->lip, lai, sizeof(struct rb_sockaddr_storage)); | |
507 | ||
508 | /* | |
509 | * copy address to 'sockhost' as a string, copy it to host too | |
510 | * so we have something valid to put into error messages... | |
511 | */ | |
512 | rb_inet_ntop_sock((struct sockaddr *)&new_client->localClient->ip, new_client->sockhost, | |
513 | sizeof(new_client->sockhost)); | |
514 | ||
515 | rb_strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host)); | |
516 | ||
517 | if (listener->sctp) { | |
518 | SetSCTP(new_client); | |
519 | } | |
520 | ||
521 | if (listener->ssl) | |
522 | { | |
523 | rb_fde_t *xF[2]; | |
524 | if(rb_socketpair(AF_UNIX, SOCK_STREAM, 0, &xF[0], &xF[1], "Incoming ssld Connection") == -1) | |
525 | { | |
526 | SetIOError(new_client); | |
527 | exit_client(new_client, new_client, new_client, "Fatal Error"); | |
528 | return; | |
529 | } | |
530 | new_client->localClient->ssl_callback = accept_sslcallback; | |
531 | defer = true; | |
532 | new_client->localClient->ssl_ctl = start_ssld_accept(F, xF[1], connid_get(new_client)); /* this will close F for us */ | |
533 | if(new_client->localClient->ssl_ctl == NULL) | |
534 | { | |
535 | SetIOError(new_client); | |
536 | exit_client(new_client, new_client, new_client, "Service Unavailable"); | |
537 | return; | |
538 | } | |
539 | F = xF[0]; | |
540 | new_client->localClient->F = F; | |
541 | SetSSL(new_client); | |
542 | } | |
543 | ||
544 | if (listener->wsock) | |
545 | { | |
546 | rb_fde_t *xF[2]; | |
547 | if(rb_socketpair(AF_UNIX, SOCK_STREAM, 0, &xF[0], &xF[1], "Incoming wsockd Connection") == -1) | |
548 | { | |
549 | SetIOError(new_client); | |
550 | exit_client(new_client, new_client, new_client, "Fatal Error"); | |
551 | return; | |
552 | } | |
553 | new_client->localClient->ws_ctl = start_wsockd_accept(F, xF[1], connid_get(new_client)); /* this will close F for us */ | |
554 | if(new_client->localClient->ws_ctl == NULL) | |
555 | { | |
556 | SetIOError(new_client); | |
557 | exit_client(new_client, new_client, new_client, "Service Unavailable"); | |
558 | return; | |
559 | } | |
560 | F = xF[0]; | |
561 | new_client->localClient->F = F; | |
562 | } | |
563 | ||
564 | new_client->localClient->listener = listener; | |
565 | ||
566 | ++listener->ref_count; | |
567 | ||
568 | authd_initiate_client(new_client, defer); | |
569 | } | |
570 | ||
571 | static int | |
572 | accept_sslcallback(struct Client *client_p, int status) | |
573 | { | |
574 | authd_deferred_client(client_p); | |
575 | return 0; /* use default handler if status != RB_OK */ | |
576 | } | |
577 | ||
578 | static const char *toofast = "ERROR :Reconnecting too fast, throttled.\r\n"; | |
579 | ||
580 | static int | |
581 | accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data) | |
582 | { | |
583 | struct Listener *listener = (struct Listener *)data; | |
584 | char buf[BUFSIZE]; | |
585 | struct ConfItem *aconf; | |
586 | static time_t last_oper_notice = 0; | |
587 | int len; | |
588 | ||
589 | if(listener->ssl && (!ircd_ssl_ok || !get_ssld_count())) | |
590 | { | |
591 | rb_close(F); | |
592 | return 0; | |
593 | } | |
594 | ||
595 | if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus */ | |
596 | { | |
597 | ++ServerStats.is_ref; | |
598 | /* | |
599 | * slow down the whining to opers bit | |
600 | */ | |
601 | if((last_oper_notice + 20) <= rb_current_time()) | |
602 | { | |
603 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
604 | "All connections in use. (%s)", | |
605 | get_listener_name(listener)); | |
606 | last_oper_notice = rb_current_time(); | |
607 | } | |
608 | ||
609 | rb_write(F, "ERROR :All connections in use\r\n", 31); | |
610 | rb_close(F); | |
611 | return 0; | |
612 | } | |
613 | ||
614 | aconf = find_dline(addr, addr->sa_family); | |
615 | if(aconf != NULL && (aconf->status & CONF_EXEMPTDLINE)) | |
616 | return 1; | |
617 | ||
618 | /* Do an initial check we aren't connecting too fast or with too many | |
619 | * from this IP... */ | |
620 | if(aconf != NULL) | |
621 | { | |
622 | ServerStats.is_ref++; | |
623 | ||
624 | if(ConfigFileEntry.dline_with_reason) | |
625 | { | |
626 | len = snprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", get_user_ban_reason(aconf)); | |
627 | if (len >= (int)(sizeof(buf)-1)) | |
628 | { | |
629 | buf[sizeof(buf) - 3] = '\r'; | |
630 | buf[sizeof(buf) - 2] = '\n'; | |
631 | buf[sizeof(buf) - 1] = '\0'; | |
632 | } | |
633 | } | |
634 | else | |
635 | strcpy(buf, "ERROR :You have been D-lined.\r\n"); | |
636 | ||
637 | rb_write(F, buf, strlen(buf)); | |
638 | rb_close(F); | |
639 | return 0; | |
640 | } | |
641 | ||
642 | if(check_reject(F, addr)) { | |
643 | /* Reject the connection without closing the socket | |
644 | * because it is now on the delay_exit list. */ | |
645 | return 0; | |
646 | } | |
647 | ||
648 | if(throttle_add(addr)) | |
649 | { | |
650 | rb_write(F, toofast, strlen(toofast)); | |
651 | rb_close(F); | |
652 | return 0; | |
653 | } | |
654 | ||
655 | return 1; | |
656 | } | |
657 | ||
658 | static void | |
659 | accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data) | |
660 | { | |
661 | struct Listener *listener = data; | |
662 | struct rb_sockaddr_storage lip; | |
663 | unsigned int locallen = sizeof(struct rb_sockaddr_storage); | |
664 | ||
665 | ServerStats.is_ac++; | |
666 | ||
667 | if(getsockname(rb_get_fd(F), (struct sockaddr *) &lip, &locallen) < 0) | |
668 | { | |
669 | /* this can fail if the connection disappeared in the meantime */ | |
670 | rb_close(F); | |
671 | return; | |
672 | } | |
673 | ||
674 | add_connection(listener, F, addr, (struct sockaddr *)&lip); | |
675 | } |