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