Postfix3.3.1
addr_match_list.c
[詳解]
1 /*++
2 /* NAME
3 /* addr_match_list 3
4 /* SUMMARY
5 /* address list membership
6 /* SYNOPSIS
7 /* #include <addr_match_list.h>
8 /*
9 /* ADDR_MATCH_LIST *addr_match_list_init(pname, flags, pattern_list)
10 /* const char *pname;
11 /* int flags;
12 /* const char *pattern_list;
13 /*
14 /* int addr_match_list_match(list, addr)
15 /* ADDR_MATCH_LIST *list;
16 /* const char *addr;
17 /*
18 /* void addr_match_list_free(list)
19 /* ADDR_MATCH_LIST *list;
20 /* DESCRIPTION
21 /* This is a convenience wrapper around the match_list module.
22 /*
23 /* This module implements tests for list membership of a
24 /* network address.
25 /*
26 /* A list pattern specifies an internet address, or a network/mask
27 /* pattern, where the mask specifies the number of bits in the
28 /* network part. When a pattern specifies a file name, its
29 /* contents are substituted for the file name; when a pattern
30 /* is a type:name table specification, table lookup is used
31 /* instead. Patterns are separated by whitespace and/or commas.
32 /* In order to reverse the result, precede a pattern with an
33 /* exclamation point (!).
34 /*
35 /* A host matches a list when its address matches a pattern.
36 /* The matching process is case insensitive.
37 /*
38 /* addr_match_list_init() performs initializations. The pname
39 /* argument specifies error reporting context. The flags
40 /* argument is the bit-wise OR of zero or more of the following:
41 /* .IP MATCH_FLAG_RETURN
42 /* Request that addr_match_list_match() logs a warning and
43 /* returns zero with list->error set to a non-zero dictionary
44 /* error code, instead of raising a fatal error.
45 /* .PP
46 /* Specify MATCH_FLAG_NONE to request none of the above.
47 /* The last argument is a list of patterns, or the absolute
48 /* pathname of a file with patterns.
49 /*
50 /* addr_match_list_match() matches the specified host address
51 /* against the specified list of patterns.
52 /*
53 /* addr_match_list_free() releases storage allocated by
54 /* addr_match_list_init().
55 /* DIAGNOSTICS
56 /* Fatal errors: unable to open or read a pattern file; invalid
57 /* pattern. Panic: interface violations.
58 /* SEE ALSO
59 /* match_list(3) generic list matching
60 /* match_ops(3) match host 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 "addr_match_list.h"
83 
84 #ifdef TEST
85 
86 #include <stdlib.h>
87 #include <unistd.h>
88 #include <string.h>
89 #include <msg.h>
90 #include <vstream.h>
91 #include <vstring_vstream.h>
92 #include <msg_vstream.h>
93 #include <dict.h>
94 #include <stringops.h> /* util_utf8_enable */
95 
96 static void usage(char *progname)
97 {
98  msg_fatal("usage: %s [-v] pattern_list address", progname);
99 }
100 
101 int main(int argc, char **argv)
102 {
103  ADDR_MATCH_LIST *list;
104  char *addr;
105  int ch;
106 
107  msg_vstream_init(argv[0], VSTREAM_ERR);
108 
109  while ((ch = GETOPT(argc, argv, "v")) > 0) {
110  switch (ch) {
111  case 'v':
112  msg_verbose++;
113  break;
114  default:
115  usage(argv[0]);
116  }
117  }
118  if (argc != optind + 2)
119  usage(argv[0]);
121  util_utf8_enable = 1;
122  list = addr_match_list_init("command line", MATCH_FLAG_PARENT
123  | MATCH_FLAG_RETURN, argv[optind]);
124  addr = argv[optind + 1];
125  if (strcmp(addr, "-") == 0) {
126  VSTRING *buf = vstring_alloc(100);
127 
128  while (vstring_get_nonl(buf, VSTREAM_IN) != VSTREAM_EOF)
129  vstream_printf("%s: %s\n", vstring_str(buf),
130  addr_match_list_match(list, vstring_str(buf)) ?
131  "YES" : list->error == 0 ? "NO" : "ERROR");
132  vstring_free(buf);
133  } else {
134  vstream_printf("%s: %s\n", addr,
135  addr_match_list_match(list, addr) > 0 ?
136  "YES" : list->error == 0 ? "NO" : "ERROR");
137  }
139  addr_match_list_free(list);
140  return (0);
141 }
142 
143 #endif
int msg_verbose
Definition: msg.c:177
#define VSTREAM_EOF
Definition: vstream.h:110
#define MATCH_FLAG_RETURN
Definition: match_list.h:40
#define addr_match_list_match(l, a)
int vstring_get_nonl(VSTRING *vp, VSTREAM *fp)
#define vstring_str(vp)
Definition: vstring.h:71
#define VSTREAM_OUT
Definition: vstream.h:67
int main(int argc, char **argv)
Definition: anvil.c:1010
#define VSTREAM_IN
Definition: vstream.h:66
#define ADDR_MATCH_LIST
#define MATCH_FLAG_PARENT
Definition: match_list.h:39
int dict_allow_surrogate
VSTREAM * vstream_printf(const char *fmt,...)
Definition: vstream.c:1335
#define addr_match_list_free
VSTRING * vstring_alloc(ssize_t len)
Definition: vstring.c:353
NORETURN msg_fatal(const char *fmt,...)
Definition: msg.c:249
int vstream_fflush(VSTREAM *stream)
Definition: vstream.c:1257
#define GETOPT(argc, argv, str)
Definition: sys_defs.h:1313
VSTRING * vstring_free(VSTRING *vp)
Definition: vstring.c:380
void msg_vstream_init(const char *name, VSTREAM *vp)
Definition: msg_vstream.c:77
#define addr_match_list_init(o, f, p)
int util_utf8_enable
Definition: printable.c:47
#define VSTREAM_ERR
Definition: vstream.h:68