bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "submission-common.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "buffer.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "str.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "istream.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "ostream.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "array.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "base64.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "hostpid.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "path-util.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "process-title.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "restrict-access.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "fd-util.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "master-service.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "master-login.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "master-service-settings.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "master-interface.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "var-expand.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "mail-error.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "mail-user.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "mail-storage-service.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "smtp-server.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "smtp-client.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include "submission-commands.h"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include <stdio.h>
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#include <unistd.h>
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#define LMTP_MASTER_FIRST_LISTEN_FD 3
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch#define IS_STANDALONE() \
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch (getenv(MASTER_IS_PARENT_ENV) == NULL)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschstruct smtp_server *smtp_server = NULL;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschstruct smtp_client *smtp_client = NULL;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschstatic bool verbose_proctitle = FALSE;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschstatic struct mail_storage_service_ctx *storage_service;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschstatic struct master_login *master_login = NULL;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschsubmission_client_created_func_t *hook_client_created = NULL;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschbool submission_debug = FALSE;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschsubmission_client_created_func_t *
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschsubmission_client_created_hook_set(submission_client_created_func_t *new_hook)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch{
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch submission_client_created_func_t *old_hook = hook_client_created;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch hook_client_created = new_hook;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch return old_hook;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch}
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschvoid submission_refresh_proctitle(void)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch{
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch struct client *client;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch string_t *title = t_str_new(128);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (!verbose_proctitle)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch return;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch str_append_c(title, '[');
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch switch (submission_client_count) {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch case 0:
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch str_append(title, "idling");
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch break;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch case 1:
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch client = submission_clients;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch str_append(title, client->user->username);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (client->user->conn.remote_ip != NULL) {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch str_append_c(title, ' ');
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch str_append(title,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch net_ip2addr(client->user->conn.remote_ip));
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch }
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch str_append_c(title, ' ');
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch str_append(title, client_state_get_name(client));
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch break;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch default:
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch str_printfa(title, "%u connections", submission_client_count);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch break;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch }
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch str_append_c(title, ']');
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch process_title_set(str_c(title));
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch}
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschstatic void submission_die(void)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch{
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch /* do nothing. submission connections typically die pretty quick anyway.
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch */
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch}
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschstatic void
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschsend_error(int fd_out, const char *hostname, const char *error_code,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch const char *error_msg)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch{
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch const char *msg;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch msg = t_strdup_printf("451 %s %s\r\n"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch "421 4.3.2 %s Shutting down due to fatal error\r\n",
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch error_code, error_msg, hostname);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (write(fd_out, msg, strlen(msg)) < 0) {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (errno != EAGAIN && errno != EPIPE)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch i_error("write(client) failed: %m");
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch }
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch}
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschstatic int
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschclient_create_from_input(const struct mail_storage_service_input *input,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch int fd_in, int fd_out, const buffer_t *input_buf,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch const char **error_r)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch{
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch struct mail_storage_service_user *user;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch struct mail_user *mail_user;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch const struct submission_settings *set;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch const char *helo = NULL;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch const unsigned char *data;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch size_t data_len;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (mail_storage_service_lookup_next(storage_service, input,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch &user, &mail_user, error_r) <= 0) {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch send_error(fd_out, my_hostname,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch "4.7.0", MAIL_ERRSTR_CRITICAL_MSG);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch return -1;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch }
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch restrict_access_allow_coredumps(TRUE);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch set = mail_storage_service_user_get_set(user)[1];
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (set->submission_relay_host == NULL ||
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch *set->submission_relay_host == '\0') {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch *error_r = "No relay host configured for submission proxy "
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch "(submission_relay_host is unset)";
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch send_error(fd_out, set->hostname,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch "4.3.5", MAIL_ERRSTR_CRITICAL_MSG);
9cb0d95152a758e35e5835b4205cfcfafba2fa63Timo Sirainen mail_user_unref(&mail_user);
9cb0d95152a758e35e5835b4205cfcfafba2fa63Timo Sirainen mail_storage_service_user_unref(&user);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch return -1;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch }
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (set->verbose_proctitle)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch verbose_proctitle = TRUE;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch /* parse input data */
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch data = NULL;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch data_len = 0;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (input_buf != NULL && input_buf->used > 0) {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch size_t len = input_buf->used, helo_len = 0;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch data = input_buf->data;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (len > 0) {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (*data == '\0') {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch helo_len = 1;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch } else {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch helo = t_strndup(data, len);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch helo_len = strlen(helo) + 1;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch }
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch }
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch /* NOTE: actually, pipelining the AUTH command is stricly
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch speaking not allowed, but we support it anyway.
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch */
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (len > helo_len) {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch data = data + helo_len;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch data_len = len - helo_len;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch }
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch }
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch (void)client_create(fd_in, fd_out, input->session_id, mail_user,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch user, set, helo, data, data_len);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch return 0;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch}
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschstatic void main_stdio_run(const char *username)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch{
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch struct mail_storage_service_input input;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch buffer_t *input_buf;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch const char *value, *error, *input_base64;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch i_zero(&input);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch input.module = input.service = "submission";
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch input.username = username != NULL ? username : getenv("USER");
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (input.username == NULL && IS_STANDALONE())
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch input.username = getlogin();
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (input.username == NULL)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch i_fatal("USER environment missing");
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if ((value = getenv("IP")) != NULL)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch (void)net_addr2ip(value, &input.remote_ip);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if ((value = getenv("LOCAL_IP")) != NULL)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch (void)net_addr2ip(value, &input.local_ip);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch input_base64 = getenv("CLIENT_INPUT");
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch input_buf = input_base64 == NULL ? NULL :
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch t_base64_decode_str(input_base64);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (client_create_from_input(&input, STDIN_FILENO, STDOUT_FILENO,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch input_buf, &error) < 0)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch i_fatal("%s", error);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch}
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschstatic void
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschlogin_client_connected(const struct master_login_client *login_client,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch const char *username, const char *const *extra_fields)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch{
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch struct mail_storage_service_input input;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch enum mail_auth_request_flags flags = login_client->auth_req.flags;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch const char *error;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch buffer_t input_buf;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch i_zero(&input);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch input.module = input.service = "submission";
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch input.local_ip = login_client->auth_req.local_ip;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch input.remote_ip = login_client->auth_req.remote_ip;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch input.local_port = login_client->auth_req.local_port;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch input.remote_port = login_client->auth_req.remote_port;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch input.username = username;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch input.userdb_fields = extra_fields;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch input.session_id = login_client->session_id;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if ((flags & MAIL_AUTH_REQUEST_FLAG_CONN_SECURED) != 0)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch input.conn_secured = TRUE;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if ((flags & MAIL_AUTH_REQUEST_FLAG_CONN_SSL_SECURED) != 0)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch input.conn_ssl_secured = TRUE;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch buffer_create_from_const_data(&input_buf, login_client->data,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch login_client->auth_req.data_size);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (client_create_from_input(&input, login_client->fd, login_client->fd,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch &input_buf, &error) < 0) {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch int fd = login_client->fd;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch i_error("%s", error);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch i_close_fd(&fd);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch master_service_client_connection_destroyed(master_service);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch }
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch}
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschstatic void login_client_failed(const struct master_login_client *client,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch const char *errormsg)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch{
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch const char *msg;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch msg = t_strdup_printf("451 4.7.0 %s\r\n"
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch "421 4.3.2 %s Shutting down due to fatal error\r\n",
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch errormsg, my_hostname);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (write(client->fd, msg, strlen(msg)) < 0) {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch /* ignored */
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch }
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch}
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschstatic void client_connected(struct master_service_connection *conn)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch{
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch /* when running standalone, we shouldn't even get here */
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch i_assert(master_login != NULL);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch master_service_client_connection_accept(conn);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch master_login_add(master_login, conn->fd);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch}
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschint main(int argc, char *argv[])
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch{
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch static const struct setting_parser_info *set_roots[] = {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch &submission_setting_parser_info,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch NULL
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch };
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch struct master_login_settings login_set;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch enum master_service_flags service_flags = 0;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch enum mail_storage_service_flags storage_service_flags = 0;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch struct smtp_server_settings smtp_server_set;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch struct smtp_client_settings smtp_client_set;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch const char *username = NULL, *auth_socket_path = "auth-master";
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch const char *error;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch int c;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch i_zero(&login_set);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch login_set.postlogin_timeout_secs = MASTER_POSTLOGIN_TIMEOUT_DEFAULT;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch login_set.request_auth_token = TRUE;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (IS_STANDALONE() && getuid() == 0 &&
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch net_getpeername(1, NULL, NULL) == 0) {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch printf("421 5.3.5 The submission binary must not be started "
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch "from inetd, use submission-login instead.\r\n");
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch return 1;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch }
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (IS_STANDALONE()) {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch service_flags |= MASTER_SERVICE_FLAG_STANDALONE |
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch MASTER_SERVICE_FLAG_STD_CLIENT;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch } else {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch service_flags |= MASTER_SERVICE_FLAG_KEEP_CONFIG_OPEN;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch }
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch master_service = master_service_init("submission", service_flags,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch &argc, &argv, "a:Dt:u:");
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch while ((c = master_getopt(master_service)) > 0) {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch switch (c) {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch case 'a':
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch auth_socket_path = optarg;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch break;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch case 't':
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (str_to_uint(optarg,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch &login_set.postlogin_timeout_secs) < 0 ||
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch login_set.postlogin_timeout_secs == 0)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch i_fatal("Invalid -t parameter: %s", optarg);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch break;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch case 'u':
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch storage_service_flags |=
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch MAIL_STORAGE_SERVICE_FLAG_USERDB_LOOKUP;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch username = optarg;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch break;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch case 'D':
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch submission_debug = TRUE;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch break;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch default:
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch return FATAL_DEFAULT;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch }
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch }
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (t_abspath(auth_socket_path, &login_set.auth_socket_path,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch &error) < 0) {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch i_fatal("t_abspath(%s) failed: %s", auth_socket_path,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch error);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch }
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (argv[optind] != NULL) {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (t_abspath(argv[optind],
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch &login_set.postlogin_socket_path, &error) < 0) {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch i_fatal("t_abspath(%s) failed: %s",
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch argv[optind], error);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch }
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch }
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch login_set.callback = login_client_connected;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch login_set.failure_callback = login_client_failed;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch master_service_init_finish(master_service);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch master_service_set_die_callback(master_service, submission_die);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch storage_service =
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch mail_storage_service_init(master_service,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch set_roots, storage_service_flags);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch /* initialize SMTP server */
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch i_zero(&smtp_server_set);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch smtp_server_set.capabilities = SMTP_CAPABILITY_DSN;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch smtp_server_set.protocol = SMTP_PROTOCOL_SMTP;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch smtp_server_set.max_pipelined_commands = 5;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch smtp_server_set.debug = submission_debug;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch smtp_server = smtp_server_init(&smtp_server_set);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch smtp_server_command_register(smtp_server, "BURL", cmd_burl, 0);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch /* initialize SMTP client */
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch i_zero(&smtp_client_set);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch smtp_client_set.my_hostname = my_hostdomain();
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch smtp_client_set.debug = submission_debug;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch smtp_client = smtp_client_init(&smtp_client_set);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch /* fake that we're running, so we know if client was destroyed
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch while handling its initial input */
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch io_loop_set_running(current_ioloop);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (IS_STANDALONE()) {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch T_BEGIN {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch main_stdio_run(username);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch } T_END;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch } else {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch master_login = master_login_init(master_service, &login_set);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch io_loop_set_running(current_ioloop);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch }
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (io_loop_is_running(current_ioloop))
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch master_service_run(master_service, client_connected);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch clients_destroy_all();
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch smtp_client_deinit(&smtp_client);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch smtp_server_deinit(&smtp_server);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (master_login != NULL)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch master_login_deinit(&master_login);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch mail_storage_service_deinit(&storage_service);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch master_service_deinit(&master_service);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch return 0;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch}