From 9634668258342789d2a16172c7c86ef51411f458 Mon Sep 17 00:00:00 2001 From: Jakub Jelen Date: Fri, 2 Aug 2024 09:56:43 +0200 Subject: [PATCH] Conditional compilation of localnetwork matching Some architectures (esp32) might not have this API. Fixes: #263 Signed-off-by: Jakub Jelen Reviewed-by: Andreas Schneider --- ConfigureChecks.cmake | 1 + config.h.cmake | 3 +++ src/config.c | 20 ++++++++++++++----- tests/unittests/CMakeLists.txt | 12 +++++++---- .../torture_config_match_localnetwork.c | 2 ++ 5 files changed, 29 insertions(+), 9 deletions(-) diff --git a/ConfigureChecks.cmake b/ConfigureChecks.cmake index 4639f0c1..8765dc6e 100644 --- a/ConfigureChecks.cmake +++ b/ConfigureChecks.cmake @@ -64,6 +64,7 @@ check_include_file(arpa/inet.h HAVE_ARPA_INET_H) check_include_file(byteswap.h HAVE_BYTESWAP_H) check_include_file(glob.h HAVE_GLOB_H) check_include_file(valgrind/valgrind.h HAVE_VALGRIND_VALGRIND_H) +check_include_file(ifaddrs.h HAVE_IFADDRS_H) if (WIN32) check_include_file(io.h HAVE_IO_H) diff --git a/config.h.cmake b/config.h.cmake index 9dec02bf..8dce5273 100644 --- a/config.h.cmake +++ b/config.h.cmake @@ -58,6 +58,9 @@ /* Define to 1 if you have the header file. */ #cmakedefine HAVE_STDINT_H 1 +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_IFADDRS_H 1 + /* Define to 1 if you have the header file. */ #cmakedefine HAVE_OPENSSL_AES_H 1 diff --git a/src/config.c b/src/config.c index 78549a40..774b3566 100644 --- a/src/config.c +++ b/src/config.c @@ -39,10 +39,12 @@ # include # include # include -# include # include # include #endif +#ifdef HAVE_IFADDRS_H +#include +#endif #include "libssh/config_parser.h" #include "libssh/config.h" @@ -639,7 +641,7 @@ ssh_config_make_absolute(ssh_session session, return out; } -#ifndef _WIN32 +#ifdef HAVE_IFADDRS_H /** * @brief Checks if host address matches the local network specified. * @@ -730,7 +732,7 @@ ssh_match_localnetwork(const char *addrlist, bool negate) return (found == (negate ? 0 : 1)); } -#endif +#endif /* HAVE_IFADDRS_H */ static int ssh_config_parse_line(ssh_session session, @@ -955,7 +957,6 @@ ssh_config_parse_line(ssh_session session, args++; break; -#ifndef _WIN32 case MATCH_LOCALNETWORK: /* Here we match only one argument */ p = ssh_config_get_str_tok(&s, NULL); @@ -968,6 +969,7 @@ ssh_config_parse_line(ssh_session session, SAFE_FREE(x); return -1; } +#ifdef HAVE_IFADDRS_H rv = match_cidr_address_list(NULL, p, -1); if (rv == -1) { ssh_set_error(session, @@ -992,9 +994,17 @@ ssh_config_parse_line(ssh_session session, } result &= rv; +#else /* HAVE_IFADDRS_H */ + ssh_set_error(session, + SSH_FATAL, + "line %d: ERROR - match localnetwork " + "not supported on this platform", + count); + SAFE_FREE(x); + return -1; +#endif /* HAVE_IFADDRS_H */ args++; break; -#endif case MATCH_UNKNOWN: default: diff --git a/tests/unittests/CMakeLists.txt b/tests/unittests/CMakeLists.txt index a22532a9..79f3856c 100644 --- a/tests/unittests/CMakeLists.txt +++ b/tests/unittests/CMakeLists.txt @@ -48,11 +48,15 @@ if (UNIX AND NOT WIN32) torture_pki_ed25519 # requires /dev/null torture_channel - # requires some non-standard API from netdb.h, in.h - # and arpa/inet.h for handling IP addresses - torture_config_match_localnetwork ) - + if (HAVE_IFADDRS_H) + set(LIBSSH_UNIT_TESTS + ${LIBSSH_UNIT_TESTS} + # requires some non-standard API from netdb.h, in.h + # and arpa/inet.h for handling IP addresses + torture_config_match_localnetwork + ) + endif (HAVE_IFADDRS_H) if (WITH_SERVER) set(LIBSSH_UNIT_TESTS ${LIBSSH_UNIT_TESTS} diff --git a/tests/unittests/torture_config_match_localnetwork.c b/tests/unittests/torture_config_match_localnetwork.c index 33ed05d1..80aae0c3 100644 --- a/tests/unittests/torture_config_match_localnetwork.c +++ b/tests/unittests/torture_config_match_localnetwork.c @@ -3,7 +3,9 @@ #include "libssh/options.h" #include "libssh/session.h" #include "match.c" +#ifdef HAVE_IFADDRS_H #include +#endif #include #include