• source navigation  • diff markup  • identifier search  • freetext search  • 

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt