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