This commit is contained in:
Wolfgang Hottgenroth 2013-03-27 23:09:15 +01:00
parent 4582de6afc
commit f30026e6b0
6 changed files with 174 additions and 6 deletions

View File

@ -6,6 +6,7 @@
#include "Database.hpp"
#include "PostfixPolicyProtocol.hpp"
#include "ChildProt.hpp"
#include "log.hpp"
using namespace std;
@ -13,27 +14,64 @@ using namespace std;
int main() {
clog.rdbuf(new Log("childProt", LOG_MAIL));
string msg;
string result;
PostfixPolicyProtocol postfixPolicyProtocol;
try {
postfixPolicyProtocol.processInput();
cerr << postfixPolicyProtocol.getSender() << endl;
cerr << postfixPolicyProtocol.getRecipient() << endl;
msg = "sender=" + postfixPolicyProtocol.getSender() + "recipient=" + postfixPolicyProtocol.getRecipient();
DatabaseHandle databaseHandle(DATABASE_FILE);
// SELECT id, delegate FROM child_v WHERE address = '%s'
string stmtChildTxt = "SELECT id, delegate FROM child_v WHERE address = '" + postfixPolicyProtocol.getRecipient() + "'";
PreparedStatement pStmtChild = databaseHandle.prepareStatement(stmtChildTxt);
result_t child = pStmtChild.next();
if (! child.empty()) {
string sender = postfixPolicyProtocol.getSender();
string::size_type atIdx = sender.find('@');
string domain;
if (string::npos != atIdx) {
domain = sender.substr(atIdx);
} else {
domain = "UNKNOWN";
}
string stmtWhitelistTxt = "SELECT * FROM whitelist_t WHERE child = '" +
child["id"] +
"' AND (address = '" +
sender +
"' OR address = '" +
domain +
"')";
PreparedStatement pStmtWhitelist = databaseHandle.prepareStatement(stmtWhitelistTxt);
result_t whitelistEntry = pStmtWhitelist.next();
if (whitelistEntry.empty()) {
msg += ", not found, redirect to " + child["delegate"];
result = "REDIRECT " + child["delegate"];
} else {
msg += ", found, pass by";
result = "OK";
}
} else {
msg += ", ignored";
result = "OK";
}
} catch (InvalidAddressTuple& iat) {
cerr << "InvalidAddressTuple" << endl;
msg = "InvalidAddressTuple";
result = "OK";
} catch (DatabaseError &de) {
cerr << "DatabaseError" << endl;
msg = "DatabaseError";
result = "OK";
}
clog << msg << endl;
postfixPolicyProtocol.sendResult(result);
}

View File

@ -11,5 +11,33 @@ DatabaseHandle::~DatabaseHandle() {
sqlite3_close(dbh);
}
PreparedStatement DatabaseHandle::prepareStatement(const std::string& statement) {
return PreparedStatement(dbh, statement);
}
PreparedStatement::PreparedStatement(sqlite3 *dbh, const std::string& statement) {
const char *tail;
int rc = sqlite3_prepare_v2(dbh, statement.c_str(), -1, &stmt, &tail);
if (SQLITE_OK != rc)
throw DatabaseError();
}
PreparedStatement::~PreparedStatement() {
sqlite3_finalize(stmt);
}
result_t PreparedStatement::next() {
int rc = sqlite3_step(stmt);
int ncol = sqlite3_column_count(stmt);
result_t result;
if (SQLITE_ROW == rc) {
for (int i = 0; i < ncol; i++) {
string key = sqlite3_column_name(stmt, i);
string value = string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, i)));
result.insert(pair<string, string>(key, value));
}
}
return result;
}

View File

@ -2,17 +2,34 @@
#define DATABASE_HPP_
#include <string>
#include <map>
#include <sqlite3.h>
class DatabaseError {};
typedef std::map<std::string, std::string> result_t;
// resource management class
class PreparedStatement {
public:
friend class DatabaseHandle;
~PreparedStatement();
result_t next();
private:
PreparedStatement(sqlite3 *dbh, const std::string& statement);
sqlite3_stmt *stmt;
};
// resource management class
class DatabaseHandle {
public:
DatabaseHandle(const std::string& filename);
~DatabaseHandle();
sqlite3 *getHandle() { return dbh; }
PreparedStatement prepareStatement(const std::string& statement);
private:
sqlite3 *dbh;
};

View File

@ -1,6 +1,6 @@
CXXFLAGS = -O2 -g -Wall -fmessage-length=0
OBJS = Database.o PostfixPolicyProtocol.o ChildProt.o
OBJS = log.o Database.o PostfixPolicyProtocol.o ChildProt.o
LIBS = -lsqlite3

42
log.cpp Normal file
View File

@ -0,0 +1,42 @@
/*
* log.cpp
*
* Created on: 27.03.2013
* Author: wn
*/
#include <syslog.h>
#include <iostream>
#include "log.hpp"
Log::Log(std::string ident, int facility) {
facility_ = facility;
priority_ = LOG_DEBUG;
strncpy(ident_, ident.c_str(), sizeof(ident_));
ident_[sizeof(ident_)-1] = '\0';
openlog(ident_, LOG_PID, facility_);
}
int Log::sync() {
if (buffer_.length()) {
syslog(priority_, "%s", buffer_.c_str());
buffer_.erase();
priority_ = LOG_DEBUG; // default to debug for each message
}
return 0;
}
int Log::overflow(int c) {
if (c != EOF) {
buffer_ += static_cast<char>(c);
} else {
sync();
}
return c;
}
std::ostream& operator<< (std::ostream& os, const LogPriority& log_priority) {
static_cast<Log *>(os.rdbuf())->priority_ = (int)log_priority;
return os;
}

43
log.hpp Normal file
View File

@ -0,0 +1,43 @@
/*
* log.hpp
*
* Created on: 27.03.2013
* Author: wn
*/
#ifndef LOG_HPP_
#define LOG_HPP_
#include <string>
#include <syslog.h>
enum LogPriority {
kLogEmerg = LOG_EMERG, // system is unusable
kLogAlert = LOG_ALERT, // action must be taken immediately
kLogCrit = LOG_CRIT, // critical conditions
kLogErr = LOG_ERR, // error conditions
kLogWarning = LOG_WARNING, // warning conditions
kLogNotice = LOG_NOTICE, // normal, but significant, condition
kLogInfo = LOG_INFO, // informational message
kLogDebug = LOG_DEBUG // debug-level message
};
std::ostream& operator<< (std::ostream& os, const LogPriority& log_priority);
class Log : public std::basic_streambuf<char, std::char_traits<char> > {
public:
explicit Log(std::string ident, int facility);
protected:
int sync();
int overflow(int c);
private:
friend std::ostream& operator<< (std::ostream& os, const LogPriority& log_priority);
std::string buffer_;
int facility_;
int priority_;
char ident_[50];
};
#endif /* LOG_HPP_ */