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"
37 #include "stg/plugin_creator.h"
40 extern volatile const time_t stgTime;
42 void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password);
43 void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8);
44 void Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8);
46 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
49 PLUGIN_CREATOR<RADIUS> radc;
50 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
55 return radc.GetPlugin();
57 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
60 int RAD_SETTINGS::ParseServices(const std::vector<std::string> & str, std::list<std::string> * lst)
62 std::copy(str.begin(), str.end(), std::back_inserter(*lst));
63 std::list<std::string>::iterator it(std::find(lst->begin(),
71 //-----------------------------------------------------------------------------
72 int RAD_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
76 std::vector<PARAM_VALUE>::const_iterator pvi;
77 ///////////////////////////
79 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
80 if (pvi == s.moduleParams.end())
82 errorStr = "Parameter \'Port\' not found.";
83 printfd(__FILE__, "Parameter 'Port' not found\n");
86 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
88 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
89 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
93 ///////////////////////////
94 pv.param = "Password";
95 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
96 if (pvi == s.moduleParams.end())
98 errorStr = "Parameter \'Password\' not found.";
99 printfd(__FILE__, "Parameter 'Password' not found\n");
102 password = pvi->value[0];
103 ///////////////////////////
104 pv.param = "AuthServices";
105 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
106 if (pvi != s.moduleParams.end())
108 ParseServices(pvi->value, &authServices);
110 ///////////////////////////
111 pv.param = "AcctServices";
112 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
113 if (pvi != s.moduleParams.end())
115 ParseServices(pvi->value, &acctServices);
120 //-----------------------------------------------------------------------------
121 //-----------------------------------------------------------------------------
122 //-----------------------------------------------------------------------------
131 InitEncrypt(&ctx, "");
133 //-----------------------------------------------------------------------------
134 void RADIUS::SetUsers(USERS * u)
138 //-----------------------------------------------------------------------------
139 void RADIUS::SetStgSettings(const SETTINGS * s)
143 //-----------------------------------------------------------------------------
144 void RADIUS::SetSettings(const MODULE_SETTINGS & s)
148 //-----------------------------------------------------------------------------
149 void RADIUS::SetStore(STORE * s)
153 //-----------------------------------------------------------------------------
154 int RADIUS::ParseSettings()
156 int ret = radSettings.ParseSettings(settings);
158 errorStr = radSettings.GetStrError();
161 //-----------------------------------------------------------------------------
162 bool RADIUS::IsRunning()
166 //-----------------------------------------------------------------------------
167 const std::string RADIUS::GetVersion() const
169 return "RADIUS data access plugin v 0.6";
171 //-----------------------------------------------------------------------------
172 uint16_t RADIUS::GetStartPosition() const
174 // Start before any authorizers!!!
177 //-----------------------------------------------------------------------------
178 uint16_t RADIUS::GetStopPosition() const
182 //-----------------------------------------------------------------------------
183 int RADIUS::PrepareNet()
185 sock = socket(AF_INET, SOCK_DGRAM, 0);
189 errorStr = "Cannot create socket.";
190 printfd(__FILE__, "Cannot create socket\n");
194 struct sockaddr_in inAddr;
195 inAddr.sin_family = AF_INET;
196 inAddr.sin_port = htons(radSettings.GetPort());
197 inAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
199 if (bind(sock, (struct sockaddr*)&inAddr, sizeof(inAddr)) < 0)
201 errorStr = "RADIUS: Bind failed.";
202 printfd(__FILE__, "Cannot bind socket\n");
208 //-----------------------------------------------------------------------------
209 int RADIUS::FinalizeNet()
214 //-----------------------------------------------------------------------------
217 std::string password(radSettings.GetPassword());
219 authServices = radSettings.GetAuthServices();
220 acctServices = radSettings.GetAcctServices();
222 InitEncrypt(&ctx, password);
233 if (pthread_create(&thread, NULL, Run, this))
235 errorStr = "Cannot create thread.";
236 printfd(__FILE__, "Cannot create thread\n");
244 //-----------------------------------------------------------------------------
252 std::map<std::string, RAD_SESSION>::iterator it;
253 for (it = sessions.begin(); it != sessions.end(); ++it)
256 if (users->FindByName(it->second.userName, &ui))
258 ui->Unauthorize(this);
261 sessions.erase(sessions.begin(), sessions.end());
267 //5 seconds to thread stops itself
268 for (int i = 0; i < 25 && isRunning; i++)
273 //after 5 seconds waiting thread still running. now killing it
276 if (pthread_kill(thread, SIGINT))
278 errorStr = "Cannot kill thread.";
279 printfd(__FILE__, "Cannot kill thread\n");
282 printfd(__FILE__, "RADIUS::Stop killed Run\n");
288 //-----------------------------------------------------------------------------
289 void * RADIUS::Run(void * d)
291 RADIUS * rad = (RADIUS *)d;
294 rad->isRunning = true;
298 if (!rad->WaitPackets(rad->sock))
302 struct sockaddr_in outerAddr;
303 if (rad->RecvData(&packet, &outerAddr))
305 printfd(__FILE__, "RADIUS::Run Error on RecvData\n");
309 if (rad->ProcessData(&packet))
311 packet.packetType = RAD_REJECT_PACKET;
313 rad->Send(packet, &outerAddr);
317 rad->isRunning = false;
321 //-----------------------------------------------------------------------------
322 int RADIUS::RecvData(RAD_PACKET * packet, struct sockaddr_in * outerAddr)
324 int8_t buf[RAD_MAX_PACKET_LEN];
325 socklen_t outerAddrLen = sizeof(struct sockaddr_in);
326 int dataLen = recvfrom(sock, buf, RAD_MAX_PACKET_LEN, 0, reinterpret_cast<struct sockaddr *>(outerAddr), &outerAddrLen);
328 Decrypt(&ctx, (char *)packet, (const char *)buf, dataLen / 8);
330 if (strncmp((char *)packet->magic, RAD_ID, RAD_MAGIC_LEN))
332 printfd(__FILE__, "RADIUS::RecvData Error magic. Wanted: '%s', got: '%s'\n", RAD_ID, packet->magic);
337 //-----------------------------------------------------------------------------
338 int RADIUS::Send(const RAD_PACKET & packet, struct sockaddr_in * outerAddr)
340 size_t len = sizeof(RAD_PACKET);
343 Encrypt(&ctx, buf, (char *)&packet, len / 8);
344 return sendto(sock, buf, len, 0, reinterpret_cast<struct sockaddr *>(outerAddr), sizeof(struct sockaddr_in));
346 //-----------------------------------------------------------------------------
347 int RADIUS::ProcessData(RAD_PACKET * packet)
349 if (strncmp((const char *)packet->protoVer, "01", 2))
351 printfd(__FILE__, "RADIUS::ProcessData packet.protoVer incorrect\n");
354 switch (packet->packetType)
356 case RAD_AUTZ_PACKET:
357 return ProcessAutzPacket(packet);
358 case RAD_AUTH_PACKET:
359 return ProcessAuthPacket(packet);
360 case RAD_POST_AUTH_PACKET:
361 return ProcessPostAuthPacket(packet);
362 case RAD_ACCT_START_PACKET:
363 return ProcessAcctStartPacket(packet);
364 case RAD_ACCT_STOP_PACKET:
365 return ProcessAcctStopPacket(packet);
366 case RAD_ACCT_UPDATE_PACKET:
367 return ProcessAcctUpdatePacket(packet);
368 case RAD_ACCT_OTHER_PACKET:
369 return ProcessAcctOtherPacket(packet);
371 printfd(__FILE__, "RADIUS::ProcessData Unsupported packet type: %d\n", packet->packetType);
376 //-----------------------------------------------------------------------------
377 int RADIUS::ProcessAutzPacket(RAD_PACKET * packet)
381 if (!IsAllowedService((char *)packet->service))
383 printfd(__FILE__, "RADIUS::ProcessAutzPacket service '%s' is not allowed to authorize\n", packet->service);
384 packet->packetType = RAD_REJECT_PACKET;
388 if (store->RestoreUserConf(&conf, (char *)packet->login))
390 packet->packetType = RAD_REJECT_PACKET;
391 printfd(__FILE__, "RADIUS::ProcessAutzPacket cannot restore conf for user '%s'\n", packet->login);
395 // At this point service can be authorized at least
396 // So we send a plain-text password
398 packet->packetType = RAD_ACCEPT_PACKET;
399 strncpy((char *)packet->password, conf.password.c_str(), RAD_PASSWORD_LEN);
403 //-----------------------------------------------------------------------------
404 int RADIUS::ProcessAuthPacket(RAD_PACKET * packet)
408 if (!CanAcctService((char *)packet->service))
411 // There are no sense to check for allowed service
412 // It has allready checked at previous stage (authorization)
414 printfd(__FILE__, "RADIUS::ProcessAuthPacket service '%s' neednot stargazer authentication\n", (char *)packet->service);
415 packet->packetType = RAD_ACCEPT_PACKET;
419 // At this point we have an accountable service
420 // All other services got a password if allowed or rejected
422 if (!FindUser(&ui, (char *)packet->login))
424 packet->packetType = RAD_REJECT_PACKET;
425 printfd(__FILE__, "RADIUS::ProcessAuthPacket user '%s' not found\n", (char *)packet->login);
429 if (ui->IsInetable())
431 packet->packetType = RAD_ACCEPT_PACKET;
435 packet->packetType = RAD_REJECT_PACKET;
438 packet->packetType = RAD_ACCEPT_PACKET;
441 //-----------------------------------------------------------------------------
442 int RADIUS::ProcessPostAuthPacket(RAD_PACKET * packet)
446 if (!CanAcctService((char *)packet->service))
449 // There are no sense to check for allowed service
450 // It has allready checked at previous stage (authorization)
452 packet->packetType = RAD_ACCEPT_PACKET;
456 if (!FindUser(&ui, (char *)packet->login))
458 packet->packetType = RAD_REJECT_PACKET;
459 printfd(__FILE__, "RADIUS::ProcessPostAuthPacket user '%s' not found\n", (char *)packet->login);
463 // I think that only Framed-User services has sense to be accountable
464 // So we have to supply a Framed-IP
466 USER_IPS ips = ui->GetProperty().ips;
467 packet->packetType = RAD_ACCEPT_PACKET;
469 // Additional checking for Framed-User service
471 if (!strncmp((char *)packet->service, "Framed-User", RAD_SERVICE_LEN))
472 packet->ip = ips[0].ip;
478 //-----------------------------------------------------------------------------
479 int RADIUS::ProcessAcctStartPacket(RAD_PACKET * packet)
483 if (!FindUser(&ui, (char *)packet->login))
485 packet->packetType = RAD_REJECT_PACKET;
486 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket user '%s' not found\n", (char *)packet->login);
490 // At this point we have to unauthorize user only if it is an accountable service
492 if (CanAcctService((char *)packet->service))
494 if (sessions.find((const char *)packet->sessid) != sessions.end())
496 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket session already started!\n");
497 packet->packetType = RAD_REJECT_PACKET;
500 USER_IPS ips = ui->GetProperty().ips;
501 if (ui->Authorize(ips[0].ip, 0xffFFffFF, this))
503 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket cannot authorize user '%s'\n", packet->login);
504 packet->packetType = RAD_REJECT_PACKET;
507 sessions[(const char *)packet->sessid].userName = (const char *)packet->login;
508 sessions[(const char *)packet->sessid].serviceType = (const char *)packet->service;
509 for_each(sessions.begin(), sessions.end(), SPrinter());
513 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket service '%s' can not be accounted\n", (char *)packet->service);
516 packet->packetType = RAD_ACCEPT_PACKET;
519 //-----------------------------------------------------------------------------
520 int RADIUS::ProcessAcctStopPacket(RAD_PACKET * packet)
522 std::map<std::string, RAD_SESSION>::iterator sid;
524 if ((sid = sessions.find((const char *)packet->sessid)) == sessions.end())
526 printfd(__FILE__, "RADIUS::ProcessAcctStopPacket session had not started yet\n");
527 packet->packetType = RAD_REJECT_PACKET;
533 if (!FindUser(&ui, sid->second.userName))
535 packet->packetType = RAD_REJECT_PACKET;
536 printfd(__FILE__, "RADIUS::ProcessPostAuthPacket user '%s' not found\n", sid->second.userName.c_str());
542 ui->Unauthorize(this);
544 packet->packetType = RAD_ACCEPT_PACKET;
547 //-----------------------------------------------------------------------------
548 int RADIUS::ProcessAcctUpdatePacket(RAD_PACKET * packet)
550 // Fake. May be use it later
551 packet->packetType = RAD_ACCEPT_PACKET;
554 //-----------------------------------------------------------------------------
555 int RADIUS::ProcessAcctOtherPacket(RAD_PACKET * packet)
557 // Fake. May be use it later
558 packet->packetType = RAD_ACCEPT_PACKET;
561 //-----------------------------------------------------------------------------
562 void RADIUS::PrintServices(const std::list<std::string> & svcs)
564 for_each(svcs.begin(), svcs.end(), Printer());
566 //-----------------------------------------------------------------------------
567 bool RADIUS::FindUser(USER_PTR * ui, const std::string & login) const
569 if (users->FindByName(login, ui))
575 //-----------------------------------------------------------------------------
576 bool RADIUS::CanAuthService(const std::string & svc) const
578 return find(authServices.begin(), authServices.end(), svc) != authServices.end();
580 //-----------------------------------------------------------------------------
581 bool RADIUS::CanAcctService(const std::string & svc) const
583 return find(acctServices.begin(), acctServices.end(), svc) != acctServices.end();
585 //-----------------------------------------------------------------------------
586 bool RADIUS::IsAllowedService(const std::string & svc) const
588 return CanAuthService(svc) || CanAcctService(svc);
590 //-----------------------------------------------------------------------------
591 bool RADIUS::WaitPackets(int sd) const
601 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
602 if (res == -1) // Error
606 printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
611 if (res == 0) // Timeout
618 //-----------------------------------------------------------------------------
620 void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password)
622 unsigned char keyL[RAD_PASSWORD_LEN]; // Пароль для шифровки
623 memset(keyL, 0, RAD_PASSWORD_LEN);
624 strncpy((char *)keyL, password.c_str(), RAD_PASSWORD_LEN);
625 Blowfish_Init(ctx, keyL, RAD_PASSWORD_LEN);
627 //-----------------------------------------------------------------------------
629 void Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
631 // len8 - длина в 8-ми байтовых блоках
633 memcpy(dst, src, len8 * 8);
635 for (int i = 0; i < len8; i++)
636 Blowfish_Encrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));
638 //-----------------------------------------------------------------------------
640 void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
642 // len8 - длина в 8-ми байтовых блоках
644 memcpy(dst, src, len8 * 8);
646 for (int i = 0; i < len8; i++)
647 Blowfish_Decrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));