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

#include "lib.h"
#include "str.h"
#include "array.h"
#include "smtp-syntax.h"

#include "smtp-server-private.h"

/* EHLO, HELO commands */

static void
cmd_helo_completed(struct smtp_server_cmd_ctx *cmd,
		   struct smtp_server_cmd_helo *data)
{
	struct smtp_server_connection *conn = cmd->conn;
	struct smtp_server_command *command = cmd->cmd;

	i_assert(smtp_server_command_is_replied(command));
	if (!smtp_server_command_replied_success(command)) {
		/* Failure */
		return;
	}

	if (conn->pending_helo == &data->helo)
		conn->pending_helo = NULL;

	/* Success */
	smtp_server_connection_reset_state(conn);

	i_free(conn->helo_domain);
	conn->helo_domain = i_strdup(data->helo.domain);
	conn->helo.domain = conn->helo_domain;
	conn->helo.domain_valid = data->helo.domain_valid;
	conn->helo.old_smtp = data->helo.old_smtp;
}

static void
cmd_helo_next(struct smtp_server_cmd_ctx *cmd,
	      struct smtp_server_cmd_helo *data)
{
	struct smtp_server_connection *conn = cmd->conn;

	if (null_strcmp(conn->helo.domain, data->helo.domain) != 0 ||
	    conn->helo.old_smtp != data->helo.old_smtp)
		data->changed = TRUE; /* Definitive assessment */
}

static void
smtp_server_cmd_helo_run(struct smtp_server_cmd_ctx *cmd, const char *params,
			 bool old_smtp)
{
	struct smtp_server_connection *conn = cmd->conn;
	const struct smtp_server_callbacks *callbacks = conn->callbacks;
	struct smtp_server_cmd_helo *helo_data;
	struct smtp_server_command *command = cmd->cmd;
	bool first = (conn->pending_helo == NULL && conn->helo.domain == NULL);
	const char *domain = NULL;
	int ret;

	/* Parse domain argument */

	if (*params == '\0') {
		smtp_server_reply(cmd, 501, "", "Missing hostname");
		return;
	}
	ret = smtp_helo_domain_parse(params, !old_smtp, &domain);

	smtp_server_command_pipeline_block(cmd);
	if (conn->state.state == SMTP_SERVER_STATE_GREETING) {
		smtp_server_connection_set_state(conn, SMTP_SERVER_STATE_HELO,
						 NULL);
	}

	helo_data = p_new(cmd->pool, struct smtp_server_cmd_helo, 1);
	helo_data->helo.domain = p_strdup(cmd->pool, domain);
	helo_data->helo.domain_valid = ( ret >= 0 );
	helo_data->helo.old_smtp = old_smtp;
	helo_data->first = first;
	command->data = helo_data;

	if (null_strcmp(conn->helo.domain, domain) != 0 ||
	    conn->helo.old_smtp != old_smtp)
		helo_data->changed = TRUE; /* Preliminary assessment */

	if (conn->pending_helo == NULL)
		conn->pending_helo = &helo_data->helo;

	smtp_server_command_add_hook(
		command, SMTP_SERVER_COMMAND_HOOK_NEXT,
		cmd_helo_next, helo_data);
	smtp_server_command_add_hook(
		command, SMTP_SERVER_COMMAND_HOOK_COMPLETED,
		cmd_helo_completed, helo_data);

	smtp_server_command_ref(command);
	if (callbacks != NULL && callbacks->conn_cmd_helo != NULL) {
		/* Specific implementation of EHLO command */
		ret = callbacks->conn_cmd_helo(conn->context, cmd, helo_data);
		if (ret <= 0) {
			i_assert(ret == 0 ||
				 smtp_server_command_is_replied(command));
			/* Command is waiting for external event or it failed */
			smtp_server_command_unref(&command);
			return;
		}
	}

	if (!smtp_server_command_is_replied(command)) {
		/* Submit default EHLO reply if none is provided */
		smtp_server_cmd_ehlo_reply_default(cmd);
	}
	smtp_server_command_unref(&command);
}

void smtp_server_cmd_ehlo(struct smtp_server_cmd_ctx *cmd, const char *params)
{
	/* ehlo = "EHLO" SP ( Domain / address-literal ) CRLF */

	smtp_server_cmd_helo_run(cmd, params, FALSE);
}

void smtp_server_cmd_helo(struct smtp_server_cmd_ctx *cmd, const char *params)
{
	/* helo = "HELO" SP Domain CRLF */

	smtp_server_cmd_helo_run(cmd, params, TRUE);
}

struct smtp_server_reply *
smtp_server_cmd_ehlo_reply_create(struct smtp_server_cmd_ctx *cmd)
{
	static struct {
		const char *name;
		void (*add)(struct smtp_server_reply *reply);
	} standard_caps[] = {
		/* Sorted alphabetically */
		{ "8BITMIME", smtp_server_reply_ehlo_add_8bitmime },
		{ "BINARYMIME", smtp_server_reply_ehlo_add_binarymime },
		{ "CHUNKING", smtp_server_reply_ehlo_add_chunking },
		{ "DSN", smtp_server_reply_ehlo_add_dsn },
		{ "ENHANCEDSTATUSCODES",
		  smtp_server_reply_ehlo_add_enhancedstatuscodes },
		{ "PIPELINING", smtp_server_reply_ehlo_add_pipelining },
		{ "SIZE", smtp_server_reply_ehlo_add_size },
		{ "STARTTLS", smtp_server_reply_ehlo_add_starttls },
		{ "VRFY", smtp_server_reply_ehlo_add_vrfy },
		{ "XCLIENT", smtp_server_reply_ehlo_add_xclient }
	};
	const unsigned int standard_caps_count = N_ELEMENTS(standard_caps);
	struct smtp_server_connection *conn = cmd->conn;
	struct smtp_server_command *command = cmd->cmd;
	struct smtp_server_cmd_helo *helo_data = command->data;
	const struct smtp_capability_extra *extra_caps = NULL;
	unsigned int extra_caps_count, i, j;
	struct smtp_server_reply *reply;

	reply = smtp_server_reply_create_ehlo(cmd->cmd);

	if (helo_data->helo.old_smtp) {
		i_assert(cmd->cmd->reg->func == smtp_server_cmd_helo);
		return reply;
	}
	i_assert(cmd->cmd->reg->func == smtp_server_cmd_ehlo);

	extra_caps_count = 0;
	if (array_is_created(&conn->extra_capabilities)) {
		extra_caps = array_get(&conn->extra_capabilities,
				       &extra_caps_count);
	}

	i = j = 0;
	while (i < standard_caps_count || j < extra_caps_count) {
		if (i < standard_caps_count && 
		    (j >= extra_caps_count ||
		     strcasecmp(standard_caps[i].name,
				extra_caps[j].name) < 0)) {
			standard_caps[i].add(reply);
			i++;
		} else {
			smtp_server_reply_ehlo_add_params(
				reply, extra_caps[j].name,
				extra_caps[j].params);
			j++;
		}
	}
	return reply;
}

void smtp_server_cmd_ehlo_reply_default(struct smtp_server_cmd_ctx *cmd)
{
	struct smtp_server_reply *reply;

	reply = smtp_server_cmd_ehlo_reply_create(cmd);
	smtp_server_reply_submit(reply);
}