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