Line data Source code
1 : /* 2 : This file is part of TALER 3 : Copyright (C) 2015-2023 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_management_wire_disable.c 19 : * @brief functions to disable an exchange wire method / bank account 20 : * @author Christian Grothoff 21 : */ 22 : #include "platform.h" 23 : #include "taler_json_lib.h" 24 : #include <gnunet/gnunet_curl_lib.h> 25 : #include <microhttpd.h> 26 : #include "taler_exchange_service.h" 27 : #include "exchange_api_curl_defaults.h" 28 : #include "taler_signatures.h" 29 : #include "taler_curl_lib.h" 30 : #include "taler_json_lib.h" 31 : 32 : 33 : struct TALER_EXCHANGE_ManagementWireDisableHandle 34 : { 35 : 36 : /** 37 : * The url for this request. 38 : */ 39 : char *url; 40 : 41 : /** 42 : * Minor context that holds body and headers. 43 : */ 44 : struct TALER_CURL_PostContext post_ctx; 45 : 46 : /** 47 : * Handle for the request. 48 : */ 49 : struct GNUNET_CURL_Job *job; 50 : 51 : /** 52 : * Function to call with the result. 53 : */ 54 : TALER_EXCHANGE_ManagementWireDisableCallback cb; 55 : 56 : /** 57 : * Closure for @a cb. 58 : */ 59 : void *cb_cls; 60 : 61 : /** 62 : * Reference to the execution context. 63 : */ 64 : struct GNUNET_CURL_Context *ctx; 65 : }; 66 : 67 : 68 : /** 69 : * Function called when we're done processing the 70 : * HTTP /management/wire/disable request. 71 : * 72 : * @param cls the `struct TALER_EXCHANGE_ManagementAuditorDisableHandle *` 73 : * @param response_code HTTP response code, 0 on error 74 : * @param response response body, NULL if not in JSON 75 : */ 76 : static void 77 6 : handle_auditor_disable_finished (void *cls, 78 : long response_code, 79 : const void *response) 80 : { 81 6 : struct TALER_EXCHANGE_ManagementWireDisableHandle *wh = cls; 82 6 : const json_t *json = response; 83 6 : struct TALER_EXCHANGE_ManagementWireDisableResponse wdr = { 84 6 : .hr.http_status = (unsigned int) response_code, 85 : .hr.reply = json 86 : }; 87 : 88 6 : wh->job = NULL; 89 6 : switch (response_code) 90 : { 91 0 : case 0: 92 : /* no reply */ 93 0 : wdr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; 94 0 : wdr.hr.hint = "server offline?"; 95 0 : break; 96 2 : case MHD_HTTP_NO_CONTENT: 97 2 : break; 98 2 : case MHD_HTTP_FORBIDDEN: 99 2 : wdr.hr.ec = TALER_JSON_get_error_code (json); 100 2 : wdr.hr.hint = TALER_JSON_get_error_hint (json); 101 2 : break; 102 2 : case MHD_HTTP_NOT_FOUND: 103 2 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 104 : "Server did not find handler at `%s'. Did you configure the correct exchange base URL?\n", 105 : wh->url); 106 2 : if (NULL != json) 107 : { 108 2 : wdr.hr.ec = TALER_JSON_get_error_code (json); 109 2 : wdr.hr.hint = TALER_JSON_get_error_hint (json); 110 : } 111 : else 112 : { 113 0 : wdr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; 114 0 : wdr.hr.hint = TALER_ErrorCode_get_hint (wdr.hr.ec); 115 : } 116 2 : break; 117 0 : case MHD_HTTP_CONFLICT: 118 0 : wdr.hr.ec = TALER_JSON_get_error_code (json); 119 0 : wdr.hr.hint = TALER_JSON_get_error_hint (json); 120 0 : break; 121 0 : default: 122 : /* unexpected response code */ 123 0 : GNUNET_break_op (0); 124 0 : wdr.hr.ec = TALER_JSON_get_error_code (json); 125 0 : wdr.hr.hint = TALER_JSON_get_error_hint (json); 126 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 127 : "Unexpected response code %u/%d exchange management disable wire\n", 128 : (unsigned int) response_code, 129 : (int) wdr.hr.ec); 130 0 : break; 131 : } 132 6 : if (NULL != wh->cb) 133 : { 134 6 : wh->cb (wh->cb_cls, 135 : &wdr); 136 6 : wh->cb = NULL; 137 : } 138 6 : TALER_EXCHANGE_management_disable_wire_cancel (wh); 139 6 : } 140 : 141 : 142 : struct TALER_EXCHANGE_ManagementWireDisableHandle * 143 6 : TALER_EXCHANGE_management_disable_wire ( 144 : struct GNUNET_CURL_Context *ctx, 145 : const char *url, 146 : const struct TALER_FullPayto payto_uri, 147 : struct GNUNET_TIME_Timestamp validity_end, 148 : const struct TALER_MasterSignatureP *master_sig, 149 : TALER_EXCHANGE_ManagementWireDisableCallback cb, 150 : void *cb_cls) 151 : { 152 : struct TALER_EXCHANGE_ManagementWireDisableHandle *wh; 153 : CURL *eh; 154 : json_t *body; 155 : 156 6 : wh = GNUNET_new (struct TALER_EXCHANGE_ManagementWireDisableHandle); 157 6 : wh->cb = cb; 158 6 : wh->cb_cls = cb_cls; 159 6 : wh->ctx = ctx; 160 6 : wh->url = TALER_url_join (url, 161 : "management/wire/disable", 162 : NULL); 163 6 : if (NULL == wh->url) 164 : { 165 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 166 : "Could not construct request URL.\n"); 167 0 : GNUNET_free (wh); 168 0 : return NULL; 169 : } 170 6 : body = GNUNET_JSON_PACK ( 171 : TALER_JSON_pack_full_payto ("payto_uri", 172 : payto_uri), 173 : GNUNET_JSON_pack_data_auto ("master_sig_del", 174 : master_sig), 175 : GNUNET_JSON_pack_timestamp ("validity_end", 176 : validity_end)); 177 6 : eh = TALER_EXCHANGE_curl_easy_get_ (wh->url); 178 12 : if ( (NULL == eh) || 179 : (GNUNET_OK != 180 6 : TALER_curl_easy_post (&wh->post_ctx, 181 : eh, 182 : body)) ) 183 : { 184 0 : GNUNET_break (0); 185 0 : if (NULL != eh) 186 0 : curl_easy_cleanup (eh); 187 0 : json_decref (body); 188 0 : GNUNET_free (wh->url); 189 0 : GNUNET_free (wh); 190 0 : return NULL; 191 : } 192 6 : json_decref (body); 193 6 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 194 : "Requesting URL '%s'\n", 195 : wh->url); 196 12 : wh->job = GNUNET_CURL_job_add2 (ctx, 197 : eh, 198 6 : wh->post_ctx.headers, 199 : &handle_auditor_disable_finished, 200 : wh); 201 6 : if (NULL == wh->job) 202 : { 203 0 : TALER_EXCHANGE_management_disable_wire_cancel (wh); 204 0 : return NULL; 205 : } 206 6 : return wh; 207 : } 208 : 209 : 210 : void 211 6 : TALER_EXCHANGE_management_disable_wire_cancel ( 212 : struct TALER_EXCHANGE_ManagementWireDisableHandle *wh) 213 : { 214 6 : if (NULL != wh->job) 215 : { 216 0 : GNUNET_CURL_job_cancel (wh->job); 217 0 : wh->job = NULL; 218 : } 219 6 : TALER_curl_easy_post_finished (&wh->post_ctx); 220 6 : GNUNET_free (wh->url); 221 6 : GNUNET_free (wh); 222 6 : }