1 |
|
/* |
2 |
|
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
|
* |
4 |
< |
* Copyright (c) 1997-2015 ircd-hybrid development team |
4 |
> |
* Copyright (c) 1997-2016 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 |
25 |
|
*/ |
26 |
|
|
27 |
|
#include "stdinc.h" |
28 |
– |
#ifdef HAVE_LIBCRYPTO |
29 |
– |
#include <openssl/rsa.h> |
30 |
– |
#include "rsa.h" |
31 |
– |
#endif |
28 |
|
#include "list.h" |
29 |
|
#include "client.h" |
30 |
|
#include "event.h" |
33 |
|
#include "ircd.h" |
34 |
|
#include "ircd_defs.h" |
35 |
|
#include "s_bsd.h" |
40 |
– |
#include "numeric.h" |
36 |
|
#include "packet.h" |
37 |
|
#include "conf.h" |
38 |
|
#include "server.h" |
39 |
|
#include "log.h" |
45 |
– |
#include "user.h" |
40 |
|
#include "send.h" |
41 |
|
#include "memory.h" |
48 |
– |
#include "channel.h" |
42 |
|
#include "parse.h" |
43 |
|
|
44 |
|
|
75 |
|
DLINK_FOREACH_SAFE(node, node_next, flatten_links.head) |
76 |
|
{ |
77 |
|
dlinkDelete(node, &flatten_links); |
78 |
< |
MyFree(node->data); |
78 |
> |
xfree(node->data); |
79 |
|
free_dlink_node(node); |
80 |
|
} |
81 |
|
|
249 |
|
{ |
250 |
|
dlink_node *node = NULL; |
251 |
|
|
259 |
– |
/* TODO: change this to set active flag to 0 when added to event! --Habeeb */ |
252 |
|
if (GlobalSetOptions.autoconn == 0) |
253 |
|
return; |
254 |
|
|
255 |
< |
DLINK_FOREACH(node, server_items.head) |
255 |
> |
DLINK_FOREACH(node, connect_items.head) |
256 |
|
{ |
257 |
|
struct MaskItem *conf = node->data; |
258 |
|
|
259 |
|
assert(conf->type == CONF_SERVER); |
260 |
|
|
261 |
< |
/* Also when already connecting! (update holdtimes) --SRB |
270 |
< |
*/ |
261 |
> |
/* Also when already connecting! (update holdtimes) --SRB */ |
262 |
|
if (!conf->port || !IsConfAllowAutoConn(conf)) |
263 |
|
continue; |
264 |
|
|
265 |
< |
|
266 |
< |
/* Skip this entry if the use of it is still on hold until |
265 |
> |
/* |
266 |
> |
* Skip this entry if the use of it is still on hold until |
267 |
|
* future. Otherwise handle this entry (and set it on hold |
268 |
|
* until next time). Will reset only hold times, if already |
269 |
|
* made one successfull connection... [this algorithm is |
288 |
|
/* Move this entry to the end of the list, if not already last */ |
289 |
|
if (node->next) |
290 |
|
{ |
291 |
< |
dlinkDelete(node, &server_items); |
292 |
< |
dlinkAddTail(conf, &conf->node, &server_items); |
291 |
> |
dlinkDelete(node, &connect_items); |
292 |
> |
dlinkAddTail(conf, &conf->node, &connect_items); |
293 |
|
} |
294 |
|
|
295 |
|
if (find_servconn_in_progress(conf->name)) |
342 |
|
check_server(const char *name, struct Client *client_p) |
343 |
|
{ |
344 |
|
dlink_node *node = NULL; |
354 |
– |
struct MaskItem *conf = NULL; |
345 |
|
struct MaskItem *server_conf = NULL; |
346 |
|
int error = -1; |
347 |
|
|
348 |
|
assert(client_p); |
349 |
|
|
350 |
|
/* Loop through looking for all possible connect items that might work */ |
351 |
< |
DLINK_FOREACH(node, server_items.head) |
351 |
> |
DLINK_FOREACH(node, connect_items.head) |
352 |
|
{ |
353 |
< |
conf = node->data; |
353 |
> |
struct MaskItem *conf = node->data; |
354 |
|
|
355 |
< |
if (match(name, conf->name)) |
355 |
> |
if (irccmp(name, conf->name)) |
356 |
|
continue; |
357 |
|
|
358 |
|
error = -3; |
359 |
|
|
360 |
< |
/* XXX: Fix me for IPv6 */ |
361 |
< |
/* XXX sockhost is the IPv4 ip as a string */ |
372 |
< |
if (!match(conf->host, client_p->host) || |
373 |
< |
!match(conf->host, client_p->sockhost)) |
360 |
> |
if (!irccmp(conf->host, client_p->host) || |
361 |
> |
!irccmp(conf->host, client_p->sockhost)) |
362 |
|
{ |
363 |
|
error = -2; |
364 |
|
|
401 |
|
return 0; |
402 |
|
} |
403 |
|
|
404 |
+ |
/* server_capab_init() |
405 |
+ |
* |
406 |
+ |
* inputs - none |
407 |
+ |
* output - none |
408 |
+ |
*/ |
409 |
+ |
void |
410 |
+ |
server_capab_init(void) |
411 |
+ |
{ |
412 |
+ |
add_capability("QS", CAPAB_QS); |
413 |
+ |
add_capability("EOB", CAPAB_EOB); |
414 |
+ |
add_capability("CLUSTER", CAPAB_CLUSTER); |
415 |
+ |
add_capability("SVS", CAPAB_SVS); |
416 |
+ |
add_capability("CHW", CAPAB_CHW); |
417 |
+ |
add_capability("HOPS", CAPAB_HOPS); |
418 |
+ |
} |
419 |
+ |
|
420 |
|
/* add_capability() |
421 |
|
* |
422 |
|
* inputs - string name of CAPAB |
429 |
|
void |
430 |
|
add_capability(const char *name, unsigned int flag) |
431 |
|
{ |
432 |
< |
struct Capability *cap = MyCalloc(sizeof(*cap)); |
432 |
> |
struct Capability *cap = xcalloc(sizeof(*cap)); |
433 |
|
|
434 |
|
cap->name = xstrdup(name); |
435 |
|
cap->cap = flag; |
454 |
|
if (!irccmp(cap->name, name)) |
455 |
|
{ |
456 |
|
dlinkDelete(node, &server_capabilities_list); |
457 |
< |
MyFree(cap->name); |
458 |
< |
MyFree(cap); |
457 |
> |
xfree(cap->name); |
458 |
> |
xfree(cap); |
459 |
|
} |
460 |
|
} |
461 |
|
} |
470 |
|
unsigned int |
471 |
|
find_capability(const char *name) |
472 |
|
{ |
473 |
< |
const dlink_node *node = NULL; |
473 |
> |
dlink_node *node; |
474 |
|
|
475 |
|
DLINK_FOREACH(node, server_capabilities_list.head) |
476 |
|
{ |
483 |
|
return 0; |
484 |
|
} |
485 |
|
|
482 |
– |
/* send_capabilities() |
483 |
– |
* |
484 |
– |
* inputs - Client pointer to send to |
485 |
– |
* - int flag of capabilities that this server can send |
486 |
– |
* output - NONE |
487 |
– |
* side effects - send the CAPAB line to a server -orabidoo |
488 |
– |
* |
489 |
– |
*/ |
490 |
– |
void |
491 |
– |
send_capabilities(struct Client *client_p) |
492 |
– |
{ |
493 |
– |
char buf[IRCD_BUFSIZE] = ""; |
494 |
– |
const dlink_node *node = NULL; |
495 |
– |
|
496 |
– |
DLINK_FOREACH(node, server_capabilities_list.head) |
497 |
– |
{ |
498 |
– |
const struct Capability *cap = node->data; |
499 |
– |
|
500 |
– |
strlcat(buf, cap->name, sizeof(buf)); |
501 |
– |
|
502 |
– |
if (node->next) |
503 |
– |
strlcat(buf, " ", sizeof(buf)); |
504 |
– |
} |
505 |
– |
|
506 |
– |
sendto_one(client_p, "CAPAB :%s", buf); |
507 |
– |
} |
508 |
– |
|
486 |
|
/* |
487 |
|
* show_capabilities - show current server capabilities |
488 |
|
* |
491 |
|
* side effects - build up string representing capabilities of server listed |
492 |
|
*/ |
493 |
|
const char * |
494 |
< |
show_capabilities(const struct Client *target_p) |
494 |
> |
get_capabilities(const struct Client *client_p) |
495 |
|
{ |
496 |
< |
static char msgbuf[IRCD_BUFSIZE] = ""; |
497 |
< |
const dlink_node *node = NULL; |
496 |
> |
static char buf[IRCD_BUFSIZE] = ""; |
497 |
> |
dlink_node *node; |
498 |
|
|
499 |
< |
strlcpy(msgbuf, "TS", sizeof(msgbuf)); |
499 |
> |
buf[0] = '\0'; |
500 |
|
|
501 |
|
DLINK_FOREACH(node, server_capabilities_list.head) |
502 |
|
{ |
503 |
|
const struct Capability *cap = node->data; |
504 |
|
|
505 |
< |
if (!IsCapable(target_p, cap->cap)) |
505 |
> |
if (client_p && !IsCapable(client_p, cap->cap)) |
506 |
|
continue; |
507 |
|
|
508 |
< |
strlcat(msgbuf, " ", sizeof(msgbuf)); |
509 |
< |
strlcat(msgbuf, cap->name, sizeof(msgbuf)); |
508 |
> |
strlcat(buf, cap->name, sizeof(buf)); |
509 |
> |
|
510 |
> |
if (node->next) |
511 |
> |
strlcat(buf, " ", sizeof(buf)); |
512 |
|
} |
513 |
|
|
514 |
< |
return msgbuf; |
514 |
> |
return buf; |
515 |
|
} |
516 |
|
|
517 |
|
/* make_server() |
525 |
|
make_server(struct Client *client_p) |
526 |
|
{ |
527 |
|
if (client_p->serv == NULL) |
528 |
< |
client_p->serv = MyCalloc(sizeof(struct Server)); |
528 |
> |
client_p->serv = xcalloc(sizeof(struct Server)); |
529 |
|
|
530 |
|
return client_p->serv; |
531 |
|
} |
585 |
|
return 0; |
586 |
|
} |
587 |
|
|
588 |
< |
/* Make sure this server isn't already connected |
589 |
< |
* Note: conf should ALWAYS be a valid C: line |
588 |
> |
/* |
589 |
> |
* Make sure this server isn't already connected. |
590 |
> |
* Note: conf should ALWAYS be a valid connect {} block |
591 |
|
*/ |
592 |
|
if ((client_p = hash_find_server(conf->name))) |
593 |
|
{ |
613 |
|
/* We already converted the ip once, so lets use it - stu */ |
614 |
|
strlcpy(client_p->sockhost, buf, sizeof(client_p->sockhost)); |
615 |
|
|
616 |
< |
/* create a socket for the server connection */ |
616 |
> |
/* Create a socket for the server connection */ |
617 |
|
if (comm_open(&client_p->connection->fd, conf->addr.ss.ss_family, SOCK_STREAM, 0, NULL) < 0) |
618 |
|
{ |
619 |
|
/* Eek, failure to create the socket */ |
624 |
|
return 0; |
625 |
|
} |
626 |
|
|
627 |
< |
/* servernames are always guaranteed under HOSTLEN chars */ |
628 |
< |
fd_note(&client_p->connection->fd, "Server: %s", conf->name); |
627 |
> |
/* Server names are always guaranteed under HOSTLEN chars */ |
628 |
> |
fd_note(&client_p->connection->fd, "Server: %s", client_p->name); |
629 |
|
|
630 |
< |
/* Attach config entries to client here rather than in |
631 |
< |
* serv_connect_callback(). This to avoid null pointer references. |
630 |
> |
/* |
631 |
> |
* Attach config entries to client here rather than in serv_connect_callback(). |
632 |
> |
* This to avoid null pointer references. |
633 |
|
*/ |
634 |
|
if (!attach_connect_block(client_p, conf->name, conf->host)) |
635 |
|
{ |
644 |
|
return 0; |
645 |
|
} |
646 |
|
|
647 |
< |
/* at this point we have a connection in progress and C/N lines |
648 |
< |
* attached to the client, the socket info should be saved in the |
649 |
< |
* client and it should either be resolved or have a valid address. |
647 |
> |
/* |
648 |
> |
* At this point we have a connection in progress and a connect {} block |
649 |
> |
* attached to the client, the socket info should be saved in the client |
650 |
> |
* and it should either be resolved or have a valid address. |
651 |
|
* |
652 |
|
* The socket has been connected or connect is in progress. |
653 |
|
*/ |
741 |
|
return 1; |
742 |
|
} |
743 |
|
|
762 |
– |
#ifdef HAVE_LIBCRYPTO |
744 |
|
static void |
745 |
|
finish_ssl_server_handshake(struct Client *client_p) |
746 |
|
{ |
747 |
< |
struct MaskItem *conf = NULL; |
748 |
< |
|
749 |
< |
conf = find_conf_name(&client_p->connection->confs, |
769 |
< |
client_p->name, CONF_SERVER); |
770 |
< |
if (conf == NULL) |
747 |
> |
const struct MaskItem *conf = find_conf_name(&client_p->connection->confs, |
748 |
> |
client_p->name, CONF_SERVER); |
749 |
> |
if (!conf) |
750 |
|
{ |
751 |
|
sendto_realops_flags(UMODE_SERVNOTICE, L_ADMIN, SEND_NOTICE, |
752 |
|
"Lost connect{} block for %s", get_client_name(client_p, SHOW_IP)); |
757 |
|
return; |
758 |
|
} |
759 |
|
|
760 |
< |
sendto_one(client_p, "PASS %s TS %d %s", conf->spasswd, TS_CURRENT, me.id); |
760 |
> |
sendto_one(client_p, "PASS %s TS %u %s", conf->spasswd, TS_CURRENT, me.id); |
761 |
|
|
762 |
< |
send_capabilities(client_p); |
762 |
> |
sendto_one(client_p, "CAPAB :%s", get_capabilities(NULL)); |
763 |
|
|
764 |
|
sendto_one(client_p, "SERVER %s 1 :%s%s", |
765 |
|
me.name, ConfigServerHide.hidden ? "(H) " : "", |
787 |
|
ssl_server_handshake(fde_t *fd, void *data) |
788 |
|
{ |
789 |
|
struct Client *client_p = data; |
790 |
< |
X509 *cert = NULL; |
812 |
< |
int ret = 0; |
790 |
> |
const char *sslerr = NULL; |
791 |
|
|
792 |
< |
if ((ret = SSL_connect(client_p->connection->fd.ssl)) <= 0) |
792 |
> |
tls_handshake_status_t ret = tls_handshake(&client_p->connection->fd.ssl, TLS_ROLE_CLIENT, &sslerr); |
793 |
> |
if (ret != TLS_HANDSHAKE_DONE) |
794 |
|
{ |
795 |
|
if ((CurrentTime - client_p->connection->firsttime) > CONNECTTIMEOUT) |
796 |
|
{ |
797 |
< |
exit_client(client_p, "Timeout during SSL handshake"); |
797 |
> |
exit_client(client_p, "Timeout during TLS handshake"); |
798 |
|
return; |
799 |
|
} |
800 |
|
|
801 |
< |
switch (SSL_get_error(client_p->connection->fd.ssl, ret)) |
801 |
> |
switch (ret) |
802 |
|
{ |
803 |
< |
case SSL_ERROR_WANT_WRITE: |
803 |
> |
case TLS_HANDSHAKE_WANT_WRITE: |
804 |
|
comm_setselect(&client_p->connection->fd, COMM_SELECT_WRITE, |
805 |
|
ssl_server_handshake, client_p, CONNECTTIMEOUT); |
806 |
|
return; |
807 |
< |
case SSL_ERROR_WANT_READ: |
807 |
> |
case TLS_HANDSHAKE_WANT_READ: |
808 |
|
comm_setselect(&client_p->connection->fd, COMM_SELECT_READ, |
809 |
|
ssl_server_handshake, client_p, CONNECTTIMEOUT); |
810 |
|
return; |
811 |
|
default: |
812 |
|
{ |
834 |
– |
const char *sslerr = ERR_error_string(ERR_get_error(), NULL); |
813 |
|
sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE, |
814 |
|
"Error connecting to %s: %s", client_p->name, |
815 |
< |
sslerr ? sslerr : "unknown SSL error"); |
816 |
< |
exit_client(client_p, "Error during SSL handshake"); |
815 |
> |
sslerr ? sslerr : "unknown TLS error"); |
816 |
> |
exit_client(client_p, "Error during TLS handshake"); |
817 |
|
return; |
818 |
|
} |
819 |
|
} |
821 |
|
|
822 |
|
comm_settimeout(&client_p->connection->fd, 0, NULL, NULL); |
823 |
|
|
824 |
< |
if ((cert = SSL_get_peer_certificate(client_p->connection->fd.ssl))) |
825 |
< |
{ |
826 |
< |
int res = SSL_get_verify_result(client_p->connection->fd.ssl); |
849 |
< |
char buf[EVP_MAX_MD_SIZE * 2 + 1] = ""; |
850 |
< |
unsigned char md[EVP_MAX_MD_SIZE] = ""; |
851 |
< |
|
852 |
< |
if (res == X509_V_OK || res == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN || |
853 |
< |
res == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE || |
854 |
< |
res == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) |
855 |
< |
{ |
856 |
< |
unsigned int n = 0; |
857 |
< |
|
858 |
< |
if (X509_digest(cert, ConfigServerInfo.message_digest_algorithm, md, &n)) |
859 |
< |
{ |
860 |
< |
binary_to_hex(md, buf, n); |
861 |
< |
client_p->certfp = xstrdup(buf); |
862 |
< |
} |
863 |
< |
} |
864 |
< |
else |
865 |
< |
ilog(LOG_TYPE_IRCD, "Server %s!%s@%s gave bad SSL client certificate: %d", |
866 |
< |
client_p->name, client_p->username, client_p->host, res); |
867 |
< |
X509_free(cert); |
868 |
< |
} |
824 |
> |
if (!tls_verify_cert(&client_p->connection->fd.ssl, ConfigServerInfo.message_digest_algorithm, &client_p->certfp)) |
825 |
> |
ilog(LOG_TYPE_IRCD, "Server %s!%s@%s gave bad TLS client certificate", |
826 |
> |
client_p->name, client_p->username, client_p->host); |
827 |
|
|
828 |
|
finish_ssl_server_handshake(client_p); |
829 |
|
} |
831 |
|
static void |
832 |
|
ssl_connect_init(struct Client *client_p, const struct MaskItem *conf, fde_t *fd) |
833 |
|
{ |
834 |
< |
if ((client_p->connection->fd.ssl = SSL_new(ConfigServerInfo.client_ctx)) == NULL) |
834 |
> |
if (!tls_new(&client_p->connection->fd.ssl, fd->fd, TLS_ROLE_CLIENT)) |
835 |
|
{ |
878 |
– |
ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s", |
879 |
– |
ERR_error_string(ERR_get_error(), NULL)); |
836 |
|
SetDead(client_p); |
837 |
< |
exit_client(client_p, "SSL_new failed"); |
837 |
> |
exit_client(client_p, "TLS context initialization failed"); |
838 |
|
return; |
839 |
|
} |
840 |
|
|
885 |
– |
SSL_set_fd(fd->ssl, fd->fd); |
886 |
– |
|
841 |
|
if (!EmptyString(conf->cipher_list)) |
842 |
< |
SSL_set_cipher_list(client_p->connection->fd.ssl, conf->cipher_list); |
842 |
> |
tls_set_ciphers(&client_p->connection->fd.ssl, conf->cipher_list); |
843 |
|
|
844 |
|
ssl_server_handshake(NULL, client_p); |
845 |
|
} |
892 |
– |
#endif |
846 |
|
|
847 |
|
/* serv_connect_callback() - complete a server connection. |
848 |
|
* |
856 |
|
serv_connect_callback(fde_t *fd, int status, void *data) |
857 |
|
{ |
858 |
|
struct Client *const client_p = data; |
906 |
– |
const struct MaskItem *conf = NULL; |
859 |
|
|
860 |
|
/* First, make sure it's a real client! */ |
861 |
|
assert(client_p); |
862 |
|
assert(&client_p->connection->fd == fd); |
863 |
|
|
864 |
|
/* Next, for backward purposes, record the ip of the server */ |
865 |
< |
memcpy(&client_p->connection->ip, &fd->connect.hostaddr, |
914 |
< |
sizeof(struct irc_ssaddr)); |
865 |
> |
memcpy(&client_p->connection->ip, &fd->connect.hostaddr, sizeof(struct irc_ssaddr)); |
866 |
|
|
867 |
|
/* Check the status */ |
868 |
|
if (status != COMM_OK) |
884 |
|
} |
885 |
|
|
886 |
|
/* COMM_OK, so continue the connection procedure */ |
887 |
< |
/* Get the C/N lines */ |
888 |
< |
conf = find_conf_name(&client_p->connection->confs, |
889 |
< |
client_p->name, CONF_SERVER); |
890 |
< |
if (conf == NULL) |
887 |
> |
/* Get the connect {} block */ |
888 |
> |
const struct MaskItem *conf = find_conf_name(&client_p->connection->confs, |
889 |
> |
client_p->name, CONF_SERVER); |
890 |
> |
if (!conf) |
891 |
|
{ |
892 |
|
sendto_realops_flags(UMODE_SERVNOTICE, L_ADMIN, SEND_NOTICE, |
893 |
|
"Lost connect{} block for %s", get_client_name(client_p, SHOW_IP)); |
901 |
|
/* Next, send the initial handshake */ |
902 |
|
SetHandshake(client_p); |
903 |
|
|
953 |
– |
#ifdef HAVE_LIBCRYPTO |
904 |
|
if (IsConfSSL(conf)) |
905 |
|
{ |
906 |
|
ssl_connect_init(client_p, conf, fd); |
907 |
|
return; |
908 |
|
} |
959 |
– |
#endif |
909 |
|
|
910 |
< |
sendto_one(client_p, "PASS %s TS %d %s", conf->spasswd, TS_CURRENT, me.id); |
910 |
> |
sendto_one(client_p, "PASS %s TS %u %s", conf->spasswd, TS_CURRENT, me.id); |
911 |
|
|
912 |
< |
send_capabilities(client_p); |
912 |
> |
sendto_one(client_p, "CAPAB :%s", get_capabilities(NULL)); |
913 |
|
|
914 |
|
sendto_one(client_p, "SERVER %s 1 :%s%s", me.name, |
915 |
|
ConfigServerHide.hidden ? "(H) " : "", me.info); |
936 |
|
find_servconn_in_progress(const char *name) |
937 |
|
{ |
938 |
|
dlink_node *ptr; |
990 |
– |
struct Client *cptr; |
939 |
|
|
940 |
|
DLINK_FOREACH(ptr, unknown_list.head) |
941 |
|
{ |
942 |
< |
cptr = ptr->data; |
942 |
> |
struct Client *cptr = ptr->data; |
943 |
|
|
944 |
< |
if (cptr && cptr->name[0]) |
945 |
< |
if (!match(name, cptr->name)) |
944 |
> |
if (cptr->name[0]) |
945 |
> |
if (!irccmp(name, cptr->name)) |
946 |
|
return cptr; |
947 |
|
} |
948 |
|
|