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