bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen#include "lib.h"
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen#include "ostream.h"
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen#include "str.h"
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen#include "strescape.h"
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen#include "master-service.h"
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen#include "log-error-buffer.h"
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen#include "doveadm-connection.h"
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen#include <unistd.h>
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainenstruct doveadm_connection {
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen struct log_error_buffer *errorbuf;
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen int fd;
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen struct ostream *output;
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen};
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainenstatic void doveadm_connection_destroy(struct doveadm_connection **_conn);
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainenstatic int doveadm_connection_send_errors(struct doveadm_connection *conn)
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen{
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen struct log_error_buffer_iter *iter;
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen const struct log_error *error;
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen string_t *str = t_str_new(256);
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen int ret = 0;
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen iter = log_error_buffer_iter_init(conn->errorbuf);
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen while ((error = log_error_buffer_iter_next(iter)) != NULL) {
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen str_truncate(str, 0);
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen str_printfa(str, "%s\t%ld\t",
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen failure_log_type_names[error->type],
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen (long)error->timestamp);
d03a871a77f8ec36f48f5fea98d810e51b186fdbTimo Sirainen str_append_tabescaped(str, error->prefix);
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen str_append_c(str, '\t');
d03a871a77f8ec36f48f5fea98d810e51b186fdbTimo Sirainen str_append_tabescaped(str, error->text);
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen str_append_c(str, '\n');
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen if (o_stream_send(conn->output,
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen str_data(str), str_len(str)) < 0) {
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen ret = -1;
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen break;
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen }
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen }
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen log_error_buffer_iter_deinit(&iter);
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen return ret;
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen}
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainenstatic int doveadm_output(struct doveadm_connection *conn)
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen{
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen int ret;
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen if ((ret = o_stream_flush(conn->output)) != 0) {
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen /* error / finished */
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen doveadm_connection_destroy(&conn);
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen }
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen return 1;
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen}
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainenvoid doveadm_connection_create(struct log_error_buffer *errorbuf, int fd)
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen{
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen struct doveadm_connection *conn;
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen conn = i_new(struct doveadm_connection, 1);
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen conn->errorbuf = errorbuf;
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen conn->fd = fd;
e93184a9055c2530366dfe617e07199603c399ddMartti Rannanjärvi conn->output = o_stream_create_fd(conn->fd, (size_t)-1);
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen if (doveadm_connection_send_errors(conn) < 0)
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen doveadm_connection_destroy(&conn);
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen else {
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen o_stream_set_flush_callback(conn->output, doveadm_output, conn);
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen o_stream_set_flush_pending(conn->output, TRUE);
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen }
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen}
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainenstatic void doveadm_connection_destroy(struct doveadm_connection **_conn)
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen{
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen struct doveadm_connection *conn = *_conn;
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen *_conn = NULL;
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen o_stream_destroy(&conn->output);
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen if (close(conn->fd) < 0)
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen i_error("close(doveadm connection) failed: %m");
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen i_free(conn);
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen master_service_client_connection_destroyed(master_service);
6a3c23e577be41f7521995c5dae19abb2fb4ffb6Timo Sirainen}