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];
81 const char *const passwd = "/bin/passwd";
83 blobmsg_parse(rpc_password_policy, __RPC_P_MAX, tb,
84 blob_data(msg), blob_len(msg));
86 if (!tb[RPC_P_USER] || !tb[RPC_P_PASSWORD])
87 return UBUS_STATUS_INVALID_ARGUMENT;
90 return UBUS_STATUS_NOT_FOUND;
92 if (!(s.st_mode & S_IXUSR))
93 return UBUS_STATUS_PERMISSION_DENIED;
96 return rpc_errno_status();
98 switch ((pid = fork()))
103 return rpc_errno_status();
112 if ((fd = open("/dev/null", O_RDWR)) > -1)
121 return rpc_errno_status();
123 if (execl(passwd, passwd,
124 blobmsg_data(tb[RPC_P_USER]), NULL))
125 return rpc_errno_status();
130 n = write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
131 blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
133 return rpc_errno_status();
135 n = write(fds[1], "\n", 1);
137 return rpc_errno_status();
141 n = write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
142 blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
144 return rpc_errno_status();
145 n = write(fds[1], "\n", 1);
147 return rpc_errno_status();
151 waitpid(pid, NULL, 0);
158 rpc_sys_packagelist(struct ubus_context *ctx, struct ubus_object *obj,
159 struct ubus_request_data *req, const char *method,
160 struct blob_attr *msg)
162 struct blob_buf buf = { 0 };
163 char var[256], pkg[128], ver[128];
164 char *tmp, *p1, *p2, *p3;
167 FILE *f = fopen("/usr/lib/opkg/status", "r");
169 return UBUS_STATUS_NOT_FOUND;
171 blob_buf_init(&buf, 0);
172 tbl = blobmsg_open_table(&buf, "packages");
173 pkg[0] = ver[0] = '\0';
175 while(fgets(var, sizeof(var), f)) {
176 p1 = strchr(var, ' ');
182 p2 = strchr(p1, ' ');
184 tmp = strchr(p1, '\n');
191 p3 = strchr(p2, ' ');
193 tmp = strchr(p2, '\n');
200 tmp = strchr(p3, '\n');
208 if (!strcmp(var, "Package:")) {
209 strncpy(pkg, p1, sizeof(pkg));
213 if (!strcmp(var, "Version:")) {
214 strncpy(ver, p1, sizeof(ver));
219 !strcmp(var, "Status:") &&
220 !strcmp(p1, "install") &&
221 !strcmp(p2, "user") &&
222 !strcmp(p3, "installed") && pkg[0] && ver[0]) {
223 blobmsg_add_string(&buf, pkg, ver);
224 pkg[0] = ver[0] = '\0';
228 blobmsg_close_table(&buf, tbl);
229 ubus_send_reply(ctx, req, buf.head);
237 rpc_sys_upgrade_test(struct ubus_context *ctx, struct ubus_object *obj,
238 struct ubus_request_data *req, const char *method,
239 struct blob_attr *msg)
241 const char *cmd[4] = { "sysupgrade", "--test", "/tmp/firmware.bin", NULL };
242 return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
246 rpc_sys_upgrade_start(struct ubus_context *ctx, struct ubus_object *obj,
247 struct ubus_request_data *req, const char *method,
248 struct blob_attr *msg)
250 struct blob_attr *tb[__RPC_UPGRADE_MAX];
251 char * const cmd[4] = { "/sbin/sysupgrade", "-n", "/tmp/firmware.bin", NULL };
252 char * const cmd_keep[3] = { "/sbin/sysupgrade", "/tmp/firmware.bin", NULL };
253 char * const * c = cmd;
255 blobmsg_parse(rpc_upgrade_policy, __RPC_UPGRADE_MAX, tb,
256 blob_data(msg), blob_len(msg));
258 if (tb[RPC_UPGRADE_KEEP] && blobmsg_get_bool(tb[RPC_UPGRADE_KEEP]))
262 /* wait for the RPC call to complete */
264 return execv(c[0], c);
271 rpc_sys_upgrade_clean(struct ubus_context *ctx, struct ubus_object *obj,
272 struct ubus_request_data *req, const char *method,
273 struct blob_attr *msg)
275 if (unlink("/tmp/firmware.bin"))
276 return rpc_errno_status();
282 rpc_sys_factory(struct ubus_context *ctx, struct ubus_object *obj,
283 struct ubus_request_data *req, const char *method,
284 struct blob_attr *msg)
286 char * const cmd[4] = { "/sbin/jffs2reset", "-y", "-r", NULL };
289 /* wait for the RPC call to complete */
291 return execv(cmd[0], cmd);
298 rpc_sys_reboot(struct ubus_context *ctx, struct ubus_object *obj,
299 struct ubus_request_data *req, const char *method,
300 struct blob_attr *msg)
314 rpc_sys_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
316 static const struct ubus_method sys_methods[] = {
317 UBUS_METHOD_NOARG("packagelist", rpc_sys_packagelist),
318 UBUS_METHOD("password_set", rpc_cgi_password_set, rpc_password_policy),
319 UBUS_METHOD_NOARG("upgrade_test", rpc_sys_upgrade_test),
320 UBUS_METHOD("upgrade_start", rpc_sys_upgrade_start,
322 UBUS_METHOD_NOARG("upgrade_clean", rpc_sys_upgrade_clean),
323 UBUS_METHOD_NOARG("factory", rpc_sys_factory),
324 UBUS_METHOD_NOARG("reboot", rpc_sys_reboot),
327 static struct ubus_object_type sys_type =
328 UBUS_OBJECT_TYPE("luci-rpc-sys", sys_methods);
330 static struct ubus_object obj = {
333 .methods = sys_methods,
334 .n_methods = ARRAY_SIZE(sys_methods),
339 return ubus_add_object(ctx, &obj);
342 struct rpc_plugin rpc_plugin = {
343 .init = rpc_sys_api_init