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