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 <pwd.h> 23 #include <grp.h> 24 #include <fcntl.h> 25 #include <errno.h> 26 #include <unistd.h> 27 #include <stdint.h> 28 #include <stdlib.h> 29 #include <string.h> 30 #include <limits.h> 31 #include <dirent.h> 32 #include <sys/stat.h> 33 #include <sys/wait.h> 34 #include <libubus.h> 35 #include <libubox/blobmsg.h> 36 #include <libubox/md5.h> 37 #include <libubox/ustream.h> 38 #include <libubox/utils.h> 39 40 #include <rpcd/plugin.h> 41 42 /* limit of sys & proc files */ 43 #define RPC_FILE_MIN_SIZE (4096) 44 45 /* limit of regular files and command output data */ 46 #define RPC_FILE_MAX_SIZE (4096 * 64) 47 48 /* limit of command line length for exec acl checks */ 49 #define RPC_CMDLINE_MAX_SIZE (1024) 50 51 #define ustream_for_each_read_buffer(stream, ptr, len) \ 52 for (ptr = ustream_get_read_buf(stream, &len); \ 53 ptr != NULL && len > 0; \ 54 ustream_consume(stream, len), ptr = ustream_get_read_buf(stream, &len)) 55 56 #define ustream_declare(us, fd, name) \ 57 us.stream.string_data = true; \ 58 us.stream.r.buffer_len = 4096; \ 59 us.stream.r.max_buffers = RPC_FILE_MAX_SIZE / 4096; \ 60 us.stream.notify_read = rpc_file_##name##_read_cb; \ 61 us.stream.notify_state = rpc_file_##name##_state_cb; \ 62 ustream_fd_init(&us, fd); 63 64 static const struct rpc_daemon_ops *ops; 65 66 struct rpc_file_exec_context { 67 struct ubus_context *context; 68 struct ubus_request_data request; 69 struct uloop_timeout timeout; 70 struct uloop_process process; 71 struct ustream_fd opipe; 72 struct ustream_fd epipe; 73 int stat; 74 int deferred_status; 75 }; 76 77 78 static struct blob_buf buf; 79 static char *canonpath; 80 static char *resolvedpath; 81 static char cmdstr[RPC_CMDLINE_MAX_SIZE]; 82 83 enum { 84 RPC_F_R_PATH, 85 RPC_F_R_SESSION, 86 __RPC_F_R_MAX, 87 }; 88 89 static const struct blobmsg_policy rpc_file_R_policy[__RPC_F_R_MAX] = { 90 [RPC_F_R_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING }, 91 [RPC_F_R_SESSION] = { .name = "ubus_rpc_session", 92 .type = BLOBMSG_TYPE_STRING }, 93 }; 94 95 enum { 96 RPC_F_RB_PATH, 97 RPC_F_RB_BASE64, 98 RPC_F_RB_SESSION, 99 __RPC_F_RB_MAX, 100 }; 101 102 static const struct blobmsg_policy rpc_file_RB_policy[__RPC_F_RB_MAX] = { 103 [RPC_F_RB_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING }, 104 [RPC_F_RB_BASE64] = { .name = "base64", .type = BLOBMSG_TYPE_BOOL }, 105 [RPC_F_RB_SESSION] = { .name = "ubus_rpc_session", 106 .type = BLOBMSG_TYPE_STRING }, 107 }; 108 109 enum { 110 RPC_F_RW_PATH, 111 RPC_F_RW_DATA, 112 RPC_F_RW_APPEND, 113 RPC_F_RW_MODE, 114 RPC_F_RW_BASE64, 115 RPC_F_RW_SESSION, 116 __RPC_F_RW_MAX, 117 }; 118 119 static const struct blobmsg_policy rpc_file_RW_policy[__RPC_F_RW_MAX] = { 120 [RPC_F_RW_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING }, 121 [RPC_F_RW_DATA] = { .name = "data", .type = BLOBMSG_TYPE_STRING }, 122 [RPC_F_RW_APPEND] = { .name = "append", .type = BLOBMSG_TYPE_BOOL }, 123 [RPC_F_RW_MODE] = { .name = "mode", .type = BLOBMSG_TYPE_INT32 }, 124 [RPC_F_RW_BASE64] = { .name = "base64", .type = BLOBMSG_TYPE_BOOL }, 125 [RPC_F_RW_SESSION] = { .name = "ubus_rpc_session", 126 .type = BLOBMSG_TYPE_STRING }, 127 }; 128 129 enum { 130 RPC_E_CMD, 131 RPC_E_PARM, 132 RPC_E_ENV, 133 RPC_E_SESSION, 134 __RPC_E_MAX, 135 }; 136 137 static const struct blobmsg_policy rpc_exec_policy[__RPC_E_MAX] = { 138 [RPC_E_CMD] = { .name = "command", .type = BLOBMSG_TYPE_STRING }, 139 [RPC_E_PARM] = { .name = "params", .type = BLOBMSG_TYPE_ARRAY }, 140 [RPC_E_ENV] = { .name = "env", .type = BLOBMSG_TYPE_TABLE }, 141 [RPC_E_SESSION] = { .name = "ubus_rpc_session", 142 .type = BLOBMSG_TYPE_STRING }, 143 }; 144 145 static const char *d_types[] = { 146 [DT_BLK] = "block", 147 [DT_CHR] = "char", 148 [DT_DIR] = "directory", 149 [DT_FIFO] = "fifo", 150 [DT_LNK] = "symlink", 151 [DT_REG] = "file", 152 [DT_SOCK] = "socket", 153 [DT_UNKNOWN] = "unknown", 154 }; 155 156 157 static int 158 rpc_errno_status(void) 159 { 160 switch (errno) 161 { 162 case EACCES: 163 return UBUS_STATUS_PERMISSION_DENIED; 164 165 case ENOTDIR: 166 return UBUS_STATUS_INVALID_ARGUMENT; 167 168 case ENOENT: 169 return UBUS_STATUS_NOT_FOUND; 170 171 case EINVAL: 172 return UBUS_STATUS_INVALID_ARGUMENT; 173 174 default: 175 return UBUS_STATUS_UNKNOWN_ERROR; 176 } 177 } 178 179 static bool 180 rpc_file_access(const struct blob_attr *sid, 181 const char *path, const char *perm) 182 { 183 if (!sid) 184 return true; 185 186 return ops->session_access(blobmsg_data(sid), "file", path, perm); 187 } 188 189 static char * 190 rpc_canonicalize_path(const char *path) 191 { 192 char *cp; 193 const char *p; 194 195 if (path == NULL || *path == '\0') 196 return NULL; 197 198 if (canonpath != NULL) 199 free(canonpath); 200 201 canonpath = strdup(path); 202 203 if (canonpath == NULL) 204 return NULL; 205 206 /* normalize */ 207 for (cp = canonpath, p = path; *p != '\0'; ) { 208 if (*p != '/') 209 goto next; 210 211 /* skip repeating / */ 212 if (p[1] == '/') { 213 p++; 214 continue; 215 } 216 217 /* /./ or /../ */ 218 if (p[1] == '.') { 219 /* skip /./ */ 220 if ((p[2] == '\0') || (p[2] == '/')) { 221 p += 2; 222 continue; 223 } 224 225 /* collapse /x/../ */ 226 if ((p[2] == '.') && ((p[3] == '\0') || (p[3] == '/'))) { 227 while ((cp > canonpath) && (*--cp != '/')) 228 ; 229 230 p += 3; 231 continue; 232 } 233 } 234 235 next: 236 *cp++ = *p++; 237 } 238 239 /* remove trailing slash if not root / */ 240 if ((cp > canonpath + 1) && (cp[-1] == '/')) 241 cp--; 242 else if (cp == canonpath) 243 *cp++ = '/'; 244 245 *cp = '\0'; 246 247 return canonpath; 248 } 249 250 /* 251 * rpc_canonicalize_path() only folds "//", "/./" and "/../" textually; it 252 * never resolves symlink components. Since the ACL check above matches the 253 * textual path, a symlink placed inside an ACL-covered directory would let 254 * a grant on the link authorize whatever file the link points to once the 255 * caller's stat()/open()/opendir() follows it. Re-resolve *path with 256 * realpath() and, if that changes the path, re-run the ACL check against 257 * the resolved target so grants only ever cover the real file. If the 258 * target does not exist yet (e.g. a new file being written), resolve and 259 * re-check the containing directory instead and rebuild *path from that. 260 */ 261 static bool 262 rpc_check_symlink_access(const struct blob_attr *sid, const char *perm, char **path) 263 { 264 char resolved[PATH_MAX]; 265 char dirbuf[PATH_MAX]; 266 const char *base; 267 struct stat lst; 268 269 if (realpath(*path, resolved) != NULL) 270 { 271 if (strcmp(resolved, *path) == 0) 272 return true; 273 274 if (!rpc_file_access(sid, resolved, perm)) 275 { 276 errno = EACCES; 277 return false; 278 } 279 280 free(resolvedpath); 281 resolvedpath = strdup(resolved); 282 283 if (resolvedpath == NULL) 284 { 285 errno = ENOMEM; 286 return false; 287 } 288 289 *path = resolvedpath; 290 return true; 291 } 292 293 if (errno == ENOENT) 294 { 295 /* realpath() also fails with ENOENT for a dangling symlink whose 296 * final target component is missing. Distinguish that case (an 297 * *existing* symlink we must not silently create-through, e.g. 298 * via open(O_CREAT) on file.write) from a genuinely nonexistent 299 * path by lstat()'ing the requested path itself. */ 300 errno = (lstat(*path, &lst) == 0 && S_ISLNK(lst.st_mode)) ? EACCES : 0; 301 } 302 303 if (errno != 0) 304 return false; 305 306 base = strrchr(*path, '/'); 307 308 if (base == NULL) 309 { 310 errno = ENOENT; 311 return false; 312 } 313 314 if (base == *path) 315 { 316 dirbuf[0] = '/'; 317 dirbuf[1] = '\0'; 318 } 319 else if ((size_t)(base - *path) >= sizeof(dirbuf)) 320 { 321 errno = ENAMETOOLONG; 322 return false; 323 } 324 else 325 { 326 memcpy(dirbuf, *path, base - *path); 327 dirbuf[base - *path] = '\0'; 328 } 329 330 base++; 331 332 if (realpath(dirbuf, resolved) == NULL) 333 return false; 334 335 if (strcmp(resolved, dirbuf) == 0) 336 { 337 errno = ENOENT; 338 return true; 339 } 340 341 free(resolvedpath); 342 343 if (asprintf(&resolvedpath, "%s/%s", resolved, base) < 0) 344 { 345 errno = ENOMEM; 346 return false; 347 } 348 349 if (!rpc_file_access(sid, resolvedpath, perm)) 350 { 351 errno = EACCES; 352 return false; 353 } 354 355 *path = resolvedpath; 356 errno = ENOENT; 357 358 return true; 359 } 360 361 static struct blob_attr ** 362 __rpc_check_path(const struct blobmsg_policy *policy, size_t policy_len, 363 int policy_path_idx, int policy_sid_idx, const char *perm, 364 struct blob_attr *msg, char **path, struct stat *s, bool use_lstat, 365 bool resolve_symlinks) 366 { 367 static struct blob_attr *tb[__RPC_F_RW_MAX]; /* largest _MAX constant */ 368 369 blobmsg_parse(policy, policy_len, tb, blob_data(msg), blob_len(msg)); 370 371 if (!tb[policy_path_idx]) 372 { 373 errno = EINVAL; 374 return NULL; 375 } 376 377 *path = rpc_canonicalize_path(blobmsg_get_string(tb[policy_path_idx])); 378 379 if (*path == NULL) 380 { 381 errno = ENOMEM; 382 return NULL; 383 } 384 385 if (!rpc_file_access(tb[policy_sid_idx], *path, perm)) 386 { 387 errno = EACCES; 388 return NULL; 389 } 390 391 if (resolve_symlinks && !rpc_check_symlink_access(tb[policy_sid_idx], perm, path)) 392 return NULL; 393 394 if (s != NULL && (use_lstat ? lstat(*path, s) : stat(*path, s)) != 0) 395 return NULL; 396 397 return tb; 398 } 399 400 // use_lstat defaults to false, symlinks are resolved and re-authorized 401 #define rpc_check_path(msg, policy_selector, perm, path, s) \ 402 __rpc_check_path(rpc_file_ ## policy_selector ## _policy, \ 403 ARRAY_SIZE(rpc_file_ ## policy_selector ## _policy), \ 404 RPC_F_ ## policy_selector ## _PATH, \ 405 RPC_F_ ## policy_selector ## _SESSION, \ 406 perm, msg, path, s, false, true) 407 408 // use_lstat control; symlinks are left unresolved since callers either 409 // want to inspect the link itself (lstat) or already handle it safely 410 // via unlink()'s no-follow semantics (remove) 411 #define rpc_check_path_with_lstat(msg, policy_selector, perm, path, s, use_lstat) \ 412 __rpc_check_path(rpc_file_ ## policy_selector ## _policy, \ 413 ARRAY_SIZE(rpc_file_ ## policy_selector ## _policy), \ 414 RPC_F_ ## policy_selector ## _PATH, \ 415 RPC_F_ ## policy_selector ## _SESSION, \ 416 perm, msg, path, s, use_lstat, false) 417 418 static int 419 rpc_file_read(struct ubus_context *ctx, struct ubus_object *obj, 420 struct ubus_request_data *req, const char *method, 421 struct blob_attr *msg) 422 { 423 struct blob_attr **tb; 424 bool base64 = false; 425 int fd, rv; 426 ssize_t len; 427 char *path; 428 struct stat s; 429 char *wbuf; 430 431 tb = rpc_check_path(msg, RB, "read", &path, &s); 432 433 if (tb == NULL) 434 return rpc_errno_status(); 435 436 if (s.st_size >= RPC_FILE_MAX_SIZE) 437 return UBUS_STATUS_NOT_SUPPORTED; 438 439 if ((fd = open(path, O_RDONLY)) < 0) 440 return rpc_errno_status(); 441 442 /* some sysfs files do not report a length */ 443 if (s.st_size == 0) 444 s.st_size = RPC_FILE_MIN_SIZE; 445 446 blob_buf_init(&buf, 0); 447 448 if (tb[RPC_F_RB_BASE64]) 449 base64 = blobmsg_get_bool(tb[RPC_F_RB_BASE64]); 450 451 len = s.st_size + 1; 452 if (base64) 453 len = B64_ENCODE_LEN(s.st_size); 454 wbuf = blobmsg_alloc_string_buffer(&buf, "data", len); 455 456 if (!wbuf) 457 { 458 rv = UBUS_STATUS_UNKNOWN_ERROR; 459 goto out; 460 } 461 462 len = read(fd, wbuf, s.st_size); 463 464 /* read() returns 0 for an empty file (st_size 0), which must reply 465 * "data": "" rather than failing; only actual read errors (< 0) map 466 * to UBUS_STATUS_NO_DATA. */ 467 if (len < 0) 468 { 469 rv = UBUS_STATUS_NO_DATA; 470 goto out; 471 } 472 473 if (base64) 474 { 475 uint8_t *data = calloc(len, sizeof(uint8_t)); 476 if (!data) 477 { 478 rv = UBUS_STATUS_UNKNOWN_ERROR; 479 goto out; 480 } 481 memcpy(data, wbuf, len); 482 483 len = b64_encode(data, len, wbuf, B64_ENCODE_LEN(len)); 484 free(data); 485 if (len < 0) 486 { 487 rv = UBUS_STATUS_UNKNOWN_ERROR; 488 goto out; 489 } 490 } 491 492 *(wbuf + len) = '\0'; 493 blobmsg_add_string_buffer(&buf); 494 495 ubus_send_reply(ctx, req, buf.head); 496 rv = UBUS_STATUS_OK; 497 498 out: 499 blob_buf_free(&buf); 500 close(fd); 501 return rv; 502 } 503 504 static int 505 rpc_file_write(struct ubus_context *ctx, struct ubus_object *obj, 506 struct ubus_request_data *req, const char *method, 507 struct blob_attr *msg) 508 { 509 struct blob_attr **tb; 510 int append = O_TRUNC; 511 mode_t mode = 0666; 512 int fd, rv = 0; 513 char *path = NULL; 514 void *data = NULL; 515 ssize_t data_len = 0; 516 517 tb = rpc_check_path(msg, RW, "write", &path, NULL); 518 519 if (tb == NULL) 520 return rpc_errno_status(); 521 522 if (!tb[RPC_F_RW_DATA]) 523 return UBUS_STATUS_INVALID_ARGUMENT; 524 525 data = blobmsg_data(tb[RPC_F_RW_DATA]); 526 data_len = blobmsg_data_len(tb[RPC_F_RW_DATA]) - 1; 527 528 if (tb[RPC_F_RW_APPEND] && blobmsg_get_bool(tb[RPC_F_RW_APPEND])) 529 append = O_APPEND; 530 531 if (tb[RPC_F_RW_MODE]) 532 mode = blobmsg_get_u32(tb[RPC_F_RW_MODE]) & 0777; 533 534 fd = open(path, O_CREAT | O_WRONLY | append, mode); 535 if (fd < 0) 536 return rpc_errno_status(); 537 538 /* data_len can be 0 for an empty "data" string; skip the decode in that 539 * case since b64_decode() asserts on a zero destination size. */ 540 if (data_len > 0 && tb[RPC_F_RW_BASE64] && blobmsg_get_bool(tb[RPC_F_RW_BASE64])) 541 { 542 data_len = b64_decode(data, data, data_len); 543 if (data_len < 0) 544 { 545 rv = UBUS_STATUS_UNKNOWN_ERROR; 546 goto out; 547 } 548 } 549 550 if (write(fd, data, data_len) < 0) 551 rv = -1; 552 553 out: 554 if (fsync(fd) < 0) 555 rv = -1; 556 557 close(fd); 558 sync(); 559 560 if (rv) 561 return rpc_errno_status(); 562 563 return 0; 564 } 565 566 static int 567 rpc_file_md5(struct ubus_context *ctx, struct ubus_object *obj, 568 struct ubus_request_data *req, const char *method, 569 struct blob_attr *msg) 570 { 571 int rv, i; 572 char *path; 573 struct stat s; 574 uint8_t md5[16]; 575 char *wbuf; 576 577 if (!rpc_check_path(msg, R, "read", &path, &s)) 578 return rpc_errno_status(); 579 580 if (!S_ISREG(s.st_mode)) 581 return UBUS_STATUS_NOT_SUPPORTED; 582 583 if ((rv = md5sum(path, md5)) <= 0) 584 return rpc_errno_status(); 585 586 blob_buf_init(&buf, 0); 587 wbuf = blobmsg_alloc_string_buffer(&buf, "md5", 33); 588 589 for (i = 0; i < 16; i++) 590 sprintf(wbuf + (i * 2), "%02x", (uint8_t) md5[i]); 591 592 blobmsg_add_string_buffer(&buf); 593 ubus_send_reply(ctx, req, buf.head); 594 blob_buf_free(&buf); 595 596 return UBUS_STATUS_OK; 597 } 598 599 /* Add a string key only if value is non-NULL */ 600 static inline void 601 blobmsg_add_string_safe(struct blob_buf *buf, const char *key, const char *val) 602 { 603 if (val) 604 blobmsg_add_string(buf, key, val); 605 } 606 607 /* Look up username from UID */ 608 static const char * 609 look_up_username(uid_t uid) 610 { 611 struct passwd *pw = getpwuid(uid); 612 return pw ? pw->pw_name : NULL; 613 } 614 615 /* Look up group name from GID */ 616 static const char * 617 look_up_groupname(gid_t gid) 618 { 619 struct group *gr = getgrgid(gid); 620 return gr ? gr->gr_name : NULL; 621 } 622 623 static int 624 _get_stat_type(struct stat *s) 625 { 626 int type; 627 628 type = S_ISREG(s->st_mode) ? DT_REG : 629 S_ISDIR(s->st_mode) ? DT_DIR : 630 S_ISCHR(s->st_mode) ? DT_CHR : 631 S_ISBLK(s->st_mode) ? DT_BLK : 632 S_ISFIFO(s->st_mode) ? DT_FIFO : 633 S_ISLNK(s->st_mode) ? DT_LNK : 634 S_ISSOCK(s->st_mode) ? DT_SOCK : 635 DT_UNKNOWN; 636 return type; 637 } 638 639 static void 640 _rpc_file_add_stat(struct stat *s) 641 { 642 const char *user = look_up_username(s->st_uid); 643 const char *group = look_up_groupname(s->st_gid); 644 645 blobmsg_add_string(&buf, "type", d_types[_get_stat_type(s)]); 646 blobmsg_add_u64(&buf, "size", s->st_size); 647 blobmsg_add_u32(&buf, "mode", s->st_mode); 648 blobmsg_add_u32(&buf, "atime", s->st_atime); 649 blobmsg_add_u32(&buf, "mtime", s->st_mtime); 650 blobmsg_add_u32(&buf, "ctime", s->st_ctime); 651 blobmsg_add_u32(&buf, "inode", s->st_ino); 652 blobmsg_add_u32(&buf, "uid", s->st_uid); 653 blobmsg_add_u32(&buf, "gid", s->st_gid); 654 blobmsg_add_string_safe(&buf, "user", user); 655 blobmsg_add_string_safe(&buf, "group", group); 656 } 657 658 static int 659 rpc_file_list(struct ubus_context *ctx, struct ubus_object *obj, 660 struct ubus_request_data *req, const char *method, 661 struct blob_attr *msg) 662 { 663 DIR *fd; 664 void *c, *d; 665 struct stat s; 666 struct dirent *e; 667 char *path, *entrypath; 668 669 if (!rpc_check_path(msg, R, "list", &path, NULL)) 670 return rpc_errno_status(); 671 672 if ((fd = opendir(path)) == NULL) 673 return rpc_errno_status(); 674 675 blob_buf_init(&buf, 0); 676 c = blobmsg_open_array(&buf, "entries"); 677 678 while ((e = readdir(fd)) != NULL) 679 { 680 if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..")) 681 continue; 682 683 if (asprintf(&entrypath, "%s/%s", path, e->d_name) < 0) 684 continue; 685 686 // Use lstat to detect symlinks 687 if (!lstat(entrypath, &s)) 688 { 689 d = blobmsg_open_table(&buf, NULL); 690 blobmsg_add_string(&buf, "name", e->d_name); 691 _rpc_file_add_stat(&s); 692 693 // add target type only for symlinks 694 if (S_ISLNK(s.st_mode)) { 695 char tbuf[PATH_MAX + 1]; 696 ssize_t tlen; 697 void *t; 698 699 // open nested table "target" for symbolic link 700 t = blobmsg_open_table(&buf, "target"); 701 702 tlen = readlink(entrypath, tbuf, sizeof(tbuf) - 1); 703 if (tlen >= 0) { 704 tbuf[tlen] = '\0'; 705 blobmsg_add_string(&buf, "name", tbuf); 706 } 707 708 struct stat target; 709 if (!stat(entrypath, &target)) { 710 _rpc_file_add_stat(&target); 711 } else { 712 blobmsg_add_string(&buf, "type", "broken"); 713 } 714 blobmsg_close_table(&buf, t); 715 } 716 blobmsg_close_table(&buf, d); 717 } 718 719 free(entrypath); 720 } 721 722 closedir(fd); 723 724 blobmsg_close_array(&buf, c); 725 ubus_send_reply(ctx, req, buf.head); 726 blob_buf_free(&buf); 727 728 return 0; 729 } 730 731 static int 732 rpc_file_stat(struct ubus_context *ctx, struct ubus_object *obj, 733 struct ubus_request_data *req, const char *method, 734 struct blob_attr *msg) 735 { 736 char *path; 737 struct stat s; 738 739 if (!rpc_check_path(msg, R, "list", &path, &s)) 740 return rpc_errno_status(); 741 742 blob_buf_init(&buf, 0); 743 744 blobmsg_add_string(&buf, "path", path); 745 _rpc_file_add_stat(&s); 746 747 ubus_send_reply(ctx, req, buf.head); 748 blob_buf_free(&buf); 749 750 return 0; 751 } 752 753 static int 754 rpc_file_lstat(struct ubus_context *ctx, struct ubus_object *obj, 755 struct ubus_request_data *req, const char *method, 756 struct blob_attr *msg) 757 { 758 char *path; 759 struct stat s; 760 761 if (!rpc_check_path_with_lstat(msg, R, "list", &path, &s, true)) 762 return rpc_errno_status(); 763 764 blob_buf_init(&buf, 0); 765 766 blobmsg_add_string(&buf, "path", path); 767 _rpc_file_add_stat(&s); 768 769 ubus_send_reply(ctx, req, buf.head); 770 blob_buf_free(&buf); 771 772 return 0; 773 } 774 775 static int 776 rpc_file_remove_recursive(const struct blob_attr *sid, const char *path); 777 778 static int 779 rpc_file_remove_recursive(const struct blob_attr *sid, const char *path) 780 { 781 DIR *fd; 782 int err = 0; 783 struct stat s; 784 struct dirent *e; 785 char *entrypath; 786 787 if ((fd = opendir(path)) == NULL) 788 return rpc_errno_status(); 789 790 for (e = readdir(fd); e != NULL && err == 0; e = readdir(fd)) 791 { 792 if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..")) 793 continue; 794 795 if (asprintf(&entrypath, "%s/%s", path, e->d_name) >= 0) 796 { 797 if (!rpc_file_access(sid, entrypath, "write")) 798 { 799 err = UBUS_STATUS_PERMISSION_DENIED; 800 } 801 else if (!lstat(entrypath, &s)) 802 { 803 if (S_ISDIR(s.st_mode)) 804 err = rpc_file_remove_recursive(sid, entrypath); 805 else if (unlink(entrypath)) 806 err = rpc_errno_status(); 807 } 808 809 free(entrypath); 810 } 811 else 812 { 813 err = UBUS_STATUS_UNKNOWN_ERROR; 814 } 815 } 816 817 closedir(fd); 818 819 if (!err && rmdir(path)) 820 return rpc_errno_status(); 821 822 return err; 823 } 824 825 static int 826 rpc_file_remove(struct ubus_context *ctx, struct ubus_object *obj, 827 struct ubus_request_data *req, const char *method, 828 struct blob_attr *msg) 829 { 830 struct blob_attr **tb; 831 struct stat s; 832 char *path = NULL; 833 834 tb = rpc_check_path_with_lstat(msg, R, "write", &path, NULL, false); 835 836 if (tb == NULL) 837 return rpc_errno_status(); 838 839 if (lstat(path, &s)) 840 return rpc_errno_status(); 841 842 if (S_ISDIR(s.st_mode)) 843 return rpc_file_remove_recursive(tb[RPC_F_R_SESSION], path); 844 845 if (unlink(path)) 846 return rpc_errno_status(); 847 848 return 0; 849 } 850 851 static const char * 852 rpc_file_exec_lookup(const char *cmd) 853 { 854 struct stat s; 855 int plen = 0, clen = strlen(cmd) + 1; 856 char *search, *p; 857 static char path[PATH_MAX]; 858 859 if (!stat(cmd, &s) && S_ISREG(s.st_mode)) 860 return cmd; 861 862 search = getenv("PATH"); 863 864 if (!search) 865 search = "/bin:/usr/bin:/sbin:/usr/sbin"; 866 867 p = search; 868 869 do 870 { 871 if (*p != ':' && *p != '\0') 872 continue; 873 874 plen = p - search; 875 876 if ((plen + clen) >= sizeof(path)) 877 continue; 878 879 strncpy(path, search, plen); 880 sprintf(path + plen, "/%s", cmd); 881 882 if (!stat(path, &s) && S_ISREG(s.st_mode)) 883 return path; 884 885 search = p + 1; 886 } 887 while (*p++); 888 889 return NULL; 890 } 891 892 893 static void 894 rpc_ustream_to_blobmsg(struct ustream *s, const char *name) 895 { 896 int len; 897 char *rbuf, *wbuf; 898 899 if ((len = ustream_pending_data(s, false)) > 0) 900 { 901 wbuf = blobmsg_alloc_string_buffer(&buf, name, len + 1); 902 903 if (!wbuf) 904 return; 905 906 ustream_for_each_read_buffer(s, rbuf, len) 907 { 908 memcpy(wbuf, rbuf, len); 909 wbuf += len; 910 } 911 912 *wbuf = 0; 913 blobmsg_add_string_buffer(&buf); 914 } 915 } 916 917 static void 918 rpc_file_exec_reply(struct rpc_file_exec_context *c, int rv) 919 { 920 uloop_timeout_cancel(&c->timeout); 921 uloop_process_delete(&c->process); 922 923 if (rv == UBUS_STATUS_OK) 924 { 925 blob_buf_init(&buf, 0); 926 927 blobmsg_add_u32(&buf, "code", WEXITSTATUS(c->stat)); 928 929 rpc_ustream_to_blobmsg(&c->opipe.stream, "stdout"); 930 rpc_ustream_to_blobmsg(&c->epipe.stream, "stderr"); 931 932 ubus_send_reply(c->context, &c->request, buf.head); 933 blob_buf_free(&buf); 934 } 935 936 ubus_complete_deferred_request(c->context, &c->request, rv); 937 938 ustream_free(&c->opipe.stream); 939 ustream_free(&c->epipe.stream); 940 941 close(c->opipe.fd.fd); 942 close(c->epipe.fd.fd); 943 944 free(c); 945 } 946 947 static void 948 rpc_file_exec_reply_cb(struct uloop_timeout *t) 949 { 950 struct rpc_file_exec_context *c = 951 container_of(t, struct rpc_file_exec_context, timeout); 952 953 rpc_file_exec_reply(c, c->deferred_status); 954 } 955 956 static void 957 rpc_file_exec_schedule_reply(struct rpc_file_exec_context *c, int rv) 958 { 959 c->deferred_status = rv; 960 c->timeout.cb = rpc_file_exec_reply_cb; 961 uloop_timeout_set(&c->timeout, 0); 962 } 963 964 static void 965 rpc_file_exec_timeout_cb(struct uloop_timeout *t) 966 { 967 struct rpc_file_exec_context *c = 968 container_of(t, struct rpc_file_exec_context, timeout); 969 970 kill(c->process.pid, SIGKILL); 971 rpc_file_exec_schedule_reply(c, UBUS_STATUS_TIMEOUT); 972 } 973 974 static void 975 rpc_file_exec_process_cb(struct uloop_process *p, int stat) 976 { 977 struct rpc_file_exec_context *c = 978 container_of(p, struct rpc_file_exec_context, process); 979 980 c->stat = stat; 981 982 ustream_poll(&c->opipe.stream); 983 ustream_poll(&c->epipe.stream); 984 } 985 986 static void 987 rpc_file_exec_opipe_read_cb(struct ustream *s, int bytes) 988 { 989 struct rpc_file_exec_context *c = 990 container_of(s, struct rpc_file_exec_context, opipe.stream); 991 992 if (ustream_read_buf_full(s)) 993 rpc_file_exec_schedule_reply(c, UBUS_STATUS_NOT_SUPPORTED); 994 } 995 996 static void 997 rpc_file_exec_epipe_read_cb(struct ustream *s, int bytes) 998 { 999 struct rpc_file_exec_context *c = 1000 container_of(s, struct rpc_file_exec_context, epipe.stream); 1001 1002 if (ustream_read_buf_full(s)) 1003 rpc_file_exec_schedule_reply(c, UBUS_STATUS_NOT_SUPPORTED); 1004 } 1005 1006 static void 1007 rpc_file_exec_opipe_state_cb(struct ustream *s) 1008 { 1009 struct rpc_file_exec_context *c = 1010 container_of(s, struct rpc_file_exec_context, opipe.stream); 1011 1012 if (c->opipe.stream.eof && c->epipe.stream.eof) 1013 rpc_file_exec_schedule_reply(c, UBUS_STATUS_OK); 1014 } 1015 1016 static void 1017 rpc_file_exec_epipe_state_cb(struct ustream *s) 1018 { 1019 struct rpc_file_exec_context *c = 1020 container_of(s, struct rpc_file_exec_context, epipe.stream); 1021 1022 if (c->opipe.stream.eof && c->epipe.stream.eof) 1023 rpc_file_exec_schedule_reply(c, UBUS_STATUS_OK); 1024 } 1025 1026 static void 1027 rpc_fdclose(int fd) 1028 { 1029 if (fd > 2) 1030 close(fd); 1031 } 1032 1033 static int 1034 rpc_file_exec_run(const char *cmd, const struct blob_attr *sid, 1035 const struct blob_attr *arg, const struct blob_attr *env, 1036 struct ubus_context *ctx, struct ubus_request_data *req) 1037 { 1038 pid_t pid; 1039 1040 int devnull; 1041 int opipe[2]; 1042 int epipe[2]; 1043 1044 int rem; 1045 struct blob_attr *cur; 1046 1047 uint8_t arglen; 1048 char *executable, **args, **tmp, *p; 1049 1050 struct rpc_file_exec_context *c; 1051 1052 if (sid && env) 1053 return UBUS_STATUS_PERMISSION_DENIED; 1054 1055 cmd = rpc_file_exec_lookup(cmd); 1056 1057 if (!cmd) 1058 return UBUS_STATUS_NOT_FOUND; 1059 1060 executable = rpc_canonicalize_path(cmd); 1061 1062 if (executable == NULL) 1063 return UBUS_STATUS_UNKNOWN_ERROR; 1064 1065 if (!rpc_file_access(sid, executable, "exec")) 1066 { 1067 if (arg == NULL || strlen(executable) >= sizeof(cmdstr)) 1068 return UBUS_STATUS_PERMISSION_DENIED; 1069 1070 arglen = 2; 1071 p = cmdstr + sprintf(cmdstr, "%s", executable); 1072 1073 blobmsg_for_each_attr(cur, arg, rem) 1074 { 1075 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) 1076 continue; 1077 1078 if (arglen == 255 || 1079 p + blobmsg_data_len(cur) >= cmdstr + sizeof(cmdstr)) 1080 return UBUS_STATUS_PERMISSION_DENIED; 1081 1082 p += sprintf(p, " %s", blobmsg_get_string(cur)); 1083 arglen++; 1084 } 1085 1086 if (!rpc_file_access(sid, cmdstr, "exec")) 1087 return UBUS_STATUS_PERMISSION_DENIED; 1088 } 1089 1090 c = malloc(sizeof(*c)); 1091 1092 if (!c) 1093 return UBUS_STATUS_UNKNOWN_ERROR; 1094 1095 if (pipe(opipe)) 1096 goto fail_opipe; 1097 1098 if (pipe(epipe)) 1099 goto fail_epipe; 1100 1101 switch ((pid = fork())) 1102 { 1103 case -1: 1104 goto fail_fork; 1105 1106 case 0: 1107 uloop_done(); 1108 1109 devnull = open("/dev/null", O_RDWR); 1110 1111 if (devnull == -1) 1112 _exit(127); 1113 1114 dup2(devnull, 0); 1115 dup2(opipe[1], 1); 1116 dup2(epipe[1], 2); 1117 1118 rpc_fdclose(devnull); 1119 rpc_fdclose(opipe[0]); 1120 rpc_fdclose(opipe[1]); 1121 rpc_fdclose(epipe[0]); 1122 rpc_fdclose(epipe[1]); 1123 1124 arglen = 2; 1125 args = malloc(sizeof(char *) * arglen); 1126 1127 if (!args) 1128 _exit(127); 1129 1130 args[0] = (char *)executable; 1131 args[1] = NULL; 1132 1133 if (arg) 1134 { 1135 blobmsg_for_each_attr(cur, arg, rem) 1136 { 1137 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) 1138 continue; 1139 1140 if (arglen == 255) 1141 { 1142 free(args); 1143 _exit(127); 1144 } 1145 1146 arglen++; 1147 tmp = realloc(args, sizeof(char *) * arglen); 1148 1149 if (!tmp) 1150 { 1151 free(args); 1152 _exit(127); 1153 } 1154 1155 args = tmp; 1156 args[arglen-2] = blobmsg_data(cur); 1157 args[arglen-1] = NULL; 1158 } 1159 } 1160 1161 if (env) 1162 { 1163 blobmsg_for_each_attr(cur, env, rem) 1164 { 1165 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) 1166 continue; 1167 1168 setenv(blobmsg_name(cur), blobmsg_data(cur), 1); 1169 } 1170 } 1171 1172 if (execv(executable, args)) 1173 _exit(127); 1174 1175 default: 1176 memset(c, 0, sizeof(*c)); 1177 1178 ustream_declare(c->opipe, opipe[0], exec_opipe); 1179 ustream_declare(c->epipe, epipe[0], exec_epipe); 1180 1181 c->process.pid = pid; 1182 c->process.cb = rpc_file_exec_process_cb; 1183 uloop_process_add(&c->process); 1184 1185 c->timeout.cb = rpc_file_exec_timeout_cb; 1186 uloop_timeout_set(&c->timeout, *ops->exec_timeout); 1187 1188 close(opipe[1]); 1189 close(epipe[1]); 1190 1191 c->context = ctx; 1192 ubus_defer_request(ctx, req, &c->request); 1193 } 1194 1195 return UBUS_STATUS_OK; 1196 1197 fail_fork: 1198 close(epipe[0]); 1199 close(epipe[1]); 1200 1201 fail_epipe: 1202 close(opipe[0]); 1203 close(opipe[1]); 1204 1205 fail_opipe: 1206 free(c); 1207 return rpc_errno_status(); 1208 } 1209 1210 static int 1211 rpc_file_exec(struct ubus_context *ctx, struct ubus_object *obj, 1212 struct ubus_request_data *req, const char *method, 1213 struct blob_attr *msg) 1214 { 1215 struct blob_attr *tb[__RPC_E_MAX]; 1216 1217 blobmsg_parse(rpc_exec_policy, __RPC_E_MAX, tb, 1218 blob_data(msg), blob_len(msg)); 1219 1220 if (!tb[RPC_E_CMD]) 1221 return UBUS_STATUS_INVALID_ARGUMENT; 1222 1223 return rpc_file_exec_run(blobmsg_data(tb[RPC_E_CMD]), tb[RPC_E_SESSION], 1224 tb[RPC_E_PARM], tb[RPC_E_ENV], ctx, req); 1225 } 1226 1227 1228 static int 1229 rpc_file_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx) 1230 { 1231 static const struct ubus_method file_methods[] = { 1232 UBUS_METHOD("read", rpc_file_read, rpc_file_RB_policy), 1233 UBUS_METHOD("write", rpc_file_write, rpc_file_RW_policy), 1234 UBUS_METHOD("list", rpc_file_list, rpc_file_R_policy), 1235 UBUS_METHOD("lstat", rpc_file_lstat, rpc_file_R_policy), 1236 UBUS_METHOD("stat", rpc_file_stat, rpc_file_R_policy), 1237 UBUS_METHOD("md5", rpc_file_md5, rpc_file_R_policy), 1238 UBUS_METHOD("remove", rpc_file_remove, rpc_file_R_policy), 1239 UBUS_METHOD("exec", rpc_file_exec, rpc_exec_policy), 1240 }; 1241 1242 static struct ubus_object_type file_type = 1243 UBUS_OBJECT_TYPE("rpcd-plugin-file", file_methods); 1244 1245 static struct ubus_object obj = { 1246 .name = "file", 1247 .type = &file_type, 1248 .methods = file_methods, 1249 .n_methods = ARRAY_SIZE(file_methods), 1250 }; 1251 1252 ops = o; 1253 1254 return ubus_add_object(ctx, &obj); 1255 } 1256 1257 struct rpc_plugin rpc_plugin = { 1258 .init = rpc_file_api_init 1259 }; 1260
This page was automatically generated by LXR 0.3.1. • OpenWrt