anastasis

Credential backup and recovery protocol and service
Log | Files | Refs | Submodules | README | LICENSE

anastasis_authorization_plugin_email.c (19108B)


      1 /*
      2   This file is part of Anastasis
      3   Copyright (C) 2019-2021 Anastasis SARL
      4 
      5   Anastasis is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU Affero General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11   A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
     12 
     13   You should have received a copy of the GNU Affero General Public License along with
     14   Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 /**
     17  * @file anastasis_authorization_plugin_email.c
     18  * @brief authorization plugin email based
     19  * @author Dominik Meister
     20  */
     21 #include "platform.h"
     22 #include "anastasis_authorization_plugin.h"
     23 #include <taler/taler_mhd_lib.h>
     24 #include <taler/taler_json_lib.h>
     25 #include <regex.h>
     26 #include "anastasis_util_lib.h"
     27 #include <gnunet/gnunet_db_lib.h>
     28 #include "anastasis_database_lib.h"
     29 
     30 /**
     31  * How many retries do we allow per code?
     32  */
     33 #define INITIAL_RETRY_COUNTER 3
     34 
     35 /**
     36  * Saves the State of a authorization plugin.
     37  */
     38 struct Email_Context
     39 {
     40 
     41   /**
     42    * Command which is executed to run the plugin (some bash script or a
     43    * command line argument)
     44    */
     45   char *auth_command;
     46 
     47   /**
     48    * Regex for email address validation.
     49    */
     50   regex_t regex;
     51 
     52   /**
     53    * Messages of the plugin, read from a resource file.
     54    */
     55   json_t *messages;
     56 
     57   /**
     58    * Context we operate in.
     59    */
     60   const struct ANASTASIS_AuthorizationContext *ac;
     61 
     62 };
     63 
     64 
     65 /**
     66  * Saves the state of a authorization process
     67  */
     68 struct ANASTASIS_AUTHORIZATION_State
     69 {
     70   /**
     71    * Public key of the challenge which is authorised
     72    */
     73   struct ANASTASIS_CRYPTO_TruthUUIDP truth_uuid;
     74 
     75   /**
     76    * Code which is sent to the user.
     77    */
     78   uint64_t code;
     79 
     80   /**
     81    * Our plugin context.
     82    */
     83   struct Email_Context *ctx;
     84 
     85   /**
     86    * Function to call when we made progress.
     87    */
     88   GNUNET_SCHEDULER_TaskCallback trigger;
     89 
     90   /**
     91    * Closure for @e trigger.
     92    */
     93   void *trigger_cls;
     94 
     95   /**
     96    * holds the truth information
     97    */
     98   char *email;
     99 
    100   /**
    101    * Handle to the helper process.
    102    */
    103   struct GNUNET_OS_Process *child;
    104 
    105   /**
    106    * Handle to wait for @e child
    107    */
    108   struct GNUNET_ChildWaitHandle *cwh;
    109 
    110   /**
    111    * Our client connection, set if suspended.
    112    */
    113   struct MHD_Connection *connection;
    114 
    115   /**
    116    * Message to send.
    117    */
    118   char *msg;
    119 
    120   /**
    121    * Offset of transmission in msg.
    122    */
    123   size_t msg_off;
    124 
    125   /**
    126    * Exit code from helper.
    127    */
    128   long unsigned int exit_code;
    129 
    130   /**
    131    * How did the helper die?
    132    */
    133   enum GNUNET_OS_ProcessStatusType pst;
    134 
    135 };
    136 
    137 
    138 /**
    139  * Obtain internationalized message @a msg_id from @a ctx using
    140  * language preferences of @a conn.
    141  *
    142  * @param messages JSON object to lookup message from
    143  * @param conn connection to lookup message for
    144  * @param msg_id unique message ID
    145  * @return NULL if message was not found
    146  */
    147 static const char *
    148 get_message (const json_t *messages,
    149              struct MHD_Connection *conn,
    150              const char *msg_id)
    151 {
    152   const char *accept_lang;
    153 
    154   accept_lang = MHD_lookup_connection_value (conn,
    155                                              MHD_HEADER_KIND,
    156                                              MHD_HTTP_HEADER_ACCEPT_LANGUAGE);
    157   if (NULL == accept_lang)
    158     accept_lang = "en_US";
    159   {
    160     const char *ret;
    161     struct GNUNET_JSON_Specification spec[] = {
    162       TALER_JSON_spec_i18n_string (msg_id,
    163                                    accept_lang,
    164                                    &ret),
    165       GNUNET_JSON_spec_end ()
    166     };
    167 
    168     if (GNUNET_OK !=
    169         GNUNET_JSON_parse (messages,
    170                            spec,
    171                            NULL, NULL))
    172     {
    173       GNUNET_break (0);
    174       GNUNET_JSON_parse_free (spec);
    175       return NULL;
    176     }
    177     GNUNET_JSON_parse_free (spec);
    178     return ret;
    179   }
    180 }
    181 
    182 
    183 /**
    184  * Validate @a data is a well-formed input into the challenge method,
    185  * i.e. @a data is a well-formed phone number for sending an SMS, or
    186  * a well-formed e-mail address for sending an e-mail. Not expected to
    187  * check that the phone number or e-mail account actually exists.
    188  *
    189  * To be possibly used before issuing a 402 payment required to the client.
    190  *
    191  * @param cls closure
    192  * @param connection HTTP client request (for queuing response)
    193  * @param mime_type mime type of @e data
    194  * @param data input to validate (i.e. is it a valid phone number, etc.)
    195  * @param data_length number of bytes in @a data
    196  * @return #GNUNET_OK if @a data is valid,
    197  *         #GNUNET_NO if @a data is invalid and a reply was successfully queued on @a connection
    198  *         #GNUNET_SYSERR if @a data invalid but we failed to queue a reply on @a connection
    199  */
    200 static enum GNUNET_GenericReturnValue
    201 email_validate (void *cls,
    202                 struct MHD_Connection *connection,
    203                 const char *mime_type,
    204                 const char *data,
    205                 size_t data_length)
    206 {
    207   struct Email_Context *ctx = cls;
    208   int regex_result;
    209   char *phone_number;
    210 
    211   phone_number = GNUNET_strndup (data,
    212                                  data_length);
    213   regex_result = regexec (&ctx->regex,
    214                           phone_number,
    215                           0,
    216                           NULL,
    217                           0);
    218   GNUNET_free (phone_number);
    219   if (0 != regex_result)
    220   {
    221     if (MHD_NO ==
    222         TALER_MHD_reply_with_error (connection,
    223                                     MHD_HTTP_CONFLICT,
    224                                     TALER_EC_ANASTASIS_EMAIL_INVALID,
    225                                     NULL))
    226       return GNUNET_SYSERR;
    227     return GNUNET_NO;
    228   }
    229   return GNUNET_OK;
    230 }
    231 
    232 
    233 /**
    234  * Begin issuing authentication challenge to user based on @a data.
    235  * I.e. start to send SMS or e-mail or launch video identification.
    236  *
    237  * @param cls closure
    238  * @param trigger function to call when we made progress
    239  * @param trigger_cls closure for @a trigger
    240  * @param truth_uuid Identifier of the challenge, to be (if possible) included in the
    241  *             interaction with the user
    242  * @param code secret code that the user has to provide back to satisfy the challenge in
    243  *             the main anastasis protocol
    244  * @param data input to validate (i.e. is it a valid phone number, etc.)
    245  * @param data_length number of bytes in @a data
    246  * @return state to track progress on the authorization operation, NULL on failure
    247  */
    248 static struct ANASTASIS_AUTHORIZATION_State *
    249 email_start (void *cls,
    250              GNUNET_SCHEDULER_TaskCallback trigger,
    251              void *trigger_cls,
    252              const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid,
    253              uint64_t code,
    254              const void *data,
    255              size_t data_length)
    256 {
    257   struct Email_Context *ctx = cls;
    258   struct ANASTASIS_AUTHORIZATION_State *as;
    259   enum GNUNET_DB_QueryStatus qs;
    260 
    261   /* If the user can show this challenge code, this
    262      plugin is already happy (no additional
    263      requirements), so mark this challenge as
    264      already satisfied from the start. */
    265   qs = ctx->ac->db->mark_challenge_code_satisfied (ctx->ac->db->cls,
    266                                                    truth_uuid,
    267                                                    code);
    268   if (qs <= 0)
    269   {
    270     GNUNET_break (0);
    271     return NULL;
    272   }
    273   as = GNUNET_new (struct ANASTASIS_AUTHORIZATION_State);
    274   as->trigger = trigger;
    275   as->trigger_cls = trigger_cls;
    276   as->ctx = ctx;
    277   as->truth_uuid = *truth_uuid;
    278   as->code = code;
    279   as->email = GNUNET_strndup (data,
    280                               data_length);
    281   return as;
    282 }
    283 
    284 
    285 /**
    286  * Function called when our Email helper has terminated.
    287  *
    288  * @param cls our `struct ANASTASIS_AUHTORIZATION_State`
    289  * @param type type of the process
    290  * @param exit_code status code of the process
    291  */
    292 static void
    293 email_done_cb (void *cls,
    294                enum GNUNET_OS_ProcessStatusType type,
    295                long unsigned int exit_code)
    296 {
    297   struct ANASTASIS_AUTHORIZATION_State *as = cls;
    298 
    299   as->cwh = NULL;
    300   if (NULL != as->child)
    301   {
    302     GNUNET_OS_process_destroy (as->child);
    303     as->child = NULL;
    304   }
    305   as->pst = type;
    306   as->exit_code = exit_code;
    307   MHD_resume_connection (as->connection);
    308   as->trigger (as->trigger_cls);
    309 }
    310 
    311 
    312 /**
    313  * Begin issuing authentication challenge to user based on @a data.
    314  * I.e. start to send SMS or e-mail or launch video identification.
    315  *
    316  * @param as authorization state
    317  * @param connection HTTP client request (for queuing response, such as redirection to video portal)
    318  * @return state of the request
    319  */
    320 static enum ANASTASIS_AUTHORIZATION_ChallengeResult
    321 email_challenge (struct ANASTASIS_AUTHORIZATION_State *as,
    322                  struct MHD_Connection *connection)
    323 {
    324   MHD_RESULT mres;
    325   const char *mime;
    326   const char *lang;
    327 
    328   mime = MHD_lookup_connection_value (connection,
    329                                       MHD_HEADER_KIND,
    330                                       MHD_HTTP_HEADER_ACCEPT);
    331   if (NULL == mime)
    332     mime = "text/plain";
    333   lang = MHD_lookup_connection_value (connection,
    334                                       MHD_HEADER_KIND,
    335                                       MHD_HTTP_HEADER_ACCEPT_LANGUAGE);
    336   if (NULL == lang)
    337     lang = "en";
    338   if (NULL == as->msg)
    339   {
    340     /* First time, start child process and feed pipe */
    341     struct GNUNET_DISK_PipeHandle *p;
    342     struct GNUNET_DISK_FileHandle *pipe_stdin;
    343 
    344     p = GNUNET_DISK_pipe (GNUNET_DISK_PF_BLOCKING_RW);
    345     if (NULL == p)
    346     {
    347       mres = TALER_MHD_reply_with_error (connection,
    348                                          MHD_HTTP_INTERNAL_SERVER_ERROR,
    349                                          TALER_EC_ANASTASIS_EMAIL_HELPER_EXEC_FAILED,
    350                                          "pipe");
    351       if (MHD_YES != mres)
    352         return ANASTASIS_AUTHORIZATION_CRES_FAILED_REPLY_FAILED;
    353       return ANASTASIS_AUTHORIZATION_CRES_FAILED;
    354     }
    355     as->child = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_ERR,
    356                                          p,
    357                                          NULL,
    358                                          NULL,
    359                                          as->ctx->auth_command,
    360                                          as->ctx->auth_command,
    361                                          as->email,
    362                                          NULL);
    363     if (NULL == as->child)
    364     {
    365       GNUNET_DISK_pipe_close (p);
    366       mres = TALER_MHD_reply_with_error (connection,
    367                                          MHD_HTTP_INTERNAL_SERVER_ERROR,
    368                                          TALER_EC_ANASTASIS_EMAIL_HELPER_EXEC_FAILED,
    369                                          "exec");
    370       if (MHD_YES != mres)
    371         return ANASTASIS_AUTHORIZATION_CRES_FAILED_REPLY_FAILED;
    372       return ANASTASIS_AUTHORIZATION_CRES_FAILED;
    373     }
    374     pipe_stdin = GNUNET_DISK_pipe_detach_end (p,
    375                                               GNUNET_DISK_PIPE_END_WRITE);
    376     GNUNET_assert (NULL != pipe_stdin);
    377     GNUNET_DISK_pipe_close (p);
    378     GNUNET_asprintf (&as->msg,
    379                      get_message (as->ctx->messages,
    380                                   connection,
    381                                   "body"),
    382                      ANASTASIS_pin2s (as->code),
    383                      ANASTASIS_CRYPTO_uuid2s (&as->truth_uuid));
    384 
    385     {
    386       const char *off = as->msg;
    387       size_t left = strlen (off);
    388 
    389       while (0 != left)
    390       {
    391         ssize_t ret;
    392 
    393         ret = GNUNET_DISK_file_write (pipe_stdin,
    394                                       off,
    395                                       left);
    396         if (ret <= 0)
    397         {
    398           mres = TALER_MHD_reply_with_error (connection,
    399                                              MHD_HTTP_INTERNAL_SERVER_ERROR,
    400                                              TALER_EC_ANASTASIS_EMAIL_HELPER_EXEC_FAILED,
    401                                              "write");
    402           if (MHD_YES != mres)
    403             return ANASTASIS_AUTHORIZATION_CRES_FAILED_REPLY_FAILED;
    404           return ANASTASIS_AUTHORIZATION_CRES_FAILED;
    405         }
    406         as->msg_off += ret;
    407         off += ret;
    408         left -= ret;
    409       }
    410       GNUNET_DISK_file_close (pipe_stdin);
    411     }
    412     as->cwh = GNUNET_wait_child (as->child,
    413                                  &email_done_cb,
    414                                  as);
    415     as->connection = connection;
    416     MHD_suspend_connection (connection);
    417     return ANASTASIS_AUTHORIZATION_CRES_SUSPENDED;
    418   }
    419   if (NULL != as->cwh)
    420   {
    421     /* Spurious call, why are we here? */
    422     GNUNET_break (0);
    423     MHD_suspend_connection (connection);
    424     return ANASTASIS_AUTHORIZATION_CRES_SUSPENDED;
    425   }
    426   if ( (GNUNET_OS_PROCESS_EXITED != as->pst) ||
    427        (0 != as->exit_code) )
    428   {
    429     char es[32];
    430 
    431     GNUNET_snprintf (es,
    432                      sizeof (es),
    433                      "%u/%d",
    434                      (unsigned int) as->exit_code,
    435                      as->pst);
    436     mres = TALER_MHD_reply_with_error (connection,
    437                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
    438                                        TALER_EC_ANASTASIS_EMAIL_HELPER_COMMAND_FAILED,
    439                                        es);
    440     if (MHD_YES != mres)
    441       return ANASTASIS_AUTHORIZATION_CRES_FAILED_REPLY_FAILED;
    442     return ANASTASIS_AUTHORIZATION_CRES_FAILED;
    443   }
    444 
    445   /* Build HTTP response */
    446   {
    447     struct MHD_Response *resp;
    448     const char *at;
    449     size_t len;
    450 
    451     at = strchr (as->email, '@');
    452     if (NULL == at)
    453       len = 0;
    454     else
    455       len = at - as->email;
    456 
    457     if (0.0 < TALER_pattern_matches (mime,
    458                                      "application/json"))
    459     {
    460       char *user;
    461 
    462       user = GNUNET_strndup (as->email,
    463                              len);
    464       resp = TALER_MHD_MAKE_JSON_PACK (
    465         GNUNET_JSON_pack_string ("challenge_type",
    466                                  "TAN_SENT"),
    467         GNUNET_JSON_pack_string ("tan_address_hint",
    468                                  user));
    469       GNUNET_free (user);
    470     }
    471     else
    472     {
    473       size_t reply_len;
    474       char *reply;
    475 
    476       reply_len = GNUNET_asprintf (&reply,
    477                                    get_message (as->ctx->messages,
    478                                                 connection,
    479                                                 "instructions"),
    480                                    (unsigned int) len,
    481                                    as->email);
    482       resp = MHD_create_response_from_buffer (reply_len,
    483                                               reply,
    484                                               MHD_RESPMEM_MUST_COPY);
    485       GNUNET_free (reply);
    486       TALER_MHD_add_global_headers (resp,
    487                                     false);
    488       GNUNET_break (MHD_YES ==
    489                     MHD_add_response_header (resp,
    490                                              MHD_HTTP_HEADER_CONTENT_TYPE,
    491                                              "text/plain"));
    492     }
    493     mres = MHD_queue_response (connection,
    494                                MHD_HTTP_OK,
    495                                resp);
    496     MHD_destroy_response (resp);
    497     if (MHD_YES != mres)
    498       return ANASTASIS_AUTHORIZATION_CRES_SUCCESS_REPLY_FAILED;
    499     return ANASTASIS_AUTHORIZATION_CRES_SUCCESS;
    500   }
    501 }
    502 
    503 
    504 /**
    505  * Free internal state associated with @a as.
    506  *
    507  * @param as state to clean up
    508  */
    509 static void
    510 email_cleanup (struct ANASTASIS_AUTHORIZATION_State *as)
    511 {
    512   if (NULL != as->cwh)
    513   {
    514     GNUNET_wait_child_cancel (as->cwh);
    515     as->cwh = NULL;
    516   }
    517   if (NULL != as->child)
    518   {
    519     (void) GNUNET_OS_process_kill (as->child,
    520                                    SIGKILL);
    521     GNUNET_break (GNUNET_OK ==
    522                   GNUNET_OS_process_wait (as->child));
    523     as->child = NULL;
    524   }
    525   GNUNET_free (as->msg);
    526   GNUNET_free (as->email);
    527   GNUNET_free (as);
    528 }
    529 
    530 
    531 /**
    532  * Initialize email based authorization plugin
    533  *
    534  * @param cls a configuration instance
    535  * @return NULL on error, otherwise a `struct ANASTASIS_AuthorizationPlugin`
    536  */
    537 void *
    538 libanastasis_plugin_authorization_email_init (void *cls);
    539 
    540 /* declaration to fix compiler warning */
    541 void *
    542 libanastasis_plugin_authorization_email_init (void *cls)
    543 {
    544   const struct ANASTASIS_AuthorizationContext *ac = cls;
    545   struct ANASTASIS_AuthorizationPlugin *plugin;
    546   const struct GNUNET_CONFIGURATION_Handle *cfg = ac->cfg;
    547   struct Email_Context *ctx;
    548 
    549   ctx = GNUNET_new (struct Email_Context);
    550   ctx->ac = ac;
    551   {
    552     char *fn;
    553     json_error_t err;
    554     char *tmp;
    555 
    556     tmp = GNUNET_OS_installation_get_path (ANASTASIS_project_data (),
    557                                            GNUNET_OS_IPK_DATADIR);
    558     GNUNET_asprintf (&fn,
    559                      "%sauthorization-email-messages.json",
    560                      tmp);
    561     GNUNET_free (tmp);
    562     ctx->messages = json_load_file (fn,
    563                                     JSON_REJECT_DUPLICATES,
    564                                     &err);
    565     if (NULL == ctx->messages)
    566     {
    567       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    568                   "Failed to load messages from `%s': %s at %d:%d\n",
    569                   fn,
    570                   err.text,
    571                   err.line,
    572                   err.column);
    573       GNUNET_free (fn);
    574       GNUNET_free (ctx);
    575       return NULL;
    576     }
    577     GNUNET_free (fn);
    578   }
    579   {
    580     int regex_result;
    581     const char *regexp = "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}";
    582 
    583     regex_result = regcomp (&ctx->regex,
    584                             regexp,
    585                             REG_EXTENDED);
    586     if (0 < regex_result)
    587     {
    588       GNUNET_break (0);
    589       json_decref (ctx->messages);
    590       GNUNET_free (ctx);
    591       return NULL;
    592     }
    593   }
    594 
    595   plugin = GNUNET_new (struct ANASTASIS_AuthorizationPlugin);
    596   plugin->retry_counter = INITIAL_RETRY_COUNTER;
    597   plugin->code_validity_period = GNUNET_TIME_UNIT_DAYS;
    598   plugin->code_rotation_period = GNUNET_TIME_UNIT_HOURS;
    599   plugin->code_retransmission_frequency = GNUNET_TIME_UNIT_MINUTES;
    600   plugin->cls = ctx;
    601   plugin->validate = &email_validate;
    602   plugin->start = &email_start;
    603   plugin->challenge = &email_challenge;
    604   plugin->cleanup = &email_cleanup;
    605 
    606   if (GNUNET_OK !=
    607       GNUNET_CONFIGURATION_get_value_string (cfg,
    608                                              "authorization-email",
    609                                              "COMMAND",
    610                                              &ctx->auth_command))
    611   {
    612     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
    613                                "authorization-email",
    614                                "COMMAND");
    615     regfree (&ctx->regex);
    616     json_decref (ctx->messages);
    617     GNUNET_free (ctx);
    618     GNUNET_free (plugin);
    619     return NULL;
    620   }
    621   return plugin;
    622 }
    623 
    624 
    625 /**
    626  * Unload authorization plugin
    627  *
    628  * @param cls a `struct ANASTASIS_AuthorizationPlugin`
    629  * @return NULL (always)
    630  */
    631 void *
    632 libanastasis_plugin_authorization_email_done (void *cls);
    633 
    634 /* declaration to fix compiler warning */
    635 void *
    636 libanastasis_plugin_authorization_email_done (void *cls)
    637 {
    638   struct ANASTASIS_AuthorizationPlugin *plugin = cls;
    639   struct Email_Context *ctx = plugin->cls;
    640 
    641   GNUNET_free (ctx->auth_command);
    642   regfree (&ctx->regex);
    643   json_decref (ctx->messages);
    644   GNUNET_free (ctx);
    645   GNUNET_free (plugin);
    646   return NULL;
    647 }