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
;
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_add_stat(struct stat
*s
)
362 type
= S_ISREG(s
->st_mode
) ? DT_REG
:
363 S_ISDIR(s
->st_mode
) ? DT_DIR
:
364 S_ISCHR(s
->st_mode
) ? DT_CHR
:
365 S_ISBLK(s
->st_mode
) ? DT_BLK
:
366 S_ISFIFO(s
->st_mode
) ? DT_FIFO
:
367 S_ISLNK(s
->st_mode
) ? DT_LNK
:
368 S_ISSOCK(s
->st_mode
) ? DT_SOCK
:
371 blobmsg_add_string(&buf
, "type", d_types
[type
]);
372 blobmsg_add_u32(&buf
, "size", s
->st_size
);
373 blobmsg_add_u32(&buf
, "mode", s
->st_mode
);
374 blobmsg_add_u32(&buf
, "atime", s
->st_atime
);
375 blobmsg_add_u32(&buf
, "mtime", s
->st_mtime
);
376 blobmsg_add_u32(&buf
, "ctime", s
->st_ctime
);
377 blobmsg_add_u32(&buf
, "inode", s
->st_ino
);
378 blobmsg_add_u32(&buf
, "uid", s
->st_uid
);
379 blobmsg_add_u32(&buf
, "gid", s
->st_gid
);
383 rpc_file_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
384 struct ubus_request_data
*req
, const char *method
,
385 struct blob_attr
*msg
)
391 char *path
, *entrypath
;
393 if (!rpc_check_path(msg
, &path
, &s
))
394 return rpc_errno_status();
396 if ((fd
= opendir(path
)) == NULL
)
397 return rpc_errno_status();
399 blob_buf_init(&buf
, 0);
400 c
= blobmsg_open_array(&buf
, "entries");
402 while ((e
= readdir(fd
)) != NULL
)
404 if (!strcmp(e
->d_name
, ".") || !strcmp(e
->d_name
, ".."))
407 if (asprintf(&entrypath
, "%s/%s", path
, e
->d_name
) < 0)
410 if (!stat(entrypath
, &s
))
412 d
= blobmsg_open_table(&buf
, NULL
);
413 blobmsg_add_string(&buf
, "name", e
->d_name
);
414 _rpc_file_add_stat(&s
);
415 blobmsg_close_table(&buf
, d
);
423 blobmsg_close_array(&buf
, c
);
424 ubus_send_reply(ctx
, req
, buf
.head
);
431 rpc_file_stat(struct ubus_context
*ctx
, struct ubus_object
*obj
,
432 struct ubus_request_data
*req
, const char *method
,
433 struct blob_attr
*msg
)
438 if (!rpc_check_path(msg
, &path
, &s
))
439 return rpc_errno_status();
441 blob_buf_init(&buf
, 0);
443 blobmsg_add_string(&buf
, "path", path
);
444 _rpc_file_add_stat(&s
);
446 ubus_send_reply(ctx
, req
, buf
.head
);
453 rpc_file_exec_lookup(const char *cmd
)
456 int plen
= 0, clen
= strlen(cmd
) + 1;
458 static char path
[PATH_MAX
];
460 if (!stat(cmd
, &s
) && S_ISREG(s
.st_mode
))
463 search
= getenv("PATH");
466 search
= "/bin:/usr/bin:/sbin:/usr/sbin";
472 if (*p
!= ':' && *p
!= '\0')
477 if ((plen
+ clen
) >= sizeof(path
))
480 strncpy(path
, search
, plen
);
481 sprintf(path
+ plen
, "/%s", cmd
);
483 if (!stat(path
, &s
) && S_ISREG(s
.st_mode
))
495 rpc_ustream_to_blobmsg(struct ustream
*s
, const char *name
)
500 if ((len
= ustream_pending_data(s
, false)) > 0)
502 wbuf
= blobmsg_alloc_string_buffer(&buf
, name
, len
+ 1);
507 ustream_for_each_read_buffer(s
, rbuf
, len
)
509 memcpy(wbuf
, rbuf
, len
);
514 blobmsg_add_string_buffer(&buf
);
519 rpc_file_exec_reply(struct rpc_file_exec_context
*c
, int rv
)
521 uloop_timeout_cancel(&c
->timeout
);
522 uloop_process_delete(&c
->process
);
524 if (rv
== UBUS_STATUS_OK
)
526 blob_buf_init(&buf
, 0);
528 blobmsg_add_u32(&buf
, "code", WEXITSTATUS(c
->stat
));
530 rpc_ustream_to_blobmsg(&c
->opipe
.stream
, "stdout");
531 rpc_ustream_to_blobmsg(&c
->epipe
.stream
, "stderr");
533 ubus_send_reply(c
->context
, &c
->request
, buf
.head
);
537 ubus_complete_deferred_request(c
->context
, &c
->request
, rv
);
539 ustream_free(&c
->opipe
.stream
);
540 ustream_free(&c
->epipe
.stream
);
542 close(c
->opipe
.fd
.fd
);
543 close(c
->epipe
.fd
.fd
);
549 rpc_file_exec_timeout_cb(struct uloop_timeout
*t
)
551 struct rpc_file_exec_context
*c
=
552 container_of(t
, struct rpc_file_exec_context
, timeout
);
554 kill(c
->process
.pid
, SIGKILL
);
555 rpc_file_exec_reply(c
, UBUS_STATUS_TIMEOUT
);
559 rpc_file_exec_process_cb(struct uloop_process
*p
, int stat
)
561 struct rpc_file_exec_context
*c
=
562 container_of(p
, struct rpc_file_exec_context
, process
);
566 ustream_poll(&c
->opipe
.stream
);
567 ustream_poll(&c
->epipe
.stream
);
571 rpc_file_exec_opipe_read_cb(struct ustream
*s
, int bytes
)
573 struct rpc_file_exec_context
*c
=
574 container_of(s
, struct rpc_file_exec_context
, opipe
.stream
);
576 if (ustream_read_buf_full(s
))
577 rpc_file_exec_reply(c
, UBUS_STATUS_NOT_SUPPORTED
);
581 rpc_file_exec_epipe_read_cb(struct ustream
*s
, int bytes
)
583 struct rpc_file_exec_context
*c
=
584 container_of(s
, struct rpc_file_exec_context
, epipe
.stream
);
586 if (ustream_read_buf_full(s
))
587 rpc_file_exec_reply(c
, UBUS_STATUS_NOT_SUPPORTED
);
591 rpc_file_exec_opipe_state_cb(struct ustream
*s
)
593 struct rpc_file_exec_context
*c
=
594 container_of(s
, struct rpc_file_exec_context
, opipe
.stream
);
596 if (c
->opipe
.stream
.eof
&& c
->epipe
.stream
.eof
)
597 rpc_file_exec_reply(c
, UBUS_STATUS_OK
);
601 rpc_file_exec_epipe_state_cb(struct ustream
*s
)
603 struct rpc_file_exec_context
*c
=
604 container_of(s
, struct rpc_file_exec_context
, epipe
.stream
);
606 if (c
->opipe
.stream
.eof
&& c
->epipe
.stream
.eof
)
607 rpc_file_exec_reply(c
, UBUS_STATUS_OK
);
618 rpc_file_exec_run(const char *cmd
,
619 const struct blob_attr
*arg
, const struct blob_attr
*env
,
620 struct ubus_context
*ctx
, struct ubus_request_data
*req
)
629 struct blob_attr
*cur
;
634 struct rpc_file_exec_context
*c
;
636 cmd
= rpc_file_exec_lookup(cmd
);
639 return UBUS_STATUS_NOT_FOUND
;
641 c
= malloc(sizeof(*c
));
644 return UBUS_STATUS_UNKNOWN_ERROR
;
646 if (pipe(opipe
) || pipe(epipe
))
647 return rpc_errno_status();
649 switch ((pid
= fork()))
652 return rpc_errno_status();
657 devnull
= open("/dev/null", O_RDWR
);
660 return UBUS_STATUS_UNKNOWN_ERROR
;
666 rpc_fdclose(devnull
);
667 rpc_fdclose(opipe
[0]);
668 rpc_fdclose(opipe
[1]);
669 rpc_fdclose(epipe
[0]);
670 rpc_fdclose(epipe
[1]);
673 args
= malloc(sizeof(char *) * arglen
);
676 return UBUS_STATUS_UNKNOWN_ERROR
;
678 args
[0] = (char *)cmd
;
683 blobmsg_for_each_attr(cur
, arg
, rem
)
685 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
691 return UBUS_STATUS_INVALID_ARGUMENT
;
695 tmp
= realloc(args
, sizeof(char *) * arglen
);
700 return UBUS_STATUS_UNKNOWN_ERROR
;
704 args
[arglen
-2] = blobmsg_data(cur
);
705 args
[arglen
-1] = NULL
;
711 blobmsg_for_each_attr(cur
, env
, rem
)
713 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
716 setenv(blobmsg_name(cur
), blobmsg_data(cur
), 1);
720 if (execv(cmd
, args
))
721 return rpc_errno_status();
724 memset(c
, 0, sizeof(*c
));
726 ustream_declare(c
->opipe
, opipe
[0], exec_opipe
);
727 ustream_declare(c
->epipe
, epipe
[0], exec_epipe
);
729 c
->process
.pid
= pid
;
730 c
->process
.cb
= rpc_file_exec_process_cb
;
731 uloop_process_add(&c
->process
);
733 c
->timeout
.cb
= rpc_file_exec_timeout_cb
;
734 uloop_timeout_set(&c
->timeout
, *ops
->exec_timeout
);
740 ubus_defer_request(ctx
, req
, &c
->request
);
743 return UBUS_STATUS_OK
;
747 rpc_file_exec(struct ubus_context
*ctx
, struct ubus_object
*obj
,
748 struct ubus_request_data
*req
, const char *method
,
749 struct blob_attr
*msg
)
751 struct blob_attr
*tb
[__RPC_E_MAX
];
753 blobmsg_parse(rpc_exec_policy
, __RPC_E_MAX
, tb
,
754 blob_data(msg
), blob_len(msg
));
757 return UBUS_STATUS_INVALID_ARGUMENT
;
759 return rpc_file_exec_run(blobmsg_data(tb
[RPC_E_CMD
]),
760 tb
[RPC_E_PARM
], tb
[RPC_E_ENV
], ctx
, req
);
765 rpc_file_api_init(const struct rpc_daemon_ops
*o
, struct ubus_context
*ctx
)
767 static const struct ubus_method file_methods
[] = {
768 UBUS_METHOD("read", rpc_file_read
, rpc_file_rb_policy
),
769 UBUS_METHOD("write", rpc_file_write
, rpc_file_rw_policy
),
770 UBUS_METHOD("list", rpc_file_list
, rpc_file_r_policy
),
771 UBUS_METHOD("stat", rpc_file_stat
, rpc_file_r_policy
),
772 UBUS_METHOD("md5", rpc_file_md5
, rpc_file_r_policy
),
773 UBUS_METHOD("exec", rpc_file_exec
, rpc_exec_policy
),
776 static struct ubus_object_type file_type
=
777 UBUS_OBJECT_TYPE("luci-rpc-file", file_methods
);
779 static struct ubus_object obj
= {
782 .methods
= file_methods
,
783 .n_methods
= ARRAY_SIZE(file_methods
),
788 return ubus_add_object(ctx
, &obj
);
791 struct rpc_plugin rpc_plugin
= {
792 .init
= rpc_file_api_init