From: Alberto Leiva Popper Date: Thu, 24 Jun 2021 17:44:55 +0000 (-0500) Subject: Global: Further #49 cleanup X-Git-Tag: v1.5.1~10 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=33b852866b9b014fb7f10b231f01943d690ed44d;p=thirdparty%2FFORT-validator.git Global: Further #49 cleanup --- diff --git a/src/http/http.c b/src/http/http.c index 793d9826..b8bd79dd 100644 --- a/src/http/http.c +++ b/src/http/http.c @@ -141,7 +141,7 @@ curl_err_string(struct http_handler *handler, CURLcode res) */ static int http_fetch(struct http_handler *handler, char const *uri, long *response_code, - long *cond_met, bool log_operation, FILE *file, bool is_ta) + long *cond_met, bool log_operation, FILE *file) { CURLcode res, res2; long unmet = 0; @@ -150,6 +150,7 @@ http_fetch(struct http_handler *handler, char const *uri, long *response_code, setopt_str(handler->curl, CURLOPT_URL, uri); setopt_writedata(handler->curl, file); + pr_val_debug("HTTP GET: %s", uri); res = curl_easy_perform(handler->curl); res2 = curl_easy_getinfo(handler->curl, CURLINFO_RESPONSE_CODE, @@ -205,17 +206,15 @@ http_fetch(struct http_handler *handler, char const *uri, long *response_code, return 0; } - if (*response_code >= HTTP_BAD_REQUEST) { + if (*response_code >= HTTP_BAD_REQUEST) return pr_val_err("Error requesting URL %s (received HTTP code %ld): %s", uri, *response_code, curl_err_string(handler, res)); - } pr_val_err("Error requesting URL %s: %s", uri, curl_err_string(handler, res)); - if (log_operation) { + if (log_operation) pr_op_err("Error requesting URL %s: %s", uri, curl_err_string(handler, res)); - } return EREQFAILED; } @@ -228,7 +227,7 @@ http_easy_cleanup(struct http_handler *handler) static int __http_download_file(struct rpki_uri *uri, long *response_code, long ims_value, - long *cond_met, bool log_operation, bool is_ta) + long *cond_met, bool log_operation) { char const *tmp_suffix = "_tmp"; struct http_handler handler; @@ -280,10 +279,9 @@ __http_download_file(struct rpki_uri *uri, long *response_code, long ims_value, CURL_TIMECOND_IFMODSINCE); } error = http_fetch(&handler, uri_get_global(uri), response_code, - cond_met, log_operation, out, is_ta); - if (error != EREQFAILED) { + cond_met, log_operation, out); + if (error != EREQFAILED) break; /* Note: Usually happy path */ - } if (retries == config_get_http_retry_count()) { pr_val_warn("Max HTTP retries (%u) reached requesting for '%s', won't retry again.", @@ -333,12 +331,12 @@ release_tmp: * request to the server failed. */ int -http_download_file(struct rpki_uri *uri, bool log_operation, bool is_ta) +http_download_file(struct rpki_uri *uri, bool log_operation) { long response; long cond_met; return __http_download_file(uri, &response, 0, &cond_met, - log_operation, is_ta); + log_operation); } /* @@ -363,7 +361,7 @@ http_download_file_with_ims(struct rpki_uri *uri, long value, int error; error = __http_download_file(uri, &response, value, &cond_met, - log_operation, false); + log_operation); if (error) return error; @@ -394,7 +392,7 @@ http_download_file_with_ims(struct rpki_uri *uri, long value, */ return __http_download_file(uri, &response, 0, &cond_met, - log_operation, false); + log_operation); } @@ -438,7 +436,7 @@ http_direct_download(char const *remote, char const *dest) response_code = 0; cond_met = 0; error = http_fetch(&handler, remote, &response_code, &cond_met, true, - out, false); + out); http_easy_cleanup(&handler); file_close(out); if (error) diff --git a/src/http/http.h b/src/http/http.h index 8cb3b0ef..717119d9 100644 --- a/src/http/http.h +++ b/src/http/http.h @@ -9,7 +9,7 @@ int http_init(void); void http_cleanup(void); -int http_download_file(struct rpki_uri *, bool, bool); +int http_download_file(struct rpki_uri *, bool); int http_download_file_with_ims(struct rpki_uri *, long, bool); int http_direct_download(char const *, char const *); diff --git a/src/log.c b/src/log.c index 83293e43..1c54a10d 100644 --- a/src/log.c +++ b/src/log.c @@ -204,9 +204,9 @@ log_setup(void) DBG.stream = stdout; INF.stream = stdout; - WRN.stream = stdout; - ERR.stream = stdout; - CRT.stream = stdout; + WRN.stream = stderr; + ERR.stream = stderr; + CRT.stream = stderr; UNK.stream = stdout; openlog("fort", LOG_CONS | LOG_PID, LOG_DAEMON); @@ -608,6 +608,9 @@ val_crypto_err(const char *format, ...) int pr_enomem(void) { + static char const *ENOMEM_MSG = "Out of memory.\n"; + ssize_t garbage; + /* * I'm not using PR_SIMPLE and friends, because those allocate. * We want to minimize allocations after a memory allocation failure. @@ -616,15 +619,21 @@ pr_enomem(void) if (LOG_ERR > op_config.level) return -ENOMEM; - if (op_config.syslog_enabled) { + if (op_config.fprintf_enabled) { lock_mutex(); - syslog(LOG_ERR | op_config.facility, "Out of memory."); + /* + * write() is AS-Safe, which implies it doesn't allocate, + * unlike printf(). + */ + garbage = write(STDERR_FILENO, ENOMEM_MSG, strlen(ENOMEM_MSG)); unlock_mutex(); + garbage++; } - if (op_config.fprintf_enabled) { + if (op_config.syslog_enabled) { lock_mutex(); - fprintf(stderr, "Out of memory.\n"); + /* This allocates, but I don't think I have more options. */ + syslog(LOG_ERR | op_config.facility, "Out of memory."); unlock_mutex(); } diff --git a/src/main.c b/src/main.c index e60d1491..89cda311 100644 --- a/src/main.c +++ b/src/main.c @@ -40,9 +40,6 @@ main(int argc, char **argv) if (error) goto just_quit; - /* TODO (issue49) don't forget to improve this. */ - pr_op_info("Fort 1.5.0.2"); - error = thvar_init(); if (error) goto revert_log; diff --git a/src/object/tal.c b/src/object/tal.c index a7081510..98d58900 100644 --- a/src/object/tal.c +++ b/src/object/tal.c @@ -541,7 +541,7 @@ handle_tal_uri(struct tal *tal, struct rpki_uri *uri, void *arg) return 0; /* Try some other TAL URI */ } error = http_download_file(uri, - reqs_errors_log_uri(uri_get_global(uri)), true); + reqs_errors_log_uri(uri_get_global(uri))); } /* Reminder: there's a positive error: EREQFAILED */ @@ -552,7 +552,6 @@ handle_tal_uri(struct tal *tal, struct rpki_uri *uri, void *arg) "TAL URI '%s' could not be downloaded.", uri_val_get_printable(uri)); } - } else { /* Look for local files */ if (!valid_file_or_dir(uri_get_local(uri), true, false, diff --git a/src/rrdp/rrdp_parser.c b/src/rrdp/rrdp_parser.c index 6b59ad15..e1c4428c 100644 --- a/src/rrdp/rrdp_parser.c +++ b/src/rrdp/rrdp_parser.c @@ -104,7 +104,7 @@ download_file(struct rpki_uri *uri, long last_update, bool log_operation) error = http_download_file_with_ims(uri, last_update, log_operation); else - error = http_download_file(uri, log_operation, false); + error = http_download_file(uri, log_operation); /* * Since distinct files can be downloaded (notification, snapshot, diff --git a/src/rsync/rsync.c b/src/rsync/rsync.c index bdc7374a..8e5d6080 100644 --- a/src/rsync/rsync.c +++ b/src/rsync/rsync.c @@ -4,6 +4,7 @@ #include #include #include /* SIGINT, SIGQUIT, etc */ +#include #include #include #include @@ -373,6 +374,7 @@ do_rsync(struct rpki_uri *uri, bool is_ta, bool log_operation) int fork_fds[2][2]; pid_t child_pid; unsigned int retries; + unsigned int i; int child_status; int error; @@ -383,6 +385,12 @@ do_rsync(struct rpki_uri *uri, bool is_ta, bool log_operation) if (error) return error; + if (log_val_enabled(LOG_DEBUG)) { + pr_val_debug("Executing RSYNC:"); + for (i = 0; i < args_len + 1; i++) + pr_val_debug(" %s", args[i]); + } + retries = 0; do { child_status = 0; @@ -586,9 +594,8 @@ rsync_download_files(struct rpki_uri *requested_uri, bool is_ta, bool force) error = get_rsync_uri(requested_uri, is_ta, &rsync_uri); } else { error = check_ancestor_error(requested_uri); - if (error) { + if (error) return error; - } error = handle_strict_strategy(requested_uri, &rsync_uri); } diff --git a/test/Makefile.am b/test/Makefile.am index c3539527..acba67a6 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -22,7 +22,6 @@ MY_LDADD = ${CHECK_LIBS} check_PROGRAMS = address.test check_PROGRAMS += clients.test check_PROGRAMS += db_table.test -check_PROGRAMS += http.test check_PROGRAMS += line_file.test check_PROGRAMS += pdu_handler.test check_PROGRAMS += rsync.test @@ -44,9 +43,6 @@ clients_test_LDADD = ${MY_LDADD} db_table_test_SOURCES = rtr/db/db_table_test.c db_table_test_LDADD = ${MY_LDADD} -http_test_SOURCES = http_test.c -http_test_LDADD = ${MY_LDADD} ${CURL_LIBS} - line_file_test_SOURCES = line_file_test.c line_file_test_LDADD = ${MY_LDADD} diff --git a/test/http_test.c b/test/http_test.c deleted file mode 100644 index affe9327..00000000 --- a/test/http_test.c +++ /dev/null @@ -1,112 +0,0 @@ -#include -#include -#include - -#include "common.c" -#include "file.c" -#include "impersonator.c" -#include "log.c" -#include "uri.c" -#include "http/http.c" - -struct response { - unsigned char *content; - size_t size; -}; - -static void -init_response(struct response *resp) -{ - resp->size = 0; - resp->content = malloc(sizeof(char)); -} - -static size_t -write_buf_cb(unsigned char *content, size_t size, size_t nmemb, void *arg) -{ - struct response *resp = arg; - unsigned char *tmp; - size_t read = size * nmemb; - - tmp = realloc(resp->content, resp->size + read + 1); - if (tmp == NULL) - return -EINVAL; - - resp->content = tmp; - memcpy(&resp->content[resp->size], content, read); - resp->size += read; - resp->content[resp->size] = 0; - - return read; -} - -static int -local_download(char const *url, long *response_code, struct response *resp) -{ - struct http_handler handler; - long cond; - int error; - - cond = 0; - error = http_easy_init(&handler); - if (error) - return error; - - error = http_fetch(&handler, url, response_code, &cond, false, - write_buf_cb, resp); - http_easy_cleanup(&handler); - return error; -} - -START_TEST(http_fetch_normal) -{ - struct response resp; - long response_code; - char const *url = "https://rrdp.ripe.net/notification.xml"; - - init_response(&resp); - response_code = 0; - - ck_assert_int_eq(http_init(), 0); - ck_assert_int_eq(local_download(url, &response_code, &resp), 0); - ck_assert_int_gt(resp.size, 0); - - http_cleanup(); - free(resp.content); - if (response_code == 0) - ck_abort_msg("NO response code received"); - else if (response_code >= HTTP_BAD_REQUEST) - ck_abort_msg("Received response code %ld", response_code); -} -END_TEST - -Suite *http_load_suite(void) -{ - Suite *suite; - TCase *fetch; - - fetch = tcase_create("Fetch"); - tcase_add_test(fetch, http_fetch_normal); - tcase_set_timeout(fetch, 60); - - suite = suite_create("http_test()"); - suite_add_tcase(suite, fetch); - - return suite; -} - -int main(void) -{ - Suite *suite; - SRunner *runner; - int tests_failed; - - suite = http_load_suite(); - - runner = srunner_create(suite); - srunner_run_all(runner, CK_NORMAL); - tests_failed = srunner_ntests_failed(runner); - srunner_free(runner); - - return (tests_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; -} diff --git a/test/impersonator.c b/test/impersonator.c index 6684e2eb..a98039b4 100644 --- a/test/impersonator.c +++ b/test/impersonator.c @@ -67,12 +67,6 @@ config_get_shuffle_tal_uris(void) return false; } -unsigned int -config_get_max_cert_depth(void) -{ - return 32; -} - char const * config_get_local_repository(void) { @@ -97,49 +91,12 @@ config_get_rsync_priority(void) return rsync_priority; } -bool -config_get_http_enabled(void) -{ - return false; -} - unsigned int config_get_http_priority(void) { return http_priority; } -bool -config_get_work_offline(void) -{ - return false; -} - -bool -config_get_color_output(void) -{ - return false; -} - -enum filename_format -config_get_filename_format(void) -{ - return FNF_NAME; -} - -char * -config_get_rsync_program(void) -{ - return "rsync"; -} - -struct string_array const * -config_get_rsync_args(bool is_ta) -{ - static const struct string_array array = { 0 }; - return &array; -} - char const * config_get_slurm(void) { @@ -248,49 +205,12 @@ config_get_val_log_facility(void) return (3<<3); /* LOG_DAEMON */ } -unsigned int -config_get_asn1_decode_max_stack(void) -{ - return 4096; -} - enum incidence_action incidence_get_action(enum incidence_id id) { return INAC_ERROR; } -/* Impersonate HTTP config */ -char const * -config_get_http_user_agent(void) -{ - return "fort-test/0.1"; -} - -unsigned int -config_get_http_connect_timeout(void) -{ - return 30; -} - -unsigned int -config_get_http_transfer_timeout(void) -{ - return 0; -} - -unsigned int -config_get_http_idle_timeout(void) -{ - return 15; -} - -char const * -config_get_http_ca_path(void) -{ - return NULL; -} - void config_set_rsync_priority(unsigned int value) {