Add filter_test and move query parser in its own file.
[apps/pfixtools.git] / postlicyd / query.c
diff --git a/postlicyd/query.c b/postlicyd/query.c
new file mode 100644 (file)
index 0000000..1dc8e6f
--- /dev/null
@@ -0,0 +1,135 @@
+/******************************************************************************/
+/*          pfixtools: a collection of postfix related tools                  */
+/*          ~~~~~~~~~                                                         */
+/*  ________________________________________________________________________  */
+/*                                                                            */
+/*  Redistribution and use in source and binary forms, with or without        */
+/*  modification, are permitted provided that the following conditions        */
+/*  are met:                                                                  */
+/*                                                                            */
+/*  1. Redistributions of source code must retain the above copyright         */
+/*     notice, this list of conditions and the following disclaimer.          */
+/*  2. Redistributions in binary form must reproduce the above copyright      */
+/*     notice, this list of conditions and the following disclaimer in the    */
+/*     documentation and/or other materials provided with the distribution.   */
+/*  3. The names of its contributors may not be used to endorse or promote    */
+/*     products derived from this software without specific prior written     */
+/*     permission.                                                            */
+/*                                                                            */
+/*  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND   */
+/*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE     */
+/*  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR        */
+/*  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS    */
+/*  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR    */
+/*  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF      */
+/*  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS  */
+/*  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN   */
+/*  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)   */
+/*  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF    */
+/*  THE POSSIBILITY OF SUCH DAMAGE.                                           */
+/******************************************************************************/
+
+/*
+ * Copyright © 2007 Pierre Habouzit
+ * Copyright © 2008 Florent Bruneau
+ */
+
+#include "query.h"
+#include "policy_tokens.h"
+
+bool query_parse(query_t *query, char *p)
+{
+#define PARSE_CHECK(expr, error, ...)                                        \
+    do {                                                                     \
+        if (!(expr)) {                                                       \
+            syslog(LOG_ERR, error, ##__VA_ARGS__);                           \
+            return false;                                                    \
+        }                                                                    \
+    } while (0)
+
+    p_clear(query, 1);
+    query->state = SMTP_UNKNOWN;
+    while (*p != '\n') {
+        char *k, *v;
+        int klen, vlen, vtk;
+
+        while (isblank(*p))
+            p++;
+        p = strchr(k = p, '=');
+        PARSE_CHECK(p, "could not find '=' in line");
+        for (klen = p - k; klen && isblank(k[klen]); klen--);
+        p += 1; /* skip = */
+
+        while (isblank(*p))
+            p++;
+        p = strchr(v = p, '\n');
+        PARSE_CHECK(p, "could not find final \\n in line");
+        for (vlen = p - v; vlen && isblank(v[vlen]); vlen--);
+        p += 1; /* skip \n */
+
+        vtk = policy_tokenize(v, vlen);
+        switch (policy_tokenize(k, klen)) {
+#define CASE(up, low)  case PTK_##up: query->low = v; v[vlen] = '\0'; syslog(LOG_DEBUG, "%s = %s", ptokens[PTK_##up], 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
+
+          case PTK_REQUEST:
+            PARSE_CHECK(vtk == PTK_SMTPD_ACCESS_POLICY,
+                        "unexpected `request' value: %.*s", vlen, v);
+            break;
+
+          case PTK_PROTOCOL_NAME:
+            PARSE_CHECK(vtk == PTK_SMTP || vtk == PTK_ESMTP,
+                        "unexpected `protocol_name' value: %.*s", vlen, v);
+            query->esmtp = vtk == PTK_ESMTP;
+            break;
+
+          case PTK_PROTOCOL_STATE:
+            switch (vtk) {
+#define CASE(name)  case PTK_##name: query->state = SMTP_##name; break;
+                CASE(CONNECT);
+                CASE(EHLO);
+                CASE(HELO);
+                CASE(MAIL);
+                CASE(RCPT);
+                CASE(DATA);
+                CASE(END_OF_MESSAGE);
+                CASE(VRFY);
+                CASE(ETRN);
+              default:
+                PARSE_CHECK(false, "unexpected `protocol_state` value: %.*s",
+                            vlen, v);
+#undef CASE
+            }
+            break;
+
+          default:
+            syslog(LOG_WARNING, "unexpected key, skipped: %.*s", klen, k);
+            continue;
+        }
+    }
+
+    return query->state != SMTP_UNKNOWN;
+#undef PARSE_CHECK
+}
+