diff options
author | Schanzenbach, Martin <martin.schanzenbach@aisec.fraunhofer.de> | 2018-08-13 08:51:19 +0200 |
---|---|---|
committer | Schanzenbach, Martin <martin.schanzenbach@aisec.fraunhofer.de> | 2018-08-13 08:51:19 +0200 |
commit | 6371b64774428e83ff83ada88bda354356718aca (patch) | |
tree | a2a68aafcf1f6faa8a84648baaf0731da1136346 /src/rest-plugins | |
parent | 6656d6c3e7111075572c042ae714c9710f30273b (diff) | |
download | gnunet-6371b64774428e83ff83ada88bda354356718aca.tar.gz gnunet-6371b64774428e83ff83ada88bda354356718aca.zip |
fix build; move rest plugins to separate folder
Diffstat (limited to 'src/rest-plugins')
-rw-r--r-- | src/rest-plugins/Makefile.am | 102 | ||||
-rw-r--r-- | src/rest-plugins/oidc_helper.c | 440 | ||||
-rw-r--r-- | src/rest-plugins/oidc_helper.h | 109 | ||||
-rw-r--r-- | src/rest-plugins/plugin_rest_copying.c | 231 | ||||
-rw-r--r-- | src/rest-plugins/plugin_rest_gns.c | 476 | ||||
-rw-r--r-- | src/rest-plugins/plugin_rest_identity.c | 1319 | ||||
-rw-r--r-- | src/rest-plugins/plugin_rest_namestore.c | 1004 | ||||
-rw-r--r-- | src/rest-plugins/plugin_rest_openid_connect.c | 2171 | ||||
-rw-r--r-- | src/rest-plugins/plugin_rest_peerinfo.c | 820 |
9 files changed, 6672 insertions, 0 deletions
diff --git a/src/rest-plugins/Makefile.am b/src/rest-plugins/Makefile.am new file mode 100644 index 000000000..ae74dc78a --- /dev/null +++ b/src/rest-plugins/Makefile.am | |||
@@ -0,0 +1,102 @@ | |||
1 | # This Makefile.am is in the public domain | ||
2 | AM_CPPFLAGS = -I$(top_srcdir)/src/include | ||
3 | |||
4 | plugindir = $(libdir)/gnunet | ||
5 | |||
6 | pkgcfgdir= $(pkgdatadir)/config.d/ | ||
7 | |||
8 | libexecdir= $(pkglibdir)/libexec/ | ||
9 | |||
10 | |||
11 | if MINGW | ||
12 | WINFLAGS = -Wl,--no-undefined -Wl,--export-all-symbols | ||
13 | endif | ||
14 | |||
15 | if USE_COVERAGE | ||
16 | AM_CFLAGS = --coverage -O0 | ||
17 | XLIBS = -lgcov | ||
18 | endif | ||
19 | |||
20 | plugin_LTLIBRARIES = \ | ||
21 | libgnunet_plugin_rest_copying.la \ | ||
22 | libgnunet_plugin_rest_peerinfo.la \ | ||
23 | libgnunet_plugin_rest_identity.la \ | ||
24 | libgnunet_plugin_rest_namestore.la \ | ||
25 | libgnunet_plugin_rest_gns.la \ | ||
26 | libgnunet_plugin_rest_openid_connect.la | ||
27 | |||
28 | |||
29 | libgnunet_plugin_rest_copying_la_SOURCES = \ | ||
30 | plugin_rest_copying.c | ||
31 | libgnunet_plugin_rest_copying_la_LIBADD = \ | ||
32 | $(top_builddir)/src/rest/libgnunetrest.la \ | ||
33 | $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \ | ||
34 | $(LTLIBINTL) -lmicrohttpd | ||
35 | libgnunet_plugin_rest_copying_la_LDFLAGS = \ | ||
36 | $(GN_PLUGIN_LDFLAGS) | ||
37 | |||
38 | libgnunet_plugin_rest_peerinfo_la_SOURCES = \ | ||
39 | plugin_rest_peerinfo.c | ||
40 | libgnunet_plugin_rest_peerinfo_la_LIBADD = \ | ||
41 | $(top_builddir)/src/peerinfo/libgnunetpeerinfo.la \ | ||
42 | $(top_builddir)/src/rest/libgnunetrest.la \ | ||
43 | $(top_builddir)/src/json/libgnunetjson.la \ | ||
44 | $(top_builddir)/src/transport/libgnunettransport.la \ | ||
45 | $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \ | ||
46 | $(LTLIBINTL) -ljansson -lmicrohttpd | ||
47 | libgnunet_plugin_rest_peerinfo_la_LDFLAGS = \ | ||
48 | $(GN_PLUGIN_LDFLAGS) | ||
49 | |||
50 | libgnunet_plugin_rest_identity_la_SOURCES = \ | ||
51 | plugin_rest_identity.c | ||
52 | libgnunet_plugin_rest_identity_la_LIBADD = \ | ||
53 | $(top_builddir)/src/identity/libgnunetidentity.la \ | ||
54 | $(top_builddir)/src/rest/libgnunetrest.la \ | ||
55 | $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \ | ||
56 | $(LTLIBINTL) -ljansson -lmicrohttpd | ||
57 | libgnunet_plugin_rest_identity_la_LDFLAGS = \ | ||
58 | $(GN_PLUGIN_LDFLAGS) | ||
59 | |||
60 | |||
61 | |||
62 | libgnunet_plugin_rest_namestore_la_SOURCES = \ | ||
63 | plugin_rest_namestore.c | ||
64 | libgnunet_plugin_rest_namestore_la_LIBADD = \ | ||
65 | $(top_builddir)/src/namestore/libgnunetnamestore.la \ | ||
66 | $(top_builddir)/src/rest/libgnunetrest.la \ | ||
67 | $(top_builddir)/src/identity/libgnunetidentity.la \ | ||
68 | $(top_builddir)/src/json/libgnunetjson.la \ | ||
69 | $(top_builddir)/src/gnsrecord/libgnunetgnsrecord.la \ | ||
70 | $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \ | ||
71 | $(LTLIBINTL) -ljansson -lmicrohttpd | ||
72 | libgnunet_plugin_rest_namestore_la_LDFLAGS = \ | ||
73 | $(GN_PLUGIN_LDFLAGS) | ||
74 | |||
75 | libgnunet_plugin_rest_gns_la_SOURCES = \ | ||
76 | plugin_rest_gns.c | ||
77 | libgnunet_plugin_rest_gns_la_LIBADD = \ | ||
78 | $(top_builddir)/src/gns/libgnunetgns.la \ | ||
79 | $(top_builddir)/src/rest/libgnunetrest.la \ | ||
80 | $(top_builddir)/src/identity/libgnunetidentity.la \ | ||
81 | $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \ | ||
82 | $(LTLIBINTL) -ljansson -lmicrohttpd | ||
83 | libgnunet_plugin_rest_gns_la_LDFLAGS = \ | ||
84 | $(GN_PLUGIN_LDFLAGS) | ||
85 | |||
86 | libgnunet_plugin_rest_openid_connect_la_SOURCES = \ | ||
87 | plugin_rest_openid_connect.c \ | ||
88 | oidc_helper.c | ||
89 | libgnunet_plugin_rest_openid_connect_la_LIBADD = \ | ||
90 | $(top_builddir)/src/identity/libgnunetidentity.la \ | ||
91 | $(top_builddir)/src/reclaim/libgnunetreclaim.la \ | ||
92 | $(top_builddir)/src/rest/libgnunetrest.la \ | ||
93 | $(top_builddir)/src/jsonapi/libgnunetjsonapi.la \ | ||
94 | $(top_builddir)/src/reclaim-attribute/libgnunetreclaimattribute.la \ | ||
95 | $(top_builddir)/src/namestore/libgnunetnamestore.la \ | ||
96 | $(top_builddir)/src/gns/libgnunetgns.la \ | ||
97 | $(top_builddir)/src/gnsrecord/libgnunetgnsrecord.la \ | ||
98 | $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \ | ||
99 | $(LTLIBINTL) -ljansson -lmicrohttpd | ||
100 | libgnunet_plugin_rest_openid_connect_la_LDFLAGS = \ | ||
101 | $(GN_PLUGIN_LDFLAGS) | ||
102 | |||
diff --git a/src/rest-plugins/oidc_helper.c b/src/rest-plugins/oidc_helper.c new file mode 100644 index 000000000..1e9e64fec --- /dev/null +++ b/src/rest-plugins/oidc_helper.c | |||
@@ -0,0 +1,440 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2010-2015 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | /** | ||
20 | * @file reclaim/oidc_helper.c | ||
21 | * @brief helper library for OIDC related functions | ||
22 | * @author Martin Schanzenbach | ||
23 | */ | ||
24 | #include "platform.h" | ||
25 | #include "gnunet_util_lib.h" | ||
26 | #include "gnunet_signatures.h" | ||
27 | #include "gnunet_reclaim_service.h" | ||
28 | #include "gnunet_reclaim_attribute_lib.h" | ||
29 | #include <jansson.h> | ||
30 | #include <inttypes.h> | ||
31 | #include "oidc_helper.h" | ||
32 | |||
33 | static char* | ||
34 | create_jwt_header(void) | ||
35 | { | ||
36 | json_t *root; | ||
37 | char *json_str; | ||
38 | |||
39 | root = json_object (); | ||
40 | json_object_set_new (root, JWT_ALG, json_string (JWT_ALG_VALUE)); | ||
41 | json_object_set_new (root, JWT_TYP, json_string (JWT_TYP_VALUE)); | ||
42 | |||
43 | json_str = json_dumps (root, JSON_INDENT(0) | JSON_COMPACT); | ||
44 | json_decref (root); | ||
45 | return json_str; | ||
46 | } | ||
47 | |||
48 | static void | ||
49 | replace_char(char* str, char find, char replace){ | ||
50 | char *current_pos = strchr(str,find); | ||
51 | while (current_pos){ | ||
52 | *current_pos = replace; | ||
53 | current_pos = strchr(current_pos,find); | ||
54 | } | ||
55 | } | ||
56 | |||
57 | //RFC4648 | ||
58 | static void | ||
59 | fix_base64(char* str) { | ||
60 | char *padding; | ||
61 | //First, remove trailing padding '=' | ||
62 | padding = strtok(str, "="); | ||
63 | while (NULL != padding) | ||
64 | padding = strtok(NULL, "="); | ||
65 | |||
66 | //Replace + with - | ||
67 | replace_char (str, '+', '-'); | ||
68 | |||
69 | //Replace / with _ | ||
70 | replace_char (str, '/', '_'); | ||
71 | |||
72 | } | ||
73 | |||
74 | /** | ||
75 | * Create a JWT from attributes | ||
76 | * | ||
77 | * @param aud_key the public of the audience | ||
78 | * @param sub_key the public key of the subject | ||
79 | * @param attrs the attribute list | ||
80 | * @param expiration_time the validity of the token | ||
81 | * @param secret_key the key used to sign the JWT | ||
82 | * @return a new base64-encoded JWT string. | ||
83 | */ | ||
84 | char* | ||
85 | OIDC_id_token_new (const struct GNUNET_CRYPTO_EcdsaPublicKey *aud_key, | ||
86 | const struct GNUNET_CRYPTO_EcdsaPublicKey *sub_key, | ||
87 | const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs, | ||
88 | const struct GNUNET_TIME_Relative *expiration_time, | ||
89 | const char *nonce, | ||
90 | const char *secret_key) | ||
91 | { | ||
92 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le; | ||
93 | struct GNUNET_HashCode signature; | ||
94 | struct GNUNET_TIME_Absolute exp_time; | ||
95 | struct GNUNET_TIME_Absolute time_now; | ||
96 | char* audience; | ||
97 | char* subject; | ||
98 | char* header; | ||
99 | char* body_str; | ||
100 | char* result; | ||
101 | char* header_base64; | ||
102 | char* body_base64; | ||
103 | char* signature_target; | ||
104 | char* signature_base64; | ||
105 | char* attr_val_str; | ||
106 | json_t* body; | ||
107 | |||
108 | //iat REQUIRED time now | ||
109 | time_now = GNUNET_TIME_absolute_get(); | ||
110 | //exp REQUIRED time expired from config | ||
111 | exp_time = GNUNET_TIME_absolute_add (time_now, *expiration_time); | ||
112 | //auth_time only if max_age | ||
113 | //nonce only if nonce | ||
114 | // OPTIONAL acr,amr,azp | ||
115 | subject = GNUNET_STRINGS_data_to_string_alloc (sub_key, | ||
116 | sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)); | ||
117 | audience = GNUNET_STRINGS_data_to_string_alloc (aud_key, | ||
118 | sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)); | ||
119 | header = create_jwt_header (); | ||
120 | body = json_object (); | ||
121 | |||
122 | //iss REQUIRED case sensitive server uri with https | ||
123 | //The issuer is the local reclaim instance (e.g. https://reclaim.id/api/openid) | ||
124 | json_object_set_new (body, | ||
125 | "iss", json_string (SERVER_ADDRESS)); | ||
126 | //sub REQUIRED public key identity, not exceed 255 ASCII length | ||
127 | json_object_set_new (body, | ||
128 | "sub", json_string (subject)); | ||
129 | //aud REQUIRED public key client_id must be there | ||
130 | json_object_set_new (body, | ||
131 | "aud", json_string (audience)); | ||
132 | //iat | ||
133 | json_object_set_new (body, | ||
134 | "iat", json_integer (time_now.abs_value_us / (1000*1000))); | ||
135 | //exp | ||
136 | json_object_set_new (body, | ||
137 | "exp", json_integer (exp_time.abs_value_us / (1000*1000))); | ||
138 | //nbf | ||
139 | json_object_set_new (body, | ||
140 | "nbf", json_integer (time_now.abs_value_us / (1000*1000))); | ||
141 | //nonce | ||
142 | if (NULL != nonce) | ||
143 | json_object_set_new (body, | ||
144 | "nonce", json_string (nonce)); | ||
145 | |||
146 | for (le = attrs->list_head; NULL != le; le = le->next) | ||
147 | { | ||
148 | attr_val_str = GNUNET_RECLAIM_ATTRIBUTE_value_to_string (le->claim->type, | ||
149 | le->claim->data, | ||
150 | le->claim->data_size); | ||
151 | json_object_set_new (body, | ||
152 | le->claim->name, | ||
153 | json_string (attr_val_str)); | ||
154 | GNUNET_free (attr_val_str); | ||
155 | } | ||
156 | body_str = json_dumps (body, JSON_INDENT(0) | JSON_COMPACT); | ||
157 | json_decref (body); | ||
158 | |||
159 | GNUNET_STRINGS_base64_encode (header, | ||
160 | strlen (header), | ||
161 | &header_base64); | ||
162 | fix_base64(header_base64); | ||
163 | |||
164 | GNUNET_STRINGS_base64_encode (body_str, | ||
165 | strlen (body_str), | ||
166 | &body_base64); | ||
167 | fix_base64(body_base64); | ||
168 | |||
169 | GNUNET_free (subject); | ||
170 | GNUNET_free (audience); | ||
171 | |||
172 | /** | ||
173 | * Creating the JWT signature. This might not be | ||
174 | * standards compliant, check. | ||
175 | */ | ||
176 | GNUNET_asprintf (&signature_target, "%s.%s", header_base64, body_base64); | ||
177 | GNUNET_CRYPTO_hmac_raw (secret_key, strlen (secret_key), signature_target, strlen (signature_target), &signature); | ||
178 | GNUNET_STRINGS_base64_encode ((const char*)&signature, | ||
179 | sizeof (struct GNUNET_HashCode), | ||
180 | &signature_base64); | ||
181 | fix_base64(signature_base64); | ||
182 | |||
183 | GNUNET_asprintf (&result, "%s.%s.%s", | ||
184 | header_base64, body_base64, signature_base64); | ||
185 | |||
186 | GNUNET_free (signature_target); | ||
187 | GNUNET_free (header); | ||
188 | GNUNET_free (body_str); | ||
189 | GNUNET_free (signature_base64); | ||
190 | GNUNET_free (body_base64); | ||
191 | GNUNET_free (header_base64); | ||
192 | return result; | ||
193 | } | ||
194 | /** | ||
195 | * Builds an OIDC authorization code including | ||
196 | * a reclaim ticket and nonce | ||
197 | * | ||
198 | * @param issuer the issuer of the ticket, used to sign the ticket and nonce | ||
199 | * @param ticket the ticket to include in the code | ||
200 | * @param nonce the nonce to include in the code | ||
201 | * @return a new authorization code (caller must free) | ||
202 | */ | ||
203 | char* | ||
204 | OIDC_build_authz_code (const struct GNUNET_CRYPTO_EcdsaPrivateKey *issuer, | ||
205 | const struct GNUNET_RECLAIM_Ticket *ticket, | ||
206 | const char* nonce) | ||
207 | { | ||
208 | char *ticket_str; | ||
209 | json_t *code_json; | ||
210 | char *signature_payload; | ||
211 | char *signature_str; | ||
212 | char *authz_code; | ||
213 | size_t signature_payload_len; | ||
214 | struct GNUNET_CRYPTO_EcdsaSignature signature; | ||
215 | struct GNUNET_CRYPTO_EccSignaturePurpose *purpose; | ||
216 | |||
217 | signature_payload_len = sizeof (struct GNUNET_RECLAIM_Ticket); | ||
218 | if (NULL != nonce) | ||
219 | signature_payload_len += strlen (nonce); | ||
220 | |||
221 | signature_payload = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) + signature_payload_len); | ||
222 | purpose = (struct GNUNET_CRYPTO_EccSignaturePurpose *)signature_payload; | ||
223 | purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) + signature_payload_len); | ||
224 | purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_RECLAIM_CODE_SIGN); | ||
225 | memcpy (&purpose[1], | ||
226 | ticket, | ||
227 | sizeof (struct GNUNET_RECLAIM_Ticket)); | ||
228 | if (NULL != nonce) | ||
229 | memcpy (((char*)&purpose[1]) + sizeof (struct GNUNET_RECLAIM_Ticket), | ||
230 | nonce, | ||
231 | strlen (nonce)); | ||
232 | if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (issuer, | ||
233 | purpose, | ||
234 | &signature)) | ||
235 | { | ||
236 | GNUNET_free (signature_payload); | ||
237 | return NULL; | ||
238 | } | ||
239 | signature_str = GNUNET_STRINGS_data_to_string_alloc (&signature, | ||
240 | sizeof (signature)); | ||
241 | ticket_str = GNUNET_STRINGS_data_to_string_alloc (ticket, | ||
242 | sizeof (struct GNUNET_RECLAIM_Ticket)); | ||
243 | |||
244 | code_json = json_object (); | ||
245 | json_object_set_new (code_json, | ||
246 | "ticket", | ||
247 | json_string (ticket_str)); | ||
248 | if (NULL != nonce) | ||
249 | json_object_set_new (code_json, | ||
250 | "nonce", | ||
251 | json_string (nonce)); | ||
252 | json_object_set_new (code_json, | ||
253 | "signature", | ||
254 | json_string (signature_str)); | ||
255 | authz_code = json_dumps (code_json, | ||
256 | JSON_INDENT(0) | JSON_COMPACT); | ||
257 | GNUNET_free (signature_payload); | ||
258 | GNUNET_free (signature_str); | ||
259 | GNUNET_free (ticket_str); | ||
260 | json_decref (code_json); | ||
261 | return authz_code; | ||
262 | } | ||
263 | |||
264 | |||
265 | |||
266 | |||
267 | /** | ||
268 | * Parse reclaim ticket and nonce from | ||
269 | * authorization code. | ||
270 | * This also verifies the signature in the code. | ||
271 | * | ||
272 | * @param audience the expected audience of the code | ||
273 | * @param code the string representation of the code | ||
274 | * @param ticket where to store the ticket | ||
275 | * @param nonce where to store the nonce | ||
276 | * @return GNUNET_OK if successful, else GNUNET_SYSERR | ||
277 | */ | ||
278 | int | ||
279 | OIDC_parse_authz_code (const struct GNUNET_CRYPTO_EcdsaPublicKey *audience, | ||
280 | const char* code, | ||
281 | struct GNUNET_RECLAIM_Ticket **ticket, | ||
282 | char **nonce) | ||
283 | { | ||
284 | json_error_t error; | ||
285 | json_t *code_json; | ||
286 | json_t *ticket_json; | ||
287 | json_t *nonce_json; | ||
288 | json_t *signature_json; | ||
289 | const char *ticket_str; | ||
290 | const char *signature_str; | ||
291 | const char *nonce_str; | ||
292 | char *code_output; | ||
293 | struct GNUNET_CRYPTO_EccSignaturePurpose *purpose; | ||
294 | struct GNUNET_CRYPTO_EcdsaSignature signature; | ||
295 | size_t signature_payload_len; | ||
296 | |||
297 | code_output = NULL; | ||
298 | GNUNET_STRINGS_base64_decode (code, | ||
299 | strlen(code), | ||
300 | (void**)&code_output); | ||
301 | code_json = json_loads (code_output, 0 , &error); | ||
302 | GNUNET_free (code_output); | ||
303 | ticket_json = json_object_get (code_json, "ticket"); | ||
304 | nonce_json = json_object_get (code_json, "nonce"); | ||
305 | signature_json = json_object_get (code_json, "signature"); | ||
306 | *ticket = NULL; | ||
307 | *nonce = NULL; | ||
308 | |||
309 | if ((NULL == ticket_json || !json_is_string (ticket_json)) || | ||
310 | (NULL == signature_json || !json_is_string (signature_json))) | ||
311 | { | ||
312 | json_decref (code_json); | ||
313 | return GNUNET_SYSERR; | ||
314 | } | ||
315 | ticket_str = json_string_value (ticket_json); | ||
316 | signature_str = json_string_value (signature_json); | ||
317 | nonce_str = NULL; | ||
318 | if (NULL != nonce_json) | ||
319 | nonce_str = json_string_value (nonce_json); | ||
320 | signature_payload_len = sizeof (struct GNUNET_RECLAIM_Ticket); | ||
321 | if (NULL != nonce_str) | ||
322 | signature_payload_len += strlen (nonce_str); | ||
323 | purpose = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) + | ||
324 | signature_payload_len); | ||
325 | purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) + signature_payload_len); | ||
326 | purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_RECLAIM_CODE_SIGN); | ||
327 | if (GNUNET_OK != GNUNET_STRINGS_string_to_data (ticket_str, | ||
328 | strlen (ticket_str), | ||
329 | &purpose[1], | ||
330 | sizeof (struct GNUNET_RECLAIM_Ticket))) | ||
331 | { | ||
332 | GNUNET_free (purpose); | ||
333 | json_decref (code_json); | ||
334 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
335 | "Cannot parse ticket!\n"); | ||
336 | return GNUNET_SYSERR; | ||
337 | } | ||
338 | if (GNUNET_OK != GNUNET_STRINGS_string_to_data (signature_str, | ||
339 | strlen (signature_str), | ||
340 | &signature, | ||
341 | sizeof (struct GNUNET_CRYPTO_EcdsaSignature))) | ||
342 | { | ||
343 | GNUNET_free (purpose); | ||
344 | json_decref (code_json); | ||
345 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
346 | "Cannot parse signature!\n"); | ||
347 | return GNUNET_SYSERR; | ||
348 | } | ||
349 | *ticket = GNUNET_new (struct GNUNET_RECLAIM_Ticket); | ||
350 | memcpy (*ticket, | ||
351 | &purpose[1], | ||
352 | sizeof (struct GNUNET_RECLAIM_Ticket)); | ||
353 | if (0 != memcmp (audience, | ||
354 | &(*ticket)->audience, | ||
355 | sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))) | ||
356 | { | ||
357 | GNUNET_free (purpose); | ||
358 | GNUNET_free (*ticket); | ||
359 | json_decref (code_json); | ||
360 | *ticket = NULL; | ||
361 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
362 | "Audience in ticket does not match client!\n"); | ||
363 | return GNUNET_SYSERR; | ||
364 | |||
365 | } | ||
366 | if (NULL != nonce_str) | ||
367 | memcpy (((char*)&purpose[1]) + sizeof (struct GNUNET_RECLAIM_Ticket), | ||
368 | nonce_str, | ||
369 | strlen (nonce_str)); | ||
370 | if (GNUNET_OK != GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_RECLAIM_CODE_SIGN, | ||
371 | purpose, | ||
372 | &signature, | ||
373 | &(*ticket)->identity)) | ||
374 | { | ||
375 | GNUNET_free (purpose); | ||
376 | GNUNET_free (*ticket); | ||
377 | json_decref (code_json); | ||
378 | *ticket = NULL; | ||
379 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
380 | "Signature of authZ code invalid!\n"); | ||
381 | return GNUNET_SYSERR; | ||
382 | } | ||
383 | *nonce = GNUNET_strdup (nonce_str); | ||
384 | return GNUNET_OK; | ||
385 | } | ||
386 | |||
387 | /** | ||
388 | * Build a token response for a token request | ||
389 | * TODO: Maybe we should add the scope here? | ||
390 | * | ||
391 | * @param access_token the access token to include | ||
392 | * @param id_token the id_token to include | ||
393 | * @param expiration_time the expiration time of the token(s) | ||
394 | * @param token_response where to store the response | ||
395 | */ | ||
396 | void | ||
397 | OIDC_build_token_response (const char *access_token, | ||
398 | const char *id_token, | ||
399 | const struct GNUNET_TIME_Relative *expiration_time, | ||
400 | char **token_response) | ||
401 | { | ||
402 | json_t *root_json; | ||
403 | |||
404 | root_json = json_object (); | ||
405 | |||
406 | GNUNET_assert (NULL != access_token); | ||
407 | GNUNET_assert (NULL != id_token); | ||
408 | GNUNET_assert (NULL != expiration_time); | ||
409 | json_object_set_new (root_json, | ||
410 | "access_token", | ||
411 | json_string (access_token)); | ||
412 | json_object_set_new (root_json, | ||
413 | "token_type", | ||
414 | json_string ("Bearer")); | ||
415 | json_object_set_new (root_json, | ||
416 | "expires_in", | ||
417 | json_integer (expiration_time->rel_value_us / (1000 * 1000))); | ||
418 | json_object_set_new (root_json, | ||
419 | "id_token", | ||
420 | json_string (id_token)); | ||
421 | *token_response = json_dumps (root_json, | ||
422 | JSON_INDENT(0) | JSON_COMPACT); | ||
423 | json_decref (root_json); | ||
424 | } | ||
425 | |||
426 | /** | ||
427 | * Generate a new access token | ||
428 | */ | ||
429 | char* | ||
430 | OIDC_access_token_new () | ||
431 | { | ||
432 | char* access_token_number; | ||
433 | char* access_token; | ||
434 | uint64_t random_number; | ||
435 | |||
436 | random_number = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX); | ||
437 | GNUNET_asprintf (&access_token_number, "%" PRIu64, random_number); | ||
438 | GNUNET_STRINGS_base64_encode(access_token_number,strlen(access_token_number),&access_token); | ||
439 | return access_token; | ||
440 | } | ||
diff --git a/src/rest-plugins/oidc_helper.h b/src/rest-plugins/oidc_helper.h new file mode 100644 index 000000000..7a0f45bf9 --- /dev/null +++ b/src/rest-plugins/oidc_helper.h | |||
@@ -0,0 +1,109 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2010-2015 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | /** | ||
20 | * @file reclaim/oidc_helper.h | ||
21 | * @brief helper library for OIDC related functions | ||
22 | * @author Martin Schanzenbach | ||
23 | */ | ||
24 | |||
25 | #ifndef JWT_H | ||
26 | #define JWT_H | ||
27 | |||
28 | #define JWT_ALG "alg" | ||
29 | |||
30 | /* Use 512bit HMAC */ | ||
31 | #define JWT_ALG_VALUE "HS512" | ||
32 | |||
33 | #define JWT_TYP "typ" | ||
34 | |||
35 | #define JWT_TYP_VALUE "jwt" | ||
36 | |||
37 | #define SERVER_ADDRESS "https://reclaim.id" | ||
38 | |||
39 | /** | ||
40 | * Create a JWT from attributes | ||
41 | * | ||
42 | * @param aud_key the public of the audience | ||
43 | * @param sub_key the public key of the subject | ||
44 | * @param attrs the attribute list | ||
45 | * @param expiration_time the validity of the token | ||
46 | * @param secret_key the key used to sign the JWT | ||
47 | * @return a new base64-encoded JWT string. | ||
48 | */ | ||
49 | char* | ||
50 | OIDC_id_token_new (const struct GNUNET_CRYPTO_EcdsaPublicKey *aud_key, | ||
51 | const struct GNUNET_CRYPTO_EcdsaPublicKey *sub_key, | ||
52 | const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs, | ||
53 | const struct GNUNET_TIME_Relative *expiration_time, | ||
54 | const char *nonce, | ||
55 | const char *secret_key); | ||
56 | |||
57 | /** | ||
58 | * Builds an OIDC authorization code including | ||
59 | * a reclaim ticket and nonce | ||
60 | * | ||
61 | * @param issuer the issuer of the ticket, used to sign the ticket and nonce | ||
62 | * @param ticket the ticket to include in the code | ||
63 | * @param nonce the nonce to include in the code | ||
64 | * @return a new authorization code (caller must free) | ||
65 | */ | ||
66 | char* | ||
67 | OIDC_build_authz_code (const struct GNUNET_CRYPTO_EcdsaPrivateKey *issuer, | ||
68 | const struct GNUNET_RECLAIM_Ticket *ticket, | ||
69 | const char* nonce); | ||
70 | |||
71 | /** | ||
72 | * Parse reclaim ticket and nonce from | ||
73 | * authorization code. | ||
74 | * This also verifies the signature in the code. | ||
75 | * | ||
76 | * @param audience the expected audience of the code | ||
77 | * @param code the string representation of the code | ||
78 | * @param ticket where to store the ticket | ||
79 | * @param nonce where to store the nonce | ||
80 | * @return GNUNET_OK if successful, else GNUNET_SYSERR | ||
81 | */ | ||
82 | int | ||
83 | OIDC_parse_authz_code (const struct GNUNET_CRYPTO_EcdsaPublicKey *audience, | ||
84 | const char* code, | ||
85 | struct GNUNET_RECLAIM_Ticket **ticket, | ||
86 | char **nonce); | ||
87 | |||
88 | /** | ||
89 | * Build a token response for a token request | ||
90 | * TODO: Maybe we should add the scope here? | ||
91 | * | ||
92 | * @param access_token the access token to include | ||
93 | * @param id_token the id_token to include | ||
94 | * @param expiration_time the expiration time of the token(s) | ||
95 | * @param token_response where to store the response | ||
96 | */ | ||
97 | void | ||
98 | OIDC_build_token_response (const char *access_token, | ||
99 | const char *id_token, | ||
100 | const struct GNUNET_TIME_Relative *expiration_time, | ||
101 | char **token_response); | ||
102 | /** | ||
103 | * Generate a new access token | ||
104 | */ | ||
105 | char* | ||
106 | OIDC_access_token_new (); | ||
107 | |||
108 | |||
109 | #endif | ||
diff --git a/src/rest-plugins/plugin_rest_copying.c b/src/rest-plugins/plugin_rest_copying.c new file mode 100644 index 000000000..668dc5d38 --- /dev/null +++ b/src/rest-plugins/plugin_rest_copying.c | |||
@@ -0,0 +1,231 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2012-2018 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | /** | ||
19 | * @author Martin Schanzenbach | ||
20 | * @file gns/plugin_rest_copying.c | ||
21 | * @brief REST plugin that serves licensing information. | ||
22 | * | ||
23 | */ | ||
24 | |||
25 | #include "platform.h" | ||
26 | #include "gnunet_rest_plugin.h" | ||
27 | #include <gnunet_rest_lib.h> | ||
28 | |||
29 | #define GNUNET_REST_API_NS_COPYING "/copying" | ||
30 | |||
31 | #define GNUNET_REST_COPYING_TEXT "GNU Affero General Public License version 3 or later. See also: <http://www.gnu.org/licenses/>" | ||
32 | |||
33 | /** | ||
34 | * @brief struct returned by the initialization function of the plugin | ||
35 | */ | ||
36 | struct Plugin | ||
37 | { | ||
38 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
39 | }; | ||
40 | |||
41 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
42 | |||
43 | struct RequestHandle | ||
44 | { | ||
45 | /** | ||
46 | * Handle to rest request | ||
47 | */ | ||
48 | struct GNUNET_REST_RequestHandle *rest_handle; | ||
49 | |||
50 | /** | ||
51 | * The plugin result processor | ||
52 | */ | ||
53 | GNUNET_REST_ResultProcessor proc; | ||
54 | |||
55 | /** | ||
56 | * The closure of the result processor | ||
57 | */ | ||
58 | void *proc_cls; | ||
59 | |||
60 | /** | ||
61 | * HTTP response code | ||
62 | */ | ||
63 | int response_code; | ||
64 | |||
65 | }; | ||
66 | |||
67 | |||
68 | /** | ||
69 | * Cleanup request handle. | ||
70 | * | ||
71 | * @param handle Handle to clean up | ||
72 | */ | ||
73 | static void | ||
74 | cleanup_handle (struct RequestHandle *handle) | ||
75 | { | ||
76 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
77 | "Cleaning up\n"); | ||
78 | GNUNET_free (handle); | ||
79 | } | ||
80 | |||
81 | |||
82 | /** | ||
83 | * Task run on shutdown. Cleans up everything. | ||
84 | * | ||
85 | * @param cls unused | ||
86 | * @param tc scheduler context | ||
87 | */ | ||
88 | static void | ||
89 | do_error (void *cls) | ||
90 | { | ||
91 | struct RequestHandle *handle = cls; | ||
92 | struct MHD_Response *resp; | ||
93 | |||
94 | resp = GNUNET_REST_create_response (NULL); | ||
95 | handle->proc (handle->proc_cls, resp, handle->response_code); | ||
96 | cleanup_handle (handle); | ||
97 | } | ||
98 | |||
99 | |||
100 | /** | ||
101 | * Handle rest request | ||
102 | * | ||
103 | * @param handle the lookup handle | ||
104 | */ | ||
105 | static void | ||
106 | get_cont (struct GNUNET_REST_RequestHandle *con_handle, | ||
107 | const char* url, | ||
108 | void *cls) | ||
109 | { | ||
110 | struct MHD_Response *resp; | ||
111 | struct RequestHandle *handle = cls; | ||
112 | |||
113 | resp = GNUNET_REST_create_response (GNUNET_REST_COPYING_TEXT); | ||
114 | handle->proc (handle->proc_cls, | ||
115 | resp, | ||
116 | MHD_HTTP_OK); | ||
117 | cleanup_handle (handle); | ||
118 | } | ||
119 | |||
120 | |||
121 | |||
122 | /** | ||
123 | * Handle rest request | ||
124 | * | ||
125 | * @param handle the lookup handle | ||
126 | */ | ||
127 | static void | ||
128 | options_cont (struct GNUNET_REST_RequestHandle *con_handle, | ||
129 | const char* url, | ||
130 | void *cls) | ||
131 | { | ||
132 | struct MHD_Response *resp; | ||
133 | struct RequestHandle *handle = cls; | ||
134 | |||
135 | resp = GNUNET_REST_create_response (NULL); | ||
136 | MHD_add_response_header (resp, | ||
137 | "Access-Control-Allow-Methods", | ||
138 | MHD_HTTP_METHOD_GET); | ||
139 | handle->proc (handle->proc_cls, | ||
140 | resp, | ||
141 | MHD_HTTP_OK); | ||
142 | cleanup_handle (handle); | ||
143 | } | ||
144 | |||
145 | |||
146 | /** | ||
147 | * Function processing the REST call | ||
148 | * | ||
149 | * @param method HTTP method | ||
150 | * @param url URL of the HTTP request | ||
151 | * @param data body of the HTTP request (optional) | ||
152 | * @param data_size length of the body | ||
153 | * @param proc callback function for the result | ||
154 | * @param proc_cls closure for @a proc | ||
155 | * @return #GNUNET_OK if request accepted | ||
156 | */ | ||
157 | static void | ||
158 | rest_copying_process_request (struct GNUNET_REST_RequestHandle *conndata_handle, | ||
159 | GNUNET_REST_ResultProcessor proc, | ||
160 | void *proc_cls) | ||
161 | { | ||
162 | static const struct GNUNET_REST_RequestHandler handlers[] = { | ||
163 | {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_COPYING, &get_cont}, | ||
164 | {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_COPYING, &options_cont}, | ||
165 | GNUNET_REST_HANDLER_END | ||
166 | }; | ||
167 | struct RequestHandle *handle = GNUNET_new (struct RequestHandle); | ||
168 | struct GNUNET_REST_RequestHandlerError err; | ||
169 | |||
170 | handle->proc_cls = proc_cls; | ||
171 | handle->proc = proc; | ||
172 | handle->rest_handle = conndata_handle; | ||
173 | |||
174 | if (GNUNET_NO == GNUNET_REST_handle_request (conndata_handle, | ||
175 | handlers, | ||
176 | &err, | ||
177 | handle)) | ||
178 | { | ||
179 | handle->response_code = err.error_code; | ||
180 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
181 | } | ||
182 | } | ||
183 | |||
184 | |||
185 | /** | ||
186 | * Entry point for the plugin. | ||
187 | * | ||
188 | * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*" | ||
189 | * @return NULL on error, otherwise the plugin context | ||
190 | */ | ||
191 | void * | ||
192 | libgnunet_plugin_rest_copying_init (void *cls) | ||
193 | { | ||
194 | static struct Plugin plugin; | ||
195 | cfg = cls; | ||
196 | struct GNUNET_REST_Plugin *api; | ||
197 | |||
198 | if (NULL != plugin.cfg) | ||
199 | return NULL; /* can only initialize once! */ | ||
200 | memset (&plugin, 0, sizeof (struct Plugin)); | ||
201 | plugin.cfg = cfg; | ||
202 | api = GNUNET_new (struct GNUNET_REST_Plugin); | ||
203 | api->cls = &plugin; | ||
204 | api->name = GNUNET_REST_API_NS_COPYING; | ||
205 | api->process_request = &rest_copying_process_request; | ||
206 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
207 | _("COPYING REST API initialized\n")); | ||
208 | return api; | ||
209 | } | ||
210 | |||
211 | |||
212 | /** | ||
213 | * Exit point from the plugin. | ||
214 | * | ||
215 | * @param cls the plugin context (as returned by "init") | ||
216 | * @return always NULL | ||
217 | */ | ||
218 | void * | ||
219 | libgnunet_plugin_rest_copying_done (void *cls) | ||
220 | { | ||
221 | struct GNUNET_REST_Plugin *api = cls; | ||
222 | struct Plugin *plugin = api->cls; | ||
223 | |||
224 | plugin->cfg = NULL; | ||
225 | GNUNET_free (api); | ||
226 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
227 | "COPYING REST plugin is finished\n"); | ||
228 | return NULL; | ||
229 | } | ||
230 | |||
231 | /* end of plugin_rest_copying.c */ | ||
diff --git a/src/rest-plugins/plugin_rest_gns.c b/src/rest-plugins/plugin_rest_gns.c new file mode 100644 index 000000000..0bf4198fc --- /dev/null +++ b/src/rest-plugins/plugin_rest_gns.c | |||
@@ -0,0 +1,476 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2012-2015 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | /** | ||
19 | * @author Philippe Buschmann | ||
20 | * @file gns/plugin_rest_gns.c | ||
21 | * @brief GNUnet Gns REST plugin | ||
22 | */ | ||
23 | |||
24 | #include "platform.h" | ||
25 | #include "gnunet_rest_plugin.h" | ||
26 | #include "gnunet_rest_lib.h" | ||
27 | #include "gnunet_json_lib.h" | ||
28 | #include "gnunet_gnsrecord_lib.h" | ||
29 | #include "gnunet_gns_service.h" | ||
30 | #include "microhttpd.h" | ||
31 | #include <jansson.h> | ||
32 | |||
33 | /** | ||
34 | * Rest API GNS Namespace | ||
35 | */ | ||
36 | #define GNUNET_REST_API_NS_GNS "/gns" | ||
37 | |||
38 | /** | ||
39 | * Rest API GNS Parameter record_type | ||
40 | */ | ||
41 | #define GNUNET_REST_GNS_PARAM_RECORD_TYPE "record_type" | ||
42 | |||
43 | /** | ||
44 | * Rest API GNS ERROR Unknown Error | ||
45 | */ | ||
46 | #define GNUNET_REST_GNS_ERROR_UNKNOWN "Unknown Error" | ||
47 | |||
48 | /** | ||
49 | * Rest API GNS ERROR Record not found | ||
50 | */ | ||
51 | #define GNUNET_REST_GNS_NOT_FOUND "Record not found" | ||
52 | |||
53 | /** | ||
54 | * The configuration handle | ||
55 | */ | ||
56 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
57 | |||
58 | /** | ||
59 | * HTTP methods allows for this plugin | ||
60 | */ | ||
61 | static char* allow_methods; | ||
62 | |||
63 | /** | ||
64 | * @brief struct returned by the initialization function of the plugin | ||
65 | */ | ||
66 | struct Plugin | ||
67 | { | ||
68 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
69 | }; | ||
70 | |||
71 | /** | ||
72 | * The request handle | ||
73 | */ | ||
74 | struct RequestHandle | ||
75 | { | ||
76 | |||
77 | /** | ||
78 | * Connection to GNS | ||
79 | */ | ||
80 | struct GNUNET_GNS_Handle *gns; | ||
81 | |||
82 | /** | ||
83 | * Active GNS lookup | ||
84 | */ | ||
85 | struct GNUNET_GNS_LookupWithTldRequest *gns_lookup; | ||
86 | |||
87 | /** | ||
88 | * Name to look up | ||
89 | */ | ||
90 | char *name; | ||
91 | |||
92 | /** | ||
93 | * Record type to look up | ||
94 | */ | ||
95 | int record_type; | ||
96 | |||
97 | /** | ||
98 | * Rest connection | ||
99 | */ | ||
100 | struct GNUNET_REST_RequestHandle *rest_handle; | ||
101 | |||
102 | /** | ||
103 | * Desired timeout for the lookup (default is no timeout). | ||
104 | */ | ||
105 | struct GNUNET_TIME_Relative timeout; | ||
106 | |||
107 | /** | ||
108 | * ID of a task associated with the resolution process. | ||
109 | */ | ||
110 | struct GNUNET_SCHEDULER_Task *timeout_task; | ||
111 | |||
112 | /** | ||
113 | * The plugin result processor | ||
114 | */ | ||
115 | GNUNET_REST_ResultProcessor proc; | ||
116 | |||
117 | /** | ||
118 | * The closure of the result processor | ||
119 | */ | ||
120 | void *proc_cls; | ||
121 | |||
122 | /** | ||
123 | * The url | ||
124 | */ | ||
125 | char *url; | ||
126 | |||
127 | /** | ||
128 | * Error response message | ||
129 | */ | ||
130 | char *emsg; | ||
131 | |||
132 | /** | ||
133 | * Response code | ||
134 | */ | ||
135 | int response_code; | ||
136 | |||
137 | }; | ||
138 | |||
139 | |||
140 | /** | ||
141 | * Cleanup lookup handle | ||
142 | * @param handle Handle to clean up | ||
143 | */ | ||
144 | static void | ||
145 | cleanup_handle (void *cls) | ||
146 | { | ||
147 | struct RequestHandle *handle = cls; | ||
148 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
149 | "Cleaning up\n"); | ||
150 | |||
151 | if (NULL != handle->gns_lookup) | ||
152 | { | ||
153 | GNUNET_GNS_lookup_with_tld_cancel (handle->gns_lookup); | ||
154 | handle->gns_lookup = NULL; | ||
155 | } | ||
156 | if (NULL != handle->gns) | ||
157 | { | ||
158 | GNUNET_GNS_disconnect (handle->gns); | ||
159 | handle->gns = NULL; | ||
160 | } | ||
161 | |||
162 | if (NULL != handle->timeout_task) | ||
163 | { | ||
164 | GNUNET_SCHEDULER_cancel (handle->timeout_task); | ||
165 | handle->timeout_task = NULL; | ||
166 | } | ||
167 | if (NULL != handle->url) | ||
168 | GNUNET_free (handle->url); | ||
169 | if (NULL != handle->name) | ||
170 | GNUNET_free (handle->name); | ||
171 | if (NULL != handle->emsg) | ||
172 | GNUNET_free (handle->emsg); | ||
173 | |||
174 | GNUNET_free (handle); | ||
175 | } | ||
176 | |||
177 | |||
178 | /** | ||
179 | * Task run on errors. Reports an error and cleans up everything. | ||
180 | * | ||
181 | * @param cls the `struct RequestHandle` | ||
182 | */ | ||
183 | static void | ||
184 | do_error (void *cls) | ||
185 | { | ||
186 | struct RequestHandle *handle = cls; | ||
187 | struct MHD_Response *resp; | ||
188 | json_t *json_error = json_object(); | ||
189 | char *response; | ||
190 | |||
191 | if (NULL == handle->emsg) | ||
192 | handle->emsg = GNUNET_strdup(GNUNET_REST_GNS_ERROR_UNKNOWN); | ||
193 | |||
194 | json_object_set_new(json_error,"error", json_string(handle->emsg)); | ||
195 | |||
196 | if (0 == handle->response_code) | ||
197 | handle->response_code = MHD_HTTP_OK; | ||
198 | response = json_dumps (json_error, 0); | ||
199 | resp = GNUNET_REST_create_response (response); | ||
200 | handle->proc (handle->proc_cls, resp, handle->response_code); | ||
201 | json_decref(json_error); | ||
202 | GNUNET_free(response); | ||
203 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
204 | } | ||
205 | |||
206 | |||
207 | /** | ||
208 | * Iterator called on obtained result for a GNS lookup. | ||
209 | * | ||
210 | * @param cls closure with the object | ||
211 | * @param was_gns #GNUNET_NO if name was not a GNS name | ||
212 | * @param rd_count number of records in @a rd | ||
213 | * @param rd the records in reply | ||
214 | */ | ||
215 | static void | ||
216 | handle_gns_response (void *cls, | ||
217 | int was_gns, | ||
218 | uint32_t rd_count, | ||
219 | const struct GNUNET_GNSRECORD_Data *rd) | ||
220 | { | ||
221 | struct RequestHandle *handle = cls; | ||
222 | struct MHD_Response *resp; | ||
223 | json_t *result_array; | ||
224 | json_t *record_obj; | ||
225 | char *result; | ||
226 | |||
227 | handle->gns_lookup = NULL; | ||
228 | |||
229 | if (GNUNET_NO == was_gns) | ||
230 | { | ||
231 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
232 | handle->emsg = GNUNET_strdup(GNUNET_REST_GNS_NOT_FOUND); | ||
233 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
234 | return; | ||
235 | } | ||
236 | |||
237 | result_array = json_array(); | ||
238 | for (uint32_t i=0;i<rd_count;i++) | ||
239 | { | ||
240 | if ((rd[i].record_type != handle->record_type) && | ||
241 | (GNUNET_GNSRECORD_TYPE_ANY != handle->record_type) ) | ||
242 | { | ||
243 | continue; | ||
244 | } | ||
245 | |||
246 | record_obj = GNUNET_JSON_from_gns_record(NULL,&rd[i]); | ||
247 | json_array_append (result_array, record_obj); | ||
248 | json_decref (record_obj); | ||
249 | } | ||
250 | |||
251 | result = json_dumps(result_array, 0); | ||
252 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result); | ||
253 | resp = GNUNET_REST_create_response (result); | ||
254 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | ||
255 | GNUNET_free (result); | ||
256 | json_decref (result_array); | ||
257 | GNUNET_SCHEDULER_add_now(&cleanup_handle, handle); | ||
258 | } | ||
259 | |||
260 | |||
261 | /** | ||
262 | * Handle gns GET request | ||
263 | * | ||
264 | * @param con_handle the connection handle | ||
265 | * @param url the url | ||
266 | * @param cls the RequestHandle | ||
267 | */ | ||
268 | void | ||
269 | get_gns_cont (struct GNUNET_REST_RequestHandle *con_handle, | ||
270 | const char* url, | ||
271 | void *cls) | ||
272 | { | ||
273 | struct RequestHandle *handle = cls; | ||
274 | struct GNUNET_HashCode key; | ||
275 | char *record_type; | ||
276 | char *name; | ||
277 | |||
278 | name = NULL; | ||
279 | handle->name = NULL; | ||
280 | if (strlen (GNUNET_REST_API_NS_GNS) < strlen (handle->url)) | ||
281 | { | ||
282 | name = &handle->url[strlen (GNUNET_REST_API_NS_GNS) + 1]; | ||
283 | } | ||
284 | |||
285 | if (NULL == name) | ||
286 | { | ||
287 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
288 | handle->emsg = GNUNET_strdup(GNUNET_REST_GNS_NOT_FOUND); | ||
289 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
290 | return; | ||
291 | } | ||
292 | if (0 >= strlen (name)) | ||
293 | { | ||
294 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
295 | handle->emsg = GNUNET_strdup(GNUNET_REST_GNS_NOT_FOUND); | ||
296 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
297 | return; | ||
298 | } | ||
299 | handle->name = GNUNET_strdup(name); | ||
300 | |||
301 | handle->record_type = UINT32_MAX; | ||
302 | GNUNET_CRYPTO_hash (GNUNET_REST_GNS_PARAM_RECORD_TYPE, | ||
303 | strlen (GNUNET_REST_GNS_PARAM_RECORD_TYPE), | ||
304 | &key); | ||
305 | if ( GNUNET_YES | ||
306 | == GNUNET_CONTAINER_multihashmap_contains (con_handle->url_param_map, | ||
307 | &key)) | ||
308 | { | ||
309 | record_type = GNUNET_CONTAINER_multihashmap_get (con_handle->url_param_map, &key); | ||
310 | handle->record_type = GNUNET_GNSRECORD_typename_to_number(record_type); | ||
311 | } | ||
312 | |||
313 | if(UINT32_MAX == handle->record_type) | ||
314 | { | ||
315 | handle->record_type = GNUNET_GNSRECORD_TYPE_ANY; | ||
316 | } | ||
317 | |||
318 | handle->gns_lookup = GNUNET_GNS_lookup_with_tld (handle->gns, | ||
319 | handle->name, | ||
320 | handle->record_type, | ||
321 | GNUNET_NO, | ||
322 | &handle_gns_response, | ||
323 | handle); | ||
324 | } | ||
325 | |||
326 | |||
327 | |||
328 | /** | ||
329 | * Respond to OPTIONS request | ||
330 | * | ||
331 | * @param con_handle the connection handle | ||
332 | * @param url the url | ||
333 | * @param cls the RequestHandle | ||
334 | */ | ||
335 | static void | ||
336 | options_cont (struct GNUNET_REST_RequestHandle *con_handle, | ||
337 | const char* url, | ||
338 | void *cls) | ||
339 | { | ||
340 | struct MHD_Response *resp; | ||
341 | struct RequestHandle *handle = cls; | ||
342 | |||
343 | //independent of path return all options | ||
344 | resp = GNUNET_REST_create_response (NULL); | ||
345 | MHD_add_response_header (resp, | ||
346 | "Access-Control-Allow-Methods", | ||
347 | allow_methods); | ||
348 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | ||
349 | GNUNET_SCHEDULER_add_now(&cleanup_handle, handle); | ||
350 | return; | ||
351 | } | ||
352 | |||
353 | |||
354 | /** | ||
355 | * Handle rest request | ||
356 | * | ||
357 | * @param handle the request handle | ||
358 | */ | ||
359 | static void | ||
360 | init_cont (struct RequestHandle *handle) | ||
361 | { | ||
362 | struct GNUNET_REST_RequestHandlerError err; | ||
363 | static const struct GNUNET_REST_RequestHandler handlers[] = { | ||
364 | {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_GNS, &get_gns_cont}, | ||
365 | {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_GNS, &options_cont}, | ||
366 | GNUNET_REST_HANDLER_END | ||
367 | }; | ||
368 | |||
369 | if (GNUNET_NO == GNUNET_REST_handle_request (handle->rest_handle, | ||
370 | handlers, | ||
371 | &err, | ||
372 | handle)) | ||
373 | { | ||
374 | handle->response_code = err.error_code; | ||
375 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
376 | } | ||
377 | } | ||
378 | |||
379 | |||
380 | /** | ||
381 | * Function processing the REST call | ||
382 | * | ||
383 | * @param method HTTP method | ||
384 | * @param url URL of the HTTP request | ||
385 | * @param data body of the HTTP request (optional) | ||
386 | * @param data_size length of the body | ||
387 | * @param proc callback function for the result | ||
388 | * @param proc_cls closure for callback function | ||
389 | * @return GNUNET_OK if request accepted | ||
390 | */ | ||
391 | static void | ||
392 | rest_process_request(struct GNUNET_REST_RequestHandle *rest_handle, | ||
393 | GNUNET_REST_ResultProcessor proc, | ||
394 | void *proc_cls) | ||
395 | { | ||
396 | struct RequestHandle *handle = GNUNET_new (struct RequestHandle); | ||
397 | |||
398 | handle->response_code = 0; | ||
399 | handle->timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60); | ||
400 | handle->proc_cls = proc_cls; | ||
401 | handle->proc = proc; | ||
402 | handle->rest_handle = rest_handle; | ||
403 | |||
404 | handle->url = GNUNET_strdup (rest_handle->url); | ||
405 | if (handle->url[strlen (handle->url)-1] == '/') | ||
406 | handle->url[strlen (handle->url)-1] = '\0'; | ||
407 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n"); | ||
408 | handle->gns = GNUNET_GNS_connect (cfg); | ||
409 | init_cont(handle); | ||
410 | |||
411 | handle->timeout_task = | ||
412 | GNUNET_SCHEDULER_add_delayed (handle->timeout, | ||
413 | &do_error, | ||
414 | handle); | ||
415 | |||
416 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected\n"); | ||
417 | } | ||
418 | |||
419 | |||
420 | /** | ||
421 | * Entry point for the plugin. | ||
422 | * | ||
423 | * @param cls Config info | ||
424 | * @return NULL on error, otherwise the plugin context | ||
425 | */ | ||
426 | void * | ||
427 | libgnunet_plugin_rest_gns_init (void *cls) | ||
428 | { | ||
429 | static struct Plugin plugin; | ||
430 | struct GNUNET_REST_Plugin *api; | ||
431 | |||
432 | cfg = cls; | ||
433 | if (NULL != plugin.cfg) | ||
434 | return NULL; /* can only initialize once! */ | ||
435 | memset (&plugin, 0, sizeof (struct Plugin)); | ||
436 | plugin.cfg = cfg; | ||
437 | api = GNUNET_new (struct GNUNET_REST_Plugin); | ||
438 | api->cls = &plugin; | ||
439 | api->name = GNUNET_REST_API_NS_GNS; | ||
440 | api->process_request = &rest_process_request; | ||
441 | GNUNET_asprintf (&allow_methods, | ||
442 | "%s, %s, %s, %s, %s", | ||
443 | MHD_HTTP_METHOD_GET, | ||
444 | MHD_HTTP_METHOD_POST, | ||
445 | MHD_HTTP_METHOD_PUT, | ||
446 | MHD_HTTP_METHOD_DELETE, | ||
447 | MHD_HTTP_METHOD_OPTIONS); | ||
448 | |||
449 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
450 | _("Gns REST API initialized\n")); | ||
451 | return api; | ||
452 | } | ||
453 | |||
454 | |||
455 | /** | ||
456 | * Exit point from the plugin. | ||
457 | * | ||
458 | * @param cls the plugin context (as returned by "init") | ||
459 | * @return always NULL | ||
460 | */ | ||
461 | void * | ||
462 | libgnunet_plugin_rest_gns_done (void *cls) | ||
463 | { | ||
464 | struct GNUNET_REST_Plugin *api = cls; | ||
465 | struct Plugin *plugin = api->cls; | ||
466 | plugin->cfg = NULL; | ||
467 | |||
468 | GNUNET_free_non_null (allow_methods); | ||
469 | GNUNET_free (api); | ||
470 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
471 | "Gns REST plugin is finished\n"); | ||
472 | return NULL; | ||
473 | } | ||
474 | |||
475 | /* end of plugin_rest_gns.c */ | ||
476 | |||
diff --git a/src/rest-plugins/plugin_rest_identity.c b/src/rest-plugins/plugin_rest_identity.c new file mode 100644 index 000000000..9f1765a63 --- /dev/null +++ b/src/rest-plugins/plugin_rest_identity.c | |||
@@ -0,0 +1,1319 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2012-2015 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | /** | ||
19 | * @author Martin Schanzenbach | ||
20 | * @author Philippe Buschmann | ||
21 | * @file identity/plugin_rest_identity.c | ||
22 | * @brief GNUnet Identity REST plugin | ||
23 | */ | ||
24 | |||
25 | #include "platform.h" | ||
26 | #include "gnunet_rest_plugin.h" | ||
27 | #include "gnunet_identity_service.h" | ||
28 | #include "gnunet_rest_lib.h" | ||
29 | #include "microhttpd.h" | ||
30 | #include <jansson.h> | ||
31 | |||
32 | /** | ||
33 | * Identity Namespace | ||
34 | */ | ||
35 | #define GNUNET_REST_API_NS_IDENTITY "/identity" | ||
36 | |||
37 | /** | ||
38 | * Identity Namespace with public key specifier | ||
39 | */ | ||
40 | #define GNUNET_REST_API_NS_IDENTITY_ALL "/identity/all" | ||
41 | |||
42 | /** | ||
43 | * Identity Namespace with public key specifier | ||
44 | */ | ||
45 | #define GNUNET_REST_API_NS_IDENTITY_PUBKEY "/identity/pubkey" | ||
46 | |||
47 | /** | ||
48 | * Identity Namespace with public key specifier | ||
49 | */ | ||
50 | #define GNUNET_REST_API_NS_IDENTITY_NAME "/identity/name" | ||
51 | |||
52 | /** | ||
53 | * Identity Subsystem Namespace | ||
54 | */ | ||
55 | #define GNUNET_REST_API_NS_IDENTITY_SUBSYSTEM "/identity/subsystem" | ||
56 | |||
57 | /** | ||
58 | * Parameter public key | ||
59 | */ | ||
60 | #define GNUNET_REST_IDENTITY_PARAM_PUBKEY "pubkey" | ||
61 | |||
62 | /** | ||
63 | * Parameter subsystem | ||
64 | */ | ||
65 | #define GNUNET_REST_IDENTITY_PARAM_SUBSYSTEM "subsystem" | ||
66 | |||
67 | /** | ||
68 | * Parameter name | ||
69 | */ | ||
70 | #define GNUNET_REST_IDENTITY_PARAM_NAME "name" | ||
71 | |||
72 | /** | ||
73 | * Parameter new name | ||
74 | */ | ||
75 | #define GNUNET_REST_IDENTITY_PARAM_NEWNAME "newname" | ||
76 | |||
77 | /** | ||
78 | * Error message Unknown Error | ||
79 | */ | ||
80 | #define GNUNET_REST_IDENTITY_ERROR_UNKNOWN "Unknown Error" | ||
81 | |||
82 | /** | ||
83 | * Error message No identity found | ||
84 | */ | ||
85 | #define GNUNET_REST_IDENTITY_NOT_FOUND "No identity found" | ||
86 | |||
87 | /** | ||
88 | * Error message Missing identity name | ||
89 | */ | ||
90 | #define GNUNET_REST_IDENTITY_MISSING_NAME "Missing identity name" | ||
91 | |||
92 | /** | ||
93 | * Error message Missing identity name | ||
94 | */ | ||
95 | #define GNUNET_REST_IDENTITY_MISSING_PUBKEY "Missing identity public key" | ||
96 | |||
97 | /** | ||
98 | * Error message No data | ||
99 | */ | ||
100 | #define GNUNET_REST_ERROR_NO_DATA "No data" | ||
101 | |||
102 | /** | ||
103 | * Error message Data invalid | ||
104 | */ | ||
105 | #define GNUNET_REST_ERROR_DATA_INVALID "Data invalid" | ||
106 | |||
107 | /** | ||
108 | * State while collecting all egos | ||
109 | */ | ||
110 | #define ID_REST_STATE_INIT 0 | ||
111 | |||
112 | /** | ||
113 | * Done collecting egos | ||
114 | */ | ||
115 | #define ID_REST_STATE_POST_INIT 1 | ||
116 | |||
117 | /** | ||
118 | * The configuration handle | ||
119 | */ | ||
120 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
121 | |||
122 | /** | ||
123 | * HTTP methods allows for this plugin | ||
124 | */ | ||
125 | static char* allow_methods; | ||
126 | |||
127 | /** | ||
128 | * @brief struct returned by the initialization function of the plugin | ||
129 | */ | ||
130 | struct Plugin | ||
131 | { | ||
132 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
133 | }; | ||
134 | |||
135 | /** | ||
136 | * The ego list | ||
137 | */ | ||
138 | struct EgoEntry | ||
139 | { | ||
140 | /** | ||
141 | * DLL | ||
142 | */ | ||
143 | struct EgoEntry *next; | ||
144 | |||
145 | /** | ||
146 | * DLL | ||
147 | */ | ||
148 | struct EgoEntry *prev; | ||
149 | |||
150 | /** | ||
151 | * Ego Identifier | ||
152 | */ | ||
153 | char *identifier; | ||
154 | |||
155 | /** | ||
156 | * Public key string | ||
157 | */ | ||
158 | char *keystring; | ||
159 | |||
160 | /** | ||
161 | * The Ego | ||
162 | */ | ||
163 | struct GNUNET_IDENTITY_Ego *ego; | ||
164 | }; | ||
165 | |||
166 | /** | ||
167 | * The request handle | ||
168 | */ | ||
169 | struct RequestHandle | ||
170 | { | ||
171 | /** | ||
172 | * The data from the REST request | ||
173 | */ | ||
174 | const char* data; | ||
175 | |||
176 | /** | ||
177 | * The name to look up | ||
178 | */ | ||
179 | char *name; | ||
180 | |||
181 | /** | ||
182 | * the length of the REST data | ||
183 | */ | ||
184 | size_t data_size; | ||
185 | |||
186 | |||
187 | /** | ||
188 | * Ego list | ||
189 | */ | ||
190 | struct EgoEntry *ego_head; | ||
191 | |||
192 | /** | ||
193 | * Ego list | ||
194 | */ | ||
195 | struct EgoEntry *ego_tail; | ||
196 | |||
197 | /** | ||
198 | * The processing state | ||
199 | */ | ||
200 | int state; | ||
201 | |||
202 | /** | ||
203 | * Handle to Identity service. | ||
204 | */ | ||
205 | struct GNUNET_IDENTITY_Handle *identity_handle; | ||
206 | |||
207 | /** | ||
208 | * IDENTITY Operation | ||
209 | */ | ||
210 | struct GNUNET_IDENTITY_Operation *op; | ||
211 | |||
212 | /** | ||
213 | * Rest connection | ||
214 | */ | ||
215 | struct GNUNET_REST_RequestHandle *rest_handle; | ||
216 | |||
217 | /** | ||
218 | * Desired timeout for the lookup (default is no timeout). | ||
219 | */ | ||
220 | struct GNUNET_TIME_Relative timeout; | ||
221 | |||
222 | /** | ||
223 | * ID of a task associated with the resolution process. | ||
224 | */ | ||
225 | struct GNUNET_SCHEDULER_Task *timeout_task; | ||
226 | |||
227 | /** | ||
228 | * The plugin result processor | ||
229 | */ | ||
230 | GNUNET_REST_ResultProcessor proc; | ||
231 | |||
232 | /** | ||
233 | * The closure of the result processor | ||
234 | */ | ||
235 | void *proc_cls; | ||
236 | |||
237 | /** | ||
238 | * The url | ||
239 | */ | ||
240 | char *url; | ||
241 | |||
242 | /** | ||
243 | * Error response message | ||
244 | */ | ||
245 | char *emsg; | ||
246 | |||
247 | /** | ||
248 | * Response code | ||
249 | */ | ||
250 | int response_code; | ||
251 | |||
252 | }; | ||
253 | |||
254 | /** | ||
255 | * Cleanup lookup handle | ||
256 | * @param handle Handle to clean up | ||
257 | */ | ||
258 | static void | ||
259 | cleanup_handle (void *cls) | ||
260 | { | ||
261 | struct RequestHandle *handle = cls; | ||
262 | struct EgoEntry *ego_entry; | ||
263 | struct EgoEntry *ego_tmp; | ||
264 | |||
265 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n"); | ||
266 | if (NULL != handle->timeout_task) | ||
267 | { | ||
268 | GNUNET_SCHEDULER_cancel (handle->timeout_task); | ||
269 | handle->timeout_task = NULL; | ||
270 | } | ||
271 | |||
272 | if (NULL != handle->url) | ||
273 | GNUNET_free(handle->url); | ||
274 | if (NULL != handle->emsg) | ||
275 | GNUNET_free(handle->emsg); | ||
276 | if (NULL != handle->name) | ||
277 | GNUNET_free (handle->name); | ||
278 | if (NULL != handle->identity_handle) | ||
279 | GNUNET_IDENTITY_disconnect (handle->identity_handle); | ||
280 | |||
281 | for (ego_entry = handle->ego_head; | ||
282 | NULL != ego_entry;) | ||
283 | { | ||
284 | ego_tmp = ego_entry; | ||
285 | ego_entry = ego_entry->next; | ||
286 | GNUNET_free(ego_tmp->identifier); | ||
287 | GNUNET_free(ego_tmp->keystring); | ||
288 | GNUNET_free(ego_tmp); | ||
289 | } | ||
290 | |||
291 | GNUNET_free(handle); | ||
292 | } | ||
293 | |||
294 | /** | ||
295 | * Task run on errors. Reports an error and cleans up everything. | ||
296 | * | ||
297 | * @param cls the `struct RequestHandle` | ||
298 | */ | ||
299 | static void | ||
300 | do_error (void *cls) | ||
301 | { | ||
302 | struct RequestHandle *handle = cls; | ||
303 | struct MHD_Response *resp; | ||
304 | json_t *json_error = json_object(); | ||
305 | char *response; | ||
306 | |||
307 | if (NULL == handle->emsg) | ||
308 | handle->emsg = GNUNET_strdup(GNUNET_REST_IDENTITY_ERROR_UNKNOWN); | ||
309 | |||
310 | json_object_set_new(json_error,"error", json_string(handle->emsg)); | ||
311 | |||
312 | if (0 == handle->response_code) | ||
313 | handle->response_code = MHD_HTTP_OK; | ||
314 | response = json_dumps (json_error, 0); | ||
315 | resp = GNUNET_REST_create_response (response); | ||
316 | handle->proc (handle->proc_cls, resp, handle->response_code); | ||
317 | json_decref(json_error); | ||
318 | GNUNET_free(response); | ||
319 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
320 | } | ||
321 | |||
322 | |||
323 | |||
324 | /** | ||
325 | * Get EgoEntry from list with either a public key or a name | ||
326 | * If public key and name are not NULL, it returns the public key result first | ||
327 | * | ||
328 | * @param handle the RequestHandle | ||
329 | * @param pubkey the public key of an identity (only one can be NULL) | ||
330 | * @param name the name of an identity (only one can be NULL) | ||
331 | * @return EgoEntry or NULL if not found | ||
332 | */ | ||
333 | struct EgoEntry* | ||
334 | get_egoentry(struct RequestHandle *handle, char* pubkey, char *name) | ||
335 | { | ||
336 | struct EgoEntry *ego_entry; | ||
337 | if (NULL != pubkey) | ||
338 | { | ||
339 | for (ego_entry = handle->ego_head; | ||
340 | NULL != ego_entry; | ||
341 | ego_entry = ego_entry->next) | ||
342 | { | ||
343 | if (0 != strcasecmp (pubkey, ego_entry->keystring)) | ||
344 | continue; | ||
345 | return ego_entry; | ||
346 | } | ||
347 | } | ||
348 | if (NULL != name) | ||
349 | { | ||
350 | for (ego_entry = handle->ego_head; | ||
351 | NULL != ego_entry; | ||
352 | ego_entry = ego_entry->next) | ||
353 | { | ||
354 | if (0 != strcasecmp (name, ego_entry->identifier)) | ||
355 | continue; | ||
356 | return ego_entry; | ||
357 | } | ||
358 | } | ||
359 | return NULL; | ||
360 | } | ||
361 | |||
362 | |||
363 | /** | ||
364 | * Callback for GET Request with subsystem | ||
365 | * | ||
366 | * @param cls the RequestHandle | ||
367 | * @param ego the Ego found | ||
368 | * @param ctx the context | ||
369 | * @param name the id of the ego | ||
370 | */ | ||
371 | static void | ||
372 | ego_get_for_subsystem (void *cls, | ||
373 | struct GNUNET_IDENTITY_Ego *ego, | ||
374 | void **ctx, | ||
375 | const char *name) | ||
376 | { | ||
377 | struct RequestHandle *handle = cls; | ||
378 | struct MHD_Response *resp; | ||
379 | struct GNUNET_CRYPTO_EcdsaPublicKey public_key; | ||
380 | json_t *json_root; | ||
381 | char *result_str; | ||
382 | char *public_key_string; | ||
383 | |||
384 | if(NULL == ego) | ||
385 | { | ||
386 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
387 | handle->emsg = GNUNET_strdup(GNUNET_REST_IDENTITY_NOT_FOUND); | ||
388 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
389 | return; | ||
390 | } | ||
391 | |||
392 | GNUNET_IDENTITY_ego_get_public_key(ego,&public_key); | ||
393 | public_key_string = GNUNET_CRYPTO_ecdsa_public_key_to_string(&public_key); | ||
394 | |||
395 | // create json with subsystem identity | ||
396 | json_root = json_object (); | ||
397 | json_object_set_new (json_root, | ||
398 | GNUNET_REST_IDENTITY_PARAM_PUBKEY, | ||
399 | json_string(public_key_string)); | ||
400 | json_object_set_new (json_root, | ||
401 | GNUNET_REST_IDENTITY_PARAM_NAME, | ||
402 | json_string(name)); | ||
403 | |||
404 | result_str = json_dumps (json_root, 0); | ||
405 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); | ||
406 | resp = GNUNET_REST_create_response (result_str); | ||
407 | |||
408 | json_decref (json_root); | ||
409 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | ||
410 | GNUNET_free(result_str); | ||
411 | GNUNET_free(public_key_string); | ||
412 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
413 | } | ||
414 | |||
415 | /** | ||
416 | * Handle identity GET request for subsystem | ||
417 | * | ||
418 | * @param con_handle the connection handle | ||
419 | * @param url the url | ||
420 | * @param cls the RequestHandle | ||
421 | */ | ||
422 | void | ||
423 | ego_get_subsystem (struct GNUNET_REST_RequestHandle *con_handle, | ||
424 | const char* url, | ||
425 | void *cls) | ||
426 | { | ||
427 | struct RequestHandle *handle = cls; | ||
428 | char *subsystem; | ||
429 | |||
430 | if (strlen (GNUNET_REST_API_NS_IDENTITY_SUBSYSTEM) >= strlen (handle->url)) | ||
431 | { | ||
432 | handle->emsg = GNUNET_strdup("Missing subsystem name"); | ||
433 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
434 | return; | ||
435 | } | ||
436 | subsystem = &handle->url[strlen ( | ||
437 | GNUNET_REST_API_NS_IDENTITY_SUBSYSTEM) + 1]; | ||
438 | //requested default identity of subsystem | ||
439 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Looking for %s's ego\n", subsystem); | ||
440 | |||
441 | handle->op = GNUNET_IDENTITY_get (handle->identity_handle, | ||
442 | subsystem, | ||
443 | &ego_get_for_subsystem, | ||
444 | handle); | ||
445 | |||
446 | if (NULL == handle->op) | ||
447 | { | ||
448 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
449 | handle->emsg = GNUNET_strdup(GNUNET_REST_IDENTITY_NOT_FOUND); | ||
450 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
451 | return; | ||
452 | } | ||
453 | } | ||
454 | |||
455 | |||
456 | /** | ||
457 | * Handle identity GET request - responds with all identities | ||
458 | * | ||
459 | * @param con_handle the connection handle | ||
460 | * @param url the url | ||
461 | * @param cls the RequestHandle | ||
462 | */ | ||
463 | void | ||
464 | ego_get_all (struct GNUNET_REST_RequestHandle *con_handle, | ||
465 | const char* url, | ||
466 | void *cls) | ||
467 | { | ||
468 | struct RequestHandle *handle = cls; | ||
469 | struct EgoEntry *ego_entry; | ||
470 | struct MHD_Response *resp; | ||
471 | json_t *json_root; | ||
472 | json_t *json_ego; | ||
473 | char *result_str; | ||
474 | |||
475 | json_root = json_array (); | ||
476 | //Return ego/egos | ||
477 | for (ego_entry = handle->ego_head; | ||
478 | NULL != ego_entry; ego_entry = ego_entry->next) | ||
479 | { | ||
480 | json_ego = json_object (); | ||
481 | json_object_set_new (json_ego, | ||
482 | GNUNET_REST_IDENTITY_PARAM_PUBKEY, | ||
483 | json_string (ego_entry->keystring)); | ||
484 | json_object_set_new (json_ego, | ||
485 | GNUNET_REST_IDENTITY_PARAM_NAME, | ||
486 | json_string (ego_entry->identifier)); | ||
487 | json_array_append (json_root, json_ego); | ||
488 | json_decref (json_ego); | ||
489 | } | ||
490 | |||
491 | if ((size_t) 0 == json_array_size (json_root)) | ||
492 | { | ||
493 | json_decref (json_root); | ||
494 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
495 | handle->emsg = GNUNET_strdup(GNUNET_REST_IDENTITY_NOT_FOUND); | ||
496 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
497 | return; | ||
498 | } | ||
499 | |||
500 | result_str = json_dumps (json_root, 0); | ||
501 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); | ||
502 | resp = GNUNET_REST_create_response (result_str); | ||
503 | |||
504 | json_decref (json_root); | ||
505 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | ||
506 | GNUNET_free(result_str); | ||
507 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
508 | } | ||
509 | |||
510 | |||
511 | /** | ||
512 | * Responds with the ego_entry identity | ||
513 | * | ||
514 | * @param handle the struct RequestHandle | ||
515 | * @param ego_entry the struct EgoEntry for the response | ||
516 | */ | ||
517 | void | ||
518 | ego_get_response (struct RequestHandle *handle, struct EgoEntry *ego_entry) | ||
519 | { | ||
520 | struct MHD_Response *resp; | ||
521 | json_t *json_ego; | ||
522 | char *result_str; | ||
523 | |||
524 | json_ego = json_object (); | ||
525 | json_object_set_new (json_ego, | ||
526 | GNUNET_REST_IDENTITY_PARAM_PUBKEY, | ||
527 | json_string (ego_entry->keystring)); | ||
528 | json_object_set_new (json_ego, | ||
529 | GNUNET_REST_IDENTITY_PARAM_NAME, | ||
530 | json_string (ego_entry->identifier)); | ||
531 | |||
532 | result_str = json_dumps (json_ego, 0); | ||
533 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); | ||
534 | resp = GNUNET_REST_create_response (result_str); | ||
535 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | ||
536 | |||
537 | json_decref (json_ego); | ||
538 | GNUNET_free(result_str); | ||
539 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
540 | } | ||
541 | |||
542 | |||
543 | /** | ||
544 | * Handle identity GET request with a public key | ||
545 | * | ||
546 | * @param con_handle the connection handle | ||
547 | * @param url the url | ||
548 | * @param cls the RequestHandle | ||
549 | */ | ||
550 | void | ||
551 | ego_get_pubkey (struct GNUNET_REST_RequestHandle *con_handle, | ||
552 | const char* url, | ||
553 | void *cls) | ||
554 | { | ||
555 | struct RequestHandle *handle = cls; | ||
556 | struct EgoEntry *ego_entry; | ||
557 | char *keystring; | ||
558 | |||
559 | keystring = NULL; | ||
560 | |||
561 | if (strlen (GNUNET_REST_API_NS_IDENTITY_PUBKEY) >= strlen (handle->url)) | ||
562 | { | ||
563 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
564 | handle->emsg = GNUNET_strdup(GNUNET_REST_IDENTITY_MISSING_PUBKEY); | ||
565 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
566 | return; | ||
567 | } | ||
568 | keystring = &handle->url[strlen (GNUNET_REST_API_NS_IDENTITY_PUBKEY)+1]; | ||
569 | ego_entry = get_egoentry(handle, keystring, NULL); | ||
570 | |||
571 | if (NULL == ego_entry) | ||
572 | { | ||
573 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
574 | handle->emsg = GNUNET_strdup(GNUNET_REST_IDENTITY_NOT_FOUND); | ||
575 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
576 | return; | ||
577 | } | ||
578 | |||
579 | ego_get_response(handle, ego_entry); | ||
580 | } | ||
581 | |||
582 | /** | ||
583 | * Handle identity GET request with a name | ||
584 | * | ||
585 | * @param con_handle the connection handle | ||
586 | * @param url the url | ||
587 | * @param cls the RequestHandle | ||
588 | */ | ||
589 | void | ||
590 | ego_get_name (struct GNUNET_REST_RequestHandle *con_handle, | ||
591 | const char* url, | ||
592 | void *cls) | ||
593 | { | ||
594 | struct RequestHandle *handle = cls; | ||
595 | struct EgoEntry *ego_entry; | ||
596 | char *egoname; | ||
597 | |||
598 | egoname = NULL; | ||
599 | |||
600 | if (strlen (GNUNET_REST_API_NS_IDENTITY_NAME) >= strlen (handle->url)) | ||
601 | { | ||
602 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
603 | handle->emsg = GNUNET_strdup(GNUNET_REST_IDENTITY_MISSING_NAME); | ||
604 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
605 | return; | ||
606 | } | ||
607 | egoname = &handle->url[strlen (GNUNET_REST_API_NS_IDENTITY_NAME)+1]; | ||
608 | ego_entry = get_egoentry(handle, NULL, egoname); | ||
609 | |||
610 | if (NULL == ego_entry) | ||
611 | { | ||
612 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
613 | handle->emsg = GNUNET_strdup(GNUNET_REST_IDENTITY_NOT_FOUND); | ||
614 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
615 | return; | ||
616 | } | ||
617 | |||
618 | ego_get_response(handle, ego_entry); | ||
619 | } | ||
620 | |||
621 | |||
622 | /** | ||
623 | * Processing finished | ||
624 | * | ||
625 | * @param cls request handle | ||
626 | * @param emsg error message | ||
627 | */ | ||
628 | static void | ||
629 | do_finished (void *cls, const char *emsg) | ||
630 | { | ||
631 | struct RequestHandle *handle = cls; | ||
632 | struct MHD_Response *resp; | ||
633 | |||
634 | handle->op = NULL; | ||
635 | if (NULL != emsg) | ||
636 | { | ||
637 | handle->emsg = GNUNET_strdup(emsg); | ||
638 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
639 | return; | ||
640 | } | ||
641 | if (0 == handle->response_code) | ||
642 | { | ||
643 | handle->response_code = MHD_HTTP_NO_CONTENT; | ||
644 | } | ||
645 | resp = GNUNET_REST_create_response (NULL); | ||
646 | handle->proc (handle->proc_cls, resp, handle->response_code); | ||
647 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
648 | } | ||
649 | |||
650 | |||
651 | /** | ||
652 | * Processing edit ego with EgoEntry ego_entry | ||
653 | * | ||
654 | * @param handle the struct RequestHandle | ||
655 | * @param ego_entry the struct EgoEntry we want to edit | ||
656 | */ | ||
657 | void | ||
658 | ego_edit (struct RequestHandle *handle, struct EgoEntry *ego_entry) | ||
659 | { | ||
660 | struct EgoEntry *ego_entry_tmp; | ||
661 | struct MHD_Response *resp; | ||
662 | json_t *data_js; | ||
663 | json_error_t err; | ||
664 | char *newname; | ||
665 | char term_data[handle->data_size + 1]; | ||
666 | int json_state; | ||
667 | |||
668 | //if no data | ||
669 | if (0 >= handle->data_size) | ||
670 | { | ||
671 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_NO_DATA); | ||
672 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
673 | return; | ||
674 | } | ||
675 | //if not json | ||
676 | term_data[handle->data_size] = '\0'; | ||
677 | GNUNET_memcpy(term_data, handle->data, handle->data_size); | ||
678 | data_js = json_loads (term_data,JSON_DECODE_ANY,&err); | ||
679 | |||
680 | if (NULL == data_js) | ||
681 | { | ||
682 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_NO_DATA); | ||
683 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
684 | return; | ||
685 | } | ||
686 | |||
687 | newname = NULL; | ||
688 | //NEW NAME | ||
689 | json_state = 0; | ||
690 | json_state = json_unpack(data_js, | ||
691 | "{s:s!}", | ||
692 | GNUNET_REST_IDENTITY_PARAM_NEWNAME, | ||
693 | &newname); | ||
694 | //Change name with pubkey or name identifier | ||
695 | if (0 != json_state) | ||
696 | { | ||
697 | |||
698 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_DATA_INVALID); | ||
699 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
700 | json_decref (data_js); | ||
701 | return; | ||
702 | } | ||
703 | |||
704 | if (NULL == newname) | ||
705 | { | ||
706 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_DATA_INVALID); | ||
707 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
708 | json_decref (data_js); | ||
709 | return; | ||
710 | } | ||
711 | |||
712 | if (0 >= strlen (newname)) | ||
713 | { | ||
714 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_DATA_INVALID); | ||
715 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
716 | json_decref (data_js); | ||
717 | return; | ||
718 | } | ||
719 | |||
720 | ego_entry_tmp = get_egoentry (handle, NULL, newname); | ||
721 | if (NULL != ego_entry_tmp) | ||
722 | { | ||
723 | //Ego with same name not allowed (even if its the ego we change) | ||
724 | resp = GNUNET_REST_create_response (NULL); | ||
725 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); | ||
726 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
727 | json_decref (data_js); | ||
728 | return; | ||
729 | } | ||
730 | handle->op = GNUNET_IDENTITY_rename (handle->identity_handle, | ||
731 | ego_entry->identifier, | ||
732 | newname, | ||
733 | &do_finished, | ||
734 | handle); | ||
735 | if (NULL == handle->op) | ||
736 | { | ||
737 | handle->emsg = GNUNET_strdup("Rename failed"); | ||
738 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
739 | json_decref (data_js); | ||
740 | return; | ||
741 | } | ||
742 | json_decref (data_js); | ||
743 | return; | ||
744 | |||
745 | } | ||
746 | |||
747 | |||
748 | /** | ||
749 | * Handle identity PUT request with public key | ||
750 | * | ||
751 | * @param con_handle the connection handle | ||
752 | * @param url the url | ||
753 | * @param cls the RequestHandle | ||
754 | */ | ||
755 | void | ||
756 | ego_edit_pubkey (struct GNUNET_REST_RequestHandle *con_handle, | ||
757 | const char* url, | ||
758 | void *cls) | ||
759 | { | ||
760 | struct RequestHandle *handle = cls; | ||
761 | struct EgoEntry *ego_entry; | ||
762 | char *keystring; | ||
763 | |||
764 | keystring = NULL; | ||
765 | |||
766 | if (strlen (GNUNET_REST_API_NS_IDENTITY_PUBKEY) >= strlen (handle->url)) | ||
767 | { | ||
768 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
769 | handle->emsg = GNUNET_strdup(GNUNET_REST_IDENTITY_MISSING_PUBKEY); | ||
770 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
771 | return; | ||
772 | } | ||
773 | keystring = &handle->url[strlen (GNUNET_REST_API_NS_IDENTITY_PUBKEY)+1]; | ||
774 | ego_entry = get_egoentry(handle, keystring, NULL); | ||
775 | |||
776 | if (NULL == ego_entry) | ||
777 | { | ||
778 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
779 | handle->emsg = GNUNET_strdup(GNUNET_REST_IDENTITY_NOT_FOUND); | ||
780 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
781 | return; | ||
782 | } | ||
783 | |||
784 | ego_edit(handle,ego_entry); | ||
785 | } | ||
786 | |||
787 | /** | ||
788 | * Handle identity PUT request with name | ||
789 | * | ||
790 | * @param con_handle the connection handle | ||
791 | * @param url the url | ||
792 | * @param cls the RequestHandle | ||
793 | */ | ||
794 | void | ||
795 | ego_edit_name (struct GNUNET_REST_RequestHandle *con_handle, | ||
796 | const char* url, | ||
797 | void *cls) | ||
798 | { | ||
799 | struct RequestHandle *handle = cls; | ||
800 | struct EgoEntry *ego_entry; | ||
801 | char *name; | ||
802 | |||
803 | name = NULL; | ||
804 | |||
805 | if (strlen (GNUNET_REST_API_NS_IDENTITY_NAME) >= strlen (handle->url)) | ||
806 | { | ||
807 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
808 | handle->emsg = GNUNET_strdup(GNUNET_REST_IDENTITY_MISSING_NAME); | ||
809 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
810 | return; | ||
811 | } | ||
812 | name = &handle->url[strlen (GNUNET_REST_API_NS_IDENTITY_NAME)+1]; | ||
813 | ego_entry = get_egoentry(handle, NULL, name); | ||
814 | |||
815 | if (NULL == ego_entry) | ||
816 | { | ||
817 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
818 | handle->emsg = GNUNET_strdup(GNUNET_REST_IDENTITY_NOT_FOUND); | ||
819 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
820 | return; | ||
821 | } | ||
822 | |||
823 | ego_edit(handle,ego_entry); | ||
824 | } | ||
825 | |||
826 | /** | ||
827 | * Handle identity subsystem PUT request with name | ||
828 | * | ||
829 | * @param con_handle the connection handle | ||
830 | * @param url the url | ||
831 | * @param cls the RequestHandle | ||
832 | */ | ||
833 | void | ||
834 | ego_edit_subsystem (struct GNUNET_REST_RequestHandle *con_handle, | ||
835 | const char* url, | ||
836 | void *cls) | ||
837 | { | ||
838 | struct RequestHandle *handle = cls; | ||
839 | struct EgoEntry *ego_entry; | ||
840 | json_t *data_js; | ||
841 | json_error_t err; | ||
842 | char *newsubsys; | ||
843 | char *name; | ||
844 | char term_data[handle->data_size + 1]; | ||
845 | int json_state; | ||
846 | |||
847 | name = NULL; | ||
848 | |||
849 | if (strlen (GNUNET_REST_API_NS_IDENTITY_SUBSYSTEM) >= strlen (handle->url)) | ||
850 | { | ||
851 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
852 | handle->emsg = GNUNET_strdup(GNUNET_REST_IDENTITY_MISSING_NAME); | ||
853 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
854 | return; | ||
855 | } | ||
856 | name = &handle->url[strlen (GNUNET_REST_API_NS_IDENTITY_SUBSYSTEM)+1]; | ||
857 | ego_entry = get_egoentry(handle, NULL, name); | ||
858 | |||
859 | if (NULL == ego_entry) | ||
860 | { | ||
861 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
862 | handle->emsg = GNUNET_strdup(GNUNET_REST_IDENTITY_NOT_FOUND); | ||
863 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
864 | return; | ||
865 | } | ||
866 | |||
867 | //if no data | ||
868 | if (0 >= handle->data_size) | ||
869 | { | ||
870 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_NO_DATA); | ||
871 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
872 | return; | ||
873 | } | ||
874 | //if not json | ||
875 | term_data[handle->data_size] = '\0'; | ||
876 | GNUNET_memcpy(term_data, handle->data, handle->data_size); | ||
877 | data_js = json_loads (term_data,JSON_DECODE_ANY,&err); | ||
878 | |||
879 | if (NULL == data_js) | ||
880 | { | ||
881 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_NO_DATA); | ||
882 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
883 | return; | ||
884 | } | ||
885 | |||
886 | newsubsys = NULL; | ||
887 | //SUBSYSTEM | ||
888 | json_state = 0; | ||
889 | json_state = json_unpack(data_js, | ||
890 | "{s:s!}", | ||
891 | GNUNET_REST_IDENTITY_PARAM_SUBSYSTEM, | ||
892 | &newsubsys); | ||
893 | //Change subsystem with pubkey or name identifier | ||
894 | if (0 != json_state) | ||
895 | { | ||
896 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_DATA_INVALID); | ||
897 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
898 | json_decref (data_js); | ||
899 | return; | ||
900 | } | ||
901 | |||
902 | if (NULL == newsubsys) | ||
903 | { | ||
904 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_DATA_INVALID); | ||
905 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
906 | json_decref (data_js); | ||
907 | return; | ||
908 | } | ||
909 | |||
910 | if (0 >= strlen (newsubsys)) | ||
911 | { | ||
912 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_DATA_INVALID); | ||
913 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
914 | json_decref (data_js); | ||
915 | return; | ||
916 | } | ||
917 | |||
918 | handle->response_code = MHD_HTTP_NO_CONTENT; | ||
919 | handle->op = GNUNET_IDENTITY_set (handle->identity_handle, | ||
920 | newsubsys, | ||
921 | ego_entry->ego, | ||
922 | &do_finished, | ||
923 | handle); | ||
924 | if (NULL == handle->op) | ||
925 | { | ||
926 | handle->emsg = GNUNET_strdup("Setting subsystem failed"); | ||
927 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
928 | return; | ||
929 | } | ||
930 | json_decref (data_js); | ||
931 | return; | ||
932 | |||
933 | } | ||
934 | |||
935 | /** | ||
936 | * Handle identity POST request | ||
937 | * | ||
938 | * @param con_handle the connection handle | ||
939 | * @param url the url | ||
940 | * @param cls the RequestHandle | ||
941 | */ | ||
942 | void | ||
943 | ego_create (struct GNUNET_REST_RequestHandle *con_handle, | ||
944 | const char* url, | ||
945 | void *cls) | ||
946 | { | ||
947 | struct RequestHandle *handle = cls; | ||
948 | struct EgoEntry *ego_entry; | ||
949 | struct MHD_Response *resp; | ||
950 | json_t *data_js; | ||
951 | json_error_t err; | ||
952 | char* egoname; | ||
953 | int json_unpack_state; | ||
954 | char term_data[handle->data_size + 1]; | ||
955 | |||
956 | if (strlen (GNUNET_REST_API_NS_IDENTITY) != strlen (handle->url)) | ||
957 | { | ||
958 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
959 | return; | ||
960 | } | ||
961 | |||
962 | if (0 >= handle->data_size) | ||
963 | { | ||
964 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_NO_DATA); | ||
965 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
966 | return; | ||
967 | } | ||
968 | term_data[handle->data_size] = '\0'; | ||
969 | GNUNET_memcpy(term_data, handle->data, handle->data_size); | ||
970 | data_js = json_loads (term_data, | ||
971 | JSON_DECODE_ANY, | ||
972 | &err); | ||
973 | if (NULL == data_js) | ||
974 | { | ||
975 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_NO_DATA); | ||
976 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
977 | json_decref (data_js); | ||
978 | return; | ||
979 | } | ||
980 | json_unpack_state = 0; | ||
981 | json_unpack_state = json_unpack(data_js, | ||
982 | "{s:s!}", | ||
983 | GNUNET_REST_IDENTITY_PARAM_NAME, | ||
984 | &egoname); | ||
985 | if (0 != json_unpack_state) | ||
986 | { | ||
987 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_DATA_INVALID); | ||
988 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
989 | json_decref (data_js); | ||
990 | return; | ||
991 | } | ||
992 | |||
993 | if (NULL == egoname) | ||
994 | { | ||
995 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_DATA_INVALID); | ||
996 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
997 | json_decref (data_js); | ||
998 | return; | ||
999 | } | ||
1000 | if (0 >= strlen (egoname)) | ||
1001 | { | ||
1002 | json_decref (data_js); | ||
1003 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_DATA_INVALID); | ||
1004 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
1005 | return; | ||
1006 | } | ||
1007 | GNUNET_STRINGS_utf8_tolower(egoname, egoname); | ||
1008 | for (ego_entry = handle->ego_head; | ||
1009 | NULL != ego_entry; ego_entry = ego_entry->next) | ||
1010 | { | ||
1011 | if (0 == strcasecmp (egoname, ego_entry->identifier)) | ||
1012 | { | ||
1013 | resp = GNUNET_REST_create_response (NULL); | ||
1014 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); | ||
1015 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
1016 | json_decref (data_js); | ||
1017 | return; | ||
1018 | } | ||
1019 | } | ||
1020 | handle->name = GNUNET_strdup(egoname); | ||
1021 | json_decref (data_js); | ||
1022 | handle->response_code = MHD_HTTP_CREATED; | ||
1023 | handle->op = GNUNET_IDENTITY_create (handle->identity_handle, handle->name, | ||
1024 | &do_finished, handle); | ||
1025 | } | ||
1026 | |||
1027 | /** | ||
1028 | * Handle identity DELETE request with public key | ||
1029 | * | ||
1030 | * @param con_handle the connection handle | ||
1031 | * @param url the url | ||
1032 | * @param cls the RequestHandle | ||
1033 | */ | ||
1034 | void | ||
1035 | ego_delete_pubkey (struct GNUNET_REST_RequestHandle *con_handle, | ||
1036 | const char* url, | ||
1037 | void *cls) | ||
1038 | { | ||
1039 | struct RequestHandle *handle = cls; | ||
1040 | struct EgoEntry *ego_entry; | ||
1041 | char *keystring; | ||
1042 | |||
1043 | keystring = NULL; | ||
1044 | |||
1045 | if (strlen (GNUNET_REST_API_NS_IDENTITY_PUBKEY) >= strlen (handle->url)) | ||
1046 | { | ||
1047 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
1048 | handle->emsg = GNUNET_strdup(GNUNET_REST_IDENTITY_MISSING_PUBKEY); | ||
1049 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
1050 | return; | ||
1051 | } | ||
1052 | keystring = &handle->url[strlen (GNUNET_REST_API_NS_IDENTITY_PUBKEY)+1]; | ||
1053 | ego_entry = get_egoentry(handle, keystring, NULL); | ||
1054 | |||
1055 | if (NULL == ego_entry) | ||
1056 | { | ||
1057 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
1058 | handle->emsg = GNUNET_strdup(GNUNET_REST_IDENTITY_NOT_FOUND); | ||
1059 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
1060 | return; | ||
1061 | } | ||
1062 | |||
1063 | handle->response_code = MHD_HTTP_NO_CONTENT; | ||
1064 | handle->op = GNUNET_IDENTITY_delete (handle->identity_handle, | ||
1065 | ego_entry->identifier, | ||
1066 | &do_finished, | ||
1067 | handle); | ||
1068 | } | ||
1069 | |||
1070 | |||
1071 | /** | ||
1072 | * Handle identity DELETE request with name | ||
1073 | * | ||
1074 | * @param con_handle the connection handle | ||
1075 | * @param url the url | ||
1076 | * @param cls the RequestHandle | ||
1077 | */ | ||
1078 | void | ||
1079 | ego_delete_name (struct GNUNET_REST_RequestHandle *con_handle, | ||
1080 | const char* url, | ||
1081 | void *cls) | ||
1082 | { | ||
1083 | struct RequestHandle *handle = cls; | ||
1084 | struct EgoEntry *ego_entry; | ||
1085 | char *name; | ||
1086 | |||
1087 | name = NULL; | ||
1088 | |||
1089 | if (strlen (GNUNET_REST_API_NS_IDENTITY_NAME) >= strlen (handle->url)) | ||
1090 | { | ||
1091 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
1092 | handle->emsg = GNUNET_strdup(GNUNET_REST_IDENTITY_MISSING_NAME); | ||
1093 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
1094 | return; | ||
1095 | } | ||
1096 | name = &handle->url[strlen (GNUNET_REST_API_NS_IDENTITY_NAME)+1]; | ||
1097 | ego_entry = get_egoentry(handle, NULL, name); | ||
1098 | |||
1099 | if (NULL == ego_entry) | ||
1100 | { | ||
1101 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
1102 | handle->emsg = GNUNET_strdup(GNUNET_REST_IDENTITY_NOT_FOUND); | ||
1103 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
1104 | return; | ||
1105 | } | ||
1106 | |||
1107 | handle->response_code = MHD_HTTP_NO_CONTENT; | ||
1108 | handle->op = GNUNET_IDENTITY_delete (handle->identity_handle, | ||
1109 | ego_entry->identifier, | ||
1110 | &do_finished, | ||
1111 | handle); | ||
1112 | } | ||
1113 | |||
1114 | |||
1115 | /** | ||
1116 | * Respond to OPTIONS request | ||
1117 | * | ||
1118 | * @param con_handle the connection handle | ||
1119 | * @param url the url | ||
1120 | * @param cls the RequestHandle | ||
1121 | */ | ||
1122 | static void | ||
1123 | options_cont (struct GNUNET_REST_RequestHandle *con_handle, const char* url, | ||
1124 | void *cls) | ||
1125 | { | ||
1126 | struct MHD_Response *resp; | ||
1127 | struct RequestHandle *handle = cls; | ||
1128 | |||
1129 | //For now, independent of path return all options | ||
1130 | resp = GNUNET_REST_create_response (NULL); | ||
1131 | MHD_add_response_header (resp, "Access-Control-Allow-Methods", allow_methods); | ||
1132 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | ||
1133 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
1134 | return; | ||
1135 | } | ||
1136 | |||
1137 | /** | ||
1138 | * Handle rest request | ||
1139 | * | ||
1140 | * @param handle the request handle | ||
1141 | */ | ||
1142 | static void | ||
1143 | init_cont (struct RequestHandle *handle) | ||
1144 | { | ||
1145 | struct GNUNET_REST_RequestHandlerError err; | ||
1146 | static const struct GNUNET_REST_RequestHandler handlers[] = { | ||
1147 | { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY_ALL, &ego_get_all }, | ||
1148 | { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY_PUBKEY, &ego_get_pubkey }, | ||
1149 | { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY_NAME, &ego_get_name }, | ||
1150 | { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY_SUBSYSTEM, &ego_get_subsystem }, | ||
1151 | { MHD_HTTP_METHOD_PUT, GNUNET_REST_API_NS_IDENTITY_PUBKEY, &ego_edit_pubkey }, | ||
1152 | { MHD_HTTP_METHOD_PUT, GNUNET_REST_API_NS_IDENTITY_NAME, &ego_edit_name }, | ||
1153 | { MHD_HTTP_METHOD_PUT, GNUNET_REST_API_NS_IDENTITY_SUBSYSTEM, &ego_edit_subsystem }, | ||
1154 | { MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_IDENTITY, &ego_create }, | ||
1155 | { MHD_HTTP_METHOD_DELETE, GNUNET_REST_API_NS_IDENTITY_PUBKEY, &ego_delete_pubkey }, | ||
1156 | { MHD_HTTP_METHOD_DELETE, GNUNET_REST_API_NS_IDENTITY_NAME, &ego_delete_name }, | ||
1157 | { MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_IDENTITY, &options_cont }, | ||
1158 | GNUNET_REST_HANDLER_END | ||
1159 | }; | ||
1160 | |||
1161 | if (GNUNET_NO | ||
1162 | == GNUNET_REST_handle_request (handle->rest_handle, handlers, &err, | ||
1163 | handle)) | ||
1164 | { | ||
1165 | handle->response_code = err.error_code; | ||
1166 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
1167 | } | ||
1168 | } | ||
1169 | |||
1170 | /** | ||
1171 | * If listing is enabled, prints information about the egos. | ||
1172 | * | ||
1173 | * This function is initially called for all egos and then again | ||
1174 | * whenever a ego's identifier changes or if it is deleted. At the | ||
1175 | * end of the initial pass over all egos, the function is once called | ||
1176 | * with 'NULL' for 'ego'. That does NOT mean that the callback won't | ||
1177 | * be invoked in the future or that there was an error. | ||
1178 | * | ||
1179 | * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get', | ||
1180 | * this function is only called ONCE, and 'NULL' being passed in | ||
1181 | * 'ego' does indicate an error (i.e. name is taken or no default | ||
1182 | * value is known). If 'ego' is non-NULL and if '*ctx' | ||
1183 | * is set in those callbacks, the value WILL be passed to a subsequent | ||
1184 | * call to the identity callback of 'GNUNET_IDENTITY_connect' (if | ||
1185 | * that one was not NULL). | ||
1186 | * | ||
1187 | * When an identity is renamed, this function is called with the | ||
1188 | * (known) ego but the NEW identifier. | ||
1189 | * | ||
1190 | * When an identity is deleted, this function is called with the | ||
1191 | * (known) ego and "NULL" for the 'identifier'. In this case, | ||
1192 | * the 'ego' is henceforth invalid (and the 'ctx' should also be | ||
1193 | * cleaned up). | ||
1194 | * | ||
1195 | * @param cls closure | ||
1196 | * @param ego ego handle | ||
1197 | * @param ctx context for application to store data for this ego | ||
1198 | * (during the lifetime of this process, initially NULL) | ||
1199 | * @param identifier identifier assigned by the user for this ego, | ||
1200 | * NULL if the user just deleted the ego and it | ||
1201 | * must thus no longer be used | ||
1202 | */ | ||
1203 | static void | ||
1204 | init_egos (void *cls, struct GNUNET_IDENTITY_Ego *ego, void **ctx, | ||
1205 | const char *identifier) | ||
1206 | { | ||
1207 | struct RequestHandle *handle = cls; | ||
1208 | struct EgoEntry *ego_entry; | ||
1209 | struct GNUNET_CRYPTO_EcdsaPublicKey pk; | ||
1210 | |||
1211 | if ((NULL == ego) && (ID_REST_STATE_INIT == handle->state)) | ||
1212 | { | ||
1213 | handle->state = ID_REST_STATE_POST_INIT; | ||
1214 | init_cont (handle); | ||
1215 | return; | ||
1216 | } | ||
1217 | if (ID_REST_STATE_INIT == handle->state) | ||
1218 | { | ||
1219 | ego_entry = GNUNET_new(struct EgoEntry); | ||
1220 | GNUNET_IDENTITY_ego_get_public_key (ego, &pk); | ||
1221 | ego_entry->keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk); | ||
1222 | ego_entry->ego = ego; | ||
1223 | GNUNET_asprintf (&ego_entry->identifier, "%s", identifier); | ||
1224 | GNUNET_CONTAINER_DLL_insert_tail(handle->ego_head, handle->ego_tail, | ||
1225 | ego_entry); | ||
1226 | } | ||
1227 | } | ||
1228 | |||
1229 | /** | ||
1230 | * Function processing the REST call | ||
1231 | * | ||
1232 | * @param method HTTP method | ||
1233 | * @param url URL of the HTTP request | ||
1234 | * @param data body of the HTTP request (optional) | ||
1235 | * @param data_size length of the body | ||
1236 | * @param proc callback function for the result | ||
1237 | * @param proc_cls closure for callback function | ||
1238 | * @return GNUNET_OK if request accepted | ||
1239 | */ | ||
1240 | static void | ||
1241 | rest_process_request (struct GNUNET_REST_RequestHandle *rest_handle, | ||
1242 | GNUNET_REST_ResultProcessor proc, void *proc_cls) | ||
1243 | { | ||
1244 | struct RequestHandle *handle = GNUNET_new(struct RequestHandle); | ||
1245 | |||
1246 | handle->response_code = 0; | ||
1247 | handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL; | ||
1248 | handle->proc_cls = proc_cls; | ||
1249 | handle->proc = proc; | ||
1250 | handle->rest_handle = rest_handle; | ||
1251 | handle->data = rest_handle->data; | ||
1252 | handle->data_size = rest_handle->data_size; | ||
1253 | |||
1254 | handle->url = GNUNET_strdup(rest_handle->url); | ||
1255 | if (handle->url[strlen (handle->url) - 1] == '/') | ||
1256 | handle->url[strlen (handle->url) - 1] = '\0'; | ||
1257 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n"); | ||
1258 | |||
1259 | handle->identity_handle = GNUNET_IDENTITY_connect (cfg, &init_egos, handle); | ||
1260 | |||
1261 | handle->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->timeout, | ||
1262 | &do_error, handle); | ||
1263 | |||
1264 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connected\n"); | ||
1265 | } | ||
1266 | |||
1267 | /** | ||
1268 | * Entry point for the plugin. | ||
1269 | * | ||
1270 | * @param cls Config info | ||
1271 | * @return NULL on error, otherwise the plugin context | ||
1272 | */ | ||
1273 | void * | ||
1274 | libgnunet_plugin_rest_identity_init (void *cls) | ||
1275 | { | ||
1276 | static struct Plugin plugin; | ||
1277 | struct GNUNET_REST_Plugin *api; | ||
1278 | |||
1279 | cfg = cls; | ||
1280 | if (NULL != plugin.cfg) | ||
1281 | return NULL; /* can only initialize once! */ | ||
1282 | memset (&plugin, 0, sizeof(struct Plugin)); | ||
1283 | plugin.cfg = cfg; | ||
1284 | api = GNUNET_new(struct GNUNET_REST_Plugin); | ||
1285 | api->cls = &plugin; | ||
1286 | api->name = GNUNET_REST_API_NS_IDENTITY; | ||
1287 | api->process_request = &rest_process_request; | ||
1288 | GNUNET_asprintf (&allow_methods, "%s, %s, %s, %s, %s", | ||
1289 | MHD_HTTP_METHOD_GET, | ||
1290 | MHD_HTTP_METHOD_POST, | ||
1291 | MHD_HTTP_METHOD_PUT, | ||
1292 | MHD_HTTP_METHOD_DELETE, | ||
1293 | MHD_HTTP_METHOD_OPTIONS); | ||
1294 | |||
1295 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, _("Identity REST API initialized\n")); | ||
1296 | return api; | ||
1297 | } | ||
1298 | |||
1299 | /** | ||
1300 | * Exit point from the plugin. | ||
1301 | * | ||
1302 | * @param cls the plugin context (as returned by "init") | ||
1303 | * @return always NULL | ||
1304 | */ | ||
1305 | void * | ||
1306 | libgnunet_plugin_rest_identity_done (void *cls) | ||
1307 | { | ||
1308 | struct GNUNET_REST_Plugin *api = cls; | ||
1309 | struct Plugin *plugin = api->cls; | ||
1310 | plugin->cfg = NULL; | ||
1311 | |||
1312 | GNUNET_free_non_null(allow_methods); | ||
1313 | GNUNET_free(api); | ||
1314 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Identity REST plugin is finished\n"); | ||
1315 | return NULL; | ||
1316 | } | ||
1317 | |||
1318 | /* end of plugin_rest_identity.c */ | ||
1319 | |||
diff --git a/src/rest-plugins/plugin_rest_namestore.c b/src/rest-plugins/plugin_rest_namestore.c new file mode 100644 index 000000000..1d72d13ff --- /dev/null +++ b/src/rest-plugins/plugin_rest_namestore.c | |||
@@ -0,0 +1,1004 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2012-2015 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | /** | ||
19 | * @author Martin Schanzenbach | ||
20 | * @author Philippe Buschmann | ||
21 | * @file namestore/plugin_rest_namestore.c | ||
22 | * @brief GNUnet Namestore REST plugin | ||
23 | */ | ||
24 | |||
25 | #include "platform.h" | ||
26 | #include "gnunet_rest_plugin.h" | ||
27 | #include "gnunet_gns_service.h" | ||
28 | #include "gnunet_namestore_service.h" | ||
29 | #include "gnunet_identity_service.h" | ||
30 | #include "gnunet_rest_lib.h" | ||
31 | #include "gnunet_json_lib.h" | ||
32 | #include "microhttpd.h" | ||
33 | #include <jansson.h> | ||
34 | |||
35 | /** | ||
36 | * Namestore Namespace | ||
37 | */ | ||
38 | #define GNUNET_REST_API_NS_NAMESTORE "/namestore" | ||
39 | |||
40 | /** | ||
41 | * Error message Unknown Error | ||
42 | */ | ||
43 | #define GNUNET_REST_NAMESTORE_ERROR_UNKNOWN "Unknown Error" | ||
44 | |||
45 | /** | ||
46 | * Error message No identity found | ||
47 | */ | ||
48 | #define GNUNET_REST_IDENTITY_NOT_FOUND "No identity found" | ||
49 | |||
50 | /** | ||
51 | * Error message No default zone specified | ||
52 | */ | ||
53 | #define GNUNET_REST_NAMESTORE_NO_DEFAULT_ZONE "No default zone specified" | ||
54 | |||
55 | /** | ||
56 | * Error message Failed request | ||
57 | */ | ||
58 | #define GNUNET_REST_NAMESTORE_FAILED "Namestore action failed" | ||
59 | |||
60 | /** | ||
61 | * Error message invalid data | ||
62 | */ | ||
63 | #define GNUNET_REST_NAMESTORE_INVALID_DATA "Data invalid" | ||
64 | |||
65 | /** | ||
66 | * Error message No data | ||
67 | */ | ||
68 | #define GNUNET_REST_NAMESTORE_NO_DATA "No data" | ||
69 | |||
70 | /** | ||
71 | * State while collecting all egos | ||
72 | */ | ||
73 | #define ID_REST_STATE_INIT 0 | ||
74 | |||
75 | /** | ||
76 | * Done collecting egos | ||
77 | */ | ||
78 | #define ID_REST_STATE_POST_INIT 1 | ||
79 | /** | ||
80 | * The configuration handle | ||
81 | */ | ||
82 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
83 | |||
84 | /** | ||
85 | * HTTP methods allows for this plugin | ||
86 | */ | ||
87 | static char* allow_methods; | ||
88 | |||
89 | /** | ||
90 | * @brief struct returned by the initialization function of the plugin | ||
91 | */ | ||
92 | struct Plugin | ||
93 | { | ||
94 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
95 | }; | ||
96 | |||
97 | /** | ||
98 | * The default namestore ego | ||
99 | */ | ||
100 | struct EgoEntry | ||
101 | { | ||
102 | /** | ||
103 | * DLL | ||
104 | */ | ||
105 | struct EgoEntry *next; | ||
106 | |||
107 | /** | ||
108 | * DLL | ||
109 | */ | ||
110 | struct EgoEntry *prev; | ||
111 | |||
112 | /** | ||
113 | * Ego Identifier | ||
114 | */ | ||
115 | char *identifier; | ||
116 | |||
117 | /** | ||
118 | * Public key string | ||
119 | */ | ||
120 | char *keystring; | ||
121 | |||
122 | /** | ||
123 | * The Ego | ||
124 | */ | ||
125 | struct GNUNET_IDENTITY_Ego *ego; | ||
126 | }; | ||
127 | |||
128 | /** | ||
129 | * The request handle | ||
130 | */ | ||
131 | struct RequestHandle | ||
132 | { | ||
133 | /** | ||
134 | * Records to store | ||
135 | */ | ||
136 | char *record_name; | ||
137 | |||
138 | /** | ||
139 | * Records to store | ||
140 | */ | ||
141 | struct GNUNET_GNSRECORD_Data *rd; | ||
142 | |||
143 | /** | ||
144 | * NAMESTORE Operation | ||
145 | */ | ||
146 | struct GNUNET_NAMESTORE_QueueEntry *add_qe; | ||
147 | |||
148 | /** | ||
149 | * Response object | ||
150 | */ | ||
151 | json_t *resp_object; | ||
152 | |||
153 | /** | ||
154 | * The processing state | ||
155 | */ | ||
156 | int state; | ||
157 | |||
158 | /** | ||
159 | * Handle to NAMESTORE | ||
160 | */ | ||
161 | struct GNUNET_NAMESTORE_Handle *ns_handle; | ||
162 | |||
163 | /** | ||
164 | * Handle to NAMESTORE it | ||
165 | */ | ||
166 | struct GNUNET_NAMESTORE_ZoneIterator *list_it; | ||
167 | |||
168 | /** | ||
169 | * Private key for the zone | ||
170 | */ | ||
171 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_pkey; | ||
172 | |||
173 | /** | ||
174 | * IDENTITY Operation | ||
175 | */ | ||
176 | struct EgoEntry *ego_entry; | ||
177 | |||
178 | /** | ||
179 | * Ego list | ||
180 | */ | ||
181 | struct EgoEntry *ego_head; | ||
182 | |||
183 | /** | ||
184 | * Ego list | ||
185 | */ | ||
186 | struct EgoEntry *ego_tail; | ||
187 | |||
188 | /** | ||
189 | * IDENTITY Operation | ||
190 | */ | ||
191 | struct GNUNET_IDENTITY_Operation *op; | ||
192 | |||
193 | /** | ||
194 | * Handle to Identity service. | ||
195 | */ | ||
196 | struct GNUNET_IDENTITY_Handle *identity_handle; | ||
197 | |||
198 | /** | ||
199 | * Rest connection | ||
200 | */ | ||
201 | struct GNUNET_REST_RequestHandle *rest_handle; | ||
202 | |||
203 | /** | ||
204 | * Desired timeout for the lookup (default is no timeout). | ||
205 | */ | ||
206 | struct GNUNET_TIME_Relative timeout; | ||
207 | |||
208 | /** | ||
209 | * ID of a task associated with the resolution process. | ||
210 | */ | ||
211 | struct GNUNET_SCHEDULER_Task *timeout_task; | ||
212 | |||
213 | /** | ||
214 | * The plugin result processor | ||
215 | */ | ||
216 | GNUNET_REST_ResultProcessor proc; | ||
217 | |||
218 | /** | ||
219 | * The closure of the result processor | ||
220 | */ | ||
221 | void *proc_cls; | ||
222 | |||
223 | /** | ||
224 | * The url | ||
225 | */ | ||
226 | char *url; | ||
227 | |||
228 | /** | ||
229 | * Error response message | ||
230 | */ | ||
231 | char *emsg; | ||
232 | |||
233 | /** | ||
234 | * Response code | ||
235 | */ | ||
236 | int response_code; | ||
237 | |||
238 | }; | ||
239 | |||
240 | /** | ||
241 | * Cleanup lookup handle | ||
242 | * @param handle Handle to clean up | ||
243 | */ | ||
244 | static void | ||
245 | cleanup_handle (void *cls) | ||
246 | { | ||
247 | struct RequestHandle *handle = cls; | ||
248 | struct EgoEntry *ego_entry; | ||
249 | struct EgoEntry *ego_tmp; | ||
250 | |||
251 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
252 | "Cleaning up\n"); | ||
253 | if (NULL != handle->timeout_task) | ||
254 | { | ||
255 | GNUNET_SCHEDULER_cancel (handle->timeout_task); | ||
256 | handle->timeout_task = NULL; | ||
257 | } | ||
258 | if (NULL != handle->record_name) | ||
259 | GNUNET_free(handle->record_name); | ||
260 | if (NULL != handle->url) | ||
261 | GNUNET_free(handle->url); | ||
262 | if (NULL != handle->emsg) | ||
263 | GNUNET_free(handle->emsg); | ||
264 | if (NULL != handle->rd) | ||
265 | { | ||
266 | if (NULL != handle->rd->data) | ||
267 | GNUNET_free((void*)handle->rd->data); | ||
268 | GNUNET_free(handle->rd); | ||
269 | } | ||
270 | if (NULL != handle->timeout_task) | ||
271 | GNUNET_SCHEDULER_cancel(handle->timeout_task); | ||
272 | if (NULL != handle->list_it) | ||
273 | GNUNET_NAMESTORE_zone_iteration_stop(handle->list_it); | ||
274 | if (NULL != handle->add_qe) | ||
275 | GNUNET_NAMESTORE_cancel(handle->add_qe); | ||
276 | if (NULL != handle->identity_handle) | ||
277 | GNUNET_IDENTITY_disconnect(handle->identity_handle); | ||
278 | if (NULL != handle->ns_handle) | ||
279 | { | ||
280 | GNUNET_NAMESTORE_disconnect(handle->ns_handle); | ||
281 | } | ||
282 | |||
283 | for (ego_entry = handle->ego_head; | ||
284 | NULL != ego_entry;) | ||
285 | { | ||
286 | ego_tmp = ego_entry; | ||
287 | ego_entry = ego_entry->next; | ||
288 | GNUNET_free(ego_tmp->identifier); | ||
289 | GNUNET_free(ego_tmp->keystring); | ||
290 | GNUNET_free(ego_tmp); | ||
291 | } | ||
292 | |||
293 | if(NULL != handle->resp_object) | ||
294 | { | ||
295 | json_decref(handle->resp_object); | ||
296 | } | ||
297 | |||
298 | GNUNET_free (handle); | ||
299 | } | ||
300 | |||
301 | |||
302 | /** | ||
303 | * Task run on errors. Reports an error and cleans up everything. | ||
304 | * | ||
305 | * @param cls the `struct RequestHandle` | ||
306 | */ | ||
307 | static void | ||
308 | do_error (void *cls) | ||
309 | { | ||
310 | struct RequestHandle *handle = cls; | ||
311 | struct MHD_Response *resp; | ||
312 | json_t *json_error = json_object(); | ||
313 | char *response; | ||
314 | |||
315 | if (NULL == handle->emsg) | ||
316 | handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_ERROR_UNKNOWN); | ||
317 | |||
318 | json_object_set_new(json_error,"error", json_string(handle->emsg)); | ||
319 | |||
320 | if (0 == handle->response_code) | ||
321 | handle->response_code = MHD_HTTP_OK; | ||
322 | response = json_dumps (json_error, 0); | ||
323 | resp = GNUNET_REST_create_response (response); | ||
324 | handle->proc (handle->proc_cls, resp, handle->response_code); | ||
325 | json_decref(json_error); | ||
326 | GNUNET_free(response); | ||
327 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
328 | } | ||
329 | |||
330 | |||
331 | /** | ||
332 | * Get EgoEntry from list with either a public key or a name | ||
333 | * If public key and name are not NULL, it returns the public key result first | ||
334 | * | ||
335 | * @param handle the RequestHandle | ||
336 | * @param pubkey the public key of an identity (only one can be NULL) | ||
337 | * @param name the name of an identity (only one can be NULL) | ||
338 | * @return EgoEntry or NULL if not found | ||
339 | */ | ||
340 | struct EgoEntry* | ||
341 | get_egoentry_namestore(struct RequestHandle *handle, char *name) | ||
342 | { | ||
343 | struct EgoEntry *ego_entry; | ||
344 | if (NULL != name) | ||
345 | { | ||
346 | for (ego_entry = handle->ego_head; | ||
347 | NULL != ego_entry; | ||
348 | ego_entry = ego_entry->next) | ||
349 | { | ||
350 | if (0 != strcasecmp (name, ego_entry->identifier)) | ||
351 | continue; | ||
352 | return ego_entry; | ||
353 | } | ||
354 | } | ||
355 | return NULL; | ||
356 | } | ||
357 | |||
358 | |||
359 | /** | ||
360 | * Does internal server error when iteration failed. | ||
361 | * | ||
362 | * @param cls the `struct RequestHandle` | ||
363 | */ | ||
364 | static void | ||
365 | namestore_iteration_error (void *cls) | ||
366 | { | ||
367 | struct RequestHandle *handle = cls; | ||
368 | handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_FAILED); | ||
369 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
370 | return; | ||
371 | } | ||
372 | |||
373 | |||
374 | /** | ||
375 | * Create finished callback | ||
376 | * | ||
377 | * @param cls the `struct RequestHandle` | ||
378 | * @param success the success indicating integer, GNUNET_OK on success | ||
379 | * @param emsg the error message (can be NULL) | ||
380 | */ | ||
381 | static void | ||
382 | create_finished (void *cls, int32_t success, const char *emsg) | ||
383 | { | ||
384 | struct RequestHandle *handle = cls; | ||
385 | struct MHD_Response *resp; | ||
386 | |||
387 | handle->add_qe = NULL; | ||
388 | if (GNUNET_YES != success) | ||
389 | { | ||
390 | if (NULL != emsg) | ||
391 | { | ||
392 | handle->emsg = GNUNET_strdup(emsg); | ||
393 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
394 | return; | ||
395 | } | ||
396 | handle->emsg = GNUNET_strdup("Error storing records"); | ||
397 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
398 | return; | ||
399 | } | ||
400 | resp = GNUNET_REST_create_response (NULL); | ||
401 | handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT); | ||
402 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
403 | } | ||
404 | |||
405 | |||
406 | /** | ||
407 | * Delete finished callback | ||
408 | * | ||
409 | * @param cls the `struct RequestHandle` | ||
410 | * @param success the success indicating integer, GNUNET_OK on success | ||
411 | * @param emsg the error message (can be NULL) | ||
412 | */ | ||
413 | static void | ||
414 | del_finished (void *cls, int32_t success, const char *emsg) | ||
415 | { | ||
416 | struct RequestHandle *handle = cls; | ||
417 | |||
418 | handle->add_qe = NULL; | ||
419 | if (GNUNET_NO == success) | ||
420 | { | ||
421 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
422 | handle->emsg = GNUNET_strdup("No record found"); | ||
423 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
424 | return; | ||
425 | } | ||
426 | if (GNUNET_SYSERR == success) | ||
427 | { | ||
428 | if (NULL != emsg) | ||
429 | { | ||
430 | handle->emsg = GNUNET_strdup(emsg); | ||
431 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
432 | return; | ||
433 | } | ||
434 | handle->emsg = GNUNET_strdup("Deleting record failed"); | ||
435 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
436 | return; | ||
437 | } | ||
438 | handle->proc (handle->proc_cls, | ||
439 | GNUNET_REST_create_response (NULL), | ||
440 | MHD_HTTP_NO_CONTENT); | ||
441 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
442 | } | ||
443 | |||
444 | |||
445 | /** | ||
446 | * Iteration over all results finished, build final | ||
447 | * response. | ||
448 | * | ||
449 | * @param cls the `struct RequestHandle` | ||
450 | */ | ||
451 | static void | ||
452 | namestore_list_finished (void *cls) | ||
453 | { | ||
454 | struct RequestHandle *handle = cls; | ||
455 | char *result_str; | ||
456 | struct MHD_Response *resp; | ||
457 | |||
458 | handle->list_it = NULL; | ||
459 | |||
460 | if (NULL == handle->resp_object) | ||
461 | { | ||
462 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
463 | return; | ||
464 | } | ||
465 | |||
466 | result_str = json_dumps (handle->resp_object, 0); | ||
467 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); | ||
468 | resp = GNUNET_REST_create_response (result_str); | ||
469 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | ||
470 | GNUNET_free_non_null (result_str); | ||
471 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
472 | } | ||
473 | |||
474 | |||
475 | /** | ||
476 | * Create a response with requested records | ||
477 | * | ||
478 | * @param handle the RequestHandle | ||
479 | */ | ||
480 | static void | ||
481 | namestore_list_iteration (void *cls, | ||
482 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key, | ||
483 | const char *rname, | ||
484 | unsigned int rd_len, | ||
485 | const struct GNUNET_GNSRECORD_Data *rd) | ||
486 | { | ||
487 | struct RequestHandle *handle = cls; | ||
488 | json_t *record_obj; | ||
489 | |||
490 | if (NULL == handle->resp_object) | ||
491 | handle->resp_object = json_array(); | ||
492 | |||
493 | for (unsigned int i = 0; i < rd_len; i++) | ||
494 | { | ||
495 | if ( (GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type) && | ||
496 | (0 != strcmp (rname, GNUNET_GNS_EMPTY_LABEL_AT)) ) | ||
497 | continue; | ||
498 | |||
499 | record_obj = GNUNET_JSON_from_gns_record(rname,rd); | ||
500 | |||
501 | if(NULL == record_obj) | ||
502 | continue; | ||
503 | |||
504 | json_array_append (handle->resp_object, record_obj); | ||
505 | json_decref (record_obj); | ||
506 | } | ||
507 | |||
508 | GNUNET_NAMESTORE_zone_iterator_next (handle->list_it, 1); | ||
509 | } | ||
510 | |||
511 | |||
512 | /** | ||
513 | * Handle namestore GET request | ||
514 | * | ||
515 | * @param con_handle the connection handle | ||
516 | * @param url the url | ||
517 | * @param cls the RequestHandle | ||
518 | */ | ||
519 | void | ||
520 | namestore_get (struct GNUNET_REST_RequestHandle *con_handle, | ||
521 | const char* url, | ||
522 | void *cls) | ||
523 | { | ||
524 | struct RequestHandle *handle = cls; | ||
525 | struct EgoEntry *ego_entry; | ||
526 | char *egoname; | ||
527 | |||
528 | egoname = NULL; | ||
529 | ego_entry = NULL; | ||
530 | |||
531 | //set zone to name if given | ||
532 | if (strlen (GNUNET_REST_API_NS_NAMESTORE) < strlen (handle->url)) | ||
533 | { | ||
534 | egoname = &handle->url[strlen (GNUNET_REST_API_NS_NAMESTORE)+1]; | ||
535 | ego_entry = get_egoentry_namestore(handle, egoname); | ||
536 | |||
537 | if (NULL == ego_entry) | ||
538 | { | ||
539 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
540 | handle->emsg = GNUNET_strdup(GNUNET_REST_IDENTITY_NOT_FOUND); | ||
541 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
542 | return; | ||
543 | } | ||
544 | } | ||
545 | if ( NULL != ego_entry ) | ||
546 | { | ||
547 | handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key(ego_entry->ego); | ||
548 | } | ||
549 | if (NULL == handle->zone_pkey) | ||
550 | { | ||
551 | handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_NO_DEFAULT_ZONE); | ||
552 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
553 | return; | ||
554 | } | ||
555 | handle->list_it = GNUNET_NAMESTORE_zone_iteration_start (handle->ns_handle, | ||
556 | handle->zone_pkey, | ||
557 | &namestore_iteration_error, | ||
558 | handle, | ||
559 | &namestore_list_iteration, | ||
560 | handle, | ||
561 | &namestore_list_finished, | ||
562 | handle); | ||
563 | if (NULL == handle->list_it) | ||
564 | { | ||
565 | handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_FAILED); | ||
566 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
567 | return; | ||
568 | } | ||
569 | } | ||
570 | |||
571 | |||
572 | /** | ||
573 | * Handle namestore POST request | ||
574 | * | ||
575 | * @param con_handle the connection handle | ||
576 | * @param url the url | ||
577 | * @param cls the RequestHandle | ||
578 | */ | ||
579 | void | ||
580 | namestore_add (struct GNUNET_REST_RequestHandle *con_handle, | ||
581 | const char* url, | ||
582 | void *cls) | ||
583 | { | ||
584 | struct RequestHandle *handle = cls; | ||
585 | struct GNUNET_GNSRECORD_Data *gns_record; | ||
586 | struct EgoEntry *ego_entry; | ||
587 | char *egoname; | ||
588 | json_t *data_js; | ||
589 | json_t *name_json; | ||
590 | json_error_t err; | ||
591 | char term_data[handle->rest_handle->data_size + 1]; | ||
592 | |||
593 | struct GNUNET_JSON_Specification gnsspec[] = { | ||
594 | GNUNET_JSON_spec_gnsrecord_data(&gns_record), | ||
595 | GNUNET_JSON_spec_end () | ||
596 | }; | ||
597 | |||
598 | if (0 >= handle->rest_handle->data_size) | ||
599 | { | ||
600 | handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_NO_DATA); | ||
601 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
602 | return; | ||
603 | } | ||
604 | term_data[handle->rest_handle->data_size] = '\0'; | ||
605 | GNUNET_memcpy(term_data, handle->rest_handle->data, | ||
606 | handle->rest_handle->data_size); | ||
607 | data_js = json_loads (term_data, JSON_DECODE_ANY, &err); | ||
608 | if (GNUNET_OK != GNUNET_JSON_parse (data_js, gnsspec, NULL, NULL)) | ||
609 | { | ||
610 | handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_INVALID_DATA); | ||
611 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
612 | GNUNET_JSON_parse_free(gnsspec); | ||
613 | json_decref (data_js); | ||
614 | return; | ||
615 | } | ||
616 | handle->rd = gns_record; | ||
617 | |||
618 | name_json = json_object_get(data_js, "record_name"); | ||
619 | if (!json_is_string(name_json)) | ||
620 | { | ||
621 | handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_INVALID_DATA); | ||
622 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
623 | json_decref (data_js); | ||
624 | return; | ||
625 | } | ||
626 | handle->record_name = GNUNET_strdup(json_string_value(name_json)); | ||
627 | if(NULL == handle->record_name) | ||
628 | { | ||
629 | handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_INVALID_DATA); | ||
630 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
631 | json_decref (data_js); | ||
632 | return; | ||
633 | } | ||
634 | if (0 >= strlen(handle->record_name)) | ||
635 | { | ||
636 | handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_INVALID_DATA); | ||
637 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
638 | json_decref (data_js); | ||
639 | return; | ||
640 | } | ||
641 | json_decref (data_js); | ||
642 | |||
643 | egoname = NULL; | ||
644 | ego_entry = NULL; | ||
645 | |||
646 | //set zone to name if given | ||
647 | if (strlen (GNUNET_REST_API_NS_NAMESTORE) < strlen (handle->url)) | ||
648 | { | ||
649 | egoname = &handle->url[strlen (GNUNET_REST_API_NS_NAMESTORE)+1]; | ||
650 | ego_entry = get_egoentry_namestore(handle, egoname); | ||
651 | |||
652 | if (NULL == ego_entry) | ||
653 | { | ||
654 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
655 | handle->emsg = GNUNET_strdup(GNUNET_REST_IDENTITY_NOT_FOUND); | ||
656 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
657 | return; | ||
658 | } | ||
659 | } | ||
660 | if (NULL != ego_entry) | ||
661 | { | ||
662 | handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key(ego_entry->ego); | ||
663 | } | ||
664 | if (NULL == handle->zone_pkey) | ||
665 | { | ||
666 | handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_NO_DEFAULT_ZONE); | ||
667 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
668 | return; | ||
669 | } | ||
670 | handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle, | ||
671 | handle->zone_pkey, | ||
672 | handle->record_name, | ||
673 | 1, | ||
674 | handle->rd, | ||
675 | &create_finished, | ||
676 | handle); | ||
677 | if (NULL == handle->add_qe) | ||
678 | { | ||
679 | handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_FAILED); | ||
680 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
681 | return; | ||
682 | } | ||
683 | } | ||
684 | |||
685 | |||
686 | /** | ||
687 | * Handle namestore DELETE request | ||
688 | * | ||
689 | * @param con_handle the connection handle | ||
690 | * @param url the url | ||
691 | * @param cls the RequestHandle | ||
692 | */ | ||
693 | void | ||
694 | namestore_delete (struct GNUNET_REST_RequestHandle *con_handle, | ||
695 | const char* url, | ||
696 | void *cls) | ||
697 | { | ||
698 | struct RequestHandle *handle = cls; | ||
699 | struct GNUNET_HashCode key; | ||
700 | struct EgoEntry *ego_entry; | ||
701 | char *egoname; | ||
702 | |||
703 | egoname = NULL; | ||
704 | ego_entry = NULL; | ||
705 | |||
706 | //set zone to name if given | ||
707 | if (strlen (GNUNET_REST_API_NS_NAMESTORE) < strlen (handle->url)) | ||
708 | { | ||
709 | egoname = &handle->url[strlen (GNUNET_REST_API_NS_NAMESTORE)+1]; | ||
710 | ego_entry = get_egoentry_namestore(handle, egoname); | ||
711 | |||
712 | if (NULL == ego_entry) | ||
713 | { | ||
714 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
715 | handle->emsg = GNUNET_strdup(GNUNET_REST_IDENTITY_NOT_FOUND); | ||
716 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
717 | return; | ||
718 | } | ||
719 | } | ||
720 | if ( NULL != ego_entry ) | ||
721 | { | ||
722 | handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key(ego_entry->ego); | ||
723 | } | ||
724 | |||
725 | GNUNET_CRYPTO_hash ("record_name", strlen ("record_name"), &key); | ||
726 | if ( GNUNET_NO | ||
727 | == GNUNET_CONTAINER_multihashmap_contains (con_handle->url_param_map, | ||
728 | &key)) | ||
729 | { | ||
730 | handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_INVALID_DATA); | ||
731 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
732 | return; | ||
733 | } | ||
734 | handle->record_name = GNUNET_strdup( | ||
735 | GNUNET_CONTAINER_multihashmap_get (con_handle->url_param_map, &key)); | ||
736 | |||
737 | if (NULL == handle->zone_pkey) | ||
738 | { | ||
739 | handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_NO_DEFAULT_ZONE); | ||
740 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
741 | return; | ||
742 | } | ||
743 | |||
744 | handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle, | ||
745 | handle->zone_pkey, | ||
746 | handle->record_name, | ||
747 | 0, | ||
748 | NULL, | ||
749 | &del_finished, | ||
750 | handle); | ||
751 | if (NULL == handle->add_qe) | ||
752 | { | ||
753 | handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_FAILED); | ||
754 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
755 | return; | ||
756 | } | ||
757 | } | ||
758 | |||
759 | |||
760 | |||
761 | /** | ||
762 | * Respond to OPTIONS request | ||
763 | * | ||
764 | * @param con_handle the connection handle | ||
765 | * @param url the url | ||
766 | * @param cls the RequestHandle | ||
767 | */ | ||
768 | static void | ||
769 | options_cont (struct GNUNET_REST_RequestHandle *con_handle, | ||
770 | const char* url, | ||
771 | void *cls) | ||
772 | { | ||
773 | struct MHD_Response *resp; | ||
774 | struct RequestHandle *handle = cls; | ||
775 | |||
776 | //independent of path return all options | ||
777 | resp = GNUNET_REST_create_response (NULL); | ||
778 | MHD_add_response_header (resp, | ||
779 | "Access-Control-Allow-Methods", | ||
780 | allow_methods); | ||
781 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | ||
782 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
783 | return; | ||
784 | } | ||
785 | |||
786 | |||
787 | /** | ||
788 | * Handle rest request | ||
789 | * | ||
790 | * @param handle the request handle | ||
791 | */ | ||
792 | static void | ||
793 | init_cont (struct RequestHandle *handle) | ||
794 | { | ||
795 | struct GNUNET_REST_RequestHandlerError err; | ||
796 | static const struct GNUNET_REST_RequestHandler handlers[] = { | ||
797 | {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_NAMESTORE, &namestore_get}, | ||
798 | {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_NAMESTORE, &namestore_add}, | ||
799 | {MHD_HTTP_METHOD_DELETE, GNUNET_REST_API_NS_NAMESTORE, &namestore_delete}, | ||
800 | {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_NAMESTORE, &options_cont}, | ||
801 | GNUNET_REST_HANDLER_END | ||
802 | }; | ||
803 | |||
804 | if (GNUNET_NO == GNUNET_REST_handle_request (handle->rest_handle, | ||
805 | handlers, | ||
806 | &err, | ||
807 | handle)) | ||
808 | { | ||
809 | handle->response_code = err.error_code; | ||
810 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
811 | } | ||
812 | } | ||
813 | |||
814 | /** | ||
815 | * @param cls closure | ||
816 | * @param ego ego handle | ||
817 | * @param ctx context for application to store data for this ego | ||
818 | * (during the lifetime of this process, initially NULL) | ||
819 | * @param identifier identifier assigned by the user for this ego, | ||
820 | * NULL if the user just deleted the ego and it | ||
821 | * must thus no longer be used | ||
822 | */ | ||
823 | static void | ||
824 | default_ego_cb (void *cls, | ||
825 | struct GNUNET_IDENTITY_Ego *ego, | ||
826 | void **ctx, | ||
827 | const char *identifier) | ||
828 | { | ||
829 | struct RequestHandle *handle = cls; | ||
830 | handle->op = NULL; | ||
831 | |||
832 | if (ego != NULL) | ||
833 | { | ||
834 | handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego); | ||
835 | } | ||
836 | } | ||
837 | |||
838 | |||
839 | /** | ||
840 | * This function is initially called for all egos and then again | ||
841 | * whenever a ego's identifier changes or if it is deleted. At the | ||
842 | * end of the initial pass over all egos, the function is once called | ||
843 | * with 'NULL' for 'ego'. That does NOT mean that the callback won't | ||
844 | * be invoked in the future or that there was an error. | ||
845 | * | ||
846 | * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get', | ||
847 | * this function is only called ONCE, and 'NULL' being passed in | ||
848 | * 'ego' does indicate an error (i.e. name is taken or no default | ||
849 | * value is known). If 'ego' is non-NULL and if '*ctx' | ||
850 | * is set in those callbacks, the value WILL be passed to a subsequent | ||
851 | * call to the identity callback of 'GNUNET_IDENTITY_connect' (if | ||
852 | * that one was not NULL). | ||
853 | * | ||
854 | * When an identity is renamed, this function is called with the | ||
855 | * (known) ego but the NEW identifier. | ||
856 | * | ||
857 | * When an identity is deleted, this function is called with the | ||
858 | * (known) ego and "NULL" for the 'identifier'. In this case, | ||
859 | * the 'ego' is henceforth invalid (and the 'ctx' should also be | ||
860 | * cleaned up). | ||
861 | * | ||
862 | * @param cls closure | ||
863 | * @param ego ego handle | ||
864 | * @param ctx context for application to store data for this ego | ||
865 | * (during the lifetime of this process, initially NULL) | ||
866 | * @param name identifier assigned by the user for this ego, | ||
867 | * NULL if the user just deleted the ego and it | ||
868 | * must thus no longer be used | ||
869 | */ | ||
870 | static void | ||
871 | id_connect_cb (void *cls, | ||
872 | struct GNUNET_IDENTITY_Ego *ego, | ||
873 | void **ctx, | ||
874 | const char *name) | ||
875 | { | ||
876 | struct RequestHandle *handle = cls; | ||
877 | struct EgoEntry *ego_entry; | ||
878 | struct GNUNET_CRYPTO_EcdsaPublicKey pk; | ||
879 | |||
880 | if ((NULL == ego) && (NULL == handle->zone_pkey)) | ||
881 | { | ||
882 | handle->op = GNUNET_IDENTITY_get (handle->identity_handle, | ||
883 | "namestore", | ||
884 | &default_ego_cb, | ||
885 | handle); | ||
886 | } | ||
887 | if ((NULL == ego) && (ID_REST_STATE_INIT == handle->state)) | ||
888 | { | ||
889 | handle->state = ID_REST_STATE_POST_INIT; | ||
890 | init_cont (handle); | ||
891 | return; | ||
892 | } | ||
893 | if (ID_REST_STATE_INIT == handle->state) | ||
894 | { | ||
895 | ego_entry = GNUNET_new(struct EgoEntry); | ||
896 | GNUNET_IDENTITY_ego_get_public_key (ego, &pk); | ||
897 | ego_entry->keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk); | ||
898 | ego_entry->ego = ego; | ||
899 | GNUNET_asprintf (&ego_entry->identifier, "%s", name); | ||
900 | GNUNET_CONTAINER_DLL_insert_tail(handle->ego_head, handle->ego_tail, | ||
901 | ego_entry); | ||
902 | } | ||
903 | |||
904 | } | ||
905 | |||
906 | |||
907 | /** | ||
908 | * Function processing the REST call | ||
909 | * | ||
910 | * @param method HTTP method | ||
911 | * @param url URL of the HTTP request | ||
912 | * @param data body of the HTTP request (optional) | ||
913 | * @param data_size length of the body | ||
914 | * @param proc callback function for the result | ||
915 | * @param proc_cls closure for callback function | ||
916 | * @return GNUNET_OK if request accepted | ||
917 | */ | ||
918 | static void | ||
919 | rest_process_request(struct GNUNET_REST_RequestHandle *rest_handle, | ||
920 | GNUNET_REST_ResultProcessor proc, | ||
921 | void *proc_cls) | ||
922 | { | ||
923 | struct RequestHandle *handle = GNUNET_new (struct RequestHandle); | ||
924 | |||
925 | handle->response_code = 0; | ||
926 | handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL; | ||
927 | handle->proc_cls = proc_cls; | ||
928 | handle->proc = proc; | ||
929 | handle->rest_handle = rest_handle; | ||
930 | handle->zone_pkey = NULL; | ||
931 | |||
932 | handle->url = GNUNET_strdup (rest_handle->url); | ||
933 | if (handle->url[strlen (handle->url)-1] == '/') | ||
934 | handle->url[strlen (handle->url)-1] = '\0'; | ||
935 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n"); | ||
936 | |||
937 | handle->identity_handle = GNUNET_IDENTITY_connect (cfg, &id_connect_cb, handle); | ||
938 | handle->ns_handle = GNUNET_NAMESTORE_connect (cfg); | ||
939 | handle->timeout_task = | ||
940 | GNUNET_SCHEDULER_add_delayed (handle->timeout, | ||
941 | &do_error, | ||
942 | handle); | ||
943 | |||
944 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected\n"); | ||
945 | } | ||
946 | |||
947 | |||
948 | /** | ||
949 | * Entry point for the plugin. | ||
950 | * | ||
951 | * @param cls Config info | ||
952 | * @return NULL on error, otherwise the plugin context | ||
953 | */ | ||
954 | void * | ||
955 | libgnunet_plugin_rest_namestore_init (void *cls) | ||
956 | { | ||
957 | static struct Plugin plugin; | ||
958 | struct GNUNET_REST_Plugin *api; | ||
959 | |||
960 | cfg = cls; | ||
961 | if (NULL != plugin.cfg) | ||
962 | return NULL; /* can only initialize once! */ | ||
963 | memset (&plugin, 0, sizeof (struct Plugin)); | ||
964 | plugin.cfg = cfg; | ||
965 | api = GNUNET_new (struct GNUNET_REST_Plugin); | ||
966 | api->cls = &plugin; | ||
967 | api->name = GNUNET_REST_API_NS_NAMESTORE; | ||
968 | api->process_request = &rest_process_request; | ||
969 | GNUNET_asprintf (&allow_methods, | ||
970 | "%s, %s, %s, %s, %s", | ||
971 | MHD_HTTP_METHOD_GET, | ||
972 | MHD_HTTP_METHOD_POST, | ||
973 | MHD_HTTP_METHOD_PUT, | ||
974 | MHD_HTTP_METHOD_DELETE, | ||
975 | MHD_HTTP_METHOD_OPTIONS); | ||
976 | |||
977 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
978 | _("Namestore REST API initialized\n")); | ||
979 | return api; | ||
980 | } | ||
981 | |||
982 | |||
983 | /** | ||
984 | * Exit point from the plugin. | ||
985 | * | ||
986 | * @param cls the plugin context (as returned by "init") | ||
987 | * @return always NULL | ||
988 | */ | ||
989 | void * | ||
990 | libgnunet_plugin_rest_namestore_done (void *cls) | ||
991 | { | ||
992 | struct GNUNET_REST_Plugin *api = cls; | ||
993 | struct Plugin *plugin = api->cls; | ||
994 | plugin->cfg = NULL; | ||
995 | |||
996 | GNUNET_free_non_null (allow_methods); | ||
997 | GNUNET_free (api); | ||
998 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
999 | "Namestore REST plugin is finished\n"); | ||
1000 | return NULL; | ||
1001 | } | ||
1002 | |||
1003 | /* end of plugin_rest_namestore.c */ | ||
1004 | |||
diff --git a/src/rest-plugins/plugin_rest_openid_connect.c b/src/rest-plugins/plugin_rest_openid_connect.c new file mode 100644 index 000000000..24673c692 --- /dev/null +++ b/src/rest-plugins/plugin_rest_openid_connect.c | |||
@@ -0,0 +1,2171 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2012-2015 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | /** | ||
19 | * @author Martin Schanzenbach | ||
20 | * @author Philippe Buschmann | ||
21 | * @file identity/plugin_rest_openid_connect.c | ||
22 | * @brief GNUnet Namestore REST plugin | ||
23 | * | ||
24 | */ | ||
25 | |||
26 | #include "platform.h" | ||
27 | #include "gnunet_rest_plugin.h" | ||
28 | #include "gnunet_identity_service.h" | ||
29 | #include "gnunet_gns_service.h" | ||
30 | #include "gnunet_gnsrecord_lib.h" | ||
31 | #include "gnunet_namestore_service.h" | ||
32 | #include "gnunet_rest_lib.h" | ||
33 | #include "gnunet_jsonapi_lib.h" | ||
34 | #include "gnunet_jsonapi_util.h" | ||
35 | #include "microhttpd.h" | ||
36 | #include <jansson.h> | ||
37 | #include <inttypes.h> | ||
38 | #include "gnunet_signatures.h" | ||
39 | #include "gnunet_reclaim_attribute_lib.h" | ||
40 | #include "gnunet_reclaim_service.h" | ||
41 | #include "oidc_helper.h" | ||
42 | |||
43 | /** | ||
44 | * REST root namespace | ||
45 | */ | ||
46 | #define GNUNET_REST_API_NS_OIDC "/openid" | ||
47 | |||
48 | /** | ||
49 | * Authorize endpoint | ||
50 | */ | ||
51 | #define GNUNET_REST_API_NS_AUTHORIZE "/openid/authorize" | ||
52 | |||
53 | /** | ||
54 | * Token endpoint | ||
55 | */ | ||
56 | #define GNUNET_REST_API_NS_TOKEN "/openid/token" | ||
57 | |||
58 | /** | ||
59 | * UserInfo endpoint | ||
60 | */ | ||
61 | #define GNUNET_REST_API_NS_USERINFO "/openid/userinfo" | ||
62 | |||
63 | /** | ||
64 | * Login namespace | ||
65 | */ | ||
66 | #define GNUNET_REST_API_NS_LOGIN "/openid/login" | ||
67 | |||
68 | /** | ||
69 | * Attribute key | ||
70 | */ | ||
71 | #define GNUNET_REST_JSONAPI_RECLAIM_ATTRIBUTE "attribute" | ||
72 | |||
73 | /** | ||
74 | * Ticket key | ||
75 | */ | ||
76 | #define GNUNET_REST_JSONAPI_IDENTITY_TICKET "ticket" | ||
77 | |||
78 | |||
79 | /** | ||
80 | * Value key | ||
81 | */ | ||
82 | #define GNUNET_REST_JSONAPI_RECLAIM_ATTRIBUTE_VALUE "value" | ||
83 | |||
84 | /** | ||
85 | * State while collecting all egos | ||
86 | */ | ||
87 | #define ID_REST_STATE_INIT 0 | ||
88 | |||
89 | /** | ||
90 | * Done collecting egos | ||
91 | */ | ||
92 | #define ID_REST_STATE_POST_INIT 1 | ||
93 | |||
94 | /** | ||
95 | * OIDC grant_type key | ||
96 | */ | ||
97 | #define OIDC_GRANT_TYPE_KEY "grant_type" | ||
98 | |||
99 | /** | ||
100 | * OIDC grant_type key | ||
101 | */ | ||
102 | #define OIDC_GRANT_TYPE_VALUE "authorization_code" | ||
103 | |||
104 | /** | ||
105 | * OIDC code key | ||
106 | */ | ||
107 | #define OIDC_CODE_KEY "code" | ||
108 | |||
109 | /** | ||
110 | * OIDC response_type key | ||
111 | */ | ||
112 | #define OIDC_RESPONSE_TYPE_KEY "response_type" | ||
113 | |||
114 | /** | ||
115 | * OIDC client_id key | ||
116 | */ | ||
117 | #define OIDC_CLIENT_ID_KEY "client_id" | ||
118 | |||
119 | /** | ||
120 | * OIDC scope key | ||
121 | */ | ||
122 | #define OIDC_SCOPE_KEY "scope" | ||
123 | |||
124 | /** | ||
125 | * OIDC redirect_uri key | ||
126 | */ | ||
127 | #define OIDC_REDIRECT_URI_KEY "redirect_uri" | ||
128 | |||
129 | /** | ||
130 | * OIDC state key | ||
131 | */ | ||
132 | #define OIDC_STATE_KEY "state" | ||
133 | |||
134 | /** | ||
135 | * OIDC nonce key | ||
136 | */ | ||
137 | #define OIDC_NONCE_KEY "nonce" | ||
138 | |||
139 | /** | ||
140 | * OIDC cookie header key | ||
141 | */ | ||
142 | #define OIDC_COOKIE_HEADER_KEY "cookie" | ||
143 | |||
144 | /** | ||
145 | * OIDC cookie header information key | ||
146 | */ | ||
147 | #define OIDC_AUTHORIZATION_HEADER_KEY "authorization" | ||
148 | |||
149 | /** | ||
150 | * OIDC cookie header information key | ||
151 | */ | ||
152 | #define OIDC_COOKIE_HEADER_INFORMATION_KEY "Identity=" | ||
153 | |||
154 | /** | ||
155 | * OIDC expected response_type while authorizing | ||
156 | */ | ||
157 | #define OIDC_EXPECTED_AUTHORIZATION_RESPONSE_TYPE "code" | ||
158 | |||
159 | /** | ||
160 | * OIDC expected scope part while authorizing | ||
161 | */ | ||
162 | #define OIDC_EXPECTED_AUTHORIZATION_SCOPE "openid" | ||
163 | |||
164 | /** | ||
165 | * OIDC ignored parameter array | ||
166 | */ | ||
167 | static char* OIDC_ignored_parameter_array [] = | ||
168 | { | ||
169 | "display", | ||
170 | "prompt", | ||
171 | "ui_locales", | ||
172 | "response_mode", | ||
173 | "id_token_hint", | ||
174 | "login_hint", | ||
175 | "acr_values" | ||
176 | }; | ||
177 | |||
178 | /** | ||
179 | * OIDC authorized identities and times hashmap | ||
180 | */ | ||
181 | struct GNUNET_CONTAINER_MultiHashMap *OIDC_identity_login_time; | ||
182 | |||
183 | /** | ||
184 | * OIDC authorized identities and times hashmap | ||
185 | */ | ||
186 | struct GNUNET_CONTAINER_MultiHashMap *OIDC_identity_grants; | ||
187 | |||
188 | /** | ||
189 | * OIDC ticket/code use only once | ||
190 | */ | ||
191 | struct GNUNET_CONTAINER_MultiHashMap *OIDC_ticket_once; | ||
192 | |||
193 | /** | ||
194 | * OIDC access_token to ticket and ego | ||
195 | */ | ||
196 | struct GNUNET_CONTAINER_MultiHashMap *OIDC_interpret_access_token; | ||
197 | |||
198 | /** | ||
199 | * The configuration handle | ||
200 | */ | ||
201 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
202 | |||
203 | /** | ||
204 | * HTTP methods allows for this plugin | ||
205 | */ | ||
206 | static char* allow_methods; | ||
207 | |||
208 | /** | ||
209 | * @brief struct returned by the initialization function of the plugin | ||
210 | */ | ||
211 | struct Plugin | ||
212 | { | ||
213 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
214 | }; | ||
215 | |||
216 | /** | ||
217 | * OIDC needed variables | ||
218 | */ | ||
219 | struct OIDC_Variables | ||
220 | { | ||
221 | /** | ||
222 | * The RP client public key | ||
223 | */ | ||
224 | struct GNUNET_CRYPTO_EcdsaPublicKey client_pkey; | ||
225 | |||
226 | /** | ||
227 | * The OIDC client id of the RP | ||
228 | */ | ||
229 | char *client_id; | ||
230 | |||
231 | /** | ||
232 | * The OIDC redirect uri | ||
233 | */ | ||
234 | char *redirect_uri; | ||
235 | |||
236 | /** | ||
237 | * The list of oidc scopes | ||
238 | */ | ||
239 | char *scope; | ||
240 | |||
241 | /** | ||
242 | * The OIDC state | ||
243 | */ | ||
244 | char *state; | ||
245 | |||
246 | /** | ||
247 | * The OIDC nonce | ||
248 | */ | ||
249 | char *nonce; | ||
250 | |||
251 | /** | ||
252 | * The OIDC response type | ||
253 | */ | ||
254 | char *response_type; | ||
255 | |||
256 | /** | ||
257 | * The identity chosen by the user to login | ||
258 | */ | ||
259 | char *login_identity; | ||
260 | |||
261 | /** | ||
262 | * The response JSON | ||
263 | */ | ||
264 | json_t *response; | ||
265 | |||
266 | }; | ||
267 | |||
268 | /** | ||
269 | * The ego list | ||
270 | */ | ||
271 | struct EgoEntry | ||
272 | { | ||
273 | /** | ||
274 | * DLL | ||
275 | */ | ||
276 | struct EgoEntry *next; | ||
277 | |||
278 | /** | ||
279 | * DLL | ||
280 | */ | ||
281 | struct EgoEntry *prev; | ||
282 | |||
283 | /** | ||
284 | * Ego Identifier | ||
285 | */ | ||
286 | char *identifier; | ||
287 | |||
288 | /** | ||
289 | * Public key string | ||
290 | */ | ||
291 | char *keystring; | ||
292 | |||
293 | /** | ||
294 | * The Ego | ||
295 | */ | ||
296 | struct GNUNET_IDENTITY_Ego *ego; | ||
297 | }; | ||
298 | |||
299 | |||
300 | struct RequestHandle | ||
301 | { | ||
302 | /** | ||
303 | * Ego list | ||
304 | */ | ||
305 | struct EgoEntry *ego_head; | ||
306 | |||
307 | /** | ||
308 | * Ego list | ||
309 | */ | ||
310 | struct EgoEntry *ego_tail; | ||
311 | |||
312 | /** | ||
313 | * Selected ego | ||
314 | */ | ||
315 | struct EgoEntry *ego_entry; | ||
316 | |||
317 | /** | ||
318 | * Pointer to ego private key | ||
319 | */ | ||
320 | struct GNUNET_CRYPTO_EcdsaPrivateKey priv_key; | ||
321 | |||
322 | /** | ||
323 | * OIDC variables | ||
324 | */ | ||
325 | struct OIDC_Variables *oidc; | ||
326 | |||
327 | /** | ||
328 | * The processing state | ||
329 | */ | ||
330 | int state; | ||
331 | |||
332 | /** | ||
333 | * Handle to Identity service. | ||
334 | */ | ||
335 | struct GNUNET_IDENTITY_Handle *identity_handle; | ||
336 | |||
337 | /** | ||
338 | * Rest connection | ||
339 | */ | ||
340 | struct GNUNET_REST_RequestHandle *rest_handle; | ||
341 | |||
342 | /** | ||
343 | * GNS handle | ||
344 | */ | ||
345 | struct GNUNET_GNS_Handle *gns_handle; | ||
346 | |||
347 | /** | ||
348 | * GNS lookup op | ||
349 | */ | ||
350 | struct GNUNET_GNS_LookupRequest *gns_op; | ||
351 | |||
352 | /** | ||
353 | * Handle to NAMESTORE | ||
354 | */ | ||
355 | struct GNUNET_NAMESTORE_Handle *namestore_handle; | ||
356 | |||
357 | /** | ||
358 | * Iterator for NAMESTORE | ||
359 | */ | ||
360 | struct GNUNET_NAMESTORE_ZoneIterator *namestore_handle_it; | ||
361 | |||
362 | /** | ||
363 | * Attribute claim list | ||
364 | */ | ||
365 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attr_list; | ||
366 | |||
367 | /** | ||
368 | * IDENTITY Operation | ||
369 | */ | ||
370 | struct GNUNET_IDENTITY_Operation *op; | ||
371 | |||
372 | /** | ||
373 | * Identity Provider | ||
374 | */ | ||
375 | struct GNUNET_RECLAIM_Handle *idp; | ||
376 | |||
377 | /** | ||
378 | * Idp Operation | ||
379 | */ | ||
380 | struct GNUNET_RECLAIM_Operation *idp_op; | ||
381 | |||
382 | /** | ||
383 | * Attribute iterator | ||
384 | */ | ||
385 | struct GNUNET_RECLAIM_AttributeIterator *attr_it; | ||
386 | |||
387 | /** | ||
388 | * Ticket iterator | ||
389 | */ | ||
390 | struct GNUNET_RECLAIM_TicketIterator *ticket_it; | ||
391 | |||
392 | /** | ||
393 | * A ticket | ||
394 | */ | ||
395 | struct GNUNET_RECLAIM_Ticket ticket; | ||
396 | |||
397 | /** | ||
398 | * Desired timeout for the lookup (default is no timeout). | ||
399 | */ | ||
400 | struct GNUNET_TIME_Relative timeout; | ||
401 | |||
402 | /** | ||
403 | * ID of a task associated with the resolution process. | ||
404 | */ | ||
405 | struct GNUNET_SCHEDULER_Task *timeout_task; | ||
406 | |||
407 | /** | ||
408 | * The plugin result processor | ||
409 | */ | ||
410 | GNUNET_REST_ResultProcessor proc; | ||
411 | |||
412 | /** | ||
413 | * The closure of the result processor | ||
414 | */ | ||
415 | void *proc_cls; | ||
416 | |||
417 | /** | ||
418 | * The url | ||
419 | */ | ||
420 | char *url; | ||
421 | |||
422 | /** | ||
423 | * The tld for redirect | ||
424 | */ | ||
425 | char *tld; | ||
426 | |||
427 | /** | ||
428 | * The redirect prefix | ||
429 | */ | ||
430 | char *redirect_prefix; | ||
431 | |||
432 | /** | ||
433 | * The redirect suffix | ||
434 | */ | ||
435 | char *redirect_suffix; | ||
436 | |||
437 | /** | ||
438 | * Error response message | ||
439 | */ | ||
440 | char *emsg; | ||
441 | |||
442 | /** | ||
443 | * Error response description | ||
444 | */ | ||
445 | char *edesc; | ||
446 | |||
447 | /** | ||
448 | * Reponse code | ||
449 | */ | ||
450 | int response_code; | ||
451 | |||
452 | /** | ||
453 | * Response object | ||
454 | */ | ||
455 | struct GNUNET_JSONAPI_Document *resp_object; | ||
456 | |||
457 | }; | ||
458 | |||
459 | /** | ||
460 | * Cleanup lookup handle | ||
461 | * @param handle Handle to clean up | ||
462 | */ | ||
463 | static void | ||
464 | cleanup_handle (struct RequestHandle *handle) | ||
465 | { | ||
466 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *claim_entry; | ||
467 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *claim_tmp; | ||
468 | struct EgoEntry *ego_entry; | ||
469 | struct EgoEntry *ego_tmp; | ||
470 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
471 | "Cleaning up\n"); | ||
472 | if (NULL != handle->resp_object) | ||
473 | GNUNET_JSONAPI_document_delete (handle->resp_object); | ||
474 | if (NULL != handle->timeout_task) | ||
475 | GNUNET_SCHEDULER_cancel (handle->timeout_task); | ||
476 | if (NULL != handle->identity_handle) | ||
477 | GNUNET_IDENTITY_disconnect (handle->identity_handle); | ||
478 | if (NULL != handle->attr_it) | ||
479 | GNUNET_RECLAIM_get_attributes_stop (handle->attr_it); | ||
480 | if (NULL != handle->ticket_it) | ||
481 | GNUNET_RECLAIM_ticket_iteration_stop (handle->ticket_it); | ||
482 | if (NULL != handle->idp) | ||
483 | GNUNET_RECLAIM_disconnect (handle->idp); | ||
484 | if (NULL != handle->url) | ||
485 | GNUNET_free (handle->url); | ||
486 | if (NULL != handle->tld) | ||
487 | GNUNET_free (handle->tld); | ||
488 | if (NULL != handle->redirect_prefix) | ||
489 | GNUNET_free (handle->redirect_prefix); | ||
490 | if (NULL != handle->redirect_suffix) | ||
491 | GNUNET_free (handle->redirect_suffix); | ||
492 | if (NULL != handle->emsg) | ||
493 | GNUNET_free (handle->emsg); | ||
494 | if (NULL != handle->edesc) | ||
495 | GNUNET_free (handle->edesc); | ||
496 | if (NULL != handle->gns_op) | ||
497 | GNUNET_GNS_lookup_cancel (handle->gns_op); | ||
498 | if (NULL != handle->gns_handle) | ||
499 | GNUNET_GNS_disconnect (handle->gns_handle); | ||
500 | |||
501 | if (NULL != handle->namestore_handle) | ||
502 | GNUNET_NAMESTORE_disconnect (handle->namestore_handle); | ||
503 | if (NULL != handle->oidc) | ||
504 | { | ||
505 | if (NULL != handle->oidc->client_id) | ||
506 | GNUNET_free(handle->oidc->client_id); | ||
507 | if (NULL != handle->oidc->login_identity) | ||
508 | GNUNET_free(handle->oidc->login_identity); | ||
509 | if (NULL != handle->oidc->nonce) | ||
510 | GNUNET_free(handle->oidc->nonce); | ||
511 | if (NULL != handle->oidc->redirect_uri) | ||
512 | GNUNET_free(handle->oidc->redirect_uri); | ||
513 | if (NULL != handle->oidc->response_type) | ||
514 | GNUNET_free(handle->oidc->response_type); | ||
515 | if (NULL != handle->oidc->scope) | ||
516 | GNUNET_free(handle->oidc->scope); | ||
517 | if (NULL != handle->oidc->state) | ||
518 | GNUNET_free(handle->oidc->state); | ||
519 | if (NULL != handle->oidc->response) | ||
520 | json_decref(handle->oidc->response); | ||
521 | GNUNET_free(handle->oidc); | ||
522 | } | ||
523 | if ( NULL != handle->attr_list ) | ||
524 | { | ||
525 | for (claim_entry = handle->attr_list->list_head; | ||
526 | NULL != claim_entry;) | ||
527 | { | ||
528 | claim_tmp = claim_entry; | ||
529 | claim_entry = claim_entry->next; | ||
530 | GNUNET_free(claim_tmp->claim); | ||
531 | GNUNET_free(claim_tmp); | ||
532 | } | ||
533 | GNUNET_free (handle->attr_list); | ||
534 | } | ||
535 | for (ego_entry = handle->ego_head; | ||
536 | NULL != ego_entry;) | ||
537 | { | ||
538 | ego_tmp = ego_entry; | ||
539 | ego_entry = ego_entry->next; | ||
540 | GNUNET_free (ego_tmp->identifier); | ||
541 | GNUNET_free (ego_tmp->keystring); | ||
542 | GNUNET_free (ego_tmp); | ||
543 | } | ||
544 | if (NULL != handle->attr_it) | ||
545 | { | ||
546 | GNUNET_free(handle->attr_it); | ||
547 | } | ||
548 | GNUNET_free (handle); | ||
549 | } | ||
550 | |||
551 | static void | ||
552 | cleanup_handle_delayed (void *cls) | ||
553 | { | ||
554 | cleanup_handle (cls); | ||
555 | } | ||
556 | |||
557 | |||
558 | /** | ||
559 | * Task run on error, sends error message. Cleans up everything. | ||
560 | * | ||
561 | * @param cls the `struct RequestHandle` | ||
562 | */ | ||
563 | static void | ||
564 | do_error (void *cls) | ||
565 | { | ||
566 | struct RequestHandle *handle = cls; | ||
567 | struct MHD_Response *resp; | ||
568 | char *json_error; | ||
569 | |||
570 | GNUNET_asprintf (&json_error, "{ \"error\" : \"%s\", \"error_description\" : \"%s\"%s%s%s}", | ||
571 | handle->emsg, | ||
572 | (NULL != handle->edesc) ? handle->edesc : "", | ||
573 | (NULL != handle->oidc->state) ? ", \"state\":\"" : "", | ||
574 | (NULL != handle->oidc->state) ? handle->oidc->state : "", | ||
575 | (NULL != handle->oidc->state) ? "\"" : ""); | ||
576 | if ( 0 == handle->response_code ) | ||
577 | { | ||
578 | handle->response_code = MHD_HTTP_BAD_REQUEST; | ||
579 | } | ||
580 | resp = GNUNET_REST_create_response (json_error); | ||
581 | if (MHD_HTTP_UNAUTHORIZED == handle->response_code) | ||
582 | { | ||
583 | MHD_add_response_header(resp, "WWW-Authenticate", "Basic"); | ||
584 | } | ||
585 | MHD_add_response_header (resp, "Content-Type", "application/json"); | ||
586 | handle->proc (handle->proc_cls, resp, handle->response_code); | ||
587 | GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle); | ||
588 | GNUNET_free (json_error); | ||
589 | } | ||
590 | |||
591 | |||
592 | /** | ||
593 | * Task run on error in userinfo endpoint, sends error header. Cleans up | ||
594 | * everything | ||
595 | * | ||
596 | * @param cls the `struct RequestHandle` | ||
597 | */ | ||
598 | static void | ||
599 | do_userinfo_error (void *cls) | ||
600 | { | ||
601 | struct RequestHandle *handle = cls; | ||
602 | struct MHD_Response *resp; | ||
603 | char *error; | ||
604 | |||
605 | GNUNET_asprintf (&error, "error=\"%s\", error_description=\"%s\"", | ||
606 | handle->emsg, | ||
607 | (NULL != handle->edesc) ? handle->edesc : ""); | ||
608 | resp = GNUNET_REST_create_response (""); | ||
609 | MHD_add_response_header(resp, "WWW-Authenticate", error); | ||
610 | handle->proc (handle->proc_cls, resp, handle->response_code); | ||
611 | GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle); | ||
612 | GNUNET_free (error); | ||
613 | } | ||
614 | |||
615 | |||
616 | /** | ||
617 | * Task run on error, sends error message and redirects. Cleans up everything. | ||
618 | * | ||
619 | * @param cls the `struct RequestHandle` | ||
620 | */ | ||
621 | static void | ||
622 | do_redirect_error (void *cls) | ||
623 | { | ||
624 | struct RequestHandle *handle = cls; | ||
625 | struct MHD_Response *resp; | ||
626 | char* redirect; | ||
627 | GNUNET_asprintf (&redirect, | ||
628 | "%s?error=%s&error_description=%s%s%s", | ||
629 | handle->oidc->redirect_uri, handle->emsg, handle->edesc, | ||
630 | (NULL != handle->oidc->state) ? "&state=" : "", | ||
631 | (NULL != handle->oidc->state) ? handle->oidc->state : ""); | ||
632 | resp = GNUNET_REST_create_response (""); | ||
633 | MHD_add_response_header (resp, "Location", redirect); | ||
634 | handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND); | ||
635 | GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle); | ||
636 | GNUNET_free (redirect); | ||
637 | } | ||
638 | |||
639 | /** | ||
640 | * Task run on timeout, sends error message. Cleans up everything. | ||
641 | * | ||
642 | * @param cls the `struct RequestHandle` | ||
643 | */ | ||
644 | static void | ||
645 | do_timeout (void *cls) | ||
646 | { | ||
647 | struct RequestHandle *handle = cls; | ||
648 | |||
649 | handle->timeout_task = NULL; | ||
650 | do_error (handle); | ||
651 | } | ||
652 | |||
653 | /** | ||
654 | * Return attributes for claim | ||
655 | * | ||
656 | * @param cls the request handle | ||
657 | */ | ||
658 | static void | ||
659 | return_userinfo_response (void *cls) | ||
660 | { | ||
661 | char* result_str; | ||
662 | struct RequestHandle *handle = cls; | ||
663 | struct MHD_Response *resp; | ||
664 | |||
665 | result_str = json_dumps (handle->oidc->response, 0); | ||
666 | |||
667 | resp = GNUNET_REST_create_response (result_str); | ||
668 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | ||
669 | GNUNET_free (result_str); | ||
670 | cleanup_handle (handle); | ||
671 | } | ||
672 | |||
673 | /** | ||
674 | * Returns base64 encoded string without padding | ||
675 | * | ||
676 | * @param string the string to encode | ||
677 | * @return base64 encoded string | ||
678 | */ | ||
679 | static char* | ||
680 | base_64_encode(const char *s) | ||
681 | { | ||
682 | char *enc; | ||
683 | char *tmp; | ||
684 | |||
685 | GNUNET_STRINGS_base64_encode(s, strlen(s), &enc); | ||
686 | tmp = strrchr (enc, '='); | ||
687 | *tmp = '\0'; | ||
688 | return enc; | ||
689 | } | ||
690 | |||
691 | /** | ||
692 | * Respond to OPTIONS request | ||
693 | * | ||
694 | * @param con_handle the connection handle | ||
695 | * @param url the url | ||
696 | * @param cls the RequestHandle | ||
697 | */ | ||
698 | static void | ||
699 | options_cont (struct GNUNET_REST_RequestHandle *con_handle, | ||
700 | const char* url, | ||
701 | void *cls) | ||
702 | { | ||
703 | struct MHD_Response *resp; | ||
704 | struct RequestHandle *handle = cls; | ||
705 | |||
706 | //For now, independent of path return all options | ||
707 | resp = GNUNET_REST_create_response (NULL); | ||
708 | MHD_add_response_header (resp, | ||
709 | "Access-Control-Allow-Methods", | ||
710 | allow_methods); | ||
711 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | ||
712 | cleanup_handle (handle); | ||
713 | return; | ||
714 | } | ||
715 | |||
716 | /** | ||
717 | * Interprets cookie header and pass its identity keystring to handle | ||
718 | */ | ||
719 | static void | ||
720 | cookie_identity_interpretation (struct RequestHandle *handle) | ||
721 | { | ||
722 | struct GNUNET_HashCode cache_key; | ||
723 | char *cookies; | ||
724 | struct GNUNET_TIME_Absolute current_time, *relog_time; | ||
725 | char delimiter[] = "; "; | ||
726 | |||
727 | //gets identity of login try with cookie | ||
728 | GNUNET_CRYPTO_hash (OIDC_COOKIE_HEADER_KEY, strlen (OIDC_COOKIE_HEADER_KEY), | ||
729 | &cache_key); | ||
730 | if ( GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->header_param_map, | ||
731 | &cache_key) ) | ||
732 | { | ||
733 | //splits cookies and find 'Identity' cookie | ||
734 | cookies = GNUNET_CONTAINER_multihashmap_get ( handle->rest_handle->header_param_map, &cache_key); | ||
735 | handle->oidc->login_identity = strtok(cookies, delimiter); | ||
736 | |||
737 | while ( NULL != handle->oidc->login_identity ) | ||
738 | { | ||
739 | if ( NULL != strstr (handle->oidc->login_identity, OIDC_COOKIE_HEADER_INFORMATION_KEY) ) | ||
740 | { | ||
741 | break; | ||
742 | } | ||
743 | handle->oidc->login_identity = strtok (NULL, delimiter); | ||
744 | } | ||
745 | GNUNET_CRYPTO_hash (handle->oidc->login_identity, strlen (handle->oidc->login_identity), | ||
746 | &cache_key); | ||
747 | if ( GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (OIDC_identity_login_time, &cache_key) ) | ||
748 | { | ||
749 | relog_time = GNUNET_CONTAINER_multihashmap_get (OIDC_identity_login_time, | ||
750 | &cache_key); | ||
751 | current_time = GNUNET_TIME_absolute_get (); | ||
752 | // 30 min after old login -> redirect to login | ||
753 | if ( current_time.abs_value_us <= relog_time->abs_value_us ) | ||
754 | { | ||
755 | handle->oidc->login_identity = strtok(handle->oidc->login_identity, OIDC_COOKIE_HEADER_INFORMATION_KEY); | ||
756 | handle->oidc->login_identity = GNUNET_strdup(handle->oidc->login_identity); | ||
757 | } else { | ||
758 | handle->oidc->login_identity = NULL; | ||
759 | } | ||
760 | } | ||
761 | else | ||
762 | { | ||
763 | handle->oidc->login_identity = NULL; | ||
764 | } | ||
765 | } | ||
766 | } | ||
767 | |||
768 | /** | ||
769 | * Redirects to login page stored in configuration file | ||
770 | */ | ||
771 | static void | ||
772 | login_redirection(void *cls) | ||
773 | { | ||
774 | char *login_base_url; | ||
775 | char *new_redirect; | ||
776 | struct MHD_Response *resp; | ||
777 | struct RequestHandle *handle = cls; | ||
778 | |||
779 | if ( GNUNET_OK | ||
780 | == GNUNET_CONFIGURATION_get_value_string (cfg, "reclaim-rest-plugin", | ||
781 | "address", &login_base_url) ) | ||
782 | { | ||
783 | GNUNET_asprintf (&new_redirect, "%s?%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s", | ||
784 | login_base_url, | ||
785 | OIDC_RESPONSE_TYPE_KEY, | ||
786 | handle->oidc->response_type, | ||
787 | OIDC_CLIENT_ID_KEY, | ||
788 | handle->oidc->client_id, | ||
789 | OIDC_REDIRECT_URI_KEY, | ||
790 | handle->oidc->redirect_uri, | ||
791 | OIDC_SCOPE_KEY, | ||
792 | handle->oidc->scope, | ||
793 | OIDC_STATE_KEY, | ||
794 | (NULL != handle->oidc->state) ? handle->oidc->state : "", | ||
795 | OIDC_NONCE_KEY, | ||
796 | (NULL != handle->oidc->nonce) ? handle->oidc->nonce : ""); | ||
797 | resp = GNUNET_REST_create_response (""); | ||
798 | MHD_add_response_header (resp, "Location", new_redirect); | ||
799 | GNUNET_free(login_base_url); | ||
800 | } | ||
801 | else | ||
802 | { | ||
803 | handle->emsg = GNUNET_strdup("server_error"); | ||
804 | handle->edesc = GNUNET_strdup ("gnunet configuration failed"); | ||
805 | handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; | ||
806 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
807 | return; | ||
808 | } | ||
809 | handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND); | ||
810 | GNUNET_free(new_redirect); | ||
811 | GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle); | ||
812 | } | ||
813 | |||
814 | /** | ||
815 | * Does internal server error when iteration failed. | ||
816 | */ | ||
817 | static void | ||
818 | oidc_iteration_error (void *cls) | ||
819 | { | ||
820 | struct RequestHandle *handle = cls; | ||
821 | handle->emsg = GNUNET_strdup("INTERNAL_SERVER_ERROR"); | ||
822 | handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; | ||
823 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
824 | } | ||
825 | |||
826 | static void | ||
827 | get_client_name_result (void *cls, | ||
828 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, | ||
829 | const char *label, | ||
830 | unsigned int rd_count, | ||
831 | const struct GNUNET_GNSRECORD_Data *rd) | ||
832 | { | ||
833 | struct RequestHandle *handle = cls; | ||
834 | struct MHD_Response *resp; | ||
835 | char *ticket_str; | ||
836 | char *redirect_uri; | ||
837 | char *code_json_string; | ||
838 | char *code_base64_final_string; | ||
839 | |||
840 | ticket_str = GNUNET_STRINGS_data_to_string_alloc (&handle->ticket, | ||
841 | sizeof (struct GNUNET_RECLAIM_Ticket)); | ||
842 | //TODO change if more attributes are needed (see max_age) | ||
843 | code_json_string = OIDC_build_authz_code (&handle->priv_key, | ||
844 | &handle->ticket, | ||
845 | handle->oidc->nonce); | ||
846 | code_base64_final_string = base_64_encode(code_json_string); | ||
847 | GNUNET_asprintf (&redirect_uri, "%s.%s/%s?%s=%s&state=%s", | ||
848 | handle->redirect_prefix, | ||
849 | handle->tld, | ||
850 | handle->redirect_suffix, | ||
851 | handle->oidc->response_type, | ||
852 | code_base64_final_string, handle->oidc->state); | ||
853 | resp = GNUNET_REST_create_response (""); | ||
854 | MHD_add_response_header (resp, "Location", redirect_uri); | ||
855 | handle->proc (handle->proc_cls, resp, MHD_HTTP_FOUND); | ||
856 | GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle); | ||
857 | GNUNET_free (redirect_uri); | ||
858 | GNUNET_free (ticket_str); | ||
859 | GNUNET_free (code_json_string); | ||
860 | GNUNET_free (code_base64_final_string); | ||
861 | return; | ||
862 | |||
863 | } | ||
864 | |||
865 | |||
866 | static void | ||
867 | get_client_name_error (void *cls) | ||
868 | { | ||
869 | struct RequestHandle *handle = cls; | ||
870 | |||
871 | handle->emsg = GNUNET_strdup("server_error"); | ||
872 | handle->edesc = GNUNET_strdup("Server cannot generate ticket, no name found for client."); | ||
873 | GNUNET_SCHEDULER_add_now (&do_redirect_error, handle); | ||
874 | } | ||
875 | |||
876 | |||
877 | static void | ||
878 | lookup_redirect_uri_result (void *cls, | ||
879 | uint32_t rd_count, | ||
880 | const struct GNUNET_GNSRECORD_Data *rd) | ||
881 | { | ||
882 | struct RequestHandle *handle = cls; | ||
883 | char *tmp; | ||
884 | char *tmp_key_str; | ||
885 | char *pos; | ||
886 | struct GNUNET_CRYPTO_EcdsaPublicKey redirect_zone; | ||
887 | |||
888 | handle->gns_op = NULL; | ||
889 | if (0 == rd_count) | ||
890 | { | ||
891 | handle->emsg = GNUNET_strdup("server_error"); | ||
892 | handle->edesc = GNUNET_strdup("Server cannot generate ticket, redirect uri not found."); | ||
893 | GNUNET_SCHEDULER_add_now (&do_redirect_error, handle); | ||
894 | return; | ||
895 | } | ||
896 | for (int i = 0; i < rd_count; i++) | ||
897 | { | ||
898 | if (GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT != rd[i].record_type) | ||
899 | continue; | ||
900 | if (0 != strcmp (rd[i].data, | ||
901 | handle->oidc->redirect_uri)) | ||
902 | continue; | ||
903 | tmp = GNUNET_strdup (rd[i].data); | ||
904 | pos = strrchr (tmp, | ||
905 | (unsigned char) '.'); | ||
906 | *pos = '\0'; | ||
907 | handle->redirect_prefix = GNUNET_strdup (tmp); | ||
908 | tmp_key_str = pos + 1; | ||
909 | pos = strchr (tmp_key_str, | ||
910 | (unsigned char) '/'); | ||
911 | *pos = '\0'; | ||
912 | handle->redirect_suffix = GNUNET_strdup (pos + 1); | ||
913 | |||
914 | GNUNET_STRINGS_string_to_data (tmp_key_str, | ||
915 | strlen (tmp_key_str), | ||
916 | &redirect_zone, | ||
917 | sizeof (redirect_zone)); | ||
918 | |||
919 | GNUNET_NAMESTORE_zone_to_name (handle->namestore_handle, | ||
920 | &handle->priv_key, | ||
921 | &redirect_zone, | ||
922 | &get_client_name_error, | ||
923 | handle, | ||
924 | &get_client_name_result, | ||
925 | handle); | ||
926 | GNUNET_free (tmp); | ||
927 | return; | ||
928 | } | ||
929 | handle->emsg = GNUNET_strdup("server_error"); | ||
930 | handle->edesc = GNUNET_strdup("Server cannot generate ticket, redirect uri not found."); | ||
931 | GNUNET_SCHEDULER_add_now (&do_redirect_error, handle); | ||
932 | } | ||
933 | |||
934 | /** | ||
935 | * Issues ticket and redirects to relying party with the authorization code as | ||
936 | * parameter. Otherwise redirects with error | ||
937 | */ | ||
938 | static void | ||
939 | oidc_ticket_issue_cb (void* cls, | ||
940 | const struct GNUNET_RECLAIM_Ticket *ticket) | ||
941 | { | ||
942 | struct RequestHandle *handle = cls; | ||
943 | |||
944 | handle->idp_op = NULL; | ||
945 | handle->ticket = *ticket; | ||
946 | if (NULL == ticket) | ||
947 | { | ||
948 | handle->emsg = GNUNET_strdup("server_error"); | ||
949 | handle->edesc = GNUNET_strdup("Server cannot generate ticket."); | ||
950 | GNUNET_SCHEDULER_add_now (&do_redirect_error, handle); | ||
951 | return; | ||
952 | } | ||
953 | handle->gns_op = GNUNET_GNS_lookup (handle->gns_handle, | ||
954 | "+", | ||
955 | &handle->oidc->client_pkey, | ||
956 | GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT, | ||
957 | GNUNET_GNS_LO_DEFAULT, | ||
958 | &lookup_redirect_uri_result, | ||
959 | handle); | ||
960 | |||
961 | } | ||
962 | |||
963 | static void | ||
964 | oidc_collect_finished_cb (void *cls) | ||
965 | { | ||
966 | struct RequestHandle *handle = cls; | ||
967 | handle->attr_it = NULL; | ||
968 | handle->ticket_it = NULL; | ||
969 | if (NULL == handle->attr_list->list_head) | ||
970 | { | ||
971 | handle->emsg = GNUNET_strdup("invalid_scope"); | ||
972 | handle->edesc = GNUNET_strdup("The requested scope is not available."); | ||
973 | GNUNET_SCHEDULER_add_now (&do_redirect_error, handle); | ||
974 | return; | ||
975 | } | ||
976 | handle->idp_op = GNUNET_RECLAIM_ticket_issue (handle->idp, | ||
977 | &handle->priv_key, | ||
978 | &handle->oidc->client_pkey, | ||
979 | handle->attr_list, | ||
980 | &oidc_ticket_issue_cb, | ||
981 | handle); | ||
982 | } | ||
983 | |||
984 | |||
985 | /** | ||
986 | * Collects all attributes for an ego if in scope parameter | ||
987 | */ | ||
988 | static void | ||
989 | oidc_attr_collect (void *cls, | ||
990 | const struct GNUNET_CRYPTO_EcdsaPublicKey *identity, | ||
991 | const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr) | ||
992 | { | ||
993 | struct RequestHandle *handle = cls; | ||
994 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le; | ||
995 | char* scope_variables; | ||
996 | char* scope_variable; | ||
997 | char delimiter[]=" "; | ||
998 | |||
999 | if ( (NULL == attr->name) || (NULL == attr->data) ) | ||
1000 | { | ||
1001 | GNUNET_RECLAIM_get_attributes_next (handle->attr_it); | ||
1002 | return; | ||
1003 | } | ||
1004 | |||
1005 | scope_variables = GNUNET_strdup(handle->oidc->scope); | ||
1006 | scope_variable = strtok (scope_variables, delimiter); | ||
1007 | while (NULL != scope_variable) | ||
1008 | { | ||
1009 | if ( 0 == strcmp (attr->name, scope_variable) ) | ||
1010 | { | ||
1011 | break; | ||
1012 | } | ||
1013 | scope_variable = strtok (NULL, delimiter); | ||
1014 | } | ||
1015 | if ( NULL == scope_variable ) | ||
1016 | { | ||
1017 | GNUNET_RECLAIM_get_attributes_next (handle->attr_it); | ||
1018 | GNUNET_free(scope_variables); | ||
1019 | return; | ||
1020 | } | ||
1021 | GNUNET_free(scope_variables); | ||
1022 | |||
1023 | le = GNUNET_new(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry); | ||
1024 | le->claim = GNUNET_RECLAIM_ATTRIBUTE_claim_new (attr->name, attr->type, | ||
1025 | attr->data, attr->data_size); | ||
1026 | GNUNET_CONTAINER_DLL_insert(handle->attr_list->list_head, | ||
1027 | handle->attr_list->list_tail, le); | ||
1028 | GNUNET_RECLAIM_get_attributes_next (handle->attr_it); | ||
1029 | } | ||
1030 | |||
1031 | |||
1032 | /** | ||
1033 | * Checks time and cookie and redirects accordingly | ||
1034 | */ | ||
1035 | static void | ||
1036 | login_check (void *cls) | ||
1037 | { | ||
1038 | struct RequestHandle *handle = cls; | ||
1039 | struct GNUNET_TIME_Absolute current_time, *relog_time; | ||
1040 | struct GNUNET_CRYPTO_EcdsaPublicKey pubkey, ego_pkey; | ||
1041 | struct GNUNET_HashCode cache_key; | ||
1042 | char *identity_cookie; | ||
1043 | |||
1044 | GNUNET_asprintf (&identity_cookie, "Identity=%s", handle->oidc->login_identity); | ||
1045 | GNUNET_CRYPTO_hash (identity_cookie, strlen (identity_cookie), &cache_key); | ||
1046 | GNUNET_free(identity_cookie); | ||
1047 | //No login time for identity -> redirect to login | ||
1048 | if ( GNUNET_YES | ||
1049 | == GNUNET_CONTAINER_multihashmap_contains (OIDC_identity_login_time, | ||
1050 | &cache_key) ) | ||
1051 | { | ||
1052 | relog_time = GNUNET_CONTAINER_multihashmap_get (OIDC_identity_login_time, | ||
1053 | &cache_key); | ||
1054 | current_time = GNUNET_TIME_absolute_get (); | ||
1055 | // 30 min after old login -> redirect to login | ||
1056 | if ( current_time.abs_value_us <= relog_time->abs_value_us ) | ||
1057 | { | ||
1058 | if ( GNUNET_OK | ||
1059 | != GNUNET_CRYPTO_ecdsa_public_key_from_string ( | ||
1060 | handle->oidc->login_identity, | ||
1061 | strlen (handle->oidc->login_identity), &pubkey) ) | ||
1062 | { | ||
1063 | handle->emsg = GNUNET_strdup("invalid_cookie"); | ||
1064 | handle->edesc = GNUNET_strdup( | ||
1065 | "The cookie of a login identity is not valid"); | ||
1066 | GNUNET_SCHEDULER_add_now (&do_redirect_error, handle); | ||
1067 | return; | ||
1068 | } | ||
1069 | // iterate over egos and compare their public key | ||
1070 | for (handle->ego_entry = handle->ego_head; | ||
1071 | NULL != handle->ego_entry; handle->ego_entry = handle->ego_entry->next) | ||
1072 | { | ||
1073 | GNUNET_IDENTITY_ego_get_public_key (handle->ego_entry->ego, &ego_pkey); | ||
1074 | if ( 0 | ||
1075 | == memcmp (&ego_pkey, &pubkey, | ||
1076 | sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)) ) | ||
1077 | { | ||
1078 | handle->priv_key = *GNUNET_IDENTITY_ego_get_private_key ( | ||
1079 | handle->ego_entry->ego); | ||
1080 | handle->resp_object = GNUNET_JSONAPI_document_new (); | ||
1081 | handle->idp = GNUNET_RECLAIM_connect (cfg); | ||
1082 | handle->attr_list = GNUNET_new( | ||
1083 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList); | ||
1084 | handle->attr_it = GNUNET_RECLAIM_get_attributes_start ( | ||
1085 | handle->idp, &handle->priv_key, &oidc_iteration_error, handle, | ||
1086 | &oidc_attr_collect, handle, &oidc_collect_finished_cb, handle); | ||
1087 | return; | ||
1088 | } | ||
1089 | } | ||
1090 | //handle->emsg = GNUNET_strdup("invalid_cookie"); | ||
1091 | //handle->edesc = GNUNET_strdup( | ||
1092 | // "The cookie of the login identity is not valid"); | ||
1093 | //GNUNET_SCHEDULER_add_now (&do_redirect_error, handle); | ||
1094 | GNUNET_SCHEDULER_add_now (&login_redirection,handle); | ||
1095 | return; | ||
1096 | } | ||
1097 | } | ||
1098 | } | ||
1099 | |||
1100 | /** | ||
1101 | * Iteration over all results finished, build final | ||
1102 | * response. | ||
1103 | * | ||
1104 | * @param cls the `struct RequestHandle` | ||
1105 | */ | ||
1106 | static void | ||
1107 | build_authz_response (void *cls) | ||
1108 | { | ||
1109 | struct RequestHandle *handle = cls; | ||
1110 | struct GNUNET_HashCode cache_key; | ||
1111 | |||
1112 | char *expected_scope; | ||
1113 | char delimiter[]=" "; | ||
1114 | int number_of_ignored_parameter, iterator; | ||
1115 | |||
1116 | |||
1117 | // REQUIRED value: redirect_uri | ||
1118 | GNUNET_CRYPTO_hash (OIDC_REDIRECT_URI_KEY, strlen (OIDC_REDIRECT_URI_KEY), | ||
1119 | &cache_key); | ||
1120 | if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->url_param_map, | ||
1121 | &cache_key)) | ||
1122 | { | ||
1123 | handle->emsg=GNUNET_strdup("invalid_request"); | ||
1124 | handle->edesc=GNUNET_strdup("missing parameter redirect_uri"); | ||
1125 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
1126 | return; | ||
1127 | } | ||
1128 | handle->oidc->redirect_uri = GNUNET_strdup (GNUNET_CONTAINER_multihashmap_get(handle->rest_handle->url_param_map, | ||
1129 | &cache_key)); | ||
1130 | |||
1131 | // REQUIRED value: response_type | ||
1132 | GNUNET_CRYPTO_hash (OIDC_RESPONSE_TYPE_KEY, strlen (OIDC_RESPONSE_TYPE_KEY), | ||
1133 | &cache_key); | ||
1134 | if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->url_param_map, | ||
1135 | &cache_key)) | ||
1136 | { | ||
1137 | handle->emsg=GNUNET_strdup("invalid_request"); | ||
1138 | handle->edesc=GNUNET_strdup("missing parameter response_type"); | ||
1139 | GNUNET_SCHEDULER_add_now (&do_redirect_error, handle); | ||
1140 | return; | ||
1141 | } | ||
1142 | handle->oidc->response_type = GNUNET_CONTAINER_multihashmap_get(handle->rest_handle->url_param_map, | ||
1143 | &cache_key); | ||
1144 | handle->oidc->response_type = GNUNET_strdup (handle->oidc->response_type); | ||
1145 | |||
1146 | // REQUIRED value: scope | ||
1147 | GNUNET_CRYPTO_hash (OIDC_SCOPE_KEY, strlen (OIDC_SCOPE_KEY), &cache_key); | ||
1148 | if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->url_param_map, | ||
1149 | &cache_key)) | ||
1150 | { | ||
1151 | handle->emsg=GNUNET_strdup("invalid_request"); | ||
1152 | handle->edesc=GNUNET_strdup("missing parameter scope"); | ||
1153 | GNUNET_SCHEDULER_add_now (&do_redirect_error, handle); | ||
1154 | return; | ||
1155 | } | ||
1156 | handle->oidc->scope = GNUNET_CONTAINER_multihashmap_get(handle->rest_handle->url_param_map, | ||
1157 | &cache_key); | ||
1158 | handle->oidc->scope = GNUNET_strdup(handle->oidc->scope); | ||
1159 | |||
1160 | //OPTIONAL value: nonce | ||
1161 | GNUNET_CRYPTO_hash (OIDC_NONCE_KEY, strlen (OIDC_NONCE_KEY), &cache_key); | ||
1162 | if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->url_param_map, | ||
1163 | &cache_key)) | ||
1164 | { | ||
1165 | handle->oidc->nonce = GNUNET_CONTAINER_multihashmap_get(handle->rest_handle->url_param_map, | ||
1166 | &cache_key); | ||
1167 | handle->oidc->nonce = GNUNET_strdup (handle->oidc->nonce); | ||
1168 | } | ||
1169 | |||
1170 | //TODO check other values if needed | ||
1171 | number_of_ignored_parameter = sizeof(OIDC_ignored_parameter_array) / sizeof(char *); | ||
1172 | for( iterator = 0; iterator < number_of_ignored_parameter; iterator++ ) | ||
1173 | { | ||
1174 | GNUNET_CRYPTO_hash (OIDC_ignored_parameter_array[iterator], | ||
1175 | strlen(OIDC_ignored_parameter_array[iterator]), | ||
1176 | &cache_key); | ||
1177 | if(GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(handle->rest_handle->url_param_map, | ||
1178 | &cache_key)) | ||
1179 | { | ||
1180 | handle->emsg=GNUNET_strdup("access_denied"); | ||
1181 | GNUNET_asprintf (&handle->edesc, "Server will not handle parameter: %s", | ||
1182 | OIDC_ignored_parameter_array[iterator]); | ||
1183 | GNUNET_SCHEDULER_add_now (&do_redirect_error, handle); | ||
1184 | return; | ||
1185 | } | ||
1186 | } | ||
1187 | |||
1188 | // Checks if response_type is 'code' | ||
1189 | if( 0 != strcmp( handle->oidc->response_type, OIDC_EXPECTED_AUTHORIZATION_RESPONSE_TYPE ) ) | ||
1190 | { | ||
1191 | handle->emsg=GNUNET_strdup("unsupported_response_type"); | ||
1192 | handle->edesc=GNUNET_strdup("The authorization server does not support " | ||
1193 | "obtaining this authorization code."); | ||
1194 | GNUNET_SCHEDULER_add_now (&do_redirect_error, handle); | ||
1195 | return; | ||
1196 | } | ||
1197 | |||
1198 | // Checks if scope contains 'openid' | ||
1199 | expected_scope = GNUNET_strdup(handle->oidc->scope); | ||
1200 | char* test; | ||
1201 | test = strtok (expected_scope, delimiter); | ||
1202 | while (NULL != test) | ||
1203 | { | ||
1204 | if ( 0 == strcmp (OIDC_EXPECTED_AUTHORIZATION_SCOPE, expected_scope) ) | ||
1205 | { | ||
1206 | break; | ||
1207 | } | ||
1208 | test = strtok (NULL, delimiter); | ||
1209 | } | ||
1210 | if (NULL == test) | ||
1211 | { | ||
1212 | handle->emsg = GNUNET_strdup("invalid_scope"); | ||
1213 | handle->edesc=GNUNET_strdup("The requested scope is invalid, unknown, or " | ||
1214 | "malformed."); | ||
1215 | GNUNET_SCHEDULER_add_now (&do_redirect_error, handle); | ||
1216 | GNUNET_free(expected_scope); | ||
1217 | return; | ||
1218 | } | ||
1219 | |||
1220 | GNUNET_free(expected_scope); | ||
1221 | |||
1222 | if( NULL != handle->oidc->login_identity ) | ||
1223 | { | ||
1224 | GNUNET_SCHEDULER_add_now(&login_check,handle); | ||
1225 | return; | ||
1226 | } | ||
1227 | |||
1228 | GNUNET_SCHEDULER_add_now(&login_redirection,handle); | ||
1229 | } | ||
1230 | |||
1231 | /** | ||
1232 | * Responds to authorization GET and url-encoded POST request | ||
1233 | * | ||
1234 | * @param con_handle the connection handle | ||
1235 | * @param url the url | ||
1236 | * @param cls the RequestHandle | ||
1237 | */ | ||
1238 | static void | ||
1239 | authorize_endpoint (struct GNUNET_REST_RequestHandle *con_handle, | ||
1240 | const char* url, | ||
1241 | void *cls) | ||
1242 | { | ||
1243 | struct RequestHandle *handle = cls; | ||
1244 | struct GNUNET_HashCode cache_key; | ||
1245 | struct EgoEntry *tmp_ego; | ||
1246 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key; | ||
1247 | struct GNUNET_CRYPTO_EcdsaPublicKey pkey; | ||
1248 | |||
1249 | cookie_identity_interpretation(handle); | ||
1250 | |||
1251 | //RECOMMENDED value: state - REQUIRED for answers | ||
1252 | GNUNET_CRYPTO_hash (OIDC_STATE_KEY, strlen (OIDC_STATE_KEY), &cache_key); | ||
1253 | if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->url_param_map, | ||
1254 | &cache_key)) | ||
1255 | { | ||
1256 | handle->oidc->state = GNUNET_CONTAINER_multihashmap_get(handle->rest_handle->url_param_map, | ||
1257 | &cache_key); | ||
1258 | handle->oidc->state = GNUNET_strdup (handle->oidc->state); | ||
1259 | } | ||
1260 | |||
1261 | // REQUIRED value: client_id | ||
1262 | GNUNET_CRYPTO_hash (OIDC_CLIENT_ID_KEY, strlen (OIDC_CLIENT_ID_KEY), | ||
1263 | &cache_key); | ||
1264 | if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->url_param_map, | ||
1265 | &cache_key)) | ||
1266 | { | ||
1267 | handle->emsg=GNUNET_strdup("invalid_request"); | ||
1268 | handle->edesc=GNUNET_strdup("missing parameter client_id"); | ||
1269 | handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; | ||
1270 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
1271 | return; | ||
1272 | } | ||
1273 | handle->oidc->client_id = GNUNET_strdup (GNUNET_CONTAINER_multihashmap_get(handle->rest_handle->url_param_map, | ||
1274 | &cache_key)); | ||
1275 | |||
1276 | if ( GNUNET_OK | ||
1277 | != GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->oidc->client_id, | ||
1278 | strlen (handle->oidc->client_id), | ||
1279 | &handle->oidc->client_pkey) ) | ||
1280 | { | ||
1281 | handle->emsg = GNUNET_strdup("unauthorized_client"); | ||
1282 | handle->edesc = GNUNET_strdup("The client is not authorized to request an " | ||
1283 | "authorization code using this method."); | ||
1284 | handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; | ||
1285 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
1286 | return; | ||
1287 | } | ||
1288 | |||
1289 | |||
1290 | if ( NULL == handle->ego_head ) | ||
1291 | { | ||
1292 | handle->emsg = GNUNET_strdup("server_error"); | ||
1293 | handle->edesc = GNUNET_strdup ("Egos are missing"); | ||
1294 | handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; | ||
1295 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
1296 | return; | ||
1297 | } | ||
1298 | |||
1299 | handle->ego_entry = handle->ego_head; | ||
1300 | handle->priv_key = *GNUNET_IDENTITY_ego_get_private_key (handle->ego_head->ego); | ||
1301 | //If we know this identity, translated the corresponding TLD | ||
1302 | //TODO: We might want to have a reverse lookup functionality for TLDs? | ||
1303 | for (tmp_ego = handle->ego_head; NULL != tmp_ego; tmp_ego = tmp_ego->next) | ||
1304 | { | ||
1305 | priv_key = GNUNET_IDENTITY_ego_get_private_key (tmp_ego->ego); | ||
1306 | GNUNET_CRYPTO_ecdsa_key_get_public (priv_key, | ||
1307 | &pkey); | ||
1308 | if ( 0 == memcmp (&pkey, &handle->oidc->client_pkey, | ||
1309 | sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)) ) | ||
1310 | { | ||
1311 | handle->tld = GNUNET_strdup (tmp_ego->identifier); | ||
1312 | handle->ego_entry = handle->ego_tail; | ||
1313 | } | ||
1314 | } | ||
1315 | GNUNET_SCHEDULER_add_now (&build_authz_response, handle); | ||
1316 | } | ||
1317 | |||
1318 | /** | ||
1319 | * Combines an identity with a login time and responds OK to login request | ||
1320 | * | ||
1321 | * @param con_handle the connection handle | ||
1322 | * @param url the url | ||
1323 | * @param cls the RequestHandle | ||
1324 | */ | ||
1325 | static void | ||
1326 | login_cont (struct GNUNET_REST_RequestHandle *con_handle, | ||
1327 | const char* url, | ||
1328 | void *cls) | ||
1329 | { | ||
1330 | struct MHD_Response *resp = GNUNET_REST_create_response (""); | ||
1331 | struct RequestHandle *handle = cls; | ||
1332 | struct GNUNET_HashCode cache_key; | ||
1333 | struct GNUNET_TIME_Absolute *current_time; | ||
1334 | struct GNUNET_TIME_Absolute *last_time; | ||
1335 | char* cookie; | ||
1336 | json_t *root; | ||
1337 | json_error_t error; | ||
1338 | json_t *identity; | ||
1339 | char term_data[handle->rest_handle->data_size+1]; | ||
1340 | term_data[handle->rest_handle->data_size] = '\0'; | ||
1341 | GNUNET_memcpy (term_data, handle->rest_handle->data, handle->rest_handle->data_size); | ||
1342 | root = json_loads (term_data, JSON_DECODE_ANY, &error); | ||
1343 | identity = json_object_get (root, "identity"); | ||
1344 | if ( json_is_string(identity) ) | ||
1345 | { | ||
1346 | GNUNET_asprintf (&cookie, "Identity=%s", json_string_value (identity)); | ||
1347 | MHD_add_response_header (resp, "Set-Cookie", cookie); | ||
1348 | MHD_add_response_header (resp, "Access-Control-Allow-Methods", "POST"); | ||
1349 | GNUNET_CRYPTO_hash (cookie, strlen (cookie), &cache_key); | ||
1350 | |||
1351 | current_time = GNUNET_new(struct GNUNET_TIME_Absolute); | ||
1352 | *current_time = GNUNET_TIME_relative_to_absolute ( | ||
1353 | GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_second_ (), | ||
1354 | 5)); | ||
1355 | last_time = GNUNET_CONTAINER_multihashmap_get(OIDC_identity_login_time, &cache_key); | ||
1356 | if (NULL != last_time) | ||
1357 | { | ||
1358 | GNUNET_free(last_time); | ||
1359 | } | ||
1360 | GNUNET_CONTAINER_multihashmap_put ( | ||
1361 | OIDC_identity_login_time, &cache_key, current_time, | ||
1362 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE); | ||
1363 | |||
1364 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | ||
1365 | GNUNET_free(cookie); | ||
1366 | } | ||
1367 | else | ||
1368 | { | ||
1369 | handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST); | ||
1370 | } | ||
1371 | json_decref (root); | ||
1372 | GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle); | ||
1373 | return; | ||
1374 | } | ||
1375 | |||
1376 | static int | ||
1377 | check_authorization (struct RequestHandle *handle, | ||
1378 | struct GNUNET_CRYPTO_EcdsaPublicKey *cid) | ||
1379 | { | ||
1380 | struct GNUNET_HashCode cache_key; | ||
1381 | char *authorization; | ||
1382 | char *credentials; | ||
1383 | char *basic_authorization; | ||
1384 | char *client_id; | ||
1385 | char *pass; | ||
1386 | char *expected_pass; | ||
1387 | int client_exists = GNUNET_NO; | ||
1388 | |||
1389 | GNUNET_CRYPTO_hash (OIDC_AUTHORIZATION_HEADER_KEY, | ||
1390 | strlen (OIDC_AUTHORIZATION_HEADER_KEY), | ||
1391 | &cache_key); | ||
1392 | if ( GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->header_param_map, | ||
1393 | &cache_key) ) | ||
1394 | { | ||
1395 | handle->emsg=GNUNET_strdup("invalid_client"); | ||
1396 | handle->edesc=GNUNET_strdup("missing authorization"); | ||
1397 | handle->response_code = MHD_HTTP_UNAUTHORIZED; | ||
1398 | return GNUNET_SYSERR; | ||
1399 | } | ||
1400 | authorization = GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->header_param_map, | ||
1401 | &cache_key); | ||
1402 | |||
1403 | //split header in "Basic" and [content] | ||
1404 | credentials = strtok (authorization, " "); | ||
1405 | if (0 != strcmp ("Basic", credentials)) | ||
1406 | { | ||
1407 | handle->emsg=GNUNET_strdup("invalid_client"); | ||
1408 | handle->response_code = MHD_HTTP_UNAUTHORIZED; | ||
1409 | return GNUNET_SYSERR; | ||
1410 | } | ||
1411 | credentials = strtok(NULL, " "); | ||
1412 | if (NULL == credentials) | ||
1413 | { | ||
1414 | handle->emsg=GNUNET_strdup("invalid_client"); | ||
1415 | handle->response_code = MHD_HTTP_UNAUTHORIZED; | ||
1416 | return GNUNET_SYSERR; | ||
1417 | } | ||
1418 | GNUNET_STRINGS_base64_decode (credentials, | ||
1419 | strlen (credentials), | ||
1420 | (void**)&basic_authorization); | ||
1421 | |||
1422 | if ( NULL == basic_authorization ) | ||
1423 | { | ||
1424 | handle->emsg=GNUNET_strdup("invalid_client"); | ||
1425 | handle->response_code = MHD_HTTP_UNAUTHORIZED; | ||
1426 | return GNUNET_SYSERR; | ||
1427 | } | ||
1428 | client_id = strtok (basic_authorization, ":"); | ||
1429 | if ( NULL == client_id ) | ||
1430 | { | ||
1431 | GNUNET_free_non_null(basic_authorization); | ||
1432 | handle->emsg=GNUNET_strdup("invalid_client"); | ||
1433 | handle->response_code = MHD_HTTP_UNAUTHORIZED; | ||
1434 | return GNUNET_SYSERR; | ||
1435 | } | ||
1436 | pass = strtok (NULL, ":"); | ||
1437 | if (NULL == pass) | ||
1438 | { | ||
1439 | GNUNET_free_non_null(basic_authorization); | ||
1440 | handle->emsg=GNUNET_strdup("invalid_client"); | ||
1441 | handle->response_code = MHD_HTTP_UNAUTHORIZED; | ||
1442 | return GNUNET_SYSERR; | ||
1443 | } | ||
1444 | |||
1445 | //check client password | ||
1446 | if ( GNUNET_OK | ||
1447 | == GNUNET_CONFIGURATION_get_value_string (cfg, "reclaim-rest-plugin", | ||
1448 | "psw", &expected_pass) ) | ||
1449 | { | ||
1450 | if (0 != strcmp (expected_pass, pass)) | ||
1451 | { | ||
1452 | GNUNET_free_non_null(basic_authorization); | ||
1453 | GNUNET_free(expected_pass); | ||
1454 | handle->emsg=GNUNET_strdup("invalid_client"); | ||
1455 | handle->response_code = MHD_HTTP_UNAUTHORIZED; | ||
1456 | return GNUNET_SYSERR; | ||
1457 | } | ||
1458 | GNUNET_free(expected_pass); | ||
1459 | } | ||
1460 | else | ||
1461 | { | ||
1462 | GNUNET_free_non_null(basic_authorization); | ||
1463 | handle->emsg = GNUNET_strdup("server_error"); | ||
1464 | handle->edesc = GNUNET_strdup ("gnunet configuration failed"); | ||
1465 | handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; | ||
1466 | return GNUNET_SYSERR; | ||
1467 | } | ||
1468 | |||
1469 | //check client_id | ||
1470 | for (handle->ego_entry = handle->ego_head; NULL != handle->ego_entry->next; ) | ||
1471 | { | ||
1472 | if ( 0 == strcmp(handle->ego_entry->keystring, client_id)) | ||
1473 | { | ||
1474 | client_exists = GNUNET_YES; | ||
1475 | break; | ||
1476 | } | ||
1477 | handle->ego_entry = handle->ego_entry->next; | ||
1478 | } | ||
1479 | if (GNUNET_NO == client_exists) | ||
1480 | { | ||
1481 | GNUNET_free_non_null(basic_authorization); | ||
1482 | handle->emsg=GNUNET_strdup("invalid_client"); | ||
1483 | handle->response_code = MHD_HTTP_UNAUTHORIZED; | ||
1484 | return GNUNET_SYSERR; | ||
1485 | } | ||
1486 | GNUNET_STRINGS_string_to_data (client_id, | ||
1487 | strlen(client_id), | ||
1488 | cid, | ||
1489 | sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)); | ||
1490 | |||
1491 | GNUNET_free (basic_authorization); | ||
1492 | return GNUNET_OK; | ||
1493 | } | ||
1494 | |||
1495 | static int | ||
1496 | ego_exists (struct RequestHandle *handle, | ||
1497 | struct GNUNET_CRYPTO_EcdsaPublicKey *test_key) | ||
1498 | { | ||
1499 | struct EgoEntry *ego_entry; | ||
1500 | struct GNUNET_CRYPTO_EcdsaPublicKey pub_key; | ||
1501 | |||
1502 | for (ego_entry = handle->ego_head; NULL != ego_entry; ego_entry = ego_entry->next) | ||
1503 | { | ||
1504 | GNUNET_IDENTITY_ego_get_public_key (ego_entry->ego, &pub_key); | ||
1505 | if (0 == memcmp (&pub_key, | ||
1506 | test_key, | ||
1507 | sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey))) | ||
1508 | { | ||
1509 | break; | ||
1510 | } | ||
1511 | } | ||
1512 | if (NULL == ego_entry) | ||
1513 | return GNUNET_NO; | ||
1514 | return GNUNET_YES; | ||
1515 | } | ||
1516 | |||
1517 | static void | ||
1518 | store_ticket_reference (const struct RequestHandle *handle, | ||
1519 | const char* access_token, | ||
1520 | const struct GNUNET_RECLAIM_Ticket *ticket, | ||
1521 | const struct GNUNET_CRYPTO_EcdsaPublicKey *cid) | ||
1522 | { | ||
1523 | struct GNUNET_HashCode cache_key; | ||
1524 | char *id_ticket_combination; | ||
1525 | char *ticket_string; | ||
1526 | char *client_id; | ||
1527 | |||
1528 | GNUNET_CRYPTO_hash(access_token, strlen(access_token), &cache_key); | ||
1529 | client_id = GNUNET_STRINGS_data_to_string_alloc (cid, | ||
1530 | sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)); | ||
1531 | ticket_string = GNUNET_STRINGS_data_to_string_alloc (ticket, | ||
1532 | sizeof (struct GNUNET_RECLAIM_Ticket)); | ||
1533 | GNUNET_asprintf(&id_ticket_combination, | ||
1534 | "%s;%s", | ||
1535 | client_id, | ||
1536 | ticket_string); | ||
1537 | GNUNET_CONTAINER_multihashmap_put(OIDC_interpret_access_token, | ||
1538 | &cache_key, | ||
1539 | id_ticket_combination, | ||
1540 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE); | ||
1541 | |||
1542 | GNUNET_free (client_id); | ||
1543 | GNUNET_free (ticket_string); | ||
1544 | } | ||
1545 | |||
1546 | /** | ||
1547 | * Responds to token url-encoded POST request | ||
1548 | * | ||
1549 | * @param con_handle the connection handle | ||
1550 | * @param url the url | ||
1551 | * @param cls the RequestHandle | ||
1552 | */ | ||
1553 | static void | ||
1554 | token_endpoint (struct GNUNET_REST_RequestHandle *con_handle, | ||
1555 | const char* url, | ||
1556 | void *cls) | ||
1557 | { | ||
1558 | struct RequestHandle *handle = cls; | ||
1559 | struct GNUNET_TIME_Relative expiration_time; | ||
1560 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *cl; | ||
1561 | struct GNUNET_RECLAIM_Ticket *ticket; | ||
1562 | struct GNUNET_CRYPTO_EcdsaPublicKey cid; | ||
1563 | struct GNUNET_HashCode cache_key; | ||
1564 | struct MHD_Response *resp; | ||
1565 | char *grant_type; | ||
1566 | char *code; | ||
1567 | char *json_response; | ||
1568 | char *id_token; | ||
1569 | char *access_token; | ||
1570 | char *jwt_secret; | ||
1571 | char *nonce; | ||
1572 | int i = 1; | ||
1573 | |||
1574 | /* | ||
1575 | * Check Authorization | ||
1576 | */ | ||
1577 | if (GNUNET_SYSERR == check_authorization (handle, | ||
1578 | &cid)) | ||
1579 | { | ||
1580 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1581 | "OIDC authorization for token endpoint failed\n"); | ||
1582 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
1583 | return; | ||
1584 | } | ||
1585 | |||
1586 | /* | ||
1587 | * Check parameter | ||
1588 | */ | ||
1589 | |||
1590 | //TODO Do not allow multiple equal parameter names | ||
1591 | //REQUIRED grant_type | ||
1592 | GNUNET_CRYPTO_hash (OIDC_GRANT_TYPE_KEY, strlen (OIDC_GRANT_TYPE_KEY), &cache_key); | ||
1593 | if (GNUNET_NO == | ||
1594 | GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->url_param_map, | ||
1595 | &cache_key)) | ||
1596 | { | ||
1597 | handle->emsg = GNUNET_strdup("invalid_request"); | ||
1598 | handle->edesc = GNUNET_strdup("missing parameter grant_type"); | ||
1599 | handle->response_code = MHD_HTTP_BAD_REQUEST; | ||
1600 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
1601 | return; | ||
1602 | } | ||
1603 | grant_type = GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->url_param_map, | ||
1604 | &cache_key); | ||
1605 | |||
1606 | //REQUIRED code | ||
1607 | GNUNET_CRYPTO_hash (OIDC_CODE_KEY, strlen (OIDC_CODE_KEY), &cache_key); | ||
1608 | if (GNUNET_NO == | ||
1609 | GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->url_param_map, | ||
1610 | &cache_key)) | ||
1611 | { | ||
1612 | handle->emsg = GNUNET_strdup("invalid_request"); | ||
1613 | handle->edesc = GNUNET_strdup("missing parameter code"); | ||
1614 | handle->response_code = MHD_HTTP_BAD_REQUEST; | ||
1615 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
1616 | return; | ||
1617 | } | ||
1618 | code = GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->url_param_map, | ||
1619 | &cache_key); | ||
1620 | |||
1621 | //REQUIRED redirect_uri | ||
1622 | GNUNET_CRYPTO_hash (OIDC_REDIRECT_URI_KEY, strlen (OIDC_REDIRECT_URI_KEY), | ||
1623 | &cache_key); | ||
1624 | if (GNUNET_NO == | ||
1625 | GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->url_param_map, | ||
1626 | &cache_key) ) | ||
1627 | { | ||
1628 | handle->emsg = GNUNET_strdup("invalid_request"); | ||
1629 | handle->edesc = GNUNET_strdup("missing parameter redirect_uri"); | ||
1630 | handle->response_code = MHD_HTTP_BAD_REQUEST; | ||
1631 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
1632 | return; | ||
1633 | } | ||
1634 | |||
1635 | //Check parameter grant_type == "authorization_code" | ||
1636 | if (0 != strcmp(OIDC_GRANT_TYPE_VALUE, grant_type)) | ||
1637 | { | ||
1638 | handle->emsg=GNUNET_strdup("unsupported_grant_type"); | ||
1639 | handle->response_code = MHD_HTTP_BAD_REQUEST; | ||
1640 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
1641 | return; | ||
1642 | } | ||
1643 | GNUNET_CRYPTO_hash (code, strlen (code), &cache_key); | ||
1644 | if (GNUNET_SYSERR == | ||
1645 | GNUNET_CONTAINER_multihashmap_put (OIDC_ticket_once, | ||
1646 | &cache_key, | ||
1647 | &i, | ||
1648 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) ) | ||
1649 | { | ||
1650 | handle->emsg = GNUNET_strdup("invalid_request"); | ||
1651 | handle->edesc = GNUNET_strdup("Cannot use the same code more than once"); | ||
1652 | handle->response_code = MHD_HTTP_BAD_REQUEST; | ||
1653 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
1654 | return; | ||
1655 | } | ||
1656 | |||
1657 | //decode code | ||
1658 | if(GNUNET_OK != OIDC_parse_authz_code (&cid, | ||
1659 | code, | ||
1660 | &ticket, | ||
1661 | &nonce)) | ||
1662 | { | ||
1663 | handle->emsg = GNUNET_strdup("invalid_request"); | ||
1664 | handle->edesc = GNUNET_strdup("invalid code"); | ||
1665 | handle->response_code = MHD_HTTP_BAD_REQUEST; | ||
1666 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
1667 | return; | ||
1668 | } | ||
1669 | |||
1670 | //create jwt | ||
1671 | if (GNUNET_OK != | ||
1672 | GNUNET_CONFIGURATION_get_value_time(cfg, | ||
1673 | "reclaim-rest-plugin", | ||
1674 | "expiration_time", | ||
1675 | &expiration_time)) | ||
1676 | { | ||
1677 | handle->emsg = GNUNET_strdup("server_error"); | ||
1678 | handle->edesc = GNUNET_strdup ("gnunet configuration failed"); | ||
1679 | handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; | ||
1680 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
1681 | GNUNET_free(ticket); | ||
1682 | return; | ||
1683 | } | ||
1684 | |||
1685 | |||
1686 | //TODO OPTIONAL acr,amr,azp | ||
1687 | if (GNUNET_NO == ego_exists (handle, | ||
1688 | &ticket->audience)) | ||
1689 | { | ||
1690 | handle->emsg = GNUNET_strdup("invalid_request"); | ||
1691 | handle->edesc = GNUNET_strdup("invalid code..."); | ||
1692 | handle->response_code = MHD_HTTP_BAD_REQUEST; | ||
1693 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
1694 | GNUNET_free(ticket); | ||
1695 | } | ||
1696 | if ( GNUNET_OK | ||
1697 | != GNUNET_CONFIGURATION_get_value_string (cfg, "reclaim-rest-plugin", | ||
1698 | "jwt_secret", &jwt_secret) ) | ||
1699 | { | ||
1700 | handle->emsg = GNUNET_strdup("invalid_request"); | ||
1701 | handle->edesc = GNUNET_strdup("No signing secret configured!"); | ||
1702 | handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; | ||
1703 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
1704 | GNUNET_free(ticket); | ||
1705 | return; | ||
1706 | } | ||
1707 | //TODO We should collect the attributes here. cl always empty | ||
1708 | cl = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList); | ||
1709 | id_token = OIDC_id_token_new (&ticket->audience, | ||
1710 | &ticket->identity, | ||
1711 | cl, | ||
1712 | &expiration_time, | ||
1713 | (NULL != nonce) ? nonce : NULL, | ||
1714 | jwt_secret); | ||
1715 | access_token = OIDC_access_token_new (); | ||
1716 | OIDC_build_token_response (access_token, | ||
1717 | id_token, | ||
1718 | &expiration_time, | ||
1719 | &json_response); | ||
1720 | |||
1721 | store_ticket_reference (handle, | ||
1722 | access_token, | ||
1723 | ticket, | ||
1724 | &cid); | ||
1725 | resp = GNUNET_REST_create_response (json_response); | ||
1726 | MHD_add_response_header (resp, "Cache-Control", "no-store"); | ||
1727 | MHD_add_response_header (resp, "Pragma", "no-cache"); | ||
1728 | MHD_add_response_header (resp, "Content-Type", "application/json"); | ||
1729 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | ||
1730 | GNUNET_RECLAIM_ATTRIBUTE_list_destroy(cl); | ||
1731 | GNUNET_free(access_token); | ||
1732 | GNUNET_free(json_response); | ||
1733 | GNUNET_free(ticket); | ||
1734 | GNUNET_free(id_token); | ||
1735 | GNUNET_SCHEDULER_add_now(&cleanup_handle_delayed, handle); | ||
1736 | } | ||
1737 | |||
1738 | /** | ||
1739 | * Collects claims and stores them in handle | ||
1740 | */ | ||
1741 | static void | ||
1742 | consume_ticket (void *cls, | ||
1743 | const struct GNUNET_CRYPTO_EcdsaPublicKey *identity, | ||
1744 | const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr) | ||
1745 | { | ||
1746 | struct RequestHandle *handle = cls; | ||
1747 | char *tmp_value; | ||
1748 | json_t *value; | ||
1749 | |||
1750 | if (NULL == identity) | ||
1751 | { | ||
1752 | GNUNET_SCHEDULER_add_now (&return_userinfo_response, handle); | ||
1753 | return; | ||
1754 | } | ||
1755 | |||
1756 | tmp_value = GNUNET_RECLAIM_ATTRIBUTE_value_to_string (attr->type, | ||
1757 | attr->data, | ||
1758 | attr->data_size); | ||
1759 | |||
1760 | value = json_string (tmp_value); | ||
1761 | |||
1762 | |||
1763 | json_object_set_new (handle->oidc->response, | ||
1764 | attr->name, | ||
1765 | value); | ||
1766 | GNUNET_free (tmp_value); | ||
1767 | } | ||
1768 | |||
1769 | /** | ||
1770 | * Responds to userinfo GET and url-encoded POST request | ||
1771 | * | ||
1772 | * @param con_handle the connection handle | ||
1773 | * @param url the url | ||
1774 | * @param cls the RequestHandle | ||
1775 | */ | ||
1776 | static void | ||
1777 | userinfo_endpoint (struct GNUNET_REST_RequestHandle *con_handle, | ||
1778 | const char* url, void *cls) | ||
1779 | { | ||
1780 | //TODO expiration time | ||
1781 | struct RequestHandle *handle = cls; | ||
1782 | char delimiter[] = " "; | ||
1783 | char delimiter_db[] = ";"; | ||
1784 | struct GNUNET_HashCode cache_key; | ||
1785 | char *authorization, *authorization_type, *authorization_access_token; | ||
1786 | char *client_ticket, *client, *ticket_str; | ||
1787 | struct GNUNET_RECLAIM_Ticket *ticket; | ||
1788 | |||
1789 | GNUNET_CRYPTO_hash (OIDC_AUTHORIZATION_HEADER_KEY, | ||
1790 | strlen (OIDC_AUTHORIZATION_HEADER_KEY), | ||
1791 | &cache_key); | ||
1792 | if ( GNUNET_NO | ||
1793 | == GNUNET_CONTAINER_multihashmap_contains ( | ||
1794 | handle->rest_handle->header_param_map, &cache_key) ) | ||
1795 | { | ||
1796 | handle->emsg = GNUNET_strdup("invalid_token"); | ||
1797 | handle->edesc = GNUNET_strdup("No Access Token"); | ||
1798 | handle->response_code = MHD_HTTP_UNAUTHORIZED; | ||
1799 | GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle); | ||
1800 | return; | ||
1801 | } | ||
1802 | authorization = GNUNET_CONTAINER_multihashmap_get ( | ||
1803 | handle->rest_handle->header_param_map, &cache_key); | ||
1804 | |||
1805 | //split header in "Bearer" and access_token | ||
1806 | authorization = GNUNET_strdup(authorization); | ||
1807 | authorization_type = strtok (authorization, delimiter); | ||
1808 | if ( 0 != strcmp ("Bearer", authorization_type) ) | ||
1809 | { | ||
1810 | handle->emsg = GNUNET_strdup("invalid_token"); | ||
1811 | handle->edesc = GNUNET_strdup("No Access Token"); | ||
1812 | handle->response_code = MHD_HTTP_UNAUTHORIZED; | ||
1813 | GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle); | ||
1814 | GNUNET_free(authorization); | ||
1815 | return; | ||
1816 | } | ||
1817 | authorization_access_token = strtok (NULL, delimiter); | ||
1818 | if ( NULL == authorization_access_token ) | ||
1819 | { | ||
1820 | handle->emsg = GNUNET_strdup("invalid_token"); | ||
1821 | handle->edesc = GNUNET_strdup("No Access Token"); | ||
1822 | handle->response_code = MHD_HTTP_UNAUTHORIZED; | ||
1823 | GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle); | ||
1824 | GNUNET_free(authorization); | ||
1825 | return; | ||
1826 | } | ||
1827 | |||
1828 | GNUNET_CRYPTO_hash (authorization_access_token, | ||
1829 | strlen (authorization_access_token), | ||
1830 | &cache_key); | ||
1831 | if ( GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (OIDC_interpret_access_token, | ||
1832 | &cache_key) ) | ||
1833 | { | ||
1834 | handle->emsg = GNUNET_strdup("invalid_token"); | ||
1835 | handle->edesc = GNUNET_strdup("The Access Token expired"); | ||
1836 | handle->response_code = MHD_HTTP_UNAUTHORIZED; | ||
1837 | GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle); | ||
1838 | GNUNET_free(authorization); | ||
1839 | return; | ||
1840 | } | ||
1841 | |||
1842 | client_ticket = GNUNET_CONTAINER_multihashmap_get(OIDC_interpret_access_token, | ||
1843 | &cache_key); | ||
1844 | client_ticket = GNUNET_strdup(client_ticket); | ||
1845 | client = strtok(client_ticket,delimiter_db); | ||
1846 | if (NULL == client) | ||
1847 | { | ||
1848 | handle->emsg = GNUNET_strdup("invalid_token"); | ||
1849 | handle->edesc = GNUNET_strdup("The Access Token expired"); | ||
1850 | handle->response_code = MHD_HTTP_UNAUTHORIZED; | ||
1851 | GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle); | ||
1852 | GNUNET_free(authorization); | ||
1853 | GNUNET_free(client_ticket); | ||
1854 | return; | ||
1855 | } | ||
1856 | handle->ego_entry = handle->ego_head; | ||
1857 | for(; NULL != handle->ego_entry; handle->ego_entry = handle->ego_entry->next) | ||
1858 | { | ||
1859 | if (0 == strcmp(handle->ego_entry->keystring,client)) | ||
1860 | { | ||
1861 | break; | ||
1862 | } | ||
1863 | } | ||
1864 | if (NULL == handle->ego_entry) | ||
1865 | { | ||
1866 | handle->emsg = GNUNET_strdup("invalid_token"); | ||
1867 | handle->edesc = GNUNET_strdup("The Access Token expired"); | ||
1868 | handle->response_code = MHD_HTTP_UNAUTHORIZED; | ||
1869 | GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle); | ||
1870 | GNUNET_free(authorization); | ||
1871 | GNUNET_free(client_ticket); | ||
1872 | return; | ||
1873 | } | ||
1874 | ticket_str = strtok(NULL, delimiter_db); | ||
1875 | if (NULL == ticket_str) | ||
1876 | { | ||
1877 | handle->emsg = GNUNET_strdup("invalid_token"); | ||
1878 | handle->edesc = GNUNET_strdup("The Access Token expired"); | ||
1879 | handle->response_code = MHD_HTTP_UNAUTHORIZED; | ||
1880 | GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle); | ||
1881 | GNUNET_free(authorization); | ||
1882 | GNUNET_free(client_ticket); | ||
1883 | return; | ||
1884 | } | ||
1885 | ticket = GNUNET_new(struct GNUNET_RECLAIM_Ticket); | ||
1886 | if ( GNUNET_OK | ||
1887 | != GNUNET_STRINGS_string_to_data (ticket_str, | ||
1888 | strlen (ticket_str), | ||
1889 | ticket, | ||
1890 | sizeof(struct GNUNET_RECLAIM_Ticket))) | ||
1891 | { | ||
1892 | handle->emsg = GNUNET_strdup("invalid_token"); | ||
1893 | handle->edesc = GNUNET_strdup("The Access Token expired"); | ||
1894 | handle->response_code = MHD_HTTP_UNAUTHORIZED; | ||
1895 | GNUNET_SCHEDULER_add_now (&do_userinfo_error, handle); | ||
1896 | GNUNET_free(ticket); | ||
1897 | GNUNET_free(authorization); | ||
1898 | GNUNET_free(client_ticket); | ||
1899 | return; | ||
1900 | } | ||
1901 | |||
1902 | handle->idp = GNUNET_RECLAIM_connect (cfg); | ||
1903 | handle->oidc->response = json_object(); | ||
1904 | json_object_set_new( handle->oidc->response, "sub", json_string( handle->ego_entry->keystring)); | ||
1905 | handle->idp_op = GNUNET_RECLAIM_ticket_consume ( | ||
1906 | handle->idp, | ||
1907 | GNUNET_IDENTITY_ego_get_private_key (handle->ego_entry->ego), | ||
1908 | ticket, | ||
1909 | consume_ticket, | ||
1910 | handle); | ||
1911 | GNUNET_free(ticket); | ||
1912 | GNUNET_free(authorization); | ||
1913 | GNUNET_free(client_ticket); | ||
1914 | |||
1915 | } | ||
1916 | |||
1917 | |||
1918 | /** | ||
1919 | * Handle rest request | ||
1920 | * | ||
1921 | * @param handle the request handle | ||
1922 | */ | ||
1923 | static void | ||
1924 | init_cont (struct RequestHandle *handle) | ||
1925 | { | ||
1926 | struct GNUNET_REST_RequestHandlerError err; | ||
1927 | static const struct GNUNET_REST_RequestHandler handlers[] = { | ||
1928 | {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_AUTHORIZE, &authorize_endpoint}, | ||
1929 | {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_AUTHORIZE, &authorize_endpoint}, //url-encoded | ||
1930 | {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_LOGIN, &login_cont}, | ||
1931 | {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_TOKEN, &token_endpoint }, | ||
1932 | {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_USERINFO, &userinfo_endpoint }, | ||
1933 | {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_USERINFO, &userinfo_endpoint }, | ||
1934 | {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_OIDC, | ||
1935 | &options_cont}, | ||
1936 | GNUNET_REST_HANDLER_END | ||
1937 | }; | ||
1938 | |||
1939 | if (GNUNET_NO == GNUNET_REST_handle_request (handle->rest_handle, | ||
1940 | handlers, | ||
1941 | &err, | ||
1942 | handle)) | ||
1943 | { | ||
1944 | handle->response_code = err.error_code; | ||
1945 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
1946 | } | ||
1947 | } | ||
1948 | |||
1949 | /** | ||
1950 | * If listing is enabled, prints information about the egos. | ||
1951 | * | ||
1952 | * This function is initially called for all egos and then again | ||
1953 | * whenever a ego's identifier changes or if it is deleted. At the | ||
1954 | * end of the initial pass over all egos, the function is once called | ||
1955 | * with 'NULL' for 'ego'. That does NOT mean that the callback won't | ||
1956 | * be invoked in the future or that there was an error. | ||
1957 | * | ||
1958 | * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get', | ||
1959 | * this function is only called ONCE, and 'NULL' being passed in | ||
1960 | * 'ego' does indicate an error (i.e. name is taken or no default | ||
1961 | * value is known). If 'ego' is non-NULL and if '*ctx' | ||
1962 | * is set in those callbacks, the value WILL be passed to a subsequent | ||
1963 | * call to the identity callback of 'GNUNET_IDENTITY_connect' (if | ||
1964 | * that one was not NULL). | ||
1965 | * | ||
1966 | * When an identity is renamed, this function is called with the | ||
1967 | * (known) ego but the NEW identifier. | ||
1968 | * | ||
1969 | * When an identity is deleted, this function is called with the | ||
1970 | * (known) ego and "NULL" for the 'identifier'. In this case, | ||
1971 | * the 'ego' is henceforth invalid (and the 'ctx' should also be | ||
1972 | * cleaned up). | ||
1973 | * | ||
1974 | * @param cls closure | ||
1975 | * @param ego ego handle | ||
1976 | * @param ctx context for application to store data for this ego | ||
1977 | * (during the lifetime of this process, initially NULL) | ||
1978 | * @param identifier identifier assigned by the user for this ego, | ||
1979 | * NULL if the user just deleted the ego and it | ||
1980 | * must thus no longer be used | ||
1981 | */ | ||
1982 | static void | ||
1983 | list_ego (void *cls, | ||
1984 | struct GNUNET_IDENTITY_Ego *ego, | ||
1985 | void **ctx, | ||
1986 | const char *identifier) | ||
1987 | { | ||
1988 | struct RequestHandle *handle = cls; | ||
1989 | struct EgoEntry *ego_entry; | ||
1990 | struct GNUNET_CRYPTO_EcdsaPublicKey pk; | ||
1991 | |||
1992 | if ((NULL == ego) && (ID_REST_STATE_INIT == handle->state)) | ||
1993 | { | ||
1994 | handle->state = ID_REST_STATE_POST_INIT; | ||
1995 | init_cont (handle); | ||
1996 | return; | ||
1997 | } | ||
1998 | if (ID_REST_STATE_INIT == handle->state) { | ||
1999 | ego_entry = GNUNET_new (struct EgoEntry); | ||
2000 | GNUNET_IDENTITY_ego_get_public_key (ego, &pk); | ||
2001 | ego_entry->keystring = | ||
2002 | GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk); | ||
2003 | ego_entry->ego = ego; | ||
2004 | ego_entry->identifier = GNUNET_strdup (identifier); | ||
2005 | GNUNET_CONTAINER_DLL_insert_tail(handle->ego_head,handle->ego_tail, ego_entry); | ||
2006 | return; | ||
2007 | } | ||
2008 | /* Ego renamed or added */ | ||
2009 | if (identifier != NULL) { | ||
2010 | for (ego_entry = handle->ego_head; NULL != ego_entry; ego_entry = ego_entry->next) { | ||
2011 | if (ego_entry->ego == ego) { | ||
2012 | /* Rename */ | ||
2013 | GNUNET_free (ego_entry->identifier); | ||
2014 | ego_entry->identifier = GNUNET_strdup (identifier); | ||
2015 | break; | ||
2016 | } | ||
2017 | } | ||
2018 | if (NULL == ego_entry) { | ||
2019 | /* Add */ | ||
2020 | ego_entry = GNUNET_new (struct EgoEntry); | ||
2021 | GNUNET_IDENTITY_ego_get_public_key (ego, &pk); | ||
2022 | ego_entry->keystring = | ||
2023 | GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk); | ||
2024 | ego_entry->ego = ego; | ||
2025 | ego_entry->identifier = GNUNET_strdup (identifier); | ||
2026 | GNUNET_CONTAINER_DLL_insert_tail(handle->ego_head,handle->ego_tail, ego_entry); | ||
2027 | } | ||
2028 | } else { | ||
2029 | /* Delete */ | ||
2030 | for (ego_entry = handle->ego_head; NULL != ego_entry; ego_entry = ego_entry->next) { | ||
2031 | if (ego_entry->ego == ego) | ||
2032 | break; | ||
2033 | } | ||
2034 | if (NULL != ego_entry) | ||
2035 | GNUNET_CONTAINER_DLL_remove(handle->ego_head,handle->ego_tail, ego_entry); | ||
2036 | } | ||
2037 | |||
2038 | } | ||
2039 | |||
2040 | static void | ||
2041 | rest_identity_process_request(struct GNUNET_REST_RequestHandle *rest_handle, | ||
2042 | GNUNET_REST_ResultProcessor proc, | ||
2043 | void *proc_cls) | ||
2044 | { | ||
2045 | struct RequestHandle *handle = GNUNET_new (struct RequestHandle); | ||
2046 | handle->oidc = GNUNET_new (struct OIDC_Variables); | ||
2047 | if ( NULL == OIDC_identity_login_time ) | ||
2048 | OIDC_identity_login_time = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO); | ||
2049 | if ( NULL == OIDC_identity_grants ) | ||
2050 | OIDC_identity_grants = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO); | ||
2051 | if ( NULL == OIDC_ticket_once ) | ||
2052 | OIDC_ticket_once = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO); | ||
2053 | if ( NULL == OIDC_interpret_access_token ) | ||
2054 | OIDC_interpret_access_token = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO); | ||
2055 | handle->response_code = 0; | ||
2056 | handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL; | ||
2057 | handle->proc_cls = proc_cls; | ||
2058 | handle->proc = proc; | ||
2059 | handle->state = ID_REST_STATE_INIT; | ||
2060 | handle->rest_handle = rest_handle; | ||
2061 | |||
2062 | handle->url = GNUNET_strdup (rest_handle->url); | ||
2063 | if (handle->url[strlen (handle->url)-1] == '/') | ||
2064 | handle->url[strlen (handle->url)-1] = '\0'; | ||
2065 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2066 | "Connecting...\n"); | ||
2067 | handle->identity_handle = GNUNET_IDENTITY_connect (cfg, | ||
2068 | &list_ego, | ||
2069 | handle); | ||
2070 | handle->gns_handle = GNUNET_GNS_connect (cfg); | ||
2071 | handle->namestore_handle = GNUNET_NAMESTORE_connect (cfg); | ||
2072 | handle->timeout_task = | ||
2073 | GNUNET_SCHEDULER_add_delayed (handle->timeout, | ||
2074 | &do_timeout, | ||
2075 | handle); | ||
2076 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2077 | "Connected\n"); | ||
2078 | } | ||
2079 | |||
2080 | /** | ||
2081 | * Entry point for the plugin. | ||
2082 | * | ||
2083 | * @param cls Config info | ||
2084 | * @return NULL on error, otherwise the plugin context | ||
2085 | */ | ||
2086 | void * | ||
2087 | libgnunet_plugin_rest_openid_connect_init (void *cls) | ||
2088 | { | ||
2089 | static struct Plugin plugin; | ||
2090 | struct GNUNET_REST_Plugin *api; | ||
2091 | |||
2092 | cfg = cls; | ||
2093 | if (NULL != plugin.cfg) | ||
2094 | return NULL; /* can only initialize once! */ | ||
2095 | memset (&plugin, 0, sizeof (struct Plugin)); | ||
2096 | plugin.cfg = cfg; | ||
2097 | api = GNUNET_new (struct GNUNET_REST_Plugin); | ||
2098 | api->cls = &plugin; | ||
2099 | api->name = GNUNET_REST_API_NS_OIDC; | ||
2100 | api->process_request = &rest_identity_process_request; | ||
2101 | GNUNET_asprintf (&allow_methods, | ||
2102 | "%s, %s, %s, %s, %s", | ||
2103 | MHD_HTTP_METHOD_GET, | ||
2104 | MHD_HTTP_METHOD_POST, | ||
2105 | MHD_HTTP_METHOD_PUT, | ||
2106 | MHD_HTTP_METHOD_DELETE, | ||
2107 | MHD_HTTP_METHOD_OPTIONS); | ||
2108 | |||
2109 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2110 | _("Identity Provider REST API initialized\n")); | ||
2111 | return api; | ||
2112 | } | ||
2113 | |||
2114 | |||
2115 | /** | ||
2116 | * Exit point from the plugin. | ||
2117 | * | ||
2118 | * @param cls the plugin context (as returned by "init") | ||
2119 | * @return always NULL | ||
2120 | */ | ||
2121 | void * | ||
2122 | libgnunet_plugin_rest_openid_connect_done (void *cls) | ||
2123 | { | ||
2124 | struct GNUNET_REST_Plugin *api = cls; | ||
2125 | struct Plugin *plugin = api->cls; | ||
2126 | plugin->cfg = NULL; | ||
2127 | |||
2128 | struct GNUNET_CONTAINER_MultiHashMapIterator *hashmap_it; | ||
2129 | void *value = NULL; | ||
2130 | hashmap_it = GNUNET_CONTAINER_multihashmap_iterator_create ( | ||
2131 | OIDC_identity_login_time); | ||
2132 | while (GNUNET_YES == | ||
2133 | GNUNET_CONTAINER_multihashmap_iterator_next (hashmap_it, NULL, value)) | ||
2134 | { | ||
2135 | if (NULL != value) | ||
2136 | GNUNET_free(value); | ||
2137 | } | ||
2138 | GNUNET_CONTAINER_multihashmap_destroy(OIDC_identity_login_time); | ||
2139 | hashmap_it = GNUNET_CONTAINER_multihashmap_iterator_create (OIDC_identity_grants); | ||
2140 | while (GNUNET_YES == | ||
2141 | GNUNET_CONTAINER_multihashmap_iterator_next (hashmap_it, NULL, value)) | ||
2142 | { | ||
2143 | if (NULL != value) | ||
2144 | GNUNET_free(value); | ||
2145 | } | ||
2146 | GNUNET_CONTAINER_multihashmap_destroy(OIDC_identity_grants); | ||
2147 | hashmap_it = GNUNET_CONTAINER_multihashmap_iterator_create (OIDC_ticket_once); | ||
2148 | while (GNUNET_YES == | ||
2149 | GNUNET_CONTAINER_multihashmap_iterator_next (hashmap_it, NULL, value)) | ||
2150 | { | ||
2151 | if (NULL != value) | ||
2152 | GNUNET_free(value); | ||
2153 | } | ||
2154 | GNUNET_CONTAINER_multihashmap_destroy(OIDC_ticket_once); | ||
2155 | hashmap_it = GNUNET_CONTAINER_multihashmap_iterator_create (OIDC_interpret_access_token); | ||
2156 | while (GNUNET_YES == | ||
2157 | GNUNET_CONTAINER_multihashmap_iterator_next (hashmap_it, NULL, value)) | ||
2158 | { | ||
2159 | if (NULL != value) | ||
2160 | GNUNET_free(value); | ||
2161 | } | ||
2162 | GNUNET_CONTAINER_multihashmap_destroy(OIDC_interpret_access_token); | ||
2163 | GNUNET_CONTAINER_multihashmap_iterator_destroy(hashmap_it); | ||
2164 | GNUNET_free_non_null (allow_methods); | ||
2165 | GNUNET_free (api); | ||
2166 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2167 | "Identity Provider REST plugin is finished\n"); | ||
2168 | return NULL; | ||
2169 | } | ||
2170 | |||
2171 | /* end of plugin_rest_identity_provider.c */ | ||
diff --git a/src/rest-plugins/plugin_rest_peerinfo.c b/src/rest-plugins/plugin_rest_peerinfo.c new file mode 100644 index 000000000..29b40088d --- /dev/null +++ b/src/rest-plugins/plugin_rest_peerinfo.c | |||
@@ -0,0 +1,820 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2012-2015 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | /** | ||
19 | * @author Martin Schanzenbach | ||
20 | * @author Philippe Buschmann | ||
21 | * @file peerinfo/plugin_rest_peerinfo.c | ||
22 | * @brief GNUnet Peerinfo REST plugin | ||
23 | */ | ||
24 | |||
25 | #include "platform.h" | ||
26 | #include "gnunet_rest_plugin.h" | ||
27 | #include "gnunet_peerinfo_service.h" | ||
28 | #include "gnunet_transport_service.h" | ||
29 | #include "gnunet_rest_lib.h" | ||
30 | #include "gnunet_json_lib.h" | ||
31 | #include "microhttpd.h" | ||
32 | #include <jansson.h> | ||
33 | |||
34 | /** | ||
35 | * Peerinfo Namespace | ||
36 | */ | ||
37 | #define GNUNET_REST_API_NS_PEERINFO "/peerinfo" | ||
38 | |||
39 | /** | ||
40 | * Peerinfo parameter peer | ||
41 | */ | ||
42 | #define GNUNET_REST_PEERINFO_PEER "peer" | ||
43 | |||
44 | /** | ||
45 | * Peerinfo parameter friend | ||
46 | */ | ||
47 | #define GNUNET_REST_PEERINFO_FRIEND "friend" | ||
48 | |||
49 | /** | ||
50 | * Peerinfo parameter array | ||
51 | */ | ||
52 | #define GNUNET_REST_PEERINFO_ARRAY "array" | ||
53 | |||
54 | /** | ||
55 | * Error message Unknown Error | ||
56 | */ | ||
57 | #define GNUNET_REST_PEERINFO_ERROR_UNKNOWN "Unknown Error" | ||
58 | |||
59 | /** | ||
60 | * How long until we time out during address lookup? | ||
61 | */ | ||
62 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) | ||
63 | /** | ||
64 | * The configuration handle | ||
65 | */ | ||
66 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
67 | |||
68 | /** | ||
69 | * HTTP methods allows for this plugin | ||
70 | */ | ||
71 | static char* allow_methods; | ||
72 | |||
73 | /** | ||
74 | * @brief struct returned by the initialization function of the plugin | ||
75 | */ | ||
76 | struct Plugin | ||
77 | { | ||
78 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
79 | }; | ||
80 | |||
81 | |||
82 | /** | ||
83 | * Record we keep for each printable address. | ||
84 | */ | ||
85 | struct AddressRecord | ||
86 | { | ||
87 | /** | ||
88 | * Current address-to-string context (if active, otherwise NULL). | ||
89 | */ | ||
90 | struct GNUNET_TRANSPORT_AddressToStringContext *atsc; | ||
91 | |||
92 | /** | ||
93 | * Address expiration time | ||
94 | */ | ||
95 | struct GNUNET_TIME_Absolute expiration; | ||
96 | |||
97 | /** | ||
98 | * Printable address. | ||
99 | */ | ||
100 | char *result; | ||
101 | |||
102 | /** | ||
103 | * Print context this address record belongs to. | ||
104 | */ | ||
105 | struct PrintContext *pc; | ||
106 | }; | ||
107 | |||
108 | |||
109 | /** | ||
110 | * Structure we use to collect printable address information. | ||
111 | */ | ||
112 | struct PrintContext | ||
113 | { | ||
114 | /** | ||
115 | * Kept in DLL. | ||
116 | */ | ||
117 | struct PrintContext *next; | ||
118 | |||
119 | /** | ||
120 | * Kept in DLL. | ||
121 | */ | ||
122 | struct PrintContext *prev; | ||
123 | |||
124 | /** | ||
125 | * Identity of the peer. | ||
126 | */ | ||
127 | struct GNUNET_PeerIdentity peer; | ||
128 | |||
129 | /** | ||
130 | * List of printable addresses. | ||
131 | */ | ||
132 | struct AddressRecord *address_list; | ||
133 | |||
134 | /** | ||
135 | * Number of completed addresses in @e address_list. | ||
136 | */ | ||
137 | unsigned int num_addresses; | ||
138 | |||
139 | /** | ||
140 | * Number of addresses allocated in @e address_list. | ||
141 | */ | ||
142 | unsigned int address_list_size; | ||
143 | |||
144 | /** | ||
145 | * Current offset in @e address_list (counted down). | ||
146 | */ | ||
147 | unsigned int off; | ||
148 | |||
149 | /** | ||
150 | * Hello was friend only, #GNUNET_YES or #GNUNET_NO | ||
151 | */ | ||
152 | int friend_only; | ||
153 | |||
154 | /** | ||
155 | * RequestHandle | ||
156 | */ | ||
157 | struct RequestHandle *handle; | ||
158 | |||
159 | }; | ||
160 | |||
161 | /** | ||
162 | * Head of list of print contexts. | ||
163 | */ | ||
164 | static struct PrintContext *pc_head; | ||
165 | |||
166 | /** | ||
167 | * Tail of list of print contexts. | ||
168 | */ | ||
169 | static struct PrintContext *pc_tail; | ||
170 | |||
171 | /** | ||
172 | * The request handle | ||
173 | */ | ||
174 | struct RequestHandle | ||
175 | { | ||
176 | /** | ||
177 | * JSON temporary array | ||
178 | */ | ||
179 | json_t *temp_array; | ||
180 | |||
181 | /** | ||
182 | * Expiration time string | ||
183 | */ | ||
184 | char *expiration_str; | ||
185 | |||
186 | /** | ||
187 | * Address string | ||
188 | */ | ||
189 | const char *address; | ||
190 | |||
191 | /** | ||
192 | * Iteration peer public key | ||
193 | */ | ||
194 | char *pubkey; | ||
195 | |||
196 | /** | ||
197 | * JSON response | ||
198 | */ | ||
199 | json_t *response; | ||
200 | |||
201 | /** | ||
202 | * Handle to PEERINFO it | ||
203 | */ | ||
204 | struct GNUNET_PEERINFO_IteratorContext *list_it; | ||
205 | |||
206 | /** | ||
207 | * Handle to PEERINFO | ||
208 | */ | ||
209 | struct GNUNET_PEERINFO_Handle *peerinfo_handle; | ||
210 | |||
211 | /** | ||
212 | * Rest connection | ||
213 | */ | ||
214 | struct GNUNET_REST_RequestHandle *rest_handle; | ||
215 | |||
216 | /** | ||
217 | * Desired timeout for the lookup (default is no timeout). | ||
218 | */ | ||
219 | struct GNUNET_TIME_Relative timeout; | ||
220 | |||
221 | /** | ||
222 | * ID of a task associated with the resolution process. | ||
223 | */ | ||
224 | struct GNUNET_SCHEDULER_Task *timeout_task; | ||
225 | |||
226 | /** | ||
227 | * The plugin result processor | ||
228 | */ | ||
229 | GNUNET_REST_ResultProcessor proc; | ||
230 | |||
231 | /** | ||
232 | * The closure of the result processor | ||
233 | */ | ||
234 | void *proc_cls; | ||
235 | |||
236 | /** | ||
237 | * The url | ||
238 | */ | ||
239 | char *url; | ||
240 | |||
241 | /** | ||
242 | * Error response message | ||
243 | */ | ||
244 | char *emsg; | ||
245 | |||
246 | /** | ||
247 | * Reponse code | ||
248 | */ | ||
249 | int response_code; | ||
250 | |||
251 | }; | ||
252 | |||
253 | |||
254 | /** | ||
255 | * Cleanup lookup handle | ||
256 | * @param handle Handle to clean up | ||
257 | */ | ||
258 | static void | ||
259 | cleanup_handle (void *cls) | ||
260 | { | ||
261 | struct RequestHandle *handle = cls; | ||
262 | |||
263 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
264 | "Cleaning up\n"); | ||
265 | if (NULL != handle->timeout_task) | ||
266 | { | ||
267 | GNUNET_SCHEDULER_cancel (handle->timeout_task); | ||
268 | handle->timeout_task = NULL; | ||
269 | } | ||
270 | if (NULL != handle->url) | ||
271 | GNUNET_free (handle->url); | ||
272 | if (NULL != handle->emsg) | ||
273 | GNUNET_free (handle->emsg); | ||
274 | if (NULL != handle->address) | ||
275 | GNUNET_free ((char*)handle->address); | ||
276 | if (NULL != handle->expiration_str) | ||
277 | GNUNET_free (handle->expiration_str); | ||
278 | if (NULL != handle->pubkey) | ||
279 | GNUNET_free (handle->pubkey); | ||
280 | |||
281 | if (NULL != handle->temp_array) | ||
282 | { | ||
283 | json_decref(handle->temp_array); | ||
284 | handle->temp_array = NULL; | ||
285 | } | ||
286 | if (NULL != handle->response) | ||
287 | { | ||
288 | json_decref(handle->response); | ||
289 | handle->response = NULL; | ||
290 | } | ||
291 | |||
292 | if (NULL != handle->list_it) | ||
293 | { | ||
294 | GNUNET_PEERINFO_iterate_cancel(handle->list_it); | ||
295 | handle->list_it = NULL; | ||
296 | } | ||
297 | if (NULL != handle->peerinfo_handle) | ||
298 | { | ||
299 | GNUNET_PEERINFO_disconnect(handle->peerinfo_handle); | ||
300 | handle->peerinfo_handle = NULL; | ||
301 | } | ||
302 | |||
303 | GNUNET_free (handle); | ||
304 | } | ||
305 | |||
306 | |||
307 | /** | ||
308 | * Task run on errors. Reports an error and cleans up everything. | ||
309 | * | ||
310 | * @param cls the `struct RequestHandle` | ||
311 | */ | ||
312 | static void | ||
313 | do_error (void *cls) | ||
314 | { | ||
315 | struct RequestHandle *handle = cls; | ||
316 | struct MHD_Response *resp; | ||
317 | json_t *json_error = json_object(); | ||
318 | char *response; | ||
319 | |||
320 | if (NULL == handle->emsg) | ||
321 | handle->emsg = GNUNET_strdup(GNUNET_REST_PEERINFO_ERROR_UNKNOWN); | ||
322 | |||
323 | json_object_set_new(json_error,"error", json_string(handle->emsg)); | ||
324 | |||
325 | if (0 == handle->response_code) | ||
326 | handle->response_code = MHD_HTTP_OK; | ||
327 | response = json_dumps (json_error, 0); | ||
328 | resp = GNUNET_REST_create_response (response); | ||
329 | handle->proc (handle->proc_cls, resp, handle->response_code); | ||
330 | json_decref(json_error); | ||
331 | GNUNET_free(response); | ||
332 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
333 | } | ||
334 | |||
335 | |||
336 | /** | ||
337 | * Function that assembles the response. | ||
338 | * | ||
339 | * @param cls the `struct RequestHandle` | ||
340 | */ | ||
341 | static void | ||
342 | peerinfo_list_finished (void *cls) | ||
343 | { | ||
344 | struct RequestHandle *handle = cls; | ||
345 | char *result_str; | ||
346 | struct MHD_Response *resp; | ||
347 | |||
348 | if (NULL == handle->response) | ||
349 | { | ||
350 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
351 | handle->emsg = GNUNET_strdup ("No peers found"); | ||
352 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
353 | return; | ||
354 | } | ||
355 | |||
356 | result_str = json_dumps (handle->response, 0); | ||
357 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); | ||
358 | resp = GNUNET_REST_create_response (result_str); | ||
359 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | ||
360 | GNUNET_free_non_null (result_str); | ||
361 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
362 | } | ||
363 | |||
364 | |||
365 | /** | ||
366 | * Iterator callback to go over all addresses and count them. | ||
367 | * | ||
368 | * @param cls `struct PrintContext *` with `off` to increment | ||
369 | * @param address the address | ||
370 | * @param expiration expiration time | ||
371 | * @return #GNUNET_OK to keep the address and continue | ||
372 | */ | ||
373 | static int | ||
374 | count_address (void *cls, | ||
375 | const struct GNUNET_HELLO_Address *address, | ||
376 | struct GNUNET_TIME_Absolute expiration) | ||
377 | { | ||
378 | struct PrintContext *pc = cls; | ||
379 | |||
380 | if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us) | ||
381 | { | ||
382 | return GNUNET_OK; /* ignore expired address */ | ||
383 | } | ||
384 | |||
385 | pc->off++; | ||
386 | return GNUNET_OK; | ||
387 | } | ||
388 | |||
389 | |||
390 | /** | ||
391 | * Print the collected address information to the console and free @a pc. | ||
392 | * | ||
393 | * @param pc printing context | ||
394 | */ | ||
395 | static void | ||
396 | dump_pc (struct PrintContext *pc) | ||
397 | { | ||
398 | struct RequestHandle *handle; | ||
399 | unsigned int i; | ||
400 | json_t *response_entry; | ||
401 | json_t *temp_array; | ||
402 | json_t *object; | ||
403 | json_t *address; | ||
404 | json_t *expires; | ||
405 | json_t *friend_and_peer_json; | ||
406 | char *friend_and_peer; | ||
407 | |||
408 | temp_array = json_array(); | ||
409 | response_entry = json_object(); | ||
410 | |||
411 | for (i = 0; i < pc->num_addresses; i++) | ||
412 | { | ||
413 | if (NULL != pc->address_list[i].result) | ||
414 | { | ||
415 | object = json_object (); | ||
416 | address = json_string(pc->address_list[i].result); | ||
417 | expires = json_string( | ||
418 | GNUNET_STRINGS_absolute_time_to_string (pc->address_list[i].expiration)); | ||
419 | json_object_set (object, "address", address); | ||
420 | json_object_set (object, "expires", expires); | ||
421 | |||
422 | json_decref(address); | ||
423 | json_decref(expires); | ||
424 | |||
425 | json_array_append(temp_array, object); | ||
426 | json_decref(object); | ||
427 | GNUNET_free (pc->address_list[i].result); | ||
428 | } | ||
429 | } | ||
430 | |||
431 | if (0 < json_array_size(temp_array)) | ||
432 | { | ||
433 | GNUNET_asprintf(&friend_and_peer, | ||
434 | "%s%s", | ||
435 | (GNUNET_YES == pc->friend_only) ? "F2F:" : "", | ||
436 | GNUNET_i2s_full (&pc->peer)); | ||
437 | friend_and_peer_json = json_string(friend_and_peer); | ||
438 | json_object_set(response_entry, | ||
439 | GNUNET_REST_PEERINFO_PEER, | ||
440 | friend_and_peer_json); | ||
441 | json_object_set(response_entry, | ||
442 | GNUNET_REST_PEERINFO_ARRAY, | ||
443 | temp_array); | ||
444 | json_array_append(pc->handle->response, response_entry); | ||
445 | json_decref(friend_and_peer_json); | ||
446 | GNUNET_free(friend_and_peer); | ||
447 | } | ||
448 | |||
449 | json_decref (temp_array); | ||
450 | json_decref(response_entry); | ||
451 | |||
452 | GNUNET_free_non_null (pc->address_list); | ||
453 | GNUNET_CONTAINER_DLL_remove (pc_head, | ||
454 | pc_tail, | ||
455 | pc); | ||
456 | handle = pc->handle; | ||
457 | GNUNET_free (pc); | ||
458 | |||
459 | if ( (NULL == pc_head) && | ||
460 | (NULL == handle->list_it) ) | ||
461 | { | ||
462 | GNUNET_SCHEDULER_add_now (&peerinfo_list_finished, handle); | ||
463 | } | ||
464 | |||
465 | } | ||
466 | |||
467 | |||
468 | /** | ||
469 | * Function to call with a human-readable format of an address | ||
470 | * | ||
471 | * @param cls closure | ||
472 | * @param address NULL on error, otherwise 0-terminated printable UTF-8 string | ||
473 | * @param res result of the address to string conversion: | ||
474 | * if #GNUNET_OK: address was valid (conversion to | ||
475 | * string might still have failed) | ||
476 | * if #GNUNET_SYSERR: address is invalid | ||
477 | */ | ||
478 | static void | ||
479 | process_resolved_address (void *cls, | ||
480 | const char *address, | ||
481 | int res) | ||
482 | { | ||
483 | struct AddressRecord *ar = cls; | ||
484 | struct PrintContext *pc = ar->pc; | ||
485 | |||
486 | if (NULL != address) | ||
487 | { | ||
488 | if (0 != strlen (address)) | ||
489 | { | ||
490 | if (NULL != ar->result) | ||
491 | GNUNET_free (ar->result); | ||
492 | ar->result = GNUNET_strdup (address); | ||
493 | } | ||
494 | return; | ||
495 | } | ||
496 | ar->atsc = NULL; | ||
497 | if (GNUNET_SYSERR == res) | ||
498 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
499 | _("Failure: Cannot convert address to string for peer `%s'\n"), | ||
500 | GNUNET_i2s (&ar->pc->peer)); | ||
501 | pc->num_addresses++; | ||
502 | if (pc->num_addresses == pc->address_list_size) | ||
503 | dump_pc (ar->pc); | ||
504 | } | ||
505 | |||
506 | |||
507 | /** | ||
508 | * Iterator callback to go over all addresses. | ||
509 | * | ||
510 | * @param cls closure | ||
511 | * @param address the address | ||
512 | * @param expiration expiration time | ||
513 | * @return #GNUNET_OK to keep the address and continue | ||
514 | */ | ||
515 | static int | ||
516 | print_address (void *cls, | ||
517 | const struct GNUNET_HELLO_Address *address, | ||
518 | struct GNUNET_TIME_Absolute expiration) | ||
519 | { | ||
520 | struct PrintContext *pc = cls; | ||
521 | struct AddressRecord *ar; | ||
522 | |||
523 | if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us) | ||
524 | { | ||
525 | return GNUNET_OK; /* ignore expired address */ | ||
526 | } | ||
527 | |||
528 | GNUNET_assert (0 < pc->off); | ||
529 | ar = &pc->address_list[--pc->off]; | ||
530 | ar->pc = pc; | ||
531 | ar->expiration = expiration; | ||
532 | GNUNET_asprintf (&ar->result, | ||
533 | "%s:%u:%u", | ||
534 | address->transport_name, | ||
535 | address->address_length, | ||
536 | address->local_info); | ||
537 | ar->atsc = GNUNET_TRANSPORT_address_to_string (cfg, | ||
538 | address, | ||
539 | GNUNET_NO, | ||
540 | TIMEOUT, | ||
541 | &process_resolved_address, | ||
542 | ar); | ||
543 | return GNUNET_OK; | ||
544 | } | ||
545 | |||
546 | |||
547 | /** | ||
548 | * Callback that processes each of the known HELLOs for the | ||
549 | * iteration response construction. | ||
550 | * | ||
551 | * @param cls closure, NULL | ||
552 | * @param peer id of the peer, NULL for last call | ||
553 | * @param hello hello message for the peer (can be NULL) | ||
554 | * @param err_msg message | ||
555 | */ | ||
556 | void | ||
557 | peerinfo_list_iteration(void *cls, | ||
558 | const struct GNUNET_PeerIdentity *peer, | ||
559 | const struct GNUNET_HELLO_Message *hello, | ||
560 | const char *err_msg) | ||
561 | { | ||
562 | struct RequestHandle *handle = cls; | ||
563 | struct PrintContext *pc; | ||
564 | int friend_only; | ||
565 | |||
566 | if (NULL == handle->response) | ||
567 | { | ||
568 | handle->response = json_array(); | ||
569 | } | ||
570 | |||
571 | if (NULL == peer) | ||
572 | { | ||
573 | handle->list_it = NULL; | ||
574 | handle->emsg = GNUNET_strdup ("Error in communication with peerinfo"); | ||
575 | if (NULL != err_msg) | ||
576 | { | ||
577 | GNUNET_free(handle->emsg); | ||
578 | handle->emsg = GNUNET_strdup (err_msg); | ||
579 | handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; | ||
580 | } | ||
581 | if (NULL == pc_head) | ||
582 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
583 | return; | ||
584 | } | ||
585 | if (NULL == hello) | ||
586 | return; | ||
587 | |||
588 | friend_only = GNUNET_NO; | ||
589 | if (NULL != hello) | ||
590 | friend_only = GNUNET_HELLO_is_friend_only (hello); | ||
591 | |||
592 | pc = GNUNET_new(struct PrintContext); | ||
593 | GNUNET_CONTAINER_DLL_insert (pc_head, | ||
594 | pc_tail, | ||
595 | pc); | ||
596 | pc->peer = *peer; | ||
597 | pc->friend_only = friend_only; | ||
598 | pc->handle = handle; | ||
599 | GNUNET_HELLO_iterate_addresses (hello, | ||
600 | GNUNET_NO, | ||
601 | &count_address, | ||
602 | pc); | ||
603 | if (0 == pc->off) | ||
604 | { | ||
605 | dump_pc (pc); | ||
606 | return; | ||
607 | } | ||
608 | pc->address_list_size = pc->off; | ||
609 | pc->address_list = GNUNET_malloc( | ||
610 | sizeof(struct AddressRecord) * pc->off); | ||
611 | GNUNET_HELLO_iterate_addresses (hello, | ||
612 | GNUNET_NO, | ||
613 | &print_address, | ||
614 | pc); | ||
615 | } | ||
616 | |||
617 | /** | ||
618 | * Handle peerinfo GET request | ||
619 | * | ||
620 | * @param con_handle the connection handle | ||
621 | * @param url the url | ||
622 | * @param cls the RequestHandle | ||
623 | */ | ||
624 | void | ||
625 | peerinfo_get (struct GNUNET_REST_RequestHandle *con_handle, | ||
626 | const char* url, | ||
627 | void *cls) | ||
628 | { | ||
629 | struct RequestHandle *handle = cls; | ||
630 | struct GNUNET_HashCode key; | ||
631 | const struct GNUNET_PeerIdentity *specific_peer; | ||
632 | //GNUNET_PEER_Id peer_id; | ||
633 | int include_friend_only; | ||
634 | char* include_friend_only_str; | ||
635 | |||
636 | include_friend_only = GNUNET_NO; | ||
637 | GNUNET_CRYPTO_hash (GNUNET_REST_PEERINFO_FRIEND, | ||
638 | strlen (GNUNET_REST_PEERINFO_FRIEND), | ||
639 | &key); | ||
640 | if ( GNUNET_YES | ||
641 | == GNUNET_CONTAINER_multihashmap_contains (con_handle->url_param_map, | ||
642 | &key)) | ||
643 | { | ||
644 | include_friend_only_str = GNUNET_CONTAINER_multihashmap_get ( | ||
645 | con_handle->url_param_map, &key); | ||
646 | if (0 == strcmp(include_friend_only_str, "yes")) | ||
647 | { | ||
648 | include_friend_only = GNUNET_YES; | ||
649 | } | ||
650 | } | ||
651 | |||
652 | specific_peer = NULL; | ||
653 | GNUNET_CRYPTO_hash (GNUNET_REST_PEERINFO_PEER, | ||
654 | strlen (GNUNET_REST_PEERINFO_PEER), | ||
655 | &key); | ||
656 | if ( GNUNET_YES | ||
657 | == GNUNET_CONTAINER_multihashmap_contains (con_handle->url_param_map, | ||
658 | &key)) | ||
659 | { | ||
660 | //peer_id = *(unsigned int*)GNUNET_CONTAINER_multihashmap_get (con_handle->url_param_map, &key); | ||
661 | //specific_peer = GNUNET_PEER_resolve2(peer_id); | ||
662 | } | ||
663 | |||
664 | handle->list_it = GNUNET_PEERINFO_iterate(handle->peerinfo_handle, | ||
665 | include_friend_only, | ||
666 | specific_peer, | ||
667 | &peerinfo_list_iteration, | ||
668 | handle); | ||
669 | } | ||
670 | |||
671 | |||
672 | |||
673 | /** | ||
674 | * Respond to OPTIONS request | ||
675 | * | ||
676 | * @param con_handle the connection handle | ||
677 | * @param url the url | ||
678 | * @param cls the RequestHandle | ||
679 | */ | ||
680 | static void | ||
681 | options_cont (struct GNUNET_REST_RequestHandle *con_handle, | ||
682 | const char* url, | ||
683 | void *cls) | ||
684 | { | ||
685 | struct MHD_Response *resp; | ||
686 | struct RequestHandle *handle = cls; | ||
687 | |||
688 | //independent of path return all options | ||
689 | resp = GNUNET_REST_create_response (NULL); | ||
690 | MHD_add_response_header (resp, | ||
691 | "Access-Control-Allow-Methods", | ||
692 | allow_methods); | ||
693 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | ||
694 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
695 | return; | ||
696 | } | ||
697 | |||
698 | |||
699 | /** | ||
700 | * Handle rest request | ||
701 | * | ||
702 | * @param handle the request handle | ||
703 | */ | ||
704 | static void | ||
705 | init_cont (struct RequestHandle *handle) | ||
706 | { | ||
707 | struct GNUNET_REST_RequestHandlerError err; | ||
708 | static const struct GNUNET_REST_RequestHandler handlers[] = { | ||
709 | {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_PEERINFO, &peerinfo_get}, | ||
710 | {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_PEERINFO, &options_cont}, | ||
711 | GNUNET_REST_HANDLER_END | ||
712 | }; | ||
713 | |||
714 | if (GNUNET_NO == GNUNET_REST_handle_request (handle->rest_handle, | ||
715 | handlers, | ||
716 | &err, | ||
717 | handle)) | ||
718 | { | ||
719 | handle->response_code = err.error_code; | ||
720 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
721 | } | ||
722 | } | ||
723 | |||
724 | |||
725 | /** | ||
726 | * Function processing the REST call | ||
727 | * | ||
728 | * @param method HTTP method | ||
729 | * @param url URL of the HTTP request | ||
730 | * @param data body of the HTTP request (optional) | ||
731 | * @param data_size length of the body | ||
732 | * @param proc callback function for the result | ||
733 | * @param proc_cls closure for callback function | ||
734 | * @return GNUNET_OK if request accepted | ||
735 | */ | ||
736 | static void | ||
737 | rest_process_request(struct GNUNET_REST_RequestHandle *rest_handle, | ||
738 | GNUNET_REST_ResultProcessor proc, | ||
739 | void *proc_cls) | ||
740 | { | ||
741 | struct RequestHandle *handle = GNUNET_new (struct RequestHandle); | ||
742 | |||
743 | handle->response_code = 0; | ||
744 | handle->timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60); | ||
745 | handle->proc_cls = proc_cls; | ||
746 | handle->proc = proc; | ||
747 | handle->rest_handle = rest_handle; | ||
748 | |||
749 | handle->url = GNUNET_strdup (rest_handle->url); | ||
750 | if (handle->url[strlen (handle->url)-1] == '/') | ||
751 | handle->url[strlen (handle->url)-1] = '\0'; | ||
752 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n"); | ||
753 | handle->peerinfo_handle = GNUNET_PEERINFO_connect(cfg); | ||
754 | init_cont(handle); | ||
755 | handle->timeout_task = | ||
756 | GNUNET_SCHEDULER_add_delayed (handle->timeout, | ||
757 | &do_error, | ||
758 | handle); | ||
759 | |||
760 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected\n"); | ||
761 | } | ||
762 | |||
763 | |||
764 | /** | ||
765 | * Entry point for the plugin. | ||
766 | * | ||
767 | * @param cls Config info | ||
768 | * @return NULL on error, otherwise the plugin context | ||
769 | */ | ||
770 | void * | ||
771 | libgnunet_plugin_rest_peerinfo_init (void *cls) | ||
772 | { | ||
773 | static struct Plugin plugin; | ||
774 | struct GNUNET_REST_Plugin *api; | ||
775 | |||
776 | cfg = cls; | ||
777 | if (NULL != plugin.cfg) | ||
778 | return NULL; /* can only initialize once! */ | ||
779 | memset (&plugin, 0, sizeof (struct Plugin)); | ||
780 | plugin.cfg = cfg; | ||
781 | api = GNUNET_new (struct GNUNET_REST_Plugin); | ||
782 | api->cls = &plugin; | ||
783 | api->name = GNUNET_REST_API_NS_PEERINFO; | ||
784 | api->process_request = &rest_process_request; | ||
785 | GNUNET_asprintf (&allow_methods, | ||
786 | "%s, %s, %s, %s, %s", | ||
787 | MHD_HTTP_METHOD_GET, | ||
788 | MHD_HTTP_METHOD_POST, | ||
789 | MHD_HTTP_METHOD_PUT, | ||
790 | MHD_HTTP_METHOD_DELETE, | ||
791 | MHD_HTTP_METHOD_OPTIONS); | ||
792 | |||
793 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
794 | _("Peerinfo REST API initialized\n")); | ||
795 | return api; | ||
796 | } | ||
797 | |||
798 | |||
799 | /** | ||
800 | * Exit point from the plugin. | ||
801 | * | ||
802 | * @param cls the plugin context (as returned by "init") | ||
803 | * @return always NULL | ||
804 | */ | ||
805 | void * | ||
806 | libgnunet_plugin_rest_peerinfo_done (void *cls) | ||
807 | { | ||
808 | struct GNUNET_REST_Plugin *api = cls; | ||
809 | struct Plugin *plugin = api->cls; | ||
810 | plugin->cfg = NULL; | ||
811 | |||
812 | GNUNET_free_non_null (allow_methods); | ||
813 | GNUNET_free (api); | ||
814 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
815 | "Peerinfo REST plugin is finished\n"); | ||
816 | return NULL; | ||
817 | } | ||
818 | |||
819 | /* end of plugin_rest_peerinfo.c */ | ||
820 | |||