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