sync-httpd2.c (28169B)
1 /* 2 This file is part of TALER 3 (C) 2019--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 sync/sync-httpd2.c 18 * @brief HTTP serving layer intended to provide basic backup operations 19 * @author Christian Grothoff 20 */ 21 #include "platform.h" 22 #include <gnunet/gnunet_util_lib.h> 23 #include "sync/sync_util.h" 24 #include "sync-httpd2.h" 25 #include "sync/sync_database_lib.h" 26 #include "sync-httpd2_config.h" 27 #include "sync-httpd2_post-backups-KEY-blocks-NONCE.h" 28 #include "sync-httpd2_delete-backups-KEY-blocks-NONCE.h" 29 #include "sync-httpd2_get-backups-KEY-objects-UID.h" 30 #include "sync-httpd2_post-backups-KEY-objects-UID.h" 31 #include "sync-httpd2_get-backups-KEY.h" 32 #include "sync-httpd2_get-backups-KEY-blocks.h" 33 #include "sync-httpd2_get-backups-KEY-blocks-reconcile.h" 34 35 /** 36 * Maximum number of concurrent connections per HTTP daemon. 37 * TALER_MHD_daemon_start() keeps our descriptors below FD_SETSIZE 38 * (1024). 300 connections is plenty, same as exchange. At the 39 * limit, MHD stops accepting, so that other sync-httpd 40 * processes sharing the listen socket (as high-performance setups do 41 * via systemd) take the connection. 42 */ 43 #define CONNECTION_LIMIT 300 44 45 46 /** 47 * @brief A handler for a backup sub-operation under /backups/$KEY/. 48 * 49 * Each entry maps an operation name ("block" or "object") and an 50 * HTTP method to a handler function. 51 */ 52 struct SH_BackupOperationHandler 53 { 54 55 /** 56 * Operation name, e.g. "block" or "object". 57 */ 58 const char *operation; 59 60 /** 61 * HTTP method this handler is for. 62 */ 63 enum MHD_HTTP_Method method; 64 65 /** 66 * Function to call. 67 * 68 * @param request the MHD request to handle 69 * @param account_pub public key of the account 70 * @param args path segments after the operation name 71 * @param upload_size number of bytes being uploaded 72 * @param is_update whether the operation is an update 73 * @return MHD action 74 */ 75 const struct MHD_Action *(*handler)( 76 struct MHD_Request *request, 77 const struct SYNC_AccountPublicKeyP *account_pub, 78 const char *const args[], 79 uint_fast64_t upload_size, 80 bool is_update); 81 82 /** 83 * Number of path segments this handler expects 84 * after the operation name. 85 */ 86 unsigned int nargs; 87 88 /** 89 * Is @e nargs only an upper bound? 90 */ 91 bool nargs_is_upper_bound; 92 }; 93 94 95 /** 96 * Should a "Connection: close" header be added to each HTTP response? 97 */ 98 static int SH_sync_connection_close; 99 100 /** 101 * Upload limit to the service, in megabytes. 102 */ 103 unsigned long long int SH_upload_limit_mb; 104 105 /** 106 * How many uploads of the maximum permitted size the shared upload 107 * buffer pool must have room for. 108 * 109 * Block uploads are handed to the handler in one piece 110 * (#MHD_action_process_upload_full()), so MHD buffers the whole body 111 * out of a daemon-wide pool. The pool is a budget, not a 112 * pre-allocation -- MHD mallocs per request and gives the space back 113 * when the request ends -- so this bounds the worst-case resident size 114 * of concurrent uploads rather than costing anything up front. 115 */ 116 #define UPLOAD_POOL_CONCURRENCY 8 117 118 119 bool 120 SH_upload_too_large (uint_fast64_t upload_size, 121 size_t *buff_size) 122 { 123 uint_fast64_t limit; 124 125 limit = ((uint_fast64_t) SH_upload_limit_mb) * 1024 * 1024; 126 if (MHD_SIZE_UNKNOWN == upload_size) 127 { 128 /* Chunked upload: the length is not known yet, so bound the buffer 129 by the limit and let MHD fail the request if it does not fit. */ 130 *buff_size = (size_t) GNUNET_MIN (limit, 131 (uint_fast64_t) SIZE_MAX); 132 return false; 133 } 134 if ( (upload_size >= limit) || 135 (upload_size > SIZE_MAX) ) 136 return true; 137 *buff_size = (size_t) upload_size; 138 return false; 139 } 140 141 142 /** 143 * Annual fee for the backup account. 144 */ 145 struct TALER_Amount SH_annual_fee; 146 147 /** 148 * Our Taler backend to process payments. 149 */ 150 char *SH_backend_url; 151 152 /** 153 * Our fulfillment URL. 154 */ 155 char *SH_fulfillment_url; 156 157 /** 158 * Our context for making HTTP requests. 159 */ 160 struct GNUNET_CURL_Context *SH_ctx; 161 162 /** 163 * Reschedule context for #SH_ctx. 164 */ 165 static struct GNUNET_CURL_RescheduleContext *rc; 166 167 /** 168 * Global return code 169 */ 170 static int global_ret; 171 172 /** 173 * Set to true if we have started an MHD daemons. 174 */ 175 static bool have_daemons; 176 177 /** 178 * Username and password to use for client authentication 179 * (optional). 180 */ 181 static char *userpass; 182 183 /** 184 * Type of the client's TLS certificate (optional). 185 */ 186 static char *certtype; 187 188 /** 189 * File with the client's TLS certificate (optional). 190 */ 191 static char *certfile; 192 193 /** 194 * File with the client's TLS private key (optional). 195 */ 196 static char *keyfile; 197 198 /** 199 * This value goes in the Authorization:-header. 200 */ 201 static char *apikey; 202 203 /** 204 * Passphrase to decrypt client's TLS private key file (optional). 205 */ 206 static char *keypass; 207 208 /** 209 * Amount of insurance. 210 */ 211 struct TALER_Amount SH_insurance; 212 213 214 /** 215 * Function to respond to GET requests on '/'. 216 * 217 * @param request the MHD request to handle 218 * @param upload_size number of bytes uploaded 219 * @return MHD action 220 */ 221 static const struct MHD_Action * 222 respond_root (struct MHD_Request *request, 223 uint_fast64_t upload_size) 224 { 225 const char *msg = "Hello, I'm sync. This HTTP server is not for humans.\n"; 226 struct MHD_Response *resp; 227 228 GNUNET_break_op (0 == upload_size); 229 resp = MHD_response_from_buffer_static ( 230 MHD_HTTP_STATUS_OK, 231 strlen (msg), 232 msg); 233 GNUNET_break (MHD_SC_OK == 234 MHD_response_add_header (resp, 235 MHD_HTTP_HEADER_CONTENT_TYPE, 236 "text/plain")); 237 return MHD_action_from_response (request, 238 resp); 239 } 240 241 242 /** 243 * Function to respond to GET requests on unknown URLs. 244 * 245 * @param request the MHD request to handle 246 * @param upload_size number of bytes uploaded 247 * @return MHD action 248 */ 249 static const struct MHD_Action * 250 respond_404 (struct MHD_Request *request, 251 uint_fast64_t upload_size) 252 { 253 const char *msg = "<html><title>404: not found</title></html>"; 254 struct MHD_Response *resp; 255 256 GNUNET_break_op (0 == upload_size); 257 resp = MHD_response_from_buffer_static ( 258 MHD_HTTP_STATUS_NOT_FOUND, 259 strlen (msg), 260 msg); 261 GNUNET_break (MHD_SC_OK == 262 MHD_response_add_header (resp, 263 MHD_HTTP_HEADER_CONTENT_TYPE, 264 "text/html")); 265 return MHD_action_from_response (request, 266 resp); 267 } 268 269 270 /** 271 * Function to respond to GET requests on '/agpl'. 272 * 273 * @param request the MHD request to handle 274 * @param upload_size number of bytes uploaded 275 * @return MHD action 276 */ 277 static const struct MHD_Action * 278 respond_agpl (struct MHD_Request *request, 279 uint_fast64_t upload_size) 280 { 281 GNUNET_break_op (0 == upload_size); 282 return TALER_MHD2_reply_agpl (request, 283 "https://git.taler.net/sync.git/"); 284 } 285 286 287 /** 288 * Maximum length of the encoded account public key in the URL. 289 * An EdDSA public key encodes to 52 characters. 290 */ 291 #define ACCOUNT_KEY_MAX 64 292 293 /** 294 * Maximum reasonable subpath length after /backups/$KEY/. 295 */ 296 #define SUBPATH_MAX 256 297 298 /** 299 * Maximum number of path segments we split a subpath into, 300 * including the operation name. 301 */ 302 #define SUBPATH_MAX_SEGMENTS 3 303 304 305 /** 306 * Handle a request on a subpath under /backups/$KEY/. 307 * 308 * Parses the subpath (e.g. "block/NONCE" or "object/UID"), 309 * splits it into segments, and dispatches to the appropriate 310 * handler using the data-driven #SH_BackupOperationHandler 311 * table. 312 * 313 * @param request the MHD request to handle 314 * @param account_pub public key of the account 315 * @param method HTTP method of the request 316 * @param subpath remainder of the URL after /backups/$KEY/ 317 * @param upload_size number of bytes being uploaded 318 * @return MHD action 319 */ 320 static const struct MHD_Action * 321 handle_backup_subpath (struct MHD_Request *request, 322 const struct SYNC_AccountPublicKeyP *account_pub, 323 enum MHD_HTTP_Method method, 324 const char *subpath, 325 uint_fast64_t upload_size) 326 { 327 static struct SH_BackupOperationHandler bhandlers[] = { 328 { 329 .operation = "blocks", 330 .method = MHD_HTTP_METHOD_POST, 331 .handler = &SH_backup_block_post, 332 .nargs = 1, 333 }, 334 { 335 .operation = "blocks", 336 /* FIXME: replace with PATCH when mhd2 supports it */ 337 .method = MHD_HTTP_METHOD_PUT, 338 .handler = &SH_backup_block_post, 339 .nargs = 1, 340 }, 341 { 342 .operation = "blocks", 343 .method = MHD_HTTP_METHOD_DELETE, 344 .handler = &SH_backup_block_delete, 345 .nargs = 1, 346 }, 347 { 348 .operation = "blocks", 349 .method = MHD_HTTP_METHOD_GET, 350 .handler = &SH_backup_blocks_get, 351 .nargs = 0, 352 }, 353 { 354 .operation = "blocks", 355 .method = MHD_HTTP_METHOD_GET, 356 .handler = &SH_backup_blocks_reconcile_get, 357 .nargs = 1, 358 .nargs_is_upper_bound = true, 359 }, 360 { 361 .operation = "objects", 362 .method = MHD_HTTP_METHOD_GET, 363 .handler = &SH_backup_object_get, 364 .nargs = 1, 365 }, 366 { 367 .operation = "objects", 368 .method = MHD_HTTP_METHOD_POST, 369 .handler = &SH_backup_object_post, 370 .nargs = 1, 371 }, 372 { .operation = NULL } 373 }; 374 size_t slen; 375 char d[SUBPATH_MAX]; 376 char *sp; 377 /* NULL-terminated, hence one more entry than we ever fill */ 378 const char *args[SUBPATH_MAX_SEGMENTS + 1] = { NULL }; 379 unsigned int i; 380 381 GNUNET_assert (NULL != request); 382 GNUNET_assert (NULL != subpath); 383 slen = strlen (subpath); 384 if (slen >= SUBPATH_MAX) 385 { 386 GNUNET_break_op (0); 387 return TALER_MHD2_reply_with_error ( 388 request, 389 MHD_HTTP_STATUS_URI_TOO_LONG, 390 TALER_EC_GENERIC_URI_TOO_LONG, 391 subpath); 392 } 393 memcpy (d, 394 subpath, 395 slen + 1); 396 397 i = 0; 398 args[i++] = strtok_r (d, 399 "/", 400 &sp); 401 while ( (NULL != args[i - 1]) && 402 (i < SUBPATH_MAX_SEGMENTS) ) 403 args[i++] = strtok_r (NULL, 404 "/", 405 &sp); 406 407 if (NULL == args[0]) 408 { 409 GNUNET_break_op (0); 410 return TALER_MHD2_reply_with_error ( 411 request, 412 MHD_HTTP_STATUS_NOT_FOUND, 413 TALER_EC_GENERIC_ENDPOINT_UNKNOWN, 414 subpath); 415 } 416 417 if (MHD_HTTP_METHOD_OPTIONS == method) 418 return TALER_MHD2_reply_cors_preflight (request); 419 420 for (unsigned int j = 0; NULL != bhandlers[j].operation; j++) 421 { 422 const struct SH_BackupOperationHandler *bh = &bhandlers[j]; 423 unsigned int nargs; 424 425 if (0 != strcmp (args[0], 426 bh->operation)) 427 continue; 428 if (bh->method != method) 429 continue; 430 431 /* Count path segments after the operation name */ 432 nargs = 0; 433 while (NULL != args[nargs + 1]) 434 nargs++; 435 436 if (bh->nargs_is_upper_bound 437 ? (nargs > bh->nargs) 438 : (nargs != bh->nargs)) 439 { 440 /* Not this handler's shape; there may be another handler for 441 the same (operation, method) with a different number of 442 arguments (e.g. GET blocks vs. GET blocks/reconcile). */ 443 continue; 444 } 445 return bh->handler (request, 446 account_pub, 447 &args[1], 448 upload_size, 449 /* FIXME: replace with PATCH when mhd2 supports it */ 450 MHD_HTTP_METHOD_PUT == bh->method); 451 } 452 453 GNUNET_break_op (0); 454 return TALER_MHD2_reply_with_error ( 455 request, 456 MHD_HTTP_STATUS_NOT_FOUND, 457 TALER_EC_GENERIC_ENDPOINT_UNKNOWN, 458 args[0]); 459 } 460 461 462 /** 463 * A client has requested the given url using the given method 464 * (#MHD_HTTP_METHOD_GET, #MHD_HTTP_METHOD_PUT, 465 * #MHD_HTTP_METHOD_DELETE, #MHD_HTTP_METHOD_POST, etc). The callback 466 * must call MHD callbacks to provide content to give back to the 467 * client. 468 * 469 * @param cls argument given together with the function 470 * pointer when the handler was registered with MHD 471 * @param request the request to handle 472 * @param path the requested uri (without arguments after "?") 473 * @param method the HTTP method used (#MHD_HTTP_METHOD_GET, 474 * #MHD_HTTP_METHOD_PUT, etc.) 475 * @param upload_size the size of the message upload content payload, 476 * #MHD_SIZE_UNKNOWN for chunked uploads (if the final chunk 477 * has not been processed yet) 478 * @return next action 479 */ 480 static const struct MHD_Action * 481 url_handler (void *cls, 482 struct MHD_Request *request, 483 const struct MHD_String *path, 484 enum MHD_HTTP_Method method, 485 uint_fast64_t upload_size) 486 { 487 static struct SH_RequestHandler handlers[] = { 488 /* Landing page, tell humans to go away. */ 489 { 490 .url = "/", 491 .method = MHD_HTTP_METHOD_GET, 492 .handler = &respond_root 493 }, 494 { 495 .url = "/agpl", 496 .method = MHD_HTTP_METHOD_GET, 497 .handler = &respond_agpl, 498 }, 499 { 500 .url = "/config", 501 .method = MHD_HTTP_METHOD_GET, 502 .handler = &SH_handler_config, 503 }, 504 { 505 .url = NULL 506 } 507 }; 508 struct SYNC_AccountPublicKeyP account_pub; 509 510 (void) cls; 511 /* Backup API */ 512 if (0 == strncmp (path->cstr, 513 "/backups/", 514 strlen ("/backups/"))) 515 { 516 const char *ac = path->cstr + strlen ("/backups/"); 517 const char *next_slash; 518 size_t ac_len; 519 520 /* Find end of account key (next '/' or end of string) */ 521 next_slash = strchr (ac, 522 '/'); 523 if (NULL == next_slash) 524 ac_len = strlen (ac); 525 else 526 ac_len = next_slash - ac; 527 528 { 529 /* Bound before we put it on the stack; the segment is 530 client-controlled */ 531 char ac_str[ACCOUNT_KEY_MAX + 1]; 532 533 if (ac_len > ACCOUNT_KEY_MAX) 534 { 535 GNUNET_break_op (0); 536 return TALER_MHD2_reply_with_error ( 537 request, 538 MHD_HTTP_STATUS_BAD_REQUEST, 539 TALER_EC_GENERIC_PARAMETER_MALFORMED, 540 "account public key"); 541 } 542 memcpy (ac_str, 543 ac, 544 ac_len); 545 ac_str[ac_len] = '\0'; 546 if (GNUNET_OK != 547 GNUNET_CRYPTO_eddsa_public_key_from_string ( 548 ac_str, 549 ac_len, 550 &account_pub.eddsa_pub)) 551 { 552 GNUNET_break_op (0); 553 return TALER_MHD2_reply_with_error ( 554 request, 555 MHD_HTTP_STATUS_BAD_REQUEST, 556 TALER_EC_GENERIC_PARAMETER_MALFORMED, 557 ac_str); 558 } 559 } 560 561 /* Without a subpath this is the account itself; only GET makes 562 sense on it, everything else needs an operation */ 563 if ( (NULL == next_slash) || 564 ('\0' == next_slash[1]) ) 565 { 566 if (MHD_HTTP_METHOD_OPTIONS == method) 567 return TALER_MHD2_reply_cors_preflight (request); 568 if (MHD_HTTP_METHOD_GET == method) 569 return SH_backup_account_get (request, 570 &account_pub); 571 GNUNET_break_op (0); 572 return TALER_MHD2_reply_with_error ( 573 request, 574 MHD_HTTP_STATUS_NOT_FOUND, 575 TALER_EC_GENERIC_ENDPOINT_UNKNOWN, 576 path->cstr); 577 } 578 579 /* Dispatch to sub-handler */ 580 return handle_backup_subpath (request, 581 &account_pub, 582 method, 583 next_slash + 1, 584 upload_size); 585 } 586 587 for (unsigned int i = 0; NULL != handlers[i].url; i++) 588 { 589 struct SH_RequestHandler *rh = &handlers[i]; 590 591 if (0 == strcmp (path->cstr, 592 rh->url)) 593 { 594 if (MHD_HTTP_METHOD_OPTIONS == method) 595 { 596 return TALER_MHD2_reply_cors_preflight (request); 597 } 598 if (rh->method == method) 599 { 600 return rh->handler (request, 601 upload_size); 602 } 603 } 604 } 605 return respond_404 (request, 606 upload_size); 607 } 608 609 610 /** 611 * Shutdown task. Invoked when the application is being terminated. 612 * 613 * @param cls NULL 614 */ 615 static void 616 do_shutdown (void *cls) 617 { 618 (void) cls; 619 TALER_MHD2_daemons_halt (); 620 if (NULL != SH_ctx) 621 { 622 GNUNET_CURL_fini (SH_ctx); 623 SH_ctx = NULL; 624 } 625 if (NULL != rc) 626 { 627 GNUNET_CURL_gnunet_rc_destroy (rc); 628 rc = NULL; 629 } 630 TALER_MHD2_daemons_destroy (); 631 SYNCDB_fini (); 632 } 633 634 635 /** 636 * Kick MHD to run now, to be called after MHD_request_resume(). 637 * Basically, we need to explicitly resume MHD's event loop whenever 638 * we made progress serving a request. This function re-schedules 639 * the task processing MHD's activities to run immediately. 640 */ 641 /* FIXME: replace with direct call... */ 642 void 643 SH_trigger_daemon (void) 644 { 645 TALER_MHD2_daemons_trigger (); 646 } 647 648 649 /** 650 * Kick GNUnet Curl scheduler to begin curl interactions. 651 */ 652 void 653 SH_trigger_curl (void) 654 { 655 GNUNET_CURL_gnunet_scheduler_reschedule (&rc); 656 } 657 658 659 /** 660 * Callback invoked on every listen socket to start the 661 * respective MHD HTTP daemon. 662 * 663 * @param cls unused 664 * @param lsock the listen socket 665 */ 666 static void 667 start_daemon (void *cls, 668 int lsock) 669 { 670 struct MHD_Daemon *mhd; 671 672 (void) cls; 673 GNUNET_assert (-1 != lsock); 674 mhd = MHD_daemon_create (&url_handler, 675 NULL); 676 if (NULL == mhd) 677 { 678 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 679 "Failed to launch HTTP service, exiting.\n"); 680 global_ret = EXIT_NO_RESTART; 681 GNUNET_SCHEDULER_shutdown (); 682 return; 683 } 684 /* Without this, the pool defaults to a fraction of the memory the 685 daemon would use for connections -- about 1 MB, far below 686 UPLOAD_LIMIT_MB -- and MHD answers anything bigger with its own 687 413 before the handler ever runs, whatever the configured limit 688 says. See the FIXME in sync-httpd2_post-backups-KEY-blocks-NONCE.c 689 for the incremental-upload alternative that would drop the pool 690 dependency entirely. */ 691 GNUNET_assert (MHD_SC_OK == 692 MHD_DAEMON_SET_OPTIONS ( 693 mhd, 694 MHD_D_OPTION_DEFAULT_TIMEOUT_MILSEC (10000), 695 MHD_D_OPTION_GLOBAL_CONNECTION_LIMIT (CONNECTION_LIMIT), 696 MHD_D_OPTION_LARGE_POOL_SIZE ( 697 (size_t) GNUNET_MIN ( 698 ((uint_fast64_t) SH_upload_limit_mb) 699 * 1024 * 1024 700 * UPLOAD_POOL_CONCURRENCY, 701 (uint_fast64_t) SIZE_MAX)), 702 MHD_D_OPTION_LISTEN_SOCKET (lsock))); 703 have_daemons = true; 704 TALER_MHD2_daemon_start (mhd); 705 } 706 707 708 /** 709 * Main function that will be run by the scheduler. 710 * 711 * @param cls closure 712 * @param args remaining command-line arguments 713 * @param cfgfile name of the configuration file used (for saving, can be 714 * NULL!) 715 * @param config configuration 716 */ 717 static void 718 run (void *cls, 719 char *const *args, 720 const char *cfgfile, 721 const struct GNUNET_CONFIGURATION_Handle *config) 722 { 723 enum TALER_MHD2_GlobalOptions go; 724 725 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 726 "Starting sync-httpd2\n"); 727 go = TALER_MHD2_GO_NONE; 728 if (SH_sync_connection_close) 729 go |= TALER_MHD2_GO_FORCE_CONNECTION_CLOSE; 730 TALER_MHD2_setup (go); 731 global_ret = EXIT_NOTCONFIGURED; 732 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, 733 NULL); 734 if (GNUNET_OK != 735 GNUNET_CONFIGURATION_get_value_number (config, 736 "sync", 737 "UPLOAD_LIMIT_MB", 738 &SH_upload_limit_mb)) 739 { 740 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 741 "sync", 742 "UPLOAD_LIMIT_MB"); 743 GNUNET_SCHEDULER_shutdown (); 744 return; 745 } 746 if (GNUNET_OK != 747 TALER_config_get_amount (config, 748 "sync", 749 "INSURANCE", 750 &SH_insurance)) 751 { 752 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 753 "sync", 754 "INSURANCE"); 755 GNUNET_SCHEDULER_shutdown (); 756 return; 757 } 758 if (GNUNET_OK != 759 TALER_config_get_amount (config, 760 "sync", 761 "ANNUAL_FEE", 762 &SH_annual_fee)) 763 { 764 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 765 "sync", 766 "ANNUAL_FEE"); 767 GNUNET_SCHEDULER_shutdown (); 768 return; 769 } 770 if (GNUNET_OK != 771 GNUNET_CONFIGURATION_get_value_string (config, 772 "sync", 773 "PAYMENT_BACKEND_URL", 774 &SH_backend_url)) 775 { 776 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 777 "sync", 778 "PAYMENT_BACKEND_URL"); 779 GNUNET_SCHEDULER_shutdown (); 780 return; 781 } 782 /* The merchant backend API expects the base URL to end with '/' 783 (see TALER_url_join), so ensure it does. */ 784 if ('/' != SH_backend_url[strlen (SH_backend_url) - 1]) 785 { 786 char *tmp; 787 788 GNUNET_asprintf (&tmp, 789 "%s/", 790 SH_backend_url); 791 GNUNET_free (SH_backend_url); 792 SH_backend_url = tmp; 793 } 794 /* The payment flow turns this into a "taler://" URI, so it needs a 795 scheme and a host */ 796 { 797 const char *host = NULL; 798 799 if (0 == strncasecmp ("https://", 800 SH_backend_url, 801 strlen ("https://"))) 802 host = &SH_backend_url[strlen ("https://")]; 803 else if (0 == strncasecmp ("http://", 804 SH_backend_url, 805 strlen ("http://"))) 806 host = &SH_backend_url[strlen ("http://")]; 807 if ( (NULL == host) || 808 ('\0' == *host) ) 809 { 810 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, 811 "sync", 812 "PAYMENT_BACKEND_URL", 813 "must be an http:// or https:// URL" 814 " with a host name"); 815 GNUNET_SCHEDULER_shutdown (); 816 return; 817 } 818 } 819 if (GNUNET_OK != 820 GNUNET_CONFIGURATION_get_value_string (config, 821 "sync", 822 "FULFILLMENT_URL", 823 &SH_fulfillment_url)) 824 { 825 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 826 "sync", 827 "FULFILLMENT_URL"); 828 GNUNET_SCHEDULER_shutdown (); 829 return; 830 } 831 832 /* setup HTTP client event loop */ 833 SH_ctx = GNUNET_CURL_init (&GNUNET_CURL_gnunet_scheduler_reschedule, 834 &rc); 835 if (NULL == SH_ctx) 836 { 837 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 838 "Failed to initialize HTTP client, exiting.\n"); 839 global_ret = EXIT_NO_RESTART; 840 GNUNET_SCHEDULER_shutdown (); 841 return; 842 } 843 rc = GNUNET_CURL_gnunet_rc_create (SH_ctx); 844 if (NULL != userpass) 845 GNUNET_CURL_set_userpass (SH_ctx, 846 userpass); 847 if (NULL != keyfile) 848 GNUNET_CURL_set_tlscert (SH_ctx, 849 certtype, 850 certfile, 851 keyfile, 852 keypass); 853 if (GNUNET_OK == 854 GNUNET_CONFIGURATION_get_value_string (config, 855 "sync", 856 "API_KEY", 857 &apikey)) 858 { 859 char *auth_header; 860 861 GNUNET_asprintf (&auth_header, 862 "%s: %s", 863 MHD_HTTP_HEADER_AUTHORIZATION, 864 apikey); 865 if (GNUNET_OK != 866 GNUNET_CURL_append_header (SH_ctx, 867 auth_header)) 868 { 869 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 870 "Failed to set %s header, trying without\n", 871 MHD_HTTP_HEADER_AUTHORIZATION); 872 } 873 GNUNET_free (auth_header); 874 } 875 876 if (GNUNET_OK != 877 SYNCDB_init (config)) 878 { 879 global_ret = EXIT_NOTCONFIGURED; 880 GNUNET_SCHEDULER_shutdown (); 881 return; 882 } 883 if (GNUNET_OK != 884 SYNCDB_preflight ()) 885 { 886 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 887 "Database not setup. Did you run sync-dbinit?\n"); 888 GNUNET_SCHEDULER_shutdown (); 889 return; 890 } 891 { 892 enum GNUNET_GenericReturnValue ret; 893 894 ret = TALER_MHD2_listen_bind (config, 895 "sync", 896 &start_daemon, 897 NULL); 898 switch (ret) 899 { 900 case GNUNET_SYSERR: 901 global_ret = EXIT_NOTCONFIGURED; 902 GNUNET_SCHEDULER_shutdown (); 903 return; 904 case GNUNET_NO: 905 if (! have_daemons) 906 { 907 global_ret = EXIT_NOTCONFIGURED; 908 GNUNET_SCHEDULER_shutdown (); 909 return; 910 } 911 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 912 "Could not open all configured listen sockets\n"); 913 break; 914 case GNUNET_OK: 915 break; 916 } 917 } 918 global_ret = EXIT_SUCCESS; 919 } 920 921 922 /** 923 * The main function of the serve tool 924 * 925 * @param argc number of arguments from the command line 926 * @param argv command line arguments 927 * @return 0 ok, 1 on error 928 */ 929 int 930 main (int argc, 931 char *const *argv) 932 { 933 struct GNUNET_GETOPT_CommandLineOption options[] = { 934 GNUNET_GETOPT_option_string ('A', 935 "auth", 936 "USERNAME:PASSWORD", 937 "use the given USERNAME and PASSWORD for client authentication", 938 &userpass), 939 /* Note: -c, -h, -L, -l and -v are reserved by GNUNET_PROGRAM_run(). 940 We use -E for the client certificate, as curl does. */ 941 GNUNET_GETOPT_option_string ('E', 942 "cert", 943 "CERTFILE", 944 "file with the TLS client certificate for TLS client authentication", 945 &certfile), 946 GNUNET_GETOPT_option_flag ('C', 947 "connection-close", 948 "force HTTP connections to be closed after each request", 949 &SH_sync_connection_close), 950 GNUNET_GETOPT_option_string ('k', 951 "key", 952 "KEYFILE", 953 "file with the private TLS key for TLS client authentication", 954 &keyfile), 955 GNUNET_GETOPT_option_string ('p', 956 "pass", 957 "KEYFILEPASSPHRASE", 958 "passphrase needed to decrypt the TLS client private key file", 959 &keypass), 960 GNUNET_GETOPT_option_string ('t', 961 "type", 962 "CERTTYPE", 963 "type of the TLS client certificate, defaults to PEM if not specified", 964 &certtype), 965 GNUNET_GETOPT_OPTION_END 966 }; 967 enum GNUNET_GenericReturnValue ret; 968 969 ret = GNUNET_PROGRAM_run (SYNC_project_data (), 970 argc, argv, 971 "sync-httpd2", 972 "sync HTTP interface", 973 options, 974 &run, NULL); 975 if (GNUNET_NO == ret) 976 return EXIT_SUCCESS; 977 if (GNUNET_SYSERR == ret) 978 return EXIT_INVALIDARGUMENT; 979 return global_ret; 980 }