4f008bab56a9477bb0e5edcdc81f95ce827e51bb
[project/rpcd.git] / sys.c
1 /*
2 * rpcd - UBUS RPC server
3 *
4 * Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
5 *
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.
9 *
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.
17 */
18
19 #include <libubus.h>
20
21 #include <rpcd/exec.h>
22 #include <rpcd/plugin.h>
23 #include <rpcd/session.h>
24 #include <sys/reboot.h>
25
26 static const struct rpc_daemon_ops *ops;
27
28 enum {
29 RPC_P_USER,
30 RPC_P_PASSWORD,
31 __RPC_P_MAX
32 };
33
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 },
37 };
38
39 enum {
40 RPC_UPGRADE_KEEP,
41 __RPC_UPGRADE_MAX
42 };
43
44 static const struct blobmsg_policy rpc_upgrade_policy[__RPC_UPGRADE_MAX] = {
45 [RPC_UPGRADE_KEEP] = { .name = "keep", .type = BLOBMSG_TYPE_BOOL },
46 };
47
48 static int
49 rpc_errno_status(void)
50 {
51 switch (errno)
52 {
53 case EACCES:
54 return UBUS_STATUS_PERMISSION_DENIED;
55
56 case ENOTDIR:
57 return UBUS_STATUS_INVALID_ARGUMENT;
58
59 case ENOENT:
60 return UBUS_STATUS_NOT_FOUND;
61
62 case EINVAL:
63 return UBUS_STATUS_INVALID_ARGUMENT;
64
65 default:
66 return UBUS_STATUS_UNKNOWN_ERROR;
67 }
68 }
69
70 static int
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)
74 {
75 pid_t pid;
76 int fd, fds[2];
77 struct stat s;
78 struct blob_attr *tb[__RPC_P_MAX];
79 ssize_t n;
80 int ret;
81
82 blobmsg_parse(rpc_password_policy, __RPC_P_MAX, tb,
83 blob_data(msg), blob_len(msg));
84
85 if (!tb[RPC_P_USER] || !tb[RPC_P_PASSWORD])
86 return UBUS_STATUS_INVALID_ARGUMENT;
87
88 if (stat("/usr/bin/passwd", &s))
89 return UBUS_STATUS_NOT_FOUND;
90
91 if (!(s.st_mode & S_IXUSR))
92 return UBUS_STATUS_PERMISSION_DENIED;
93
94 if (pipe(fds))
95 return rpc_errno_status();
96
97 switch ((pid = fork()))
98 {
99 case -1:
100 close(fds[0]);
101 close(fds[1]);
102 return rpc_errno_status();
103
104 case 0:
105 uloop_done();
106
107 dup2(fds[0], 0);
108 close(fds[0]);
109 close(fds[1]);
110
111 if ((fd = open("/dev/null", O_RDWR)) > -1)
112 {
113 dup2(fd, 1);
114 dup2(fd, 2);
115 close(fd);
116 }
117
118 ret = chdir("/");
119 if (ret < 0)
120 return rpc_errno_status();
121
122 if (execl("/usr/bin/passwd", "/usr/bin/passwd",
123 blobmsg_data(tb[RPC_P_USER]), NULL))
124 return rpc_errno_status();
125
126 default:
127 close(fds[0]);
128
129 n = write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
130 blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
131 if (n < 0)
132 return rpc_errno_status();
133
134 n = write(fds[1], "\n", 1);
135 if (n < 0)
136 return rpc_errno_status();
137
138 usleep(100 * 1000);
139
140 n = write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
141 blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
142 if (n < 0)
143 return rpc_errno_status();
144 n = write(fds[1], "\n", 1);
145 if (n < 0)
146 return rpc_errno_status();
147
148 close(fds[1]);
149
150 waitpid(pid, NULL, 0);
151
152 return 0;
153 }
154 }
155
156 static int
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)
160 {
161 struct blob_buf buf = { 0 };
162 char var[256], pkg[128], ver[128];
163 char *tmp, *p1, *p2, *p3;
164 void *tbl;
165
166 FILE *f = fopen("/usr/lib/opkg/status", "r");
167 if (!f)
168 return UBUS_STATUS_NOT_FOUND;
169
170 blob_buf_init(&buf, 0);
171 tbl = blobmsg_open_table(&buf, "packages");
172 pkg[0] = ver[0] = '\0';
173
174 while(fgets(var, sizeof(var), f)) {
175 p1 = strchr(var, ' ');
176 p2 = p3 = NULL;
177 if (!p1)
178 goto procstr;
179
180 *p1++ = '\0';
181 p2 = strchr(p1, ' ');
182 if (!p2) {
183 tmp = strchr(p1, '\n');
184 if (tmp)
185 *tmp = '\0';
186 goto procstr;
187 }
188
189 *p2++ = '\0';
190 p3 = strchr(p2, ' ');
191 if (!p3) {
192 tmp = strchr(p2, '\n');
193 if (tmp)
194 *tmp = '\0';
195 goto procstr;
196 }
197
198 *p3++ = '\0';
199 tmp = strchr(p3, '\n');
200 if (tmp)
201 *tmp = '\0';
202
203 procstr:
204 if (!p1)
205 continue;
206
207 if (!strcmp(var, "Package:")) {
208 strncpy(pkg, p1, sizeof(pkg));
209 continue;
210 }
211
212 if (!strcmp(var, "Version:")) {
213 strncpy(ver, p1, sizeof(ver));
214 continue;
215 }
216
217 if (p2 && p3 &&
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';
224 }
225 }
226
227 blobmsg_close_table(&buf, tbl);
228 ubus_send_reply(ctx, req, buf.head);
229 fclose(f);
230
231 return 0;
232 }
233
234 static int
235 rpc_sys_upgrade_test(struct ubus_context *ctx, struct ubus_object *obj,
236 struct ubus_request_data *req, const char *method,
237 struct blob_attr *msg)
238 {
239 const char *cmd[4] = { "sysupgrade", "--test", "/tmp/firmware.bin", NULL };
240 return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
241 }
242
243 static int
244 rpc_sys_upgrade_start(struct ubus_context *ctx, struct ubus_object *obj,
245 struct ubus_request_data *req, const char *method,
246 struct blob_attr *msg)
247 {
248 struct blob_attr *tb[__RPC_UPGRADE_MAX];
249 char * const cmd[4] = { "/sbin/sysupgrade", "-n", "/tmp/firmware.bin", NULL };
250 char * const cmd_keep[3] = { "/sbin/sysupgrade", "/tmp/firmware.bin", NULL };
251 char * const * c = cmd;
252
253 blobmsg_parse(rpc_upgrade_policy, __RPC_UPGRADE_MAX, tb,
254 blob_data(msg), blob_len(msg));
255
256 if (tb[RPC_UPGRADE_KEEP] && blobmsg_get_bool(tb[RPC_UPGRADE_KEEP]))
257 c = cmd_keep;
258
259 if (!fork()) {
260 /* wait for the RPC call to complete */
261 sleep(2);
262 return execv(c[0], c);
263 }
264
265 return 0;
266 }
267
268 static int
269 rpc_sys_upgrade_clean(struct ubus_context *ctx, struct ubus_object *obj,
270 struct ubus_request_data *req, const char *method,
271 struct blob_attr *msg)
272 {
273 if (unlink("/tmp/firmware.bin"))
274 return rpc_errno_status();
275
276 return 0;
277 }
278
279 static int
280 rpc_sys_factory(struct ubus_context *ctx, struct ubus_object *obj,
281 struct ubus_request_data *req, const char *method,
282 struct blob_attr *msg)
283 {
284 char * const cmd[4] = { "/sbin/jffs2reset", "-y", "-r", NULL };
285
286 if (!fork()) {
287 /* wait for the RPC call to complete */
288 sleep(2);
289 return execv(cmd[0], cmd);
290 }
291
292 return 0;
293 }
294
295 static int
296 rpc_sys_reboot(struct ubus_context *ctx, struct ubus_object *obj,
297 struct ubus_request_data *req, const char *method,
298 struct blob_attr *msg)
299 {
300 if (!fork()) {
301 sync();
302 sleep(2);
303 reboot(RB_AUTOBOOT);
304 while (1)
305 ;
306 }
307
308 return 0;
309 }
310
311 static int
312 rpc_sys_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
313 {
314 static const struct ubus_method sys_methods[] = {
315 UBUS_METHOD_NOARG("packagelist", rpc_sys_packagelist),
316 UBUS_METHOD("password_set", rpc_cgi_password_set, rpc_password_policy),
317 UBUS_METHOD_NOARG("upgrade_test", rpc_sys_upgrade_test),
318 UBUS_METHOD("upgrade_start", rpc_sys_upgrade_start,
319 rpc_upgrade_policy),
320 UBUS_METHOD_NOARG("upgrade_clean", rpc_sys_upgrade_clean),
321 UBUS_METHOD_NOARG("factory", rpc_sys_factory),
322 UBUS_METHOD_NOARG("reboot", rpc_sys_reboot),
323 };
324
325 static struct ubus_object_type sys_type =
326 UBUS_OBJECT_TYPE("luci-rpc-sys", sys_methods);
327
328 static struct ubus_object obj = {
329 .name = "rpc-sys",
330 .type = &sys_type,
331 .methods = sys_methods,
332 .n_methods = ARRAY_SIZE(sys_methods),
333 };
334
335 ops = o;
336
337 return ubus_add_object(ctx, &obj);
338 }
339
340 struct rpc_plugin rpc_plugin = {
341 .init = rpc_sys_api_init
342 };