Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Diff to update table_sqlite(5) #54

Open
wants to merge 43 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
3136fbb
skeleton for smtpfd
poolpOrg May 15, 2017
2820310
Let the default conf location be /etc/mail.
snimmagadda May 20, 2017
b290b1a
s/newd_conf/smtpfd_conf.
snimmagadda May 20, 2017
e2c574d
s/newd_process/smtpfd_process.
snimmagadda May 20, 2017
29158c8
Replace remaining 'newd' with 'smtpfd'.
snimmagadda May 20, 2017
ffb8bf4
Fix mandoc lint warnings.
snimmagadda May 20, 2017
715fb90
unlink(2) requires cpath in pledge.
snimmagadda May 20, 2017
50e5fe7
remove sample implementation for process info
ericfaurot May 23, 2017
b12ed3b
introduce a helper layer for imsg process management.
ericfaurot May 24, 2017
2eae71f
convert engine.c to use the procimsg abstraction
ericfaurot May 24, 2017
ae02db5
use imsgproc abstraction for main process.
ericfaurot May 24, 2017
410d861
use imsgproc abstraction for control clients
ericfaurot May 26, 2017
602e5ab
revert change that was not supposed to be commited
ericfaurot May 26, 2017
26d2baf
remove unused imsgev-related code
ericfaurot May 29, 2017
bbb466b
replace config parser and get rid of config handling in the
ericfaurot May 29, 2017
3aa74a7
simplify engine and frontend code:
ericfaurot May 29, 2017
1121fc2
set procname earlier and get rid of the log_procnames indirection
ericfaurot May 29, 2017
112f687
declare p_* as external globals in smtpfd.h and put them in smtpfd.c
ericfaurot May 29, 2017
e2c3f59
use proc_compose() directly everywhere
ericfaurot May 29, 2017
07834ee
make ctl_conn and control_state internal to contol.c
ericfaurot May 29, 2017
e5ad1bd
move mostly empty engine.h and frontend.h to smtpfd.h
ericfaurot May 29, 2017
f5ff470
implement the -D option and tweak manpage
ericfaurot May 30, 2017
a64a73f
mark static funciotns as static
ericfaurot May 30, 2017
96e5d22
tweak includes
ericfaurot May 30, 2017
e95fb1e
use fatalx() instead of errx()
ericfaurot May 30, 2017
d6d8f1e
get rid of ctl_conn structure and use imsgproc directly
ericfaurot May 30, 2017
3e3f654
- use static where appropriate
ericfaurot May 30, 2017
b1e1128
tweak the condig parser a bit.
ericfaurot May 30, 2017
2381969
pass the filter processes and flattened chains to the engine
ericfaurot May 30, 2017
6823d6c
remove unused showinfo code
ericfaurot May 31, 2017
1d8035d
missing semicolon
ericfaurot May 31, 2017
5808452
display __func__ in log message where appropriate
ericfaurot Jul 10, 2017
7c41923
tweak proc.c
ericfaurot Jul 15, 2017
8970ebb
main -> priv
ericfaurot Jul 15, 2017
4ca7e6a
move forward
ericfaurot Jul 18, 2017
d4aaaf1
more work on smtpfd
ericfaurot Jul 19, 2017
8d3417b
tweaks
ericfaurot Jul 22, 2017
85c98eb
consider that a smtpf session has two ends A and B, and simply relay
ericfaurot Jul 27, 2017
003f0c9
missing else
ericfaurot Jul 27, 2017
90dd9fa
add an example of smtpf handling a command directly, instead of forwa…
ericfaurot Jul 27, 2017
368dbd8
Merge branch 'master' into smtpfd
ericfaurot Apr 24, 2018
778dbaa
Add traces and a simple filtering PoC.
ericfaurot Jul 9, 2018
71b604c
Merge branch 'master' into smtpfd
ericfaurot Jul 10, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions extras/filters/smtpfd/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# $OpenBSD$

PROG= smtpfd

SRCS+= control.c
SRCS+= engine.c
SRCS+= frontend.c
SRCS+= frontend_smtpf.c
SRCS+= io.c
SRCS+= iobuf.c
SRCS+= log.c
SRCS+= logmsg.c
SRCS+= parse.y
SRCS+= proc.c
SRCS+= resolver.c
SRCS+= smtpfd.c

MAN= smtpfd.8 smtpfd.conf.5

CFLAGS+= -Wall -I${.CURDIR}
CFLAGS+= -Wstrict-prototypes -Wmissing-prototypes
CFLAGS+= -Wmissing-declarations
CFLAGS+= -Wshadow -Wpointer-arith -Wcast-qual
CFLAGS+= -Wsign-compare
YFLAGS=
LDADD+= -levent -lutil
DPADD+= ${LIBEVENT} ${LIBUTIL}

.include <bsd.prog.mk>
250 changes: 250 additions & 0 deletions extras/filters/smtpfd/control.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
/* $OpenBSD$ */

/*
* Copyright (c) 2017 Eric Faurot <eric@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include <sys/types.h>
#include <sys/queue.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>

#include <errno.h>
#include <event.h>
#include <imsg.h>
#include <pwd.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "smtpfd.h"

#include "log.h"
#include "proc.h"

#define CONTROL_BACKLOG 5

static void control_init(const char *);
static void control_listen(void);
static void control_pause(void);
static void control_resume(void);
static void control_accept(int, short, void *);
static void control_close(struct imsgproc *);
static void control_dispatch_priv(struct imsgproc *, struct imsg *, void *);
static void control_dispatch_client(struct imsgproc *, struct imsg *, void *);

static struct {
struct event evt;
int fd;
int pause;
} ctl;

void
control(int debug, int verbose)
{
struct passwd *pw;

/* Early initialisation. */
log_init(debug, LOG_DAEMON);
log_setverbose(verbose);
log_procinit("control");
setproctitle("control");

control_init(SMTPFD_SOCKET);

/* Drop priviledges. */
if ((pw = getpwnam(SMTPFD_USER)) == NULL)
fatalx("unknown user " SMTPFD_USER);

if (setgroups(1, &pw->pw_gid) ||
setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
fatal("cannot drop privileges");

if (chroot(pw->pw_dir) == 1)
fatal("%s: chroot", __func__);

if (pledge("stdio unix recvfd sendfd", NULL) == -1)
fatal("%s: pledge", __func__);

event_init();

signal(SIGPIPE, SIG_IGN);

/* Setup imsg socket with parent. */
p_priv = proc_attach(PROC_PRIV, 3);
if (p_priv == NULL)
fatal("%s: proc_attach", __func__);
proc_setcallback(p_priv, control_dispatch_priv, NULL);
proc_enable(p_priv);

event_dispatch();

exit(0);
}

static void
control_init(const char *path)
{
struct sockaddr_un sun;
mode_t old_umask;
int fd;

fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
if (fd == -1)
fatal("%s: socket", __func__);

memset(&sun, 0, sizeof(sun));
sun.sun_family = AF_UNIX;
strlcpy(sun.sun_path, SMTPFD_SOCKET, sizeof(sun.sun_path));

if ((unlink(path) == -1) && (errno != ENOENT))
fatal("%s: unlink: %s", __func__, path);

old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1)
fatal("%s: bind: %s", __func__, path);
umask(old_umask);

if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1)
fatal("%s: chmod: %s", __func__, path);

ctl.fd = fd;
}

static void
control_listen(void)
{
if (listen(ctl.fd, CONTROL_BACKLOG) == -1)
fatal("%s: listen", __func__);

ctl.pause = 0;
control_resume();
}

static void
control_pause(void)
{
struct timeval tv;

event_del(&ctl.evt);

tv.tv_sec = 1;
tv.tv_usec = 0;

evtimer_set(&ctl.evt, control_accept, NULL);
evtimer_add(&ctl.evt, &tv);
ctl.pause = 1;
}

static void
control_resume(void)
{
if (ctl.pause) {
evtimer_del(&ctl.evt);
ctl.pause = 0;
}
event_set(&ctl.evt, ctl.fd, EV_READ | EV_PERSIST, control_accept, NULL);
event_add(&ctl.evt, NULL);
}

static void
control_accept(int fd, short event, void *arg)
{
struct imsgproc *proc;
int sock;

if (ctl.pause) {
ctl.pause = 0;
control_resume();
return;
}

sock = accept4(ctl.fd, NULL, NULL, SOCK_CLOEXEC | SOCK_NONBLOCK);
if (sock == -1) {
if (errno == ENFILE || errno == EMFILE)
control_pause();
else if (errno != EWOULDBLOCK && errno != EINTR &&
errno != ECONNABORTED)
log_warn("%s: accept4", __func__);
return;
}

proc = proc_attach(PROC_CLIENT, sock);
if (proc == NULL) {
log_warn("%s: proc_attach", __func__);
close(sock);
return;
}
proc_setcallback(proc, control_dispatch_client, NULL);
proc_enable(proc);
}

static void
control_close(struct imsgproc *proc)
{
proc_free(proc);

if (ctl.pause)
control_resume();
}

static void
control_dispatch_priv(struct imsgproc *proc, struct imsg *imsg, void *arg)
{
if (imsg == NULL) {
log_debug("%s: imsg connection lost", __func__);
event_loopexit(NULL);
return;
}

if (log_getverbose() > LOGLEVEL_IMSG)
log_imsg(proc, imsg);

switch (imsg->hdr.type) {
case IMSG_CONF_START:
m_end(proc);
break;

case IMSG_CONF_END:
m_end(proc);
control_listen();
break;

default:
fatalx("%s: unexpected imsg %s", __func__,
log_fmt_imsgtype(imsg->hdr.type));
}
}

static void
control_dispatch_client(struct imsgproc *proc, struct imsg *imsg, void *arg)
{
if (imsg == NULL) {
control_close(proc);
return;
}

if (log_getverbose() > LOGLEVEL_IMSG)
log_imsg(proc, imsg);

switch (imsg->hdr.type) {
default:
log_debug("%s: error handling imsg %d", __func__,
imsg->hdr.type);
}
}
Loading