From 9b993811fe1fd208afd954e86c0bfc81a78af04f Mon Sep 17 00:00:00 2001 From: Florent Bruneau Date: Fri, 12 Sep 2008 15:16:05 +0200 Subject: [PATCH] Prepare filter and config design. Signed-off-by: Florent Bruneau --- postlicyd/Makefile | 2 +- postlicyd/config.c | 70 +++++++++++++++++++++++++ postlicyd/config.h | 46 +++++++++++++++++ postlicyd/filter.c | 103 +++++++++++++++++++++++++++++++++++++ postlicyd/filter.h | 98 +++++++++++++++++++++++++++++++++++ postlicyd/main-postlicyd.c | 62 +--------------------- postlicyd/query.h | 103 +++++++++++++++++++++++++++++++++++++ 7 files changed, 422 insertions(+), 62 deletions(-) create mode 100644 postlicyd/config.c create mode 100644 postlicyd/config.h create mode 100644 postlicyd/filter.c create mode 100644 postlicyd/filter.h create mode 100644 postlicyd/query.h diff --git a/postlicyd/Makefile b/postlicyd/Makefile index 7331063..a16ba30 100644 --- a/postlicyd/Makefile +++ b/postlicyd/Makefile @@ -35,7 +35,7 @@ PROGRAMS = postlicyd GENERATED = policy_tokens.h policy_tokens.c filter_tokens.h filter_tokens.c TESTS = test-rbl -postlicyd_SOURCES = greylist.c rbl.c main-postlicyd.c $(GENERATED) ../common/lib.a +postlicyd_SOURCES = greylist.c rbl.c main-postlicyd.c filter.c config.c $(GENERATED) ../common/lib.a postlicyd_LIBADD = $(TC_LIBS) tst-rbl_SOURCES = tst-rbl.c diff --git a/postlicyd/config.c b/postlicyd/config.c new file mode 100644 index 0000000..dc952cc --- /dev/null +++ b/postlicyd/config.c @@ -0,0 +1,70 @@ +/******************************************************************************/ +/* 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 © 2008 Florent Bruneau + */ + +#include "filter.h" +#include "config.h" + +struct config_t { + filter_t *filters; + int filters_len; + int filters_size; + + int entry_point; +}; + +static inline config_t *config_new(void) +{ + config_t *config = p_new(config_t, 1); + config->entry_point = -1; + return config; +} + +void config_delete(config_t **config) +{ + if (*config) { + for (int i = 0 ; i < (*config)->filters_len ; ++i) { + filter_wipe((*config)->filters + i); + } + p_delete(&(*config)->filters); + } +} + +config_t *config_read(const char *file) +{ + config_t *config; + + config = config_new(); + return config; +} diff --git a/postlicyd/config.h b/postlicyd/config.h new file mode 100644 index 0000000..f823bce --- /dev/null +++ b/postlicyd/config.h @@ -0,0 +1,46 @@ +/******************************************************************************/ +/* 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 © 2008 Florent Bruneau + */ + +#ifndef PFIXTOOLS_CONFIG_H +#define PFIXTOOLS_CONFIG_H + +typedef struct config_t config_t; + +__attribute__((nonnull(1))) +config_t *config_read(const char *file); + +void config_delete(config_t **config); + +#endif diff --git a/postlicyd/filter.c b/postlicyd/filter.c new file mode 100644 index 0000000..a98d29d --- /dev/null +++ b/postlicyd/filter.c @@ -0,0 +1,103 @@ +/******************************************************************************/ +/* 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 © 2008 Florent Bruneau + */ + +#include "str.h" +#include "buffer.h" +#include "filter.h" + +static filter_runner_t runners[FTK_count]; +static filter_constructor_t constructors[FTK_count]; +static filter_destructor_t destructors[FTK_count]; + +void filter_register(const char *type, filter_constructor_t constructor, + filter_destructor_t destructor, filter_runner_t runner) +{ + filter_token tok = filter_tokenize(type, m_strlen(type)); + assert(tok != FTK_UNKNOWN && "Unknown filter type"); + runners[tok] = runner; + constructors[tok] = constructor; + destructors[tok] = destructor; +} + +bool filter_build(filter_t *filter) +{ + filter_constructor_t constructor = constructors[filter->type]; + if (constructor) { + return constructor(filter); + } + return true; +} + +void filter_wipe(filter_t *filter) +{ + filter_destructor_t destructor = destructors[filter->type]; + if (destructor) { + destructor(filter); + } + p_delete(&filter->hooks); + p_delete(&filter->params); + p_delete(&filter->name); +} + +filter_result_t filter_run(const filter_t *filter, const query_t *query) +{ + return runners[filter->type](filter, query); +} + +bool filter_set_name(filter_t *filter, const char *name, ssize_t len) +{ + filter->name = p_new(char, len + 1); + memcpy(filter->name, name, len); + filter->name[len] = '\0'; + return true; +} + +bool filter_set_type(filter_t *filter, const char *type, ssize_t len) +{ + filter->type = filter_tokenize(type, len); + return filter->type != FTK_UNKNOWN; +} + +bool filter_add_param(filter_t *filter, const char *name, ssize_t name_len, + const char *value, ssize_t value_len) +{ + return true; +} + +bool filter_add_hook(filter_t *filter, const char *name, ssize_t name_len, + const char *value, ssize_t value_len) +{ + return true; +} diff --git a/postlicyd/filter.h b/postlicyd/filter.h new file mode 100644 index 0000000..f532d1a --- /dev/null +++ b/postlicyd/filter.h @@ -0,0 +1,98 @@ +/******************************************************************************/ +/* 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 © 2008 Florent Bruneau + */ + +#ifndef PFIXTOOLS_FILTER_H +#define PFIXTOOLS_FILTER_H + +#include "common.h" +#include "filter_tokens.h" +#include "query.h" + +typedef filter_token filter_type_t; + +typedef struct filter_hook_t { + const char *name; + const char *value; +} filter_hook_t; + +typedef struct filter_params_t { + const char *name; + const char *value; +} filter_params_t; + +typedef struct filter_t { + char *name; + filter_type_t type; + + filter_hook_t *hooks; + filter_params_t *params; + void *data; +} filter_t; + +typedef const char *filter_result_t; +typedef filter_result_t (*filter_runner_t)(const filter_t *filter, + const query_t *query); +typedef bool (*filter_constructor_t)(filter_t *filter); +typedef void (*filter_destructor_t)(filter_t *filter); + +__attribute__((nonnull(1,4))) +void filter_register(const char *type, filter_constructor_t constructor, + filter_destructor_t destructor, filter_runner_t runner); + +__attribute__((nonnull(1,2))) +bool filter_set_name(filter_t *filter, const char *name, ssize_t len); + +__attribute__((nonnull(1,2))) +bool filter_set_type(filter_t *filter, const char *type, ssize_t len); + +__attribute__((nonnull(1,2,4))) +bool filter_add_param(filter_t *filter, const char *name, ssize_t name_len, + const char *value, ssize_t value_len); + +__attribute__((nonnull(1,2,4))) +bool filter_add_hook(filter_t *filter, const char *name, ssize_t name_len, + const char *value, ssize_t value_len); + +__attribute__((nonnull(1))) +bool filter_build(filter_t *filter); + +__attribute__((nonnull(1))) +void filter_wipe(filter_t *filter); + +__attribute__((nonnull(1,2))) +filter_result_t filter_run(const filter_t *filter, const query_t *query); + + +#endif diff --git a/postlicyd/main-postlicyd.c b/postlicyd/main-postlicyd.c index 950e581..7d6970d 100644 --- a/postlicyd/main-postlicyd.c +++ b/postlicyd/main-postlicyd.c @@ -41,6 +41,7 @@ #include "epoll.h" #include "policy_tokens.h" #include "server.h" +#include "query.h" #define DAEMON_NAME "postlicyd" #define DEFAULT_PORT 10000 @@ -49,67 +50,6 @@ DECLARE_MAIN -enum smtp_state { - SMTP_UNKNOWN, - SMTP_CONNECT, - SMTP_EHLO, - SMTP_HELO = SMTP_EHLO, - SMTP_MAIL, - SMTP_RCPT, - SMTP_DATA, - SMTP_END_OF_MESSAGE, - SMTP_VRFY, - SMTP_ETRN, -}; - -/* \see http://www.postfix.org/SMTPD_POLICY_README.html */ -typedef struct query_t { - unsigned state : 4; - unsigned esmtp : 1; - - const char *helo_name; - const char *queue_id; - const char *sender; - const char *recipient; - const char *recipient_count; - const char *client_address; - const char *client_name; - const char *reverse_client_name; - const char *instance; - - /* postfix 2.2+ */ - const char *sasl_method; - const char *sasl_username; - const char *sasl_sender; - const char *size; - const char *ccert_subject; - const char *ccert_issuer; - const char *ccert_fingerprint; - - /* postfix 2.3+ */ - const char *encryption_protocol; - const char *encryption_cipher; - const char *encryption_keysize; - const char *etrn_domain; - - /* postfix 2.5+ */ - const char *stress; - - const char *eoq; -} query_t; - -static query_t *query_new(void) -{ - return p_new(query_t, 1); -} - -static void query_delete(query_t **query) -{ - if (*query) { - p_delete(query); - } -} - static void *query_starter(server_t* server) { return query_new(); diff --git a/postlicyd/query.h b/postlicyd/query.h new file mode 100644 index 0000000..b11991c --- /dev/null +++ b/postlicyd/query.h @@ -0,0 +1,103 @@ +/******************************************************************************/ +/* 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 + */ + +#ifndef PFIXTOOLS_QUERY_H +#define PFIXTOOLS_QUERY_H + +#include "mem.h" + +enum smtp_state { + SMTP_UNKNOWN, + SMTP_CONNECT, + SMTP_EHLO, + SMTP_HELO = SMTP_EHLO, + SMTP_MAIL, + SMTP_RCPT, + SMTP_DATA, + SMTP_END_OF_MESSAGE, + SMTP_VRFY, + SMTP_ETRN, +}; + +/* \see http://www.postfix.org/SMTPD_POLICY_README.html */ +typedef struct query_t { + unsigned state : 4; + unsigned esmtp : 1; + + const char *helo_name; + const char *queue_id; + const char *sender; + const char *recipient; + const char *recipient_count; + const char *client_address; + const char *client_name; + const char *reverse_client_name; + const char *instance; + + /* postfix 2.2+ */ + const char *sasl_method; + const char *sasl_username; + const char *sasl_sender; + const char *size; + const char *ccert_subject; + const char *ccert_issuer; + const char *ccert_fingerprint; + + /* postfix 2.3+ */ + const char *encryption_protocol; + const char *encryption_cipher; + const char *encryption_keysize; + const char *etrn_domain; + + /* postfix 2.5+ */ + const char *stress; + + const char *eoq; +} query_t; + +static inline query_t *query_new(void) +{ + return p_new(query_t, 1); +} + +static inline void query_delete(query_t **query) +{ + if (*query) { + p_delete(query); + } +} + +#endif -- 2.20.1