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];
82 blobmsg_parse(rpc_password_policy, __RPC_P_MAX, tb,
83 blob_data(msg), blob_len(msg));
85 if (!tb[RPC_P_USER] || !tb[RPC_P_PASSWORD])
86 return UBUS_STATUS_INVALID_ARGUMENT;
88 if (stat("/usr/bin/passwd", &s))
89 return UBUS_STATUS_NOT_FOUND;
91 if (!(s.st_mode & S_IXUSR))
92 return UBUS_STATUS_PERMISSION_DENIED;
95 return rpc_errno_status();
97 switch ((pid = fork()))
102 return rpc_errno_status();
111 if ((fd = open("/dev/null", O_RDWR)) > -1)
120 return rpc_errno_status();
122 if (execl("/usr/bin/passwd", "/usr/bin/passwd",
123 blobmsg_data(tb[RPC_P_USER]), NULL))
124 return rpc_errno_status();
129 n = write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
130 blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
132 return rpc_errno_status();
134 n = write(fds[1], "\n", 1);
136 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();
144 n = write(fds[1], "\n", 1);
146 return rpc_errno_status();
150 waitpid(pid, NULL, 0);
157 rpc_sys_packagelist(struct ubus_context *ctx, struct ubus_object *obj,
158 struct ubus_request_data *req, const char *method,
159 struct blob_attr *msg)
161 struct blob_buf buf = { 0 };
162 char var[256], pkg[128], ver[128];
163 char *tmp, *p1, *p2, *p3;
166 FILE *f = fopen("/usr/lib/opkg/status", "r");
168 return UBUS_STATUS_NOT_FOUND;
170 blob_buf_init(&buf, 0);
171 tbl = blobmsg_open_table(&buf, "packages");
172 pkg[0] = ver[0] = '\0';
174 while(fgets(var, sizeof(var), f)) {
175 p1 = strchr(var, ' ');
181 p2 = strchr(p1, ' ');
183 tmp = strchr(p1, '\n');
190 p3 = strchr(p2, ' ');
192 tmp = strchr(p2, '\n');
199 tmp = strchr(p3, '\n');
207 if (!strcmp(var, "Package:")) {
208 strncpy(pkg, p1, sizeof(pkg));
212 if (!strcmp(var, "Version:")) {
213 strncpy(ver, p1, sizeof(ver));
218 !strcmp(var, "Status:") &&
219 !strcmp(p1, "install") &&
220 !strcmp(p2, "user") &&
221 !strcmp(p3, "installed") && pkg[0] && ver[0]) {
222 blobmsg_add_string(&buf, pkg, ver);
223 pkg[0] = ver[0] = '\0';
227 blobmsg_close_table(&buf, tbl);
228 ubus_send_reply(ctx, req, buf.head);
236 rpc_sys_upgrade_test(struct ubus_context *ctx, struct ubus_object *obj,
237 struct ubus_request_data *req, const char *method,
238 struct blob_attr *msg)
240 const char *cmd[4] = { "sysupgrade", "--test", "/tmp/firmware.bin", NULL };
241 return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
245 rpc_sys_upgrade_start(struct ubus_context *ctx, struct ubus_object *obj,
246 struct ubus_request_data *req, const char *method,
247 struct blob_attr *msg)
249 struct blob_attr *tb[__RPC_UPGRADE_MAX];
250 char * const cmd[4] = { "/sbin/sysupgrade", "-n", "/tmp/firmware.bin", NULL };
251 char * const cmd_keep[3] = { "/sbin/sysupgrade", "/tmp/firmware.bin", NULL };
252 char * const * c = cmd;
254 blobmsg_parse(rpc_upgrade_policy, __RPC_UPGRADE_MAX, tb,
255 blob_data(msg), blob_len(msg));
257 if (tb[RPC_UPGRADE_KEEP] && blobmsg_get_bool(tb[RPC_UPGRADE_KEEP]))
261 /* wait for the RPC call to complete */
263 return execv(c[0], c);
270 rpc_sys_upgrade_clean(struct ubus_context *ctx, struct ubus_object *obj,
271 struct ubus_request_data *req, const char *method,
272 struct blob_attr *msg)
274 if (unlink("/tmp/firmware.bin"))
275 return rpc_errno_status();
281 rpc_sys_factory(struct ubus_context *ctx, struct ubus_object *obj,
282 struct ubus_request_data *req, const char *method,
283 struct blob_attr *msg)
285 char * const cmd[4] = { "/sbin/jffs2reset", "-y", "-r", NULL };
288 /* wait for the RPC call to complete */
290 return execv(cmd[0], cmd);
297 rpc_sys_reboot(struct ubus_context *ctx, struct ubus_object *obj,
298 struct ubus_request_data *req, const char *method,
299 struct blob_attr *msg)
313 rpc_sys_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
315 static const struct ubus_method sys_methods[] = {
316 UBUS_METHOD_NOARG("packagelist", rpc_sys_packagelist),
317 UBUS_METHOD("password_set", rpc_cgi_password_set, rpc_password_policy),
318 UBUS_METHOD_NOARG("upgrade_test", rpc_sys_upgrade_test),
319 UBUS_METHOD("upgrade_start", rpc_sys_upgrade_start,
321 UBUS_METHOD_NOARG("upgrade_clean", rpc_sys_upgrade_clean),
322 UBUS_METHOD_NOARG("factory", rpc_sys_factory),
323 UBUS_METHOD_NOARG("reboot", rpc_sys_reboot),
326 static struct ubus_object_type sys_type =
327 UBUS_OBJECT_TYPE("luci-rpc-sys", sys_methods);
329 static struct ubus_object obj = {
332 .methods = sys_methods,
333 .n_methods = ARRAY_SIZE(sys_methods),
338 return ubus_add_object(ctx, &obj);
341 struct rpc_plugin rpc_plugin = {
342 .init = rpc_sys_api_init