aboutsummaryrefslogtreecommitdiff
path: root/src/did/gnunet-did.c
blob: 7ed0042636274ad6ba224f333771e258fab7c88c (plain) (blame)
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
/*
   This file is part of GNUnet.
   Copyright (C) 2012-2015 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
 */
/**
 * @author Tristan Schwieren
 * @file src/did/gnunet-did.c
 * @brief DID Method Wrapper 
 *
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_namestore_service.h"
#include "gnunet_gns_service.h"

/**
 * return value
 */
static int ret;

/**
 * Attribute Add 
 */
static char *attr_add;

/**
 * Attribute remove 
 */
static int *attr_remove;

/**
 *  Attibute get
 */
static int *attr_get;

/**
 * Attribute did
 */
static char *attr_did;

/**
 * Attribute did
 */
static char *attr_ego;

static struct GNUNET_GNS_Handle *gns_handle;
static struct GNUNET_NAMESTORE_Handle *namestore_handle;
static struct GNUNET_CONFIGURATRION_Handle *my_cfg;

static void resolve_did_document();
static void add_did_document();
static void get_pkey_from_attr_did();
static void print_did_document();
static void remove_did_document();
static void remove_did_ego_lookup_cb();
static void remove_did_callback();

// TODO
// static void get_did_for_ego();
// static void replace_did_document(); - use remove_did_document and add_did_document

// Add a data DID Document type

// Should the module only store and retrieve a DID document or also generate and cofigure it?
// static void generate_did_document();

static void cleanup();

static void
run (void *cls,
     char *const *args,
     const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *c)
{
  gns_handle = GNUNET_GNS_connect(c);
  namestore_handle = GNUNET_NAMESTORE_connect(c);
  my_cfg = c;

  // check if GNS_handle could connect
  if(gns_handle == NULL){
    ret = 1;
    return;
  }
  
  // check if NAMESTORE_handle could connect
  if(namestore_handle == NULL){
    ret = 1;
    return;
  }

  // check for more than one argument given
  //if (NULL != attr_did && NULL != attr_delete ||
  //    NULL != attr_did && NULL != attr_add ||
  //    NULL != attr_delete && NULL != attr_add)
  if(false)
  {
    ret = 1;
    GNUNET_SCHEDULER_add_now(cleanup, NULL);
    return;
  }

  if (NULL != attr_add) {
    add_did_document();
  } else if (1 == attr_get){
    resolve_did_document();
  } else if (1 == attr_remove) {
    remove_did_document();
  } else {
    // No Argument found
    printf("No correct argument combination found. Use gnunet-did -h for help");
    ret = 1;
    GNUNET_SCHEDULER_add_now(cleanup, NULL);
    return;
  }
}

int
main (int argc, char *const argv[])
{
  struct GNUNET_GETOPT_CommandLineOption options[] = {
    GNUNET_GETOPT_option_string ('a',
                                 "add",
                                 "VALUE",
                                 gettext_noop ("Add an DID Document"),
                                 &attr_add),
    GNUNET_GETOPT_option_flag ('r',
                              "remove",
                              gettext_noop ("Remove the DID Document with DID from GNUNET"),
                              &attr_remove),
    GNUNET_GETOPT_option_flag ('g',
                              "get",
                              gettext_noop ("Get the DID Document associated with the given DID"),
                              &attr_get),
    GNUNET_GETOPT_option_string ('d',
                                 "did",
                                 "DID",
                                 gettext_noop ("The DID to work with"),
                                 &attr_did),
    GNUNET_GETOPT_option_string ('e',
                                 "ego",
                                 "EGO",
                                 gettext_noop ("The EGO to work with"),
                                 &attr_ego),
    GNUNET_GETOPT_OPTION_END
  };

  if (GNUNET_OK != GNUNET_PROGRAM_run (argc,
                                       argv,
                                       "gnunet-did",
                                       ("did command line tool"),
                                       options,
                                       &run,
                                       NULL))
    return 1;
  else
    return ret;
}

    
static void
add_did_document(){
  printf("Do nothing\n");
}

static void
resolve_did_document()
{
  if (attr_did == NULL){
    printf("Set DID option to resolve DID\n");
  }

  struct GNUNET_IDENTITY_PublicKey pkey;
  get_pkey_from_attr_did(&pkey);

  // TODO: Check the type of returned records
  GNUNET_GNS_lookup(gns_handle, "didd", &pkey, GNUNET_DNSPARSER_TYPE_TXT, GNUNET_GNS_LO_DEFAULT, &print_did_document, NULL);
}

static void
get_pkey_from_attr_did(struct GNUNET_IDENTITY_PublicKey * pkey)
{
  char id_str[59];

  if ((1 != (sscanf (attr_did, "did:reclaim:%58s", id_str))) ||
      (GNUNET_OK != GNUNET_IDENTITY_public_key_from_string (id_str, pkey)))
  {
    fprintf (stderr, _ ("Invalid DID `%s'\n"), id_str);
    GNUNET_SCHEDULER_add_now(cleanup, NULL);
    ret = 1;
    return;
  }
}

static void 
print_did_document(
  void *cls,
  uint32_t rd_count,
  const struct GNUNET_GNSRECORD_Data *rd)
{
  // TODO: Remove "store.sock" at the end of print
  if (rd_count != 1)
  {
    printf("An ego should only have one DID Document");
    ret = 1;
    return;
  }

  printf("%s\n", rd[0].data);

  GNUNET_SCHEDULER_add_now(cleanup, NULL);
  ret = 0;
  return;
}

static void 
remove_did_document()
{
  if(attr_did == NULL && attr_ego == NULL){
    printf("Remove requieres an ego or did option\n");
    GNUNET_SCHEDULER_add_now(cleanup, NULL);
    ret = 1;
    return;
  } else if(attr_did != NULL && attr_ego != NULL){
    printf("Only set one of the EGO or DID options\n");
    GNUNET_SCHEDULER_add_now(cleanup, NULL);
    ret = 1;
    return;
  } else if (attr_ego != NULL){
    GNUNET_IDENTITY_ego_lookup(my_cfg,
                              attr_ego,
                              &remove_did_ego_lookup_cb,
                              NULL);
  } else if (attr_did != NULL){
    // TODO: Use did->pkey->ego->skey to remove did document
    // struct GNUNET_IDENTITY_PublicKey pkey;
    // get_pkey_from_attr_did(&pkey);
    printf("Remove by DID not supported\n");
    GNUNET_SCHEDULER_add_now(cleanup, NULL);
    ret = 1;
    return;
  } else {
    printf("Something during the remove went wrong. Make sure you set the options correct\n");
  }
}

static void 
remove_did_ego_lookup_cb(void *cls, struct GNUNET_IDENTITY_Ego * ego){
  const struct GNUNET_IDENTITY_PrivateKey * skey = GNUNET_IDENTITY_ego_get_private_key(ego);
  const int emp[0];
  struct GNUNET_GNSRECORD_Data rd = {
    .data = &emp, 
    .expiration_time = 0, 
    .data_size = 0, 
    .record_type = 0, 
    .flags = GNUNET_GNSRECORD_RF_NONE
  };

  GNUNET_NAMESTORE_records_store (namestore_handle,
                                  skey,
                                  "didd",
                                  0,
                                  &rd,
                                  &remove_did_callback,
                                  NULL);
}

static void
remove_did_callback(){
  // Test if record was removed from Namestore
  printf("DID Document has been removed\n");
  GNUNET_SCHEDULER_add_now(cleanup, NULL);
  ret = 0;
  return;
}

static void 
cleanup(void * cls){
  GNUNET_GNS_disconnect(gns_handle);
  GNUNET_NAMESTORE_disconnect(namestore_handle);
  GNUNET_SCHEDULER_shutdown();
}