1 /* $NetBSD: domain_list.c,v 1.2 2017/02/14 01:16:45 christos Exp $ */
2
3 /*++
4 /* NAME
5 /* domain_list 3
6 /* SUMMARY
7 /* match a host or domain name against a pattern list
8 /* SYNOPSIS
9 /* #include <domain_list.h>
10 /*
11 /* DOMAIN_LIST *domain_list_init(pname, flags, pattern_list)
12 /* const char *pname;
13 /* int flags;
14 /* const char *pattern_list;
15 /*
16 /* int domain_list_match(list, name)
17 /* DOMAIN_LIST *list;
18 /* const char *name;
19 /*
20 /* void domain_list_free(list)
21 /* DOMAIN_LIST *list;
22 /* DESCRIPTION
23 /* This is a convenience wrapper around the match_list module.
24 /*
25 /* This module implements tests for list membership of a host or
26 /* domain name.
27 /*
28 /* Patterns are separated by whitespace and/or commas. A pattern
29 /* is either a string, a file name (in which case the contents
30 /* of the file are substituted for the file name) or a type:name
31 /* lookup table specification.
32 /*
33 /* A host name matches a domain list when its name appears in the
34 /* list of domain patterns, or when any of its parent domains appears
35 /* in the list of domain patterns. The matching process is case
36 /* insensitive. In order to reverse the result, precede a
37 /* pattern with an exclamation point (!).
38 /*
39 /* domain_list_init() performs initializations. The pname
40 /* argument specifies error reporting context. The flags argument
41 /* is the bit-wise OR of zero or more of the following:
42 /* .IP MATCH_FLAG_PARENT
43 /* The hostname pattern foo.com matches itself and any name below
44 /* the domain foo.com. If this flag is cleared, foo.com matches itself
45 /* only, and .foo.com matches any name below the domain foo.com.
46 /* .IP MATCH_FLAG_RETURN
47 /* Request that domain_list_match() logs a warning and returns
48 /* zero, with list->error set to a non-zero dictionary error
49 /* code, instead of raising a fatal error.
50 /* .PP
51 /* Specify MATCH_FLAG_NONE to request none of the above.
52 /* The last argument is a list of domain patterns, or the name of
53 /* a file containing domain patterns.
54 /*
55 /* domain_list_match() matches the specified host or domain name
56 /* against the specified pattern list.
57 /*
58 /* domain_list_free() releases storage allocated by domain_list_init().
59 /* DIAGNOSTICS
60 /* Fatal error: unable to open or read a domain_list file; invalid
61 /* domain_list pattern.
62 /* SEE ALSO
63 /* match_list(3) generic list matching
64 /* match_ops(3) match hosts by name or by address
65 /* LICENSE
66 /* .ad
67 /* .fi
68 /* The Secure Mailer license must be distributed with this software.
69 /* AUTHOR(S)
70 /* Wietse Venema
71 /* IBM T.J. Watson Research
72 /* P.O. Box 704
73 /* Yorktown Heights, NY 10598, USA
74 /*--*/
75
76 /* System library. */
77
78 #include <sys_defs.h>
79
80 /* Utility library. */
81
82 #include <match_list.h>
83
84 /* Global library. */
85
86 #include "domain_list.h"
87
88 #ifdef TEST
89
90 #include <stdlib.h>
91 #include <unistd.h>
92 #include <msg.h>
93 #include <vstream.h>
94 #include <msg_vstream.h>
95 #include <dict.h>
96 #include <stringops.h> /* util_utf8_enable */
97
usage(char * progname)98 static void usage(char *progname)
99 {
100 msg_fatal("usage: %s [-v] patterns hostname", progname);
101 }
102
main(int argc,char ** argv)103 int main(int argc, char **argv)
104 {
105 DOMAIN_LIST *list;
106 char *host;
107 int ch;
108
109 msg_vstream_init(argv[0], VSTREAM_ERR);
110
111 while ((ch = GETOPT(argc, argv, "v")) > 0) {
112 switch (ch) {
113 case 'v':
114 msg_verbose++;
115 break;
116 default:
117 usage(argv[0]);
118 }
119 }
120 if (argc != optind + 2)
121 usage(argv[0]);
122 dict_allow_surrogate = 1;
123 util_utf8_enable = 1;
124 list = domain_list_init("command line", MATCH_FLAG_PARENT
125 | MATCH_FLAG_RETURN, argv[optind]);
126 host = argv[optind + 1];
127 vstream_printf("%s: %s\n", host, domain_list_match(list, host) ?
128 "YES" : list->error == 0 ? "NO" : "ERROR");
129 vstream_fflush(VSTREAM_OUT);
130 domain_list_free(list);
131 return (0);
132 }
133
134 #endif
135