-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitgroups.c
More file actions
executable file
·42 lines (36 loc) · 769 Bytes
/
Copy pathinitgroups.c
File metadata and controls
executable file
·42 lines (36 loc) · 769 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* Implement initgroups()
*/
#include <pwd.h>
#include <grp.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <stdlib.h>
int myinitgroups(const char *user, gid_t group) {
int ngrps;
uid_t uid;
gid_t gids[NGROUPS_MAX + 1];
struct pwd *pwd;
struct group *grp;
char *name;
ngrps = 0;
while ((grp = getgrent()) != NULL) {
for (int i = 0; (name = grp->gr_mem[i]); i++) {
if (strcmp(name, user) == 0) {
gids[ngrps++] = grp->gr_gid;
}
}
}
endgrent();
gids[ngrps] = group;
return setgroups(ngrps, gids);
}
int main(int argc, char **argv) {
if (myinitgroups("meguca", 0) == -1) {
printf("Failed to init groups, is this process privilaged?\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}