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 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
45 STG_CLIENT::STG_CLIENT(const std::string & host, uint16_t port, uint16_t lp, const std::string & pass)
50 sock = socket(AF_INET, SOCK_DGRAM, 0);
53 std::string message = strerror(errno);
54 message = "Socket create error: '" + message + "'";
55 throw std::runtime_error(message);
58 struct hostent * he = NULL;
59 he = gethostbyname(host.c_str());
62 throw std::runtime_error("gethostbyname error");
65 outerAddr.sin_family = AF_INET;
66 outerAddr.sin_port = htons(port);
67 outerAddr.sin_addr.s_addr = *(uint32_t *)he->h_addr;
71 //-----------------------------------------------------------------------------
72 STG_CLIENT::~STG_CLIENT()
76 //-----------------------------------------------------------------------------
77 uint32_t STG_CLIENT::GetFramedIP() const
81 //-----------------------------------------------------------------------------
83 void STG_CLIENT::InitEncrypt()
85 unsigned char keyL[RAD_PASSWORD_LEN];
86 memset(keyL, 0, RAD_PASSWORD_LEN);
87 strncpy((char *)keyL, password.c_str(), RAD_PASSWORD_LEN);
88 Blowfish_Init(&ctx, keyL, RAD_PASSWORD_LEN);
90 //-----------------------------------------------------------------------------
91 int STG_CLIENT::PrepareNet()
95 struct sockaddr_in localAddr;
96 localAddr.sin_family = AF_INET;
97 localAddr.sin_port = htons(localPort);
98 localAddr.sin_addr.s_addr = inet_addr("0.0.0.0");;
100 if (bind(sock, (struct sockaddr *)&localAddr, sizeof(localAddr)))
102 errorStr = "Bind failed";
108 //-----------------------------------------------------------------------------
109 int STG_CLIENT::Start()
113 //-----------------------------------------------------------------------------
114 int STG_CLIENT::Stop()
118 //-----------------------------------------------------------------------------
119 string STG_CLIENT::GetUserPassword() const
123 //-----------------------------------------------------------------------------
124 int STG_CLIENT::Send(const RAD_PACKET & packet)
126 char buf[RAD_MAX_PACKET_LEN];
128 Encrypt(buf, (char *)&packet, sizeof(RAD_PACKET) / 8);
130 int res = sendto(sock, buf, sizeof(RAD_PACKET), 0, (struct sockaddr *)&outerAddr, sizeof(outerAddr));
133 errorStr = "Error sending data";
137 //-----------------------------------------------------------------------------
138 int STG_CLIENT::RecvData(RAD_PACKET * packet)
140 char buf[RAD_MAX_PACKET_LEN];
143 struct sockaddr_in addr;
144 socklen_t len = sizeof(struct sockaddr_in);
146 res = recvfrom(sock, buf, RAD_MAX_PACKET_LEN, 0, reinterpret_cast<struct sockaddr *>(&addr), &len);
149 errorStr = "Error receiving data";
153 Decrypt((char *)packet, buf, res / 8);
157 //-----------------------------------------------------------------------------
158 int STG_CLIENT::Request(RAD_PACKET * packet, const std::string & login, const std::string & svc, uint8_t packetType)
162 memcpy((void *)&packet->magic, (void *)RAD_ID, RAD_MAGIC_LEN);
163 packet->protoVer[0] = '0';
164 packet->protoVer[1] = '1';
165 packet->packetType = packetType;
167 strncpy((char *)packet->login, login.c_str(), RAD_LOGIN_LEN);
168 strncpy((char *)packet->service, svc.c_str(), RAD_SERVICE_LEN);
174 res = RecvData(packet);
178 if (strncmp((char *)packet->magic, RAD_ID, RAD_MAGIC_LEN))
180 errorStr = "Magic invalid. Wanted: '";
182 errorStr += "', got: '";
183 errorStr += (char *)packet->magic;
190 //-----------------------------------------------------------------------------
191 int STG_CLIENT::Authorize(const string & login, const string & svc)
197 if (Request(&packet, login, svc, RAD_AUTZ_PACKET))
200 if (packet.packetType != RAD_ACCEPT_PACKET)
203 userPassword = (char *)packet.password;
207 //-----------------------------------------------------------------------------
208 int STG_CLIENT::Authenticate(const string & login, const string & svc)
214 if (Request(&packet, login, svc, RAD_AUTH_PACKET))
217 if (packet.packetType != RAD_ACCEPT_PACKET)
222 //-----------------------------------------------------------------------------
223 int STG_CLIENT::PostAuthenticate(const string & login, const string & svc)
229 if (Request(&packet, login, svc, RAD_POST_AUTH_PACKET))
232 if (packet.packetType != RAD_ACCEPT_PACKET)
235 if (svc == "Framed-User")
236 framedIP = packet.ip;
242 //-----------------------------------------------------------------------------
243 int STG_CLIENT::Account(const std::string & type, const string & login, const string & svc, const string & sessid)
248 strncpy((char *)packet.sessid, sessid.c_str(), RAD_SESSID_LEN);
252 if (Request(&packet, login, svc, RAD_ACCT_START_PACKET))
255 else if (type == "Stop")
257 if (Request(&packet, login, svc, RAD_ACCT_STOP_PACKET))
260 else if (type == "Interim-Update")
262 if (Request(&packet, login, svc, RAD_ACCT_UPDATE_PACKET))
267 if (Request(&packet, login, svc, RAD_ACCT_OTHER_PACKET))
271 if (packet.packetType != RAD_ACCEPT_PACKET)
276 //-----------------------------------------------------------------------------
277 void STG_CLIENT::Encrypt(char * dst, const char * src, int len8)
279 // len8 - длина в 8-ми байтовых блоках
281 memcpy(dst, src, len8 * 8);
283 for (int i = 0; i < len8; i++)
284 Blowfish_Encrypt(&ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));
286 //-----------------------------------------------------------------------------
287 void STG_CLIENT::Decrypt(char * dst, const char * src, int len8)
289 // len8 - длина в 8-ми байтовых блоках
291 memcpy(dst, src, len8 * 8);
293 for (int i = 0; i < len8; i++)
294 Blowfish_Decrypt(&ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));
296 //-----------------------------------------------------------------------------