bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "submission-common.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "ioloop.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "array.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "str.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "llist.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "istream.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "ostream.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "mail-storage.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "smtp-reply.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/* The command handling of the submission proxy service aims to follow the
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch following rules:
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch - Attempt to keep pipelined commands pipelined when proxying them to the
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch actual relay service.
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch - Don't forward commands if they're known to fail at the relay server. Errors
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch can still occur if pipelined commands fail. Abort subsequent pending
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch commands if such failures affect those commands.
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch - Keep predictable errors consistent as much as possible; send our own reply
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if the error condition is clear (e.g. missing MAIL, RCPT).
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch*/
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschbool client_command_handle_proxy_reply(struct client *client,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch const struct smtp_reply *reply, struct smtp_reply *reply_r)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch{
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch *reply_r = *reply;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch switch (reply->status) {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch case SMTP_CLIENT_COMMAND_ERROR_ABORTED:
64a087b8c2a82fd1850ba675a3352a78a61a80e0Timo Sirainen return FALSE;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch case SMTP_CLIENT_COMMAND_ERROR_HOST_LOOKUP_FAILED:
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch case SMTP_CLIENT_COMMAND_ERROR_CONNECT_FAILED:
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch case SMTP_CLIENT_COMMAND_ERROR_AUTH_FAILED:
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch i_unreached();
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch return FALSE;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch case SMTP_CLIENT_COMMAND_ERROR_CONNECTION_CLOSED:
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch case SMTP_CLIENT_COMMAND_ERROR_CONNECTION_LOST:
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch case SMTP_CLIENT_COMMAND_ERROR_BAD_REPLY:
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch case SMTP_CLIENT_COMMAND_ERROR_TIMED_OUT:
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch client_destroy(client,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch "4.4.0", "Lost connection to relay server");
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch return FALSE;
b5d61c3f6ead1284cb54b6f30e3e907f9c8a7ffeStephan Bosch /* RFC 4954, Section 6: 530 5.7.0 Authentication required
b5d61c3f6ead1284cb54b6f30e3e907f9c8a7ffeStephan Bosch
b5d61c3f6ead1284cb54b6f30e3e907f9c8a7ffeStephan Bosch This response SHOULD be returned by any command other than AUTH,
b5d61c3f6ead1284cb54b6f30e3e907f9c8a7ffeStephan Bosch EHLO, HELO, NOOP, RSET, or QUIT when server policy requires
b5d61c3f6ead1284cb54b6f30e3e907f9c8a7ffeStephan Bosch authentication in order to perform the requested action and
b5d61c3f6ead1284cb54b6f30e3e907f9c8a7ffeStephan Bosch authentication is not currently in force. */
b5d61c3f6ead1284cb54b6f30e3e907f9c8a7ffeStephan Bosch case 530:
b5d61c3f6ead1284cb54b6f30e3e907f9c8a7ffeStephan Bosch i_error("Relay server requires authentication: %s",
b5d61c3f6ead1284cb54b6f30e3e907f9c8a7ffeStephan Bosch smtp_reply_log(reply));
b5d61c3f6ead1284cb54b6f30e3e907f9c8a7ffeStephan Bosch client_destroy(client, "4.3.5",
b5d61c3f6ead1284cb54b6f30e3e907f9c8a7ffeStephan Bosch "Internal error occurred. "
b5d61c3f6ead1284cb54b6f30e3e907f9c8a7ffeStephan Bosch "Refer to server log for more information.");
b5d61c3f6ead1284cb54b6f30e3e907f9c8a7ffeStephan Bosch return FALSE;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch default:
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch break;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch }
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (!smtp_reply_has_enhanced_code(reply)) {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch reply_r->enhanced_code =
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch SMTP_REPLY_ENH_CODE(reply->status / 100, 0, 0);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch }
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch return TRUE;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch}