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.
33 #include <libubox/blobmsg.h>
34 #include <libubox/md5.h>
35 #include <libubox/ustream.h>
36 #include <libubox/utils.h>
38 #include <rpcd/plugin.h>
40 /* limit of sys & proc files */
41 #define RPC_FILE_MIN_SIZE (128)
43 /* limit of regular files and command output data */
44 #define RPC_FILE_MAX_SIZE (4096 * 64)
46 #define ustream_for_each_read_buffer(stream, ptr, len) \
47 for (ptr = ustream_get_read_buf(stream, &len); \
48 ptr != NULL && len > 0; \
49 ustream_consume(stream, len), ptr = ustream_get_read_buf(stream, &len))
51 #define ustream_declare(us, fd, name) \
52 us.stream.string_data = true; \
53 us.stream.r.buffer_len = 4096; \
54 us.stream.r.max_buffers = RPC_FILE_MAX_SIZE / 4096; \
55 us.stream.notify_read = rpc_file_##name##_read_cb; \
56 us.stream.notify_state = rpc_file_##name##_state_cb; \
57 ustream_fd_init(&us, fd);
59 static const struct rpc_daemon_ops
*ops
;
61 struct rpc_file_exec_context
{
62 struct ubus_context
*context
;
63 struct ubus_request_data request
;
64 struct uloop_timeout timeout
;
65 struct uloop_process process
;
66 struct ustream_fd opipe
;
67 struct ustream_fd epipe
;
72 static struct blob_buf buf
;
73 static char *canonpath
;
81 static const struct blobmsg_policy rpc_file_r_policy
[__RPC_F_R_MAX
] = {
82 [RPC_F_R_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
83 [RPC_F_R_SESSION
] = { .name
= "ubus_rpc_session",
84 .type
= BLOBMSG_TYPE_STRING
},
94 static const struct blobmsg_policy rpc_file_rb_policy
[__RPC_F_RB_MAX
] = {
95 [RPC_F_RB_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
96 [RPC_F_RB_BASE64
] = { .name
= "base64", .type
= BLOBMSG_TYPE_BOOL
},
97 [RPC_F_RB_SESSION
] = { .name
= "ubus_rpc_session",
98 .type
= BLOBMSG_TYPE_STRING
},
111 static const struct blobmsg_policy rpc_file_rw_policy
[__RPC_F_RW_MAX
] = {
112 [RPC_F_RW_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
113 [RPC_F_RW_DATA
] = { .name
= "data", .type
= BLOBMSG_TYPE_STRING
},
114 [RPC_F_RW_APPEND
] = { .name
= "append", .type
= BLOBMSG_TYPE_BOOL
},
115 [RPC_F_RW_MODE
] = { .name
= "mode", .type
= BLOBMSG_TYPE_INT32
},
116 [RPC_F_RW_BASE64
] = { .name
= "base64", .type
= BLOBMSG_TYPE_BOOL
},
117 [RPC_F_RW_SESSION
] = { .name
= "ubus_rpc_session",
118 .type
= BLOBMSG_TYPE_STRING
},
129 static const struct blobmsg_policy rpc_exec_policy
[__RPC_E_MAX
] = {
130 [RPC_E_CMD
] = { .name
= "command", .type
= BLOBMSG_TYPE_STRING
},
131 [RPC_E_PARM
] = { .name
= "params", .type
= BLOBMSG_TYPE_ARRAY
},
132 [RPC_E_ENV
] = { .name
= "env", .type
= BLOBMSG_TYPE_TABLE
},
133 [RPC_E_SESSION
] = { .name
= "ubus_rpc_session",
134 .type
= BLOBMSG_TYPE_STRING
},
137 static const char *d_types
[] = {
140 [DT_DIR
] = "directory",
142 [DT_LNK
] = "symlink",
144 [DT_SOCK
] = "socket",
145 [DT_UNKNOWN
] = "unknown",
150 rpc_errno_status(void)
155 return UBUS_STATUS_PERMISSION_DENIED
;
158 return UBUS_STATUS_INVALID_ARGUMENT
;
161 return UBUS_STATUS_NOT_FOUND
;
164 return UBUS_STATUS_INVALID_ARGUMENT
;
167 return UBUS_STATUS_UNKNOWN_ERROR
;
172 rpc_file_read_access(const struct blob_attr
*sid
, const char *path
)
177 return ops
->session_access(blobmsg_data(sid
), "file", path
, "read");
181 rpc_file_write_access(const struct blob_attr
*sid
, const char *path
)
186 return ops
->session_access(blobmsg_data(sid
), "file", path
, "write");
190 rpc_file_exec_access(const struct blob_attr
*sid
, const char *path
)
195 return ops
->session_access(blobmsg_data(sid
), "file", path
, "exec");
199 rpc_canonicalize_path(const char *path
)
204 if (path
== NULL
|| *path
== '\0')
207 if (canonpath
!= NULL
)
210 canonpath
= strdup(path
);
212 if (canonpath
== NULL
)
216 for (cp
= canonpath
, p
= path
; *p
!= '\0'; ) {
220 /* skip repeating / */
229 if ((p
[2] == '\0') || (p
[2] == '/')) {
234 /* collapse /x/../ */
235 if ((p
[2] == '.') && ((p
[3] == '\0') || (p
[3] == '/'))) {
236 while ((cp
> canonpath
) && (*--cp
!= '/'))
248 /* remove trailing slash if not root / */
249 if ((cp
> canonpath
+ 1) && (cp
[-1] == '/'))
251 else if (cp
== canonpath
)
259 static struct blob_attr
**
260 rpc_check_path(struct blob_attr
*msg
, char **path
, struct stat
*s
)
262 static struct blob_attr
*tb
[__RPC_F_R_MAX
];
264 blobmsg_parse(rpc_file_r_policy
, __RPC_F_R_MAX
, tb
, blob_data(msg
), blob_len(msg
));
266 if (!tb
[RPC_F_R_PATH
])
272 *path
= rpc_canonicalize_path(blobmsg_get_string(tb
[RPC_F_R_PATH
]));
280 if (!rpc_file_read_access(tb
[RPC_F_R_SESSION
], *path
))
293 rpc_file_read(struct ubus_context
*ctx
, struct ubus_object
*obj
,
294 struct ubus_request_data
*req
, const char *method
,
295 struct blob_attr
*msg
)
297 static struct blob_attr
*tb
[__RPC_F_RB_MAX
];
305 blobmsg_parse(rpc_file_rb_policy
, __RPC_F_RB_MAX
, tb
, blob_data(msg
), blob_len(msg
));
307 if (!tb
[RPC_F_RB_PATH
])
308 return rpc_errno_status();
310 path
= rpc_canonicalize_path(blobmsg_get_string(tb
[RPC_F_RB_PATH
]));
313 return UBUS_STATUS_UNKNOWN_ERROR
;
315 if (!rpc_file_read_access(tb
[RPC_F_RB_SESSION
], path
))
316 return UBUS_STATUS_PERMISSION_DENIED
;
319 return rpc_errno_status();
321 if (s
.st_size
>= RPC_FILE_MAX_SIZE
)
322 return UBUS_STATUS_NOT_SUPPORTED
;
324 if ((fd
= open(path
, O_RDONLY
)) < 0)
325 return rpc_errno_status();
327 /* some sysfs files do not report a length */
329 s
.st_size
= RPC_FILE_MIN_SIZE
;
331 blob_buf_init(&buf
, 0);
333 if (tb
[RPC_F_RB_BASE64
])
334 base64
= blobmsg_get_bool(tb
[RPC_F_RB_BASE64
]);
338 len
= B64_ENCODE_LEN(s
.st_size
);
339 wbuf
= blobmsg_alloc_string_buffer(&buf
, "data", len
);
343 rv
= UBUS_STATUS_UNKNOWN_ERROR
;
347 if ((len
= read(fd
, wbuf
, s
.st_size
)) <= 0)
349 rv
= UBUS_STATUS_NO_DATA
;
355 uint8_t *data
= calloc(len
, sizeof(uint8_t));
358 rv
= UBUS_STATUS_UNKNOWN_ERROR
;
361 memcpy(data
, wbuf
, len
);
363 len
= b64_encode(data
, len
, wbuf
, B64_ENCODE_LEN(len
));
367 rv
= UBUS_STATUS_UNKNOWN_ERROR
;
372 *(wbuf
+ len
) = '\0';
373 blobmsg_add_string_buffer(&buf
);
375 ubus_send_reply(ctx
, req
, buf
.head
);
385 rpc_file_write(struct ubus_context
*ctx
, struct ubus_object
*obj
,
386 struct ubus_request_data
*req
, const char *method
,
387 struct blob_attr
*msg
)
389 struct blob_attr
*tb
[__RPC_F_RW_MAX
];
390 int append
= O_TRUNC
;
391 mode_t prev_mode
, mode
= 0666;
395 ssize_t data_len
= 0;
397 blobmsg_parse(rpc_file_rw_policy
, __RPC_F_RW_MAX
, tb
,
398 blob_data(msg
), blob_len(msg
));
400 if (!tb
[RPC_F_RW_PATH
] || !tb
[RPC_F_RW_DATA
])
401 return UBUS_STATUS_INVALID_ARGUMENT
;
403 path
= rpc_canonicalize_path(blobmsg_get_string(tb
[RPC_F_RW_PATH
]));
406 return UBUS_STATUS_UNKNOWN_ERROR
;
408 if (!rpc_file_write_access(tb
[RPC_F_RW_SESSION
], path
))
409 return UBUS_STATUS_PERMISSION_DENIED
;
411 data
= blobmsg_data(tb
[RPC_F_RW_DATA
]);
412 data_len
= blobmsg_data_len(tb
[RPC_F_RW_DATA
]) - 1;
414 if (tb
[RPC_F_RW_APPEND
] && blobmsg_get_bool(tb
[RPC_F_RW_APPEND
]))
417 if (tb
[RPC_F_RW_MODE
])
418 mode
= blobmsg_get_u32(tb
[RPC_F_RW_MODE
]);
420 prev_mode
= umask(0);
421 fd
= open(path
, O_CREAT
| O_WRONLY
| append
, mode
);
424 return rpc_errno_status();
426 if (tb
[RPC_F_RW_BASE64
] && blobmsg_get_bool(tb
[RPC_F_RW_BASE64
]))
428 data_len
= b64_decode(data
, data
, data_len
);
431 rv
= UBUS_STATUS_UNKNOWN_ERROR
;
436 if (write(fd
, data
, data_len
) < 0)
447 return rpc_errno_status();
453 rpc_file_md5(struct ubus_context
*ctx
, struct ubus_object
*obj
,
454 struct ubus_request_data
*req
, const char *method
,
455 struct blob_attr
*msg
)
463 if (!rpc_check_path(msg
, &path
, &s
))
464 return rpc_errno_status();
466 if (!S_ISREG(s
.st_mode
))
467 return UBUS_STATUS_NOT_SUPPORTED
;
469 if ((rv
= md5sum(path
, md5
)) <= 0)
470 return rpc_errno_status();
472 blob_buf_init(&buf
, 0);
473 wbuf
= blobmsg_alloc_string_buffer(&buf
, "md5", 33);
475 for (i
= 0; i
< 16; i
++)
476 sprintf(wbuf
+ (i
* 2), "%02x", (uint8_t) md5
[i
]);
478 blobmsg_add_string_buffer(&buf
);
479 ubus_send_reply(ctx
, req
, buf
.head
);
482 return UBUS_STATUS_OK
;
486 _rpc_file_add_stat(struct stat
*s
)
490 type
= S_ISREG(s
->st_mode
) ? DT_REG
:
491 S_ISDIR(s
->st_mode
) ? DT_DIR
:
492 S_ISCHR(s
->st_mode
) ? DT_CHR
:
493 S_ISBLK(s
->st_mode
) ? DT_BLK
:
494 S_ISFIFO(s
->st_mode
) ? DT_FIFO
:
495 S_ISLNK(s
->st_mode
) ? DT_LNK
:
496 S_ISSOCK(s
->st_mode
) ? DT_SOCK
:
499 blobmsg_add_string(&buf
, "type", d_types
[type
]);
500 blobmsg_add_u32(&buf
, "size", s
->st_size
);
501 blobmsg_add_u32(&buf
, "mode", s
->st_mode
);
502 blobmsg_add_u32(&buf
, "atime", s
->st_atime
);
503 blobmsg_add_u32(&buf
, "mtime", s
->st_mtime
);
504 blobmsg_add_u32(&buf
, "ctime", s
->st_ctime
);
505 blobmsg_add_u32(&buf
, "inode", s
->st_ino
);
506 blobmsg_add_u32(&buf
, "uid", s
->st_uid
);
507 blobmsg_add_u32(&buf
, "gid", s
->st_gid
);
511 rpc_file_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
512 struct ubus_request_data
*req
, const char *method
,
513 struct blob_attr
*msg
)
519 char *path
, *entrypath
;
521 if (!rpc_check_path(msg
, &path
, &s
))
522 return rpc_errno_status();
524 if ((fd
= opendir(path
)) == NULL
)
525 return rpc_errno_status();
527 blob_buf_init(&buf
, 0);
528 c
= blobmsg_open_array(&buf
, "entries");
530 while ((e
= readdir(fd
)) != NULL
)
532 if (!strcmp(e
->d_name
, ".") || !strcmp(e
->d_name
, ".."))
535 if (asprintf(&entrypath
, "%s/%s", path
, e
->d_name
) < 0)
538 if (!stat(entrypath
, &s
))
540 d
= blobmsg_open_table(&buf
, NULL
);
541 blobmsg_add_string(&buf
, "name", e
->d_name
);
542 _rpc_file_add_stat(&s
);
543 blobmsg_close_table(&buf
, d
);
551 blobmsg_close_array(&buf
, c
);
552 ubus_send_reply(ctx
, req
, buf
.head
);
559 rpc_file_stat(struct ubus_context
*ctx
, struct ubus_object
*obj
,
560 struct ubus_request_data
*req
, const char *method
,
561 struct blob_attr
*msg
)
566 if (!rpc_check_path(msg
, &path
, &s
))
567 return rpc_errno_status();
569 blob_buf_init(&buf
, 0);
571 blobmsg_add_string(&buf
, "path", path
);
572 _rpc_file_add_stat(&s
);
574 ubus_send_reply(ctx
, req
, buf
.head
);
581 rpc_file_exec_lookup(const char *cmd
)
584 int plen
= 0, clen
= strlen(cmd
) + 1;
586 static char path
[PATH_MAX
];
588 if (!stat(cmd
, &s
) && S_ISREG(s
.st_mode
))
591 search
= getenv("PATH");
594 search
= "/bin:/usr/bin:/sbin:/usr/sbin";
600 if (*p
!= ':' && *p
!= '\0')
605 if ((plen
+ clen
) >= sizeof(path
))
608 strncpy(path
, search
, plen
);
609 sprintf(path
+ plen
, "/%s", cmd
);
611 if (!stat(path
, &s
) && S_ISREG(s
.st_mode
))
623 rpc_ustream_to_blobmsg(struct ustream
*s
, const char *name
)
628 if ((len
= ustream_pending_data(s
, false)) > 0)
630 wbuf
= blobmsg_alloc_string_buffer(&buf
, name
, len
+ 1);
635 ustream_for_each_read_buffer(s
, rbuf
, len
)
637 memcpy(wbuf
, rbuf
, len
);
642 blobmsg_add_string_buffer(&buf
);
647 rpc_file_exec_reply(struct rpc_file_exec_context
*c
, int rv
)
649 uloop_timeout_cancel(&c
->timeout
);
650 uloop_process_delete(&c
->process
);
652 if (rv
== UBUS_STATUS_OK
)
654 blob_buf_init(&buf
, 0);
656 blobmsg_add_u32(&buf
, "code", WEXITSTATUS(c
->stat
));
658 rpc_ustream_to_blobmsg(&c
->opipe
.stream
, "stdout");
659 rpc_ustream_to_blobmsg(&c
->epipe
.stream
, "stderr");
661 ubus_send_reply(c
->context
, &c
->request
, buf
.head
);
665 ubus_complete_deferred_request(c
->context
, &c
->request
, rv
);
667 ustream_free(&c
->opipe
.stream
);
668 ustream_free(&c
->epipe
.stream
);
670 close(c
->opipe
.fd
.fd
);
671 close(c
->epipe
.fd
.fd
);
677 rpc_file_exec_timeout_cb(struct uloop_timeout
*t
)
679 struct rpc_file_exec_context
*c
=
680 container_of(t
, struct rpc_file_exec_context
, timeout
);
682 kill(c
->process
.pid
, SIGKILL
);
683 rpc_file_exec_reply(c
, UBUS_STATUS_TIMEOUT
);
687 rpc_file_exec_process_cb(struct uloop_process
*p
, int stat
)
689 struct rpc_file_exec_context
*c
=
690 container_of(p
, struct rpc_file_exec_context
, process
);
694 ustream_poll(&c
->opipe
.stream
);
695 ustream_poll(&c
->epipe
.stream
);
699 rpc_file_exec_opipe_read_cb(struct ustream
*s
, int bytes
)
701 struct rpc_file_exec_context
*c
=
702 container_of(s
, struct rpc_file_exec_context
, opipe
.stream
);
704 if (ustream_read_buf_full(s
))
705 rpc_file_exec_reply(c
, UBUS_STATUS_NOT_SUPPORTED
);
709 rpc_file_exec_epipe_read_cb(struct ustream
*s
, int bytes
)
711 struct rpc_file_exec_context
*c
=
712 container_of(s
, struct rpc_file_exec_context
, epipe
.stream
);
714 if (ustream_read_buf_full(s
))
715 rpc_file_exec_reply(c
, UBUS_STATUS_NOT_SUPPORTED
);
719 rpc_file_exec_opipe_state_cb(struct ustream
*s
)
721 struct rpc_file_exec_context
*c
=
722 container_of(s
, struct rpc_file_exec_context
, opipe
.stream
);
724 if (c
->opipe
.stream
.eof
&& c
->epipe
.stream
.eof
)
725 rpc_file_exec_reply(c
, UBUS_STATUS_OK
);
729 rpc_file_exec_epipe_state_cb(struct ustream
*s
)
731 struct rpc_file_exec_context
*c
=
732 container_of(s
, struct rpc_file_exec_context
, epipe
.stream
);
734 if (c
->opipe
.stream
.eof
&& c
->epipe
.stream
.eof
)
735 rpc_file_exec_reply(c
, UBUS_STATUS_OK
);
746 rpc_file_exec_run(const char *cmd
, const struct blob_attr
*sid
,
747 const struct blob_attr
*arg
, const struct blob_attr
*env
,
748 struct ubus_context
*ctx
, struct ubus_request_data
*req
)
757 struct blob_attr
*cur
;
760 char *executable
, **args
, **tmp
;
762 struct rpc_file_exec_context
*c
;
764 cmd
= rpc_file_exec_lookup(cmd
);
767 return UBUS_STATUS_NOT_FOUND
;
769 executable
= rpc_canonicalize_path(cmd
);
771 if (executable
== NULL
)
772 return UBUS_STATUS_UNKNOWN_ERROR
;
774 if (!rpc_file_exec_access(sid
, executable
))
775 return UBUS_STATUS_PERMISSION_DENIED
;
777 c
= malloc(sizeof(*c
));
780 return UBUS_STATUS_UNKNOWN_ERROR
;
782 if (pipe(opipe
) || pipe(epipe
))
783 return rpc_errno_status();
785 switch ((pid
= fork()))
788 return rpc_errno_status();
793 devnull
= open("/dev/null", O_RDWR
);
796 return UBUS_STATUS_UNKNOWN_ERROR
;
802 rpc_fdclose(devnull
);
803 rpc_fdclose(opipe
[0]);
804 rpc_fdclose(opipe
[1]);
805 rpc_fdclose(epipe
[0]);
806 rpc_fdclose(epipe
[1]);
809 args
= malloc(sizeof(char *) * arglen
);
812 return UBUS_STATUS_UNKNOWN_ERROR
;
814 args
[0] = (char *)executable
;
819 blobmsg_for_each_attr(cur
, arg
, rem
)
821 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
827 return UBUS_STATUS_INVALID_ARGUMENT
;
831 tmp
= realloc(args
, sizeof(char *) * arglen
);
836 return UBUS_STATUS_UNKNOWN_ERROR
;
840 args
[arglen
-2] = blobmsg_data(cur
);
841 args
[arglen
-1] = NULL
;
847 blobmsg_for_each_attr(cur
, env
, rem
)
849 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
852 setenv(blobmsg_name(cur
), blobmsg_data(cur
), 1);
856 if (execv(executable
, args
))
857 return rpc_errno_status();
860 memset(c
, 0, sizeof(*c
));
862 ustream_declare(c
->opipe
, opipe
[0], exec_opipe
);
863 ustream_declare(c
->epipe
, epipe
[0], exec_epipe
);
865 c
->process
.pid
= pid
;
866 c
->process
.cb
= rpc_file_exec_process_cb
;
867 uloop_process_add(&c
->process
);
869 c
->timeout
.cb
= rpc_file_exec_timeout_cb
;
870 uloop_timeout_set(&c
->timeout
, *ops
->exec_timeout
);
876 ubus_defer_request(ctx
, req
, &c
->request
);
879 return UBUS_STATUS_OK
;
883 rpc_file_exec(struct ubus_context
*ctx
, struct ubus_object
*obj
,
884 struct ubus_request_data
*req
, const char *method
,
885 struct blob_attr
*msg
)
887 struct blob_attr
*tb
[__RPC_E_MAX
];
889 blobmsg_parse(rpc_exec_policy
, __RPC_E_MAX
, tb
,
890 blob_data(msg
), blob_len(msg
));
893 return UBUS_STATUS_INVALID_ARGUMENT
;
895 return rpc_file_exec_run(blobmsg_data(tb
[RPC_E_CMD
]), tb
[RPC_E_SESSION
],
896 tb
[RPC_E_PARM
], tb
[RPC_E_ENV
], ctx
, req
);
901 rpc_file_api_init(const struct rpc_daemon_ops
*o
, struct ubus_context
*ctx
)
903 static const struct ubus_method file_methods
[] = {
904 UBUS_METHOD("read", rpc_file_read
, rpc_file_rb_policy
),
905 UBUS_METHOD("write", rpc_file_write
, rpc_file_rw_policy
),
906 UBUS_METHOD("list", rpc_file_list
, rpc_file_r_policy
),
907 UBUS_METHOD("stat", rpc_file_stat
, rpc_file_r_policy
),
908 UBUS_METHOD("md5", rpc_file_md5
, rpc_file_r_policy
),
909 UBUS_METHOD("exec", rpc_file_exec
, rpc_exec_policy
),
912 static struct ubus_object_type file_type
=
913 UBUS_OBJECT_TYPE("luci-rpc-file", file_methods
);
915 static struct ubus_object obj
= {
918 .methods
= file_methods
,
919 .n_methods
= ARRAY_SIZE(file_methods
),
924 return ubus_add_object(ctx
, &obj
);
927 struct rpc_plugin rpc_plugin
= {
928 .init
= rpc_file_api_init