-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswitch_user.cpp
42 lines (36 loc) · 1013 Bytes
/
switch_user.cpp
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
#include <unistd.h>
#include <stdio.h>
#include <pwd.h>
#include <sys/types.h>
/* root 切换到目标用户 */
static bool switch_to_user(uid_t user_id, gid_t gp_id) {
/* 先保证目标用户不是 root */
if (user_id == 0 && gp_id == 0) {
return false;
}
/* 当前用户是合法用户:root 或者目标用户 */
gid_t gid = getgid();
uid_t uid = getuid();
if (((gid != 0) || uid != 0) && ((gid != gp_id) || uid != user_id)) {
return false; // 当前用户不是 root 又不是目标那么返回 false
}
if (uid != 0) { // 不是 root 那么已经是目标用户
return true;
}
/* 切换到目标用户 */
if (setgid(gp_id) < 0 || setuid(user_id) < 0) {
return false;
}
return true;
}
int main() {
struct passwd* user;
user = getpwnam_r("consolexin");
bool ret = switch_to_user(user->pw_uid, user->pw_gid);
if (ret) {
puts("ok");
} else {
puts("error");
}
return 0;
}