* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+#include <stdbool.h>
#include <libubus.h>
#include <rpcd/exec.h>
[RPC_UPGRADE_KEEP] = { .name = "keep", .type = BLOBMSG_TYPE_BOOL },
};
+enum {
+ RPC_PACKAGELIST_ALL,
+ __RPC_PACKAGELIST_MAX
+};
+
+static const struct blobmsg_policy rpc_packagelist_policy[__RPC_PACKAGELIST_MAX] = {
+ [RPC_PACKAGELIST_ALL] = { .name = "all", .type = BLOBMSG_TYPE_BOOL },
+};
+
static int
rpc_errno_status(void)
{
struct blob_attr *tb[__RPC_P_MAX];
ssize_t n;
int ret;
+ const char *const passwd = "/bin/passwd";
+ const struct timespec ts = {0, 100 * 1000 * 1000};
blobmsg_parse(rpc_password_policy, __RPC_P_MAX, tb,
blob_data(msg), blob_len(msg));
if (!tb[RPC_P_USER] || !tb[RPC_P_PASSWORD])
return UBUS_STATUS_INVALID_ARGUMENT;
- if (stat("/usr/bin/passwd", &s))
+ if (stat(passwd, &s))
return UBUS_STATUS_NOT_FOUND;
if (!(s.st_mode & S_IXUSR))
if (ret < 0)
return rpc_errno_status();
- if (execl("/usr/bin/passwd", "/usr/bin/passwd",
+ if (execl(passwd, passwd,
blobmsg_data(tb[RPC_P_USER]), NULL))
return rpc_errno_status();
if (n < 0)
return rpc_errno_status();
- usleep(100 * 1000);
+ nanosleep(&ts, NULL);
n = write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
}
}
+static bool
+is_field(const char *type, const char *line)
+{
+ return strncmp(line, type, strlen(type)) == 0;
+}
+
+static bool
+is_blank(const char *line)
+{
+ for (; *line; line++)
+ if (!isspace(*line))
+ return false;
+ return true;
+}
+
static int
rpc_sys_packagelist(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
+ struct blob_attr *tb[__RPC_PACKAGELIST_MAX];
+ bool all = false, installed = false, auto_installed = false;
struct blob_buf buf = { 0 };
- char var[256], pkg[128], ver[128];
- char *tmp, *p1, *p2, *p3;
+ char line[256], tmp[128], pkg[128], ver[128];
+ char *pkg_abi;
void *tbl;
+ blobmsg_parse(rpc_packagelist_policy, __RPC_PACKAGELIST_MAX, tb,
+ blob_data(msg), blob_len(msg));
+
+ if (tb[RPC_PACKAGELIST_ALL] && blobmsg_get_bool(tb[RPC_PACKAGELIST_ALL]))
+ all = true;
+
FILE *f = fopen("/usr/lib/opkg/status", "r");
if (!f)
return UBUS_STATUS_NOT_FOUND;
tbl = blobmsg_open_table(&buf, "packages");
pkg[0] = ver[0] = '\0';
- while(fgets(var, sizeof(var), f)) {
- p1 = strchr(var, ' ');
- p2 = p3 = NULL;
- if (!p1)
- goto procstr;
-
- *p1++ = '\0';
- p2 = strchr(p1, ' ');
- if (!p2) {
- tmp = strchr(p1, '\n');
- if (tmp)
- *tmp = '\0';
- goto procstr;
- }
-
- *p2++ = '\0';
- p3 = strchr(p2, ' ');
- if (!p3) {
- tmp = strchr(p2, '\n');
- if (tmp)
- *tmp = '\0';
- goto procstr;
- }
-
- *p3++ = '\0';
- tmp = strchr(p3, '\n');
- if (tmp)
- *tmp = '\0';
-
-procstr:
- if (!p1)
- continue;
-
- if (!strcmp(var, "Package:")) {
- strncpy(pkg, p1, sizeof(pkg));
- continue;
- }
-
- if (!strcmp(var, "Version:")) {
- strncpy(ver, p1, sizeof(ver));
- continue;
- }
-
- if (p2 && p3 &&
- !strcmp(var, "Status:") &&
- !strcmp(p1, "install") &&
- !strcmp(p2, "user") &&
- !strcmp(p3, "installed") && pkg[0] && ver[0]) {
- blobmsg_add_string(&buf, pkg, ver);
- pkg[0] = ver[0] = '\0';
+ while (fgets(line, sizeof(line), f)) {
+ switch (line[0]) {
+ case 'A':
+ if (is_field("ABIVersion", line)) {
+ /* if there is ABIVersion, remove that suffix */
+ if (sscanf(line, "ABIVersion: %127s", tmp) == 1
+ && strlen(tmp) < strlen(pkg)) {
+ pkg_abi = pkg + (strlen(pkg) - strlen(tmp));
+ if (strncmp(pkg_abi, tmp, strlen(tmp)) == 0)
+ pkg_abi[0] = '\0';
+ }
+ } else if (is_field("Auto-Installed", line))
+ if (sscanf(line, "Auto-Installed: %63s", tmp) == 1)
+ auto_installed = (strcmp(tmp, "yes") == 0);
+ break;
+ case 'P':
+ if (is_field("Package", line))
+ if (sscanf(line, "Package: %127s", pkg) != 1)
+ pkg[0] = '\0';
+ break;
+ case 'V':
+ if (is_field("Version", line))
+ if (sscanf(line, "Version: %127s", ver) != 1)
+ ver[0] = '\0';
+ break;
+ case 'S':
+ if (is_field("Status", line))
+ if (sscanf(line, "Status: install %63s installed", tmp) == 1)
+ installed = true;
+ break;
+ default:
+ if (is_blank(line)) {
+ if (installed && (all || !auto_installed) && pkg[0] && ver[0])
+ blobmsg_add_string(&buf, pkg, ver);
+ pkg[0] = ver[0] = '\0';
+ installed = auto_installed = false;
+ }
+ break;
}
}
rpc_sys_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
{
static const struct ubus_method sys_methods[] = {
- UBUS_METHOD_NOARG("packagelist", rpc_sys_packagelist),
+ UBUS_METHOD("packagelist", rpc_sys_packagelist, rpc_packagelist_policy),
UBUS_METHOD("password_set", rpc_cgi_password_set, rpc_password_policy),
UBUS_METHOD_NOARG("upgrade_test", rpc_sys_upgrade_test),
UBUS_METHOD("upgrade_start", rpc_sys_upgrade_start,
};
static struct ubus_object_type sys_type =
- UBUS_OBJECT_TYPE("luci-rpc-sys", sys_methods);
+ UBUS_OBJECT_TYPE("rpcd-plugin-sys", sys_methods);
static struct ubus_object obj = {
.name = "rpc-sys",