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
;
77 static const struct blobmsg_policy rpc_file_policy
[__RPC_F_MAX
] = {
78 [RPC_F_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
79 [RPC_F_DATA
] = { .name
= "data", .type
= BLOBMSG_TYPE_STRING
},
89 static const struct blobmsg_policy rpc_exec_policy
[__RPC_E_MAX
] = {
90 [RPC_E_CMD
] = { .name
= "command", .type
= BLOBMSG_TYPE_STRING
},
91 [RPC_E_PARM
] = { .name
= "params", .type
= BLOBMSG_TYPE_ARRAY
},
92 [RPC_E_ENV
] = { .name
= "env", .type
= BLOBMSG_TYPE_TABLE
},
95 static const char *d_types
[] = {
98 [DT_DIR
] = "directory",
100 [DT_LNK
] = "symlink",
102 [DT_SOCK
] = "socket",
103 [DT_UNKNOWN
] = "unknown",
108 rpc_errno_status(void)
113 return UBUS_STATUS_PERMISSION_DENIED
;
116 return UBUS_STATUS_INVALID_ARGUMENT
;
119 return UBUS_STATUS_NOT_FOUND
;
122 return UBUS_STATUS_INVALID_ARGUMENT
;
125 return UBUS_STATUS_UNKNOWN_ERROR
;
129 static struct blob_attr
**
130 rpc_check_path(struct blob_attr
*msg
, char **path
, struct stat
*s
)
132 static struct blob_attr
*tb
[__RPC_F_MAX
];
134 blobmsg_parse(rpc_file_policy
, __RPC_F_MAX
, tb
, blob_data(msg
), blob_len(msg
));
142 *path
= blobmsg_data(tb
[RPC_F_PATH
]);
151 rpc_file_read(struct ubus_context
*ctx
, struct ubus_object
*obj
,
152 struct ubus_request_data
*req
, const char *method
,
153 struct blob_attr
*msg
)
160 if (!rpc_check_path(msg
, &path
, &s
))
161 return rpc_errno_status();
163 if (s
.st_size
>= RPC_FILE_MAX_SIZE
)
164 return UBUS_STATUS_NOT_SUPPORTED
;
166 if ((fd
= open(path
, O_RDONLY
)) < 0)
167 return rpc_errno_status();
169 /* some sysfs files do not report a length */
171 s
.st_size
= RPC_FILE_MIN_SIZE
;
173 blob_buf_init(&buf
, 0);
175 wbuf
= blobmsg_alloc_string_buffer(&buf
, "data", s
.st_size
+ 1);
179 rv
= UBUS_STATUS_UNKNOWN_ERROR
;
183 if ((len
= read(fd
, wbuf
, s
.st_size
)) <= 0)
185 rv
= UBUS_STATUS_NO_DATA
;
190 blobmsg_add_string_buffer(&buf
);
192 ubus_send_reply(ctx
, req
, buf
.head
);
201 rpc_file_write(struct ubus_context
*ctx
, struct ubus_object
*obj
,
202 struct ubus_request_data
*req
, const char *method
,
203 struct blob_attr
*msg
)
208 struct blob_attr
**tb
;
210 if (!(tb
= rpc_check_path(msg
, &path
, &s
)))
211 return rpc_errno_status();
214 return UBUS_STATUS_INVALID_ARGUMENT
;
216 if ((fd
= open(path
, O_WRONLY
)) < 0)
217 return rpc_errno_status();
219 write(fd
, blobmsg_data(tb
[RPC_F_DATA
]), blobmsg_data_len(tb
[RPC_F_DATA
]));
226 rpc_file_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
227 struct ubus_request_data
*req
, const char *method
,
228 struct blob_attr
*msg
)
236 if (!rpc_check_path(msg
, &path
, &s
))
237 return rpc_errno_status();
239 if ((fd
= opendir(path
)) == NULL
)
240 return rpc_errno_status();
242 blob_buf_init(&buf
, 0);
243 c
= blobmsg_open_array(&buf
, "entries");
245 while ((e
= readdir(fd
)) != NULL
)
247 if (!strcmp(e
->d_name
, ".") || !strcmp(e
->d_name
, ".."))
250 d
= blobmsg_open_table(&buf
, NULL
);
251 blobmsg_add_string(&buf
, "name", e
->d_name
);
252 blobmsg_add_string(&buf
, "type", d_types
[e
->d_type
]);
253 blobmsg_close_table(&buf
, d
);
256 blobmsg_close_array(&buf
, c
);
257 ubus_send_reply(ctx
, req
, buf
.head
);
263 rpc_file_stat(struct ubus_context
*ctx
, struct ubus_object
*obj
,
264 struct ubus_request_data
*req
, const char *method
,
265 struct blob_attr
*msg
)
271 if (!rpc_check_path(msg
, &path
, &s
))
272 return rpc_errno_status();
274 blob_buf_init(&buf
, 0);
276 type
= S_ISREG(s
.st_mode
) ? DT_REG
:
277 S_ISDIR(s
.st_mode
) ? DT_DIR
:
278 S_ISCHR(s
.st_mode
) ? DT_CHR
:
279 S_ISBLK(s
.st_mode
) ? DT_BLK
:
280 S_ISFIFO(s
.st_mode
) ? DT_FIFO
:
281 S_ISLNK(s
.st_mode
) ? DT_LNK
:
282 S_ISSOCK(s
.st_mode
) ? DT_SOCK
:
285 blobmsg_add_string(&buf
, "path", path
);
286 blobmsg_add_string(&buf
, "type", d_types
[type
]);
287 blobmsg_add_u32(&buf
, "size", s
.st_size
);
288 blobmsg_add_u32(&buf
, "mode", s
.st_mode
);
289 blobmsg_add_u32(&buf
, "atime", s
.st_atime
);
290 blobmsg_add_u32(&buf
, "mtime", s
.st_mtime
);
291 blobmsg_add_u32(&buf
, "ctime", s
.st_ctime
);
292 blobmsg_add_u32(&buf
, "inode", s
.st_ino
);
293 blobmsg_add_u32(&buf
, "uid", s
.st_uid
);
294 blobmsg_add_u32(&buf
, "gid", s
.st_gid
);
296 ubus_send_reply(ctx
, req
, buf
.head
);
302 rpc_file_exec_lookup(const char *cmd
)
305 int plen
= 0, clen
= strlen(cmd
) + 1;
307 static char path
[PATH_MAX
];
309 if (!stat(cmd
, &s
) && S_ISREG(s
.st_mode
))
312 search
= getenv("PATH");
315 search
= "/bin:/usr/bin:/sbin:/usr/sbin";
321 if (*p
!= ':' && *p
!= '\0')
326 if ((plen
+ clen
) >= sizeof(path
))
329 strncpy(path
, search
, plen
);
330 sprintf(path
+ plen
, "/%s", cmd
);
332 if (!stat(path
, &s
) && S_ISREG(s
.st_mode
))
344 rpc_ustream_to_blobmsg(struct ustream
*s
, const char *name
)
349 if ((len
= ustream_pending_data(s
, false)) > 0)
351 wbuf
= blobmsg_alloc_string_buffer(&buf
, name
, len
+ 1);
356 ustream_for_each_read_buffer(s
, rbuf
, len
)
358 memcpy(wbuf
, rbuf
, len
);
363 blobmsg_add_string_buffer(&buf
);
368 rpc_file_exec_reply(struct rpc_file_exec_context
*c
, int rv
)
370 uloop_timeout_cancel(&c
->timeout
);
371 uloop_process_delete(&c
->process
);
373 if (rv
== UBUS_STATUS_OK
)
375 blob_buf_init(&buf
, 0);
377 blobmsg_add_u32(&buf
, "code", WEXITSTATUS(c
->stat
));
379 rpc_ustream_to_blobmsg(&c
->opipe
.stream
, "stdout");
380 rpc_ustream_to_blobmsg(&c
->epipe
.stream
, "stderr");
382 ubus_send_reply(c
->context
, &c
->request
, buf
.head
);
385 ubus_complete_deferred_request(c
->context
, &c
->request
, rv
);
387 ustream_free(&c
->opipe
.stream
);
388 ustream_free(&c
->epipe
.stream
);
390 close(c
->opipe
.fd
.fd
);
391 close(c
->epipe
.fd
.fd
);
397 rpc_file_exec_timeout_cb(struct uloop_timeout
*t
)
399 struct rpc_file_exec_context
*c
=
400 container_of(t
, struct rpc_file_exec_context
, timeout
);
402 kill(c
->process
.pid
, SIGKILL
);
403 rpc_file_exec_reply(c
, UBUS_STATUS_TIMEOUT
);
407 rpc_file_exec_process_cb(struct uloop_process
*p
, int stat
)
409 struct rpc_file_exec_context
*c
=
410 container_of(p
, struct rpc_file_exec_context
, process
);
414 ustream_poll(&c
->opipe
.stream
);
415 ustream_poll(&c
->epipe
.stream
);
419 rpc_file_exec_opipe_read_cb(struct ustream
*s
, int bytes
)
421 struct rpc_file_exec_context
*c
=
422 container_of(s
, struct rpc_file_exec_context
, opipe
.stream
);
424 if (ustream_read_buf_full(s
))
425 rpc_file_exec_reply(c
, UBUS_STATUS_NOT_SUPPORTED
);
429 rpc_file_exec_epipe_read_cb(struct ustream
*s
, int bytes
)
431 struct rpc_file_exec_context
*c
=
432 container_of(s
, struct rpc_file_exec_context
, epipe
.stream
);
434 if (ustream_read_buf_full(s
))
435 rpc_file_exec_reply(c
, UBUS_STATUS_NOT_SUPPORTED
);
439 rpc_file_exec_opipe_state_cb(struct ustream
*s
)
441 struct rpc_file_exec_context
*c
=
442 container_of(s
, struct rpc_file_exec_context
, opipe
.stream
);
444 if (c
->opipe
.stream
.eof
&& c
->epipe
.stream
.eof
)
445 rpc_file_exec_reply(c
, UBUS_STATUS_OK
);
449 rpc_file_exec_epipe_state_cb(struct ustream
*s
)
451 struct rpc_file_exec_context
*c
=
452 container_of(s
, struct rpc_file_exec_context
, epipe
.stream
);
454 if (c
->opipe
.stream
.eof
&& c
->epipe
.stream
.eof
)
455 rpc_file_exec_reply(c
, UBUS_STATUS_OK
);
459 rpc_file_exec_run(const char *cmd
,
460 const struct blob_attr
*arg
, const struct blob_attr
*env
,
461 struct ubus_context
*ctx
, struct ubus_request_data
*req
)
469 struct blob_attr
*cur
;
474 struct rpc_file_exec_context
*c
;
476 cmd
= rpc_file_exec_lookup(cmd
);
479 return UBUS_STATUS_NOT_FOUND
;
481 c
= malloc(sizeof(*c
));
484 return UBUS_STATUS_UNKNOWN_ERROR
;
486 if (pipe(opipe
) || pipe(epipe
))
487 return rpc_errno_status();
489 switch ((pid
= fork()))
492 return rpc_errno_status();
507 args
= malloc(sizeof(char *) * arglen
);
510 return UBUS_STATUS_UNKNOWN_ERROR
;
512 args
[0] = (char *)cmd
;
517 blobmsg_for_each_attr(cur
, arg
, rem
)
519 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
524 if (!(args
= realloc(args
, sizeof(char *) * arglen
)))
525 return UBUS_STATUS_UNKNOWN_ERROR
;
527 args
[arglen
-2] = blobmsg_data(cur
);
528 args
[arglen
-1] = NULL
;
534 blobmsg_for_each_attr(cur
, env
, rem
)
536 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
539 setenv(blobmsg_name(cur
), blobmsg_data(cur
), 1);
543 if (execv(cmd
, args
))
544 return rpc_errno_status();
547 memset(c
, 0, sizeof(*c
));
549 ustream_declare(c
->opipe
, opipe
[0], exec_opipe
);
550 ustream_declare(c
->epipe
, epipe
[0], exec_epipe
);
552 c
->process
.pid
= pid
;
553 c
->process
.cb
= rpc_file_exec_process_cb
;
554 uloop_process_add(&c
->process
);
556 c
->timeout
.cb
= rpc_file_exec_timeout_cb
;
557 uloop_timeout_set(&c
->timeout
, RPC_FILE_MAX_RUNTIME
);
563 ubus_defer_request(ctx
, req
, &c
->request
);
566 return UBUS_STATUS_OK
;
570 rpc_file_exec(struct ubus_context
*ctx
, struct ubus_object
*obj
,
571 struct ubus_request_data
*req
, const char *method
,
572 struct blob_attr
*msg
)
574 struct blob_attr
*tb
[__RPC_E_MAX
];
576 blobmsg_parse(rpc_exec_policy
, __RPC_E_MAX
, tb
,
577 blob_data(msg
), blob_len(msg
));
580 return UBUS_STATUS_INVALID_ARGUMENT
;
582 return rpc_file_exec_run(blobmsg_data(tb
[RPC_E_CMD
]),
583 tb
[RPC_E_PARM
], tb
[RPC_E_ENV
], ctx
, req
);
588 rpc_file_api_init(const struct rpc_daemon_ops
*o
, struct ubus_context
*ctx
)
590 static const struct ubus_method file_methods
[] = {
591 UBUS_METHOD("read", rpc_file_read
, rpc_file_policy
),
592 UBUS_METHOD("write", rpc_file_write
, rpc_file_policy
),
593 UBUS_METHOD("list", rpc_file_list
, rpc_file_policy
),
594 UBUS_METHOD("stat", rpc_file_stat
, rpc_file_policy
),
595 UBUS_METHOD("exec", rpc_file_exec
, rpc_exec_policy
),
598 static struct ubus_object_type file_type
=
599 UBUS_OBJECT_TYPE("luci-rpc-file", file_methods
);
601 static struct ubus_object obj
= {
604 .methods
= file_methods
,
605 .n_methods
= ARRAY_SIZE(file_methods
),
608 return ubus_add_object(ctx
, &obj
);
611 const struct rpc_plugin rpc_plugin
= {
612 .init
= rpc_file_api_init