bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "submission-common.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "str.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "array.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "smtp-parser.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "smtp-address.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "smtp-syntax.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "smtp-client.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "smtp-client-connection.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "submission-commands.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch/*
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch * RCPT command
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch */
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschstruct cmd_rcpt_context {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch struct client *client;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch struct smtp_server_cmd_ctx *cmd;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch struct smtp_server_cmd_rcpt *data;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch struct smtp_client_command *cmd_proxied;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch};
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschstatic void cmd_rcpt_replied(struct smtp_server_cmd_ctx *cmd)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch{
0cce67356125083a0affed3ca9cb4f278258e5beJosef 'Jeff' Sipek struct cmd_rcpt_context *rcpt_cmd = cmd->context;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (rcpt_cmd->cmd_proxied != NULL)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch smtp_client_command_abort(&rcpt_cmd->cmd_proxied);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch}
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschstatic void cmd_rcpt_proxy_cb(const struct smtp_reply *proxy_reply,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch struct cmd_rcpt_context *rcpt_cmd)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch{
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch struct smtp_server_cmd_ctx *cmd = rcpt_cmd->cmd;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch struct client *client = rcpt_cmd->client;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch struct smtp_reply reply;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch /* finished proxying MAIL command to relay server */
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch i_assert(rcpt_cmd != NULL);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch rcpt_cmd->cmd_proxied = NULL;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (!client_command_handle_proxy_reply(client, proxy_reply, &reply))
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch return;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if ((proxy_reply->status / 100) == 2) {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch /* the default 2.0.0 code won't do */
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (!smtp_reply_has_enhanced_code(proxy_reply))
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch reply.enhanced_code = SMTP_REPLY_ENH_CODE(2, 1, 5);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch }
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch /* forward reply */
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch smtp_server_reply_forward(cmd, &reply);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch}
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschint cmd_rcpt(void *conn_ctx, struct smtp_server_cmd_ctx *cmd,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch struct smtp_server_cmd_rcpt *data)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch{
0cce67356125083a0affed3ca9cb4f278258e5beJosef 'Jeff' Sipek struct client *client = conn_ctx;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch struct cmd_rcpt_context *rcpt_cmd;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch /* queue command (pipeline) */
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch rcpt_cmd = p_new(cmd->pool, struct cmd_rcpt_context, 1);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch rcpt_cmd->cmd = cmd;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch rcpt_cmd->data = data;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch rcpt_cmd->client = client;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
0cce67356125083a0affed3ca9cb4f278258e5beJosef 'Jeff' Sipek cmd->context = rcpt_cmd;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch cmd->hook_replied = cmd_rcpt_replied;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch rcpt_cmd->cmd_proxied = smtp_client_command_rcpt_submit(
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch client->proxy_conn, 0, data->path, &data->params,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch cmd_rcpt_proxy_cb, rcpt_cmd);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch return 0;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch}