X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/a622b081808494796d3679acaebbf5ee364fe9de..389821ffc3891259fb41b2e9fe9b6f891ff24aa6:/projects/stargazer/plugins/other/radius/radius.cpp diff --git a/projects/stargazer/plugins/other/radius/radius.cpp b/projects/stargazer/plugins/other/radius/radius.cpp index 18c18816..8e52cdb4 100644 --- a/projects/stargazer/plugins/other/radius/radius.cpp +++ b/projects/stargazer/plugins/other/radius/radius.cpp @@ -37,16 +37,20 @@ #include "stg/plugin_creator.h" #include "radius.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; //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- +namespace +{ PLUGIN_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(); //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- @@ -89,7 +93,7 @@ if (ParseIntInRange(pvi->value[0], 2, 65535, &p)) printfd(__FILE__, "Cannot parse parameter 'Port'\n"); return -1; } -port = p; +port = static_cast(p); /////////////////////////// pv.param = "Password"; pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv); @@ -136,7 +140,8 @@ RADIUS::RADIUS() thread(), mutex(), sock(-1), - packet() + packet(), + logger(GetPluginLogger(GetStgLogger(), "radius")) { InitEncrypt(&ctx, ""); } @@ -156,6 +161,7 @@ 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; } @@ -168,6 +174,7 @@ inAddr.sin_addr.s_addr = inet_addr("0.0.0.0"); 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; } @@ -202,6 +209,7 @@ if (!isRunning) if (pthread_create(&thread, NULL, Run, this)) { errorStr = "Cannot create thread."; + logger("Cannot create thread."); printfd(__FILE__, "Cannot create thread\n"); return -1; } @@ -253,7 +261,7 @@ sigset_t signalSet; sigfillset(&signalSet); pthread_sigmask(SIG_BLOCK, &signalSet, NULL); -RADIUS * rad = (RADIUS *)d; +RADIUS * rad = static_cast(d); RAD_PACKET packet; rad->isRunning = true; @@ -288,25 +296,36 @@ int RADIUS::RecvData(RAD_PACKET * packet, struct sockaddr_in * outerAddr) { 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(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(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(outerAddr), sizeof(struct sockaddr_in)); +ssize_t res = sendto(sock, buf, len, 0, reinterpret_cast(outerAddr), sizeof(struct sockaddr_in)); +if (res < 0) + logger("sendto error: %s", strerror(errno)); +return res; } //----------------------------------------------------------------------------- int RADIUS::ProcessData(RAD_PACKET * packet) @@ -336,7 +355,6 @@ switch (packet->packetType) printfd(__FILE__, "RADIUS::ProcessData Unsupported packet type: %d\n", packet->packetType); return -1; }; -return 0; } //----------------------------------------------------------------------------- int RADIUS::ProcessAutzPacket(RAD_PACKET * packet) @@ -463,7 +481,7 @@ if (CanAcctService((char *)packet->service)) return -1; } USER_IPS ips = ui->GetProperty().ips; - if (users->Authorize(ui->GetLogin(), 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; @@ -524,11 +542,6 @@ packet->packetType = RAD_ACCEPT_PACKET; return 0; } //----------------------------------------------------------------------------- -void RADIUS::PrintServices(const std::list & svcs) -{ -for_each(svcs.begin(), svcs.end(), Printer()); -} -//----------------------------------------------------------------------------- bool RADIUS::FindUser(USER_PTR * ui, const std::string & login) const { if (users->FindByName(login, ui)) @@ -553,6 +566,9 @@ bool RADIUS::IsAllowedService(const std::string & svc) const return CanAuthService(svc) || CanAcctService(svc); } //----------------------------------------------------------------------------- +namespace +{ + inline void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password) { @@ -563,23 +579,25 @@ Blowfish_Init(ctx, keyL, RAD_PASSWORD_LEN); } //----------------------------------------------------------------------------- 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(dst) + i * 2, static_cast(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(dst) + i * 2, static_cast(dst) + i * 2 + 1); } + +} // namespace anonymous