Postfix3.3.1
dns_rr_filter.c
[詳解]
1 /*++
2 /* NAME
3 /* dns_rr_filter 3
4 /* SUMMARY
5 /* DNS resource record filter
6 /* SYNOPSIS
7 /* #include <dns.h>
8 /*
9 /* void dns_rr_filter_compile(title, map_names)
10 /* const char *title;
11 /* const char *map_names;
12 /* INTERNAL INTERFACES
13 /* int dns_rr_filter_execute(rrlist)
14 /* DNS_RR **rrlist;
15 /*
16 /* MAPS *dns_rr_filter_maps;
17 /* DESCRIPTION
18 /* This module implements a simple filter for dns_lookup*()
19 /* results.
20 /*
21 /* dns_rr_filter_compile() initializes a result filter. The
22 /* title and map_names arguments are as with maps_create().
23 /* This function may be invoked more than once; only the last
24 /* filter takes effect.
25 /*
26 /* dns_rr_filter_execute() converts each resource record in the
27 /* specified list with dns_strrecord to ASCII form and matches
28 /* that against the specified maps. If a match is found it
29 /* executes the corresponding action. Currently, only the
30 /* "ignore" action is implemented. This removes the matched
31 /* record from the list. The result is 0 in case of success,
32 /* -1 in case of error.
33 /*
34 /* dns_rr_filter_maps is updated by dns_rr_filter_compile().
35 /* LICENSE
36 /* .ad
37 /* .fi
38 /* The Secure Mailer license must be distributed with this software.
39 /* AUTHOR(S)
40 /* Wietse Venema
41 /* IBM T.J. Watson Research
42 /* P.O. Box 704
43 /* Yorktown Heights, NY 10598, USA
44 /*--*/
45 
46  /*
47  * System library.
48  */
49 #include <sys_defs.h>
50 #include <ctype.h>
51 
52 #ifdef STRCASECMP_IN_STRINGS_H
53 #include <strings.h>
54 #endif
55 
56  /*
57  * Utility library.
58  */
59 #include <msg.h>
60 #include <vstring.h>
61 #include <myaddrinfo.h>
62 
63  /*
64  * Global library.
65  */
66 #include <maps.h>
67 
68  /*
69  * DNS library.
70  */
71 #define LIBDNS_INTERNAL
72 #include <dns.h>
73 
74  /*
75  * Application-specific.
76  */
78 
79 static DNS_RR dns_rr_filter_error[1];
80 
81 #define STR vstring_str
82 
83 /* dns_rr_filter_compile - compile dns result filter */
84 
85 void dns_rr_filter_compile(const char *title, const char *map_names)
86 {
87  if (dns_rr_filter_maps != 0)
88  maps_free(dns_rr_filter_maps);
89  dns_rr_filter_maps = maps_create(title, map_names,
91 }
92 
93 /* dns_rr_action - execute action from filter map */
94 
95 static DNS_RR *dns_rr_action(const char *cmd, DNS_RR *rr, const char *rr_text)
96 {
97  const char *cmd_args = cmd + strcspn(cmd, " \t");
98  int cmd_len = cmd_args - cmd;
99 
100  while (*cmd_args && ISSPACE(*cmd_args))
101  cmd_args++;
102 
103 #define STREQUAL(x,y,l) (strncasecmp((x), (y), (l)) == 0 && (y)[l] == 0)
104 
105  if (STREQUAL(cmd, "IGNORE", cmd_len)) {
106  msg_info("ignoring DNS RR: %s", rr_text);
107  return (0);
108  } else {
109  msg_warn("%s: unknown DNS filter action: \"%s\"",
110  dns_rr_filter_maps->title, cmd);
111  return (dns_rr_filter_error);
112  }
113  return (rr);
114 }
115 
116 /* dns_rr_filter_execute - filter DNS lookup result */
117 
119 {
120  static VSTRING *buf = 0;
121  DNS_RR **rrp;
122  DNS_RR *rr;
123  const char *map_res;
124  DNS_RR *act_res;
125 
126  /*
127  * Convert the resource record to string form, then search the maps for a
128  * matching action.
129  */
130  if (buf == 0)
131  buf = vstring_alloc(100);
132  for (rrp = rrlist; (rr = *rrp) != 0; /* see below */ ) {
133  map_res = maps_find(dns_rr_filter_maps, dns_strrecord(buf, rr),
135  if (map_res != 0) {
136  if ((act_res = dns_rr_action(map_res, rr, STR(buf))) == 0) {
137  *rrp = rr->next; /* do not advance in the list */
138  rr->next = 0;
139  dns_rr_free(rr);
140  continue;
141  } else if (act_res == dns_rr_filter_error) {
142  return (-1);
143  }
144  } else if (dns_rr_filter_maps->error) {
145  return (-1);
146  }
147  rrp = &(rr->next); /* do advance in the list */
148  }
149  return (0);
150 }
#define STREQUAL(x, y, l)
Definition: maps.h:22
#define DICT_FLAG_FOLD_FIX
Definition: dict.h:124
MAPS * dns_rr_filter_maps
Definition: dns_rr_filter.c:77
MAPS * maps_create(const char *title, const char *map_names, int dict_flags)
Definition: maps.c:112
void dns_rr_filter_compile(const char *title, const char *map_names)
Definition: dns_rr_filter.c:85
char * title
Definition: maps.h:23
#define DICT_FLAG_LOCK
Definition: dict.h:116
void msg_warn(const char *fmt,...)
Definition: msg.c:215
VSTRING * vstring_alloc(ssize_t len)
Definition: vstring.c:353
struct DNS_RR * next
Definition: dns.h:147
MAPS * maps_free(MAPS *maps)
Definition: maps.c:213
int error
Definition: maps.h:25
int dns_rr_filter_execute(DNS_RR **rrlist)
#define ISSPACE(c)
Definition: sys_defs.h:1753
char * dns_strrecord(VSTRING *, DNS_RR *)
Definition: dns_strrecord.c:50
#define STR
Definition: dns_rr_filter.c:81
#define DICT_FLAG_NONE
Definition: dict.h:109
const char * maps_find(MAPS *maps, const char *name, int flags)
Definition: maps.c:162
void dns_rr_free(DNS_RR *)
Definition: dns_rr.c:137
Definition: dns.h:139
void msg_info(const char *fmt,...)
Definition: msg.c:199