fix regression of AFB_SESSION_NONE
[src/app-framework-binder.git] / src / afb-api-so.c
1 /*
2  * Copyright (C) 2016 "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 <stdio.h>
21 #include <assert.h>
22 #include <string.h>
23 #include <dirent.h>
24 #include <dlfcn.h>
25 #include <unistd.h>
26 #include <limits.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29
30 #include <afb/afb-plugin.h>
31 #include <afb/afb-req-itf.h>
32 #include <afb/afb-event-sender-itf.h>
33
34 #include "session.h"
35 #include "afb-common.h"
36 #include "afb-context.h"
37 #include "afb-apis.h"
38 #include "afb-api-so.h"
39 #include "afb-sig-handler.h"
40 #include "verbose.h"
41
42 struct api_so_desc {
43         struct AFB_plugin *plugin;      /* descriptor */
44         size_t apilength;
45         void *handle;                   /* context of dlopen */
46         struct AFB_interface interface; /* interface */
47 };
48
49 static int api_timeout = 15;
50
51 static const char plugin_register_function[] = "pluginAfbV1Register";
52
53 static void afb_api_so_event_sender_push(struct api_so_desc *desc, const char *name, struct json_object *object)
54 {
55         size_t length;
56         char *event;
57
58         assert(desc->plugin != NULL);
59         length = strlen(name);
60         event = alloca(length + 2 + desc->apilength);
61         memcpy(event, desc->plugin->v1.prefix, desc->apilength);
62         event[desc->apilength] = '/';
63         memcpy(event + desc->apilength + 1, name, length + 1);
64         ctxClientEventSend(NULL, event, object);
65 }
66
67 static const struct afb_event_sender_itf event_sender_itf = {
68         .push = (void*)afb_api_so_event_sender_push
69 };
70
71 static struct afb_event_sender afb_api_so_get_event_sender(struct api_so_desc *desc)
72 {
73         return (struct afb_event_sender){ .itf = &event_sender_itf, .closure = desc };
74 }
75
76 static const struct afb_daemon_itf daemon_itf = {
77         .get_event_sender = (void*)afb_api_so_get_event_sender,
78         .get_event_loop = (void*)afb_common_get_event_loop,
79         .get_user_bus = (void*)afb_common_get_user_bus,
80         .get_system_bus = (void*)afb_common_get_system_bus
81 };
82
83 struct monitoring {
84         struct afb_req req;
85         void (*action)(struct afb_req);
86 };
87
88 static void monitored_call(int signum, struct monitoring *data)
89 {
90         if (signum != 0)
91                 afb_req_fail_f(data->req, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
92         else
93                 data->action(data->req);
94 }
95
96 static void call_check(struct afb_req req, struct afb_context *context, const struct AFB_verb_desc_v1 *verb)
97 {
98         struct monitoring data;
99
100         int stag = (int)verb->session;
101
102         if ((stag & (AFB_SESSION_CREATE|AFB_SESSION_CLOSE|AFB_SESSION_RENEW|AFB_SESSION_CHECK|AFB_SESSION_LOA_EQ)) != 0) {
103                 if (!afb_context_check(context)) {
104                         afb_context_close(context);
105                         afb_req_fail(req, "failed", "invalid token's identity");
106                         return;
107                 }       
108         }
109
110         if ((stag & AFB_SESSION_CREATE) != 0) {
111                 if (afb_context_check_loa(context, 1)) {
112                         afb_req_fail(req, "failed", "invalid creation state");
113                         return;
114                 }
115                 afb_context_change_loa(context, 1);
116                 afb_context_refresh(context);
117         }
118         
119         if ((stag & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0)
120                 afb_context_refresh(context);
121
122         if ((stag & AFB_SESSION_CLOSE) != 0) {
123                 afb_context_change_loa(context, 0);
124                 afb_context_close(context);
125         }
126
127         if ((stag & AFB_SESSION_LOA_GE) != 0) {
128                 int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
129                 if (!afb_context_check_loa(context, loa)) {
130                         afb_req_fail(req, "failed", "invalid LOA");
131                         return;
132                 }
133         }
134
135         if ((stag & AFB_SESSION_LOA_LE) != 0) {
136                 int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
137                 if (afb_context_check_loa(context, loa + 1)) {
138                         afb_req_fail(req, "failed", "invalid LOA");
139                         return;
140                 }
141         }
142
143         data.req = req;
144         data.action = verb->callback;
145         afb_sig_monitor((void*)monitored_call, &data, api_timeout);
146 }
147
148 static void call(struct api_so_desc *desc, struct afb_req req, struct afb_context *context, const char *verb, size_t lenverb)
149 {
150         const struct AFB_verb_desc_v1 *v;
151
152         v = desc->plugin->v1.verbs;
153         while (v->name && (strncasecmp(v->name, verb, lenverb) || v->name[lenverb]))
154                 v++;
155         if (v->name)
156                 call_check(req, context, v);
157         else
158                 afb_req_fail_f(req, "unknown-verb", "verb %.*s unknown within api %s", (int)lenverb, verb, desc->plugin->v1.prefix);
159 }
160
161 int afb_api_so_add_plugin(const char *path)
162 {
163         int rc;
164         void *handle;
165         struct api_so_desc *desc;
166         struct AFB_plugin *(*pluginAfbV1RegisterFct) (const struct AFB_interface *interface);
167
168         // This is a loadable library let's check if it's a plugin
169         rc = 0;
170         handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
171         if (handle == NULL) {
172                 ERROR("plugin [%s] not loadable", path);
173                 goto error;
174         }
175
176         /* retrieves the register function */
177         pluginAfbV1RegisterFct = dlsym(handle, plugin_register_function);
178         if (!pluginAfbV1RegisterFct) {
179                 ERROR("plugin [%s] is not an AFB plugin", path);
180                 goto error2;
181         }
182         INFO("plugin [%s] is a valid AFB plugin", path);
183         rc = -1;
184
185         /* allocates the description */
186         desc = calloc(1, sizeof *desc);
187         if (desc == NULL) {
188                 ERROR("out of memory");
189                 goto error2;
190         }
191         desc->handle = handle;
192
193         /* init the interface */
194         desc->interface.verbosity = 0;
195         desc->interface.mode = AFB_MODE_LOCAL;
196         desc->interface.daemon.itf = &daemon_itf;
197         desc->interface.daemon.closure = desc;
198
199         /* init the plugin */
200         desc->plugin = pluginAfbV1RegisterFct(&desc->interface);
201         if (desc->plugin == NULL) {
202                 ERROR("plugin [%s] register function failed. continuing...", path);
203                 goto error3;
204         }
205
206         /* check the returned structure */
207         if (desc->plugin->type != AFB_PLUGIN_VERSION_1) {
208                 ERROR("plugin [%s] invalid type %d...", path, desc->plugin->type);
209                 goto error3;
210         }
211         if (desc->plugin->v1.prefix == NULL || *desc->plugin->v1.prefix == 0) {
212                 ERROR("plugin [%s] bad prefix...", path);
213                 goto error3;
214         }
215         if (!afb_apis_is_valid_api_name(desc->plugin->v1.prefix)) {
216                 ERROR("plugin [%s] invalid prefix...", path);
217                 goto error3;
218         }
219         if (desc->plugin->v1.info == NULL || *desc->plugin->v1.info == 0) {
220                 ERROR("plugin [%s] bad description...", path);
221                 goto error3;
222         }
223         if (desc->plugin->v1.verbs == NULL) {
224                 ERROR("plugin [%s] no APIs...", path);
225                 goto error3;
226         }
227
228         /* records the plugin */
229         desc->apilength = strlen(desc->plugin->v1.prefix);
230         if (afb_apis_add(desc->plugin->v1.prefix, (struct afb_api){
231                         .closure = desc,
232                         .call = (void*)call}) < 0) {
233                 ERROR("plugin [%s] can't be registered...", path);
234                 goto error3;
235         }
236         NOTICE("plugin %s loaded with API prefix %s", path, desc->plugin->v1.prefix);
237         return 0;
238
239 error3:
240         free(desc);
241 error2:
242         dlclose(handle);
243 error:
244         return rc;
245 }
246
247 static int adddirs(char path[PATH_MAX], size_t end)
248 {
249         DIR *dir;
250         struct dirent ent, *result;
251         size_t len;
252
253         /* open the DIR now */
254         dir = opendir(path);
255         if (dir == NULL) {
256                 ERROR("can't scan plugin directory %s, %m", path);
257                 return -1;
258         }
259         INFO("Scanning dir=[%s] for plugins", path);
260
261         /* scan each entry */
262         if (end)
263                 path[end++] = '/';
264         for (;;) {
265                 readdir_r(dir, &ent, &result);
266                 if (result == NULL)
267                         break;
268
269                 len = strlen(ent.d_name);
270                 if (len + end >= PATH_MAX) {
271                         ERROR("path too long while scanning plugins for %s", ent.d_name);
272                         continue;
273                 }
274                 memcpy(&path[end], ent.d_name, len+1);
275                 if (ent.d_type == DT_DIR) {
276                         /* case of directories */
277                         if (ent.d_name[0] == '.') {
278                                 if (len == 1)
279                                         continue;
280                                 if (ent.d_name[1] == '.' && len == 2)
281                                         continue;
282                         }
283                         adddirs(path, end+len);;
284                 } else if (ent.d_type == DT_REG) {
285                         /* case of files */
286                         if (!strstr(ent.d_name, ".so"))
287                                 continue;
288                         if (afb_api_so_add_plugin(path) < 0)
289                                 return -1;
290                 }
291         }
292         closedir(dir);
293         return 0;
294 }
295
296 int afb_api_so_add_directory(const char *path)
297 {
298         size_t length;
299         char buffer[PATH_MAX];
300
301         length = strlen(path);
302         if (length >= sizeof(buffer)) {
303                 ERROR("path too long %lu [%.99s...]", (unsigned long)length, path);
304                 return -1;
305         }
306
307         memcpy(buffer, path, length + 1);
308         return adddirs(buffer, length);
309 }
310
311 int afb_api_so_add_path(const char *path)
312 {
313         struct stat st;
314         int rc;
315
316         rc = stat(path, &st);
317         if (rc < 0)
318                 ERROR("Invalid plugin path [%s]: %m", path);
319         else if (S_ISDIR(st.st_mode))
320                 rc = afb_api_so_add_directory(path);
321         else if (strstr(path, ".so"))
322                 rc = afb_api_so_add_plugin(path);
323         else
324                 INFO("not a plugin [%s], skipped", path);
325         return rc;
326 }
327
328 int afb_api_so_add_pathset(const char *pathset)
329 {
330         static char sep[] = ":";
331         char *ps, *p;
332
333         ps = strdupa(pathset);
334         for (;;) {
335                 p = strsep(&ps, sep);
336                 if (!p)
337                         return 0;
338                 if (afb_api_so_add_path(p) < 0)
339                         return -1;
340         }
341 }
342