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"
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;
43 std::string UserToXML(const USER & user, bool loginInStart, bool showPass, time_t lastTime = 0)
48 answer += "<User result=\"ok\">";
50 answer += "<User result=\"ok\" login=\"" + user.GetLogin() + "\">";
52 answer += "<Login value=\"" + user.GetLogin() + "\"/>";
54 if (user.GetProperty().password.ModificationTime() > lastTime)
57 answer += "<Password value=\"" + user.GetProperty().password.Get() + "\" />";
59 answer += "<Password value=\"++++++\"/>";
62 if (user.GetProperty().cash.ModificationTime() > lastTime)
63 answer += "<Cash value=\"" + x2str(user.GetProperty().cash.Get()) + "\"/>";
64 if (user.GetProperty().freeMb.ModificationTime() > lastTime)
65 answer += "<FreeMb value=\"" + x2str(user.GetProperty().freeMb.Get()) + "\"/>";
66 if (user.GetProperty().credit.ModificationTime() > lastTime)
67 answer += "<Credit value=\"" + x2str(user.GetProperty().credit.Get()) + "\"/>";
69 if (user.GetProperty().nextTariff.Get() != "")
71 if (user.GetProperty().tariffName.ModificationTime() > lastTime ||
72 user.GetProperty().nextTariff.ModificationTime() > lastTime)
73 answer += "<Tariff value=\"" + user.GetProperty().tariffName.Get() + "/" + user.GetProperty().nextTariff.Get() + "\"/>";
77 if (user.GetProperty().tariffName.ModificationTime() > lastTime)
78 answer += "<Tariff value=\"" + user.GetProperty().tariffName.Get() + "\"/>";
81 if (user.GetProperty().note.ModificationTime() > lastTime)
82 answer += "<Note value=\"" + Encode12str(user.GetProperty().note) + "\"/>";
83 if (user.GetProperty().phone.ModificationTime() > lastTime)
84 answer += "<Phone value=\"" + Encode12str(user.GetProperty().phone) + "\"/>";
85 if (user.GetProperty().address.ModificationTime() > lastTime)
86 answer += "<Address value=\"" + Encode12str(user.GetProperty().address) + "\"/>";
87 if (user.GetProperty().email.ModificationTime() > lastTime)
88 answer += "<Email value=\"" + Encode12str(user.GetProperty().email) + "\"/>";
90 std::vector<const USER_PROPERTY_LOGGED<std::string> *> userdata;
91 userdata.push_back(user.GetProperty().userdata0.GetPointer());
92 userdata.push_back(user.GetProperty().userdata1.GetPointer());
93 userdata.push_back(user.GetProperty().userdata2.GetPointer());
94 userdata.push_back(user.GetProperty().userdata3.GetPointer());
95 userdata.push_back(user.GetProperty().userdata4.GetPointer());
96 userdata.push_back(user.GetProperty().userdata5.GetPointer());
97 userdata.push_back(user.GetProperty().userdata6.GetPointer());
98 userdata.push_back(user.GetProperty().userdata7.GetPointer());
99 userdata.push_back(user.GetProperty().userdata8.GetPointer());
100 userdata.push_back(user.GetProperty().userdata9.GetPointer());
102 for (size_t i = 0; i < userdata.size(); i++)
103 if (userdata[i]->ModificationTime() > lastTime)
104 answer += "<UserData" + x2str(i) + " value=\"" + Encode12str(userdata[i]->Get()) + "\" />";
106 if (user.GetProperty().realName.ModificationTime() > lastTime)
107 answer += "<Name value=\"" + Encode12str(user.GetProperty().realName) + "\"/>";
108 if (user.GetProperty().group.ModificationTime() > lastTime)
109 answer += "<Group value=\"" + Encode12str(user.GetProperty().group) + "\"/>";
110 if (user.GetConnectedModificationTime() > lastTime)
111 answer += std::string("<Status value=\"") + (user.GetConnected() ? "1" : "0") + "\"/>";
112 if (user.GetProperty().alwaysOnline.ModificationTime() > lastTime)
113 answer += std::string("<AOnline value=\"") + (user.GetProperty().alwaysOnline.Get() ? "1" : "0") + "\"/>";
114 if (user.GetCurrIPModificationTime() > lastTime)
115 answer += "<CurrIP value=\"" + inet_ntostring(user.GetCurrIP()) + "\"/>";
116 if (user.GetPingTime() > lastTime)
117 answer += "<PingTime value=\"" + x2str(user.GetPingTime()) + "\"/>";
118 if (user.GetProperty().ips.ModificationTime() > lastTime)
119 answer += "<IP value=\"" + user.GetProperty().ips.Get().GetIpStr() + "\"/>";
122 const DIR_TRAFF & upload(user.GetProperty().down.Get());
123 const DIR_TRAFF & download(user.GetProperty().up.Get());
124 if (user.GetProperty().up.ModificationTime() > lastTime)
125 for (size_t j = 0; j < DIR_NUM; j++)
126 answer += " MU" + x2str(j) + "=\"" + x2str(upload[j]) + "\"";
127 if (user.GetProperty().down.ModificationTime() > lastTime)
128 for (size_t j = 0; j < DIR_NUM; j++)
129 answer += " MD" + x2str(j) + "=\"" + x2str(download[j]) + "\"";
130 if (user.GetSessionUploadModificationTime() > lastTime)
131 for (size_t j = 0; j < DIR_NUM; j++)
132 answer += " SU" + x2str(j) + "=\"" + x2str(user.GetSessionUpload()[j]) + "\"";
133 if (user.GetSessionDownloadModificationTime() > lastTime)
134 for (size_t j = 0; j < DIR_NUM; j++)
135 answer += " SD" + x2str(j) + "=\"" + x2str(user.GetSessionDownload()[j]) + "\"";
138 if (user.GetProperty().disabled.ModificationTime() > lastTime)
139 answer += std::string("<Down value=\"") + (user.GetProperty().disabled.Get() ? "1" : "0") + "\"/>";
140 if (user.GetProperty().disabledDetailStat.ModificationTime() > lastTime)
141 answer += std::string("<DisableDetailStat value=\"") + (user.GetProperty().disabledDetailStat.Get() ? "1" : "0") + "\"/>";
142 if (user.GetProperty().passive.ModificationTime() > lastTime)
143 answer += std::string("<Passive value=\"") + (user.GetProperty().passive.Get() ? "1" : "0") + "\"/>";
144 if (user.GetProperty().lastCashAdd.ModificationTime() > lastTime)
145 answer += "<LastCash value=\"" + x2str(user.GetProperty().lastCashAdd.Get()) + "\"/>";
146 if (user.GetProperty().lastCashAddTime.ModificationTime() > lastTime)
147 answer += "<LastTimeCash value=\"" + x2str(user.GetProperty().lastCashAddTime.Get()) + "\"/>";
148 if (user.GetProperty().lastActivityTime.ModificationTime() > lastTime)
149 answer += "<LastActivityTime value=\"" + x2str(user.GetProperty().lastActivityTime.Get()) + "\"/>";
150 if (user.GetProperty().creditExpire.ModificationTime() > lastTime)
151 answer += "<CreditExpire value=\"" + x2str(user.GetProperty().creditExpire.Get()) + "\"/>";
155 answer += "<AuthorizedBy>";
156 std::vector<std::string> list(user.GetAuthorizers());
157 for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
158 answer += "<Auth name=\"" + *it + "\"/>";
159 answer += "</AuthorizedBy>";
167 } // namespace anonymous
169 int GET_USER::Start(void *, const char * el, const char ** attr)
171 if (strcasecmp(el, tag.c_str()) != 0)
181 void GET_USER::CreateAnswer()
185 if (m_users.FindByName(login, &u))
186 answer = "<" + tag + " result=\"error\" reason=\"User not found.\"/>";
188 answer = UserToXML(*u, false, currAdmin.GetPriv()->userConf || currAdmin.GetPriv()->userPasswd);
191 int GET_USERS::Start(void *, const char * el, const char ** attr)
193 lastUpdateFound = false;
194 if (strcasecmp(el, tag.c_str()) != 0)
197 while (attr && *attr && *(attr + 1))
199 if (strcasecmp(*attr, "LastUpdate") == 0)
201 if (str2x(*(attr + 1), lastUserUpdateTime) == 0)
202 lastUpdateFound = true;
210 void GET_USERS::CreateAnswer()
212 int h = m_users.OpenSearch();
215 printfd(__FILE__, "m_users.OpenSearch() error\n");
220 answer = "<" + tag + " LastUpdate=\"" + x2str(time(NULL)) + "\">";
222 answer = GetOpenTag();
226 while (m_users.SearchNext(h, &u) == 0)
227 answer += UserToXML(*u, true, currAdmin.GetPriv()->userConf || currAdmin.GetPriv()->userPasswd, lastUserUpdateTime);
229 m_users.CloseSearch(h);
231 answer += GetCloseTag();
234 int ADD_USER::Start(void *, const char * el, const char ** attr)
240 if (strcasecmp(el, tag.c_str()) == 0)
245 if (strcasecmp(el, "login") == 0)
254 int ADD_USER::End(void *, const char *el)
258 if (strcasecmp(el, tag.c_str()) == 0)
267 void ADD_USER::CreateAnswer()
269 if (CheckUserData() == 0)
270 answer = "<" + tag + " result=\"ok\"/>";
272 answer = "<" + tag + " result=\"error\" reason=\"Access denied\"/>";
275 int ADD_USER::CheckUserData()
278 if (m_users.FindByName(login, &u))
279 return m_users.Add(login, &currAdmin);
284 CHG_USER::CHG_USER(const ADMIN & admin, USERS & users, const TARIFFS & tariffs)
285 : BASE_PARSER(admin, "SetUser"),
288 usr(new USER_STAT_RES),
289 ucr(new USER_CONF_RES),
290 upr(new RESETABLE<uint64_t>[DIR_NUM]),
291 downr(new RESETABLE<uint64_t>[DIR_NUM]),
292 cashMustBeAdded(false),
297 CHG_USER::~CHG_USER()
305 int CHG_USER::Start(void *, const char * el, const char ** attr)
311 if (strcasecmp(el, tag.c_str()) == 0)
316 if (strcasecmp(el, "login") == 0)
322 if (strcasecmp(el, "ip") == 0)
326 ucr->ips = StrToIPS(attr[1]);
330 printfd(__FILE__, "StrToIPS Error!\n");
334 if (strcasecmp(el, "password") == 0)
336 ucr->password = attr[1];
340 if (strcasecmp(el, "address") == 0)
342 ucr->address = Decode21str(attr[1]);
346 if (strcasecmp(el, "aonline") == 0)
348 ucr->alwaysOnline = (*(attr[1]) != '0');
352 if (strcasecmp(el, "cash") == 0)
354 if (attr[2] && (strcasecmp(attr[2], "msg") == 0))
355 cashMsg = Decode21str(attr[3]);
358 if (strtodouble2(attr[1], cash) == 0)
361 if (strcasecmp(attr[0], "set") == 0)
362 cashMustBeAdded = false;
364 if (strcasecmp(attr[0], "add") == 0)
365 cashMustBeAdded = true;
370 if (strcasecmp(el, "CreditExpire") == 0)
372 long int creditExpire = 0;
373 if (str2x(attr[1], creditExpire) == 0)
374 ucr->creditExpire = (time_t)creditExpire;
379 if (strcasecmp(el, "credit") == 0)
382 if (strtodouble2(attr[1], credit) == 0)
383 ucr->credit = credit;
387 if (strcasecmp(el, "freemb") == 0)
390 if (strtodouble2(attr[1], freeMb) == 0)
391 usr->freeMb = freeMb;
395 if (strcasecmp(el, "down") == 0)
398 if (str2x(attr[1], down) == 0)
399 ucr->disabled = down;
403 if (strcasecmp(el, "DisableDetailStat") == 0)
405 int disabledDetailStat = 0;
406 if (str2x(attr[1], disabledDetailStat) == 0)
407 ucr->disabledDetailStat = disabledDetailStat;
411 if (strcasecmp(el, "email") == 0)
413 ucr->email = Decode21str(attr[1]);
417 for (int i = 0; i < USERDATA_NUM; i++)
420 sprintf(name, "userdata%d", i);
421 if (strcasecmp(el, name) == 0)
423 ucr->userdata[i] = Decode21str(attr[1]);
428 if (strcasecmp(el, "group") == 0)
430 ucr->group = Decode21str(attr[1]);
434 if (strcasecmp(el, "note") == 0)
436 ucr->note = Decode21str(attr[1]);
440 if (strcasecmp(el, "passive") == 0)
443 if (str2x(attr[1], passive) == 0)
444 ucr->passive = passive;
448 if (strcasecmp(el, "phone") == 0)
450 ucr->phone = Decode21str(attr[1]);
454 if (strcasecmp(el, "Name") == 0)
456 ucr->realName = Decode21str(attr[1]);
460 if (strcasecmp(el, "traff") == 0)
465 int dir = attr[j][2] - '0';
467 if (strncasecmp(attr[j], "md", 2) == 0)
470 str2x(attr[j + 1], t);
473 if (strncasecmp(attr[j], "mu", 2) == 0)
476 str2x(attr[j + 1], t);
484 if (strcasecmp(el, "tariff") == 0)
486 if (strcasecmp(attr[0], "now") == 0)
487 ucr->tariffName = attr[1];
489 if (strcasecmp(attr[0], "delayed") == 0)
490 ucr->nextTariff = attr[1];
498 int CHG_USER::End(void *, const char *el)
502 if (strcasecmp(el, tag.c_str()) != 0)
513 void CHG_USER::CreateAnswer()
516 answer = "<" + tag + " result=\"ok\"/>";
518 answer = "<" + tag + " result=\"error\"/>";
521 int CHG_USER::ApplyChanges()
523 printfd(__FILE__, "PARSER_CHG_USER::ApplyChanges()\n");
527 if (m_users.FindByName(login, &u))
534 bool alwaysOnline = u->GetProperty().alwaysOnline;
535 if (!ucr->alwaysOnline.empty())
538 alwaysOnline = ucr->alwaysOnline.const_data();
540 bool onlyOneIP = u->GetProperty().ips.ConstData().OnlyOneIP();
541 if (!ucr->ips.empty())
544 onlyOneIP = ucr->ips.const_data().OnlyOneIP();
547 if (check && alwaysOnline && !onlyOneIP)
549 printfd(__FILE__, "Requested change leads to a forbidden state: AlwaysOnline with multiple IP's\n");
550 GetStgLogger()("%s Requested change leads to a forbidden state: AlwaysOnline with multiple IP's", currAdmin.GetLogStr().c_str());
555 for (size_t i = 0; i < ucr->ips.const_data().Count(); ++i)
558 uint32_t ip = ucr->ips.const_data().operator[](i).ip;
559 if (m_users.IsIPInUse(ip, login, &user))
561 printfd(__FILE__, "Trying to assign an IP %s to '%s' that is already in use by '%s'\n", inet_ntostring(ip).c_str(), login.c_str(), user->GetLogin().c_str());
562 GetStgLogger()("%s trying to assign an IP %s to '%s' that is currently in use by '%s'", currAdmin.GetLogStr().c_str(), inet_ntostring(ip).c_str(), login.c_str(), user->GetLogin().c_str());
568 if (!ucr->ips.empty())
569 if (!u->GetProperty().ips.Set(ucr->ips.const_data(), &currAdmin, login, store))
572 if (!ucr->alwaysOnline.empty())
573 if (!u->GetProperty().alwaysOnline.Set(ucr->alwaysOnline.const_data(),
574 &currAdmin, login, store))
577 if (!ucr->address.empty())
578 if (!u->GetProperty().address.Set(ucr->address.const_data(), &currAdmin, login, store))
581 if (!ucr->creditExpire.empty())
582 if (!u->GetProperty().creditExpire.Set(ucr->creditExpire.const_data(),
583 &currAdmin, login, store))
586 if (!ucr->credit.empty())
587 if (!u->GetProperty().credit.Set(ucr->credit.const_data(), &currAdmin, login, store))
590 if (!usr->freeMb.empty())
591 if (!u->GetProperty().freeMb.Set(usr->freeMb.const_data(), &currAdmin, login, store))
594 if (!ucr->disabled.empty())
595 if (!u->GetProperty().disabled.Set(ucr->disabled.const_data(), &currAdmin, login, store))
598 if (!ucr->disabledDetailStat.empty())
599 if (!u->GetProperty().disabledDetailStat.Set(ucr->disabledDetailStat.const_data(), &currAdmin, login, store))
602 if (!ucr->email.empty())
603 if (!u->GetProperty().email.Set(ucr->email.const_data(), &currAdmin, login, store))
606 if (!ucr->group.empty())
607 if (!u->GetProperty().group.Set(ucr->group.const_data(), &currAdmin, login, store))
610 if (!ucr->note.empty())
611 if (!u->GetProperty().note.Set(ucr->note.const_data(), &currAdmin, login, store))
614 std::vector<USER_PROPERTY_LOGGED<std::string> *> userdata;
615 userdata.push_back(u->GetProperty().userdata0.GetPointer());
616 userdata.push_back(u->GetProperty().userdata1.GetPointer());
617 userdata.push_back(u->GetProperty().userdata2.GetPointer());
618 userdata.push_back(u->GetProperty().userdata3.GetPointer());
619 userdata.push_back(u->GetProperty().userdata4.GetPointer());
620 userdata.push_back(u->GetProperty().userdata5.GetPointer());
621 userdata.push_back(u->GetProperty().userdata6.GetPointer());
622 userdata.push_back(u->GetProperty().userdata7.GetPointer());
623 userdata.push_back(u->GetProperty().userdata8.GetPointer());
624 userdata.push_back(u->GetProperty().userdata9.GetPointer());
626 for (int i = 0; i < (int)userdata.size(); i++)
627 if (!ucr->userdata[i].empty())
628 if(!userdata[i]->Set(ucr->userdata[i].const_data(), &currAdmin, login, store))
631 if (!ucr->passive.empty())
632 if (!u->GetProperty().passive.Set(ucr->passive.const_data(), &currAdmin, login, store))
635 if (!ucr->password.empty())
636 if (!u->GetProperty().password.Set(ucr->password.const_data(), &currAdmin, login, store))
639 if (!ucr->phone.empty())
640 if (!u->GetProperty().phone.Set(ucr->phone.const_data(), &currAdmin, login, store))
643 if (!ucr->realName.empty())
644 if (!u->GetProperty().realName.Set(ucr->realName.const_data(), &currAdmin, login, store))
648 if (!usr->cash.empty())
651 if (!u->GetProperty().cash.Set(usr->cash.const_data() + u->GetProperty().cash,
658 if (!u->GetProperty().cash.Set(usr->cash.const_data(), &currAdmin, login, store, cashMsg))
663 if (!ucr->tariffName.empty())
665 if (m_tariffs.FindByName(ucr->tariffName.const_data()))
667 if (!u->GetProperty().tariffName.Set(ucr->tariffName.const_data(), &currAdmin, login, store))
669 u->ResetNextTariff();
673 //WriteServLog("SetUser: Tariff %s not found", ud.conf.tariffName.c_str());
678 if (!ucr->nextTariff.empty())
680 if (m_tariffs.FindByName(ucr->nextTariff.const_data()))
682 if (!u->GetProperty().nextTariff.Set(ucr->nextTariff.const_data(), &currAdmin, login, store))
687 //WriteServLog("SetUser: Tariff %s not found", ud.conf.tariffName.c_str());
692 DIR_TRAFF up = u->GetProperty().up;
693 DIR_TRAFF down = u->GetProperty().down;
696 for (int i = 0; i < DIR_NUM; i++)
700 up[i] = upr[i].data();
703 if (!downr[i].empty())
705 down[i] = downr[i].data();
711 if (!u->GetProperty().up.Set(up, &currAdmin, login, store))
715 if (!u->GetProperty().down.Set(down, &currAdmin, login, store))
724 int DEL_USER::Start(void *, const char *el, const char **attr)
727 if (strcasecmp(el, tag.c_str()) == 0)
729 if (attr[0] == NULL || attr[1] == NULL)
731 //CreateAnswer("Parameters error!");
736 if (m_users.FindByName(attr[1], &u))
748 int DEL_USER::End(void *, const char *el)
750 if (strcasecmp(el, tag.c_str()) == 0)
753 m_users.Del(u->GetLogin(), &currAdmin);
760 void DEL_USER::CreateAnswer()
763 answer = "<" + tag + " value=\"error\" reason=\"User not found\"/>";
765 answer = "<" + tag + " value=\"ok\"/>";
768 int CHECK_USER::Start(void *, const char *el, const char **attr)
770 if (strcasecmp(el, tag.c_str()) == 0)
772 if (attr[0] == NULL || attr[1] == NULL ||
773 attr[2] == NULL || attr[3] == NULL)
775 CreateAnswer("Invalid parameters.");
776 printfd(__FILE__, "PARSER_CHECK_USER - attr err\n");
781 if (m_users.FindByName(attr[1], &user))
783 CreateAnswer("User not found.");
784 printfd(__FILE__, "PARSER_CHECK_USER - login err\n");
788 if (strcmp(user->GetProperty().password.Get().c_str(), attr[3]))
790 CreateAnswer("Wrong password.");
791 printfd(__FILE__, "PARSER_CHECK_USER - passwd err\n");
801 int CHECK_USER::End(void *, const char *el)
803 if (strcasecmp(el, tag.c_str()) == 0)
808 void CHECK_USER::CreateAnswer(const char * error)
811 answer = "<" + tag + " value=\"Err\" reason=\"" + error + "\"/>";
813 answer = "<" + tag + " value=\"Ok\"/>";