summaryrefslogtreecommitdiff
path: root/usr.sbin/smtpd/lka_filter.c
blob: 000cb94d72ca8a9d755813f6707f818d0d7eb7ea (plain)
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
/*	$OpenBSD: lka_filter.c,v 1.4 2018/11/03 20:45:48 gilles Exp $	*/

/*
 * Copyright (c) 2018 Gilles Chehade <gilles@poolp.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/tree.h>
#include <sys/socket.h>

#include <netinet/in.h>

#include <errno.h>
#include <event.h>
#include <imsg.h>
#include <inttypes.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "smtpd.h"
#include "log.h"

static void	filter_proceed(uint64_t);
static void	filter_rewrite(uint64_t, const char *);
static void	filter_reject(uint64_t, const char *);
static void	filter_disconnect(uint64_t, const char *);

static void	filter_write(const char *, uint64_t, const char *, const char *, const char *);

static int	filter_exec_notimpl(uint64_t, struct filter_rule *, const char *, const char *);
static int	filter_exec_connected(uint64_t, struct filter_rule *, const char *, const char *);
static int	filter_exec_helo(uint64_t, struct filter_rule *, const char *, const char *);
static int	filter_exec_mail_from(uint64_t, struct filter_rule *, const char *, const char *);
static int	filter_exec_rcpt_to(uint64_t, struct filter_rule *, const char *, const char *);


static struct filter_exec {
	enum filter_phase	phase;
	const char	       *phase_name;
	int		       (*func)(uint64_t, struct filter_rule *, const char *, const char *);
} filter_execs[] = {
	{ FILTER_AUTH,     	"auth",		filter_exec_notimpl },
	{ FILTER_CONNECTED,	"connected",	filter_exec_connected },
	{ FILTER_DATA,    	"data",		filter_exec_notimpl },
	{ FILTER_EHLO,		"ehlo",		filter_exec_helo },
	{ FILTER_HELO,		"helo",		filter_exec_helo },
	{ FILTER_STARTTLS,     	"starttls",	filter_exec_notimpl },
	{ FILTER_MAIL_FROM,    	"mail-from",	filter_exec_mail_from },
	{ FILTER_NOOP,    	"noop",		filter_exec_notimpl },
	{ FILTER_QUIT,    	"quit",		filter_exec_notimpl },
	{ FILTER_RCPT_TO,    	"rcpt-to",	filter_exec_rcpt_to },
	{ FILTER_RSET,    	"rset",		filter_exec_notimpl },
};

void
lka_filter(uint64_t reqid, enum filter_phase phase, const char *hostname, const char *param)
{
	struct filter_rule	*rule;
	uint8_t			i;

	for (i = 0; i < nitems(filter_execs); ++i)
		if (phase == filter_execs[i].phase)
			break;
	if (i == nitems(filter_execs))
		goto proceed;

	TAILQ_FOREACH(rule, &env->sc_filter_rules[phase], entry) {
		if (rule->proc) {
			filter_write(rule->proc, reqid,
			    filter_execs[i].phase_name, hostname, param);
			return; /* deferred */
		}

		if (! filter_execs[i].func(reqid, rule, hostname, param)) {
			if (rule->rewrite)
				filter_rewrite(reqid, rule->rewrite);
			else if (rule->disconnect)
				filter_disconnect(reqid, rule->disconnect);
			else
				filter_reject(reqid, rule->reject);
			return;
		}
	}

proceed:
	filter_proceed(reqid);
}


int
lka_filter_response(uint64_t reqid, const char *response, const char *param)
{
	if (strcmp(response, "proceed") == 0)
		filter_proceed(reqid);
	else if (strcmp(response, "rewrite") == 0)
		filter_rewrite(reqid, param);
	else if (strcmp(response, "reject") == 0)
		filter_reject(reqid, param);
	else if (strcmp(response, "disconnect") == 0)
		filter_disconnect(reqid, param);
	else
		return 0;
	return 1;
}

static void
filter_write(const char *name, uint64_t reqid, const char *phase, const char *hostname, const char *param)
{
	int	n;

	if (strcmp(phase, "connected") == 0 ||
	    strcmp(phase, "helo") == 0 ||
	    strcmp(phase, "ehlo") == 0)
		n = io_printf(lka_proc_get_io(name),
		    "filter-request|smtp-in|%s|%016"PRIx64"|%s|%s\n",
		    phase, reqid, hostname, param);
	else
		n = io_printf(lka_proc_get_io(name),
		    "filter-request|smtp-in|%s|%016"PRIx64"|%s\n",
		    phase, reqid, param);
	if (n == -1)
		fatalx("failed to write to processor");
}

static void
filter_proceed(uint64_t reqid)
{
	m_create(p_pony, IMSG_SMTP_FILTER, 0, 0, -1);
	m_add_id(p_pony, reqid);
	m_add_int(p_pony, FILTER_PROCEED);
	m_close(p_pony);
}

static void
filter_rewrite(uint64_t reqid, const char *param)
{
	m_create(p_pony, IMSG_SMTP_FILTER, 0, 0, -1);
	m_add_id(p_pony, reqid);
	m_add_int(p_pony, FILTER_REWRITE);
	m_add_string(p_pony, param);
	m_close(p_pony);
}

static void
filter_reject(uint64_t reqid, const char *message)
{
	m_create(p_pony, IMSG_SMTP_FILTER, 0, 0, -1);
	m_add_id(p_pony, reqid);
	m_add_int(p_pony, FILTER_REJECT);
	m_add_string(p_pony, message);
	m_close(p_pony);
}

static void
filter_disconnect(uint64_t reqid, const char *message)
{
	m_create(p_pony, IMSG_SMTP_FILTER, 0, 0, -1);
	m_add_id(p_pony, reqid);
	m_add_int(p_pony, FILTER_DISCONNECT);
	m_add_string(p_pony, message);
	m_close(p_pony);
}


/* below is code for builtin filters */

static int
filter_check_table(struct filter_rule *rule, enum table_service kind, const char *key)
{
	int	ret = 0;

	if (rule->table) {
		if (table_lookup(rule->table, NULL, key, kind, NULL) > 0)
			ret = 1;
		ret = rule->not_table < 0 ? !ret : ret;
	}
	return ret;
}

static int
filter_check_regex(struct filter_rule *rule, const char *key)
{
	int	ret = 0;

	if (rule->regex) {
		if (table_lookup(rule->regex, NULL, key, K_REGEX, NULL) > 0)
			ret = 1;
		ret = rule->not_regex < 0 ? !ret : ret;
	}
	return ret;
}

static int
filter_check_rdns_connected(struct filter_rule *rule, const char *hostname)
{
	int	ret = 0;
	struct netaddr	netaddr;

	if (rule->rdns) {
		ret = text_to_netaddr(&netaddr, hostname);
		ret = rule->not_rdns < 0 ? !ret : ret;
	}
	return ret;
}

static int
filter_check_rdns_helo(struct filter_rule *rule, const char *hostname, const char *param)
{
	int	ret = 0;
	struct netaddr	netaddr;

	if (rule->rdns) {
		ret = text_to_netaddr(&netaddr, hostname);
		if (!ret)
			ret = strcasecmp(hostname, param);
		ret = rule->not_rdns < 0 ? !ret : ret;
	}
	return ret;
}

static int
filter_exec_notimpl(uint64_t reqid, struct filter_rule *rule, const char *hostname, const char *param)
{
	return 1;
}

static int
filter_exec_connected(uint64_t reqid, struct filter_rule *rule, const char *hostname, const char *param)
{
	if (filter_check_table(rule, K_NETADDR, param) ||
	    filter_check_regex(rule, param) ||
	    filter_check_rdns_connected(rule, hostname))
		return 0;
	return 1;
}

static int
filter_exec_helo(uint64_t reqid, struct filter_rule *rule, const char *hostname, const char *param)
{
	if (filter_check_table(rule, K_DOMAIN, param) ||
	    filter_check_regex(rule, param) ||
	    filter_check_rdns_helo(rule, hostname, param))
		return 0;
	return 1;
}

static int
filter_exec_mail_from(uint64_t reqid, struct filter_rule *rule, const char *hostname, const char *param)
{
	char	buffer[SMTPD_MAXMAILADDRSIZE];

	(void)strlcpy(buffer, param+1, sizeof(buffer));
	buffer[strcspn(buffer, ">")] = '\0';
	param = buffer;

	if (filter_check_table(rule, K_MAILADDR, param) ||
	    filter_check_regex(rule, param))
		return 0;
	return 1;
}

static int
filter_exec_rcpt_to(uint64_t reqid, struct filter_rule *rule, const char *hostname, const char *param)
{
	char	buffer[SMTPD_MAXMAILADDRSIZE];

	(void)strlcpy(buffer, param+1, sizeof(buffer));
	buffer[strcspn(buffer, ">")] = '\0';
	param = buffer;

	if (filter_check_table(rule, K_MAILADDR, param) ||
	    filter_check_regex(rule, param))
		return 0;
	return 1;
}