2 * rpcd - UBUS RPC server
4 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
5 * Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #define _GNU_SOURCE /* crypt() */
22 #include <libubox/avl-cmp.h>
23 #include <libubox/blobmsg.h>
24 #include <libubox/utils.h>
35 #include <rpcd/session.h>
37 static struct avl_tree sessions;
38 static struct blob_buf buf;
40 static LIST_HEAD(create_callbacks);
41 static LIST_HEAD(destroy_callbacks);
43 static const struct blobmsg_policy new_policy = {
44 .name = "timeout", .type = BLOBMSG_TYPE_INT32
47 static const struct blobmsg_policy sid_policy = {
48 .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING
56 static const struct blobmsg_policy set_policy[__RPC_SS_MAX] = {
57 [RPC_SS_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
58 [RPC_SS_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE },
66 static const struct blobmsg_policy get_policy[__RPC_SG_MAX] = {
67 [RPC_SG_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
68 [RPC_SG_KEYS] = { .name = "keys", .type = BLOBMSG_TYPE_ARRAY },
77 static const struct blobmsg_policy acl_policy[__RPC_SA_MAX] = {
78 [RPC_SA_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
79 [RPC_SA_SCOPE] = { .name = "scope", .type = BLOBMSG_TYPE_STRING },
80 [RPC_SA_OBJECTS] = { .name = "objects", .type = BLOBMSG_TYPE_ARRAY },
90 static const struct blobmsg_policy perm_policy[__RPC_SP_MAX] = {
91 [RPC_SP_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
92 [RPC_SP_SCOPE] = { .name = "scope", .type = BLOBMSG_TYPE_STRING },
93 [RPC_SP_OBJECT] = { .name = "object", .type = BLOBMSG_TYPE_STRING },
94 [RPC_SP_FUNCTION] = { .name = "function", .type = BLOBMSG_TYPE_STRING },
104 static const struct blobmsg_policy dump_policy[__RPC_DUMP_MAX] = {
105 [RPC_DUMP_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
106 [RPC_DUMP_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
107 [RPC_DUMP_EXPIRES] = { .name = "expires", .type = BLOBMSG_TYPE_INT32 },
108 [RPC_DUMP_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
117 static const struct blobmsg_policy login_policy[__RPC_L_MAX] = {
118 [RPC_L_USERNAME] = { .name = "username", .type = BLOBMSG_TYPE_STRING },
119 [RPC_L_PASSWORD] = { .name = "password", .type = BLOBMSG_TYPE_STRING },
120 [RPC_L_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
124 * Keys in the AVL tree contain all pattern characters up to the first wildcard.
125 * To look up entries, start with the last entry that has a key less than or
126 * equal to the method name, then work backwards as long as the AVL key still
127 * matches its counterpart in the object name
129 #define uh_foreach_matching_acl_prefix(_acl, _avl, _obj, _func) \
130 for (_acl = avl_find_le_element(_avl, _obj, _acl, avl); \
132 _acl = avl_is_first(_avl, &(_acl)->avl) ? NULL : \
133 avl_prev_element((_acl), avl))
135 #define uh_foreach_matching_acl(_acl, _avl, _obj, _func) \
136 uh_foreach_matching_acl_prefix(_acl, _avl, _obj, _func) \
137 if (!strncmp((_acl)->object, _obj, (_acl)->sort_len) && \
138 !fnmatch((_acl)->object, (_obj), FNM_NOESCAPE) && \
139 !fnmatch((_acl)->function, (_func), FNM_NOESCAPE))
142 rpc_random(char *dest)
144 unsigned char buf[16] = { 0 };
148 f = fopen("/dev/urandom", "r");
152 fread(buf, 1, sizeof(buf), f);
155 for (i = 0; i < sizeof(buf); i++)
156 sprintf(dest + (i<<1), "%02x", buf[i]);
160 rpc_session_dump_data(struct rpc_session *ses, struct blob_buf *b)
162 struct rpc_session_data *d;
164 avl_for_each_element(&ses->data, d, avl) {
165 blobmsg_add_field(b, blobmsg_type(d->attr), blobmsg_name(d->attr),
166 blobmsg_data(d->attr), blobmsg_data_len(d->attr));
171 rpc_session_dump_acls(struct rpc_session *ses, struct blob_buf *b)
173 struct rpc_session_acl *acl;
174 struct rpc_session_acl_scope *acl_scope;
175 const char *lastobj = NULL;
176 const char *lastscope = NULL;
177 void *c = NULL, *d = NULL;
179 avl_for_each_element(&ses->acls, acl_scope, avl) {
180 if (!lastscope || strcmp(acl_scope->avl.key, lastscope))
182 if (c) blobmsg_close_table(b, c);
183 c = blobmsg_open_table(b, acl_scope->avl.key);
189 avl_for_each_element(&acl_scope->acls, acl, avl) {
190 if (!lastobj || strcmp(acl->object, lastobj))
192 if (d) blobmsg_close_array(b, d);
193 d = blobmsg_open_array(b, acl->object);
196 blobmsg_add_string(b, NULL, acl->function);
197 lastobj = acl->object;
200 if (d) blobmsg_close_array(b, d);
203 if (c) blobmsg_close_table(b, c);
207 rpc_session_to_blob(struct rpc_session *ses, bool acls)
211 blob_buf_init(&buf, 0);
213 blobmsg_add_string(&buf, "ubus_rpc_session", ses->id);
214 blobmsg_add_u32(&buf, "timeout", ses->timeout);
215 blobmsg_add_u32(&buf, "expires", uloop_timeout_remaining(&ses->t) / 1000);
218 c = blobmsg_open_table(&buf, "acls");
219 rpc_session_dump_acls(ses, &buf);
220 blobmsg_close_table(&buf, c);
223 c = blobmsg_open_table(&buf, "data");
224 rpc_session_dump_data(ses, &buf);
225 blobmsg_close_table(&buf, c);
229 rpc_session_dump(struct rpc_session *ses, struct ubus_context *ctx,
230 struct ubus_request_data *req)
232 rpc_session_to_blob(ses, true);
234 ubus_send_reply(ctx, req, buf.head);
238 rpc_touch_session(struct rpc_session *ses)
240 if (ses->timeout > 0)
241 uloop_timeout_set(&ses->t, ses->timeout * 1000);
245 rpc_session_destroy(struct rpc_session *ses)
247 struct rpc_session_acl *acl, *nacl;
248 struct rpc_session_acl_scope *acl_scope, *nacl_scope;
249 struct rpc_session_data *data, *ndata;
250 struct rpc_session_cb *cb;
252 list_for_each_entry(cb, &destroy_callbacks, list)
253 cb->cb(ses, cb->priv);
255 uloop_timeout_cancel(&ses->t);
257 avl_for_each_element_safe(&ses->acls, acl_scope, avl, nacl_scope) {
258 avl_remove_all_elements(&acl_scope->acls, acl, avl, nacl)
261 avl_delete(&ses->acls, &acl_scope->avl);
265 avl_remove_all_elements(&ses->data, data, avl, ndata)
268 avl_delete(&sessions, &ses->avl);
272 static void rpc_session_timeout(struct uloop_timeout *t)
274 struct rpc_session *ses;
276 ses = container_of(t, struct rpc_session, t);
277 rpc_session_destroy(ses);
280 static struct rpc_session *
281 rpc_session_new(void)
283 struct rpc_session *ses;
285 ses = calloc(1, sizeof(*ses));
290 ses->avl.key = ses->id;
292 avl_init(&ses->acls, avl_strcmp, true, NULL);
293 avl_init(&ses->data, avl_strcmp, false, NULL);
295 ses->t.cb = rpc_session_timeout;
300 static struct rpc_session *
301 rpc_session_create(int timeout)
303 struct rpc_session *ses;
304 struct rpc_session_cb *cb;
306 ses = rpc_session_new();
313 ses->timeout = timeout;
315 avl_insert(&sessions, &ses->avl);
317 rpc_touch_session(ses);
319 list_for_each_entry(cb, &create_callbacks, list)
320 cb->cb(ses, cb->priv);
325 static struct rpc_session *
326 rpc_session_get(const char *id)
328 struct rpc_session *ses;
330 ses = avl_find_element(&sessions, id, ses, avl);
334 rpc_touch_session(ses);
339 rpc_handle_create(struct ubus_context *ctx, struct ubus_object *obj,
340 struct ubus_request_data *req, const char *method,
341 struct blob_attr *msg)
343 struct rpc_session *ses;
344 struct blob_attr *tb;
345 int timeout = RPC_DEFAULT_SESSION_TIMEOUT;
347 blobmsg_parse(&new_policy, 1, &tb, blob_data(msg), blob_len(msg));
349 timeout = blobmsg_get_u32(tb);
351 ses = rpc_session_create(timeout);
353 rpc_session_dump(ses, ctx, req);
359 rpc_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
360 struct ubus_request_data *req, const char *method,
361 struct blob_attr *msg)
363 struct rpc_session *ses;
364 struct blob_attr *tb;
366 blobmsg_parse(&sid_policy, 1, &tb, blob_data(msg), blob_len(msg));
369 avl_for_each_element(&sessions, ses, avl)
370 rpc_session_dump(ses, ctx, req);
374 ses = rpc_session_get(blobmsg_data(tb));
376 return UBUS_STATUS_NOT_FOUND;
378 rpc_session_dump(ses, ctx, req);
384 uh_id_len(const char *str)
386 return strcspn(str, "*?[");
390 rpc_session_grant(struct rpc_session *ses,
391 const char *scope, const char *object, const char *function)
393 struct rpc_session_acl *acl;
394 struct rpc_session_acl_scope *acl_scope;
395 char *new_scope, *new_obj, *new_func, *new_id;
398 if (!object || !function)
399 return UBUS_STATUS_INVALID_ARGUMENT;
401 acl_scope = avl_find_element(&ses->acls, scope, acl_scope, avl);
404 uh_foreach_matching_acl_prefix(acl, &acl_scope->acls, object, function) {
405 if (!strcmp(acl->object, object) &&
406 !strcmp(acl->function, function))
412 acl_scope = calloc_a(sizeof(*acl_scope),
413 &new_scope, strlen(scope) + 1);
416 return UBUS_STATUS_UNKNOWN_ERROR;
418 acl_scope->avl.key = strcpy(new_scope, scope);
419 avl_init(&acl_scope->acls, avl_strcmp, true, NULL);
420 avl_insert(&ses->acls, &acl_scope->avl);
423 id_len = uh_id_len(object);
424 acl = calloc_a(sizeof(*acl),
425 &new_obj, strlen(object) + 1,
426 &new_func, strlen(function) + 1,
427 &new_id, id_len + 1);
430 return UBUS_STATUS_UNKNOWN_ERROR;
432 acl->object = strcpy(new_obj, object);
433 acl->function = strcpy(new_func, function);
434 acl->avl.key = strncpy(new_id, object, id_len);
435 avl_insert(&acl_scope->acls, &acl->avl);
441 rpc_session_revoke(struct rpc_session *ses,
442 const char *scope, const char *object, const char *function)
444 struct rpc_session_acl *acl, *next;
445 struct rpc_session_acl_scope *acl_scope;
449 acl_scope = avl_find_element(&ses->acls, scope, acl_scope, avl);
454 if (!object && !function) {
455 avl_remove_all_elements(&acl_scope->acls, acl, avl, next)
457 avl_delete(&ses->acls, &acl_scope->avl);
462 id_len = uh_id_len(object);
463 id = alloca(id_len + 1);
464 strncpy(id, object, id_len);
467 acl = avl_find_element(&acl_scope->acls, id, acl, avl);
469 if (!avl_is_last(&acl_scope->acls, &acl->avl))
470 next = avl_next_element(acl, avl);
474 if (strcmp(id, acl->avl.key) != 0)
477 if (!strcmp(acl->object, object) &&
478 !strcmp(acl->function, function)) {
479 avl_delete(&acl_scope->acls, &acl->avl);
485 if (avl_is_empty(&acl_scope->acls)) {
486 avl_delete(&ses->acls, &acl_scope->avl);
495 rpc_handle_acl(struct ubus_context *ctx, struct ubus_object *obj,
496 struct ubus_request_data *req, const char *method,
497 struct blob_attr *msg)
499 struct rpc_session *ses;
500 struct blob_attr *tb[__RPC_SA_MAX];
501 struct blob_attr *attr, *sattr;
502 const char *object, *function;
503 const char *scope = "ubus";
506 int (*cb)(struct rpc_session *ses,
507 const char *scope, const char *object, const char *function);
509 blobmsg_parse(acl_policy, __RPC_SA_MAX, tb, blob_data(msg), blob_len(msg));
512 return UBUS_STATUS_INVALID_ARGUMENT;
514 ses = rpc_session_get(blobmsg_data(tb[RPC_SA_SID]));
516 return UBUS_STATUS_NOT_FOUND;
518 if (tb[RPC_SA_SCOPE])
519 scope = blobmsg_data(tb[RPC_SA_SCOPE]);
521 if (!strcmp(method, "grant"))
522 cb = rpc_session_grant;
524 cb = rpc_session_revoke;
526 if (!tb[RPC_SA_OBJECTS])
527 return cb(ses, scope, NULL, NULL);
529 blobmsg_for_each_attr(attr, tb[RPC_SA_OBJECTS], rem1) {
530 if (blobmsg_type(attr) != BLOBMSG_TYPE_ARRAY)
536 blobmsg_for_each_attr(sattr, attr, rem2) {
537 if (blobmsg_type(sattr) != BLOBMSG_TYPE_STRING)
541 object = blobmsg_data(sattr);
543 function = blobmsg_data(sattr);
548 if (object && function)
549 cb(ses, scope, object, function);
556 rpc_session_acl_allowed(struct rpc_session *ses, const char *scope,
557 const char *obj, const char *fun)
559 struct rpc_session_acl *acl;
560 struct rpc_session_acl_scope *acl_scope;
562 acl_scope = avl_find_element(&ses->acls, scope, acl_scope, avl);
565 uh_foreach_matching_acl(acl, &acl_scope->acls, obj, fun)
573 rpc_handle_access(struct ubus_context *ctx, struct ubus_object *obj,
574 struct ubus_request_data *req, const char *method,
575 struct blob_attr *msg)
577 struct rpc_session *ses;
578 struct blob_attr *tb[__RPC_SP_MAX];
579 const char *scope = "ubus";
582 blobmsg_parse(perm_policy, __RPC_SP_MAX, tb, blob_data(msg), blob_len(msg));
585 return UBUS_STATUS_INVALID_ARGUMENT;
587 ses = rpc_session_get(blobmsg_data(tb[RPC_SP_SID]));
589 return UBUS_STATUS_NOT_FOUND;
591 blob_buf_init(&buf, 0);
593 if (tb[RPC_SP_OBJECT] && tb[RPC_SP_FUNCTION])
595 if (tb[RPC_SP_SCOPE])
596 scope = blobmsg_data(tb[RPC_SP_SCOPE]);
598 allow = rpc_session_acl_allowed(ses, scope,
599 blobmsg_data(tb[RPC_SP_OBJECT]),
600 blobmsg_data(tb[RPC_SP_FUNCTION]));
602 blobmsg_add_u8(&buf, "access", allow);
606 rpc_session_dump_acls(ses, &buf);
609 ubus_send_reply(ctx, req, buf.head);
615 rpc_session_set(struct rpc_session *ses, const char *key, struct blob_attr *val)
617 struct rpc_session_data *data;
619 data = avl_find_element(&ses->data, key, data, avl);
621 avl_delete(&ses->data, &data->avl);
625 data = calloc(1, sizeof(*data) + blob_pad_len(val));
629 memcpy(data->attr, val, blob_pad_len(val));
630 data->avl.key = blobmsg_name(data->attr);
631 avl_insert(&ses->data, &data->avl);
635 rpc_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
636 struct ubus_request_data *req, const char *method,
637 struct blob_attr *msg)
639 struct rpc_session *ses;
640 struct blob_attr *tb[__RPC_SS_MAX];
641 struct blob_attr *attr;
644 blobmsg_parse(set_policy, __RPC_SS_MAX, tb, blob_data(msg), blob_len(msg));
646 if (!tb[RPC_SS_SID] || !tb[RPC_SS_VALUES])
647 return UBUS_STATUS_INVALID_ARGUMENT;
649 ses = rpc_session_get(blobmsg_data(tb[RPC_SS_SID]));
651 return UBUS_STATUS_NOT_FOUND;
653 blobmsg_for_each_attr(attr, tb[RPC_SS_VALUES], rem) {
654 if (!blobmsg_name(attr)[0])
657 rpc_session_set(ses, blobmsg_name(attr), attr);
664 rpc_handle_get(struct ubus_context *ctx, struct ubus_object *obj,
665 struct ubus_request_data *req, const char *method,
666 struct blob_attr *msg)
668 struct rpc_session *ses;
669 struct rpc_session_data *data;
670 struct blob_attr *tb[__RPC_SG_MAX];
671 struct blob_attr *attr;
675 blobmsg_parse(get_policy, __RPC_SG_MAX, tb, blob_data(msg), blob_len(msg));
678 return UBUS_STATUS_INVALID_ARGUMENT;
680 ses = rpc_session_get(blobmsg_data(tb[RPC_SG_SID]));
682 return UBUS_STATUS_NOT_FOUND;
684 blob_buf_init(&buf, 0);
685 c = blobmsg_open_table(&buf, "values");
688 blobmsg_for_each_attr(attr, tb[RPC_SG_KEYS], rem) {
689 if (blobmsg_type(attr) != BLOBMSG_TYPE_STRING)
692 data = avl_find_element(&ses->data, blobmsg_data(attr), data, avl);
696 blobmsg_add_field(&buf, blobmsg_type(data->attr),
697 blobmsg_name(data->attr),
698 blobmsg_data(data->attr),
699 blobmsg_data_len(data->attr));
702 rpc_session_dump_data(ses, &buf);
704 blobmsg_close_table(&buf, c);
705 ubus_send_reply(ctx, req, buf.head);
711 rpc_handle_unset(struct ubus_context *ctx, struct ubus_object *obj,
712 struct ubus_request_data *req, const char *method,
713 struct blob_attr *msg)
715 struct rpc_session *ses;
716 struct rpc_session_data *data, *ndata;
717 struct blob_attr *tb[__RPC_SA_MAX];
718 struct blob_attr *attr;
721 blobmsg_parse(get_policy, __RPC_SG_MAX, tb, blob_data(msg), blob_len(msg));
724 return UBUS_STATUS_INVALID_ARGUMENT;
726 ses = rpc_session_get(blobmsg_data(tb[RPC_SG_SID]));
728 return UBUS_STATUS_NOT_FOUND;
730 if (!tb[RPC_SG_KEYS]) {
731 avl_remove_all_elements(&ses->data, data, avl, ndata)
736 blobmsg_for_each_attr(attr, tb[RPC_SG_KEYS], rem) {
737 if (blobmsg_type(attr) != BLOBMSG_TYPE_STRING)
740 data = avl_find_element(&ses->data, blobmsg_data(attr), data, avl);
744 avl_delete(&ses->data, &data->avl);
752 rpc_handle_destroy(struct ubus_context *ctx, struct ubus_object *obj,
753 struct ubus_request_data *req, const char *method,
754 struct blob_attr *msg)
756 struct rpc_session *ses;
757 struct blob_attr *tb;
759 blobmsg_parse(&sid_policy, 1, &tb, blob_data(msg), blob_len(msg));
762 return UBUS_STATUS_INVALID_ARGUMENT;
764 if (!strcmp(blobmsg_get_string(tb), RPC_DEFAULT_SESSION_ID))
765 return UBUS_STATUS_PERMISSION_DENIED;
767 ses = rpc_session_get(blobmsg_data(tb));
769 return UBUS_STATUS_NOT_FOUND;
771 rpc_session_destroy(ses);
778 rpc_login_test_password(const char *hash, const char *password)
782 /* password is not set */
783 if (!hash || !*hash || !strcmp(hash, "!") || !strcmp(hash, "x"))
788 /* password hash refers to shadow/passwd */
789 else if (!strncmp(hash, "$p$", 3))
792 struct spwd *sp = getspnam(hash + 3);
797 return rpc_login_test_password(sp->sp_pwdp, password);
799 struct passwd *pw = getpwnam(hash + 3);
804 return rpc_login_test_password(pw->pw_passwd, password);
808 crypt_hash = crypt(password, hash);
810 return !strcmp(crypt_hash, hash);
813 static struct uci_section *
814 rpc_login_test_login(struct uci_context *uci,
815 const char *username, const char *password)
817 struct uci_package *p = NULL;
818 struct uci_section *s;
819 struct uci_element *e;
820 struct uci_ptr ptr = { .package = "rpcd" };
822 uci_load(uci, ptr.package, &p);
827 uci_foreach_element(&p->sections, e)
829 s = uci_to_section(e);
831 if (strcmp(s->type, "login"))
834 ptr.section = s->e.name;
837 /* test for matching username */
838 ptr.option = "username";
841 if (uci_lookup_ptr(uci, &ptr, NULL, true))
844 if (ptr.o->type != UCI_TYPE_STRING)
847 if (strcmp(ptr.o->v.string, username))
850 /* If password is NULL, we're restoring ACLs for an existing session,
851 * in this case do not check the password again. */
855 /* test for matching password */
856 ptr.option = "password";
859 if (uci_lookup_ptr(uci, &ptr, NULL, true))
862 if (ptr.o->type != UCI_TYPE_STRING)
865 if (rpc_login_test_password(ptr.o->v.string, password))
873 rpc_login_test_permission(struct uci_section *s,
874 const char *perm, const char *group)
877 struct uci_option *o;
878 struct uci_element *e, *l;
880 /* If the login section is not provided, we're setting up acls for the
881 * default session, in this case uncondionally allow access to the
882 * "unauthenticated" access group */
884 return !strcmp(group, "unauthenticated");
887 uci_foreach_element(&s->options, e)
889 o = uci_to_option(e);
891 if (o->type != UCI_TYPE_LIST)
894 if (strcmp(o->e.name, perm))
897 /* Match negative expressions first. If a negative expression matches
898 * the current group name then deny access. */
899 uci_foreach_element(&o->v.list, l) {
905 while (isspace(*++p));
910 if (!fnmatch(p, group, 0))
914 uci_foreach_element(&o->v.list, l) {
915 if (!l->name || !*l->name || *l->name == '!')
918 if (!fnmatch(l->name, group, 0))
923 /* make sure that write permission implies read permission */
924 if (!strcmp(perm, "read"))
925 return rpc_login_test_permission(s, "write", group);
931 rpc_login_setup_acl_scope(struct rpc_session *ses,
932 struct blob_attr *acl_perm,
933 struct blob_attr *acl_scope)
935 struct blob_attr *acl_obj, *acl_func;
939 * Parse ACL scopes in table notation.
949 if (blobmsg_type(acl_scope) == BLOBMSG_TYPE_TABLE) {
950 blobmsg_for_each_attr(acl_obj, acl_scope, rem) {
951 if (blobmsg_type(acl_obj) != BLOBMSG_TYPE_ARRAY)
954 blobmsg_for_each_attr(acl_func, acl_obj, rem2) {
955 if (blobmsg_type(acl_func) != BLOBMSG_TYPE_STRING)
958 rpc_session_grant(ses, blobmsg_name(acl_scope),
959 blobmsg_name(acl_obj),
960 blobmsg_data(acl_func));
966 * Parse ACL scopes in array notation. The permission ("read" or "write")
967 * will be used as function name for each object.
975 else if (blobmsg_type(acl_scope) == BLOBMSG_TYPE_ARRAY) {
976 blobmsg_for_each_attr(acl_obj, acl_scope, rem) {
977 if (blobmsg_type(acl_obj) != BLOBMSG_TYPE_STRING)
980 rpc_session_grant(ses, blobmsg_name(acl_scope),
981 blobmsg_data(acl_obj),
982 blobmsg_name(acl_perm));
988 rpc_login_setup_acl_file(struct rpc_session *ses, struct uci_section *login,
991 struct blob_buf acl = { 0 };
992 struct blob_attr *acl_group, *acl_perm, *acl_scope;
995 blob_buf_init(&acl, 0);
997 if (!blobmsg_add_json_from_file(&acl, path)) {
998 fprintf(stderr, "Failed to parse %s\n", path);
1002 /* Iterate access groups in toplevel object */
1003 blob_for_each_attr(acl_group, acl.head, rem) {
1004 /* Iterate permission objects in each access group object */
1005 blobmsg_for_each_attr(acl_perm, acl_group, rem2) {
1006 if (blobmsg_type(acl_perm) != BLOBMSG_TYPE_TABLE)
1009 /* Only "read" and "write" permissions are defined */
1010 if (strcmp(blobmsg_name(acl_perm), "read") &&
1011 strcmp(blobmsg_name(acl_perm), "write"))
1015 * Check if the current user context specifies the current
1016 * "read" or "write" permission in the given access group.
1018 if (!rpc_login_test_permission(login, blobmsg_name(acl_perm),
1019 blobmsg_name(acl_group)))
1022 /* Iterate scope objects within the permission object */
1023 blobmsg_for_each_attr(acl_scope, acl_perm, rem3) {
1024 /* Setup the scopes of the access group */
1025 rpc_login_setup_acl_scope(ses, acl_perm, acl_scope);
1028 * Add the access group itself as object to the "access-group"
1029 * meta scope and the the permission level ("read" or "write")
1043 * This allows session clients to easily query the allowed
1044 * access groups without having to test access of each single
1045 * <scope>/<object>/<function> tuple defined in a group.
1047 rpc_session_grant(ses, "access-group",
1048 blobmsg_name(acl_group),
1049 blobmsg_name(acl_perm));
1055 blob_buf_free(&acl);
1059 rpc_login_setup_acls(struct rpc_session *ses, struct uci_section *login)
1064 if (glob(RPC_SESSION_ACL_DIR "/*.json", 0, NULL, &gl))
1067 for (i = 0; i < gl.gl_pathc; i++)
1068 rpc_login_setup_acl_file(ses, login, gl.gl_pathv[i]);
1074 rpc_handle_login(struct ubus_context *ctx, struct ubus_object *obj,
1075 struct ubus_request_data *req, const char *method,
1076 struct blob_attr *msg)
1078 struct uci_context *uci = NULL;
1079 struct uci_section *login;
1080 struct rpc_session *ses;
1081 struct blob_attr *tb[__RPC_L_MAX];
1082 int timeout = RPC_DEFAULT_SESSION_TIMEOUT;
1085 blobmsg_parse(login_policy, __RPC_L_MAX, tb, blob_data(msg), blob_len(msg));
1087 if (!tb[RPC_L_USERNAME] || !tb[RPC_L_PASSWORD]) {
1088 rv = UBUS_STATUS_INVALID_ARGUMENT;
1092 uci = uci_alloc_context();
1095 rv = UBUS_STATUS_UNKNOWN_ERROR;
1099 login = rpc_login_test_login(uci, blobmsg_get_string(tb[RPC_L_USERNAME]),
1100 blobmsg_get_string(tb[RPC_L_PASSWORD]));
1103 rv = UBUS_STATUS_PERMISSION_DENIED;
1107 if (tb[RPC_L_TIMEOUT])
1108 timeout = blobmsg_get_u32(tb[RPC_L_TIMEOUT]);
1110 ses = rpc_session_create(timeout);
1113 rv = UBUS_STATUS_UNKNOWN_ERROR;
1117 rpc_login_setup_acls(ses, login);
1119 rpc_session_set(ses, "user", tb[RPC_L_USERNAME]);
1120 rpc_session_dump(ses, ctx, req);
1124 uci_free_context(uci);
1131 rpc_validate_sid(const char *id)
1136 if (strlen(id) != RPC_SID_LEN)
1140 if (!isxdigit(*id++))
1147 rpc_blob_to_file(const char *path, struct blob_attr *attr)
1151 fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0600);
1156 len = write(fd, attr, blob_pad_len(attr));
1160 if (len != blob_pad_len(attr))
1169 static struct blob_attr *
1170 rpc_blob_from_file(const char *path)
1174 struct blob_attr head, *attr = NULL;
1176 if (stat(path, &s) || !S_ISREG(s.st_mode))
1179 fd = open(path, O_RDONLY);
1184 len = read(fd, &head, sizeof(head));
1186 if (len != sizeof(head) || blob_pad_len(&head) != s.st_size)
1189 attr = calloc(1, s.st_size);
1194 memcpy(attr, &head, sizeof(head));
1196 len += read(fd, (char *)attr + sizeof(head), s.st_size - sizeof(head));
1198 if (len != blob_pad_len(&head))
1216 rpc_session_from_blob(struct uci_context *uci, struct blob_attr *attr)
1219 const char *user = NULL;
1220 struct rpc_session *ses;
1221 struct uci_section *login;
1222 struct blob_attr *tb[__RPC_DUMP_MAX], *data;
1224 blobmsg_parse(dump_policy, __RPC_DUMP_MAX, tb,
1225 blob_data(attr), blob_len(attr));
1227 for (i = 0; i < __RPC_DUMP_MAX; i++)
1231 ses = rpc_session_new();
1236 memcpy(ses->id, blobmsg_data(tb[RPC_DUMP_SID]), RPC_SID_LEN);
1238 ses->timeout = blobmsg_get_u32(tb[RPC_DUMP_TIMEOUT]);
1240 blobmsg_for_each_attr(data, tb[RPC_DUMP_DATA], rem) {
1241 rpc_session_set(ses, blobmsg_name(data), data);
1243 if (!strcmp(blobmsg_name(data), "username"))
1244 user = blobmsg_get_string(data);
1248 login = rpc_login_test_login(uci, user, NULL);
1250 rpc_login_setup_acls(ses, login);
1253 avl_insert(&sessions, &ses->avl);
1255 uloop_timeout_set(&ses->t, blobmsg_get_u32(tb[RPC_DUMP_EXPIRES]) * 1000);
1260 int rpc_session_api_init(struct ubus_context *ctx)
1262 struct rpc_session *ses;
1264 static const struct ubus_method session_methods[] = {
1265 UBUS_METHOD("create", rpc_handle_create, &new_policy),
1266 UBUS_METHOD("list", rpc_handle_list, &sid_policy),
1267 UBUS_METHOD("grant", rpc_handle_acl, acl_policy),
1268 UBUS_METHOD("revoke", rpc_handle_acl, acl_policy),
1269 UBUS_METHOD("access", rpc_handle_access, perm_policy),
1270 UBUS_METHOD("set", rpc_handle_set, set_policy),
1271 UBUS_METHOD("get", rpc_handle_get, get_policy),
1272 UBUS_METHOD("unset", rpc_handle_unset, get_policy),
1273 UBUS_METHOD("destroy", rpc_handle_destroy, &sid_policy),
1274 UBUS_METHOD("login", rpc_handle_login, login_policy),
1277 static struct ubus_object_type session_type =
1278 UBUS_OBJECT_TYPE("luci-rpc-session", session_methods);
1280 static struct ubus_object obj = {
1282 .type = &session_type,
1283 .methods = session_methods,
1284 .n_methods = ARRAY_SIZE(session_methods),
1287 avl_init(&sessions, avl_strcmp, false, NULL);
1289 /* setup the default session */
1290 ses = rpc_session_new();
1293 strcpy(ses->id, RPC_DEFAULT_SESSION_ID);
1294 rpc_login_setup_acls(ses, NULL);
1295 avl_insert(&sessions, &ses->avl);
1298 return ubus_add_object(ctx, &obj);
1301 bool rpc_session_access(const char *sid, const char *scope,
1302 const char *object, const char *function)
1304 struct rpc_session *ses = rpc_session_get(sid);
1309 return rpc_session_acl_allowed(ses, scope, object, function);
1312 void rpc_session_create_cb(struct rpc_session_cb *cb)
1315 list_add(&cb->list, &create_callbacks);
1318 void rpc_session_destroy_cb(struct rpc_session_cb *cb)
1321 list_add(&cb->list, &destroy_callbacks);
1324 void rpc_session_freeze(void)
1327 struct rpc_session *ses;
1328 char path[PATH_MAX];
1330 if (stat(RPC_SESSION_DIRECTORY, &s))
1331 mkdir(RPC_SESSION_DIRECTORY, 0700);
1333 avl_for_each_element(&sessions, ses, avl) {
1334 /* skip default session */
1335 if (!strcmp(ses->id, RPC_DEFAULT_SESSION_ID))
1338 snprintf(path, sizeof(path) - 1, RPC_SESSION_DIRECTORY "/%s", ses->id);
1339 rpc_session_to_blob(ses, false);
1340 rpc_blob_to_file(path, buf.head);
1344 void rpc_session_thaw(void)
1347 char path[PATH_MAX];
1349 struct blob_attr *attr;
1350 struct uci_context *uci;
1352 d = opendir(RPC_SESSION_DIRECTORY);
1357 uci = uci_alloc_context();
1362 while ((e = readdir(d)) != NULL) {
1363 if (!rpc_validate_sid(e->d_name))
1366 snprintf(path, sizeof(path) - 1,
1367 RPC_SESSION_DIRECTORY "/%s", e->d_name);
1369 attr = rpc_blob_from_file(path);
1372 rpc_session_from_blob(uci, attr);
1381 uci_free_context(uci);