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