Files
palera1n/src/exec_checkra1n.c
T

85 lines
2.2 KiB
C
Raw Normal View History

2023-01-16 17:33:12 +08:00
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <stddef.h>
#include <stdbool.h>
#include <unistd.h>
#include <spawn.h>
#include <ctype.h>
#include <errno.h>
2023-01-22 19:18:35 +08:00
#include <limits.h>
2023-01-16 17:33:12 +08:00
2023-01-24 14:36:49 +08:00
#include <sys/types.h>
#include <sys/wait.h>
2023-01-16 17:33:12 +08:00
#include <sys/stat.h>
extern char **environ;
2023-02-06 01:03:29 +08:00
#include <palerain.h>
2023-01-28 20:54:06 +08:00
#include <xxd-embedded.h>
2023-01-22 19:18:35 +08:00
char* pongo_path = NULL;
2023-02-03 13:22:07 +08:00
char* ext_checkra1n = NULL;
2023-01-16 17:33:12 +08:00
2023-02-04 21:47:53 +08:00
#define tmpdir getenv("TMPDIR") == NULL ? "/tmp" : getenv("TMPDIR")
2023-02-03 13:22:07 +08:00
int exec_checkra1n() {
2023-01-16 17:33:12 +08:00
LOG(LOG_INFO, "About to execute checkra1n");
int fd, ret;
2023-02-03 13:22:07 +08:00
char* checkra1n_path = NULL;
if (ext_checkra1n != NULL) {
checkra1n_path = ext_checkra1n;
goto checkra1n_exec;
}
2023-02-04 21:47:53 +08:00
checkra1n_path = malloc(strlen(tmpdir) + 20);
snprintf(checkra1n_path, strlen(tmpdir) + 20, "%s/checkra1n.XXXXXX", tmpdir);
2023-01-16 17:33:12 +08:00
fd = mkstemp(checkra1n_path);
if (fd == -1) {
LOG(LOG_FATAL, "Cannot open temporary file: %d (%s)", errno, strerror(errno));
2023-02-03 13:22:07 +08:00
return -1;
2023-01-16 17:33:12 +08:00
}
ssize_t didWrite = write(fd, checkra1n, checkra1n_len);
2023-01-24 14:36:49 +08:00
if (didWrite != (ssize_t)checkra1n_len) {
2023-01-16 17:33:12 +08:00
LOG(LOG_FATAL, "Size written does not match expected: %lld != %d: %d (%s)", didWrite, checkra1n_len, errno, strerror(errno));
close(fd);
unlink(checkra1n_path);
2023-02-03 13:22:07 +08:00
return -1;
2023-01-16 17:33:12 +08:00
}
close(fd);
ret = chmod(checkra1n_path, 0700);
if (ret) {
LOG(LOG_FATAL, "Cannot chmod %s: %d (%s)", checkra1n_path, errno, strerror(errno));
unlink(checkra1n_path);
2023-02-03 13:22:07 +08:00
return -1;
2023-01-16 17:33:12 +08:00
}
2023-02-03 13:22:07 +08:00
checkra1n_exec: {};
char args[0x10] = "-pE";
2023-01-30 22:16:15 +08:00
if (demote) strncat(args, "d", 0xf);
if (verbose >= 2) strncat(args, "v", 0xf);
if (pongo_path != NULL) strncat(args, "k", 0xf); // keep this at last
2023-01-17 20:48:54 +08:00
pid_t pid;
2023-01-16 18:06:23 +08:00
char* checkra1n_argv[] = {
2023-01-16 17:33:12 +08:00
checkra1n_path,
2023-01-17 20:48:54 +08:00
args,
2023-01-22 19:18:35 +08:00
pongo_path,
2023-01-16 17:33:12 +08:00
NULL
};
2023-01-16 18:06:23 +08:00
ret = posix_spawn(&pid, checkra1n_path, NULL, NULL, checkra1n_argv, environ);
2023-01-22 19:18:35 +08:00
if (pongo_path != NULL) free(pongo_path);
pongo_path = NULL;
2023-01-16 17:33:12 +08:00
if (ret) {
LOG(LOG_FATAL, "Cannot posix spawn %s: %d (%s)", checkra1n_path, errno, strerror(errno));
2023-02-03 13:22:07 +08:00
if (ext_checkra1n != NULL) unlink(checkra1n_path);
return -1;
2023-01-16 17:33:12 +08:00
}
2023-01-22 19:18:35 +08:00
LOG(LOG_VERBOSE2, "%s spawned successfully", checkra1n_path);
2023-01-22 02:37:44 +08:00
sleep(2);
2023-02-04 21:47:53 +08:00
if (ext_checkra1n == NULL) {
2023-02-06 11:27:26 +08:00
unlink(checkra1n_path);
2023-02-03 14:42:41 +08:00
free(checkra1n_path);
checkra1n_path = NULL;
}
2023-01-16 17:33:12 +08:00
waitpid(pid, NULL, 0);
2023-02-03 13:22:07 +08:00
return 0;
2023-01-16 17:33:12 +08:00
}