I18N: support IDNA2008. Bug 1911
[exim.git] / src / src / utf8.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) Jeremy Harris 2015, 2016 */
6 /* See the file NOTICE for conditions of use and distribution. */
7
8
9 #include "exim.h"
10
11 #ifdef SUPPORT_I18N
12
13 #ifdef SUPPORT_I18N_2008
14 # include <idn2.h>
15 #else
16 # include <idna.h>
17 #endif
18
19 #include <punycode.h>
20 #include <stringprep.h>
21
22 static uschar *
23 string_localpart_alabel_to_utf8_(const uschar * alabel, uschar ** err);
24
25 /**************************************************/
26
27 BOOL
28 string_is_utf8(const uschar * s)
29 {
30 uschar c;
31 if (s) while ((c = *s++)) if (c & 0x80) return TRUE;
32 return FALSE;
33 }
34
35 static BOOL
36 string_is_alabel(const uschar * s)
37 {
38 return s[0] == 'x' && s[1] == 'n' && s[2] == '-' && s[3] == '-';
39 }
40
41 /**************************************************/
42 /* Domain conversions.
43 The *err string pointer should be null before the call
44
45 Return NULL for error, with optional errstr pointer filled in
46 */
47
48 uschar *
49 string_domain_utf8_to_alabel(const uschar * utf8, uschar ** err)
50 {
51 uschar * s1, * s;
52 int rc;
53
54 #ifdef SUPPORT_I18N_2008
55 /* Only lowercase is accepted by the library call.  A pity since we lose
56 any mixed-case annotation.  This does not really matter for a domain. */
57   {
58   uschar c;
59   for (s1 = s = US utf8; (c = *s1); s1++) if (!(c & 0x80) && isupper(c))
60     {
61     s = string_copy(utf8);
62     for (s1 = s + (s1 - utf8); (c = *s1); s1++) if (!(c & 0x80) && isupper(c))
63       *s1 = tolower(c);
64     break;
65     }
66   }
67 if ((rc = idn2_lookup_u8(CCS s, &s1, IDN2_NFC_INPUT)) != IDN2_OK)
68   {
69   if (err) *err = US idn2_strerror(rc);
70   return NULL;
71   }
72 #else
73 s = US stringprep_utf8_nfkc_normalize(CCS utf8, -1);
74 if (  (rc = idna_to_ascii_8z(CCS s, CSS &s1, IDNA_ALLOW_UNASSIGNED))
75    != IDNA_SUCCESS)
76   {
77   free(s);
78   if (err) *err = US idna_strerror(rc);
79   return NULL;
80   }
81 free(s);
82 #endif
83 s = string_copy(s1);
84 free(s1);
85 return s;
86 }
87
88
89
90 uschar *
91 string_domain_alabel_to_utf8(const uschar * alabel, uschar ** err)
92 {
93 #ifdef SUPPORT_I18N_2008
94 const uschar * label;
95 int sep = '.';
96 uschar * s = NULL;
97
98 while (label = string_nextinlist(&alabel, &sep, NULL, 0))
99   if (  string_is_alabel(label)
100      && !(label = string_localpart_alabel_to_utf8_(label, err))
101      )
102     return NULL;
103   else
104     s = string_append_listele(s, '.', label);
105 return s;
106
107 #else
108
109 uschar * s1, * s;
110 int rc;
111
112 if (  (rc = idna_to_unicode_8z8z(CCS alabel, CSS &s1, IDNA_USE_STD3_ASCII_RULES))
113    != IDNA_SUCCESS)
114   {
115   if (err) *err = US idna_strerror(rc);
116   return NULL;
117   }
118 s = string_copy(s1);
119 free(s1);
120 return s;
121 #endif
122 }
123
124 /**************************************************/
125 /* localpart conversions */
126 /* the *err string pointer should be null before the call */
127
128
129 uschar *
130 string_localpart_utf8_to_alabel(const uschar * utf8, uschar ** err)
131 {
132 size_t ucs4_len;
133 punycode_uint * p;
134 size_t p_len;
135 uschar * res;
136 int rc;
137
138 if (!string_is_utf8(utf8)) return string_copy(utf8);
139
140 p = (punycode_uint *) stringprep_utf8_to_ucs4(CCS utf8, -1, &ucs4_len);
141 p_len = ucs4_len*4;     /* this multiplier is pure guesswork */
142 res = store_get(p_len+5);
143
144 res[0] = 'x'; res[1] = 'n'; res[2] = res[3] = '-';
145
146 if ((rc = punycode_encode(ucs4_len, p, NULL, &p_len, CS res+4)) != PUNYCODE_SUCCESS)
147   {
148   DEBUG(D_expand) debug_printf("l_u2a: bad '%s'\n", punycode_strerror(rc));
149   free(p);
150   if (err) *err = US punycode_strerror(rc);
151   return NULL;
152   }
153 p_len += 4;
154 free(p);
155 res[p_len] = '\0';
156 return res;
157 }
158
159
160 static uschar *
161 string_localpart_alabel_to_utf8_(const uschar * alabel, uschar ** err)
162 {
163 size_t p_len;
164 punycode_uint * p;
165 int rc;
166 uschar * s, * res;
167
168 DEBUG(D_expand) debug_printf("l_a2u: '%s'\n", alabel);
169 alabel += 4;
170 p_len = Ustrlen(alabel);
171 p = (punycode_uint *) store_get((p_len+1) * sizeof(*p));
172
173 if ((rc = punycode_decode(p_len, CCS alabel, &p_len, p, NULL)) != PUNYCODE_SUCCESS)
174   {
175   if (err) *err = US punycode_strerror(rc);
176   return NULL;
177   }
178
179 s = US stringprep_ucs4_to_utf8(p, p_len, NULL, &p_len);
180 res = string_copyn(s, p_len);
181 free(s);
182 return res;
183 }
184
185
186 uschar *
187 string_localpart_alabel_to_utf8(const uschar * alabel, uschar ** err)
188 {
189 if (string_is_alabel(alabel))
190   return string_localpart_alabel_to_utf8_(alabel, err);
191
192 if (err) *err = US"bad alabel prefix";
193 return NULL;
194 }
195
196
197 /**************************************************/
198 /* Whole address conversion.
199 The *err string pointer should be null before the call.
200
201 Return NULL on oeeror, with (optional) errstring pointer filled in
202 */
203
204 uschar *
205 string_address_utf8_to_alabel(const uschar * utf8, uschar ** err)
206 {
207 const uschar * s;
208 uschar * l;
209 uschar * d;
210
211 if (!*utf8) return string_copy(utf8);
212
213 DEBUG(D_expand) debug_printf("addr from utf8 <%s>", utf8);
214
215 for (s = utf8; *s; s++)
216   if (*s == '@')
217     {
218     l = string_copyn(utf8, s - utf8);
219     if (  !(l = string_localpart_utf8_to_alabel(l, err))
220        || !(d = string_domain_utf8_to_alabel(++s, err))
221        )
222       return NULL;
223     l = string_sprintf("%s@%s", l, d);
224     DEBUG(D_expand) debug_printf(" -> <%s>\n", l);
225     return l;
226     }
227
228 l =  string_localpart_utf8_to_alabel(utf8, err);
229 DEBUG(D_expand) debug_printf(" -> <%s>\n", l);
230 return l;
231 }
232
233
234
235 /*************************************************
236 *         Report the library versions.           *
237 *************************************************/
238
239 /* See a description in tls-openssl.c for an explanation of why this exists.
240
241 Arguments:   a FILE* to print the results to
242 Returns:     nothing
243 */
244
245 void
246 utf8_version_report(FILE *f)
247 {
248 #ifdef SUPPORT_I18N_2008
249 fprintf(f, "Library version: IDN2: Compile: %s\n"
250            "                       Runtime: %s\n",
251         IDN2_VERSION,
252         idn2_check_version(NULL));
253 fprintf(f, "Library version: Stringprep: Compile: %s\n"
254            "                             Runtime: %s\n",
255         STRINGPREP_VERSION,
256         stringprep_check_version(NULL));
257 #else
258 fprintf(f, "Library version: IDN: Compile: %s\n"
259            "                      Runtime: %s\n",
260         STRINGPREP_VERSION,
261         stringprep_check_version(NULL));
262 #endif
263 }
264
265 #endif  /* whole file */
266
267 /* vi: aw ai sw=2
268 */
269 /* End of utf8.c */