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.
31 #include <libubox/blobmsg.h>
32 #include <libubox/md5.h>
33 #include <libubox/ustream.h>
34 #include <libubox/utils.h>
36 #include <rpcd/plugin.h>
38 /* limit of sys & proc files */
39 #define RPC_FILE_MIN_SIZE (128)
41 /* limit of regular files and command output data */
42 #define RPC_FILE_MAX_SIZE (4096 * 64)
44 #define ustream_for_each_read_buffer(stream, ptr, len) \
45 for (ptr = ustream_get_read_buf(stream, &len); \
46 ptr != NULL && len > 0; \
47 ustream_consume(stream, len), ptr = ustream_get_read_buf(stream, &len))
49 #define ustream_declare(us, fd, name) \
50 us.stream.string_data = true; \
51 us.stream.r.buffer_len = 4096; \
52 us.stream.r.max_buffers = RPC_FILE_MAX_SIZE / 4096; \
53 us.stream.notify_read = rpc_file_##name##_read_cb; \
54 us.stream.notify_state = rpc_file_##name##_state_cb; \
55 ustream_fd_init(&us, fd);
57 static const struct rpc_daemon_ops *ops;
59 struct rpc_file_exec_context {
60 struct ubus_context *context;
61 struct ubus_request_data request;
62 struct uloop_timeout timeout;
63 struct uloop_process process;
64 struct ustream_fd opipe;
65 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_rb_policy[__RPC_F_RB_MAX] = {
88 [RPC_F_RB_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
89 [RPC_F_RB_BASE64] = { .name = "base64", .type = BLOBMSG_TYPE_BOOL },
101 static const struct blobmsg_policy rpc_file_rw_policy[__RPC_F_RW_MAX] = {
102 [RPC_F_RW_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
103 [RPC_F_RW_DATA] = { .name = "data", .type = BLOBMSG_TYPE_STRING },
104 [RPC_F_RW_APPEND] = { .name = "append", .type = BLOBMSG_TYPE_BOOL },
105 [RPC_F_RW_MODE] = { .name = "mode", .type = BLOBMSG_TYPE_INT32 },
106 [RPC_F_RW_BASE64] = { .name = "base64", .type = BLOBMSG_TYPE_BOOL },
116 static const struct blobmsg_policy rpc_exec_policy[__RPC_E_MAX] = {
117 [RPC_E_CMD] = { .name = "command", .type = BLOBMSG_TYPE_STRING },
118 [RPC_E_PARM] = { .name = "params", .type = BLOBMSG_TYPE_ARRAY },
119 [RPC_E_ENV] = { .name = "env", .type = BLOBMSG_TYPE_TABLE },
122 static const char *d_types[] = {
125 [DT_DIR] = "directory",
127 [DT_LNK] = "symlink",
129 [DT_SOCK] = "socket",
130 [DT_UNKNOWN] = "unknown",
135 rpc_errno_status(void)
140 return UBUS_STATUS_PERMISSION_DENIED;
143 return UBUS_STATUS_INVALID_ARGUMENT;
146 return UBUS_STATUS_NOT_FOUND;
149 return UBUS_STATUS_INVALID_ARGUMENT;
152 return UBUS_STATUS_UNKNOWN_ERROR;
156 static struct blob_attr **
157 rpc_check_path(struct blob_attr *msg, char **path, struct stat *s)
159 static struct blob_attr *tb[__RPC_F_R_MAX];
161 blobmsg_parse(rpc_file_r_policy, __RPC_F_R_MAX, tb, blob_data(msg), blob_len(msg));
163 if (!tb[RPC_F_R_PATH])
169 *path = blobmsg_data(tb[RPC_F_R_PATH]);
178 rpc_file_read(struct ubus_context *ctx, struct ubus_object *obj,
179 struct ubus_request_data *req, const char *method,
180 struct blob_attr *msg)
182 static struct blob_attr *tb[__RPC_F_RB_MAX];
190 blobmsg_parse(rpc_file_rb_policy, __RPC_F_RB_MAX, tb, blob_data(msg), blob_len(msg));
192 if (!tb[RPC_F_RB_PATH])
193 return rpc_errno_status();
195 path = blobmsg_data(tb[RPC_F_RB_PATH]);
198 return rpc_errno_status();
200 if (s.st_size >= RPC_FILE_MAX_SIZE)
201 return UBUS_STATUS_NOT_SUPPORTED;
203 if ((fd = open(path, O_RDONLY)) < 0)
204 return rpc_errno_status();
206 /* some sysfs files do not report a length */
208 s.st_size = RPC_FILE_MIN_SIZE;
210 blob_buf_init(&buf, 0);
212 if (tb[RPC_F_RB_BASE64])
213 base64 = blobmsg_get_bool(tb[RPC_F_RB_BASE64]);
217 len = B64_ENCODE_LEN(s.st_size);
218 wbuf = blobmsg_alloc_string_buffer(&buf, "data", len);
222 rv = UBUS_STATUS_UNKNOWN_ERROR;
226 if ((len = read(fd, wbuf, s.st_size)) <= 0)
228 rv = UBUS_STATUS_NO_DATA;
234 uint8_t *data = calloc(len, sizeof(uint8_t));
237 rv = UBUS_STATUS_UNKNOWN_ERROR;
240 memcpy(data, wbuf, len);
242 len = b64_encode(data, len, wbuf, B64_ENCODE_LEN(len));
246 rv = UBUS_STATUS_UNKNOWN_ERROR;
251 *(wbuf + len) = '\0';
252 blobmsg_add_string_buffer(&buf);
254 ubus_send_reply(ctx, req, buf.head);
264 rpc_file_write(struct ubus_context *ctx, struct ubus_object *obj,
265 struct ubus_request_data *req, const char *method,
266 struct blob_attr *msg)
268 struct blob_attr *tb[__RPC_F_RW_MAX];
269 int append = O_TRUNC;
270 mode_t prev_mode, mode = 0666;
273 ssize_t data_len = 0;
275 blobmsg_parse(rpc_file_rw_policy, __RPC_F_RW_MAX, tb,
276 blob_data(msg), blob_len(msg));
278 if (!tb[RPC_F_RW_PATH] || !tb[RPC_F_RW_DATA])
279 return UBUS_STATUS_INVALID_ARGUMENT;
281 data = blobmsg_data(tb[RPC_F_RW_DATA]);
282 data_len = blobmsg_data_len(tb[RPC_F_RW_DATA]) - 1;
284 if (tb[RPC_F_RW_APPEND] && blobmsg_get_bool(tb[RPC_F_RW_APPEND]))
287 if (tb[RPC_F_RW_MODE])
288 mode = blobmsg_get_u32(tb[RPC_F_RW_MODE]);
290 prev_mode = umask(0);
291 fd = open(blobmsg_data(tb[RPC_F_RW_PATH]), O_CREAT | O_WRONLY | append, mode);
294 return rpc_errno_status();
296 if (tb[RPC_F_RW_BASE64] && blobmsg_get_bool(tb[RPC_F_RW_BASE64]))
298 data_len = b64_decode(data, data, data_len);
301 rv = UBUS_STATUS_UNKNOWN_ERROR;
306 if (write(fd, data, data_len) < 0)
317 return rpc_errno_status();
323 rpc_file_md5(struct ubus_context *ctx, struct ubus_object *obj,
324 struct ubus_request_data *req, const char *method,
325 struct blob_attr *msg)
333 if (!rpc_check_path(msg, &path, &s))
334 return rpc_errno_status();
336 if (!S_ISREG(s.st_mode))
337 return UBUS_STATUS_NOT_SUPPORTED;
339 if ((rv = md5sum(path, md5)) <= 0)
340 return rpc_errno_status();
342 blob_buf_init(&buf, 0);
343 wbuf = blobmsg_alloc_string_buffer(&buf, "md5", 33);
345 for (i = 0; i < 16; i++)
346 sprintf(wbuf + (i * 2), "%02x", (uint8_t) md5[i]);
348 blobmsg_add_string_buffer(&buf);
349 ubus_send_reply(ctx, req, buf.head);
352 return UBUS_STATUS_OK;
356 rpc_file_list(struct ubus_context *ctx, struct ubus_object *obj,
357 struct ubus_request_data *req, const char *method,
358 struct blob_attr *msg)
366 if (!rpc_check_path(msg, &path, &s))
367 return rpc_errno_status();
369 if ((fd = opendir(path)) == NULL)
370 return rpc_errno_status();
372 blob_buf_init(&buf, 0);
373 c = blobmsg_open_array(&buf, "entries");
375 while ((e = readdir(fd)) != NULL)
377 if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, ".."))
380 d = blobmsg_open_table(&buf, NULL);
381 blobmsg_add_string(&buf, "name", e->d_name);
382 blobmsg_add_string(&buf, "type", d_types[e->d_type]);
383 blobmsg_close_table(&buf, d);
388 blobmsg_close_array(&buf, c);
389 ubus_send_reply(ctx, req, buf.head);
396 rpc_file_stat(struct ubus_context *ctx, struct ubus_object *obj,
397 struct ubus_request_data *req, const char *method,
398 struct blob_attr *msg)
404 if (!rpc_check_path(msg, &path, &s))
405 return rpc_errno_status();
407 blob_buf_init(&buf, 0);
409 type = S_ISREG(s.st_mode) ? DT_REG :
410 S_ISDIR(s.st_mode) ? DT_DIR :
411 S_ISCHR(s.st_mode) ? DT_CHR :
412 S_ISBLK(s.st_mode) ? DT_BLK :
413 S_ISFIFO(s.st_mode) ? DT_FIFO :
414 S_ISLNK(s.st_mode) ? DT_LNK :
415 S_ISSOCK(s.st_mode) ? DT_SOCK :
418 blobmsg_add_string(&buf, "path", path);
419 blobmsg_add_string(&buf, "type", d_types[type]);
420 blobmsg_add_u32(&buf, "size", s.st_size);
421 blobmsg_add_u32(&buf, "mode", s.st_mode);
422 blobmsg_add_u32(&buf, "atime", s.st_atime);
423 blobmsg_add_u32(&buf, "mtime", s.st_mtime);
424 blobmsg_add_u32(&buf, "ctime", s.st_ctime);
425 blobmsg_add_u32(&buf, "inode", s.st_ino);
426 blobmsg_add_u32(&buf, "uid", s.st_uid);
427 blobmsg_add_u32(&buf, "gid", s.st_gid);
429 ubus_send_reply(ctx, req, buf.head);
436 rpc_file_exec_lookup(const char *cmd)
439 int plen = 0, clen = strlen(cmd) + 1;
441 static char path[PATH_MAX];
443 if (!stat(cmd, &s) && S_ISREG(s.st_mode))
446 search = getenv("PATH");
449 search = "/bin:/usr/bin:/sbin:/usr/sbin";
455 if (*p != ':' && *p != '\0')
460 if ((plen + clen) >= sizeof(path))
463 strncpy(path, search, plen);
464 sprintf(path + plen, "/%s", cmd);
466 if (!stat(path, &s) && S_ISREG(s.st_mode))
478 rpc_ustream_to_blobmsg(struct ustream *s, const char *name)
483 if ((len = ustream_pending_data(s, false)) > 0)
485 wbuf = blobmsg_alloc_string_buffer(&buf, name, len + 1);
490 ustream_for_each_read_buffer(s, rbuf, len)
492 memcpy(wbuf, rbuf, len);
497 blobmsg_add_string_buffer(&buf);
502 rpc_file_exec_reply(struct rpc_file_exec_context *c, int rv)
504 uloop_timeout_cancel(&c->timeout);
505 uloop_process_delete(&c->process);
507 if (rv == UBUS_STATUS_OK)
509 blob_buf_init(&buf, 0);
511 blobmsg_add_u32(&buf, "code", WEXITSTATUS(c->stat));
513 rpc_ustream_to_blobmsg(&c->opipe.stream, "stdout");
514 rpc_ustream_to_blobmsg(&c->epipe.stream, "stderr");
516 ubus_send_reply(c->context, &c->request, buf.head);
520 ubus_complete_deferred_request(c->context, &c->request, rv);
522 ustream_free(&c->opipe.stream);
523 ustream_free(&c->epipe.stream);
525 close(c->opipe.fd.fd);
526 close(c->epipe.fd.fd);
532 rpc_file_exec_timeout_cb(struct uloop_timeout *t)
534 struct rpc_file_exec_context *c =
535 container_of(t, struct rpc_file_exec_context, timeout);
537 kill(c->process.pid, SIGKILL);
538 rpc_file_exec_reply(c, UBUS_STATUS_TIMEOUT);
542 rpc_file_exec_process_cb(struct uloop_process *p, int stat)
544 struct rpc_file_exec_context *c =
545 container_of(p, struct rpc_file_exec_context, process);
549 ustream_poll(&c->opipe.stream);
550 ustream_poll(&c->epipe.stream);
554 rpc_file_exec_opipe_read_cb(struct ustream *s, int bytes)
556 struct rpc_file_exec_context *c =
557 container_of(s, struct rpc_file_exec_context, opipe.stream);
559 if (ustream_read_buf_full(s))
560 rpc_file_exec_reply(c, UBUS_STATUS_NOT_SUPPORTED);
564 rpc_file_exec_epipe_read_cb(struct ustream *s, int bytes)
566 struct rpc_file_exec_context *c =
567 container_of(s, struct rpc_file_exec_context, epipe.stream);
569 if (ustream_read_buf_full(s))
570 rpc_file_exec_reply(c, UBUS_STATUS_NOT_SUPPORTED);
574 rpc_file_exec_opipe_state_cb(struct ustream *s)
576 struct rpc_file_exec_context *c =
577 container_of(s, struct rpc_file_exec_context, opipe.stream);
579 if (c->opipe.stream.eof && c->epipe.stream.eof)
580 rpc_file_exec_reply(c, UBUS_STATUS_OK);
584 rpc_file_exec_epipe_state_cb(struct ustream *s)
586 struct rpc_file_exec_context *c =
587 container_of(s, struct rpc_file_exec_context, epipe.stream);
589 if (c->opipe.stream.eof && c->epipe.stream.eof)
590 rpc_file_exec_reply(c, UBUS_STATUS_OK);
594 rpc_file_exec_run(const char *cmd,
595 const struct blob_attr *arg, const struct blob_attr *env,
596 struct ubus_context *ctx, struct ubus_request_data *req)
605 struct blob_attr *cur;
610 struct rpc_file_exec_context *c;
612 cmd = rpc_file_exec_lookup(cmd);
615 return UBUS_STATUS_NOT_FOUND;
617 c = malloc(sizeof(*c));
620 return UBUS_STATUS_UNKNOWN_ERROR;
622 if (pipe(opipe) || pipe(epipe))
623 return rpc_errno_status();
625 switch ((pid = fork()))
628 return rpc_errno_status();
633 devnull = open("/dev/null", O_RDWR);
636 return UBUS_STATUS_UNKNOWN_ERROR;
649 args = malloc(sizeof(char *) * arglen);
652 return UBUS_STATUS_UNKNOWN_ERROR;
654 args[0] = (char *)cmd;
659 blobmsg_for_each_attr(cur, arg, rem)
661 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
667 return UBUS_STATUS_INVALID_ARGUMENT;
671 tmp = realloc(args, sizeof(char *) * arglen);
676 return UBUS_STATUS_UNKNOWN_ERROR;
680 args[arglen-2] = blobmsg_data(cur);
681 args[arglen-1] = NULL;
687 blobmsg_for_each_attr(cur, env, rem)
689 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
692 setenv(blobmsg_name(cur), blobmsg_data(cur), 1);
696 if (execv(cmd, args))
697 return rpc_errno_status();
700 memset(c, 0, sizeof(*c));
702 ustream_declare(c->opipe, opipe[0], exec_opipe);
703 ustream_declare(c->epipe, epipe[0], exec_epipe);
705 c->process.pid = pid;
706 c->process.cb = rpc_file_exec_process_cb;
707 uloop_process_add(&c->process);
709 c->timeout.cb = rpc_file_exec_timeout_cb;
710 uloop_timeout_set(&c->timeout, *ops->exec_timeout);
716 ubus_defer_request(ctx, req, &c->request);
719 return UBUS_STATUS_OK;
723 rpc_file_exec(struct ubus_context *ctx, struct ubus_object *obj,
724 struct ubus_request_data *req, const char *method,
725 struct blob_attr *msg)
727 struct blob_attr *tb[__RPC_E_MAX];
729 blobmsg_parse(rpc_exec_policy, __RPC_E_MAX, tb,
730 blob_data(msg), blob_len(msg));
733 return UBUS_STATUS_INVALID_ARGUMENT;
735 return rpc_file_exec_run(blobmsg_data(tb[RPC_E_CMD]),
736 tb[RPC_E_PARM], tb[RPC_E_ENV], ctx, req);
741 rpc_file_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
743 static const struct ubus_method file_methods[] = {
744 UBUS_METHOD("read", rpc_file_read, rpc_file_rb_policy),
745 UBUS_METHOD("write", rpc_file_write, rpc_file_rw_policy),
746 UBUS_METHOD("list", rpc_file_list, rpc_file_r_policy),
747 UBUS_METHOD("stat", rpc_file_stat, rpc_file_r_policy),
748 UBUS_METHOD("md5", rpc_file_md5, rpc_file_r_policy),
749 UBUS_METHOD("exec", rpc_file_exec, rpc_exec_policy),
752 static struct ubus_object_type file_type =
753 UBUS_OBJECT_TYPE("luci-rpc-file", file_methods);
755 static struct ubus_object obj = {
758 .methods = file_methods,
759 .n_methods = ARRAY_SIZE(file_methods),
764 return ubus_add_object(ctx, &obj);
767 struct rpc_plugin rpc_plugin = {
768 .init = rpc_file_api_init