-
Notifications
You must be signed in to change notification settings - Fork 272
/
commands.c
1348 lines (1194 loc) · 36.9 KB
/
commands.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2009-2016 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "ioloop.h"
#include "array.h"
#include "str.h"
#include "strescape.h"
#include "hostpid.h"
#include "istream.h"
#include "istream-concat.h"
#include "ostream.h"
#include "istream-dot.h"
#include "safe-mkstemp.h"
#include "hex-dec.h"
#include "time-util.h"
#include "var-expand.h"
#include "restrict-access.h"
#include "settings-parser.h"
#include "anvil-client.h"
#include "master-service.h"
#include "master-service-ssl.h"
#include "iostream-ssl.h"
#include "rfc822-parser.h"
#include "message-date.h"
#include "auth-master.h"
#include "mail-storage-service.h"
#include "index/raw/raw-storage.h"
#include "lda-settings.h"
#include "lmtp-settings.h"
#include "mail-namespace.h"
#include "mail-deliver.h"
#include "main.h"
#include "client.h"
#include "commands.h"
#include "lmtp-proxy.h"
#define ERRSTR_TEMP_MAILBOX_FAIL "451 4.3.0 <%s> Temporary internal error"
#define ERRSTR_TEMP_USERDB_FAIL_PREFIX "451 4.3.0 <%s> "
#define ERRSTR_TEMP_USERDB_FAIL \
ERRSTR_TEMP_USERDB_FAIL_PREFIX "Temporary user lookup failure"
#define LMTP_PROXY_DEFAULT_TIMEOUT_MSECS (1000*125)
static void client_input_data_write(struct client *client);
int cmd_lhlo(struct client *client, const char *args)
{
struct rfc822_parser_context parser;
string_t *domain = t_str_new(128);
const char *p;
int ret = 0;
if (*args == '\0') {
client_send_line(client, "501 Missing hostname");
return 0;
}
/* domain / address-literal */
rfc822_parser_init(&parser, (const unsigned char *)args, strlen(args),
NULL);
if (*args != '[')
ret = rfc822_parse_dot_atom(&parser, domain);
else {
for (p = args+1; *p != ']'; p++) {
if (*p == '\\' || *p == '[')
break;
}
if (strcmp(p, "]") != 0)
ret = -1;
}
if (ret < 0) {
str_truncate(domain, 0);
str_append(domain, "invalid");
}
client_state_reset(client, "LHLO");
client_send_line(client, "250-%s", client->my_domain);
if (master_service_ssl_is_enabled(master_service) &&
client->ssl_iostream == NULL)
client_send_line(client, "250-STARTTLS");
if (client_is_trusted(client))
client_send_line(client, "250-XCLIENT ADDR PORT TTL TIMEOUT");
client_send_line(client, "250-8BITMIME");
client_send_line(client, "250-ENHANCEDSTATUSCODES");
client_send_line(client, "250 PIPELINING");
i_free(client->lhlo);
client->lhlo = i_strdup(str_c(domain));
client_state_set(client, "LHLO", "");
return 0;
}
int cmd_starttls(struct client *client)
{
struct ostream *plain_output = client->output;
const char *error;
if (client->ssl_iostream != NULL) {
o_stream_nsend_str(client->output,
"443 5.5.1 TLS is already active.\r\n");
return 0;
}
if (master_service_ssl_init(master_service,
&client->input, &client->output,
&client->ssl_iostream, &error) < 0) {
i_error("TLS initialization failed: %s", error);
o_stream_nsend_str(client->output,
"454 4.7.0 Internal error, TLS not available.\r\n");
return 0;
}
o_stream_nsend_str(plain_output,
"220 2.0.0 Begin TLS negotiation now.\r\n");
if (ssl_iostream_handshake(client->ssl_iostream) < 0) {
client_destroy(client, NULL, NULL);
return -1;
}
return 0;
}
static int parse_address(const char *str, const char **address_r,
const char **rest_r)
{
const char *start;
if (*str++ != '<')
return -1;
start = str;
if (*str == '"') {
/* "quoted-string"@domain */
for (str++; *str != '"'; str++) {
if (*str == '\\')
str++;
if (*str == '\0')
return -1;
}
str++;
}
for (; *str != '>'; str++) {
if (*str == '\0' || *str == ' ')
return -1;
}
*address_r = t_strdup_until(start, str);
if (*str++ != '>')
return -1;
if (*str == ' ')
str++;
else if (*str != '\0')
return -1;
*rest_r = str;
return 0;
}
static const char *
parse_xtext(struct client *client, const char *value)
{
const char *p;
string_t *str;
unsigned int i;
p = strchr(value, '+');
if (p == NULL)
return p_strdup(client->state_pool, value);
/*
hexchar = ASCII "+" immediately followed by two upper case
hexadecimal digits
*/
str = t_str_new(128);
for (i = 0; value[i] != '\0'; i++) {
if (value[i] == '+' && value[i+1] != '\0' && value[i+2] != '\0') {
str_append_c(str, hex2dec((const void *)(value+i+1), 2));
i += 2;
} else {
str_append_c(str, value[i]);
}
}
return p_strdup(client->state_pool, str_c(str));
}
static void lmtp_anvil_init(void)
{
if (anvil == NULL) {
const char *path = t_strdup_printf("%s/anvil", base_dir);
anvil = anvil_client_init(path, NULL, 0);
}
}
int cmd_mail(struct client *client, const char *args)
{
const char *addr, *const *argv;
if (client->state.mail_from != NULL) {
client_send_line(client, "503 5.5.1 MAIL already given");
return 0;
}
if (strncasecmp(args, "FROM:", 5) != 0 ||
parse_address(args + 5, &addr, &args) < 0) {
client_send_line(client, "501 5.5.4 Invalid parameters");
return 0;
}
argv = t_strsplit(args, " ");
for (; *argv != NULL; argv++) {
if (strcasecmp(*argv, "BODY=7BIT") == 0)
client->state.mail_body_7bit = TRUE;
else if (strcasecmp(*argv, "BODY=8BITMIME") == 0)
client->state.mail_body_8bitmime = TRUE;
else {
client_send_line(client,
"501 5.5.4 Unsupported options");
return 0;
}
}
client->state.mail_from = p_strdup(client->state_pool, addr);
p_array_init(&client->state.rcpt_to, client->state_pool, 64);
client_send_line(client, "250 2.1.0 OK");
client_state_set(client, "MAIL FROM", client->state.mail_from);
if (client->lmtp_set->lmtp_user_concurrency_limit > 0) {
/* connect to anvil before dropping privileges */
lmtp_anvil_init();
}
client->state.mail_from_timeval = ioloop_timeval;
return 0;
}
static bool
client_proxy_rcpt_parse_fields(struct lmtp_proxy_rcpt_settings *set,
const char *const *args, const char **address)
{
const char *p, *key, *value;
bool proxying = FALSE, port_set = FALSE;
for (; *args != NULL; args++) {
p = strchr(*args, '=');
if (p == NULL) {
key = *args;
value = "";
} else {
key = t_strdup_until(*args, p);
value = p + 1;
}
if (strcmp(key, "proxy") == 0)
proxying = TRUE;
else if (strcmp(key, "host") == 0)
set->host = value;
else if (strcmp(key, "port") == 0) {
if (net_str2port(value, &set->port) < 0) {
i_error("proxy: Invalid port number %s", value);
return FALSE;
}
port_set = TRUE;
} else if (strcmp(key, "proxy_timeout") == 0) {
if (str_to_uint(value, &set->timeout_msecs) < 0) {
i_error("proxy: Invalid proxy_timeout value %s", value);
return FALSE;
}
set->timeout_msecs *= 1000;
} else if (strcmp(key, "protocol") == 0) {
if (strcmp(value, "lmtp") == 0)
set->protocol = LMTP_CLIENT_PROTOCOL_LMTP;
else if (strcmp(value, "smtp") == 0) {
set->protocol = LMTP_CLIENT_PROTOCOL_SMTP;
if (!port_set)
set->port = 25;
} else {
i_error("proxy: Unknown protocol %s", value);
return FALSE;
}
} else if (strcmp(key, "user") == 0 ||
strcmp(key, "destuser") == 0) {
/* changing the username */
*address = value;
} else {
/* just ignore it */
}
}
if (proxying && set->host == NULL) {
i_error("proxy: host not given");
return FALSE;
}
return proxying;
}
static bool
client_proxy_is_ourself(const struct client *client,
const struct lmtp_proxy_rcpt_settings *set)
{
struct ip_addr ip;
if (set->port != client->local_port)
return FALSE;
if (net_addr2ip(set->host, &ip) < 0)
return FALSE;
if (!net_ip_compare(&ip, &client->local_ip))
return FALSE;
return TRUE;
}
static const char *
address_add_detail(const char *username, char delim_c,
const char *detail)
{
const char *domain;
const char delim[] = {delim_c, '\0'};
domain = strchr(username, '@');
if (domain == NULL)
return t_strconcat(username, delim, detail, NULL);
else {
username = t_strdup_until(username, domain);
return t_strconcat(username, delim, detail, domain, NULL);
}
}
static bool client_proxy_rcpt(struct client *client, const char *address,
const char *username, const char *detail, char delim,
const struct lmtp_recipient_params *params)
{
struct auth_master_connection *auth_conn;
struct lmtp_proxy_rcpt_settings set;
struct auth_user_info info;
struct mail_storage_service_input input;
const char *args, *const *fields, *errstr, *orig_username = username;
pool_t pool;
int ret;
memset(&input, 0, sizeof(input));
input.module = input.service = "lmtp";
mail_storage_service_init_settings(storage_service, &input);
memset(&info, 0, sizeof(info));
info.service = master_service_get_name(master_service);
info.local_ip = client->local_ip;
info.remote_ip = client->remote_ip;
info.local_port = client->local_port;
info.remote_port = client->remote_port;
pool = pool_alloconly_create("auth lookup", 1024);
auth_conn = mail_storage_service_get_auth_conn(storage_service);
ret = auth_master_pass_lookup(auth_conn, username, &info,
pool, &fields);
if (ret <= 0) {
errstr = ret < 0 && fields[0] != NULL ? t_strdup(fields[0]) :
t_strdup_printf(ERRSTR_TEMP_USERDB_FAIL, address);
pool_unref(&pool);
if (ret < 0) {
client_send_line(client, "%s", errstr);
return TRUE;
} else {
/* user not found from passdb. try userdb also. */
return FALSE;
}
}
memset(&set, 0, sizeof(set));
set.port = client->local_port;
set.protocol = LMTP_CLIENT_PROTOCOL_LMTP;
set.timeout_msecs = LMTP_PROXY_DEFAULT_TIMEOUT_MSECS;
set.params = *params;
if (!client_proxy_rcpt_parse_fields(&set, fields, &username)) {
/* not proxying this user */
pool_unref(&pool);
return FALSE;
}
if (strcmp(username, orig_username) != 0) {
/* username changed. change the address as well */
if (*detail == '\0')
address = username;
else
address = address_add_detail(username, delim, detail);
} else if (client_proxy_is_ourself(client, &set)) {
i_error("Proxying to <%s> loops to itself", username);
client_send_line(client, "554 5.4.6 <%s> "
"Proxying loops to itself", address);
pool_unref(&pool);
return TRUE;
}
if (client->proxy_ttl <= 1) {
i_error("Proxying to <%s> appears to be looping (TTL=0)",
username);
client_send_line(client, "554 5.4.6 <%s> "
"Proxying appears to be looping (TTL=0)",
username);
pool_unref(&pool);
return TRUE;
}
if (array_count(&client->state.rcpt_to) != 0) {
client_send_line(client, "451 4.3.0 <%s> "
"Can't handle mixed proxy/non-proxy destinations",
address);
pool_unref(&pool);
return TRUE;
}
if (client->proxy == NULL) {
struct lmtp_proxy_settings proxy_set;
memset(&proxy_set, 0, sizeof(proxy_set));
proxy_set.my_hostname = client->my_domain;
proxy_set.dns_client_socket_path = dns_client_socket_path;
proxy_set.session_id = client->state.session_id;
proxy_set.source_ip = client->remote_ip;
proxy_set.source_port = client->remote_port;
proxy_set.proxy_ttl = client->proxy_ttl-1;
client->proxy = lmtp_proxy_init(&proxy_set, client->output);
if (client->state.mail_body_8bitmime)
args = " BODY=8BITMIME";
else if (client->state.mail_body_7bit)
args = " BODY=7BIT";
else
args = "";
lmtp_proxy_mail_from(client->proxy, t_strdup_printf(
"<%s>%s", client->state.mail_from, args));
}
if (lmtp_proxy_add_rcpt(client->proxy, address, &set) < 0)
client_send_line(client, ERRSTR_TEMP_REMOTE_FAILURE);
else
client_send_line(client, "250 2.1.5 OK");
pool_unref(&pool);
return TRUE;
}
static const char *lmtp_unescape_address(const char *name)
{
string_t *str;
const char *p;
if (*name != '"')
return name;
/* quoted-string local-part. drop the quotes unless there's a
'@' character inside or there's an error. */
str = t_str_new(128);
for (p = name+1; *p != '"'; p++) {
if (*p == '\0')
return name;
if (*p == '\\') {
if (p[1] == '\0') {
/* error */
return name;
}
p++;
}
if (*p == '@')
return name;
str_append_c(str, *p);
}
p++;
if (*p != '@' && *p != '\0')
return name;
str_append(str, p);
return str_c(str);
}
static void rcpt_address_parse(struct client *client, const char *address,
const char **username_r, char *delim_r,
const char **detail_r)
{
const char *p, *domain;
size_t idx;
*username_r = address;
*detail_r = "";
if (*client->unexpanded_lda_set->recipient_delimiter == '\0')
return;
domain = strchr(address, '@');
/* first character that matches the recipient_delimiter */
idx = strcspn(address, client->unexpanded_lda_set->recipient_delimiter);
p = address[idx] != '\0' ? address + idx : NULL;
if (p != NULL && (domain == NULL || p < domain)) {
*delim_r = *p;
/* user+detail@domain */
*username_r = t_strdup_until(*username_r, p);
if (domain == NULL)
*detail_r = p+1;
else {
*detail_r = t_strdup_until(p+1, domain);
*username_r = t_strconcat(*username_r, domain, NULL);
}
}
}
static void lmtp_address_translate(struct client *client, const char **address)
{
const char *transpos = client->lmtp_set->lmtp_address_translate;
const char *p, *nextstr, *addrpos = *address;
unsigned int len;
string_t *username, *domain, *dest = NULL;
if (*transpos == '\0')
return;
username = t_str_new(64);
domain = t_str_new(64);
/* check that string matches up to the first '%' */
p = strchr(transpos, '%');
if (p == NULL)
len = strlen(transpos);
else
len = p-transpos;
if (strncmp(transpos, addrpos, len) != 0)
return;
transpos += len;
addrpos += len;
while (*transpos != '\0') {
switch (transpos[1]) {
case 'n':
case 'u':
dest = username;
break;
case 'd':
dest = domain;
break;
default:
return;
}
transpos += 2;
/* find where the next string starts */
if (*transpos == '\0') {
str_append(dest, addrpos);
break;
}
p = strchr(transpos, '%');
if (p == NULL)
nextstr = transpos;
else
nextstr = t_strdup_until(transpos, p);
p = strstr(addrpos, nextstr);
if (p == NULL)
return;
str_append_n(dest, addrpos, p-addrpos);
len = strlen(nextstr);
transpos += len;
addrpos = p + len;
}
str_append_c(username, '@');
if (domain != NULL)
str_append_str(username, domain);
*address = str_c(username);
}
static void
client_send_line_overquota(struct client *client,
const struct mail_recipient *rcpt, const char *error)
{
struct lda_settings *lda_set =
mail_storage_service_user_get_set(rcpt->service_user)[1];
client_send_line(client, "%s <%s> %s", lda_set->quota_full_tempfail ?
"452 4.2.2" : "552 5.2.2", rcpt->address, error);
}
static int
lmtp_rcpt_to_is_over_quota(struct client *client,
const struct mail_recipient *rcpt)
{
struct mail_user *user;
struct mail_namespace *ns;
struct mailbox *box;
struct mailbox_status status;
const char *errstr;
enum mail_error error;
int ret;
if (!client->lmtp_set->lmtp_rcpt_check_quota)
return 0;
ret = mail_storage_service_next(storage_service,
rcpt->service_user, &user);
if (ret < 0)
return -1;
ns = mail_namespace_find_inbox(user->namespaces);
box = mailbox_alloc(ns->list, "INBOX", 0);
ret = mailbox_get_status(box, STATUS_CHECK_OVER_QUOTA, &status);
if (ret < 0) {
errstr = mailbox_get_last_error(box, &error);
if (error == MAIL_ERROR_NOQUOTA) {
client_send_line_overquota(client, rcpt, errstr);
ret = 1;
}
}
mailbox_free(&box);
mail_user_unref(&user);
return ret;
}
static void rcpt_anvil_lookup_callback(const char *reply, void *context)
{
struct mail_recipient *rcpt = context;
i_assert(rcpt->client->state.anvil_queries > 0);
rcpt->anvil_query = NULL;
if (reply == NULL) {
/* lookup failed */
} else if (str_to_uint(reply, &rcpt->parallel_count) < 0) {
i_error("Invalid reply from anvil: %s", reply);
}
if (--rcpt->client->state.anvil_queries == 0 &&
rcpt->client->state.anvil_pending_data_write) {
/* DATA command was finished, but we were still waiting on
anvil before handling any users */
client_input_data_write(rcpt->client);
}
}
int cmd_rcpt(struct client *client, const char *args)
{
struct mail_recipient *rcpt;
struct mail_storage_service_input input;
const char *params, *address, *username, *detail, *prefix;
const char *const *argv;
const char *error = NULL;
char delim = '\0';
int ret = 0;
if (client->state.mail_from == NULL) {
client_send_line(client, "503 5.5.1 MAIL needed first");
return 0;
}
if (strncasecmp(args, "TO:", 3) != 0 ||
parse_address(args + 3, &address, ¶ms) < 0) {
client_send_line(client, "501 5.5.4 Invalid parameters");
return 0;
}
rcpt = p_new(client->state_pool, struct mail_recipient, 1);
rcpt->client = client;
address = lmtp_unescape_address(address);
argv = t_strsplit(params, " ");
for (; *argv != NULL; argv++) {
if (strncasecmp(*argv, "ORCPT=", 6) == 0) {
rcpt->params.dsn_orcpt = parse_xtext(client, *argv + 6);
} else {
client_send_line(client, "501 5.5.4 Unsupported options");
return 0;
}
}
rcpt_address_parse(client, address, &username, &delim, &detail);
client_state_set(client, "RCPT TO", address);
if (client->lmtp_set->lmtp_proxy) {
if (client_proxy_rcpt(client, address, username, detail, delim,
&rcpt->params))
return 0;
}
/* Use a unique session_id for each mail delivery. This is especially
important for stats process to not see duplicate sessions. */
if (array_count(&client->state.rcpt_to) == 0)
rcpt->session_id = client->state.session_id;
else {
rcpt->session_id =
p_strdup_printf(client->state_pool, "%s:%u",
client->state.session_id,
array_count(&client->state.rcpt_to)+1);
}
memset(&input, 0, sizeof(input));
input.module = input.service = "lmtp";
input.username = username;
input.local_ip = client->local_ip;
input.remote_ip = client->remote_ip;
input.local_port = client->local_port;
input.remote_port = client->remote_port;
input.session_id = rcpt->session_id;
ret = mail_storage_service_lookup(storage_service, &input,
&rcpt->service_user, &error);
if (ret < 0) {
prefix = t_strdup_printf(ERRSTR_TEMP_USERDB_FAIL_PREFIX,
username);
client_send_line(client, "%s%s", prefix, error);
return 0;
}
if (ret == 0) {
client_send_line(client,
"550 5.1.1 <%s> User doesn't exist: %s",
address, username);
return 0;
}
if (client->proxy != NULL) {
/* NOTE: if this restriction is ever removed, we'll also need
to send different message bodies to local and proxy
(with and without Return-Path: header) */
client_send_line(client, "451 4.3.0 <%s> "
"Can't handle mixed proxy/non-proxy destinations",
address);
mail_storage_service_user_free(&rcpt->service_user);
return 0;
}
lmtp_address_translate(client, &address);
rcpt->address = p_strdup(client->state_pool, address);
rcpt->detail = p_strdup(client->state_pool, detail);
if ((ret = lmtp_rcpt_to_is_over_quota(client, rcpt)) != 0) {
if (ret < 0) {
client_send_line(client, ERRSTR_TEMP_MAILBOX_FAIL,
rcpt->address);
}
mail_storage_service_user_free(&rcpt->service_user);
return 0;
}
array_append(&client->state.rcpt_to, &rcpt, 1);
client_send_line(client, "250 2.1.5 OK");
if (client->lmtp_set->lmtp_user_concurrency_limit > 0) {
const char *query = t_strconcat("LOOKUP\t",
master_service_get_name(master_service),
"/", str_tabescape(username), NULL);
client->state.anvil_queries++;
rcpt->anvil_query = anvil_client_query(anvil, query,
rcpt_anvil_lookup_callback, rcpt);
}
return 0;
}
int cmd_quit(struct client *client, const char *args ATTR_UNUSED)
{
client_send_line(client, "221 2.0.0 OK");
/* don't log the (state name) for successful QUITs */
i_info("Disconnect from %s: Successful quit", client_remote_id(client));
client->disconnected = TRUE;
client_destroy(client, NULL, NULL);
return -1;
}
int cmd_vrfy(struct client *client, const char *args ATTR_UNUSED)
{
client_send_line(client, "252 2.3.3 Try RCPT instead");
return 0;
}
int cmd_rset(struct client *client, const char *args ATTR_UNUSED)
{
client_state_reset(client, "RSET");
client_send_line(client, "250 2.0.0 OK");
return 0;
}
int cmd_noop(struct client *client, const char *args ATTR_UNUSED)
{
client_send_line(client, "250 2.0.0 OK");
return 0;
}
static bool orcpt_get_valid_rfc822(const char *orcpt, const char **addr_r)
{
if (orcpt == NULL || strncasecmp(orcpt, "rfc822;", 7) != 0)
return FALSE;
/* FIXME: we should verify the address further */
*addr_r = orcpt + 7;
return TRUE;
}
static int
client_deliver(struct client *client, const struct mail_recipient *rcpt,
struct mail *src_mail, struct mail_deliver_session *session)
{
struct mail_deliver_context dctx;
struct mail_storage *storage;
const struct mail_storage_service_input *input;
const struct mail_storage_settings *mail_set;
struct lda_settings *lda_set;
struct mail_namespace *ns;
struct setting_parser_context *set_parser;
struct timeval delivery_time_started;
void **sets;
const char *line, *error, *username;
string_t *str;
enum mail_error mail_error;
int ret;
i_assert(client->state.anvil_queries == 0);
input = mail_storage_service_user_get_input(rcpt->service_user);
username = t_strdup(input->username);
if (client->lmtp_set->lmtp_user_concurrency_limit > 0 &&
rcpt->parallel_count >= client->lmtp_set->lmtp_user_concurrency_limit) {
client_send_line(client, ERRSTR_TEMP_USERDB_FAIL_PREFIX
"Too many concurrent deliveries for user",
rcpt->address);
return -1;
}
mail_set = mail_storage_service_user_get_mail_set(rcpt->service_user);
set_parser = mail_storage_service_user_get_settings_parser(rcpt->service_user);
if (client->proxy_timeout_secs > 0 &&
(mail_set->mail_max_lock_timeout == 0 ||
mail_set->mail_max_lock_timeout > client->proxy_timeout_secs)) {
/* set lock timeout waits to be less than when proxy has
advertised that it's going to timeout the connection.
this avoids duplicate deliveries in case the delivery
succeeds after the proxy has already disconnected from us. */
line = t_strdup_printf("mail_max_lock_timeout=%u",
client->proxy_timeout_secs <= 1 ? 1 :
client->proxy_timeout_secs-1);
if (settings_parse_line(set_parser, line) < 0)
i_unreached();
}
/* get the timestamp before user is created, since it starts the I/O */
io_loop_time_refresh();
delivery_time_started = ioloop_timeval;
client_state_set(client, "DATA", username);
i_set_failure_prefix("lmtp(%s, %s): ", my_pid, username);
if (mail_storage_service_next(storage_service, rcpt->service_user,
&client->state.dest_user) < 0) {
client_send_line(client, ERRSTR_TEMP_MAILBOX_FAIL,
rcpt->address);
return -1;
}
str = t_str_new(256);
var_expand(str, client->state.dest_user->set->mail_log_prefix,
mail_user_var_expand_table(client->state.dest_user));
i_set_failure_prefix("%s", str_c(str));
sets = mail_storage_service_user_get_set(rcpt->service_user);
lda_set = sets[1];
settings_var_expand(&lda_setting_parser_info, lda_set, client->pool,
mail_user_var_expand_table(client->state.dest_user));
memset(&dctx, 0, sizeof(dctx));
dctx.session = session;
dctx.pool = session->pool;
dctx.set = lda_set;
dctx.timeout_secs = LDA_SUBMISSION_TIMEOUT_SECS;
dctx.session_id = rcpt->session_id;
dctx.src_mail = src_mail;
dctx.src_envelope_sender = client->state.mail_from;
dctx.dest_user = client->state.dest_user;
dctx.session_time_msecs =
timeval_diff_msecs(&client->state.data_end_timeval,
&client->state.mail_from_timeval);
dctx.delivery_time_started = delivery_time_started;
if (orcpt_get_valid_rfc822(rcpt->params.dsn_orcpt, &dctx.dest_addr)) {
/* used ORCPT */
} else if (*dctx.set->lda_original_recipient_header != '\0') {
dctx.dest_addr = mail_deliver_get_address(src_mail,
dctx.set->lda_original_recipient_header);
}
if (dctx.dest_addr == NULL)
dctx.dest_addr = rcpt->address;
dctx.final_dest_addr = rcpt->address;
if (*rcpt->detail == '\0' ||
!client->lmtp_set->lmtp_save_to_detail_mailbox)
dctx.dest_mailbox_name = "INBOX";
else {
ns = mail_namespace_find_inbox(dctx.dest_user->namespaces);
dctx.dest_mailbox_name =
t_strconcat(ns->prefix, rcpt->detail, NULL);
}
dctx.save_dest_mail = array_count(&client->state.rcpt_to) > 1 &&
client->state.first_saved_mail == NULL;
if (client->lmtp_set->lmtp_user_concurrency_limit > 0) {
master_service_anvil_send(master_service, t_strconcat(
"CONNECT\t", my_pid, "\t", master_service_get_name(master_service),
"/", username, "\n", NULL));
}
if (mail_deliver(&dctx, &storage) == 0) {
if (dctx.dest_mail != NULL) {
i_assert(client->state.first_saved_mail == NULL);
client->state.first_saved_mail = dctx.dest_mail;
}
client_send_line(client, "250 2.0.0 <%s> %s Saved",
rcpt->address, rcpt->session_id);
ret = 0;
} else if (dctx.tempfail_error != NULL) {
client_send_line(client, "451 4.2.0 <%s> %s",
rcpt->address, dctx.tempfail_error);
ret = -1;
} else if (storage != NULL) {
error = mail_storage_get_last_error(storage, &mail_error);
if (mail_error == MAIL_ERROR_NOQUOTA) {
client_send_line_overquota(client, rcpt, error);
} else {
client_send_line(client, "451 4.2.0 <%s> %s",
rcpt->address, error);
}
ret = -1;
} else {
/* This shouldn't happen */
i_error("BUG: Saving failed to unknown storage");
client_send_line(client, ERRSTR_TEMP_MAILBOX_FAIL,
rcpt->address);
ret = -1;
}
if (client->lmtp_set->lmtp_user_concurrency_limit > 0) {
master_service_anvil_send(master_service, t_strconcat(
"DISCONNECT\t", my_pid, "\t", master_service_get_name(master_service),
"/", username, "\n", NULL));
}
return ret;
}
static bool client_deliver_next(struct client *client, struct mail *src_mail,
struct mail_deliver_session *session)
{
struct mail_recipient *const *rcpts;
unsigned int count;
int ret;
rcpts = array_get(&client->state.rcpt_to, &count);
while (client->state.rcpt_idx < count) {
ret = client_deliver(client, rcpts[client->state.rcpt_idx],
src_mail, session);
client_state_set(client, "DATA", "");
i_set_failure_prefix("lmtp(%s): ", my_pid);
client->state.rcpt_idx++;
if (ret == 0)
return TRUE;
/* failed. try the next one. */
if (client->state.dest_user != NULL)
mail_user_unref(&client->state.dest_user);
}
return FALSE;
}
static void client_rcpt_fail_all(struct client *client)
{
struct mail_recipient *const *rcptp;
array_foreach(&client->state.rcpt_to, rcptp) {
client_send_line(client, ERRSTR_TEMP_MAILBOX_FAIL,
(*rcptp)->address);
}
}
static struct istream *client_get_input(struct client *client)
{
struct client_state *state = &client->state;
struct istream *cinput, *inputs[3];
inputs[0] = i_stream_create_from_data(state->added_headers,
strlen(state->added_headers));
if (state->mail_data_output != NULL) {
o_stream_unref(&state->mail_data_output);
inputs[1] = i_stream_create_fd(state->mail_data_fd,
MAIL_READ_FULL_BLOCK_SIZE,
FALSE);
i_stream_set_init_buffer_size(inputs[1],
MAIL_READ_FULL_BLOCK_SIZE);
} else {
inputs[1] = i_stream_create_from_data(state->mail_data->data,
state->mail_data->used);
}
inputs[2] = NULL;
cinput = i_stream_create_concat(inputs);
i_stream_set_name(cinput, "<lmtp DATA>");
i_stream_unref(&inputs[0]);
i_stream_unref(&inputs[1]);
return cinput;
}
static int client_open_raw_mail(struct client *client, struct istream *input)
{
static const char *wanted_headers[] = {
"From", "To", "Message-ID", "Subject", "Return-Path",
NULL
};
struct mailbox *box;
struct mailbox_transaction_context *trans;
struct mailbox_header_lookup_ctx *headers_ctx;
enum mail_error error;
if (raw_mailbox_alloc_stream(client->raw_mail_user, input,
(time_t)-1, client->state.mail_from,
&box) < 0) {