3ce7d3
From fb4271f5881a83c2cfb639587597b9a80c536a6d Mon Sep 17 00:00:00 2001
3ce7d3
From: =?UTF-8?q?Petr=20Men=C5=A1=C3=ADk?= <pemensik@redhat.com>
3ce7d3
Date: Tue, 29 Jan 2019 20:59:57 +0100
3ce7d3
Subject: [PATCH] Replace libidn2 support with libidn
3ce7d3
3ce7d3
They should be more or less compatible. Try to maintain original
3ce7d3
behaviour of old 9.11 libidn patch, ignore any in output filter.
3ce7d3
---
3ce7d3
 bin/dig/Makefile.in |  6 ++---
3ce7d3
 bin/dig/dighost.c   | 64 ++++++++++++++++++++++++++++++---------------
3ce7d3
 config.h.in         |  4 +--
3ce7d3
 configure.in        | 56 +++++++++++++++++++--------------------
3ce7d3
 4 files changed, 76 insertions(+), 54 deletions(-)
3ce7d3
3ce7d3
diff --git a/bin/dig/Makefile.in b/bin/dig/Makefile.in
3ce7d3
index 3edd951..75441b0 100644
3ce7d3
--- a/bin/dig/Makefile.in
3ce7d3
+++ b/bin/dig/Makefile.in
3ce7d3
@@ -19,7 +19,7 @@ READLINE_LIB = @READLINE_LIB@
3ce7d3
 
3ce7d3
 CINCLUDES =	-I${srcdir}/include ${DNS_INCLUDES} \
3ce7d3
 		${BIND9_INCLUDES} ${ISC_INCLUDES} \
3ce7d3
-		${LWRES_INCLUDES} ${ISCCFG_INCLUDES} @LIBIDN2_CFLAGS@ @DST_OPENSSL_INC@
3ce7d3
+		${LWRES_INCLUDES} ${ISCCFG_INCLUDES} @LIBIDN_CFLAGS@ @DST_OPENSSL_INC@
3ce7d3
 
3ce7d3
 CDEFINES =	-DVERSION=\"${VERSION}\" @CRYPTO@
3ce7d3
 CWARNINGS =
3ce7d3
@@ -41,10 +41,10 @@ DEPLIBS =	${DNSDEPLIBS} ${BIND9DEPLIBS} ${ISCDEPLIBS} \
3ce7d3
 		${ISCCFGDEPLIBS} ${LWRESDEPLIBS}
3ce7d3
 
3ce7d3
 LIBS =		${LWRESLIBS} ${BIND9LIBS} ${ISCCFGLIBS} \
3ce7d3
-		${ISCLIBS} @IDNKIT_LIBS@ @LIBIDN2_LIBS@ @LIBS@
3ce7d3
+		${ISCLIBS} @IDNKIT_LIBS@ @LIBIDN_LIBS@ @LIBS@
3ce7d3
 
3ce7d3
 NOSYMLIBS =	${LWRESLIBS} ${BIND9LIBS} ${ISCCFGLIBS} \
3ce7d3
-		${ISCNOSYMLIBS} @IDNKIT_LIBS@ @LIBIDN2_LIBS@ @LIBS@
3ce7d3
+		${ISCNOSYMLIBS} @IDNKIT_LIBS@ @LIBIDN_LIBS@ @LIBS@
3ce7d3
 
3ce7d3
 SUBDIRS =
3ce7d3
 
3ce7d3
diff --git a/bin/dig/dighost.c b/bin/dig/dighost.c
3ce7d3
index ffc16c4..a345a21 100644
3ce7d3
--- a/bin/dig/dighost.c
3ce7d3
+++ b/bin/dig/dighost.c
3ce7d3
@@ -38,8 +38,9 @@
3ce7d3
 #include <idn/api.h>
3ce7d3
 #endif
3ce7d3
 
3ce7d3
-#ifdef WITH_LIBIDN2
3ce7d3
-#include <idn2.h>
3ce7d3
+#ifdef WITH_LIBIDN
3ce7d3
+#include <stringprep.h>
3ce7d3
+#include <idna.h>
3ce7d3
 #endif
3ce7d3
 #endif /* WITH_IDN_SUPPORT */
3ce7d3
 
3ce7d3
@@ -4761,7 +4762,7 @@ idn_ace_to_locale(const char *from, char *to, size_t tolen) {
3ce7d3
 }
3ce7d3
 #endif /* WITH_IDNKIT */
3ce7d3
 
3ce7d3
-#ifdef WITH_LIBIDN2
3ce7d3
+#ifdef WITH_LIBIDN
3ce7d3
 static void
3ce7d3
 idn_initialize(void) {
3ce7d3
 }
3ce7d3
@@ -4769,16 +4770,25 @@ idn_initialize(void) {
3ce7d3
 static isc_result_t
3ce7d3
 idn_locale_to_ace(const char *from, char *to, size_t tolen) {
3ce7d3
 	int res;
3ce7d3
+	char *utf8_str;
3ce7d3
 	char *tmp_str = NULL;
3ce7d3
 
3ce7d3
-	res = idn2_to_ascii_lz(from, &tmp_str, IDN2_NONTRANSITIONAL|IDN2_NFC_INPUT);
3ce7d3
-	if (res == IDN2_DISALLOWED) {
3ce7d3
-		res = idn2_to_ascii_lz(from, &tmp_str, IDN2_TRANSITIONAL|IDN2_NFC_INPUT);
3ce7d3
+	debug ("libidn_locale_to_utf8");
3ce7d3
+	utf8_str = stringprep_locale_to_utf8 (from);
3ce7d3
+	if (utf8_str == NULL) {
3ce7d3
+		debug ("libidn stringprep_locale_to_utf8 failure");
3ce7d3
+		return ISC_R_FAILURE;
3ce7d3
 	}
3ce7d3
 
3ce7d3
-	if (res == IDN2_OK) {
3ce7d3
+	int iresult;
3ce7d3
+
3ce7d3
+	debug ("libidn_utf8_to_ascii");
3ce7d3
+	res = idna_to_ascii_8z (utf8_str, &tmp_str, 0);
3ce7d3
+	free (utf8_str);
3ce7d3
+
3ce7d3
+	if (res == IDNA_SUCCESS) {
3ce7d3
 		/*
3ce7d3
-		 * idn2_to_ascii_lz() normalizes all strings to lowerl case,
3ce7d3
+		 * idna_to_ascii_8z() normalizes all strings to lowerl case,
3ce7d3
 		 * but we generally don't want to lowercase all input strings;
3ce7d3
 		 * make sure to return the original case if the two strings
3ce7d3
 		 * differ only in case
3ce7d3
@@ -4786,26 +4796,26 @@ idn_locale_to_ace(const char *from, char *to, size_t tolen) {
3ce7d3
 		if (!strcasecmp(from, tmp_str)) {
3ce7d3
 			if (strlen(from) >= tolen) {
3ce7d3
 				debug("from string is too long");
3ce7d3
-				idn2_free(tmp_str);
3ce7d3
+				free(tmp_str);
3ce7d3
 				return ISC_R_NOSPACE;
3ce7d3
 			}
3ce7d3
-			idn2_free(tmp_str);
3ce7d3
+			free(tmp_str);
3ce7d3
 			(void) strlcpy(to, from, tolen);
3ce7d3
 			return ISC_R_SUCCESS;
3ce7d3
 		}
3ce7d3
 		/* check the length */
3ce7d3
 		if (strlen(tmp_str) >= tolen) {
3ce7d3
 			debug("ACE string is too long");
3ce7d3
-			idn2_free(tmp_str);
3ce7d3
+			free(tmp_str);
3ce7d3
 			return ISC_R_NOSPACE;
3ce7d3
 		}
3ce7d3
 
3ce7d3
 		(void) strlcpy(to, tmp_str, tolen);
3ce7d3
-		idn2_free(tmp_str);
3ce7d3
+		free(tmp_str);
3ce7d3
 		return ISC_R_SUCCESS;
3ce7d3
 	}
3ce7d3
 
3ce7d3
-	fatal("'%s' is not a legal IDN name (%s), use +noidnin", from, idn2_strerror(res));
3ce7d3
+	fatal("'%s' is not a legal IDN name (%s), use +noidnin", from, idna_strerror (res));
3ce7d3
 	return ISC_R_FAILURE;
3ce7d3
 }
3ce7d3
 
3ce7d3
@@ -4813,29 +4823,41 @@ idn_locale_to_ace(const char *from, char *to, size_t tolen) {
3ce7d3
 static isc_result_t
3ce7d3
 idn_ace_to_locale(const char *from, char *to, size_t tolen) {
3ce7d3
 	int res;
3ce7d3
+	char *tmp2 = NULL;
3ce7d3
 	char *tmp_str = NULL;
3ce7d3
 
3ce7d3
-	res = idn2_to_unicode_8zlz(from, &tmp_str,
3ce7d3
-				   IDN2_NONTRANSITIONAL|IDN2_NFC_INPUT);
3ce7d3
+	res = idna_to_unicode_8z8z (from, &tmp2, 0);
3ce7d3
+	if (res != IDNA_SUCCESS) {
3ce7d3
+		debug ("output_filter: %s", idna_strerror (res));
3ce7d3
+		return ISC_R_SUCCESS;
3ce7d3
+	}
3ce7d3
+
3ce7d3
+	tmp_str = stringprep_utf8_to_locale (tmp2);
3ce7d3
+	if (tmp_str == NULL) {
3ce7d3
+		debug ("output_filter: stringprep_utf8_to_locale failed");
3ce7d3
+		res = idna_to_ascii_8z(tmp2, &tmp_str, 0);
3ce7d3
+	}
3ce7d3
+
3ce7d3
+	free(tmp2);
3ce7d3
 
3ce7d3
-	if (res == IDN2_OK) {
3ce7d3
+	if (res == IDNA_SUCCESS) {
3ce7d3
 		/* check the length */
3ce7d3
 		if (strlen(tmp_str) >= tolen) {
3ce7d3
 			debug("encoded ASC string is too long");
3ce7d3
-			idn2_free(tmp_str);
3ce7d3
+			free(tmp_str);
3ce7d3
 			return ISC_R_FAILURE;
3ce7d3
 		}
3ce7d3
 
3ce7d3
 		(void) strlcpy(to, tmp_str, tolen);
3ce7d3
-		idn2_free(tmp_str);
3ce7d3
+		free(tmp_str);
3ce7d3
 		return ISC_R_SUCCESS;
3ce7d3
 	}
3ce7d3
-
3ce7d3
-	fatal("'%s' is not a legal IDN name (%s), use +noidnout", from, idn2_strerror(res));
3ce7d3
+	// fatal("'%s' is not a legal IDN name (%s), use +noidnout", from, idna_strerror(res));
3ce7d3
+	free(tmp_str);
3ce7d3
 	return ISC_R_FAILURE;
3ce7d3
 }
3ce7d3
 #endif /* WITH_IDN_OUT_SUPPORT */
3ce7d3
-#endif /* WITH_LIBIDN2 */
3ce7d3
+#endif /* WITH_LIBIDN */
3ce7d3
 #endif /* WITH_IDN_SUPPORT */
3ce7d3
 
3ce7d3
 #ifdef DIG_SIGCHASE
3ce7d3
diff --git a/config.h.in b/config.h.in
3ce7d3
index 1dc65cf..9eb8a16 100644
3ce7d3
--- a/config.h.in
3ce7d3
+++ b/config.h.in
3ce7d3
@@ -615,8 +615,8 @@ int sigwait(const unsigned int *set, int *sig);
3ce7d3
 /* define if IDN input support is to be included. */
3ce7d3
 #undef WITH_IDN_SUPPORT
3ce7d3
 
3ce7d3
-/* define if libidn2 support is to be included. */
3ce7d3
-#undef WITH_LIBIDN2
3ce7d3
+/* define if libidn support is to be included. */
3ce7d3
+#undef WITH_LIBIDN
3ce7d3
 
3ce7d3
 /* Define WORDS_BIGENDIAN to 1 if your processor stores words with the most
3ce7d3
    significant byte first (like Motorola and SPARC, unlike Intel). */
3ce7d3
diff --git a/configure.in b/configure.in
3ce7d3
index 9a1d16d..1397c50 100644
3ce7d3
--- a/configure.in
3ce7d3
+++ b/configure.in
3ce7d3
@@ -4864,36 +4864,36 @@ fi
3ce7d3
 AC_SUBST(IDNKIT_LIBS)
3ce7d3
 
3ce7d3
 #
3ce7d3
-# IDN support using libidn2
3ce7d3
+# IDN support using libidn
3ce7d3
 #
3ce7d3
 
3ce7d3
-LIBIDN2_CFLAGS=
3ce7d3
-LIBIDN2_LDFLAGS=
3ce7d3
-LIBIDN2_LIBS=
3ce7d3
-AC_ARG_WITH(libidn2,
3ce7d3
-	AS_HELP_STRING([--with-libidn2[=PATH]], [enable IDN support using GNU libidn2 [yes|no|path]]),
3ce7d3
-	use_libidn2="$withval", use_libidn2="no")
3ce7d3
-AS_CASE([$use_libidn2],
3ce7d3
+LIBIDN_CFLAGS=
3ce7d3
+LIBIDN_LDFLAGS=
3ce7d3
+LIBIDN_LIBS=
3ce7d3
+AC_ARG_WITH(libidn,
3ce7d3
+	AS_HELP_STRING([--with-libidn[=PATH]], [enable IDN support using GNU libidn [yes|no|path]]),
3ce7d3
+	use_libidn="$withval", use_libidn="no")
3ce7d3
+AS_CASE([$use_libidn],
3ce7d3
 	[no],[:],
3ce7d3
 	[yes],[:],
3ce7d3
 	[*],[
3ce7d3
-	    LIBIDN2_CFLAGS="-I$use_libidn2/include"
3ce7d3
-	    LIBIDN2_LDFLAGS="-L$use_libidn2/lib"
3ce7d3
+	    LIBIDN_CFLAGS="-I$use_libidn/include"
3ce7d3
+	    LIBIDN_LDFLAGS="-L$use_libidn/lib"
3ce7d3
 	])
3ce7d3
 
3ce7d3
-AS_IF([test "$use_libidn2" != "no"],
3ce7d3
+AS_IF([test "$use_libidn" != "no"],
3ce7d3
       [save_CFLAGS="$CFLAGS"
3ce7d3
        save_LIBS="$LIBS"
3ce7d3
        save_LDFLAGS="$LDFLAGS"
3ce7d3
-       CFLAGS="$LIBIDN2_CFLAGS $CFLAGS"
3ce7d3
-       LDFLAGS="$LIBIDN2_LDFLAGS $LDFLAGS"
3ce7d3
-       AC_SEARCH_LIBS([idn2_to_ascii_8z], [idn2],
3ce7d3
+       CFLAGS="$LIBIDN_CFLAGS $CFLAGS"
3ce7d3
+       LDFLAGS="$LIBIDN_LDFLAGS $LDFLAGS"
3ce7d3
+       AC_SEARCH_LIBS([idna_to_ascii_8z], [idn],
3ce7d3
 		      [AC_DEFINE(WITH_IDN_SUPPORT, 1, [define if IDN input support is to be included.])
3ce7d3
-                       AC_DEFINE(WITH_LIBIDN2, 1, [define if libidn2 support is to be included.])
3ce7d3
-                       LIBIDN2_LIBS="$LIBIDN2_LDFLAGS -lidn2"],
3ce7d3
-		      [AC_MSG_ERROR([libidn2 requested, but not found])])
3ce7d3
-       AC_TRY_LINK([#include <idn2.h>],
3ce7d3
-		   [idn2_to_unicode_8zlz(".", NULL, IDN2_NONTRANSITIONAL|IDN2_NFC_INPUT);],
3ce7d3
+                       AC_DEFINE(WITH_LIBIDN, 1, [define if libidn support is to be included.])
3ce7d3
+                       LIBIDN_LIBS="$LIBIDN_LDFLAGS -lidn"],
3ce7d3
+		      [AC_MSG_ERROR([libidn requested, but not found])])
3ce7d3
+       AC_TRY_LINK([#include <idna.h>],
3ce7d3
+		   [idna_to_unicode_8zlz(".", NULL, 0);],
3ce7d3
 		   [AC_MSG_RESULT(yes)
3ce7d3
 		    AC_DEFINE(WITH_IDN_OUT_SUPPORT, 1, [define if IDN output support is to be included.])],
3ce7d3
 		   [AC_MSG_RESULT([no])])
3ce7d3
@@ -4901,21 +4901,21 @@ AS_IF([test "$use_libidn2" != "no"],
3ce7d3
        LIBS="$save_LIBS"
3ce7d3
        LDFLAGS="$save_LDFLAGS"
3ce7d3
       ])
3ce7d3
-AC_SUBST([LIBIDN2_CFLAGS])
3ce7d3
-AC_SUBST([LIBIDN2_LIBS])
3ce7d3
+AC_SUBST([LIBIDN_CFLAGS])
3ce7d3
+AC_SUBST([LIBIDN_LIBS])
3ce7d3
 
3ce7d3
 #
3ce7d3
 # IDN support in general
3ce7d3
 #
3ce7d3
 
3ce7d3
-# check if idnkit and libidn2 are not used at the same time
3ce7d3
-if test "$use_idnkit" != no && test "$use_libidn2" != no; then
3ce7d3
-    AC_MSG_ERROR([idnkit and libidn2 cannot be used at the same time.])
3ce7d3
+# check if idnkit and libidn are not used at the same time
3ce7d3
+if test "$use_idnkit" != no && test "$use_libidn" != no; then
3ce7d3
+    AC_MSG_ERROR([idnkit and libidn cannot be used at the same time.])
3ce7d3
 fi
3ce7d3
 # the IDN support is on
3ce7d3
-if test "$use_idnkit" != no || test "$use_libidn2" != no; then
3ce7d3
+if test "$use_idnkit" != no || test "$use_libidn" != no; then
3ce7d3
 	AC_DEFINE(WITH_IDN_SUPPORT, 1, [define if IDN input support is to be included.])
3ce7d3
-	if test "$use_libidn2" = no || test "$use_libidn2_out" != no; then
3ce7d3
+	if test "$use_libidn" = no || test "$use_libidn_out" != no; then
3ce7d3
 		AC_DEFINE(WITH_IDN_OUT_SUPPORT, 1, [define if IDN output support is to be included.])
3ce7d3
 	fi
3ce7d3
 fi
3ce7d3
@@ -5618,7 +5618,7 @@ report() {
3ce7d3
 	test "X$JSONSTATS" = "X" || echo "    JSON statistics (--with-libjson)"
3ce7d3
 	test "X$ZLIB" = "X" || echo "    HTTP zlib compression (--with-zlib)"
3ce7d3
 	test "X$NZD_TOOLS" = "X" || echo "    LMDB database to store configuration for 'addzone' zones (--with-lmdb)"
3ce7d3
-	test "no" = "$use_libidn2" || echo "    IDN support (--with-libidn2)"
3ce7d3
+	test "no" = "$use_libidn" || echo "    IDN support (--with-libidn)"
3ce7d3
     fi
3ce7d3
 
3ce7d3
     if test "no" != "$use_pkcs11"; then
3ce7d3
@@ -5716,7 +5716,7 @@ report() {
3ce7d3
     test "X$JSONSTATS" = "X" && echo "    JSON statistics (--with-libjson)"
3ce7d3
     test "X$ZLIB" = "X" && echo "    HTTP zlib compression (--with-zlib)"
3ce7d3
     test "X$NZD_TOOLS" = "X" && echo "    LMDB database to store configuration for 'addzone' zones (--with-lmdb)"
3ce7d3
-    test "no" = "$use_libidn2" && echo "    IDN support (--with-libidn2)"
3ce7d3
+    test "no" = "$use_libidn" && echo "    IDN support (--with-libidn)"
3ce7d3
 
3ce7d3
     echo "-------------------------------------------------------------------------------"
3ce7d3
     echo "Configured paths:"
3ce7d3
-- 
3ce7d3
2.20.1
3ce7d3