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);
249 //-----------------------------------------------------------------------------
250 void * RADIUS::Run(void * d)
253 sigfillset(&signalSet);
254 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
256 RADIUS * rad = (RADIUS *)d;
259 rad->isRunning = true;
263 if (!WaitPackets(rad->sock))
267 struct sockaddr_in outerAddr;
268 if (rad->RecvData(&packet, &outerAddr))
270 printfd(__FILE__, "RADIUS::Run Error on RecvData\n");
274 if (rad->ProcessData(&packet))
276 packet.packetType = RAD_REJECT_PACKET;
278 rad->Send(packet, &outerAddr);
282 rad->isRunning = false;
286 //-----------------------------------------------------------------------------
287 int RADIUS::RecvData(RAD_PACKET * packet, struct sockaddr_in * outerAddr)
289 int8_t buf[RAD_MAX_PACKET_LEN];
290 socklen_t outerAddrLen = sizeof(struct sockaddr_in);
291 int dataLen = recvfrom(sock, buf, RAD_MAX_PACKET_LEN, 0, reinterpret_cast<struct sockaddr *>(outerAddr), &outerAddrLen);
293 Decrypt(&ctx, (char *)packet, (const char *)buf, dataLen / 8);
295 if (strncmp((char *)packet->magic, RAD_ID, RAD_MAGIC_LEN))
297 printfd(__FILE__, "RADIUS::RecvData Error magic. Wanted: '%s', got: '%s'\n", RAD_ID, packet->magic);
302 //-----------------------------------------------------------------------------
303 int RADIUS::Send(const RAD_PACKET & packet, struct sockaddr_in * outerAddr)
305 size_t len = sizeof(RAD_PACKET);
308 Encrypt(&ctx, buf, (char *)&packet, len / 8);
309 return sendto(sock, buf, len, 0, reinterpret_cast<struct sockaddr *>(outerAddr), sizeof(struct sockaddr_in));
311 //-----------------------------------------------------------------------------
312 int RADIUS::ProcessData(RAD_PACKET * packet)
314 if (strncmp((const char *)packet->protoVer, "01", 2))
316 printfd(__FILE__, "RADIUS::ProcessData packet.protoVer incorrect\n");
319 switch (packet->packetType)
321 case RAD_AUTZ_PACKET:
322 return ProcessAutzPacket(packet);
323 case RAD_AUTH_PACKET:
324 return ProcessAuthPacket(packet);
325 case RAD_POST_AUTH_PACKET:
326 return ProcessPostAuthPacket(packet);
327 case RAD_ACCT_START_PACKET:
328 return ProcessAcctStartPacket(packet);
329 case RAD_ACCT_STOP_PACKET:
330 return ProcessAcctStopPacket(packet);
331 case RAD_ACCT_UPDATE_PACKET:
332 return ProcessAcctUpdatePacket(packet);
333 case RAD_ACCT_OTHER_PACKET:
334 return ProcessAcctOtherPacket(packet);
336 printfd(__FILE__, "RADIUS::ProcessData Unsupported packet type: %d\n", packet->packetType);
341 //-----------------------------------------------------------------------------
342 int RADIUS::ProcessAutzPacket(RAD_PACKET * packet)
346 if (!IsAllowedService((char *)packet->service))
348 printfd(__FILE__, "RADIUS::ProcessAutzPacket service '%s' is not allowed to authorize\n", packet->service);
349 packet->packetType = RAD_REJECT_PACKET;
353 if (store->RestoreUserConf(&conf, (char *)packet->login))
355 packet->packetType = RAD_REJECT_PACKET;
356 printfd(__FILE__, "RADIUS::ProcessAutzPacket cannot restore conf for user '%s'\n", packet->login);
360 // At this point service can be authorized at least
361 // So we send a plain-text password
363 packet->packetType = RAD_ACCEPT_PACKET;
364 strncpy((char *)packet->password, conf.password.c_str(), RAD_PASSWORD_LEN);
368 //-----------------------------------------------------------------------------
369 int RADIUS::ProcessAuthPacket(RAD_PACKET * packet)
373 if (!CanAcctService((char *)packet->service))
376 // There are no sense to check for allowed service
377 // It has allready checked at previous stage (authorization)
379 printfd(__FILE__, "RADIUS::ProcessAuthPacket service '%s' neednot stargazer authentication\n", (char *)packet->service);
380 packet->packetType = RAD_ACCEPT_PACKET;
384 // At this point we have an accountable service
385 // All other services got a password if allowed or rejected
387 if (!FindUser(&ui, (char *)packet->login))
389 packet->packetType = RAD_REJECT_PACKET;
390 printfd(__FILE__, "RADIUS::ProcessAuthPacket user '%s' not found\n", (char *)packet->login);
394 if (ui->IsInetable())
396 packet->packetType = RAD_ACCEPT_PACKET;
400 packet->packetType = RAD_REJECT_PACKET;
403 packet->packetType = RAD_ACCEPT_PACKET;
406 //-----------------------------------------------------------------------------
407 int RADIUS::ProcessPostAuthPacket(RAD_PACKET * packet)
411 if (!CanAcctService((char *)packet->service))
414 // There are no sense to check for allowed service
415 // It has allready checked at previous stage (authorization)
417 packet->packetType = RAD_ACCEPT_PACKET;
421 if (!FindUser(&ui, (char *)packet->login))
423 packet->packetType = RAD_REJECT_PACKET;
424 printfd(__FILE__, "RADIUS::ProcessPostAuthPacket user '%s' not found\n", (char *)packet->login);
428 // I think that only Framed-User services has sense to be accountable
429 // So we have to supply a Framed-IP
431 USER_IPS ips = ui->GetProperty().ips;
432 packet->packetType = RAD_ACCEPT_PACKET;
434 // Additional checking for Framed-User service
436 if (!strncmp((char *)packet->service, "Framed-User", RAD_SERVICE_LEN))
437 packet->ip = ips[0].ip;
443 //-----------------------------------------------------------------------------
444 int RADIUS::ProcessAcctStartPacket(RAD_PACKET * packet)
448 if (!FindUser(&ui, (char *)packet->login))
450 packet->packetType = RAD_REJECT_PACKET;
451 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket user '%s' not found\n", (char *)packet->login);
455 // At this point we have to unauthorize user only if it is an accountable service
457 if (CanAcctService((char *)packet->service))
459 if (sessions.find((const char *)packet->sessid) != sessions.end())
461 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket session already started!\n");
462 packet->packetType = RAD_REJECT_PACKET;
465 USER_IPS ips = ui->GetProperty().ips;
466 if (users->Authorize(ui->GetLogin(), ips[0].ip, 0xffFFffFF, this))
468 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket cannot authorize user '%s'\n", packet->login);
469 packet->packetType = RAD_REJECT_PACKET;
472 sessions[(const char *)packet->sessid].userName = (const char *)packet->login;
473 sessions[(const char *)packet->sessid].serviceType = (const char *)packet->service;
474 for_each(sessions.begin(), sessions.end(), SPrinter());
478 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket service '%s' can not be accounted\n", (char *)packet->service);
481 packet->packetType = RAD_ACCEPT_PACKET;
484 //-----------------------------------------------------------------------------
485 int RADIUS::ProcessAcctStopPacket(RAD_PACKET * packet)
487 std::map<std::string, RAD_SESSION>::iterator sid;
489 if ((sid = sessions.find((const char *)packet->sessid)) == sessions.end())
491 printfd(__FILE__, "RADIUS::ProcessAcctStopPacket session had not started yet\n");
492 packet->packetType = RAD_REJECT_PACKET;
498 if (!FindUser(&ui, sid->second.userName))
500 packet->packetType = RAD_REJECT_PACKET;
501 printfd(__FILE__, "RADIUS::ProcessPostAuthPacket user '%s' not found\n", sid->second.userName.c_str());
507 users->Unauthorize(ui->GetLogin(), this);
509 packet->packetType = RAD_ACCEPT_PACKET;
512 //-----------------------------------------------------------------------------
513 int RADIUS::ProcessAcctUpdatePacket(RAD_PACKET * packet)
515 // Fake. May be use it later
516 packet->packetType = RAD_ACCEPT_PACKET;
519 //-----------------------------------------------------------------------------
520 int RADIUS::ProcessAcctOtherPacket(RAD_PACKET * packet)
522 // Fake. May be use it later
523 packet->packetType = RAD_ACCEPT_PACKET;
526 //-----------------------------------------------------------------------------
527 void RADIUS::PrintServices(const std::list<std::string> & svcs)
529 for_each(svcs.begin(), svcs.end(), Printer());
531 //-----------------------------------------------------------------------------
532 bool RADIUS::FindUser(USER_PTR * ui, const std::string & login) const
534 if (users->FindByName(login, ui))
540 //-----------------------------------------------------------------------------
541 bool RADIUS::CanAuthService(const std::string & svc) const
543 return find(authServices.begin(), authServices.end(), svc) != authServices.end();
545 //-----------------------------------------------------------------------------
546 bool RADIUS::CanAcctService(const std::string & svc) const
548 return find(acctServices.begin(), acctServices.end(), svc) != acctServices.end();
550 //-----------------------------------------------------------------------------
551 bool RADIUS::IsAllowedService(const std::string & svc) const
553 return CanAuthService(svc) || CanAcctService(svc);
555 //-----------------------------------------------------------------------------
557 void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password)
559 unsigned char keyL[RAD_PASSWORD_LEN]; // Пароль для шифровки
560 memset(keyL, 0, RAD_PASSWORD_LEN);
561 strncpy((char *)keyL, password.c_str(), RAD_PASSWORD_LEN);
562 Blowfish_Init(ctx, keyL, RAD_PASSWORD_LEN);
564 //-----------------------------------------------------------------------------
566 void Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
568 // len8 - длина в 8-ми байтовых блоках
570 memcpy(dst, src, len8 * 8);
572 for (int i = 0; i < len8; i++)
573 Blowfish_Encrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));
575 //-----------------------------------------------------------------------------
577 void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
579 // len8 - длина в 8-ми байтовых блоках
581 memcpy(dst, src, len8 * 8);
583 for (int i = 0; i < len8; i++)
584 Blowfish_Decrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));