]>
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 | |
8f103562 | 46 | static struct Listener *ListenerPollList = NULL; |
1087485c | 47 | static int accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data); |
8e09c4a2 | 48 | static void accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data); |
762468f8 | 49 | static SSL_OPEN_CB accept_sslcallback; |
54ac8b60 | 50 | |
8f103562 | 51 | static struct Listener * |
e7046ee5 | 52 | make_listener(struct rb_sockaddr_storage *addr) |
54ac8b60 | 53 | { |
8f103562 | 54 | struct Listener *listener = (struct Listener *) rb_malloc(sizeof(struct Listener)); |
54ac8b60 | 55 | s_assert(0 != listener); |
54ac8b60 | 56 | listener->name = me.name; |
f691939a VY |
57 | listener->F = NULL; |
58 | ||
c6ad9b0c | 59 | memcpy(&listener->addr, addr, sizeof(listener->addr)); |
54ac8b60 VY |
60 | listener->next = NULL; |
61 | return listener; | |
62 | } | |
63 | ||
64 | void | |
8f103562 | 65 | free_listener(struct Listener *listener) |
54ac8b60 VY |
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 | { | |
8f103562 | 77 | struct Listener *prev = ListenerPollList; |
54ac8b60 VY |
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 | ||
40173bcb JT |
94 | /* |
95 | * get_listener_port - return displayable listener port | |
96 | */ | |
97 | static uint16_t | |
98 | get_listener_port(const struct Listener *listener) | |
99 | { | |
c6ad9b0c | 100 | return ntohs(GET_SS_PORT(&listener->addr[0])); |
40173bcb JT |
101 | } |
102 | ||
54ac8b60 VY |
103 | /* |
104 | * get_listener_name - return displayable listener name and port | |
54ac8b60 VY |
105 | */ |
106 | const char * | |
8f103562 | 107 | get_listener_name(const struct Listener *listener) |
54ac8b60 | 108 | { |
c6ad9b0c | 109 | static char buf[BUFSIZE]; |
54ac8b60 | 110 | |
5203cba5 | 111 | snprintf(buf, sizeof(buf), "%s[%s/%u]", |
40173bcb | 112 | me.name, listener->name, get_listener_port(listener)); |
54ac8b60 VY |
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 | { | |
8f103562 | 125 | struct Listener *listener = 0; |
54ac8b60 VY |
126 | |
127 | for (listener = ListenerPollList; listener; listener = listener->next) | |
128 | { | |
55abcbb2 | 129 | sendto_one_numeric(source_p, RPL_STATSPLINE, |
40173bcb JT |
130 | form_str(RPL_STATSPLINE), 'P', |
131 | get_listener_port(listener), | |
54ac8b60 | 132 | IsOperAdmin(source_p) ? listener->name : me.name, |
8bd5767b | 133 | listener->ref_count, (listener->active) ? "active" : "disabled", |
c6ad9b0c | 134 | listener->sctp ? " sctp" : " tcp", |
c6d72037 | 135 | listener->ssl ? " ssl" : ""); |
54ac8b60 VY |
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. | |
54ac8b60 | 143 | */ |
54ac8b60 VY |
144 | |
145 | static int | |
8f103562 | 146 | inetport(struct Listener *listener) |
54ac8b60 | 147 | { |
f691939a | 148 | rb_fde_t *F; |
40173bcb | 149 | const char *errstr; |
c6ad9b0c SA |
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 | } | |
54ac8b60 | 162 | |
c6ad9b0c | 163 | memset(listener->vhost, 0, sizeof(listener->vhost)); |
55abcbb2 | 164 | |
c6ad9b0c SA |
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 | } | |
54ac8b60 | 172 | |
c6ad9b0c SA |
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)); | |
54ac8b60 VY |
181 | } |
182 | ||
c6ad9b0c SA |
183 | if (listener->vhost[0] != '\0') { |
184 | listener->name = listener->vhost; | |
1087485c | 185 | } |
c6ad9b0c SA |
186 | |
187 | if (F == NULL) { | |
40173bcb | 188 | sendto_realops_snomask(SNO_GENERAL, L_ALL, |
c6ad9b0c SA |
189 | "Cannot open socket for listener on %s port %d", |
190 | listener->sctp ? "SCTP" : "TCP", | |
40173bcb | 191 | get_listener_port(listener)); |
c6ad9b0c SA |
192 | ilog(L_MAIN, "Cannot open socket for %s listener %s", |
193 | listener->sctp ? "SCTP" : "TCP", | |
40173bcb | 194 | get_listener_name(listener)); |
1087485c | 195 | return 0; |
54ac8b60 VY |
196 | } |
197 | ||
c6ad9b0c SA |
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]); | |
54ac8b60 VY |
202 | } |
203 | ||
c6ad9b0c | 204 | if (ret) { |
40173bcb JT |
205 | errstr = strerror(rb_get_sockerr(F)); |
206 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
c6ad9b0c SA |
207 | "Cannot bind for listener on %s port %d: %s", |
208 | listener->sctp ? "SCTP" : "TCP", | |
40173bcb | 209 | get_listener_port(listener), errstr); |
c6ad9b0c SA |
210 | ilog(L_MAIN, "Cannot bind for %s listener %s: %s", |
211 | listener->sctp ? "SCTP" : "TCP", | |
40173bcb | 212 | get_listener_name(listener), errstr); |
1087485c JT |
213 | rb_close(F); |
214 | return 0; | |
54ac8b60 VY |
215 | } |
216 | ||
9057170c | 217 | if(rb_listen(F, SOMAXCONN, listener->defer_accept)) |
1087485c | 218 | { |
40173bcb JT |
219 | errstr = strerror(rb_get_sockerr(F)); |
220 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
c6ad9b0c SA |
221 | "Cannot listen() for listener on %s port %d: %s", |
222 | listener->sctp ? "SCTP" : "TCP", | |
40173bcb | 223 | get_listener_port(listener), errstr); |
c6ad9b0c SA |
224 | ilog(L_MAIN, "Cannot listen() for %s listener %s: %s", |
225 | listener->sctp ? "SCTP" : "TCP", | |
40173bcb | 226 | get_listener_name(listener), errstr); |
1087485c JT |
227 | rb_close(F); |
228 | return 0; | |
54ac8b60 VY |
229 | } |
230 | ||
1087485c JT |
231 | listener->F = F; |
232 | ||
f691939a | 233 | rb_accept_tcp(listener->F, accept_precallback, accept_callback, listener); |
54ac8b60 VY |
234 | return 1; |
235 | } | |
236 | ||
8f103562 | 237 | static struct Listener * |
c6ad9b0c | 238 | find_listener(struct rb_sockaddr_storage *addr, int sctp) |
54ac8b60 | 239 | { |
8f103562 JT |
240 | struct Listener *listener = NULL; |
241 | struct Listener *last_closed = NULL; | |
54ac8b60 | 242 | |
c6ad9b0c SA |
243 | for (listener = ListenerPollList; listener; listener = listener->next) { |
244 | if (listener->sctp != sctp) | |
54ac8b60 | 245 | continue; |
55abcbb2 | 246 | |
c6ad9b0c SA |
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: | |
54ac8b60 | 253 | { |
c6ad9b0c SA |
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; | |
54ac8b60 | 262 | } |
c6ad9b0c SA |
263 | |
264 | case AF_INET6: | |
54ac8b60 | 265 | { |
c6ad9b0c SA |
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; | |
54ac8b60 | 274 | } |
55abcbb2 | 275 | |
c6ad9b0c SA |
276 | default: |
277 | break; | |
54ac8b60 | 278 | } |
c6ad9b0c | 279 | } |
54ac8b60 | 280 | |
c6ad9b0c SA |
281 | if (listener->F == NULL) { |
282 | last_closed = listener; | |
283 | } else { | |
284 | return listener; | |
54ac8b60 | 285 | } |
c6ad9b0c SA |
286 | |
287 | next: | |
288 | continue; | |
54ac8b60 VY |
289 | } |
290 | return last_closed; | |
291 | } | |
292 | ||
293 | ||
294 | /* | |
c6ad9b0c | 295 | * add_tcp_listener- create a new listener |
54ac8b60 VY |
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 | |
c6ad9b0c | 301 | add_tcp_listener(int port, const char *vhost_ip, int family, int ssl, int defer_accept, int wsock) |
54ac8b60 | 302 | { |
8f103562 | 303 | struct Listener *listener; |
c6ad9b0c | 304 | struct rb_sockaddr_storage vaddr[ARRAY_SIZE(listener->addr)]; |
54ac8b60 VY |
305 | |
306 | /* | |
307 | * if no port in conf line, don't bother | |
308 | */ | |
c6ad9b0c | 309 | if (port == 0) |
54ac8b60 VY |
310 | return; |
311 | memset(&vaddr, 0, sizeof(vaddr)); | |
c6ad9b0c SA |
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) | |
54ac8b60 | 320 | return; |
c6ad9b0c SA |
321 | } else { |
322 | if (rb_inet_pton(family, vhost_ip, &((struct sockaddr_in6 *)&vaddr[0])->sin6_addr) <= 0) | |
54ac8b60 | 323 | return; |
54ac8b60 | 324 | } |
c6ad9b0c SA |
325 | } else { |
326 | switch(family) { | |
54ac8b60 | 327 | case AF_INET: |
c6ad9b0c | 328 | ((struct sockaddr_in *)&vaddr[0])->sin_addr.s_addr = INADDR_ANY; |
54ac8b60 | 329 | break; |
54ac8b60 | 330 | case AF_INET6: |
c6ad9b0c | 331 | memcpy(&((struct sockaddr_in6 *)&vaddr[0])->sin6_addr, &in6addr_any, sizeof(struct in6_addr)); |
54ac8b60 VY |
332 | break; |
333 | default: | |
334 | return; | |
55abcbb2 | 335 | } |
54ac8b60 | 336 | } |
c6ad9b0c | 337 | switch(family) { |
54ac8b60 | 338 | case AF_INET: |
c6ad9b0c SA |
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)); | |
54ac8b60 | 342 | break; |
54ac8b60 | 343 | case AF_INET6: |
c6ad9b0c SA |
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)); | |
54ac8b60 | 347 | break; |
54ac8b60 VY |
348 | default: |
349 | break; | |
350 | } | |
c6ad9b0c SA |
351 | if ((listener = find_listener(vaddr, 0))) { |
352 | if (listener->F != NULL) | |
54ac8b60 | 353 | return; |
c6ad9b0c SA |
354 | } else { |
355 | listener = make_listener(vaddr); | |
54ac8b60 VY |
356 | listener->next = ListenerPollList; |
357 | ListenerPollList = listener; | |
358 | } | |
359 | ||
f691939a | 360 | listener->F = NULL; |
c6d72037 | 361 | listener->ssl = ssl; |
02270e96 | 362 | listener->defer_accept = defer_accept; |
c6ad9b0c | 363 | listener->sctp = 0; |
c53ca1e0 | 364 | listener->wsock = wsock; |
54ac8b60 | 365 | |
c6ad9b0c | 366 | if (inetport(listener)) { |
54ac8b60 | 367 | listener->active = 1; |
c6ad9b0c | 368 | } else { |
54ac8b60 | 369 | close_listener(listener); |
c6ad9b0c SA |
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) { | |
17809d2d | 392 | if (rb_inet_pton_sock(vhost_ip1, &vaddr[0]) <= 0) |
c6ad9b0c SA |
393 | return; |
394 | ||
395 | if (vhost_ip2 != NULL) { | |
17809d2d | 396 | if (rb_inet_pton_sock(vhost_ip2, &vaddr[1]) <= 0) |
c6ad9b0c SA |
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 | } | |
54ac8b60 VY |
442 | } |
443 | ||
444 | /* | |
445 | * close_listener - close a single listener | |
446 | */ | |
447 | void | |
8f103562 | 448 | close_listener(struct Listener *listener) |
54ac8b60 VY |
449 | { |
450 | s_assert(listener != NULL); | |
451 | if(listener == NULL) | |
452 | return; | |
1087485c JT |
453 | if(listener->F != NULL) |
454 | { | |
455 | rb_close(listener->F); | |
456 | listener->F = NULL; | |
54ac8b60 VY |
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 | { | |
8f103562 JT |
473 | struct Listener *listener; |
474 | struct Listener *listener_next = 0; | |
54ac8b60 VY |
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 | ||
54ac8b60 | 485 | /* |
55abcbb2 | 486 | * add_connection - creates a client which has just connected to us on |
54ac8b60 VY |
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 | */ | |
1087485c | 491 | static void |
b5b4a0e7 | 492 | add_connection(struct Listener *listener, rb_fde_t *F, struct sockaddr *sai, struct sockaddr *lai) |
54ac8b60 VY |
493 | { |
494 | struct Client *new_client; | |
762468f8 | 495 | bool defer = false; |
54ac8b60 VY |
496 | s_assert(NULL != listener); |
497 | ||
55abcbb2 | 498 | /* |
54ac8b60 VY |
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); | |
50b1e598 SA |
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)); | |
54ac8b60 | 516 | |
c6ad9b0c SA |
517 | if (listener->sctp) { |
518 | SetSCTP(new_client); | |
519 | } | |
520 | ||
b5b4a0e7 AC |
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 | { | |
50b1e598 SA |
526 | SetIOError(new_client); |
527 | exit_client(new_client, new_client, new_client, "Fatal Error"); | |
b5b4a0e7 AC |
528 | return; |
529 | } | |
762468f8 SA |
530 | new_client->localClient->ssl_callback = accept_sslcallback; |
531 | defer = true; | |
de7cf7e0 | 532 | new_client->localClient->ssl_ctl = start_ssld_accept(F, xF[1], connid_get(new_client)); /* this will close F for us */ |
b5b4a0e7 AC |
533 | if(new_client->localClient->ssl_ctl == NULL) |
534 | { | |
50b1e598 SA |
535 | SetIOError(new_client); |
536 | exit_client(new_client, new_client, new_client, "Service Unavailable"); | |
b5b4a0e7 AC |
537 | return; |
538 | } | |
539 | F = xF[0]; | |
50b1e598 | 540 | new_client->localClient->F = F; |
b5b4a0e7 | 541 | SetSSL(new_client); |
bbdc439a | 542 | SetSecure(new_client); |
b5b4a0e7 | 543 | } |
1cf798be EK |
544 | else |
545 | { | |
546 | struct ConfItem *aconf; | |
547 | aconf = find_conf_by_address(NULL, NULL, NULL, sai, CONF_SECURE | 1, sai->sa_family, NULL, NULL); | |
548 | ||
549 | if (aconf != NULL) | |
550 | SetSecure(new_client); | |
551 | } | |
b5b4a0e7 | 552 | |
c53ca1e0 AC |
553 | if (listener->wsock) |
554 | { | |
555 | rb_fde_t *xF[2]; | |
556 | if(rb_socketpair(AF_UNIX, SOCK_STREAM, 0, &xF[0], &xF[1], "Incoming wsockd Connection") == -1) | |
557 | { | |
50b1e598 SA |
558 | SetIOError(new_client); |
559 | exit_client(new_client, new_client, new_client, "Fatal Error"); | |
c53ca1e0 AC |
560 | return; |
561 | } | |
562 | new_client->localClient->ws_ctl = start_wsockd_accept(F, xF[1], connid_get(new_client)); /* this will close F for us */ | |
563 | if(new_client->localClient->ws_ctl == NULL) | |
564 | { | |
50b1e598 SA |
565 | SetIOError(new_client); |
566 | exit_client(new_client, new_client, new_client, "Service Unavailable"); | |
c53ca1e0 AC |
567 | return; |
568 | } | |
569 | F = xF[0]; | |
50b1e598 | 570 | new_client->localClient->F = F; |
c53ca1e0 AC |
571 | } |
572 | ||
54ac8b60 | 573 | new_client->localClient->listener = listener; |
c6d72037 | 574 | |
54ac8b60 VY |
575 | ++listener->ref_count; |
576 | ||
762468f8 SA |
577 | authd_initiate_client(new_client, defer); |
578 | } | |
579 | ||
580 | static int | |
581 | accept_sslcallback(struct Client *client_p, int status) | |
582 | { | |
583 | authd_deferred_client(client_p); | |
584 | return 0; /* use default handler if status != RB_OK */ | |
54ac8b60 | 585 | } |
0d89d5cd | 586 | |
43946961 JT |
587 | static const char *toofast = "ERROR :Reconnecting too fast, throttled.\r\n"; |
588 | ||
1087485c JT |
589 | static int |
590 | accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data) | |
591 | { | |
592 | struct Listener *listener = (struct Listener *)data; | |
593 | char buf[BUFSIZE]; | |
594 | struct ConfItem *aconf; | |
595 | static time_t last_oper_notice = 0; | |
b52c2949 | 596 | int len; |
1087485c | 597 | |
bfc44622 | 598 | if(listener->ssl && (!ircd_ssl_ok || !get_ssld_count())) |
8bd5767b JT |
599 | { |
600 | rb_close(F); | |
601 | return 0; | |
c6d72037 VY |
602 | } |
603 | ||
1087485c JT |
604 | if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus */ |
605 | { | |
47adde3d | 606 | ++ServerStats.is_ref; |
1087485c JT |
607 | /* |
608 | * slow down the whining to opers bit | |
609 | */ | |
610 | if((last_oper_notice + 20) <= rb_current_time()) | |
611 | { | |
26716d6d | 612 | sendto_realops_snomask(SNO_GENERAL, L_ALL, |
1087485c JT |
613 | "All connections in use. (%s)", |
614 | get_listener_name(listener)); | |
615 | last_oper_notice = rb_current_time(); | |
616 | } | |
55abcbb2 | 617 | |
76d82c19 | 618 | rb_write(F, "ERROR :All connections in use\r\n", 31); |
1087485c | 619 | rb_close(F); |
1087485c JT |
620 | return 0; |
621 | } | |
622 | ||
41d7fefa | 623 | aconf = find_dline(addr, addr->sa_family); |
1087485c JT |
624 | if(aconf != NULL && (aconf->status & CONF_EXEMPTDLINE)) |
625 | return 1; | |
55abcbb2 | 626 | |
1087485c JT |
627 | /* Do an initial check we aren't connecting too fast or with too many |
628 | * from this IP... */ | |
629 | if(aconf != NULL) | |
630 | { | |
47adde3d | 631 | ServerStats.is_ref++; |
55abcbb2 | 632 | |
1087485c JT |
633 | if(ConfigFileEntry.dline_with_reason) |
634 | { | |
5203cba5 | 635 | len = snprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", get_user_ban_reason(aconf)); |
b52c2949 | 636 | if (len >= (int)(sizeof(buf)-1)) |
1087485c JT |
637 | { |
638 | buf[sizeof(buf) - 3] = '\r'; | |
639 | buf[sizeof(buf) - 2] = '\n'; | |
640 | buf[sizeof(buf) - 1] = '\0'; | |
641 | } | |
642 | } | |
643 | else | |
644 | strcpy(buf, "ERROR :You have been D-lined.\r\n"); | |
55abcbb2 | 645 | |
1087485c JT |
646 | rb_write(F, buf, strlen(buf)); |
647 | rb_close(F); | |
648 | return 0; | |
649 | } | |
650 | ||
8467fd9c SA |
651 | if(check_reject(F, addr)) { |
652 | /* Reject the connection without closing the socket | |
653 | * because it is now on the delay_exit list. */ | |
43946961 | 654 | return 0; |
8467fd9c | 655 | } |
55abcbb2 | 656 | |
43946961 JT |
657 | if(throttle_add(addr)) |
658 | { | |
659 | rb_write(F, toofast, strlen(toofast)); | |
660 | rb_close(F); | |
661 | return 0; | |
662 | } | |
663 | ||
1087485c JT |
664 | return 1; |
665 | } | |
666 | ||
667 | static void | |
668 | accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data) | |
669 | { | |
670 | struct Listener *listener = data; | |
671 | struct rb_sockaddr_storage lip; | |
672 | unsigned int locallen = sizeof(struct rb_sockaddr_storage); | |
55abcbb2 | 673 | |
47adde3d | 674 | ServerStats.is_ac++; |
1087485c JT |
675 | |
676 | if(getsockname(rb_get_fd(F), (struct sockaddr *) &lip, &locallen) < 0) | |
677 | { | |
d60a42a2 | 678 | /* this can fail if the connection disappeared in the meantime */ |
1087485c | 679 | rb_close(F); |
9692f954 | 680 | return; |
1087485c | 681 | } |
55abcbb2 | 682 | |
b5b4a0e7 | 683 | add_connection(listener, F, addr, (struct sockaddr *)&lip); |
0d89d5cd | 684 | } |