summaryrefslogtreecommitdiffstats
path: root/src/lmtp/lmtp-commands.c
blob: dd8b791e0cbe88487c47e2513a472a04bb3a7837 (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
/* Copyright (c) 2009-2018 Dovecot authors, see the included COPYING file */

#include "lmtp-common.h"
#include "str.h"
#include "istream.h"
#include "istream-concat.h"
#include "ostream.h"
#include "iostream-temp.h"
#include "master-service.h"
#include "settings-parser.h"
#include "lda-settings.h"
#include "mail-user.h"
#include "smtp-address.h"
#include "mail-deliver.h"
#include "mail-error.h"
#include "lmtp-recipient.h"
#include "lmtp-proxy.h"
#include "lmtp-local.h"
#include "lmtp-commands.h"

/*
 * MAIL command
 */

int cmd_mail(void *conn_ctx,
	     struct smtp_server_cmd_ctx *cmd,
	     struct smtp_server_cmd_mail *data)
{
	struct client *client = (struct client *)conn_ctx;

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

int client_default_cmd_mail(struct client *client,
			    struct smtp_server_cmd_ctx *cmd ATTR_UNUSED,
			    struct smtp_server_cmd_mail *data ATTR_UNUSED)
{
	if (client->lmtp_set->lmtp_user_concurrency_limit > 0) {
		/* Connect to anvil before dropping privileges */
		lmtp_anvil_init();
	}
	return 1;
}

/*
 * RCPT command
 */

static int
cmd_rcpt_handle_forward_fields(struct smtp_server_cmd_ctx *cmd,
			       struct lmtp_recipient *lrcpt)
{
	struct smtp_server_recipient *rcpt = lrcpt->rcpt;
	string_t *xforward;
	const char *error;
	int ret;

	ret = smtp_params_rcpt_decode_extra(&rcpt->params,
					    LMTP_RCPT_FORWARD_PARAMETER,
					    &xforward, FALSE, &error);
	if (ret < 0) {
		smtp_server_reply(cmd, 501, "5.5.4",
				  "Invalid "LMTP_RCPT_FORWARD_PARAMETER"= "
				  "parameter: %s", error);
		return -1;
	}
	if (ret == 0)
		return 0;

	/* Drop the parameter */
	(void)smtp_params_rcpt_drop_extra(&rcpt->params,
					  LMTP_RCPT_FORWARD_PARAMETER, NULL);

	/* Check the real IP rather than the proxied client IP, since XCLIENT
	   command will update that, thereby making it untrusted. Unlike the
	   XCLIENT command, the RCPT forward parameter needs to be used after
	   the XCLIENT is first issued. */
	if (!smtp_server_connection_is_trusted(rcpt->conn)) {
		smtp_server_reply(cmd, 550, "5.7.14",
				  "Unacceptable "LMTP_RCPT_FORWARD_PARAMETER"= "
				  "parameter: You are not from trusted IP");
		return -1;
	}

	lrcpt->forward_fields = p_strdup(rcpt->pool, str_c(xforward));
	return 0;
}

int cmd_rcpt(void *conn_ctx, struct smtp_server_cmd_ctx *cmd,
	     struct smtp_server_recipient *rcpt)
{
	struct client *client = (struct client *)conn_ctx;
	struct smtp_server_transaction *trans;
	struct lmtp_recipient *lrcpt;

	trans = smtp_server_connection_get_transaction(rcpt->conn);
	i_assert(trans != NULL); /* MAIL command is synchronous */

	lrcpt = lmtp_recipient_create(client, trans, rcpt);

	if (cmd_rcpt_handle_forward_fields(cmd, lrcpt) < 0)
		return -1;

	return client->v.cmd_rcpt(client, cmd, lrcpt);
}

int client_default_cmd_rcpt(struct client *client,
			    struct smtp_server_cmd_ctx *cmd,
			    struct lmtp_recipient *lrcpt)
{
	struct smtp_server_recipient *rcpt = lrcpt->rcpt;
	const char *username, *detail;
	char delim = '\0';
	int ret;

	i_assert(!smtp_address_isnull(rcpt->path));
	if (*rcpt->path->localpart == '\0' && rcpt->path->domain == NULL) {
		smtp_server_recipient_reply(
			rcpt, 550, "5.1.1",
			"Unacceptable TO: Empty path not allowed");
		return -1;
	}

	smtp_address_detail_parse_temp(
		client->unexpanded_lda_set->recipient_delimiter,
		rcpt->path, &username, &delim, &detail);
	i_assert(*username != '\0');

	/* Make user name and detail available in the recipient event. The
	   mail_user event (for local delivery) also adds the user field, but
	   adding it here makes it available to the recipient event in general.
	   Additionally, the auth lookups performed for local and proxy delivery
	   can further override the "user" recipient event when the auth service
	   returns a different user name. In any case, we provide the initial
	   value here.
	 */
	event_add_str(rcpt->event, "user", username);
	if (detail[0] != '\0')
		event_add_str(rcpt->event, "detail", detail);

	if (client->lmtp_set->lmtp_proxy) {
		/* proxied? */
		if ((ret=lmtp_proxy_rcpt(client, cmd, lrcpt,
					 username, detail, delim)) != 0)
			return (ret < 0 ? -1 : 0);
		/* no */
	}

	/* local delivery */
	return lmtp_local_rcpt(client, cmd, lrcpt, username, detail);
}

/*
 * DATA command
 */

static void
cmd_data_create_added_headers(struct client *client,
			      struct smtp_server_cmd_ctx *cmd ATTR_UNUSED,
			      struct smtp_server_transaction *trans)
{
	size_t proxy_offset = 0;
	string_t *str;

	str = t_str_new(512);

	/* Headers for local deliveries only */
	if (client->local != NULL)
		lmtp_local_add_headers(client->local, trans, str);

	/* Headers for local and proxied messages */
	proxy_offset = str_len(str);
	if (client->lmtp_set->lmtp_add_received_header) {
		const struct lmtp_settings *lmtp_set = client->lmtp_set;
		enum smtp_server_trace_rcpt_to_address rcpt_to_address =
			SMTP_SERVER_TRACE_RCPT_TO_ADDRESS_FINAL;

		switch (lmtp_set->parsed_lmtp_hdr_delivery_address) {
		case LMTP_HDR_DELIVERY_ADDRESS_NONE:
			rcpt_to_address =
				SMTP_SERVER_TRACE_RCPT_TO_ADDRESS_NONE;
			break;
		case LMTP_HDR_DELIVERY_ADDRESS_FINAL:
			rcpt_to_address =
				SMTP_SERVER_TRACE_RCPT_TO_ADDRESS_FINAL;
			break;
		case LMTP_HDR_DELIVERY_ADDRESS_ORIGINAL:
			rcpt_to_address =
				SMTP_SERVER_TRACE_RCPT_TO_ADDRESS_ORIGINAL;
			break;
		}

		smtp_server_transaction_write_trace_record(
			str, trans, rcpt_to_address);
	}

	client->state.added_headers_local =
		p_strdup(client->state_pool, str_c(str));
	client->state.added_headers_proxy =
		client->state.added_headers_local + proxy_offset;
}

static int
cmd_data_finish(struct client *client,
		struct smtp_server_cmd_ctx *cmd,
		struct smtp_server_transaction *trans)
{
	struct client_state *state = &client->state;
	struct istream *input_msg;
	int ret;

	i_assert(HAS_ALL_BITS(trans->flags,
			      SMTP_SERVER_TRANSACTION_FLAG_REPLY_PER_RCPT));

	client->state.data_end_timeval = ioloop_timeval;

	/* finish the message */
	input_msg = iostream_temp_finish(&state->mail_data_output,
					 IO_BLOCK_SIZE);

	ret = client->v.cmd_data(client, cmd, trans,
				 input_msg, client->state.data_size);
	i_stream_unref(&input_msg);

	return ret;
}

int cmd_data_continue(void *conn_ctx, struct smtp_server_cmd_ctx *cmd,
		      struct smtp_server_transaction *trans)
{
	struct client *client = (struct client *)conn_ctx;
	struct client_state *state = &client->state;
	struct istream *data_input = state->data_input;
	const unsigned char *data;
	size_t size;
	ssize_t ret;

	i_assert(state->mail_data_output != NULL);

	while ((ret = i_stream_read(data_input)) > 0 || ret == -2) {
		data = i_stream_get_data(data_input, &size);
		if (o_stream_send(state->mail_data_output,
			data, size) != (ssize_t)size) {
			e_error(client->event, "write(%s) failed: %s",
				o_stream_get_name(state->mail_data_output),
				o_stream_get_error(state->mail_data_output));
			smtp_server_reply(cmd, 451, "4.3.0",
				"Temporary internal failure");
			return -1;
		}

		i_stream_skip(data_input, size);

		if (!smtp_server_cmd_data_check_size(cmd))
			return -1;
	}

	if (ret == 0)
		return 0;
	if (ret < 0 && data_input->stream_errno != 0) {
		/* Client probably disconnected */
		return -1;
	}

	/* Current data stream position is the data size */
	client->state.data_size = data_input->v_offset;

	/* The ending "." line was seen. finish delivery. */
	return cmd_data_finish(client, cmd, trans);
}

int cmd_data_begin(void *conn_ctx,
		   struct smtp_server_cmd_ctx *cmd ATTR_UNUSED,
		   struct smtp_server_transaction *trans ATTR_UNUSED,
		   struct istream *data_input)
{
	struct client *client = (struct client *)conn_ctx;
	string_t *path;

	i_assert(client->state.mail_data_output == NULL);

	path = t_str_new(256);
	mail_user_set_get_temp_prefix(path, client->raw_mail_user->set);
	client->state.mail_data_output = 
		iostream_temp_create_named(str_c(path), 0, "(lmtp data)");

	client->state.data_input = data_input;
	return 0;
}

int client_default_cmd_data(struct client *client,
			    struct smtp_server_cmd_ctx *cmd,
			    struct smtp_server_transaction *trans,
			    struct istream *data_input,
			    uoff_t data_size ATTR_UNUSED)
{
	struct client_state *state = &client->state;
	struct istream *input_local, *input_proxy;
	struct istream *inputs[3];

	/* Formulate prepended headers for both local and proxy delivery */
	cmd_data_create_added_headers(client, cmd, trans);

	/* Construct message streams for local and proxy delivery */
	input_local = input_proxy = NULL;
	if (client->local != NULL) {
		inputs[0] = i_stream_create_from_data(
			state->added_headers_local,
			strlen(state->added_headers_local));
		inputs[1] = data_input;
		inputs[2] = NULL;

		input_local = i_stream_create_concat(inputs);
		i_stream_set_name(input_local, "<lmtp DATA local>");
		i_stream_unref(&inputs[0]);
	}
	if (client->proxy != NULL) {
		inputs[0] = i_stream_create_from_data(
			state->added_headers_proxy,
			strlen(state->added_headers_proxy));
		inputs[1] = data_input;
		inputs[2] = NULL;

		input_proxy = i_stream_create_concat(inputs);
		i_stream_set_name(input_proxy, "<lmtp DATA proxy>");
		i_stream_unref(&inputs[0]);
	}

	/* local delivery */
	if (client->local != NULL) {
		lmtp_local_data(client, cmd, trans, input_local);
		i_stream_unref(&input_local);
	}
	/* proxy delivery */
	if (client->proxy != NULL) {
		lmtp_proxy_data(client, cmd, trans, input_proxy);
		i_stream_unref(&input_proxy);
	}
	return 0;
}