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
38 #include "stg_client.h"
42 void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password);
43 void Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8);
44 void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8);
46 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
49 STG_CLIENT::STG_CLIENT(const std::string & host, uint16_t port, uint16_t lp, const std::string & pass)
54 sock = socket(AF_INET, SOCK_DGRAM, 0);
57 std::string message = strerror(errno);
58 message = "Socket create error: '" + message + "'";
59 throw std::runtime_error(message);
62 struct hostent * he = NULL;
63 he = gethostbyname(host.c_str());
66 throw std::runtime_error("gethostbyname error");
69 outerAddr.sin_family = AF_INET;
70 outerAddr.sin_port = htons(port);
71 outerAddr.sin_addr.s_addr = *(uint32_t *)he->h_addr;
73 InitEncrypt(&ctx, password);
77 //-----------------------------------------------------------------------------
78 STG_CLIENT::~STG_CLIENT()
82 //-----------------------------------------------------------------------------
83 uint32_t STG_CLIENT::GetFramedIP() const
87 //-----------------------------------------------------------------------------
88 int STG_CLIENT::PrepareNet()
92 struct sockaddr_in localAddr;
93 localAddr.sin_family = AF_INET;
94 localAddr.sin_port = htons(localPort);
95 localAddr.sin_addr.s_addr = inet_addr("0.0.0.0");;
97 if (bind(sock, (struct sockaddr *)&localAddr, sizeof(localAddr)))
99 errorStr = "Bind failed";
105 //-----------------------------------------------------------------------------
106 string STG_CLIENT::GetUserPassword() const
110 //-----------------------------------------------------------------------------
111 int STG_CLIENT::Send(const RAD_PACKET & packet)
113 char buf[RAD_MAX_PACKET_LEN];
115 Encrypt(&ctx, buf, (char *)&packet, sizeof(RAD_PACKET) / 8);
117 int res = sendto(sock, buf, sizeof(RAD_PACKET), 0, (struct sockaddr *)&outerAddr, sizeof(outerAddr));
120 errorStr = "Error sending data";
124 //-----------------------------------------------------------------------------
125 int STG_CLIENT::RecvData(RAD_PACKET * packet)
127 char buf[RAD_MAX_PACKET_LEN];
130 struct sockaddr_in addr;
131 socklen_t len = sizeof(struct sockaddr_in);
133 res = recvfrom(sock, buf, RAD_MAX_PACKET_LEN, 0, reinterpret_cast<struct sockaddr *>(&addr), &len);
136 errorStr = "Error receiving data";
140 Decrypt(&ctx, (char *)packet, buf, res / 8);
144 //-----------------------------------------------------------------------------
145 int STG_CLIENT::Request(RAD_PACKET * packet, const std::string & login, const std::string & svc, uint8_t packetType)
149 memcpy((void *)&packet->magic, (void *)RAD_ID, RAD_MAGIC_LEN);
150 packet->protoVer[0] = '0';
151 packet->protoVer[1] = '1';
152 packet->packetType = packetType;
154 strncpy((char *)packet->login, login.c_str(), RAD_LOGIN_LEN);
155 strncpy((char *)packet->service, svc.c_str(), RAD_SERVICE_LEN);
161 res = RecvData(packet);
165 if (strncmp((char *)packet->magic, RAD_ID, RAD_MAGIC_LEN))
167 errorStr = "Magic invalid. Wanted: '";
169 errorStr += "', got: '";
170 errorStr += (char *)packet->magic;
177 //-----------------------------------------------------------------------------
178 int STG_CLIENT::Authorize(const string & login, const string & svc)
184 if (Request(&packet, login, svc, RAD_AUTZ_PACKET))
187 if (packet.packetType != RAD_ACCEPT_PACKET)
190 userPassword = (char *)packet.password;
194 //-----------------------------------------------------------------------------
195 int STG_CLIENT::Authenticate(const string & login, const string & svc)
201 if (Request(&packet, login, svc, RAD_AUTH_PACKET))
204 if (packet.packetType != RAD_ACCEPT_PACKET)
209 //-----------------------------------------------------------------------------
210 int STG_CLIENT::PostAuthenticate(const string & login, const string & svc)
216 if (Request(&packet, login, svc, RAD_POST_AUTH_PACKET))
219 if (packet.packetType != RAD_ACCEPT_PACKET)
222 if (svc == "Framed-User")
223 framedIP = packet.ip;
229 //-----------------------------------------------------------------------------
230 int STG_CLIENT::Account(const std::string & type, const string & login, const string & svc, const string & sessid)
235 strncpy((char *)packet.sessid, sessid.c_str(), RAD_SESSID_LEN);
239 if (Request(&packet, login, svc, RAD_ACCT_START_PACKET))
242 else if (type == "Stop")
244 if (Request(&packet, login, svc, RAD_ACCT_STOP_PACKET))
247 else if (type == "Interim-Update")
249 if (Request(&packet, login, svc, RAD_ACCT_UPDATE_PACKET))
254 if (Request(&packet, login, svc, RAD_ACCT_OTHER_PACKET))
258 if (packet.packetType != RAD_ACCEPT_PACKET)
263 //-----------------------------------------------------------------------------
265 void Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
267 // len8 - длина в 8-ми байтовых блоках
269 memcpy(dst, src, len8 * 8);
271 for (int i = 0; i < len8; i++)
272 Blowfish_Encrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));
274 //-----------------------------------------------------------------------------
276 void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
278 // len8 - длина в 8-ми байтовых блоках
280 memcpy(dst, src, len8 * 8);
282 for (int i = 0; i < len8; i++)
283 Blowfish_Decrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));
285 //-----------------------------------------------------------------------------
287 void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password)
289 unsigned char keyL[RAD_PASSWORD_LEN];
290 memset(keyL, 0, RAD_PASSWORD_LEN);
291 strncpy((char *)keyL, password.c_str(), RAD_PASSWORD_LEN);
292 Blowfish_Init(ctx, keyL, RAD_PASSWORD_LEN);
294 //-----------------------------------------------------------------------------