diff --git a/apps/su.cpp b/apps/su.cpp index 33786533..80b1a8a8 100644 --- a/apps/su.cpp +++ b/apps/su.cpp @@ -1,4 +1,6 @@ +#include #include +#include #include #include #include @@ -89,6 +91,30 @@ char* getpass() return buf; } +Result set_supplementary_groups(const char* name) +{ + Vector 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(extra_groups.size()), extra_groups.data()) < 0) return err(errno); + + return {}; +} + Result luna_main(int argc, char** argv) { StringView name; @@ -134,6 +160,8 @@ Result 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);