From 98a15fa859157fb6fd6540329a204f3cf0629d64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89rico=20Rolim?= Date: Sat, 25 Jul 2020 01:34:36 -0300 Subject: [PATCH] sourcecode: create enter-chroot. Written in C, is a setid program and does the job of launch-chroot, which is being retired. --- linux-utils/.local/bin/launch-chroot | 14 --- sourcecode/.gitignore | 2 + sourcecode/enter-chroot/enter-chroot.c | 113 +++++++++++++++++++++++++ sourcecode/enter-chroot/install.sh | 8 ++ sourcecode/sshfs-map/.gitignore | 1 - 5 files changed, 123 insertions(+), 15 deletions(-) delete mode 100755 linux-utils/.local/bin/launch-chroot create mode 100644 sourcecode/.gitignore create mode 100644 sourcecode/enter-chroot/enter-chroot.c create mode 100755 sourcecode/enter-chroot/install.sh delete mode 100644 sourcecode/sshfs-map/.gitignore diff --git a/linux-utils/.local/bin/launch-chroot b/linux-utils/.local/bin/launch-chroot deleted file mode 100755 index bc004b7..0000000 --- a/linux-utils/.local/bin/launch-chroot +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/sh - -# use with sudo -E - -ROOTDIR="${1:-/mnt}" -USERNAME="${SUDO_USER:-ericonr}" - -chroot "$ROOTDIR" /bin/sudo -u $USERNAME \ - env CHROOTED=1 \ - XDG_RUNTIME_DIR=$XDG_RUNTIME_DIR \ - WAYLAND_DISPLAY=$WAYLAND_DISPLAY \ - DISPLAY=$DISPLAY \ - DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS \ - /bin/fish -C "cd" diff --git a/sourcecode/.gitignore b/sourcecode/.gitignore new file mode 100644 index 0000000..312b172 --- /dev/null +++ b/sourcecode/.gitignore @@ -0,0 +1,2 @@ +sshfs-map/sshfs-map +enter-chroot/enter-chroot diff --git a/sourcecode/enter-chroot/enter-chroot.c b/sourcecode/enter-chroot/enter-chroot.c new file mode 100644 index 0000000..4675c07 --- /dev/null +++ b/sourcecode/enter-chroot/enter-chroot.c @@ -0,0 +1,113 @@ +#define _XOPEN_SOURCE +#define _BSD_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char * const argv[]) +{ + char *mntpoint = "/mnt", *shell = "/bin/fish"; + bool check_chrooted = true; + int c, rv; + + while ((c = getopt(argc, argv, "m:s:c")) != -1) { + switch (c) { + case 'm': + mntpoint = optarg; + break; + case 's': + shell = optarg; + break; + case 'c': + check_chrooted = false; + break; + default: + puts("wrong usage"); + exit(EXIT_FAILURE); + } + } + + uid_t + uid = getuid(), + euid = geteuid(); + gid_t + gid = getgid(), + egid = getegid(); + + char cwd[PATH_MAX]; + getcwd(cwd, PATH_MAX); + + if (uid == 0 && gid == 0) { + fputs("running as root", stderr); + } else { + fprintf(stderr, "running as %d:%d\n", uid, gid); + } + fprintf(stderr, "effective perms %d:%d\n", euid, egid); + + // check if exists and/or can be accessed + { + int mntpoint_rv = 0; + struct stat mntpoint_stat = {0}; + mntpoint_rv = stat(mntpoint, &mntpoint_stat); + if (mntpoint_rv == -1) { + fprintf(stderr, "couldn't stat '%s': %m\n", mntpoint); + exit(EXIT_FAILURE); + } + } + + // check for the presense of /chrooted + if (check_chrooted) { + char chrooted[PATH_MAX] = {0}; + int chrooted_rv = 0; + struct stat chrooted_stat = {0}; + strncpy(chrooted, mntpoint, PATH_MAX); + strncat(chrooted, "/chrooted", PATH_MAX - 1); + fprintf(stderr, "checking file in '%s'\n", chrooted); + + chrooted_rv = stat(chrooted, &chrooted_stat); + if (chrooted_rv == -1 && errno == ENOENT) { + fprintf(stderr, "'%s' doesn't exist, aborting...\n", chrooted); + exit(EXIT_FAILURE); + } + } + + rv = chroot(mntpoint); + if (rv == -1) { + fprintf(stderr, "chroot failed: %m\n"); + exit(EXIT_FAILURE); + } + + rv = setenv("CHROOTED", "1", 1); + if (rv == -1) { + fprintf(stderr, "CHROOTED not set in env: %m\n"); + // non-fatal + } + + rv = setgid(gid); + if (rv == -1) { + fprintf(stderr, "setgid failed: %m\n"); + exit(EXIT_FAILURE); + } + rv = setuid(uid); + if(rv == -1) { + fprintf(stderr, "setuid failed: %m\n"); + exit(EXIT_FAILURE); + } + + rv = chdir(cwd); + if (rv == -1) { + fprintf(stderr, "chdir failed: %m\n"); + // non-fatal + } + + char *shellv[] = {shell, NULL}; + rv = execv(shell, shellv); + fprintf(stderr, "execv %s failed: %m\n", shell); + return EXIT_FAILURE; +} diff --git a/sourcecode/enter-chroot/install.sh b/sourcecode/enter-chroot/install.sh new file mode 100755 index 0000000..f0b94a5 --- /dev/null +++ b/sourcecode/enter-chroot/install.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +set -e + +rm -f enter-chroot +make enter-chroot + +install -m4755 enter-chroot /usr/local/bin/ diff --git a/sourcecode/sshfs-map/.gitignore b/sourcecode/sshfs-map/.gitignore deleted file mode 100644 index f5d8282..0000000 --- a/sourcecode/sshfs-map/.gitignore +++ /dev/null @@ -1 +0,0 @@ -sshfs-map