From: Stephan Bosch Date: Sun, 23 Sep 2018 17:43:05 +0000 (+0200) Subject: lib-smtp: client: Allow connecting to SMTP/LMTP services offered through unix sockets. X-Git-Tag: 2.3.5~118 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=deb8c48b9d360e05dc0165bddef9d02d92b5f639;p=thirdparty%2Fdovecot%2Fcore.git lib-smtp: client: Allow connecting to SMTP/LMTP services offered through unix sockets. --- diff --git a/src/lib-smtp/smtp-client-connection.c b/src/lib-smtp/smtp-client-connection.c index 2f320b9c14..e037514156 100644 --- a/src/lib-smtp/smtp-client-connection.c +++ b/src/lib-smtp/smtp-client-connection.c @@ -64,8 +64,13 @@ const char * smpt_client_connection_label(struct smtp_client_connection *conn) { if (conn->label == NULL) { - conn->label = i_strdup_printf("%s:%u [%u]", - conn->host, conn->port, conn->id); + if (conn->path == NULL) { + conn->label = i_strdup_printf("%s:%u [%u]", + conn->host, conn->port, conn->id); + } else { + conn->label = i_strdup_printf("unix:%s [%u]", + conn->path, conn->id); + } } return conn->label; } @@ -1530,6 +1535,20 @@ smtp_client_connection_connect_next_ip(struct smtp_client_connection *conn) smtp_client_connection_do_connect(conn); } +static void +smtp_client_connection_connect_unix(struct smtp_client_connection *conn) +{ + timeout_remove(&conn->to_connect); + + smtp_client_connection_debug(conn, + "Connecting to socket %s", conn->path); + + connection_init_client_unix(conn->client->conn_list, &conn->conn, + conn->path); + + smtp_client_connection_do_connect(conn); +} + static void smtp_client_connection_delayed_host_lookup_failure( struct smtp_client_connection *conn) @@ -1646,20 +1665,28 @@ void smtp_client_connection_connect(struct smtp_client_connection *conn, smtp_client_connection_set_state(conn, SMTP_CLIENT_CONNECTION_STATE_CONNECTING); - smtp_client_connection_lookup_ip(conn); - if (conn->ips_count == 0) - return; + if (conn->path == NULL) { + smtp_client_connection_lookup_ip(conn); + if (conn->ips_count == 0) + return; - /* always work asynchronously */ - timeout_remove(&conn->to_connect); - conn->to_connect = timeout_add(0, - smtp_client_connection_connect_next_ip, conn); + /* always work asynchronously */ + timeout_remove(&conn->to_connect); + conn->to_connect = timeout_add(0, + smtp_client_connection_connect_next_ip, conn); + } else { + /* always work asynchronously */ + timeout_remove(&conn->to_connect); + conn->to_connect = timeout_add(0, + smtp_client_connection_connect_unix, conn); + } } static const struct connection_settings smtp_client_connection_set = { .input_max_size = (size_t)-1, .output_max_size = (size_t)-1, - .client = TRUE + .client = TRUE, + .delayed_unix_client_connected_callback = TRUE }; static const struct connection_vfuncs smtp_client_connection_vfuncs = { @@ -1810,8 +1837,6 @@ smtp_client_connection_do_create(struct smtp_client *client, const char *name, connection_init(conn->client->conn_list, &conn->conn); - smtp_client_connection_debug(conn, "Connection created"); - return conn; } @@ -1829,6 +1854,8 @@ smtp_client_connection_create(struct smtp_client *client, conn->port = port; conn->ssl_mode = ssl_mode; + smtp_client_connection_debug(conn, "Connection created"); + return conn; } @@ -1851,6 +1878,23 @@ smtp_client_connection_create_ip(struct smtp_client *client, return conn; } +struct smtp_client_connection * +smtp_client_connection_create_unix(struct smtp_client *client, + enum smtp_protocol protocol, + const char *path, + const struct smtp_client_settings *set) +{ + struct smtp_client_connection *conn; + const char *name = t_strconcat("unix:", path, NULL); + + conn = smtp_client_connection_do_create(client, name, protocol, set); + conn->path = p_strdup(conn->pool, path); + + smtp_client_connection_debug(conn, "Connection created"); + + return conn; +} + void smtp_client_connection_ref(struct smtp_client_connection *conn) { i_assert(conn->refcount >= 0); diff --git a/src/lib-smtp/smtp-client-connection.h b/src/lib-smtp/smtp-client-connection.h index 2c9cce3fb3..08eb3d30f9 100644 --- a/src/lib-smtp/smtp-client-connection.h +++ b/src/lib-smtp/smtp-client-connection.h @@ -47,6 +47,13 @@ smtp_client_connection_create_ip(struct smtp_client *client, const char *hostname, enum smtp_client_connection_ssl_mode ssl_mode, const struct smtp_client_settings *set) ATTR_NULL(5,7); +struct smtp_client_connection * +smtp_client_connection_create_unix(struct smtp_client *client, + enum smtp_protocol protocol, + const char *path, + const struct smtp_client_settings *set) + ATTR_NULL(4); + void smtp_client_connection_ref(struct smtp_client_connection *conn); void smtp_client_connection_unref(struct smtp_client_connection **_conn); void smtp_client_connection_close(struct smtp_client_connection **_conn); diff --git a/src/lib-smtp/smtp-client-private.h b/src/lib-smtp/smtp-client-private.h index aa61460664..df4aa4e916 100644 --- a/src/lib-smtp/smtp-client-private.h +++ b/src/lib-smtp/smtp-client-private.h @@ -145,7 +145,7 @@ struct smtp_client_connection { char *label; enum smtp_protocol protocol; - const char *host; + const char *path, *host; in_port_t port; enum smtp_client_connection_ssl_mode ssl_mode;