Postfix3.3.1
namadr_list.c
[詳解]
1 /*++
2 /* NAME
3 /* namadr_list 3
4 /* SUMMARY
5 /* name/address list membership
6 /* SYNOPSIS
7 /* #include <namadr_list.h>
8 /*
9 /* NAMADR_LIST *namadr_list_init(pname, flags, pattern_list)
10 /* const char *pname;
11 /* int flags;
12 /* const char *pattern_list;
13 /*
14 /* int namadr_list_match(list, name, addr)
15 /* NAMADR_LIST *list;
16 /* const char *name;
17 /* const char *addr;
18 /*
19 /* void namadr_list_free(list)
20 /* NAMADR_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
25 /* hostname or network address.
26 /*
27 /* A list pattern specifies a host name, a domain name,
28 /* an internet address, or a network/mask pattern, where the
29 /* mask specifies the number of bits in the network part.
30 /* When a pattern specifies a file name, its contents are
31 /* substituted for the file name; when a pattern is a
32 /* type:name table specification, table lookup is used
33 /* instead.
34 /* Patterns are separated by whitespace and/or commas. In
35 /* order to reverse the result, precede a pattern with an
36 /* exclamation point (!).
37 /*
38 /* A host matches a list when its name or address matches
39 /* a pattern, or when any of its parent domains matches a
40 /* pattern. The matching process is case insensitive.
41 /*
42 /* namadr_list_init() performs initializations. The pname
43 /* argument specifies error reporting context. The flags
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 last 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 <stdlib.h>
95 #include <unistd.h>
96 #include <msg.h>
97 #include <vstream.h>
98 #include <msg_vstream.h>
99 #include <dict.h>
100 #include <stringops.h> /* util_utf8_enable */
101 
102 static void usage(char *progname)
103 {
104  msg_fatal("usage: %s [-v] pattern_list hostname address", progname);
105 }
106 
107 int main(int argc, char **argv)
108 {
109  NAMADR_LIST *list;
110  char *host;
111  char *addr;
112  int ch;
113 
114  msg_vstream_init(argv[0], VSTREAM_ERR);
115 
116  while ((ch = GETOPT(argc, argv, "v")) > 0) {
117  switch (ch) {
118  case 'v':
119  msg_verbose++;
120  break;
121  default:
122  usage(argv[0]);
123  }
124  }
125  if (argc != optind + 3)
126  usage(argv[0]);
128  util_utf8_enable = 1;
129  list = namadr_list_init("command line", MATCH_FLAG_PARENT
130  | MATCH_FLAG_RETURN, argv[optind]);
131  host = argv[optind + 1];
132  addr = argv[optind + 2];
133  vstream_printf("%s/%s: %s\n", host, addr,
134  namadr_list_match(list, host, addr) ?
135  "YES" : list->error == 0 ? "NO" : "ERROR");
137  namadr_list_free(list);
138  return (0);
139 }
140 
141 #endif
int msg_verbose
Definition: msg.c:177
#define MATCH_FLAG_RETURN
Definition: match_list.h:40
#define VSTREAM_OUT
Definition: vstream.h:67
int main(int argc, char **argv)
Definition: anvil.c:1010
#define MATCH_FLAG_PARENT
Definition: match_list.h:39
int dict_allow_surrogate
VSTREAM * vstream_printf(const char *fmt,...)
Definition: vstream.c:1335
#define namadr_list_free
Definition: namadr_list.h:27
NORETURN msg_fatal(const char *fmt,...)
Definition: msg.c:249
int vstream_fflush(VSTREAM *stream)
Definition: vstream.c:1257
#define namadr_list_match
Definition: namadr_list.h:26
#define GETOPT(argc, argv, str)
Definition: sys_defs.h:1313
void msg_vstream_init(const char *name, VSTREAM *vp)
Definition: msg_vstream.c:77
#define NAMADR_LIST
Definition: namadr_list.h:22
int util_utf8_enable
Definition: printable.c:47
#define VSTREAM_ERR
Definition: vstream.h:68
#define namadr_list_init(o, f, p)
Definition: namadr_list.h:24