2 * rpcd - UBUS RPC server
4 * Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 #include <libubox/blobmsg.h>
30 #include <libubox/md5.h>
31 #include <libubox/ustream.h>
33 #include <rpcd/plugin.h>
35 /* limit of sys & proc files */
36 #define RPC_FILE_MIN_SIZE (128)
38 /* limit of regular files and command output data */
39 #define RPC_FILE_MAX_SIZE (4096 * 64)
40 #define RPC_FILE_MAX_RUNTIME (3 * 1000)
42 #define ustream_for_each_read_buffer(stream, ptr, len) \
43 for (ptr = ustream_get_read_buf(stream, &len); \
44 ptr != NULL && len > 0; \
45 ustream_consume(stream, len), ptr = ustream_get_read_buf(stream, &len))
47 #define ustream_declare(us, fd, name) \
48 us.stream.string_data = true; \
49 us.stream.r.buffer_len = 4096; \
50 us.stream.r.max_buffers = RPC_FILE_MAX_SIZE / 4096; \
51 us.stream.notify_read = rpc_file_##name##_read_cb; \
52 us.stream.notify_state = rpc_file_##name##_state_cb; \
53 ustream_fd_init(&us, fd);
55 struct rpc_file_exec_context {
56 struct ubus_context *context;
57 struct ubus_request_data request;
58 struct uloop_timeout timeout;
59 struct uloop_process process;
60 struct ustream_fd opipe;
61 struct ustream_fd epipe;
70 static struct blob_buf buf;
77 static const struct blobmsg_policy rpc_file_r_policy[__RPC_F_R_MAX] = {
78 [RPC_F_R_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
87 static const struct blobmsg_policy rpc_file_rw_policy[__RPC_F_RW_MAX] = {
88 [RPC_F_RW_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
89 [RPC_F_RW_DATA] = { .name = "data", .type = BLOBMSG_TYPE_STRING },
99 static const struct blobmsg_policy rpc_exec_policy[__RPC_E_MAX] = {
100 [RPC_E_CMD] = { .name = "command", .type = BLOBMSG_TYPE_STRING },
101 [RPC_E_PARM] = { .name = "params", .type = BLOBMSG_TYPE_ARRAY },
102 [RPC_E_ENV] = { .name = "env", .type = BLOBMSG_TYPE_TABLE },
105 static const char *d_types[] = {
108 [DT_DIR] = "directory",
110 [DT_LNK] = "symlink",
112 [DT_SOCK] = "socket",
113 [DT_UNKNOWN] = "unknown",
118 rpc_errno_status(void)
123 return UBUS_STATUS_PERMISSION_DENIED;
126 return UBUS_STATUS_INVALID_ARGUMENT;
129 return UBUS_STATUS_NOT_FOUND;
132 return UBUS_STATUS_INVALID_ARGUMENT;
135 return UBUS_STATUS_UNKNOWN_ERROR;
139 static struct blob_attr **
140 rpc_check_path(struct blob_attr *msg, char **path, struct stat *s)
142 static struct blob_attr *tb[__RPC_F_R_MAX];
144 blobmsg_parse(rpc_file_r_policy, __RPC_F_R_MAX, tb, blob_data(msg), blob_len(msg));
146 if (!tb[RPC_F_R_PATH])
152 *path = blobmsg_data(tb[RPC_F_R_PATH]);
161 rpc_file_read(struct ubus_context *ctx, struct ubus_object *obj,
162 struct ubus_request_data *req, const char *method,
163 struct blob_attr *msg)
170 if (!rpc_check_path(msg, &path, &s))
171 return rpc_errno_status();
173 if (s.st_size >= RPC_FILE_MAX_SIZE)
174 return UBUS_STATUS_NOT_SUPPORTED;
176 if ((fd = open(path, O_RDONLY)) < 0)
177 return rpc_errno_status();
179 /* some sysfs files do not report a length */
181 s.st_size = RPC_FILE_MIN_SIZE;
183 blob_buf_init(&buf, 0);
185 wbuf = blobmsg_alloc_string_buffer(&buf, "data", s.st_size + 1);
189 rv = UBUS_STATUS_UNKNOWN_ERROR;
193 if ((len = read(fd, wbuf, s.st_size)) <= 0)
195 rv = UBUS_STATUS_NO_DATA;
200 blobmsg_add_string_buffer(&buf);
202 ubus_send_reply(ctx, req, buf.head);
212 rpc_file_write(struct ubus_context *ctx, struct ubus_object *obj,
213 struct ubus_request_data *req, const char *method,
214 struct blob_attr *msg)
217 struct blob_attr *tb[__RPC_F_RW_MAX];
219 blobmsg_parse(rpc_file_rw_policy, __RPC_F_RW_MAX, tb,
220 blob_data(msg), blob_len(msg));
222 if (!tb[RPC_F_RW_PATH] || !tb[RPC_F_RW_DATA])
223 return UBUS_STATUS_INVALID_ARGUMENT;
225 if ((fd = open(blobmsg_data(tb[RPC_F_RW_PATH]), O_CREAT | O_TRUNC | O_WRONLY, 0666)) < 0)
226 return rpc_errno_status();
228 if (write(fd, blobmsg_data(tb[RPC_F_RW_DATA]), blobmsg_data_len(tb[RPC_F_RW_DATA])) < 0)
229 return rpc_errno_status();
232 return rpc_errno_status();
241 rpc_file_md5(struct ubus_context *ctx, struct ubus_object *obj,
242 struct ubus_request_data *req, const char *method,
243 struct blob_attr *msg)
251 if (!rpc_check_path(msg, &path, &s))
252 return rpc_errno_status();
254 if (!S_ISREG(s.st_mode))
255 return UBUS_STATUS_NOT_SUPPORTED;
257 if ((rv = md5sum(path, md5)) <= 0)
258 return rpc_errno_status();
260 blob_buf_init(&buf, 0);
261 wbuf = blobmsg_alloc_string_buffer(&buf, "md5", 33);
263 for (i = 0; i < 16; i++)
264 sprintf(wbuf + (i * 2), "%02x", (uint8_t) md5[i]);
266 blobmsg_add_string_buffer(&buf);
267 ubus_send_reply(ctx, req, buf.head);
270 return UBUS_STATUS_OK;
274 rpc_file_list(struct ubus_context *ctx, struct ubus_object *obj,
275 struct ubus_request_data *req, const char *method,
276 struct blob_attr *msg)
284 if (!rpc_check_path(msg, &path, &s))
285 return rpc_errno_status();
287 if ((fd = opendir(path)) == NULL)
288 return rpc_errno_status();
290 blob_buf_init(&buf, 0);
291 c = blobmsg_open_array(&buf, "entries");
293 while ((e = readdir(fd)) != NULL)
295 if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, ".."))
298 d = blobmsg_open_table(&buf, NULL);
299 blobmsg_add_string(&buf, "name", e->d_name);
300 blobmsg_add_string(&buf, "type", d_types[e->d_type]);
301 blobmsg_close_table(&buf, d);
306 blobmsg_close_array(&buf, c);
307 ubus_send_reply(ctx, req, buf.head);
314 rpc_file_stat(struct ubus_context *ctx, struct ubus_object *obj,
315 struct ubus_request_data *req, const char *method,
316 struct blob_attr *msg)
322 if (!rpc_check_path(msg, &path, &s))
323 return rpc_errno_status();
325 blob_buf_init(&buf, 0);
327 type = S_ISREG(s.st_mode) ? DT_REG :
328 S_ISDIR(s.st_mode) ? DT_DIR :
329 S_ISCHR(s.st_mode) ? DT_CHR :
330 S_ISBLK(s.st_mode) ? DT_BLK :
331 S_ISFIFO(s.st_mode) ? DT_FIFO :
332 S_ISLNK(s.st_mode) ? DT_LNK :
333 S_ISSOCK(s.st_mode) ? DT_SOCK :
336 blobmsg_add_string(&buf, "path", path);
337 blobmsg_add_string(&buf, "type", d_types[type]);
338 blobmsg_add_u32(&buf, "size", s.st_size);
339 blobmsg_add_u32(&buf, "mode", s.st_mode);
340 blobmsg_add_u32(&buf, "atime", s.st_atime);
341 blobmsg_add_u32(&buf, "mtime", s.st_mtime);
342 blobmsg_add_u32(&buf, "ctime", s.st_ctime);
343 blobmsg_add_u32(&buf, "inode", s.st_ino);
344 blobmsg_add_u32(&buf, "uid", s.st_uid);
345 blobmsg_add_u32(&buf, "gid", s.st_gid);
347 ubus_send_reply(ctx, req, buf.head);
354 rpc_file_exec_lookup(const char *cmd)
357 int plen = 0, clen = strlen(cmd) + 1;
359 static char path[PATH_MAX];
361 if (!stat(cmd, &s) && S_ISREG(s.st_mode))
364 search = getenv("PATH");
367 search = "/bin:/usr/bin:/sbin:/usr/sbin";
373 if (*p != ':' && *p != '\0')
378 if ((plen + clen) >= sizeof(path))
381 strncpy(path, search, plen);
382 sprintf(path + plen, "/%s", cmd);
384 if (!stat(path, &s) && S_ISREG(s.st_mode))
396 rpc_ustream_to_blobmsg(struct ustream *s, const char *name)
401 if ((len = ustream_pending_data(s, false)) > 0)
403 wbuf = blobmsg_alloc_string_buffer(&buf, name, len + 1);
408 ustream_for_each_read_buffer(s, rbuf, len)
410 memcpy(wbuf, rbuf, len);
415 blobmsg_add_string_buffer(&buf);
420 rpc_file_exec_reply(struct rpc_file_exec_context *c, int rv)
422 uloop_timeout_cancel(&c->timeout);
423 uloop_process_delete(&c->process);
425 if (rv == UBUS_STATUS_OK)
427 blob_buf_init(&buf, 0);
429 blobmsg_add_u32(&buf, "code", WEXITSTATUS(c->stat));
431 rpc_ustream_to_blobmsg(&c->opipe.stream, "stdout");
432 rpc_ustream_to_blobmsg(&c->epipe.stream, "stderr");
434 ubus_send_reply(c->context, &c->request, buf.head);
438 ubus_complete_deferred_request(c->context, &c->request, rv);
440 ustream_free(&c->opipe.stream);
441 ustream_free(&c->epipe.stream);
443 close(c->opipe.fd.fd);
444 close(c->epipe.fd.fd);
450 rpc_file_exec_timeout_cb(struct uloop_timeout *t)
452 struct rpc_file_exec_context *c =
453 container_of(t, struct rpc_file_exec_context, timeout);
455 kill(c->process.pid, SIGKILL);
456 rpc_file_exec_reply(c, UBUS_STATUS_TIMEOUT);
460 rpc_file_exec_process_cb(struct uloop_process *p, int stat)
462 struct rpc_file_exec_context *c =
463 container_of(p, struct rpc_file_exec_context, process);
467 ustream_poll(&c->opipe.stream);
468 ustream_poll(&c->epipe.stream);
472 rpc_file_exec_opipe_read_cb(struct ustream *s, int bytes)
474 struct rpc_file_exec_context *c =
475 container_of(s, struct rpc_file_exec_context, opipe.stream);
477 if (ustream_read_buf_full(s))
478 rpc_file_exec_reply(c, UBUS_STATUS_NOT_SUPPORTED);
482 rpc_file_exec_epipe_read_cb(struct ustream *s, int bytes)
484 struct rpc_file_exec_context *c =
485 container_of(s, struct rpc_file_exec_context, epipe.stream);
487 if (ustream_read_buf_full(s))
488 rpc_file_exec_reply(c, UBUS_STATUS_NOT_SUPPORTED);
492 rpc_file_exec_opipe_state_cb(struct ustream *s)
494 struct rpc_file_exec_context *c =
495 container_of(s, struct rpc_file_exec_context, opipe.stream);
497 if (c->opipe.stream.eof && c->epipe.stream.eof)
498 rpc_file_exec_reply(c, UBUS_STATUS_OK);
502 rpc_file_exec_epipe_state_cb(struct ustream *s)
504 struct rpc_file_exec_context *c =
505 container_of(s, struct rpc_file_exec_context, epipe.stream);
507 if (c->opipe.stream.eof && c->epipe.stream.eof)
508 rpc_file_exec_reply(c, UBUS_STATUS_OK);
512 rpc_file_exec_run(const char *cmd,
513 const struct blob_attr *arg, const struct blob_attr *env,
514 struct ubus_context *ctx, struct ubus_request_data *req)
522 struct blob_attr *cur;
527 struct rpc_file_exec_context *c;
529 cmd = rpc_file_exec_lookup(cmd);
532 return UBUS_STATUS_NOT_FOUND;
534 c = malloc(sizeof(*c));
537 return UBUS_STATUS_UNKNOWN_ERROR;
539 if (pipe(opipe) || pipe(epipe))
540 return rpc_errno_status();
542 switch ((pid = fork()))
545 return rpc_errno_status();
560 args = malloc(sizeof(char *) * arglen);
563 return UBUS_STATUS_UNKNOWN_ERROR;
565 args[0] = (char *)cmd;
570 blobmsg_for_each_attr(cur, arg, rem)
572 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
577 if (!(args = realloc(args, sizeof(char *) * arglen)))
578 return UBUS_STATUS_UNKNOWN_ERROR;
580 args[arglen-2] = blobmsg_data(cur);
581 args[arglen-1] = NULL;
587 blobmsg_for_each_attr(cur, env, rem)
589 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
592 setenv(blobmsg_name(cur), blobmsg_data(cur), 1);
596 if (execv(cmd, args))
597 return rpc_errno_status();
600 memset(c, 0, sizeof(*c));
602 ustream_declare(c->opipe, opipe[0], exec_opipe);
603 ustream_declare(c->epipe, epipe[0], exec_epipe);
605 c->process.pid = pid;
606 c->process.cb = rpc_file_exec_process_cb;
607 uloop_process_add(&c->process);
609 c->timeout.cb = rpc_file_exec_timeout_cb;
610 uloop_timeout_set(&c->timeout, RPC_FILE_MAX_RUNTIME);
616 ubus_defer_request(ctx, req, &c->request);
619 return UBUS_STATUS_OK;
623 rpc_file_exec(struct ubus_context *ctx, struct ubus_object *obj,
624 struct ubus_request_data *req, const char *method,
625 struct blob_attr *msg)
627 struct blob_attr *tb[__RPC_E_MAX];
629 blobmsg_parse(rpc_exec_policy, __RPC_E_MAX, tb,
630 blob_data(msg), blob_len(msg));
633 return UBUS_STATUS_INVALID_ARGUMENT;
635 return rpc_file_exec_run(blobmsg_data(tb[RPC_E_CMD]),
636 tb[RPC_E_PARM], tb[RPC_E_ENV], ctx, req);
641 rpc_file_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
643 static const struct ubus_method file_methods[] = {
644 UBUS_METHOD("read", rpc_file_read, rpc_file_r_policy),
645 UBUS_METHOD("write", rpc_file_write, rpc_file_rw_policy),
646 UBUS_METHOD("list", rpc_file_list, rpc_file_r_policy),
647 UBUS_METHOD("stat", rpc_file_stat, rpc_file_r_policy),
648 UBUS_METHOD("md5", rpc_file_md5, rpc_file_r_policy),
649 UBUS_METHOD("exec", rpc_file_exec, rpc_exec_policy),
652 static struct ubus_object_type file_type =
653 UBUS_OBJECT_TYPE("luci-rpc-file", file_methods);
655 static struct ubus_object obj = {
658 .methods = file_methods,
659 .n_methods = ARRAY_SIZE(file_methods),
662 return ubus_add_object(ctx, &obj);
665 struct rpc_plugin rpc_plugin = {
666 .init = rpc_file_api_init