Postfix3.3.1
valid_utf8_hostname.c
[詳解]
1 /*++
2 /* NAME
3 /* valid_utf8_hostname 3
4 /* SUMMARY
5 /* validate (maybe UTF-8) domain name
6 /* SYNOPSIS
7 /* #include <valid_utf8_hostname.h>
8 /*
9 /* int valid_utf8_hostname(
10 /* int enable_utf8,
11 /* const char *domain,
12 /* int gripe)
13 /* DESCRIPTION
14 /* valid_utf8_hostname() is a wrapper around valid_hostname().
15 /* If EAI support is compiled in, and enable_utf8 is true, the
16 /* name is converted from UTF-8 to ASCII per IDNA rules, before
17 /* invoking valid_hostname().
18 /* SEE ALSO
19 /* valid_hostname(3) STD3 hostname validation.
20 /* DIAGNOSTICS
21 /* Fatal errors: memory allocation problem.
22 /* Warnings: malformed domain name.
23 /* LICENSE
24 /* .ad
25 /* .fi
26 /* The Secure Mailer license must be distributed with this software.
27 /* AUTHOR(S)
28 /* Wietse Venema
29 /* IBM T.J. Watson Research
30 /* P.O. Box 704
31 /* Yorktown Heights, NY 10598, USA
32 /*--*/
33 
34  /*
35  * System library.
36  */
37 #include <sys_defs.h>
38 
39  /*
40  * Utility library.
41  */
42 #include <msg.h>
43 #include <mymalloc.h>
44 #include <stringops.h>
45 #include <valid_hostname.h>
46 #include <midna_domain.h>
47 #include <valid_utf8_hostname.h>
48 
49 /* valid_utf8_hostname - validate internationalized domain name */
50 
51 int valid_utf8_hostname(int enable_utf8, const char *name, int gripe)
52 {
53  static const char myname[] = "valid_utf8_hostname";
54 
55  /*
56  * Trivial cases first.
57  */
58  if (*name == 0) {
59  if (gripe)
60  msg_warn("%s: empty domain name", myname);
61  return (0);
62  }
63 
64  /*
65  * Convert non-ASCII domain name to ASCII and validate the result per
66  * STD3. midna_domain_to_ascii() applies valid_hostname() to the result.
67  * Propagate the gripe parameter for better diagnostics (note that
68  * midna_domain_to_ascii() logs a problem only when the result is not
69  * cached).
70  */
71 #ifndef NO_EAI
72  if (enable_utf8 && !allascii(name)) {
73  if (midna_domain_to_ascii(name) == 0) {
74  if (gripe)
75  msg_warn("%s: malformed UTF-8 domain name", myname);
76  return (0);
77  } else {
78  return (1);
79  }
80  }
81 #endif
82 
83  /*
84  * Validate ASCII name per STD3.
85  */
86  return (valid_hostname(name, gripe));
87 }
int valid_hostname(const char *name, int gripe)
int valid_utf8_hostname(int enable_utf8, const char *name, int gripe)
void msg_warn(const char *fmt,...)
Definition: msg.c:215
#define allascii(s)
Definition: stringops.h:66
const char * midna_domain_to_ascii(const char *name)
Definition: midna_domain.c:261