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>
32 #include <libubox/utils.h>
34 #include <rpcd/plugin.h>
36 /* limit of sys & proc files */
37 #define RPC_FILE_MIN_SIZE (128)
39 /* limit of regular files and command output data */
40 #define RPC_FILE_MAX_SIZE (4096 * 64)
41 #define RPC_FILE_MAX_RUNTIME (3 * 1000)
43 #define ustream_for_each_read_buffer(stream, ptr, len) \
44 for (ptr = ustream_get_read_buf(stream, &len); \
45 ptr != NULL && len > 0; \
46 ustream_consume(stream, len), ptr = ustream_get_read_buf(stream, &len))
48 #define ustream_declare(us, fd, name) \
49 us.stream.string_data = true; \
50 us.stream.r.buffer_len = 4096; \
51 us.stream.r.max_buffers = RPC_FILE_MAX_SIZE / 4096; \
52 us.stream.notify_read = rpc_file_##name##_read_cb; \
53 us.stream.notify_state = rpc_file_##name##_state_cb; \
54 ustream_fd_init(&us, fd);
56 struct rpc_file_exec_context {
57 struct ubus_context *context;
58 struct ubus_request_data request;
59 struct uloop_timeout timeout;
60 struct uloop_process process;
61 struct ustream_fd opipe;
62 struct ustream_fd epipe;
71 static struct blob_buf buf;
78 static const struct blobmsg_policy rpc_file_r_policy[__RPC_F_R_MAX] = {
79 [RPC_F_R_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
88 static const struct blobmsg_policy rpc_file_rb_policy[__RPC_F_RB_MAX] = {
89 [RPC_F_RB_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
90 [RPC_F_RB_BASE64] = { .name = "base64", .type = BLOBMSG_TYPE_BOOL },
100 static const struct blobmsg_policy rpc_file_rw_policy[__RPC_F_RW_MAX] = {
101 [RPC_F_RW_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
102 [RPC_F_RW_DATA] = { .name = "data", .type = BLOBMSG_TYPE_STRING },
103 [RPC_F_RW_BASE64] = { .name = "base64", .type = BLOBMSG_TYPE_BOOL },
113 static const struct blobmsg_policy rpc_exec_policy[__RPC_E_MAX] = {
114 [RPC_E_CMD] = { .name = "command", .type = BLOBMSG_TYPE_STRING },
115 [RPC_E_PARM] = { .name = "params", .type = BLOBMSG_TYPE_ARRAY },
116 [RPC_E_ENV] = { .name = "env", .type = BLOBMSG_TYPE_TABLE },
119 static const char *d_types[] = {
122 [DT_DIR] = "directory",
124 [DT_LNK] = "symlink",
126 [DT_SOCK] = "socket",
127 [DT_UNKNOWN] = "unknown",
132 rpc_errno_status(void)
137 return UBUS_STATUS_PERMISSION_DENIED;
140 return UBUS_STATUS_INVALID_ARGUMENT;
143 return UBUS_STATUS_NOT_FOUND;
146 return UBUS_STATUS_INVALID_ARGUMENT;
149 return UBUS_STATUS_UNKNOWN_ERROR;
153 static struct blob_attr **
154 rpc_check_path(struct blob_attr *msg, char **path, struct stat *s)
156 static struct blob_attr *tb[__RPC_F_R_MAX];
158 blobmsg_parse(rpc_file_r_policy, __RPC_F_R_MAX, tb, blob_data(msg), blob_len(msg));
160 if (!tb[RPC_F_R_PATH])
166 *path = blobmsg_data(tb[RPC_F_R_PATH]);
175 rpc_file_read(struct ubus_context *ctx, struct ubus_object *obj,
176 struct ubus_request_data *req, const char *method,
177 struct blob_attr *msg)
179 static struct blob_attr *tb[__RPC_F_RB_MAX];
187 blobmsg_parse(rpc_file_rb_policy, __RPC_F_RB_MAX, tb, blob_data(msg), blob_len(msg));
189 if (!tb[RPC_F_RB_PATH])
190 return rpc_errno_status();
192 path = blobmsg_data(tb[RPC_F_RB_PATH]);
195 return rpc_errno_status();
197 if (s.st_size >= RPC_FILE_MAX_SIZE)
198 return UBUS_STATUS_NOT_SUPPORTED;
200 if ((fd = open(path, O_RDONLY)) < 0)
201 return rpc_errno_status();
203 /* some sysfs files do not report a length */
205 s.st_size = RPC_FILE_MIN_SIZE;
207 blob_buf_init(&buf, 0);
209 if (tb[RPC_F_RB_BASE64])
210 base64 = blobmsg_get_bool(tb[RPC_F_RB_BASE64]);
214 len = B64_ENCODE_LEN(s.st_size);
215 wbuf = blobmsg_alloc_string_buffer(&buf, "data", len);
219 rv = UBUS_STATUS_UNKNOWN_ERROR;
223 if ((len = read(fd, wbuf, s.st_size)) <= 0)
225 rv = UBUS_STATUS_NO_DATA;
231 uint8_t *data = calloc(len, sizeof(uint8_t));
234 rv = UBUS_STATUS_UNKNOWN_ERROR;
237 memcpy(data, wbuf, len);
239 len = b64_encode(data, len, wbuf, B64_ENCODE_LEN(len));
243 rv = UBUS_STATUS_UNKNOWN_ERROR;
249 blobmsg_add_string_buffer(&buf);
251 ubus_send_reply(ctx, req, buf.head);
261 rpc_file_write(struct ubus_context *ctx, struct ubus_object *obj,
262 struct ubus_request_data *req, const char *method,
263 struct blob_attr *msg)
265 struct blob_attr *tb[__RPC_F_RW_MAX];
270 blobmsg_parse(rpc_file_rw_policy, __RPC_F_RW_MAX, tb,
271 blob_data(msg), blob_len(msg));
273 if (!tb[RPC_F_RW_PATH] || !tb[RPC_F_RW_DATA])
274 return UBUS_STATUS_INVALID_ARGUMENT;
276 data = blobmsg_data(tb[RPC_F_RW_DATA]);
277 data_len = blobmsg_data_len(tb[RPC_F_RW_DATA]) - 1;
279 if ((fd = open(blobmsg_data(tb[RPC_F_RW_PATH]), O_CREAT | O_TRUNC | O_WRONLY, 0666)) < 0)
280 return rpc_errno_status();
282 if (tb[RPC_F_RW_BASE64] && blobmsg_get_bool(tb[RPC_F_RW_BASE64]))
284 data_len = b64_decode(data, data, data_len);
287 rv = UBUS_STATUS_UNKNOWN_ERROR;
292 if (write(fd, data, data_len) < 0)
303 return rpc_errno_status();
309 rpc_file_md5(struct ubus_context *ctx, struct ubus_object *obj,
310 struct ubus_request_data *req, const char *method,
311 struct blob_attr *msg)
319 if (!rpc_check_path(msg, &path, &s))
320 return rpc_errno_status();
322 if (!S_ISREG(s.st_mode))
323 return UBUS_STATUS_NOT_SUPPORTED;
325 if ((rv = md5sum(path, md5)) <= 0)
326 return rpc_errno_status();
328 blob_buf_init(&buf, 0);
329 wbuf = blobmsg_alloc_string_buffer(&buf, "md5", 33);
331 for (i = 0; i < 16; i++)
332 sprintf(wbuf + (i * 2), "%02x", (uint8_t) md5[i]);
334 blobmsg_add_string_buffer(&buf);
335 ubus_send_reply(ctx, req, buf.head);
338 return UBUS_STATUS_OK;
342 rpc_file_list(struct ubus_context *ctx, struct ubus_object *obj,
343 struct ubus_request_data *req, const char *method,
344 struct blob_attr *msg)
352 if (!rpc_check_path(msg, &path, &s))
353 return rpc_errno_status();
355 if ((fd = opendir(path)) == NULL)
356 return rpc_errno_status();
358 blob_buf_init(&buf, 0);
359 c = blobmsg_open_array(&buf, "entries");
361 while ((e = readdir(fd)) != NULL)
363 if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, ".."))
366 d = blobmsg_open_table(&buf, NULL);
367 blobmsg_add_string(&buf, "name", e->d_name);
368 blobmsg_add_string(&buf, "type", d_types[e->d_type]);
369 blobmsg_close_table(&buf, d);
374 blobmsg_close_array(&buf, c);
375 ubus_send_reply(ctx, req, buf.head);
382 rpc_file_stat(struct ubus_context *ctx, struct ubus_object *obj,
383 struct ubus_request_data *req, const char *method,
384 struct blob_attr *msg)
390 if (!rpc_check_path(msg, &path, &s))
391 return rpc_errno_status();
393 blob_buf_init(&buf, 0);
395 type = S_ISREG(s.st_mode) ? DT_REG :
396 S_ISDIR(s.st_mode) ? DT_DIR :
397 S_ISCHR(s.st_mode) ? DT_CHR :
398 S_ISBLK(s.st_mode) ? DT_BLK :
399 S_ISFIFO(s.st_mode) ? DT_FIFO :
400 S_ISLNK(s.st_mode) ? DT_LNK :
401 S_ISSOCK(s.st_mode) ? DT_SOCK :
404 blobmsg_add_string(&buf, "path", path);
405 blobmsg_add_string(&buf, "type", d_types[type]);
406 blobmsg_add_u32(&buf, "size", s.st_size);
407 blobmsg_add_u32(&buf, "mode", s.st_mode);
408 blobmsg_add_u32(&buf, "atime", s.st_atime);
409 blobmsg_add_u32(&buf, "mtime", s.st_mtime);
410 blobmsg_add_u32(&buf, "ctime", s.st_ctime);
411 blobmsg_add_u32(&buf, "inode", s.st_ino);
412 blobmsg_add_u32(&buf, "uid", s.st_uid);
413 blobmsg_add_u32(&buf, "gid", s.st_gid);
415 ubus_send_reply(ctx, req, buf.head);
422 rpc_file_exec_lookup(const char *cmd)
425 int plen = 0, clen = strlen(cmd) + 1;
427 static char path[PATH_MAX];
429 if (!stat(cmd, &s) && S_ISREG(s.st_mode))
432 search = getenv("PATH");
435 search = "/bin:/usr/bin:/sbin:/usr/sbin";
441 if (*p != ':' && *p != '\0')
446 if ((plen + clen) >= sizeof(path))
449 strncpy(path, search, plen);
450 sprintf(path + plen, "/%s", cmd);
452 if (!stat(path, &s) && S_ISREG(s.st_mode))
464 rpc_ustream_to_blobmsg(struct ustream *s, const char *name)
469 if ((len = ustream_pending_data(s, false)) > 0)
471 wbuf = blobmsg_alloc_string_buffer(&buf, name, len + 1);
476 ustream_for_each_read_buffer(s, rbuf, len)
478 memcpy(wbuf, rbuf, len);
483 blobmsg_add_string_buffer(&buf);
488 rpc_file_exec_reply(struct rpc_file_exec_context *c, int rv)
490 uloop_timeout_cancel(&c->timeout);
491 uloop_process_delete(&c->process);
493 if (rv == UBUS_STATUS_OK)
495 blob_buf_init(&buf, 0);
497 blobmsg_add_u32(&buf, "code", WEXITSTATUS(c->stat));
499 rpc_ustream_to_blobmsg(&c->opipe.stream, "stdout");
500 rpc_ustream_to_blobmsg(&c->epipe.stream, "stderr");
502 ubus_send_reply(c->context, &c->request, buf.head);
506 ubus_complete_deferred_request(c->context, &c->request, rv);
508 ustream_free(&c->opipe.stream);
509 ustream_free(&c->epipe.stream);
511 close(c->opipe.fd.fd);
512 close(c->epipe.fd.fd);
518 rpc_file_exec_timeout_cb(struct uloop_timeout *t)
520 struct rpc_file_exec_context *c =
521 container_of(t, struct rpc_file_exec_context, timeout);
523 kill(c->process.pid, SIGKILL);
524 rpc_file_exec_reply(c, UBUS_STATUS_TIMEOUT);
528 rpc_file_exec_process_cb(struct uloop_process *p, int stat)
530 struct rpc_file_exec_context *c =
531 container_of(p, struct rpc_file_exec_context, process);
535 ustream_poll(&c->opipe.stream);
536 ustream_poll(&c->epipe.stream);
540 rpc_file_exec_opipe_read_cb(struct ustream *s, int bytes)
542 struct rpc_file_exec_context *c =
543 container_of(s, struct rpc_file_exec_context, opipe.stream);
545 if (ustream_read_buf_full(s))
546 rpc_file_exec_reply(c, UBUS_STATUS_NOT_SUPPORTED);
550 rpc_file_exec_epipe_read_cb(struct ustream *s, int bytes)
552 struct rpc_file_exec_context *c =
553 container_of(s, struct rpc_file_exec_context, epipe.stream);
555 if (ustream_read_buf_full(s))
556 rpc_file_exec_reply(c, UBUS_STATUS_NOT_SUPPORTED);
560 rpc_file_exec_opipe_state_cb(struct ustream *s)
562 struct rpc_file_exec_context *c =
563 container_of(s, struct rpc_file_exec_context, opipe.stream);
565 if (c->opipe.stream.eof && c->epipe.stream.eof)
566 rpc_file_exec_reply(c, UBUS_STATUS_OK);
570 rpc_file_exec_epipe_state_cb(struct ustream *s)
572 struct rpc_file_exec_context *c =
573 container_of(s, struct rpc_file_exec_context, epipe.stream);
575 if (c->opipe.stream.eof && c->epipe.stream.eof)
576 rpc_file_exec_reply(c, UBUS_STATUS_OK);
580 rpc_file_exec_run(const char *cmd,
581 const struct blob_attr *arg, const struct blob_attr *env,
582 struct ubus_context *ctx, struct ubus_request_data *req)
590 struct blob_attr *cur;
595 struct rpc_file_exec_context *c;
597 cmd = rpc_file_exec_lookup(cmd);
600 return UBUS_STATUS_NOT_FOUND;
602 c = malloc(sizeof(*c));
605 return UBUS_STATUS_UNKNOWN_ERROR;
607 if (pipe(opipe) || pipe(epipe))
608 return rpc_errno_status();
610 switch ((pid = fork()))
613 return rpc_errno_status();
628 args = malloc(sizeof(char *) * arglen);
631 return UBUS_STATUS_UNKNOWN_ERROR;
633 args[0] = (char *)cmd;
638 blobmsg_for_each_attr(cur, arg, rem)
640 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
645 if (!(args = realloc(args, sizeof(char *) * arglen)))
646 return UBUS_STATUS_UNKNOWN_ERROR;
648 args[arglen-2] = blobmsg_data(cur);
649 args[arglen-1] = NULL;
655 blobmsg_for_each_attr(cur, env, rem)
657 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
660 setenv(blobmsg_name(cur), blobmsg_data(cur), 1);
664 if (execv(cmd, args))
665 return rpc_errno_status();
668 memset(c, 0, sizeof(*c));
670 ustream_declare(c->opipe, opipe[0], exec_opipe);
671 ustream_declare(c->epipe, epipe[0], exec_epipe);
673 c->process.pid = pid;
674 c->process.cb = rpc_file_exec_process_cb;
675 uloop_process_add(&c->process);
677 c->timeout.cb = rpc_file_exec_timeout_cb;
678 uloop_timeout_set(&c->timeout, RPC_FILE_MAX_RUNTIME);
684 ubus_defer_request(ctx, req, &c->request);
687 return UBUS_STATUS_OK;
691 rpc_file_exec(struct ubus_context *ctx, struct ubus_object *obj,
692 struct ubus_request_data *req, const char *method,
693 struct blob_attr *msg)
695 struct blob_attr *tb[__RPC_E_MAX];
697 blobmsg_parse(rpc_exec_policy, __RPC_E_MAX, tb,
698 blob_data(msg), blob_len(msg));
701 return UBUS_STATUS_INVALID_ARGUMENT;
703 return rpc_file_exec_run(blobmsg_data(tb[RPC_E_CMD]),
704 tb[RPC_E_PARM], tb[RPC_E_ENV], ctx, req);
709 rpc_file_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
711 static const struct ubus_method file_methods[] = {
712 UBUS_METHOD("read", rpc_file_read, rpc_file_rb_policy),
713 UBUS_METHOD("write", rpc_file_write, rpc_file_rw_policy),
714 UBUS_METHOD("list", rpc_file_list, rpc_file_r_policy),
715 UBUS_METHOD("stat", rpc_file_stat, rpc_file_r_policy),
716 UBUS_METHOD("md5", rpc_file_md5, rpc_file_r_policy),
717 UBUS_METHOD("exec", rpc_file_exec, rpc_exec_policy),
720 static struct ubus_object_type file_type =
721 UBUS_OBJECT_TYPE("luci-rpc-file", file_methods);
723 static struct ubus_object obj = {
726 .methods = file_methods,
727 .n_methods = ARRAY_SIZE(file_methods),
730 return ubus_add_object(ctx, &obj);
733 struct rpc_plugin rpc_plugin = {
734 .init = rpc_file_api_init