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