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