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 },
49 rpc_errno_status(void)
54 return UBUS_STATUS_PERMISSION_DENIED;
57 return UBUS_STATUS_INVALID_ARGUMENT;
60 return UBUS_STATUS_NOT_FOUND;
63 return UBUS_STATUS_INVALID_ARGUMENT;
66 return UBUS_STATUS_UNKNOWN_ERROR;
71 rpc_cgi_password_set(struct ubus_context *ctx, struct ubus_object *obj,
72 struct ubus_request_data *req, const char *method,
73 struct blob_attr *msg)
78 struct blob_attr *tb[__RPC_P_MAX];
80 blobmsg_parse(rpc_password_policy, __RPC_P_MAX, tb,
81 blob_data(msg), blob_len(msg));
83 if (!tb[RPC_P_USER] || !tb[RPC_P_PASSWORD])
84 return UBUS_STATUS_INVALID_ARGUMENT;
86 if (stat("/usr/bin/passwd", &s))
87 return UBUS_STATUS_NOT_FOUND;
89 if (!(s.st_mode & S_IXUSR))
90 return UBUS_STATUS_PERMISSION_DENIED;
93 return rpc_errno_status();
95 switch ((pid = fork()))
100 return rpc_errno_status();
109 if ((fd = open("/dev/null", O_RDWR)) > -1)
118 if (execl("/usr/bin/passwd", "/usr/bin/passwd",
119 blobmsg_data(tb[RPC_P_USER]), NULL))
120 return rpc_errno_status();
125 write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
126 blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
127 write(fds[1], "\n", 1);
131 write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
132 blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
133 write(fds[1], "\n", 1);
137 waitpid(pid, NULL, 0);
144 rpc_sys_upgrade_test(struct ubus_context *ctx, struct ubus_object *obj,
145 struct ubus_request_data *req, const char *method,
146 struct blob_attr *msg)
148 const char *cmd[4] = { "sysupgrade", "--test", "/tmp/firmware.bin", NULL };
149 return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
153 rpc_sys_upgrade_start(struct ubus_context *ctx, struct ubus_object *obj,
154 struct ubus_request_data *req, const char *method,
155 struct blob_attr *msg)
157 struct blob_attr *tb[__RPC_UPGRADE_MAX];
158 char * const cmd[4] = { "/sbin/sysupgrade", "-n", "/tmp/firmware.bin", NULL };
159 char * const cmd_keep[3] = { "/sbin/sysupgrade", "/tmp/firmware.bin", NULL };
160 char * const * c = cmd;
162 blobmsg_parse(rpc_upgrade_policy, __RPC_UPGRADE_MAX, tb,
163 blob_data(msg), blob_len(msg));
165 if (tb[RPC_UPGRADE_KEEP] && blobmsg_get_bool(tb[RPC_UPGRADE_KEEP]))
169 /* wait for the RPC call to complete */
171 return execv(c[0], c);
178 rpc_sys_upgrade_clean(struct ubus_context *ctx, struct ubus_object *obj,
179 struct ubus_request_data *req, const char *method,
180 struct blob_attr *msg)
182 if (unlink("/tmp/firmware.bin"))
183 return rpc_errno_status();
189 rpc_sys_factory(struct ubus_context *ctx, struct ubus_object *obj,
190 struct ubus_request_data *req, const char *method,
191 struct blob_attr *msg)
193 char * const cmd[4] = { "/sbin/jffs2reset", "-y", "-r", NULL };
196 /* wait for the RPC call to complete */
198 return execv(cmd[0], cmd);
205 rpc_sys_reboot(struct ubus_context *ctx, struct ubus_object *obj,
206 struct ubus_request_data *req, const char *method,
207 struct blob_attr *msg)
221 rpc_sys_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
223 static const struct ubus_method sys_methods[] = {
224 UBUS_METHOD("password_set", rpc_cgi_password_set, rpc_password_policy),
225 UBUS_METHOD_NOARG("upgrade_test", rpc_sys_upgrade_test),
226 UBUS_METHOD("upgrade_start", rpc_sys_upgrade_start,
228 UBUS_METHOD_NOARG("upgrade_clean", rpc_sys_upgrade_clean),
229 UBUS_METHOD_NOARG("factory", rpc_sys_factory),
230 UBUS_METHOD_NOARG("reboot", rpc_sys_reboot),
233 static struct ubus_object_type sys_type =
234 UBUS_OBJECT_TYPE("luci-rpc-sys", sys_methods);
236 static struct ubus_object obj = {
239 .methods = sys_methods,
240 .n_methods = ARRAY_SIZE(sys_methods),
245 return ubus_add_object(ctx, &obj);
248 struct rpc_plugin rpc_plugin = {
249 .init = rpc_sys_api_init