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