exchange_api_restrictions.c (4065B)
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 return GNUNET_SYSERR; 55 } 56 ok = (0 == strcmp (wm1, 57 wm2)); 58 GNUNET_free (wm1); 59 GNUNET_free (wm2); 60 if (! ok) 61 return GNUNET_NO; 62 } 63 64 for (unsigned int i = 0; i<limit; i++) 65 { 66 const struct TALER_EXCHANGE_AccountRestriction *ar 67 = check_credit 68 ? &account->credit_restrictions[i] 69 : &account->debit_restrictions[i]; 70 71 switch (ar->type) 72 { 73 case TALER_EXCHANGE_AR_INVALID: 74 GNUNET_break (0); 75 return GNUNET_SYSERR; 76 case TALER_EXCHANGE_AR_DENY: 77 return GNUNET_NO; 78 case TALER_EXCHANGE_AR_REGEX: 79 { 80 regex_t ex; 81 bool allowed = false; 82 83 if (0 != regcomp (&ex, 84 ar->details.regex.posix_egrep, 85 REG_NOSUB | REG_EXTENDED)) 86 { 87 GNUNET_break_op (0); 88 return GNUNET_SYSERR; 89 } 90 if (0 == 91 regexec (&ex, 92 payto_uri.normalized_payto, 93 0, NULL, 94 0)) 95 { 96 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 97 "Account `%s' allowed by regex\n", 98 payto_uri.normalized_payto); 99 allowed = true; 100 } 101 regfree (&ex); 102 if (! allowed) 103 return GNUNET_NO; 104 break; 105 } 106 } /* end switch */ 107 } /* end loop over restrictions */ 108 return GNUNET_YES; 109 } 110 111 112 void 113 TALER_EXCHANGE_keys_evaluate_hard_limits ( 114 const struct TALER_EXCHANGE_Keys *keys, 115 enum TALER_KYCLOGIC_KycTriggerEvent event, 116 struct TALER_Amount *limit) 117 { 118 for (unsigned int i = 0; i<keys->hard_limits_length; i++) 119 { 120 const struct TALER_EXCHANGE_AccountLimit *al 121 = &keys->hard_limits[i]; 122 123 if (event != al->operation_type) 124 continue; 125 if (al->soft_limit) 126 continue; 127 if (! TALER_amount_cmp_currency (limit, 128 &al->threshold)) 129 continue; 130 GNUNET_break (GNUNET_OK == 131 TALER_amount_min (limit, 132 limit, 133 &al->threshold)); 134 } 135 } 136 137 138 bool 139 TALER_EXCHANGE_keys_evaluate_zero_limits ( 140 const struct TALER_EXCHANGE_Keys *keys, 141 enum TALER_KYCLOGIC_KycTriggerEvent event) 142 { 143 for (unsigned int i = 0; i<keys->zero_limits_length; i++) 144 { 145 const struct TALER_EXCHANGE_ZeroLimitedOperation *zlo 146 = &keys->zero_limits[i]; 147 148 if (event == zlo->operation_type) 149 return true; 150 } 151 return false; 152 }