Rearrange stuff

This commit is contained in:
Juan José Gutiérrez de Quevedo Pérez 2025-03-25 10:59:11 +01:00 committed by Juanjo Gutiérrez
parent 518f578298
commit d7e2aee3a3
No known key found for this signature in database
GPG key ID: 2EE7726C7CA75D4E
21 changed files with 20 additions and 139 deletions

68
include/Database.h Normal file
View file

@ -0,0 +1,68 @@
/**
* hermes antispam proxy
* Copyright (C) 2006, 2007 Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* @author Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
*/
#ifndef DATABASE_H
#define DATABASE_H
#include "hermes.h"
#include <stdlib.h>
#include <iostream>
#include <string>
#include <sqlite3.h>
#include <pthread.h>
#include "Utils.h"
using namespace std;
/**
* this class implements an interface to the sqlite3 database
*/
class Database
{
private:
string dbfile;
sqlite3 *dbh;
void _open();
int countRows(string);
void doQuery(string);
unsigned long getIntValue(string&);
public:
Database();
~Database();
void setDatabaseFile(string);
static string cleanString(string);
void init();
void open();
void close();
bool greylisted(string,string,string,int,int,int);
bool whitelistedIP(string);
bool whitelistedHostname(string);
bool whitelistedTO(string);
bool whitelistedDomain(string);
bool blacklistedTO(string);
bool blacklistedToDomain(string);
bool blacklistedIP(string);
bool blacklistedFROM(string);
bool allowedDomainPerIP(string,string);
unsigned long cleanDB();
};
#endif //DATABASE_H

64
include/Exception.h Normal file
View file

@ -0,0 +1,64 @@
/**
* hermes antispam proxy
* Copyright (C) 2006, 2007 Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* @author Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
*/
#ifndef EXCEPTION_H
#define EXCEPTION_H
#include <iostream>
#include <string>
#include "Configfile.h"
#include "Logger.h"
using namespace std;
class Exception
{
private:
string error;
string file;
unsigned line;
void notifyByEmail(string);
public:
Exception(string,string,unsigned);
operator string();
friend ostream& operator<<(ostream&,Exception&);
};
class NetworkException:public Exception
{
public:
NetworkException(string p_error,string p_file,int p_line):Exception(p_error,p_file,p_line){}
};
class SQLException:public Exception
{
public:
SQLException(string p_error,string p_file,int p_line):Exception(p_error,p_file,p_line){}
};
class NotifyException
{
private:
string error;
public:
NotifyException(string p_error){ error=p_error; }
operator string(){ return "NotifyException: "+error;};
};
#endif //EXCEPTION_H

57
include/FileLogger.h Normal file
View file

@ -0,0 +1,57 @@
/**
* hermes antispam proxy
* Copyright (C) 2006, 2007 Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* @author Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
*/
#ifndef FILELOGGER_H
#define FILELOGGER_H
#include <stdio.h>
#include <pthread.h>
#include <string>
#include <list>
#include "Logger.h"
#include "Configfile.h"
using namespace std;
/**
* this class implements a logger that writes to a file
*
* @see Logger
*/
class FileLogger: public Logger
{
unsigned char linecount;
time_t last_rotation;
private:
FILE *f;
pthread_mutex_t mutex;
list<string> tmpstrings;
void openFile(string);
void closeFile();
void syncBufferToDisk();
void rotateLog();
string getProcessedRotateFilename();
public:
FileLogger();
~FileLogger();
void addMessage(string,int,int,string);
};
#endif //FILELOGGER_H

49
include/Logger.h Normal file
View file

@ -0,0 +1,49 @@
/**
* hermes antispam proxy
* Copyright (C) 2006, 2007 Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* @author Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
*/
#ifndef LOGGER_H
#define LOGGER_H
#include <string>
using namespace std;
/**
* logger base class
*/
class Logger
{
public:
virtual ~Logger(){}; //empty destructor, not creating anything
virtual void addMessage(string,int,int,string)=0;
};
#ifndef WIN32
#include "UnixLogger.h"
#endif //WIN32
#ifndef HERMES_LOG_INFO
#define HERMES_LOG_ERR 0
#define HERMES_LOG_INFO 1
#define HERMES_LOG_DEBUG 2
#endif //HERMES_LOG_INFO
#include "FileLogger.h"
#include "NullLogger.h"
#endif //LOGGER_H

41
include/NullLogger.h Normal file
View file

@ -0,0 +1,41 @@
/**
* hermes antispam proxy
* Copyright (C) 2006, 2007 Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* @author Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
*/
#ifndef NULLLOGGER_H
#define NULLLOGGER_H
#include <string>
#include "Logger.h"
using namespace std;
/**
* this logger implements a null logger
* messages added to this logger go nowhere
*
* @see Logger
*/
class NullLogger: public Logger
{
public:
void addMessage(string,int,int,string){}; //ignore messages
};
#endif //NULLLOGGER_H

19
include/Proxy.h Normal file
View file

@ -0,0 +1,19 @@
// Proxy.h
#pragma once
#include <string>
#include "SocketInterface.h"
#define SMTP_STATE_WAIT_FOR_HELO 0
#define SMTP_STATE_WAIT_FOR_MAILFROM 1
#define SMTP_STATE_WAIT_FOR_RCPTTO 2
#define SMTP_STATE_WAIT_FOR_DATA 3
class Proxy {
public:
Proxy(SocketInterface* outside_socket) : outside(outside_socket) {}
void run(std::string& peer_address);
private:
SocketInterface* outside;
};

56
include/ServerSocket.h Normal file
View file

@ -0,0 +1,56 @@
/**
* hermes antispam proxy
* Copyright (C) 2006, 2007 Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* @author Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
*/
#ifndef SERVERSOCKET_H
#define SERVERSOCKET_H
#include <sys/types.h>
#ifdef WIN32
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <netdb.h>
#include <libintl.h>
#endif //WIN32
#include <iostream>
#include "hermes.h"
#include "Socket.h"
/**
* implement the server specific methods for socket, mainly listen() and accept()
* @see Socket
*/
class ServerSocket: public Socket
{
private:
unsigned int port;
string listen_ip;
public:
ServerSocket(){};
~ServerSocket(){};
void setPort(unsigned int);
void setListenIP(string&);
void listen();
void listen(unsigned int,string);
int accept(string *);
};
#endif //SERVERSOCKET_H

106
include/Socket.h Normal file
View file

@ -0,0 +1,106 @@
/**
* hermes antispam proxy
* Copyright (C) 2006, 2007 Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* @author Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
*/
#ifndef SOCKET_H
#define SOCKET_H
#include "hermes.h"
#include <iostream>
#include <string>
#include <sstream>
#include <sys/types.h>
#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#ifndef AI_ADDRCONFIG
#define AI_ADDRCONFIG 0 //if the windows we are compiling at is old, define it as 0
#endif //AI_ADDRCONFIG
#else
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#endif //WIN32
#include <errno.h>
#ifdef HAVE_SSL
#include <openssl/ssl.h>
#include <openssl/rand.h>
#endif //HAVE_SSL
//this is a bit of a hack
//if a system doesn't have MSG_NOSIGNAL then we define it as 0 (that is, no options selected)
//I've tried this on Solaris and NetBSD and it seems to work. Still, recheck in the future (TODO)
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif //MSG_NOSIGNAL
#include "Configfile.h"
#include "Utils.h"
using namespace std;
/**
* implements a socket class independent of operating system and that supports ssl
*/
class Socket
{
protected:
int fd;
private:
// bool closed;
static int created_sockets;
#ifdef HAVE_SSL
bool ssl_enabled;
SSL *ssl;
static SSL_CTX *ssl_ctx_client;
static SSL_CTX *ssl_ctx_server;
#endif //HAVE_SSL
public:
Socket();
~Socket();
#ifdef HAVE_SSL
bool is_ssl_enabled();
void prepareSSL(bool);
void startSSL(bool);
#endif //HAVE_SSL
void setFD(int);
bool canRead(float);
bool connect(string,unsigned int);
int getFD();
static struct sockaddr resolve(string);
static string resolveToString(string);
static string resolveInverselyToString(string);
void init();
void close();
//reading and writing
char readByte();
ssize_t readBytes(void *,ssize_t);
string readLine();
void writeByte(char);
void writeBytes(void *,ssize_t);
void writeLine(string);
bool isClosed();
void setTimeout(float,float);
};
#endif //SOCKET_H

16
include/SocketInterface.h Normal file
View file

@ -0,0 +1,16 @@
// SocketInterface.h
#pragma once
#include <string>
class SocketInterface {
public:
virtual ~SocketInterface() = default;
virtual void connect(const std::string& host, unsigned short port) = 0;
virtual void writeLine(const std::string& data) = 0;
virtual std::string readLine() = 0;
virtual bool canRead(double timeout) = 0;
virtual bool isClosed() = 0;
virtual void close() = 0;
virtual void prepareSSL(bool incoming) = 0;
virtual void startSSL(bool incoming) = 0;
};

44
include/Spf.h Normal file
View file

@ -0,0 +1,44 @@
/**
* hermes antispam proxy
* Copyright (C) 2006, 2007 Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* @author Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
*/
#ifndef SPF_H
#define SPF_H
#include "hermes.h"
extern "C"
{
#include <spf2/spf.h>
}
class Spf
{
private:
static SPF_server_t *spfserver;
SPF_request_t *spfrequest;
SPF_response_t *spfresponse;
pthread_mutex_t mutex;
public:
Spf();
~Spf();
static void deinitialize();
bool query(const string&, const string&, const string&);
};
#endif //SPF_H

43
include/UnixLogger.h Normal file
View file

@ -0,0 +1,43 @@
/**
* hermes antispam proxy
* Copyright (C) 2006, 2007 Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* @author Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
*/
#ifndef UNIXLOGGER_H
#define UNIXLOGGER_H
#include <syslog.h>
#include <string>
#include "Logger.h"
using namespace std;
/**
* implements the logger for Linux/UNIX
*
* @see Logger
*/
class UnixLogger: public Logger
{
public:
UnixLogger();
~UnixLogger();
void addMessage(string,int,int,string);
};
#endif //UNIXLOGGER_H

88
include/Utils.h Normal file
View file

@ -0,0 +1,88 @@
/**
* hermes antispam proxy
* Copyright (C) 2006, 2007 Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* @author Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
*/
#ifndef UTILS_H
#define UTILS_H
#include "hermes.h"
#include <string>
#include <sstream>
#include <iostream>
#include <dirent.h>
#include <time.h>
#ifndef WIN32
#include <pwd.h>
#include <grp.h>
#endif //WIN32
#include "Database.h"
#include "Socket.h"
using namespace std;
#ifdef WIN32
#define sleep(x) Sleep(1000*(x))
#endif //WIN32
/*#ifndef MAX
#define MAX(x,y) (((x)>(y))?(x):(y))
#endif //MAX*/
/**
* this class implements common utilities
*/
class Utils
{
public:
//string utilities
static string strtolower(string);
static string trim(string);
static string inttostr(int);
static string ulongtostr(unsigned long);
//email-related utilities
static string getmail(string&);
static string getdomain(string&);
static string reverseip(string&);
//spam-related utilities (TODO: move to a different class)
static bool greylist(string,string&,string&,string&);
static bool listed_on_dns_lists(list<string>&,unsigned char,string&);
static bool whitelisted(string,string&);
static bool blacklisted(string,string&,string&);
#ifndef WIN32
//posix-utils
static int usertouid(string);
static int grouptogid(string);
#endif //WIN32
//misc
static string get_canonical_filename(string);
static bool file_exists(string);
static bool dir_exists(string);
static string errnotostrerror(int);
static string rfc2821_date(time_t *timestamp=NULL);
static string gethostname();
static void write_pid(string,pid_t);
static string gethostname(int s);
};
#endif //UTILS_H

41
include/hermes.h Normal file
View file

@ -0,0 +1,41 @@
/**
* hermes antispam proxy
* Copyright (C) 2006, 2007 Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* @author Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
*/
#ifndef SMTPPROXY_H
#define SMTPPROXY_H
#include "Exception.h"
#include <assert.h>
#include <string>
#define _(x) (x)
#define LOG(x,y) hermes_log.addMessage(__FILE__,__LINE__,x,y)
#define LERR(x) LOG(HERMES_LOG_ERR,x)
#define LINF(x) LOG(HERMES_LOG_INFO,x)
#define LDEB(x) LOG(HERMES_LOG_DEBUG,x)
typedef struct
{
int new_fd;
std::string peer_address;
unsigned long connection_id;
}new_conn_info;
#endif //SMTPPROXY_H