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