summaryrefslogtreecommitdiff
path: root/src/namestore/gnunet-namestore-fcfsd.c
blob: e1fa0a0a6a899b0c0f6550cbe8aa6e8e7ea4a07a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
/*
     This file is part of GNUnet.
     Copyright (C) 2012-2021 GNUnet e.V.

     GNUnet is free software: you can redistribute it and/or modify it
     under the terms of the GNU Affero General Public License as published
     by the Free Software Foundation, either version 3 of the License,
     or (at your option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     Affero General Public License for more details.

     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

     SPDX-License-Identifier: AGPL3.0-or-later
 */

/**
 * @file gnunet-namestore-fcfsd.c
 * @brief HTTP daemon that offers first-come-first-serve GNS domain registration
 * @author Christian Grothoff
 */

#include "platform.h"
#include <microhttpd.h>
#include "gnunet_util_lib.h"
#include "gnunet_identity_service.h"
#include "gnunet_gnsrecord_lib.h"
#include "gnunet_namestore_service.h"
#include "gnunet_mhd_compat.h"
#include "gnunet_json_lib.h"

/**
 * Structure representing a static page.
 * "Static" means that the server does not process the page before sending it
 * to the client.  Clients can still process the received data, for example
 * because there are scripting elements within.
 */
struct StaticPage
{
  /**
   * Handle to file on disk.
   */
  struct GNUNET_DISK_FileHandle *handle;

  /**
   * Size in bytes of the file.
   */
  uint64_t size;

  /**
   * Cached response object to send to clients.
   */
  struct MHD_Response *response;
};

/**
 * Structure containing some request-specific data.
 */
struct RequestData
{
  /**
   * The connection this request was sent in.
   */
  struct MHD_Connection *c;

  /**
   * Body of the response object.
   */
  char *body;

  /**
   * Length in bytes of the body.
   */
  size_t body_length;

  /**
   * Response code.
   */
  int code;

  /**
   * Task started to search for an entry in the namestore.
   */
  struct GNUNET_NAMESTORE_QueueEntry *searching;

  /**
   * Task started to iterate over the namestore.
   */
  struct GNUNET_NAMESTORE_ZoneIterator *iterating;

  /**
   * Pointer used while processing POST data.
   */
  void *ptr;

  /**
   * Name requested to be registered.
   */
  char *register_name;

  /**
   * Key (encoded as a string) to be associated with the requested name.
   */
  char *register_key;

  /**
   * Key to be associated with the requested name.
   */
  struct GNUNET_IDENTITY_PublicKey key;
};

/**
 * Name of the zone being managed.
 */
static char *zone = NULL;

/**
 * The port the daemon is listening to for HTTP requests.
 */
static unsigned long long port = 18080;

/**
 * Connection with the namestore service.
 */
static struct GNUNET_NAMESTORE_Handle *namestore = NULL;

/**
 * Connection with the identity service.
 */
static struct GNUNET_IDENTITY_Handle *identity = NULL;

/**
 * Private key of the zone.
 */
static const struct GNUNET_IDENTITY_PrivateKey *zone_key = NULL;

/**
 * The HTTP daemon.
 */
static struct MHD_Daemon *httpd = NULL;

/**
 * Task executing the HTTP daemon.
 */
static struct GNUNET_SCHEDULER_Task *httpd_task = NULL;

/**
 * The main page, a.k.a. "index.html"
 */
static struct StaticPage *main_page = NULL;

/**
 * Page indicating the requested resource could not be found.
 */
static struct StaticPage *notfound_page = NULL;

/**
 * Page indicating the requested resource could not be accessed, and other
 * errors.
 */
static struct StaticPage *forbidden_page = NULL;

/**
 * Task ran at shutdown to clean up everything.
 *
 * @param cls unused
 */
static void
do_shutdown (void *cls)
{
  /* We cheat a bit here: the file descriptor is implicitly closed by MHD, so
   calling `GNUNET_DISK_file_close' would generate a spurious warning message
   in the log. Since that function does nothing but close the descriptor and
   free the allocated memory, After destroying the response all that's left to
   do is call `GNUNET_free'. */
  if (NULL != main_page)
  {
    MHD_destroy_response (main_page->response);
    GNUNET_free (main_page->handle);
    GNUNET_free (main_page);
  }
  if (NULL != notfound_page)
  {
    MHD_destroy_response (main_page->response);
    GNUNET_free (main_page->handle);
    GNUNET_free (main_page);
  }
  if (NULL != forbidden_page)
  {
    MHD_destroy_response (main_page->response);
    GNUNET_free (main_page->handle);
    GNUNET_free (main_page);
  }

  if (NULL != namestore)
  {
    GNUNET_NAMESTORE_disconnect (namestore);
  }

  if (NULL != identity)
  {
    GNUNET_IDENTITY_disconnect (identity);
  }
}

/**
 * Called when the HTTP server has some pending operations.
 *
 * @param cls unused
 */
static void
do_httpd (void *cls);

/**
 * Schedule a task to run MHD.
 */
static void
run_httpd (void)
{
  fd_set rs;
  fd_set ws;
  fd_set es;

  struct GNUNET_NETWORK_FDSet *grs = GNUNET_NETWORK_fdset_create ();
  struct GNUNET_NETWORK_FDSet *gws = GNUNET_NETWORK_fdset_create ();
  struct GNUNET_NETWORK_FDSet *ges = GNUNET_NETWORK_fdset_create ();

  FD_ZERO (&rs);
  FD_ZERO (&ws);
  FD_ZERO (&es);

  int max = -1;
  GNUNET_assert (MHD_YES == MHD_get_fdset (httpd, &rs, &ws, &es, &max));

  unsigned MHD_LONG_LONG timeout = 0;
  struct GNUNET_TIME_Relative gtime = GNUNET_TIME_UNIT_FOREVER_REL;
  if (MHD_YES == MHD_get_timeout (httpd, &timeout))
  {
    gtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
                                           timeout);
  }

  GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
  GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
  GNUNET_NETWORK_fdset_copy_native (ges, &es, max + 1);

  httpd_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
                                            gtime,
                                            grs,
                                            gws,
                                            &do_httpd,
                                            NULL);
  GNUNET_NETWORK_fdset_destroy (grs);
  GNUNET_NETWORK_fdset_destroy (gws);
  GNUNET_NETWORK_fdset_destroy (ges);
}

/**
 * Called when the HTTP server has some pending operations.
 *
 * @param cls unused
 */
static void
do_httpd (void *cls)
{
  httpd_task = NULL;
  MHD_run (httpd);
  run_httpd ();
}

static void
run_httpd_now (void)
{
  if (NULL != httpd_task)
  {
    GNUNET_SCHEDULER_cancel (httpd_task);
    httpd_task = NULL;
  }
  httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd, NULL);
}

/**
 * Generate a JSON object.
 *
 * @param key the key for the first element
 * @param value the value for the first element
 * @param ... key-value pairs of the object, terminated by NULL
 * @return a JSON string (allocated)
 */
static char *
make_json (const char *key, const char *value, ...)
{
  va_list args;
  va_start(args, value);

  json_t *obj = NULL;

  obj = json_object ();
  if (NULL == key || NULL == value)
  {
    va_end (args);
    return json_dumps (obj, JSON_COMPACT);
  }

  json_object_set (obj, key, json_string (value));

  char *k = va_arg (args, char *);
  if (NULL == k)
  {
    va_end (args);
    return json_dumps (obj, JSON_COMPACT);
  }
  char *v = va_arg (args, char *);
  if (NULL == v)
  {
    va_end (args);
    return json_dumps (obj, JSON_COMPACT);
  }

  while (NULL != k && NULL != v)
  {
    json_object_set (obj, k, json_string (v));
    k = va_arg (args, char *);
    if (NULL != k)
    {
      v = va_arg (args, char *);
    }
  }

  va_end (args);

  char *json = json_dumps (obj, JSON_COMPACT);
  json_decref (obj);

  return json;
}

/**
 * The namestore search task failed.
 *
 * @param cls the request data
 */
static void
search_error_cb (void *cls)
{
  struct RequestData *rd = cls;
  MHD_resume_connection (rd->c);
  rd->searching = NULL;
  rd->body = make_json ("error", "true",
                        "message", _ ("can not search the namestore"),
                        NULL);
  rd->body_length = strlen (rd->body);
  rd->code = MHD_HTTP_INTERNAL_SERVER_ERROR;
  run_httpd_now ();
}

/**
 * The lookup terminated with some results.
 *
 * @param cls closure
 * @param zone the private key of the zone
 * @param label the result label
 * @param count number of records found
 * @param d records found
 */
static void
search_done_cb (void *cls,
                const struct GNUNET_IDENTITY_PrivateKey *zone,
                const char *label,
                unsigned int count,
                const struct GNUNET_GNSRECORD_Data *d)
{
  (void) zone;
  (void) d;

  struct RequestData *rd = cls;
  MHD_resume_connection (rd->c);

  rd->searching = NULL;
  rd->body = make_json ("error", "false",
                        "free", (0 == count) ? "true" : "false",
                        NULL);
  rd->body_length = strlen (rd->body);
  rd->code = MHD_HTTP_OK;

  run_httpd_now ();
}

/**
 * An error occurred while registering a name.
 *
 * @param cls the connection
 */
static void
register_error_cb (void *cls)
{
  struct RequestData *rd = cls;

  MHD_resume_connection (rd->c);
  rd->searching = NULL;
  rd->body = make_json ("error", "true",
                        "message", _ ("unable to scan namestore"),
                        NULL);
  rd->body_length = strlen (rd->body);
  rd->code = MHD_HTTP_INTERNAL_SERVER_ERROR;
  run_httpd_now ();
}

/**
 * A name/key pair has been successfully registered, or maybe not.
 *
 * @param cls the connection
 * @param status result of the operation
 * @param emsg error message if any
 */
static void
register_done_cb (void *cls,
                  int32_t status,
                  const char *emsg)
{
  struct RequestData *rd = cls;

  MHD_resume_connection (rd->c);
  rd->searching = NULL;

  if (GNUNET_SYSERR == status || GNUNET_NO == status)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                _ ("Failed to create record for `%s': %s\n"),
                rd->register_name,
                emsg);
    rd->body = make_json ("error", "true",
                          "message", emsg,
                          NULL);
    rd->body_length = strlen (rd->body);
    rd->code = MHD_HTTP_INTERNAL_SERVER_ERROR;
  }
  else
  {
    rd->body = make_json ("error", "false",
                          "message", _ ("no errors"),
                          NULL);
    rd->body_length = strlen (rd->body);
    rd->code = MHD_HTTP_OK;
  }

  run_httpd_now ();
}

/**
 * Attempt to register the requested name.
 *
 * @param cls the connection
 * @param key the zone key
 * @param label name of the record
 * @param count number of records found
 * @param d records
 */
static void
register_do_cb (void *cls,
                const struct GNUNET_IDENTITY_PrivateKey *key,
                const char *label,
                unsigned int count,
                const struct GNUNET_GNSRECORD_Data *d)
{
  (void) key;
  (void) d;

  struct RequestData *rd = cls;

  rd->searching = NULL;

  if (0 != count)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                _ ("The requested key `%s' exists as `%s'\n"),
                rd->register_key,
                label);

    MHD_resume_connection (rd->c);
    rd->searching = NULL;
    rd->body = make_json ("error", "true",
                          "message", _ ("key exists"),
                          NULL);
    rd->body_length = strlen (rd->body);
    rd->code = MHD_HTTP_FORBIDDEN;
    run_httpd_now ();
    return;
  }

  struct GNUNET_GNSRECORD_Data gd;
  char *gdraw = NULL;

  if (GNUNET_OK != GNUNET_GNSRECORD_data_from_identity (&(rd->key),
                                                        &gdraw,
                                                        &(gd.data_size),
                                                        &(gd.record_type)))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                _ ("Error creating record data\n"));
    MHD_resume_connection (rd->c);
    rd->searching = NULL;
    rd->body = make_json ("error", "true",
                          "message", _ ("unable to store record"),
                          NULL);
    rd->body_length = strlen (rd->body);
    rd->code = MHD_HTTP_INTERNAL_SERVER_ERROR;
    run_httpd_now ();
    return;
  }

  gd.data = gdraw;
  gd.expiration_time = UINT64_MAX;
  gd.flags = GNUNET_GNSRECORD_RF_NONE;

  rd->searching = GNUNET_NAMESTORE_records_store (namestore,
                                                  zone_key,
                                                  rd->register_name,
                                                  1,
                                                  &gd,
                                                  &register_done_cb,
                                                  rd);

  GNUNET_free (gdraw);
}

/**
 * An error occurred while iterating the namestore.
 *
 * @param cls the connection
 */
static void
iterate_error_cb (void *cls)
{
  struct RequestData *rd = cls;

  MHD_resume_connection (rd->c);
  rd->iterating = NULL;
  rd->body = make_json ("error", "true",
                        "message", _ ("unable to scan namestore"),
                        NULL);
  rd->body_length = strlen (rd->body);
  rd->code = MHD_HTTP_INTERNAL_SERVER_ERROR;
  run_httpd_now ();
}

/**
 * A block was received from the namestore.
 *
 * @param cls the connection
 * @param key the zone key
 * @param label the records' label
 * @param count number of records found
 * @param d the found records
 */
static void
iterate_do_cb (void *cls,
               const struct GNUNET_IDENTITY_PrivateKey *key,
               const char *label,
               unsigned int count,
               const struct GNUNET_GNSRECORD_Data *d)
{
  (void) key;
  (void) label;
  (void) d;

  struct RequestData *rd = cls;

  if (0 == strcmp (label, rd->register_name))
  {
    GNUNET_break (0 != count);
    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                _ ("Requested name `%s' exists with `%u' records\n"),
                rd->register_name,
                count);

    MHD_resume_connection (rd->c);
    rd->body = make_json ("error", "true",
                          "message", _ ("name exists\n"),
                          NULL);
    rd->body_length = strlen (rd->body);
    rd->code = MHD_HTTP_FORBIDDEN;
    GNUNET_NAMESTORE_zone_iteration_stop (rd->iterating);
    run_httpd_now ();
    return;
  }

  GNUNET_NAMESTORE_zone_iterator_next (rd->iterating, 1);
}

/**
 * All entries in the namestore have been iterated over.
 *
 * @param cls the connection
 */
static void
iterate_done_cb (void *cls)
{
  struct RequestData *rd = cls;

  rd->iterating = NULL;

  /* See if the key was not registered already */
  rd->searching = GNUNET_NAMESTORE_zone_to_name (namestore,
                                                 zone_key,
                                                 &(rd->key),
                                                 &register_error_cb,
                                                 rd,
                                                 &register_do_cb,
                                                 rd);
}

/**
 * Generate a response containing JSON and send it to the client.
 *
 * @param c the connection
 * @param body the response body
 * @param length the body length in bytes
 * @param code the response code
 * @return MHD_NO on error
 */
static MHD_RESULT
serve_json (struct MHD_Connection *c,
            char *body,
            size_t length,
            int code)
{
  struct MHD_Response *response =
    MHD_create_response_from_buffer (length,
                                     body,
                                     MHD_RESPMEM_PERSISTENT);
  MHD_RESULT r = MHD_queue_response (c, code, response);
  MHD_destroy_response (response);
  return r;
}

/**
 * Send a response back to a connected client.
 *
 * @param cls unused
 * @param connection the connection with the client
 * @param url the requested address
 * @param method the HTTP method used
 * @param version the protocol version (including the "HTTP/" part)
 * @param upload_data data sent with a POST request
 * @param upload_data_size length in bytes of the POST data
 * @param ptr used to pass data between request handling phases
 * @return MHD_NO on error
 */
static MHD_RESULT
create_response (void *cls,
                 struct MHD_Connection *connection,
                 const char *url,
                 const char *method,
                 const char *version,
                 const char *upload_data,
                 size_t *upload_data_size,
                 void **ptr)
{
  (void) cls;
  (void) version;

  struct RequestData *rd = *ptr;

  if (0 == strcmp (method, MHD_HTTP_METHOD_GET))
  {
    /* Handle a previously suspended request */
    if (NULL != rd)
    {
      return serve_json (rd->c, rd->body, rd->body_length, rd->code);
    }

    if (0 == strcmp ("/", url))
    {
      return MHD_queue_response (connection,
                                 MHD_HTTP_OK,
                                 main_page->response);
    }

    if (0 == strcmp ("/search", url))
    {
      const char *name = MHD_lookup_connection_value (connection,
                                                      MHD_GET_ARGUMENT_KIND,
                                                      "name");
      if (NULL == name)
      {
        return MHD_queue_response (connection,
                                   MHD_HTTP_BAD_REQUEST,
                                   forbidden_page->response);
      }

      MHD_suspend_connection (connection);
      rd = GNUNET_new (struct RequestData);
      rd->c = connection;
      rd->searching = GNUNET_NAMESTORE_records_lookup (namestore,
                                                       zone_key,
                                                       name,
                                                       &search_error_cb,
                                                       rd,
                                                       &search_done_cb,
                                                       rd);
      *ptr = rd;
      return MHD_YES;
    }

    return MHD_queue_response (connection,
                               MHD_HTTP_NOT_FOUND,
                               notfound_page->response);
  }

  if (0 == strcmp (method, MHD_HTTP_METHOD_HEAD))
  {
    /* We take a shortcut here by always serving the main page: starting a
     namestore lookup, allocating the necessary resources, waiting for the
     lookup to complete and then discard everything just because it was a HEAD
     and thus only the headers are significative, is an unnecessary waste of
     resources. The handling of this method could be smarter, for example by
     sending a proper content type header based on the endpoint, but this is
     not a service in which HEAD requests are significant, so there's no need
     to spend too much time here. */
    return MHD_queue_response (connection,
                               MHD_HTTP_OK,
                               main_page->response);
  }

  if (0 == strcmp (method, MHD_HTTP_METHOD_POST))
  {
    if (0 == strcmp ("/register", url))
    {
      /* Handle a previously suspended request */
      if (NULL != rd && NULL != rd->body)
      {
        return serve_json (rd->c, rd->body, rd->body_length, rd->code);
      }

      if (NULL == rd)
      {
        rd = GNUNET_new (struct RequestData);
        rd->c = connection;
        rd->body = NULL;
        rd->ptr = NULL;
        *ptr = rd;
      }

      json_t *json = NULL;
      enum GNUNET_JSON_PostResult result =
        GNUNET_JSON_post_parser (32 * 1024,
                                 connection,
                                 &(rd->ptr),
                                 upload_data,
                                 upload_data_size,
                                 &json);

      switch (result)
      {
      case GNUNET_JSON_PR_CONTINUE:
        /* Keep processing POST data */
        return MHD_YES;
      case GNUNET_JSON_PR_OUT_OF_MEMORY:
      case GNUNET_JSON_PR_REQUEST_TOO_LARGE:
        rd->body = make_json ("error", "true",
                              "message", _ ("unable to process submitted data"),
                              NULL);
        rd->body_length = strlen (rd->body);
        rd->code = MHD_HTTP_PAYLOAD_TOO_LARGE;
        return MHD_YES;
      case GNUNET_JSON_PR_JSON_INVALID:
        rd->body = make_json ("error", "true",
                              "message", _ ("the submitted data is invalid"),
                              NULL);
        rd->body_length = strlen (rd->body);
        rd->code = MHD_HTTP_BAD_REQUEST;
        return MHD_YES;
      default:
        break;
      }

      /* POST data has been read in its entirety */

      const char *name = json_string_value(json_object_get(json, "name"));
      const char *key = json_string_value(json_object_get(json, "key"));
      if (NULL == name || NULL == key || 0 == strlen (name) || 0 == strlen (key))
      {
        json_decref (json);
        rd->body = make_json ("error", "true",
                              "message", _ ("invalid parameters"),
                              NULL);
        rd->body_length = strlen (rd->body);
        rd->code = MHD_HTTP_BAD_REQUEST;
        return MHD_YES;
      }

      rd->register_name = strdup (name);
      rd->register_key = strdup (key);

      json_decref (json);
      GNUNET_JSON_post_parser_cleanup (rd->ptr);

      if (NULL != strchr (rd->register_name, '.') ||
          NULL != strchr (rd->register_name, '+'))
      {
        rd->body = make_json ("error", "true",
                              "message", _ ("invalid name"),
                              NULL);
        rd->body_length = strlen (rd->body);
        rd->code = MHD_HTTP_BAD_REQUEST;
        return MHD_YES;
      }

      if (GNUNET_OK != GNUNET_IDENTITY_public_key_from_string (rd->register_key,
                                                               &(rd->key)))
      {
        GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                    _ ("Unable to parse key %s\n"),
                    rd->register_key);

        rd->body = make_json ("error", "true",
                              "message", _ ("unable to parse key"),
                              NULL);
        rd->body_length = strlen (rd->body);
        rd->code = MHD_HTTP_INTERNAL_SERVER_ERROR;
        return MHD_YES;
      }

      MHD_suspend_connection (connection);
      /* See if the requested name is free */
      rd->iterating =
        GNUNET_NAMESTORE_zone_iteration_start (namestore,
                                               zone_key,
                                               &iterate_error_cb,
                                               rd,
                                               &iterate_do_cb,
                                               rd,
                                               &iterate_done_cb,
                                               rd);
      return MHD_YES;
    }

    return MHD_queue_response (connection,
                               MHD_HTTP_FORBIDDEN,
                               forbidden_page->response);
  }

  return MHD_queue_response (connection,
                             MHD_HTTP_NOT_IMPLEMENTED,
                             forbidden_page->response);
}

/**
 * Called when a request is completed.
 *
 * @param cls unused
 * @param connection the connection
 * @param ptr connection-specific data
 * @param status status code
 */
static void
completed_cb (void *cls,
              struct MHD_Connection *connection,
              void **ptr,
              enum MHD_RequestTerminationCode status)
{
  (void) cls;
  (void) connection;
  (void) status;

  struct RequestData *rd = *ptr;

  if (NULL == rd)
  {
    return;
  }

  if (NULL == rd->body)
  {
    GNUNET_free (rd->body);
  }

  if (NULL != rd->searching)
  {
    GNUNET_NAMESTORE_cancel (rd->searching);
  }

  if (NULL != rd->register_name)
  {
    GNUNET_free (rd->register_name);
  }

  if (NULL != rd->register_key)
  {
    GNUNET_free (rd->register_key);
  }

  if (NULL != rd->iterating)
  {
    GNUNET_NAMESTORE_zone_iteration_stop (rd->iterating);
  }

  GNUNET_free (rd);
}

/**
 * Called for each ego provided by the identity service.
 *
 * @param cls closure
 * @param ego the ego
 * @param ctx application-provided data for the ego
 * @param name the ego name
 */
static void
identity_cb (void *cls,
             struct GNUNET_IDENTITY_Ego *ego,
             void **ctx,
             const char *name)
{
  (void) cls;
  (void) ctx;

  if (NULL == name || 0 != strcmp (name, zone))
  {
    return;
  }

  if (NULL == ego)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _ ("No ego configured for `fcfsd` subsystem\n"));
    GNUNET_SCHEDULER_shutdown ();
    return;
  }

  zone_key = GNUNET_IDENTITY_ego_get_private_key (ego);

  int flags = MHD_USE_DUAL_STACK | MHD_USE_DEBUG | MHD_ALLOW_SUSPEND_RESUME;
  do
  {
    httpd = MHD_start_daemon (flags,
                              (uint16_t) port,
                              NULL, NULL,
                              &create_response, NULL,
                              MHD_OPTION_CONNECTION_LIMIT, 128,
                              MHD_OPTION_PER_IP_CONNECTION_LIMIT, 1,
                              MHD_OPTION_CONNECTION_TIMEOUT, 4 * 1024,
                              MHD_OPTION_NOTIFY_COMPLETED, &completed_cb, NULL,
                              MHD_OPTION_END);
    flags = MHD_USE_DEBUG;
  } while (NULL == httpd && flags != MHD_USE_DEBUG);

  if (NULL == httpd)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _ ("Failed to start HTTP server\n"));
    GNUNET_SCHEDULER_shutdown ();
    return;
  }

  run_httpd ();
}

/**
 * Open a file on disk and generate a response object for it.
 *
 * @param name name of the file to open
 * @param basedir directory where the file is located
 * @return #GNUNET_SYSERR on error
 */
static struct StaticPage *
open_static_page (const char *name, const char *basedir)
{
  char *fullname = NULL;
  GNUNET_asprintf (&fullname, "%s/fcfsd-%s", basedir, name);

  struct GNUNET_DISK_FileHandle *f =
    GNUNET_DISK_file_open (fullname,
                           GNUNET_DISK_OPEN_READ,
                           GNUNET_DISK_PERM_NONE);
  GNUNET_free (fullname);

  if (NULL == f)
  {
    return NULL;
  }

  off_t size = 0;
  if (GNUNET_SYSERR == GNUNET_DISK_file_handle_size (f, &size))
  {
    GNUNET_DISK_file_close (f);
    return NULL;
  }

  struct MHD_Response *response =
    MHD_create_response_from_fd64 (size,
                                   f->fd);

  if (NULL == response)
  {
    GNUNET_DISK_file_close (f);
    return NULL;
  }

  struct StaticPage *page = GNUNET_new (struct StaticPage);
  page->handle = f;
  page->size = (uint64_t) size;
  page->response = response;
  return page;
}

/**
 * Called after the service is up.
 *
 * @param cls closure
 * @param args remaining command line arguments
 * @param cfgfile name of the configuration file
 * @param cfg the service configuration
 */
static void
run_service (void *cls,
             char *const *args,
             const char *cfgfile,
             const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  (void) cls;
  (void) args;
  (void) cfgfile;

  GNUNET_log_setup ("fcfsd", "WARNING", NULL);

  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg,
                                                          "fcfsd",
                                                          "HTTPPORT",
                                                          &port))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                _ ("No port specified, using default value\n"));
  }

  GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);

  namestore = GNUNET_NAMESTORE_connect (cfg);
  if (NULL == namestore)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _ ("Failed to connect to namestore\n"));
    GNUNET_SCHEDULER_shutdown ();
    return;
  }

  identity = GNUNET_IDENTITY_connect (cfg, &identity_cb, NULL);
  if (NULL == identity)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _ ("Failed to connect to identity\n"));
    GNUNET_SCHEDULER_shutdown ();
    return;
  }

  char *basedir = NULL;
  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
                                                            "fcfsd",
                                                            "HTMLDIR",
                                                            &basedir))
  {
    basedir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
  }

  main_page = open_static_page ("index.html", basedir);
  notfound_page = open_static_page ("notfound.html", basedir);
  forbidden_page = open_static_page ("forbidden.html", basedir);

  GNUNET_free (basedir);

  if (NULL == main_page || NULL == notfound_page || NULL == forbidden_page)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _ ("Unable to set up the daemon\n"));
    GNUNET_SCHEDULER_shutdown ();
    return;
  }
}

/**
 * The main function of the fcfs daemon.
 *
 * @param argc number of arguments from the command line
 * @parsm argv the command line argumens
 * @return 0 successful exit, a different value otherwise
 */
int
main (int argc, char *const *argv)
{
  struct GNUNET_GETOPT_CommandLineOption options[] = {
    GNUNET_GETOPT_option_mandatory
    (GNUNET_GETOPT_option_string ('z',
                                  "zone",
                                  "EGO",
                                  gettext_noop ("name of the zone managed by FCFSD"),
                                  &zone)),
    GNUNET_GETOPT_OPTION_END
  };

  return ((GNUNET_OK == GNUNET_PROGRAM_run (argc,
                                            argv,
                                            "gnunet-namestore-fcfsd",
                                            _ ("GNU Name System First-Come-First-Served name registration service"),
                                            options,
                                            &run_service,
                                            NULL)) ?
          0 :
          1);
}