file.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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. #define _GNU_SOURCE
  20. #include <fcntl.h>
  21. #include <errno.h>
  22. #include <unistd.h>
  23. #include <stdint.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <limits.h>
  27. #include <dirent.h>
  28. #include <sys/stat.h>
  29. #include <sys/wait.h>
  30. #include <libubus.h>
  31. #include <libubox/blobmsg.h>
  32. #include <libubox/md5.h>
  33. #include <libubox/ustream.h>
  34. #include <libubox/utils.h>
  35. #include <rpcd/plugin.h>
  36. /* limit of sys & proc files */
  37. #define RPC_FILE_MIN_SIZE (4096)
  38. /* limit of regular files and command output data */
  39. #define RPC_FILE_MAX_SIZE (4096 * 64)
  40. /* limit of command line length for exec acl checks */
  41. #define RPC_CMDLINE_MAX_SIZE (1024)
  42. #define ustream_for_each_read_buffer(stream, ptr, len) \
  43. for (ptr = ustream_get_read_buf(stream, &len); \
  44. ptr != NULL && len > 0; \
  45. ustream_consume(stream, len), ptr = ustream_get_read_buf(stream, &len))
  46. #define ustream_declare(us, fd, name) \
  47. us.stream.string_data = true; \
  48. us.stream.r.buffer_len = 4096; \
  49. us.stream.r.max_buffers = RPC_FILE_MAX_SIZE / 4096; \
  50. us.stream.notify_read = rpc_file_##name##_read_cb; \
  51. us.stream.notify_state = rpc_file_##name##_state_cb; \
  52. ustream_fd_init(&us, fd);
  53. static const struct rpc_daemon_ops *ops;
  54. struct rpc_file_exec_context {
  55. struct ubus_context *context;
  56. struct ubus_request_data request;
  57. struct uloop_timeout timeout;
  58. struct uloop_process process;
  59. struct ustream_fd opipe;
  60. struct ustream_fd epipe;
  61. int stat;
  62. };
  63. static struct blob_buf buf;
  64. static char *canonpath;
  65. static char cmdstr[RPC_CMDLINE_MAX_SIZE];
  66. enum {
  67. RPC_F_R_PATH,
  68. RPC_F_R_SESSION,
  69. __RPC_F_R_MAX,
  70. };
  71. static const struct blobmsg_policy rpc_file_R_policy[__RPC_F_R_MAX] = {
  72. [RPC_F_R_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
  73. [RPC_F_R_SESSION] = { .name = "ubus_rpc_session",
  74. .type = BLOBMSG_TYPE_STRING },
  75. };
  76. enum {
  77. RPC_F_RB_PATH,
  78. RPC_F_RB_BASE64,
  79. RPC_F_RB_SESSION,
  80. __RPC_F_RB_MAX,
  81. };
  82. static const struct blobmsg_policy rpc_file_RB_policy[__RPC_F_RB_MAX] = {
  83. [RPC_F_RB_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
  84. [RPC_F_RB_BASE64] = { .name = "base64", .type = BLOBMSG_TYPE_BOOL },
  85. [RPC_F_RB_SESSION] = { .name = "ubus_rpc_session",
  86. .type = BLOBMSG_TYPE_STRING },
  87. };
  88. enum {
  89. RPC_F_RW_PATH,
  90. RPC_F_RW_DATA,
  91. RPC_F_RW_APPEND,
  92. RPC_F_RW_MODE,
  93. RPC_F_RW_BASE64,
  94. RPC_F_RW_SESSION,
  95. __RPC_F_RW_MAX,
  96. };
  97. static const struct blobmsg_policy rpc_file_RW_policy[__RPC_F_RW_MAX] = {
  98. [RPC_F_RW_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
  99. [RPC_F_RW_DATA] = { .name = "data", .type = BLOBMSG_TYPE_STRING },
  100. [RPC_F_RW_APPEND] = { .name = "append", .type = BLOBMSG_TYPE_BOOL },
  101. [RPC_F_RW_MODE] = { .name = "mode", .type = BLOBMSG_TYPE_INT32 },
  102. [RPC_F_RW_BASE64] = { .name = "base64", .type = BLOBMSG_TYPE_BOOL },
  103. [RPC_F_RW_SESSION] = { .name = "ubus_rpc_session",
  104. .type = BLOBMSG_TYPE_STRING },
  105. };
  106. enum {
  107. RPC_E_CMD,
  108. RPC_E_PARM,
  109. RPC_E_ENV,
  110. RPC_E_SESSION,
  111. __RPC_E_MAX,
  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. [RPC_E_SESSION] = { .name = "ubus_rpc_session",
  118. .type = BLOBMSG_TYPE_STRING },
  119. };
  120. static const char *d_types[] = {
  121. [DT_BLK] = "block",
  122. [DT_CHR] = "char",
  123. [DT_DIR] = "directory",
  124. [DT_FIFO] = "fifo",
  125. [DT_LNK] = "symlink",
  126. [DT_REG] = "file",
  127. [DT_SOCK] = "socket",
  128. [DT_UNKNOWN] = "unknown",
  129. };
  130. static int
  131. rpc_errno_status(void)
  132. {
  133. switch (errno)
  134. {
  135. case EACCES:
  136. return UBUS_STATUS_PERMISSION_DENIED;
  137. case ENOTDIR:
  138. return UBUS_STATUS_INVALID_ARGUMENT;
  139. case ENOENT:
  140. return UBUS_STATUS_NOT_FOUND;
  141. case EINVAL:
  142. return UBUS_STATUS_INVALID_ARGUMENT;
  143. default:
  144. return UBUS_STATUS_UNKNOWN_ERROR;
  145. }
  146. }
  147. static bool
  148. rpc_file_access(const struct blob_attr *sid,
  149. const char *path, const char *perm)
  150. {
  151. if (!sid)
  152. return true;
  153. return ops->session_access(blobmsg_data(sid), "file", path, perm);
  154. }
  155. static char *
  156. rpc_canonicalize_path(const char *path)
  157. {
  158. char *cp;
  159. const char *p;
  160. if (path == NULL || *path == '\0')
  161. return NULL;
  162. if (canonpath != NULL)
  163. free(canonpath);
  164. canonpath = strdup(path);
  165. if (canonpath == NULL)
  166. return NULL;
  167. /* normalize */
  168. for (cp = canonpath, p = path; *p != '\0'; ) {
  169. if (*p != '/')
  170. goto next;
  171. /* skip repeating / */
  172. if (p[1] == '/') {
  173. p++;
  174. continue;
  175. }
  176. /* /./ or /../ */
  177. if (p[1] == '.') {
  178. /* skip /./ */
  179. if ((p[2] == '\0') || (p[2] == '/')) {
  180. p += 2;
  181. continue;
  182. }
  183. /* collapse /x/../ */
  184. if ((p[2] == '.') && ((p[3] == '\0') || (p[3] == '/'))) {
  185. while ((cp > canonpath) && (*--cp != '/'))
  186. ;
  187. p += 3;
  188. continue;
  189. }
  190. }
  191. next:
  192. *cp++ = *p++;
  193. }
  194. /* remove trailing slash if not root / */
  195. if ((cp > canonpath + 1) && (cp[-1] == '/'))
  196. cp--;
  197. else if (cp == canonpath)
  198. *cp++ = '/';
  199. *cp = '\0';
  200. return canonpath;
  201. }
  202. static struct blob_attr **
  203. __rpc_check_path(const struct blobmsg_policy *policy, size_t policy_len,
  204. int policy_path_idx, int policy_sid_idx, const char *perm,
  205. struct blob_attr *msg, char **path, struct stat *s)
  206. {
  207. static struct blob_attr *tb[__RPC_F_RW_MAX]; /* largest _MAX constant */
  208. blobmsg_parse(policy, policy_len, tb, blob_data(msg), blob_len(msg));
  209. if (!tb[policy_path_idx])
  210. {
  211. errno = EINVAL;
  212. return NULL;
  213. }
  214. *path = rpc_canonicalize_path(blobmsg_get_string(tb[policy_path_idx]));
  215. if (*path == NULL)
  216. {
  217. errno = ENOMEM;
  218. return NULL;
  219. }
  220. if (!rpc_file_access(tb[policy_sid_idx], *path, perm))
  221. {
  222. errno = EACCES;
  223. return NULL;
  224. }
  225. if (s != NULL && stat(*path, s) != 0)
  226. return NULL;
  227. return tb;
  228. }
  229. #define rpc_check_path(msg, policy_selector, perm, path, s) \
  230. __rpc_check_path(rpc_file_ ## policy_selector ## _policy, \
  231. ARRAY_SIZE(rpc_file_ ## policy_selector ## _policy), \
  232. RPC_F_ ## policy_selector ## _PATH, \
  233. RPC_F_ ## policy_selector ## _SESSION, \
  234. perm, msg, path, s)
  235. static int
  236. rpc_file_read(struct ubus_context *ctx, struct ubus_object *obj,
  237. struct ubus_request_data *req, const char *method,
  238. struct blob_attr *msg)
  239. {
  240. struct blob_attr **tb;
  241. bool base64 = false;
  242. int fd, rv;
  243. ssize_t len;
  244. char *path;
  245. struct stat s;
  246. char *wbuf;
  247. tb = rpc_check_path(msg, RB, "read", &path, &s);
  248. if (tb == NULL)
  249. return rpc_errno_status();
  250. if (s.st_size >= RPC_FILE_MAX_SIZE)
  251. return UBUS_STATUS_NOT_SUPPORTED;
  252. if ((fd = open(path, O_RDONLY)) < 0)
  253. return rpc_errno_status();
  254. /* some sysfs files do not report a length */
  255. if (s.st_size == 0)
  256. s.st_size = RPC_FILE_MIN_SIZE;
  257. blob_buf_init(&buf, 0);
  258. if (tb[RPC_F_RB_BASE64])
  259. base64 = blobmsg_get_bool(tb[RPC_F_RB_BASE64]);
  260. len = s.st_size + 1;
  261. if (base64)
  262. len = B64_ENCODE_LEN(s.st_size);
  263. wbuf = blobmsg_alloc_string_buffer(&buf, "data", len);
  264. if (!wbuf)
  265. {
  266. rv = UBUS_STATUS_UNKNOWN_ERROR;
  267. goto out;
  268. }
  269. if ((len = read(fd, wbuf, s.st_size)) <= 0)
  270. {
  271. rv = UBUS_STATUS_NO_DATA;
  272. goto out;
  273. }
  274. if (base64)
  275. {
  276. uint8_t *data = calloc(len, sizeof(uint8_t));
  277. if (!data)
  278. {
  279. rv = UBUS_STATUS_UNKNOWN_ERROR;
  280. goto out;
  281. }
  282. memcpy(data, wbuf, len);
  283. len = b64_encode(data, len, wbuf, B64_ENCODE_LEN(len));
  284. free(data);
  285. if (len < 0)
  286. {
  287. rv = UBUS_STATUS_UNKNOWN_ERROR;
  288. goto out;
  289. }
  290. }
  291. *(wbuf + len) = '\0';
  292. blobmsg_add_string_buffer(&buf);
  293. ubus_send_reply(ctx, req, buf.head);
  294. rv = UBUS_STATUS_OK;
  295. out:
  296. blob_buf_free(&buf);
  297. close(fd);
  298. return rv;
  299. }
  300. static int
  301. rpc_file_write(struct ubus_context *ctx, struct ubus_object *obj,
  302. struct ubus_request_data *req, const char *method,
  303. struct blob_attr *msg)
  304. {
  305. struct blob_attr **tb;
  306. int append = O_TRUNC;
  307. mode_t prev_mode, mode = 0666;
  308. int fd, rv = 0;
  309. char *path = NULL;
  310. void *data = NULL;
  311. ssize_t data_len = 0;
  312. tb = rpc_check_path(msg, RW, "write", &path, NULL);
  313. if (tb == NULL)
  314. return rpc_errno_status();
  315. if (!tb[RPC_F_RW_DATA])
  316. return UBUS_STATUS_INVALID_ARGUMENT;
  317. data = blobmsg_data(tb[RPC_F_RW_DATA]);
  318. data_len = blobmsg_data_len(tb[RPC_F_RW_DATA]) - 1;
  319. if (tb[RPC_F_RW_APPEND] && blobmsg_get_bool(tb[RPC_F_RW_APPEND]))
  320. append = O_APPEND;
  321. if (tb[RPC_F_RW_MODE])
  322. mode = blobmsg_get_u32(tb[RPC_F_RW_MODE]);
  323. prev_mode = umask(0);
  324. fd = open(path, O_CREAT | O_WRONLY | append, mode);
  325. umask(prev_mode);
  326. if (fd < 0)
  327. return rpc_errno_status();
  328. if (tb[RPC_F_RW_BASE64] && blobmsg_get_bool(tb[RPC_F_RW_BASE64]))
  329. {
  330. data_len = b64_decode(data, data, data_len);
  331. if (data_len < 0)
  332. {
  333. rv = UBUS_STATUS_UNKNOWN_ERROR;
  334. goto out;
  335. }
  336. }
  337. if (write(fd, data, data_len) < 0)
  338. rv = -1;
  339. out:
  340. if (fsync(fd) < 0)
  341. rv = -1;
  342. close(fd);
  343. sync();
  344. if (rv)
  345. return rpc_errno_status();
  346. return 0;
  347. }
  348. static int
  349. rpc_file_md5(struct ubus_context *ctx, struct ubus_object *obj,
  350. struct ubus_request_data *req, const char *method,
  351. struct blob_attr *msg)
  352. {
  353. int rv, i;
  354. char *path;
  355. struct stat s;
  356. uint8_t md5[16];
  357. char *wbuf;
  358. if (!rpc_check_path(msg, R, "read", &path, &s))
  359. return rpc_errno_status();
  360. if (!S_ISREG(s.st_mode))
  361. return UBUS_STATUS_NOT_SUPPORTED;
  362. if ((rv = md5sum(path, md5)) <= 0)
  363. return rpc_errno_status();
  364. blob_buf_init(&buf, 0);
  365. wbuf = blobmsg_alloc_string_buffer(&buf, "md5", 33);
  366. for (i = 0; i < 16; i++)
  367. sprintf(wbuf + (i * 2), "%02x", (uint8_t) md5[i]);
  368. blobmsg_add_string_buffer(&buf);
  369. ubus_send_reply(ctx, req, buf.head);
  370. blob_buf_free(&buf);
  371. return UBUS_STATUS_OK;
  372. }
  373. static void
  374. _rpc_file_add_stat(struct stat *s)
  375. {
  376. int type;
  377. type = S_ISREG(s->st_mode) ? DT_REG :
  378. S_ISDIR(s->st_mode) ? DT_DIR :
  379. S_ISCHR(s->st_mode) ? DT_CHR :
  380. S_ISBLK(s->st_mode) ? DT_BLK :
  381. S_ISFIFO(s->st_mode) ? DT_FIFO :
  382. S_ISLNK(s->st_mode) ? DT_LNK :
  383. S_ISSOCK(s->st_mode) ? DT_SOCK :
  384. DT_UNKNOWN;
  385. blobmsg_add_string(&buf, "type", d_types[type]);
  386. blobmsg_add_u32(&buf, "size", s->st_size);
  387. blobmsg_add_u32(&buf, "mode", s->st_mode);
  388. blobmsg_add_u32(&buf, "atime", s->st_atime);
  389. blobmsg_add_u32(&buf, "mtime", s->st_mtime);
  390. blobmsg_add_u32(&buf, "ctime", s->st_ctime);
  391. blobmsg_add_u32(&buf, "inode", s->st_ino);
  392. blobmsg_add_u32(&buf, "uid", s->st_uid);
  393. blobmsg_add_u32(&buf, "gid", s->st_gid);
  394. }
  395. static int
  396. rpc_file_list(struct ubus_context *ctx, struct ubus_object *obj,
  397. struct ubus_request_data *req, const char *method,
  398. struct blob_attr *msg)
  399. {
  400. DIR *fd;
  401. void *c, *d;
  402. struct stat s;
  403. struct dirent *e;
  404. char *path, *entrypath;
  405. if (!rpc_check_path(msg, R, "list", &path, NULL))
  406. return rpc_errno_status();
  407. if ((fd = opendir(path)) == NULL)
  408. return rpc_errno_status();
  409. blob_buf_init(&buf, 0);
  410. c = blobmsg_open_array(&buf, "entries");
  411. while ((e = readdir(fd)) != NULL)
  412. {
  413. if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, ".."))
  414. continue;
  415. if (asprintf(&entrypath, "%s/%s", path, e->d_name) < 0)
  416. continue;
  417. if (!stat(entrypath, &s))
  418. {
  419. d = blobmsg_open_table(&buf, NULL);
  420. blobmsg_add_string(&buf, "name", e->d_name);
  421. _rpc_file_add_stat(&s);
  422. blobmsg_close_table(&buf, d);
  423. }
  424. free(entrypath);
  425. }
  426. closedir(fd);
  427. blobmsg_close_array(&buf, c);
  428. ubus_send_reply(ctx, req, buf.head);
  429. blob_buf_free(&buf);
  430. return 0;
  431. }
  432. static int
  433. rpc_file_stat(struct ubus_context *ctx, struct ubus_object *obj,
  434. struct ubus_request_data *req, const char *method,
  435. struct blob_attr *msg)
  436. {
  437. char *path;
  438. struct stat s;
  439. if (!rpc_check_path(msg, R, "list", &path, &s))
  440. return rpc_errno_status();
  441. blob_buf_init(&buf, 0);
  442. blobmsg_add_string(&buf, "path", path);
  443. _rpc_file_add_stat(&s);
  444. ubus_send_reply(ctx, req, buf.head);
  445. blob_buf_free(&buf);
  446. return 0;
  447. }
  448. static int
  449. rpc_file_remove_recursive(const char *path);
  450. static int
  451. rpc_file_remove_recursive(const char *path)
  452. {
  453. DIR *fd;
  454. int err = 0;
  455. struct stat s;
  456. struct dirent *e;
  457. char *entrypath;
  458. if ((fd = opendir(path)) == NULL)
  459. return rpc_errno_status();
  460. for (e = readdir(fd); e != NULL && err == 0; e = readdir(fd))
  461. {
  462. if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, ".."))
  463. continue;
  464. if (asprintf(&entrypath, "%s/%s", path, e->d_name) >= 0)
  465. {
  466. if (!lstat(entrypath, &s))
  467. {
  468. if (S_ISDIR(s.st_mode))
  469. err = rpc_file_remove_recursive(entrypath);
  470. else if (unlink(entrypath))
  471. err = rpc_errno_status();
  472. }
  473. free(entrypath);
  474. }
  475. else
  476. {
  477. err = UBUS_STATUS_UNKNOWN_ERROR;
  478. }
  479. }
  480. closedir(fd);
  481. if (!err && rmdir(path))
  482. return rpc_errno_status();
  483. return err;
  484. }
  485. static int
  486. rpc_file_remove(struct ubus_context *ctx, struct ubus_object *obj,
  487. struct ubus_request_data *req, const char *method,
  488. struct blob_attr *msg)
  489. {
  490. struct stat s;
  491. char *path = NULL;
  492. if (!rpc_check_path(msg, R, "write", &path, NULL))
  493. return rpc_errno_status();
  494. if (lstat(path, &s))
  495. return rpc_errno_status();
  496. if (S_ISDIR(s.st_mode))
  497. return rpc_file_remove_recursive(path);
  498. if (unlink(path))
  499. return rpc_errno_status();
  500. return 0;
  501. }
  502. static const char *
  503. rpc_file_exec_lookup(const char *cmd)
  504. {
  505. struct stat s;
  506. int plen = 0, clen = strlen(cmd) + 1;
  507. char *search, *p;
  508. static char path[PATH_MAX];
  509. if (!stat(cmd, &s) && S_ISREG(s.st_mode))
  510. return cmd;
  511. search = getenv("PATH");
  512. if (!search)
  513. search = "/bin:/usr/bin:/sbin:/usr/sbin";
  514. p = search;
  515. do
  516. {
  517. if (*p != ':' && *p != '\0')
  518. continue;
  519. plen = p - search;
  520. if ((plen + clen) >= sizeof(path))
  521. continue;
  522. strncpy(path, search, plen);
  523. sprintf(path + plen, "/%s", cmd);
  524. if (!stat(path, &s) && S_ISREG(s.st_mode))
  525. return path;
  526. search = p + 1;
  527. }
  528. while (*p++);
  529. return NULL;
  530. }
  531. static void
  532. rpc_ustream_to_blobmsg(struct ustream *s, const char *name)
  533. {
  534. int len;
  535. char *rbuf, *wbuf;
  536. if ((len = ustream_pending_data(s, false)) > 0)
  537. {
  538. wbuf = blobmsg_alloc_string_buffer(&buf, name, len + 1);
  539. if (!wbuf)
  540. return;
  541. ustream_for_each_read_buffer(s, rbuf, len)
  542. {
  543. memcpy(wbuf, rbuf, len);
  544. wbuf += len;
  545. }
  546. *wbuf = 0;
  547. blobmsg_add_string_buffer(&buf);
  548. }
  549. }
  550. static void
  551. rpc_file_exec_reply(struct rpc_file_exec_context *c, int rv)
  552. {
  553. uloop_timeout_cancel(&c->timeout);
  554. uloop_process_delete(&c->process);
  555. if (rv == UBUS_STATUS_OK)
  556. {
  557. blob_buf_init(&buf, 0);
  558. blobmsg_add_u32(&buf, "code", WEXITSTATUS(c->stat));
  559. rpc_ustream_to_blobmsg(&c->opipe.stream, "stdout");
  560. rpc_ustream_to_blobmsg(&c->epipe.stream, "stderr");
  561. ubus_send_reply(c->context, &c->request, buf.head);
  562. blob_buf_free(&buf);
  563. }
  564. ubus_complete_deferred_request(c->context, &c->request, rv);
  565. ustream_free(&c->opipe.stream);
  566. ustream_free(&c->epipe.stream);
  567. close(c->opipe.fd.fd);
  568. close(c->epipe.fd.fd);
  569. free(c);
  570. }
  571. static void
  572. rpc_file_exec_timeout_cb(struct uloop_timeout *t)
  573. {
  574. struct rpc_file_exec_context *c =
  575. container_of(t, struct rpc_file_exec_context, timeout);
  576. kill(c->process.pid, SIGKILL);
  577. rpc_file_exec_reply(c, UBUS_STATUS_TIMEOUT);
  578. }
  579. static void
  580. rpc_file_exec_process_cb(struct uloop_process *p, int stat)
  581. {
  582. struct rpc_file_exec_context *c =
  583. container_of(p, struct rpc_file_exec_context, process);
  584. c->stat = stat;
  585. ustream_poll(&c->opipe.stream);
  586. ustream_poll(&c->epipe.stream);
  587. }
  588. static void
  589. rpc_file_exec_opipe_read_cb(struct ustream *s, int bytes)
  590. {
  591. struct rpc_file_exec_context *c =
  592. container_of(s, struct rpc_file_exec_context, opipe.stream);
  593. if (ustream_read_buf_full(s))
  594. rpc_file_exec_reply(c, UBUS_STATUS_NOT_SUPPORTED);
  595. }
  596. static void
  597. rpc_file_exec_epipe_read_cb(struct ustream *s, int bytes)
  598. {
  599. struct rpc_file_exec_context *c =
  600. container_of(s, struct rpc_file_exec_context, epipe.stream);
  601. if (ustream_read_buf_full(s))
  602. rpc_file_exec_reply(c, UBUS_STATUS_NOT_SUPPORTED);
  603. }
  604. static void
  605. rpc_file_exec_opipe_state_cb(struct ustream *s)
  606. {
  607. struct rpc_file_exec_context *c =
  608. container_of(s, struct rpc_file_exec_context, opipe.stream);
  609. if (c->opipe.stream.eof && c->epipe.stream.eof)
  610. rpc_file_exec_reply(c, UBUS_STATUS_OK);
  611. }
  612. static void
  613. rpc_file_exec_epipe_state_cb(struct ustream *s)
  614. {
  615. struct rpc_file_exec_context *c =
  616. container_of(s, struct rpc_file_exec_context, epipe.stream);
  617. if (c->opipe.stream.eof && c->epipe.stream.eof)
  618. rpc_file_exec_reply(c, UBUS_STATUS_OK);
  619. }
  620. static void
  621. rpc_fdclose(int fd)
  622. {
  623. if (fd > 2)
  624. close(fd);
  625. }
  626. static int
  627. rpc_file_exec_run(const char *cmd, const struct blob_attr *sid,
  628. const struct blob_attr *arg, const struct blob_attr *env,
  629. struct ubus_context *ctx, struct ubus_request_data *req)
  630. {
  631. pid_t pid;
  632. int devnull;
  633. int opipe[2];
  634. int epipe[2];
  635. int rem;
  636. struct blob_attr *cur;
  637. uint8_t arglen;
  638. char *executable, **args, **tmp, *p;
  639. struct rpc_file_exec_context *c;
  640. if (sid && env)
  641. return UBUS_STATUS_PERMISSION_DENIED;
  642. cmd = rpc_file_exec_lookup(cmd);
  643. if (!cmd)
  644. return UBUS_STATUS_NOT_FOUND;
  645. executable = rpc_canonicalize_path(cmd);
  646. if (executable == NULL)
  647. return UBUS_STATUS_UNKNOWN_ERROR;
  648. if (!rpc_file_access(sid, executable, "exec"))
  649. {
  650. if (arg == NULL || strlen(executable) >= sizeof(cmdstr))
  651. return UBUS_STATUS_PERMISSION_DENIED;
  652. arglen = 2;
  653. p = cmdstr + sprintf(cmdstr, "%s", executable);
  654. blobmsg_for_each_attr(cur, arg, rem)
  655. {
  656. if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
  657. continue;
  658. if (arglen == 255 ||
  659. p + blobmsg_data_len(cur) >= cmdstr + sizeof(cmdstr))
  660. return UBUS_STATUS_PERMISSION_DENIED;
  661. p += sprintf(p, " %s", blobmsg_get_string(cur));
  662. arglen++;
  663. }
  664. if (!rpc_file_access(sid, cmdstr, "exec"))
  665. return UBUS_STATUS_PERMISSION_DENIED;
  666. }
  667. c = malloc(sizeof(*c));
  668. if (!c)
  669. return UBUS_STATUS_UNKNOWN_ERROR;
  670. if (pipe(opipe))
  671. goto fail_opipe;
  672. if (pipe(epipe))
  673. goto fail_epipe;
  674. switch ((pid = fork()))
  675. {
  676. case -1:
  677. goto fail_fork;
  678. case 0:
  679. uloop_done();
  680. devnull = open("/dev/null", O_RDWR);
  681. if (devnull == -1)
  682. return UBUS_STATUS_UNKNOWN_ERROR;
  683. dup2(devnull, 0);
  684. dup2(opipe[1], 1);
  685. dup2(epipe[1], 2);
  686. rpc_fdclose(devnull);
  687. rpc_fdclose(opipe[0]);
  688. rpc_fdclose(opipe[1]);
  689. rpc_fdclose(epipe[0]);
  690. rpc_fdclose(epipe[1]);
  691. arglen = 2;
  692. args = malloc(sizeof(char *) * arglen);
  693. if (!args)
  694. return UBUS_STATUS_UNKNOWN_ERROR;
  695. args[0] = (char *)executable;
  696. args[1] = NULL;
  697. if (arg)
  698. {
  699. blobmsg_for_each_attr(cur, arg, rem)
  700. {
  701. if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
  702. continue;
  703. if (arglen == 255)
  704. {
  705. free(args);
  706. return UBUS_STATUS_INVALID_ARGUMENT;
  707. }
  708. arglen++;
  709. tmp = realloc(args, sizeof(char *) * arglen);
  710. if (!tmp)
  711. {
  712. free(args);
  713. return UBUS_STATUS_UNKNOWN_ERROR;
  714. }
  715. args = tmp;
  716. args[arglen-2] = blobmsg_data(cur);
  717. args[arglen-1] = NULL;
  718. }
  719. }
  720. if (env)
  721. {
  722. blobmsg_for_each_attr(cur, env, rem)
  723. {
  724. if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
  725. continue;
  726. setenv(blobmsg_name(cur), blobmsg_data(cur), 1);
  727. }
  728. }
  729. if (execv(executable, args))
  730. return rpc_errno_status();
  731. default:
  732. memset(c, 0, sizeof(*c));
  733. ustream_declare(c->opipe, opipe[0], exec_opipe);
  734. ustream_declare(c->epipe, epipe[0], exec_epipe);
  735. c->process.pid = pid;
  736. c->process.cb = rpc_file_exec_process_cb;
  737. uloop_process_add(&c->process);
  738. c->timeout.cb = rpc_file_exec_timeout_cb;
  739. uloop_timeout_set(&c->timeout, *ops->exec_timeout);
  740. close(opipe[1]);
  741. close(epipe[1]);
  742. c->context = ctx;
  743. ubus_defer_request(ctx, req, &c->request);
  744. }
  745. return UBUS_STATUS_OK;
  746. fail_fork:
  747. close(epipe[0]);
  748. close(epipe[1]);
  749. fail_epipe:
  750. close(opipe[0]);
  751. close(opipe[1]);
  752. fail_opipe:
  753. free(c);
  754. return rpc_errno_status();
  755. }
  756. static int
  757. rpc_file_exec(struct ubus_context *ctx, struct ubus_object *obj,
  758. struct ubus_request_data *req, const char *method,
  759. struct blob_attr *msg)
  760. {
  761. struct blob_attr *tb[__RPC_E_MAX];
  762. blobmsg_parse(rpc_exec_policy, __RPC_E_MAX, tb,
  763. blob_data(msg), blob_len(msg));
  764. if (!tb[RPC_E_CMD])
  765. return UBUS_STATUS_INVALID_ARGUMENT;
  766. return rpc_file_exec_run(blobmsg_data(tb[RPC_E_CMD]), tb[RPC_E_SESSION],
  767. tb[RPC_E_PARM], tb[RPC_E_ENV], ctx, req);
  768. }
  769. static int
  770. rpc_file_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
  771. {
  772. static const struct ubus_method file_methods[] = {
  773. UBUS_METHOD("read", rpc_file_read, rpc_file_RB_policy),
  774. UBUS_METHOD("write", rpc_file_write, rpc_file_RW_policy),
  775. UBUS_METHOD("list", rpc_file_list, rpc_file_R_policy),
  776. UBUS_METHOD("stat", rpc_file_stat, rpc_file_R_policy),
  777. UBUS_METHOD("md5", rpc_file_md5, rpc_file_R_policy),
  778. UBUS_METHOD("remove", rpc_file_remove, rpc_file_R_policy),
  779. UBUS_METHOD("exec", rpc_file_exec, rpc_exec_policy),
  780. };
  781. static struct ubus_object_type file_type =
  782. UBUS_OBJECT_TYPE("rpcd-plugin-file", file_methods);
  783. static struct ubus_object obj = {
  784. .name = "file",
  785. .type = &file_type,
  786. .methods = file_methods,
  787. .n_methods = ARRAY_SIZE(file_methods),
  788. };
  789. ops = o;
  790. return ubus_add_object(ctx, &obj);
  791. }
  792. struct rpc_plugin rpc_plugin = {
  793. .init = rpc_file_api_init
  794. };