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