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