exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

exchange_api_restrictions.c (4109B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2024 Taler Systems SA
      4 
      5   TALER 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   TALER 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   TALER; see the file COPYING.  If not, see
     15   <http://www.gnu.org/licenses/>
     16 */
     17 /**
     18  * @file lib/exchange_api_restrictions.c
     19  * @brief convenience functions related to account restrictions
     20  * @author Christian Grothoff
     21  */
     22 #include "taler/taler_exchange_service.h"
     23 #include <regex.h>
     24 
     25 
     26 enum GNUNET_GenericReturnValue
     27 TALER_EXCHANGE_test_account_allowed (
     28   const struct TALER_EXCHANGE_WireAccount *account,
     29   bool check_credit,
     30   const struct TALER_NormalizedPayto payto_uri)
     31 {
     32   unsigned int limit
     33     = check_credit
     34     ? account->credit_restrictions_length
     35     : account->debit_restrictions_length;
     36 
     37   /* check wire method matches */
     38   {
     39     char *wm1;
     40     char *wm2;
     41     bool ok;
     42 
     43     wm1 = TALER_payto_get_method (payto_uri.normalized_payto);
     44     if (NULL == wm1)
     45     {
     46       GNUNET_break (0);
     47       GNUNET_free (wm1);
     48       return GNUNET_SYSERR;
     49     }
     50     wm2 = TALER_payto_get_method (account->fpayto_uri.full_payto);
     51     if (NULL == wm2)
     52     {
     53       GNUNET_break (0);
     54       GNUNET_free (wm1);
     55       return GNUNET_SYSERR;
     56     }
     57     ok = (0 == strcmp (wm1,
     58                        wm2));
     59     GNUNET_free (wm1);
     60     GNUNET_free (wm2);
     61     if (! ok)
     62       return GNUNET_NO;
     63   }
     64 
     65   for (unsigned int i = 0; i<limit; i++)
     66   {
     67     const struct TALER_EXCHANGE_AccountRestriction *ar
     68       = check_credit
     69       ? &account->credit_restrictions[i]
     70       : &account->debit_restrictions[i];
     71 
     72     switch (ar->type)
     73     {
     74     case TALER_EXCHANGE_AR_INVALID:
     75       GNUNET_break (0);
     76       return GNUNET_SYSERR;
     77     case TALER_EXCHANGE_AR_DENY:
     78       return GNUNET_NO;
     79     case TALER_EXCHANGE_AR_REGEX:
     80       {
     81         regex_t ex;
     82         bool allowed = false;
     83 
     84         if (0 != regcomp (&ex,
     85                           ar->details.regex.posix_egrep,
     86                           REG_NOSUB | REG_EXTENDED))
     87         {
     88           GNUNET_break_op (0);
     89           return GNUNET_SYSERR;
     90         }
     91         if (0 ==
     92             regexec (&ex,
     93                      payto_uri.normalized_payto,
     94                      0, NULL,
     95                      0))
     96         {
     97           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     98                       "Account `%s' allowed by regex\n",
     99                       payto_uri.normalized_payto);
    100           allowed = true;
    101         }
    102         regfree (&ex);
    103         if (! allowed)
    104           return GNUNET_NO;
    105         break;
    106       }
    107     }     /* end switch */
    108   }     /* end loop over restrictions */
    109   return GNUNET_YES;
    110 }
    111 
    112 
    113 void
    114 TALER_EXCHANGE_keys_evaluate_hard_limits (
    115   const struct TALER_EXCHANGE_Keys *keys,
    116   enum TALER_KYCLOGIC_KycTriggerEvent event,
    117   struct TALER_Amount *limit)
    118 {
    119   for (unsigned int i = 0; i<keys->hard_limits_length; i++)
    120   {
    121     const struct TALER_EXCHANGE_AccountLimit *al
    122       = &keys->hard_limits[i];
    123 
    124     if (event != al->operation_type)
    125       continue;
    126     if (al->soft_limit)
    127       continue;
    128     if (GNUNET_YES !=
    129         TALER_amount_cmp_currency (limit,
    130                                    &al->threshold))
    131       continue;
    132     GNUNET_break (GNUNET_OK ==
    133                   TALER_amount_min (limit,
    134                                     limit,
    135                                     &al->threshold));
    136   }
    137 }
    138 
    139 
    140 bool
    141 TALER_EXCHANGE_keys_evaluate_zero_limits (
    142   const struct TALER_EXCHANGE_Keys *keys,
    143   enum TALER_KYCLOGIC_KycTriggerEvent event)
    144 {
    145   for (unsigned int i = 0; i<keys->zero_limits_length; i++)
    146   {
    147     const struct TALER_EXCHANGE_ZeroLimitedOperation *zlo
    148       = &keys->zero_limits[i];
    149 
    150     if (event == zlo->operation_type)
    151       return true;
    152   }
    153   return false;
    154 }