file: add stat() information to directory listings
[project/rpcd.git] / file.c
1 /*
2 * rpcd - UBUS RPC server
3 *
4 * Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
5 * Copyright (C) 2016 Luka Perkov <luka@openwrt.org>
6 *
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.
10 *
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.
18 */
19
20 #define _GNU_SOURCE
21
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <limits.h>
29 #include <dirent.h>
30 #include <sys/stat.h>
31 #include <sys/wait.h>
32 #include <libubus.h>
33 #include <libubox/blobmsg.h>
34 #include <libubox/md5.h>
35 #include <libubox/ustream.h>
36 #include <libubox/utils.h>
37
38 #include <rpcd/plugin.h>
39
40 /* limit of sys & proc files */
41 #define RPC_FILE_MIN_SIZE (128)
42
43 /* limit of regular files and command output data */
44 #define RPC_FILE_MAX_SIZE (4096 * 64)
45
46 #define ustream_for_each_read_buffer(stream, ptr, len) \
47 for (ptr = ustream_get_read_buf(stream, &len); \
48 ptr != NULL && len > 0; \
49 ustream_consume(stream, len), ptr = ustream_get_read_buf(stream, &len))
50
51 #define ustream_declare(us, fd, name) \
52 us.stream.string_data = true; \
53 us.stream.r.buffer_len = 4096; \
54 us.stream.r.max_buffers = RPC_FILE_MAX_SIZE / 4096; \
55 us.stream.notify_read = rpc_file_##name##_read_cb; \
56 us.stream.notify_state = rpc_file_##name##_state_cb; \
57 ustream_fd_init(&us, fd);
58
59 static const struct rpc_daemon_ops *ops;
60
61 struct rpc_file_exec_context {
62 struct ubus_context *context;
63 struct ubus_request_data request;
64 struct uloop_timeout timeout;
65 struct uloop_process process;
66 struct ustream_fd opipe;
67 struct ustream_fd epipe;
68 int stat;
69 };
70
71
72 static struct blob_buf buf;
73
74 enum {
75 RPC_F_R_PATH,
76 __RPC_F_R_MAX,
77 };
78
79 static const struct blobmsg_policy rpc_file_r_policy[__RPC_F_R_MAX] = {
80 [RPC_F_R_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
81 };
82
83 enum {
84 RPC_F_RB_PATH,
85 RPC_F_RB_BASE64,
86 __RPC_F_RB_MAX,
87 };
88
89 static const struct blobmsg_policy rpc_file_rb_policy[__RPC_F_RB_MAX] = {
90 [RPC_F_RB_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
91 [RPC_F_RB_BASE64] = { .name = "base64", .type = BLOBMSG_TYPE_BOOL },
92 };
93
94 enum {
95 RPC_F_RW_PATH,
96 RPC_F_RW_DATA,
97 RPC_F_RW_APPEND,
98 RPC_F_RW_MODE,
99 RPC_F_RW_BASE64,
100 __RPC_F_RW_MAX,
101 };
102
103 static const struct blobmsg_policy rpc_file_rw_policy[__RPC_F_RW_MAX] = {
104 [RPC_F_RW_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
105 [RPC_F_RW_DATA] = { .name = "data", .type = BLOBMSG_TYPE_STRING },
106 [RPC_F_RW_APPEND] = { .name = "append", .type = BLOBMSG_TYPE_BOOL },
107 [RPC_F_RW_MODE] = { .name = "mode", .type = BLOBMSG_TYPE_INT32 },
108 [RPC_F_RW_BASE64] = { .name = "base64", .type = BLOBMSG_TYPE_BOOL },
109 };
110
111 enum {
112 RPC_E_CMD,
113 RPC_E_PARM,
114 RPC_E_ENV,
115 __RPC_E_MAX,
116 };
117
118 static const struct blobmsg_policy rpc_exec_policy[__RPC_E_MAX] = {
119 [RPC_E_CMD] = { .name = "command", .type = BLOBMSG_TYPE_STRING },
120 [RPC_E_PARM] = { .name = "params", .type = BLOBMSG_TYPE_ARRAY },
121 [RPC_E_ENV] = { .name = "env", .type = BLOBMSG_TYPE_TABLE },
122 };
123
124 static const char *d_types[] = {
125 [DT_BLK] = "block",
126 [DT_CHR] = "char",
127 [DT_DIR] = "directory",
128 [DT_FIFO] = "fifo",
129 [DT_LNK] = "symlink",
130 [DT_REG] = "file",
131 [DT_SOCK] = "socket",
132 [DT_UNKNOWN] = "unknown",
133 };
134
135
136 static int
137 rpc_errno_status(void)
138 {
139 switch (errno)
140 {
141 case EACCES:
142 return UBUS_STATUS_PERMISSION_DENIED;
143
144 case ENOTDIR:
145 return UBUS_STATUS_INVALID_ARGUMENT;
146
147 case ENOENT:
148 return UBUS_STATUS_NOT_FOUND;
149
150 case EINVAL:
151 return UBUS_STATUS_INVALID_ARGUMENT;
152
153 default:
154 return UBUS_STATUS_UNKNOWN_ERROR;
155 }
156 }
157
158 static struct blob_attr **
159 rpc_check_path(struct blob_attr *msg, char **path, struct stat *s)
160 {
161 static struct blob_attr *tb[__RPC_F_R_MAX];
162
163 blobmsg_parse(rpc_file_r_policy, __RPC_F_R_MAX, tb, blob_data(msg), blob_len(msg));
164
165 if (!tb[RPC_F_R_PATH])
166 {
167 errno = EINVAL;
168 return NULL;
169 }
170
171 *path = blobmsg_data(tb[RPC_F_R_PATH]);
172
173 if (stat(*path, s))
174 return NULL;
175
176 return tb;
177 }
178
179 static int
180 rpc_file_read(struct ubus_context *ctx, struct ubus_object *obj,
181 struct ubus_request_data *req, const char *method,
182 struct blob_attr *msg)
183 {
184 static struct blob_attr *tb[__RPC_F_RB_MAX];
185 bool base64 = false;
186 int fd, rv;
187 ssize_t len;
188 char *path;
189 struct stat s;
190 char *wbuf;
191
192 blobmsg_parse(rpc_file_rb_policy, __RPC_F_RB_MAX, tb, blob_data(msg), blob_len(msg));
193
194 if (!tb[RPC_F_RB_PATH])
195 return rpc_errno_status();
196
197 path = blobmsg_data(tb[RPC_F_RB_PATH]);
198
199 if (stat(path, &s))
200 return rpc_errno_status();
201
202 if (s.st_size >= RPC_FILE_MAX_SIZE)
203 return UBUS_STATUS_NOT_SUPPORTED;
204
205 if ((fd = open(path, O_RDONLY)) < 0)
206 return rpc_errno_status();
207
208 /* some sysfs files do not report a length */
209 if (s.st_size == 0)
210 s.st_size = RPC_FILE_MIN_SIZE;
211
212 blob_buf_init(&buf, 0);
213
214 if (tb[RPC_F_RB_BASE64])
215 base64 = blobmsg_get_bool(tb[RPC_F_RB_BASE64]);
216
217 len = s.st_size + 1;
218 if (base64)
219 len = B64_ENCODE_LEN(s.st_size);
220 wbuf = blobmsg_alloc_string_buffer(&buf, "data", len);
221
222 if (!wbuf)
223 {
224 rv = UBUS_STATUS_UNKNOWN_ERROR;
225 goto out;
226 }
227
228 if ((len = read(fd, wbuf, s.st_size)) <= 0)
229 {
230 rv = UBUS_STATUS_NO_DATA;
231 goto out;
232 }
233
234 if (base64)
235 {
236 uint8_t *data = calloc(len, sizeof(uint8_t));
237 if (!data)
238 {
239 rv = UBUS_STATUS_UNKNOWN_ERROR;
240 goto out;
241 }
242 memcpy(data, wbuf, len);
243
244 len = b64_encode(data, len, wbuf, B64_ENCODE_LEN(len));
245 free(data);
246 if (len < 0)
247 {
248 rv = UBUS_STATUS_UNKNOWN_ERROR;
249 goto out;
250 }
251 }
252
253 *(wbuf + len) = '\0';
254 blobmsg_add_string_buffer(&buf);
255
256 ubus_send_reply(ctx, req, buf.head);
257 rv = UBUS_STATUS_OK;
258
259 out:
260 blob_buf_free(&buf);
261 close(fd);
262 return rv;
263 }
264
265 static int
266 rpc_file_write(struct ubus_context *ctx, struct ubus_object *obj,
267 struct ubus_request_data *req, const char *method,
268 struct blob_attr *msg)
269 {
270 struct blob_attr *tb[__RPC_F_RW_MAX];
271 int append = O_TRUNC;
272 mode_t prev_mode, mode = 0666;
273 int fd, rv = 0;
274 void *data = NULL;
275 ssize_t data_len = 0;
276
277 blobmsg_parse(rpc_file_rw_policy, __RPC_F_RW_MAX, tb,
278 blob_data(msg), blob_len(msg));
279
280 if (!tb[RPC_F_RW_PATH] || !tb[RPC_F_RW_DATA])
281 return UBUS_STATUS_INVALID_ARGUMENT;
282
283 data = blobmsg_data(tb[RPC_F_RW_DATA]);
284 data_len = blobmsg_data_len(tb[RPC_F_RW_DATA]) - 1;
285
286 if (tb[RPC_F_RW_APPEND] && blobmsg_get_bool(tb[RPC_F_RW_APPEND]))
287 append = O_APPEND;
288
289 if (tb[RPC_F_RW_MODE])
290 mode = blobmsg_get_u32(tb[RPC_F_RW_MODE]);
291
292 prev_mode = umask(0);
293 fd = open(blobmsg_data(tb[RPC_F_RW_PATH]), O_CREAT | O_WRONLY | append, mode);
294 umask(prev_mode);
295 if (fd < 0)
296 return rpc_errno_status();
297
298 if (tb[RPC_F_RW_BASE64] && blobmsg_get_bool(tb[RPC_F_RW_BASE64]))
299 {
300 data_len = b64_decode(data, data, data_len);
301 if (data_len < 0)
302 {
303 rv = UBUS_STATUS_UNKNOWN_ERROR;
304 goto out;
305 }
306 }
307
308 if (write(fd, data, data_len) < 0)
309 rv = -1;
310
311 out:
312 if (fsync(fd) < 0)
313 rv = -1;
314
315 close(fd);
316 sync();
317
318 if (rv)
319 return rpc_errno_status();
320
321 return 0;
322 }
323
324 static int
325 rpc_file_md5(struct ubus_context *ctx, struct ubus_object *obj,
326 struct ubus_request_data *req, const char *method,
327 struct blob_attr *msg)
328 {
329 int rv, i;
330 char *path;
331 struct stat s;
332 uint8_t md5[16];
333 char *wbuf;
334
335 if (!rpc_check_path(msg, &path, &s))
336 return rpc_errno_status();
337
338 if (!S_ISREG(s.st_mode))
339 return UBUS_STATUS_NOT_SUPPORTED;
340
341 if ((rv = md5sum(path, md5)) <= 0)
342 return rpc_errno_status();
343
344 blob_buf_init(&buf, 0);
345 wbuf = blobmsg_alloc_string_buffer(&buf, "md5", 33);
346
347 for (i = 0; i < 16; i++)
348 sprintf(wbuf + (i * 2), "%02x", (uint8_t) md5[i]);
349
350 blobmsg_add_string_buffer(&buf);
351 ubus_send_reply(ctx, req, buf.head);
352 blob_buf_free(&buf);
353
354 return UBUS_STATUS_OK;
355 }
356
357 static void
358 _rpc_file_add_stat(struct stat *s)
359 {
360 int type;
361
362 type = S_ISREG(s->st_mode) ? DT_REG :
363 S_ISDIR(s->st_mode) ? DT_DIR :
364 S_ISCHR(s->st_mode) ? DT_CHR :
365 S_ISBLK(s->st_mode) ? DT_BLK :
366 S_ISFIFO(s->st_mode) ? DT_FIFO :
367 S_ISLNK(s->st_mode) ? DT_LNK :
368 S_ISSOCK(s->st_mode) ? DT_SOCK :
369 DT_UNKNOWN;
370
371 blobmsg_add_string(&buf, "type", d_types[type]);
372 blobmsg_add_u32(&buf, "size", s->st_size);
373 blobmsg_add_u32(&buf, "mode", s->st_mode);
374 blobmsg_add_u32(&buf, "atime", s->st_atime);
375 blobmsg_add_u32(&buf, "mtime", s->st_mtime);
376 blobmsg_add_u32(&buf, "ctime", s->st_ctime);
377 blobmsg_add_u32(&buf, "inode", s->st_ino);
378 blobmsg_add_u32(&buf, "uid", s->st_uid);
379 blobmsg_add_u32(&buf, "gid", s->st_gid);
380 }
381
382 static int
383 rpc_file_list(struct ubus_context *ctx, struct ubus_object *obj,
384 struct ubus_request_data *req, const char *method,
385 struct blob_attr *msg)
386 {
387 DIR *fd;
388 void *c, *d;
389 struct stat s;
390 struct dirent *e;
391 char *path, *entrypath;
392
393 if (!rpc_check_path(msg, &path, &s))
394 return rpc_errno_status();
395
396 if ((fd = opendir(path)) == NULL)
397 return rpc_errno_status();
398
399 blob_buf_init(&buf, 0);
400 c = blobmsg_open_array(&buf, "entries");
401
402 while ((e = readdir(fd)) != NULL)
403 {
404 if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, ".."))
405 continue;
406
407 if (asprintf(&entrypath, "%s/%s", path, e->d_name) < 0)
408 continue;
409
410 if (!stat(entrypath, &s))
411 {
412 d = blobmsg_open_table(&buf, NULL);
413 blobmsg_add_string(&buf, "name", e->d_name);
414 _rpc_file_add_stat(&s);
415 blobmsg_close_table(&buf, d);
416 }
417
418 free(entrypath);
419 }
420
421 closedir(fd);
422
423 blobmsg_close_array(&buf, c);
424 ubus_send_reply(ctx, req, buf.head);
425 blob_buf_free(&buf);
426
427 return 0;
428 }
429
430 static int
431 rpc_file_stat(struct ubus_context *ctx, struct ubus_object *obj,
432 struct ubus_request_data *req, const char *method,
433 struct blob_attr *msg)
434 {
435 char *path;
436 struct stat s;
437
438 if (!rpc_check_path(msg, &path, &s))
439 return rpc_errno_status();
440
441 blob_buf_init(&buf, 0);
442
443 blobmsg_add_string(&buf, "path", path);
444 _rpc_file_add_stat(&s);
445
446 ubus_send_reply(ctx, req, buf.head);
447 blob_buf_free(&buf);
448
449 return 0;
450 }
451
452 static const char *
453 rpc_file_exec_lookup(const char *cmd)
454 {
455 struct stat s;
456 int plen = 0, clen = strlen(cmd) + 1;
457 char *search, *p;
458 static char path[PATH_MAX];
459
460 if (!stat(cmd, &s) && S_ISREG(s.st_mode))
461 return cmd;
462
463 search = getenv("PATH");
464
465 if (!search)
466 search = "/bin:/usr/bin:/sbin:/usr/sbin";
467
468 p = search;
469
470 do
471 {
472 if (*p != ':' && *p != '\0')
473 continue;
474
475 plen = p - search;
476
477 if ((plen + clen) >= sizeof(path))
478 continue;
479
480 strncpy(path, search, plen);
481 sprintf(path + plen, "/%s", cmd);
482
483 if (!stat(path, &s) && S_ISREG(s.st_mode))
484 return path;
485
486 search = p + 1;
487 }
488 while (*p++);
489
490 return NULL;
491 }
492
493
494 static void
495 rpc_ustream_to_blobmsg(struct ustream *s, const char *name)
496 {
497 int len;
498 char *rbuf, *wbuf;
499
500 if ((len = ustream_pending_data(s, false)) > 0)
501 {
502 wbuf = blobmsg_alloc_string_buffer(&buf, name, len + 1);
503
504 if (!wbuf)
505 return;
506
507 ustream_for_each_read_buffer(s, rbuf, len)
508 {
509 memcpy(wbuf, rbuf, len);
510 wbuf += len;
511 }
512
513 *wbuf = 0;
514 blobmsg_add_string_buffer(&buf);
515 }
516 }
517
518 static void
519 rpc_file_exec_reply(struct rpc_file_exec_context *c, int rv)
520 {
521 uloop_timeout_cancel(&c->timeout);
522 uloop_process_delete(&c->process);
523
524 if (rv == UBUS_STATUS_OK)
525 {
526 blob_buf_init(&buf, 0);
527
528 blobmsg_add_u32(&buf, "code", WEXITSTATUS(c->stat));
529
530 rpc_ustream_to_blobmsg(&c->opipe.stream, "stdout");
531 rpc_ustream_to_blobmsg(&c->epipe.stream, "stderr");
532
533 ubus_send_reply(c->context, &c->request, buf.head);
534 blob_buf_free(&buf);
535 }
536
537 ubus_complete_deferred_request(c->context, &c->request, rv);
538
539 ustream_free(&c->opipe.stream);
540 ustream_free(&c->epipe.stream);
541
542 close(c->opipe.fd.fd);
543 close(c->epipe.fd.fd);
544
545 free(c);
546 }
547
548 static void
549 rpc_file_exec_timeout_cb(struct uloop_timeout *t)
550 {
551 struct rpc_file_exec_context *c =
552 container_of(t, struct rpc_file_exec_context, timeout);
553
554 kill(c->process.pid, SIGKILL);
555 rpc_file_exec_reply(c, UBUS_STATUS_TIMEOUT);
556 }
557
558 static void
559 rpc_file_exec_process_cb(struct uloop_process *p, int stat)
560 {
561 struct rpc_file_exec_context *c =
562 container_of(p, struct rpc_file_exec_context, process);
563
564 c->stat = stat;
565
566 ustream_poll(&c->opipe.stream);
567 ustream_poll(&c->epipe.stream);
568 }
569
570 static void
571 rpc_file_exec_opipe_read_cb(struct ustream *s, int bytes)
572 {
573 struct rpc_file_exec_context *c =
574 container_of(s, struct rpc_file_exec_context, opipe.stream);
575
576 if (ustream_read_buf_full(s))
577 rpc_file_exec_reply(c, UBUS_STATUS_NOT_SUPPORTED);
578 }
579
580 static void
581 rpc_file_exec_epipe_read_cb(struct ustream *s, int bytes)
582 {
583 struct rpc_file_exec_context *c =
584 container_of(s, struct rpc_file_exec_context, epipe.stream);
585
586 if (ustream_read_buf_full(s))
587 rpc_file_exec_reply(c, UBUS_STATUS_NOT_SUPPORTED);
588 }
589
590 static void
591 rpc_file_exec_opipe_state_cb(struct ustream *s)
592 {
593 struct rpc_file_exec_context *c =
594 container_of(s, struct rpc_file_exec_context, opipe.stream);
595
596 if (c->opipe.stream.eof && c->epipe.stream.eof)
597 rpc_file_exec_reply(c, UBUS_STATUS_OK);
598 }
599
600 static void
601 rpc_file_exec_epipe_state_cb(struct ustream *s)
602 {
603 struct rpc_file_exec_context *c =
604 container_of(s, struct rpc_file_exec_context, epipe.stream);
605
606 if (c->opipe.stream.eof && c->epipe.stream.eof)
607 rpc_file_exec_reply(c, UBUS_STATUS_OK);
608 }
609
610 static void
611 rpc_fdclose(int fd)
612 {
613 if (fd > 2)
614 close(fd);
615 }
616
617 static int
618 rpc_file_exec_run(const char *cmd,
619 const struct blob_attr *arg, const struct blob_attr *env,
620 struct ubus_context *ctx, struct ubus_request_data *req)
621 {
622 pid_t pid;
623
624 int devnull;
625 int opipe[2];
626 int epipe[2];
627
628 int rem;
629 struct blob_attr *cur;
630
631 uint8_t arglen;
632 char **args, **tmp;
633
634 struct rpc_file_exec_context *c;
635
636 cmd = rpc_file_exec_lookup(cmd);
637
638 if (!cmd)
639 return UBUS_STATUS_NOT_FOUND;
640
641 c = malloc(sizeof(*c));
642
643 if (!c)
644 return UBUS_STATUS_UNKNOWN_ERROR;
645
646 if (pipe(opipe) || pipe(epipe))
647 return rpc_errno_status();
648
649 switch ((pid = fork()))
650 {
651 case -1:
652 return rpc_errno_status();
653
654 case 0:
655 uloop_done();
656
657 devnull = open("/dev/null", O_RDWR);
658
659 if (devnull == -1)
660 return UBUS_STATUS_UNKNOWN_ERROR;
661
662 dup2(devnull, 0);
663 dup2(opipe[1], 1);
664 dup2(epipe[1], 2);
665
666 rpc_fdclose(devnull);
667 rpc_fdclose(opipe[0]);
668 rpc_fdclose(opipe[1]);
669 rpc_fdclose(epipe[0]);
670 rpc_fdclose(epipe[1]);
671
672 arglen = 2;
673 args = malloc(sizeof(char *) * arglen);
674
675 if (!args)
676 return UBUS_STATUS_UNKNOWN_ERROR;
677
678 args[0] = (char *)cmd;
679 args[1] = NULL;
680
681 if (arg)
682 {
683 blobmsg_for_each_attr(cur, arg, rem)
684 {
685 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
686 continue;
687
688 if (arglen == 255)
689 {
690 free(args);
691 return UBUS_STATUS_INVALID_ARGUMENT;
692 }
693
694 arglen++;
695 tmp = realloc(args, sizeof(char *) * arglen);
696
697 if (!tmp)
698 {
699 free(args);
700 return UBUS_STATUS_UNKNOWN_ERROR;
701 }
702
703 args = tmp;
704 args[arglen-2] = blobmsg_data(cur);
705 args[arglen-1] = NULL;
706 }
707 }
708
709 if (env)
710 {
711 blobmsg_for_each_attr(cur, env, rem)
712 {
713 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
714 continue;
715
716 setenv(blobmsg_name(cur), blobmsg_data(cur), 1);
717 }
718 }
719
720 if (execv(cmd, args))
721 return rpc_errno_status();
722
723 default:
724 memset(c, 0, sizeof(*c));
725
726 ustream_declare(c->opipe, opipe[0], exec_opipe);
727 ustream_declare(c->epipe, epipe[0], exec_epipe);
728
729 c->process.pid = pid;
730 c->process.cb = rpc_file_exec_process_cb;
731 uloop_process_add(&c->process);
732
733 c->timeout.cb = rpc_file_exec_timeout_cb;
734 uloop_timeout_set(&c->timeout, *ops->exec_timeout);
735
736 close(opipe[1]);
737 close(epipe[1]);
738
739 c->context = ctx;
740 ubus_defer_request(ctx, req, &c->request);
741 }
742
743 return UBUS_STATUS_OK;
744 }
745
746 static int
747 rpc_file_exec(struct ubus_context *ctx, struct ubus_object *obj,
748 struct ubus_request_data *req, const char *method,
749 struct blob_attr *msg)
750 {
751 struct blob_attr *tb[__RPC_E_MAX];
752
753 blobmsg_parse(rpc_exec_policy, __RPC_E_MAX, tb,
754 blob_data(msg), blob_len(msg));
755
756 if (!tb[RPC_E_CMD])
757 return UBUS_STATUS_INVALID_ARGUMENT;
758
759 return rpc_file_exec_run(blobmsg_data(tb[RPC_E_CMD]),
760 tb[RPC_E_PARM], tb[RPC_E_ENV], ctx, req);
761 }
762
763
764 static int
765 rpc_file_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
766 {
767 static const struct ubus_method file_methods[] = {
768 UBUS_METHOD("read", rpc_file_read, rpc_file_rb_policy),
769 UBUS_METHOD("write", rpc_file_write, rpc_file_rw_policy),
770 UBUS_METHOD("list", rpc_file_list, rpc_file_r_policy),
771 UBUS_METHOD("stat", rpc_file_stat, rpc_file_r_policy),
772 UBUS_METHOD("md5", rpc_file_md5, rpc_file_r_policy),
773 UBUS_METHOD("exec", rpc_file_exec, rpc_exec_policy),
774 };
775
776 static struct ubus_object_type file_type =
777 UBUS_OBJECT_TYPE("luci-rpc-file", file_methods);
778
779 static struct ubus_object obj = {
780 .name = "file",
781 .type = &file_type,
782 .methods = file_methods,
783 .n_methods = ARRAY_SIZE(file_methods),
784 };
785
786 ops = o;
787
788 return ubus_add_object(ctx, &obj);
789 }
790
791 struct rpc_plugin rpc_plugin = {
792 .init = rpc_file_api_init
793 };