From 0607cfda5b3449d0850861c140d36fbcc0f2ef1d Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Sat, 26 Jan 2013 15:29:09 +0100 Subject: [PATCH 1/1] initial commit --- CMakeLists.txt | 20 ++ file.c | 255 +++++++++++++++++++++++ file.h | 29 +++ main.c | 66 ++++++ session.c | 643 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ session.h | 54 +++++ 6 files changed, 1067 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 file.c create mode 100644 file.h create mode 100644 main.c create mode 100644 session.c create mode 100644 session.h diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..eef3856 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 2.6) + +PROJECT(luci-rpcd C) +ADD_DEFINITIONS(-Os -Wall -Werror --std=gnu99 -g3 -Wmissing-declarations) + +SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") + +IF(APPLE) + INCLUDE_DIRECTORIES(/opt/local/include) + LINK_DIRECTORIES(/opt/local/lib) +ENDIF() + +ADD_EXECUTABLE(luci-rpcd main.c session.c file.c) +TARGET_LINK_LIBRARIES(luci-rpcd ubox ubus) + +SET(CMAKE_INSTALL_PREFIX /usr) + +INSTALL(TARGETS luci-rpcd + RUNTIME DESTINATION sbin +) diff --git a/file.c b/file.c new file mode 100644 index 0000000..11a8123 --- /dev/null +++ b/file.c @@ -0,0 +1,255 @@ +/* + * luci-rpcd - LuCI UBUS RPC server + * + * Copyright (C) 2013 Jo-Philipp Wich + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include +#include + +#include "file.h" + +static struct blob_buf buf; + +enum { + RPC_F_PATH, + RPC_F_DATA, + __RPC_F_MAX, +}; + +static const struct blobmsg_policy file_policy[__RPC_F_MAX] = { + [RPC_F_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING }, + [RPC_F_DATA] = { .name = "data", .type = BLOBMSG_TYPE_STRING }, +}; + +static const char *d_types[] = { + [DT_BLK] = "block", + [DT_CHR] = "char", + [DT_DIR] = "directory", + [DT_FIFO] = "fifo", + [DT_LNK] = "symlink", + [DT_REG] = "file", + [DT_SOCK] = "socket", + [DT_UNKNOWN] = "unknown", +}; + + +static int +rpc_errno_status(void) +{ + switch (errno) + { + case EACCES: + return UBUS_STATUS_PERMISSION_DENIED; + + case ENOTDIR: + return UBUS_STATUS_INVALID_ARGUMENT; + + case ENOENT: + return UBUS_STATUS_NOT_FOUND; + + case EINVAL: + return UBUS_STATUS_INVALID_ARGUMENT; + + default: + return UBUS_STATUS_UNKNOWN_ERROR; + } +} + +static struct blob_attr ** +rpc_check_path(struct blob_attr *msg, char **path, struct stat *s) +{ + static struct blob_attr *tb[__RPC_F_MAX]; + + blobmsg_parse(file_policy, __RPC_F_MAX, tb, blob_data(msg), blob_len(msg)); + + if (!tb[RPC_F_PATH]) + { + errno = EINVAL; + return NULL; + } + + *path = blobmsg_data(tb[RPC_F_PATH]); + + if (stat(*path, s)) + return NULL; + + return tb; +} + +static int +rpc_handle_read(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + int fd, rlen; + char *path; + char buffer[RPC_FILE_MAX_SIZE]; + struct stat s; + + if (!rpc_check_path(msg, &path, &s)) + return rpc_errno_status(); + + if (s.st_size >= RPC_FILE_MAX_SIZE) + return UBUS_STATUS_NOT_SUPPORTED; + + if ((fd = open(path, O_RDONLY)) < 0) + return rpc_errno_status(); + + if ((rlen = read(fd, buffer, RPC_FILE_MAX_SIZE-1)) > 0) + buffer[rlen] = 0; + + close(fd); + + if (rlen <= 0) + return UBUS_STATUS_NO_DATA; + + blob_buf_init(&buf, 0); + blobmsg_add_string(&buf, "data", buffer); + ubus_send_reply(ctx, req, buf.head); + + return 0; +} + +static int +rpc_handle_write(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + int fd, rv; + char *path; + struct stat s; + struct blob_attr **tb; + + if (!(tb = rpc_check_path(msg, &path, &s))) + return rpc_errno_status(); + + if (!tb[RPC_F_DATA]) + return UBUS_STATUS_INVALID_ARGUMENT; + + if ((fd = open(path, O_WRONLY)) < 0) + return rpc_errno_status(); + + rv = write(fd, blobmsg_data(tb[RPC_F_DATA]), blobmsg_data_len(tb[RPC_F_DATA])); + + close(fd); + + if (rv <= 0) + return UBUS_STATUS_NO_DATA; + + return 0; +} + +static int +rpc_handle_list(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + DIR *fd; + void *c, *d; + char *path; + struct stat s; + struct dirent *e; + + if (!rpc_check_path(msg, &path, &s)) + return rpc_errno_status(); + + if ((fd = opendir(path)) == NULL) + return rpc_errno_status(); + + blob_buf_init(&buf, 0); + c = blobmsg_open_array(&buf, "entries"); + + while ((e = readdir(fd)) != NULL) + { + if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..")) + continue; + + d = blobmsg_open_table(&buf, NULL); + blobmsg_add_string(&buf, "name", e->d_name); + blobmsg_add_string(&buf, "type", d_types[e->d_type]); + blobmsg_close_table(&buf, d); + } + + blobmsg_close_array(&buf, c); + ubus_send_reply(ctx, req, buf.head); + + return 0; +} + +static int +rpc_handle_stat(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + int type; + char *path; + struct stat s; + + if (!rpc_check_path(msg, &path, &s)) + return rpc_errno_status(); + + blob_buf_init(&buf, 0); + + type = S_ISREG(s.st_mode) ? DT_REG : + S_ISDIR(s.st_mode) ? DT_DIR : + S_ISCHR(s.st_mode) ? DT_CHR : + S_ISBLK(s.st_mode) ? DT_BLK : + S_ISFIFO(s.st_mode) ? DT_FIFO : + S_ISLNK(s.st_mode) ? DT_LNK : + S_ISSOCK(s.st_mode) ? DT_SOCK : + DT_UNKNOWN; + + blobmsg_add_string(&buf, "path", path); + blobmsg_add_string(&buf, "type", d_types[type]); + blobmsg_add_u32(&buf, "size", s.st_size); + blobmsg_add_u32(&buf, "mode", s.st_mode); + blobmsg_add_u32(&buf, "atime", s.st_atime); + blobmsg_add_u32(&buf, "mtime", s.st_mtime); + blobmsg_add_u32(&buf, "ctime", s.st_ctime); + blobmsg_add_u32(&buf, "inode", s.st_ino); + blobmsg_add_u32(&buf, "uid", s.st_uid); + blobmsg_add_u32(&buf, "gid", s.st_gid); + + ubus_send_reply(ctx, req, buf.head); + + return 0; +} + + +int rpc_file_api_init(struct ubus_context *ctx) +{ + static const struct ubus_method file_methods[] = { + UBUS_METHOD("read", rpc_handle_read, file_policy), + UBUS_METHOD("write", rpc_handle_write, file_policy), + UBUS_METHOD("list", rpc_handle_list, file_policy), + UBUS_METHOD("stat", rpc_handle_stat, file_policy), + }; + + static struct ubus_object_type file_type = + UBUS_OBJECT_TYPE("luci-rpc-file", file_methods); + + static struct ubus_object obj = { + .name = "file", + .type = &file_type, + .methods = file_methods, + .n_methods = ARRAY_SIZE(file_methods), + }; + + return ubus_add_object(ctx, &obj); +} diff --git a/file.h b/file.h new file mode 100644 index 0000000..a00f2b1 --- /dev/null +++ b/file.h @@ -0,0 +1,29 @@ +/* + * luci-rpcd - LuCI UBUS RPC server + * + * Copyright (C) 2013 Jo-Philipp Wich + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef __RPC_FILE_H +#define __RPC_FILE_H + +#include +#include + +#define RPC_FILE_MAX_SIZE (1024 * 256) + +int rpc_file_api_init(struct ubus_context *ctx); + +#endif diff --git a/main.c b/main.c new file mode 100644 index 0000000..52725cd --- /dev/null +++ b/main.c @@ -0,0 +1,66 @@ +/* + * luci-rpcd - LuCI UBUS RPC server + * + * Copyright (C) 2013 Felix Fietkau + * Copyright (C) 2013 Jo-Philipp Wich + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#include +#include + +#include "session.h" +#include "file.h" + +static struct ubus_context *ctx; + +int main(int argc, char **argv) +{ + const char *ubus_socket = NULL; + int ch; + + while ((ch = getopt(argc, argv, "s:")) != -1) { + switch (ch) { + case 's': + ubus_socket = optarg; + break; + default: + break; + } + } + + argc -= optind; + argv += optind; + + uloop_init(); + + ctx = ubus_connect(ubus_socket); + if (!ctx) { + fprintf(stderr, "Failed to connect to ubus\n"); + return -1; + } + + ubus_add_uloop(ctx); + + rpc_session_api_init(ctx); + rpc_file_api_init(ctx); + + uloop_run(); + ubus_free(ctx); + uloop_done(); + + return 0; +} diff --git a/session.c b/session.c new file mode 100644 index 0000000..9c2b35d --- /dev/null +++ b/session.c @@ -0,0 +1,643 @@ +/* + * luci-rpcd - LuCI UBUS RPC server + * + * Copyright (C) 2013 Felix Fietkau + * Copyright (C) 2013 Jo-Philipp Wich + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include + +#include "session.h" + +static struct avl_tree sessions; +static struct blob_buf buf; + +static const struct blobmsg_policy new_policy = { + .name = "timeout", .type = BLOBMSG_TYPE_INT32 +}; + +static const struct blobmsg_policy sid_policy = { + .name = "sid", .type = BLOBMSG_TYPE_STRING +}; + +enum { + RPC_SS_SID, + RPC_SS_VALUES, + __RPC_SS_MAX, +}; +static const struct blobmsg_policy set_policy[__RPC_SS_MAX] = { + [RPC_SS_SID] = { .name = "sid", .type = BLOBMSG_TYPE_STRING }, + [RPC_SS_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE }, +}; + +enum { + RPC_SG_SID, + RPC_SG_KEYS, + __RPC_SG_MAX, +}; +static const struct blobmsg_policy get_policy[__RPC_SG_MAX] = { + [RPC_SG_SID] = { .name = "sid", .type = BLOBMSG_TYPE_STRING }, + [RPC_SG_KEYS] = { .name = "keys", .type = BLOBMSG_TYPE_ARRAY }, +}; + +enum { + RPC_SA_SID, + RPC_SA_OBJECTS, + __RPC_SA_MAX, +}; +static const struct blobmsg_policy acl_policy[__RPC_SA_MAX] = { + [RPC_SA_SID] = { .name = "sid", .type = BLOBMSG_TYPE_STRING }, + [RPC_SA_OBJECTS] = { .name = "objects", .type = BLOBMSG_TYPE_ARRAY }, +}; + +enum { + RPC_SP_SID, + RPC_SP_OBJECT, + RPC_SP_FUNCTION, + __RPC_SP_MAX, +}; +static const struct blobmsg_policy perm_policy[__RPC_SP_MAX] = { + [RPC_SP_SID] = { .name = "sid", .type = BLOBMSG_TYPE_STRING }, + [RPC_SP_OBJECT] = { .name = "object", .type = BLOBMSG_TYPE_STRING }, + [RPC_SP_FUNCTION] = { .name = "function", .type = BLOBMSG_TYPE_STRING }, +}; + +/* + * Keys in the AVL tree contain all pattern characters up to the first wildcard. + * To look up entries, start with the last entry that has a key less than or + * equal to the method name, then work backwards as long as the AVL key still + * matches its counterpart in the object name + */ +#define uh_foreach_matching_acl_prefix(_acl, _ses, _obj, _func) \ + for (_acl = avl_find_le_element(&(_ses)->acls, _obj, _acl, avl); \ + _acl; \ + _acl = avl_is_first(&(ses)->acls, &(_acl)->avl) ? NULL : \ + avl_prev_element((_acl), avl)) + +#define uh_foreach_matching_acl(_acl, _ses, _obj, _func) \ + uh_foreach_matching_acl_prefix(_acl, _ses, _obj, _func) \ + if (!strncmp((_acl)->object, _obj, (_acl)->sort_len) && \ + !fnmatch((_acl)->object, (_obj), FNM_NOESCAPE) && \ + !fnmatch((_acl)->function, (_func), FNM_NOESCAPE)) + +static void +rpc_random(char *dest) +{ + unsigned char buf[16] = { 0 }; + FILE *f; + int i; + + f = fopen("/dev/urandom", "r"); + if (!f) + return; + + fread(buf, 1, sizeof(buf), f); + fclose(f); + + for (i = 0; i < sizeof(buf); i++) + sprintf(dest + (i<<1), "%02x", buf[i]); +} + +static void +rpc_session_dump_data(struct rpc_session *ses, struct blob_buf *b) +{ + struct rpc_session_data *d; + + avl_for_each_element(&ses->data, d, avl) { + blobmsg_add_field(b, blobmsg_type(d->attr), blobmsg_name(d->attr), + blobmsg_data(d->attr), blobmsg_data_len(d->attr)); + } +} + +static void +rpc_session_dump_acls(struct rpc_session *ses, struct blob_buf *b) +{ + struct rpc_session_acl *acl; + const char *lastobj = NULL; + void *c = NULL; + + avl_for_each_element(&ses->acls, acl, avl) { + if (!lastobj || strcmp(acl->object, lastobj)) + { + if (c) blobmsg_close_array(b, c); + c = blobmsg_open_array(b, acl->object); + } + + blobmsg_add_string(b, NULL, acl->function); + lastobj = acl->object; + } + + if (c) blobmsg_close_array(b, c); +} + +static void +rpc_session_dump(struct rpc_session *ses, + struct ubus_context *ctx, + struct ubus_request_data *req) +{ + void *c; + + blob_buf_init(&buf, 0); + + blobmsg_add_string(&buf, "sid", ses->id); + blobmsg_add_u32(&buf, "timeout", ses->timeout); + blobmsg_add_u32(&buf, "expires", uloop_timeout_remaining(&ses->t) / 1000); + + c = blobmsg_open_table(&buf, "acls"); + rpc_session_dump_acls(ses, &buf); + blobmsg_close_table(&buf, c); + + c = blobmsg_open_table(&buf, "data"); + rpc_session_dump_data(ses, &buf); + blobmsg_close_table(&buf, c); + + ubus_send_reply(ctx, req, buf.head); +} + +static void +rpc_touch_session(struct rpc_session *ses) +{ + uloop_timeout_set(&ses->t, ses->timeout * 1000); +} + +static void +rpc_session_destroy(struct rpc_session *ses) +{ + struct rpc_session_acl *acl, *nacl; + struct rpc_session_data *data, *ndata; + + uloop_timeout_cancel(&ses->t); + avl_remove_all_elements(&ses->acls, acl, avl, nacl) + free(acl); + + avl_remove_all_elements(&ses->data, data, avl, ndata) + free(data); + + avl_delete(&sessions, &ses->avl); + free(ses); +} + +static void rpc_session_timeout(struct uloop_timeout *t) +{ + struct rpc_session *ses; + + ses = container_of(t, struct rpc_session, t); + rpc_session_destroy(ses); +} + +static struct rpc_session * +rpc_session_create(int timeout) +{ + struct rpc_session *ses; + + ses = calloc(1, sizeof(*ses)); + if (!ses) + return NULL; + + ses->timeout = timeout; + ses->avl.key = ses->id; + rpc_random(ses->id); + + avl_insert(&sessions, &ses->avl); + avl_init(&ses->acls, avl_strcmp, true, NULL); + avl_init(&ses->data, avl_strcmp, false, NULL); + + ses->t.cb = rpc_session_timeout; + rpc_touch_session(ses); + + return ses; +} + +static struct rpc_session * +rpc_session_get(const char *id) +{ + struct rpc_session *ses; + + ses = avl_find_element(&sessions, id, ses, avl); + if (!ses) + return NULL; + + rpc_touch_session(ses); + return ses; +} + +static int +rpc_handle_create(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + struct rpc_session *ses; + struct blob_attr *tb; + int timeout = RPC_DEFAULT_SESSION_TIMEOUT; + + blobmsg_parse(&new_policy, 1, &tb, blob_data(msg), blob_len(msg)); + if (tb) + timeout = blobmsg_get_u32(tb); + + ses = rpc_session_create(timeout); + if (ses) + rpc_session_dump(ses, ctx, req); + + return 0; +} + +static int +rpc_handle_list(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + struct rpc_session *ses; + struct blob_attr *tb; + + blobmsg_parse(&sid_policy, 1, &tb, blob_data(msg), blob_len(msg)); + + if (!tb) { + avl_for_each_element(&sessions, ses, avl) + rpc_session_dump(ses, ctx, req); + return 0; + } + + ses = rpc_session_get(blobmsg_data(tb)); + if (!ses) + return UBUS_STATUS_NOT_FOUND; + + rpc_session_dump(ses, ctx, req); + + return 0; +} + +static int +uh_id_len(const char *str) +{ + return strcspn(str, "*?["); +} + +static int +rpc_session_grant(struct rpc_session *ses, struct ubus_context *ctx, + const char *object, const char *function) +{ + struct rpc_session_acl *acl; + char *new_obj, *new_func, *new_id; + int id_len; + + if (!object || !function) + return UBUS_STATUS_INVALID_ARGUMENT; + + uh_foreach_matching_acl_prefix(acl, ses, object, function) { + if (!strcmp(acl->object, object) && + !strcmp(acl->function, function)) + return 0; + } + + id_len = uh_id_len(object); + acl = calloc_a(sizeof(*acl), + &new_obj, strlen(object) + 1, + &new_func, strlen(function) + 1, + &new_id, id_len + 1); + + if (!acl) + return UBUS_STATUS_UNKNOWN_ERROR; + + acl->object = strcpy(new_obj, object); + acl->function = strcpy(new_func, function); + acl->avl.key = strncpy(new_id, object, id_len); + avl_insert(&ses->acls, &acl->avl); + + return 0; +} + +static int +rpc_session_revoke(struct rpc_session *ses, struct ubus_context *ctx, + const char *object, const char *function) +{ + struct rpc_session_acl *acl, *next; + int id_len; + char *id; + + if (!object && !function) { + avl_remove_all_elements(&ses->acls, acl, avl, next) + free(acl); + return 0; + } + + id_len = uh_id_len(object); + id = alloca(id_len + 1); + strncpy(id, object, id_len); + id[id_len] = 0; + + acl = avl_find_element(&ses->acls, id, acl, avl); + while (acl) { + if (!avl_is_last(&ses->acls, &acl->avl)) + next = avl_next_element(acl, avl); + else + next = NULL; + + if (strcmp(id, acl->avl.key) != 0) + break; + + if (!strcmp(acl->object, object) && + !strcmp(acl->function, function)) { + avl_delete(&ses->acls, &acl->avl); + free(acl); + } + acl = next; + } + + return 0; +} + + +static int +rpc_handle_acl(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + struct rpc_session *ses; + struct blob_attr *tb[__RPC_SA_MAX]; + struct blob_attr *attr, *sattr; + const char *object, *function; + int rem1, rem2; + + int (*cb)(struct rpc_session *ses, struct ubus_context *ctx, + const char *object, const char *function); + + blobmsg_parse(acl_policy, __RPC_SA_MAX, tb, blob_data(msg), blob_len(msg)); + + if (!tb[RPC_SA_SID]) + return UBUS_STATUS_INVALID_ARGUMENT; + + ses = rpc_session_get(blobmsg_data(tb[RPC_SA_SID])); + if (!ses) + return UBUS_STATUS_NOT_FOUND; + + if (!strcmp(method, "grant")) + cb = rpc_session_grant; + else + cb = rpc_session_revoke; + + if (!tb[RPC_SA_OBJECTS]) + return cb(ses, ctx, NULL, NULL); + + blobmsg_for_each_attr(attr, tb[RPC_SA_OBJECTS], rem1) { + if (blob_id(attr) != BLOBMSG_TYPE_ARRAY) + continue; + + object = NULL; + function = NULL; + + blobmsg_for_each_attr(sattr, attr, rem2) { + if (blob_id(sattr) != BLOBMSG_TYPE_STRING) + continue; + + if (!object) + object = blobmsg_data(sattr); + else if (!function) + function = blobmsg_data(sattr); + else + break; + } + + if (object && function) + cb(ses, ctx, object, function); + } + + return 0; +} + +static bool +rpc_session_acl_allowed(struct rpc_session *ses, const char *obj, const char *fun) +{ + struct rpc_session_acl *acl; + + uh_foreach_matching_acl(acl, ses, obj, fun) + return true; + + return false; +} + +static int +rpc_handle_access(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + struct rpc_session *ses; + struct blob_attr *tb[__RPC_SP_MAX]; + bool allow; + + blobmsg_parse(perm_policy, __RPC_SP_MAX, tb, blob_data(msg), blob_len(msg)); + + if (!tb[RPC_SP_SID] || !tb[RPC_SP_OBJECT] || !tb[RPC_SP_FUNCTION]) + return UBUS_STATUS_INVALID_ARGUMENT; + + ses = rpc_session_get(blobmsg_data(tb[RPC_SP_SID])); + if (!ses) + return UBUS_STATUS_NOT_FOUND; + + allow = rpc_session_acl_allowed(ses, + blobmsg_data(tb[RPC_SP_OBJECT]), + blobmsg_data(tb[RPC_SP_FUNCTION])); + + blob_buf_init(&buf, 0); + blobmsg_add_u8(&buf, "access", allow); + ubus_send_reply(ctx, req, buf.head); + + return 0; +} + +static int +rpc_handle_set(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + struct rpc_session *ses; + struct rpc_session_data *data; + struct blob_attr *tb[__RPC_SA_MAX]; + struct blob_attr *attr; + int rem; + + blobmsg_parse(set_policy, __RPC_SS_MAX, tb, blob_data(msg), blob_len(msg)); + + if (!tb[RPC_SS_SID] || !tb[RPC_SS_VALUES]) + return UBUS_STATUS_INVALID_ARGUMENT; + + ses = rpc_session_get(blobmsg_data(tb[RPC_SS_SID])); + if (!ses) + return UBUS_STATUS_NOT_FOUND; + + blobmsg_for_each_attr(attr, tb[RPC_SS_VALUES], rem) { + if (!blobmsg_name(attr)[0]) + continue; + + data = avl_find_element(&ses->data, blobmsg_name(attr), data, avl); + if (data) { + avl_delete(&ses->data, &data->avl); + free(data); + } + + data = calloc(1, sizeof(*data) + blob_pad_len(attr)); + if (!data) + break; + + memcpy(data->attr, attr, blob_pad_len(attr)); + data->avl.key = blobmsg_name(data->attr); + avl_insert(&ses->data, &data->avl); + } + + return 0; +} + +static int +rpc_handle_get(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + struct rpc_session *ses; + struct rpc_session_data *data; + struct blob_attr *tb[__RPC_SA_MAX]; + struct blob_attr *attr; + void *c; + int rem; + + blobmsg_parse(get_policy, __RPC_SG_MAX, tb, blob_data(msg), blob_len(msg)); + + if (!tb[RPC_SG_SID]) + return UBUS_STATUS_INVALID_ARGUMENT; + + ses = rpc_session_get(blobmsg_data(tb[RPC_SG_SID])); + if (!ses) + return UBUS_STATUS_NOT_FOUND; + + blob_buf_init(&buf, 0); + c = blobmsg_open_table(&buf, "values"); + + if (!tb[RPC_SG_KEYS]) { + rpc_session_dump_data(ses, &buf); + return 0; + } + + blobmsg_for_each_attr(attr, tb[RPC_SG_KEYS], rem) { + if (blob_id(attr) != BLOBMSG_TYPE_STRING) + continue; + + data = avl_find_element(&ses->data, blobmsg_data(attr), data, avl); + if (!data) + continue; + + blobmsg_add_field(&buf, blobmsg_type(data->attr), + blobmsg_name(data->attr), + blobmsg_data(data->attr), + blobmsg_data_len(data->attr)); + } + + blobmsg_close_table(&buf, c); + ubus_send_reply(ctx, req, buf.head); + + return 0; +} + +static int +rpc_handle_unset(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + struct rpc_session *ses; + struct rpc_session_data *data, *ndata; + struct blob_attr *tb[__RPC_SA_MAX]; + struct blob_attr *attr; + int rem; + + blobmsg_parse(get_policy, __RPC_SG_MAX, tb, blob_data(msg), blob_len(msg)); + + if (!tb[RPC_SG_SID]) + return UBUS_STATUS_INVALID_ARGUMENT; + + ses = rpc_session_get(blobmsg_data(tb[RPC_SG_SID])); + if (!ses) + return UBUS_STATUS_NOT_FOUND; + + if (!tb[RPC_SG_KEYS]) { + avl_remove_all_elements(&ses->data, data, avl, ndata) + free(data); + return 0; + } + + blobmsg_for_each_attr(attr, tb[RPC_SG_KEYS], rem) { + if (blob_id(attr) != BLOBMSG_TYPE_STRING) + continue; + + data = avl_find_element(&ses->data, blobmsg_data(attr), data, avl); + if (!data) + continue; + + avl_delete(&ses->data, &data->avl); + free(data); + } + + return 0; +} + +static int +rpc_handle_destroy(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + struct rpc_session *ses; + struct blob_attr *tb; + + blobmsg_parse(&sid_policy, 1, &tb, blob_data(msg), blob_len(msg)); + + if (!tb) + return UBUS_STATUS_INVALID_ARGUMENT; + + ses = rpc_session_get(blobmsg_data(tb)); + if (!ses) + return UBUS_STATUS_NOT_FOUND; + + rpc_session_destroy(ses); + + return 0; +} + +int rpc_session_api_init(struct ubus_context *ctx) +{ + static const struct ubus_method session_methods[] = { + UBUS_METHOD("create", rpc_handle_create, &new_policy), + UBUS_METHOD("list", rpc_handle_list, &sid_policy), + UBUS_METHOD("grant", rpc_handle_acl, acl_policy), + UBUS_METHOD("revoke", rpc_handle_acl, acl_policy), + UBUS_METHOD("access", rpc_handle_access, perm_policy), + UBUS_METHOD("set", rpc_handle_set, set_policy), + UBUS_METHOD("get", rpc_handle_get, get_policy), + UBUS_METHOD("unset", rpc_handle_unset, get_policy), + UBUS_METHOD("destroy", rpc_handle_destroy, &sid_policy), + }; + + static struct ubus_object_type session_type = + UBUS_OBJECT_TYPE("luci-rpc-session", session_methods); + + static struct ubus_object obj = { + .name = "session", + .type = &session_type, + .methods = session_methods, + .n_methods = ARRAY_SIZE(session_methods), + }; + + avl_init(&sessions, avl_strcmp, false, NULL); + + return ubus_add_object(ctx, &obj); +} diff --git a/session.h b/session.h new file mode 100644 index 0000000..8b95967 --- /dev/null +++ b/session.h @@ -0,0 +1,54 @@ +/* + * luci-rpcd - LuCI UBUS RPC server + * + * Copyright (C) 2013 Felix Fietkau + * Copyright (C) 2013 Jo-Philipp Wich + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef __RPC_SESSION_H +#define __RPC_SESSION_H + +#include +#include + +#define RPC_SID_LEN 32 +#define RPC_DEFAULT_SESSION_TIMEOUT 300 + +struct rpc_session { + struct avl_node avl; + char id[RPC_SID_LEN + 1]; + + struct uloop_timeout t; + struct avl_tree data; + struct avl_tree acls; + + int timeout; +}; + +struct rpc_session_data { + struct avl_node avl; + struct blob_attr attr[]; +}; + +struct rpc_session_acl { + struct avl_node avl; + const char *object; + const char *function; + int sort_len; +}; + +int rpc_session_api_init(struct ubus_context *ctx); + +#endif -- 2.11.0