Debug: feed startup "whats supported" info through normal debug channel
[exim.git] / src / src / utf8.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) Jeremy Harris 2015 - 2018 */
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 /* Avoid lowercasing plain-ascii domains */
56 if (!string_is_utf8(utf8))
57   return string_copy(utf8);
58
59 /* Only lowercase is accepted by the library call.  A pity since we lose
60 any mixed-case annotation.  This does not really matter for a domain. */
61   {
62   uschar c;
63   for (s1 = s = US utf8; (c = *s1); s1++) if (!(c & 0x80) && isupper(c))
64     {
65     s = string_copy(utf8);
66     for (s1 = s + (s1 - utf8); (c = *s1); s1++) if (!(c & 0x80) && isupper(c))
67       *s1 = tolower(c);
68     break;
69     }
70   }
71 if ((rc = idn2_lookup_u8((const uint8_t *) s, &s1, IDN2_NFC_INPUT)) != IDN2_OK)
72   {
73   if (err) *err = US idn2_strerror(rc);
74   return NULL;
75   }
76 #else
77 s = US stringprep_utf8_nfkc_normalize(CCS utf8, -1);
78 if (  (rc = idna_to_ascii_8z(CCS s, CSS &s1, IDNA_ALLOW_UNASSIGNED))
79    != IDNA_SUCCESS)
80   {
81   free(s);
82   if (err) *err = US idna_strerror(rc);
83   return NULL;
84   }
85 free(s);
86 #endif
87 s = string_copy(s1);
88 free(s1);
89 return s;
90 }
91
92
93
94 uschar *
95 string_domain_alabel_to_utf8(const uschar * alabel, uschar ** err)
96 {
97 #ifdef SUPPORT_I18N_2008
98 const uschar * label;
99 int sep = '.';
100 gstring * g = NULL;
101
102 while (label = string_nextinlist(&alabel, &sep, NULL, 0))
103   if (  string_is_alabel(label)
104      && !(label = string_localpart_alabel_to_utf8_(label, err))
105      )
106     return NULL;
107   else
108     g = string_append_listele(g, '.', label);
109 return string_from_gstring(g);
110
111 #else
112
113 uschar * s1, * s;
114 int rc;
115
116 if (  (rc = idna_to_unicode_8z8z(CCS alabel, CSS &s1, IDNA_USE_STD3_ASCII_RULES))
117    != IDNA_SUCCESS)
118   {
119   if (err) *err = US idna_strerror(rc);
120   return NULL;
121   }
122 s = string_copy(s1);
123 free(s1);
124 return s;
125 #endif
126 }
127
128 /**************************************************/
129 /* localpart conversions */
130 /* the *err string pointer should be null before the call */
131
132
133 uschar *
134 string_localpart_utf8_to_alabel(const uschar * utf8, uschar ** err)
135 {
136 size_t ucs4_len = 0;
137 punycode_uint * p;
138 size_t p_len;
139 uschar * res;
140 int rc;
141
142 if (!string_is_utf8(utf8)) return string_copy(utf8);
143
144 p = (punycode_uint *) stringprep_utf8_to_ucs4(CCS utf8, -1, &ucs4_len);
145 if (!p || !ucs4_len)
146   {
147   if (err) *err = US"l_u2a: bad UTF-8 input";
148   return NULL;
149   }
150 p_len = ucs4_len*4;     /* this multiplier is pure guesswork */
151 res = store_get(p_len+5, is_tainted(utf8));
152
153 res[0] = 'x'; res[1] = 'n'; res[2] = res[3] = '-';
154
155 if ((rc = punycode_encode(ucs4_len, p, NULL, &p_len, CS res+4)) != PUNYCODE_SUCCESS)
156   {
157   DEBUG(D_expand) debug_printf("l_u2a: bad '%s'\n", punycode_strerror(rc));
158   free(p);
159   if (err) *err = US punycode_strerror(rc);
160   return NULL;
161   }
162 p_len += 4;
163 free(p);
164 res[p_len] = '\0';
165 return res;
166 }
167
168
169 static uschar *
170 string_localpart_alabel_to_utf8_(const uschar * alabel, uschar ** err)
171 {
172 size_t p_len;
173 punycode_uint * p;
174 int rc;
175 uschar * s, * res;
176
177 DEBUG(D_expand) debug_printf("l_a2u: '%s'\n", alabel);
178 alabel += 4;
179 p_len = Ustrlen(alabel);
180 p = store_get((p_len+1) * sizeof(*p), is_tainted(alabel));
181
182 if ((rc = punycode_decode(p_len, CCS alabel, &p_len, p, NULL)) != PUNYCODE_SUCCESS)
183   {
184   if (err) *err = US punycode_strerror(rc);
185   return NULL;
186   }
187
188 s = US stringprep_ucs4_to_utf8(p, p_len, NULL, &p_len);
189 res = string_copyn(s, p_len);
190 free(s);
191 return res;
192 }
193
194
195 uschar *
196 string_localpart_alabel_to_utf8(const uschar * alabel, uschar ** err)
197 {
198 if (string_is_alabel(alabel))
199   return string_localpart_alabel_to_utf8_(alabel, err);
200
201 if (err) *err = US"bad alabel prefix";
202 return NULL;
203 }
204
205
206 /**************************************************/
207 /* Whole address conversion.
208 The *err string pointer should be null before the call.
209
210 Return NULL on error, with (optional) errstring pointer filled in
211 */
212
213 uschar *
214 string_address_utf8_to_alabel(const uschar * utf8, uschar ** err)
215 {
216 uschar * l, * d;
217
218 if (!*utf8) return string_copy(utf8);
219
220 DEBUG(D_expand) debug_printf("addr from utf8 <%s>", utf8);
221
222 for (const uschar * s = utf8; *s; s++)
223   if (*s == '@')
224     {
225     l = string_copyn(utf8, s - utf8);
226     if (  !(l = string_localpart_utf8_to_alabel(l, err))
227        || !(d = string_domain_utf8_to_alabel(++s, err))
228        )
229       return NULL;
230     l = string_sprintf("%s@%s", l, d);
231     DEBUG(D_expand) debug_printf(" -> <%s>\n", l);
232     return l;
233     }
234
235 l =  string_localpart_utf8_to_alabel(utf8, err);
236 DEBUG(D_expand) debug_printf(" -> <%s>\n", l);
237 return l;
238 }
239
240
241
242 /*************************************************
243 *         Report the library versions.           *
244 *************************************************/
245
246 /* See a description in tls-openssl.c for an explanation of why this exists.
247
248 Arguments:   string to append to
249 Returns:     string
250 */
251
252 gstring *
253 utf8_version_report(gstring * g)
254 {
255 #ifdef SUPPORT_I18N_2008
256 g = string_fmt_append(g, "Library version: IDN2: Compile: %s\n"
257            "                       Runtime: %s\n",
258         IDN2_VERSION,
259         idn2_check_version(NULL));
260 g = string_fmt_append(g, "Library version: Stringprep: Compile: %s\n"
261            "                             Runtime: %s\n",
262         STRINGPREP_VERSION,
263         stringprep_check_version(NULL));
264 #else
265 g = string_fmt_append(g, "Library version: IDN: Compile: %s\n"
266            "                      Runtime: %s\n",
267         STRINGPREP_VERSION,
268         stringprep_check_version(NULL));
269 #endif
270 return g;
271 }
272
273 #endif  /* whole file */
274
275 /* vi: aw ai sw=2
276 */
277 /* End of utf8.c */