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