2 * rpcd - UBUS RPC server
4 * Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
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.
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.
21 #include <rpcd/exec.h>
22 #include <rpcd/plugin.h>
23 #include <rpcd/session.h>
24 #include <sys/reboot.h>
26 static const struct rpc_daemon_ops
*ops
;
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
},
44 static const struct blobmsg_policy rpc_upgrade_policy
[__RPC_UPGRADE_MAX
] = {
45 [RPC_UPGRADE_KEEP
] = { .name
= "keep", .type
= BLOBMSG_TYPE_BOOL
},
53 static const struct blobmsg_policy rpc_packagelist_policy
[__RPC_PACKAGELIST_MAX
] = {
54 [RPC_PACKAGELIST_ALL
] = { .name
= "all", .type
= BLOBMSG_TYPE_BOOL
},
58 rpc_errno_status(void)
63 return UBUS_STATUS_PERMISSION_DENIED
;
66 return UBUS_STATUS_INVALID_ARGUMENT
;
69 return UBUS_STATUS_NOT_FOUND
;
72 return UBUS_STATUS_INVALID_ARGUMENT
;
75 return UBUS_STATUS_UNKNOWN_ERROR
;
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
)
87 struct blob_attr
*tb
[__RPC_P_MAX
];
90 const char *const passwd
= "/bin/passwd";
91 const struct timespec ts
= {0, 100 * 1000 * 1000};
93 blobmsg_parse(rpc_password_policy
, __RPC_P_MAX
, tb
,
94 blob_data(msg
), blob_len(msg
));
96 if (!tb
[RPC_P_USER
] || !tb
[RPC_P_PASSWORD
])
97 return UBUS_STATUS_INVALID_ARGUMENT
;
100 return UBUS_STATUS_NOT_FOUND
;
102 if (!(s
.st_mode
& S_IXUSR
))
103 return UBUS_STATUS_PERMISSION_DENIED
;
106 return rpc_errno_status();
108 switch ((pid
= fork()))
113 return rpc_errno_status();
122 if ((fd
= open("/dev/null", O_RDWR
)) > -1)
131 return rpc_errno_status();
133 if (execl(passwd
, passwd
,
134 blobmsg_data(tb
[RPC_P_USER
]), NULL
))
135 return rpc_errno_status();
140 n
= write(fds
[1], blobmsg_data(tb
[RPC_P_PASSWORD
]),
141 blobmsg_data_len(tb
[RPC_P_PASSWORD
]) - 1);
143 return rpc_errno_status();
145 n
= write(fds
[1], "\n", 1);
147 return rpc_errno_status();
149 nanosleep(&ts
, NULL
);
151 n
= write(fds
[1], blobmsg_data(tb
[RPC_P_PASSWORD
]),
152 blobmsg_data_len(tb
[RPC_P_PASSWORD
]) - 1);
154 return rpc_errno_status();
155 n
= write(fds
[1], "\n", 1);
157 return rpc_errno_status();
161 waitpid(pid
, NULL
, 0);
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
)
172 struct blob_attr
*tb
[__RPC_PACKAGELIST_MAX
];
174 struct blob_buf buf
= { 0 };
175 char var
[256], pkg
[128] = { 0 }, ver
[128] = { 0 };
176 char *tmp
, *p1
, *p2
, *p3
;
179 blobmsg_parse(rpc_packagelist_policy
, __RPC_PACKAGELIST_MAX
, tb
,
180 blob_data(msg
), blob_len(msg
));
182 if (tb
[RPC_PACKAGELIST_ALL
] && blobmsg_get_bool(tb
[RPC_PACKAGELIST_ALL
]))
185 FILE *f
= fopen("/usr/lib/opkg/status", "r");
187 return UBUS_STATUS_NOT_FOUND
;
189 blob_buf_init(&buf
, 0);
190 tbl
= blobmsg_open_table(&buf
, "packages");
191 pkg
[0] = ver
[0] = '\0';
193 while(fgets(var
, sizeof(var
), f
)) {
194 p1
= strchr(var
, ' ');
200 p2
= strchr(p1
, ' ');
202 tmp
= strchr(p1
, '\n');
209 p3
= strchr(p2
, ' ');
211 tmp
= strchr(p2
, '\n');
218 tmp
= strchr(p3
, '\n');
226 if (!strcmp(var
, "Package:")) {
227 strncpy(pkg
, p1
, sizeof(pkg
) - 1);
231 /* If there is ABIVersion, remove that suffix */
232 if (!strcmp(var
, "ABIVersion:")) {
233 if (strlen(pkg
) <= strlen(p1
))
235 tmp
= &pkg
[strlen(pkg
) - strlen(p1
)];
236 if (strncmp(p1
, tmp
, strlen(p1
)))
243 if (!strcmp(var
, "Version:")) {
244 strncpy(ver
, p1
, sizeof(ver
) - 1);
249 !strcmp(var
, "Status:") &&
250 !strcmp(p1
, "install") &&
251 (all
|| strstr(p2
, "user")) &&
252 !strcmp(p3
, "installed") && pkg
[0] && ver
[0]) {
253 blobmsg_add_string(&buf
, pkg
, ver
);
254 pkg
[0] = ver
[0] = '\0';
258 blobmsg_close_table(&buf
, tbl
);
259 ubus_send_reply(ctx
, req
, buf
.head
);
267 rpc_sys_upgrade_test(struct ubus_context
*ctx
, struct ubus_object
*obj
,
268 struct ubus_request_data
*req
, const char *method
,
269 struct blob_attr
*msg
)
271 const char *cmd
[4] = { "sysupgrade", "--test", "/tmp/firmware.bin", NULL
};
272 return ops
->exec(cmd
, NULL
, NULL
, NULL
, NULL
, NULL
, ctx
, req
);
276 rpc_sys_upgrade_start(struct ubus_context
*ctx
, struct ubus_object
*obj
,
277 struct ubus_request_data
*req
, const char *method
,
278 struct blob_attr
*msg
)
280 struct blob_attr
*tb
[__RPC_UPGRADE_MAX
];
281 char * const cmd
[4] = { "/sbin/sysupgrade", "-n", "/tmp/firmware.bin", NULL
};
282 char * const cmd_keep
[3] = { "/sbin/sysupgrade", "/tmp/firmware.bin", NULL
};
283 char * const * c
= cmd
;
285 blobmsg_parse(rpc_upgrade_policy
, __RPC_UPGRADE_MAX
, tb
,
286 blob_data(msg
), blob_len(msg
));
288 if (tb
[RPC_UPGRADE_KEEP
] && blobmsg_get_bool(tb
[RPC_UPGRADE_KEEP
]))
292 /* wait for the RPC call to complete */
294 return execv(c
[0], c
);
301 rpc_sys_upgrade_clean(struct ubus_context
*ctx
, struct ubus_object
*obj
,
302 struct ubus_request_data
*req
, const char *method
,
303 struct blob_attr
*msg
)
305 if (unlink("/tmp/firmware.bin"))
306 return rpc_errno_status();
312 rpc_sys_factory(struct ubus_context
*ctx
, struct ubus_object
*obj
,
313 struct ubus_request_data
*req
, const char *method
,
314 struct blob_attr
*msg
)
316 char * const cmd
[4] = { "/sbin/jffs2reset", "-y", "-r", NULL
};
319 /* wait for the RPC call to complete */
321 return execv(cmd
[0], cmd
);
328 rpc_sys_reboot(struct ubus_context
*ctx
, struct ubus_object
*obj
,
329 struct ubus_request_data
*req
, const char *method
,
330 struct blob_attr
*msg
)
344 rpc_sys_api_init(const struct rpc_daemon_ops
*o
, struct ubus_context
*ctx
)
346 static const struct ubus_method sys_methods
[] = {
347 UBUS_METHOD("packagelist", rpc_sys_packagelist
, rpc_packagelist_policy
),
348 UBUS_METHOD("password_set", rpc_cgi_password_set
, rpc_password_policy
),
349 UBUS_METHOD_NOARG("upgrade_test", rpc_sys_upgrade_test
),
350 UBUS_METHOD("upgrade_start", rpc_sys_upgrade_start
,
352 UBUS_METHOD_NOARG("upgrade_clean", rpc_sys_upgrade_clean
),
353 UBUS_METHOD_NOARG("factory", rpc_sys_factory
),
354 UBUS_METHOD_NOARG("reboot", rpc_sys_reboot
),
357 static struct ubus_object_type sys_type
=
358 UBUS_OBJECT_TYPE("rpcd-plugin-sys", sys_methods
);
360 static struct ubus_object obj
= {
363 .methods
= sys_methods
,
364 .n_methods
= ARRAY_SIZE(sys_methods
),
369 return ubus_add_object(ctx
, &obj
);
372 struct rpc_plugin rpc_plugin
= {
373 .init
= rpc_sys_api_init