3
/* valid_mailhost_addr 3
5
/* mailhost address syntax validation
7
/* #include <valid_mailhost_addr.h>
9
/* const char *valid_mailhost_addr(name, gripe)
13
/* int valid_mailhost_literal(addr, gripe)
17
/* valid_mailhost_addr() requires that the input is a valid
18
/* RFC 2821 string representation of an IPv4 or IPv6 network
19
/* address. A valid IPv4 address is in dotted quad decimal
20
/* form. A valid IPv6 address includes the "IPV6:" prefix as
21
/* required by RFC 2821, and is in valid hexadecimal form or
22
/* in valid IPv4-in-IPv6 form. The result value is the bare
23
/* address in the input argument (i.e. text after "IPV6:"
24
/* prefix, if any) in case of success, a null pointer in case
27
/* valid_mailhost_literal() requires an address enclosed in
28
/* []. The result is non-zero in case of success, zero in
31
/* These routines operate silently unless the gripe parameter
32
/* specifies a non-zero value. The macros DO_GRIPE and DONT_GRIPE
33
/* provide suitable constants.
35
/* The IPV6_COL macro defines the "IPv6:" prefix.
37
/* Warnings are logged with msg_warn().
40
/* RFC 952, RFC 1123, RFC 1035, RFC 2821
44
/* The Secure Mailer license must be distributed with this software.
47
/* IBM T.J. Watson Research
49
/* Yorktown Heights, NY 10598, USA
58
#ifdef STRCASECMP_IN_STRINGS_H
62
/* Utility library. */
65
#include <myaddrinfo.h>
69
#include <valid_mailhost_addr.h>
71
/* Application-specific. */
73
#define IPV6_COL_LEN (sizeof(IPV6_COL) - 1)
74
#define HAS_IPV6_COL(str) (strncasecmp((str), IPV6_COL, IPV6_COL_LEN) == 0)
75
#define SKIP_IPV6_COL(str) (HAS_IPV6_COL(str) ? (str) + IPV6_COL_LEN : (str))
77
/* valid_mailhost_addr - validate RFC 2821 numerical address form */
79
const char *valid_mailhost_addr(const char *addr, int gripe)
81
const char *bare_addr;
83
bare_addr = SKIP_IPV6_COL(addr);
84
return ((bare_addr != addr ? valid_ipv6_hostaddr : valid_ipv4_hostaddr)
85
(bare_addr, gripe) ? bare_addr : 0);
88
/* valid_mailhost_literal - validate [RFC 2821 numerical address] form */
90
int valid_mailhost_literal(const char *addr, int gripe)
92
const char *myname = "valid_mailhost_literal";
93
MAI_HOSTADDR_STR hostaddr;
99
msg_warn("%s: '[' expected at start: %.100s", myname, addr);
102
if ((last = strchr(addr, ']')) == 0) {
104
msg_warn("%s: ']' expected at end: %.100s", myname, addr);
109
msg_warn("%s: unexpected text after ']': %.100s", myname, addr);
112
if ((address_bytes = last - addr - 1) >= sizeof(hostaddr.buf)) {
114
msg_warn("%s: too much text: %.100s", myname, addr);
117
strncpy(hostaddr.buf, addr + 1, address_bytes);
118
hostaddr.buf[address_bytes] = 0;
119
return (valid_mailhost_addr(hostaddr.buf, gripe) != 0);
125
* Test program - reads hostnames from stdin, reports invalid hostnames to
132
#include <vstring_vstream.h>
133
#include <msg_vstream.h>
135
int main(int unused_argc, char **argv)
137
VSTRING *buffer = vstring_alloc(1);
139
msg_vstream_init(argv[0], VSTREAM_ERR);
142
while (vstring_fgets_nonl(buffer, VSTREAM_IN)) {
143
msg_info("testing: \"%s\"", vstring_str(buffer));
144
if (vstring_str(buffer)[0] == '[')
145
valid_mailhost_literal(vstring_str(buffer), DO_GRIPE);
147
valid_mailhost_addr(vstring_str(buffer), DO_GRIPE);