Skip to content

Commit

Permalink
lib-smtp: server: Add workarounds for well-known MAIL and RCPT comman…
Browse files Browse the repository at this point in the history
…d syntax deviations.
  • Loading branch information
stephanbosch authored and sirainen committed Apr 12, 2018
1 parent 81c8955 commit 6b16d3e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
24 changes: 21 additions & 3 deletions src/lib-smtp/smtp-server-cmd-mail.c
Expand Up @@ -73,6 +73,7 @@ void smtp_server_cmd_mail(struct smtp_server_cmd_ctx *cmd,
const struct smtp_server_callbacks *callbacks = conn->callbacks;
struct smtp_server_command *command = cmd->cmd;
struct smtp_server_cmd_mail *mail_data;
enum smtp_address_parse_flags path_parse_flags;
struct smtp_address *path;
enum smtp_param_parse_error pperror;
const char *error;
Expand All @@ -91,9 +92,26 @@ void smtp_server_cmd_mail(struct smtp_server_cmd_ctx *cmd,
smtp_server_reply(cmd, 501, "5.5.4", "Invalid parameters");
return;
}
if (smtp_address_parse_path_full(pool_datastack_create(), params + 5,
SMTP_ADDRESS_PARSE_FLAG_ALLOW_EMPTY,
&path, &error, &params) < 0) {
if (params[5] != ' ' && params[5] != '\t') {
params += 5;
} else if ((set->workarounds &
SMTP_SERVER_WORKAROUND_WHITESPACE_BEFORE_PATH) != 0) {
params += 5;
while (*params == ' ' || *params == '\t')
params++;
} else {
smtp_server_reply(cmd, 501, "5.5.4",
"Invalid FROM: "
"Unexpected whitespace before path");
return;
}
path_parse_flags = SMTP_ADDRESS_PARSE_FLAG_ALLOW_EMPTY;
if (*params != '\0' &&
(set->workarounds & SMTP_SERVER_WORKAROUND_MAILBOX_FOR_PATH) != 0)
path_parse_flags |= SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL;
if (smtp_address_parse_path_full(pool_datastack_create(), params,
path_parse_flags, &path, &error,
&params) < 0) {
smtp_server_reply(cmd, 501, "5.5.4", "Invalid FROM: %s", error);
return;
}
Expand Down
23 changes: 20 additions & 3 deletions src/lib-smtp/smtp-server-cmd-rcpt.c
Expand Up @@ -100,6 +100,7 @@ void smtp_server_cmd_rcpt(struct smtp_server_cmd_ctx *cmd,
const struct smtp_server_callbacks *callbacks = conn->callbacks;
struct smtp_server_command *command = cmd->cmd;
struct smtp_server_cmd_rcpt *rcpt_data;
enum smtp_address_parse_flags path_parse_flags;
struct smtp_address *path;
enum smtp_param_parse_error pperror;
const char *error;
Expand All @@ -120,9 +121,25 @@ void smtp_server_cmd_rcpt(struct smtp_server_cmd_ctx *cmd,
501, "5.5.4", "Invalid parameters");
return;
}
if (smtp_address_parse_path_full(pool_datastack_create(), params + 3,
SMTP_ADDRESS_PARSE_FLAG_ALLOW_LOCALPART,
&path, &error, &params) < 0) {
if (params[3] != ' ' && params[3] != '\t') {
params += 3;
} else if ((set->workarounds &
SMTP_SERVER_WORKAROUND_WHITESPACE_BEFORE_PATH) != 0) {
params += 3;
while (*params == ' ' || *params == '\t')
params++;
} else {
smtp_server_reply(cmd, 501, "5.5.4",
"Invalid TO: "
"Unexpected whitespace before path");
return;
}
path_parse_flags = SMTP_ADDRESS_PARSE_FLAG_ALLOW_LOCALPART;
if ((set->workarounds & SMTP_SERVER_WORKAROUND_MAILBOX_FOR_PATH) != 0)
path_parse_flags |= SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL;
if (smtp_address_parse_path_full(pool_datastack_create(), params,
path_parse_flags, &path, &error,
&params) < 0) {
smtp_server_reply(cmd,
501, "5.5.4", "Invalid TO: %s", error);
return;
Expand Down
2 changes: 2 additions & 0 deletions src/lib-smtp/smtp-server-connection.c
Expand Up @@ -794,6 +794,8 @@ smtp_server_connection_alloc(struct smtp_server *server,
}
if (set->capabilities != 0)
conn->set.capabilities = set->capabilities;
conn->set.workarounds |= set->workarounds;

if (set->max_client_idle_time_msecs > 0) {
conn->set.max_client_idle_time_msecs =
set->max_client_idle_time_msecs;
Expand Down
1 change: 1 addition & 0 deletions src/lib-smtp/smtp-server.c
Expand Up @@ -43,6 +43,7 @@ struct smtp_server *smtp_server_init(const struct smtp_server_settings *set)
} else {
server->set.capabilities = set->capabilities;
}
server->set.workarounds = set->workarounds;
server->set.max_client_idle_time_msecs = set->max_client_idle_time_msecs;
server->set.max_pipelined_commands = (set->max_pipelined_commands > 0 ?
set->max_pipelined_commands : 1);
Expand Down
6 changes: 6 additions & 0 deletions src/lib-smtp/smtp-server.h
Expand Up @@ -226,9 +226,15 @@ struct smtp_server_callbacks {
* Server
*/

enum smtp_server_workarounds {
SMTP_SERVER_WORKAROUND_WHITESPACE_BEFORE_PATH = BIT(0),
SMTP_SERVER_WORKAROUND_MAILBOX_FOR_PATH = BIT(1)
};

struct smtp_server_settings {
enum smtp_protocol protocol;
enum smtp_capability capabilities;
enum smtp_server_workarounds workarounds;

const char *hostname;
const char *login_greeting;
Expand Down

0 comments on commit 6b16d3e

Please sign in to comment.