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

Manage unix socket #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions doc/nullmailer-send.8
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ Set the SMTP authentication password.
.BI source= HOSTNAME
Set the source address for connections to the remote host.
.TP
.BI unix-socket=\fISOCKETNAME
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that since this is a .BI line, the \fI is unnecessary.

Set the unix socket to connect to. When this option is set, neither
.B port
nor
.B source
can be used, and
.B hostname
is ignored.
.TP
.B auth-login
Force SMTP "AUTH LOGIN" mode instead of auto-detecting.
.TP
Expand Down
2 changes: 1 addition & 1 deletion lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ libmisc_a_SOURCES = \
canonicalize.h canonicalize.cc \
configio.h config_path.cc \
config_read.cc config_readlist.cc config_readint.cc config_syserr.cc \
connect.h tcpconnect.cc \
connect.h socketconnect.cc \
defines.h \
errcodes.h errcodes.cc \
hostname.h hostname.cc \
Expand Down
1 change: 1 addition & 0 deletions lib/connect.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
#define NULLMAILER_CONNECT__H__

extern int tcpconnect(const char* hostname, int port, const char* source);
extern int unixconnect(const char* unix_socket);

#endif // NULLMAILER_CONNECT__H__
17 changes: 17 additions & 0 deletions lib/tcpconnect.cc → lib/socketconnect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <netdb.h>
#include <string.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
Expand Down Expand Up @@ -173,3 +174,19 @@ int tcpconnect(const char* hostname, int port, const char* source)
}

#endif

int unixconnect(const char* unix_socket)
{
struct sockaddr_un sa;
memset(&sa, 0, sizeof(sa));
sa.sun_family = AF_UNIX;
strncpy(sa.sun_path, unix_socket, sizeof(sa.sun_path) - 1);
int s = socket(PF_UNIX, SOCK_STREAM, 0);
if(s == -1)
return -ERR_SOCKET;
if(connect(s, (sockaddr*)&sa, sizeof(sa)) != 0) {
close(s);
return err_return(errno, ERR_CONN_FAILED);
}
return s;
}
27 changes: 18 additions & 9 deletions protocols/protocol.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ int port = 0;
int auth_method = AUTH_DETECT;
int use_tls = 0;
int use_starttls = 0;
const char* unix_socket = 0;
const char* remote = 0;
const char* source = 0;
const char* cli_help_suffix = "";
const char* cli_args_usage = "< options 3< mail-file";
const int cli_args_min = 0;
const int cli_args_max = 0;
cli_option cli_options[] = {
{ 0, "unix-socket", cli_option::string, 0, &unix_socket,
"Set the path to the unix socket for the remote", 0 },
{ 0, "host", cli_option::string, 0, &remote,
"Set the hostname for the remote", 0 },
{ 'p', "port", cli_option::integer, 0, &port,
Expand Down Expand Up @@ -134,17 +137,23 @@ static void parse_options(void)
int cli_main(int, char*[])
{
parse_options();
if (remote == 0)
protocol_fail(ERR_USAGE, "Remote host not set");
if (port == 0)
port = use_tls ? default_tls_port : default_port;
if (port < 0)
protocol_fail(ERR_USAGE, "Invalid value for port");
if (use_tls || use_starttls)
tls_init(remote);
if (unix_socket == 0) {
if (remote == 0)
protocol_fail(ERR_USAGE, "Remote host not set");
if (port == 0)
port = use_tls ? default_tls_port : default_port;
if (port < 0)
protocol_fail(ERR_USAGE, "Invalid value for port");
if (use_tls || use_starttls)
tls_init(remote);
}
else if (port != 0 || source != 0)
protocol_fail(ERR_USAGE, "Both INET and UNIX socket specified");
fdibuf in(3, true);
protocol_prep(in);
int fd = tcpconnect(remote, port, source);
int fd = unix_socket == 0
? tcpconnect(remote, port, source)
: unixconnect(unix_socket);
if(fd < 0)
protocol_fail(-fd, "Connect failed");
if (use_tls)
Expand Down