*/
/*
- * Author : Maxim Mamontov <faust@stargazer.dp.ua>
+ * Author : Maksym Mamontov <stg@madf.info>
*/
/*
#include <cerrno>
#include <algorithm>
+#include "stg/store.h"
+#include "stg/common.h"
+#include "stg/user_conf.h"
+#include "stg/user_property.h"
+#include "stg/plugin_creator.h"
#include "radius.h"
-#include "store.h"
-#include "common.h"
-#include "user_conf.h"
-#include "../../../user_property.h"
-extern volatile const time_t stgTime;
-
-void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password);
-void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8);
-void Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8);
+extern volatile time_t stgTime;
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
-class RAD_CREATOR {
-private:
- RADIUS * rad;
-
-public:
- RAD_CREATOR()
- : rad(new RADIUS())
- {
- }
- ~RAD_CREATOR()
- {
- delete rad;
- }
+namespace
+{
+PLUGIN_CREATOR<RADIUS> radc;
- RADIUS * GetPlugin()
- {
- return rad;
- }
-};
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
-RAD_CREATOR radc;
+void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password);
+void Decrypt(BLOWFISH_CTX * ctx, void * dst, const void * src, unsigned long len8);
+void Encrypt(BLOWFISH_CTX * ctx, void * dst, const void * src, unsigned long len8);
+}
+extern "C" PLUGIN * GetPlugin();
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
-int RAD_SETTINGS::ParseIntInRange(const std::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 std::vector<std::string> & str, std::list<std::string> * lst)
{
std::copy(str.begin(), str.end(), std::back_inserter(*lst));
printfd(__FILE__, "Cannot parse parameter 'Port'\n");
return -1;
}
-port = p;
+port = static_cast<uint16_t>(p);
///////////////////////////
pv.param = "Password";
pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
RADIUS::RADIUS()
- : nonstop(false),
+ : ctx(),
+ errorStr(),
+ radSettings(),
+ settings(),
+ authServices(),
+ acctServices(),
+ sessions(),
+ nonstop(false),
isRunning(false),
users(NULL),
stgSettings(NULL),
store(NULL),
- sock(-1)
+ thread(),
+ mutex(),
+ sock(-1),
+ packet(),
+ logger(GetPluginLogger(GetStgLogger(), "radius"))
{
InitEncrypt(&ctx, "");
}
//-----------------------------------------------------------------------------
-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(STORE * s)
-{
-store = s;
-}
-//-----------------------------------------------------------------------------
int RADIUS::ParseSettings()
{
int ret = radSettings.ParseSettings(settings);
return ret;
}
//-----------------------------------------------------------------------------
-bool RADIUS::IsRunning()
-{
-return isRunning;
-}
-//-----------------------------------------------------------------------------
-const std::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;
-}
-//-----------------------------------------------------------------------------
int RADIUS::PrepareNet()
{
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0)
{
errorStr = "Cannot create socket.";
+ logger("Cannot create a socket: %s", strerror(errno));
printfd(__FILE__, "Cannot create socket\n");
return -1;
}
if (bind(sock, (struct sockaddr*)&inAddr, sizeof(inAddr)) < 0)
{
errorStr = "RADIUS: Bind failed.";
+ logger("Cannot bind the socket: %s", strerror(errno));
printfd(__FILE__, "Cannot bind socket\n");
return -1;
}
if (pthread_create(&thread, NULL, Run, this))
{
errorStr = "Cannot create thread.";
+ logger("Cannot create thread.");
printfd(__FILE__, "Cannot create thread\n");
return -1;
}
USER_PTR ui;
if (users->FindByName(it->second.userName, &ui))
{
- ui->Unauthorize(this);
+ users->Unauthorize(ui->GetLogin(), this);
}
}
sessions.erase(sessions.begin(), sessions.end());
//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");
+ struct timespec ts = {0, 200000000};
+ nanosleep(&ts, NULL);
}
}
+if (isRunning)
+ return -1;
+
return 0;
}
//-----------------------------------------------------------------------------
void * RADIUS::Run(void * d)
{
-RADIUS * rad = (RADIUS *)d;
+sigset_t signalSet;
+sigfillset(&signalSet);
+pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
+
+RADIUS * rad = static_cast<RADIUS *>(d);
RAD_PACKET packet;
rad->isRunning = true;
while (rad->nonstop)
{
- if (!rad->WaitPackets(rad->sock))
+ if (!WaitPackets(rad->sock))
{
continue;
}
{
int8_t buf[RAD_MAX_PACKET_LEN];
socklen_t outerAddrLen = sizeof(struct sockaddr_in);
- int dataLen = recvfrom(sock, buf, RAD_MAX_PACKET_LEN, 0, reinterpret_cast<struct sockaddr *>(outerAddr), &outerAddrLen);
- if (dataLen > 0) {
- Decrypt(&ctx, (char *)packet, (const char *)buf, dataLen / 8);
- }
+ ssize_t dataLen = recvfrom(sock, buf, RAD_MAX_PACKET_LEN, 0, reinterpret_cast<struct sockaddr *>(outerAddr), &outerAddrLen);
+ if (dataLen < 0)
+ {
+ logger("recvfrom error: %s", strerror(errno));
+ return -1;
+ }
+ if (dataLen == 0)
+ return -1;
+
+ Decrypt(&ctx, (char *)packet, (const char *)buf, dataLen / 8);
+
if (strncmp((char *)packet->magic, RAD_ID, RAD_MAGIC_LEN))
{
printfd(__FILE__, "RADIUS::RecvData Error magic. Wanted: '%s', got: '%s'\n", RAD_ID, packet->magic);
return -1;
}
+
return 0;
}
//-----------------------------------------------------------------------------
-int RADIUS::Send(const RAD_PACKET & packet, struct sockaddr_in * outerAddr)
+ssize_t RADIUS::Send(const RAD_PACKET & packet, struct sockaddr_in * outerAddr)
{
size_t len = sizeof(RAD_PACKET);
char buf[1032];
Encrypt(&ctx, buf, (char *)&packet, len / 8);
-return sendto(sock, buf, len, 0, reinterpret_cast<struct sockaddr *>(outerAddr), sizeof(struct sockaddr_in));
+ssize_t res = sendto(sock, buf, len, 0, reinterpret_cast<struct sockaddr *>(outerAddr), sizeof(struct sockaddr_in));
+if (res < 0)
+ logger("sendto error: %s", strerror(errno));
+return res;
}
//-----------------------------------------------------------------------------
int RADIUS::ProcessData(RAD_PACKET * packet)
printfd(__FILE__, "RADIUS::ProcessData Unsupported packet type: %d\n", packet->packetType);
return -1;
};
-return 0;
}
//-----------------------------------------------------------------------------
int RADIUS::ProcessAutzPacket(RAD_PACKET * packet)
return -1;
}
USER_IPS ips = ui->GetProperty().ips;
- if (ui->Authorize(ips[0].ip, 0xffFFffFF, this))
+ if (!users->Authorize(ui->GetLogin(), ips[0].ip, 0xffFFffFF, this))
{
printfd(__FILE__, "RADIUS::ProcessAcctStartPacket cannot authorize user '%s'\n", packet->login);
packet->packetType = RAD_REJECT_PACKET;
sessions.erase(sid);
-ui->Unauthorize(this);
+users->Unauthorize(ui->GetLogin(), this);
packet->packetType = RAD_ACCEPT_PACKET;
return 0;
return 0;
}
//-----------------------------------------------------------------------------
-void RADIUS::PrintServices(const std::list<std::string> & svcs)
-{
-for_each(svcs.begin(), svcs.end(), Printer());
-}
-//-----------------------------------------------------------------------------
bool RADIUS::FindUser(USER_PTR * ui, const std::string & login) const
{
if (users->FindByName(login, ui))
return CanAuthService(svc) || CanAcctService(svc);
}
//-----------------------------------------------------------------------------
-bool RADIUS::WaitPackets(int sd) const
+namespace
{
-fd_set rfds;
-FD_ZERO(&rfds);
-FD_SET(sd, &rfds);
-
-struct timeval tv;
-tv.tv_sec = 0;
-tv.tv_usec = 500000;
-
-int res = select(sd + 1, &rfds, NULL, NULL, &tv);
-if (res == -1) // Error
- {
- if (errno != EINTR)
- {
- printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
- }
- return false;
- }
-
-if (res == 0) // Timeout
- {
- return false;
- }
-return true;
-}
-//-----------------------------------------------------------------------------
inline
void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password)
{
}
//-----------------------------------------------------------------------------
inline
-void Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
+void Encrypt(BLOWFISH_CTX * ctx, void * dst, const void * src, unsigned long len8)
{
// len8 - длина в 8-ми байтовых блоках
if (dst != src)
memcpy(dst, src, len8 * 8);
-for (int i = 0; i < len8; i++)
- Blowfish_Encrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));
+for (size_t i = 0; i < len8; i++)
+ Blowfish_Encrypt(ctx, static_cast<uint32_t *>(dst) + i * 2, static_cast<uint32_t *>(dst) + i * 2 + 1);
}
//-----------------------------------------------------------------------------
inline
-void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
+void Decrypt(BLOWFISH_CTX * ctx, void * dst, const void * src, unsigned long len8)
{
// len8 - длина в 8-ми байтовых блоках
if (dst != src)
memcpy(dst, src, len8 * 8);
-for (int i = 0; i < len8; i++)
- Blowfish_Decrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));
+for (size_t i = 0; i < len8; i++)
+ Blowfish_Decrypt(ctx, static_cast<uint32_t *>(dst) + i * 2, static_cast<uint32_t *>(dst) + i * 2 + 1);
}
+
+} // namespace anonymous