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