sys: fix memory leak in packagelist
[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 blob_buf_free(&buf);
230 fclose(f);
231
232 return 0;
233 }
234
235 static int
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)
239 {
240 const char *cmd[4] = { "sysupgrade", "--test", "/tmp/firmware.bin", NULL };
241 return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
242 }
243
244 static int
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)
248 {
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;
253
254 blobmsg_parse(rpc_upgrade_policy, __RPC_UPGRADE_MAX, tb,
255 blob_data(msg), blob_len(msg));
256
257 if (tb[RPC_UPGRADE_KEEP] && blobmsg_get_bool(tb[RPC_UPGRADE_KEEP]))
258 c = cmd_keep;
259
260 if (!fork()) {
261 /* wait for the RPC call to complete */
262 sleep(2);
263 return execv(c[0], c);
264 }
265
266 return 0;
267 }
268
269 static int
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)
273 {
274 if (unlink("/tmp/firmware.bin"))
275 return rpc_errno_status();
276
277 return 0;
278 }
279
280 static int
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)
284 {
285 char * const cmd[4] = { "/sbin/jffs2reset", "-y", "-r", NULL };
286
287 if (!fork()) {
288 /* wait for the RPC call to complete */
289 sleep(2);
290 return execv(cmd[0], cmd);
291 }
292
293 return 0;
294 }
295
296 static int
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)
300 {
301 if (!fork()) {
302 sync();
303 sleep(2);
304 reboot(RB_AUTOBOOT);
305 while (1)
306 ;
307 }
308
309 return 0;
310 }
311
312 static int
313 rpc_sys_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
314 {
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,
320 rpc_upgrade_policy),
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),
324 };
325
326 static struct ubus_object_type sys_type =
327 UBUS_OBJECT_TYPE("luci-rpc-sys", sys_methods);
328
329 static struct ubus_object obj = {
330 .name = "rpc-sys",
331 .type = &sys_type,
332 .methods = sys_methods,
333 .n_methods = ARRAY_SIZE(sys_methods),
334 };
335
336 ops = o;
337
338 return ubus_add_object(ctx, &obj);
339 }
340
341 struct rpc_plugin rpc_plugin = {
342 .init = rpc_sys_api_init
343 };