Fedora 30 packaging fix issu
[src/app-framework-binder.git] / src / afb-api-so-v1.c
1 /*
2  * Copyright (C) 2016, 2017, 2018 "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 #if defined(WITH_LEGACY_BINDING_V1)
19
20 #define _GNU_SOURCE
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <dlfcn.h>
25 #include <assert.h>
26 #include <stdarg.h>
27
28 #include <json-c/json.h>
29 #include <afb/afb-binding-v1.h>
30
31 #include "afb-api.h"
32 #include "afb-api-so-v1.h"
33 #include "afb-apiset.h"
34 #include "afb-export.h"
35 #include "afb-common.h"
36 #include "afb-context.h"
37 #include "afb-api-so.h"
38 #include "afb-xreq.h"
39 #include "verbose.h"
40
41 /*
42  * names of symbols
43  */
44 static const char afb_api_so_v1_register[] = "afbBindingV1Register";
45 static const char afb_api_so_v1_service_init[] = "afbBindingV1ServiceInit";
46 static const char afb_api_so_v1_service_event[] = "afbBindingV1ServiceEvent";
47
48 static const struct afb_verb_desc_v1 *search(struct afb_binding_v1 *binding, const char *name)
49 {
50         const struct afb_verb_desc_v1 *verb;
51
52         verb = binding->v1.verbs;
53         while (verb->name && strcasecmp(verb->name, name))
54                 verb++;
55         return verb->name ? verb : NULL;
56 }
57
58 void afb_api_so_v1_process_call(struct afb_binding_v1 *binding, struct afb_xreq *xreq)
59 {
60         const struct afb_verb_desc_v1 *verb;
61
62         verb = search(binding, xreq->request.called_verb);
63         afb_xreq_call_verb_v1(xreq, verb);
64 }
65
66 static struct json_object *addperm(struct json_object *o, struct json_object *x)
67 {
68         struct json_object *a;
69
70         if (!o)
71                 return x;
72
73         if (!json_object_object_get_ex(o, "allOf", &a)) {
74                 a = json_object_new_array();
75                 json_object_array_add(a, o);
76                 o = json_object_new_object();
77                 json_object_object_add(o, "allOf", a);
78         }
79         json_object_array_add(a, x);
80         return o;
81 }
82
83 static struct json_object *addperm_key_val(struct json_object *o, const char *key, struct json_object *val)
84 {
85         struct json_object *x = json_object_new_object();
86         json_object_object_add(x, key, val);
87         return addperm(o, x);
88 }
89
90 static struct json_object *addperm_key_valstr(struct json_object *o, const char *key, const char *val)
91 {
92         return addperm_key_val(o, key, json_object_new_string(val));
93 }
94
95 static struct json_object *addperm_key_valint(struct json_object *o, const char *key, int val)
96 {
97         return addperm_key_val(o, key, json_object_new_int(val));
98 }
99
100 struct json_object *afb_api_so_v1_make_description_openAPIv3(struct afb_binding_v1 *binding, const char *apiname)
101 {
102         char buffer[256];
103         const struct afb_verb_desc_v1 *verb;
104         struct json_object *r, *f, *a, *i, *p, *g;
105
106         r = json_object_new_object();
107         json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
108
109         i = json_object_new_object();
110         json_object_object_add(r, "info", i);
111         json_object_object_add(i, "title", json_object_new_string(apiname));
112         json_object_object_add(i, "version", json_object_new_string("0.0.0"));
113         json_object_object_add(i, "description", json_object_new_string(binding->v1.info ?: apiname));
114
115         p = json_object_new_object();
116         json_object_object_add(r, "paths", p);
117         verb = binding->v1.verbs;
118         while (verb->name) {
119                 buffer[0] = '/';
120                 strncpy(buffer + 1, verb->name, sizeof buffer - 1);
121                 buffer[sizeof buffer - 1] = 0;
122                 f = json_object_new_object();
123                 json_object_object_add(p, buffer, f);
124                 g = json_object_new_object();
125                 json_object_object_add(f, "get", g);
126
127                 a = NULL;
128                 if (verb->session & AFB_SESSION_CLOSE_X1)
129                         a = addperm_key_valstr(a, "session", "close");
130                 if (verb->session & AFB_SESSION_CHECK_X1)
131                         a = addperm_key_valstr(a, "session", "check");
132                 if (verb->session & AFB_SESSION_RENEW_X1)
133                         a = addperm_key_valstr(a, "token", "refresh");
134                 if (verb->session & AFB_SESSION_LOA_MASK_X1)
135                         a = addperm_key_valint(a, "LOA", (verb->session >> AFB_SESSION_LOA_SHIFT_X1) & AFB_SESSION_LOA_MASK_X1);
136                 if (a)
137                         json_object_object_add(g, "x-permissions", a);
138
139                 a = json_object_new_object();
140                 json_object_object_add(g, "responses", a);
141                 f = json_object_new_object();
142                 json_object_object_add(a, "200", f);
143                 json_object_object_add(f, "description", json_object_new_string(verb->info));
144                 verb++;
145         }
146         return r;
147 }
148
149 int afb_api_so_v1_add(const char *path, void *handle, struct afb_apiset *declare_set, struct afb_apiset * call_set)
150 {
151         struct afb_binding_v1 *binding; /* descriptor */
152         struct afb_binding_v1 *(*register_function) (const struct afb_binding_interface_v1 *interface);
153         int (*init)(struct afb_service_x1 service);
154         void (*onevent)(const char *event, struct json_object *object);
155         struct afb_export *export;
156
157         /* retrieves the register function */
158         register_function = dlsym(handle, afb_api_so_v1_register);
159         if (!register_function)
160                 return 0;
161
162         INFO("binding [%s] is a valid AFB binding V1", path);
163
164         /* allocates the description */
165         init = dlsym(handle, afb_api_so_v1_service_init);
166         onevent = dlsym(handle, afb_api_so_v1_service_event);
167         export = afb_export_create_v1(declare_set, call_set, path, init, onevent, path);
168         if (export == NULL) {
169                 ERROR("binding [%s] creation failure...", path);
170                 goto error;
171         }
172         binding = afb_export_register_v1(export, register_function);
173         if (binding == NULL) {
174                 ERROR("binding [%s] register failure...", path);
175                 goto error;
176         }
177
178         /* check the returned structure */
179         if (binding->type != AFB_BINDING_VERSION_1) {
180                 ERROR("binding [%s] invalid type %d...", path, binding->type);
181                 goto error;
182         }
183         if (binding->v1.prefix == NULL || *binding->v1.prefix == 0) {
184                 ERROR("binding [%s] bad prefix...", path);
185                 goto error;
186         }
187         if (!afb_api_is_valid_name(binding->v1.prefix)) {
188                 ERROR("binding [%s] invalid prefix...", path);
189                 goto error;
190         }
191         if (binding->v1.info == NULL || *binding->v1.info == 0) {
192                 ERROR("binding [%s] bad description...", path);
193                 goto error;
194         }
195         if (binding->v1.verbs == NULL) {
196                 ERROR("binding [%s] no verbs...", path);
197                 goto error;
198         }
199
200         /* records the binding */
201         if (!strcmp(path, afb_export_apiname(export))) {
202                 if (afb_export_rename(export, binding->v1.prefix) < 0) {
203                         ERROR("binding [%s] can't be renamed to %s", path, binding->v1.prefix);
204                         goto error;
205                 }
206         }
207
208         if (afb_export_declare(export, 0) < 0) {
209                 ERROR("binding [%s] can't be registered...", path);
210                 goto error;
211         }
212         INFO("binding %s loaded with API prefix %s", path, afb_export_apiname(export));
213         afb_export_unref(export);
214         return 1;
215
216 error:
217         afb_export_unref(export);
218
219         return -1;
220 }
221
222 #endif
223