Move some code.
[apps/pfixtools.git] / postlicyd / match.c
index 2ec5e03..558bc88 100644 (file)
@@ -54,6 +54,15 @@ typedef struct match_condition_t {
 } match_condition_t;
 ARRAY(match_condition_t)
 
+static const char *condition_names[] = {
+  "unknown",
+  "equals to",
+  "differs from",
+  "contains",
+  "is contained",
+  "is empty"
+};
+
 #define CONDITION_INIT { PTK_UNKNOWN, false, MATCH_UNKNOWN, NULL, 0 }
 
 typedef struct match_config_t {
@@ -86,8 +95,8 @@ static bool match_filter_constructor(filter_t *filter)
 
 #define PARSE_CHECK(Expr, Str, ...)                                            \
     if (!(Expr)) {                                                             \
-        syslog(LOG_ERR, Str, ##__VA_ARGS__);                                   \
-        match_config_delete(&config);                                        \
+        err(Str, ##__VA_ARGS__);                                               \
+        match_config_delete(&config);                                          \
         return false;                                                          \
     }
 
@@ -124,7 +133,7 @@ static bool match_filter_constructor(filter_t *filter)
             condition.field = policy_tokenize(p, n - p);
             PARSE_CHECK(condition.field >= PTK_HELO_NAME
                         && condition.field < PTK_SMTPD_ACCESS_POLICY,
-                        "invalid field name %.*s", n - p, p);
+                        "invalid field name %.*s", (int)(n - p), p);
             p = skipspaces(n);
             n = p + 1;
             PARSE_CHECK(IS_OP_START(*p) && IS_OP_END(*n),
@@ -146,7 +155,7 @@ static bool match_filter_constructor(filter_t *filter)
             PARSE_CHECK(condition.condition != MATCH_UNKNOWN,
                         "invalid operator");
             if (condition.condition != MATCH_EMPTY) {
-                p = m_strnextsp(n + 1);
+                p = skipspaces(n + 1);
                 PARSE_CHECK(*p, "no value defined to check the condition");
                 condition.value_len = param->value_len - (p - param->value);
                 condition.value     = p_dupstr(p, condition.value_len);
@@ -173,34 +182,11 @@ static void match_filter_destructor(filter_t *filter)
 
 static inline bool match_condition(const match_condition_t *cond, const query_t *query)
 {
-    const char *field = NULL;
-    switch (cond->field) {
-#define CASE(Up, Low)                                                          \
-      case PTK_ ## Up: field = query->Low; break;
-      CASE(HELO_NAME, helo_name)
-      CASE(QUEUE_ID, queue_id)
-      CASE(SENDER, sender)
-      CASE(RECIPIENT, recipient)
-      CASE(RECIPIENT_COUNT, recipient_count)
-      CASE(CLIENT_ADDRESS, client_address)
-      CASE(CLIENT_NAME, client_name)
-      CASE(REVERSE_CLIENT_NAME, reverse_client_name)
-      CASE(INSTANCE, instance)
-      CASE(SASL_METHOD, sasl_method)
-      CASE(SASL_USERNAME, sasl_username)
-      CASE(SASL_SENDER, sasl_sender)
-      CASE(SIZE, size)
-      CASE(CCERT_SUBJECT, ccert_subject)
-      CASE(CCERT_ISSUER, ccert_issuer)
-      CASE(CCERT_FINGERPRINT, ccert_fingerprint)
-      CASE(ENCRYPTION_PROTOCOL, encryption_protocol)
-      CASE(ENCRYPTION_CIPHER, encryption_cipher)
-      CASE(ENCRYPTION_KEYSIZE, encryption_keysize)
-      CASE(ETRN_DOMAIN, etrn_domain)
-      CASE(STRESS, stress)
-#undef CASE
-      default: return false;
-    }
+    const char *field = query_field_for_id(query, cond->field);
+    debug("running condition: \"%s\" %s %s\"%s\"",
+          field, condition_names[cond->condition],
+          cond->case_sensitive ? "" : "(alternative) ",
+          cond->value ? cond->value : "(none)");
     switch (cond->condition) {
       case MATCH_EQUAL:
       case MATCH_DIFFER:
@@ -247,20 +233,25 @@ static inline bool match_condition(const match_condition_t *cond, const query_t
     return true;
 }
 
-static filter_result_t match_filter(const filter_t *filter, const query_t *query)
+static filter_result_t match_filter(const filter_t *filter, const query_t *query,
+                                    filter_context_t *context)
 {
     const match_config_t *config = filter->data;
     foreach (const match_condition_t *condition, config->conditions) {
         bool r = match_condition(condition, query);
         if (!r && config->match_all) {
+            debug("condition failed, match_all failed");
             return HTK_FAIL;
         } else if (r && !(config->match_all)) {
+            debug("condition succeed, not-match_all succeed");
             return HTK_MATCH;
         }
     }}
     if (config->match_all) {
+        debug("all conditions matched, match_all succeed");
         return HTK_MATCH;
     } else {
+        debug("no condition matched, not-match_all failed");
         return HTK_FAIL;
     }
 }
@@ -268,7 +259,8 @@ static filter_result_t match_filter(const filter_t *filter, const query_t *query
 static int match_init(void)
 {
     filter_type_t type =  filter_register("match", match_filter_constructor,
-                                          match_filter_destructor, match_filter);
+                                          match_filter_destructor, match_filter,
+                                          NULL, NULL);
     /* Hooks.
      */
     (void)filter_hook_register(type, "abort");