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.
30 #include <libubox/blobmsg.h>
31 #include <libubox/md5.h>
32 #include <libubox/ustream.h>
33 #include <libubox/utils.h>
35 #include <rpcd/plugin.h>
37 /* limit of sys & proc files */
38 #define RPC_FILE_MIN_SIZE (128)
40 /* limit of regular files and command output data */
41 #define RPC_FILE_MAX_SIZE (4096 * 64)
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 static const struct rpc_daemon_ops
*ops
;
58 struct rpc_file_exec_context
{
59 struct ubus_context
*context
;
60 struct ubus_request_data request
;
61 struct uloop_timeout timeout
;
62 struct uloop_process process
;
63 struct ustream_fd opipe
;
64 struct ustream_fd epipe
;
73 static struct blob_buf buf
;
80 static const struct blobmsg_policy rpc_file_r_policy
[__RPC_F_R_MAX
] = {
81 [RPC_F_R_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
90 static const struct blobmsg_policy rpc_file_rb_policy
[__RPC_F_RB_MAX
] = {
91 [RPC_F_RB_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
92 [RPC_F_RB_BASE64
] = { .name
= "base64", .type
= BLOBMSG_TYPE_BOOL
},
104 static const struct blobmsg_policy rpc_file_rw_policy
[__RPC_F_RW_MAX
] = {
105 [RPC_F_RW_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
106 [RPC_F_RW_DATA
] = { .name
= "data", .type
= BLOBMSG_TYPE_STRING
},
107 [RPC_F_RW_APPEND
] = { .name
= "append", .type
= BLOBMSG_TYPE_BOOL
},
108 [RPC_F_RW_MODE
] = { .name
= "mode", .type
= BLOBMSG_TYPE_INT32
},
109 [RPC_F_RW_BASE64
] = { .name
= "base64", .type
= BLOBMSG_TYPE_BOOL
},
119 static const struct blobmsg_policy rpc_exec_policy
[__RPC_E_MAX
] = {
120 [RPC_E_CMD
] = { .name
= "command", .type
= BLOBMSG_TYPE_STRING
},
121 [RPC_E_PARM
] = { .name
= "params", .type
= BLOBMSG_TYPE_ARRAY
},
122 [RPC_E_ENV
] = { .name
= "env", .type
= BLOBMSG_TYPE_TABLE
},
125 static const char *d_types
[] = {
128 [DT_DIR
] = "directory",
130 [DT_LNK
] = "symlink",
132 [DT_SOCK
] = "socket",
133 [DT_UNKNOWN
] = "unknown",
138 rpc_errno_status(void)
143 return UBUS_STATUS_PERMISSION_DENIED
;
146 return UBUS_STATUS_INVALID_ARGUMENT
;
149 return UBUS_STATUS_NOT_FOUND
;
152 return UBUS_STATUS_INVALID_ARGUMENT
;
155 return UBUS_STATUS_UNKNOWN_ERROR
;
159 static struct blob_attr
**
160 rpc_check_path(struct blob_attr
*msg
, char **path
, struct stat
*s
)
162 static struct blob_attr
*tb
[__RPC_F_R_MAX
];
164 blobmsg_parse(rpc_file_r_policy
, __RPC_F_R_MAX
, tb
, blob_data(msg
), blob_len(msg
));
166 if (!tb
[RPC_F_R_PATH
])
172 *path
= blobmsg_data(tb
[RPC_F_R_PATH
]);
181 rpc_file_read(struct ubus_context
*ctx
, struct ubus_object
*obj
,
182 struct ubus_request_data
*req
, const char *method
,
183 struct blob_attr
*msg
)
185 static struct blob_attr
*tb
[__RPC_F_RB_MAX
];
193 blobmsg_parse(rpc_file_rb_policy
, __RPC_F_RB_MAX
, tb
, blob_data(msg
), blob_len(msg
));
195 if (!tb
[RPC_F_RB_PATH
])
196 return rpc_errno_status();
198 path
= blobmsg_data(tb
[RPC_F_RB_PATH
]);
201 return rpc_errno_status();
203 if (s
.st_size
>= RPC_FILE_MAX_SIZE
)
204 return UBUS_STATUS_NOT_SUPPORTED
;
206 if ((fd
= open(path
, O_RDONLY
)) < 0)
207 return rpc_errno_status();
209 /* some sysfs files do not report a length */
211 s
.st_size
= RPC_FILE_MIN_SIZE
;
213 blob_buf_init(&buf
, 0);
215 if (tb
[RPC_F_RB_BASE64
])
216 base64
= blobmsg_get_bool(tb
[RPC_F_RB_BASE64
]);
220 len
= B64_ENCODE_LEN(s
.st_size
);
221 wbuf
= blobmsg_alloc_string_buffer(&buf
, "data", len
);
225 rv
= UBUS_STATUS_UNKNOWN_ERROR
;
229 if ((len
= read(fd
, wbuf
, s
.st_size
)) <= 0)
231 rv
= UBUS_STATUS_NO_DATA
;
237 uint8_t *data
= calloc(len
, sizeof(uint8_t));
240 rv
= UBUS_STATUS_UNKNOWN_ERROR
;
243 memcpy(data
, wbuf
, len
);
245 len
= b64_encode(data
, len
, wbuf
, B64_ENCODE_LEN(len
));
249 rv
= UBUS_STATUS_UNKNOWN_ERROR
;
254 *(wbuf
+ len
) = '\0';
255 blobmsg_add_string_buffer(&buf
);
257 ubus_send_reply(ctx
, req
, buf
.head
);
267 rpc_file_write(struct ubus_context
*ctx
, struct ubus_object
*obj
,
268 struct ubus_request_data
*req
, const char *method
,
269 struct blob_attr
*msg
)
271 struct blob_attr
*tb
[__RPC_F_RW_MAX
];
272 int append
= O_TRUNC
;
273 mode_t prev_mode
, mode
= 0666;
276 ssize_t data_len
= 0;
278 blobmsg_parse(rpc_file_rw_policy
, __RPC_F_RW_MAX
, tb
,
279 blob_data(msg
), blob_len(msg
));
281 if (!tb
[RPC_F_RW_PATH
] || !tb
[RPC_F_RW_DATA
])
282 return UBUS_STATUS_INVALID_ARGUMENT
;
284 data
= blobmsg_data(tb
[RPC_F_RW_DATA
]);
285 data_len
= blobmsg_data_len(tb
[RPC_F_RW_DATA
]) - 1;
287 if (tb
[RPC_F_RW_APPEND
] && blobmsg_get_bool(tb
[RPC_F_RW_APPEND
]))
290 if (tb
[RPC_F_RW_MODE
])
291 mode
= blobmsg_get_u32(tb
[RPC_F_RW_MODE
]);
293 prev_mode
= umask(0);
294 fd
= open(blobmsg_data(tb
[RPC_F_RW_PATH
]), O_CREAT
| O_WRONLY
| append
, mode
);
297 return rpc_errno_status();
299 if (tb
[RPC_F_RW_BASE64
] && blobmsg_get_bool(tb
[RPC_F_RW_BASE64
]))
301 data_len
= b64_decode(data
, data
, data_len
);
304 rv
= UBUS_STATUS_UNKNOWN_ERROR
;
309 if (write(fd
, data
, data_len
) < 0)
320 return rpc_errno_status();
326 rpc_file_md5(struct ubus_context
*ctx
, struct ubus_object
*obj
,
327 struct ubus_request_data
*req
, const char *method
,
328 struct blob_attr
*msg
)
336 if (!rpc_check_path(msg
, &path
, &s
))
337 return rpc_errno_status();
339 if (!S_ISREG(s
.st_mode
))
340 return UBUS_STATUS_NOT_SUPPORTED
;
342 if ((rv
= md5sum(path
, md5
)) <= 0)
343 return rpc_errno_status();
345 blob_buf_init(&buf
, 0);
346 wbuf
= blobmsg_alloc_string_buffer(&buf
, "md5", 33);
348 for (i
= 0; i
< 16; i
++)
349 sprintf(wbuf
+ (i
* 2), "%02x", (uint8_t) md5
[i
]);
351 blobmsg_add_string_buffer(&buf
);
352 ubus_send_reply(ctx
, req
, buf
.head
);
355 return UBUS_STATUS_OK
;
359 rpc_file_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
360 struct ubus_request_data
*req
, const char *method
,
361 struct blob_attr
*msg
)
369 if (!rpc_check_path(msg
, &path
, &s
))
370 return rpc_errno_status();
372 if ((fd
= opendir(path
)) == NULL
)
373 return rpc_errno_status();
375 blob_buf_init(&buf
, 0);
376 c
= blobmsg_open_array(&buf
, "entries");
378 while ((e
= readdir(fd
)) != NULL
)
380 if (!strcmp(e
->d_name
, ".") || !strcmp(e
->d_name
, ".."))
383 d
= blobmsg_open_table(&buf
, NULL
);
384 blobmsg_add_string(&buf
, "name", e
->d_name
);
385 blobmsg_add_string(&buf
, "type", d_types
[e
->d_type
]);
386 blobmsg_close_table(&buf
, d
);
391 blobmsg_close_array(&buf
, c
);
392 ubus_send_reply(ctx
, req
, buf
.head
);
399 rpc_file_stat(struct ubus_context
*ctx
, struct ubus_object
*obj
,
400 struct ubus_request_data
*req
, const char *method
,
401 struct blob_attr
*msg
)
407 if (!rpc_check_path(msg
, &path
, &s
))
408 return rpc_errno_status();
410 blob_buf_init(&buf
, 0);
412 type
= S_ISREG(s
.st_mode
) ? DT_REG
:
413 S_ISDIR(s
.st_mode
) ? DT_DIR
:
414 S_ISCHR(s
.st_mode
) ? DT_CHR
:
415 S_ISBLK(s
.st_mode
) ? DT_BLK
:
416 S_ISFIFO(s
.st_mode
) ? DT_FIFO
:
417 S_ISLNK(s
.st_mode
) ? DT_LNK
:
418 S_ISSOCK(s
.st_mode
) ? DT_SOCK
:
421 blobmsg_add_string(&buf
, "path", path
);
422 blobmsg_add_string(&buf
, "type", d_types
[type
]);
423 blobmsg_add_u32(&buf
, "size", s
.st_size
);
424 blobmsg_add_u32(&buf
, "mode", s
.st_mode
);
425 blobmsg_add_u32(&buf
, "atime", s
.st_atime
);
426 blobmsg_add_u32(&buf
, "mtime", s
.st_mtime
);
427 blobmsg_add_u32(&buf
, "ctime", s
.st_ctime
);
428 blobmsg_add_u32(&buf
, "inode", s
.st_ino
);
429 blobmsg_add_u32(&buf
, "uid", s
.st_uid
);
430 blobmsg_add_u32(&buf
, "gid", s
.st_gid
);
432 ubus_send_reply(ctx
, req
, buf
.head
);
439 rpc_file_exec_lookup(const char *cmd
)
442 int plen
= 0, clen
= strlen(cmd
) + 1;
444 static char path
[PATH_MAX
];
446 if (!stat(cmd
, &s
) && S_ISREG(s
.st_mode
))
449 search
= getenv("PATH");
452 search
= "/bin:/usr/bin:/sbin:/usr/sbin";
458 if (*p
!= ':' && *p
!= '\0')
463 if ((plen
+ clen
) >= sizeof(path
))
466 strncpy(path
, search
, plen
);
467 sprintf(path
+ plen
, "/%s", cmd
);
469 if (!stat(path
, &s
) && S_ISREG(s
.st_mode
))
481 rpc_ustream_to_blobmsg(struct ustream
*s
, const char *name
)
486 if ((len
= ustream_pending_data(s
, false)) > 0)
488 wbuf
= blobmsg_alloc_string_buffer(&buf
, name
, len
+ 1);
493 ustream_for_each_read_buffer(s
, rbuf
, len
)
495 memcpy(wbuf
, rbuf
, len
);
500 blobmsg_add_string_buffer(&buf
);
505 rpc_file_exec_reply(struct rpc_file_exec_context
*c
, int rv
)
507 uloop_timeout_cancel(&c
->timeout
);
508 uloop_process_delete(&c
->process
);
510 if (rv
== UBUS_STATUS_OK
)
512 blob_buf_init(&buf
, 0);
514 blobmsg_add_u32(&buf
, "code", WEXITSTATUS(c
->stat
));
516 rpc_ustream_to_blobmsg(&c
->opipe
.stream
, "stdout");
517 rpc_ustream_to_blobmsg(&c
->epipe
.stream
, "stderr");
519 ubus_send_reply(c
->context
, &c
->request
, buf
.head
);
523 ubus_complete_deferred_request(c
->context
, &c
->request
, rv
);
525 ustream_free(&c
->opipe
.stream
);
526 ustream_free(&c
->epipe
.stream
);
528 close(c
->opipe
.fd
.fd
);
529 close(c
->epipe
.fd
.fd
);
535 rpc_file_exec_timeout_cb(struct uloop_timeout
*t
)
537 struct rpc_file_exec_context
*c
=
538 container_of(t
, struct rpc_file_exec_context
, timeout
);
540 kill(c
->process
.pid
, SIGKILL
);
541 rpc_file_exec_reply(c
, UBUS_STATUS_TIMEOUT
);
545 rpc_file_exec_process_cb(struct uloop_process
*p
, int stat
)
547 struct rpc_file_exec_context
*c
=
548 container_of(p
, struct rpc_file_exec_context
, process
);
552 ustream_poll(&c
->opipe
.stream
);
553 ustream_poll(&c
->epipe
.stream
);
557 rpc_file_exec_opipe_read_cb(struct ustream
*s
, int bytes
)
559 struct rpc_file_exec_context
*c
=
560 container_of(s
, struct rpc_file_exec_context
, opipe
.stream
);
562 if (ustream_read_buf_full(s
))
563 rpc_file_exec_reply(c
, UBUS_STATUS_NOT_SUPPORTED
);
567 rpc_file_exec_epipe_read_cb(struct ustream
*s
, int bytes
)
569 struct rpc_file_exec_context
*c
=
570 container_of(s
, struct rpc_file_exec_context
, epipe
.stream
);
572 if (ustream_read_buf_full(s
))
573 rpc_file_exec_reply(c
, UBUS_STATUS_NOT_SUPPORTED
);
577 rpc_file_exec_opipe_state_cb(struct ustream
*s
)
579 struct rpc_file_exec_context
*c
=
580 container_of(s
, struct rpc_file_exec_context
, opipe
.stream
);
582 if (c
->opipe
.stream
.eof
&& c
->epipe
.stream
.eof
)
583 rpc_file_exec_reply(c
, UBUS_STATUS_OK
);
587 rpc_file_exec_epipe_state_cb(struct ustream
*s
)
589 struct rpc_file_exec_context
*c
=
590 container_of(s
, struct rpc_file_exec_context
, epipe
.stream
);
592 if (c
->opipe
.stream
.eof
&& c
->epipe
.stream
.eof
)
593 rpc_file_exec_reply(c
, UBUS_STATUS_OK
);
597 rpc_file_exec_run(const char *cmd
,
598 const struct blob_attr
*arg
, const struct blob_attr
*env
,
599 struct ubus_context
*ctx
, struct ubus_request_data
*req
)
607 struct blob_attr
*cur
;
612 struct rpc_file_exec_context
*c
;
614 cmd
= rpc_file_exec_lookup(cmd
);
617 return UBUS_STATUS_NOT_FOUND
;
619 c
= malloc(sizeof(*c
));
622 return UBUS_STATUS_UNKNOWN_ERROR
;
624 if (pipe(opipe
) || pipe(epipe
))
625 return rpc_errno_status();
627 switch ((pid
= fork()))
630 return rpc_errno_status();
645 args
= malloc(sizeof(char *) * arglen
);
648 return UBUS_STATUS_UNKNOWN_ERROR
;
650 args
[0] = (char *)cmd
;
655 blobmsg_for_each_attr(cur
, arg
, rem
)
657 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
662 if (!(args
= realloc(args
, sizeof(char *) * arglen
)))
663 return UBUS_STATUS_UNKNOWN_ERROR
;
665 args
[arglen
-2] = blobmsg_data(cur
);
666 args
[arglen
-1] = NULL
;
672 blobmsg_for_each_attr(cur
, env
, rem
)
674 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
677 setenv(blobmsg_name(cur
), blobmsg_data(cur
), 1);
681 if (execv(cmd
, args
))
682 return rpc_errno_status();
685 memset(c
, 0, sizeof(*c
));
687 ustream_declare(c
->opipe
, opipe
[0], exec_opipe
);
688 ustream_declare(c
->epipe
, epipe
[0], exec_epipe
);
690 c
->process
.pid
= pid
;
691 c
->process
.cb
= rpc_file_exec_process_cb
;
692 uloop_process_add(&c
->process
);
694 c
->timeout
.cb
= rpc_file_exec_timeout_cb
;
695 uloop_timeout_set(&c
->timeout
, *ops
->exec_timeout
);
701 ubus_defer_request(ctx
, req
, &c
->request
);
704 return UBUS_STATUS_OK
;
708 rpc_file_exec(struct ubus_context
*ctx
, struct ubus_object
*obj
,
709 struct ubus_request_data
*req
, const char *method
,
710 struct blob_attr
*msg
)
712 struct blob_attr
*tb
[__RPC_E_MAX
];
714 blobmsg_parse(rpc_exec_policy
, __RPC_E_MAX
, tb
,
715 blob_data(msg
), blob_len(msg
));
718 return UBUS_STATUS_INVALID_ARGUMENT
;
720 return rpc_file_exec_run(blobmsg_data(tb
[RPC_E_CMD
]),
721 tb
[RPC_E_PARM
], tb
[RPC_E_ENV
], ctx
, req
);
726 rpc_file_api_init(const struct rpc_daemon_ops
*o
, struct ubus_context
*ctx
)
728 static const struct ubus_method file_methods
[] = {
729 UBUS_METHOD("read", rpc_file_read
, rpc_file_rb_policy
),
730 UBUS_METHOD("write", rpc_file_write
, rpc_file_rw_policy
),
731 UBUS_METHOD("list", rpc_file_list
, rpc_file_r_policy
),
732 UBUS_METHOD("stat", rpc_file_stat
, rpc_file_r_policy
),
733 UBUS_METHOD("md5", rpc_file_md5
, rpc_file_r_policy
),
734 UBUS_METHOD("exec", rpc_file_exec
, rpc_exec_policy
),
737 static struct ubus_object_type file_type
=
738 UBUS_OBJECT_TYPE("luci-rpc-file", file_methods
);
740 static struct ubus_object obj
= {
743 .methods
= file_methods
,
744 .n_methods
= ARRAY_SIZE(file_methods
),
749 return ubus_add_object(ctx
, &obj
);
752 struct rpc_plugin rpc_plugin
= {
753 .init
= rpc_file_api_init