diff --git a/userspace/CMakeLists.txt b/userspace/CMakeLists.txt index a53eac0415..1c36198b25 100644 --- a/userspace/CMakeLists.txt +++ b/userspace/CMakeLists.txt @@ -22,6 +22,7 @@ set(USERSPACE_PROJECTS sleep snake stat + sudo sync tee test diff --git a/userspace/sudo/CMakeLists.txt b/userspace/sudo/CMakeLists.txt new file mode 100644 index 0000000000..f1cf6b2962 --- /dev/null +++ b/userspace/sudo/CMakeLists.txt @@ -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 +) diff --git a/userspace/sudo/main.cpp b/userspace/sudo/main.cpp new file mode 100644 index 0000000000..3ab61075a9 --- /dev/null +++ b/userspace/sudo/main.cpp @@ -0,0 +1,31 @@ +#include +#include + +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; +}