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