Skip to content

Commit

Permalink
Merge pull request #273 from loomy/feature-lmtp
Browse files Browse the repository at this point in the history
first version of lmtp feature
  • Loading branch information
dinhvh committed Apr 21, 2017
2 parents 5164ba2 + e2742e5 commit d4244c3
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 10 deletions.
31 changes: 31 additions & 0 deletions src/data-types/connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,34 @@ int mail_tcp_connect_with_local_address(const char * server, uint16_t port,
return mail_tcp_connect_with_local_address_timeout(server, port, local_address, local_port, 0);
}

#ifndef WIN32
#include <sys/un.h>
int mail_unix_connect_socket(const char *path)
{
struct sockaddr_un sa;
int s;

if (!(memcpy(sa.sun_path, path, strlen(path)))) {
return -1;
}
sa.sun_family = AF_UNIX;


if (0 > (s = socket(AF_UNIX, SOCK_STREAM, 0)))
return -1;

if (prepare_fd(s))
goto close_socket;
if (connect(s, (struct sockaddr *) &sa, sizeof(struct sockaddr_un)))
goto close_socket;
return s;

close_socket:
close(s);
return -1;
}
#endif

int mail_tcp_connect_with_local_address_timeout(const char * server, uint16_t port,
const char * local_address, uint16_t local_port, time_t timeout)
{
Expand All @@ -200,6 +228,9 @@ int mail_tcp_connect_with_local_address_timeout(const char * server, uint16_t po
#else
int s;
int r;

if ('/' == server[0])
return mail_unix_connect_socket(server);
#endif

#ifndef HAVE_IPV6
Expand Down
92 changes: 92 additions & 0 deletions src/low-level/smtp/mailsmtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,65 @@ int mailsmtp_data_message(mailsmtp * session,
}
}

int mailsmtp_status(int smtpstatus)
{
switch(smtpstatus) {
case 250:
return MAILSMTP_NO_ERROR;

case 552:
return MAILSMTP_ERROR_EXCEED_STORAGE_ALLOCATION;

case 554:
return MAILSMTP_ERROR_TRANSACTION_FAILED;

case 451:
return MAILSMTP_ERROR_IN_PROCESSING;

case 452:
return MAILSMTP_ERROR_INSUFFICIENT_SYSTEM_STORAGE;

case 0:
return MAILSMTP_ERROR_STREAM;

default:
return MAILSMTP_ERROR_UNEXPECTED_CODE;
}

}

/* lmtp data command
* on the end of data transaction lmtp returns a status for
* every recipient it delivered.
* recipient_list holds the rcpts from smtp_rcpt()
* the last error is returned to the caller
* if retcodes is not NULL, the individual rcpt-stati are stored there
* */
int maillmtp_data_message(mailsmtp * session,
const char * message,
size_t size,
clist * recipient_list,
int * retcodes)
{
int r, ret = MAILSMTP_NO_ERROR;
unsigned int i = 0;
clistiter *iter;

r = send_data(session, message, size);

if (r == -1)
return MAILSMTP_ERROR_STREAM;

for (iter = clist_begin(recipient_list); iter; iter = clist_next(iter)) {
r = read_response(session);
if (MAILSMTP_NO_ERROR != mailsmtp_status(r))
ret = mailsmtp_status(r);
if (retcodes)
retcodes[i++] = r;
}
return ret;
}

int mailsmtp_data_message_quit(mailsmtp * session,
const char * message,
size_t size)
Expand Down Expand Up @@ -673,6 +732,39 @@ int mailesmtp_ehlo_with_ip(mailsmtp * session, int useip)
}
}

/* lmtp processing */
int mailesmtp_lhlo(mailsmtp * session , const char *hostname)
{
int r;
char command[SMTP_STRING_SIZE];

if (!hostname)
hostname = "localhost";

snprintf(command, SMTP_STRING_SIZE, "LHLO %s\r\n", hostname);
r = send_command(session, command);
if (r == -1)
return MAILSMTP_ERROR_STREAM;
r = read_response(session);

switch (r) {
case 250:
return mailesmtp_parse_ehlo(session);

case 504:
return MAILSMTP_ERROR_NOT_IMPLEMENTED;

case 550:
return MAILSMTP_ERROR_ACTION_NOT_TAKEN;

case 0:
return MAILSMTP_ERROR_STREAM;

default:
return MAILSMTP_ERROR_UNEXPECTED_CODE;
}
}

/*
if return_full is TRUE, the entire message is returned on error
envid can be NULL
Expand Down
10 changes: 10 additions & 0 deletions src/low-level/smtp/mailsmtp.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ int mailsmtp_data_message(mailsmtp * session,
const char * message,
size_t size);

LIBETPAN_EXPORT
int maillmtp_data_message(mailsmtp * session,
const char * message,
size_t size,
clist * recipient_list,
int * retcodes);

LIBETPAN_EXPORT
int mailsmtp_data_message_quit(mailsmtp * session,
const char * message,
Expand All @@ -115,6 +122,9 @@ int mailsmtp_data_message_quit_no_disconnect(mailsmtp * session,
const char * message,
size_t size);

LIBETPAN_EXPORT
int mailesmtp_lhlo(mailsmtp * session, const char *hostname);

LIBETPAN_EXPORT
int mailesmtp_ehlo(mailsmtp * session);

Expand Down
43 changes: 33 additions & 10 deletions tests/smtpsend.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ char *smtp_from;
int smtp_tls = 0;
int smtp_esmtp = 1;
int smtp_ssl = 0;
int smtp_lmtp = 0;

struct mem_message {
char *data;
Expand Down Expand Up @@ -145,10 +146,12 @@ void release(struct mem_message *message) {

int send_message(char *data, size_t len, char**rcpts) {
int s = -1;
int ret;
int ret, i;
char **r;
int esmtp = 0;
mailsmtp *smtp = NULL;
int *retcodes = NULL;
clist *recipients = clist_new();

if ((smtp = mailsmtp_new(0, NULL)) == NULL) {
perror("mailsmtp_new");
Expand All @@ -175,7 +178,9 @@ int send_message(char *data, size_t len, char**rcpts) {
}

/* then introduce ourselves */
if (smtp_esmtp && (ret = mailesmtp_ehlo(smtp)) == MAILSMTP_NO_ERROR)
if (smtp_lmtp)
ret = mailesmtp_lhlo(smtp, "lmtp-test");
else if (smtp_esmtp && (ret = mailesmtp_ehlo(smtp)) == MAILSMTP_NO_ERROR)
esmtp = 1;
else if (!smtp_esmtp || ret == MAILSMTP_ERROR_NOT_IMPLEMENTED)
ret = mailsmtp_helo(smtp);
Expand All @@ -192,7 +197,9 @@ int send_message(char *data, size_t len, char**rcpts) {

if (esmtp && smtp_tls) {
/* introduce ourselves again */
if (smtp_esmtp && (ret = mailesmtp_ehlo(smtp)) == MAILSMTP_NO_ERROR)
if (smtp_lmtp)
ret = mailesmtp_lhlo(smtp, "lmtp-test");
else if (smtp_esmtp && (ret = mailesmtp_ehlo(smtp)) == MAILSMTP_NO_ERROR)
esmtp = 1;
else if (!smtp_esmtp || ret == MAILSMTP_ERROR_NOT_IMPLEMENTED)
ret = mailsmtp_helo(smtp);
Expand Down Expand Up @@ -227,24 +234,36 @@ int send_message(char *data, size_t len, char**rcpts) {
mailsmtp_rcpt(smtp, *r))) != MAILSMTP_NO_ERROR) {
fprintf(stderr, "mailsmtp_rcpt: %s: %s\n", *r, mailsmtp_strerror(ret));
goto error;
}else{
clist_append(recipients, *r);
}
}

/* message */
if ((ret = mailsmtp_data(smtp)) != MAILSMTP_NO_ERROR) {
fprintf(stderr, "mailsmtp_data: %s\n", mailsmtp_strerror(ret));
goto error;
}
if ((ret = mailsmtp_data_message(smtp, data, len)) != MAILSMTP_NO_ERROR) {
fprintf(stderr, "mailsmtp_data_message: %s\n", mailsmtp_strerror(ret));
goto error;
if (smtp_lmtp){
retcodes = malloc((clist_count(recipients) * sizeof(int)));
if ((ret = maillmtp_data_message(smtp, data, len, recipients, retcodes)) != MAILSMTP_NO_ERROR) {
fprintf(stderr, "maillmtp_data: %s (%d)\n", mailsmtp_strerror(ret), ret);
goto error;
}
for (i = 0; i < clist_count(recipients); i++)
fprintf(stderr, "recipient %s returned: %d\n", (char *)clist_nth_data(recipients, i), retcodes[i]);
} else if ((ret = mailsmtp_data_message(smtp, data, len)) != MAILSMTP_NO_ERROR) {
fprintf(stderr, "mailsmtp_data_message: %s\n", mailsmtp_strerror(ret));
goto error;
}
mailsmtp_free(smtp);
return 0;

error:
if (smtp != NULL)
mailsmtp_free(smtp);
free(retcodes);
clist_free(recipients);
if (s >= 0)
close(s);
return -1;
Expand All @@ -266,14 +285,15 @@ int main(int argc, char **argv) {
{"tls", 0, 0, 'S'},
{"no-esmtp", 0, 0, 'E'},
{"ssl", 0, 0, 'L'},
{"lmtp", 0, 0, 'T'},
};
#endif

while(1) {
#if HAVE_GETOPT_LONG
r = getopt_long(argc, argv, "s:p:u:v:f:SEL", long_options, &indx);
r = getopt_long(argc, argv, "s:p:u:v:f:SELT", long_options, &indx);
#else
r = getopt(argc, argv, "s:p:u:v:f:SEL");
r = getopt(argc, argv, "s:p:u:v:f:SELT");
#endif
if (r < 0)
break;
Expand Down Expand Up @@ -310,14 +330,17 @@ int main(int argc, char **argv) {
case 'L':
smtp_ssl = 1;
break;
case 'T':
smtp_lmtp = 1;
break;
}
}

argc -= optind;
argv += optind;

if (argc < 1) {
fprintf(stderr, "usage: smtpsend [-f from] [-u user] [-v password] [-s server] [-p port] [-S] [-L] <rcpts>...\n");
fprintf(stderr, "usage: smtpsend [-f from] [-u user] [-v password] [-s server] [-p port] [-S] [-L] [-T] <rcpts>...\n");
return EXIT_FAILURE;
}

Expand Down

0 comments on commit d4244c3

Please sign in to comment.