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"
25 #include "stg/tariffs.h"
26 #include "stg/user_property.h"
27 #include "stg/user_conf.h"
28 #include "stg/user_stat.h"
29 #include "stg/common.h"
34 using STG::PARSER::GET_USERS;
35 using STG::PARSER::GET_USER;
36 using STG::PARSER::ADD_USER;
37 using STG::PARSER::DEL_USER;
38 using STG::PARSER::CHG_USER;
39 using STG::PARSER::CHECK_USER;
41 const char * GET_USERS::tag = "GetUsers";
42 const char * GET_USER::tag = "GetUser";
43 const char * ADD_USER::tag = "AddUser";
44 const char * CHG_USER::tag = "SetUser";
45 const char * DEL_USER::tag = "DelUser";
46 const char * CHECK_USER::tag = "CheckUser";
51 std::string UserToXML(const USER & user, bool loginInStart, bool showPass, time_t lastTime = 0)
56 answer += "<User result=\"ok\">";
58 answer += "<User result=\"ok\" login=\"" + user.GetLogin() + "\">";
60 answer += "<Login value=\"" + user.GetLogin() + "\"/>";
62 if (user.GetProperty().password.ModificationTime() > lastTime)
65 answer += "<Password value=\"" + user.GetProperty().password.Get() + "\" />";
67 answer += "<Password value=\"++++++\"/>";
70 if (user.GetProperty().cash.ModificationTime() > lastTime)
71 answer += "<Cash value=\"" + x2str(user.GetProperty().cash.Get()) + "\"/>";
72 if (user.GetProperty().freeMb.ModificationTime() > lastTime)
73 answer += "<FreeMb value=\"" + x2str(user.GetProperty().freeMb.Get()) + "\"/>";
74 if (user.GetProperty().credit.ModificationTime() > lastTime)
75 answer += "<Credit value=\"" + x2str(user.GetProperty().credit.Get()) + "\"/>";
77 if (user.GetProperty().nextTariff.Get() != "")
79 if (user.GetProperty().tariffName.ModificationTime() > lastTime ||
80 user.GetProperty().nextTariff.ModificationTime() > lastTime)
81 answer += "<Tariff value=\"" + user.GetProperty().tariffName.Get() + "/" + user.GetProperty().nextTariff.Get() + "\"/>";
85 if (user.GetProperty().tariffName.ModificationTime() > lastTime)
86 answer += "<Tariff value=\"" + user.GetProperty().tariffName.Get() + "\"/>";
89 if (user.GetProperty().note.ModificationTime() > lastTime)
90 answer += "<Note value=\"" + Encode12str(user.GetProperty().note) + "\"/>";
91 if (user.GetProperty().phone.ModificationTime() > lastTime)
92 answer += "<Phone value=\"" + Encode12str(user.GetProperty().phone) + "\"/>";
93 if (user.GetProperty().address.ModificationTime() > lastTime)
94 answer += "<Address value=\"" + Encode12str(user.GetProperty().address) + "\"/>";
95 if (user.GetProperty().email.ModificationTime() > lastTime)
96 answer += "<Email value=\"" + Encode12str(user.GetProperty().email) + "\"/>";
98 std::vector<const USER_PROPERTY_LOGGED<std::string> *> userdata;
99 userdata.push_back(user.GetProperty().userdata0.GetPointer());
100 userdata.push_back(user.GetProperty().userdata1.GetPointer());
101 userdata.push_back(user.GetProperty().userdata2.GetPointer());
102 userdata.push_back(user.GetProperty().userdata3.GetPointer());
103 userdata.push_back(user.GetProperty().userdata4.GetPointer());
104 userdata.push_back(user.GetProperty().userdata5.GetPointer());
105 userdata.push_back(user.GetProperty().userdata6.GetPointer());
106 userdata.push_back(user.GetProperty().userdata7.GetPointer());
107 userdata.push_back(user.GetProperty().userdata8.GetPointer());
108 userdata.push_back(user.GetProperty().userdata9.GetPointer());
110 for (size_t i = 0; i < userdata.size(); i++)
111 if (userdata[i]->ModificationTime() > lastTime)
112 answer += "<UserData" + x2str(i) + " value=\"" + Encode12str(userdata[i]->Get()) + "\" />";
114 if (user.GetProperty().realName.ModificationTime() > lastTime)
115 answer += "<Name value=\"" + Encode12str(user.GetProperty().realName) + "\"/>";
116 if (user.GetProperty().group.ModificationTime() > lastTime)
117 answer += "<Group value=\"" + Encode12str(user.GetProperty().group) + "\"/>";
118 if (user.GetConnectedModificationTime() > lastTime)
119 answer += std::string("<Status value=\"") + (user.GetConnected() ? "1" : "0") + "\"/>";
120 if (user.GetProperty().alwaysOnline.ModificationTime() > lastTime)
121 answer += std::string("<AOnline value=\"") + (user.GetProperty().alwaysOnline.Get() ? "1" : "0") + "\"/>";
122 if (user.GetCurrIPModificationTime() > lastTime)
123 answer += "<CurrIP value=\"" + inet_ntostring(user.GetCurrIP()) + "\"/>";
124 if (user.GetPingTime() > lastTime)
125 answer += "<PingTime value=\"" + x2str(user.GetPingTime()) + "\"/>";
126 if (user.GetProperty().ips.ModificationTime() > lastTime)
127 answer += "<IP value=\"" + user.GetProperty().ips.Get().GetIpStr() + "\"/>";
130 const DIR_TRAFF & upload(user.GetProperty().down.Get());
131 const DIR_TRAFF & download(user.GetProperty().up.Get());
132 if (user.GetProperty().up.ModificationTime() > lastTime)
133 for (size_t j = 0; j < DIR_NUM; j++)
134 answer += " MU" + x2str(j) + "=\"" + x2str(upload[j]) + "\"";
135 if (user.GetProperty().down.ModificationTime() > lastTime)
136 for (size_t j = 0; j < DIR_NUM; j++)
137 answer += " MD" + x2str(j) + "=\"" + x2str(download[j]) + "\"";
138 if (user.GetSessionUploadModificationTime() > lastTime)
139 for (size_t j = 0; j < DIR_NUM; j++)
140 answer += " SU" + x2str(j) + "=\"" + x2str(user.GetSessionUpload()[j]) + "\"";
141 if (user.GetSessionDownloadModificationTime() > lastTime)
142 for (size_t j = 0; j < DIR_NUM; j++)
143 answer += " SD" + x2str(j) + "=\"" + x2str(user.GetSessionDownload()[j]) + "\"";
146 if (user.GetProperty().disabled.ModificationTime() > lastTime)
147 answer += std::string("<Down value=\"") + (user.GetProperty().disabled.Get() ? "1" : "0") + "\"/>";
148 if (user.GetProperty().disabledDetailStat.ModificationTime() > lastTime)
149 answer += std::string("<DisableDetailStat value=\"") + (user.GetProperty().disabledDetailStat.Get() ? "1" : "0") + "\"/>";
150 if (user.GetProperty().passive.ModificationTime() > lastTime)
151 answer += std::string("<Passive value=\"") + (user.GetProperty().passive.Get() ? "1" : "0") + "\"/>";
152 if (user.GetProperty().lastCashAdd.ModificationTime() > lastTime)
153 answer += "<LastCash value=\"" + x2str(user.GetProperty().lastCashAdd.Get()) + "\"/>";
154 if (user.GetProperty().lastCashAddTime.ModificationTime() > lastTime)
155 answer += "<LastTimeCash value=\"" + x2str(user.GetProperty().lastCashAddTime.Get()) + "\"/>";
156 if (user.GetProperty().lastActivityTime.ModificationTime() > lastTime)
157 answer += "<LastActivityTime value=\"" + x2str(user.GetProperty().lastActivityTime.Get()) + "\"/>";
158 if (user.GetProperty().creditExpire.ModificationTime() > lastTime)
159 answer += "<CreditExpire value=\"" + x2str(user.GetProperty().creditExpire.Get()) + "\"/>";
163 answer += "<AuthorizedBy>";
164 std::vector<std::string> list(user.GetAuthorizers());
165 for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
166 answer += "<Auth name=\"" + *it + "\"/>";
167 answer += "</AuthorizedBy>";
175 } // namespace anonymous
177 int GET_USERS::Start(void *, const char * el, const char ** attr)
179 if (strcasecmp(el, m_tag.c_str()) != 0)
182 while (attr && *attr && *(attr + 1))
184 if (strcasecmp(*attr, "LastUpdate") == 0)
185 str2x(*(attr + 1), m_lastUserUpdateTime);
192 void GET_USERS::CreateAnswer()
194 int h = m_users.OpenSearch();
197 if (m_lastUserUpdateTime > 0)
198 m_answer = "<" + m_tag + " LastUpdate=\"" + x2str(time(NULL)) + "\">";
200 m_answer = GetOpenTag();
204 while (m_users.SearchNext(h, &u) == 0)
205 m_answer += UserToXML(*u, true, m_currAdmin.GetPriv()->userConf || m_currAdmin.GetPriv()->userPasswd, m_lastUserUpdateTime);
207 m_users.CloseSearch(h);
209 m_answer += GetCloseTag();
212 int GET_USER::Start(void *, const char * el, const char ** attr)
214 if (strcasecmp(el, m_tag.c_str()) != 0)
224 void GET_USER::CreateAnswer()
228 if (m_users.FindByName(m_login, &u))
229 m_answer = "<" + m_tag + " result=\"error\" reason=\"User not found.\"/>";
231 m_answer = UserToXML(*u, false, m_currAdmin.GetPriv()->userConf || m_currAdmin.GetPriv()->userPasswd);
234 int ADD_USER::Start(void *, const char * el, const char ** attr)
240 if (strcasecmp(el, m_tag.c_str()) == 0)
245 if (strcasecmp(el, "login") == 0)
254 void ADD_USER::CreateAnswer()
256 if (m_users.Exists(m_login))
257 m_answer = "<" + m_tag + " result=\"error\" reason=\"User '" + m_login + "' exists.\"/>";
258 else if (m_users.Add(m_login, &m_currAdmin) == 0)
259 m_answer = "<" + m_tag + " result=\"ok\"/>";
261 m_answer = "<" + m_tag + " result=\"error\" reason=\"Access denied\"/>";
264 int CHG_USER::Start(void *, const char * el, const char ** attr)
270 if (strcasecmp(el, m_tag.c_str()) == 0)
275 if (strcasecmp(el, "login") == 0)
281 if (strcasecmp(el, "ip") == 0)
283 m_ucr.ips = StrToIPS(attr[1]);
287 if (strcasecmp(el, "password") == 0)
289 m_ucr.password = attr[1];
293 if (strcasecmp(el, "address") == 0)
295 m_ucr.address = Decode21str(attr[1]);
299 if (strcasecmp(el, "aonline") == 0)
301 m_ucr.alwaysOnline = (*(attr[1]) != '0');
305 if (strcasecmp(el, "cash") == 0)
307 if (attr[2] && (strcasecmp(attr[2], "msg") == 0))
308 m_cashMsg = Decode21str(attr[3]);
311 if (strtodouble2(attr[1], cash) == 0)
314 m_cashMustBeAdded = (strcasecmp(attr[0], "add") == 0);
319 if (strcasecmp(el, "CreditExpire") == 0)
321 long int creditExpire = 0;
322 if (str2x(attr[1], creditExpire) == 0)
323 m_ucr.creditExpire = (time_t)creditExpire;
328 if (strcasecmp(el, "credit") == 0)
331 if (strtodouble2(attr[1], credit) == 0)
332 m_ucr.credit = credit;
336 if (strcasecmp(el, "freemb") == 0)
339 if (strtodouble2(attr[1], freeMb) == 0)
340 m_usr.freeMb = freeMb;
344 if (strcasecmp(el, "down") == 0)
347 if (str2x(attr[1], down) == 0)
348 m_ucr.disabled = down;
352 if (strcasecmp(el, "DisableDetailStat") == 0)
354 int disabledDetailStat = 0;
355 if (str2x(attr[1], disabledDetailStat) == 0)
356 m_ucr.disabledDetailStat = disabledDetailStat;
360 if (strcasecmp(el, "email") == 0)
362 m_ucr.email = Decode21str(attr[1]);
366 for (int i = 0; i < USERDATA_NUM; i++)
369 sprintf(name, "userdata%d", i);
370 if (strcasecmp(el, name) == 0)
372 m_ucr.userdata[i] = Decode21str(attr[1]);
377 if (strcasecmp(el, "group") == 0)
379 m_ucr.group = Decode21str(attr[1]);
383 if (strcasecmp(el, "note") == 0)
385 m_ucr.note = Decode21str(attr[1]);
389 if (strcasecmp(el, "passive") == 0)
392 if (str2x(attr[1], passive) == 0)
393 m_ucr.passive = passive;
397 if (strcasecmp(el, "phone") == 0)
399 m_ucr.phone = Decode21str(attr[1]);
403 if (strcasecmp(el, "Name") == 0)
405 m_ucr.realName = Decode21str(attr[1]);
409 if (strcasecmp(el, "traff") == 0)
414 int dir = attr[j][2] - '0';
416 if (strncasecmp(attr[j], "md", 2) == 0)
419 str2x(attr[j + 1], t);
422 if (strncasecmp(attr[j], "mu", 2) == 0)
425 str2x(attr[j + 1], t);
433 if (strcasecmp(el, "tariff") == 0)
435 if (strcasecmp(attr[0], "now") == 0)
436 m_ucr.tariffName = attr[1];
438 if (strcasecmp(attr[0], "delayed") == 0)
439 m_ucr.nextTariff = attr[1];
447 void CHG_USER::CreateAnswer()
449 if (ApplyChanges() == 0)
450 m_answer = "<" + m_tag + " result=\"ok\"/>";
452 m_answer = "<" + m_tag + " result=\"error\"/>";
455 int CHG_USER::ApplyChanges()
457 printfd(__FILE__, "PARSER_CHG_USER::ApplyChanges()\n");
460 if (m_users.FindByName(m_login, &u))
464 bool alwaysOnline = u->GetProperty().alwaysOnline;
465 if (!m_ucr.alwaysOnline.empty())
468 alwaysOnline = m_ucr.alwaysOnline.const_data();
470 bool onlyOneIP = u->GetProperty().ips.ConstData().OnlyOneIP();
471 if (!m_ucr.ips.empty())
474 onlyOneIP = m_ucr.ips.const_data().OnlyOneIP();
477 if (check && alwaysOnline && !onlyOneIP)
479 printfd(__FILE__, "Requested change leads to a forbidden state: AlwaysOnline with multiple IP's\n");
480 GetStgLogger()("%s Requested change leads to a forbidden state: AlwaysOnline with multiple IP's", m_currAdmin.GetLogStr().c_str());
484 for (size_t i = 0; i < m_ucr.ips.const_data().Count(); ++i)
487 uint32_t ip = m_ucr.ips.const_data().operator[](i).ip;
488 if (m_users.IsIPInUse(ip, m_login, &user))
490 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());
491 GetStgLogger()("%s trying to assign an IP %s to '%s' that is currently in use by '%s'", m_currAdmin.GetLogStr().c_str(), inet_ntostring(ip).c_str(), m_login.c_str(), user->GetLogin().c_str());
496 if (!m_ucr.ips.empty())
497 if (!u->GetProperty().ips.Set(m_ucr.ips.const_data(), &m_currAdmin, m_login, &m_store))
500 if (!m_ucr.alwaysOnline.empty())
501 if (!u->GetProperty().alwaysOnline.Set(m_ucr.alwaysOnline.const_data(),
502 &m_currAdmin, m_login, &m_store))
505 if (!m_ucr.address.empty())
506 if (!u->GetProperty().address.Set(m_ucr.address.const_data(), &m_currAdmin, m_login, &m_store))
509 if (!m_ucr.creditExpire.empty())
510 if (!u->GetProperty().creditExpire.Set(m_ucr.creditExpire.const_data(),
511 &m_currAdmin, m_login, &m_store))
514 if (!m_ucr.credit.empty())
515 if (!u->GetProperty().credit.Set(m_ucr.credit.const_data(), &m_currAdmin, m_login, &m_store))
518 if (!m_usr.freeMb.empty())
519 if (!u->GetProperty().freeMb.Set(m_usr.freeMb.const_data(), &m_currAdmin, m_login, &m_store))
522 if (!m_ucr.disabled.empty())
523 if (!u->GetProperty().disabled.Set(m_ucr.disabled.const_data(), &m_currAdmin, m_login, &m_store))
526 if (!m_ucr.disabledDetailStat.empty())
527 if (!u->GetProperty().disabledDetailStat.Set(m_ucr.disabledDetailStat.const_data(), &m_currAdmin, m_login, &m_store))
530 if (!m_ucr.email.empty())
531 if (!u->GetProperty().email.Set(m_ucr.email.const_data(), &m_currAdmin, m_login, &m_store))
534 if (!m_ucr.group.empty())
535 if (!u->GetProperty().group.Set(m_ucr.group.const_data(), &m_currAdmin, m_login, &m_store))
538 if (!m_ucr.note.empty())
539 if (!u->GetProperty().note.Set(m_ucr.note.const_data(), &m_currAdmin, m_login, &m_store))
542 std::vector<USER_PROPERTY_LOGGED<std::string> *> userdata;
543 userdata.push_back(u->GetProperty().userdata0.GetPointer());
544 userdata.push_back(u->GetProperty().userdata1.GetPointer());
545 userdata.push_back(u->GetProperty().userdata2.GetPointer());
546 userdata.push_back(u->GetProperty().userdata3.GetPointer());
547 userdata.push_back(u->GetProperty().userdata4.GetPointer());
548 userdata.push_back(u->GetProperty().userdata5.GetPointer());
549 userdata.push_back(u->GetProperty().userdata6.GetPointer());
550 userdata.push_back(u->GetProperty().userdata7.GetPointer());
551 userdata.push_back(u->GetProperty().userdata8.GetPointer());
552 userdata.push_back(u->GetProperty().userdata9.GetPointer());
554 for (int i = 0; i < (int)userdata.size(); i++)
555 if (!m_ucr.userdata[i].empty())
556 if(!userdata[i]->Set(m_ucr.userdata[i].const_data(), &m_currAdmin, m_login, &m_store))
559 if (!m_ucr.passive.empty())
560 if (!u->GetProperty().passive.Set(m_ucr.passive.const_data(), &m_currAdmin, m_login, &m_store))
563 if (!m_ucr.password.empty())
564 if (!u->GetProperty().password.Set(m_ucr.password.const_data(), &m_currAdmin, m_login, &m_store))
567 if (!m_ucr.phone.empty())
568 if (!u->GetProperty().phone.Set(m_ucr.phone.const_data(), &m_currAdmin, m_login, &m_store))
571 if (!m_ucr.realName.empty())
572 if (!u->GetProperty().realName.Set(m_ucr.realName.const_data(), &m_currAdmin, m_login, &m_store))
576 if (!m_usr.cash.empty())
577 if (m_cashMustBeAdded)
579 if (!u->GetProperty().cash.Set(m_usr.cash.const_data() + u->GetProperty().cash,
586 if (!u->GetProperty().cash.Set(m_usr.cash.const_data(), &m_currAdmin, m_login, &m_store, m_cashMsg))
591 if (!m_ucr.tariffName.empty())
593 if (m_tariffs.FindByName(m_ucr.tariffName.const_data()))
595 if (!u->GetProperty().tariffName.Set(m_ucr.tariffName.const_data(), &m_currAdmin, m_login, &m_store))
597 u->ResetNextTariff();
601 //WriteServLog("SetUser: Tariff %s not found", ud.conf.tariffName.c_str());
606 if (!m_ucr.nextTariff.empty())
608 if (m_tariffs.FindByName(m_ucr.nextTariff.const_data()))
610 if (!u->GetProperty().nextTariff.Set(m_ucr.nextTariff.const_data(), &m_currAdmin, m_login, &m_store))
615 //WriteServLog("SetUser: Tariff %s not found", ud.conf.tariffName.c_str());
620 DIR_TRAFF up = u->GetProperty().up;
621 DIR_TRAFF down = u->GetProperty().down;
624 for (int i = 0; i < DIR_NUM; i++)
626 if (!m_upr[i].empty())
628 up[i] = m_upr[i].data();
631 if (!m_downr[i].empty())
633 down[i] = m_downr[i].data();
639 if (!u->GetProperty().up.Set(up, &m_currAdmin, m_login, &m_store))
643 if (!u->GetProperty().down.Set(down, &m_currAdmin, m_login, &m_store))
652 int DEL_USER::Start(void *, const char *el, const char **attr)
655 if (strcasecmp(el, m_tag.c_str()) == 0)
657 if (attr[0] == NULL || attr[1] == NULL)
659 //CreateAnswer("Parameters error!");
664 if (m_users.FindByName(attr[1], &u))
676 int DEL_USER::End(void *, const char *el)
678 if (strcasecmp(el, m_tag.c_str()) == 0)
681 m_users.Del(u->GetLogin(), &m_currAdmin);
688 void DEL_USER::CreateAnswer()
691 m_answer = "<" + m_tag + " value=\"error\" reason=\"User not found\"/>";
693 m_answer = "<" + m_tag + " value=\"ok\"/>";
696 int CHECK_USER::Start(void *, const char *el, const char **attr)
698 if (strcasecmp(el, m_tag.c_str()) == 0)
700 if (attr[0] == NULL || attr[1] == NULL ||
701 attr[2] == NULL || attr[3] == NULL)
703 CreateAnswer("Invalid parameters.");
704 printfd(__FILE__, "PARSER_CHECK_USER - attr err\n");
709 if (m_users.FindByName(attr[1], &user))
711 CreateAnswer("User not found.");
712 printfd(__FILE__, "PARSER_CHECK_USER - login err\n");
716 if (strcmp(user->GetProperty().password.Get().c_str(), attr[3]))
718 CreateAnswer("Wrong password.");
719 printfd(__FILE__, "PARSER_CHECK_USER - passwd err\n");
729 int CHECK_USER::End(void *, const char *el)
731 if (strcasecmp(el, m_tag.c_str()) == 0)
736 void CHECK_USER::CreateAnswer(const char * error)
739 m_answer = "<" + m_tag + " value=\"Err\" reason=\"" + error + "\"/>";
741 m_answer = "<" + m_tag + " value=\"Ok\"/>";