add a system object that allows us to set passwords and trigger sysupgrade
[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
80         blobmsg_parse(rpc_password_policy, __RPC_P_MAX, tb,
81                       blob_data(msg), blob_len(msg));
82
83         if (!tb[RPC_P_USER] || !tb[RPC_P_PASSWORD])
84                 return UBUS_STATUS_INVALID_ARGUMENT;
85
86         if (stat("/usr/bin/passwd", &s))
87                 return UBUS_STATUS_NOT_FOUND;
88
89         if (!(s.st_mode & S_IXUSR))
90                 return UBUS_STATUS_PERMISSION_DENIED;
91
92         if (pipe(fds))
93                 return rpc_errno_status();
94
95         switch ((pid = fork()))
96         {
97         case -1:
98                 close(fds[0]);
99                 close(fds[1]);
100                 return rpc_errno_status();
101
102         case 0:
103                 uloop_done();
104
105                 dup2(fds[0], 0);
106                 close(fds[0]);
107                 close(fds[1]);
108
109                 if ((fd = open("/dev/null", O_RDWR)) > -1)
110                 {
111                         dup2(fd, 1);
112                         dup2(fd, 2);
113                         close(fd);
114                 }
115
116                 chdir("/");
117
118                 if (execl("/usr/bin/passwd", "/usr/bin/passwd",
119                           blobmsg_data(tb[RPC_P_USER]), NULL))
120                         return rpc_errno_status();
121
122         default:
123                 close(fds[0]);
124
125                 write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
126                               blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
127                 write(fds[1], "\n", 1);
128
129                 usleep(100 * 1000);
130
131                 write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
132                               blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
133                 write(fds[1], "\n", 1);
134
135                 close(fds[1]);
136
137                 waitpid(pid, NULL, 0);
138
139                 return 0;
140         }
141 }
142
143 static int
144 rpc_sys_upgrade_test(struct ubus_context *ctx, struct ubus_object *obj,
145                        struct ubus_request_data *req, const char *method,
146                        struct blob_attr *msg)
147 {
148         const char *cmd[4] = { "sysupgrade", "--test", "/tmp/firmware.bin", NULL };
149         return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
150 }
151
152 static int
153 rpc_sys_upgrade_start(struct ubus_context *ctx, struct ubus_object *obj,
154                         struct ubus_request_data *req, const char *method,
155                         struct blob_attr *msg)
156 {
157         struct blob_attr *tb[__RPC_UPGRADE_MAX];
158         char * const cmd[4] = { "/sbin/sysupgrade", "-n", "/tmp/firmware.bin", NULL };
159         char * const cmd_keep[3] = { "/sbin/sysupgrade", "/tmp/firmware.bin", NULL };
160         char * const * c = cmd;
161
162         blobmsg_parse(rpc_upgrade_policy, __RPC_UPGRADE_MAX, tb,
163                       blob_data(msg), blob_len(msg));
164
165         if (tb[RPC_UPGRADE_KEEP] && blobmsg_get_bool(tb[RPC_UPGRADE_KEEP]))
166                 c = cmd_keep;
167
168         if (!fork()) {
169                 /* wait for the RPC call to complete */
170                 sleep(2);
171                 return execv(c[0], c);
172         }
173
174         return 0;
175 }
176
177 static int
178 rpc_sys_upgrade_clean(struct ubus_context *ctx, struct ubus_object *obj,
179                         struct ubus_request_data *req, const char *method,
180                         struct blob_attr *msg)
181 {
182         if (unlink("/tmp/firmware.bin"))
183                 return rpc_errno_status();
184
185         return 0;
186 }
187
188 static int
189 rpc_sys_factory(struct ubus_context *ctx, struct ubus_object *obj,
190                  struct ubus_request_data *req, const char *method,
191                  struct blob_attr *msg)
192 {
193         char * const cmd[4] = { "/sbin/jffs2reset", "-y", "-r", NULL };
194
195         if (!fork()) {
196                 /* wait for the RPC call to complete */
197                 sleep(2);
198                 return execv(cmd[0], cmd);
199         }
200
201         return 0;
202 }
203
204 static int
205 rpc_sys_reboot(struct ubus_context *ctx, struct ubus_object *obj,
206                  struct ubus_request_data *req, const char *method,
207                  struct blob_attr *msg)
208 {
209         if (!fork()) {
210                 sync();
211                 sleep(2);
212                 reboot(RB_AUTOBOOT);
213                 while (1)
214                         ;
215         }
216
217         return 0;
218 }
219
220 static int
221 rpc_sys_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
222 {
223         static const struct ubus_method sys_methods[] = {
224                 UBUS_METHOD("password_set", rpc_cgi_password_set, rpc_password_policy),
225                 UBUS_METHOD_NOARG("upgrade_test", rpc_sys_upgrade_test),
226                 UBUS_METHOD("upgrade_start",      rpc_sys_upgrade_start,
227                                                   rpc_upgrade_policy),
228                 UBUS_METHOD_NOARG("upgrade_clean", rpc_sys_upgrade_clean),
229                 UBUS_METHOD_NOARG("factory", rpc_sys_factory),
230                 UBUS_METHOD_NOARG("reboot", rpc_sys_reboot),
231         };
232
233         static struct ubus_object_type sys_type =
234                 UBUS_OBJECT_TYPE("luci-rpc-sys", sys_methods);
235
236         static struct ubus_object obj = {
237                 .name = "rpc-sys",
238                 .type = &sys_type,
239                 .methods = sys_methods,
240                 .n_methods = ARRAY_SIZE(sys_methods),
241         };
242
243         ops = o;
244
245         return ubus_add_object(ctx, &obj);
246 }
247
248 struct rpc_plugin rpc_plugin = {
249         .init = rpc_sys_api_init
250 };