/
client.c
443 lines (376 loc) · 11 KB
/
client.c
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
/* Copyright (c) 2009-2017 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "base64.h"
#include "str.h"
#include "llist.h"
#include "iostream.h"
#include "istream.h"
#include "ostream.h"
#include "hostpid.h"
#include "process-title.h"
#include "var-expand.h"
#include "settings-parser.h"
#include "anvil-client.h"
#include "master-service.h"
#include "master-service-ssl.h"
#include "master-service-settings.h"
#include "iostream-ssl.h"
#include "mail-namespace.h"
#include "mail-storage.h"
#include "mail-storage-service.h"
#include "raw-storage.h"
#include "main.h"
#include "lda-settings.h"
#include "lmtp-settings.h"
#include "lmtp-local.h"
#include "lmtp-proxy.h"
#include "commands.h"
#include "client.h"
#include <unistd.h>
#define CLIENT_IDLE_TIMEOUT_MSECS (1000*60*5)
#define CLIENT_MAX_INPUT_SIZE 4096
static struct client *clients = NULL;
unsigned int clients_count = 0;
static bool verbose_proctitle = FALSE;
const char *client_remote_id(struct client *client)
{
const char *addr;
addr = net_ip2addr(&client->remote_ip);
if (addr[0] == '\0')
addr = "local";
return addr;
}
void client_state_set(struct client *client, const char *name, const char *args)
{
string_t *title;
client->state.name = name;
if (!verbose_proctitle)
return;
title = t_str_new(128);
str_append_c(title, '[');
switch (clients_count) {
case 0:
str_append(title, "idling");
break;
case 1:
str_append(title, client_remote_id(client));
str_append_c(title, ' ');
str_append(title, client->state.name);
if (args[0] != '\0')
str_printfa(title, " %s", args);
break;
default:
str_printfa(title, "%u connections", clients_count);
break;
}
str_append_c(title, ']');
process_title_set(str_c(title));
}
static void client_idle_timeout(struct client *client)
{
client_destroy(client,
t_strdup_printf("421 4.4.2 %s", client->my_domain),
"Disconnected client for inactivity");
}
static void client_raw_user_create(struct client *client)
{
void **sets;
sets = master_service_settings_get_others(master_service);
client->raw_mail_user =
raw_storage_create_from_set(client->user_set_info, sets[0]);
}
static void client_read_settings(struct client *client)
{
struct mail_storage_service_input input;
const struct setting_parser_context *set_parser;
struct lmtp_settings *lmtp_set;
struct lda_settings *lda_set;
const char *error;
i_zero(&input);
input.module = input.service = "lmtp";
input.local_ip = client->local_ip;
input.remote_ip = client->remote_ip;
input.username = "";
if (mail_storage_service_read_settings(storage_service, &input,
client->pool,
&client->user_set_info,
&set_parser, &error) < 0)
i_fatal("%s", error);
lmtp_settings_dup(set_parser, client->pool, &lmtp_set, &lda_set);
const struct var_expand_table *tab =
mail_storage_service_get_var_expand_table(storage_service, &input);
if (settings_var_expand(&lmtp_setting_parser_info, lmtp_set,
client->pool, tab, &error) <= 0)
i_fatal("Failed to expand settings: %s", error);
client->service_set = master_service_settings_get(master_service);
client->lmtp_set = lmtp_set;
client->unexpanded_lda_set = lda_set;
}
static void client_generate_session_id(struct client *client)
{
guid_128_t guid;
string_t *id = t_str_new(30);
guid_128_generate(guid);
base64_encode(guid, sizeof(guid), id);
i_assert(str_c(id)[str_len(id)-2] == '=');
str_truncate(id, str_len(id)-2); /* drop trailing "==" */
client->state.session_id = p_strdup(client->state_pool, str_c(id));
}
struct client *client_create(int fd_in, int fd_out,
const struct master_service_connection *conn)
{
struct client *client;
pool_t pool;
/* always use nonblocking I/O */
net_set_nonblock(fd_in, TRUE);
net_set_nonblock(fd_out, TRUE);
pool = pool_alloconly_create("lmtp client", 2048);
client = p_new(pool, struct client, 1);
client->pool = pool;
client->fd_in = fd_in;
client->fd_out = fd_out;
client->remote_ip = conn->remote_ip;
client->remote_port = conn->remote_port;
client->local_ip = conn->local_ip;
client->local_port = conn->local_port;
client->input = i_stream_create_fd(fd_in, CLIENT_MAX_INPUT_SIZE);
client->output = o_stream_create_fd(fd_out, (size_t)-1);
o_stream_set_no_error_handling(client->output, TRUE);
client_io_reset(client);
client->state_pool = pool_alloconly_create("client state", 4096);
client->state.mail_data_fd = -1;
client_read_settings(client);
client_raw_user_create(client);
client_generate_session_id(client);
client->my_domain = client->unexpanded_lda_set->hostname;
client->lhlo = i_strdup("missing");
client->proxy_ttl = LMTP_PROXY_DEFAULT_TTL;
if (client->service_set->verbose_proctitle)
verbose_proctitle = TRUE;
DLLIST_PREPEND(&clients, client);
clients_count++;
client_state_set(client, "banner", "");
client_send_line(client, "220 %s %s", client->my_domain,
client->lmtp_set->login_greeting);
i_info("Connect from %s", client_remote_id(client));
return client;
}
void client_state_reset(struct client *client, const char *state_name)
{
struct mail_recipient *const *rcptp;
if (client->proxy != NULL)
lmtp_proxy_deinit(&client->proxy);
if (array_is_created(&client->state.rcpt_to)) {
array_foreach_modifiable(&client->state.rcpt_to, rcptp) {
if ((*rcptp)->anvil_query != NULL)
anvil_client_query_abort(anvil, &(*rcptp)->anvil_query);
client_rcpt_anvil_disconnect(*rcptp);
mail_storage_service_user_unref(&(*rcptp)->service_user);
}
}
if (client->state.raw_mail != NULL) {
struct mailbox_transaction_context *raw_trans =
client->state.raw_mail->transaction;
struct mailbox *raw_box = client->state.raw_mail->box;
mail_free(&client->state.raw_mail);
mailbox_transaction_rollback(&raw_trans);
mailbox_free(&raw_box);
}
buffer_free(&client->state.mail_data);
o_stream_unref(&client->state.mail_data_output);
i_close_fd(&client->state.mail_data_fd);
i_zero(&client->state);
p_clear(client->state_pool);
client->state.mail_data_fd = -1;
client_generate_session_id(client);
client_state_set(client, state_name, "");
}
void client_destroy(struct client *client, const char *prefix,
const char *reason)
{
client_disconnect(client, prefix, reason);
o_stream_uncork(client->output);
clients_count--;
DLLIST_REMOVE(&clients, client);
client_state_set(client, "destroyed", "");
if (client->raw_mail_user != NULL)
mail_user_unref(&client->raw_mail_user);
if (client->proxy != NULL)
lmtp_proxy_deinit(&client->proxy);
io_remove(&client->io);
timeout_remove(&client->to_idle);
if (client->ssl_iostream != NULL)
ssl_iostream_destroy(&client->ssl_iostream);
i_stream_destroy(&client->input);
o_stream_destroy(&client->output);
fd_close_maybe_stdio(&client->fd_in, &client->fd_out);
client_state_reset(client, "destroyed");
i_free(client->lhlo);
pool_unref(&client->state_pool);
pool_unref(&client->pool);
master_service_client_connection_destroyed(master_service);
}
static const char *client_get_disconnect_reason(struct client *client)
{
const char *err;
if (client->ssl_iostream != NULL &&
!ssl_iostream_is_handshaked(client->ssl_iostream)) {
err = ssl_iostream_get_last_error(client->ssl_iostream);
if (err != NULL) {
return t_strdup_printf("TLS handshaking failed: %s",
err);
}
}
return io_stream_get_disconnect_reason(client->input, client->output);
}
void client_disconnect(struct client *client, const char *prefix,
const char *reason)
{
if (client->disconnected)
return;
if (reason != NULL)
client_send_line(client, "%s %s", prefix, reason);
else
reason = client_get_disconnect_reason(client);
i_info("Disconnect from %s: %s (in %s)", client_remote_id(client),
reason, client->state.name);
client->disconnected = TRUE;
}
bool client_is_trusted(struct client *client)
{
const char *const *net;
struct ip_addr net_ip;
unsigned int bits;
if (client->lmtp_set->login_trusted_networks == NULL)
return FALSE;
net = t_strsplit_spaces(client->lmtp_set->login_trusted_networks, ", ");
for (; *net != NULL; net++) {
if (net_parse_range(*net, &net_ip, &bits) < 0) {
i_error("login_trusted_networks: "
"Invalid network '%s'", *net);
break;
}
if (net_is_in_network(&client->remote_ip, &net_ip, bits))
return TRUE;
}
return FALSE;
}
void clients_destroy(void)
{
while (clients != NULL) {
client_destroy(clients,
t_strdup_printf("421 4.3.2 %s", clients->my_domain),
"Shutting down");
}
}
/*
* Input handling
*/
static int client_input_line(struct client *client, const char *line)
{
const char *cmd, *args;
args = strchr(line, ' ');
if (args == NULL) {
cmd = line;
args = "";
} else {
cmd = t_strdup_until(line, args);
args++;
}
cmd = t_str_ucase(cmd);
if (strcmp(cmd, "LHLO") == 0)
return cmd_lhlo(client, args);
if (strcmp(cmd, "STARTTLS") == 0 &&
master_service_ssl_is_enabled(master_service))
return cmd_starttls(client);
if (strcmp(cmd, "MAIL") == 0)
return cmd_mail(client, args);
if (strcmp(cmd, "RCPT") == 0)
return cmd_rcpt(client, args);
if (strcmp(cmd, "DATA") == 0)
return cmd_data(client, args);
if (strcmp(cmd, "QUIT") == 0)
return cmd_quit(client, args);
if (strcmp(cmd, "VRFY") == 0)
return cmd_vrfy(client, args);
if (strcmp(cmd, "RSET") == 0)
return cmd_rset(client, args);
if (strcmp(cmd, "NOOP") == 0)
return cmd_noop(client, args);
if (strcmp(cmd, "XCLIENT") == 0)
return cmd_xclient(client, args);
client_send_line(client, "502 5.5.2 Unknown command");
return 0;
}
int client_input_read(struct client *client)
{
client->last_input = ioloop_time;
timeout_reset(client->to_idle);
switch (i_stream_read(client->input)) {
case -2:
/* buffer full */
client_destroy(client, "502 5.5.2",
"Disconnected: Input buffer full");
return -1;
case -1:
/* disconnected */
client_destroy(client, NULL, NULL);
return -1;
case 0:
/* nothing new read */
return 0;
default:
/* something was read */
return 0;
}
}
void client_input_handle(struct client *client)
{
struct ostream *output;
const char *line;
int ret;
output = client->output;
o_stream_ref(output);
while ((line = i_stream_next_line(client->input)) != NULL) {
T_BEGIN {
o_stream_cork(output);
ret = client_input_line(client, line);
o_stream_uncork(output);
} T_END;
if (ret < 0)
break;
}
o_stream_unref(&output);
}
static void client_input(struct client *client)
{
if (client_input_read(client) < 0)
return;
client_input_handle(client);
}
void client_io_reset(struct client *client)
{
io_remove(&client->io);
timeout_remove(&client->to_idle);
client->io = io_add(client->fd_in, IO_READ, client_input, client);
client->last_input = ioloop_time;
client->to_idle = timeout_add(CLIENT_IDLE_TIMEOUT_MSECS,
client_idle_timeout, client);
}
/*
* Output handling
*/
void client_send_line(struct client *client, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
T_BEGIN {
string_t *str;
str = t_str_new(256);
str_vprintfa(str, fmt, args);
str_append(str, "\r\n");
o_stream_nsend(client->output, str_data(str), str_len(str));
} T_END;
va_end(args);
}