file: add remove operation
[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 static char *canonpath;
74
75 enum {
76 RPC_F_R_PATH,
77 RPC_F_R_SESSION,
78 __RPC_F_R_MAX,
79 };
80
81 static const struct blobmsg_policy rpc_file_r_policy[__RPC_F_R_MAX] = {
82 [RPC_F_R_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
83 [RPC_F_R_SESSION] = { .name = "ubus_rpc_session",
84 .type = BLOBMSG_TYPE_STRING },
85 };
86
87 enum {
88 RPC_F_RB_PATH,
89 RPC_F_RB_BASE64,
90 RPC_F_RB_SESSION,
91 __RPC_F_RB_MAX,
92 };
93
94 static const struct blobmsg_policy rpc_file_rb_policy[__RPC_F_RB_MAX] = {
95 [RPC_F_RB_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
96 [RPC_F_RB_BASE64] = { .name = "base64", .type = BLOBMSG_TYPE_BOOL },
97 [RPC_F_RB_SESSION] = { .name = "ubus_rpc_session",
98 .type = BLOBMSG_TYPE_STRING },
99 };
100
101 enum {
102 RPC_F_RW_PATH,
103 RPC_F_RW_DATA,
104 RPC_F_RW_APPEND,
105 RPC_F_RW_MODE,
106 RPC_F_RW_BASE64,
107 RPC_F_RW_SESSION,
108 __RPC_F_RW_MAX,
109 };
110
111 static const struct blobmsg_policy rpc_file_rw_policy[__RPC_F_RW_MAX] = {
112 [RPC_F_RW_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
113 [RPC_F_RW_DATA] = { .name = "data", .type = BLOBMSG_TYPE_STRING },
114 [RPC_F_RW_APPEND] = { .name = "append", .type = BLOBMSG_TYPE_BOOL },
115 [RPC_F_RW_MODE] = { .name = "mode", .type = BLOBMSG_TYPE_INT32 },
116 [RPC_F_RW_BASE64] = { .name = "base64", .type = BLOBMSG_TYPE_BOOL },
117 [RPC_F_RW_SESSION] = { .name = "ubus_rpc_session",
118 .type = BLOBMSG_TYPE_STRING },
119 };
120
121 enum {
122 RPC_E_CMD,
123 RPC_E_PARM,
124 RPC_E_ENV,
125 RPC_E_SESSION,
126 __RPC_E_MAX,
127 };
128
129 static const struct blobmsg_policy rpc_exec_policy[__RPC_E_MAX] = {
130 [RPC_E_CMD] = { .name = "command", .type = BLOBMSG_TYPE_STRING },
131 [RPC_E_PARM] = { .name = "params", .type = BLOBMSG_TYPE_ARRAY },
132 [RPC_E_ENV] = { .name = "env", .type = BLOBMSG_TYPE_TABLE },
133 [RPC_E_SESSION] = { .name = "ubus_rpc_session",
134 .type = BLOBMSG_TYPE_STRING },
135 };
136
137 static const char *d_types[] = {
138 [DT_BLK] = "block",
139 [DT_CHR] = "char",
140 [DT_DIR] = "directory",
141 [DT_FIFO] = "fifo",
142 [DT_LNK] = "symlink",
143 [DT_REG] = "file",
144 [DT_SOCK] = "socket",
145 [DT_UNKNOWN] = "unknown",
146 };
147
148
149 static int
150 rpc_errno_status(void)
151 {
152 switch (errno)
153 {
154 case EACCES:
155 return UBUS_STATUS_PERMISSION_DENIED;
156
157 case ENOTDIR:
158 return UBUS_STATUS_INVALID_ARGUMENT;
159
160 case ENOENT:
161 return UBUS_STATUS_NOT_FOUND;
162
163 case EINVAL:
164 return UBUS_STATUS_INVALID_ARGUMENT;
165
166 default:
167 return UBUS_STATUS_UNKNOWN_ERROR;
168 }
169 }
170
171 static bool
172 rpc_file_read_access(const struct blob_attr *sid, const char *path)
173 {
174 if (!sid)
175 return true;
176
177 return ops->session_access(blobmsg_data(sid), "file", path, "read");
178 }
179
180 static bool
181 rpc_file_write_access(const struct blob_attr *sid, const char *path)
182 {
183 if (!sid)
184 return true;
185
186 return ops->session_access(blobmsg_data(sid), "file", path, "write");
187 }
188
189 static bool
190 rpc_file_exec_access(const struct blob_attr *sid, const char *path)
191 {
192 if (!sid)
193 return true;
194
195 return ops->session_access(blobmsg_data(sid), "file", path, "exec");
196 }
197
198 static char *
199 rpc_canonicalize_path(const char *path)
200 {
201 char *cp;
202 const char *p;
203
204 if (path == NULL || *path == '\0')
205 return NULL;
206
207 if (canonpath != NULL)
208 free(canonpath);
209
210 canonpath = strdup(path);
211
212 if (canonpath == NULL)
213 return NULL;
214
215 /* normalize */
216 for (cp = canonpath, p = path; *p != '\0'; ) {
217 if (*p != '/')
218 goto next;
219
220 /* skip repeating / */
221 if (p[1] == '/') {
222 p++;
223 continue;
224 }
225
226 /* /./ or /../ */
227 if (p[1] == '.') {
228 /* skip /./ */
229 if ((p[2] == '\0') || (p[2] == '/')) {
230 p += 2;
231 continue;
232 }
233
234 /* collapse /x/../ */
235 if ((p[2] == '.') && ((p[3] == '\0') || (p[3] == '/'))) {
236 while ((cp > canonpath) && (*--cp != '/'))
237 ;
238
239 p += 3;
240 continue;
241 }
242 }
243
244 next:
245 *cp++ = *p++;
246 }
247
248 /* remove trailing slash if not root / */
249 if ((cp > canonpath + 1) && (cp[-1] == '/'))
250 cp--;
251 else if (cp == canonpath)
252 *cp++ = '/';
253
254 *cp = '\0';
255
256 return canonpath;
257 }
258
259 static struct blob_attr **
260 rpc_check_path(struct blob_attr *msg, char **path, struct stat *s)
261 {
262 static struct blob_attr *tb[__RPC_F_R_MAX];
263
264 blobmsg_parse(rpc_file_r_policy, __RPC_F_R_MAX, tb, blob_data(msg), blob_len(msg));
265
266 if (!tb[RPC_F_R_PATH])
267 {
268 errno = EINVAL;
269 return NULL;
270 }
271
272 *path = rpc_canonicalize_path(blobmsg_get_string(tb[RPC_F_R_PATH]));
273
274 if (*path == NULL)
275 {
276 errno = ENOMEM;
277 return NULL;
278 }
279
280 if (!rpc_file_read_access(tb[RPC_F_R_SESSION], *path))
281 {
282 errno = EACCES;
283 return NULL;
284 }
285
286 if (stat(*path, s))
287 return NULL;
288
289 return tb;
290 }
291
292 static int
293 rpc_file_read(struct ubus_context *ctx, struct ubus_object *obj,
294 struct ubus_request_data *req, const char *method,
295 struct blob_attr *msg)
296 {
297 static struct blob_attr *tb[__RPC_F_RB_MAX];
298 bool base64 = false;
299 int fd, rv;
300 ssize_t len;
301 char *path;
302 struct stat s;
303 char *wbuf;
304
305 blobmsg_parse(rpc_file_rb_policy, __RPC_F_RB_MAX, tb, blob_data(msg), blob_len(msg));
306
307 if (!tb[RPC_F_RB_PATH])
308 return rpc_errno_status();
309
310 path = rpc_canonicalize_path(blobmsg_get_string(tb[RPC_F_RB_PATH]));
311
312 if (path == NULL)
313 return UBUS_STATUS_UNKNOWN_ERROR;
314
315 if (!rpc_file_read_access(tb[RPC_F_RB_SESSION], path))
316 return UBUS_STATUS_PERMISSION_DENIED;
317
318 if (stat(path, &s))
319 return rpc_errno_status();
320
321 if (s.st_size >= RPC_FILE_MAX_SIZE)
322 return UBUS_STATUS_NOT_SUPPORTED;
323
324 if ((fd = open(path, O_RDONLY)) < 0)
325 return rpc_errno_status();
326
327 /* some sysfs files do not report a length */
328 if (s.st_size == 0)
329 s.st_size = RPC_FILE_MIN_SIZE;
330
331 blob_buf_init(&buf, 0);
332
333 if (tb[RPC_F_RB_BASE64])
334 base64 = blobmsg_get_bool(tb[RPC_F_RB_BASE64]);
335
336 len = s.st_size + 1;
337 if (base64)
338 len = B64_ENCODE_LEN(s.st_size);
339 wbuf = blobmsg_alloc_string_buffer(&buf, "data", len);
340
341 if (!wbuf)
342 {
343 rv = UBUS_STATUS_UNKNOWN_ERROR;
344 goto out;
345 }
346
347 if ((len = read(fd, wbuf, s.st_size)) <= 0)
348 {
349 rv = UBUS_STATUS_NO_DATA;
350 goto out;
351 }
352
353 if (base64)
354 {
355 uint8_t *data = calloc(len, sizeof(uint8_t));
356 if (!data)
357 {
358 rv = UBUS_STATUS_UNKNOWN_ERROR;
359 goto out;
360 }
361 memcpy(data, wbuf, len);
362
363 len = b64_encode(data, len, wbuf, B64_ENCODE_LEN(len));
364 free(data);
365 if (len < 0)
366 {
367 rv = UBUS_STATUS_UNKNOWN_ERROR;
368 goto out;
369 }
370 }
371
372 *(wbuf + len) = '\0';
373 blobmsg_add_string_buffer(&buf);
374
375 ubus_send_reply(ctx, req, buf.head);
376 rv = UBUS_STATUS_OK;
377
378 out:
379 blob_buf_free(&buf);
380 close(fd);
381 return rv;
382 }
383
384 static int
385 rpc_file_write(struct ubus_context *ctx, struct ubus_object *obj,
386 struct ubus_request_data *req, const char *method,
387 struct blob_attr *msg)
388 {
389 struct blob_attr *tb[__RPC_F_RW_MAX];
390 int append = O_TRUNC;
391 mode_t prev_mode, mode = 0666;
392 int fd, rv = 0;
393 char *path = NULL;
394 void *data = NULL;
395 ssize_t data_len = 0;
396
397 blobmsg_parse(rpc_file_rw_policy, __RPC_F_RW_MAX, tb,
398 blob_data(msg), blob_len(msg));
399
400 if (!tb[RPC_F_RW_PATH] || !tb[RPC_F_RW_DATA])
401 return UBUS_STATUS_INVALID_ARGUMENT;
402
403 path = rpc_canonicalize_path(blobmsg_get_string(tb[RPC_F_RW_PATH]));
404
405 if (path == NULL)
406 return UBUS_STATUS_UNKNOWN_ERROR;
407
408 if (!rpc_file_write_access(tb[RPC_F_RW_SESSION], path))
409 return UBUS_STATUS_PERMISSION_DENIED;
410
411 data = blobmsg_data(tb[RPC_F_RW_DATA]);
412 data_len = blobmsg_data_len(tb[RPC_F_RW_DATA]) - 1;
413
414 if (tb[RPC_F_RW_APPEND] && blobmsg_get_bool(tb[RPC_F_RW_APPEND]))
415 append = O_APPEND;
416
417 if (tb[RPC_F_RW_MODE])
418 mode = blobmsg_get_u32(tb[RPC_F_RW_MODE]);
419
420 prev_mode = umask(0);
421 fd = open(path, O_CREAT | O_WRONLY | append, mode);
422 umask(prev_mode);
423 if (fd < 0)
424 return rpc_errno_status();
425
426 if (tb[RPC_F_RW_BASE64] && blobmsg_get_bool(tb[RPC_F_RW_BASE64]))
427 {
428 data_len = b64_decode(data, data, data_len);
429 if (data_len < 0)
430 {
431 rv = UBUS_STATUS_UNKNOWN_ERROR;
432 goto out;
433 }
434 }
435
436 if (write(fd, data, data_len) < 0)
437 rv = -1;
438
439 out:
440 if (fsync(fd) < 0)
441 rv = -1;
442
443 close(fd);
444 sync();
445
446 if (rv)
447 return rpc_errno_status();
448
449 return 0;
450 }
451
452 static int
453 rpc_file_md5(struct ubus_context *ctx, struct ubus_object *obj,
454 struct ubus_request_data *req, const char *method,
455 struct blob_attr *msg)
456 {
457 int rv, i;
458 char *path;
459 struct stat s;
460 uint8_t md5[16];
461 char *wbuf;
462
463 if (!rpc_check_path(msg, &path, &s))
464 return rpc_errno_status();
465
466 if (!S_ISREG(s.st_mode))
467 return UBUS_STATUS_NOT_SUPPORTED;
468
469 if ((rv = md5sum(path, md5)) <= 0)
470 return rpc_errno_status();
471
472 blob_buf_init(&buf, 0);
473 wbuf = blobmsg_alloc_string_buffer(&buf, "md5", 33);
474
475 for (i = 0; i < 16; i++)
476 sprintf(wbuf + (i * 2), "%02x", (uint8_t) md5[i]);
477
478 blobmsg_add_string_buffer(&buf);
479 ubus_send_reply(ctx, req, buf.head);
480 blob_buf_free(&buf);
481
482 return UBUS_STATUS_OK;
483 }
484
485 static void
486 _rpc_file_add_stat(struct stat *s)
487 {
488 int type;
489
490 type = S_ISREG(s->st_mode) ? DT_REG :
491 S_ISDIR(s->st_mode) ? DT_DIR :
492 S_ISCHR(s->st_mode) ? DT_CHR :
493 S_ISBLK(s->st_mode) ? DT_BLK :
494 S_ISFIFO(s->st_mode) ? DT_FIFO :
495 S_ISLNK(s->st_mode) ? DT_LNK :
496 S_ISSOCK(s->st_mode) ? DT_SOCK :
497 DT_UNKNOWN;
498
499 blobmsg_add_string(&buf, "type", d_types[type]);
500 blobmsg_add_u32(&buf, "size", s->st_size);
501 blobmsg_add_u32(&buf, "mode", s->st_mode);
502 blobmsg_add_u32(&buf, "atime", s->st_atime);
503 blobmsg_add_u32(&buf, "mtime", s->st_mtime);
504 blobmsg_add_u32(&buf, "ctime", s->st_ctime);
505 blobmsg_add_u32(&buf, "inode", s->st_ino);
506 blobmsg_add_u32(&buf, "uid", s->st_uid);
507 blobmsg_add_u32(&buf, "gid", s->st_gid);
508 }
509
510 static int
511 rpc_file_list(struct ubus_context *ctx, struct ubus_object *obj,
512 struct ubus_request_data *req, const char *method,
513 struct blob_attr *msg)
514 {
515 DIR *fd;
516 void *c, *d;
517 struct stat s;
518 struct dirent *e;
519 char *path, *entrypath;
520
521 if (!rpc_check_path(msg, &path, &s))
522 return rpc_errno_status();
523
524 if ((fd = opendir(path)) == NULL)
525 return rpc_errno_status();
526
527 blob_buf_init(&buf, 0);
528 c = blobmsg_open_array(&buf, "entries");
529
530 while ((e = readdir(fd)) != NULL)
531 {
532 if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, ".."))
533 continue;
534
535 if (asprintf(&entrypath, "%s/%s", path, e->d_name) < 0)
536 continue;
537
538 if (!stat(entrypath, &s))
539 {
540 d = blobmsg_open_table(&buf, NULL);
541 blobmsg_add_string(&buf, "name", e->d_name);
542 _rpc_file_add_stat(&s);
543 blobmsg_close_table(&buf, d);
544 }
545
546 free(entrypath);
547 }
548
549 closedir(fd);
550
551 blobmsg_close_array(&buf, c);
552 ubus_send_reply(ctx, req, buf.head);
553 blob_buf_free(&buf);
554
555 return 0;
556 }
557
558 static int
559 rpc_file_stat(struct ubus_context *ctx, struct ubus_object *obj,
560 struct ubus_request_data *req, const char *method,
561 struct blob_attr *msg)
562 {
563 char *path;
564 struct stat s;
565
566 if (!rpc_check_path(msg, &path, &s))
567 return rpc_errno_status();
568
569 blob_buf_init(&buf, 0);
570
571 blobmsg_add_string(&buf, "path", path);
572 _rpc_file_add_stat(&s);
573
574 ubus_send_reply(ctx, req, buf.head);
575 blob_buf_free(&buf);
576
577 return 0;
578 }
579
580 static int
581 rpc_file_remove_recursive(const char *path);
582
583 static int
584 rpc_file_remove_recursive(const char *path)
585 {
586 DIR *fd;
587 int err = 0;
588 struct stat s;
589 struct dirent *e;
590 char *entrypath;
591
592 if ((fd = opendir(path)) == NULL)
593 return rpc_errno_status();
594
595 for (e = readdir(fd); e != NULL && err == 0; e = readdir(fd))
596 {
597 if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, ".."))
598 continue;
599
600 if (asprintf(&entrypath, "%s/%s", path, e->d_name) >= 0)
601 {
602 if (!lstat(entrypath, &s))
603 {
604 if (S_ISDIR(s.st_mode))
605 err = rpc_file_remove_recursive(entrypath);
606 else if (unlink(entrypath))
607 err = rpc_errno_status();
608 }
609
610 free(entrypath);
611 }
612 else
613 {
614 err = UBUS_STATUS_UNKNOWN_ERROR;
615 }
616 }
617
618 closedir(fd);
619
620 if (!err && rmdir(path))
621 return rpc_errno_status();
622
623 return err;
624 }
625
626 static int
627 rpc_file_remove(struct ubus_context *ctx, struct ubus_object *obj,
628 struct ubus_request_data *req, const char *method,
629 struct blob_attr *msg)
630 {
631 struct stat s;
632 struct blob_attr *tb[__RPC_F_R_MAX];
633 char *path = NULL;
634
635 blobmsg_parse(rpc_file_r_policy, __RPC_F_R_MAX, tb,
636 blob_data(msg), blob_len(msg));
637
638 if (!tb[RPC_F_RW_PATH])
639 return UBUS_STATUS_INVALID_ARGUMENT;
640
641 path = rpc_canonicalize_path(blobmsg_get_string(tb[RPC_F_RW_PATH]));
642
643 if (path == NULL)
644 return UBUS_STATUS_UNKNOWN_ERROR;
645
646 if (!rpc_file_write_access(tb[RPC_F_R_SESSION], path))
647 return UBUS_STATUS_PERMISSION_DENIED;
648
649 if (lstat(path, &s))
650 return rpc_errno_status();
651
652 if (S_ISDIR(s.st_mode))
653 return rpc_file_remove_recursive(path);
654
655 if (unlink(path))
656 return rpc_errno_status();
657
658 return 0;
659 }
660
661 static const char *
662 rpc_file_exec_lookup(const char *cmd)
663 {
664 struct stat s;
665 int plen = 0, clen = strlen(cmd) + 1;
666 char *search, *p;
667 static char path[PATH_MAX];
668
669 if (!stat(cmd, &s) && S_ISREG(s.st_mode))
670 return cmd;
671
672 search = getenv("PATH");
673
674 if (!search)
675 search = "/bin:/usr/bin:/sbin:/usr/sbin";
676
677 p = search;
678
679 do
680 {
681 if (*p != ':' && *p != '\0')
682 continue;
683
684 plen = p - search;
685
686 if ((plen + clen) >= sizeof(path))
687 continue;
688
689 strncpy(path, search, plen);
690 sprintf(path + plen, "/%s", cmd);
691
692 if (!stat(path, &s) && S_ISREG(s.st_mode))
693 return path;
694
695 search = p + 1;
696 }
697 while (*p++);
698
699 return NULL;
700 }
701
702
703 static void
704 rpc_ustream_to_blobmsg(struct ustream *s, const char *name)
705 {
706 int len;
707 char *rbuf, *wbuf;
708
709 if ((len = ustream_pending_data(s, false)) > 0)
710 {
711 wbuf = blobmsg_alloc_string_buffer(&buf, name, len + 1);
712
713 if (!wbuf)
714 return;
715
716 ustream_for_each_read_buffer(s, rbuf, len)
717 {
718 memcpy(wbuf, rbuf, len);
719 wbuf += len;
720 }
721
722 *wbuf = 0;
723 blobmsg_add_string_buffer(&buf);
724 }
725 }
726
727 static void
728 rpc_file_exec_reply(struct rpc_file_exec_context *c, int rv)
729 {
730 uloop_timeout_cancel(&c->timeout);
731 uloop_process_delete(&c->process);
732
733 if (rv == UBUS_STATUS_OK)
734 {
735 blob_buf_init(&buf, 0);
736
737 blobmsg_add_u32(&buf, "code", WEXITSTATUS(c->stat));
738
739 rpc_ustream_to_blobmsg(&c->opipe.stream, "stdout");
740 rpc_ustream_to_blobmsg(&c->epipe.stream, "stderr");
741
742 ubus_send_reply(c->context, &c->request, buf.head);
743 blob_buf_free(&buf);
744 }
745
746 ubus_complete_deferred_request(c->context, &c->request, rv);
747
748 ustream_free(&c->opipe.stream);
749 ustream_free(&c->epipe.stream);
750
751 close(c->opipe.fd.fd);
752 close(c->epipe.fd.fd);
753
754 free(c);
755 }
756
757 static void
758 rpc_file_exec_timeout_cb(struct uloop_timeout *t)
759 {
760 struct rpc_file_exec_context *c =
761 container_of(t, struct rpc_file_exec_context, timeout);
762
763 kill(c->process.pid, SIGKILL);
764 rpc_file_exec_reply(c, UBUS_STATUS_TIMEOUT);
765 }
766
767 static void
768 rpc_file_exec_process_cb(struct uloop_process *p, int stat)
769 {
770 struct rpc_file_exec_context *c =
771 container_of(p, struct rpc_file_exec_context, process);
772
773 c->stat = stat;
774
775 ustream_poll(&c->opipe.stream);
776 ustream_poll(&c->epipe.stream);
777 }
778
779 static void
780 rpc_file_exec_opipe_read_cb(struct ustream *s, int bytes)
781 {
782 struct rpc_file_exec_context *c =
783 container_of(s, struct rpc_file_exec_context, opipe.stream);
784
785 if (ustream_read_buf_full(s))
786 rpc_file_exec_reply(c, UBUS_STATUS_NOT_SUPPORTED);
787 }
788
789 static void
790 rpc_file_exec_epipe_read_cb(struct ustream *s, int bytes)
791 {
792 struct rpc_file_exec_context *c =
793 container_of(s, struct rpc_file_exec_context, epipe.stream);
794
795 if (ustream_read_buf_full(s))
796 rpc_file_exec_reply(c, UBUS_STATUS_NOT_SUPPORTED);
797 }
798
799 static void
800 rpc_file_exec_opipe_state_cb(struct ustream *s)
801 {
802 struct rpc_file_exec_context *c =
803 container_of(s, struct rpc_file_exec_context, opipe.stream);
804
805 if (c->opipe.stream.eof && c->epipe.stream.eof)
806 rpc_file_exec_reply(c, UBUS_STATUS_OK);
807 }
808
809 static void
810 rpc_file_exec_epipe_state_cb(struct ustream *s)
811 {
812 struct rpc_file_exec_context *c =
813 container_of(s, struct rpc_file_exec_context, epipe.stream);
814
815 if (c->opipe.stream.eof && c->epipe.stream.eof)
816 rpc_file_exec_reply(c, UBUS_STATUS_OK);
817 }
818
819 static void
820 rpc_fdclose(int fd)
821 {
822 if (fd > 2)
823 close(fd);
824 }
825
826 static int
827 rpc_file_exec_run(const char *cmd, const struct blob_attr *sid,
828 const struct blob_attr *arg, const struct blob_attr *env,
829 struct ubus_context *ctx, struct ubus_request_data *req)
830 {
831 pid_t pid;
832
833 int devnull;
834 int opipe[2];
835 int epipe[2];
836
837 int rem;
838 struct blob_attr *cur;
839
840 uint8_t arglen;
841 char *executable, **args, **tmp;
842
843 struct rpc_file_exec_context *c;
844
845 cmd = rpc_file_exec_lookup(cmd);
846
847 if (!cmd)
848 return UBUS_STATUS_NOT_FOUND;
849
850 executable = rpc_canonicalize_path(cmd);
851
852 if (executable == NULL)
853 return UBUS_STATUS_UNKNOWN_ERROR;
854
855 if (!rpc_file_exec_access(sid, executable))
856 return UBUS_STATUS_PERMISSION_DENIED;
857
858 c = malloc(sizeof(*c));
859
860 if (!c)
861 return UBUS_STATUS_UNKNOWN_ERROR;
862
863 if (pipe(opipe) || pipe(epipe))
864 return rpc_errno_status();
865
866 switch ((pid = fork()))
867 {
868 case -1:
869 return rpc_errno_status();
870
871 case 0:
872 uloop_done();
873
874 devnull = open("/dev/null", O_RDWR);
875
876 if (devnull == -1)
877 return UBUS_STATUS_UNKNOWN_ERROR;
878
879 dup2(devnull, 0);
880 dup2(opipe[1], 1);
881 dup2(epipe[1], 2);
882
883 rpc_fdclose(devnull);
884 rpc_fdclose(opipe[0]);
885 rpc_fdclose(opipe[1]);
886 rpc_fdclose(epipe[0]);
887 rpc_fdclose(epipe[1]);
888
889 arglen = 2;
890 args = malloc(sizeof(char *) * arglen);
891
892 if (!args)
893 return UBUS_STATUS_UNKNOWN_ERROR;
894
895 args[0] = (char *)executable;
896 args[1] = NULL;
897
898 if (arg)
899 {
900 blobmsg_for_each_attr(cur, arg, rem)
901 {
902 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
903 continue;
904
905 if (arglen == 255)
906 {
907 free(args);
908 return UBUS_STATUS_INVALID_ARGUMENT;
909 }
910
911 arglen++;
912 tmp = realloc(args, sizeof(char *) * arglen);
913
914 if (!tmp)
915 {
916 free(args);
917 return UBUS_STATUS_UNKNOWN_ERROR;
918 }
919
920 args = tmp;
921 args[arglen-2] = blobmsg_data(cur);
922 args[arglen-1] = NULL;
923 }
924 }
925
926 if (env)
927 {
928 blobmsg_for_each_attr(cur, env, rem)
929 {
930 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
931 continue;
932
933 setenv(blobmsg_name(cur), blobmsg_data(cur), 1);
934 }
935 }
936
937 if (execv(executable, args))
938 return rpc_errno_status();
939
940 default:
941 memset(c, 0, sizeof(*c));
942
943 ustream_declare(c->opipe, opipe[0], exec_opipe);
944 ustream_declare(c->epipe, epipe[0], exec_epipe);
945
946 c->process.pid = pid;
947 c->process.cb = rpc_file_exec_process_cb;
948 uloop_process_add(&c->process);
949
950 c->timeout.cb = rpc_file_exec_timeout_cb;
951 uloop_timeout_set(&c->timeout, *ops->exec_timeout);
952
953 close(opipe[1]);
954 close(epipe[1]);
955
956 c->context = ctx;
957 ubus_defer_request(ctx, req, &c->request);
958 }
959
960 return UBUS_STATUS_OK;
961 }
962
963 static int
964 rpc_file_exec(struct ubus_context *ctx, struct ubus_object *obj,
965 struct ubus_request_data *req, const char *method,
966 struct blob_attr *msg)
967 {
968 struct blob_attr *tb[__RPC_E_MAX];
969
970 blobmsg_parse(rpc_exec_policy, __RPC_E_MAX, tb,
971 blob_data(msg), blob_len(msg));
972
973 if (!tb[RPC_E_CMD])
974 return UBUS_STATUS_INVALID_ARGUMENT;
975
976 return rpc_file_exec_run(blobmsg_data(tb[RPC_E_CMD]), tb[RPC_E_SESSION],
977 tb[RPC_E_PARM], tb[RPC_E_ENV], ctx, req);
978 }
979
980
981 static int
982 rpc_file_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
983 {
984 static const struct ubus_method file_methods[] = {
985 UBUS_METHOD("read", rpc_file_read, rpc_file_rb_policy),
986 UBUS_METHOD("write", rpc_file_write, rpc_file_rw_policy),
987 UBUS_METHOD("list", rpc_file_list, rpc_file_r_policy),
988 UBUS_METHOD("stat", rpc_file_stat, rpc_file_r_policy),
989 UBUS_METHOD("md5", rpc_file_md5, rpc_file_r_policy),
990 UBUS_METHOD("remove", rpc_file_remove, rpc_file_r_policy),
991 UBUS_METHOD("exec", rpc_file_exec, rpc_exec_policy),
992 };
993
994 static struct ubus_object_type file_type =
995 UBUS_OBJECT_TYPE("luci-rpc-file", file_methods);
996
997 static struct ubus_object obj = {
998 .name = "file",
999 .type = &file_type,
1000 .methods = file_methods,
1001 .n_methods = ARRAY_SIZE(file_methods),
1002 };
1003
1004 ops = o;
1005
1006 return ubus_add_object(ctx, &obj);
1007 }
1008
1009 struct rpc_plugin rpc_plugin = {
1010 .init = rpc_file_api_init
1011 };