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 void InitEncrypt(BLOWFISH_CTX * ctx, const string & password);
38 void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8);
39 void Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8);
41 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
70 //-----------------------------------------------------------------------------
71 BASE_PLUGIN * GetPlugin()
73 return radc.GetPlugin();
75 //-----------------------------------------------------------------------------
76 //-----------------------------------------------------------------------------
77 //-----------------------------------------------------------------------------
78 int RAD_SETTINGS::ParseIntInRange(const string & str, int min, int max, int * val)
80 if (str2x(str.c_str(), *val))
82 errorStr = "Incorrect value \'" + str + "\'.";
85 if (*val < min || *val > max)
87 errorStr = "Value \'" + str + "\' out of range.";
92 //-----------------------------------------------------------------------------
93 int RAD_SETTINGS::ParseServices(const vector<string> & str, list<string> * lst)
95 copy(str.begin(), str.end(), back_inserter(*lst));
96 list<string>::iterator it(find(lst->begin(),
104 //-----------------------------------------------------------------------------
105 int RAD_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
109 vector<PARAM_VALUE>::const_iterator pvi;
110 ///////////////////////////
112 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
113 if (pvi == s.moduleParams.end())
115 errorStr = "Parameter \'Port\' not found.";
116 printfd(__FILE__, "Parameter 'Port' not found\n");
119 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
121 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
122 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
126 ///////////////////////////
127 pv.param = "Password";
128 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
129 if (pvi == s.moduleParams.end())
131 errorStr = "Parameter \'Password\' not found.";
132 printfd(__FILE__, "Parameter 'Password' not found\n");
135 password = pvi->value[0];
136 ///////////////////////////
137 pv.param = "AuthServices";
138 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
139 if (pvi != s.moduleParams.end())
141 ParseServices(pvi->value, &authServices);
143 ///////////////////////////
144 pv.param = "AcctServices";
145 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
146 if (pvi != s.moduleParams.end())
148 ParseServices(pvi->value, &acctServices);
153 //-----------------------------------------------------------------------------
154 //-----------------------------------------------------------------------------
155 //-----------------------------------------------------------------------------
165 //-----------------------------------------------------------------------------
166 void RADIUS::SetUsers(USERS * u)
170 //-----------------------------------------------------------------------------
171 void RADIUS::SetStgSettings(const SETTINGS * s)
175 //-----------------------------------------------------------------------------
176 void RADIUS::SetSettings(const MODULE_SETTINGS & s)
180 //-----------------------------------------------------------------------------
181 void RADIUS::SetStore(BASE_STORE * s)
185 //-----------------------------------------------------------------------------
186 int RADIUS::ParseSettings()
188 int ret = radSettings.ParseSettings(settings);
190 errorStr = radSettings.GetStrError();
193 //-----------------------------------------------------------------------------
194 bool RADIUS::IsRunning()
198 //-----------------------------------------------------------------------------
199 const string RADIUS::GetVersion() const
201 return "RADIUS data access plugin v 0.6";
203 //-----------------------------------------------------------------------------
204 uint16_t RADIUS::GetStartPosition() const
206 // Start before any authorizers!!!
209 //-----------------------------------------------------------------------------
210 uint16_t RADIUS::GetStopPosition() const
214 //-----------------------------------------------------------------------------
215 int RADIUS::PrepareNet()
217 sock = socket(AF_INET, SOCK_DGRAM, 0);
221 errorStr = "Cannot create socket.";
222 printfd(__FILE__, "Cannot create socket\n");
226 struct sockaddr_in inAddr;
227 inAddr.sin_family = AF_INET;
228 inAddr.sin_port = htons(radSettings.GetPort());
229 inAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
231 if (bind(sock, (struct sockaddr*)&inAddr, sizeof(inAddr)) < 0)
233 errorStr = "RADIUS: Bind failed.";
234 printfd(__FILE__, "Cannot bind socket\n");
240 //-----------------------------------------------------------------------------
241 int RADIUS::FinalizeNet()
246 //-----------------------------------------------------------------------------
249 string password(radSettings.GetPassword());
251 authServices = radSettings.GetAuthServices();
252 acctServices = radSettings.GetAcctServices();
254 InitEncrypt(&ctx, password);
265 if (pthread_create(&thread, NULL, Run, this))
267 errorStr = "Cannot create thread.";
268 printfd(__FILE__, "Cannot create thread\n");
276 //-----------------------------------------------------------------------------
284 map<string, RAD_SESSION>::iterator it;
285 for (it = sessions.begin(); it != sessions.end(); ++it)
288 if (users->FindByName(it->second.userName, &ui))
290 ui->Unauthorize(this);
293 sessions.erase(sessions.begin(), sessions.end());
299 //5 seconds to thread stops itself
300 for (int i = 0; i < 25 && isRunning; i++)
305 //after 5 seconds waiting thread still running. now killing it
308 if (pthread_kill(thread, SIGINT))
310 errorStr = "Cannot kill thread.";
311 printfd(__FILE__, "Cannot kill thread\n");
314 printfd(__FILE__, "RADIUS::Stop killed Run\n");
320 //-----------------------------------------------------------------------------
321 void * RADIUS::Run(void * d)
323 RADIUS * rad = (RADIUS *)d;
326 rad->isRunning = true;
330 if (!rad->WaitPackets(rad->sock))
334 struct sockaddr_in outerAddr;
335 if (rad->RecvData(&packet, &outerAddr))
337 printfd(__FILE__, "RADIUS::Run Error on RecvData\n");
341 if (rad->ProcessData(&packet))
343 packet.packetType = RAD_REJECT_PACKET;
345 rad->Send(packet, &outerAddr);
349 rad->isRunning = false;
353 //-----------------------------------------------------------------------------
354 int RADIUS::RecvData(RAD_PACKET * packet, struct sockaddr_in * outerAddr)
356 int8_t buf[RAD_MAX_PACKET_LEN];
357 socklen_t outerAddrLen = sizeof(struct sockaddr_in);
358 int dataLen = recvfrom(sock, buf, RAD_MAX_PACKET_LEN, 0, reinterpret_cast<struct sockaddr *>(outerAddr), &outerAddrLen);
360 Decrypt(&ctx, (char *)packet, (const char *)buf, dataLen / 8);
362 if (strncmp((char *)packet->magic, RAD_ID, RAD_MAGIC_LEN))
364 printfd(__FILE__, "RADIUS::RecvData Error magic. Wanted: '%s', got: '%s'\n", RAD_ID, packet->magic);
369 //-----------------------------------------------------------------------------
370 int RADIUS::Send(const RAD_PACKET & packet, struct sockaddr_in * outerAddr)
372 size_t len = sizeof(RAD_PACKET);
375 Encrypt(&ctx, buf, (char *)&packet, len / 8);
376 return sendto(sock, buf, len, 0, reinterpret_cast<struct sockaddr *>(outerAddr), sizeof(struct sockaddr_in));
378 //-----------------------------------------------------------------------------
379 int RADIUS::ProcessData(RAD_PACKET * packet)
381 if (strncmp((const char *)packet->protoVer, "01", 2))
383 printfd(__FILE__, "RADIUS::ProcessData packet.protoVer incorrect\n");
386 switch (packet->packetType)
388 case RAD_AUTZ_PACKET:
389 return ProcessAutzPacket(packet);
390 case RAD_AUTH_PACKET:
391 return ProcessAuthPacket(packet);
392 case RAD_POST_AUTH_PACKET:
393 return ProcessPostAuthPacket(packet);
394 case RAD_ACCT_START_PACKET:
395 return ProcessAcctStartPacket(packet);
396 case RAD_ACCT_STOP_PACKET:
397 return ProcessAcctStopPacket(packet);
398 case RAD_ACCT_UPDATE_PACKET:
399 return ProcessAcctUpdatePacket(packet);
400 case RAD_ACCT_OTHER_PACKET:
401 return ProcessAcctOtherPacket(packet);
403 printfd(__FILE__, "RADIUS::ProcessData Unsupported packet type: %d\n", packet->packetType);
408 //-----------------------------------------------------------------------------
409 int RADIUS::ProcessAutzPacket(RAD_PACKET * packet)
413 if (!IsAllowedService((char *)packet->service))
415 printfd(__FILE__, "RADIUS::ProcessAutzPacket service '%s' is not allowed to authorize\n", packet->service);
416 packet->packetType = RAD_REJECT_PACKET;
420 if (store->RestoreUserConf(&conf, (char *)packet->login))
422 packet->packetType = RAD_REJECT_PACKET;
423 printfd(__FILE__, "RADIUS::ProcessAutzPacket cannot restore conf for user '%s'\n", packet->login);
427 // At this point service can be authorized at least
428 // So we send a plain-text password
430 packet->packetType = RAD_ACCEPT_PACKET;
431 strncpy((char *)packet->password, conf.password.c_str(), RAD_PASSWORD_LEN);
435 //-----------------------------------------------------------------------------
436 int RADIUS::ProcessAuthPacket(RAD_PACKET * packet)
440 if (!CanAcctService((char *)packet->service))
443 // There are no sense to check for allowed service
444 // It has allready checked at previous stage (authorization)
446 printfd(__FILE__, "RADIUS::ProcessAuthPacket service '%s' neednot stargazer authentication\n", (char *)packet->service);
447 packet->packetType = RAD_ACCEPT_PACKET;
451 // At this point we have an accountable service
452 // All other services got a password if allowed or rejected
454 if (!FindUser(&ui, (char *)packet->login))
456 packet->packetType = RAD_REJECT_PACKET;
457 printfd(__FILE__, "RADIUS::ProcessAuthPacket user '%s' not found\n", (char *)packet->login);
461 if (ui->IsInetable())
463 packet->packetType = RAD_ACCEPT_PACKET;
467 packet->packetType = RAD_REJECT_PACKET;
470 packet->packetType = RAD_ACCEPT_PACKET;
473 //-----------------------------------------------------------------------------
474 int RADIUS::ProcessPostAuthPacket(RAD_PACKET * packet)
478 if (!CanAcctService((char *)packet->service))
481 // There are no sense to check for allowed service
482 // It has allready checked at previous stage (authorization)
484 packet->packetType = RAD_ACCEPT_PACKET;
488 if (!FindUser(&ui, (char *)packet->login))
490 packet->packetType = RAD_REJECT_PACKET;
491 printfd(__FILE__, "RADIUS::ProcessPostAuthPacket user '%s' not found\n", (char *)packet->login);
495 // I think that only Framed-User services has sense to be accountable
496 // So we have to supply a Framed-IP
498 USER_IPS ips = ui->property.ips;
499 packet->packetType = RAD_ACCEPT_PACKET;
501 // Additional checking for Framed-User service
503 if (!strncmp((char *)packet->service, "Framed-User", RAD_SERVICE_LEN))
504 packet->ip = ips[0].ip;
510 //-----------------------------------------------------------------------------
511 int RADIUS::ProcessAcctStartPacket(RAD_PACKET * packet)
515 if (!FindUser(&ui, (char *)packet->login))
517 packet->packetType = RAD_REJECT_PACKET;
518 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket user '%s' not found\n", (char *)packet->login);
522 // At this point we have to unauthorize user only if it is an accountable service
524 if (CanAcctService((char *)packet->service))
526 if (sessions.find((const char *)packet->sessid) != sessions.end())
528 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket session already started!\n");
529 packet->packetType = RAD_REJECT_PACKET;
532 USER_IPS ips = ui->property.ips;
533 if (ui->Authorize(ips[0].ip, "", 0xffFFffFF, this))
535 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket cannot authorize user '%s'\n", packet->login);
536 packet->packetType = RAD_REJECT_PACKET;
539 sessions[(const char *)packet->sessid].userName = (const char *)packet->login;
540 sessions[(const char *)packet->sessid].serviceType = (const char *)packet->service;
541 for_each(sessions.begin(), sessions.end(), SPrinter());
545 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket service '%s' can not be accounted\n", (char *)packet->service);
548 packet->packetType = RAD_ACCEPT_PACKET;
551 //-----------------------------------------------------------------------------
552 int RADIUS::ProcessAcctStopPacket(RAD_PACKET * packet)
554 map<string, RAD_SESSION>::iterator sid;
556 if ((sid = sessions.find((const char *)packet->sessid)) == sessions.end())
558 printfd(__FILE__, "RADIUS::ProcessAcctStopPacket session had not started yet\n");
559 packet->packetType = RAD_REJECT_PACKET;
565 if (!FindUser(&ui, sid->second.userName))
567 packet->packetType = RAD_REJECT_PACKET;
568 printfd(__FILE__, "RADIUS::ProcessPostAuthPacket user '%s' not found\n", sid->second.userName.c_str());
574 ui->Unauthorize(this);
576 packet->packetType = RAD_ACCEPT_PACKET;
579 //-----------------------------------------------------------------------------
580 int RADIUS::ProcessAcctUpdatePacket(RAD_PACKET * packet)
582 // Fake. May be use it later
583 packet->packetType = RAD_ACCEPT_PACKET;
586 //-----------------------------------------------------------------------------
587 int RADIUS::ProcessAcctOtherPacket(RAD_PACKET * packet)
589 // Fake. May be use it later
590 packet->packetType = RAD_ACCEPT_PACKET;
593 //-----------------------------------------------------------------------------
594 void RADIUS::PrintServices(const list<string> & svcs)
596 for_each(svcs.begin(), svcs.end(), Printer());
598 //-----------------------------------------------------------------------------
599 bool RADIUS::FindUser(user_iter * ui, const std::string & login) const
601 if (users->FindByName(login, ui))
607 //-----------------------------------------------------------------------------
608 bool RADIUS::CanAuthService(const std::string & svc) const
610 return find(authServices.begin(), authServices.end(), svc) != authServices.end();
612 //-----------------------------------------------------------------------------
613 bool RADIUS::CanAcctService(const std::string & svc) const
615 return find(acctServices.begin(), acctServices.end(), svc) != acctServices.end();
617 //-----------------------------------------------------------------------------
618 bool RADIUS::IsAllowedService(const std::string & svc) const
620 return CanAuthService(svc) || CanAcctService(svc);
622 //-----------------------------------------------------------------------------
623 bool RADIUS::WaitPackets(int sd) const
633 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
634 if (res == -1) // Error
638 printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
643 if (res == 0) // Timeout
650 //-----------------------------------------------------------------------------
652 void InitEncrypt(BLOWFISH_CTX * ctx, const string & password)
654 unsigned char keyL[RAD_PASSWORD_LEN]; // Пароль для шифровки
655 memset(keyL, 0, RAD_PASSWORD_LEN);
656 strncpy((char *)keyL, password.c_str(), RAD_PASSWORD_LEN);
657 Blowfish_Init(ctx, keyL, RAD_PASSWORD_LEN);
659 //-----------------------------------------------------------------------------
661 void Encrypt(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_Encrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));
670 //-----------------------------------------------------------------------------
672 void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
674 // len8 - длина в 8-ми байтовых блоках
676 memcpy(dst, src, len8 * 8);
678 for (int i = 0; i < len8; i++)
679 Blowfish_Decrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));