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