Skip to content

Commit a1c4932

Browse files
committed
initial import
0 parents  commit a1c4932

26 files changed

Lines changed: 3546 additions & 0 deletions

.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
**/*~
2+
**/*.d
3+
**/*.o
4+
5+
**/.deps
6+
**/Makefile
7+
**/Makefile.in
8+
**/aclocal.m4
9+
**/.dirstamp
10+
**/autom4te.cache
11+
**/config.h
12+
**/config.h.in
13+
**/config.log
14+
**/config.status
15+
**/configure
16+
**/stamp-h1
17+
18+
table-postgres

Makefile.am

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
noinst_PROGRAMS = table-postgres
2+
3+
table_postgres_SOURCES = table_postgres.c dict.c log.c table_stdio.c util.c
4+
5+
AM_CFLAGS =
6+
LDADD = $(LIBOBJS)
7+
8+
smtpdir = ${prefix}/libexec/smtpd
9+
10+
install-exec-local: $(noinst_PROGRAMS)
11+
$(MKDIR_P) $(DESTDIR)$(smtpdir)
12+
$(INSTALL_PROGRAM_ENV) $(INSTALL_PROGRAM) $(noinst_PROGRAMS) $(DESTDIR)$(smtpdir)
13+
14+
README.md: table-postgres.5
15+
mandoc -Tmarkdown -l table-postgres.5 > README.md

README.md

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
TABLE\_POSTGRESQL(5) - File Formats Manual
2+
3+
# NAME
4+
5+
**table\_postgresql** - format description for smtpd PostgreSQL tables
6+
7+
# DESCRIPTION
8+
9+
This manual page documents the file format of PostgreSQL tables used
10+
by the
11+
smtpd(8)
12+
mail daemon.
13+
14+
The format described here applies to tables as defined in
15+
smtpd.conf(5).
16+
17+
# POSTGRESQL TABLE
18+
19+
A postgresql table allows the storing of usernames, passwords, aliases, and domains
20+
in a format that is shareable across various machines that support
21+
postgres(1).
22+
23+
The table is used by
24+
smtpd(8)
25+
when authenticating a user, when user information such as user-id and/or
26+
home directory is required for a delivery, when a domain lookup may be required,
27+
and/or when looking for an alias.
28+
29+
A PostgreSQL table consists of one or more
30+
postgresql(1)
31+
databases with one or more tables.
32+
33+
If the table is used for authentication, the password should be
34+
encrypted using the
35+
crypt(3)
36+
function. Such passwords can be generated using the
37+
encrypt(1)
38+
utility or
39+
smtpctl(8)
40+
encrypt command.
41+
42+
# POSTGRESQL TABLE CONFIG FILE
43+
44+
The following configuration options are available:
45+
46+
**conninfo**
47+
**host**=*'host'*
48+
**user**=*'user'*
49+
**password**=*'password'*
50+
**dbname**=*'dbname'*
51+
52+
> Connection info needed to connect to the PostgreSQL database.
53+
> For example:
54+
55+
> **conninfo**
56+
> **host**=*'db.example.com'*
57+
> **user**=*'maildba'*
58+
> **password**=*'OpenSMTPDRules!'*
59+
> **dbname**=*'opensmtpdb'*
60+
61+
**query\_alias**
62+
*SQL statement*
63+
64+
> This is used to provide a query to look up aliases. The question mark
65+
> is replaced with the appropriate data. For alias it is the left hand side of
66+
> the SMTP address. This expects one VARCHAR to be returned with the user name
67+
> the alias resolves to.
68+
69+
**query\_credentials**
70+
*SQL statement*
71+
72+
> This is used to provide a query for looking up user credentials. The question
73+
> mark is replaced with the appropriate data. For credentials it is the left
74+
> hand side of the SMTP address. The query expects that there are two VARCHARS
75+
> returned, one with a user name and one with a password in
76+
> crypt(3)
77+
> format.
78+
79+
**query\_domain**
80+
*SQL statement*
81+
82+
> This is used to provide a query for looking up a domain. The question mark
83+
> is replaced with the appropriate data. For the domain it would be the
84+
> right hand side of the SMTP address. This expects one VARCHAR to be returned
85+
> with a matching domain name.
86+
87+
**query\_mailaddrmap**
88+
*SQL statement*
89+
90+
> This is used to provide a query to look up senders. The question mark
91+
> is replaced with the appropriate data. This expects one VARCHAR to be
92+
> returned with the address the sender is allowed to send mails from.
93+
94+
A generic SQL statement would be something like:
95+
96+
query_ SELECT value FROM table WHERE key=$1;
97+
98+
# EXAMPLES
99+
100+
## GENERIC EXAMPLE
101+
102+
Example based on the OpenSMTPD FAQ: Building a Mail Server
103+
The filtering part is excluded in this example.
104+
105+
The configuration below is for a medium-size mail server which handles
106+
multiple domains with multiple virtual users and is based on several
107+
assumptions. One is that a single system user named vmail is used for all
108+
virtual users. This user needs to be created:
109+
110+
# useradd -g =uid -c "Virtual Mail" -d /var/vmail -s /sbin/nologin vmail
111+
# mkdir /var/vmail
112+
# chown vmail:vmail /var/vmail
113+
114+
*PostgreSQL schema*
115+
116+
CREATE TABLE domains (
117+
id SERIAL,
118+
domain VARCHAR(255) NOT NULL DEFAULT ''
119+
);
120+
CREATE TABLE virtuals (
121+
id SERIAL,
122+
email VARCHAR(255) NOT NULL DEFAULT '',
123+
destination VARCHAR(255) NOT NULL DEFAULT ''
124+
);
125+
CREATE TABLE credentials (
126+
id SERIAL,
127+
email VARCHAR(255) NOT NULL DEFAULT '',
128+
password VARCHAR(255) NOT NULL DEFAULT ''
129+
);
130+
INSERT INTO domains VALUES (1, "example.com");
131+
INSERT INTO domains VALUES (2, "example.net");
132+
INSERT INTO domains VALUES (3, "example.org");
133+
134+
INSERT INTO virtuals VALUES (1, "abuse@example.com", "bob@example.com");
135+
INSERT INTO virtuals VALUES (2, "postmaster@example.com", "bob@example.com");
136+
INSERT INTO virtuals VALUES (3, "webmaster@example.com", "bob@example.com");
137+
INSERT INTO virtuals VALUES (4, "bob@example.com", "vmail");
138+
INSERT INTO virtuals VALUES (5, "abuse@example.net", "alice@example.net");
139+
INSERT INTO virtuals VALUES (6, "postmaster@example.net", "alice@example.net");
140+
INSERT INTO virtuals VALUES (7, "webmaster@example.net", "alice@example.net");
141+
INSERT INTO virtuals VALUES (8, "alice@example.net", "vmail");
142+
143+
INSERT INTO credentials VALUES (1, "bob@example.com", "$2b$08$ANGFKBL.BnDLL0bUl7I6aumTCLRJSQluSQLuueWRG.xceworWrUIu");
144+
INSERT INTO credentials VALUES (2, "alice@example.net", "$2b$08$AkHdB37kaj2NEoTcISHSYOCEBA5vyW1RcD8H1HG.XX0P/G1KIYwii");
145+
146+
*/etc/mail/postgresql.conf*
147+
148+
conninfo host='db.example.com' user='maildba' password='OpenSMTPDRules!' dbname='opensmtpdb'
149+
query_alias SELECT destination FROM virtuals WHERE email=$1;
150+
query_credentials SELECT email, password FROM credentials WHERE email=$1;
151+
query_domain SELECT domain FROM domains WHERE domain=$1;
152+
153+
*/etc/mail/smtpd.conf*
154+
155+
table domains postgres:/etc/mail/postgres.conf
156+
table virtuals postgres:/etc/mail/postgres.conf
157+
table credentials postgres:/etc/mail/postgres.conf
158+
listen on egress port 25 tls pki mail.example.com
159+
listen on egress port 587 tls-require pki mail.example.com auth <credentials>
160+
accept from any for domain <domains> virtual <virtuals> deliver to mbox
161+
162+
## MOVING FROM POSTFIX (& POSTFIXADMIN)
163+
164+
*/etc/mail/postgres.conf*
165+
166+
conninfo host='db.example.com' user='postfix' password='PostfixOutOpenSMTPDin' dbname='postfix'
167+
query_alias SELECT destination FROM alias WHERE email=$1;
168+
query_credentials SELECT username, password FROM mailbox WHERE username=$1;
169+
query_domain SELECT domain FROM domain WHERE domain=$1;
170+
171+
The rest of the config remains the same.
172+
173+
# FILES
174+
175+
*/etc/mail/postgres.conf*
176+
177+
> Default
178+
> table-postgresql(8)
179+
> configuration file.
180+
181+
# TODO
182+
183+
Documenting the following query options:
184+
185+
**query_netaddr**
186+
**query_userinfo**
187+
**query_source**
188+
**query_mailaddr**
189+
**query_addrname**
190+
191+
# SEE ALSO
192+
193+
smtpd.conf(5),
194+
smtpctl(8),
195+
smtpd(8),
196+
encrypt(1),
197+
crypt(3)
198+
199+
Nixpkgs - September 30, 2016

bootstrap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#! /bin/sh
2+
3+
autoreconf -vfi

compat.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "config.h"
2+
3+
#include <stddef.h>
4+
#include <limits.h>
5+
6+
#ifndef UID_MAX
7+
#define UID_MAX UINT_MAX
8+
#endif
9+
#ifndef GID_MAX
10+
#define GID_MAX UINT_MAX
11+
#endif
12+
13+
#ifndef __dead
14+
#define __dead __attribute__((noreturn))
15+
#endif
16+
17+
#ifndef HAVE_ASPRINTF
18+
int asprintf(char **, const char *, ...);
19+
#endif
20+
21+
#ifndef HAVE_GETPROGNAME
22+
const char *getprogname(void);
23+
#endif
24+
25+
#ifndef HAVE_STRLCAT
26+
size_t strlcat(char *, const char *, size_t);
27+
#endif
28+
29+
#ifndef HAVE_STRLCPY
30+
size_t strlcpy(char *, const char *, size_t);
31+
#endif
32+
33+
#ifndef HAVE_STRSEP
34+
char *strsep(char **, const char *);
35+
#endif
36+
37+
#ifndef HAVE_STRTONUM
38+
long long strtonum(const char *, long long, long long, const char **);
39+
#endif

configure.ac

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
AC_INIT([table-postgres], [0.1], [bugs@opensmtpd.org])
2+
AC_CONFIG_AUX_DIR(etc)
3+
AM_INIT_AUTOMAKE([-Wall foreign subdir-objects])
4+
AC_CONFIG_LIBOBJ_DIR(openbsd-compat)
5+
AC_PROG_CC
6+
AC_USE_SYSTEM_EXTENSIONS
7+
8+
AC_ARG_WITH([libbsd],
9+
AS_HELP_STRING([--with-libbsd],
10+
[Build with libbsd library (default: disabled)]))
11+
12+
AS_IF([test "x$with_libbsd" = "xyes"], [
13+
PKG_CHECK_MODULES([libbsd], [libbsd-overlay libbsd-ctor], [
14+
CFLAGS="$libbsd_CFLAGS $CFLAGS"
15+
LIBS="$libbsd_LIBS $LIBS"
16+
])
17+
])
18+
19+
AC_REPLACE_FUNCS([ \
20+
asprintf \
21+
getprogname \
22+
err \
23+
strlcat \
24+
strlcpy \
25+
strsep \
26+
strtonum \
27+
])
28+
29+
AC_SEARCH_LIBS([PQconnectdbParams], [pq], [], [
30+
AC_MSG_ERROR([requires sqlite3])
31+
])
32+
33+
CFLAGS="$CFLAGS -I$srcdir/openbsd-compat"
34+
35+
AC_CHECK_HEADER([sys/tree.h], [], [
36+
CFLAGS="$CFLAGS -I$srcdir/openbsd-compat/tree"
37+
])
38+
39+
AC_DEFUN([CC_ADD_CHECK_FLAGS], [
40+
AC_MSG_CHECKING([if $CC supports $1 flag])
41+
old_CFLAGS="$CFLAGS"
42+
CFLAGS="$CFLAGS $1"
43+
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [])], [
44+
AC_MSG_RESULT(yes)
45+
], [
46+
AC_MSG_RESULT(no)
47+
CFLAGS="$old_CFLAGS"
48+
])
49+
])
50+
CC_ADD_CHECK_FLAGS([-MMD])
51+
52+
AC_CONFIG_HEADERS([config.h])
53+
AC_CONFIG_FILES([
54+
Makefile
55+
])
56+
AC_OUTPUT

0 commit comments

Comments
 (0)