1 /******************************************************************************/
2 /* pfixtools: a collection of postfix related tools */
4 /* ________________________________________________________________________ */
6 /* Redistribution and use in source and binary forms, with or without */
7 /* modification, are permitted provided that the following conditions */
10 /* 1. Redistributions of source code must retain the above copyright */
11 /* notice, this list of conditions and the following disclaimer. */
12 /* 2. Redistributions in binary form must reproduce the above copyright */
13 /* notice, this list of conditions and the following disclaimer in the */
14 /* documentation and/or other materials provided with the distribution. */
15 /* 3. The names of its contributors may not be used to endorse or promote */
16 /* products derived from this software without specific prior written */
19 /* THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS */
20 /* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY */
23 /* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL */
24 /* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS */
25 /* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) */
26 /* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, */
27 /* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN */
28 /* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
29 /* POSSIBILITY OF SUCH DAMAGE. */
31 /* Copyright (c) 2006-2008 the Authors */
32 /* see AUTHORS and source files for details */
33 /******************************************************************************/
36 * Copyright © 2008 Florent Bruneau
41 #include "policy_tokens.h"
43 typedef struct match_condition_t {
44 postlicyd_token field;
57 ARRAY(match_condition_t)
59 static const char *condition_names[] = {
68 #define CONDITION_INIT { PTK_UNKNOWN, false, MATCH_UNKNOWN, { NULL, 0 } }
70 typedef struct match_config_t {
71 A(match_condition_t) conditions;
75 static match_config_t *match_config_new(void)
77 return p_new(match_config_t, 1);
80 static inline void match_condition_wipe(match_condition_t *condition)
82 char *str = (char*)condition->value.str;
84 condition->value.str = NULL;
85 condition->value.len = 0;
88 static void match_config_delete(match_config_t **config)
91 array_deep_wipe((*config)->conditions, match_condition_wipe);
96 static bool match_filter_constructor(filter_t *filter)
98 match_config_t *config = match_config_new();
100 #define PARSE_CHECK(Expr, Str, ...) \
102 err(Str, ##__VA_ARGS__); \
103 match_config_delete(&config); \
107 foreach (filter_param_t *param, filter->params) {
108 switch (param->type) {
109 FILTER_PARAM_PARSE_BOOLEAN(MATCH_ALL, config->match_all);
111 /* condition parameter is:
112 * field_name OPERATOR value.
113 * valid operators are:
114 * == field_name is strictly equal to
115 * =i field_name is case insensitively equal to
116 * != field_name is not equal to
117 * !i field_name is not case insensitively equal to
118 * >= field_name contains
119 * >i field_name contains case insensitively
120 * <= field_name is contained
121 * <i field_name is contained case insensitively
122 * #= field_name is empty or not set
123 * #i field_name is not empty
125 case ATK_CONDITION: {
126 #define IS_OP_START(N) \
127 ((N) == '=' || (N) == '!' || (N) == '>' || (N) == '<' || (N) == '#')
128 #define IS_OP_END(N) \
129 ((N) == '=' || (N) == 'i')
130 match_condition_t condition = CONDITION_INIT;
131 const char *p = skipspaces(param->value);
132 const char *n = p + 1;
133 PARSE_CHECK(isalnum(*p), "invalid field name");
134 for (n = p + 1 ; *n && (isalnum(*n) || *n == '_') ; ++n);
135 PARSE_CHECK(*n && (isspace(*n) || IS_OP_START(*n)),
136 "invalid condition, expected operator after field name");
137 condition.field = policy_tokenize(p, n - p);
138 PARSE_CHECK(condition.field >= PTK_HELO_NAME
139 && condition.field < PTK_SMTPD_ACCESS_POLICY,
140 "invalid field name %.*s", (int)(n - p), p);
143 PARSE_CHECK(IS_OP_START(*p) && IS_OP_END(*n),
144 "invalid operator %2s", p);
146 #define CASE_OP(C, Value) \
148 condition.condition = MATCH_ ## Value; \
149 PARSE_CHECK(*n == '=' || *n == 'i', "invalid operator modifier %c", *n);\
150 condition.case_sensitive = !!(*n == '='); \
153 CASE_OP('!', DIFFER);
154 CASE_OP('>', CONTAINS);
155 CASE_OP('<', CONTAINED);
159 PARSE_CHECK(condition.condition != MATCH_UNKNOWN,
161 if (condition.condition != MATCH_EMPTY) {
162 p = skipspaces(n + 1);
163 PARSE_CHECK(*p, "no value defined to check the condition");
164 condition.value.len = param->value_len - (p - param->value);
165 condition.value.str = p_dupstr(p, condition.value.len);
167 array_add(config->conditions, condition);
174 PARSE_CHECK(config->conditions.len > 0,
175 "no condition defined");
176 filter->data = config;
180 static void match_filter_destructor(filter_t *filter)
182 match_config_t *config = filter->data;
183 match_config_delete(&config);
184 filter->data = config;
187 static inline bool match_condition(const match_condition_t *cond, const query_t *query)
189 const static_str_t *field = query_field_for_id(query, cond->field);
190 debug("running condition: \"%s\" %s %s\"%s\"",
191 field->str, condition_names[cond->condition],
192 cond->case_sensitive ? "" : "(alternative) ",
193 cond->value.str ? cond->value.str : "(none)");
194 switch (cond->condition) {
197 if (field == NULL || field->str == NULL) {
198 return cond->condition != MATCH_DIFFER;
200 if (cond->case_sensitive) {
201 return !!((strcmp(field->str, cond->value.str) == 0)
202 ^ (cond->condition == MATCH_DIFFER));
204 return !!((ascii_strcasecmp(field->str, cond->value.str) == 0)
205 ^ (cond->condition == MATCH_DIFFER));
210 if (field == NULL || field->str == NULL) {
213 if (cond->case_sensitive) {
214 return strstr(field->str, cond->value.str);
216 return m_stristrn(field->str, cond->value.str, cond->value.len);
220 case MATCH_CONTAINED:
221 if (field == NULL || field->str == NULL) {
224 if (cond->case_sensitive) {
225 return strstr(cond->value.str, field->str);
227 return m_stristr(cond->value.str, field->str);
232 return !!((field == NULL || field->len == 0) ^ (!cond->case_sensitive));
235 assert(false && "invalid condition type");
240 static filter_result_t match_filter(const filter_t *filter, const query_t *query,
241 filter_context_t *context)
243 const match_config_t *config = filter->data;
244 foreach (const match_condition_t *condition, config->conditions) {
245 bool r = match_condition(condition, query);
246 if (!r && config->match_all) {
247 debug("condition failed, match_all failed");
249 } else if (r && !(config->match_all)) {
250 debug("condition succeed, not-match_all succeed");
254 if (config->match_all) {
255 debug("all conditions matched, match_all succeed");
258 debug("no condition matched, not-match_all failed");
263 static int match_init(void)
265 filter_type_t type = filter_register("match", match_filter_constructor,
266 match_filter_destructor, match_filter,
270 (void)filter_hook_register(type, "abort");
271 (void)filter_hook_register(type, "error");
272 (void)filter_hook_register(type, "match");
273 (void)filter_hook_register(type, "fail");
277 (void)filter_param_register(type, "match_all");
278 (void)filter_param_register(type, "condition");
281 module_init(match_init);