summaryrefslogtreecommitdiffstats
path: root/src/submission/submission-backend.c
blob: 889a24b7a4d48dc89e4488f62fc274cc2cf5d2a1 (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/* Copyright (c) 2018 Dovecot authors, see the included COPYING file */

#include "submission-common.h"
#include "llist.h"
#include "istream.h"
#include "istream-sized.h"

#include "submission-recipient.h"
#include "submission-client.h"
#include "submission-commands.h"
#include "submission-backend.h"

struct submission_backend_module_register
submission_backend_module_register = { 0 };

void submission_backend_init(struct submission_backend *backend,
			     pool_t pool, struct client *client,
			     const struct submission_backend_vfuncs *vfunc)
{
	backend->pool = pool;
	backend->client = client;
	backend->v = *vfunc;

	p_array_init(&backend->module_contexts, pool, 5);

	client->backends_count++;
	DLLIST_PREPEND(&client->backends, backend);
}

static void submission_backend_destroy(struct submission_backend *backend)
{
	struct client *client = backend->client;

	i_stream_unref(&backend->data_input);

	i_free(backend->fail_enh_code);
	i_free(backend->fail_reason);

	DLLIST_REMOVE(&client->backends, backend);
	backend->v.destroy(backend);
	pool_unref(&backend->pool);
}

void submission_backends_destroy_all(struct client *client)
{
	while (client->backends != NULL)
		submission_backend_destroy(client->backends);
	array_clear(&client->rcpt_backends);
	client->state.backend = NULL;
}

void submission_backend_start(struct submission_backend *backend)
{
	if (backend->started)
		return;
	if (backend->fail_reason != NULL) {
		/* Don't restart until failure is reset at transaction end */
		return;
	}
	backend->started = TRUE;
	backend->v.start(backend);
}

void submission_backend_started(struct submission_backend *backend,
				enum smtp_capability caps)
{
	struct client *client = backend->client;

	if (backend == client->backend_default)
		client_default_backend_started(client, caps);
	backend->ready = TRUE;
	if (backend->v.ready != NULL)
		backend->v.ready(backend, caps);
}

static void
submission_backend_fail_rcpts(struct submission_backend *backend)
{
	struct client *client = backend->client;
	struct submission_recipient *srcpt;
	const char *enh_code = backend->fail_enh_code;
	const char *reason = backend->fail_reason;

	i_assert(array_count(&client->rcpt_to) > 0);

	i_assert(reason != NULL);
	if (enh_code == NULL)
		enh_code = "4.0.0";

	array_foreach_elem(&client->rcpt_to, srcpt) {
		struct smtp_server_recipient *rcpt = srcpt->rcpt;

		if (srcpt->backend != backend)
			continue;
		if (rcpt->cmd == NULL)
			continue;

		smtp_server_recipient_reply(rcpt, 451, enh_code, "%s", reason);
	}
}

static inline void
submission_backend_reply_failure(struct submission_backend *backend,
				 struct smtp_server_cmd_ctx *cmd)
{
	const char *enh_code = backend->fail_enh_code;
	const char *reason = backend->fail_reason;

	if (enh_code == NULL)
		enh_code = "4.0.0";

	i_assert(smtp_server_command_get_reply_count(cmd->cmd) == 1);
	smtp_server_reply(cmd, 451, enh_code, "%s", reason);
}

static inline bool
submission_backend_handle_failure(struct submission_backend *backend,
				  struct smtp_server_cmd_ctx *cmd)
{
	if (backend->fail_reason == NULL)
		return TRUE;

	/* already failed */
	submission_backend_reply_failure(backend, cmd);
	return TRUE;
}

void submission_backend_fail(struct submission_backend *backend,
			     struct smtp_server_cmd_ctx *cmd,
			     const char *enh_code, const char *reason)
{
	struct client *client = backend->client;
	bool failed_before = (backend->fail_reason != NULL);

	/* Can be called several times */

	if (backend == client->backend_default) {
		/* default backend: fail the whole client */
		client_destroy(&client, enh_code, reason);
		return;
	}

	/* Non-default backend for this transaction (maybe even for only
	   some of the approved recipients): fail only the affected
	   transaction and/or specific recipients. */

	/* Remember the failure only once */
	if (!failed_before) {
		backend->fail_enh_code = i_strdup(enh_code);
		backend->fail_reason = i_strdup(reason);
	}
	if (cmd == NULL) {
		/* Called outside command context: just remember the failure */
	} else if (smtp_server_command_get_reply_count(cmd->cmd) > 1) {
		/* Fail DATA/BDAT/BURL command expecting several replies */
		submission_backend_fail_rcpts(backend);
	} else {
		/* Single command */
		submission_backend_reply_failure(backend, cmd);
	}

	/* Call the fail vfunc only once */
	if (!failed_before && backend->v.fail != NULL)
		backend->v.fail(backend, enh_code, reason);
	backend->started = FALSE;
	backend->ready = FALSE;
}

void submission_backends_client_input_pre(struct client *client)
{
	struct submission_backend *backend;

	for (backend = client->backends; backend != NULL;
	     backend = backend->next) {
		if (!backend->started)
			continue;
		if (backend->v.client_input_pre != NULL)
			backend->v.client_input_pre(backend);

	}
}

void submission_backends_client_input_post(struct client *client)
{
	struct submission_backend *backend;

	for (backend = client->backends; backend != NULL;
	     backend = backend->next) {
		if (!backend->started)
			continue;
		if (backend->v.client_input_post != NULL)
			backend->v.client_input_post(backend);
	}
}

uoff_t submission_backend_get_max_mail_size(struct submission_backend *backend)
{
	if (backend->v.get_max_mail_size != NULL)
		return backend->v.get_max_mail_size(backend);
	return UOFF_T_MAX;
}

void submission_backend_trans_start(struct submission_backend *backend,
				    struct smtp_server_transaction *trans)
{
	submission_backend_start(backend);

	if (backend->trans_started)
		return;
	backend->trans_started = TRUE;

	if (backend->v.trans_start != NULL) {
		backend->v.trans_start(backend, trans,
				       trans->mail_from, &trans->params);
	}
}

static void
submission_backend_trans_free(struct submission_backend *backend,
			      struct smtp_server_transaction *trans)
{
	i_stream_unref(&backend->data_input);
	if (backend->v.trans_free != NULL)
		backend->v.trans_free(backend, trans);
	backend->trans_started = FALSE;

	i_free(backend->fail_enh_code);
	i_free(backend->fail_reason);
}

void submission_backends_trans_start(struct client *client,
				     struct smtp_server_transaction *trans)
{
	struct submission_backend *backend;

	i_assert(client->state.backend != NULL);
	submission_backend_trans_start(client->state.backend, trans);

	array_foreach_elem(&client->pending_backends, backend)
		submission_backend_trans_start(backend, trans);
	array_clear(&client->pending_backends);
}

void submission_backends_trans_free(struct client *client,
				     struct smtp_server_transaction *trans)
{
	struct submission_backend *backend;

	i_assert(client->state.backend != NULL ||
		 array_count(&client->rcpt_backends) == 0);

	array_foreach_elem(&client->rcpt_backends, backend)
		submission_backend_trans_free(backend, trans);
	array_clear(&client->pending_backends);
	array_clear(&client->rcpt_backends);
	client->state.backend = NULL;
}

int submission_backend_cmd_helo(struct submission_backend *backend,
				struct smtp_server_cmd_ctx *cmd,
				struct smtp_server_cmd_helo *data)
{
	/* failure on default backend closes the client connection */
	i_assert(backend->fail_reason == NULL);

	if (!backend->started || backend->v.cmd_helo == NULL) {
		/* default backend is not interested, respond right away */
		submission_helo_reply_submit(cmd, data);
		return 1;
	}

	return backend->v.cmd_helo(backend, cmd, data);
}

void submission_backend_helo_reply_submit(
	struct submission_backend *backend ATTR_UNUSED,
	struct smtp_server_cmd_ctx *cmd, struct smtp_server_cmd_helo *data)
{
	submission_helo_reply_submit(cmd, data);
}

int submission_backend_cmd_mail(struct submission_backend *backend,
				struct smtp_server_cmd_ctx *cmd,
				struct smtp_server_cmd_mail *data)
{
	if (!submission_backend_handle_failure(backend, cmd))
		return -1;

	submission_backend_start(backend);

	if (backend->v.cmd_mail == NULL) {
		/* mail backend is not interested, respond right away */
		return 1;
	}

	return backend->v.cmd_mail(backend, cmd, data);
}

static void
submission_backend_add_pending(struct submission_backend *backend)
{
	struct client *client = backend->client;
	struct submission_backend *pending_backend;

	array_foreach_elem(&client->pending_backends, pending_backend) {
		if (backend == pending_backend)
			return;
	}

	array_push_back(&client->pending_backends, &backend);
}

int submission_backend_cmd_rcpt(struct submission_backend *backend,
				struct smtp_server_cmd_ctx *cmd,
				struct submission_recipient *srcpt)
{
	struct smtp_server_transaction *trans;

	if (!submission_backend_handle_failure(backend, cmd))
		return -1;

	i_assert(backend->started);

	if (backend->v.cmd_rcpt == NULL) {
		/* backend is not interested, respond right away */
		return 1;
	}

	trans = smtp_server_connection_get_transaction(cmd->conn);
	if (trans != NULL)
		submission_backend_trans_start(srcpt->backend, trans);
	else
		submission_backend_add_pending(srcpt->backend);

	return backend->v.cmd_rcpt(backend, cmd, srcpt);
}

int submission_backend_cmd_rset(struct submission_backend *backend,
				struct smtp_server_cmd_ctx *cmd)
{
	if (!submission_backend_handle_failure(backend, cmd))
		return -1;

	submission_backend_start(backend);

	if (backend->v.cmd_rset == NULL) {
		/* backend is not interested, respond right away */
		return 1;
	}
	return backend->v.cmd_rset(backend, cmd);
}

static int
submission_backend_cmd_data(struct submission_backend *backend,
			    struct smtp_server_cmd_ctx *cmd,
			    struct smtp_server_transaction *trans)
{
	if (backend->fail_reason != NULL) {
		submission_backend_fail_rcpts(backend);
		return 0;
	}

	i_assert(backend->started);

	return backend->v.cmd_data(backend, cmd, trans,
				   backend->data_input, backend->data_size);
}

int submission_backends_cmd_data(struct client *client,
				 struct smtp_server_cmd_ctx *cmd,
				 struct smtp_server_transaction *trans,
				 struct istream *data_input, uoff_t data_size)
{
	struct submission_backend *backend;
	int ret = 0;

	i_assert(array_count(&client->rcpt_backends) > 0);

	/* Make sure data input stream is at the beginning (plugins may have
	   messed with it. */
	i_stream_seek(data_input, 0);

	/* create the data_input streams first */
	array_foreach_elem(&client->rcpt_backends, backend) {
		backend->data_input =
			i_stream_create_sized(data_input, data_size);
		backend->data_size = data_size;
	}

	/* now that all the streams are created, start reading them
	   (reading them earlier could have caused the data_input parent's
	   offset to change) */
	array_foreach_elem(&client->rcpt_backends, backend) {
		ret = submission_backend_cmd_data(backend, cmd, trans);
		if (ret < 0)
			break;
	}

	return ret;
}

int submission_backend_cmd_vrfy(struct submission_backend *backend,
				struct smtp_server_cmd_ctx *cmd,
				const char *param)
{
	/* failure on default backend closes the client connection */
	i_assert(backend->fail_reason == NULL);

	submission_backend_start(backend);

	if (backend->v.cmd_vrfy == NULL) {
		/* backend is not interested, respond right away */
		return 1;
	}
	return backend->v.cmd_vrfy(backend, cmd, param);
}

int submission_backend_cmd_noop(struct submission_backend *backend,
				struct smtp_server_cmd_ctx *cmd)
{
	/* failure on default backend closes the client connection */
	i_assert(backend->fail_reason == NULL);

	submission_backend_start(backend);

	if (backend->v.cmd_noop == NULL) {
		/* backend is not interested, respond right away */
		return 1;
	}
	return backend->v.cmd_noop(backend, cmd);
}

int submission_backend_cmd_quit(struct submission_backend *backend,
				struct smtp_server_cmd_ctx *cmd)
{
	/* failure on default backend closes the client connection */
	i_assert(backend->fail_reason == NULL);

	if (!backend->started) {
		/* quit before backend even started */
		return 1;
	}
	if (backend->v.cmd_quit == NULL) {
		/* backend is not interested, respond right away */
		return 1;
	}
	return backend->v.cmd_quit(backend, cmd);
}