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 },
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_MODE] = { .name = "mode", .type = BLOBMSG_TYPE_INT32 },
105 [RPC_F_RW_BASE64] = { .name = "base64", .type = BLOBMSG_TYPE_BOOL },
115 static const struct blobmsg_policy rpc_exec_policy[__RPC_E_MAX] = {
116 [RPC_E_CMD] = { .name = "command", .type = BLOBMSG_TYPE_STRING },
117 [RPC_E_PARM] = { .name = "params", .type = BLOBMSG_TYPE_ARRAY },
118 [RPC_E_ENV] = { .name = "env", .type = BLOBMSG_TYPE_TABLE },
121 static const char *d_types[] = {
124 [DT_DIR] = "directory",
126 [DT_LNK] = "symlink",
128 [DT_SOCK] = "socket",
129 [DT_UNKNOWN] = "unknown",
134 rpc_errno_status(void)
139 return UBUS_STATUS_PERMISSION_DENIED;
142 return UBUS_STATUS_INVALID_ARGUMENT;
145 return UBUS_STATUS_NOT_FOUND;
148 return UBUS_STATUS_INVALID_ARGUMENT;
151 return UBUS_STATUS_UNKNOWN_ERROR;
155 static struct blob_attr **
156 rpc_check_path(struct blob_attr *msg, char **path, struct stat *s)
158 static struct blob_attr *tb[__RPC_F_R_MAX];
160 blobmsg_parse(rpc_file_r_policy, __RPC_F_R_MAX, tb, blob_data(msg), blob_len(msg));
162 if (!tb[RPC_F_R_PATH])
168 *path = blobmsg_data(tb[RPC_F_R_PATH]);
177 rpc_file_read(struct ubus_context *ctx, struct ubus_object *obj,
178 struct ubus_request_data *req, const char *method,
179 struct blob_attr *msg)
181 static struct blob_attr *tb[__RPC_F_RB_MAX];
189 blobmsg_parse(rpc_file_rb_policy, __RPC_F_RB_MAX, tb, blob_data(msg), blob_len(msg));
191 if (!tb[RPC_F_RB_PATH])
192 return rpc_errno_status();
194 path = blobmsg_data(tb[RPC_F_RB_PATH]);
197 return rpc_errno_status();
199 if (s.st_size >= RPC_FILE_MAX_SIZE)
200 return UBUS_STATUS_NOT_SUPPORTED;
202 if ((fd = open(path, O_RDONLY)) < 0)
203 return rpc_errno_status();
205 /* some sysfs files do not report a length */
207 s.st_size = RPC_FILE_MIN_SIZE;
209 blob_buf_init(&buf, 0);
211 if (tb[RPC_F_RB_BASE64])
212 base64 = blobmsg_get_bool(tb[RPC_F_RB_BASE64]);
216 len = B64_ENCODE_LEN(s.st_size);
217 wbuf = blobmsg_alloc_string_buffer(&buf, "data", len);
221 rv = UBUS_STATUS_UNKNOWN_ERROR;
225 if ((len = read(fd, wbuf, s.st_size)) <= 0)
227 rv = UBUS_STATUS_NO_DATA;
233 uint8_t *data = calloc(len, sizeof(uint8_t));
236 rv = UBUS_STATUS_UNKNOWN_ERROR;
239 memcpy(data, wbuf, len);
241 len = b64_encode(data, len, wbuf, B64_ENCODE_LEN(len));
245 rv = UBUS_STATUS_UNKNOWN_ERROR;
250 *(wbuf + len) = '\0';
251 blobmsg_add_string_buffer(&buf);
253 ubus_send_reply(ctx, req, buf.head);
263 rpc_file_write(struct ubus_context *ctx, struct ubus_object *obj,
264 struct ubus_request_data *req, const char *method,
265 struct blob_attr *msg)
267 struct blob_attr *tb[__RPC_F_RW_MAX];
268 mode_t prev_mode, mode = 0666;
273 blobmsg_parse(rpc_file_rw_policy, __RPC_F_RW_MAX, tb,
274 blob_data(msg), blob_len(msg));
276 if (!tb[RPC_F_RW_PATH] || !tb[RPC_F_RW_DATA])
277 return UBUS_STATUS_INVALID_ARGUMENT;
279 data = blobmsg_data(tb[RPC_F_RW_DATA]);
280 data_len = blobmsg_data_len(tb[RPC_F_RW_DATA]) - 1;
282 if (tb[RPC_F_RW_MODE])
283 mode = blobmsg_get_u32(tb[RPC_F_RW_MODE]);
285 prev_mode = umask(0);
286 fd = open(blobmsg_data(tb[RPC_F_RW_PATH]), O_CREAT | O_WRONLY | O_TRUNC, mode);
289 return rpc_errno_status();
291 if (tb[RPC_F_RW_BASE64] && blobmsg_get_bool(tb[RPC_F_RW_BASE64]))
293 data_len = b64_decode(data, data, data_len);
296 rv = UBUS_STATUS_UNKNOWN_ERROR;
301 if (write(fd, data, data_len) < 0)
312 return rpc_errno_status();
318 rpc_file_md5(struct ubus_context *ctx, struct ubus_object *obj,
319 struct ubus_request_data *req, const char *method,
320 struct blob_attr *msg)
328 if (!rpc_check_path(msg, &path, &s))
329 return rpc_errno_status();
331 if (!S_ISREG(s.st_mode))
332 return UBUS_STATUS_NOT_SUPPORTED;
334 if ((rv = md5sum(path, md5)) <= 0)
335 return rpc_errno_status();
337 blob_buf_init(&buf, 0);
338 wbuf = blobmsg_alloc_string_buffer(&buf, "md5", 33);
340 for (i = 0; i < 16; i++)
341 sprintf(wbuf + (i * 2), "%02x", (uint8_t) md5[i]);
343 blobmsg_add_string_buffer(&buf);
344 ubus_send_reply(ctx, req, buf.head);
347 return UBUS_STATUS_OK;
351 rpc_file_list(struct ubus_context *ctx, struct ubus_object *obj,
352 struct ubus_request_data *req, const char *method,
353 struct blob_attr *msg)
361 if (!rpc_check_path(msg, &path, &s))
362 return rpc_errno_status();
364 if ((fd = opendir(path)) == NULL)
365 return rpc_errno_status();
367 blob_buf_init(&buf, 0);
368 c = blobmsg_open_array(&buf, "entries");
370 while ((e = readdir(fd)) != NULL)
372 if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, ".."))
375 d = blobmsg_open_table(&buf, NULL);
376 blobmsg_add_string(&buf, "name", e->d_name);
377 blobmsg_add_string(&buf, "type", d_types[e->d_type]);
378 blobmsg_close_table(&buf, d);
383 blobmsg_close_array(&buf, c);
384 ubus_send_reply(ctx, req, buf.head);
391 rpc_file_stat(struct ubus_context *ctx, struct ubus_object *obj,
392 struct ubus_request_data *req, const char *method,
393 struct blob_attr *msg)
399 if (!rpc_check_path(msg, &path, &s))
400 return rpc_errno_status();
402 blob_buf_init(&buf, 0);
404 type = S_ISREG(s.st_mode) ? DT_REG :
405 S_ISDIR(s.st_mode) ? DT_DIR :
406 S_ISCHR(s.st_mode) ? DT_CHR :
407 S_ISBLK(s.st_mode) ? DT_BLK :
408 S_ISFIFO(s.st_mode) ? DT_FIFO :
409 S_ISLNK(s.st_mode) ? DT_LNK :
410 S_ISSOCK(s.st_mode) ? DT_SOCK :
413 blobmsg_add_string(&buf, "path", path);
414 blobmsg_add_string(&buf, "type", d_types[type]);
415 blobmsg_add_u32(&buf, "size", s.st_size);
416 blobmsg_add_u32(&buf, "mode", s.st_mode);
417 blobmsg_add_u32(&buf, "atime", s.st_atime);
418 blobmsg_add_u32(&buf, "mtime", s.st_mtime);
419 blobmsg_add_u32(&buf, "ctime", s.st_ctime);
420 blobmsg_add_u32(&buf, "inode", s.st_ino);
421 blobmsg_add_u32(&buf, "uid", s.st_uid);
422 blobmsg_add_u32(&buf, "gid", s.st_gid);
424 ubus_send_reply(ctx, req, buf.head);
431 rpc_file_exec_lookup(const char *cmd)
434 int plen = 0, clen = strlen(cmd) + 1;
436 static char path[PATH_MAX];
438 if (!stat(cmd, &s) && S_ISREG(s.st_mode))
441 search = getenv("PATH");
444 search = "/bin:/usr/bin:/sbin:/usr/sbin";
450 if (*p != ':' && *p != '\0')
455 if ((plen + clen) >= sizeof(path))
458 strncpy(path, search, plen);
459 sprintf(path + plen, "/%s", cmd);
461 if (!stat(path, &s) && S_ISREG(s.st_mode))
473 rpc_ustream_to_blobmsg(struct ustream *s, const char *name)
478 if ((len = ustream_pending_data(s, false)) > 0)
480 wbuf = blobmsg_alloc_string_buffer(&buf, name, len + 1);
485 ustream_for_each_read_buffer(s, rbuf, len)
487 memcpy(wbuf, rbuf, len);
492 blobmsg_add_string_buffer(&buf);
497 rpc_file_exec_reply(struct rpc_file_exec_context *c, int rv)
499 uloop_timeout_cancel(&c->timeout);
500 uloop_process_delete(&c->process);
502 if (rv == UBUS_STATUS_OK)
504 blob_buf_init(&buf, 0);
506 blobmsg_add_u32(&buf, "code", WEXITSTATUS(c->stat));
508 rpc_ustream_to_blobmsg(&c->opipe.stream, "stdout");
509 rpc_ustream_to_blobmsg(&c->epipe.stream, "stderr");
511 ubus_send_reply(c->context, &c->request, buf.head);
515 ubus_complete_deferred_request(c->context, &c->request, rv);
517 ustream_free(&c->opipe.stream);
518 ustream_free(&c->epipe.stream);
520 close(c->opipe.fd.fd);
521 close(c->epipe.fd.fd);
527 rpc_file_exec_timeout_cb(struct uloop_timeout *t)
529 struct rpc_file_exec_context *c =
530 container_of(t, struct rpc_file_exec_context, timeout);
532 kill(c->process.pid, SIGKILL);
533 rpc_file_exec_reply(c, UBUS_STATUS_TIMEOUT);
537 rpc_file_exec_process_cb(struct uloop_process *p, int stat)
539 struct rpc_file_exec_context *c =
540 container_of(p, struct rpc_file_exec_context, process);
544 ustream_poll(&c->opipe.stream);
545 ustream_poll(&c->epipe.stream);
549 rpc_file_exec_opipe_read_cb(struct ustream *s, int bytes)
551 struct rpc_file_exec_context *c =
552 container_of(s, struct rpc_file_exec_context, opipe.stream);
554 if (ustream_read_buf_full(s))
555 rpc_file_exec_reply(c, UBUS_STATUS_NOT_SUPPORTED);
559 rpc_file_exec_epipe_read_cb(struct ustream *s, int bytes)
561 struct rpc_file_exec_context *c =
562 container_of(s, struct rpc_file_exec_context, epipe.stream);
564 if (ustream_read_buf_full(s))
565 rpc_file_exec_reply(c, UBUS_STATUS_NOT_SUPPORTED);
569 rpc_file_exec_opipe_state_cb(struct ustream *s)
571 struct rpc_file_exec_context *c =
572 container_of(s, struct rpc_file_exec_context, opipe.stream);
574 if (c->opipe.stream.eof && c->epipe.stream.eof)
575 rpc_file_exec_reply(c, UBUS_STATUS_OK);
579 rpc_file_exec_epipe_state_cb(struct ustream *s)
581 struct rpc_file_exec_context *c =
582 container_of(s, struct rpc_file_exec_context, epipe.stream);
584 if (c->opipe.stream.eof && c->epipe.stream.eof)
585 rpc_file_exec_reply(c, UBUS_STATUS_OK);
589 rpc_file_exec_run(const char *cmd,
590 const struct blob_attr *arg, const struct blob_attr *env,
591 struct ubus_context *ctx, struct ubus_request_data *req)
599 struct blob_attr *cur;
604 struct rpc_file_exec_context *c;
606 cmd = rpc_file_exec_lookup(cmd);
609 return UBUS_STATUS_NOT_FOUND;
611 c = malloc(sizeof(*c));
614 return UBUS_STATUS_UNKNOWN_ERROR;
616 if (pipe(opipe) || pipe(epipe))
617 return rpc_errno_status();
619 switch ((pid = fork()))
622 return rpc_errno_status();
637 args = malloc(sizeof(char *) * arglen);
640 return UBUS_STATUS_UNKNOWN_ERROR;
642 args[0] = (char *)cmd;
647 blobmsg_for_each_attr(cur, arg, rem)
649 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
654 if (!(args = realloc(args, sizeof(char *) * arglen)))
655 return UBUS_STATUS_UNKNOWN_ERROR;
657 args[arglen-2] = blobmsg_data(cur);
658 args[arglen-1] = NULL;
664 blobmsg_for_each_attr(cur, env, rem)
666 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
669 setenv(blobmsg_name(cur), blobmsg_data(cur), 1);
673 if (execv(cmd, args))
674 return rpc_errno_status();
677 memset(c, 0, sizeof(*c));
679 ustream_declare(c->opipe, opipe[0], exec_opipe);
680 ustream_declare(c->epipe, epipe[0], exec_epipe);
682 c->process.pid = pid;
683 c->process.cb = rpc_file_exec_process_cb;
684 uloop_process_add(&c->process);
686 c->timeout.cb = rpc_file_exec_timeout_cb;
687 uloop_timeout_set(&c->timeout, RPC_FILE_MAX_RUNTIME);
693 ubus_defer_request(ctx, req, &c->request);
696 return UBUS_STATUS_OK;
700 rpc_file_exec(struct ubus_context *ctx, struct ubus_object *obj,
701 struct ubus_request_data *req, const char *method,
702 struct blob_attr *msg)
704 struct blob_attr *tb[__RPC_E_MAX];
706 blobmsg_parse(rpc_exec_policy, __RPC_E_MAX, tb,
707 blob_data(msg), blob_len(msg));
710 return UBUS_STATUS_INVALID_ARGUMENT;
712 return rpc_file_exec_run(blobmsg_data(tb[RPC_E_CMD]),
713 tb[RPC_E_PARM], tb[RPC_E_ENV], ctx, req);
718 rpc_file_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
720 static const struct ubus_method file_methods[] = {
721 UBUS_METHOD("read", rpc_file_read, rpc_file_rb_policy),
722 UBUS_METHOD("write", rpc_file_write, rpc_file_rw_policy),
723 UBUS_METHOD("list", rpc_file_list, rpc_file_r_policy),
724 UBUS_METHOD("stat", rpc_file_stat, rpc_file_r_policy),
725 UBUS_METHOD("md5", rpc_file_md5, rpc_file_r_policy),
726 UBUS_METHOD("exec", rpc_file_exec, rpc_exec_policy),
729 static struct ubus_object_type file_type =
730 UBUS_OBJECT_TYPE("luci-rpc-file", file_methods);
732 static struct ubus_object obj = {
735 .methods = file_methods,
736 .n_methods = ARRAY_SIZE(file_methods),
739 return ubus_add_object(ctx, &obj);
742 struct rpc_plugin rpc_plugin = {
743 .init = rpc_file_api_init