summaryrefslogtreecommitdiffstats
path: root/bind-9.5-libidn.patch
blob: e7233f5805a83a0e3be6769321dc4018760bfbd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
diff -up bind-9.5.0b2/bin/dig/Makefile.in.libidn bind-9.5.0b2/bin/dig/Makefile.in
--- bind-9.5.0b2/bin/dig/Makefile.in.libidn	2007-06-20 01:46:59.000000000 +0200
+++ bind-9.5.0b2/bin/dig/Makefile.in	2008-02-11 17:50:08.000000000 +0100
@@ -45,7 +45,7 @@ DEPLIBS =	${DNSDEPLIBS} ${BIND9DEPLIBS} 
 		${LWRESDEPLIBS}
 
 LIBS =		${LWRESLIBS} ${DNSLIBS} ${BIND9LIBS} ${ISCLIBS} \
-		${ISCCFGLIBS} @IDNLIBS@ @LIBS@
+		${ISCCFGLIBS} @IDNLIBS@ @LIBS@ -lidn
 
 SUBDIRS =
 
@@ -63,6 +63,8 @@ HTMLPAGES =	dig.html host.html nslookup.
 
 MANOBJS =	${MANPAGES} ${HTMLPAGES}
 
+EXT_CFLAGS = -DWITH_LIBIDN
+
 @BIND9_MAKE_RULES@
 
 dig@EXEEXT@: dig.@O@ dighost.@O@ ${UOBJS} ${DEPLIBS}
diff -up bind-9.5.0b2/bin/dig/dighost.c.libidn bind-9.5.0b2/bin/dig/dighost.c
--- bind-9.5.0b2/bin/dig/dighost.c.libidn	2008-01-18 00:46:35.000000000 +0100
+++ bind-9.5.0b2/bin/dig/dighost.c	2008-02-11 17:49:03.000000000 +0100
@@ -44,6 +44,11 @@
 #include <idn/api.h>
 #endif
 
+#ifdef WITH_LIBIDN
+#include <stringprep.h>
+#include <idna.h>
+#endif
+
 #include <dns/byaddr.h>
 #ifdef DIG_SIGCHASE
 #include <dns/dnssec.h>
@@ -147,6 +152,14 @@ static void		idn_check_result(idn_result
 int  idnoptions	= 0;
 #endif
 
+#ifdef WITH_LIBIDN
+static isc_result_t	libidn_locale_to_utf8 (const char* from, char **to);
+static isc_result_t	libidn_utf8_to_ascii (const char* from, char *to);
+static isc_result_t	output_filter (isc_buffer_t *buffer,
+				       unsigned int used_org,
+				       isc_boolean_t absolute);
+#endif
+
 /*%
  * Exit Codes:
  *
@@ -1004,6 +1017,9 @@ void
 setup_system(void) {
 	dig_searchlist_t *domain = NULL;
 	lwres_result_t lwresult;
+#ifdef WITH_LIBIDN
+	isc_result_t result;
+#endif
 
 	debug("setup_system()");
 
@@ -1052,8 +1068,15 @@ setup_system(void) {
 
 #ifdef WITH_IDN
 	initialize_idn();
+	
+#endif
+#ifdef WITH_LIBIDN
+	result = dns_name_settotextfilter(output_filter);
+	check_result(result, "dns_name_settotextfilter");
+#ifdef HAVE_SETLOCALE
+	setlocale (LC_ALL, "");
+#endif
 #endif
-
 	if (keyfile[0] != 0)
 		setup_file_key();
 	else if (keysecret[0] != 0)
@@ -1743,12 +1766,18 @@ setup_lookup(dig_lookup_t *lookup) {
 	idn_result_t mr;
 	char utf8_textname[MXNAME], utf8_origin[MXNAME], idn_textname[MXNAME];
 #endif
+#ifdef WITH_LIBIDN
+	char *utf8_str = NULL, utf8_name[MXNAME], ascii_name[MXNAME];
+#endif
 
 #ifdef WITH_IDN
 	result = dns_name_settotextfilter(output_filter);
 	check_result(result, "dns_name_settotextfilter");
 #endif
-
+#ifdef WITH_LIBIDN
+	result = dns_name_settotextfilter (output_filter);
+	check_result(result, "dns_name_settotextfilter");
+#endif
 	REQUIRE(lookup != NULL);
 	INSIST(!free_now);
 
@@ -1785,6 +1814,16 @@ setup_lookup(dig_lookup_t *lookup) {
 	mr = idn_encodename(IDN_LOCALCONV | IDN_DELIMMAP, lookup->textname,
 			    utf8_textname, sizeof(utf8_textname));
 	idn_check_result(mr, "convert textname to UTF-8");
+#elif defined (WITH_LIBIDN)
+	result = libidn_locale_to_utf8 (lookup->textname, &utf8_str);
+	check_result (result, "converting textname to UTF-8");
+	len = strlen (utf8_str);
+	if (len < MXNAME) {
+		(void) strcpy (utf8_name, utf8_str);
+	} else {
+		fatal ("Too long name");
+	}
+	isc_mem_free (mctx, utf8_str);
 #endif
 
 	/*
@@ -1804,6 +1843,15 @@ setup_lookup(dig_lookup_t *lookup) {
 			lookup->origin = ISC_LIST_HEAD(search_list);
 			lookup->need_search = ISC_FALSE;
 		}
+#elif defined (WITH_LIBIDN)
+		if ((count_dots(utf8_name) >= ndots) || !usesearch) {
+			lookup->origin = NULL; /* Force abs lookup */
+			lookup->done_as_is = ISC_TRUE;
+			lookup->need_search = usesearch;
+		} else if (lookup->origin == NULL && usesearch) {
+			lookup->origin = ISC_LIST_HEAD(search_list);
+			lookup->need_search = ISC_FALSE;
+		}
 #else
 		if ((count_dots(lookup->textname) >= ndots) || !usesearch) {
 			lookup->origin = NULL; /* Force abs lookup */
@@ -1830,6 +1878,20 @@ setup_lookup(dig_lookup_t *lookup) {
 			    IDN_IDNCONV | IDN_LENCHECK, utf8_textname,
 			    idn_textname, sizeof(idn_textname));
 	idn_check_result(mr, "convert UTF-8 textname to IDN encoding");
+#elif defined (WITH_LIBIDN)
+	if (lookup->origin != NULL) {
+		result = libidn_locale_to_utf8 (lookup->origin->origin, &utf8_str);
+		check_result (result, "convert origin to UTF-8");
+		if (len + strlen (utf8_str) + 1 < MXNAME) {
+			utf8_name[len++] = '.';
+			(void) strcpy (utf8_name + len, utf8_str);
+		} else {
+			fatal ("Too long name + origin");
+		}
+		isc_mem_free (mctx, utf8_str);
+	}
+
+	result = libidn_utf8_to_ascii (utf8_name, ascii_name);
 #else
 	if (lookup->origin != NULL) {
 		debug("trying origin %s", lookup->origin->origin);
@@ -1886,6 +1948,14 @@ setup_lookup(dig_lookup_t *lookup) {
 						   dns_rootname,
 						   ISC_FALSE,
 						   &lookup->namebuf);
+#elif defined (WITH_LIBIDN)
+			len = strlen (ascii_name);
+			isc_buffer_init(&b, ascii_name, len);
+			isc_buffer_add(&b, len);
+			result = dns_name_fromtext(lookup->name, &b,
+						   dns_rootname,
+						   ISC_FALSE,
+						   &lookup->namebuf);
 #else
 			len = strlen(lookup->textname);
 			isc_buffer_init(&b, lookup->textname, len);
@@ -3317,7 +3387,7 @@ destroy_libs(void) {
 	void * ptr;
 	dig_message_t *chase_msg;
 #endif
-#ifdef WITH_IDN
+#if defined (WITH_IDN) || defined (WITH_LIBIDN)
 	isc_result_t result;
 #endif
 
@@ -3356,6 +3426,10 @@ destroy_libs(void) {
 	result = dns_name_settotextfilter(NULL);
 	check_result(result, "dns_name_settotextfilter");
 #endif
+#ifdef WITH_LIBIDN
+	result = dns_name_settotextfilter (NULL);
+	check_result(result, "clearing dns_name_settotextfilter");
+#endif
 	dns_name_destroy();
 
 	if (commctx != NULL) {
@@ -3532,6 +3606,79 @@ idn_check_result(idn_result_t r, const c
 	}
 }
 #endif /* WITH_IDN */
+#ifdef WITH_LIBIDN
+/* If stringprep_locale_to_utf8 fails simple copy string */
+static isc_result_t
+libidn_locale_to_utf8 (const char *from, char **to) {
+	char *utf8_str;
+
+	utf8_str = stringprep_locale_to_utf8 (from);
+	if (utf8_str == NULL) {
+		*to = isc_mem_allocate (mctx, strlen (from) + 1);
+		if (*to == NULL)
+			return (ISC_R_NOMEMORY);
+		(void) strcpy (*to, from);
+	} else {
+		*to = isc_mem_allocate (mctx, strlen (utf8_str) + 1);
+		if (*to == NULL)
+			return (ISC_R_NOMEMORY);
+		(void) strcpy (*to, utf8_str);
+		free (utf8_str);
+	}
+	return (ISC_R_SUCCESS);
+}
+static isc_result_t
+libidn_utf8_to_ascii (const char *from, char *to) {
+	char *ascii;
+
+	if (idna_to_ascii_8z (from, &ascii, 0) != IDNA_SUCCESS)
+		return (ISC_R_FAILURE);
+
+	(void) strcpy (to, ascii);
+	free (ascii);
+	return (ISC_R_SUCCESS);
+}
+/* based on idnkit's code*/
+static isc_result_t
+output_filter (isc_buffer_t *buffer, unsigned int used_org,
+	       isc_boolean_t absolute) {
+	char tmp1[MXNAME], *tmp2;
+        size_t fromlen, tolen;
+        isc_boolean_t end_with_dot;
+
+        fromlen = isc_buffer_usedlength(buffer) - used_org;
+	if (fromlen >= MXNAME)
+		return (ISC_R_SUCCESS);
+        memcpy(tmp1, (char *)isc_buffer_base(buffer) + used_org, fromlen);
+        end_with_dot = (tmp1[fromlen - 1] == '.') ? ISC_TRUE : ISC_FALSE;
+        if (absolute && !end_with_dot) {
+                fromlen++;
+		if (fromlen >= MXNAME)
+			return (ISC_R_SUCCESS);
+                tmp1[fromlen - 1] = '.';
+        }
+        tmp1[fromlen] = '\0';
+
+	if (idna_to_unicode_lzlz (tmp1, &tmp2, 0) != IDNA_SUCCESS)
+		return (ISC_R_SUCCESS);
+
+	(void) strcpy (tmp1, tmp2);
+	free (tmp2);
+
+        tolen = strlen(tmp1);
+        if (absolute && !end_with_dot && tmp1[tolen - 1] == '.')
+                tolen--;
+
+        if (isc_buffer_length(buffer) < used_org + tolen)
+                return (ISC_R_NOSPACE);
+
+        isc_buffer_subtract(buffer, isc_buffer_usedlength(buffer) - used_org);
+        memcpy(isc_buffer_used(buffer), tmp1, tolen);
+        isc_buffer_add(buffer, tolen);
+
+        return (ISC_R_SUCCESS);
+}
+#endif /* WITH_LIBIDN*/
 
 #ifdef DIG_SIGCHASE
 void