challenger-httpd.c (27535B)
1 /* 2 This file is part of Challenger 3 (C) 2023, 2024, 2025 Taler Systems SA 4 5 Challenger 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 Challenger 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 Challenger; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file challenger/challenger-httpd.c 18 * @brief OAuth 2.0 server challenging users to demonstrate ability to receive messages 19 * @author Christian Grothoff 20 */ 21 #include "platform.h" 22 #include <microhttpd.h> 23 #include <gnunet/gnunet_util_lib.h> 24 #include <gnunet/gnunet_curl_lib.h> 25 #include "challenger_util.h" 26 #include "challenger-httpd.h" 27 #include "challenger-httpd_agpl.h" 28 #include "challenger-httpd_authorize.h" 29 #include "challenger-httpd_challenge.h" 30 #include "challenger-httpd_config.h" 31 #include "challenger-httpd_info.h" 32 #include "challenger-httpd_setup.h" 33 #include "challenger-httpd_solve.h" 34 #include "challenger-httpd_token.h" 35 #include "challenger-httpd_spa.h" 36 #include "challenger_database_lib.h" 37 #include "challenger-database/preflight.h" 38 39 40 /** 41 * Should a "Connection: close" header be added to each HTTP response? 42 */ 43 static int CH_challenger_connection_close; 44 45 /** 46 * Our context for making HTTP requests. 47 */ 48 struct GNUNET_CURL_Context *CH_ctx; 49 50 /** 51 * Reschedule context for #CH_ctx. 52 */ 53 static struct GNUNET_CURL_RescheduleContext *rc; 54 55 /** 56 * Global return code 57 */ 58 static int global_ret; 59 60 /** 61 * True if we started any HTTP daemon. 62 */ 63 static bool have_daemons; 64 65 struct CHALLENGERDB_PostgresContext *CH_context; 66 67 char *CH_base_url; 68 69 void *CH_message_template; 70 71 size_t CH_message_template_len; 72 73 struct GNUNET_TIME_Relative CH_validation_duration; 74 75 struct GNUNET_TIME_Relative CH_validation_expiration; 76 77 struct GNUNET_TIME_Relative CH_token_expiration; 78 79 struct GNUNET_TIME_Relative CH_pin_retransmission_frequency; 80 81 json_t *CH_restrictions; 82 83 char *CH_address_type; 84 85 char *CH_address_hint; 86 87 char *CH_auth_command; 88 89 char *CH_spa_dir; 90 91 92 /** 93 * Function called first by MHD with the full URL. 94 * 95 * @param cls NULL 96 * @param full_url the full URL 97 * @param con MHD connection object 98 * @return our handler context 99 */ 100 static void * 101 full_url_track_callback (void *cls, 102 const char *full_url, 103 struct MHD_Connection *con) 104 { 105 struct CH_HandlerContext *hc; 106 107 hc = GNUNET_new (struct CH_HandlerContext); 108 GNUNET_async_scope_fresh (&hc->async_scope_id); 109 GNUNET_SCHEDULER_begin_async_scope (&hc->async_scope_id); 110 hc->full_url = GNUNET_strdup (full_url); 111 return hc; 112 } 113 114 115 /** 116 * A client has requested the given url using the given method 117 * (#MHD_HTTP_METHOD_GET, #MHD_HTTP_METHOD_PUT, 118 * #MHD_HTTP_METHOD_DELETE, #MHD_HTTP_METHOD_POST, etc). The callback 119 * must call MHD callbacks to provide content to give back to the 120 * client and return an HTTP status code (i.e. #MHD_HTTP_OK, 121 * #MHD_HTTP_NOT_FOUND, etc.). 122 * 123 * @param cls argument given together with the function 124 * pointer when the handler was registered with MHD 125 * @param connection connection handle 126 * @param url the requested url 127 * @param method the HTTP method used (#MHD_HTTP_METHOD_GET, 128 * #MHD_HTTP_METHOD_PUT, etc.) 129 * @param version the HTTP version string (i.e. 130 * #MHD_HTTP_VERSION_1_1) 131 * @param upload_data the data being uploaded (excluding HEADERS, 132 * for a POST that fits into memory and that is encoded 133 * with a supported encoding, the POST data will NOT be 134 * given in upload_data and is instead available as 135 * part of #MHD_get_connection_values; very large POST 136 * data *will* be made available incrementally in 137 * @a upload_data) 138 * @param upload_data_size set initially to the size of the 139 * @a upload_data provided; the method must update this 140 * value to the number of bytes NOT processed; 141 * @param con_cls pointer that the callback can set to some 142 * address and that will be preserved by MHD for future 143 * calls for this request; since the access handler may 144 * be called many times (i.e., for a PUT/POST operation 145 * with plenty of upload data) this allows the application 146 * to easily associate some request-specific state. 147 * If necessary, this state can be cleaned up in the 148 * global #MHD_RequestCompletedCallback (which 149 * can be set with the #MHD_OPTION_NOTIFY_COMPLETED). 150 * Initially, `*con_cls` will be NULL. 151 * @return #MHD_YES if the connection was handled successfully, 152 * #MHD_NO if the socket must be closed due to a serious 153 * error while handling the request 154 */ 155 static enum MHD_Result 156 url_handler (void *cls, 157 struct MHD_Connection *connection, 158 const char *url, 159 const char *method, 160 const char *version, 161 const char *upload_data, 162 size_t *upload_data_size, 163 void **con_cls) 164 { 165 static struct CH_RequestHandler handlers[] = { 166 /* Landing page, redirects the user-agent to the SPA at /webui/. */ 167 { 168 .url = "/", 169 .method = MHD_HTTP_METHOD_GET, 170 .handler = &CH_spa_redirect 171 }, 172 { 173 .url = "/webui/", 174 .method = MHD_HTTP_METHOD_GET, 175 .handler = &CH_handler_spa, 176 }, 177 { 178 .url = "/agpl", 179 .method = MHD_HTTP_METHOD_GET, 180 .handler = &CH_handler_agpl 181 }, 182 { 183 .url = "/config", 184 .method = MHD_HTTP_METHOD_GET, 185 .handler = &CH_handler_config 186 }, 187 { 188 .url = "/setup/", 189 .method = MHD_HTTP_METHOD_POST, 190 .handler = &CH_handler_setup 191 }, 192 { 193 .url = "/authorize/", 194 .method = MHD_HTTP_METHOD_GET, 195 .handler = &CH_handler_authorize 196 }, 197 { 198 .url = "/authorize/", 199 .method = MHD_HTTP_METHOD_POST, 200 .handler = &CH_handler_authorize 201 }, 202 { 203 .url = "/challenge/", 204 .method = MHD_HTTP_METHOD_POST, 205 .handler = &CH_handler_challenge 206 }, 207 { 208 .url = "/solve/", 209 .method = MHD_HTTP_METHOD_POST, 210 .handler = &CH_handler_solve 211 }, 212 { 213 .url = "/token", 214 .method = MHD_HTTP_METHOD_POST, 215 .handler = &CH_handler_token 216 }, 217 { 218 .url = "/info", 219 .method = MHD_HTTP_METHOD_GET, 220 .handler = &CH_handler_info 221 }, 222 { 223 NULL, NULL, NULL 224 } 225 }; 226 struct CH_HandlerContext *hc = *con_cls; 227 228 (void) cls; 229 (void) version; 230 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 231 "Handling %s request for `%s'\n", 232 method, 233 url); 234 if (NULL == hc->connection) 235 { 236 const char *correlation_id; 237 bool found = false; 238 239 hc->connection = connection; 240 /* We only read the correlation ID on the first callback for every client */ 241 correlation_id = MHD_lookup_connection_value (connection, 242 MHD_HEADER_KIND, 243 "Challenger-Correlation-Id"); 244 if ( (NULL != correlation_id) && 245 (GNUNET_YES != GNUNET_CURL_is_valid_scope_id (correlation_id)) ) 246 { 247 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 248 "Illegal incoming correlation ID\n"); 249 correlation_id = NULL; 250 } 251 if (NULL != correlation_id) 252 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 253 "Handling request for (%s) URL '%s', correlation_id=%s\n", 254 method, 255 url, 256 correlation_id); 257 else 258 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 259 "Handling request (%s) for URL '%s'\n", 260 method, 261 url); 262 263 for (unsigned int i = 0; NULL != handlers[i].url; i++) 264 { 265 struct CH_RequestHandler *rh = &handlers[i]; 266 267 if ( (0 == strcmp (url, 268 rh->url)) || 269 ( (0 == strncmp (url, 270 rh->url, 271 strlen (rh->url))) && 272 (1 < strlen (rh->url)) && 273 ('/' == rh->url[strlen (rh->url) - 1]) ) ) 274 { 275 found = true; 276 if (0 == strcasecmp (method, 277 MHD_HTTP_METHOD_OPTIONS)) 278 { 279 return TALER_MHD_reply_cors_preflight (connection); 280 } 281 if (0 == strcasecmp (method, 282 rh->method)) 283 { 284 hc->rh = rh; 285 break; 286 } 287 if ( (0 == strcasecmp (method, 288 MHD_HTTP_METHOD_HEAD)) && 289 (0 == strcasecmp (rh->method, 290 MHD_HTTP_METHOD_GET)) ) 291 { 292 hc->rh = rh; 293 break; 294 } 295 } 296 } 297 if (NULL == hc->rh) 298 { 299 if (found) 300 { 301 char allow[128] = "OPTIONS"; 302 size_t aoff = strlen ("OPTIONS"); 303 struct MHD_Response *resp; 304 enum MHD_Result ret; 305 306 GNUNET_break_op (0); 307 /* Build Allow header: OPTIONS is always supported (handled above), 308 plus every method registered for this URL, plus HEAD wherever 309 GET is registered */ 310 for (unsigned int j = 0; NULL != handlers[j].url; j++) 311 { 312 const struct CH_RequestHandler *rh2 = &handlers[j]; 313 314 if ( (0 != strcmp (url, rh2->url)) && 315 ! ( (0 == strncmp (url, rh2->url, strlen (rh2->url))) && 316 (1 < strlen (rh2->url)) && 317 ('/' == rh2->url[strlen (rh2->url) - 1]) ) ) 318 continue; 319 GNUNET_assert (aoff + strlen (rh2->method) + 3 < sizeof (allow)); 320 memcpy (&allow[aoff], ", ", 2); 321 aoff += 2; 322 memcpy (&allow[aoff], rh2->method, strlen (rh2->method)); 323 aoff += strlen (rh2->method); 324 allow[aoff] = '\0'; 325 if (0 != strcasecmp (rh2->method, 326 MHD_HTTP_METHOD_GET)) 327 continue; 328 GNUNET_assert (aoff + strlen (MHD_HTTP_METHOD_HEAD) + 3 329 < sizeof (allow)); 330 memcpy (&allow[aoff], ", ", 2); 331 aoff += 2; 332 memcpy (&allow[aoff], 333 MHD_HTTP_METHOD_HEAD, 334 strlen (MHD_HTTP_METHOD_HEAD)); 335 aoff += strlen (MHD_HTTP_METHOD_HEAD); 336 allow[aoff] = '\0'; 337 } 338 339 resp = MHD_create_response_from_buffer (0, 340 NULL, 341 MHD_RESPMEM_PERSISTENT); 342 TALER_MHD_add_global_headers (resp, 343 false); 344 GNUNET_break (MHD_YES == 345 MHD_add_response_header (resp, 346 MHD_HTTP_HEADER_ALLOW, 347 allow)); 348 ret = MHD_queue_response (hc->connection, 349 MHD_HTTP_METHOD_NOT_ALLOWED, 350 resp); 351 MHD_destroy_response (resp); 352 return ret; 353 } 354 GNUNET_break_op (0); 355 return TALER_MHD_reply_static (hc->connection, 356 MHD_HTTP_NOT_FOUND, 357 "text/plain", 358 NULL, 359 0); 360 } 361 } 362 else 363 { 364 GNUNET_SCHEDULER_begin_async_scope (&hc->async_scope_id); 365 } 366 GNUNET_assert (NULL != hc->rh); 367 hc->path = &url[strlen (hc->rh->url)]; 368 return hc->rh->handler (hc, 369 upload_data, 370 upload_data_size); 371 } 372 373 374 /** 375 * Shutdown task. Invoked when the application is being terminated. 376 * 377 * @param cls NULL 378 */ 379 static void 380 do_shutdown (void *cls) 381 { 382 (void) cls; 383 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 384 "Stopping challenger-httpd\n"); 385 TALER_MHD_daemons_halt (); 386 CH_wakeup_challenge_on_shutdown (); 387 if ( (NULL != CH_message_template) && 388 (MAP_FAILED != CH_message_template) ) 389 { 390 if (0 != 391 munmap (CH_message_template, 392 CH_message_template_len)) 393 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, 394 "munmap"); 395 CH_message_template = NULL; 396 CH_message_template_len = 0; 397 } 398 TALER_MHD_daemons_destroy (); 399 if (NULL != CH_ctx) 400 { 401 GNUNET_CURL_fini (CH_ctx); 402 CH_ctx = NULL; 403 } 404 if (NULL != rc) 405 { 406 GNUNET_CURL_gnunet_rc_destroy (rc); 407 rc = NULL; 408 } 409 if (NULL != CH_context) 410 { 411 CHALLENGERDB_disconnect (CH_context); 412 CH_context = NULL; 413 } 414 } 415 416 417 /** 418 * Function called whenever MHD is done with a request. If the 419 * request was a POST, we may have stored a `struct Buffer *` in the 420 * @a con_cls that might still need to be cleaned up. Call the 421 * respective function to free the memory. 422 * 423 * @param cls client-defined closure 424 * @param connection connection handle 425 * @param con_cls value as set by the last call to 426 * the #MHD_AccessHandlerCallback 427 * @param toe reason for request termination 428 * @see #MHD_OPTION_NOTIFY_COMPLETED 429 * @ingroup request 430 */ 431 static void 432 handle_mhd_completion_callback (void *cls, 433 struct MHD_Connection *connection, 434 void **con_cls, 435 enum MHD_RequestTerminationCode toe) 436 { 437 struct CH_HandlerContext *hc = *con_cls; 438 const union MHD_ConnectionInfo *ci; 439 440 (void) cls; 441 if (NULL == hc) 442 return; 443 GNUNET_assert ( (hc->connection == connection) || 444 (NULL == hc->connection) ); 445 GNUNET_break (GNUNET_OK == 446 CHALLENGERDB_preflight (CH_context)); 447 ci = MHD_get_connection_info (connection, 448 MHD_CONNECTION_INFO_HTTP_STATUS); 449 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 450 "Finished handling request with status %d (HTTP status %u)\n", 451 (int) toe, 452 (NULL != ci) ? ci->http_status : 0); 453 if (NULL != hc->cc) 454 hc->cc (hc->ctx); 455 GNUNET_free (hc->full_url); 456 GNUNET_free (hc); 457 *con_cls = NULL; 458 } 459 460 461 /** 462 * Kick MHD to run now, to be called after MHD_resume_connection(). 463 * Basically, we need to explicitly resume MHD's event loop whenever 464 * we made progress serving a request. This function re-schedules 465 * the task processing MHD's activities to run immediately. 466 * 467 * FIXME: replace by direct call... 468 */ 469 void 470 CH_trigger_daemon () 471 { 472 TALER_MHD_daemon_trigger (); 473 } 474 475 476 /** 477 * Kick GNUnet Curl scheduler to begin curl interactions. 478 */ 479 void 480 CH_trigger_curl () 481 { 482 GNUNET_CURL_gnunet_scheduler_reschedule (&rc); 483 } 484 485 486 /** 487 * Callback invoked on every listen socket to start the 488 * respective MHD HTTP daemon. 489 * 490 * @param cls unused 491 * @param lsock the listen socket 492 */ 493 static void 494 start_daemon (void *cls, 495 int lsock) 496 { 497 struct MHD_Daemon *mhd; 498 499 (void) cls; 500 GNUNET_assert (-1 != lsock); 501 mhd = MHD_start_daemon (MHD_USE_SUSPEND_RESUME | MHD_USE_DUAL_STACK, 502 0 /* port */, 503 NULL, NULL, 504 &url_handler, NULL, 505 MHD_OPTION_LISTEN_SOCKET, lsock, 506 MHD_OPTION_URI_LOG_CALLBACK, 507 &full_url_track_callback, NULL, 508 MHD_OPTION_NOTIFY_COMPLETED, 509 &handle_mhd_completion_callback, NULL, 510 MHD_OPTION_CONNECTION_TIMEOUT, 511 (unsigned int) 10 /* 10s */, 512 MHD_OPTION_END); 513 if (NULL == mhd) 514 { 515 global_ret = EXIT_FAILURE; 516 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 517 "Failed to launch HTTP daemon.\n"); 518 GNUNET_SCHEDULER_shutdown (); 519 return; 520 } 521 have_daemons = true; 522 TALER_MHD_daemon_start (mhd); 523 } 524 525 526 /** 527 * Main function that will be run by the scheduler. 528 * 529 * @param cls closure 530 * @param args remaining command-line arguments 531 * @param cfgfile name of the configuration file used (for saving, can be 532 * NULL!) 533 * @param config configuration 534 */ 535 static void 536 run (void *cls, 537 char *const *args, 538 const char *cfgfile, 539 const struct GNUNET_CONFIGURATION_Handle *config) 540 { 541 enum TALER_MHD_GlobalOptions go; 542 543 (void) cls; 544 (void) args; 545 (void) cfgfile; 546 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 547 "Starting challenger-httpd\n"); 548 go = TALER_MHD_GO_NONE; 549 if (CH_challenger_connection_close) 550 go |= TALER_MHD_GO_FORCE_CONNECTION_CLOSE; 551 TALER_MHD_setup (go); 552 553 if (GNUNET_OK != 554 GNUNET_CONFIGURATION_get_value_time (config, 555 "CHALLENGER", 556 "VALIDATION_DURATION", 557 &CH_validation_duration)) 558 { 559 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 560 "CHALLENGER", 561 "VALIDATION_DURATION"); 562 global_ret = EXIT_NOTCONFIGURED; 563 GNUNET_SCHEDULER_shutdown (); 564 return; 565 } 566 if (GNUNET_OK != 567 GNUNET_CONFIGURATION_get_value_time (config, 568 "CHALLENGER", 569 "VALIDATION_EXPIRATION", 570 &CH_validation_expiration)) 571 { 572 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 573 "CHALLENGER", 574 "VALIDATION_EXPIRATION"); 575 global_ret = EXIT_NOTCONFIGURED; 576 GNUNET_SCHEDULER_shutdown (); 577 return; 578 } 579 if (GNUNET_OK != 580 GNUNET_CONFIGURATION_get_value_time (config, 581 "CHALLENGER", 582 "TOKEN_EXPIRATION", 583 &CH_token_expiration)) 584 { 585 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING, 586 "CHALLENGER", 587 "TOKEN_EXPIRATION"); 588 CH_token_expiration = GNUNET_TIME_UNIT_HOURS; 589 } 590 if (GNUNET_OK != 591 GNUNET_CONFIGURATION_get_value_time (config, 592 "CHALLENGER", 593 "PIN_RETRANSMISSION_FREQUENCY", 594 &CH_pin_retransmission_frequency)) 595 { 596 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING, 597 "CHALLENGER", 598 "PIN_RETRANSMISSION_FREQUENCY"); 599 CH_pin_retransmission_frequency 600 = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 601 5); 602 } 603 if (GNUNET_OK != 604 GNUNET_CONFIGURATION_get_value_string (config, 605 "CHALLENGER", 606 "AUTH_COMMAND", 607 &CH_auth_command)) 608 { 609 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 610 "CHALLENGER", 611 "AUTH_COMMAND"); 612 global_ret = EXIT_NOTCONFIGURED; 613 GNUNET_SCHEDULER_shutdown (); 614 return; 615 } 616 if (GNUNET_OK != 617 GNUNET_CONFIGURATION_get_value_string (config, 618 "CHALLENGER", 619 "ADDRESS_TYPE", 620 &CH_address_type)) 621 { 622 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 623 "CHALLENGER", 624 "ADDRESS_TYPE"); 625 global_ret = EXIT_NOTCONFIGURED; 626 GNUNET_SCHEDULER_shutdown (); 627 return; 628 } 629 if (GNUNET_OK != 630 GNUNET_CONFIGURATION_get_value_string (config, 631 "CHALLENGER", 632 "ADDRESS_HINT", 633 &CH_address_hint)) 634 { 635 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING, 636 "CHALLENGER", 637 "ADDRESS_HINT"); 638 CH_address_hint = GNUNET_strdup (""); 639 } 640 if (GNUNET_OK != 641 GNUNET_CONFIGURATION_get_value_string (config, 642 "CHALLENGER", 643 "BASE_URL", 644 &CH_base_url)) 645 { 646 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 647 "CHALLENGER", 648 "BASE_URL"); 649 global_ret = EXIT_NOTCONFIGURED; 650 GNUNET_SCHEDULER_shutdown (); 651 return; 652 } 653 if ( (! TALER_url_valid_charset (CH_base_url)) || 654 (! TALER_is_web_url (CH_base_url) ) || 655 (0 == strlen (CH_base_url)) || 656 ('/' != CH_base_url[strlen (CH_base_url) - 1]) ) 657 { 658 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, 659 "CHALLENGER", 660 "BASE_URL", 661 "invalid URL"); 662 global_ret = EXIT_NOTCONFIGURED; 663 GNUNET_SCHEDULER_shutdown (); 664 return; 665 } 666 { 667 char *tmpl_file; 668 int fd; 669 struct stat s; 670 671 if (GNUNET_OK != 672 GNUNET_CONFIGURATION_get_value_filename (config, 673 "CHALLENGER", 674 "MESSAGE_TEMPLATE_FILE", 675 &tmpl_file)) 676 { 677 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING, 678 "CHALLENGER", 679 "MESSAGE_TEMPLATE_FILE"); 680 } 681 else 682 { 683 fd = open (tmpl_file, 684 O_RDONLY); 685 GNUNET_free (tmpl_file); 686 if (-1 == fd) 687 { 688 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, 689 "CHALLENGER", 690 "MESSAGE_TEMPLATE_FILE", 691 strerror (errno)); 692 global_ret = EXIT_NOTCONFIGURED; 693 GNUNET_SCHEDULER_shutdown (); 694 return; 695 } 696 if (0 != fstat (fd, 697 &s)) 698 { 699 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, 700 "CHALLENGER", 701 "MESSAGE_TEMPLATE_FILE", 702 strerror (errno)); 703 GNUNET_break (0 == close (fd)); 704 global_ret = EXIT_NOTCONFIGURED; 705 GNUNET_SCHEDULER_shutdown (); 706 return; 707 } 708 CH_message_template_len = (size_t) s.st_size; 709 CH_message_template = mmap (NULL, 710 CH_message_template_len, 711 PROT_READ, 712 MAP_SHARED, 713 fd, 714 0); 715 GNUNET_break (0 == close (fd)); 716 if (MAP_FAILED == CH_message_template) 717 { 718 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, 719 "CHALLENGER", 720 "MESSAGE_TEMPLATE_FILE", 721 strerror (errno)); 722 global_ret = EXIT_NOTCONFIGURED; 723 GNUNET_SCHEDULER_shutdown (); 724 return; 725 } 726 } 727 } 728 729 { 730 char *restrictions; 731 732 if (GNUNET_OK == 733 GNUNET_CONFIGURATION_get_value_string (config, 734 "CHALLENGER", 735 "ADDRESS_RESTRICTIONS", 736 &restrictions)) 737 { 738 json_error_t err; 739 740 CH_restrictions = json_loads (restrictions, 741 JSON_REJECT_DUPLICATES, 742 &err); 743 GNUNET_free (restrictions); 744 if (NULL == CH_restrictions) 745 { 746 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, 747 "CHALLENGER", 748 "ADDRESS_RESTRICTIONS", 749 err.text); 750 global_ret = EXIT_NOTCONFIGURED; 751 GNUNET_SCHEDULER_shutdown (); 752 return; 753 } 754 if (! json_is_object (CH_restrictions)) 755 { 756 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, 757 "CHALLENGER", 758 "ADDRESS_RESTRICTIONS", 759 "must be a JSON object"); 760 global_ret = EXIT_NOTCONFIGURED; 761 GNUNET_SCHEDULER_shutdown (); 762 return; 763 } 764 } 765 else 766 { 767 CH_restrictions = json_object (); 768 GNUNET_assert (NULL != CH_restrictions); 769 } 770 } 771 772 if (GNUNET_OK == 773 GNUNET_CONFIGURATION_get_value_filename (config, 774 "challenger", 775 "SPA_DIR", 776 &CH_spa_dir)) 777 { 778 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 779 "Loading challenger SPA from %s\n", 780 CH_spa_dir); 781 } 782 else 783 { 784 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 785 "Loading challenger SPA from default location\n"); 786 } 787 788 if (GNUNET_OK != 789 CH_spa_init ()) 790 { 791 global_ret = EXIT_NOTCONFIGURED; 792 GNUNET_SCHEDULER_shutdown (); 793 return; 794 } 795 796 797 global_ret = EXIT_NOTCONFIGURED; 798 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, 799 NULL); 800 /* setup HTTP client event loop */ 801 CH_ctx = GNUNET_CURL_init (&GNUNET_CURL_gnunet_scheduler_reschedule, 802 &rc); 803 rc = GNUNET_CURL_gnunet_rc_create (CH_ctx); 804 if (NULL == 805 (CH_context = CHALLENGERDB_connect (config))) 806 { 807 global_ret = EXIT_FAILURE; 808 GNUNET_SCHEDULER_shutdown (); 809 return; 810 } 811 global_ret = EXIT_SUCCESS; 812 { 813 enum GNUNET_GenericReturnValue ret; 814 815 ret = TALER_MHD_listen_bind (config, 816 "challenger", 817 &start_daemon, 818 NULL); 819 switch (ret) 820 { 821 case GNUNET_SYSERR: 822 global_ret = EXIT_NOTCONFIGURED; 823 GNUNET_SCHEDULER_shutdown (); 824 return; 825 case GNUNET_NO: 826 if (! have_daemons) 827 { 828 global_ret = EXIT_FAILURE; 829 GNUNET_SCHEDULER_shutdown (); 830 return; 831 } 832 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 833 "Could not open all configured listen sockets\n"); 834 break; 835 case GNUNET_OK: 836 break; 837 } 838 } 839 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 840 "Challenger-httpd ready\n"); 841 } 842 843 844 /** 845 * The main function of the serve tool 846 * 847 * @param argc number of arguments from the command line 848 * @param argv command line arguments 849 * @return 0 ok, 1 on error 850 */ 851 int 852 main (int argc, 853 char *const *argv) 854 { 855 struct GNUNET_GETOPT_CommandLineOption options[] = { 856 GNUNET_GETOPT_option_flag ('C', 857 "connection-close", 858 "force HTTP connections to be closed after each request", 859 &CH_challenger_connection_close), 860 GNUNET_GETOPT_OPTION_END 861 }; 862 enum GNUNET_GenericReturnValue ret; 863 864 ret = GNUNET_PROGRAM_run (CHALLENGER_project_data (), 865 argc, argv, 866 "challenger-httpd", 867 "challenger REST and OAuth 2.0 API", 868 options, 869 &run, NULL); 870 if (GNUNET_NO == ret) 871 return EXIT_SUCCESS; 872 if (GNUNET_SYSERR == ret) 873 return EXIT_NOTCONFIGURED; 874 return global_ret; 875 }