anastasis

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

anastasis_api_redux_free.c (10607B)


      1 /*
      2   This file is part of Anastasis
      3   Copyright (C) 2020, 2021, 2022 Anastasis SARL
      4 
      5   Anastasis is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU 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 General Public License for more details.
     12 
     13   You should have received a copy of the GNU General Public License along with
     14   Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 /**
     17  * @file reducer/anastasis_api_redux_free.c
     18  * @brief release a parsed reducer state
     19  * @author Christian Grothoff
     20  *
     21  * Every structure here is allocated zeroed (GNUNET_new(_array)) and every
     22  * array's length field is set before the array is filled, so this code also
     23  * has to cope with partially initialized states: the parser simply hands a
     24  * half-built state to ANASTASIS_REDUX_state_free_() when it gives up.
     25  */
     26 #include "platform.h"
     27 #include "anastasis_redux.h"
     28 #include "anastasis_api_redux.h"
     29 #include "anastasis_api_redux_state.h"
     30 
     31 
     32 void
     33 ANASTASIS_REDUX_provider_url_clear_ (struct ANASTASIS_ProviderUrl *pu)
     34 {
     35   GNUNET_free (pu->url);
     36 }
     37 
     38 
     39 void
     40 ANASTASIS_REDUX_provider_config_clear_ (struct ANASTASIS_ReduxProviderConfig *
     41                                         cfg
     42                                         )
     43 {
     44   for (unsigned int j = 0; j < cfg->methods_len; j++)
     45   {
     46     GNUNET_free (cfg->methods[j].type);
     47     TALER_amount_list_free (&cfg->methods[j].usage_fees);
     48   }
     49   GNUNET_free (cfg->methods);
     50   GNUNET_free (cfg->business_name);
     51   GNUNET_free (cfg->currency);
     52   TALER_amount_list_free (&cfg->annual_fees);
     53   TALER_amount_list_free (&cfg->truth_upload_fees);
     54   TALER_amount_list_free (&cfg->liability_limits);
     55   for (unsigned int j = 0; j < cfg->currencies_len; j++)
     56     GNUNET_free (cfg->currencies[j]);
     57   GNUNET_free (cfg->currencies);
     58   memset (cfg,
     59           0,
     60           sizeof (*cfg));
     61 }
     62 
     63 
     64 void
     65 ANASTASIS_REDUX_providers_clear_ (struct ANASTASIS_ReduxCommon *common)
     66 {
     67   for (unsigned int i = 0; i < common->providers_len; i++)
     68   {
     69     struct ANASTASIS_ReduxProvider *p = &common->providers[i];
     70 
     71     ANASTASIS_REDUX_provider_url_clear_ (&p->url);
     72     if (p->have_config)
     73       ANASTASIS_REDUX_provider_config_clear_ (&p->config);
     74   }
     75   GNUNET_free (common->providers);
     76   common->providers = NULL;
     77   common->providers_len = 0;
     78 }
     79 
     80 
     81 void
     82 ANASTASIS_REDUX_continents_clear_ (struct ANASTASIS_ReduxCommon *common)
     83 {
     84   for (unsigned int i = 0; i < common->continents_len; i++)
     85   {
     86     struct ANASTASIS_ReduxContinent *c = &common->continents[i];
     87 
     88     GNUNET_free (c->name);
     89     json_decref (c->name_i18n);
     90   }
     91   GNUNET_free (common->continents);
     92   common->continents = NULL;
     93   common->continents_len = 0;
     94 }
     95 
     96 
     97 void
     98 ANASTASIS_REDUX_countries_clear_ (struct ANASTASIS_ReduxCommon *common)
     99 {
    100   for (unsigned int i = 0; i < common->countries_len; i++)
    101   {
    102     struct ANASTASIS_ReduxCountry *c = &common->countries[i];
    103 
    104     GNUNET_free (c->code);
    105     GNUNET_free (c->name);
    106     GNUNET_free (c->continent);
    107     GNUNET_free (c->call_code);
    108     GNUNET_free (c->currency);
    109     json_decref (c->name_i18n);
    110     json_decref (c->continent_i18n);
    111   }
    112   GNUNET_free (common->countries);
    113   common->countries = NULL;
    114   common->countries_len = 0;
    115 }
    116 
    117 
    118 void
    119 ANASTASIS_REDUX_required_attributes_clear_ (
    120   struct ANASTASIS_ReduxCommon *common)
    121 {
    122   for (unsigned int i = 0; i < common->required_attributes_len; i++)
    123   {
    124     struct ANASTASIS_ReduxAttributeSpec *a = &common->required_attributes[i];
    125 
    126     GNUNET_free (a->type);
    127     GNUNET_free (a->name);
    128     GNUNET_free (a->label);
    129     GNUNET_free (a->widget);
    130     GNUNET_free (a->uuid);
    131     GNUNET_free (a->tooltip);
    132     GNUNET_free (a->validation_regex);
    133     GNUNET_free (a->validation_logic);
    134     GNUNET_free (a->autocomplete);
    135     json_decref (a->label_i18n);
    136   }
    137   GNUNET_free (common->required_attributes);
    138   common->required_attributes = NULL;
    139   common->required_attributes_len = 0;
    140 }
    141 
    142 
    143 /**
    144  * Free the contents of @a common.
    145  *
    146  * @param[in,out] common state to free
    147  */
    148 static void
    149 free_common (struct ANASTASIS_ReduxCommon *common)
    150 {
    151   ANASTASIS_REDUX_continents_clear_ (common);
    152   ANASTASIS_REDUX_countries_clear_ (common);
    153   ANASTASIS_REDUX_required_attributes_clear_ (common);
    154   ANASTASIS_REDUX_providers_clear_ (common);
    155   for (unsigned int i = 0; i < common->currencies_len; i++)
    156     GNUNET_free (common->currencies[i]);
    157   GNUNET_free (common->currencies);
    158   json_decref (common->identity_attributes);
    159   GNUNET_free (common->selected_continent);
    160   GNUNET_free (common->selected_country);
    161   GNUNET_free (common->preferred_currency);
    162 }
    163 
    164 
    165 /**
    166  * Free the contents of @a backup.
    167  *
    168  * @param[in,out] backup state to free
    169  */
    170 void
    171 ANASTASIS_REDUX_policy_clear_ (struct ANASTASIS_ReduxPolicy *p)
    172 {
    173   for (unsigned int j = 0; j < p->methods_len; j++)
    174   {
    175     struct ANASTASIS_ReduxPolicyMethod *pm = &p->methods[j];
    176 
    177     ANASTASIS_REDUX_provider_url_clear_ (&pm->provider);
    178     if (NULL != pm->truth)
    179       ANASTASIS_truth_free (pm->truth);
    180   }
    181   GNUNET_free (p->methods);
    182   p->methods = NULL;
    183   p->methods_len = 0;
    184 }
    185 
    186 
    187 void
    188 ANASTASIS_REDUX_policies_clear_ (struct ANASTASIS_ReduxBackup *backup)
    189 {
    190   for (unsigned int i = 0; i < backup->policies_len; i++)
    191     ANASTASIS_REDUX_policy_clear_ (&backup->policies[i]);
    192   GNUNET_free (backup->policies);
    193   backup->policies = NULL;
    194   backup->policies_len = 0;
    195 }
    196 
    197 
    198 void
    199 ANASTASIS_REDUX_policy_providers_clear_ (struct ANASTASIS_ReduxBackup *backup)
    200 {
    201   for (unsigned int i = 0; i < backup->policy_providers_len; i++)
    202     ANASTASIS_REDUX_provider_url_clear_ (
    203       &backup->policy_providers[i].provider_url);
    204   GNUNET_free (backup->policy_providers);
    205   backup->policy_providers = NULL;
    206   backup->policy_providers_len = 0;
    207 }
    208 
    209 
    210 void
    211 ANASTASIS_REDUX_payments_clear_ (struct ANASTASIS_ReduxBackup *backup)
    212 {
    213   for (unsigned int i = 0; i < backup->payments_len; i++)
    214     GNUNET_free (backup->payments[i]);
    215   GNUNET_free (backup->payments);
    216   backup->payments = NULL;
    217   backup->payments_len = 0;
    218   backup->have_payments = false;
    219 }
    220 
    221 
    222 void
    223 ANASTASIS_REDUX_policy_payment_requests_clear_ (
    224   struct ANASTASIS_ReduxBackup *backup)
    225 {
    226   for (unsigned int i = 0; i < backup->policy_payment_requests_len; i++)
    227   {
    228     struct ANASTASIS_ReduxPolicyPaymentRequest *ppr
    229       = &backup->policy_payment_requests[i];
    230 
    231     GNUNET_free (ppr->payto);
    232     ANASTASIS_REDUX_provider_url_clear_ (&ppr->provider);
    233   }
    234   GNUNET_free (backup->policy_payment_requests);
    235   backup->policy_payment_requests = NULL;
    236   backup->policy_payment_requests_len = 0;
    237   backup->have_policy_payment_requests = false;
    238 }
    239 
    240 
    241 static void
    242 free_backup (struct ANASTASIS_ReduxBackup *backup)
    243 {
    244   for (unsigned int i = 0; i < backup->authentication_methods_len; i++)
    245   {
    246     struct ANASTASIS_ReduxAuthMethod *am = &backup->authentication_methods[i];
    247 
    248     GNUNET_free (am->type);
    249     GNUNET_free (am->instructions);
    250     GNUNET_free (am->challenge);
    251     GNUNET_free (am->mime_type);
    252   }
    253   GNUNET_free (backup->authentication_methods);
    254   ANASTASIS_REDUX_policies_clear_ (backup);
    255   ANASTASIS_REDUX_policy_providers_clear_ (backup);
    256   GNUNET_free (backup->upload_fees);
    257   ANASTASIS_REDUX_payments_clear_ (backup);
    258   ANASTASIS_REDUX_policy_payment_requests_clear_ (backup);
    259   for (unsigned int i = 0; i < backup->success_details_len; i++)
    260     ANASTASIS_REDUX_provider_url_clear_ (
    261       &backup->success_details[i].provider_url);
    262   GNUNET_free (backup->success_details);
    263   json_decref (backup->core_secret);
    264   GNUNET_free (backup->secret_name);
    265 }
    266 
    267 
    268 /**
    269  * Free the variant-specific contents of @a fb, but not @a fb itself.
    270  *
    271  * @param[in,out] fb feedback to reset
    272  */
    273 void
    274 ANASTASIS_REDUX_feedback_clear_ (struct ANASTASIS_ReduxFeedback *fb)
    275 {
    276   GNUNET_free (fb->display_hint);
    277   switch (fb->status)
    278   {
    279   case ANASTASIS_RFS_CODE_IN_FILE:
    280     GNUNET_free (fb->details.code_in_file.filename);
    281     break;
    282   case ANASTASIS_RFS_SEND_TO_ADDRESS:
    283     GNUNET_free (fb->details.send_to_address.address_hint);
    284     break;
    285   case ANASTASIS_RFS_TALER_PAYMENT:
    286     GNUNET_free (fb->details.taler_payment.taler_pay_uri);
    287     ANASTASIS_REDUX_provider_url_clear_ (&fb->details.taler_payment.provider);
    288     break;
    289   case ANASTASIS_RFS_IBAN_INSTRUCTIONS:
    290     GNUNET_free (fb->details.iban_instructions.target_iban);
    291     GNUNET_free (fb->details.iban_instructions.target_business_name);
    292     GNUNET_free (fb->details.iban_instructions.wire_transfer_subject);
    293     break;
    294   case ANASTASIS_RFS_SERVER_FAILURE:
    295   case ANASTASIS_RFS_TRUTH_UNKNOWN:
    296   case ANASTASIS_RFS_SOLVED:
    297   case ANASTASIS_RFS_INCORRECT_ANSWER:
    298   case ANASTASIS_RFS_RATE_LIMIT_EXCEEDED:
    299     /* nothing heap-allocated beyond the display hint */
    300     break;
    301   }
    302   memset (&fb->details,
    303           0,
    304           sizeof (fb->details));
    305   fb->display_hint = NULL;
    306 }
    307 
    308 
    309 void
    310 ANASTASIS_REDUX_recovery_info_free_ (struct ANASTASIS_ReduxRecoveryInfo *ri)
    311 {
    312   if (NULL == ri)
    313     return;
    314   for (unsigned int i = 0; i < ri->challenges_len; i++)
    315   {
    316     GNUNET_free (ri->challenges[i].type);
    317     GNUNET_free (ri->challenges[i].instructions);
    318     GNUNET_free (ri->challenges[i].answer);
    319   }
    320   GNUNET_free (ri->challenges);
    321   for (unsigned int i = 0; i < ri->policies_len; i++)
    322     GNUNET_free (ri->policies[i].uuids);
    323   GNUNET_free (ri->policies);
    324   GNUNET_free (ri->secret_name);
    325   ANASTASIS_REDUX_provider_url_clear_ (&ri->provider_url);
    326   GNUNET_free (ri);
    327 }
    328 
    329 
    330 /**
    331  * Free the contents of @a recovery.
    332  *
    333  * @param[in,out] recovery state to free
    334  */
    335 static void
    336 free_recovery (struct ANASTASIS_ReduxRecovery *recovery)
    337 {
    338   if (NULL != recovery->r)
    339     ANASTASIS_recovery_abort (recovery->r);
    340   ANASTASIS_REDUX_recovery_info_free_ (recovery->ri);
    341   for (unsigned int i = 0; i < recovery->challenge_feedback_len; i++)
    342     ANASTASIS_REDUX_feedback_clear_ (&recovery->challenge_feedback[i]);
    343   GNUNET_free (recovery->challenge_feedback);
    344   json_decref (recovery->core_secret);
    345 }
    346 
    347 
    348 void
    349 ANASTASIS_REDUX_state_free_ (struct ANASTASIS_ReduxState *rs)
    350 {
    351   if (NULL == rs)
    352     return;
    353   switch (rs->type)
    354   {
    355   case ANASTASIS_RT_BACKUP:
    356     free_backup (&rs->details.backup);
    357     free_common (&rs->common);
    358     break;
    359   case ANASTASIS_RT_RECOVERY:
    360     free_recovery (&rs->details.recovery);
    361     free_common (&rs->common);
    362     break;
    363   case ANASTASIS_RT_ERROR:
    364     GNUNET_free (rs->details.error.hint);
    365     GNUNET_free (rs->details.error.detail);
    366     break;
    367   }
    368   GNUNET_free (rs);
    369 }
    370 
    371 
    372 /* end of anastasis_api_redux_free.c */