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
40 #include "stg_client.h"
42 typedef std::vector<std::pair<std::string, std::string> > PAIRS;
44 //-----------------------------------------------------------------------------
46 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;
69 InitEncrypt(&ctx, password);
74 STG_CLIENT::~STG_CLIENT()
79 int STG_CLIENT::PrepareNet()
84 int STG_CLIENT::Send(const RAD_PACKET & packet)
86 /*char buf[RAD_MAX_PACKET_LEN];
88 Encrypt(&ctx, buf, (char *)&packet, sizeof(RAD_PACKET) / 8);
90 int res = sendto(sock, buf, sizeof(RAD_PACKET), 0, (struct sockaddr *)&outerAddr, sizeof(outerAddr));
93 errorStr = "Error sending data";
98 int STG_CLIENT::RecvData(RAD_PACKET * packet)
100 /*char buf[RAD_MAX_PACKET_LEN];
103 struct sockaddr_in addr;
104 socklen_t len = sizeof(struct sockaddr_in);
106 res = recvfrom(sock, buf, RAD_MAX_PACKET_LEN, 0, reinterpret_cast<struct sockaddr *>(&addr), &len);
109 errorStr = "Error receiving data";
113 Decrypt(&ctx, (char *)packet, buf, res / 8);
118 int STG_CLIENT::Request(RAD_PACKET * packet, const std::string & login, const std::string & svc, uint8_t packetType)
122 memcpy((void *)&packet->magic, (void *)RAD_ID, RAD_MAGIC_LEN);
123 packet->protoVer[0] = '0';
124 packet->protoVer[1] = '1';
125 packet->packetType = packetType;
127 strncpy((char *)packet->login, login.c_str(), RAD_LOGIN_LEN);
128 strncpy((char *)packet->service, svc.c_str(), RAD_SERVICE_LEN);
134 res = RecvData(packet);
138 if (strncmp((char *)packet->magic, RAD_ID, RAD_MAGIC_LEN))
140 errorStr = "Magic invalid. Wanted: '";
142 errorStr += "', got: '";
143 errorStr += (char *)packet->magic;
151 //-----------------------------------------------------------------------------
153 const STG_PAIRS * STG_CLIENT::Authorize(const std::string & login, const std::string & svc)
159 if (Request(&packet, login, svc, RAD_AUTZ_PACKET))
162 if (packet.packetType != RAD_ACCEPT_PACKET)
165 userPassword = (char *)packet.password;*/
168 pairs.push_back(std::make_pair("Cleartext-Password", userPassword));
170 return ToSTGPairs(pairs);
173 const STG_PAIRS * STG_CLIENT::Authenticate(const std::string & login, const std::string & svc)
179 if (Request(&packet, login, svc, RAD_AUTH_PACKET))
182 if (packet.packetType != RAD_ACCEPT_PACKET)
187 return ToSTGPairs(pairs);
190 const STG_PAIRS * STG_CLIENT::PostAuth(const std::string & login, const std::string & svc)
196 if (Request(&packet, login, svc, RAD_POST_AUTH_PACKET))
199 if (packet.packetType != RAD_ACCEPT_PACKET)
202 if (svc == "Framed-User")
203 framedIP = packet.ip;
208 pairs.push_back(std::make_pair("Framed-IP-Address", inet_ntostring(framedIP)));
210 return ToSTGPairs(pairs);
213 const STG_PAIRS * STG_CLIENT::PreAcct(const std::string & login, const std::String & service)
217 return ToSTGPairs(pairs);
220 const STG_PAIRS * STG_CLIENT::Account(const std::string & type, const std::string & login, const std::string & svc, const std::string & sessid)
225 strncpy((char *)packet.sessid, sessid.c_str(), RAD_SESSID_LEN);
229 if (Request(&packet, login, svc, RAD_ACCT_START_PACKET))
232 else if (type == "Stop")
234 if (Request(&packet, login, svc, RAD_ACCT_STOP_PACKET))
237 else if (type == "Interim-Update")
239 if (Request(&packet, login, svc, RAD_ACCT_UPDATE_PACKET))
244 if (Request(&packet, login, svc, RAD_ACCT_OTHER_PACKET))
248 if (packet.packetType != RAD_ACCEPT_PACKET)
253 return ToSTGPairs(pairs);
256 //-----------------------------------------------------------------------------
258 std::string STG_CLIENT_ST::m_host;
259 uint16_t STG_CLIENT_ST::m_port(6666);
260 std::string STG_CLIENT_ST::m_password;
262 //-----------------------------------------------------------------------------
264 STG_CLIENT * STG_CLIENT_ST::Get()
266 static STG_CLIENT * stgClient = NULL;
267 if ( stgClient == NULL )
268 stgClient = new STG_CLIENT(m_host, m_port, m_password);
272 void STG_CLIENT_ST::Configure(const std::string & host, uint16_t port, const std::string & password)
276 m_password = password;
279 //-----------------------------------------------------------------------------
281 const STG_PAIR * ToSTGPairs(const PAIRS & source)
283 STG_PAIR * pairs = new STG_PAIR[source.size() + 1];
284 for (size_t pos = 0; pos < source.size(); ++pos) {
285 bzero(pairs[pos].key, sizeof(STG_PAIR::key));
286 bzero(pairs[pos].value, sizeof(STG_PAIR::value));
287 strncpy(pairs[pos].key, source[pos].first.c_str(), sizeof(STG_PAIR::key));
288 strncpy(pairs[pos].value, source[pos].second.c_str(), sizeof(STG_PAIR::value));
291 bzero(pairs[sources.size()].key, sizeof(STG_PAIR::key));
292 bzero(pairs[sources.size()].value, sizeof(STG_PAIR::value));