From 07f040ea0baf97bceddc8da01c21b49ea4e143f9 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 20 Nov 2023 08:58:48 +0100 Subject: [PATCH] Switch from libidn to libidn2 to get IDN conversion conforming to IDNA2008 + TR46 specifications (RFC 5890, RFC 5891, RFC 5892, RFC 5893, TR 46) Signed-off-by: DL6ER --- src/CMakeLists.txt | 7 +++++-- src/api/list.c | 13 ++++++------- src/api/search.c | 10 +++++----- src/dnsmasq/config.h | 2 +- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4b02c2e9..3428cedb 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -283,9 +283,12 @@ find_package(Threads REQUIRED) find_library(LIBHOGWEED NAMES libhogweed${CMAKE_STATIC_LIBRARY_SUFFIX} hogweed HINTS /usr/local/lib64) find_library(LIBGMP NAMES libgmp${CMAKE_STATIC_LIBRARY_SUFFIX} gmp) find_library(LIBNETTLE NAMES libnettle${CMAKE_STATIC_LIBRARY_SUFFIX} nettle HINTS /usr/local/lib64) -find_library(LIBIDN NAMES libidn${CMAKE_STATIC_LIBRARY_SUFFIX} idn) -target_link_libraries(pihole-FTL rt Threads::Threads ${LIBHOGWEED} ${LIBGMP} ${LIBNETTLE} ${LIBIDN}) +# for IDN2 we need the idn2 library which in turn depends on the unistring library +find_library(LIBIDN2 NAMES libidn2${CMAKE_STATIC_LIBRARY_SUFFIX} idn) +find_library(LIBUNISTRING NAMES libunistring${CMAKE_STATIC_LIBRARY_SUFFIX} unistring) + +target_link_libraries(pihole-FTL rt Threads::Threads ${LIBHOGWEED} ${LIBGMP} ${LIBNETTLE} ${LIBIDN2} ${LIBUNISTRING}) if(LUA_DL STREQUAL "true") find_library(LIBDL dl) diff --git a/src/api/list.c b/src/api/list.c index 0502f5ec..42f71441 100644 --- a/src/api/list.c +++ b/src/api/list.c @@ -19,8 +19,7 @@ #include "database/network-table.h" // valid_domain() #include "tools/gravity-parseList.h" - -#include +#include static int api_list_read(struct ftl_conn *api, const int code, @@ -75,9 +74,9 @@ static int api_list_read(struct ftl_conn *api, else // domainlists { char *unicode = NULL; - const Idna_rc rc = idna_to_unicode_lzlz(table.domain, &unicode, 0); + const int rc = idn2_to_unicode_lzlz(table.domain, &unicode, IDN2_NONTRANSITIONAL); JSON_COPY_STR_TO_OBJECT(row, "domain", table.domain); - if(rc == IDNA_SUCCESS) + if(rc == IDN2_OK) JSON_COPY_STR_TO_OBJECT(row, "unicode", unicode); else JSON_COPY_STR_TO_OBJECT(row, "unicode", table.domain); @@ -417,14 +416,14 @@ static int api_list_write(struct ftl_conn *api, listtype == GRAVITY_DOMAINLIST_DENY_EXACT) { char *punycode = NULL; - const Idna_rc rc = idna_to_ascii_lz(it->valuestring, &punycode, 0); - if (rc != IDNA_SUCCESS) + const int rc = idn2_to_ascii_lz(it->valuestring, &punycode, IDN2_NONTRANSITIONAL); + if (rc != IDN2_OK) { // Invalid domain name return send_json_error(api, 400, "bad_request", "Invalid request: Invalid domain name", - idna_strerror(rc)); + idn2_strerror(rc)); } // Convert punycode domain to lowercase for(unsigned int i = 0u; i < strlen(punycode); i++) diff --git a/src/api/search.c b/src/api/search.c index 0ed5a3bf..511d9c97 100644 --- a/src/api/search.c +++ b/src/api/search.c @@ -15,7 +15,7 @@ #include "database/gravity-db.h" // match_regex() #include "regex_r.h" -#include +#include #define MAX_SEARCH_RESULTS 10000u @@ -182,18 +182,18 @@ int api_search(struct ftl_conn *api) // use characters drawn from a large repertoire (Unicode), but IDNA // allows the non-ASCII characters to be represented using only the // ASCII characters already allowed in so-called host names today. - // idna_to_ascii_lz() convert domain name in the locale’s encoding to an + // idn2_to_ascii_lz() convert domain name in the locale’s encoding to an // ASCII string. The domain name may contain several labels, separated // by dots. The output buffer must be deallocated by the caller. char *punycode = NULL; - const Idna_rc rc = idna_to_ascii_lz(domain, &punycode, 0); - if (rc != IDNA_SUCCESS) + const int rc = idn2_to_ascii_lz(domain, &punycode, IDN2_NONTRANSITIONAL); + if (rc != IDN2_OK) { // Invalid domain name return send_json_error(api, 400, "bad_request", "Invalid request: Invalid domain name", - idna_strerror(rc)); + idn2_strerror(rc)); } // Convert punycode domain to lowercase diff --git a/src/dnsmasq/config.h b/src/dnsmasq/config.h index df302fbd..d73359be 100644 --- a/src/dnsmasq/config.h +++ b/src/dnsmasq/config.h @@ -205,7 +205,7 @@ RESOLVFILE /* Pi-hole definitions */ #define HAVE_LUASCRIPT -#define HAVE_IDN +#define HAVE_LIBIDN2 #define HAVE_DNSSEC #ifdef DNSMASQ_ALL_OPTS #define HAVE_DBUS