1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
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 */
15 #ifdef SUPPORT_I18N_2008
22 #include <stringprep.h>
25 string_localpart_alabel_to_utf8_(const uschar * alabel, uschar ** err);
27 /**************************************************/
30 string_is_utf8(const uschar * s)
33 if (s) while ((c = *s++)) if (c & 0x80) return TRUE;
38 string_is_alabel(const uschar * s)
40 return s[0] == 'x' && s[1] == 'n' && s[2] == '-' && s[3] == '-';
43 /**************************************************/
44 /* Domain conversions.
45 The *err string pointer should be null before the call
47 Return NULL for error, with optional errstr pointer filled in
51 string_domain_utf8_to_alabel(const uschar * utf8, uschar ** err)
56 #ifdef SUPPORT_I18N_2008
57 /* Avoid lowercasing plain-ascii domains */
58 if (!string_is_utf8(utf8))
59 return string_copy(utf8);
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. */
65 for (s1 = s = US utf8; (c = *s1); s1++) if (!(c & 0x80) && isupper(c))
67 s = string_copy(utf8);
68 for (s1 = s + (s1 - utf8); (c = *s1); s1++) if (!(c & 0x80) && isupper(c))
73 if ((rc = idn2_lookup_u8((const uint8_t *) s, &s1, IDN2_NFC_INPUT)) != IDN2_OK)
75 if (err) *err = US idn2_strerror(rc);
79 s = US stringprep_utf8_nfkc_normalize(CCS utf8, -1);
80 if ( (rc = idna_to_ascii_8z(CCS s, CSS &s1, IDNA_ALLOW_UNASSIGNED))
84 if (err) *err = US idna_strerror(rc);
97 string_domain_alabel_to_utf8(const uschar * alabel, uschar ** err)
99 #ifdef SUPPORT_I18N_2008
100 const uschar * label;
104 while (label = string_nextinlist(&alabel, &sep, NULL, 0))
105 if ( string_is_alabel(label)
106 && !(label = string_localpart_alabel_to_utf8_(label, err))
110 g = string_append_listele(g, '.', label);
111 return string_from_gstring(g);
118 if ( (rc = idna_to_unicode_8z8z(CCS alabel, CSS &s1, IDNA_USE_STD3_ASCII_RULES))
121 if (err) *err = US idna_strerror(rc);
130 /**************************************************/
131 /* localpart conversions */
132 /* the *err string pointer should be null before the call */
136 string_localpart_utf8_to_alabel(const uschar * utf8, uschar ** err)
144 if (!string_is_utf8(utf8)) return string_copy(utf8);
146 p = (punycode_uint *) stringprep_utf8_to_ucs4(CCS utf8, -1, &ucs4_len);
149 if (err) *err = US"l_u2a: bad UTF-8 input";
152 p_len = ucs4_len*4; /* this multiplier is pure guesswork */
153 res = store_get(p_len+5, utf8);
155 res[0] = 'x'; res[1] = 'n'; res[2] = res[3] = '-';
157 if ((rc = punycode_encode(ucs4_len, p, NULL, &p_len, CS res+4)) != PUNYCODE_SUCCESS)
159 DEBUG(D_expand) debug_printf("l_u2a: bad '%s'\n", punycode_strerror(rc));
161 if (err) *err = US punycode_strerror(rc);
172 string_localpart_alabel_to_utf8_(const uschar * alabel, uschar ** err)
179 DEBUG(D_expand) debug_printf("l_a2u: '%s'\n", alabel);
181 p_len = Ustrlen(alabel);
182 p = store_get((p_len+1) * sizeof(*p), alabel);
184 if ((rc = punycode_decode(p_len, CCS alabel, &p_len, p, NULL)) != PUNYCODE_SUCCESS)
186 if (err) *err = US punycode_strerror(rc);
190 s = US stringprep_ucs4_to_utf8(p, p_len, NULL, &p_len);
191 res = string_copyn(s, p_len);
198 string_localpart_alabel_to_utf8(const uschar * alabel, uschar ** err)
200 if (string_is_alabel(alabel))
201 return string_localpart_alabel_to_utf8_(alabel, err);
203 if (err) *err = US"bad alabel prefix";
208 /**************************************************/
209 /* Whole address conversion.
210 The *err string pointer should be null before the call.
212 Return NULL on error, with (optional) errstring pointer filled in
216 string_address_utf8_to_alabel(const uschar * utf8, uschar ** err)
220 if (!*utf8) return string_copy(utf8);
222 DEBUG(D_expand) debug_printf("addr from utf8 <%s>", utf8);
224 for (const uschar * s = utf8; *s; s++)
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))
232 l = string_sprintf("%s@%s", l, d);
233 DEBUG(D_expand) debug_printf(" -> <%s>\n", l);
237 l = string_localpart_utf8_to_alabel(utf8, err);
238 DEBUG(D_expand) debug_printf(" -> <%s>\n", l);
244 /*************************************************
245 * Report the library versions. *
246 *************************************************/
248 /* See a description in tls-openssl.c for an explanation of why this exists.
250 Arguments: string to append to
255 utf8_version_report(gstring * g)
257 #ifdef SUPPORT_I18N_2008
258 g = string_fmt_append(g, "Library version: IDN2: Compile: %s\n"
261 idn2_check_version(NULL));
262 g = string_fmt_append(g, "Library version: Stringprep: Compile: %s\n"
265 stringprep_check_version(NULL));
267 g = string_fmt_append(g, "Library version: IDN: Compile: %s\n"
270 stringprep_check_version(NULL));
275 #endif /* whole file */