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