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 * Realization of data access via Stargazer for RADIUS
25 * $Date: 2010/04/16 12:30:02 $
30 #include <sys/types.h>
31 #include <unistd.h> // close
34 #include "stg_client.h"
38 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
41 STG_CLIENT::STG_CLIENT()
47 //-----------------------------------------------------------------------------
48 STG_CLIENT::~STG_CLIENT()
51 //-----------------------------------------------------------------------------
52 void STG_CLIENT::SetServer(const string & host)
54 STG_CLIENT::host = host;
56 //-----------------------------------------------------------------------------
57 void STG_CLIENT::SetPort(uint16_t port)
59 STG_CLIENT::port = port;
61 //-----------------------------------------------------------------------------
62 void STG_CLIENT::SetLocalPort(uint16_t port)
64 STG_CLIENT::localPort = port;
66 //-----------------------------------------------------------------------------
67 void STG_CLIENT::SetPassword(const string & password)
69 STG_CLIENT::password = password;
71 //-----------------------------------------------------------------------------
72 uint32_t STG_CLIENT::GetFramedIP() const
76 //-----------------------------------------------------------------------------
77 void STG_CLIENT::InitEncrypt()
79 unsigned char keyL[RAD_PASSWORD_LEN];
80 memset(keyL, 0, RAD_PASSWORD_LEN);
81 strncpy((char *)keyL, password.c_str(), RAD_PASSWORD_LEN);
82 Blowfish_Init(&ctx, keyL, RAD_PASSWORD_LEN);
84 //-----------------------------------------------------------------------------
85 int STG_CLIENT::PrepareNet()
87 sock = socket(AF_INET, SOCK_DGRAM, 0);
90 errorStr = "Socket create error";
94 struct hostent * he = NULL;
95 he = gethostbyname(host.c_str());
98 errorStr = "gethostbyname error";
104 struct sockaddr_in localAddr;
105 localAddr.sin_family = AF_INET;
106 localAddr.sin_port = htons(localPort);
107 localAddr.sin_addr.s_addr = inet_addr("0.0.0.0");;
109 if (bind(sock, (struct sockaddr *)&localAddr, sizeof(localAddr)))
111 errorStr = "Bind failed";
116 outerAddr.sin_family = AF_INET;
117 outerAddr.sin_port = htons(port);
118 outerAddr.sin_addr.s_addr = *(uint32_t *)he->h_addr;
120 outerAddrLen = sizeof(struct sockaddr_in);
124 //-----------------------------------------------------------------------------
125 void STG_CLIENT::FinalizeNet()
129 //-----------------------------------------------------------------------------
130 int STG_CLIENT::Start()
136 //-----------------------------------------------------------------------------
137 int STG_CLIENT::Stop()
143 //-----------------------------------------------------------------------------
144 string STG_CLIENT::GetUserPassword() const
148 //-----------------------------------------------------------------------------
149 int STG_CLIENT::Send(const RAD_PACKET & packet)
151 char buf[RAD_MAX_PACKET_LEN];
153 Encrypt(buf, (char *)&packet, sizeof(RAD_PACKET) / 8);
155 int res = sendto(sock, buf, sizeof(RAD_PACKET), 0, (struct sockaddr *)&outerAddr, outerAddrLen);
158 errorStr = "Error sending data";
162 //-----------------------------------------------------------------------------
163 int STG_CLIENT::RecvData(RAD_PACKET * packet)
165 char buf[RAD_MAX_PACKET_LEN];
168 outerAddrLen = sizeof(struct sockaddr_in);
170 res = recvfrom(sock, buf, RAD_MAX_PACKET_LEN, 0, (struct sockaddr *)&outerAddr, &outerAddrLen);
173 errorStr = "Error receiving data";
177 Decrypt((char *)packet, buf, res / 8);
181 //-----------------------------------------------------------------------------
182 int STG_CLIENT::Request(RAD_PACKET * packet, const std::string & login, const std::string & svc, uint8_t packetType)
186 memcpy((void *)&packet->magic, (void *)RAD_ID, RAD_MAGIC_LEN);
187 packet->protoVer[0] = '0';
188 packet->protoVer[1] = '1';
189 packet->packetType = packetType;
191 strncpy((char *)packet->login, login.c_str(), RAD_LOGIN_LEN);
192 strncpy((char *)packet->service, svc.c_str(), RAD_SERVICE_LEN);
198 res = RecvData(packet);
202 if (strncmp((char *)packet->magic, RAD_ID, RAD_MAGIC_LEN))
204 errorStr = "Magic invalid. Wanted: '";
206 errorStr += "', got: '";
207 errorStr += (char *)packet->magic;
214 //-----------------------------------------------------------------------------
215 int STG_CLIENT::Authorize(const string & login, const string & svc)
221 if (Request(&packet, login, svc, RAD_AUTZ_PACKET))
224 if (packet.packetType != RAD_ACCEPT_PACKET)
227 userPassword = (char *)packet.password;
231 //-----------------------------------------------------------------------------
232 int STG_CLIENT::Authenticate(const string & login, const string & svc)
238 if (Request(&packet, login, svc, RAD_AUTH_PACKET))
241 if (packet.packetType != RAD_ACCEPT_PACKET)
246 //-----------------------------------------------------------------------------
247 int STG_CLIENT::PostAuthenticate(const string & login, const string & svc)
253 if (Request(&packet, login, svc, RAD_POST_AUTH_PACKET))
256 if (packet.packetType != RAD_ACCEPT_PACKET)
259 if (svc == "Framed-User")
260 framedIP = packet.ip;
266 //-----------------------------------------------------------------------------
267 int STG_CLIENT::Account(const std::string & type, const string & login, const string & svc, const string & sessid)
272 strncpy((char *)packet.sessid, sessid.c_str(), RAD_SESSID_LEN);
276 if (Request(&packet, login, svc, RAD_ACCT_START_PACKET))
279 else if (type == "Stop")
281 if (Request(&packet, login, svc, RAD_ACCT_STOP_PACKET))
284 else if (type == "Interim-Update")
286 if (Request(&packet, login, svc, RAD_ACCT_UPDATE_PACKET))
291 if (Request(&packet, login, svc, RAD_ACCT_OTHER_PACKET))
295 if (packet.packetType != RAD_ACCEPT_PACKET)
300 //-----------------------------------------------------------------------------
301 void STG_CLIENT::Encrypt(char * dst, const char * src, int len8)
303 // len8 - длина в 8-ми байтовых блоках
305 memcpy(dst, src, len8 * 8);
307 for (int i = 0; i < len8; i++)
308 Blowfish_Encrypt(&ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));
310 //-----------------------------------------------------------------------------
311 void STG_CLIENT::Decrypt(char * dst, const char * src, int len8)
313 // len8 - длина в 8-ми байтовых блоках
315 memcpy(dst, src, len8 * 8);
317 for (int i = 0; i < len8; i++)
318 Blowfish_Decrypt(&ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));
320 //-----------------------------------------------------------------------------