#include <yajl/yajl_gen.h>
#include <map>
+#include <stdexcept>
#include <cstring>
#include <cerrno>
StringGen m_type;
};
+std::string toString(Config::ReturnCode code)
+{
+ switch (code)
+ {
+ case Config::REJECT: return "reject";
+ case Config::FAIL: return "fail";
+ case Config::OK: return "ok";
+ case Config::HANDLED: return "handled";
+ case Config::INVALID: return "invalid";
+ case Config::USERLOCK: return "userlock";
+ case Config::NOTFOUND: return "notfound";
+ case Config::NOOP: return "noop";
+ case Config::UPDATED: return "noop";
+ }
+ return "reject";
+}
+
}
class Conn::Impl
time_t m_lastActivity;
ProtoParser m_parser;
+ template <typename T>
+ const T& stageMember(T Config::Section::* member) const
+ {
+ switch (m_parser.stage())
+ {
+ case AUTHORIZE: return m_config.autz.*member;
+ case AUTHENTICATE: return m_config.auth.*member;
+ case POSTAUTH: return m_config.postauth.*member;
+ case PREACCT: return m_config.preacct.*member;
+ case ACCOUNTING: return m_config.acct.*member;
+ }
+ throw std::runtime_error("Invalid stage: '" + m_parser.stageStr() + "'.");
+ }
+
+ const Config::Pairs& match() const { return stageMember(&Config::Section::match); }
+ const Config::Pairs& modify() const { return stageMember(&Config::Section::modify); }
+ const Config::Pairs& reply() const { return stageMember(&Config::Section::reply); }
+ Config::ReturnCode returnCode() const { return stageMember(&Config::Section::returnCode); }
+
static void process(void* data);
void processPing();
void processPong();
void Conn::Impl::process(void* data)
{
Impl& impl = *static_cast<Impl*>(data);
- switch (impl.m_parser.packet())
+ try
+ {
+ switch (impl.m_parser.packet())
+ {
+ case PING:
+ impl.processPing();
+ return;
+ case PONG:
+ impl.processPong();
+ return;
+ case DATA:
+ impl.processData();
+ return;
+ }
+ }
+ catch (const std::exception& ex)
{
- case PING:
- impl.processPing();
- return;
- case PONG:
- impl.processPong();
- return;
- case DATA:
- impl.processData();
- return;
+ printfd(__FILE__, "Processing error. %s", ex.what());
+ impl.m_logger("Processing error. %s", ex.what());
}
printfd(__FILE__, "Received invalid packet type: '%s'.\n", impl.m_parser.packetStr().c_str());
impl.m_logger("Received invalid packet type: " + impl.m_parser.packetStr());
int handle = m_users.OpenSearch();
USER_PTR user = NULL;
- bool match = false;
+ bool matched = false;
while (m_users.SearchNext(handle, &user) == 0)
{
if (user == NULL)
continue;
- match = true;
- for (Config::Pairs::const_iterator it = m_config.match.begin(); it != m_config.match.end(); ++it)
+ matched = true;
+ for (Config::Pairs::const_iterator it = match().begin(); it != match().end(); ++it)
{
Config::Pairs::const_iterator pos = m_parser.data().find(it->first);
if (pos == m_parser.data().end())
{
- match = false;
+ matched = false;
break;
}
if (user->GetParamValue(it->second) != pos->second)
{
- match = false;
+ matched = false;
break;
}
}
- if (!match)
+ if (!matched)
continue;
answer(*user);
break;
}
- if (!match)
+ if (!matched)
answerNo();
m_users.CloseSearch(handle);
bool Conn::Impl::answer(const USER& user)
{
printfd(__FILE__, "Got match. Sending answer...\n");
- MapGen reply;
- for (Config::Pairs::const_iterator it = m_config.reply.begin(); it != m_config.reply.end(); ++it)
- reply.add(it->first, new StringGen(user.GetParamValue(it->second)));
+ MapGen replyData;
+ for (Config::Pairs::const_iterator it = reply().begin(); it != reply().end(); ++it)
+ replyData.add(it->first, new StringGen(user.GetParamValue(it->second)));
- MapGen modify;
- for (Config::Pairs::const_iterator it = m_config.modify.begin(); it != m_config.modify.end(); ++it)
- modify.add(it->first, new StringGen(user.GetParamValue(it->second)));
+ MapGen modifyData;
+ for (Config::Pairs::const_iterator it = modify().begin(); it != modify().end(); ++it)
+ modifyData.add(it->first, new StringGen(user.GetParamValue(it->second)));
PacketGen gen("data");
gen.add("result", "ok")
- .add("reply", reply)
- .add("modify", modify);
+ .add("reply", replyData)
+ .add("modify", modifyData);
m_lastPing = time(NULL);
printfd(__FILE__, "No match. Sending answer...\n");
PacketGen gen("data");
gen.add("result", "no");
+ gen.add("return_code", toString(returnCode()));
m_lastPing = time(NULL);