rpcd: Switch to nanosleep
[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 const char *
581 rpc_file_exec_lookup(const char *cmd)
582 {
583 struct stat s;
584 int plen = 0, clen = strlen(cmd) + 1;
585 char *search, *p;
586 static char path[PATH_MAX];
587
588 if (!stat(cmd, &s) && S_ISREG(s.st_mode))
589 return cmd;
590
591 search = getenv("PATH");
592
593 if (!search)
594 search = "/bin:/usr/bin:/sbin:/usr/sbin";
595
596 p = search;
597
598 do
599 {
600 if (*p != ':' && *p != '\0')
601 continue;
602
603 plen = p - search;
604
605 if ((plen + clen) >= sizeof(path))
606 continue;
607
608 strncpy(path, search, plen);
609 sprintf(path + plen, "/%s", cmd);
610
611 if (!stat(path, &s) && S_ISREG(s.st_mode))
612 return path;
613
614 search = p + 1;
615 }
616 while (*p++);
617
618 return NULL;
619 }
620
621
622 static void
623 rpc_ustream_to_blobmsg(struct ustream *s, const char *name)
624 {
625 int len;
626 char *rbuf, *wbuf;
627
628 if ((len = ustream_pending_data(s, false)) > 0)
629 {
630 wbuf = blobmsg_alloc_string_buffer(&buf, name, len + 1);
631
632 if (!wbuf)
633 return;
634
635 ustream_for_each_read_buffer(s, rbuf, len)
636 {
637 memcpy(wbuf, rbuf, len);
638 wbuf += len;
639 }
640
641 *wbuf = 0;
642 blobmsg_add_string_buffer(&buf);
643 }
644 }
645
646 static void
647 rpc_file_exec_reply(struct rpc_file_exec_context *c, int rv)
648 {
649 uloop_timeout_cancel(&c->timeout);
650 uloop_process_delete(&c->process);
651
652 if (rv == UBUS_STATUS_OK)
653 {
654 blob_buf_init(&buf, 0);
655
656 blobmsg_add_u32(&buf, "code", WEXITSTATUS(c->stat));
657
658 rpc_ustream_to_blobmsg(&c->opipe.stream, "stdout");
659 rpc_ustream_to_blobmsg(&c->epipe.stream, "stderr");
660
661 ubus_send_reply(c->context, &c->request, buf.head);
662 blob_buf_free(&buf);
663 }
664
665 ubus_complete_deferred_request(c->context, &c->request, rv);
666
667 ustream_free(&c->opipe.stream);
668 ustream_free(&c->epipe.stream);
669
670 close(c->opipe.fd.fd);
671 close(c->epipe.fd.fd);
672
673 free(c);
674 }
675
676 static void
677 rpc_file_exec_timeout_cb(struct uloop_timeout *t)
678 {
679 struct rpc_file_exec_context *c =
680 container_of(t, struct rpc_file_exec_context, timeout);
681
682 kill(c->process.pid, SIGKILL);
683 rpc_file_exec_reply(c, UBUS_STATUS_TIMEOUT);
684 }
685
686 static void
687 rpc_file_exec_process_cb(struct uloop_process *p, int stat)
688 {
689 struct rpc_file_exec_context *c =
690 container_of(p, struct rpc_file_exec_context, process);
691
692 c->stat = stat;
693
694 ustream_poll(&c->opipe.stream);
695 ustream_poll(&c->epipe.stream);
696 }
697
698 static void
699 rpc_file_exec_opipe_read_cb(struct ustream *s, int bytes)
700 {
701 struct rpc_file_exec_context *c =
702 container_of(s, struct rpc_file_exec_context, opipe.stream);
703
704 if (ustream_read_buf_full(s))
705 rpc_file_exec_reply(c, UBUS_STATUS_NOT_SUPPORTED);
706 }
707
708 static void
709 rpc_file_exec_epipe_read_cb(struct ustream *s, int bytes)
710 {
711 struct rpc_file_exec_context *c =
712 container_of(s, struct rpc_file_exec_context, epipe.stream);
713
714 if (ustream_read_buf_full(s))
715 rpc_file_exec_reply(c, UBUS_STATUS_NOT_SUPPORTED);
716 }
717
718 static void
719 rpc_file_exec_opipe_state_cb(struct ustream *s)
720 {
721 struct rpc_file_exec_context *c =
722 container_of(s, struct rpc_file_exec_context, opipe.stream);
723
724 if (c->opipe.stream.eof && c->epipe.stream.eof)
725 rpc_file_exec_reply(c, UBUS_STATUS_OK);
726 }
727
728 static void
729 rpc_file_exec_epipe_state_cb(struct ustream *s)
730 {
731 struct rpc_file_exec_context *c =
732 container_of(s, struct rpc_file_exec_context, epipe.stream);
733
734 if (c->opipe.stream.eof && c->epipe.stream.eof)
735 rpc_file_exec_reply(c, UBUS_STATUS_OK);
736 }
737
738 static void
739 rpc_fdclose(int fd)
740 {
741 if (fd > 2)
742 close(fd);
743 }
744
745 static int
746 rpc_file_exec_run(const char *cmd, const struct blob_attr *sid,
747 const struct blob_attr *arg, const struct blob_attr *env,
748 struct ubus_context *ctx, struct ubus_request_data *req)
749 {
750 pid_t pid;
751
752 int devnull;
753 int opipe[2];
754 int epipe[2];
755
756 int rem;
757 struct blob_attr *cur;
758
759 uint8_t arglen;
760 char *executable, **args, **tmp;
761
762 struct rpc_file_exec_context *c;
763
764 cmd = rpc_file_exec_lookup(cmd);
765
766 if (!cmd)
767 return UBUS_STATUS_NOT_FOUND;
768
769 executable = rpc_canonicalize_path(cmd);
770
771 if (executable == NULL)
772 return UBUS_STATUS_UNKNOWN_ERROR;
773
774 if (!rpc_file_exec_access(sid, executable))
775 return UBUS_STATUS_PERMISSION_DENIED;
776
777 c = malloc(sizeof(*c));
778
779 if (!c)
780 return UBUS_STATUS_UNKNOWN_ERROR;
781
782 if (pipe(opipe) || pipe(epipe))
783 return rpc_errno_status();
784
785 switch ((pid = fork()))
786 {
787 case -1:
788 return rpc_errno_status();
789
790 case 0:
791 uloop_done();
792
793 devnull = open("/dev/null", O_RDWR);
794
795 if (devnull == -1)
796 return UBUS_STATUS_UNKNOWN_ERROR;
797
798 dup2(devnull, 0);
799 dup2(opipe[1], 1);
800 dup2(epipe[1], 2);
801
802 rpc_fdclose(devnull);
803 rpc_fdclose(opipe[0]);
804 rpc_fdclose(opipe[1]);
805 rpc_fdclose(epipe[0]);
806 rpc_fdclose(epipe[1]);
807
808 arglen = 2;
809 args = malloc(sizeof(char *) * arglen);
810
811 if (!args)
812 return UBUS_STATUS_UNKNOWN_ERROR;
813
814 args[0] = (char *)executable;
815 args[1] = NULL;
816
817 if (arg)
818 {
819 blobmsg_for_each_attr(cur, arg, rem)
820 {
821 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
822 continue;
823
824 if (arglen == 255)
825 {
826 free(args);
827 return UBUS_STATUS_INVALID_ARGUMENT;
828 }
829
830 arglen++;
831 tmp = realloc(args, sizeof(char *) * arglen);
832
833 if (!tmp)
834 {
835 free(args);
836 return UBUS_STATUS_UNKNOWN_ERROR;
837 }
838
839 args = tmp;
840 args[arglen-2] = blobmsg_data(cur);
841 args[arglen-1] = NULL;
842 }
843 }
844
845 if (env)
846 {
847 blobmsg_for_each_attr(cur, env, rem)
848 {
849 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
850 continue;
851
852 setenv(blobmsg_name(cur), blobmsg_data(cur), 1);
853 }
854 }
855
856 if (execv(executable, args))
857 return rpc_errno_status();
858
859 default:
860 memset(c, 0, sizeof(*c));
861
862 ustream_declare(c->opipe, opipe[0], exec_opipe);
863 ustream_declare(c->epipe, epipe[0], exec_epipe);
864
865 c->process.pid = pid;
866 c->process.cb = rpc_file_exec_process_cb;
867 uloop_process_add(&c->process);
868
869 c->timeout.cb = rpc_file_exec_timeout_cb;
870 uloop_timeout_set(&c->timeout, *ops->exec_timeout);
871
872 close(opipe[1]);
873 close(epipe[1]);
874
875 c->context = ctx;
876 ubus_defer_request(ctx, req, &c->request);
877 }
878
879 return UBUS_STATUS_OK;
880 }
881
882 static int
883 rpc_file_exec(struct ubus_context *ctx, struct ubus_object *obj,
884 struct ubus_request_data *req, const char *method,
885 struct blob_attr *msg)
886 {
887 struct blob_attr *tb[__RPC_E_MAX];
888
889 blobmsg_parse(rpc_exec_policy, __RPC_E_MAX, tb,
890 blob_data(msg), blob_len(msg));
891
892 if (!tb[RPC_E_CMD])
893 return UBUS_STATUS_INVALID_ARGUMENT;
894
895 return rpc_file_exec_run(blobmsg_data(tb[RPC_E_CMD]), tb[RPC_E_SESSION],
896 tb[RPC_E_PARM], tb[RPC_E_ENV], ctx, req);
897 }
898
899
900 static int
901 rpc_file_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
902 {
903 static const struct ubus_method file_methods[] = {
904 UBUS_METHOD("read", rpc_file_read, rpc_file_rb_policy),
905 UBUS_METHOD("write", rpc_file_write, rpc_file_rw_policy),
906 UBUS_METHOD("list", rpc_file_list, rpc_file_r_policy),
907 UBUS_METHOD("stat", rpc_file_stat, rpc_file_r_policy),
908 UBUS_METHOD("md5", rpc_file_md5, rpc_file_r_policy),
909 UBUS_METHOD("exec", rpc_file_exec, rpc_exec_policy),
910 };
911
912 static struct ubus_object_type file_type =
913 UBUS_OBJECT_TYPE("luci-rpc-file", file_methods);
914
915 static struct ubus_object obj = {
916 .name = "file",
917 .type = &file_type,
918 .methods = file_methods,
919 .n_methods = ARRAY_SIZE(file_methods),
920 };
921
922 ops = o;
923
924 return ubus_add_object(ctx, &obj);
925 }
926
927 struct rpc_plugin rpc_plugin = {
928 .init = rpc_file_api_init
929 };