2 * uhttpd - Tiny single-threaded httpd
4 * Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
5 * Copyright (C) 2013 Felix Fietkau <nbd@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 #include <libubox/avl-cmp.h>
21 #include <libubox/utils.h>
25 #include "ubus-session.h"
27 static struct avl_tree sessions
;
28 static struct blob_buf buf
;
30 static const struct blobmsg_policy new_policy
= {
31 .name
= "timeout", .type
= BLOBMSG_TYPE_INT32
34 static const struct blobmsg_policy sid_policy
= {
35 .name
= "sid", .type
= BLOBMSG_TYPE_STRING
43 static const struct blobmsg_policy set_policy
[__UH_UBUS_SS_MAX
] = {
44 [UH_UBUS_SS_SID
] = { .name
= "sid", .type
= BLOBMSG_TYPE_STRING
},
45 [UH_UBUS_SS_VALUES
] = { .name
= "values", .type
= BLOBMSG_TYPE_TABLE
},
53 static const struct blobmsg_policy get_policy
[__UH_UBUS_SG_MAX
] = {
54 [UH_UBUS_SG_SID
] = { .name
= "sid", .type
= BLOBMSG_TYPE_STRING
},
55 [UH_UBUS_SG_KEYS
] = { .name
= "keys", .type
= BLOBMSG_TYPE_ARRAY
},
63 static const struct blobmsg_policy acl_policy
[__UH_UBUS_SA_MAX
] = {
64 [UH_UBUS_SA_SID
] = { .name
= "sid", .type
= BLOBMSG_TYPE_STRING
},
65 [UH_UBUS_SA_OBJECTS
] = { .name
= "objects", .type
= BLOBMSG_TYPE_ARRAY
},
69 * Keys in the AVL tree contain all pattern characters up to the first wildcard.
70 * To look up entries, start with the last entry that has a key less than or
71 * equal to the method name, then work backwards as long as the AVL key still
72 * matches its counterpart in the object name
74 #define uh_foreach_matching_acl_prefix(_acl, _ses, _obj, _func) \
75 for (_acl = avl_find_le_element(&(_ses)->acls, _obj, _acl, avl); \
77 _acl = avl_is_first(&(ses)->acls, &(_acl)->avl) ? NULL : \
78 avl_prev_element((_acl), avl))
80 #define uh_foreach_matching_acl(_acl, _ses, _obj, _func) \
81 uh_foreach_matching_acl_prefix(_acl, _ses, _obj, _func) \
82 if (!strncmp((_acl)->object, _obj, (_acl)->sort_len) && \
83 !fnmatch((_acl)->object, (_obj), FNM_NOESCAPE) && \
84 !fnmatch((_acl)->function, (_func), FNM_NOESCAPE))
87 uh_ubus_random(char *dest
)
89 unsigned char buf
[16] = { 0 };
93 f
= fopen("/dev/urandom", "r");
97 fread(buf
, 1, sizeof(buf
), f
);
100 for (i
= 0; i
< sizeof(buf
); i
++)
101 sprintf(dest
+ (i
<<1), "%02x", buf
[i
]);
105 uh_ubus_session_dump_data(struct uh_ubus_session
*ses
, struct blob_buf
*b
)
107 struct uh_ubus_session_data
*d
;
109 avl_for_each_element(&ses
->data
, d
, avl
) {
110 blobmsg_add_field(b
, blobmsg_type(d
->attr
), blobmsg_name(d
->attr
),
111 blobmsg_data(d
->attr
), blobmsg_data_len(d
->attr
));
116 uh_ubus_session_dump_acls(struct uh_ubus_session
*ses
, struct blob_buf
*b
)
118 struct uh_ubus_session_acl
*acl
;
119 const char *lastobj
= NULL
;
122 avl_for_each_element(&ses
->acls
, acl
, avl
) {
123 if (!lastobj
|| strcmp(acl
->object
, lastobj
))
125 if (c
) blobmsg_close_array(b
, c
);
126 c
= blobmsg_open_array(b
, acl
->object
);
129 blobmsg_add_string(b
, NULL
, acl
->function
);
130 lastobj
= acl
->object
;
133 if (c
) blobmsg_close_array(b
, c
);
137 uh_ubus_session_dump(struct uh_ubus_session
*ses
,
138 struct ubus_context
*ctx
,
139 struct ubus_request_data
*req
)
143 blob_buf_init(&buf
, 0);
145 blobmsg_add_string(&buf
, "sid", ses
->id
);
146 blobmsg_add_u32(&buf
, "timeout", ses
->timeout
);
148 c
= blobmsg_open_table(&buf
, "acls");
149 uh_ubus_session_dump_acls(ses
, &buf
);
150 blobmsg_close_table(&buf
, c
);
152 c
= blobmsg_open_table(&buf
, "data");
153 uh_ubus_session_dump_data(ses
, &buf
);
154 blobmsg_close_table(&buf
, c
);
156 ubus_send_reply(ctx
, req
, buf
.head
);
160 uh_ubus_touch_session(struct uh_ubus_session
*ses
)
162 uloop_timeout_set(&ses
->t
, ses
->timeout
* 1000);
166 uh_ubus_session_destroy(struct uh_ubus_session
*ses
)
168 struct uh_ubus_session_acl
*acl
, *nacl
;
169 struct uh_ubus_session_data
*data
, *ndata
;
171 uloop_timeout_cancel(&ses
->t
);
172 avl_remove_all_elements(&ses
->acls
, acl
, avl
, nacl
)
175 avl_remove_all_elements(&ses
->data
, data
, avl
, ndata
)
178 avl_delete(&sessions
, &ses
->avl
);
182 static void uh_ubus_session_timeout(struct uloop_timeout
*t
)
184 struct uh_ubus_session
*ses
;
186 ses
= container_of(t
, struct uh_ubus_session
, t
);
187 uh_ubus_session_destroy(ses
);
190 static struct uh_ubus_session
*
191 uh_ubus_session_create(int timeout
)
193 struct uh_ubus_session
*ses
;
195 ses
= calloc(1, sizeof(*ses
));
199 ses
->timeout
= timeout
;
200 ses
->avl
.key
= ses
->id
;
201 uh_ubus_random(ses
->id
);
203 avl_insert(&sessions
, &ses
->avl
);
204 avl_init(&ses
->acls
, avl_strcmp
, true, NULL
);
205 avl_init(&ses
->data
, avl_strcmp
, false, NULL
);
207 ses
->t
.cb
= uh_ubus_session_timeout
;
208 uh_ubus_touch_session(ses
);
213 struct uh_ubus_session
*
214 uh_ubus_session_get(const char *id
)
216 struct uh_ubus_session
*ses
;
218 ses
= avl_find_element(&sessions
, id
, ses
, avl
);
222 uh_ubus_touch_session(ses
);
227 uh_ubus_handle_create(struct ubus_context
*ctx
, struct ubus_object
*obj
,
228 struct ubus_request_data
*req
, const char *method
,
229 struct blob_attr
*msg
)
231 struct uh_ubus_session
*ses
;
232 struct blob_attr
*tb
;
233 int timeout
= UBUS_DEFAULT_SESSION_TIMEOUT
;
235 blobmsg_parse(&new_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
237 timeout
= blobmsg_get_u32(tb
);
239 ses
= uh_ubus_session_create(timeout
);
241 uh_ubus_session_dump(ses
, ctx
, req
);
247 uh_ubus_handle_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
248 struct ubus_request_data
*req
, const char *method
,
249 struct blob_attr
*msg
)
251 struct uh_ubus_session
*ses
;
252 struct blob_attr
*tb
;
254 blobmsg_parse(&sid_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
257 avl_for_each_element(&sessions
, ses
, avl
)
258 uh_ubus_session_dump(ses
, ctx
, req
);
262 ses
= uh_ubus_session_get(blobmsg_data(tb
));
264 return UBUS_STATUS_NOT_FOUND
;
266 uh_ubus_session_dump(ses
, ctx
, req
);
272 uh_id_len(const char *str
)
274 return strcspn(str
, "*?[");
278 uh_ubus_session_grant(struct uh_ubus_session
*ses
, struct ubus_context
*ctx
,
279 const char *object
, const char *function
)
281 struct uh_ubus_session_acl
*acl
;
282 char *new_obj
, *new_func
, *new_id
;
285 if (!object
|| !function
)
286 return UBUS_STATUS_INVALID_ARGUMENT
;
288 uh_foreach_matching_acl_prefix(acl
, ses
, object
, function
) {
289 if (!strcmp(acl
->object
, object
) &&
290 !strcmp(acl
->function
, function
))
294 id_len
= uh_id_len(object
);
295 acl
= calloc_a(sizeof(*acl
),
296 &new_obj
, strlen(object
) + 1,
297 &new_func
, strlen(function
) + 1,
298 &new_id
, id_len
+ 1);
301 return UBUS_STATUS_UNKNOWN_ERROR
;
303 acl
->object
= strcpy(new_obj
, object
);
304 acl
->function
= strcpy(new_func
, function
);
305 acl
->avl
.key
= strncpy(new_id
, object
, id_len
);
306 avl_insert(&ses
->acls
, &acl
->avl
);
312 uh_ubus_session_revoke(struct uh_ubus_session
*ses
, struct ubus_context
*ctx
,
313 const char *object
, const char *function
)
315 struct uh_ubus_session_acl
*acl
, *next
;
319 if (!object
&& !function
) {
320 avl_remove_all_elements(&ses
->acls
, acl
, avl
, next
)
325 id_len
= uh_id_len(object
);
326 id
= alloca(id_len
+ 1);
327 strncpy(id
, object
, id_len
);
330 acl
= avl_find_element(&ses
->acls
, id
, acl
, avl
);
332 if (!avl_is_last(&ses
->acls
, &acl
->avl
))
333 next
= avl_next_element(acl
, avl
);
337 if (strcmp(id
, acl
->avl
.key
) != 0)
340 if (!strcmp(acl
->object
, object
) &&
341 !strcmp(acl
->function
, function
)) {
342 avl_delete(&ses
->acls
, &acl
->avl
);
353 uh_ubus_handle_acl(struct ubus_context
*ctx
, struct ubus_object
*obj
,
354 struct ubus_request_data
*req
, const char *method
,
355 struct blob_attr
*msg
)
357 struct uh_ubus_session
*ses
;
358 struct blob_attr
*tb
[__UH_UBUS_SA_MAX
];
359 struct blob_attr
*attr
, *sattr
;
360 const char *object
, *function
;
363 int (*cb
)(struct uh_ubus_session
*ses
, struct ubus_context
*ctx
,
364 const char *object
, const char *function
);
366 blobmsg_parse(acl_policy
, __UH_UBUS_SA_MAX
, tb
, blob_data(msg
), blob_len(msg
));
368 if (!tb
[UH_UBUS_SA_SID
])
369 return UBUS_STATUS_INVALID_ARGUMENT
;
371 ses
= uh_ubus_session_get(blobmsg_data(tb
[UH_UBUS_SA_SID
]));
373 return UBUS_STATUS_NOT_FOUND
;
375 if (!strcmp(method
, "grant"))
376 cb
= uh_ubus_session_grant
;
378 cb
= uh_ubus_session_revoke
;
380 if (!tb
[UH_UBUS_SA_OBJECTS
])
381 return cb(ses
, ctx
, NULL
, NULL
);
383 blobmsg_for_each_attr(attr
, tb
[UH_UBUS_SA_OBJECTS
], rem1
) {
384 if (blob_id(attr
) != BLOBMSG_TYPE_ARRAY
)
390 blobmsg_for_each_attr(sattr
, attr
, rem2
) {
391 if (blob_id(sattr
) != BLOBMSG_TYPE_STRING
)
395 object
= blobmsg_data(sattr
);
397 function
= blobmsg_data(sattr
);
402 if (object
&& function
)
403 cb(ses
, ctx
, object
, function
);
410 uh_ubus_handle_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
411 struct ubus_request_data
*req
, const char *method
,
412 struct blob_attr
*msg
)
414 struct uh_ubus_session
*ses
;
415 struct uh_ubus_session_data
*data
;
416 struct blob_attr
*tb
[__UH_UBUS_SA_MAX
];
417 struct blob_attr
*attr
;
420 blobmsg_parse(set_policy
, __UH_UBUS_SS_MAX
, tb
, blob_data(msg
), blob_len(msg
));
422 if (!tb
[UH_UBUS_SS_SID
] || !tb
[UH_UBUS_SS_VALUES
])
423 return UBUS_STATUS_INVALID_ARGUMENT
;
425 ses
= uh_ubus_session_get(blobmsg_data(tb
[UH_UBUS_SS_SID
]));
427 return UBUS_STATUS_NOT_FOUND
;
429 blobmsg_for_each_attr(attr
, tb
[UH_UBUS_SS_VALUES
], rem
) {
430 if (!blobmsg_name(attr
)[0])
433 data
= avl_find_element(&ses
->data
, blobmsg_name(attr
), data
, avl
);
435 avl_delete(&ses
->data
, &data
->avl
);
439 data
= calloc(1, sizeof(*data
) + blob_pad_len(attr
));
443 memcpy(data
->attr
, attr
, blob_pad_len(attr
));
444 data
->avl
.key
= blobmsg_name(data
->attr
);
445 avl_insert(&ses
->data
, &data
->avl
);
452 uh_ubus_handle_get(struct ubus_context
*ctx
, struct ubus_object
*obj
,
453 struct ubus_request_data
*req
, const char *method
,
454 struct blob_attr
*msg
)
456 struct uh_ubus_session
*ses
;
457 struct uh_ubus_session_data
*data
;
458 struct blob_attr
*tb
[__UH_UBUS_SA_MAX
];
459 struct blob_attr
*attr
;
463 blobmsg_parse(get_policy
, __UH_UBUS_SG_MAX
, tb
, blob_data(msg
), blob_len(msg
));
465 if (!tb
[UH_UBUS_SG_SID
])
466 return UBUS_STATUS_INVALID_ARGUMENT
;
468 ses
= uh_ubus_session_get(blobmsg_data(tb
[UH_UBUS_SG_SID
]));
470 return UBUS_STATUS_NOT_FOUND
;
472 blob_buf_init(&buf
, 0);
473 c
= blobmsg_open_table(&buf
, "values");
475 if (!tb
[UH_UBUS_SG_KEYS
]) {
476 uh_ubus_session_dump_data(ses
, &buf
);
480 blobmsg_for_each_attr(attr
, tb
[UH_UBUS_SG_KEYS
], rem
) {
481 if (blob_id(attr
) != BLOBMSG_TYPE_STRING
)
484 data
= avl_find_element(&ses
->data
, blobmsg_data(attr
), data
, avl
);
488 blobmsg_add_field(&buf
, blobmsg_type(data
->attr
),
489 blobmsg_name(data
->attr
),
490 blobmsg_data(data
->attr
),
491 blobmsg_data_len(data
->attr
));
494 blobmsg_close_table(&buf
, c
);
495 ubus_send_reply(ctx
, req
, buf
.head
);
501 uh_ubus_handle_unset(struct ubus_context
*ctx
, struct ubus_object
*obj
,
502 struct ubus_request_data
*req
, const char *method
,
503 struct blob_attr
*msg
)
505 struct uh_ubus_session
*ses
;
506 struct uh_ubus_session_data
*data
, *ndata
;
507 struct blob_attr
*tb
[__UH_UBUS_SA_MAX
];
508 struct blob_attr
*attr
;
511 blobmsg_parse(get_policy
, __UH_UBUS_SG_MAX
, tb
, blob_data(msg
), blob_len(msg
));
513 if (!tb
[UH_UBUS_SG_SID
])
514 return UBUS_STATUS_INVALID_ARGUMENT
;
516 ses
= uh_ubus_session_get(blobmsg_data(tb
[UH_UBUS_SG_SID
]));
518 return UBUS_STATUS_NOT_FOUND
;
520 if (!tb
[UH_UBUS_SG_KEYS
]) {
521 avl_remove_all_elements(&ses
->data
, data
, avl
, ndata
)
526 blobmsg_for_each_attr(attr
, tb
[UH_UBUS_SG_KEYS
], rem
) {
527 if (blob_id(attr
) != BLOBMSG_TYPE_STRING
)
530 data
= avl_find_element(&ses
->data
, blobmsg_data(attr
), data
, avl
);
534 avl_delete(&ses
->data
, &data
->avl
);
542 uh_ubus_handle_destroy(struct ubus_context
*ctx
, struct ubus_object
*obj
,
543 struct ubus_request_data
*req
, const char *method
,
544 struct blob_attr
*msg
)
546 struct uh_ubus_session
*ses
;
547 struct blob_attr
*tb
;
549 blobmsg_parse(&sid_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
552 return UBUS_STATUS_INVALID_ARGUMENT
;
554 ses
= uh_ubus_session_get(blobmsg_data(tb
));
556 return UBUS_STATUS_NOT_FOUND
;
558 uh_ubus_session_destroy(ses
);
563 bool uh_ubus_session_acl_allowed(struct uh_ubus_session
*ses
, const char *obj
, const char *fun
)
565 struct uh_ubus_session_acl
*acl
;
567 uh_foreach_matching_acl(acl
, ses
, obj
, fun
)
573 int ubus_session_api_init(struct ubus_context
*ctx
)
575 static const struct ubus_method session_methods
[] = {
576 UBUS_METHOD("create", uh_ubus_handle_create
, &new_policy
),
577 UBUS_METHOD("list", uh_ubus_handle_list
, &sid_policy
),
578 UBUS_METHOD("grant", uh_ubus_handle_acl
, acl_policy
),
579 UBUS_METHOD("revoke", uh_ubus_handle_acl
, acl_policy
),
580 UBUS_METHOD("set", uh_ubus_handle_set
, set_policy
),
581 UBUS_METHOD("get", uh_ubus_handle_get
, get_policy
),
582 UBUS_METHOD("unset", uh_ubus_handle_unset
, get_policy
),
583 UBUS_METHOD("destroy", uh_ubus_handle_destroy
, &sid_policy
),
586 static struct ubus_object_type session_type
=
587 UBUS_OBJECT_TYPE("uhttpd", session_methods
);
589 static struct ubus_object obj
= {
591 .type
= &session_type
,
592 .methods
= session_methods
,
593 .n_methods
= ARRAY_SIZE(session_methods
),
596 avl_init(&sessions
, avl_strcmp
, false, NULL
);
598 return ubus_add_object(ctx
, &obj
);