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 //-----------------------------------------------------------------------------
141 InitEncrypt(&ctx, "");
143 //-----------------------------------------------------------------------------
144 int RADIUS::ParseSettings()
146 int ret = radSettings.ParseSettings(settings);
148 errorStr = radSettings.GetStrError();
151 //-----------------------------------------------------------------------------
152 int RADIUS::PrepareNet()
154 sock = socket(AF_INET, SOCK_DGRAM, 0);
158 errorStr = "Cannot create socket.";
159 printfd(__FILE__, "Cannot create socket\n");
163 struct sockaddr_in inAddr;
164 inAddr.sin_family = AF_INET;
165 inAddr.sin_port = htons(radSettings.GetPort());
166 inAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
168 if (bind(sock, (struct sockaddr*)&inAddr, sizeof(inAddr)) < 0)
170 errorStr = "RADIUS: Bind failed.";
171 printfd(__FILE__, "Cannot bind socket\n");
177 //-----------------------------------------------------------------------------
178 int RADIUS::FinalizeNet()
183 //-----------------------------------------------------------------------------
186 std::string password(radSettings.GetPassword());
188 authServices = radSettings.GetAuthServices();
189 acctServices = radSettings.GetAcctServices();
191 InitEncrypt(&ctx, password);
202 if (pthread_create(&thread, NULL, Run, this))
204 errorStr = "Cannot create thread.";
205 printfd(__FILE__, "Cannot create thread\n");
213 //-----------------------------------------------------------------------------
221 std::map<std::string, RAD_SESSION>::iterator it;
222 for (it = sessions.begin(); it != sessions.end(); ++it)
225 if (users->FindByName(it->second.userName, &ui))
227 users->Unauthorize(ui->GetLogin(), this);
230 sessions.erase(sessions.begin(), sessions.end());
236 //5 seconds to thread stops itself
237 for (int i = 0; i < 25 && isRunning; i++)
239 struct timespec ts = {0, 200000000};
240 nanosleep(&ts, NULL);
243 //after 5 seconds waiting thread still running. now killing it
246 if (pthread_kill(thread, SIGINT))
248 errorStr = "Cannot kill thread.";
249 printfd(__FILE__, "Cannot kill thread\n");
252 printfd(__FILE__, "RADIUS::Stop killed Run\n");
258 //-----------------------------------------------------------------------------
259 void * RADIUS::Run(void * d)
262 sigfillset(&signalSet);
263 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
265 RADIUS * rad = (RADIUS *)d;
268 rad->isRunning = true;
272 if (!WaitPackets(rad->sock))
276 struct sockaddr_in outerAddr;
277 if (rad->RecvData(&packet, &outerAddr))
279 printfd(__FILE__, "RADIUS::Run Error on RecvData\n");
283 if (rad->ProcessData(&packet))
285 packet.packetType = RAD_REJECT_PACKET;
287 rad->Send(packet, &outerAddr);
291 rad->isRunning = false;
295 //-----------------------------------------------------------------------------
296 int RADIUS::RecvData(RAD_PACKET * packet, struct sockaddr_in * outerAddr)
298 int8_t buf[RAD_MAX_PACKET_LEN];
299 socklen_t outerAddrLen = sizeof(struct sockaddr_in);
300 int dataLen = recvfrom(sock, buf, RAD_MAX_PACKET_LEN, 0, reinterpret_cast<struct sockaddr *>(outerAddr), &outerAddrLen);
302 Decrypt(&ctx, (char *)packet, (const char *)buf, dataLen / 8);
304 if (strncmp((char *)packet->magic, RAD_ID, RAD_MAGIC_LEN))
306 printfd(__FILE__, "RADIUS::RecvData Error magic. Wanted: '%s', got: '%s'\n", RAD_ID, packet->magic);
311 //-----------------------------------------------------------------------------
312 int RADIUS::Send(const RAD_PACKET & packet, struct sockaddr_in * outerAddr)
314 size_t len = sizeof(RAD_PACKET);
317 Encrypt(&ctx, buf, (char *)&packet, len / 8);
318 return sendto(sock, buf, len, 0, reinterpret_cast<struct sockaddr *>(outerAddr), sizeof(struct sockaddr_in));
320 //-----------------------------------------------------------------------------
321 int RADIUS::ProcessData(RAD_PACKET * packet)
323 if (strncmp((const char *)packet->protoVer, "01", 2))
325 printfd(__FILE__, "RADIUS::ProcessData packet.protoVer incorrect\n");
328 switch (packet->packetType)
330 case RAD_AUTZ_PACKET:
331 return ProcessAutzPacket(packet);
332 case RAD_AUTH_PACKET:
333 return ProcessAuthPacket(packet);
334 case RAD_POST_AUTH_PACKET:
335 return ProcessPostAuthPacket(packet);
336 case RAD_ACCT_START_PACKET:
337 return ProcessAcctStartPacket(packet);
338 case RAD_ACCT_STOP_PACKET:
339 return ProcessAcctStopPacket(packet);
340 case RAD_ACCT_UPDATE_PACKET:
341 return ProcessAcctUpdatePacket(packet);
342 case RAD_ACCT_OTHER_PACKET:
343 return ProcessAcctOtherPacket(packet);
345 printfd(__FILE__, "RADIUS::ProcessData Unsupported packet type: %d\n", packet->packetType);
350 //-----------------------------------------------------------------------------
351 int RADIUS::ProcessAutzPacket(RAD_PACKET * packet)
355 if (!IsAllowedService((char *)packet->service))
357 printfd(__FILE__, "RADIUS::ProcessAutzPacket service '%s' is not allowed to authorize\n", packet->service);
358 packet->packetType = RAD_REJECT_PACKET;
362 if (store->RestoreUserConf(&conf, (char *)packet->login))
364 packet->packetType = RAD_REJECT_PACKET;
365 printfd(__FILE__, "RADIUS::ProcessAutzPacket cannot restore conf for user '%s'\n", packet->login);
369 // At this point service can be authorized at least
370 // So we send a plain-text password
372 packet->packetType = RAD_ACCEPT_PACKET;
373 strncpy((char *)packet->password, conf.password.c_str(), RAD_PASSWORD_LEN);
377 //-----------------------------------------------------------------------------
378 int RADIUS::ProcessAuthPacket(RAD_PACKET * packet)
382 if (!CanAcctService((char *)packet->service))
385 // There are no sense to check for allowed service
386 // It has allready checked at previous stage (authorization)
388 printfd(__FILE__, "RADIUS::ProcessAuthPacket service '%s' neednot stargazer authentication\n", (char *)packet->service);
389 packet->packetType = RAD_ACCEPT_PACKET;
393 // At this point we have an accountable service
394 // All other services got a password if allowed or rejected
396 if (!FindUser(&ui, (char *)packet->login))
398 packet->packetType = RAD_REJECT_PACKET;
399 printfd(__FILE__, "RADIUS::ProcessAuthPacket user '%s' not found\n", (char *)packet->login);
403 if (ui->IsInetable())
405 packet->packetType = RAD_ACCEPT_PACKET;
409 packet->packetType = RAD_REJECT_PACKET;
412 packet->packetType = RAD_ACCEPT_PACKET;
415 //-----------------------------------------------------------------------------
416 int RADIUS::ProcessPostAuthPacket(RAD_PACKET * packet)
420 if (!CanAcctService((char *)packet->service))
423 // There are no sense to check for allowed service
424 // It has allready checked at previous stage (authorization)
426 packet->packetType = RAD_ACCEPT_PACKET;
430 if (!FindUser(&ui, (char *)packet->login))
432 packet->packetType = RAD_REJECT_PACKET;
433 printfd(__FILE__, "RADIUS::ProcessPostAuthPacket user '%s' not found\n", (char *)packet->login);
437 // I think that only Framed-User services has sense to be accountable
438 // So we have to supply a Framed-IP
440 USER_IPS ips = ui->GetProperty().ips;
441 packet->packetType = RAD_ACCEPT_PACKET;
443 // Additional checking for Framed-User service
445 if (!strncmp((char *)packet->service, "Framed-User", RAD_SERVICE_LEN))
446 packet->ip = ips[0].ip;
452 //-----------------------------------------------------------------------------
453 int RADIUS::ProcessAcctStartPacket(RAD_PACKET * packet)
457 if (!FindUser(&ui, (char *)packet->login))
459 packet->packetType = RAD_REJECT_PACKET;
460 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket user '%s' not found\n", (char *)packet->login);
464 // At this point we have to unauthorize user only if it is an accountable service
466 if (CanAcctService((char *)packet->service))
468 if (sessions.find((const char *)packet->sessid) != sessions.end())
470 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket session already started!\n");
471 packet->packetType = RAD_REJECT_PACKET;
474 USER_IPS ips = ui->GetProperty().ips;
475 if (users->Authorize(ui->GetLogin(), ips[0].ip, 0xffFFffFF, this))
477 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket cannot authorize user '%s'\n", packet->login);
478 packet->packetType = RAD_REJECT_PACKET;
481 sessions[(const char *)packet->sessid].userName = (const char *)packet->login;
482 sessions[(const char *)packet->sessid].serviceType = (const char *)packet->service;
483 for_each(sessions.begin(), sessions.end(), SPrinter());
487 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket service '%s' can not be accounted\n", (char *)packet->service);
490 packet->packetType = RAD_ACCEPT_PACKET;
493 //-----------------------------------------------------------------------------
494 int RADIUS::ProcessAcctStopPacket(RAD_PACKET * packet)
496 std::map<std::string, RAD_SESSION>::iterator sid;
498 if ((sid = sessions.find((const char *)packet->sessid)) == sessions.end())
500 printfd(__FILE__, "RADIUS::ProcessAcctStopPacket session had not started yet\n");
501 packet->packetType = RAD_REJECT_PACKET;
507 if (!FindUser(&ui, sid->second.userName))
509 packet->packetType = RAD_REJECT_PACKET;
510 printfd(__FILE__, "RADIUS::ProcessPostAuthPacket user '%s' not found\n", sid->second.userName.c_str());
516 users->Unauthorize(ui->GetLogin(), this);
518 packet->packetType = RAD_ACCEPT_PACKET;
521 //-----------------------------------------------------------------------------
522 int RADIUS::ProcessAcctUpdatePacket(RAD_PACKET * packet)
524 // Fake. May be use it later
525 packet->packetType = RAD_ACCEPT_PACKET;
528 //-----------------------------------------------------------------------------
529 int RADIUS::ProcessAcctOtherPacket(RAD_PACKET * packet)
531 // Fake. May be use it later
532 packet->packetType = RAD_ACCEPT_PACKET;
535 //-----------------------------------------------------------------------------
536 void RADIUS::PrintServices(const std::list<std::string> & svcs)
538 for_each(svcs.begin(), svcs.end(), Printer());
540 //-----------------------------------------------------------------------------
541 bool RADIUS::FindUser(USER_PTR * ui, const std::string & login) const
543 if (users->FindByName(login, ui))
549 //-----------------------------------------------------------------------------
550 bool RADIUS::CanAuthService(const std::string & svc) const
552 return find(authServices.begin(), authServices.end(), svc) != authServices.end();
554 //-----------------------------------------------------------------------------
555 bool RADIUS::CanAcctService(const std::string & svc) const
557 return find(acctServices.begin(), acctServices.end(), svc) != acctServices.end();
559 //-----------------------------------------------------------------------------
560 bool RADIUS::IsAllowedService(const std::string & svc) const
562 return CanAuthService(svc) || CanAcctService(svc);
564 //-----------------------------------------------------------------------------
566 void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password)
568 unsigned char keyL[RAD_PASSWORD_LEN]; // Пароль для шифровки
569 memset(keyL, 0, RAD_PASSWORD_LEN);
570 strncpy((char *)keyL, password.c_str(), RAD_PASSWORD_LEN);
571 Blowfish_Init(ctx, keyL, RAD_PASSWORD_LEN);
573 //-----------------------------------------------------------------------------
575 void Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
577 // len8 - длина в 8-ми байтовых блоках
579 memcpy(dst, src, len8 * 8);
581 for (int i = 0; i < len8; i++)
582 Blowfish_Encrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));
584 //-----------------------------------------------------------------------------
586 void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
588 // len8 - длина в 8-ми байтовых блоках
590 memcpy(dst, src, len8 * 8);
592 for (int i = 0; i < len8; i++)
593 Blowfish_Decrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));