2 * rpcd - UBUS RPC server
4 * Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
5 * Copyright (C) 2016 Luka Perkov <luka@openwrt.org>
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
33 #include <libubox/blobmsg.h>
34 #include <libubox/md5.h>
35 #include <libubox/ustream.h>
36 #include <libubox/utils.h>
38 #include <rpcd/plugin.h>
40 /* limit of sys & proc files */
41 #define RPC_FILE_MIN_SIZE (4096)
43 /* limit of regular files and command output data */
44 #define RPC_FILE_MAX_SIZE (4096 * 64)
46 #define ustream_for_each_read_buffer(stream, ptr, len) \
47 for (ptr = ustream_get_read_buf(stream, &len); \
48 ptr != NULL && len > 0; \
49 ustream_consume(stream, len), ptr = ustream_get_read_buf(stream, &len))
51 #define ustream_declare(us, fd, name) \
52 us.stream.string_data = true; \
53 us.stream.r.buffer_len = 4096; \
54 us.stream.r.max_buffers = RPC_FILE_MAX_SIZE / 4096; \
55 us.stream.notify_read = rpc_file_##name##_read_cb; \
56 us.stream.notify_state = rpc_file_##name##_state_cb; \
57 ustream_fd_init(&us, fd);
59 static const struct rpc_daemon_ops *ops;
61 struct rpc_file_exec_context {
62 struct ubus_context *context;
63 struct ubus_request_data request;
64 struct uloop_timeout timeout;
65 struct uloop_process process;
66 struct ustream_fd opipe;
67 struct ustream_fd epipe;
72 static struct blob_buf buf;
73 static char *canonpath;
81 static const struct blobmsg_policy rpc_file_R_policy[__RPC_F_R_MAX] = {
82 [RPC_F_R_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
83 [RPC_F_R_SESSION] = { .name = "ubus_rpc_session",
84 .type = BLOBMSG_TYPE_STRING },
94 static const struct blobmsg_policy rpc_file_RB_policy[__RPC_F_RB_MAX] = {
95 [RPC_F_RB_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
96 [RPC_F_RB_BASE64] = { .name = "base64", .type = BLOBMSG_TYPE_BOOL },
97 [RPC_F_RB_SESSION] = { .name = "ubus_rpc_session",
98 .type = BLOBMSG_TYPE_STRING },
111 static const struct blobmsg_policy rpc_file_RW_policy[__RPC_F_RW_MAX] = {
112 [RPC_F_RW_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
113 [RPC_F_RW_DATA] = { .name = "data", .type = BLOBMSG_TYPE_STRING },
114 [RPC_F_RW_APPEND] = { .name = "append", .type = BLOBMSG_TYPE_BOOL },
115 [RPC_F_RW_MODE] = { .name = "mode", .type = BLOBMSG_TYPE_INT32 },
116 [RPC_F_RW_BASE64] = { .name = "base64", .type = BLOBMSG_TYPE_BOOL },
117 [RPC_F_RW_SESSION] = { .name = "ubus_rpc_session",
118 .type = BLOBMSG_TYPE_STRING },
129 static const struct blobmsg_policy rpc_exec_policy[__RPC_E_MAX] = {
130 [RPC_E_CMD] = { .name = "command", .type = BLOBMSG_TYPE_STRING },
131 [RPC_E_PARM] = { .name = "params", .type = BLOBMSG_TYPE_ARRAY },
132 [RPC_E_ENV] = { .name = "env", .type = BLOBMSG_TYPE_TABLE },
133 [RPC_E_SESSION] = { .name = "ubus_rpc_session",
134 .type = BLOBMSG_TYPE_STRING },
137 static const char *d_types[] = {
140 [DT_DIR] = "directory",
142 [DT_LNK] = "symlink",
144 [DT_SOCK] = "socket",
145 [DT_UNKNOWN] = "unknown",
150 rpc_errno_status(void)
155 return UBUS_STATUS_PERMISSION_DENIED;
158 return UBUS_STATUS_INVALID_ARGUMENT;
161 return UBUS_STATUS_NOT_FOUND;
164 return UBUS_STATUS_INVALID_ARGUMENT;
167 return UBUS_STATUS_UNKNOWN_ERROR;
172 rpc_file_access(const struct blob_attr *sid,
173 const char *path, const char *perm)
178 return ops->session_access(blobmsg_data(sid), "file", path, perm);
182 rpc_canonicalize_path(const char *path)
187 if (path == NULL || *path == '\0')
190 if (canonpath != NULL)
193 canonpath = strdup(path);
195 if (canonpath == NULL)
199 for (cp = canonpath, p = path; *p != '\0'; ) {
203 /* skip repeating / */
212 if ((p[2] == '\0') || (p[2] == '/')) {
217 /* collapse /x/../ */
218 if ((p[2] == '.') && ((p[3] == '\0') || (p[3] == '/'))) {
219 while ((cp > canonpath) && (*--cp != '/'))
231 /* remove trailing slash if not root / */
232 if ((cp > canonpath + 1) && (cp[-1] == '/'))
234 else if (cp == canonpath)
242 static struct blob_attr **
243 __rpc_check_path(const struct blobmsg_policy *policy, size_t policy_len,
244 int policy_path_idx, int policy_sid_idx, const char *perm,
245 struct blob_attr *msg, char **path, struct stat *s)
247 static struct blob_attr *tb[__RPC_F_RW_MAX]; /* largest _MAX constant */
249 blobmsg_parse(policy, policy_len, tb, blob_data(msg), blob_len(msg));
251 if (!tb[policy_path_idx])
257 *path = rpc_canonicalize_path(blobmsg_get_string(tb[policy_path_idx]));
265 if (!rpc_file_access(tb[policy_sid_idx], *path, perm))
271 if (s != NULL && stat(*path, s) != 0)
277 #define rpc_check_path(msg, policy_selector, perm, path, s) \
278 __rpc_check_path(rpc_file_ ## policy_selector ## _policy, \
279 ARRAY_SIZE(rpc_file_ ## policy_selector ## _policy), \
280 RPC_F_ ## policy_selector ## _PATH, \
281 RPC_F_ ## policy_selector ## _SESSION, \
285 rpc_file_read(struct ubus_context *ctx, struct ubus_object *obj,
286 struct ubus_request_data *req, const char *method,
287 struct blob_attr *msg)
289 struct blob_attr **tb;
297 tb = rpc_check_path(msg, RB, "read", &path, &s);
300 return rpc_errno_status();
302 if (s.st_size >= RPC_FILE_MAX_SIZE)
303 return UBUS_STATUS_NOT_SUPPORTED;
305 if ((fd = open(path, O_RDONLY)) < 0)
306 return rpc_errno_status();
308 /* some sysfs files do not report a length */
310 s.st_size = RPC_FILE_MIN_SIZE;
312 blob_buf_init(&buf, 0);
314 if (tb[RPC_F_RB_BASE64])
315 base64 = blobmsg_get_bool(tb[RPC_F_RB_BASE64]);
319 len = B64_ENCODE_LEN(s.st_size);
320 wbuf = blobmsg_alloc_string_buffer(&buf, "data", len);
324 rv = UBUS_STATUS_UNKNOWN_ERROR;
328 if ((len = read(fd, wbuf, s.st_size)) <= 0)
330 rv = UBUS_STATUS_NO_DATA;
336 uint8_t *data = calloc(len, sizeof(uint8_t));
339 rv = UBUS_STATUS_UNKNOWN_ERROR;
342 memcpy(data, wbuf, len);
344 len = b64_encode(data, len, wbuf, B64_ENCODE_LEN(len));
348 rv = UBUS_STATUS_UNKNOWN_ERROR;
353 *(wbuf + len) = '\0';
354 blobmsg_add_string_buffer(&buf);
356 ubus_send_reply(ctx, req, buf.head);
366 rpc_file_write(struct ubus_context *ctx, struct ubus_object *obj,
367 struct ubus_request_data *req, const char *method,
368 struct blob_attr *msg)
370 struct blob_attr **tb;
371 int append = O_TRUNC;
372 mode_t prev_mode, mode = 0666;
376 ssize_t data_len = 0;
378 tb = rpc_check_path(msg, RW, "write", &path, NULL);
381 return rpc_errno_status();
383 if (!tb[RPC_F_RW_DATA])
384 return UBUS_STATUS_INVALID_ARGUMENT;
386 data = blobmsg_data(tb[RPC_F_RW_DATA]);
387 data_len = blobmsg_data_len(tb[RPC_F_RW_DATA]) - 1;
389 if (tb[RPC_F_RW_APPEND] && blobmsg_get_bool(tb[RPC_F_RW_APPEND]))
392 if (tb[RPC_F_RW_MODE])
393 mode = blobmsg_get_u32(tb[RPC_F_RW_MODE]);
395 prev_mode = umask(0);
396 fd = open(path, O_CREAT | O_WRONLY | append, mode);
399 return rpc_errno_status();
401 if (tb[RPC_F_RW_BASE64] && blobmsg_get_bool(tb[RPC_F_RW_BASE64]))
403 data_len = b64_decode(data, data, data_len);
406 rv = UBUS_STATUS_UNKNOWN_ERROR;
411 if (write(fd, data, data_len) < 0)
422 return rpc_errno_status();
428 rpc_file_md5(struct ubus_context *ctx, struct ubus_object *obj,
429 struct ubus_request_data *req, const char *method,
430 struct blob_attr *msg)
438 if (!rpc_check_path(msg, R, "read", &path, &s))
439 return rpc_errno_status();
441 if (!S_ISREG(s.st_mode))
442 return UBUS_STATUS_NOT_SUPPORTED;
444 if ((rv = md5sum(path, md5)) <= 0)
445 return rpc_errno_status();
447 blob_buf_init(&buf, 0);
448 wbuf = blobmsg_alloc_string_buffer(&buf, "md5", 33);
450 for (i = 0; i < 16; i++)
451 sprintf(wbuf + (i * 2), "%02x", (uint8_t) md5[i]);
453 blobmsg_add_string_buffer(&buf);
454 ubus_send_reply(ctx, req, buf.head);
457 return UBUS_STATUS_OK;
461 _rpc_file_add_stat(struct stat *s)
465 type = S_ISREG(s->st_mode) ? DT_REG :
466 S_ISDIR(s->st_mode) ? DT_DIR :
467 S_ISCHR(s->st_mode) ? DT_CHR :
468 S_ISBLK(s->st_mode) ? DT_BLK :
469 S_ISFIFO(s->st_mode) ? DT_FIFO :
470 S_ISLNK(s->st_mode) ? DT_LNK :
471 S_ISSOCK(s->st_mode) ? DT_SOCK :
474 blobmsg_add_string(&buf, "type", d_types[type]);
475 blobmsg_add_u32(&buf, "size", s->st_size);
476 blobmsg_add_u32(&buf, "mode", s->st_mode);
477 blobmsg_add_u32(&buf, "atime", s->st_atime);
478 blobmsg_add_u32(&buf, "mtime", s->st_mtime);
479 blobmsg_add_u32(&buf, "ctime", s->st_ctime);
480 blobmsg_add_u32(&buf, "inode", s->st_ino);
481 blobmsg_add_u32(&buf, "uid", s->st_uid);
482 blobmsg_add_u32(&buf, "gid", s->st_gid);
486 rpc_file_list(struct ubus_context *ctx, struct ubus_object *obj,
487 struct ubus_request_data *req, const char *method,
488 struct blob_attr *msg)
494 char *path, *entrypath;
496 if (!rpc_check_path(msg, R, "list", &path, NULL))
497 return rpc_errno_status();
499 if ((fd = opendir(path)) == NULL)
500 return rpc_errno_status();
502 blob_buf_init(&buf, 0);
503 c = blobmsg_open_array(&buf, "entries");
505 while ((e = readdir(fd)) != NULL)
507 if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, ".."))
510 if (asprintf(&entrypath, "%s/%s", path, e->d_name) < 0)
513 if (!stat(entrypath, &s))
515 d = blobmsg_open_table(&buf, NULL);
516 blobmsg_add_string(&buf, "name", e->d_name);
517 _rpc_file_add_stat(&s);
518 blobmsg_close_table(&buf, d);
526 blobmsg_close_array(&buf, c);
527 ubus_send_reply(ctx, req, buf.head);
534 rpc_file_stat(struct ubus_context *ctx, struct ubus_object *obj,
535 struct ubus_request_data *req, const char *method,
536 struct blob_attr *msg)
541 if (!rpc_check_path(msg, R, "list", &path, &s))
542 return rpc_errno_status();
544 blob_buf_init(&buf, 0);
546 blobmsg_add_string(&buf, "path", path);
547 _rpc_file_add_stat(&s);
549 ubus_send_reply(ctx, req, buf.head);
556 rpc_file_remove_recursive(const char *path);
559 rpc_file_remove_recursive(const char *path)
567 if ((fd = opendir(path)) == NULL)
568 return rpc_errno_status();
570 for (e = readdir(fd); e != NULL && err == 0; e = readdir(fd))
572 if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, ".."))
575 if (asprintf(&entrypath, "%s/%s", path, e->d_name) >= 0)
577 if (!lstat(entrypath, &s))
579 if (S_ISDIR(s.st_mode))
580 err = rpc_file_remove_recursive(entrypath);
581 else if (unlink(entrypath))
582 err = rpc_errno_status();
589 err = UBUS_STATUS_UNKNOWN_ERROR;
595 if (!err && rmdir(path))
596 return rpc_errno_status();
602 rpc_file_remove(struct ubus_context *ctx, struct ubus_object *obj,
603 struct ubus_request_data *req, const char *method,
604 struct blob_attr *msg)
609 if (!rpc_check_path(msg, R, "write", &path, NULL))
610 return rpc_errno_status();
613 return rpc_errno_status();
615 if (S_ISDIR(s.st_mode))
616 return rpc_file_remove_recursive(path);
619 return rpc_errno_status();
625 rpc_file_exec_lookup(const char *cmd)
628 int plen = 0, clen = strlen(cmd) + 1;
630 static char path[PATH_MAX];
632 if (!stat(cmd, &s) && S_ISREG(s.st_mode))
635 search = getenv("PATH");
638 search = "/bin:/usr/bin:/sbin:/usr/sbin";
644 if (*p != ':' && *p != '\0')
649 if ((plen + clen) >= sizeof(path))
652 strncpy(path, search, plen);
653 sprintf(path + plen, "/%s", cmd);
655 if (!stat(path, &s) && S_ISREG(s.st_mode))
667 rpc_ustream_to_blobmsg(struct ustream *s, const char *name)
672 if ((len = ustream_pending_data(s, false)) > 0)
674 wbuf = blobmsg_alloc_string_buffer(&buf, name, len + 1);
679 ustream_for_each_read_buffer(s, rbuf, len)
681 memcpy(wbuf, rbuf, len);
686 blobmsg_add_string_buffer(&buf);
691 rpc_file_exec_reply(struct rpc_file_exec_context *c, int rv)
693 uloop_timeout_cancel(&c->timeout);
694 uloop_process_delete(&c->process);
696 if (rv == UBUS_STATUS_OK)
698 blob_buf_init(&buf, 0);
700 blobmsg_add_u32(&buf, "code", WEXITSTATUS(c->stat));
702 rpc_ustream_to_blobmsg(&c->opipe.stream, "stdout");
703 rpc_ustream_to_blobmsg(&c->epipe.stream, "stderr");
705 ubus_send_reply(c->context, &c->request, buf.head);
709 ubus_complete_deferred_request(c->context, &c->request, rv);
711 ustream_free(&c->opipe.stream);
712 ustream_free(&c->epipe.stream);
714 close(c->opipe.fd.fd);
715 close(c->epipe.fd.fd);
721 rpc_file_exec_timeout_cb(struct uloop_timeout *t)
723 struct rpc_file_exec_context *c =
724 container_of(t, struct rpc_file_exec_context, timeout);
726 kill(c->process.pid, SIGKILL);
727 rpc_file_exec_reply(c, UBUS_STATUS_TIMEOUT);
731 rpc_file_exec_process_cb(struct uloop_process *p, int stat)
733 struct rpc_file_exec_context *c =
734 container_of(p, struct rpc_file_exec_context, process);
738 ustream_poll(&c->opipe.stream);
739 ustream_poll(&c->epipe.stream);
743 rpc_file_exec_opipe_read_cb(struct ustream *s, int bytes)
745 struct rpc_file_exec_context *c =
746 container_of(s, struct rpc_file_exec_context, opipe.stream);
748 if (ustream_read_buf_full(s))
749 rpc_file_exec_reply(c, UBUS_STATUS_NOT_SUPPORTED);
753 rpc_file_exec_epipe_read_cb(struct ustream *s, int bytes)
755 struct rpc_file_exec_context *c =
756 container_of(s, struct rpc_file_exec_context, epipe.stream);
758 if (ustream_read_buf_full(s))
759 rpc_file_exec_reply(c, UBUS_STATUS_NOT_SUPPORTED);
763 rpc_file_exec_opipe_state_cb(struct ustream *s)
765 struct rpc_file_exec_context *c =
766 container_of(s, struct rpc_file_exec_context, opipe.stream);
768 if (c->opipe.stream.eof && c->epipe.stream.eof)
769 rpc_file_exec_reply(c, UBUS_STATUS_OK);
773 rpc_file_exec_epipe_state_cb(struct ustream *s)
775 struct rpc_file_exec_context *c =
776 container_of(s, struct rpc_file_exec_context, epipe.stream);
778 if (c->opipe.stream.eof && c->epipe.stream.eof)
779 rpc_file_exec_reply(c, UBUS_STATUS_OK);
790 rpc_file_exec_run(const char *cmd, const struct blob_attr *sid,
791 const struct blob_attr *arg, const struct blob_attr *env,
792 struct ubus_context *ctx, struct ubus_request_data *req)
801 struct blob_attr *cur;
804 char *executable, **args, **tmp;
806 struct rpc_file_exec_context *c;
808 cmd = rpc_file_exec_lookup(cmd);
811 return UBUS_STATUS_NOT_FOUND;
813 executable = rpc_canonicalize_path(cmd);
815 if (executable == NULL)
816 return UBUS_STATUS_UNKNOWN_ERROR;
818 if (!rpc_file_access(sid, executable, "exec"))
819 return UBUS_STATUS_PERMISSION_DENIED;
821 c = malloc(sizeof(*c));
824 return UBUS_STATUS_UNKNOWN_ERROR;
832 switch ((pid = fork()))
840 devnull = open("/dev/null", O_RDWR);
843 return UBUS_STATUS_UNKNOWN_ERROR;
849 rpc_fdclose(devnull);
850 rpc_fdclose(opipe[0]);
851 rpc_fdclose(opipe[1]);
852 rpc_fdclose(epipe[0]);
853 rpc_fdclose(epipe[1]);
856 args = malloc(sizeof(char *) * arglen);
859 return UBUS_STATUS_UNKNOWN_ERROR;
861 args[0] = (char *)executable;
866 blobmsg_for_each_attr(cur, arg, rem)
868 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
874 return UBUS_STATUS_INVALID_ARGUMENT;
878 tmp = realloc(args, sizeof(char *) * arglen);
883 return UBUS_STATUS_UNKNOWN_ERROR;
887 args[arglen-2] = blobmsg_data(cur);
888 args[arglen-1] = NULL;
894 blobmsg_for_each_attr(cur, env, rem)
896 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
899 setenv(blobmsg_name(cur), blobmsg_data(cur), 1);
903 if (execv(executable, args))
904 return rpc_errno_status();
907 memset(c, 0, sizeof(*c));
909 ustream_declare(c->opipe, opipe[0], exec_opipe);
910 ustream_declare(c->epipe, epipe[0], exec_epipe);
912 c->process.pid = pid;
913 c->process.cb = rpc_file_exec_process_cb;
914 uloop_process_add(&c->process);
916 c->timeout.cb = rpc_file_exec_timeout_cb;
917 uloop_timeout_set(&c->timeout, *ops->exec_timeout);
923 ubus_defer_request(ctx, req, &c->request);
926 return UBUS_STATUS_OK;
938 return rpc_errno_status();
942 rpc_file_exec(struct ubus_context *ctx, struct ubus_object *obj,
943 struct ubus_request_data *req, const char *method,
944 struct blob_attr *msg)
946 struct blob_attr *tb[__RPC_E_MAX];
948 blobmsg_parse(rpc_exec_policy, __RPC_E_MAX, tb,
949 blob_data(msg), blob_len(msg));
952 return UBUS_STATUS_INVALID_ARGUMENT;
954 return rpc_file_exec_run(blobmsg_data(tb[RPC_E_CMD]), tb[RPC_E_SESSION],
955 tb[RPC_E_PARM], tb[RPC_E_ENV], ctx, req);
960 rpc_file_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
962 static const struct ubus_method file_methods[] = {
963 UBUS_METHOD("read", rpc_file_read, rpc_file_RB_policy),
964 UBUS_METHOD("write", rpc_file_write, rpc_file_RW_policy),
965 UBUS_METHOD("list", rpc_file_list, rpc_file_R_policy),
966 UBUS_METHOD("stat", rpc_file_stat, rpc_file_R_policy),
967 UBUS_METHOD("md5", rpc_file_md5, rpc_file_R_policy),
968 UBUS_METHOD("remove", rpc_file_remove, rpc_file_R_policy),
969 UBUS_METHOD("exec", rpc_file_exec, rpc_exec_policy),
972 static struct ubus_object_type file_type =
973 UBUS_OBJECT_TYPE("luci-rpc-file", file_methods);
975 static struct ubus_object obj = {
978 .methods = file_methods,
979 .n_methods = ARRAY_SIZE(file_methods),
984 return ubus_add_object(ctx, &obj);
987 struct rpc_plugin rpc_plugin = {
988 .init = rpc_file_api_init