sys: fix passwd path
[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         const char *const passwd = "/bin/passwd";
82
83         blobmsg_parse(rpc_password_policy, __RPC_P_MAX, tb,
84                       blob_data(msg), blob_len(msg));
85
86         if (!tb[RPC_P_USER] || !tb[RPC_P_PASSWORD])
87                 return UBUS_STATUS_INVALID_ARGUMENT;
88
89         if (stat(passwd, &s))
90                 return UBUS_STATUS_NOT_FOUND;
91
92         if (!(s.st_mode & S_IXUSR))
93                 return UBUS_STATUS_PERMISSION_DENIED;
94
95         if (pipe(fds))
96                 return rpc_errno_status();
97
98         switch ((pid = fork()))
99         {
100         case -1:
101                 close(fds[0]);
102                 close(fds[1]);
103                 return rpc_errno_status();
104
105         case 0:
106                 uloop_done();
107
108                 dup2(fds[0], 0);
109                 close(fds[0]);
110                 close(fds[1]);
111
112                 if ((fd = open("/dev/null", O_RDWR)) > -1)
113                 {
114                         dup2(fd, 1);
115                         dup2(fd, 2);
116                         close(fd);
117                 }
118
119                 ret = chdir("/");
120                 if (ret < 0)
121                         return rpc_errno_status();
122
123                 if (execl(passwd, passwd,
124                           blobmsg_data(tb[RPC_P_USER]), NULL))
125                         return rpc_errno_status();
126
127         default:
128                 close(fds[0]);
129
130                 n = write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
131                               blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
132                 if (n < 0)
133                         return rpc_errno_status();
134
135                 n = write(fds[1], "\n", 1);
136                 if (n < 0)
137                         return rpc_errno_status();
138
139                 usleep(100 * 1000);
140
141                 n = write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
142                               blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
143                 if (n < 0)
144                         return rpc_errno_status();
145                 n = write(fds[1], "\n", 1);
146                 if (n < 0)
147                         return rpc_errno_status();
148
149                 close(fds[1]);
150
151                 waitpid(pid, NULL, 0);
152
153                 return 0;
154         }
155 }
156
157 static int
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)
161 {
162         struct blob_buf buf = { 0 };
163         char var[256], pkg[128], ver[128];
164         char *tmp, *p1, *p2, *p3;
165         void *tbl;
166
167         FILE *f = fopen("/usr/lib/opkg/status", "r");
168         if (!f)
169                 return UBUS_STATUS_NOT_FOUND;
170
171         blob_buf_init(&buf, 0);
172         tbl = blobmsg_open_table(&buf, "packages");
173         pkg[0] = ver[0] = '\0';
174
175         while(fgets(var, sizeof(var), f)) {
176                 p1 = strchr(var, ' ');
177                 p2 = p3 = NULL;
178                 if (!p1)
179                         goto procstr;
180
181                 *p1++ = '\0';
182                 p2 = strchr(p1, ' ');
183                 if (!p2) {
184                         tmp = strchr(p1, '\n');
185                         if (tmp)
186                                 *tmp = '\0';
187                         goto procstr;
188                 }
189
190                 *p2++ = '\0';
191                 p3 = strchr(p2, ' ');
192                 if (!p3) {
193                         tmp = strchr(p2, '\n');
194                         if (tmp)
195                                 *tmp = '\0';
196                         goto procstr;
197                 }
198
199                 *p3++ = '\0';
200                 tmp = strchr(p3, '\n');
201                 if (tmp)
202                         *tmp = '\0';
203
204 procstr:
205                 if (!p1)
206                         continue;
207
208                 if (!strcmp(var, "Package:")) {
209                         strncpy(pkg, p1, sizeof(pkg));
210                         continue;
211                 }
212
213                 if (!strcmp(var, "Version:")) {
214                         strncpy(ver, p1, sizeof(ver));
215                         continue;
216                 }
217
218                 if (p2 && p3 &&
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';
225                 }
226         }
227
228         blobmsg_close_table(&buf, tbl);
229         ubus_send_reply(ctx, req, buf.head);
230         blob_buf_free(&buf);
231         fclose(f);
232
233         return 0;
234 }
235
236 static int
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)
240 {
241         const char *cmd[4] = { "sysupgrade", "--test", "/tmp/firmware.bin", NULL };
242         return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
243 }
244
245 static int
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)
249 {
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;
254
255         blobmsg_parse(rpc_upgrade_policy, __RPC_UPGRADE_MAX, tb,
256                       blob_data(msg), blob_len(msg));
257
258         if (tb[RPC_UPGRADE_KEEP] && blobmsg_get_bool(tb[RPC_UPGRADE_KEEP]))
259                 c = cmd_keep;
260
261         if (!fork()) {
262                 /* wait for the RPC call to complete */
263                 sleep(2);
264                 return execv(c[0], c);
265         }
266
267         return 0;
268 }
269
270 static int
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)
274 {
275         if (unlink("/tmp/firmware.bin"))
276                 return rpc_errno_status();
277
278         return 0;
279 }
280
281 static int
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)
285 {
286         char * const cmd[4] = { "/sbin/jffs2reset", "-y", "-r", NULL };
287
288         if (!fork()) {
289                 /* wait for the RPC call to complete */
290                 sleep(2);
291                 return execv(cmd[0], cmd);
292         }
293
294         return 0;
295 }
296
297 static int
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)
301 {
302         if (!fork()) {
303                 sync();
304                 sleep(2);
305                 reboot(RB_AUTOBOOT);
306                 while (1)
307                         ;
308         }
309
310         return 0;
311 }
312
313 static int
314 rpc_sys_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
315 {
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,
321                                                   rpc_upgrade_policy),
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),
325         };
326
327         static struct ubus_object_type sys_type =
328                 UBUS_OBJECT_TYPE("luci-rpc-sys", sys_methods);
329
330         static struct ubus_object obj = {
331                 .name = "rpc-sys",
332                 .type = &sys_type,
333                 .methods = sys_methods,
334                 .n_methods = ARRAY_SIZE(sys_methods),
335         };
336
337         ops = o;
338
339         return ubus_add_object(ctx, &obj);
340 }
341
342 struct rpc_plugin rpc_plugin = {
343         .init = rpc_sys_api_init
344 };