summaryrefslogtreecommitdiff
path: root/src/authorization/anastasis_authorization_plugin_post.c
blob: aeda95632b05c10491017887173b6a32b3509e81 (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
/*
  This file is part of Anastasis
  Copyright (C) 2021 Anastasis SARL

  Anastasis is free software; you can redistribute it and/or modify it under the
  terms of the GNU Lesser 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 Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License along with
  Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file anastasis_authorization_plugin_post.c
 * @brief authorization plugin post based
 * @author Christian Grothoff
 */
#include "platform.h"
#include "anastasis_authorization_plugin.h"
#include <taler/taler_mhd_lib.h>
#include <taler/taler_json_lib.h>
#include <jansson.h>
#include "anastasis_util_lib.h"


/**
 * Saves the State of a authorization plugin.
 */
struct PostContext
{

  /**
   * Command which is executed to run the plugin (some bash script or a
   * command line argument)
   */
  char *auth_command;

  /**
   * Messages of the plugin, read from a resource file.
   */
  json_t *messages;
};


/**
 * Saves the state of a authorization process
 */
struct ANASTASIS_AUTHORIZATION_State
{
  /**
   * Public key of the challenge which is authorised
   */
  struct ANASTASIS_CRYPTO_TruthUUIDP truth_uuid;

  /**
   * Code which is sent to the user.
   */
  uint64_t code;

  /**
   * Our plugin context.
   */
  struct PostContext *ctx;

  /**
   * Function to call when we made progress.
   */
  GNUNET_SCHEDULER_TaskCallback trigger;

  /**
   * Closure for @e trigger.
   */
  void *trigger_cls;

  /**
   * holds the truth information
   */
  json_t *post;

  /**
   * Handle to the helper process.
   */
  struct GNUNET_OS_Process *child;

  /**
   * Handle to wait for @e child
   */
  struct GNUNET_ChildWaitHandle *cwh;

  /**
   * Our client connection, set if suspended.
   */
  struct MHD_Connection *connection;

  /**
   * Message to send.
   */
  char *msg;

  /**
   * Offset of transmission in msg.
   */
  size_t msg_off;

  /**
   * Exit code from helper.
   */
  long unsigned int exit_code;

  /**
   * How did the helper die?
   */
  enum GNUNET_OS_ProcessStatusType pst;


};


/**
 * Obtain internationalized message @a msg_id from @a ctx using
 * language preferences of @a conn.
 *
 * @param messages JSON object to lookup message from
 * @param conn connection to lookup message for
 * @param msg_id unique message ID
 * @return NULL if message was not found
 */
static const char *
get_message (const json_t *messages,
             struct MHD_Connection *conn,
             const char *msg_id)
{
  const char *accept_lang;

  accept_lang = MHD_lookup_connection_value (conn,
                                             MHD_HEADER_KIND,
                                             MHD_HTTP_HEADER_ACCEPT_LANGUAGE);
  if (NULL == accept_lang)
    accept_lang = "en_US";
  {
    const char *ret;
    struct GNUNET_JSON_Specification spec[] = {
      TALER_JSON_spec_i18n_string (msg_id,
                                   accept_lang,
                                   &ret),
      GNUNET_JSON_spec_end ()
    };

    if (GNUNET_OK !=
        GNUNET_JSON_parse (messages,
                           spec,
                           NULL, NULL))
    {
      GNUNET_break (0);
      return NULL;
    }
    return ret;
  }
}


/**
 * Validate @a data is a well-formed input into the challenge method,
 * i.e. @a data is a well-formed phone number for sending an SMS, or
 * a well-formed e-mail address for sending an e-mail. Not expected to
 * check that the phone number or e-mail account actually exists.
 *
 * To be possibly used before issuing a 402 payment required to the client.
 *
 * @param cls closure
 * @param connection HTTP client request (for queuing response)
 * @param mime_type mime type of @e data
 * @param data input to validate (i.e. is it a valid phone number, etc.)
 * @param data_length number of bytes in @a data
 * @return #GNUNET_OK if @a data is valid,
 *         #GNUNET_NO if @a data is invalid and a reply was successfully queued on @a connection
 *         #GNUNET_SYSERR if @a data invalid but we failed to queue a reply on @a connection
 */
static enum GNUNET_GenericReturnValue
post_validate (void *cls,
               struct MHD_Connection *connection,
               const char *mime_type,
               const char *data,
               size_t data_length)
{
  struct PostContext *ctx = cls;
  json_t *j;
  json_error_t error;
  const char *name;
  const char *street;
  const char *city;
  const char *zip;
  const char *country;
  struct GNUNET_JSON_Specification spec[] = {
    GNUNET_JSON_spec_string ("full_name",
                             &name),
    GNUNET_JSON_spec_string ("street",
                             &street),
    GNUNET_JSON_spec_string ("city",
                             &city),
    GNUNET_JSON_spec_string ("postcode",
                             &zip),
    GNUNET_JSON_spec_string ("country",
                             &country),
    GNUNET_JSON_spec_end ()
  };

  (void) ctx;
  j = json_loadb (data,
                  data_length,
                  JSON_REJECT_DUPLICATES,
                  &error);
  if (NULL == j)
  {
    if (MHD_NO ==
        TALER_MHD_reply_with_error (connection,
                                    MHD_HTTP_EXPECTATION_FAILED,
                                    TALER_EC_ANASTASIS_POST_INVALID,
                                    "JSON malformed"))
      return GNUNET_SYSERR;
    return GNUNET_NO;
  }

  if (GNUNET_OK !=
      GNUNET_JSON_parse (j,
                         spec,
                         NULL, NULL))
  {
    GNUNET_break (0);
    json_decref (j);
    if (MHD_NO ==
        TALER_MHD_reply_with_error (connection,
                                    MHD_HTTP_EXPECTATION_FAILED,
                                    TALER_EC_ANASTASIS_POST_INVALID,
                                    "JSON lacked required address information"))
      return GNUNET_SYSERR;
    return GNUNET_NO;
  }
  json_decref (j);
  return GNUNET_OK;
}


/**
 * Begin issuing authentication challenge to user based on @a data.
 * I.e. start to send mail.
 *
 * @param cls closure
 * @param trigger function to call when we made progress
 * @param trigger_cls closure for @a trigger
 * @param truth_uuid Identifier of the challenge, to be (if possible) included in the
 *             interaction with the user
 * @param code secret code that the user has to provide back to satisfy the challenge in
 *             the main anastasis protocol
 * @param data input to validate (i.e. is it a valid phone number, etc.)
 * @param data_length number of bytes in @a data
 * @return state to track progress on the authorization operation, NULL on failure
 */
static struct ANASTASIS_AUTHORIZATION_State *
post_start (void *cls,
            GNUNET_SCHEDULER_TaskCallback trigger,
            void *trigger_cls,
            const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid,
            uint64_t code,
            const void *data,
            size_t data_length)
{
  struct PostContext *ctx = cls;
  struct ANASTASIS_AUTHORIZATION_State *as;
  json_error_t error;

  as = GNUNET_new (struct ANASTASIS_AUTHORIZATION_State);
  as->trigger = trigger;
  as->trigger_cls = trigger_cls;
  as->ctx = ctx;
  as->truth_uuid = *truth_uuid;
  as->code = code;
  as->post = json_loadb (data,
                         data_length,
                         JSON_REJECT_DUPLICATES,
                         &error);
  if (NULL == as->post)
  {
    GNUNET_break (0);
    GNUNET_free (as);
    return NULL;
  }
  return as;
}


/**
 * Function called when our Post helper has terminated.
 *
 * @param cls our `struct ANASTASIS_AUHTORIZATION_State`
 * @param type type of the process
 * @param exit_code status code of the process
 */
static void
post_done_cb (void *cls,
              enum GNUNET_OS_ProcessStatusType type,
              long unsigned int exit_code)
{
  struct ANASTASIS_AUTHORIZATION_State *as = cls;

  as->child = NULL;
  as->cwh = NULL;
  as->pst = type;
  as->exit_code = exit_code;
  MHD_resume_connection (as->connection);
  as->trigger (as->trigger_cls);
}


/**
 * Begin issuing authentication challenge to user based on @a data.
 * I.e. start to send SMS or e-mail or launch video identification.
 *
 * @param as authorization state
 * @param connection HTTP client request (for queuing response, such as redirection to video portal)
 * @return state of the request
 */
static enum ANASTASIS_AUTHORIZATION_Result
post_process (struct ANASTASIS_AUTHORIZATION_State *as,
              struct MHD_Connection *connection)
{
  const char *mime;
  const char *lang;
  MHD_RESULT mres;
  const char *name;
  const char *street;
  const char *city;
  const char *zip;
  const char *country;
  struct GNUNET_JSON_Specification spec[] = {
    GNUNET_JSON_spec_string ("full_name",
                             &name),
    GNUNET_JSON_spec_string ("street",
                             &street),
    GNUNET_JSON_spec_string ("city",
                             &city),
    GNUNET_JSON_spec_string ("postcode",
                             &zip),
    GNUNET_JSON_spec_string ("country",
                             &country),
    GNUNET_JSON_spec_end ()
  };

  mime = MHD_lookup_connection_value (connection,
                                      MHD_HEADER_KIND,
                                      MHD_HTTP_HEADER_ACCEPT);
  if (NULL == mime)
    mime = "text/plain";
  lang = MHD_lookup_connection_value (connection,
                                      MHD_HEADER_KIND,
                                      MHD_HTTP_HEADER_ACCEPT_LANGUAGE);
  if (NULL == lang)
    lang = "en";
  if (GNUNET_OK !=
      GNUNET_JSON_parse (as->post,
                         spec,
                         NULL, NULL))
  {
    GNUNET_break (0);
    mres = TALER_MHD_reply_with_error (connection,
                                       MHD_HTTP_INTERNAL_SERVER_ERROR,
                                       TALER_EC_ANASTASIS_POST_INVALID,
                                       "address information incomplete");
    if (MHD_YES != mres)
      return ANASTASIS_AUTHORIZATION_RES_FAILED_REPLY_FAILED;
    return ANASTASIS_AUTHORIZATION_RES_FAILED;
  }
  if (NULL == as->msg)
  {
    /* First time, start child process and feed pipe */
    struct GNUNET_DISK_PipeHandle *p;
    struct GNUNET_DISK_FileHandle *pipe_stdin;

    p = GNUNET_DISK_pipe (GNUNET_DISK_PF_BLOCKING_RW);
    if (NULL == p)
    {
      mres = TALER_MHD_reply_with_error (connection,
                                         MHD_HTTP_INTERNAL_SERVER_ERROR,
                                         TALER_EC_ANASTASIS_POST_HELPER_EXEC_FAILED,
                                         "pipe");
      if (MHD_YES != mres)
        return ANASTASIS_AUTHORIZATION_RES_FAILED_REPLY_FAILED;
      return ANASTASIS_AUTHORIZATION_RES_FAILED;
    }
    as->child = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_ERR,
                                         p,
                                         NULL,
                                         NULL,
                                         as->ctx->auth_command,
                                         as->ctx->auth_command,
                                         name,
                                         street,
                                         city,
                                         zip,
                                         country,
                                         NULL);
    if (NULL == as->child)
    {
      GNUNET_DISK_pipe_close (p);
      mres = TALER_MHD_reply_with_error (connection,
                                         MHD_HTTP_INTERNAL_SERVER_ERROR,
                                         TALER_EC_ANASTASIS_POST_HELPER_EXEC_FAILED,
                                         "exec");
      if (MHD_YES != mres)
        return ANASTASIS_AUTHORIZATION_RES_FAILED_REPLY_FAILED;
      return ANASTASIS_AUTHORIZATION_RES_FAILED;
    }
    pipe_stdin = GNUNET_DISK_pipe_detach_end (p,
                                              GNUNET_DISK_PIPE_END_WRITE);
    GNUNET_assert (NULL != pipe_stdin);
    GNUNET_DISK_pipe_close (p);
    {
      char *tpk;

      tpk = GNUNET_STRINGS_data_to_string_alloc (
        &as->truth_uuid,
        sizeof (as->truth_uuid));
      GNUNET_asprintf (&as->msg,
                       get_message (as->ctx->messages,
                                    connection,
                                    "body"),
                       (unsigned long long) as->code,
                       tpk);
      GNUNET_free (tpk);
    }

    {
      const char *off = as->msg;
      size_t left = strlen (off);

      while (0 != left)
      {
        ssize_t ret;

        if (0 == left)
          break;
        ret = GNUNET_DISK_file_write (pipe_stdin,
                                      off,
                                      left);
        if (ret <= 0)
        {
          mres = TALER_MHD_reply_with_error (connection,
                                             MHD_HTTP_INTERNAL_SERVER_ERROR,
                                             TALER_EC_ANASTASIS_POST_HELPER_EXEC_FAILED,
                                             "write");
          if (MHD_YES != mres)
            return ANASTASIS_AUTHORIZATION_RES_FAILED_REPLY_FAILED;
          return ANASTASIS_AUTHORIZATION_RES_FAILED;
        }
        as->msg_off += ret;
        off += ret;
        left -= ret;
      }
      GNUNET_DISK_file_close (pipe_stdin);
    }
    as->cwh = GNUNET_wait_child (as->child,
                                 &post_done_cb,
                                 as);
    as->connection = connection;
    MHD_suspend_connection (connection);
    return ANASTASIS_AUTHORIZATION_RES_SUSPENDED;
  }
  if (NULL != as->cwh)
  {
    /* Spurious call, why are we here? */
    GNUNET_break (0);
    MHD_suspend_connection (connection);
    return ANASTASIS_AUTHORIZATION_RES_SUSPENDED;
  }
  if ( (GNUNET_OS_PROCESS_EXITED != as->pst) ||
       (0 != as->exit_code) )
  {
    char es[32];

    GNUNET_snprintf (es,
                     sizeof (es),
                     "%u/%d",
                     (unsigned int) as->exit_code,
                     as->pst);
    mres = TALER_MHD_reply_with_error (connection,
                                       MHD_HTTP_INTERNAL_SERVER_ERROR,
                                       TALER_EC_ANASTASIS_POST_HELPER_COMMAND_FAILED,
                                       es);
    if (MHD_YES != mres)
      return ANASTASIS_AUTHORIZATION_RES_FAILED_REPLY_FAILED;
    return ANASTASIS_AUTHORIZATION_RES_FAILED;
  }

  /* Build HTTP response */
  {
    struct MHD_Response *resp;

    if (TALER_MHD_xmime_matches (mime,
                                 "application/json"))
    {
      resp = TALER_MHD_MAKE_JSON_PACK (
        GNUNET_JSON_pack_uint64 ("code",
                                 TALER_EC_ANASTASIS_TRUTH_CHALLENGE_RESPONSE_REQUIRED),
        GNUNET_JSON_pack_string ("hint",
                                 TALER_ErrorCode_get_hint (
                                   TALER_EC_ANASTASIS_TRUTH_CHALLENGE_RESPONSE_REQUIRED)),
        GNUNET_JSON_pack_string ("detail",
                                 zip));
    }
    else
    {
      size_t reply_len;
      char *reply;

      reply_len = GNUNET_asprintf (&reply,
                                   get_message (as->ctx->messages,
                                                connection,
                                                "instructions"),
                                   zip);
      resp = MHD_create_response_from_buffer (reply_len,
                                              reply,
                                              MHD_RESPMEM_MUST_COPY);
      GNUNET_free (reply);
      TALER_MHD_add_global_headers (resp);
    }
    mres = MHD_queue_response (connection,
                               MHD_HTTP_FORBIDDEN,
                               resp);
    MHD_destroy_response (resp);
    if (MHD_YES != mres)
      return ANASTASIS_AUTHORIZATION_RES_SUCCESS_REPLY_FAILED;
    return ANASTASIS_AUTHORIZATION_RES_SUCCESS;
  }
}


/**
 * Free internal state associated with @a as.
 *
 * @param as state to clean up
 */
static void
post_cleanup (struct ANASTASIS_AUTHORIZATION_State *as)
{
  if (NULL != as->cwh)
  {
    GNUNET_wait_child_cancel (as->cwh);
    as->cwh = NULL;
  }
  if (NULL != as->child)
  {
    (void) GNUNET_OS_process_kill (as->child,
                                   SIGKILL);
    GNUNET_break (GNUNET_OK ==
                  GNUNET_OS_process_wait (as->child));
    as->child = NULL;
  }
  GNUNET_free (as->msg);
  json_decref (as->post);
  GNUNET_free (as);
}


/**
 * Initialize post based authorization plugin
 *
 * @param cls a configuration instance
 * @return NULL on error, otherwise a `struct ANASTASIS_AuthorizationPlugin`
 */
void *
libanastasis_plugin_authorization_post_init (void *cls)
{
  struct ANASTASIS_AuthorizationPlugin *plugin;
  struct GNUNET_CONFIGURATION_Handle *cfg = cls;
  struct PostContext *ctx;

  ctx = GNUNET_new (struct PostContext);
  {
    char *fn;
    json_error_t err;

    GNUNET_asprintf (&fn,
                     "%sauthorization-post-messages.json",
                     GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR));
    ctx->messages = json_load_file (fn,
                                    JSON_REJECT_DUPLICATES,
                                    &err);
    if (NULL == ctx->messages)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "Failed to load messages from `%s': %s at %d:%d\n",
                  fn,
                  err.text,
                  err.line,
                  err.column);
      GNUNET_free (fn);
      GNUNET_free (ctx);
      return NULL;
    }
    GNUNET_free (fn);
  }
  plugin = GNUNET_new (struct ANASTASIS_AuthorizationPlugin);
  plugin->code_validity_period = GNUNET_TIME_UNIT_MONTHS;
  plugin->code_rotation_period = GNUNET_TIME_UNIT_WEEKS;
  plugin->code_retransmission_frequency
    = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_DAYS,
                                     2);
  plugin->cls = ctx;
  plugin->validate = &post_validate;
  plugin->start = &post_start;
  plugin->process = &post_process;
  plugin->cleanup = &post_cleanup;

  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_string (cfg,
                                             "authorization-post",
                                             "COMMAND",
                                             &ctx->auth_command))
  {
    GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
                               "authorization-post",
                               "COMMAND");
    json_decref (ctx->messages);
    GNUNET_free (ctx);
    GNUNET_free (plugin);
    return NULL;
  }
  return plugin;
}


/**
 * Unload authorization plugin
 *
 * @param cls a `struct ANASTASIS_AuthorizationPlugin`
 * @return NULL (always)
 */
void *
libanastasis_plugin_authorization_post_done (void *cls)
{
  struct ANASTASIS_AuthorizationPlugin *plugin = cls;
  struct PostContext *ctx = plugin->cls;

  GNUNET_free (ctx->auth_command);
  json_decref (ctx->messages);
  GNUNET_free (ctx);
  GNUNET_free (plugin);
  return NULL;
}