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 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
67 BASE_PLUGIN * GetPlugin()
69 return radc.GetPlugin();
71 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
74 int RAD_SETTINGS::ParseIntInRange(const string & str, int min, int max, int * val)
76 if (str2x(str.c_str(), *val))
78 errorStr = "Incorrect value \'" + str + "\'.";
81 if (*val < min || *val > max)
83 errorStr = "Value \'" + str + "\' out of range.";
88 //-----------------------------------------------------------------------------
89 int RAD_SETTINGS::ParseServices(const vector<string> & str, list<string> * lst)
91 copy(str.begin(), str.end(), back_inserter(*lst));
92 list<string>::iterator it(find(lst->begin(),
100 //-----------------------------------------------------------------------------
101 int RAD_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
105 vector<PARAM_VALUE>::const_iterator pvi;
106 ///////////////////////////
108 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
109 if (pvi == s.moduleParams.end())
111 errorStr = "Parameter \'Port\' not found.";
112 printfd(__FILE__, "Parameter 'Port' not found\n");
115 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
117 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
118 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
122 ///////////////////////////
123 pv.param = "Password";
124 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
125 if (pvi == s.moduleParams.end())
127 errorStr = "Parameter \'Password\' not found.";
128 printfd(__FILE__, "Parameter 'Password' not found\n");
131 password = pvi->value[0];
132 ///////////////////////////
133 pv.param = "AuthServices";
134 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
135 if (pvi != s.moduleParams.end())
137 ParseServices(pvi->value, &authServices);
139 ///////////////////////////
140 pv.param = "AcctServices";
141 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
142 if (pvi != s.moduleParams.end())
144 ParseServices(pvi->value, &acctServices);
149 //-----------------------------------------------------------------------------
150 //-----------------------------------------------------------------------------
151 //-----------------------------------------------------------------------------
161 //-----------------------------------------------------------------------------
162 void RADIUS::SetUsers(USERS * u)
166 //-----------------------------------------------------------------------------
167 void RADIUS::SetStgSettings(const SETTINGS * s)
171 //-----------------------------------------------------------------------------
172 void RADIUS::SetSettings(const MODULE_SETTINGS & s)
176 //-----------------------------------------------------------------------------
177 void RADIUS::SetStore(BASE_STORE * s)
181 //-----------------------------------------------------------------------------
182 int RADIUS::ParseSettings()
184 int ret = radSettings.ParseSettings(settings);
186 errorStr = radSettings.GetStrError();
189 //-----------------------------------------------------------------------------
190 bool RADIUS::IsRunning()
194 //-----------------------------------------------------------------------------
195 const string RADIUS::GetVersion() const
197 return "RADIUS data access plugin v 0.6";
199 //-----------------------------------------------------------------------------
200 uint16_t RADIUS::GetStartPosition() const
202 // Start before any authorizers!!!
205 //-----------------------------------------------------------------------------
206 uint16_t RADIUS::GetStopPosition() const
210 //-----------------------------------------------------------------------------
211 void RADIUS::SetUserNotifier(user_iter)
214 //-----------------------------------------------------------------------------
215 void RADIUS::UnSetUserNotifier(user_iter)
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 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 map<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->property.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->property.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 map<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::InitEncrypt(BLOWFISH_CTX * ctx, const string & password)
600 unsigned char keyL[RAD_PASSWORD_LEN]; // Пароль для шифровки
601 memset(keyL, 0, RAD_PASSWORD_LEN);
602 strncpy((char *)keyL, password.c_str(), RAD_PASSWORD_LEN);
603 Blowfish_Init(ctx, keyL, RAD_PASSWORD_LEN);
605 //-----------------------------------------------------------------------------
606 void RADIUS::Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
608 // len8 - длина в 8-ми байтовых блоках
610 memcpy(dst, src, len8 * 8);
612 for (int i = 0; i < len8; i++)
613 Blowfish_Encrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));
615 //-----------------------------------------------------------------------------
616 void RADIUS::Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
618 // len8 - длина в 8-ми байтовых блоках
620 memcpy(dst, src, len8 * 8);
622 for (int i = 0; i < len8; i++)
623 Blowfish_Decrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));
625 //-----------------------------------------------------------------------------
626 void RADIUS::PrintServices(const list<string> & svcs)
628 for_each(svcs.begin(), svcs.end(), Printer());
630 //-----------------------------------------------------------------------------
631 bool RADIUS::FindUser(user_iter * ui, const std::string & login) const
633 if (users->FindByName(login, ui))
639 //-----------------------------------------------------------------------------
640 bool RADIUS::CanAuthService(const std::string & svc) const
642 return find(authServices.begin(), authServices.end(), svc) != authServices.end();
644 //-----------------------------------------------------------------------------
645 bool RADIUS::CanAcctService(const std::string & svc) const
647 return find(acctServices.begin(), acctServices.end(), svc) != acctServices.end();
649 //-----------------------------------------------------------------------------
650 bool RADIUS::IsAllowedService(const std::string & svc) const
652 return CanAuthService(svc) || CanAcctService(svc);
654 //-----------------------------------------------------------------------------
655 bool RADIUS::WaitPackets(int sd) const
665 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
666 if (res == -1) // Error
670 printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
675 if (res == 0) // Timeout