2 * luci-rpcd - LuCI UBUS RPC server
4 * Copyright (C) 2013 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.
31 static struct blob_buf buf;
39 static const struct blobmsg_policy rpc_file_policy[__RPC_F_MAX] = {
40 [RPC_F_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
41 [RPC_F_DATA] = { .name = "data", .type = BLOBMSG_TYPE_STRING },
51 static const struct blobmsg_policy rpc_exec_policy[__RPC_E_MAX] = {
52 [RPC_E_CMD] = { .name = "command", .type = BLOBMSG_TYPE_STRING },
53 [RPC_E_PARM] = { .name = "params", .type = BLOBMSG_TYPE_ARRAY },
54 [RPC_E_ENV] = { .name = "env", .type = BLOBMSG_TYPE_TABLE },
57 static const char *d_types[] = {
60 [DT_DIR] = "directory",
65 [DT_UNKNOWN] = "unknown",
70 rpc_errno_status(void)
75 return UBUS_STATUS_PERMISSION_DENIED;
78 return UBUS_STATUS_INVALID_ARGUMENT;
81 return UBUS_STATUS_NOT_FOUND;
84 return UBUS_STATUS_INVALID_ARGUMENT;
87 return UBUS_STATUS_UNKNOWN_ERROR;
91 static struct blob_attr **
92 rpc_check_path(struct blob_attr *msg, char **path, struct stat *s)
94 static struct blob_attr *tb[__RPC_F_MAX];
96 blobmsg_parse(rpc_file_policy, __RPC_F_MAX, tb, blob_data(msg), blob_len(msg));
104 *path = blobmsg_data(tb[RPC_F_PATH]);
113 rpc_file_read(struct ubus_context *ctx, struct ubus_object *obj,
114 struct ubus_request_data *req, const char *method,
115 struct blob_attr *msg)
122 if (!rpc_check_path(msg, &path, &s))
123 return rpc_errno_status();
125 if (s.st_size >= RPC_FILE_MAX_SIZE)
126 return UBUS_STATUS_NOT_SUPPORTED;
128 if ((fd = open(path, O_RDONLY)) < 0)
129 return rpc_errno_status();
131 /* some sysfs files do not report a length */
133 s.st_size = RPC_FILE_MIN_SIZE;
135 blob_buf_init(&buf, 0);
137 wbuf = blobmsg_alloc_string_buffer(&buf, "data", s.st_size + 1);
141 rv = UBUS_STATUS_UNKNOWN_ERROR;
145 if ((len = read(fd, wbuf, s.st_size)) <= 0)
147 rv = UBUS_STATUS_NO_DATA;
152 blobmsg_add_string_buffer(&buf);
154 ubus_send_reply(ctx, req, buf.head);
163 rpc_file_write(struct ubus_context *ctx, struct ubus_object *obj,
164 struct ubus_request_data *req, const char *method,
165 struct blob_attr *msg)
170 struct blob_attr **tb;
172 if (!(tb = rpc_check_path(msg, &path, &s)))
173 return rpc_errno_status();
176 return UBUS_STATUS_INVALID_ARGUMENT;
178 if ((fd = open(path, O_WRONLY)) < 0)
179 return rpc_errno_status();
181 write(fd, blobmsg_data(tb[RPC_F_DATA]), blobmsg_data_len(tb[RPC_F_DATA]));
188 rpc_file_list(struct ubus_context *ctx, struct ubus_object *obj,
189 struct ubus_request_data *req, const char *method,
190 struct blob_attr *msg)
198 if (!rpc_check_path(msg, &path, &s))
199 return rpc_errno_status();
201 if ((fd = opendir(path)) == NULL)
202 return rpc_errno_status();
204 blob_buf_init(&buf, 0);
205 c = blobmsg_open_array(&buf, "entries");
207 while ((e = readdir(fd)) != NULL)
209 if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, ".."))
212 d = blobmsg_open_table(&buf, NULL);
213 blobmsg_add_string(&buf, "name", e->d_name);
214 blobmsg_add_string(&buf, "type", d_types[e->d_type]);
215 blobmsg_close_table(&buf, d);
218 blobmsg_close_array(&buf, c);
219 ubus_send_reply(ctx, req, buf.head);
225 rpc_file_stat(struct ubus_context *ctx, struct ubus_object *obj,
226 struct ubus_request_data *req, const char *method,
227 struct blob_attr *msg)
233 if (!rpc_check_path(msg, &path, &s))
234 return rpc_errno_status();
236 blob_buf_init(&buf, 0);
238 type = S_ISREG(s.st_mode) ? DT_REG :
239 S_ISDIR(s.st_mode) ? DT_DIR :
240 S_ISCHR(s.st_mode) ? DT_CHR :
241 S_ISBLK(s.st_mode) ? DT_BLK :
242 S_ISFIFO(s.st_mode) ? DT_FIFO :
243 S_ISLNK(s.st_mode) ? DT_LNK :
244 S_ISSOCK(s.st_mode) ? DT_SOCK :
247 blobmsg_add_string(&buf, "path", path);
248 blobmsg_add_string(&buf, "type", d_types[type]);
249 blobmsg_add_u32(&buf, "size", s.st_size);
250 blobmsg_add_u32(&buf, "mode", s.st_mode);
251 blobmsg_add_u32(&buf, "atime", s.st_atime);
252 blobmsg_add_u32(&buf, "mtime", s.st_mtime);
253 blobmsg_add_u32(&buf, "ctime", s.st_ctime);
254 blobmsg_add_u32(&buf, "inode", s.st_ino);
255 blobmsg_add_u32(&buf, "uid", s.st_uid);
256 blobmsg_add_u32(&buf, "gid", s.st_gid);
258 ubus_send_reply(ctx, req, buf.head);
264 rpc_file_exec_lookup(const char *cmd)
267 int plen = 0, clen = strlen(cmd) + 1;
269 static char path[PATH_MAX];
271 if (!stat(cmd, &s) && S_ISREG(s.st_mode))
274 search = getenv("PATH");
277 search = "/bin:/usr/bin:/sbin:/usr/sbin";
283 if (*p != ':' && *p != '\0')
288 if ((plen + clen) >= sizeof(path))
291 strncpy(path, search, plen);
292 sprintf(path + plen, "/%s", cmd);
294 if (!stat(path, &s) && S_ISREG(s.st_mode))
306 rpc_ustream_to_blobmsg(struct ustream *s, const char *name)
311 if ((len = ustream_pending_data(s, false)) > 0)
313 wbuf = blobmsg_alloc_string_buffer(&buf, name, len + 1);
318 ustream_for_each_read_buffer(s, rbuf, len)
320 memcpy(wbuf, rbuf, len);
325 blobmsg_add_string_buffer(&buf);
330 rpc_file_exec_reply(struct rpc_file_exec_context *c, int rv)
332 uloop_timeout_cancel(&c->timeout);
333 uloop_process_delete(&c->process);
335 if (rv == UBUS_STATUS_OK)
337 blob_buf_init(&buf, 0);
339 blobmsg_add_u32(&buf, "code", WEXITSTATUS(c->stat));
341 rpc_ustream_to_blobmsg(&c->opipe.stream, "stdout");
342 rpc_ustream_to_blobmsg(&c->epipe.stream, "stderr");
344 ubus_send_reply(c->context, &c->request, buf.head);
347 ubus_complete_deferred_request(c->context, &c->request, rv);
349 ustream_free(&c->opipe.stream);
350 ustream_free(&c->epipe.stream);
352 close(c->opipe.fd.fd);
353 close(c->epipe.fd.fd);
359 rpc_file_exec_timeout_cb(struct uloop_timeout *t)
361 struct rpc_file_exec_context *c =
362 container_of(t, struct rpc_file_exec_context, timeout);
364 kill(c->process.pid, SIGKILL);
365 rpc_file_exec_reply(c, UBUS_STATUS_TIMEOUT);
369 rpc_file_exec_process_cb(struct uloop_process *p, int stat)
371 struct rpc_file_exec_context *c =
372 container_of(p, struct rpc_file_exec_context, process);
376 ustream_poll(&c->opipe.stream);
377 ustream_poll(&c->epipe.stream);
381 rpc_file_exec_opipe_read_cb(struct ustream *s, int bytes)
383 struct rpc_file_exec_context *c =
384 container_of(s, struct rpc_file_exec_context, opipe.stream);
386 if (ustream_read_buf_full(s))
387 rpc_file_exec_reply(c, UBUS_STATUS_NOT_SUPPORTED);
391 rpc_file_exec_epipe_read_cb(struct ustream *s, int bytes)
393 struct rpc_file_exec_context *c =
394 container_of(s, struct rpc_file_exec_context, epipe.stream);
396 if (ustream_read_buf_full(s))
397 rpc_file_exec_reply(c, UBUS_STATUS_NOT_SUPPORTED);
401 rpc_file_exec_opipe_state_cb(struct ustream *s)
403 struct rpc_file_exec_context *c =
404 container_of(s, struct rpc_file_exec_context, opipe.stream);
406 if (c->opipe.stream.eof && c->epipe.stream.eof)
407 rpc_file_exec_reply(c, UBUS_STATUS_OK);
411 rpc_file_exec_epipe_state_cb(struct ustream *s)
413 struct rpc_file_exec_context *c =
414 container_of(s, struct rpc_file_exec_context, epipe.stream);
416 if (c->opipe.stream.eof && c->epipe.stream.eof)
417 rpc_file_exec_reply(c, UBUS_STATUS_OK);
421 rpc_file_exec_run(const char *cmd,
422 const struct blob_attr *arg, const struct blob_attr *env,
423 struct ubus_context *ctx, struct ubus_request_data *req)
431 struct blob_attr *cur;
436 struct rpc_file_exec_context *c;
438 cmd = rpc_file_exec_lookup(cmd);
441 return UBUS_STATUS_NOT_FOUND;
443 c = malloc(sizeof(*c));
446 return UBUS_STATUS_UNKNOWN_ERROR;
448 if (pipe(opipe) || pipe(epipe))
449 return rpc_errno_status();
451 switch ((pid = fork()))
454 return rpc_errno_status();
469 args = malloc(sizeof(char *) * arglen);
472 return UBUS_STATUS_UNKNOWN_ERROR;
474 args[0] = (char *)cmd;
479 blobmsg_for_each_attr(cur, arg, rem)
481 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
486 if (!(args = realloc(args, sizeof(char *) * arglen)))
487 return UBUS_STATUS_UNKNOWN_ERROR;
489 args[arglen-2] = blobmsg_data(cur);
490 args[arglen-1] = NULL;
496 blobmsg_for_each_attr(cur, env, rem)
498 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
501 setenv(blobmsg_name(cur), blobmsg_data(cur), 1);
505 if (execv(cmd, args))
506 return rpc_errno_status();
509 memset(c, 0, sizeof(*c));
511 ustream_declare(c->opipe, opipe[0], exec_opipe);
512 ustream_declare(c->epipe, epipe[0], exec_epipe);
514 c->process.pid = pid;
515 c->process.cb = rpc_file_exec_process_cb;
516 uloop_process_add(&c->process);
518 c->timeout.cb = rpc_file_exec_timeout_cb;
519 uloop_timeout_set(&c->timeout, RPC_FILE_MAX_RUNTIME);
525 ubus_defer_request(ctx, req, &c->request);
528 return UBUS_STATUS_OK;
532 rpc_file_exec(struct ubus_context *ctx, struct ubus_object *obj,
533 struct ubus_request_data *req, const char *method,
534 struct blob_attr *msg)
536 struct blob_attr *tb[__RPC_E_MAX];
538 blobmsg_parse(rpc_exec_policy, __RPC_E_MAX, tb,
539 blob_data(msg), blob_len(msg));
542 return UBUS_STATUS_INVALID_ARGUMENT;
544 return rpc_file_exec_run(blobmsg_data(tb[RPC_E_CMD]),
545 tb[RPC_E_PARM], tb[RPC_E_ENV], ctx, req);
549 int rpc_file_api_init(struct ubus_context *ctx)
551 static const struct ubus_method file_methods[] = {
552 UBUS_METHOD("read", rpc_file_read, rpc_file_policy),
553 UBUS_METHOD("write", rpc_file_write, rpc_file_policy),
554 UBUS_METHOD("list", rpc_file_list, rpc_file_policy),
555 UBUS_METHOD("stat", rpc_file_stat, rpc_file_policy),
556 UBUS_METHOD("exec", rpc_file_exec, rpc_exec_policy),
559 static struct ubus_object_type file_type =
560 UBUS_OBJECT_TYPE("luci-rpc-file", file_methods);
562 static struct ubus_object obj = {
565 .methods = file_methods,
566 .n_methods = ARRAY_SIZE(file_methods),
569 return ubus_add_object(ctx, &obj);