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 $
36 #include "user_conf.h"
37 #include "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::ParseIntInRange(const std::string & str, int min, int max, int * val)
83 if (str2x(str.c_str(), *val))
85 errorStr = "Incorrect value \'" + str + "\'.";
88 if (*val < min || *val > max)
90 errorStr = "Value \'" + str + "\' out of range.";
95 //-----------------------------------------------------------------------------
96 int RAD_SETTINGS::ParseServices(const std::vector<std::string> & str, std::list<std::string> * lst)
98 std::copy(str.begin(), str.end(), std::back_inserter(*lst));
99 std::list<std::string>::iterator it(std::find(lst->begin(),
102 if (it != lst->end())
107 //-----------------------------------------------------------------------------
108 int RAD_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
112 std::vector<PARAM_VALUE>::const_iterator pvi;
113 ///////////////////////////
115 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
116 if (pvi == s.moduleParams.end())
118 errorStr = "Parameter \'Port\' not found.";
119 printfd(__FILE__, "Parameter 'Port' not found\n");
122 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
124 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
125 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
129 ///////////////////////////
130 pv.param = "Password";
131 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
132 if (pvi == s.moduleParams.end())
134 errorStr = "Parameter \'Password\' not found.";
135 printfd(__FILE__, "Parameter 'Password' not found\n");
138 password = pvi->value[0];
139 ///////////////////////////
140 pv.param = "AuthServices";
141 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
142 if (pvi != s.moduleParams.end())
144 ParseServices(pvi->value, &authServices);
146 ///////////////////////////
147 pv.param = "AcctServices";
148 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
149 if (pvi != s.moduleParams.end())
151 ParseServices(pvi->value, &acctServices);
156 //-----------------------------------------------------------------------------
157 //-----------------------------------------------------------------------------
158 //-----------------------------------------------------------------------------
167 InitEncrypt(&ctx, "");
169 //-----------------------------------------------------------------------------
170 void RADIUS::SetUsers(USERS * u)
174 //-----------------------------------------------------------------------------
175 void RADIUS::SetStgSettings(const SETTINGS * s)
179 //-----------------------------------------------------------------------------
180 void RADIUS::SetSettings(const MODULE_SETTINGS & s)
184 //-----------------------------------------------------------------------------
185 void RADIUS::SetStore(STORE * s)
189 //-----------------------------------------------------------------------------
190 int RADIUS::ParseSettings()
192 int ret = radSettings.ParseSettings(settings);
194 errorStr = radSettings.GetStrError();
197 //-----------------------------------------------------------------------------
198 bool RADIUS::IsRunning()
202 //-----------------------------------------------------------------------------
203 const std::string RADIUS::GetVersion() const
205 return "RADIUS data access plugin v 0.6";
207 //-----------------------------------------------------------------------------
208 uint16_t RADIUS::GetStartPosition() const
210 // Start before any authorizers!!!
213 //-----------------------------------------------------------------------------
214 uint16_t RADIUS::GetStopPosition() const
218 //-----------------------------------------------------------------------------
219 int RADIUS::PrepareNet()
221 sock = socket(AF_INET, SOCK_DGRAM, 0);
225 errorStr = "Cannot create socket.";
226 printfd(__FILE__, "Cannot create socket\n");
230 struct sockaddr_in inAddr;
231 inAddr.sin_family = AF_INET;
232 inAddr.sin_port = htons(radSettings.GetPort());
233 inAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
235 if (bind(sock, (struct sockaddr*)&inAddr, sizeof(inAddr)) < 0)
237 errorStr = "RADIUS: Bind failed.";
238 printfd(__FILE__, "Cannot bind socket\n");
244 //-----------------------------------------------------------------------------
245 int RADIUS::FinalizeNet()
250 //-----------------------------------------------------------------------------
253 std::string password(radSettings.GetPassword());
255 authServices = radSettings.GetAuthServices();
256 acctServices = radSettings.GetAcctServices();
258 InitEncrypt(&ctx, password);
269 if (pthread_create(&thread, NULL, Run, this))
271 errorStr = "Cannot create thread.";
272 printfd(__FILE__, "Cannot create thread\n");
280 //-----------------------------------------------------------------------------
288 std::map<std::string, RAD_SESSION>::iterator it;
289 for (it = sessions.begin(); it != sessions.end(); ++it)
292 if (users->FindByName(it->second.userName, &ui))
294 ui->Unauthorize(this);
297 sessions.erase(sessions.begin(), sessions.end());
303 //5 seconds to thread stops itself
304 for (int i = 0; i < 25 && isRunning; i++)
309 //after 5 seconds waiting thread still running. now killing it
312 if (pthread_kill(thread, SIGINT))
314 errorStr = "Cannot kill thread.";
315 printfd(__FILE__, "Cannot kill thread\n");
318 printfd(__FILE__, "RADIUS::Stop killed Run\n");
324 //-----------------------------------------------------------------------------
325 void * RADIUS::Run(void * d)
327 RADIUS * rad = (RADIUS *)d;
330 rad->isRunning = true;
334 if (!rad->WaitPackets(rad->sock))
338 struct sockaddr_in outerAddr;
339 if (rad->RecvData(&packet, &outerAddr))
341 printfd(__FILE__, "RADIUS::Run Error on RecvData\n");
345 if (rad->ProcessData(&packet))
347 packet.packetType = RAD_REJECT_PACKET;
349 rad->Send(packet, &outerAddr);
353 rad->isRunning = false;
357 //-----------------------------------------------------------------------------
358 int RADIUS::RecvData(RAD_PACKET * packet, struct sockaddr_in * outerAddr)
360 int8_t buf[RAD_MAX_PACKET_LEN];
361 socklen_t outerAddrLen = sizeof(struct sockaddr_in);
362 int dataLen = recvfrom(sock, buf, RAD_MAX_PACKET_LEN, 0, reinterpret_cast<struct sockaddr *>(outerAddr), &outerAddrLen);
364 Decrypt(&ctx, (char *)packet, (const char *)buf, dataLen / 8);
366 if (strncmp((char *)packet->magic, RAD_ID, RAD_MAGIC_LEN))
368 printfd(__FILE__, "RADIUS::RecvData Error magic. Wanted: '%s', got: '%s'\n", RAD_ID, packet->magic);
373 //-----------------------------------------------------------------------------
374 int RADIUS::Send(const RAD_PACKET & packet, struct sockaddr_in * outerAddr)
376 size_t len = sizeof(RAD_PACKET);
379 Encrypt(&ctx, buf, (char *)&packet, len / 8);
380 return sendto(sock, buf, len, 0, reinterpret_cast<struct sockaddr *>(outerAddr), sizeof(struct sockaddr_in));
382 //-----------------------------------------------------------------------------
383 int RADIUS::ProcessData(RAD_PACKET * packet)
385 if (strncmp((const char *)packet->protoVer, "01", 2))
387 printfd(__FILE__, "RADIUS::ProcessData packet.protoVer incorrect\n");
390 switch (packet->packetType)
392 case RAD_AUTZ_PACKET:
393 return ProcessAutzPacket(packet);
394 case RAD_AUTH_PACKET:
395 return ProcessAuthPacket(packet);
396 case RAD_POST_AUTH_PACKET:
397 return ProcessPostAuthPacket(packet);
398 case RAD_ACCT_START_PACKET:
399 return ProcessAcctStartPacket(packet);
400 case RAD_ACCT_STOP_PACKET:
401 return ProcessAcctStopPacket(packet);
402 case RAD_ACCT_UPDATE_PACKET:
403 return ProcessAcctUpdatePacket(packet);
404 case RAD_ACCT_OTHER_PACKET:
405 return ProcessAcctOtherPacket(packet);
407 printfd(__FILE__, "RADIUS::ProcessData Unsupported packet type: %d\n", packet->packetType);
412 //-----------------------------------------------------------------------------
413 int RADIUS::ProcessAutzPacket(RAD_PACKET * packet)
417 if (!IsAllowedService((char *)packet->service))
419 printfd(__FILE__, "RADIUS::ProcessAutzPacket service '%s' is not allowed to authorize\n", packet->service);
420 packet->packetType = RAD_REJECT_PACKET;
424 if (store->RestoreUserConf(&conf, (char *)packet->login))
426 packet->packetType = RAD_REJECT_PACKET;
427 printfd(__FILE__, "RADIUS::ProcessAutzPacket cannot restore conf for user '%s'\n", packet->login);
431 // At this point service can be authorized at least
432 // So we send a plain-text password
434 packet->packetType = RAD_ACCEPT_PACKET;
435 strncpy((char *)packet->password, conf.password.c_str(), RAD_PASSWORD_LEN);
439 //-----------------------------------------------------------------------------
440 int RADIUS::ProcessAuthPacket(RAD_PACKET * packet)
444 if (!CanAcctService((char *)packet->service))
447 // There are no sense to check for allowed service
448 // It has allready checked at previous stage (authorization)
450 printfd(__FILE__, "RADIUS::ProcessAuthPacket service '%s' neednot stargazer authentication\n", (char *)packet->service);
451 packet->packetType = RAD_ACCEPT_PACKET;
455 // At this point we have an accountable service
456 // All other services got a password if allowed or rejected
458 if (!FindUser(&ui, (char *)packet->login))
460 packet->packetType = RAD_REJECT_PACKET;
461 printfd(__FILE__, "RADIUS::ProcessAuthPacket user '%s' not found\n", (char *)packet->login);
465 if (ui->IsInetable())
467 packet->packetType = RAD_ACCEPT_PACKET;
471 packet->packetType = RAD_REJECT_PACKET;
474 packet->packetType = RAD_ACCEPT_PACKET;
477 //-----------------------------------------------------------------------------
478 int RADIUS::ProcessPostAuthPacket(RAD_PACKET * packet)
482 if (!CanAcctService((char *)packet->service))
485 // There are no sense to check for allowed service
486 // It has allready checked at previous stage (authorization)
488 packet->packetType = RAD_ACCEPT_PACKET;
492 if (!FindUser(&ui, (char *)packet->login))
494 packet->packetType = RAD_REJECT_PACKET;
495 printfd(__FILE__, "RADIUS::ProcessPostAuthPacket user '%s' not found\n", (char *)packet->login);
499 // I think that only Framed-User services has sense to be accountable
500 // So we have to supply a Framed-IP
502 USER_IPS ips = ui->GetProperty().ips;
503 packet->packetType = RAD_ACCEPT_PACKET;
505 // Additional checking for Framed-User service
507 if (!strncmp((char *)packet->service, "Framed-User", RAD_SERVICE_LEN))
508 packet->ip = ips[0].ip;
514 //-----------------------------------------------------------------------------
515 int RADIUS::ProcessAcctStartPacket(RAD_PACKET * packet)
519 if (!FindUser(&ui, (char *)packet->login))
521 packet->packetType = RAD_REJECT_PACKET;
522 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket user '%s' not found\n", (char *)packet->login);
526 // At this point we have to unauthorize user only if it is an accountable service
528 if (CanAcctService((char *)packet->service))
530 if (sessions.find((const char *)packet->sessid) != sessions.end())
532 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket session already started!\n");
533 packet->packetType = RAD_REJECT_PACKET;
536 USER_IPS ips = ui->GetProperty().ips;
537 if (ui->Authorize(ips[0].ip, 0xffFFffFF, this))
539 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket cannot authorize user '%s'\n", packet->login);
540 packet->packetType = RAD_REJECT_PACKET;
543 sessions[(const char *)packet->sessid].userName = (const char *)packet->login;
544 sessions[(const char *)packet->sessid].serviceType = (const char *)packet->service;
545 for_each(sessions.begin(), sessions.end(), SPrinter());
549 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket service '%s' can not be accounted\n", (char *)packet->service);
552 packet->packetType = RAD_ACCEPT_PACKET;
555 //-----------------------------------------------------------------------------
556 int RADIUS::ProcessAcctStopPacket(RAD_PACKET * packet)
558 std::map<std::string, RAD_SESSION>::iterator sid;
560 if ((sid = sessions.find((const char *)packet->sessid)) == sessions.end())
562 printfd(__FILE__, "RADIUS::ProcessAcctStopPacket session had not started yet\n");
563 packet->packetType = RAD_REJECT_PACKET;
569 if (!FindUser(&ui, sid->second.userName))
571 packet->packetType = RAD_REJECT_PACKET;
572 printfd(__FILE__, "RADIUS::ProcessPostAuthPacket user '%s' not found\n", sid->second.userName.c_str());
578 ui->Unauthorize(this);
580 packet->packetType = RAD_ACCEPT_PACKET;
583 //-----------------------------------------------------------------------------
584 int RADIUS::ProcessAcctUpdatePacket(RAD_PACKET * packet)
586 // Fake. May be use it later
587 packet->packetType = RAD_ACCEPT_PACKET;
590 //-----------------------------------------------------------------------------
591 int RADIUS::ProcessAcctOtherPacket(RAD_PACKET * packet)
593 // Fake. May be use it later
594 packet->packetType = RAD_ACCEPT_PACKET;
597 //-----------------------------------------------------------------------------
598 void RADIUS::PrintServices(const std::list<std::string> & svcs)
600 for_each(svcs.begin(), svcs.end(), Printer());
602 //-----------------------------------------------------------------------------
603 bool RADIUS::FindUser(USER_PTR * ui, const std::string & login) const
605 if (users->FindByName(login, ui))
611 //-----------------------------------------------------------------------------
612 bool RADIUS::CanAuthService(const std::string & svc) const
614 return find(authServices.begin(), authServices.end(), svc) != authServices.end();
616 //-----------------------------------------------------------------------------
617 bool RADIUS::CanAcctService(const std::string & svc) const
619 return find(acctServices.begin(), acctServices.end(), svc) != acctServices.end();
621 //-----------------------------------------------------------------------------
622 bool RADIUS::IsAllowedService(const std::string & svc) const
624 return CanAuthService(svc) || CanAcctService(svc);
626 //-----------------------------------------------------------------------------
627 bool RADIUS::WaitPackets(int sd) const
637 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
638 if (res == -1) // Error
642 printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
647 if (res == 0) // Timeout
654 //-----------------------------------------------------------------------------
656 void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password)
658 unsigned char keyL[RAD_PASSWORD_LEN]; // Пароль для шифровки
659 memset(keyL, 0, RAD_PASSWORD_LEN);
660 strncpy((char *)keyL, password.c_str(), RAD_PASSWORD_LEN);
661 Blowfish_Init(ctx, keyL, RAD_PASSWORD_LEN);
663 //-----------------------------------------------------------------------------
665 void Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
667 // len8 - длина в 8-ми байтовых блоках
669 memcpy(dst, src, len8 * 8);
671 for (int i = 0; i < len8; i++)
672 Blowfish_Encrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));
674 //-----------------------------------------------------------------------------
676 void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
678 // len8 - длина в 8-ми байтовых блоках
680 memcpy(dst, src, len8 * 8);
682 for (int i = 0; i < len8; i++)
683 Blowfish_Decrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));