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.
30 #include <libubox/blobmsg.h>
31 #include <libubox/md5.h>
32 #include <libubox/ustream.h>
33 #include <libubox/utils.h>
35 #include <rpcd/plugin.h>
37 /* limit of sys & proc files */
38 #define RPC_FILE_MIN_SIZE (128)
40 /* limit of regular files and command output data */
41 #define RPC_FILE_MAX_SIZE (4096 * 64)
42 #define RPC_FILE_MAX_RUNTIME (3 * 1000)
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 struct rpc_file_exec_context {
58 struct ubus_context *context;
59 struct ubus_request_data request;
60 struct uloop_timeout timeout;
61 struct uloop_process process;
62 struct ustream_fd opipe;
63 struct ustream_fd epipe;
72 static struct blob_buf buf;
79 static const struct blobmsg_policy rpc_file_r_policy[__RPC_F_R_MAX] = {
80 [RPC_F_R_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
89 static const struct blobmsg_policy rpc_file_rb_policy[__RPC_F_RB_MAX] = {
90 [RPC_F_RB_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
91 [RPC_F_RB_BASE64] = { .name = "base64", .type = BLOBMSG_TYPE_BOOL },
103 static const struct blobmsg_policy rpc_file_rw_policy[__RPC_F_RW_MAX] = {
104 [RPC_F_RW_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
105 [RPC_F_RW_DATA] = { .name = "data", .type = BLOBMSG_TYPE_STRING },
106 [RPC_F_RW_APPEND] = { .name = "append", .type = BLOBMSG_TYPE_BOOL },
107 [RPC_F_RW_MODE] = { .name = "mode", .type = BLOBMSG_TYPE_INT32 },
108 [RPC_F_RW_BASE64] = { .name = "base64", .type = BLOBMSG_TYPE_BOOL },
118 static const struct blobmsg_policy rpc_exec_policy[__RPC_E_MAX] = {
119 [RPC_E_CMD] = { .name = "command", .type = BLOBMSG_TYPE_STRING },
120 [RPC_E_PARM] = { .name = "params", .type = BLOBMSG_TYPE_ARRAY },
121 [RPC_E_ENV] = { .name = "env", .type = BLOBMSG_TYPE_TABLE },
124 static const char *d_types[] = {
127 [DT_DIR] = "directory",
129 [DT_LNK] = "symlink",
131 [DT_SOCK] = "socket",
132 [DT_UNKNOWN] = "unknown",
137 rpc_errno_status(void)
142 return UBUS_STATUS_PERMISSION_DENIED;
145 return UBUS_STATUS_INVALID_ARGUMENT;
148 return UBUS_STATUS_NOT_FOUND;
151 return UBUS_STATUS_INVALID_ARGUMENT;
154 return UBUS_STATUS_UNKNOWN_ERROR;
158 static struct blob_attr **
159 rpc_check_path(struct blob_attr *msg, char **path, struct stat *s)
161 static struct blob_attr *tb[__RPC_F_R_MAX];
163 blobmsg_parse(rpc_file_r_policy, __RPC_F_R_MAX, tb, blob_data(msg), blob_len(msg));
165 if (!tb[RPC_F_R_PATH])
171 *path = blobmsg_data(tb[RPC_F_R_PATH]);
180 rpc_file_read(struct ubus_context *ctx, struct ubus_object *obj,
181 struct ubus_request_data *req, const char *method,
182 struct blob_attr *msg)
184 static struct blob_attr *tb[__RPC_F_RB_MAX];
192 blobmsg_parse(rpc_file_rb_policy, __RPC_F_RB_MAX, tb, blob_data(msg), blob_len(msg));
194 if (!tb[RPC_F_RB_PATH])
195 return rpc_errno_status();
197 path = blobmsg_data(tb[RPC_F_RB_PATH]);
200 return rpc_errno_status();
202 if (s.st_size >= RPC_FILE_MAX_SIZE)
203 return UBUS_STATUS_NOT_SUPPORTED;
205 if ((fd = open(path, O_RDONLY)) < 0)
206 return rpc_errno_status();
208 /* some sysfs files do not report a length */
210 s.st_size = RPC_FILE_MIN_SIZE;
212 blob_buf_init(&buf, 0);
214 if (tb[RPC_F_RB_BASE64])
215 base64 = blobmsg_get_bool(tb[RPC_F_RB_BASE64]);
219 len = B64_ENCODE_LEN(s.st_size);
220 wbuf = blobmsg_alloc_string_buffer(&buf, "data", len);
224 rv = UBUS_STATUS_UNKNOWN_ERROR;
228 if ((len = read(fd, wbuf, s.st_size)) <= 0)
230 rv = UBUS_STATUS_NO_DATA;
236 uint8_t *data = calloc(len, sizeof(uint8_t));
239 rv = UBUS_STATUS_UNKNOWN_ERROR;
242 memcpy(data, wbuf, len);
244 len = b64_encode(data, len, wbuf, B64_ENCODE_LEN(len));
248 rv = UBUS_STATUS_UNKNOWN_ERROR;
253 *(wbuf + len) = '\0';
254 blobmsg_add_string_buffer(&buf);
256 ubus_send_reply(ctx, req, buf.head);
266 rpc_file_write(struct ubus_context *ctx, struct ubus_object *obj,
267 struct ubus_request_data *req, const char *method,
268 struct blob_attr *msg)
270 struct blob_attr *tb[__RPC_F_RW_MAX];
271 int append = O_TRUNC;
272 mode_t prev_mode, mode = 0666;
275 ssize_t data_len = 0;
277 blobmsg_parse(rpc_file_rw_policy, __RPC_F_RW_MAX, tb,
278 blob_data(msg), blob_len(msg));
280 if (!tb[RPC_F_RW_PATH] || !tb[RPC_F_RW_DATA])
281 return UBUS_STATUS_INVALID_ARGUMENT;
283 data = blobmsg_data(tb[RPC_F_RW_DATA]);
284 data_len = blobmsg_data_len(tb[RPC_F_RW_DATA]) - 1;
286 if (tb[RPC_F_RW_APPEND] && blobmsg_get_bool(tb[RPC_F_RW_APPEND]))
289 if (tb[RPC_F_RW_MODE])
290 mode = blobmsg_get_u32(tb[RPC_F_RW_MODE]);
292 prev_mode = umask(0);
293 fd = open(blobmsg_data(tb[RPC_F_RW_PATH]), O_CREAT | O_WRONLY | append, mode);
296 return rpc_errno_status();
298 if (tb[RPC_F_RW_BASE64] && blobmsg_get_bool(tb[RPC_F_RW_BASE64]))
300 data_len = b64_decode(data, data, data_len);
303 rv = UBUS_STATUS_UNKNOWN_ERROR;
308 if (write(fd, data, data_len) < 0)
319 return rpc_errno_status();
325 rpc_file_md5(struct ubus_context *ctx, struct ubus_object *obj,
326 struct ubus_request_data *req, const char *method,
327 struct blob_attr *msg)
335 if (!rpc_check_path(msg, &path, &s))
336 return rpc_errno_status();
338 if (!S_ISREG(s.st_mode))
339 return UBUS_STATUS_NOT_SUPPORTED;
341 if ((rv = md5sum(path, md5)) <= 0)
342 return rpc_errno_status();
344 blob_buf_init(&buf, 0);
345 wbuf = blobmsg_alloc_string_buffer(&buf, "md5", 33);
347 for (i = 0; i < 16; i++)
348 sprintf(wbuf + (i * 2), "%02x", (uint8_t) md5[i]);
350 blobmsg_add_string_buffer(&buf);
351 ubus_send_reply(ctx, req, buf.head);
354 return UBUS_STATUS_OK;
358 rpc_file_list(struct ubus_context *ctx, struct ubus_object *obj,
359 struct ubus_request_data *req, const char *method,
360 struct blob_attr *msg)
368 if (!rpc_check_path(msg, &path, &s))
369 return rpc_errno_status();
371 if ((fd = opendir(path)) == NULL)
372 return rpc_errno_status();
374 blob_buf_init(&buf, 0);
375 c = blobmsg_open_array(&buf, "entries");
377 while ((e = readdir(fd)) != NULL)
379 if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, ".."))
382 d = blobmsg_open_table(&buf, NULL);
383 blobmsg_add_string(&buf, "name", e->d_name);
384 blobmsg_add_string(&buf, "type", d_types[e->d_type]);
385 blobmsg_close_table(&buf, d);
390 blobmsg_close_array(&buf, c);
391 ubus_send_reply(ctx, req, buf.head);
398 rpc_file_stat(struct ubus_context *ctx, struct ubus_object *obj,
399 struct ubus_request_data *req, const char *method,
400 struct blob_attr *msg)
406 if (!rpc_check_path(msg, &path, &s))
407 return rpc_errno_status();
409 blob_buf_init(&buf, 0);
411 type = S_ISREG(s.st_mode) ? DT_REG :
412 S_ISDIR(s.st_mode) ? DT_DIR :
413 S_ISCHR(s.st_mode) ? DT_CHR :
414 S_ISBLK(s.st_mode) ? DT_BLK :
415 S_ISFIFO(s.st_mode) ? DT_FIFO :
416 S_ISLNK(s.st_mode) ? DT_LNK :
417 S_ISSOCK(s.st_mode) ? DT_SOCK :
420 blobmsg_add_string(&buf, "path", path);
421 blobmsg_add_string(&buf, "type", d_types[type]);
422 blobmsg_add_u32(&buf, "size", s.st_size);
423 blobmsg_add_u32(&buf, "mode", s.st_mode);
424 blobmsg_add_u32(&buf, "atime", s.st_atime);
425 blobmsg_add_u32(&buf, "mtime", s.st_mtime);
426 blobmsg_add_u32(&buf, "ctime", s.st_ctime);
427 blobmsg_add_u32(&buf, "inode", s.st_ino);
428 blobmsg_add_u32(&buf, "uid", s.st_uid);
429 blobmsg_add_u32(&buf, "gid", s.st_gid);
431 ubus_send_reply(ctx, req, buf.head);
438 rpc_file_exec_lookup(const char *cmd)
441 int plen = 0, clen = strlen(cmd) + 1;
443 static char path[PATH_MAX];
445 if (!stat(cmd, &s) && S_ISREG(s.st_mode))
448 search = getenv("PATH");
451 search = "/bin:/usr/bin:/sbin:/usr/sbin";
457 if (*p != ':' && *p != '\0')
462 if ((plen + clen) >= sizeof(path))
465 strncpy(path, search, plen);
466 sprintf(path + plen, "/%s", cmd);
468 if (!stat(path, &s) && S_ISREG(s.st_mode))
480 rpc_ustream_to_blobmsg(struct ustream *s, const char *name)
485 if ((len = ustream_pending_data(s, false)) > 0)
487 wbuf = blobmsg_alloc_string_buffer(&buf, name, len + 1);
492 ustream_for_each_read_buffer(s, rbuf, len)
494 memcpy(wbuf, rbuf, len);
499 blobmsg_add_string_buffer(&buf);
504 rpc_file_exec_reply(struct rpc_file_exec_context *c, int rv)
506 uloop_timeout_cancel(&c->timeout);
507 uloop_process_delete(&c->process);
509 if (rv == UBUS_STATUS_OK)
511 blob_buf_init(&buf, 0);
513 blobmsg_add_u32(&buf, "code", WEXITSTATUS(c->stat));
515 rpc_ustream_to_blobmsg(&c->opipe.stream, "stdout");
516 rpc_ustream_to_blobmsg(&c->epipe.stream, "stderr");
518 ubus_send_reply(c->context, &c->request, buf.head);
522 ubus_complete_deferred_request(c->context, &c->request, rv);
524 ustream_free(&c->opipe.stream);
525 ustream_free(&c->epipe.stream);
527 close(c->opipe.fd.fd);
528 close(c->epipe.fd.fd);
534 rpc_file_exec_timeout_cb(struct uloop_timeout *t)
536 struct rpc_file_exec_context *c =
537 container_of(t, struct rpc_file_exec_context, timeout);
539 kill(c->process.pid, SIGKILL);
540 rpc_file_exec_reply(c, UBUS_STATUS_TIMEOUT);
544 rpc_file_exec_process_cb(struct uloop_process *p, int stat)
546 struct rpc_file_exec_context *c =
547 container_of(p, struct rpc_file_exec_context, process);
551 ustream_poll(&c->opipe.stream);
552 ustream_poll(&c->epipe.stream);
556 rpc_file_exec_opipe_read_cb(struct ustream *s, int bytes)
558 struct rpc_file_exec_context *c =
559 container_of(s, struct rpc_file_exec_context, opipe.stream);
561 if (ustream_read_buf_full(s))
562 rpc_file_exec_reply(c, UBUS_STATUS_NOT_SUPPORTED);
566 rpc_file_exec_epipe_read_cb(struct ustream *s, int bytes)
568 struct rpc_file_exec_context *c =
569 container_of(s, struct rpc_file_exec_context, epipe.stream);
571 if (ustream_read_buf_full(s))
572 rpc_file_exec_reply(c, UBUS_STATUS_NOT_SUPPORTED);
576 rpc_file_exec_opipe_state_cb(struct ustream *s)
578 struct rpc_file_exec_context *c =
579 container_of(s, struct rpc_file_exec_context, opipe.stream);
581 if (c->opipe.stream.eof && c->epipe.stream.eof)
582 rpc_file_exec_reply(c, UBUS_STATUS_OK);
586 rpc_file_exec_epipe_state_cb(struct ustream *s)
588 struct rpc_file_exec_context *c =
589 container_of(s, struct rpc_file_exec_context, epipe.stream);
591 if (c->opipe.stream.eof && c->epipe.stream.eof)
592 rpc_file_exec_reply(c, UBUS_STATUS_OK);
596 rpc_file_exec_run(const char *cmd,
597 const struct blob_attr *arg, const struct blob_attr *env,
598 struct ubus_context *ctx, struct ubus_request_data *req)
606 struct blob_attr *cur;
611 struct rpc_file_exec_context *c;
613 cmd = rpc_file_exec_lookup(cmd);
616 return UBUS_STATUS_NOT_FOUND;
618 c = malloc(sizeof(*c));
621 return UBUS_STATUS_UNKNOWN_ERROR;
623 if (pipe(opipe) || pipe(epipe))
624 return rpc_errno_status();
626 switch ((pid = fork()))
629 return rpc_errno_status();
644 args = malloc(sizeof(char *) * arglen);
647 return UBUS_STATUS_UNKNOWN_ERROR;
649 args[0] = (char *)cmd;
654 blobmsg_for_each_attr(cur, arg, rem)
656 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
661 if (!(args = realloc(args, sizeof(char *) * arglen)))
662 return UBUS_STATUS_UNKNOWN_ERROR;
664 args[arglen-2] = blobmsg_data(cur);
665 args[arglen-1] = NULL;
671 blobmsg_for_each_attr(cur, env, rem)
673 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
676 setenv(blobmsg_name(cur), blobmsg_data(cur), 1);
680 if (execv(cmd, args))
681 return rpc_errno_status();
684 memset(c, 0, sizeof(*c));
686 ustream_declare(c->opipe, opipe[0], exec_opipe);
687 ustream_declare(c->epipe, epipe[0], exec_epipe);
689 c->process.pid = pid;
690 c->process.cb = rpc_file_exec_process_cb;
691 uloop_process_add(&c->process);
693 c->timeout.cb = rpc_file_exec_timeout_cb;
694 uloop_timeout_set(&c->timeout, RPC_FILE_MAX_RUNTIME);
700 ubus_defer_request(ctx, req, &c->request);
703 return UBUS_STATUS_OK;
707 rpc_file_exec(struct ubus_context *ctx, struct ubus_object *obj,
708 struct ubus_request_data *req, const char *method,
709 struct blob_attr *msg)
711 struct blob_attr *tb[__RPC_E_MAX];
713 blobmsg_parse(rpc_exec_policy, __RPC_E_MAX, tb,
714 blob_data(msg), blob_len(msg));
717 return UBUS_STATUS_INVALID_ARGUMENT;
719 return rpc_file_exec_run(blobmsg_data(tb[RPC_E_CMD]),
720 tb[RPC_E_PARM], tb[RPC_E_ENV], ctx, req);
725 rpc_file_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
727 static const struct ubus_method file_methods[] = {
728 UBUS_METHOD("read", rpc_file_read, rpc_file_rb_policy),
729 UBUS_METHOD("write", rpc_file_write, rpc_file_rw_policy),
730 UBUS_METHOD("list", rpc_file_list, rpc_file_r_policy),
731 UBUS_METHOD("stat", rpc_file_stat, rpc_file_r_policy),
732 UBUS_METHOD("md5", rpc_file_md5, rpc_file_r_policy),
733 UBUS_METHOD("exec", rpc_file_exec, rpc_exec_policy),
736 static struct ubus_object_type file_type =
737 UBUS_OBJECT_TYPE("luci-rpc-file", file_methods);
739 static struct ubus_object obj = {
742 .methods = file_methods,
743 .n_methods = ARRAY_SIZE(file_methods),
746 return ubus_add_object(ctx, &obj);
749 struct rpc_plugin rpc_plugin = {
750 .init = rpc_file_api_init