su: Support supplementary groups

This commit is contained in:
apio 2023-11-22 21:29:03 +01:00
parent 8a90db837b
commit 1005305d5a
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -1,4 +1,6 @@
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <os/ArgumentParser.h>
#include <pwd.h>
#include <signal.h>
@ -89,6 +91,30 @@ char* getpass()
return buf;
}
Result<void> set_supplementary_groups(const char* name)
{
Vector<gid_t> extra_groups;
setgrent();
group* grp;
while ((grp = getgrent()))
{
for (char** user = grp->gr_mem; *user; user++)
{
if (!strcmp(*user, name))
{
TRY(extra_groups.try_append(grp->gr_gid));
break;
}
}
}
endgrent();
if (setgroups(static_cast<int>(extra_groups.size()), extra_groups.data()) < 0) return err(errno);
return {};
}
Result<int> luna_main(int argc, char** argv)
{
StringView name;
@ -134,6 +160,8 @@ Result<int> luna_main(int argc, char** argv)
memset(pass, 0, strlen(pass));
}
TRY(set_supplementary_groups(name.chars()));
setgid(entry->pw_gid);
setuid(entry->pw_uid);