logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git
commit: a0dfb2e9316895985f17eefde0596126a26c59d2
parent 9c217fe1593ca848e178a61338eae474b18ed9c1
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sat, 12 Mar 2022 02:34:57 +0100

bin/id: Add support for -g and -r

Diffstat:

Mbin/id.c42++++++++++++++++++++++++++++++++++++++++++
Mtest-bin/id9+++++++++
2 files changed, 51 insertions(+), 0 deletions(-)

diff --git a/bin/id.c b/bin/id.c @@ -144,6 +144,8 @@ main(int argc, char *argv[]) int ret, c; bool euid_flag = false; bool G_flag = false; + bool g_flag = false; + bool real_flag = false; /* flawfinder: ignore. Old implementations of getopt should fix themselves */ while((c = getopt(argc, argv, ":Ggunr")) != EOF) @@ -159,6 +161,12 @@ main(int argc, char *argv[]) case 'u': euid_flag = true; break; + case 'g': + g_flag = true; + break; + case 'r': + real_flag = true; + break; default: assert(false); } @@ -167,6 +175,40 @@ main(int argc, char *argv[]) argc -= optind; argv += optind; + if(g_flag) + { + gid_t gid; + + if(real_flag) + { + gid = getgid(); + } + else + { + gid = getegid(); + } + + if(!name_flag) + { + ret = printf("%u\n", gid); + } + else + { + struct group *gr = getgrgid(gid); + + assert(gr->gr_gid == gid); + + ret = printf("%s\n", gr->gr_name); + } + + if(ret < 0) + { + return 1; + } + + return 0; + } + // geteuid, getuid, getegid, getgid shall always be successful uid_t uid = getuid(); uid_t euid = geteuid(); diff --git a/test-bin/id b/test-bin/id @@ -27,6 +27,14 @@ devfull_body() { atf_check -s exit:1 sh -c '../bin/id -n >/dev/full' } +atf_test_case group +group_body() { + atf_check -o "inline:$(id -g)\n" ../bin/id -g + atf_check -o "inline:$(id -gr)\n" ../bin/id -gr + atf_check -o "inline:$(id -gn)\n" ../bin/id -gn + atf_check -o "inline:$(id -gnr)\n" ../bin/id -gnr +} + atf_test_case noetc noetc_body() { bwrap_args="--ro-bind-try /usr /usr --ro-bind-try /lib /lib --ro-bind-try /lib64 /lib64 --ro-bind ../bin /bin" @@ -51,6 +59,7 @@ atf_init_test_cases() { atf_add_test_case noargs atf_add_test_case names + atf_add_test_case group atf_add_test_case noetc }