persist_aml_program_result.c (4776B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2023, 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 Affero 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 Affero General Public License for more details. 12 13 You should have received a copy of the GNU Affero General Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file persist_aml_program_result.c 18 * @brief helper function store results of AML programs 19 * @author Christian Grothoff 20 */ 21 #include "exchangedb_lib.h" 22 #include "taler/taler_kyclogic_lib.h" 23 #include "exchange-database/insert_aml_decision.h" 24 #include "exchange-database/insert_aml_program_failure.h" 25 #include "exchange-database/insert_successor_measure.h" 26 #include "exchange-database/persist_aml_program_result.h" 27 #include "helper.h" 28 #include <gnunet/gnunet_common.h> 29 30 31 enum GNUNET_DB_QueryStatus 32 TALER_EXCHANGEDB_persist_aml_program_result ( 33 struct TALER_EXCHANGEDB_PostgresContext *pg, 34 uint64_t process_row, 35 const struct TALER_NormalizedPaytoHashP *account_id, 36 const struct TALER_KYCLOGIC_AmlProgramResult *apr, 37 enum TALER_EXCHANGEDB_PersistProgramResultStatus *ret_pprs) 38 { 39 enum GNUNET_DB_QueryStatus qs; 40 json_t *jmeasures = NULL; 41 struct TALER_KYCLOGIC_LegitimizationRuleSet *lrs = NULL; 42 43 GNUNET_assert (NULL != ret_pprs); 44 45 *ret_pprs = TALER_EXCHANGEDB_PPRS_OK; 46 47 if ( (TALER_KYCLOGIC_AMLR_SUCCESS == apr->status) && 48 (NULL != apr->details.success.new_measures) ) 49 { 50 lrs = TALER_KYCLOGIC_rules_parse (apr->details.success.new_rules); 51 if (NULL == lrs) 52 { 53 qs = TALER_EXCHANGEDB_insert_aml_program_failure ( 54 pg, 55 process_row, 56 account_id, 57 "Failed to parse AML program output", 58 TALER_EC_EXCHANGE_KYC_AML_PROGRAM_MALFORMED_RESULT); 59 GNUNET_break (qs > 0); 60 return qs; 61 } 62 jmeasures = TALER_KYCLOGIC_get_jmeasures ( 63 lrs, 64 apr->details.success.new_measures); 65 if (NULL == jmeasures) 66 { 67 char *err; 68 69 GNUNET_break (0); 70 GNUNET_asprintf (&err, 71 "Failed to find measures `%s' specified in AML program output", 72 apr->details.success.new_measures); 73 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 74 "AML program specified invalid measures `%s'\n", 75 apr->details.success.new_measures); 76 qs = TALER_EXCHANGEDB_insert_aml_program_failure ( 77 pg, 78 process_row, 79 account_id, 80 err, 81 TALER_EC_EXCHANGE_KYC_AML_PROGRAM_MALFORMED_RESULT); 82 *ret_pprs = TALER_EXCHANGEDB_PPRS_BAD_OUTCOME; 83 TALER_KYCLOGIC_rules_free (lrs); 84 GNUNET_free (err); 85 GNUNET_break (qs > 0); 86 return qs; 87 } 88 } 89 90 qs = TALER_EXCHANGEDB_clear_aml_lock ( 91 pg, 92 account_id); 93 switch (apr->status) 94 { 95 case TALER_KYCLOGIC_AMLR_FAILURE: 96 qs = TALER_EXCHANGEDB_insert_aml_program_failure ( 97 pg, 98 process_row, 99 account_id, 100 apr->details.failure.error_message, 101 apr->details.failure.ec); 102 GNUNET_break (qs > 0); 103 goto cleanup; 104 case TALER_KYCLOGIC_AMLR_SUCCESS: 105 { 106 struct TALER_FullPayto null_payto_uri = { 0 }; 107 bool invalid_officer; 108 bool unknown_account; 109 struct GNUNET_TIME_Timestamp last_date; 110 uint64_t legitimization_measure_serial_id; 111 bool is_wallet; 112 113 qs = TALER_EXCHANGEDB_insert_aml_decision ( 114 pg, 115 null_payto_uri, 116 account_id, 117 GNUNET_TIME_timestamp_get (), 118 apr->details.success.expiration_time, 119 apr->details.success.account_properties, 120 apr->details.success.new_rules, 121 apr->details.success.to_investigate, 122 apr->details.success.new_measures, 123 jmeasures, 124 NULL, /* justification */ 125 NULL, /* decider_pub */ 126 NULL, /* decider_sig */ 127 apr->details.success.num_events, 128 apr->details.success.events, 129 NULL, /* form ID */ 130 0, /* enc_attributes_size*/ 131 NULL, /* enc_attributes*/ 132 NULL, /* attributes_hash */ 133 GNUNET_TIME_UNIT_ZERO_TS, /* attributes_expiration_time */ 134 &invalid_officer, 135 &unknown_account, 136 &last_date, 137 &legitimization_measure_serial_id, 138 &is_wallet); 139 GNUNET_break (qs > 0); 140 goto cleanup; 141 } 142 } 143 GNUNET_break (0); 144 qs = GNUNET_DB_STATUS_HARD_ERROR; 145 cleanup: 146 TALER_KYCLOGIC_rules_free (lrs); 147 json_decref (jmeasures); 148 return qs; 149 }