sys.c 8.9 KB

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