cmake: Find libubox/blobmsg_json.h
[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_upgrade_test(struct ubus_context *ctx, struct ubus_object *obj,
158 struct ubus_request_data *req, const char *method,
159 struct blob_attr *msg)
160 {
161 const char *cmd[4] = { "sysupgrade", "--test", "/tmp/firmware.bin", NULL };
162 return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
163 }
164
165 static int
166 rpc_sys_upgrade_start(struct ubus_context *ctx, struct ubus_object *obj,
167 struct ubus_request_data *req, const char *method,
168 struct blob_attr *msg)
169 {
170 struct blob_attr *tb[__RPC_UPGRADE_MAX];
171 char * const cmd[4] = { "/sbin/sysupgrade", "-n", "/tmp/firmware.bin", NULL };
172 char * const cmd_keep[3] = { "/sbin/sysupgrade", "/tmp/firmware.bin", NULL };
173 char * const * c = cmd;
174
175 blobmsg_parse(rpc_upgrade_policy, __RPC_UPGRADE_MAX, tb,
176 blob_data(msg), blob_len(msg));
177
178 if (tb[RPC_UPGRADE_KEEP] && blobmsg_get_bool(tb[RPC_UPGRADE_KEEP]))
179 c = cmd_keep;
180
181 if (!fork()) {
182 /* wait for the RPC call to complete */
183 sleep(2);
184 return execv(c[0], c);
185 }
186
187 return 0;
188 }
189
190 static int
191 rpc_sys_upgrade_clean(struct ubus_context *ctx, struct ubus_object *obj,
192 struct ubus_request_data *req, const char *method,
193 struct blob_attr *msg)
194 {
195 if (unlink("/tmp/firmware.bin"))
196 return rpc_errno_status();
197
198 return 0;
199 }
200
201 static int
202 rpc_sys_factory(struct ubus_context *ctx, struct ubus_object *obj,
203 struct ubus_request_data *req, const char *method,
204 struct blob_attr *msg)
205 {
206 char * const cmd[4] = { "/sbin/jffs2reset", "-y", "-r", NULL };
207
208 if (!fork()) {
209 /* wait for the RPC call to complete */
210 sleep(2);
211 return execv(cmd[0], cmd);
212 }
213
214 return 0;
215 }
216
217 static int
218 rpc_sys_reboot(struct ubus_context *ctx, struct ubus_object *obj,
219 struct ubus_request_data *req, const char *method,
220 struct blob_attr *msg)
221 {
222 if (!fork()) {
223 sync();
224 sleep(2);
225 reboot(RB_AUTOBOOT);
226 while (1)
227 ;
228 }
229
230 return 0;
231 }
232
233 static int
234 rpc_sys_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
235 {
236 static const struct ubus_method sys_methods[] = {
237 UBUS_METHOD("password_set", rpc_cgi_password_set, rpc_password_policy),
238 UBUS_METHOD_NOARG("upgrade_test", rpc_sys_upgrade_test),
239 UBUS_METHOD("upgrade_start", rpc_sys_upgrade_start,
240 rpc_upgrade_policy),
241 UBUS_METHOD_NOARG("upgrade_clean", rpc_sys_upgrade_clean),
242 UBUS_METHOD_NOARG("factory", rpc_sys_factory),
243 UBUS_METHOD_NOARG("reboot", rpc_sys_reboot),
244 };
245
246 static struct ubus_object_type sys_type =
247 UBUS_OBJECT_TYPE("luci-rpc-sys", sys_methods);
248
249 static struct ubus_object obj = {
250 .name = "rpc-sys",
251 .type = &sys_type,
252 .methods = sys_methods,
253 .n_methods = ARRAY_SIZE(sys_methods),
254 };
255
256 ops = o;
257
258 return ubus_add_object(ctx, &obj);
259 }
260
261 struct rpc_plugin rpc_plugin = {
262 .init = rpc_sys_api_init
263 };