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 };
156 f
= fopen("/dev/urandom", "r");
160 fread(buf
, 1, sizeof(buf
), f
);
163 for (i
= 0; i
< sizeof(buf
); i
++)
164 sprintf(dest
+ (i
<<1), "%02x", buf
[i
]);
168 rpc_session_dump_data(struct rpc_session
*ses
, struct blob_buf
*b
)
170 struct rpc_session_data
*d
;
172 avl_for_each_element(&ses
->data
, d
, avl
) {
173 blobmsg_add_field(b
, blobmsg_type(d
->attr
), blobmsg_name(d
->attr
),
174 blobmsg_data(d
->attr
), blobmsg_data_len(d
->attr
));
179 rpc_session_dump_acls(struct rpc_session
*ses
, struct blob_buf
*b
)
181 struct rpc_session_acl
*acl
;
182 struct rpc_session_acl_scope
*acl_scope
;
183 const char *lastobj
= NULL
;
184 const char *lastscope
= NULL
;
185 void *c
= NULL
, *d
= NULL
;
187 avl_for_each_element(&ses
->acls
, acl_scope
, avl
) {
188 if (!lastscope
|| strcmp(acl_scope
->avl
.key
, lastscope
))
190 if (c
) blobmsg_close_table(b
, c
);
191 c
= blobmsg_open_table(b
, acl_scope
->avl
.key
);
197 avl_for_each_element(&acl_scope
->acls
, acl
, avl
) {
198 if (!lastobj
|| strcmp(acl
->object
, lastobj
))
200 if (d
) blobmsg_close_array(b
, d
);
201 d
= blobmsg_open_array(b
, acl
->object
);
204 blobmsg_add_string(b
, NULL
, acl
->function
);
205 lastobj
= acl
->object
;
208 if (d
) blobmsg_close_array(b
, d
);
211 if (c
) blobmsg_close_table(b
, c
);
215 rpc_session_to_blob(struct rpc_session
*ses
, bool acls
)
219 blob_buf_init(&buf
, 0);
221 blobmsg_add_string(&buf
, "ubus_rpc_session", ses
->id
);
222 blobmsg_add_u32(&buf
, "timeout", ses
->timeout
);
223 blobmsg_add_u32(&buf
, "expires", uloop_timeout_remaining(&ses
->t
) / 1000);
226 c
= blobmsg_open_table(&buf
, "acls");
227 rpc_session_dump_acls(ses
, &buf
);
228 blobmsg_close_table(&buf
, c
);
231 c
= blobmsg_open_table(&buf
, "data");
232 rpc_session_dump_data(ses
, &buf
);
233 blobmsg_close_table(&buf
, c
);
237 rpc_session_dump(struct rpc_session
*ses
, struct ubus_context
*ctx
,
238 struct ubus_request_data
*req
)
240 rpc_session_to_blob(ses
, true);
242 ubus_send_reply(ctx
, req
, buf
.head
);
246 rpc_touch_session(struct rpc_session
*ses
)
248 if (ses
->timeout
> 0)
249 uloop_timeout_set(&ses
->t
, ses
->timeout
* 1000);
253 rpc_session_destroy(struct rpc_session
*ses
)
255 struct rpc_session_acl
*acl
, *nacl
;
256 struct rpc_session_acl_scope
*acl_scope
, *nacl_scope
;
257 struct rpc_session_data
*data
, *ndata
;
258 struct rpc_session_cb
*cb
;
260 list_for_each_entry(cb
, &destroy_callbacks
, list
)
261 cb
->cb(ses
, cb
->priv
);
263 uloop_timeout_cancel(&ses
->t
);
265 avl_for_each_element_safe(&ses
->acls
, acl_scope
, avl
, nacl_scope
) {
266 avl_remove_all_elements(&acl_scope
->acls
, acl
, avl
, nacl
)
269 avl_delete(&ses
->acls
, &acl_scope
->avl
);
273 avl_remove_all_elements(&ses
->data
, data
, avl
, ndata
)
276 avl_delete(&sessions
, &ses
->avl
);
280 static void rpc_session_timeout(struct uloop_timeout
*t
)
282 struct rpc_session
*ses
;
284 ses
= container_of(t
, struct rpc_session
, t
);
285 rpc_session_destroy(ses
);
288 static struct rpc_session
*
289 rpc_session_new(void)
291 struct rpc_session
*ses
;
293 ses
= calloc(1, sizeof(*ses
));
298 ses
->avl
.key
= ses
->id
;
300 avl_init(&ses
->acls
, avl_strcmp
, true, NULL
);
301 avl_init(&ses
->data
, avl_strcmp
, false, NULL
);
303 ses
->t
.cb
= rpc_session_timeout
;
308 static struct rpc_session
*
309 rpc_session_create(int timeout
)
311 struct rpc_session
*ses
;
312 struct rpc_session_cb
*cb
;
314 ses
= rpc_session_new();
321 ses
->timeout
= timeout
;
323 avl_insert(&sessions
, &ses
->avl
);
325 rpc_touch_session(ses
);
327 list_for_each_entry(cb
, &create_callbacks
, list
)
328 cb
->cb(ses
, cb
->priv
);
333 static struct rpc_session
*
334 rpc_session_get(const char *id
)
336 struct rpc_session
*ses
;
338 ses
= avl_find_element(&sessions
, id
, ses
, avl
);
342 rpc_touch_session(ses
);
347 rpc_handle_create(struct ubus_context
*ctx
, struct ubus_object
*obj
,
348 struct ubus_request_data
*req
, const char *method
,
349 struct blob_attr
*msg
)
351 struct rpc_session
*ses
;
352 struct blob_attr
*tb
;
353 int timeout
= RPC_DEFAULT_SESSION_TIMEOUT
;
355 blobmsg_parse(new_policy
, __RPC_SN_MAX
, &tb
, blob_data(msg
), blob_len(msg
));
357 timeout
= blobmsg_get_u32(tb
);
359 ses
= rpc_session_create(timeout
);
361 rpc_session_dump(ses
, ctx
, req
);
367 rpc_handle_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
368 struct ubus_request_data
*req
, const char *method
,
369 struct blob_attr
*msg
)
371 struct rpc_session
*ses
;
372 struct blob_attr
*tb
;
374 blobmsg_parse(sid_policy
, __RPC_SI_MAX
, &tb
, blob_data(msg
), blob_len(msg
));
377 avl_for_each_element(&sessions
, ses
, avl
)
378 rpc_session_dump(ses
, ctx
, req
);
382 ses
= rpc_session_get(blobmsg_data(tb
));
384 return UBUS_STATUS_NOT_FOUND
;
386 rpc_session_dump(ses
, ctx
, req
);
392 uh_id_len(const char *str
)
394 return strcspn(str
, "*?[");
398 rpc_session_grant(struct rpc_session
*ses
,
399 const char *scope
, const char *object
, const char *function
)
401 struct rpc_session_acl
*acl
;
402 struct rpc_session_acl_scope
*acl_scope
;
403 char *new_scope
, *new_obj
, *new_func
, *new_id
;
406 if (!object
|| !function
)
407 return UBUS_STATUS_INVALID_ARGUMENT
;
409 acl_scope
= avl_find_element(&ses
->acls
, scope
, acl_scope
, avl
);
412 uh_foreach_matching_acl_prefix(acl
, &acl_scope
->acls
, object
, function
) {
413 if (!strcmp(acl
->object
, object
) &&
414 !strcmp(acl
->function
, function
))
420 acl_scope
= calloc_a(sizeof(*acl_scope
),
421 &new_scope
, strlen(scope
) + 1);
424 return UBUS_STATUS_UNKNOWN_ERROR
;
426 acl_scope
->avl
.key
= strcpy(new_scope
, scope
);
427 avl_init(&acl_scope
->acls
, avl_strcmp
, true, NULL
);
428 avl_insert(&ses
->acls
, &acl_scope
->avl
);
431 id_len
= uh_id_len(object
);
432 acl
= calloc_a(sizeof(*acl
),
433 &new_obj
, strlen(object
) + 1,
434 &new_func
, strlen(function
) + 1,
435 &new_id
, id_len
+ 1);
438 return UBUS_STATUS_UNKNOWN_ERROR
;
440 acl
->object
= strcpy(new_obj
, object
);
441 acl
->function
= strcpy(new_func
, function
);
442 acl
->avl
.key
= strncpy(new_id
, object
, id_len
);
443 avl_insert(&acl_scope
->acls
, &acl
->avl
);
449 rpc_session_revoke(struct rpc_session
*ses
,
450 const char *scope
, const char *object
, const char *function
)
452 struct rpc_session_acl
*acl
, *next
;
453 struct rpc_session_acl_scope
*acl_scope
;
457 acl_scope
= avl_find_element(&ses
->acls
, scope
, acl_scope
, avl
);
462 if (!object
&& !function
) {
463 avl_remove_all_elements(&acl_scope
->acls
, acl
, avl
, next
)
465 avl_delete(&ses
->acls
, &acl_scope
->avl
);
470 id_len
= uh_id_len(object
);
471 id
= alloca(id_len
+ 1);
472 strncpy(id
, object
, id_len
);
475 acl
= avl_find_element(&acl_scope
->acls
, id
, acl
, avl
);
477 if (!avl_is_last(&acl_scope
->acls
, &acl
->avl
))
478 next
= avl_next_element(acl
, avl
);
482 if (strcmp(id
, acl
->avl
.key
) != 0)
485 if (!strcmp(acl
->object
, object
) &&
486 !strcmp(acl
->function
, function
)) {
487 avl_delete(&acl_scope
->acls
, &acl
->avl
);
493 if (avl_is_empty(&acl_scope
->acls
)) {
494 avl_delete(&ses
->acls
, &acl_scope
->avl
);
503 rpc_handle_acl(struct ubus_context
*ctx
, struct ubus_object
*obj
,
504 struct ubus_request_data
*req
, const char *method
,
505 struct blob_attr
*msg
)
507 struct rpc_session
*ses
;
508 struct blob_attr
*tb
[__RPC_SA_MAX
];
509 struct blob_attr
*attr
, *sattr
;
510 const char *object
, *function
;
511 const char *scope
= "ubus";
514 int (*cb
)(struct rpc_session
*ses
,
515 const char *scope
, const char *object
, const char *function
);
517 blobmsg_parse(acl_policy
, __RPC_SA_MAX
, tb
, blob_data(msg
), blob_len(msg
));
520 return UBUS_STATUS_INVALID_ARGUMENT
;
522 ses
= rpc_session_get(blobmsg_data(tb
[RPC_SA_SID
]));
524 return UBUS_STATUS_NOT_FOUND
;
526 if (tb
[RPC_SA_SCOPE
])
527 scope
= blobmsg_data(tb
[RPC_SA_SCOPE
]);
529 if (!strcmp(method
, "grant"))
530 cb
= rpc_session_grant
;
532 cb
= rpc_session_revoke
;
534 if (!tb
[RPC_SA_OBJECTS
])
535 return cb(ses
, scope
, NULL
, NULL
);
537 blobmsg_for_each_attr(attr
, tb
[RPC_SA_OBJECTS
], rem1
) {
538 if (blobmsg_type(attr
) != BLOBMSG_TYPE_ARRAY
)
544 blobmsg_for_each_attr(sattr
, attr
, rem2
) {
545 if (blobmsg_type(sattr
) != BLOBMSG_TYPE_STRING
)
549 object
= blobmsg_data(sattr
);
551 function
= blobmsg_data(sattr
);
556 if (object
&& function
)
557 cb(ses
, scope
, object
, function
);
564 rpc_session_acl_allowed(struct rpc_session
*ses
, const char *scope
,
565 const char *obj
, const char *fun
)
567 struct rpc_session_acl
*acl
;
568 struct rpc_session_acl_scope
*acl_scope
;
570 acl_scope
= avl_find_element(&ses
->acls
, scope
, acl_scope
, avl
);
573 uh_foreach_matching_acl(acl
, &acl_scope
->acls
, obj
, fun
)
581 rpc_handle_access(struct ubus_context
*ctx
, struct ubus_object
*obj
,
582 struct ubus_request_data
*req
, const char *method
,
583 struct blob_attr
*msg
)
585 struct rpc_session
*ses
;
586 struct blob_attr
*tb
[__RPC_SP_MAX
];
587 const char *scope
= "ubus";
590 blobmsg_parse(perm_policy
, __RPC_SP_MAX
, tb
, blob_data(msg
), blob_len(msg
));
593 return UBUS_STATUS_INVALID_ARGUMENT
;
595 ses
= rpc_session_get(blobmsg_data(tb
[RPC_SP_SID
]));
597 return UBUS_STATUS_NOT_FOUND
;
599 blob_buf_init(&buf
, 0);
601 if (tb
[RPC_SP_OBJECT
] && tb
[RPC_SP_FUNCTION
])
603 if (tb
[RPC_SP_SCOPE
])
604 scope
= blobmsg_data(tb
[RPC_SP_SCOPE
]);
606 allow
= rpc_session_acl_allowed(ses
, scope
,
607 blobmsg_data(tb
[RPC_SP_OBJECT
]),
608 blobmsg_data(tb
[RPC_SP_FUNCTION
]));
610 blobmsg_add_u8(&buf
, "access", allow
);
614 rpc_session_dump_acls(ses
, &buf
);
617 ubus_send_reply(ctx
, req
, buf
.head
);
623 rpc_session_set(struct rpc_session
*ses
, const char *key
, struct blob_attr
*val
)
625 struct rpc_session_data
*data
;
627 data
= avl_find_element(&ses
->data
, key
, data
, avl
);
629 avl_delete(&ses
->data
, &data
->avl
);
633 data
= calloc(1, sizeof(*data
) + blob_pad_len(val
));
637 memcpy(data
->attr
, val
, blob_pad_len(val
));
638 data
->avl
.key
= blobmsg_name(data
->attr
);
639 avl_insert(&ses
->data
, &data
->avl
);
643 rpc_handle_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
644 struct ubus_request_data
*req
, const char *method
,
645 struct blob_attr
*msg
)
647 struct rpc_session
*ses
;
648 struct blob_attr
*tb
[__RPC_SS_MAX
];
649 struct blob_attr
*attr
;
652 blobmsg_parse(set_policy
, __RPC_SS_MAX
, tb
, blob_data(msg
), blob_len(msg
));
654 if (!tb
[RPC_SS_SID
] || !tb
[RPC_SS_VALUES
])
655 return UBUS_STATUS_INVALID_ARGUMENT
;
657 ses
= rpc_session_get(blobmsg_data(tb
[RPC_SS_SID
]));
659 return UBUS_STATUS_NOT_FOUND
;
661 blobmsg_for_each_attr(attr
, tb
[RPC_SS_VALUES
], rem
) {
662 if (!blobmsg_name(attr
)[0])
665 rpc_session_set(ses
, blobmsg_name(attr
), attr
);
672 rpc_handle_get(struct ubus_context
*ctx
, struct ubus_object
*obj
,
673 struct ubus_request_data
*req
, const char *method
,
674 struct blob_attr
*msg
)
676 struct rpc_session
*ses
;
677 struct rpc_session_data
*data
;
678 struct blob_attr
*tb
[__RPC_SG_MAX
];
679 struct blob_attr
*attr
;
683 blobmsg_parse(get_policy
, __RPC_SG_MAX
, tb
, blob_data(msg
), blob_len(msg
));
686 return UBUS_STATUS_INVALID_ARGUMENT
;
688 ses
= rpc_session_get(blobmsg_data(tb
[RPC_SG_SID
]));
690 return UBUS_STATUS_NOT_FOUND
;
692 blob_buf_init(&buf
, 0);
693 c
= blobmsg_open_table(&buf
, "values");
696 blobmsg_for_each_attr(attr
, tb
[RPC_SG_KEYS
], rem
) {
697 if (blobmsg_type(attr
) != BLOBMSG_TYPE_STRING
)
700 data
= avl_find_element(&ses
->data
, blobmsg_data(attr
), data
, avl
);
704 blobmsg_add_field(&buf
, blobmsg_type(data
->attr
),
705 blobmsg_name(data
->attr
),
706 blobmsg_data(data
->attr
),
707 blobmsg_data_len(data
->attr
));
710 rpc_session_dump_data(ses
, &buf
);
712 blobmsg_close_table(&buf
, c
);
713 ubus_send_reply(ctx
, req
, buf
.head
);
719 rpc_handle_unset(struct ubus_context
*ctx
, struct ubus_object
*obj
,
720 struct ubus_request_data
*req
, const char *method
,
721 struct blob_attr
*msg
)
723 struct rpc_session
*ses
;
724 struct rpc_session_data
*data
, *ndata
;
725 struct blob_attr
*tb
[__RPC_SA_MAX
];
726 struct blob_attr
*attr
;
729 blobmsg_parse(get_policy
, __RPC_SG_MAX
, tb
, blob_data(msg
), blob_len(msg
));
732 return UBUS_STATUS_INVALID_ARGUMENT
;
734 ses
= rpc_session_get(blobmsg_data(tb
[RPC_SG_SID
]));
736 return UBUS_STATUS_NOT_FOUND
;
738 if (!tb
[RPC_SG_KEYS
]) {
739 avl_remove_all_elements(&ses
->data
, data
, avl
, ndata
)
744 blobmsg_for_each_attr(attr
, tb
[RPC_SG_KEYS
], rem
) {
745 if (blobmsg_type(attr
) != BLOBMSG_TYPE_STRING
)
748 data
= avl_find_element(&ses
->data
, blobmsg_data(attr
), data
, avl
);
752 avl_delete(&ses
->data
, &data
->avl
);
760 rpc_handle_destroy(struct ubus_context
*ctx
, struct ubus_object
*obj
,
761 struct ubus_request_data
*req
, const char *method
,
762 struct blob_attr
*msg
)
764 struct rpc_session
*ses
;
765 struct blob_attr
*tb
;
767 blobmsg_parse(sid_policy
, __RPC_SI_MAX
, &tb
, blob_data(msg
), blob_len(msg
));
770 return UBUS_STATUS_INVALID_ARGUMENT
;
772 if (!strcmp(blobmsg_get_string(tb
), RPC_DEFAULT_SESSION_ID
))
773 return UBUS_STATUS_PERMISSION_DENIED
;
775 ses
= rpc_session_get(blobmsg_data(tb
));
777 return UBUS_STATUS_NOT_FOUND
;
779 rpc_session_destroy(ses
);
786 rpc_login_test_password(const char *hash
, const char *password
)
790 /* password is not set */
791 if (!hash
|| !*hash
|| !strcmp(hash
, "!") || !strcmp(hash
, "x"))
796 /* password hash refers to shadow/passwd */
797 else if (!strncmp(hash
, "$p$", 3))
800 struct spwd
*sp
= getspnam(hash
+ 3);
805 return rpc_login_test_password(sp
->sp_pwdp
, password
);
807 struct passwd
*pw
= getpwnam(hash
+ 3);
812 return rpc_login_test_password(pw
->pw_passwd
, password
);
816 crypt_hash
= crypt(password
, hash
);
818 return !strcmp(crypt_hash
, hash
);
821 static struct uci_section
*
822 rpc_login_test_login(struct uci_context
*uci
,
823 const char *username
, const char *password
)
825 struct uci_package
*p
= NULL
;
826 struct uci_section
*s
;
827 struct uci_element
*e
;
828 struct uci_ptr ptr
= { .package
= "rpcd" };
830 uci_load(uci
, ptr
.package
, &p
);
835 uci_foreach_element(&p
->sections
, e
)
837 s
= uci_to_section(e
);
839 if (strcmp(s
->type
, "login"))
842 ptr
.section
= s
->e
.name
;
845 /* test for matching username */
846 ptr
.option
= "username";
849 if (uci_lookup_ptr(uci
, &ptr
, NULL
, true))
852 if (ptr
.o
->type
!= UCI_TYPE_STRING
)
855 if (strcmp(ptr
.o
->v
.string
, username
))
858 /* If password is NULL, we're restoring ACLs for an existing session,
859 * in this case do not check the password again. */
863 /* test for matching password */
864 ptr
.option
= "password";
867 if (uci_lookup_ptr(uci
, &ptr
, NULL
, true))
870 if (ptr
.o
->type
!= UCI_TYPE_STRING
)
873 if (rpc_login_test_password(ptr
.o
->v
.string
, password
))
881 rpc_login_test_permission(struct uci_section
*s
,
882 const char *perm
, const char *group
)
885 struct uci_option
*o
;
886 struct uci_element
*e
, *l
;
888 /* If the login section is not provided, we're setting up acls for the
889 * default session, in this case uncondionally allow access to the
890 * "unauthenticated" access group */
892 return !strcmp(group
, "unauthenticated");
895 uci_foreach_element(&s
->options
, e
)
897 o
= uci_to_option(e
);
899 if (o
->type
!= UCI_TYPE_LIST
)
902 if (strcmp(o
->e
.name
, perm
))
905 /* Match negative expressions first. If a negative expression matches
906 * the current group name then deny access. */
907 uci_foreach_element(&o
->v
.list
, l
) {
913 while (isspace(*++p
));
918 if (!fnmatch(p
, group
, 0))
922 uci_foreach_element(&o
->v
.list
, l
) {
923 if (!l
->name
|| !*l
->name
|| *l
->name
== '!')
926 if (!fnmatch(l
->name
, group
, 0))
931 /* make sure that write permission implies read permission */
932 if (!strcmp(perm
, "read"))
933 return rpc_login_test_permission(s
, "write", group
);
939 rpc_login_setup_acl_scope(struct rpc_session
*ses
,
940 struct blob_attr
*acl_perm
,
941 struct blob_attr
*acl_scope
)
943 struct blob_attr
*acl_obj
, *acl_func
;
947 * Parse ACL scopes in table notation.
957 if (blobmsg_type(acl_scope
) == BLOBMSG_TYPE_TABLE
) {
958 blobmsg_for_each_attr(acl_obj
, acl_scope
, rem
) {
959 if (blobmsg_type(acl_obj
) != BLOBMSG_TYPE_ARRAY
)
962 blobmsg_for_each_attr(acl_func
, acl_obj
, rem2
) {
963 if (blobmsg_type(acl_func
) != BLOBMSG_TYPE_STRING
)
966 rpc_session_grant(ses
, blobmsg_name(acl_scope
),
967 blobmsg_name(acl_obj
),
968 blobmsg_data(acl_func
));
974 * Parse ACL scopes in array notation. The permission ("read" or "write")
975 * will be used as function name for each object.
983 else if (blobmsg_type(acl_scope
) == BLOBMSG_TYPE_ARRAY
) {
984 blobmsg_for_each_attr(acl_obj
, acl_scope
, rem
) {
985 if (blobmsg_type(acl_obj
) != BLOBMSG_TYPE_STRING
)
988 rpc_session_grant(ses
, blobmsg_name(acl_scope
),
989 blobmsg_data(acl_obj
),
990 blobmsg_name(acl_perm
));
996 rpc_login_setup_acl_file(struct rpc_session
*ses
, struct uci_section
*login
,
999 struct blob_buf acl
= { 0 };
1000 struct blob_attr
*acl_group
, *acl_perm
, *acl_scope
;
1001 int rem
, rem2
, rem3
;
1003 blob_buf_init(&acl
, 0);
1005 if (!blobmsg_add_json_from_file(&acl
, path
)) {
1006 fprintf(stderr
, "Failed to parse %s\n", path
);
1010 /* Iterate access groups in toplevel object */
1011 blob_for_each_attr(acl_group
, acl
.head
, rem
) {
1012 /* Iterate permission objects in each access group object */
1013 blobmsg_for_each_attr(acl_perm
, acl_group
, rem2
) {
1014 if (blobmsg_type(acl_perm
) != BLOBMSG_TYPE_TABLE
)
1017 /* Only "read" and "write" permissions are defined */
1018 if (strcmp(blobmsg_name(acl_perm
), "read") &&
1019 strcmp(blobmsg_name(acl_perm
), "write"))
1023 * Check if the current user context specifies the current
1024 * "read" or "write" permission in the given access group.
1026 if (!rpc_login_test_permission(login
, blobmsg_name(acl_perm
),
1027 blobmsg_name(acl_group
)))
1030 /* Iterate scope objects within the permission object */
1031 blobmsg_for_each_attr(acl_scope
, acl_perm
, rem3
) {
1032 /* Setup the scopes of the access group */
1033 rpc_login_setup_acl_scope(ses
, acl_perm
, acl_scope
);
1036 * Add the access group itself as object to the "access-group"
1037 * meta scope and the the permission level ("read" or "write")
1051 * This allows session clients to easily query the allowed
1052 * access groups without having to test access of each single
1053 * <scope>/<object>/<function> tuple defined in a group.
1055 rpc_session_grant(ses
, "access-group",
1056 blobmsg_name(acl_group
),
1057 blobmsg_name(acl_perm
));
1063 blob_buf_free(&acl
);
1067 rpc_login_setup_acls(struct rpc_session
*ses
, struct uci_section
*login
)
1072 if (glob(RPC_SESSION_ACL_DIR
"/*.json", 0, NULL
, &gl
))
1075 for (i
= 0; i
< gl
.gl_pathc
; i
++)
1076 rpc_login_setup_acl_file(ses
, login
, gl
.gl_pathv
[i
]);
1082 rpc_handle_login(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1083 struct ubus_request_data
*req
, const char *method
,
1084 struct blob_attr
*msg
)
1086 struct uci_context
*uci
= NULL
;
1087 struct uci_section
*login
;
1088 struct rpc_session
*ses
;
1089 struct blob_attr
*tb
[__RPC_L_MAX
];
1090 int timeout
= RPC_DEFAULT_SESSION_TIMEOUT
;
1093 blobmsg_parse(login_policy
, __RPC_L_MAX
, tb
, blob_data(msg
), blob_len(msg
));
1095 if (!tb
[RPC_L_USERNAME
] || !tb
[RPC_L_PASSWORD
]) {
1096 rv
= UBUS_STATUS_INVALID_ARGUMENT
;
1100 uci
= uci_alloc_context();
1103 rv
= UBUS_STATUS_UNKNOWN_ERROR
;
1107 login
= rpc_login_test_login(uci
, blobmsg_get_string(tb
[RPC_L_USERNAME
]),
1108 blobmsg_get_string(tb
[RPC_L_PASSWORD
]));
1111 rv
= UBUS_STATUS_PERMISSION_DENIED
;
1115 if (tb
[RPC_L_TIMEOUT
])
1116 timeout
= blobmsg_get_u32(tb
[RPC_L_TIMEOUT
]);
1118 ses
= rpc_session_create(timeout
);
1121 rv
= UBUS_STATUS_UNKNOWN_ERROR
;
1125 rpc_login_setup_acls(ses
, login
);
1127 rpc_session_set(ses
, "user", tb
[RPC_L_USERNAME
]);
1128 rpc_session_dump(ses
, ctx
, req
);
1132 uci_free_context(uci
);
1139 rpc_validate_sid(const char *id
)
1144 if (strlen(id
) != RPC_SID_LEN
)
1148 if (!isxdigit(*id
++))
1155 rpc_blob_to_file(const char *path
, struct blob_attr
*attr
)
1159 fd
= open(path
, O_WRONLY
| O_CREAT
| O_EXCL
, 0600);
1164 len
= write(fd
, attr
, blob_pad_len(attr
));
1168 if (len
!= blob_pad_len(attr
))
1177 static struct blob_attr
*
1178 rpc_blob_from_file(const char *path
)
1182 struct blob_attr head
, *attr
= NULL
;
1184 if (stat(path
, &s
) || !S_ISREG(s
.st_mode
))
1187 fd
= open(path
, O_RDONLY
);
1192 len
= read(fd
, &head
, sizeof(head
));
1194 if (len
!= sizeof(head
) || blob_pad_len(&head
) != s
.st_size
)
1197 attr
= calloc(1, s
.st_size
);
1202 memcpy(attr
, &head
, sizeof(head
));
1204 len
+= read(fd
, (char *)attr
+ sizeof(head
), s
.st_size
- sizeof(head
));
1206 if (len
!= blob_pad_len(&head
))
1224 rpc_session_from_blob(struct uci_context
*uci
, struct blob_attr
*attr
)
1227 const char *user
= NULL
;
1228 struct rpc_session
*ses
;
1229 struct uci_section
*login
;
1230 struct blob_attr
*tb
[__RPC_DUMP_MAX
], *data
;
1232 blobmsg_parse(dump_policy
, __RPC_DUMP_MAX
, tb
,
1233 blob_data(attr
), blob_len(attr
));
1235 for (i
= 0; i
< __RPC_DUMP_MAX
; i
++)
1239 ses
= rpc_session_new();
1244 memcpy(ses
->id
, blobmsg_data(tb
[RPC_DUMP_SID
]), RPC_SID_LEN
);
1246 ses
->timeout
= blobmsg_get_u32(tb
[RPC_DUMP_TIMEOUT
]);
1248 blobmsg_for_each_attr(data
, tb
[RPC_DUMP_DATA
], rem
) {
1249 rpc_session_set(ses
, blobmsg_name(data
), data
);
1251 if (!strcmp(blobmsg_name(data
), "username"))
1252 user
= blobmsg_get_string(data
);
1256 login
= rpc_login_test_login(uci
, user
, NULL
);
1258 rpc_login_setup_acls(ses
, login
);
1261 avl_insert(&sessions
, &ses
->avl
);
1263 uloop_timeout_set(&ses
->t
, blobmsg_get_u32(tb
[RPC_DUMP_EXPIRES
]) * 1000);
1268 int rpc_session_api_init(struct ubus_context
*ctx
)
1270 struct rpc_session
*ses
;
1272 static const struct ubus_method session_methods
[] = {
1273 UBUS_METHOD("create", rpc_handle_create
, new_policy
),
1274 UBUS_METHOD("list", rpc_handle_list
, sid_policy
),
1275 UBUS_METHOD("grant", rpc_handle_acl
, acl_policy
),
1276 UBUS_METHOD("revoke", rpc_handle_acl
, acl_policy
),
1277 UBUS_METHOD("access", rpc_handle_access
, perm_policy
),
1278 UBUS_METHOD("set", rpc_handle_set
, set_policy
),
1279 UBUS_METHOD("get", rpc_handle_get
, get_policy
),
1280 UBUS_METHOD("unset", rpc_handle_unset
, get_policy
),
1281 UBUS_METHOD("destroy", rpc_handle_destroy
, sid_policy
),
1282 UBUS_METHOD("login", rpc_handle_login
, login_policy
),
1285 static struct ubus_object_type session_type
=
1286 UBUS_OBJECT_TYPE("luci-rpc-session", session_methods
);
1288 static struct ubus_object obj
= {
1290 .type
= &session_type
,
1291 .methods
= session_methods
,
1292 .n_methods
= ARRAY_SIZE(session_methods
),
1295 avl_init(&sessions
, avl_strcmp
, false, NULL
);
1297 /* setup the default session */
1298 ses
= rpc_session_new();
1301 strcpy(ses
->id
, RPC_DEFAULT_SESSION_ID
);
1302 rpc_login_setup_acls(ses
, NULL
);
1303 avl_insert(&sessions
, &ses
->avl
);
1306 return ubus_add_object(ctx
, &obj
);
1309 bool rpc_session_access(const char *sid
, const char *scope
,
1310 const char *object
, const char *function
)
1312 struct rpc_session
*ses
= rpc_session_get(sid
);
1317 return rpc_session_acl_allowed(ses
, scope
, object
, function
);
1320 void rpc_session_create_cb(struct rpc_session_cb
*cb
)
1323 list_add(&cb
->list
, &create_callbacks
);
1326 void rpc_session_destroy_cb(struct rpc_session_cb
*cb
)
1329 list_add(&cb
->list
, &destroy_callbacks
);
1332 void rpc_session_freeze(void)
1335 struct rpc_session
*ses
;
1336 char path
[PATH_MAX
];
1338 if (stat(RPC_SESSION_DIRECTORY
, &s
))
1339 mkdir(RPC_SESSION_DIRECTORY
, 0700);
1341 avl_for_each_element(&sessions
, ses
, avl
) {
1342 /* skip default session */
1343 if (!strcmp(ses
->id
, RPC_DEFAULT_SESSION_ID
))
1346 snprintf(path
, sizeof(path
) - 1, RPC_SESSION_DIRECTORY
"/%s", ses
->id
);
1347 rpc_session_to_blob(ses
, false);
1348 rpc_blob_to_file(path
, buf
.head
);
1352 void rpc_session_thaw(void)
1355 char path
[PATH_MAX
];
1357 struct blob_attr
*attr
;
1358 struct uci_context
*uci
;
1360 d
= opendir(RPC_SESSION_DIRECTORY
);
1365 uci
= uci_alloc_context();
1370 while ((e
= readdir(d
)) != NULL
) {
1371 if (!rpc_validate_sid(e
->d_name
))
1374 snprintf(path
, sizeof(path
) - 1,
1375 RPC_SESSION_DIRECTORY
"/%s", e
->d_name
);
1377 attr
= rpc_blob_from_file(path
);
1380 rpc_session_from_blob(uci
, attr
);
1389 uci_free_context(uci
);