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.
31 #include <libubox/blobmsg.h>
32 #include <libubox/md5.h>
33 #include <libubox/ustream.h>
34 #include <libubox/utils.h>
36 #include <rpcd/plugin.h>
38 /* limit of sys & proc files */
39 #define RPC_FILE_MIN_SIZE (128)
41 /* limit of regular files and command output data */
42 #define RPC_FILE_MAX_SIZE (4096 * 64)
44 #define ustream_for_each_read_buffer(stream, ptr, len) \
45 for (ptr = ustream_get_read_buf(stream, &len); \
46 ptr != NULL && len > 0; \
47 ustream_consume(stream, len), ptr = ustream_get_read_buf(stream, &len))
49 #define ustream_declare(us, fd, name) \
50 us.stream.string_data = true; \
51 us.stream.r.buffer_len = 4096; \
52 us.stream.r.max_buffers = RPC_FILE_MAX_SIZE / 4096; \
53 us.stream.notify_read = rpc_file_##name##_read_cb; \
54 us.stream.notify_state = rpc_file_##name##_state_cb; \
55 ustream_fd_init(&us, fd);
57 static const struct rpc_daemon_ops
*ops
;
59 struct rpc_file_exec_context
{
60 struct ubus_context
*context
;
61 struct ubus_request_data request
;
62 struct uloop_timeout timeout
;
63 struct uloop_process process
;
64 struct ustream_fd opipe
;
65 struct ustream_fd epipe
;
74 static struct blob_buf buf
;
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
},
91 static const struct blobmsg_policy rpc_file_rb_policy
[__RPC_F_RB_MAX
] = {
92 [RPC_F_RB_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
93 [RPC_F_RB_BASE64
] = { .name
= "base64", .type
= BLOBMSG_TYPE_BOOL
},
105 static const struct blobmsg_policy rpc_file_rw_policy
[__RPC_F_RW_MAX
] = {
106 [RPC_F_RW_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
107 [RPC_F_RW_DATA
] = { .name
= "data", .type
= BLOBMSG_TYPE_STRING
},
108 [RPC_F_RW_APPEND
] = { .name
= "append", .type
= BLOBMSG_TYPE_BOOL
},
109 [RPC_F_RW_MODE
] = { .name
= "mode", .type
= BLOBMSG_TYPE_INT32
},
110 [RPC_F_RW_BASE64
] = { .name
= "base64", .type
= BLOBMSG_TYPE_BOOL
},
120 static const struct blobmsg_policy rpc_exec_policy
[__RPC_E_MAX
] = {
121 [RPC_E_CMD
] = { .name
= "command", .type
= BLOBMSG_TYPE_STRING
},
122 [RPC_E_PARM
] = { .name
= "params", .type
= BLOBMSG_TYPE_ARRAY
},
123 [RPC_E_ENV
] = { .name
= "env", .type
= BLOBMSG_TYPE_TABLE
},
126 static const char *d_types
[] = {
129 [DT_DIR
] = "directory",
131 [DT_LNK
] = "symlink",
133 [DT_SOCK
] = "socket",
134 [DT_UNKNOWN
] = "unknown",
139 rpc_errno_status(void)
144 return UBUS_STATUS_PERMISSION_DENIED
;
147 return UBUS_STATUS_INVALID_ARGUMENT
;
150 return UBUS_STATUS_NOT_FOUND
;
153 return UBUS_STATUS_INVALID_ARGUMENT
;
156 return UBUS_STATUS_UNKNOWN_ERROR
;
160 static struct blob_attr
**
161 rpc_check_path(struct blob_attr
*msg
, char **path
, struct stat
*s
)
163 static struct blob_attr
*tb
[__RPC_F_R_MAX
];
165 blobmsg_parse(rpc_file_r_policy
, __RPC_F_R_MAX
, tb
, blob_data(msg
), blob_len(msg
));
167 if (!tb
[RPC_F_R_PATH
])
173 *path
= blobmsg_data(tb
[RPC_F_R_PATH
]);
182 rpc_file_read(struct ubus_context
*ctx
, struct ubus_object
*obj
,
183 struct ubus_request_data
*req
, const char *method
,
184 struct blob_attr
*msg
)
186 static struct blob_attr
*tb
[__RPC_F_RB_MAX
];
194 blobmsg_parse(rpc_file_rb_policy
, __RPC_F_RB_MAX
, tb
, blob_data(msg
), blob_len(msg
));
196 if (!tb
[RPC_F_RB_PATH
])
197 return rpc_errno_status();
199 path
= blobmsg_data(tb
[RPC_F_RB_PATH
]);
202 return rpc_errno_status();
204 if (s
.st_size
>= RPC_FILE_MAX_SIZE
)
205 return UBUS_STATUS_NOT_SUPPORTED
;
207 if ((fd
= open(path
, O_RDONLY
)) < 0)
208 return rpc_errno_status();
210 /* some sysfs files do not report a length */
212 s
.st_size
= RPC_FILE_MIN_SIZE
;
214 blob_buf_init(&buf
, 0);
216 if (tb
[RPC_F_RB_BASE64
])
217 base64
= blobmsg_get_bool(tb
[RPC_F_RB_BASE64
]);
221 len
= B64_ENCODE_LEN(s
.st_size
);
222 wbuf
= blobmsg_alloc_string_buffer(&buf
, "data", len
);
226 rv
= UBUS_STATUS_UNKNOWN_ERROR
;
230 if ((len
= read(fd
, wbuf
, s
.st_size
)) <= 0)
232 rv
= UBUS_STATUS_NO_DATA
;
238 uint8_t *data
= calloc(len
, sizeof(uint8_t));
241 rv
= UBUS_STATUS_UNKNOWN_ERROR
;
244 memcpy(data
, wbuf
, len
);
246 len
= b64_encode(data
, len
, wbuf
, B64_ENCODE_LEN(len
));
250 rv
= UBUS_STATUS_UNKNOWN_ERROR
;
255 *(wbuf
+ len
) = '\0';
256 blobmsg_add_string_buffer(&buf
);
258 ubus_send_reply(ctx
, req
, buf
.head
);
268 rpc_file_write(struct ubus_context
*ctx
, struct ubus_object
*obj
,
269 struct ubus_request_data
*req
, const char *method
,
270 struct blob_attr
*msg
)
272 struct blob_attr
*tb
[__RPC_F_RW_MAX
];
273 int append
= O_TRUNC
;
274 mode_t prev_mode
, mode
= 0666;
277 ssize_t data_len
= 0;
279 blobmsg_parse(rpc_file_rw_policy
, __RPC_F_RW_MAX
, tb
,
280 blob_data(msg
), blob_len(msg
));
282 if (!tb
[RPC_F_RW_PATH
] || !tb
[RPC_F_RW_DATA
])
283 return UBUS_STATUS_INVALID_ARGUMENT
;
285 data
= blobmsg_data(tb
[RPC_F_RW_DATA
]);
286 data_len
= blobmsg_data_len(tb
[RPC_F_RW_DATA
]) - 1;
288 if (tb
[RPC_F_RW_APPEND
] && blobmsg_get_bool(tb
[RPC_F_RW_APPEND
]))
291 if (tb
[RPC_F_RW_MODE
])
292 mode
= blobmsg_get_u32(tb
[RPC_F_RW_MODE
]);
294 prev_mode
= umask(0);
295 fd
= open(blobmsg_data(tb
[RPC_F_RW_PATH
]), O_CREAT
| O_WRONLY
| append
, mode
);
298 return rpc_errno_status();
300 if (tb
[RPC_F_RW_BASE64
] && blobmsg_get_bool(tb
[RPC_F_RW_BASE64
]))
302 data_len
= b64_decode(data
, data
, data_len
);
305 rv
= UBUS_STATUS_UNKNOWN_ERROR
;
310 if (write(fd
, data
, data_len
) < 0)
321 return rpc_errno_status();
327 rpc_file_md5(struct ubus_context
*ctx
, struct ubus_object
*obj
,
328 struct ubus_request_data
*req
, const char *method
,
329 struct blob_attr
*msg
)
337 if (!rpc_check_path(msg
, &path
, &s
))
338 return rpc_errno_status();
340 if (!S_ISREG(s
.st_mode
))
341 return UBUS_STATUS_NOT_SUPPORTED
;
343 if ((rv
= md5sum(path
, md5
)) <= 0)
344 return rpc_errno_status();
346 blob_buf_init(&buf
, 0);
347 wbuf
= blobmsg_alloc_string_buffer(&buf
, "md5", 33);
349 for (i
= 0; i
< 16; i
++)
350 sprintf(wbuf
+ (i
* 2), "%02x", (uint8_t) md5
[i
]);
352 blobmsg_add_string_buffer(&buf
);
353 ubus_send_reply(ctx
, req
, buf
.head
);
356 return UBUS_STATUS_OK
;
360 rpc_file_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
361 struct ubus_request_data
*req
, const char *method
,
362 struct blob_attr
*msg
)
370 if (!rpc_check_path(msg
, &path
, &s
))
371 return rpc_errno_status();
373 if ((fd
= opendir(path
)) == NULL
)
374 return rpc_errno_status();
376 blob_buf_init(&buf
, 0);
377 c
= blobmsg_open_array(&buf
, "entries");
379 while ((e
= readdir(fd
)) != NULL
)
381 if (!strcmp(e
->d_name
, ".") || !strcmp(e
->d_name
, ".."))
384 d
= blobmsg_open_table(&buf
, NULL
);
385 blobmsg_add_string(&buf
, "name", e
->d_name
);
386 blobmsg_add_string(&buf
, "type", d_types
[e
->d_type
]);
387 blobmsg_close_table(&buf
, d
);
392 blobmsg_close_array(&buf
, c
);
393 ubus_send_reply(ctx
, req
, buf
.head
);
400 rpc_file_stat(struct ubus_context
*ctx
, struct ubus_object
*obj
,
401 struct ubus_request_data
*req
, const char *method
,
402 struct blob_attr
*msg
)
408 if (!rpc_check_path(msg
, &path
, &s
))
409 return rpc_errno_status();
411 blob_buf_init(&buf
, 0);
413 type
= S_ISREG(s
.st_mode
) ? DT_REG
:
414 S_ISDIR(s
.st_mode
) ? DT_DIR
:
415 S_ISCHR(s
.st_mode
) ? DT_CHR
:
416 S_ISBLK(s
.st_mode
) ? DT_BLK
:
417 S_ISFIFO(s
.st_mode
) ? DT_FIFO
:
418 S_ISLNK(s
.st_mode
) ? DT_LNK
:
419 S_ISSOCK(s
.st_mode
) ? DT_SOCK
:
422 blobmsg_add_string(&buf
, "path", path
);
423 blobmsg_add_string(&buf
, "type", d_types
[type
]);
424 blobmsg_add_u32(&buf
, "size", s
.st_size
);
425 blobmsg_add_u32(&buf
, "mode", s
.st_mode
);
426 blobmsg_add_u32(&buf
, "atime", s
.st_atime
);
427 blobmsg_add_u32(&buf
, "mtime", s
.st_mtime
);
428 blobmsg_add_u32(&buf
, "ctime", s
.st_ctime
);
429 blobmsg_add_u32(&buf
, "inode", s
.st_ino
);
430 blobmsg_add_u32(&buf
, "uid", s
.st_uid
);
431 blobmsg_add_u32(&buf
, "gid", s
.st_gid
);
433 ubus_send_reply(ctx
, req
, buf
.head
);
440 rpc_file_exec_lookup(const char *cmd
)
443 int plen
= 0, clen
= strlen(cmd
) + 1;
445 static char path
[PATH_MAX
];
447 if (!stat(cmd
, &s
) && S_ISREG(s
.st_mode
))
450 search
= getenv("PATH");
453 search
= "/bin:/usr/bin:/sbin:/usr/sbin";
459 if (*p
!= ':' && *p
!= '\0')
464 if ((plen
+ clen
) >= sizeof(path
))
467 strncpy(path
, search
, plen
);
468 sprintf(path
+ plen
, "/%s", cmd
);
470 if (!stat(path
, &s
) && S_ISREG(s
.st_mode
))
482 rpc_ustream_to_blobmsg(struct ustream
*s
, const char *name
)
487 if ((len
= ustream_pending_data(s
, false)) > 0)
489 wbuf
= blobmsg_alloc_string_buffer(&buf
, name
, len
+ 1);
494 ustream_for_each_read_buffer(s
, rbuf
, len
)
496 memcpy(wbuf
, rbuf
, len
);
501 blobmsg_add_string_buffer(&buf
);
506 rpc_file_exec_reply(struct rpc_file_exec_context
*c
, int rv
)
508 uloop_timeout_cancel(&c
->timeout
);
509 uloop_process_delete(&c
->process
);
511 if (rv
== UBUS_STATUS_OK
)
513 blob_buf_init(&buf
, 0);
515 blobmsg_add_u32(&buf
, "code", WEXITSTATUS(c
->stat
));
517 rpc_ustream_to_blobmsg(&c
->opipe
.stream
, "stdout");
518 rpc_ustream_to_blobmsg(&c
->epipe
.stream
, "stderr");
520 ubus_send_reply(c
->context
, &c
->request
, buf
.head
);
524 ubus_complete_deferred_request(c
->context
, &c
->request
, rv
);
526 ustream_free(&c
->opipe
.stream
);
527 ustream_free(&c
->epipe
.stream
);
529 close(c
->opipe
.fd
.fd
);
530 close(c
->epipe
.fd
.fd
);
536 rpc_file_exec_timeout_cb(struct uloop_timeout
*t
)
538 struct rpc_file_exec_context
*c
=
539 container_of(t
, struct rpc_file_exec_context
, timeout
);
541 kill(c
->process
.pid
, SIGKILL
);
542 rpc_file_exec_reply(c
, UBUS_STATUS_TIMEOUT
);
546 rpc_file_exec_process_cb(struct uloop_process
*p
, int stat
)
548 struct rpc_file_exec_context
*c
=
549 container_of(p
, struct rpc_file_exec_context
, process
);
553 ustream_poll(&c
->opipe
.stream
);
554 ustream_poll(&c
->epipe
.stream
);
558 rpc_file_exec_opipe_read_cb(struct ustream
*s
, int bytes
)
560 struct rpc_file_exec_context
*c
=
561 container_of(s
, struct rpc_file_exec_context
, opipe
.stream
);
563 if (ustream_read_buf_full(s
))
564 rpc_file_exec_reply(c
, UBUS_STATUS_NOT_SUPPORTED
);
568 rpc_file_exec_epipe_read_cb(struct ustream
*s
, int bytes
)
570 struct rpc_file_exec_context
*c
=
571 container_of(s
, struct rpc_file_exec_context
, epipe
.stream
);
573 if (ustream_read_buf_full(s
))
574 rpc_file_exec_reply(c
, UBUS_STATUS_NOT_SUPPORTED
);
578 rpc_file_exec_opipe_state_cb(struct ustream
*s
)
580 struct rpc_file_exec_context
*c
=
581 container_of(s
, struct rpc_file_exec_context
, opipe
.stream
);
583 if (c
->opipe
.stream
.eof
&& c
->epipe
.stream
.eof
)
584 rpc_file_exec_reply(c
, UBUS_STATUS_OK
);
588 rpc_file_exec_epipe_state_cb(struct ustream
*s
)
590 struct rpc_file_exec_context
*c
=
591 container_of(s
, struct rpc_file_exec_context
, epipe
.stream
);
593 if (c
->opipe
.stream
.eof
&& c
->epipe
.stream
.eof
)
594 rpc_file_exec_reply(c
, UBUS_STATUS_OK
);
598 rpc_file_exec_run(const char *cmd
,
599 const struct blob_attr
*arg
, const struct blob_attr
*env
,
600 struct ubus_context
*ctx
, struct ubus_request_data
*req
)
608 struct blob_attr
*cur
;
613 struct rpc_file_exec_context
*c
;
615 cmd
= rpc_file_exec_lookup(cmd
);
618 return UBUS_STATUS_NOT_FOUND
;
620 c
= malloc(sizeof(*c
));
623 return UBUS_STATUS_UNKNOWN_ERROR
;
625 if (pipe(opipe
) || pipe(epipe
))
626 return rpc_errno_status();
628 switch ((pid
= fork()))
631 return rpc_errno_status();
646 args
= malloc(sizeof(char *) * arglen
);
649 return UBUS_STATUS_UNKNOWN_ERROR
;
651 args
[0] = (char *)cmd
;
656 blobmsg_for_each_attr(cur
, arg
, rem
)
658 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
664 return UBUS_STATUS_INVALID_ARGUMENT
;
668 tmp
= realloc(args
, sizeof(char *) * arglen
);
673 return UBUS_STATUS_UNKNOWN_ERROR
;
677 args
[arglen
-2] = blobmsg_data(cur
);
678 args
[arglen
-1] = NULL
;
684 blobmsg_for_each_attr(cur
, env
, rem
)
686 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
689 setenv(blobmsg_name(cur
), blobmsg_data(cur
), 1);
693 if (execv(cmd
, args
))
694 return rpc_errno_status();
697 memset(c
, 0, sizeof(*c
));
699 ustream_declare(c
->opipe
, opipe
[0], exec_opipe
);
700 ustream_declare(c
->epipe
, epipe
[0], exec_epipe
);
702 c
->process
.pid
= pid
;
703 c
->process
.cb
= rpc_file_exec_process_cb
;
704 uloop_process_add(&c
->process
);
706 c
->timeout
.cb
= rpc_file_exec_timeout_cb
;
707 uloop_timeout_set(&c
->timeout
, *ops
->exec_timeout
);
713 ubus_defer_request(ctx
, req
, &c
->request
);
716 return UBUS_STATUS_OK
;
720 rpc_file_exec(struct ubus_context
*ctx
, struct ubus_object
*obj
,
721 struct ubus_request_data
*req
, const char *method
,
722 struct blob_attr
*msg
)
724 struct blob_attr
*tb
[__RPC_E_MAX
];
726 blobmsg_parse(rpc_exec_policy
, __RPC_E_MAX
, tb
,
727 blob_data(msg
), blob_len(msg
));
730 return UBUS_STATUS_INVALID_ARGUMENT
;
732 return rpc_file_exec_run(blobmsg_data(tb
[RPC_E_CMD
]),
733 tb
[RPC_E_PARM
], tb
[RPC_E_ENV
], ctx
, req
);
738 rpc_file_api_init(const struct rpc_daemon_ops
*o
, struct ubus_context
*ctx
)
740 static const struct ubus_method file_methods
[] = {
741 UBUS_METHOD("read", rpc_file_read
, rpc_file_rb_policy
),
742 UBUS_METHOD("write", rpc_file_write
, rpc_file_rw_policy
),
743 UBUS_METHOD("list", rpc_file_list
, rpc_file_r_policy
),
744 UBUS_METHOD("stat", rpc_file_stat
, rpc_file_r_policy
),
745 UBUS_METHOD("md5", rpc_file_md5
, rpc_file_r_policy
),
746 UBUS_METHOD("exec", rpc_file_exec
, rpc_exec_policy
),
749 static struct ubus_object_type file_type
=
750 UBUS_OBJECT_TYPE("luci-rpc-file", file_methods
);
752 static struct ubus_object obj
= {
755 .methods
= file_methods
,
756 .n_methods
= ARRAY_SIZE(file_methods
),
761 return ubus_add_object(ctx
, &obj
);
764 struct rpc_plugin rpc_plugin
= {
765 .init
= rpc_file_api_init