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
|
/* $OpenBSD: smtp_session.c,v 1.13 2008/11/25 20:35:54 gilles Exp $ */
/*
* Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org>
* Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.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 <sys/types.h>
#include <sys/queue.h>
#include <sys/tree.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <event.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "smtpd.h"
int session_rfc5321_helo_handler(struct session *, char *);
int session_rfc5321_ehlo_handler(struct session *, char *);
int session_rfc5321_rset_handler(struct session *, char *);
int session_rfc5321_noop_handler(struct session *, char *);
int session_rfc5321_data_handler(struct session *, char *);
int session_rfc5321_mail_handler(struct session *, char *);
int session_rfc5321_rcpt_handler(struct session *, char *);
int session_rfc5321_vrfy_handler(struct session *, char *);
int session_rfc5321_expn_handler(struct session *, char *);
int session_rfc5321_turn_handler(struct session *, char *);
int session_rfc5321_help_handler(struct session *, char *);
int session_rfc5321_quit_handler(struct session *, char *);
int session_rfc5321_none_handler(struct session *, char *);
int session_rfc1652_mail_handler(struct session *, char *);
int session_rfc3207_stls_handler(struct session *, char *);
int session_rfc4954_auth_handler(struct session *, char *);
void session_command(struct session *, char *, char *);
int session_set_path(struct path *, char *);
void session_timeout(int, short, void *);
struct session_timeout {
enum session_state state;
time_t timeout;
};
struct session_timeout rfc5321_timeouttab[] = {
{ S_INIT, 300 },
{ S_GREETED, 300 },
{ S_HELO, 300 },
{ S_MAIL, 300 },
{ S_RCPT, 300 },
{ S_DATA, 120 },
{ S_DATACONTENT, 180 },
{ S_DONE, 600 }
};
struct session_cmd {
char *name;
int (*func)(struct session *, char *);
};
struct session_cmd rfc5321_cmdtab[] = {
{ "helo", session_rfc5321_helo_handler },
{ "ehlo", session_rfc5321_ehlo_handler },
{ "rset", session_rfc5321_rset_handler },
{ "noop", session_rfc5321_noop_handler },
{ "data", session_rfc5321_data_handler },
{ "mail from", session_rfc5321_mail_handler },
{ "rcpt to", session_rfc5321_rcpt_handler },
{ "vrfy", session_rfc5321_vrfy_handler },
{ "expn", session_rfc5321_expn_handler },
{ "turn", session_rfc5321_turn_handler },
{ "help", session_rfc5321_help_handler },
{ "quit", session_rfc5321_quit_handler }
};
struct session_cmd rfc1652_cmdtab[] = {
{ "mail from", session_rfc1652_mail_handler },
};
struct session_cmd rfc3207_cmdtab[] = {
{ "starttls", session_rfc3207_stls_handler }
};
struct session_cmd rfc4954_cmdtab[] = {
{ "auth", session_rfc4954_auth_handler }
};
int
session_rfc3207_stls_handler(struct session *s, char *args)
{
if (s->s_state == S_GREETED) {
evbuffer_add_printf(s->s_bev->output,
"503 Polite people say HELO first\r\n");
return 1;
}
if (args != NULL) {
evbuffer_add_printf(s->s_bev->output,
"501 Syntax error (no parameters allowed)\r\n");
return 1;
}
evbuffer_add_printf(s->s_bev->output,
"220 Ready to start TLS\r\n");
s->s_state = S_TLS;
return 1;
}
int
session_rfc4954_auth_handler(struct session *s, char *args)
{
char *method;
char *eom;
struct session_auth_req req;
if (s->s_state == S_GREETED) {
evbuffer_add_printf(s->s_bev->output,
"503 Polite people say HELO first\r\n");
return 1;
}
if (args == NULL) {
evbuffer_add_printf(s->s_bev->output,
"501 Syntax error (no parameters given)\r\n");
return 1;
}
method = args;
eom = strchr(args, ' ');
if (eom == NULL)
eom = strchr(args, '\t');
if (eom != NULL)
*eom++ = '\0';
if (eom == NULL) {
/* NEEDS_FIX - unsupported yet */
evbuffer_add_printf(s->s_bev->output,
"501 Syntax error\r\n");
return 1;
}
req.session_id = s->s_id;
if (strlcpy(req.buffer, eom, sizeof(req.buffer)) >=
sizeof(req.buffer)) {
evbuffer_add_printf(s->s_bev->output,
"501 Syntax error\r\n");
return 1;
}
s->s_state = S_AUTH;
imsg_compose(s->s_env->sc_ibufs[PROC_PARENT], IMSG_PARENT_AUTHENTICATE,
0, 0, -1, &req, sizeof(req));
return 1;
}
int
session_rfc1652_mail_handler(struct session *s, char *args)
{
char *body;
if (s->s_state == S_GREETED) {
evbuffer_add_printf(s->s_bev->output,
"503 Polite people say HELO first\r\n");
return 1;
}
body = strrchr(args, ' ');
if (body != NULL) {
*body++ = '\0';
if (strcasecmp("body=7bit", body) == 0) {
s->s_flags &= ~F_8BITMIME;
}
else if (strcasecmp("body=8bitmime", body) != 0) {
evbuffer_add_printf(s->s_bev->output,
"503 Invalid BODY\r\n");
return 1;
}
return session_rfc5321_mail_handler(s, args);
}
return 0;
}
int
session_rfc5321_helo_handler(struct session *s, char *args)
{
void *p;
char addrbuf[INET6_ADDRSTRLEN];
if (args == NULL) {
evbuffer_add_printf(s->s_bev->output,
"501 HELO requires domain address.\r\n");
return 1;
}
if (strlcpy(s->s_msg.session_helo, args, sizeof(s->s_msg.session_helo))
>= sizeof(s->s_msg.session_helo)) {
evbuffer_add_printf(s->s_bev->output,
"501 Invalid domain name\r\n");
return 1;
}
s->s_state = S_HELO;
s->s_flags = 0;
if (s->s_ss.ss_family == PF_INET) {
struct sockaddr_in *ssin = (struct sockaddr_in *)&s->s_ss;
p = &ssin->sin_addr.s_addr;
}
if (s->s_ss.ss_family == PF_INET6) {
struct sockaddr_in6 *ssin6 = (struct sockaddr_in6 *)&s->s_ss;
p = &ssin6->sin6_addr.s6_addr;
}
bzero(addrbuf, sizeof (addrbuf));
inet_ntop(s->s_ss.ss_family, p, addrbuf, sizeof (addrbuf));
evbuffer_add_printf(s->s_bev->output,
"250 %s Hello %s [%s%s], pleased to meet you\r\n",
s->s_env->sc_hostname, args,
s->s_ss.ss_family == PF_INET ? "" : "IPv6:", addrbuf);
return 1;
}
int
session_rfc5321_ehlo_handler(struct session *s, char *args)
{
void *p;
char addrbuf[INET6_ADDRSTRLEN];
if (args == NULL) {
evbuffer_add_printf(s->s_bev->output,
"501 EHLO requires domain address.\r\n");
return 1;
}
if (strlcpy(s->s_msg.session_helo, args, sizeof(s->s_msg.session_helo))
>= sizeof(s->s_msg.session_helo)) {
evbuffer_add_printf(s->s_bev->output,
"501 Invalid domain name\r\n");
return 1;
}
s->s_state = S_HELO;
s->s_flags = F_EHLO;
s->s_flags |= F_8BITMIME;
if (s->s_ss.ss_family == PF_INET) {
struct sockaddr_in *ssin = (struct sockaddr_in *)&s->s_ss;
p = &ssin->sin_addr.s_addr;
}
if (s->s_ss.ss_family == PF_INET6) {
struct sockaddr_in6 *ssin6 = (struct sockaddr_in6 *)&s->s_ss;
p = &ssin6->sin6_addr.s6_addr;
}
bzero(addrbuf, sizeof (addrbuf));
inet_ntop(s->s_ss.ss_family, p, addrbuf, sizeof (addrbuf));
evbuffer_add_printf(s->s_bev->output,
"250-%s Hello %s [%s%s], pleased to meet you\r\n",
s->s_env->sc_hostname, args,
s->s_ss.ss_family == PF_INET ? "" : "IPv6:", addrbuf);
evbuffer_add_printf(s->s_bev->output, "250-8BITMIME\r\n");
/* only advertise starttls if listener can support it */
if (s->s_l->flags & F_STARTTLS)
evbuffer_add_printf(s->s_bev->output, "250-STARTTLS\r\n");
/* only advertise auth if session is secure */
/*
if (s->s_flags & F_SECURE)
evbuffer_add_printf(s->s_bev->output, "250-AUTH %s\r\n", "PLAIN");
*/
evbuffer_add_printf(s->s_bev->output, "250 HELP\r\n");
return 1;
}
int
session_rfc5321_rset_handler(struct session *s, char *args)
{
s->s_msg.rcptcount = 0;
s->s_state = S_HELO;
evbuffer_add_printf(s->s_bev->output, "250 Reset state.\r\n");
return 1;
}
int
session_rfc5321_noop_handler(struct session *s, char *args)
{
evbuffer_add_printf(s->s_bev->output, "250 OK.\r\n");
return 1;
}
int
session_rfc5321_mail_handler(struct session *s, char *args)
{
char buffer[MAX_PATH_SIZE];
if (s->s_state == S_GREETED) {
evbuffer_add_printf(s->s_bev->output,
"503 Polite people say HELO first\r\n");
return 1;
}
if (strlcpy(buffer, args, sizeof(buffer)) >= sizeof(buffer)) {
evbuffer_add_printf(s->s_bev->output,
"553 Syntax error for sender address\r\n");
return 1;
}
if (! session_set_path(&s->s_msg.sender, buffer)) {
/* No need to even transmit to MFA, path is invalid */
evbuffer_add_printf(s->s_bev->output,
"553 Syntax error for sender address\r\n");
return 1;
}
s->s_msg.rcptcount = 0;
s->s_state = S_MAILREQUEST;
s->s_msg.id = s->s_id;
s->s_msg.session_id = s->s_id;
s->s_msg.session_ss = s->s_ss;
log_debug("session_mail_handler: sending notification to mfa");
imsg_compose(s->s_env->sc_ibufs[PROC_MFA], IMSG_MFA_RPATH_SUBMIT,
0, 0, -1, &s->s_msg, sizeof(s->s_msg));
return 1;
}
int
session_rfc5321_rcpt_handler(struct session *s, char *args)
{
char buffer[MAX_PATH_SIZE];
struct message_recipient mr;
if (s->s_state == S_GREETED) {
evbuffer_add_printf(s->s_bev->output,
"503 Polite people say HELO first\r\n");
return 1;
}
if (s->s_state == S_HELO) {
evbuffer_add_printf(s->s_bev->output,
"503 Need MAIL before RCPT\r\n");
return 1;
}
bzero(&mr, sizeof(mr));
if (strlcpy(buffer, args, sizeof(buffer)) >= sizeof(buffer)) {
evbuffer_add_printf(s->s_bev->output,
"553 Syntax error for recipient address\r\n");
return 1;
}
if (! session_set_path(&mr.path, buffer)) {
/* No need to even transmit to MFA, path is invalid */
evbuffer_add_printf(s->s_bev->output,
"553 Syntax error for recipient address\r\n");
return 1;
}
mr.id = s->s_msg.id;
s->s_state = S_RCPTREQUEST;
mr.ss = s->s_ss;
imsg_compose(s->s_env->sc_ibufs[PROC_MFA], IMSG_MFA_RCPT_SUBMIT,
0, 0, -1, &mr, sizeof(mr));
return 1;
}
int
session_rfc5321_quit_handler(struct session *s, char *args)
{
evbuffer_add_printf(s->s_bev->output, "221 %s Closing connection.\r\n",
s->s_env->sc_hostname);
s->s_flags |= F_QUIT;
return 1;
}
int
session_rfc5321_data_handler(struct session *s, char *args)
{
if (s->s_state == S_GREETED) {
evbuffer_add_printf(s->s_bev->output,
"503 Polite people say HELO first\r\n");
return 1;
}
if (s->s_state == S_HELO) {
evbuffer_add_printf(s->s_bev->output,
"503 Need MAIL before DATA\r\n");
return 1;
}
if (s->s_state == S_MAIL) {
evbuffer_add_printf(s->s_bev->output,
"503 Need RCPT before DATA\r\n");
return 1;
}
s->s_state = S_DATA;
session_pickup(s, NULL);
return 1;
}
int
session_rfc5321_vrfy_handler(struct session *s, char *args)
{
evbuffer_add_printf(s->s_bev->output,
"252 Cannot VRFY user; try RCPT to attempt delivery.\r\n");
return 1;
}
int
session_rfc5321_expn_handler(struct session *s, char *args)
{
evbuffer_add_printf(s->s_bev->output,
"502 Sorry, we do not allow this operation.\r\n");
return 1;
}
int
session_rfc5321_turn_handler(struct session *s, char *args)
{
evbuffer_add_printf(s->s_bev->output,
"502 Sorry, we do not allow this operation.\r\n");
return 1;
}
int
session_rfc5321_help_handler(struct session *s, char *args)
{
evbuffer_add_printf(s->s_bev->output,
"214- This is OpenSMTPD\r\n"
"214- To report bugs in the implementation, please contact\r\n"
"214- bugs@openbsd.org with full details\r\n");
evbuffer_add_printf(s->s_bev->output,
"214 End of HELP info\r\n");
return 1;
}
void
session_command(struct session *s, char *cmd, char *args)
{
int i;
if (!(s->s_flags & F_EHLO))
goto rfc5321;
/* RFC 1652 - 8BITMIME */
for (i = 0; i < (int)(sizeof(rfc1652_cmdtab) / sizeof(struct session_cmd)); ++i)
if (strcasecmp(rfc1652_cmdtab[i].name, cmd) == 0)
break;
if (i < (int)(sizeof(rfc1652_cmdtab) / sizeof(struct session_cmd))) {
if (rfc1652_cmdtab[i].func(s, args))
return;
}
/* RFC 3207 - STARTTLS */
for (i = 0; i < (int)(sizeof(rfc3207_cmdtab) / sizeof(struct session_cmd)); ++i)
if (strcasecmp(rfc3207_cmdtab[i].name, cmd) == 0)
break;
if (i < (int)(sizeof(rfc3207_cmdtab) / sizeof(struct session_cmd))) {
if (rfc3207_cmdtab[i].func(s, args))
return;
}
/* RFC 4954 - AUTH */
/*
for (i = 0; i < (int)(sizeof(rfc4954_cmdtab) / sizeof(struct session_cmd)); ++i)
if (strcasecmp(rfc4954_cmdtab[i].name, cmd) == 0)
break;
if (i < (int)(sizeof(rfc4954_cmdtab) / sizeof(struct session_cmd))) {
if (rfc4954_cmdtab[i].func(s, args))
return;
}
*/
rfc5321:
/* RFC 5321 - SMTP */
for (i = 0; i < (int)(sizeof(rfc5321_cmdtab) / sizeof(struct session_cmd)); ++i)
if (strcasecmp(rfc5321_cmdtab[i].name, cmd) == 0)
break;
if (i < (int)(sizeof(rfc5321_cmdtab) / sizeof(struct session_cmd))) {
if (rfc5321_cmdtab[i].func(s, args))
return;
}
evbuffer_add_printf(s->s_bev->output,
"500 Command unrecognized.\r\n");
}
void
session_pickup(struct session *s, struct submit_status *ss)
{
if (s == NULL)
fatal("session_pickup: desynchronized");
bufferevent_disable(s->s_bev, EV_READ);
bufferevent_enable(s->s_bev, EV_WRITE);
switch (s->s_state) {
case S_INIT:
s->s_state = S_GREETED;
log_debug("session_pickup: greeting client");
evbuffer_add_printf(s->s_bev->output,
SMTPD_BANNER, s->s_env->sc_hostname);
break;
case S_GREETED:
case S_HELO:
break;
case S_TLS:
s->s_state = S_GREETED;
ssl_session_init(s);
bufferevent_disable(s->s_bev, EV_READ|EV_WRITE);
break;
case S_AUTH:
if (s->s_flags & F_AUTHENTICATED) {
evbuffer_add_printf(s->s_bev->output,
"235 Authentication Succeeded\r\n");
}
else {
evbuffer_add_printf(s->s_bev->output,
"535 Authentication Credentials Invalid\r\n");
}
break;
case S_MAILREQUEST:
/* sender was not accepted, downgrade state */
if (ss->code != 250) {
s->s_state = S_HELO;
evbuffer_add_printf(s->s_bev->output,
"%d Sender rejected\r\n", ss->code);
return;
}
s->s_state = S_MAIL;
s->s_msg.sender = ss->u.path;
imsg_compose(s->s_env->sc_ibufs[PROC_QUEUE],
IMSG_QUEUE_CREATE_MESSAGE_FILE, 0, 0, -1, &s->s_msg,
sizeof(s->s_msg));
break;
case S_MAIL:
evbuffer_add_printf(s->s_bev->output,
"%d Sender ok\r\n", ss->code);
strlcpy(s->s_msg.message_id, ss->u.msgid, MAXPATHLEN);
if (s->s_msg.datafp == NULL) {
/* Remove message file */
imsg_compose(s->s_env->sc_ibufs[PROC_QUEUE], IMSG_QUEUE_DELETE_MESSAGE_FILE,
0, 0, -1, &s->s_msg, sizeof(s->s_msg));
return;
}
break;
case S_RCPTREQUEST:
/* recipient was not accepted */
if (ss->code != 250) {
/* We do not have a valid recipient, downgrade state */
if (s->s_msg.rcptcount == 0)
s->s_state = S_MAIL;
else
s->s_state = S_RCPT;
evbuffer_add_printf(s->s_bev->output,
"%d %s\r\n", ss->code, "Recipient rejected");
return;
}
s->s_state = S_RCPT;
s->s_msg.rcptcount++;
s->s_msg.recipient = ss->u.path;
imsg_compose(s->s_env->sc_ibufs[PROC_QUEUE],
IMSG_QUEUE_MESSAGE_SUBMIT, 0, 0, -1, &s->s_msg,
sizeof(s->s_msg));
break;
case S_RCPT:
evbuffer_add_printf(s->s_bev->output,
"%d Recipient ok\r\n", ss->code);
break;
case S_DATA:
if (s->s_msg.datafp == NULL) {
evbuffer_add_printf(s->s_bev->output,
"421 Service temporarily unavailable\r\n");
return;
}
s->s_state = S_DATACONTENT;
evbuffer_add_printf(s->s_bev->output,
"354 Enter mail, end with \".\" on a line by itself\r\n");
break;
case S_DATACONTENT:
break;
case S_DONE:
s->s_state = S_HELO;
s->s_msg.datafp = NULL;
evbuffer_add_printf(s->s_bev->output,
"250 %s Message accepted for delivery\r\n",
s->s_msg.message_id);
break;
default:
log_debug("session_pickup: state value: %d", s->s_state);
fatal("session_pickup: unknown state");
break;
}
}
void
session_init(struct listener *l, struct session *s)
{
s->s_state = S_INIT;
s->s_env = l->env;
s->s_l = l;
s->s_id = queue_generate_id();
strlcpy(s->s_hostname, "<unknown>", MAXHOSTNAMELEN);
strlcpy(s->s_msg.session_hostname, s->s_hostname, MAXHOSTNAMELEN);
SPLAY_INSERT(sessiontree, &s->s_env->sc_sessions, s);
imsg_compose(s->s_env->sc_ibufs[PROC_LKA], IMSG_LKA_HOSTNAME_LOOKUP,
0, 0, -1, s, sizeof(struct session));
if ((s->s_bev = bufferevent_new(s->s_fd, session_read, session_write,
session_error, s)) == NULL)
fatal(NULL);
if (l->flags & F_SSMTP) {
log_debug("session_init: initializing ssl");
ssl_session_init(s);
return;
}
session_pickup(s, NULL);
}
void
session_read(struct bufferevent *bev, void *p)
{
struct session *s = p;
char *line;
char *ep;
char *args;
read:
s->s_tm = time(NULL);
line = evbuffer_readline(bev->input);
if (line == NULL) {
bufferevent_disable(s->s_bev, EV_READ);
bufferevent_enable(s->s_bev, EV_WRITE);
return;
}
if (s->s_state == S_DATACONTENT) {
line[strcspn(line, "\r")] = '\0';
if (strcmp(line, ".") == 0) {
s->s_state = S_DONE;
fclose(s->s_msg.datafp);
if (s->s_msg.status & S_MESSAGE_PERMFAILURE) {
evbuffer_add_printf(s->s_bev->output,
"554 Transaction failed\r\n");
s->s_msg.datafp = NULL;
/* Remove message file */
imsg_compose(s->s_env->sc_ibufs[PROC_QUEUE], IMSG_QUEUE_DELETE_MESSAGE_FILE,
0, 0, -1, &s->s_msg, sizeof(s->s_msg));
bufferevent_enable(s->s_bev, EV_WRITE);
free(line);
return;
}
bufferevent_disable(s->s_bev, EV_READ);
session_msg_submit(s);
free(line);
return;
} else {
size_t i;
size_t len;
len = strlen(line);
fwrite(line, 1, len, s->s_msg.datafp);
fwrite("\n", 1, 1, s->s_msg.datafp);
fflush(s->s_msg.datafp);
if (! (s->s_flags & F_8BITMIME)) {
for (i = 0; i < len; ++i) {
if (line[i] & 0x80) {
s->s_msg.status |= S_MESSAGE_PERMFAILURE;
strlcpy(s->s_msg.session_errorline, "8BIT data transfered over 7BIT limited channel",
sizeof s->s_msg.session_errorline);
}
}
}
free(line);
}
goto read;
}
bufferevent_disable(s->s_bev, EV_READ);
bufferevent_enable(s->s_bev, EV_WRITE);
line[strcspn(line, "\r")] = '\0';
if ((ep = strchr(line, ':')) == NULL)
ep = strchr(line, ' ');
if (ep != NULL) {
*ep = '\0';
args = ++ep;
while (isspace((int)*args))
args++;
} else
args = NULL;
log_debug("command: %s\targs: %s", line, args);
session_command(s, line, args);
free(line);
return;
}
void
session_write(struct bufferevent *bev, void *p)
{
struct session *s = p;
if (!(s->s_flags & F_QUIT)) {
if (! EVBUFFER_LENGTH(EVBUFFER_OUTPUT(bev))) {
bufferevent_disable(s->s_bev, EV_WRITE);
bufferevent_enable(s->s_bev, EV_READ);
}
if (s->s_state == S_TLS)
session_pickup(s, NULL);
return;
}
if (! EVBUFFER_LENGTH(EVBUFFER_OUTPUT(bev))) {
session_destroy(s);
}
}
void
session_destroy(struct session *s)
{
/*
* cleanup
*/
log_debug("session_destroy: killing client");
close(s->s_fd);
if (s->s_msg.datafp != NULL) {
fclose(s->s_msg.datafp);
s->s_msg.datafp = NULL;
/* Remove message file */
imsg_compose(s->s_env->sc_ibufs[PROC_QUEUE], IMSG_QUEUE_DELETE_MESSAGE_FILE,
0, 0, -1, &s->s_msg, sizeof(s->s_msg));
}
if (s->s_bev != NULL) {
bufferevent_disable(s->s_bev, EV_READ|EV_WRITE);
bufferevent_free(s->s_bev);
}
ssl_session_destroy(s);
SPLAY_REMOVE(sessiontree, &s->s_env->sc_sessions, s);
bzero(s, sizeof(*s));
free(s);
}
void
session_error(struct bufferevent *bev, short event, void *p)
{
struct session *s = p;
session_destroy(s);
}
void
session_msg_submit(struct session *s)
{
imsg_compose(s->s_env->sc_ibufs[PROC_QUEUE],
IMSG_QUEUE_MESSAGE_COMPLETE, 0, 0, -1, &s->s_msg,
sizeof(s->s_msg));
s->s_state = S_DONE;
}
int
session_cmp(struct session *s1, struct session *s2)
{
/*
* do not return u_int64_t's
*/
if (s1->s_id < s2->s_id)
return (-1);
if (s1->s_id > s2->s_id)
return (1);
return (0);
}
int
session_set_path(struct path *path, char *line)
{
size_t len;
char *username;
char *hostname;
len = strlen(line);
if (*line != '<' || line[len - 1] != '>')
return 0;
line[len - 1] = '\0';
username = line + 1;
hostname = strchr(username, '@');
if (username[0] == '\0') {
*path->user = '\0';
*path->domain = '\0';
return 1;
}
if (hostname == NULL) {
if (strcasecmp(username, "postmaster") != 0)
return 0;
hostname = "localhost";
} else {
*hostname++ = '\0';
}
if (strlcpy(path->user, username, sizeof(path->user))
>= MAX_LOCALPART_SIZE)
return 0;
if (strlcpy(path->domain, hostname, sizeof(path->domain))
>= MAX_DOMAINPART_SIZE)
return 0;
return 1;
}
void
session_timeout(int fd, short event, void *p)
{
struct smtpd *env = p;
struct session *sessionp;
struct session *rmsession;
struct timeval tv;
time_t tm;
u_int8_t i;
tm = time(NULL);
rmsession = NULL;
SPLAY_FOREACH(sessionp, sessiontree, &env->sc_sessions) {
if (rmsession != NULL)
session_destroy(rmsession);
for (i = 0; i < sizeof (rfc5321_timeouttab) /
sizeof(struct session_timeout); ++i)
if (rfc5321_timeouttab[i].state == sessionp->s_state)
break;
if (i == sizeof (rfc5321_timeouttab) / sizeof (struct session_timeout)) {
if (tm - SMTPD_SESSION_TIMEOUT < sessionp->s_tm)
continue;
}
else if (tm - rfc5321_timeouttab[i].timeout < sessionp->s_tm) {
continue;
}
rmsession = sessionp;
}
if (rmsession != NULL)
session_destroy(rmsession);
tv.tv_sec = 1;
tv.tv_usec = 0;
evtimer_add(&env->sc_ev, &tv);
}
SPLAY_GENERATE(sessiontree, session, s_nodes, session_cmp);
|