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"
33 using STG::PARSER::GET_USERS;
34 using STG::PARSER::GET_USER;
35 using STG::PARSER::ADD_USER;
36 using STG::PARSER::DEL_USER;
37 using STG::PARSER::CHG_USER;
38 using STG::PARSER::CHECK_USER;
40 const char * GET_USERS::tag = "GetUsers";
41 const char * GET_USER::tag = "GetUser";
42 const char * ADD_USER::tag = "AddUser";
43 const char * CHG_USER::tag = "SetUser";
44 const char * DEL_USER::tag = "DelUser";
45 const char * CHECK_USER::tag = "CheckUser";
50 std::string UserToXML(const USER & user, bool loginInStart, bool showPass, time_t lastTime = 0)
55 answer += "<User login=\"" + user.GetLogin() + "\" result=\"ok\">";
57 answer += "<User result=\"ok\">";
59 answer += "<Login value=\"" + user.GetLogin() + "\"/>";
61 if (user.GetProperty().password.ModificationTime() > lastTime)
64 answer += "<Password value=\"" + user.GetProperty().password.Get() + "\" />";
66 answer += "<Password value=\"++++++\"/>";
69 if (user.GetProperty().cash.ModificationTime() > lastTime)
70 answer += "<Cash value=\"" + x2str(user.GetProperty().cash.Get()) + "\"/>";
71 if (user.GetProperty().freeMb.ModificationTime() > lastTime)
72 answer += "<FreeMb value=\"" + x2str(user.GetProperty().freeMb.Get()) + "\"/>";
73 if (user.GetProperty().credit.ModificationTime() > lastTime)
74 answer += "<Credit value=\"" + x2str(user.GetProperty().credit.Get()) + "\"/>";
76 if (user.GetProperty().nextTariff.Get() != "")
78 if (user.GetProperty().tariffName.ModificationTime() > lastTime ||
79 user.GetProperty().nextTariff.ModificationTime() > lastTime)
80 answer += "<Tariff value=\"" + user.GetProperty().tariffName.Get() + "/" + user.GetProperty().nextTariff.Get() + "\"/>";
84 if (user.GetProperty().tariffName.ModificationTime() > lastTime)
85 answer += "<Tariff value=\"" + user.GetProperty().tariffName.Get() + "\"/>";
88 if (user.GetProperty().note.ModificationTime() > lastTime)
89 answer += "<Note value=\"" + Encode12str(user.GetProperty().note) + "\"/>";
90 if (user.GetProperty().phone.ModificationTime() > lastTime)
91 answer += "<Phone value=\"" + Encode12str(user.GetProperty().phone) + "\"/>";
92 if (user.GetProperty().address.ModificationTime() > lastTime)
93 answer += "<Address value=\"" + Encode12str(user.GetProperty().address) + "\"/>";
94 if (user.GetProperty().email.ModificationTime() > lastTime)
95 answer += "<Email value=\"" + Encode12str(user.GetProperty().email) + "\"/>";
97 std::vector<const USER_PROPERTY_LOGGED<std::string> *> userdata;
98 userdata.push_back(user.GetProperty().userdata0.GetPointer());
99 userdata.push_back(user.GetProperty().userdata1.GetPointer());
100 userdata.push_back(user.GetProperty().userdata2.GetPointer());
101 userdata.push_back(user.GetProperty().userdata3.GetPointer());
102 userdata.push_back(user.GetProperty().userdata4.GetPointer());
103 userdata.push_back(user.GetProperty().userdata5.GetPointer());
104 userdata.push_back(user.GetProperty().userdata6.GetPointer());
105 userdata.push_back(user.GetProperty().userdata7.GetPointer());
106 userdata.push_back(user.GetProperty().userdata8.GetPointer());
107 userdata.push_back(user.GetProperty().userdata9.GetPointer());
109 for (size_t i = 0; i < userdata.size(); i++)
110 if (userdata[i]->ModificationTime() > lastTime)
111 answer += "<UserData" + x2str(i) + " value=\"" + Encode12str(userdata[i]->Get()) + "\" />";
113 if (user.GetProperty().realName.ModificationTime() > lastTime)
114 answer += "<Name value=\"" + Encode12str(user.GetProperty().realName) + "\"/>";
115 if (user.GetProperty().group.ModificationTime() > lastTime)
116 answer += "<Group value=\"" + Encode12str(user.GetProperty().group) + "\"/>";
117 if (user.GetConnectedModificationTime() > lastTime)
118 answer += std::string("<Status value=\"") + (user.GetConnected() ? "1" : "0") + "\"/>";
119 if (user.GetProperty().alwaysOnline.ModificationTime() > lastTime)
120 answer += std::string("<AOnline value=\"") + (user.GetProperty().alwaysOnline.Get() ? "1" : "0") + "\"/>";
121 if (user.GetCurrIPModificationTime() > lastTime)
122 answer += "<CurrIP value=\"" + inet_ntostring(user.GetCurrIP()) + "\"/>";
123 if (user.GetPingTime() > lastTime)
124 answer += "<PingTime value=\"" + x2str(user.GetPingTime()) + "\"/>";
125 if (user.GetProperty().ips.ModificationTime() > lastTime)
126 answer += "<IP value=\"" + user.GetProperty().ips.Get().GetIpStr() + "\"/>";
129 const DIR_TRAFF & upload(user.GetProperty().up.Get());
130 const DIR_TRAFF & download(user.GetProperty().down.Get());
131 if (user.GetProperty().up.ModificationTime() > lastTime)
132 for (size_t j = 0; j < DIR_NUM; j++)
133 answer += " MU" + x2str(j) + "=\"" + x2str(upload[j]) + "\"";
134 if (user.GetProperty().down.ModificationTime() > lastTime)
135 for (size_t j = 0; j < DIR_NUM; j++)
136 answer += " MD" + x2str(j) + "=\"" + x2str(download[j]) + "\"";
137 if (user.GetSessionUploadModificationTime() > lastTime)
138 for (size_t j = 0; j < DIR_NUM; j++)
139 answer += " SU" + x2str(j) + "=\"" + x2str(user.GetSessionUpload()[j]) + "\"";
140 if (user.GetSessionDownloadModificationTime() > lastTime)
141 for (size_t j = 0; j < DIR_NUM; j++)
142 answer += " SD" + x2str(j) + "=\"" + x2str(user.GetSessionDownload()[j]) + "\"";
145 if (user.GetProperty().disabled.ModificationTime() > lastTime)
146 answer += std::string("<Down value=\"") + (user.GetProperty().disabled.Get() ? "1" : "0") + "\"/>";
147 if (user.GetProperty().disabledDetailStat.ModificationTime() > lastTime)
148 answer += std::string("<DisableDetailStat value=\"") + (user.GetProperty().disabledDetailStat.Get() ? "1" : "0") + "\"/>";
149 if (user.GetProperty().passive.ModificationTime() > lastTime)
150 answer += std::string("<Passive value=\"") + (user.GetProperty().passive.Get() ? "1" : "0") + "\"/>";
151 if (user.GetProperty().lastCashAdd.ModificationTime() > lastTime)
152 answer += "<LastCash value=\"" + x2str(user.GetProperty().lastCashAdd.Get()) + "\"/>";
153 if (user.GetProperty().lastCashAddTime.ModificationTime() > lastTime)
154 answer += "<LastTimeCash value=\"" + x2str(user.GetProperty().lastCashAddTime.Get()) + "\"/>";
155 if (user.GetProperty().lastActivityTime.ModificationTime() > lastTime)
156 answer += "<LastActivityTime value=\"" + x2str(user.GetProperty().lastActivityTime.Get()) + "\"/>";
157 if (user.GetProperty().creditExpire.ModificationTime() > lastTime)
158 answer += "<CreditExpire value=\"" + x2str(user.GetProperty().creditExpire.Get()) + "\"/>";
162 answer += "<AuthorizedBy>";
163 std::vector<std::string> list(user.GetAuthorizers());
164 for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
165 answer += "<Auth name=\"" + *it + "\"/>";
166 answer += "</AuthorizedBy>";
174 } // namespace anonymous
176 int GET_USERS::Start(void *, const char * el, const char ** attr)
178 if (strcasecmp(el, m_tag.c_str()) != 0)
180 printfd(__FILE__, "Got wrong tag: '%s' instead of '%s'\n", el, m_tag.c_str());
184 while (attr && *attr && *(attr + 1))
186 if (strcasecmp(*attr, "LastUpdate") == 0)
187 str2x(*(attr + 1), m_lastUserUpdateTime);
194 void GET_USERS::CreateAnswer()
196 int h = m_users.OpenSearch();
199 if (m_lastUserUpdateTime > 0)
200 m_answer = "<Users LastUpdate=\"" + x2str(time(NULL)) + "\">";
202 m_answer = "<Users>";
206 while (m_users.SearchNext(h, &u) == 0)
207 m_answer += UserToXML(*u, true, m_currAdmin.GetPriv()->userConf || m_currAdmin.GetPriv()->userPasswd, m_lastUserUpdateTime);
209 m_users.CloseSearch(h);
211 m_answer += "</Users>";
214 int GET_USER::Start(void *, const char * el, const char ** attr)
216 if (strcasecmp(el, m_tag.c_str()) != 0)
226 void GET_USER::CreateAnswer()
230 if (m_users.FindByName(m_login, &u))
231 m_answer = "<User result=\"error\" reason=\"User not found.\"/>";
233 m_answer = UserToXML(*u, false, m_currAdmin.GetPriv()->userConf || m_currAdmin.GetPriv()->userPasswd);
236 int ADD_USER::Start(void *, const char * el, const char ** attr)
242 if (strcasecmp(el, m_tag.c_str()) == 0)
247 if (strcasecmp(el, "login") == 0)
256 void ADD_USER::CreateAnswer()
258 if (m_users.Exists(m_login))
259 m_answer = "<" + m_tag + " result=\"error\" reason=\"User '" + m_login + "' exists.\"/>";
260 else if (m_users.Add(m_login, &m_currAdmin) == 0)
261 m_answer = "<" + m_tag + " result=\"ok\"/>";
263 m_answer = "<" + m_tag + " result=\"error\" reason=\"Access denied\"/>";
266 int CHG_USER::Start(void *, const char * el, const char ** attr)
272 if (strcasecmp(el, m_tag.c_str()) == 0)
277 if (strcasecmp(el, "login") == 0)
283 if (strcasecmp(el, "ip") == 0)
285 m_ucr.ips = StrToIPS(attr[1]);
289 if (strcasecmp(el, "password") == 0)
291 m_ucr.password = attr[1];
295 if (strcasecmp(el, "address") == 0)
297 m_ucr.address = Decode21str(attr[1]);
301 if (strcasecmp(el, "aonline") == 0)
303 m_ucr.alwaysOnline = (*(attr[1]) != '0');
307 if (strcasecmp(el, "cash") == 0)
309 if (attr[2] && (strcasecmp(attr[2], "msg") == 0))
310 m_cashMsg = Decode21str(attr[3]);
313 if (strtodouble2(attr[1], cash) == 0)
316 m_cashMustBeAdded = (strcasecmp(attr[0], "add") == 0);
321 if (strcasecmp(el, "CreditExpire") == 0)
323 long int creditExpire = 0;
324 if (str2x(attr[1], creditExpire) == 0)
325 m_ucr.creditExpire = (time_t)creditExpire;
330 if (strcasecmp(el, "credit") == 0)
333 if (strtodouble2(attr[1], credit) == 0)
334 m_ucr.credit = credit;
338 if (strcasecmp(el, "freemb") == 0)
341 if (strtodouble2(attr[1], freeMb) == 0)
342 m_usr.freeMb = freeMb;
346 if (strcasecmp(el, "down") == 0)
349 if (str2x(attr[1], down) == 0)
350 m_ucr.disabled = down;
354 if (strcasecmp(el, "DisableDetailStat") == 0)
356 int disabledDetailStat = 0;
357 if (str2x(attr[1], disabledDetailStat) == 0)
358 m_ucr.disabledDetailStat = disabledDetailStat;
362 if (strcasecmp(el, "email") == 0)
364 m_ucr.email = Decode21str(attr[1]);
368 for (int i = 0; i < USERDATA_NUM; i++)
371 sprintf(name, "userdata%d", i);
372 if (strcasecmp(el, name) == 0)
374 m_ucr.userdata[i] = Decode21str(attr[1]);
379 if (strcasecmp(el, "group") == 0)
381 m_ucr.group = Decode21str(attr[1]);
385 if (strcasecmp(el, "note") == 0)
387 m_ucr.note = Decode21str(attr[1]);
391 if (strcasecmp(el, "passive") == 0)
394 if (str2x(attr[1], passive) == 0)
395 m_ucr.passive = passive;
399 if (strcasecmp(el, "phone") == 0)
401 m_ucr.phone = Decode21str(attr[1]);
405 if (strcasecmp(el, "Name") == 0)
407 m_ucr.realName = Decode21str(attr[1]);
411 if (strcasecmp(el, "traff") == 0)
416 int dir = attr[j][2] - '0';
418 if (strncasecmp(attr[j], "md", 2) == 0)
421 str2x(attr[j + 1], t);
424 if (strncasecmp(attr[j], "mu", 2) == 0)
427 str2x(attr[j + 1], t);
435 if (strcasecmp(el, "tariff") == 0)
437 if (strcasecmp(attr[0], "now") == 0)
438 m_ucr.tariffName = attr[1];
440 if (strcasecmp(attr[0], "delayed") == 0)
441 m_ucr.nextTariff = attr[1];
449 void CHG_USER::CreateAnswer()
451 if (ApplyChanges() == 0)
452 m_answer = "<" + m_tag + " result=\"ok\"/>";
454 m_answer = "<" + m_tag + " result=\"error\"/>";
457 int CHG_USER::ApplyChanges()
459 printfd(__FILE__, "PARSER_CHG_USER::ApplyChanges()\n");
462 if (m_users.FindByName(m_login, &u))
466 bool alwaysOnline = u->GetProperty().alwaysOnline;
467 if (!m_ucr.alwaysOnline.empty())
470 alwaysOnline = m_ucr.alwaysOnline.const_data();
472 bool onlyOneIP = u->GetProperty().ips.ConstData().OnlyOneIP();
473 if (!m_ucr.ips.empty())
476 onlyOneIP = m_ucr.ips.const_data().OnlyOneIP();
479 if (check && alwaysOnline && !onlyOneIP)
481 printfd(__FILE__, "Requested change leads to a forbidden state: AlwaysOnline with multiple IP's\n");
482 GetStgLogger()("%s Requested change leads to a forbidden state: AlwaysOnline with multiple IP's", m_currAdmin.GetLogStr().c_str());
486 for (size_t i = 0; i < m_ucr.ips.const_data().Count(); ++i)
489 uint32_t ip = m_ucr.ips.const_data().operator[](i).ip;
490 if (m_users.IsIPInUse(ip, m_login, &user))
492 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());
493 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());
498 if (!m_ucr.ips.empty())
499 if (!u->GetProperty().ips.Set(m_ucr.ips.const_data(), &m_currAdmin, m_login, &m_store))
502 if (!m_ucr.alwaysOnline.empty())
503 if (!u->GetProperty().alwaysOnline.Set(m_ucr.alwaysOnline.const_data(),
504 &m_currAdmin, m_login, &m_store))
507 if (!m_ucr.address.empty())
508 if (!u->GetProperty().address.Set(m_ucr.address.const_data(), &m_currAdmin, m_login, &m_store))
511 if (!m_ucr.creditExpire.empty())
512 if (!u->GetProperty().creditExpire.Set(m_ucr.creditExpire.const_data(),
513 &m_currAdmin, m_login, &m_store))
516 if (!m_ucr.credit.empty())
517 if (!u->GetProperty().credit.Set(m_ucr.credit.const_data(), &m_currAdmin, m_login, &m_store))
520 if (!m_usr.freeMb.empty())
521 if (!u->GetProperty().freeMb.Set(m_usr.freeMb.const_data(), &m_currAdmin, m_login, &m_store))
524 if (!m_ucr.disabled.empty())
525 if (!u->GetProperty().disabled.Set(m_ucr.disabled.const_data(), &m_currAdmin, m_login, &m_store))
528 if (!m_ucr.disabledDetailStat.empty())
529 if (!u->GetProperty().disabledDetailStat.Set(m_ucr.disabledDetailStat.const_data(), &m_currAdmin, m_login, &m_store))
532 if (!m_ucr.email.empty())
533 if (!u->GetProperty().email.Set(m_ucr.email.const_data(), &m_currAdmin, m_login, &m_store))
536 if (!m_ucr.group.empty())
537 if (!u->GetProperty().group.Set(m_ucr.group.const_data(), &m_currAdmin, m_login, &m_store))
540 if (!m_ucr.note.empty())
541 if (!u->GetProperty().note.Set(m_ucr.note.const_data(), &m_currAdmin, m_login, &m_store))
544 std::vector<USER_PROPERTY_LOGGED<std::string> *> userdata;
545 userdata.push_back(u->GetProperty().userdata0.GetPointer());
546 userdata.push_back(u->GetProperty().userdata1.GetPointer());
547 userdata.push_back(u->GetProperty().userdata2.GetPointer());
548 userdata.push_back(u->GetProperty().userdata3.GetPointer());
549 userdata.push_back(u->GetProperty().userdata4.GetPointer());
550 userdata.push_back(u->GetProperty().userdata5.GetPointer());
551 userdata.push_back(u->GetProperty().userdata6.GetPointer());
552 userdata.push_back(u->GetProperty().userdata7.GetPointer());
553 userdata.push_back(u->GetProperty().userdata8.GetPointer());
554 userdata.push_back(u->GetProperty().userdata9.GetPointer());
556 for (int i = 0; i < (int)userdata.size(); i++)
557 if (!m_ucr.userdata[i].empty())
558 if(!userdata[i]->Set(m_ucr.userdata[i].const_data(), &m_currAdmin, m_login, &m_store))
561 if (!m_ucr.passive.empty())
562 if (!u->GetProperty().passive.Set(m_ucr.passive.const_data(), &m_currAdmin, m_login, &m_store))
565 if (!m_ucr.password.empty())
566 if (!u->GetProperty().password.Set(m_ucr.password.const_data(), &m_currAdmin, m_login, &m_store))
569 if (!m_ucr.phone.empty())
570 if (!u->GetProperty().phone.Set(m_ucr.phone.const_data(), &m_currAdmin, m_login, &m_store))
573 if (!m_ucr.realName.empty())
574 if (!u->GetProperty().realName.Set(m_ucr.realName.const_data(), &m_currAdmin, m_login, &m_store))
577 if (!m_usr.cash.empty())
579 if (m_cashMustBeAdded)
581 if (!u->GetProperty().cash.Set(m_usr.cash.const_data() + u->GetProperty().cash,
590 if (!u->GetProperty().cash.Set(m_usr.cash.const_data(), &m_currAdmin, m_login, &m_store, m_cashMsg))
595 if (!m_ucr.tariffName.empty())
597 const TARIFF * tariff = u->GetTariff();
598 const TARIFF * newTariff = m_tariffs.FindByName(m_ucr.tariffName.const_data());
599 if (m_tariffs.FindByName(m_ucr.tariffName.const_data()))
601 if (!u->GetProperty().tariffName.Set(m_ucr.tariffName.const_data(), &m_currAdmin, m_login, &m_store))
603 u->ResetNextTariff();
607 //WriteServLog("SetUser: Tariff %s not found", ud.conf.tariffName.c_str());
612 if (!m_ucr.nextTariff.empty())
614 if (m_tariffs.FindByName(m_ucr.nextTariff.const_data()))
616 if (!u->GetProperty().nextTariff.Set(m_ucr.nextTariff.const_data(), &m_currAdmin, m_login, &m_store))
621 //WriteServLog("SetUser: Tariff %s not found", ud.conf.tariffName.c_str());
626 DIR_TRAFF up = u->GetProperty().up;
627 DIR_TRAFF down = u->GetProperty().down;
630 for (int i = 0; i < DIR_NUM; i++)
632 if (!m_upr[i].empty())
634 up[i] = m_upr[i].data();
637 if (!m_downr[i].empty())
639 down[i] = m_downr[i].data();
645 if (!u->GetProperty().up.Set(up, &m_currAdmin, m_login, &m_store))
649 if (!u->GetProperty().down.Set(down, &m_currAdmin, m_login, &m_store))
658 int DEL_USER::Start(void *, const char *el, const char **attr)
661 if (strcasecmp(el, m_tag.c_str()) == 0)
663 if (attr[0] == NULL || attr[1] == NULL)
665 //CreateAnswer("Parameters error!");
670 if (m_users.FindByName(attr[1], &u))
682 int DEL_USER::End(void *, const char *el)
684 if (strcasecmp(el, m_tag.c_str()) == 0)
687 m_users.Del(u->GetLogin(), &m_currAdmin);
694 void DEL_USER::CreateAnswer()
697 m_answer = "<" + m_tag + " value=\"error\" reason=\"User not found\"/>";
699 m_answer = "<" + m_tag + " value=\"ok\"/>";
702 int CHECK_USER::Start(void *, const char *el, const char **attr)
704 if (strcasecmp(el, m_tag.c_str()) == 0)
706 if (attr[0] == NULL || attr[1] == NULL ||
707 attr[2] == NULL || attr[3] == NULL)
709 CreateAnswer("Invalid parameters.");
710 printfd(__FILE__, "PARSER_CHECK_USER - attr err\n");
715 if (m_users.FindByName(attr[1], &user))
717 CreateAnswer("User not found.");
718 printfd(__FILE__, "PARSER_CHECK_USER - login err\n");
722 if (strcmp(user->GetProperty().password.Get().c_str(), attr[3]))
724 CreateAnswer("Wrong password.");
725 printfd(__FILE__, "PARSER_CHECK_USER - passwd err\n");
735 int CHECK_USER::End(void *, const char *el)
737 if (strcasecmp(el, m_tag.c_str()) == 0)
742 void CHECK_USER::CreateAnswer(const char * error)
745 m_answer = "<" + m_tag + " value=\"Err\" reason=\"" + error + "\"/>";
747 m_answer = "<" + m_tag + " value=\"Ok\"/>";