2013-01-26 15:29:09 +01:00
|
|
|
/*
|
2013-09-02 17:09:57 +02:00
|
|
|
* rpcd - UBUS RPC server
|
2013-01-26 15:29:09 +01:00
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
|
2014-01-12 13:48:58 +01:00
|
|
|
* Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
|
2013-01-26 15:29:09 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2013-09-10 19:41:29 +02:00
|
|
|
#define _GNU_SOURCE /* crypt() */
|
|
|
|
|
2013-01-26 15:29:09 +01:00
|
|
|
#include <libubox/avl-cmp.h>
|
2013-09-10 19:41:29 +02:00
|
|
|
#include <libubox/blobmsg.h>
|
2013-01-26 15:29:09 +01:00
|
|
|
#include <libubox/utils.h>
|
|
|
|
#include <libubus.h>
|
|
|
|
#include <fnmatch.h>
|
2013-09-10 19:41:29 +02:00
|
|
|
#include <glob.h>
|
|
|
|
#include <uci.h>
|
2014-06-29 23:22:50 +02:00
|
|
|
#include <limits.h>
|
2013-09-10 19:41:29 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_SHADOW
|
|
|
|
#include <shadow.h>
|
|
|
|
#endif
|
2013-01-26 15:29:09 +01:00
|
|
|
|
2013-09-04 12:46:11 +02:00
|
|
|
#include <rpcd/session.h>
|
2013-01-26 15:29:09 +01:00
|
|
|
|
|
|
|
static struct avl_tree sessions;
|
|
|
|
static struct blob_buf buf;
|
|
|
|
|
2013-09-03 12:21:09 +02:00
|
|
|
static LIST_HEAD(create_callbacks);
|
|
|
|
static LIST_HEAD(destroy_callbacks);
|
|
|
|
|
2015-02-09 12:51:44 +01:00
|
|
|
enum {
|
|
|
|
RPC_SN_TIMEOUT,
|
|
|
|
__RPC_SN_MAX,
|
|
|
|
};
|
|
|
|
static const struct blobmsg_policy new_policy[__RPC_SN_MAX] = {
|
|
|
|
[RPC_SN_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
|
2013-01-26 15:29:09 +01:00
|
|
|
};
|
|
|
|
|
2015-02-09 12:51:44 +01:00
|
|
|
enum {
|
|
|
|
RPC_SI_SID,
|
|
|
|
__RPC_SI_MAX,
|
|
|
|
};
|
|
|
|
static const struct blobmsg_policy sid_policy[__RPC_SI_MAX] = {
|
|
|
|
[RPC_SI_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
|
2013-01-26 15:29:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
RPC_SS_SID,
|
|
|
|
RPC_SS_VALUES,
|
|
|
|
__RPC_SS_MAX,
|
|
|
|
};
|
|
|
|
static const struct blobmsg_policy set_policy[__RPC_SS_MAX] = {
|
2013-09-13 14:52:35 +02:00
|
|
|
[RPC_SS_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
|
2013-01-26 15:29:09 +01:00
|
|
|
[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] = {
|
2013-09-13 14:52:35 +02:00
|
|
|
[RPC_SG_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
|
2013-01-26 15:29:09 +01:00
|
|
|
[RPC_SG_KEYS] = { .name = "keys", .type = BLOBMSG_TYPE_ARRAY },
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
RPC_SA_SID,
|
2013-08-08 18:53:59 +02:00
|
|
|
RPC_SA_SCOPE,
|
2013-01-26 15:29:09 +01:00
|
|
|
RPC_SA_OBJECTS,
|
|
|
|
__RPC_SA_MAX,
|
|
|
|
};
|
|
|
|
static const struct blobmsg_policy acl_policy[__RPC_SA_MAX] = {
|
2013-09-13 14:52:35 +02:00
|
|
|
[RPC_SA_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
|
2013-08-08 18:53:59 +02:00
|
|
|
[RPC_SA_SCOPE] = { .name = "scope", .type = BLOBMSG_TYPE_STRING },
|
2013-01-26 15:29:09 +01:00
|
|
|
[RPC_SA_OBJECTS] = { .name = "objects", .type = BLOBMSG_TYPE_ARRAY },
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
RPC_SP_SID,
|
2013-08-08 18:53:59 +02:00
|
|
|
RPC_SP_SCOPE,
|
2013-01-26 15:29:09 +01:00
|
|
|
RPC_SP_OBJECT,
|
|
|
|
RPC_SP_FUNCTION,
|
|
|
|
__RPC_SP_MAX,
|
|
|
|
};
|
|
|
|
static const struct blobmsg_policy perm_policy[__RPC_SP_MAX] = {
|
2013-09-13 14:52:35 +02:00
|
|
|
[RPC_SP_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
|
2013-08-08 18:53:59 +02:00
|
|
|
[RPC_SP_SCOPE] = { .name = "scope", .type = BLOBMSG_TYPE_STRING },
|
2013-01-26 15:29:09 +01:00
|
|
|
[RPC_SP_OBJECT] = { .name = "object", .type = BLOBMSG_TYPE_STRING },
|
|
|
|
[RPC_SP_FUNCTION] = { .name = "function", .type = BLOBMSG_TYPE_STRING },
|
|
|
|
};
|
|
|
|
|
2013-09-04 15:18:14 +02:00
|
|
|
enum {
|
|
|
|
RPC_DUMP_SID,
|
|
|
|
RPC_DUMP_TIMEOUT,
|
|
|
|
RPC_DUMP_EXPIRES,
|
|
|
|
RPC_DUMP_DATA,
|
|
|
|
__RPC_DUMP_MAX,
|
|
|
|
};
|
|
|
|
static const struct blobmsg_policy dump_policy[__RPC_DUMP_MAX] = {
|
2013-09-13 14:52:35 +02:00
|
|
|
[RPC_DUMP_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
|
2013-09-04 15:18:14 +02:00
|
|
|
[RPC_DUMP_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
|
2021-11-04 12:00:05 +01:00
|
|
|
[RPC_DUMP_EXPIRES] = { .name = "expires", .type = BLOBMSG_TYPE_INT64 },
|
2013-09-04 15:18:14 +02:00
|
|
|
[RPC_DUMP_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
|
|
|
|
};
|
|
|
|
|
2013-09-10 19:41:29 +02:00
|
|
|
enum {
|
|
|
|
RPC_L_USERNAME,
|
|
|
|
RPC_L_PASSWORD,
|
|
|
|
RPC_L_TIMEOUT,
|
|
|
|
__RPC_L_MAX,
|
|
|
|
};
|
|
|
|
static const struct blobmsg_policy login_policy[__RPC_L_MAX] = {
|
|
|
|
[RPC_L_USERNAME] = { .name = "username", .type = BLOBMSG_TYPE_STRING },
|
|
|
|
[RPC_L_PASSWORD] = { .name = "password", .type = BLOBMSG_TYPE_STRING },
|
|
|
|
[RPC_L_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
|
|
|
|
};
|
|
|
|
|
2013-01-26 15:29:09 +01:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2013-08-08 18:53:59 +02:00
|
|
|
#define uh_foreach_matching_acl_prefix(_acl, _avl, _obj, _func) \
|
|
|
|
for (_acl = avl_find_le_element(_avl, _obj, _acl, avl); \
|
|
|
|
_acl; \
|
|
|
|
_acl = avl_is_first(_avl, &(_acl)->avl) ? NULL : \
|
2013-01-26 15:29:09 +01:00
|
|
|
avl_prev_element((_acl), avl))
|
|
|
|
|
2013-08-08 18:53:59 +02:00
|
|
|
#define uh_foreach_matching_acl(_acl, _avl, _obj, _func) \
|
|
|
|
uh_foreach_matching_acl_prefix(_acl, _avl, _obj, _func) \
|
2013-01-26 15:29:09 +01:00
|
|
|
if (!strncmp((_acl)->object, _obj, (_acl)->sort_len) && \
|
|
|
|
!fnmatch((_acl)->object, (_obj), FNM_NOESCAPE) && \
|
|
|
|
!fnmatch((_acl)->function, (_func), FNM_NOESCAPE))
|
|
|
|
|
2015-03-28 17:05:56 +01:00
|
|
|
static int
|
2013-01-26 15:29:09 +01:00
|
|
|
rpc_random(char *dest)
|
|
|
|
{
|
|
|
|
unsigned char buf[16] = { 0 };
|
|
|
|
FILE *f;
|
|
|
|
int i;
|
2015-03-28 17:05:56 +01:00
|
|
|
int ret;
|
2013-01-26 15:29:09 +01:00
|
|
|
|
|
|
|
f = fopen("/dev/urandom", "r");
|
|
|
|
if (!f)
|
2015-03-28 17:05:56 +01:00
|
|
|
return -1;
|
2013-01-26 15:29:09 +01:00
|
|
|
|
2015-03-28 17:05:56 +01:00
|
|
|
ret = fread(buf, 1, sizeof(buf), f);
|
2013-01-26 15:29:09 +01:00
|
|
|
fclose(f);
|
|
|
|
|
2015-03-28 17:05:56 +01:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2013-01-26 15:29:09 +01:00
|
|
|
for (i = 0; i < sizeof(buf); i++)
|
|
|
|
sprintf(dest + (i<<1), "%02x", buf[i]);
|
2015-03-28 17:05:56 +01:00
|
|
|
|
|
|
|
return 0;
|
2013-01-26 15:29:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2013-08-08 18:53:59 +02:00
|
|
|
struct rpc_session_acl_scope *acl_scope;
|
2013-01-26 15:29:09 +01:00
|
|
|
const char *lastobj = NULL;
|
2013-08-08 18:53:59 +02:00
|
|
|
const char *lastscope = NULL;
|
|
|
|
void *c = NULL, *d = NULL;
|
2013-01-26 15:29:09 +01:00
|
|
|
|
2013-08-08 18:53:59 +02:00
|
|
|
avl_for_each_element(&ses->acls, acl_scope, avl) {
|
|
|
|
if (!lastscope || strcmp(acl_scope->avl.key, lastscope))
|
2013-01-26 15:29:09 +01:00
|
|
|
{
|
2013-08-08 18:53:59 +02:00
|
|
|
if (c) blobmsg_close_table(b, c);
|
|
|
|
c = blobmsg_open_table(b, acl_scope->avl.key);
|
2013-08-10 21:56:06 +02:00
|
|
|
lastobj = NULL;
|
2013-01-26 15:29:09 +01:00
|
|
|
}
|
|
|
|
|
2013-08-08 18:53:59 +02:00
|
|
|
d = NULL;
|
|
|
|
|
|
|
|
avl_for_each_element(&acl_scope->acls, acl, avl) {
|
|
|
|
if (!lastobj || strcmp(acl->object, lastobj))
|
|
|
|
{
|
|
|
|
if (d) blobmsg_close_array(b, d);
|
|
|
|
d = blobmsg_open_array(b, acl->object);
|
|
|
|
}
|
|
|
|
|
|
|
|
blobmsg_add_string(b, NULL, acl->function);
|
|
|
|
lastobj = acl->object;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d) blobmsg_close_array(b, d);
|
2013-01-26 15:29:09 +01:00
|
|
|
}
|
|
|
|
|
2013-08-08 18:53:59 +02:00
|
|
|
if (c) blobmsg_close_table(b, c);
|
2013-01-26 15:29:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-09-12 12:13:05 +02:00
|
|
|
rpc_session_to_blob(struct rpc_session *ses, bool acls)
|
2013-01-26 15:29:09 +01:00
|
|
|
{
|
|
|
|
void *c;
|
|
|
|
|
|
|
|
blob_buf_init(&buf, 0);
|
|
|
|
|
2013-09-13 14:52:35 +02:00
|
|
|
blobmsg_add_string(&buf, "ubus_rpc_session", ses->id);
|
2013-01-26 15:29:09 +01:00
|
|
|
blobmsg_add_u32(&buf, "timeout", ses->timeout);
|
2021-11-04 12:00:05 +01:00
|
|
|
blobmsg_add_u64(&buf, "expires", uloop_timeout_remaining64(&ses->t) / 1000);
|
2013-01-26 15:29:09 +01:00
|
|
|
|
2013-09-12 12:13:05 +02:00
|
|
|
if (acls) {
|
|
|
|
c = blobmsg_open_table(&buf, "acls");
|
|
|
|
rpc_session_dump_acls(ses, &buf);
|
|
|
|
blobmsg_close_table(&buf, c);
|
|
|
|
}
|
|
|
|
|
2013-01-26 15:29:09 +01:00
|
|
|
c = blobmsg_open_table(&buf, "data");
|
|
|
|
rpc_session_dump_data(ses, &buf);
|
|
|
|
blobmsg_close_table(&buf, c);
|
2013-09-04 15:18:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rpc_session_dump(struct rpc_session *ses, struct ubus_context *ctx,
|
|
|
|
struct ubus_request_data *req)
|
|
|
|
{
|
2013-09-12 12:13:05 +02:00
|
|
|
rpc_session_to_blob(ses, true);
|
2013-01-26 15:29:09 +01:00
|
|
|
|
|
|
|
ubus_send_reply(ctx, req, buf.head);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rpc_touch_session(struct rpc_session *ses)
|
|
|
|
{
|
2013-09-10 19:41:29 +02:00
|
|
|
if (ses->timeout > 0)
|
|
|
|
uloop_timeout_set(&ses->t, ses->timeout * 1000);
|
2013-01-26 15:29:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rpc_session_destroy(struct rpc_session *ses)
|
|
|
|
{
|
|
|
|
struct rpc_session_acl *acl, *nacl;
|
2013-08-08 18:53:59 +02:00
|
|
|
struct rpc_session_acl_scope *acl_scope, *nacl_scope;
|
2013-01-26 15:29:09 +01:00
|
|
|
struct rpc_session_data *data, *ndata;
|
2013-09-03 12:21:09 +02:00
|
|
|
struct rpc_session_cb *cb;
|
|
|
|
|
|
|
|
list_for_each_entry(cb, &destroy_callbacks, list)
|
|
|
|
cb->cb(ses, cb->priv);
|
2013-01-26 15:29:09 +01:00
|
|
|
|
|
|
|
uloop_timeout_cancel(&ses->t);
|
2013-08-08 18:53:59 +02:00
|
|
|
|
|
|
|
avl_for_each_element_safe(&ses->acls, acl_scope, avl, nacl_scope) {
|
|
|
|
avl_remove_all_elements(&acl_scope->acls, acl, avl, nacl)
|
|
|
|
free(acl);
|
|
|
|
|
|
|
|
avl_delete(&ses->acls, &acl_scope->avl);
|
|
|
|
free(acl_scope);
|
|
|
|
}
|
2013-01-26 15:29:09 +01:00
|
|
|
|
|
|
|
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 *
|
2013-09-04 15:18:14 +02:00
|
|
|
rpc_session_new(void)
|
2013-01-26 15:29:09 +01:00
|
|
|
{
|
|
|
|
struct rpc_session *ses;
|
|
|
|
|
|
|
|
ses = calloc(1, sizeof(*ses));
|
2013-09-04 15:18:14 +02:00
|
|
|
|
2013-01-26 15:29:09 +01:00
|
|
|
if (!ses)
|
|
|
|
return NULL;
|
|
|
|
|
2013-09-04 15:18:14 +02:00
|
|
|
ses->avl.key = ses->id;
|
2013-01-26 15:29:09 +01:00
|
|
|
|
|
|
|
avl_init(&ses->acls, avl_strcmp, true, NULL);
|
|
|
|
avl_init(&ses->data, avl_strcmp, false, NULL);
|
|
|
|
|
|
|
|
ses->t.cb = rpc_session_timeout;
|
2013-09-04 15:18:14 +02:00
|
|
|
|
|
|
|
return ses;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct rpc_session *
|
|
|
|
rpc_session_create(int timeout)
|
|
|
|
{
|
|
|
|
struct rpc_session *ses;
|
|
|
|
struct rpc_session_cb *cb;
|
|
|
|
|
|
|
|
ses = rpc_session_new();
|
|
|
|
|
|
|
|
if (!ses)
|
|
|
|
return NULL;
|
|
|
|
|
2015-03-28 17:05:56 +01:00
|
|
|
if (rpc_random(ses->id))
|
|
|
|
return NULL;
|
2013-09-04 15:18:14 +02:00
|
|
|
|
|
|
|
ses->timeout = timeout;
|
|
|
|
|
|
|
|
avl_insert(&sessions, &ses->avl);
|
|
|
|
|
2013-01-26 15:29:09 +01:00
|
|
|
rpc_touch_session(ses);
|
|
|
|
|
2013-09-03 12:21:09 +02:00
|
|
|
list_for_each_entry(cb, &create_callbacks, list)
|
|
|
|
cb->cb(ses, cb->priv);
|
|
|
|
|
2013-01-26 15:29:09 +01:00
|
|
|
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;
|
|
|
|
|
2015-02-09 12:51:44 +01:00
|
|
|
blobmsg_parse(new_policy, __RPC_SN_MAX, &tb, blob_data(msg), blob_len(msg));
|
2013-01-26 15:29:09 +01:00
|
|
|
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;
|
|
|
|
|
2015-02-09 12:51:44 +01:00
|
|
|
blobmsg_parse(sid_policy, __RPC_SI_MAX, &tb, blob_data(msg), blob_len(msg));
|
2013-01-26 15:29:09 +01:00
|
|
|
|
|
|
|
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
|
2013-09-12 11:50:33 +02:00
|
|
|
rpc_session_grant(struct rpc_session *ses,
|
2013-08-08 18:53:59 +02:00
|
|
|
const char *scope, const char *object, const char *function)
|
2013-01-26 15:29:09 +01:00
|
|
|
{
|
|
|
|
struct rpc_session_acl *acl;
|
2013-08-08 18:53:59 +02:00
|
|
|
struct rpc_session_acl_scope *acl_scope;
|
|
|
|
char *new_scope, *new_obj, *new_func, *new_id;
|
2013-01-26 15:29:09 +01:00
|
|
|
int id_len;
|
|
|
|
|
|
|
|
if (!object || !function)
|
|
|
|
return UBUS_STATUS_INVALID_ARGUMENT;
|
|
|
|
|
2013-08-08 18:53:59 +02:00
|
|
|
acl_scope = avl_find_element(&ses->acls, scope, acl_scope, avl);
|
|
|
|
|
|
|
|
if (acl_scope) {
|
|
|
|
uh_foreach_matching_acl_prefix(acl, &acl_scope->acls, object, function) {
|
|
|
|
if (!strcmp(acl->object, object) &&
|
|
|
|
!strcmp(acl->function, function))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!acl_scope) {
|
|
|
|
acl_scope = calloc_a(sizeof(*acl_scope),
|
|
|
|
&new_scope, strlen(scope) + 1);
|
|
|
|
|
|
|
|
if (!acl_scope)
|
|
|
|
return UBUS_STATUS_UNKNOWN_ERROR;
|
|
|
|
|
|
|
|
acl_scope->avl.key = strcpy(new_scope, scope);
|
|
|
|
avl_init(&acl_scope->acls, avl_strcmp, true, NULL);
|
|
|
|
avl_insert(&ses->acls, &acl_scope->avl);
|
2013-01-26 15:29:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2013-08-08 18:53:59 +02:00
|
|
|
avl_insert(&acl_scope->acls, &acl->avl);
|
2013-01-26 15:29:09 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-09-12 11:50:33 +02:00
|
|
|
rpc_session_revoke(struct rpc_session *ses,
|
2013-08-08 18:53:59 +02:00
|
|
|
const char *scope, const char *object, const char *function)
|
2013-01-26 15:29:09 +01:00
|
|
|
{
|
|
|
|
struct rpc_session_acl *acl, *next;
|
2013-08-08 18:53:59 +02:00
|
|
|
struct rpc_session_acl_scope *acl_scope;
|
2013-01-26 15:29:09 +01:00
|
|
|
int id_len;
|
|
|
|
char *id;
|
|
|
|
|
2013-08-08 18:53:59 +02:00
|
|
|
acl_scope = avl_find_element(&ses->acls, scope, acl_scope, avl);
|
|
|
|
|
|
|
|
if (!acl_scope)
|
|
|
|
return 0;
|
|
|
|
|
2013-01-26 15:29:09 +01:00
|
|
|
if (!object && !function) {
|
2013-08-08 18:53:59 +02:00
|
|
|
avl_remove_all_elements(&acl_scope->acls, acl, avl, next)
|
2013-01-26 15:29:09 +01:00
|
|
|
free(acl);
|
2013-08-08 18:53:59 +02:00
|
|
|
avl_delete(&ses->acls, &acl_scope->avl);
|
|
|
|
free(acl_scope);
|
2013-01-26 15:29:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
id_len = uh_id_len(object);
|
|
|
|
id = alloca(id_len + 1);
|
|
|
|
strncpy(id, object, id_len);
|
|
|
|
id[id_len] = 0;
|
|
|
|
|
2013-08-08 18:53:59 +02:00
|
|
|
acl = avl_find_element(&acl_scope->acls, id, acl, avl);
|
2013-01-26 15:29:09 +01:00
|
|
|
while (acl) {
|
2013-08-08 18:53:59 +02:00
|
|
|
if (!avl_is_last(&acl_scope->acls, &acl->avl))
|
2013-01-26 15:29:09 +01:00
|
|
|
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)) {
|
2013-08-08 18:53:59 +02:00
|
|
|
avl_delete(&acl_scope->acls, &acl->avl);
|
2013-01-26 15:29:09 +01:00
|
|
|
free(acl);
|
|
|
|
}
|
|
|
|
acl = next;
|
|
|
|
}
|
|
|
|
|
2013-08-08 18:53:59 +02:00
|
|
|
if (avl_is_empty(&acl_scope->acls)) {
|
|
|
|
avl_delete(&ses->acls, &acl_scope->avl);
|
|
|
|
free(acl_scope);
|
|
|
|
}
|
|
|
|
|
2013-01-26 15:29:09 +01:00
|
|
|
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;
|
2013-08-08 18:53:59 +02:00
|
|
|
const char *scope = "ubus";
|
2013-01-26 15:29:09 +01:00
|
|
|
int rem1, rem2;
|
|
|
|
|
2013-09-12 11:50:33 +02:00
|
|
|
int (*cb)(struct rpc_session *ses,
|
|
|
|
const char *scope, const char *object, const char *function);
|
2013-01-26 15:29:09 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2013-08-08 18:53:59 +02:00
|
|
|
if (tb[RPC_SA_SCOPE])
|
|
|
|
scope = blobmsg_data(tb[RPC_SA_SCOPE]);
|
|
|
|
|
2013-01-26 15:29:09 +01:00
|
|
|
if (!strcmp(method, "grant"))
|
|
|
|
cb = rpc_session_grant;
|
|
|
|
else
|
|
|
|
cb = rpc_session_revoke;
|
|
|
|
|
|
|
|
if (!tb[RPC_SA_OBJECTS])
|
2013-09-12 11:50:33 +02:00
|
|
|
return cb(ses, scope, NULL, NULL);
|
2013-01-26 15:29:09 +01:00
|
|
|
|
|
|
|
blobmsg_for_each_attr(attr, tb[RPC_SA_OBJECTS], rem1) {
|
2013-09-12 13:31:21 +02:00
|
|
|
if (blobmsg_type(attr) != BLOBMSG_TYPE_ARRAY)
|
2013-01-26 15:29:09 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
object = NULL;
|
|
|
|
function = NULL;
|
|
|
|
|
|
|
|
blobmsg_for_each_attr(sattr, attr, rem2) {
|
2013-09-12 13:31:21 +02:00
|
|
|
if (blobmsg_type(sattr) != BLOBMSG_TYPE_STRING)
|
2013-01-26 15:29:09 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!object)
|
|
|
|
object = blobmsg_data(sattr);
|
|
|
|
else if (!function)
|
|
|
|
function = blobmsg_data(sattr);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object && function)
|
2013-09-12 11:50:33 +02:00
|
|
|
cb(ses, scope, object, function);
|
2013-01-26 15:29:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2013-08-08 18:53:59 +02:00
|
|
|
rpc_session_acl_allowed(struct rpc_session *ses, const char *scope,
|
|
|
|
const char *obj, const char *fun)
|
2013-01-26 15:29:09 +01:00
|
|
|
{
|
|
|
|
struct rpc_session_acl *acl;
|
2013-08-08 18:53:59 +02:00
|
|
|
struct rpc_session_acl_scope *acl_scope;
|
2013-01-26 15:29:09 +01:00
|
|
|
|
2013-08-08 18:53:59 +02:00
|
|
|
acl_scope = avl_find_element(&ses->acls, scope, acl_scope, avl);
|
|
|
|
|
|
|
|
if (acl_scope) {
|
|
|
|
uh_foreach_matching_acl(acl, &acl_scope->acls, obj, fun)
|
|
|
|
return true;
|
|
|
|
}
|
2013-01-26 15:29:09 +01:00
|
|
|
|
|
|
|
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];
|
2013-08-08 18:53:59 +02:00
|
|
|
const char *scope = "ubus";
|
2013-01-26 15:29:09 +01:00
|
|
|
bool allow;
|
|
|
|
|
|
|
|
blobmsg_parse(perm_policy, __RPC_SP_MAX, tb, blob_data(msg), blob_len(msg));
|
|
|
|
|
2014-01-19 20:45:25 +01:00
|
|
|
if (!tb[RPC_SP_SID])
|
2013-01-26 15:29:09 +01:00
|
|
|
return UBUS_STATUS_INVALID_ARGUMENT;
|
|
|
|
|
|
|
|
ses = rpc_session_get(blobmsg_data(tb[RPC_SP_SID]));
|
|
|
|
if (!ses)
|
|
|
|
return UBUS_STATUS_NOT_FOUND;
|
|
|
|
|
2014-01-19 20:45:25 +01:00
|
|
|
blob_buf_init(&buf, 0);
|
2013-08-08 18:53:59 +02:00
|
|
|
|
2014-01-19 20:45:25 +01:00
|
|
|
if (tb[RPC_SP_OBJECT] && tb[RPC_SP_FUNCTION])
|
|
|
|
{
|
|
|
|
if (tb[RPC_SP_SCOPE])
|
|
|
|
scope = blobmsg_data(tb[RPC_SP_SCOPE]);
|
|
|
|
|
|
|
|
allow = rpc_session_acl_allowed(ses, scope,
|
|
|
|
blobmsg_data(tb[RPC_SP_OBJECT]),
|
|
|
|
blobmsg_data(tb[RPC_SP_FUNCTION]));
|
|
|
|
|
|
|
|
blobmsg_add_u8(&buf, "access", allow);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rpc_session_dump_acls(ses, &buf);
|
|
|
|
}
|
2013-01-26 15:29:09 +01:00
|
|
|
|
|
|
|
ubus_send_reply(ctx, req, buf.head);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-04 15:18:14 +02:00
|
|
|
static void
|
2018-03-15 12:05:31 +01:00
|
|
|
rpc_session_set(struct rpc_session *ses, struct blob_attr *val)
|
2013-09-04 15:18:14 +02:00
|
|
|
{
|
|
|
|
struct rpc_session_data *data;
|
|
|
|
|
2018-03-15 12:05:31 +01:00
|
|
|
data = avl_find_element(&ses->data, blobmsg_name(val), data, avl);
|
2013-09-04 15:18:14 +02:00
|
|
|
if (data) {
|
|
|
|
avl_delete(&ses->data, &data->avl);
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
data = calloc(1, sizeof(*data) + blob_pad_len(val));
|
|
|
|
if (!data)
|
|
|
|
return;
|
|
|
|
|
|
|
|
memcpy(data->attr, val, blob_pad_len(val));
|
|
|
|
data->avl.key = blobmsg_name(data->attr);
|
|
|
|
avl_insert(&ses->data, &data->avl);
|
|
|
|
}
|
|
|
|
|
2013-01-26 15:29:09 +01:00
|
|
|
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;
|
2013-09-12 11:51:58 +02:00
|
|
|
struct blob_attr *tb[__RPC_SS_MAX];
|
2013-01-26 15:29:09 +01:00
|
|
|
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;
|
|
|
|
|
2018-03-15 12:05:31 +01:00
|
|
|
rpc_session_set(ses, attr);
|
2013-01-26 15:29:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2013-09-12 11:51:58 +02:00
|
|
|
struct blob_attr *tb[__RPC_SG_MAX];
|
2013-01-26 15:29:09 +01:00
|
|
|
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");
|
|
|
|
|
2013-05-30 14:58:30 +02:00
|
|
|
if (tb[RPC_SG_KEYS])
|
|
|
|
blobmsg_for_each_attr(attr, tb[RPC_SG_KEYS], rem) {
|
2013-09-12 13:31:21 +02:00
|
|
|
if (blobmsg_type(attr) != BLOBMSG_TYPE_STRING)
|
2013-05-30 14:58:30 +02:00
|
|
|
continue;
|
2013-01-26 15:29:09 +01:00
|
|
|
|
2013-05-30 14:58:30 +02:00
|
|
|
data = avl_find_element(&ses->data, blobmsg_data(attr), data, avl);
|
|
|
|
if (!data)
|
|
|
|
continue;
|
2013-01-26 15:29:09 +01:00
|
|
|
|
2013-05-30 14:58:30 +02:00
|
|
|
blobmsg_add_field(&buf, blobmsg_type(data->attr),
|
|
|
|
blobmsg_name(data->attr),
|
|
|
|
blobmsg_data(data->attr),
|
|
|
|
blobmsg_data_len(data->attr));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rpc_session_dump_data(ses, &buf);
|
2013-01-26 15:29:09 +01:00
|
|
|
|
|
|
|
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) {
|
2013-09-12 13:31:21 +02:00
|
|
|
if (blobmsg_type(attr) != BLOBMSG_TYPE_STRING)
|
2013-01-26 15:29:09 +01:00
|
|
|
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;
|
|
|
|
|
2015-02-09 12:51:44 +01:00
|
|
|
blobmsg_parse(sid_policy, __RPC_SI_MAX, &tb, blob_data(msg), blob_len(msg));
|
2013-01-26 15:29:09 +01:00
|
|
|
|
|
|
|
if (!tb)
|
|
|
|
return UBUS_STATUS_INVALID_ARGUMENT;
|
|
|
|
|
2013-09-12 10:35:55 +02:00
|
|
|
if (!strcmp(blobmsg_get_string(tb), RPC_DEFAULT_SESSION_ID))
|
|
|
|
return UBUS_STATUS_PERMISSION_DENIED;
|
|
|
|
|
2013-01-26 15:29:09 +01:00
|
|
|
ses = rpc_session_get(blobmsg_data(tb));
|
|
|
|
if (!ses)
|
|
|
|
return UBUS_STATUS_NOT_FOUND;
|
|
|
|
|
|
|
|
rpc_session_destroy(ses);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-04 15:18:14 +02:00
|
|
|
|
2013-09-10 19:41:29 +02:00
|
|
|
static bool
|
|
|
|
rpc_login_test_password(const char *hash, const char *password)
|
|
|
|
{
|
|
|
|
char *crypt_hash;
|
|
|
|
|
|
|
|
/* password is not set */
|
2020-03-17 16:55:57 +01:00
|
|
|
if (!hash || !*hash)
|
2013-09-10 19:41:29 +02:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* password hash refers to shadow/passwd */
|
|
|
|
else if (!strncmp(hash, "$p$", 3))
|
|
|
|
{
|
|
|
|
#ifdef HAVE_SHADOW
|
|
|
|
struct spwd *sp = getspnam(hash + 3);
|
|
|
|
|
|
|
|
if (!sp)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return rpc_login_test_password(sp->sp_pwdp, password);
|
|
|
|
#else
|
|
|
|
struct passwd *pw = getpwnam(hash + 3);
|
|
|
|
|
|
|
|
if (!pw)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return rpc_login_test_password(pw->pw_passwd, password);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-09-10 20:12:39 +02:00
|
|
|
crypt_hash = crypt(password, hash);
|
2013-09-10 19:41:29 +02:00
|
|
|
|
2019-05-22 14:25:52 +02:00
|
|
|
return (crypt_hash && !strcmp(crypt_hash, hash));
|
2013-09-10 19:41:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct uci_section *
|
|
|
|
rpc_login_test_login(struct uci_context *uci,
|
|
|
|
const char *username, const char *password)
|
|
|
|
{
|
2013-09-10 20:12:39 +02:00
|
|
|
struct uci_package *p = NULL;
|
2013-09-10 19:41:29 +02:00
|
|
|
struct uci_section *s;
|
|
|
|
struct uci_element *e;
|
|
|
|
struct uci_ptr ptr = { .package = "rpcd" };
|
|
|
|
|
2021-07-13 20:31:16 +02:00
|
|
|
if (!uci_lookup_ptr(uci, &ptr, NULL, false) && ptr.p) {
|
|
|
|
uci_unload(uci, ptr.p);
|
|
|
|
ptr.flags = 0;
|
|
|
|
ptr.p = NULL;
|
|
|
|
}
|
|
|
|
|
2013-09-10 19:41:29 +02:00
|
|
|
uci_load(uci, ptr.package, &p);
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
uci_foreach_element(&p->sections, e)
|
|
|
|
{
|
|
|
|
s = uci_to_section(e);
|
|
|
|
|
|
|
|
if (strcmp(s->type, "login"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ptr.section = s->e.name;
|
|
|
|
ptr.s = NULL;
|
|
|
|
|
|
|
|
/* test for matching username */
|
|
|
|
ptr.option = "username";
|
|
|
|
ptr.o = NULL;
|
|
|
|
|
|
|
|
if (uci_lookup_ptr(uci, &ptr, NULL, true))
|
|
|
|
continue;
|
|
|
|
|
2024-05-28 15:58:29 +02:00
|
|
|
if (!ptr.o)
|
|
|
|
continue;
|
|
|
|
|
2013-09-10 19:41:29 +02:00
|
|
|
if (ptr.o->type != UCI_TYPE_STRING)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (strcmp(ptr.o->v.string, username))
|
|
|
|
continue;
|
|
|
|
|
2013-09-12 11:45:59 +02:00
|
|
|
/* If password is NULL, we're restoring ACLs for an existing session,
|
|
|
|
* in this case do not check the password again. */
|
|
|
|
if (!password)
|
|
|
|
return ptr.s;
|
|
|
|
|
2013-09-10 19:41:29 +02:00
|
|
|
/* test for matching password */
|
|
|
|
ptr.option = "password";
|
|
|
|
ptr.o = NULL;
|
|
|
|
|
|
|
|
if (uci_lookup_ptr(uci, &ptr, NULL, true))
|
|
|
|
continue;
|
|
|
|
|
2024-05-28 15:58:29 +02:00
|
|
|
if (!ptr.o)
|
|
|
|
continue;
|
|
|
|
|
2013-09-10 19:41:29 +02:00
|
|
|
if (ptr.o->type != UCI_TYPE_STRING)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (rpc_login_test_password(ptr.o->v.string, password))
|
|
|
|
return ptr.s;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
rpc_login_test_permission(struct uci_section *s,
|
|
|
|
const char *perm, const char *group)
|
|
|
|
{
|
2013-09-12 13:10:30 +02:00
|
|
|
const char *p;
|
2013-09-10 19:41:29 +02:00
|
|
|
struct uci_option *o;
|
|
|
|
struct uci_element *e, *l;
|
|
|
|
|
|
|
|
/* If the login section is not provided, we're setting up acls for the
|
|
|
|
* default session, in this case uncondionally allow access to the
|
|
|
|
* "unauthenticated" access group */
|
|
|
|
if (!s) {
|
|
|
|
return !strcmp(group, "unauthenticated");
|
|
|
|
}
|
|
|
|
|
|
|
|
uci_foreach_element(&s->options, e)
|
|
|
|
{
|
|
|
|
o = uci_to_option(e);
|
|
|
|
|
|
|
|
if (o->type != UCI_TYPE_LIST)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (strcmp(o->e.name, perm))
|
|
|
|
continue;
|
|
|
|
|
2013-09-12 13:10:30 +02:00
|
|
|
/* Match negative expressions first. If a negative expression matches
|
|
|
|
* the current group name then deny access. */
|
|
|
|
uci_foreach_element(&o->v.list, l) {
|
|
|
|
p = l->name;
|
|
|
|
|
|
|
|
if (!p || *p != '!')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
while (isspace(*++p));
|
|
|
|
|
|
|
|
if (!*p)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!fnmatch(p, group, 0))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uci_foreach_element(&o->v.list, l) {
|
|
|
|
if (!l->name || !*l->name || *l->name == '!')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!fnmatch(l->name, group, 0))
|
2013-09-10 19:41:29 +02:00
|
|
|
return true;
|
2013-09-12 13:10:30 +02:00
|
|
|
}
|
2013-09-10 19:41:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* make sure that write permission implies read permission */
|
|
|
|
if (!strcmp(perm, "read"))
|
|
|
|
return rpc_login_test_permission(s, "write", group);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rpc_login_setup_acl_scope(struct rpc_session *ses,
|
|
|
|
struct blob_attr *acl_perm,
|
|
|
|
struct blob_attr *acl_scope)
|
|
|
|
{
|
|
|
|
struct blob_attr *acl_obj, *acl_func;
|
|
|
|
int rem, rem2;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse ACL scopes in table notation.
|
|
|
|
*
|
|
|
|
* "<scope>": {
|
|
|
|
* "<object>": [
|
|
|
|
* "<function>",
|
|
|
|
* "<function>",
|
|
|
|
* ...
|
|
|
|
* ]
|
|
|
|
* }
|
|
|
|
*/
|
2013-09-12 13:31:21 +02:00
|
|
|
if (blobmsg_type(acl_scope) == BLOBMSG_TYPE_TABLE) {
|
2013-09-10 19:41:29 +02:00
|
|
|
blobmsg_for_each_attr(acl_obj, acl_scope, rem) {
|
2013-09-12 13:31:21 +02:00
|
|
|
if (blobmsg_type(acl_obj) != BLOBMSG_TYPE_ARRAY)
|
2013-09-10 19:41:29 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
blobmsg_for_each_attr(acl_func, acl_obj, rem2) {
|
2013-09-12 13:31:21 +02:00
|
|
|
if (blobmsg_type(acl_func) != BLOBMSG_TYPE_STRING)
|
2013-09-10 19:41:29 +02:00
|
|
|
continue;
|
|
|
|
|
2013-09-12 11:50:33 +02:00
|
|
|
rpc_session_grant(ses, blobmsg_name(acl_scope),
|
|
|
|
blobmsg_name(acl_obj),
|
|
|
|
blobmsg_data(acl_func));
|
2013-09-10 19:41:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse ACL scopes in array notation. The permission ("read" or "write")
|
|
|
|
* will be used as function name for each object.
|
|
|
|
*
|
|
|
|
* "<scope>": [
|
|
|
|
* "<object>",
|
|
|
|
* "<object>",
|
|
|
|
* ...
|
|
|
|
* ]
|
|
|
|
*/
|
2013-09-12 13:31:21 +02:00
|
|
|
else if (blobmsg_type(acl_scope) == BLOBMSG_TYPE_ARRAY) {
|
2013-09-10 19:41:29 +02:00
|
|
|
blobmsg_for_each_attr(acl_obj, acl_scope, rem) {
|
2013-09-12 13:31:21 +02:00
|
|
|
if (blobmsg_type(acl_obj) != BLOBMSG_TYPE_STRING)
|
2013-09-10 19:41:29 +02:00
|
|
|
continue;
|
|
|
|
|
2013-09-12 11:50:33 +02:00
|
|
|
rpc_session_grant(ses, blobmsg_name(acl_scope),
|
|
|
|
blobmsg_data(acl_obj),
|
|
|
|
blobmsg_name(acl_perm));
|
2013-09-10 19:41:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rpc_login_setup_acl_file(struct rpc_session *ses, struct uci_section *login,
|
|
|
|
const char *path)
|
|
|
|
{
|
|
|
|
struct blob_buf acl = { 0 };
|
|
|
|
struct blob_attr *acl_group, *acl_perm, *acl_scope;
|
|
|
|
int rem, rem2, rem3;
|
|
|
|
|
|
|
|
blob_buf_init(&acl, 0);
|
|
|
|
|
|
|
|
if (!blobmsg_add_json_from_file(&acl, path)) {
|
|
|
|
fprintf(stderr, "Failed to parse %s\n", path);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Iterate access groups in toplevel object */
|
|
|
|
blob_for_each_attr(acl_group, acl.head, rem) {
|
|
|
|
/* Iterate permission objects in each access group object */
|
|
|
|
blobmsg_for_each_attr(acl_perm, acl_group, rem2) {
|
2013-09-12 13:31:21 +02:00
|
|
|
if (blobmsg_type(acl_perm) != BLOBMSG_TYPE_TABLE)
|
2013-09-10 19:41:29 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Only "read" and "write" permissions are defined */
|
|
|
|
if (strcmp(blobmsg_name(acl_perm), "read") &&
|
|
|
|
strcmp(blobmsg_name(acl_perm), "write"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if the current user context specifies the current
|
|
|
|
* "read" or "write" permission in the given access group.
|
|
|
|
*/
|
|
|
|
if (!rpc_login_test_permission(login, blobmsg_name(acl_perm),
|
|
|
|
blobmsg_name(acl_group)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Iterate scope objects within the permission object */
|
|
|
|
blobmsg_for_each_attr(acl_scope, acl_perm, rem3) {
|
|
|
|
/* Setup the scopes of the access group */
|
|
|
|
rpc_login_setup_acl_scope(ses, acl_perm, acl_scope);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add the access group itself as object to the "access-group"
|
|
|
|
* meta scope and the the permission level ("read" or "write")
|
|
|
|
* as function, so
|
|
|
|
* "<group>": {
|
|
|
|
* "<permission>": {
|
|
|
|
* "<scope>": ...
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* becomes
|
|
|
|
* "access-group": {
|
|
|
|
* "<group>": [
|
|
|
|
* "<permission>"
|
|
|
|
* ]
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* This allows session clients to easily query the allowed
|
|
|
|
* access groups without having to test access of each single
|
|
|
|
* <scope>/<object>/<function> tuple defined in a group.
|
|
|
|
*/
|
2013-09-12 11:50:33 +02:00
|
|
|
rpc_session_grant(ses, "access-group",
|
|
|
|
blobmsg_name(acl_group),
|
|
|
|
blobmsg_name(acl_perm));
|
2013-09-10 19:41:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
blob_buf_free(&acl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rpc_login_setup_acls(struct rpc_session *ses, struct uci_section *login)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
glob_t gl;
|
|
|
|
|
|
|
|
if (glob(RPC_SESSION_ACL_DIR "/*.json", 0, NULL, &gl))
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < gl.gl_pathc; i++)
|
|
|
|
rpc_login_setup_acl_file(ses, login, gl.gl_pathv[i]);
|
|
|
|
|
|
|
|
globfree(&gl);
|
|
|
|
}
|
|
|
|
|
2018-03-15 11:22:47 +01:00
|
|
|
static struct rpc_session *
|
|
|
|
rpc_reclaim_apply_session(const char *expected_username)
|
|
|
|
{
|
|
|
|
struct rpc_session_data *username;
|
|
|
|
struct rpc_session *ses;
|
|
|
|
|
|
|
|
if (!apply_sid[0])
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ses = rpc_session_get(apply_sid);
|
|
|
|
|
|
|
|
if (!ses)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
username = avl_find_element(&ses->data, "username", username, avl);
|
|
|
|
|
|
|
|
if (!username || blobmsg_type(username->attr) != BLOBMSG_TYPE_STRING)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (strcmp(blobmsg_get_string(username->attr), expected_username))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return ses;
|
|
|
|
}
|
|
|
|
|
2013-09-10 19:41:29 +02:00
|
|
|
static int
|
|
|
|
rpc_handle_login(struct ubus_context *ctx, struct ubus_object *obj,
|
|
|
|
struct ubus_request_data *req, const char *method,
|
|
|
|
struct blob_attr *msg)
|
|
|
|
{
|
|
|
|
struct uci_context *uci = NULL;
|
|
|
|
struct uci_section *login;
|
|
|
|
struct rpc_session *ses;
|
|
|
|
struct blob_attr *tb[__RPC_L_MAX];
|
|
|
|
int timeout = RPC_DEFAULT_SESSION_TIMEOUT;
|
|
|
|
int rv = 0;
|
|
|
|
|
2013-09-10 20:01:05 +02:00
|
|
|
blobmsg_parse(login_policy, __RPC_L_MAX, tb, blob_data(msg), blob_len(msg));
|
2013-09-10 19:41:29 +02:00
|
|
|
|
|
|
|
if (!tb[RPC_L_USERNAME] || !tb[RPC_L_PASSWORD]) {
|
|
|
|
rv = UBUS_STATUS_INVALID_ARGUMENT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
uci = uci_alloc_context();
|
|
|
|
|
|
|
|
if (!uci) {
|
|
|
|
rv = UBUS_STATUS_UNKNOWN_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
login = rpc_login_test_login(uci, blobmsg_get_string(tb[RPC_L_USERNAME]),
|
|
|
|
blobmsg_get_string(tb[RPC_L_PASSWORD]));
|
|
|
|
|
|
|
|
if (!login) {
|
|
|
|
rv = UBUS_STATUS_PERMISSION_DENIED;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tb[RPC_L_TIMEOUT])
|
|
|
|
timeout = blobmsg_get_u32(tb[RPC_L_TIMEOUT]);
|
|
|
|
|
2018-03-15 11:22:47 +01:00
|
|
|
/*
|
|
|
|
* attempt to reclaim a pending apply session, but only accept it
|
|
|
|
* if the username matches, otherwise perform a new login
|
|
|
|
*/
|
|
|
|
|
|
|
|
ses = rpc_reclaim_apply_session(blobmsg_get_string(tb[RPC_L_USERNAME]));
|
|
|
|
|
|
|
|
if (!ses)
|
|
|
|
ses = rpc_session_create(timeout);
|
2013-09-10 19:41:29 +02:00
|
|
|
|
|
|
|
if (!ses) {
|
|
|
|
rv = UBUS_STATUS_UNKNOWN_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
rpc_login_setup_acls(ses, login);
|
|
|
|
|
2018-03-15 12:05:31 +01:00
|
|
|
rpc_session_set(ses, tb[RPC_L_USERNAME]);
|
2013-09-10 19:41:29 +02:00
|
|
|
rpc_session_dump(ses, ctx, req);
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (uci)
|
|
|
|
uci_free_context(uci);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-04 15:18:14 +02:00
|
|
|
static bool
|
|
|
|
rpc_validate_sid(const char *id)
|
|
|
|
{
|
|
|
|
if (!id)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (strlen(id) != RPC_SID_LEN)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
while (*id)
|
|
|
|
if (!isxdigit(*id++))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
rpc_blob_to_file(const char *path, struct blob_attr *attr)
|
|
|
|
{
|
|
|
|
int fd, len;
|
|
|
|
|
|
|
|
fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0600);
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
return fd;
|
|
|
|
|
|
|
|
len = write(fd, attr, blob_pad_len(attr));
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (len != blob_pad_len(attr))
|
|
|
|
{
|
|
|
|
unlink(path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct blob_attr *
|
|
|
|
rpc_blob_from_file(const char *path)
|
|
|
|
{
|
|
|
|
int fd = -1, len;
|
|
|
|
struct stat s;
|
|
|
|
struct blob_attr head, *attr = NULL;
|
|
|
|
|
|
|
|
if (stat(path, &s) || !S_ISREG(s.st_mode))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
len = read(fd, &head, sizeof(head));
|
|
|
|
|
|
|
|
if (len != sizeof(head) || blob_pad_len(&head) != s.st_size)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
attr = calloc(1, s.st_size);
|
|
|
|
|
|
|
|
if (!attr)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
memcpy(attr, &head, sizeof(head));
|
|
|
|
|
|
|
|
len += read(fd, (char *)attr + sizeof(head), s.st_size - sizeof(head));
|
|
|
|
|
|
|
|
if (len != blob_pad_len(&head))
|
|
|
|
goto fail;
|
|
|
|
|
2013-10-26 16:16:29 +02:00
|
|
|
close(fd);
|
|
|
|
|
2013-09-04 15:18:14 +02:00
|
|
|
return attr;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (attr)
|
|
|
|
free(attr);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2013-09-12 11:45:59 +02:00
|
|
|
rpc_session_from_blob(struct uci_context *uci, struct blob_attr *attr)
|
2013-09-04 15:18:14 +02:00
|
|
|
{
|
2013-09-12 11:45:59 +02:00
|
|
|
int i, rem;
|
|
|
|
const char *user = NULL;
|
2013-09-04 15:18:14 +02:00
|
|
|
struct rpc_session *ses;
|
2013-09-12 11:45:59 +02:00
|
|
|
struct uci_section *login;
|
|
|
|
struct blob_attr *tb[__RPC_DUMP_MAX], *data;
|
2013-09-04 15:18:14 +02:00
|
|
|
|
|
|
|
blobmsg_parse(dump_policy, __RPC_DUMP_MAX, tb,
|
|
|
|
blob_data(attr), blob_len(attr));
|
|
|
|
|
|
|
|
for (i = 0; i < __RPC_DUMP_MAX; i++)
|
|
|
|
if (!tb[i])
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ses = rpc_session_new();
|
|
|
|
|
|
|
|
if (!ses)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
memcpy(ses->id, blobmsg_data(tb[RPC_DUMP_SID]), RPC_SID_LEN);
|
|
|
|
|
|
|
|
ses->timeout = blobmsg_get_u32(tb[RPC_DUMP_TIMEOUT]);
|
|
|
|
|
2013-09-12 11:45:59 +02:00
|
|
|
blobmsg_for_each_attr(data, tb[RPC_DUMP_DATA], rem) {
|
2018-03-15 12:05:31 +01:00
|
|
|
rpc_session_set(ses, data);
|
2013-09-12 11:45:59 +02:00
|
|
|
|
2018-03-15 12:22:34 +01:00
|
|
|
if (blobmsg_type(data) != BLOBMSG_TYPE_STRING)
|
|
|
|
continue;
|
|
|
|
|
2013-09-12 11:45:59 +02:00
|
|
|
if (!strcmp(blobmsg_name(data), "username"))
|
|
|
|
user = blobmsg_get_string(data);
|
2013-09-04 15:18:14 +02:00
|
|
|
}
|
|
|
|
|
2013-09-12 11:45:59 +02:00
|
|
|
if (uci && user) {
|
|
|
|
login = rpc_login_test_login(uci, user, NULL);
|
|
|
|
if (login)
|
|
|
|
rpc_login_setup_acls(ses, login);
|
2013-09-04 15:18:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
avl_insert(&sessions, &ses->avl);
|
|
|
|
|
2021-11-04 13:38:01 +01:00
|
|
|
uloop_timeout_set(&ses->t, blobmsg_get_u64(tb[RPC_DUMP_EXPIRES]) * 1000);
|
2013-09-04 15:18:14 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-26 15:29:09 +01:00
|
|
|
int rpc_session_api_init(struct ubus_context *ctx)
|
|
|
|
{
|
2013-09-10 19:41:29 +02:00
|
|
|
struct rpc_session *ses;
|
|
|
|
|
2013-01-26 15:29:09 +01:00
|
|
|
static const struct ubus_method session_methods[] = {
|
2015-02-09 12:51:44 +01:00
|
|
|
UBUS_METHOD("create", rpc_handle_create, new_policy),
|
|
|
|
UBUS_METHOD("list", rpc_handle_list, sid_policy),
|
2013-01-26 15:29:09 +01:00
|
|
|
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),
|
2015-02-09 12:51:44 +01:00
|
|
|
UBUS_METHOD("destroy", rpc_handle_destroy, sid_policy),
|
2013-09-10 19:41:29 +02:00
|
|
|
UBUS_METHOD("login", rpc_handle_login, login_policy),
|
2013-01-26 15:29:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct ubus_object_type session_type =
|
2021-12-08 20:07:28 +01:00
|
|
|
UBUS_OBJECT_TYPE("rpcd-plugin-session", session_methods);
|
2013-01-26 15:29:09 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2013-09-10 19:41:29 +02:00
|
|
|
/* setup the default session */
|
|
|
|
ses = rpc_session_new();
|
|
|
|
|
|
|
|
if (ses) {
|
|
|
|
strcpy(ses->id, RPC_DEFAULT_SESSION_ID);
|
|
|
|
rpc_login_setup_acls(ses, NULL);
|
|
|
|
avl_insert(&sessions, &ses->avl);
|
|
|
|
}
|
|
|
|
|
2013-01-26 15:29:09 +01:00
|
|
|
return ubus_add_object(ctx, &obj);
|
|
|
|
}
|
2013-08-12 13:51:51 +02:00
|
|
|
|
|
|
|
bool rpc_session_access(const char *sid, const char *scope,
|
|
|
|
const char *object, const char *function)
|
|
|
|
{
|
|
|
|
struct rpc_session *ses = rpc_session_get(sid);
|
|
|
|
|
|
|
|
if (!ses)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return rpc_session_acl_allowed(ses, scope, object, function);
|
|
|
|
}
|
2013-09-03 12:21:09 +02:00
|
|
|
|
|
|
|
void rpc_session_create_cb(struct rpc_session_cb *cb)
|
|
|
|
{
|
|
|
|
if (cb && cb->cb)
|
|
|
|
list_add(&cb->list, &create_callbacks);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rpc_session_destroy_cb(struct rpc_session_cb *cb)
|
|
|
|
{
|
|
|
|
if (cb && cb->cb)
|
|
|
|
list_add(&cb->list, &destroy_callbacks);
|
|
|
|
}
|
2013-09-04 15:18:14 +02:00
|
|
|
|
|
|
|
void rpc_session_freeze(void)
|
|
|
|
{
|
|
|
|
struct stat s;
|
|
|
|
struct rpc_session *ses;
|
|
|
|
char path[PATH_MAX];
|
|
|
|
|
|
|
|
if (stat(RPC_SESSION_DIRECTORY, &s))
|
|
|
|
mkdir(RPC_SESSION_DIRECTORY, 0700);
|
|
|
|
|
|
|
|
avl_for_each_element(&sessions, ses, avl) {
|
2013-09-10 19:41:29 +02:00
|
|
|
/* skip default session */
|
|
|
|
if (!strcmp(ses->id, RPC_DEFAULT_SESSION_ID))
|
|
|
|
continue;
|
|
|
|
|
2013-09-04 15:18:14 +02:00
|
|
|
snprintf(path, sizeof(path) - 1, RPC_SESSION_DIRECTORY "/%s", ses->id);
|
2013-09-12 12:13:05 +02:00
|
|
|
rpc_session_to_blob(ses, false);
|
2013-09-04 15:18:14 +02:00
|
|
|
rpc_blob_to_file(path, buf.head);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void rpc_session_thaw(void)
|
|
|
|
{
|
|
|
|
DIR *d;
|
|
|
|
char path[PATH_MAX];
|
|
|
|
struct dirent *e;
|
|
|
|
struct blob_attr *attr;
|
2013-09-12 11:45:59 +02:00
|
|
|
struct uci_context *uci;
|
2013-09-04 15:18:14 +02:00
|
|
|
|
|
|
|
d = opendir(RPC_SESSION_DIRECTORY);
|
|
|
|
|
|
|
|
if (!d)
|
|
|
|
return;
|
|
|
|
|
2013-09-12 11:45:59 +02:00
|
|
|
uci = uci_alloc_context();
|
|
|
|
|
|
|
|
if (!uci)
|
|
|
|
return;
|
|
|
|
|
2013-09-04 15:18:14 +02:00
|
|
|
while ((e = readdir(d)) != NULL) {
|
|
|
|
if (!rpc_validate_sid(e->d_name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
snprintf(path, sizeof(path) - 1,
|
|
|
|
RPC_SESSION_DIRECTORY "/%s", e->d_name);
|
|
|
|
|
|
|
|
attr = rpc_blob_from_file(path);
|
|
|
|
|
|
|
|
if (attr) {
|
2013-09-12 11:45:59 +02:00
|
|
|
rpc_session_from_blob(uci, attr);
|
2013-09-04 15:18:14 +02:00
|
|
|
free(attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
unlink(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(d);
|
2013-09-12 11:45:59 +02:00
|
|
|
|
|
|
|
uci_free_context(uci);
|
2013-09-04 15:18:14 +02:00
|
|
|
}
|