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 $
35 extern volatile const time_t stgTime;
37 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
67 BASE_PLUGIN * GetPlugin()
69 return radc.GetPlugin();
71 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
74 uint16_t RAD_SETTINGS::GetPort() const
78 //-----------------------------------------------------------------------------
79 uint32_t RAD_SETTINGS::GetServerIP() const
83 //-----------------------------------------------------------------------------
84 int RAD_SETTINGS::GetPassword(string * password) const
86 *password = RAD_SETTINGS::password;
89 //-----------------------------------------------------------------------------
90 int RAD_SETTINGS::GetAuthServices(list<string> * svcs) const
95 //-----------------------------------------------------------------------------
96 int RAD_SETTINGS::GetAcctServices(list<string> * svcs) const
101 //-----------------------------------------------------------------------------
102 int RAD_SETTINGS::ParseIP(const string & str, uint32_t * IP)
104 *IP = inet_addr(str.c_str());
105 return *IP == INADDR_NONE ? -1 : 0;
107 //-----------------------------------------------------------------------------
108 int RAD_SETTINGS::ParseIntInRange(const string & str, int min, int max, int * val)
110 if (str2x(str.c_str(), *val))
112 errorStr = "Incorrect value \'" + str + "\'.";
115 if (*val < min || *val > max)
117 errorStr = "Value \'" + str + "\' out of range.";
122 //-----------------------------------------------------------------------------
123 int RAD_SETTINGS::ParseServices(const vector<string> & str, list<string> * lst)
125 copy(str.begin(), str.end(), back_inserter(*lst));
126 list<string>::iterator it(find(lst->begin(),
129 if (it != lst->end())
134 //-----------------------------------------------------------------------------
135 int RAD_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
139 vector<PARAM_VALUE>::const_iterator pvi;
140 ///////////////////////////
142 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
143 if (pvi == s.moduleParams.end())
145 errorStr = "Parameter \'Port\' not found.";
146 printfd(__FILE__, "Parameter 'Port' not found\n");
149 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
151 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
152 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
156 ///////////////////////////
157 pv.param = "ServerIP";
158 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
159 if (pvi == s.moduleParams.end())
165 if (ParseIP(pvi->value[0], &serverIP))
170 ///////////////////////////
171 pv.param = "Password";
172 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
173 if (pvi == s.moduleParams.end())
175 errorStr = "Parameter \'Password\' not found.";
176 printfd(__FILE__, "Parameter 'Password' not found\n");
179 password = pvi->value[0];
180 ///////////////////////////
181 pv.param = "AuthServices";
182 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
183 if (pvi != s.moduleParams.end())
185 ParseServices(pvi->value, &authServices);
187 ///////////////////////////
188 pv.param = "AcctServices";
189 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
190 if (pvi != s.moduleParams.end())
192 ParseServices(pvi->value, &acctServices);
197 //-----------------------------------------------------------------------------
198 //-----------------------------------------------------------------------------
199 //-----------------------------------------------------------------------------
204 //-----------------------------------------------------------------------------
205 void RADIUS::SetUsers(USERS * u)
209 //-----------------------------------------------------------------------------
210 void RADIUS::SetStgSettings(const SETTINGS * s)
214 //-----------------------------------------------------------------------------
215 void RADIUS::SetSettings(const MODULE_SETTINGS & s)
219 //-----------------------------------------------------------------------------
220 void RADIUS::SetStore(BASE_STORE * s)
224 //-----------------------------------------------------------------------------
225 int RADIUS::ParseSettings()
227 int ret = radSettings.ParseSettings(settings);
229 errorStr = radSettings.GetStrError();
232 //-----------------------------------------------------------------------------
233 bool RADIUS::IsRunning()
237 //-----------------------------------------------------------------------------
238 const string RADIUS::GetVersion() const
240 return "RADIUS data access plugin v 0.6";
242 //-----------------------------------------------------------------------------
243 uint16_t RADIUS::GetStartPosition() const
245 // Start before any authorizers!!!
248 //-----------------------------------------------------------------------------
249 uint16_t RADIUS::GetStopPosition() const
253 //-----------------------------------------------------------------------------
254 void RADIUS::SetUserNotifier(user_iter)
257 //-----------------------------------------------------------------------------
258 void RADIUS::UnSetUserNotifier(user_iter)
261 //-----------------------------------------------------------------------------
262 int RADIUS::PrepareNet()
264 sock = socket(AF_INET, SOCK_DGRAM, 0);
268 errorStr = "Cannot create socket.";
269 printfd(__FILE__, "Cannot create socket\n");
273 inAddr.sin_family = AF_INET;
274 inAddr.sin_port = htons(port);
275 inAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
277 if (bind(sock, (struct sockaddr*)&inAddr, sizeof(inAddr)) < 0)
279 errorStr = "RADIUS: Bind failed.";
280 printfd(__FILE__, "Cannot bind socket\n");
286 //-----------------------------------------------------------------------------
287 int RADIUS::FinalizeNet()
292 //-----------------------------------------------------------------------------
297 radSettings.GetPassword(&password);
298 port = radSettings.GetPort();
299 serverIP = radSettings.GetServerIP();
300 radSettings.GetAuthServices(&authServices);
301 radSettings.GetAcctServices(&acctServices);
303 InitEncrypt(&ctx, password);
314 if (pthread_create(&thread, NULL, Run, this))
316 errorStr = "Cannot create thread.";
317 printfd(__FILE__, "Cannot create thread\n");
325 //-----------------------------------------------------------------------------
333 map<string, RAD_SESSION>::iterator it;
334 for (it = sessions.begin(); it != sessions.end(); ++it)
337 if (users->FindByName(it->second.userName, &ui))
339 ui->Unauthorize(this);
342 sessions.erase(sessions.begin(), sessions.end());
348 //5 seconds to thread stops itself
349 for (int i = 0; i < 25 && isRunning; i++)
354 //after 5 seconds waiting thread still running. now killing it
357 if (pthread_kill(thread, SIGINT))
359 errorStr = "Cannot kill thread.";
360 printfd(__FILE__, "Cannot kill thread\n");
363 printfd(__FILE__, "RADIUS::Stop killed Run\n");
369 //-----------------------------------------------------------------------------
370 void * RADIUS::Run(void * d)
372 RADIUS * rad = (RADIUS *)d;
375 rad->isRunning = true;
379 if (!rad->WaitPackets(rad->sock))
383 if (rad->RecvData(&packet))
385 printfd(__FILE__, "RADIUS::Run Error on RecvData\n");
389 if (rad->ProcessData(&packet))
391 packet.packetType = RAD_REJECT_PACKET;
397 rad->isRunning = false;
401 //-----------------------------------------------------------------------------
402 int RADIUS::RecvData(RAD_PACKET * packet)
404 int8_t buf[RAD_MAX_PACKET_LEN];
405 outerAddrLen = sizeof(struct sockaddr_in);
406 int dataLen = recvfrom(sock, buf, RAD_MAX_PACKET_LEN, 0, (struct sockaddr *)&outerAddr, &outerAddrLen);
408 Decrypt(&ctx, (char *)packet, (const char *)buf, dataLen / 8);
410 if (strncmp((char *)packet->magic, RAD_ID, RAD_MAGIC_LEN))
412 printfd(__FILE__, "RADIUS::RecvData Error magic. Wanted: '%s', got: '%s'\n", RAD_ID, packet->magic);
417 //-----------------------------------------------------------------------------
418 int RADIUS::Send(const RAD_PACKET & packet)
420 int res, len = sizeof(RAD_PACKET);
423 Encrypt(&ctx, buf, (char *)&packet, len / 8);
424 res = sendto(sock, buf, len, 0, (struct sockaddr *)&outerAddr, outerAddrLen);
428 //-----------------------------------------------------------------------------
429 int RADIUS::ProcessData(RAD_PACKET * packet)
431 //struct in_addr addr = {packet->ip};
432 if (strncmp((const char *)packet->protoVer, "01", 2))
434 printfd(__FILE__, "RADIUS::ProcessData packet.protoVer incorrect\n");
437 switch (packet->packetType)
439 case RAD_AUTZ_PACKET:
440 return ProcessAutzPacket(packet);
441 case RAD_AUTH_PACKET:
442 return ProcessAuthPacket(packet);
443 case RAD_POST_AUTH_PACKET:
444 return ProcessPostAuthPacket(packet);
445 case RAD_ACCT_START_PACKET:
446 return ProcessAcctStartPacket(packet);
447 case RAD_ACCT_STOP_PACKET:
448 return ProcessAcctStopPacket(packet);
449 case RAD_ACCT_UPDATE_PACKET:
450 return ProcessAcctUpdatePacket(packet);
451 case RAD_ACCT_OTHER_PACKET:
452 return ProcessAcctOtherPacket(packet);
454 printfd(__FILE__, "RADIUS::ProcessData Unsupported packet type: %d\n", packet->packetType);
459 //-----------------------------------------------------------------------------
460 int RADIUS::ProcessAutzPacket(RAD_PACKET * packet)
464 if (!IsAllowedService((char *)packet->service))
466 printfd(__FILE__, "RADIUS::ProcessAutzPacket service '%s' is not allowed to authorize\n", packet->service);
467 packet->packetType = RAD_REJECT_PACKET;
471 if (store->RestoreUserConf(&conf, (char *)packet->login))
473 packet->packetType = RAD_REJECT_PACKET;
474 printfd(__FILE__, "RADIUS::ProcessAutzPacket cannot restore conf for user '%s'\n", packet->login);
478 // At this point service can be authorized at least
479 // So we send a plain-text password
481 packet->packetType = RAD_ACCEPT_PACKET;
482 strncpy((char *)packet->password, conf.password.c_str(), RAD_PASSWORD_LEN);
486 //-----------------------------------------------------------------------------
487 int RADIUS::ProcessAuthPacket(RAD_PACKET * packet)
491 if (!CanAcctService((char *)packet->service))
494 // There are no sense to check for allowed service
495 // It has allready checked at previous stage (authorization)
497 printfd(__FILE__, "RADIUS::ProcessAuthPacket service '%s' neednot stargazer authentication\n", (char *)packet->service);
498 packet->packetType = RAD_ACCEPT_PACKET;
502 // At this point we have an accountable service
503 // All other services got a password if allowed or rejected
505 if (!FindUser(&ui, (char *)packet->login))
507 packet->packetType = RAD_REJECT_PACKET;
508 printfd(__FILE__, "RADIUS::ProcessAuthPacket user '%s' not found\n", (char *)packet->login);
512 if (ui->IsInetable())
514 packet->packetType = RAD_ACCEPT_PACKET;
518 packet->packetType = RAD_REJECT_PACKET;
521 packet->packetType = RAD_ACCEPT_PACKET;
524 //-----------------------------------------------------------------------------
525 int RADIUS::ProcessPostAuthPacket(RAD_PACKET * packet)
529 if (!CanAcctService((char *)packet->service))
532 // There are no sense to check for allowed service
533 // It has allready checked at previous stage (authorization)
535 packet->packetType = RAD_ACCEPT_PACKET;
539 if (!FindUser(&ui, (char *)packet->login))
541 packet->packetType = RAD_REJECT_PACKET;
542 printfd(__FILE__, "RADIUS::ProcessPostAuthPacket user '%s' not found\n", (char *)packet->login);
546 // I think that only Framed-User services has sense to be accountable
547 // So we have to supply a Framed-IP
549 USER_IPS ips = ui->property.ips;
550 packet->packetType = RAD_ACCEPT_PACKET;
552 // Additional checking for Framed-User service
554 if (!strncmp((char *)packet->service, "Framed-User", RAD_SERVICE_LEN))
555 packet->ip = ips[0].ip;
561 //-----------------------------------------------------------------------------
562 int RADIUS::ProcessAcctStartPacket(RAD_PACKET * packet)
566 if (!FindUser(&ui, (char *)packet->login))
568 packet->packetType = RAD_REJECT_PACKET;
569 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket user '%s' not found\n", (char *)packet->login);
573 // At this point we have to unauthorize user only if it is an accountable service
575 if (CanAcctService((char *)packet->service))
577 if (sessions.find((const char *)packet->sessid) != sessions.end())
579 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket session already started!\n");
580 packet->packetType = RAD_REJECT_PACKET;
583 USER_IPS ips = ui->property.ips;
584 if (ui->Authorize(ips[0].ip, "", 0xffFFffFF, this))
586 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket cannot authorize user '%s'\n", packet->login);
587 packet->packetType = RAD_REJECT_PACKET;
590 sessions[(const char *)packet->sessid].userName = (const char *)packet->login;
591 sessions[(const char *)packet->sessid].serviceType = (const char *)packet->service;
592 for_each(sessions.begin(), sessions.end(), SPrinter());
596 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket service '%s' can not be accounted\n", (char *)packet->service);
599 packet->packetType = RAD_ACCEPT_PACKET;
602 //-----------------------------------------------------------------------------
603 int RADIUS::ProcessAcctStopPacket(RAD_PACKET * packet)
605 map<string, RAD_SESSION>::iterator sid;
607 if ((sid = sessions.find((const char *)packet->sessid)) == sessions.end())
609 printfd(__FILE__, "RADIUS::ProcessAcctStopPacket session had not started yet\n");
610 packet->packetType = RAD_REJECT_PACKET;
616 if (!FindUser(&ui, sid->second.userName))
618 packet->packetType = RAD_REJECT_PACKET;
619 printfd(__FILE__, "RADIUS::ProcessPostAuthPacket user '%s' not found\n", sid->second.userName.c_str());
625 ui->Unauthorize(this);
627 packet->packetType = RAD_ACCEPT_PACKET;
630 //-----------------------------------------------------------------------------
631 int RADIUS::ProcessAcctUpdatePacket(RAD_PACKET * packet)
633 // Fake. May be used later
634 packet->packetType = RAD_ACCEPT_PACKET;
637 //-----------------------------------------------------------------------------
638 int RADIUS::ProcessAcctOtherPacket(RAD_PACKET * packet)
640 // Fake. May be used later
641 packet->packetType = RAD_ACCEPT_PACKET;
644 //-----------------------------------------------------------------------------
645 void RADIUS::InitEncrypt(BLOWFISH_CTX * ctx, const string & password)
647 unsigned char keyL[RAD_PASSWORD_LEN]; // Пароль для шифровки
648 memset(keyL, 0, RAD_PASSWORD_LEN);
649 strncpy((char *)keyL, password.c_str(), RAD_PASSWORD_LEN);
650 Blowfish_Init(ctx, keyL, RAD_PASSWORD_LEN);
652 //-----------------------------------------------------------------------------
653 void RADIUS::Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
655 // len8 - длина в 8-ми байтовых блоках
657 memcpy(dst, src, len8 * 8);
659 for (int i = 0; i < len8; i++)
660 Blowfish_Encrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));
662 //-----------------------------------------------------------------------------
663 void RADIUS::Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
665 // len8 - длина в 8-ми байтовых блоках
667 memcpy(dst, src, len8 * 8);
669 for (int i = 0; i < len8; i++)
670 Blowfish_Decrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));
672 //-----------------------------------------------------------------------------
673 void RADIUS::PrintServices(const list<string> & svcs)
675 for_each(svcs.begin(), svcs.end(), Printer());
677 //-----------------------------------------------------------------------------
678 bool RADIUS::FindUser(user_iter * ui, const std::string & login) const
680 if (users->FindByName(login, ui))
686 //-----------------------------------------------------------------------------
687 bool RADIUS::CanAuthService(const std::string & svc) const
689 return find(authServices.begin(), authServices.end(), svc) != authServices.end();
691 //-----------------------------------------------------------------------------
692 bool RADIUS::CanAcctService(const std::string & svc) const
694 return find(acctServices.begin(), acctServices.end(), svc) != acctServices.end();
696 //-----------------------------------------------------------------------------
697 bool RADIUS::IsAllowedService(const std::string & svc) const
699 return CanAuthService(svc) || CanAcctService(svc);
701 //-----------------------------------------------------------------------------
702 bool RADIUS::WaitPackets(int sd) const
712 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
713 if (res == -1) // Error
717 printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
722 if (res == 0) // Timeout