aboutsummaryrefslogtreecommitdiff
path: root/src/mint/taler-mint-httpd.c
blob: 61d0bbdceb2e64dc7e216c2bcdb743b259674444 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
  This file is part of TALER
  (C) 2014 GNUnet e.V.

  TALER is free software; you can redistribute it and/or modify it under the
  terms of the GNU Affero General Public License as published by the Free Software
  Foundation; either version 3, or (at your option) any later version.

  TALER is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License along with
  TALER; see the file COPYING.  If not, If not, see <http://www.gnu.org/licenses/>
*/

/**
 * @file taler-mint-httpd.c
 * @brief Serve the HTTP interface of the mint
 * @author Florian Dold
 * @author Benedikt Mueller
 * @author Christian Grothoff
 */
#include "platform.h"
#include <gnunet/gnunet_util_lib.h>
#include <jansson.h>
#include <microhttpd.h>
#include <libpq-fe.h>
#include <pthread.h>
#include "mint.h"
#include "mint_db.h"
#include "taler_signatures.h"
#include "taler_rsa.h"
#include "taler_json_lib.h"
#include "taler-mint-httpd_parsing.h"
#include "taler-mint-httpd_mhd.h"
#include "taler-mint-httpd_keys.h"
#include "taler-mint-httpd_deposit.h"
#include "taler-mint-httpd_withdraw.h"
#include "taler-mint-httpd_refresh.h"


/**
 * Base directory of the mint (global)
 */
char *mintdir;

/**
 * The mint's configuration (global)
 */
struct GNUNET_CONFIGURATION_Handle *cfg;

/**
 * Master public key (according to the
 * configuration in the mint directory).
 */
struct GNUNET_CRYPTO_EddsaPublicKey master_pub;

/**
 * The HTTP Daemon.
 */
static struct MHD_Daemon *mydaemon;

/**
 * The kappa value for refreshing.
 */
static unsigned int refresh_security_parameter;

/**
 * Port to run the daemon on.
 */
static uint16_t serve_port;


/**
 * Convert a string representing an EdDSA signature to an EdDSA
 * signature.
 *
 * FIXME: this should be in GNUnet.
 * FIXME: why? this code is dead, even here!
 *
 * @param enc encoded EdDSA signature
 * @param enclen number of bytes in @a enc (without 0-terminator)
 * @param pub where to store the EdDSA signature
 * @return #GNUNET_OK on success
 */
int
TALER_eddsa_signature_from_string (const char *enc,
                                   size_t enclen,
                                   struct GNUNET_CRYPTO_EddsaSignature *sig)
{
  size_t keylen = (sizeof (struct GNUNET_CRYPTO_EddsaSignature)) * 8;

  if (keylen % 5 > 0)
    keylen += 5 - keylen % 5;
  keylen /= 5;
  if (enclen != keylen)
    return GNUNET_SYSERR;

  if (GNUNET_OK != GNUNET_STRINGS_string_to_data (enc, enclen,
						  sig,
						  sizeof (struct GNUNET_CRYPTO_EddsaSignature)))
    return GNUNET_SYSERR;
  return GNUNET_OK;
}


/**
 * Handle a request coming from libmicrohttpd.
 *
 * @param cls closure for MHD daemon (unused)
 * @param connection the connection
 * @param url the requested url
 * @param method the method (POST, GET, ...)
 * @param upload_data request data
 * @param upload_data_size size of @a upload_data in bytes
 * @param con_cls closure for request (a `struct Buffer *`)
 * @return MHD result code
 */
static int
handle_mhd_request (void *cls,
                    struct MHD_Connection *connection,
                    const char *url,
                    const char *method,
                    const char *version,
                    const char *upload_data,
                    size_t *upload_data_size,
                    void **con_cls)
{
  static struct RequestHandler handlers[] =
    {
      { "/", MHD_HTTP_METHOD_GET, "text/plain",
        "Hello, I'm the mint\n", 0,
        &TALER_MINT_handler_static_response, MHD_HTTP_OK },
      { "/agpl", MHD_HTTP_METHOD_GET, "text/plain",
        NULL, 0,
        &TALER_MINT_handler_agpl_redirect, MHD_HTTP_FOUND },
      { "/keys", MHD_HTTP_METHOD_GET, "application/json",
        NULL, 0,
        &TALER_MINT_handler_keys, MHD_HTTP_OK },
      { "/keys", NULL, "text/plain",
        "Only GET is allowed", 0,
        &TALER_MINT_handler_send_json_pack_error, MHD_HTTP_METHOD_NOT_ALLOWED },
      { "/withdraw/status", MHD_HTTP_METHOD_GET, "application/json",
        NULL, 0,
        &TALER_MINT_handler_withdraw_status, MHD_HTTP_OK },
      { "/withdraw/status", NULL, "text/plain",
        "Only GET is allowed", 0,
        &TALER_MINT_handler_send_json_pack_error, MHD_HTTP_METHOD_NOT_ALLOWED },
      { "/withdraw/sign", MHD_HTTP_METHOD_GET, "application/json",
        NULL, 0,
        &TALER_MINT_handler_withdraw_sign, MHD_HTTP_OK },
      { "/withdraw/sign", NULL, "text/plain",
        "Only GET is allowed", 0,
        &TALER_MINT_handler_send_json_pack_error, MHD_HTTP_METHOD_NOT_ALLOWED },
      { "/refresh/melt", MHD_HTTP_METHOD_POST, "application/json",
        NULL, 0,
        &TALER_MINT_handler_refresh_melt, MHD_HTTP_OK },
      { "/refresh/melt", NULL, "text/plain",
        "Only POST is allowed", 0,
        &TALER_MINT_handler_send_json_pack_error, MHD_HTTP_METHOD_NOT_ALLOWED },
      { "/refresh/commit", MHD_HTTP_METHOD_POST, "application/json",
        NULL, 0,
        &TALER_MINT_handler_refresh_commit, MHD_HTTP_OK },
      { "/refresh/commit", NULL, "text/plain",
        "Only POST is allowed", 0,
        &TALER_MINT_handler_send_json_pack_error, MHD_HTTP_METHOD_NOT_ALLOWED },
      { "/refresh/reveal", MHD_HTTP_METHOD_POST, "application/json",
        NULL, 0,
        &TALER_MINT_handler_refresh_melt, MHD_HTTP_OK },
      { "/refresh/reveal", NULL, "text/plain",
        "Only POST is allowed", 0,
        &TALER_MINT_handler_send_json_pack_error, MHD_HTTP_METHOD_NOT_ALLOWED },
      { "/refresh/link", MHD_HTTP_METHOD_GET, "application/json",
        NULL, 0,
        &TALER_MINT_handler_refresh_link, MHD_HTTP_OK },
      { "/refresh/link", NULL, "text/plain",
        "Only GET is allowed", 0,
        &TALER_MINT_handler_send_json_pack_error, MHD_HTTP_METHOD_NOT_ALLOWED },
      { "/refresh/reveal", MHD_HTTP_METHOD_GET, "application/json",
        NULL, 0,
        &TALER_MINT_handler_refresh_reveal, MHD_HTTP_OK },
      { "/refresh/reveal", NULL, "text/plain",
        "Only GET is allowed", 0,
        &TALER_MINT_handler_send_json_pack_error, MHD_HTTP_METHOD_NOT_ALLOWED },
      { "/deposit", MHD_HTTP_METHOD_POST, "application/json",
        NULL, 0,
        &TALER_MINT_handler_deposit, MHD_HTTP_OK },
      { "/deposit", NULL, "text/plain",
        "Only POST is allowed", 0,
        &TALER_MINT_handler_send_json_pack_error, MHD_HTTP_METHOD_NOT_ALLOWED },
      { NULL, NULL, NULL, NULL, 0, 0 }
    };
  static struct RequestHandler h404 =
    {
      "", NULL, "text/html",
      "<html><title>404: not found</title></html>", 0,
      &TALER_MINT_handler_static_response, MHD_HTTP_NOT_FOUND
    };
  struct RequestHandler *rh;
  unsigned int i;

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Handling request for URL '%s'\n",
              url);
  for (i=0;NULL != handlers[i].url;i++)
  {
    rh = &handlers[i];
    if ( (0 == strcasecmp (url,
                           rh->url)) &&
         ( (NULL == rh->method) ||
           (0 == strcasecmp (method,
                             rh->method)) ) )
      return rh->handler (rh,
                          connection,
                          con_cls,
                          upload_data,
                          upload_data_size);
  }
  return TALER_MINT_handler_static_response (&h404,
                                             connection,
                                             con_cls,
                                             upload_data,
                                             upload_data_size);
}



/**
 * Load configuration parameters for the mint
 * server into the corresponding global variables.
 *
 * @param param mint_directory the mint's directory
 * @return GNUNET_OK on success
 */
static int
mint_serve_process_config (const char *mint_directory)
{
  unsigned long long port;
  unsigned long long kappa;
  char *master_pub_str;
  char *db_cfg;

  cfg = TALER_MINT_config_load (mint_directory);
  if (NULL == cfg)
  {
    fprintf (stderr,
             "can't load mint configuration\n");
    return 1;
  }
  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_string (cfg,
                                             "mint", "master_pub",
                                             &master_pub_str))
  {
    fprintf (stderr,
             "No master public key given in mint configuration.");
    return GNUNET_NO;
  }
  if (GNUNET_OK !=
      GNUNET_CRYPTO_eddsa_public_key_from_string (master_pub_str,
                                                  strlen (master_pub_str),
                                                  &master_pub))
  {
    fprintf (stderr,
             "Invalid master public key given in mint configuration.");
    return GNUNET_NO;
  }

  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_string (cfg,
                                             "mint", "db",
                                             &db_cfg))
  {
    fprintf (stderr,
             "invalid configuration: mint.db\n");
    return GNUNET_NO;
  }
  if (GNUNET_OK !=
      TALER_MINT_DB_init (db_cfg))
  {
    fprintf (stderr,
             "failed to initialize DB subsystem\n");
    return GNUNET_NO;
  }

  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_number (cfg,
                                             "mint", "port",
                                             &port))
  {
    fprintf (stderr,
             "invalid configuration: mint.port\n");
    return GNUNET_NO;
  }

  if ((port == 0) || (port > UINT16_MAX))
  {
    fprintf (stderr,
             "invalid configuration (value out of range): mint.port\n");
    return GNUNET_NO;
  }
  serve_port = port;

  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_number (cfg,
                                             "mint", "refresh_security_parameter",
                                             &kappa))
  {
    fprintf (stderr,
             "invalid configuration: mint.refresh_security_parameter\n");
    return GNUNET_NO;
  }
  refresh_security_parameter = kappa;

  return GNUNET_OK;
}


/**
 * The main function of the serve tool
 *
 * @param argc number of arguments from the command line
 * @param argv command line arguments
 * @return 0 ok, 1 on error
 */
int
main (int argc, char *const *argv)
{
  static const struct GNUNET_GETOPT_CommandLineOption options[] = {
    GNUNET_GETOPT_OPTION_HELP ("gnunet-mint-keyup OPTIONS"),
    {'d', "mint-dir", "DIR",
     "mint directory", 1,
     &GNUNET_GETOPT_set_filename, &mintdir},
    GNUNET_GETOPT_OPTION_END
  };
  int ret;

  GNUNET_assert (GNUNET_OK ==
                 GNUNET_log_setup ("taler-mint-serve",
                                   "INFO",
                                   NULL));
  if (GNUNET_GETOPT_run ("taler-mint-serve",
                         options,
                         argc, argv) < 0)
    return 1;
  if (NULL == mintdir)
  {
    fprintf (stderr,
             "no mint dir given\n");
    return 1;
  }

  if (GNUNET_OK != mint_serve_process_config (mintdir))
    return 1;


  mydaemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
                               serve_port,
                               NULL, NULL,
                               &handle_mhd_request, NULL,
                               MHD_OPTION_END);

  if (NULL == mydaemon)
  {
    fprintf (stderr,
             "Failed to start MHD.\n");
    return 1;
  }

  ret = TALER_MINT_key_reload_loop ();
  MHD_stop_daemon (mydaemon);
  return (GNUNET_OK == ret) ? 0 : 1;
}