]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp
Include common headers in rpcconfig.cpp
[stg.git] / projects / stargazer / plugins / configuration / rpcconfig / rpcconfig.cpp
1 #include <cstdlib>
2 #include <csignal>
3
4 #include "rpcconfig.h"
5
6 #include "admin.h"
7 #include "module_settings.h"
8 #include "common.h"
9
10 #include "info_methods.h"
11 #include "users_methods.h"
12 #include "tariffs_methods.h"
13 #include "admins_methods.h"
14 #include "messages_methods.h"
15
16 class RPC_CONFIG_CREATOR {
17 private:
18     RPC_CONFIG * rpcconfig;
19
20 public:
21     RPC_CONFIG_CREATOR()
22         : rpcconfig(new RPC_CONFIG())
23         {
24         }
25     ~RPC_CONFIG_CREATOR()
26         {
27         delete rpcconfig;
28         }
29
30     RPC_CONFIG * GetPlugin()
31         {
32         return rpcconfig;
33         }
34 };
35
36 RPC_CONFIG_CREATOR rpcc;
37
38 RPC_CONFIG_SETTINGS::RPC_CONFIG_SETTINGS()
39     : errorStr(),
40       port(0),
41       cookieTimeout(0)
42 {
43 }
44
45 int RPC_CONFIG_SETTINGS::ParseIntInRange(const std::string & str,
46                                          int min,
47                                          int max,
48                                          int * val)
49 {
50 if (str2x(str.c_str(), *val))
51     {
52     errorStr = "Incorrect value \'" + str + "\'.";
53     return -1;
54     }
55 if (*val < min || *val > max)
56     {
57     errorStr = "Value \'" + str + "\' out of range.";
58     return -1;
59     }
60 return 0;
61 }
62
63 int RPC_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
64 {
65 int p;
66 PARAM_VALUE pv;
67 vector<PARAM_VALUE>::const_iterator pvi;
68
69 pv.param = "Port";
70 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
71 if (pvi == s.moduleParams.end())
72     {
73     errorStr = "Parameter \'Port\' not found.";
74     printfd(__FILE__, "Parameter 'Port' not found\n");
75     return -1;
76     }
77 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
78     {
79     errorStr = "Cannot parse parameter \'Port\': " + errorStr;
80     printfd(__FILE__, "Cannot parse parameter 'Port'\n");
81     return -1;
82     }
83 port = p;
84
85 pv.param = "CookieTimeout";
86 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
87 if (pvi == s.moduleParams.end())
88     {
89     cookieTimeout = 1800; // 30 * 60
90     }
91 else
92     {
93     if (str2x(pvi->value[0], cookieTimeout))
94         {
95         errorStr = "Incorrect value of CookieTimeout: \'" + pvi->value[0] + "\'";
96         printfd(__FILE__, "Incorrect value of 'CookieTimeout'\n");
97         return -1;
98         }
99     }
100
101 return 0;
102 }
103
104 BASE_PLUGIN * GetPlugin()
105 {
106 return rpcc.GetPlugin();
107 }
108
109 RPC_CONFIG::RPC_CONFIG()
110     : users(NULL),
111       admins(NULL),
112       tariffs(NULL),
113       store(NULL),
114       stgSettings(NULL),
115       rpcServer(NULL),
116       running(false),
117       stopped(true)
118 {
119 }
120
121 RPC_CONFIG::~RPC_CONFIG()
122 {
123 // delete server
124 delete rpcServer;
125 }
126
127 int RPC_CONFIG::ParseSettings()
128 {
129 int ret = rpcConfigSettings.ParseSettings(settings);
130
131 if (ret)
132     errorStr = rpcConfigSettings.GetStrError();
133
134 return ret;
135 }
136
137 int RPC_CONFIG::Start()
138 {
139 InitiateRegistry();
140 running = true;
141 rpcServer = new xmlrpc_c::serverAbyss(
142         rpcRegistry,
143         rpcConfigSettings.GetPort(),
144         "/var/log/stargazer_rpc.log"
145         );
146 if (pthread_create(&tid, NULL, Run, this))
147     {
148     errorStr = "Failed to create RPC thread";
149     printfd(__FILE__, "Failed to crate RPC thread\n");
150     return -1;
151     }
152 return 0;
153 }
154
155 int RPC_CONFIG::Stop()
156 {
157 running = false;
158 for (int i = 0; i < 5 && !stopped; ++i)
159     usleep(200000);
160 //rpcServer->terminate();
161 if (!stopped)
162     {
163     if (pthread_kill(tid, SIGTERM))
164         {
165         errorStr = "Failed to kill thread";
166         printfd(__FILE__, "Failed to kill thread\n");
167         }
168     for (int i = 0; i < 25 && !stopped; ++i)
169         usleep(200000);
170     if (!stopped)
171         {
172         printfd(__FILE__, "Failed to stop RPC thread\n");
173         errorStr = "Failed to stop RPC thread";
174         return -1;
175         }
176     else
177         {
178         pthread_join(tid, NULL);
179         }
180     }
181 return 0;
182 }
183
184 void * RPC_CONFIG::Run(void * rc)
185 {
186 RPC_CONFIG * config = static_cast<RPC_CONFIG *>(rc);
187
188 config->stopped = false;
189 while (config->running)
190     {
191     config->rpcServer->runOnce();
192     }
193 config->stopped = true;
194
195 return NULL;
196 }
197
198 bool RPC_CONFIG::GetAdminInfo(const std::string & cookie,
199                               ADMIN_INFO * info)
200 {
201 std::map<std::string,
202          ADMIN_INFO>::iterator it;
203
204 it = cookies.find(cookie);
205
206 if (it == cookies.end())
207     {
208     return true;
209     }
210
211 if (difftime(it->second.accessTime, time(NULL)) >
212     rpcConfigSettings.GetCookieTimeout())
213     {
214     cookies.erase(it);
215     return true;
216     }
217
218 // Update access time
219 time(&it->second.accessTime);
220 *info = it->second;
221 return false;
222 }
223
224 bool RPC_CONFIG::CheckAdmin(const std::string & login,
225                             const std::string & password,
226                             std::string * cookie)
227 {
228 ADMIN admin;
229
230 if (!admins->AdminCorrect(login, password, &admin))
231     {
232     return true;
233     }
234
235 ADMIN_INFO info;
236 time(&info.accessTime);
237 info.admin = login;
238 info.priviledges = *admin.GetPriv();
239 *cookie = GetCookie();
240 cookies[*cookie] = info;
241
242 return false;
243 }
244
245 bool RPC_CONFIG::LogoutAdmin(const std::string & cookie)
246 {
247 std::map<std::string,
248          ADMIN_INFO>::iterator it;
249
250 it = cookies.find(cookie);
251
252 if (it == cookies.end())
253     {
254     return true;
255     }
256
257 cookies.erase(it);
258
259 return false;
260 }
261
262 std::string RPC_CONFIG::GetCookie() const
263 {
264 std::string charset("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
265 std::string cookie;
266
267 for (int i = 0; i < 64; ++i)
268     {
269     cookie += charset[rand() % charset.length()];
270     };
271
272 return cookie;
273 }
274
275 void RPC_CONFIG::InitiateRegistry()
276 {
277 // manage registry
278 xmlrpc_c::methodPtr const methodInfoPtr(new METHOD_INFO(
279             tariffs,
280             users,
281             stgSettings
282             ));
283 rpcRegistry.addMethod("stargazer.info", methodInfoPtr);
284
285 xmlrpc_c::methodPtr const methodLoginPtr(new METHOD_LOGIN(
286             this
287             ));
288 rpcRegistry.addMethod("stargazer.login", methodLoginPtr);
289
290 xmlrpc_c::methodPtr const methodLogoutPtr(new METHOD_LOGOUT(
291             this
292             ));
293 rpcRegistry.addMethod("stargazer.logout", methodLogoutPtr);
294
295 xmlrpc_c::methodPtr const methodGetUserPtr(new METHOD_USER_GET(
296             this,
297             users
298             ));
299 rpcRegistry.addMethod("stargazer.get_user", methodGetUserPtr);
300
301 xmlrpc_c::methodPtr const methodAddUserPtr(new METHOD_USER_ADD(
302             this,
303             admins,
304             users
305             ));
306 rpcRegistry.addMethod("stargazer.add_user", methodAddUserPtr);
307
308 xmlrpc_c::methodPtr const methodDelUserPtr(new METHOD_USER_DEL(
309             this,
310             admins,
311             users
312             ));
313 rpcRegistry.addMethod("stargazer.del_user", methodDelUserPtr);
314
315 xmlrpc_c::methodPtr const methodGetUsersPtr(new METHOD_USERS_GET(
316             this,
317             users
318             ));
319 rpcRegistry.addMethod("stargazer.get_users", methodGetUsersPtr);
320
321 xmlrpc_c::methodPtr const methodChgUserPtr(new METHOD_USER_CHG(
322             this,
323             admins,
324             tariffs,
325             store,
326             users
327             ));
328 rpcRegistry.addMethod("stargazer.chg_user", methodChgUserPtr);
329
330 xmlrpc_c::methodPtr const methodAddCashPtr(new METHOD_USER_CASH_ADD(
331             this,
332             admins,
333             store,
334             users
335             ));
336 rpcRegistry.addMethod("stargazer.add_cash", methodAddCashPtr);
337
338 xmlrpc_c::methodPtr const methodSetCashPtr(new METHOD_USER_CASH_SET(
339             this,
340             admins,
341             store,
342             users
343             ));
344 rpcRegistry.addMethod("stargazer.set_cash", methodSetCashPtr);
345
346 xmlrpc_c::methodPtr const methodTariffChangePtr(new METHOD_USER_TARIFF_CHANGE(
347             this,
348             admins,
349             tariffs,
350             store,
351             users
352             ));
353 rpcRegistry.addMethod("stargazer.tariff_change", methodTariffChangePtr);
354
355 xmlrpc_c::methodPtr const methodGetTariffPtr(new METHOD_TARIFF_GET(
356             this,
357             tariffs
358             ));
359 rpcRegistry.addMethod("stargazer.get_tariff", methodGetTariffPtr);
360
361 xmlrpc_c::methodPtr const methodChgTariffPtr(new METHOD_TARIFF_CHG(
362             this,
363             admins,
364             tariffs
365             ));
366 rpcRegistry.addMethod("stargazer.chg_tariff", methodChgTariffPtr);
367
368 xmlrpc_c::methodPtr const methodGetTariffsPtr(new METHOD_TARIFFS_GET(
369             this,
370             tariffs
371             ));
372 rpcRegistry.addMethod("stargazer.get_tariffs", methodGetTariffsPtr);
373
374 xmlrpc_c::methodPtr const methodAddTariffPtr(new METHOD_TARIFF_ADD(
375             this,
376             admins,
377             tariffs
378             ));
379 rpcRegistry.addMethod("stargazer.add_tariff", methodAddTariffPtr);
380
381 xmlrpc_c::methodPtr const methodDelTariffPtr(new METHOD_TARIFF_DEL(
382             this,
383             admins,
384             tariffs,
385             users
386             ));
387 rpcRegistry.addMethod("stargazer.del_tariff", methodDelTariffPtr);
388
389 xmlrpc_c::methodPtr const methodGetAdminPtr(new METHOD_ADMIN_GET(
390             this,
391             admins
392             ));
393 rpcRegistry.addMethod("stargazer.get_admin", methodGetAdminPtr);
394
395 xmlrpc_c::methodPtr const methodAddAdminPtr(new METHOD_ADMIN_ADD(
396             this,
397             admins
398             ));
399 rpcRegistry.addMethod("stargazer.add_admin", methodAddAdminPtr);
400
401 xmlrpc_c::methodPtr const methodDelAdminPtr(new METHOD_ADMIN_DEL(
402             this,
403             admins
404             ));
405 rpcRegistry.addMethod("stargazer.del_admin", methodDelAdminPtr);
406
407 xmlrpc_c::methodPtr const methodChgAdminPtr(new METHOD_ADMIN_CHG(
408             this,
409             admins
410             ));
411 rpcRegistry.addMethod("stargazer.chg_admin", methodChgAdminPtr);
412
413 xmlrpc_c::methodPtr const methodGetAdminsPtr(new METHOD_ADMINS_GET(
414             this,
415             admins
416             ));
417 rpcRegistry.addMethod("stargazer.get_admins", methodGetAdminsPtr);
418
419 xmlrpc_c::methodPtr const methodSendMessagePtr(new METHOD_MESSAGE_SEND(
420             this,
421             users
422             ));
423 rpcRegistry.addMethod("stargazer.send_message", methodSendMessagePtr);
424
425 xmlrpc_c::methodPtr const methodGetOnlinIPsPtr(new METHOD_GET_ONLINE_IPS(
426             this,
427             users
428             ));
429 rpcRegistry.addMethod("stargazer.get_online_ips", methodGetOnlinIPsPtr);
430 }
431