Initial commit

This commit is contained in:
Fierelier 2023-12-11 13:12:57 +01:00
commit 24d1992dfa
7 changed files with 194 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/privlx

9
LICENSE Normal file
View File

@ -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.

5
compile Executable file
View File

@ -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

4
env Normal file
View File

@ -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}"

19
install Executable file
View File

@ -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"

34
readme.txt Normal file
View File

@ -0,0 +1,34 @@
privlx (Privilege List eXecute)
Allow unprivileged users to run executables for privileged users.
* Syntax:
privlx <command>
* 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

122
src/main.c Normal file
View File

@ -0,0 +1,122 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <pwd.h>
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 <command> ...\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;
}