aboutsummaryrefslogtreecommitdiffstats
path: root/extras/filters/filter-rspamd/filter_rspamd.c
blob: 43118dfdd12780553890322c32664c6dce1e7166 (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
/*
 * Copyright (c) 2016 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 "includes.h"

#include <sys/types.h>

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

#include <smtpd-api.h>

#include "rspamd.h"


static int
on_connect(uint64_t id, struct filter_connect *conn)
{
	struct session	*rs = filter_api_session(id);
	const char	*ip;

	/* will fail on local enqueuer, fallback to "localhost" */
	ip = filter_api_sockaddr_to_text((struct sockaddr *)&conn->local);
	if (ip == NULL)
		ip = "127.0.0.1";
	rs->ip = xstrdup(ip, "on_connect");
	rs->hostname = xstrdup(conn->hostname, "on_connect");

	return filter_api_accept(id);
}

static int
on_helo(uint64_t id, const char *helo)
{
	struct session	*rs = filter_api_session(id);

	rs->helo = xstrdup(helo, "on_helo");

	return filter_api_accept(id);
}

static int
on_mail(uint64_t id, struct mailaddr *mail)
{
	struct transaction	*tx = filter_api_transaction(id);
	const char		*address;

	address = filter_api_mailaddr_to_text(mail);
	tx->from = xstrdup(address, "on_mail");

	return filter_api_accept(id);
}

static int
on_rcpt(uint64_t id, struct mailaddr *rcpt)
{
	struct transaction	*tx = filter_api_transaction(id);
	const char		*address;

	address = filter_api_mailaddr_to_text(rcpt);
	dict_set(&tx->rcpts, address, NULL);

	return filter_api_accept(id);
}

static int
on_data(uint64_t id)
{
	struct transaction	*tx = filter_api_transaction(id);

	if (! rspamd_connect(tx))
		return filter_api_reject_code(id, FILTER_FAIL, 421,
		    "temporary failure");

	return 1;
}

static void
on_dataline(uint64_t id, const char *line)
{
	struct transaction     *tx = filter_api_transaction(id);

	rspamd_send_chunk(tx, line);
}

static int
on_eom(uint64_t id, size_t size)
{
	struct transaction	*tx = filter_api_transaction(id);

	rspamd_send_chunk(tx, NULL);

	return 1;
}

int
main(int argc, char **argv)
{
	int ch, C = 0, d = 0, v = 0;
	const char *l = NULL;
	char *c = NULL, *h = RSPAMD_HOST, *p = RSPAMD_PORT, *s = NULL;

	log_init(1);

	while ((ch = getopt(argc, argv, "dh:l:p:s:v")) != -1) {
		switch (ch) {
		case 'C':
			C = 1;
			break;
		case 'c':
			c = optarg;
			break;
		case 'd':
			d = 1;
			break;
		case 'h':
			h = optarg;
			break;
		case 'l':
			l = optarg;
			break;
		case 'p':
			p = optarg;
			break;
		case 's':
			s = optarg;
			break;
		case 'v':
			v |= TRACE_DEBUG;
			break;
		default:
			log_warnx("warn: bad option");
			return 1;
			/* NOTREACHED */
		}
	}
	argc -= optind;
	argv += optind;

	if (c)
		c = strip(c);
	if (h)
		h = strip(h);
	if (p)
		p = strip(p);

	log_init(d);
	log_verbose(v);

	log_debug("debug: starting...");

	rspamd_resolve(h, p);

	filter_api_on_connect(on_connect);
	filter_api_on_helo(on_helo);
	filter_api_on_mail(on_mail);
	filter_api_on_rcpt(on_rcpt);
	filter_api_on_data(on_data);
	filter_api_on_dataline(on_dataline);
	filter_api_on_eom(on_eom);

	filter_api_session_allocator(session_allocator);
	filter_api_session_destructor(session_destructor);

	filter_api_transaction_allocator(transaction_allocator);
	filter_api_transaction_destructor(transaction_destructor);

	filter_api_data_buffered();

	/*
	if (c)
		filter_api_set_chroot(c);
	if (C)
		filter_api_no_chroot();
	*/
	filter_api_no_chroot();

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

	return 1;
}