Sort verbs and compute their permissions
[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 #define NO_BINDING_VERBOSE_MACRO
20
21 #include <string.h>
22 #include <dlfcn.h>
23 #include <assert.h>
24
25 #include <afb/afb-binding.h>
26 #include <json-c/json.h>
27
28 #include "afb-api.h"
29 #include "afb-apiset.h"
30 #include "afb-svc.h"
31 #include "afb-ditf.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 "afb-perm.h"
38 #include "verbose.h"
39
40 /*
41  * names of symbols
42  */
43 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
44
45 /**
46  * structure for memorizing verbs sorted with permissions
47  */
48 struct verb_v2 {
49         const struct afb_verb_v2 *verb;
50         struct afb_perm *perm;
51 };
52
53 /*
54  * Description of a binding
55  */
56 struct api_so_v2 {
57         const struct afb_binding_v2 *binding;   /* descriptor */
58         void *handle;                   /* context of dlopen */
59         struct afb_svc *service;        /* handler for service started */
60         struct afb_ditf ditf;           /* daemon interface */
61         int count;
62         struct verb_v2 verbs[1];
63 };
64
65 static const struct afb_verb_v2 *search(struct api_so_v2 *desc, const char *verb)
66 {
67         const struct afb_verb_v2 *v;
68         int i, l, u, c;
69
70         l = 0;
71         u = desc->count;
72         while (l < u) {
73                 i = (l + u) >> 1;
74                 v = desc->verbs[i].verb;
75                 c = strcasecmp(v->verb, verb);
76                 if (c == 0)
77                         return v;
78                 if (c < 0)
79                         l = i + 1;
80                 else
81                         u = i;
82         }
83         return NULL;
84 }
85
86 static void call_cb(void *closure, struct afb_xreq *xreq)
87 {
88         struct api_so_v2 *desc = closure;
89         const struct afb_verb_v2 *verb;
90
91         verb = search(desc, xreq->verb);
92         if (!verb)
93                 afb_xreq_fail_unknown_verb(xreq);
94         else
95                 if (!xreq_session_check_apply(xreq, verb->session))
96                         afb_xreq_call(xreq, verb->callback);
97 }
98
99 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
100 {
101         int (*start)(const struct afb_binding_interface *interface, struct afb_service service);
102         void (*onevent)(const char *event, struct json_object *object);
103
104         struct api_so_v2 *desc = closure;
105
106         /* check state */
107         if (desc->service != NULL) {
108                 /* not an error when onneed */
109                 if (onneed != 0)
110                         return 0;
111
112                 /* already started: it is an error */
113                 ERROR("Service %s already started", desc->binding->api);
114                 return -1;
115         }
116
117         /* get the initialisation */
118         start = desc->binding->start;
119         if (start == NULL) {
120                 /* not an error when onneed */
121                 if (onneed != 0)
122                         return 0;
123
124                 /* no initialisation method */
125                 ERROR("Binding %s is not a service", desc->binding->api);
126                 return -1;
127         }
128
129         /* get the event handler if any */
130         onevent = desc->binding->onevent;
131         desc->service = afb_svc_create_v2(apiset, share_session, onevent, start, &desc->ditf.interface);
132         if (desc->service == NULL) {
133                 /* starting error */
134                 ERROR("Starting service %s failed", desc->binding->api);
135                 return -1;
136         }
137
138         return 0;
139 }
140
141 static void update_hooks_cb(void *closure)
142 {
143         struct api_so_v2 *desc = closure;
144         afb_ditf_update_hook(&desc->ditf);
145 }
146
147 static int get_verbosity_cb(void *closure)
148 {
149         struct api_so_v2 *desc = closure;
150         return desc->ditf.interface.verbosity;
151 }
152
153 static void set_verbosity_cb(void *closure, int level)
154 {
155         struct api_so_v2 *desc = closure;
156         desc->ditf.interface.verbosity = level;
157 }
158
159 static struct json_object *describe_cb(void *closure)
160 {
161         struct api_so_v2 *desc = closure;
162         return desc->binding->specification ? json_tokener_parse(desc->binding->specification) : NULL;
163 }
164
165 static struct afb_api_itf so_v2_api_itf = {
166         .call = call_cb,
167         .service_start = service_start_cb,
168         .update_hooks = update_hooks_cb,
169         .get_verbosity = get_verbosity_cb,
170         .set_verbosity = set_verbosity_cb,
171         .describe = describe_cb
172
173 };
174
175 int afb_api_so_v2_add_binding(const struct afb_binding_v2 *binding, void *handle, struct afb_apiset *apiset)
176 {
177         int rc;
178         struct api_so_v2 *desc;
179         struct afb_api afb_api;
180         const struct afb_verb_v2 *bv;
181         int count, i, j;
182
183         /* basic checks */
184         assert(binding->api);
185         assert(binding->specification);
186         assert(binding->verbs);
187
188         /* count the verbs */
189         count = 0;
190         while (binding->verbs[count].verb)
191                 count++;
192
193         /* allocates the description */
194         desc = malloc(sizeof *desc + (count - 1) * sizeof desc->verbs);
195         if (desc == NULL) {
196                 ERROR("out of memory");
197                 goto error;
198         }
199         desc->binding = binding;
200         desc->handle = handle;
201         desc->service = NULL;
202         memset(&desc->ditf, 0, sizeof desc->ditf);
203         desc->count = count;
204
205         /* fill the verbs sorted */
206         for (i = 0 ; i < count ; i++) {
207                 desc->verbs[i].perm = NULL;
208                 j = i;
209                 bv = &binding->verbs[j];
210                 while (j && strcasecmp(bv->verb, desc->verbs[j-1].verb->verb) < 0) {
211                         desc->verbs[j].verb = desc->verbs[j-1].verb;
212                         j--;
213                 }
214                 desc->verbs[j].verb = bv;
215         }
216
217         /* makes the permissions */
218         for (i = 0 ; i < count ; i++) {
219                 if (desc->verbs[i].verb->permissions) {
220                         desc->verbs[i].perm = afb_perm_parse(desc->verbs[i].verb->permissions);
221                         if (!desc->verbs[i].perm) {
222                                 ERROR("Bad permission specification for verb %s of api %s: %s",
223                                         desc->verbs[i].verb->verb, binding->api,
224                                         desc->verbs[i].verb->permissions);
225                                 goto error2;
226                         }
227                 }
228         }
229
230         /* init the interface */
231         afb_ditf_init(&desc->ditf, binding->api);
232
233         /* init the binding */
234         if (binding->init) {
235                 INFO("binding %s calling init function", binding->api);
236                 rc = binding->init(&desc->ditf.interface);
237                 if (rc < 0) {
238                         ERROR("binding %s initialisation function failed...", binding->api);
239                         goto error2;
240                 }
241         }
242
243         /* records the binding */
244         afb_api.closure = desc;
245         afb_api.itf = &so_v2_api_itf;
246         if (afb_apiset_add(apiset, binding->api, afb_api) < 0) {
247                 ERROR("binding %s can't be registered to set %s...", binding->api, afb_apiset_name(apiset));
248                 goto error2;
249         }
250         NOTICE("binding %s added to set %s", binding->api, afb_apiset_name(apiset));
251         return 1;
252
253 error2:
254         count = desc->count;
255         while (count)
256                 if (desc->verbs[--count].perm)
257                         afb_perm_unref(desc->verbs[count].perm);
258         free(desc);
259 error:
260         return -1;
261 }
262
263 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *apiset)
264 {
265         const struct afb_binding_v2 *binding;
266
267         /* retrieves the register function */
268         binding = dlsym(handle, afb_api_so_v2_descriptor);
269         if (!binding)
270                 return 0;
271
272         INFO("binding [%s] looks like an AFB binding V2", path);
273
274         /* basic checks */
275         if (binding->api == NULL || *binding->api == 0) {
276                 ERROR("binding [%s] bad api name...", path);
277                 goto error;
278         }
279         if (!afb_api_is_valid_name(binding->api)) {
280                 ERROR("binding [%s] invalid api name...", path);
281                 goto error;
282         }
283         if (binding->specification == NULL || *binding->specification == 0) {
284                 ERROR("binding [%s] bad specification...", path);
285                 goto error;
286         }
287         if (binding->verbs == NULL) {
288                 ERROR("binding [%s] no verbs...", path);
289                 goto error;
290         }
291
292         return afb_api_so_v2_add_binding(binding, handle, apiset);
293
294  error:
295         return -1;
296 }
297