taler-merchant-httpd.c (57245B)
1 /* 2 This file is part of TALER 3 (C) 2014-2025 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 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 <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file src/backend/taler-merchant-httpd.c 18 * @brief HTTP serving layer intended to perform crypto-work and 19 * communication with the exchange 20 * @author Marcello Stanisci 21 * @author Christian Grothoff 22 * @author Florian Dold 23 * @author Priscilla HUANG 24 */ 25 #include "platform.h" 26 #include <taler/taler_dbevents.h> 27 #include <taler/taler_bank_service.h> 28 #include <taler/taler_mhd_lib.h> 29 #include <taler/taler_templating_lib.h> 30 #include <taler/taler_exchange_service.h> 31 #include <donau/donau_service.h> 32 #include "taler/taler_merchant_util.h" 33 #include "taler-merchant-httpd_auth.h" 34 #include "taler-merchant-httpd_dispatcher.h" 35 #include "taler-merchant-httpd_exchanges.h" 36 #include "taler-merchant-httpd_helper.h" 37 #include "taler-merchant-httpd_mhd.h" 38 #include "taler-merchant-httpd_mfa.h" 39 #include "taler-merchant-httpd_post-private-orders.h" 40 #include "taler-merchant-httpd_post-orders-ORDER_ID-abort.h" 41 #include "taler-merchant-httpd_post-challenge-ID.h" 42 #include "taler-merchant-httpd_get-orders-ORDER_ID.h" 43 #include "taler-merchant-httpd_get-sessions-SESSION_ID.h" 44 #include "taler-merchant-httpd_get-templates-TEMPLATE_ID.h" 45 #include "taler-merchant-httpd_get-exchanges.h" 46 #include "taler-merchant-httpd_get-webui.h" 47 #include "taler-merchant-httpd_get-terms.h" 48 #include "taler-merchant-httpd_get-private-kyc.h" 49 #include "taler-merchant-httpd_get-private-statistics-report-transactions.h" 50 #include "taler-merchant-httpd_post-private-donau.h" 51 #include "taler-merchant-httpd_get-private-orders-ORDER_ID.h" 52 #include "taler-merchant-httpd_get-private-orders.h" 53 #include "taler-merchant-httpd_post-orders-ORDER_ID-pay.h" 54 #include "taler-merchant-httpd_post-orders-ORDER_ID-refund.h" 55 #include "taler-merchant-httpd_post-private-accounts-H_WIRE-kycauth.h" 56 #include "merchant-database/iterate_instances.h" 57 #include "merchant-database/set_instance.h" 58 #include "merchant-database/iterate_accounts_by_instance.h" 59 #include "merchant-database/event_listen.h" 60 #include "merchant-database/preflight.h" 61 #include "merchant-database/event_notify.h" 62 63 /** 64 * Backlog for listen operation on unix-domain sockets. 65 */ 66 #define UNIX_BACKLOG 500 67 68 /** 69 * Maximum number of concurrent connections per HTTP daemon. 70 * TALER_MHD_daemon_start() keeps our descriptors below FD_SETSIZE 71 * (1024). 300 connections is plenty, same as exchange. At the 72 * limit, MHD stops accepting, so that other taler-merchant-httpd 73 * processes sharing the listen socket (as high-performance setups do 74 * via systemd) take the connection. 75 */ 76 #define CONNECTION_LIMIT 300 77 78 79 /** 80 * Default maximum upload size permitted. Can be overridden 81 * per handler. 82 */ 83 #define DEFAULT_MAX_UPLOAD_SIZE (16 * 1024) 84 85 char *TMH_currency; 86 87 char *TMH_base_url; 88 89 char *TMH_spa_dir; 90 91 char *TMH_helper_email; 92 93 char *TMH_helper_sms; 94 95 char *TMH_phone_regex; 96 97 regex_t TMH_phone_rx; 98 99 char *TMH_allowed_payment_targets; 100 101 char *TMH_default_persona; 102 103 char *TMH_payment_target_regex; 104 105 regex_t TMH_payment_target_re; 106 107 int TMH_force_audit; 108 109 struct TALER_MERCHANTDB_PostgresContext *TMH_db; 110 111 struct GNUNET_CONTAINER_MultiHashMap *TMH_by_id_map; 112 113 struct GNUNET_TIME_Relative TMH_default_pay_delay; 114 115 struct GNUNET_TIME_Relative TMH_default_refund_delay; 116 117 struct GNUNET_TIME_Relative TMH_default_wire_transfer_delay; 118 119 enum GNUNET_TIME_RounderInterval TMH_default_wire_transfer_rounding_interval; 120 121 int TMH_strict_v19; 122 123 int TMH_auth_disabled; 124 125 int TMH_have_self_provisioning; 126 127 int TMH_password_change_mfa; 128 129 enum TEH_TanChannelSet TEH_mandatory_tan_channels; 130 131 struct GNUNET_TIME_Relative TMH_legal_expiration; 132 133 unsigned int TMH_num_cspecs; 134 135 json_t *TMH_global_spa_config_data; 136 137 struct TALER_CurrencySpecification *TMH_cspecs; 138 139 struct GNUNET_CURL_Context *TMH_curl_ctx; 140 141 /** 142 * Event handler for instance settings changes. 143 */ 144 static struct GNUNET_DB_EventHandler *instance_eh; 145 146 /** 147 * True if we started any HTTP daemon. 148 */ 149 static bool have_daemons; 150 151 /** 152 * Should a "Connection: close" header be added to each HTTP response? 153 */ 154 static int merchant_connection_close; 155 156 /** 157 * Should we enable HTTP/2 and HTTP/3 when talking to the exchange 158 * (and donau)? Those are not expected to be terribly beneficial for 159 * a server with stable connections to an exchange, but they could 160 * cause stability issues with libcurl. Per default, we *enforce* 161 * HTTP/1.x-only, as that is the conservative and most tested code 162 * path. 163 */ 164 static int enable_h3; 165 166 /** 167 * Context for integrating #TMH_curl_ctx with the 168 * GNUnet event loop. 169 */ 170 static struct GNUNET_CURL_RescheduleContext *merchant_curl_rc; 171 172 /** 173 * Global return code 174 */ 175 static int global_ret; 176 177 /** 178 * Our configuration. 179 */ 180 const struct GNUNET_CONFIGURATION_Handle *TMH_cfg; 181 182 183 void 184 TMH_wire_method_free (struct TMH_WireMethod *wm) 185 { 186 GNUNET_free (wm->payto_uri.full_payto); 187 GNUNET_free (wm->wire_method); 188 GNUNET_free (wm->extra_wire_subject_metadata); 189 GNUNET_free (wm->credit_facade_url); 190 json_decref (wm->credit_facade_credentials); 191 GNUNET_free (wm); 192 } 193 194 195 void 196 TMH_instance_decref (struct TMH_MerchantInstance *mi) 197 { 198 struct TMH_WireMethod *wm; 199 200 mi->rc--; 201 if (0 != mi->rc) 202 return; 203 TMH_force_get_orders_resume (mi); 204 while (NULL != (wm = (mi->wm_head))) 205 { 206 GNUNET_CONTAINER_DLL_remove (mi->wm_head, 207 mi->wm_tail, 208 wm); 209 TMH_wire_method_free (wm); 210 } 211 212 GNUNET_free (mi->settings.id); 213 GNUNET_free (mi->settings.name); 214 GNUNET_free (mi->settings.email); 215 GNUNET_free (mi->settings.phone); 216 GNUNET_free (mi->settings.website); 217 GNUNET_free (mi->settings.logo); 218 json_decref (mi->settings.address); 219 json_decref (mi->settings.jurisdiction); 220 GNUNET_free (mi); 221 } 222 223 224 enum GNUNET_GenericReturnValue 225 TMH_instance_free_cb (void *cls, 226 const struct GNUNET_HashCode *key, 227 void *value) 228 { 229 struct TMH_MerchantInstance *mi = value; 230 231 (void) cls; 232 (void) key; 233 TMH_force_get_orders_resume (mi); 234 GNUNET_assert (GNUNET_OK == 235 GNUNET_CONTAINER_multihashmap_remove (TMH_by_id_map, 236 &mi->h_instance, 237 mi)); 238 TMH_instance_decref (mi); 239 return GNUNET_YES; 240 } 241 242 243 /** 244 * Shutdown task (invoked when the application is being 245 * terminated for any reason) 246 * 247 * @param cls NULL 248 */ 249 static void 250 do_shutdown (void *cls) 251 { 252 (void) cls; 253 TALER_MHD_daemons_halt (); 254 TMH_handler_statistic_report_transactions_cleanup (); 255 TMH_force_kac_resume (); 256 TMH_force_orders_resume (); 257 TMH_force_get_sessions_ID_resume (); 258 TMH_force_get_templates_ID_resume (); 259 TMH_force_get_orders_resume_typst (); 260 TMH_force_ac_resume (); 261 TMH_force_pc_resume (); 262 TMH_force_kyc_resume (); 263 TMH_force_gorc_resume (); 264 TMH_force_wallet_get_order_resume (); 265 TMH_force_wallet_refund_order_resume (); 266 TMH_force_post_donau_resume (); 267 TMH_challenge_done (); 268 if (NULL != instance_eh) 269 { 270 TALER_MERCHANTDB_event_listen_cancel (instance_eh); 271 instance_eh = NULL; 272 } 273 if (NULL != TMH_by_id_map) 274 { 275 GNUNET_CONTAINER_multihashmap_iterate (TMH_by_id_map, 276 &TMH_instance_free_cb, 277 NULL); 278 GNUNET_CONTAINER_multihashmap_destroy (TMH_by_id_map); 279 TMH_by_id_map = NULL; 280 } 281 TALER_MHD_daemons_destroy (); 282 TMH_EXCHANGES_done (); 283 if (NULL != TMH_db) 284 { 285 TALER_MERCHANTDB_disconnect (TMH_db); 286 TMH_db = NULL; 287 } 288 TALER_TEMPLATING_done (); 289 if (NULL != TMH_curl_ctx) 290 { 291 GNUNET_CURL_fini (TMH_curl_ctx); 292 TMH_curl_ctx = NULL; 293 } 294 if (NULL != merchant_curl_rc) 295 { 296 GNUNET_CURL_gnunet_rc_destroy (merchant_curl_rc); 297 merchant_curl_rc = NULL; 298 } 299 if (NULL != TMH_payment_target_regex) 300 { 301 regfree (&TMH_payment_target_re); 302 GNUNET_free (TMH_payment_target_regex); 303 } 304 } 305 306 307 /** 308 * Function called whenever MHD is done with a request. If the 309 * request was a POST, we may have stored a `struct Buffer *` in the 310 * @a con_cls that might still need to be cleaned up. Call the 311 * respective function to free the memory. 312 * 313 * @param cls client-defined closure 314 * @param connection connection handle 315 * @param con_cls value as set by the last call to 316 * the #MHD_AccessHandlerCallback 317 * @param toe reason for request termination 318 * @see #MHD_OPTION_NOTIFY_COMPLETED 319 * @ingroup request 320 */ 321 static void 322 handle_mhd_completion_callback (void *cls, 323 struct MHD_Connection *connection, 324 void **con_cls, 325 enum MHD_RequestTerminationCode toe) 326 { 327 struct TMH_HandlerContext *hc = *con_cls; 328 329 (void) cls; 330 if (NULL == hc) 331 return; 332 GNUNET_SCHEDULER_begin_async_scope (&hc->async_scope_id); 333 { 334 #if MHD_VERSION >= 0x00097304 335 const union MHD_ConnectionInfo *ci; 336 unsigned int http_status = 0; 337 338 ci = MHD_get_connection_info (connection, 339 MHD_CONNECTION_INFO_HTTP_STATUS); 340 if (NULL != ci) 341 http_status = ci->http_status; 342 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 343 "Request for `%s' completed with HTTP status %u (%d)\n", 344 hc->url, 345 http_status, 346 toe); 347 #else 348 (void) connection; 349 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 350 "Finished handling request for `%s' with MHD termination code %d\n", 351 hc->url, 352 (int) toe); 353 #endif 354 } 355 if (NULL != hc->cc) 356 hc->cc (hc->ctx); 357 TALER_MHD_parse_post_cleanup_callback (hc->json_parse_context); 358 GNUNET_free (hc->infix); 359 GNUNET_free (hc->rewritten_url); 360 if (NULL != hc->request_body) 361 json_decref (hc->request_body); 362 if (NULL != hc->instance) 363 TMH_instance_decref (hc->instance); 364 TALER_MERCHANTDB_preflight (TMH_db); 365 GNUNET_free (hc->full_url); 366 GNUNET_free (hc); 367 *con_cls = NULL; 368 } 369 370 371 struct TMH_MerchantInstance * 372 TMH_lookup_instance (const char *instance_id) 373 { 374 struct GNUNET_HashCode h_instance; 375 char *id; 376 377 if (NULL == instance_id) 378 id = GNUNET_strdup ("admin"); 379 else 380 id = GNUNET_STRINGS_utf8_tolower (instance_id); 381 GNUNET_CRYPTO_hash (id, 382 strlen (id), 383 &h_instance); 384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 385 "Looking for by-id key %s of '%s' in hashmap\n", 386 GNUNET_h2s (&h_instance), 387 id); 388 GNUNET_free (id); 389 /* We're fine if that returns NULL, the calling routine knows how 390 to handle that */ 391 return GNUNET_CONTAINER_multihashmap_get (TMH_by_id_map, 392 &h_instance); 393 } 394 395 396 /** 397 * Add instance definition to our active set of instances. 398 * 399 * @param[in,out] mi merchant instance details to define 400 * @return #GNUNET_OK on success, #GNUNET_NO if the same ID is in use already 401 */ 402 enum GNUNET_GenericReturnValue 403 TMH_add_instance (struct TMH_MerchantInstance *mi) 404 { 405 const char *id; 406 enum GNUNET_GenericReturnValue ret; 407 408 id = mi->settings.id; 409 if (NULL == id) 410 id = "admin"; 411 GNUNET_CRYPTO_hash (id, 412 strlen (id), 413 &mi->h_instance); 414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 415 "Looking for by-id key %s of `%s' in hashmap\n", 416 GNUNET_h2s (&mi->h_instance), 417 id); 418 ret = GNUNET_CONTAINER_multihashmap_put (TMH_by_id_map, 419 &mi->h_instance, 420 mi, 421 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); 422 if (GNUNET_OK == ret) 423 { 424 GNUNET_assert (mi->rc < UINT_MAX); 425 mi->rc++; 426 } 427 return ret; 428 } 429 430 431 /** 432 * Function called first by MHD with the full URL. 433 * 434 * @param cls NULL 435 * @param full_url the full URL 436 * @param con MHD connection object 437 * @return our handler context 438 */ 439 static void * 440 full_url_track_callback (void *cls, 441 const char *full_url, 442 struct MHD_Connection *con) 443 { 444 struct TMH_HandlerContext *hc; 445 446 hc = GNUNET_new (struct TMH_HandlerContext); 447 hc->connection = con; 448 GNUNET_async_scope_fresh (&hc->async_scope_id); 449 GNUNET_SCHEDULER_begin_async_scope (&hc->async_scope_id); 450 hc->full_url = GNUNET_strdup (full_url); 451 return hc; 452 } 453 454 455 /** 456 * The callback was called again by MHD, continue processing 457 * the request with the already identified handler. 458 * 459 * @param hc the handler context 460 * @param upload_data the data being uploaded (excluding HEADERS, 461 * for a POST that fits into memory and that is encoded 462 * with a supported encoding, the POST data will NOT be 463 * given in upload_data and is instead available as 464 * part of #MHD_get_connection_values; very large POST 465 * data *will* be made available incrementally in 466 * @a upload_data) 467 * @param upload_data_size set initially to the size of the 468 * @a upload_data provided; the method must update this 469 * value to the number of bytes NOT processed; 470 * @return #MHD_YES if the connection was handled successfully, 471 * #MHD_NO if the socket must be closed due to a serious 472 * error while handling the request 473 */ 474 static enum MHD_Result 475 process_upload_with_handler (struct TMH_HandlerContext *hc, 476 const char *upload_data, 477 size_t *upload_data_size) 478 { 479 GNUNET_assert (NULL != hc->rh); 480 GNUNET_SCHEDULER_begin_async_scope (&hc->async_scope_id); 481 if ( (hc->has_body) && 482 (NULL == hc->request_body) ) 483 { 484 size_t mul = hc->rh->max_upload; 485 enum GNUNET_GenericReturnValue res; 486 487 if (0 == mul) 488 mul = DEFAULT_MAX_UPLOAD_SIZE; 489 if ( (hc->total_upload + *upload_data_size < hc->total_upload) || 490 (hc->total_upload + *upload_data_size > mul) ) 491 { 492 /* Client exceeds upload limit. Should _usually_ be checked earlier 493 when we look at the MHD_HTTP_HEADER_CONTENT_LENGTH, alas with 494 chunked encoding an uploader MAY have omitted this, and thus 495 not permitted us to check on time. In this case, we just close 496 the connection once it exceeds our limit (instead of waiting 497 for the upload to complete and then fail). This could theoretically 498 cause some clients to retry, alas broken or malicious clients 499 are likely to retry anyway, so little we can do about it, and 500 failing earlier seems the best option here. */ 501 GNUNET_break_op (0); 502 return MHD_NO; 503 } 504 hc->total_upload += *upload_data_size; 505 res = TALER_MHD_parse_post_json (hc->connection, 506 &hc->json_parse_context, 507 upload_data, 508 upload_data_size, 509 &hc->request_body); 510 if (GNUNET_SYSERR == res) 511 return MHD_NO; 512 /* A error response was already generated */ 513 if ( (GNUNET_NO == res) || 514 /* or, need more data to accomplish parsing */ 515 (NULL == hc->request_body) ) 516 return MHD_YES; /* let MHD call us *again* */ 517 } 518 /* Upload complete (if any), call handler to generate reply */ 519 return hc->rh->handler (hc->rh, 520 hc->connection, 521 hc); 522 } 523 524 525 /** 526 * Log information about the request being handled. 527 * 528 * @param hc handler context 529 * @param method HTTP method of the request 530 */ 531 static void 532 log_request (const struct TMH_HandlerContext *hc, 533 const char *method) 534 { 535 const char *correlation_id; 536 537 correlation_id = MHD_lookup_connection_value (hc->connection, 538 MHD_HEADER_KIND, 539 "Taler-Correlation-Id"); 540 if ( (NULL != correlation_id) && 541 (GNUNET_YES != 542 GNUNET_CURL_is_valid_scope_id (correlation_id)) ) 543 { 544 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 545 "Illegal incoming correlation ID\n"); 546 correlation_id = NULL; 547 } 548 if (NULL != correlation_id) 549 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 550 "Handling request for (%s) URL '%s', correlation_id=%s\n", 551 method, 552 hc->url, 553 correlation_id); 554 else 555 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 556 "Handling request (%s) for URL '%s'\n", 557 method, 558 hc->url); 559 } 560 561 562 /** 563 * Identify the instance of the request from the URL. 564 * 565 * @param[in,out] hc handler context 566 * @param[in,out] urlp URL path of the request, updated to point to the rest 567 * @param[out] use_admin set to true if we are using the admin instance 568 * @return #GNUNET_OK on success, 569 * #GNUNET_NO if an error was queued (return #MHD_YES) 570 * #GNUNET_SYSERR to close the connection (return #MHD_NO) 571 */ 572 static enum GNUNET_GenericReturnValue 573 identify_instance (struct TMH_HandlerContext *hc, 574 const char **urlp, 575 bool *use_admin) 576 { 577 const char *url = *urlp; 578 const char *instance_prefix = "/instances/"; 579 580 if (0 == strncmp (url, 581 instance_prefix, 582 strlen (instance_prefix))) 583 { 584 /* url starts with "/instances/" */ 585 const char *istart = url + strlen (instance_prefix); 586 const char *slash = strchr (istart, '/'); 587 char *raw_id; 588 char *instance_id; 589 590 if (NULL == slash) 591 raw_id = GNUNET_strdup (istart); 592 else 593 raw_id = GNUNET_strndup (istart, 594 slash - istart); 595 /* Instance IDs are case-insensitive, so fold the segment before comparing 596 it against "admin" below. Without this, '/instances/Admin/' misses the 597 redirect to the modern path, leaving use_admin false, and every 598 'default_only' handler that '/instances/admin/' reaches (all of 599 /management/) then replies 404. */ 600 instance_id = GNUNET_STRINGS_utf8_tolower (raw_id); 601 GNUNET_free (raw_id); 602 if (0 == strcmp (instance_id, 603 "admin")) 604 { 605 enum MHD_Result ret; 606 struct MHD_Response *response; 607 const char *rstart = hc->full_url + strlen (instance_prefix); 608 const char *rslash = strchr (rstart, '/'); 609 610 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 611 "Client used deprecated '/instances/admin/' path. Redirecting to modern path\n"); 612 613 response 614 = MHD_create_response_from_buffer (0, 615 NULL, 616 MHD_RESPMEM_PERSISTENT); 617 TALER_MHD_add_global_headers (response, 618 true); 619 if (MHD_NO == 620 MHD_add_response_header (response, 621 MHD_HTTP_HEADER_LOCATION, 622 NULL == rslash 623 ? "/" 624 : rslash)) 625 { 626 GNUNET_break (0); 627 MHD_destroy_response (response); 628 GNUNET_free (instance_id); 629 return GNUNET_SYSERR; 630 } 631 ret = MHD_queue_response (hc->connection, 632 MHD_HTTP_PERMANENT_REDIRECT, 633 response); 634 MHD_destroy_response (response); 635 GNUNET_free (instance_id); 636 return (MHD_YES == ret) ? GNUNET_NO : GNUNET_SYSERR; 637 } 638 hc->instance = TMH_lookup_instance (instance_id); 639 if ( (NULL == hc->instance) && 640 (0 == strcmp ("admin", 641 instance_id)) ) 642 hc->instance = TMH_lookup_instance (NULL); 643 GNUNET_free (instance_id); 644 if (NULL == slash) 645 *urlp = ""; 646 else 647 *urlp = slash; 648 } 649 else 650 { 651 /* use 'default' */ 652 *use_admin = true; 653 hc->instance = TMH_lookup_instance (NULL); 654 } 655 if (NULL != hc->instance) 656 { 657 GNUNET_assert (hc->instance->rc < UINT_MAX); 658 hc->instance->rc++; 659 } 660 return GNUNET_OK; 661 } 662 663 664 /** 665 * A client has requested the given url using the given method 666 * (#MHD_HTTP_METHOD_GET, #MHD_HTTP_METHOD_PUT, 667 * #MHD_HTTP_METHOD_DELETE, #MHD_HTTP_METHOD_POST, etc). The callback 668 * must call MHD callbacks to provide content to give back to the 669 * client and return an HTTP status code (i.e. #MHD_HTTP_OK, 670 * #MHD_HTTP_NOT_FOUND, etc.). 671 * 672 * @param cls argument given together with the function 673 * pointer when the handler was registered with MHD 674 * @param connection the MHD connection to handle 675 * @param url the requested url 676 * @param method the HTTP method used (#MHD_HTTP_METHOD_GET, 677 * #MHD_HTTP_METHOD_PUT, etc.) 678 * @param version the HTTP version string (i.e. 679 * #MHD_HTTP_VERSION_1_1) 680 * @param upload_data the data being uploaded (excluding HEADERS, 681 * for a POST that fits into memory and that is encoded 682 * with a supported encoding, the POST data will NOT be 683 * given in upload_data and is instead available as 684 * part of #MHD_get_connection_values; very large POST 685 * data *will* be made available incrementally in 686 * @a upload_data) 687 * @param upload_data_size set initially to the size of the 688 * @a upload_data provided; the method must update this 689 * value to the number of bytes NOT processed; 690 * @param con_cls pointer that the callback can set to some 691 * address and that will be preserved by MHD for future 692 * calls for this request; since the access handler may 693 * be called many times (i.e., for a PUT/POST operation 694 * with plenty of upload data) this allows the application 695 * to easily associate some request-specific state. 696 * If necessary, this state can be cleaned up in the 697 * global #MHD_RequestCompletedCallback (which 698 * can be set with the #MHD_OPTION_NOTIFY_COMPLETED). 699 * Initially, `*con_cls` will be set up by the 700 * full_url_track_callback(). 701 * @return #MHD_YES if the connection was handled successfully, 702 * #MHD_NO if the socket must be closed due to a serious 703 * error while handling the request 704 */ 705 static enum MHD_Result 706 url_handler (void *cls, 707 struct MHD_Connection *connection, 708 const char *url, 709 const char *method, 710 const char *version, 711 const char *upload_data, 712 size_t *upload_data_size, 713 void **con_cls) 714 { 715 struct TMH_HandlerContext *hc = *con_cls; 716 bool use_admin = false; 717 bool is_public = false; 718 719 (void) cls; 720 (void) version; 721 if (NULL == hc->url) 722 { 723 /* First time. 724 * Find out the merchant backend instance for the request. 725 * If there is an instance, remove the instance specification 726 * from the beginning of the request URL. */ 727 enum GNUNET_GenericReturnValue ret; 728 729 hc->url = url; 730 log_request (hc, 731 method); 732 ret = identify_instance (hc, 733 &url, 734 &use_admin); 735 if (GNUNET_OK != ret) 736 return (GNUNET_NO == ret) ? MHD_YES : MHD_NO; 737 } 738 739 if (NULL != hc->instance) 740 { 741 /* Narrow DB interaction to selected instance */ 742 enum GNUNET_DB_QueryStatus qs; 743 744 qs = TALER_MERCHANTDB_set_instance (TMH_db, 745 hc->instance->settings.id); 746 switch (qs) 747 { 748 case GNUNET_DB_STATUS_HARD_ERROR: 749 GNUNET_break (0); 750 return TALER_MHD_reply_with_error ( 751 connection, 752 MHD_HTTP_INTERNAL_SERVER_ERROR, 753 TALER_EC_GENERIC_DB_SETUP_FAILED, 754 "set_instance"); 755 case GNUNET_DB_STATUS_SOFT_ERROR: 756 GNUNET_break (0); 757 return TALER_MHD_reply_with_error ( 758 connection, 759 MHD_HTTP_INTERNAL_SERVER_ERROR, 760 TALER_EC_GENERIC_DB_SETUP_FAILED, 761 "set_instance"); 762 case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: 763 return TALER_MHD_reply_with_error ( 764 connection, 765 MHD_HTTP_NOT_FOUND, 766 TALER_EC_MERCHANT_GENERIC_INSTANCE_UNKNOWN, 767 hc->url); 768 case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: 769 break; 770 } 771 } 772 773 if (NULL != hc->rh) 774 { 775 enum MHD_Result res; 776 777 /* MHD calls us again for a request, we already identified 778 the handler, just continue processing with the handler */ 779 res = process_upload_with_handler (hc, 780 upload_data, 781 upload_data_size); 782 if (NULL != hc->instance) 783 { 784 GNUNET_break (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == 785 TALER_MERCHANTDB_set_instance (TMH_db, 786 NULL)); 787 } 788 return res; 789 } 790 791 /* First time, let's figure out the handler */ 792 { 793 enum GNUNET_GenericReturnValue ret; 794 795 ret = TMH_dispatch_request (hc, 796 url, 797 method, 798 use_admin, 799 &is_public); 800 if (GNUNET_OK != ret) 801 { 802 if (NULL != hc->instance) 803 { 804 GNUNET_break (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == 805 TALER_MERCHANTDB_set_instance (TMH_db, 806 NULL)); 807 } 808 return (GNUNET_NO == ret) ? MHD_YES : MHD_NO; 809 } 810 } 811 812 /* At this point, we must have found a handler */ 813 GNUNET_assert (NULL != hc->rh); 814 815 /* If an instance must be there, check one exists */ 816 if ( (NULL == hc->instance) && 817 (! hc->rh->skip_instance) ) 818 { 819 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 820 "Instance for `%s' not known\n", 821 hc->url); 822 return TALER_MHD_reply_with_error (connection, 823 MHD_HTTP_NOT_FOUND, 824 TALER_EC_MERCHANT_GENERIC_INSTANCE_UNKNOWN, 825 hc->url); 826 } 827 828 /* Perform access control for non-public handlers */ 829 if (! is_public) 830 { 831 enum GNUNET_GenericReturnValue ret; 832 833 ret = TMH_perform_access_control (hc); 834 if (GNUNET_OK != ret) 835 { 836 if (NULL != hc->instance) 837 { 838 GNUNET_break (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == 839 TALER_MERCHANTDB_set_instance (TMH_db, 840 NULL)); 841 } 842 return (GNUNET_NO == ret) ? MHD_YES : MHD_NO; 843 } 844 } 845 if (NULL != hc->instance) 846 { 847 GNUNET_break (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == 848 TALER_MERCHANTDB_set_instance (TMH_db, 849 NULL)); 850 } 851 852 if ( (NULL != hc->instance) && /* make static analysis happy */ 853 (! hc->rh->skip_instance) && 854 (hc->instance->deleted) && 855 (! hc->rh->allow_deleted_instance) ) 856 { 857 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 858 "Instance `%s' was deleted\n", 859 hc->instance->settings.id); 860 return TALER_MHD_reply_with_error (connection, 861 MHD_HTTP_NOT_FOUND, 862 TALER_EC_MERCHANT_GENERIC_INSTANCE_DELETED, 863 hc->instance->settings.id); 864 } 865 866 /* Check upload constraints */ 867 hc->has_body = ( (0 == strcasecmp (method, 868 MHD_HTTP_METHOD_POST)) || 869 /* PUT is not yet used */ 870 (0 == strcasecmp (method, 871 MHD_HTTP_METHOD_PATCH)) ); 872 if (hc->has_body) 873 { 874 /* This is a macro: it will queue an error response and return 875 from this function if the upload would be too large. */ 876 TALER_MHD_check_content_length (connection, 877 0 == hc->rh->max_upload 878 ? DEFAULT_MAX_UPLOAD_SIZE 879 : hc->rh->max_upload); 880 GNUNET_break (NULL == hc->request_body); /* can't have it already */ 881 } 882 /* wait for MHD to call us again, this time hc->url will be non-NULL 883 and we should jump straight into process_upload_with_handler(). */ 884 return MHD_YES; 885 } 886 887 888 /** 889 * Callback invoked with information about a bank account. 890 * 891 * @param cls closure with a `struct TMH_MerchantInstance *` 892 * @param merchant_priv private key of the merchant instance 893 * @param acc details about the account 894 */ 895 static void 896 add_account_cb (void *cls, 897 const struct TALER_MerchantPrivateKeyP *merchant_priv, 898 const struct TALER_MERCHANTDB_AccountDetails *acc) 899 { 900 struct TMH_MerchantInstance *mi = cls; 901 struct TMH_WireMethod *wm; 902 903 (void) merchant_priv; 904 wm = GNUNET_new (struct TMH_WireMethod); 905 wm->h_wire = acc->h_wire; 906 wm->payto_uri.full_payto 907 = GNUNET_strdup (acc->payto_uri.full_payto); 908 if (NULL != acc->extra_wire_subject_metadata) 909 wm->extra_wire_subject_metadata 910 = GNUNET_strdup (acc->extra_wire_subject_metadata); 911 wm->wire_salt = acc->salt; 912 wm->wire_method 913 = TALER_payto_get_method (acc->payto_uri.full_payto); 914 wm->active = acc->active; 915 GNUNET_CONTAINER_DLL_insert (mi->wm_head, 916 mi->wm_tail, 917 wm); 918 } 919 920 921 /** 922 * Function called during startup to add all known instances to our 923 * hash map in memory for faster lookups when we receive requests. 924 * 925 * @param cls closure, NULL, unused 926 * @param merchant_pub public key of the instance 927 * @param merchant_priv private key of the instance, NULL if not available 928 * @param is detailed configuration settings for the instance 929 * @param ias authentication settings for the instance 930 */ 931 static void 932 add_instance_cb (void *cls, 933 const struct TALER_MerchantPublicKeyP *merchant_pub, 934 const struct TALER_MerchantPrivateKeyP *merchant_priv, 935 const struct TALER_MERCHANTDB_InstanceSettings *is, 936 const struct TALER_MERCHANTDB_InstanceAuthSettings *ias) 937 { 938 struct TMH_MerchantInstance *mi; 939 enum GNUNET_DB_QueryStatus qs; 940 941 (void) cls; 942 mi = TMH_lookup_instance (is->id); 943 if (NULL != mi) 944 { 945 /* (outdated) entry exists, remove old entry */ 946 (void) TMH_instance_free_cb (NULL, 947 &mi->h_instance, 948 mi); 949 } 950 mi = GNUNET_new (struct TMH_MerchantInstance); 951 mi->settings = *is; 952 mi->auth = *ias; 953 mi->settings.id = GNUNET_STRINGS_utf8_tolower (mi->settings.id); 954 mi->settings.name = GNUNET_strdup (mi->settings.name); 955 if (NULL != mi->settings.email) 956 mi->settings.email = GNUNET_strdup (mi->settings.email); 957 if (NULL != mi->settings.phone) 958 mi->settings.phone = GNUNET_strdup (mi->settings.phone); 959 if (NULL != mi->settings.website) 960 mi->settings.website = GNUNET_strdup (mi->settings.website); 961 if (NULL != mi->settings.logo) 962 mi->settings.logo = GNUNET_strdup (mi->settings.logo); 963 mi->settings.address = json_incref (mi->settings.address); 964 mi->settings.jurisdiction = json_incref (mi->settings.jurisdiction); 965 if (NULL != merchant_priv) 966 mi->merchant_priv = *merchant_priv; 967 else 968 mi->deleted = true; 969 mi->merchant_pub = *merchant_pub; 970 qs = TALER_MERCHANTDB_set_instance (TMH_db, 971 mi->settings.id); 972 switch (qs) 973 { 974 case GNUNET_DB_STATUS_SOFT_ERROR: 975 case GNUNET_DB_STATUS_HARD_ERROR: 976 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 977 "Error setting instance `%s'\n", 978 mi->settings.id); 979 GNUNET_SCHEDULER_shutdown (); 980 return; 981 case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: 982 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 983 "Instance `%s' not found in the database (legacy case mismatch not migrated?)\n", 984 mi->settings.id); 985 mi->rc = 1; 986 TMH_instance_decref (mi); 987 return; 988 case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: 989 break; 990 } 991 qs = TALER_MERCHANTDB_iterate_accounts_by_instance ( 992 TMH_db, 993 mi->settings.id, 994 &add_account_cb, 995 mi); 996 if (0 > qs) 997 { 998 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 999 "Error loading accounts of `%s' from database\n", 1000 mi->settings.id); 1001 GNUNET_SCHEDULER_shutdown (); 1002 return; 1003 } 1004 GNUNET_assert (GNUNET_OK == 1005 TMH_add_instance (mi)); 1006 GNUNET_break (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == 1007 TALER_MERCHANTDB_set_instance (TMH_db, 1008 NULL)); 1009 } 1010 1011 1012 /** 1013 * Trigger (re)loading of instance settings from DB. 1014 * 1015 * @param cls NULL 1016 * @param extra ID of the instance that changed, NULL 1017 * to load all instances (will not handle purges!) 1018 * @param extra_len number of bytes in @a extra 1019 */ 1020 static void 1021 load_instances (void *cls, 1022 const void *extra, 1023 size_t extra_len) 1024 { 1025 enum GNUNET_DB_QueryStatus qs; 1026 const char *id = extra; 1027 1028 (void) cls; 1029 if ( (NULL != extra) && 1030 ( (0 == extra_len) || 1031 ('\0' != id[extra_len - 1]) ) ) 1032 { 1033 GNUNET_break (0 == extra_len); 1034 extra = NULL; 1035 } 1036 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1037 "Received instance settings notification: reload `%s'\n", 1038 id); 1039 if (NULL == extra) 1040 { 1041 qs = TALER_MERCHANTDB_iterate_instances (TMH_db, 1042 false, 1043 &add_instance_cb, 1044 NULL); 1045 } 1046 else 1047 { 1048 struct TMH_MerchantInstance *mi; 1049 1050 /* This must be done here to handle instance 1051 purging, as for purged instances, the DB 1052 lookup below will otherwise do nothing */ 1053 mi = TMH_lookup_instance (id); 1054 if (NULL != mi) 1055 { 1056 (void) TMH_instance_free_cb (NULL, 1057 &mi->h_instance, 1058 mi); 1059 } 1060 qs = TALER_MERCHANTDB_iterate_instances_by_id (TMH_db, 1061 id, 1062 false, 1063 &add_instance_cb, 1064 NULL); 1065 } 1066 if (0 > qs) 1067 { 1068 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1069 "Failed initialization. Check database setup.\n"); 1070 global_ret = EXIT_FAILURE; 1071 GNUNET_SCHEDULER_shutdown (); 1072 return; 1073 } 1074 } 1075 1076 1077 /** 1078 * A transaction modified an instance setting (or created/deleted/purged 1079 * one). Notify all backends about the change. 1080 * 1081 * @param id ID of the instance that changed 1082 */ 1083 void 1084 TMH_reload_instances (const char *id) 1085 { 1086 struct GNUNET_DB_EventHeaderP es = { 1087 .size = htons (sizeof (es)), 1088 .type = htons (TALER_DBEVENT_MERCHANT_INSTANCE_SETTINGS) 1089 }; 1090 1091 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1092 "Generating instance settings notification: reload `%s'\n", 1093 id); 1094 TALER_MERCHANTDB_event_notify (TMH_db, 1095 &es, 1096 id, 1097 (NULL == id) 1098 ? 0 1099 : strlen (id) + 1); 1100 } 1101 1102 1103 /** 1104 * Callback invoked on every listen socket to start the 1105 * respective MHD HTTP daemon. 1106 * 1107 * @param cls unused 1108 * @param lsock the listen socket 1109 */ 1110 static void 1111 start_daemon (void *cls, 1112 int lsock) 1113 { 1114 struct MHD_Daemon *mhd; 1115 1116 (void) cls; 1117 GNUNET_assert (-1 != lsock); 1118 mhd = MHD_start_daemon (MHD_USE_SUSPEND_RESUME | MHD_USE_DUAL_STACK 1119 | MHD_USE_AUTO, 1120 0 /* port */, 1121 NULL, NULL, 1122 &url_handler, NULL, 1123 MHD_OPTION_LISTEN_SOCKET, lsock, 1124 MHD_OPTION_URI_LOG_CALLBACK, 1125 &full_url_track_callback, NULL, 1126 MHD_OPTION_NOTIFY_COMPLETED, 1127 &handle_mhd_completion_callback, NULL, 1128 MHD_OPTION_CONNECTION_TIMEOUT, 1129 (unsigned int) 10 /* 10s */, 1130 MHD_OPTION_CONNECTION_LIMIT, 1131 (unsigned int) CONNECTION_LIMIT, 1132 MHD_OPTION_END); 1133 if (NULL == mhd) 1134 { 1135 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1136 "Failed to launch HTTP service.\n"); 1137 GNUNET_SCHEDULER_shutdown (); 1138 return; 1139 } 1140 have_daemons = true; 1141 TALER_MHD_daemon_start (mhd); 1142 } 1143 1144 1145 /** 1146 * Main function that will be run by the scheduler. 1147 * 1148 * @param cls closure 1149 * @param args remaining command-line arguments 1150 * @param cfgfile name of the configuration file used (for saving, can be 1151 * NULL!) 1152 * @param config configuration 1153 */ 1154 static void 1155 run (void *cls, 1156 char *const *args, 1157 const char *cfgfile, 1158 const struct GNUNET_CONFIGURATION_Handle *config) 1159 { 1160 enum TALER_MHD_GlobalOptions go; 1161 int elen; 1162 1163 (void) cls; 1164 (void) args; 1165 (void) cfgfile; 1166 TMH_cfg = config; 1167 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1168 "Starting taler-merchant-httpd\n"); 1169 go = TALER_MHD_GO_NONE; 1170 if (merchant_connection_close) 1171 go |= TALER_MHD_GO_FORCE_CONNECTION_CLOSE; 1172 TALER_MHD_setup (go); 1173 TALER_EXCHANGE_setup (enable_h3 1174 ? TALER_EXCHANGE_GO_ENABLE_HTTP3 1175 : TALER_EXCHANGE_GO_FORCE_HTTP1_1); 1176 DONAU_setup (enable_h3 1177 ? DONAU_GO_ENABLE_HTTP3 1178 : DONAU_GO_FORCE_HTTP1_1); 1179 1180 global_ret = EXIT_SUCCESS; 1181 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, 1182 NULL); 1183 1184 TMH_curl_ctx 1185 = GNUNET_CURL_init (&GNUNET_CURL_gnunet_scheduler_reschedule, 1186 &merchant_curl_rc); 1187 if (NULL == TMH_curl_ctx) 1188 { 1189 GNUNET_break (0); 1190 global_ret = EXIT_FAILURE; 1191 GNUNET_SCHEDULER_shutdown (); 1192 return; 1193 } 1194 merchant_curl_rc = GNUNET_CURL_gnunet_rc_create (TMH_curl_ctx); 1195 /* Disable 100 continue processing */ 1196 GNUNET_break (GNUNET_OK == 1197 GNUNET_CURL_append_header (TMH_curl_ctx, 1198 MHD_HTTP_HEADER_EXPECT ":")); 1199 GNUNET_CURL_enable_async_scope_header (TMH_curl_ctx, 1200 "Taler-Correlation-Id"); 1201 1202 if (GNUNET_SYSERR == 1203 TALER_config_get_currency (TMH_cfg, 1204 "merchant", 1205 &TMH_currency)) 1206 { 1207 global_ret = EXIT_NOTCONFIGURED; 1208 GNUNET_SCHEDULER_shutdown (); 1209 return; 1210 } 1211 if (GNUNET_OK != 1212 TALER_CONFIG_parse_currencies (TMH_cfg, 1213 TMH_currency, 1214 &TMH_num_cspecs, 1215 &TMH_cspecs)) 1216 { 1217 global_ret = EXIT_NOTCONFIGURED; 1218 GNUNET_SCHEDULER_shutdown (); 1219 return; 1220 } 1221 1222 { 1223 char *spa_data; 1224 1225 if (GNUNET_OK == 1226 GNUNET_CONFIGURATION_get_value_string (TMH_cfg, 1227 "merchant", 1228 "GLOBAL_SPA_CONFIG_DATA", 1229 &spa_data)) 1230 { 1231 json_error_t err; 1232 1233 TMH_global_spa_config_data = json_loads (spa_data, 1234 JSON_REJECT_DUPLICATES, 1235 &err); 1236 GNUNET_free (spa_data); 1237 if (NULL == TMH_global_spa_config_data) 1238 { 1239 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, 1240 "merchant", 1241 "GLOBAL_SPA_CONFIG_DATA", 1242 err.text); 1243 global_ret = EXIT_NOTCONFIGURED; 1244 GNUNET_SCHEDULER_shutdown (); 1245 return; 1246 } 1247 } 1248 } 1249 1250 1251 if (GNUNET_SYSERR == 1252 (TMH_strict_v19 1253 = GNUNET_CONFIGURATION_get_value_yesno (TMH_cfg, 1254 "merchant", 1255 "STRICT_PROTOCOL_V19"))) 1256 { 1257 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO, 1258 "merchant", 1259 "STRICT_PROTOCOL_V19"); 1260 TMH_strict_v19 = GNUNET_NO; 1261 } 1262 if (GNUNET_SYSERR == 1263 (TMH_auth_disabled = GNUNET_CONFIGURATION_get_value_yesno (TMH_cfg, 1264 "merchant", 1265 "DISABLE_AUTHENTICATION"))) 1266 { 1267 TMH_auth_disabled = GNUNET_NO; 1268 } 1269 if (GNUNET_YES == TMH_auth_disabled) 1270 { 1271 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1272 "DANGEROUS: Endpoint Authentication disabled!"); 1273 } 1274 1275 if (GNUNET_SYSERR == 1276 (TMH_have_self_provisioning 1277 = GNUNET_CONFIGURATION_get_value_yesno (TMH_cfg, 1278 "merchant", 1279 "ENABLE_SELF_PROVISIONING"))) 1280 { 1281 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO, 1282 "merchant", 1283 "ENABLE_SELF_PROVISIONING"); 1284 TMH_have_self_provisioning = GNUNET_NO; 1285 } 1286 1287 if (GNUNET_SYSERR == 1288 (TMH_password_change_mfa 1289 = GNUNET_CONFIGURATION_get_value_yesno (TMH_cfg, 1290 "merchant", 1291 "PASSWORD_CHANGE_MFA"))) 1292 { 1293 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO, 1294 "merchant", 1295 "PASSWORD_CHANGE_MFA"); 1296 TMH_password_change_mfa = GNUNET_NO; 1297 } 1298 1299 if (GNUNET_OK != 1300 GNUNET_CONFIGURATION_get_value_time (TMH_cfg, 1301 "merchant", 1302 "LEGAL_PRESERVATION", 1303 &TMH_legal_expiration)) 1304 { 1305 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 1306 "merchant", 1307 "LEGAL_PRESERVATION"); 1308 global_ret = EXIT_NOTCONFIGURED; 1309 GNUNET_SCHEDULER_shutdown (); 1310 return; 1311 } 1312 1313 if (GNUNET_OK != 1314 GNUNET_CONFIGURATION_get_value_time (TMH_cfg, 1315 "merchant", 1316 "DEFAULT_PAY_DELAY", 1317 &TMH_default_pay_delay)) 1318 { 1319 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO, 1320 "merchant", 1321 "DEFAULT_PAY_DELAY"); 1322 TMH_default_pay_delay = GNUNET_TIME_UNIT_DAYS; 1323 } 1324 if (GNUNET_TIME_relative_is_forever (TMH_default_pay_delay)) 1325 { 1326 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_INFO, 1327 "merchant", 1328 "DEFAULT_PAY_DELAY", 1329 "forever is not allowed"); 1330 global_ret = EXIT_NOTCONFIGURED; 1331 GNUNET_SCHEDULER_shutdown (); 1332 return; 1333 } 1334 if (GNUNET_OK != 1335 GNUNET_CONFIGURATION_get_value_time (TMH_cfg, 1336 "merchant", 1337 "DEFAULT_REFUND_DELAY", 1338 &TMH_default_refund_delay)) 1339 { 1340 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO, 1341 "merchant", 1342 "DEFAULT_REFUND_DELAY"); 1343 TMH_default_refund_delay = GNUNET_TIME_relative_multiply ( 1344 GNUNET_TIME_UNIT_DAYS, 1345 15); 1346 } 1347 if (GNUNET_TIME_relative_is_forever (TMH_default_refund_delay)) 1348 { 1349 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_INFO, 1350 "merchant", 1351 "DEFAULT_REFUND_DELAY", 1352 "forever is not allowed"); 1353 global_ret = EXIT_NOTCONFIGURED; 1354 GNUNET_SCHEDULER_shutdown (); 1355 return; 1356 } 1357 1358 if (GNUNET_OK != 1359 GNUNET_CONFIGURATION_get_value_time (TMH_cfg, 1360 "merchant", 1361 "DEFAULT_WIRE_TRANSFER_DELAY", 1362 &TMH_default_wire_transfer_delay)) 1363 { 1364 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO, 1365 "merchant", 1366 "DEFAULT_WIRE_TRANSFER_DELAY"); 1367 TMH_default_wire_transfer_delay = GNUNET_TIME_UNIT_MONTHS; 1368 } 1369 if (GNUNET_TIME_relative_is_forever (TMH_default_wire_transfer_delay)) 1370 { 1371 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_INFO, 1372 "merchant", 1373 "DEFAULT_WIRE_TRANSFER_DELAY", 1374 "forever is not allowed"); 1375 global_ret = EXIT_NOTCONFIGURED; 1376 GNUNET_SCHEDULER_shutdown (); 1377 return; 1378 } 1379 1380 { 1381 char *dwtri; 1382 1383 if (GNUNET_OK != 1384 GNUNET_CONFIGURATION_get_value_string ( 1385 TMH_cfg, 1386 "merchant", 1387 "DEFAULT_WIRE_TRANSFER_ROUNDING_INTERVAL", 1388 &dwtri)) 1389 { 1390 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO, 1391 "merchant", 1392 "DEFAULT_WIRE_TRANSFER_ROUNDING_INTERVAL"); 1393 TMH_default_wire_transfer_rounding_interval = GNUNET_TIME_RI_NONE; 1394 } 1395 else 1396 { 1397 if (GNUNET_OK != 1398 GNUNET_TIME_string_to_round_interval ( 1399 dwtri, 1400 &TMH_default_wire_transfer_rounding_interval)) 1401 { 1402 GNUNET_log_config_invalid ( 1403 GNUNET_ERROR_TYPE_ERROR, 1404 "merchant", 1405 "DEFAULT_WIRE_TRANSFER_ROUNDING_INTERVAL", 1406 "invalid time rounding interval"); 1407 global_ret = EXIT_NOTCONFIGURED; 1408 GNUNET_free (dwtri); 1409 GNUNET_SCHEDULER_shutdown (); 1410 return; 1411 } 1412 GNUNET_free (dwtri); 1413 } 1414 } 1415 1416 TMH_load_terms (TMH_cfg); 1417 1418 if (GNUNET_OK != 1419 GNUNET_CONFIGURATION_get_value_string (TMH_cfg, 1420 "merchant", 1421 "PAYMENT_TARGET_TYPES", 1422 &TMH_allowed_payment_targets)) 1423 { 1424 TMH_allowed_payment_targets = GNUNET_strdup ("*"); 1425 } 1426 1427 if (GNUNET_OK != 1428 GNUNET_CONFIGURATION_get_value_string (TMH_cfg, 1429 "merchant", 1430 "DEFAULT_PERSONA", 1431 &TMH_default_persona)) 1432 { 1433 TMH_default_persona = GNUNET_strdup ("expert"); 1434 } 1435 1436 if (GNUNET_OK != 1437 GNUNET_CONFIGURATION_get_value_string (TMH_cfg, 1438 "merchant", 1439 "PAYMENT_TARGET_REGEX", 1440 &TMH_payment_target_regex)) 1441 { 1442 TMH_payment_target_regex = NULL; 1443 } 1444 else 1445 { 1446 if (0 == strlen (TMH_payment_target_regex)) 1447 { 1448 GNUNET_free (TMH_payment_target_regex); 1449 } 1450 else 1451 { 1452 if (0 != regcomp (&TMH_payment_target_re, 1453 TMH_payment_target_regex, 1454 REG_NOSUB | REG_EXTENDED)) 1455 { 1456 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, 1457 "merchant", 1458 "PAYMENT_TARGET_REGEX", 1459 "malformed regular expression"); 1460 global_ret = EXIT_NOTCONFIGURED; 1461 GNUNET_free (TMH_payment_target_regex); 1462 GNUNET_SCHEDULER_shutdown (); 1463 return; 1464 } 1465 } 1466 } 1467 if (GNUNET_OK != 1468 GNUNET_CONFIGURATION_get_value_string (TMH_cfg, 1469 "merchant", 1470 "PHONE_REGEX", 1471 &TMH_phone_regex)) 1472 { 1473 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING, 1474 "merchant", 1475 "PHONE_REGEX", 1476 "no restrictions on phone number specified"); 1477 } 1478 else 1479 { 1480 if (0 != regcomp (&TMH_phone_rx, 1481 TMH_phone_regex, 1482 REG_EXTENDED)) 1483 { 1484 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, 1485 "merchant", 1486 "PHONE_REGEX", 1487 "Invalid regex specified"); 1488 global_ret = EXIT_NOTCONFIGURED; 1489 GNUNET_SCHEDULER_shutdown (); 1490 return; 1491 } 1492 } 1493 1494 if (GNUNET_OK != 1495 GNUNET_CONFIGURATION_get_value_string (TMH_cfg, 1496 "merchant", 1497 "HELPER_SMS", 1498 &TMH_helper_sms)) 1499 { 1500 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING, 1501 "merchant", 1502 "HELPER_SMS", 1503 "no helper specified"); 1504 } 1505 1506 if (GNUNET_OK != 1507 GNUNET_CONFIGURATION_get_value_string (TMH_cfg, 1508 "merchant", 1509 "HELPER_EMAIL", 1510 &TMH_helper_email)) 1511 { 1512 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING, 1513 "merchant", 1514 "HELPER_EMAIL", 1515 "no helper specified"); 1516 } 1517 1518 { 1519 char *tan_channels; 1520 1521 if (GNUNET_OK == 1522 GNUNET_CONFIGURATION_get_value_string (TMH_cfg, 1523 "merchant", 1524 "MANDATORY_TAN_CHANNELS", 1525 &tan_channels)) 1526 { 1527 for (char *tok = strtok (tan_channels, 1528 " "); 1529 NULL != tok; 1530 tok = strtok (NULL, 1531 " ")) 1532 { 1533 if (0 == strcasecmp (tok, 1534 "sms")) 1535 { 1536 if (NULL == TMH_helper_sms) 1537 { 1538 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, 1539 "merchant", 1540 "MANDATORY_TAN_CHANNELS", 1541 "SMS mandatory, but no HELPER_SMS configured"); 1542 global_ret = EXIT_NOTCONFIGURED; 1543 GNUNET_SCHEDULER_shutdown (); 1544 GNUNET_free (tan_channels); 1545 return; 1546 } 1547 TEH_mandatory_tan_channels |= TMH_TCS_SMS; 1548 } 1549 else if (0 == strcasecmp (tok, 1550 "email")) 1551 { 1552 if (NULL == TMH_helper_email) 1553 { 1554 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, 1555 "merchant", 1556 "MANDATORY_TAN_CHANNELS", 1557 "EMAIL mandatory, but no HELPER_EMAIL configured"); 1558 global_ret = EXIT_NOTCONFIGURED; 1559 GNUNET_SCHEDULER_shutdown (); 1560 GNUNET_free (tan_channels); 1561 return; 1562 } 1563 TEH_mandatory_tan_channels |= TMH_TCS_EMAIL; 1564 } 1565 else 1566 { 1567 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, 1568 "merchant", 1569 "MANDATORY_TAN_CHANNELS", 1570 tok); 1571 global_ret = EXIT_NOTCONFIGURED; 1572 GNUNET_SCHEDULER_shutdown (); 1573 GNUNET_free (tan_channels); 1574 return; 1575 } 1576 } 1577 GNUNET_free (tan_channels); 1578 } 1579 } 1580 1581 if ( (GNUNET_YES == TMH_password_change_mfa) && 1582 (TMH_TCS_NONE == TEH_mandatory_tan_channels) ) 1583 { 1584 GNUNET_log_config_invalid ( 1585 GNUNET_ERROR_TYPE_ERROR, 1586 "merchant", 1587 "PASSWORD_CHANGE_MFA", 1588 "requires at least one MANDATORY_TAN_CHANNELS entry"); 1589 global_ret = EXIT_NOTCONFIGURED; 1590 GNUNET_SCHEDULER_shutdown (); 1591 return; 1592 } 1593 1594 if (GNUNET_OK == 1595 GNUNET_CONFIGURATION_get_value_string (TMH_cfg, 1596 "merchant", 1597 "BASE_URL", 1598 &TMH_base_url)) 1599 { 1600 if (! TALER_is_web_url (TMH_base_url)) 1601 { 1602 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, 1603 "merchant", 1604 "BASE_URL", 1605 "Needs to start with 'http://' or 'https://'"); 1606 global_ret = EXIT_NOTCONFIGURED; 1607 GNUNET_SCHEDULER_shutdown (); 1608 return; 1609 } 1610 } 1611 if (GNUNET_OK == 1612 GNUNET_CONFIGURATION_get_value_filename (TMH_cfg, 1613 "merchant", 1614 "BACKOFFICE_SPA_DIR", 1615 &TMH_spa_dir)) 1616 { 1617 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1618 "Loading merchant SPA from %s\n", 1619 TMH_spa_dir); 1620 } 1621 else 1622 { 1623 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1624 "Loading merchant SPA from default location\n"); 1625 } 1626 1627 if (GNUNET_YES == 1628 GNUNET_CONFIGURATION_get_value_yesno (TMH_cfg, 1629 "merchant", 1630 "FORCE_AUDIT")) 1631 TMH_force_audit = GNUNET_YES; 1632 if (GNUNET_OK != 1633 TALER_TEMPLATING_init (TALER_MERCHANT_project_data ())) 1634 { 1635 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1636 "Failed to setup templates\n"); 1637 global_ret = EXIT_NO_RESTART; 1638 GNUNET_SCHEDULER_shutdown (); 1639 return; 1640 } 1641 if (GNUNET_OK != 1642 TMH_spa_init (TMH_spa_dir)) 1643 { 1644 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1645 "Failed to load single page app\n"); 1646 global_ret = EXIT_NO_RESTART; 1647 GNUNET_SCHEDULER_shutdown (); 1648 return; 1649 } 1650 /* /static/ is currently not used */ 1651 /* (void) TMH_statics_init (); */ 1652 if (NULL == 1653 (TMH_by_id_map = GNUNET_CONTAINER_multihashmap_create (4, 1654 GNUNET_YES))) 1655 { 1656 global_ret = EXIT_FAILURE; 1657 GNUNET_SCHEDULER_shutdown (); 1658 return; 1659 } 1660 if (NULL == 1661 (TMH_db = TALER_MERCHANTDB_connect (TMH_cfg))) 1662 { 1663 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1664 "Failed to connect to database. Consider running taler-merchant-dbinit!\n"); 1665 global_ret = EXIT_FAILURE; 1666 GNUNET_SCHEDULER_shutdown (); 1667 return; 1668 } 1669 elen = TMH_EXCHANGES_init (config); 1670 if (GNUNET_SYSERR == elen) 1671 { 1672 global_ret = EXIT_NOTCONFIGURED; 1673 GNUNET_SCHEDULER_shutdown (); 1674 return; 1675 } 1676 if (0 == elen) 1677 { 1678 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1679 "Fatal: no trusted exchanges configured. Exiting.\n"); 1680 global_ret = EXIT_NOTCONFIGURED; 1681 GNUNET_SCHEDULER_shutdown (); 1682 return; 1683 } 1684 1685 { 1686 struct GNUNET_DB_EventHeaderP es = { 1687 .size = htons (sizeof (es)), 1688 .type = htons (TALER_DBEVENT_MERCHANT_INSTANCE_SETTINGS) 1689 }; 1690 1691 instance_eh = TALER_MERCHANTDB_event_listen (TMH_db, 1692 &es, 1693 GNUNET_TIME_UNIT_FOREVER_REL, 1694 &load_instances, 1695 NULL); 1696 } 1697 load_instances (NULL, 1698 NULL, 1699 0); 1700 { 1701 enum GNUNET_GenericReturnValue ret; 1702 1703 ret = TALER_MHD_listen_bind (TMH_cfg, 1704 "merchant", 1705 &start_daemon, 1706 NULL); 1707 switch (ret) 1708 { 1709 case GNUNET_SYSERR: 1710 global_ret = EXIT_NOTCONFIGURED; 1711 GNUNET_SCHEDULER_shutdown (); 1712 return; 1713 case GNUNET_NO: 1714 if (! have_daemons) 1715 { 1716 global_ret = EXIT_FAILURE; 1717 GNUNET_SCHEDULER_shutdown (); 1718 return; 1719 } 1720 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1721 "Could not open all configured listen sockets\n"); 1722 break; 1723 case GNUNET_OK: 1724 break; 1725 } 1726 } 1727 global_ret = EXIT_SUCCESS; 1728 } 1729 1730 1731 /** 1732 * The main function of the serve tool 1733 * 1734 * @param argc number of arguments from the command line 1735 * @param argv command line arguments 1736 * @return 0 ok, non-zero on error 1737 */ 1738 int 1739 main (int argc, 1740 char *const *argv) 1741 { 1742 enum GNUNET_GenericReturnValue res; 1743 struct GNUNET_GETOPT_CommandLineOption options[] = { 1744 GNUNET_GETOPT_option_flag ('C', 1745 "connection-close", 1746 "force HTTP connections to be closed after each request", 1747 &merchant_connection_close), 1748 GNUNET_GETOPT_option_flag ('3', 1749 "http3", 1750 "enable support for HTTP/2 and HTTP/3", 1751 &enable_h3), 1752 GNUNET_GETOPT_option_timetravel ('T', 1753 "timetravel"), 1754 GNUNET_GETOPT_option_version (PACKAGE_VERSION), 1755 GNUNET_GETOPT_OPTION_END 1756 }; 1757 1758 res = GNUNET_PROGRAM_run ( 1759 TALER_MERCHANT_project_data (), 1760 argc, argv, 1761 "taler-merchant-httpd", 1762 "Taler merchant's HTTP backend interface", 1763 options, 1764 &run, NULL); 1765 if (GNUNET_SYSERR == res) 1766 return EXIT_NOTCONFIGURED; 1767 if (GNUNET_NO == res) 1768 return EXIT_SUCCESS; 1769 return global_ret; 1770 }