From 24d1992dfa362e6463a7589e7992eb75eed9e797 Mon Sep 17 00:00:00 2001 From: Fierelier Date: Mon, 11 Dec 2023 13:12:57 +0100 Subject: [PATCH] Initial commit --- .gitignore | 1 + LICENSE | 9 ++++ compile | 5 +++ env | 4 ++ install | 19 +++++++++ readme.txt | 34 +++++++++++++++ src/main.c | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 194 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100755 compile create mode 100644 env create mode 100755 install create mode 100644 readme.txt create mode 100644 src/main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ceb16cb --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/privlx diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..67fe97b --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2023 + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/compile b/compile new file mode 100755 index 0000000..2d0d37a --- /dev/null +++ b/compile @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +set -e +SCRIPT_DIR="$(dirname "$(realpath -s "$BASH_SOURCE")")" +source "$SCRIPT_DIR/env" +"$CC" -std=gnu89 -Werror -Wall -Os -D APP_NAME="\"$APP_NAME\"" -D APP_CONF="\"$APP_CONF\"" "$SCRIPT_DIR/src/main.c" -o "$SCRIPT_DIR/$APP_NAME" $CFLAGS diff --git a/env b/env new file mode 100644 index 0000000..9f3fa27 --- /dev/null +++ b/env @@ -0,0 +1,4 @@ +APP_NAME="${APP_NAME:=privlx}" +APP_BIN="${APP_BIN:=/usr/local/bin/$APP_NAME}" +APP_CONF="${APP_CONF:=/etc/$APP_NAME}" +CC="${CC:=cc}" diff --git a/install b/install new file mode 100755 index 0000000..f66f1cb --- /dev/null +++ b/install @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +set -e +SCRIPT_DIR="$(dirname "$(realpath -s "$BASH_SOURCE")")" +source "$SCRIPT_DIR/env" +mkdir -p "$(dirname "$APP_BIN")" +cp "$SCRIPT_DIR/$APP_NAME" "$APP_BIN" +chown 0 "$APP_BIN" +chgrp 0 "$APP_BIN" +chmod 755 "$APP_BIN" +chmod u+s "$APP_BIN" +chmod g+s "$APP_BIN" + +mkdir -p "$APP_CONF" +if ! [ -f "$APP_CONF/commands.conf" ]; then + echo "" > "$APP_CONF/commands.conf" +fi +chown 0 "$APP_CONF/commands.conf" +chgrp 0 "$APP_CONF/commands.conf" +chmod 644 "$APP_CONF/commands.conf" diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..bc60fb8 --- /dev/null +++ b/readme.txt @@ -0,0 +1,34 @@ +privlx (Privilege List eXecute) +Allow unprivileged users to run executables for privileged users. + +* Syntax: +privlx + +* Config: +Add commands you want to allow users to run as root in /etc/privlx/commands.conf, one per line. + +Example (/etc/privlx/commands.conf): +poweroff +reboot +pm-hibernate +pm-powersave +pm-suspend-hybrid +pm-suspend + +Allow the user to access power management tools. Be warned, poweroff and reboot let the user break the system. It is best to make a wrapper script, to only allow defined behavior. + +* BUILDING +* Configuration: +Before compiling or installing, you may set these environment variables, either in a shell, or inside of ./env: +- CC: Your C compiler (default: cc) +- CFLAGS: Compilation flags (default: none) +- APP_NAME: What the app is called. This is used for outputting errors (default: privlx) +- APP_BIN: Where the app's binary is stored (default: /usr/local/$APP_NAME) +- APP_CONF: Where the app's configuration files are stored (default: /etc/$APP_NAME) + +* Compilation: +Run ./compile +You need a C compiler, and standard libraries + +* Installation: +Run ./install as root diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..c87deb9 --- /dev/null +++ b/src/main.c @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +void *emalloc(void *ptr, size_t size) { + void *m = realloc(ptr,size); + if (size != 0 && m == NULL) { + fprintf(stderr,"["APP_NAME"] Error 255: realloc() failed\n"); + exit(255); + } + return m; +} + +int main(int argc, char **argv) { + if (clearenv() != 0) { // Clear environment to boost security + fprintf(stderr,"["APP_NAME"] Error 254: Clearing environment failed\n"); + exit(254); + } + + uid_t uid = getuid(); // User who's calling the program + gid_t gid = getgid(); // Group that's calling the program + uid_t euid = geteuid(); // User who owns the program + gid_t egid = getegid(); // Group that owns the program + + // ENV + // PATH + if (euid == 0) { + setenv("PATH","/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",1); + } else { + setenv("PATH","/usr/local/bin:/usr/bin:/bin",1); + } + + // UID, USER, HOME + char passwStore[sizeof(struct passwd)]; + sprintf(passwStore,"%d",euid); + setenv("UID",passwStore,1); + struct passwd * pw = getpwuid(euid); + if (pw == NULL) { + fprintf(stderr,"["APP_NAME"] Error 253: Failed to get user information (errno: %d: %s)\n",errno,strerror(errno)); + return 253; + } + setenv("USER",pw -> pw_name,1); + setenv("HOME",pw -> pw_dir,1); + + // Strip first argument, and move them by 1 + char **cmd; + if (argc > 1) { + cmd = emalloc(NULL,argc * sizeof(char*)); + int i = 1; + while (i < argc) { + cmd[i - 1] = argv[i]; + ++i; + } + cmd[argc - 1] = NULL; + } else { + fprintf(stderr,"["APP_NAME"] Error 252: Invalid syntax, use: %s ...\n",argv[0]); + return 252; + } + + int file; + file = open(APP_CONF"/commands.conf",O_RDONLY); + if (file == -1) { + fprintf(stderr,"["APP_NAME"] Error 251: Failed opening "APP_CONF"/commands.conf (errno %d: %s)\n",errno,strerror(errno)); + return 251; + } + + char program[PATH_MAX]; + program[0] = 0; + { + int lineIndex = 0; + ssize_t size; + while (1) { + size = read(file,(void *)(program + lineIndex),1); + if (size == -1) { + fprintf(stderr,"["APP_NAME"] Error 250: Failed reading commands.conf (errno %d: %s)\n",errno,strerror(errno)); + return 250; + } + + if ((program[lineIndex] == '\n') || (size == 0)) { // end of line/file + program[lineIndex] = 0; + + if (strcmp(program,cmd[0]) == 0) { // command found + break; + } + + program[0] = 0; + lineIndex = 0; + } else { + ++lineIndex; + } + + if (size == 0) { // end of file + fprintf(stderr,"["APP_NAME"] Error 249: Requested program is not in commands.conf\n"); + return 249; + } + + if (lineIndex >= PATH_MAX) { // line is too long (this could be handled better) + fprintf(stderr,"["APP_NAME"] Error 248: Malformed commands.conf (line longer than %d bytes)\n",PATH_MAX); + return 248; + } + } + } + + // Get permissions + if (setreuid(euid,egid) == -1) { + fprintf(stderr,"["APP_NAME"] Error 247: Failed to elevate user (errno %d: %s)\n",errno,strerror(errno)); + return 247; + } + + // Run progrem + if (execvp(cmd[0],cmd) == -1) { + setreuid(uid,gid); + fprintf(stderr,"["APP_NAME"] Error 246: Failed launching process (errno %d: %s)\n",errno,strerror(errno)); + return 246; + } + return 0; +}