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