2 #include <sys/socket.h>
3 #include <netinet/in.h>
13 #include <ostream> // xmlrpc-c devs have missed something :)
15 #include "stg/common.h"
16 #include "stg/admin.h"
17 #include "stg/module_settings.h"
18 #include "stg/settings.h"
19 #include "stg/plugin_creator.h"
21 #include "rpcconfig.h"
22 #include "info_methods.h"
23 #include "users_methods.h"
24 #include "tariffs_methods.h"
25 #include "admins_methods.h"
26 #include "messages_methods.h"
30 PLUGIN_CREATOR<RPC_CONFIG> rpcc;
33 extern "C" PLUGIN * GetPlugin();
35 RPC_CONFIG_SETTINGS::RPC_CONFIG_SETTINGS()
41 int RPC_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
45 std::vector<PARAM_VALUE>::const_iterator pvi;
46 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
47 if (pvi == s.moduleParams.end() || pvi->value.empty())
49 errorStr = "Parameter \'Port\' not found.";
50 printfd(__FILE__, "Parameter 'Port' not found\n");
54 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
56 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
57 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
60 port = static_cast<uint16_t>(p);
62 pv.param = "CookieTimeout";
63 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
64 if (pvi == s.moduleParams.end() || pvi->value.empty())
66 cookieTimeout = 1800; // 30 * 60
70 if (str2x(pvi->value[0], cookieTimeout))
72 errorStr = "Incorrect value of CookieTimeout: \'" + pvi->value[0] + "\'";
73 printfd(__FILE__, "Incorrect value of 'CookieTimeout'\n");
83 return rpcc.GetPlugin();
86 RPC_CONFIG::RPC_CONFIG()
96 logger(GetPluginLogger(GetStgLogger(), "conf_rpc"))
100 RPC_CONFIG::~RPC_CONFIG()
106 int RPC_CONFIG::ParseSettings()
108 int ret = rpcConfigSettings.ParseSettings(settings);
111 errorStr = rpcConfigSettings.GetStrError();
116 void RPC_CONFIG::SetStgSettings(const SETTINGS * s)
118 dayFee = s->GetDayFee();
119 dirNames.erase(dirNames.begin(), dirNames.end());
120 for (size_t i = 0; i < DIR_NUM; ++i) {
121 dirNames.push_back(s->GetDirName(i));
125 int RPC_CONFIG::Start()
130 fd = socket(AF_INET, SOCK_STREAM, 0);
133 errorStr = "Failed to create socket";
134 logger("Cannot create a socket: %s", strerror(errno));
135 printfd(__FILE__, "Failed to create listening socket: %s\n", strerror(errno));
141 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)))
143 errorStr = "Setsockopt failed.";
144 logger("setsockopt error: %s", strerror(errno));
145 printfd(__FILE__, "Setsockopt failed: %s\n", strerror(errno));
149 struct sockaddr_in addr;
150 addr.sin_family = AF_INET;
151 addr.sin_port = htons(rpcConfigSettings.GetPort());
152 addr.sin_addr.s_addr = inet_addr("0.0.0.0");
154 if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)))
156 logger("Cannot bind the socket: %s", strerror(errno));
157 errorStr = "Failed to bind socket";
158 printfd(__FILE__, "Failed to bind listening socket: %s\n", strerror(errno));
164 logger("Cannot listen the socket: %s", strerror(errno));
165 errorStr = "Failed to listen socket";
166 printfd(__FILE__, "Failed to listen listening socket: %s\n", strerror(errno));
170 rpcServer = new xmlrpc_c::serverAbyss(
171 xmlrpc_c::serverAbyss::constrOpt()
172 .registryP(&rpcRegistry)
173 .logFileName("/var/log/stargazer_rpc.log")
177 if (pthread_create(&tid, NULL, Run, this))
179 errorStr = "Failed to create RPC thread";
180 logger("Cannot create RPC thread.");
181 printfd(__FILE__, "Failed to crate RPC thread\n");
188 int RPC_CONFIG::Stop()
191 for (int i = 0; i < 5 && !stopped; ++i)
193 struct timespec ts = {0, 200000000};
194 nanosleep(&ts, NULL);
200 logger("Cannot stop RPC thread.");
201 printfd(__FILE__, "Failed to stop RPC thread\n");
202 errorStr = "Failed to stop RPC thread";
207 pthread_join(tid, NULL);
215 void * RPC_CONFIG::Run(void * rc)
218 sigfillset(&signalSet);
219 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
221 RPC_CONFIG * config = static_cast<RPC_CONFIG *>(rc);
223 config->stopped = false;
224 while (config->running)
226 if (WaitPackets(config->fd))
227 config->rpcServer->runOnce();
229 config->stopped = true;
234 bool RPC_CONFIG::GetAdminInfo(const std::string & cookie,
237 std::map<std::string,
238 ADMIN_INFO>::iterator it;
240 it = cookies.find(cookie);
242 if (it == cookies.end())
247 if (difftime(it->second.accessTime, time(NULL)) >
248 rpcConfigSettings.GetCookieTimeout())
254 // Update access time
255 time(&it->second.accessTime);
260 bool RPC_CONFIG::CheckAdmin(const std::string & login,
261 const std::string & password,
262 std::string * cookie)
264 ADMIN * admin = NULL;
266 if (!admins->Correct(login, password, &admin))
268 logger("Attempt to connect with invalid credentials. Login: %s", login.c_str());
273 time(&info.accessTime);
275 info.priviledges = *admin->GetPriv();
276 *cookie = GetCookie();
277 cookies[*cookie] = info;
282 bool RPC_CONFIG::LogoutAdmin(const std::string & cookie)
284 std::map<std::string,
285 ADMIN_INFO>::iterator it;
287 it = cookies.find(cookie);
289 if (it == cookies.end())
299 std::string RPC_CONFIG::GetCookie() const
301 std::string charset("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
304 for (int i = 0; i < 64; ++i)
306 cookie += charset[rand() % charset.length()];
312 void RPC_CONFIG::InitiateRegistry()
315 xmlrpc_c::methodPtr const methodInfoPtr(new METHOD_INFO(
321 rpcRegistry.addMethod("stargazer.info", methodInfoPtr);
323 xmlrpc_c::methodPtr const methodLoginPtr(new METHOD_LOGIN(
326 rpcRegistry.addMethod("stargazer.login", methodLoginPtr);
328 xmlrpc_c::methodPtr const methodLogoutPtr(new METHOD_LOGOUT(
331 rpcRegistry.addMethod("stargazer.logout", methodLogoutPtr);
333 xmlrpc_c::methodPtr const methodGetUserPtr(new METHOD_USER_GET(
337 rpcRegistry.addMethod("stargazer.get_user", methodGetUserPtr);
339 xmlrpc_c::methodPtr const methodAddUserPtr(new METHOD_USER_ADD(
344 rpcRegistry.addMethod("stargazer.add_user", methodAddUserPtr);
346 xmlrpc_c::methodPtr const methodDelUserPtr(new METHOD_USER_DEL(
351 rpcRegistry.addMethod("stargazer.del_user", methodDelUserPtr);
353 xmlrpc_c::methodPtr const methodGetUsersPtr(new METHOD_USERS_GET(
357 rpcRegistry.addMethod("stargazer.get_users", methodGetUsersPtr);
359 xmlrpc_c::methodPtr const methodChgUserPtr(new METHOD_USER_CHG(
366 rpcRegistry.addMethod("stargazer.chg_user", methodChgUserPtr);
368 xmlrpc_c::methodPtr const methodAddCashPtr(new METHOD_USER_CASH_ADD(
374 rpcRegistry.addMethod("stargazer.add_user_cash", methodAddCashPtr);
376 xmlrpc_c::methodPtr const methodSetCashPtr(new METHOD_USER_CASH_SET(
382 rpcRegistry.addMethod("stargazer.set_user_cash", methodSetCashPtr);
384 xmlrpc_c::methodPtr const methodTariffChangePtr(new METHOD_USER_TARIFF_CHANGE(
391 rpcRegistry.addMethod("stargazer.chg_user_tariff", methodTariffChangePtr);
393 xmlrpc_c::methodPtr const methodGetTariffPtr(new METHOD_TARIFF_GET(
397 rpcRegistry.addMethod("stargazer.get_tariff", methodGetTariffPtr);
399 xmlrpc_c::methodPtr const methodChgTariffPtr(new METHOD_TARIFF_CHG(
404 rpcRegistry.addMethod("stargazer.chg_tariff", methodChgTariffPtr);
406 xmlrpc_c::methodPtr const methodGetTariffsPtr(new METHOD_TARIFFS_GET(
410 rpcRegistry.addMethod("stargazer.get_tariffs", methodGetTariffsPtr);
412 xmlrpc_c::methodPtr const methodAddTariffPtr(new METHOD_TARIFF_ADD(
417 rpcRegistry.addMethod("stargazer.add_tariff", methodAddTariffPtr);
419 xmlrpc_c::methodPtr const methodDelTariffPtr(new METHOD_TARIFF_DEL(
425 rpcRegistry.addMethod("stargazer.del_tariff", methodDelTariffPtr);
427 xmlrpc_c::methodPtr const methodGetAdminPtr(new METHOD_ADMIN_GET(
431 rpcRegistry.addMethod("stargazer.get_admin", methodGetAdminPtr);
433 xmlrpc_c::methodPtr const methodAddAdminPtr(new METHOD_ADMIN_ADD(
437 rpcRegistry.addMethod("stargazer.add_admin", methodAddAdminPtr);
439 xmlrpc_c::methodPtr const methodDelAdminPtr(new METHOD_ADMIN_DEL(
443 rpcRegistry.addMethod("stargazer.del_admin", methodDelAdminPtr);
445 xmlrpc_c::methodPtr const methodChgAdminPtr(new METHOD_ADMIN_CHG(
449 rpcRegistry.addMethod("stargazer.chg_admin", methodChgAdminPtr);
451 xmlrpc_c::methodPtr const methodGetAdminsPtr(new METHOD_ADMINS_GET(
455 rpcRegistry.addMethod("stargazer.get_admins", methodGetAdminsPtr);
457 xmlrpc_c::methodPtr const methodSendMessagePtr(new METHOD_MESSAGE_SEND(
461 rpcRegistry.addMethod("stargazer.send_user_message", methodSendMessagePtr);
463 xmlrpc_c::methodPtr const methodGetOnlinIPsPtr(new METHOD_GET_ONLINE_IPS(
467 rpcRegistry.addMethod("stargazer.get_online_ips", methodGetOnlinIPsPtr);
469 xmlrpc_c::methodPtr const methodGetUserAuthByPtr(new METHOD_GET_USER_AUTH_BY(
473 rpcRegistry.addMethod("stargazer.get_user_auth_by", methodGetUserAuthByPtr);