1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) Jeremy Harris 2015, 2016 */
6 /* See the file NOTICE for conditions of use and distribution. */
13 #ifdef SUPPORT_I18N_2008
20 #include <stringprep.h>
23 string_localpart_alabel_to_utf8_(const uschar * alabel, uschar ** err);
25 /**************************************************/
28 string_is_utf8(const uschar * s)
31 if (s) while ((c = *s++)) if (c & 0x80) return TRUE;
36 string_is_alabel(const uschar * s)
38 return s[0] == 'x' && s[1] == 'n' && s[2] == '-' && s[3] == '-';
41 /**************************************************/
42 /* Domain conversions.
43 The *err string pointer should be null before the call
45 Return NULL for error, with optional errstr pointer filled in
49 string_domain_utf8_to_alabel(const uschar * utf8, uschar ** err)
54 #ifdef SUPPORT_I18N_2008
55 /* Avoid lowercasing plain-ascii domains */
56 if (!string_is_utf8(utf8))
57 return string_copy(utf8);
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. */
63 for (s1 = s = US utf8; (c = *s1); s1++) if (!(c & 0x80) && isupper(c))
65 s = string_copy(utf8);
66 for (s1 = s + (s1 - utf8); (c = *s1); s1++) if (!(c & 0x80) && isupper(c))
71 if ((rc = idn2_lookup_u8(CCS s, &s1, IDN2_NFC_INPUT)) != IDN2_OK)
73 if (err) *err = US idn2_strerror(rc);
77 s = US stringprep_utf8_nfkc_normalize(CCS utf8, -1);
78 if ( (rc = idna_to_ascii_8z(CCS s, CSS &s1, IDNA_ALLOW_UNASSIGNED))
82 if (err) *err = US idna_strerror(rc);
95 string_domain_alabel_to_utf8(const uschar * alabel, uschar ** err)
97 #ifdef SUPPORT_I18N_2008
102 while (label = string_nextinlist(&alabel, &sep, NULL, 0))
103 if ( string_is_alabel(label)
104 && !(label = string_localpart_alabel_to_utf8_(label, err))
108 s = string_append_listele(s, '.', label);
116 if ( (rc = idna_to_unicode_8z8z(CCS alabel, CSS &s1, IDNA_USE_STD3_ASCII_RULES))
119 if (err) *err = US idna_strerror(rc);
128 /**************************************************/
129 /* localpart conversions */
130 /* the *err string pointer should be null before the call */
134 string_localpart_utf8_to_alabel(const uschar * utf8, uschar ** err)
142 if (!string_is_utf8(utf8)) return string_copy(utf8);
144 p = (punycode_uint *) stringprep_utf8_to_ucs4(CCS utf8, -1, &ucs4_len);
145 p_len = ucs4_len*4; /* this multiplier is pure guesswork */
146 res = store_get(p_len+5);
148 res[0] = 'x'; res[1] = 'n'; res[2] = res[3] = '-';
150 if ((rc = punycode_encode(ucs4_len, p, NULL, &p_len, CS res+4)) != PUNYCODE_SUCCESS)
152 DEBUG(D_expand) debug_printf("l_u2a: bad '%s'\n", punycode_strerror(rc));
154 if (err) *err = US punycode_strerror(rc);
165 string_localpart_alabel_to_utf8_(const uschar * alabel, uschar ** err)
172 DEBUG(D_expand) debug_printf("l_a2u: '%s'\n", alabel);
174 p_len = Ustrlen(alabel);
175 p = (punycode_uint *) store_get((p_len+1) * sizeof(*p));
177 if ((rc = punycode_decode(p_len, CCS alabel, &p_len, p, NULL)) != PUNYCODE_SUCCESS)
179 if (err) *err = US punycode_strerror(rc);
183 s = US stringprep_ucs4_to_utf8(p, p_len, NULL, &p_len);
184 res = string_copyn(s, p_len);
191 string_localpart_alabel_to_utf8(const uschar * alabel, uschar ** err)
193 if (string_is_alabel(alabel))
194 return string_localpart_alabel_to_utf8_(alabel, err);
196 if (err) *err = US"bad alabel prefix";
201 /**************************************************/
202 /* Whole address conversion.
203 The *err string pointer should be null before the call.
205 Return NULL on error, with (optional) errstring pointer filled in
209 string_address_utf8_to_alabel(const uschar * utf8, uschar ** err)
215 if (!*utf8) return string_copy(utf8);
217 DEBUG(D_expand) debug_printf("addr from utf8 <%s>", utf8);
219 for (s = utf8; *s; s++)
222 l = string_copyn(utf8, s - utf8);
223 if ( !(l = string_localpart_utf8_to_alabel(l, err))
224 || !(d = string_domain_utf8_to_alabel(++s, err))
227 l = string_sprintf("%s@%s", l, d);
228 DEBUG(D_expand) debug_printf(" -> <%s>\n", l);
232 l = string_localpart_utf8_to_alabel(utf8, err);
233 DEBUG(D_expand) debug_printf(" -> <%s>\n", l);
239 /*************************************************
240 * Report the library versions. *
241 *************************************************/
243 /* See a description in tls-openssl.c for an explanation of why this exists.
245 Arguments: a FILE* to print the results to
250 utf8_version_report(FILE *f)
252 #ifdef SUPPORT_I18N_2008
253 fprintf(f, "Library version: IDN2: Compile: %s\n"
256 idn2_check_version(NULL));
257 fprintf(f, "Library version: Stringprep: Compile: %s\n"
260 stringprep_check_version(NULL));
262 fprintf(f, "Library version: IDN: Compile: %s\n"
265 stringprep_check_version(NULL));
269 #endif /* whole file */