bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschstatic struct mail_storage_service_ctx *storage_service;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschstatic struct master_login *master_login = NULL;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschsubmission_client_created_func_t *hook_client_created = NULL;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschsubmission_client_created_hook_set(submission_client_created_func_t *new_hook)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch submission_client_created_func_t *old_hook = hook_client_created;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch str_append(title, client_state_get_name(client));
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch str_printfa(title, "%u connections", submission_client_count);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch /* do nothing. submission connections typically die pretty quick anyway.
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschsend_error(int fd_out, const char *hostname, const char *error_code,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch "421 4.3.2 %s Shutting down due to fatal error\r\n",
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 if (mail_storage_service_lookup_next(storage_service, input,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch set = mail_storage_service_user_get_set(user)[1];
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch *error_r = "No relay host configured for submission proxy "
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch "(submission_relay_host is unset)";
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch /* parse input data */
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (input_buf != NULL && input_buf->used > 0) {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch /* NOTE: actually, pipelining the AUTH command is stricly
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch speaking not allowed, but we support it anyway.
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch (void)client_create(fd_in, fd_out, input->session_id, mail_user,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschstatic void main_stdio_run(const char *username)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch input.username = username != NULL ? username : getenv("USER");
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (input.username == NULL && IS_STANDALONE())
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (client_create_from_input(&input, STDIN_FILENO, STDOUT_FILENO,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschlogin_client_connected(const struct master_login_client *login_client,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch const char *username, const char *const *extra_fields)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch enum mail_auth_request_flags flags = login_client->auth_req.flags;
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 if ((flags & MAIL_AUTH_REQUEST_FLAG_CONN_SECURED) != 0)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if ((flags & MAIL_AUTH_REQUEST_FLAG_CONN_SSL_SECURED) != 0)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch buffer_create_from_const_data(&input_buf, login_client->data,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (client_create_from_input(&input, login_client->fd, login_client->fd,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch master_service_client_connection_destroyed(master_service);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschstatic void login_client_failed(const struct master_login_client *client,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch "421 4.3.2 %s Shutting down due to fatal error\r\n",
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (write(client->fd, msg, strlen(msg)) < 0) {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch /* ignored */
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Boschstatic void client_connected(struct master_service_connection *conn)
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch /* when running standalone, we shouldn't even get here */
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch master_service_client_connection_accept(conn);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch static const struct setting_parser_info *set_roots[] = {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch enum mail_storage_service_flags storage_service_flags = 0;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch const char *username = NULL, *auth_socket_path = "auth-master";
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch login_set.postlogin_timeout_secs = MASTER_POSTLOGIN_TIMEOUT_DEFAULT;
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 service_flags |= MASTER_SERVICE_FLAG_STANDALONE |
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch service_flags |= MASTER_SERVICE_FLAG_KEEP_CONFIG_OPEN;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch master_service = master_service_init("submission", service_flags,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch while ((c = master_getopt(master_service)) > 0) {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch if (t_abspath(auth_socket_path, &login_set.auth_socket_path,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch i_fatal("t_abspath(%s) failed: %s", auth_socket_path,
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch &login_set.postlogin_socket_path, &error) < 0) {
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch login_set.failure_callback = login_client_failed;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch master_service_set_die_callback(master_service, submission_die);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch /* initialize SMTP server */
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch smtp_server_set.capabilities = SMTP_CAPABILITY_DSN;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch smtp_server_set.protocol = SMTP_PROTOCOL_SMTP;
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch smtp_server = smtp_server_init(&smtp_server_set);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch smtp_server_command_register(smtp_server, "BURL", cmd_burl, 0);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch /* initialize SMTP client */
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch smtp_client_set.my_hostname = my_hostdomain();
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch smtp_client = smtp_client_init(&smtp_client_set);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch /* fake that we're running, so we know if client was destroyed
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch while handling its initial input */
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch master_login = master_login_init(master_service, &login_set);
2cbbe9b4829adb184c83dbf780316f4144559054Stephan Bosch master_service_run(master_service, client_connected);