Id: Print username corresponding to uid/euid

Also print euid and egid if they differ from uid or gid.
This commit is contained in:
Bananymous 2023-08-18 15:36:51 +03:00
parent dc7391dc89
commit 30463c9082
2 changed files with 31 additions and 2 deletions

View File

@ -8,7 +8,7 @@ set(SOURCES
add_executable(id ${SOURCES}) add_executable(id ${SOURCES})
target_compile_options(id PUBLIC -O2 -g) target_compile_options(id PUBLIC -O2 -g)
target_link_libraries(id PUBLIC libc) target_link_libraries(id PUBLIC libc ban)
add_custom_target(id-install add_custom_target(id-install
COMMAND cp ${CMAKE_CURRENT_BINARY_DIR}/id ${BANAN_BIN}/ COMMAND cp ${CMAKE_CURRENT_BINARY_DIR}/id ${BANAN_BIN}/

View File

@ -1,7 +1,36 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <pwd.h>
int main() int main()
{ {
printf("uid %u, gid %u, euid %u, egid %u\n", getuid(), getgid(), geteuid(), getegid()); uid_t uid = getuid();
uid_t euid = geteuid();
gid_t gid = getgid();
gid_t egid = getegid();
passwd* pw_uid = getpwuid(uid);
if (pw_uid == nullptr)
{
fprintf(stderr, "Unknown user #%d\n", uid);
return 1;
}
passwd* pw_euid = getpwuid(euid);
if (pw_euid == nullptr)
{
fprintf(stderr, "Unknown user #%d\n", euid);
return 1;
}
printf("uid=%u(%s)", uid, pw_uid->pw_name);
if (uid != euid)
printf(",euid=%u(%s)",euid, pw_euid->pw_name);
printf(" gid=%u", gid);
if (gid != egid)
printf(",egid=%u", egid);
printf("\n");
} }