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