2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
22 * This file contains a realization of radius data access plugin for Stargazer
25 * $Date: 2009/12/13 14:17:13 $
33 #include "stg/store.h"
34 #include "stg/common.h"
35 #include "stg/user_conf.h"
36 #include "stg/user_property.h"
39 extern volatile const time_t stgTime;
41 void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password);
42 void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8);
43 void Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8);
45 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
71 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
76 return radc.GetPlugin();
78 //-----------------------------------------------------------------------------
79 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
81 int RAD_SETTINGS::ParseServices(const std::vector<std::string> & str, std::list<std::string> * lst)
83 std::copy(str.begin(), str.end(), std::back_inserter(*lst));
84 std::list<std::string>::iterator it(std::find(lst->begin(),
92 //-----------------------------------------------------------------------------
93 int RAD_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
97 std::vector<PARAM_VALUE>::const_iterator pvi;
98 ///////////////////////////
100 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
101 if (pvi == s.moduleParams.end())
103 errorStr = "Parameter \'Port\' not found.";
104 printfd(__FILE__, "Parameter 'Port' not found\n");
107 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
109 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
110 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
114 ///////////////////////////
115 pv.param = "Password";
116 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
117 if (pvi == s.moduleParams.end())
119 errorStr = "Parameter \'Password\' not found.";
120 printfd(__FILE__, "Parameter 'Password' not found\n");
123 password = pvi->value[0];
124 ///////////////////////////
125 pv.param = "AuthServices";
126 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
127 if (pvi != s.moduleParams.end())
129 ParseServices(pvi->value, &authServices);
131 ///////////////////////////
132 pv.param = "AcctServices";
133 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
134 if (pvi != s.moduleParams.end())
136 ParseServices(pvi->value, &acctServices);
141 //-----------------------------------------------------------------------------
142 //-----------------------------------------------------------------------------
143 //-----------------------------------------------------------------------------
152 InitEncrypt(&ctx, "");
154 //-----------------------------------------------------------------------------
155 void RADIUS::SetUsers(USERS * u)
159 //-----------------------------------------------------------------------------
160 void RADIUS::SetStgSettings(const SETTINGS * s)
164 //-----------------------------------------------------------------------------
165 void RADIUS::SetSettings(const MODULE_SETTINGS & s)
169 //-----------------------------------------------------------------------------
170 void RADIUS::SetStore(STORE * s)
174 //-----------------------------------------------------------------------------
175 int RADIUS::ParseSettings()
177 int ret = radSettings.ParseSettings(settings);
179 errorStr = radSettings.GetStrError();
182 //-----------------------------------------------------------------------------
183 bool RADIUS::IsRunning()
187 //-----------------------------------------------------------------------------
188 const std::string RADIUS::GetVersion() const
190 return "RADIUS data access plugin v 0.6";
192 //-----------------------------------------------------------------------------
193 uint16_t RADIUS::GetStartPosition() const
195 // Start before any authorizers!!!
198 //-----------------------------------------------------------------------------
199 uint16_t RADIUS::GetStopPosition() const
203 //-----------------------------------------------------------------------------
204 int RADIUS::PrepareNet()
206 sock = socket(AF_INET, SOCK_DGRAM, 0);
210 errorStr = "Cannot create socket.";
211 printfd(__FILE__, "Cannot create socket\n");
215 struct sockaddr_in inAddr;
216 inAddr.sin_family = AF_INET;
217 inAddr.sin_port = htons(radSettings.GetPort());
218 inAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
220 if (bind(sock, (struct sockaddr*)&inAddr, sizeof(inAddr)) < 0)
222 errorStr = "RADIUS: Bind failed.";
223 printfd(__FILE__, "Cannot bind socket\n");
229 //-----------------------------------------------------------------------------
230 int RADIUS::FinalizeNet()
235 //-----------------------------------------------------------------------------
238 std::string password(radSettings.GetPassword());
240 authServices = radSettings.GetAuthServices();
241 acctServices = radSettings.GetAcctServices();
243 InitEncrypt(&ctx, password);
254 if (pthread_create(&thread, NULL, Run, this))
256 errorStr = "Cannot create thread.";
257 printfd(__FILE__, "Cannot create thread\n");
265 //-----------------------------------------------------------------------------
273 std::map<std::string, RAD_SESSION>::iterator it;
274 for (it = sessions.begin(); it != sessions.end(); ++it)
277 if (users->FindByName(it->second.userName, &ui))
279 ui->Unauthorize(this);
282 sessions.erase(sessions.begin(), sessions.end());
288 //5 seconds to thread stops itself
289 for (int i = 0; i < 25 && isRunning; i++)
294 //after 5 seconds waiting thread still running. now killing it
297 if (pthread_kill(thread, SIGINT))
299 errorStr = "Cannot kill thread.";
300 printfd(__FILE__, "Cannot kill thread\n");
303 printfd(__FILE__, "RADIUS::Stop killed Run\n");
309 //-----------------------------------------------------------------------------
310 void * RADIUS::Run(void * d)
312 RADIUS * rad = (RADIUS *)d;
315 rad->isRunning = true;
319 if (!rad->WaitPackets(rad->sock))
323 struct sockaddr_in outerAddr;
324 if (rad->RecvData(&packet, &outerAddr))
326 printfd(__FILE__, "RADIUS::Run Error on RecvData\n");
330 if (rad->ProcessData(&packet))
332 packet.packetType = RAD_REJECT_PACKET;
334 rad->Send(packet, &outerAddr);
338 rad->isRunning = false;
342 //-----------------------------------------------------------------------------
343 int RADIUS::RecvData(RAD_PACKET * packet, struct sockaddr_in * outerAddr)
345 int8_t buf[RAD_MAX_PACKET_LEN];
346 socklen_t outerAddrLen = sizeof(struct sockaddr_in);
347 int dataLen = recvfrom(sock, buf, RAD_MAX_PACKET_LEN, 0, reinterpret_cast<struct sockaddr *>(outerAddr), &outerAddrLen);
349 Decrypt(&ctx, (char *)packet, (const char *)buf, dataLen / 8);
351 if (strncmp((char *)packet->magic, RAD_ID, RAD_MAGIC_LEN))
353 printfd(__FILE__, "RADIUS::RecvData Error magic. Wanted: '%s', got: '%s'\n", RAD_ID, packet->magic);
358 //-----------------------------------------------------------------------------
359 int RADIUS::Send(const RAD_PACKET & packet, struct sockaddr_in * outerAddr)
361 size_t len = sizeof(RAD_PACKET);
364 Encrypt(&ctx, buf, (char *)&packet, len / 8);
365 return sendto(sock, buf, len, 0, reinterpret_cast<struct sockaddr *>(outerAddr), sizeof(struct sockaddr_in));
367 //-----------------------------------------------------------------------------
368 int RADIUS::ProcessData(RAD_PACKET * packet)
370 if (strncmp((const char *)packet->protoVer, "01", 2))
372 printfd(__FILE__, "RADIUS::ProcessData packet.protoVer incorrect\n");
375 switch (packet->packetType)
377 case RAD_AUTZ_PACKET:
378 return ProcessAutzPacket(packet);
379 case RAD_AUTH_PACKET:
380 return ProcessAuthPacket(packet);
381 case RAD_POST_AUTH_PACKET:
382 return ProcessPostAuthPacket(packet);
383 case RAD_ACCT_START_PACKET:
384 return ProcessAcctStartPacket(packet);
385 case RAD_ACCT_STOP_PACKET:
386 return ProcessAcctStopPacket(packet);
387 case RAD_ACCT_UPDATE_PACKET:
388 return ProcessAcctUpdatePacket(packet);
389 case RAD_ACCT_OTHER_PACKET:
390 return ProcessAcctOtherPacket(packet);
392 printfd(__FILE__, "RADIUS::ProcessData Unsupported packet type: %d\n", packet->packetType);
397 //-----------------------------------------------------------------------------
398 int RADIUS::ProcessAutzPacket(RAD_PACKET * packet)
402 if (!IsAllowedService((char *)packet->service))
404 printfd(__FILE__, "RADIUS::ProcessAutzPacket service '%s' is not allowed to authorize\n", packet->service);
405 packet->packetType = RAD_REJECT_PACKET;
409 if (store->RestoreUserConf(&conf, (char *)packet->login))
411 packet->packetType = RAD_REJECT_PACKET;
412 printfd(__FILE__, "RADIUS::ProcessAutzPacket cannot restore conf for user '%s'\n", packet->login);
416 // At this point service can be authorized at least
417 // So we send a plain-text password
419 packet->packetType = RAD_ACCEPT_PACKET;
420 strncpy((char *)packet->password, conf.password.c_str(), RAD_PASSWORD_LEN);
424 //-----------------------------------------------------------------------------
425 int RADIUS::ProcessAuthPacket(RAD_PACKET * packet)
429 if (!CanAcctService((char *)packet->service))
432 // There are no sense to check for allowed service
433 // It has allready checked at previous stage (authorization)
435 printfd(__FILE__, "RADIUS::ProcessAuthPacket service '%s' neednot stargazer authentication\n", (char *)packet->service);
436 packet->packetType = RAD_ACCEPT_PACKET;
440 // At this point we have an accountable service
441 // All other services got a password if allowed or rejected
443 if (!FindUser(&ui, (char *)packet->login))
445 packet->packetType = RAD_REJECT_PACKET;
446 printfd(__FILE__, "RADIUS::ProcessAuthPacket user '%s' not found\n", (char *)packet->login);
450 if (ui->IsInetable())
452 packet->packetType = RAD_ACCEPT_PACKET;
456 packet->packetType = RAD_REJECT_PACKET;
459 packet->packetType = RAD_ACCEPT_PACKET;
462 //-----------------------------------------------------------------------------
463 int RADIUS::ProcessPostAuthPacket(RAD_PACKET * packet)
467 if (!CanAcctService((char *)packet->service))
470 // There are no sense to check for allowed service
471 // It has allready checked at previous stage (authorization)
473 packet->packetType = RAD_ACCEPT_PACKET;
477 if (!FindUser(&ui, (char *)packet->login))
479 packet->packetType = RAD_REJECT_PACKET;
480 printfd(__FILE__, "RADIUS::ProcessPostAuthPacket user '%s' not found\n", (char *)packet->login);
484 // I think that only Framed-User services has sense to be accountable
485 // So we have to supply a Framed-IP
487 USER_IPS ips = ui->GetProperty().ips;
488 packet->packetType = RAD_ACCEPT_PACKET;
490 // Additional checking for Framed-User service
492 if (!strncmp((char *)packet->service, "Framed-User", RAD_SERVICE_LEN))
493 packet->ip = ips[0].ip;
499 //-----------------------------------------------------------------------------
500 int RADIUS::ProcessAcctStartPacket(RAD_PACKET * packet)
504 if (!FindUser(&ui, (char *)packet->login))
506 packet->packetType = RAD_REJECT_PACKET;
507 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket user '%s' not found\n", (char *)packet->login);
511 // At this point we have to unauthorize user only if it is an accountable service
513 if (CanAcctService((char *)packet->service))
515 if (sessions.find((const char *)packet->sessid) != sessions.end())
517 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket session already started!\n");
518 packet->packetType = RAD_REJECT_PACKET;
521 USER_IPS ips = ui->GetProperty().ips;
522 if (ui->Authorize(ips[0].ip, 0xffFFffFF, this))
524 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket cannot authorize user '%s'\n", packet->login);
525 packet->packetType = RAD_REJECT_PACKET;
528 sessions[(const char *)packet->sessid].userName = (const char *)packet->login;
529 sessions[(const char *)packet->sessid].serviceType = (const char *)packet->service;
530 for_each(sessions.begin(), sessions.end(), SPrinter());
534 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket service '%s' can not be accounted\n", (char *)packet->service);
537 packet->packetType = RAD_ACCEPT_PACKET;
540 //-----------------------------------------------------------------------------
541 int RADIUS::ProcessAcctStopPacket(RAD_PACKET * packet)
543 std::map<std::string, RAD_SESSION>::iterator sid;
545 if ((sid = sessions.find((const char *)packet->sessid)) == sessions.end())
547 printfd(__FILE__, "RADIUS::ProcessAcctStopPacket session had not started yet\n");
548 packet->packetType = RAD_REJECT_PACKET;
554 if (!FindUser(&ui, sid->second.userName))
556 packet->packetType = RAD_REJECT_PACKET;
557 printfd(__FILE__, "RADIUS::ProcessPostAuthPacket user '%s' not found\n", sid->second.userName.c_str());
563 ui->Unauthorize(this);
565 packet->packetType = RAD_ACCEPT_PACKET;
568 //-----------------------------------------------------------------------------
569 int RADIUS::ProcessAcctUpdatePacket(RAD_PACKET * packet)
571 // Fake. May be use it later
572 packet->packetType = RAD_ACCEPT_PACKET;
575 //-----------------------------------------------------------------------------
576 int RADIUS::ProcessAcctOtherPacket(RAD_PACKET * packet)
578 // Fake. May be use it later
579 packet->packetType = RAD_ACCEPT_PACKET;
582 //-----------------------------------------------------------------------------
583 void RADIUS::PrintServices(const std::list<std::string> & svcs)
585 for_each(svcs.begin(), svcs.end(), Printer());
587 //-----------------------------------------------------------------------------
588 bool RADIUS::FindUser(USER_PTR * ui, const std::string & login) const
590 if (users->FindByName(login, ui))
596 //-----------------------------------------------------------------------------
597 bool RADIUS::CanAuthService(const std::string & svc) const
599 return find(authServices.begin(), authServices.end(), svc) != authServices.end();
601 //-----------------------------------------------------------------------------
602 bool RADIUS::CanAcctService(const std::string & svc) const
604 return find(acctServices.begin(), acctServices.end(), svc) != acctServices.end();
606 //-----------------------------------------------------------------------------
607 bool RADIUS::IsAllowedService(const std::string & svc) const
609 return CanAuthService(svc) || CanAcctService(svc);
611 //-----------------------------------------------------------------------------
612 bool RADIUS::WaitPackets(int sd) const
622 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
623 if (res == -1) // Error
627 printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
632 if (res == 0) // Timeout
639 //-----------------------------------------------------------------------------
641 void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password)
643 unsigned char keyL[RAD_PASSWORD_LEN]; // Пароль для шифровки
644 memset(keyL, 0, RAD_PASSWORD_LEN);
645 strncpy((char *)keyL, password.c_str(), RAD_PASSWORD_LEN);
646 Blowfish_Init(ctx, keyL, RAD_PASSWORD_LEN);
648 //-----------------------------------------------------------------------------
650 void Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
652 // len8 - длина в 8-ми байтовых блоках
654 memcpy(dst, src, len8 * 8);
656 for (int i = 0; i < len8; i++)
657 Blowfish_Encrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));
659 //-----------------------------------------------------------------------------
661 void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
663 // len8 - длина в 8-ми байтовых блоках
665 memcpy(dst, src, len8 * 8);
667 for (int i = 0; i < len8; i++)
668 Blowfish_Decrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));