Skip to content

Commit

Permalink
lib-smtp: client: Allow connecting to SMTP/LMTP services offered thro…
Browse files Browse the repository at this point in the history
…ugh unix sockets.
  • Loading branch information
stephanbosch authored and villesavolainen committed Feb 12, 2019
1 parent 28a96e0 commit deb8c48
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 13 deletions.
68 changes: 56 additions & 12 deletions src/lib-smtp/smtp-client-connection.c
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/lib-smtp/smtp-client-connection.h
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/lib-smtp/smtp-client-private.h
Expand Up @@ -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;

Expand Down

0 comments on commit deb8c48

Please sign in to comment.