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