summaryrefslogtreecommitdiff
path: root/src/reducer/anastasis_api_providers.c
blob: 82243f58ce76e3fa3070543b28b44a1d8238c7e0 (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
/*
  This file is part of Anastasis
  Copyright (C) 2020, 2021, 2022 Anastasis SARL

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

  Anastasis 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 General Public License for more details.

  You should have received a copy of the GNU General Public License along with
  Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file reducer/anastasis_api_providers.c
 * @brief anastasis provider synchronization logic
 * @author Christian Grothoff
 */

#include <platform.h>
#include <jansson.h>
#include "anastasis_redux.h"
#include "anastasis_error_codes.h"
#include "anastasis_api_redux.h"


/**
 * Main data structure for sync_providers().
 */
struct MasterSync;


/**
 * Data structure for one provider we are syncing /config with.
 */
struct SyncEntry
{
  /**
   * Kept in a DLL.
   */
  struct SyncEntry *next;

  /**
   * Kept in a DLL.
   */
  struct SyncEntry *prev;

  /**
   * Sync operation we are part of.
   */
  struct MasterSync *ms;

  /**
   * Redux action for this provider.
   */
  struct ANASTASIS_ReduxAction *ra;
};


/**
 * Main data structure for sync_providers().
 */
struct MasterSync
{
  /**
   * Our own sync action we expose externally.
   */
  struct ANASTASIS_ReduxAction ra;
  /**
   * Head of DLL with entries per provider.
   */
  struct SyncEntry *se_head;
  /**
   * Tail of DLL with entries per provider.
   */
  struct SyncEntry *se_tail;

  /**
   * Function to call with the result.
   */
  ANASTASIS_ActionCallback cb;

  /**
   * Closure for @e cb.
   */
  void *cb_cls;

};


/**
 * Free @a cls data structure.
 *
 * @param[in] cls data structure to free, must be a `struct MasterSync *`
 */
static void
clean_sync (void *cls)
{
  struct MasterSync *ms = cls;
  struct SyncEntry *se;

  while (NULL != (se = ms->se_head))
  {
    GNUNET_CONTAINER_DLL_remove (ms->se_head,
                                 ms->se_tail,
                                 se);
    se->ra->cleanup (se->ra->cleanup_cls);
    GNUNET_free (se);
  }
  GNUNET_free (ms);
}


/**
 * Function called when we have made progress on any of the
 * providers we are trying to sync with.
 *
 * @param cls closure
 * @param error error code, #TALER_EC_NONE if @a new_bs is the new successful state
 * @param new_state the new state of the operation (client should json_incref() to keep an alias)
 */
static void
sync_progress (void *cls,
               enum TALER_ErrorCode error,
               json_t *new_state)
{
  struct SyncEntry *se = cls;
  struct MasterSync *ms = se->ms;

  GNUNET_CONTAINER_DLL_remove (ms->se_head,
                               ms->se_tail,
                               se);
  GNUNET_free (se);
  ms->cb (ms->cb_cls,
          error,
          new_state);
  clean_sync (ms);
}


struct ANASTASIS_ReduxAction *
ANASTASIS_REDUX_sync_providers_ (json_t *state,
                                 const json_t *arguments,
                                 ANASTASIS_ActionCallback cb,
                                 void *cb_cls)
{
  json_t *rd;
  json_t *cs_arr;
  struct MasterSync *ms;

  rd = json_object_get (state,
                        "recovery_document");
  if (NULL == rd)
  {
    GNUNET_break (0);
    ANASTASIS_redux_fail_ (cb,
                           cb_cls,
                           TALER_EC_ANASTASIS_REDUCER_STATE_INVALID,
                           "'recovery_document' missing");
    return NULL;
  }
  cs_arr = json_object_get (rd,
                            "challenges");
  if (! json_is_array (cs_arr))
  {
    GNUNET_break_op (0);
    ANASTASIS_redux_fail_ (cb,
                           cb_cls,
                           TALER_EC_ANASTASIS_REDUCER_STATE_INVALID,
                           "'recovery_document' must be an array");
    return NULL;
  }
  ms = GNUNET_new (struct MasterSync);
  ms->cb = cb;
  ms->cb_cls = cb_cls;
  {
    json_t *cs;
    unsigned int n_index;

    json_array_foreach (cs_arr, n_index, cs)
    {
      const char *provider_url;
      struct GNUNET_JSON_Specification spec[] = {
        GNUNET_JSON_spec_string ("url",
                                 &provider_url),
        GNUNET_JSON_spec_end ()
      };
      struct ANASTASIS_CRYPTO_ProviderSaltP provider_salt;
      struct SyncEntry *se;

      if (GNUNET_OK !=
          GNUNET_JSON_parse (cs,
                             spec,
                             NULL, NULL))
      {
        GNUNET_break_op (0);
        ANASTASIS_redux_fail_ (cb,
                               cb_cls,
                               TALER_EC_ANASTASIS_REDUCER_STATE_INVALID,
                               "'recovery_document' missing");
        clean_sync (ms);
        return NULL;
      }
      if (GNUNET_OK ==
          ANASTASIS_reducer_lookup_salt (state,
                                         provider_url,
                                         &provider_salt))
        continue; /* provider already ready */
      se = GNUNET_new (struct SyncEntry);
      se->ms = ms;
      GNUNET_CONTAINER_DLL_insert (ms->se_head,
                                   ms->se_tail,
                                   se);
      se->ra = ANASTASIS_REDUX_add_provider_to_state_ (provider_url,
                                                       state,
                                                       &sync_progress,
                                                       se);
    }
  }
  if (NULL == ms->se_head)
  {
    /* everything already synced */
    clean_sync (ms);
    ANASTASIS_redux_fail_ (cb,
                           cb_cls,
                           TALER_EC_ANASTASIS_REDUCER_PROVIDERS_ALREADY_SYNCED,
                           "already in sync");
    return NULL;
  }
  ms->ra.cleanup = &clean_sync;
  ms->ra.cleanup_cls = ms;
  return &ms->ra;
}


struct ANASTASIS_ReduxAction *
ANASTASIS_REDUX_poll_providers_ (json_t *state,
                                 const json_t *arguments,
                                 ANASTASIS_ActionCallback cb,
                                 void *cb_cls)
{
  json_t *ap;
  const char *url;
  json_t *obj;
  struct MasterSync *ms;

  ap = json_object_get (state,
                        "authentication_providers");
  if (NULL == ap)
  {
    GNUNET_break (0);
    ANASTASIS_redux_fail_ (cb,
                           cb_cls,
                           TALER_EC_ANASTASIS_REDUCER_STATE_INVALID,
                           "'authentication_providers' missing");
    return NULL;
  }
  ms = GNUNET_new (struct MasterSync);
  ms->cb = cb;
  ms->cb_cls = cb_cls;
  json_object_foreach (ap, url, obj)
  {
    struct ANASTASIS_CRYPTO_ProviderSaltP provider_salt;
    struct SyncEntry *se;
    struct ANASTASIS_ReduxAction *ra;

    if (GNUNET_OK ==
        ANASTASIS_reducer_lookup_salt (state,
                                       url,
                                       &provider_salt))
      continue;
    se = GNUNET_new (struct SyncEntry);
    se->ms = ms;
    GNUNET_CONTAINER_DLL_insert (ms->se_head,
                                 ms->se_tail,
                                 se);
    ra = ANASTASIS_REDUX_add_provider_to_state_ (url,
                                                 state,
                                                 &sync_progress,
                                                 se);
    if (NULL == ra)
      return NULL; /* sync_progress already called! */
    se->ra = ra;
  }
  if (NULL == ms->se_head)
  {
    /* everything already synced */
    clean_sync (ms);
    ANASTASIS_redux_fail_ (cb,
                           cb_cls,
                           TALER_EC_ANASTASIS_REDUCER_ACTION_INVALID,
                           "already in sync");
    return NULL;
  }
  ms->ra.cleanup = &clean_sync;
  ms->ra.cleanup_cls = ms;
  return &ms->ra;
}