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 },
102 static const struct blobmsg_policy rpc_file_rw_policy[__RPC_F_RW_MAX] = {
103 [RPC_F_RW_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
104 [RPC_F_RW_DATA] = { .name = "data", .type = BLOBMSG_TYPE_STRING },
105 [RPC_F_RW_APPEND] = { .name = "append", .type = BLOBMSG_TYPE_BOOL },
106 [RPC_F_RW_MODE] = { .name = "mode", .type = BLOBMSG_TYPE_INT32 },
107 [RPC_F_RW_BASE64] = { .name = "base64", .type = BLOBMSG_TYPE_BOOL },
117 static const struct blobmsg_policy rpc_exec_policy[__RPC_E_MAX] = {
118 [RPC_E_CMD] = { .name = "command", .type = BLOBMSG_TYPE_STRING },
119 [RPC_E_PARM] = { .name = "params", .type = BLOBMSG_TYPE_ARRAY },
120 [RPC_E_ENV] = { .name = "env", .type = BLOBMSG_TYPE_TABLE },
123 static const char *d_types[] = {
126 [DT_DIR] = "directory",
128 [DT_LNK] = "symlink",
130 [DT_SOCK] = "socket",
131 [DT_UNKNOWN] = "unknown",
136 rpc_errno_status(void)
141 return UBUS_STATUS_PERMISSION_DENIED;
144 return UBUS_STATUS_INVALID_ARGUMENT;
147 return UBUS_STATUS_NOT_FOUND;
150 return UBUS_STATUS_INVALID_ARGUMENT;
153 return UBUS_STATUS_UNKNOWN_ERROR;
157 static struct blob_attr **
158 rpc_check_path(struct blob_attr *msg, char **path, struct stat *s)
160 static struct blob_attr *tb[__RPC_F_R_MAX];
162 blobmsg_parse(rpc_file_r_policy, __RPC_F_R_MAX, tb, blob_data(msg), blob_len(msg));
164 if (!tb[RPC_F_R_PATH])
170 *path = blobmsg_data(tb[RPC_F_R_PATH]);
179 rpc_file_read(struct ubus_context *ctx, struct ubus_object *obj,
180 struct ubus_request_data *req, const char *method,
181 struct blob_attr *msg)
183 static struct blob_attr *tb[__RPC_F_RB_MAX];
191 blobmsg_parse(rpc_file_rb_policy, __RPC_F_RB_MAX, tb, blob_data(msg), blob_len(msg));
193 if (!tb[RPC_F_RB_PATH])
194 return rpc_errno_status();
196 path = blobmsg_data(tb[RPC_F_RB_PATH]);
199 return rpc_errno_status();
201 if (s.st_size >= RPC_FILE_MAX_SIZE)
202 return UBUS_STATUS_NOT_SUPPORTED;
204 if ((fd = open(path, O_RDONLY)) < 0)
205 return rpc_errno_status();
207 /* some sysfs files do not report a length */
209 s.st_size = RPC_FILE_MIN_SIZE;
211 blob_buf_init(&buf, 0);
213 if (tb[RPC_F_RB_BASE64])
214 base64 = blobmsg_get_bool(tb[RPC_F_RB_BASE64]);
218 len = B64_ENCODE_LEN(s.st_size);
219 wbuf = blobmsg_alloc_string_buffer(&buf, "data", len);
223 rv = UBUS_STATUS_UNKNOWN_ERROR;
227 if ((len = read(fd, wbuf, s.st_size)) <= 0)
229 rv = UBUS_STATUS_NO_DATA;
235 uint8_t *data = calloc(len, sizeof(uint8_t));
238 rv = UBUS_STATUS_UNKNOWN_ERROR;
241 memcpy(data, wbuf, len);
243 len = b64_encode(data, len, wbuf, B64_ENCODE_LEN(len));
247 rv = UBUS_STATUS_UNKNOWN_ERROR;
252 *(wbuf + len) = '\0';
253 blobmsg_add_string_buffer(&buf);
255 ubus_send_reply(ctx, req, buf.head);
265 rpc_file_write(struct ubus_context *ctx, struct ubus_object *obj,
266 struct ubus_request_data *req, const char *method,
267 struct blob_attr *msg)
269 struct blob_attr *tb[__RPC_F_RW_MAX];
270 int append = O_TRUNC;
271 mode_t prev_mode, mode = 0666;
274 ssize_t data_len = 0;
276 blobmsg_parse(rpc_file_rw_policy, __RPC_F_RW_MAX, tb,
277 blob_data(msg), blob_len(msg));
279 if (!tb[RPC_F_RW_PATH] || !tb[RPC_F_RW_DATA])
280 return UBUS_STATUS_INVALID_ARGUMENT;
282 data = blobmsg_data(tb[RPC_F_RW_DATA]);
283 data_len = blobmsg_data_len(tb[RPC_F_RW_DATA]) - 1;
285 if (tb[RPC_F_RW_APPEND] && blobmsg_get_bool(tb[RPC_F_RW_APPEND]))
288 if (tb[RPC_F_RW_MODE])
289 mode = blobmsg_get_u32(tb[RPC_F_RW_MODE]);
291 prev_mode = umask(0);
292 fd = open(blobmsg_data(tb[RPC_F_RW_PATH]), O_CREAT | O_WRONLY | append, mode);
295 return rpc_errno_status();
297 if (tb[RPC_F_RW_BASE64] && blobmsg_get_bool(tb[RPC_F_RW_BASE64]))
299 data_len = b64_decode(data, data, data_len);
302 rv = UBUS_STATUS_UNKNOWN_ERROR;
307 if (write(fd, data, data_len) < 0)
318 return rpc_errno_status();
324 rpc_file_md5(struct ubus_context *ctx, struct ubus_object *obj,
325 struct ubus_request_data *req, const char *method,
326 struct blob_attr *msg)
334 if (!rpc_check_path(msg, &path, &s))
335 return rpc_errno_status();
337 if (!S_ISREG(s.st_mode))
338 return UBUS_STATUS_NOT_SUPPORTED;
340 if ((rv = md5sum(path, md5)) <= 0)
341 return rpc_errno_status();
343 blob_buf_init(&buf, 0);
344 wbuf = blobmsg_alloc_string_buffer(&buf, "md5", 33);
346 for (i = 0; i < 16; i++)
347 sprintf(wbuf + (i * 2), "%02x", (uint8_t) md5[i]);
349 blobmsg_add_string_buffer(&buf);
350 ubus_send_reply(ctx, req, buf.head);
353 return UBUS_STATUS_OK;
357 rpc_file_list(struct ubus_context *ctx, struct ubus_object *obj,
358 struct ubus_request_data *req, const char *method,
359 struct blob_attr *msg)
367 if (!rpc_check_path(msg, &path, &s))
368 return rpc_errno_status();
370 if ((fd = opendir(path)) == NULL)
371 return rpc_errno_status();
373 blob_buf_init(&buf, 0);
374 c = blobmsg_open_array(&buf, "entries");
376 while ((e = readdir(fd)) != NULL)
378 if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, ".."))
381 d = blobmsg_open_table(&buf, NULL);
382 blobmsg_add_string(&buf, "name", e->d_name);
383 blobmsg_add_string(&buf, "type", d_types[e->d_type]);
384 blobmsg_close_table(&buf, d);
389 blobmsg_close_array(&buf, c);
390 ubus_send_reply(ctx, req, buf.head);
397 rpc_file_stat(struct ubus_context *ctx, struct ubus_object *obj,
398 struct ubus_request_data *req, const char *method,
399 struct blob_attr *msg)
405 if (!rpc_check_path(msg, &path, &s))
406 return rpc_errno_status();
408 blob_buf_init(&buf, 0);
410 type = S_ISREG(s.st_mode) ? DT_REG :
411 S_ISDIR(s.st_mode) ? DT_DIR :
412 S_ISCHR(s.st_mode) ? DT_CHR :
413 S_ISBLK(s.st_mode) ? DT_BLK :
414 S_ISFIFO(s.st_mode) ? DT_FIFO :
415 S_ISLNK(s.st_mode) ? DT_LNK :
416 S_ISSOCK(s.st_mode) ? DT_SOCK :
419 blobmsg_add_string(&buf, "path", path);
420 blobmsg_add_string(&buf, "type", d_types[type]);
421 blobmsg_add_u32(&buf, "size", s.st_size);
422 blobmsg_add_u32(&buf, "mode", s.st_mode);
423 blobmsg_add_u32(&buf, "atime", s.st_atime);
424 blobmsg_add_u32(&buf, "mtime", s.st_mtime);
425 blobmsg_add_u32(&buf, "ctime", s.st_ctime);
426 blobmsg_add_u32(&buf, "inode", s.st_ino);
427 blobmsg_add_u32(&buf, "uid", s.st_uid);
428 blobmsg_add_u32(&buf, "gid", s.st_gid);
430 ubus_send_reply(ctx, req, buf.head);
437 rpc_file_exec_lookup(const char *cmd)
440 int plen = 0, clen = strlen(cmd) + 1;
442 static char path[PATH_MAX];
444 if (!stat(cmd, &s) && S_ISREG(s.st_mode))
447 search = getenv("PATH");
450 search = "/bin:/usr/bin:/sbin:/usr/sbin";
456 if (*p != ':' && *p != '\0')
461 if ((plen + clen) >= sizeof(path))
464 strncpy(path, search, plen);
465 sprintf(path + plen, "/%s", cmd);
467 if (!stat(path, &s) && S_ISREG(s.st_mode))
479 rpc_ustream_to_blobmsg(struct ustream *s, const char *name)
484 if ((len = ustream_pending_data(s, false)) > 0)
486 wbuf = blobmsg_alloc_string_buffer(&buf, name, len + 1);
491 ustream_for_each_read_buffer(s, rbuf, len)
493 memcpy(wbuf, rbuf, len);
498 blobmsg_add_string_buffer(&buf);
503 rpc_file_exec_reply(struct rpc_file_exec_context *c, int rv)
505 uloop_timeout_cancel(&c->timeout);
506 uloop_process_delete(&c->process);
508 if (rv == UBUS_STATUS_OK)
510 blob_buf_init(&buf, 0);
512 blobmsg_add_u32(&buf, "code", WEXITSTATUS(c->stat));
514 rpc_ustream_to_blobmsg(&c->opipe.stream, "stdout");
515 rpc_ustream_to_blobmsg(&c->epipe.stream, "stderr");
517 ubus_send_reply(c->context, &c->request, buf.head);
521 ubus_complete_deferred_request(c->context, &c->request, rv);
523 ustream_free(&c->opipe.stream);
524 ustream_free(&c->epipe.stream);
526 close(c->opipe.fd.fd);
527 close(c->epipe.fd.fd);
533 rpc_file_exec_timeout_cb(struct uloop_timeout *t)
535 struct rpc_file_exec_context *c =
536 container_of(t, struct rpc_file_exec_context, timeout);
538 kill(c->process.pid, SIGKILL);
539 rpc_file_exec_reply(c, UBUS_STATUS_TIMEOUT);
543 rpc_file_exec_process_cb(struct uloop_process *p, int stat)
545 struct rpc_file_exec_context *c =
546 container_of(p, struct rpc_file_exec_context, process);
550 ustream_poll(&c->opipe.stream);
551 ustream_poll(&c->epipe.stream);
555 rpc_file_exec_opipe_read_cb(struct ustream *s, int bytes)
557 struct rpc_file_exec_context *c =
558 container_of(s, struct rpc_file_exec_context, opipe.stream);
560 if (ustream_read_buf_full(s))
561 rpc_file_exec_reply(c, UBUS_STATUS_NOT_SUPPORTED);
565 rpc_file_exec_epipe_read_cb(struct ustream *s, int bytes)
567 struct rpc_file_exec_context *c =
568 container_of(s, struct rpc_file_exec_context, epipe.stream);
570 if (ustream_read_buf_full(s))
571 rpc_file_exec_reply(c, UBUS_STATUS_NOT_SUPPORTED);
575 rpc_file_exec_opipe_state_cb(struct ustream *s)
577 struct rpc_file_exec_context *c =
578 container_of(s, struct rpc_file_exec_context, opipe.stream);
580 if (c->opipe.stream.eof && c->epipe.stream.eof)
581 rpc_file_exec_reply(c, UBUS_STATUS_OK);
585 rpc_file_exec_epipe_state_cb(struct ustream *s)
587 struct rpc_file_exec_context *c =
588 container_of(s, struct rpc_file_exec_context, epipe.stream);
590 if (c->opipe.stream.eof && c->epipe.stream.eof)
591 rpc_file_exec_reply(c, UBUS_STATUS_OK);
595 rpc_file_exec_run(const char *cmd,
596 const struct blob_attr *arg, const struct blob_attr *env,
597 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();
643 args = malloc(sizeof(char *) * arglen);
646 return UBUS_STATUS_UNKNOWN_ERROR;
648 args[0] = (char *)cmd;
653 blobmsg_for_each_attr(cur, arg, rem)
655 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
660 if (!(args = realloc(args, sizeof(char *) * arglen)))
661 return UBUS_STATUS_UNKNOWN_ERROR;
663 args[arglen-2] = blobmsg_data(cur);
664 args[arglen-1] = NULL;
670 blobmsg_for_each_attr(cur, env, rem)
672 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
675 setenv(blobmsg_name(cur), blobmsg_data(cur), 1);
679 if (execv(cmd, args))
680 return rpc_errno_status();
683 memset(c, 0, sizeof(*c));
685 ustream_declare(c->opipe, opipe[0], exec_opipe);
686 ustream_declare(c->epipe, epipe[0], exec_epipe);
688 c->process.pid = pid;
689 c->process.cb = rpc_file_exec_process_cb;
690 uloop_process_add(&c->process);
692 c->timeout.cb = rpc_file_exec_timeout_cb;
693 uloop_timeout_set(&c->timeout, RPC_FILE_MAX_RUNTIME);
699 ubus_defer_request(ctx, req, &c->request);
702 return UBUS_STATUS_OK;
706 rpc_file_exec(struct ubus_context *ctx, struct ubus_object *obj,
707 struct ubus_request_data *req, const char *method,
708 struct blob_attr *msg)
710 struct blob_attr *tb[__RPC_E_MAX];
712 blobmsg_parse(rpc_exec_policy, __RPC_E_MAX, tb,
713 blob_data(msg), blob_len(msg));
716 return UBUS_STATUS_INVALID_ARGUMENT;
718 return rpc_file_exec_run(blobmsg_data(tb[RPC_E_CMD]),
719 tb[RPC_E_PARM], tb[RPC_E_ENV], ctx, req);
724 rpc_file_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
726 static const struct ubus_method file_methods[] = {
727 UBUS_METHOD("read", rpc_file_read, rpc_file_rb_policy),
728 UBUS_METHOD("write", rpc_file_write, rpc_file_rw_policy),
729 UBUS_METHOD("list", rpc_file_list, rpc_file_r_policy),
730 UBUS_METHOD("stat", rpc_file_stat, rpc_file_r_policy),
731 UBUS_METHOD("md5", rpc_file_md5, rpc_file_r_policy),
732 UBUS_METHOD("exec", rpc_file_exec, rpc_exec_policy),
735 static struct ubus_object_type file_type =
736 UBUS_OBJECT_TYPE("luci-rpc-file", file_methods);
738 static struct ubus_object obj = {
741 .methods = file_methods,
742 .n_methods = ARRAY_SIZE(file_methods),
745 return ubus_add_object(ctx, &obj);
748 struct rpc_plugin rpc_plugin = {
749 .init = rpc_file_api_init