Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2014-2019, 2022 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU Affero 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 Affero General Public License for more details.
12 :
13 : You should have received a copy of the GNU Affero General Public License along with
14 : TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
15 : */
16 : /**
17 : * @file taler-exchange-httpd_link.c
18 : * @brief Handle /coins/$COIN_PUB/link requests
19 : * @author Florian Dold
20 : * @author Benedikt Mueller
21 : * @author Christian Grothoff
22 : */
23 : #include "platform.h"
24 : #include <gnunet/gnunet_util_lib.h>
25 : #include <jansson.h>
26 : #include <microhttpd.h>
27 : #include "taler_mhd_lib.h"
28 : #include "taler-exchange-httpd_mhd.h"
29 : #include "taler-exchange-httpd_link.h"
30 : #include "taler-exchange-httpd_responses.h"
31 :
32 :
33 : /**
34 : * Closure for #handle_link_data().
35 : */
36 : struct HTD_Context
37 : {
38 :
39 : /**
40 : * Public key of the coin for which we are running link.
41 : */
42 : struct TALER_CoinSpendPublicKeyP coin_pub;
43 :
44 : /**
45 : * Json array with transfer data we collect.
46 : */
47 : json_t *mlist;
48 :
49 : /**
50 : * Taler error code.
51 : */
52 : enum TALER_ErrorCode ec;
53 : };
54 :
55 :
56 : /**
57 : * Function called with the session hashes and transfer secret
58 : * information for a given coin. Gets the linkage data and
59 : * builds the reply for the client.
60 : *
61 : * @param cls closure, a `struct HTD_Context`
62 : * @param transfer_pub public transfer key for the session
63 : * @param ldl link data related to @a transfer_pub
64 : */
65 : static void
66 0 : handle_link_data (void *cls,
67 : const struct TALER_TransferPublicKeyP *transfer_pub,
68 : const struct TALER_EXCHANGEDB_LinkList *ldl)
69 : {
70 0 : struct HTD_Context *ctx = cls;
71 : json_t *list;
72 :
73 0 : if (NULL == ctx->mlist)
74 0 : return; /* we failed earlier */
75 0 : if (NULL == (list = json_array ()))
76 0 : goto fail;
77 :
78 0 : for (const struct TALER_EXCHANGEDB_LinkList *pos = ldl;
79 : NULL != pos;
80 0 : pos = pos->next)
81 : {
82 : json_t *obj;
83 :
84 0 : obj = GNUNET_JSON_PACK (
85 : TALER_JSON_pack_denom_pub ("denom_pub",
86 : &pos->denom_pub),
87 : TALER_JSON_pack_blinded_denom_sig ("ev_sig",
88 : &pos->ev_sig),
89 : GNUNET_JSON_pack_uint64 ("coin_idx",
90 : pos->coin_refresh_offset),
91 : TALER_JSON_pack_exchange_withdraw_values ("ewv",
92 : &pos->alg_values),
93 : GNUNET_JSON_pack_data_auto ("link_sig",
94 : &pos->orig_coin_link_sig),
95 : GNUNET_JSON_pack_allow_null (
96 : pos->have_nonce
97 : ? GNUNET_JSON_pack_data_auto ("cs_nonce",
98 : &pos->nonce)
99 : : GNUNET_JSON_pack_string ("cs_nonce",
100 : NULL)));
101 0 : if ( (NULL == obj) ||
102 : (0 !=
103 0 : json_array_append_new (list,
104 : obj)) )
105 : {
106 0 : json_decref (list);
107 0 : goto fail;
108 : }
109 : }
110 : {
111 : json_t *root;
112 :
113 0 : root = GNUNET_JSON_PACK (
114 : GNUNET_JSON_pack_array_steal ("new_coins",
115 : list),
116 : GNUNET_JSON_pack_data_auto ("transfer_pub",
117 : transfer_pub));
118 0 : if ( (NULL == root) ||
119 : (0 !=
120 0 : json_array_append_new (ctx->mlist,
121 : root)) )
122 0 : goto fail;
123 : }
124 0 : return;
125 0 : fail:
126 0 : ctx->ec = TALER_EC_GENERIC_JSON_ALLOCATION_FAILURE;
127 0 : json_decref (ctx->mlist);
128 0 : ctx->mlist = NULL;
129 : }
130 :
131 :
132 : /**
133 : * Execute a link operation. Returns the linkage information that will allow
134 : * the owner of a coin to follow the trail to the refreshed coin.
135 : *
136 : * If it returns a non-error code, the transaction logic MUST NOT queue a MHD
137 : * response. IF it returns an hard error, the transaction logic MUST queue a
138 : * MHD response and set @a mhd_ret. IF it returns the soft error code, the
139 : * function MAY be called again to retry and MUST not queue a MHD response.
140 : *
141 : * @param cls closure
142 : * @param connection MHD request which triggered the transaction
143 : * @param[out] mhd_ret set to MHD response status for @a connection,
144 : * if transaction failed (!)
145 : * @return transaction status
146 : */
147 : static enum GNUNET_DB_QueryStatus
148 0 : link_transaction (void *cls,
149 : struct MHD_Connection *connection,
150 : MHD_RESULT *mhd_ret)
151 : {
152 0 : struct HTD_Context *ctx = cls;
153 : enum GNUNET_DB_QueryStatus qs;
154 :
155 0 : qs = TEH_plugin->get_link_data (TEH_plugin->cls,
156 0 : &ctx->coin_pub,
157 : &handle_link_data,
158 : ctx);
159 0 : if (NULL == ctx->mlist)
160 : {
161 0 : *mhd_ret = TALER_MHD_reply_with_error (connection,
162 : MHD_HTTP_INTERNAL_SERVER_ERROR,
163 : ctx->ec,
164 : NULL);
165 0 : return GNUNET_DB_STATUS_HARD_ERROR;
166 : }
167 0 : if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
168 : {
169 0 : *mhd_ret = TALER_MHD_reply_with_error (connection,
170 : MHD_HTTP_NOT_FOUND,
171 : TALER_EC_EXCHANGE_LINK_COIN_UNKNOWN,
172 : NULL);
173 0 : return GNUNET_DB_STATUS_HARD_ERROR;
174 : }
175 0 : return qs;
176 : }
177 :
178 :
179 : MHD_RESULT
180 0 : TEH_handler_link (struct TEH_RequestContext *rc,
181 : const char *const args[2])
182 : {
183 : struct HTD_Context ctx;
184 : MHD_RESULT mhd_ret;
185 :
186 0 : memset (&ctx,
187 : 0,
188 : sizeof (ctx));
189 0 : if (GNUNET_OK !=
190 0 : GNUNET_STRINGS_string_to_data (args[0],
191 : strlen (args[0]),
192 : &ctx.coin_pub,
193 : sizeof (ctx.coin_pub)))
194 : {
195 0 : GNUNET_break_op (0);
196 0 : return TALER_MHD_reply_with_error (rc->connection,
197 : MHD_HTTP_BAD_REQUEST,
198 : TALER_EC_EXCHANGE_GENERIC_COINS_INVALID_COIN_PUB,
199 : args[0]);
200 : }
201 0 : ctx.mlist = json_array ();
202 0 : GNUNET_assert (NULL != ctx.mlist);
203 0 : if (GNUNET_OK !=
204 0 : TEH_DB_run_transaction (rc->connection,
205 : "run link",
206 : TEH_MT_REQUEST_OTHER,
207 : &mhd_ret,
208 : &link_transaction,
209 : &ctx))
210 : {
211 0 : if (NULL != ctx.mlist)
212 0 : json_decref (ctx.mlist);
213 0 : return mhd_ret;
214 : }
215 0 : mhd_ret = TALER_MHD_reply_json (rc->connection,
216 0 : ctx.mlist,
217 : MHD_HTTP_OK);
218 0 : json_decref (ctx.mlist);
219 0 : return mhd_ret;
220 : }
221 :
222 :
223 : /* end of taler-exchange-httpd_link.c */
|