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