1 |
|
/* |
2 |
< |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
< |
* s_bsd.c: Network functions. |
2 |
> |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
|
* |
4 |
< |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
4 |
> |
* Copyright (c) 1997-2014 ircd-hybrid development team |
5 |
|
* |
6 |
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
* it under the terms of the GNU General Public License as published by |
17 |
|
* along with this program; if not, write to the Free Software |
18 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
19 |
|
* USA |
20 |
< |
* |
21 |
< |
* $Id$ |
20 |
> |
*/ |
21 |
> |
|
22 |
> |
/*! \file s_bsd.c |
23 |
> |
* \brief Network functions. |
24 |
> |
* \version $Id$ |
25 |
|
*/ |
26 |
|
|
27 |
|
#include "stdinc.h" |
42 |
|
#include "irc_res.h" |
43 |
|
#include "restart.h" |
44 |
|
#include "s_auth.h" |
45 |
< |
#include "s_conf.h" |
46 |
< |
#include "s_log.h" |
45 |
> |
#include "conf.h" |
46 |
> |
#include "log.h" |
47 |
|
#include "s_serv.h" |
48 |
|
#include "send.h" |
49 |
|
#include "memory.h" |
50 |
|
#include "s_user.h" |
51 |
< |
#include "hook.h" |
51 |
> |
|
52 |
|
|
53 |
|
static const char *comm_err_str[] = { "Comm OK", "Error during bind()", |
54 |
|
"Error during DNS lookup", "connect timeout", "Error during connect()", |
55 |
|
"Comm Error" }; |
56 |
|
|
55 |
– |
struct Callback *setup_socket_cb = NULL; |
56 |
– |
|
57 |
|
static void comm_connect_callback(fde_t *, int); |
58 |
|
static PF comm_connect_timeout; |
59 |
|
static void comm_connect_dns_callback(void *, const struct irc_ssaddr *, const char *); |
106 |
|
} |
107 |
|
|
108 |
|
/* |
109 |
< |
* report_error - report an error from an errno. |
109 |
> |
* report_error - report an error from an errno. |
110 |
|
* Record error to log and also send a copy to all *LOCAL* opers online. |
111 |
|
* |
112 |
|
* text is a *format* string for outputing error. It must |
120 |
|
* Cannot use perror() within daemon. stderr is closed in |
121 |
|
* ircd and cannot be used. And, worse yet, it might have |
122 |
|
* been reassigned to a normal connection... |
123 |
< |
* |
123 |
> |
* |
124 |
|
* Actually stderr is still there IFF ircd was run with -s --Rodder |
125 |
|
*/ |
126 |
|
|
127 |
|
void |
128 |
< |
report_error(int level, const char* text, const char* who, int error) |
128 |
> |
report_error(int level, const char* text, const char* who, int error) |
129 |
|
{ |
130 |
|
who = (who) ? who : ""; |
131 |
|
|
132 |
< |
sendto_realops_flags(UMODE_DEBUG, level, text, who, strerror(error)); |
132 |
> |
sendto_realops_flags(UMODE_DEBUG, level, SEND_NOTICE, |
133 |
> |
text, who, strerror(error)); |
134 |
|
ilog(LOG_TYPE_IRCD, text, who, strerror(error)); |
135 |
|
} |
136 |
|
|
139 |
|
* |
140 |
|
* Set the socket non-blocking, and other wonderful bits. |
141 |
|
*/ |
142 |
< |
static void * |
143 |
< |
setup_socket(va_list args) |
142 |
> |
static void |
143 |
> |
setup_socket(int fd) |
144 |
|
{ |
144 |
– |
int fd = va_arg(args, int); |
145 |
|
int opt = 1; |
146 |
|
|
147 |
|
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt)); |
152 |
|
#endif |
153 |
|
|
154 |
|
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK); |
155 |
– |
|
156 |
– |
return NULL; |
157 |
– |
} |
158 |
– |
|
159 |
– |
/* |
160 |
– |
* init_comm() |
161 |
– |
* |
162 |
– |
* Initializes comm subsystem. |
163 |
– |
*/ |
164 |
– |
void |
165 |
– |
init_comm(void) |
166 |
– |
{ |
167 |
– |
setup_socket_cb = register_callback("setup_socket", setup_socket); |
168 |
– |
init_netio(); |
155 |
|
} |
156 |
|
|
157 |
|
/* |
162 |
|
void |
163 |
|
close_connection(struct Client *client_p) |
164 |
|
{ |
165 |
< |
struct ConfItem *conf; |
180 |
< |
struct AccessItem *aconf; |
181 |
< |
struct ClassItem *aclass; |
165 |
> |
dlink_node *ptr = NULL; |
166 |
|
|
167 |
< |
assert(NULL != client_p); |
167 |
> |
assert(client_p); |
168 |
|
|
169 |
|
if (!IsDead(client_p)) |
170 |
|
{ |
173 |
|
* even if it is marked as blocked (COMM_SELECT_READ handler is called |
174 |
|
* before COMM_SELECT_WRITE). Let's try, nothing to lose.. -adx |
175 |
|
*/ |
192 |
– |
ClearSendqBlocked(client_p); |
176 |
|
send_queued_write(client_p); |
177 |
|
} |
178 |
|
|
190 |
|
ServerStats.is_sbr += client_p->localClient->recv.bytes; |
191 |
|
ServerStats.is_sti += CurrentTime - client_p->localClient->firsttime; |
192 |
|
|
193 |
< |
/* XXX Does this even make any sense at all anymore? |
211 |
< |
* scheduling a 'quick' reconnect could cause a pile of |
212 |
< |
* nick collides under TSora protocol... -db |
213 |
< |
*/ |
214 |
< |
/* |
215 |
< |
* If the connection has been up for a long amount of time, schedule |
216 |
< |
* a 'quick' reconnect, else reset the next-connect cycle. |
217 |
< |
*/ |
218 |
< |
if ((conf = find_conf_exact(SERVER_TYPE, client_p->name, |
219 |
< |
client_p->username, client_p->host))) |
193 |
> |
DLINK_FOREACH(ptr, server_items.head) |
194 |
|
{ |
195 |
+ |
struct MaskItem *conf = ptr->data; |
196 |
+ |
|
197 |
+ |
if (irccmp(conf->name, client_p->name)) |
198 |
+ |
continue; |
199 |
+ |
|
200 |
|
/* |
201 |
< |
* Reschedule a faster reconnect, if this was a automatically |
202 |
< |
* connected configuration entry. (Note that if we have had |
224 |
< |
* a rehash in between, the status has been changed to |
225 |
< |
* CONF_ILLEGAL). But only do this if it was a "good" link. |
201 |
> |
* Reset next-connect cycle of all connect{} blocks that match |
202 |
> |
* this servername. |
203 |
|
*/ |
204 |
< |
aconf = map_to_conf(conf); |
228 |
< |
aclass = map_to_conf(aconf->class_ptr); |
229 |
< |
aconf->hold = time(NULL); |
230 |
< |
aconf->hold += (aconf->hold - client_p->localClient->since > HANGONGOODLINK) ? |
231 |
< |
HANGONRETRYDELAY : ConFreq(aclass); |
204 |
> |
conf->until = CurrentTime + conf->class->con_freq; |
205 |
|
} |
206 |
|
} |
207 |
|
else |
219 |
|
if (client_p->localClient->fd.flags.open) |
220 |
|
fd_close(&client_p->localClient->fd); |
221 |
|
|
249 |
– |
if (HasServlink(client_p)) |
250 |
– |
{ |
251 |
– |
if (client_p->localClient->ctrlfd.flags.open) |
252 |
– |
fd_close(&client_p->localClient->ctrlfd); |
253 |
– |
} |
254 |
– |
|
222 |
|
dbuf_clear(&client_p->localClient->buf_sendq); |
223 |
|
dbuf_clear(&client_p->localClient->buf_recvq); |
224 |
< |
|
224 |
> |
|
225 |
|
MyFree(client_p->localClient->passwd); |
226 |
< |
detach_conf(client_p, CONF_TYPE); |
260 |
< |
client_p->from = NULL; /* ...this should catch them! >:) --msa */ |
226 |
> |
detach_conf(client_p, CONF_CLIENT|CONF_OPER|CONF_SERVER); |
227 |
|
} |
228 |
|
|
229 |
|
#ifdef HAVE_LIBCRYPTO |
234 |
|
static void |
235 |
|
ssl_handshake(int fd, struct Client *client_p) |
236 |
|
{ |
237 |
< |
int ret = SSL_accept(client_p->localClient->fd.ssl); |
237 |
> |
X509 *cert = NULL; |
238 |
> |
int ret = 0; |
239 |
> |
|
240 |
> |
if ((ret = SSL_accept(client_p->localClient->fd.ssl)) <= 0) |
241 |
> |
{ |
242 |
> |
if ((CurrentTime - client_p->localClient->firsttime) > 30) |
243 |
> |
{ |
244 |
> |
exit_client(client_p, "Timeout during SSL handshake"); |
245 |
> |
return; |
246 |
> |
} |
247 |
|
|
273 |
– |
if (ret <= 0) |
248 |
|
switch (SSL_get_error(client_p->localClient->fd.ssl, ret)) |
249 |
|
{ |
250 |
|
case SSL_ERROR_WANT_WRITE: |
251 |
|
comm_setselect(&client_p->localClient->fd, COMM_SELECT_WRITE, |
252 |
< |
(PF *) ssl_handshake, client_p, 0); |
252 |
> |
(PF *)ssl_handshake, client_p, 30); |
253 |
|
return; |
254 |
|
|
255 |
|
case SSL_ERROR_WANT_READ: |
256 |
|
comm_setselect(&client_p->localClient->fd, COMM_SELECT_READ, |
257 |
< |
(PF *) ssl_handshake, client_p, 0); |
257 |
> |
(PF *)ssl_handshake, client_p, 30); |
258 |
|
return; |
259 |
|
|
260 |
|
default: |
261 |
< |
exit_client(client_p, client_p, "Error during SSL handshake"); |
262 |
< |
return; |
261 |
> |
exit_client(client_p, "Error during SSL handshake"); |
262 |
> |
return; |
263 |
> |
} |
264 |
> |
} |
265 |
> |
|
266 |
> |
comm_settimeout(&client_p->localClient->fd, 0, NULL, NULL); |
267 |
> |
|
268 |
> |
if ((cert = SSL_get_peer_certificate(client_p->localClient->fd.ssl))) |
269 |
> |
{ |
270 |
> |
int res = SSL_get_verify_result(client_p->localClient->fd.ssl); |
271 |
> |
char buf[EVP_MAX_MD_SIZE * 2 + 1] = { '\0' }; |
272 |
> |
unsigned char md[EVP_MAX_MD_SIZE] = { '\0' }; |
273 |
> |
|
274 |
> |
if (res == X509_V_OK || res == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN || |
275 |
> |
res == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE || |
276 |
> |
res == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) |
277 |
> |
{ |
278 |
> |
unsigned int i = 0, n = 0; |
279 |
> |
|
280 |
> |
if (X509_digest(cert, EVP_sha256(), md, &n)) |
281 |
> |
{ |
282 |
> |
for (; i < n; ++i) |
283 |
> |
snprintf(buf + 2 * i, 3, "%02X", md[i]); |
284 |
> |
client_p->certfp = xstrdup(buf); |
285 |
> |
} |
286 |
|
} |
287 |
+ |
else |
288 |
+ |
ilog(LOG_TYPE_IRCD, "Client %s!%s@%s gave bad SSL client certificate: %d", |
289 |
+ |
client_p->name, client_p->username, client_p->host, res); |
290 |
+ |
X509_free(cert); |
291 |
+ |
} |
292 |
|
|
293 |
< |
execute_callback(auth_cb, client_p); |
293 |
> |
start_auth(client_p); |
294 |
|
} |
295 |
|
#endif |
296 |
|
|
297 |
|
/* |
298 |
< |
* add_connection - creates a client which has just connected to us on |
298 |
> |
* add_connection - creates a client which has just connected to us on |
299 |
|
* the given fd. The sockhost field is initialized with the ip# of the host. |
300 |
|
* An unique id is calculated now, in case it is needed for auth. |
301 |
|
* The client is sent to the auth module for verification, and not put in |
304 |
|
void |
305 |
|
add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd) |
306 |
|
{ |
307 |
< |
struct Client *new_client = make_client(NULL); |
307 |
> |
struct Client *client_p = make_client(NULL); |
308 |
|
|
309 |
< |
fd_open(&new_client->localClient->fd, fd, 1, |
309 |
> |
fd_open(&client_p->localClient->fd, fd, 1, |
310 |
|
(listener->flags & LISTENER_SSL) ? |
311 |
< |
"Incoming SSL connection" : "Incoming connection"); |
311 |
> |
"Incoming SSL connection" : "Incoming connection"); |
312 |
|
|
313 |
|
/* |
314 |
|
* copy address to 'sockhost' as a string, copy it to host too |
315 |
|
* so we have something valid to put into error messages... |
316 |
|
*/ |
317 |
< |
memcpy(&new_client->localClient->ip, irn, sizeof(struct irc_ssaddr)); |
317 |
> |
memcpy(&client_p->localClient->ip, irn, sizeof(struct irc_ssaddr)); |
318 |
|
|
319 |
< |
getnameinfo((struct sockaddr *)&new_client->localClient->ip, |
320 |
< |
new_client->localClient->ip.ss_len, new_client->sockhost, |
321 |
< |
sizeof(new_client->sockhost), NULL, 0, NI_NUMERICHOST); |
322 |
< |
new_client->localClient->aftype = new_client->localClient->ip.ss.ss_family; |
323 |
< |
|
324 |
< |
if (new_client->sockhost[0] == ':' && new_client->sockhost[1] == ':') |
325 |
< |
{ |
326 |
< |
strlcpy(new_client->host, "0", sizeof(new_client->host)); |
327 |
< |
strlcpy(new_client->host+1, new_client->sockhost, sizeof(new_client->host)-1); |
328 |
< |
memmove(new_client->sockhost+1, new_client->sockhost, sizeof(new_client->sockhost)-1); |
329 |
< |
new_client->sockhost[0] = '0'; |
319 |
> |
getnameinfo((struct sockaddr *)&client_p->localClient->ip, |
320 |
> |
client_p->localClient->ip.ss_len, client_p->sockhost, |
321 |
> |
sizeof(client_p->sockhost), NULL, 0, NI_NUMERICHOST); |
322 |
> |
client_p->localClient->aftype = client_p->localClient->ip.ss.ss_family; |
323 |
> |
|
324 |
> |
#ifdef HAVE_LIBGEOIP |
325 |
> |
/* XXX IPV6 SUPPORT XXX */ |
326 |
> |
if (irn->ss.ss_family == AF_INET && geoip_ctx) |
327 |
> |
{ |
328 |
> |
const struct sockaddr_in *v4 = (const struct sockaddr_in *)&client_p->localClient->ip; |
329 |
> |
client_p->localClient->country_id = GeoIP_id_by_ipnum(geoip_ctx, (unsigned long)ntohl(v4->sin_addr.s_addr)); |
330 |
> |
} |
331 |
> |
#endif |
332 |
> |
|
333 |
> |
if (client_p->sockhost[0] == ':' && client_p->sockhost[1] == ':') |
334 |
> |
{ |
335 |
> |
strlcpy(client_p->host, "0", sizeof(client_p->host)); |
336 |
> |
strlcpy(client_p->host+1, client_p->sockhost, sizeof(client_p->host)-1); |
337 |
> |
memmove(client_p->sockhost+1, client_p->sockhost, sizeof(client_p->sockhost)-1); |
338 |
> |
client_p->sockhost[0] = '0'; |
339 |
|
} |
340 |
|
else |
341 |
< |
strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host)); |
341 |
> |
strlcpy(client_p->host, client_p->sockhost, sizeof(client_p->host)); |
342 |
|
|
343 |
< |
new_client->localClient->listener = listener; |
343 |
> |
client_p->localClient->listener = listener; |
344 |
|
++listener->ref_count; |
345 |
|
|
346 |
|
#ifdef HAVE_LIBCRYPTO |
347 |
|
if (listener->flags & LISTENER_SSL) |
348 |
|
{ |
349 |
< |
if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.server_ctx)) == NULL) |
349 |
> |
if ((client_p->localClient->fd.ssl = SSL_new(ServerInfo.server_ctx)) == NULL) |
350 |
|
{ |
351 |
|
ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s", |
352 |
|
ERR_error_string(ERR_get_error(), NULL)); |
353 |
|
|
354 |
< |
SetDead(new_client); |
355 |
< |
exit_client(new_client, new_client, "SSL_new failed"); |
354 |
> |
SetDead(client_p); |
355 |
> |
exit_client(client_p, "SSL_new failed"); |
356 |
|
return; |
357 |
|
} |
358 |
|
|
359 |
< |
SSL_set_fd(new_client->localClient->fd.ssl, fd); |
360 |
< |
ssl_handshake(0, new_client); |
359 |
> |
AddFlag(client_p, FLAGS_SSL); |
360 |
> |
SSL_set_fd(client_p->localClient->fd.ssl, fd); |
361 |
> |
ssl_handshake(0, client_p); |
362 |
|
} |
363 |
|
else |
364 |
|
#endif |
365 |
< |
execute_callback(auth_cb, new_client); |
365 |
> |
start_auth(client_p); |
366 |
|
} |
367 |
|
|
368 |
|
/* |
417 |
|
* flush functions, and when comm_close() is implemented correctly |
418 |
|
* with close functions, we _actually_ don't call comm_close() here .. |
419 |
|
* -- originally Adrian's notes |
420 |
< |
* comm_close() is replaced with fd_close() in fdlist.c |
420 |
> |
* comm_close() is replaced with fd_close() in fdlist.c |
421 |
|
*/ |
422 |
|
void |
423 |
|
comm_setflush(fde_t *fd, time_t timeout, PF *callback, void *cbdata) |
509 |
|
* -- adrian |
510 |
|
*/ |
511 |
|
if ((clocal != NULL) && (bind(fd->fd, clocal, socklen) < 0)) |
512 |
< |
{ |
512 |
> |
{ |
513 |
|
/* Failure, call the callback with COMM_ERR_BIND */ |
514 |
|
comm_connect_callback(fd, COMM_ERR_BIND); |
515 |
|
/* ... and quit */ |
606 |
|
/* Copy over the DNS reply info so we can use it in the connect() */ |
607 |
|
/* |
608 |
|
* Note we don't fudge the refcount here, because we aren't keeping |
609 |
< |
* the DNS record around, and the DNS cache is gone anyway.. |
609 |
> |
* the DNS record around, and the DNS cache is gone anyway.. |
610 |
|
* -- adrian |
611 |
|
*/ |
612 |
|
memcpy(&F->connect.hostaddr, addr, addr->ss_len); |
637 |
|
return; |
638 |
|
|
639 |
|
/* Try the connect() */ |
640 |
< |
retval = connect(fd->fd, (struct sockaddr *) &fd->connect.hostaddr, |
640 |
> |
retval = connect(fd->fd, (struct sockaddr *) &fd->connect.hostaddr, |
641 |
|
fd->connect.hostaddr.ss_len); |
642 |
|
|
643 |
|
/* Error? */ |
703 |
|
if (fd < 0) |
704 |
|
return -1; /* errno will be passed through, yay.. */ |
705 |
|
|
706 |
< |
execute_callback(setup_socket_cb, fd); |
706 |
> |
setup_socket(fd); |
707 |
|
|
708 |
|
/* update things in our fd tracking */ |
709 |
|
fd_open(F, fd, 1, note); |
744 |
|
pn->ss_len = addrlen; |
745 |
|
#endif |
746 |
|
|
747 |
< |
execute_callback(setup_socket_cb, newfd); |
747 |
> |
setup_socket(newfd); |
748 |
|
|
749 |
|
/* .. and return */ |
750 |
|
return newfd; |
751 |
|
} |
752 |
|
|
753 |
< |
/* |
753 |
> |
/* |
754 |
|
* remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address |
755 |
|
* OSes with IPv6 mapping listening on both |
756 |
|
* AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures |
757 |
< |
* |
757 |
> |
* |
758 |
|
*/ |
759 |
|
#ifdef IPV6 |
760 |
|
void |
764 |
|
{ |
765 |
|
if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr)) |
766 |
|
{ |
767 |
< |
struct sockaddr_in6 v6; |
767 |
> |
struct sockaddr_in6 v6; |
768 |
|
struct sockaddr_in *v4 = (struct sockaddr_in *)addr; |
769 |
|
|
770 |
|
memcpy(&v6, addr, sizeof(v6)); |
774 |
|
addr->ss.ss_family = AF_INET; |
775 |
|
addr->ss_len = sizeof(struct sockaddr_in); |
776 |
|
} |
777 |
< |
else |
777 |
> |
else |
778 |
|
addr->ss_len = sizeof(struct sockaddr_in6); |
779 |
|
} |
780 |
|
else |
781 |
|
addr->ss_len = sizeof(struct sockaddr_in); |
782 |
< |
} |
782 |
> |
} |
783 |
|
#endif |