secure subcall api and verb
[src/app-framework-binder.git] / src / afb-subcall.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
23 #include <json-c/json.h>
24 #include <afb/afb-req-itf.h>
25
26 #include "afb-subcall.h"
27 #include "afb-msg-json.h"
28 #include "afb-apis.h"
29 #include "afb-context.h"
30 #include "afb-xreq.h"
31 #include "afb-cred.h"
32 #include "verbose.h"
33 #include "jobs.h"
34
35 struct subcall;
36
37 static void subcall_destroy(struct afb_xreq *xreq);
38 static void subcall_reply(struct afb_xreq *xreq, int iserror, struct json_object *obj);
39 static int subcall_subscribe(struct afb_xreq *xreq, struct afb_event event);
40 static int subcall_unsubscribe(struct afb_xreq *xreq, struct afb_event event);
41
42 const struct afb_xreq_query_itf afb_subcall_xreq_itf = {
43         .reply = subcall_reply,
44         .unref = subcall_destroy,
45         .subscribe = subcall_subscribe,
46         .unsubscribe = subcall_unsubscribe
47 };
48
49 struct subcall
50 {
51         struct afb_xreq xreq;
52         struct afb_xreq *caller;
53         void (*callback)(void*, int, struct json_object*);
54         void *closure;
55 };
56
57 static void subcall_destroy(struct afb_xreq *xreq)
58 {
59         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
60
61         json_object_put(subcall->xreq.json);
62         afb_cred_unref(subcall->xreq.cred);
63         afb_xreq_unref(subcall->caller);
64         free(subcall);
65 }
66
67 static void subcall_reply(struct afb_xreq *xreq, int iserror, struct json_object *obj)
68 {
69         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
70
71         subcall->callback(subcall->closure, iserror, obj);
72         json_object_put(obj);
73 }
74
75 static int subcall_subscribe(struct afb_xreq *xreq, struct afb_event event)
76 {
77         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
78
79         return afb_xreq_subscribe(subcall->caller, event);
80 }
81
82 static int subcall_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
83 {
84         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
85
86         return afb_xreq_unsubscribe(subcall->caller, event);
87 }
88
89 static struct subcall *create_subcall(struct afb_xreq *caller, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *closure)
90 {
91         struct subcall *subcall;
92         size_t lenapi, lenverb;
93         char *copy;
94
95         lenapi = 1 + strlen(api);
96         lenverb = 1 + strlen(verb);
97         subcall = malloc(lenapi + lenverb + sizeof *subcall);
98         if (subcall == NULL) {
99                 return NULL;
100         }
101         afb_xreq_init(&subcall->xreq, &afb_subcall_xreq_itf);
102         afb_context_subinit(&subcall->xreq.context, &caller->context);
103         subcall->xreq.cred = afb_cred_addref(caller->cred);
104         subcall->xreq.json = args;
105         copy = (char*)&subcall[1];
106         memcpy(copy, api, lenapi);
107         subcall->xreq.api = copy;
108         copy = &copy[lenapi];
109         memcpy(copy, verb, lenverb);
110         subcall->xreq.verb = copy;
111         subcall->caller = caller;
112         subcall->callback = callback;
113         subcall->closure = closure;
114         afb_xreq_addref(caller);
115         json_object_get(args);
116         return subcall;
117 }
118
119 void afb_subcall(
120                 struct afb_xreq *caller,
121                 const char *api,
122                 const char *verb,
123                 struct json_object *args,
124                 void (*callback)(void*, int, struct json_object*),
125                 void *closure
126 )
127 {
128         struct subcall *subcall;
129
130         subcall = create_subcall(caller, api, verb, args, callback, closure);
131         if (subcall == NULL) {
132                 callback(closure, 1, afb_msg_json_internal_error());
133                 return;
134         }
135
136         afb_apis_call(&subcall->xreq);
137         afb_xreq_unref(&subcall->xreq);
138 }
139
140 struct subcall_sync
141 {
142         struct afb_xreq *caller;
143         const char *api;
144         const char *verb;
145         struct json_object *args;
146         struct jobloop *jobloop;
147         struct json_object *result;
148         int iserror;
149 };
150
151 static void subcall_sync_leave(struct subcall_sync *sync)
152 {
153         struct jobloop *jobloop = sync->jobloop;
154         sync->jobloop = NULL;
155         if (jobloop)
156                 jobs_leave(jobloop);
157 }
158
159 static void subcall_sync_reply(void *closure, int iserror, struct json_object *obj)
160 {
161         struct subcall_sync *sync = closure;
162
163         sync->iserror = iserror;
164         sync->result = obj;
165         json_object_get(obj);
166         subcall_sync_leave(sync);
167 }
168
169 static void subcall_sync_enter(int signum, void *closure, struct jobloop *jobloop)
170 {
171         struct subcall_sync *sync = closure;
172
173         if (!signum) {
174                 sync->jobloop = jobloop;
175                 afb_xreq_unhooked_subcall(sync->caller, sync->api, sync->verb, sync->args, subcall_sync_reply, sync);
176         } else {
177                 sync->result = json_object_get(afb_msg_json_internal_error());
178                 sync->iserror = 1;
179                 subcall_sync_leave(sync);
180         }
181 }
182
183 int afb_subcall_sync(
184                 struct afb_xreq *caller,
185                 const char *api,
186                 const char *verb,
187                 struct json_object *args,
188                 struct json_object **result
189 )
190 {
191         int rc;
192         struct subcall_sync sync;
193
194         sync.caller = caller;
195         sync.api = api;
196         sync.verb = verb;
197         sync.args = args;
198         sync.jobloop = NULL;
199         sync.result = NULL;
200         sync.iserror = 1;
201
202         rc = jobs_enter(NULL, 0, subcall_sync_enter, &sync);
203         if (rc < 0) {
204                 sync.result = json_object_get(afb_msg_json_internal_error());
205                 sync.iserror = 1;
206         }
207         rc = !sync.iserror;
208         *result = sync.result;
209         return rc;
210 }
211
212