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()
42 int RPC_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
46 std::vector<PARAM_VALUE>::const_iterator pvi;
47 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
48 if (pvi == s.moduleParams.end())
50 errorStr = "Parameter \'Port\' not found.";
51 printfd(__FILE__, "Parameter 'Port' not found\n");
55 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
57 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
58 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
61 port = static_cast<uint16_t>(p);
63 pv.param = "CookieTimeout";
64 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
65 if (pvi == s.moduleParams.end())
67 cookieTimeout = 1800; // 30 * 60
71 if (str2x(pvi->value[0], cookieTimeout))
73 errorStr = "Incorrect value of CookieTimeout: \'" + pvi->value[0] + "\'";
74 printfd(__FILE__, "Incorrect value of 'CookieTimeout'\n");
84 return rpcc.GetPlugin();
87 RPC_CONFIG::RPC_CONFIG()
104 logger(GetPluginLogger(GetStgLogger(), "conf_rpc"))
108 RPC_CONFIG::~RPC_CONFIG()
114 int RPC_CONFIG::ParseSettings()
116 int ret = rpcConfigSettings.ParseSettings(settings);
119 errorStr = rpcConfigSettings.GetStrError();
124 void RPC_CONFIG::SetStgSettings(const SETTINGS * s)
126 dayFee = s->GetDayFee();
127 dirNames.erase(dirNames.begin(), dirNames.end());
128 for (size_t i = 0; i < DIR_NUM; ++i) {
129 dirNames.push_back(s->GetDirName(i));
133 int RPC_CONFIG::Start()
138 fd = socket(AF_INET, SOCK_STREAM, 0);
141 errorStr = "Failed to create socket";
142 logger("Cannot create a socket: %s", strerror(errno));
143 printfd(__FILE__, "Failed to create listening socket: %s\n", strerror(errno));
149 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)))
151 errorStr = "Setsockopt failed.";
152 logger("setsockopt error: %s", strerror(errno));
153 printfd(__FILE__, "Setsockopt failed: %s\n", strerror(errno));
157 struct sockaddr_in addr;
158 addr.sin_family = AF_INET;
159 addr.sin_port = htons(rpcConfigSettings.GetPort());
160 addr.sin_addr.s_addr = inet_addr("0.0.0.0");
162 if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)))
164 logger("Cannot bind the socket: %s", strerror(errno));
165 errorStr = "Failed to bind socket";
166 printfd(__FILE__, "Failed to bind listening socket: %s\n", strerror(errno));
172 logger("Cannot listen the socket: %s", strerror(errno));
173 errorStr = "Failed to listen socket";
174 printfd(__FILE__, "Failed to listen listening socket: %s\n", strerror(errno));
178 rpcServer = new xmlrpc_c::serverAbyss(
179 xmlrpc_c::serverAbyss::constrOpt()
180 .registryP(&rpcRegistry)
181 .logFileName("/var/log/stargazer_rpc.log")
185 if (pthread_create(&tid, NULL, Run, this))
187 errorStr = "Failed to create RPC thread";
188 logger("Cannot create RPC thread.");
189 printfd(__FILE__, "Failed to crate RPC thread\n");
196 int RPC_CONFIG::Stop()
199 for (int i = 0; i < 5 && !stopped; ++i)
201 struct timespec ts = {0, 200000000};
202 nanosleep(&ts, NULL);
208 logger("Cannot stop RPC thread.");
209 printfd(__FILE__, "Failed to stop RPC thread\n");
210 errorStr = "Failed to stop RPC thread";
215 pthread_join(tid, NULL);
223 void * RPC_CONFIG::Run(void * rc)
226 sigfillset(&signalSet);
227 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
229 RPC_CONFIG * config = static_cast<RPC_CONFIG *>(rc);
231 config->stopped = false;
232 while (config->running)
234 if (WaitPackets(config->fd))
235 config->rpcServer->runOnce();
237 config->stopped = true;
242 bool RPC_CONFIG::GetAdminInfo(const std::string & cookie,
245 std::map<std::string,
246 ADMIN_INFO>::iterator it;
248 it = cookies.find(cookie);
250 if (it == cookies.end())
255 if (difftime(it->second.accessTime, time(NULL)) >
256 rpcConfigSettings.GetCookieTimeout())
262 // Update access time
263 time(&it->second.accessTime);
268 bool RPC_CONFIG::CheckAdmin(const std::string & login,
269 const std::string & password,
270 std::string * cookie)
272 ADMIN * admin = NULL;
274 if (!admins->Correct(login, password, &admin))
276 logger("Attempt to connect with invalid credentials. Login: %s", login.c_str());
281 time(&info.accessTime);
283 info.priviledges = *admin->GetPriv();
284 *cookie = GetCookie();
285 cookies[*cookie] = info;
290 bool RPC_CONFIG::LogoutAdmin(const std::string & cookie)
292 std::map<std::string,
293 ADMIN_INFO>::iterator it;
295 it = cookies.find(cookie);
297 if (it == cookies.end())
307 std::string RPC_CONFIG::GetCookie() const
309 std::string charset("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
312 for (int i = 0; i < 64; ++i)
314 cookie += charset[rand() % charset.length()];
320 void RPC_CONFIG::InitiateRegistry()
323 xmlrpc_c::methodPtr const methodInfoPtr(new METHOD_INFO(
329 rpcRegistry.addMethod("stargazer.info", methodInfoPtr);
331 xmlrpc_c::methodPtr const methodLoginPtr(new METHOD_LOGIN(
334 rpcRegistry.addMethod("stargazer.login", methodLoginPtr);
336 xmlrpc_c::methodPtr const methodLogoutPtr(new METHOD_LOGOUT(
339 rpcRegistry.addMethod("stargazer.logout", methodLogoutPtr);
341 xmlrpc_c::methodPtr const methodGetUserPtr(new METHOD_USER_GET(
345 rpcRegistry.addMethod("stargazer.get_user", methodGetUserPtr);
347 xmlrpc_c::methodPtr const methodAddUserPtr(new METHOD_USER_ADD(
352 rpcRegistry.addMethod("stargazer.add_user", methodAddUserPtr);
354 xmlrpc_c::methodPtr const methodDelUserPtr(new METHOD_USER_DEL(
359 rpcRegistry.addMethod("stargazer.del_user", methodDelUserPtr);
361 xmlrpc_c::methodPtr const methodGetUsersPtr(new METHOD_USERS_GET(
365 rpcRegistry.addMethod("stargazer.get_users", methodGetUsersPtr);
367 xmlrpc_c::methodPtr const methodChgUserPtr(new METHOD_USER_CHG(
374 rpcRegistry.addMethod("stargazer.chg_user", methodChgUserPtr);
376 xmlrpc_c::methodPtr const methodAddCashPtr(new METHOD_USER_CASH_ADD(
382 rpcRegistry.addMethod("stargazer.add_user_cash", methodAddCashPtr);
384 xmlrpc_c::methodPtr const methodSetCashPtr(new METHOD_USER_CASH_SET(
390 rpcRegistry.addMethod("stargazer.set_user_cash", methodSetCashPtr);
392 xmlrpc_c::methodPtr const methodTariffChangePtr(new METHOD_USER_TARIFF_CHANGE(
399 rpcRegistry.addMethod("stargazer.chg_user_tariff", methodTariffChangePtr);
401 xmlrpc_c::methodPtr const methodGetTariffPtr(new METHOD_TARIFF_GET(
405 rpcRegistry.addMethod("stargazer.get_tariff", methodGetTariffPtr);
407 xmlrpc_c::methodPtr const methodChgTariffPtr(new METHOD_TARIFF_CHG(
412 rpcRegistry.addMethod("stargazer.chg_tariff", methodChgTariffPtr);
414 xmlrpc_c::methodPtr const methodGetTariffsPtr(new METHOD_TARIFFS_GET(
418 rpcRegistry.addMethod("stargazer.get_tariffs", methodGetTariffsPtr);
420 xmlrpc_c::methodPtr const methodAddTariffPtr(new METHOD_TARIFF_ADD(
425 rpcRegistry.addMethod("stargazer.add_tariff", methodAddTariffPtr);
427 xmlrpc_c::methodPtr const methodDelTariffPtr(new METHOD_TARIFF_DEL(
433 rpcRegistry.addMethod("stargazer.del_tariff", methodDelTariffPtr);
435 xmlrpc_c::methodPtr const methodGetAdminPtr(new METHOD_ADMIN_GET(
439 rpcRegistry.addMethod("stargazer.get_admin", methodGetAdminPtr);
441 xmlrpc_c::methodPtr const methodAddAdminPtr(new METHOD_ADMIN_ADD(
445 rpcRegistry.addMethod("stargazer.add_admin", methodAddAdminPtr);
447 xmlrpc_c::methodPtr const methodDelAdminPtr(new METHOD_ADMIN_DEL(
451 rpcRegistry.addMethod("stargazer.del_admin", methodDelAdminPtr);
453 xmlrpc_c::methodPtr const methodChgAdminPtr(new METHOD_ADMIN_CHG(
457 rpcRegistry.addMethod("stargazer.chg_admin", methodChgAdminPtr);
459 xmlrpc_c::methodPtr const methodGetAdminsPtr(new METHOD_ADMINS_GET(
463 rpcRegistry.addMethod("stargazer.get_admins", methodGetAdminsPtr);
465 xmlrpc_c::methodPtr const methodSendMessagePtr(new METHOD_MESSAGE_SEND(
469 rpcRegistry.addMethod("stargazer.send_user_message", methodSendMessagePtr);
471 xmlrpc_c::methodPtr const methodGetOnlinIPsPtr(new METHOD_GET_ONLINE_IPS(
475 rpcRegistry.addMethod("stargazer.get_online_ips", methodGetOnlinIPsPtr);
477 xmlrpc_c::methodPtr const methodGetUserAuthByPtr(new METHOD_GET_USER_AUTH_BY(
481 rpcRegistry.addMethod("stargazer.get_user_auth_by", methodGetUserAuthByPtr);