-TBOUNCE_LOG_FORGE
-TBOUNCE_LOG_RCPT_BUF
-TBOUNCE_STAT
+-TBOUNCE_STR_PARAMETER
-TBOUNCE_TEMPLATE
-TBOUNCE_TEMPLATES
-TBOUNCE_TIME_DIVISOR
Cleanup: hard-coded GCC dependencies. Eray Aslan. File:
makedefs.
+
+20140717
+
+ Safety: manipulate unsigned characters while decoding.
+ Files: global/xtext.c, global/uxtext.c.
+
+ Infrastructure: ACE label to UTF-8 conversion. Files:
+ util/midna.[hc].
+
+ Infrastructure: macro expansion with printable() filter.
+ Files: util/mac_expand.[hc].
+
+ Feature: when expanding myhostname or mydomain in bounce
+ template messages, and smtputf8_enable=yes, convert ACE
+ (xn--mumble) labels into UTF-8. bounce/bounce_template.c.
Things to do after the stable release:
+ up-convert myhostname in MIME boundary strings?
+
+ Table-driven case folding and case-insensitive string
+ comparison specifically for UTF-8.
+
When downgrading message/global to 7bit, is quoted-printable
the appropriate encoding? Should it be base64?
<b>DESCRIPTION</b>
The Postfix <a href="bounce.8.html"><b>bounce</b>(8)</a> server produces delivery status notification
- (DSN) messages for undeliverable mail, delayed mail, successful deliv-
+ (DSN) messages for undeliverable mail, delayed mail, successful deliv‐
ery or address verification requests.
By default, these notifications are generated from built-in templates
with message headers and message text. Sites can override the built-in
- information by specifying a bounce template file with the <b><a href="postconf.5.html#bounce_template_file">bounce_tem</a>-</b>
- <b><a href="postconf.5.html#bounce_template_file">plate_file</a></b> configuration parameter.
+ information by specifying a bounce template file with the <b>bounce_tem</b>‐\b‐
+ <b>plate_file</b> configuration parameter.
- This document describes the general procedure to create a bounce tem-
- plate file, followed by the specific details of bounce template for-
+ This document describes the general procedure to create a bounce tem‐
+ plate file, followed by the specific details of bounce template for‐
mats.
<b>GENERAL PROCEDURE</b>
expansion of time value parameters that appear in the delayed mail
notification text.
- Once the result is satisfactory, copy the template to the Postfix con-
+ Once the result is satisfactory, copy the template to the Postfix con‐
figuration directory and specify in <a href="postconf.5.html">main.cf</a> something like:
/etc/postfix/<a href="postconf.5.html">main.cf</a>:
<b>TEMPLATE FILE FORMAT</b>
The template file can specify templates for failed mail, delayed mail,
successful delivery or for address verification. These templates are
- named <b>failure_template</b>, <b>delay_template</b>, <b>success_template</b> and <b>ver-</b>
+ named <b>failure_template</b>, <b>delay_template</b>, <b>success_template</b> and <b>ver</b>‐\b‐
<b>ify_template</b>, respectively. You can but do not have to specify all
four templates in a bounce template file.
Each template starts with "<i>template</i><b>_</b><i>name</i> <b>=</b> <<<b>EOF</b>" and ends with a line
that contains the word "<b>EOF</b>" only. You can change the word EOF, but you
- can't enclose it in quotes as with the shell or with Perl (<i>tem-</i>
+ can't enclose it in quotes as with the shell or with Perl (<i>tem‐</i>
<i>plate</i><b>_</b><i>name</i> <b>=</b> <<<b>'EOF'</b>). Here is an example:
# The failure template is used for undeliverable mail.
The mail system
EOF
- The usage and specification of bounce templates is subject to the fol-
+ The usage and specification of bounce templates is subject to the fol‐
lowing restrictions:
- <b>o</b> No special meaning is given to the backslash character or to
+ · No special meaning is given to the backslash character or to
leading whitespace; these are always taken literally.
- <b>o</b> Inside the << context, the "$" character is special. To produce
+ · Inside the << context, the "$" character is special. To produce
a "$" character as output, specify "$$".
- <b>o</b> Outside the << context, lines beginning with "#" are ignored, as
+ · Outside the << context, lines beginning with "#" are ignored, as
are empty lines, and lines consisting of whitespace only.
Examples of all templates can be found in the file <b>bounce.cf.default</b> in
<b>TEMPLATE HEADER FORMAT</b>
The first portion of a bounce template consists of optional template
- headers. Some become message headers in the delivery status notifica-
+ headers. Some become message headers in the delivery status notifica‐
tion; some control the formatting of that notification. Headers not
specified in a template will be left at their default value.
notification.
<b>Subject:</b>
- The subject in the message header of the delivery status notifi-
+ The subject in the message header of the delivery status notifi‐
cation that is returned to the sender.
<b>Postmaster-Subject:</b>
- The subject that will be used in Postmaster copies of undeliver-
+ The subject that will be used in Postmaster copies of undeliver‐
able or delayed mail notifications. These copies are sent under
control of the <a href="postconf.5.html#notify_classes">notify_classes</a> configuration parameter.
The usage and specification of template message headers is subject to
the following restrictions:
- <b>o</b> Template message header names can be specified in upper case,
+ · Template message header names can be specified in upper case,
lower case or mixed case. Postfix always produces bounce message
header labels of the form "<b>From:</b>" and "<b>Subject:</b>".
- <b>o</b> Template message headers must not span multiple lines.
+ · Template message headers must not span multiple lines.
- <b>o</b> Template message headers do not support $parameter expansions.
+ · Template message headers do not support $parameter expansions.
- <b>o</b> Template message headers must contain ASCII characters only, and
+ · Template message headers must contain ASCII characters only, and
must not contain ASCII null characters.
<b>TEMPLATE MESSAGE TEXT FORMAT</b>
expressed in the time unit specified by <i>suffix</i>. See above under
<b><a href="postconf.5.html#delay_warning_time">delay_warning_time</a></b> for possible <i>suffix</i> values.
+ <b><a href="postconf.5.html#mydomain">mydomain</a></b>
+ Expands into the value of the <b><a href="postconf.5.html#mydomain">mydomain</a></b> parameter. With "smt‐
+ putf8_enable = yes", this replaces ACE labels (xn--mumble) with
+ their UTF-8 equivalent.
+
+ This feature is available in Postfix 2.12.
+
+ <b><a href="postconf.5.html#myhostname">myhostname</a></b>
+ Expands into the value of the <b><a href="postconf.5.html#myhostname">myhostname</a></b> parameter. With "smt‐
+ putf8_enable = yes", this replaces ACE labels (xn--mumble) with
+ their UTF-8 equivalent.
+
+ This feature is available in Postfix 2.12.
+
The usage and specification of template message text is subject to the
following restrictions:
- <b>o</b> The template message text is not sent in Postmaster copies of
+ · The template message text is not sent in Postmaster copies of
delivery status notifications.
- <b>o</b> If the template message text contains non-ASCII characters,
+ · If the template message text contains non-ASCII characters,
Postfix requires that the <b>Charset:</b> template header is updated.
Specify an appropriate superset of US-ASCII. A superset is
- needed because Postfix appends ASCII text after the message tem-
+ needed because Postfix appends ASCII text after the message tem‐
plate when it sends a delivery status notification.
<b>SEE ALSO</b>
daemon. </dd>
<dt> <b> forward </b> </dt> <dd> Local forwarding or aliasing. When
-a message is received with "SMTPUTF8 requested", then the forwarded
-(aliased) message automatically has "SMTPUTF8 requested". </dd>
+a message is received with "SMTPUTF8 required", then the forwarded
+(aliased) message always has "SMTPUTF8 required". </dd>
<dt> <b> bounce </b> </dt> <dd> Submission by the <a href="bounce.8.html">bounce(8)</a> daemon.
-When a message is received with "SMTPUTF8 requested", then the
-delivery status notification automatically has "SMTPUTF8 requested".
-</dd>
+When a message is received with "SMTPUTF8 required", then the
+delivery status notification always has "SMTPUTF8 required". </dd>
<dt> <b> notify </b> </dt> <dd> Postmaster notification from the
<a href="smtp.8.html">smtp(8)</a> or <a href="smtpd.8.html">smtpd(8)</a> daemon. </dd>
parameter, expressed in the time unit specified by
\fIsuffix\fR. See above under \fBdelay_warning_time\fR for
possible \fIsuffix\fR values.
+.IP \fBmydomain\fR
+Expands into the value of the \fBmydomain\fR parameter.
+With "smtputf8_enable = yes", this replaces ACE labels
+(xn--mumble) with their UTF-8 equivalent.
+.sp
+This feature is available in Postfix 2.12.
+.IP \fBmyhostname\fR
+Expands into the value of the \fBmyhostname\fR parameter.
+With "smtputf8_enable = yes", this replaces ACE labels
+(xn--mumble) with their UTF-8 equivalent.
+.sp
+This feature is available in Postfix 2.12.
.PP
The usage and specification of template message text is
subject to the following restrictions:
.br
.IP "\fB forward \fR"
Local forwarding or aliasing. When
-a message is received with "SMTPUTF8 requested", then the forwarded
-(aliased) message automatically has "SMTPUTF8 requested".
+a message is received with "SMTPUTF8 required", then the forwarded
+(aliased) message always has "SMTPUTF8 required".
.br
.IP "\fB bounce \fR"
Submission by the \fBbounce\fR(8) daemon.
-When a message is received with "SMTPUTF8 requested", then the
-delivery status notification automatically has "SMTPUTF8 requested".
+When a message is received with "SMTPUTF8 required", then the
+delivery status notification always has "SMTPUTF8 required".
.br
.IP "\fB notify \fR"
Postmaster notification from the
# parameter, expressed in the time unit specified by
# \fIsuffix\fR. See above under \fBdelay_warning_time\fR for
# possible \fIsuffix\fR values.
+# .IP \fBmydomain\fR
+# Expands into the value of the \fBmydomain\fR parameter.
+# With "smtputf8_enable = yes", this replaces ACE labels
+# (xn--mumble) with their UTF-8 equivalent.
+# .sp
+# This feature is available in Postfix 2.12.
+# .IP \fBmyhostname\fR
+# Expands into the value of the \fBmyhostname\fR parameter.
+# With "smtputf8_enable = yes", this replaces ACE labels
+# (xn--mumble) with their UTF-8 equivalent.
+# .sp
+# This feature is available in Postfix 2.12.
# .PP
# The usage and specification of template message text is
# subject to the following restrictions:
daemon. </dd>
<dt> <b> forward </b> </dt> <dd> Local forwarding or aliasing. When
-a message is received with "SMTPUTF8 requested", then the forwarded
-(aliased) message automatically has "SMTPUTF8 requested". </dd>
+a message is received with "SMTPUTF8 required", then the forwarded
+(aliased) message always has "SMTPUTF8 required". </dd>
<dt> <b> bounce </b> </dt> <dd> Submission by the bounce(8) daemon.
-When a message is received with "SMTPUTF8 requested", then the
-delivery status notification automatically has "SMTPUTF8 requested".
-</dd>
+When a message is received with "SMTPUTF8 required", then the
+delivery status notification always has "SMTPUTF8 required". </dd>
<dt> <b> notify </b> </dt> <dd> Postmaster notification from the
smtp(8) or smtpd(8) daemon. </dd>
bounce_template.o: ../../include/mail_conf.h
bounce_template.o: ../../include/mail_params.h
bounce_template.o: ../../include/mail_proto.h
+bounce_template.o: ../../include/midna.h
bounce_template.o: ../../include/msg.h
bounce_template.o: ../../include/mymalloc.h
bounce_template.o: ../../include/split_at.h
/*
* Bundle up a bunch of parameters and initialize information that will
* be discovered on the fly.
+ *
+ * XXX Instead of overriding the returned-message MIME encoding, separate
+ * the returned-message MIME encoding from the (boiler plate, delivery
+ * status) MIME encoding.
*/
bounce_info = (BOUNCE_INFO *) mymalloc(sizeof(*bounce_info));
bounce_info->service = service;
bounce_info->queue_name = queue_name;
bounce_info->queue_id = queue_id;
bounce_info->smtputf8 = smtputf8;
- /* Fix 20140708: propagate smtputf8 attribute to bounce message. */
- bounce_info->smtputf8_attr =
- vstring_export(vstring_sprintf(vstring_alloc(20), "%s=%d",
- MAIL_ATTR_SMTPUTF8, smtputf8));
/* Fix 20140708: override MIME encoding: addresses may be 8bit. */
- if (bounce_info->smtputf8) {
+ /* Fix 20140718: override MIME encoding: 8bit $myhostname expansion. */
+ if (var_smtputf8_enable /* was: bounce_info->smtputf8 */ ) {
bounce_info->mime_encoding = "8bit";
} else if (strcmp(encoding, MAIL_ATTR_ENC_8BIT) == 0) {
bounce_info->mime_encoding = "8bit";
bounce_info->queue_id);
vstring_free(bounce_info->buf);
vstring_free(bounce_info->sender);
- myfree(bounce_info->smtputf8_attr);
myfree(bounce_info->mail_name);
myfree((char *) bounce_info->mime_boundary);
myfree((char *) bounce_info);
* headers and will make all addresses fully qualified.
*/
#define STREQ(a, b) (strcasecmp((a), (b)) == 0)
+#define STRNE(a, b) (strcasecmp((a), (b)) != 0)
/*
* Generic headers.
/*
* MIME header.
*/
+#define NOT_US_ASCII(tp) \
+ STRNE(bounce_template_charset(template), "us-ascii")
+
+#define NOT_7BIT_MIME(bp) \
+ (bp->mime_encoding && STRNE(bp->mime_encoding, MAIL_ATTR_ENC_7BIT))
+
post_mail_fputs(bounce, "");
post_mail_fprintf(bounce, "--%s", bounce_info->mime_boundary);
post_mail_fprintf(bounce, "Content-Description: %s", "Notification");
+ /* Fix 20140718: UTF-8 address or $myhostname expansion. */
post_mail_fprintf(bounce, "Content-Type: %s; charset=%s",
- "text/plain", bounce_template_charset(template));
+ "text/plain", NOT_US_ASCII(template) ?
+ bounce_template_charset(template) :
+ NOT_7BIT_MIME(bounce_info) ?
+ "utf-8" : "us-ascii");
/* Fix 20140709: addresses may be 8bit. */
- if (bounce_info->smtputf8)
+ if (NOT_7BIT_MIME(bounce_info))
post_mail_fprintf(bounce, "Content-Transfer-Encoding: %s",
- bounce_info->mime_encoding);
+ bounce_info->mime_encoding);
post_mail_fputs(bounce, "");
return (vstream_ferror(bounce));
(bounce_info->smtputf8 & SMTPUTF8_FLAG_REQUESTED) ?
"global-" : "");
/* Fix 20140709: addresses may be 8bit. */
- if (bounce_info->smtputf8)
+ if (NOT_7BIT_MIME(bounce_info))
post_mail_fprintf(bounce, "Content-Transfer-Encoding: %s",
bounce_info->mime_encoding);
}
post_mail_fprintf(bounce, "X-%s-Queue-ID: %s",
bounce_info->mail_name, bounce_info->queue_id);
+
+#define IS_UTF8_ADDRESS(str, len) \
+ ((str)[0] != 0 && !allascii(str) && valid_utf8_string((str), (len)))
+
/* Fix 20140708: use "utf-8" or "rfc822" as appropriate. */
if (VSTRING_LEN(bounce_info->sender) > 0)
post_mail_fprintf(bounce, "X-%s-Sender: %s; %s",
bounce_info->mail_name, bounce_info->smtputf8
- && STR(bounce_info->sender)[0]
- && !allascii(STR(bounce_info->sender))
- && valid_utf8_string(STR(bounce_info->sender),
+ && IS_UTF8_ADDRESS(STR(bounce_info->sender),
VSTRING_LEN(bounce_info->sender)) ?
"utf-8" : "rfc822", STR(bounce_info->sender));
if (bounce_info->arrival_time > 0)
post_mail_fputs(bounce, "");
/* Fix 20140708: Don't send "utf-8" type with non-UTF8 address. */
post_mail_fprintf(bounce, "Final-Recipient: %s; %s",
- bounce_info->smtputf8 && rcpt->address[0]
- && !allascii(rcpt->address)
- && valid_utf8_string(rcpt->address,
- strlen(rcpt->address)) ?
+ bounce_info->smtputf8
+ && IS_UTF8_ADDRESS(rcpt->address,
+ strlen(rcpt->address)) ?
"utf-8" : "rfc822", rcpt->address);
/*
} else if (NON_NULL_EMPTY(rcpt->orig_addr)) {
/* Fix 20140708: Don't send "utf-8" type with non-UTF8 address. */
post_mail_fprintf(bounce, "Original-Recipient: %s; %s",
- bounce_info->smtputf8 && rcpt->orig_addr[0]
- && !allascii(rcpt->orig_addr)
- && valid_utf8_string(rcpt->orig_addr,
- strlen(rcpt->orig_addr)) ?
+ bounce_info->smtputf8
+ && IS_UTF8_ADDRESS(rcpt->orig_addr,
+ strlen(rcpt->orig_addr)) ?
"utf-8" : "rfc822", rcpt->orig_addr);
}
post_mail_fprintf(bounce, "Action: %s",
post_mail_fprintf(bounce, "Content-Type: %s",
headers_only == DSN_RET_HDRS ?
"text/rfc822-headers" : "message/rfc822");
- if (bounce_info->mime_encoding)
+ if (NOT_7BIT_MIME(bounce_info))
post_mail_fprintf(bounce, "Content-Transfer-Encoding: %s",
bounce_info->mime_encoding);
post_mail_fputs(bounce, "");
BOUNCE_LOG *log_handle; /* open logfile */
char *mail_name; /* $mail_name, cooked */
int smtputf8; /* SMTPUTF8 requested */
- char *smtputf8_attr; /* pre-formatted record value */
} BOUNCE_INFO;
/* */
#include <split_at.h>
#include <stringops.h>
#include <mymalloc.h>
+#ifndef NO_EAI
+#include <midna.h>
+#endif
/* Global library. */
0, 0,
};
+ /*
+ * Parameters whose value may have to be converted to UTF-8 for presentation
+ * purposes.
+ */
+typedef struct {
+ const char *param_name; /* parameter name */
+ char **value; /* parameter value */
+} BOUNCE_STR_PARAMETER;
+
+static const BOUNCE_STR_PARAMETER str_parameter[] = {
+ VAR_MYHOSTNAME, &var_myhostname,
+ VAR_MYDOMAIN, &var_mydomain,
+ 0, 0,
+};
+
/*
* SLMs.
*/
BOUNCE_TEMPLATE *tp = (BOUNCE_TEMPLATE *) context;
const BOUNCE_TIME_PARAMETER *bp;
const BOUNCE_TIME_DIVISOR *bd;
+ const BOUNCE_STR_PARAMETER *sp;
static VSTRING *buf;
int result;
+ const char *asc_val;
+ const char *utf8_val;
/*
* Look for parameter names that can have a time unit suffix, and scale
key + bp->param_name_len + 1, key);
}
}
+
+ /*
+ * Look for parameter names that may have to be up-converted for
+ * presentation purposes.
+ */
+#ifndef NO_EAI
+ if (var_smtputf8_enable) {
+ for (sp = str_parameter; sp->param_name; sp++) {
+ if (strcmp(key, sp->param_name) == 0) {
+ asc_val = sp->value[0];
+ if (!allascii(asc_val)) {
+ msg_warn("%s: conversion \"%s\" failed: "
+ "non-ASCII input value: \"%s\"",
+ tp->origin, key, asc_val);
+ return (asc_val);
+ } else if ((utf8_val = midna_ascii_to_utf8(asc_val)) == 0) {
+ msg_warn("%s: conversion \"%s\" failed: "
+ "input value: \"%s\"",
+ tp->origin, key, asc_val);
+ return (asc_val);
+ } else {
+ return (utf8_val);
+ }
+ }
+ }
+ }
+#endif
return (mail_conf_lookup_eval(key));
}
VSTRING *buf = vstring_alloc(100);
const char **cpp;
int stat;
- const char *filter = "\t !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
if (tp->flags & BOUNCE_TMPL_FLAG_NEW_BUFFER)
bounce_template_parse_buffer(tp);
for (cpp = tp->message_text; *cpp; cpp++) {
- stat = mac_expand(buf, *cpp, MAC_EXP_FLAG_NONE, filter,
+ stat = mac_expand(buf, *cpp, MAC_EXP_FLAG_PRINTABLE, (char *) 0,
bounce_template_lookup, (char *) tp);
if (stat & MAC_PARSE_ERROR)
msg_fatal("%s: bad $name syntax in %s template: %s",
* Patches change both the patchlevel and the release date. Snapshots have no
* patchlevel; they change the release date only.
*/
-#define MAIL_RELEASE_DATE "20140716"
+#define MAIL_RELEASE_DATE "20140720"
#define MAIL_VERSION_NUMBER "2.12"
#ifdef SNAPSHOT
VSTRING *uxtext_unquote_append(VSTRING *unquoted, const char *quoted)
{
- const char *cp;
+ const unsigned char *cp;
int ch;
- for (cp = quoted; (ch = *cp) != 0; cp++) {
+ for (cp = (const unsigned char *) quoted; (ch = *cp) != 0; cp++) {
if (ch == '\\' && cp[1] == 'x' && cp[2] == '{') {
cp += 2;
int unicode = 0;
VSTRING *xtext_unquote_append(VSTRING *unquoted, const char *quoted)
{
- const char *cp;
+ const unsigned char *cp;
int ch;
- for (cp = quoted; (ch = *cp) != 0; cp++) {
+ for (cp = (const unsigned char *) quoted; (ch = *cp) != 0; cp++) {
if (ch == '+') {
if (ISDIGIT(cp[1]))
ch = (cp[1] - '0') << 4;
load_file.o: vstream.h
load_file.o: warn_stat.h
load_lib.o: load_lib.c
-load_lib.o: load_lib.h
-load_lib.o: msg.h
load_lib.o: sys_defs.h
lowercase.o: lowercase.c
lowercase.o: stringops.h
mac_expand.o: mac_parse.h
mac_expand.o: msg.h
mac_expand.o: mymalloc.h
+mac_expand.o: stringops.h
mac_expand.o: sys_defs.h
mac_expand.o: vbuf.h
mac_expand.o: vstring.h
midna.o: mymalloc.h
midna.o: stringops.h
midna.o: sys_defs.h
+midna.o: valid_hostname.h
midna.o: vbuf.h
midna.o: vstring.h
msg.o: msg.c
/* string, including macro names in the values of conditional
/* expressions. Do not expand macros, and do not write to the
/* result argument.
+/* .IP MAC_EXP_FLAG_PRINTABLE
+/* Use the printable() function instead of \fIfilter\fR.
/* .PP
/* The constant MAC_EXP_FLAG_NONE specifies a manifest null value.
/* .RE
#include <msg.h>
#include <vstring.h>
#include <mymalloc.h>
+#include <stringops.h>
#include <mac_parse.h>
#include <mac_expand.h>
} else {
len = VSTRING_LEN(mc->result);
vstring_strcat(mc->result, text);
- if (mc->filter) {
+ if (mc->flags & MAC_EXP_FLAG_PRINTABLE) {
+ printable(vstring_str(mc->result) + len, '_');
+ } else if (mc->filter) {
cp = vstring_str(mc->result) + len;
while (*(cp += strspn(cp, mc->filter)))
*cp++ = '_';
#define MAC_EXP_FLAG_RECURSE (1<<0)
#define MAC_EXP_FLAG_APPEND (1<<1)
#define MAC_EXP_FLAG_SCAN (1<<2)
+#define MAC_EXP_FLAG_PRINTABLE (1<<3)
/*
* Real lookup or just a test?
/*
/* const char *midna_utf8_to_ascii(
/* const char *name)
+/*
+/* const char *midna_ascii_to_utf8(
+/* const char *name)
/* DESCRIPTION
/* The functions in this module transform domain names from
/* or to IDNA form. The result is cached to avoid repeated
/* midna_utf8_to_ascii() converts an UTF-8 domain name to
/* ASCII. The result is a null pointer in case of error.
/*
+/* midna_ascii_to_utf8() converts an ASCII domain name to
+/* UTF-8. The result is a null pointer in case of error.
+/*
/* midna_cache_size specifies the size of the conversion result
/* cache. This value is used only once, upon the first lookup
/* request.
#include <msg.h>
#include <ctable.h>
#include <stringops.h>
+#include <valid_hostname.h>
#include <midna.h>
/*
}
}
-/* midna_utf8_to_ascii_free - cache element destructor */
+/* midna_ascii_to_utf8_create - convert ASCII domain to UTF8 */
+
+static void *midna_ascii_to_utf8_create(const char *name, void *unused_context)
+{
+ const char myname[] = "midna_ascii_to_utf8_create";
+ char buf[1024]; /* XXX */
+ UErrorCode error = U_ZERO_ERROR;
+ UIDNAInfo info = UIDNA_INFO_INITIALIZER;
+ UIDNA *idna;
+ int anl;
+
+ /*
+ * Paranoia: do not expose uidna_*() to unfiltered network data.
+ */
+ if (valid_hostname(name, DONT_GRIPE) == 0) {
+ msg_warn("%s: Problem translating domain \"%s\" to UTF8 form: %s",
+ myname, name, "malformed ASCII");
+ return (0);
+ }
+ idna = uidna_openUTS46(UIDNA_DEFAULT, &error);
+ anl = uidna_nameToUnicodeUTF8(idna,
+ name, strlen(name),
+ buf, sizeof(buf),
+ &info,
+ &error);
+ uidna_close(idna);
+ if (U_SUCCESS(error) && info.errors == 0 && anl > 0) {
+ return (mystrndup(buf, anl));
+ } else {
+ msg_warn("%s: Problem translating domain \"%s\" to IDNA form: %s",
+ myname, name, u_errorName(error));
+ return (0);
+ }
+}
+
+/* midna_cache_free - cache element destructor */
-static void midna_utf8_to_ascii_free(void *value, void *unused_context)
+static void midna_cache_free(void *value, void *unused_context)
{
if (value)
myfree(value);
if (midna_utf8_to_ascii_cache == 0)
midna_utf8_to_ascii_cache = ctable_create(midna_cache_size,
midna_utf8_to_ascii_create,
- midna_utf8_to_ascii_free,
+ midna_cache_free,
(void *) 0);
return (ctable_locate(midna_utf8_to_ascii_cache, name));
}
+/* midna_ascii_to_utf8 - convert UTF8 hostname to ASCII */
+
+const char *midna_ascii_to_utf8(const char *name)
+{
+ static CTABLE *midna_ascii_to_utf8_cache = 0;
+
+ if (midna_ascii_to_utf8_cache == 0)
+ midna_ascii_to_utf8_cache = ctable_create(midna_cache_size,
+ midna_ascii_to_utf8_create,
+ midna_cache_free,
+ (void *) 0);
+ return (ctable_locate(midna_ascii_to_utf8_cache, name));
+}
+
#ifdef TEST
/*
* stderr.
*/
#include <stdlib.h>
+#include <locale.h>
#include <stringops.h> /* XXX temp_utf8_kludge */
#include <vstring.h>
int main(int argc, char **argv)
{
VSTRING *buffer = vstring_alloc(1);
- const char *res;
+ const char *bp;
+ const char *ascii;
+ const char *utf8;
+
+ if (setlocale(LC_ALL, "C") == 0)
+ msg_fatal("setlocale(LC_ALL, C) failed: %m");
msg_vstream_init(argv[0], VSTREAM_ERR);
msg_verbose = 1;
temp_utf8_kludge = 1;
while (vstring_fgets_nonl(buffer, VSTREAM_IN)) {
- msg_info("testing: \"%s\"", vstring_str(buffer));
- res = midna_utf8_to_ascii(vstring_str(buffer));
- if (res != 0)
- msg_info("result: \"%s\"", res);
+ msg_info("testing: \"%s\"", bp = vstring_str(buffer));
+ if (!allascii(bp)) {
+ ascii = midna_utf8_to_ascii(bp);
+ if (ascii != 0) {
+ msg_info("\"%s\" -> \"%s\"", bp, ascii);
+ utf8 = midna_ascii_to_utf8(ascii);
+ if (utf8 != 0) {
+ msg_info("\"%s\" -> \"%s\" -> \"%s\"",
+ bp, ascii, utf8);
+ if (strcmp(utf8, bp) != 0)
+ msg_warn("\"%s\" != \"%s\"", bp, utf8);
+ }
+ }
+ } else {
+ utf8 = midna_ascii_to_utf8(bp);
+ if (utf8 != 0) {
+ msg_info("\"%s\" -> \"%s\"", bp, utf8);
+ ascii = midna_utf8_to_ascii(utf8);
+ if (ascii != 0) {
+ msg_info("\"%s\" -> \"%s\" -> \"%s\"",
+ bp, utf8, ascii);
+ if (strcmp(ascii, bp) != 0)
+ msg_warn("\"%s\" != \"%s\"", bp, ascii);
+ }
+ }
+ }
}
exit(0);
}
* External interface.
*/
extern const char *midna_utf8_to_ascii(const char *);
+extern const char *midna_ascii_to_utf8(const char *);
/* LICENSE
/* .ad