forked from openwrt/rpcd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rc.c
409 lines (330 loc) · 9.04 KB
/
rc.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// SPDX-License-Identifier: ISC OR MIT
/*
* rpcd - UBUS RPC server
*
* Copyright (C) 2020 Rafał Miłecki <[email protected]>
*/
#include <dirent.h>
#include <fcntl.h>
#include <linux/limits.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <libubox/blobmsg.h>
#include <libubox/ulog.h>
#include <libubox/uloop.h>
#include <libubus.h>
#include <rpcd/rc.h>
#define RC_LIST_EXEC_TIMEOUT_MS 3000
enum {
RC_LIST_NAME,
RC_LIST_SKIP_RUNNING_CHECK,
__RC_LIST_MAX
};
static const struct blobmsg_policy rc_list_policy[] = {
[RC_LIST_NAME] = { "name", BLOBMSG_TYPE_STRING },
[RC_LIST_SKIP_RUNNING_CHECK] = { "skip_running_check", BLOBMSG_TYPE_BOOL },
};
enum {
RC_INIT_NAME,
RC_INIT_ACTION,
__RC_INIT_MAX
};
static const struct blobmsg_policy rc_init_policy[] = {
[RC_INIT_NAME] = { "name", BLOBMSG_TYPE_STRING },
[RC_INIT_ACTION] = { "action", BLOBMSG_TYPE_STRING },
};
struct rc_list_context {
struct uloop_process process;
struct uloop_timeout timeout;
struct ubus_context *ctx;
struct ubus_request_data req;
struct blob_buf *buf;
DIR *dir;
bool skip_running_check;
const char *req_name;
/* Info about currently processed init.d entry */
struct {
char path[PATH_MAX];
const char *d_name;
int start;
int stop;
bool enabled;
bool running;
bool use_procd;
} entry;
};
static void rc_list_readdir(struct rc_list_context *c);
/**
* rc_check_script - check if script is safe to execute as root
*
* Check if it's owned by root and if only root can modify it.
*/
static int rc_check_script(const char *path)
{
struct stat s;
if (stat(path, &s))
return UBUS_STATUS_NOT_FOUND;
if (s.st_uid != 0 || s.st_gid != 0 || !(s.st_mode & S_IXUSR) || (s.st_mode & S_IWOTH))
return UBUS_STATUS_PERMISSION_DENIED;
return UBUS_STATUS_OK;
}
static void rc_list_add_table(struct rc_list_context *c)
{
void *e;
e = blobmsg_open_table(c->buf, c->entry.d_name);
if (c->entry.start >= 0)
blobmsg_add_u16(c->buf, "start", c->entry.start);
if (c->entry.stop >= 0)
blobmsg_add_u16(c->buf, "stop", c->entry.stop);
blobmsg_add_u8(c->buf, "enabled", c->entry.enabled);
if (!c->skip_running_check && c->entry.use_procd)
blobmsg_add_u8(c->buf, "running", c->entry.running);
blobmsg_close_table(c->buf, e);
}
static void rpc_list_exec_timeout_cb(struct uloop_timeout *t)
{
struct rc_list_context *c = container_of(t, struct rc_list_context, timeout);
ULOG_WARN("Timeout waiting for %s\n", c->entry.path);
uloop_process_delete(&c->process);
kill(c->process.pid, SIGKILL);
rc_list_readdir(c);
}
/**
* rc_exec - execute a file and call callback on complete
*/
static int rc_list_exec(struct rc_list_context *c, const char *action, uloop_process_handler cb)
{
pid_t pid;
int err;
int fd;
pid = fork();
switch (pid) {
case -1:
return -errno;
case 0:
if (c->skip_running_check)
exit(-EFAULT);
if (!c->entry.use_procd)
exit(-EOPNOTSUPP);
/* Set stdin, stdout & stderr to /dev/null */
fd = open("/dev/null", O_RDWR);
if (fd >= 0) {
dup2(fd, 0);
dup2(fd, 1);
dup2(fd, 2);
if (fd > 2)
close(fd);
}
uloop_end();
execl(c->entry.path, c->entry.path, action, NULL);
exit(errno);
default:
c->process.pid = pid;
c->process.cb = cb;
err = uloop_process_add(&c->process);
if (err)
return err;
c->timeout.cb = rpc_list_exec_timeout_cb;
err = uloop_timeout_set(&c->timeout, RC_LIST_EXEC_TIMEOUT_MS);
if (err) {
uloop_process_delete(&c->process);
return err;
}
return 0;
}
}
static void rc_list_exec_running_cb(struct uloop_process *p, int stat)
{
struct rc_list_context *c = container_of(p, struct rc_list_context, process);
uloop_timeout_cancel(&c->timeout);
c->entry.running = !stat;
rc_list_add_table(c);
rc_list_readdir(c);
}
static void rc_list_readdir(struct rc_list_context *c)
{
struct dirent *e;
FILE *fp;
e = readdir(c->dir);
/*
* If scanning for a specific script and entry.d_name is set
* we can assume we found a matching one in the previous
* iteration since entry.d_name is set only if a match is found.
*/
if (!e || (c->req_name && c->entry.d_name)) {
closedir(c->dir);
ubus_send_reply(c->ctx, &c->req, c->buf->head);
ubus_complete_deferred_request(c->ctx, &c->req, UBUS_STATUS_OK);
return;
}
if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, ".."))
goto next;
if (c->req_name && strcmp(e->d_name, c->req_name))
goto next;
memset(&c->entry, 0, sizeof(c->entry));
c->entry.start = -1;
c->entry.stop = -1;
snprintf(c->entry.path, sizeof(c->entry.path), "/etc/init.d/%s", e->d_name);
if (rc_check_script(c->entry.path))
goto next;
c->entry.d_name = e->d_name;
fp = fopen(c->entry.path, "r");
if (fp) {
struct stat s;
char path[PATH_MAX];
char line[255];
bool beginning;
int count = 0;
beginning = true;
while ((c->entry.start < 0 || c->entry.stop < 0 ||
(!c->skip_running_check && !c->entry.use_procd)) &&
count <= 10 && fgets(line, sizeof(line), fp)) {
if (beginning) {
if (!strncmp(line, "START=", 6)) {
c->entry.start = strtoul(line + 6, NULL, 0);
} else if (!strncmp(line, "STOP=", 5)) {
c->entry.stop = strtoul(line + 5, NULL, 0);
} else if (!c->skip_running_check && !strncmp(line, "USE_PROCD=", 10)) {
c->entry.use_procd = !!strtoul(line + 10, NULL, 0);
}
count++;
}
beginning = !!strchr(line, '\n');
}
fclose(fp);
if (c->entry.start >= 0) {
snprintf(path, sizeof(path), "/etc/rc.d/S%02d%s", c->entry.start, c->entry.d_name);
if (!stat(path, &s) && (s.st_mode & S_IXUSR))
c->entry.enabled = true;
}
}
if (rc_list_exec(c, "running", rc_list_exec_running_cb))
goto next;
return;
next:
rc_list_readdir(c);
}
/**
* rc_list - allocate listing context and start reading directory
*/
static int rc_list(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_attr *tb[__RC_LIST_MAX];
static struct blob_buf buf;
struct rc_list_context *c;
blobmsg_parse(rc_list_policy, __RC_LIST_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
blob_buf_init(&buf, 0);
c = calloc(1, sizeof(*c));
if (!c)
return UBUS_STATUS_UNKNOWN_ERROR;
c->ctx = ctx;
c->buf = &buf;
c->dir = opendir("/etc/init.d");
if (!c->dir) {
free(c);
return UBUS_STATUS_UNKNOWN_ERROR;
}
if (tb[RC_LIST_SKIP_RUNNING_CHECK])
c->skip_running_check = blobmsg_get_bool(tb[RC_LIST_SKIP_RUNNING_CHECK]);
if (tb[RC_LIST_NAME])
c->req_name = blobmsg_get_string(tb[RC_LIST_NAME]);
ubus_defer_request(ctx, req, &c->req);
rc_list_readdir(c);
return 0; /* Deferred */
}
struct rc_init_context {
struct uloop_process process;
struct ubus_context *ctx;
struct ubus_request_data req;
};
static void rc_init_cb(struct uloop_process *p, int stat)
{
struct rc_init_context *c = container_of(p, struct rc_init_context, process);
ubus_complete_deferred_request(c->ctx, &c->req, UBUS_STATUS_OK);
free(c);
}
static int rc_init(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_attr *tb[__RC_INIT_MAX];
struct rc_init_context *c;
char path[PATH_MAX];
const char *action;
const char *name;
const char *chr;
pid_t pid;
int err;
int fd;
blobmsg_parse(rc_init_policy, __RC_INIT_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
if (!tb[RC_INIT_NAME] || !tb[RC_INIT_ACTION])
return UBUS_STATUS_INVALID_ARGUMENT;
name = blobmsg_get_string(tb[RC_INIT_NAME]);
/* Validate script name */
for (chr = name; (chr = strchr(chr, '.')); chr++) {
if (*(chr + 1) == '.')
return UBUS_STATUS_INVALID_ARGUMENT;
}
if (strchr(name, '/'))
return UBUS_STATUS_INVALID_ARGUMENT;
snprintf(path, sizeof(path), "/etc/init.d/%s", name);
/* Validate script privileges */
err = rc_check_script(path);
if (err)
return err;
action = blobmsg_get_string(tb[RC_INIT_ACTION]);
if (strcmp(action, "disable") &&
strcmp(action, "enable") &&
strcmp(action, "stop") &&
strcmp(action, "start") &&
strcmp(action, "restart") &&
strcmp(action, "reload"))
return UBUS_STATUS_INVALID_ARGUMENT;
c = calloc(1, sizeof(*c));
if (!c)
return UBUS_STATUS_UNKNOWN_ERROR;
pid = fork();
switch (pid) {
case -1:
free(c);
return UBUS_STATUS_UNKNOWN_ERROR;
case 0:
/* Set stdin, stdout & stderr to /dev/null */
fd = open("/dev/null", O_RDWR);
if (fd >= 0) {
dup2(fd, 0);
dup2(fd, 1);
dup2(fd, 2);
if (fd > 2)
close(fd);
}
uloop_end();
execl(path, path, action, NULL);
exit(errno);
default:
c->ctx = ctx;
c->process.pid = pid;
c->process.cb = rc_init_cb;
uloop_process_add(&c->process);
ubus_defer_request(ctx, req, &c->req);
return 0; /* Deferred */
}
}
int rpc_rc_api_init(struct ubus_context *ctx)
{
static const struct ubus_method rc_methods[] = {
UBUS_METHOD("list", rc_list, rc_list_policy),
UBUS_METHOD("init", rc_init, rc_init_policy),
};
static struct ubus_object_type rc_type =
UBUS_OBJECT_TYPE("rc", rc_methods);
static struct ubus_object obj = {
.name = "rc",
.type = &rc_type,
.methods = rc_methods,
.n_methods = ARRAY_SIZE(rc_methods),
};
return ubus_add_object(ctx, &obj);
}