-
Notifications
You must be signed in to change notification settings - Fork 1
/
libopenredir.c
239 lines (192 loc) · 6.44 KB
/
libopenredir.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include<stdarg.h>
#include<dlfcn.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<limits.h>
#include<unistd.h>
#include<lua.h>
#include<lualib.h>
#include<lauxlib.h>
static int lib_initialized = 0;
static int (*orig_open)(const char*, int, mode_t) = 0;
static int (*orig_open64)(const char*, int, mode_t) = 0;
static int (*orig_creat)(const char*, mode_t) = 0;
static ssize_t (*orig_readlink)(const char*, char*, size_t) = 0;
static int (*orig___open_2)(const char*, int) = 0;
static int (*orig___open64_2)(const char*, int) = 0;
static int (*orig_stat)(const char*, void*) = 0;
static int (*orig_statx)(int, const char*, int, unsigned int, void*) = 0;
static int (*orig___lxstat)(int, const char*, void*) = 0;
static int (*orig___lxstat64)(int, const char*, void*) = 0;
static int (*orig_execve)(const char*, char *const*, char *const*) = 0;
static void* (*orig_opendir)(const char*) = 0;
static ssize_t (*orig_getxattr)(const char*, const char*, void*, size_t) = 0;
static ssize_t (*orig_lgetxattr)(const char*, const char*, void*, size_t) = 0;
static lua_State *L = NULL;
void lib_init();
void die(char *fmt, ...) {
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n");
fflush(stderr);
exit(-1);
}
static char* redirect(const char* func, const char* file){
int ret;
const char *new;
lua_getglobal(L, "redirect");
lua_pushstring(L, func);
lua_pushstring(L, file);
ret = lua_pcall(L, 2, 1, 0);
if(ret){
fprintf(stderr, "取得重定向文件路径时出错了: %s\n", lua_tostring(L, -1));
lua_pop(L, 1); /* 错误信息 */
return (char*)file;
}else{
new = lua_tostring(L, -1);
lua_pop(L, 1); /* 返回值 */
}
return (char*)new;
}
int open(const char* file, int flags, mode_t mode) {
lib_init();
file = redirect("open", file);
return orig_open(file, flags, mode);
}
int open64(const char* file, int flags, mode_t mode) {
lib_init();
file = redirect("open64", file);
return orig_open64(file, flags, mode);
}
int creat(const char* file, mode_t mode) {
lib_init();
file = redirect("creat", file);
return orig_creat(file, mode);
}
ssize_t readlink(const char* file, char* buf, size_t bufsiz) {
lib_init();
file = redirect("readlink", file);
return orig_readlink(file, buf, bufsiz);
}
int __open_2(const char* file, int flags) {
lib_init();
file = redirect("__open_2", file);
return orig___open_2(file, flags);
}
int __open64_2(const char* file, int flags) {
lib_init();
file = redirect("__open64_2", file);
return orig___open64_2(file, flags);
}
int stat(const char* file, void* buf) {
lib_init();
file = redirect("stat", file);
return orig_stat(file, buf);
}
int statx(int fd, const char* file, int flags, unsigned int mask, void* buf) {
lib_init();
file = redirect("statx", file);
return orig_statx(fd, file, flags, mask, buf);
}
int __lxstat(int vers, const char* file, void* buf) {
lib_init();
file = redirect("__lxstat", file);
return orig___lxstat(vers, file, buf);
}
int __lxstat64(int vers, const char* file, void* buf) {
lib_init();
file = redirect("__lxstat64", file);
return orig___lxstat64(vers, file, buf);
}
int execve(const char* file, char *const* argv, char *const* envp) {
lib_init();
file = redirect("execve", file);
return orig_execve(file, argv, envp);
}
void* opendir(const char* file) {
lib_init();
file = redirect("opendir", file);
return orig_opendir(file);
}
ssize_t getxattr(const char* file, const char* name, void* value, size_t size) {
lib_init();
file = redirect("getxattr", file);
return orig_getxattr(file, name, value, size);
}
ssize_t lgetxattr(const char* file, const char* name, void* value, size_t size) {
lib_init();
file = redirect("lgetxattr", file);
return orig_lgetxattr(file, name, value, size);
}
void lib_init() {
void *libhdl;
char *dlerr;
if (lib_initialized) return;
if (!(libhdl=dlopen("libc.so.6", RTLD_LAZY)))
die("Failed to patch library calls: %s", dlerror());
orig_open = dlsym(libhdl, "open");
if ((dlerr=dlerror()) != NULL)
die("Failed to patch open() library call: %s", dlerr);
orig_open64 = dlsym(libhdl, "open64");
if ((dlerr=dlerror()) != NULL)
die("Failed to patch open64() library call: %s", dlerr);
orig_creat = dlsym(libhdl, "creat");
if ((dlerr=dlerror()) != NULL)
die("Failed to patch creat() library call: %s", dlerr);
orig_readlink = dlsym(libhdl, "readlink");
if ((dlerr=dlerror()) != NULL)
die("Failed to patch readlink() library call: %s", dlerr);
orig___open_2 = dlsym(libhdl, "__open_2");
if ((dlerr=dlerror()) != NULL)
die("Failed to patch __open_2() library call: %s", dlerr);
orig___open64_2 = dlsym(libhdl, "__open64_2");
if ((dlerr=dlerror()) != NULL)
die("Failed to patch __open64_2() library call: %s", dlerr);
orig_stat = dlsym(libhdl, "stat");
if ((dlerr=dlerror()) != NULL)
die("Failed to patch stat() library call: %s", dlerr);
orig_statx = dlsym(libhdl, "statx");
if ((dlerr=dlerror()) != NULL)
die("Failed to patch statx() library call: %s", dlerr);
orig___lxstat = dlsym(libhdl, "__lxstat");
if ((dlerr=dlerror()) != NULL)
die("Failed to patch __lxstat() library call: %s", dlerr);
orig___lxstat64 = dlsym(libhdl, "__lxstat64");
if ((dlerr=dlerror()) != NULL)
die("Failed to patch __lxstat64() library call: %s", dlerr);
orig_execve = dlsym(libhdl, "execve");
if ((dlerr=dlerror()) != NULL)
die("Failed to patch execve() library call: %s", dlerr);
orig_opendir = dlsym(libhdl, "opendir");
if ((dlerr=dlerror()) != NULL)
die("Failed to patch opendir() library call: %s", dlerr);
orig_getxattr = dlsym(libhdl, "getxattr");
if ((dlerr=dlerror()) != NULL)
die("Failed to patch getxattr() library call: %s", dlerr);
orig_lgetxattr = dlsym(libhdl, "lgetxattr");
if ((dlerr=dlerror()) != NULL)
die("Failed to patch lgetxattr() library call: %s", dlerr);
int ret;
L = luaL_newstate();
luaL_openlibs(L);
char config[PATH_MAX];
if (getenv("OPENREDIR_CONFDIR"))
strncpy(config, getenv("OPENREDIR_CONFDIR"), PATH_MAX);
else
strncpy(config, getenv("HOME"), PATH_MAX);
strcat(config, "/.openredir.lua");
ret = luaL_dofile(L, config);
if(ret){
die("Error run ~/.openredir.lua");
}
lua_getglobal(L, "redirect");
if(!lua_isfunction(L,-1)){
die("Error run 'redirect' function in openredir.lua");
}
lua_pop(L, 1);
lib_initialized = 1;
}
/* vim: set syntax=c.tornadotmpl: */