Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve unix sockets support #180

Open
keenser opened this issue Nov 7, 2022 · 3 comments
Open

improve unix sockets support #180

keenser opened this issue Nov 7, 2022 · 3 comments

Comments

@keenser
Copy link

keenser commented Nov 7, 2022

Hi, please add support for socket only listeners
Currently 'socket' configuration option takes effect only with enabled 'port' or 'tls_port' and if there is no socket file already exists in filesystem
POC patch:

+++ b/src/server.c
@@ -407,6 +407,10 @@ static int dm_bind_and_listen(int sock, struct sockaddr *saddr, socklen_t len, i
                err = errno;
                TRACE(TRACE_EMERG, "setsockopt::error [%s]", strerror(err));
        }
+       if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &so_reuseaddress, sizeof(so_reuseaddress)) == -1) {
+               err = errno;
+               TRACE(TRACE_EMERG, "setsockopt::error [%s]", strerror(err));
+       }
        /* bind the address */
        if ((bind(sock, saddr, len)) == -1) {
                err = errno;
@@ -442,6 +446,7 @@ static int create_unix_socket(ServerConfig_T * conf)
 
        TRACE(TRACE_DEBUG, "create socket [%s] backlog [%d]", conf->socket, conf->backlog);
 
+       unlink(conf->socket);
        // any error in dm_bind_and_listen is fatal
        dm_bind_and_listen(sock, (struct sockaddr *)&un, sizeof(un), conf->backlog, FALSE);
        
@@ -796,7 +801,7 @@ int server_run(ServerConfig_T *conf)
        if (server_setup(conf))
                return -1;
 
-       if (strlen(conf->port) || strlen(conf->ssl_port)) {
+       if (strlen(conf->port) || strlen(conf->ssl_port) || strlen(conf->socket)) {
 
                if (MATCH(conf->service_name, "HTTP")) {
                        int port = atoi(conf->port);
@alan-hicks
Copy link
Member

Hi Keenser,

I'm confused as to your use case and what you are trying to accomplish. Clients using DBMail services expect ip:port based connections.

Although your patch doesn't apply using the current branch, setsockopt (@@ -407,6 +407,10) appears to duplicate the same four lines, and unlink (@@ -442,6 +446,7) is already in server_close_sockets so I'm unsure why you would add it to create_unix_socket.

@keenser
Copy link
Author

keenser commented Nov 8, 2022

Hi Alan-hicks,
I'm trying to use socket-only setup for dbmail-lmtpd service togehter with postfix (mailbox_transport = lmtp:unix:/dbmail/lmtp.sock) running on the same host. Listening inet port via dbmail-lmtpd is not required in this sittuation))

As I can see current implementation of dm_bind_and_listen uses setsockopt only for SO_REUSEADDR
and (@@ -407,6 +407,10) adds SO_REUSEPORT property

You are right that server_close_sockets already has unlink, but problem still appers if effective_user and effective_group used in configuration
socket file created with uid 0, while server_close_sockets executes with unprivilated user configured in effective_user and unlink failes to remove socket file in my setup
We can change uid/gid just after file creation in tail of create_unix_socket but this will not cover some scenarios when server_close_sockets can't be executed (killall -9 dbmail-lmtpd, server power failure , etc)

@alan-hicks
Copy link
Member

Thanks for the clear use case and issues you are facing. This is a useful enhancement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants