su: Support supplementary groups
This commit is contained in:
parent
8a90db837b
commit
1005305d5a
28
apps/su.cpp
28
apps/su.cpp
@ -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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user