aboutsummaryrefslogtreecommitdiffstats
path: root/extras/filters/filter-regex/filter_regex.c
blob: 02fd36ef20ba619beb5f5886370dc43a485a9456 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 * Copyright (c) 2015, Armin Wolfermann <armin@wolfermann.org>
 * Copyright (c) 2015, 2016 Joerg Jung <jung@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 "includes.h"

#include <sys/types.h>
#include <sys/queue.h>

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include <regex.h>
#include <unistd.h>

#include <smtpd-api.h>

#define REGEX_CONF SMTPD_CONFDIR "/filter-regex.conf"

struct regex {
	SIMPLEQ_ENTRY(regex) el;
	char *s, n;
	regex_t p;
};

static SIMPLEQ_HEAD(regex_q, regex)
	regex_connect = SIMPLEQ_HEAD_INITIALIZER(regex_connect),
	regex_helo = SIMPLEQ_HEAD_INITIALIZER(regex_helo),
	regex_mail = SIMPLEQ_HEAD_INITIALIZER(regex_mail),
	regex_rcpt = SIMPLEQ_HEAD_INITIALIZER(regex_rcpt),
	regex_dataline = SIMPLEQ_HEAD_INITIALIZER(regex_dataline);
static struct { const char *s; struct regex_q *rq; } regex_s[] = {
	{ "connect", &regex_connect }, { "helo", &regex_helo },
	{ "mail", &regex_mail }, { "rcpt", &regex_rcpt },
	{ "dataline", &regex_dataline }, { NULL, NULL } };
static size_t regex_limit;

static int
regex_parse(char *l, size_t no)
{
	struct regex_q *rq = NULL;
	struct regex *re;
	char *k, buf[BUFSIZ];
	int i, r;

	l = strip(l);
	if ((k = strsep(&l, " \t")) == NULL || strlen(k) == 0 || *k == '#')
		return 0; /* skip empty or commented line */
	for (i = 0; regex_s[i].s != NULL && rq == NULL; i++)
		if (strcmp(k, regex_s[i].s) == 0)
			rq = regex_s[i].rq;
	if (rq == NULL) {
		log_warnx("warn: parse: unknown keyword %s line %lu", k, no);
		return -1;
	}
	if (strlen((l = strip(l))) == 0 || *l == '#') {
		log_warnx("warn: parse: missing value line %lu", no);
		return -1;
	}
	re = xcalloc(1, sizeof(struct regex), "parse");
	re->s = xstrdup(l, "parse");
	if ((re->n = (l[0] == '!' && isspace((unsigned char)l[1]))))
		l = strip(++l);
	if ((r = regcomp(&re->p, l, REG_EXTENDED|REG_NOSUB)) != 0) {
		regerror(r, &re->p, buf, sizeof(buf));
		log_warnx("warn: parse: regcomp %s line %lu", buf, no);
		free(re->s);
		free(re);
		return -1;
	}
	SIMPLEQ_INSERT_TAIL(rq, re, el);
	log_debug("debug: parse: %s %s line %lu", k, re->s, no);
	return 0;
}

static int
regex_load(const char *c)
{
	FILE *f;
	char *l = NULL;
	size_t sz = 0, no = 0;
	ssize_t len;

	if ((f = fopen(c, "r")) == NULL) {
		log_warn("warn: load: fopen %s", c);
		return -1;
	}
	while ((len = getline(&l, &sz, f)) != -1) {
		if (l[len - 1] == '\n')
			l[len - 1] = '\0';
		if (regex_parse(l, ++no) == -1) {
			free(l);
			fclose(f);
			return -1;
		}
	}
	if (ferror(f)) {
		log_warn("warn: load: getline");
		free(l);
		fclose(f);
		return -1;
	}
	free(l);
	fclose(f);
	return 0;
}

static int
regex_match(struct regex_q *rq, const char *s)
{
	struct regex *re;
	char buf[BUFSIZ];
	int r;

	SIMPLEQ_FOREACH(re, rq, el) {
		if ((r = regexec(&re->p, s, 0, NULL, 0)) != 0) {
			if (r != REG_NOMATCH) {
				regerror(r, &re->p, buf, sizeof(buf));
				log_warnx("warn: match: regexec %s", buf);
			}
			continue;
		}
		log_info("info: match: %s to %s", re->s, s);
		return (re->n == 0);
	}
	return 0;
}

static void
regex_clear(void)
{
	struct regex *re;
	int i;

	for (i = 0; regex_s[i].rq != NULL; i++) {
		while((re = SIMPLEQ_FIRST(regex_s[i].rq)) != NULL) {
			SIMPLEQ_REMOVE_HEAD(regex_s[i].rq, el);
			regfree(&re->p);
			free(re->s);
			free(re);
		}
	}
}

static int
regex_on_connect(uint64_t id, struct filter_connect *c)
{
	if (regex_match(&regex_connect, c->hostname)) {
		log_warnx("warn: session %016"PRIx64": on_connect: REJECT connect hostname", id);
		return filter_api_reject_code(id, FILTER_FAIL, 554, "5.7.1 Hostname rejected");
	}
	return filter_api_accept(id);
}

static int
regex_on_helo(uint64_t id, const char *h)
{
	if (regex_match(&regex_helo, h)) {
		log_warnx("warn: session %016"PRIx64": on_helo: REJECT helo hostname", id);
		return filter_api_reject_code(id, FILTER_FAIL, 554, "5.7.1 Helo rejected");
	}
	return filter_api_accept(id);
}

static int
regex_on_mail(uint64_t id, struct mailaddr *m)
{
	if (regex_match(&regex_mail, filter_api_mailaddr_to_text(m))) {
		log_warnx("warn: session %016"PRIx64": on_mail: REJECT mail from", id);
		return filter_api_reject_code(id, FILTER_FAIL, 554, "5.7.1 Sender rejected");
	}
	return filter_api_accept(id);
}

static int
regex_on_rcpt(uint64_t id, struct mailaddr *r)
{
	if (regex_match(&regex_rcpt, filter_api_mailaddr_to_text(r))) {
		log_warnx("warn: session %016"PRIx64": on_rcpt: REJECT rcpt to", id);
		return filter_api_reject_code(id, FILTER_FAIL, 554, "5.7.1 Recipient rejected");
	}
	return filter_api_accept(id);
}

static void
regex_on_dataline(uint64_t id, const char *l)
{
	struct { int m; size_t l; } *u;

	filter_api_writeln(id, l);
	if ((u = filter_api_get_udata(id)) == NULL) {
		u = xcalloc(1, sizeof(*u), "on_dataline");
		filter_api_set_udata(id, u);
	}
	u->l += strlen(l);
	if (u->m || (regex_limit && u->l >= regex_limit))
		return;
	u->m = regex_match(&regex_dataline, l);
}

static int
regex_on_eom(uint64_t id, size_t size)
{
	int *m;

	if ((m = filter_api_get_udata(id)) == NULL)
		return filter_api_accept(id);
	if (*m) {
		log_warnx("warn: session %016"PRIx64": on_eom: REJECT dataline", id);
		return filter_api_reject_code(id, FILTER_CLOSE, 554, "5.7.1 Message content rejected");
	}
	return filter_api_accept(id);
}

static void
regex_on_tx_commit(uint64_t id)
{
	free(filter_api_get_udata(id));
	filter_api_set_udata(id, NULL);
}

static void
regex_on_tx_rollback(uint64_t id)
{
	free(filter_api_get_udata(id));
	filter_api_set_udata(id, NULL);
}

int
main(int argc, char **argv)
{
	int ch, d = 0, v = 0;
	const char *errstr, *l = NULL;

	log_init(1);

	while ((ch = getopt(argc, argv, "dl:v")) != -1) {
		switch (ch) {
		case 'd':
			d = 1;
			break;
		case 'l':
			l = optarg;
			break;
		case 'v':
			v |= TRACE_DEBUG;
			break;
		default:
			log_warnx("warn: bad option");
			return 1;
			/* NOTREACHED */
		}
	}
	argc -= optind;
	argv += optind;
	if (argc > 1)
		fatalx("bogus argument(s)");

	if (l) {
		regex_limit = strtonum(l, 1, UINT_MAX, &errstr); /* todo: SIZE_MAX here? */
		if (errstr)
			fatalx("limit option is %s: %s", errstr, l);
	}

	log_init(d);
	log_verbose(v);

	log_debug("debug: starting...");
	if (regex_load((argc == 1) ? argv[0] : REGEX_CONF) == -1)
		fatalx("configuration failed");

	filter_api_on_connect(regex_on_connect);
	filter_api_on_helo(regex_on_helo);
	filter_api_on_mail(regex_on_mail);
	filter_api_on_rcpt(regex_on_rcpt);
	filter_api_on_dataline(regex_on_dataline);
	filter_api_on_eom(regex_on_eom);
	filter_api_on_tx_commit(regex_on_tx_commit);
	filter_api_on_tx_rollback(regex_on_tx_rollback);

	filter_api_loop();
	regex_clear();
	log_debug("debug: exiting");

	return 1;
}