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/namestore | |
parent | 6656d6c3e7111075572c042ae714c9710f30273b (diff) | |
download | gnunet-6371b64774428e83ff83ada88bda354356718aca.tar.gz gnunet-6371b64774428e83ff83ada88bda354356718aca.zip |
fix build; move rest plugins to separate folder
Diffstat (limited to 'src/namestore')
-rw-r--r-- | src/namestore/Makefile.am | 23 | ||||
-rw-r--r-- | src/namestore/plugin_rest_namestore.c | 1004 |
2 files changed, 1 insertions, 1026 deletions
diff --git a/src/namestore/Makefile.am b/src/namestore/Makefile.am index 7f44c2a71..a349921d7 100644 --- a/src/namestore/Makefile.am +++ b/src/namestore/Makefile.am | |||
@@ -97,12 +97,6 @@ check_PROGRAMS = \ | |||
97 | $(FLAT_TESTS) | 97 | $(FLAT_TESTS) |
98 | endif | 98 | endif |
99 | 99 | ||
100 | if HAVE_MHD | ||
101 | if HAVE_JSON | ||
102 | REST_PLUGIN=libgnunet_plugin_rest_namestore.la | ||
103 | endif | ||
104 | endif | ||
105 | |||
106 | if ENABLE_TEST_RUN | 100 | if ENABLE_TEST_RUN |
107 | AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH;unset XDG_DATA_HOME;unset XDG_CONFIG_HOME; | 101 | AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH;unset XDG_DATA_HOME;unset XDG_CONFIG_HOME; |
108 | TESTS = \ | 102 | TESTS = \ |
@@ -186,8 +180,7 @@ gnunet_service_namestore_LDADD = \ | |||
186 | plugin_LTLIBRARIES = \ | 180 | plugin_LTLIBRARIES = \ |
187 | $(SQLITE_PLUGIN) \ | 181 | $(SQLITE_PLUGIN) \ |
188 | $(POSTGRES_PLUGIN) \ | 182 | $(POSTGRES_PLUGIN) \ |
189 | $(FLAT_PLUGIN) \ | 183 | $(FLAT_PLUGIN) |
190 | $(REST_PLUGIN) | ||
191 | 184 | ||
192 | 185 | ||
193 | 186 | ||
@@ -224,20 +217,6 @@ libgnunet_plugin_namestore_postgres_la_LIBADD = \ | |||
224 | libgnunet_plugin_namestore_postgres_la_LDFLAGS = \ | 217 | libgnunet_plugin_namestore_postgres_la_LDFLAGS = \ |
225 | $(GN_PLUGIN_LDFLAGS) $(POSTGRESQL_LDFLAGS) | 218 | $(GN_PLUGIN_LDFLAGS) $(POSTGRESQL_LDFLAGS) |
226 | 219 | ||
227 | libgnunet_plugin_rest_namestore_la_SOURCES = \ | ||
228 | plugin_rest_namestore.c | ||
229 | libgnunet_plugin_rest_namestore_la_LIBADD = \ | ||
230 | libgnunetnamestore.la \ | ||
231 | $(top_builddir)/src/rest/libgnunetrest.la \ | ||
232 | $(top_builddir)/src/identity/libgnunetidentity.la \ | ||
233 | $(top_builddir)/src/json/libgnunetjson.la \ | ||
234 | $(top_builddir)/src/gnsrecord/libgnunetgnsrecord.la \ | ||
235 | $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \ | ||
236 | $(LTLIBINTL) -ljansson -lmicrohttpd | ||
237 | libgnunet_plugin_rest_namestore_la_LDFLAGS = \ | ||
238 | $(GN_PLUGIN_LDFLAGS) | ||
239 | |||
240 | |||
241 | test_namestore_api_store_flat_SOURCES = \ | 220 | test_namestore_api_store_flat_SOURCES = \ |
242 | test_namestore_api_store.c | 221 | test_namestore_api_store.c |
243 | test_namestore_api_store_flat_LDADD = \ | 222 | test_namestore_api_store_flat_LDADD = \ |
diff --git a/src/namestore/plugin_rest_namestore.c b/src/namestore/plugin_rest_namestore.c deleted file mode 100644 index 1d72d13ff..000000000 --- a/src/namestore/plugin_rest_namestore.c +++ /dev/null | |||
@@ -1,1004 +0,0 @@ | |||
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 | |||