2 * rpcd - UBUS RPC server
4 * Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
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.
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.
21 #include <rpcd/exec.h>
22 #include <rpcd/plugin.h>
23 #include <rpcd/session.h>
24 #include <sys/reboot.h>
26 static const struct rpc_daemon_ops *ops;
34 static const struct blobmsg_policy rpc_password_policy[__RPC_P_MAX] = {
35 [RPC_P_USER] = { .name = "user", .type = BLOBMSG_TYPE_STRING },
36 [RPC_P_PASSWORD] = { .name = "password", .type = BLOBMSG_TYPE_STRING },
44 static const struct blobmsg_policy rpc_upgrade_policy[__RPC_UPGRADE_MAX] = {
45 [RPC_UPGRADE_KEEP] = { .name = "keep", .type = BLOBMSG_TYPE_BOOL },
53 static const struct blobmsg_policy rpc_packagelist_policy[__RPC_PACKAGELIST_MAX] = {
54 [RPC_PACKAGELIST_ALL] = { .name = "all", .type = BLOBMSG_TYPE_BOOL },
58 rpc_errno_status(void)
63 return UBUS_STATUS_PERMISSION_DENIED;
66 return UBUS_STATUS_INVALID_ARGUMENT;
69 return UBUS_STATUS_NOT_FOUND;
72 return UBUS_STATUS_INVALID_ARGUMENT;
75 return UBUS_STATUS_UNKNOWN_ERROR;
80 rpc_cgi_password_set(struct ubus_context *ctx, struct ubus_object *obj,
81 struct ubus_request_data *req, const char *method,
82 struct blob_attr *msg)
87 struct blob_attr *tb[__RPC_P_MAX];
90 const char *const passwd = "/bin/passwd";
91 const struct timespec ts = {0, 100 * 1000 * 1000};
93 blobmsg_parse(rpc_password_policy, __RPC_P_MAX, tb,
94 blob_data(msg), blob_len(msg));
96 if (!tb[RPC_P_USER] || !tb[RPC_P_PASSWORD])
97 return UBUS_STATUS_INVALID_ARGUMENT;
100 return UBUS_STATUS_NOT_FOUND;
102 if (!(s.st_mode & S_IXUSR))
103 return UBUS_STATUS_PERMISSION_DENIED;
106 return rpc_errno_status();
108 switch ((pid = fork()))
113 return rpc_errno_status();
122 if ((fd = open("/dev/null", O_RDWR)) > -1)
131 return rpc_errno_status();
133 if (execl(passwd, passwd,
134 blobmsg_data(tb[RPC_P_USER]), NULL))
135 return rpc_errno_status();
140 n = write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
141 blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
143 return rpc_errno_status();
145 n = write(fds[1], "\n", 1);
147 return rpc_errno_status();
149 nanosleep(&ts, NULL);
151 n = write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
152 blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
154 return rpc_errno_status();
155 n = write(fds[1], "\n", 1);
157 return rpc_errno_status();
161 waitpid(pid, NULL, 0);
168 rpc_sys_packagelist(struct ubus_context *ctx, struct ubus_object *obj,
169 struct ubus_request_data *req, const char *method,
170 struct blob_attr *msg)
172 struct blob_attr *tb[__RPC_PACKAGELIST_MAX];
174 struct blob_buf buf = { 0 };
175 char var[256], pkg[128], ver[128];
176 char *tmp, *p1, *p2, *p3;
179 blobmsg_parse(rpc_packagelist_policy, __RPC_PACKAGELIST_MAX, tb,
180 blob_data(msg), blob_len(msg));
182 if (tb[RPC_PACKAGELIST_ALL] && blobmsg_get_bool(tb[RPC_PACKAGELIST_ALL]))
185 FILE *f = fopen("/usr/lib/opkg/status", "r");
187 return UBUS_STATUS_NOT_FOUND;
189 blob_buf_init(&buf, 0);
190 tbl = blobmsg_open_table(&buf, "packages");
191 pkg[0] = ver[0] = '\0';
193 while(fgets(var, sizeof(var), f)) {
194 p1 = strchr(var, ' ');
200 p2 = strchr(p1, ' ');
202 tmp = strchr(p1, '\n');
209 p3 = strchr(p2, ' ');
211 tmp = strchr(p2, '\n');
218 tmp = strchr(p3, '\n');
226 if (!strcmp(var, "Package:")) {
227 strncpy(pkg, p1, sizeof(pkg));
231 if (!strcmp(var, "Version:")) {
232 strncpy(ver, p1, sizeof(ver));
237 !strcmp(var, "Status:") &&
238 !strcmp(p1, "install") &&
239 (all || strstr(p2, "user")) &&
240 !strcmp(p3, "installed") && pkg[0] && ver[0]) {
241 blobmsg_add_string(&buf, pkg, ver);
242 pkg[0] = ver[0] = '\0';
246 blobmsg_close_table(&buf, tbl);
247 ubus_send_reply(ctx, req, buf.head);
255 rpc_sys_upgrade_test(struct ubus_context *ctx, struct ubus_object *obj,
256 struct ubus_request_data *req, const char *method,
257 struct blob_attr *msg)
259 const char *cmd[4] = { "sysupgrade", "--test", "/tmp/firmware.bin", NULL };
260 return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
264 rpc_sys_upgrade_start(struct ubus_context *ctx, struct ubus_object *obj,
265 struct ubus_request_data *req, const char *method,
266 struct blob_attr *msg)
268 struct blob_attr *tb[__RPC_UPGRADE_MAX];
269 char * const cmd[4] = { "/sbin/sysupgrade", "-n", "/tmp/firmware.bin", NULL };
270 char * const cmd_keep[3] = { "/sbin/sysupgrade", "/tmp/firmware.bin", NULL };
271 char * const * c = cmd;
273 blobmsg_parse(rpc_upgrade_policy, __RPC_UPGRADE_MAX, tb,
274 blob_data(msg), blob_len(msg));
276 if (tb[RPC_UPGRADE_KEEP] && blobmsg_get_bool(tb[RPC_UPGRADE_KEEP]))
280 /* wait for the RPC call to complete */
282 return execv(c[0], c);
289 rpc_sys_upgrade_clean(struct ubus_context *ctx, struct ubus_object *obj,
290 struct ubus_request_data *req, const char *method,
291 struct blob_attr *msg)
293 if (unlink("/tmp/firmware.bin"))
294 return rpc_errno_status();
300 rpc_sys_factory(struct ubus_context *ctx, struct ubus_object *obj,
301 struct ubus_request_data *req, const char *method,
302 struct blob_attr *msg)
304 char * const cmd[4] = { "/sbin/jffs2reset", "-y", "-r", NULL };
307 /* wait for the RPC call to complete */
309 return execv(cmd[0], cmd);
316 rpc_sys_reboot(struct ubus_context *ctx, struct ubus_object *obj,
317 struct ubus_request_data *req, const char *method,
318 struct blob_attr *msg)
332 rpc_sys_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
334 static const struct ubus_method sys_methods[] = {
335 UBUS_METHOD("packagelist", rpc_sys_packagelist, rpc_packagelist_policy),
336 UBUS_METHOD("password_set", rpc_cgi_password_set, rpc_password_policy),
337 UBUS_METHOD_NOARG("upgrade_test", rpc_sys_upgrade_test),
338 UBUS_METHOD("upgrade_start", rpc_sys_upgrade_start,
340 UBUS_METHOD_NOARG("upgrade_clean", rpc_sys_upgrade_clean),
341 UBUS_METHOD_NOARG("factory", rpc_sys_factory),
342 UBUS_METHOD_NOARG("reboot", rpc_sys_reboot),
345 static struct ubus_object_type sys_type =
346 UBUS_OBJECT_TYPE("luci-rpc-sys", sys_methods);
348 static struct ubus_object obj = {
351 .methods = sys_methods,
352 .n_methods = ARRAY_SIZE(sys_methods),
357 return ubus_add_object(ctx, &obj);
360 struct rpc_plugin rpc_plugin = {
361 .init = rpc_sys_api_init