summaryrefslogtreecommitdiffstats
path: root/src/lib-smtp/smtp-server-reply.c
blob: 60342db0b26fe9ccd6a8d36b224fab122b36cac6 (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
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
/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "str.h"
#include "array.h"
#include "istream.h"
#include "ostream.h"
#include "smtp-address.h"
#include "smtp-reply.h"

#include "smtp-server-private.h"

/*
 * Reply
 */

static void smtp_server_reply_destroy(struct smtp_server_reply *reply)
{
	if (reply->command == NULL)
		return;

	if (reply->event != NULL) {
		e_debug(reply->event, "Destroy");
		event_unref(&reply->event);
	}

	if (reply->content == NULL)
		return;
	str_free(&reply->content->text);
}

static void smtp_server_reply_clear(struct smtp_server_reply *reply)
{
	smtp_server_reply_destroy(reply);
	if (reply->submitted) {
		i_assert(reply->command->replies_submitted > 0);
		reply->command->replies_submitted--;
	}
	reply->submitted = FALSE;
	reply->forwarded = FALSE;
}

static void smtp_server_reply_update_event(struct smtp_server_reply *reply)
{
	struct smtp_server_command *command = reply->command;

	event_add_int(reply->event, "index", reply->index);
	event_add_int(reply->event, "status", reply->content->status);

	if (command->replies_expected > 1) {
		event_set_append_log_prefix(reply->event,
			t_strdup_printf("%u reply [%u/%u]: ",
					reply->content->status,
					reply->index+1,
					command->replies_expected));
	} else {
		event_set_append_log_prefix(reply->event,
			t_strdup_printf("%u reply: ",
					reply->content->status));
	}
}

static struct smtp_server_reply *
smtp_server_reply_alloc(struct smtp_server_command *cmd, unsigned int index)
{
	struct smtp_server_reply *reply;
	pool_t pool = cmd->context.pool;

	if (array_is_created(&cmd->replies)) {
		reply = array_idx_modifiable(&cmd->replies, index);
		/* get rid of any existing reply */
		i_assert(!reply->sent);
		smtp_server_reply_clear(reply);
	} else {
		p_array_init(&cmd->replies, pool, cmd->replies_expected);
		array_idx_clear(&cmd->replies, cmd->replies_expected - 1);
		reply = array_idx_modifiable(&cmd->replies, index);
	}
	reply->event = event_create(cmd->context.event);

	return reply;
}

static void
smtp_server_reply_update_prefix(struct smtp_server_reply *reply,
				unsigned int status, const char *enh_code)
{
	pool_t pool = reply->command->context.pool;
	string_t *textbuf, *new_text;
	const char *new_prefix, *text, *p;
	size_t text_len, prefix_len, line_len;

	if (enh_code == NULL || *enh_code == '\0') {
		new_prefix = p_strdup_printf(pool, "%03u-", status);
	} else {
		new_prefix = p_strdup_printf(pool, "%03u-%s ",
					     status, enh_code);
	}

	i_assert(reply->content != NULL);
	textbuf = reply->content->text;

	if (textbuf == NULL || str_len(textbuf) == 0) {
		reply->content->status_prefix = new_prefix;
		return;
	}
	new_text = str_new(default_pool, 256);

	prefix_len = strlen(reply->content->status_prefix);
	text = str_c(textbuf);
	text_len = str_len(textbuf);

	i_assert(text_len > prefix_len);
	text_len -= prefix_len;
	text += prefix_len;

	for (;;) {
		reply->content->last_line = str_len(new_text);

		p = strchr(text, '\n');
		i_assert(p != NULL && p > text && *(p-1) == '\r');
		p++;

		str_append(new_text, new_prefix);
		str_append_data(new_text, text, p - text);

		line_len = (size_t)(p - text);
		i_assert(text_len >= line_len);
		text_len -= line_len;
		text = p;

		if (text_len <= prefix_len)
			break;

		text_len -= prefix_len;
		text += prefix_len;
	}

	str_free(&textbuf);
	reply->content->text = new_text;
	reply->content->status_prefix = new_prefix;
}

void smtp_server_reply_set_status(struct smtp_server_reply *reply,
				  unsigned int status, const char *enh_code)
{
	pool_t pool = reply->command->context.pool;

	/* RFC 5321, Section 4.2:

	   In the absence of extensions negotiated with the client, SMTP servers
	   MUST NOT send reply codes whose first digits are other than 2, 3, 4,
	   or 5.  Clients that receive such out-of-range codes SHOULD normally
	   treat them as fatal errors and terminate the mail transaction.
	 */
	i_assert(status >= 200 && status < 560);

	/* RFC 2034, Section 4:

	   All status codes returned by the server must agree with the primary
	   response code, that is, a 2xx response must incorporate a 2.X.X code,
	   a 4xx response must incorporate a 4.X.X code, and a 5xx response must
	   incorporate a 5.X.X code.
	 */
	i_assert(enh_code == NULL || *enh_code == '\0' ||
		((unsigned int)(enh_code[0] - '0') == (status / 100)
			&& enh_code[1] == '.'));

	if (reply->content->status == status &&
	    null_strcmp(reply->content->enhanced_code, enh_code) == 0)
		return;

	smtp_server_reply_update_prefix(reply, status, enh_code);
	reply->content->status = status;
	reply->content->enhanced_code = p_strdup(pool, enh_code);
}

unsigned int smtp_server_reply_get_status(struct smtp_server_reply *reply,
					  const char **enh_code_r)
{
	if (enh_code_r != NULL)
		*enh_code_r = reply->content->enhanced_code;
	return reply->content->status;
}

struct smtp_server_reply *
smtp_server_reply_create_index(struct smtp_server_command *cmd,
			       unsigned int index, unsigned int status,
			       const char *enh_code)
{
	struct smtp_server_reply *reply;
	pool_t pool = cmd->context.pool;

	i_assert(cmd->replies_expected > 0);
	i_assert(index < cmd->replies_expected);

	reply = smtp_server_reply_alloc(cmd, index);
	reply->index = index;
	reply->command = cmd;

	if (reply->content == NULL)
		reply->content = p_new(pool, struct smtp_server_reply_content, 1);
	smtp_server_reply_set_status(reply, status, enh_code);
	reply->content->text = str_new(default_pool, 256);

	smtp_server_reply_update_event(reply);

	return reply;
}

struct smtp_server_reply *
smtp_server_reply_create(struct smtp_server_command *cmd,
			 unsigned int status, const char *enh_code)
{
	return smtp_server_reply_create_index(cmd, 0, status, enh_code);
}

struct smtp_server_reply *
smtp_server_reply_create_forward(struct smtp_server_command *cmd,
	unsigned int index, const struct smtp_reply *from)
{
	struct smtp_server_reply *reply;
	string_t *textbuf;
	char *text;
	size_t last_line, i;

	reply = smtp_server_reply_create_index(cmd, index,
		from->status, smtp_reply_get_enh_code(from));
	smtp_reply_write(reply->content->text, from);

	i_assert(reply->content != NULL);
	textbuf = reply->content->text;
	text = str_c_modifiable(textbuf);

	/* Find the last line */
	reply->content->last_line = last_line = 0;
	for (i = 0; i < str_len(textbuf); i++) {
		if (text[i] == '\n') {
			reply->content->last_line = last_line;
			last_line = i + 1;
		}
	}

	/* Make this reply suitable for further amendment with
	   smtp_server_reply_add_text() */
	if ((reply->content->last_line + 3) < str_len(textbuf)) {
		i_assert(text[reply->content->last_line + 3] == ' ');
		text[reply->content->last_line + 3] = '-';
	} else {
		str_append_c(textbuf, '-');
	}

	reply->forwarded = TRUE;

	return reply;
}

void smtp_server_reply_free(struct smtp_server_command *cmd)
{
	unsigned int i;

	if (!array_is_created(&cmd->replies))
		return;

	for (i = 0; i < cmd->replies_expected; i++) {
		struct smtp_server_reply *reply =
			array_idx_modifiable(&cmd->replies, i);
		smtp_server_reply_destroy(reply);
	}
}

void smtp_server_reply_add_text(struct smtp_server_reply *reply,
				const char *text)
{
	string_t *textbuf = reply->content->text;

	i_assert(!reply->submitted);

	if (*text == '\0')
		return;

	do {
		const char *p;

		reply->content->last_line = str_len(textbuf);

		p = strchr(text, '\n');
		str_append(textbuf, reply->content->status_prefix);
		if (p == NULL) {
			str_append(textbuf, text);
			text = NULL;
		} else {
			if (p > text && *(p-1) == '\r')
				str_append_data(textbuf, text, p - text - 1);
			else
				str_append_data(textbuf, text, p - text);
			text = p + 1;
		}
		str_append(textbuf, "\r\n");
	} while (text != NULL && *text != '\0');
}

static size_t
smtp_server_reply_get_path_len(struct smtp_server_reply *reply)
{
	size_t prefix_len = strlen(reply->content->status_prefix);
	size_t text_len = str_len(reply->content->text), line_len, path_len;
	const char *text = str_c(reply->content->text);
	const char *text_end = text + text_len, *line_end;

	i_assert(prefix_len <= text_len);

	line_end = strchr(text, '\r');
	if (line_end == NULL) {
		line_end = text_end;
		line_len = text_len;
	} else {
		i_assert(line_end + 1 < text_end);
		i_assert(*(line_end + 1) == '\n');
		line_len = line_end - text;
	}

	if (prefix_len == line_len || text[prefix_len] != '<') {
		path_len = 0;
	} else {
		const char *path_begin = &text[prefix_len], *path_end;

		path_end = strchr(path_begin, '>');
		if (path_end == NULL || path_end > line_end)
			path_len = 0;
		else {
			i_assert(path_end < line_end);
			path_end++;
			path_len = path_end - path_begin;
			if (path_end < line_end && *path_end != ' ')
				path_len = 0;
		}
	}

	i_assert(prefix_len + path_len <= text_len);
	return path_len;
}

void smtp_server_reply_prepend_text(struct smtp_server_reply *reply,
				    const char *text_prefix)
{
	const char *text = str_c(reply->content->text);
	size_t tlen = str_len(reply->content->text), offset;

	i_assert(!reply->sent);
	i_assert(reply->content != NULL);
	i_assert(reply->content->text != NULL);

	offset = strlen(reply->content->status_prefix) +
		smtp_server_reply_get_path_len(reply);
	i_assert(offset < tlen);
	if (text[offset] == ' ')
		offset++;

	str_insert(reply->content->text, offset, text_prefix);

	if (reply->content->last_line > 0)
		reply->content->last_line += strlen(text_prefix);
}

void smtp_server_reply_replace_path(struct smtp_server_reply *reply,
				    struct smtp_address *path, bool add)
{
	size_t prefix_len, path_len;
	const char *path_text;

	i_assert(!reply->sent);
	i_assert(reply->content != NULL);
	i_assert(reply->content->text != NULL);

	prefix_len = strlen(reply->content->status_prefix);
	path_len = smtp_server_reply_get_path_len(reply);

	if (path_len > 0) {
		path_text = smtp_address_encode_path(path);
		str_replace(reply->content->text, prefix_len, path_len,
			    path_text);
	} else if (add) {
		path_text = t_strdup_printf(
			"<%s> ", smtp_address_encode(path));
		str_insert(reply->content->text, prefix_len, path_text);
	}
}

void smtp_server_reply_submit(struct smtp_server_reply *reply)
{
	i_assert(!reply->submitted);
	i_assert(reply->content != NULL);
	i_assert(str_len(reply->content->text) >= 5);
	e_debug(reply->event, "Submitted");

	reply->command->replies_submitted++;
	reply->submitted = TRUE;
	smtp_server_command_submit_reply(reply->command);
}

void smtp_server_reply_submit_duplicate(struct smtp_server_cmd_ctx *_cmd,
					unsigned int index,
					unsigned int from_index)
{
	struct smtp_server_command *cmd = _cmd->cmd;
	struct smtp_server_reply *reply, *from_reply;

	i_assert(cmd->replies_expected > 0);
	i_assert(index < cmd->replies_expected);
	i_assert(from_index < cmd->replies_expected);
	i_assert(array_is_created(&cmd->replies));

	from_reply = array_idx_modifiable(&cmd->replies, from_index);
	i_assert(from_reply->content != NULL);
	i_assert(from_reply->submitted);

	reply = smtp_server_reply_alloc(cmd, index);
	reply->index = index;
	reply->command = cmd;
	reply->content = from_reply->content;
	smtp_server_reply_update_event(reply);

	smtp_server_reply_submit(reply);
}

void smtp_server_reply_indexv(struct smtp_server_cmd_ctx *_cmd,
	unsigned int index, unsigned int status, const char *enh_code,
	const char *fmt, va_list args)
{
	struct smtp_server_command *cmd = _cmd->cmd;
	struct smtp_server_reply *reply;

	reply = smtp_server_reply_create_index(cmd, index, status, enh_code);
	smtp_server_reply_add_text(reply, t_strdup_vprintf(fmt, args));
	smtp_server_reply_submit(reply);
}

void smtp_server_reply(struct smtp_server_cmd_ctx *_cmd,
	unsigned int status, const char *enh_code, const char *fmt, ...)
{
	struct smtp_server_command *cmd = _cmd->cmd;
	va_list args;

	i_assert(cmd->replies_expected <= 1);

	va_start(args, fmt);
	smtp_server_reply_indexv(_cmd, 0, status, enh_code, fmt, args);
	va_end(args);
}

void smtp_server_reply_index(struct smtp_server_cmd_ctx *_cmd,
	unsigned int index, unsigned int status, const char *enh_code,
	const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	smtp_server_reply_indexv(_cmd, index, status, enh_code, fmt, args);
	va_end(args);
}

void smtp_server_reply_index_forward(struct smtp_server_cmd_ctx *cmd,
	unsigned int index, const struct smtp_reply *from)
{
	smtp_server_reply_submit(
		smtp_server_reply_create_forward(cmd->cmd, index, from));
}

void smtp_server_reply_forward(struct smtp_server_cmd_ctx *_cmd,
			       const struct smtp_reply *from)
{
	struct smtp_server_command *cmd = _cmd->cmd;

	i_assert(cmd->replies_expected <= 1);

	smtp_server_reply_submit(
		smtp_server_reply_create_forward(cmd, 0, from));
}

static void ATTR_FORMAT(4, 0)
smtp_server_reply_allv(struct smtp_server_cmd_ctx *_cmd,
		       unsigned int status, const char *enh_code,
		       const char *fmt, va_list args)
{
	struct smtp_server_command *cmd = _cmd->cmd;
	struct smtp_server_reply *reply;
	const char *text;
	unsigned int first, i = 0;

	/* find the first unsent reply */
	if (array_is_created(&cmd->replies)) {
		for (; i < cmd->replies_expected; i++) {
			struct smtp_server_reply *reply =
				array_idx_modifiable(&cmd->replies, i);
			if (!reply->sent)
				break;
		}
		i_assert (i < cmd->replies_expected);
	}
	first = i++;

	/* compose the reply text */
	text = t_strdup_vprintf(fmt, args);

	/* submit the first remaining reply */
	reply = smtp_server_reply_create_index(cmd, first, status, enh_code);
	smtp_server_reply_add_text(reply, text);
	smtp_server_reply_submit(reply);

	/* duplicate the rest from it */
	for (; i < cmd->replies_expected; i++)
		smtp_server_reply_submit_duplicate(_cmd, i, first);
}

void smtp_server_reply_all(struct smtp_server_cmd_ctx *_cmd,
			   unsigned int status, const char *enh_code,
			   const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	smtp_server_reply_allv(_cmd, status, enh_code, fmt, args);
	va_end(args);
}

void smtp_server_reply_early(struct smtp_server_cmd_ctx *_cmd,
			     unsigned int status, const char *enh_code,
			     const char *fmt, ...)
{
	va_list args;

	_cmd->cmd->reply_early = TRUE;

	va_start(args, fmt);
	smtp_server_reply_allv(_cmd, status, enh_code, fmt, args);
	va_end(args);
}

void smtp_server_reply_quit(struct smtp_server_cmd_ctx *_cmd)
{
	struct smtp_server_command *cmd = _cmd->cmd;
	struct smtp_server_reply *reply;

	reply = smtp_server_reply_create(cmd, 221, "2.0.0");
	smtp_server_reply_add_text(reply, "Bye");
	smtp_server_reply_submit(reply);
}

static void
smtp_server_reply_write_one_line(const struct smtp_server_reply *reply,
				 string_t *str, bool skip_status)
{
	string_t *textbuf;
	const char *text, *p;
	size_t text_len, prefix_len, line_len;

	i_assert(reply->content != NULL);
	textbuf = reply->content->text;
	i_assert(str_len(textbuf) > 0);

	prefix_len = strlen(reply->content->status_prefix);
	text = str_c(textbuf);
	text_len = str_len(textbuf);

	if (skip_status) {
		i_assert(text_len > prefix_len);
		text_len -= prefix_len;
		text += prefix_len;
	}

	for (;;) {
		p = strchr(text, '\n');
		i_assert(p != NULL && p > text && *(p-1) == '\r');
		str_append_data(str, text, p - text - 1);
		line_len = (size_t)(p - text) + 1;
		i_assert(text_len >= line_len);
		text_len -= line_len;
		text = p + 1;

		if (text_len <= prefix_len)
			break;

		text_len -= prefix_len;
		text += prefix_len;
		str_append_c(str, ' ');
	}
}

const char *
smtp_server_reply_get_one_line(const struct smtp_server_reply *reply)
{
	string_t *str = t_str_new(256);

	smtp_server_reply_write_one_line(reply, str, FALSE);
	return str_c(str);
}

const char *
smtp_server_reply_get_message(const struct smtp_server_reply *reply)
{
	string_t *str = t_str_new(256);

	smtp_server_reply_write_one_line(reply, str, TRUE);
	return str_c(str);
}

static int smtp_server_reply_send_real(struct smtp_server_reply *reply)
{
	struct smtp_server_command *cmd = reply->command;
	struct smtp_server_connection *conn = cmd->context.conn;
	struct ostream *output = conn->conn.output;
	string_t *textbuf;
	char *text;
	int ret = 0;

	i_assert(reply->content != NULL);
	textbuf = reply->content->text;
	i_assert(str_len(textbuf) > 0);

	/* substitute '-' with ' ' in last line */
	text = str_c_modifiable(textbuf);
	text = text + reply->content->last_line + 3;
	if (text[0] != ' ') {
		i_assert(text[0] == '-');
		text[0] = ' ';
	}

	if (o_stream_send(output, str_data(textbuf), str_len(textbuf)) < 0) {
		e_debug(reply->event, "Send failed: %s",
			o_stream_get_disconnect_reason(output));
		smtp_server_connection_handle_output_error(conn);
		return -1;
	}

	e_debug(reply->event, "Sent: %s",
		smtp_server_reply_get_one_line(reply));
	return ret;
}

int smtp_server_reply_send(struct smtp_server_reply *reply)
{
	int ret;

	if (reply->sent)
		return 0;

	T_BEGIN {
		ret = smtp_server_reply_send_real(reply);
	} T_END;

	reply->sent = TRUE;
	return ret;
}

bool smtp_server_reply_is_success(const struct smtp_server_reply *reply)
{
	i_assert(reply->content != NULL);
	return (reply->content->status / 100 == 2);
}

void smtp_server_reply_add_to_event(const struct smtp_server_reply *reply,
				    struct event_passthrough *e)
{
	i_assert(reply->content != NULL);
	e->add_int("status_code", reply->content->status);
	if (reply->content->enhanced_code != NULL &&
	    reply->content->enhanced_code[0] != '\0')
		e->add_str("enhanced_code", reply->content->enhanced_code);
	if (!smtp_server_reply_is_success(reply))
		e->add_str("error", smtp_server_reply_get_message(reply));
}

/*
 * EHLO reply
 */

struct smtp_server_reply *
smtp_server_reply_create_ehlo(struct smtp_server_command *cmd)
{
	struct smtp_server_connection *conn = cmd->context.conn;
	struct smtp_server_reply *reply;
	string_t *textbuf;

	reply = smtp_server_reply_create(cmd, 250, "");
	textbuf = reply->content->text;
	str_append(textbuf, reply->content->status_prefix);
	str_append(textbuf, conn->set.hostname);
	str_append(textbuf, "\r\n");

	return reply;
}

void smtp_server_reply_ehlo_add(struct smtp_server_reply *reply,
				const char *keyword)
{
	string_t *textbuf;

	i_assert(!reply->submitted);
	i_assert(reply->content != NULL);
	textbuf = reply->content->text;

	reply->content->last_line = str_len(textbuf);
	str_append(textbuf, reply->content->status_prefix);
	str_append(textbuf, keyword);
	str_append(textbuf, "\r\n");
}

void smtp_server_reply_ehlo_add_param(struct smtp_server_reply *reply,
	const char *keyword, const char *param_fmt, ...)
{
	va_list args;
	string_t *textbuf;

	i_assert(!reply->submitted);
	i_assert(reply->content != NULL);
	textbuf = reply->content->text;

	reply->content->last_line = str_len(textbuf);
	str_append(textbuf, reply->content->status_prefix);
	str_append(textbuf, keyword);
	if (*param_fmt != '\0') {
		va_start(args, param_fmt);
		str_append_c(textbuf, ' ');
		str_vprintfa(textbuf, param_fmt, args);
		va_end(args);
	}
	str_append(textbuf, "\r\n");
}

void smtp_server_reply_ehlo_add_params(struct smtp_server_reply *reply,
				       const char *keyword,
				       const char *const *params)
{
	string_t *textbuf;

	i_assert(!reply->submitted);
	i_assert(reply->content != NULL);
	textbuf = reply->content->text;

	reply->content->last_line = str_len(textbuf);
	str_append(textbuf, reply->content->status_prefix);
	str_append(textbuf, keyword);
	if (params != NULL) {
		while (*params != NULL) {
			str_append_c(textbuf, ' ');
			str_append(textbuf, *params);
			params++;
		}
	}
	str_append(textbuf, "\r\n");
}

void smtp_server_reply_ehlo_add_8bitmime(struct smtp_server_reply *reply)
{
	struct smtp_server_cmd_ctx *cmd = &reply->command->context;
	struct smtp_server_connection *conn = cmd->conn;
	enum smtp_capability caps = conn->set.capabilities;

	if ((caps & SMTP_CAPABILITY_8BITMIME) == 0)
		return;
	smtp_server_reply_ehlo_add(reply, "8BITMIME");
}

void smtp_server_reply_ehlo_add_binarymime(struct smtp_server_reply *reply)
{
	struct smtp_server_cmd_ctx *cmd = &reply->command->context;
	struct smtp_server_connection *conn = cmd->conn;
	enum smtp_capability caps = conn->set.capabilities;

	if ((caps & SMTP_CAPABILITY_BINARYMIME) == 0 ||
	    (caps & SMTP_CAPABILITY_CHUNKING) == 0)
		return;
	smtp_server_reply_ehlo_add(reply, "BINARYMIME");
}

void smtp_server_reply_ehlo_add_chunking(struct smtp_server_reply *reply)
{
	struct smtp_server_cmd_ctx *cmd = &reply->command->context;
	struct smtp_server_connection *conn = cmd->conn;
	enum smtp_capability caps = conn->set.capabilities;

	if ((caps & SMTP_CAPABILITY_CHUNKING) == 0)
		return;
	smtp_server_reply_ehlo_add(reply, "CHUNKING");
}

void smtp_server_reply_ehlo_add_dsn(struct smtp_server_reply *reply)
{
	struct smtp_server_cmd_ctx *cmd = &reply->command->context;
	struct smtp_server_connection *conn = cmd->conn;
	enum smtp_capability caps = conn->set.capabilities;

	if ((caps & SMTP_CAPABILITY_DSN) == 0)
		return;
	smtp_server_reply_ehlo_add(reply, "DSN");
}

void smtp_server_reply_ehlo_add_enhancedstatuscodes(
	struct smtp_server_reply *reply)
{
	struct smtp_server_cmd_ctx *cmd = &reply->command->context;
	struct smtp_server_connection *conn = cmd->conn;
	enum smtp_capability caps = conn->set.capabilities;

	if ((caps & SMTP_CAPABILITY_ENHANCEDSTATUSCODES) == 0)
		return;
	smtp_server_reply_ehlo_add(reply, "ENHANCEDSTATUSCODES");
}

void smtp_server_reply_ehlo_add_pipelining(struct smtp_server_reply *reply)
{
	smtp_server_reply_ehlo_add(reply, "PIPELINING");
}

void smtp_server_reply_ehlo_add_size(struct smtp_server_reply *reply)
{
	struct smtp_server_cmd_ctx *cmd = &reply->command->context;
	struct smtp_server_connection *conn = cmd->conn;
	enum smtp_capability caps = conn->set.capabilities;
	uoff_t cap_size = conn->set.max_message_size;

	if ((caps & SMTP_CAPABILITY_SIZE) == 0)
		return;

	if (cap_size > 0 && cap_size != UOFF_T_MAX) {
		smtp_server_reply_ehlo_add_param(reply,
			"SIZE", "%"PRIuUOFF_T, cap_size);
	} else {
		smtp_server_reply_ehlo_add(reply, "SIZE");
	}
}

void smtp_server_reply_ehlo_add_starttls(struct smtp_server_reply *reply)
{
	struct smtp_server_cmd_ctx *cmd = &reply->command->context;
	struct smtp_server_connection *conn = cmd->conn;
	enum smtp_capability caps = conn->set.capabilities;

	if ((caps & SMTP_CAPABILITY_STARTTLS) == 0)
		return;
	smtp_server_reply_ehlo_add(reply, "STARTTLS");
}

void smtp_server_reply_ehlo_add_vrfy(struct smtp_server_reply *reply)
{
	struct smtp_server_cmd_ctx *cmd = &reply->command->context;
	struct smtp_server_connection *conn = cmd->conn;
	enum smtp_capability caps = conn->set.capabilities;

	if ((caps & SMTP_CAPABILITY_VRFY) == 0)
		return;
	smtp_server_reply_ehlo_add(reply, "VRFY");
}

void smtp_server_reply_ehlo_add_xclient(struct smtp_server_reply *reply)
{
	static const char *base_fields =
		"ADDR PORT PROTO HELO LOGIN SESSION TTL TIMEOUT";
	struct smtp_server_cmd_ctx *cmd = &reply->command->context;
	struct smtp_server_connection *conn = cmd->conn;

	if (!smtp_server_connection_is_trusted(conn))
		return;
	if (conn->set.xclient_extensions == NULL ||
	    *conn->set.xclient_extensions == NULL) {
		smtp_server_reply_ehlo_add_param(reply, "XCLIENT", "%s",
			base_fields);
		return;
	}

	smtp_server_reply_ehlo_add_param(reply, "XCLIENT", "%s",
		t_strconcat(base_fields, " ",
			t_strarray_join(conn->set.xclient_extensions, " "),
			NULL));
}