]> git.stg.codes - stg.git/blob - projects/rlm_stg/stg_client.cpp
Fix trailing whitespaces.
[stg.git] / projects / rlm_stg / stg_client.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 /*
22  *  Realization of data access via Stargazer for RADIUS
23  *
24  *  $Revision: 1.8 $
25  *  $Date: 2010/04/16 12:30:02 $
26  *
27  */
28
29 #include <netdb.h>
30 #include <sys/types.h>
31 #include <unistd.h> // close
32
33 #include <cerrno>
34 #include <cstring>
35 #include <vector>
36 #include <utility>
37
38 #include <stdexcept>
39
40 #include "stg_client.h"
41
42 typedef std::vector<std::pair<std::string, std::string> > PAIRS;
43
44 //-----------------------------------------------------------------------------
45
46 STG_CLIENT::STG_CLIENT(const std::string & host, uint16_t port, uint16_t lp, const std::string & pass)
47     : password(pass),
48       framedIP(0)
49 {
50 /*sock = socket(AF_INET, SOCK_DGRAM, 0);
51 if (sock == -1)
52     {
53     std::string message = strerror(errno);
54     message = "Socket create error: '" + message + "'";
55     throw std::runtime_error(message);
56     }
57
58 struct hostent * he = NULL;
59 he = gethostbyname(host.c_str());
60 if (he == NULL)
61     {
62     throw std::runtime_error("gethostbyname error");
63     }
64
65 outerAddr.sin_family = AF_INET;
66 outerAddr.sin_port = htons(port);
67 outerAddr.sin_addr.s_addr = *(uint32_t *)he->h_addr;
68
69 InitEncrypt(&ctx, password);
70
71 PrepareNet();*/
72 }
73
74 STG_CLIENT::~STG_CLIENT()
75 {
76 /*close(sock);*/
77 }
78
79 int STG_CLIENT::PrepareNet()
80 {
81 return 0;
82 }
83
84 int STG_CLIENT::Send(const RAD_PACKET & packet)
85 {
86 /*char buf[RAD_MAX_PACKET_LEN];
87
88 Encrypt(&ctx, buf, (char *)&packet, sizeof(RAD_PACKET) / 8);
89
90 int res = sendto(sock, buf, sizeof(RAD_PACKET), 0, (struct sockaddr *)&outerAddr, sizeof(outerAddr));
91
92 if (res == -1)
93     errorStr = "Error sending data";
94
95 return res;*/
96 }
97
98 int STG_CLIENT::RecvData(RAD_PACKET * packet)
99 {
100 /*char buf[RAD_MAX_PACKET_LEN];
101 int res;
102
103 struct sockaddr_in addr;
104 socklen_t len = sizeof(struct sockaddr_in);
105
106 res = recvfrom(sock, buf, RAD_MAX_PACKET_LEN, 0, reinterpret_cast<struct sockaddr *>(&addr), &len);
107 if (res == -1)
108     {
109     errorStr = "Error receiving data";
110     return -1;
111     }
112
113 Decrypt(&ctx, (char *)packet, buf, res / 8);
114
115 return 0;*/
116 }
117
118 int STG_CLIENT::Request(RAD_PACKET * packet, const std::string & login, const std::string & svc, uint8_t packetType)
119 {
120 /*int res;
121
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;
126 packet->ip = 0;
127 strncpy((char *)packet->login, login.c_str(), RAD_LOGIN_LEN);
128 strncpy((char *)packet->service, svc.c_str(), RAD_SERVICE_LEN);
129
130 res = Send(*packet);
131 if (res == -1)
132     return -1;
133
134 res = RecvData(packet);
135 if (res == -1)
136     return -1;
137
138 if (strncmp((char *)packet->magic, RAD_ID, RAD_MAGIC_LEN))
139     {
140     errorStr = "Magic invalid. Wanted: '";
141     errorStr += RAD_ID;
142     errorStr += "', got: '";
143     errorStr += (char *)packet->magic;
144     errorStr += "'";
145     return -1;
146     }
147
148 return 0;*/
149 }
150
151 //-----------------------------------------------------------------------------
152
153 const STG_PAIRS * STG_CLIENT::Authorize(const std::string & login, const std::string & svc)
154 {
155 /*RAD_PACKET packet;
156
157 userPassword = "";
158
159 if (Request(&packet, login, svc, RAD_AUTZ_PACKET))
160     return -1;
161
162 if (packet.packetType != RAD_ACCEPT_PACKET)
163     return -1;
164
165 userPassword = (char *)packet.password;*/
166
167 PAIRS pairs;
168 pairs.push_back(std::make_pair("Cleartext-Password", userPassword));
169
170 return ToSTGPairs(pairs);
171 }
172
173 const STG_PAIRS * STG_CLIENT::Authenticate(const std::string & login, const std::string & svc)
174 {
175 /*RAD_PACKET packet;
176
177 userPassword = "";
178
179 if (Request(&packet, login, svc, RAD_AUTH_PACKET))
180     return -1;
181
182 if (packet.packetType != RAD_ACCEPT_PACKET)
183     return -1;*/
184
185 PAIRS pairs;
186
187 return ToSTGPairs(pairs);
188 }
189
190 const STG_PAIRS * STG_CLIENT::PostAuth(const std::string & login, const std::string & svc)
191 {
192 /*RAD_PACKET packet;
193
194 userPassword = "";
195
196 if (Request(&packet, login, svc, RAD_POST_AUTH_PACKET))
197     return -1;
198
199 if (packet.packetType != RAD_ACCEPT_PACKET)
200     return -1;
201
202 if (svc == "Framed-User")
203     framedIP = packet.ip;
204 else
205     framedIP = 0;*/
206
207 PAIRS pairs;
208 pairs.push_back(std::make_pair("Framed-IP-Address", inet_ntostring(framedIP)));
209
210 return ToSTGPairs(pairs);
211 }
212
213 const STG_PAIRS * STG_CLIENT::PreAcct(const std::string & login, const std::String & service)
214 {
215 PAIRS pairs;
216
217 return ToSTGPairs(pairs);
218 }
219
220 const STG_PAIRS * STG_CLIENT::Account(const std::string & type, const std::string & login, const std::string & svc, const std::string & sessid)
221 {
222 /*RAD_PACKET packet;
223
224 userPassword = "";
225 strncpy((char *)packet.sessid, sessid.c_str(), RAD_SESSID_LEN);
226
227 if (type == "Start")
228     {
229     if (Request(&packet, login, svc, RAD_ACCT_START_PACKET))
230         return -1;
231     }
232 else if (type == "Stop")
233     {
234     if (Request(&packet, login, svc, RAD_ACCT_STOP_PACKET))
235         return -1;
236     }
237 else if (type == "Interim-Update")
238     {
239     if (Request(&packet, login, svc, RAD_ACCT_UPDATE_PACKET))
240         return -1;
241     }
242 else
243     {
244     if (Request(&packet, login, svc, RAD_ACCT_OTHER_PACKET))
245         return -1;
246     }
247
248 if (packet.packetType != RAD_ACCEPT_PACKET)
249     return -1;*/
250
251 PAIRS pairs;
252
253 return ToSTGPairs(pairs);
254 }
255
256 //-----------------------------------------------------------------------------
257
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;
261
262 //-----------------------------------------------------------------------------
263
264 STG_CLIENT * STG_CLIENT_ST::Get()
265 {
266     static STG_CLIENT * stgClient = NULL;
267     if ( stgClient == NULL )
268         stgClient = new STG_CLIENT(m_host, m_port, m_password);
269     return stgClient;
270 }
271
272 void STG_CLIENT_ST::Configure(const std::string & host, uint16_t port, const std::string & password)
273 {
274     m_host = host;
275     m_port = port;
276     m_password = password;
277 }
278
279 //-----------------------------------------------------------------------------
280
281 const STG_PAIR * ToSTGPairs(const PAIRS & source)
282 {
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));
289         ++pos;
290     }
291     bzero(pairs[sources.size()].key, sizeof(STG_PAIR::key));
292     bzero(pairs[sources.size()].value, sizeof(STG_PAIR::value));
293
294     return pairs;
295 }