forked from abcz316/SKRoot-linuxKernelRoot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsu.cpp
More file actions
168 lines (152 loc) · 4.4 KB
/
Copy pathsu.cpp
File metadata and controls
168 lines (152 loc) · 4.4 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "su.h"
#include "log.h"
#include "su_hide_path_utils.h"
#include "../testRoot/kernel_root_helper.h"
#include "../testRoot/myself_path_utils.h"
namespace {
/*
* Bionic's atoi runs through strtol().
* Use our own implementation for faster conversion.
*/
int parse_int(const char* s) {
int val = 0;
char c;
while ((c = *(s++))) {
if (c > '9' || c < '0')
return -1;
val = val * 10 + c - '0';
}
return val;
}
}
void usage(int status) {
FILE* stream = (status == EXIT_SUCCESS) ? stdout : stderr;
fprintf(stream,
"linux kernel root\n\n"
"Usage: su [options] [-] [user [argument...]]\n\n"
"Options:\n"
" -c, --command COMMAND pass COMMAND to the invoked shell\n"
" -h, --help display this help message and exit\n"
" -, -l, --login pretend the shell to be a login shell\n"
" -m, -p,\n"
" --preserve-environment preserve the entire environment\n"
" -s, --shell SHELL use SHELL instead of the default " DEFAULT_SHELL "\n"
" -v, --version display version number and exit\n"
" -V display version code and exit\n"
" -mm, -M,\n"
" --mount-master force run in the global mount namespace\n\n");
exit(status);
}
static inline std::string get_root_key() {
char myself_path[1024] = { 0 };
char processname[1024];
get_executable_path(myself_path, processname, sizeof(myself_path));
TRACE("su start: my directory:%s, processname:%s\n", myself_path, processname);
std::string str_root_key = parse_root_key_by_myself_path(myself_path);
return str_root_key;
}
int su_client_main(int argc, char* argv[]) {
int c;
struct option long_opts[] = {
{ "command", required_argument, nullptr, 'c' },
{ "help", no_argument, nullptr, 'h' },
{ "login", no_argument, nullptr, 'l' },
{ "preserve-environment", no_argument, nullptr, 'p' },
{ "shell", required_argument, nullptr, 's' },
{ "version", no_argument, nullptr, 'v' },
{ "context", required_argument, nullptr, 'z' },
{ "mount-master", no_argument, nullptr, 'M' },
{ nullptr, 0, nullptr, 0 },
};
su_request su_req;
for (int i = 0; i < argc; i++) {
// Replace -cn with -z, -mm with -M for supporting getopt_long
if (strcmp(argv[i], "-cn") == 0)
strcpy(argv[i], "-z");
else if (strcmp(argv[i], "-mm") == 0)
strcpy(argv[i], "-M");
}
while ((c = getopt_long(argc, argv, "c:hlmps:Vvuz:M", long_opts, nullptr)) != -1) {
switch (c) {
case 'c':
for (int i = optind - 1; i < argc; ++i) {
if (!su_req.command.empty())
su_req.command += ' ';
su_req.command += argv[i];
}
optind = argc;
break;
case 'h':
usage(EXIT_SUCCESS);
break;
case 'l':
su_req.login = true;
break;
case 'm':
case 'p':
su_req.keepenv = true;
break;
case 's':
su_req.shell = optarg;
break;
case 'V':
printf("%d\n", ROOT_VER_CODE);
exit(EXIT_SUCCESS);
case 'v':
printf("%s\n", ROOT_VERSION);
exit(EXIT_SUCCESS);
case 'z':
// Do nothing, placed here for legacy support :)
break;
case 'M':
su_req.mount_master = true;
break;
default:
/* Bionic getopt_long doesn't terminate its error output by newline */
fprintf(stderr, "\n");
usage(2);
}
}
if (optind < argc && strcmp(argv[optind], "-") == 0) {
su_req.login = true;
optind++;
}
std::string root_key = get_root_key();
TRACE("root_key:%s\n", root_key.c_str());
if (fork() == 0) {
kernel_root::get_root(root_key.c_str());
TRACE("current uid:%d\n", getuid());
/* username or uid */
if (optind < argc) {
struct passwd* pw;
pw = getpwnam(argv[optind]);
if (pw) {
su_req.uid = pw->pw_uid;
} else {
su_req.uid = parse_int(argv[optind]);
}
optind++;
}
struct passwd* pw = getpwuid(su_req.uid);
if (pw) {
setenv("HOME", pw->pw_dir, 1);
setenv("USER", pw->pw_name, 1);
setenv("LOGNAME", pw->pw_name, 1);
setenv("SHELL", su_req.shell.data(), 1);
}
const char* new_argv[4] = { nullptr };
new_argv[0] = su_req.login ? "-" : su_req.shell.data();
if (!su_req.command.empty()) {
new_argv[1] = "-c";
new_argv[2] = su_req.command.data();
}
execvp(su_req.shell.data(), (char**)new_argv);
} else {
wait(NULL);
}
exit(0);
return 0;
}
int main(int argc, char* argv[]) {
return su_client_main(argc, argv);
}