]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-smtp: client: Allow connecting to SMTP/LMTP services offered through unix sockets.
authorStephan Bosch <stephan.bosch@dovecot.fi>
Sun, 23 Sep 2018 17:43:05 +0000 (19:43 +0200)
committerVille Savolainen <ville.savolainen@dovecot.fi>
Tue, 12 Feb 2019 13:41:05 +0000 (15:41 +0200)
src/lib-smtp/smtp-client-connection.c
src/lib-smtp/smtp-client-connection.h
src/lib-smtp/smtp-client-private.h

index 2f320b9c149b9e032b121b6bab4ac2b0c0cd06a1..e0375141567978c970d96c27900c7494f88b1699 100644 (file)
@@ -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);
index 2c9cce3fb30402f165b3aa74633d7b45f539f1d5..08eb3d30f90c1b4231d841400ffbced5cf197d28 100644 (file)
@@ -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);
index aa61460664095ec3e8ea7f0e1f773a79fa40ad7b..df4aa4e91689a8061fc247d1b820e9636cd160ff 100644 (file)
@@ -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;