ucode: initialize module search path early
[project/rpcd.git] / ucode.c
1 /*
2 * rpcd - UBUS RPC server - ucode plugin
3 *
4 * Copyright (C) 2021 Jo-Philipp Wich <jo@mein.io>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <dirent.h>
22 #include <limits.h>
23
24 #include <libubox/blobmsg.h>
25 #include <libubox/blobmsg_json.h>
26
27 #include <libubus.h>
28
29 #include <ucode/compiler.h>
30 #include <ucode/lib.h>
31 #include <ucode/vm.h>
32
33 #include <rpcd/plugin.h>
34
35 #define RPC_UCSCRIPT_DIRECTORY INSTALL_PREFIX "/share/rpcd/ucode"
36
37 static struct blob_buf buf;
38 static int request_timeout;
39
40 /*
41 * Track script instances and registered ubus objects in these lists.
42 *
43 * This is primarily done to make Valgrind happy and to mark the
44 * related memory as reachable. Since we don't have a teardown
45 * mechanism in rpcd plugins we can't orderly free the related
46 * ubus object and ucode VM memory anyway.
47 */
48 static LIST_HEAD(scripts);
49 static LIST_HEAD(uuobjs);
50
51 typedef struct {
52 struct list_head list;
53 uc_vm_t vm;
54 uc_resource_type_t *requesttype;
55 uc_value_t *pending_replies;
56 char *path;
57 } rpc_ucode_script_t;
58
59 typedef struct {
60 struct list_head list;
61 rpc_ucode_script_t *script;
62 uc_value_t *signature;
63 struct ubus_object ubusobj;
64 } rpc_ucode_ubus_obj_t;
65
66 typedef struct {
67 struct ubus_context *ubus;
68 struct ubus_request_data req;
69 struct uloop_timeout timeout;
70 rpc_ucode_script_t *script;
71 uc_value_t *func;
72 uc_value_t *args;
73 uc_value_t *info;
74 bool replied;
75 } rpc_ucode_call_ctx_t;
76
77 static uc_parse_config_t config = {
78 .strict_declarations = false,
79 .lstrip_blocks = true,
80 .trim_blocks = true,
81 .raw_mode = true
82 };
83
84
85 static rpc_ucode_script_t *
86 rpc_ucode_obj_to_script(struct ubus_object *obj)
87 {
88 rpc_ucode_ubus_obj_t *uo = container_of(obj, rpc_ucode_ubus_obj_t, ubusobj);
89
90 return uo->script;
91 }
92
93 static uc_value_t *
94 rpc_ucode_obj_to_signature(struct ubus_object *obj)
95 {
96 rpc_ucode_ubus_obj_t *uo = container_of(obj, rpc_ucode_ubus_obj_t, ubusobj);
97
98 return uo->signature;
99 }
100
101 static void
102 rpc_ucode_ucv_array_to_blob(uc_value_t *val, struct blob_buf *blob);
103
104 static void
105 rpc_ucode_ucv_object_to_blob(uc_value_t *val, struct blob_buf *blob);
106
107 static void
108 rpc_ucode_ucv_to_blob(const char *name, uc_value_t *val, struct blob_buf *blob)
109 {
110 int64_t n;
111 void *c;
112
113 switch (ucv_type(val)) {
114 case UC_NULL:
115 blobmsg_add_field(blob, BLOBMSG_TYPE_UNSPEC, name, NULL, 0);
116 break;
117
118 case UC_BOOLEAN:
119 blobmsg_add_u8(blob, name, ucv_boolean_get(val));
120 break;
121
122 case UC_INTEGER:
123 n = ucv_int64_get(val);
124
125 if (errno == ERANGE)
126 blobmsg_add_u64(blob, name, ucv_uint64_get(val));
127 else if (n >= INT32_MIN && n <= INT32_MAX)
128 blobmsg_add_u32(blob, name, n);
129 else
130 blobmsg_add_u64(blob, name, n);
131
132 break;
133
134 case UC_DOUBLE:
135 blobmsg_add_double(blob, name, ucv_double_get(val));
136 break;
137
138 case UC_STRING:
139 blobmsg_add_string(blob, name, ucv_string_get(val));
140 break;
141
142 case UC_ARRAY:
143 c = blobmsg_open_array(blob, name);
144 rpc_ucode_ucv_array_to_blob(val, blob);
145 blobmsg_close_array(blob, c);
146 break;
147
148 case UC_OBJECT:
149 c = blobmsg_open_table(blob, name);
150 rpc_ucode_ucv_object_to_blob(val, blob);
151 blobmsg_close_table(blob, c);
152 break;
153
154 default:
155 break;
156 }
157 }
158
159 static void
160 rpc_ucode_ucv_array_to_blob(uc_value_t *val, struct blob_buf *blob)
161 {
162 size_t i;
163
164 for (i = 0; i < ucv_array_length(val); i++)
165 rpc_ucode_ucv_to_blob(NULL, ucv_array_get(val, i), blob);
166 }
167
168 static void
169 rpc_ucode_ucv_object_to_blob(uc_value_t *val, struct blob_buf *blob)
170 {
171 ucv_object_foreach(val, k, v)
172 rpc_ucode_ucv_to_blob(k, v, blob);
173 }
174
175 static uc_value_t *
176 rpc_ucode_blob_to_ucv(uc_vm_t *vm, struct blob_attr *attr, bool table, const char **name);
177
178 static uc_value_t *
179 rpc_ucode_blob_array_to_ucv(uc_vm_t *vm, struct blob_attr *attr, size_t len, bool table)
180 {
181 uc_value_t *o = table ? ucv_object_new(vm) : ucv_array_new(vm);
182 uc_value_t *v;
183 struct blob_attr *pos;
184 size_t rem = len;
185 const char *name;
186
187 if (!o)
188 return NULL;
189
190 __blob_for_each_attr(pos, attr, rem) {
191 name = NULL;
192 v = rpc_ucode_blob_to_ucv(vm, pos, table, &name);
193
194 if (table && name)
195 ucv_object_add(o, name, v);
196 else if (!table)
197 ucv_array_push(o, v);
198 else
199 ucv_put(v);
200 }
201
202 return o;
203 }
204
205 static uc_value_t *
206 rpc_ucode_blob_to_ucv(uc_vm_t *vm, struct blob_attr *attr, bool table, const char **name)
207 {
208 void *data;
209 int len;
210
211 if (!blobmsg_check_attr(attr, false))
212 return NULL;
213
214 if (table && blobmsg_name(attr)[0])
215 *name = blobmsg_name(attr);
216
217 data = blobmsg_data(attr);
218 len = blobmsg_data_len(attr);
219
220 switch (blob_id(attr)) {
221 case BLOBMSG_TYPE_BOOL:
222 return ucv_boolean_new(*(uint8_t *)data);
223
224 case BLOBMSG_TYPE_INT16:
225 return ucv_int64_new((int16_t)be16_to_cpu(*(uint16_t *)data));
226
227 case BLOBMSG_TYPE_INT32:
228 return ucv_int64_new((int32_t)be32_to_cpu(*(uint32_t *)data));
229
230 case BLOBMSG_TYPE_INT64:
231 return ucv_int64_new((int64_t)be64_to_cpu(*(uint64_t *)data));
232
233 case BLOBMSG_TYPE_DOUBLE:
234 ;
235 union {
236 double d;
237 uint64_t u64;
238 } v;
239
240 v.u64 = be64_to_cpu(*(uint64_t *)data);
241
242 return ucv_double_new(v.d);
243
244 case BLOBMSG_TYPE_STRING:
245 return ucv_string_new(data);
246
247 case BLOBMSG_TYPE_ARRAY:
248 return rpc_ucode_blob_array_to_ucv(vm, data, len, false);
249
250 case BLOBMSG_TYPE_TABLE:
251 return rpc_ucode_blob_array_to_ucv(vm, data, len, true);
252
253 default:
254 return NULL;
255 }
256 }
257
258 static int
259 rpc_ucode_validate_call_args(struct ubus_object *obj, const char *ubus_method_name, struct blob_attr *msg, uc_value_t **res)
260 {
261 rpc_ucode_script_t *script = rpc_ucode_obj_to_script(obj);
262 const struct ubus_method *method = NULL;
263 const struct blobmsg_hdr *hdr;
264 struct blob_attr *attr;
265 bool found;
266 size_t i;
267 int len;
268
269 for (i = 0; i < obj->n_methods; i++) {
270 if (!strcmp(obj->methods[i].name, ubus_method_name)) {
271 method = &obj->methods[i];
272 break;
273 }
274 }
275
276 if (!method)
277 return UBUS_STATUS_METHOD_NOT_FOUND;
278
279 len = blob_len(msg);
280
281 __blob_for_each_attr(attr, blob_data(msg), len) {
282 if (!blobmsg_check_attr_len(attr, false, len))
283 return UBUS_STATUS_INVALID_ARGUMENT;
284
285 if (!blob_is_extended(attr))
286 return UBUS_STATUS_INVALID_ARGUMENT;
287
288 hdr = blob_data(attr);
289 found = false;
290
291 for (i = 0; i < method->n_policy; i++) {
292 if (blobmsg_namelen(hdr) != strlen(method->policy[i].name))
293 continue;
294
295 if (strcmp(method->policy[i].name, (char *)hdr->name))
296 continue;
297
298 /* named argument found but wrong type */
299 if (blob_id(attr) != method->policy[i].type)
300 goto inval;
301
302 found = true;
303 break;
304 }
305
306 /* named argument not found in policy */
307 if (!found)
308 goto inval;
309 }
310
311 *res = rpc_ucode_blob_array_to_ucv(&script->vm, blob_data(msg), blob_len(msg), true);
312
313 return UBUS_STATUS_OK;
314
315 inval:
316 *res = NULL;
317
318 return UBUS_STATUS_INVALID_ARGUMENT;
319 }
320
321 static uc_value_t *
322 rpc_ucode_gather_call_info(uc_vm_t *vm,
323 struct ubus_context *ctx, struct ubus_request_data *req,
324 struct ubus_object *obj, const char *ubus_method_name)
325 {
326 uc_value_t *info, *o;
327
328 info = ucv_object_new(vm);
329
330 o = ucv_object_new(vm);
331
332 ucv_object_add(o, "user", ucv_string_new(req->acl.user));
333 ucv_object_add(o, "group", ucv_string_new(req->acl.group));
334 ucv_object_add(o, "object", ucv_string_new(req->acl.object));
335
336 ucv_object_add(info, "acl", o);
337
338 o = ucv_object_new(vm);
339
340 ucv_object_add(o, "id", ucv_uint64_new(obj->id));
341 ucv_object_add(o, "name", ucv_string_new(obj->name));
342
343 if (obj->path)
344 ucv_object_add(o, "path", ucv_string_new(obj->path));
345
346 ucv_object_add(info, "object", o);
347
348 ucv_object_add(info, "method", ucv_string_new(ubus_method_name));
349
350 return info;
351 }
352
353 static void
354 rpc_ucode_request_finish(rpc_ucode_call_ctx_t *callctx, int code, uc_value_t *reply)
355 {
356 rpc_ucode_script_t *script = callctx->script;
357 uc_resource_t *r;
358 size_t i;
359
360 if (callctx->replied)
361 return;
362
363 if (reply) {
364 blob_buf_init(&buf, 0);
365 rpc_ucode_ucv_object_to_blob(reply, &buf);
366 ubus_send_reply(callctx->ubus, &callctx->req, buf.head);
367 }
368
369 ubus_complete_deferred_request(callctx->ubus, &callctx->req, code);
370
371 callctx->replied = true;
372
373 for (i = 0; i < ucv_array_length(script->pending_replies); i++) {
374 r = (uc_resource_t *)ucv_array_get(script->pending_replies, i);
375
376 if (r && r->data == callctx) {
377 ucv_array_set(script->pending_replies, i, NULL);
378 break;
379 }
380 }
381 }
382
383 static void
384 rpc_ucode_request_timeout(struct uloop_timeout *timeout)
385 {
386 rpc_ucode_call_ctx_t *callctx = container_of(timeout, rpc_ucode_call_ctx_t, timeout);
387
388 rpc_ucode_request_finish(callctx, UBUS_STATUS_TIMEOUT, NULL);
389 }
390
391 static int
392 rpc_ucode_script_call(struct ubus_context *ctx, struct ubus_object *obj,
393 struct ubus_request_data *req, const char *ubus_method_name,
394 struct blob_attr *msg)
395 {
396 rpc_ucode_script_t *script = rpc_ucode_obj_to_script(obj);
397 uc_value_t *func, *args = NULL, *reqobj, *reqproto, *res;
398 rpc_ucode_call_ctx_t *callctx;
399 size_t i;
400 int rv;
401
402 rv = rpc_ucode_validate_call_args(obj, ubus_method_name, msg, &args);
403
404 if (rv != UBUS_STATUS_OK)
405 return rv;
406
407 func = ucv_object_get(
408 ucv_object_get(rpc_ucode_obj_to_signature(obj), ubus_method_name, NULL),
409 "call", NULL
410 );
411
412 if (!ucv_is_callable(func))
413 return UBUS_STATUS_METHOD_NOT_FOUND;
414
415 /* allocate deferred method call context */
416 callctx = calloc(1, sizeof(*callctx));
417
418 if (!callctx)
419 return UBUS_STATUS_UNKNOWN_ERROR;
420
421 callctx->ubus = ctx;
422 callctx->script = script;
423
424 ubus_defer_request(ctx, req, &callctx->req);
425
426 /* create ucode request type object and set properties */
427 reqobj = uc_resource_new(script->requesttype, callctx);
428 reqproto = ucv_object_new(&script->vm);
429
430 ucv_object_add(reqproto, "args", args);
431 ucv_object_add(reqproto, "info",
432 rpc_ucode_gather_call_info(&script->vm, ctx, req, obj, ubus_method_name));
433
434 ucv_prototype_set(ucv_prototype_get(reqobj), reqproto);
435
436 /* push handler and request object onto stack */
437 uc_vm_stack_push(&script->vm, ucv_get(func));
438 uc_vm_stack_push(&script->vm, ucv_get(reqobj));
439
440 /* execute request handler function */
441 switch (uc_vm_call(&script->vm, false, 1)) {
442 case EXCEPTION_NONE:
443 res = uc_vm_stack_pop(&script->vm);
444
445 /* The handler function invoked a nested aync ubus request and returned it */
446 if (ucv_resource_dataptr(res, "ubus.deferred")) {
447 /* Install guard timer in case the reply callback is never called */
448 callctx->timeout.cb = rpc_ucode_request_timeout;
449 uloop_timeout_set(&callctx->timeout, request_timeout);
450
451 /* Add wrapped request context into registry to prevent GC'ing
452 * until reply or timeout occurred */
453 for (i = 0;; i++) {
454 if (ucv_array_get(script->pending_replies, i) == NULL) {
455 ucv_array_set(script->pending_replies, i, ucv_get(reqobj));
456 break;
457 }
458 }
459 }
460
461 /* Otherwise, when the function returned an object, treat it as
462 * reply data and conclude deferred request immediately */
463 else if (ucv_type(res) == UC_OBJECT) {
464 blob_buf_init(&buf, 0);
465 rpc_ucode_ucv_object_to_blob(res, &buf);
466 ubus_send_reply(ctx, &callctx->req, buf.head);
467
468 ubus_complete_deferred_request(ctx, &callctx->req, UBUS_STATUS_OK);
469 callctx->replied = true;
470 }
471
472 /* If neither a deferred ubus request, nor a plain object were
473 * returned and if reqobj.reply() hasn't been called, immediately
474 * finish deferred request with UBUS_STATUS_NO_DATA. The */
475 else if (!callctx->replied) {
476 ubus_complete_deferred_request(ctx, &callctx->req, UBUS_STATUS_NO_DATA);
477 callctx->replied = true;
478 }
479
480 ucv_put(res);
481 break;
482
483 /* if the handler function invoked exit(), forward exit status as ubus
484 * return code, map out of range values to UBUS_STATUS_UNKNOWN_ERROR. */
485 case EXCEPTION_EXIT:
486 rv = script->vm.arg.s32;
487
488 if (rv < UBUS_STATUS_OK || rv >= __UBUS_STATUS_LAST)
489 rv = UBUS_STATUS_UNKNOWN_ERROR;
490
491 ubus_complete_deferred_request(ctx, &callctx->req, rv);
492 callctx->replied = true;
493 break;
494
495 /* treat other exceptions as unknown error */
496 default:
497 ubus_complete_deferred_request(ctx, &callctx->req, UBUS_STATUS_UNKNOWN_ERROR);
498 callctx->replied = true;
499 break;
500 }
501
502 /* release request object */
503 ucv_put(reqobj);
504
505 /* garbage collect */
506 ucv_gc(&script->vm);
507
508 return UBUS_STATUS_OK;
509 }
510
511 static uc_program_t *
512 rpc_ucode_script_compile(const char *path, uc_source_t *src)
513 {
514 char *syntax_error = NULL;
515 uc_program_t *prog;
516
517 prog = uc_compile(&config, src, &syntax_error);
518
519 if (!prog)
520 fprintf(stderr, "Unable to compile ucode script %s: %s\n",
521 path, syntax_error);
522
523 uc_source_put(src);
524 free(syntax_error);
525
526 return prog;
527 }
528
529 static bool
530 rpc_ucode_script_validate(rpc_ucode_script_t *script)
531 {
532 uc_value_t *signature = uc_vm_registry_get(&script->vm, "rpcd.ucode.signature");
533 uc_value_t *args, *func;
534
535 if (ucv_type(signature) != UC_OBJECT) {
536 fprintf(stderr, "Invalid object signature for ucode script %s"
537 " - expected dictionary, got %s\n",
538 script->path, ucv_typename(signature));
539
540 return false;
541 }
542
543 ucv_object_foreach(signature, ubus_object_name, ubus_object_methods) {
544 if (ucv_type(ubus_object_methods) != UC_OBJECT) {
545 fprintf(stderr, "Invalid method signature for ucode script %s, object %s"
546 " - expected dictionary, got %s\n",
547 script->path, ubus_object_name, ucv_typename(ubus_object_methods));
548
549 return false;
550 }
551
552 ucv_object_foreach(ubus_object_methods, ubus_method_name, ubus_method_definition) {
553 func = ucv_object_get(ubus_method_definition, "call", NULL);
554 args = ucv_object_get(ubus_method_definition, "args", NULL);
555
556 if (ucv_type(ubus_method_definition) != UC_OBJECT) {
557 fprintf(stderr, "Invalid method definition for ucode script %s, object %s, method %s"
558 " - expected dictionary, got %s\n",
559 script->path, ubus_object_name, ubus_method_name, ucv_typename(ubus_method_definition));
560
561 return false;
562 }
563
564 if (!ucv_is_callable(func)) {
565 fprintf(stderr, "Invalid method callback for ucode script %s, object %s, method %s"
566 " - expected callable, got %s\n",
567 script->path, ubus_object_name, ubus_method_name, ucv_typename(func));
568
569 return false;
570 }
571
572 if (args) {
573 if (ucv_type(args) != UC_OBJECT) {
574 fprintf(stderr, "Invalid method argument definition for ucode script %s, "
575 "object %s, method %s - expected dictionary, got %s\n",
576 script->path, ubus_object_name, ubus_method_name, ucv_typename(args));
577
578 return false;
579 }
580
581 ucv_object_foreach(args, ubus_argument_name, ubus_argument_typehint) {
582 switch (ucv_type(ubus_argument_typehint)) {
583 case UC_BOOLEAN:
584 case UC_INTEGER:
585 case UC_DOUBLE:
586 case UC_STRING:
587 case UC_ARRAY:
588 case UC_OBJECT:
589 continue;
590
591 default:
592 fprintf(stderr, "Unsupported argument type for ucode script %s, object %s, "
593 "method %s, argument %s - expected boolean, integer, string, "
594 "array or object, got %s\n",
595 script->path, ubus_object_name, ubus_method_name, ubus_argument_name,
596 ucv_typename(ubus_argument_typehint));
597
598 return false;
599 }
600 }
601 }
602 }
603 }
604
605 return true;
606 }
607
608 static bool
609 rpc_ucode_method_register(struct ubus_method *method, const char *ubus_method_name, uc_value_t *ubus_method_arguments)
610 {
611 struct blobmsg_policy *policy;
612 enum blobmsg_type type;
613
614 method->name = strdup(ubus_method_name);
615
616 if (!method->name) {
617 fprintf(stderr, "Unable to allocate ubus method name: %s\n",
618 strerror(errno));
619
620 return false;
621 }
622
623 method->policy = calloc(ucv_object_length(ubus_method_arguments), sizeof(*method->policy));
624
625 if (!method->policy) {
626 fprintf(stderr, "Unable to allocate ubus method argument policy: %s\n",
627 strerror(errno));
628
629 return false;
630 }
631
632 method->handler = rpc_ucode_script_call;
633
634 ucv_object_foreach(ubus_method_arguments, ubus_argument_name, ubus_argument_typehint) {
635 switch (ucv_type(ubus_argument_typehint)) {
636 case UC_BOOLEAN:
637 type = BLOBMSG_TYPE_INT8;
638 break;
639
640 case UC_INTEGER:
641 switch (ucv_int64_get(ubus_argument_typehint)) {
642 case 8:
643 type = BLOBMSG_TYPE_INT8;
644 break;
645
646 case 16:
647 type = BLOBMSG_TYPE_INT16;
648 break;
649
650 case 64:
651 type = BLOBMSG_TYPE_INT64;
652 break;
653
654 default:
655 type = BLOBMSG_TYPE_INT32;
656 break;
657 }
658
659 break;
660
661 case UC_DOUBLE:
662 type = BLOBMSG_TYPE_DOUBLE;
663 break;
664
665 case UC_ARRAY:
666 type = BLOBMSG_TYPE_ARRAY;
667 break;
668
669 case UC_OBJECT:
670 type = BLOBMSG_TYPE_TABLE;
671 break;
672
673 default:
674 type = BLOBMSG_TYPE_STRING;
675 break;
676 }
677
678 policy = (struct blobmsg_policy *)&method->policy[method->n_policy++];
679
680 policy->type = type;
681 policy->name = strdup(ubus_argument_name);
682
683 if (!policy->name) {
684 fprintf(stderr, "Unable to allocate ubus method argument name: %s\n",
685 strerror(errno));
686
687 return false;
688 }
689 }
690
691 return true;
692 }
693
694 static bool
695 rpc_ucode_script_register(struct ubus_context *ctx, rpc_ucode_script_t *script)
696 {
697 uc_value_t *signature = uc_vm_registry_get(&script->vm, "rpcd.ucode.signature");
698 const struct blobmsg_policy *policy;
699 rpc_ucode_ubus_obj_t *uuobj = NULL;
700 char *tptr, *tnptr, *onptr, *mptr;
701 struct ubus_method *method;
702 struct ubus_object *obj;
703 size_t typelen, namelen;
704 uc_value_t *args;
705 int rv;
706
707 if (!rpc_ucode_script_validate(script))
708 return false;
709
710 ucv_object_foreach(signature, ubus_object_name, ubus_object_methods) {
711 namelen = strlen(ubus_object_name);
712 typelen = strlen("rpcd-plugin-ucode-") + namelen;
713
714 uuobj = calloc_a(sizeof(*uuobj),
715 &onptr, namelen + 1,
716 &mptr, ucv_object_length(ubus_object_methods) * sizeof(struct ubus_method),
717 &tptr, sizeof(struct ubus_object_type),
718 &tnptr, typelen + 1);
719
720 if (!uuobj) {
721 fprintf(stderr, "Unable to allocate ubus object signature: %s\n",
722 strerror(errno));
723
724 continue;
725 }
726
727 list_add(&uuobj->list, &uuobjs);
728
729 uuobj->script = script;
730 uuobj->signature = ubus_object_methods;
731
732 snprintf(tnptr, typelen, "rpcd-plugin-ucode-%s", ubus_object_name);
733
734 method = (struct ubus_method *)mptr;
735
736 obj = &uuobj->ubusobj;
737 obj->name = strncpy(onptr, ubus_object_name, namelen);
738 obj->methods = method;
739
740 obj->type = (struct ubus_object_type *)tptr;
741 obj->type->name = tnptr;
742 obj->type->methods = obj->methods;
743
744 ucv_object_foreach(ubus_object_methods, ubus_method_name, ubus_method_definition) {
745 args = ucv_object_get(ubus_method_definition, "args", NULL);
746
747 if (!rpc_ucode_method_register(&method[obj->n_methods++], ubus_method_name, args))
748 goto free;
749 }
750
751 obj->type = (struct ubus_object_type *)tptr;
752 obj->type->name = tnptr;
753 obj->type->methods = obj->methods;
754 obj->type->n_methods = obj->n_methods;
755
756 rv = ubus_add_object(ctx, obj);
757
758 if (rv != UBUS_STATUS_OK) {
759 fprintf(stderr, "Unable to register ubus object %s: %s\n",
760 obj->name, ubus_strerror(rv));
761
762 goto free;
763 }
764
765 continue;
766
767 free:
768 for (; obj->n_methods > 0; method++, obj->n_methods--) {
769 for (policy = method->policy; method->n_policy > 0; policy++, method->n_policy--)
770 free((char *)policy->name);
771
772 free((char *)method->name);
773 free((char *)method->policy);
774 }
775
776 free(uuobj);
777 }
778
779 return true;
780 }
781
782 static uc_value_t *
783 rpc_ucode_request_reply(uc_vm_t *vm, size_t nargs)
784 {
785 rpc_ucode_call_ctx_t **callctx = uc_fn_this("rpcd.ucode.request");
786 uc_value_t *reply = uc_fn_arg(0);
787 uc_value_t *rcode = uc_fn_arg(1);
788 int64_t code = UBUS_STATUS_OK;
789
790 if (!callctx || !*callctx) {
791 uc_vm_raise_exception(vm, EXCEPTION_RUNTIME,
792 "Attempt to invoke reply() on invalid self");
793
794 return NULL;
795 }
796 else if (reply && ucv_type(reply) != UC_OBJECT) {
797 uc_vm_raise_exception(vm, EXCEPTION_RUNTIME,
798 "First argument to reply() must be null or an object");
799
800 return NULL;
801 }
802 else if (rcode && ucv_type(rcode) != UC_INTEGER) {
803 uc_vm_raise_exception(vm, EXCEPTION_RUNTIME,
804 "Second argument to reply() must be null or an integer");
805
806 return NULL;
807 }
808
809 if ((*callctx)->replied) {
810 uc_vm_raise_exception(vm, EXCEPTION_RUNTIME,
811 "Reply has already been sent");
812
813 return NULL;
814 }
815
816 if (rcode) {
817 code = ucv_int64_get(rcode);
818
819 if (errno == ERANGE || code < 0 || code > __UBUS_STATUS_LAST)
820 code = UBUS_STATUS_UNKNOWN_ERROR;
821 }
822
823 rpc_ucode_request_finish(*callctx, code, reply);
824
825 return NULL;
826 }
827
828 static uc_value_t *
829 rpc_ucode_request_error(uc_vm_t *vm, size_t nargs)
830 {
831 rpc_ucode_call_ctx_t **callctx = uc_fn_this("rpcd.ucode.request");
832 uc_value_t *rcode = uc_fn_arg(0);
833 int64_t code;
834
835 if (!callctx || !*callctx) {
836 uc_vm_raise_exception(vm, EXCEPTION_RUNTIME,
837 "Attempt to invoke error() on invalid self");
838
839 return NULL;
840 }
841 else if (ucv_type(rcode) != UC_INTEGER) {
842 uc_vm_raise_exception(vm, EXCEPTION_RUNTIME,
843 "First argument to error() must be an integer");
844
845 return NULL;
846 }
847
848 if ((*callctx)->replied) {
849 uc_vm_raise_exception(vm, EXCEPTION_RUNTIME,
850 "Reply has already been sent");
851
852 return NULL;
853 }
854
855
856 code = ucv_int64_get(rcode);
857
858 if (errno == ERANGE || code < 0 || code > __UBUS_STATUS_LAST)
859 code = UBUS_STATUS_UNKNOWN_ERROR;
860
861 rpc_ucode_request_finish(*callctx, code, NULL);
862
863 return NULL;
864 }
865
866 static const uc_function_list_t rpc_ucode_request_fns[] = {
867 { "reply", rpc_ucode_request_reply },
868 { "error", rpc_ucode_request_error },
869 };
870
871 static void
872 rpc_ucode_request_gc(void *ud)
873 {
874 rpc_ucode_call_ctx_t *callctx = ud;
875
876 uloop_timeout_cancel(&callctx->timeout);
877 free(callctx);
878 }
879
880 static void
881 rpc_ucode_init_globals(rpc_ucode_script_t *script)
882 {
883 uc_vm_t *vm = &script->vm;
884 uc_value_t *scope = uc_vm_scope_get(vm);
885
886 #define status_const(name) \
887 ucv_object_add(scope, #name, ucv_uint64_new(name))
888
889 status_const(UBUS_STATUS_OK);
890 status_const(UBUS_STATUS_INVALID_COMMAND);
891 status_const(UBUS_STATUS_INVALID_ARGUMENT);
892 status_const(UBUS_STATUS_METHOD_NOT_FOUND);
893 status_const(UBUS_STATUS_NOT_FOUND);
894 status_const(UBUS_STATUS_NO_DATA);
895 status_const(UBUS_STATUS_PERMISSION_DENIED);
896 status_const(UBUS_STATUS_TIMEOUT);
897 status_const(UBUS_STATUS_NOT_SUPPORTED);
898 status_const(UBUS_STATUS_UNKNOWN_ERROR);
899 status_const(UBUS_STATUS_CONNECTION_FAILED);
900
901 #undef status_const
902
903 uc_stdlib_load(scope);
904
905 script->requesttype = uc_type_declare(vm, "rpcd.ucode.request",
906 rpc_ucode_request_fns, rpc_ucode_request_gc);
907 }
908
909 static rpc_ucode_script_t *
910 rpc_ucode_script_execute(struct ubus_context *ctx, const char *path, uc_program_t *prog)
911 {
912 rpc_ucode_script_t *script;
913 uc_value_t *signature;
914 uc_vm_status_t status;
915 size_t pathlen;
916 char *pptr;
917
918 pathlen = strlen(path);
919 script = calloc_a(sizeof(*script), &pptr, pathlen + 1);
920
921 if (!script) {
922 fprintf(stderr, "Unable to allocate context for ucode script %s: %s\n",
923 path, strerror(errno));
924
925 uc_program_put(prog);
926
927 return NULL;
928 }
929
930 script->path = strncpy(pptr, path, pathlen);
931
932 uc_vm_init(&script->vm, &config);
933 rpc_ucode_init_globals(script);
934
935 status = uc_vm_execute(&script->vm, prog, &signature);
936
937 script->pending_replies = ucv_array_new(&script->vm);
938
939 uc_vm_registry_set(&script->vm, "rpcd.ucode.signature", signature);
940 uc_vm_registry_set(&script->vm, "rpcd.ucode.deferreds", script->pending_replies);
941
942 uc_program_put(prog);
943 ucv_gc(&script->vm);
944
945 switch (status) {
946 case STATUS_OK:
947 if (rpc_ucode_script_register(ctx, script))
948 return script;
949
950 fprintf(stderr, "Skipping registration of ucode script %s\n", path);
951 break;
952
953 case STATUS_EXIT:
954 fprintf(stderr, "The ucode script %s invoked exit(%" PRId64 ")\n",
955 path, ucv_int64_get(signature));
956 break;
957
958 case ERROR_COMPILE:
959 fprintf(stderr, "Compilation error while executing ucode script %s\n", path);
960 break;
961
962 case ERROR_RUNTIME:
963 fprintf(stderr, "Runtime error while executing ucode script %s\n", path);
964 break;
965 }
966
967 uc_vm_free(&script->vm);
968 free(script);
969
970 return NULL;
971 }
972
973 static int
974 rpc_ucode_init_script(struct ubus_context *ctx, const char *path)
975 {
976 rpc_ucode_script_t *script;
977 uc_program_t *prog;
978 uc_source_t *src;
979
980 src = uc_source_new_file(path);
981
982 if (!src) {
983 fprintf(stderr, "Unable to open ucode script %s: %s\n",
984 path, strerror(errno));
985
986 return UBUS_STATUS_UNKNOWN_ERROR;
987 }
988
989 prog = rpc_ucode_script_compile(path, src);
990
991 if (!prog)
992 return UBUS_STATUS_UNKNOWN_ERROR;
993
994 script = rpc_ucode_script_execute(ctx, path, prog);
995
996 if (!script)
997 return UBUS_STATUS_UNKNOWN_ERROR;
998
999 list_add(&script->list, &scripts);
1000
1001 return UBUS_STATUS_OK;
1002 }
1003
1004 static int
1005 rpc_ucode_api_init(const struct rpc_daemon_ops *ops, struct ubus_context *ctx)
1006 {
1007 char path[PATH_MAX];
1008 struct dirent *e;
1009 struct stat s;
1010 int rv = 0;
1011 DIR *d;
1012
1013 request_timeout = *ops->exec_timeout;
1014
1015 /* reopen ucode.so with RTLD_GLOBAL in order to export libucode runtime
1016 * symbols for ucode extensions loaded later at runtime */
1017 if (!dlopen(RPC_LIBRARY_DIRECTORY "/ucode.so", RTLD_LAZY|RTLD_GLOBAL)) {
1018 fprintf(stderr, "Failed to dlopen() ucode.so: %s, dynamic ucode plugins may fail\n",
1019 dlerror());
1020 }
1021
1022 /* initialize default module search path */
1023 uc_search_path_init(&config.module_search_path);
1024
1025 if ((d = opendir(RPC_UCSCRIPT_DIRECTORY)) != NULL) {
1026 while ((e = readdir(d)) != NULL) {
1027 snprintf(path, sizeof(path), RPC_UCSCRIPT_DIRECTORY "/%s", e->d_name);
1028
1029 if (stat(path, &s) || !S_ISREG(s.st_mode))
1030 continue;
1031
1032 if (s.st_mode & S_IWOTH) {
1033 fprintf(stderr, "Ignoring ucode script %s because it is world writable\n",
1034 path);
1035
1036 continue;
1037 }
1038
1039 rv |= rpc_ucode_init_script(ctx, path);
1040 }
1041
1042 closedir(d);
1043 }
1044
1045 return rv;
1046 }
1047
1048 struct rpc_plugin rpc_plugin = {
1049 .init = rpc_ucode_api_init
1050 };