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";
92 blobmsg_parse(rpc_password_policy, __RPC_P_MAX, tb,
93 blob_data(msg), blob_len(msg));
95 if (!tb[RPC_P_USER] || !tb[RPC_P_PASSWORD])
96 return UBUS_STATUS_INVALID_ARGUMENT;
99 return UBUS_STATUS_NOT_FOUND;
101 if (!(s.st_mode & S_IXUSR))
102 return UBUS_STATUS_PERMISSION_DENIED;
105 return rpc_errno_status();
107 switch ((pid = fork()))
112 return rpc_errno_status();
121 if ((fd = open("/dev/null", O_RDWR)) > -1)
130 return rpc_errno_status();
132 if (execl(passwd, passwd,
133 blobmsg_data(tb[RPC_P_USER]), NULL))
134 return rpc_errno_status();
139 n = write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
140 blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
142 return rpc_errno_status();
144 n = write(fds[1], "\n", 1);
146 return rpc_errno_status();
150 n = write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
151 blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
153 return rpc_errno_status();
154 n = write(fds[1], "\n", 1);
156 return rpc_errno_status();
160 waitpid(pid, NULL, 0);
167 rpc_sys_packagelist(struct ubus_context *ctx, struct ubus_object *obj,
168 struct ubus_request_data *req, const char *method,
169 struct blob_attr *msg)
171 struct blob_attr *tb[__RPC_PACKAGELIST_MAX];
173 struct blob_buf buf = { 0 };
174 char var[256], pkg[128], ver[128];
175 char *tmp, *p1, *p2, *p3;
178 blobmsg_parse(rpc_packagelist_policy, __RPC_PACKAGELIST_MAX, tb,
179 blob_data(msg), blob_len(msg));
181 if (tb[RPC_PACKAGELIST_ALL] && blobmsg_get_bool(tb[RPC_PACKAGELIST_ALL]))
184 FILE *f = fopen("/usr/lib/opkg/status", "r");
186 return UBUS_STATUS_NOT_FOUND;
188 blob_buf_init(&buf, 0);
189 tbl = blobmsg_open_table(&buf, "packages");
190 pkg[0] = ver[0] = '\0';
192 while(fgets(var, sizeof(var), f)) {
193 p1 = strchr(var, ' ');
199 p2 = strchr(p1, ' ');
201 tmp = strchr(p1, '\n');
208 p3 = strchr(p2, ' ');
210 tmp = strchr(p2, '\n');
217 tmp = strchr(p3, '\n');
225 if (!strcmp(var, "Package:")) {
226 strncpy(pkg, p1, sizeof(pkg));
230 if (!strcmp(var, "Version:")) {
231 strncpy(ver, p1, sizeof(ver));
236 !strcmp(var, "Status:") &&
237 !strcmp(p1, "install") &&
238 (all || strstr(p2, "user")) &&
239 !strcmp(p3, "installed") && pkg[0] && ver[0]) {
240 blobmsg_add_string(&buf, pkg, ver);
241 pkg[0] = ver[0] = '\0';
245 blobmsg_close_table(&buf, tbl);
246 ubus_send_reply(ctx, req, buf.head);
254 rpc_sys_upgrade_test(struct ubus_context *ctx, struct ubus_object *obj,
255 struct ubus_request_data *req, const char *method,
256 struct blob_attr *msg)
258 const char *cmd[4] = { "sysupgrade", "--test", "/tmp/firmware.bin", NULL };
259 return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
263 rpc_sys_upgrade_start(struct ubus_context *ctx, struct ubus_object *obj,
264 struct ubus_request_data *req, const char *method,
265 struct blob_attr *msg)
267 struct blob_attr *tb[__RPC_UPGRADE_MAX];
268 char * const cmd[4] = { "/sbin/sysupgrade", "-n", "/tmp/firmware.bin", NULL };
269 char * const cmd_keep[3] = { "/sbin/sysupgrade", "/tmp/firmware.bin", NULL };
270 char * const * c = cmd;
272 blobmsg_parse(rpc_upgrade_policy, __RPC_UPGRADE_MAX, tb,
273 blob_data(msg), blob_len(msg));
275 if (tb[RPC_UPGRADE_KEEP] && blobmsg_get_bool(tb[RPC_UPGRADE_KEEP]))
279 /* wait for the RPC call to complete */
281 return execv(c[0], c);
288 rpc_sys_upgrade_clean(struct ubus_context *ctx, struct ubus_object *obj,
289 struct ubus_request_data *req, const char *method,
290 struct blob_attr *msg)
292 if (unlink("/tmp/firmware.bin"))
293 return rpc_errno_status();
299 rpc_sys_factory(struct ubus_context *ctx, struct ubus_object *obj,
300 struct ubus_request_data *req, const char *method,
301 struct blob_attr *msg)
303 char * const cmd[4] = { "/sbin/jffs2reset", "-y", "-r", NULL };
306 /* wait for the RPC call to complete */
308 return execv(cmd[0], cmd);
315 rpc_sys_reboot(struct ubus_context *ctx, struct ubus_object *obj,
316 struct ubus_request_data *req, const char *method,
317 struct blob_attr *msg)
331 rpc_sys_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
333 static const struct ubus_method sys_methods[] = {
334 UBUS_METHOD("packagelist", rpc_sys_packagelist, rpc_packagelist_policy),
335 UBUS_METHOD("password_set", rpc_cgi_password_set, rpc_password_policy),
336 UBUS_METHOD_NOARG("upgrade_test", rpc_sys_upgrade_test),
337 UBUS_METHOD("upgrade_start", rpc_sys_upgrade_start,
339 UBUS_METHOD_NOARG("upgrade_clean", rpc_sys_upgrade_clean),
340 UBUS_METHOD_NOARG("factory", rpc_sys_factory),
341 UBUS_METHOD_NOARG("reboot", rpc_sys_reboot),
344 static struct ubus_object_type sys_type =
345 UBUS_OBJECT_TYPE("luci-rpc-sys", sys_methods);
347 static struct ubus_object obj = {
350 .methods = sys_methods,
351 .n_methods = ARRAY_SIZE(sys_methods),
356 return ubus_add_object(ctx, &obj);
359 struct rpc_plugin rpc_plugin = {
360 .init = rpc_sys_api_init