sys.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. #include <libubus.h>
  19. #include <rpcd/exec.h>
  20. #include <rpcd/plugin.h>
  21. #include <rpcd/session.h>
  22. #include <sys/reboot.h>
  23. static const struct rpc_daemon_ops *ops;
  24. enum {
  25. RPC_P_USER,
  26. RPC_P_PASSWORD,
  27. __RPC_P_MAX
  28. };
  29. static const struct blobmsg_policy rpc_password_policy[__RPC_P_MAX] = {
  30. [RPC_P_USER] = { .name = "user", .type = BLOBMSG_TYPE_STRING },
  31. [RPC_P_PASSWORD] = { .name = "password", .type = BLOBMSG_TYPE_STRING },
  32. };
  33. enum {
  34. RPC_UPGRADE_KEEP,
  35. __RPC_UPGRADE_MAX
  36. };
  37. static const struct blobmsg_policy rpc_upgrade_policy[__RPC_UPGRADE_MAX] = {
  38. [RPC_UPGRADE_KEEP] = { .name = "keep", .type = BLOBMSG_TYPE_BOOL },
  39. };
  40. enum {
  41. RPC_PACKAGELIST_ALL,
  42. __RPC_PACKAGELIST_MAX
  43. };
  44. static const struct blobmsg_policy rpc_packagelist_policy[__RPC_PACKAGELIST_MAX] = {
  45. [RPC_PACKAGELIST_ALL] = { .name = "all", .type = BLOBMSG_TYPE_BOOL },
  46. };
  47. static int
  48. rpc_errno_status(void)
  49. {
  50. switch (errno)
  51. {
  52. case EACCES:
  53. return UBUS_STATUS_PERMISSION_DENIED;
  54. case ENOTDIR:
  55. return UBUS_STATUS_INVALID_ARGUMENT;
  56. case ENOENT:
  57. return UBUS_STATUS_NOT_FOUND;
  58. case EINVAL:
  59. return UBUS_STATUS_INVALID_ARGUMENT;
  60. default:
  61. return UBUS_STATUS_UNKNOWN_ERROR;
  62. }
  63. }
  64. static int
  65. rpc_cgi_password_set(struct ubus_context *ctx, struct ubus_object *obj,
  66. struct ubus_request_data *req, const char *method,
  67. struct blob_attr *msg)
  68. {
  69. pid_t pid;
  70. int fd, fds[2];
  71. struct stat s;
  72. struct blob_attr *tb[__RPC_P_MAX];
  73. ssize_t n;
  74. int ret;
  75. const char *const passwd = "/bin/passwd";
  76. const struct timespec ts = {0, 100 * 1000 * 1000};
  77. blobmsg_parse(rpc_password_policy, __RPC_P_MAX, tb,
  78. blob_data(msg), blob_len(msg));
  79. if (!tb[RPC_P_USER] || !tb[RPC_P_PASSWORD])
  80. return UBUS_STATUS_INVALID_ARGUMENT;
  81. if (stat(passwd, &s))
  82. return UBUS_STATUS_NOT_FOUND;
  83. if (!(s.st_mode & S_IXUSR))
  84. return UBUS_STATUS_PERMISSION_DENIED;
  85. if (pipe(fds))
  86. return rpc_errno_status();
  87. switch ((pid = fork()))
  88. {
  89. case -1:
  90. close(fds[0]);
  91. close(fds[1]);
  92. return rpc_errno_status();
  93. case 0:
  94. uloop_done();
  95. dup2(fds[0], 0);
  96. close(fds[0]);
  97. close(fds[1]);
  98. if ((fd = open("/dev/null", O_RDWR)) > -1)
  99. {
  100. dup2(fd, 1);
  101. dup2(fd, 2);
  102. close(fd);
  103. }
  104. ret = chdir("/");
  105. if (ret < 0)
  106. return rpc_errno_status();
  107. if (execl(passwd, passwd,
  108. blobmsg_data(tb[RPC_P_USER]), NULL))
  109. return rpc_errno_status();
  110. default:
  111. close(fds[0]);
  112. n = write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
  113. blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
  114. if (n < 0)
  115. return rpc_errno_status();
  116. n = write(fds[1], "\n", 1);
  117. if (n < 0)
  118. return rpc_errno_status();
  119. nanosleep(&ts, NULL);
  120. n = write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
  121. blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
  122. if (n < 0)
  123. return rpc_errno_status();
  124. n = write(fds[1], "\n", 1);
  125. if (n < 0)
  126. return rpc_errno_status();
  127. close(fds[1]);
  128. waitpid(pid, NULL, 0);
  129. return 0;
  130. }
  131. }
  132. static int
  133. rpc_sys_packagelist(struct ubus_context *ctx, struct ubus_object *obj,
  134. struct ubus_request_data *req, const char *method,
  135. struct blob_attr *msg)
  136. {
  137. struct blob_attr *tb[__RPC_PACKAGELIST_MAX];
  138. int all = false;
  139. struct blob_buf buf = { 0 };
  140. char var[256], pkg[128], ver[128];
  141. char *tmp, *p1, *p2, *p3;
  142. void *tbl;
  143. blobmsg_parse(rpc_packagelist_policy, __RPC_PACKAGELIST_MAX, tb,
  144. blob_data(msg), blob_len(msg));
  145. if (tb[RPC_PACKAGELIST_ALL] && blobmsg_get_bool(tb[RPC_PACKAGELIST_ALL]))
  146. all = true;
  147. FILE *f = fopen("/usr/lib/opkg/status", "r");
  148. if (!f)
  149. return UBUS_STATUS_NOT_FOUND;
  150. blob_buf_init(&buf, 0);
  151. tbl = blobmsg_open_table(&buf, "packages");
  152. pkg[0] = ver[0] = '\0';
  153. while(fgets(var, sizeof(var), f)) {
  154. p1 = strchr(var, ' ');
  155. p2 = p3 = NULL;
  156. if (!p1)
  157. goto procstr;
  158. *p1++ = '\0';
  159. p2 = strchr(p1, ' ');
  160. if (!p2) {
  161. tmp = strchr(p1, '\n');
  162. if (tmp)
  163. *tmp = '\0';
  164. goto procstr;
  165. }
  166. *p2++ = '\0';
  167. p3 = strchr(p2, ' ');
  168. if (!p3) {
  169. tmp = strchr(p2, '\n');
  170. if (tmp)
  171. *tmp = '\0';
  172. goto procstr;
  173. }
  174. *p3++ = '\0';
  175. tmp = strchr(p3, '\n');
  176. if (tmp)
  177. *tmp = '\0';
  178. procstr:
  179. if (!p1)
  180. continue;
  181. if (!strcmp(var, "Package:")) {
  182. strncpy(pkg, p1, sizeof(pkg));
  183. continue;
  184. }
  185. /* If there is ABIVersion, remove that suffix */
  186. if (!strcmp(var, "ABIVersion:")) {
  187. if (strlen(pkg) <= strlen(p1))
  188. continue;
  189. tmp = &pkg[strlen(pkg) - strlen(p1)];
  190. if (strncmp(p1, tmp, strlen(p1)))
  191. continue;
  192. *tmp = '\0';
  193. continue;
  194. }
  195. if (!strcmp(var, "Version:")) {
  196. strncpy(ver, p1, sizeof(ver));
  197. continue;
  198. }
  199. if (p2 && p3 &&
  200. !strcmp(var, "Status:") &&
  201. !strcmp(p1, "install") &&
  202. (all || strstr(p2, "user")) &&
  203. !strcmp(p3, "installed") && pkg[0] && ver[0]) {
  204. blobmsg_add_string(&buf, pkg, ver);
  205. pkg[0] = ver[0] = '\0';
  206. }
  207. }
  208. blobmsg_close_table(&buf, tbl);
  209. ubus_send_reply(ctx, req, buf.head);
  210. blob_buf_free(&buf);
  211. fclose(f);
  212. return 0;
  213. }
  214. static int
  215. rpc_sys_upgrade_test(struct ubus_context *ctx, struct ubus_object *obj,
  216. struct ubus_request_data *req, const char *method,
  217. struct blob_attr *msg)
  218. {
  219. const char *cmd[4] = { "sysupgrade", "--test", "/tmp/firmware.bin", NULL };
  220. return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
  221. }
  222. static int
  223. rpc_sys_upgrade_start(struct ubus_context *ctx, struct ubus_object *obj,
  224. struct ubus_request_data *req, const char *method,
  225. struct blob_attr *msg)
  226. {
  227. struct blob_attr *tb[__RPC_UPGRADE_MAX];
  228. char * const cmd[4] = { "/sbin/sysupgrade", "-n", "/tmp/firmware.bin", NULL };
  229. char * const cmd_keep[3] = { "/sbin/sysupgrade", "/tmp/firmware.bin", NULL };
  230. char * const * c = cmd;
  231. blobmsg_parse(rpc_upgrade_policy, __RPC_UPGRADE_MAX, tb,
  232. blob_data(msg), blob_len(msg));
  233. if (tb[RPC_UPGRADE_KEEP] && blobmsg_get_bool(tb[RPC_UPGRADE_KEEP]))
  234. c = cmd_keep;
  235. if (!fork()) {
  236. /* wait for the RPC call to complete */
  237. sleep(2);
  238. return execv(c[0], c);
  239. }
  240. return 0;
  241. }
  242. static int
  243. rpc_sys_upgrade_clean(struct ubus_context *ctx, struct ubus_object *obj,
  244. struct ubus_request_data *req, const char *method,
  245. struct blob_attr *msg)
  246. {
  247. if (unlink("/tmp/firmware.bin"))
  248. return rpc_errno_status();
  249. return 0;
  250. }
  251. static int
  252. rpc_sys_factory(struct ubus_context *ctx, struct ubus_object *obj,
  253. struct ubus_request_data *req, const char *method,
  254. struct blob_attr *msg)
  255. {
  256. char * const cmd[4] = { "/sbin/jffs2reset", "-y", "-r", NULL };
  257. if (!fork()) {
  258. /* wait for the RPC call to complete */
  259. sleep(2);
  260. return execv(cmd[0], cmd);
  261. }
  262. return 0;
  263. }
  264. static int
  265. rpc_sys_reboot(struct ubus_context *ctx, struct ubus_object *obj,
  266. struct ubus_request_data *req, const char *method,
  267. struct blob_attr *msg)
  268. {
  269. if (!fork()) {
  270. sync();
  271. sleep(2);
  272. reboot(RB_AUTOBOOT);
  273. while (1)
  274. ;
  275. }
  276. return 0;
  277. }
  278. static int
  279. rpc_sys_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
  280. {
  281. static const struct ubus_method sys_methods[] = {
  282. UBUS_METHOD("packagelist", rpc_sys_packagelist, rpc_packagelist_policy),
  283. UBUS_METHOD("password_set", rpc_cgi_password_set, rpc_password_policy),
  284. UBUS_METHOD_NOARG("upgrade_test", rpc_sys_upgrade_test),
  285. UBUS_METHOD("upgrade_start", rpc_sys_upgrade_start,
  286. rpc_upgrade_policy),
  287. UBUS_METHOD_NOARG("upgrade_clean", rpc_sys_upgrade_clean),
  288. UBUS_METHOD_NOARG("factory", rpc_sys_factory),
  289. UBUS_METHOD_NOARG("reboot", rpc_sys_reboot),
  290. };
  291. static struct ubus_object_type sys_type =
  292. UBUS_OBJECT_TYPE("luci-rpc-sys", sys_methods);
  293. static struct ubus_object obj = {
  294. .name = "rpc-sys",
  295. .type = &sys_type,
  296. .methods = sys_methods,
  297. .n_methods = ARRAY_SIZE(sys_methods),
  298. };
  299. ops = o;
  300. return ubus_add_object(ctx, &obj);
  301. }
  302. struct rpc_plugin rpc_plugin = {
  303. .init = rpc_sys_api_init
  304. };