1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 09:57:35 +00:00

Add some basic setgroups(), getgroups() and initgroups().

Also teach /bin/id to print the user's supplemental groups.
This commit is contained in:
Andreas Kling 2018-11-07 01:38:51 +01:00
parent d3bd3791cb
commit a7f1d892a9
12 changed files with 167 additions and 19 deletions

View file

@ -118,4 +118,26 @@ next_entry:
return __grdb_entry;
}
int initgroups(const char* user, gid_t extra_gid)
{
size_t count = 0;
gid_t gids[32];
bool extra_gid_added = false;
setgrent();
while (auto* gr = getgrent()) {
for (auto* mem = gr->gr_mem; *mem; ++mem) {
if (!strcmp(*mem, user)) {
gids[count++] = gr->gr_gid;
if (gr->gr_gid == extra_gid)
extra_gid_added = true;
break;
}
}
}
endgrent();
if (!extra_gid_added)
gids[count++] = extra_gid;
return setgroups(count, gids);
}
}

View file

@ -18,4 +18,6 @@ void endgrent();
struct group* getgrnam(const char* name);
struct group* getgrgid(gid_t);
int initgroups(const char* user, gid_t);
__END_DECLS

View file

@ -3,6 +3,9 @@
#include <errno.h>
#include <stdarg.h>
#include <assert.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <Kernel/Syscall.h>
extern "C" {
@ -218,4 +221,16 @@ int dup2(int old_fd, int new_fd)
__RETURN_WITH_ERRNO(rc, rc, -1);
}
int setgroups(size_t size, const gid_t* list)
{
int rc = Syscall::invoke(Syscall::SC_getgroups, (dword)size, (dword)list);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
int getgroups(int size, gid_t list[])
{
int rc = Syscall::invoke(Syscall::SC_getgroups, (dword)size, (dword)list);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
}

View file

@ -21,6 +21,8 @@ gid_t getegid();
uid_t getuid();
gid_t getgid();
pid_t getpid();
int getgroups(int size, gid_t list[]);
int setgroups(size_t, const gid_t*);
pid_t tcgetpgrp(int fd);
int tcsetpgrp(int fd, pid_t pgid);
int open(const char* path, int options, ...);