afb-export: refactor of binder interface
[src/app-framework-binder.git] / src / afb-api-so-v2.c
1 /*
2  * Copyright (C) 2016, 2017 "IoT.bzh"
3  * Author José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define _GNU_SOURCE
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <dlfcn.h>
23 #include <assert.h>
24
25 #include <afb/afb-binding-v2.h>
26 #include <json-c/json.h>
27
28 #include "afb-api.h"
29 #include "afb-api-so-v2.h"
30 #include "afb-apiset.h"
31 #include "afb-export.h"
32 #include "afb-evt.h"
33 #include "afb-common.h"
34 #include "afb-context.h"
35 #include "afb-api-so.h"
36 #include "afb-xreq.h"
37 #include "jobs.h"
38 #include "verbose.h"
39
40 /*
41  * names of symbols
42  */
43 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
44 static const char afb_api_so_v2_data[] = "afbBindingV2data";
45
46 /*
47  * Description of a binding
48  */
49 struct api_so_v2 {
50         const struct afb_binding_v2 *binding;   /* descriptor */
51         void *handle;                           /* context of dlopen */
52         struct afb_export *export;              /* exportations */
53 };
54
55 static const struct afb_verb_v2 *search(struct api_so_v2 *desc, const char *name)
56 {
57         const struct afb_verb_v2 *verb;
58
59         verb = desc->binding->verbs;
60         while (verb->verb && strcasecmp(verb->verb, name))
61                 verb++;
62         return verb->verb ? verb : NULL;
63         return NULL;
64 }
65
66 static void call_cb(void *closure, struct afb_xreq *xreq)
67 {
68         struct api_so_v2 *desc = closure;
69         const struct afb_verb_v2 *verb;
70
71         verb = search(desc, xreq->verb);
72         afb_xreq_call_verb_v2(xreq, verb);
73 }
74
75 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
76 {
77         int rc;
78         int (*start)();
79         void (*onevent)(const char *event, struct json_object *object);
80
81         struct api_so_v2 *desc = closure;
82
83         /* check state */
84         if (afb_export_is_started(desc->export)) {
85                 /* not an error when onneed */
86                 if (onneed != 0)
87                         goto done;
88
89                 /* already started: it is an error */
90                 ERROR("Service %s already started", afb_export_apiname(desc->export));
91                 return -1;
92         }
93
94         /* get the initialisation */
95         start = desc->binding->init;
96         onevent = desc->binding->onevent;
97
98         /* unshare the session if asked */
99         if (!share_session) {
100                 rc = afb_export_unshare_session(desc->export);
101                 if (rc < 0) {
102                         ERROR("Can't unshare the session for %s", afb_export_apiname(desc->export));
103                         return -1;
104                 }
105         }
106
107         /* set event handling */
108         rc = afb_export_handle_events(desc->export, onevent);
109         if (rc < 0) {
110                 ERROR("Can't set event handler for %s", afb_export_apiname(desc->export));
111                 return -1;
112         }
113
114         /* Starts the service */
115         rc = afb_export_start_v2(desc->export, start);
116         if (rc < 0) {
117                 /* initialisation error */
118                 ERROR("Initialisation of service %s failed (%d): %m", afb_export_apiname(desc->export), rc);
119                 return rc;
120         }
121
122 done:
123         return 0;
124 }
125
126 static void update_hooks_cb(void *closure)
127 {
128         struct api_so_v2 *desc = closure;
129         afb_export_update_hook(desc->export);
130 }
131
132 static int get_verbosity_cb(void *closure)
133 {
134         struct api_so_v2 *desc = closure;
135         return afb_export_verbosity_get(desc->export);
136 }
137
138 static void set_verbosity_cb(void *closure, int level)
139 {
140         struct api_so_v2 *desc = closure;
141         afb_export_verbosity_set(desc->export, level);
142 }
143
144 static struct json_object *addperm(struct json_object *o, struct json_object *x)
145 {
146         struct json_object *a;
147
148         if (!o)
149                 return x;
150
151         if (!json_object_object_get_ex(o, "allOf", &a)) {
152                 a = json_object_new_array();
153                 json_object_array_add(a, o);
154                 o = json_object_new_object();
155                 json_object_object_add(o, "allOf", a);
156         }
157         json_object_array_add(a, x);
158         return o;
159 }
160
161 static struct json_object *addperm_key_val(struct json_object *o, const char *key, struct json_object *val)
162 {
163         struct json_object *x = json_object_new_object();
164         json_object_object_add(x, key, val);
165         return addperm(o, x);
166 }
167
168 static struct json_object *addperm_key_valstr(struct json_object *o, const char *key, const char *val)
169 {
170         return addperm_key_val(o, key, json_object_new_string(val));
171 }
172
173 static struct json_object *addperm_key_valint(struct json_object *o, const char *key, int val)
174 {
175         return addperm_key_val(o, key, json_object_new_int(val));
176 }
177
178 static struct json_object *addauth_or_array(struct json_object *o, const struct afb_auth *auth);
179
180 static struct json_object *addauth(struct json_object *o, const struct afb_auth *auth)
181 {
182         switch(auth->type) {
183         case afb_auth_No: return addperm(o, json_object_new_boolean(0));
184         case afb_auth_Token: return addperm_key_valstr(o, "session", "check");
185         case afb_auth_LOA: return addperm_key_valint(o, "LOA", auth->loa);
186         case afb_auth_Permission: return addperm_key_valstr(o, "permission", auth->text);
187         case afb_auth_Or: return addperm_key_val(o, "anyOf", addauth_or_array(json_object_new_array(), auth));
188         case afb_auth_And: return addauth(addauth(o, auth->first), auth->next);
189         case afb_auth_Not: return addperm_key_val(o, "not", addauth(NULL, auth->first));
190         case afb_auth_Yes: return addperm(o, json_object_new_boolean(1));
191         }
192         return o;
193 }
194
195 static struct json_object *addauth_or_array(struct json_object *o, const struct afb_auth *auth)
196 {
197         if (auth->type != afb_auth_Or)
198                 json_object_array_add(o, addauth(NULL, auth));
199         else {
200                 addauth_or_array(o, auth->first);
201                 addauth_or_array(o, auth->next);
202         }
203
204         return o;
205 }
206
207 static struct json_object *make_description_openAPIv3(struct api_so_v2 *desc)
208 {
209         char buffer[256];
210         const struct afb_verb_v2 *verb;
211         struct json_object *r, *f, *a, *i, *p, *g;
212
213         r = json_object_new_object();
214         json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
215
216         i = json_object_new_object();
217         json_object_object_add(r, "info", i);
218         json_object_object_add(i, "title", json_object_new_string(afb_export_apiname(desc->export)));
219         json_object_object_add(i, "version", json_object_new_string("0.0.0"));
220         json_object_object_add(i, "description", json_object_new_string(desc->binding->info ?: afb_export_apiname(desc->export)));
221
222         p = json_object_new_object();
223         json_object_object_add(r, "paths", p);
224         verb = desc->binding->verbs;
225         while (verb->verb) {
226                 buffer[0] = '/';
227                 strncpy(buffer + 1, verb->verb, sizeof buffer - 1);
228                 buffer[sizeof buffer - 1] = 0;
229                 f = json_object_new_object();
230                 json_object_object_add(p, buffer, f);
231                 g = json_object_new_object();
232                 json_object_object_add(f, "get", g);
233
234                 a = NULL;
235                 if (verb->session & AFB_SESSION_CLOSE_V2)
236                         a = addperm_key_valstr(a, "session", "close");
237                 if (verb->session & AFB_SESSION_CHECK_V2)
238                         a = addperm_key_valstr(a, "session", "check");
239                 if (verb->session & AFB_SESSION_REFRESH_V2)
240                         a = addperm_key_valstr(a, "token", "refresh");
241                 if (verb->session & AFB_SESSION_LOA_MASK_V2)
242                         a = addperm_key_valint(a, "LOA", verb->session & AFB_SESSION_LOA_MASK_V2);
243                 if (verb->auth)
244                         a = addauth(a, verb->auth);
245                 if (a)
246                         json_object_object_add(g, "x-permissions", a);
247
248                 a = json_object_new_object();
249                 json_object_object_add(g, "responses", a);
250                 f = json_object_new_object();
251                 json_object_object_add(a, "200", f);
252                 json_object_object_add(f, "description", json_object_new_string(verb->info?:verb->verb));
253                 verb++;
254         }
255         return r;
256 }
257
258 static struct json_object *describe_cb(void *closure)
259 {
260         struct api_so_v2 *desc = closure;
261         struct json_object *r = desc->binding->specification ? json_tokener_parse(desc->binding->specification) : NULL;
262         if (!r)
263                 r = make_description_openAPIv3(desc);
264         return r;
265 }
266
267 static struct afb_api_itf so_v2_api_itf = {
268         .call = call_cb,
269         .service_start = service_start_cb,
270         .update_hooks = update_hooks_cb,
271         .get_verbosity = get_verbosity_cb,
272         .set_verbosity = set_verbosity_cb,
273         .describe = describe_cb
274 };
275
276 int afb_api_so_v2_add_binding(const struct afb_binding_v2 *binding, void *handle, struct afb_apiset *apiset, struct afb_binding_data_v2 *data)
277 {
278         int rc;
279         struct api_so_v2 *desc;
280         struct afb_api afb_api;
281         struct afb_export *export;
282
283         /* basic checks */
284         assert(binding);
285         assert(binding->api);
286         assert(binding->verbs);
287         assert(data);
288
289         /* allocates the description */
290         export = afb_export_create_v2(binding->api, data);
291         desc = calloc(1, sizeof *desc);
292         if (desc == NULL) {
293                 ERROR("out of memory");
294                 goto error;
295         }
296         desc->binding = binding;
297         desc->handle = handle;
298         desc->export = export;
299
300         /* init the binding */
301         if (binding->preinit) {
302                 INFO("binding %s calling preinit function", binding->api);
303                 rc = binding->preinit();
304                 if (rc < 0) {
305                         ERROR("binding %s preinit function failed...", afb_export_apiname(desc->export));
306                         goto error;
307                 }
308         }
309
310         /* records the binding */
311         afb_api.closure = desc;
312         afb_api.itf = &so_v2_api_itf;
313         afb_api.noconcurrency = binding->noconcurrency;
314         if (afb_apiset_add(apiset, afb_export_apiname(desc->export), afb_api) < 0) {
315                 ERROR("binding %s can't be registered to set %s...", afb_export_apiname(desc->export), afb_apiset_name(apiset));
316                 goto error;
317         }
318         INFO("binding %s added to set %s", afb_export_apiname(desc->export), afb_apiset_name(apiset));
319         return 1;
320
321 error:
322         afb_export_destroy(export);
323         free(desc);
324
325         return -1;
326 }
327
328 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *apiset)
329 {
330         const struct afb_binding_v2 *binding;
331         struct afb_binding_data_v2 *data;
332
333         /* retrieves the register function */
334         binding = dlsym(handle, afb_api_so_v2_descriptor);
335         data = dlsym(handle, afb_api_so_v2_data);
336         if (!binding && !data)
337                 return 0;
338
339         INFO("binding [%s] looks like an AFB binding V2", path);
340
341         /* basic checks */
342         if (!binding || !data) {
343                 ERROR("binding [%s] incomplete symbol set: %s is missing",
344                         path, binding ? afb_api_so_v2_data : afb_api_so_v2_descriptor);
345                 goto error;
346         }
347         if (binding->api == NULL || *binding->api == 0) {
348                 ERROR("binding [%s] bad api name...", path);
349                 goto error;
350         }
351         if (!afb_api_is_valid_name(binding->api)) {
352                 ERROR("binding [%s] invalid api name...", path);
353                 goto error;
354         }
355 #if 0
356         if (binding->specification == NULL || *binding->specification == 0) {
357                 ERROR("binding [%s] bad specification...", path);
358                 goto error;
359         }
360 #endif
361         if (binding->verbs == NULL) {
362                 ERROR("binding [%s] no verbs...", path);
363                 goto error;
364         }
365
366         return afb_api_so_v2_add_binding(binding, handle, apiset, data);
367
368  error:
369         return -1;
370 }
371