Tomas Hozza 3fed71
diff --git a/bin/dig/Makefile.in b/bin/dig/Makefile.in
Tomas Hozza 3fed71
index 5e9febc..b1861a5 100644
Tomas Hozza 3fed71
--- a/bin/dig/Makefile.in
Tomas Hozza 3fed71
+++ b/bin/dig/Makefile.in
Tomas Hozza 3fed71
@@ -48,10 +48,10 @@ DEPLIBS =	${DNSDEPLIBS} ${BIND9DEPLIBS} ${ISCDEPLIBS} \
Tomas Hozza 3fed71
 		${ISCCFGDEPLIBS} ${LWRESDEPLIBS}
Tomas Hozza 3fed71
 
Tomas Hozza 3fed71
 LIBS =		${LWRESLIBS} ${BIND9LIBS} ${ISCCFGLIBS} \
Tomas Hozza 3fed71
-		${ISCLIBS} @IDNLIBS@ @LIBS@
Tomas Hozza 3fed71
+		${ISCLIBS} @IDNLIBS@ @LIBS@ -lidn
Tomas Hozza 3fed71
 
Tomas Hozza 3fed71
 NOSYMLIBS =	${LWRESLIBS} ${BIND9LIBS} ${ISCCFGLIBS} \
Tomas Hozza 3fed71
-		${ISCNOSYMLIBS} @IDNLIBS@ @LIBS@
Tomas Hozza 3fed71
+		${ISCNOSYMLIBS} @IDNLIBS@ @LIBS@ -lidn
Tomas Hozza 3fed71
 
Tomas Hozza 3fed71
 SUBDIRS =
Tomas Hozza 3fed71
 
Tomas Hozza 3fed71
@@ -69,6 +69,8 @@ HTMLPAGES =	dig.html host.html nslookup.html
Tomas Hozza 3fed71
 
Tomas Hozza 3fed71
 MANOBJS =	${MANPAGES} ${HTMLPAGES}
Tomas Hozza 3fed71
 
Tomas Hozza 3fed71
+EXT_CFLAGS = -DWITH_LIBIDN
Tomas Hozza 3fed71
+
Tomas Hozza 3fed71
 @BIND9_MAKE_RULES@
Tomas Hozza 3fed71
 
Tomas Hozza 3fed71
 dig@EXEEXT@: dig.@O@ dighost.@O@ ${UOBJS} ${DEPLIBS}
Tomas Hozza 3fed71
diff --git a/bin/dig/dig.docbook b/bin/dig/dig.docbook
Tomas Hozza 3fed71
index c54d677..1079421 100644
Tomas Hozza 3fed71
--- a/bin/dig/dig.docbook
Tomas Hozza 3fed71
+++ b/bin/dig/dig.docbook
Tomas Hozza 3fed71
@@ -1170,8 +1170,8 @@ dig +qr www.isc.org any -x 127.0.0.1 isc.org ns +noqr
Tomas Hozza 4ecbfd
       <command>dig</command> appropriately converts character encoding of
Tomas Hozza 4ecbfd
       domain name before sending a request to DNS server or displaying a
Tomas Hozza 4ecbfd
       reply from the server.
Tomas Hozza 4ecbfd
-      If you'd like to turn off the IDN support for some reason, defines
Tomas Hozza 4ecbfd
-      the <envar>IDN_DISABLE</envar> environment variable.
Tomas Hozza 4ecbfd
+      If you'd like to turn off the IDN support for some reason, define
Tomas Hozza 4ecbfd
+      the <envar>CHARSET=ASCII</envar> environment variable.
Tomas Hozza 3fed71
       The IDN support is disabled if the variable is set when
Tomas Hozza 4ecbfd
       <command>dig</command> runs.
Tomas Hozza 4ecbfd
     </para>
Tomas Hozza 3fed71
diff --git a/bin/dig/dighost.c b/bin/dig/dighost.c
Tomas Hozza 3fed71
index 3ca7cb9..f11884e 100644
Tomas Hozza 3fed71
--- a/bin/dig/dighost.c
Tomas Hozza 3fed71
+++ b/bin/dig/dighost.c
Adam Tkac 3416d0
@@ -44,6 +44,11 @@
Adam Tkac 3416d0
 #include <idn/api.h>
Adam Tkac 3416d0
 #endif
Adam Tkac 3416d0
 
Adam Tkac 3416d0
+#ifdef WITH_LIBIDN
Adam Tkac 3416d0
+#include <stringprep.h>
Adam Tkac 3416d0
+#include <idna.h>
Adam Tkac 3416d0
+#endif
Adam Tkac 3416d0
+
Adam Tkac 3416d0
 #include <dns/byaddr.h>
Adam Tkac 3416d0
 #ifdef DIG_SIGCHASE
Tomas Hozza 37d1c7
 #include <dns/callbacks.h>
Tomas Hozza 3fed71
@@ -168,6 +173,14 @@ static void		idn_check_result(idn_result_t r, const char *msg);
Adam Tkac 3416d0
 int  idnoptions	= 0;
Adam Tkac 3416d0
 #endif
Adam Tkac 3416d0
 
Adam Tkac 3416d0
+#ifdef WITH_LIBIDN
Tomas Hozza 4ecbfd
+static isc_result_t	libidn_locale_to_utf8 (const char* from, char *to);
Adam Tkac 3416d0
+static isc_result_t	libidn_utf8_to_ascii (const char* from, char *to);
Adam Tkac 3416d0
+static isc_result_t	output_filter (isc_buffer_t *buffer,
Adam Tkac 3416d0
+				       unsigned int used_org,
Adam Tkac 3416d0
+				       isc_boolean_t absolute);
Adam Tkac 3416d0
+#endif
Adam Tkac 3416d0
+
Tomas Hozza abe4be
 isc_socket_t *keep = NULL;
Tomas Hozza abe4be
 isc_sockaddr_t keepaddr;
Tomas Hozza abe4be
 
Tomas Hozza 3fed71
@@ -1404,8 +1417,15 @@ setup_system(void) {
Adam Tkac 3416d0
 
Adam Tkac 3416d0
 #ifdef WITH_IDN
Adam Tkac 3416d0
 	initialize_idn();
Adam Tkac 3416d0
+	
Adam Tkac 3416d0
+#endif
Adam Tkac 3416d0
+#ifdef WITH_LIBIDN
Adam Tkac 3416d0
+	result = dns_name_settotextfilter(output_filter);
Adam Tkac 3416d0
+	check_result(result, "dns_name_settotextfilter");
Adam Tkac 3416d0
+#ifdef HAVE_SETLOCALE
Adam Tkac 3416d0
+	setlocale (LC_ALL, "");
Adam Tkac 3416d0
+#endif
Adam Tkac 3416d0
 #endif
Adam Tkac 3416d0
-
Adam Tkac 3416d0
 	if (keyfile[0] != 0)
Adam Tkac 3416d0
 		setup_file_key();
Adam Tkac 3416d0
 	else if (keysecret[0] != 0)
Tomas Hozza 3fed71
@@ -2191,12 +2211,14 @@ setup_lookup(dig_lookup_t *lookup) {
Adam Tkac 3416d0
 	idn_result_t mr;
Adam Tkac 3416d0
 	char utf8_textname[MXNAME], utf8_origin[MXNAME], idn_textname[MXNAME];
Adam Tkac 3416d0
 #endif
Adam Tkac 3416d0
+#ifdef WITH_LIBIDN
Tomas Hozza 4ecbfd
+	char utf8_str[MXNAME], utf8_name[MXNAME], ascii_name[MXNAME];
Adam Tkac 3416d0
+#endif
Adam Tkac 3416d0
 
Tomas Hozza 4ecbfd
-#ifdef WITH_IDN
Tomas Hozza 4ecbfd
+#if defined (WITH_IDN) || defined (WITH_LIBIDN)
Adam Tkac 3416d0
 	result = dns_name_settotextfilter(output_filter);
Adam Tkac 3416d0
 	check_result(result, "dns_name_settotextfilter");
Adam Tkac 3416d0
 #endif
Adam Tkac 3416d0
-
Adam Tkac 3416d0
 	REQUIRE(lookup != NULL);
Adam Tkac 3416d0
 	INSIST(!free_now);
Adam Tkac 3416d0
 
Tomas Hozza 3fed71
@@ -2233,6 +2255,14 @@ setup_lookup(dig_lookup_t *lookup) {
Adam Tkac 3416d0
 	mr = idn_encodename(IDN_LOCALCONV | IDN_DELIMMAP, lookup->textname,
Adam Tkac 3416d0
 			    utf8_textname, sizeof(utf8_textname));
Adam Tkac 3416d0
 	idn_check_result(mr, "convert textname to UTF-8");
Adam Tkac 3416d0
+#elif defined (WITH_LIBIDN)
Tomas Hozza 4ecbfd
+	result = libidn_locale_to_utf8 (lookup->textname, utf8_str);
Tomas Hozza 4ecbfd
+	check_result (result, "convert textname to UTF-8");
Adam Tkac 3416d0
+	len = strlen (utf8_str);
Tomas Hozza 4ecbfd
+	if (len < MXNAME)
Adam Tkac 3416d0
+		(void) strcpy (utf8_name, utf8_str);
Tomas Hozza 4ecbfd
+	else
Adam Tkac 3416d0
+		fatal ("Too long name");
Adam Tkac 3416d0
 #endif
Adam Tkac 3416d0
 
Adam Tkac 3416d0
 	/*
Tomas Hozza 3fed71
@@ -2245,15 +2275,11 @@ setup_lookup(dig_lookup_t *lookup) {
Tomas Hozza 4ecbfd
 	if (lookup->new_search) {
Tomas Hozza 4ecbfd
 #ifdef WITH_IDN
Tomas Hozza 4ecbfd
 		if ((count_dots(utf8_textname) >= ndots) || !usesearch) {
Tomas Hozza 4ecbfd
-			lookup->origin = NULL; /* Force abs lookup */
Tomas Hozza 4ecbfd
-			lookup->done_as_is = ISC_TRUE;
Tomas Hozza 4ecbfd
-			lookup->need_search = usesearch;
Tomas Hozza 4ecbfd
-		} else if (lookup->origin == NULL && usesearch) {
Tomas Hozza 4ecbfd
-			lookup->origin = ISC_LIST_HEAD(search_list);
Tomas Hozza 4ecbfd
-			lookup->need_search = ISC_FALSE;
Tomas Hozza 4ecbfd
-		}
Adam Tkac 3416d0
+#elif defined (WITH_LIBIDN)
Adam Tkac 3416d0
+		if ((count_dots(utf8_name) >= ndots) || !usesearch) {
Adam Tkac 3416d0
 #else
Adam Tkac 3416d0
 		if ((count_dots(lookup->textname) >= ndots) || !usesearch) {
Tomas Hozza 4ecbfd
+#endif
Adam Tkac 3416d0
 			lookup->origin = NULL; /* Force abs lookup */
Tomas Hozza 4ecbfd
 			lookup->done_as_is = ISC_TRUE;
Tomas Hozza 4ecbfd
 			lookup->need_search = usesearch;
Tomas Hozza 3fed71
@@ -2261,7 +2287,6 @@ setup_lookup(dig_lookup_t *lookup) {
Tomas Hozza 4ecbfd
 			lookup->origin = ISC_LIST_HEAD(search_list);
Tomas Hozza 4ecbfd
 			lookup->need_search = ISC_FALSE;
Tomas Hozza 4ecbfd
 		}
Tomas Hozza 4ecbfd
-#endif
Tomas Hozza 4ecbfd
 	}
Tomas Hozza 4ecbfd
 
Tomas Hozza 4ecbfd
 #ifdef WITH_IDN
Tomas Hozza 3fed71
@@ -2278,6 +2303,20 @@ setup_lookup(dig_lookup_t *lookup) {
Adam Tkac 3416d0
 			    IDN_IDNCONV | IDN_LENCHECK, utf8_textname,
Adam Tkac 3416d0
 			    idn_textname, sizeof(idn_textname));
Adam Tkac 3416d0
 	idn_check_result(mr, "convert UTF-8 textname to IDN encoding");
Adam Tkac 3416d0
+#elif defined (WITH_LIBIDN)
Adam Tkac 3416d0
+	if (lookup->origin != NULL) {
Tomas Hozza 4ecbfd
+		result = libidn_locale_to_utf8 (lookup->origin->origin, utf8_str);
Adam Tkac 3416d0
+		check_result (result, "convert origin to UTF-8");
Tomas Hozza 4ecbfd
+		if (len > 0 && utf8_name[len - 1] != '.') {
Adam Tkac 3416d0
+			utf8_name[len++] = '.';
Tomas Hozza 4ecbfd
+			if (len + strlen (utf8_str) < MXNAME)
Tomas Hozza 4ecbfd
+				(void) strcpy (utf8_name + len, utf8_str);
Tomas Hozza 4ecbfd
+			else
Tomas Hozza 4ecbfd
+				fatal ("Too long name + origin");
Adam Tkac 3416d0
+		}
Adam Tkac 3416d0
+	}
Adam Tkac 3416d0
+
Adam Tkac 3416d0
+	result = libidn_utf8_to_ascii (utf8_name, ascii_name);
Adam Tkac 3416d0
 #else
Adam Tkac 3416d0
 	if (lookup->origin != NULL) {
Adam Tkac 3416d0
 		debug("trying origin %s", lookup->origin->origin);
Michal Ruprich d886cd
@@ -2372,6 +2411,13 @@ setup_lookup(dig_lookup_t *lookup) {
Adam Tkac e99cc3
 			result = dns_name_fromtext(lookup->name, &b,
Adam Tkac e99cc3
 						   dns_rootname, 0,
Adam Tkac 3416d0
 						   &lookup->namebuf);
Adam Tkac 3416d0
+#elif defined (WITH_LIBIDN)
Adam Tkac 3416d0
+			len = strlen (ascii_name);
Adam Tkac 3416d0
+			isc_buffer_init(&b, ascii_name, len);
Adam Tkac 3416d0
+			isc_buffer_add(&b, len);
Adam Tkac 3416d0
+			result = dns_name_fromtext(lookup->name, &b,
Adam Tkac e99cc3
+						   dns_rootname, 0,
Adam Tkac 3416d0
+						   &lookup->namebuf);
Adam Tkac 3416d0
 #else
Michal Ruprich d886cd
			len = (unsigned int) strlen(lookup->textname);
Adam Tkac 3416d0
 			isc_buffer_init(&b, lookup->textname, len);
Tomas Hozza 3fed71
@@ -4227,7 +4273,7 @@ destroy_libs(void) {
Adam Tkac 3416d0
 	void * ptr;
Adam Tkac 3416d0
 	dig_message_t *chase_msg;
Adam Tkac 3416d0
 #endif
Adam Tkac 3416d0
-#ifdef WITH_IDN
Adam Tkac 3416d0
+#if defined (WITH_IDN) || defined (WITH_LIBIDN)
Adam Tkac 3416d0
 	isc_result_t result;
Adam Tkac 3416d0
 #endif
Adam Tkac 3416d0
 
Tomas Hozza 3fed71
@@ -4268,6 +4314,10 @@ destroy_libs(void) {
Adam Tkac bbeea4
 	result = dns_name_settotextfilter(NULL);
Adam Tkac bbeea4
 	check_result(result, "dns_name_settotextfilter");
Adam Tkac 3416d0
 #endif
Adam Tkac 3416d0
+#ifdef WITH_LIBIDN
Adam Tkac 3416d0
+	result = dns_name_settotextfilter (NULL);
Adam Tkac 3416d0
+	check_result(result, "clearing dns_name_settotextfilter");
Adam Tkac 3416d0
+#endif
Adam Tkac 3416d0
 	dns_name_destroy();
Adam Tkac 3416d0
 
Adam Tkac 3416d0
 	if (commctx != NULL) {
Tomas Hozza 3fed71
@@ -4453,6 +4503,97 @@ idn_check_result(idn_result_t r, const char *msg) {
Adam Tkac 3416d0
 	}
Adam Tkac 3416d0
 }
Adam Tkac 3416d0
 #endif /* WITH_IDN */
Adam Tkac 3416d0
+#ifdef WITH_LIBIDN
Adam Tkac 3416d0
+static isc_result_t
Tomas Hozza 4ecbfd
+libidn_locale_to_utf8 (const char *from, char *to) {
Adam Tkac 3416d0
+	char *utf8_str;
Adam Tkac 3416d0
+
Tomas Hozza 4ecbfd
+	debug ("libidn_locale_to_utf8");
Adam Tkac 3416d0
+	utf8_str = stringprep_locale_to_utf8 (from);
Tomas Hozza 4ecbfd
+	if (utf8_str != NULL) {
Tomas Hozza 4ecbfd
+		(void) strcpy (to, utf8_str);
Adam Tkac 3416d0
+		free (utf8_str);
Tomas Hozza 4ecbfd
+		return ISC_R_SUCCESS;
Adam Tkac 3416d0
+	}
Tomas Hozza 4ecbfd
+
Tomas Hozza 4ecbfd
+	debug ("libidn_locale_to_utf8: failure");
Tomas Hozza 4ecbfd
+	return ISC_R_FAILURE;
Adam Tkac 3416d0
+}
Adam Tkac 3416d0
+static isc_result_t
Adam Tkac 3416d0
+libidn_utf8_to_ascii (const char *from, char *to) {
Adam Tkac 3416d0
+	char *ascii;
Tomas Hozza 4ecbfd
+	int iresult;
Adam Tkac 3416d0
+
Tomas Hozza 4ecbfd
+	debug ("libidn_utf8_to_ascii");
Tomas Hozza 4ecbfd
+	iresult = idna_to_ascii_8z (from, &ascii, 0);
Tomas Hozza 4ecbfd
+	if (iresult != IDNA_SUCCESS) {
Tomas Hozza 4ecbfd
+		debug ("idna_to_ascii_8z: %s", idna_strerror (iresult));
Tomas Hozza 4ecbfd
+		return ISC_R_FAILURE;
Tomas Hozza 4ecbfd
+	}
Adam Tkac 3416d0
+
Adam Tkac 3416d0
+	(void) strcpy (to, ascii);
Adam Tkac 3416d0
+	free (ascii);
Tomas Hozza 4ecbfd
+	return ISC_R_SUCCESS;
Adam Tkac 3416d0
+}
Tomas Hozza 4ecbfd
+
Adam Tkac 3416d0
+static isc_result_t
Adam Tkac 3416d0
+output_filter (isc_buffer_t *buffer, unsigned int used_org,
Adam Tkac 3416d0
+	       isc_boolean_t absolute) {
Tomas Hozza 4ecbfd
+
Adam Tkac 3416d0
+	char tmp1[MXNAME], *tmp2;
Adam Tkac 3416d0
+        size_t fromlen, tolen;
Adam Tkac 3416d0
+        isc_boolean_t end_with_dot;
Tomas Hozza 4ecbfd
+	int iresult;
Adam Tkac 3416d0
+
Tomas Hozza 4ecbfd
+	debug ("output_filter");
Tomas Hozza 4ecbfd
+
Tomas Hozza 4ecbfd
+        fromlen = isc_buffer_usedlength (buffer) - used_org;
Adam Tkac 3416d0
+	if (fromlen >= MXNAME)
Tomas Hozza 4ecbfd
+		return ISC_R_SUCCESS;
Tomas Hozza 4ecbfd
+        memcpy (tmp1, (char *) isc_buffer_base (buffer) + used_org, fromlen);
Adam Tkac 3416d0
+        end_with_dot = (tmp1[fromlen - 1] == '.') ? ISC_TRUE : ISC_FALSE;
Adam Tkac 3416d0
+        if (absolute && !end_with_dot) {
Adam Tkac 3416d0
+                fromlen++;
Adam Tkac 3416d0
+		if (fromlen >= MXNAME)
Tomas Hozza 4ecbfd
+			return ISC_R_SUCCESS;
Adam Tkac 3416d0
+                tmp1[fromlen - 1] = '.';
Adam Tkac 3416d0
+        }
Adam Tkac 3416d0
+        tmp1[fromlen] = '\0';
Adam Tkac 3416d0
+
Tomas Hozza 4ecbfd
+	iresult = idna_to_unicode_8z8z (tmp1, &tmp2, 0);
Tomas Hozza 4ecbfd
+	if (iresult != IDNA_SUCCESS) {
Tomas Hozza 4ecbfd
+		debug ("output_filter: %s", idna_strerror (iresult));
Tomas Hozza 4ecbfd
+		return ISC_R_SUCCESS;
Tomas Hozza 4ecbfd
+	}
Tomas Hozza 4ecbfd
+
Tomas Hozza 4ecbfd
+	(void) strcpy (tmp1, tmp2);
Tomas Hozza 4ecbfd
+	free (tmp2);
Tomas Hozza 4ecbfd
+
Tomas Hozza 4ecbfd
+	tmp2 = stringprep_utf8_to_locale (tmp1);
Tomas Hozza 4ecbfd
+	if (tmp2 == NULL) {
Tomas Hozza 4ecbfd
+		debug ("output_filter: stringprep_utf8_to_locale failed");
Tomas Hozza 4ecbfd
+		return ISC_R_SUCCESS;
Tomas Hozza 4ecbfd
+	}
Adam Tkac 3416d0
+
Adam Tkac 3416d0
+	(void) strcpy (tmp1, tmp2);
Adam Tkac 3416d0
+	free (tmp2);
Adam Tkac 3416d0
+
Tomas Hozza 4ecbfd
+        tolen = strlen (tmp1);
Adam Tkac 3416d0
+        if (absolute && !end_with_dot && tmp1[tolen - 1] == '.')
Adam Tkac 3416d0
+                tolen--;
Adam Tkac 3416d0
+
Tomas Hozza 4ecbfd
+        if (isc_buffer_length (buffer) < used_org + tolen)
Tomas Hozza 4ecbfd
+                return ISC_R_NOSPACE;
Adam Tkac 3416d0
+
Tomas Hozza 4ecbfd
+	debug ("%s", tmp1);
Adam Tkac 3416d0
+
Tomas Hozza 4ecbfd
+        isc_buffer_subtract (buffer, isc_buffer_usedlength (buffer) - used_org);
Tomas Hozza 4ecbfd
+        memcpy (isc_buffer_used (buffer), tmp1, tolen);
Tomas Hozza 4ecbfd
+        isc_buffer_add (buffer, tolen);
Tomas Hozza 4ecbfd
+
Tomas Hozza 4ecbfd
+        return ISC_R_SUCCESS;
Adam Tkac 3416d0
+}
Adam Tkac 3416d0
+#endif /* WITH_LIBIDN*/
Adam Tkac 3416d0
 
Adam Tkac 3416d0
 #ifdef DIG_SIGCHASE
Adam Tkac 3416d0
 void