cmake: Find libubus.h
[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 #include <fcntl.h>
21 #include <errno.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <limits.h>
26 #include <dirent.h>
27 #include <sys/stat.h>
28 #include <sys/wait.h>
29 #include <libubus.h>
30 #include <libubox/blobmsg.h>
31 #include <libubox/md5.h>
32 #include <libubox/ustream.h>
33 #include <libubox/utils.h>
34
35 #include <rpcd/plugin.h>
36
37 /* limit of sys & proc files */
38 #define RPC_FILE_MIN_SIZE               (128)
39
40 /* limit of regular files and command output data */
41 #define RPC_FILE_MAX_SIZE               (4096 * 64)
42 #define RPC_FILE_MAX_RUNTIME    (3 * 1000)
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 struct rpc_file_exec_context {
58         struct ubus_context *context;
59         struct ubus_request_data request;
60         struct uloop_timeout timeout;
61         struct uloop_process process;
62         struct ustream_fd opipe;
63         struct ustream_fd epipe;
64         int outlen;
65         char *out;
66         int errlen;
67         char *err;
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 int
358 rpc_file_list(struct ubus_context *ctx, struct ubus_object *obj,
359               struct ubus_request_data *req, const char *method,
360               struct blob_attr *msg)
361 {
362         DIR *fd;
363         void *c, *d;
364         char *path;
365         struct stat s;
366         struct dirent *e;
367
368         if (!rpc_check_path(msg, &path, &s))
369                 return rpc_errno_status();
370
371         if ((fd = opendir(path)) == NULL)
372                 return rpc_errno_status();
373
374         blob_buf_init(&buf, 0);
375         c = blobmsg_open_array(&buf, "entries");
376
377         while ((e = readdir(fd)) != NULL)
378         {
379                 if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, ".."))
380                         continue;
381
382                 d = blobmsg_open_table(&buf, NULL);
383                 blobmsg_add_string(&buf, "name", e->d_name);
384                 blobmsg_add_string(&buf, "type", d_types[e->d_type]);
385                 blobmsg_close_table(&buf, d);
386         }
387
388         closedir(fd);
389
390         blobmsg_close_array(&buf, c);
391         ubus_send_reply(ctx, req, buf.head);
392         blob_buf_free(&buf);
393
394         return 0;
395 }
396
397 static int
398 rpc_file_stat(struct ubus_context *ctx, struct ubus_object *obj,
399               struct ubus_request_data *req, const char *method,
400               struct blob_attr *msg)
401 {
402         int type;
403         char *path;
404         struct stat s;
405
406         if (!rpc_check_path(msg, &path, &s))
407                 return rpc_errno_status();
408
409         blob_buf_init(&buf, 0);
410
411         type = S_ISREG(s.st_mode) ? DT_REG :
412                 S_ISDIR(s.st_mode) ? DT_DIR :
413                  S_ISCHR(s.st_mode) ? DT_CHR :
414                   S_ISBLK(s.st_mode) ? DT_BLK :
415                    S_ISFIFO(s.st_mode) ? DT_FIFO :
416                     S_ISLNK(s.st_mode) ? DT_LNK :
417                      S_ISSOCK(s.st_mode) ? DT_SOCK :
418                       DT_UNKNOWN;
419
420         blobmsg_add_string(&buf, "path", path);
421         blobmsg_add_string(&buf, "type", d_types[type]);
422         blobmsg_add_u32(&buf, "size",  s.st_size);
423         blobmsg_add_u32(&buf, "mode",  s.st_mode);
424         blobmsg_add_u32(&buf, "atime", s.st_atime);
425         blobmsg_add_u32(&buf, "mtime", s.st_mtime);
426         blobmsg_add_u32(&buf, "ctime", s.st_ctime);
427         blobmsg_add_u32(&buf, "inode", s.st_ino);
428         blobmsg_add_u32(&buf, "uid",   s.st_uid);
429         blobmsg_add_u32(&buf, "gid",   s.st_gid);
430
431         ubus_send_reply(ctx, req, buf.head);
432         blob_buf_free(&buf);
433
434         return 0;
435 }
436
437 static const char *
438 rpc_file_exec_lookup(const char *cmd)
439 {
440         struct stat s;
441         int plen = 0, clen = strlen(cmd) + 1;
442         char *search, *p;
443         static char path[PATH_MAX];
444
445         if (!stat(cmd, &s) && S_ISREG(s.st_mode))
446                 return cmd;
447
448         search = getenv("PATH");
449
450         if (!search)
451                 search = "/bin:/usr/bin:/sbin:/usr/sbin";
452
453         p = search;
454
455         do
456         {
457                 if (*p != ':' && *p != '\0')
458                         continue;
459
460                 plen = p - search;
461
462                 if ((plen + clen) >= sizeof(path))
463                         continue;
464
465                 strncpy(path, search, plen);
466                 sprintf(path + plen, "/%s", cmd);
467
468                 if (!stat(path, &s) && S_ISREG(s.st_mode))
469                         return path;
470
471                 search = p + 1;
472         }
473         while (*p++);
474
475         return NULL;
476 }
477
478
479 static void
480 rpc_ustream_to_blobmsg(struct ustream *s, const char *name)
481 {
482         int len;
483         char *rbuf, *wbuf;
484
485         if ((len = ustream_pending_data(s, false)) > 0)
486         {
487                 wbuf = blobmsg_alloc_string_buffer(&buf, name, len + 1);
488
489                 if (!wbuf)
490                         return;
491
492                 ustream_for_each_read_buffer(s, rbuf, len)
493                 {
494                         memcpy(wbuf, rbuf, len);
495                         wbuf += len;
496                 }
497
498                 *wbuf = 0;
499                 blobmsg_add_string_buffer(&buf);
500         }
501 }
502
503 static void
504 rpc_file_exec_reply(struct rpc_file_exec_context *c, int rv)
505 {
506         uloop_timeout_cancel(&c->timeout);
507         uloop_process_delete(&c->process);
508
509         if (rv == UBUS_STATUS_OK)
510         {
511                 blob_buf_init(&buf, 0);
512
513                 blobmsg_add_u32(&buf, "code", WEXITSTATUS(c->stat));
514
515                 rpc_ustream_to_blobmsg(&c->opipe.stream, "stdout");
516                 rpc_ustream_to_blobmsg(&c->epipe.stream, "stderr");
517
518                 ubus_send_reply(c->context, &c->request, buf.head);
519                 blob_buf_free(&buf);
520         }
521
522         ubus_complete_deferred_request(c->context, &c->request, rv);
523
524         ustream_free(&c->opipe.stream);
525         ustream_free(&c->epipe.stream);
526
527         close(c->opipe.fd.fd);
528         close(c->epipe.fd.fd);
529
530         free(c);
531 }
532
533 static void
534 rpc_file_exec_timeout_cb(struct uloop_timeout *t)
535 {
536         struct rpc_file_exec_context *c =
537                 container_of(t, struct rpc_file_exec_context, timeout);
538
539         kill(c->process.pid, SIGKILL);
540         rpc_file_exec_reply(c, UBUS_STATUS_TIMEOUT);
541 }
542
543 static void
544 rpc_file_exec_process_cb(struct uloop_process *p, int stat)
545 {
546         struct rpc_file_exec_context *c =
547                 container_of(p, struct rpc_file_exec_context, process);
548
549         c->stat = stat;
550
551         ustream_poll(&c->opipe.stream);
552         ustream_poll(&c->epipe.stream);
553 }
554
555 static void
556 rpc_file_exec_opipe_read_cb(struct ustream *s, int bytes)
557 {
558         struct rpc_file_exec_context *c =
559                 container_of(s, struct rpc_file_exec_context, opipe.stream);
560
561         if (ustream_read_buf_full(s))
562                 rpc_file_exec_reply(c, UBUS_STATUS_NOT_SUPPORTED);
563 }
564
565 static void
566 rpc_file_exec_epipe_read_cb(struct ustream *s, int bytes)
567 {
568         struct rpc_file_exec_context *c =
569                 container_of(s, struct rpc_file_exec_context, epipe.stream);
570
571         if (ustream_read_buf_full(s))
572                 rpc_file_exec_reply(c, UBUS_STATUS_NOT_SUPPORTED);
573 }
574
575 static void
576 rpc_file_exec_opipe_state_cb(struct ustream *s)
577 {
578         struct rpc_file_exec_context *c =
579                 container_of(s, struct rpc_file_exec_context, opipe.stream);
580
581         if (c->opipe.stream.eof && c->epipe.stream.eof)
582                 rpc_file_exec_reply(c, UBUS_STATUS_OK);
583 }
584
585 static void
586 rpc_file_exec_epipe_state_cb(struct ustream *s)
587 {
588         struct rpc_file_exec_context *c =
589                 container_of(s, struct rpc_file_exec_context, epipe.stream);
590
591         if (c->opipe.stream.eof && c->epipe.stream.eof)
592                 rpc_file_exec_reply(c, UBUS_STATUS_OK);
593 }
594
595 static int
596 rpc_file_exec_run(const char *cmd,
597                   const struct blob_attr *arg, const struct blob_attr *env,
598                   struct ubus_context *ctx, struct ubus_request_data *req)
599 {
600         pid_t pid;
601
602         int opipe[2];
603         int epipe[2];
604
605         int rem;
606         struct blob_attr *cur;
607
608         char arglen;
609         char **args;
610
611         struct rpc_file_exec_context *c;
612
613         cmd = rpc_file_exec_lookup(cmd);
614
615         if (!cmd)
616                 return UBUS_STATUS_NOT_FOUND;
617
618         c = malloc(sizeof(*c));
619
620         if (!c)
621                 return UBUS_STATUS_UNKNOWN_ERROR;
622
623         if (pipe(opipe) || pipe(epipe))
624                 return rpc_errno_status();
625
626         switch ((pid = fork()))
627         {
628         case -1:
629                 return rpc_errno_status();
630
631         case 0:
632                 uloop_done();
633
634                 dup2(opipe[1], 1);
635                 dup2(epipe[1], 2);
636
637                 close(0);
638                 close(opipe[0]);
639                 close(opipe[1]);
640                 close(epipe[0]);
641                 close(epipe[1]);
642
643                 arglen = 2;
644                 args = malloc(sizeof(char *) * arglen);
645
646                 if (!args)
647                         return UBUS_STATUS_UNKNOWN_ERROR;
648
649                 args[0] = (char *)cmd;
650                 args[1] = NULL;
651
652                 if (arg)
653                 {
654                         blobmsg_for_each_attr(cur, arg, rem)
655                         {
656                                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
657                                         continue;
658
659                                 arglen++;
660
661                                 if (!(args = realloc(args, sizeof(char *) * arglen)))
662                                         return UBUS_STATUS_UNKNOWN_ERROR;
663
664                                 args[arglen-2] = blobmsg_data(cur);
665                                 args[arglen-1] = NULL;
666                         }
667                 }
668
669                 if (env)
670                 {
671                         blobmsg_for_each_attr(cur, env, rem)
672                         {
673                                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
674                                         continue;
675
676                                 setenv(blobmsg_name(cur), blobmsg_data(cur), 1);
677                         }
678                 }
679
680                 if (execv(cmd, args))
681                         return rpc_errno_status();
682
683         default:
684                 memset(c, 0, sizeof(*c));
685
686                 ustream_declare(c->opipe, opipe[0], exec_opipe);
687                 ustream_declare(c->epipe, epipe[0], exec_epipe);
688
689                 c->process.pid = pid;
690                 c->process.cb = rpc_file_exec_process_cb;
691                 uloop_process_add(&c->process);
692
693                 c->timeout.cb = rpc_file_exec_timeout_cb;
694                 uloop_timeout_set(&c->timeout, RPC_FILE_MAX_RUNTIME);
695
696                 close(opipe[1]);
697                 close(epipe[1]);
698
699                 c->context = ctx;
700                 ubus_defer_request(ctx, req, &c->request);
701         }
702
703         return UBUS_STATUS_OK;
704 }
705
706 static int
707 rpc_file_exec(struct ubus_context *ctx, struct ubus_object *obj,
708               struct ubus_request_data *req, const char *method,
709               struct blob_attr *msg)
710 {
711         struct blob_attr *tb[__RPC_E_MAX];
712
713         blobmsg_parse(rpc_exec_policy, __RPC_E_MAX, tb,
714                       blob_data(msg), blob_len(msg));
715
716         if (!tb[RPC_E_CMD])
717                 return UBUS_STATUS_INVALID_ARGUMENT;
718
719         return rpc_file_exec_run(blobmsg_data(tb[RPC_E_CMD]),
720                                               tb[RPC_E_PARM], tb[RPC_E_ENV], ctx, req);
721 }
722
723
724 static int
725 rpc_file_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
726 {
727         static const struct ubus_method file_methods[] = {
728                 UBUS_METHOD("read",    rpc_file_read,  rpc_file_rb_policy),
729                 UBUS_METHOD("write",   rpc_file_write, rpc_file_rw_policy),
730                 UBUS_METHOD("list",    rpc_file_list,  rpc_file_r_policy),
731                 UBUS_METHOD("stat",    rpc_file_stat,  rpc_file_r_policy),
732                 UBUS_METHOD("md5",     rpc_file_md5,   rpc_file_r_policy),
733                 UBUS_METHOD("exec",    rpc_file_exec,  rpc_exec_policy),
734         };
735
736         static struct ubus_object_type file_type =
737                 UBUS_OBJECT_TYPE("luci-rpc-file", file_methods);
738
739         static struct ubus_object obj = {
740                 .name = "file",
741                 .type = &file_type,
742                 .methods = file_methods,
743                 .n_methods = ARRAY_SIZE(file_methods),
744         };
745
746         return ubus_add_object(ctx, &obj);
747 }
748
749 struct rpc_plugin rpc_plugin = {
750         .init = rpc_file_api_init
751 };