Userspace: Implement barebones sudo

This doesn't do any password checking or anything. Just sets uid and
gid before execvp()
This commit is contained in:
Bananymous 2023-12-06 18:15:42 +02:00
parent 24b71d1170
commit 06a84da844
3 changed files with 49 additions and 0 deletions

View File

@ -22,6 +22,7 @@ set(USERSPACE_PROJECTS
sleep
snake
stat
sudo
sync
tee
test

View File

@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.26)
project(sudo CXX)
set(SOURCES
main.cpp
)
add_executable(sudo ${SOURCES})
target_compile_options(sudo PUBLIC -O2 -g)
target_link_libraries(sudo PUBLIC libc)
add_custom_target(sudo-install
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/sudo ${BANAN_BIN}/
COMMAND /bin/chmod u+s ${BANAN_BIN}/sudo
DEPENDS sudo
)

31
userspace/sudo/main.cpp Normal file
View File

@ -0,0 +1,31 @@
#include <stdio.h>
#include <unistd.h>
int usage(char* argv0, int ret)
{
FILE* fp = (ret == 0) ? stdout : stderr;
fprintf(fp, "usage: %s COMMAND [ARGUMENTS]\n", argv0);
return ret;
}
int main(int argc, char** argv)
{
if (argc < 2)
return usage(argv[0], 1);
if (setuid(0) == -1)
{
perror("setuid");
return 1;
}
if (setgid(0) == -1)
{
perror("setgid");
return 1;
}
execvp(argv[1], argv + 1);
perror("execv");
return 1;
}