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 //-----------------------------------------------------------------------------
164 InitEncrypt(&ctx, "");
166 //-----------------------------------------------------------------------------
167 void RADIUS::SetUsers(USERS * u)
171 //-----------------------------------------------------------------------------
172 void RADIUS::SetStgSettings(const SETTINGS * s)
176 //-----------------------------------------------------------------------------
177 void RADIUS::SetSettings(const MODULE_SETTINGS & s)
181 //-----------------------------------------------------------------------------
182 void RADIUS::SetStore(BASE_STORE * s)
186 //-----------------------------------------------------------------------------
187 int RADIUS::ParseSettings()
189 int ret = radSettings.ParseSettings(settings);
191 errorStr = radSettings.GetStrError();
194 //-----------------------------------------------------------------------------
195 bool RADIUS::IsRunning()
199 //-----------------------------------------------------------------------------
200 const string RADIUS::GetVersion() const
202 return "RADIUS data access plugin v 0.6";
204 //-----------------------------------------------------------------------------
205 uint16_t RADIUS::GetStartPosition() const
207 // Start before any authorizers!!!
210 //-----------------------------------------------------------------------------
211 uint16_t RADIUS::GetStopPosition() const
215 //-----------------------------------------------------------------------------
216 int RADIUS::PrepareNet()
218 sock = socket(AF_INET, SOCK_DGRAM, 0);
222 errorStr = "Cannot create socket.";
223 printfd(__FILE__, "Cannot create socket\n");
227 struct sockaddr_in inAddr;
228 inAddr.sin_family = AF_INET;
229 inAddr.sin_port = htons(radSettings.GetPort());
230 inAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
232 if (bind(sock, (struct sockaddr*)&inAddr, sizeof(inAddr)) < 0)
234 errorStr = "RADIUS: Bind failed.";
235 printfd(__FILE__, "Cannot bind socket\n");
241 //-----------------------------------------------------------------------------
242 int RADIUS::FinalizeNet()
247 //-----------------------------------------------------------------------------
250 string password(radSettings.GetPassword());
252 authServices = radSettings.GetAuthServices();
253 acctServices = radSettings.GetAcctServices();
255 InitEncrypt(&ctx, password);
266 if (pthread_create(&thread, NULL, Run, this))
268 errorStr = "Cannot create thread.";
269 printfd(__FILE__, "Cannot create thread\n");
277 //-----------------------------------------------------------------------------
285 map<string, RAD_SESSION>::iterator it;
286 for (it = sessions.begin(); it != sessions.end(); ++it)
289 if (users->FindByName(it->second.userName, &ui))
291 ui->Unauthorize(this);
294 sessions.erase(sessions.begin(), sessions.end());
300 //5 seconds to thread stops itself
301 for (int i = 0; i < 25 && isRunning; i++)
306 //after 5 seconds waiting thread still running. now killing it
309 if (pthread_kill(thread, SIGINT))
311 errorStr = "Cannot kill thread.";
312 printfd(__FILE__, "Cannot kill thread\n");
315 printfd(__FILE__, "RADIUS::Stop killed Run\n");
321 //-----------------------------------------------------------------------------
322 void * RADIUS::Run(void * d)
324 RADIUS * rad = (RADIUS *)d;
327 rad->isRunning = true;
331 if (!rad->WaitPackets(rad->sock))
335 struct sockaddr_in outerAddr;
336 if (rad->RecvData(&packet, &outerAddr))
338 printfd(__FILE__, "RADIUS::Run Error on RecvData\n");
342 if (rad->ProcessData(&packet))
344 packet.packetType = RAD_REJECT_PACKET;
346 rad->Send(packet, &outerAddr);
350 rad->isRunning = false;
354 //-----------------------------------------------------------------------------
355 int RADIUS::RecvData(RAD_PACKET * packet, struct sockaddr_in * outerAddr)
357 int8_t buf[RAD_MAX_PACKET_LEN];
358 socklen_t outerAddrLen = sizeof(struct sockaddr_in);
359 int dataLen = recvfrom(sock, buf, RAD_MAX_PACKET_LEN, 0, reinterpret_cast<struct sockaddr *>(outerAddr), &outerAddrLen);
361 Decrypt(&ctx, (char *)packet, (const char *)buf, dataLen / 8);
363 if (strncmp((char *)packet->magic, RAD_ID, RAD_MAGIC_LEN))
365 printfd(__FILE__, "RADIUS::RecvData Error magic. Wanted: '%s', got: '%s'\n", RAD_ID, packet->magic);
370 //-----------------------------------------------------------------------------
371 int RADIUS::Send(const RAD_PACKET & packet, struct sockaddr_in * outerAddr)
373 size_t len = sizeof(RAD_PACKET);
376 Encrypt(&ctx, buf, (char *)&packet, len / 8);
377 return sendto(sock, buf, len, 0, reinterpret_cast<struct sockaddr *>(outerAddr), sizeof(struct sockaddr_in));
379 //-----------------------------------------------------------------------------
380 int RADIUS::ProcessData(RAD_PACKET * packet)
382 if (strncmp((const char *)packet->protoVer, "01", 2))
384 printfd(__FILE__, "RADIUS::ProcessData packet.protoVer incorrect\n");
387 switch (packet->packetType)
389 case RAD_AUTZ_PACKET:
390 return ProcessAutzPacket(packet);
391 case RAD_AUTH_PACKET:
392 return ProcessAuthPacket(packet);
393 case RAD_POST_AUTH_PACKET:
394 return ProcessPostAuthPacket(packet);
395 case RAD_ACCT_START_PACKET:
396 return ProcessAcctStartPacket(packet);
397 case RAD_ACCT_STOP_PACKET:
398 return ProcessAcctStopPacket(packet);
399 case RAD_ACCT_UPDATE_PACKET:
400 return ProcessAcctUpdatePacket(packet);
401 case RAD_ACCT_OTHER_PACKET:
402 return ProcessAcctOtherPacket(packet);
404 printfd(__FILE__, "RADIUS::ProcessData Unsupported packet type: %d\n", packet->packetType);
409 //-----------------------------------------------------------------------------
410 int RADIUS::ProcessAutzPacket(RAD_PACKET * packet)
414 if (!IsAllowedService((char *)packet->service))
416 printfd(__FILE__, "RADIUS::ProcessAutzPacket service '%s' is not allowed to authorize\n", packet->service);
417 packet->packetType = RAD_REJECT_PACKET;
421 if (store->RestoreUserConf(&conf, (char *)packet->login))
423 packet->packetType = RAD_REJECT_PACKET;
424 printfd(__FILE__, "RADIUS::ProcessAutzPacket cannot restore conf for user '%s'\n", packet->login);
428 // At this point service can be authorized at least
429 // So we send a plain-text password
431 packet->packetType = RAD_ACCEPT_PACKET;
432 strncpy((char *)packet->password, conf.password.c_str(), RAD_PASSWORD_LEN);
436 //-----------------------------------------------------------------------------
437 int RADIUS::ProcessAuthPacket(RAD_PACKET * packet)
441 if (!CanAcctService((char *)packet->service))
444 // There are no sense to check for allowed service
445 // It has allready checked at previous stage (authorization)
447 printfd(__FILE__, "RADIUS::ProcessAuthPacket service '%s' neednot stargazer authentication\n", (char *)packet->service);
448 packet->packetType = RAD_ACCEPT_PACKET;
452 // At this point we have an accountable service
453 // All other services got a password if allowed or rejected
455 if (!FindUser(&ui, (char *)packet->login))
457 packet->packetType = RAD_REJECT_PACKET;
458 printfd(__FILE__, "RADIUS::ProcessAuthPacket user '%s' not found\n", (char *)packet->login);
462 if (ui->IsInetable())
464 packet->packetType = RAD_ACCEPT_PACKET;
468 packet->packetType = RAD_REJECT_PACKET;
471 packet->packetType = RAD_ACCEPT_PACKET;
474 //-----------------------------------------------------------------------------
475 int RADIUS::ProcessPostAuthPacket(RAD_PACKET * packet)
479 if (!CanAcctService((char *)packet->service))
482 // There are no sense to check for allowed service
483 // It has allready checked at previous stage (authorization)
485 packet->packetType = RAD_ACCEPT_PACKET;
489 if (!FindUser(&ui, (char *)packet->login))
491 packet->packetType = RAD_REJECT_PACKET;
492 printfd(__FILE__, "RADIUS::ProcessPostAuthPacket user '%s' not found\n", (char *)packet->login);
496 // I think that only Framed-User services has sense to be accountable
497 // So we have to supply a Framed-IP
499 USER_IPS ips = ui->property.ips;
500 packet->packetType = RAD_ACCEPT_PACKET;
502 // Additional checking for Framed-User service
504 if (!strncmp((char *)packet->service, "Framed-User", RAD_SERVICE_LEN))
505 packet->ip = ips[0].ip;
511 //-----------------------------------------------------------------------------
512 int RADIUS::ProcessAcctStartPacket(RAD_PACKET * packet)
516 if (!FindUser(&ui, (char *)packet->login))
518 packet->packetType = RAD_REJECT_PACKET;
519 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket user '%s' not found\n", (char *)packet->login);
523 // At this point we have to unauthorize user only if it is an accountable service
525 if (CanAcctService((char *)packet->service))
527 if (sessions.find((const char *)packet->sessid) != sessions.end())
529 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket session already started!\n");
530 packet->packetType = RAD_REJECT_PACKET;
533 USER_IPS ips = ui->property.ips;
534 if (ui->Authorize(ips[0].ip, "", 0xffFFffFF, this))
536 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket cannot authorize user '%s'\n", packet->login);
537 packet->packetType = RAD_REJECT_PACKET;
540 sessions[(const char *)packet->sessid].userName = (const char *)packet->login;
541 sessions[(const char *)packet->sessid].serviceType = (const char *)packet->service;
542 for_each(sessions.begin(), sessions.end(), SPrinter());
546 printfd(__FILE__, "RADIUS::ProcessAcctStartPacket service '%s' can not be accounted\n", (char *)packet->service);
549 packet->packetType = RAD_ACCEPT_PACKET;
552 //-----------------------------------------------------------------------------
553 int RADIUS::ProcessAcctStopPacket(RAD_PACKET * packet)
555 map<string, RAD_SESSION>::iterator sid;
557 if ((sid = sessions.find((const char *)packet->sessid)) == sessions.end())
559 printfd(__FILE__, "RADIUS::ProcessAcctStopPacket session had not started yet\n");
560 packet->packetType = RAD_REJECT_PACKET;
566 if (!FindUser(&ui, sid->second.userName))
568 packet->packetType = RAD_REJECT_PACKET;
569 printfd(__FILE__, "RADIUS::ProcessPostAuthPacket user '%s' not found\n", sid->second.userName.c_str());
575 ui->Unauthorize(this);
577 packet->packetType = RAD_ACCEPT_PACKET;
580 //-----------------------------------------------------------------------------
581 int RADIUS::ProcessAcctUpdatePacket(RAD_PACKET * packet)
583 // Fake. May be use it later
584 packet->packetType = RAD_ACCEPT_PACKET;
587 //-----------------------------------------------------------------------------
588 int RADIUS::ProcessAcctOtherPacket(RAD_PACKET * packet)
590 // Fake. May be use it later
591 packet->packetType = RAD_ACCEPT_PACKET;
594 //-----------------------------------------------------------------------------
595 void RADIUS::PrintServices(const list<string> & svcs)
597 for_each(svcs.begin(), svcs.end(), Printer());
599 //-----------------------------------------------------------------------------
600 bool RADIUS::FindUser(user_iter * ui, const std::string & login) const
602 if (users->FindByName(login, ui))
608 //-----------------------------------------------------------------------------
609 bool RADIUS::CanAuthService(const std::string & svc) const
611 return find(authServices.begin(), authServices.end(), svc) != authServices.end();
613 //-----------------------------------------------------------------------------
614 bool RADIUS::CanAcctService(const std::string & svc) const
616 return find(acctServices.begin(), acctServices.end(), svc) != acctServices.end();
618 //-----------------------------------------------------------------------------
619 bool RADIUS::IsAllowedService(const std::string & svc) const
621 return CanAuthService(svc) || CanAcctService(svc);
623 //-----------------------------------------------------------------------------
624 bool RADIUS::WaitPackets(int sd) const
634 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
635 if (res == -1) // Error
639 printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
644 if (res == 0) // Timeout
651 //-----------------------------------------------------------------------------
653 void InitEncrypt(BLOWFISH_CTX * ctx, const string & password)
655 unsigned char keyL[RAD_PASSWORD_LEN]; // Пароль для шифровки
656 memset(keyL, 0, RAD_PASSWORD_LEN);
657 strncpy((char *)keyL, password.c_str(), RAD_PASSWORD_LEN);
658 Blowfish_Init(ctx, keyL, RAD_PASSWORD_LEN);
660 //-----------------------------------------------------------------------------
662 void Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
664 // len8 - длина в 8-ми байтовых блоках
666 memcpy(dst, src, len8 * 8);
668 for (int i = 0; i < len8; i++)
669 Blowfish_Encrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));
671 //-----------------------------------------------------------------------------
673 void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
675 // len8 - длина в 8-ми байтовых блоках
677 memcpy(dst, src, len8 * 8);
679 for (int i = 0; i < len8; i++)
680 Blowfish_Decrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));