forked from ganglia/monitor-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
become_a_nobody.c
52 lines (44 loc) · 1.02 KB
/
become_a_nobody.c
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
43
44
45
46
47
48
49
50
51
52
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <unistd.h>
#include <sys/types.h>
#include <grp.h>
#include <pwd.h>
#include "become_a_nobody.h"
#include <gm_msg.h>
void
become_a_nobody( const char *username )
{
int rval;
struct passwd *pw;
pw = getpwnam(username);
if ( pw == NULL )
{
err_quit("user '%s' does not exist\n\n", username);
}
rval = getuid();
if ( rval != pw->pw_uid )
{
if ( rval != 0 )
{
err_quit("Must be root to setuid to \"%s\"\n\n", username);
}
rval = setgid(pw->pw_gid);
if ( rval < 0 )
{
err_quit("exiting. setgid %d error", (int) pw->pw_gid);
}
rval = initgroups(username, pw->pw_gid);
if ( rval < 0 )
{
err_quit("exiting. initgroups '%s', %d error",
username, (int) pw->pw_gid);
}
rval = setuid(pw->pw_uid);
if ( rval < 0 )
{
err_quit("exiting. setuid '%s' error", username);
}
}
}