-#include "common.h"
-
-extern volatile const time_t stgTime;
-
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
-class RAD_CREATOR
-{
-private:
- RADIUS * rad;
-
-public:
- RAD_CREATOR()
- : rad(new RADIUS())
- {
- };
- ~RAD_CREATOR()
- {
- delete rad;
- };
-
- RADIUS * GetPlugin()
- {
- return rad;
- };
-};
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
-RAD_CREATOR radc;
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
-BASE_PLUGIN * GetPlugin()
-{
-return radc.GetPlugin();
-}
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
-int RAD_SETTINGS::ParseIntInRange(const string & str, int min, int max, int * val)
-{
-if (str2x(str.c_str(), *val))
- {
- errorStr = "Incorrect value \'" + str + "\'.";
- return -1;
- }
-if (*val < min || *val > max)
- {
- errorStr = "Value \'" + str + "\' out of range.";
- return -1;
- }
-return 0;
-}
-//-----------------------------------------------------------------------------
-int RAD_SETTINGS::ParseServices(const vector<string> & str, list<string> * lst)
-{
- copy(str.begin(), str.end(), back_inserter(*lst));
- list<string>::iterator it(find(lst->begin(),
- lst->end(),
- "empty"));
- if (it != lst->end())
- *it = "";
-
- return 0;
-}
-//-----------------------------------------------------------------------------
-int RAD_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
-{
-int p;
-PARAM_VALUE pv;
-vector<PARAM_VALUE>::const_iterator pvi;
-///////////////////////////
-pv.param = "Port";
-pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
-if (pvi == s.moduleParams.end())
- {
- errorStr = "Parameter \'Port\' not found.";
- printfd(__FILE__, "Parameter 'Port' not found\n");
- return -1;
- }
-if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
- {
- errorStr = "Cannot parse parameter \'Port\': " + errorStr;
- printfd(__FILE__, "Cannot parse parameter 'Port'\n");
- return -1;
- }
-port = p;
-///////////////////////////
-pv.param = "Password";
-pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
-if (pvi == s.moduleParams.end())
- {
- errorStr = "Parameter \'Password\' not found.";
- printfd(__FILE__, "Parameter 'Password' not found\n");
- return -1;
- }
-password = pvi->value[0];
-///////////////////////////
-pv.param = "AuthServices";
-pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
-if (pvi != s.moduleParams.end())
- {
- ParseServices(pvi->value, &authServices);
- }
-///////////////////////////
-pv.param = "AcctServices";
-pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
-if (pvi != s.moduleParams.end())
- {
- ParseServices(pvi->value, &acctServices);
- }
-
-return 0;
-}
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
-RADIUS::RADIUS()
- : nonstop(false),
- isRunning(false),
- users(NULL),
- stgSettings(NULL),
- store(NULL),
- sock(-1)
-{
-}
-//-----------------------------------------------------------------------------
-void RADIUS::SetUsers(USERS * u)
-{
-users = u;
-}
-//-----------------------------------------------------------------------------
-void RADIUS::SetStgSettings(const SETTINGS * s)
-{
-stgSettings = s;
-}
-//-----------------------------------------------------------------------------
-void RADIUS::SetSettings(const MODULE_SETTINGS & s)
-{
-settings = s;
-}
-//-----------------------------------------------------------------------------
-void RADIUS::SetStore(BASE_STORE * s)
-{
-store = s;
-}
-//-----------------------------------------------------------------------------
-int RADIUS::ParseSettings()
-{
-int ret = radSettings.ParseSettings(settings);
-if (ret)
- errorStr = radSettings.GetStrError();
-return ret;
-}
-//-----------------------------------------------------------------------------
-bool RADIUS::IsRunning()
-{
-return isRunning;
-}
-//-----------------------------------------------------------------------------
-const string RADIUS::GetVersion() const
-{
-return "RADIUS data access plugin v 0.6";
-}
-//-----------------------------------------------------------------------------
-uint16_t RADIUS::GetStartPosition() const
-{
-// Start before any authorizers!!!
-return 20;
-}
-//-----------------------------------------------------------------------------
-uint16_t RADIUS::GetStopPosition() const
-{
-return 20;
-}
-//-----------------------------------------------------------------------------
-void RADIUS::SetUserNotifier(user_iter)
-{
-}
-//-----------------------------------------------------------------------------
-void RADIUS::UnSetUserNotifier(user_iter)
-{
-}
-//-----------------------------------------------------------------------------
-int RADIUS::PrepareNet()
-{
-sock = socket(AF_INET, SOCK_DGRAM, 0);
-
-if (sock < 0)
- {
- errorStr = "Cannot create socket.";
- printfd(__FILE__, "Cannot create socket\n");
- return -1;
- }
-
-struct sockaddr_in inAddr;
-inAddr.sin_family = AF_INET;
-inAddr.sin_port = htons(radSettings.GetPort());
-inAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
-
-if (bind(sock, (struct sockaddr*)&inAddr, sizeof(inAddr)) < 0)
- {
- errorStr = "RADIUS: Bind failed.";
- printfd(__FILE__, "Cannot bind socket\n");
- return -1;
- }
-
-return 0;
-}
-//-----------------------------------------------------------------------------
-int RADIUS::FinalizeNet()
-{
-close(sock);
-return 0;
-}
-//-----------------------------------------------------------------------------
-int RADIUS::Start()
-{
-string password(radSettings.GetPassword());
-
-authServices = radSettings.GetAuthServices();
-acctServices = radSettings.GetAcctServices();
-
-InitEncrypt(&ctx, password);
-
-nonstop = true;
-
-if (PrepareNet())
- {
- return -1;
- }
-
-if (!isRunning)
- {
- if (pthread_create(&thread, NULL, Run, this))
- {
- errorStr = "Cannot create thread.";
- printfd(__FILE__, "Cannot create thread\n");
- return -1;
- }
- }
-
-errorStr = "";
-return 0;
-}
-//-----------------------------------------------------------------------------
-int RADIUS::Stop()
-{
-if (!IsRunning())
- return 0;
-
-nonstop = false;
-
-map<string, RAD_SESSION>::iterator it;
-for (it = sessions.begin(); it != sessions.end(); ++it)
- {
- user_iter ui;
- if (users->FindByName(it->second.userName, &ui))
- {
- ui->Unauthorize(this);
- }
- }
-sessions.erase(sessions.begin(), sessions.end());
-
-FinalizeNet();
-
-if (isRunning)
- {
- //5 seconds to thread stops itself
- for (int i = 0; i < 25 && isRunning; i++)
- {
- usleep(200000);
- }
-
- //after 5 seconds waiting thread still running. now killing it
- if (isRunning)
- {
- if (pthread_kill(thread, SIGINT))
- {
- errorStr = "Cannot kill thread.";
- printfd(__FILE__, "Cannot kill thread\n");
- return -1;
- }
- printfd(__FILE__, "RADIUS::Stop killed Run\n");
- }
- }
-
-return 0;
-}
-//-----------------------------------------------------------------------------
-void * RADIUS::Run(void * d)
-{
-RADIUS * rad = (RADIUS *)d;
-RAD_PACKET packet;
-
-rad->isRunning = true;