forked from Bananymous/banan-os
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:
parent
24b71d1170
commit
06a84da844
|
@ -22,6 +22,7 @@ set(USERSPACE_PROJECTS
|
|||
sleep
|
||||
snake
|
||||
stat
|
||||
sudo
|
||||
sync
|
||||
tee
|
||||
test
|
||||
|
|
|
@ -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
|
||||
)
|
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue