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);
47 static const struct blobmsg_policy new_policy[__RPC_SN_MAX] = {
48 [RPC_SN_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
55 static const struct blobmsg_policy sid_policy[__RPC_SI_MAX] = {
56 [RPC_SI_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
64 static const struct blobmsg_policy set_policy[__RPC_SS_MAX] = {
65 [RPC_SS_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
66 [RPC_SS_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE },
74 static const struct blobmsg_policy get_policy[__RPC_SG_MAX] = {
75 [RPC_SG_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
76 [RPC_SG_KEYS] = { .name = "keys", .type = BLOBMSG_TYPE_ARRAY },
85 static const struct blobmsg_policy acl_policy[__RPC_SA_MAX] = {
86 [RPC_SA_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
87 [RPC_SA_SCOPE] = { .name = "scope", .type = BLOBMSG_TYPE_STRING },
88 [RPC_SA_OBJECTS] = { .name = "objects", .type = BLOBMSG_TYPE_ARRAY },
98 static const struct blobmsg_policy perm_policy[__RPC_SP_MAX] = {
99 [RPC_SP_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
100 [RPC_SP_SCOPE] = { .name = "scope", .type = BLOBMSG_TYPE_STRING },
101 [RPC_SP_OBJECT] = { .name = "object", .type = BLOBMSG_TYPE_STRING },
102 [RPC_SP_FUNCTION] = { .name = "function", .type = BLOBMSG_TYPE_STRING },
112 static const struct blobmsg_policy dump_policy[__RPC_DUMP_MAX] = {
113 [RPC_DUMP_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
114 [RPC_DUMP_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
115 [RPC_DUMP_EXPIRES] = { .name = "expires", .type = BLOBMSG_TYPE_INT32 },
116 [RPC_DUMP_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
125 static const struct blobmsg_policy login_policy[__RPC_L_MAX] = {
126 [RPC_L_USERNAME] = { .name = "username", .type = BLOBMSG_TYPE_STRING },
127 [RPC_L_PASSWORD] = { .name = "password", .type = BLOBMSG_TYPE_STRING },
128 [RPC_L_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
132 * Keys in the AVL tree contain all pattern characters up to the first wildcard.
133 * To look up entries, start with the last entry that has a key less than or
134 * equal to the method name, then work backwards as long as the AVL key still
135 * matches its counterpart in the object name
137 #define uh_foreach_matching_acl_prefix(_acl, _avl, _obj, _func) \
138 for (_acl = avl_find_le_element(_avl, _obj, _acl, avl); \
140 _acl = avl_is_first(_avl, &(_acl)->avl) ? NULL : \
141 avl_prev_element((_acl), avl))
143 #define uh_foreach_matching_acl(_acl, _avl, _obj, _func) \
144 uh_foreach_matching_acl_prefix(_acl, _avl, _obj, _func) \
145 if (!strncmp((_acl)->object, _obj, (_acl)->sort_len) && \
146 !fnmatch((_acl)->object, (_obj), FNM_NOESCAPE) && \
147 !fnmatch((_acl)->function, (_func), FNM_NOESCAPE))
150 rpc_random(char *dest)
152 unsigned char buf[16] = { 0 };
157 f = fopen("/dev/urandom", "r");
161 ret = fread(buf, 1, sizeof(buf), f);
167 for (i = 0; i < sizeof(buf); i++)
168 sprintf(dest + (i<<1), "%02x", buf[i]);
174 rpc_session_dump_data(struct rpc_session *ses, struct blob_buf *b)
176 struct rpc_session_data *d;
178 avl_for_each_element(&ses->data, d, avl) {
179 blobmsg_add_field(b, blobmsg_type(d->attr), blobmsg_name(d->attr),
180 blobmsg_data(d->attr), blobmsg_data_len(d->attr));
185 rpc_session_dump_acls(struct rpc_session *ses, struct blob_buf *b)
187 struct rpc_session_acl *acl;
188 struct rpc_session_acl_scope *acl_scope;
189 const char *lastobj = NULL;
190 const char *lastscope = NULL;
191 void *c = NULL, *d = NULL;
193 avl_for_each_element(&ses->acls, acl_scope, avl) {
194 if (!lastscope || strcmp(acl_scope->avl.key, lastscope))
196 if (c) blobmsg_close_table(b, c);
197 c = blobmsg_open_table(b, acl_scope->avl.key);
203 avl_for_each_element(&acl_scope->acls, acl, avl) {
204 if (!lastobj || strcmp(acl->object, lastobj))
206 if (d) blobmsg_close_array(b, d);
207 d = blobmsg_open_array(b, acl->object);
210 blobmsg_add_string(b, NULL, acl->function);
211 lastobj = acl->object;
214 if (d) blobmsg_close_array(b, d);
217 if (c) blobmsg_close_table(b, c);
221 rpc_session_to_blob(struct rpc_session *ses, bool acls)
225 blob_buf_init(&buf, 0);
227 blobmsg_add_string(&buf, "ubus_rpc_session", ses->id);
228 blobmsg_add_u32(&buf, "timeout", ses->timeout);
229 blobmsg_add_u32(&buf, "expires", uloop_timeout_remaining(&ses->t) / 1000);
232 c = blobmsg_open_table(&buf, "acls");
233 rpc_session_dump_acls(ses, &buf);
234 blobmsg_close_table(&buf, c);
237 c = blobmsg_open_table(&buf, "data");
238 rpc_session_dump_data(ses, &buf);
239 blobmsg_close_table(&buf, c);
243 rpc_session_dump(struct rpc_session *ses, struct ubus_context *ctx,
244 struct ubus_request_data *req)
246 rpc_session_to_blob(ses, true);
248 ubus_send_reply(ctx, req, buf.head);
252 rpc_touch_session(struct rpc_session *ses)
254 if (ses->timeout > 0)
255 uloop_timeout_set(&ses->t, ses->timeout * 1000);
259 rpc_session_destroy(struct rpc_session *ses)
261 struct rpc_session_acl *acl, *nacl;
262 struct rpc_session_acl_scope *acl_scope, *nacl_scope;
263 struct rpc_session_data *data, *ndata;
264 struct rpc_session_cb *cb;
266 list_for_each_entry(cb, &destroy_callbacks, list)
267 cb->cb(ses, cb->priv);
269 uloop_timeout_cancel(&ses->t);
271 avl_for_each_element_safe(&ses->acls, acl_scope, avl, nacl_scope) {
272 avl_remove_all_elements(&acl_scope->acls, acl, avl, nacl)
275 avl_delete(&ses->acls, &acl_scope->avl);
279 avl_remove_all_elements(&ses->data, data, avl, ndata)
282 avl_delete(&sessions, &ses->avl);
286 static void rpc_session_timeout(struct uloop_timeout *t)
288 struct rpc_session *ses;
290 ses = container_of(t, struct rpc_session, t);
291 rpc_session_destroy(ses);
294 static struct rpc_session *
295 rpc_session_new(void)
297 struct rpc_session *ses;
299 ses = calloc(1, sizeof(*ses));
304 ses->avl.key = ses->id;
306 avl_init(&ses->acls, avl_strcmp, true, NULL);
307 avl_init(&ses->data, avl_strcmp, false, NULL);
309 ses->t.cb = rpc_session_timeout;
314 static struct rpc_session *
315 rpc_session_create(int timeout)
317 struct rpc_session *ses;
318 struct rpc_session_cb *cb;
320 ses = rpc_session_new();
325 if (rpc_random(ses->id))
328 ses->timeout = timeout;
330 avl_insert(&sessions, &ses->avl);
332 rpc_touch_session(ses);
334 list_for_each_entry(cb, &create_callbacks, list)
335 cb->cb(ses, cb->priv);
340 static struct rpc_session *
341 rpc_session_get(const char *id)
343 struct rpc_session *ses;
345 ses = avl_find_element(&sessions, id, ses, avl);
349 rpc_touch_session(ses);
354 rpc_handle_create(struct ubus_context *ctx, struct ubus_object *obj,
355 struct ubus_request_data *req, const char *method,
356 struct blob_attr *msg)
358 struct rpc_session *ses;
359 struct blob_attr *tb;
360 int timeout = RPC_DEFAULT_SESSION_TIMEOUT;
362 blobmsg_parse(new_policy, __RPC_SN_MAX, &tb, blob_data(msg), blob_len(msg));
364 timeout = blobmsg_get_u32(tb);
366 ses = rpc_session_create(timeout);
368 rpc_session_dump(ses, ctx, req);
374 rpc_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
375 struct ubus_request_data *req, const char *method,
376 struct blob_attr *msg)
378 struct rpc_session *ses;
379 struct blob_attr *tb;
381 blobmsg_parse(sid_policy, __RPC_SI_MAX, &tb, blob_data(msg), blob_len(msg));
384 avl_for_each_element(&sessions, ses, avl)
385 rpc_session_dump(ses, ctx, req);
389 ses = rpc_session_get(blobmsg_data(tb));
391 return UBUS_STATUS_NOT_FOUND;
393 rpc_session_dump(ses, ctx, req);
399 uh_id_len(const char *str)
401 return strcspn(str, "*?[");
405 rpc_session_grant(struct rpc_session *ses,
406 const char *scope, const char *object, const char *function)
408 struct rpc_session_acl *acl;
409 struct rpc_session_acl_scope *acl_scope;
410 char *new_scope, *new_obj, *new_func, *new_id;
413 if (!object || !function)
414 return UBUS_STATUS_INVALID_ARGUMENT;
416 acl_scope = avl_find_element(&ses->acls, scope, acl_scope, avl);
419 uh_foreach_matching_acl_prefix(acl, &acl_scope->acls, object, function) {
420 if (!strcmp(acl->object, object) &&
421 !strcmp(acl->function, function))
427 acl_scope = calloc_a(sizeof(*acl_scope),
428 &new_scope, strlen(scope) + 1);
431 return UBUS_STATUS_UNKNOWN_ERROR;
433 acl_scope->avl.key = strcpy(new_scope, scope);
434 avl_init(&acl_scope->acls, avl_strcmp, true, NULL);
435 avl_insert(&ses->acls, &acl_scope->avl);
438 id_len = uh_id_len(object);
439 acl = calloc_a(sizeof(*acl),
440 &new_obj, strlen(object) + 1,
441 &new_func, strlen(function) + 1,
442 &new_id, id_len + 1);
445 return UBUS_STATUS_UNKNOWN_ERROR;
447 acl->object = strcpy(new_obj, object);
448 acl->function = strcpy(new_func, function);
449 acl->avl.key = strncpy(new_id, object, id_len);
450 avl_insert(&acl_scope->acls, &acl->avl);
456 rpc_session_revoke(struct rpc_session *ses,
457 const char *scope, const char *object, const char *function)
459 struct rpc_session_acl *acl, *next;
460 struct rpc_session_acl_scope *acl_scope;
464 acl_scope = avl_find_element(&ses->acls, scope, acl_scope, avl);
469 if (!object && !function) {
470 avl_remove_all_elements(&acl_scope->acls, acl, avl, next)
472 avl_delete(&ses->acls, &acl_scope->avl);
477 id_len = uh_id_len(object);
478 id = alloca(id_len + 1);
479 strncpy(id, object, id_len);
482 acl = avl_find_element(&acl_scope->acls, id, acl, avl);
484 if (!avl_is_last(&acl_scope->acls, &acl->avl))
485 next = avl_next_element(acl, avl);
489 if (strcmp(id, acl->avl.key) != 0)
492 if (!strcmp(acl->object, object) &&
493 !strcmp(acl->function, function)) {
494 avl_delete(&acl_scope->acls, &acl->avl);
500 if (avl_is_empty(&acl_scope->acls)) {
501 avl_delete(&ses->acls, &acl_scope->avl);
510 rpc_handle_acl(struct ubus_context *ctx, struct ubus_object *obj,
511 struct ubus_request_data *req, const char *method,
512 struct blob_attr *msg)
514 struct rpc_session *ses;
515 struct blob_attr *tb[__RPC_SA_MAX];
516 struct blob_attr *attr, *sattr;
517 const char *object, *function;
518 const char *scope = "ubus";
521 int (*cb)(struct rpc_session *ses,
522 const char *scope, const char *object, const char *function);
524 blobmsg_parse(acl_policy, __RPC_SA_MAX, tb, blob_data(msg), blob_len(msg));
527 return UBUS_STATUS_INVALID_ARGUMENT;
529 ses = rpc_session_get(blobmsg_data(tb[RPC_SA_SID]));
531 return UBUS_STATUS_NOT_FOUND;
533 if (tb[RPC_SA_SCOPE])
534 scope = blobmsg_data(tb[RPC_SA_SCOPE]);
536 if (!strcmp(method, "grant"))
537 cb = rpc_session_grant;
539 cb = rpc_session_revoke;
541 if (!tb[RPC_SA_OBJECTS])
542 return cb(ses, scope, NULL, NULL);
544 blobmsg_for_each_attr(attr, tb[RPC_SA_OBJECTS], rem1) {
545 if (blobmsg_type(attr) != BLOBMSG_TYPE_ARRAY)
551 blobmsg_for_each_attr(sattr, attr, rem2) {
552 if (blobmsg_type(sattr) != BLOBMSG_TYPE_STRING)
556 object = blobmsg_data(sattr);
558 function = blobmsg_data(sattr);
563 if (object && function)
564 cb(ses, scope, object, function);
571 rpc_session_acl_allowed(struct rpc_session *ses, const char *scope,
572 const char *obj, const char *fun)
574 struct rpc_session_acl *acl;
575 struct rpc_session_acl_scope *acl_scope;
577 acl_scope = avl_find_element(&ses->acls, scope, acl_scope, avl);
580 uh_foreach_matching_acl(acl, &acl_scope->acls, obj, fun)
588 rpc_handle_access(struct ubus_context *ctx, struct ubus_object *obj,
589 struct ubus_request_data *req, const char *method,
590 struct blob_attr *msg)
592 struct rpc_session *ses;
593 struct blob_attr *tb[__RPC_SP_MAX];
594 const char *scope = "ubus";
597 blobmsg_parse(perm_policy, __RPC_SP_MAX, tb, blob_data(msg), blob_len(msg));
600 return UBUS_STATUS_INVALID_ARGUMENT;
602 ses = rpc_session_get(blobmsg_data(tb[RPC_SP_SID]));
604 return UBUS_STATUS_NOT_FOUND;
606 blob_buf_init(&buf, 0);
608 if (tb[RPC_SP_OBJECT] && tb[RPC_SP_FUNCTION])
610 if (tb[RPC_SP_SCOPE])
611 scope = blobmsg_data(tb[RPC_SP_SCOPE]);
613 allow = rpc_session_acl_allowed(ses, scope,
614 blobmsg_data(tb[RPC_SP_OBJECT]),
615 blobmsg_data(tb[RPC_SP_FUNCTION]));
617 blobmsg_add_u8(&buf, "access", allow);
621 rpc_session_dump_acls(ses, &buf);
624 ubus_send_reply(ctx, req, buf.head);
630 rpc_session_set(struct rpc_session *ses, struct blob_attr *val)
632 struct rpc_session_data *data;
634 data = avl_find_element(&ses->data, blobmsg_name(val), data, avl);
636 avl_delete(&ses->data, &data->avl);
640 data = calloc(1, sizeof(*data) + blob_pad_len(val));
644 memcpy(data->attr, val, blob_pad_len(val));
645 data->avl.key = blobmsg_name(data->attr);
646 avl_insert(&ses->data, &data->avl);
650 rpc_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
651 struct ubus_request_data *req, const char *method,
652 struct blob_attr *msg)
654 struct rpc_session *ses;
655 struct blob_attr *tb[__RPC_SS_MAX];
656 struct blob_attr *attr;
659 blobmsg_parse(set_policy, __RPC_SS_MAX, tb, blob_data(msg), blob_len(msg));
661 if (!tb[RPC_SS_SID] || !tb[RPC_SS_VALUES])
662 return UBUS_STATUS_INVALID_ARGUMENT;
664 ses = rpc_session_get(blobmsg_data(tb[RPC_SS_SID]));
666 return UBUS_STATUS_NOT_FOUND;
668 blobmsg_for_each_attr(attr, tb[RPC_SS_VALUES], rem) {
669 if (!blobmsg_name(attr)[0])
672 rpc_session_set(ses, attr);
679 rpc_handle_get(struct ubus_context *ctx, struct ubus_object *obj,
680 struct ubus_request_data *req, const char *method,
681 struct blob_attr *msg)
683 struct rpc_session *ses;
684 struct rpc_session_data *data;
685 struct blob_attr *tb[__RPC_SG_MAX];
686 struct blob_attr *attr;
690 blobmsg_parse(get_policy, __RPC_SG_MAX, tb, blob_data(msg), blob_len(msg));
693 return UBUS_STATUS_INVALID_ARGUMENT;
695 ses = rpc_session_get(blobmsg_data(tb[RPC_SG_SID]));
697 return UBUS_STATUS_NOT_FOUND;
699 blob_buf_init(&buf, 0);
700 c = blobmsg_open_table(&buf, "values");
703 blobmsg_for_each_attr(attr, tb[RPC_SG_KEYS], rem) {
704 if (blobmsg_type(attr) != BLOBMSG_TYPE_STRING)
707 data = avl_find_element(&ses->data, blobmsg_data(attr), data, avl);
711 blobmsg_add_field(&buf, blobmsg_type(data->attr),
712 blobmsg_name(data->attr),
713 blobmsg_data(data->attr),
714 blobmsg_data_len(data->attr));
717 rpc_session_dump_data(ses, &buf);
719 blobmsg_close_table(&buf, c);
720 ubus_send_reply(ctx, req, buf.head);
726 rpc_handle_unset(struct ubus_context *ctx, struct ubus_object *obj,
727 struct ubus_request_data *req, const char *method,
728 struct blob_attr *msg)
730 struct rpc_session *ses;
731 struct rpc_session_data *data, *ndata;
732 struct blob_attr *tb[__RPC_SA_MAX];
733 struct blob_attr *attr;
736 blobmsg_parse(get_policy, __RPC_SG_MAX, tb, blob_data(msg), blob_len(msg));
739 return UBUS_STATUS_INVALID_ARGUMENT;
741 ses = rpc_session_get(blobmsg_data(tb[RPC_SG_SID]));
743 return UBUS_STATUS_NOT_FOUND;
745 if (!tb[RPC_SG_KEYS]) {
746 avl_remove_all_elements(&ses->data, data, avl, ndata)
751 blobmsg_for_each_attr(attr, tb[RPC_SG_KEYS], rem) {
752 if (blobmsg_type(attr) != BLOBMSG_TYPE_STRING)
755 data = avl_find_element(&ses->data, blobmsg_data(attr), data, avl);
759 avl_delete(&ses->data, &data->avl);
767 rpc_handle_destroy(struct ubus_context *ctx, struct ubus_object *obj,
768 struct ubus_request_data *req, const char *method,
769 struct blob_attr *msg)
771 struct rpc_session *ses;
772 struct blob_attr *tb;
774 blobmsg_parse(sid_policy, __RPC_SI_MAX, &tb, blob_data(msg), blob_len(msg));
777 return UBUS_STATUS_INVALID_ARGUMENT;
779 if (!strcmp(blobmsg_get_string(tb), RPC_DEFAULT_SESSION_ID))
780 return UBUS_STATUS_PERMISSION_DENIED;
782 ses = rpc_session_get(blobmsg_data(tb));
784 return UBUS_STATUS_NOT_FOUND;
786 rpc_session_destroy(ses);
793 rpc_login_test_password(const char *hash, const char *password)
797 /* password is not set */
798 if (!hash || !*hash || !strcmp(hash, "!") || !strcmp(hash, "x"))
803 /* password hash refers to shadow/passwd */
804 else if (!strncmp(hash, "$p$", 3))
807 struct spwd *sp = getspnam(hash + 3);
812 return rpc_login_test_password(sp->sp_pwdp, password);
814 struct passwd *pw = getpwnam(hash + 3);
819 return rpc_login_test_password(pw->pw_passwd, password);
823 crypt_hash = crypt(password, hash);
825 return !strcmp(crypt_hash, hash);
828 static struct uci_section *
829 rpc_login_test_login(struct uci_context *uci,
830 const char *username, const char *password)
832 struct uci_package *p = NULL;
833 struct uci_section *s;
834 struct uci_element *e;
835 struct uci_ptr ptr = { .package = "rpcd" };
837 uci_load(uci, ptr.package, &p);
842 uci_foreach_element(&p->sections, e)
844 s = uci_to_section(e);
846 if (strcmp(s->type, "login"))
849 ptr.section = s->e.name;
852 /* test for matching username */
853 ptr.option = "username";
856 if (uci_lookup_ptr(uci, &ptr, NULL, true))
859 if (ptr.o->type != UCI_TYPE_STRING)
862 if (strcmp(ptr.o->v.string, username))
865 /* If password is NULL, we're restoring ACLs for an existing session,
866 * in this case do not check the password again. */
870 /* test for matching password */
871 ptr.option = "password";
874 if (uci_lookup_ptr(uci, &ptr, NULL, true))
877 if (ptr.o->type != UCI_TYPE_STRING)
880 if (rpc_login_test_password(ptr.o->v.string, password))
888 rpc_login_test_permission(struct uci_section *s,
889 const char *perm, const char *group)
892 struct uci_option *o;
893 struct uci_element *e, *l;
895 /* If the login section is not provided, we're setting up acls for the
896 * default session, in this case uncondionally allow access to the
897 * "unauthenticated" access group */
899 return !strcmp(group, "unauthenticated");
902 uci_foreach_element(&s->options, e)
904 o = uci_to_option(e);
906 if (o->type != UCI_TYPE_LIST)
909 if (strcmp(o->e.name, perm))
912 /* Match negative expressions first. If a negative expression matches
913 * the current group name then deny access. */
914 uci_foreach_element(&o->v.list, l) {
920 while (isspace(*++p));
925 if (!fnmatch(p, group, 0))
929 uci_foreach_element(&o->v.list, l) {
930 if (!l->name || !*l->name || *l->name == '!')
933 if (!fnmatch(l->name, group, 0))
938 /* make sure that write permission implies read permission */
939 if (!strcmp(perm, "read"))
940 return rpc_login_test_permission(s, "write", group);
946 rpc_login_setup_acl_scope(struct rpc_session *ses,
947 struct blob_attr *acl_perm,
948 struct blob_attr *acl_scope)
950 struct blob_attr *acl_obj, *acl_func;
954 * Parse ACL scopes in table notation.
964 if (blobmsg_type(acl_scope) == BLOBMSG_TYPE_TABLE) {
965 blobmsg_for_each_attr(acl_obj, acl_scope, rem) {
966 if (blobmsg_type(acl_obj) != BLOBMSG_TYPE_ARRAY)
969 blobmsg_for_each_attr(acl_func, acl_obj, rem2) {
970 if (blobmsg_type(acl_func) != BLOBMSG_TYPE_STRING)
973 rpc_session_grant(ses, blobmsg_name(acl_scope),
974 blobmsg_name(acl_obj),
975 blobmsg_data(acl_func));
981 * Parse ACL scopes in array notation. The permission ("read" or "write")
982 * will be used as function name for each object.
990 else if (blobmsg_type(acl_scope) == BLOBMSG_TYPE_ARRAY) {
991 blobmsg_for_each_attr(acl_obj, acl_scope, rem) {
992 if (blobmsg_type(acl_obj) != BLOBMSG_TYPE_STRING)
995 rpc_session_grant(ses, blobmsg_name(acl_scope),
996 blobmsg_data(acl_obj),
997 blobmsg_name(acl_perm));
1003 rpc_login_setup_acl_file(struct rpc_session *ses, struct uci_section *login,
1006 struct blob_buf acl = { 0 };
1007 struct blob_attr *acl_group, *acl_perm, *acl_scope;
1008 int rem, rem2, rem3;
1010 blob_buf_init(&acl, 0);
1012 if (!blobmsg_add_json_from_file(&acl, path)) {
1013 fprintf(stderr, "Failed to parse %s\n", path);
1017 /* Iterate access groups in toplevel object */
1018 blob_for_each_attr(acl_group, acl.head, rem) {
1019 /* Iterate permission objects in each access group object */
1020 blobmsg_for_each_attr(acl_perm, acl_group, rem2) {
1021 if (blobmsg_type(acl_perm) != BLOBMSG_TYPE_TABLE)
1024 /* Only "read" and "write" permissions are defined */
1025 if (strcmp(blobmsg_name(acl_perm), "read") &&
1026 strcmp(blobmsg_name(acl_perm), "write"))
1030 * Check if the current user context specifies the current
1031 * "read" or "write" permission in the given access group.
1033 if (!rpc_login_test_permission(login, blobmsg_name(acl_perm),
1034 blobmsg_name(acl_group)))
1037 /* Iterate scope objects within the permission object */
1038 blobmsg_for_each_attr(acl_scope, acl_perm, rem3) {
1039 /* Setup the scopes of the access group */
1040 rpc_login_setup_acl_scope(ses, acl_perm, acl_scope);
1043 * Add the access group itself as object to the "access-group"
1044 * meta scope and the the permission level ("read" or "write")
1058 * This allows session clients to easily query the allowed
1059 * access groups without having to test access of each single
1060 * <scope>/<object>/<function> tuple defined in a group.
1062 rpc_session_grant(ses, "access-group",
1063 blobmsg_name(acl_group),
1064 blobmsg_name(acl_perm));
1070 blob_buf_free(&acl);
1074 rpc_login_setup_acls(struct rpc_session *ses, struct uci_section *login)
1079 if (glob(RPC_SESSION_ACL_DIR "/*.json", 0, NULL, &gl))
1082 for (i = 0; i < gl.gl_pathc; i++)
1083 rpc_login_setup_acl_file(ses, login, gl.gl_pathv[i]);
1088 static struct rpc_session *
1089 rpc_reclaim_apply_session(const char *expected_username)
1091 struct rpc_session_data *username;
1092 struct rpc_session *ses;
1097 ses = rpc_session_get(apply_sid);
1102 username = avl_find_element(&ses->data, "username", username, avl);
1104 if (!username || blobmsg_type(username->attr) != BLOBMSG_TYPE_STRING)
1107 if (strcmp(blobmsg_get_string(username->attr), expected_username))
1114 rpc_handle_login(struct ubus_context *ctx, struct ubus_object *obj,
1115 struct ubus_request_data *req, const char *method,
1116 struct blob_attr *msg)
1118 struct uci_context *uci = NULL;
1119 struct uci_section *login;
1120 struct rpc_session *ses;
1121 struct blob_attr *tb[__RPC_L_MAX];
1122 int timeout = RPC_DEFAULT_SESSION_TIMEOUT;
1125 blobmsg_parse(login_policy, __RPC_L_MAX, tb, blob_data(msg), blob_len(msg));
1127 if (!tb[RPC_L_USERNAME] || !tb[RPC_L_PASSWORD]) {
1128 rv = UBUS_STATUS_INVALID_ARGUMENT;
1132 uci = uci_alloc_context();
1135 rv = UBUS_STATUS_UNKNOWN_ERROR;
1139 login = rpc_login_test_login(uci, blobmsg_get_string(tb[RPC_L_USERNAME]),
1140 blobmsg_get_string(tb[RPC_L_PASSWORD]));
1143 rv = UBUS_STATUS_PERMISSION_DENIED;
1147 if (tb[RPC_L_TIMEOUT])
1148 timeout = blobmsg_get_u32(tb[RPC_L_TIMEOUT]);
1151 * attempt to reclaim a pending apply session, but only accept it
1152 * if the username matches, otherwise perform a new login
1155 ses = rpc_reclaim_apply_session(blobmsg_get_string(tb[RPC_L_USERNAME]));
1158 ses = rpc_session_create(timeout);
1161 rv = UBUS_STATUS_UNKNOWN_ERROR;
1165 rpc_login_setup_acls(ses, login);
1167 rpc_session_set(ses, tb[RPC_L_USERNAME]);
1168 rpc_session_dump(ses, ctx, req);
1172 uci_free_context(uci);
1179 rpc_validate_sid(const char *id)
1184 if (strlen(id) != RPC_SID_LEN)
1188 if (!isxdigit(*id++))
1195 rpc_blob_to_file(const char *path, struct blob_attr *attr)
1199 fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0600);
1204 len = write(fd, attr, blob_pad_len(attr));
1208 if (len != blob_pad_len(attr))
1217 static struct blob_attr *
1218 rpc_blob_from_file(const char *path)
1222 struct blob_attr head, *attr = NULL;
1224 if (stat(path, &s) || !S_ISREG(s.st_mode))
1227 fd = open(path, O_RDONLY);
1232 len = read(fd, &head, sizeof(head));
1234 if (len != sizeof(head) || blob_pad_len(&head) != s.st_size)
1237 attr = calloc(1, s.st_size);
1242 memcpy(attr, &head, sizeof(head));
1244 len += read(fd, (char *)attr + sizeof(head), s.st_size - sizeof(head));
1246 if (len != blob_pad_len(&head))
1264 rpc_session_from_blob(struct uci_context *uci, struct blob_attr *attr)
1267 const char *user = NULL;
1268 struct rpc_session *ses;
1269 struct uci_section *login;
1270 struct blob_attr *tb[__RPC_DUMP_MAX], *data;
1272 blobmsg_parse(dump_policy, __RPC_DUMP_MAX, tb,
1273 blob_data(attr), blob_len(attr));
1275 for (i = 0; i < __RPC_DUMP_MAX; i++)
1279 ses = rpc_session_new();
1284 memcpy(ses->id, blobmsg_data(tb[RPC_DUMP_SID]), RPC_SID_LEN);
1286 ses->timeout = blobmsg_get_u32(tb[RPC_DUMP_TIMEOUT]);
1288 blobmsg_for_each_attr(data, tb[RPC_DUMP_DATA], rem) {
1289 rpc_session_set(ses, data);
1291 if (blobmsg_type(data) != BLOBMSG_TYPE_STRING)
1294 if (!strcmp(blobmsg_name(data), "username"))
1295 user = blobmsg_get_string(data);
1299 login = rpc_login_test_login(uci, user, NULL);
1301 rpc_login_setup_acls(ses, login);
1304 avl_insert(&sessions, &ses->avl);
1306 uloop_timeout_set(&ses->t, blobmsg_get_u32(tb[RPC_DUMP_EXPIRES]) * 1000);
1311 int rpc_session_api_init(struct ubus_context *ctx)
1313 struct rpc_session *ses;
1315 static const struct ubus_method session_methods[] = {
1316 UBUS_METHOD("create", rpc_handle_create, new_policy),
1317 UBUS_METHOD("list", rpc_handle_list, sid_policy),
1318 UBUS_METHOD("grant", rpc_handle_acl, acl_policy),
1319 UBUS_METHOD("revoke", rpc_handle_acl, acl_policy),
1320 UBUS_METHOD("access", rpc_handle_access, perm_policy),
1321 UBUS_METHOD("set", rpc_handle_set, set_policy),
1322 UBUS_METHOD("get", rpc_handle_get, get_policy),
1323 UBUS_METHOD("unset", rpc_handle_unset, get_policy),
1324 UBUS_METHOD("destroy", rpc_handle_destroy, sid_policy),
1325 UBUS_METHOD("login", rpc_handle_login, login_policy),
1328 static struct ubus_object_type session_type =
1329 UBUS_OBJECT_TYPE("luci-rpc-session", session_methods);
1331 static struct ubus_object obj = {
1333 .type = &session_type,
1334 .methods = session_methods,
1335 .n_methods = ARRAY_SIZE(session_methods),
1338 avl_init(&sessions, avl_strcmp, false, NULL);
1340 /* setup the default session */
1341 ses = rpc_session_new();
1344 strcpy(ses->id, RPC_DEFAULT_SESSION_ID);
1345 rpc_login_setup_acls(ses, NULL);
1346 avl_insert(&sessions, &ses->avl);
1349 return ubus_add_object(ctx, &obj);
1352 bool rpc_session_access(const char *sid, const char *scope,
1353 const char *object, const char *function)
1355 struct rpc_session *ses = rpc_session_get(sid);
1360 return rpc_session_acl_allowed(ses, scope, object, function);
1363 void rpc_session_create_cb(struct rpc_session_cb *cb)
1366 list_add(&cb->list, &create_callbacks);
1369 void rpc_session_destroy_cb(struct rpc_session_cb *cb)
1372 list_add(&cb->list, &destroy_callbacks);
1375 void rpc_session_freeze(void)
1378 struct rpc_session *ses;
1379 char path[PATH_MAX];
1381 if (stat(RPC_SESSION_DIRECTORY, &s))
1382 mkdir(RPC_SESSION_DIRECTORY, 0700);
1384 avl_for_each_element(&sessions, ses, avl) {
1385 /* skip default session */
1386 if (!strcmp(ses->id, RPC_DEFAULT_SESSION_ID))
1389 snprintf(path, sizeof(path) - 1, RPC_SESSION_DIRECTORY "/%s", ses->id);
1390 rpc_session_to_blob(ses, false);
1391 rpc_blob_to_file(path, buf.head);
1395 void rpc_session_thaw(void)
1398 char path[PATH_MAX];
1400 struct blob_attr *attr;
1401 struct uci_context *uci;
1403 d = opendir(RPC_SESSION_DIRECTORY);
1408 uci = uci_alloc_context();
1413 while ((e = readdir(d)) != NULL) {
1414 if (!rpc_validate_sid(e->d_name))
1417 snprintf(path, sizeof(path) - 1,
1418 RPC_SESSION_DIRECTORY "/%s", e->d_name);
1420 attr = rpc_blob_from_file(path);
1423 rpc_session_from_blob(uci, attr);
1432 uci_free_context(uci);