-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.c
375 lines (292 loc) · 8.73 KB
/
command.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
* MIT License
*
* Copyright (c) 2019 Jianhui Zhao <zhaojh329@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <limits.h>
#include <shadow.h>
#include <sys/stat.h>
#include <math.h>
#include <pwd.h>
#include "log/log.h"
#include "list.h"
#include "utils.h"
#include "command.h"
static int nrunning;
static LIST_HEAD(task_pending);
static void run_task(struct task *t);
/* For execute command */
static bool login_test(const char *username, const char *password)
{
struct spwd *sp;
if (!username || *username == 0)
return false;
sp = getspnam(username);
if (!sp || !sp->sp_pwdp[1])
return false;
if (!password)
password = "";
return !strcmp(crypt(password, sp->sp_pwdp), sp->sp_pwdp);
}
static const char *cmd_lookup(const char *cmd)
{
struct stat s;
int plen = 0, clen = strlen(cmd) + 1;
char *search, *p;
static char path[PATH_MAX];
if (!stat(cmd, &s) && S_ISREG(s.st_mode))
return cmd;
search = getenv("PATH");
if (!search)
search = "/bin:/usr/bin:/sbin:/usr/sbin";
p = search;
do {
if (*p != ':' && *p != '\0')
continue;
plen = p - search;
if ((plen + clen) >= sizeof(path))
continue;
strncpy(path, search, plen);
sprintf(path + plen, "/%s", cmd);
if (!stat(path, &s) && S_ISREG(s.st_mode))
return path;
search = p + 1;
} while (*p++);
return NULL;
}
static const char *cmderr2str(int err)
{
switch (err) {
case RTTY_CMD_ERR_PERMIT:
return "operation not permitted";
case RTTY_CMD_ERR_NOT_FOUND:
return "not found";
case RTTY_CMD_ERR_NOMEM:
return "no mem";
case RTTY_CMD_ERR_SYSERR:
return "sys error";
case RTTY_CMD_ERR_RESP_TOOBIG:
return "stdout+stderr is too big";
default:
return "";
}
}
static void task_free(struct task *t)
{
int i;
/* stdout watcher */
if (t->ioo.fd > 0) {
ev_io_stop(t->rtty->loop, &t->ioo);
close(t->ioo.fd);
}
/* stderr watcher */
if (t->ioe.fd > 0) {
ev_io_stop(t->rtty->loop, &t->ioe);
close(t->ioe.fd);
}
ev_child_stop(t->rtty->loop, &t->cw);
ev_timer_stop(t->rtty->loop, &t->timer);
buffer_free(&t->ob);
buffer_free(&t->eb);
for (i = 0; i < t->nparams; i++)
free(t->params[i + 1]);
free(t->params);
free(t);
}
static void cmd_err_reply(struct rtty *rtty, const char *token, int err)
{
char str[256] = "";
snprintf(str, sizeof(str) - 1, "{\"token\":\"%s\","
"\"attrs\":{\"err\":%d,\"msg\":\"%s\"}}", token, err, cmderr2str(err));
rtty_send_msg(rtty, MSG_TYPE_CMD, str, strlen(str));
}
static void cmd_reply(struct task *t, int code)
{
size_t len = buffer_length(&t->ob) + buffer_length(&t->eb);
struct rtty *rtty = t->rtty;
char *str, *pos;
int ret;
len = ceil(len * 4.0 / 3) + 200;
str = calloc(1, len);
if (!str) {
cmd_err_reply(t->rtty, t->token, RTTY_CMD_ERR_NOMEM);
return;
}
pos = str;
ret = snprintf(pos, len, "{\"token\":\"%s\","
"\"attrs\":{\"code\":%d,\"stdout\":\"", t->token, code);
len -= ret;
pos += ret;
ret = b64_encode(buffer_data(&t->ob), buffer_length(&t->ob), pos, len);
len -= ret;
pos += ret;
ret = snprintf(pos, len, "\",\"stderr\":\"");
len -= ret;
pos += ret;
ret = b64_encode(buffer_data(&t->eb), buffer_length(&t->eb), pos, len);
len -= ret;
pos += ret;
ret = snprintf(pos, len, "\"}}");
pos += ret;
rtty_send_msg(rtty, MSG_TYPE_CMD, str, pos - str);
free(str);
}
static void ev_child_exit(struct ev_loop *loop, struct ev_child *w, int revents)
{
struct task *t = container_of(w, struct task, cw);
cmd_reply(t, WEXITSTATUS(w->rstatus));
task_free(t);
nrunning--;
if (list_empty(&task_pending))
return;
t = list_first_entry(&task_pending, struct task, list);
if (t) {
list_del(&t->list);
run_task(t);
}
}
static void ev_timer_cb(struct ev_loop *loop, struct ev_timer *w, int revents)
{
struct task *t = container_of(w, struct task, timer);
task_free(t);
nrunning--;
log_err("exec '%s' timeout\n", t->cmd);
}
static void ev_io_stdout_cb(struct ev_loop *loop, struct ev_io *w, int revents)
{
struct task *t = container_of(w, struct task, ioo);
bool eof;
buffer_put_fd(&t->ob, w->fd, -1, &eof);
}
static void ev_io_stderr_cb(struct ev_loop *loop, struct ev_io *w, int revents)
{
struct task *t = container_of(w, struct task, ioe);
bool eof;
buffer_put_fd(&t->eb, w->fd, -1, &eof);
}
static void run_task(struct task *t)
{
int opipe[2];
int epipe[2];
pid_t pid;
int err;
if (pipe2(opipe, O_CLOEXEC | O_NONBLOCK) < 0 ||
pipe2(epipe, O_CLOEXEC | O_NONBLOCK) < 0) {
log_err("pipe2 failed: %s\n", strerror(errno));
err = RTTY_CMD_ERR_SYSERR;
goto ERR;
}
pid = fork();
if (pid < 0) {
log_err("fork: %s\n", strerror(errno));
err = RTTY_CMD_ERR_SYSERR;
goto ERR;
} else if (pid == 0) {
/* Close unused read end */
close(opipe[0]);
close(epipe[0]);
/* Redirect */
dup2(opipe[1], STDOUT_FILENO);
dup2(epipe[1], STDERR_FILENO);
close(opipe[1]);
close(epipe[1]);
if (setuid(t->uid) < 0)
exit(1);
execv(t->cmd, t->params);
} else {
/* Close unused write end */
close(opipe[1]);
close(epipe[1]);
/* Watch child's status */
ev_child_init(&t->cw, ev_child_exit, pid, 0);
ev_child_start(t->rtty->loop, &t->cw);
ev_io_init(&t->ioo, ev_io_stdout_cb, opipe[0], EV_READ);
ev_io_start(t->rtty->loop, &t->ioo);
ev_io_init(&t->ioe, ev_io_stderr_cb, epipe[0], EV_READ);
ev_io_start(t->rtty->loop, &t->ioe);
ev_timer_init(&t->timer, ev_timer_cb, RTTY_CMD_EXEC_TIMEOUT, 0);
ev_timer_start(t->rtty->loop, &t->timer);
nrunning++;
return;
}
ERR:
cmd_err_reply(t->rtty, t->token, err);
task_free(t);
}
static void add_task(struct rtty *rtty, const char *token, uid_t uid, const char *cmd, const char *data)
{
struct task *t;
int i;
t = calloc(1, sizeof(struct task) + strlen(cmd) + 1);
if (!t) {
cmd_err_reply(rtty, token, RTTY_CMD_ERR_NOMEM);
return;
}
t->rtty = rtty;
t->uid = uid;
strcpy(t->cmd, cmd);
strcpy(t->token, token);
t->nparams = *data++;
t->params = calloc(t->nparams + 2, sizeof(char *));
t->params[0] = t->cmd;
for (i = 0; i < t->nparams; i++) {
t->params[i + 1] = strdup(data);
data += strlen(t->params[i + 1]) + 1;
}
if (nrunning < RTTY_CMD_MAX_RUNNING)
run_task(t);
else
list_add_tail(&t->list, &task_pending);
}
void run_command(struct rtty *rtty, const char *data)
{
const char *username = data;
const char *password = username + strlen(username) + 1;
const char *cmd = password + strlen(password) + 1;
const char *token = cmd + strlen(cmd) + 1;
struct passwd *pw;
int err = 0;
data = token + strlen(token) + 1;
if (!username[0] || !login_test(username, password)) {
err = RTTY_CMD_ERR_PERMIT;
goto ERR;
}
pw = getpwnam(username);
if (!pw) {
err = RTTY_CMD_ERR_PERMIT;
goto ERR;
}
cmd = cmd_lookup(cmd);
if (!cmd) {
err = RTTY_CMD_ERR_NOT_FOUND;
goto ERR;
}
add_task(rtty, token, pw->pw_uid, cmd, data);
return;
ERR:
cmd_err_reply(rtty, token, err);
}