2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
22 #include "parser_users.h"
24 #include "stg/users.h"
26 #include "stg/user_conf.h"
27 #include "stg/user_stat.h"
28 #include "stg/tariffs.h"
29 #include "stg/tariff.h"
30 #include "stg/user_property.h"
35 using STG::PARSER::GET_USERS;
36 using STG::PARSER::GET_USER;
37 using STG::PARSER::ADD_USER;
38 using STG::PARSER::DEL_USER;
39 using STG::PARSER::CHG_USER;
40 using STG::PARSER::CHECK_USER;
42 const char * GET_USERS::tag = "GetUsers";
43 const char * GET_USER::tag = "GetUser";
44 const char * ADD_USER::tag = "AddUser";
45 const char * CHG_USER::tag = "SetUser";
46 const char * DEL_USER::tag = "DelUser";
47 const char * CHECK_USER::tag = "CheckUser";
49 using UserPtr = STG::User*;
50 using ConstUserPtr = const STG::User*;
55 std::string UserToXML(const STG::User & user, bool loginInStart, bool showPass, time_t lastTime = 0)
60 answer += "<User login=\"" + user.GetLogin() + "\" result=\"ok\">";
62 answer += "<User result=\"ok\">";
64 answer += "<Login value=\"" + user.GetLogin() + "\"/>";
66 if (user.GetProperties().password.ModificationTime() > lastTime)
69 answer += "<Password value=\"" + user.GetProperties().password.Get() + "\" />";
71 answer += "<Password value=\"++++++\"/>";
74 if (user.GetProperties().cash.ModificationTime() > lastTime)
75 answer += "<Cash value=\"" + std::to_string(user.GetProperties().cash.Get()) + "\"/>";
76 if (user.GetProperties().freeMb.ModificationTime() > lastTime)
77 answer += "<FreeMb value=\"" + std::to_string(user.GetProperties().freeMb.Get()) + "\"/>";
78 if (user.GetProperties().credit.ModificationTime() > lastTime)
79 answer += "<Credit value=\"" + std::to_string(user.GetProperties().credit.Get()) + "\"/>";
81 if (user.GetProperties().nextTariff.Get() != "")
83 if (user.GetProperties().tariffName.ModificationTime() > lastTime ||
84 user.GetProperties().nextTariff.ModificationTime() > lastTime)
85 answer += "<Tariff value=\"" + user.GetProperties().tariffName.Get() + "/" + user.GetProperties().nextTariff.Get() + "\"/>";
89 if (user.GetProperties().tariffName.ModificationTime() > lastTime)
90 answer += "<Tariff value=\"" + user.GetProperties().tariffName.Get() + "\"/>";
93 if (user.GetProperties().note.ModificationTime() > lastTime)
94 answer += "<Note value=\"" + Encode12str(user.GetProperties().note) + "\"/>";
95 if (user.GetProperties().phone.ModificationTime() > lastTime)
96 answer += "<Phone value=\"" + Encode12str(user.GetProperties().phone) + "\"/>";
97 if (user.GetProperties().address.ModificationTime() > lastTime)
98 answer += "<Address value=\"" + Encode12str(user.GetProperties().address) + "\"/>";
99 if (user.GetProperties().email.ModificationTime() > lastTime)
100 answer += "<Email value=\"" + Encode12str(user.GetProperties().email) + "\"/>";
102 std::vector<const STG::UserPropertyLogged<std::string> *> userdata;
103 userdata.push_back(user.GetProperties().userdata0.GetPointer());
104 userdata.push_back(user.GetProperties().userdata1.GetPointer());
105 userdata.push_back(user.GetProperties().userdata2.GetPointer());
106 userdata.push_back(user.GetProperties().userdata3.GetPointer());
107 userdata.push_back(user.GetProperties().userdata4.GetPointer());
108 userdata.push_back(user.GetProperties().userdata5.GetPointer());
109 userdata.push_back(user.GetProperties().userdata6.GetPointer());
110 userdata.push_back(user.GetProperties().userdata7.GetPointer());
111 userdata.push_back(user.GetProperties().userdata8.GetPointer());
112 userdata.push_back(user.GetProperties().userdata9.GetPointer());
114 for (size_t i = 0; i < userdata.size(); i++)
115 if (userdata[i]->ModificationTime() > lastTime)
116 answer += "<UserData" + std::to_string(i) + " value=\"" + Encode12str(userdata[i]->Get()) + "\" />";
118 if (user.GetProperties().realName.ModificationTime() > lastTime)
119 answer += "<Name value=\"" + Encode12str(user.GetProperties().realName) + "\"/>";
120 if (user.GetProperties().group.ModificationTime() > lastTime)
121 answer += "<Group value=\"" + Encode12str(user.GetProperties().group) + "\"/>";
122 if (user.GetConnectedModificationTime() > lastTime)
123 answer += std::string("<Status value=\"") + (user.GetConnected() ? "1" : "0") + "\"/>";
124 if (user.GetProperties().alwaysOnline.ModificationTime() > lastTime)
125 answer += std::string("<AOnline value=\"") + (user.GetProperties().alwaysOnline.Get() ? "1" : "0") + "\"/>";
126 if (user.GetCurrIPModificationTime() > lastTime)
127 answer += "<CurrIP value=\"" + inet_ntostring(user.GetCurrIP()) + "\"/>";
128 if (user.GetPingTime() > lastTime)
129 answer += "<PingTime value=\"" + std::to_string(user.GetPingTime()) + "\"/>";
130 if (user.GetProperties().ips.ModificationTime() > lastTime)
131 answer += "<IP value=\"" + user.GetProperties().ips.Get().toString() + "\"/>";
134 const auto & upload(user.GetProperties().up.Get());
135 const auto & download(user.GetProperties().down.Get());
136 if (user.GetProperties().up.ModificationTime() > lastTime)
137 for (size_t j = 0; j < DIR_NUM; j++)
138 answer += " MU" + std::to_string(j) + "=\"" + std::to_string(upload[j]) + "\"";
139 if (user.GetProperties().down.ModificationTime() > lastTime)
140 for (size_t j = 0; j < DIR_NUM; j++)
141 answer += " MD" + std::to_string(j) + "=\"" + std::to_string(download[j]) + "\"";
142 if (user.GetSessionUploadModificationTime() > lastTime)
143 for (size_t j = 0; j < DIR_NUM; j++)
144 answer += " SU" + std::to_string(j) + "=\"" + std::to_string(user.GetSessionUpload()[j]) + "\"";
145 if (user.GetSessionDownloadModificationTime() > lastTime)
146 for (size_t j = 0; j < DIR_NUM; j++)
147 answer += " SD" + std::to_string(j) + "=\"" + std::to_string(user.GetSessionDownload()[j]) + "\"";
150 if (user.GetProperties().disabled.ModificationTime() > lastTime)
151 answer += std::string("<Down value=\"") + (user.GetProperties().disabled.Get() ? "1" : "0") + "\"/>";
152 if (user.GetProperties().disabledDetailStat.ModificationTime() > lastTime)
153 answer += std::string("<DisableDetailStat value=\"") + (user.GetProperties().disabledDetailStat.Get() ? "1" : "0") + "\"/>";
154 if (user.GetProperties().passive.ModificationTime() > lastTime)
155 answer += std::string("<Passive value=\"") + (user.GetProperties().passive.Get() ? "1" : "0") + "\"/>";
156 if (user.GetProperties().lastCashAdd.ModificationTime() > lastTime)
157 answer += "<LastCash value=\"" + std::to_string(user.GetProperties().lastCashAdd.Get()) + "\"/>";
158 if (user.GetProperties().lastCashAddTime.ModificationTime() > lastTime)
159 answer += "<LastTimeCash value=\"" + std::to_string(user.GetProperties().lastCashAddTime.Get()) + "\"/>";
160 if (user.GetProperties().lastActivityTime.ModificationTime() > lastTime)
161 answer += "<LastActivityTime value=\"" + std::to_string(user.GetProperties().lastActivityTime.Get()) + "\"/>";
162 if (user.GetProperties().creditExpire.ModificationTime() > lastTime)
163 answer += "<CreditExpire value=\"" + std::to_string(user.GetProperties().creditExpire.Get()) + "\"/>";
167 answer += "<AuthorizedBy>";
168 std::vector<std::string> list(user.GetAuthorizers());
169 for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
170 answer += "<Auth name=\"" + *it + "\"/>";
171 answer += "</AuthorizedBy>";
179 } // namespace anonymous
181 int GET_USERS::Start(void *, const char * el, const char ** attr)
183 if (strcasecmp(el, m_tag.c_str()) != 0)
185 printfd(__FILE__, "Got wrong tag: '%s' instead of '%s'\n", el, m_tag.c_str());
189 while (attr && *attr && *(attr + 1))
191 if (strcasecmp(*attr, "LastUpdate") == 0)
192 str2x(*(attr + 1), m_lastUserUpdateTime);
199 void GET_USERS::CreateAnswer()
201 int h = m_users.OpenSearch();
204 if (m_lastUserUpdateTime > 0)
205 m_answer = "<Users LastUpdate=\"" + std::to_string(time(NULL)) + "\">";
207 m_answer = "<Users>";
211 while (m_users.SearchNext(h, &u) == 0)
212 m_answer += UserToXML(*u, true, m_currAdmin.priv().userConf || m_currAdmin.priv().userPasswd, m_lastUserUpdateTime);
214 m_users.CloseSearch(h);
216 m_answer += "</Users>";
219 int GET_USER::Start(void *, const char * el, const char ** attr)
221 if (strcasecmp(el, m_tag.c_str()) != 0)
231 void GET_USER::CreateAnswer()
235 if (m_users.FindByName(m_login, &u))
236 m_answer = "<User result=\"error\" reason=\"User not found.\"/>";
238 m_answer = UserToXML(*u, false, m_currAdmin.priv().userConf || m_currAdmin.priv().userPasswd);
241 int ADD_USER::Start(void *, const char * el, const char ** attr)
247 if (strcasecmp(el, m_tag.c_str()) == 0)
252 if (strcasecmp(el, "login") == 0)
261 void ADD_USER::CreateAnswer()
263 if (m_users.Exists(m_login))
264 m_answer = "<" + m_tag + " result=\"error\" reason=\"User '" + m_login + "' exists.\"/>";
265 else if (m_users.Add(m_login, &m_currAdmin) == 0)
266 m_answer = "<" + m_tag + " result=\"ok\"/>";
268 m_answer = "<" + m_tag + " result=\"error\" reason=\"Access denied\"/>";
271 int CHG_USER::Start(void *, const char * el, const char ** attr)
277 if (strcasecmp(el, m_tag.c_str()) == 0)
282 if (strcasecmp(el, "login") == 0)
288 if (strcasecmp(el, "ip") == 0)
290 m_ucr.ips = UserIPs::parse(attr[1]);
294 if (strcasecmp(el, "password") == 0)
296 m_ucr.password = attr[1];
300 if (strcasecmp(el, "address") == 0)
302 m_ucr.address = Decode21str(attr[1]);
306 if (strcasecmp(el, "aonline") == 0)
308 m_ucr.alwaysOnline = (*(attr[1]) != '0');
312 if (strcasecmp(el, "cash") == 0)
314 if (attr[2] && (strcasecmp(attr[2], "msg") == 0))
315 m_cashMsg = Decode21str(attr[3]);
318 if (strtodouble2(attr[1], cash) == 0)
321 m_cashMustBeAdded = (strcasecmp(attr[0], "add") == 0);
326 if (strcasecmp(el, "CreditExpire") == 0)
328 long int creditExpire = 0;
329 if (str2x(attr[1], creditExpire) == 0)
330 m_ucr.creditExpire = creditExpire;
335 if (strcasecmp(el, "credit") == 0)
338 if (strtodouble2(attr[1], credit) == 0)
339 m_ucr.credit = credit;
343 if (strcasecmp(el, "freemb") == 0)
346 if (strtodouble2(attr[1], freeMb) == 0)
347 m_usr.freeMb = freeMb;
351 if (strcasecmp(el, "down") == 0)
354 if (str2x(attr[1], down) == 0)
355 m_ucr.disabled = down;
359 if (strcasecmp(el, "DisableDetailStat") == 0)
361 int disabledDetailStat = 0;
362 if (str2x(attr[1], disabledDetailStat) == 0)
363 m_ucr.disabledDetailStat = disabledDetailStat;
367 if (strcasecmp(el, "email") == 0)
369 m_ucr.email = Decode21str(attr[1]);
373 for (int i = 0; i < USERDATA_NUM; i++)
376 sprintf(name, "userdata%d", i);
377 if (strcasecmp(el, name) == 0)
379 m_ucr.userdata[i] = Decode21str(attr[1]);
384 if (strcasecmp(el, "group") == 0)
386 m_ucr.group = Decode21str(attr[1]);
390 if (strcasecmp(el, "note") == 0)
392 m_ucr.note = Decode21str(attr[1]);
396 if (strcasecmp(el, "passive") == 0)
399 if (str2x(attr[1], passive) == 0)
400 m_ucr.passive = passive;
404 if (strcasecmp(el, "phone") == 0)
406 m_ucr.phone = Decode21str(attr[1]);
410 if (strcasecmp(el, "Name") == 0)
412 m_ucr.realName = Decode21str(attr[1]);
416 if (strcasecmp(el, "traff") == 0)
421 int dir = attr[j][2] - '0';
423 if (strncasecmp(attr[j], "md", 2) == 0)
426 str2x(attr[j + 1], t);
429 if (strncasecmp(attr[j], "mu", 2) == 0)
432 str2x(attr[j + 1], t);
440 if (strcasecmp(el, "tariff") == 0)
442 if (strcasecmp(attr[0], "now") == 0)
443 m_ucr.tariffName = attr[1];
445 if (strcasecmp(attr[0], "delayed") == 0)
446 m_ucr.nextTariff = attr[1];
454 void CHG_USER::CreateAnswer()
456 if (ApplyChanges() == 0)
457 m_answer = "<" + m_tag + " result=\"ok\"/>";
459 m_answer = "<" + m_tag + " result=\"error\"/>";
462 int CHG_USER::ApplyChanges()
464 printfd(__FILE__, "PARSER_CHG_USER::ApplyChanges()\n");
467 if (m_users.FindByName(m_login, &u))
471 bool alwaysOnline = u->GetProperties().alwaysOnline;
472 if (m_ucr.alwaysOnline)
475 alwaysOnline = m_ucr.alwaysOnline.value();
477 bool onlyOneIP = u->GetProperties().ips.ConstData().onlyOneIP();
481 onlyOneIP = m_ucr.ips.value().onlyOneIP();
484 if (check && alwaysOnline && !onlyOneIP)
486 printfd(__FILE__, "Requested change leads to a forbidden state: AlwaysOnline with multiple IP's\n");
487 PluginLogger::get("conf_sg")("%s Requested change leads to a forbidden state: AlwaysOnline with multiple IP's", m_currAdmin.logStr().c_str());
491 for (size_t i = 0; i < m_ucr.ips.value().count(); ++i)
494 uint32_t ip = m_ucr.ips.value().operator[](i).ip;
495 if (m_users.IsIPInUse(ip, m_login, &user))
497 printfd(__FILE__, "Trying to assign an IP %s to '%s' that is already in use by '%s'\n", inet_ntostring(ip).c_str(), m_login.c_str(), user->GetLogin().c_str());
498 PluginLogger::get("conf_sg")("%s trying to assign an IP %s to '%s' that is currently in use by '%s'", m_currAdmin.logStr().c_str(), inet_ntostring(ip).c_str(), m_login.c_str(), user->GetLogin().c_str());
504 if (!u->GetProperties().ips.Set(m_ucr.ips.value(), m_currAdmin, m_login, m_store))
507 if (m_ucr.alwaysOnline)
508 if (!u->GetProperties().alwaysOnline.Set(m_ucr.alwaysOnline.value(),
509 m_currAdmin, m_login, m_store))
513 if (!u->GetProperties().address.Set(m_ucr.address.value(), m_currAdmin, m_login, m_store))
516 if (m_ucr.creditExpire)
517 if (!u->GetProperties().creditExpire.Set(m_ucr.creditExpire.value(),
518 m_currAdmin, m_login, m_store))
522 if (!u->GetProperties().credit.Set(m_ucr.credit.value(), m_currAdmin, m_login, m_store))
526 if (!u->GetProperties().freeMb.Set(m_usr.freeMb.value(), m_currAdmin, m_login, m_store))
530 if (!u->GetProperties().disabled.Set(m_ucr.disabled.value(), m_currAdmin, m_login, m_store))
533 if (m_ucr.disabledDetailStat)
534 if (!u->GetProperties().disabledDetailStat.Set(m_ucr.disabledDetailStat.value(), m_currAdmin, m_login, m_store))
538 if (!u->GetProperties().email.Set(m_ucr.email.value(), m_currAdmin, m_login, m_store))
542 if (!u->GetProperties().group.Set(m_ucr.group.value(), m_currAdmin, m_login, m_store))
546 if (!u->GetProperties().note.Set(m_ucr.note.value(), m_currAdmin, m_login, m_store))
549 std::vector<STG::UserPropertyLogged<std::string> *> userdata;
550 userdata.push_back(u->GetProperties().userdata0.GetPointer());
551 userdata.push_back(u->GetProperties().userdata1.GetPointer());
552 userdata.push_back(u->GetProperties().userdata2.GetPointer());
553 userdata.push_back(u->GetProperties().userdata3.GetPointer());
554 userdata.push_back(u->GetProperties().userdata4.GetPointer());
555 userdata.push_back(u->GetProperties().userdata5.GetPointer());
556 userdata.push_back(u->GetProperties().userdata6.GetPointer());
557 userdata.push_back(u->GetProperties().userdata7.GetPointer());
558 userdata.push_back(u->GetProperties().userdata8.GetPointer());
559 userdata.push_back(u->GetProperties().userdata9.GetPointer());
561 for (size_t i = 0; i < userdata.size(); i++)
562 if (m_ucr.userdata[i])
563 if(!userdata[i]->Set(m_ucr.userdata[i].value(), m_currAdmin, m_login, m_store))
567 if (!u->GetProperties().passive.Set(m_ucr.passive.value(), m_currAdmin, m_login, m_store))
571 if (!u->GetProperties().password.Set(m_ucr.password.value(), m_currAdmin, m_login, m_store))
575 if (!u->GetProperties().phone.Set(m_ucr.phone.value(), m_currAdmin, m_login, m_store))
579 if (!u->GetProperties().realName.Set(m_ucr.realName.value(), m_currAdmin, m_login, m_store))
584 if (m_cashMustBeAdded)
586 if (!u->GetProperties().cash.Set(m_usr.cash.value() + u->GetProperties().cash,
595 if (!u->GetProperties().cash.Set(m_usr.cash.value(), m_currAdmin, m_login, m_store, m_cashMsg))
600 if (m_ucr.tariffName)
602 const auto newTariff = m_tariffs.FindByName(m_ucr.tariffName.value());
605 const auto tariff = u->GetTariff();
606 std::string message = tariff->TariffChangeIsAllowed(*newTariff, time(NULL));
609 if (!u->GetProperties().tariffName.Set(m_ucr.tariffName.value(), m_currAdmin, m_login, m_store))
611 u->ResetNextTariff();
615 PluginLogger::get("conf_sg")("Tariff change is prohibited for user %s. %s", u->GetLogin().c_str(), message.c_str());
620 //WriteServLog("SetUser: Tariff %s not found", ud.conf.tariffName.c_str());
625 if (m_ucr.nextTariff)
627 if (m_tariffs.FindByName(m_ucr.nextTariff.value()))
629 if (!u->GetProperties().nextTariff.Set(m_ucr.nextTariff.value(), m_currAdmin, m_login, m_store))
634 //WriteServLog("SetUser: Tariff %s not found", ud.conf.tariffName.c_str());
639 auto up = u->GetProperties().up.get();
640 auto down = u->GetProperties().down.get();
643 for (int i = 0; i < DIR_NUM; i++)
647 up[i] = m_upr[i].value();
652 down[i] = m_downr[i].value();
658 if (!u->GetProperties().up.Set(up, m_currAdmin, m_login, m_store))
662 if (!u->GetProperties().down.Set(down, m_currAdmin, m_login, m_store))
671 int DEL_USER::Start(void *, const char *el, const char **attr)
674 if (strcasecmp(el, m_tag.c_str()) == 0)
676 if (attr[0] == NULL || attr[1] == NULL)
678 //CreateAnswer("Parameters error!");
683 if (m_users.FindByName(attr[1], &u))
695 int DEL_USER::End(void *, const char *el)
697 if (strcasecmp(el, m_tag.c_str()) == 0)
700 m_users.Del(u->GetLogin(), &m_currAdmin);
707 void DEL_USER::CreateAnswer()
710 m_answer = "<" + m_tag + " value=\"error\" reason=\"User not found\"/>";
712 m_answer = "<" + m_tag + " value=\"ok\"/>";
715 int CHECK_USER::Start(void *, const char *el, const char **attr)
717 if (strcasecmp(el, m_tag.c_str()) == 0)
719 if (attr[0] == NULL || attr[1] == NULL ||
720 attr[2] == NULL || attr[3] == NULL)
722 CreateAnswer("Invalid parameters.");
723 printfd(__FILE__, "PARSER_CHECK_USER - attr err\n");
728 if (m_users.FindByName(attr[1], &user))
730 CreateAnswer("User not found.");
731 printfd(__FILE__, "PARSER_CHECK_USER - login err\n");
735 if (strcmp(user->GetProperties().password.Get().c_str(), attr[3]))
737 CreateAnswer("Wrong password.");
738 printfd(__FILE__, "PARSER_CHECK_USER - passwd err\n");
748 int CHECK_USER::End(void *, const char *el)
750 if (strcasecmp(el, m_tag.c_str()) == 0)
755 void CHECK_USER::CreateAnswer(const char * error)
758 m_answer = "<" + m_tag + " value=\"Err\" reason=\"" + error + "\"/>";
760 m_answer = "<" + m_tag + " value=\"Ok\"/>";