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/ustream.h>
32 #include <rpcd/plugin.h>
34 /* limit of sys & proc files */
35 #define RPC_FILE_MIN_SIZE (128)
37 /* limit of regular files and command output data */
38 #define RPC_FILE_MAX_SIZE (4096 * 64)
39 #define RPC_FILE_MAX_RUNTIME (3 * 1000)
41 #define ustream_for_each_read_buffer(stream, ptr, len) \
42 for (ptr = ustream_get_read_buf(stream, &len); \
43 ptr != NULL && len > 0; \
44 ustream_consume(stream, len), ptr = ustream_get_read_buf(stream, &len))
46 #define ustream_declare(us, fd, name) \
47 us.stream.string_data = true; \
48 us.stream.r.buffer_len = 4096; \
49 us.stream.r.max_buffers = RPC_FILE_MAX_SIZE / 4096; \
50 us.stream.notify_read = rpc_file_##name##_read_cb; \
51 us.stream.notify_state = rpc_file_##name##_state_cb; \
52 ustream_fd_init(&us, fd);
54 struct rpc_file_exec_context
{
55 struct ubus_context
*context
;
56 struct ubus_request_data request
;
57 struct uloop_timeout timeout
;
58 struct uloop_process process
;
59 struct ustream_fd opipe
;
60 struct ustream_fd epipe
;
69 static struct blob_buf buf
;
76 static const struct blobmsg_policy rpc_file_r_policy
[__RPC_F_R_MAX
] = {
77 [RPC_F_R_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
86 static const struct blobmsg_policy rpc_file_rw_policy
[__RPC_F_RW_MAX
] = {
87 [RPC_F_RW_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
88 [RPC_F_RW_DATA
] = { .name
= "data", .type
= BLOBMSG_TYPE_STRING
},
98 static const struct blobmsg_policy rpc_exec_policy
[__RPC_E_MAX
] = {
99 [RPC_E_CMD
] = { .name
= "command", .type
= BLOBMSG_TYPE_STRING
},
100 [RPC_E_PARM
] = { .name
= "params", .type
= BLOBMSG_TYPE_ARRAY
},
101 [RPC_E_ENV
] = { .name
= "env", .type
= BLOBMSG_TYPE_TABLE
},
104 static const char *d_types
[] = {
107 [DT_DIR
] = "directory",
109 [DT_LNK
] = "symlink",
111 [DT_SOCK
] = "socket",
112 [DT_UNKNOWN
] = "unknown",
117 rpc_errno_status(void)
122 return UBUS_STATUS_PERMISSION_DENIED
;
125 return UBUS_STATUS_INVALID_ARGUMENT
;
128 return UBUS_STATUS_NOT_FOUND
;
131 return UBUS_STATUS_INVALID_ARGUMENT
;
134 return UBUS_STATUS_UNKNOWN_ERROR
;
138 static struct blob_attr
**
139 rpc_check_path(struct blob_attr
*msg
, char **path
, struct stat
*s
)
141 static struct blob_attr
*tb
[__RPC_F_R_MAX
];
143 blobmsg_parse(rpc_file_r_policy
, __RPC_F_R_MAX
, tb
, blob_data(msg
), blob_len(msg
));
145 if (!tb
[RPC_F_R_PATH
])
151 *path
= blobmsg_data(tb
[RPC_F_R_PATH
]);
160 rpc_file_read(struct ubus_context
*ctx
, struct ubus_object
*obj
,
161 struct ubus_request_data
*req
, const char *method
,
162 struct blob_attr
*msg
)
169 if (!rpc_check_path(msg
, &path
, &s
))
170 return rpc_errno_status();
172 if (s
.st_size
>= RPC_FILE_MAX_SIZE
)
173 return UBUS_STATUS_NOT_SUPPORTED
;
175 if ((fd
= open(path
, O_RDONLY
)) < 0)
176 return rpc_errno_status();
178 /* some sysfs files do not report a length */
180 s
.st_size
= RPC_FILE_MIN_SIZE
;
182 blob_buf_init(&buf
, 0);
184 wbuf
= blobmsg_alloc_string_buffer(&buf
, "data", s
.st_size
+ 1);
188 rv
= UBUS_STATUS_UNKNOWN_ERROR
;
192 if ((len
= read(fd
, wbuf
, s
.st_size
)) <= 0)
194 rv
= UBUS_STATUS_NO_DATA
;
199 blobmsg_add_string_buffer(&buf
);
201 ubus_send_reply(ctx
, req
, buf
.head
);
211 rpc_file_write(struct ubus_context
*ctx
, struct ubus_object
*obj
,
212 struct ubus_request_data
*req
, const char *method
,
213 struct blob_attr
*msg
)
216 struct blob_attr
*tb
[__RPC_F_RW_MAX
];
218 blobmsg_parse(rpc_file_rw_policy
, __RPC_F_RW_MAX
, tb
,
219 blob_data(msg
), blob_len(msg
));
221 if (!tb
[RPC_F_RW_PATH
] || !tb
[RPC_F_RW_DATA
])
222 return UBUS_STATUS_INVALID_ARGUMENT
;
224 if ((fd
= open(blobmsg_data(tb
[RPC_F_RW_PATH
]), O_CREAT
| O_TRUNC
| O_WRONLY
, 0666)) < 0)
225 return rpc_errno_status();
227 if (write(fd
, blobmsg_data(tb
[RPC_F_RW_DATA
]), blobmsg_data_len(tb
[RPC_F_RW_DATA
])) < 0)
228 return rpc_errno_status();
231 return rpc_errno_status();
240 rpc_file_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
241 struct ubus_request_data
*req
, const char *method
,
242 struct blob_attr
*msg
)
250 if (!rpc_check_path(msg
, &path
, &s
))
251 return rpc_errno_status();
253 if ((fd
= opendir(path
)) == NULL
)
254 return rpc_errno_status();
256 blob_buf_init(&buf
, 0);
257 c
= blobmsg_open_array(&buf
, "entries");
259 while ((e
= readdir(fd
)) != NULL
)
261 if (!strcmp(e
->d_name
, ".") || !strcmp(e
->d_name
, ".."))
264 d
= blobmsg_open_table(&buf
, NULL
);
265 blobmsg_add_string(&buf
, "name", e
->d_name
);
266 blobmsg_add_string(&buf
, "type", d_types
[e
->d_type
]);
267 blobmsg_close_table(&buf
, d
);
272 blobmsg_close_array(&buf
, c
);
273 ubus_send_reply(ctx
, req
, buf
.head
);
280 rpc_file_stat(struct ubus_context
*ctx
, struct ubus_object
*obj
,
281 struct ubus_request_data
*req
, const char *method
,
282 struct blob_attr
*msg
)
288 if (!rpc_check_path(msg
, &path
, &s
))
289 return rpc_errno_status();
291 blob_buf_init(&buf
, 0);
293 type
= S_ISREG(s
.st_mode
) ? DT_REG
:
294 S_ISDIR(s
.st_mode
) ? DT_DIR
:
295 S_ISCHR(s
.st_mode
) ? DT_CHR
:
296 S_ISBLK(s
.st_mode
) ? DT_BLK
:
297 S_ISFIFO(s
.st_mode
) ? DT_FIFO
:
298 S_ISLNK(s
.st_mode
) ? DT_LNK
:
299 S_ISSOCK(s
.st_mode
) ? DT_SOCK
:
302 blobmsg_add_string(&buf
, "path", path
);
303 blobmsg_add_string(&buf
, "type", d_types
[type
]);
304 blobmsg_add_u32(&buf
, "size", s
.st_size
);
305 blobmsg_add_u32(&buf
, "mode", s
.st_mode
);
306 blobmsg_add_u32(&buf
, "atime", s
.st_atime
);
307 blobmsg_add_u32(&buf
, "mtime", s
.st_mtime
);
308 blobmsg_add_u32(&buf
, "ctime", s
.st_ctime
);
309 blobmsg_add_u32(&buf
, "inode", s
.st_ino
);
310 blobmsg_add_u32(&buf
, "uid", s
.st_uid
);
311 blobmsg_add_u32(&buf
, "gid", s
.st_gid
);
313 ubus_send_reply(ctx
, req
, buf
.head
);
320 rpc_file_exec_lookup(const char *cmd
)
323 int plen
= 0, clen
= strlen(cmd
) + 1;
325 static char path
[PATH_MAX
];
327 if (!stat(cmd
, &s
) && S_ISREG(s
.st_mode
))
330 search
= getenv("PATH");
333 search
= "/bin:/usr/bin:/sbin:/usr/sbin";
339 if (*p
!= ':' && *p
!= '\0')
344 if ((plen
+ clen
) >= sizeof(path
))
347 strncpy(path
, search
, plen
);
348 sprintf(path
+ plen
, "/%s", cmd
);
350 if (!stat(path
, &s
) && S_ISREG(s
.st_mode
))
362 rpc_ustream_to_blobmsg(struct ustream
*s
, const char *name
)
367 if ((len
= ustream_pending_data(s
, false)) > 0)
369 wbuf
= blobmsg_alloc_string_buffer(&buf
, name
, len
+ 1);
374 ustream_for_each_read_buffer(s
, rbuf
, len
)
376 memcpy(wbuf
, rbuf
, len
);
381 blobmsg_add_string_buffer(&buf
);
386 rpc_file_exec_reply(struct rpc_file_exec_context
*c
, int rv
)
388 uloop_timeout_cancel(&c
->timeout
);
389 uloop_process_delete(&c
->process
);
391 if (rv
== UBUS_STATUS_OK
)
393 blob_buf_init(&buf
, 0);
395 blobmsg_add_u32(&buf
, "code", WEXITSTATUS(c
->stat
));
397 rpc_ustream_to_blobmsg(&c
->opipe
.stream
, "stdout");
398 rpc_ustream_to_blobmsg(&c
->epipe
.stream
, "stderr");
400 ubus_send_reply(c
->context
, &c
->request
, buf
.head
);
404 ubus_complete_deferred_request(c
->context
, &c
->request
, rv
);
406 ustream_free(&c
->opipe
.stream
);
407 ustream_free(&c
->epipe
.stream
);
409 close(c
->opipe
.fd
.fd
);
410 close(c
->epipe
.fd
.fd
);
416 rpc_file_exec_timeout_cb(struct uloop_timeout
*t
)
418 struct rpc_file_exec_context
*c
=
419 container_of(t
, struct rpc_file_exec_context
, timeout
);
421 kill(c
->process
.pid
, SIGKILL
);
422 rpc_file_exec_reply(c
, UBUS_STATUS_TIMEOUT
);
426 rpc_file_exec_process_cb(struct uloop_process
*p
, int stat
)
428 struct rpc_file_exec_context
*c
=
429 container_of(p
, struct rpc_file_exec_context
, process
);
433 ustream_poll(&c
->opipe
.stream
);
434 ustream_poll(&c
->epipe
.stream
);
438 rpc_file_exec_opipe_read_cb(struct ustream
*s
, int bytes
)
440 struct rpc_file_exec_context
*c
=
441 container_of(s
, struct rpc_file_exec_context
, opipe
.stream
);
443 if (ustream_read_buf_full(s
))
444 rpc_file_exec_reply(c
, UBUS_STATUS_NOT_SUPPORTED
);
448 rpc_file_exec_epipe_read_cb(struct ustream
*s
, int bytes
)
450 struct rpc_file_exec_context
*c
=
451 container_of(s
, struct rpc_file_exec_context
, epipe
.stream
);
453 if (ustream_read_buf_full(s
))
454 rpc_file_exec_reply(c
, UBUS_STATUS_NOT_SUPPORTED
);
458 rpc_file_exec_opipe_state_cb(struct ustream
*s
)
460 struct rpc_file_exec_context
*c
=
461 container_of(s
, struct rpc_file_exec_context
, opipe
.stream
);
463 if (c
->opipe
.stream
.eof
&& c
->epipe
.stream
.eof
)
464 rpc_file_exec_reply(c
, UBUS_STATUS_OK
);
468 rpc_file_exec_epipe_state_cb(struct ustream
*s
)
470 struct rpc_file_exec_context
*c
=
471 container_of(s
, struct rpc_file_exec_context
, epipe
.stream
);
473 if (c
->opipe
.stream
.eof
&& c
->epipe
.stream
.eof
)
474 rpc_file_exec_reply(c
, UBUS_STATUS_OK
);
478 rpc_file_exec_run(const char *cmd
,
479 const struct blob_attr
*arg
, const struct blob_attr
*env
,
480 struct ubus_context
*ctx
, struct ubus_request_data
*req
)
488 struct blob_attr
*cur
;
493 struct rpc_file_exec_context
*c
;
495 cmd
= rpc_file_exec_lookup(cmd
);
498 return UBUS_STATUS_NOT_FOUND
;
500 c
= malloc(sizeof(*c
));
503 return UBUS_STATUS_UNKNOWN_ERROR
;
505 if (pipe(opipe
) || pipe(epipe
))
506 return rpc_errno_status();
508 switch ((pid
= fork()))
511 return rpc_errno_status();
526 args
= malloc(sizeof(char *) * arglen
);
529 return UBUS_STATUS_UNKNOWN_ERROR
;
531 args
[0] = (char *)cmd
;
536 blobmsg_for_each_attr(cur
, arg
, rem
)
538 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
543 if (!(args
= realloc(args
, sizeof(char *) * arglen
)))
544 return UBUS_STATUS_UNKNOWN_ERROR
;
546 args
[arglen
-2] = blobmsg_data(cur
);
547 args
[arglen
-1] = NULL
;
553 blobmsg_for_each_attr(cur
, env
, rem
)
555 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
558 setenv(blobmsg_name(cur
), blobmsg_data(cur
), 1);
562 if (execv(cmd
, args
))
563 return rpc_errno_status();
566 memset(c
, 0, sizeof(*c
));
568 ustream_declare(c
->opipe
, opipe
[0], exec_opipe
);
569 ustream_declare(c
->epipe
, epipe
[0], exec_epipe
);
571 c
->process
.pid
= pid
;
572 c
->process
.cb
= rpc_file_exec_process_cb
;
573 uloop_process_add(&c
->process
);
575 c
->timeout
.cb
= rpc_file_exec_timeout_cb
;
576 uloop_timeout_set(&c
->timeout
, RPC_FILE_MAX_RUNTIME
);
582 ubus_defer_request(ctx
, req
, &c
->request
);
585 return UBUS_STATUS_OK
;
589 rpc_file_exec(struct ubus_context
*ctx
, struct ubus_object
*obj
,
590 struct ubus_request_data
*req
, const char *method
,
591 struct blob_attr
*msg
)
593 struct blob_attr
*tb
[__RPC_E_MAX
];
595 blobmsg_parse(rpc_exec_policy
, __RPC_E_MAX
, tb
,
596 blob_data(msg
), blob_len(msg
));
599 return UBUS_STATUS_INVALID_ARGUMENT
;
601 return rpc_file_exec_run(blobmsg_data(tb
[RPC_E_CMD
]),
602 tb
[RPC_E_PARM
], tb
[RPC_E_ENV
], ctx
, req
);
607 rpc_file_api_init(const struct rpc_daemon_ops
*o
, struct ubus_context
*ctx
)
609 static const struct ubus_method file_methods
[] = {
610 UBUS_METHOD("read", rpc_file_read
, rpc_file_r_policy
),
611 UBUS_METHOD("write", rpc_file_write
, rpc_file_rw_policy
),
612 UBUS_METHOD("list", rpc_file_list
, rpc_file_r_policy
),
613 UBUS_METHOD("stat", rpc_file_stat
, rpc_file_r_policy
),
614 UBUS_METHOD("exec", rpc_file_exec
, rpc_exec_policy
),
617 static struct ubus_object_type file_type
=
618 UBUS_OBJECT_TYPE("luci-rpc-file", file_methods
);
620 static struct ubus_object obj
= {
623 .methods
= file_methods
,
624 .n_methods
= ARRAY_SIZE(file_methods
),
627 return ubus_add_object(ctx
, &obj
);
630 struct rpc_plugin rpc_plugin
= {
631 .init
= rpc_file_api_init