xref: /netbsd-src/external/ibm-public/postfix/dist/src/global/addr_match_list.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1 /*	$NetBSD: addr_match_list.c,v 1.2 2017/02/14 01:16:45 christos Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	addr_match_list 3
6 /* SUMMARY
7 /*	address list membership
8 /* SYNOPSIS
9 /*	#include <addr_match_list.h>
10 /*
11 /*	ADDR_MATCH_LIST *addr_match_list_init(pname, flags, pattern_list)
12 /*	const char *pname;
13 /*	int	flags;
14 /*	const char *pattern_list;
15 /*
16 /*	int	addr_match_list_match(list, addr)
17 /*	ADDR_MATCH_LIST *list;
18 /*	const char *addr;
19 /*
20 /*	void	addr_match_list_free(list)
21 /*	ADDR_MATCH_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 /*	network address.
27 /*
28 /*	A list pattern specifies an internet address, or a network/mask
29 /*	pattern, where the mask specifies the number of bits in the
30 /*	network part.  When a pattern specifies a file name, its
31 /*	contents are substituted for the file name; when a pattern
32 /*	is a type:name table specification, table lookup is used
33 /*	instead.  Patterns are separated by whitespace and/or commas.
34 /*	In order to reverse the result, precede a pattern with an
35 /*	exclamation point (!).
36 /*
37 /*	A host matches a list when its address matches a pattern.
38 /*	The matching process is case insensitive.
39 /*
40 /*	addr_match_list_init() performs initializations. The pname
41 /*	argument specifies error reporting context. The flags
42 /*	argument is the bit-wise OR of zero or more of the following:
43 /* .IP MATCH_FLAG_RETURN
44 /*	Request that addr_match_list_match() logs a warning and
45 /*	returns zero with list->error set to a non-zero dictionary
46 /*	error code, instead of raising a fatal error.
47 /* .PP
48 /*	Specify MATCH_FLAG_NONE to request none of the above.
49 /*	The last argument is a list of patterns, or the absolute
50 /*	pathname of a file with patterns.
51 /*
52 /*	addr_match_list_match() matches the specified host address
53 /*	against the specified list of patterns.
54 /*
55 /*	addr_match_list_free() releases storage allocated by
56 /*	addr_match_list_init().
57 /* DIAGNOSTICS
58 /*	Fatal errors: unable to open or read a pattern file; invalid
59 /*	pattern. Panic: interface violations.
60 /* SEE ALSO
61 /*	match_list(3) generic list matching
62 /*	match_ops(3) match host 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 "addr_match_list.h"
85 
86 #ifdef TEST
87 
88 #include <stdlib.h>
89 #include <unistd.h>
90 #include <string.h>
91 #include <msg.h>
92 #include <vstream.h>
93 #include <vstring_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] pattern_list address", progname);
101 }
102 
main(int argc,char ** argv)103 int     main(int argc, char **argv)
104 {
105     ADDR_MATCH_LIST *list;
106     char   *addr;
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 = addr_match_list_init("command line", MATCH_FLAG_PARENT
125 				| MATCH_FLAG_RETURN, argv[optind]);
126     addr = argv[optind + 1];
127     if (strcmp(addr, "-") == 0) {
128 	VSTRING *buf = vstring_alloc(100);
129 
130 	while (vstring_get_nonl(buf, VSTREAM_IN) != VSTREAM_EOF)
131 	    vstream_printf("%s: %s\n", vstring_str(buf),
132 			   addr_match_list_match(list, vstring_str(buf)) ?
133 			   "YES" : list->error == 0 ? "NO" : "ERROR");
134 	vstring_free(buf);
135     } else {
136 	vstream_printf("%s: %s\n", addr,
137 		       addr_match_list_match(list, addr) > 0 ?
138 		       "YES" : list->error == 0 ? "NO" : "ERROR");
139     }
140     vstream_fflush(VSTREAM_OUT);
141     addr_match_list_free(list);
142     return (0);
143 }
144 
145 #endif
146