#include "buffer.h"
#include "common.h"
+#include "epoll.h"
#include "threads.h"
#include "tokens.h"
#define DAEMON_NAME "postlicyd"
+#define DEFAULT_PORT 10000
+#define RUNAS_USER "nobody"
+#define RUNAS_GROUP "nogroup"
enum smtp_state {
SMTP_UNKNOWN,
const char *recipient_count;
const char *client_address;
const char *client_name;
- const char *rclient_name;
+ const char *reverse_client_name;
const char *instance;
/* postfix 2.2+ */
const char *encryption_cipher;
const char *encryption_keysize;
const char *etrn_domain;
-
- buffer_t data;
} query_t;
-static query_t *query_init(query_t *rq) {
- memset(rq, 0, offsetof(query_t, data));
- buffer_init(&rq->data);
- return rq;
+typedef struct plicyd_t {
+ unsigned listener : 1;
+ unsigned watchwr : 1;
+ int fd;
+ buffer_t ibuf;
+ buffer_t obuf;
+} plicyd_t;
+
+
+static plicyd_t *plicyd_new(void)
+{
+ plicyd_t *plicyd = p_new(plicyd_t, 1);
+ plicyd->fd = -1;
+ return plicyd;
}
-static void query_wipe(query_t *rq) {
- buffer_wipe(&rq->data);
+
+#if 0
+static void plicyd_delete(plicyd_t **plicyd)
+{
+ if (*plicyd) {
+ if ((*plicyd)->fd >= 0)
+ close((*plicyd)->fd);
+ buffer_wipe(&(*plicyd)->ibuf);
+ buffer_wipe(&(*plicyd)->obuf);
+ p_delete(plicyd);
+ }
}
+#endif
-static int postfix_parsejob(query_t *query)
+static int postfix_parsejob(query_t *query, char *p)
{
#define PARSE_CHECK(expr, error, ...) \
do { \
} \
} while (0)
- char *p = vskipspaces(query->data.data);
-
- memset(query, 0, offsetof(query_t, data));
- while (p[0] != '\r' || p[1] != '\n') {
+ p_clear(query, 1);
+ while (*p != '\n') {
char *k, *v;
int klen, vlen, vtk;
while (isblank(*p))
p++;
- p = strstr(v = p, "\r\n");
- PARSE_CHECK(p, "could not find final \\r\\n in line");
+ 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 += 2; /* skip \r\n */
+ p += 1; /* skip \n */
vtk = tokenize(v, vlen);
switch (tokenize(k, klen)) {
CASE(RECIPIENT_COUNT, recipient_count);
CASE(CLIENT_ADDRESS, client_address);
CASE(CLIENT_NAME, client_name);
- CASE(RCLIENT_NAME, rclient_name);
+ CASE(REVERSE_CLIENT_NAME, reverse_client_name);
CASE(INSTANCE, instance);
CASE(SASL_METHOD, sasl_method);
CASE(SASL_USERNAME, sasl_username);
static void *policy_run(int fd, void *data)
{
- query_t q;
- query_init(&q);
+ buffer_t buf;
+ buffer_init(&buf);
for (;;) {
- int nb = buffer_read(&q.data, fd, -1);
+ ssize_t search_offs = MAX(0, buf.len - 1);
+ int nb = buffer_read(&buf, fd, -1);
const char *eoq;
+ query_t q;
if (nb < 0) {
if (errno == EAGAIN || errno == EINTR)
break;
}
if (nb == 0) {
- if (q.data.len)
+ if (buf.len)
syslog(LOG_ERR, "unexpected end of data");
break;
}
- eoq = strstr(q.data.data + MAX(0, q.data.len - 3), "\r\n\r\n");
+ eoq = strstr(buf.data + search_offs, "\n\n");
if (!eoq)
continue;
- if (postfix_parsejob(&q) < 0)
+ if (postfix_parsejob(&q, buf.data) < 0)
break;
- buffer_consume(&q.data, eoq + strlen("\r\n\r\n") - q.data.data);
- if (xwrite(fd, "DUNNO\r\n", strlen("DUNNO\r\n"))) {
+ buffer_consume(&buf, eoq + strlen("\n\n") - buf.data);
+ if (xwrite(fd, "DUNNO\n\n", strlen("DUNNO\n\n"))) {
UNIXERR("write");
break;
}
}
+ buffer_wipe(&buf);
- query_wipe(&q);
close(fd);
return NULL;
}
-static int main_loop(void)
+int start_listener(int port)
{
- int exitcode = EXIT_SUCCESS;
- int sock = -1;
-
- while (!sigint) {
- int fd = accept(sock, NULL, 0);
- if (fd < 0) {
- if (errno != EINTR || errno != EAGAIN)
- UNIXERR("accept");
- continue;
- }
-
- thread_launch(policy_run, fd, NULL);
- threads_join();
+ struct sockaddr_in addr = {
+ .sin_family = AF_INET,
+ .sin_addr = { htonl(INADDR_LOOPBACK) },
+ };
+ struct epoll_event evt = { .events = EPOLLIN };
+ plicyd_t *tmp;
+ int sock;
+
+ addr.sin_port = htons(port);
+ sock = tcp_listen_nonblock((const struct sockaddr *)&addr, sizeof(addr));
+ if (sock < 0) {
+ return -1;
}
- close(sock);
- return exitcode;
+ evt.data.ptr = tmp = plicyd_new();
+ tmp->fd = sock;
+ tmp->listener = true;
+ if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &evt) < 0) {
+ UNIXERR("epoll_ctl");
+ return -1;
+ }
+ return 0;
}
-
/* administrivia {{{ */
static int main_initialize(void)
signal(SIGPIPE, SIG_IGN);
signal(SIGINT, &common_sighandler);
signal(SIGTERM, &common_sighandler);
+ signal(SIGHUP, &common_sighandler);
signal(SIGSEGV, &common_sighandler);
syslog(LOG_INFO, "Starting...");
return 0;
fputs("usage: "DAEMON_NAME" [options] config\n"
"\n"
"Options:\n"
+ " -l <port> port to listen to\n"
" -p <pidfile> file to write our pid to\n"
+ " -f stay in foreground\n"
, stderr);
}
int main(int argc, char *argv[])
{
const char *pidfile = NULL;
- int res;
+ bool daemonize = true;
+ int port = DEFAULT_PORT;
- for (int c = 0; (c = getopt(argc, argv, "h" "p:")) >= 0; ) {
+ for (int c = 0; (c = getopt(argc, argv, "hf" "l:p:")) >= 0; ) {
switch (c) {
case 'p':
pidfile = optarg;
break;
+ case 'l':
+ port = atoi(optarg);
+ break;
+ case 'f':
+ daemonize = false;
+ break;
default:
usage();
return EXIT_FAILURE;
return EXIT_FAILURE;
}
- if (daemon_detach() < 0) {
+ if (drop_privileges(RUNAS_USER, RUNAS_GROUP) < 0) {
+ syslog(LOG_CRIT, "unable to drop privileges");
+ return EXIT_FAILURE;
+ }
+
+ if (daemonize && daemon_detach() < 0) {
syslog(LOG_CRIT, "unable to fork");
return EXIT_FAILURE;
}
pidfile_refresh();
- res = main_loop();
+
+ if (start_listener(port) < 0)
+ return EXIT_FAILURE;
+
+ while (!sigint) {
+ struct epoll_event evts[1024];
+ int n;
+
+ n = epoll_wait(epollfd, evts, countof(evts), -1);
+ if (n < 0) {
+ if (errno != EAGAIN && errno != EINTR) {
+ UNIXERR("epoll_wait");
+ return EXIT_FAILURE;
+ }
+ continue;
+ }
+
+ while (--n >= 0) {
+ plicyd_t *d = evts[n].data.ptr;
+
+ if (d->listener) {
+ int fd = accept(d->fd, NULL, 0);
+ if (fd < 0) {
+ if (errno != EINTR && errno != EAGAIN) {
+ UNIXERR("accept");
+ return EXIT_FAILURE;
+ }
+ continue;
+ }
+ thread_launch(policy_run, fd, NULL);
+ }
+ }
+ threads_join();
+ }
+
syslog(LOG_INFO, "Stopping...");
- return res;
+ return EXIT_SUCCESS;
}