diff options
author | Dr. David von Oheimb <David.von.Oheimb@siemens.com> | 2021-07-13 10:20:38 +0200 |
---|---|---|
committer | Dr. David von Oheimb <dev@ddvo.net> | 2021-11-22 15:38:39 +0100 |
commit | 4599ea9fe31953c0c50738ed4b91ade76a693356 (patch) | |
tree | 6547f15e6b28a22e2383c496bb102f8f4de66b49 /apps/lib | |
parent | SSL_export_keying_material: fix return check (diff) | |
download | openssl-4599ea9fe31953c0c50738ed4b91ade76a693356.tar.xz openssl-4599ea9fe31953c0c50738ed4b91ade76a693356.zip |
Fix HTTP server port output and allow dynamic verbosity setting
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/16061)
Diffstat (limited to 'apps/lib')
-rw-r--r-- | apps/lib/http_server.c | 32 | ||||
-rw-r--r-- | apps/lib/s_socket.c | 39 |
2 files changed, 51 insertions, 20 deletions
diff --git a/apps/lib/http_server.c b/apps/lib/http_server.c index 8f654660b4..e531201d17 100644 --- a/apps/lib/http_server.c +++ b/apps/lib/http_server.c @@ -216,18 +216,27 @@ void spawn_loop(const char *prog) #endif #ifndef OPENSSL_NO_SOCK -BIO *http_server_init_bio(const char *prog, const char *port) +BIO *http_server_init(const char *prog, const char *port, int verb) { BIO *acbio = NULL, *bufbio; int asock; + int port_num; + if (verb >= 0) { + if (verb > LOG_TRACE) { + log_message(prog, LOG_ERR, + "Logging verbosity level %d too high", verb); + return NULL; + } + verbosity = verb; + } bufbio = BIO_new(BIO_f_buffer()); if (bufbio == NULL) goto err; acbio = BIO_new(BIO_s_accept()); if (acbio == NULL || BIO_set_bind_mode(acbio, BIO_BIND_REUSEADDR) < 0 - || BIO_set_accept_port(acbio, port) < 0) { + || BIO_set_accept_port(acbio, port /* may be "0" */) < 0) { log_message(prog, LOG_ERR, "Error setting up accept BIO"); goto err; } @@ -241,7 +250,8 @@ BIO *http_server_init_bio(const char *prog, const char *port) /* Report back what address and port are used */ BIO_get_fd(acbio, &asock); - if (!report_server_accept(bio_out, asock, 1, 1)) { + port_num = report_server_accept(bio_out, asock, 1, 1); + if (port_num == 0) { log_message(prog, LOG_ERR, "Error printing ACCEPT string"); goto err; } @@ -283,8 +293,7 @@ static int urldecode(char *p) int http_server_get_asn1_req(const ASN1_ITEM *it, ASN1_VALUE **preq, char **ppath, BIO **pcbio, BIO *acbio, int *found_keep_alive, - const char *prog, const char *port, - int accept_get, int timeout) + const char *prog, int accept_get, int timeout) { BIO *cbio = *pcbio, *getbio = NULL, *b64 = NULL; int len; @@ -298,15 +307,24 @@ int http_server_get_asn1_req(const ASN1_ITEM *it, ASN1_VALUE **preq, *ppath = NULL; if (cbio == NULL) { + char *port; + + get_sock_info_address(BIO_get_fd(acbio, NULL), NULL, &port); + if (port == NULL) { + log_message(prog, LOG_ERR, "Cannot get port listening on"); + goto fatal; + } log_message(prog, LOG_DEBUG, - "Awaiting new connection on port %s...", port); + "Awaiting new connection on port %s ...", port); + OPENSSL_free(port); + if (BIO_do_accept(acbio) <= 0) /* Connection loss before accept() is routine, ignore silently */ return ret; *pcbio = cbio = BIO_pop(acbio); } else { - log_message(prog, LOG_DEBUG, "Awaiting next request..."); + log_message(prog, LOG_DEBUG, "Awaiting next request ..."); } if (cbio == NULL) { /* Cannot call http_server_send_status(cbio, ...) */ diff --git a/apps/lib/s_socket.c b/apps/lib/s_socket.c index 805a1f0f3d..0751d460e8 100644 --- a/apps/lib/s_socket.c +++ b/apps/lib/s_socket.c @@ -207,6 +207,25 @@ out: return ret; } +void get_sock_info_address(int asock, char **hostname, char **service) +{ + union BIO_sock_info_u info; + + if (hostname != NULL) + *hostname = NULL; + if (service != NULL) + *service = NULL; + + if ((info.addr = BIO_ADDR_new()) != NULL + && BIO_sock_info(asock, BIO_SOCK_INFO_ADDRESS, &info)) { + if (hostname != NULL) + *hostname = BIO_ADDR_hostname_string(info.addr, 1); + if (service != NULL) + *service = BIO_ADDR_service_string(info.addr, 1); + } + BIO_ADDR_free(info.addr); +} + int report_server_accept(BIO *out, int asock, int with_address, int with_pid) { int success = 1; @@ -214,30 +233,24 @@ int report_server_accept(BIO *out, int asock, int with_address, int with_pid) if (BIO_printf(out, "ACCEPT") <= 0) return 0; if (with_address) { - union BIO_sock_info_u info; - char *hostname = NULL; - char *service = NULL; + char *hostname, *service; - if ((info.addr = BIO_ADDR_new()) != NULL - && BIO_sock_info(asock, BIO_SOCK_INFO_ADDRESS, &info) - && (hostname = BIO_ADDR_hostname_string(info.addr, 1)) != NULL - && (service = BIO_ADDR_service_string(info.addr, 1)) != NULL) { + get_sock_info_address(asock, &hostname, &service); + success = hostname != NULL && service != NULL; + if (success) success = BIO_printf(out, strchr(hostname, ':') == NULL ? /* IPv4 */ " %s:%s" : /* IPv6 */ " [%s]:%s", hostname, service) > 0; - } else { + else (void)BIO_printf(out, "unknown:error\n"); - success = 0; - } OPENSSL_free(hostname); OPENSSL_free(service); - BIO_ADDR_free(info.addr); } if (with_pid) - success = success && BIO_printf(out, " PID=%d", getpid()) > 0; - success = success && BIO_printf(out, "\n") > 0; + success *= BIO_printf(out, " PID=%d", getpid()) > 0; + success *= BIO_printf(out, "\n") > 0; (void)BIO_flush(out); return success; |