exchange_api_post-management-auditors-AUDITOR_PUB-disable.c (7698B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2015-2026 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_post-management-auditors-AUDITOR_PUB-disable.c 19 * @brief functions to disable an auditor 20 * @author Christian Grothoff 21 */ 22 #include "taler/platform.h" 23 #include "taler/taler_json_lib.h" 24 #include <gnunet/gnunet_curl_lib.h> 25 #include <microhttpd.h> 26 #include "taler/taler_exchange_service.h" 27 #include "taler/taler-exchange/post-management-auditors-AUDITOR_PUB-disable.h" 28 #include "exchange_api_curl_defaults.h" 29 #include "taler/taler_signatures.h" 30 #include "taler/taler_curl_lib.h" 31 32 33 /** 34 * @brief Handle for a POST /management/auditors/$AUDITOR_PUB/disable request. 35 */ 36 struct TALER_EXCHANGE_PostManagementAuditorsDisableHandle 37 { 38 39 /** 40 * The base URL for this request. 41 */ 42 char *base_url; 43 44 /** 45 * The full URL for this request, set during _start. 46 */ 47 char *url; 48 49 /** 50 * Minor context that holds body and headers. 51 */ 52 struct TALER_CURL_PostContext post_ctx; 53 54 /** 55 * Handle for the request. 56 */ 57 struct GNUNET_CURL_Job *job; 58 59 /** 60 * Function to call with the result. 61 */ 62 TALER_EXCHANGE_PostManagementAuditorsDisableCallback cb; 63 64 /** 65 * Closure for @a cb. 66 */ 67 TALER_EXCHANGE_POST_MANAGEMENT_AUDITORS_DISABLE_RESULT_CLOSURE *cb_cls; 68 69 /** 70 * Reference to the execution context. 71 */ 72 struct GNUNET_CURL_Context *ctx; 73 74 /** 75 * Public signing key of the auditor. 76 */ 77 struct TALER_AuditorPublicKeyP auditor_pub; 78 79 /** 80 * When was this decided? 81 */ 82 struct GNUNET_TIME_Timestamp validity_end; 83 84 /** 85 * Signature affirming the auditor disablement. 86 */ 87 struct TALER_MasterSignatureP master_sig; 88 89 }; 90 91 92 /** 93 * Function called when we're done processing the 94 * HTTP POST /management/auditors/$AUDITOR_PUB/disable request. 95 * 96 * @param cls the `struct TALER_EXCHANGE_PostManagementAuditorsDisableHandle` 97 * @param response_code HTTP response code, 0 on error 98 * @param response response body, NULL if not in JSON 99 */ 100 static void 101 handle_auditors_disable_finished (void *cls, 102 long response_code, 103 const void *response) 104 { 105 struct TALER_EXCHANGE_PostManagementAuditorsDisableHandle *pmadh = cls; 106 const json_t *json = response; 107 struct TALER_EXCHANGE_PostManagementAuditorsDisableResponse res = { 108 .hr.http_status = (unsigned int) response_code, 109 .hr.reply = json 110 }; 111 112 pmadh->job = NULL; 113 switch (response_code) 114 { 115 case MHD_HTTP_NO_CONTENT: 116 break; 117 case MHD_HTTP_FORBIDDEN: 118 res.hr.ec = TALER_JSON_get_error_code (json); 119 res.hr.hint = TALER_JSON_get_error_hint (json); 120 break; 121 case MHD_HTTP_NOT_FOUND: 122 res.hr.ec = TALER_JSON_get_error_code (json); 123 res.hr.hint = TALER_JSON_get_error_hint (json); 124 break; 125 case MHD_HTTP_CONFLICT: 126 res.hr.ec = TALER_JSON_get_error_code (json); 127 res.hr.hint = TALER_JSON_get_error_hint (json); 128 break; 129 default: 130 /* unexpected response code */ 131 GNUNET_break_op (0); 132 res.hr.ec = TALER_JSON_get_error_code (json); 133 res.hr.hint = TALER_JSON_get_error_hint (json); 134 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 135 "Unexpected response code %u/%d for exchange management auditor disable\n", 136 (unsigned int) response_code, 137 (int) res.hr.ec); 138 break; 139 } 140 if (NULL != pmadh->cb) 141 { 142 pmadh->cb (pmadh->cb_cls, 143 &res); 144 pmadh->cb = NULL; 145 } 146 TALER_EXCHANGE_post_management_auditors_disable_cancel (pmadh); 147 } 148 149 150 struct TALER_EXCHANGE_PostManagementAuditorsDisableHandle * 151 TALER_EXCHANGE_post_management_auditors_disable_create ( 152 struct GNUNET_CURL_Context *ctx, 153 const char *url, 154 const struct TALER_AuditorPublicKeyP *auditor_pub, 155 struct GNUNET_TIME_Timestamp validity_end, 156 const struct TALER_MasterSignatureP *master_sig) 157 { 158 struct TALER_EXCHANGE_PostManagementAuditorsDisableHandle *pmadh; 159 160 pmadh = GNUNET_new ( 161 struct TALER_EXCHANGE_PostManagementAuditorsDisableHandle); 162 pmadh->ctx = ctx; 163 pmadh->base_url = GNUNET_strdup (url); 164 pmadh->auditor_pub = *auditor_pub; 165 pmadh->validity_end = validity_end; 166 pmadh->master_sig = *master_sig; 167 return pmadh; 168 } 169 170 171 enum TALER_ErrorCode 172 TALER_EXCHANGE_post_management_auditors_disable_start ( 173 struct TALER_EXCHANGE_PostManagementAuditorsDisableHandle *pmadh, 174 TALER_EXCHANGE_PostManagementAuditorsDisableCallback cb, 175 TALER_EXCHANGE_POST_MANAGEMENT_AUDITORS_DISABLE_RESULT_CLOSURE *cb_cls) 176 { 177 CURL *eh; 178 json_t *body; 179 180 pmadh->cb = cb; 181 pmadh->cb_cls = cb_cls; 182 { 183 char epub_str[sizeof (pmadh->auditor_pub) * 2]; 184 char arg_str[sizeof (epub_str) + 64]; 185 char *end; 186 187 end = GNUNET_STRINGS_data_to_string (&pmadh->auditor_pub, 188 sizeof (pmadh->auditor_pub), 189 epub_str, 190 sizeof (epub_str)); 191 *end = '\0'; 192 GNUNET_snprintf (arg_str, 193 sizeof (arg_str), 194 "management/auditors/%s/disable", 195 epub_str); 196 pmadh->url = TALER_url_join (pmadh->base_url, 197 arg_str, 198 NULL); 199 } 200 if (NULL == pmadh->url) 201 { 202 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 203 "Could not construct request URL.\n"); 204 return TALER_EC_GENERIC_CONFIGURATION_INVALID; 205 } 206 body = GNUNET_JSON_PACK ( 207 GNUNET_JSON_pack_data_auto ("master_sig", 208 &pmadh->master_sig), 209 GNUNET_JSON_pack_timestamp ("validity_end", 210 pmadh->validity_end)); 211 eh = TALER_EXCHANGE_curl_easy_get_ (pmadh->url); 212 if ( (NULL == eh) || 213 (GNUNET_OK != 214 TALER_curl_easy_post (&pmadh->post_ctx, 215 eh, 216 body)) ) 217 { 218 GNUNET_break (0); 219 if (NULL != eh) 220 curl_easy_cleanup (eh); 221 json_decref (body); 222 GNUNET_free (pmadh->url); 223 pmadh->url = NULL; 224 return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE; 225 } 226 json_decref (body); 227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 228 "Requesting URL '%s'\n", 229 pmadh->url); 230 pmadh->job = GNUNET_CURL_job_add2 (pmadh->ctx, 231 eh, 232 pmadh->post_ctx.headers, 233 &handle_auditors_disable_finished, 234 pmadh); 235 if (NULL == pmadh->job) 236 { 237 TALER_curl_easy_post_finished (&pmadh->post_ctx); 238 GNUNET_free (pmadh->url); 239 pmadh->url = NULL; 240 return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE; 241 } 242 return TALER_EC_NONE; 243 } 244 245 246 void 247 TALER_EXCHANGE_post_management_auditors_disable_cancel ( 248 struct TALER_EXCHANGE_PostManagementAuditorsDisableHandle *pmadh) 249 { 250 if (NULL != pmadh->job) 251 { 252 GNUNET_CURL_job_cancel (pmadh->job); 253 pmadh->job = NULL; 254 } 255 TALER_curl_easy_post_finished (&pmadh->post_ctx); 256 GNUNET_free (pmadh->url); 257 GNUNET_free (pmadh->base_url); 258 GNUNET_free (pmadh); 259 } 260 261 262 /* end of exchange_api_post-management-auditors-AUDITOR_PUB-disable.c */