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_auditor_add_denomination.c
19 : * @brief functions for the auditor to add its signature for denomination at the exchange
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 "auditor_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_AuditorAddDenominationHandle
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_AuditorAddDenominationCallback 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 POST /auditor/$AUDITOR_PUB/$H_DENOM_PUB request.
71 : *
72 : * @param cls the `struct TALER_EXCHANGE_AuditorAddDenominationHandle *`
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 4028 : handle_auditor_add_denomination_finished (void *cls,
78 : long response_code,
79 : const void *response)
80 : {
81 4028 : struct TALER_EXCHANGE_AuditorAddDenominationHandle *ah = cls;
82 4028 : const json_t *json = response;
83 4028 : struct TALER_EXCHANGE_AuditorAddDenominationResponse adr = {
84 4028 : .hr.http_status = (unsigned int) response_code,
85 : .hr.reply = json
86 : };
87 :
88 4028 : ah->job = NULL;
89 4028 : switch (response_code)
90 : {
91 4028 : case MHD_HTTP_NO_CONTENT:
92 4028 : break;
93 0 : case MHD_HTTP_FORBIDDEN:
94 0 : adr.hr.ec = TALER_JSON_get_error_code (json);
95 0 : adr.hr.hint = TALER_JSON_get_error_hint (json);
96 0 : break;
97 0 : case MHD_HTTP_NOT_FOUND:
98 0 : adr.hr.ec = TALER_JSON_get_error_code (json);
99 0 : adr.hr.hint = TALER_JSON_get_error_hint (json);
100 0 : break;
101 0 : case MHD_HTTP_GONE:
102 0 : adr.hr.ec = TALER_JSON_get_error_code (json);
103 0 : adr.hr.hint = TALER_JSON_get_error_hint (json);
104 0 : break;
105 0 : case MHD_HTTP_PRECONDITION_FAILED:
106 0 : adr.hr.ec = TALER_JSON_get_error_code (json);
107 0 : adr.hr.hint = TALER_JSON_get_error_hint (json);
108 0 : break;
109 0 : default:
110 : /* unexpected response code */
111 0 : if (NULL != json)
112 : {
113 0 : adr.hr.ec = TALER_JSON_get_error_code (json);
114 0 : adr.hr.hint = TALER_JSON_get_error_hint (json);
115 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
116 : "Unexpected response code %u/%d for exchange auditor-add-denomination at URL `%s'\n",
117 : (unsigned int) response_code,
118 : (int) adr.hr.ec,
119 : ah->url);
120 : }
121 : else
122 : {
123 0 : adr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
124 0 : adr.hr.hint = NULL;
125 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
126 : "Unexpected HTTP response code %u (no JSON returned) at URL `%s'\n",
127 : (unsigned int) response_code,
128 : ah->url);
129 : }
130 0 : break;
131 : }
132 4028 : if (NULL != ah->cb)
133 : {
134 4028 : ah->cb (ah->cb_cls,
135 : &adr);
136 4028 : ah->cb = NULL;
137 : }
138 4028 : TALER_EXCHANGE_add_auditor_denomination_cancel (ah);
139 4028 : }
140 :
141 :
142 : struct TALER_EXCHANGE_AuditorAddDenominationHandle *
143 4028 : TALER_EXCHANGE_add_auditor_denomination (
144 : struct GNUNET_CURL_Context *ctx,
145 : const char *url,
146 : const struct TALER_DenominationHashP *h_denom_pub,
147 : const struct TALER_AuditorPublicKeyP *auditor_pub,
148 : const struct TALER_AuditorSignatureP *auditor_sig,
149 : TALER_EXCHANGE_AuditorAddDenominationCallback cb,
150 : void *cb_cls)
151 : {
152 : struct TALER_EXCHANGE_AuditorAddDenominationHandle *ah;
153 : CURL *eh;
154 : json_t *body;
155 :
156 4028 : ah = GNUNET_new (struct TALER_EXCHANGE_AuditorAddDenominationHandle);
157 4028 : ah->cb = cb;
158 4028 : ah->cb_cls = cb_cls;
159 4028 : ah->ctx = ctx;
160 : {
161 : char apub_str[sizeof (*auditor_pub) * 2];
162 : char denom_str[sizeof (*h_denom_pub) * 2];
163 : char arg_str[sizeof (apub_str) + sizeof (denom_str) + 32];
164 : char *end;
165 :
166 4028 : end = GNUNET_STRINGS_data_to_string (auditor_pub,
167 : sizeof (*auditor_pub),
168 : apub_str,
169 : sizeof (apub_str));
170 4028 : *end = '\0';
171 4028 : end = GNUNET_STRINGS_data_to_string (h_denom_pub,
172 : sizeof (*h_denom_pub),
173 : denom_str,
174 : sizeof (denom_str));
175 4028 : *end = '\0';
176 4028 : GNUNET_snprintf (arg_str,
177 : sizeof (arg_str),
178 : "auditors/%s/%s",
179 : apub_str,
180 : denom_str);
181 4028 : ah->url = TALER_url_join (url,
182 : arg_str,
183 : NULL);
184 : }
185 4028 : if (NULL == ah->url)
186 : {
187 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
188 : "Could not construct request URL.\n");
189 0 : GNUNET_free (ah);
190 0 : return NULL;
191 : }
192 4028 : body = GNUNET_JSON_PACK (
193 : GNUNET_JSON_pack_data_auto ("auditor_sig",
194 : auditor_sig));
195 4028 : eh = TALER_AUDITOR_curl_easy_get_ (ah->url);
196 8056 : if ( (NULL == eh) ||
197 : (GNUNET_OK !=
198 4028 : TALER_curl_easy_post (&ah->post_ctx,
199 : eh,
200 : body)) )
201 : {
202 0 : GNUNET_break (0);
203 0 : if (NULL != eh)
204 0 : curl_easy_cleanup (eh);
205 0 : json_decref (body);
206 0 : GNUNET_free (ah->url);
207 0 : return NULL;
208 : }
209 4028 : json_decref (body);
210 4028 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
211 : "Requesting URL '%s'\n",
212 : ah->url);
213 8056 : ah->job = GNUNET_CURL_job_add2 (ctx,
214 : eh,
215 4028 : ah->post_ctx.headers,
216 : &handle_auditor_add_denomination_finished,
217 : ah);
218 4028 : if (NULL == ah->job)
219 : {
220 0 : TALER_EXCHANGE_add_auditor_denomination_cancel (ah);
221 0 : return NULL;
222 : }
223 4028 : return ah;
224 : }
225 :
226 :
227 : void
228 4028 : TALER_EXCHANGE_add_auditor_denomination_cancel (
229 : struct TALER_EXCHANGE_AuditorAddDenominationHandle *ah)
230 : {
231 4028 : if (NULL != ah->job)
232 : {
233 0 : GNUNET_CURL_job_cancel (ah->job);
234 0 : ah->job = NULL;
235 : }
236 4028 : TALER_curl_easy_post_finished (&ah->post_ctx);
237 4028 : GNUNET_free (ah->url);
238 4028 : GNUNET_free (ah);
239 4028 : }
|