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_auditor_enable.c
19 : * @brief functions to enable an auditor
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 : /**
34 : * @brief Handle for a POST /management/auditors request.
35 : */
36 : struct TALER_EXCHANGE_ManagementAuditorEnableHandle
37 : {
38 :
39 : /**
40 : * The url for this request.
41 : */
42 : char *url;
43 :
44 : /**
45 : * Minor context that holds body and headers.
46 : */
47 : struct TALER_CURL_PostContext post_ctx;
48 :
49 : /**
50 : * Handle for the request.
51 : */
52 : struct GNUNET_CURL_Job *job;
53 :
54 : /**
55 : * Function to call with the result.
56 : */
57 : TALER_EXCHANGE_ManagementAuditorEnableCallback cb;
58 :
59 : /**
60 : * Closure for @a cb.
61 : */
62 : void *cb_cls;
63 :
64 : /**
65 : * Reference to the execution context.
66 : */
67 : struct GNUNET_CURL_Context *ctx;
68 : };
69 :
70 :
71 : /**
72 : * Function called when we're done processing the
73 : * HTTP POST /management/auditors request.
74 : *
75 : * @param cls the `struct TALER_EXCHANGE_ManagementAuditorEnableHandle *`
76 : * @param response_code HTTP response code, 0 on error
77 : * @param response response body, NULL if not in JSON
78 : */
79 : static void
80 12 : handle_auditor_enable_finished (void *cls,
81 : long response_code,
82 : const void *response)
83 : {
84 12 : struct TALER_EXCHANGE_ManagementAuditorEnableHandle *ah = cls;
85 12 : const json_t *json = response;
86 12 : struct TALER_EXCHANGE_ManagementAuditorEnableResponse auditor_enable_response
87 : = {
88 12 : .hr.http_status = (unsigned int) response_code,
89 : .hr.reply = json
90 : };
91 :
92 12 : ah->job = NULL;
93 12 : switch (response_code)
94 : {
95 10 : case MHD_HTTP_NO_CONTENT:
96 10 : break;
97 2 : case MHD_HTTP_FORBIDDEN:
98 2 : auditor_enable_response.hr.ec = TALER_JSON_get_error_code (json);
99 2 : auditor_enable_response.hr.hint = TALER_JSON_get_error_hint (json);
100 2 : break;
101 0 : case MHD_HTTP_NOT_FOUND:
102 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
103 : "Server did not find handler at `%s'. Did you configure the correct exchange base URL?\n",
104 : ah->url);
105 0 : if (NULL != json)
106 : {
107 0 : auditor_enable_response.hr.ec = TALER_JSON_get_error_code (json);
108 0 : auditor_enable_response.hr.hint = TALER_JSON_get_error_hint (json);
109 : }
110 : else
111 : {
112 0 : auditor_enable_response.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
113 0 : auditor_enable_response.hr.hint = TALER_ErrorCode_get_hint (
114 : auditor_enable_response.hr.ec);
115 : }
116 0 : break;
117 0 : case MHD_HTTP_CONFLICT:
118 0 : auditor_enable_response.hr.ec = TALER_JSON_get_error_code (json);
119 0 : auditor_enable_response.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 : auditor_enable_response.hr.ec = TALER_JSON_get_error_code (json);
125 0 : auditor_enable_response.hr.hint = TALER_JSON_get_error_hint (json);
126 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
127 : "Unexpected response code %u/%d for exchange management auditor enable\n",
128 : (unsigned int) response_code,
129 : (int) auditor_enable_response.hr.ec);
130 0 : break;
131 : }
132 12 : if (NULL != ah->cb)
133 : {
134 12 : ah->cb (ah->cb_cls,
135 : &auditor_enable_response);
136 12 : ah->cb = NULL;
137 : }
138 12 : TALER_EXCHANGE_management_enable_auditor_cancel (ah);
139 12 : }
140 :
141 :
142 : struct TALER_EXCHANGE_ManagementAuditorEnableHandle *
143 12 : TALER_EXCHANGE_management_enable_auditor (
144 : struct GNUNET_CURL_Context *ctx,
145 : const char *url,
146 : const struct TALER_AuditorPublicKeyP *auditor_pub,
147 : const char *auditor_url,
148 : const char *auditor_name,
149 : struct GNUNET_TIME_Timestamp validity_start,
150 : const struct TALER_MasterSignatureP *master_sig,
151 : TALER_EXCHANGE_ManagementAuditorEnableCallback cb,
152 : void *cb_cls)
153 : {
154 : struct TALER_EXCHANGE_ManagementAuditorEnableHandle *ah;
155 : CURL *eh;
156 : json_t *body;
157 :
158 12 : ah = GNUNET_new (struct TALER_EXCHANGE_ManagementAuditorEnableHandle);
159 12 : ah->cb = cb;
160 12 : ah->cb_cls = cb_cls;
161 12 : ah->ctx = ctx;
162 12 : ah->url = TALER_url_join (url,
163 : "management/auditors",
164 : NULL);
165 12 : if (NULL == ah->url)
166 : {
167 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
168 : "Could not construct request URL.\n");
169 0 : GNUNET_free (ah);
170 0 : return NULL;
171 : }
172 12 : body = GNUNET_JSON_PACK (
173 : GNUNET_JSON_pack_string ("auditor_url",
174 : auditor_url),
175 : GNUNET_JSON_pack_string ("auditor_name",
176 : auditor_name),
177 : GNUNET_JSON_pack_data_auto ("auditor_pub",
178 : auditor_pub),
179 : GNUNET_JSON_pack_data_auto ("master_sig",
180 : master_sig),
181 : GNUNET_JSON_pack_timestamp ("validity_start",
182 : validity_start));
183 12 : eh = TALER_EXCHANGE_curl_easy_get_ (ah->url);
184 24 : if ( (NULL == eh) ||
185 : (GNUNET_OK !=
186 12 : TALER_curl_easy_post (&ah->post_ctx,
187 : eh,
188 : body)) )
189 : {
190 0 : GNUNET_break (0);
191 0 : json_decref (body);
192 0 : if (NULL != eh)
193 0 : curl_easy_cleanup (eh);
194 0 : GNUNET_free (ah->url);
195 0 : return NULL;
196 : }
197 12 : json_decref (body);
198 12 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
199 : "Requesting URL '%s'\n",
200 : ah->url);
201 24 : ah->job = GNUNET_CURL_job_add2 (ctx,
202 : eh,
203 12 : ah->post_ctx.headers,
204 : &handle_auditor_enable_finished,
205 : ah);
206 12 : if (NULL == ah->job)
207 : {
208 0 : TALER_EXCHANGE_management_enable_auditor_cancel (ah);
209 0 : return NULL;
210 : }
211 12 : return ah;
212 : }
213 :
214 :
215 : void
216 12 : TALER_EXCHANGE_management_enable_auditor_cancel (
217 : struct TALER_EXCHANGE_ManagementAuditorEnableHandle *ah)
218 : {
219 12 : if (NULL != ah->job)
220 : {
221 0 : GNUNET_CURL_job_cancel (ah->job);
222 0 : ah->job = NULL;
223 : }
224 12 : TALER_curl_easy_post_finished (&ah->post_ctx);
225 12 : GNUNET_free (ah->url);
226 12 : GNUNET_free (ah);
227 12 : }
|