forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidna.c
326 lines (289 loc) · 9.65 KB
/
idna.c
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/**
* @file
* Handling of international domain names
*
* @authors
* Copyright (C) 2003,2005,2008-2009 Thomas Roessler <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page addr_idna Handling of international domain names
*
* Handling of international domain names
*/
#include "config.h"
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "mutt/mutt.h"
#include "idna2.h"
#ifdef HAVE_STRINGPREP_H
#include <stringprep.h>
#elif defined(HAVE_IDN_STRINGPREP_H)
#include <idn/stringprep.h>
#endif
#define IDN2_SKIP_LIBIDN_COMPAT
#ifdef HAVE_IDN2_H
#include <idn2.h>
#elif defined(HAVE_IDN_IDN2_H)
#include <idn/idn2.h>
#elif defined(HAVE_IDNA_H)
#include <idna.h>
#elif defined(HAVE_IDN_IDNA_H)
#include <idn/idna.h>
#endif
#if defined(HAVE_IDN2_H) || defined(HAVE_IDN_IDN2_H)
#define IDN_VERSION 2
#elif defined(HAVE_IDNA_H) || defined(HAVE_IDN_IDNA_H)
#define IDN_VERSION 1
#endif
/* These Config Variables are only used in mutt/idna.c */
#ifdef HAVE_LIBIDN
bool C_IdnDecode; ///< Config: (idn) Decode international domain names
bool C_IdnEncode; ///< Config: (idn) Encode international domain names
#endif
#ifdef HAVE_LIBIDN
/* Work around incompatibilities in the libidn API */
#if (!defined(HAVE_IDNA_TO_ASCII_8Z) && defined(HAVE_IDNA_TO_ASCII_FROM_UTF8))
#define idna_to_ascii_8z(input, output, flags) \
idna_to_ascii_from_utf8(input, output, (flags) &1, ((flags) &2) ? 1 : 0)
#endif
#if (!defined(HAVE_IDNA_TO_ASCII_LZ) && defined(HAVE_IDNA_TO_ASCII_FROM_LOCALE))
#define idna_to_ascii_lz(input, output, flags) \
idna_to_ascii_from_locale(input, output, (flags) &1, ((flags) &2) ? 1 : 0)
#endif
#if (!defined(HAVE_IDNA_TO_UNICODE_8Z8Z) && defined(HAVE_IDNA_TO_UNICODE_UTF8_FROM_UTF8))
#define idna_to_unicode_8z8z(input, output, flags) \
idna_to_unicode_utf8_from_utf8(input, output, (flags) &1, ((flags) &2) ? 1 : 0)
#endif
#endif /* HAVE_LIBIDN */
#ifdef HAVE_LIBIDN
/**
* check_idn - Is domain in Punycode?
* @param domain Domain to test
* @retval true At least one part of domain is in Punycode
*/
static bool check_idn(char *domain)
{
if (!domain)
return false;
if (mutt_str_startswith(domain, "xn--", CASE_IGNORE))
return true;
while ((domain = strchr(domain, '.')))
{
if (mutt_str_startswith(++domain, "xn--", CASE_IGNORE))
return true;
}
return false;
}
/**
* mutt_idna_to_ascii_lz - Convert a domain to Punycode
* @param[in] input Domain
* @param[out] output Result
* @param[in] flags Flags, e.g. IDNA_ALLOW_UNASSIGNED
* @retval 0 Success
* @retval >0 Failure, error code
*
* Convert a domain from the current locale to Punycode.
*
* @note The caller must free output
*/
int mutt_idna_to_ascii_lz(const char *input, char **output, int flags)
{
if (!input || !output)
return 1;
#if IDN_VERSION == 2
return idn2_to_ascii_8z(input, output, flags | IDN2_NFC_INPUT | IDN2_NONTRANSITIONAL);
#else
return idna_to_ascii_lz(input, output, flags);
#endif
}
#endif /* HAVE_LIBIDN */
/**
* mutt_idna_intl_to_local - Convert an email's domain from Punycode
* @param user Username
* @param domain Domain
* @param flags Flags, e.g. #MI_MAY_BE_IRREVERSIBLE
* @retval ptr Newly allocated local email address
* @retval NULL Error in conversion
*
* If #C_IdnDecode is set, then the domain will be converted from Punycode.
* For example, "xn--ls8h.la" becomes the emoji domain: ":poop:.la"
* Then the user and domain are changed from 'utf-8' to the encoding in
* #C_Charset.
*
* If the flag #MI_MAY_BE_IRREVERSIBLE is NOT given, then the results will be
* checked to make sure that the transformation is "undo-able".
*
* @note The caller must free the returned string.
*/
char *mutt_idna_intl_to_local(const char *user, const char *domain, int flags)
{
char *mailbox = NULL;
char *reversed_user = NULL, *reversed_domain = NULL;
char *tmp = NULL;
char *local_user = mutt_str_strdup(user);
char *local_domain = mutt_str_strdup(domain);
#ifdef HAVE_LIBIDN
bool is_idn_encoded = check_idn(local_domain);
if (is_idn_encoded && C_IdnDecode)
{
#if IDN_VERSION == 2
if (idn2_to_unicode_8z8z(local_domain, &tmp, IDN2_ALLOW_UNASSIGNED) != IDN2_OK)
#else
if (idna_to_unicode_8z8z(local_domain, &tmp, IDNA_ALLOW_UNASSIGNED) != IDNA_SUCCESS)
#endif
{
goto cleanup;
}
mutt_str_replace(&local_domain, tmp);
FREE(&tmp);
}
#endif /* HAVE_LIBIDN */
/* we don't want charset-hook effects, so we set flags to 0 */
if (mutt_ch_convert_string(&local_user, "utf-8", C_Charset, 0) != 0)
goto cleanup;
if (mutt_ch_convert_string(&local_domain, "utf-8", C_Charset, 0) != 0)
goto cleanup;
/* make sure that we can convert back and come out with the same
* user and domain name. */
if ((flags & MI_MAY_BE_IRREVERSIBLE) == 0)
{
reversed_user = mutt_str_strdup(local_user);
if (mutt_ch_convert_string(&reversed_user, C_Charset, "utf-8", 0) != 0)
{
mutt_debug(LL_DEBUG1, "Not reversible. Charset conv to utf-8 failed for user = '%s'\n",
reversed_user);
goto cleanup;
}
if (mutt_str_strcasecmp(user, reversed_user) != 0)
{
mutt_debug(LL_DEBUG1, "#1 Not reversible. orig = '%s', reversed = '%s'\n",
user, reversed_user);
goto cleanup;
}
reversed_domain = mutt_str_strdup(local_domain);
if (mutt_ch_convert_string(&reversed_domain, C_Charset, "utf-8", 0) != 0)
{
mutt_debug(LL_DEBUG1, "Not reversible. Charset conv to utf-8 failed for domain = '%s'\n",
reversed_domain);
goto cleanup;
}
#ifdef HAVE_LIBIDN
/* If the original domain was UTF-8, idna encoding here could
* produce a non-matching domain! Thus we only want to do the
* idna_to_ascii_8z() if the original domain was IDNA encoded. */
if (is_idn_encoded && C_IdnDecode)
{
#if IDN_VERSION == 2
if (idn2_to_ascii_8z(reversed_domain, &tmp,
IDN2_ALLOW_UNASSIGNED | IDN2_NFC_INPUT | IDN2_NONTRANSITIONAL) != IDN2_OK)
#else
if (idna_to_ascii_8z(reversed_domain, &tmp, IDNA_ALLOW_UNASSIGNED) != IDNA_SUCCESS)
#endif
{
mutt_debug(LL_DEBUG1, "Not reversible. idna_to_ascii_8z failed for domain = '%s'\n",
reversed_domain);
goto cleanup;
}
mutt_str_replace(&reversed_domain, tmp);
}
#endif /* HAVE_LIBIDN */
if (mutt_str_strcasecmp(domain, reversed_domain) != 0)
{
mutt_debug(LL_DEBUG1, "#2 Not reversible. orig = '%s', reversed = '%s'\n",
domain, reversed_domain);
goto cleanup;
}
}
mailbox = mutt_mem_malloc(mutt_str_strlen(local_user) + mutt_str_strlen(local_domain) + 2);
sprintf(mailbox, "%s@%s", NONULL(local_user), NONULL(local_domain));
cleanup:
FREE(&local_user);
FREE(&local_domain);
FREE(&tmp);
FREE(&reversed_domain);
FREE(&reversed_user);
return mailbox;
}
/**
* mutt_idna_local_to_intl - Convert an email's domain to Punycode
* @param user Username
* @param domain Domain
* @retval ptr Newly allocated Punycode email address
* @retval NULL Error in conversion
*
* The user and domain are assumed to be encoded according to #C_Charset.
* They are converted to 'utf-8'. If #C_IdnEncode is set, then the domain
* will be converted to Punycode. For example, the emoji domain:
* ":poop:.la" becomes "xn--ls8h.la"
*
* @note The caller must free the returned string.
*/
char *mutt_idna_local_to_intl(const char *user, const char *domain)
{
char *mailbox = NULL;
char *tmp = NULL;
char *intl_user = mutt_str_strdup(user);
char *intl_domain = mutt_str_strdup(domain);
/* we don't want charset-hook effects, so we set flags to 0 */
if (mutt_ch_convert_string(&intl_user, C_Charset, "utf-8", 0) != 0)
goto cleanup;
if (mutt_ch_convert_string(&intl_domain, C_Charset, "utf-8", 0) != 0)
goto cleanup;
#ifdef HAVE_LIBIDN
if (C_IdnEncode)
{
#if IDN_VERSION == 2
if (idn2_to_ascii_8z(intl_domain, &tmp,
IDN2_ALLOW_UNASSIGNED | IDN2_NFC_INPUT | IDN2_NONTRANSITIONAL) != IDN2_OK)
#else
if (idna_to_ascii_8z(intl_domain, &tmp, IDNA_ALLOW_UNASSIGNED) != IDNA_SUCCESS)
#endif
{
goto cleanup;
}
mutt_str_replace(&intl_domain, tmp);
}
#endif /* HAVE_LIBIDN */
mailbox = mutt_mem_malloc(mutt_str_strlen(intl_user) + mutt_str_strlen(intl_domain) + 2);
sprintf(mailbox, "%s@%s", NONULL(intl_user), NONULL(intl_domain));
cleanup:
FREE(&intl_user);
FREE(&intl_domain);
FREE(&tmp);
return mailbox;
}
/**
* mutt_idna_print_version - Create an IDN version string
* @retval ptr Version string
*
* @note This is a static string and must not be freed.
*/
const char *mutt_idna_print_version(void)
{
static char vstring[256];
#ifdef HAVE_LIBIDN
#if IDN_VERSION == 2
snprintf(vstring, sizeof(vstring), "libidn2: %s (compiled with %s)",
idn2_check_version(NULL), IDN2_VERSION);
#elif IDN_VERSION == 1
snprintf(vstring, sizeof(vstring), "libidn: %s (compiled with %s)",
stringprep_check_version(NULL), STRINGPREP_VERSION);
#endif
#endif /* HAVE_LIBIDN */
return vstring;
}