2016-10-23 22:04:55 +02:00
|
|
|
/*
|
|
|
|
* piler-smtp.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <signal.h>
|
2016-11-06 22:16:03 +01:00
|
|
|
#include <sys/epoll.h>
|
2016-10-23 22:04:55 +02:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <piler.h>
|
|
|
|
|
2016-10-26 21:26:20 +02:00
|
|
|
#define PROGNAME "piler-smtp"
|
2016-10-23 22:04:55 +02:00
|
|
|
|
|
|
|
extern char *optarg;
|
|
|
|
extern int optind;
|
2016-11-06 22:16:03 +01:00
|
|
|
|
|
|
|
struct epoll_event event, *events=NULL;
|
|
|
|
int num_connections = 0;
|
2016-10-23 22:04:55 +02:00
|
|
|
int listenerfd = -1;
|
|
|
|
|
|
|
|
char *configfile = CONFIG_FILE;
|
2017-08-08 15:34:45 +02:00
|
|
|
struct config cfg;
|
2016-10-23 22:04:55 +02:00
|
|
|
struct passwd *pwd;
|
2016-10-30 22:53:49 +01:00
|
|
|
struct smtp_session *session, **sessions=NULL;
|
2020-12-27 23:40:39 +01:00
|
|
|
struct smtp_acl *smtp_acl[MAXHASH];
|
2016-10-23 22:04:55 +02:00
|
|
|
|
|
|
|
|
2017-11-30 10:32:14 +01:00
|
|
|
void usage(){
|
|
|
|
printf("\nusage: piler\n\n");
|
|
|
|
printf(" -c <config file> Config file to use if not the default\n");
|
|
|
|
printf(" -d Fork to the background\n");
|
|
|
|
printf(" -v Return the version and build number\n");
|
|
|
|
printf(" -V Return the version and some build parameters\n");
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-13 14:50:49 +01:00
|
|
|
void p_clean_exit(int sig){
|
2018-03-17 17:32:32 +01:00
|
|
|
if(sig > 0) syslog(LOG_PRIORITY, "got signal: %d, %s", sig, strsignal(sig));
|
2018-03-13 14:50:49 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
if(listenerfd != -1) close(listenerfd);
|
|
|
|
|
2016-12-22 10:46:11 +01:00
|
|
|
if(sessions){
|
2020-08-08 21:28:09 +02:00
|
|
|
for(int i=0; i<cfg.max_connections; i++){
|
2016-12-22 10:46:11 +01:00
|
|
|
if(sessions[i]) free_smtp_session(sessions[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(sessions);
|
2016-10-23 22:04:55 +02:00
|
|
|
}
|
|
|
|
|
2016-11-06 22:16:03 +01:00
|
|
|
if(events) free(events);
|
2016-10-30 22:53:49 +01:00
|
|
|
|
2020-12-27 23:40:39 +01:00
|
|
|
clear_smtp_acl(smtp_acl);
|
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
syslog(LOG_PRIORITY, "%s has been terminated", PROGNAME);
|
|
|
|
|
|
|
|
ERR_free_strings();
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void fatal(char *s){
|
|
|
|
syslog(LOG_PRIORITY, "%s", s);
|
2018-03-13 14:50:49 +01:00
|
|
|
p_clean_exit(0);
|
2016-10-23 22:04:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void check_for_client_timeout(){
|
|
|
|
time_t now;
|
|
|
|
|
2019-12-15 15:08:16 +01:00
|
|
|
time(&now);
|
|
|
|
|
|
|
|
if(cfg.verbosity >= LOG_DEBUG) syslog(LOG_PRIORITY, "%s @%ld", __func__, now);
|
2016-10-23 22:04:55 +02:00
|
|
|
|
2019-12-15 15:08:16 +01:00
|
|
|
if(num_connections > 0){
|
2020-08-08 21:28:09 +02:00
|
|
|
for(int i=0; i<cfg.max_connections; i++){
|
2016-11-06 22:16:03 +01:00
|
|
|
if(sessions[i] && now - sessions[i]->lasttime >= cfg.smtp_timeout){
|
2019-12-15 15:08:16 +01:00
|
|
|
syslog(LOG_PRIORITY, "client %s timeout, lasttime: %ld", sessions[i]->remote_host, sessions[i]->lasttime);
|
2016-11-12 10:48:08 +01:00
|
|
|
tear_down_session(sessions, sessions[i]->slot, &num_connections);
|
2016-10-23 22:04:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 20:47:04 +01:00
|
|
|
alarm(cfg.check_for_client_timeout_interval);
|
2016-10-23 22:04:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void initialise_configuration(){
|
|
|
|
cfg = read_config(configfile);
|
|
|
|
|
|
|
|
if(strlen(cfg.username) > 1){
|
|
|
|
pwd = getpwnam(cfg.username);
|
|
|
|
if(!pwd) fatal(ERR_NON_EXISTENT_USER);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(getuid() == 0 && pwd){
|
|
|
|
check_and_create_directories(&cfg, pwd->pw_uid, pwd->pw_gid);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(chdir(cfg.workdir)){
|
|
|
|
syslog(LOG_PRIORITY, "workdir: *%s*", cfg.workdir);
|
|
|
|
fatal(ERR_CHDIR);
|
|
|
|
}
|
|
|
|
|
|
|
|
setlocale(LC_MESSAGES, cfg.locale);
|
|
|
|
setlocale(LC_CTYPE, cfg.locale);
|
|
|
|
|
2020-12-27 23:40:39 +01:00
|
|
|
load_smtp_acl(smtp_acl);
|
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
syslog(LOG_PRIORITY, "reloaded config: %s", configfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv){
|
2020-08-08 21:28:09 +02:00
|
|
|
int client_sockfd;
|
|
|
|
int i, daemonise=0;
|
2016-10-23 22:04:55 +02:00
|
|
|
int client_len = sizeof(struct sockaddr_storage);
|
2016-11-12 10:48:08 +01:00
|
|
|
ssize_t readlen;
|
2016-10-23 22:04:55 +02:00
|
|
|
struct sockaddr_storage client_address;
|
2016-11-06 22:16:03 +01:00
|
|
|
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
|
2016-10-23 22:04:55 +02:00
|
|
|
char readbuf[BIGBUFSIZE];
|
2016-11-06 22:16:03 +01:00
|
|
|
int efd;
|
2016-10-23 22:04:55 +02:00
|
|
|
|
|
|
|
while((i = getopt(argc, argv, "c:dvVh")) > 0){
|
|
|
|
switch(i){
|
|
|
|
|
|
|
|
case 'c' :
|
|
|
|
configfile = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd' :
|
|
|
|
daemonise = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v' :
|
|
|
|
case 'V' :
|
|
|
|
printf("%s build %d\n", VERSION, get_build());
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 'h' :
|
2019-12-15 15:08:16 +01:00
|
|
|
default :
|
2017-11-30 10:32:14 +01:00
|
|
|
usage();
|
2016-10-23 22:04:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-26 21:26:20 +02:00
|
|
|
(void) openlog(PROGNAME, LOG_PID, LOG_MAIL);
|
2016-10-23 22:04:55 +02:00
|
|
|
|
|
|
|
initialise_configuration();
|
|
|
|
|
2016-11-06 22:16:03 +01:00
|
|
|
listenerfd = create_and_bind(cfg.listen_addr, cfg.listen_port);
|
2016-10-23 22:04:55 +02:00
|
|
|
if(listenerfd == -1){
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-11-10 21:25:42 +01:00
|
|
|
if(make_socket_non_blocking(listenerfd) == -1){
|
|
|
|
fatal("make_socket_non_blocking()");
|
|
|
|
}
|
|
|
|
|
2016-11-06 22:16:03 +01:00
|
|
|
if(listen(listenerfd, cfg.backlog) == -1){
|
|
|
|
fatal("ERROR: listen()");
|
|
|
|
}
|
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
if(drop_privileges(pwd)) fatal(ERR_SETUID);
|
|
|
|
|
2016-11-06 22:16:03 +01:00
|
|
|
efd = epoll_create1(0);
|
|
|
|
if(efd == -1){
|
|
|
|
fatal("ERROR: epoll_create()");
|
|
|
|
}
|
|
|
|
|
|
|
|
event.data.fd = listenerfd;
|
|
|
|
event.events = EPOLLIN | EPOLLET;
|
|
|
|
if(epoll_ctl(efd, EPOLL_CTL_ADD, listenerfd, &event) == -1){
|
|
|
|
fatal("ERROR: epoll_ctl() on efd");
|
|
|
|
}
|
|
|
|
|
2016-10-30 22:53:49 +01:00
|
|
|
set_signal_handler(SIGINT, p_clean_exit);
|
2018-03-13 14:50:49 +01:00
|
|
|
set_signal_handler(SIGTERM, p_clean_exit);
|
|
|
|
set_signal_handler(SIGKILL, p_clean_exit);
|
|
|
|
set_signal_handler(SIGSEGV, p_clean_exit);
|
2018-03-06 11:59:24 +01:00
|
|
|
|
2018-03-17 17:32:32 +01:00
|
|
|
set_signal_handler(SIGPIPE, SIG_IGN);
|
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
set_signal_handler(SIGALRM, check_for_client_timeout);
|
|
|
|
set_signal_handler(SIGHUP, initialise_configuration);
|
|
|
|
|
2016-10-30 22:53:49 +01:00
|
|
|
// calloc() initialitizes the allocated memory
|
|
|
|
|
|
|
|
sessions = calloc(cfg.max_connections, sizeof(struct smtp_session));
|
2016-11-06 22:16:03 +01:00
|
|
|
events = calloc(cfg.max_connections, sizeof(struct epoll_event));
|
2016-10-30 22:53:49 +01:00
|
|
|
|
2016-11-06 22:16:03 +01:00
|
|
|
if(!sessions || !events) fatal("ERROR: calloc()");
|
2016-10-23 22:04:55 +02:00
|
|
|
|
|
|
|
SSL_library_init();
|
|
|
|
SSL_load_error_strings();
|
|
|
|
|
|
|
|
srand(getpid());
|
|
|
|
|
2016-10-26 21:26:20 +02:00
|
|
|
syslog(LOG_PRIORITY, "%s %s, build %d starting", PROGNAME, VERSION, get_build());
|
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
#if HAVE_DAEMON == 1
|
|
|
|
if(daemonise == 1 && daemon(1, 0) == -1) fatal(ERR_DAEMON);
|
|
|
|
#endif
|
|
|
|
|
2019-11-27 20:47:04 +01:00
|
|
|
alarm(cfg.check_for_client_timeout_interval);
|
2017-11-05 16:17:56 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
for(;;){
|
2020-08-08 21:28:09 +02:00
|
|
|
int n = epoll_wait(efd, events, cfg.max_connections, -1);
|
2016-11-06 22:16:03 +01:00
|
|
|
for(i=0; i<n; i++){
|
|
|
|
|
2017-11-05 08:35:01 +01:00
|
|
|
// Office365 sometimes behaves oddly: when it receives the 250 OK
|
|
|
|
// message after sending the email, it doesn't send the QUIT command
|
|
|
|
// rather it aborts the connection
|
|
|
|
|
2016-11-06 22:16:03 +01:00
|
|
|
if((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN))){
|
2017-11-01 07:54:16 +01:00
|
|
|
if(cfg.verbosity >= _LOG_EXTREME) syslog(LOG_PRIORITY, "ERROR: the remote end hung up without sending QUIT");
|
2017-11-05 08:35:01 +01:00
|
|
|
session = get_session_by_socket(sessions, cfg.max_connections, events[i].data.fd);
|
|
|
|
if(session)
|
|
|
|
tear_down_session(sessions, session->slot, &num_connections);
|
|
|
|
else
|
|
|
|
close(events[i].data.fd);
|
2016-11-06 22:16:03 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have 1 or more incoming connections to process
|
2016-10-23 22:04:55 +02:00
|
|
|
|
2016-11-06 22:16:03 +01:00
|
|
|
else if(listenerfd == events[i].data.fd){
|
2016-10-23 22:04:55 +02:00
|
|
|
|
2016-11-06 22:16:03 +01:00
|
|
|
while(1){
|
2016-10-23 22:04:55 +02:00
|
|
|
|
|
|
|
client_sockfd = accept(listenerfd, (struct sockaddr *)&client_address, (socklen_t *)&client_len);
|
2016-11-06 22:16:03 +01:00
|
|
|
if(client_sockfd == -1){
|
|
|
|
if((errno == EAGAIN) || (errno == EWOULDBLOCK)){
|
|
|
|
// We have processed all incoming connections
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
2018-05-03 13:08:05 +02:00
|
|
|
syslog(LOG_PRIORITY, "ERROR: accept(): '%s'", strerror(errno));
|
2016-11-06 22:16:03 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(getnameinfo((struct sockaddr *)&client_address, client_len, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0){
|
2017-11-04 15:34:34 +01:00
|
|
|
// Strictly speaking it's not correct to log num_connections+1 connections
|
|
|
|
// but it still gives a good clue how many connections we have at the moment
|
2018-05-13 16:48:20 +02:00
|
|
|
syslog(LOG_PRIORITY, "connected from %s:%s on fd=%d (active connections: %d)", hbuf, sbuf, client_sockfd, num_connections + 1);
|
2016-11-06 22:16:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(make_socket_non_blocking(client_sockfd) == -1){
|
|
|
|
syslog(LOG_PRIORITY, "ERROR: cannot make the socket non blocking");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
event.data.fd = client_sockfd;
|
|
|
|
event.events = EPOLLIN | EPOLLET;
|
|
|
|
if(epoll_ctl(efd, EPOLL_CTL_ADD, client_sockfd, &event) == -1){
|
|
|
|
syslog(LOG_PRIORITY, "ERROR: epoll_ctl() on client_sockfd");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-12-27 23:40:39 +01:00
|
|
|
start_new_session(sessions, client_sockfd, &num_connections, smtp_acl, hbuf, &cfg);
|
2016-10-23 22:04:55 +02:00
|
|
|
}
|
|
|
|
|
2016-11-06 22:16:03 +01:00
|
|
|
continue;
|
|
|
|
}
|
2016-10-23 22:04:55 +02:00
|
|
|
|
|
|
|
|
2016-11-06 22:16:03 +01:00
|
|
|
// handle data from an existing connection
|
2016-10-23 22:04:55 +02:00
|
|
|
|
2016-11-06 22:16:03 +01:00
|
|
|
else {
|
|
|
|
int done = 0;
|
2016-10-23 22:04:55 +02:00
|
|
|
|
2016-11-12 10:48:08 +01:00
|
|
|
session = get_session_by_socket(sessions, cfg.max_connections, events[i].data.fd);
|
2016-11-06 22:16:03 +01:00
|
|
|
if(session == NULL){
|
|
|
|
syslog(LOG_PRIORITY, "ERROR: cannot find session for this socket: %d", events[i].data.fd);
|
|
|
|
close(events[i].data.fd);
|
|
|
|
continue;
|
|
|
|
}
|
2016-10-23 22:04:55 +02:00
|
|
|
|
2016-11-06 22:16:03 +01:00
|
|
|
time(&(session->lasttime));
|
2016-10-23 22:04:55 +02:00
|
|
|
|
2016-11-06 22:16:03 +01:00
|
|
|
while(1){
|
|
|
|
memset(readbuf, 0, sizeof(readbuf));
|
2016-10-23 22:04:55 +02:00
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
if(session->net.use_ssl == 1)
|
|
|
|
readlen = SSL_read(session->net.ssl, (char*)&readbuf[0], sizeof(readbuf)-1);
|
2016-11-06 22:16:03 +01:00
|
|
|
else
|
2016-11-12 10:48:08 +01:00
|
|
|
readlen = read(events[i].data.fd, (char*)&readbuf[0], sizeof(readbuf)-1);
|
2016-10-23 22:04:55 +02:00
|
|
|
|
2017-11-01 07:54:16 +01:00
|
|
|
if(cfg.verbosity >= _LOG_EXTREME && readlen > 0) syslog(LOG_PRIORITY, "got %ld bytes to read", readlen);
|
2016-10-23 22:04:55 +02:00
|
|
|
|
2016-11-12 10:48:08 +01:00
|
|
|
if(readlen == -1){
|
2016-11-06 22:16:03 +01:00
|
|
|
/* If errno == EAGAIN, that means we have read all data. So go back to the main loop. */
|
|
|
|
if(errno != EAGAIN){
|
|
|
|
done = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-11-12 10:48:08 +01:00
|
|
|
else if(readlen == 0){
|
2016-11-06 22:16:03 +01:00
|
|
|
/* End of file. The remote has closed the connection. */
|
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-02-20 15:59:21 +01:00
|
|
|
handle_data(session, &readbuf[0], readlen, &cfg);
|
2016-11-06 22:16:03 +01:00
|
|
|
|
|
|
|
if(session->protocol_state == SMTP_STATE_BDAT && session->bad == 1){
|
2016-11-12 10:48:08 +01:00
|
|
|
done = 1;
|
2016-11-06 22:16:03 +01:00
|
|
|
break;
|
2016-10-23 22:04:55 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-06 22:16:03 +01:00
|
|
|
|
2017-01-18 09:38:30 +01:00
|
|
|
/* Don't wait until the remote client closes the connection after he sent the QUIT command */
|
|
|
|
|
|
|
|
if(done || session->protocol_state == SMTP_STATE_FINISHED){
|
2016-11-12 10:48:08 +01:00
|
|
|
tear_down_session(sessions, session->slot, &num_connections);
|
2016-11-06 22:16:03 +01:00
|
|
|
}
|
2016-10-23 22:04:55 +02:00
|
|
|
}
|
2016-11-06 22:16:03 +01:00
|
|
|
|
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|